From 161d6981096f5700ff5a2618bc9337c157eb717a Mon Sep 17 00:00:00 2001 From: Wolfram Sang Date: Tue, 28 Oct 2014 17:40:40 +0100 Subject: core: platform: add warning if driver has no owner Commit 9447057eaff8 ("platform_device: use a macro instead of platform_driver_register") introduced a codepath which could result into drivers having no owner. This went unnoticed for months, so add a warning in case this happens again somewhere else somewhen. Signed-off-by: Wolfram Sang Signed-off-by: Greg Kroah-Hartman --- drivers/base/driver.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'drivers/base') diff --git a/drivers/base/driver.c b/drivers/base/driver.c index 9e29943e56ca..6b10ff3bb410 100644 --- a/drivers/base/driver.c +++ b/drivers/base/driver.c @@ -151,6 +151,9 @@ int driver_register(struct device_driver *drv) BUG_ON(!drv->bus->p); + if (!drv->owner) + printk(KERN_WARNING "Driver '%s' needs an owner", drv->name); + if ((drv->bus->probe && drv->probe) || (drv->bus->remove && drv->remove) || (drv->bus->shutdown && drv->shutdown)) -- cgit v1.2.3 From c3b50dc219e1437e4dcb6a1639b004648dc29faa Mon Sep 17 00:00:00 2001 From: Wolfram Sang Date: Tue, 28 Oct 2014 17:40:41 +0100 Subject: core: platform: let platform_driver_probe initialize module owner Since commit 9447057eaff8 ("platform_device: use a macro instead of platform_driver_register"), platform_driver_register() always overwrites the .owner field of a platform_driver with THIS_MODULE. This breaks platform_driver_probe() which uses it from within the platform core instead of the module init. Fix it by using a similar #define construct to obtain THIS_MODULE and pass it on later. Reported-by: Russell King Signed-off-by: Wolfram Sang Signed-off-by: Greg Kroah-Hartman --- drivers/base/platform.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'drivers/base') diff --git a/drivers/base/platform.c b/drivers/base/platform.c index b2afc29403f9..c87a63326871 100644 --- a/drivers/base/platform.c +++ b/drivers/base/platform.c @@ -580,9 +580,10 @@ void platform_driver_unregister(struct platform_driver *drv) EXPORT_SYMBOL_GPL(platform_driver_unregister); /** - * platform_driver_probe - register driver for non-hotpluggable device + * __platform_driver_probe - register driver for non-hotpluggable device * @drv: platform driver structure * @probe: the driver probe routine, probably from an __init section + * @module: module which will be the owner of the driver * * Use this instead of platform_driver_register() when you know the device * is not hotpluggable and has already been registered, and you want to @@ -598,8 +599,8 @@ EXPORT_SYMBOL_GPL(platform_driver_unregister); * Returns zero if the driver registered and bound to a device, else returns * a negative error code and with the driver not registered. */ -int __init_or_module platform_driver_probe(struct platform_driver *drv, - int (*probe)(struct platform_device *)) +int __init_or_module __platform_driver_probe(struct platform_driver *drv, + int (*probe)(struct platform_device *), struct module *module) { int retval, code; @@ -614,7 +615,7 @@ int __init_or_module platform_driver_probe(struct platform_driver *drv, /* temporary section violation during probe() */ drv->probe = probe; - retval = code = platform_driver_register(drv); + retval = code = __platform_driver_register(drv, module); /* * Fixup that section violation, being paranoid about code scanning @@ -633,7 +634,7 @@ int __init_or_module platform_driver_probe(struct platform_driver *drv, platform_driver_unregister(drv); return retval; } -EXPORT_SYMBOL_GPL(platform_driver_probe); +EXPORT_SYMBOL_GPL(__platform_driver_probe); /** * platform_create_bundle - register driver and create corresponding device -- cgit v1.2.3 From 291f653a140ad880426125e5e9dbb70f4c184683 Mon Sep 17 00:00:00 2001 From: Wolfram Sang Date: Tue, 28 Oct 2014 17:40:42 +0100 Subject: core: platform: let platform_create_bundle initialize module owner Since commit 9447057eaff8 ("platform_device: use a macro instead of platform_driver_register"), platform_driver_register() always overwrites the .owner field of a platform_driver with THIS_MODULE. This breaks platform_create_bundle() which uses it via platform_driver_probe() from within the platform core instead of the module init. Fix it by using a similar #define construct to obtain THIS_MODULE and pass it on later. Reported-by: Russell King Signed-off-by: Wolfram Sang Signed-off-by: Greg Kroah-Hartman --- drivers/base/platform.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'drivers/base') diff --git a/drivers/base/platform.c b/drivers/base/platform.c index c87a63326871..cdb6c076c3f7 100644 --- a/drivers/base/platform.c +++ b/drivers/base/platform.c @@ -637,24 +637,25 @@ int __init_or_module __platform_driver_probe(struct platform_driver *drv, EXPORT_SYMBOL_GPL(__platform_driver_probe); /** - * platform_create_bundle - register driver and create corresponding device + * __platform_create_bundle - register driver and create corresponding device * @driver: platform driver structure * @probe: the driver probe routine, probably from an __init section * @res: set of resources that needs to be allocated for the device * @n_res: number of resources * @data: platform specific data for this platform device * @size: size of platform specific data + * @module: module which will be the owner of the driver * * Use this in legacy-style modules that probe hardware directly and * register a single platform device and corresponding platform driver. * * Returns &struct platform_device pointer on success, or ERR_PTR() on error. */ -struct platform_device * __init_or_module platform_create_bundle( +struct platform_device * __init_or_module __platform_create_bundle( struct platform_driver *driver, int (*probe)(struct platform_device *), struct resource *res, unsigned int n_res, - const void *data, size_t size) + const void *data, size_t size, struct module *module) { struct platform_device *pdev; int error; @@ -677,7 +678,7 @@ struct platform_device * __init_or_module platform_create_bundle( if (error) goto err_pdev_put; - error = platform_driver_probe(driver, probe); + error = __platform_driver_probe(driver, probe, module); if (error) goto err_pdev_del; @@ -690,7 +691,7 @@ err_pdev_put: err_out: return ERR_PTR(error); } -EXPORT_SYMBOL_GPL(platform_create_bundle); +EXPORT_SYMBOL_GPL(__platform_create_bundle); /* modalias support enables more hands-off userspace setup: * (a) environment variable lets new-style hotplug events work once system is -- cgit v1.2.3 From 0cd75047de7f54d6fb4aba0ec5818f8194815a5a Mon Sep 17 00:00:00 2001 From: Sergey Klyaus Date: Wed, 8 Oct 2014 11:31:54 +0400 Subject: driver core: fix race with userland in device_add() bus_add_device() should be called before devtmpfs_create_node(), so when userland application opens device from devtmpfs, it wouldn't get ENODEV from kernel, because device_add() wasn't completed. Signed-off-by: Sergey Klyaus Signed-off-by: Greg Kroah-Hartman --- drivers/base/core.c | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'drivers/base') diff --git a/drivers/base/core.c b/drivers/base/core.c index 14d162952c3b..00ec7ce6f3e3 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -1019,18 +1019,6 @@ int device_add(struct device *dev) if (error) goto attrError; - if (MAJOR(dev->devt)) { - error = device_create_file(dev, &dev_attr_dev); - if (error) - goto ueventattrError; - - error = device_create_sys_dev_entry(dev); - if (error) - goto devtattrError; - - devtmpfs_create_node(dev); - } - error = device_add_class_symlinks(dev); if (error) goto SymlinkError; @@ -1045,6 +1033,18 @@ int device_add(struct device *dev) goto DPMError; device_pm_add(dev); + if (MAJOR(dev->devt)) { + error = device_create_file(dev, &dev_attr_dev); + if (error) + goto DevAttrError; + + error = device_create_sys_dev_entry(dev); + if (error) + goto SysEntryError; + + devtmpfs_create_node(dev); + } + /* Notify clients of device addition. This call must come * after dpm_sysfs_add() and before kobject_uevent(). */ @@ -1074,6 +1074,12 @@ int device_add(struct device *dev) done: put_device(dev); return error; + SysEntryError: + if (MAJOR(dev->devt)) + device_remove_file(dev, &dev_attr_dev); + DevAttrError: + device_pm_remove(dev); + dpm_sysfs_remove(dev); DPMError: bus_remove_device(dev); BusError: @@ -1081,14 +1087,6 @@ done: AttrsError: device_remove_class_symlinks(dev); SymlinkError: - if (MAJOR(dev->devt)) - devtmpfs_delete_node(dev); - if (MAJOR(dev->devt)) - device_remove_sys_dev_entry(dev); - devtattrError: - if (MAJOR(dev->devt)) - device_remove_file(dev, &dev_attr_dev); - ueventattrError: device_remove_file(dev, &dev_attr_uevent); attrError: kobject_uevent(&dev->kobj, KOBJ_REMOVE); -- cgit v1.2.3 From 0372ffb35d00288802265586a29c117911d02fb8 Mon Sep 17 00:00:00 2001 From: Alex Williamson Date: Fri, 31 Oct 2014 11:13:07 -0600 Subject: driver core: Fix unbalanced device reference in drivers_probe bus_find_device_by_name() acquires a device reference which is never released. This results in an object leak, which on older kernels results in failure to release all resources of PCI devices. libvirt uses drivers_probe to re-attach devices to the host after assignment and is therefore a common trigger for this leak. Example: # cd /sys/bus/pci/ # dmesg -C # echo 1 > devices/0000\:01\:00.0/sriov_numvfs # echo 0 > devices/0000\:01\:00.0/sriov_numvfs # dmesg | grep 01:10 pci 0000:01:10.0: [8086:10ca] type 00 class 0x020000 kobject: '0000:01:10.0' (ffff8801d79cd0a8): kobject_add_internal: parent: '0000:00:01.0', set: 'devices' kobject: '0000:01:10.0' (ffff8801d79cd0a8): kobject_uevent_env kobject: '0000:01:10.0' (ffff8801d79cd0a8): fill_kobj_path: path = '/devices/pci0000:00/0000:00:01.0/0000:01:10.0' kobject: '0000:01:10.0' (ffff8801d79cd0a8): kobject_uevent_env kobject: '0000:01:10.0' (ffff8801d79cd0a8): fill_kobj_path: path = '/devices/pci0000:00/0000:00:01.0/0000:01:10.0' kobject: '0000:01:10.0' (ffff8801d79cd0a8): kobject_uevent_env kobject: '0000:01:10.0' (ffff8801d79cd0a8): fill_kobj_path: path = '/devices/pci0000:00/0000:00:01.0/0000:01:10.0' kobject: '0000:01:10.0' (ffff8801d79cd0a8): kobject_cleanup, parent (null) kobject: '0000:01:10.0' (ffff8801d79cd0a8): calling ktype release kobject: '0000:01:10.0': free name [kobject freed as expected] # dmesg -C # echo 1 > devices/0000\:01\:00.0/sriov_numvfs # echo 0000:01:10.0 > drivers_probe # echo 0 > devices/0000\:01\:00.0/sriov_numvfs # dmesg | grep 01:10 pci 0000:01:10.0: [8086:10ca] type 00 class 0x020000 kobject: '0000:01:10.0' (ffff8801d79ce0a8): kobject_add_internal: parent: '0000:00:01.0', set: 'devices' kobject: '0000:01:10.0' (ffff8801d79ce0a8): kobject_uevent_env kobject: '0000:01:10.0' (ffff8801d79ce0a8): fill_kobj_path: path = '/devices/pci0000:00/0000:00:01.0/0000:01:10.0' kobject: '0000:01:10.0' (ffff8801d79ce0a8): kobject_uevent_env kobject: '0000:01:10.0' (ffff8801d79ce0a8): fill_kobj_path: path = '/devices/pci0000:00/0000:00:01.0/0000:01:10.0' kobject: '0000:01:10.0' (ffff8801d79ce0a8): kobject_uevent_env kobject: '0000:01:10.0' (ffff8801d79ce0a8): fill_kobj_path: path = '/devices/pci0000:00/0000:00:01.0/0000:01:10.0' [no free] Signed-off-by: Alex Williamson Cc: stable@vger.kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/base/bus.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'drivers/base') diff --git a/drivers/base/bus.c b/drivers/base/bus.c index 83e910a57563..876bae5ade33 100644 --- a/drivers/base/bus.c +++ b/drivers/base/bus.c @@ -254,13 +254,15 @@ static ssize_t store_drivers_probe(struct bus_type *bus, const char *buf, size_t count) { struct device *dev; + int err = -EINVAL; dev = bus_find_device_by_name(bus, NULL, buf); if (!dev) return -ENODEV; - if (bus_rescan_devices_helper(dev, NULL) != 0) - return -EINVAL; - return count; + if (bus_rescan_devices_helper(dev, NULL) == 0) + err = count; + put_device(dev); + return err; } static struct device *next_device(struct klist_iter *i) -- cgit v1.2.3 From 5aaba36318e5995e8c95d077a46d9a4d00fcc1cd Mon Sep 17 00:00:00 2001 From: Sudeep Holla Date: Tue, 30 Sep 2014 14:48:22 +0100 Subject: cpumask: factor out show_cpumap into separate helper function Many sysfs *_show function use cpu{list,mask}_scnprintf to copy cpumap to the buffer aligned to PAGE_SIZE, append '\n' and '\0' to return null terminated buffer with newline. This patch creates a new helper function cpumap_print_to_pagebuf in cpumask.h using newly added bitmap_print_to_pagebuf and consolidates most of those sysfs functions using the new helper function. Signed-off-by: Sudeep Holla Suggested-by: Stephen Boyd Tested-by: Stephen Boyd Acked-by: "Rafael J. Wysocki" Acked-by: Bjorn Helgaas Acked-by: Peter Zijlstra (Intel) Cc: Greg Kroah-Hartman Cc: x86@kernel.org Cc: linux-acpi@vger.kernel.org Cc: linux-pci@vger.kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/base/cpu.c | 5 +---- drivers/base/node.c | 14 ++++---------- drivers/base/topology.c | 22 ++-------------------- 3 files changed, 7 insertions(+), 34 deletions(-) (limited to 'drivers/base') diff --git a/drivers/base/cpu.c b/drivers/base/cpu.c index 006b1bc5297d..4d8a56406fbb 100644 --- a/drivers/base/cpu.c +++ b/drivers/base/cpu.c @@ -207,11 +207,8 @@ static ssize_t show_cpus_attr(struct device *dev, char *buf) { struct cpu_attr *ca = container_of(attr, struct cpu_attr, attr); - int n = cpulist_scnprintf(buf, PAGE_SIZE-2, *(ca->map)); - buf[n++] = '\n'; - buf[n] = '\0'; - return n; + return cpumap_print_to_pagebuf(true, buf, *ca->map); } #define _CPU_ATTR(name, map) \ diff --git a/drivers/base/node.c b/drivers/base/node.c index 472168cd0c97..a3b82e9c7f20 100644 --- a/drivers/base/node.c +++ b/drivers/base/node.c @@ -25,32 +25,26 @@ static struct bus_type node_subsys = { }; -static ssize_t node_read_cpumap(struct device *dev, int type, char *buf) +static ssize_t node_read_cpumap(struct device *dev, bool list, char *buf) { struct node *node_dev = to_node(dev); const struct cpumask *mask = cpumask_of_node(node_dev->dev.id); - int len; /* 2008/04/07: buf currently PAGE_SIZE, need 9 chars per 32 bits. */ BUILD_BUG_ON((NR_CPUS/32 * 9) > (PAGE_SIZE-1)); - len = type? - cpulist_scnprintf(buf, PAGE_SIZE-2, mask) : - cpumask_scnprintf(buf, PAGE_SIZE-2, mask); - buf[len++] = '\n'; - buf[len] = '\0'; - return len; + return cpumap_print_to_pagebuf(list, buf, mask); } static inline ssize_t node_read_cpumask(struct device *dev, struct device_attribute *attr, char *buf) { - return node_read_cpumap(dev, 0, buf); + return node_read_cpumap(dev, false, buf); } static inline ssize_t node_read_cpulist(struct device *dev, struct device_attribute *attr, char *buf) { - return node_read_cpumap(dev, 1, buf); + return node_read_cpumap(dev, true, buf); } static DEVICE_ATTR(cpumap, S_IRUGO, node_read_cpumask, NULL); diff --git a/drivers/base/topology.c b/drivers/base/topology.c index be7c1fb7c0c9..f7c353843ddf 100644 --- a/drivers/base/topology.c +++ b/drivers/base/topology.c @@ -42,29 +42,11 @@ static ssize_t show_##name(struct device *dev, \ return sprintf(buf, "%d\n", topology_##name(dev->id)); \ } -#if defined(topology_thread_cpumask) || defined(topology_core_cpumask) || \ - defined(topology_book_cpumask) -static ssize_t show_cpumap(int type, const struct cpumask *mask, char *buf) -{ - ptrdiff_t len = PTR_ALIGN(buf + PAGE_SIZE - 1, PAGE_SIZE) - buf; - int n = 0; - - if (len > 1) { - n = type? - cpulist_scnprintf(buf, len-2, mask) : - cpumask_scnprintf(buf, len-2, mask); - buf[n++] = '\n'; - buf[n] = '\0'; - } - return n; -} -#endif - #define define_siblings_show_map(name) \ static ssize_t show_##name(struct device *dev, \ struct device_attribute *attr, char *buf) \ { \ - return show_cpumap(0, topology_##name(dev->id), buf); \ + return cpumap_print_to_pagebuf(false, buf, topology_##name(dev->id));\ } #define define_siblings_show_list(name) \ @@ -72,7 +54,7 @@ static ssize_t show_##name##_list(struct device *dev, \ struct device_attribute *attr, \ char *buf) \ { \ - return show_cpumap(1, topology_##name(dev->id), buf); \ + return cpumap_print_to_pagebuf(true, buf, topology_##name(dev->id));\ } #define define_siblings_show_func(name) \ -- cgit v1.2.3 From d6ea8d01d1893e1299a4016d62fdda870b8cc215 Mon Sep 17 00:00:00 2001 From: Sudeep Holla Date: Tue, 30 Sep 2014 14:48:23 +0100 Subject: topology: replace custom attribute macros with standard DEVICE_ATTR* Currently couple of custom macros are defined to declare the device attributes. However there are already standard macros defined in device.h that suffice the need and these custom macros can be removed. This patch replaces custom attribute macros with standard DEVICE_ATTR_RO attribute Signed-off-by: Sudeep Holla Cc: Greg Kroah-Hartman Signed-off-by: Greg Kroah-Hartman --- drivers/base/topology.c | 53 ++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 29 deletions(-) (limited to 'drivers/base') diff --git a/drivers/base/topology.c b/drivers/base/topology.c index f7c353843ddf..6491f45200a7 100644 --- a/drivers/base/topology.c +++ b/drivers/base/topology.c @@ -29,57 +29,52 @@ #include #include -#define define_one_ro_named(_name, _func) \ - static DEVICE_ATTR(_name, 0444, _func, NULL) - -#define define_one_ro(_name) \ - static DEVICE_ATTR(_name, 0444, show_##_name, NULL) - #define define_id_show_func(name) \ -static ssize_t show_##name(struct device *dev, \ +static ssize_t name##_show(struct device *dev, \ struct device_attribute *attr, char *buf) \ { \ return sprintf(buf, "%d\n", topology_##name(dev->id)); \ } -#define define_siblings_show_map(name) \ -static ssize_t show_##name(struct device *dev, \ +#define define_siblings_show_map(name, mask) \ +static ssize_t name##_show(struct device *dev, \ struct device_attribute *attr, char *buf) \ { \ - return cpumap_print_to_pagebuf(false, buf, topology_##name(dev->id));\ + return cpumap_print_to_pagebuf(false, buf, topology_##mask(dev->id));\ } -#define define_siblings_show_list(name) \ -static ssize_t show_##name##_list(struct device *dev, \ - struct device_attribute *attr, \ - char *buf) \ +#define define_siblings_show_list(name, mask) \ +static ssize_t name##_list_show(struct device *dev, \ + struct device_attribute *attr, \ + char *buf) \ { \ - return cpumap_print_to_pagebuf(true, buf, topology_##name(dev->id));\ + return cpumap_print_to_pagebuf(true, buf, topology_##mask(dev->id));\ } -#define define_siblings_show_func(name) \ - define_siblings_show_map(name); define_siblings_show_list(name) +#define define_siblings_show_func(name, mask) \ + define_siblings_show_map(name, mask); \ + define_siblings_show_list(name, mask) define_id_show_func(physical_package_id); -define_one_ro(physical_package_id); +static DEVICE_ATTR_RO(physical_package_id); define_id_show_func(core_id); -define_one_ro(core_id); +static DEVICE_ATTR_RO(core_id); -define_siblings_show_func(thread_cpumask); -define_one_ro_named(thread_siblings, show_thread_cpumask); -define_one_ro_named(thread_siblings_list, show_thread_cpumask_list); +define_siblings_show_func(thread_siblings, thread_cpumask); +static DEVICE_ATTR_RO(thread_siblings); +static DEVICE_ATTR_RO(thread_siblings_list); -define_siblings_show_func(core_cpumask); -define_one_ro_named(core_siblings, show_core_cpumask); -define_one_ro_named(core_siblings_list, show_core_cpumask_list); +define_siblings_show_func(core_siblings, core_cpumask); +static DEVICE_ATTR_RO(core_siblings); +static DEVICE_ATTR_RO(core_siblings_list); #ifdef CONFIG_SCHED_BOOK define_id_show_func(book_id); -define_one_ro(book_id); -define_siblings_show_func(book_cpumask); -define_one_ro_named(book_siblings, show_book_cpumask); -define_one_ro_named(book_siblings_list, show_book_cpumask_list); +static DEVICE_ATTR_RO(book_id); +define_siblings_show_func(book_siblings, book_cpumask); +static DEVICE_ATTR_RO(book_siblings); +static DEVICE_ATTR_RO(book_siblings_list); #endif static struct attribute *default_attrs[] = { -- cgit v1.2.3 From 3d52943b3a51497a777e6d7d840a38596a92cee9 Mon Sep 17 00:00:00 2001 From: Sudeep Holla Date: Tue, 30 Sep 2014 14:48:24 +0100 Subject: drivers: base: add cpu_device_create to support per-cpu devices This patch adds a new function to create per-cpu devices. This helps in: 1. reusing the device infrastructure to create any cpu related attributes and corresponding sysfs instead of creating and dealing with raw kobjects directly 2. retaining the legacy path(/sys/devices/system/cpu/..) to support existing sysfs ABI 3. avoiding to create links in the bus directory pointing to the device as there would be per-cpu instance of these devices with the same name since dev->bus is not populated to cpu_sysbus on purpose Signed-off-by: Sudeep Holla Tested-by: Stephen Boyd Cc: Greg Kroah-Hartman Cc: David Herrmann Cc: Kay Sievers Signed-off-by: Greg Kroah-Hartman --- drivers/base/cpu.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'drivers/base') diff --git a/drivers/base/cpu.c b/drivers/base/cpu.c index 4d8a56406fbb..f829a4c71749 100644 --- a/drivers/base/cpu.c +++ b/drivers/base/cpu.c @@ -363,6 +363,60 @@ struct device *get_cpu_device(unsigned cpu) } EXPORT_SYMBOL_GPL(get_cpu_device); +static void device_create_release(struct device *dev) +{ + kfree(dev); +} + +static struct device * +__cpu_device_create(struct device *parent, void *drvdata, + const struct attribute_group **groups, + const char *fmt, va_list args) +{ + struct device *dev = NULL; + int retval = -ENODEV; + + dev = kzalloc(sizeof(*dev), GFP_KERNEL); + if (!dev) { + retval = -ENOMEM; + goto error; + } + + device_initialize(dev); + dev->parent = parent; + dev->groups = groups; + dev->release = device_create_release; + dev_set_drvdata(dev, drvdata); + + retval = kobject_set_name_vargs(&dev->kobj, fmt, args); + if (retval) + goto error; + + retval = device_add(dev); + if (retval) + goto error; + + return dev; + +error: + put_device(dev); + return ERR_PTR(retval); +} + +struct device *cpu_device_create(struct device *parent, void *drvdata, + const struct attribute_group **groups, + const char *fmt, ...) +{ + va_list vargs; + struct device *dev; + + va_start(vargs, fmt); + dev = __cpu_device_create(parent, drvdata, groups, fmt, vargs); + va_end(vargs); + return dev; +} +EXPORT_SYMBOL_GPL(cpu_device_create); + #ifdef CONFIG_GENERIC_CPU_AUTOPROBE static DEVICE_ATTR(modalias, 0444, print_cpu_modalias, NULL); #endif -- cgit v1.2.3 From 246246cbde5e840012f853e27630ebb59f409486 Mon Sep 17 00:00:00 2001 From: Sudeep Holla Date: Tue, 30 Sep 2014 14:48:25 +0100 Subject: drivers: base: support cpu cache information interface to userspace via sysfs This patch adds initial support for providing processor cache information to userspace through sysfs interface. This is based on already existing implementations(x86, ia64, s390 and powerpc) and hence the interface is intended to be fully compatible. The main purpose of this generic support is to avoid further code duplication to support new architectures and also to unify all the existing different implementations. This implementation maintains the hierarchy of cache objects which reflects the system's cache topology. Cache devices are instantiated as needed as CPUs come online. The cache information is replicated per-cpu even if they are shared. A per-cpu array of cache information maintained is used mainly for sysfs-related book keeping. It also implements the shared_cpu_map attribute, which is essential for enabling both kernel and user-space to discover the system's overall cache topology. This patch also add the missing ABI documentation for the cacheinfo sysfs interface already, which is well defined and widely used. Signed-off-by: Sudeep Holla Reviewed-by: Stephen Boyd Tested-by: Stephen Boyd Cc: Greg Kroah-Hartman Cc: linux-api@vger.kernel.org Cc: linux390@de.ibm.com Cc: linux-arm-kernel@lists.infradead.org Cc: linux-ia64@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org Cc: linux-s390@vger.kernel.org Cc: x86@kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/base/Makefile | 2 +- drivers/base/cacheinfo.c | 541 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 542 insertions(+), 1 deletion(-) create mode 100644 drivers/base/cacheinfo.c (limited to 'drivers/base') diff --git a/drivers/base/Makefile b/drivers/base/Makefile index 6922cd6850a2..e81a55ca513c 100644 --- a/drivers/base/Makefile +++ b/drivers/base/Makefile @@ -4,7 +4,7 @@ obj-y := component.o core.o bus.o dd.o syscore.o \ driver.o class.o platform.o \ cpu.o firmware.o init.o map.o devres.o \ attribute_container.o transport_class.o \ - topology.o container.o + topology.o container.o cacheinfo.o obj-$(CONFIG_DEVTMPFS) += devtmpfs.o obj-$(CONFIG_DMA_CMA) += dma-contiguous.o obj-y += power/ diff --git a/drivers/base/cacheinfo.c b/drivers/base/cacheinfo.c new file mode 100644 index 000000000000..8ed4ea1f3716 --- /dev/null +++ b/drivers/base/cacheinfo.c @@ -0,0 +1,541 @@ +/* + * cacheinfo support - processor cache information via sysfs + * + * Based on arch/x86/kernel/cpu/intel_cacheinfo.c + * Author: Sudeep Holla + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed "as is" WITHOUT ANY WARRANTY of any + * kind, whether express or implied; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* pointer to per cpu cacheinfo */ +static DEFINE_PER_CPU(struct cpu_cacheinfo, ci_cpu_cacheinfo); +#define ci_cacheinfo(cpu) (&per_cpu(ci_cpu_cacheinfo, cpu)) +#define cache_leaves(cpu) (ci_cacheinfo(cpu)->num_leaves) +#define per_cpu_cacheinfo(cpu) (ci_cacheinfo(cpu)->info_list) + +struct cpu_cacheinfo *get_cpu_cacheinfo(unsigned int cpu) +{ + return ci_cacheinfo(cpu); +} + +#ifdef CONFIG_OF +static int cache_setup_of_node(unsigned int cpu) +{ + struct device_node *np; + struct cacheinfo *this_leaf; + struct device *cpu_dev = get_cpu_device(cpu); + struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu); + unsigned int index = 0; + + /* skip if of_node is already populated */ + if (this_cpu_ci->info_list->of_node) + return 0; + + if (!cpu_dev) { + pr_err("No cpu device for CPU %d\n", cpu); + return -ENODEV; + } + np = cpu_dev->of_node; + if (!np) { + pr_err("Failed to find cpu%d device node\n", cpu); + return -ENOENT; + } + + while (np && index < cache_leaves(cpu)) { + this_leaf = this_cpu_ci->info_list + index; + if (this_leaf->level != 1) + np = of_find_next_cache_node(np); + else + np = of_node_get(np);/* cpu node itself */ + this_leaf->of_node = np; + index++; + } + return 0; +} + +static inline bool cache_leaves_are_shared(struct cacheinfo *this_leaf, + struct cacheinfo *sib_leaf) +{ + return sib_leaf->of_node == this_leaf->of_node; +} +#else +static inline int cache_setup_of_node(unsigned int cpu) { return 0; } +static inline bool cache_leaves_are_shared(struct cacheinfo *this_leaf, + struct cacheinfo *sib_leaf) +{ + /* + * For non-DT systems, assume unique level 1 cache, system-wide + * shared caches for all other levels. This will be used only if + * arch specific code has not populated shared_cpu_map + */ + return !(this_leaf->level == 1); +} +#endif + +static int cache_shared_cpu_map_setup(unsigned int cpu) +{ + struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu); + struct cacheinfo *this_leaf, *sib_leaf; + unsigned int index; + int ret; + + ret = cache_setup_of_node(cpu); + if (ret) + return ret; + + for (index = 0; index < cache_leaves(cpu); index++) { + unsigned int i; + + this_leaf = this_cpu_ci->info_list + index; + /* skip if shared_cpu_map is already populated */ + if (!cpumask_empty(&this_leaf->shared_cpu_map)) + continue; + + cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map); + for_each_online_cpu(i) { + struct cpu_cacheinfo *sib_cpu_ci = get_cpu_cacheinfo(i); + + if (i == cpu || !sib_cpu_ci->info_list) + continue;/* skip if itself or no cacheinfo */ + sib_leaf = sib_cpu_ci->info_list + index; + if (cache_leaves_are_shared(this_leaf, sib_leaf)) { + cpumask_set_cpu(cpu, &sib_leaf->shared_cpu_map); + cpumask_set_cpu(i, &this_leaf->shared_cpu_map); + } + } + } + + return 0; +} + +static void cache_shared_cpu_map_remove(unsigned int cpu) +{ + struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu); + struct cacheinfo *this_leaf, *sib_leaf; + unsigned int sibling, index; + + for (index = 0; index < cache_leaves(cpu); index++) { + this_leaf = this_cpu_ci->info_list + index; + for_each_cpu(sibling, &this_leaf->shared_cpu_map) { + struct cpu_cacheinfo *sib_cpu_ci; + + if (sibling == cpu) /* skip itself */ + continue; + sib_cpu_ci = get_cpu_cacheinfo(sibling); + sib_leaf = sib_cpu_ci->info_list + index; + cpumask_clear_cpu(cpu, &sib_leaf->shared_cpu_map); + cpumask_clear_cpu(sibling, &this_leaf->shared_cpu_map); + } + of_node_put(this_leaf->of_node); + } +} + +static void free_cache_attributes(unsigned int cpu) +{ + cache_shared_cpu_map_remove(cpu); + + kfree(per_cpu_cacheinfo(cpu)); + per_cpu_cacheinfo(cpu) = NULL; +} + +int __weak init_cache_level(unsigned int cpu) +{ + return -ENOENT; +} + +int __weak populate_cache_leaves(unsigned int cpu) +{ + return -ENOENT; +} + +static int detect_cache_attributes(unsigned int cpu) +{ + int ret; + + if (init_cache_level(cpu)) + return -ENOENT; + + per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu), + sizeof(struct cacheinfo), GFP_KERNEL); + if (per_cpu_cacheinfo(cpu) == NULL) + return -ENOMEM; + + ret = populate_cache_leaves(cpu); + if (ret) + goto free_ci; + /* + * For systems using DT for cache hierarcy, of_node and shared_cpu_map + * will be set up here only if they are not populated already + */ + ret = cache_shared_cpu_map_setup(cpu); + if (ret) + goto free_ci; + return 0; + +free_ci: + free_cache_attributes(cpu); + return ret; +} + +/* pointer to cpuX/cache device */ +static DEFINE_PER_CPU(struct device *, ci_cache_dev); +#define per_cpu_cache_dev(cpu) (per_cpu(ci_cache_dev, cpu)) + +static cpumask_t cache_dev_map; + +/* pointer to array of devices for cpuX/cache/indexY */ +static DEFINE_PER_CPU(struct device **, ci_index_dev); +#define per_cpu_index_dev(cpu) (per_cpu(ci_index_dev, cpu)) +#define per_cache_index_dev(cpu, idx) ((per_cpu_index_dev(cpu))[idx]) + +#define show_one(file_name, object) \ +static ssize_t file_name##_show(struct device *dev, \ + struct device_attribute *attr, char *buf) \ +{ \ + struct cacheinfo *this_leaf = dev_get_drvdata(dev); \ + return sprintf(buf, "%u\n", this_leaf->object); \ +} + +show_one(level, level); +show_one(coherency_line_size, coherency_line_size); +show_one(number_of_sets, number_of_sets); +show_one(physical_line_partition, physical_line_partition); +show_one(ways_of_associativity, ways_of_associativity); + +static ssize_t size_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct cacheinfo *this_leaf = dev_get_drvdata(dev); + + return sprintf(buf, "%uK\n", this_leaf->size >> 10); +} + +static ssize_t shared_cpumap_show_func(struct device *dev, bool list, char *buf) +{ + struct cacheinfo *this_leaf = dev_get_drvdata(dev); + const struct cpumask *mask = &this_leaf->shared_cpu_map; + + return cpumap_print_to_pagebuf(list, buf, mask); +} + +static ssize_t shared_cpu_map_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + return shared_cpumap_show_func(dev, false, buf); +} + +static ssize_t shared_cpu_list_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + return shared_cpumap_show_func(dev, true, buf); +} + +static ssize_t type_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct cacheinfo *this_leaf = dev_get_drvdata(dev); + + switch (this_leaf->type) { + case CACHE_TYPE_DATA: + return sprintf(buf, "Data\n"); + case CACHE_TYPE_INST: + return sprintf(buf, "Instruction\n"); + case CACHE_TYPE_UNIFIED: + return sprintf(buf, "Unified\n"); + default: + return -EINVAL; + } +} + +static ssize_t allocation_policy_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct cacheinfo *this_leaf = dev_get_drvdata(dev); + unsigned int ci_attr = this_leaf->attributes; + int n = 0; + + if ((ci_attr & CACHE_READ_ALLOCATE) && (ci_attr & CACHE_WRITE_ALLOCATE)) + n = sprintf(buf, "ReadWriteAllocate\n"); + else if (ci_attr & CACHE_READ_ALLOCATE) + n = sprintf(buf, "ReadAllocate\n"); + else if (ci_attr & CACHE_WRITE_ALLOCATE) + n = sprintf(buf, "WriteAllocate\n"); + return n; +} + +static ssize_t write_policy_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct cacheinfo *this_leaf = dev_get_drvdata(dev); + unsigned int ci_attr = this_leaf->attributes; + int n = 0; + + if (ci_attr & CACHE_WRITE_THROUGH) + n = sprintf(buf, "WriteThrough\n"); + else if (ci_attr & CACHE_WRITE_BACK) + n = sprintf(buf, "WriteBack\n"); + return n; +} + +static DEVICE_ATTR_RO(level); +static DEVICE_ATTR_RO(type); +static DEVICE_ATTR_RO(coherency_line_size); +static DEVICE_ATTR_RO(ways_of_associativity); +static DEVICE_ATTR_RO(number_of_sets); +static DEVICE_ATTR_RO(size); +static DEVICE_ATTR_RO(allocation_policy); +static DEVICE_ATTR_RO(write_policy); +static DEVICE_ATTR_RO(shared_cpu_map); +static DEVICE_ATTR_RO(shared_cpu_list); +static DEVICE_ATTR_RO(physical_line_partition); + +static struct attribute *cache_default_attrs[] = { + &dev_attr_type.attr, + &dev_attr_level.attr, + &dev_attr_shared_cpu_map.attr, + &dev_attr_shared_cpu_list.attr, + &dev_attr_coherency_line_size.attr, + &dev_attr_ways_of_associativity.attr, + &dev_attr_number_of_sets.attr, + &dev_attr_size.attr, + &dev_attr_allocation_policy.attr, + &dev_attr_write_policy.attr, + &dev_attr_physical_line_partition.attr, + NULL +}; + +static umode_t +cache_default_attrs_is_visible(struct kobject *kobj, + struct attribute *attr, int unused) +{ + struct device *dev = kobj_to_dev(kobj); + struct cacheinfo *this_leaf = dev_get_drvdata(dev); + const struct cpumask *mask = &this_leaf->shared_cpu_map; + umode_t mode = attr->mode; + + if ((attr == &dev_attr_type.attr) && this_leaf->type) + return mode; + if ((attr == &dev_attr_level.attr) && this_leaf->level) + return mode; + if ((attr == &dev_attr_shared_cpu_map.attr) && !cpumask_empty(mask)) + return mode; + if ((attr == &dev_attr_shared_cpu_list.attr) && !cpumask_empty(mask)) + return mode; + if ((attr == &dev_attr_coherency_line_size.attr) && + this_leaf->coherency_line_size) + return mode; + if ((attr == &dev_attr_ways_of_associativity.attr) && + this_leaf->size) /* allow 0 = full associativity */ + return mode; + if ((attr == &dev_attr_number_of_sets.attr) && + this_leaf->number_of_sets) + return mode; + if ((attr == &dev_attr_size.attr) && this_leaf->size) + return mode; + if ((attr == &dev_attr_write_policy.attr) && + (this_leaf->attributes & CACHE_WRITE_POLICY_MASK)) + return mode; + if ((attr == &dev_attr_allocation_policy.attr) && + (this_leaf->attributes & CACHE_ALLOCATE_POLICY_MASK)) + return mode; + if ((attr == &dev_attr_physical_line_partition.attr) && + this_leaf->physical_line_partition) + return mode; + + return 0; +} + +static const struct attribute_group cache_default_group = { + .attrs = cache_default_attrs, + .is_visible = cache_default_attrs_is_visible, +}; + +static const struct attribute_group *cache_default_groups[] = { + &cache_default_group, + NULL, +}; + +static const struct attribute_group *cache_private_groups[] = { + &cache_default_group, + NULL, /* Place holder for private group */ + NULL, +}; + +const struct attribute_group * +__weak cache_get_priv_group(struct cacheinfo *this_leaf) +{ + return NULL; +} + +static const struct attribute_group ** +cache_get_attribute_groups(struct cacheinfo *this_leaf) +{ + const struct attribute_group *priv_group = + cache_get_priv_group(this_leaf); + + if (!priv_group) + return cache_default_groups; + + if (!cache_private_groups[1]) + cache_private_groups[1] = priv_group; + + return cache_private_groups; +} + +/* Add/Remove cache interface for CPU device */ +static void cpu_cache_sysfs_exit(unsigned int cpu) +{ + int i; + struct device *ci_dev; + + if (per_cpu_index_dev(cpu)) { + for (i = 0; i < cache_leaves(cpu); i++) { + ci_dev = per_cache_index_dev(cpu, i); + if (!ci_dev) + continue; + device_unregister(ci_dev); + } + kfree(per_cpu_index_dev(cpu)); + per_cpu_index_dev(cpu) = NULL; + } + device_unregister(per_cpu_cache_dev(cpu)); + per_cpu_cache_dev(cpu) = NULL; +} + +static int cpu_cache_sysfs_init(unsigned int cpu) +{ + struct device *dev = get_cpu_device(cpu); + + if (per_cpu_cacheinfo(cpu) == NULL) + return -ENOENT; + + per_cpu_cache_dev(cpu) = cpu_device_create(dev, NULL, NULL, "cache"); + if (IS_ERR(per_cpu_cache_dev(cpu))) + return PTR_ERR(per_cpu_cache_dev(cpu)); + + /* Allocate all required memory */ + per_cpu_index_dev(cpu) = kcalloc(cache_leaves(cpu), + sizeof(struct device *), GFP_KERNEL); + if (unlikely(per_cpu_index_dev(cpu) == NULL)) + goto err_out; + + return 0; + +err_out: + cpu_cache_sysfs_exit(cpu); + return -ENOMEM; +} + +static int cache_add_dev(unsigned int cpu) +{ + unsigned int i; + int rc; + struct device *ci_dev, *parent; + struct cacheinfo *this_leaf; + struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu); + const struct attribute_group **cache_groups; + + rc = cpu_cache_sysfs_init(cpu); + if (unlikely(rc < 0)) + return rc; + + parent = per_cpu_cache_dev(cpu); + for (i = 0; i < cache_leaves(cpu); i++) { + this_leaf = this_cpu_ci->info_list + i; + if (this_leaf->disable_sysfs) + continue; + cache_groups = cache_get_attribute_groups(this_leaf); + ci_dev = cpu_device_create(parent, this_leaf, cache_groups, + "index%1u", i); + if (IS_ERR(ci_dev)) { + rc = PTR_ERR(ci_dev); + goto err; + } + per_cache_index_dev(cpu, i) = ci_dev; + } + cpumask_set_cpu(cpu, &cache_dev_map); + + return 0; +err: + cpu_cache_sysfs_exit(cpu); + return rc; +} + +static void cache_remove_dev(unsigned int cpu) +{ + if (!cpumask_test_cpu(cpu, &cache_dev_map)) + return; + cpumask_clear_cpu(cpu, &cache_dev_map); + + cpu_cache_sysfs_exit(cpu); +} + +static int cacheinfo_cpu_callback(struct notifier_block *nfb, + unsigned long action, void *hcpu) +{ + unsigned int cpu = (unsigned long)hcpu; + int rc = 0; + + switch (action & ~CPU_TASKS_FROZEN) { + case CPU_ONLINE: + rc = detect_cache_attributes(cpu); + if (!rc) + rc = cache_add_dev(cpu); + break; + case CPU_DEAD: + cache_remove_dev(cpu); + if (per_cpu_cacheinfo(cpu)) + free_cache_attributes(cpu); + break; + } + return notifier_from_errno(rc); +} + +static int __init cacheinfo_sysfs_init(void) +{ + int cpu, rc = 0; + + cpu_notifier_register_begin(); + + for_each_online_cpu(cpu) { + rc = detect_cache_attributes(cpu); + if (rc) { + pr_err("error detecting cacheinfo..cpu%d\n", cpu); + goto out; + } + rc = cache_add_dev(cpu); + if (rc) { + free_cache_attributes(cpu); + pr_err("error populating cacheinfo..cpu%d\n", cpu); + goto out; + } + } + __hotcpu_notifier(cacheinfo_cpu_callback, 0); + +out: + cpu_notifier_register_done(); + return rc; +} + +device_initcall(cacheinfo_sysfs_init); -- cgit v1.2.3 From 6386a15c445c1cd0108671a0078a4b65af89799c Mon Sep 17 00:00:00 2001 From: Wolfram Sang Date: Tue, 11 Nov 2014 00:49:25 +0100 Subject: Revert "core: platform: add warning if driver has no owner" This is too noisy at the moment, triggered by codepaths not accessed on our test-systems. Needs more investigation. Signed-off-by: Wolfram Sang Signed-off-by: Greg Kroah-Hartman --- drivers/base/driver.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'drivers/base') diff --git a/drivers/base/driver.c b/drivers/base/driver.c index 6b10ff3bb410..9e29943e56ca 100644 --- a/drivers/base/driver.c +++ b/drivers/base/driver.c @@ -151,9 +151,6 @@ int driver_register(struct device_driver *drv) BUG_ON(!drv->bus->p); - if (!drv->owner) - printk(KERN_WARNING "Driver '%s' needs an owner", drv->name); - if ((drv->bus->probe && drv->probe) || (drv->bus->remove && drv->remove) || (drv->bus->shutdown && drv->shutdown)) -- cgit v1.2.3 From 6df43c9b4d87b6767debdfa8318c5374415e8fc0 Mon Sep 17 00:00:00 2001 From: Sudeep Holla Date: Wed, 12 Nov 2014 14:42:53 +0000 Subject: drivers/base: cacheinfo: remove noisy error boot message On systems that don't support cacheinfo, this error message can be considered noisy and irrelevant. The error messages can be added to the functions that architectures implement overiding the weak default definition if really required. This patch removes the concerned error message in the generic code. Signed-off-by: Sudeep Holla Reported-by: Fabio Estevam Signed-off-by: Greg Kroah-Hartman --- drivers/base/cacheinfo.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'drivers/base') diff --git a/drivers/base/cacheinfo.c b/drivers/base/cacheinfo.c index 8ed4ea1f3716..6e64563361f0 100644 --- a/drivers/base/cacheinfo.c +++ b/drivers/base/cacheinfo.c @@ -520,10 +520,8 @@ static int __init cacheinfo_sysfs_init(void) for_each_online_cpu(cpu) { rc = detect_cache_attributes(cpu); - if (rc) { - pr_err("error detecting cacheinfo..cpu%d\n", cpu); + if (rc) goto out; - } rc = cache_add_dev(cpu); if (rc) { free_cache_attributes(cpu); -- cgit v1.2.3 From d45333294da8341f64d22219729249f7102c490e Mon Sep 17 00:00:00 2001 From: Johannes Berg Date: Thu, 13 Nov 2014 22:16:29 +0100 Subject: devcoredump: provide a one-way disable function Since device/firmware coredumps can contain private data, it can be desirable to turn them off unconditionally to be certain that no such data will be collected by the system. To achieve this, provide a "disabled" sysfs class attribute that can only be changed from 0 to 1 and not back. Upon disabling, discard existing coredumps and stop storing new ones. Signed-off-by: Johannes Berg Reviewed-by: Kees Cook Signed-off-by: Greg Kroah-Hartman --- drivers/base/devcoredump.c | 56 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 8 deletions(-) (limited to 'drivers/base') diff --git a/drivers/base/devcoredump.c b/drivers/base/devcoredump.c index 96614b04544c..1bd120a0b084 100644 --- a/drivers/base/devcoredump.c +++ b/drivers/base/devcoredump.c @@ -31,6 +31,11 @@ #include #include +static struct class devcd_class; + +/* global disable flag, for security purposes */ +static bool devcd_disabled; + /* if data isn't read by userspace after 5 minutes then delete it */ #define DEVCD_TIMEOUT (HZ * 60 * 5) @@ -121,11 +126,51 @@ static const struct attribute_group *devcd_dev_groups[] = { &devcd_dev_group, NULL, }; +static int devcd_free(struct device *dev, void *data) +{ + struct devcd_entry *devcd = dev_to_devcd(dev); + + flush_delayed_work(&devcd->del_wk); + return 0; +} + +static ssize_t disabled_show(struct class *class, struct class_attribute *attr, + char *buf) +{ + return sprintf(buf, "%d\n", devcd_disabled); +} + +static ssize_t disabled_store(struct class *class, struct class_attribute *attr, + const char *buf, size_t count) +{ + long tmp = simple_strtol(buf, NULL, 10); + + /* + * This essentially makes the attribute write-once, since you can't + * go back to not having it disabled. This is intentional, it serves + * as a system lockdown feature. + */ + if (tmp != 1) + return -EINVAL; + + devcd_disabled = true; + + class_for_each_device(&devcd_class, NULL, NULL, devcd_free); + + return count; +} + +static struct class_attribute devcd_class_attrs[] = { + __ATTR_RW(disabled), + __ATTR_NULL +}; + static struct class devcd_class = { .name = "devcoredump", .owner = THIS_MODULE, .dev_release = devcd_dev_release, .dev_groups = devcd_dev_groups, + .class_attrs = devcd_class_attrs, }; static ssize_t devcd_readv(char *buffer, loff_t offset, size_t count, @@ -192,6 +237,9 @@ void dev_coredumpm(struct device *dev, struct module *owner, struct devcd_entry *devcd; struct device *existing; + if (devcd_disabled) + goto free; + existing = class_find_device(&devcd_class, NULL, dev, devcd_match_failing); if (existing) { @@ -249,14 +297,6 @@ static int __init devcoredump_init(void) } __initcall(devcoredump_init); -static int devcd_free(struct device *dev, void *data) -{ - struct devcd_entry *devcd = dev_to_devcd(dev); - - flush_delayed_work(&devcd->del_wk); - return 0; -} - static void __exit devcoredump_exit(void) { class_for_each_device(&devcd_class, NULL, NULL, devcd_free); -- cgit v1.2.3 From 000deba73a85381d393d58ce0a2a5ccf6a096b5a Mon Sep 17 00:00:00 2001 From: "Kweh, Hock Leong" Date: Tue, 18 Nov 2014 10:56:59 +0800 Subject: firmware loader: fix hung task warning dump When using request_firmware_nowait() with FW_ACTION_NOHOTPLUG param to expose user helper interface, if the user do not react immediately, after 120 seconds there will be a hung task warning message dumped as below: [ 3000.784235] INFO: task kworker/0:0:8259 blocked for more than 120 seconds. [ 3000.791281] Tainted: G E 3.16.0-rc1-yocto-standard #41 [ 3000.798082] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. [ 3000.806072] kworker/0:0 D cd0075c8 0 8259 2 0x00000000 [ 3000.812765] Workqueue: events request_firmware_work_func [ 3000.818253] cd375e18 00000046 0000000e cd0075c8 000000f0 cd40ea00 cd375fec 1b883e89 [ 3000.826374] 0000026b cd40ea00 80000000 00000001 cd0075c8 00000000 cd375de4 c119917f [ 3000.834492] cd563360 cd375df4 c119a0ab cd563360 00000000 cd375e24 c119a1d6 00000000 [ 3000.842616] Call Trace: [ 3000.845252] [] ? kernfs_next_descendant_post+0x3f/0x50 [ 3000.851543] [] ? kernfs_activate+0x6b/0xc0 [ 3000.856790] [] ? kernfs_add_one+0xd6/0x130 [ 3000.862047] [] schedule+0x22/0x60 [ 3000.866548] [] schedule_timeout+0x175/0x1d0 [ 3000.871887] [] ? __kernfs_create_file+0x71/0xa0 [ 3000.877574] [] ? sysfs_add_file_mode_ns+0xaa/0x180 [ 3000.883533] [] wait_for_completion+0x6f/0xb0 [ 3000.888961] [] ? wake_up_process+0x40/0x40 [ 3000.894219] [] _request_firmware+0x750/0x9f0 [ 3000.899666] [] ? n_tty_receive_buf2+0x1f/0x30 [ 3000.905200] [] request_firmware_work_func+0x22/0x50 [ 3000.911235] [] process_one_work+0x122/0x380 [ 3000.916571] [] worker_thread+0xf9/0x470 [ 3000.921555] [] ? create_and_start_worker+0x50/0x50 [ 3000.927497] [] ? create_and_start_worker+0x50/0x50 [ 3000.933448] [] kthread+0x9f/0xc0 [ 3000.937850] [] ret_from_kernel_thread+0x20/0x30 [ 3000.943548] [] ? kthread_worker_fn+0x100/0x100 This patch change the wait_for_completion() function call to wait_for_completion_interruptible() function call for solving the issue. Cc: Matt Fleming Signed-off-by: Kweh, Hock Leong Acked-by: Ming Lei Acked-by: Greg Kroah-Hartman Signed-off-by: Greg Kroah-Hartman --- drivers/base/firmware_class.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'drivers/base') diff --git a/drivers/base/firmware_class.c b/drivers/base/firmware_class.c index 3d785ebb48d3..9962744b59aa 100644 --- a/drivers/base/firmware_class.c +++ b/drivers/base/firmware_class.c @@ -925,7 +925,7 @@ static int _request_firmware_load(struct firmware_priv *fw_priv, kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD); } - wait_for_completion(&buf->completion); + retval = wait_for_completion_interruptible(&buf->completion); cancel_delayed_work_sync(&fw_priv->timeout_work); if (is_fw_load_aborted(buf)) @@ -1004,7 +1004,7 @@ static int sync_cached_firmware_buf(struct firmware_buf *buf) break; } mutex_unlock(&fw_lock); - wait_for_completion(&buf->completion); + ret = wait_for_completion_interruptible(&buf->completion); mutex_lock(&fw_lock); } mutex_unlock(&fw_lock); -- cgit v1.2.3 From daa3d67fa3b5a997761ba594c6bca41b3e78963f Mon Sep 17 00:00:00 2001 From: Markus Elfring Date: Wed, 19 Nov 2014 11:38:38 +0100 Subject: firmware class: Deletion of an unnecessary check before the function call "vunmap" The vunmap() function performes also input parameter validation. Thus the test around the call is not needed. This issue was detected by using the Coccinelle software. Signed-off-by: Markus Elfring Signed-off-by: Greg Kroah-Hartman --- drivers/base/firmware_class.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'drivers/base') diff --git a/drivers/base/firmware_class.c b/drivers/base/firmware_class.c index 9962744b59aa..58470c395301 100644 --- a/drivers/base/firmware_class.c +++ b/drivers/base/firmware_class.c @@ -591,8 +591,7 @@ static int fw_map_pages_buf(struct firmware_buf *buf) if (!buf->is_paged_buf) return 0; - if (buf->data) - vunmap(buf->data); + vunmap(buf->data); buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO); if (!buf->data) return -ENOMEM; -- cgit v1.2.3