lkml.org 
[lkml]   [2016]   [Sep]   [9]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH v5 2/5] firmware: encapsulate firmware loading status
Date
From: Daniel Wagner <daniel.wagner@bmw-carit.de>

The firmware user helper code tracks the current state of the loading
process via unsigned long status and a complection in struct
firmware_buf. We only need this for the usermode helper as such we can
encapsulate all this data into its own data structure.

Cc: Ming Lei <ming.lei@canonical.com>
Cc: Luis R. Rodriguez <mcgrof@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Daniel Wagner <daniel.wagner@bmw-carit.de>
---
drivers/base/firmware_class.c | 130 +++++++++++++++++++++++++++++-------------
1 file changed, 91 insertions(+), 39 deletions(-)

diff --git a/drivers/base/firmware_class.c b/drivers/base/firmware_class.c
index 8eba1fb..821babe 100644
--- a/drivers/base/firmware_class.c
+++ b/drivers/base/firmware_class.c
@@ -91,12 +91,6 @@ static inline bool fw_is_builtin_firmware(const struct firmware *fw)
}
#endif

-enum {
- FW_STATUS_LOADING,
- FW_STATUS_DONE,
- FW_STATUS_ABORT,
-};
-
static int loading_timeout = 60; /* In seconds */

static inline long firmware_loading_timeout(void)
@@ -104,6 +98,77 @@ static inline long firmware_loading_timeout(void)
return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
}

+#ifdef CONFIG_FW_LOADER_USER_HELPER
+
+enum {
+ FW_UMH_UNKNOWN,
+ FW_UMH_LOADING,
+ FW_UMH_DONE,
+ FW_UMH_ABORTED,
+};
+
+struct fw_umh {
+ struct completion completion;
+ unsigned long status;
+};
+
+static void fw_umh_init(struct fw_umh *fw_umh)
+{
+ init_completion(&fw_umh->completion);
+ fw_umh->status = FW_UMH_UNKNOWN;
+}
+
+static int __fw_umh_check(struct fw_umh *fw_umh, unsigned long status)
+{
+ return test_bit(status, &fw_umh->status);
+}
+
+static int fw_umh_wait_timeout(struct fw_umh *fw_umh, long timeout)
+{
+ int ret;
+
+ ret = wait_for_completion_interruptible_timeout(&fw_umh->completion,
+ timeout);
+ if (ret != 0 && test_bit(FW_UMH_ABORTED, &fw_umh->status))
+ return -ENOENT;
+
+ return ret;
+}
+
+static void __fw_umh_set(struct fw_umh *fw_umh,
+ unsigned long status)
+{
+ set_bit(status, &fw_umh->status);
+
+ if (status == FW_UMH_DONE || status == FW_UMH_ABORTED) {
+ clear_bit(FW_UMH_LOADING, &fw_umh->status);
+ complete_all(&fw_umh->completion);
+ }
+}
+
+#define fw_umh_start(fw_umh) \
+ __fw_umh_set(fw_umh, FW_UMH_LOADING)
+#define fw_umh_done(fw_umh) \
+ __fw_umh_set(fw_umh, FW_UMH_DONE)
+#define fw_umh_aborted(fw_umh) \
+ __fw_umh_set(fw_umh, FW_UMH_ABORTED)
+#define fw_umh_is_loading(fw_umh) \
+ __fw_umh_check(fw_umh, FW_UMH_LOADING)
+#define fw_umh_is_done(fw_umh) \
+ __fw_umh_check(fw_umh, FW_UMH_DONE)
+#define fw_umh_is_aborted(fw_umh) \
+ __fw_umh_check(fw_umh, FW_UMH_ABORTED)
+
+#else /* CONFIG_FW_LOADER_USER_HELPER */
+
+#define fw_umh_wait_timeout(fw_st, long) 0
+
+#define fw_umh_done(fw_st)
+#define fw_umh_is_done(fw_st) true
+#define fw_umh_is_aborted(fw_st) false
+
+#endif /* !CONFIG_FW_LOADER_USER_HELPER */
+
/* firmware behavior options */
#define FW_OPT_UEVENT (1U << 0)
#define FW_OPT_NOWAIT (1U << 1)
@@ -145,13 +210,12 @@ struct firmware_cache {
struct firmware_buf {
struct kref ref;
struct list_head list;
- struct completion completion;
struct firmware_cache *fwc;
- unsigned long status;
void *data;
size_t size;
size_t allocated_size;
#ifdef CONFIG_FW_LOADER_USER_HELPER
+ struct fw_umh fw_umh;
bool is_paged_buf;
bool need_uevent;
struct page **pages;
@@ -205,8 +269,8 @@ static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
buf->fwc = fwc;
buf->data = dbuf;
buf->allocated_size = size;
- init_completion(&buf->completion);
#ifdef CONFIG_FW_LOADER_USER_HELPER
+ fw_umh_init(&buf->fw_umh);
INIT_LIST_HEAD(&buf->pending_list);
#endif

@@ -309,8 +373,7 @@ static void fw_finish_direct_load(struct device *device,
struct firmware_buf *buf)
{
mutex_lock(&fw_lock);
- set_bit(FW_STATUS_DONE, &buf->status);
- complete_all(&buf->completion);
+ fw_umh_done(&buf->fw_umh);
mutex_unlock(&fw_lock);
}

@@ -478,12 +541,11 @@ static void __fw_load_abort(struct firmware_buf *buf)
* There is a small window in which user can write to 'loading'
* between loading done and disappearance of 'loading'
*/
- if (test_bit(FW_STATUS_DONE, &buf->status))
+ if (fw_umh_is_done(&buf->fw_umh))
return;

list_del_init(&buf->pending_list);
- set_bit(FW_STATUS_ABORT, &buf->status);
- complete_all(&buf->completion);
+ fw_umh_aborted(&buf->fw_umh);
}

static void fw_load_abort(struct firmware_priv *fw_priv)
@@ -496,9 +558,6 @@ static void fw_load_abort(struct firmware_priv *fw_priv)
fw_priv->buf = NULL;
}

-#define is_fw_load_aborted(buf) \
- test_bit(FW_STATUS_ABORT, &(buf)->status)
-
static LIST_HEAD(pending_fw_head);

/* reboot notifier for avoid deadlock with usermode_lock */
@@ -598,7 +657,7 @@ static ssize_t firmware_loading_show(struct device *dev,

mutex_lock(&fw_lock);
if (fw_priv->buf)
- loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
+ loading = fw_umh_is_loading(&fw_priv->buf->fw_umh);
mutex_unlock(&fw_lock);

return sprintf(buf, "%d\n", loading);
@@ -653,23 +712,20 @@ static ssize_t firmware_loading_store(struct device *dev,
switch (loading) {
case 1:
/* discarding any previous partial load */
- if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
+ if (!fw_umh_is_done(&fw_buf->fw_umh)) {
for (i = 0; i < fw_buf->nr_pages; i++)
__free_page(fw_buf->pages[i]);
vfree(fw_buf->pages);
fw_buf->pages = NULL;
fw_buf->page_array_size = 0;
fw_buf->nr_pages = 0;
- set_bit(FW_STATUS_LOADING, &fw_buf->status);
+ fw_umh_start(&fw_buf->fw_umh);
}
break;
case 0:
- if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
+ if (fw_umh_is_loading(&fw_buf->fw_umh)) {
int rc;

- set_bit(FW_STATUS_DONE, &fw_buf->status);
- clear_bit(FW_STATUS_LOADING, &fw_buf->status);
-
/*
* Several loading requests may be pending on
* one same firmware buf, so let all requests
@@ -691,10 +747,11 @@ static ssize_t firmware_loading_store(struct device *dev,
*/
list_del_init(&fw_buf->pending_list);
if (rc) {
- set_bit(FW_STATUS_ABORT, &fw_buf->status);
+ fw_umh_aborted(&fw_buf->fw_umh);
written = rc;
+ } else {
+ fw_umh_done(&fw_buf->fw_umh);
}
- complete_all(&fw_buf->completion);
break;
}
/* fallthrough */
@@ -755,7 +812,7 @@ static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,

mutex_lock(&fw_lock);
buf = fw_priv->buf;
- if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
+ if (!buf || fw_umh_is_done(&buf->fw_umh)) {
ret_count = -ENODEV;
goto out;
}
@@ -842,7 +899,7 @@ static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,

mutex_lock(&fw_lock);
buf = fw_priv->buf;
- if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
+ if (!buf || fw_umh_is_done(&buf->fw_umh)) {
retval = -ENODEV;
goto out;
}
@@ -955,8 +1012,7 @@ static int _request_firmware_load(struct firmware_priv *fw_priv,
timeout = MAX_JIFFY_OFFSET;
}

- retval = wait_for_completion_interruptible_timeout(&buf->completion,
- timeout);
+ retval = fw_umh_wait_timeout(&buf->fw_umh, timeout);
if (retval == -ERESTARTSYS || !retval) {
mutex_lock(&fw_lock);
fw_load_abort(fw_priv);
@@ -965,7 +1021,7 @@ static int _request_firmware_load(struct firmware_priv *fw_priv,
retval = 0;
}

- if (is_fw_load_aborted(buf))
+ if (fw_umh_is_aborted(&buf->fw_umh))
retval = -EAGAIN;
else if (buf->is_paged_buf && !buf->data)
retval = -ENOMEM;
@@ -1015,29 +1071,25 @@ fw_load_from_user_helper(struct firmware *firmware, const char *name,
return -ENOENT;
}

-/* No abort during direct loading */
-#define is_fw_load_aborted(buf) false
-
#ifdef CONFIG_PM_SLEEP
static inline void kill_requests_without_uevent(void) { }
#endif

#endif /* CONFIG_FW_LOADER_USER_HELPER */

-
/* wait until the shared firmware_buf becomes ready (or error) */
static int sync_cached_firmware_buf(struct firmware_buf *buf)
{
int ret = 0;

mutex_lock(&fw_lock);
- while (!test_bit(FW_STATUS_DONE, &buf->status)) {
- if (is_fw_load_aborted(buf)) {
+ while (!fw_umh_is_done(&buf->fw_umh)) {
+ if (fw_umh_is_aborted(&buf->fw_umh)) {
ret = -ENOENT;
break;
}
mutex_unlock(&fw_lock);
- ret = wait_for_completion_interruptible(&buf->completion);
+ ret = fw_umh_wait_timeout(&buf->fw_umh, 0);
mutex_lock(&fw_lock);
}
mutex_unlock(&fw_lock);
@@ -1095,7 +1147,7 @@ static int assign_firmware_buf(struct firmware *fw, struct device *device,
struct firmware_buf *buf = fw->priv;

mutex_lock(&fw_lock);
- if (!buf->size || is_fw_load_aborted(buf)) {
+ if (!buf->size || fw_umh_is_aborted(&buf->fw_umh)) {
mutex_unlock(&fw_lock);
return -ENOENT;
}
--
2.7.4
\
 
 \ /
  Last update: 2016-09-17 09:59    [W:0.160 / U:0.108 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site