mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-05-16 02:01:18 -04:00
Merge tag 'driver-core-7.0-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core
Pull driver core fixes from Danilo Krummrich: - Generalize driver_override in the driver core, providing a common sysfs implementation and concurrency-safe accessors for bus implementations - Do not use driver_override as IRQ name in the hwmon axi-fan driver - Remove an unnecessary driver_override check in sh platform_early - Migrate the platform bus to use the generic driver_override infrastructure, fixing a UAF condition caused by accessing the driver_override field without proper locking in the platform_match() callback * tag 'driver-core-7.0-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core: driver core: platform: use generic driver_override infrastructure sh: platform_early: remove pdev->driver_override check hwmon: axi-fan: don't use driver_override as IRQ name docs: driver-model: document driver_override driver core: generalize driver_override in struct device
This commit is contained in:
@@ -99,3 +99,51 @@ of the driver is decremented. All symlinks between the two are removed.
|
||||
When a driver is removed, the list of devices that it supports is
|
||||
iterated over, and the driver's remove callback is called for each
|
||||
one. The device is removed from that list and the symlinks removed.
|
||||
|
||||
|
||||
Driver Override
|
||||
~~~~~~~~~~~~~~~
|
||||
|
||||
Userspace may override the standard matching by writing a driver name to
|
||||
a device's ``driver_override`` sysfs attribute. When set, only a driver
|
||||
whose name matches the override will be considered during binding. This
|
||||
bypasses all bus-specific matching (OF, ACPI, ID tables, etc.).
|
||||
|
||||
The override may be cleared by writing an empty string, which returns
|
||||
the device to standard matching rules. Writing to ``driver_override``
|
||||
does not automatically unbind the device from its current driver or
|
||||
make any attempt to load the specified driver.
|
||||
|
||||
Buses opt into this mechanism by setting the ``driver_override`` flag in
|
||||
their ``struct bus_type``::
|
||||
|
||||
const struct bus_type example_bus_type = {
|
||||
...
|
||||
.driver_override = true,
|
||||
};
|
||||
|
||||
When the flag is set, the driver core automatically creates the
|
||||
``driver_override`` sysfs attribute for every device on that bus.
|
||||
|
||||
The bus's ``match()`` callback should check the override before performing
|
||||
its own matching, using ``device_match_driver_override()``::
|
||||
|
||||
static int example_match(struct device *dev, const struct device_driver *drv)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = device_match_driver_override(dev, drv);
|
||||
if (ret >= 0)
|
||||
return ret;
|
||||
|
||||
/* Fall through to bus-specific matching... */
|
||||
}
|
||||
|
||||
``device_match_driver_override()`` returns > 0 if the override matches
|
||||
the given driver, 0 if the override is set but does not match, or < 0 if
|
||||
no override is set at all.
|
||||
|
||||
Additional helpers are available:
|
||||
|
||||
- ``device_set_driver_override()`` - set or clear the override from kernel code.
|
||||
- ``device_has_driver_override()`` - check whether an override is set.
|
||||
|
||||
@@ -26,10 +26,6 @@ static int platform_match(struct device *dev, struct device_driver *drv)
|
||||
struct platform_device *pdev = to_platform_device(dev);
|
||||
struct platform_driver *pdrv = to_platform_driver(drv);
|
||||
|
||||
/* When driver_override is set, only bind to the matching driver */
|
||||
if (pdev->driver_override)
|
||||
return !strcmp(pdev->driver_override, drv->name);
|
||||
|
||||
/* Then try to match against the id table */
|
||||
if (pdrv->id_table)
|
||||
return platform_match_id(pdrv->id_table, pdev) != NULL;
|
||||
|
||||
@@ -504,6 +504,36 @@ int bus_for_each_drv(const struct bus_type *bus, struct device_driver *start,
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(bus_for_each_drv);
|
||||
|
||||
static ssize_t driver_override_store(struct device *dev,
|
||||
struct device_attribute *attr,
|
||||
const char *buf, size_t count)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = __device_set_driver_override(dev, buf, count);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
static ssize_t driver_override_show(struct device *dev,
|
||||
struct device_attribute *attr, char *buf)
|
||||
{
|
||||
guard(spinlock)(&dev->driver_override.lock);
|
||||
return sysfs_emit(buf, "%s\n", dev->driver_override.name);
|
||||
}
|
||||
static DEVICE_ATTR_RW(driver_override);
|
||||
|
||||
static struct attribute *driver_override_dev_attrs[] = {
|
||||
&dev_attr_driver_override.attr,
|
||||
NULL,
|
||||
};
|
||||
|
||||
static const struct attribute_group driver_override_dev_group = {
|
||||
.attrs = driver_override_dev_attrs,
|
||||
};
|
||||
|
||||
/**
|
||||
* bus_add_device - add device to bus
|
||||
* @dev: device being added
|
||||
@@ -537,9 +567,15 @@ int bus_add_device(struct device *dev)
|
||||
if (error)
|
||||
goto out_put;
|
||||
|
||||
if (dev->bus->driver_override) {
|
||||
error = device_add_group(dev, &driver_override_dev_group);
|
||||
if (error)
|
||||
goto out_groups;
|
||||
}
|
||||
|
||||
error = sysfs_create_link(&sp->devices_kset->kobj, &dev->kobj, dev_name(dev));
|
||||
if (error)
|
||||
goto out_groups;
|
||||
goto out_override;
|
||||
|
||||
error = sysfs_create_link(&dev->kobj, &sp->subsys.kobj, "subsystem");
|
||||
if (error)
|
||||
@@ -550,6 +586,9 @@ int bus_add_device(struct device *dev)
|
||||
|
||||
out_subsys:
|
||||
sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev));
|
||||
out_override:
|
||||
if (dev->bus->driver_override)
|
||||
device_remove_group(dev, &driver_override_dev_group);
|
||||
out_groups:
|
||||
device_remove_groups(dev, sp->bus->dev_groups);
|
||||
out_put:
|
||||
@@ -607,6 +646,8 @@ void bus_remove_device(struct device *dev)
|
||||
|
||||
sysfs_remove_link(&dev->kobj, "subsystem");
|
||||
sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev));
|
||||
if (dev->bus->driver_override)
|
||||
device_remove_group(dev, &driver_override_dev_group);
|
||||
device_remove_groups(dev, dev->bus->dev_groups);
|
||||
if (klist_node_attached(&dev->p->knode_bus))
|
||||
klist_del(&dev->p->knode_bus);
|
||||
|
||||
@@ -2556,6 +2556,7 @@ static void device_release(struct kobject *kobj)
|
||||
devres_release_all(dev);
|
||||
|
||||
kfree(dev->dma_range_map);
|
||||
kfree(dev->driver_override.name);
|
||||
|
||||
if (dev->release)
|
||||
dev->release(dev);
|
||||
@@ -3159,6 +3160,7 @@ void device_initialize(struct device *dev)
|
||||
kobject_init(&dev->kobj, &device_ktype);
|
||||
INIT_LIST_HEAD(&dev->dma_pools);
|
||||
mutex_init(&dev->mutex);
|
||||
spin_lock_init(&dev->driver_override.lock);
|
||||
lockdep_set_novalidate_class(&dev->mutex);
|
||||
spin_lock_init(&dev->devres_lock);
|
||||
INIT_LIST_HEAD(&dev->devres_head);
|
||||
|
||||
@@ -381,6 +381,66 @@ static void __exit deferred_probe_exit(void)
|
||||
}
|
||||
__exitcall(deferred_probe_exit);
|
||||
|
||||
int __device_set_driver_override(struct device *dev, const char *s, size_t len)
|
||||
{
|
||||
const char *new, *old;
|
||||
char *cp;
|
||||
|
||||
if (!s)
|
||||
return -EINVAL;
|
||||
|
||||
/*
|
||||
* The stored value will be used in sysfs show callback (sysfs_emit()),
|
||||
* which has a length limit of PAGE_SIZE and adds a trailing newline.
|
||||
* Thus we can store one character less to avoid truncation during sysfs
|
||||
* show.
|
||||
*/
|
||||
if (len >= (PAGE_SIZE - 1))
|
||||
return -EINVAL;
|
||||
|
||||
/*
|
||||
* Compute the real length of the string in case userspace sends us a
|
||||
* bunch of \0 characters like python likes to do.
|
||||
*/
|
||||
len = strlen(s);
|
||||
|
||||
if (!len) {
|
||||
/* Empty string passed - clear override */
|
||||
spin_lock(&dev->driver_override.lock);
|
||||
old = dev->driver_override.name;
|
||||
dev->driver_override.name = NULL;
|
||||
spin_unlock(&dev->driver_override.lock);
|
||||
kfree(old);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
cp = strnchr(s, len, '\n');
|
||||
if (cp)
|
||||
len = cp - s;
|
||||
|
||||
new = kstrndup(s, len, GFP_KERNEL);
|
||||
if (!new)
|
||||
return -ENOMEM;
|
||||
|
||||
spin_lock(&dev->driver_override.lock);
|
||||
old = dev->driver_override.name;
|
||||
if (cp != s) {
|
||||
dev->driver_override.name = new;
|
||||
spin_unlock(&dev->driver_override.lock);
|
||||
} else {
|
||||
/* "\n" passed - clear override */
|
||||
dev->driver_override.name = NULL;
|
||||
spin_unlock(&dev->driver_override.lock);
|
||||
|
||||
kfree(new);
|
||||
}
|
||||
kfree(old);
|
||||
|
||||
return 0;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(__device_set_driver_override);
|
||||
|
||||
/**
|
||||
* device_is_bound() - Check if device is bound to a driver
|
||||
* @dev: device to check
|
||||
|
||||
@@ -603,7 +603,6 @@ static void platform_device_release(struct device *dev)
|
||||
kfree(pa->pdev.dev.platform_data);
|
||||
kfree(pa->pdev.mfd_cell);
|
||||
kfree(pa->pdev.resource);
|
||||
kfree(pa->pdev.driver_override);
|
||||
kfree(pa);
|
||||
}
|
||||
|
||||
@@ -1306,38 +1305,9 @@ static ssize_t numa_node_show(struct device *dev,
|
||||
}
|
||||
static DEVICE_ATTR_RO(numa_node);
|
||||
|
||||
static ssize_t driver_override_show(struct device *dev,
|
||||
struct device_attribute *attr, char *buf)
|
||||
{
|
||||
struct platform_device *pdev = to_platform_device(dev);
|
||||
ssize_t len;
|
||||
|
||||
device_lock(dev);
|
||||
len = sysfs_emit(buf, "%s\n", pdev->driver_override);
|
||||
device_unlock(dev);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static ssize_t driver_override_store(struct device *dev,
|
||||
struct device_attribute *attr,
|
||||
const char *buf, size_t count)
|
||||
{
|
||||
struct platform_device *pdev = to_platform_device(dev);
|
||||
int ret;
|
||||
|
||||
ret = driver_set_override(dev, &pdev->driver_override, buf, count);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
return count;
|
||||
}
|
||||
static DEVICE_ATTR_RW(driver_override);
|
||||
|
||||
static struct attribute *platform_dev_attrs[] = {
|
||||
&dev_attr_modalias.attr,
|
||||
&dev_attr_numa_node.attr,
|
||||
&dev_attr_driver_override.attr,
|
||||
NULL,
|
||||
};
|
||||
|
||||
@@ -1377,10 +1347,12 @@ 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);
|
||||
int ret;
|
||||
|
||||
/* When driver_override is set, only bind to the matching driver */
|
||||
if (pdev->driver_override)
|
||||
return !strcmp(pdev->driver_override, drv->name);
|
||||
ret = device_match_driver_override(dev, drv);
|
||||
if (ret >= 0)
|
||||
return ret;
|
||||
|
||||
/* Attempt an OF style match first */
|
||||
if (of_driver_match_device(dev, drv))
|
||||
@@ -1516,6 +1488,7 @@ static const struct dev_pm_ops platform_dev_pm_ops = {
|
||||
const struct bus_type platform_bus_type = {
|
||||
.name = "platform",
|
||||
.dev_groups = platform_dev_groups,
|
||||
.driver_override = true,
|
||||
.match = platform_match,
|
||||
.uevent = platform_uevent,
|
||||
.probe = platform_probe,
|
||||
|
||||
@@ -36,7 +36,7 @@ static int simple_pm_bus_probe(struct platform_device *pdev)
|
||||
* that's not listed in simple_pm_bus_of_match. We don't want to do any
|
||||
* of the simple-pm-bus tasks for these devices, so return early.
|
||||
*/
|
||||
if (pdev->driver_override)
|
||||
if (device_has_driver_override(&pdev->dev))
|
||||
return 0;
|
||||
|
||||
match = of_match_device(dev->driver->of_match_table, dev);
|
||||
@@ -78,7 +78,7 @@ static void simple_pm_bus_remove(struct platform_device *pdev)
|
||||
{
|
||||
const void *data = of_device_get_match_data(&pdev->dev);
|
||||
|
||||
if (pdev->driver_override || data)
|
||||
if (device_has_driver_override(&pdev->dev) || data)
|
||||
return;
|
||||
|
||||
dev_dbg(&pdev->dev, "%s\n", __func__);
|
||||
|
||||
@@ -706,8 +706,7 @@ struct clk_hw *imx_clk_scu_alloc_dev(const char *name,
|
||||
if (ret)
|
||||
goto put_device;
|
||||
|
||||
ret = driver_set_override(&pdev->dev, &pdev->driver_override,
|
||||
"imx-scu-clk", strlen("imx-scu-clk"));
|
||||
ret = device_set_driver_override(&pdev->dev, "imx-scu-clk");
|
||||
if (ret)
|
||||
goto put_device;
|
||||
|
||||
|
||||
@@ -507,7 +507,7 @@ static int axi_fan_control_probe(struct platform_device *pdev)
|
||||
ret = devm_request_threaded_irq(&pdev->dev, ctl->irq, NULL,
|
||||
axi_fan_control_irq_handler,
|
||||
IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
|
||||
pdev->driver_override, ctl);
|
||||
NULL, ctl);
|
||||
if (ret)
|
||||
return dev_err_probe(&pdev->dev, ret,
|
||||
"failed to request an irq\n");
|
||||
|
||||
@@ -1535,10 +1535,8 @@ static int of_qcom_slim_ngd_register(struct device *parent,
|
||||
ngd->id = id;
|
||||
ngd->pdev->dev.parent = parent;
|
||||
|
||||
ret = driver_set_override(&ngd->pdev->dev,
|
||||
&ngd->pdev->driver_override,
|
||||
QCOM_SLIM_NGD_DRV_NAME,
|
||||
strlen(QCOM_SLIM_NGD_DRV_NAME));
|
||||
ret = device_set_driver_override(&ngd->pdev->dev,
|
||||
QCOM_SLIM_NGD_DRV_NAME);
|
||||
if (ret) {
|
||||
platform_device_put(ngd->pdev);
|
||||
kfree(ngd);
|
||||
|
||||
@@ -483,6 +483,8 @@ struct device_physical_location {
|
||||
* on. This shrinks the "Board Support Packages" (BSPs) and
|
||||
* minimizes board-specific #ifdefs in drivers.
|
||||
* @driver_data: Private pointer for driver specific info.
|
||||
* @driver_override: Driver name to force a match. Do not touch directly; use
|
||||
* device_set_driver_override() instead.
|
||||
* @links: Links to suppliers and consumers of this device.
|
||||
* @power: For device power management.
|
||||
* See Documentation/driver-api/pm/devices.rst for details.
|
||||
@@ -576,6 +578,10 @@ struct device {
|
||||
core doesn't touch it */
|
||||
void *driver_data; /* Driver data, set and get with
|
||||
dev_set_drvdata/dev_get_drvdata */
|
||||
struct {
|
||||
const char *name;
|
||||
spinlock_t lock;
|
||||
} driver_override;
|
||||
struct mutex mutex; /* mutex to synchronize calls to
|
||||
* its driver.
|
||||
*/
|
||||
@@ -701,6 +707,54 @@ struct device_link {
|
||||
|
||||
#define kobj_to_dev(__kobj) container_of_const(__kobj, struct device, kobj)
|
||||
|
||||
int __device_set_driver_override(struct device *dev, const char *s, size_t len);
|
||||
|
||||
/**
|
||||
* device_set_driver_override() - Helper to set or clear driver override.
|
||||
* @dev: Device to change
|
||||
* @s: NUL-terminated string, new driver name to force a match, pass empty
|
||||
* string to clear it ("" or "\n", where the latter is only for sysfs
|
||||
* interface).
|
||||
*
|
||||
* Helper to set or clear driver override of a device.
|
||||
*
|
||||
* Returns: 0 on success or a negative error code on failure.
|
||||
*/
|
||||
static inline int device_set_driver_override(struct device *dev, const char *s)
|
||||
{
|
||||
return __device_set_driver_override(dev, s, s ? strlen(s) : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* device_has_driver_override() - Check if a driver override has been set.
|
||||
* @dev: device to check
|
||||
*
|
||||
* Returns true if a driver override has been set for this device.
|
||||
*/
|
||||
static inline bool device_has_driver_override(struct device *dev)
|
||||
{
|
||||
guard(spinlock)(&dev->driver_override.lock);
|
||||
return !!dev->driver_override.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* device_match_driver_override() - Match a driver against the device's driver_override.
|
||||
* @dev: device to check
|
||||
* @drv: driver to match against
|
||||
*
|
||||
* Returns > 0 if a driver override is set and matches the given driver, 0 if a
|
||||
* driver override is set but does not match, or < 0 if a driver override is not
|
||||
* set at all.
|
||||
*/
|
||||
static inline int device_match_driver_override(struct device *dev,
|
||||
const struct device_driver *drv)
|
||||
{
|
||||
guard(spinlock)(&dev->driver_override.lock);
|
||||
if (dev->driver_override.name)
|
||||
return !strcmp(dev->driver_override.name, drv->name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* device_iommu_mapped - Returns true when the device DMA is translated
|
||||
* by an IOMMU
|
||||
|
||||
@@ -65,6 +65,9 @@ struct fwnode_handle;
|
||||
* this bus.
|
||||
* @pm: Power management operations of this bus, callback the specific
|
||||
* device driver's pm-ops.
|
||||
* @driver_override: Set to true if this bus supports the driver_override
|
||||
* mechanism, which allows userspace to force a specific
|
||||
* driver to bind to a device via a sysfs attribute.
|
||||
* @need_parent_lock: When probing or removing a device on this bus, the
|
||||
* device core should lock the device's parent.
|
||||
*
|
||||
@@ -106,6 +109,7 @@ struct bus_type {
|
||||
|
||||
const struct dev_pm_ops *pm;
|
||||
|
||||
bool driver_override;
|
||||
bool need_parent_lock;
|
||||
};
|
||||
|
||||
|
||||
@@ -31,11 +31,6 @@ struct platform_device {
|
||||
struct resource *resource;
|
||||
|
||||
const struct platform_device_id *id_entry;
|
||||
/*
|
||||
* Driver name to force a match. Do not set directly, because core
|
||||
* frees it. Use driver_set_override() to set or clear it.
|
||||
*/
|
||||
const char *driver_override;
|
||||
|
||||
/* MFD cell pointer */
|
||||
struct mfd_cell *mfd_cell;
|
||||
|
||||
@@ -1360,10 +1360,10 @@ static int i2s_create_secondary_device(struct samsung_i2s_priv *priv)
|
||||
if (!pdev_sec)
|
||||
return -ENOMEM;
|
||||
|
||||
pdev_sec->driver_override = kstrdup("samsung-i2s", GFP_KERNEL);
|
||||
if (!pdev_sec->driver_override) {
|
||||
ret = device_set_driver_override(&pdev_sec->dev, "samsung-i2s");
|
||||
if (ret) {
|
||||
platform_device_put(pdev_sec);
|
||||
return -ENOMEM;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = platform_device_add(pdev_sec);
|
||||
|
||||
Reference in New Issue
Block a user