Skip to content

Commit 523ded7

Browse files
AlanSterngregkh
authored andcommitted
device_schedule_callback() needs a module reference
This patch (as896b) fixes an oversight in the design of device_schedule_callback(). It is necessary to acquire a reference to the module owning the callback routine, to prevent the module from being unloaded before the callback can run. Signed-off-by: Alan Stern <stern@rowland.harvard.edu> Cc: Satyam Sharma <satyam.sharma@gmail.com> Cc: Neil Brown <neilb@suse.de> Cc: Cornelia Huck <cornelia.huck@de.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent fa1a8c2 commit 523ded7

4 files changed

Lines changed: 29 additions & 13 deletions

File tree

‎drivers/base/core.c‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,10 @@ void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
480480
EXPORT_SYMBOL_GPL(device_remove_bin_file);
481481

482482
/**
483-
* device_schedule_callback - helper to schedule a callback for a device
483+
* device_schedule_callback_owner - helper to schedule a callback for a device
484484
* @dev: device.
485485
* @func: callback function to invoke later.
486+
* @owner: module owning the callback routine
486487
*
487488
* Attribute methods must not unregister themselves or their parent device
488489
* (which would amount to the same thing). Attempts to do so will deadlock,
@@ -493,20 +494,23 @@ EXPORT_SYMBOL_GPL(device_remove_bin_file);
493494
* argument in the workqueue's process context. @dev will be pinned until
494495
* @func returns.
495496
*
497+
* This routine is usually called via the inline device_schedule_callback(),
498+
* which automatically sets @owner to THIS_MODULE.
499+
*
496500
* Returns 0 if the request was submitted, -ENOMEM if storage could not
497-
* be allocated.
501+
* be allocated, -ENODEV if a reference to @owner isn't available.
498502
*
499503
* NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
500504
* underlying sysfs routine (since it is intended for use by attribute
501505
* methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
502506
*/
503-
int device_schedule_callback(struct device *dev,
504-
void (*func)(struct device *))
507+
int device_schedule_callback_owner(struct device *dev,
508+
void (*func)(struct device *), struct module *owner)
505509
{
506510
return sysfs_schedule_callback(&dev->kobj,
507-
(void (*)(void *)) func, dev);
511+
(void (*)(void *)) func, dev, owner);
508512
}
509-
EXPORT_SYMBOL_GPL(device_schedule_callback);
513+
EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
510514

511515
static void klist_children_get(struct klist_node *n)
512516
{

‎fs/sysfs/file.c‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ struct sysfs_schedule_callback_struct {
633633
struct kobject *kobj;
634634
void (*func)(void *);
635635
void *data;
636+
struct module *owner;
636637
struct work_struct work;
637638
};
638639

@@ -643,6 +644,7 @@ static void sysfs_schedule_callback_work(struct work_struct *work)
643644

644645
(ss->func)(ss->data);
645646
kobject_put(ss->kobj);
647+
module_put(ss->owner);
646648
kfree(ss);
647649
}
648650

@@ -651,6 +653,7 @@ static void sysfs_schedule_callback_work(struct work_struct *work)
651653
* @kobj: object we're acting for.
652654
* @func: callback function to invoke later.
653655
* @data: argument to pass to @func.
656+
* @owner: module owning the callback code
654657
*
655658
* sysfs attribute methods must not unregister themselves or their parent
656659
* kobject (which would amount to the same thing). Attempts to do so will
@@ -663,20 +666,25 @@ static void sysfs_schedule_callback_work(struct work_struct *work)
663666
* until @func returns.
664667
*
665668
* Returns 0 if the request was submitted, -ENOMEM if storage could not
666-
* be allocated.
669+
* be allocated, -ENODEV if a reference to @owner isn't available.
667670
*/
668671
int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
669-
void *data)
672+
void *data, struct module *owner)
670673
{
671674
struct sysfs_schedule_callback_struct *ss;
672675

676+
if (!try_module_get(owner))
677+
return -ENODEV;
673678
ss = kmalloc(sizeof(*ss), GFP_KERNEL);
674-
if (!ss)
679+
if (!ss) {
680+
module_put(owner);
675681
return -ENOMEM;
682+
}
676683
kobject_get(kobj);
677684
ss->kobj = kobj;
678685
ss->func = func;
679686
ss->data = data;
687+
ss->owner = owner;
680688
INIT_WORK(&ss->work, sysfs_schedule_callback_work);
681689
schedule_work(&ss->work);
682690
return 0;

‎include/linux/device.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,12 @@ extern int __must_check device_create_bin_file(struct device *dev,
367367
struct bin_attribute *attr);
368368
extern void device_remove_bin_file(struct device *dev,
369369
struct bin_attribute *attr);
370-
extern int device_schedule_callback(struct device *dev,
371-
void (*func)(struct device *));
370+
extern int device_schedule_callback_owner(struct device *dev,
371+
void (*func)(struct device *), struct module *owner);
372+
373+
/* This is a macro to avoid include problems with THIS_MODULE */
374+
#define device_schedule_callback(dev, func) \
375+
device_schedule_callback_owner(dev, func, THIS_MODULE)
372376

373377
/* device resource management */
374378
typedef void (*dr_release_t)(struct device *dev, void *res);

‎include/linux/sysfs.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ struct sysfs_ops {
8080
#ifdef CONFIG_SYSFS
8181

8282
extern int sysfs_schedule_callback(struct kobject *kobj,
83-
void (*func)(void *), void *data);
83+
void (*func)(void *), void *data, struct module *owner);
8484

8585
extern int __must_check
8686
sysfs_create_dir(struct kobject *, struct dentry *);
@@ -137,7 +137,7 @@ extern int __must_check sysfs_init(void);
137137
#else /* CONFIG_SYSFS */
138138

139139
static inline int sysfs_schedule_callback(struct kobject *kobj,
140-
void (*func)(void *), void *data)
140+
void (*func)(void *), void *data, struct module *owner)
141141
{
142142
return -ENOSYS;
143143
}

0 commit comments

Comments
 (0)