Skip to content

Commit d69d804

Browse files
committed
driver core: have match() callback in struct bus_type take a const *
In the match() callback, the struct device_driver * should not be changed, so change the function callback to be a const *. This is one step of many towards making the driver core safe to have struct device_driver in read-only memory. Because the match() callback is in all busses, all busses are modified to handle this properly. This does entail switching some container_of() calls to container_of_const() to properly handle the constant *. For some busses, like PCI and USB and HV, the const * is cast away in the match callback as those busses do want to modify those structures at this point in time (they have a local lock in the driver structure.) That will have to be changed in the future if they wish to have their struct device * in read-only-memory. Cc: Rafael J. Wysocki <[email protected]> Reviewed-by: Alex Elder <[email protected]> Acked-by: Sumit Garg <[email protected]> Link: https://lore.kernel.org/r/2024070136-wrongdoer-busily-01e8@gregkh Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 6b521fc commit d69d804

File tree

163 files changed

+268
-338
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

163 files changed

+268
-338
lines changed

arch/arm/common/locomo.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -816,10 +816,10 @@ EXPORT_SYMBOL(locomo_frontlight_set);
816816
* We model this as a regular bus type, and hang devices directly
817817
* off this.
818818
*/
819-
static int locomo_match(struct device *_dev, struct device_driver *_drv)
819+
static int locomo_match(struct device *_dev, const struct device_driver *_drv)
820820
{
821821
struct locomo_dev *dev = LOCOMO_DEV(_dev);
822-
struct locomo_driver *drv = LOCOMO_DRV(_drv);
822+
const struct locomo_driver *drv = LOCOMO_DRV(_drv);
823823

824824
return dev->devid == drv->devid;
825825
}

arch/arm/include/asm/hardware/locomo.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ struct locomo_driver {
189189
void (*remove)(struct locomo_dev *);
190190
};
191191

192-
#define LOCOMO_DRV(_d) container_of((_d), struct locomo_driver, drv)
192+
#define LOCOMO_DRV(_d) container_of_const((_d), struct locomo_driver, drv)
193193

194194
#define LOCOMO_DRIVER_NAME(_ldev) ((_ldev)->dev.driver->name)
195195

arch/parisc/include/asm/parisc-device.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct parisc_driver {
4141

4242

4343
#define to_parisc_device(d) container_of(d, struct parisc_device, dev)
44-
#define to_parisc_driver(d) container_of(d, struct parisc_driver, drv)
44+
#define to_parisc_driver(d) container_of_const(d, struct parisc_driver, drv)
4545
#define parisc_parent(d) to_parisc_device(d->dev.parent)
4646

4747
static inline const char *parisc_pathname(struct parisc_device *d)

arch/parisc/kernel/drivers.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static int for_each_padev(int (*fn)(struct device *, void *), void * data)
9797
* @driver: the PA-RISC driver to try
9898
* @dev: the PA-RISC device to try
9999
*/
100-
static int match_device(struct parisc_driver *driver, struct parisc_device *dev)
100+
static int match_device(const struct parisc_driver *driver, struct parisc_device *dev)
101101
{
102102
const struct parisc_device_id *ids;
103103

@@ -548,7 +548,7 @@ alloc_pa_dev(unsigned long hpa, struct hardware_path *mod_path)
548548
return dev;
549549
}
550550

551-
static int parisc_generic_match(struct device *dev, struct device_driver *drv)
551+
static int parisc_generic_match(struct device *dev, const struct device_driver *drv)
552552
{
553553
return match_device(to_parisc_driver(drv), to_parisc_device(dev));
554554
}

arch/powerpc/include/asm/ps3.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,7 @@ int ps3_system_bus_device_register(struct ps3_system_bus_device *dev);
390390
int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv);
391391
void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv);
392392

393-
static inline struct ps3_system_bus_driver *ps3_drv_to_system_bus_drv(
394-
struct device_driver *_drv)
395-
{
396-
return container_of(_drv, struct ps3_system_bus_driver, core);
397-
}
393+
#define ps3_drv_to_system_bus_drv(_drv) container_of_const(_drv, struct ps3_system_bus_driver, core)
398394
static inline struct ps3_system_bus_device *ps3_dev_to_system_bus_dev(
399395
const struct device *_dev)
400396
{

arch/powerpc/include/asm/vio.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,7 @@ static inline int vio_enable_interrupts(struct vio_dev *dev)
156156
}
157157
#endif
158158

159-
static inline struct vio_driver *to_vio_driver(struct device_driver *drv)
160-
{
161-
return container_of(drv, struct vio_driver, driver);
162-
}
163-
159+
#define to_vio_driver(__drv) container_of_const(__drv, struct vio_driver, driver)
164160
#define to_vio_dev(__dev) container_of_const(__dev, struct vio_dev, dev)
165161

166162
#endif /* __KERNEL__ */

arch/powerpc/platforms/ps3/system-bus.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,10 @@ int ps3_mmio_region_init(struct ps3_system_bus_device *dev,
333333
EXPORT_SYMBOL_GPL(ps3_mmio_region_init);
334334

335335
static int ps3_system_bus_match(struct device *_dev,
336-
struct device_driver *_drv)
336+
const struct device_driver *_drv)
337337
{
338338
int result;
339-
struct ps3_system_bus_driver *drv = ps3_drv_to_system_bus_drv(_drv);
339+
const struct ps3_system_bus_driver *drv = ps3_drv_to_system_bus_drv(_drv);
340340
struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
341341

342342
if (!dev->match_sub_id)

arch/powerpc/platforms/pseries/ibmebus.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ static struct attribute *ibmbus_bus_attrs[] = {
339339
};
340340
ATTRIBUTE_GROUPS(ibmbus_bus);
341341

342-
static int ibmebus_bus_bus_match(struct device *dev, struct device_driver *drv)
342+
static int ibmebus_bus_bus_match(struct device *dev, const struct device_driver *drv)
343343
{
344344
const struct of_device_id *matches = drv->of_match_table;
345345

arch/powerpc/platforms/pseries/vio.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1576,10 +1576,10 @@ void vio_unregister_device(struct vio_dev *viodev)
15761576
}
15771577
EXPORT_SYMBOL(vio_unregister_device);
15781578

1579-
static int vio_bus_match(struct device *dev, struct device_driver *drv)
1579+
static int vio_bus_match(struct device *dev, const struct device_driver *drv)
15801580
{
15811581
const struct vio_dev *vio_dev = to_vio_dev(dev);
1582-
struct vio_driver *vio_drv = to_vio_driver(drv);
1582+
const struct vio_driver *vio_drv = to_vio_driver(drv);
15831583
const struct vio_device_id *ids = vio_drv->id_table;
15841584

15851585
return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
@@ -1689,7 +1689,7 @@ struct vio_dev *vio_find_node(struct device_node *vnode)
16891689
/* construct the kobject name from the device node */
16901690
if (of_node_is_type(vnode_parent, "vdevice")) {
16911691
const __be32 *prop;
1692-
1692+
16931693
prop = of_get_property(vnode, "reg", NULL);
16941694
if (!prop)
16951695
goto out;

arch/s390/include/asm/ccwdev.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ extern void ccw_device_get_id(struct ccw_device *, struct ccw_dev_id *);
210210
#define get_ccwdev_lock(x) (x)->ccwlock
211211

212212
#define to_ccwdev(n) container_of(n, struct ccw_device, dev)
213-
#define to_ccwdrv(n) container_of(n, struct ccw_driver, driver)
213+
#define to_ccwdrv(n) container_of_const(n, struct ccw_driver, driver)
214214

215215
extern struct ccw_device *ccw_device_create_console(struct ccw_driver *);
216216
extern void ccw_device_destroy_console(struct ccw_device *);

arch/sparc/include/asm/vio.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -483,11 +483,7 @@ int __vio_register_driver(struct vio_driver *drv, struct module *owner,
483483
__vio_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
484484
void vio_unregister_driver(struct vio_driver *drv);
485485

486-
static inline struct vio_driver *to_vio_driver(struct device_driver *drv)
487-
{
488-
return container_of(drv, struct vio_driver, driver);
489-
}
490-
486+
#define to_vio_driver(__drv) container_of_const(__drv, struct vio_driver, driver)
491487
#define to_vio_dev(__dev) container_of_const(__dev, struct vio_dev, dev)
492488

493489
int vio_ldc_send(struct vio_driver_state *vio, void *data, int len);

arch/sparc/kernel/vio.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ static int vio_hotplug(const struct device *dev, struct kobj_uevent_env *env)
5454
return 0;
5555
}
5656

57-
static int vio_bus_match(struct device *dev, struct device_driver *drv)
57+
static int vio_bus_match(struct device *dev, const struct device_driver *drv)
5858
{
5959
struct vio_dev *vio_dev = to_vio_dev(dev);
60-
struct vio_driver *vio_drv = to_vio_driver(drv);
60+
const struct vio_driver *vio_drv = to_vio_driver(drv);
6161
const struct vio_device_id *matches = vio_drv->id_table;
6262

6363
if (!matches)

drivers/acpi/bus.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1045,10 +1045,10 @@ EXPORT_SYMBOL(acpi_bus_unregister_driver);
10451045
ACPI Bus operations
10461046
-------------------------------------------------------------------------- */
10471047

1048-
static int acpi_bus_match(struct device *dev, struct device_driver *drv)
1048+
static int acpi_bus_match(struct device *dev, const struct device_driver *drv)
10491049
{
10501050
struct acpi_device *acpi_dev = to_acpi_device(dev);
1051-
struct acpi_driver *acpi_drv = to_acpi_driver(drv);
1051+
const struct acpi_driver *acpi_drv = to_acpi_driver(drv);
10521052

10531053
return acpi_dev->flags.match_driver
10541054
&& !acpi_match_device_ids(acpi_dev, acpi_drv->ids);

drivers/amba/bus.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <linux/iommu.h>
2727
#include <linux/dma-map-ops.h>
2828

29-
#define to_amba_driver(d) container_of(d, struct amba_driver, drv)
29+
#define to_amba_driver(d) container_of_const(d, struct amba_driver, drv)
3030

3131
/* called on periphid match and class 0x9 coresight device. */
3232
static int
@@ -205,10 +205,10 @@ static int amba_read_periphid(struct amba_device *dev)
205205
return ret;
206206
}
207207

208-
static int amba_match(struct device *dev, struct device_driver *drv)
208+
static int amba_match(struct device *dev, const struct device_driver *drv)
209209
{
210210
struct amba_device *pcdev = to_amba_device(dev);
211-
struct amba_driver *pcdrv = to_amba_driver(drv);
211+
const struct amba_driver *pcdrv = to_amba_driver(drv);
212212

213213
mutex_lock(&pcdev->periphid_lock);
214214
if (!pcdev->periphid) {

drivers/base/auxiliary.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ static const struct auxiliary_device_id *auxiliary_match_id(const struct auxilia
177177
return NULL;
178178
}
179179

180-
static int auxiliary_match(struct device *dev, struct device_driver *drv)
180+
static int auxiliary_match(struct device *dev, const struct device_driver *drv)
181181
{
182182
struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
183183
const struct auxiliary_driver *auxdrv = to_auxiliary_drv(drv);

drivers/base/base.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ void device_set_deferred_probe_reason(const struct device *dev, struct va_format
164164
static inline int driver_match_device(const struct device_driver *drv,
165165
struct device *dev)
166166
{
167-
/* cast will be removed in the future when match can handle a const pointer properly. */
168-
return drv->bus->match ? drv->bus->match(dev, (struct device_driver *)drv) : 1;
167+
return drv->bus->match ? drv->bus->match(dev, drv) : 1;
169168
}
170169

171170
static inline void dev_sync_state(struct device *dev)

drivers/base/cpu.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
static DEFINE_PER_CPU(struct device *, cpu_sys_devices);
2828

29-
static int cpu_subsys_match(struct device *dev, struct device_driver *drv)
29+
static int cpu_subsys_match(struct device *dev, const struct device_driver *drv)
3030
{
3131
/* ACPI style match is the only one that may succeed. */
3232
if (acpi_driver_match_device(dev, drv))

drivers/base/isa.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct isa_dev {
2323

2424
#define to_isa_dev(x) container_of((x), struct isa_dev, dev)
2525

26-
static int isa_bus_match(struct device *dev, struct device_driver *driver)
26+
static int isa_bus_match(struct device *dev, const struct device_driver *driver)
2727
{
2828
struct isa_driver *isa_driver = to_isa_driver(driver);
2929

drivers/base/platform.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ __ATTRIBUTE_GROUPS(platform_dev);
13321332
* and compare it against the name of the driver. Return whether they match
13331333
* or not.
13341334
*/
1335-
static int platform_match(struct device *dev, struct device_driver *drv)
1335+
static int platform_match(struct device *dev, const struct device_driver *drv)
13361336
{
13371337
struct platform_device *pdev = to_platform_device(dev);
13381338
struct platform_driver *pdrv = to_platform_driver(drv);

drivers/bcma/main.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static unsigned int bcma_bus_next_num;
2626
/* bcma_buses_mutex locks the bcma_bus_next_num */
2727
static DEFINE_MUTEX(bcma_buses_mutex);
2828

29-
static int bcma_bus_match(struct device *dev, struct device_driver *drv);
29+
static int bcma_bus_match(struct device *dev, const struct device_driver *drv);
3030
static int bcma_device_probe(struct device *dev);
3131
static void bcma_device_remove(struct device *dev);
3232
static int bcma_device_uevent(const struct device *dev, struct kobj_uevent_env *env);
@@ -584,10 +584,10 @@ void bcma_driver_unregister(struct bcma_driver *drv)
584584
}
585585
EXPORT_SYMBOL_GPL(bcma_driver_unregister);
586586

587-
static int bcma_bus_match(struct device *dev, struct device_driver *drv)
587+
static int bcma_bus_match(struct device *dev, const struct device_driver *drv)
588588
{
589589
struct bcma_device *core = container_of(dev, struct bcma_device, dev);
590-
struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
590+
const struct bcma_driver *adrv = container_of_const(drv, struct bcma_driver, drv);
591591
const struct bcma_device_id *cid = &core->id;
592592
const struct bcma_device_id *did;
593593

drivers/bus/fsl-mc/fsl-mc-bus.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ static phys_addr_t mc_portal_base_phys_addr;
8080
*
8181
* Returns 1 on success, 0 otherwise.
8282
*/
83-
static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv)
83+
static int fsl_mc_bus_match(struct device *dev, const struct device_driver *drv)
8484
{
8585
const struct fsl_mc_device_id *id;
8686
struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
87-
struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
87+
const struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
8888
bool found = false;
8989

9090
/* When driver_override is set, only bind to the matching driver */

drivers/bus/mhi/ep/main.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1694,10 +1694,10 @@ static int mhi_ep_uevent(const struct device *dev, struct kobj_uevent_env *env)
16941694
mhi_dev->name);
16951695
}
16961696

1697-
static int mhi_ep_match(struct device *dev, struct device_driver *drv)
1697+
static int mhi_ep_match(struct device *dev, const struct device_driver *drv)
16981698
{
16991699
struct mhi_ep_device *mhi_dev = to_mhi_ep_device(dev);
1700-
struct mhi_ep_driver *mhi_drv = to_mhi_ep_driver(drv);
1700+
const struct mhi_ep_driver *mhi_drv = to_mhi_ep_driver(drv);
17011701
const struct mhi_device_id *id;
17021702

17031703
/*

drivers/bus/mhi/host/init.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1442,10 +1442,10 @@ static int mhi_uevent(const struct device *dev, struct kobj_uevent_env *env)
14421442
mhi_dev->name);
14431443
}
14441444

1445-
static int mhi_match(struct device *dev, struct device_driver *drv)
1445+
static int mhi_match(struct device *dev, const struct device_driver *drv)
14461446
{
14471447
struct mhi_device *mhi_dev = to_mhi_device(dev);
1448-
struct mhi_driver *mhi_drv = to_mhi_driver(drv);
1448+
const struct mhi_driver *mhi_drv = to_mhi_driver(drv);
14491449
const struct mhi_device_id *id;
14501450

14511451
/*

drivers/bus/mips_cdmm.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
/* Each block of device registers is 64 bytes */
3838
#define CDMM_DRB_SIZE 64
3939

40-
#define to_mips_cdmm_driver(d) container_of(d, struct mips_cdmm_driver, drv)
40+
#define to_mips_cdmm_driver(d) container_of_const(d, struct mips_cdmm_driver, drv)
4141

4242
/* Default physical base address */
4343
static phys_addr_t mips_cdmm_default_base;
@@ -59,10 +59,10 @@ mips_cdmm_lookup(const struct mips_cdmm_device_id *table,
5959
return ret ? table : NULL;
6060
}
6161

62-
static int mips_cdmm_match(struct device *dev, struct device_driver *drv)
62+
static int mips_cdmm_match(struct device *dev, const struct device_driver *drv)
6363
{
6464
struct mips_cdmm_device *cdev = to_mips_cdmm_device(dev);
65-
struct mips_cdmm_driver *cdrv = to_mips_cdmm_driver(drv);
65+
const struct mips_cdmm_driver *cdrv = to_mips_cdmm_driver(drv);
6666

6767
return mips_cdmm_lookup(cdrv->id_table, cdev) != NULL;
6868
}

drivers/bus/moxtet.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ static const struct attribute_group *moxtet_dev_groups[] = {
8383
NULL,
8484
};
8585

86-
static int moxtet_match(struct device *dev, struct device_driver *drv)
86+
static int moxtet_match(struct device *dev, const struct device_driver *drv)
8787
{
8888
struct moxtet_device *mdev = to_moxtet_device(dev);
89-
struct moxtet_driver *tdrv = to_moxtet_driver(drv);
89+
const struct moxtet_driver *tdrv = to_moxtet_driver(drv);
9090
const enum turris_mox_module_id *t;
9191

9292
if (of_driver_match_device(dev, drv))

drivers/bus/sunxi-rsb.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ struct sunxi_rsb {
130130
/* bus / slave device related functions */
131131
static const struct bus_type sunxi_rsb_bus;
132132

133-
static int sunxi_rsb_device_match(struct device *dev, struct device_driver *drv)
133+
static int sunxi_rsb_device_match(struct device *dev, const struct device_driver *drv)
134134
{
135135
return of_driver_match_device(dev, drv);
136136
}

drivers/cdx/cdx.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,10 @@ EXPORT_SYMBOL_GPL(cdx_clear_master);
262262
*
263263
* Return: true on success, false otherwise.
264264
*/
265-
static int cdx_bus_match(struct device *dev, struct device_driver *drv)
265+
static int cdx_bus_match(struct device *dev, const struct device_driver *drv)
266266
{
267267
struct cdx_device *cdx_dev = to_cdx_device(dev);
268-
struct cdx_driver *cdx_drv = to_cdx_driver(drv);
268+
const struct cdx_driver *cdx_drv = to_cdx_driver(drv);
269269
const struct cdx_device_id *found_id = NULL;
270270
const struct cdx_device_id *ids;
271271

drivers/cxl/core/port.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2082,7 +2082,7 @@ static int cxl_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
20822082
cxl_device_id(dev));
20832083
}
20842084

2085-
static int cxl_bus_match(struct device *dev, struct device_driver *drv)
2085+
static int cxl_bus_match(struct device *dev, const struct device_driver *drv)
20862086
{
20872087
return cxl_device_id(dev) == to_cxl_drv(drv)->id;
20882088
}

drivers/cxl/cxl.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -823,10 +823,7 @@ struct cxl_driver {
823823
int id;
824824
};
825825

826-
static inline struct cxl_driver *to_cxl_drv(struct device_driver *drv)
827-
{
828-
return container_of(drv, struct cxl_driver, drv);
829-
}
826+
#define to_cxl_drv(__drv) container_of_const(__drv, struct cxl_driver, drv)
830827

831828
int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner,
832829
const char *modname);

0 commit comments

Comments
 (0)