Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'driver-core-3.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core

Pull driver core patches from Greg KH:
"Here are some driver core patches for 3.11-rc2. They aren't really
bugfixes, but a bunch of new helper macros for drivers to properly
create attribute groups, which drivers and subsystems need to fix up a
ton of race issues with incorrectly creating sysfs files (binary and
normal) after userspace has been told that the device is present.

Also here is the ability to create binary files as attribute groups,
to solve that race condition, which was impossible to do before this,
so that's my fault the drivers were broken.

The majority of the .c changes is indenting and moving code around a
bit. It affects no existing code, but allows the large backlog of 70+
patches that I already have created to start flowing into the
different subtrees, instead of having to live in my driver-core tree,
causing merge nightmares in linux-next for the next few months.

These were finalized too late for the -rc1 merge window, which is why
they were didn't make that pull request, testing and review from
others didn't happen until a few weeks ago, and then there's the whole
distraction of the past few days, which prevented these from getting
to you sooner, sorry about that.

Oh, and there's a bugfix for the documentation build warning in here
as well. All of these have been in linux-next this week, with no
reported problems"

* tag 'driver-core-3.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core:
driver-core: fix new kernel-doc warning in base/platform.c
sysfs: use file mode defines from stat.h
sysfs: add more helper macro's for (bin_)attribute(_groups)
driver core: add default groups to struct class
driver core: Introduce device_create_groups
sysfs: prevent warning when only using binary attributes
sysfs: add support for binary attributes in groups
driver core: device.h: add RW and RO attribute macros
sysfs.h: add BIN_ATTR macro
sysfs.h: add ATTRIBUTE_GROUPS() macro
sysfs.h: add __ATTR_RW() macro

+227 -75
+94 -32
drivers/base/core.c
··· 528 528 int error; 529 529 530 530 if (class) { 531 - error = device_add_attributes(dev, class->dev_attrs); 531 + error = device_add_groups(dev, class->dev_groups); 532 532 if (error) 533 533 return error; 534 + error = device_add_attributes(dev, class->dev_attrs); 535 + if (error) 536 + goto err_remove_class_groups; 534 537 error = device_add_bin_attributes(dev, class->dev_bin_attrs); 535 538 if (error) 536 539 goto err_remove_class_attrs; ··· 566 563 err_remove_class_attrs: 567 564 if (class) 568 565 device_remove_attributes(dev, class->dev_attrs); 566 + err_remove_class_groups: 567 + if (class) 568 + device_remove_groups(dev, class->dev_groups); 569 569 570 570 return error; 571 571 } ··· 587 581 if (class) { 588 582 device_remove_attributes(dev, class->dev_attrs); 589 583 device_remove_bin_attributes(dev, class->dev_bin_attrs); 584 + device_remove_groups(dev, class->dev_groups); 590 585 } 591 586 } 592 587 ··· 1674 1667 kfree(dev); 1675 1668 } 1676 1669 1670 + static struct device * 1671 + device_create_groups_vargs(struct class *class, struct device *parent, 1672 + dev_t devt, void *drvdata, 1673 + const struct attribute_group **groups, 1674 + const char *fmt, va_list args) 1675 + { 1676 + struct device *dev = NULL; 1677 + int retval = -ENODEV; 1678 + 1679 + if (class == NULL || IS_ERR(class)) 1680 + goto error; 1681 + 1682 + dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1683 + if (!dev) { 1684 + retval = -ENOMEM; 1685 + goto error; 1686 + } 1687 + 1688 + dev->devt = devt; 1689 + dev->class = class; 1690 + dev->parent = parent; 1691 + dev->groups = groups; 1692 + dev->release = device_create_release; 1693 + dev_set_drvdata(dev, drvdata); 1694 + 1695 + retval = kobject_set_name_vargs(&dev->kobj, fmt, args); 1696 + if (retval) 1697 + goto error; 1698 + 1699 + retval = device_register(dev); 1700 + if (retval) 1701 + goto error; 1702 + 1703 + return dev; 1704 + 1705 + error: 1706 + put_device(dev); 1707 + return ERR_PTR(retval); 1708 + } 1709 + 1677 1710 /** 1678 1711 * device_create_vargs - creates a device and registers it with sysfs 1679 1712 * @class: pointer to the struct class that this device should be registered to ··· 1743 1696 dev_t devt, void *drvdata, const char *fmt, 1744 1697 va_list args) 1745 1698 { 1746 - struct device *dev = NULL; 1747 - int retval = -ENODEV; 1748 - 1749 - if (class == NULL || IS_ERR(class)) 1750 - goto error; 1751 - 1752 - dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1753 - if (!dev) { 1754 - retval = -ENOMEM; 1755 - goto error; 1756 - } 1757 - 1758 - dev->devt = devt; 1759 - dev->class = class; 1760 - dev->parent = parent; 1761 - dev->release = device_create_release; 1762 - dev_set_drvdata(dev, drvdata); 1763 - 1764 - retval = kobject_set_name_vargs(&dev->kobj, fmt, args); 1765 - if (retval) 1766 - goto error; 1767 - 1768 - retval = device_register(dev); 1769 - if (retval) 1770 - goto error; 1771 - 1772 - return dev; 1773 - 1774 - error: 1775 - put_device(dev); 1776 - return ERR_PTR(retval); 1699 + return device_create_groups_vargs(class, parent, devt, drvdata, NULL, 1700 + fmt, args); 1777 1701 } 1778 1702 EXPORT_SYMBOL_GPL(device_create_vargs); 1779 1703 ··· 1784 1766 return dev; 1785 1767 } 1786 1768 EXPORT_SYMBOL_GPL(device_create); 1769 + 1770 + /** 1771 + * device_create_with_groups - creates a device and registers it with sysfs 1772 + * @class: pointer to the struct class that this device should be registered to 1773 + * @parent: pointer to the parent struct device of this new device, if any 1774 + * @devt: the dev_t for the char device to be added 1775 + * @drvdata: the data to be added to the device for callbacks 1776 + * @groups: NULL-terminated list of attribute groups to be created 1777 + * @fmt: string for the device's name 1778 + * 1779 + * This function can be used by char device classes. A struct device 1780 + * will be created in sysfs, registered to the specified class. 1781 + * Additional attributes specified in the groups parameter will also 1782 + * be created automatically. 1783 + * 1784 + * A "dev" file will be created, showing the dev_t for the device, if 1785 + * the dev_t is not 0,0. 1786 + * If a pointer to a parent struct device is passed in, the newly created 1787 + * struct device will be a child of that device in sysfs. 1788 + * The pointer to the struct device will be returned from the call. 1789 + * Any further sysfs files that might be required can be created using this 1790 + * pointer. 1791 + * 1792 + * Returns &struct device pointer on success, or ERR_PTR() on error. 1793 + * 1794 + * Note: the struct class passed to this function must have previously 1795 + * been created with a call to class_create(). 1796 + */ 1797 + struct device *device_create_with_groups(struct class *class, 1798 + struct device *parent, dev_t devt, 1799 + void *drvdata, 1800 + const struct attribute_group **groups, 1801 + const char *fmt, ...) 1802 + { 1803 + va_list vargs; 1804 + struct device *dev; 1805 + 1806 + va_start(vargs, fmt); 1807 + dev = device_create_groups_vargs(class, parent, devt, drvdata, groups, 1808 + fmt, vargs); 1809 + va_end(vargs); 1810 + return dev; 1811 + } 1812 + EXPORT_SYMBOL_GPL(device_create_with_groups); 1787 1813 1788 1814 static int __match_devt(struct device *dev, const void *data) 1789 1815 {
+1
drivers/base/platform.c
··· 522 522 /** 523 523 * __platform_driver_register - register a driver for platform-level devices 524 524 * @drv: platform driver structure 525 + * @owner: owning module/driver 525 526 */ 526 527 int __platform_driver_register(struct platform_driver *drv, 527 528 struct module *owner)
+48 -22
fs/sysfs/group.c
··· 20 20 const struct attribute_group *grp) 21 21 { 22 22 struct attribute *const* attr; 23 - int i; 23 + struct bin_attribute *const* bin_attr; 24 24 25 - for (i = 0, attr = grp->attrs; *attr; i++, attr++) 26 - sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name); 25 + if (grp->attrs) 26 + for (attr = grp->attrs; *attr; attr++) 27 + sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name); 28 + if (grp->bin_attrs) 29 + for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++) 30 + sysfs_remove_bin_file(kobj, *bin_attr); 27 31 } 28 32 29 33 static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj, 30 34 const struct attribute_group *grp, int update) 31 35 { 32 36 struct attribute *const* attr; 37 + struct bin_attribute *const* bin_attr; 33 38 int error = 0, i; 34 39 35 - for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) { 36 - umode_t mode = 0; 40 + if (grp->attrs) { 41 + for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) { 42 + umode_t mode = 0; 37 43 38 - /* in update mode, we're changing the permissions or 39 - * visibility. Do this by first removing then 40 - * re-adding (if required) the file */ 41 - if (update) 42 - sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name); 43 - if (grp->is_visible) { 44 - mode = grp->is_visible(kobj, *attr, i); 45 - if (!mode) 46 - continue; 44 + /* 45 + * In update mode, we're changing the permissions or 46 + * visibility. Do this by first removing then 47 + * re-adding (if required) the file. 48 + */ 49 + if (update) 50 + sysfs_hash_and_remove(dir_sd, NULL, 51 + (*attr)->name); 52 + if (grp->is_visible) { 53 + mode = grp->is_visible(kobj, *attr, i); 54 + if (!mode) 55 + continue; 56 + } 57 + error = sysfs_add_file_mode(dir_sd, *attr, 58 + SYSFS_KOBJ_ATTR, 59 + (*attr)->mode | mode); 60 + if (unlikely(error)) 61 + break; 47 62 } 48 - error = sysfs_add_file_mode(dir_sd, *attr, SYSFS_KOBJ_ATTR, 49 - (*attr)->mode | mode); 50 - if (unlikely(error)) 51 - break; 63 + if (error) { 64 + remove_files(dir_sd, kobj, grp); 65 + goto exit; 66 + } 52 67 } 53 - if (error) 54 - remove_files(dir_sd, kobj, grp); 68 + 69 + if (grp->bin_attrs) { 70 + for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++) { 71 + if (update) 72 + sysfs_remove_bin_file(kobj, *bin_attr); 73 + error = sysfs_create_bin_file(kobj, *bin_attr); 74 + if (error) 75 + break; 76 + } 77 + if (error) 78 + remove_files(dir_sd, kobj, grp); 79 + } 80 + exit: 55 81 return error; 56 82 } 57 83 ··· 93 67 /* Updates may happen before the object has been instantiated */ 94 68 if (unlikely(update && !kobj->sd)) 95 69 return -EINVAL; 96 - if (!grp->attrs) { 97 - WARN(1, "sysfs: attrs not set by subsystem for group: %s/%s\n", 70 + if (!grp->attrs && !grp->bin_attrs) { 71 + WARN(1, "sysfs: (bin_)attrs not set by subsystem for group: %s/%s\n", 98 72 kobj->name, grp->name ? "" : grp->name); 99 73 return -EINVAL; 100 74 }
+29 -8
include/linux/device.h
··· 47 47 }; 48 48 49 49 #define BUS_ATTR(_name, _mode, _show, _store) \ 50 - struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store) 50 + struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store) 51 + #define BUS_ATTR_RW(_name) \ 52 + struct bus_attribute bus_attr_##_name = __ATTR_RW(_name) 53 + #define BUS_ATTR_RO(_name) \ 54 + struct bus_attribute bus_attr_##_name = __ATTR_RO(_name) 51 55 52 56 extern int __must_check bus_create_file(struct bus_type *, 53 57 struct bus_attribute *); ··· 265 261 size_t count); 266 262 }; 267 263 268 - #define DRIVER_ATTR(_name, _mode, _show, _store) \ 269 - struct driver_attribute driver_attr_##_name = \ 270 - __ATTR(_name, _mode, _show, _store) 264 + #define DRIVER_ATTR(_name, _mode, _show, _store) \ 265 + struct driver_attribute driver_attr_##_name = __ATTR(_name, _mode, _show, _store) 266 + #define DRIVER_ATTR_RW(_name) \ 267 + struct driver_attribute driver_attr_##_name = __ATTR_RW(_name) 268 + #define DRIVER_ATTR_RO(_name) \ 269 + struct driver_attribute driver_attr_##_name = __ATTR_RO(_name) 271 270 272 271 extern int __must_check driver_create_file(struct device_driver *driver, 273 272 const struct driver_attribute *attr); ··· 320 313 * @name: Name of the class. 321 314 * @owner: The module owner. 322 315 * @class_attrs: Default attributes of this class. 316 + * @dev_groups: Default attributes of the devices that belong to the class. 323 317 * @dev_attrs: Default attributes of the devices belong to the class. 324 318 * @dev_bin_attrs: Default binary attributes of the devices belong to the class. 325 319 * @dev_kobj: The kobject that represents this class and links it into the hierarchy. ··· 350 342 struct module *owner; 351 343 352 344 struct class_attribute *class_attrs; 353 - struct device_attribute *dev_attrs; 345 + struct device_attribute *dev_attrs; /* use dev_groups instead */ 346 + const struct attribute_group **dev_groups; 354 347 struct bin_attribute *dev_bin_attrs; 355 348 struct kobject *dev_kobj; 356 349 ··· 423 414 const struct class_attribute *attr); 424 415 }; 425 416 426 - #define CLASS_ATTR(_name, _mode, _show, _store) \ 427 - struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store) 417 + #define CLASS_ATTR(_name, _mode, _show, _store) \ 418 + struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store) 419 + #define CLASS_ATTR_RW(_name) \ 420 + struct class_attribute class_attr_##_name = __ATTR_RW(_name) 421 + #define CLASS_ATTR_RO(_name) \ 422 + struct class_attribute class_attr_##_name = __ATTR_RO(_name) 428 423 429 424 extern int __must_check class_create_file(struct class *class, 430 425 const struct class_attribute *attr); ··· 436 423 const struct class_attribute *attr); 437 424 438 425 /* Simple class attribute that is just a static string */ 439 - 440 426 struct class_attribute_string { 441 427 struct class_attribute attr; 442 428 char *str; ··· 524 512 525 513 #define DEVICE_ATTR(_name, _mode, _show, _store) \ 526 514 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store) 515 + #define DEVICE_ATTR_RW(_name) \ 516 + struct device_attribute dev_attr_##_name = __ATTR_RW(_name) 517 + #define DEVICE_ATTR_RO(_name) \ 518 + struct device_attribute dev_attr_##_name = __ATTR_RO(_name) 527 519 #define DEVICE_ULONG_ATTR(_name, _mode, _var) \ 528 520 struct dev_ext_attribute dev_attr_##_name = \ 529 521 { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) } ··· 939 923 extern __printf(5, 6) 940 924 struct device *device_create(struct class *cls, struct device *parent, 941 925 dev_t devt, void *drvdata, 926 + const char *fmt, ...); 927 + extern __printf(6, 7) 928 + struct device *device_create_with_groups(struct class *cls, 929 + struct device *parent, dev_t devt, void *drvdata, 930 + const struct attribute_group **groups, 942 931 const char *fmt, ...); 943 932 extern void device_destroy(struct class *cls, dev_t devt); 944 933
+55 -9
include/linux/sysfs.h
··· 17 17 #include <linux/list.h> 18 18 #include <linux/lockdep.h> 19 19 #include <linux/kobject_ns.h> 20 + #include <linux/stat.h> 20 21 #include <linux/atomic.h> 21 22 22 23 struct kobject; 23 24 struct module; 25 + struct bin_attribute; 24 26 enum kobj_ns_type; 25 27 26 28 struct attribute { ··· 61 59 umode_t (*is_visible)(struct kobject *, 62 60 struct attribute *, int); 63 61 struct attribute **attrs; 62 + struct bin_attribute **bin_attrs; 64 63 }; 65 - 66 - 67 64 68 65 /** 69 66 * Use these macros to make defining attributes easier. See include/linux/device.h 70 67 * for examples.. 71 68 */ 72 69 73 - #define __ATTR(_name,_mode,_show,_store) { \ 74 - .attr = {.name = __stringify(_name), .mode = _mode }, \ 75 - .show = _show, \ 76 - .store = _store, \ 70 + #define __ATTR(_name,_mode,_show,_store) { \ 71 + .attr = {.name = __stringify(_name), .mode = _mode }, \ 72 + .show = _show, \ 73 + .store = _store, \ 77 74 } 78 75 79 - #define __ATTR_RO(_name) { \ 80 - .attr = { .name = __stringify(_name), .mode = 0444 }, \ 81 - .show = _name##_show, \ 76 + #define __ATTR_RO(_name) { \ 77 + .attr = { .name = __stringify(_name), .mode = S_IRUGO }, \ 78 + .show = _name##_show, \ 82 79 } 80 + 81 + #define __ATTR_RW(_name) __ATTR(_name, (S_IWUSR | S_IRUGO), \ 82 + _name##_show, _name##_store) 83 83 84 84 #define __ATTR_NULL { .attr = { .name = NULL } } 85 85 ··· 95 91 #else 96 92 #define __ATTR_IGNORE_LOCKDEP __ATTR 97 93 #endif 94 + 95 + #define __ATTRIBUTE_GROUPS(_name) \ 96 + static const struct attribute_group *_name##_groups[] = { \ 97 + &_name##_group, \ 98 + NULL, \ 99 + } 100 + 101 + #define ATTRIBUTE_GROUPS(_name) \ 102 + static const struct attribute_group _name##_group = { \ 103 + .attrs = _name##_attrs, \ 104 + }; \ 105 + __ATTRIBUTE_GROUPS(_name) 98 106 99 107 #define attr_name(_attr) (_attr).attr.name 100 108 ··· 136 120 * added to sysfs if you don't have this. 137 121 */ 138 122 #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr) 123 + 124 + /* macros to create static binary attributes easier */ 125 + #define __BIN_ATTR(_name, _mode, _read, _write, _size) { \ 126 + .attr = { .name = __stringify(_name), .mode = _mode }, \ 127 + .read = _read, \ 128 + .write = _write, \ 129 + .size = _size, \ 130 + } 131 + 132 + #define __BIN_ATTR_RO(_name, _size) { \ 133 + .attr = { .name = __stringify(_name), .mode = S_IRUGO }, \ 134 + .read = _name##_read, \ 135 + .size = _size, \ 136 + } 137 + 138 + #define __BIN_ATTR_RW(_name, _size) __BIN_ATTR(_name, \ 139 + (S_IWUSR | S_IRUGO), _name##_read, \ 140 + _name##_write) 141 + 142 + #define __BIN_ATTR_NULL __ATTR_NULL 143 + 144 + #define BIN_ATTR(_name, _mode, _read, _write, _size) \ 145 + struct bin_attribute bin_attr_##_name = __BIN_ATTR(_name, _mode, _read, \ 146 + _write, _size) 147 + 148 + #define BIN_ATTR_RO(_name, _size) \ 149 + struct bin_attribute bin_attr_##_name = __BIN_ATTR_RO(_name, _size) 150 + 151 + #define BIN_ATTR_RW(_name, _size) \ 152 + struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size) 139 153 140 154 struct sysfs_ops { 141 155 ssize_t (*show)(struct kobject *, struct attribute *,char *);
-2
kernel/events/core.c
··· 6234 6234 return count; 6235 6235 } 6236 6236 6237 - #define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store) 6238 - 6239 6237 static struct device_attribute pmu_dev_attrs[] = { 6240 6238 __ATTR_RO(type), 6241 6239 __ATTR_RW(perf_event_mux_interval_ms),
-2
mm/backing-dev.c
··· 232 232 bdi_cap_stable_pages_required(bdi) ? 1 : 0); 233 233 } 234 234 235 - #define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store) 236 - 237 235 static struct device_attribute bdi_dev_attrs[] = { 238 236 __ATTR_RW(read_ahead_kb), 239 237 __ATTR_RW(min_ratio),