public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: Ming Lei <ming.lei@canonical.com>
Cc: linux-kernel@vger.kernel.org,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: Re: [PATCH 1/3] firmware: Avoid superfluous usermodehelper lock
Date: Wed, 08 May 2013 19:51:29 +0200	[thread overview]
Message-ID: <s5hsj1xxsv2.wl%tiwai@suse.de> (raw)
In-Reply-To: <s5h38txwhqq.wl%tiwai@suse.de>

At Wed, 08 May 2013 18:37:01 +0200,
Takashi Iwai wrote:
> 
> At Wed, 08 May 2013 18:21:11 +0200,
> Takashi Iwai wrote:
> > 
> > At Wed, 8 May 2013 23:52:02 +0800,
> > Ming Lei wrote:
> > > 
> > > On Wed, May 8, 2013 at 2:56 PM, Takashi Iwai <tiwai@suse.de> wrote:
> > > > When a firmware file can be loaded directly, there is no good reason
> > > > to lock usermodehelper.  It's needed only when the direct fw load
> > > > fails and falls back to the user-mode helper.
> > > 
> > > I remembered that we discussed the problem before, :-)
> > > 
> > > Some crazy drivers might call request_firmware inside resume callback
> > > (for example, USB devices might be rebind in resume), with
> > > usermodehelper_read_lock, we can find the mistake easily and log it.
> > > 
> > > I am wondering if it is good to remove the usermodehelper lock.
> > > 
> > > Could you let us know any benefit to do it?
> > 
> > Well, the question is whether usermodehelper lock is really an
> > appropriate stuff for *checking* the availability of direct fs
> > access.  I find it doesn't fit well any longer, in the situation where
> > no actual user-space call is needed.  Though, I'm not quite sure which
> > lock or flag can be used instead...
> 
> In other words, the first patch is no essential part of the fix.
> I can revisit the second patch without this one and resend if
> preferred.

FWIW, below is the revised patch.
It's alone without the patch 1 in the previous series.


Takashi

---
From: Takashi Iwai <tiwai@suse.de>
Subject: [PATCH v2] firmware: Avoid deadlock of usermodehelper lock at shutdown

When a system goes to reboot/shutdown, it tries to disable the
usermode helper via usermodehelper_disable().  This might be blocked
when a driver tries to load a firmware beforehand and it's stuck by
some reason.

In this patch, the firmware class driver registers a reboot notifier
so that it can abort all pending f/w bufs.  Also enable a flag for
avoiding the call of usermodehelper after the reboot/shutdown starts.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 drivers/base/firmware_class.c | 40 +++++++++++++++++++++++++++++++++-------
 1 file changed, 33 insertions(+), 7 deletions(-)

diff --git a/drivers/base/firmware_class.c b/drivers/base/firmware_class.c
index 4b1f926..972e535 100644
--- a/drivers/base/firmware_class.c
+++ b/drivers/base/firmware_class.c
@@ -27,6 +27,7 @@
 #include <linux/pm.h>
 #include <linux/suspend.h>
 #include <linux/syscore_ops.h>
+#include <linux/reboot.h>
 
 #include <generated/utsrelease.h>
 
@@ -171,6 +172,7 @@ static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
 	strcpy(buf->fw_id, fw_name);
 	buf->fwc = fwc;
 	init_completion(&buf->completion);
+	INIT_LIST_HEAD(&buf->list);
 
 	pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
 
@@ -446,10 +448,9 @@ static struct firmware_priv *to_firmware_priv(struct device *dev)
 	return container_of(dev, struct firmware_priv, dev);
 }
 
-static void fw_load_abort(struct firmware_priv *fw_priv)
+static void fw_load_abort(struct firmware_buf *buf)
 {
-	struct firmware_buf *buf = fw_priv->buf;
-
+	list_del_init(&buf->list);
 	set_bit(FW_STATUS_ABORT, &buf->status);
 	complete_all(&buf->completion);
 }
@@ -457,6 +458,24 @@ static void fw_load_abort(struct firmware_priv *fw_priv)
 #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 */
+static int fw_shutdown_notify(struct notifier_block *unused1,
+			      unsigned long unused2, void *unused3)
+{
+	mutex_lock(&fw_lock);
+	while (!list_empty(&pending_fw_head))
+		fw_load_abort(list_first_entry(&pending_fw_head,
+					       struct firmware_buf, list));
+	mutex_unlock(&fw_lock);
+	return NOTIFY_DONE;
+}
+
+static struct notifier_block fw_shutdown_nb = {
+	.notifier_call = fw_shutdown_notify,
+};
+
 static ssize_t firmware_timeout_show(struct class *class,
 				     struct class_attribute *attr,
 				     char *buf)
@@ -604,6 +623,7 @@ static ssize_t firmware_loading_store(struct device *dev,
 			 * is completed.
 			 * */
 			fw_map_pages_buf(fw_buf);
+			list_del_init(&fw_buf->list);
 			complete_all(&fw_buf->completion);
 			break;
 		}
@@ -612,7 +632,7 @@ static ssize_t firmware_loading_store(struct device *dev,
 		dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
 		/* fallthrough */
 	case -1:
-		fw_load_abort(fw_priv);
+		fw_load_abort(fw_buf);
 		break;
 	}
 out:
@@ -680,7 +700,7 @@ static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
 		new_pages = kmalloc(new_array_size * sizeof(void *),
 				    GFP_KERNEL);
 		if (!new_pages) {
-			fw_load_abort(fw_priv);
+			fw_load_abort(buf);
 			return -ENOMEM;
 		}
 		memcpy(new_pages, buf->pages,
@@ -697,7 +717,7 @@ static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
 			alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
 
 		if (!buf->pages[buf->nr_pages]) {
-			fw_load_abort(fw_priv);
+			fw_load_abort(buf);
 			return -ENOMEM;
 		}
 		buf->nr_pages++;
@@ -781,7 +801,7 @@ static void firmware_class_timeout_work(struct work_struct *work)
 		mutex_unlock(&fw_lock);
 		return;
 	}
-	fw_load_abort(fw_priv);
+	fw_load_abort(fw_priv->buf);
 	mutex_unlock(&fw_lock);
 }
 
@@ -857,6 +877,10 @@ static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
 		kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
 	}
 
+	mutex_lock(&fw_lock);
+	list_add(&buf->list, &pending_fw_head);
+	mutex_unlock(&fw_lock);
+
 	wait_for_completion(&buf->completion);
 
 	cancel_delayed_work_sync(&fw_priv->timeout_work);
@@ -1517,6 +1541,7 @@ static int __init firmware_class_init(void)
 {
 	fw_cache_init();
 #ifdef CONFIG_FW_LOADER_USER_HELPER
+	register_reboot_notifier(&fw_shutdown_nb);
 	return class_register(&firmware_class);
 #else
 	return 0;
@@ -1530,6 +1555,7 @@ static void __exit firmware_class_exit(void)
 	unregister_pm_notifier(&fw_cache.pm_notify);
 #endif
 #ifdef CONFIG_FW_LOADER_USER_HELPER
+	unregister_reboot_notifier(&fw_shutdown_nb);
 	class_unregister(&firmware_class);
 #endif
 }
-- 
1.8.2.1


  reply	other threads:[~2013-05-08 17:51 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-08  6:56 [PATCH 0/3] firmware: Fix usermodehelper deadlock at shutdown Takashi Iwai
2013-05-08  6:56 ` [PATCH 1/3] firmware: Avoid superfluous usermodehelper lock Takashi Iwai
2013-05-08 15:52   ` Ming Lei
2013-05-08 16:21     ` Takashi Iwai
2013-05-08 16:37       ` Takashi Iwai
2013-05-08 17:51         ` Takashi Iwai [this message]
2013-05-09  1:25           ` Ming Lei
2013-05-09  7:31             ` Takashi Iwai
2013-05-09  8:43               ` Ming Lei
2013-05-09 17:04                 ` Takashi Iwai
2013-05-10  1:25                   ` Ming Lei
2013-05-10  9:32                     ` Takashi Iwai
2013-05-11 13:01                       ` Ming Lei
2013-05-12  7:20                         ` Takashi Iwai
2013-05-12 13:32                           ` Ming Lei
2013-05-13 14:48                             ` Takashi Iwai
2013-05-12 13:59           ` Ming Lei
2013-05-13 15:04             ` Takashi Iwai
2013-05-08  6:56 ` [PATCH 2/3] firmware: Avoid deadlock of usermodehelper lock at shutdown Takashi Iwai
2013-05-08 15:56   ` Ming Lei
2013-05-08 16:15     ` Takashi Iwai
2013-05-09  1:19       ` Ming Lei
2013-05-09  7:34         ` Takashi Iwai
2013-05-08  6:56 ` [PATCH 3/3] dell_rbu: Select CONFIG_FW_LOADER_USER_HELPER explicitly Takashi Iwai
2013-05-08 15:42 ` [PATCH 0/3] firmware: Fix usermodehelper deadlock at shutdown Greg Kroah-Hartman
2013-05-08 15:48   ` Takashi Iwai
2013-05-08 15:59   ` Ming Lei
2013-05-08 16:07 ` Ming Lei
2013-05-08 16:26   ` Takashi Iwai
2013-05-08 18:46     ` Kay Sievers
2013-05-09  7:26       ` Takashi Iwai
2013-05-21 17:02 ` Greg Kroah-Hartman
2013-05-22  1:21   ` Ming Lei
2013-05-22 16:25     ` Takashi Iwai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=s5hsj1xxsv2.wl%tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ming.lei@canonical.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox