Skip to content

Commit c6f7e72

Browse files
committed
driver core: remove fields from struct bus_type
struct bus_type is static everywhere in the kernel. This moves the kobject in the structure out of it, and a bunch of other private only to the driver core fields are now moved to a private structure. This lets us dynamically create the backing kobject properly and gives us the chance to be able to document to users exactly how to use the struct bus_type as there are no fields they can improperly access. Thanks to Kay for the build fixes on this patch. Cc: Kay Sievers <kay.sievers@vrfy.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent b249072 commit c6f7e72

7 files changed

Lines changed: 104 additions & 70 deletions

File tree

‎drivers/base/base.h‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
11

2-
/* initialisation functions */
2+
/**
3+
* struct bus_type_private - structure to hold the private to the driver core portions of the bus_type structure.
4+
*
5+
* @subsys - the struct kset that defines this bus. This is the main kobject
6+
* @drivers_kset - the list of drivers associated with this bus
7+
* @devices_kset - the list of devices associated with this bus
8+
* @klist_devices - the klist to iterate over the @devices_kset
9+
* @klist_drivers - the klist to iterate over the @drivers_kset
10+
* @bus_notifier - the bus notifier list for anything that cares about things
11+
* on this bus.
12+
* @bus - pointer back to the struct bus_type that this structure is associated
13+
* with.
14+
*
15+
* This structure is the one that is the actual kobject allowing struct
16+
* bus_type to be statically allocated safely. Nothing outside of the driver
17+
* core should ever touch these fields.
18+
*/
19+
struct bus_type_private {
20+
struct kset subsys;
21+
struct kset *drivers_kset;
22+
struct kset *devices_kset;
23+
struct klist klist_devices;
24+
struct klist klist_drivers;
25+
struct blocking_notifier_head bus_notifier;
26+
unsigned int drivers_autoprobe:1;
27+
struct bus_type *bus;
28+
};
29+
330

31+
/* initialisation functions */
432
extern int devices_init(void);
533
extern int buses_init(void);
634
extern int classes_init(void);

‎drivers/base/bus.c‎

Lines changed: 65 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "power/power.h"
1818

1919
#define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
20-
#define to_bus(obj) container_of(obj, struct bus_type, subsys.kobj)
20+
#define to_bus(obj) container_of(obj, struct bus_type_private, subsys.kobj)
2121

2222
/*
2323
* sysfs bindings for drivers
@@ -32,13 +32,17 @@ static int __must_check bus_rescan_devices_helper(struct device *dev,
3232

3333
static struct bus_type *bus_get(struct bus_type *bus)
3434
{
35-
return bus ? container_of(kset_get(&bus->subsys),
36-
struct bus_type, subsys) : NULL;
35+
if (bus) {
36+
kset_get(&bus->p->subsys);
37+
return bus;
38+
}
39+
return NULL;
3740
}
3841

3942
static void bus_put(struct bus_type *bus)
4043
{
41-
kset_put(&bus->subsys);
44+
if (bus)
45+
kset_put(&bus->p->subsys);
4246
}
4347

4448
static ssize_t
@@ -104,11 +108,11 @@ static ssize_t
104108
bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
105109
{
106110
struct bus_attribute * bus_attr = to_bus_attr(attr);
107-
struct bus_type * bus = to_bus(kobj);
111+
struct bus_type_private *bus_priv = to_bus(kobj);
108112
ssize_t ret = 0;
109113

110114
if (bus_attr->show)
111-
ret = bus_attr->show(bus, buf);
115+
ret = bus_attr->show(bus_priv->bus, buf);
112116
return ret;
113117
}
114118

@@ -117,11 +121,11 @@ bus_attr_store(struct kobject * kobj, struct attribute * attr,
117121
const char * buf, size_t count)
118122
{
119123
struct bus_attribute * bus_attr = to_bus_attr(attr);
120-
struct bus_type * bus = to_bus(kobj);
124+
struct bus_type_private *bus_priv = to_bus(kobj);
121125
ssize_t ret = 0;
122126

123127
if (bus_attr->store)
124-
ret = bus_attr->store(bus, buf, count);
128+
ret = bus_attr->store(bus_priv->bus, buf, count);
125129
return ret;
126130
}
127131

@@ -134,7 +138,7 @@ int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
134138
{
135139
int error;
136140
if (bus_get(bus)) {
137-
error = sysfs_create_file(&bus->subsys.kobj, &attr->attr);
141+
error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
138142
bus_put(bus);
139143
} else
140144
error = -EINVAL;
@@ -144,7 +148,7 @@ int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
144148
void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
145149
{
146150
if (bus_get(bus)) {
147-
sysfs_remove_file(&bus->subsys.kobj, &attr->attr);
151+
sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
148152
bus_put(bus);
149153
}
150154
}
@@ -237,16 +241,16 @@ static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
237241

238242
static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
239243
{
240-
return sprintf(buf, "%d\n", bus->drivers_autoprobe);
244+
return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
241245
}
242246

243247
static ssize_t store_drivers_autoprobe(struct bus_type *bus,
244248
const char *buf, size_t count)
245249
{
246250
if (buf[0] == '0')
247-
bus->drivers_autoprobe = 0;
251+
bus->p->drivers_autoprobe = 0;
248252
else
249-
bus->drivers_autoprobe = 1;
253+
bus->p->drivers_autoprobe = 1;
250254
return count;
251255
}
252256

@@ -300,7 +304,7 @@ int bus_for_each_dev(struct bus_type * bus, struct device * start,
300304
if (!bus)
301305
return -EINVAL;
302306

303-
klist_iter_init_node(&bus->klist_devices, &i,
307+
klist_iter_init_node(&bus->p->klist_devices, &i,
304308
(start ? &start->knode_bus : NULL));
305309
while ((dev = next_device(&i)) && !error)
306310
error = fn(dev, data);
@@ -333,7 +337,7 @@ struct device * bus_find_device(struct bus_type *bus,
333337
if (!bus)
334338
return NULL;
335339

336-
klist_iter_init_node(&bus->klist_devices, &i,
340+
klist_iter_init_node(&bus->p->klist_devices, &i,
337341
(start ? &start->knode_bus : NULL));
338342
while ((dev = next_device(&i)))
339343
if (match(dev, data) && get_device(dev))
@@ -379,7 +383,7 @@ int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
379383
if (!bus)
380384
return -EINVAL;
381385

382-
klist_iter_init_node(&bus->klist_drivers, &i,
386+
klist_iter_init_node(&bus->p->klist_drivers, &i,
383387
start ? &start->knode_bus : NULL);
384388
while ((drv = next_driver(&i)) && !error)
385389
error = fn(drv, data);
@@ -420,7 +424,7 @@ static void device_remove_attrs(struct bus_type * bus, struct device * dev)
420424
static int make_deprecated_bus_links(struct device *dev)
421425
{
422426
return sysfs_create_link(&dev->kobj,
423-
&dev->bus->subsys.kobj, "bus");
427+
&dev->bus->p->subsys.kobj, "bus");
424428
}
425429

426430
static void remove_deprecated_bus_links(struct device *dev)
@@ -449,12 +453,12 @@ int bus_add_device(struct device * dev)
449453
error = device_add_attrs(bus, dev);
450454
if (error)
451455
goto out_put;
452-
error = sysfs_create_link(&bus->devices_kset->kobj,
456+
error = sysfs_create_link(&bus->p->devices_kset->kobj,
453457
&dev->kobj, dev->bus_id);
454458
if (error)
455459
goto out_id;
456460
error = sysfs_create_link(&dev->kobj,
457-
&dev->bus->subsys.kobj, "subsystem");
461+
&dev->bus->p->subsys.kobj, "subsystem");
458462
if (error)
459463
goto out_subsys;
460464
error = make_deprecated_bus_links(dev);
@@ -466,7 +470,7 @@ int bus_add_device(struct device * dev)
466470
out_deprecated:
467471
sysfs_remove_link(&dev->kobj, "subsystem");
468472
out_subsys:
469-
sysfs_remove_link(&bus->devices_kset->kobj, dev->bus_id);
473+
sysfs_remove_link(&bus->p->devices_kset->kobj, dev->bus_id);
470474
out_id:
471475
device_remove_attrs(bus, dev);
472476
out_put:
@@ -488,11 +492,11 @@ void bus_attach_device(struct device * dev)
488492

489493
if (bus) {
490494
dev->is_registered = 1;
491-
if (bus->drivers_autoprobe)
495+
if (bus->p->drivers_autoprobe)
492496
ret = device_attach(dev);
493497
WARN_ON(ret < 0);
494498
if (ret >= 0)
495-
klist_add_tail(&dev->knode_bus, &bus->klist_devices);
499+
klist_add_tail(&dev->knode_bus, &bus->p->klist_devices);
496500
else
497501
dev->is_registered = 0;
498502
}
@@ -512,7 +516,7 @@ void bus_remove_device(struct device * dev)
512516
if (dev->bus) {
513517
sysfs_remove_link(&dev->kobj, "subsystem");
514518
remove_deprecated_bus_links(dev);
515-
sysfs_remove_link(&dev->bus->devices_kset->kobj, dev->bus_id);
519+
sysfs_remove_link(&dev->bus->p->devices_kset->kobj, dev->bus_id);
516520
device_remove_attrs(dev->bus, dev);
517521
if (dev->is_registered) {
518522
dev->is_registered = 0;
@@ -638,18 +642,18 @@ int bus_add_driver(struct device_driver *drv)
638642
error = kobject_set_name(&drv->kobj, "%s", drv->name);
639643
if (error)
640644
goto out_put_bus;
641-
drv->kobj.kset = bus->drivers_kset;
645+
drv->kobj.kset = bus->p->drivers_kset;
642646
drv->kobj.ktype = &driver_ktype;
643647
error = kobject_register(&drv->kobj);
644648
if (error)
645649
goto out_put_bus;
646650

647-
if (drv->bus->drivers_autoprobe) {
651+
if (drv->bus->p->drivers_autoprobe) {
648652
error = driver_attach(drv);
649653
if (error)
650654
goto out_unregister;
651655
}
652-
klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
656+
klist_add_tail(&drv->knode_bus, &bus->p->klist_drivers);
653657
module_add_driver(drv->owner, drv);
654658

655659
error = driver_create_file(drv, &driver_attr_uevent);
@@ -828,7 +832,7 @@ static ssize_t bus_uevent_store(struct bus_type *bus,
828832
enum kobject_action action;
829833

830834
if (kobject_action_type(buf, count, &action) == 0)
831-
kobject_uevent(&bus->subsys.kobj, action);
835+
kobject_uevent(&bus->p->subsys.kobj, action);
832836
return count;
833837
}
834838
static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
@@ -844,42 +848,50 @@ static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
844848
int bus_register(struct bus_type * bus)
845849
{
846850
int retval;
851+
struct bus_type_private *priv;
852+
853+
priv = kzalloc(sizeof(struct bus_type_private), GFP_KERNEL);
854+
if (!priv)
855+
return -ENOMEM;
856+
857+
priv->bus = bus;
858+
bus->p = priv;
847859

848-
BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier);
860+
BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
849861

850-
retval = kobject_set_name(&bus->subsys.kobj, "%s", bus->name);
862+
retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
851863
if (retval)
852864
goto out;
853865

854-
bus->subsys.kobj.kset = bus_kset;
855-
bus->subsys.kobj.ktype = &bus_ktype;
866+
priv->subsys.kobj.kset = bus_kset;
867+
priv->subsys.kobj.ktype = &bus_ktype;
868+
priv->drivers_autoprobe = 1;
856869

857-
retval = kset_register(&bus->subsys);
870+
retval = kset_register(&priv->subsys);
858871
if (retval)
859872
goto out;
860873

861874
retval = bus_create_file(bus, &bus_attr_uevent);
862875
if (retval)
863876
goto bus_uevent_fail;
864877

865-
bus->devices_kset = kset_create_and_add("devices", NULL,
866-
&bus->subsys.kobj);
867-
if (!bus->devices_kset) {
878+
priv->devices_kset = kset_create_and_add("devices", NULL,
879+
&priv->subsys.kobj);
880+
if (!priv->devices_kset) {
868881
retval = -ENOMEM;
869882
goto bus_devices_fail;
870883
}
871884

872-
bus->drivers_kset = kset_create_and_add("drivers", NULL,
873-
&bus->subsys.kobj);
874-
if (!bus->drivers_kset) {
885+
priv->drivers_kset = kset_create_and_add("drivers", NULL,
886+
&priv->subsys.kobj);
887+
if (!priv->drivers_kset) {
875888
retval = -ENOMEM;
876889
goto bus_drivers_fail;
877890
}
878891

879-
klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
880-
klist_init(&bus->klist_drivers, NULL, NULL);
892+
klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
893+
klist_init(&priv->klist_drivers, NULL, NULL);
881894

882-
bus->drivers_autoprobe = 1;
883895
retval = add_probe_files(bus);
884896
if (retval)
885897
goto bus_probe_files_fail;
@@ -894,13 +906,14 @@ int bus_register(struct bus_type * bus)
894906
bus_attrs_fail:
895907
remove_probe_files(bus);
896908
bus_probe_files_fail:
897-
kset_unregister(bus->drivers_kset);
909+
kset_unregister(bus->p->drivers_kset);
898910
bus_drivers_fail:
899-
kset_unregister(bus->devices_kset);
911+
kset_unregister(bus->p->devices_kset);
900912
bus_devices_fail:
901913
bus_remove_file(bus, &bus_attr_uevent);
902914
bus_uevent_fail:
903-
kset_unregister(&bus->subsys);
915+
kset_unregister(&bus->p->subsys);
916+
kfree(bus->p);
904917
out:
905918
return retval;
906919
}
@@ -917,33 +930,34 @@ void bus_unregister(struct bus_type * bus)
917930
pr_debug("bus %s: unregistering\n", bus->name);
918931
bus_remove_attrs(bus);
919932
remove_probe_files(bus);
920-
kset_unregister(bus->drivers_kset);
921-
kset_unregister(bus->devices_kset);
933+
kset_unregister(bus->p->drivers_kset);
934+
kset_unregister(bus->p->devices_kset);
922935
bus_remove_file(bus, &bus_attr_uevent);
923-
kset_unregister(&bus->subsys);
936+
kset_unregister(&bus->p->subsys);
937+
kfree(bus->p);
924938
}
925939

926940
int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
927941
{
928-
return blocking_notifier_chain_register(&bus->bus_notifier, nb);
942+
return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
929943
}
930944
EXPORT_SYMBOL_GPL(bus_register_notifier);
931945

932946
int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
933947
{
934-
return blocking_notifier_chain_unregister(&bus->bus_notifier, nb);
948+
return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
935949
}
936950
EXPORT_SYMBOL_GPL(bus_unregister_notifier);
937951

938952
struct kset *bus_get_kset(struct bus_type *bus)
939953
{
940-
return &bus->subsys;
954+
return &bus->p->subsys;
941955
}
942956
EXPORT_SYMBOL_GPL(bus_get_kset);
943957

944958
struct klist *bus_get_device_klist(struct bus_type *bus)
945959
{
946-
return &bus->klist_devices;
960+
return &bus->p->klist_devices;
947961
}
948962
EXPORT_SYMBOL_GPL(bus_get_device_klist);
949963

‎drivers/base/core.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ int device_add(struct device *dev)
769769

770770
/* notify clients of device entry (new way) */
771771
if (dev->bus)
772-
blocking_notifier_call_chain(&dev->bus->bus_notifier,
772+
blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
773773
BUS_NOTIFY_ADD_DEVICE, dev);
774774

775775
error = device_create_file(dev, &uevent_attr);
@@ -820,7 +820,7 @@ int device_add(struct device *dev)
820820
dpm_sysfs_remove(dev);
821821
PMError:
822822
if (dev->bus)
823-
blocking_notifier_call_chain(&dev->bus->bus_notifier,
823+
blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
824824
BUS_NOTIFY_DEL_DEVICE, dev);
825825
device_remove_attrs(dev);
826826
AttrsError:
@@ -999,7 +999,7 @@ void device_del(struct device * dev)
999999
if (platform_notify_remove)
10001000
platform_notify_remove(dev);
10011001
if (dev->bus)
1002-
blocking_notifier_call_chain(&dev->bus->bus_notifier,
1002+
blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
10031003
BUS_NOTIFY_DEL_DEVICE, dev);
10041004
kobject_uevent(&dev->kobj, KOBJ_REMOVE);
10051005
kobject_del(&dev->kobj);

0 commit comments

Comments
 (0)