History

Merge tag 'driver-core-7.2-rc1' of gitolite.kernel.org:pub/scm/linux/kernel/git/driver-core/driver-core

Pull driver core updates from Danilo Krummrich: "Deferred probe: - Fix race where deferred probe timeout work could be permanently canceled by using mod_delayed_work() - Fix missing jiffies conversion in deferred_probe_extend_timeout() - Guard timeout extension with delayed_work_pending() to prevent premature firing - Use system_percpu_wq instead of the deprecated system_wq - Update deferred_probe_timeout documentation device: - Replace direct struct device bitfield access (can_match, dma_iommu, dma_skip_sync, dma_ops_bypass, state_synced, dma_coherent, of_node_reused, offline, offline_disabled) with flag-based accessors using bit operations - Reject devices with unregistered buses - Delete unused DEVICE_ATTR_PREALLOC() - Add low-level device attribute macros with const show/store callbacks, allowing device attributes to reside in read-only memory - Move core device attributes to read-only memory - Constify group array pointers in driver_add_groups() / driver_remove_groups(), struct bus_type, and struct device_driver device property: - Fix fwnode reference leak in fwnode_graph_get_endpoint_by_id() - Initialize all fields of fwnode_handle in fwnode_init() - Provide swnode_get()/swnode_put() wrappers around kobject_get/put() - Allow passing struct software_node_ref_args pointers directly to PROPERTY_ENTRY_REF() driver_override: - Migrate amba, cdx, vmbus, and rpmsg to the generic driver_override infrastructure, fixing a UAF from unsynchronized access to driver_override in bus match() callbacks - Remove the now-unused driver_set_override() firmware loader: - Fix recursive lock deadlock in device_cache_fw_images() when async work falls back to synchronous execution - Fix device reference leak in firmware_upload_register() platform: - Pass KBUILD_MODNAME through the platform driver registration macro to create module symlinks in sysfs for built-in drivers; move module_kset initialization to a pure_initcall and tegra cbb registration to core_initcall to ensure correct ordering - Pass THIS_MODULE implicitly through a coresight_init_driver() macro sysfs: - Upgrade OOB write detection in sysfs_kf_seq_show() from printk to WARN - Add return value clamping to sysfs_kf_read() Rust: - ACPI: Fix missing match data for PRP0001 by exporting acpi_of_match_device() - Auxiliary: Replace drvdata() with dedicated registration data on auxiliary_device. drvdata() exposed the driver's bus device private data beyond the driver's own scope, creating ordering constraints and forcing the data to outlive all registrations that access it. Registration data is instead scoped structurally to the Registration object, making lifecycle ordering enforced by construction rather than convention. - Rust-native device driver lifetimes (HRT): Allow Rust device drivers to carry a lifetime parameter on their bus device private data, tied to the device binding scope -- the interval during which a bus device is bound to a driver. Device resources like pci::Bar<'a> and IoMem<'a> can be stored directly in the driver's bus device private data with a lifetime bounded by the binding scope, so the compiler enforces at build time that they do not outlive the binding. This removes Devres indirection from every access site and eliminates try_access() failure paths in destructors. Bus driver traits use a Generic Associated Type (GAT) Data<'bound> to introduce the lifetime on the private data, rather than parameterizing the Driver trait itself. Auxiliary registration data, where the lifetime is not introduced by a trait callback but must be threaded through Registration, uses the ForLt trait (a type-level abstraction for types generic over a lifetime). Misc: - Fix DT overlayed devices not probing by reverting the broken treewide overlay fix and re-running fw_devlink consumer pickup when an overlay is applied to a bound device - Use root_device_register() for faux bus root device; add sanity check for failed bus init - Fix dev_has_sync_state() data race with READ_ONCE() and move it to base.h - Avoid spurious device_links warning when removing a device while its supplier is unbinding - Switch ISA bus to dynamic root device - Fix suspicious RCU usage in kernfs_put() - Remove devcoredump exit callback - Constify devfreq_event_class" * tag 'driver-core-7.2-rc1' of gitolite.kernel.org:pub/scm/linux/kernel/git/driver-core/driver-core: (81 commits) software node: allow passing reference args to PROPERTY_ENTRY_REF() driver core: platform: set mod_name in driver registration coresight: pass THIS_MODULE implicitly through a macro kernel: param: initialize module_kset in a pure_initcall soc/tegra: cbb: Move driver registration from pure_initcall to core_initcall firmware_loader: Fix recursive lock in device_cache_fw_images() driver core: Use system_percpu_wq instead of system_wq driver core: remove driver_set_override() rpmsg: use generic driver_override infrastructure Drivers: hv: vmbus: use generic driver_override infrastructure cdx: use generic driver_override infrastructure amba: use generic driver_override infrastructure rust: devres: add 'static bound to Devres<T> samples: rust: rust_driver_auxiliary: showcase lifetime-bound registration data rust: auxiliary: generalize Registration over ForLt rust: types: add `ForLt` trait for higher-ranked lifetime support gpu: nova-core: separate driver type from driver data samples: rust: rust_driver_pci: use HRT lifetime for Bar rust: io: make IoMem and ExclusiveIoMem lifetime-parameterized rust: pci: make Bar lifetime-parameterized ...

Linus Torvalds committed 5 months ago

Commit 36808d52 parents 5504ce0 fe22174128 files · +1814 −1134Browse files

Unified · Split

128 files
  1. Documentation/admin-guide/kernel-parameters.txt +3 −2
  2. Documentation/driver-api/driver-model/platform.rst +2 −1
  3. MAINTAINERS +1 −0
  4. arch/arc/mm/dma.c +2 −2
  5. arch/arm/mach-highbank/highbank.c +1 −1
  6. arch/arm/mach-mvebu/coherency.c +1 −1
  7. arch/arm/mm/dma-mapping-nommu.c +2 −2
  8. arch/arm/mm/dma-mapping.c +14 −14
  9. arch/arm64/kernel/cpufeature.c +1 −1
  10. arch/arm64/mm/dma-mapping.c +1 −1
  11. arch/mips/include/asm/mips_mt.h +0 −1
  12. arch/mips/mm/dma-noncoherent.c +1 −1
  13. arch/powerpc/kernel/dma-iommu.c +4 −4
  14. arch/powerpc/platforms/pseries/hotplug-memory.c +2 −2
  15. arch/riscv/mm/dma-noncoherent.c +1 −1
  16. drivers/acpi/bus.c +3 −3
  17. drivers/acpi/scan.c +1 −1
  18. drivers/amba/bus.c +6 −31
  19. drivers/base/base.h +18 −18
  20. drivers/base/bus.c +9 −2
  21. drivers/base/core.c +125 −59
  22. drivers/base/cpu.c +2 −2
  23. drivers/base/dd.c +12 −15
  24. drivers/base/devcoredump.c +0 −7
  25. drivers/base/driver.c +2 −77
  26. drivers/base/faux.c +15 −17
  27. drivers/base/firmware_loader/main.c +2 −1
  28. drivers/base/firmware_loader/sysfs_upload.c +3 −5
  29. drivers/base/isa.c +6 −6
  30. drivers/base/memory.c +1 −1
  31. drivers/base/pinctrl.c +1 −1
  32. drivers/base/platform.c +15 −8
  33. drivers/base/property.c +3 −1
  34. drivers/base/swnode.c +24 −16
  35. drivers/bus/imx-weim.c +0 −6
  36. drivers/cdx/cdx.c +5 −35
  37. drivers/cpufreq/rcpufreq_dt.rs +5 −4
  38. drivers/devfreq/devfreq-event.c +13 −10
  39. drivers/dma/ti/k3-udma-glue.c +3 −3
  40. drivers/dma/ti/k3-udma.c +3 −3
  41. drivers/gpu/drm/nova/driver.rs +5 −1
  42. drivers/gpu/drm/tyr/driver.rs +7 −6
  43. drivers/gpu/nova-core/driver.rs +23 −15
  44. drivers/gpu/nova-core/gpu.rs +1 −1
  45. drivers/gpu/nova-core/nova_core.rs +1 −1
  46. drivers/hv/vmbus_drv.c +10 −33
  47. drivers/hwtracing/coresight/coresight-catu.c +1 −1
  48. drivers/hwtracing/coresight/coresight-core.c +5 −4
  49. drivers/hwtracing/coresight/coresight-cpu-debug.c +1 −2
  50. drivers/hwtracing/coresight/coresight-funnel.c +1 −2
  51. drivers/hwtracing/coresight/coresight-replicator.c +1 −2
  52. drivers/hwtracing/coresight/coresight-stm.c +1 −1
  53. drivers/hwtracing/coresight/coresight-tmc-core.c +1 −1
  54. drivers/hwtracing/coresight/coresight-tnoc.c +1 −1
  55. drivers/hwtracing/coresight/coresight-tpdm.c +1 −2
  56. drivers/hwtracing/coresight/coresight-tpiu.c +1 −1
  57. drivers/i2c/i2c-core-of.c +0 −5
  58. drivers/iommu/dma-iommu.c +6 −3
  59. drivers/iommu/iommu.c +2 −3
  60. drivers/net/pcs/pcs-xpcs-plat.c +1 −1
  61. drivers/of/device.c +3 −3
  62. drivers/of/dynamic.c +0 −1
  63. drivers/of/overlay.c +15 −0
  64. drivers/of/platform.c +0 −5
  65. drivers/pci/of.c +1 −1
  66. drivers/pci/pwrctrl/core.c +1 −1
  67. drivers/pwm/pwm_th1520.rs +7 −6
  68. drivers/rpmsg/qcom_glink_native.c +0 −2
  69. drivers/rpmsg/rpmsg_core.c +7 −36
  70. drivers/rpmsg/virtio_rpmsg_bus.c +0 −1
  71. drivers/soc/tegra/cbb/tegra194-cbb.c +1 −1
  72. drivers/soc/tegra/cbb/tegra234-cbb.c +1 −1
  73. drivers/spi/spi.c +0 −5
  74. drivers/tty/serial/serial_base_bus.c +1 −1
  75. drivers/usb/gadget/udc/aspeed-vhub/dev.c +1 −1
  76. fs/kernfs/dir.c +7 −4
  77. fs/sysfs/file.c +6 −3
  78. include/acpi/acpi_bus.h +11 −0
  79. include/linux/amba/bus.h +0 −5
  80. include/linux/auxiliary_bus.h +4 −0
  81. include/linux/cdx/cdx_bus.h +0 −4
  82. include/linux/coresight.h +5 −2
  83. include/linux/device.h +136 −85
  84. include/linux/device/bus.h +3 −3
  85. include/linux/device/class.h +1 −1
  86. include/linux/device/driver.h +4 −6
  87. include/linux/dma-map-ops.h +3 −3
  88. include/linux/dma-mapping.h +1 −1
  89. include/linux/fwnode.h +3 −0
  90. include/linux/hyperv.h +0 −5
  91. include/linux/iommu-dma.h +2 −1
  92. include/linux/platform_device.h +9 −8
  93. include/linux/property.h +7 −1
  94. include/linux/rpmsg.h +0 −4
  95. kernel/cpu.c +2 −2
  96. kernel/dma/mapping.c +5 −7
  97. kernel/params.c +4 −4
  98. mm/hmm.c +1 −1
  99. rust/Makefile +1 −0
  100. rust/helpers/acpi.c +16 −0
  101. rust/helpers/helpers.c +1 −0
  102. rust/kernel/alloc/kbox.rs +39 −6
  103. rust/kernel/auxiliary.rs +213 −72
  104. rust/kernel/cpufreq.rs +5 −4
  105. rust/kernel/device.rs +46 −75
  106. rust/kernel/devres.rs +4 −4
  107. rust/kernel/dma.rs +1 −1
  108. rust/kernel/driver.rs +88 −27
  109. rust/kernel/i2c.rs +34 −27
  110. rust/kernel/io/mem.rs +62 −59
  111. rust/kernel/pci.rs +31 −20
  112. rust/kernel/pci/id.rs +1 −1
  113. rust/kernel/pci/io.rs +31 −23
  114. rust/kernel/platform.rs +33 −23
  115. rust/kernel/types.rs +10 −2
  116. rust/kernel/types/for_lt.rs +122 −0
  117. rust/kernel/usb.rs +36 −21
  118. rust/macros/for_lt.rs +248 −0
  119. rust/macros/lib.rs +13 −0
  120. samples/rust/rust_debugfs.rs +6 −5
  121. samples/rust/rust_dma.rs +5 −1
  122. samples/rust/rust_driver_auxiliary.rs +56 −23
  123. samples/rust/rust_driver_i2c.rs +7 −6
  124. samples/rust/rust_driver_pci.rs +43 −47
  125. samples/rust/rust_driver_platform.rs +5 −4
  126. samples/rust/rust_driver_usb.rs +8 −7
  127. samples/rust/rust_i2c_client.rs +9 −5
  128. samples/rust/rust_soc.rs +5 −4
Documentation/admin-guide/kernel-parameters.txt+3−2
@@ -1228,8 +1228,9 @@ Kernel parameters · 1227 unmodified lines
12281228 deferred probe to give up waiting on dependencies to
12291229 probe. Only specific dependencies (subsystems or
12301230 drivers) that have opted in will be ignored. A timeout
1231− of 0 will timeout at the end of initcalls. If the time
1232− out hasn't expired, it'll be restarted by each
1231+ of 0 will timeout at the end of initcalls; a negative
1232+ value is treated as an infinite timeout value. If the
1233+ timeout hasn't expired, it'll be restarted by each
12331234 successful driver registration. This option will also
12341235 dump out devices still on the deferred probe list after
12351236 retrying.
Documentation/driver-api/driver-model/platform.rst+2−1
@@ -70,7 +70,8 @@ Kernel modules can be composed of several platform drivers. The platform core · 69 unmodified lines
7070 provides helpers to register and unregister an array of drivers::
7171
7272 int __platform_register_drivers(struct platform_driver * const *drivers,
73− unsigned int count, struct module *owner);
73+ unsigned int count, struct module *owner,
74+ const char *mod_name);
7475 void platform_unregister_drivers(struct platform_driver * const *drivers,
7576 unsigned int count);
7677
MAINTAINERS+1−0
@@ -297,6 +297,7 @@ F: include/linux/acpi.h · 296 unmodified lines
297297 F: include/linux/fwnode.h
298298 F: include/linux/fw_table.h
299299 F: lib/fw_table.c
300+F: rust/helpers/acpi.c
300301 F: rust/kernel/acpi.rs
301302 F: tools/power/acpi/
302303
arch/arc/mm/dma.c+2−2
@@ -98,8 +98,8 @@ void arch_setup_dma_ops(struct device *dev, bool coherent) · 97 unmodified lines
9898 * DMA buffers.
9999 */
100100 if (is_isa_arcv2() && ioc_enable && coherent)
101− dev->dma_coherent = true;
101+ dev_set_dma_coherent(dev);
102102
103103 dev_info(dev, "use %scoherent DMA ops\n",
104− dev->dma_coherent ? "" : "non");
104+ dev_dma_coherent(dev) ? "" : "non");
105105 }
arch/arm/mach-highbank/highbank.c+1−1
@@ -98,7 +98,7 @@ static int highbank_platform_notifier(struct notifier_block *nb, · 97 unmodified lines
9898 if (of_property_read_bool(dev->of_node, "dma-coherent")) {
9999 val = readl(sregs_base + reg);
100100 writel(val | 0xff01, sregs_base + reg);
101− dev->dma_coherent = true;
101+ dev_set_dma_coherent(dev);
102102 }
103103
104104 return NOTIFY_OK;
arch/arm/mach-mvebu/coherency.c+1−1
@@ -95,7 +95,7 @@ static int mvebu_hwcc_notifier(struct notifier_block *nb, · 94 unmodified lines
9595
9696 if (event != BUS_NOTIFY_ADD_DEVICE)
9797 return NOTIFY_DONE;
98− dev->dma_coherent = true;
98+ dev_set_dma_coherent(dev);
9999
100100 return NOTIFY_OK;
101101 }
arch/arm/mm/dma-mapping-nommu.c+2−2
@@ -42,11 +42,11 @@ void arch_setup_dma_ops(struct device *dev, bool coherent) · 41 unmodified lines
4242 * enough to check if MPU is in use or not since in absence of
4343 * MPU system memory map is used.
4444 */
45− dev->dma_coherent = cacheid ? coherent : true;
45+ dev_assign_dma_coherent(dev, cacheid ? coherent : true);
4646 } else {
4747 /*
4848 * Assume coherent DMA in case MMU/MPU has not been set up.
4949 */
50− dev->dma_coherent = (get_cr() & CR_M) ? coherent : true;
50+ dev_assign_dma_coherent(dev, (get_cr() & CR_M) ? coherent : true);
5151 }
5252 }
arch/arm/mm/dma-mapping.c+14−14
@@ -1076,7 +1076,7 @@ static void *arm_iommu_alloc_attrs(struct device *dev, size_t size, · 1075 unmodified lines
10761076 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
10771077 struct page **pages;
10781078 void *addr = NULL;
1079− int coherent_flag = dev->dma_coherent ? COHERENT : NORMAL;
1079+ int coherent_flag = dev_dma_coherent(dev) ? COHERENT : NORMAL;
10801080
10811081 *handle = DMA_MAPPING_ERROR;
10821082 size = PAGE_ALIGN(size);
@@ -1124,7 +1124,7 @@ static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma, · 41 unmodified lines
11241124 if (vma->vm_pgoff >= nr_pages)
11251125 return -ENXIO;
11261126
1127− if (!dev->dma_coherent)
1127+ if (!dev_dma_coherent(dev))
11281128 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
11291129
11301130 err = vm_map_pages(vma, pages, nr_pages);
@@ -1141,7 +1141,7 @@ static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma, · 10 unmodified lines
11411141 static void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
11421142 dma_addr_t handle, unsigned long attrs)
11431143 {
1144− int coherent_flag = dev->dma_coherent ? COHERENT : NORMAL;
1144+ int coherent_flag = dev_dma_coherent(dev) ? COHERENT : NORMAL;
11451145 struct page **pages;
11461146 size = PAGE_ALIGN(size);
11471147
@@ -1202,7 +1202,7 @@ static int __map_sg_chunk(struct device *dev, struct scatterlist *sg, · 54 unmodified lines
12021202 phys_addr_t phys = page_to_phys(sg_page(s));
12031203 unsigned int len = PAGE_ALIGN(s->offset + s->length);
12041204
1205− if (!dev->dma_coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
1205+ if (!dev_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
12061206 arch_sync_dma_for_device(sg_phys(s), s->length, dir);
12071207
12081208 prot = __dma_info_to_prot(dir, attrs);
@@ -1304,7 +1304,7 @@ static void arm_iommu_unmap_sg(struct device *dev, · 95 unmodified lines
13041304 if (sg_dma_len(s))
13051305 __iommu_remove_mapping(dev, sg_dma_address(s),
13061306 sg_dma_len(s));
1307− if (!dev->dma_coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
1307+ if (!dev_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
13081308 arch_sync_dma_for_cpu(sg_phys(s), s->length, dir);
13091309 }
13101310 }
@@ -1323,7 +1323,7 @@ static void arm_iommu_sync_sg_for_cpu(struct device *dev, · 12 unmodified lines
13231323 struct scatterlist *s;
13241324 int i;
13251325
1326− if (dev->dma_coherent)
1326+ if (dev_dma_coherent(dev))
13271327 return;
13281328
13291329 for_each_sg(sg, s, nents, i)
@@ -1345,7 +1345,7 @@ static void arm_iommu_sync_sg_for_device(struct device *dev, · 15 unmodified lines
13451345 struct scatterlist *s;
13461346 int i;
13471347
1348− if (dev->dma_coherent)
1348+ if (dev_dma_coherent(dev))
13491349 return;
13501350
13511351 for_each_sg(sg, s, nents, i)
@@ -1371,7 +1371,7 @@ static dma_addr_t arm_iommu_map_phys(struct device *dev, phys_addr_t phys, · 19 unmodified lines
13711371 dma_addr_t dma_addr;
13721372 int ret, prot;
13731373
1374− if (!dev->dma_coherent &&
1374+ if (!dev_dma_coherent(dev) &&
13751375 !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO)))
13761376 arch_sync_dma_for_device(phys, size, dir);
13771377
@@ -1412,7 +1412,7 @@ static void arm_iommu_unmap_phys(struct device *dev, dma_addr_t handle, · 34 unmodified lines
14121412 if (!iova)
14131413 return;
14141414
1415− if (!dev->dma_coherent &&
1415+ if (!dev_dma_coherent(dev) &&
14161416 !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO))) {
14171417 phys_addr_t phys = iommu_iova_to_phys(mapping->domain, iova);
14181418
@@ -1431,7 +1431,7 @@ static void arm_iommu_sync_single_for_cpu(struct device *dev, · 12 unmodified lines
14311431 unsigned int offset = handle & ~PAGE_MASK;
14321432 phys_addr_t phys;
14331433
1434− if (dev->dma_coherent || !iova)
1434+ if (dev_dma_coherent(dev) || !iova)
14351435 return;
14361436
14371437 phys = iommu_iova_to_phys(mapping->domain, iova);
@@ -1446,7 +1446,7 @@ static void arm_iommu_sync_single_for_device(struct device *dev, · 8 unmodified lines
14461446 unsigned int offset = handle & ~PAGE_MASK;
14471447 phys_addr_t phys;
14481448
1449− if (dev->dma_coherent || !iova)
1449+ if (dev_dma_coherent(dev) || !iova)
14501450 return;
14511451
14521452 phys = iommu_iova_to_phys(mapping->domain, iova);
@@ -1701,13 +1701,13 @@ static void arm_teardown_iommu_dma_ops(struct device *dev) { } · 248 unmodified lines
17011701 void arch_setup_dma_ops(struct device *dev, bool coherent)
17021702 {
17031703 /*
1704− * Due to legacy code that sets the ->dma_coherent flag from a bus
1705− * notifier we can't just assign coherent to the ->dma_coherent flag
1704+ * Due to legacy code that sets the dma_coherent flag from a bus
1705+ * notifier we can't just assign coherent to the dma_coherent flag
17061706 * here, but instead have to make sure we only set but never clear it
17071707 * for now.
17081708 */
17091709 if (coherent)
1710− dev->dma_coherent = true;
1710+ dev_set_dma_coherent(dev);
17111711
17121712 /*
17131713 * Don't override the dma_ops if they have already been set. Ideally
arch/arm64/kernel/cpufeature.c+1−1
@@ -4052,7 +4052,7 @@ static int enable_mismatched_32bit_el0(unsigned int cpu) · 4051 unmodified lines
40524052 */
40534053 lucky_winner = cpu_32bit ? cpu : cpumask_any_and(cpu_32bit_el0_mask,
40544054 cpu_active_mask);
4055− get_cpu_device(lucky_winner)->offline_disabled = true;
4055+ dev_set_offline_disabled(get_cpu_device(lucky_winner));
40564056 setup_elf_hwcaps(compat_elf_hwcaps);
40574057 elf_hwcap_fixup();
40584058 pr_info("Asymmetric 32-bit EL0 support detected on CPU %u; CPU hot-unplug disabled on CPU %u\n",
arch/arm64/mm/dma-mapping.c+1−1
@@ -48,7 +48,7 @@ void arch_setup_dma_ops(struct device *dev, bool coherent) · 47 unmodified lines
4848 dev_driver_string(dev), dev_name(dev),
4949 ARCH_DMA_MINALIGN, cls);
5050
51− dev->dma_coherent = coherent;
51+ dev_assign_dma_coherent(dev, coherent);
5252
5353 xen_setup_dma_ops(dev);
5454 }
arch/mips/include/asm/mips_mt.h+0−1
@@ -23,7 +23,6 @@ extern void mips_mt_set_cpuoptions(void); · 22 unmodified lines
2323 static inline void mips_mt_set_cpuoptions(void) { }
2424 #endif
2525
26−struct class;
2726 extern const struct class mt_class;
2827
2928 #endif /* __ASM_MIPS_MT_H */
arch/mips/mm/dma-noncoherent.c+1−1
@@ -139,6 +139,6 @@ void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, · 138 unmodified lines
139139 #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS
140140 void arch_setup_dma_ops(struct device *dev, bool coherent)
141141 {
142− dev->dma_coherent = coherent;
142+ dev_assign_dma_coherent(dev, coherent);
143143 }
144144 #endif
arch/powerpc/kernel/dma-iommu.c+4−4
@@ -67,7 +67,7 @@ bool arch_dma_unmap_sg_direct(struct device *dev, struct scatterlist *sg, · 66 unmodified lines
6767 }
6868 bool arch_dma_alloc_direct(struct device *dev)
6969 {
70− if (dev->dma_ops_bypass && dev->bus_dma_limit)
70+ if (dev_dma_ops_bypass(dev) && dev->bus_dma_limit)
7171 return true;
7272
7373 return false;
@@ -75,7 +75,7 @@ bool arch_dma_alloc_direct(struct device *dev) · 1 unmodified line
7575
7676 bool arch_dma_free_direct(struct device *dev, dma_addr_t dma_handle)
7777 {
78− if (!dev->dma_ops_bypass || !dev->bus_dma_limit)
78+ if (!dev_dma_ops_bypass(dev) || !dev->bus_dma_limit)
7979 return false;
8080
8181 return is_direct_handle(dev, dma_handle);
@@ -164,7 +164,7 @@ int dma_iommu_dma_supported(struct device *dev, u64 mask) · 82 unmodified lines
164164 * fixed ops will be used for RAM. This is limited by
165165 * bus_dma_limit which is set when RAM is pre-mapped.
166166 */
167− dev->dma_ops_bypass = true;
167+ dev_set_dma_ops_bypass(dev);
168168 dev_info(dev, "iommu: 64-bit OK but direct DMA is limited by %llx\n",
169169 dev->bus_dma_limit);
170170 return 1;
@@ -185,7 +185,7 @@ int dma_iommu_dma_supported(struct device *dev, u64 mask) · 14 unmodified lines
185185 }
186186
187187 dev_dbg(dev, "iommu: not 64-bit, using default ops\n");
188− dev->dma_ops_bypass = false;
188+ dev_clear_dma_ops_bypass(dev);
189189 return 1;
190190 }
191191
arch/powerpc/platforms/pseries/hotplug-memory.c+2−2
@@ -213,9 +213,9 @@ static int dlpar_change_lmb_state(struct drmem_lmb *lmb, bool online) · 212 unmodified lines
213213 return -EINVAL;
214214 }
215215
216− if (online && mem_block->dev.offline)
216+ if (online && dev_offline(&mem_block->dev))
217217 rc = device_online(&mem_block->dev);
218− else if (!online && !mem_block->dev.offline)
218+ else if (!online && !dev_offline(&mem_block->dev))
219219 rc = device_offline(&mem_block->dev);
220220 else
221221 rc = 0;
arch/riscv/mm/dma-noncoherent.c+1−1
@@ -140,7 +140,7 @@ void arch_setup_dma_ops(struct device *dev, bool coherent) · 139 unmodified lines
140140 "%s %s: device non-coherent but no non-coherent operations supported",
141141 dev_driver_string(dev), dev_name(dev));
142142
143− dev->dma_coherent = coherent;
143+ dev_assign_dma_coherent(dev, coherent);
144144 }
145145
146146 void riscv_noncoherent_supported(void)
drivers/acpi/bus.c+3−3
@@ -898,9 +898,9 @@ const struct acpi_device *acpi_companion_match(const struct device *dev) · 897 unmodified lines
898898 * identifiers and a _DSD object with the "compatible" property, use that
899899 * property to match against the given list of identifiers.
900900 */
901−static bool acpi_of_match_device(const struct acpi_device *adev,
902− const struct of_device_id *of_match_table,
903− const struct of_device_id **of_id)
901+bool acpi_of_match_device(const struct acpi_device *adev,
902+ const struct of_device_id *of_match_table,
903+ const struct of_device_id **of_id)
904904 {
905905 const union acpi_object *of_compatible, *obj;
906906 int i, nval;
drivers/acpi/scan.c+1−1
@@ -122,7 +122,7 @@ bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent) · 121 unmodified lines
122122 mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
123123
124124 list_for_each_entry(pn, &adev->physical_node_list, node)
125− if (device_supports_offline(pn->dev) && !pn->dev->offline) {
125+ if (device_supports_offline(pn->dev) && !dev_offline(pn->dev)) {
126126 if (uevent)
127127 kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp);
128128
drivers/amba/bus.c+6−31
@@ -82,33 +82,6 @@ static void amba_put_disable_pclk(struct amba_device *pcdev) · 81 unmodified lines
8282 }
8383
8484
85−static ssize_t driver_override_show(struct device *_dev,
86− struct device_attribute *attr, char *buf)
87−{
88− struct amba_device *dev = to_amba_device(_dev);
89− ssize_t len;
90−
91− device_lock(_dev);
92− len = sprintf(buf, "%s\n", dev->driver_override);
93− device_unlock(_dev);
94− return len;
95−}
96−
97−static ssize_t driver_override_store(struct device *_dev,
98− struct device_attribute *attr,
99− const char *buf, size_t count)
100−{
101− struct amba_device *dev = to_amba_device(_dev);
102− int ret;
103−
104− ret = driver_set_override(_dev, &dev->driver_override, buf, count);
105− if (ret)
106− return ret;
107−
108− return count;
109−}
110−static DEVICE_ATTR_RW(driver_override);
111−
11285 #define amba_attr_func(name,fmt,arg...) \
11386 static ssize_t name##_show(struct device *_dev, \
11487 struct device_attribute *attr, char *buf) \
@@ -126,7 +99,6 @@ amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n", · 11 unmodified lines
12699 static struct attribute *amba_dev_attrs[] = {
127100 &dev_attr_id.attr,
128101 &dev_attr_resource.attr,
129− &dev_attr_driver_override.attr,
130102 NULL,
131103 };
132104 ATTRIBUTE_GROUPS(amba_dev);
@@ -209,10 +181,11 @@ static int amba_match(struct device *dev, const struct device_driver *drv) · 76 unmodified lines
209181 {
210182 struct amba_device *pcdev = to_amba_device(dev);
211183 const struct amba_driver *pcdrv = to_amba_driver(drv);
184+ int ret;
212185
213186 mutex_lock(&pcdev->periphid_lock);
214187 if (!pcdev->periphid) {
215− int ret = amba_read_periphid(pcdev);
188+ ret = amba_read_periphid(pcdev);
216189
217190 /*
218191 * Returning any error other than -EPROBE_DEFER from bus match
@@ -230,8 +203,9 @@ static int amba_match(struct device *dev, const struct device_driver *drv) · 11 unmodified lines
230203 mutex_unlock(&pcdev->periphid_lock);
231204
232205 /* When driver_override is set, only bind to the matching driver */
233− if (pcdev->driver_override)
234− return !strcmp(pcdev->driver_override, drv->name);
206+ ret = device_match_driver_override(dev, drv);
207+ if (ret >= 0)
208+ return ret;
235209
236210 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
237211 }
@@ -436,6 +410,7 @@ static const struct dev_pm_ops amba_pm = { · 198 unmodified lines
436410 const struct bus_type amba_bustype = {
437411 .name = "amba",
438412 .dev_groups = amba_dev_groups,
413+ .driver_override = true,
439414 .match = amba_match,
440415 .uevent = amba_uevent,
441416 .probe = amba_probe,
drivers/base/base.h+18−18
@@ -86,18 +86,6 @@ struct driver_private { · 85 unmodified lines
8686 };
8787 #define to_driver(obj) container_of(obj, struct driver_private, kobj)
8888
89−#ifdef CONFIG_RUST
90−/**
91− * struct driver_type - Representation of a Rust driver type.
92− */
93−struct driver_type {
94− /**
95− * @id: Representation of core::any::TypeId.
96− */
97− u8 id[16];
98−} __packed;
99−#endif
100−
10189 /**
10290 * struct device_private - structure to hold the private to the driver core
10391 * portions of the device structure.
@@ -115,7 +103,6 @@ struct driver_type { · 11 unmodified lines
115103 * dev_err_probe() for later retrieval via debugfs
116104 * @device: pointer back to the struct device that this structure is
117105 * associated with.
118− * @driver_type: The type of the bound Rust driver.
119106 * @dead: This device is currently either in the process of or has been
120107 * removed from the system. Any asynchronous events scheduled for this
121108 * device should exit without taking any action.
@@ -132,9 +119,6 @@ struct device_private { · 10 unmodified lines
132119 const struct device_driver *async_driver;
133120 char *deferred_probe_reason;
134121 struct device *device;
135−#ifdef CONFIG_RUST
136− struct driver_type driver_type;
137−#endif
138122 u8 dead:1;
139123 };
140124 #define to_device_private_parent(obj) \
@@ -188,6 +172,20 @@ static inline int driver_match_device(const struct device_driver *drv, · 47 unmodified lines
188172 return drv->bus->match ? drv->bus->match(dev, drv) : 1;
189173 }
190174
175+static inline bool dev_has_sync_state(struct device *dev)
176+{
177+ struct device_driver *drv;
178+
179+ if (!dev)
180+ return false;
181+ drv = READ_ONCE(dev->driver);
182+ if (drv && drv->sync_state)
183+ return true;
184+ if (dev->bus && dev->bus->sync_state)
185+ return true;
186+ return false;
187+}
188+
191189 static inline void dev_sync_state(struct device *dev)
192190 {
193191 if (dev->bus->sync_state)
@@ -196,8 +194,10 @@ static inline void dev_sync_state(struct device *dev) · 2 unmodified lines
196194 dev->driver->sync_state(dev);
197195 }
198196
199−int driver_add_groups(const struct device_driver *drv, const struct attribute_group **groups);
200−void driver_remove_groups(const struct device_driver *drv, const struct attribute_group **groups);
197+int driver_add_groups(const struct device_driver *drv,
198+ const struct attribute_group *const *groups);
199+void driver_remove_groups(const struct device_driver *drv,
200+ const struct attribute_group *const *groups);
201201 void device_driver_detach(struct device *dev);
202202
203203 static inline void device_set_driver(struct device *dev, const struct device_driver *drv)
drivers/base/bus.c+9−2
@@ -544,10 +544,10 @@ static const struct attribute_group driver_override_dev_group = { · 543 unmodified lines
544544 */
545545 int bus_add_device(struct device *dev)
546546 {
547− struct subsys_private *sp = bus_to_subsys(dev->bus);
547+ struct subsys_private *sp;
548548 int error;
549549
550− if (!sp) {
550+ if (!dev->bus) {
551551 /*
552552 * This is a normal operation for many devices that do not
553553 * have a bus assigned to them, just say that all went
@@ -556,6 +556,13 @@ int bus_add_device(struct device *dev) · 2 unmodified lines
556556 return 0;
557557 }
558558
559+ sp = bus_to_subsys(dev->bus);
560+ if (!sp) {
561+ pr_err("%s: cannot add device '%s' to unregistered bus '%s'\n",
562+ __func__, dev_name(dev), dev->bus->name);
563+ return -EINVAL;
564+ }
565+
559566 /*
560567 * Reference in sp is now incremented and will be dropped when
561568 * the device is removed from the bus
drivers/base/core.c+125−59
@@ -235,6 +235,79 @@ static void __fw_devlink_pickup_dangling_consumers(struct fwnode_handle *fwnode, · 234 unmodified lines
235235 __fw_devlink_pickup_dangling_consumers(child, new_sup);
236236 }
237237
238+static void fw_devlink_pickup_dangling_consumers(struct device *dev)
239+{
240+ struct fwnode_handle *child;
241+
242+ guard(mutex)(&fwnode_link_lock);
243+
244+ fwnode_for_each_available_child_node(dev->fwnode, child)
245+ __fw_devlink_pickup_dangling_consumers(child, dev->fwnode);
246+ __fw_devlink_link_to_consumers(dev);
247+}
248+
249+/**
250+ * fw_devlink_refresh_fwnode - Recheck the tree under this firmware node
251+ * @fwnode: The fwnode under which the fwnode tree has changed
252+ *
253+ * This function is mainly meant to adjust the supplier/consumer dependencies
254+ * after a fwnode tree overlay has occurred.
255+ */
256+void fw_devlink_refresh_fwnode(struct fwnode_handle *fwnode)
257+{
258+ struct device *dev;
259+
260+ /*
261+ * Find the closest ancestor fwnode that has been converted to a device
262+ * that can bind to a driver (bus device).
263+ */
264+ fwnode_handle_get(fwnode);
265+ do {
266+ if (fwnode_test_flag(fwnode, FWNODE_FLAG_NOT_DEVICE))
267+ continue;
268+
269+ dev = get_dev_from_fwnode(fwnode);
270+ if (!dev)
271+ continue;
272+
273+ if (dev->bus)
274+ break;
275+
276+ put_device(dev);
277+ } while ((fwnode = fwnode_get_next_parent(fwnode)));
278+
279+ /*
280+ * If none of the ancestor fwnodes have (yet) been converted to a device
281+ * that can bind to a driver, there's nothing to fix up.
282+ */
283+ if (!fwnode)
284+ return;
285+
286+ WARN(device_is_bound(dev) && dev->links.status != DL_DEV_DRIVER_BOUND,
287+ "Don't multithread overlaying and probing the same device!\n");
288+
289+ /*
290+ * If the device has already bound to a driver, then we need to redo
291+ * some of the work that was done after the device was bound to a
292+ * driver. If the device hasn't bound to a driver, running things too
293+ * soon would incorrectly pick up consumers that it shouldn't.
294+ */
295+ if (dev->links.status == DL_DEV_DRIVER_BOUND) {
296+ fw_devlink_pickup_dangling_consumers(dev);
297+ /*
298+ * Some of dangling consumers could have been put previously in
299+ * the deferred probe list due to the unavailability of their
300+ * suppliers. Those consumers have been picked up and some of
301+ * their suppliers links have been updated. Time to re-try their
302+ * probe sequence.
303+ */
304+ driver_deferred_probe_trigger();
305+ }
306+
307+ put_device(dev);
308+ fwnode_handle_put(fwnode);
309+}
310+
238311 static DEFINE_MUTEX(device_links_lock);
239312 DEFINE_STATIC_SRCU(device_links_srcu);
240313
@@ -422,7 +495,7 @@ void device_pm_move_to_tail(struct device *dev) · 181 unmodified lines
422495 #define to_devlink(dev) container_of((dev), struct device_link, link_dev)
423496
424497 static ssize_t status_show(struct device *dev,
425− struct device_attribute *attr, char *buf)
498+ const struct device_attribute *attr, char *buf)
426499 {
427500 const char *output;
428501
@@ -452,10 +525,10 @@ static ssize_t status_show(struct device *dev, · 23 unmodified lines
452525
453526 return sysfs_emit(buf, "%s\n", output);
454527 }
455−static DEVICE_ATTR_RO(status);
528+static const DEVICE_ATTR_RO(status);
456529
457530 static ssize_t auto_remove_on_show(struct device *dev,
458− struct device_attribute *attr, char *buf)
531+ const struct device_attribute *attr, char *buf)
459532 {
460533 struct device_link *link = to_devlink(dev);
461534 const char *output;
@@ -469,27 +542,27 @@ static ssize_t auto_remove_on_show(struct device *dev, · 7 unmodified lines
469542
470543 return sysfs_emit(buf, "%s\n", output);
471544 }
472−static DEVICE_ATTR_RO(auto_remove_on);
545+static const DEVICE_ATTR_RO(auto_remove_on);
473546
474547 static ssize_t runtime_pm_show(struct device *dev,
475− struct device_attribute *attr, char *buf)
548+ const struct device_attribute *attr, char *buf)
476549 {
477550 struct device_link *link = to_devlink(dev);
478551
479552 return sysfs_emit(buf, "%d\n", device_link_test(link, DL_FLAG_PM_RUNTIME));
480553 }
481−static DEVICE_ATTR_RO(runtime_pm);
554+static const DEVICE_ATTR_RO(runtime_pm);
482555
483556 static ssize_t sync_state_only_show(struct device *dev,
484− struct device_attribute *attr, char *buf)
557+ const struct device_attribute *attr, char *buf)
485558 {
486559 struct device_link *link = to_devlink(dev);
487560
488561 return sysfs_emit(buf, "%d\n", device_link_test(link, DL_FLAG_SYNC_STATE_ONLY));
489562 }
490−static DEVICE_ATTR_RO(sync_state_only);
563+static const DEVICE_ATTR_RO(sync_state_only);
491564
492−static struct attribute *devlink_attrs[] = {
565+static const struct attribute *const devlink_attrs[] = {
493566 &dev_attr_status.attr,
494567 &dev_attr_auto_remove_on.attr,
495568 &dev_attr_runtime_pm.attr,
@@ -1011,7 +1084,7 @@ static void device_links_missing_supplier(struct device *dev) · 515 unmodified lines
10111084
10121085 static bool dev_is_best_effort(struct device *dev)
10131086 {
1014− return (fw_devlink_best_effort && dev->can_match) ||
1087+ return (fw_devlink_best_effort && dev_can_match(dev)) ||
10151088 (dev->fwnode && fwnode_test_flag(dev->fwnode, FWNODE_FLAG_BEST_EFFORT));
10161089 }
10171090
@@ -1079,7 +1152,7 @@ int device_links_check_suppliers(struct device *dev) · 61 unmodified lines
10791152
10801153 if (dev_is_best_effort(dev) &&
10811154 device_link_test(link, DL_FLAG_INFERRED) &&
1082− !link->supplier->can_match) {
1155+ !dev_can_match(link->supplier)) {
10831156 ret = -EAGAIN;
10841157 continue;
10851158 }
@@ -1123,7 +1196,7 @@ static void __device_links_queue_sync_state(struct device *dev, · 37 unmodified lines
11231196
11241197 if (!dev_has_sync_state(dev))
11251198 return;
1126− if (dev->state_synced)
1199+ if (dev_state_synced(dev))
11271200 return;
11281201
11291202 list_for_each_entry(link, &dev->links.consumers, s_node) {
@@ -1138,7 +1211,7 @@ static void __device_links_queue_sync_state(struct device *dev, · 8 unmodified lines
11381211 * than once. This can happen if new consumers get added to the device
11391212 * and probed before the list is flushed.
11401213 */
1141− dev->state_synced = true;
1214+ dev_set_state_synced(dev);
11421215
11431216 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
11441217 return;
@@ -1233,7 +1306,7 @@ static void device_link_drop_managed(struct device_link *link) · 88 unmodified lines
12331306 }
12341307
12351308 static ssize_t waiting_for_supplier_show(struct device *dev,
1236− struct device_attribute *attr,
1309+ const struct device_attribute *attr,
12371310 char *buf)
12381311 {
12391312 bool val;
@@ -1244,7 +1317,7 @@ static ssize_t waiting_for_supplier_show(struct device *dev, · 4 unmodified lines
12441317 device_unlock(dev);
12451318 return sysfs_emit(buf, "%u\n", val);
12461319 }
1247−static DEVICE_ATTR_RO(waiting_for_supplier);
1320+static const DEVICE_ATTR_RO(waiting_for_supplier);
12481321
12491322 /**
12501323 * device_links_force_bind - Prepares device to be force bound
@@ -1312,16 +1385,8 @@ void device_links_driver_bound(struct device *dev) · 61 unmodified lines
13121385 * child firmware node.
13131386 */
13141387 if (dev->fwnode && dev->fwnode->dev == dev) {
1315− struct fwnode_handle *child;
1316−
13171388 fwnode_links_purge_suppliers(dev->fwnode);
1318−
1319− guard(mutex)(&fwnode_link_lock);
1320−
1321− fwnode_for_each_available_child_node(dev->fwnode, child)
1322− __fw_devlink_pickup_dangling_consumers(child,
1323− dev->fwnode);
1324− __fw_devlink_link_to_consumers(dev);
1389+ fw_devlink_pickup_dangling_consumers(dev);
13251390 }
13261391 device_remove_file(dev, &dev_attr_waiting_for_supplier);
13271392
@@ -1370,7 +1435,7 @@ void device_links_driver_bound(struct device *dev) · 42 unmodified lines
13701435 } else if (dev_is_best_effort(dev) &&
13711436 device_link_test(link, DL_FLAG_INFERRED) &&
13721437 link->status != DL_STATE_CONSUMER_PROBE &&
1373− !link->supplier->can_match) {
1438+ !dev_can_match(link->supplier)) {
13741439 /*
13751440 * When dev_is_best_effort() is true, we ignore device
13761441 * links to suppliers that don't have a driver. If the
@@ -1435,7 +1500,8 @@ static void __device_links_no_driver(struct device *dev) · 58 unmodified lines
14351500 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
14361501 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
14371502 } else {
1438− WARN_ON(!device_link_test(link, DL_FLAG_SYNC_STATE_ONLY));
1503+ WARN_ON(link->supplier->links.status != DL_DEV_UNBINDING &&
1504+ !device_link_test(link, DL_FLAG_SYNC_STATE_ONLY));
14391505 WRITE_ONCE(link->status, DL_STATE_DORMANT);
14401506 }
14411507 }
@@ -1758,7 +1824,7 @@ static int fw_devlink_no_driver(struct device *dev, void *data) · 316 unmodified lines
17581824 {
17591825 struct device_link *link = to_devlink(dev);
17601826
1761− if (!link->supplier->can_match)
1827+ if (!dev_can_match(link->supplier))
17621828 fw_devlink_relax_link(link);
17631829
17641830 return 0;
@@ -1779,7 +1845,7 @@ static int fw_devlink_dev_sync_state(struct device *dev, void *data) · 14 unmodified lines
17791845 struct device *sup = link->supplier;
17801846
17811847 if (!device_link_test(link, DL_FLAG_MANAGED) ||
1782− link->status == DL_STATE_ACTIVE || sup->state_synced ||
1848+ link->status == DL_STATE_ACTIVE || dev_state_synced(sup) ||
17831849 !dev_has_sync_state(sup))
17841850 return 0;
17851851
@@ -1793,7 +1859,7 @@ static int fw_devlink_dev_sync_state(struct device *dev, void *data) · 7 unmodified lines
17931859 return 0;
17941860
17951861 dev_warn(sup, "Timed out. Forcing sync_state()\n");
1796− sup->state_synced = true;
1862+ dev_set_state_synced(sup);
17971863 get_device(sup);
17981864 list_add_tail(&sup->links.defer_sync, data);
17991865
@@ -2419,9 +2485,11 @@ static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, · 619 unmodified lines
24192485
24202486 if (dev_attr->show)
24212487 ret = dev_attr->show(dev, dev_attr, buf);
2488+ else if (dev_attr->show_const)
2489+ ret = dev_attr->show_const(dev, dev_attr, buf);
24222490 if (ret >= (ssize_t)PAGE_SIZE) {
2423− printk("dev_attr_show: %pS returned bad count\n",
2424− dev_attr->show);
2491+ printk("dev_attr_show: %pS/%pS returned bad count\n",
2492+ dev_attr->show, dev_attr->show_const);
24252493 }
24262494 return ret;
24272495 }
@@ -2435,6 +2503,8 @@ static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, · 7 unmodified lines
24352503
24362504 if (dev_attr->store)
24372505 ret = dev_attr->store(dev, dev_attr, buf, count);
2506+ else if (dev_attr->store_const)
2507+ ret = dev_attr->store_const(dev, dev_attr, buf, count);
24382508 return ret;
24392509 }
24402510
@@ -2722,7 +2792,7 @@ static const struct kset_uevent_ops device_uevent_ops = { · 281 unmodified lines
27222792 .uevent = dev_uevent,
27232793 };
27242794
2725−static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
2795+static ssize_t uevent_show(struct device *dev, const struct device_attribute *attr,
27262796 char *buf)
27272797 {
27282798 struct kobject *top_kobj;
@@ -2765,7 +2835,7 @@ out: · 36 unmodified lines
27652835 return len;
27662836 }
27672837
2768−static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
2838+static ssize_t uevent_store(struct device *dev, const struct device_attribute *attr,
27692839 const char *buf, size_t count)
27702840 {
27712841 int rc;
@@ -2779,20 +2849,20 @@ static ssize_t uevent_store(struct device *dev, struct device_attribute *attr, · 7 unmodified lines
27792849
27802850 return count;
27812851 }
2782−static DEVICE_ATTR_RW(uevent);
2852+static const DEVICE_ATTR_RW(uevent);
27832853
2784−static ssize_t online_show(struct device *dev, struct device_attribute *attr,
2854+static ssize_t online_show(struct device *dev, const struct device_attribute *attr,
27852855 char *buf)
27862856 {
27872857 bool val;
27882858
27892859 device_lock(dev);
2790− val = !dev->offline;
2860+ val = !dev_offline(dev);
27912861 device_unlock(dev);
27922862 return sysfs_emit(buf, "%u\n", val);
27932863 }
27942864
2795−static ssize_t online_store(struct device *dev, struct device_attribute *attr,
2865+static ssize_t online_store(struct device *dev, const struct device_attribute *attr,
27962866 const char *buf, size_t count)
27972867 {
27982868 bool val;
@@ -2810,9 +2880,9 @@ static ssize_t online_store(struct device *dev, struct device_attribute *attr, · 11 unmodified lines
28102880 unlock_device_hotplug();
28112881 return ret < 0 ? ret : count;
28122882 }
2813−static DEVICE_ATTR_RW(online);
2883+static const DEVICE_ATTR_RW(online);
28142884
2815−static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
2885+static ssize_t removable_show(struct device *dev, const struct device_attribute *attr,
28162886 char *buf)
28172887 {
28182888 const char *loc;
@@ -2829,7 +2899,7 @@ static ssize_t removable_show(struct device *dev, struct device_attribute *attr, · 10 unmodified lines
28292899 }
28302900 return sysfs_emit(buf, "%s\n", loc);
28312901 }
2832−static DEVICE_ATTR_RO(removable);
2902+static const DEVICE_ATTR_RO(removable);
28332903
28342904 int device_add_groups(struct device *dev,
28352905 const struct attribute_group *const *groups)
@@ -2913,7 +2983,7 @@ static int device_add_attrs(struct device *dev) · 77 unmodified lines
29132983 if (error)
29142984 goto err_remove_type_groups;
29152985
2916− if (device_supports_offline(dev) && !dev->offline_disabled) {
2986+ if (device_supports_offline(dev) && !dev_offline_disabled(dev)) {
29172987 error = device_create_file(dev, &dev_attr_online);
29182988 if (error)
29192989 goto err_remove_dev_groups;
@@ -2980,12 +3050,12 @@ static void device_remove_attrs(struct device *dev) · 60 unmodified lines
29803050 device_remove_groups(dev, class->dev_groups);
29813051 }
29823052
2983−static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
3053+static ssize_t dev_show(struct device *dev, const struct device_attribute *attr,
29843054 char *buf)
29853055 {
29863056 return print_dev_t(buf, dev->devt);
29873057 }
2988−static DEVICE_ATTR_RO(dev);
3058+static const DEVICE_ATTR_RO(dev);
29893059
29903060 /* /sys/devices/ */
29913061 struct kset *devices_kset;
@@ -3047,10 +3117,10 @@ int device_create_file(struct device *dev, · 55 unmodified lines
30473117 int error = 0;
30483118
30493119 if (dev) {
3050− WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
3120+ WARN(((attr->attr.mode & S_IWUGO) && !(attr->store || attr->store_const)),
30513121 "Attribute %s: write permission without 'store'\n",
30523122 attr->attr.name);
3053− WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
3123+ WARN(((attr->attr.mode & S_IRUGO) && !(attr->show || attr->show_const)),
30543124 "Attribute %s: read permission without 'show'\n",
30553125 attr->attr.name);
30563126 error = sysfs_create_file(&dev->kobj, &attr->attr);
@@ -3170,11 +3240,7 @@ void device_initialize(struct device *dev) · 113 unmodified lines
31703240 INIT_LIST_HEAD(&dev->links.suppliers);
31713241 INIT_LIST_HEAD(&dev->links.defer_sync);
31723242 dev->links.status = DL_DEV_NO_DRIVER;
3173−#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
3174− defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
3175− defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
3176− dev->dma_coherent = dma_default_coherent;
3177−#endif
3243+ dev_assign_dma_coherent(dev, dma_default_coherent);
31783244 swiotlb_dev_init(dev);
31793245 }
31803246 EXPORT_SYMBOL_GPL(device_initialize);
@@ -3710,7 +3776,7 @@ int device_add(struct device *dev) · 529 unmodified lines
37103776 * match with any driver, don't block its consumers from probing in
37113777 * case the consumer device is able to operate without this supplier.
37123778 */
3713− if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match)
3779+ if (dev->fwnode && fw_devlink_drv_reg_done && !dev_can_match(dev))
37143780 fw_devlink_unblock_consumers(dev);
37153781
37163782 if (parent)
@@ -4180,7 +4246,7 @@ static int device_check_offline(struct device *dev, void *not_used) · 463 unmodified lines
41804246 if (ret)
41814247 return ret;
41824248
4183− return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
4249+ return device_supports_offline(dev) && !dev_offline(dev) ? -EBUSY : 0;
41844250 }
41854251
41864252 /**
@@ -4198,7 +4264,7 @@ int device_offline(struct device *dev) · 11 unmodified lines
41984264 {
41994265 int ret;
42004266
4201− if (dev->offline_disabled)
4267+ if (dev_offline_disabled(dev))
42024268 return -EPERM;
42034269
42044270 ret = device_for_each_child(dev, NULL, device_check_offline);
@@ -4207,13 +4273,13 @@ int device_offline(struct device *dev) · 2 unmodified lines
42074273
42084274 device_lock(dev);
42094275 if (device_supports_offline(dev)) {
4210− if (dev->offline) {
4276+ if (dev_offline(dev)) {
42114277 ret = 1;
42124278 } else {
42134279 ret = dev->bus->offline(dev);
42144280 if (!ret) {
42154281 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
4216− dev->offline = true;
4282+ dev_set_offline(dev);
42174283 }
42184284 }
42194285 }
@@ -4238,11 +4304,11 @@ int device_online(struct device *dev) · 18 unmodified lines
42384304
42394305 device_lock(dev);
42404306 if (device_supports_offline(dev)) {
4241− if (dev->offline) {
4307+ if (dev_offline(dev)) {
42424308 ret = dev->bus->online(dev);
42434309 if (!ret) {
42444310 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
4245− dev->offline = false;
4311+ dev_clear_offline(dev);
42464312 }
42474313 } else {
42484314 ret = 1;
@@ -4716,7 +4782,7 @@ static int device_attrs_change_owner(struct device *dev, kuid_t kuid, · 467 unmodified lines
47164782 if (error)
47174783 return error;
47184784
4719− if (device_supports_offline(dev) && !dev->offline_disabled) {
4785+ if (device_supports_offline(dev) && !dev_offline_disabled(dev)) {
47204786 /* Change online device attributes of @dev to @kuid/@kgid. */
47214787 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
47224788 kuid, kgid);
@@ -5283,7 +5349,7 @@ void device_set_of_node_from_dev(struct device *dev, const struct device *dev2) · 560 unmodified lines
52835349 {
52845350 of_node_put(dev->of_node);
52855351 dev->of_node = of_node_get(dev2->of_node);
5286− dev->of_node_reused = true;
5352+ dev_set_of_node_reused(dev);
52875353 }
52885354 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
52895355
drivers/base/cpu.c+2−2
@@ -422,8 +422,8 @@ int register_cpu(struct cpu *cpu, int num) · 421 unmodified lines
422422 cpu->dev.id = num;
423423 cpu->dev.bus = &cpu_subsys;
424424 cpu->dev.release = cpu_device_release;
425− cpu->dev.offline_disabled = !cpu->hotpluggable;
426− cpu->dev.offline = !cpu_online(num);
425+ dev_assign_offline_disabled(&cpu->dev, !cpu->hotpluggable);
426+ dev_assign_offline(&cpu->dev, !cpu_online(num));
427427 cpu->dev.of_node = of_get_cpu_node(num, NULL);
428428 cpu->dev.groups = common_cpu_attr_groups;
429429 if (cpu->hotpluggable)
drivers/base/dd.c+12−15
@@ -132,7 +132,7 @@ static DECLARE_WORK(deferred_probe_work, deferred_probe_work_func); · 131 unmodified lines
132132
133133 void driver_deferred_probe_add(struct device *dev)
134134 {
135− if (!dev->can_match)
135+ if (!dev_can_match(dev))
136136 return;
137137
138138 mutex_lock(&deferred_probe_mutex);
@@ -323,12 +323,11 @@ void deferred_probe_extend_timeout(void) · 184 unmodified lines
323323 * If the work hasn't been queued yet or if the work expired, don't
324324 * start a new one.
325325 */
326− if (cancel_delayed_work(&deferred_probe_timeout_work)) {
327− schedule_delayed_work(&deferred_probe_timeout_work,
328− driver_deferred_probe_timeout * HZ);
326+ if (delayed_work_pending(&deferred_probe_timeout_work) &&
327+ mod_delayed_work(system_percpu_wq, &deferred_probe_timeout_work,
328+ secs_to_jiffies(driver_deferred_probe_timeout)))
329329 pr_debug("Extended deferred probe timeout by %d secs\n",
330330 driver_deferred_probe_timeout);
331− }
332331 }
333332
334333 /**
@@ -569,12 +568,10 @@ static ssize_t state_synced_store(struct device *dev, · 234 unmodified lines
569568 return -EINVAL;
570569
571570 device_lock(dev);
572− if (!dev->state_synced) {
573− dev->state_synced = true;
571+ if (!dev_test_and_set_state_synced(dev))
574572 dev_sync_state(dev);
575− } else {
573+ else
576574 ret = -EINVAL;
577− }
578575 device_unlock(dev);
579576
580577 return ret ? ret : count;
@@ -586,7 +583,7 @@ static ssize_t state_synced_show(struct device *dev, · 5 unmodified lines
586583 bool val;
587584
588585 device_lock(dev);
589− val = dev->state_synced;
586+ val = dev_state_synced(dev);
590587 device_unlock(dev);
591588
592589 return sysfs_emit(buf, "%u\n", val);
@@ -595,9 +592,9 @@ static DEVICE_ATTR_RW(state_synced); · 2 unmodified lines
595592
596593 static void device_unbind_cleanup(struct device *dev)
597594 {
598− devres_release_all(dev);
599595 if (dev->driver->p_cb.post_unbind_rust)
600596 dev->driver->p_cb.post_unbind_rust(dev);
597+ devres_release_all(dev);
601598 arch_teardown_dma_ops(dev);
602599 kfree(dev->dma_range_map);
603600 dev->dma_range_map = NULL;
@@ -849,14 +846,14 @@ static int __driver_probe_device(const struct device_driver *drv, struct device · 245 unmodified lines
849846 return dev_err_probe(dev, -EPROBE_DEFER, "Device not ready to probe\n");
850847
851848 /*
852− * Set can_match = true after calling dev_ready_to_probe(), so
849+ * Call dev_set_can_match() after calling dev_ready_to_probe(), so
853850 * driver_deferred_probe_add() won't actually add the device to the
854851 * deferred probe list when dev_ready_to_probe() returns false.
855852 *
856853 * When dev_ready_to_probe() returns false, it means that device_add()
857854 * will do another probe() attempt for us.
858855 */
859− dev->can_match = true;
856+ dev_set_can_match(dev);
860857 dev_dbg(dev, "bus: '%s': %s: matched device with driver %s\n",
861858 drv->bus->name, __func__, drv->name);
862859
@@ -1002,7 +999,7 @@ static int __device_attach_driver(struct device_driver *drv, void *_data) · 139 unmodified lines
1002999 return 0;
10031000 } else if (ret == -EPROBE_DEFER) {
10041001 dev_dbg(dev, "Device match requests probe deferral\n");
1005− dev->can_match = true;
1002+ dev_set_can_match(dev);
10061003 driver_deferred_probe_add(dev);
10071004 /*
10081005 * Device can't match with a driver right now, so don't attempt
@@ -1254,7 +1251,7 @@ static int __driver_attach(struct device *dev, void *data) · 245 unmodified lines
12541251 return 0;
12551252 } else if (ret == -EPROBE_DEFER) {
12561253 dev_dbg(dev, "Device match requests probe deferral\n");
1257− dev->can_match = true;
1254+ dev_set_can_match(dev);
12581255 driver_deferred_probe_add(dev);
12591256 /*
12601257 * Driver could not match with device, but may match with
drivers/base/devcoredump.c+0−7
@@ -471,10 +471,3 @@ static int __init devcoredump_init(void) · 470 unmodified lines
471471 return class_register(&devcd_class);
472472 }
473473 __initcall(devcoredump_init);
474−
475−static void __exit devcoredump_exit(void)
476−{
477− class_for_each_device(&devcd_class, NULL, NULL, devcd_free);
478− class_unregister(&devcd_class);
479−}
480−__exitcall(devcoredump_exit);
drivers/base/driver.c+2−77
@@ -30,81 +30,6 @@ static struct device *next_device(struct klist_iter *i) · 29 unmodified lines
3030 return dev;
3131 }
3232
33−/**
34− * driver_set_override() - Helper to set or clear driver override.
35− * @dev: Device to change
36− * @override: Address of string to change (e.g. &device->driver_override);
37− * The contents will be freed and hold newly allocated override.
38− * @s: NUL-terminated string, new driver name to force a match, pass empty
39− * string to clear it ("" or "\n", where the latter is only for sysfs
40− * interface).
41− * @len: length of @s
42− *
43− * Helper to set or clear driver override in a device, intended for the cases
44− * when the driver_override field is allocated by driver/bus code.
45− *
46− * Returns: 0 on success or a negative error code on failure.
47− */
48−int driver_set_override(struct device *dev, const char **override,
49− const char *s, size_t len)
50−{
51− const char *new, *old;
52− char *cp;
53−
54− if (!override || !s)
55− return -EINVAL;
56−
57− /*
58− * The stored value will be used in sysfs show callback (sysfs_emit()),
59− * which has a length limit of PAGE_SIZE and adds a trailing newline.
60− * Thus we can store one character less to avoid truncation during sysfs
61− * show.
62− */
63− if (len >= (PAGE_SIZE - 1))
64− return -EINVAL;
65−
66− /*
67− * Compute the real length of the string in case userspace sends us a
68− * bunch of \0 characters like python likes to do.
69− */
70− len = strlen(s);
71−
72− if (!len) {
73− /* Empty string passed - clear override */
74− device_lock(dev);
75− old = *override;
76− *override = NULL;
77− device_unlock(dev);
78− kfree(old);
79−
80− return 0;
81− }
82−
83− cp = strnchr(s, len, '\n');
84− if (cp)
85− len = cp - s;
86−
87− new = kstrndup(s, len, GFP_KERNEL);
88− if (!new)
89− return -ENOMEM;
90−
91− device_lock(dev);
92− old = *override;
93− if (cp != s) {
94− *override = new;
95− } else {
96− /* "\n" passed - clear override */
97− kfree(new);
98− *override = NULL;
99− }
100− device_unlock(dev);
101−
102− kfree(old);
103−
104− return 0;
105−}
106−EXPORT_SYMBOL_GPL(driver_set_override);
107−
10833 /**
10934 * driver_for_each_device - Iterator for devices bound to a driver.
11035 * @drv: Driver we're iterating.
@@ -203,13 +128,13 @@ void driver_remove_file(const struct device_driver *drv, · 92 unmodified lines
203128 EXPORT_SYMBOL_GPL(driver_remove_file);
204129
205130 int driver_add_groups(const struct device_driver *drv,
206− const struct attribute_group **groups)
131+ const struct attribute_group *const *groups)
207132 {
208133 return sysfs_create_groups(&drv->p->kobj, groups);
209134 }
210135
211136 void driver_remove_groups(const struct device_driver *drv,
212− const struct attribute_group **groups)
137+ const struct attribute_group *const *groups)
213138 {
214139 sysfs_remove_groups(&drv->p->kobj, groups);
215140 }
drivers/base/faux.c+15−17
@@ -133,6 +133,9 @@ struct faux_device *faux_device_create_with_groups(const char *name, · 132 unmodified lines
133133 struct device *dev;
134134 int ret;
135135
136+ if (!faux_bus_root)
137+ return NULL;
138+
136139 faux_obj = kzalloc_obj(*faux_obj);
137140 if (!faux_obj)
138141 return NULL;
@@ -232,34 +235,29 @@ EXPORT_SYMBOL_GPL(faux_device_destroy); · 93 unmodified lines
232235
233236 int __init faux_bus_init(void)
234237 {
238+ struct device *root;
235239 int ret;
236240
237− faux_bus_root = kzalloc_obj(*faux_bus_root);
238− if (!faux_bus_root)
239− return -ENOMEM;
240−
241− dev_set_name(faux_bus_root, "faux");
242−
243− ret = device_register(faux_bus_root);
244− if (ret) {
245− put_device(faux_bus_root);
246− return ret;
247− }
241+ root = root_device_register("faux");
242+ if (IS_ERR(root))
243+ return PTR_ERR(root);
248244
249245 ret = bus_register(&faux_bus_type);
250246 if (ret)
251− goto error_bus;
247+ goto err_deregister_root;
252248
253249 ret = driver_register(&faux_driver);
254250 if (ret)
255− goto error_driver;
251+ goto err_deregister_bus;
256252
257− return ret;
253+ faux_bus_root = root;
254+
255+ return 0;
258256
259−error_driver:
257+err_deregister_bus:
260258 bus_unregister(&faux_bus_type);
259+err_deregister_root:
260+ root_device_unregister(root);
261261
262−error_bus:
263− device_unregister(faux_bus_root);
264262 return ret;
265263 }
drivers/base/firmware_loader/main.c+2−1
@@ -1503,9 +1503,10 @@ static void device_cache_fw_images(void) · 1502 unmodified lines
15031503
15041504 mutex_lock(&fw_lock);
15051505 fwc->state = FW_LOADER_START_CACHE;
1506− dpm_for_each_dev(NULL, dev_cache_fw_image);
15071506 mutex_unlock(&fw_lock);
15081507
1508+ dpm_for_each_dev(NULL, dev_cache_fw_image);
1509+
15091510 /* wait for completion of caching firmware for all devices */
15101511 async_synchronize_full_domain(&fw_cache_domain);
15111512
drivers/base/firmware_loader/sysfs_upload.c+3−5
@@ -343,7 +343,6 @@ firmware_upload_register(struct module *module, struct device *parent, · 342 unmodified lines
343343 goto free_fw_upload_priv;
344344 }
345345 fw_upload->priv = fw_sysfs;
346− fw_sysfs->fw_upload_priv = fw_upload_priv;
347346 fw_dev = &fw_sysfs->dev;
348347
349348 ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, NULL, 0, 0,
@@ -351,10 +350,12 @@ firmware_upload_register(struct module *module, struct device *parent, · 1 unmodified line
351350 if (ret != 0) {
352351 if (ret > 0)
353352 ret = -EINVAL;
354− goto free_fw_sysfs;
353+ put_device(fw_dev);
354+ goto free_fw_upload_priv;
355355 }
356356 fw_priv->is_paged_buf = true;
357357 fw_sysfs->fw_priv = fw_priv;
358+ fw_sysfs->fw_upload_priv = fw_upload_priv;
358359
359360 ret = device_add(fw_dev);
360361 if (ret) {
@@ -365,9 +366,6 @@ firmware_upload_register(struct module *module, struct device *parent, · 4 unmodified lines
365366
366367 return fw_upload;
367368
368−free_fw_sysfs:
369− kfree(fw_sysfs);
370−
371369 free_fw_upload_priv:
372370 kfree(fw_upload_priv);
373371
drivers/base/isa.c+6−6
@@ -11,9 +11,7 @@ · 10 unmodified lines
1111 #include <linux/dma-mapping.h>
1212 #include <linux/isa.h>
1313
14−static struct device isa_bus = {
15− .init_name = "isa"
16−};
14+static struct device *isa_bus;
1715
1816 struct isa_dev {
1917 struct device dev;
@@ -131,7 +129,7 @@ int isa_register_driver(struct isa_driver *isa_driver, unsigned int ndev) · 111 unmodified lines
131129 break;
132130 }
133131
134− isa_dev->dev.parent = &isa_bus;
132+ isa_dev->dev.parent = isa_bus;
135133 isa_dev->dev.bus = &isa_bus_type;
136134
137135 dev_set_name(&isa_dev->dev, "%s.%u",
@@ -169,9 +167,11 @@ static int __init isa_bus_init(void) · 31 unmodified lines
169167
170168 error = bus_register(&isa_bus_type);
171169 if (!error) {
172− error = device_register(&isa_bus);
173− if (error)
170+ isa_bus = root_device_register("isa");
171+ if (IS_ERR(isa_bus)) {
172+ error = PTR_ERR(isa_bus);
174173 bus_unregister(&isa_bus_type);
174+ }
175175 }
176176 return error;
177177 }
drivers/base/memory.c+1−1
@@ -697,7 +697,7 @@ static int __add_memory_block(struct memory_block *memory) · 696 unmodified lines
697697 memory->dev.id = memory->start_section_nr / sections_per_block;
698698 memory->dev.release = memory_block_release;
699699 memory->dev.groups = memory_memblk_attr_groups;
700− memory->dev.offline = memory->state == MEM_OFFLINE;
700+ dev_assign_offline(&memory->dev, memory->state == MEM_OFFLINE);
701701
702702 ret = device_register(&memory->dev);
703703 if (ret) {
drivers/base/pinctrl.c+1−1
@@ -24,7 +24,7 @@ int pinctrl_bind_pins(struct device *dev) · 23 unmodified lines
2424 {
2525 int ret;
2626
27− if (dev->of_node_reused)
27+ if (dev_of_node_reused(dev))
2828 return 0;
2929
3030 dev->pins = devm_kzalloc(dev, sizeof(*(dev->pins)), GFP_KERNEL);
drivers/base/platform.c+15−8
@@ -870,7 +870,7 @@ struct platform_device *platform_device_register_full(const struct platform_devi · 869 unmodified lines
870870 pdev->dev.parent = pdevinfo->parent;
871871 pdev->dev.fwnode = pdevinfo->fwnode;
872872 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
873− pdev->dev.of_node_reused = pdevinfo->of_node_reused;
873+ dev_assign_of_node_reused(&pdev->dev, pdevinfo->of_node_reused);
874874
875875 if (pdevinfo->dma_mask) {
876876 pdev->platform_dma_mask = pdevinfo->dma_mask;
@@ -915,11 +915,14 @@ EXPORT_SYMBOL_GPL(platform_device_register_full); · 38 unmodified lines
915915 * __platform_driver_register - register a driver for platform-level devices
916916 * @drv: platform driver structure
917917 * @owner: owning module/driver
918+ * @mod_name: module name string
918919 */
919−int __platform_driver_register(struct platform_driver *drv, struct module *owner)
920+int __platform_driver_register(struct platform_driver *drv, struct module *owner,
921+ const char *mod_name)
920922 {
921923 drv->driver.owner = owner;
922924 drv->driver.bus = &platform_bus_type;
925+ drv->driver.mod_name = mod_name;
923926
924927 return driver_register(&drv->driver);
925928 }
@@ -952,6 +955,7 @@ static int is_bound_to_driver(struct device *dev, void *driver) · 26 unmodified lines
952955 * @drv: platform driver structure
953956 * @probe: the driver probe routine, probably from an __init section
954957 * @module: module which will be the owner of the driver
958+ * @mod_name: module name string
955959 *
956960 * Use this instead of platform_driver_register() when you know the device
957961 * is not hotpluggable and has already been registered, and you want to
@@ -969,7 +973,8 @@ static int is_bound_to_driver(struct device *dev, void *driver) · 11 unmodified lines
969973 */
970974 int __init_or_module __platform_driver_probe(struct platform_driver *drv,
971975 int (*probe)(struct platform_device *),
972− struct module *module)
976+ struct module *module,
977+ const char *mod_name)
973978 {
974979 int retval;
975980
@@ -997,7 +1002,7 @@ int __init_or_module __platform_driver_probe(struct platform_driver *drv, · 21 unmodified lines
9971002
9981003 /* temporary section violation during probe() */
9991004 drv->probe = probe;
1000− retval = __platform_driver_register(drv, module);
1005+ retval = __platform_driver_register(drv, module, mod_name);
10011006 if (retval)
10021007 return retval;
10031008
@@ -1025,6 +1030,7 @@ EXPORT_SYMBOL_GPL(__platform_driver_probe); · 21 unmodified lines
10251030 * @data: platform specific data for this platform device
10261031 * @size: size of platform specific data
10271032 * @module: module which will be the owner of the driver
1033+ * @mod_name: module name string
10281034 *
10291035 * Use this in legacy-style modules that probe hardware directly and
10301036 * register a single platform device and corresponding platform driver.
@@ -1035,7 +1041,7 @@ struct platform_device * __init_or_module · 4 unmodified lines
10351041 __platform_create_bundle(struct platform_driver *driver,
10361042 int (*probe)(struct platform_device *),
10371043 struct resource *res, unsigned int n_res,
1038− const void *data, size_t size, struct module *module)
1044+ const void *data, size_t size, struct module *module, const char *mod_name)
10391045 {
10401046 struct platform_device *pdev;
10411047 int error;
@@ -1058,7 +1064,7 @@ __platform_create_bundle(struct platform_driver *driver, · 16 unmodified lines
10581064 if (error)
10591065 goto err_pdev_put;
10601066
1061− error = __platform_driver_probe(driver, probe, module);
1067+ error = __platform_driver_probe(driver, probe, module, mod_name);
10621068 if (error)
10631069 goto err_pdev_del;
10641070
@@ -1078,6 +1084,7 @@ EXPORT_SYMBOL_GPL(__platform_create_bundle); · 13 unmodified lines
10781084 * @drivers: an array of drivers to register
10791085 * @count: the number of drivers to register
10801086 * @owner: module owning the drivers
1087+ * @mod_name: module name string
10811088 *
10821089 * Registers platform drivers specified by an array. On failure to register a
10831090 * driver, all previously registered drivers will be unregistered. Callers of
@@ -1087,7 +1094,7 @@ EXPORT_SYMBOL_GPL(__platform_create_bundle); · 3 unmodified lines
10871094 * Returns: 0 on success or a negative error code on failure.
10881095 */
10891096 int __platform_register_drivers(struct platform_driver * const *drivers,
1090− unsigned int count, struct module *owner)
1097+ unsigned int count, struct module *owner, const char *mod_name)
10911098 {
10921099 unsigned int i;
10931100 int err;
@@ -1095,7 +1102,7 @@ int __platform_register_drivers(struct platform_driver * const *drivers, · 1 unmodified line
10951102 for (i = 0; i < count; i++) {
10961103 pr_debug("registering platform driver %ps\n", drivers[i]);
10971104
1098− err = __platform_driver_register(drivers[i], owner);
1105+ err = __platform_driver_register(drivers[i], owner, mod_name);
10991106 if (err < 0) {
11001107 pr_err("failed to register platform driver %ps: %d\n",
11011108 drivers[i], err);
drivers/base/property.c+3−1
@@ -1277,8 +1277,10 @@ fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode, · 1276 unmodified lines
12771277 if (fwnode_ep.port != port)
12781278 continue;
12791279
1280− if (fwnode_ep.id == endpoint)
1280+ if (fwnode_ep.id == endpoint) {
1281+ fwnode_handle_put(best_ep);
12811282 return ep;
1283+ }
12821284
12831285 if (!endpoint_next)
12841286 continue;
drivers/base/swnode.c+24−16
@@ -374,20 +374,28 @@ EXPORT_SYMBOL_GPL(property_entries_free); · 373 unmodified lines
374374 /* -------------------------------------------------------------------------- */
375375 /* fwnode operations */
376376
377−static struct fwnode_handle *software_node_get(struct fwnode_handle *fwnode)
377+static struct swnode *swnode_get(struct swnode *swnode)
378378 {
379− struct swnode *swnode = to_swnode(fwnode);
380−
381379 kobject_get(&swnode->kobj);
382380
381+ return swnode;
382+}
383+
384+static void swnode_put(struct swnode *swnode)
385+{
386+ kobject_put(&swnode->kobj);
387+}
388+
389+static struct fwnode_handle *software_node_get(struct fwnode_handle *fwnode)
390+{
391+ struct swnode *swnode = swnode_get(to_swnode(fwnode));
392+
383393 return &swnode->fwnode;
384394 }
385395
386396 static void software_node_put(struct fwnode_handle *fwnode)
387397 {
388− struct swnode *swnode = to_swnode(fwnode);
389−
390− kobject_put(&swnode->kobj);
398+ swnode_put(to_swnode(fwnode));
391399 }
392400
393401 static bool software_node_property_present(const struct fwnode_handle *fwnode,
@@ -493,7 +501,7 @@ software_node_get_named_child_node(const struct fwnode_handle *fwnode, · 99 unmodified lines
493501
494502 list_for_each_entry(child, &swnode->children, entry) {
495503 if (!strcmp(childname, kobject_name(&child->kobj))) {
496− kobject_get(&child->kobj);
504+ swnode_get(child);
497505 return &child->fwnode;
498506 }
499507 }
@@ -737,7 +745,7 @@ software_node_find_by_name(const struct software_node *parent, const char *name) · 237 unmodified lines
737745 swnode = kobj_to_swnode(k);
738746 if (parent == swnode->node->parent && swnode->node->name &&
739747 !strcmp(name, swnode->node->name)) {
740− kobject_get(&swnode->kobj);
748+ swnode_get(swnode);
741749 break;
742750 }
743751 swnode = NULL;
@@ -835,13 +843,13 @@ swnode_register(const struct software_node *node, struct swnode *parent, · 91 unmodified lines
835843 parent ? &parent->kobj : NULL,
836844 "node%d", swnode->id);
837845 if (ret) {
838− kobject_put(&swnode->kobj);
846+ swnode_put(swnode);
839847 return ERR_PTR(ret);
840848 }
841849
842850 /*
843851 * Assign the flag only in the successful case, so
844− * the above kobject_put() won't mess up with properties.
852+ * the above swnode_put() won't mess up with properties.
845853 */
846854 swnode->allocated = allocated;
847855
@@ -978,7 +986,7 @@ void fwnode_remove_software_node(struct fwnode_handle *fwnode) · 130 unmodified lines
978986 if (!swnode)
979987 return;
980988
981− kobject_put(&swnode->kobj);
989+ swnode_put(swnode);
982990 }
983991 EXPORT_SYMBOL_GPL(fwnode_remove_software_node);
984992
@@ -1002,7 +1010,7 @@ int device_add_software_node(struct device *dev, const struct software_node *nod · 17 unmodified lines
10021010
10031011 swnode = software_node_to_swnode(node);
10041012 if (swnode) {
1005− kobject_get(&swnode->kobj);
1013+ swnode_get(swnode);
10061014 } else {
10071015 ret = software_node_register(node);
10081016 if (ret)
@@ -1044,7 +1052,7 @@ void device_remove_software_node(struct device *dev) · 35 unmodified lines
10441052 software_node_notify_remove(dev);
10451053
10461054 set_secondary_fwnode(dev, NULL);
1047− kobject_put(&swnode->kobj);
1055+ swnode_put(swnode);
10481056 }
10491057 EXPORT_SYMBOL_GPL(device_remove_software_node);
10501058
@@ -1097,7 +1105,7 @@ void software_node_notify(struct device *dev) · 46 unmodified lines
10971105 if (!swnode)
10981106 return;
10991107
1100− kobject_get(&swnode->kobj);
1108+ swnode_get(swnode);
11011109 ret = sysfs_create_link(&dev->kobj, &swnode->kobj, "software_node");
11021110 if (ret)
11031111 return;
@@ -1119,11 +1127,11 @@ void software_node_notify_remove(struct device *dev) · 15 unmodified lines
11191127
11201128 sysfs_remove_link(&swnode->kobj, dev_name(dev));
11211129 sysfs_remove_link(&dev->kobj, "software_node");
1122− kobject_put(&swnode->kobj);
1130+ swnode_put(swnode);
11231131
11241132 if (swnode->managed) {
11251133 set_secondary_fwnode(dev, NULL);
1126− kobject_put(&swnode->kobj);
1134+ swnode_put(swnode);
11271135 }
11281136 }
11291137
drivers/bus/imx-weim.c+0−6
@@ -327,12 +327,6 @@ static int of_weim_notify(struct notifier_block *nb, unsigned long action, · 326 unmodified lines
327327 "Failed to setup timing for '%pOF'\n", rd->dn);
328328
329329 if (!of_node_check_flag(rd->dn, OF_POPULATED)) {
330− /*
331− * Clear the flag before adding the device so that
332− * fw_devlink doesn't skip adding consumers to this
333− * device.
334− */
335− fwnode_clear_flag(&rd->dn->fwnode, FWNODE_FLAG_NOT_DEVICE);
336330 if (!of_platform_device_create(rd->dn, NULL, &pdev->dev)) {
337331 dev_err(&pdev->dev,
338332 "Failed to create child device '%pOF'\n",
drivers/cdx/cdx.c+5−35
@@ -156,8 +156,6 @@ static int cdx_unregister_device(struct device *dev, · 155 unmodified lines
156156 } else {
157157 cdx_destroy_res_attr(cdx_dev, MAX_CDX_DEV_RESOURCES);
158158 debugfs_remove_recursive(cdx_dev->debugfs_dir);
159− kfree(cdx_dev->driver_override);
160− cdx_dev->driver_override = NULL;
161159 }
162160
163161 /*
@@ -268,6 +266,7 @@ static int cdx_bus_match(struct device *dev, const struct device_driver *drv) · 104 unmodified lines
268266 const struct cdx_driver *cdx_drv = to_cdx_driver(drv);
269267 const struct cdx_device_id *found_id = NULL;
270268 const struct cdx_device_id *ids;
269+ int ret;
271270
272271 if (cdx_dev->is_bus)
273272 return false;
@@ -275,7 +274,8 @@ static int cdx_bus_match(struct device *dev, const struct device_driver *drv) · 1 unmodified line
275274 ids = cdx_drv->match_id_table;
276275
277276 /* When driver_override is set, only bind to the matching driver */
278− if (cdx_dev->driver_override && strcmp(cdx_dev->driver_override, drv->name))
277+ ret = device_match_driver_override(dev, drv);
278+ if (ret == 0)
279279 return false;
280280
281281 found_id = cdx_match_id(ids, cdx_dev);
@@ -289,7 +289,7 @@ static int cdx_bus_match(struct device *dev, const struct device_driver *drv) · 7 unmodified lines
289289 */
290290 if (!found_id->override_only)
291291 return true;
292− if (cdx_dev->driver_override)
292+ if (ret > 0)
293293 return true;
294294
295295 ids = found_id + 1;
@@ -453,36 +453,6 @@ static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, · 157 unmodified lines
453453 }
454454 static DEVICE_ATTR_RO(modalias);
455455
456−static ssize_t driver_override_store(struct device *dev,
457− struct device_attribute *attr,
458− const char *buf, size_t count)
459−{
460− struct cdx_device *cdx_dev = to_cdx_device(dev);
461− int ret;
462−
463− if (WARN_ON(dev->bus != &cdx_bus_type))
464− return -EINVAL;
465−
466− ret = driver_set_override(dev, &cdx_dev->driver_override, buf, count);
467− if (ret)
468− return ret;
469−
470− return count;
471−}
472−
473−static ssize_t driver_override_show(struct device *dev,
474− struct device_attribute *attr, char *buf)
475−{
476− struct cdx_device *cdx_dev = to_cdx_device(dev);
477− ssize_t len;
478−
479− device_lock(dev);
480− len = sysfs_emit(buf, "%s\n", cdx_dev->driver_override);
481− device_unlock(dev);
482− return len;
483−}
484−static DEVICE_ATTR_RW(driver_override);
485−
486456 static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
487457 const char *buf, size_t count)
488458 {
@@ -552,7 +522,6 @@ static struct attribute *cdx_dev_attrs[] = { · 63 unmodified lines
552522 &dev_attr_class.attr,
553523 &dev_attr_revision.attr,
554524 &dev_attr_modalias.attr,
555− &dev_attr_driver_override.attr,
556525 NULL,
557526 };
558527
@@ -646,6 +615,7 @@ ATTRIBUTE_GROUPS(cdx_bus); · 87 unmodified lines
646615
647616 const struct bus_type cdx_bus_type = {
648617 .name = "cdx",
618+ .driver_override = true,
649619 .match = cdx_bus_match,
650620 .probe = cdx_probe,
651621 .remove = cdx_remove,
drivers/cpufreq/rcpufreq_dt.rs+5−4
@@ -201,12 +201,13 @@ kernel::of_device_table!( · 200 unmodified lines
201201
202202 impl platform::Driver for CPUFreqDTDriver {
203203 type IdInfo = ();
204+ type Data<'bound> = Self;
204205 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
205206
206− fn probe(
207− pdev: &platform::Device<Core>,
208− _id_info: Option<&Self::IdInfo>,
209− ) -> impl PinInit<Self, Error> {
207+ fn probe<'bound>(
208+ pdev: &'bound platform::Device<Core<'_>>,
209+ _id_info: Option<&'bound Self::IdInfo>,
210+ ) -> impl PinInit<Self, Error> + 'bound {
210211 cpufreq::Registration::<CPUFreqDTDriver>::new_foreign_owned(pdev.as_ref())?;
211212 Ok(Self {})
212213 }
drivers/devfreq/devfreq-event.c+13−10
@@ -17,7 +17,13 @@ · 16 unmodified lines
1717 #include <linux/list.h>
1818 #include <linux/of.h>
1919
20−static struct class *devfreq_event_class;
20+static struct attribute *devfreq_event_attrs[];
21+ATTRIBUTE_GROUPS(devfreq_event);
22+
23+static const struct class devfreq_event_class = {
24+ .name = "devfreq-event",
25+ .dev_groups = devfreq_event_groups
26+};
2127
2228 /* The list of all devfreq event list */
2329 static LIST_HEAD(devfreq_event_list);
@@ -321,7 +327,7 @@ struct devfreq_event_dev *devfreq_event_add_edev(struct device *dev, · 297 unmodified lines
321327 edev->desc = desc;
322328 edev->enable_count = 0;
323329 edev->dev.parent = dev;
324− edev->dev.class = devfreq_event_class;
330+ edev->dev.class = &devfreq_event_class;
325331 edev->dev.release = devfreq_event_release_edev;
326332
327333 dev_set_name(&edev->dev, "event%d", atomic_inc_return(&event_no));
@@ -461,18 +467,15 @@ static struct attribute *devfreq_event_attrs[] = { · 133 unmodified lines
461467 &dev_attr_enable_count.attr,
462468 NULL,
463469 };
464−ATTRIBUTE_GROUPS(devfreq_event);
465470
466471 static int __init devfreq_event_init(void)
467472 {
468− devfreq_event_class = class_create("devfreq-event");
469− if (IS_ERR(devfreq_event_class)) {
470− pr_err("%s: couldn't create class\n", __FILE__);
471− return PTR_ERR(devfreq_event_class);
472− }
473+ int err;
473474
474− devfreq_event_class->dev_groups = devfreq_event_groups;
475+ err = class_register(&devfreq_event_class);
476+ if (err)
477+ pr_err("%s: couldn't create class\n", __FILE__);
475478
476− return 0;
479+ return err;
477480 }
478481 subsys_initcall(devfreq_event_init);
drivers/dma/ti/k3-udma-glue.c+3−3
@@ -312,7 +312,7 @@ k3_udma_glue_request_tx_chn_common(struct device *dev, · 311 unmodified lines
312312
313313 if (xudma_is_pktdma(tx_chn->common.udmax)) {
314314 /* prepare the channel device as coherent */
315− tx_chn->common.chan_dev.dma_coherent = true;
315+ dev_set_dma_coherent(&tx_chn->common.chan_dev);
316316 dma_coerce_mask_and_coherent(&tx_chn->common.chan_dev,
317317 DMA_BIT_MASK(48));
318318 }
@@ -1003,7 +1003,7 @@ k3_udma_glue_request_rx_chn_priv(struct device *dev, const char *name, · 684 unmodified lines
10031003
10041004 if (xudma_is_pktdma(rx_chn->common.udmax)) {
10051005 /* prepare the channel device as coherent */
1006− rx_chn->common.chan_dev.dma_coherent = true;
1006+ dev_set_dma_coherent(&rx_chn->common.chan_dev);
10071007 dma_coerce_mask_and_coherent(&rx_chn->common.chan_dev,
10081008 DMA_BIT_MASK(48));
10091009 }
@@ -1104,7 +1104,7 @@ k3_udma_glue_request_remote_rx_chn_common(struct k3_udma_glue_rx_channel *rx_chn · 94 unmodified lines
11041104
11051105 if (xudma_is_pktdma(rx_chn->common.udmax)) {
11061106 /* prepare the channel device as coherent */
1107− rx_chn->common.chan_dev.dma_coherent = true;
1107+ dev_set_dma_coherent(&rx_chn->common.chan_dev);
11081108 dma_coerce_mask_and_coherent(&rx_chn->common.chan_dev,
11091109 DMA_BIT_MASK(48));
11101110 rx_chn->single_fdq = false;
drivers/dma/ti/k3-udma.c+3−3
@@ -428,18 +428,18 @@ static void k3_configure_chan_coherency(struct dma_chan *chan, u32 asel) · 427 unmodified lines
428428 /* No special handling for the channel */
429429 chan->dev->chan_dma_dev = false;
430430
431− chan_dev->dma_coherent = false;
431+ dev_clear_dma_coherent(chan_dev);
432432 chan_dev->dma_parms = NULL;
433433 } else if (asel == 14 || asel == 15) {
434434 chan->dev->chan_dma_dev = true;
435435
436− chan_dev->dma_coherent = true;
436+ dev_set_dma_coherent(chan_dev);
437437 dma_coerce_mask_and_coherent(chan_dev, DMA_BIT_MASK(48));
438438 chan_dev->dma_parms = chan_dev->parent->dma_parms;
439439 } else {
440440 dev_warn(chan->device->dev, "Invalid ASEL value: %u\n", asel);
441441
442− chan_dev->dma_coherent = false;
442+ dev_clear_dma_coherent(chan_dev);
443443 chan_dev->dma_parms = NULL;
444444 }
445445 }
drivers/gpu/drm/nova/driver.rs+5−1
@@ -51,9 +51,13 @@ kernel::auxiliary_device_table!( · 50 unmodified lines
5151
5252 impl auxiliary::Driver for NovaDriver {
5353 type IdInfo = ();
54+ type Data<'bound> = Self;
5455 const ID_TABLE: auxiliary::IdTable<Self::IdInfo> = &AUX_TABLE;
5556
56− fn probe(adev: &auxiliary::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> {
57+ fn probe<'bound>(
58+ adev: &'bound auxiliary::Device<Core<'_>>,
59+ _info: &'bound Self::IdInfo,
60+ ) -> impl PinInit<Self, Error> + 'bound {
5761 let data = try_pin_init!(NovaData { adev: adev.into() });
5862
5963 let drm = drm::Device::<Self>::new(adev.as_ref(), data)?;
drivers/gpu/drm/tyr/driver.rs+7−6
@@ -37,7 +37,7 @@ use crate::{ · 36 unmodified lines
3737 regs, //
3838 };
3939
40−pub(crate) type IoMem = kernel::io::mem::IoMem<SZ_2M>;
40+pub(crate) type IoMem = kernel::io::mem::IoMem<'static, SZ_2M>;
4141
4242 pub(crate) struct TyrDrmDriver;
4343
@@ -91,12 +91,13 @@ kernel::of_device_table!( · 47 unmodified lines
9191
9292 impl platform::Driver for TyrPlatformDriverData {
9393 type IdInfo = ();
94+ type Data<'bound> = Self;
9495 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
9596
96− fn probe(
97− pdev: &platform::Device<Core>,
98− _info: Option<&Self::IdInfo>,
99− ) -> impl PinInit<Self, Error> {
97+ fn probe<'bound>(
98+ pdev: &'bound platform::Device<Core<'_>>,
99+ _info: Option<&'bound Self::IdInfo>,
100+ ) -> impl PinInit<Self, Error> + 'bound {
100101 let core_clk = Clk::get(pdev.as_ref(), Some(c"core"))?;
101102 let stacks_clk = OptionalClk::get(pdev.as_ref(), Some(c"stacks"))?;
102103 let coregroup_clk = OptionalClk::get(pdev.as_ref(), Some(c"coregroup"))?;
@@ -109,7 +110,7 @@ impl platform::Driver for TyrPlatformDriverData { · 6 unmodified lines
109110 let sram_regulator = Regulator::<regulator::Enabled>::get(pdev.as_ref(), c"sram")?;
110111
111112 let request = pdev.io_request_by_index(0).ok_or(ENODEV)?;
112− let iomem = Arc::pin_init(request.iomap_sized::<SZ_2M>(), GFP_KERNEL)?;
113+ let iomem = Arc::new(request.iomap_sized::<SZ_2M>()?.into_devres()?, GFP_KERNEL)?;
113114
114115 issue_soft_reset(pdev.as_ref(), &iomem)?;
115116 gpu::l2_power_on(pdev.as_ref(), &iomem)?;
drivers/gpu/nova-core/driver.rs+23−15
@@ -3,7 +3,6 @@ · 2 unmodified lines
33 use kernel::{
44 auxiliary,
55 device::Core,
6− devres::Devres,
76 dma::Device,
87 dma::DmaMask,
98 pci,
@@ -21,6 +20,7 @@ use kernel::{ · 11 unmodified lines
2120 },
2221 Arc,
2322 },
23+ types::ForLt,
2424 };
2525
2626 use crate::gpu::Gpu;
@@ -29,13 +29,15 @@ use crate::gpu::Gpu; · 2 unmodified lines
2929 static AUXILIARY_ID_COUNTER: Atomic<u32> = Atomic::new(0);
3030
3131 #[pin_data]
32−pub(crate) struct NovaCore {
32+pub(crate) struct NovaCore<'bound> {
3333 #[pin]
3434 pub(crate) gpu: Gpu,
35− #[pin]
36− _reg: Devres<auxiliary::Registration>,
35+ #[allow(clippy::type_complexity)]
36+ _reg: auxiliary::Registration<'bound, ForLt!(())>,
3737 }
3838
39+pub(crate) struct NovaCoreDriver;
40+
3941 const BAR0_SIZE: usize = SZ_16M;
4042
4143 // For now we only support Ampere which can use up to 47-bit DMA addresses.
@@ -46,12 +48,12 @@ const BAR0_SIZE: usize = SZ_16M; · 4 unmodified lines
4648 // DMA addresses. These systems should be quite rare.
4749 const GPU_DMA_BITS: u32 = 47;
4850
49−pub(crate) type Bar0 = pci::Bar<BAR0_SIZE>;
51+pub(crate) type Bar0 = pci::Bar<'static, BAR0_SIZE>;
5052
5153 kernel::pci_device_table!(
5254 PCI_TABLE,
5355 MODULE_PCI_TABLE,
54− <NovaCore as pci::Driver>::IdInfo,
56+ <NovaCoreDriver as pci::Driver>::IdInfo,
5557 [
5658 // Modern NVIDIA GPUs will show up as either VGA or 3D controllers.
5759 (
@@ -73,11 +75,15 @@ kernel::pci_device_table!( · 15 unmodified lines
7375 ]
7476 );
7577
76−impl pci::Driver for NovaCore {
78+impl pci::Driver for NovaCoreDriver {
7779 type IdInfo = ();
80+ type Data<'bound> = NovaCore<'bound>;
7881 const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
7982
80− fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> {
83+ fn probe<'bound>(
84+ pdev: &'bound pci::Device<Core<'_>>,
85+ _info: &'bound Self::IdInfo,
86+ ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
8187 pin_init::pin_init_scope(move || {
8288 dev_dbg!(pdev, "Probe Nova Core GPU driver.\n");
8389
@@ -89,26 +95,28 @@ impl pci::Driver for NovaCore { · 5 unmodified lines
8995 // other threads of execution.
9096 unsafe { pdev.dma_set_mask_and_coherent(DmaMask::new::<GPU_DMA_BITS>())? };
9197
92− let bar = Arc::pin_init(
93− pdev.iomap_region_sized::<BAR0_SIZE>(0, c"nova-core/bar0"),
98+ let bar = Arc::new(
99+ pdev.iomap_region_sized::<BAR0_SIZE>(0, c"nova-core/bar0")?
100+ .into_devres()?,
94101 GFP_KERNEL,
95102 )?;
96103
97− Ok(try_pin_init!(Self {
104+ Ok(try_pin_init!(NovaCore {
98105 gpu <- Gpu::new(pdev, bar.clone(), bar.access(pdev.as_ref())?),
99− _reg <- auxiliary::Registration::new(
106+ _reg: auxiliary::Registration::new(
100107 pdev.as_ref(),
101108 c"nova-drm",
102109 // TODO[XARR]: Use XArray or perhaps IDA for proper ID allocation/recycling. For
103110 // now, use a simple atomic counter that never recycles IDs.
104111 AUXILIARY_ID_COUNTER.fetch_add(1, Relaxed),
105− crate::MODULE_NAME
106− ),
112+ crate::MODULE_NAME,
113+ (),
114+ )?,
107115 }))
108116 })
109117 }
110118
111− fn unbind(pdev: &pci::Device<Core>, this: Pin<&Self>) {
119+ fn unbind<'bound>(pdev: &'bound pci::Device<Core<'_>>, this: Pin<&Self::Data<'bound>>) {
112120 this.gpu.unbind(pdev.as_ref());
113121 }
114122 }
drivers/gpu/nova-core/gpu.rs+1−1
@@ -278,7 +278,7 @@ impl Gpu { · 277 unmodified lines
278278 /// Called when the corresponding [`Device`](device::Device) is unbound.
279279 ///
280280 /// Note: This method must only be called from `Driver::unbind`.
281− pub(crate) fn unbind(&self, dev: &device::Device<device::Core>) {
281+ pub(crate) fn unbind(&self, dev: &device::Device<device::Core<'_>>) {
282282 kernel::warn_on!(self
283283 .bar
284284 .access(dev)
drivers/gpu/nova-core/nova_core.rs+1−1
@@ -47,7 +47,7 @@ struct NovaCoreModule { · 46 unmodified lines
4747 // Fields are dropped in declaration order, so `_driver` is dropped first,
4848 // then `_debugfs_guard` clears `DEBUGFS_ROOT`.
4949 #[pin]
50− _driver: Registration<pci::Adapter<driver::NovaCore>>,
50+ _driver: Registration<pci::Adapter<driver::NovaCoreDriver>>,
5151 _debugfs_guard: DebugfsRootGuard,
5252 }
5353
drivers/hv/vmbus_drv.c+10−33
@@ -548,34 +548,6 @@ static ssize_t device_show(struct device *dev, · 547 unmodified lines
548548 }
549549 static DEVICE_ATTR_RO(device);
550550
551−static ssize_t driver_override_store(struct device *dev,
552− struct device_attribute *attr,
553− const char *buf, size_t count)
554−{
555− struct hv_device *hv_dev = device_to_hv_device(dev);
556− int ret;
557−
558− ret = driver_set_override(dev, &hv_dev->driver_override, buf, count);
559− if (ret)
560− return ret;
561−
562− return count;
563−}
564−
565−static ssize_t driver_override_show(struct device *dev,
566− struct device_attribute *attr, char *buf)
567−{
568− struct hv_device *hv_dev = device_to_hv_device(dev);
569− ssize_t len;
570−
571− device_lock(dev);
572− len = sysfs_emit(buf, "%s\n", hv_dev->driver_override);
573− device_unlock(dev);
574−
575− return len;
576−}
577−static DEVICE_ATTR_RW(driver_override);
578−
579551 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
580552 static struct attribute *vmbus_dev_attrs[] = {
581553 &dev_attr_id.attr,
@@ -606,7 +578,6 @@ static struct attribute *vmbus_dev_attrs[] = { · 24 unmodified lines
606578 &dev_attr_channel_vp_mapping.attr,
607579 &dev_attr_vendor.attr,
608580 &dev_attr_device.attr,
609− &dev_attr_driver_override.attr,
610581 NULL,
611582 };
612583
@@ -718,9 +689,11 @@ static const struct hv_vmbus_device_id *hv_vmbus_get_id(const struct hv_driver * · 105 unmodified lines
718689 {
719690 const guid_t *guid = &dev->dev_type;
720691 const struct hv_vmbus_device_id *id;
692+ int ret;
721693
722− /* When driver_override is set, only bind to the matching driver */
723− if (dev->driver_override && strcmp(dev->driver_override, drv->name))
694+ /* If a driver override is set, only bind to the matching driver */
695+ ret = device_match_driver_override(&dev->device, &drv->driver);
696+ if (ret == 0)
724697 return NULL;
725698
726699 /* Look at the dynamic ids first, before the static ones */
@@ -728,8 +701,11 @@ static const struct hv_vmbus_device_id *hv_vmbus_get_id(const struct hv_driver * · 1 unmodified line
728701 if (!id)
729702 id = hv_vmbus_dev_match(drv->id_table, guid);
730703
731− /* driver_override will always match, send a dummy id */
732− if (!id && dev->driver_override)
704+ /*
705+ * If there's a matching driver override, this function should succeed,
706+ * thus return a dummy device ID if no matching ID is found.
707+ */
708+ if (!id && ret > 0)
733709 id = &vmbus_device_null;
734710
735711 return id;
@@ -1031,6 +1007,7 @@ static const struct dev_pm_ops vmbus_pm = { · 295 unmodified lines
10311007 /* The one and only one */
10321008 static const struct bus_type hv_bus = {
10331009 .name = "vmbus",
1010+ .driver_override = true,
10341011 .match = vmbus_match,
10351012 .shutdown = vmbus_shutdown,
10361013 .remove = vmbus_remove,
drivers/hwtracing/coresight/coresight-catu.c+1−1
@@ -706,7 +706,7 @@ static int __init catu_init(void) · 705 unmodified lines
706706 {
707707 int ret;
708708
709− ret = coresight_init_driver("catu", &catu_driver, &catu_platform_driver, THIS_MODULE);
709+ ret = coresight_init_driver("catu", &catu_driver, &catu_platform_driver);
710710 tmc_etr_set_catu_ops(&etr_catu_buf_ops);
711711 return ret;
712712 }
drivers/hwtracing/coresight/coresight-core.c+5−4
@@ -1694,8 +1694,9 @@ static void __exit coresight_exit(void) · 1693 unmodified lines
16941694 module_init(coresight_init);
16951695 module_exit(coresight_exit);
16961696
1697−int coresight_init_driver(const char *drv, struct amba_driver *amba_drv,
1698− struct platform_driver *pdev_drv, struct module *owner)
1697+int coresight_init_driver_with_owner(const char *drv, struct amba_driver *amba_drv,
1698+ struct platform_driver *pdev_drv, struct module *owner,
1699+ const char *mod_name)
16991700 {
17001701 int ret;
17011702
@@ -1705,7 +1706,7 @@ int coresight_init_driver(const char *drv, struct amba_driver *amba_drv, · 3 unmodified lines
17051706 return ret;
17061707 }
17071708
1708− ret = __platform_driver_register(pdev_drv, owner);
1709+ ret = __platform_driver_register(pdev_drv, owner, mod_name);
17091710 if (!ret)
17101711 return 0;
17111712
@@ -1713,7 +1714,7 @@ int coresight_init_driver(const char *drv, struct amba_driver *amba_drv, · 1 unmodified line
17131714 amba_driver_unregister(amba_drv);
17141715 return ret;
17151716 }
1716−EXPORT_SYMBOL_GPL(coresight_init_driver);
1717+EXPORT_SYMBOL_GPL(coresight_init_driver_with_owner);
17171718
17181719 void coresight_remove_driver(struct amba_driver *amba_drv,
17191720 struct platform_driver *pdev_drv)
drivers/hwtracing/coresight/coresight-cpu-debug.c+1−2
@@ -757,8 +757,7 @@ static struct platform_driver debug_platform_driver = { · 756 unmodified lines
757757
758758 static int __init debug_init(void)
759759 {
760− return coresight_init_driver("debug", &debug_driver, &debug_platform_driver,
761− THIS_MODULE);
760+ return coresight_init_driver("debug", &debug_driver, &debug_platform_driver);
762761 }
763762
764763 static void __exit debug_exit(void)
drivers/hwtracing/coresight/coresight-funnel.c+1−2
@@ -412,8 +412,7 @@ static struct amba_driver dynamic_funnel_driver = { · 411 unmodified lines
412412
413413 static int __init funnel_init(void)
414414 {
415− return coresight_init_driver("funnel", &dynamic_funnel_driver, &funnel_driver,
416− THIS_MODULE);
415+ return coresight_init_driver("funnel", &dynamic_funnel_driver, &funnel_driver);
417416 }
418417
419418 static void __exit funnel_exit(void)
drivers/hwtracing/coresight/coresight-replicator.c+1−2
@@ -418,8 +418,7 @@ static struct amba_driver dynamic_replicator_driver = { · 417 unmodified lines
418418
419419 static int __init replicator_init(void)
420420 {
421− return coresight_init_driver("replicator", &dynamic_replicator_driver, &replicator_driver,
422− THIS_MODULE);
421+ return coresight_init_driver("replicator", &dynamic_replicator_driver, &replicator_driver);
423422 }
424423
425424 static void __exit replicator_exit(void)
drivers/hwtracing/coresight/coresight-stm.c+1−1
@@ -1050,7 +1050,7 @@ static struct platform_driver stm_platform_driver = { · 1049 unmodified lines
10501050
10511051 static int __init stm_init(void)
10521052 {
1053− return coresight_init_driver("stm", &stm_driver, &stm_platform_driver, THIS_MODULE);
1053+ return coresight_init_driver("stm", &stm_driver, &stm_platform_driver);
10541054 }
10551055
10561056 static void __exit stm_exit(void)
drivers/hwtracing/coresight/coresight-tmc-core.c+1−1
@@ -1046,7 +1046,7 @@ static struct platform_driver tmc_platform_driver = { · 1045 unmodified lines
10461046
10471047 static int __init tmc_init(void)
10481048 {
1049− return coresight_init_driver("tmc", &tmc_driver, &tmc_platform_driver, THIS_MODULE);
1049+ return coresight_init_driver("tmc", &tmc_driver, &tmc_platform_driver);
10501050 }
10511051
10521052 static void __exit tmc_exit(void)
drivers/hwtracing/coresight/coresight-tnoc.c+1−1
@@ -344,7 +344,7 @@ static struct platform_driver itnoc_driver = { · 343 unmodified lines
344344
345345 static int __init tnoc_init(void)
346346 {
347− return coresight_init_driver("tnoc", &trace_noc_driver, &itnoc_driver, THIS_MODULE);
347+ return coresight_init_driver("tnoc", &trace_noc_driver, &itnoc_driver);
348348 }
349349
350350 static void __exit tnoc_exit(void)
drivers/hwtracing/coresight/coresight-tpdm.c+1−2
@@ -1563,8 +1563,7 @@ static struct platform_driver static_tpdm_driver = { · 1562 unmodified lines
15631563
15641564 static int __init tpdm_init(void)
15651565 {
1566− return coresight_init_driver("tpdm", &dynamic_tpdm_driver, &static_tpdm_driver,
1567− THIS_MODULE);
1566+ return coresight_init_driver("tpdm", &dynamic_tpdm_driver, &static_tpdm_driver);
15681567 }
15691568
15701569 static void __exit tpdm_exit(void)
drivers/hwtracing/coresight/coresight-tpiu.c+1−1
@@ -310,7 +310,7 @@ static struct platform_driver tpiu_platform_driver = { · 309 unmodified lines
310310
311311 static int __init tpiu_init(void)
312312 {
313− return coresight_init_driver("tpiu", &tpiu_driver, &tpiu_platform_driver, THIS_MODULE);
313+ return coresight_init_driver("tpiu", &tpiu_driver, &tpiu_platform_driver);
314314 }
315315
316316 static void __exit tpiu_exit(void)
drivers/i2c/i2c-core-of.c+0−5
@@ -176,11 +176,6 @@ static int of_i2c_notify(struct notifier_block *nb, unsigned long action, · 175 unmodified lines
176176 return NOTIFY_OK;
177177 }
178178
179− /*
180− * Clear the flag before adding the device so that fw_devlink
181− * doesn't skip adding consumers to this device.
182− */
183− fwnode_clear_flag(&rd->dn->fwnode, FWNODE_FLAG_NOT_DEVICE);
184179 client = of_i2c_register_device(adap, rd->dn);
185180 if (IS_ERR(client)) {
186181 dev_err(&adap->dev, "failed to create client for '%pOF'\n",
drivers/iommu/dma-iommu.c+6−3
@@ -2149,18 +2149,21 @@ EXPORT_SYMBOL_GPL(dma_iova_destroy); · 2148 unmodified lines
21492149
21502150 void iommu_setup_dma_ops(struct device *dev, struct iommu_domain *domain)
21512151 {
2152+ bool dma_iommu;
2153+
21522154 if (dev_is_pci(dev))
21532155 dev->iommu->pci_32bit_workaround = !iommu_dma_forcedac;
21542156
2155− dev->dma_iommu = iommu_is_dma_domain(domain);
2156− if (dev->dma_iommu && iommu_dma_init_domain(domain, dev))
2157+ dma_iommu = iommu_is_dma_domain(domain);
2158+ dev_assign_dma_iommu(dev, dma_iommu);
2159+ if (dma_iommu && iommu_dma_init_domain(domain, dev))
21572160 goto out_err;
21582161
21592162 return;
21602163 out_err:
21612164 pr_warn("Failed to set up IOMMU for device %s; retaining platform DMA ops\n",
21622165 dev_name(dev));
2163− dev->dma_iommu = false;
2166+ dev_clear_dma_iommu(dev);
21642167 }
21652168
21662169 static bool has_msi_cookie(const struct iommu_domain *domain)
drivers/iommu/iommu.c+2−3
@@ -611,9 +611,8 @@ static void iommu_deinit_device(struct device *dev) · 610 unmodified lines
611611 dev->iommu_group = NULL;
612612 module_put(ops->owner);
613613 dev_iommu_free(dev);
614−#ifdef CONFIG_IOMMU_DMA
615− dev->dma_iommu = false;
616−#endif
614+ if (IS_ENABLED(CONFIG_IOMMU_DMA))
615+ dev_clear_dma_iommu(dev);
617616 }
618617
619618 static struct iommu_domain *pasid_array_entry_to_domain(void *entry)
drivers/net/pcs/pcs-xpcs-plat.c+1−1
@@ -349,7 +349,7 @@ static int xpcs_plat_init_dev(struct dw_xpcs_plat *pxpcs) · 348 unmodified lines
349349 * up later. Make sure DD-core is aware of the OF-node being re-used.
350350 */
351351 device_set_node(&mdiodev->dev, fwnode_handle_get(dev_fwnode(dev)));
352− mdiodev->dev.of_node_reused = true;
352+ dev_set_of_node_reused(&mdiodev->dev);
353353
354354 /* Pass the data further so the DW XPCS driver core could use it */
355355 mdiodev->dev.platform_data = (void *)device_get_match_data(dev);
drivers/of/device.c+3−3
@@ -26,7 +26,7 @@ · 25 unmodified lines
2626 const struct of_device_id *of_match_device(const struct of_device_id *matches,
2727 const struct device *dev)
2828 {
29− if (!matches || !dev->of_node || dev->of_node_reused)
29+ if (!matches || !dev->of_node || dev_of_node_reused(dev))
3030 return NULL;
3131 return of_match_node(matches, dev->of_node);
3232 }
@@ -192,7 +192,7 @@ ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len) · 159 unmodified lines
192192 {
193193 ssize_t sl;
194194
195− if (!dev || !dev->of_node || dev->of_node_reused)
195+ if (!dev || !dev->of_node || dev_of_node_reused(dev))
196196 return -ENODEV;
197197
198198 sl = of_modalias(dev->of_node, str, len - 2);
@@ -254,7 +254,7 @@ int of_device_uevent_modalias(const struct device *dev, struct kobj_uevent_env * · 55 unmodified lines
254254 {
255255 int sl;
256256
257− if ((!dev) || (!dev->of_node) || dev->of_node_reused)
257+ if ((!dev) || (!dev->of_node) || dev_of_node_reused(dev))
258258 return -ENODEV;
259259
260260 /* Devicetree modalias is tricky, we add it in 2 steps */
drivers/of/dynamic.c+0−1
@@ -225,7 +225,6 @@ static void __of_attach_node(struct device_node *np) · 224 unmodified lines
225225 np->sibling = np->parent->child;
226226 np->parent->child = np;
227227 of_node_clear_flag(np, OF_DETACHED);
228− fwnode_set_flag(&np->fwnode, FWNODE_FLAG_NOT_DEVICE);
229228
230229 raw_spin_unlock_irqrestore(&devtree_lock, flags);
231230
drivers/of/overlay.c+15−0
@@ -185,6 +185,15 @@ static int overlay_notify(struct overlay_changeset *ovcs, · 184 unmodified lines
185185 return 0;
186186 }
187187
188+static void overlay_fw_devlink_refresh(struct overlay_changeset *ovcs)
189+{
190+ for (int i = 0; i < ovcs->count; i++) {
191+ struct device_node *np = ovcs->fragments[i].target;
192+
193+ fw_devlink_refresh_fwnode(of_fwnode_handle(np));
194+ }
195+}
196+
188197 /*
189198 * The values of properties in the "/__symbols__" node are paths in
190199 * the ovcs->overlay_root. When duplicating the properties, the paths
@@ -951,6 +960,12 @@ static int of_overlay_apply(struct overlay_changeset *ovcs, · 760 unmodified lines
951960 pr_err("overlay apply changeset entry notify error %d\n", ret);
952961 /* notify failure is not fatal, continue */
953962
963+ /*
964+ * Needs to happen after changeset notify to give the listeners a chance
965+ * to finish creating all the devices they need to create.
966+ */
967+ overlay_fw_devlink_refresh(ovcs);
968+
954969 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
955970 if (ret_tmp)
956971 if (!ret)
drivers/of/platform.c+0−5
@@ -744,11 +744,6 @@ static int of_platform_notify(struct notifier_block *nb, · 743 unmodified lines
744744 if (of_node_check_flag(rd->dn, OF_POPULATED))
745745 return NOTIFY_OK;
746746
747− /*
748− * Clear the flag before adding the device so that fw_devlink
749− * doesn't skip adding consumers to this device.
750− */
751− fwnode_clear_flag(&rd->dn->fwnode, FWNODE_FLAG_NOT_DEVICE);
752747 /* pdev_parent may be NULL when no bus platform device */
753748 pdev_parent = of_find_device_by_node(parent);
754749 pdev = of_platform_device_create(rd->dn, NULL,
drivers/pci/of.c+1−1
@@ -38,7 +38,7 @@ int pci_set_of_node(struct pci_dev *dev) · 37 unmodified lines
3838 struct device *pdev __free(put_device) =
3939 bus_find_device_by_of_node(&platform_bus_type, node);
4040 if (pdev)
41− dev->bus->dev.of_node_reused = true;
41+ dev_set_of_node_reused(&dev->bus->dev);
4242
4343 device_set_node(&dev->dev, of_fwnode_handle(no_free_ptr(node)));
4444 return 0;
drivers/pci/pwrctrl/core.c+1−1
@@ -39,7 +39,7 @@ static int pci_pwrctrl_notify(struct notifier_block *nb, unsigned long action, · 38 unmodified lines
3939 * If we got here then the PCI device is the second after the
4040 * power control platform device. Mark its OF node as reused.
4141 */
42− dev->of_node_reused = true;
42+ dev_set_of_node_reused(dev);
4343 break;
4444 }
4545
drivers/pwm/pwm_th1520.rs+7−6
@@ -92,7 +92,7 @@ struct Th1520WfHw { · 91 unmodified lines
9292 #[pin_data(PinnedDrop)]
9393 struct Th1520PwmDriverData {
9494 #[pin]
95− iomem: devres::Devres<IoMem<TH1520_PWM_REG_SIZE>>,
95+ iomem: devres::Devres<IoMem<'static, TH1520_PWM_REG_SIZE>>,
9696 clk: Clk,
9797 }
9898
@@ -316,12 +316,13 @@ kernel::of_device_table!( · 217 unmodified lines
316316
317317 impl platform::Driver for Th1520PwmPlatformDriver {
318318 type IdInfo = ();
319+ type Data<'bound> = Self;
319320 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
320321
321− fn probe(
322− pdev: &platform::Device<Core>,
323− _id_info: Option<&Self::IdInfo>,
324− ) -> impl PinInit<Self, Error> {
322+ fn probe<'bound>(
323+ pdev: &'bound platform::Device<Core<'_>>,
324+ _id_info: Option<&'bound Self::IdInfo>,
325+ ) -> impl PinInit<Self, Error> + 'bound {
325326 let dev = pdev.as_ref();
326327 let request = pdev.io_request_by_index(0).ok_or(ENODEV)?;
327328
@@ -351,7 +352,7 @@ impl platform::Driver for Th1520PwmPlatformDriver { · 23 unmodified lines
351352 dev,
352353 TH1520_MAX_PWM_NUM,
353354 try_pin_init!(Th1520PwmDriverData {
354− iomem <- request.iomap_sized::<TH1520_PWM_REG_SIZE>(),
355+ iomem <- request.iomap_sized::<TH1520_PWM_REG_SIZE>()?.into_devres(),
355356 clk <- clk,
356357 }),
357358 )?;
drivers/rpmsg/qcom_glink_native.c+0−2
@@ -1626,7 +1626,6 @@ static void qcom_glink_rpdev_release(struct device *dev) · 1625 unmodified lines
16261626 {
16271627 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
16281628
1629− kfree(rpdev->driver_override);
16301629 kfree(rpdev);
16311630 }
16321631
@@ -1862,7 +1861,6 @@ static void qcom_glink_device_release(struct device *dev) · 229 unmodified lines
18621861
18631862 /* Release qcom_glink_alloc_channel() reference */
18641863 kref_put(&channel->refcount, qcom_glink_channel_release);
1865− kfree(rpdev->driver_override);
18661864 kfree(rpdev);
18671865 }
18681866
drivers/rpmsg/rpmsg_core.c+7−36
@@ -358,33 +358,6 @@ rpmsg_show_attr(src, src, "0x%x\n"); · 357 unmodified lines
358358 rpmsg_show_attr(dst, dst, "0x%x\n");
359359 rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
360360
361−static ssize_t driver_override_store(struct device *dev,
362− struct device_attribute *attr,
363− const char *buf, size_t count)
364−{
365− struct rpmsg_device *rpdev = to_rpmsg_device(dev);
366− int ret;
367−
368− ret = driver_set_override(dev, &rpdev->driver_override, buf, count);
369− if (ret)
370− return ret;
371−
372− return count;
373−}
374−
375−static ssize_t driver_override_show(struct device *dev,
376− struct device_attribute *attr, char *buf)
377−{
378− struct rpmsg_device *rpdev = to_rpmsg_device(dev);
379− ssize_t len;
380−
381− device_lock(dev);
382− len = sysfs_emit(buf, "%s\n", rpdev->driver_override);
383− device_unlock(dev);
384− return len;
385−}
386−static DEVICE_ATTR_RW(driver_override);
387−
388361 static ssize_t modalias_show(struct device *dev,
389362 struct device_attribute *attr, char *buf)
390363 {
@@ -405,7 +378,6 @@ static struct attribute *rpmsg_dev_attrs[] = { · 14 unmodified lines
405378 &dev_attr_dst.attr,
406379 &dev_attr_src.attr,
407380 &dev_attr_announce.attr,
408− &dev_attr_driver_override.attr,
409381 NULL,
410382 };
411383 ATTRIBUTE_GROUPS(rpmsg_dev);
@@ -424,9 +396,11 @@ static int rpmsg_dev_match(struct device *dev, const struct device_driver *drv) · 12 unmodified lines
424396 const struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
425397 const struct rpmsg_device_id *ids = rpdrv->id_table;
426398 unsigned int i;
399+ int ret;
427400
428− if (rpdev->driver_override)
429− return !strcmp(rpdev->driver_override, drv->name);
401+ ret = device_match_driver_override(dev, drv);
402+ if (ret >= 0)
403+ return ret;
430404
431405 if (ids)
432406 for (i = 0; ids[i].name[0]; i++)
@@ -535,6 +509,7 @@ static const struct bus_type rpmsg_bus = { · 102 unmodified lines
535509 .name = "rpmsg",
536510 .match = rpmsg_dev_match,
537511 .dev_groups = rpmsg_dev_groups,
512+ .driver_override = true,
538513 .uevent = rpmsg_uevent,
539514 .probe = rpmsg_dev_probe,
540515 .remove = rpmsg_dev_remove,
@@ -560,11 +535,9 @@ int rpmsg_register_device_override(struct rpmsg_device *rpdev, · 19 unmodified lines
560535
561536 device_initialize(dev);
562537 if (driver_override) {
563− ret = driver_set_override(dev, &rpdev->driver_override,
564− driver_override,
565− strlen(driver_override));
538+ ret = device_set_driver_override(dev, driver_override);
566539 if (ret) {
567− dev_err(dev, "device_set_override failed: %d\n", ret);
540+ dev_err(dev, "device_set_driver_override() failed: %d\n", ret);
568541 put_device(dev);
569542 return ret;
570543 }
@@ -573,8 +546,6 @@ int rpmsg_register_device_override(struct rpmsg_device *rpdev, · 2 unmodified lines
573546 ret = device_add(dev);
574547 if (ret) {
575548 dev_err(dev, "device_add failed: %d\n", ret);
576− kfree(rpdev->driver_override);
577− rpdev->driver_override = NULL;
578549 put_device(dev);
579550 }
580551
drivers/rpmsg/virtio_rpmsg_bus.c+0−1
@@ -374,7 +374,6 @@ static void virtio_rpmsg_release_device(struct device *dev) · 373 unmodified lines
374374 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
375375 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
376376
377− kfree(rpdev->driver_override);
378377 kfree(vch);
379378 }
380379
drivers/soc/tegra/cbb/tegra194-cbb.c+1−1
@@ -2342,7 +2342,7 @@ static int __init tegra194_cbb_init(void) · 2341 unmodified lines
23422342 {
23432343 return platform_driver_register(&tegra194_cbb_driver);
23442344 }
2345−pure_initcall(tegra194_cbb_init);
2345+core_initcall(tegra194_cbb_init);
23462346
23472347 static void __exit tegra194_cbb_exit(void)
23482348 {
drivers/soc/tegra/cbb/tegra234-cbb.c+1−1
@@ -1774,7 +1774,7 @@ static int __init tegra234_cbb_init(void) · 1773 unmodified lines
17741774 {
17751775 return platform_driver_register(&tegra234_cbb_driver);
17761776 }
1777−pure_initcall(tegra234_cbb_init);
1777+core_initcall(tegra234_cbb_init);
17781778
17791779 static void __exit tegra234_cbb_exit(void)
17801780 {
drivers/spi/spi.c+0−5
@@ -5003,11 +5003,6 @@ static int of_spi_notify(struct notifier_block *nb, unsigned long action, · 5002 unmodified lines
50035003 return NOTIFY_OK;
50045004 }
50055005
5006− /*
5007− * Clear the flag before adding the device so that fw_devlink
5008− * doesn't skip adding consumers to this device.
5009− */
5010− fwnode_clear_flag(&rd->dn->fwnode, FWNODE_FLAG_NOT_DEVICE);
50115006 spi = of_register_spi_device(ctlr, rd->dn);
50125007 put_device(&ctlr->dev);
50135008
drivers/tty/serial/serial_base_bus.c+1−1
@@ -74,7 +74,7 @@ static int serial_base_device_init(struct uart_port *port, · 73 unmodified lines
7474 dev->parent = parent_dev;
7575 dev->bus = &serial_base_bus_type;
7676 dev->release = release;
77− dev->of_node_reused = true;
77+ dev_set_of_node_reused(dev);
7878
7979 device_set_node(dev, fwnode_handle_get(dev_fwnode(parent_dev)));
8080
drivers/usb/gadget/udc/aspeed-vhub/dev.c+1−1
@@ -593,7 +593,7 @@ int ast_vhub_init_dev(struct ast_vhub *vhub, unsigned int idx) · 592 unmodified lines
593593 d->gadget.max_speed = USB_SPEED_HIGH;
594594 d->gadget.speed = USB_SPEED_UNKNOWN;
595595 d->gadget.dev.of_node = vhub->pdev->dev.of_node;
596− d->gadget.dev.of_node_reused = true;
596+ dev_set_of_node_reused(&d->gadget.dev);
597597
598598 rc = usb_add_gadget_udc(d->port_dev, &d->gadget);
599599 if (rc != 0)
fs/kernfs/dir.c+7−4
@@ -597,10 +597,13 @@ void kernfs_put(struct kernfs_node *kn) · 596 unmodified lines
597597 */
598598 parent = kernfs_parent(kn);
599599
600− WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS,
601− "kernfs_put: %s/%s: released with incorrect active_ref %d\n",
602− parent ? rcu_dereference(parent->name) : "",
603− rcu_dereference(kn->name), atomic_read(&kn->active));
600+ if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS) {
601+ guard(rcu)();
602+ WARN_ONCE(1,
603+ "kernfs_put: %s/%s: released with incorrect active_ref %d\n",
604+ parent ? rcu_dereference(parent->name) : "",
605+ rcu_dereference(kn->name), atomic_read(&kn->active));
606+ }
604607
605608 if (kernfs_type(kn) == KERNFS_LINK)
606609 kernfs_put(kn->symlink.target_kn);
fs/sysfs/file.c+6−3
@@ -70,9 +70,8 @@ static int sysfs_kf_seq_show(struct seq_file *sf, void *v) · 69 unmodified lines
7070 * The code works fine with PAGE_SIZE return but it's likely to
7171 * indicate truncated result or overflow in normal use cases.
7272 */
73− if (count >= (ssize_t)PAGE_SIZE) {
74− printk("fill_read_buffer: %pS returned bad count\n",
75− ops->show);
73+ if (count >= PAGE_SIZE) {
74+ WARN(1, "OOB write or bad count %zd at %pS\n", count, ops->show);
7675 /* Try to struggle along */
7776 count = PAGE_SIZE - 1;
7877 }
@@ -120,6 +119,10 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf, · 41 unmodified lines
120119 len = ops->show(kobj, of->kn->priv, buf);
121120 if (len < 0)
122121 return len;
122+ if (len >= (ssize_t)PAGE_SIZE) {
123+ printk("fill_read_buffer: %pS returned bad count\n", ops->show);
124+ len = PAGE_SIZE - 1;
125+ }
123126 if (pos) {
124127 if (len <= pos)
125128 return 0;
include/acpi/acpi_bus.h+11−0
@@ -189,6 +189,10 @@ struct acpi_driver { · 188 unmodified lines
189189 * -----------
190190 */
191191
192+bool acpi_of_match_device(const struct acpi_device *adev,
193+ const struct of_device_id *of_match_table,
194+ const struct of_device_id **of_id);
195+
192196 /* Status (_STA) */
193197
194198 struct acpi_device_status {
@@ -996,6 +1000,13 @@ int acpi_scan_add_dep(acpi_handle handle, struct acpi_handle_list *dep_devices); · 801 unmodified lines
9961000 u32 arch_acpi_add_auto_dep(acpi_handle handle);
9971001 #else /* CONFIG_ACPI */
9981002
1003+static inline bool acpi_of_match_device(const struct acpi_device *adev,
1004+ const struct of_device_id *of_match_table,
1005+ const struct of_device_id **of_id)
1006+{
1007+ return false;
1008+}
1009+
9991010 static inline int register_acpi_bus_type(void *bus) { return 0; }
10001011 static inline int unregister_acpi_bus_type(void *bus) { return 0; }
10011012
include/linux/amba/bus.h+0−5
@@ -71,11 +71,6 @@ struct amba_device { · 70 unmodified lines
7171 unsigned int cid;
7272 struct amba_cs_uci_id uci;
7373 unsigned int irq[AMBA_NR_IRQS];
74− /*
75− * Driver name to force a match. Do not set directly, because core
76− * frees it. Use driver_set_override() to set or clear it.
77− */
78− const char *driver_override;
7974 };
8075
8176 struct amba_driver {
include/linux/auxiliary_bus.h+4−0
@@ -62,6 +62,9 @@ · 61 unmodified lines
6262 * @sysfs.irqs: irqs xarray contains irq indices which are used by the device,
6363 * @sysfs.lock: Synchronize irq sysfs creation,
6464 * @sysfs.irq_dir_exists: whether "irqs" directory exists,
65+ * @registration_data_rust: private data owned by the registering (parent)
66+ * driver; valid for as long as the device is
67+ * registered with the driver core,
6568 *
6669 * An auxiliary_device represents a part of its parent device's functionality.
6770 * It is given a name that, combined with the registering drivers
@@ -148,6 +151,7 @@ struct auxiliary_device { · 80 unmodified lines
148151 struct mutex lock; /* Synchronize irq sysfs creation */
149152 bool irq_dir_exists;
150153 } sysfs;
154+ void *registration_data_rust;
151155 };
152156
153157 /**
include/linux/cdx/cdx_bus.h+0−4
@@ -137,9 +137,6 @@ struct cdx_controller { · 136 unmodified lines
137137 * @enabled: is this bus enabled
138138 * @msi_dev_id: MSI Device ID associated with CDX device
139139 * @num_msi: Number of MSI's supported by the device
140− * @driver_override: driver name to force a match; do not set directly,
141− * because core frees it; use driver_set_override() to
142− * set or clear it.
143140 * @irqchip_lock: lock to synchronize irq/msi configuration
144141 * @msi_write_pending: MSI write pending for this device
145142 */
@@ -165,7 +162,6 @@ struct cdx_device { · 19 unmodified lines
165162 bool enabled;
166163 u32 msi_dev_id;
167164 u32 num_msi;
168− const char *driver_override;
169165 struct mutex irqchip_lock;
170166 bool msi_write_pending;
171167 };
include/linux/coresight.h+5−2
@@ -691,8 +691,11 @@ coresight_find_output_type(struct coresight_platform_data *pdata, · 690 unmodified lines
691691 enum coresight_dev_type type,
692692 union coresight_dev_subtype subtype);
693693
694−int coresight_init_driver(const char *drv, struct amba_driver *amba_drv,
695− struct platform_driver *pdev_drv, struct module *owner);
694+int coresight_init_driver_with_owner(const char *drv, struct amba_driver *amba_drv,
695+ struct platform_driver *pdev_drv, struct module *owner,
696+ const char *mod_name);
697+#define coresight_init_driver(drv, amba_drv, pdev_drv) \
698+ coresight_init_driver_with_owner(drv, amba_drv, pdev_drv, THIS_MODULE, KBUILD_MODNAME)
696699
697700 void coresight_remove_driver(struct amba_driver *amba_drv,
698701 struct platform_driver *pdev_drv);
include/linux/device.h+136−85
@@ -38,7 +38,6 @@ struct device_private; · 37 unmodified lines
3838 struct device_driver;
3939 struct driver_private;
4040 struct module;
41−struct class;
4241 struct subsys_private;
4342 struct device_node;
4443 struct fwnode_handle;
@@ -104,10 +103,18 @@ struct device_type { · 59 unmodified lines
104103 */
105104 struct device_attribute {
106105 struct attribute attr;
107− ssize_t (*show)(struct device *dev, struct device_attribute *attr,
108− char *buf);
109− ssize_t (*store)(struct device *dev, struct device_attribute *attr,
110− const char *buf, size_t count);
106+ __SYSFS_FUNCTION_ALTERNATIVE(
107+ ssize_t (*show)(struct device *dev, struct device_attribute *attr,
108+ char *buf);
109+ ssize_t (*show_const)(struct device *dev, const struct device_attribute *attr,
110+ char *buf);
111+ );
112+ __SYSFS_FUNCTION_ALTERNATIVE(
113+ ssize_t (*store)(struct device *dev, struct device_attribute *attr,
114+ const char *buf, size_t count);
115+ ssize_t (*store_const)(struct device *dev, const struct device_attribute *attr,
116+ const char *buf, size_t count);
117+ );
111118 };
112119
113120 /**
@@ -135,6 +142,77 @@ ssize_t device_store_bool(struct device *dev, struct device_attribute *attr, · 21 unmodified lines
135142 ssize_t device_show_string(struct device *dev, struct device_attribute *attr,
136143 char *buf);
137144
145+typedef ssize_t __device_show_handler_const(struct device *dev, const struct device_attribute *attr,
146+ char *buf);
147+typedef ssize_t __device_store_handler_const(struct device *dev, const struct device_attribute *attr,
148+ const char *buf, size_t count);
149+
150+#ifdef CONFIG_CFI
151+
152+#define __DEVICE_ATTR_SHOW_STORE(_show, _store) \
153+ .show = _Generic(_show, \
154+ __device_show_handler_const * : NULL, \
155+ default : _show \
156+ ), \
157+ .show_const = _Generic(_show, \
158+ __device_show_handler_const * : _show, \
159+ default : NULL \
160+ ), \
161+ .store = _Generic(_store, \
162+ __device_store_handler_const * : NULL, \
163+ default : _store \
164+ ), \
165+ .store_const = _Generic(_store, \
166+ __device_store_handler_const * : _store, \
167+ default : NULL \
168+ ),
169+
170+#else
171+
172+#define __DEVICE_ATTR_SHOW_STORE(_show, _store) \
173+ .show = _Generic(_show, \
174+ __device_show_handler_const * : (void *)_show, \
175+ default : _show \
176+ ), \
177+ .store = _Generic(_store, \
178+ __device_store_handler_const * : (void *)_store, \
179+ default : _store \
180+ ), \
181+
182+#endif
183+
184+
185+#define __DEVICE_ATTR(_name, _mode, _show, _store) { \
186+ .attr = {.name = __stringify(_name), \
187+ .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
188+ __DEVICE_ATTR_SHOW_STORE(_show, _store) \
189+}
190+
191+#define __DEVICE_ATTR_RO_MODE(_name, _mode) \
192+ __DEVICE_ATTR(_name, _mode, _name##_show, NULL)
193+
194+#define __DEVICE_ATTR_RO(_name) \
195+ __DEVICE_ATTR_RO_MODE(_name, 0444)
196+
197+#define __DEVICE_ATTR_WO(_name) \
198+ __DEVICE_ATTR(_name, 0200, NULL, _name##_store)
199+
200+#define __DEVICE_ATTR_RW_MODE(_name, _mode) \
201+ __DEVICE_ATTR(_name, _mode, _name##_show, _name##_store)
202+
203+#define __DEVICE_ATTR_RW(_name) \
204+ __DEVICE_ATTR_RW_MODE(_name, 0644)
205+
206+#ifdef CONFIG_DEBUG_LOCK_ALLOC
207+#define __DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) { \
208+ .attr = {.name = __stringify(_name), .mode = _mode, \
209+ .ignore_lockdep = true }, \
210+ __DEVICE_ATTR_SHOW_STORE(_show, _store) \
211+}
212+#else
213+#define __DEVICE_ATTR_IGNORE_LOCKDEP __DEVICE_ATTR
214+#endif
215+
138216 /**
139217 * DEVICE_ATTR - Define a device attribute.
140218 * @_name: Attribute name.
@@ -155,20 +233,7 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr, · 14 unmodified lines
155233 * };
156234 */
157235 #define DEVICE_ATTR(_name, _mode, _show, _store) \
158− struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
159−
160−/**
161− * DEVICE_ATTR_PREALLOC - Define a preallocated device attribute.
162− * @_name: Attribute name.
163− * @_mode: File mode.
164− * @_show: Show handler. Optional, but mandatory if attribute is readable.
165− * @_store: Store handler. Optional, but mandatory if attribute is writable.
166− *
167− * Like DEVICE_ATTR(), but ``SYSFS_PREALLOC`` is set on @_mode.
168− */
169−#define DEVICE_ATTR_PREALLOC(_name, _mode, _show, _store) \
170− struct device_attribute dev_attr_##_name = \
171− __ATTR_PREALLOC(_name, _mode, _show, _store)
236+ struct device_attribute dev_attr_##_name = __DEVICE_ATTR(_name, _mode, _show, _store)
172237
173238 /**
174239 * DEVICE_ATTR_RW - Define a read-write device attribute.
@@ -178,7 +243,7 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr, · 3 unmodified lines
178243 * and @_store is <_name>_store.
179244 */
180245 #define DEVICE_ATTR_RW(_name) \
181− struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
246+ struct device_attribute dev_attr_##_name = __DEVICE_ATTR_RW(_name)
182247
183248 /**
184249 * DEVICE_ATTR_ADMIN_RW - Define an admin-only read-write device attribute.
@@ -187,7 +252,7 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr, · 2 unmodified lines
187252 * Like DEVICE_ATTR_RW(), but @_mode is 0600.
188253 */
189254 #define DEVICE_ATTR_ADMIN_RW(_name) \
190− struct device_attribute dev_attr_##_name = __ATTR_RW_MODE(_name, 0600)
255+ struct device_attribute dev_attr_##_name = __DEVICE_ATTR_RW_MODE(_name, 0600)
191256
192257 /**
193258 * DEVICE_ATTR_RW_NAMED - Define a read-write device attribute with a sysfs name
@@ -201,8 +266,7 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr, · 7 unmodified lines
201266 #define DEVICE_ATTR_RW_NAMED(_name, _attrname) \
202267 struct device_attribute dev_attr_##_name = { \
203268 .attr = { .name = _attrname, .mode = 0644 }, \
204− .show = _name##_show, \
205− .store = _name##_store, \
269+ __DEVICE_ATTR_SHOW_STORE(_name##_show, _name##_store) \
206270 }
207271
208272 /**
@@ -212,7 +276,7 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr, · 3 unmodified lines
212276 * Like DEVICE_ATTR(), but @_mode is 0444 and @_show is <_name>_show.
213277 */
214278 #define DEVICE_ATTR_RO(_name) \
215− struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
279+ struct device_attribute dev_attr_##_name = __DEVICE_ATTR_RO(_name)
216280
217281 /**
218282 * DEVICE_ATTR_ADMIN_RO - Define an admin-only readable device attribute.
@@ -221,7 +285,7 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr, · 2 unmodified lines
221285 * Like DEVICE_ATTR_RO(), but @_mode is 0400.
222286 */
223287 #define DEVICE_ATTR_ADMIN_RO(_name) \
224− struct device_attribute dev_attr_##_name = __ATTR_RO_MODE(_name, 0400)
288+ struct device_attribute dev_attr_##_name = __DEVICE_ATTR_RO_MODE(_name, 0400)
225289
226290 /**
227291 * DEVICE_ATTR_RO_NAMED - Define a read-only device attribute with a sysfs name
@@ -235,7 +299,7 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr, · 7 unmodified lines
235299 #define DEVICE_ATTR_RO_NAMED(_name, _attrname) \
236300 struct device_attribute dev_attr_##_name = { \
237301 .attr = { .name = _attrname, .mode = 0444 }, \
238− .show = _name##_show, \
302+ __DEVICE_ATTR_SHOW_STORE(_name##_show, NULL) \
239303 }
240304
241305 /**
@@ -245,7 +309,7 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr, · 3 unmodified lines
245309 * Like DEVICE_ATTR(), but @_mode is 0200 and @_store is <_name>_store.
246310 */
247311 #define DEVICE_ATTR_WO(_name) \
248− struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
312+ struct device_attribute dev_attr_##_name = __DEVICE_ATTR_WO(_name)
249313
250314 /**
251315 * DEVICE_ATTR_WO_NAMED - Define a read-only device attribute with a sysfs name
@@ -259,7 +323,7 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr, · 7 unmodified lines
259323 #define DEVICE_ATTR_WO_NAMED(_name, _attrname) \
260324 struct device_attribute dev_attr_##_name = { \
261325 .attr = { .name = _attrname, .mode = 0200 }, \
262− .store = _name##_store, \
326+ __DEVICE_ATTR_SHOW_STORE(NULL, _name##_store) \
263327 }
264328
265329 /**
@@ -273,7 +337,7 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr, · 7 unmodified lines
273337 */
274338 #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
275339 struct dev_ext_attribute dev_attr_##_name = \
276− { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
340+ { __DEVICE_ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
277341
278342 /**
279343 * DEVICE_INT_ATTR - Define a device attribute backed by an int.
@@ -285,7 +349,7 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr, · 5 unmodified lines
285349 */
286350 #define DEVICE_INT_ATTR(_name, _mode, _var) \
287351 struct dev_ext_attribute dev_attr_##_name = \
288− { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
352+ { __DEVICE_ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
289353
290354 /**
291355 * DEVICE_BOOL_ATTR - Define a device attribute backed by a bool.
@@ -297,7 +361,7 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr, · 5 unmodified lines
297361 */
298362 #define DEVICE_BOOL_ATTR(_name, _mode, _var) \
299363 struct dev_ext_attribute dev_attr_##_name = \
300− { __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
364+ { __DEVICE_ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
301365
302366 /**
303367 * DEVICE_STRING_ATTR_RO - Define a device attribute backed by a r/o string.
@@ -310,11 +374,11 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr, · 6 unmodified lines
310374 */
311375 #define DEVICE_STRING_ATTR_RO(_name, _mode, _var) \
312376 struct dev_ext_attribute dev_attr_##_name = \
313− { __ATTR(_name, (_mode) & ~0222, device_show_string, NULL), (_var) }
377+ { __DEVICE_ATTR(_name, (_mode) & ~0222, device_show_string, NULL), (_var) }
314378
315379 #define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
316380 struct device_attribute dev_attr_##_name = \
317− __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
381+ __DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
318382
319383 int device_create_file(struct device *device,
320384 const struct device_attribute *entry);
@@ -512,10 +576,40 @@ struct device_physical_location { · 191 unmodified lines
512576 *
513577 * @DEV_FLAG_READY_TO_PROBE: If set then device_add() has finished enough
514578 * initialization that probe could be called.
579+ * @DEV_FLAG_CAN_MATCH: The device has matched with a driver at least once or it
580+ * is in a bus (like AMBA) which can't check for matching drivers
581+ * until other devices probe successfully.
582+ * @DEV_FLAG_DMA_IOMMU: Device is using default IOMMU implementation for DMA and
583+ * doesn't rely on dma_ops structure.
584+ * @DEV_FLAG_DMA_SKIP_SYNC: DMA sync operations can be skipped for coherent
585+ * buffers.
586+ * @DEV_FLAG_DMA_OPS_BYPASS: If set then the dma_ops are bypassed for the
587+ * streaming DMA operations (->map_* / ->unmap_* / ->sync_*), and
588+ * optional (if the coherent mask is large enough) also for dma
589+ * allocations. This flag is managed by the dma ops instance from
590+ * ->dma_supported.
591+ * @DEV_FLAG_STATE_SYNCED: The hardware state of this device has been synced to
592+ * match the software state of this device by calling the
593+ * driver/bus sync_state() callback.
594+ * @DEV_FLAG_DMA_COHERENT: This particular device is dma coherent, even if the
595+ * architecture supports non-coherent devices.
596+ * @DEV_FLAG_OF_NODE_REUSED: Set if the device-tree node is shared with an
597+ * ancestor device.
598+ * @DEV_FLAG_OFFLINE_DISABLED: If set, the device is permanently online.
599+ * @DEV_FLAG_OFFLINE: Set after successful invocation of bus type's .offline().
515600 * @DEV_FLAG_COUNT: Number of defined struct_device_flags.
516601 */
517602 enum struct_device_flags {
518603 DEV_FLAG_READY_TO_PROBE = 0,
604+ DEV_FLAG_CAN_MATCH = 1,
605+ DEV_FLAG_DMA_IOMMU = 2,
606+ DEV_FLAG_DMA_SKIP_SYNC = 3,
607+ DEV_FLAG_DMA_OPS_BYPASS = 4,
608+ DEV_FLAG_STATE_SYNCED = 5,
609+ DEV_FLAG_DMA_COHERENT = 6,
610+ DEV_FLAG_OF_NODE_REUSED = 7,
611+ DEV_FLAG_OFFLINE_DISABLED = 8,
612+ DEV_FLAG_OFFLINE = 9,
519613
520614 DEV_FLAG_COUNT
521615 };
@@ -594,27 +688,6 @@ enum struct_device_flags { · 72 unmodified lines
594688 * @removable: Whether the device can be removed from the system. This
595689 * should be set by the subsystem / bus driver that discovered
596690 * the device.
597− *
598− * @offline_disabled: If set, the device is permanently online.
599− * @offline: Set after successful invocation of bus type's .offline().
600− * @of_node_reused: Set if the device-tree node is shared with an ancestor
601− * device.
602− * @state_synced: The hardware state of this device has been synced to match
603− * the software state of this device by calling the driver/bus
604− * sync_state() callback.
605− * @can_match: The device has matched with a driver at least once or it is in
606− * a bus (like AMBA) which can't check for matching drivers until
607− * other devices probe successfully.
608− * @dma_coherent: this particular device is dma coherent, even if the
609− * architecture supports non-coherent devices.
610− * @dma_ops_bypass: If set to %true then the dma_ops are bypassed for the
611− * streaming DMA operations (->map_* / ->unmap_* / ->sync_*),
612− * and optionall (if the coherent mask is large enough) also
613− * for dma allocations. This flag is managed by the dma ops
614− * instance from ->dma_supported.
615− * @dma_skip_sync: DMA sync operations can be skipped for coherent buffers.
616− * @dma_iommu: Device is using default IOMMU implementation for DMA and
617− * doesn't rely on dma_ops structure.
618691 * @flags: DEV_FLAG_XXX flags. Use atomic bitfield operations to modify.
619692 *
620693 * At the lowest level, every device in a Linux system is represented by an
@@ -719,26 +792,6 @@ struct device { · 98 unmodified lines
719792
720793 enum device_removable removable;
721794
722− bool offline_disabled:1;
723− bool offline:1;
724− bool of_node_reused:1;
725− bool state_synced:1;
726− bool can_match:1;
727−#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
728− defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
729− defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
730− bool dma_coherent:1;
731−#endif
732−#ifdef CONFIG_DMA_OPS_BYPASS
733− bool dma_ops_bypass : 1;
734−#endif
735−#ifdef CONFIG_DMA_NEED_SYNC
736− bool dma_skip_sync:1;
737−#endif
738−#ifdef CONFIG_IOMMU_DMA
739− bool dma_iommu:1;
740−#endif
741−
742795 DECLARE_BITMAP(flags, DEV_FLAG_COUNT);
743796 };
744797
@@ -765,6 +818,15 @@ static inline bool dev_test_and_set_##accessor_name(struct device *dev) \ · 20 unmodified lines
765818 }
766819
767820 __create_dev_flag_accessors(ready_to_probe, DEV_FLAG_READY_TO_PROBE);
821+__create_dev_flag_accessors(can_match, DEV_FLAG_CAN_MATCH);
822+__create_dev_flag_accessors(dma_iommu, DEV_FLAG_DMA_IOMMU);
823+__create_dev_flag_accessors(dma_skip_sync, DEV_FLAG_DMA_SKIP_SYNC);
824+__create_dev_flag_accessors(dma_ops_bypass, DEV_FLAG_DMA_OPS_BYPASS);
825+__create_dev_flag_accessors(state_synced, DEV_FLAG_STATE_SYNCED);
826+__create_dev_flag_accessors(dma_coherent, DEV_FLAG_DMA_COHERENT);
827+__create_dev_flag_accessors(of_node_reused, DEV_FLAG_OF_NODE_REUSED);
828+__create_dev_flag_accessors(offline_disabled, DEV_FLAG_OFFLINE_DISABLED);
829+__create_dev_flag_accessors(offline, DEV_FLAG_OFFLINE);
768830
769831 #undef __create_dev_flag_accessors
770832
@@ -1063,17 +1125,6 @@ static inline void device_lock_assert(struct device *dev) · 292 unmodified lines
10631125 lockdep_assert_held(&dev->mutex);
10641126 }
10651127
1066−static inline bool dev_has_sync_state(struct device *dev)
1067−{
1068− if (!dev)
1069− return false;
1070− if (dev->driver && dev->driver->sync_state)
1071− return true;
1072− if (dev->bus && dev->bus->sync_state)
1073− return true;
1074− return false;
1075−}
1076−
10771128 static inline int dev_set_drv_sync_state(struct device *dev,
10781129 void (*fn)(struct device *dev))
10791130 {
include/linux/device/bus.h+3−3
@@ -83,9 +83,9 @@ struct fwnode_handle; · 82 unmodified lines
8383 struct bus_type {
8484 const char *name;
8585 const char *dev_name;
86− const struct attribute_group **bus_groups;
87− const struct attribute_group **dev_groups;
88− const struct attribute_group **drv_groups;
86+ const struct attribute_group *const *bus_groups;
87+ const struct attribute_group *const *dev_groups;
88+ const struct attribute_group *const *drv_groups;
8989
9090 int (*match)(struct device *dev, const struct device_driver *drv);
9191 int (*uevent)(const struct device *dev, struct kobj_uevent_env *env);
include/linux/device/class.h+1−1
@@ -34,7 +34,7 @@ struct fwnode_handle; · 33 unmodified lines
3434 * @class_release: Called to release this class.
3535 * @dev_release: Called to release the device.
3636 * @shutdown_pre: Called at shut-down time before driver shutdown.
37− * @ns_type: Callbacks so sysfs can detemine namespaces.
37+ * @ns_type: Callbacks so sysfs can determine namespaces.
3838 * @namespace: Namespace of the device belongs to this class.
3939 * @get_ownership: Allows class to specify uid/gid of the sysfs directories
4040 * for the devices belonging to the class. Usually tied to
include/linux/device/driver.h+4−6
@@ -114,8 +114,8 @@ struct device_driver { · 113 unmodified lines
114114 void (*shutdown) (struct device *dev);
115115 int (*suspend) (struct device *dev, pm_message_t state);
116116 int (*resume) (struct device *dev);
117− const struct attribute_group **groups;
118− const struct attribute_group **dev_groups;
117+ const struct attribute_group *const *groups;
118+ const struct attribute_group *const *dev_groups;
119119
120120 const struct dev_pm_ops *pm;
121121 void (*coredump) (struct device *dev);
@@ -123,8 +123,8 @@ struct device_driver { · 1 unmodified line
123123 struct driver_private *p;
124124 struct {
125125 /*
126− * Called after remove() and after all devres entries have been
127− * processed. This is a Rust only callback.
126+ * Called after remove() but before devres entries are released.
127+ * This is a Rust only callback.
128128 */
129129 void (*post_unbind_rust)(struct device *dev);
130130 } p_cb;
@@ -160,8 +160,6 @@ int __must_check driver_create_file(const struct device_driver *driver, · 29 unmodified lines
160160 void driver_remove_file(const struct device_driver *driver,
161161 const struct driver_attribute *attr);
162162
163−int driver_set_override(struct device *dev, const char **override,
164− const char *s, size_t len);
165163 int __must_check driver_for_each_device(struct device_driver *drv, struct device *start,
166164 void *data, device_iter_t fn);
167165 struct device *driver_find_device(const struct device_driver *drv,
include/linux/dma-map-ops.h+3−3
@@ -225,7 +225,7 @@ int dma_direct_set_offset(struct device *dev, phys_addr_t cpu_start, · 224 unmodified lines
225225 extern bool dma_default_coherent;
226226 static inline bool dev_is_dma_coherent(struct device *dev)
227227 {
228− return dev->dma_coherent;
228+ return dev_dma_coherent(dev);
229229 }
230230 #else
231231 #define dma_default_coherent true
@@ -240,8 +240,8 @@ static inline void dma_reset_need_sync(struct device *dev) · 8 unmodified lines
240240 {
241241 #ifdef CONFIG_DMA_NEED_SYNC
242242 /* Reset it only once so that the function can be called on hotpath */
243− if (unlikely(dev->dma_skip_sync))
244− dev->dma_skip_sync = false;
243+ if (unlikely(dev_dma_skip_sync(dev)))
244+ dev_clear_dma_skip_sync(dev);
245245 #endif
246246 }
247247
include/linux/dma-mapping.h+1−1
@@ -429,7 +429,7 @@ bool __dma_need_sync(struct device *dev, dma_addr_t dma_addr); · 428 unmodified lines
429429 static inline bool dma_dev_need_sync(const struct device *dev)
430430 {
431431 /* Always call DMA sync operations when debugging is enabled */
432− return !dev->dma_skip_sync || IS_ENABLED(CONFIG_DMA_API_DEBUG);
432+ return !dev_dma_skip_sync(dev) || IS_ENABLED(CONFIG_DMA_API_DEBUG);
433433 }
434434
435435 static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
include/linux/fwnode.h+3−0
@@ -210,8 +210,10 @@ static inline void fwnode_init(struct fwnode_handle *fwnode, · 209 unmodified lines
210210 {
211211 fwnode->secondary = NULL;
212212 fwnode->ops = ops;
213+ fwnode->dev = NULL;
213214 INIT_LIST_HEAD(&fwnode->consumers);
214215 INIT_LIST_HEAD(&fwnode->suppliers);
216+ fwnode->flags = 0;
215217 }
216218
217219 static inline void fwnode_set_flag(struct fwnode_handle *fwnode,
@@ -251,6 +253,7 @@ int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup, · 33 unmodified lines
251253 u8 flags);
252254 void fwnode_links_purge(struct fwnode_handle *fwnode);
253255 void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode);
256+void fw_devlink_refresh_fwnode(struct fwnode_handle *fwnode);
254257 bool fw_devlink_is_strict(void);
255258
256259 #endif
include/linux/hyperv.h+0−5
@@ -1272,11 +1272,6 @@ struct hv_device { · 1271 unmodified lines
12721272 u16 device_id;
12731273
12741274 struct device device;
1275− /*
1276− * Driver name to force a match. Do not set directly, because core
1277− * frees it. Use driver_set_override() to set or clear it.
1278− */
1279− const char *driver_override;
12801275
12811276 struct vmbus_channel *channel;
12821277 struct kset *channels_kset;
include/linux/iommu-dma.h+2−1
@@ -7,12 +7,13 @@ · 6 unmodified lines
77 #ifndef _LINUX_IOMMU_DMA_H
88 #define _LINUX_IOMMU_DMA_H
99
10+#include <linux/device.h>
1011 #include <linux/dma-direction.h>
1112
1213 #ifdef CONFIG_IOMMU_DMA
1314 static inline bool use_dma_iommu(struct device *dev)
1415 {
15− return dev->dma_iommu;
16+ return dev_dma_iommu(dev);
1617 }
1718 #else
1819 static inline bool use_dma_iommu(struct device *dev)
include/linux/platform_device.h+9−8
@@ -293,18 +293,19 @@ struct platform_driver { · 292 unmodified lines
293293 * use a macro to avoid include chaining to get THIS_MODULE
294294 */
295295 #define platform_driver_register(drv) \
296− __platform_driver_register(drv, THIS_MODULE)
296+ __platform_driver_register(drv, THIS_MODULE, KBUILD_MODNAME)
297297 extern int __platform_driver_register(struct platform_driver *,
298− struct module *);
298+ struct module *, const char *mod_name);
299299 extern void platform_driver_unregister(struct platform_driver *);
300300
301301 /* non-hotpluggable platform devices may use this so that probe() and
302302 * its support may live in __init sections, conserving runtime memory.
303303 */
304304 #define platform_driver_probe(drv, probe) \
305− __platform_driver_probe(drv, probe, THIS_MODULE)
305+ __platform_driver_probe(drv, probe, THIS_MODULE, KBUILD_MODNAME)
306306 extern int __platform_driver_probe(struct platform_driver *driver,
307− int (*probe)(struct platform_device *), struct module *module);
307+ int (*probe)(struct platform_device *), struct module *module,
308+ const char *mod_name);
308309
309310 static inline void *platform_get_drvdata(const struct platform_device *pdev)
310311 {
@@ -368,19 +369,19 @@ static int __init __platform_driver##_init(void) \ · 57 unmodified lines
368369 device_initcall(__platform_driver##_init); \
369370
370371 #define platform_create_bundle(driver, probe, res, n_res, data, size) \
371− __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
372+ __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE, KBUILD_MODNAME)
372373 extern struct platform_device *__platform_create_bundle(
373374 struct platform_driver *driver, int (*probe)(struct platform_device *),
374375 struct resource *res, unsigned int n_res,
375− const void *data, size_t size, struct module *module);
376+ const void *data, size_t size, struct module *module, const char *mod_name);
376377
377378 int __platform_register_drivers(struct platform_driver * const *drivers,
378− unsigned int count, struct module *owner);
379+ unsigned int count, struct module *owner, const char *mod_name);
379380 void platform_unregister_drivers(struct platform_driver * const *drivers,
380381 unsigned int count);
381382
382383 #define platform_register_drivers(drivers, count) \
383− __platform_register_drivers(drivers, count, THIS_MODULE)
384+ __platform_register_drivers(drivers, count, THIS_MODULE, KBUILD_MODNAME)
384385
385386 #ifdef CONFIG_SUSPEND
386387 extern int platform_pm_suspend(struct device *dev);
include/linux/property.h+7−1
@@ -471,12 +471,18 @@ struct property_entry { · 470 unmodified lines
471471 #define PROPERTY_ENTRY_STRING(_name_, _val_) \
472472 __PROPERTY_ENTRY_ELEMENT(_name_, str, STRING, _val_)
473473
474+#define __PROPERTY_ENTRY_REF_ARGS(_ref_, ...) \
475+ _Generic(_ref_, \
476+ const struct software_node_ref_args *: _ref_, \
477+ struct software_node_ref_args *: _ref_, \
478+ default: &SOFTWARE_NODE_REFERENCE(_ref_, ##__VA_ARGS__))
479+
474480 #define PROPERTY_ENTRY_REF(_name_, _ref_, ...) \
475481 (struct property_entry) { \
476482 .name = _name_, \
477483 .length = sizeof(struct software_node_ref_args), \
478484 .type = DEV_PROP_REF, \
479− { .pointer = &SOFTWARE_NODE_REFERENCE(_ref_, ##__VA_ARGS__), }, \
485+ { .pointer = __PROPERTY_ENTRY_REF_ARGS(_ref_, ##__VA_ARGS__) }, \
480486 }
481487
482488 #define PROPERTY_ENTRY_BOOL(_name_) \
include/linux/rpmsg.h+0−4
@@ -41,9 +41,6 @@ struct rpmsg_channel_info { · 40 unmodified lines
4141 * rpmsg_device - device that belong to the rpmsg bus
4242 * @dev: the device struct
4343 * @id: device id (used to match between rpmsg drivers and devices)
44− * @driver_override: driver name to force a match; do not set directly,
45− * because core frees it; use driver_set_override() to
46− * set or clear it.
4744 * @src: local address
4845 * @dst: destination address
4946 * @ept: the rpmsg endpoint of this channel
@@ -53,7 +50,6 @@ struct rpmsg_channel_info { · 3 unmodified lines
5350 struct rpmsg_device {
5451 struct device dev;
5552 struct rpmsg_device_id id;
56− const char *driver_override;
5753 u32 src;
5854 u32 dst;
5955 struct rpmsg_endpoint *ept;
kernel/cpu.c+2−2
@@ -2639,7 +2639,7 @@ static void cpuhp_offline_cpu_device(unsigned int cpu) · 2638 unmodified lines
26392639 {
26402640 struct device *dev = get_cpu_device(cpu);
26412641
2642− dev->offline = true;
2642+ dev_set_offline(dev);
26432643 /* Tell user space about the state change */
26442644 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
26452645 }
@@ -2648,7 +2648,7 @@ static void cpuhp_online_cpu_device(unsigned int cpu) · 2 unmodified lines
26482648 {
26492649 struct device *dev = get_cpu_device(cpu);
26502650
2651− dev->offline = false;
2651+ dev_clear_offline(dev);
26522652 /* Tell user space about the state change */
26532653 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
26542654 }
kernel/dma/mapping.c+5−7
@@ -126,11 +126,9 @@ static bool dma_go_direct(struct device *dev, dma_addr_t mask, · 125 unmodified lines
126126 if (likely(!ops))
127127 return true;
128128
129−#ifdef CONFIG_DMA_OPS_BYPASS
130− if (dev->dma_ops_bypass)
129+ if (IS_ENABLED(CONFIG_DMA_OPS_BYPASS) && dev_dma_ops_bypass(dev))
131130 return min_not_zero(mask, dev->bus_dma_limit) >=
132131 dma_direct_get_required_mask(dev);
133−#endif
134132 return false;
135133 }
136134
@@ -472,7 +470,7 @@ bool dma_need_unmap(struct device *dev) · 335 unmodified lines
472470 {
473471 if (!dma_map_direct(dev, get_dma_ops(dev)))
474472 return true;
475− if (!dev->dma_skip_sync)
473+ if (!dev_dma_skip_sync(dev))
476474 return true;
477475 return IS_ENABLED(CONFIG_DMA_API_DEBUG);
478476 }
@@ -488,16 +486,16 @@ static void dma_setup_need_sync(struct device *dev) · 9 unmodified lines
488486 * mapping, if any. During the device initialization, it's
489487 * enough to check only for the DMA coherence.
490488 */
491− dev->dma_skip_sync = dev_is_dma_coherent(dev);
489+ dev_assign_dma_skip_sync(dev, dev_is_dma_coherent(dev));
492490 else if (!ops->sync_single_for_device && !ops->sync_single_for_cpu &&
493491 !ops->sync_sg_for_device && !ops->sync_sg_for_cpu)
494492 /*
495493 * Synchronization is not possible when none of DMA sync ops
496494 * is set.
497495 */
498− dev->dma_skip_sync = true;
496+ dev_set_dma_skip_sync(dev);
499497 else
500− dev->dma_skip_sync = false;
498+ dev_clear_dma_skip_sync(dev);
501499 }
502500 #else /* !CONFIG_DMA_NEED_SYNC */
503501 static inline void dma_setup_need_sync(struct device *dev) { }
kernel/params.c+4−4
@@ -942,9 +942,9 @@ const struct kobj_type module_ktype = { · 941 unmodified lines
942942 /*
943943 * param_sysfs_init - create "module" kset
944944 *
945− * This must be done before the initramfs is unpacked and
946− * request_module() thus becomes possible, because otherwise the
947− * module load would fail in mod_sysfs_init.
945+ * This must be done before any driver registration so that when a driver comes
946+ * from a built-in module, the driver core can add the module under /sys/module
947+ * and create the associated driver symlinks.
948948 */
949949 static int __init param_sysfs_init(void)
950950 {
@@ -957,7 +957,7 @@ static int __init param_sysfs_init(void) · 6 unmodified lines
957957
958958 return 0;
959959 }
960−subsys_initcall(param_sysfs_init);
960+pure_initcall(param_sysfs_init);
961961
962962 /*
963963 * param_sysfs_builtin_init - add sysfs version and parameter
mm/hmm.c+1−1
@@ -709,7 +709,7 @@ int hmm_dma_map_alloc(struct device *dev, struct hmm_dma_map *map, · 708 unmodified lines
709709 * best approximation to ensure no swiotlb buffering happens.
710710 */
711711 #ifdef CONFIG_DMA_NEED_SYNC
712− dma_need_sync = !dev->dma_skip_sync;
712+ dma_need_sync = !dev_dma_skip_sync(dev);
713713 #endif /* CONFIG_DMA_NEED_SYNC */
714714 if (dma_need_sync || dma_addressing_limited(dev))
715715 return -EOPNOTSUPP;
rust/Makefile+1−0
@@ -119,6 +119,7 @@ syn-cfgs := \ · 118 unmodified lines
119119 feature="parsing" \
120120 feature="printing" \
121121 feature="proc-macro" \
122+ feature="visit" \
122123 feature="visit-mut"
123124
124125 syn-flags := \
rust/helpers/acpi.c+16−0
@@ -0,0 +1,16 @@
1+// SPDX-License-Identifier: GPL-2.0
2+
3+#include <linux/acpi.h>
4+#include <acpi/acpi_bus.h>
5+
6+__rust_helper bool rust_helper_acpi_of_match_device(const struct acpi_device *adev,
7+ const struct of_device_id *of_match_table,
8+ const struct of_device_id **of_id)
9+{
10+ return acpi_of_match_device(adev, of_match_table, of_id);
11+}
12+
13+__rust_helper struct acpi_device *rust_helper_to_acpi_device_node(struct fwnode_handle *fwnode)
14+{
15+ return to_acpi_device_node(fwnode);
16+}
rust/helpers/helpers.c+1−0
@@ -38,6 +38,7 @@ · 37 unmodified lines
3838 #define __rust_helper __always_inline
3939 #endif
4040
41+#include "acpi.c"
4142 #include "atomic.c"
4243 #include "atomic_ext.c"
4344 #include "auxiliary.c"
rust/kernel/alloc/kbox.rs+39−6
@@ -279,6 +279,27 @@ where · 278 unmodified lines
279279 Ok(Box(ptr.cast(), PhantomData))
280280 }
281281
282+ /// Creates a new zero-initialized `Box<T, A>`.
283+ ///
284+ /// New memory is allocated with `A` and the [`__GFP_ZERO`] flag. The allocation may fail, in
285+ /// which case an error is returned. For ZSTs no memory is allocated.
286+ ///
287+ /// # Examples
288+ ///
289+ /// ```
290+ /// let b = KBox::<[u8; 128]>::zeroed(GFP_KERNEL)?;
291+ /// assert_eq!(*b, [0; 128]);
292+ /// # Ok::<(), Error>(())
293+ /// ```
294+ pub fn zeroed(flags: Flags) -> Result<Self, AllocError>
295+ where
296+ T: Zeroable,
297+ {
298+ // SAFETY: `__GFP_ZERO` guarantees the memory is zeroed; `T: Zeroable` guarantees that
299+ // all-zeroes is a valid bit pattern for `T`.
300+ Ok(unsafe { Self::new_uninit(flags | __GFP_ZERO)?.assume_init() })
301+ }
302+
282303 /// Constructs a new `Pin<Box<T, A>>`. If `T` does not implement [`Unpin`], then `x` will be
283304 /// pinned in memory and can't be moved.
284305 #[inline]
@@ -483,7 +504,7 @@ where · 198 unmodified lines
483504
484505 // SAFETY: The pointer returned by `into_foreign` comes from a well aligned
485506 // pointer to `T` allocated by `A`.
486−unsafe impl<T: 'static, A> ForeignOwnable for Box<T, A>
507+unsafe impl<T, A> ForeignOwnable for Box<T, A>
487508 where
488509 A: Allocator,
489510 {
@@ -493,8 +514,14 @@ where · 3 unmodified lines
493514 core::mem::align_of::<T>()
494515 };
495516
496− type Borrowed<'a> = &'a T;
497− type BorrowedMut<'a> = &'a mut T;
517+ type Borrowed<'a>
518+ = &'a T
519+ where
520+ Self: 'a;
521+ type BorrowedMut<'a>
522+ = &'a mut T
523+ where
524+ Self: 'a;
498525
499526 fn into_foreign(self) -> *mut c_void {
500527 Box::into_raw(self).cast()
@@ -522,13 +549,19 @@ where · 21 unmodified lines
522549
523550 // SAFETY: The pointer returned by `into_foreign` comes from a well aligned
524551 // pointer to `T` allocated by `A`.
525−unsafe impl<T: 'static, A> ForeignOwnable for Pin<Box<T, A>>
552+unsafe impl<T, A> ForeignOwnable for Pin<Box<T, A>>
526553 where
527554 A: Allocator,
528555 {
529556 const FOREIGN_ALIGN: usize = <Box<T, A> as ForeignOwnable>::FOREIGN_ALIGN;
530− type Borrowed<'a> = Pin<&'a T>;
531− type BorrowedMut<'a> = Pin<&'a mut T>;
557+ type Borrowed<'a>
558+ = Pin<&'a T>
559+ where
560+ Self: 'a;
561+ type BorrowedMut<'a>
562+ = Pin<&'a mut T>
563+ where
564+ Self: 'a;
532565
533566 fn into_foreign(self) -> *mut c_void {
534567 // SAFETY: We are still treating the box as pinned.
rust/kernel/auxiliary.rs+213−72
@@ -12,19 +12,25 @@ use crate::{ · 11 unmodified lines
1212 RawDeviceId,
1313 RawDeviceIdIndex, //
1414 },
15− devres::Devres,
15+
1616 driver,
1717 error::{
1818 from_result,
1919 to_result, //
2020 },
2121 prelude::*,
22− types::Opaque,
22+ types::{
23+ ForLt,
24+ ForeignOwnable,
25+ Opaque, //
26+ },
2327 ThisModule, //
2428 };
2529 use core::{
30+ any::TypeId,
2631 marker::PhantomData,
2732 mem::offset_of,
33+ pin::Pin,
2834 ptr::{
2935 addr_of_mut,
3036 NonNull, //
@@ -36,18 +42,18 @@ pub struct Adapter<T: Driver>(T); · 5 unmodified lines
3642
3743 // SAFETY:
3844 // - `bindings::auxiliary_driver` is a C type declared as `repr(C)`.
39−// - `T` is the type of the driver's device private data.
45+// - `T::Data` is the type of the driver's device private data.
4046 // - `struct auxiliary_driver` embeds a `struct device_driver`.
4147 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
42−unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
48+unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
4349 type DriverType = bindings::auxiliary_driver;
44− type DriverData = T;
50+ type DriverData<'bound> = T::Data<'bound>;
4551 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
4652 }
4753
4854 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
4955 // a preceding call to `register` has been successful.
50−unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
56+unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
5157 unsafe fn register(
5258 adrv: &Opaque<Self::DriverType>,
5359 name: &'static CStr,
@@ -73,7 +79,7 @@ unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { · 19 unmodified lines
7379 }
7480 }
7581
76−impl<T: Driver + 'static> Adapter<T> {
82+impl<T: Driver> Adapter<T> {
7783 extern "C" fn probe_callback(
7884 adev: *mut bindings::auxiliary_device,
7985 id: *const bindings::auxiliary_device_id,
@@ -82,7 +88,7 @@ impl<T: Driver + 'static> Adapter<T> { · 2 unmodified lines
8288 // `struct auxiliary_device`.
8389 //
8490 // INVARIANT: `adev` is valid for the duration of `probe_callback()`.
85− let adev = unsafe { &*adev.cast::<Device<device::CoreInternal>>() };
91+ let adev = unsafe { &*adev.cast::<Device<device::CoreInternal<'_>>>() };
8692
8793 // SAFETY: `DeviceId` is a `#[repr(transparent)`] wrapper of `struct auxiliary_device_id`
8894 // and does not add additional invariants, so it's safe to transmute.
@@ -102,12 +108,12 @@ impl<T: Driver + 'static> Adapter<T> { · 13 unmodified lines
102108 // `struct auxiliary_device`.
103109 //
104110 // INVARIANT: `adev` is valid for the duration of `remove_callback()`.
105− let adev = unsafe { &*adev.cast::<Device<device::CoreInternal>>() };
111+ let adev = unsafe { &*adev.cast::<Device<device::CoreInternal<'_>>>() };
106112
107113 // SAFETY: `remove_callback` is only ever called after a successful call to
108114 // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
109− // and stored a `Pin<KBox<T>>`.
110− let data = unsafe { adev.as_ref().drvdata_borrow::<T>() };
115+ // and stored a `Pin<KBox<T::Data<'_>>>`.
116+ let data = unsafe { adev.as_ref().drvdata_borrow::<T::Data<'_>>() };
111117
112118 T::unbind(adev, data);
113119 }
@@ -197,13 +203,19 @@ pub trait Driver { · 83 unmodified lines
197203 /// type IdInfo: 'static = ();
198204 type IdInfo: 'static;
199205
206+ /// The type of the driver's bus device private data.
207+ type Data<'bound>: Send + 'bound;
208+
200209 /// The table of device ids supported by the driver.
201210 const ID_TABLE: IdTable<Self::IdInfo>;
202211
203212 /// Auxiliary driver probe.
204213 ///
205214 /// Called when an auxiliary device is matches a corresponding driver.
206− fn probe(dev: &Device<device::Core>, id_info: &Self::IdInfo) -> impl PinInit<Self, Error>;
215+ fn probe<'bound>(
216+ dev: &'bound Device<device::Core<'_>>,
217+ id_info: &'bound Self::IdInfo,
218+ ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound;
207219
208220 /// Auxiliary driver unbind.
209221 ///
@@ -214,8 +226,8 @@ pub trait Driver { · 4 unmodified lines
214226 /// `&Device<Core>` or `&Device<Bound>` reference. For instance, drivers may try to perform I/O
215227 /// operations to gracefully tear down the device.
216228 ///
217− /// Otherwise, release operations for driver resources should be performed in `Self::drop`.
218− fn unbind(dev: &Device<device::Core>, this: Pin<&Self>) {
229+ /// Otherwise, release operations for driver resources should be performed in `Drop`.
230+ fn unbind<'bound>(dev: &'bound Device<device::Core<'_>>, this: Pin<&Self::Data<'bound>>) {
219231 let _ = (dev, this);
220232 }
221233 }
@@ -257,6 +269,49 @@ impl Device<device::Bound> { · 35 unmodified lines
257269 // SAFETY: A bound auxiliary device always has a bound parent device.
258270 unsafe { parent.as_bound() }
259271 }
272+
273+ /// Returns a pinned reference to the registration data set by the registering (parent) driver.
274+ ///
275+ /// `F` is the [`ForLt`](trait@ForLt) encoding of the data type. The returned
276+ /// reference has its lifetime shortened from `'static` to `&self`'s borrow lifetime via
277+ /// [`ForLt::cast_ref`].
278+ ///
279+ /// Returns [`EINVAL`] if `F` does not match the type used by the parent driver when calling
280+ /// [`Registration::new()`].
281+ ///
282+ /// Returns [`ENOENT`] if no registration data has been set, e.g. when the device was
283+ /// registered by a C driver.
284+ pub fn registration_data<F: ForLt + 'static>(&self) -> Result<Pin<&F::Of<'_>>> {
285+ // SAFETY: By the type invariant, `self.as_raw()` is a valid `struct auxiliary_device`.
286+ let ptr = unsafe { (*self.as_raw()).registration_data_rust };
287+ if ptr.is_null() {
288+ dev_warn!(
289+ self.as_ref(),
290+ "No registration data set; parent is not a Rust driver.\n"
291+ );
292+ return Err(ENOENT);
293+ }
294+
295+ // SAFETY: `ptr` is non-null and was set via `into_foreign()` in `Registration::new()`;
296+ // `RegistrationData` is `#[repr(C)]` with `type_id` at offset 0, so reading a `TypeId`
297+ // at the start of the allocation is valid regardless of `F`.
298+ let type_id = unsafe { ptr.cast::<TypeId>().read() };
299+ if type_id != TypeId::of::<F>() {
300+ return Err(EINVAL);
301+ }
302+
303+ // SAFETY: The `TypeId` check above confirms that the stored type matches
304+ // `F::Of<'static>`; `ptr` remains valid until `Registration::drop()` calls
305+ // `from_foreign()`.
306+ let wrapper = unsafe { Pin::<KBox<RegistrationData<F::Of<'static>>>>::borrow(ptr) };
307+
308+ // SAFETY: `data` is a structurally pinned field of `RegistrationData`.
309+ let pinned: Pin<&F::Of<'_>> = unsafe { wrapper.map_unchecked(|w| &w.data) };
310+
311+ // SAFETY: The data was pinned when stored; `cast_ref` only shortens
312+ // the lifetime, so the pinning guarantee is preserved.
313+ Ok(unsafe { Pin::new_unchecked(F::cast_ref(pinned.get_ref())) })
314+ }
260315 }
261316
262317 impl Device {
@@ -326,87 +381,173 @@ unsafe impl Send for Device {} · 63 unmodified lines
326381 // (i.e. `Device<Normal>) are thread safe.
327382 unsafe impl Sync for Device {}
328383
384+// SAFETY: Same as `Device<Normal>` -- the underlying `struct auxiliary_device` is the same;
385+// `Bound` is a zero-sized type-state marker that does not affect thread safety.
386+unsafe impl Sync for Device<device::Bound> {}
387+
388+/// Wrapper that stores a [`TypeId`] alongside the registration data for runtime type checking.
389+#[repr(C)]
390+#[pin_data]
391+struct RegistrationData<T> {
392+ type_id: TypeId,
393+ #[pin]
394+ data: T,
395+}
396+
329397 /// The registration of an auxiliary device.
330398 ///
331399 /// This type represents the registration of a [`struct auxiliary_device`]. When its parent device
332400 /// is unbound, the corresponding auxiliary device will be unregistered from the system.
333401 ///
402+/// The type parameter `F` is a [`ForLt`](trait@ForLt) encoding of the registration
403+/// data type. For non-lifetime-parameterized types, use [`ForLt!(T)`](macro@ForLt).
404+/// The data can be accessed by the auxiliary driver through [`Device::registration_data()`].
405+///
334406 /// # Invariants
335407 ///
336−/// `self.0` always holds a valid pointer to an initialized and registered
337−/// [`struct auxiliary_device`].
338−pub struct Registration(NonNull<bindings::auxiliary_device>);
408+/// `self.adev` always holds a valid pointer to an initialized and registered
409+/// [`struct auxiliary_device`] whose `registration_data_rust` field points to a
410+/// valid `Pin<KBox<RegistrationData<F::Of<'static>>>>`.
411+pub struct Registration<'a, F: ForLt + 'static> {
412+ adev: NonNull<bindings::auxiliary_device>,
413+ _phantom: PhantomData<F::Of<'a>>,
414+}
339415
340−impl Registration {
341− /// Create and register a new auxiliary device.
342− pub fn new<'a>(
416+impl<'a, F: ForLt> Registration<'a, F>
417+where
418+ for<'b> F::Of<'b>: Send + Sync,
419+{
420+ /// Create and register a new auxiliary device with the given registration data.
421+ ///
422+ /// The `data` is owned by the registration and can be accessed through the auxiliary device
423+ /// via [`Device::registration_data()`].
424+ ///
425+ /// # Safety
426+ ///
427+ /// The caller must not `mem::forget()` the returned [`Registration`] or otherwise prevent its
428+ /// [`Drop`] implementation from running, since the registration data may contain borrowed
429+ /// references that become invalid after `'a` ends.
430+ ///
431+ /// If the registration data is `'static`, use the safe [`Registration::new()`] instead.
432+ pub unsafe fn new_with_lt<E>(
343433 parent: &'a device::Device<device::Bound>,
344− name: &'a CStr,
434+ name: &CStr,
345435 id: u32,
346− modname: &'a CStr,
347− ) -> impl PinInit<Devres<Self>, Error> + 'a {
348− pin_init::pin_init_scope(move || {
349− let boxed = KBox::new(Opaque::<bindings::auxiliary_device>::zeroed(), GFP_KERNEL)?;
350− let adev = boxed.get();
351−
352− // SAFETY: It's safe to set the fields of `struct auxiliary_device` on initialization.
353− unsafe {
354− (*adev).dev.parent = parent.as_raw();
355− (*adev).dev.release = Some(Device::release);
356− (*adev).name = name.as_char_ptr();
357− (*adev).id = id;
358− }
359−
360− // SAFETY: `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`,
361− // which has not been initialized yet.
362− unsafe { bindings::auxiliary_device_init(adev) };
363−
364− // Now that `adev` is initialized, leak the `Box`; the corresponding memory will be
365− // freed by `Device::release` when the last reference to the `struct auxiliary_device`
366− // is dropped.
367− let _ = KBox::into_raw(boxed);
368−
369− // SAFETY:
370− // - `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`, which
371− // has been initialized,
372− // - `modname.as_char_ptr()` is a NULL terminated string.
373− let ret = unsafe { bindings::__auxiliary_device_add(adev, modname.as_char_ptr()) };
374− if ret != 0 {
375− // SAFETY: `adev` is guaranteed to be a valid pointer to a
376− // `struct auxiliary_device`, which has been initialized.
377− unsafe { bindings::auxiliary_device_uninit(adev) };
378−
379− return Err(Error::from_errno(ret));
380− }
381−
382− // INVARIANT: The device will remain registered until `auxiliary_device_delete()` is
383− // called, which happens in `Self::drop()`.
384− Ok(Devres::new(
385− parent,
386− // SAFETY: `adev` is guaranteed to be non-null, since the `KBox` was allocated
387− // successfully.
388− Self(unsafe { NonNull::new_unchecked(adev) }),
389− ))
436+ modname: &CStr,
437+ data: impl PinInit<F::Of<'a>, E>,
438+ ) -> Result<Self>
439+ where
440+ Error: From<E>,
441+ {
442+ let data = KBox::pin_init::<Error>(
443+ try_pin_init!(RegistrationData {
444+ type_id: TypeId::of::<F>(),
445+ data <- data,
446+ }),
447+ GFP_KERNEL,
448+ )?;
449+
450+ // SAFETY: `'a` is invariant (via `Registration`'s `PhantomData`). Lifetimes do not
451+ // affect layout, so RegistrationData<F::Of<'a>> and RegistrationData<F::Of<'static>>
452+ // have identical representation.
453+ let data: Pin<KBox<RegistrationData<F::Of<'static>>>> =
454+ unsafe { core::mem::transmute(data) };
455+
456+ let boxed: KBox<Opaque<bindings::auxiliary_device>> = KBox::zeroed(GFP_KERNEL)?;
457+ let adev = boxed.get();
458+
459+ // SAFETY: It's safe to set the fields of `struct auxiliary_device` on initialization.
460+ unsafe {
461+ (*adev).dev.parent = parent.as_raw();
462+ (*adev).dev.release = Some(Device::release);
463+ (*adev).name = name.as_char_ptr();
464+ (*adev).id = id;
465+ (*adev).registration_data_rust = data.into_foreign();
466+ }
467+
468+ // SAFETY: `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`,
469+ // which has not been initialized yet.
470+ unsafe { bindings::auxiliary_device_init(adev) };
471+
472+ // Now that `adev` is initialized, leak the `Box`; the corresponding memory will be
473+ // freed by `Device::release` when the last reference to the `struct auxiliary_device`
474+ // is dropped.
475+ let _ = KBox::into_raw(boxed);
476+
477+ // SAFETY:
478+ // - `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`, which
479+ // has been initialized,
480+ // - `modname.as_char_ptr()` is a NULL terminated string.
481+ let ret = unsafe { bindings::__auxiliary_device_add(adev, modname.as_char_ptr()) };
482+ if ret != 0 {
483+ // SAFETY: `registration_data` was set above via `into_foreign()`.
484+ drop(unsafe {
485+ Pin::<KBox<RegistrationData<F::Of<'static>>>>::from_foreign(
486+ (*adev).registration_data_rust,
487+ )
488+ });
489+
490+ // SAFETY: `adev` is guaranteed to be a valid pointer to a
491+ // `struct auxiliary_device`, which has been initialized.
492+ unsafe { bindings::auxiliary_device_uninit(adev) };
493+
494+ return Err(Error::from_errno(ret));
495+ }
496+
497+ // INVARIANT: The device will remain registered until `auxiliary_device_delete()` is
498+ // called, which happens in `Self::drop()`.
499+ Ok(Self {
500+ // SAFETY: `adev` is guaranteed to be non-null, since the `KBox` was allocated
501+ // successfully.
502+ adev: unsafe { NonNull::new_unchecked(adev) },
503+ _phantom: PhantomData,
390504 })
391505 }
506+
507+ /// Create and register a new auxiliary device with `'static` registration data.
508+ ///
509+ /// Safe variant of [`Registration::new_with_lt()`] for registration data that does not contain
510+ /// borrowed references.
511+ pub fn new<E>(
512+ parent: &'a device::Device<device::Bound>,
513+ name: &CStr,
514+ id: u32,
515+ modname: &CStr,
516+ data: impl PinInit<F::Of<'a>, E>,
517+ ) -> Result<Self>
518+ where
519+ F::Of<'a>: 'static,
520+ Error: From<E>,
521+ {
522+ // SAFETY: `F::Of<'a>: 'static` guarantees the data contains no borrowed references,
523+ // so forgetting the `Registration` cannot cause use-after-free.
524+ unsafe { Self::new_with_lt(parent, name, id, modname, data) }
525+ }
392526 }
393527
394−impl Drop for Registration {
528+impl<F: ForLt> Drop for Registration<'_, F> {
395529 fn drop(&mut self) {
396− // SAFETY: By the type invariant of `Self`, `self.0.as_ptr()` is a valid registered
530+ // SAFETY: By the type invariant of `Self`, `self.adev.as_ptr()` is a valid registered
397531 // `struct auxiliary_device`.
398− unsafe { bindings::auxiliary_device_delete(self.0.as_ptr()) };
532+ unsafe { bindings::auxiliary_device_delete(self.adev.as_ptr()) };
533+
534+ // SAFETY: `registration_data` was set in `new()` via `into_foreign()`.
535+ drop(unsafe {
536+ Pin::<KBox<RegistrationData<F::Of<'static>>>>::from_foreign(
537+ (*self.adev.as_ptr()).registration_data_rust,
538+ )
539+ });
399540
400541 // This drops the reference we acquired through `auxiliary_device_init()`.
401542 //
402− // SAFETY: By the type invariant of `Self`, `self.0.as_ptr()` is a valid registered
543+ // SAFETY: By the type invariant of `Self`, `self.adev.as_ptr()` is a valid registered
403544 // `struct auxiliary_device`.
404− unsafe { bindings::auxiliary_device_uninit(self.0.as_ptr()) };
545+ unsafe { bindings::auxiliary_device_uninit(self.adev.as_ptr()) };
405546 }
406547 }
407548
408549 // SAFETY: A `Registration` of a `struct auxiliary_device` can be released from any thread.
409−unsafe impl Send for Registration {}
550+unsafe impl<F: ForLt> Send for Registration<'_, F> where for<'a> F::Of<'a>: Send {}
410551
411552 // SAFETY: `Registration` does not expose any methods or fields that need synchronization.
412−unsafe impl Sync for Registration {}
553+unsafe impl<F: ForLt> Sync for Registration<'_, F> where for<'a> F::Of<'a>: Send {}
rust/kernel/cpufreq.rs+5−4
@@ -888,12 +888,13 @@ pub trait Driver { · 887 unmodified lines
888888 ///
889889 /// impl platform::Driver for SampleDriver {
890890 /// type IdInfo = ();
891+/// type Data<'bound> = Self;
891892 /// const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = None;
892893 ///
893−/// fn probe(
894−/// pdev: &platform::Device<Core>,
895−/// _id_info: Option<&Self::IdInfo>,
896−/// ) -> impl PinInit<Self, Error> {
894+/// fn probe<'bound>(
895+/// pdev: &'bound platform::Device<Core<'_>>,
896+/// _id_info: Option<&'bound Self::IdInfo>,
897+/// ) -> impl PinInit<Self, Error> + 'bound {
897898 /// cpufreq::Registration::<SampleDriver>::new_foreign_owned(pdev.as_ref())?;
898899 /// Ok(Self {})
899900 /// }
rust/kernel/device.rs+46−75
@@ -15,16 +15,12 @@ use crate::{ · 14 unmodified lines
1515 }, //
1616 };
1717 use core::{
18− any::TypeId,
1918 marker::PhantomData,
2019 ptr, //
2120 };
2221
2322 pub mod property;
2423
25−// Assert that we can `read()` / `write()` a `TypeId` instance from / into `struct driver_type`.
26−static_assert!(core::mem::size_of::<bindings::driver_type>() >= core::mem::size_of::<TypeId>());
27−
2824 /// The core representation of a device in the kernel's driver model.
2925 ///
3026 /// This structure represents the Rust abstraction for a C `struct device`. A [`Device`] can either
@@ -205,30 +201,13 @@ impl Device { · 174 unmodified lines
205201 }
206202 }
207203
208−impl Device<CoreInternal> {
209− fn set_type_id<T: 'static>(&self) {
210− // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
211− let private = unsafe { (*self.as_raw()).p };
212−
213− // SAFETY: For a bound device (implied by the `CoreInternal` device context), `private` is
214− // guaranteed to be a valid pointer to a `struct device_private`.
215− let driver_type = unsafe { &raw mut (*private).driver_type };
216−
217− // SAFETY: `driver_type` is valid for (unaligned) writes of a `TypeId`.
218− unsafe {
219− driver_type
220− .cast::<TypeId>()
221− .write_unaligned(TypeId::of::<T>())
222− };
223− }
224−
204+impl<'a> Device<CoreInternal<'a>> {
225205 /// Store a pointer to the bound driver's private data.
226− pub fn set_drvdata<T: 'static>(&self, data: impl PinInit<T, Error>) -> Result {
206+ pub fn set_drvdata<T>(&self, data: impl PinInit<T, Error>) -> Result {
227207 let data = KBox::pin_init(data, GFP_KERNEL)?;
228208
229209 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
230210 unsafe { bindings::dev_set_drvdata(self.as_raw(), data.into_foreign().cast()) };
231− self.set_type_id::<T>();
232211
233212 Ok(())
234213 }
@@ -239,7 +218,7 @@ impl Device<CoreInternal> { · 4 unmodified lines
239218 ///
240219 /// - The type `T` must match the type of the `ForeignOwnable` previously stored by
241220 /// [`Device::set_drvdata`].
242− pub(crate) unsafe fn drvdata_obtain<T: 'static>(&self) -> Option<Pin<KBox<T>>> {
221+ pub(crate) unsafe fn drvdata_obtain<T>(&self) -> Option<Pin<KBox<T>>> {
243222 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
244223 let ptr = unsafe { bindings::dev_get_drvdata(self.as_raw()) };
245224
@@ -265,7 +244,7 @@ impl Device<CoreInternal> { · 19 unmodified lines
265244 /// device is fully unbound.
266245 /// - The type `T` must match the type of the `ForeignOwnable` previously stored by
267246 /// [`Device::set_drvdata`].
268− pub unsafe fn drvdata_borrow<T: 'static>(&self) -> Pin<&T> {
247+ pub unsafe fn drvdata_borrow<T>(&self) -> Pin<&T> {
269248 // SAFETY: `drvdata_unchecked()` has the exact same safety requirements as the ones
270249 // required by this method.
271250 unsafe { self.drvdata_unchecked() }
@@ -281,7 +260,7 @@ impl Device<Bound> { · 9 unmodified lines
281260 /// the device is fully unbound.
282261 /// - The type `T` must match the type of the `ForeignOwnable` previously stored by
283262 /// [`Device::set_drvdata`].
284− unsafe fn drvdata_unchecked<T: 'static>(&self) -> Pin<&T> {
263+ unsafe fn drvdata_unchecked<T>(&self) -> Pin<&T> {
285264 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
286265 let ptr = unsafe { bindings::dev_get_drvdata(self.as_raw()) };
287266
@@ -292,45 +271,6 @@ impl Device<Bound> { · 4 unmodified lines
292271 // in `into_foreign()`.
293272 unsafe { Pin::<KBox<T>>::borrow(ptr.cast()) }
294273 }
295−
296− fn match_type_id<T: 'static>(&self) -> Result {
297− // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
298− let private = unsafe { (*self.as_raw()).p };
299−
300− // SAFETY: For a bound device, `private` is guaranteed to be a valid pointer to a
301− // `struct device_private`.
302− let driver_type = unsafe { &raw mut (*private).driver_type };
303−
304− // SAFETY:
305− // - `driver_type` is valid for (unaligned) reads of a `TypeId`.
306− // - A bound device guarantees that `driver_type` contains a valid `TypeId` value.
307− let type_id = unsafe { driver_type.cast::<TypeId>().read_unaligned() };
308−
309− if type_id != TypeId::of::<T>() {
310− return Err(EINVAL);
311− }
312−
313− Ok(())
314− }
315−
316− /// Access a driver's private data.
317− ///
318− /// Returns a pinned reference to the driver's private data or [`EINVAL`] if it doesn't match
319− /// the asserted type `T`.
320− pub fn drvdata<T: 'static>(&self) -> Result<Pin<&T>> {
321− // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
322− if unsafe { bindings::dev_get_drvdata(self.as_raw()) }.is_null() {
323− return Err(ENOENT);
324− }
325−
326− self.match_type_id::<T>()?;
327−
328− // SAFETY:
329− // - The above check of `dev_get_drvdata()` guarantees that we are called after
330− // `set_drvdata()`.
331− // - We've just checked that the type of the driver's private data is in fact `T`.
332− Ok(unsafe { self.drvdata_unchecked() })
333− }
334274 }
335275
336276 impl<Ctx: DeviceContext> Device<Ctx> {
@@ -527,6 +467,10 @@ unsafe impl Send for Device {} · 190 unmodified lines
527467 // synchronization in `struct device`.
528468 unsafe impl Sync for Device {}
529469
470+// SAFETY: Same as `Device<Normal>` -- the underlying `struct device` is the same; `Bound` is a
471+// zero-sized type-state marker that does not affect thread safety.
472+unsafe impl Sync for Device<Bound> {}
473+
530474 /// Marker trait for the context or scope of a bus specific device.
531475 ///
532476 /// [`DeviceContext`] is a marker trait for types representing the context of a bus specific
@@ -567,7 +511,7 @@ pub struct Normal; · 34 unmodified lines
567511 /// callback it appears in. It is intended to be used for synchronization purposes. Bus device
568512 /// implementations can implement methods for [`Device<Core>`], such that they can only be called
569513 /// from bus callbacks.
570−pub struct Core;
514+pub struct Core<'a>(PhantomData<&'a ()>);
571515
572516 /// Semantically the same as [`Core`], but reserved for internal usage of the corresponding bus
573517 /// abstraction.
@@ -578,7 +522,7 @@ pub struct Core; · 4 unmodified lines
578522 ///
579523 /// This context mainly exists to share generic [`Device`] infrastructure that should only be called
580524 /// from bus callbacks with bus abstractions, but without making them accessible for drivers.
581−pub struct CoreInternal;
525+pub struct CoreInternal<'a>(PhantomData<&'a ()>);
582526
583527 /// The [`Bound`] context is the [`DeviceContext`] of a bus specific device when it is guaranteed to
584528 /// be bound to a driver.
@@ -602,14 +546,14 @@ mod private { · 17 unmodified lines
602546 pub trait Sealed {}
603547
604548 impl Sealed for super::Bound {}
605− impl Sealed for super::Core {}
606− impl Sealed for super::CoreInternal {}
549+ impl<'a> Sealed for super::Core<'a> {}
550+ impl<'a> Sealed for super::CoreInternal<'a> {}
607551 impl Sealed for super::Normal {}
608552 }
609553
610554 impl DeviceContext for Bound {}
611−impl DeviceContext for Core {}
612−impl DeviceContext for CoreInternal {}
555+impl<'a> DeviceContext for Core<'a> {}
556+impl<'a> DeviceContext for CoreInternal<'a> {}
613557 impl DeviceContext for Normal {}
614558
615559 impl<Ctx: DeviceContext> AsRef<Device<Ctx>> for Device<Ctx> {
@@ -659,6 +603,22 @@ pub unsafe trait AsBusDevice<Ctx: DeviceContext>: AsRef<Device<Ctx>> { · 43 unmodified lines
659603 #[doc(hidden)]
660604 #[macro_export]
661605 macro_rules! __impl_device_context_deref {
606+ (unsafe { $device:ident, <$lt:lifetime> $src:ty => $dst:ty }) => {
607+ impl<$lt> ::core::ops::Deref for $device<$src> {
608+ type Target = $device<$dst>;
609+
610+ fn deref(&self) -> &Self::Target {
611+ let ptr: *const Self = self;
612+
613+ // CAST: `$device<$src>` and `$device<$dst>` transparently wrap the same type by the
614+ // safety requirement of the macro.
615+ let ptr = ptr.cast::<Self::Target>();
616+
617+ // SAFETY: `ptr` was derived from `&self`.
618+ unsafe { &*ptr }
619+ }
620+ }
621+ };
662622 (unsafe { $device:ident, $src:ty => $dst:ty }) => {
663623 impl ::core::ops::Deref for $device<$src> {
664624 type Target = $device<$dst>;
@@ -691,14 +651,14 @@ macro_rules! impl_device_context_deref { · 26 unmodified lines
691651 // `__impl_device_context_deref!`.
692652 ::kernel::__impl_device_context_deref!(unsafe {
693653 $device,
694− $crate::device::CoreInternal => $crate::device::Core
654+ <'a> $crate::device::CoreInternal<'a> => $crate::device::Core<'a>
695655 });
696656
697657 // SAFETY: This macro has the exact same safety requirement as
698658 // `__impl_device_context_deref!`.
699659 ::kernel::__impl_device_context_deref!(unsafe {
700660 $device,
701− $crate::device::Core => $crate::device::Bound
661+ <'a> $crate::device::Core<'a> => $crate::device::Bound
702662 });
703663
704664 // SAFETY: This macro has the exact same safety requirement as
@@ -713,6 +673,13 @@ macro_rules! impl_device_context_deref { · 8 unmodified lines
713673 #[doc(hidden)]
714674 #[macro_export]
715675 macro_rules! __impl_device_context_into_aref {
676+ (<$lt:lifetime> $src:ty, $device:tt) => {
677+ impl<$lt> ::core::convert::From<&$device<$src>> for $crate::sync::aref::ARef<$device> {
678+ fn from(dev: &$device<$src>) -> Self {
679+ (&**dev).into()
680+ }
681+ }
682+ };
716683 ($src:ty, $device:tt) => {
717684 impl ::core::convert::From<&$device<$src>> for $crate::sync::aref::ARef<$device> {
718685 fn from(dev: &$device<$src>) -> Self {
@@ -727,8 +694,12 @@ macro_rules! __impl_device_context_into_aref { · 8 unmodified lines
727694 #[macro_export]
728695 macro_rules! impl_device_context_into_aref {
729696 ($device:tt) => {
730− ::kernel::__impl_device_context_into_aref!($crate::device::CoreInternal, $device);
731− ::kernel::__impl_device_context_into_aref!($crate::device::Core, $device);
697+ ::kernel::__impl_device_context_into_aref!(
698+ <'a> $crate::device::CoreInternal<'a>, $device
699+ );
700+ ::kernel::__impl_device_context_into_aref!(
701+ <'a> $crate::device::Core<'a>, $device
702+ );
732703 ::kernel::__impl_device_context_into_aref!($crate::device::Bound, $device);
733704 };
734705 }
rust/kernel/devres.rs+4−4
@@ -122,7 +122,7 @@ struct Inner<T> { · 121 unmodified lines
122122 /// # Ok(())
123123 /// # }
124124 /// ```
125−pub struct Devres<T: Send> {
125+pub struct Devres<T: Send + 'static> {
126126 dev: ARef<Device>,
127127 inner: Arc<Inner<T>>,
128128 }
@@ -184,7 +184,7 @@ mod base { · 55 unmodified lines
184184 }
185185 }
186186
187−impl<T: Send> Devres<T> {
187+impl<T: Send + 'static> Devres<T> {
188188 /// Creates a new [`Devres`] instance of the given `data`.
189189 ///
190190 /// The `data` encapsulated within the returned `Devres` instance' `data` will be
@@ -304,7 +304,7 @@ impl<T: Send> Devres<T> { · 113 unmodified lines
304304 /// pci, //
305305 /// };
306306 ///
307− /// fn from_core(dev: &pci::Device<Core>, devres: Devres<pci::Bar<0x4>>) -> Result {
307+ /// fn from_core(dev: &pci::Device<Core<'_>>, devres: Devres<pci::Bar<'_, 0x4>>) -> Result {
308308 /// let bar = devres.access(dev.as_ref())?;
309309 ///
310310 /// let _ = bar.read32(0x0);
@@ -349,7 +349,7 @@ unsafe impl<T: Send> Send for Devres<T> {} · 38 unmodified lines
349349 // SAFETY: `Devres` can be shared with any task, if `T: Sync`.
350350 unsafe impl<T: Send + Sync> Sync for Devres<T> {}
351351
352−impl<T: Send> Drop for Devres<T> {
352+impl<T: Send + 'static> Drop for Devres<T> {
353353 fn drop(&mut self) {
354354 // SAFETY: When `drop` runs, it is guaranteed that nobody is accessing the revocable data
355355 // anymore, hence it is safe not to wait for the grace period to finish.
rust/kernel/dma.rs+1−1
@@ -47,7 +47,7 @@ pub type DmaAddress = bindings::dma_addr_t; · 46 unmodified lines
4747 /// where the underlying bus is DMA capable, such as:
4848 #[cfg_attr(CONFIG_PCI, doc = "* [`pci::Device`](kernel::pci::Device)")]
4949 /// * [`platform::Device`](::kernel::platform::Device)
50−pub trait Device: AsRef<device::Device<Core>> {
50+pub trait Device<'a>: AsRef<device::Device<Core<'a>>> {
5151 /// Set up the device's DMA streaming addressing capabilities.
5252 ///
5353 /// This method is usually called once from `probe()` as soon as the device capabilities are
rust/kernel/driver.rs+88−27
@@ -13,10 +13,13 @@ · 12 unmodified lines
1313 //! The main driver interface is defined by a bus specific driver trait. For instance:
1414 //!
1515 //! ```ignore
16−//! pub trait Driver: Send {
16+//! pub trait Driver {
1717 //! /// The type holding information about each device ID supported by the driver.
1818 //! type IdInfo: 'static;
1919 //!
20+//! /// The type of the driver's bus device private data.
21+//! type Data<'bound>: Send + 'bound;
22+//!
2023 //! /// The table of OF device ids supported by the driver.
2124 //! const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = None;
2225 //!
@@ -24,10 +27,16 @@ · 1 unmodified line
2427 //! const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = None;
2528 //!
2629 //! /// Driver probe.
27−//! fn probe(dev: &Device<device::Core>, id_info: &Self::IdInfo) -> impl PinInit<Self, Error>;
30+//! fn probe<'bound>(
31+//! dev: &'bound Device<device::Core<'_>>,
32+//! id_info: &'bound Self::IdInfo,
33+//! ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound;
2834 //!
2935 //! /// Driver unbind (optional).
30−//! fn unbind(dev: &Device<device::Core>, this: Pin<&Self>) {
36+//! fn unbind<'bound>(
37+//! dev: &'bound Device<device::Core<'_>>,
38+//! this: Pin<&Self::Data<'bound>>,
39+//! ) {
3140 //! let _ = (dev, this);
3241 //! }
3342 //! }
@@ -42,8 +51,9 @@ · 8 unmodified lines
4251 )]
4352 #![cfg_attr(CONFIG_PCI, doc = "* [`pci::Driver`](kernel::pci::Driver)")]
4453 //!
45−//! The `probe()` callback should return a `impl PinInit<Self, Error>`, i.e. the driver's private
46−//! data. The bus abstraction should store the pointer in the corresponding bus device. The generic
54+//! The `probe()` callback should return a
55+//! `impl PinInit<Self::Data<'bound>, Error>`, i.e. the driver's private data. The bus
56+//! abstraction should store the pointer in the corresponding bus device. The generic
4757 //! [`Device`] infrastructure provides common helpers for this purpose on its
4858 //! [`Device<CoreInternal>`] implementation.
4959 //!
@@ -118,8 +128,8 @@ pub unsafe trait DriverLayout { · 68 unmodified lines
118128 /// The specific driver type embedding a `struct device_driver`.
119129 type DriverType: Default;
120130
121− /// The type of the driver's device private data.
122− type DriverData;
131+ /// The type of the driver's bus device private data.
132+ type DriverData<'bound>;
123133
124134 /// Byte offset of the embedded `struct device_driver` within `DriverType`.
125135 ///
@@ -181,20 +191,20 @@ unsafe impl<T: RegistrationOps> Sync for Registration<T> {} · 55 unmodified lines
181191 // any thread, so `Registration` is `Send`.
182192 unsafe impl<T: RegistrationOps> Send for Registration<T> {}
183193
184−impl<T: RegistrationOps + 'static> Registration<T> {
194+impl<T: RegistrationOps> Registration<T> {
185195 extern "C" fn post_unbind_callback(dev: *mut bindings::device) {
186196 // SAFETY: The driver core only ever calls the post unbind callback with a valid pointer to
187197 // a `struct device`.
188198 //
189199 // INVARIANT: `dev` is valid for the duration of the `post_unbind_callback()`.
190− let dev = unsafe { &*dev.cast::<device::Device<device::CoreInternal>>() };
200+ let dev = unsafe { &*dev.cast::<device::Device<device::CoreInternal<'_>>>() };
191201
192− // `remove()` and all devres callbacks have been completed at this point, hence drop the
193− // driver's device private data.
202+ // `remove()` has been completed at this point; devres resources are still valid and will
203+ // be released after the driver's bus device private data is dropped.
194204 //
195205 // SAFETY: By the safety requirements of the `Driver` trait, `T::DriverData` is the
196− // driver's device private data type.
197− drop(unsafe { dev.drvdata_obtain::<T::DriverData>() });
206+ // driver's bus device private data type.
207+ drop(unsafe { dev.drvdata_obtain::<T::DriverData<'_>>() });
198208 }
199209
200210 /// Attach generic `struct device_driver` callbacks.
@@ -215,7 +225,10 @@ impl<T: RegistrationOps + 'static> Registration<T> { · 14 unmodified lines
215225 }
216226
217227 /// Creates a new instance of the registration object.
218− pub fn new(name: &'static CStr, module: &'static ThisModule) -> impl PinInit<Self, Error> {
228+ pub fn new(name: &'static CStr, module: &'static ThisModule) -> impl PinInit<Self, Error>
229+ where
230+ T: 'static,
231+ {
219232 try_pin_init!(Self {
220233 reg <- Opaque::try_ffi_init(|ptr: *mut T::DriverType| {
221234 // SAFETY: `try_ffi_init` guarantees that `ptr` is valid for write.
@@ -278,6 +291,26 @@ macro_rules! module_driver { · 56 unmodified lines
278291 }
279292 }
280293
294+// Calling the FFI function directly from the `Adapter` impl may result in it being called
295+// directly from driver modules. This happens since the Rust compiler will use monomorphisation, so
296+// it might happen that functions are instantiated within the calling driver module. For now, work
297+// around this with `#[inline(never)]` helpers.
298+//
299+// TODO: Remove once a more generic solution has been implemented. For instance, we may be able to
300+// leverage `bindgen` to take care of this depending on whether a symbol is (already) exported.
301+#[inline(never)]
302+#[allow(clippy::missing_safety_doc)]
303+#[allow(dead_code)]
304+#[must_use]
305+unsafe fn acpi_of_match_device(
306+ adev: *const bindings::acpi_device,
307+ of_match_table: *const bindings::of_device_id,
308+ of_id: *mut *const bindings::of_device_id,
309+) -> bool {
310+ // SAFETY: Safety requirements are the same as `bindings::acpi_of_match_device`.
311+ unsafe { bindings::acpi_of_match_device(adev, of_match_table, of_id) }
312+}
313+
281314 /// The bus independent adapter to match a drivers and a devices.
282315 ///
283316 /// This trait should be implemented by the bus specific adapter, which represents the connection
@@ -329,35 +362,63 @@ pub trait Adapter { · 45 unmodified lines
329362 ///
330363 /// If this returns `None`, it means there is no match with an entry in the [`of::IdTable`].
331364 fn of_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
332− #[cfg(not(CONFIG_OF))]
365+ let table = Self::of_id_table()?;
366+
367+ #[cfg(not(any(CONFIG_OF, CONFIG_ACPI)))]
333368 {
334− let _ = dev;
335− None
369+ let _ = (dev, table);
336370 }
337371
338372 #[cfg(CONFIG_OF)]
339373 {
340− let table = Self::of_id_table()?;
341−
342374 // SAFETY:
343375 // - `table` has static lifetime, hence it's valid for read,
344376 // - `dev` is guaranteed to be valid while it's alive, and so is `dev.as_raw()`.
345377 let raw_id = unsafe { bindings::of_match_device(table.as_ptr(), dev.as_raw()) };
346378
347− if raw_id.is_null() {
348− None
349− } else {
379+ if !raw_id.is_null() {
350380 // SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `struct of_device_id`
351381 // and does not add additional invariants, so it's safe to transmute.
352382 let id = unsafe { &*raw_id.cast::<of::DeviceId>() };
353383
354− Some(
355− table.info(<of::DeviceId as crate::device_id::RawDeviceIdIndex>::index(
356− id,
357− )),
358− )
384+ return Some(table.info(
385+ <of::DeviceId as crate::device_id::RawDeviceIdIndex>::index(id),
386+ ));
387+ }
388+ }
389+
390+ #[cfg(CONFIG_ACPI)]
391+ {
392+ use core::ptr;
393+ use device::property::FwNode;
394+
395+ let mut raw_id = ptr::null();
396+
397+ let fwnode = dev.fwnode().map_or(ptr::null_mut(), FwNode::as_raw);
398+
399+ // SAFETY: `fwnode` is a pointer to a valid `fwnode_handle`. A null pointer will be
400+ // passed through the function.
401+ let adev = unsafe { bindings::to_acpi_device_node(fwnode) };
402+
403+ // SAFETY:
404+ // - `adev` is a valid pointer to `acpi_device` or is null. It is guaranteed to be
405+ // valid as long as `dev` is alive.
406+ // - `table` has static lifetime, hence it's valid for read.
407+ if unsafe { acpi_of_match_device(adev, table.as_ptr(), &raw mut raw_id) } {
408+ // SAFETY:
409+ // - the function returns true, therefore `raw_id` has been set to a pointer to a
410+ // valid `of_device_id`.
411+ // - `DeviceId` is a `#[repr(transparent)]` wrapper of `struct of_device_id`
412+ // and does not add additional invariants, so it's safe to transmute.
413+ let id = unsafe { &*raw_id.cast::<of::DeviceId>() };
414+
415+ return Some(table.info(
416+ <of::DeviceId as crate::device_id::RawDeviceIdIndex>::index(id),
417+ ));
359418 }
360419 }
420+
421+ None
361422 }
362423
363424 /// Returns the driver's private data from the matching entry of any of the ID tables, if any.
rust/kernel/i2c.rs+34−27
@@ -93,18 +93,18 @@ pub struct Adapter<T: Driver>(T); · 92 unmodified lines
9393
9494 // SAFETY:
9595 // - `bindings::i2c_driver` is a C type declared as `repr(C)`.
96−// - `T` is the type of the driver's device private data.
96+// - `T::Data` is the type of the driver's device private data.
9797 // - `struct i2c_driver` embeds a `struct device_driver`.
9898 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
99−unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
99+unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
100100 type DriverType = bindings::i2c_driver;
101− type DriverData = T;
101+ type DriverData<'bound> = T::Data<'bound>;
102102 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
103103 }
104104
105105 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
106106 // a preceding call to `register` has been successful.
107−unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
107+unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
108108 unsafe fn register(
109109 idrv: &Opaque<Self::DriverType>,
110110 name: &'static CStr,
@@ -151,13 +151,13 @@ unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { · 40 unmodified lines
151151 }
152152 }
153153
154−impl<T: Driver + 'static> Adapter<T> {
154+impl<T: Driver> Adapter<T> {
155155 extern "C" fn probe_callback(idev: *mut bindings::i2c_client) -> kernel::ffi::c_int {
156156 // SAFETY: The I2C bus only ever calls the probe callback with a valid pointer to a
157157 // `struct i2c_client`.
158158 //
159159 // INVARIANT: `idev` is valid for the duration of `probe_callback()`.
160− let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal>>() };
160+ let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal<'_>>>() };
161161
162162 let info =
163163 Self::i2c_id_info(idev).or_else(|| <Self as driver::Adapter>::id_info(idev.as_ref()));
@@ -172,24 +172,24 @@ impl<T: Driver + 'static> Adapter<T> { · 8 unmodified lines
172172
173173 extern "C" fn remove_callback(idev: *mut bindings::i2c_client) {
174174 // SAFETY: `idev` is a valid pointer to a `struct i2c_client`.
175− let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal>>() };
175+ let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal<'_>>>() };
176176
177177 // SAFETY: `remove_callback` is only ever called after a successful call to
178178 // `probe_callback`, hence it's guaranteed that `I2cClient::set_drvdata()` has been called
179− // and stored a `Pin<KBox<T>>`.
180− let data = unsafe { idev.as_ref().drvdata_borrow::<T>() };
179+ // and stored a `Pin<KBox<T::Data<'_>>>`.
180+ let data = unsafe { idev.as_ref().drvdata_borrow::<T::Data<'_>>() };
181181
182182 T::unbind(idev, data);
183183 }
184184
185185 extern "C" fn shutdown_callback(idev: *mut bindings::i2c_client) {
186186 // SAFETY: `shutdown_callback` is only ever called for a valid `idev`
187− let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal>>() };
187+ let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal<'_>>>() };
188188
189189 // SAFETY: `shutdown_callback` is only ever called after a successful call to
190190 // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
191− // and stored a `Pin<KBox<T>>`.
192− let data = unsafe { idev.as_ref().drvdata_borrow::<T>() };
191+ // and stored a `Pin<KBox<T::Data<'_>>>`.
192+ let data = unsafe { idev.as_ref().drvdata_borrow::<T::Data<'_>>() };
193193
194194 T::shutdown(idev, data);
195195 }
@@ -222,7 +222,7 @@ impl<T: Driver + 'static> Adapter<T> { · 26 unmodified lines
222222 }
223223 }
224224
225−impl<T: Driver + 'static> driver::Adapter for Adapter<T> {
225+impl<T: Driver> driver::Adapter for Adapter<T> {
226226 type IdInfo = T::IdInfo;
227227
228228 fn of_id_table() -> Option<of::IdTable<Self::IdInfo>> {
@@ -294,22 +294,26 @@ macro_rules! module_i2c_driver { · 65 unmodified lines
294294 ///
295295 /// impl i2c::Driver for MyDriver {
296296 /// type IdInfo = ();
297+/// type Data<'bound> = Self;
297298 /// const I2C_ID_TABLE: Option<i2c::IdTable<Self::IdInfo>> = Some(&I2C_TABLE);
298299 /// const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
299300 /// const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
300301 ///
301−/// fn probe(
302−/// _idev: &i2c::I2cClient<Core>,
303−/// _id_info: Option<&Self::IdInfo>,
304−/// ) -> impl PinInit<Self, Error> {
302+/// fn probe<'bound>(
303+/// _idev: &'bound i2c::I2cClient<Core<'_>>,
304+/// _id_info: Option<&'bound Self::IdInfo>,
305+/// ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
305306 /// Err(ENODEV)
306307 /// }
307308 ///
308−/// fn shutdown(_idev: &i2c::I2cClient<Core>, this: Pin<&Self>) {
309+/// fn shutdown<'bound>(
310+/// _idev: &'bound i2c::I2cClient<Core<'_>>,
311+/// this: Pin<&Self::Data<'bound>>,
312+/// ) {
309313 /// }
310314 /// }
311315 ///```
312−pub trait Driver: Send {
316+pub trait Driver {
313317 /// The type holding information about each device id supported by the driver.
314318 // TODO: Use `associated_type_defaults` once stabilized:
315319 //
@@ -318,6 +322,9 @@ pub trait Driver: Send { · 2 unmodified lines
318322 // ```
319323 type IdInfo: 'static;
320324
325+ /// The type of the driver's bus device private data.
326+ type Data<'bound>: Send + 'bound;
327+
321328 /// The table of device ids supported by the driver.
322329 const I2C_ID_TABLE: Option<IdTable<Self::IdInfo>> = None;
323330
@@ -331,10 +338,10 @@ pub trait Driver: Send { · 7 unmodified lines
331338 ///
332339 /// Called when a new i2c client is added or discovered.
333340 /// Implementers should attempt to initialize the client here.
334− fn probe(
335− dev: &I2cClient<device::Core>,
336− id_info: Option<&Self::IdInfo>,
337− ) -> impl PinInit<Self, Error>;
341+ fn probe<'bound>(
342+ dev: &'bound I2cClient<device::Core<'_>>,
343+ id_info: Option<&'bound Self::IdInfo>,
344+ ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound;
338345
339346 /// I2C driver shutdown.
340347 ///
@@ -346,8 +353,8 @@ pub trait Driver: Send { · 5 unmodified lines
346353 ///
347354 /// This callback is distinct from final resource cleanup, as the driver instance remains valid
348355 /// after it returns. Any deallocation or teardown of driver-owned resources should instead be
349− /// handled in `Self::drop`.
350− fn shutdown(dev: &I2cClient<device::Core>, this: Pin<&Self>) {
356+ /// handled in `Drop`.
357+ fn shutdown<'bound>(dev: &'bound I2cClient<device::Core<'_>>, this: Pin<&Self::Data<'bound>>) {
351358 let _ = (dev, this);
352359 }
353360
@@ -360,8 +367,8 @@ pub trait Driver: Send { · 6 unmodified lines
360367 /// `&Device<Core>` or `&Device<Bound>` reference. For instance, drivers may try to perform I/O
361368 /// operations to gracefully tear down the device.
362369 ///
363− /// Otherwise, release operations for driver resources should be performed in `Self::drop`.
364− fn unbind(dev: &I2cClient<device::Core>, this: Pin<&Self>) {
370+ /// Otherwise, release operations for driver resources should be performed in `Drop`.
371+ fn unbind<'bound>(dev: &'bound I2cClient<device::Core<'_>>, this: Pin<&Self::Data<'bound>>) {
365372 let _ = (dev, this);
366373 }
367374 }
rust/kernel/io/mem.rs+62−59
@@ -62,33 +62,31 @@ impl<'a> IoRequest<'a> { · 61 unmodified lines
6262 ///
6363 /// impl platform::Driver for SampleDriver {
6464 /// # type IdInfo = ();
65+ /// # type Data<'bound> = Self;
6566 ///
66− /// fn probe(
67− /// pdev: &platform::Device<Core>,
68− /// info: Option<&Self::IdInfo>,
69− /// ) -> impl PinInit<Self, Error> {
67+ /// fn probe<'bound>(
68+ /// pdev: &'bound platform::Device<Core<'_>>,
69+ /// info: Option<&'bound Self::IdInfo>,
70+ /// ) -> impl PinInit<Self, Error> + 'bound {
7071 /// let offset = 0; // Some offset.
7172 ///
7273 /// // If the size is known at compile time, use [`Self::iomap_sized`].
7374 /// //
7475 /// // No runtime checks will apply when reading and writing.
7576 /// let request = pdev.io_request_by_index(0).ok_or(ENODEV)?;
76− /// let iomem = request.iomap_sized::<42>();
77− /// let iomem = KBox::pin_init(iomem, GFP_KERNEL)?;
78− ///
79− /// let io = iomem.access(pdev.as_ref())?;
77+ /// let iomem = request.iomap_sized::<42>()?;
8078 ///
8179 /// // Read and write a 32-bit value at `offset`.
82− /// let data = io.read32(offset);
80+ /// let data = iomem.read32(offset);
8381 ///
84− /// io.write32(data, offset);
82+ /// iomem.write32(data, offset);
8583 ///
8684 /// # Ok(SampleDriver)
8785 /// }
8886 /// }
8987 /// ```
90− pub fn iomap_sized<const SIZE: usize>(self) -> impl PinInit<Devres<IoMem<SIZE>>, Error> + 'a {
91− IoMem::new(self)
88+ pub fn iomap_sized<const SIZE: usize>(self) -> Result<IoMem<'a, SIZE>> {
89+ IoMem::ioremap(self.device, self.resource)
9290 }
9391
9492 /// Same as [`Self::iomap_sized`] but with exclusive access to the
@@ -97,10 +95,8 @@ impl<'a> IoRequest<'a> { · 2 unmodified lines
9795 /// This uses the [`ioremap()`] C API.
9896 ///
9997 /// [`ioremap()`]: https://docs.kernel.org/driver-api/device-io.html#getting-access-to-the-device
100− pub fn iomap_exclusive_sized<const SIZE: usize>(
101− self,
102− ) -> impl PinInit<Devres<ExclusiveIoMem<SIZE>>, Error> + 'a {
103− ExclusiveIoMem::new(self)
98+ pub fn iomap_exclusive_sized<const SIZE: usize>(self) -> Result<ExclusiveIoMem<'a, SIZE>> {
99+ ExclusiveIoMem::ioremap(self.device, self.resource)
104100 }
105101
106102 /// Maps an [`IoRequest`] where the size is not known at compile time,
@@ -126,11 +122,12 @@ impl<'a> IoRequest<'a> { · 19 unmodified lines
126122 ///
127123 /// impl platform::Driver for SampleDriver {
128124 /// # type IdInfo = ();
125+ /// # type Data<'bound> = Self;
129126 ///
130− /// fn probe(
131− /// pdev: &platform::Device<Core>,
132− /// info: Option<&Self::IdInfo>,
133− /// ) -> impl PinInit<Self, Error> {
127+ /// fn probe<'bound>(
128+ /// pdev: &'bound platform::Device<Core<'_>>,
129+ /// info: Option<&'bound Self::IdInfo>,
130+ /// ) -> impl PinInit<Self, Error> + 'bound {
134131 /// let offset = 0; // Some offset.
135132 ///
136133 /// // Unlike [`Self::iomap_sized`], here the size of the memory region
@@ -138,27 +135,24 @@ impl<'a> IoRequest<'a> { · 1 unmodified line
138135 /// // family of functions should be used, leading to runtime checks on every
139136 /// // access.
140137 /// let request = pdev.io_request_by_index(0).ok_or(ENODEV)?;
141− /// let iomem = request.iomap();
142− /// let iomem = KBox::pin_init(iomem, GFP_KERNEL)?;
143− ///
144− /// let io = iomem.access(pdev.as_ref())?;
138+ /// let iomem = request.iomap()?;
145139 ///
146− /// let data = io.try_read32(offset)?;
140+ /// let data = iomem.try_read32(offset)?;
147141 ///
148− /// io.try_write32(data, offset)?;
142+ /// iomem.try_write32(data, offset)?;
149143 ///
150144 /// # Ok(SampleDriver)
151145 /// }
152146 /// }
153147 /// ```
154− pub fn iomap(self) -> impl PinInit<Devres<IoMem<0>>, Error> + 'a {
155− Self::iomap_sized::<0>(self)
148+ pub fn iomap(self) -> Result<IoMem<'a>> {
149+ self.iomap_sized::<0>()
156150 }
157151
158152 /// Same as [`Self::iomap`] but with exclusive access to the underlying
159153 /// region.
160− pub fn iomap_exclusive(self) -> impl PinInit<Devres<ExclusiveIoMem<0>>, Error> + 'a {
161− Self::iomap_exclusive_sized::<0>(self)
154+ pub fn iomap_exclusive(self) -> Result<ExclusiveIoMem<'a, 0>> {
155+ self.iomap_exclusive_sized::<0>()
162156 }
163157 }
164158
@@ -167,9 +161,9 @@ impl<'a> IoRequest<'a> { · 2 unmodified lines
167161 /// # Invariants
168162 ///
169163 /// - [`ExclusiveIoMem`] has exclusive access to the underlying [`IoMem`].
170−pub struct ExclusiveIoMem<const SIZE: usize> {
164+pub struct ExclusiveIoMem<'a, const SIZE: usize> {
171165 /// The underlying `IoMem` instance.
172− iomem: IoMem<SIZE>,
166+ iomem: IoMem<'a, SIZE>,
173167
174168 /// The region abstraction. This represents exclusive access to the
175169 /// range represented by the underlying `iomem`.
@@ -178,9 +172,9 @@ pub struct ExclusiveIoMem<const SIZE: usize> { · 2 unmodified lines
178172 _region: Region,
179173 }
180174
181−impl<const SIZE: usize> ExclusiveIoMem<SIZE> {
175+impl<'a, const SIZE: usize> ExclusiveIoMem<'a, SIZE> {
182176 /// Creates a new `ExclusiveIoMem` instance.
183− fn ioremap(resource: &Resource) -> Result<Self> {
177+ fn ioremap(dev: &'a Device<Bound>, resource: &Resource) -> Result<Self> {
184178 let start = resource.start();
185179 let size = resource.size();
186180 let name = resource.name().unwrap_or_default();
@@ -194,26 +188,29 @@ impl<const SIZE: usize> ExclusiveIoMem<SIZE> { · 7 unmodified lines
194188 )
195189 .ok_or(EBUSY)?;
196190
197− let iomem = IoMem::ioremap(resource)?;
191+ let iomem = IoMem::ioremap(dev, resource)?;
198192
199− let iomem = ExclusiveIoMem {
193+ Ok(ExclusiveIoMem {
200194 iomem,
201195 _region: region,
202− };
203−
204− Ok(iomem)
196+ })
205197 }
206198
207− /// Creates a new `ExclusiveIoMem` instance from a previously acquired [`IoRequest`].
208− pub fn new<'a>(io_request: IoRequest<'a>) -> impl PinInit<Devres<Self>, Error> + 'a {
209− let dev = io_request.device;
210− let res = io_request.resource;
211−
212− Devres::new(dev, Self::ioremap(res))
199+ /// Consume the `ExclusiveIoMem` and register it as a device-managed resource.
200+ ///
201+ /// The returned `Devres<ExclusiveIoMem<'static, SIZE>>` can outlive the original lifetime
202+ /// `'a`. Access to the I/O memory is revoked when the device is unbound.
203+ pub fn into_devres(self) -> Result<Devres<ExclusiveIoMem<'static, SIZE>>> {
204+ // SAFETY: Casting to `'static` is sound because `Devres` guarantees the
205+ // `ExclusiveIoMem` does not actually outlive the device -- access is revoked and the
206+ // resource is released when the device is unbound.
207+ let iomem: ExclusiveIoMem<'static, SIZE> = unsafe { core::mem::transmute(self) };
208+ let dev = iomem.iomem.dev;
209+ Devres::new(dev, iomem)
213210 }
214211 }
215212
216−impl<const SIZE: usize> Deref for ExclusiveIoMem<SIZE> {
213+impl<const SIZE: usize> Deref for ExclusiveIoMem<'_, SIZE> {
217214 type Target = Mmio<SIZE>;
218215
219216 fn deref(&self) -> &Self::Target {
@@ -230,12 +227,13 @@ impl<const SIZE: usize> Deref for ExclusiveIoMem<SIZE> { · 10 unmodified lines
230227 ///
231228 /// [`IoMem`] always holds an [`MmioRaw`] instance that holds a valid pointer to the
232229 /// start of the I/O memory mapped region.
233−pub struct IoMem<const SIZE: usize = 0> {
230+pub struct IoMem<'a, const SIZE: usize = 0> {
231+ dev: &'a Device<Bound>,
234232 io: MmioRaw<SIZE>,
235233 }
236234
237−impl<const SIZE: usize> IoMem<SIZE> {
238− fn ioremap(resource: &Resource) -> Result<Self> {
235+impl<'a, const SIZE: usize> IoMem<'a, SIZE> {
236+ fn ioremap(dev: &'a Device<Bound>, resource: &Resource) -> Result<Self> {
239237 // Note: Some ioremap() implementations use types that depend on the CPU
240238 // word width rather than the bus address width.
241239 //
@@ -267,28 +265,33 @@ impl<const SIZE: usize> IoMem<SIZE> { · 25 unmodified lines
267265 }
268266
269267 let io = MmioRaw::new(addr as usize, size)?;
270− let io = IoMem { io };
271268
272− Ok(io)
269+ Ok(IoMem { dev, io })
273270 }
274271
275− /// Creates a new `IoMem` instance from a previously acquired [`IoRequest`].
276− pub fn new<'a>(io_request: IoRequest<'a>) -> impl PinInit<Devres<Self>, Error> + 'a {
277− let dev = io_request.device;
278− let res = io_request.resource;
279−
280− Devres::new(dev, Self::ioremap(res))
272+ /// Consume the `IoMem` and register it as a device-managed resource.
273+ ///
274+ /// The returned `Devres<IoMem<'static, SIZE>>` can outlive the original
275+ /// lifetime `'a`. Access to the I/O memory is revoked when the device
276+ /// is unbound.
277+ pub fn into_devres(self) -> Result<Devres<IoMem<'static, SIZE>>> {
278+ // SAFETY: Casting to `'static` is sound because `Devres` guarantees the `IoMem` does not
279+ // actually outlive the device -- access is revoked and the resource is released when the
280+ // device is unbound.
281+ let iomem: IoMem<'static, SIZE> = unsafe { core::mem::transmute(self) };
282+ let dev = iomem.dev;
283+ Devres::new(dev, iomem)
281284 }
282285 }
283286
284−impl<const SIZE: usize> Drop for IoMem<SIZE> {
287+impl<const SIZE: usize> Drop for IoMem<'_, SIZE> {
285288 fn drop(&mut self) {
286289 // SAFETY: Safe as by the invariant of `Io`.
287290 unsafe { bindings::iounmap(self.io.addr() as *mut c_void) }
288291 }
289292 }
290293
291−impl<const SIZE: usize> Deref for IoMem<SIZE> {
294+impl<const SIZE: usize> Deref for IoMem<'_, SIZE> {
292295 type Target = Mmio<SIZE>;
293296
294297 fn deref(&self) -> &Self::Target {
rust/kernel/pci.rs+31−20
@@ -59,18 +59,18 @@ pub struct Adapter<T: Driver>(T); · 58 unmodified lines
5959
6060 // SAFETY:
6161 // - `bindings::pci_driver` is a C type declared as `repr(C)`.
62−// - `T` is the type of the driver's device private data.
62+// - `T::Data` is the type of the driver's device private data.
6363 // - `struct pci_driver` embeds a `struct device_driver`.
6464 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
65−unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
65+unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
6666 type DriverType = bindings::pci_driver;
67− type DriverData = T;
67+ type DriverData<'bound> = T::Data<'bound>;
6868 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
6969 }
7070
7171 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
7272 // a preceding call to `register` has been successful.
73−unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
73+unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
7474 unsafe fn register(
7575 pdrv: &Opaque<Self::DriverType>,
7676 name: &'static CStr,
@@ -96,7 +96,7 @@ unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { · 19 unmodified lines
9696 }
9797 }
9898
99−impl<T: Driver + 'static> Adapter<T> {
99+impl<T: Driver> Adapter<T> {
100100 extern "C" fn probe_callback(
101101 pdev: *mut bindings::pci_dev,
102102 id: *const bindings::pci_device_id,
@@ -105,7 +105,7 @@ impl<T: Driver + 'static> Adapter<T> { · 2 unmodified lines
105105 // `struct pci_dev`.
106106 //
107107 // INVARIANT: `pdev` is valid for the duration of `probe_callback()`.
108− let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal>>() };
108+ let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal<'_>>>() };
109109
110110 // SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `struct pci_device_id` and
111111 // does not add additional invariants, so it's safe to transmute.
@@ -125,12 +125,12 @@ impl<T: Driver + 'static> Adapter<T> { · 13 unmodified lines
125125 // `struct pci_dev`.
126126 //
127127 // INVARIANT: `pdev` is valid for the duration of `remove_callback()`.
128− let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal>>() };
128+ let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal<'_>>>() };
129129
130130 // SAFETY: `remove_callback` is only ever called after a successful call to
131131 // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
132− // and stored a `Pin<KBox<T>>`.
133− let data = unsafe { pdev.as_ref().drvdata_borrow::<T>() };
132+ // and stored a `Pin<KBox<T::Data<'_>>>`.
133+ let data = unsafe { pdev.as_ref().drvdata_borrow::<T::Data<'_>>() };
134134
135135 T::unbind(pdev, data);
136136 }
@@ -279,19 +279,20 @@ macro_rules! pci_device_table { · 142 unmodified lines
279279 ///
280280 /// impl pci::Driver for MyDriver {
281281 /// type IdInfo = ();
282+/// type Data<'bound> = Self;
282283 /// const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
283284 ///
284−/// fn probe(
285−/// _pdev: &pci::Device<Core>,
286−/// _id_info: &Self::IdInfo,
287−/// ) -> impl PinInit<Self, Error> {
285+/// fn probe<'bound>(
286+/// _pdev: &'bound pci::Device<Core<'_>>,
287+/// _id_info: &'bound Self::IdInfo,
288+/// ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
288289 /// Err(ENODEV)
289290 /// }
290291 /// }
291292 ///```
292293 /// Drivers must implement this trait in order to get a PCI driver registered. Please refer to the
293294 /// `Adapter` documentation for an example.
294−pub trait Driver: Send {
295+pub trait Driver {
295296 /// The type holding information about each device id supported by the driver.
296297 // TODO: Use `associated_type_defaults` once stabilized:
297298 //
@@ -300,6 +301,9 @@ pub trait Driver: Send { · 2 unmodified lines
300301 // ```
301302 type IdInfo: 'static;
302303
304+ /// The type of the driver's bus device private data.
305+ type Data<'bound>: Send + 'bound;
306+
303307 /// The table of device ids supported by the driver.
304308 const ID_TABLE: IdTable<Self::IdInfo>;
305309
@@ -307,7 +311,10 @@ pub trait Driver: Send { · 1 unmodified line
307311 ///
308312 /// Called when a new pci device is added or discovered. Implementers should
309313 /// attempt to initialize the device here.
310− fn probe(dev: &Device<device::Core>, id_info: &Self::IdInfo) -> impl PinInit<Self, Error>;
314+ fn probe<'bound>(
315+ dev: &'bound Device<device::Core<'_>>,
316+ id_info: &'bound Self::IdInfo,
317+ ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound;
311318
312319 /// PCI driver unbind.
313320 ///
@@ -318,8 +325,8 @@ pub trait Driver: Send { · 4 unmodified lines
318325 /// `&Device<Core>` or `&Device<Bound>` reference. For instance, drivers may try to perform I/O
319326 /// operations to gracefully tear down the device.
320327 ///
321− /// Otherwise, release operations for driver resources should be performed in `Self::drop`.
322− fn unbind(dev: &Device<device::Core>, this: Pin<&Self>) {
328+ /// Otherwise, release operations for driver resources should be performed in `Drop`.
329+ fn unbind<'bound>(dev: &'bound Device<device::Core<'_>>, this: Pin<&Self::Data<'bound>>) {
323330 let _ = (dev, this);
324331 }
325332 }
@@ -354,7 +361,7 @@ impl Device { · 28 unmodified lines
354361 ///
355362 /// ```
356363 /// # use kernel::{device::Core, pci::{self, Vendor}, prelude::*};
357− /// fn log_device_info(pdev: &pci::Device<Core>) -> Result {
364+ /// fn log_device_info(pdev: &pci::Device<Core<'_>>) -> Result {
358365 /// // Get an instance of `Vendor`.
359366 /// let vendor = pdev.vendor_id();
360367 /// dev_info!(
@@ -445,7 +452,7 @@ impl Device { · 84 unmodified lines
445452 }
446453 }
447454
448−impl Device<device::Core> {
455+impl<'a> Device<device::Core<'a>> {
449456 /// Enable memory resources for this device.
450457 pub fn enable_device_mem(&self) -> Result {
451458 // SAFETY: `self.as_raw` is guaranteed to be a pointer to a valid `struct pci_dev`.
@@ -471,7 +478,7 @@ unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for Device<Ctx> · 19 unmodified lines
471478 kernel::impl_device_context_deref!(unsafe { Device });
472479 kernel::impl_device_context_into_aref!(Device);
473480
474−impl crate::dma::Device for Device<device::Core> {}
481+impl<'a> crate::dma::Device<'a> for Device<device::Core<'a>> {}
475482
476483 // SAFETY: Instances of `Device` are always reference-counted.
477484 unsafe impl crate::sync::aref::AlwaysRefCounted for Device {
@@ -523,3 +530,7 @@ unsafe impl Send for Device {} · 45 unmodified lines
523530 // SAFETY: `Device` can be shared among threads because all methods of `Device`
524531 // (i.e. `Device<Normal>) are thread safe.
525532 unsafe impl Sync for Device {}
533+
534+// SAFETY: Same as `Device<Normal>` -- the underlying `struct pci_dev` is the same;
535+// `Bound` is a zero-sized type-state marker that does not affect thread safety.
536+unsafe impl Sync for Device<device::Bound> {}
rust/kernel/pci/id.rs+1−1
@@ -19,7 +19,7 @@ use crate::{ · 18 unmodified lines
1919 ///
2020 /// ```
2121 /// # use kernel::{device::Core, pci::{self, Class}, prelude::*};
22−/// fn probe_device(pdev: &pci::Device<Core>) -> Result {
22+/// fn probe_device(pdev: &pci::Device<Core<'_>>) -> Result {
2323 /// let pci_class = pdev.pci_class();
2424 /// dev_info!(
2525 /// pdev,
rust/kernel/pci/io.rs+31−23
@@ -14,8 +14,7 @@ use crate::{ · 13 unmodified lines
1414 Mmio,
1515 MmioRaw, //
1616 },
17− prelude::*,
18− sync::aref::ARef, //
17+ prelude::*, //
1918 };
2019 use core::{
2120 marker::PhantomData,
@@ -146,14 +145,18 @@ impl<'a, S: ConfigSpaceKind> IoKnownSize for ConfigSpace<'a, S> { · 124 unmodified lines
146145 ///
147146 /// `Bar` always holds an `IoRaw` instance that holds a valid pointer to the start of the I/O
148147 /// memory mapped PCI BAR and its size.
149−pub struct Bar<const SIZE: usize = 0> {
150− pdev: ARef<Device>,
148+pub struct Bar<'a, const SIZE: usize = 0> {
149+ pdev: &'a Device<device::Bound>,
151150 io: MmioRaw<SIZE>,
152151 num: i32,
153152 }
154153
155−impl<const SIZE: usize> Bar<SIZE> {
156− pub(super) fn new(pdev: &Device, num: u32, name: &CStr) -> Result<Self> {
154+impl<'a, const SIZE: usize> Bar<'a, SIZE> {
155+ pub(super) fn new(
156+ pdev: &'a Device<device::Bound>,
157+ num: u32,
158+ name: &'static CStr,
159+ ) -> Result<Self> {
157160 let len = pdev.resource_len(num)?;
158161 if len == 0 {
159162 return Err(ENOMEM);
@@ -196,11 +199,7 @@ impl<const SIZE: usize> Bar<SIZE> { · 36 unmodified lines
196199 }
197200 };
198201
199− Ok(Bar {
200− pdev: pdev.into(),
201− io,
202− num,
203− })
202+ Ok(Bar { pdev, io, num })
204203 }
205204
206205 /// # Safety
@@ -219,11 +218,24 @@ impl<const SIZE: usize> Bar<SIZE> { · 12 unmodified lines
219218
220219 fn release(&self) {
221220 // SAFETY: The safety requirements are guaranteed by the type invariant of `self.pdev`.
222− unsafe { Self::do_release(&self.pdev, self.io.addr(), self.num) };
221+ unsafe { Self::do_release(self.pdev, self.io.addr(), self.num) };
222+ }
223+
224+ /// Consume the `Bar` and register it as a device-managed resource.
225+ ///
226+ /// The returned `Devres<Bar<'static, SIZE>>` can outlive the original lifetime `'a`. Access
227+ /// to the BAR is revoked when the device is unbound.
228+ pub fn into_devres(self) -> Result<Devres<Bar<'static, SIZE>>> {
229+ // SAFETY: Casting to `'static` is sound because `Devres` guarantees the `Bar` does not
230+ // actually outlive the device -- access is revoked and the resource is released when the
231+ // device is unbound.
232+ let bar: Bar<'static, SIZE> = unsafe { core::mem::transmute(self) };
233+ let pdev = bar.pdev;
234+ Devres::new(pdev.as_ref(), bar)
223235 }
224236 }
225237
226−impl Bar {
238+impl Bar<'_> {
227239 #[inline]
228240 pub(super) fn index_is_valid(index: u32) -> bool {
229241 // A `struct pci_dev` owns an array of resources with at most `PCI_NUM_RESOURCES` entries.
@@ -231,13 +243,13 @@ impl Bar { · 1 unmodified line
231243 }
232244 }
233245
234−impl<const SIZE: usize> Drop for Bar<SIZE> {
246+impl<const SIZE: usize> Drop for Bar<'_, SIZE> {
235247 fn drop(&mut self) {
236248 self.release();
237249 }
238250 }
239251
240−impl<const SIZE: usize> Deref for Bar<SIZE> {
252+impl<const SIZE: usize> Deref for Bar<'_, SIZE> {
241253 type Target = Mmio<SIZE>;
242254
243255 fn deref(&self) -> &Self::Target {
@@ -252,17 +264,13 @@ impl Device<device::Bound> { · 8 unmodified lines
252264 pub fn iomap_region_sized<'a, const SIZE: usize>(
253265 &'a self,
254266 bar: u32,
255− name: &'a CStr,
256− ) -> impl PinInit<Devres<Bar<SIZE>>, Error> + 'a {
257− Devres::new(self.as_ref(), Bar::<SIZE>::new(self, bar, name))
267+ name: &'static CStr,
268+ ) -> Result<Bar<'a, SIZE>> {
269+ Bar::new(self, bar, name)
258270 }
259271
260272 /// Maps an entire PCI BAR after performing a region-request on it.
261− pub fn iomap_region<'a>(
262− &'a self,
263− bar: u32,
264− name: &'a CStr,
265− ) -> impl PinInit<Devres<Bar>, Error> + 'a {
273+ pub fn iomap_region<'a>(&'a self, bar: u32, name: &'static CStr) -> Result<Bar<'a>> {
266274 self.iomap_region_sized::<0>(bar, name)
267275 }
268276
rust/kernel/platform.rs+33−23
@@ -45,18 +45,18 @@ pub struct Adapter<T: Driver>(T); · 44 unmodified lines
4545
4646 // SAFETY:
4747 // - `bindings::platform_driver` is a C type declared as `repr(C)`.
48−// - `T` is the type of the driver's device private data.
48+// - `T::Data` is the type of the driver's device private data.
4949 // - `struct platform_driver` embeds a `struct device_driver`.
5050 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
51−unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
51+unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
5252 type DriverType = bindings::platform_driver;
53− type DriverData = T;
53+ type DriverData<'bound> = T::Data<'bound>;
5454 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
5555 }
5656
5757 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
5858 // a preceding call to `register` has been successful.
59−unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
59+unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
6060 unsafe fn register(
6161 pdrv: &Opaque<Self::DriverType>,
6262 name: &'static CStr,
@@ -82,7 +82,9 @@ unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { · 19 unmodified lines
8282 }
8383
8484 // SAFETY: `pdrv` is guaranteed to be a valid `DriverType`.
85− to_result(unsafe { bindings::__platform_driver_register(pdrv.get(), module.0) })
85+ to_result(unsafe {
86+ bindings::__platform_driver_register(pdrv.get(), module.0, name.as_char_ptr())
87+ })
8688 }
8789
8890 unsafe fn unregister(pdrv: &Opaque<Self::DriverType>) {
@@ -91,13 +93,13 @@ unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { · 2 unmodified lines
9193 }
9294 }
9395
94−impl<T: Driver + 'static> Adapter<T> {
96+impl<T: Driver> Adapter<T> {
9597 extern "C" fn probe_callback(pdev: *mut bindings::platform_device) -> kernel::ffi::c_int {
9698 // SAFETY: The platform bus only ever calls the probe callback with a valid pointer to a
9799 // `struct platform_device`.
98100 //
99101 // INVARIANT: `pdev` is valid for the duration of `probe_callback()`.
100− let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal>>() };
102+ let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal<'_>>>() };
101103 let info = <Self as driver::Adapter>::id_info(pdev.as_ref());
102104
103105 from_result(|| {
@@ -113,18 +115,18 @@ impl<T: Driver + 'static> Adapter<T> { · 9 unmodified lines
113115 // `struct platform_device`.
114116 //
115117 // INVARIANT: `pdev` is valid for the duration of `remove_callback()`.
116− let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal>>() };
118+ let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal<'_>>>() };
117119
118120 // SAFETY: `remove_callback` is only ever called after a successful call to
119121 // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
120− // and stored a `Pin<KBox<T>>`.
121− let data = unsafe { pdev.as_ref().drvdata_borrow::<T>() };
122+ // and stored a `Pin<KBox<T::Data<'_>>>`.
123+ let data = unsafe { pdev.as_ref().drvdata_borrow::<T::Data<'_>>() };
122124
123125 T::unbind(pdev, data);
124126 }
125127 }
126128
127−impl<T: Driver + 'static> driver::Adapter for Adapter<T> {
129+impl<T: Driver> driver::Adapter for Adapter<T> {
128130 type IdInfo = T::IdInfo;
129131
130132 fn of_id_table() -> Option<of::IdTable<Self::IdInfo>> {
@@ -192,18 +194,19 @@ macro_rules! module_platform_driver { · 61 unmodified lines
192194 ///
193195 /// impl platform::Driver for MyDriver {
194196 /// type IdInfo = ();
197+/// type Data<'bound> = Self;
195198 /// const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
196199 /// const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
197200 ///
198−/// fn probe(
199−/// _pdev: &platform::Device<Core>,
200−/// _id_info: Option<&Self::IdInfo>,
201−/// ) -> impl PinInit<Self, Error> {
201+/// fn probe<'bound>(
202+/// _pdev: &'bound platform::Device<Core<'_>>,
203+/// _id_info: Option<&'bound Self::IdInfo>,
204+/// ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
202205 /// Err(ENODEV)
203206 /// }
204207 /// }
205208 ///```
206−pub trait Driver: Send {
209+pub trait Driver {
207210 /// The type holding driver private data about each device id supported by the driver.
208211 // TODO: Use associated_type_defaults once stabilized:
209212 //
@@ -212,6 +215,9 @@ pub trait Driver: Send { · 2 unmodified lines
212215 // ```
213216 type IdInfo: 'static;
214217
218+ /// The type of the driver's bus device private data.
219+ type Data<'bound>: Send + 'bound;
220+
215221 /// The table of OF device ids supported by the driver.
216222 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = None;
217223
@@ -222,10 +228,10 @@ pub trait Driver: Send { · 4 unmodified lines
222228 ///
223229 /// Called when a new platform device is added or discovered.
224230 /// Implementers should attempt to initialize the device here.
225− fn probe(
226− dev: &Device<device::Core>,
227− id_info: Option<&Self::IdInfo>,
228− ) -> impl PinInit<Self, Error>;
231+ fn probe<'bound>(
232+ dev: &'bound Device<device::Core<'_>>,
233+ id_info: Option<&'bound Self::IdInfo>,
234+ ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound;
229235
230236 /// Platform driver unbind.
231237 ///
@@ -236,8 +242,8 @@ pub trait Driver: Send { · 4 unmodified lines
236242 /// `&Device<Core>` or `&Device<Bound>` reference. For instance, drivers may try to perform I/O
237243 /// operations to gracefully tear down the device.
238244 ///
239− /// Otherwise, release operations for driver resources should be performed in `Self::drop`.
240− fn unbind(dev: &Device<device::Core>, this: Pin<&Self>) {
245+ /// Otherwise, release operations for driver resources should be performed in `Drop`.
246+ fn unbind<'bound>(dev: &'bound Device<device::Core<'_>>, this: Pin<&Self::Data<'bound>>) {
241247 let _ = (dev, this);
242248 }
243249 }
@@ -509,7 +515,7 @@ impl Device<Bound> { · 265 unmodified lines
509515 kernel::impl_device_context_deref!(unsafe { Device });
510516 kernel::impl_device_context_into_aref!(Device);
511517
512−impl crate::dma::Device for Device<device::Core> {}
518+impl<'a> crate::dma::Device<'a> for Device<device::Core<'a>> {}
513519
514520 // SAFETY: Instances of `Device` are always reference-counted.
515521 unsafe impl crate::sync::aref::AlwaysRefCounted for Device {
@@ -561,3 +567,7 @@ unsafe impl Send for Device {} · 45 unmodified lines
561567 // SAFETY: `Device` can be shared among threads because all methods of `Device`
562568 // (i.e. `Device<Normal>) are thread safe.
563569 unsafe impl Sync for Device {}
570+
571+// SAFETY: Same as `Device<Normal>` -- the underlying `struct platform_device` is the same;
572+// `Bound` is a zero-sized type-state marker that does not affect thread safety.
573+unsafe impl Sync for Device<device::Bound> {}
rust/kernel/types.rs+10−2
@@ -11,6 +11,10 @@ use core::{ · 10 unmodified lines
1111 };
1212 use pin_init::{PinInit, Wrapper, Zeroable};
1313
14+#[doc(hidden)]
15+pub mod for_lt;
16+pub use for_lt::ForLt;
17+
1418 /// Used to transfer ownership to and from foreign (non-Rust) languages.
1519 ///
1620 /// Ownership is transferred from Rust to a foreign language by calling [`Self::into_foreign`] and
@@ -27,10 +31,14 @@ pub unsafe trait ForeignOwnable: Sized { · 10 unmodified lines
2731 const FOREIGN_ALIGN: usize;
2832
2933 /// Type used to immutably borrow a value that is currently foreign-owned.
30− type Borrowed<'a>;
34+ type Borrowed<'a>
35+ where
36+ Self: 'a;
3137
3238 /// Type used to mutably borrow a value that is currently foreign-owned.
33− type BorrowedMut<'a>;
39+ type BorrowedMut<'a>
40+ where
41+ Self: 'a;
3442
3543 /// Converts a Rust-owned object to a foreign-owned one.
3644 ///
rust/kernel/types/for_lt.rs+122−0
@@ -0,0 +1,122 @@
1+// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
3+//! Provide implementation and test of the `ForLt` trait and macro.
4+//!
5+//! This module is hidden and user should just use `ForLt!` directly.
6+
7+use core::marker::PhantomData;
8+
9+/// Representation of types generic over a lifetime.
10+///
11+/// The type must be covariant over the generic lifetime, i.e. the lifetime parameter
12+/// can be soundly shortened.
13+///
14+/// The lifetime involved must be covariant.
15+///
16+/// # Macro
17+///
18+/// It is not recommended to implement this trait directly. `ForLt!` macro is provided to obtain a
19+/// type that implements this trait.
20+///
21+/// The full syntax is
22+///
23+/// ```
24+/// # use kernel::types::ForLt;
25+/// # fn expect_lt<F: ForLt>() {}
26+/// # struct TypeThatUse<'a>(&'a ());
27+/// # expect_lt::<
28+/// ForLt!(for<'a> TypeThatUse<'a>)
29+/// # >();
30+/// ```
31+///
32+/// which gives a type so that `<ForLt!(for<'a> TypeThatUse<'a>) as ForLt>::Of<'b>`
33+/// is `TypeThatUse<'b>`.
34+///
35+/// You may also use a short-hand syntax which works similar to lifetime elision.
36+/// The macro also accepts types that do not involve a lifetime at all.
37+///
38+/// ```
39+/// # use kernel::types::ForLt;
40+/// # fn expect_lt<F: ForLt>() {}
41+/// # struct TypeThatUse<'a>(&'a ());
42+/// # expect_lt::<
43+/// ForLt!(TypeThatUse<'_>) // Equivalent to `ForLt!(for<'a> TypeThatUse<'a>)`.
44+/// # >();
45+/// # expect_lt::<
46+/// ForLt!(&u32) // Equivalent to `ForLt!(for<'a> &'a u32)`.
47+/// # >();
48+/// # expect_lt::<
49+/// ForLt!(u32) // Equivalent to `ForLt!(for<'a> u32)`.
50+/// # >();
51+/// ```
52+///
53+/// The macro will attempt to prove that the type is indeed covariant over the lifetime supplied.
54+/// When it cannot be syntactically proven, it will emit checks to ask the Rust compiler to prove
55+/// it.
56+///
57+/// ```ignore,compile_fail
58+/// # use kernel::types::ForLt;
59+/// # fn expect_lt<F: ForLt>() {}
60+/// # expect_lt::<
61+/// ForLt!(fn(&u32)) // Contravariant, will fail compilation.
62+/// # >();
63+/// ```
64+///
65+/// There is a limitation if the type refers to generic parameters; if the macro cannot prove the
66+/// covariance syntactically, the emitted checks will fail the compilation as it needs to refer to
67+/// the generic parameter but is in a separate item.
68+///
69+/// ```
70+/// # use kernel::types::ForLt;
71+/// fn expect_lt<F: ForLt>() {}
72+/// # #[allow(clippy::unnecessary_safety_comment, reason = "false positive")]
73+/// fn generic_fn<T: 'static>() {
74+/// // Syntactically proven by the macro
75+/// expect_lt::<ForLt!(&T)>();
76+/// // Syntactically proven by the macro
77+/// expect_lt::<ForLt!(&KBox<T>)>();
78+/// // Cannot be syntactically proven, need to check covariance of `KBox`
79+/// // expect_lt::<ForLt!(&KBox<&T>)>();
80+/// }
81+/// ```
82+///
83+/// # Safety
84+///
85+/// `Self::Of<'a>` must be covariant over the lifetime `'a`.
86+pub unsafe trait ForLt {
87+ /// The type parameterized by the lifetime.
88+ type Of<'a>: 'a;
89+
90+ /// Cast a reference to a shorter lifetime.
91+ #[inline(always)]
92+ fn cast_ref<'r, 'short: 'r, 'long: 'short>(long: &'r Self::Of<'long>) -> &'r Self::Of<'short> {
93+ // SAFETY: This is sound as this trait guarantees covariance.
94+ unsafe { core::mem::transmute(long) }
95+ }
96+}
97+pub use macros::ForLt;
98+
99+/// This is intended to be an "unsafe-to-refer-to" type.
100+///
101+/// Must only be used by the `ForLt!` macro.
102+///
103+/// `T` is the magic `dyn for<'a> WithLt<'a, TypeThatUse<'a>>` generated by macro.
104+///
105+/// `WF` is a type that the macro can use to assert some specific type is well-formed.
106+///
107+/// `N` is to provide the macro a place to emit arbitrary items, in case it needs to prove
108+/// additional properties.
109+#[doc(hidden)]
110+pub struct UnsafeForLtImpl<T: ?Sized, WF, const N: usize>(PhantomData<(WF, T)>);
111+
112+// This is a helper trait for implementation `ForLt` to be able to use HRTB.
113+#[doc(hidden)]
114+pub trait WithLt<'a> {
115+ type Of: 'a;
116+}
117+
118+// SAFETY: In `ForLt!` macro, a covariance proof is generated when naming `UnsafeForLtImpl`
119+// and it will fail to evaluate if the type is not covariant.
120+unsafe impl<T: ?Sized + for<'a> WithLt<'a>, WF> ForLt for UnsafeForLtImpl<T, WF, 0> {
121+ type Of<'a> = <T as WithLt<'a>>::Of;
122+}
rust/kernel/usb.rs+36−21
@@ -36,18 +36,18 @@ pub struct Adapter<T: Driver>(T); · 35 unmodified lines
3636
3737 // SAFETY:
3838 // - `bindings::usb_driver` is a C type declared as `repr(C)`.
39−// - `T` is the type of the driver's device private data.
39+// - `T::Data` is the type of the driver's device private data.
4040 // - `struct usb_driver` embeds a `struct device_driver`.
4141 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
42−unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
42+unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
4343 type DriverType = bindings::usb_driver;
44− type DriverData = T;
44+ type DriverData<'bound> = T::Data<'bound>;
4545 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
4646 }
4747
4848 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
4949 // a preceding call to `register` has been successful.
50−unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
50+unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
5151 unsafe fn register(
5252 udrv: &Opaque<Self::DriverType>,
5353 name: &'static CStr,
@@ -73,7 +73,7 @@ unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { · 19 unmodified lines
7373 }
7474 }
7575
76−impl<T: Driver + 'static> Adapter<T> {
76+impl<T: Driver> Adapter<T> {
7777 extern "C" fn probe_callback(
7878 intf: *mut bindings::usb_interface,
7979 id: *const bindings::usb_device_id,
@@ -82,7 +82,7 @@ impl<T: Driver + 'static> Adapter<T> { · 2 unmodified lines
8282 // `struct usb_interface` and `struct usb_device_id`.
8383 //
8484 // INVARIANT: `intf` is valid for the duration of `probe_callback()`.
85− let intf = unsafe { &*intf.cast::<Interface<device::CoreInternal>>() };
85+ let intf = unsafe { &*intf.cast::<Interface<device::CoreInternal<'_>>>() };
8686
8787 from_result(|| {
8888 // SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `struct usb_device_id` and
@@ -92,7 +92,7 @@ impl<T: Driver + 'static> Adapter<T> { · 3 unmodified lines
9292 let info = T::ID_TABLE.info(id.index());
9393 let data = T::probe(intf, id, info);
9494
95− let dev: &device::Device<device::CoreInternal> = intf.as_ref();
95+ let dev: &device::Device<device::CoreInternal<'_>> = intf.as_ref();
9696 dev.set_drvdata(data)?;
9797 Ok(0)
9898 })
@@ -103,14 +103,14 @@ impl<T: Driver + 'static> Adapter<T> { · 4 unmodified lines
103103 // `struct usb_interface`.
104104 //
105105 // INVARIANT: `intf` is valid for the duration of `disconnect_callback()`.
106− let intf = unsafe { &*intf.cast::<Interface<device::CoreInternal>>() };
106+ let intf = unsafe { &*intf.cast::<Interface<device::CoreInternal<'_>>>() };
107107
108− let dev: &device::Device<device::CoreInternal> = intf.as_ref();
108+ let dev: &device::Device<device::CoreInternal<'_>> = intf.as_ref();
109109
110110 // SAFETY: `disconnect_callback` is only ever called after a successful call to
111111 // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
112− // and stored a `Pin<KBox<T>>`.
113− let data = unsafe { dev.drvdata_borrow::<T>() };
112+ // and stored a `Pin<KBox<T::Data<'_>>>`.
113+ let data = unsafe { dev.drvdata_borrow::<T::Data<'_>>() };
114114
115115 T::disconnect(intf, data);
116116 }
@@ -287,23 +287,31 @@ macro_rules! usb_device_table { · 170 unmodified lines
287287 ///
288288 /// impl usb::Driver for MyDriver {
289289 /// type IdInfo = ();
290+/// type Data<'bound> = Self;
290291 /// const ID_TABLE: usb::IdTable<Self::IdInfo> = &USB_TABLE;
291292 ///
292−/// fn probe(
293−/// _interface: &usb::Interface<Core>,
293+/// fn probe<'bound>(
294+/// _interface: &'bound usb::Interface<Core<'_>>,
294295 /// _id: &usb::DeviceId,
295−/// _info: &Self::IdInfo,
296−/// ) -> impl PinInit<Self, Error> {
296+/// _info: &'bound Self::IdInfo,
297+/// ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
297298 /// Err(ENODEV)
298299 /// }
299300 ///
300−/// fn disconnect(_interface: &usb::Interface<Core>, _data: Pin<&Self>) {}
301+/// fn disconnect<'bound>(
302+/// _interface: &'bound usb::Interface<Core<'_>>,
303+/// _data: Pin<&Self::Data<'bound>>,
304+/// ) {
305+/// }
301306 /// }
302307 ///```
303308 pub trait Driver {
304309 /// The type holding information about each one of the device ids supported by the driver.
305310 type IdInfo: 'static;
306311
312+ /// The type of the driver's bus device private data.
313+ type Data<'bound>: Send + 'bound;
314+
307315 /// The table of device ids supported by the driver.
308316 const ID_TABLE: IdTable<Self::IdInfo>;
309317
@@ -311,16 +319,19 @@ pub trait Driver { · 1 unmodified line
311319 ///
312320 /// Called when a new USB interface is bound to this driver.
313321 /// Implementers should attempt to initialize the interface here.
314− fn probe(
315− interface: &Interface<device::Core>,
322+ fn probe<'bound>(
323+ interface: &'bound Interface<device::Core<'_>>,
316324 id: &DeviceId,
317− id_info: &Self::IdInfo,
318− ) -> impl PinInit<Self, Error>;
325+ id_info: &'bound Self::IdInfo,
326+ ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound;
319327
320328 /// USB driver disconnect.
321329 ///
322330 /// Called when the USB interface is about to be unbound from this driver.
323− fn disconnect(interface: &Interface<device::Core>, data: Pin<&Self>);
331+ fn disconnect<'bound>(
332+ interface: &'bound Interface<device::Core<'_>>,
333+ data: Pin<&Self::Data<'bound>>,
334+ );
324335 }
325336
326337 /// A USB interface.
@@ -464,6 +475,10 @@ unsafe impl Send for Device {} · 137 unmodified lines
464475 // allow any mutation through a shared reference.
465476 unsafe impl Sync for Device {}
466477
478+// SAFETY: Same as `Device<Normal>` -- the underlying `struct usb_device` is the same;
479+// `Bound` is a zero-sized type-state marker that does not affect thread safety.
480+unsafe impl Sync for Device<device::Bound> {}
481+
467482 /// Declares a kernel module that exposes a single USB driver.
468483 ///
469484 /// # Examples
rust/macros/for_lt.rs+248−0
@@ -0,0 +1,248 @@
1+// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
3+use proc_macro2::{
4+ Span,
5+ TokenStream, //
6+};
7+use quote::{
8+ format_ident,
9+ quote, //
10+};
11+use syn::{
12+ parse::{
13+ Parse,
14+ ParseStream, //
15+ },
16+ visit::Visit,
17+ visit_mut::VisitMut,
18+ Lifetime,
19+ Result,
20+ Token,
21+ Type, //
22+};
23+
24+pub(crate) enum HigherRankedType {
25+ Explicit {
26+ _for_token: Token![for],
27+ _lt_token: Token![<],
28+ lifetime: Lifetime,
29+ _gt_token: Token![>],
30+ ty: Type,
31+ },
32+ Implicit {
33+ ty: Type,
34+ },
35+}
36+
37+impl Parse for HigherRankedType {
38+ fn parse(input: ParseStream<'_>) -> Result<Self> {
39+ if input.peek(Token![for]) {
40+ Ok(Self::Explicit {
41+ _for_token: input.parse()?,
42+ _lt_token: input.parse()?,
43+ lifetime: input.parse()?,
44+ _gt_token: input.parse()?,
45+ ty: input.parse()?,
46+ })
47+ } else {
48+ Ok(Self::Implicit { ty: input.parse()? })
49+ }
50+ }
51+}
52+
53+trait TypeExt {
54+ fn expand_elided_lifetime(&self, explicit_lt: &Lifetime) -> Type;
55+ fn replace_lifetime(&self, src: &Lifetime, dst: &Lifetime) -> Type;
56+ fn has_lifetime(&self, lt: &Lifetime) -> bool;
57+}
58+
59+impl TypeExt for Type {
60+ fn expand_elided_lifetime(&self, explicit_lt: &Lifetime) -> Type {
61+ struct ElidedLifetimeExpander<'a>(&'a Lifetime);
62+
63+ impl VisitMut for ElidedLifetimeExpander<'_> {
64+ fn visit_lifetime_mut(&mut self, lifetime: &mut Lifetime) {
65+ // Expand explicit `'_`
66+ if lifetime.ident == "_" {
67+ *lifetime = self.0.clone();
68+ }
69+ }
70+
71+ fn visit_type_reference_mut(&mut self, reference: &mut syn::TypeReference) {
72+ syn::visit_mut::visit_type_reference_mut(self, reference);
73+
74+ if reference.lifetime.is_none() {
75+ reference.lifetime = Some(self.0.clone());
76+ }
77+ }
78+ }
79+
80+ let mut ret = self.clone();
81+ ElidedLifetimeExpander(explicit_lt).visit_type_mut(&mut ret);
82+ ret
83+ }
84+
85+ fn replace_lifetime(&self, src: &Lifetime, dst: &Lifetime) -> Type {
86+ struct LifetimeReplacer<'a>(&'a Lifetime, &'a Lifetime);
87+
88+ impl VisitMut for LifetimeReplacer<'_> {
89+ fn visit_lifetime_mut(&mut self, lifetime: &mut Lifetime) {
90+ if lifetime.ident == self.0.ident {
91+ *lifetime = self.1.clone();
92+ }
93+ }
94+ }
95+
96+ let mut ret = self.clone();
97+ LifetimeReplacer(src, dst).visit_type_mut(&mut ret);
98+ ret
99+ }
100+
101+ fn has_lifetime(&self, lt: &Lifetime) -> bool {
102+ struct HasLifetime<'a>(&'a Lifetime, bool);
103+
104+ impl Visit<'_> for HasLifetime<'_> {
105+ fn visit_lifetime(&mut self, lifetime: &Lifetime) {
106+ if lifetime.ident == self.0.ident {
107+ self.1 = true;
108+ }
109+ }
110+
111+ // Macro invocations are opaque; conservatively assume they may
112+ // reference the lifetime.
113+ fn visit_macro(&mut self, _: &syn::Macro) {
114+ self.1 = true;
115+ }
116+ }
117+
118+ let mut visitor = HasLifetime(lt, false);
119+ visitor.visit_type(self);
120+ visitor.1
121+ }
122+}
123+
124+struct Prover<'a>(&'a Lifetime, Vec<&'a Type>);
125+
126+impl<'a> Prover<'a> {
127+ /// Prove that `ty` is covariant over `'lt`.
128+ ///
129+ /// This also needs to prove that it'll be wellformed for any instance of `'lt`.
130+ /// It can be assumed that `ty` will be wellformed if `'lt` is substituted to `'static`.
131+ fn prove(&mut self, ty: &'a Type) {
132+ match ty {
133+ Type::Paren(ty) => self.prove(&ty.elem),
134+ Type::Group(ty) => self.prove(&ty.elem),
135+
136+ // No lifetime involved
137+ Type::Never(_) => {}
138+
139+ // `[T; N]` and `[T]` is covariant over `T`.
140+ Type::Array(ty) => self.prove(&ty.elem),
141+ Type::Slice(ty) => self.prove(&ty.elem),
142+
143+ Type::Tuple(ty) => {
144+ for elem in &ty.elems {
145+ self.prove(elem);
146+ }
147+ }
148+
149+ // `*const T` is covariant over `T`
150+ Type::Ptr(ty) if ty.const_token.is_some() => self.prove(&ty.elem),
151+
152+ // `&T` is covariant over `T` and lifetime.
153+ //
154+ // Note that if we encounter `&'other_lt T`, then we still need to make sure the type
155+ // is wellformed if `T` involves `&'lt`, so we defer to the compiler.
156+ //
157+ // This is to block cases like `ForLt!(for<'a> &'static &'a u32)`, as the presence of
158+ // the type implies `'a: 'static` but this is unsound.
159+ Type::Reference(ty)
160+ if ty.mutability.is_none() && ty.lifetime.as_ref() == Some(self.0) =>
161+ {
162+ self.prove(&ty.elem)
163+ }
164+
165+ // `&[mut] T` is covariant over lifetime.
166+ // In case we have `&[mut] NoLifetime`, we don't need to do additional checks.
167+ Type::Reference(ty) if !ty.elem.has_lifetime(self.0) => (),
168+
169+ // No mention of lifetime at all, no need to perform compiler check.
170+ ty if !ty.has_lifetime(self.0) => (),
171+
172+ // Otherwise, we need to emit checks so that compiler can determine if the types are
173+ // actually covariant.
174+ ty => self.1.push(ty),
175+ }
176+ }
177+}
178+
179+pub(crate) fn for_lt(input: HigherRankedType) -> TokenStream {
180+ let (ty, lifetime) = match input {
181+ HigherRankedType::Explicit { lifetime, ty, .. } => (ty, lifetime),
182+ HigherRankedType::Implicit { ty } => {
183+ // If there's no explicit `for<'a>` binder, inject a synthetic `'__elided` lifetime
184+ // and expand elided sites.
185+ let lifetime = Lifetime {
186+ apostrophe: Span::mixed_site(),
187+ ident: format_ident!("__elided", span = Span::mixed_site()),
188+ };
189+ (ty.expand_elided_lifetime(&lifetime), lifetime)
190+ }
191+ };
192+
193+ let mut prover = Prover(&lifetime, Vec::new());
194+ prover.prove(&ty);
195+
196+ let mut proof = Vec::new();
197+
198+ // Emit proofs for every type that requires additional compiler help in proving covariance.
199+ for (idx, required_proof) in prover.1.into_iter().enumerate() {
200+ // Insert a proof that the type is well-formed.
201+ //
202+ // This is intended to workaround a Rust compiler soundness bug related to HRTB.
203+ // https://github.com/rust-lang/rust/issues/152489
204+ //
205+ // This needs to be a struct instead of fn to avoid the implied WF bounds.
206+ let wf_proof_name = format_ident!("ProveWf{idx}");
207+ proof.push(quote!(
208+ struct #wf_proof_name<#lifetime>(
209+ ::core::marker::PhantomData<&#lifetime ()>, #required_proof
210+ );
211+ ));
212+
213+ // Insert a proof that the type is covariant.
214+ let cov_proof_name = format_ident!("prove_covariant_{idx}");
215+ proof.push(quote!(
216+ fn #cov_proof_name<'__short, '__long: '__short>(
217+ long: #wf_proof_name<'__long>
218+ ) -> #wf_proof_name<'__short> {
219+ long
220+ }
221+ ));
222+ }
223+
224+ // Make sure that the type is wellformed when substituting lifetime with `'static`.
225+ //
226+ // Currently the Rust compiler doesn't check this, see the above `ProveWf` documentation.
227+ //
228+ // We prefer to use this way of proving WF-ness as it can work when generics are involved.
229+ let ty_static = ty.replace_lifetime(
230+ &lifetime,
231+ &Lifetime {
232+ apostrophe: Span::mixed_site(),
233+ ident: format_ident!("static"),
234+ },
235+ );
236+
237+ quote!(
238+ ::kernel::types::for_lt::UnsafeForLtImpl::<
239+ dyn for<#lifetime> ::kernel::types::for_lt::WithLt<#lifetime, Of = #ty>,
240+ #ty_static,
241+ {
242+ #(#proof)*
243+
244+ 0
245+ }
246+ >
247+ )
248+}
rust/macros/lib.rs+13−0
@@ -17,6 +17,7 @@ · 16 unmodified lines
1717 mod concat_idents;
1818 mod export;
1919 mod fmt;
20+mod for_lt;
2021 mod helpers;
2122 mod kunit;
2223 mod module;
@@ -489,3 +490,15 @@ pub fn kunit_tests(attr: TokenStream, input: TokenStream) -> TokenStream { · 466 unmodified lines
489490 .unwrap_or_else(|e| e.into_compile_error())
490491 .into()
491492 }
493+
494+/// Obtain a type that implements [`ForLt`] for the given higher-ranked type.
495+///
496+/// Please refer to the documentation of the [`ForLt`] trait.
497+///
498+/// [`ForLt`]: trait.ForLt.html
499+#[proc_macro]
500+// The macro shares the name with the trait.
501+#[allow(non_snake_case)]
502+pub fn ForLt(input: TokenStream) -> TokenStream {
503+ for_lt::for_lt(parse_macro_input!(input)).into()
504+}
samples/rust/rust_debugfs.rs+6−5
@@ -117,13 +117,14 @@ kernel::acpi_device_table!( · 116 unmodified lines
117117
118118 impl platform::Driver for RustDebugFs {
119119 type IdInfo = ();
120+ type Data<'bound> = Self;
120121 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = None;
121122 const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
122123
123− fn probe(
124− pdev: &platform::Device<Core>,
125− _info: Option<&Self::IdInfo>,
126− ) -> impl PinInit<Self, Error> {
124+ fn probe<'bound>(
125+ pdev: &'bound platform::Device<Core<'_>>,
126+ _info: Option<&'bound Self::IdInfo>,
127+ ) -> impl PinInit<Self, Error> + 'bound {
127128 RustDebugFs::new(pdev).pin_chain(|this| {
128129 this.counter.store(91, Relaxed);
129130 {
@@ -146,7 +147,7 @@ impl RustDebugFs { · 16 unmodified lines
146147 dir.read_write_file(c"pair", new_mutex!(Inner { x: 3, y: 10 }))
147148 }
148149
149− fn new(pdev: &platform::Device<Core>) -> impl PinInit<Self, Error> + '_ {
150+ fn new<'a>(pdev: &'a platform::Device<Core<'_>>) -> impl PinInit<Self, Error> + 'a {
150151 let debugfs = Dir::new(c"sample_debugfs");
151152 let dev = pdev.as_ref();
152153
samples/rust/rust_dma.rs+5−1
@@ -58,9 +58,13 @@ kernel::pci_device_table!( · 57 unmodified lines
5858
5959 impl pci::Driver for DmaSampleDriver {
6060 type IdInfo = ();
61+ type Data<'bound> = Self;
6162 const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
6263
63− fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> {
64+ fn probe<'bound>(
65+ pdev: &'bound pci::Device<Core<'_>>,
66+ _info: &'bound Self::IdInfo,
67+ ) -> impl PinInit<Self, Error> + 'bound {
6468 pin_init::pin_init_scope(move || {
6569 dev_info!(pdev, "Probe DMA test driver.\n");
6670
samples/rust/rust_driver_auxiliary.rs+56−23
@@ -10,15 +10,13 @@ use kernel::{ · 9 unmodified lines
1010 Bound,
1111 Core, //
1212 },
13− devres::Devres,
1413 driver,
1514 pci,
1615 prelude::*,
16+ types::ForLt,
1717 InPlaceModule, //
1818 };
1919
20−use core::any::TypeId;
21−
2220 const MODULE_NAME: &CStr = <LocalModule as kernel::ModuleMetadata>::NAME;
2321 const AUXILIARY_NAME: &CStr = c"auxiliary";
2422
@@ -33,10 +31,14 @@ kernel::auxiliary_device_table!( · 8 unmodified lines
3331
3432 impl auxiliary::Driver for AuxiliaryDriver {
3533 type IdInfo = ();
34+ type Data<'bound> = Self;
3635
3736 const ID_TABLE: auxiliary::IdTable<Self::IdInfo> = &AUX_TABLE;
3837
39− fn probe(adev: &auxiliary::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> {
38+ fn probe<'bound>(
39+ adev: &'bound auxiliary::Device<Core<'_>>,
40+ _info: &'bound Self::IdInfo,
41+ ) -> impl PinInit<Self, Error> + 'bound {
4042 dev_info!(
4143 adev,
4244 "Probing auxiliary driver for auxiliary device with id={}\n",
@@ -49,13 +51,17 @@ impl auxiliary::Driver for AuxiliaryDriver { · 6 unmodified lines
4951 }
5052 }
5153
52−#[pin_data]
53−struct ParentDriver {
54− private: TypeId,
55− #[pin]
56− _reg0: Devres<auxiliary::Registration>,
57− #[pin]
58− _reg1: Devres<auxiliary::Registration>,
54+struct Data<'bound> {
55+ index: u32,
56+ parent: &'bound pci::Device<Bound>,
57+}
58+
59+struct ParentDriver;
60+
61+#[allow(clippy::type_complexity)]
62+struct ParentData<'bound> {
63+ _reg0: auxiliary::Registration<'bound, ForLt!(Data<'_>)>,
64+ _reg1: auxiliary::Registration<'bound, ForLt!(Data<'_>)>,
5965 }
6066
6167 kernel::pci_device_table!(
@@ -67,26 +73,53 @@ kernel::pci_device_table!( · 5 unmodified lines
6773
6874 impl pci::Driver for ParentDriver {
6975 type IdInfo = ();
76+ type Data<'bound> = ParentData<'bound>;
7077
7178 const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
7279
73− fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> {
74− try_pin_init!(Self {
75− private: TypeId::of::<Self>(),
76− _reg0 <- auxiliary::Registration::new(pdev.as_ref(), AUXILIARY_NAME, 0, MODULE_NAME),
77− _reg1 <- auxiliary::Registration::new(pdev.as_ref(), AUXILIARY_NAME, 1, MODULE_NAME),
80+ fn probe<'bound>(
81+ pdev: &'bound pci::Device<Core<'_>>,
82+ _info: &'bound Self::IdInfo,
83+ ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
84+ Ok(ParentData {
85+ // SAFETY: `ParentData` is the driver's private data, which is dropped when the
86+ // device is unbound; i.e. `mem::forget()` is never called on it.
87+ _reg0: unsafe {
88+ auxiliary::Registration::new_with_lt(
89+ pdev.as_ref(),
90+ AUXILIARY_NAME,
91+ 0,
92+ MODULE_NAME,
93+ Data {
94+ index: 0,
95+ parent: pdev,
96+ },
97+ )?
98+ },
99+ // SAFETY: See `_reg0` above.
100+ _reg1: unsafe {
101+ auxiliary::Registration::new_with_lt(
102+ pdev.as_ref(),
103+ AUXILIARY_NAME,
104+ 1,
105+ MODULE_NAME,
106+ Data {
107+ index: 1,
108+ parent: pdev,
109+ },
110+ )?
111+ },
78112 })
79113 }
80114 }
81115
82116 impl ParentDriver {
83117 fn connect(adev: &auxiliary::Device<Bound>) -> Result {
84− let dev = adev.parent();
85− let pdev: &pci::Device<Bound> = dev.try_into()?;
86− let drvdata = dev.drvdata::<Self>()?;
118+ let data = adev.registration_data::<ForLt!(Data<'_>)>()?;
119+ let pdev = data.parent;
87120
88121 dev_info!(
89− dev,
122+ pdev,
90123 "Connect auxiliary {} with parent: VendorID={}, DeviceID={:#x}\n",
91124 adev.id(),
92125 pdev.vendor_id(),
@@ -94,9 +127,9 @@ impl ParentDriver { · 1 unmodified line
94127 );
95128
96129 dev_info!(
97− dev,
98− "We have access to the private data of {:?}.\n",
99− drvdata.private
130+ pdev,
131+ "Connected to auxiliary device with index {}.\n",
132+ data.index
100133 );
101134
102135 Ok(())
samples/rust/rust_driver_i2c.rs+7−6
@@ -35,15 +35,16 @@ kernel::of_device_table! { · 34 unmodified lines
3535
3636 impl i2c::Driver for SampleDriver {
3737 type IdInfo = u32;
38+ type Data<'bound> = Self;
3839
3940 const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
4041 const I2C_ID_TABLE: Option<i2c::IdTable<Self::IdInfo>> = Some(&I2C_TABLE);
4142 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
4243
43− fn probe(
44− idev: &i2c::I2cClient<Core>,
45− info: Option<&Self::IdInfo>,
46− ) -> impl PinInit<Self, Error> {
44+ fn probe<'bound>(
45+ idev: &'bound i2c::I2cClient<Core<'_>>,
46+ info: Option<&'bound Self::IdInfo>,
47+ ) -> impl PinInit<Self, Error> + 'bound {
4748 let dev = idev.as_ref();
4849
4950 dev_info!(dev, "Probe Rust I2C driver sample.\n");
@@ -55,11 +56,11 @@ impl i2c::Driver for SampleDriver { · 5 unmodified lines
5556 Ok(Self)
5657 }
5758
58− fn shutdown(idev: &i2c::I2cClient<Core>, _this: Pin<&Self>) {
59+ fn shutdown<'bound>(idev: &'bound i2c::I2cClient<Core<'_>>, _this: Pin<&Self>) {
5960 dev_info!(idev.as_ref(), "Shutdown Rust I2C driver sample.\n");
6061 }
6162
62− fn unbind(idev: &i2c::I2cClient<Core>, _this: Pin<&Self>) {
63+ fn unbind<'bound>(idev: &'bound i2c::I2cClient<Core<'_>>, _this: Pin<&Self>) {
6364 dev_info!(idev.as_ref(), "Unbind Rust I2C driver sample.\n");
6465 }
6566 }
samples/rust/rust_driver_pci.rs+43−47
@@ -9,7 +9,6 @@ use kernel::{ · 8 unmodified lines
99 Bound,
1010 Core, //
1111 },
12− devres::Devres,
1312 io::{
1413 register,
1514 register::Array,
@@ -17,8 +16,7 @@ use kernel::{ · 1 unmodified line
1716 },
1817 num::Bounded,
1918 pci,
20− prelude::*,
21− sync::aref::ARef, //
19+ prelude::*, //
2220 };
2321
2422 mod regs {
@@ -45,7 +43,7 @@ mod regs { · 20 unmodified lines
4543 pub(super) const END: usize = 0x10;
4644 }
4745
48−type Bar0 = pci::Bar<{ regs::END }>;
46+type Bar0<'bound> = pci::Bar<'bound, { regs::END }>;
4947
5048 #[derive(Copy, Clone, Debug)]
5149 struct TestIndex(u8);
@@ -66,14 +64,14 @@ impl TestIndex { · 14 unmodified lines
6664 const NO_EVENTFD: Self = Self(0);
6765 }
6866
69−#[pin_data(PinnedDrop)]
70−struct SampleDriver {
71− pdev: ARef<pci::Device>,
72− #[pin]
73− bar: Devres<Bar0>,
67+struct SampleDriverData<'bound> {
68+ pdev: &'bound pci::Device,
69+ bar: Bar0<'bound>,
7470 index: TestIndex,
7571 }
7672
73+struct SampleDriver;
74+
7775 kernel::pci_device_table!(
7876 PCI_TABLE,
7977 MODULE_PCI_TABLE,
@@ -84,8 +82,8 @@ kernel::pci_device_table!( · 4 unmodified lines
8482 )]
8583 );
8684
87−impl SampleDriver {
88− fn testdev(index: &TestIndex, bar: &Bar0) -> Result<u32> {
85+impl SampleDriverData<'_> {
86+ fn testdev(index: &TestIndex, bar: &Bar0<'_>) -> Result<u32> {
8987 // Select the test.
9088 bar.write_reg(regs::TEST::zeroed().with_index(*index));
9189
@@ -140,51 +138,49 @@ impl SampleDriver { · 48 unmodified lines
140138
141139 impl pci::Driver for SampleDriver {
142140 type IdInfo = TestIndex;
141+ type Data<'bound> = SampleDriverData<'bound>;
143142
144143 const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
145144
146− fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> impl PinInit<Self, Error> {
147− pin_init::pin_init_scope(move || {
148− let vendor = pdev.vendor_id();
149− dev_dbg!(
150− pdev,
151− "Probe Rust PCI driver sample (PCI ID: {}, 0x{:x}).\n",
152− vendor,
153− pdev.device_id()
154− );
155−
156− pdev.enable_device_mem()?;
157− pdev.set_master();
158−
159− Ok(try_pin_init!(Self {
160− bar <- pdev.iomap_region_sized::<{ regs::END }>(0, c"rust_driver_pci"),
161− index: *info,
162− _: {
163− let bar = bar.access(pdev.as_ref())?;
164−
165− dev_info!(
166− pdev,
167− "pci-testdev data-match count: {}\n",
168− Self::testdev(info, bar)?
169− );
170− Self::config_space(pdev);
171− },
172− pdev: pdev.into(),
173− }))
145+ fn probe<'bound>(
146+ pdev: &'bound pci::Device<Core<'_>>,
147+ info: &'bound Self::IdInfo,
148+ ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
149+ let vendor = pdev.vendor_id();
150+ dev_dbg!(
151+ pdev,
152+ "Probe Rust PCI driver sample (PCI ID: {}, 0x{:x}).\n",
153+ vendor,
154+ pdev.device_id()
155+ );
156+
157+ pdev.enable_device_mem()?;
158+ pdev.set_master();
159+
160+ let bar = pdev.iomap_region_sized::<{ regs::END }>(0, c"rust_driver_pci")?;
161+
162+ dev_info!(
163+ pdev,
164+ "pci-testdev data-match count: {}\n",
165+ SampleDriverData::testdev(info, &bar)?
166+ );
167+ SampleDriverData::config_space(pdev);
168+
169+ Ok(SampleDriverData {
170+ pdev,
171+ bar,
172+ index: *info,
174173 })
175174 }
176175
177− fn unbind(pdev: &pci::Device<Core>, this: Pin<&Self>) {
178− if let Ok(bar) = this.bar.access(pdev.as_ref()) {
179− // Reset pci-testdev by writing a new test index.
180− bar.write_reg(regs::TEST::zeroed().with_index(this.index));
181− }
176+ fn unbind<'bound>(_pdev: &'bound pci::Device<Core<'_>>, this: Pin<&Self::Data<'bound>>) {
177+ this.bar
178+ .write_reg(regs::TEST::zeroed().with_index(this.index));
182179 }
183180 }
184181
185−#[pinned_drop]
186−impl PinnedDrop for SampleDriver {
187− fn drop(self: Pin<&mut Self>) {
182+impl Drop for SampleDriverData<'_> {
183+ fn drop(&mut self) {
188184 dev_dbg!(self.pdev, "Remove Rust PCI driver sample.\n");
189185 }
190186 }
samples/rust/rust_driver_platform.rs+5−4
@@ -101,13 +101,14 @@ kernel::acpi_device_table!( · 100 unmodified lines
101101
102102 impl platform::Driver for SampleDriver {
103103 type IdInfo = Info;
104+ type Data<'bound> = Self;
104105 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
105106 const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
106107
107− fn probe(
108− pdev: &platform::Device<Core>,
109− info: Option<&Self::IdInfo>,
110− ) -> impl PinInit<Self, Error> {
108+ fn probe<'bound>(
109+ pdev: &'bound platform::Device<Core<'_>>,
110+ info: Option<&'bound Self::IdInfo>,
111+ ) -> impl PinInit<Self, Error> + 'bound {
111112 let dev = pdev.as_ref();
112113
113114 dev_dbg!(dev, "Probe Rust Platform driver sample.\n");
samples/rust/rust_driver_usb.rs+8−7
@@ -26,21 +26,22 @@ kernel::usb_device_table!( · 25 unmodified lines
2626
2727 impl usb::Driver for SampleDriver {
2828 type IdInfo = ();
29+ type Data<'bound> = Self;
2930 const ID_TABLE: usb::IdTable<Self::IdInfo> = &USB_TABLE;
3031
31− fn probe(
32− intf: &usb::Interface<Core>,
32+ fn probe<'bound>(
33+ intf: &'bound usb::Interface<Core<'_>>,
3334 _id: &usb::DeviceId,
34− _info: &Self::IdInfo,
35− ) -> impl PinInit<Self, Error> {
36− let dev: &device::Device<Core> = intf.as_ref();
35+ _info: &'bound Self::IdInfo,
36+ ) -> impl PinInit<Self, Error> + 'bound {
37+ let dev: &device::Device<Core<'_>> = intf.as_ref();
3738 dev_info!(dev, "Rust USB driver sample probed\n");
3839
3940 Ok(Self { _intf: intf.into() })
4041 }
4142
42− fn disconnect(intf: &usb::Interface<Core>, _data: Pin<&Self>) {
43− let dev: &device::Device<Core> = intf.as_ref();
43+ fn disconnect<'bound>(intf: &'bound usb::Interface<Core<'_>>, _data: Pin<&Self>) {
44+ let dev: &device::Device<Core<'_>> = intf.as_ref();
4445 dev_info!(dev, "Rust USB driver sample disconnected\n");
4546 }
4647 }
samples/rust/rust_i2c_client.rs+9−5
@@ -106,13 +106,14 @@ const BOARD_INFO: i2c::I2cBoardInfo = · 105 unmodified lines
106106
107107 impl platform::Driver for SampleDriver {
108108 type IdInfo = ();
109+ type Data<'bound> = Self;
109110 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
110111 const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
111112
112− fn probe(
113− pdev: &platform::Device<device::Core>,
114− _info: Option<&Self::IdInfo>,
115− ) -> impl PinInit<Self, Error> {
113+ fn probe<'bound>(
114+ pdev: &'bound platform::Device<device::Core<'_>>,
115+ _info: Option<&'bound Self::IdInfo>,
116+ ) -> impl PinInit<Self, Error> + 'bound {
116117 dev_info!(
117118 pdev.as_ref(),
118119 "Probe Rust I2C Client registration sample.\n"
@@ -129,7 +130,10 @@ impl platform::Driver for SampleDriver { · 10 unmodified lines
129130 })
130131 }
131132
132− fn unbind(pdev: &platform::Device<device::Core>, _this: Pin<&Self>) {
133+ fn unbind<'bound>(
134+ pdev: &'bound platform::Device<device::Core<'_>>,
135+ _this: Pin<&Self::Data<'bound>>,
136+ ) {
133137 dev_info!(
134138 pdev.as_ref(),
135139 "Unbind Rust I2C Client registration sample.\n"
samples/rust/rust_soc.rs+5−4
@@ -37,13 +37,14 @@ kernel::acpi_device_table!( · 36 unmodified lines
3737
3838 impl platform::Driver for SampleSocDriver {
3939 type IdInfo = ();
40+ type Data<'bound> = Self;
4041 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
4142 const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
4243
43− fn probe(
44− pdev: &platform::Device<Core>,
45− _info: Option<&Self::IdInfo>,
46− ) -> impl PinInit<Self, Error> {
44+ fn probe<'bound>(
45+ pdev: &'bound platform::Device<Core<'_>>,
46+ _info: Option<&'bound Self::IdInfo>,
47+ ) -> impl PinInit<Self, Error> + 'bound {
4748 dev_dbg!(pdev, "Probe Rust SoC driver sample.\n");
4849
4950 let pdev = pdev.into();