Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935408AbcKQKBI (ORCPT ); Thu, 17 Nov 2016 05:01:08 -0500 Received: from atlantic540.startdedicated.de ([188.138.9.77]:49577 "EHLO atlantic540.startdedicated.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933862AbcKQKBE (ORCPT ); Thu, 17 Nov 2016 05:01:04 -0500 From: Daniel Wagner To: linux-kernel@vger.kernel.org Cc: Ming Lei , "Luis R . Rodriguez" , Greg Kroah-Hartman , "Rafael J . Wysocki" , Daniel Vetter , Takashi Iwai , Bjorn Andersson , Arend van Spriel , Yves-Alexis Perez , Daniel Wagner Subject: [PATCH v7 1/4] firmware: refactor loading status Date: Thu, 17 Nov 2016 11:00:48 +0100 Message-Id: <1479376851-2307-2-git-send-email-wagi@monom.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1479376851-2307-1-git-send-email-wagi@monom.org> References: <1479376851-2307-1-git-send-email-wagi@monom.org> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 8971 Lines: 293 From: Daniel Wagner The firmware loader tracks the current state of the loading process via unsigned long status and a completion in struct firmware_buf. Instead of open code tracking the state, introduce data structure which encapsulate the state tracking and synchronization. While at it also separate UHM states from direct loading states, e.g. the loading_timeout is only defined when CONFIG_FW_LOADER_USER_HELPER. Cc: Ming Lei Cc: Luis R. Rodriguez Cc: Greg Kroah-Hartman Signed-off-by: Daniel Wagner --- drivers/base/firmware_class.c | 127 +++++++++++++++++++++++++++++++----------- 1 file changed, 93 insertions(+), 34 deletions(-) diff --git a/drivers/base/firmware_class.c b/drivers/base/firmware_class.c index a95e1e572697..d3b614d10d60 100644 --- a/drivers/base/firmware_class.c +++ b/drivers/base/firmware_class.c @@ -91,10 +91,11 @@ static inline bool fw_is_builtin_firmware(const struct firmware *fw) } #endif -enum { +enum fw_status { + FW_STATUS_UNKNOWN, FW_STATUS_LOADING, FW_STATUS_DONE, - FW_STATUS_ABORT, + FW_STATUS_ABORTED, }; static int loading_timeout = 60; /* In seconds */ @@ -104,6 +105,76 @@ static inline long firmware_loading_timeout(void) return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET; } +/* + * Concurrent request_firmware() for the same firmware need to be + * serialized. struct fw_state is simple state machine which hold the + * state of the firmware loading. + */ +struct fw_state { + struct completion completion; + unsigned long status; +}; + +static void fw_state_init(struct fw_state *fw_st) +{ + init_completion(&fw_st->completion); + fw_st->status = FW_STATUS_UNKNOWN; +} + +static int __fw_state_check(struct fw_state *fw_st, enum fw_status status) +{ + return test_bit(status, &fw_st->status); +} + +static long __fw_state_wait_common(struct fw_state *fw_st, long timeout) +{ + long ret; + + ret = wait_for_completion_interruptible_timeout(&fw_st->completion, + timeout); + if (ret != 0 && test_bit(FW_STATUS_ABORTED, &fw_st->status)) + return -ENOENT; + + return ret; +} + +static void __fw_state_set(struct fw_state *fw_st, + enum fw_status status) +{ + set_bit(status, &fw_st->status); + + if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED) { + clear_bit(FW_STATUS_LOADING, &fw_st->status); + complete_all(&fw_st->completion); + } +} + +#define fw_state_start(fw_st) \ + __fw_state_set(fw_st, FW_STATUS_LOADING) +#define fw_state_done(fw_st) \ + __fw_state_set(fw_st, FW_STATUS_DONE) +#define fw_state_is_done(fw_st) \ + __fw_state_check(fw_st, FW_STATUS_DONE) +#define fw_state_wait(fw_st) \ + __fw_state_wait_common(fw_st, MAX_SCHEDULE_TIMEOUT) + +#ifndef CONFIG_FW_LOADER_USER_HELPER + +#define fw_state_is_aborted(fw_st) false + +#else /* CONFIG_FW_LOADER_USER_HELPER */ + +#define fw_state_aborted(fw_st) \ + __fw_state_set(fw_st, FW_STATUS_ABORTED) +#define fw_state_is_loading(fw_st) \ + __fw_state_check(fw_st, FW_STATUS_LOADING) +#define fw_state_is_aborted(fw_st) \ + __fw_state_check(fw_st, FW_STATUS_ABORTED) +#define fw_state_wait_timeout(fw_st, timeout) \ + __fw_state_wait_common(fw_st, timeout) + +#endif /* CONFIG_FW_LOADER_USER_HELPER */ + /* firmware behavior options */ #define FW_OPT_UEVENT (1U << 0) #define FW_OPT_NOWAIT (1U << 1) @@ -145,9 +216,8 @@ struct firmware_cache { struct firmware_buf { struct kref ref; struct list_head list; - struct completion completion; struct firmware_cache *fwc; - unsigned long status; + struct fw_state fw_st; void *data; size_t size; size_t allocated_size; @@ -205,7 +275,7 @@ 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); + fw_state_init(&buf->fw_st); #ifdef CONFIG_FW_LOADER_USER_HELPER INIT_LIST_HEAD(&buf->pending_list); #endif @@ -309,8 +379,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_state_done(&buf->fw_st); mutex_unlock(&fw_lock); } @@ -478,12 +547,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_state_is_done(&buf->fw_st)) return; list_del_init(&buf->pending_list); - set_bit(FW_STATUS_ABORT, &buf->status); - complete_all(&buf->completion); + fw_state_aborted(&buf->fw_st); } static void fw_load_abort(struct firmware_priv *fw_priv) @@ -496,9 +564,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 +663,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_state_is_loading(&fw_priv->buf->fw_st); mutex_unlock(&fw_lock); return sprintf(buf, "%d\n", loading); @@ -653,23 +718,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_state_is_done(&fw_buf->fw_st)) { 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_state_start(&fw_buf->fw_st); } break; case 0: - if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) { + if (fw_state_is_loading(&fw_buf->fw_st)) { 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 +753,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_state_aborted(&fw_buf->fw_st); written = rc; + } else { + fw_state_done(&fw_buf->fw_st); } - complete_all(&fw_buf->completion); break; } /* fallthrough */ @@ -755,7 +818,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_state_is_done(&buf->fw_st)) { ret_count = -ENODEV; goto out; } @@ -842,7 +905,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_state_is_done(&buf->fw_st)) { retval = -ENODEV; goto out; } @@ -955,8 +1018,7 @@ static int _request_firmware_load(struct firmware_priv *fw_priv, timeout = MAX_JIFFY_OFFSET; } - timeout = wait_for_completion_interruptible_timeout(&buf->completion, - timeout); + timeout = fw_state_wait_timeout(&buf->fw_st, timeout); if (timeout == -ERESTARTSYS || !timeout) { retval = timeout; mutex_lock(&fw_lock); @@ -966,7 +1028,7 @@ static int _request_firmware_load(struct firmware_priv *fw_priv, retval = 0; } - if (is_fw_load_aborted(buf)) + if (fw_state_is_aborted(&buf->fw_st)) retval = -EAGAIN; else if (buf->is_paged_buf && !buf->data) retval = -ENOMEM; @@ -1016,9 +1078,6 @@ 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 @@ -1032,13 +1091,13 @@ 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_state_is_done(&buf->fw_st)) { + if (fw_state_is_aborted(&buf->fw_st)) { ret = -ENOENT; break; } mutex_unlock(&fw_lock); - ret = wait_for_completion_interruptible(&buf->completion); + ret = fw_state_wait(&buf->fw_st); mutex_lock(&fw_lock); } mutex_unlock(&fw_lock); @@ -1096,7 +1155,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_state_is_aborted(&buf->fw_st)) { mutex_unlock(&fw_lock); return -ENOENT; } -- 2.7.4