Skip to content

Commit 8c2bf4a

Browse files
committed
Merge tag 'driver-core-7.1-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core
Pull driver core fixes from Danilo Krummrich: - Prevent a device from being probed before device_add() has finished initializing it; gate probe with a "ready_to_probe" device flag to avoid races with concurrent driver_register() calls - Fix a kernel-doc warning for DEV_FLAG_COUNT introduced by the above - Return -ENOTCONN from software_node_get_reference_args() when a referenced software node is known but not yet registered, allowing callers to defer probe - In sysfs_group_attrs_change_owner(), also check is_visible_const(); missed when the const variant was introduced * tag 'driver-core-7.1-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core: driver core: Add kernel-doc for DEV_FLAG_COUNT enum value sysfs: attribute_group: Respect is_visible_const() when changing owner software node: return -ENOTCONN when referenced swnode is not registered yet driver core: Don't let a device probe until it's ready
2 parents bea8d77 + 5b48431 commit 8c2bf4a

6 files changed

Lines changed: 88 additions & 3 deletions

File tree

drivers/base/core.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3688,6 +3688,21 @@ int device_add(struct device *dev)
36883688
fw_devlink_link_device(dev);
36893689
}
36903690

3691+
/*
3692+
* The moment the device was linked into the bus's "klist_devices" in
3693+
* bus_add_device() then it's possible that probe could have been
3694+
* attempted in a different thread via userspace loading a driver
3695+
* matching the device. "ready_to_probe" being unset would have
3696+
* blocked those attempts. Now that all of the above initialization has
3697+
* happened, unblock probe. If probe happens through another thread
3698+
* after this point but before bus_probe_device() runs then it's fine.
3699+
* bus_probe_device() -> device_initial_probe() -> __device_attach()
3700+
* will notice (under device_lock) that the device is already bound.
3701+
*/
3702+
device_lock(dev);
3703+
dev_set_ready_to_probe(dev);
3704+
device_unlock(dev);
3705+
36913706
bus_probe_device(dev);
36923707

36933708
/*

drivers/base/dd.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,26 @@ static int __driver_probe_device(const struct device_driver *drv, struct device
836836
if (dev->driver)
837837
return -EBUSY;
838838

839+
/*
840+
* In device_add(), the "struct device" gets linked into the subsystem's
841+
* list of devices and broadcast to userspace (via uevent) before we're
842+
* quite ready to probe. Those open pathways to driver probe before
843+
* we've finished enough of device_add() to reliably support probe.
844+
* Detect this and tell other pathways to try again later. device_add()
845+
* itself will also try to probe immediately after setting
846+
* "ready_to_probe".
847+
*/
848+
if (!dev_ready_to_probe(dev))
849+
return dev_err_probe(dev, -EPROBE_DEFER, "Device not ready to probe\n");
850+
851+
/*
852+
* Set can_match = true after calling dev_ready_to_probe(), so
853+
* driver_deferred_probe_add() won't actually add the device to the
854+
* deferred probe list when dev_ready_to_probe() returns false.
855+
*
856+
* When dev_ready_to_probe() returns false, it means that device_add()
857+
* will do another probe() attempt for us.
858+
*/
839859
dev->can_match = true;
840860
dev_dbg(dev, "bus: '%s': %s: matched device with driver %s\n",
841861
drv->bus->name, __func__, drv->name);

drivers/base/property.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,8 @@ EXPORT_SYMBOL_GPL(fwnode_property_match_property_string);
602602
* %-ENOENT when the index is out of bounds, the index has an empty
603603
* reference or the property was not found
604604
* %-EINVAL on parse error
605+
* %-ENOTCONN when the remote firmware node exists but has not been
606+
* registered yet
605607
*/
606608
int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
607609
const char *prop, const char *nargs_prop,

drivers/base/swnode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ software_node_get_reference_args(const struct fwnode_handle *fwnode,
554554
return -EINVAL;
555555

556556
if (!refnode)
557-
return -ENOENT;
557+
return -ENOTCONN;
558558

559559
if (nargs_prop) {
560560
error = fwnode_property_read_u32(refnode, nargs_prop, &nargs_prop_val);

fs/sysfs/group.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,11 @@ static int sysfs_group_attrs_change_owner(struct kobject *kobj,
517517
struct attribute *const *attr;
518518

519519
for (i = 0, attr = grp->attrs; *attr; i++, attr++) {
520-
if (grp->is_visible) {
521-
mode = grp->is_visible(kobj, *attr, i);
520+
if (grp->is_visible || grp->is_visible_const) {
521+
if (grp->is_visible)
522+
mode = grp->is_visible(kobj, *attr, i);
523+
else
524+
mode = grp->is_visible_const(kobj, *attr, i);
522525
if (mode & SYSFS_GROUP_INVISIBLE)
523526
break;
524527
if (!mode)

include/linux/device.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,22 @@ struct device_physical_location {
504504
bool lid;
505505
};
506506

507+
/**
508+
* enum struct_device_flags - Flags in struct device
509+
*
510+
* Each flag should have a set of accessor functions created via
511+
* __create_dev_flag_accessors() for each access.
512+
*
513+
* @DEV_FLAG_READY_TO_PROBE: If set then device_add() has finished enough
514+
* initialization that probe could be called.
515+
* @DEV_FLAG_COUNT: Number of defined struct_device_flags.
516+
*/
517+
enum struct_device_flags {
518+
DEV_FLAG_READY_TO_PROBE = 0,
519+
520+
DEV_FLAG_COUNT
521+
};
522+
507523
/**
508524
* struct device - The basic device structure
509525
* @parent: The device's "parent" device, the device to which it is attached.
@@ -599,6 +615,7 @@ struct device_physical_location {
599615
* @dma_skip_sync: DMA sync operations can be skipped for coherent buffers.
600616
* @dma_iommu: Device is using default IOMMU implementation for DMA and
601617
* doesn't rely on dma_ops structure.
618+
* @flags: DEV_FLAG_XXX flags. Use atomic bitfield operations to modify.
602619
*
603620
* At the lowest level, every device in a Linux system is represented by an
604621
* instance of struct device. The device structure contains the information
@@ -721,8 +738,36 @@ struct device {
721738
#ifdef CONFIG_IOMMU_DMA
722739
bool dma_iommu:1;
723740
#endif
741+
742+
DECLARE_BITMAP(flags, DEV_FLAG_COUNT);
724743
};
725744

745+
#define __create_dev_flag_accessors(accessor_name, flag_name) \
746+
static inline bool dev_##accessor_name(const struct device *dev) \
747+
{ \
748+
return test_bit(flag_name, dev->flags); \
749+
} \
750+
static inline void dev_set_##accessor_name(struct device *dev) \
751+
{ \
752+
set_bit(flag_name, dev->flags); \
753+
} \
754+
static inline void dev_clear_##accessor_name(struct device *dev) \
755+
{ \
756+
clear_bit(flag_name, dev->flags); \
757+
} \
758+
static inline void dev_assign_##accessor_name(struct device *dev, bool value) \
759+
{ \
760+
assign_bit(flag_name, dev->flags, value); \
761+
} \
762+
static inline bool dev_test_and_set_##accessor_name(struct device *dev) \
763+
{ \
764+
return test_and_set_bit(flag_name, dev->flags); \
765+
}
766+
767+
__create_dev_flag_accessors(ready_to_probe, DEV_FLAG_READY_TO_PROBE);
768+
769+
#undef __create_dev_flag_accessors
770+
726771
/**
727772
* struct device_link - Device link representation.
728773
* @supplier: The device on the supplier end of the link.

0 commit comments

Comments
 (0)