ACPI: driver: Use devres-based resource management
Subsystems (4)
Rafael J. Wysocki
maintainercover ACPI: driver: Use devres-based resource management
Hi All,
This series adds devm_acpi_install_notify_handler() for installing an ACPI notify handler along with a devm action to remove it automatically on driver removal and switches over a few core ACPI drivers using acpi_dev_install_notify_handler() superseded by the above to using devres-based resource management.
This allows some some hard to maintain and error prone driver code to be shaved off.
Thanks!
Rafael J. Wysocki
maintainerPATCH 1/17 ACPI: bus: Introduce devm_acpi_install_notify_handler()
From: "Rafael J. Wysocki" <[email protected]>
Introduce devm_acpi_install_notify_handler() for installing an ACPI notify handler managed by devres that will be removed automatically on driver detach.
It installs the notify handler on the device object in the ACPI namespace that corresponds to the owner device's ACPI companion, if present (an error is returned if the owner device doesn't have an ACPI companion).
Currently, there is no way to manually remove the notify handler installed by it because none of its users brought on subsequently will need to do that.
--- drivers/acpi/bus.c | 67 +++++++++++++++++++++++++++++++++++++++++ include/acpi/acpi_bus.h | 2 ++ 2 files changed, 69 insertions(+)
drivers/acpi/bus.c +67 -0
@@ -679,6 +679,73 @@ void acpi_dev_remove_notify_handler(struct acpi_device *adev, }EXPORT_SYMBOL_GPL (acpi_dev_remove_notify_handler); +struct acpi_notify_handler_devres { +acpi_notify_handler handler; +u32 handler_type; +}; + +static void devm_acpi_notify_handler_release (struct device *dev,void *res) +{ +struct acpi_notify_handler_devres *dr = res; + +acpi_dev_remove_notify_handler (ACPI_COMPANION (dev), dr->handler_type, + dr->handler); +} + +/** +* devm_acpi_install_notify_handler - Install an ACPI notify handler for a +* managed device +* @dev: Device to install a notify handler for +* @handler_type: Type of the notify handler +* @handler: Handler function to install +* @context: Data passed back to the handler function +* +* This function performs the same function as acpi_dev_install_notify_handler() +* called for the ACPI companion of @dev with the same @handler_type, @handler, +* and @context arguments, but the ACPI notify handler installed by it will be +* automatically removed on driver detach. +* +* Callers should ensure that all resources used by @handler have been allocated +* prior to invoking this function, in which case those resources should be +* devres-managed so that they won't be released before the notify handler +* removal. Otherwise, special synchronization between @handler and the +* management of those resources is required. +* +* When the request fails, an error message is printed with contextual +* information (device name, handler function and error code). Don't add extra +* error messages at the call sites. +* +* Return: 0 on success or a negative error number. +*/ +int devm_acpi_install_notify_handler (struct device *dev,u32 handler_type, +acpi_notify_handler handler,void *context) +{ +struct acpi_notify_handler_devres *dr; +struct acpi_device *adev; +int ret; + + adev =ACPI_COMPANION (dev); +if (!adev) +return dev_err_probe (dev, -ENODEV ,"No ACPI companion in %s()\n" , __func__); + + dr =devres_alloc (devm_acpi_notify_handler_release,sizeof (*dr),GFP_KERNEL ); +if (!dr) +return -ENOMEM ; + + ret =acpi_dev_install_notify_handler (adev, handler_type, handler, context); +if (ret) { +devres_free (dr); +return dev_err_probe (dev, ret,"Failed to install an ACPI notify handler\n" ); + } + + dr->handler = handler; + dr->handler_type = handler_type; +devres_add (dev, dr); + +return 0 ; +} +EXPORT_SYMBOL_GPL (devm_acpi_install_notify_handler); +/* Handle events targeting \_SB device (at present only graceful shutdown) */ #define ACPI_SB_NOTIFY_SHUTDOWN_REQUEST 0x81
include/acpi/acpi_bus.h +2 -0
@@ -629,6 +629,8 @@ int acpi_dev_install_notify_handler(struct acpi_device *adev,void acpi_dev_remove_notify_handler (struct acpi_device *adev,u32 handler_type,acpi_notify_handler handler); +int devm_acpi_install_notify_handler (struct device *dev,u32 handler_type, +acpi_notify_handler handler,void *context);extern int acpi_notifier_call_chain (const char *device_class,const char *bus_id,u32 type,u32 data);extern int register_acpi_notifier (struct notifier_block *);
Signature · 1 line
2.51.0
Re: [PATCH v1 01/17] ACPI: bus: Introduce devm_acpi_install_notify_handler()
Quoted 12 lines · Rafael J. Wysocki
Introduce
devm_acpi_install_notify_handler()for installing an ACPI notify handler managed by devres that will be removed automatically on driver detach.It installs the notify handler on the device object in the ACPI namespace that corresponds to the owner device's ACPI companion, if present (an error is returned if the owner device doesn't have an ACPI companion).
Currently, there is no way to manually remove the notify handler installed by it because none of its users brought on subsequently will need to do that.
...
drivers/acpi/bus.c void acpi_dev_remove_notify_handler(struct acpi_device *adev, · line 689
Quoted 3 lines
drivers/acpi/bus.c +3
-0+static void devm_acpi_notify_handler_release (struct device *dev,void *res) +{ +struct acpi_notify_handler_devres *dr = res;
'dr' is usually associated with internal devres structures and might be misleading in here, I would rename to something like handler_devres.
drivers/acpi/bus.c void acpi_dev_remove_notify_handler(struct acpi_device *adev, · line 691
Quoted 1 line
drivers/acpi/bus.c +1
-0+ acpi_dev_remove_notify_handler(ACPI_COMPANION(dev), dr->handler_type,
acpi_dev might be also part of the same data structure, so you won't need to take dev again and derive adev from it.
Quoted 2 lines
(quoted diff) +2
-0+ dr->handler); +}
...
Quoted 3 lines
+/**
+ *devm_acpi_install_notify_handler- Install an ACPI notify handler for a
+ * managed device
There is a stray space just after asterisk.
Quoted 10 lines
+ * @dev: Device to install a notify handler for
+ * @handler_type: Type of the notify handler
+ * @handler: Handler function to install
+ * @context: Data passed back to the handler function
+ *
+ * This function performs the same function asacpi_dev_install_notify_handler()
+ * called for the ACPI companion of @dev with the same @handler_type, @handler,
+ * and @context arguments, but the ACPI notify handler installed by it will be
+ * automatically removed on driver detach.
+ *
Quoted 8 lines
+ * Callers should ensure that all resources used by @handler have been allocated
+ * prior to invoking this function, in which case those resources should be
+ * devres-managed so that they won't be released before the notify handler
+ * removal. Otherwise, special synchronization between @handler and the
+ * management of those resources is required.
+ *
+ * When the request fails, an error message is printed with contextual
+ * information (device name, handler function and error code). Don't add extra
This "handler function" points to __func__? If so, it seems misleading.
Quoted 4 lines
+ * error messages at the call sites.
+ *
+ * Return: 0 on success or a negative error number.
+ */
drivers/acpi/bus.c void acpi_dev_remove_notify_handler(struct acpi_device *adev, · line 729
Quoted 10 lines
drivers/acpi/bus.c +10
-0+int devm_acpi_install_notify_handler (struct device *dev,u32 handler_type, +acpi_notify_handler handler,void *context) +{ +struct acpi_notify_handler_devres *dr; +struct acpi_device *adev; +int ret; + + adev =ACPI_COMPANION (dev); +if (!adev) +return dev_err_probe (dev, -ENODEV ,"No ACPI companion in %s()\n" , __func__);
Not sure how __func__ may help here. We will have a device instance to be printed. It's obvious then how to find the culprit call.
Quoted 16 lines
drivers/acpi/bus.c +15
-0+ dr =devres_alloc (devm_acpi_notify_handler_release,sizeof (*dr),GFP_KERNEL ); +if (!dr) +return -ENOMEM ; + + ret =acpi_dev_install_notify_handler (adev, handler_type, handler, context); +if (ret) { +devres_free (dr); +return dev_err_probe (dev, ret,"Failed to install an ACPI notify handler\n" ); + } + + dr->handler = handler; + dr->handler_type = handler_type; +devres_add (dev, dr); +return 0 ; +}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Rafael J. Wysocki
maintainerRe: [PATCH v1 01/17] ACPI: bus: Introduce devm_acpi_install_notify_handler()
Quoted 16 lines · Andy Shevchenko
Quoted 4 lines · Rafael J. Wysocki
Introduce
devm_acpi_install_notify_handler()for installing an ACPI notify handler managed by devres that will be removed automatically on driver detach.Quoted 8 lines
It installs the notify handler on the device object in the ACPI namespace that corresponds to the owner device's ACPI companion, if present (an error is returned if the owner device doesn't have an ACPI companion).
Currently, there is no way to manually remove the notify handler installed by it because none of its users brought on subsequently will need to do that.
Quoted 8 lines
...
drivers/acpi/bus.c void acpi_dev_remove_notify_handler(struct acpi_device *adev, · line 689
Quoted 3 lines
drivers/acpi/bus.c +3
-0+static void devm_acpi_notify_handler_release (struct device *dev,void *res) +{ +struct acpi_notify_handler_devres *dr = res;'dr' is usually associated with internal devres structures and might be misleading in here, I would rename to something like
handler_devres.
Well, whatever.
Quoted 4 lines
Quoted 1 line
+ acpi_dev_remove_notify_handler(ACPI_COMPANION(dev),
dr->handler_type,
acpi_devmight be also part of the same data structure, so you won't need to take dev again and derive adev from it.
I'm not sure what you mean.
Put acpi_dev into struct acpi_notify_handler_devres? That can be done in a follow-up patch.
Quoted 10 lines
Quoted 2 lines
(quoted diff) +2
-0+ dr->handler); +}...
Quoted 3 lines
+/**
+ *devm_acpi_install_notify_handler- Install an ACPI notify handler for a
+ * managed deviceThere is a stray space just after asterisk.
Which asterisk?
Quoted 19 lines
Quoted 10 lines
+ * @dev: Device to install a notify handler for
+ * @handler_type: Type of the notify handler
+ * @handler: Handler function to install
+ * @context: Data passed back to the handler function
+ *
+ * This function performs the same function asacpi_dev_install_notify_handler()
+ * called for the ACPI companion of @dev with the same @handler_type, @handler,
+ * and @context arguments, but the ACPI notify handler installed by it will be
+ * automatically removed on driver detach.
+ *Quoted 8 lines
+ * Callers should ensure that all resources used by @handler have been allocated
+ * prior to invoking this function, in which case those resources should be
+ * devres-managed so that they won't be released before the notify handler
+ * removal. Otherwise, special synchronization between @handler and the
+ * management of those resources is required.
+ *
+ * When the request fails, an error message is printed with contextual
+ * information (device name, handler function and error code). Don't add extra
Quoted 1 line
This "handler function" points to
__func__? If so, it seems misleading.
Yes, this sentence should just be "When the request fails, an error message is printed" without the "contextual information" part.
Quoted 9 lines
Quoted 9 lines
+ * error messages at the call sites.
+ *
+ * Return: 0 on success or a negative error number.
+ */drivers/acpi/bus.c +5
-0+int devm_acpi_install_notify_handler (struct device *dev,u32 handler_type, +acpi_notify_handler handler,void *context) +{ +struct acpi_notify_handler_devres *dr; +struct acpi_device *adev;
Quoted 8 lines
drivers/acpi/bus.c void acpi_dev_remove_notify_handler(struct acpi_device *adev, · line 729
Quoted 5 lines
drivers/acpi/bus.c +5
-0+int ret; + + adev =ACPI_COMPANION (dev); +if (!adev) +return dev_err_probe (dev, -ENODEV ,"No ACPI companion in %s()\n" , __func__);Not sure how
__func__may help here. We will have a device instance to be printed. It's obvious then how to find the culprit call.
But it doesn't hurt either, does it?
Quoted 14 lines
Quoted 13 lines
drivers/acpi/bus.c +13
-0+ dr =devres_alloc (devm_acpi_notify_handler_release,sizeof (*dr),GFP_KERNEL ); +if (!dr) +return -ENOMEM ; + + ret =acpi_dev_install_notify_handler (adev, handler_type, handler, context); +if (ret) { +devres_free (dr); +return dev_err_probe (dev, ret,"Failed to install an ACPI notify handler\n" ); + } + + dr->handler = handler; + dr->handler_type = handler_type; +devres_add (dev, dr);
Quoted 4 lines
drivers/acpi/bus.c void acpi_dev_remove_notify_handler(struct acpi_device *adev, · line 746
Quoted 2 lines
drivers/acpi/bus.c +2
-0+return 0 ; +}--
So thanks for the review, but I don't think I want to send a v2 at this point.
I'd rather send a follow-up patch to clean up these things.
Re: [PATCH v1 01/17] ACPI: bus: Introduce devm_acpi_install_notify_handler()
Quoted 3 lines · Rafael J. Wysocki
Quoted 1 line · Andy Shevchenko
On Thu, May 21, 2026 at 03:59:50PM +0200, Rafael J. Wysocki wrote:
...
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
+ acpi_dev_remove_notify_handler(ACPI_COMPANION(dev),
dr->handler_type,
acpi_devmight be also part of the same data structure, so you won't need to take dev again and derive adev from it.I'm not sure what you mean.
Put
acpi_devinto structacpi_notify_handler_devres?
Yes.
Quoted 1 line
Quoted 1 line
Quoted 1 line
+
dr->handler);
...
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
+ *
devm_acpi_install_notify_handler- Install an ACPI notify handler for a+ * managed device
There is a stray space just after asterisk.
Which asterisk?
The line above has "<space>*<space>(sic!)<tab><tab> ... managed device". The <space> after the asterisk is a stray one.
...
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
+ return
dev_err_probe(dev, -ENODEV, "No ACPI companion in %s()\n", __func__);Not sure how
__func__may help here. We will have a device instance to be printed. It's obvious then how to find the culprit call.But it doesn't hurt either, does it?
From my p.o.v. it's just extra information that's not needed. But I'm not going fight to death against it.
...
Quoted 3 lines
So thanks for the review, but I don't think I want to send a v2 at this point.
I'd rather send a follow-up patch to clean up these things.
Okay!
Signature · 2 lines
With Best Regards, Andy Shevchenko
Rafael J. Wysocki
maintainerPATCH 2/17 ACPI: NFIT: core: Use devm_acpi_install_notify_handler()
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
Now that devm_acpi_install_notify_handler() is available, use it in acpi_nfit_probe() instead of a custom devm action removing an ACPI notify handler installed via acpi_dev_install_notify_handler().
Also drop the explicit ACPI_COMPANION() check against NULL that is not necessary any more becuase devm_acpi_install_notify_handler() carries out an equivalent check internally and use ACPI_HANDLE() to retrieve the platform device's ACPI handle.
No intentional functional impact.
--- drivers/acpi/nfit/core.c | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-)
drivers/acpi/nfit/core.c +3 -21
@@ -3298,14 +3298,6 @@ static void acpi_nfit_notify(acpi_handle handle, u32 event, void *data)device_unlock (dev); } -static void acpi_nfit_remove_notify_handler (void *data) -{ -struct acpi_device *adev = data; - -acpi_dev_remove_notify_handler (adev,ACPI_DEVICE_NOTIFY , - acpi_nfit_notify); -} -void acpi_nfit_shutdown (void *data) {struct acpi_nfit_desc *acpi_desc = data;
@@ -3342,22 +3334,12 @@ static int acpi_nfit_probe(struct platform_device *pdev)struct acpi_nfit_desc *acpi_desc;struct device *dev = &pdev->dev;struct acpi_table_header *tbl; -struct acpi_device *adev;acpi_status status =AE_OK ;acpi_size sz;int rc =0 ; - adev =ACPI_COMPANION (&pdev->dev); -if (!adev) -return -ENODEV ; - - rc =acpi_dev_install_notify_handler (adev,ACPI_DEVICE_NOTIFY , - acpi_nfit_notify, dev); -if (rc) -return rc; - - rc =devm_add_action_or_reset (dev, acpi_nfit_remove_notify_handler, - adev); + rc =devm_acpi_install_notify_handler (dev,ACPI_DEVICE_NOTIFY , + acpi_nfit_notify, dev);if (rc)return rc;
@@ -3388,7 +3370,7 @@ static int acpi_nfit_probe(struct platform_device *pdev) acpi_desc->acpi_header = *tbl;/* Evaluate _FIT and override with that if present */ - status =acpi_evaluate_object (adev->handle,"_FIT" ,NULL , &buf); + status =acpi_evaluate_object (ACPI_HANDLE (dev),"_FIT" ,NULL , &buf);if (ACPI_SUCCESS (status) && buf.length >0 ) {union acpi_object *obj = buf.pointer;
Signature · 1 line
2.51.0
Rafael J. Wysocki
maintainerRe: [PATCH v1 02/17] ACPI: NFIT: core: Use devm_acpi_install_notify_handler()
Quoted 64 lines · Rafael J. Wysocki
From: "Rafael J. Wysocki" <[email protected]>
Now that
devm_acpi_install_notify_handler()is available, use it inacpi_nfit_probe()instead of a custom devm action removing an ACPI notify handler installed viaacpi_dev_install_notify_handler().Also drop the explicit
ACPI_COMPANION()check against NULL that is not necessary any more becuasedevm_acpi_install_notify_handler()carries out an equivalent check internally and useACPI_HANDLE()to retrieve the platform device's ACPI handle.No intentional functional impact.
Signed-off-by Rafael J. Wysocki <[email protected]>--- drivers/acpi/nfit/core.c | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-)drivers/acpi/nfit/core.c +2
-20@@ -3298,14 +3298,6 @@ static void acpi_nfit_notify(acpi_handle handle, u32 event, void *data)device_unlock (dev); } -static void acpi_nfit_remove_notify_handler (void *data) -{ -struct acpi_device *adev = data; - -acpi_dev_remove_notify_handler (adev,ACPI_DEVICE_NOTIFY , - acpi_nfit_notify); -} -void acpi_nfit_shutdown (void *data) {struct acpi_nfit_desc *acpi_desc = data;@@ -3342,22 +3334,12 @@ static int acpi_nfit_probe(struct platform_device *pdev)struct acpi_nfit_desc *acpi_desc;struct device *dev = &pdev->dev;struct acpi_table_header *tbl; -struct acpi_device *adev;acpi_status status =AE_OK ;acpi_size sz;int rc =0 ; - adev =ACPI_COMPANION (&pdev->dev); -if (!adev) -return -ENODEV ; - - rc =acpi_dev_install_notify_handler (adev,ACPI_DEVICE_NOTIFY , - acpi_nfit_notify, dev); -if (rc) -return rc; - - rc =devm_add_action_or_reset (dev, acpi_nfit_remove_notify_handler, - adev); + rc =devm_acpi_install_notify_handler (dev,ACPI_DEVICE_NOTIFY , + acpi_nfit_notify, dev);if (rc)return rc;
drivers/acpi/nfit/core.c static int acpi_nfit_probe(struct platform_device *pdev)
Quoted 9 lines
drivers/acpi/nfit/core.c +1
-2@@ -3388,7 +3370,7 @@ static int acpi_nfit_probe(struct platform_device *pdev) acpi_desc->acpi_header = *tbl;/* Evaluate _FIT and override with that if present */ - status =acpi_evaluate_object (adev->handle,"_FIT" ,NULL , &buf); + status =acpi_evaluate_object (ACPI_HANDLE (dev),"_FIT" ,NULL , &buf);if (ACPI_SUCCESS (status) && buf.length >0 ) {union acpi_object *obj = buf.pointer; --
I would appreciate feedback on this, but the change is straightforward, so in the absence of any I'll pick it up assuming no objections.
Rafael J. Wysocki
maintainerPATCH 3/17 ACPI: AC: Switch over to devres-based resource management
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
Use devm_kzalloc() for allocating memory, devm_power_supply_register() for registering a power supply class device and the newly introduced devm_acpi_install_notify_handler() for installing an ACPI notify handler.
Note that the code ordering change related to the third of the above modifications does not matter because there is no order dependency between the battery notifier and the ACPI notify handler.
No intentional functional impact.
--- drivers/acpi/ac.c | 37 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 26 deletions(-)
drivers/acpi/ac.c +11 -26
@@ -193,6 +193,7 @@ static const struct dmi_system_id ac_dmi_table[] __initconst = {static int acpi_ac_probe (struct platform_device *pdev) {struct power_supply_config psy_cfg = {}; +struct device *dev = &pdev->dev;struct acpi_device *adev;struct acpi_ac *ac;int result;
@@ -201,7 +202,7 @@ static int acpi_ac_probe(struct platform_device *pdev)if (!adev)return -ENODEV ; - ac = kzalloc_obj(struct acpi_ac); + ac =devm_kzalloc (dev,sizeof (*ac),GFP_KERNEL );if (!ac)return -ENOMEM ;
@@ -211,7 +212,7 @@ static int acpi_ac_probe(struct platform_device *pdev) result =acpi_ac_get_state (ac);if (result) - goto err_release_ac; +return result; psy_cfg.drv_data = ac;
@@ -220,33 +221,22 @@ static int acpi_ac_probe(struct platform_device *pdev) ac->charger_desc.properties = ac_props; ac->charger_desc.num_properties =ARRAY_SIZE (ac_props); ac->charger_desc.get_property = get_ac_property; - ac->charger =power_supply_register (&pdev->dev, - &ac->charger_desc, &psy_cfg); -if (IS_ERR (ac->charger)) { - result =PTR_ERR (ac->charger); - goto err_release_ac; - } + ac->charger =devm_power_supply_register (dev, &ac->charger_desc, &psy_cfg); +if (IS_ERR (ac->charger)) +return PTR_ERR (ac->charger);pr_info ("AC Adapter [%s] (%s-line)\n" ,acpi_device_bid (adev),str_on_off (ac->state)); + result =devm_acpi_install_notify_handler (dev,ACPI_ALL_NOTIFY , + acpi_ac_notify, ac); +if (result) +return result; + ac->battery_nb.notifier_call = acpi_ac_battery_notify;register_acpi_notifier (&ac->battery_nb); - result =acpi_dev_install_notify_handler (adev,ACPI_ALL_NOTIFY , - acpi_ac_notify, ac); -if (result) - goto err_unregister; -return 0 ; - -err_unregister: -power_supply_unregister (ac->charger); -unregister_acpi_notifier (&ac->battery_nb); -err_release_ac: -kfree (ac); - -return result; }#ifdef CONFIG_PM_SLEEP
@@ -271,12 +261,7 @@ static void acpi_ac_remove(struct platform_device *pdev) {struct acpi_ac *ac =platform_get_drvdata (pdev); -acpi_dev_remove_notify_handler (ac->device,ACPI_ALL_NOTIFY , - acpi_ac_notify); -power_supply_unregister (ac->charger);unregister_acpi_notifier (&ac->battery_nb); - -kfree (ac); }static struct platform_driver acpi_ac_driver = {
Signature · 1 line
2.51.0
Rafael J. Wysocki
maintainerPATCH 4/17 ACPI: battery: Switch over to devres-based resource management
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
The ACPI battery driver already uses devm_kzalloc() for allocating memory and devm_mutex_init() for mutex initialization, but it still carries out some manual rollback in acpi_battery_probe().
Switch it over to devres-based resource management completely by making three changes:
* Rename acpi_battery_update_retry() to devm_acpi_battery_update_retry(), turn sysfs_battery_cleanup() into a devm action and modify the former to add it.
* Add devm_acpi_battery_init_wakeup() for initializing the wakeup source and make it add a custom devm action to automatically remove the wakeup source registered by it.
* Make acpi_battery_probe() use devm_acpi_install_notify_handler() that has just been introduced for installing an ACPI notify handler.
Note that the code ordering change related to the last of the above changes does not matter because there is no functional dependency between the PM notifier and the wakeup source or the ACPI notify handler.
No intentional functional impact.
--- drivers/acpi/battery.c | 75 ++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 36 deletions(-)
drivers/acpi/battery.c +39 -36
@@ -1182,6 +1182,26 @@ static const struct dmi_system_id bat_dmi_table[] __initconst = { {}, }; +static void acpi_battery_wakeup_cleanup (void *data) +{ +device_init_wakeup (data, false); +} + +static int devm_acpi_battery_init_wakeup (struct device *dev) +{ +device_init_wakeup (dev, true); +return devm_add_action_or_reset (dev, acpi_battery_wakeup_cleanup, dev); +} + +static void sysfs_battery_cleanup (void *data) +{ +struct acpi_battery *battery = data; + +guard (mutex)(&battery->update_lock); + +sysfs_remove_battery (battery); +} +/* * Some machines'(E,G Lenovo Z480) ECs are not stable * during boot up and this causes battery driver fails to be
@@ -1190,10 +1210,15 @@ static const struct dmi_system_id bat_dmi_table[] __initconst = {* may work. So add retry code here and 20ms sleep between * every retries. */ -static int acpi_battery_update_retry (struct acpi_battery *battery) +static int devm_acpi_battery_update_retry (struct device *dev, +struct acpi_battery *battery) {int retry, ret; + ret =devm_add_action (dev, sysfs_battery_cleanup, battery); +if (ret) +return ret; +guard (mutex)(&battery->update_lock);for (retry =5 ; retry; retry--) {
@@ -1206,27 +1231,21 @@ static int acpi_battery_update_retry(struct acpi_battery *battery)return ret; } -static void sysfs_battery_cleanup (struct acpi_battery *battery) -{ -guard (mutex)(&battery->update_lock); - -sysfs_remove_battery (battery); -} -static int acpi_battery_probe (struct platform_device *pdev) { +struct device *dev = &pdev->dev;struct acpi_battery *battery;struct acpi_device *device;int result; - device =ACPI_COMPANION (&pdev->dev); + device =ACPI_COMPANION (dev);if (!device)return -ENODEV ;if (device->dep_unmet)return -EPROBE_DEFER ; - battery =devm_kzalloc (&pdev->dev,sizeof (*battery),GFP_KERNEL ); + battery =devm_kzalloc (dev,sizeof (*battery),GFP_KERNEL );if (!battery)return -ENOMEM ;
@@ -1235,54 +1254,38 @@ static int acpi_battery_probe(struct platform_device *pdev) battery->phys_dev = &pdev->dev; battery->device = device; - result =devm_mutex_init (&pdev->dev, &battery->update_lock); + result =devm_mutex_init (dev, &battery->update_lock);if (result)return result;if (acpi_has_method (battery->device->handle,"_BIX" ))set_bit (ACPI_BATTERY_XINFO_PRESENT , &battery->flags); - result =acpi_battery_update_retry (battery); + result =devm_acpi_battery_update_retry (dev, battery);if (result) - goto fail; +return result;pr_info ("Slot [%s] (battery %s)\n" ,acpi_device_bid (device), device->status.battery_present ?"present" :"absent" ); - battery->pm_nb.notifier_call = battery_notify; - result =register_pm_notifier (&battery->pm_nb); + result =devm_acpi_battery_init_wakeup (dev);if (result) - goto fail; - -device_init_wakeup (&pdev->dev, true); +return result; - result =acpi_dev_install_notify_handler (device,ACPI_ALL_NOTIFY , - acpi_battery_notify, battery); + result =devm_acpi_install_notify_handler (dev,ACPI_ALL_NOTIFY , + acpi_battery_notify, battery);if (result) - goto fail_pm; - -return 0 ; - -fail_pm: -device_init_wakeup (&pdev->dev, false); -unregister_pm_notifier (&battery->pm_nb); -fail: -sysfs_battery_cleanup (battery); +return result; -return result; + battery->pm_nb.notifier_call = battery_notify; +return register_pm_notifier (&battery->pm_nb); }static void acpi_battery_remove (struct platform_device *pdev) {struct acpi_battery *battery =platform_get_drvdata (pdev); -acpi_dev_remove_notify_handler (battery->device,ACPI_ALL_NOTIFY , - acpi_battery_notify); - -device_init_wakeup (&pdev->dev, false);unregister_pm_notifier (&battery->pm_nb); - -sysfs_battery_cleanup (battery); }/* this is needed to learn about changes made in suspended state */
Signature · 1 line
2.51.0
Rafael J. Wysocki
maintainerPATCH 5/17 ACPI: HED: Refine guarding against adding a second instance
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
There can be only one ACPI hardware event device (HED) in use at a time, so acpi_hed_probe() uses static variable hed_handle for guarding against adding a second HED instance, but there is no reason for that variable to hold an ACPI handle, so change it to a bool one.
While at it also set that variable at the end of acpi_hed_probe() to avouid the need to clear it when installing the ACPI notify handler fails.
Note that ACPI devices are enumerated sequentially, so there's no need for additional locking around the accesses to that variable.
No intentional functional impact.
--- drivers/acpi/hed.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/acpi/hed.c +6 -6
@@ -22,7 +22,7 @@ static const struct acpi_device_id acpi_hed_ids[] = { };MODULE_DEVICE_TABLE (acpi, acpi_hed_ids); -static acpi_handle hed_handle; +static bool hed_present;static BLOCKING_NOTIFIER_HEAD (acpi_hed_notify_list);
@@ -58,25 +58,25 @@ static int acpi_hed_probe(struct platform_device *pdev)return -ENODEV ;/* Only one hardware error device */ -if (hed_handle) +if (hed_present)return -EINVAL ; - hed_handle = device->handle; err =acpi_dev_install_notify_handler (device,ACPI_DEVICE_NOTIFY , acpi_hed_notify, device);if (err) - hed_handle =NULL ; +return err; -return err; + hed_present = true; +return 0 ; }static void acpi_hed_remove (struct platform_device *pdev) {struct acpi_device *device =ACPI_COMPANION (&pdev->dev); + hed_present = false;acpi_dev_remove_notify_handler (device,ACPI_DEVICE_NOTIFY , acpi_hed_notify); - hed_handle =NULL ; }static struct platform_driver acpi_hed_driver = {
Signature · 1 line
2.51.0
Rafael J. Wysocki
maintainerPATCH 6/17 ACPI: HED: Switch over to devres-based resource management
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
Use the newly introduced devm_acpi_install_notify_handler() for installing an ACPI notify handler and since that function checks the ACPI companion of the owner device against NULL internally, remove the the explicit ACPI companion check from acpi_hed_probe().
No intentional functional impact.
--- drivers/acpi/hed.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-)
drivers/acpi/hed.c +2 -11
@@ -50,19 +50,14 @@ static void acpi_hed_notify(acpi_handle handle, u32 event, void *data)static int acpi_hed_probe (struct platform_device *pdev) { -struct acpi_device *device;int err; - device =ACPI_COMPANION (&pdev->dev); -if (!device) -return -ENODEV ; -/* Only one hardware error device */ if (hed_present)return -EINVAL ; - err =acpi_dev_install_notify_handler (device,ACPI_DEVICE_NOTIFY , - acpi_hed_notify, device); + err =devm_acpi_install_notify_handler (&pdev->dev,ACPI_DEVICE_NOTIFY , + acpi_hed_notify,NULL );if (err)return err;
@@ -72,11 +67,7 @@ static int acpi_hed_probe(struct platform_device *pdev)static void acpi_hed_remove (struct platform_device *pdev) { -struct acpi_device *device =ACPI_COMPANION (&pdev->dev); - hed_present = false; -acpi_dev_remove_notify_handler (device,ACPI_DEVICE_NOTIFY , - acpi_hed_notify); }static struct platform_driver acpi_hed_driver = {
Signature · 1 line
2.51.0
Re: [PATCH v1 06/17] ACPI: HED: Switch over to devres-based resource management
Quoted 6 lines · Rafael J. Wysocki
Use the newly introduced
devm_acpi_install_notify_handler()for installing an ACPI notify handler and since that function checks the ACPI companion of the owner device against NULL internally, remove the the explicit ACPI companion check fromacpi_hed_probe().No intentional functional impact.
...
drivers/acpi/hed.c static int acpi_hed_probe(struct platform_device *pdev) · line 71
Quoted 8 lines
static void
acpi_hed_remove(struct platform_device *pdev){drivers/acpi/hed.c +0
-4-struct acpi_device *device =ACPI_COMPANION (&pdev->dev); - hed_present = false; -acpi_dev_remove_notify_handler (device,ACPI_DEVICE_NOTIFY , - acpi_hed_notify); }
devm will be cleaned after this, right? So, here is a window that if one tries to unbind device from the driver in one thread and do the opposite in another they will see the flag false while resources are still allocated for the old one. It seems that hed_present should be also devm:ed?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Rafael J. Wysocki
maintainerRe: [PATCH v1 06/17] ACPI: HED: Switch over to devres-based resource management
Quoted 21 lines · Andy Shevchenko
Quoted 6 lines · Rafael J. Wysocki
Use the newly introduced
devm_acpi_install_notify_handler()for installing an ACPI notify handler and since that function checks the ACPI companion of the owner device against NULL internally, remove the the explicit ACPI companion check fromacpi_hed_probe().No intentional functional impact.
...
drivers/acpi/hed.c static int acpi_hed_probe(struct platform_device *pdev) · line 71
Quoted 8 lines
static void
acpi_hed_remove(struct platform_device *pdev){drivers/acpi/hed.c +0
-4-struct acpi_device *device =ACPI_COMPANION (&pdev->dev); - hed_present = false; -acpi_dev_remove_notify_handler (device,ACPI_DEVICE_NOTIFY , - acpi_hed_notify); }
Quoted 1 line
devm will be cleaned after this, right?
Yes.
Quoted 4 lines
So, here is a window that if one tries
to unbind device from the driver in one thread and do the opposite in another they will see the flag false while resources are still allocated for the old one. It seems thathed_presentshould be also devm:ed?
If this is the same device, both binding and unbinding (including devm) take place under its device lock which will serialize all of that.
If they are different devices with different ACPI companions, there's no resource conflict. If they are both valid HEDs (which would be a stretch already) and they both trigger a notification exactly at the "wrong" time (more stretch), worst-case stuff will be called twice in a row via blocking_notifier_call_chain().
Re: [PATCH v1 06/17] ACPI: HED: Switch over to devres-based resource management
Quoted 3 lines · Rafael J. Wysocki
Quoted 1 line · Andy Shevchenko
On Thu, May 21, 2026 at 04:04:03PM +0200, Rafael J. Wysocki wrote:
...
Quoted 22 lines
Quoted 10 lines
drivers/acpi/hed.c static int acpi_hed_probe(struct platform_device *pdev) · line 71
Quoted 8 lines
static void
acpi_hed_remove(struct platform_device *pdev){drivers/acpi/hed.c +0
-4-struct acpi_device *device =ACPI_COMPANION (&pdev->dev); - hed_present = false; -acpi_dev_remove_notify_handler (device,ACPI_DEVICE_NOTIFY , - acpi_hed_notify); }devm will be cleaned after this, right?
Yes.
Quoted 4 lines
So, here is a window that if one tries
to unbind device from the driver in one thread and do the opposite in another they will see the flag false while resources are still allocated for the old one. It seems thathed_presentshould be also devm:ed?If this is the same device, both binding and unbinding (including devm) take place under its device lock which will serialize all of that.
Quoted 5 lines
If they are different devices with different ACPI companions, there's no resource conflict. If they are both valid HEDs (which would be a stretch already) and they both trigger a notification exactly at the "wrong" time (more stretch), worst-case stuff will be called twice in a row via
blocking_notifier_call_chain().
Thanks for elaborating. We are good then.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Rafael J. Wysocki
maintainerPATCH 7/17 ACPI: thermal: Switch over to devres-based resource management
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
Switch over the ACPI thermal zone driver to devres-based resource management by making the following changes:
* Turn acpi_thermal_zone_free() into a devm action added from acpi_thermal_probe() after allocating the struct acpi_thermal object.
* Rename acpi_thermal_unregister_thermal_zone() to acpi_thermal_zone_unregister(), add acpi_thermal_pm_queue flushing to it, and turn it into a devm action added by acpi_thermal_probe() after calling acpi_thermal_register_thermal_zone().
* Use the newly introduced devm_acpi_install_notify_handler() for installing an ACPI notify handler.
* Drop acpi_thermal_remove() that is not necessary any more.
No intentional functional impact.
--- drivers/acpi/thermal.c | 53 +++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 32 deletions(-)
drivers/acpi/thermal.c +21 -32
@@ -655,8 +655,12 @@ static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz,return result; } -static void acpi_thermal_unregister_thermal_zone (struct acpi_thermal *tz) +static void acpi_thermal_zone_unregister (void *data) { +struct acpi_thermal *tz = data; + +flush_workqueue (acpi_thermal_pm_queue); +thermal_zone_device_disable (tz->thermal_zone);acpi_thermal_zone_sysfs_remove (tz);thermal_zone_device_unregister (tz->thermal_zone);
@@ -765,8 +769,9 @@ static void acpi_thermal_check_fn(struct work_struct *work)mutex_unlock (&tz->thermal_check_lock); } -static void acpi_thermal_free_thermal_zone (struct acpi_thermal *tz) +static void acpi_thermal_zone_free (void *data) { +struct acpi_thermal *tz = data;int i;acpi_handle_list_free (&tz->trips.passive.trip.devices);
@@ -779,7 +784,8 @@ static void acpi_thermal_free_thermal_zone(struct acpi_thermal *tz)static int acpi_thermal_probe (struct platform_device *pdev) {struct thermal_trip trip_table[ACPI_THERMAL_MAX_NR_TRIPS ] = {0 }; -struct acpi_device *device =ACPI_COMPANION (&pdev->dev); +struct device *dev = &pdev->dev; +struct acpi_device *device =ACPI_COMPANION (dev);struct acpi_thermal_trip *acpi_trip;struct thermal_trip *trip;struct acpi_thermal *tz;
@@ -795,6 +801,10 @@ static int acpi_thermal_probe(struct platform_device *pdev)if (!tz)return -ENOMEM ; + result =devm_add_action_or_reset (dev, acpi_thermal_zone_free, tz); +if (result) +return result; +platform_set_drvdata (pdev, tz); tz->device = device;
@@ -817,7 +827,7 @@ static int acpi_thermal_probe(struct platform_device *pdev)/* Get temperature [_TMP] (required). */ result =acpi_thermal_get_temperature (tz);if (result) - goto free_memory; +return result;/* Determine the default polling frequency [_TZP]. */ if (tzp)
@@ -870,7 +880,11 @@ static int acpi_thermal_probe(struct platform_device *pdev) trip - trip_table, passive_delay);if (result) - goto free_memory; +return result; + + result =devm_add_action_or_reset (dev, acpi_thermal_zone_unregister, tz); +if (result) +return result;refcount_set (&tz->thermal_check_count,3 );mutex_init (&tz->thermal_check_lock);
@@ -879,32 +893,8 @@ static int acpi_thermal_probe(struct platform_device *pdev)pr_info ("Thermal Zone [%s] (%ld C)\n" ,acpi_device_bid (device),deci_kelvin_to_celsius (tz->temp_dk)); - result =acpi_dev_install_notify_handler (device,ACPI_DEVICE_NOTIFY , - acpi_thermal_notify, tz); -if (result) - goto flush_wq; - -return 0 ; - -flush_wq: -flush_workqueue (acpi_thermal_pm_queue); -acpi_thermal_unregister_thermal_zone (tz); -free_memory: -acpi_thermal_free_thermal_zone (tz); - -return result; -} - -static void acpi_thermal_remove (struct platform_device *pdev) -{ -struct acpi_thermal *tz =platform_get_drvdata (pdev); - -acpi_dev_remove_notify_handler (tz->device,ACPI_DEVICE_NOTIFY , - acpi_thermal_notify); - -flush_workqueue (acpi_thermal_pm_queue); -acpi_thermal_unregister_thermal_zone (tz); -acpi_thermal_free_thermal_zone (tz); +return devm_acpi_install_notify_handler (dev,ACPI_DEVICE_NOTIFY , + acpi_thermal_notify, tz); }#ifdef CONFIG_PM_SLEEP
@@ -937,7 +927,6 @@ MODULE_DEVICE_TABLE(acpi, thermal_device_ids);static struct platform_driver acpi_thermal_driver = { .probe = acpi_thermal_probe, - .remove = acpi_thermal_remove, .driver = { .name ="acpi-thermal" , .acpi_match_table = thermal_device_ids,
Signature · 1 line
2.51.0
Rafael J. Wysocki
maintainerPATCH 8/17 ACPI: PAD: Rearrange acpi_pad_notify()
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
Use an if () in acpi_pad_notify() instead of a switch () statement to make the code somewhat easier to follow and reduce its indentation level.
No intentional functional impact.
--- drivers/acpi/acpi_pad.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/acpi/acpi_pad.c +5 -8
@@ -409,16 +409,13 @@ static void acpi_pad_notify(acpi_handle handle, u32 event, void *data) {struct acpi_device *adev = data; -switch (event) { -case ACPI_PROCESSOR_AGGREGATOR_NOTIFY : -acpi_pad_handle_notify (handle); -acpi_bus_generate_netlink_event ("acpi_pad" , -dev_name (&adev->dev), event,0 ); -break ; -default : +if (event !=ACPI_PROCESSOR_AGGREGATOR_NOTIFY ) {pr_warn ("Unsupported event [0x%x]\n" , event); -break ; +return ; } + +acpi_pad_handle_notify (handle); +acpi_bus_generate_netlink_event ("acpi_pad" ,dev_name (&adev->dev), event,0 ); }static int acpi_pad_probe (struct platform_device *pdev)
Signature · 1 line
2.51.0
Rafael J. Wysocki
maintainerPATCH 9/17 ACPI: PAD: Pass struct device pointer to acpi_pad_notify()
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
Use the struct device pointer to the dev member in the struct platform_device object representing the platform device used for driver binding as the last argument of acpi_dev_install_notify_handler() and accordingly update acpi_pad_notify() to pass that pointer directly to dev_name() when generating the netlink event.
Since the dev_name() value for an ACPI-enumerated platform device is the same as the dev_name() value for the dev member of its ACPI companion object, as per acpi_create_platform_device(), the above code modification is not expected to cause functionality to change.
--- drivers/acpi/acpi_pad.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/acpi/acpi_pad.c +2 -4
@@ -407,15 +407,13 @@ static void acpi_pad_handle_notify(acpi_handle handle)static void acpi_pad_notify (acpi_handle handle,u32 event,void *data) { -struct acpi_device *adev = data; -if (event !=ACPI_PROCESSOR_AGGREGATOR_NOTIFY ) {pr_warn ("Unsupported event [0x%x]\n" , event);return ; }acpi_pad_handle_notify (handle); -acpi_bus_generate_netlink_event ("acpi_pad" ,dev_name (&adev->dev), event,0 ); +acpi_bus_generate_netlink_event ("acpi_pad" ,dev_name (data), event,0 ); }static int acpi_pad_probe (struct platform_device *pdev)
@@ -427,7 +425,7 @@ static int acpi_pad_probe(struct platform_device *pdev)return -ENODEV ;return acpi_dev_install_notify_handler (adev,ACPI_DEVICE_NOTIFY , - acpi_pad_notify, adev); + acpi_pad_notify, &pdev->dev); }static void acpi_pad_remove (struct platform_device *pdev)
Signature · 1 line
2.51.0
Rafael J. Wysocki
maintainerPATCH 10/17 ACPI: PAD: Fix teardown ordering in acpi_pad_remove()
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
The ACPI notify handler installed by acpi_pad_probe() needs to be removed before calling acpi_pad_idle_cpus() in acpi_pad_remove() so it doesn't schedule idle time injection on some CPUs again.
--- drivers/acpi/acpi_pad.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/acpi/acpi_pad.c +3 -3
@@ -430,12 +430,12 @@ static int acpi_pad_probe(struct platform_device *pdev)static void acpi_pad_remove (struct platform_device *pdev) { +acpi_dev_remove_notify_handler (ACPI_COMPANION (&pdev->dev), +ACPI_DEVICE_NOTIFY , acpi_pad_notify); +mutex_lock (&isolated_cpus_lock);acpi_pad_idle_cpus (0 );mutex_unlock (&isolated_cpus_lock); - -acpi_dev_remove_notify_handler (ACPI_COMPANION (&pdev->dev), -ACPI_DEVICE_NOTIFY , acpi_pad_notify); }static const struct acpi_device_id pad_device_ids[] = {
Signature · 1 line
2.51.0
Rafael J. Wysocki
maintainerPATCH 11/17 ACPI: PAD: Switch over to devres-based resource management
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
Use the newly introduced devm_acpi_install_notify_handler() for installing an ACPI notify handler and since that function checks the ACPI companion of the owner device against NULL internally, remove the the explicit ACPI companion check from acpi_pad_probe().
However, to prevent the notify handler from running acpi_pad_idle_cpus() with the number of idle CPUs greater than zero after acpi_pad_remove() has returned, add a bool static variable for synchronization between the two.
--- drivers/acpi/acpi_pad.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
drivers/acpi/acpi_pad.c +9 -10
@@ -31,6 +31,8 @@static DEFINE_MUTEX (isolated_cpus_lock);static DEFINE_MUTEX (round_robin_lock); +static bool acpi_pad_teardown; +static unsigned int power_saving_mwait_eax;static unsigned char tsc_detected_unstable;
@@ -359,6 +361,9 @@ static int acpi_pad_pur(acpi_handle handle)union acpi_object *package;int num =-1 ; +if (unlikely (acpi_pad_teardown)) +return -1 ; +if (ACPI_FAILURE (acpi_evaluate_object (handle,"_PUR" ,NULL , &buffer)))return num;
@@ -418,22 +423,16 @@ static void acpi_pad_notify(acpi_handle handle, u32 event, void *data)static int acpi_pad_probe (struct platform_device *pdev) { -struct acpi_device *adev; + acpi_pad_teardown = false; - adev =ACPI_COMPANION (&pdev->dev); -if (!adev) -return -ENODEV ; - -return acpi_dev_install_notify_handler (adev,ACPI_DEVICE_NOTIFY , - acpi_pad_notify, &pdev->dev); +return devm_acpi_install_notify_handler (&pdev->dev,ACPI_DEVICE_NOTIFY , + acpi_pad_notify, &pdev->dev); }static void acpi_pad_remove (struct platform_device *pdev) { -acpi_dev_remove_notify_handler (ACPI_COMPANION (&pdev->dev), -ACPI_DEVICE_NOTIFY , acpi_pad_notify); -mutex_lock (&isolated_cpus_lock); + acpi_pad_teardown = true;acpi_pad_idle_cpus (0 );mutex_unlock (&isolated_cpus_lock); }
Signature · 1 line
2.51.0
Rafael J. Wysocki
maintainerPATCH 12/17 ACPI: video: Reduce the number of auxiliary device dereferences
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
Store the &aux_dev->dev pointer in a separate local variable in acpi_video_bus_probe() to avoid dereferencing aux_dev many times.
No intentional functional impact.
--- drivers/acpi/acpi_video.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/acpi/acpi_video.c +4 -3
@@ -1978,7 +1978,8 @@ static bool acpi_video_bus_dev_is_duplicate(struct device *dev)static int acpi_video_bus_probe (struct auxiliary_device *aux_dev,const struct auxiliary_device_id *id_unused) { -struct acpi_device *device =ACPI_COMPANION (&aux_dev->dev); +struct device *dev = &aux_dev->dev; +struct acpi_device *device =ACPI_COMPANION (dev);static DEFINE_MUTEX (probe_lock);struct acpi_video_bus *video;static int instance;
@@ -1988,7 +1989,7 @@ static int acpi_video_bus_probe(struct auxiliary_device *aux_dev,/* Probe one video bus device at a time in case there are duplicates. */ guard (mutex)(&probe_lock); -if (!allow_duplicates &&acpi_video_bus_dev_is_duplicate (&aux_dev->dev)) { +if (!allow_duplicates &&acpi_video_bus_dev_is_duplicate (dev)) {pr_info (FW_BUG "Duplicate ACPI video bus devices for the" " same VGA controller, please try module "
@@ -2059,7 +2060,7 @@ static int acpi_video_bus_probe(struct auxiliary_device *aux_dev, !auto_detect)acpi_video_bus_register_backlight (video); - error =acpi_video_bus_add_notify_handler (video, &aux_dev->dev); + error =acpi_video_bus_add_notify_handler (video, dev);if (error) goto err_del;
Signature · 1 line
2.51.0
Rafael J. Wysocki
maintainerPATCH 13/17 ACPI: video: Rearrange probe and remove code
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
Rearrange some ACPI video bus probe and remove code so that it is more clear that the probe and removal are carried in reverse orders, which will also facilitate subsequent changes.
No intentional functional impact.
--- drivers/acpi/acpi_video.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/acpi/acpi_video.c +10 -10
@@ -2002,6 +2002,9 @@ static int acpi_video_bus_probe(struct auxiliary_device *aux_dev,if (!video)return -ENOMEM ; + video->device = device; + device->driver_data = video; +/* * A hack to fix the duplicate name "VID" problem on T61 and the * duplicate name "VGA" problem on Pa 3553.
@@ -2016,9 +2019,6 @@ static int acpi_video_bus_probe(struct auxiliary_device *aux_dev,auxiliary_set_drvdata (aux_dev, video); - video->device = device; - device->driver_data = video; -acpi_video_bus_find_cap (video); error =acpi_video_bus_check (video);if (error)
@@ -2041,10 +2041,6 @@ static int acpi_video_bus_probe(struct auxiliary_device *aux_dev,acpi_device_bid (device),str_yes_no (video->flags.multihead), str_yes_no(video->flags.rom), str_yes_no(video->flags.post)); -mutex_lock (&video_list_lock); -list_add_tail (&video->entry, &video_bus_head); -mutex_unlock (&video_list_lock); -/* * If backlight-type auto-detection is used then a native backlight may * show up later and this may change the result from video to native.
@@ -2060,6 +2056,10 @@ static int acpi_video_bus_probe(struct auxiliary_device *aux_dev, !auto_detect)acpi_video_bus_register_backlight (video); +mutex_lock (&video_list_lock); +list_add_tail (&video->entry, &video_bus_head); +mutex_unlock (&video_list_lock); + error =acpi_video_bus_add_notify_handler (video, dev);if (error) goto err_del;
@@ -2096,15 +2096,15 @@ static void acpi_video_bus_remove(struct auxiliary_device *aux_dev)acpi_dev_remove_notify_handler (device,ACPI_DEVICE_NOTIFY , acpi_video_bus_notify); +acpi_video_bus_remove_notify_handler (video); +mutex_lock (&video_list_lock);list_del (&video->entry);mutex_unlock (&video_list_lock); - -acpi_video_bus_remove_notify_handler (video);acpi_video_bus_unregister_backlight (video);acpi_video_bus_put_devices (video); -kfree (video->attached_array); +kfree (video); device->driver_data =NULL ; }
Signature · 1 line
2.51.0
Rafael J. Wysocki
maintainerPATCH 14/17 ACPI: video: Use devm action for video bus object cleanup
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
Introduce acpi_video_bus_free() for freeing video bus object memory and reversing changes related to it made during ACPI video bus device probe, modify acpi_video_bus_probe() to add acpi_video_bus_free() as a devm action, and remove the code superseded by it from acpi_video_bus_remove().
No intentional functional impact.
--- drivers/acpi/acpi_video.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
drivers/acpi/acpi_video.c +13 -7
@@ -1953,6 +1953,14 @@ static int acpi_video_bus_put_devices(struct acpi_video_bus *video)return 0 ; } +static void acpi_video_bus_free (void *data) +{ +struct acpi_video_bus *video = data; + + video->device->driver_data =NULL ; +kfree (video); +} +static int duplicate_dev_check (struct device *sibling,void *data) {struct acpi_video_bus *video;
@@ -2005,6 +2013,10 @@ static int acpi_video_bus_probe(struct auxiliary_device *aux_dev, video->device = device; device->driver_data = video; + error =devm_add_action_or_reset (dev, acpi_video_bus_free, video); +if (error) +return error; +/* * A hack to fix the duplicate name "VID" problem on T61 and the * duplicate name "VGA" problem on Pa 3553.
@@ -2022,7 +2034,7 @@ static int acpi_video_bus_probe(struct auxiliary_device *aux_dev,acpi_video_bus_find_cap (video); error =acpi_video_bus_check (video);if (error) - goto err_free_video; +return error;mutex_init (&video->device_list_lock);INIT_LIST_HEAD (&video->video_device_list);
@@ -2081,9 +2093,6 @@ static int acpi_video_bus_probe(struct auxiliary_device *aux_dev, err_put_video:acpi_video_bus_put_devices (video);kfree (video->attached_array); -err_free_video: -kfree (video); - device->driver_data =NULL ;return error; }
@@ -2104,9 +2113,6 @@ static void acpi_video_bus_remove(struct auxiliary_device *aux_dev)acpi_video_bus_unregister_backlight (video);acpi_video_bus_put_devices (video);kfree (video->attached_array); - -kfree (video); - device->driver_data =NULL ; }static int __init is_i740 (struct pci_dev *dev)
Signature · 1 line
2.51.0
Rafael J. Wysocki
maintainerPATCH 15/17 ACPI: video: Use devm action for freeing video devices
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
Rename acpi_video_bus_put_devices() to devm_acpi_video_bus_get_devices() and turn acpi_video_bus_put_devices() into a devm action added by it for freeing the video devices allocated by it and the attached_array memory.
Accordingly, remove the acpi_video_bus_put_devices() calls and attached_array freeing from acpi_video_bus_remove() and the rollback path in acpi_video_bus_probe().
No intentional functional impact.
--- drivers/acpi/acpi_video.c | 53 +++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 25 deletions(-)
drivers/acpi/acpi_video.c +28 -25
@@ -1494,10 +1494,31 @@ int acpi_video_get_edid(struct acpi_device *device, int type, int device_id, }EXPORT_SYMBOL (acpi_video_get_edid); -static int -acpi_video_bus_get_devices (struct acpi_video_bus *video, -struct acpi_device *device) +static void acpi_video_bus_put_devices (void *data) +{ +struct acpi_video_bus *video = data; +struct acpi_video_device *dev, *next; + +mutex_lock (&video->device_list_lock); +list_for_each_entry_safe (dev, next, &video->video_device_list, entry) { +list_del (&dev->entry); +kfree (dev); + } +mutex_unlock (&video->device_list_lock); + +kfree (video->attached_array); + video->attached_array =NULL ; +} + +static int devm_acpi_video_bus_get_devices (struct device *dev, +struct acpi_video_bus *video) { +int ret; + + ret =devm_add_action (dev, acpi_video_bus_put_devices, video); +if (ret) +return ret; +/* * There are systems where video module known to work fine regardless * of broken _DOD and ignoring returned value here doesn't cause
@@ -1505,7 +1526,8 @@ acpi_video_bus_get_devices(struct acpi_video_bus *video,*/ acpi_video_device_enumerate (video); -return acpi_dev_for_each_child (device, acpi_video_bus_get_one_device, video); +return acpi_dev_for_each_child (video->device, + acpi_video_bus_get_one_device, video); }/* acpi_video interface */
@@ -1939,20 +1961,6 @@ static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video) video->input =NULL ; } -static int acpi_video_bus_put_devices (struct acpi_video_bus *video) -{ -struct acpi_video_device *dev, *next; - -mutex_lock (&video->device_list_lock); -list_for_each_entry_safe (dev, next, &video->video_device_list, entry) { -list_del (&dev->entry); -kfree (dev); - } -mutex_unlock (&video->device_list_lock); - -return 0 ; -} -static void acpi_video_bus_free (void *data) {struct acpi_video_bus *video = data;
@@ -2039,9 +2047,9 @@ static int acpi_video_bus_probe(struct auxiliary_device *aux_dev,mutex_init (&video->device_list_lock);INIT_LIST_HEAD (&video->video_device_list); - error =acpi_video_bus_get_devices (video, device); + error =devm_acpi_video_bus_get_devices (dev, video);if (error) - goto err_put_video; +return error;/* * HP ZBook Fury 16 G10 requires ACPI video's child devices have _PS0
@@ -2090,9 +2098,6 @@ static int acpi_video_bus_probe(struct auxiliary_device *aux_dev,list_del (&video->entry);mutex_unlock (&video_list_lock);acpi_video_bus_unregister_backlight (video); -err_put_video: -acpi_video_bus_put_devices (video); -kfree (video->attached_array);return error; }
@@ -2111,8 +2116,6 @@ static void acpi_video_bus_remove(struct auxiliary_device *aux_dev)list_del (&video->entry);mutex_unlock (&video_list_lock);acpi_video_bus_unregister_backlight (video); -acpi_video_bus_put_devices (video); -kfree (video->attached_array); }static int __init is_i740 (struct pci_dev *dev)
Signature · 1 line
2.51.0
Rafael J. Wysocki
maintainerPATCH 16/17 ACPI: video: Use devm for video->entry and backlight cleanup
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
Introduce acpi_video_bus_del() for removing the video bus object from the video_bus_head list and unregistering backlight and make acpi_video_bus_probe() add it as a devm action after adding the video bus object to the video_bus_head list.
Accordingly, remove the code superseded by it from acpi_video_bus_remove() and from the rollback path in acpi_video_bus_probe().
No intentional functional impact.
--- drivers/acpi/acpi_video.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-)
drivers/acpi/acpi_video.c +16 -11
@@ -1969,6 +1969,17 @@ static void acpi_video_bus_free(void *data)kfree (video); } +static void acpi_video_bus_del (void *data) +{ +struct acpi_video_bus *video = data; + +mutex_lock (&video_list_lock); +list_del (&video->entry); +mutex_unlock (&video_list_lock); + +acpi_video_bus_unregister_backlight (video); +} +static int duplicate_dev_check (struct device *sibling,void *data) {struct acpi_video_bus *video;
@@ -2080,9 +2091,13 @@ static int acpi_video_bus_probe(struct auxiliary_device *aux_dev,list_add_tail (&video->entry, &video_bus_head);mutex_unlock (&video_list_lock); + error =devm_add_action_or_reset (dev, acpi_video_bus_del, video); +if (error) +return error; + error =acpi_video_bus_add_notify_handler (video, dev);if (error) - goto err_del; +return error; error =acpi_dev_install_notify_handler (device,ACPI_DEVICE_NOTIFY , acpi_video_bus_notify, video);
@@ -2093,11 +2108,6 @@ static int acpi_video_bus_probe(struct auxiliary_device *aux_dev, err_remove:acpi_video_bus_remove_notify_handler (video); -err_del: -mutex_lock (&video_list_lock); -list_del (&video->entry); -mutex_unlock (&video_list_lock); -acpi_video_bus_unregister_backlight (video);return error; }
@@ -2111,11 +2121,6 @@ static void acpi_video_bus_remove(struct auxiliary_device *aux_dev) acpi_video_bus_notify);acpi_video_bus_remove_notify_handler (video); - -mutex_lock (&video_list_lock); -list_del (&video->entry); -mutex_unlock (&video_list_lock); -acpi_video_bus_unregister_backlight (video); }static int __init is_i740 (struct pci_dev *dev)
Signature · 1 line
2.51.0
Rafael J. Wysocki
maintainerPATCH 17/17 ACPI: video: Switch over to devres-based resource management
In reply to Rafael J. WysockiFrom: "Rafael J. Wysocki" <[email protected]>
Turn acpi_video_bus_remove_notify_handler() into a devm action added by acpi_video_bus_probe() after calling acpi_video_bus_add_notify_handler and use the newly introduced devm_acpi_install_notify_handler() to install an ACPI notify handler for the video bus device.
This replaces the rollback path remnant in acpi_video_bus_probe() and allows acpi_video_bus_remove() to be dropped altogether.
No intentional functional impact.
--- drivers/acpi/acpi_video.c | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-)
drivers/acpi/acpi_video.c +7 -23
@@ -76,7 +76,6 @@ static DEFINE_MUTEX(video_list_lock);static LIST_HEAD (video_bus_head);static int acpi_video_bus_probe (struct auxiliary_device *aux_dev,const struct auxiliary_device_id *id); -static void acpi_video_bus_remove (struct auxiliary_device *aux);static void acpi_video_bus_notify (acpi_handle handle,u32 event,void *data);/*
@@ -99,7 +98,6 @@ MODULE_DEVICE_TABLE(auxiliary, video_bus_auxiliary_id_table);static struct auxiliary_driver acpi_video_bus = { .probe = acpi_video_bus_probe, - .remove = acpi_video_bus_remove, .id_table = video_bus_auxiliary_id_table, };
@@ -1945,8 +1943,9 @@ static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev) } } -static void acpi_video_bus_remove_notify_handler (struct acpi_video_bus *video) +static void acpi_video_bus_remove_notify_handler (void *data) { +struct acpi_video_bus *video = data;struct acpi_video_device *dev;mutex_lock (&video->device_list_lock);
@@ -2099,28 +2098,13 @@ static int acpi_video_bus_probe(struct auxiliary_device *aux_dev,if (error)return error; - error =acpi_dev_install_notify_handler (device,ACPI_DEVICE_NOTIFY , - acpi_video_bus_notify, video); + error =devm_add_action_or_reset (dev, acpi_video_bus_remove_notify_handler, + video);if (error) - goto err_remove; - -return 0 ; - -err_remove: -acpi_video_bus_remove_notify_handler (video); - -return error; -} - -static void acpi_video_bus_remove (struct auxiliary_device *aux_dev) -{ -struct acpi_video_bus *video =auxiliary_get_drvdata (aux_dev); -struct acpi_device *device =ACPI_COMPANION (&aux_dev->dev); - -acpi_dev_remove_notify_handler (device,ACPI_DEVICE_NOTIFY , - acpi_video_bus_notify); +return error; -acpi_video_bus_remove_notify_handler (video); +return devm_acpi_install_notify_handler (dev,ACPI_DEVICE_NOTIFY , + acpi_video_bus_notify, video); }static int __init is_i740 (struct pci_dev *dev)
Signature · 1 line
2.51.0