module: create a request_module_nowait()
Subsystems (3)
PATCH 1/3 module: create a request_module_nowait()
Quoted 1 line
From
48cd2d5171ae8fb40ef6092a566b4889b9ac72b7Mon Sep 17 00:00:00 2001
From: Arjan van de Ven <[email protected]>
Date: Sun, 1 Feb 2009 12:02:39 -0800
Subject: [PATCH] module: create a request_module_nowait()
There seems to be a common pattern in the kernel where drivers want to call request_module() from inside a module_init() function. Currently this would deadlock.
As a result, several drivers go through hoops like scheduling things via kevent, or creating custom work queues (because kevent can deadlock on them).
This patch changes this to use a request_module_nowait() function macro instead, which just fires the modprobe off but doesn't wait for it, and thus avoids the original deadlock entirely.
On my laptop this already results in one less kernel thread running..
--- include/linux/kmod.h | 9 ++++++--- kernel/kmod.c | 10 ++++++---- 2 files changed, 12 insertions(+), 7 deletions(-)
include/linux/kmod.h +6 -3
@@ -29,10 +29,13 @@#ifdef CONFIG_MODULES /* modprobe exit status on success, -ve on error. Return value * usually useless though. */ -extern int request_module (const char * name, ...) __attribute__ ((format (printf,1 ,2 ))); -#define try_then_request_module(x, mod...) ((x) ?: (request_module(mod), (x))) +extern int __request_module (int wait,const char *name, ...) __attribute__ ((format (printf,2 ,3 ))); +#define request_module(mod...) __request_module(1, mod) +#define request_module_nowait(mod...) __request_module(0, mod) +#define try_then_request_module(x, mod...) ((x) ?: (__request_module(0, mod), (x)))#else -static inline int request_module (const char * name, ...) {return -ENOSYS ; } +static inline int request_module (const char *name, ...) {return -ENOSYS ; } +static inline int request_module_nowait (const char *name, ...) {return -ENOSYS ; } #define try_then_request_module(x, mod...) (x)#endif
kernel/kmod.c +6 -4
@@ -50,7 +50,8 @@ static struct workqueue_struct *khelper_wq;char modprobe_path[KMOD_PATH_LEN ] ="/sbin/modprobe" ;/** -* request_module - try to load a kernel module +* __request_module - try to load a kernel module +* @wait: wait (or not) for the operation to complete * @fmt: printf style format string for the name of the module * @...: arguments as specified in the format string *
@@ -63,7 +64,8 @@ char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";* If module auto-loading support is disabled then this function * becomes a no-operation. */ -int request_module (const char *fmt, ...) + +int __request_module (int wait,const char *fmt, ...) {va_list args;char module_name[MODULE_NAME_LEN ];
@@ -108,11 +110,11 @@ int request_module(const char *fmt, ...) return -ENOMEM; } - ret =call_usermodehelper (modprobe_path, argv, envp,1 ); + ret =call_usermodehelper (modprobe_path, argv, envp, wait);atomic_dec (&kmod_concurrent);return ret; } -EXPORT_SYMBOL (request_module); +EXPORT_SYMBOL (__request_module);#endif /* CONFIG_MODULES */ struct subprocess_info {
Signature · 8 lines
1.6.0.6 -- Arjan van de Ven Intel Open Source Technology Centre For development, discussion and tips for power savings, visit http://www.lesswatts.org
PATCH 2/3 use the new request_module_nowait() in the hid driver
Quoted 2 lines
From
b8c36ec88e272ba2e755e6104579f1e751ee81f8Mon Sep 17 00:00:00 2001
From: Arjan van de Ven <[email protected]>
Date: Sun, 1 Feb 2009 12:02:39 -0800
Subject: [PATCH] use the new request_module_nowait() in the hid driver
Now that there is a request_module_nowait(), use it in the hid driver. This gets rid of a kernel thread, and also greatly simplifies the code...
--- drivers/hid/hid-core.c | 19 +------------------ 1 files changed, 1 insertions(+), 18 deletions(-)
drivers/hid/hid-core.c +1 -18
@@ -1812,15 +1812,6 @@ void hid_unregister_driver(struct hid_driver *hdrv) }EXPORT_SYMBOL_GPL (hid_unregister_driver); -#ifdef CONFIG_HID_COMPAT -static void hid_compat_load (struct work_struct *ws) -{ -request_module ("hid-dummy" ); -} -static DECLARE_WORK(hid_compat_work, hid_compat_load); -static struct workqueue_struct *hid_compat_wq; -#endif -static int __init hid_init (void ) {int ret;
@@ -1836,12 +1827,7 @@ static int __init hid_init(void) goto err_bus;#ifdef CONFIG_HID_COMPAT - hid_compat_wq =create_singlethread_workqueue ("hid_compat" ); -if (!hid_compat_wq) { -hidraw_exit (); - goto err; - } -queue_work (hid_compat_wq, &hid_compat_work); +request_module_nowait ("hid-dummy" );#endif return 0 ;
@@ -1853,9 +1839,6 @@ err: static void __exit hid_exit(void) { -#ifdef CONFIG_HID_COMPAT -destroy_workqueue (hid_compat_wq); -#endif hidraw_exit ();bus_unregister (&hid_bus_type); }
Signature · 7 lines
1.6.0.6 -- Arjan van de Ven Intel Open Source Technology Centre For development, discussion and tips for power savings, visit http://www.lesswatts.org
Re: [PATCH 2/3] use the new request_module_nowait() in the hid driver
Quoted 9 lines · Arjan van de Ven
From
b8c36ec88e272ba2e755e6104579f1e751ee81f8Mon Sep 17 00:00:00 2001
From: Arjan van de Ven <[email protected]>
Date: Sun, 1 Feb 2009 12:02:39 -0800
Subject: [PATCH] use the newrequest_module_nowait()in the hid driverNow that there is a
request_module_nowait(), use it in the hid driver. This gets rid of a kernel thread, and also greatly simplifies the code...Signed-off-by Arjan van de Ven <[email protected]>
Rusty, could you please take this altogether with the patch introducing request_module_nowait() itself?
Thanks,
Signature · 2 lines
Jiri Kosina SUSE Labs
Re: [PATCH 2/3] use the new request_module_nowait() in the hid driver
Quoted 4 lines · On Wednesday 11 February 2009 03:53:29 Jiri Kosina wrote:
Acked-by Jiri Kosina <[email protected]>Rusty, could you please take this altogether with the patch introducing
request_module_nowait()itself?
Yep, done!
Thanks,
Rusty.
PATCH 3/3 use the new request_module_nowait() in drivers/media
In reply to Arjan van de VenQuoted 1 line
From
2311993ceed96ec0bb023419120d9aeada205242Mon Sep 17 00:00:00 2001
From: Arjan van de Ven <[email protected]>
Date: Sun, 1 Feb 2009 12:02:39 -0800
Subject: [PATCH] use the new request_module_nowait() in drivers/media
Several drivers/media/video drivers use keventd to load modules to avoid the "load a module from the module init code" deadlock.
Now that we have request_module_nowait() this can be simplified greatly.
--- drivers/media/video/bt8xx/bttv-driver.c | 8 +------- drivers/media/video/bt8xx/bttvp.h | 3 --- drivers/media/video/cx88/cx88-mpeg.c | 14 +++----------- drivers/media/video/cx88/cx88.h | 1 - drivers/media/video/em28xx/em28xx-cards.c | 17 ++++------------- drivers/media/video/em28xx/em28xx.h | 2 -- drivers/media/video/saa7134/saa7134-core.c | 19 ++++++------------- drivers/media/video/saa7134/saa7134.h | 2 -- 8 files changed, 14 insertions(+), 52 deletions(-)
drivers/media/video/bt8xx/bttv-driver.c +1 -7
@@ -175,15 +175,9 @@ static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);/* ----------------------------------------------------------------------- */ /* dvb auto-load setup */ #if defined(CONFIG_MODULES ) && defined(MODULE ) -static void request_module_async (struct work_struct *work) -{ -request_module ("dvb-bt8xx" ); -} -static void request_modules (struct bttv *dev) { -INIT_WORK (&dev->request_module_wk, request_module_async); -schedule_work (&dev->request_module_wk); +request_module_async ("dvb-bt8xx" ); }#else #define request_modules (dev)
drivers/media/video/bt8xx/bttvp.h +0 -3
@@ -439,9 +439,6 @@ struct bttv {unsigned int users;struct bttv_fh init; -/* used to make dvb-bt8xx autoloadable */ -struct work_struct request_module_wk; -/* Default (0) and current (1) video capturing and overlay cropping parameters in bttv_tvnorm.cropcap units. Protected by bttv.lock. */
drivers/media/video/cx88/cx88-mpeg.c +3 -11
@@ -50,20 +50,12 @@ MODULE_PARM_DESC(debug,"enable debug messages [mpeg]"); printk(KERN_DEBUG "%s/2-mpeg: " fmt, core->name, ## arg) #if defined(CONFIG_MODULES) && defined(MODULE) -static void request_module_async (struct work_struct *work) +static void request_modules (struct cx8802_dev *dev) { - struct cx8802_dev *dev=container_of(work, struct cx8802_dev, request_module_wk); -if (dev->core->board.mpeg &CX88_MPEG_DVB ) -request_module ("cx88-dvb" ); +request_module_async ("cx88-dvb" );if (dev->core->board.mpeg &CX88_MPEG_BLACKBIRD ) -request_module ("cx88-blackbird" ); -} - -static void request_modules (struct cx8802_dev *dev) -{ -INIT_WORK (&dev->request_module_wk, request_module_async); -schedule_work (&dev->request_module_wk); +request_module_async ("cx88-blackbird" ); }#else #define request_modules (dev)
drivers/media/video/cx88/cx88.h +0 -1
@@ -510,7 +510,6 @@ struct cx8802_dev {/* List of attached drivers */ struct list_head drvlist; -struct work_struct request_module_wk; };/* ----------------------------------------------------------- */
drivers/media/video/em28xx/em28xx-cards.c +4 -13
@@ -1809,24 +1809,15 @@ void em28xx_card_setup(struct em28xx *dev)#if defined(CONFIG_MODULES ) && defined(MODULE ) -static void request_module_async (struct work_struct *work) +static void request_modules (struct em28xx *dev) { -struct em28xx *dev =container_of (work, - struct em28xx, request_module_wk); -if (dev->has_audio_class) -request_module ("snd-usb-audio" ); +request_module_async ("snd-usb-audio" );else if (dev->has_alsa_audio) -request_module ("em28xx-alsa" ); +request_module_async ("em28xx-alsa" );if (dev->board.has_dvb) -request_module ("em28xx-dvb" ); -} - -static void request_modules (struct em28xx *dev) -{ -INIT_WORK (&dev->request_module_wk, request_module_async); -schedule_work (&dev->request_module_wk); +request_module_async ("em28xx-dvb" ); }#else #define request_modules (dev)
drivers/media/video/em28xx/em28xx.h +0 -2
@@ -498,8 +498,6 @@ struct em28xx {enum em28xx_dev_state state;enum em28xx_io_method io; -struct work_struct request_module_wk; -/* locks */ struct mutex lock;struct mutex ctrl_urb_lock;/* protects urb_buf */
drivers/media/video/saa7134/saa7134-core.c +6 -13
@@ -149,23 +149,16 @@ void saa7134_set_gpio(struct saa7134_dev *dev, int bit_no, int value)/* delayed request_module */ #if defined(CONFIG_MODULES ) && defined(MODULE ) - -static void request_module_async (struct work_struct *work){ - struct saa7134_dev* dev = container_of(work, struct saa7134_dev, request_module_wk); +static void request_submodules (struct saa7134_dev *dev) +{if (card_is_empress (dev)) -request_module ("saa7134-empress" ); +request_module_async ("saa7134-empress" );if (card_is_dvb (dev)) -request_module ("saa7134-dvb" ); +request_module_async ("saa7134-dvb" );if (alsa) -request_module ("saa7134-alsa" ); +request_module_async ("saa7134-alsa" );if (oss) -request_module ("saa7134-oss" ); -} - -static void request_submodules (struct saa7134_dev *dev) -{ -INIT_WORK (&dev->request_module_wk, request_module_async); -schedule_work (&dev->request_module_wk); +request_module_async ("saa7134-oss" ); }#else
drivers/media/video/saa7134/saa7134.h +0 -2
@@ -482,8 +482,6 @@ struct saa7134_dev {struct mutex lock;spinlock_t slock;struct v4l2_prio_state prio; -/* workstruct for loading modules */ -struct work_struct request_module_wk;/* insmod option/autodetected */ int autodetected;
Signature · 8 lines
1.6.0.6 -- Arjan van de Ven Intel Open Source Technology Centre For development, discussion and tips for power savings, visit http://www.lesswatts.org
Re: [PATCH 3/3] use the new request_module_nowait() in drivers/media
On Sun, 8 Feb 2009 10:43:14 -0800
Arjan van de Ven <[email protected]> wrote:
blargh I sent an older version of this patch
I meant to send this one
Quoted 1 line
From
2311993ceed96ec0bb023419120d9aeada205242Mon Sep 17 00:00:00 2001
From: Arjan van de Ven <[email protected]>
Subject: [PATCH] use the new request_module_nowait() in drivers/media
Several drivers/media/video drivers use keventd to load modules to avoid the "load a module from the module init code" deadlock.
Now that we have request_module_nowait() this can be simplified greatly.
--- drivers/media/video/bt8xx/bttv-driver.c | 8 +------- drivers/media/video/bt8xx/bttvp.h | 3 --- drivers/media/video/cx88/cx88-mpeg.c | 14 +++----------- drivers/media/video/cx88/cx88.h | 1 - drivers/media/video/em28xx/em28xx-cards.c | 17 ++++------------- drivers/media/video/em28xx/em28xx.h | 2 -- drivers/media/video/saa7134/saa7134-core.c | 19 ++++++------------- drivers/media/video/saa7134/saa7134.h | 2 -- 8 files changed, 14 insertions(+), 52 deletions(-)
drivers/media/video/bt8xx/bttv-driver.c +1 -7
@@ -175,15 +175,9 @@ static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);/* ----------------------------------------------------------------------- */ /* dvb auto-load setup */ #if defined(CONFIG_MODULES ) && defined(MODULE ) -static void request_module_async (struct work_struct *work) -{ -request_module ("dvb-bt8xx" ); -} -static void request_modules (struct bttv *dev) { -INIT_WORK (&dev->request_module_wk, request_module_async); -schedule_work (&dev->request_module_wk); +request_module_nowait ("dvb-bt8xx" ); }#else #define request_modules (dev)
drivers/media/video/bt8xx/bttvp.h +0 -3
@@ -439,9 +439,6 @@ struct bttv {unsigned int users;struct bttv_fh init; -/* used to make dvb-bt8xx autoloadable */ -struct work_struct request_module_wk; -/* Default (0) and current (1) video capturing and overlay cropping parameters in bttv_tvnorm.cropcap units. Protected by bttv.lock. */
drivers/media/video/cx88/cx88-mpeg.c +3 -11
@@ -50,20 +50,12 @@ MODULE_PARM_DESC(debug,"enable debug messages [mpeg]"); printk(KERN_DEBUG "%s/2-mpeg: " fmt, core->name, ## arg) #if defined(CONFIG_MODULES) && defined(MODULE) -static void request_module_async (struct work_struct *work) +static void request_modules (struct cx8802_dev *dev) { - struct cx8802_dev *dev=container_of(work, struct cx8802_dev, request_module_wk); -if (dev->core->board.mpeg &CX88_MPEG_DVB ) -request_module ("cx88-dvb" ); +request_module_nowait ("cx88-dvb" );if (dev->core->board.mpeg &CX88_MPEG_BLACKBIRD ) -request_module ("cx88-blackbird" ); -} - -static void request_modules (struct cx8802_dev *dev) -{ -INIT_WORK (&dev->request_module_wk, request_module_async); -schedule_work (&dev->request_module_wk); +request_module_nowait ("cx88-blackbird" ); }#else #define request_modules (dev)
drivers/media/video/cx88/cx88.h +0 -1
@@ -510,7 +510,6 @@ struct cx8802_dev {/* List of attached drivers */ struct list_head drvlist; -struct work_struct request_module_wk; };/* ----------------------------------------------------------- */
drivers/media/video/em28xx/em28xx-cards.c +4 -13
@@ -1809,24 +1809,15 @@ void em28xx_card_setup(struct em28xx *dev)#if defined(CONFIG_MODULES ) && defined(MODULE ) -static void request_module_async (struct work_struct *work) +static void request_modules (struct em28xx *dev) { -struct em28xx *dev =container_of (work, - struct em28xx, request_module_wk); -if (dev->has_audio_class) -request_module ("snd-usb-audio" ); +request_module_nowait ("snd-usb-audio" );else if (dev->has_alsa_audio) -request_module ("em28xx-alsa" ); +request_module_nowait ("em28xx-alsa" );if (dev->board.has_dvb) -request_module ("em28xx-dvb" ); -} - -static void request_modules (struct em28xx *dev) -{ -INIT_WORK (&dev->request_module_wk, request_module_async); -schedule_work (&dev->request_module_wk); +request_module_nowait ("em28xx-dvb" ); }#else #define request_modules (dev)
drivers/media/video/em28xx/em28xx.h +0 -2
@@ -498,8 +498,6 @@ struct em28xx {enum em28xx_dev_state state;enum em28xx_io_method io; -struct work_struct request_module_wk; -/* locks */ struct mutex lock;struct mutex ctrl_urb_lock;/* protects urb_buf */
drivers/media/video/saa7134/saa7134-core.c +6 -13
@@ -149,23 +149,16 @@ void saa7134_set_gpio(struct saa7134_dev *dev, int bit_no, int value)/* delayed request_module */ #if defined(CONFIG_MODULES ) && defined(MODULE ) - -static void request_module_async (struct work_struct *work){ - struct saa7134_dev* dev = container_of(work, struct saa7134_dev, request_module_wk); +static void request_submodules (struct saa7134_dev *dev) +{if (card_is_empress (dev)) -request_module ("saa7134-empress" ); +request_module_nowait ("saa7134-empress" );if (card_is_dvb (dev)) -request_module ("saa7134-dvb" ); +request_module_nowait ("saa7134-dvb" );if (alsa) -request_module ("saa7134-alsa" ); +request_module_nowait ("saa7134-alsa" );if (oss) -request_module ("saa7134-oss" ); -} - -static void request_submodules (struct saa7134_dev *dev) -{ -INIT_WORK (&dev->request_module_wk, request_module_async); -schedule_work (&dev->request_module_wk); +request_module_nowait ("saa7134-oss" ); }#else
drivers/media/video/saa7134/saa7134.h +0 -2
@@ -482,8 +482,6 @@ struct saa7134_dev {struct mutex lock;spinlock_t slock;struct v4l2_prio_state prio; -/* workstruct for loading modules */ -struct work_struct request_module_wk;/* insmod option/autodetected */ int autodetected;
Signature · 8 lines
1.6.0.6 -- Arjan van de Ven Intel Open Source Technology Centre For development, discussion and tips for power savings, visit http://www.lesswatts.org
Mauro Carvalho Chehab
Acked-byRe: [PATCH 3/3] use the new request_module_nowait() in drivers/media
Quoted 10 lines · On Sun, 8 Feb 2009 11:00:52 -0800 Arjan van de Ven <[email protected]> wrote:
On Sun, 8 Feb 2009 10:43:14 -0800
Arjan van de Ven <[email protected]> wrote:blargh I sent an older version of this patch
I meant to send this oneFrom
2311993ceed96ec0bb023419120d9aeada205242Mon Sep 17 00:00:00 2001
From: Arjan van de Ven <[email protected]>
Subject: [PATCH] use the newrequest_module_nowait()indrivers/media
Quoted 7 lines
Several
drivers/media/videodrivers use keventd to load modules to avoid the "load a module from the module init code" deadlock.Now that we have
request_module_nowait()this can be simplified greatly.Signed-off-by Arjan van de Ven <[email protected]>
Cheers,
Mauro
Re: [PATCH 3/3] use the new request_module_nowait() in drivers/media
Quoted 4 lines · On Tuesday 10 February 2009 03:04:56 Mauro Carvalho Chehab wrote:
Quoted 2 lines · On Sun, 8 Feb 2009 11:00:52 -0800 Arjan van de Ven <[email protected]> wrote:
From: Arjan van de Ven <[email protected]>
Subject: [PATCH] use the newrequest_module_nowait()indrivers/media
...
Quoted 4 lines
Cheers,
Mauro
Thanks Mauro. Since I have the prereq, I've taken this into my tree too.
Thanks,
Rusty.
Re: [PATCH 3/3] use the new request_module_nowait() in drivers/media
Quoted 5 lines · On Wed, 11 Feb 2009 00:02:15 +1030 Rusty Russell <[email protected]> wrote:
Quoted 4 lines · On Tuesday 10 February 2009 03:04:56 Mauro Carvalho Chehab wrote:
Quoted 2 lines · On Sun, 8 Feb 2009 11:00:52 -0800 Arjan van de Ven <[email protected]> wrote:
From: Arjan van de Ven <[email protected]>
Subject: [PATCH] use the newrequest_module_nowait()indrivers/media
Quoted 7 lines
...
Quoted 4 lines
Cheers,
MauroThanks Mauro. Since I have the prereq, I've taken this into my tree too.
OK.
Cheers,
Mauro
Quoted 3 lines · On Monday 09 February 2009 05:12:01 Arjan van de Ven wrote:
This patch changes this to use a
request_module_nowait()function macro instead, which just fires the modprobe off but doesn't wait for it, and thus avoids the original deadlock entirely.
Nice, applied. Made one change; I've taken to using the new-fangled bool type for flag args, for slight extra clarity.
(Which uncovered another interesting issue, which lead to several more
patches).
Thanks,
Rusty.
Quoted 11 lines · On Mon, 9 Feb 2009 17:02:17 +1030 Rusty Russell <[email protected]> wrote:
Quoted 3 lines · On Monday 09 February 2009 05:12:01 Arjan van de Ven wrote:
This patch changes this to use a
request_module_nowait()function macro instead, which just fires the modprobe off but doesn't wait for it, and thus avoids the original deadlock entirely.Nice, applied. Made one change; I've taken to using the new-fangled bool type for flag args, for slight extra clarity.
(Which uncovered another interesting issue, which lead to several more
patches).
for this case that's a bit unfortunate though.
the call usermode helper code takes 3 values, -1, 0 and 1. Right now the code only uses 0 and 1, but I've been looking at using -1 as well in this exact codepath in case there is a problem with the 0 option (the 0 option waits for the exec but not the result, the -1 waits for nothing)
Signature · 3 lines
Arjan van de Ven Intel Open Source Technology Centre For development, discussion and tips for power savings, visit http://www.lesswatts.org
Quoted 8 lines · On Tuesday 10 February 2009 01:55:31 Arjan van de Ven wrote:
Quoted 2 lines · On Mon, 9 Feb 2009 17:02:17 +1030 Rusty Russell <[email protected]> wrote:
Nice, applied. Made one change; I've taken to using the new-fangled bool type for flag args, for slight extra clarity.
for this case that's a bit unfortunate though.
the call usermode helper code takes 3 values, -1, 0 and 1.
Yes, we could re-use the umh enum if we want to change it.
Thanks,
Rusty.