ACPI: scan: A few ad-hoc cleanups
Subsystems (2)
cover ACPI: scan: A few ad-hoc cleanups
While looking for something else in the scan.c I noticed the room of improvement. Hence this series. Also bus.c patches, which some how were related to my research, but I think are independent from the scan.c improvements.
Andy Shevchenko (7):
ACPI: bus: Make container_of() no-op where it makes sense
ACPI: bus: Don't use "proxy" headers
ACPI: scan: Replace infinite for-loop with finite while-loop
ACPI: scan: Use list_first_entry_or_null() in acpi_device_hid()
ACPI: scan: Move misleading comment to acpi_dma_configure_id()
ACPI: scan: Use standard error checking pattern
ACPI: scan: Introduce typedef:s for struct acpi_hotplug_context
members
drivers/acpi/dock.c | 48 +++++++++++++++-------------------------- drivers/acpi/scan.c | 42 +++++++++++++++++------------------- include/acpi/acpi_bus.h | 28 +++++++++++++++--------- 3 files changed, 55 insertions(+), 63 deletions(-)
Signature · 1 line
2.43.0.rc1.1.gbec44491f096
PATCH 1/7 ACPI: bus: Make container_of() no-op where it makes sense
Move list head node to be the first member in a few data structures in order to make container_of() no-op at compile time. On x86_64 with a custom (default + a few dozens of drivers enabled) configuration:
add/remove: 0/0 grow/shrink: 5/12 up/down: 21/-124 (-103)
...
Total: Before=39924675, After=39924572, chg -0.00%
--- include/acpi/acpi_bus.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
include/acpi/acpi_bus.h +5 -5
@@ -124,8 +124,8 @@ static inline struct acpi_hotplug_profile *to_acpi_hotplug_profile( }struct acpi_scan_handler { -const struct acpi_device_id *ids;struct list_head list_node; +const struct acpi_device_id *ids;bool (*match)(const char *idstr,const struct acpi_device_id **matchid);int (*attach)(struct acpi_device *dev,const struct acpi_device_id *id);void (*detach)(struct acpi_device *dev);
@@ -269,6 +269,7 @@ struct acpi_device_power_flags { };struct acpi_device_power_state { +struct list_head resources;/* Power resources referenced */ struct {u8 valid:1 ;u8 explicit_set:1 ;/* _PSx present? */
@@ -276,7 +277,6 @@ struct acpi_device_power_state { } flags;int power;/* % Power (compared to D0) */ int latency;/* Dx->D0 time (microseconds) */ -struct list_head resources;/* Power resources referenced */ };struct acpi_device_power {
@@ -342,16 +342,16 @@ struct acpi_device_wakeup { };struct acpi_device_physical_node { -unsigned int node_id;struct list_head node; +unsigned int node_id;struct device *dev;bool put_online:1 ; };struct acpi_device_properties { +struct list_head list;const guid_t *guid;union acpi_object *properties; -struct list_head list;void **bufs; };
@@ -488,12 +488,12 @@ struct acpi_device {/* Non-device subnode */ struct acpi_data_node { +struct list_head sibling;const char *name;acpi_handle handle;struct fwnode_handle fwnode;struct fwnode_handle *parent;struct acpi_device_data data; -struct list_head sibling;struct kobject kobj;struct completion kobj_done; };
Signature · 1 line
2.43.0.rc1.1.gbec44491f096
PATCH 2/7 ACPI: bus: Don't use "proxy" headers
In reply to Andy ShevchenkoUpdate header inclusions to follow IWYU (Include What You Use) principle.
--- include/acpi/acpi_bus.h | 5 +++++ 1 file changed, 5 insertions(+)
include/acpi/acpi_bus.h +5 -0
@@ -9,8 +9,13 @@ #ifndef __ACPI_BUS_H__ #define __ACPI_BUS_H__ +#include <linux/completion.h> +#include <linux/container_of.h> #include <linux/device.h> +#include <linux/kobject.h> +#include <linux/mutex.h> #include <linux/property.h> +#include <linux/types.h> struct acpi_handle_list { u32 count;
Signature · 1 line
2.43.0.rc1.1.gbec44491f096
PATCH 3/7 ACPI: scan: Replace infinite for-loop with finite while-loop
In reply to Andy ShevchenkoThe infinite loops is harder to understand (as one has to go over the body in order to find main exit conditional) and it's more verbose than usual approach with a while-loop.
Note, we may not use list_for_each_entry_safe() as there is locking involved and the saved pointer may become invalid behind our back.
--- drivers/acpi/scan.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/acpi/scan.c +6 -8
@@ -530,15 +530,10 @@ static DEFINE_MUTEX(acpi_device_del_lock);static void acpi_device_del_work_fn (struct work_struct *work_not_used) { -for (;;) { -struct acpi_device *adev; +struct acpi_device *adev; -mutex_lock (&acpi_device_del_lock); - -if (list_empty (&acpi_device_del_list)) { -mutex_unlock (&acpi_device_del_lock); -break ; - } +mutex_lock (&acpi_device_del_lock); +while (!list_empty (&acpi_device_del_list)) { adev =list_first_entry (&acpi_device_del_list, struct acpi_device, del_list);list_del (&adev->del_list);
@@ -555,7 +550,10 @@ static void acpi_device_del_work_fn(struct work_struct *work_not_used)*/ acpi_power_transition (adev,ACPI_STATE_D3_COLD );acpi_dev_put (adev); + +mutex_lock (&acpi_device_del_lock); } +mutex_unlock (&acpi_device_del_lock); }/**
Signature · 1 line
2.43.0.rc1.1.gbec44491f096
Rafael J. Wysocki
maintainerRe: [PATCH v1 3/7] ACPI: scan: Replace infinite for-loop with finite while-loop
Quoted 38 lines · Andy Shevchenko
The infinite loops is harder to understand (as one has to go over the body in order to find main exit conditional) and it's more verbose than usual approach with a while-loop.
Note, we may not use
list_for_each_entry_safe()as there is locking involved and the saved pointer may become invalid behind our back.Signed-off-by Andy Shevchenko <[email protected]>--- drivers/acpi/scan.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)drivers/acpi/scan.c +3
-8@@ -530,15 +530,10 @@ static DEFINE_MUTEX(acpi_device_del_lock);static void acpi_device_del_work_fn (struct work_struct *work_not_used) { -for (;;) { -struct acpi_device *adev; +struct acpi_device *adev; -mutex_lock (&acpi_device_del_lock); - -if (list_empty (&acpi_device_del_list)) { -mutex_unlock (&acpi_device_del_lock); -break ; - } +mutex_lock (&acpi_device_del_lock); +while (!list_empty (&acpi_device_del_list)) { adev =list_first_entry (&acpi_device_del_list, struct acpi_device, del_list);list_del (&adev->del_list);@@ -555,7 +550,10 @@ static void acpi_device_del_work_fn(struct work_struct *work_not_used)*/
drivers/acpi/scan.c static void acpi_device_del_work_fn(struct work_struct *work_not_used)
Quoted 10 lines
drivers/acpi/scan.c +3
-1acpi_power_transition (adev,ACPI_STATE_D3_COLD );acpi_dev_put (adev); + +mutex_lock (&acpi_device_del_lock); } +mutex_unlock (&acpi_device_del_lock); }/** --
I don't quite agree with this one, sorry.
The rest of the series has been applied as 6.10 material.
Thanks!
Re: [PATCH v1 3/7] ACPI: scan: Replace infinite for-loop with finite while-loop
Quoted 2 lines · Rafael J. Wysocki
On Mon, Mar 25, 2024 at 1:34 PM Andy Shevchenko
<[email protected]> wrote:
...
Quoted 1 line
I don't quite agree with this one, sorry.
No problem.
Quoted 1 line
The rest of the series has been applied as 6.10 material.
Thank you!
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH 4/7 ACPI: scan: Use list_first_entry_or_null() in acpi_device_hid()
In reply to Andy ShevchenkoTo replace list_empty() + list_first_entry() pair to simplify code.
--- drivers/acpi/scan.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/acpi/scan.c +2 -2
@@ -1296,10 +1296,10 @@ const char *acpi_device_hid(struct acpi_device *device) {struct acpi_hardware_id *hid; -if (list_empty (&device->pnp.ids)) + hid = list_first_entry_or_null(&device->pnp.ids, struct acpi_hardware_id, list); +if (!hid)return dummy_hid; - hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);return hid->id; }EXPORT_SYMBOL (acpi_device_hid);
Signature · 1 line
2.43.0.rc1.1.gbec44491f096
PATCH 5/7 ACPI: scan: Move misleading comment to acpi_dma_configure_id()
In reply to Andy ShevchenkoThe acpi_iommu_configure_id() implementation has a misleading comment since after it the flow does something different to what it states. Move the commit to the caller and with that unshadow the error code inside acpi_iommu_configure_id().
--- drivers/acpi/scan.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/acpi/scan.c +5 -5
@@ -1623,12 +1623,11 @@ static int acpi_iommu_configure_id(struct device *dev, const u32 *id_in)if (!err && dev->bus) err =iommu_probe_device (dev); -/* Ignore all other errors apart from EPROBE_DEFER */ -if (err == -EPROBE_DEFER ) { +if (err == -EPROBE_DEFER )return err; - }else if (err) { +if (err) {dev_dbg (dev,"Adding to IOMMU failed: %d\n" , err); -return -ENODEV ; +return err; }if (!acpi_iommu_fwspec_ops (dev))return -ENODEV ;
@@ -1669,13 +1668,14 @@ int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,acpi_arch_dma_setup (dev); +/* Ignore all other errors apart from EPROBE_DEFER */ ret =acpi_iommu_configure_id (dev, input_id);if (ret == -EPROBE_DEFER )return -EPROBE_DEFER ;/* * Historically this routine doesn't fail driver probing due to errors -* in acpi_iommu_configure_id() +* in acpi_iommu_configure_id(). */ arch_setup_dma_ops (dev,0 ,U64_MAX , attr ==DEV_DMA_COHERENT );
Signature · 1 line
2.43.0.rc1.1.gbec44491f096
PATCH 6/7 ACPI: scan: Use standard error checking pattern
In reply to Andy ShevchenkoCheck for an error and return it as it's the usual way to handle this.
--- drivers/acpi/scan.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/acpi/scan.c +5 -4
@@ -1579,12 +1579,13 @@ int acpi_iommu_fwspec_init(struct device *dev, u32 id,struct fwnode_handle *fwnode, const struct iommu_ops *ops) { - int ret = iommu_fwspec_init(dev, fwnode, ops); + int ret; -if (!ret) - ret =iommu_fwspec_add_ids (dev, &id,1 ); + ret =iommu_fwspec_init (dev, fwnode, ops); +if (ret) +return ret; -return ret; +return iommu_fwspec_add_ids (dev, &id,1 ); }static inline const struct iommu_ops *acpi_iommu_fwspec_ops (struct device *dev)
Signature · 1 line
2.43.0.rc1.1.gbec44491f096
PATCH 7/7 ACPI: scan: Introduce typedef:s for struct acpi_hotplug_context members
In reply to Andy ShevchenkoFollow the struct acpi_device_ops approach and introduce typedef:s for the members. It makes code less verbose and more particular on what parameters we take or types we use.
--- drivers/acpi/dock.c | 48 +++++++++++++++-------------------------- drivers/acpi/scan.c | 5 ++--- include/acpi/acpi_bus.h | 13 ++++++----- 3 files changed, 27 insertions(+), 39 deletions(-)
drivers/acpi/dock.c +17 -31
@@ -88,43 +88,29 @@ static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event, enum dock_callback_type cb_type) {struct acpi_device *adev = dd->adev; +acpi_hp_fixup fixup =NULL ; +acpi_hp_uevent uevent =NULL ; +acpi_hp_notify notify =NULL ;acpi_lock_hp_context (); -if (!adev->hp) - goto out; - -if (cb_type ==DOCK_CALL_FIXUP ) { -void (*fixup)(struct acpi_device *); - - fixup = adev->hp->fixup; -if (fixup) { -acpi_unlock_hp_context (); -fixup (adev); -return ; - } - }else if (cb_type ==DOCK_CALL_UEVENT ) { -void (*uevent)(struct acpi_device *,u32 ); - - uevent = adev->hp->uevent; -if (uevent) { -acpi_unlock_hp_context (); -uevent (adev, event); -return ; - } - }else { -int (*notify)(struct acpi_device *,u32 ); - - notify = adev->hp->notify; -if (notify) { -acpi_unlock_hp_context (); -notify (adev, event); -return ; - } +if (adev->hp) { +if (cb_type ==DOCK_CALL_FIXUP ) + fixup = adev->hp->fixup; +else if (cb_type ==DOCK_CALL_UEVENT ) + uevent = adev->hp->uevent; +else + notify = adev->hp->notify; } - out:acpi_unlock_hp_context (); + +if (fixup) +fixup (adev); +else if (uevent) +uevent (adev, event); +else if (notify) +notify (adev, event); }static struct dock_station *find_dock_station (acpi_handle handle)
drivers/acpi/scan.c +2 -3
@@ -73,8 +73,7 @@ void acpi_unlock_hp_context(void)void acpi_initialize_hp_context (struct acpi_device *adev,struct acpi_hotplug_context *hp, -int (*notify)(struct acpi_device *,u32 ), -void (*uevent)(struct acpi_device *,u32 )) +acpi_hp_notify notify,acpi_hp_uevent uevent) {acpi_lock_hp_context (); hp->notify = notify;
@@ -428,7 +427,7 @@ void acpi_device_hotplug(struct acpi_device *adev, u32 src) }else if (adev->flags.hotplug_notify) { error =acpi_generic_hotplug_event (adev, src); }else { -int (*notify)(struct acpi_device *,u32 ); +acpi_hp_notify notify;acpi_lock_hp_context (); notify = adev->hp ? adev->hp->notify :NULL ;
include/acpi/acpi_bus.h +8 -5
@@ -144,11 +144,15 @@ struct acpi_scan_handler {* -------------------- */ +typedef int (*acpi_hp_notify ) (struct acpi_device *,u32 ); +typedef void (*acpi_hp_uevent ) (struct acpi_device *,u32 ); +typedef void (*acpi_hp_fixup ) (struct acpi_device *); +struct acpi_hotplug_context {struct acpi_device *self; -int (*notify)(struct acpi_device *,u32 ); -void (*uevent)(struct acpi_device *,u32 ); -void (*fixup)(struct acpi_device *); +acpi_hp_notify notify; +acpi_hp_uevent uevent; +acpi_hp_fixup fixup; };/*
@@ -583,8 +587,7 @@ static inline void acpi_set_hp_context(struct acpi_device *adev,void acpi_initialize_hp_context (struct acpi_device *adev,struct acpi_hotplug_context *hp, -int (*notify)(struct acpi_device *,u32 ), -void (*uevent)(struct acpi_device *,u32 )); +acpi_hp_notify notify,acpi_hp_uevent uevent);/* acpi_device.dev.bus == &acpi_bus_type */ extern const struct bus_type acpi_bus_type;
Signature · 1 line
2.43.0.rc1.1.gbec44491f096
Quoted 4 lines · Andy Shevchenko
While looking for something else in the
scan.cI noticed the room of improvement. Hence this series. Alsobus.cpatches, which some how were related to my research, but I think are independent from thescan.cimprovements.
Changes looks good to me.
Quoted 16 lines
Andy Shevchenko (7):
ACPI: bus: Makecontainer_of()no-op where it makes sense
ACPI: bus: Don't use "proxy" headers
ACPI: scan: Replace infinite for-loop with finite while-loop
ACPI: scan: Uselist_first_entry_or_null()inacpi_device_hid()
ACPI: scan: Move misleading comment toacpi_dma_configure_id()
ACPI: scan: Use standard error checking pattern
ACPI: scan: Introduce typedef:s for structacpi_hotplug_context
membersdrivers/acpi/dock.c | 48 +++++++++++++++-------------------------- drivers/acpi/scan.c | 42 +++++++++++++++++------------------- include/acpi/acpi_bus.h | 28 +++++++++++++++--------- 3 files changed, 55 insertions(+), 63 deletions(-)
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer