driver core, iio: suppress driver_override
Subsystems (6)
cover driver core, iio: suppress driver_override
Recently Sashiko started reporting the missed NULL check of spi_get_device_match_data() or device_get_match_data() in SPI drivers in IIO subsystem. It appears that the way to crash can be made by use of driver_override sysfs attribute. However, many drivers, that may be instantiate via ACPI, OF, or, in some cases, user space won't work without necessary driver data. These are, e.g., most of the drivers in IIO subsystem. Trying to override the driver for the device that has no matching entry makes no sense in such cases and might lead to a crash, when the driver is not prepared for that. Instead of adding a NULL check for driver data pointer in each of that drivers, effectively meaning a dead code for normal functionality, introduce a special attribute in the struct device_driver to allow drivers just to hide the attribute for good.
The last two patches are the examples of use and code simplification at the same time.
I consider getting an Ack from Mark for SPI, and from Jonathan for IIO and route this via driver core, while providing an immutable branch/tag for the above mentioned stakeholders.
Note, this doesn't change the state of affairs for some busses that do not have driver_override flag set while using custom approach. On a brief look it's the s390 crypto case which may not ever need the above and hence left untouched.
Andy Shevchenko (4):
driver core: allow certain drivers prohibit override via sysfs
spi: Support suppress_override_attrs flag
iio: imu: inv_mpu6050: Suppress driver_override sysfs attribute
iio: imu: inv_icm42600: Suppress driver_override sysfs attribute
Documentation/driver-api/driver-model/binding.rst | 4 ++++ drivers/base/bus.c | 4 ++-- drivers/iio/imu/inv_icm42600/inv_icm42600_spi.c | 8 ++------ drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c | 3 +-- drivers/spi/spi.c | 11 +++++++++++ include/linux/device/driver.h | 2 ++ 6 files changed, 22 insertions(+), 10 deletions(-)
Signature · 1 line
2.50.1
PATCH 1/4 driver core: allow certain drivers prohibit override via sysfs
Many drivers, that may be instantiate via ACPI, OF, or, in some cases, user space won't work without necessary driver data. These are, e.g., most of the drivers in IIO subsystem. Trying to override the driver for the device that has no matching entry makes no sense in such cases and might lead to a crash, when the driver is not prepared for that. Instead of adding a NULL check for driver data pointer in each of that drivers, effectively meaning a dead code for normal functionality, introduce a special attribute in the struct device_driver to allow drivers just to hide the attribute for good.
--- Documentation/driver-api/driver-model/binding.rst | 4 ++++ drivers/base/bus.c | 4 ++-- include/linux/device/driver.h | 2 ++ 3 files changed, 8 insertions(+), 2 deletions(-)
Documentation/driver-api/driver-model/binding.rst +4 -0
@@ -147,3 +147,7 @@ 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. + +Any driver, which bus has ``driver_override`` flag set, can suppress that +behaviour by setting the ``suppress_override_attrs`` flag in their ``struct +device_driver``. In this case the sysfs attribute will not show.
drivers/base/bus.c +2 -2
@@ -594,7 +594,7 @@ 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) +if (dev->bus->driver_override && !dev->driver->suppress_override_attrs)device_remove_group (dev, &driver_override_dev_group); out_groups:device_remove_groups (dev, sp->bus->dev_groups);
@@ -653,7 +653,7 @@ 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) +if (dev->bus->driver_override && !dev->driver->suppress_override_attrs)device_remove_group (dev, &driver_override_dev_group);device_remove_groups (dev, dev->bus->dev_groups);if (klist_node_attached (&dev->p->knode_bus))
include/linux/device/driver.h +2 -0
@@ -55,6 +55,7 @@ enum probe_type {* @owner: The module owner. * @mod_name: Used for built-in modules. * @suppress_bind_attrs: Disables bind/unbind via sysfs. +* @suppress_override_attrs: Disables driver_override via sysfs. * @probe_type: Type of the probe (synchronous or asynchronous) to use. * @of_match_table: The open firmware table. * @acpi_match_table: The ACPI match table.
@@ -103,6 +104,7 @@ struct device_driver {const char *mod_name;/* used for built-in modules */ bool suppress_bind_attrs;/* disables bind/unbind via sysfs */ +bool suppress_override_attrs;/* disables driver_override via sysfs */ enum probe_type probe_type;const struct of_device_id *of_match_table;
Signature · 1 line
2.50.1
Danilo Krummrich
maintainerRe: [PATCH v1 1/4] driver core: allow certain drivers prohibit override via sysfs
drivers/base/bus.c int bus_add_device(struct device *dev) · line 597
Quoted 10 lines · Andy Shevchenko
drivers/base/bus.c +1
-1@@ -594,7 +594,7 @@ 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) +if (dev->bus->driver_override && !dev->driver->suppress_override_attrs)
I think that doesn't work, when bus_add_device() is called no driver is bound to the device yet, so dev->driver should be NULL.
But more in general, I think this isn't an implementation issue, but a conceptual issue.
The driver_override attribute is on the device, and indicates whether this device should be forcefully matched against some specific driver.
Now, if we want to suppress this attribute, we can't really let some driver define whether is should be suppressed for this device.
Which driver would that be? The one that's already bound?
That said, I think the problem you actually want to resolve is not "suppress the driver_override attribute", but rather "don't bind this driver through driver_override".
IOW, this is a match() problem not a sysfs attribute problem.
So, I think the change that we actually want, is this one:
include/linux/device.h +5 -3
@@ -839,9 +839,11 @@ 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 ; +if (!dev->driver_override.name) +return -1 ; +if (drv->no_driver_override) +return 0 ; +return !strcmp (dev->driver_override.name, drv->name); }/**
include/linux/device/driver.h +2 -0
@@ -55,6 +55,7 @@ enum probe_type {* @owner: The module owner. * @mod_name: Used for built-in modules. * @suppress_bind_attrs: Disables bind/unbind via sysfs. +* @no_driver_override: The driver will not be matched via driver_override. * @probe_type: Type of the probe (synchronous or asynchronous) to use. * @of_match_table: The open firmware table. * @acpi_match_table: The ACPI match table.
@@ -103,6 +104,7 @@ struct device_driver {const char *mod_name;/* used for built-in modules */ bool suppress_bind_attrs;/* disables bind/unbind via sysfs */ +bool no_driver_override;/* not matchable via driver_override */ enum probe_type probe_type;const struct of_device_id *of_match_table;
This should also get us rid of the SPI patch, since SPI already uses device_match_driver_override() in spi_match_device().
kernel test robot
build botRe: [PATCH v1 1/4] driver core: allow certain drivers prohibit override via sysfs
In reply to Andy Shevchenkokernel test robot noticed "Oops:general_protection_fault,probably_for_non-canonical_address#:#[##]SMP_KASAN_PTI" on:
Hello,
kernel test robot noticed "Oops:general_protection_fault,probably_for_non-canonical_address#:#[##]SMP_KASAN_PTI" on:
commit: f93c013feebc74afe81ce1ffd045c1625b86c4ce ("[PATCH v1 1/4] driver core: allow certain drivers prohibit override via sysfs")
url: https://github.com/intel-lab-lkp/linux/commits/Andy-Shevchenko/driver-core-allow-certain-drivers-prohibit-override-via-sysfs/20260513-124947
patch link: https://lore.kernel.org/all/[email protected]/
patch subject: [PATCH v1 1/4] driver core: allow certain drivers prohibit override via sysfs
in testcase: boot
config: x86_64-rhel-9.4-kunit
compiler: gcc-14
test machine: qemu-system-x86_64 -enable-kvm -cpu SandyBridge -smp 2 -m 32G
(please refer to attached dmesg/kmsg for entire log/backtrace)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-lkp/[email protected]
[ 98.674811][ T2697] ppdev: user-space parallel port driver [ 98.682051][ T2693] RAPL PMU: API unit is 2^-32 Joules, 0 fixed counters, 10737418240 ms ovfl timer [ 98.749342][ T2701] Error: Driver 'pcspkr' is already registered, aborting... [ 98.754059][ T2699] sr 1:0:0:0: [sr0] scsi3-mmc drive: 4x/4x cd/rw xa/form2 tray [ 98.754984][ T2699] cdrom: Uniform CD-ROM driver Revision: 3.20 [ 98.756646][ T2690] Oops: general protection fault, probably for non-canonical address 0xdffffc0000000004: 0000 [#1] SMP KASAN PTI [ 98.757883][ T2690] KASAN: null-ptr-deref in range [0x0000000000000020-0x0000000000000027] [ 98.758816][ T2690] CPU: 1 UID: 0 PID: 2690 Comm: udevd Not tainted 7.1.0-rc1+ #1 PREEMPT(lazy) [ 98.759772][ T2690] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 [ 98.760884][ T2690] RIP: 0010:bus_remove_device (kbuild/src/drivers/base/bus.c:656 (discriminator 1)) [ 98.761606][ T2690] Code: 48 89 fa 48 c1 ea 03 80 3c 02 00 0f 85 dc 01 00 00 48 ba 00 00 00 00 00 fc ff df 48 8b 43 68 48 8d 78 21 48 89 f9 48 c1 e9 03 <0f> b6 14 11 48 89 f9 83 e1 07 38 ca 7f 08 84 d2 0f 85 84 01 00 00 All code ======== 0: 48 89 fa mov %rdi,%rdx 3: 48 c1 ea 03 shr $0x3,%rdx 7: 80 3c 02 00 cmpb $0x0,(%rdx,%rax,1) b: 0f 85 dc 01 00 00 jne 0x1ed 11: 48 ba 00 00 00 00 00 movabs $0xdffffc0000000000,%rdx 18: fc ff df 1b: 48 8b 43 68 mov 0x68(%rbx),%rax 1f: 48 8d 78 21 lea 0x21(%rax),%rdi 23: 48 89 f9 mov %rdi,%rcx 26: 48 c1 e9 03 shr $0x3,%rcx 2a:* 0f b6 14 11 movzbl (%rcx,%rdx,1),%edx <-- trapping instruction 2e: 48 89 f9 mov %rdi,%rcx 31: 83 e1 07 and $0x7,%ecx 34: 38 ca cmp %cl,%dl 36: 7f 08 jg 0x40 38: 84 d2 test %dl,%dl 3a: 0f 85 84 01 00 00 jne 0x1c4
Code starting with the faulting instruction
===========================================
0: 0f b6 14 11 movzbl (%rcx,%rdx,1),%edx 4: 48 89 f9 mov %rdi,%rcx 7: 83 e1 07 and $0x7,%ecx a: 38 ca cmp %cl,%dl c: 7f 08 jg 0x16 e: 84 d2 test %dl,%dl 10: 0f 85 84 01 00 00 jne 0x19a [ 98.763619][ T2690] RSP: 0018:ffffc9000075efd0 EFLAGS: 00010202 [ 98.764294][ T2690] RAX: 0000000000000000 RBX: ffff8881d0677010 RCX: 0000000000000004 [ 98.765179][ T2690] RDX: dffffc0000000000 RSI: 0000000000000008 RDI: 0000000000000021 [ 98.766066][ T2690] RBP: ffff888100348c00 R08: 0000000000000001 R09: ffffed1020060f55 [ 98.766979][ T2690] R10: ffff888100307aaf R11: fffffffface0012b R12: ffffffffb0964be0 [ 98.767842][ T2690] R13: 1ffff920000ebdfc R14: ffff888100348c58 R15: ffff8881d0677060 [ 98.768702][ T2690] FS: 00007fb9b77bc300(0000) GS:ffff88876b59b000(0000) knlGS:0000000000000000 [ 98.769674][ T2690] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 98.770377][ T2690] CR2: 00007fb9b77898e0 CR3: 000000023c500000 CR4: 00000000000406f0 [ 98.771294][ T2690] Call Trace: [ 98.771694][ T2690] <TASK> [ 98.772063][ T2690] ? __pfx_bus_remove_device (kbuild/src/drivers/base/bus.c:623) [ 98.772686][ T2690] ? __pfx_device_remove_attrs (kbuild/src/drivers/base/core.c:3073) [ 98.773297][ T2690] device_del (kbuild/src/drivers/base/core.c:3891) [ 98.773787][ T2690] ? __pfx_device_del (kbuild/src/include/linux/kobject.h:89)
The kernel config and materials to reproduce are available at:
https://download.01.org/0day-ci/archive/20260523/[email protected]
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH 2/4 spi: Support suppress_override_attrs flag
In reply to Andy ShevchenkoSome device driver may want to suppress the driver_override sysfs attribute by specifying a certain flag in their struct device_driver. Since SPI uses explicit attribute instantiation, add that support here.
--- drivers/spi/spi.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/spi/spi.c +11 -0
@@ -214,8 +214,19 @@ static struct attribute *spi_dev_attrs[] = { NULL, }; +static umode_t spi_dev_attr_is_visible (struct kobject *kobj,struct attribute *attr,int i) +{ +struct device *dev =kobj_to_dev (kobj); + +if (attr == &dev_attr_driver_override.attr) +return dev->driver->suppress_override_attrs ?0 : attr->mode; + +return attr->mode; +} +static const struct attribute_group spi_dev_group = { .attrs = spi_dev_attrs, + .is_visible = spi_dev_attr_is_visible, };static struct attribute *spi_device_statistics_attrs[] = {
Signature · 1 line
2.50.1
Re: [PATCH v1 2/4] spi: Support suppress_override_attrs flag
Quoted 3 lines · Andy Shevchenko
Some device driver may want to suppress the
driver_overridesysfs attribute by specifying a certain flag in their structdevice_driver. Since SPI uses explicit attribute instantiation, add that support here.
Danilo Krummrich
maintainerRe: [PATCH v1 2/4] spi: Support suppress_override_attrs flag
In reply to Andy ShevchenkoQuoted 5 lines · Andy Shevchenko
drivers/spi/spi.c +0
-0
drivers/spi/spi.c static struct attribute *spi_dev_attrs[] = { · line 222
Quoted 9 lines
drivers/spi/spi.c +6
-0@@ -214,8 +214,19 @@ static struct attribute *spi_dev_attrs[] = { NULL, }; +static umode_t spi_dev_attr_is_visible (struct kobject *kobj,struct attribute *attr,int i) +{ +struct device *dev =kobj_to_dev (kobj); + +if (attr == &dev_attr_driver_override.attr) +return dev->driver->suppress_override_attrs ?0 : attr->mode;
This should have the same design problem as described in [1].
Also, wouldn't this oops the kernel right away as dev->driver should be NULL? How did you test this?
Quoted 12 lines
drivers/spi/spi.c +5
-0+ +return attr->mode; +} +static const struct attribute_group spi_dev_group = { .attrs = spi_dev_attrs, + .is_visible = spi_dev_attr_is_visible, };static struct attribute *spi_device_statistics_attrs[] = {Signature · 1 line
2.50.1
PATCH 3/4 iio: imu: inv_mpu6050: Suppress driver_override sysfs attribute
In reply to Andy ShevchenkoThe driver makes no sense for the devices without associated driver data. Instead of checking for absence of it, disable driver_override feature and drop the no more required check.
--- drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c +1 -2
@@ -46,8 +46,6 @@ static int inv_mpu_probe(struct spi_device *spi) }else if ((match =device_get_match_data (&spi->dev))) { chip_type = (uintptr_t )match; name =dev_name (&spi->dev); - }else { -return -ENODEV ; } regmap =devm_regmap_init_spi (spi, &inv_mpu_regmap_config);
@@ -175,6 +173,7 @@ static struct spi_driver inv_mpu_driver = { .acpi_match_table = inv_acpi_match, .name = "inv-mpu6000-spi", .pm = pm_ptr(&inv_mpu_pmops), + .suppress_override_attrs = true, }, };
Signature · 1 line
2.50.1
PATCH 4/4 iio: imu: inv_icm42600: Suppress driver_override sysfs attribute
In reply to Andy ShevchenkoThe driver makes no sense for the devices without associated driver data. Instead of checking for absence of it, disable driver_override feature and drop the no more required check.
--- drivers/iio/imu/inv_icm42600/inv_icm42600_spi.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/iio/imu/inv_icm42600/inv_icm42600_spi.c +2 -6
@@ -50,20 +50,15 @@ static int inv_icm42600_spi_bus_setup(struct inv_icm42600_state *st)static int inv_icm42600_probe (struct spi_device *spi) { -const void *match;enum inv_icm42600_chip chip;struct regmap *regmap; - match =device_get_match_data (&spi->dev); -if (!match) -return -EINVAL ; - chip = (uintptr_t )match; -/* use SPI specific regmap */ regmap =devm_regmap_init_spi (spi, &inv_icm42600_spi_regmap_config);if (IS_ERR (regmap))return PTR_ERR (regmap); + chip = (uintptr_t )device_get_match_data (&spi->dev);return inv_icm42600_core_probe (regmap, chip, inv_icm42600_spi_bus_setup); }
@@ -115,6 +110,7 @@ static struct spi_driver inv_icm42600_driver = { .name = "inv-icm42600-spi", .of_match_table = inv_icm42600_of_matches, .pm = pm_ptr(&inv_icm42600_pm_ops), + .suppress_override_attrs = true, }, .id_table = inv_icm42600_id, .probe = inv_icm42600_probe,
Signature · 1 line
2.50.1
Quoted 14 lines · On Fri, 8 May 2026 11:42:38 +0200 Andy Shevchenko…
Recently Sashiko started reporting the missed NULL check of
spi_get_device_match_data()ordevice_get_match_data()in SPI drivers in IIO subsystem. It appears that the way to crash can be made by use ofdriver_overridesysfs attribute. However, many drivers, that may be instantiate via ACPI, OF, or, in some cases, user space won't work without necessary driver data. These are, e.g., most of the drivers in IIO subsystem. Trying to override the driver for the device that has no matching entry makes no sense in such cases and might lead to a crash, when the driver is not prepared for that. Instead of adding a NULL check for driver data pointer in each of that drivers, effectively meaning a dead code for normal functionality, introduce a special attribute in the structdevice_driverto allow drivers just to hide the attribute for good.
Quoted 6 lines
The last two patches are the examples of use and code simplification at the same time.
I consider getting an Ack from Mark for SPI, and from Jonathan for IIO and route this via driver core, while providing an immutable branch/tag for the above mentioned stakeholders.
Works for me.
Quoted 5 lines
Note, this doesn't change the state of affairs for some busses that do not have
driver_overrideflag set while using custom approach. On a brief look it's the s390 crypto case which may not ever need the above and hence left untouched.
Excellent! To me this makes a lot of sense as removing an attribute that never works for particular drivers is neat and tidy. I suppose there is a very small risk that some really strange scripts rely on that attribute existing but not on it doing anything useful. If that turns out to be the case we can just make it fail in the callbacks but definitely not as neat.
One possible concern though based on a typical driver evolution:
1) Driver written - supports only one part, so to match_data involved and driver_override is fine.
2) Driver gains support for second device. match_data added and now driver_override is not ok so we set the new flag. Interface disappears and perhaps someone was using it. Obviously that case is broken anyway.
I guess I can make an IIO wide rule that we always set the flag. That leaves us with a gap though as we have a lot of drivers currently at step 1 of above that might progress to step 2 over time. I guess we deal with any reported regressions on a case by case basis.
Ah well - still a step in the right direction even with risks of regressions.
For all patches
Thanks for proposing this Andy
Jonathan
Quoted 15 lines
Andy Shevchenko (4):
driver core: allow certain drivers prohibit override via sysfs
spi: Supportsuppress_override_attrsflag
iio: imu:inv_mpu6050: Suppressdriver_overridesysfs attribute
iio: imu:inv_icm42600: Suppressdriver_overridesysfs attributeDocumentation/driver-api/driver-model/binding.rst | 4 ++++ drivers/base/bus.c | 4 ++-- drivers/iio/imu/inv_icm42600/inv_icm42600_spi.c | 8 ++------ drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c | 3 +-- drivers/spi/spi.c | 11 +++++++++++ include/linux/device/driver.h | 2 ++ 6 files changed, 22 insertions(+), 10 deletions(-)