module: don't ignore sysfs_create_link() failures
PATCH v2 1/1 module: don't ignore sysfs_create_link() failures
From: Arnd Bergmann <[email protected]>
The sysfs_create_link() return code is marked as __must_check, but the module_add_driver() function tries hard to not care, by assigning the return code to a variable. When building with 'make W=1', gcc still warns because this variable is only assigned but not used:
drivers/base/module.c: In function 'module_add_driver':drivers/base/module.c:36:6: warning: variable 'no_warn' set but not used [-Wunused-but-set-variable]
Rework the code to properly unwind and return the error code to the caller. My reading of the original code was that it tries to not fail when the links already exist, so keep ignoring -EEXIST errors.
See-also: 4a7fb6363f2d ("add __must_check to device management code")
---
v2: rework to actually handle the error. I have not tested the
error handling beyond build testing, so please review carefully.
--- drivers/base/base.h | 2 +- drivers/base/bus.c | 7 ++++++- drivers/base/module.c | 42 +++++++++++++++++++++++++++++++----------- 3 files changed, 38 insertions(+), 13 deletions(-)
drivers/base/base.h +1 -1
@@ -192,7 +192,7 @@ extern struct kset *devices_kset;void devices_kset_move_last (struct device *dev);#if defined(CONFIG_MODULES ) && defined(CONFIG_SYSFS ) -void module_add_driver (struct module *mod,struct device_driver *drv); +int module_add_driver (struct module *mod,struct device_driver *drv);void module_remove_driver (struct device_driver *drv);#else static inline void module_add_driver(struct module *mod,
drivers/base/bus.c +6 -1
@@ -674,7 +674,12 @@ int bus_add_driver(struct device_driver *drv)if (error) goto out_del_list; } -module_add_driver (drv->owner, drv); + error =module_add_driver (drv->owner, drv); +if (error) { +printk (KERN_ERR "%s: failed to create module links for %s\n" , + __func__, drv->name); + goto out_del_list; + } error =driver_create_file (drv, &driver_attr_uevent);if (error) {
drivers/base/module.c +31 -11
62 lines not on this page · raw message
Signature · 1 line
2.39.2
Greg Kroah-Hartman
maintainerQuoted 49 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
The
sysfs_create_link()return code is marked as__must_check, but themodule_add_driver()function tries hard to not care, by assigning the return code to a variable. When building with 'make W=1', gcc still warns because this variable is only assigned but not used:
drivers/base/module.c: In function 'module_add_driver':drivers/base/module.c:36:6: warning: variable 'no_warn' set but not used [-Wunused-but-set-variable]Rework the code to properly unwind and return the error code to the caller. My reading of the original code was that it tries to not fail when the links already exist, so keep ignoring
-EEXISTerrors.Cc Luis Chamberlain <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc "Rafael J. Wysocki" <[email protected]>Fixes e17e0f51aeea ("Driver core: show drivers in /sys/module/")See-also:
4a7fb6363f2d("add__must_checkto device management code")Signed-off-by Arnd Bergmann <[email protected]>---
v2: rework to actually handle the error. I have not tested the
error handling beyond build testing, so please review carefully.--- drivers/base/base.h | 2 +- drivers/base/bus.c | 7 ++++++- drivers/base/module.c | 42 +++++++++++++++++++++++++++++++----------- 3 files changed, 38 insertions(+), 13 deletions(-)drivers/base/base.h +1
-1@@ -192,7 +192,7 @@ extern struct kset *devices_kset;void devices_kset_move_last (struct device *dev);#if defined(CONFIG_MODULES ) && defined(CONFIG_SYSFS ) -void module_add_driver (struct module *mod,struct device_driver *drv); +int module_add_driver (struct module *mod,struct device_driver *drv);void module_remove_driver (struct device_driver *drv);#else static inline void module_add_driver(struct module *mod,drivers/base/bus.c +0
-0
drivers/base/bus.c int bus_add_driver(struct device_driver *drv) · line 681
Quoted 9 lines
drivers/base/bus.c +5
-1@@ -674,7 +674,12 @@ int bus_add_driver(struct device_driver *drv)if (error) goto out_del_list; } -module_add_driver (drv->owner, drv); + error =module_add_driver (drv->owner, drv); +if (error) { +printk (KERN_ERR "%s: failed to create module links for %s\n" , + __func__, drv->name); + goto out_del_list;
Don't we need to walk back the driver_attach() call here if this fails?
Quoted 27 lines
drivers/base/bus.c +1
-0+ } error =driver_create_file (drv, &driver_attr_uevent);if (error) {drivers/base/module.c +3
-3@@ -30,14 +30,14 @@ static void module_create_drivers_dir(struct module_kobject *mk)mutex_unlock (&drivers_dir_mutex); } -void module_add_driver (struct module *mod,struct device_driver *drv) +int module_add_driver (struct module *mod,struct device_driver *drv) {char *driver_name; -int no_warn; +int ret;struct module_kobject *mk =NULL ;if (!drv) -return ; +return 0 ;if (mod) mk = &mod->mkobj;
drivers/base/module.c void module_add_driver(struct module *mod, struct device_driver *drv) · line 62
Quoted 8 lines
drivers/base/module.c +4
-1@@ -56,17 +56,37 @@ void module_add_driver(struct module *mod, struct device_driver *drv) }if (!mk) -return ; +return 0 ; + + ret =sysfs_create_link (&drv->p->kobj, &mk->kobj,"module" ); +if (ret && ret != -EEXIST )
Why would EEXIST happen here? How can this be called twice?
Quoted 14 lines
drivers/base/module.c +5
-7+return ret; -/* Don't check return codes; these calls are idempotent */ - no_warn =sysfs_create_link (&drv->p->kobj, &mk->kobj,"module" ); driver_name =make_driver_name (drv); -if (driver_name) { -module_create_drivers_dir (mk); - no_warn =sysfs_create_link (mk->drivers_dir, &drv->p->kobj, - driver_name); -kfree (driver_name); +if (!driver_name) { + ret = -ENOMEM ; + goto out; + }
drivers/base/module.c void module_add_driver(struct module *mod, struct device_driver *drv) · line 79
Quoted 10 lines
drivers/base/module.c +9
-0+ +module_create_drivers_dir (mk); +if (!mk->drivers_dir) { + ret = -EINVAL ; + goto out; } + + ret =sysfs_create_link (mk->drivers_dir, &drv->p->kobj, driver_name); +if (ret && ret != -EEXIST ) + goto out;
Same EEXIST question here.
thanks,
greg k-h
Quoted 17 lines · Greg Kroah-Hartman
Quoted 5 lines · Arnd Bergmann
drivers/base/bus.c +0
-0drivers/base/bus.c int bus_add_driver(struct device_driver *drv) · line 681
Quoted 9 lines
drivers/base/bus.c +5
-1@@ -674,7 +674,12 @@ int bus_add_driver(struct device_driver *drv)if (error) goto out_del_list; } -module_add_driver (drv->owner, drv); + error =module_add_driver (drv->owner, drv); +if (error) { +printk (KERN_ERR "%s: failed to create module links for %s\n" , + __func__, drv->name); + goto out_del_list;Don't we need to walk back the
driver_attach()call here if this fails?
Yes, fixed now. There are still some other calls right after it that print an error but don't cause bus_add_driver() to fail though. We may want to add similar unwinding there, but that feels like it should be a separate patch.
Quoted 10 lines
drivers/base/bus.c int bus_add_driver(struct device_driver *drv)
Quoted 7 lines
drivers/base/bus.c +4
-1if (!mk) -return ; +return 0 ; + + ret =sysfs_create_link (&drv->p->kobj, &mk->kobj,"module" ); +if (ret && ret != -EEXIST )Why would EEXIST happen here? How can this be called twice?
My impression was that the lack of error handling and the comment was ab out a case where that might happen intentionally. I've removed it now as I couldn't find any evidence that this is really needed. I suppose we would find out in testing if we do.
Arnd