Skip to content

Commit

Permalink
driver core: have match() callback in struct bus_type take a const *
Browse files Browse the repository at this point in the history
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 <rafael@kernel.org>
Reviewed-by: Alex Elder <elder@kernel.org>
Acked-by: Sumit Garg <sumit.garg@linaro.org>
Link: https://lore.kernel.org/r/2024070136-wrongdoer-busily-01e8@gregkh
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
gregkh committed Jul 3, 2024
1 parent 6b521fc commit d69d804
Show file tree
Hide file tree
Showing 163 changed files with 268 additions and 338 deletions.
4 changes: 2 additions & 2 deletions arch/arm/common/locomo.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,10 +816,10 @@ EXPORT_SYMBOL(locomo_frontlight_set);
* We model this as a regular bus type, and hang devices directly
* off this.
*/
static int locomo_match(struct device *_dev, struct device_driver *_drv)
static int locomo_match(struct device *_dev, const struct device_driver *_drv)
{
struct locomo_dev *dev = LOCOMO_DEV(_dev);
struct locomo_driver *drv = LOCOMO_DRV(_drv);
const struct locomo_driver *drv = LOCOMO_DRV(_drv);

return dev->devid == drv->devid;
}
Expand Down
2 changes: 1 addition & 1 deletion arch/arm/include/asm/hardware/locomo.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ struct locomo_driver {
void (*remove)(struct locomo_dev *);
};

#define LOCOMO_DRV(_d) container_of((_d), struct locomo_driver, drv)
#define LOCOMO_DRV(_d) container_of_const((_d), struct locomo_driver, drv)

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

Expand Down
2 changes: 1 addition & 1 deletion arch/parisc/include/asm/parisc-device.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct parisc_driver {


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

static inline const char *parisc_pathname(struct parisc_device *d)
Expand Down
4 changes: 2 additions & 2 deletions arch/parisc/kernel/drivers.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static int for_each_padev(int (*fn)(struct device *, void *), void * data)
* @driver: the PA-RISC driver to try
* @dev: the PA-RISC device to try
*/
static int match_device(struct parisc_driver *driver, struct parisc_device *dev)
static int match_device(const struct parisc_driver *driver, struct parisc_device *dev)
{
const struct parisc_device_id *ids;

Expand Down Expand Up @@ -548,7 +548,7 @@ alloc_pa_dev(unsigned long hpa, struct hardware_path *mod_path)
return dev;
}

static int parisc_generic_match(struct device *dev, struct device_driver *drv)
static int parisc_generic_match(struct device *dev, const struct device_driver *drv)
{
return match_device(to_parisc_driver(drv), to_parisc_device(dev));
}
Expand Down
6 changes: 1 addition & 5 deletions arch/powerpc/include/asm/ps3.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,7 @@ int ps3_system_bus_device_register(struct ps3_system_bus_device *dev);
int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv);
void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv);

static inline struct ps3_system_bus_driver *ps3_drv_to_system_bus_drv(
struct device_driver *_drv)
{
return container_of(_drv, struct ps3_system_bus_driver, core);
}
#define ps3_drv_to_system_bus_drv(_drv) container_of_const(_drv, struct ps3_system_bus_driver, core)
static inline struct ps3_system_bus_device *ps3_dev_to_system_bus_dev(
const struct device *_dev)
{
Expand Down
6 changes: 1 addition & 5 deletions arch/powerpc/include/asm/vio.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,7 @@ static inline int vio_enable_interrupts(struct vio_dev *dev)
}
#endif

static inline struct vio_driver *to_vio_driver(struct device_driver *drv)
{
return container_of(drv, struct vio_driver, driver);
}

#define to_vio_driver(__drv) container_of_const(__drv, struct vio_driver, driver)
#define to_vio_dev(__dev) container_of_const(__dev, struct vio_dev, dev)

#endif /* __KERNEL__ */
Expand Down
4 changes: 2 additions & 2 deletions arch/powerpc/platforms/ps3/system-bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,10 @@ int ps3_mmio_region_init(struct ps3_system_bus_device *dev,
EXPORT_SYMBOL_GPL(ps3_mmio_region_init);

static int ps3_system_bus_match(struct device *_dev,
struct device_driver *_drv)
const struct device_driver *_drv)
{
int result;
struct ps3_system_bus_driver *drv = ps3_drv_to_system_bus_drv(_drv);
const struct ps3_system_bus_driver *drv = ps3_drv_to_system_bus_drv(_drv);
struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);

if (!dev->match_sub_id)
Expand Down
2 changes: 1 addition & 1 deletion arch/powerpc/platforms/pseries/ibmebus.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ static struct attribute *ibmbus_bus_attrs[] = {
};
ATTRIBUTE_GROUPS(ibmbus_bus);

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

Expand Down
6 changes: 3 additions & 3 deletions arch/powerpc/platforms/pseries/vio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1576,10 +1576,10 @@ void vio_unregister_device(struct vio_dev *viodev)
}
EXPORT_SYMBOL(vio_unregister_device);

static int vio_bus_match(struct device *dev, struct device_driver *drv)
static int vio_bus_match(struct device *dev, const struct device_driver *drv)
{
const struct vio_dev *vio_dev = to_vio_dev(dev);
struct vio_driver *vio_drv = to_vio_driver(drv);
const struct vio_driver *vio_drv = to_vio_driver(drv);
const struct vio_device_id *ids = vio_drv->id_table;

return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
Expand Down Expand Up @@ -1689,7 +1689,7 @@ struct vio_dev *vio_find_node(struct device_node *vnode)
/* construct the kobject name from the device node */
if (of_node_is_type(vnode_parent, "vdevice")) {
const __be32 *prop;

prop = of_get_property(vnode, "reg", NULL);
if (!prop)
goto out;
Expand Down
2 changes: 1 addition & 1 deletion arch/s390/include/asm/ccwdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ extern void ccw_device_get_id(struct ccw_device *, struct ccw_dev_id *);
#define get_ccwdev_lock(x) (x)->ccwlock

#define to_ccwdev(n) container_of(n, struct ccw_device, dev)
#define to_ccwdrv(n) container_of(n, struct ccw_driver, driver)
#define to_ccwdrv(n) container_of_const(n, struct ccw_driver, driver)

extern struct ccw_device *ccw_device_create_console(struct ccw_driver *);
extern void ccw_device_destroy_console(struct ccw_device *);
Expand Down
6 changes: 1 addition & 5 deletions arch/sparc/include/asm/vio.h
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,7 @@ int __vio_register_driver(struct vio_driver *drv, struct module *owner,
__vio_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
void vio_unregister_driver(struct vio_driver *drv);

static inline struct vio_driver *to_vio_driver(struct device_driver *drv)
{
return container_of(drv, struct vio_driver, driver);
}

#define to_vio_driver(__drv) container_of_const(__drv, struct vio_driver, driver)
#define to_vio_dev(__dev) container_of_const(__dev, struct vio_dev, dev)

int vio_ldc_send(struct vio_driver_state *vio, void *data, int len);
Expand Down
4 changes: 2 additions & 2 deletions arch/sparc/kernel/vio.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ static int vio_hotplug(const struct device *dev, struct kobj_uevent_env *env)
return 0;
}

static int vio_bus_match(struct device *dev, struct device_driver *drv)
static int vio_bus_match(struct device *dev, const struct device_driver *drv)
{
struct vio_dev *vio_dev = to_vio_dev(dev);
struct vio_driver *vio_drv = to_vio_driver(drv);
const struct vio_driver *vio_drv = to_vio_driver(drv);
const struct vio_device_id *matches = vio_drv->id_table;

if (!matches)
Expand Down
4 changes: 2 additions & 2 deletions drivers/acpi/bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -1045,10 +1045,10 @@ EXPORT_SYMBOL(acpi_bus_unregister_driver);
ACPI Bus operations
-------------------------------------------------------------------------- */

static int acpi_bus_match(struct device *dev, struct device_driver *drv)
static int acpi_bus_match(struct device *dev, const struct device_driver *drv)
{
struct acpi_device *acpi_dev = to_acpi_device(dev);
struct acpi_driver *acpi_drv = to_acpi_driver(drv);
const struct acpi_driver *acpi_drv = to_acpi_driver(drv);

return acpi_dev->flags.match_driver
&& !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Expand Down
6 changes: 3 additions & 3 deletions drivers/amba/bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <linux/iommu.h>
#include <linux/dma-map-ops.h>

#define to_amba_driver(d) container_of(d, struct amba_driver, drv)
#define to_amba_driver(d) container_of_const(d, struct amba_driver, drv)

/* called on periphid match and class 0x9 coresight device. */
static int
Expand Down Expand Up @@ -205,10 +205,10 @@ static int amba_read_periphid(struct amba_device *dev)
return ret;
}

static int amba_match(struct device *dev, struct device_driver *drv)
static int amba_match(struct device *dev, const struct device_driver *drv)
{
struct amba_device *pcdev = to_amba_device(dev);
struct amba_driver *pcdrv = to_amba_driver(drv);
const struct amba_driver *pcdrv = to_amba_driver(drv);

mutex_lock(&pcdev->periphid_lock);
if (!pcdev->periphid) {
Expand Down
2 changes: 1 addition & 1 deletion drivers/base/auxiliary.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ static const struct auxiliary_device_id *auxiliary_match_id(const struct auxilia
return NULL;
}

static int auxiliary_match(struct device *dev, struct device_driver *drv)
static int auxiliary_match(struct device *dev, const struct device_driver *drv)
{
struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
const struct auxiliary_driver *auxdrv = to_auxiliary_drv(drv);
Expand Down
3 changes: 1 addition & 2 deletions drivers/base/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ void device_set_deferred_probe_reason(const struct device *dev, struct va_format
static inline int driver_match_device(const struct device_driver *drv,
struct device *dev)
{
/* cast will be removed in the future when match can handle a const pointer properly. */
return drv->bus->match ? drv->bus->match(dev, (struct device_driver *)drv) : 1;
return drv->bus->match ? drv->bus->match(dev, drv) : 1;
}

static inline void dev_sync_state(struct device *dev)
Expand Down
2 changes: 1 addition & 1 deletion drivers/base/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

static DEFINE_PER_CPU(struct device *, cpu_sys_devices);

static int cpu_subsys_match(struct device *dev, struct device_driver *drv)
static int cpu_subsys_match(struct device *dev, const struct device_driver *drv)
{
/* ACPI style match is the only one that may succeed. */
if (acpi_driver_match_device(dev, drv))
Expand Down
2 changes: 1 addition & 1 deletion drivers/base/isa.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct isa_dev {

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

static int isa_bus_match(struct device *dev, struct device_driver *driver)
static int isa_bus_match(struct device *dev, const struct device_driver *driver)
{
struct isa_driver *isa_driver = to_isa_driver(driver);

Expand Down
2 changes: 1 addition & 1 deletion drivers/base/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ __ATTRIBUTE_GROUPS(platform_dev);
* and compare it against the name of the driver. Return whether they match
* or not.
*/
static int platform_match(struct device *dev, struct device_driver *drv)
static int platform_match(struct device *dev, const struct device_driver *drv)
{
struct platform_device *pdev = to_platform_device(dev);
struct platform_driver *pdrv = to_platform_driver(drv);
Expand Down
6 changes: 3 additions & 3 deletions drivers/bcma/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static unsigned int bcma_bus_next_num;
/* bcma_buses_mutex locks the bcma_bus_next_num */
static DEFINE_MUTEX(bcma_buses_mutex);

static int bcma_bus_match(struct device *dev, struct device_driver *drv);
static int bcma_bus_match(struct device *dev, const struct device_driver *drv);
static int bcma_device_probe(struct device *dev);
static void bcma_device_remove(struct device *dev);
static int bcma_device_uevent(const struct device *dev, struct kobj_uevent_env *env);
Expand Down Expand Up @@ -584,10 +584,10 @@ void bcma_driver_unregister(struct bcma_driver *drv)
}
EXPORT_SYMBOL_GPL(bcma_driver_unregister);

static int bcma_bus_match(struct device *dev, struct device_driver *drv)
static int bcma_bus_match(struct device *dev, const struct device_driver *drv)
{
struct bcma_device *core = container_of(dev, struct bcma_device, dev);
struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
const struct bcma_driver *adrv = container_of_const(drv, struct bcma_driver, drv);
const struct bcma_device_id *cid = &core->id;
const struct bcma_device_id *did;

Expand Down
4 changes: 2 additions & 2 deletions drivers/bus/fsl-mc/fsl-mc-bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ static phys_addr_t mc_portal_base_phys_addr;
*
* Returns 1 on success, 0 otherwise.
*/
static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv)
static int fsl_mc_bus_match(struct device *dev, const struct device_driver *drv)
{
const struct fsl_mc_device_id *id;
struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
const struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
bool found = false;

/* When driver_override is set, only bind to the matching driver */
Expand Down
4 changes: 2 additions & 2 deletions drivers/bus/mhi/ep/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1694,10 +1694,10 @@ static int mhi_ep_uevent(const struct device *dev, struct kobj_uevent_env *env)
mhi_dev->name);
}

static int mhi_ep_match(struct device *dev, struct device_driver *drv)
static int mhi_ep_match(struct device *dev, const struct device_driver *drv)
{
struct mhi_ep_device *mhi_dev = to_mhi_ep_device(dev);
struct mhi_ep_driver *mhi_drv = to_mhi_ep_driver(drv);
const struct mhi_ep_driver *mhi_drv = to_mhi_ep_driver(drv);
const struct mhi_device_id *id;

/*
Expand Down
4 changes: 2 additions & 2 deletions drivers/bus/mhi/host/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -1442,10 +1442,10 @@ static int mhi_uevent(const struct device *dev, struct kobj_uevent_env *env)
mhi_dev->name);
}

static int mhi_match(struct device *dev, struct device_driver *drv)
static int mhi_match(struct device *dev, const struct device_driver *drv)
{
struct mhi_device *mhi_dev = to_mhi_device(dev);
struct mhi_driver *mhi_drv = to_mhi_driver(drv);
const struct mhi_driver *mhi_drv = to_mhi_driver(drv);
const struct mhi_device_id *id;

/*
Expand Down
6 changes: 3 additions & 3 deletions drivers/bus/mips_cdmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
/* Each block of device registers is 64 bytes */
#define CDMM_DRB_SIZE 64

#define to_mips_cdmm_driver(d) container_of(d, struct mips_cdmm_driver, drv)
#define to_mips_cdmm_driver(d) container_of_const(d, struct mips_cdmm_driver, drv)

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

static int mips_cdmm_match(struct device *dev, struct device_driver *drv)
static int mips_cdmm_match(struct device *dev, const struct device_driver *drv)
{
struct mips_cdmm_device *cdev = to_mips_cdmm_device(dev);
struct mips_cdmm_driver *cdrv = to_mips_cdmm_driver(drv);
const struct mips_cdmm_driver *cdrv = to_mips_cdmm_driver(drv);

return mips_cdmm_lookup(cdrv->id_table, cdev) != NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/bus/moxtet.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ static const struct attribute_group *moxtet_dev_groups[] = {
NULL,
};

static int moxtet_match(struct device *dev, struct device_driver *drv)
static int moxtet_match(struct device *dev, const struct device_driver *drv)
{
struct moxtet_device *mdev = to_moxtet_device(dev);
struct moxtet_driver *tdrv = to_moxtet_driver(drv);
const struct moxtet_driver *tdrv = to_moxtet_driver(drv);
const enum turris_mox_module_id *t;

if (of_driver_match_device(dev, drv))
Expand Down
2 changes: 1 addition & 1 deletion drivers/bus/sunxi-rsb.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ struct sunxi_rsb {
/* bus / slave device related functions */
static const struct bus_type sunxi_rsb_bus;

static int sunxi_rsb_device_match(struct device *dev, struct device_driver *drv)
static int sunxi_rsb_device_match(struct device *dev, const struct device_driver *drv)
{
return of_driver_match_device(dev, drv);
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/cdx/cdx.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ EXPORT_SYMBOL_GPL(cdx_clear_master);
*
* Return: true on success, false otherwise.
*/
static int cdx_bus_match(struct device *dev, struct device_driver *drv)
static int cdx_bus_match(struct device *dev, const struct device_driver *drv)
{
struct cdx_device *cdx_dev = to_cdx_device(dev);
struct cdx_driver *cdx_drv = to_cdx_driver(drv);
const struct cdx_driver *cdx_drv = to_cdx_driver(drv);
const struct cdx_device_id *found_id = NULL;
const struct cdx_device_id *ids;

Expand Down
2 changes: 1 addition & 1 deletion drivers/cxl/core/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,7 @@ static int cxl_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
cxl_device_id(dev));
}

static int cxl_bus_match(struct device *dev, struct device_driver *drv)
static int cxl_bus_match(struct device *dev, const struct device_driver *drv)
{
return cxl_device_id(dev) == to_cxl_drv(drv)->id;
}
Expand Down
5 changes: 1 addition & 4 deletions drivers/cxl/cxl.h
Original file line number Diff line number Diff line change
Expand Up @@ -823,10 +823,7 @@ struct cxl_driver {
int id;
};

static inline struct cxl_driver *to_cxl_drv(struct device_driver *drv)
{
return container_of(drv, struct cxl_driver, drv);
}
#define to_cxl_drv(__drv) container_of_const(__drv, struct cxl_driver, drv)

int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner,
const char *modname);
Expand Down
Loading

0 comments on commit d69d804

Please sign in to comment.