Skip to content

Commit cb3d104

Browse files
author
Danilo Krummrich
committed
driver core: generalize driver_override in struct device
Currently, there are 12 busses (including platform and PCI) that duplicate the driver_override logic for their individual devices. All of them seem to be prone to the bug described in [1]. While this could be solved for every bus individually using a separate lock, solving this in the driver-core generically results in less (and cleaner) changes overall. Thus, move driver_override to struct device, provide corresponding accessors for busses and handle locking with a separate lock internally. In particular, add device_set_driver_override(), device_has_driver_override(), device_match_driver_override() and generalize the sysfs store() and show() callbacks via a driver_override feature flag in struct bus_type. Until all busses have migrated, keep driver_set_override() in place. Note that we can't use the device lock for the reasons described in [2]. Link: https://bugzilla.kernel.org/show_bug.cgi?id=220789 [1] Link: https://lore.kernel.org/driver-core/DGRGTIRHA62X.3RY09D9SOK77P@kernel.org/ [2] Tested-by: Gui-Dong Han <hanguidong02@gmail.com> Co-developed-by: Gui-Dong Han <hanguidong02@gmail.com> Signed-off-by: Gui-Dong Han <hanguidong02@gmail.com> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Link: https://patch.msgid.link/20260303115720.48783-2-dakr@kernel.org [ Use dev->bus instead of sp->bus for consistency; fix commit message to refer to the struct bus_type's driver_override feature flag. - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent f338e77 commit cb3d104

5 files changed

Lines changed: 162 additions & 1 deletion

File tree

drivers/base/bus.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,36 @@ int bus_for_each_drv(const struct bus_type *bus, struct device_driver *start,
504504
}
505505
EXPORT_SYMBOL_GPL(bus_for_each_drv);
506506

507+
static ssize_t driver_override_store(struct device *dev,
508+
struct device_attribute *attr,
509+
const char *buf, size_t count)
510+
{
511+
int ret;
512+
513+
ret = __device_set_driver_override(dev, buf, count);
514+
if (ret)
515+
return ret;
516+
517+
return count;
518+
}
519+
520+
static ssize_t driver_override_show(struct device *dev,
521+
struct device_attribute *attr, char *buf)
522+
{
523+
guard(spinlock)(&dev->driver_override.lock);
524+
return sysfs_emit(buf, "%s\n", dev->driver_override.name);
525+
}
526+
static DEVICE_ATTR_RW(driver_override);
527+
528+
static struct attribute *driver_override_dev_attrs[] = {
529+
&dev_attr_driver_override.attr,
530+
NULL,
531+
};
532+
533+
static const struct attribute_group driver_override_dev_group = {
534+
.attrs = driver_override_dev_attrs,
535+
};
536+
507537
/**
508538
* bus_add_device - add device to bus
509539
* @dev: device being added
@@ -537,9 +567,15 @@ int bus_add_device(struct device *dev)
537567
if (error)
538568
goto out_put;
539569

570+
if (dev->bus->driver_override) {
571+
error = device_add_group(dev, &driver_override_dev_group);
572+
if (error)
573+
goto out_groups;
574+
}
575+
540576
error = sysfs_create_link(&sp->devices_kset->kobj, &dev->kobj, dev_name(dev));
541577
if (error)
542-
goto out_groups;
578+
goto out_override;
543579

544580
error = sysfs_create_link(&dev->kobj, &sp->subsys.kobj, "subsystem");
545581
if (error)
@@ -550,6 +586,9 @@ int bus_add_device(struct device *dev)
550586

551587
out_subsys:
552588
sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev));
589+
out_override:
590+
if (dev->bus->driver_override)
591+
device_remove_group(dev, &driver_override_dev_group);
553592
out_groups:
554593
device_remove_groups(dev, sp->bus->dev_groups);
555594
out_put:
@@ -607,6 +646,8 @@ void bus_remove_device(struct device *dev)
607646

608647
sysfs_remove_link(&dev->kobj, "subsystem");
609648
sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev));
649+
if (dev->bus->driver_override)
650+
device_remove_group(dev, &driver_override_dev_group);
610651
device_remove_groups(dev, dev->bus->dev_groups);
611652
if (klist_node_attached(&dev->p->knode_bus))
612653
klist_del(&dev->p->knode_bus);

drivers/base/core.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,6 +2556,7 @@ static void device_release(struct kobject *kobj)
25562556
devres_release_all(dev);
25572557

25582558
kfree(dev->dma_range_map);
2559+
kfree(dev->driver_override.name);
25592560

25602561
if (dev->release)
25612562
dev->release(dev);
@@ -3159,6 +3160,7 @@ void device_initialize(struct device *dev)
31593160
kobject_init(&dev->kobj, &device_ktype);
31603161
INIT_LIST_HEAD(&dev->dma_pools);
31613162
mutex_init(&dev->mutex);
3163+
spin_lock_init(&dev->driver_override.lock);
31623164
lockdep_set_novalidate_class(&dev->mutex);
31633165
spin_lock_init(&dev->devres_lock);
31643166
INIT_LIST_HEAD(&dev->devres_head);

drivers/base/dd.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,66 @@ static void __exit deferred_probe_exit(void)
381381
}
382382
__exitcall(deferred_probe_exit);
383383

384+
int __device_set_driver_override(struct device *dev, const char *s, size_t len)
385+
{
386+
const char *new, *old;
387+
char *cp;
388+
389+
if (!s)
390+
return -EINVAL;
391+
392+
/*
393+
* The stored value will be used in sysfs show callback (sysfs_emit()),
394+
* which has a length limit of PAGE_SIZE and adds a trailing newline.
395+
* Thus we can store one character less to avoid truncation during sysfs
396+
* show.
397+
*/
398+
if (len >= (PAGE_SIZE - 1))
399+
return -EINVAL;
400+
401+
/*
402+
* Compute the real length of the string in case userspace sends us a
403+
* bunch of \0 characters like python likes to do.
404+
*/
405+
len = strlen(s);
406+
407+
if (!len) {
408+
/* Empty string passed - clear override */
409+
spin_lock(&dev->driver_override.lock);
410+
old = dev->driver_override.name;
411+
dev->driver_override.name = NULL;
412+
spin_unlock(&dev->driver_override.lock);
413+
kfree(old);
414+
415+
return 0;
416+
}
417+
418+
cp = strnchr(s, len, '\n');
419+
if (cp)
420+
len = cp - s;
421+
422+
new = kstrndup(s, len, GFP_KERNEL);
423+
if (!new)
424+
return -ENOMEM;
425+
426+
spin_lock(&dev->driver_override.lock);
427+
old = dev->driver_override.name;
428+
if (cp != s) {
429+
dev->driver_override.name = new;
430+
spin_unlock(&dev->driver_override.lock);
431+
} else {
432+
/* "\n" passed - clear override */
433+
dev->driver_override.name = NULL;
434+
spin_unlock(&dev->driver_override.lock);
435+
436+
kfree(new);
437+
}
438+
kfree(old);
439+
440+
return 0;
441+
}
442+
EXPORT_SYMBOL_GPL(__device_set_driver_override);
443+
384444
/**
385445
* device_is_bound() - Check if device is bound to a driver
386446
* @dev: device to check

include/linux/device.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ struct device_physical_location {
483483
* on. This shrinks the "Board Support Packages" (BSPs) and
484484
* minimizes board-specific #ifdefs in drivers.
485485
* @driver_data: Private pointer for driver specific info.
486+
* @driver_override: Driver name to force a match. Do not touch directly; use
487+
* device_set_driver_override() instead.
486488
* @links: Links to suppliers and consumers of this device.
487489
* @power: For device power management.
488490
* See Documentation/driver-api/pm/devices.rst for details.
@@ -576,6 +578,10 @@ struct device {
576578
core doesn't touch it */
577579
void *driver_data; /* Driver data, set and get with
578580
dev_set_drvdata/dev_get_drvdata */
581+
struct {
582+
const char *name;
583+
spinlock_t lock;
584+
} driver_override;
579585
struct mutex mutex; /* mutex to synchronize calls to
580586
* its driver.
581587
*/
@@ -701,6 +707,54 @@ struct device_link {
701707

702708
#define kobj_to_dev(__kobj) container_of_const(__kobj, struct device, kobj)
703709

710+
int __device_set_driver_override(struct device *dev, const char *s, size_t len);
711+
712+
/**
713+
* device_set_driver_override() - Helper to set or clear driver override.
714+
* @dev: Device to change
715+
* @s: NUL-terminated string, new driver name to force a match, pass empty
716+
* string to clear it ("" or "\n", where the latter is only for sysfs
717+
* interface).
718+
*
719+
* Helper to set or clear driver override of a device.
720+
*
721+
* Returns: 0 on success or a negative error code on failure.
722+
*/
723+
static inline int device_set_driver_override(struct device *dev, const char *s)
724+
{
725+
return __device_set_driver_override(dev, s, s ? strlen(s) : 0);
726+
}
727+
728+
/**
729+
* device_has_driver_override() - Check if a driver override has been set.
730+
* @dev: device to check
731+
*
732+
* Returns true if a driver override has been set for this device.
733+
*/
734+
static inline bool device_has_driver_override(struct device *dev)
735+
{
736+
guard(spinlock)(&dev->driver_override.lock);
737+
return !!dev->driver_override.name;
738+
}
739+
740+
/**
741+
* device_match_driver_override() - Match a driver against the device's driver_override.
742+
* @dev: device to check
743+
* @drv: driver to match against
744+
*
745+
* Returns > 0 if a driver override is set and matches the given driver, 0 if a
746+
* driver override is set but does not match, or < 0 if a driver override is not
747+
* set at all.
748+
*/
749+
static inline int device_match_driver_override(struct device *dev,
750+
const struct device_driver *drv)
751+
{
752+
guard(spinlock)(&dev->driver_override.lock);
753+
if (dev->driver_override.name)
754+
return !strcmp(dev->driver_override.name, drv->name);
755+
return -1;
756+
}
757+
704758
/**
705759
* device_iommu_mapped - Returns true when the device DMA is translated
706760
* by an IOMMU

include/linux/device/bus.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ struct fwnode_handle;
6565
* this bus.
6666
* @pm: Power management operations of this bus, callback the specific
6767
* device driver's pm-ops.
68+
* @driver_override: Set to true if this bus supports the driver_override
69+
* mechanism, which allows userspace to force a specific
70+
* driver to bind to a device via a sysfs attribute.
6871
* @need_parent_lock: When probing or removing a device on this bus, the
6972
* device core should lock the device's parent.
7073
*
@@ -106,6 +109,7 @@ struct bus_type {
106109

107110
const struct dev_pm_ops *pm;
108111

112+
bool driver_override;
109113
bool need_parent_lock;
110114
};
111115

0 commit comments

Comments
 (0)