ACPI / hotplug: Fix concurrency issues and memory leaks
Subsystems (3)
PATCH 1/1 ACPI / hotplug: Fix concurrency issues and memory leaks
From: Rafael J. Wysocki <[email protected]>
This changeset is aimed at fixing a few different but related problems in the ACPI hotplug infrastructure.
First of all, since notify handlers may be run in parallel with acpi_bus_scan(), acpi_bus_trim() and acpi_bus_hot_remove_device() and some of them are installed for ACPI handles that have no struct acpi_device objects attached (i.e. before those objects are created), those notify handlers have to take acpi_scan_lock to prevent races from taking place (e.g. a struct acpi_device is found to be present for the given ACPI handle, but right after that it is removed by acpi_bus_trim() running in parallel to the given notify handler). Moreover, since some of them call acpi_bus_scan() and acpi_bus_trim(), this leads to the conclusion that acpi_scan_lock should be acquired by the callers of these two funtions rather by these functions themselves.
For these reasons, make all notify handlers that can handle device addition and eject events take acpi_scan_lock and remove the acpi_scan_lock locking from acpi_bus_scan() and acpi_bus_trim(). Accordingly, update all of their users to make sure that they are always called under acpi_scan_lock.
Furthermore, since eject operations are carried out asynchronously with respect to the notify events that trigger them, with the help of acpi_bus_hot_remove_device(), even if notify handlers take the ACPI scan lock, it still is possible that, for example, acpi_bus_trim() will run between acpi_bus_hot_remove_device() and the notify handler that scheduled its execution and that acpi_bus_trim() will remove the device node passed to acpi_bus_hot_remove_device() for ejection. In that case, the struct acpi_device object obtained by acpi_bus_hot_remove_device() will be invalid and not-so-funny things will ensue. To protect agaist that, make the users of acpi_bus_hot_remove_device() run get_device() on ACPI device node objects that are about to be passed to it and make acpi_bus_hot_remove_device() run put_device() on them and check if their ACPI handles are not NULL (make acpi_device_unregister() clear the device nodes' ACPI handles for that check to work).
Finally, observe that acpi_os_hotplug_execute() actually can fail, in which case its caller ought to free memory allocated for the context object to prevent leaks from happening. It also needs to run put_device() on the device node that it ran get_device() on previously in that case. Modify the code accordingly.
---
On top of linux-pm.git/linux-next.
Thanks,
Rafael
--- drivers/acpi/acpi_memhotplug.c | 56 +++++++++++++++++++----------- drivers/acpi/container.c | 10 +++-- drivers/acpi/dock.c | 19 ++++++++-- drivers/acpi/processor_driver.c | 24 +++++++++--- drivers/acpi/scan.c | 69 +++++++++++++++++++++++++------------ drivers/pci/hotplug/acpiphp_glue.c | 6 +++ drivers/pci/hotplug/sgi_hotplug.c | 5 ++ include/acpi/acpi_bus.h | 3 + 8 files changed, 138 insertions(+), 54 deletions(-)
drivers/acpi/scan.c +48 -21
@@ -42,6 +42,18 @@ struct acpi_device_bus_id{struct list_head node; }; +void acpi_scan_lock_acquire (void ) +{ +mutex_lock (&acpi_scan_lock); +} +EXPORT_SYMBOL_GPL (acpi_scan_lock_acquire); + +void acpi_scan_lock_release (void ) +{ +mutex_unlock (&acpi_scan_lock); +} +EXPORT_SYMBOL_GPL (acpi_scan_lock_release); +int acpi_scan_add_handler (struct acpi_scan_handler *handler) {if (!handler || !handler->attach)
@@ -95,8 +107,6 @@ acpi_device_modalias_show(struct device } static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL); -static void __acpi_bus_trim (struct acpi_device *start); -/** * acpi_bus_hot_remove_device: hot-remove a device and its children * @context: struct acpi_eject_event pointer (freed in this func)
@@ -107,7 +117,7 @@ static void __acpi_bus_trim(struct acpi_*/ void acpi_bus_hot_remove_device (void *context) { -struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context; +struct acpi_eject_event *ej_event = context;struct acpi_device *device = ej_event->device;acpi_handle handle = device->handle;acpi_handle temp;
@@ -118,11 +128,19 @@ void acpi_bus_hot_remove_device(void *comutex_lock (&acpi_scan_lock); +/* If there is no handle, the device node has been unregistered. */ +if (!device->handle) { +dev_dbg (&device->dev,"ACPI handle missing\n" ); +put_device (&device->dev); + goto out; + } +ACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"Hot-removing device %s...\n" ,dev_name (&device->dev))); -__acpi_bus_trim (device); -/* Device node has been released. */ +acpi_bus_trim (device); +/* Device node has been unregistered. */ +put_device (&device->dev); device =NULL ;if (ACPI_SUCCESS (acpi_get_handle (handle,"_LCK" , &temp))) {
@@ -151,6 +169,7 @@ void acpi_bus_hot_remove_device(void *co ost_code, NULL); } + out:mutex_unlock (&acpi_scan_lock);kfree (context);return ;
@@ -212,6 +231,7 @@ acpi_eject_store(struct device *d, struc goto err; } +get_device (&acpi_device->dev); ej_event->device = acpi_device;if (acpi_device->flags.eject_pending) {/* event originated from ACPI eject notification */
@@ -224,7 +244,11 @@ acpi_eject_store(struct device *d, struc ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL); } -acpi_os_hotplug_execute (acpi_bus_hot_remove_device, (void *)ej_event); + status =acpi_os_hotplug_execute (acpi_bus_hot_remove_device, ej_event); +if (ACPI_FAILURE (status)) { +put_device (&acpi_device->dev); +kfree (ej_event); + } err:return ret; }
@@ -779,6 +803,7 @@ static void acpi_device_unregister(struc* no more references. */ acpi_device_set_power (device,ACPI_STATE_D3_COLD ); + device->handle =NULL ;put_device (&device->dev); }
@@ -1626,14 +1651,14 @@ static acpi_status acpi_bus_device_attac* there has been a real error. There just have been no suitable ACPI objects * in the table trunk from which the kernel could create a device and add an * appropriate driver. +* +* Must be called under acpi_scan_lock. */ int acpi_bus_scan (acpi_handle handle) {void *device =NULL ;int error =0 ; -mutex_lock (&acpi_scan_lock); -if (ACPI_SUCCESS (acpi_bus_check_add (handle,0 ,NULL , &device)))acpi_walk_namespace (ACPI_TYPE_ANY , handle,ACPI_UINT32_MAX , acpi_bus_check_add,NULL ,NULL , &device);
@@ -1644,7 +1669,6 @@ int acpi_bus_scan(acpi_handle handle)acpi_walk_namespace (ACPI_TYPE_ANY , handle,ACPI_UINT32_MAX , acpi_bus_device_attach,NULL ,NULL ,NULL ); -mutex_unlock (&acpi_scan_lock);return error; }EXPORT_SYMBOL (acpi_bus_scan);
@@ -1681,7 +1705,13 @@ static acpi_status acpi_bus_remove(acpi_return AE_OK ; } -static void __acpi_bus_trim (struct acpi_device *start) +/** +* acpi_bus_trim - Remove ACPI device node and all of its descendants +* @start: Root of the ACPI device nodes subtree to remove. +* +* Must be called under acpi_scan_lock. +*/ +void acpi_bus_trim (struct acpi_device *start) {/* * Execute acpi_bus_device_detach() as a post-order callback to detach
@@ -1698,13 +1728,6 @@ static void __acpi_bus_trim(struct acpi_ acpi_bus_remove, NULL, NULL);acpi_bus_remove (start->handle,0 ,NULL ,NULL ); } - -void acpi_bus_trim (struct acpi_device *start) -{ -mutex_lock (&acpi_scan_lock); -__acpi_bus_trim (start); -mutex_unlock (&acpi_scan_lock); -}EXPORT_SYMBOL_GPL (acpi_bus_trim);static int acpi_bus_scan_fixed (void )
@@ -1762,23 +1785,27 @@ int __init acpi_scan_init(void)acpi_csrt_init ();acpi_container_init (); +mutex_lock (&acpi_scan_lock);/* * Enumerate devices in the ACPI namespace. */ result =acpi_bus_scan (ACPI_ROOT_OBJECT );if (result) -return result; + goto out; result =acpi_bus_get_device (ACPI_ROOT_OBJECT , &acpi_root);if (result) -return result; + goto out; result =acpi_bus_scan_fixed ();if (result) {acpi_device_unregister (acpi_root); -return result; + goto out; }acpi_update_all_gpes (); -return 0 ; + + out: +mutex_unlock (&acpi_scan_lock); +return result; }
include/acpi/acpi_bus.h +3 -0
@@ -395,6 +395,9 @@ int acpi_bus_receive_event(struct acpi_bstatic inline int acpi_bus_generate_proc_event (struct acpi_device *device,u8 type,int data) {return 0 ; }#endif + +void acpi_scan_lock_acquire (void ); +void acpi_scan_lock_release (void );int acpi_scan_add_handler (struct acpi_scan_handler *handler);int acpi_bus_register_driver (struct acpi_driver *driver);void acpi_bus_unregister_driver (struct acpi_driver *driver);
drivers/acpi/acpi_memhotplug.c +37 -19
116 lines not on this page · raw message
drivers/acpi/processor_driver.c +17 -7
@@ -683,8 +683,11 @@ static void acpi_processor_hotplug_notifstruct acpi_device *device =NULL ;struct acpi_eject_event *ej_event =NULL ;u32 ost_code =ACPI_OST_SC_NON_SPECIFIC_FAILURE ;/* default */ +acpi_status status;int result; +acpi_scan_lock_acquire (); +switch (event) {case ACPI_NOTIFY_BUS_CHECK :case ACPI_NOTIFY_DEVICE_CHECK :
@@ -733,25 +736,32 @@ static void acpi_processor_hotplug_notifbreak ; } +get_device (&device->dev); ej_event->device = device; ej_event->event =ACPI_NOTIFY_EJECT_REQUEST ; -acpi_os_hotplug_execute (acpi_bus_hot_remove_device, - (void *)ej_event); - -/* eject is performed asynchronously */ -return ; +/* The eject is carried out asynchronously. */ + status =acpi_os_hotplug_execute (acpi_bus_hot_remove_device, + ej_event); +if (ACPI_FAILURE (status)) { +put_device (&device->dev); +kfree (ej_event); +break ; + } + goto out;default :ACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"Unsupported event [0x%x]\n" , event));/* non-hotplug event; possibly handled by other handler */ -return ; + goto out; }/* Inform firmware that the hotplug operation has completed */ (void )acpi_evaluate_hotplug_ost (handle, event, ost_code,NULL ); -return ; + + out: +acpi_scan_lock_release ();; }static acpi_status is_processor_device (acpi_handle handle)
drivers/acpi/container.c +7 -3
@@ -88,6 +88,8 @@ static void container_notify_cb(acpi_hanacpi_status status;u32 ost_code =ACPI_OST_SC_NON_SPECIFIC_FAILURE ;/* default */ +acpi_scan_lock_acquire (); +switch (type) {case ACPI_NOTIFY_BUS_CHECK :/* Fall through */
@@ -130,18 +132,20 @@ static void container_notify_cb(acpi_hanif (!acpi_bus_get_device (handle, &device) && device) { device->flags.eject_pending =1 ;kobject_uevent (&device->dev.kobj,KOBJ_OFFLINE ); -return ; + goto out; }break ;default :/* non-hotplug event; possibly handled by other handler */ -return ; + goto out; }/* Inform firmware that the hotplug operation has completed */ (void )acpi_evaluate_hotplug_ost (handle, type, ost_code,NULL ); -return ; + + out: +acpi_scan_lock_release (); }static bool is_container (acpi_handle handle)
drivers/acpi/dock.c +16 -3
@@ -744,7 +744,9 @@ static void acpi_dock_deferred_cb(void * {struct dock_data *data = context; +acpi_scan_lock_acquire ();dock_notify (data->handle, data->event, data->ds); +acpi_scan_lock_release ();kfree (data); }
@@ -757,20 +759,31 @@ static int acpi_dock_notifier_call(strucif (event !=ACPI_NOTIFY_BUS_CHECK && event !=ACPI_NOTIFY_DEVICE_CHECK && event !=ACPI_NOTIFY_EJECT_REQUEST )return 0 ; + +acpi_scan_lock_acquire (); +list_for_each_entry (dock_station, &dock_stations, sibling) {if (dock_station->handle == handle) {struct dock_data *dd; +acpi_status status; dd =kmalloc (sizeof (*dd),GFP_KERNEL );if (!dd) -return 0 ; +break ; + dd->handle = handle; dd->event = event; dd->ds = dock_station; -acpi_os_hotplug_execute (acpi_dock_deferred_cb, dd); -return 0 ; + status =acpi_os_hotplug_execute (acpi_dock_deferred_cb, + dd); +if (ACPI_FAILURE (status)) +kfree (dd); + +break ; } } + +acpi_scan_lock_release ();return 0 ; }
drivers/pci/hotplug/acpiphp_glue.c +6 -0
@@ -1218,6 +1218,8 @@ static void _handle_hotplug_event_bridge handle = hp_work->handle; type = hp_work->type; +acpi_scan_lock_acquire (); +if (acpi_bus_get_device (handle, &device)) {/* This bridge must have just been physically inserted */ handle_bridge_insertion (handle, type);
@@ -1295,6 +1297,7 @@ static void _handle_hotplug_event_bridge } out: +acpi_scan_lock_release ();kfree (hp_work);/* allocated in handle_hotplug_event_bridge */ }
@@ -1341,6 +1344,8 @@ static void _handle_hotplug_event_func(s func = (struct acpiphp_func *)context; +acpi_scan_lock_acquire (); +switch (type) {case ACPI_NOTIFY_BUS_CHECK :/* bus re-enumerate */
@@ -1371,6 +1376,7 @@ static void _handle_hotplug_event_func(sbreak ; } +acpi_scan_lock_release ();kfree (hp_work);/* allocated in handle_hotplug_event_func */ }
drivers/pci/hotplug/sgi_hotplug.c +4 -1
@@ -425,6 +425,7 @@ static int enable_slot(struct hotplug_sl pdevice =NULL ; } +acpi_scan_lock_acquire ();/* * Walk the rootbus node's immediate children looking for * the slot's device node(s). There can be more than
@@ -458,6 +459,7 @@ static int enable_slot(struct hotplug_sl } } } +acpi_scan_lock_release (); }/* Call the driver for the new device */
@@ -508,6 +510,7 @@ static int disable_slot(struct hotplug_s/* Get the rootbus node pointer */ phandle =PCI_CONTROLLER (slot->pci_bus)->acpi_handle; +acpi_scan_lock_acquire ();/* * Walk the rootbus node's immediate children looking for * the slot's device node(s). There can be more than
@@ -538,7 +541,7 @@ static int disable_slot(struct hotplug_sacpi_bus_trim (device); } } - +acpi_scan_lock_release (); }/* Free the SN resources assigned to the Linux device.*/
Quoted 40 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
This changeset is aimed at fixing a few different but related problems in the ACPI hotplug infrastructure.
First of all, since notify handlers may be run in parallel with
acpi_bus_scan(),acpi_bus_trim()andacpi_bus_hot_remove_device()and some of them are installed for ACPI handles that have no structacpi_deviceobjects attached (i.e. before those objects are created), those notify handlers have to takeacpi_scan_lockto prevent races from taking place (e.g. a structacpi_deviceis found to be present for the given ACPI handle, but right after that it is removed byacpi_bus_trim()running in parallel to the given notify handler). Moreover, since some of them callacpi_bus_scan()andacpi_bus_trim(), this leads to the conclusion thatacpi_scan_lockshould be acquired by the callers of these two funtions rather by these functions themselves.For these reasons, make all notify handlers that can handle device addition and eject events take
acpi_scan_lockand remove theacpi_scan_locklocking fromacpi_bus_scan()andacpi_bus_trim(). Accordingly, update all of their users to make sure that they are always called underacpi_scan_lock.Furthermore, since eject operations are carried out asynchronously with respect to the notify events that trigger them, with the help of
acpi_bus_hot_remove_device(), even if notify handlers take the ACPI scan lock, it still is possible that, for example,acpi_bus_trim()will run betweenacpi_bus_hot_remove_device()and the notify handler that scheduled its execution and thatacpi_bus_trim()will remove the device node passed toacpi_bus_hot_remove_device()for ejection. In that case, the structacpi_deviceobject obtained byacpi_bus_hot_remove_device()will be invalid and not-so-funny things will ensue. To protect agaist that, make the users ofacpi_bus_hot_remove_device()runget_device()on ACPI device node objects that are about to be passed to it and makeacpi_bus_hot_remove_device()runput_device()on them and check if their ACPI handles are not NULL (makeacpi_device_unregister()clear the device nodes' ACPI handles for that check to work).
Quoted 7 lines
Finally, observe that
acpi_os_hotplug_execute()actually can fail, in which case its caller ought to free memory allocated for the context object to prevent leaks from happening. It also needs to runput_device()on the device node that it ranget_device()on previously in that case. Modify the code accordingly.Signed-off-by Rafael J. Wysocki <[email protected]>
Quoted 47 lines · Yinghai Lu
Quoted 46 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
This changeset is aimed at fixing a few different but related problems in the ACPI hotplug infrastructure.
First of all, since notify handlers may be run in parallel with
acpi_bus_scan(),acpi_bus_trim()andacpi_bus_hot_remove_device()and some of them are installed for ACPI handles that have no structacpi_deviceobjects attached (i.e. before those objects are created), those notify handlers have to takeacpi_scan_lockto prevent races from taking place (e.g. a structacpi_deviceis found to be present for the given ACPI handle, but right after that it is removed byacpi_bus_trim()running in parallel to the given notify handler). Moreover, since some of them callacpi_bus_scan()andacpi_bus_trim(), this leads to the conclusion thatacpi_scan_lockshould be acquired by the callers of these two funtions rather by these functions themselves.For these reasons, make all notify handlers that can handle device addition and eject events take
acpi_scan_lockand remove theacpi_scan_locklocking fromacpi_bus_scan()andacpi_bus_trim(). Accordingly, update all of their users to make sure that they are always called underacpi_scan_lock.Furthermore, since eject operations are carried out asynchronously with respect to the notify events that trigger them, with the help of
acpi_bus_hot_remove_device(), even if notify handlers take the ACPI scan lock, it still is possible that, for example,acpi_bus_trim()will run betweenacpi_bus_hot_remove_device()and the notify handler that scheduled its execution and thatacpi_bus_trim()will remove the device node passed toacpi_bus_hot_remove_device()for ejection. In that case, the structacpi_deviceobject obtained byacpi_bus_hot_remove_device()will be invalid and not-so-funny things will ensue. To protect agaist that, make the users ofacpi_bus_hot_remove_device()runget_device()on ACPI device node objects that are about to be passed to it and makeacpi_bus_hot_remove_device()runput_device()on them and check if their ACPI handles are not NULL (makeacpi_device_unregister()clear the device nodes' ACPI handles for that check to work).Finally, observe that
acpi_os_hotplug_execute()actually can fail, in which case its caller ought to free memory allocated for the context object to prevent leaks from happening. It also needs to runput_device()on the device node that it ranget_device()on previously in that case. Modify the code accordingly.
Quoted 3 lines
Quoted 1 line
Signed-off-by Rafael J. Wysocki <[email protected]>Acked-by Yinghai Lu <[email protected]>
Thanks!
Hi Rafael,
The patch seems good.
There is a comment below.
Quoted 434 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
This changeset is aimed at fixing a few different but related problems in the ACPI hotplug infrastructure.
First of all, since notify handlers may be run in parallel with
acpi_bus_scan(),acpi_bus_trim()andacpi_bus_hot_remove_device()and some of them are installed for ACPI handles that have no structacpi_deviceobjects attached (i.e. before those objects are created), those notify handlers have to takeacpi_scan_lockto prevent races from taking place (e.g. a structacpi_deviceis found to be present for the given ACPI handle, but right after that it is removed byacpi_bus_trim()running in parallel to the given notify handler). Moreover, since some of them callacpi_bus_scan()andacpi_bus_trim(), this leads to the conclusion thatacpi_scan_lockshould be acquired by the callers of these two funtions rather by these functions themselves.For these reasons, make all notify handlers that can handle device addition and eject events take
acpi_scan_lockand remove theacpi_scan_locklocking fromacpi_bus_scan()andacpi_bus_trim(). Accordingly, update all of their users to make sure that they are always called underacpi_scan_lock.Furthermore, since eject operations are carried out asynchronously with respect to the notify events that trigger them, with the help of
acpi_bus_hot_remove_device(), even if notify handlers take the ACPI scan lock, it still is possible that, for example,acpi_bus_trim()will run betweenacpi_bus_hot_remove_device()and the notify handler that scheduled its execution and thatacpi_bus_trim()will remove the device node passed toacpi_bus_hot_remove_device()for ejection. In that case, the structacpi_deviceobject obtained byacpi_bus_hot_remove_device()will be invalid and not-so-funny things will ensue. To protect agaist that, make the users ofacpi_bus_hot_remove_device()runget_device()on ACPI device node objects that are about to be passed to it and makeacpi_bus_hot_remove_device()runput_device()on them and check if their ACPI handles are not NULL (makeacpi_device_unregister()clear the device nodes' ACPI handles for that check to work).Finally, observe that
acpi_os_hotplug_execute()actually can fail, in which case its caller ought to free memory allocated for the context object to prevent leaks from happening. It also needs to runput_device()on the device node that it ranget_device()on previously in that case. Modify the code accordingly.Signed-off-by Rafael J. Wysocki <[email protected]>---
On top of linux-pm.git/linux-next.
Thanks,
Rafael--- drivers/acpi/acpi_memhotplug.c | 56 +++++++++++++++++++----------- drivers/acpi/container.c | 10 +++-- drivers/acpi/dock.c | 19 ++++++++-- drivers/acpi/processor_driver.c | 24 +++++++++--- drivers/acpi/scan.c | 69 +++++++++++++++++++++++++------------ drivers/pci/hotplug/acpiphp_glue.c | 6 +++ drivers/pci/hotplug/sgi_hotplug.c | 5 ++ include/acpi/acpi_bus.h | 3 + 8 files changed, 138 insertions(+), 54 deletions(-)Index:
test/drivers/acpi/scan.c
===================================================================drivers/acpi/scan.c +48
-21@@ -42,6 +42,18 @@ struct acpi_device_bus_id{struct list_head node; }; +void acpi_scan_lock_acquire (void ) +{ +mutex_lock (&acpi_scan_lock); +} +EXPORT_SYMBOL_GPL (acpi_scan_lock_acquire); + +void acpi_scan_lock_release (void ) +{ +mutex_unlock (&acpi_scan_lock); +} +EXPORT_SYMBOL_GPL (acpi_scan_lock_release); +int acpi_scan_add_handler (struct acpi_scan_handler *handler) {if (!handler || !handler->attach)@@ -95,8 +107,6 @@ acpi_device_modalias_show(struct device } static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL); -static void __acpi_bus_trim (struct acpi_device *start); -/** * acpi_bus_hot_remove_device: hot-remove a device and its children * @context: struct acpi_eject_event pointer (freed in this func) @@ -107,7 +117,7 @@ static void __acpi_bus_trim(struct acpi_*/ void acpi_bus_hot_remove_device (void *context) { -struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context; +struct acpi_eject_event *ej_event = context;struct acpi_device *device = ej_event->device;acpi_handle handle = device->handle;acpi_handle temp;@@ -118,11 +128,19 @@ void acpi_bus_hot_remove_device(void *comutex_lock (&acpi_scan_lock); +/* If there is no handle, the device node has been unregistered. */ +if (!device->handle) { +dev_dbg (&device->dev,"ACPI handle missing\n" ); +put_device (&device->dev); + goto out; + } +ACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"Hot-removing device %s...\n" ,dev_name (&device->dev))); -__acpi_bus_trim (device); -/* Device node has been released. */ +acpi_bus_trim (device); +/* Device node has been unregistered. */ +put_device (&device->dev); device =NULL ;if (ACPI_SUCCESS (acpi_get_handle (handle,"_LCK" , &temp))) {@@ -151,6 +169,7 @@ void acpi_bus_hot_remove_device(void *co ost_code, NULL); } + out:mutex_unlock (&acpi_scan_lock);kfree (context);return ;@@ -212,6 +231,7 @@ acpi_eject_store(struct device *d, struc goto err; } +get_device (&acpi_device->dev); ej_event->device = acpi_device;if (acpi_device->flags.eject_pending) {/* event originated from ACPI eject notification */ @@ -224,7 +244,11 @@ acpi_eject_store(struct device *d, struc ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL); } -acpi_os_hotplug_execute (acpi_bus_hot_remove_device, (void *)ej_event); + status =acpi_os_hotplug_execute (acpi_bus_hot_remove_device, ej_event); +if (ACPI_FAILURE (status)) { +put_device (&acpi_device->dev); +kfree (ej_event); + } err:return ret; }@@ -779,6 +803,7 @@ static void acpi_device_unregister(struc* no more references. */ acpi_device_set_power (device,ACPI_STATE_D3_COLD ); + device->handle =NULL ;put_device (&device->dev); }@@ -1626,14 +1651,14 @@ static acpi_status acpi_bus_device_attac* there has been a real error. There just have been no suitable ACPI objects * in the table trunk from which the kernel could create a device and add an * appropriate driver. +* +* Must be called under acpi_scan_lock. */ int acpi_bus_scan (acpi_handle handle) {void *device =NULL ;int error =0 ; -mutex_lock (&acpi_scan_lock); -if (ACPI_SUCCESS (acpi_bus_check_add (handle,0 ,NULL , &device)))acpi_walk_namespace (ACPI_TYPE_ANY , handle,ACPI_UINT32_MAX , acpi_bus_check_add,NULL ,NULL , &device);@@ -1644,7 +1669,6 @@ int acpi_bus_scan(acpi_handle handle)acpi_walk_namespace (ACPI_TYPE_ANY , handle,ACPI_UINT32_MAX , acpi_bus_device_attach,NULL ,NULL ,NULL ); -mutex_unlock (&acpi_scan_lock);return error; }EXPORT_SYMBOL (acpi_bus_scan);@@ -1681,7 +1705,13 @@ static acpi_status acpi_bus_remove(acpi_return AE_OK ; } -static void __acpi_bus_trim (struct acpi_device *start) +/** +* acpi_bus_trim - Remove ACPI device node and all of its descendants +* @start: Root of the ACPI device nodes subtree to remove. +* +* Must be called under acpi_scan_lock. +*/ +void acpi_bus_trim (struct acpi_device *start) {/* * Execute acpi_bus_device_detach() as a post-order callback to detach @@ -1698,13 +1728,6 @@ static void __acpi_bus_trim(struct acpi_ acpi_bus_remove, NULL, NULL);acpi_bus_remove (start->handle,0 ,NULL ,NULL ); } - -void acpi_bus_trim (struct acpi_device *start) -{ -mutex_lock (&acpi_scan_lock); -__acpi_bus_trim (start); -mutex_unlock (&acpi_scan_lock); -}EXPORT_SYMBOL_GPL (acpi_bus_trim);static int acpi_bus_scan_fixed (void )@@ -1762,23 +1785,27 @@ int __init acpi_scan_init(void)acpi_csrt_init ();acpi_container_init (); +mutex_lock (&acpi_scan_lock);/* * Enumerate devices in the ACPI namespace. */ result =acpi_bus_scan (ACPI_ROOT_OBJECT );if (result) -return result; + goto out; result =acpi_bus_get_device (ACPI_ROOT_OBJECT , &acpi_root);if (result) -return result; + goto out; result =acpi_bus_scan_fixed ();if (result) {acpi_device_unregister (acpi_root); -return result; + goto out; }acpi_update_all_gpes (); -return 0 ; + + out: +mutex_unlock (&acpi_scan_lock); +return result; }Index:
test/include/acpi/acpi_bus.h
===================================================================include/acpi/acpi_bus.h +3
-0@@ -395,6 +395,9 @@ int acpi_bus_receive_event(struct acpi_bstatic inline int acpi_bus_generate_proc_event (struct acpi_device *device,u8 type,int data) {return 0 ; }#endif + +void acpi_scan_lock_acquire (void ); +void acpi_scan_lock_release (void );int acpi_scan_add_handler (struct acpi_scan_handler *handler);int acpi_bus_register_driver (struct acpi_driver *driver);void acpi_bus_unregister_driver (struct acpi_driver *driver);Index:
test/drivers/acpi/acpi_memhotplug.c
===================================================================drivers/acpi/acpi_memhotplug.c +37
-19@@ -153,14 +153,16 @@ acpi_memory_get_device_resources(structreturn 0 ; } -static int -acpi_memory_get_device (acpi_handle handle, -struct acpi_memory_device **mem_device) +static int acpi_memory_get_device (acpi_handle handle, +struct acpi_memory_device **mem_device) {struct acpi_device *device =NULL ; -int result; +int result =0 ; + +acpi_scan_lock_acquire (); -if (!acpi_bus_get_device (handle, &device) && device) +acpi_bus_get_device (handle, &device); +if (device) goto end;/* @@ -169,23 +171,28 @@ acpi_memory_get_device(acpi_handle handl*/ result =acpi_bus_scan (handle);if (result) { -acpi_handle_warn (handle,"Cannot add acpi bus\n" ); -return -EINVAL ; +acpi_handle_warn (handle,"ACPI namespace scan failed\n" ); + result = -EINVAL ; + goto out; } result =acpi_bus_get_device (handle, &device);if (result) {acpi_handle_warn (handle,"Missing device object\n" ); -return -EINVAL ; + result = -EINVAL ; + goto out; } - end: + end: *mem_device =acpi_driver_data (device);if (!(*mem_device)) {dev_err (&device->dev,"driver data not found\n" ); -return -ENODEV ; + result = -ENODEV ; + goto out; } -return 0 ; + out: +acpi_scan_lock_release (); +return result; }static int acpi_memory_check_device (struct acpi_memory_device *mem_device)@@ -305,6 +312,7 @@ static void acpi_memory_device_notify(acstruct acpi_device *device;struct acpi_eject_event *ej_event =NULL ;u32 ost_code =ACPI_OST_SC_NON_SPECIFIC_FAILURE ;/* default */ +acpi_status status;switch (event) {case ACPI_NOTIFY_BUS_CHECK :@@ -327,29 +335,40 @@ static void acpi_memory_device_notify(acACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"\nReceived EJECT REQUEST notification for device\n" )); + status =AE_ERROR ; +acpi_scan_lock_acquire (); +if (acpi_bus_get_device (handle, &device)) {acpi_handle_err (handle,"Device doesn't exist\n" ); -break ; + goto unlock; } mem_device =acpi_driver_data (device);if (!mem_device) {acpi_handle_err (handle,"Driver Data is NULL\n" ); -break ; + goto unlock; } ej_event =kmalloc (sizeof (*ej_event),GFP_KERNEL );if (!ej_event) {pr_err (PREFIX "No memory, dropping EJECT\n" ); -break ; + goto unlock; } +get_device (&device->dev); ej_event->device = device; ej_event->event =ACPI_NOTIFY_EJECT_REQUEST ; -acpi_os_hotplug_execute (acpi_bus_hot_remove_device, - (void *)ej_event); +/* The eject is carried out asynchronously. */ + status =acpi_os_hotplug_execute (acpi_bus_hot_remove_device, + ej_event); +if (ACPI_FAILURE (status)) { +put_device (&device->dev); +kfree (ej_event); + } -/* eject is performed asynchronously */ -return ; + unlock: +acpi_scan_lock_release (); +if (ACPI_SUCCESS (status)) +return ;default :ACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"Unsupported event [0x%x]\n" , event));@@ -360,7 +379,6 @@ static void acpi_memory_device_notify(ac/* Inform firmware that the hotplug operation has completed */ (void )acpi_evaluate_hotplug_ost (handle, event, ost_code,NULL ); -return ; }static void acpi_memory_device_free (struct acpi_memory_device *mem_device)Index:
test/drivers/acpi/processor_driver.c
===================================================================drivers/acpi/processor_driver.c +13
-6@@ -683,8 +683,11 @@ static void acpi_processor_hotplug_notifstruct acpi_device *device =NULL ;struct acpi_eject_event *ej_event =NULL ;u32 ost_code =ACPI_OST_SC_NON_SPECIFIC_FAILURE ;/* default */ +acpi_status status;int result; +acpi_scan_lock_acquire (); +switch (event) {case ACPI_NOTIFY_BUS_CHECK :case ACPI_NOTIFY_DEVICE_CHECK :@@ -733,25 +736,32 @@ static void acpi_processor_hotplug_notifbreak ; } +get_device (&device->dev); ej_event->device = device; ej_event->event =ACPI_NOTIFY_EJECT_REQUEST ; -acpi_os_hotplug_execute (acpi_bus_hot_remove_device, - (void *)ej_event); - -/* eject is performed asynchronously */ -return ; +/* The eject is carried out asynchronously. */ + status =acpi_os_hotplug_execute (acpi_bus_hot_remove_device, + ej_event); +if (ACPI_FAILURE (status)) { +put_device (&device->dev); +kfree (ej_event); +break ; + } + goto out;default :ACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"Unsupported event [0x%x]\n" , event));/* non-hotplug event; possibly handled by other handler */ -return ;
drivers/acpi/processor_driver.c static void acpi_processor_hotplug_notif · line 765
Quoted 10 lines
drivers/acpi/processor_driver.c +4
-1+ goto out; }/* Inform firmware that the hotplug operation has completed */ (void )acpi_evaluate_hotplug_ost (handle, event, ost_code,NULL ); -return ; + + out: +acpi_scan_lock_release ();;
extra ";"
Thanks,
Yasuaki Ishimatsu
Quoted 165 lines
drivers/acpi/processor_driver.c +0
-0}static
acpi_statusis_processor_device(acpi_handle handle)
Index:test/drivers/acpi/container.c
===================================================================drivers/acpi/container.c +7
-3@@ -88,6 +88,8 @@ static void container_notify_cb(acpi_hanacpi_status status;u32 ost_code =ACPI_OST_SC_NON_SPECIFIC_FAILURE ;/* default */ +acpi_scan_lock_acquire (); +switch (type) {case ACPI_NOTIFY_BUS_CHECK :/* Fall through */ @@ -130,18 +132,20 @@ static void container_notify_cb(acpi_hanif (!acpi_bus_get_device (handle, &device) && device) { device->flags.eject_pending =1 ;kobject_uevent (&device->dev.kobj,KOBJ_OFFLINE ); -return ; + goto out; }break ;default :/* non-hotplug event; possibly handled by other handler */ -return ; + goto out; }/* Inform firmware that the hotplug operation has completed */ (void )acpi_evaluate_hotplug_ost (handle, type, ost_code,NULL ); -return ; + + out: +acpi_scan_lock_release (); }static bool is_container (acpi_handle handle)Index:
test/drivers/acpi/dock.c
===================================================================drivers/acpi/dock.c +16
-3@@ -744,7 +744,9 @@ static void acpi_dock_deferred_cb(void * {struct dock_data *data = context; +acpi_scan_lock_acquire ();dock_notify (data->handle, data->event, data->ds); +acpi_scan_lock_release ();kfree (data); }@@ -757,20 +759,31 @@ static int acpi_dock_notifier_call(strucif (event !=ACPI_NOTIFY_BUS_CHECK && event !=ACPI_NOTIFY_DEVICE_CHECK && event !=ACPI_NOTIFY_EJECT_REQUEST )return 0 ; + +acpi_scan_lock_acquire (); +list_for_each_entry (dock_station, &dock_stations, sibling) {if (dock_station->handle == handle) {struct dock_data *dd; +acpi_status status; dd =kmalloc (sizeof (*dd),GFP_KERNEL );if (!dd) -return 0 ; +break ; + dd->handle = handle; dd->event = event; dd->ds = dock_station; -acpi_os_hotplug_execute (acpi_dock_deferred_cb, dd); -return 0 ; + status =acpi_os_hotplug_execute (acpi_dock_deferred_cb, + dd); +if (ACPI_FAILURE (status)) +kfree (dd); + +break ; } } + +acpi_scan_lock_release ();return 0 ; }Index:
test/drivers/pci/hotplug/acpiphp_glue.c
===================================================================drivers/pci/hotplug/acpiphp_glue.c +6
-0@@ -1218,6 +1218,8 @@ static void _handle_hotplug_event_bridge handle = hp_work->handle; type = hp_work->type; +acpi_scan_lock_acquire (); +if (acpi_bus_get_device (handle, &device)) {/* This bridge must have just been physically inserted */ handle_bridge_insertion (handle, type);@@ -1295,6 +1297,7 @@ static void _handle_hotplug_event_bridge } out: +acpi_scan_lock_release ();kfree (hp_work);/* allocated in handle_hotplug_event_bridge */ }@@ -1341,6 +1344,8 @@ static void _handle_hotplug_event_func(s func = (struct acpiphp_func *)context; +acpi_scan_lock_acquire (); +switch (type) {case ACPI_NOTIFY_BUS_CHECK :/* bus re-enumerate */ @@ -1371,6 +1376,7 @@ static void _handle_hotplug_event_func(sbreak ; } +acpi_scan_lock_release ();kfree (hp_work);/* allocated in handle_hotplug_event_func */ }Index:
test/drivers/pci/hotplug/sgi_hotplug.c
===================================================================drivers/pci/hotplug/sgi_hotplug.c +4
-1@@ -425,6 +425,7 @@ static int enable_slot(struct hotplug_sl pdevice =NULL ; } +acpi_scan_lock_acquire ();/* * Walk the rootbus node's immediate children looking for * the slot's device node(s). There can be more than @@ -458,6 +459,7 @@ static int enable_slot(struct hotplug_sl } } } +acpi_scan_lock_release (); }/* Call the driver for the new device */ @@ -508,6 +510,7 @@ static int disable_slot(struct hotplug_s/* Get the rootbus node pointer */ phandle =PCI_CONTROLLER (slot->pci_bus)->acpi_handle; +acpi_scan_lock_acquire ();/* * Walk the rootbus node's immediate children looking for * the slot's device node(s). There can be more than @@ -538,7 +541,7 @@ static int disable_slot(struct hotplug_sacpi_bus_trim (device); } } - +acpi_scan_lock_release (); }/* Free the SN resources assigned to the Linux device.*/
Hi Rafael,
I have another comment at container.c.
Quoted 467 lines · Yasuaki Ishimatsu
Hi Rafael,
The patch seems good.
There is a comment below.Quoted 434 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
This changeset is aimed at fixing a few different but related problems in the ACPI hotplug infrastructure.
First of all, since notify handlers may be run in parallel with
acpi_bus_scan(),acpi_bus_trim()andacpi_bus_hot_remove_device()and some of them are installed for ACPI handles that have no structacpi_deviceobjects attached (i.e. before those objects are created), those notify handlers have to takeacpi_scan_lockto prevent races from taking place (e.g. a structacpi_deviceis found to be present for the given ACPI handle, but right after that it is removed byacpi_bus_trim()running in parallel to the given notify handler). Moreover, since some of them callacpi_bus_scan()andacpi_bus_trim(), this leads to the conclusion thatacpi_scan_lockshould be acquired by the callers of these two funtions rather by these functions themselves.For these reasons, make all notify handlers that can handle device addition and eject events take
acpi_scan_lockand remove theacpi_scan_locklocking fromacpi_bus_scan()andacpi_bus_trim(). Accordingly, update all of their users to make sure that they are always called underacpi_scan_lock.Furthermore, since eject operations are carried out asynchronously with respect to the notify events that trigger them, with the help of
acpi_bus_hot_remove_device(), even if notify handlers take the ACPI scan lock, it still is possible that, for example,acpi_bus_trim()will run betweenacpi_bus_hot_remove_device()and the notify handler that scheduled its execution and thatacpi_bus_trim()will remove the device node passed toacpi_bus_hot_remove_device()for ejection. In that case, the structacpi_deviceobject obtained byacpi_bus_hot_remove_device()will be invalid and not-so-funny things will ensue. To protect agaist that, make the users ofacpi_bus_hot_remove_device()runget_device()on ACPI device node objects that are about to be passed to it and makeacpi_bus_hot_remove_device()runput_device()on them and check if their ACPI handles are not NULL (makeacpi_device_unregister()clear the device nodes' ACPI handles for that check to work).Finally, observe that
acpi_os_hotplug_execute()actually can fail, in which case its caller ought to free memory allocated for the context object to prevent leaks from happening. It also needs to runput_device()on the device node that it ranget_device()on previously in that case. Modify the code accordingly.Signed-off-by Rafael J. Wysocki <[email protected]>---
On top of linux-pm.git/linux-next.
Thanks,
Rafael--- drivers/acpi/acpi_memhotplug.c | 56 +++++++++++++++++++----------- drivers/acpi/container.c | 10 +++-- drivers/acpi/dock.c | 19 ++++++++-- drivers/acpi/processor_driver.c | 24 +++++++++--- drivers/acpi/scan.c | 69 +++++++++++++++++++++++++------------ drivers/pci/hotplug/acpiphp_glue.c | 6 +++ drivers/pci/hotplug/sgi_hotplug.c | 5 ++ include/acpi/acpi_bus.h | 3 + 8 files changed, 138 insertions(+), 54 deletions(-)Index:
test/drivers/acpi/scan.c
===================================================================drivers/acpi/scan.c +48
-21@@ -42,6 +42,18 @@ struct acpi_device_bus_id{struct list_head node; }; +void acpi_scan_lock_acquire (void ) +{ +mutex_lock (&acpi_scan_lock); +} +EXPORT_SYMBOL_GPL (acpi_scan_lock_acquire); + +void acpi_scan_lock_release (void ) +{ +mutex_unlock (&acpi_scan_lock); +} +EXPORT_SYMBOL_GPL (acpi_scan_lock_release); +int acpi_scan_add_handler (struct acpi_scan_handler *handler) {if (!handler || !handler->attach)@@ -95,8 +107,6 @@ acpi_device_modalias_show(struct device } static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL); -static void __acpi_bus_trim (struct acpi_device *start); -/** * acpi_bus_hot_remove_device: hot-remove a device and its children * @context: struct acpi_eject_event pointer (freed in this func) @@ -107,7 +117,7 @@ static void __acpi_bus_trim(struct acpi_*/ void acpi_bus_hot_remove_device (void *context) { -struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context; +struct acpi_eject_event *ej_event = context;struct acpi_device *device = ej_event->device;acpi_handle handle = device->handle;acpi_handle temp;@@ -118,11 +128,19 @@ void acpi_bus_hot_remove_device(void *comutex_lock (&acpi_scan_lock); +/* If there is no handle, the device node has been unregistered. */ +if (!device->handle) { +dev_dbg (&device->dev,"ACPI handle missing\n" ); +put_device (&device->dev); + goto out; + } +ACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"Hot-removing device %s...\n" ,dev_name (&device->dev))); -__acpi_bus_trim (device); -/* Device node has been released. */ +acpi_bus_trim (device); +/* Device node has been unregistered. */ +put_device (&device->dev); device =NULL ;if (ACPI_SUCCESS (acpi_get_handle (handle,"_LCK" , &temp))) {@@ -151,6 +169,7 @@ void acpi_bus_hot_remove_device(void *co ost_code, NULL); } + out:mutex_unlock (&acpi_scan_lock);kfree (context);return ;@@ -212,6 +231,7 @@ acpi_eject_store(struct device *d, struc goto err; } +get_device (&acpi_device->dev); ej_event->device = acpi_device;if (acpi_device->flags.eject_pending) {/* event originated from ACPI eject notification */ @@ -224,7 +244,11 @@ acpi_eject_store(struct device *d, struc ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL); } -acpi_os_hotplug_execute (acpi_bus_hot_remove_device, (void *)ej_event); + status =acpi_os_hotplug_execute (acpi_bus_hot_remove_device, ej_event); +if (ACPI_FAILURE (status)) { +put_device (&acpi_device->dev); +kfree (ej_event); + } err:return ret; }@@ -779,6 +803,7 @@ static void acpi_device_unregister(struc* no more references. */ acpi_device_set_power (device,ACPI_STATE_D3_COLD ); + device->handle =NULL ;put_device (&device->dev); }@@ -1626,14 +1651,14 @@ static acpi_status acpi_bus_device_attac* there has been a real error. There just have been no suitable ACPI objects * in the table trunk from which the kernel could create a device and add an * appropriate driver. +* +* Must be called under acpi_scan_lock. */ int acpi_bus_scan (acpi_handle handle) {void *device =NULL ;int error =0 ; -mutex_lock (&acpi_scan_lock); -if (ACPI_SUCCESS (acpi_bus_check_add (handle,0 ,NULL , &device)))acpi_walk_namespace (ACPI_TYPE_ANY , handle,ACPI_UINT32_MAX , acpi_bus_check_add,NULL ,NULL , &device);@@ -1644,7 +1669,6 @@ int acpi_bus_scan(acpi_handle handle)acpi_walk_namespace (ACPI_TYPE_ANY , handle,ACPI_UINT32_MAX , acpi_bus_device_attach,NULL ,NULL ,NULL ); -mutex_unlock (&acpi_scan_lock);return error; }EXPORT_SYMBOL (acpi_bus_scan);@@ -1681,7 +1705,13 @@ static acpi_status acpi_bus_remove(acpi_return AE_OK ; } -static void __acpi_bus_trim (struct acpi_device *start) +/** +* acpi_bus_trim - Remove ACPI device node and all of its descendants +* @start: Root of the ACPI device nodes subtree to remove. +* +* Must be called under acpi_scan_lock. +*/ +void acpi_bus_trim (struct acpi_device *start) {/* * Execute acpi_bus_device_detach() as a post-order callback to detach @@ -1698,13 +1728,6 @@ static void __acpi_bus_trim(struct acpi_ acpi_bus_remove, NULL, NULL);acpi_bus_remove (start->handle,0 ,NULL ,NULL ); } - -void acpi_bus_trim (struct acpi_device *start) -{ -mutex_lock (&acpi_scan_lock); -__acpi_bus_trim (start); -mutex_unlock (&acpi_scan_lock); -}EXPORT_SYMBOL_GPL (acpi_bus_trim);static int acpi_bus_scan_fixed (void )@@ -1762,23 +1785,27 @@ int __init acpi_scan_init(void)acpi_csrt_init ();acpi_container_init (); +mutex_lock (&acpi_scan_lock);/* * Enumerate devices in the ACPI namespace. */ result =acpi_bus_scan (ACPI_ROOT_OBJECT );if (result) -return result; + goto out; result =acpi_bus_get_device (ACPI_ROOT_OBJECT , &acpi_root);if (result) -return result; + goto out; result =acpi_bus_scan_fixed ();if (result) {acpi_device_unregister (acpi_root); -return result; + goto out; }acpi_update_all_gpes (); -return 0 ; + + out: +mutex_unlock (&acpi_scan_lock); +return result; }Index:
test/include/acpi/acpi_bus.h
===================================================================include/acpi/acpi_bus.h +3
-0@@ -395,6 +395,9 @@ int acpi_bus_receive_event(struct acpi_bstatic inline int acpi_bus_generate_proc_event (struct acpi_device *device,u8 type,int data) {return 0 ; }#endif + +void acpi_scan_lock_acquire (void ); +void acpi_scan_lock_release (void );int acpi_scan_add_handler (struct acpi_scan_handler *handler);int acpi_bus_register_driver (struct acpi_driver *driver);void acpi_bus_unregister_driver (struct acpi_driver *driver);Index:
test/drivers/acpi/acpi_memhotplug.c
===================================================================drivers/acpi/acpi_memhotplug.c +37
-19@@ -153,14 +153,16 @@ acpi_memory_get_device_resources(structreturn 0 ; } -static int -acpi_memory_get_device (acpi_handle handle, -struct acpi_memory_device **mem_device) +static int acpi_memory_get_device (acpi_handle handle, +struct acpi_memory_device **mem_device) {struct acpi_device *device =NULL ; -int result; +int result =0 ; + +acpi_scan_lock_acquire (); -if (!acpi_bus_get_device (handle, &device) && device) +acpi_bus_get_device (handle, &device); +if (device) goto end;/* @@ -169,23 +171,28 @@ acpi_memory_get_device(acpi_handle handl*/ result =acpi_bus_scan (handle);if (result) { -acpi_handle_warn (handle,"Cannot add acpi bus\n" ); -return -EINVAL ; +acpi_handle_warn (handle,"ACPI namespace scan failed\n" ); + result = -EINVAL ; + goto out; } result =acpi_bus_get_device (handle, &device);if (result) {acpi_handle_warn (handle,"Missing device object\n" ); -return -EINVAL ; + result = -EINVAL ; + goto out; } - end: + end: *mem_device =acpi_driver_data (device);if (!(*mem_device)) {dev_err (&device->dev,"driver data not found\n" ); -return -ENODEV ; + result = -ENODEV ; + goto out; } -return 0 ; + out: +acpi_scan_lock_release (); +return result; }static int acpi_memory_check_device (struct acpi_memory_device *mem_device)@@ -305,6 +312,7 @@ static void acpi_memory_device_notify(acstruct acpi_device *device;struct acpi_eject_event *ej_event =NULL ;u32 ost_code =ACPI_OST_SC_NON_SPECIFIC_FAILURE ;/* default */ +acpi_status status;switch (event) {case ACPI_NOTIFY_BUS_CHECK :@@ -327,29 +335,40 @@ static void acpi_memory_device_notify(acACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"\nReceived EJECT REQUEST notification for device\n" )); + status =AE_ERROR ; +acpi_scan_lock_acquire (); +if (acpi_bus_get_device (handle, &device)) {acpi_handle_err (handle,"Device doesn't exist\n" ); -break ; + goto unlock; } mem_device =acpi_driver_data (device);if (!mem_device) {acpi_handle_err (handle,"Driver Data is NULL\n" ); -break ; + goto unlock; } ej_event =kmalloc (sizeof (*ej_event),GFP_KERNEL );if (!ej_event) {pr_err (PREFIX "No memory, dropping EJECT\n" ); -break ; + goto unlock; } +get_device (&device->dev); ej_event->device = device; ej_event->event =ACPI_NOTIFY_EJECT_REQUEST ; -acpi_os_hotplug_execute (acpi_bus_hot_remove_device, - (void *)ej_event); +/* The eject is carried out asynchronously. */ + status =acpi_os_hotplug_execute (acpi_bus_hot_remove_device, + ej_event); +if (ACPI_FAILURE (status)) { +put_device (&device->dev); +kfree (ej_event); + } -/* eject is performed asynchronously */ -return ; + unlock: +acpi_scan_lock_release (); +if (ACPI_SUCCESS (status)) +return ;default :ACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"Unsupported event [0x%x]\n" , event));@@ -360,7 +379,6 @@ static void acpi_memory_device_notify(ac/* Inform firmware that the hotplug operation has completed */ (void )acpi_evaluate_hotplug_ost (handle, event, ost_code,NULL ); -return ; }static void acpi_memory_device_free (struct acpi_memory_device *mem_device)Index:
test/drivers/acpi/processor_driver.c
===================================================================drivers/acpi/processor_driver.c +13
-6@@ -683,8 +683,11 @@ static void acpi_processor_hotplug_notifstruct acpi_device *device =NULL ;struct acpi_eject_event *ej_event =NULL ;u32 ost_code =ACPI_OST_SC_NON_SPECIFIC_FAILURE ;/* default */ +acpi_status status;int result; +acpi_scan_lock_acquire (); +switch (event) {case ACPI_NOTIFY_BUS_CHECK :case ACPI_NOTIFY_DEVICE_CHECK :@@ -733,25 +736,32 @@ static void acpi_processor_hotplug_notifbreak ; } +get_device (&device->dev); ej_event->device = device; ej_event->event =ACPI_NOTIFY_EJECT_REQUEST ; -acpi_os_hotplug_execute (acpi_bus_hot_remove_device, - (void *)ej_event); - -/* eject is performed asynchronously */ -return ; +/* The eject is carried out asynchronously. */ + status =acpi_os_hotplug_execute (acpi_bus_hot_remove_device, + ej_event); +if (ACPI_FAILURE (status)) { +put_device (&device->dev); +kfree (ej_event); +break ; + } + goto out;default :ACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"Unsupported event [0x%x]\n" , event));/* non-hotplug event; possibly handled by other handler */ -return ;drivers/acpi/processor_driver.c static void acpi_processor_hotplug_notif · line 765
Quoted 10 lines
drivers/acpi/processor_driver.c +4
-1+ goto out; }/* Inform firmware that the hotplug operation has completed */ (void )acpi_evaluate_hotplug_ost (handle, event, ost_code,NULL ); -return ; + + out: +acpi_scan_lock_release ();;extra ";"
Thanks,
Yasuaki IshimatsuQuoted 11 lines
drivers/acpi/processor_driver.c +0
-0}static
acpi_statusis_processor_device(acpi_handle handle)
Index:test/drivers/acpi/container.c
===================================================================drivers/acpi/container.c +0
-0@@ -88,6 +88,8 @@ static void container_notify_cb(acpi_hanacpi_status status;u32 ost_code =ACPI_OST_SC_NON_SPECIFIC_FAILURE ;/* default */
Quoted 5 lines
drivers/acpi/container.c static void container_notify_cb(acpi_han · line 95
Quoted 5 lines
drivers/acpi/container.c +2
-0+acpi_scan_lock_acquire (); +switch (type) {case ACPI_NOTIFY_BUS_CHECK :/* Fall through */
101 present = is_device_present(handle);
102 status = acpi_bus_get_device(handle, &device);
103 if (!present) {
104 if (ACPI_SUCCESS(status)) {
105 /* device exist and this is a remove request */
106 device->flags.eject_pending = 1;
107 kobject_uevent(&device->dev.kobj, KOBJ_OFLINE);108 return;
It should use "goto out" instead of return.
109 }
110 break;
111 }
Thanks,
Yasuaki Ishimatsu
Quoted 155 lines
Quoted 141 lines
drivers/acpi/container.c +5
-3@@ -130,18 +132,20 @@ static void container_notify_cb(acpi_hanif (!acpi_bus_get_device (handle, &device) && device) { device->flags.eject_pending =1 ;kobject_uevent (&device->dev.kobj,KOBJ_OFFLINE ); -return ; + goto out; }break ;default :/* non-hotplug event; possibly handled by other handler */ -return ; + goto out; }/* Inform firmware that the hotplug operation has completed */ (void )acpi_evaluate_hotplug_ost (handle, type, ost_code,NULL ); -return ; + + out: +acpi_scan_lock_release (); }static bool is_container (acpi_handle handle)Index:
test/drivers/acpi/dock.c
===================================================================drivers/acpi/dock.c +16
-3@@ -744,7 +744,9 @@ static void acpi_dock_deferred_cb(void * {struct dock_data *data = context; +acpi_scan_lock_acquire ();dock_notify (data->handle, data->event, data->ds); +acpi_scan_lock_release ();kfree (data); }@@ -757,20 +759,31 @@ static int acpi_dock_notifier_call(strucif (event !=ACPI_NOTIFY_BUS_CHECK && event !=ACPI_NOTIFY_DEVICE_CHECK && event !=ACPI_NOTIFY_EJECT_REQUEST )return 0 ; + +acpi_scan_lock_acquire (); +list_for_each_entry (dock_station, &dock_stations, sibling) {if (dock_station->handle == handle) {struct dock_data *dd; +acpi_status status; dd =kmalloc (sizeof (*dd),GFP_KERNEL );if (!dd) -return 0 ; +break ; + dd->handle = handle; dd->event = event; dd->ds = dock_station; -acpi_os_hotplug_execute (acpi_dock_deferred_cb, dd); -return 0 ; + status =acpi_os_hotplug_execute (acpi_dock_deferred_cb, + dd); +if (ACPI_FAILURE (status)) +kfree (dd); + +break ; } } + +acpi_scan_lock_release ();return 0 ; }Index:
test/drivers/pci/hotplug/acpiphp_glue.c
===================================================================drivers/pci/hotplug/acpiphp_glue.c +6
-0@@ -1218,6 +1218,8 @@ static void _handle_hotplug_event_bridge handle = hp_work->handle; type = hp_work->type; +acpi_scan_lock_acquire (); +if (acpi_bus_get_device (handle, &device)) {/* This bridge must have just been physically inserted */ handle_bridge_insertion (handle, type);@@ -1295,6 +1297,7 @@ static void _handle_hotplug_event_bridge } out: +acpi_scan_lock_release ();kfree (hp_work);/* allocated in handle_hotplug_event_bridge */ }@@ -1341,6 +1344,8 @@ static void _handle_hotplug_event_func(s func = (struct acpiphp_func *)context; +acpi_scan_lock_acquire (); +switch (type) {case ACPI_NOTIFY_BUS_CHECK :/* bus re-enumerate */ @@ -1371,6 +1376,7 @@ static void _handle_hotplug_event_func(sbreak ; } +acpi_scan_lock_release ();kfree (hp_work);/* allocated in handle_hotplug_event_func */ }Index:
test/drivers/pci/hotplug/sgi_hotplug.c
===================================================================drivers/pci/hotplug/sgi_hotplug.c +3
-0@@ -425,6 +425,7 @@ static int enable_slot(struct hotplug_sl pdevice =NULL ; } +acpi_scan_lock_acquire ();/* * Walk the rootbus node's immediate children looking for * the slot's device node(s). There can be more than @@ -458,6 +459,7 @@ static int enable_slot(struct hotplug_sl } } } +acpi_scan_lock_release (); }/* Call the driver for the new device */ @@ -508,6 +510,7 @@ static int disable_slot(struct hotplug_s/* Get the rootbus node pointer */ phandle =PCI_CONTROLLER (slot->pci_bus)->acpi_handle; +acpi_scan_lock_acquire ();/* * Walk the rootbus node's immediate children looking for * the slot's device node(s). There can be more than drivers/pci/hotplug/sgi_hotplug.c static int disable_slot(struct hotplug_s · line 547
Quoted 8 lines
drivers/pci/hotplug/sgi_hotplug.c +1
-1@@ -538,7 +541,7 @@ static int disable_slot(struct hotplug_sacpi_bus_trim (device); } } - +acpi_scan_lock_release (); }/* Free the SN resources assigned to the Linux device.*/ --
To unsubscribe from this list: send the line "unsubscribe linux-acpi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 486 lines · Yasuaki Ishimatsu
Hi Rafael,
I have another comment at
container.c.Quoted 467 lines · Yasuaki Ishimatsu
Hi Rafael,
The patch seems good.
There is a comment below.Quoted 434 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
This changeset is aimed at fixing a few different but related problems in the ACPI hotplug infrastructure.
First of all, since notify handlers may be run in parallel with
acpi_bus_scan(),acpi_bus_trim()andacpi_bus_hot_remove_device()and some of them are installed for ACPI handles that have no structacpi_deviceobjects attached (i.e. before those objects are created), those notify handlers have to takeacpi_scan_lockto prevent races from taking place (e.g. a structacpi_deviceis found to be present for the given ACPI handle, but right after that it is removed byacpi_bus_trim()running in parallel to the given notify handler). Moreover, since some of them callacpi_bus_scan()andacpi_bus_trim(), this leads to the conclusion thatacpi_scan_lockshould be acquired by the callers of these two funtions rather by these functions themselves.For these reasons, make all notify handlers that can handle device addition and eject events take
acpi_scan_lockand remove theacpi_scan_locklocking fromacpi_bus_scan()andacpi_bus_trim(). Accordingly, update all of their users to make sure that they are always called underacpi_scan_lock.Furthermore, since eject operations are carried out asynchronously with respect to the notify events that trigger them, with the help of
acpi_bus_hot_remove_device(), even if notify handlers take the ACPI scan lock, it still is possible that, for example,acpi_bus_trim()will run betweenacpi_bus_hot_remove_device()and the notify handler that scheduled its execution and thatacpi_bus_trim()will remove the device node passed toacpi_bus_hot_remove_device()for ejection. In that case, the structacpi_deviceobject obtained byacpi_bus_hot_remove_device()will be invalid and not-so-funny things will ensue. To protect agaist that, make the users ofacpi_bus_hot_remove_device()runget_device()on ACPI device node objects that are about to be passed to it and makeacpi_bus_hot_remove_device()runput_device()on them and check if their ACPI handles are not NULL (makeacpi_device_unregister()clear the device nodes' ACPI handles for that check to work).Finally, observe that
acpi_os_hotplug_execute()actually can fail, in which case its caller ought to free memory allocated for the context object to prevent leaks from happening. It also needs to runput_device()on the device node that it ranget_device()on previously in that case. Modify the code accordingly.Signed-off-by Rafael J. Wysocki <[email protected]>---
On top of linux-pm.git/linux-next.
Thanks,
Rafael--- drivers/acpi/acpi_memhotplug.c | 56 +++++++++++++++++++----------- drivers/acpi/container.c | 10 +++-- drivers/acpi/dock.c | 19 ++++++++-- drivers/acpi/processor_driver.c | 24 +++++++++--- drivers/acpi/scan.c | 69 +++++++++++++++++++++++++------------ drivers/pci/hotplug/acpiphp_glue.c | 6 +++ drivers/pci/hotplug/sgi_hotplug.c | 5 ++ include/acpi/acpi_bus.h | 3 + 8 files changed, 138 insertions(+), 54 deletions(-)Index:
test/drivers/acpi/scan.c
===================================================================drivers/acpi/scan.c +48
-21@@ -42,6 +42,18 @@ struct acpi_device_bus_id{struct list_head node; }; +void acpi_scan_lock_acquire (void ) +{ +mutex_lock (&acpi_scan_lock); +} +EXPORT_SYMBOL_GPL (acpi_scan_lock_acquire); + +void acpi_scan_lock_release (void ) +{ +mutex_unlock (&acpi_scan_lock); +} +EXPORT_SYMBOL_GPL (acpi_scan_lock_release); +int acpi_scan_add_handler (struct acpi_scan_handler *handler) {if (!handler || !handler->attach)@@ -95,8 +107,6 @@ acpi_device_modalias_show(struct device } static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL); -static void __acpi_bus_trim (struct acpi_device *start); -/** * acpi_bus_hot_remove_device: hot-remove a device and its children * @context: struct acpi_eject_event pointer (freed in this func) @@ -107,7 +117,7 @@ static void __acpi_bus_trim(struct acpi_*/ void acpi_bus_hot_remove_device (void *context) { -struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context; +struct acpi_eject_event *ej_event = context;struct acpi_device *device = ej_event->device;acpi_handle handle = device->handle;acpi_handle temp;@@ -118,11 +128,19 @@ void acpi_bus_hot_remove_device(void *comutex_lock (&acpi_scan_lock); +/* If there is no handle, the device node has been unregistered. */ +if (!device->handle) { +dev_dbg (&device->dev,"ACPI handle missing\n" ); +put_device (&device->dev); + goto out; + } +ACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"Hot-removing device %s...\n" ,dev_name (&device->dev))); -__acpi_bus_trim (device); -/* Device node has been released. */ +acpi_bus_trim (device); +/* Device node has been unregistered. */ +put_device (&device->dev); device =NULL ;if (ACPI_SUCCESS (acpi_get_handle (handle,"_LCK" , &temp))) {@@ -151,6 +169,7 @@ void acpi_bus_hot_remove_device(void *co ost_code, NULL); } + out:mutex_unlock (&acpi_scan_lock);kfree (context);return ;@@ -212,6 +231,7 @@ acpi_eject_store(struct device *d, struc goto err; } +get_device (&acpi_device->dev); ej_event->device = acpi_device;if (acpi_device->flags.eject_pending) {/* event originated from ACPI eject notification */ @@ -224,7 +244,11 @@ acpi_eject_store(struct device *d, struc ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL); } -acpi_os_hotplug_execute (acpi_bus_hot_remove_device, (void *)ej_event); + status =acpi_os_hotplug_execute (acpi_bus_hot_remove_device, ej_event); +if (ACPI_FAILURE (status)) { +put_device (&acpi_device->dev); +kfree (ej_event); + } err:return ret; }@@ -779,6 +803,7 @@ static void acpi_device_unregister(struc* no more references. */ acpi_device_set_power (device,ACPI_STATE_D3_COLD ); + device->handle =NULL ;put_device (&device->dev); }@@ -1626,14 +1651,14 @@ static acpi_status acpi_bus_device_attac* there has been a real error. There just have been no suitable ACPI objects * in the table trunk from which the kernel could create a device and add an * appropriate driver. +* +* Must be called under acpi_scan_lock. */ int acpi_bus_scan (acpi_handle handle) {void *device =NULL ;int error =0 ; -mutex_lock (&acpi_scan_lock); -if (ACPI_SUCCESS (acpi_bus_check_add (handle,0 ,NULL , &device)))acpi_walk_namespace (ACPI_TYPE_ANY , handle,ACPI_UINT32_MAX , acpi_bus_check_add,NULL ,NULL , &device);@@ -1644,7 +1669,6 @@ int acpi_bus_scan(acpi_handle handle)acpi_walk_namespace (ACPI_TYPE_ANY , handle,ACPI_UINT32_MAX , acpi_bus_device_attach,NULL ,NULL ,NULL ); -mutex_unlock (&acpi_scan_lock);return error; }EXPORT_SYMBOL (acpi_bus_scan);@@ -1681,7 +1705,13 @@ static acpi_status acpi_bus_remove(acpi_return AE_OK ; } -static void __acpi_bus_trim (struct acpi_device *start) +/** +* acpi_bus_trim - Remove ACPI device node and all of its descendants +* @start: Root of the ACPI device nodes subtree to remove. +* +* Must be called under acpi_scan_lock. +*/ +void acpi_bus_trim (struct acpi_device *start) {/* * Execute acpi_bus_device_detach() as a post-order callback to detach @@ -1698,13 +1728,6 @@ static void __acpi_bus_trim(struct acpi_ acpi_bus_remove, NULL, NULL);acpi_bus_remove (start->handle,0 ,NULL ,NULL ); } - -void acpi_bus_trim (struct acpi_device *start) -{ -mutex_lock (&acpi_scan_lock); -__acpi_bus_trim (start); -mutex_unlock (&acpi_scan_lock); -}EXPORT_SYMBOL_GPL (acpi_bus_trim);static int acpi_bus_scan_fixed (void )@@ -1762,23 +1785,27 @@ int __init acpi_scan_init(void)acpi_csrt_init ();acpi_container_init (); +mutex_lock (&acpi_scan_lock);/* * Enumerate devices in the ACPI namespace. */ result =acpi_bus_scan (ACPI_ROOT_OBJECT );if (result) -return result; + goto out; result =acpi_bus_get_device (ACPI_ROOT_OBJECT , &acpi_root);if (result) -return result; + goto out; result =acpi_bus_scan_fixed ();if (result) {acpi_device_unregister (acpi_root); -return result; + goto out; }acpi_update_all_gpes (); -return 0 ; + + out: +mutex_unlock (&acpi_scan_lock); +return result; }Index:
test/include/acpi/acpi_bus.h
===================================================================include/acpi/acpi_bus.h +3
-0@@ -395,6 +395,9 @@ int acpi_bus_receive_event(struct acpi_bstatic inline int acpi_bus_generate_proc_event (struct acpi_device *device,u8 type,int data) {return 0 ; }#endif + +void acpi_scan_lock_acquire (void ); +void acpi_scan_lock_release (void );int acpi_scan_add_handler (struct acpi_scan_handler *handler);int acpi_bus_register_driver (struct acpi_driver *driver);void acpi_bus_unregister_driver (struct acpi_driver *driver);Index:
test/drivers/acpi/acpi_memhotplug.c
===================================================================drivers/acpi/acpi_memhotplug.c +37
-19@@ -153,14 +153,16 @@ acpi_memory_get_device_resources(structreturn 0 ; } -static int -acpi_memory_get_device (acpi_handle handle, -struct acpi_memory_device **mem_device) +static int acpi_memory_get_device (acpi_handle handle, +struct acpi_memory_device **mem_device) {struct acpi_device *device =NULL ; -int result; +int result =0 ; + +acpi_scan_lock_acquire (); -if (!acpi_bus_get_device (handle, &device) && device) +acpi_bus_get_device (handle, &device); +if (device) goto end;/* @@ -169,23 +171,28 @@ acpi_memory_get_device(acpi_handle handl*/ result =acpi_bus_scan (handle);if (result) { -acpi_handle_warn (handle,"Cannot add acpi bus\n" ); -return -EINVAL ; +acpi_handle_warn (handle,"ACPI namespace scan failed\n" ); + result = -EINVAL ; + goto out; } result =acpi_bus_get_device (handle, &device);if (result) {acpi_handle_warn (handle,"Missing device object\n" ); -return -EINVAL ; + result = -EINVAL ; + goto out; } - end: + end: *mem_device =acpi_driver_data (device);if (!(*mem_device)) {dev_err (&device->dev,"driver data not found\n" ); -return -ENODEV ; + result = -ENODEV ; + goto out; } -return 0 ; + out: +acpi_scan_lock_release (); +return result; }static int acpi_memory_check_device (struct acpi_memory_device *mem_device)@@ -305,6 +312,7 @@ static void acpi_memory_device_notify(acstruct acpi_device *device;struct acpi_eject_event *ej_event =NULL ;u32 ost_code =ACPI_OST_SC_NON_SPECIFIC_FAILURE ;/* default */ +acpi_status status;switch (event) {case ACPI_NOTIFY_BUS_CHECK :@@ -327,29 +335,40 @@ static void acpi_memory_device_notify(acACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"\nReceived EJECT REQUEST notification for device\n" )); + status =AE_ERROR ; +acpi_scan_lock_acquire (); +if (acpi_bus_get_device (handle, &device)) {acpi_handle_err (handle,"Device doesn't exist\n" ); -break ; + goto unlock; } mem_device =acpi_driver_data (device);if (!mem_device) {acpi_handle_err (handle,"Driver Data is NULL\n" ); -break ; + goto unlock; } ej_event =kmalloc (sizeof (*ej_event),GFP_KERNEL );if (!ej_event) {pr_err (PREFIX "No memory, dropping EJECT\n" ); -break ; + goto unlock; } +get_device (&device->dev); ej_event->device = device; ej_event->event =ACPI_NOTIFY_EJECT_REQUEST ; -acpi_os_hotplug_execute (acpi_bus_hot_remove_device, - (void *)ej_event); +/* The eject is carried out asynchronously. */ + status =acpi_os_hotplug_execute (acpi_bus_hot_remove_device, + ej_event); +if (ACPI_FAILURE (status)) { +put_device (&device->dev); +kfree (ej_event); + } -/* eject is performed asynchronously */ -return ; + unlock: +acpi_scan_lock_release (); +if (ACPI_SUCCESS (status)) +return ;default :ACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"Unsupported event [0x%x]\n" , event));@@ -360,7 +379,6 @@ static void acpi_memory_device_notify(ac/* Inform firmware that the hotplug operation has completed */ (void )acpi_evaluate_hotplug_ost (handle, event, ost_code,NULL ); -return ; }static void acpi_memory_device_free (struct acpi_memory_device *mem_device)Index:
test/drivers/acpi/processor_driver.c
===================================================================drivers/acpi/processor_driver.c +13
-6@@ -683,8 +683,11 @@ static void acpi_processor_hotplug_notifstruct acpi_device *device =NULL ;struct acpi_eject_event *ej_event =NULL ;u32 ost_code =ACPI_OST_SC_NON_SPECIFIC_FAILURE ;/* default */ +acpi_status status;int result; +acpi_scan_lock_acquire (); +switch (event) {case ACPI_NOTIFY_BUS_CHECK :case ACPI_NOTIFY_DEVICE_CHECK :@@ -733,25 +736,32 @@ static void acpi_processor_hotplug_notifbreak ; } +get_device (&device->dev); ej_event->device = device; ej_event->event =ACPI_NOTIFY_EJECT_REQUEST ; -acpi_os_hotplug_execute (acpi_bus_hot_remove_device, - (void *)ej_event); - -/* eject is performed asynchronously */ -return ; +/* The eject is carried out asynchronously. */ + status =acpi_os_hotplug_execute (acpi_bus_hot_remove_device, + ej_event); +if (ACPI_FAILURE (status)) { +put_device (&device->dev); +kfree (ej_event); +break ; + } + goto out;default :ACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"Unsupported event [0x%x]\n" , event));/* non-hotplug event; possibly handled by other handler */ -return ;drivers/acpi/processor_driver.c static void acpi_processor_hotplug_notif · line 765
Quoted 10 lines
drivers/acpi/processor_driver.c +4
-1+ goto out; }/* Inform firmware that the hotplug operation has completed */ (void )acpi_evaluate_hotplug_ost (handle, event, ost_code,NULL ); -return ; + + out: +acpi_scan_lock_release ();;extra ";"
Thanks,
Yasuaki IshimatsuQuoted 11 lines
drivers/acpi/processor_driver.c +0
-0}static
acpi_statusis_processor_device(acpi_handle handle)
Index:test/drivers/acpi/container.c
===================================================================drivers/acpi/container.c +0
-0@@ -88,6 +88,8 @@ static void container_notify_cb(acpi_hanacpi_status status;u32 ost_code =ACPI_OST_SC_NON_SPECIFIC_FAILURE ;/* default */ Quoted 5 lines
drivers/acpi/container.c static void container_notify_cb(acpi_han · line 95
Quoted 5 lines
drivers/acpi/container.c +2
-0+acpi_scan_lock_acquire (); +switch (type) {case ACPI_NOTIFY_BUS_CHECK :/* Fall through */ 101 present = is_device_present(handle); 102 status = acpi_bus_get_device(handle, &device); 103 if (!present) { 104 if (ACPI_SUCCESS(status)) { 105 /* device exist and this is a remove request */ 106 device->flags.eject_pending = 1; 107 kobject_uevent(&device->dev.kobj, KOBJ_OFLINE);
Quoted 7 lines
108 return;
It should use "goto out" instead of return.
109 }
110 break;
111 }
Indeed, I have overlooked that. Thanks for spotting it!
I'll send an update with this issue fixed (and your comment from the previous message addressed) shortly.
Thanks,
Rafael
Signature · 2 lines
I speak only for myself. Rafael J. Wysocki, Intel Open Source Technology Center.
PATCH 1/1 ACPI / hotplug: Fix concurrency issues and memory leaks
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
This changeset is aimed at fixing a few different but related problems in the ACPI hotplug infrastructure.
First of all, since notify handlers may be run in parallel with acpi_bus_scan(), acpi_bus_trim() and acpi_bus_hot_remove_device() and some of them are installed for ACPI handles that have no struct acpi_device objects attached (i.e. before those objects are created), those notify handlers have to take acpi_scan_lock to prevent races from taking place (e.g. a struct acpi_device is found to be present for the given ACPI handle, but right after that it is removed by acpi_bus_trim() running in parallel to the given notify handler). Moreover, since some of them call acpi_bus_scan() and acpi_bus_trim(), this leads to the conclusion that acpi_scan_lock should be acquired by the callers of these two funtions rather by these functions themselves.
For these reasons, make all notify handlers that can handle device addition and eject events take acpi_scan_lock and remove the acpi_scan_lock locking from acpi_bus_scan() and acpi_bus_trim(). Accordingly, update all of their users to make sure that they are always called under acpi_scan_lock.
Furthermore, since eject operations are carried out asynchronously with respect to the notify events that trigger them, with the help of acpi_bus_hot_remove_device(), even if notify handlers take the ACPI scan lock, it still is possible that, for example, acpi_bus_trim() will run between acpi_bus_hot_remove_device() and the notify handler that scheduled its execution and that acpi_bus_trim() will remove the device node passed to acpi_bus_hot_remove_device() for ejection. In that case, the struct acpi_device object obtained by acpi_bus_hot_remove_device() will be invalid and not-so-funny things will ensue. To protect agaist that, make the users of acpi_bus_hot_remove_device() run get_device() on ACPI device node objects that are about to be passed to it and make acpi_bus_hot_remove_device() run put_device() on them and check if their ACPI handles are not NULL (make acpi_device_unregister() clear the device nodes' ACPI handles for that check to work).
Finally, observe that acpi_os_hotplug_execute() actually can fail, in which case its caller ought to free memory allocated for the context object to prevent leaks from happening. It also needs to run put_device() on the device node that it ran get_device() on previously in that case. Modify the code accordingly.
---
This includes fixes for two issues spotted by Yasuaki Ishimatsu.
Thanks,
Rafael
--- drivers/acpi/acpi_memhotplug.c | 56 +++++++++++++++++++----------- drivers/acpi/container.c | 12 ++++-- drivers/acpi/dock.c | 19 ++++++++-- drivers/acpi/processor_driver.c | 24 +++++++++--- drivers/acpi/scan.c | 69 +++++++++++++++++++++++++------------ drivers/pci/hotplug/acpiphp_glue.c | 6 +++ drivers/pci/hotplug/sgi_hotplug.c | 5 ++ include/acpi/acpi_bus.h | 3 + 8 files changed, 139 insertions(+), 55 deletions(-)
drivers/acpi/scan.c +48 -21
@@ -42,6 +42,18 @@ struct acpi_device_bus_id{struct list_head node; }; +void acpi_scan_lock_acquire (void ) +{ +mutex_lock (&acpi_scan_lock); +} +EXPORT_SYMBOL_GPL (acpi_scan_lock_acquire); + +void acpi_scan_lock_release (void ) +{ +mutex_unlock (&acpi_scan_lock); +} +EXPORT_SYMBOL_GPL (acpi_scan_lock_release); +int acpi_scan_add_handler (struct acpi_scan_handler *handler) {if (!handler || !handler->attach)
@@ -95,8 +107,6 @@ acpi_device_modalias_show(struct device } static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL); -static void __acpi_bus_trim (struct acpi_device *start); -/** * acpi_bus_hot_remove_device: hot-remove a device and its children * @context: struct acpi_eject_event pointer (freed in this func)
@@ -107,7 +117,7 @@ static void __acpi_bus_trim(struct acpi_*/ void acpi_bus_hot_remove_device (void *context) { -struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context; +struct acpi_eject_event *ej_event = context;struct acpi_device *device = ej_event->device;acpi_handle handle = device->handle;acpi_handle temp;
@@ -118,11 +128,19 @@ void acpi_bus_hot_remove_device(void *comutex_lock (&acpi_scan_lock); +/* If there is no handle, the device node has been unregistered. */ +if (!device->handle) { +dev_dbg (&device->dev,"ACPI handle missing\n" ); +put_device (&device->dev); + goto out; + } +ACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"Hot-removing device %s...\n" ,dev_name (&device->dev))); -__acpi_bus_trim (device); -/* Device node has been released. */ +acpi_bus_trim (device); +/* Device node has been unregistered. */ +put_device (&device->dev); device =NULL ;if (ACPI_SUCCESS (acpi_get_handle (handle,"_LCK" , &temp))) {
@@ -151,6 +169,7 @@ void acpi_bus_hot_remove_device(void *co ost_code, NULL); } + out:mutex_unlock (&acpi_scan_lock);kfree (context);return ;
@@ -212,6 +231,7 @@ acpi_eject_store(struct device *d, struc goto err; } +get_device (&acpi_device->dev); ej_event->device = acpi_device;if (acpi_device->flags.eject_pending) {/* event originated from ACPI eject notification */
@@ -224,7 +244,11 @@ acpi_eject_store(struct device *d, struc ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL); } -acpi_os_hotplug_execute (acpi_bus_hot_remove_device, (void *)ej_event); + status =acpi_os_hotplug_execute (acpi_bus_hot_remove_device, ej_event); +if (ACPI_FAILURE (status)) { +put_device (&acpi_device->dev); +kfree (ej_event); + } err:return ret; }
@@ -779,6 +803,7 @@ static void acpi_device_unregister(struc* no more references. */ acpi_device_set_power (device,ACPI_STATE_D3_COLD ); + device->handle =NULL ;put_device (&device->dev); }
@@ -1626,14 +1651,14 @@ static acpi_status acpi_bus_device_attac* there has been a real error. There just have been no suitable ACPI objects * in the table trunk from which the kernel could create a device and add an * appropriate driver. +* +* Must be called under acpi_scan_lock. */ int acpi_bus_scan (acpi_handle handle) {void *device =NULL ;int error =0 ; -mutex_lock (&acpi_scan_lock); -if (ACPI_SUCCESS (acpi_bus_check_add (handle,0 ,NULL , &device)))acpi_walk_namespace (ACPI_TYPE_ANY , handle,ACPI_UINT32_MAX , acpi_bus_check_add,NULL ,NULL , &device);
@@ -1644,7 +1669,6 @@ int acpi_bus_scan(acpi_handle handle)acpi_walk_namespace (ACPI_TYPE_ANY , handle,ACPI_UINT32_MAX , acpi_bus_device_attach,NULL ,NULL ,NULL ); -mutex_unlock (&acpi_scan_lock);return error; }EXPORT_SYMBOL (acpi_bus_scan);
@@ -1681,7 +1705,13 @@ static acpi_status acpi_bus_remove(acpi_return AE_OK ; } -static void __acpi_bus_trim (struct acpi_device *start) +/** +* acpi_bus_trim - Remove ACPI device node and all of its descendants +* @start: Root of the ACPI device nodes subtree to remove. +* +* Must be called under acpi_scan_lock. +*/ +void acpi_bus_trim (struct acpi_device *start) {/* * Execute acpi_bus_device_detach() as a post-order callback to detach
@@ -1698,13 +1728,6 @@ static void __acpi_bus_trim(struct acpi_ acpi_bus_remove, NULL, NULL);acpi_bus_remove (start->handle,0 ,NULL ,NULL ); } - -void acpi_bus_trim (struct acpi_device *start) -{ -mutex_lock (&acpi_scan_lock); -__acpi_bus_trim (start); -mutex_unlock (&acpi_scan_lock); -}EXPORT_SYMBOL_GPL (acpi_bus_trim);static int acpi_bus_scan_fixed (void )
@@ -1761,23 +1784,27 @@ int __init acpi_scan_init(void)acpi_csrt_init ();acpi_container_init (); +mutex_lock (&acpi_scan_lock);/* * Enumerate devices in the ACPI namespace. */ result =acpi_bus_scan (ACPI_ROOT_OBJECT );if (result) -return result; + goto out; result =acpi_bus_get_device (ACPI_ROOT_OBJECT , &acpi_root);if (result) -return result; + goto out; result =acpi_bus_scan_fixed ();if (result) {acpi_device_unregister (acpi_root); -return result; + goto out; }acpi_update_all_gpes (); -return 0 ; + + out: +mutex_unlock (&acpi_scan_lock); +return result; }
include/acpi/acpi_bus.h +3 -0
@@ -395,6 +395,9 @@ int acpi_bus_receive_event(struct acpi_bstatic inline int acpi_bus_generate_proc_event (struct acpi_device *device,u8 type,int data) {return 0 ; }#endif + +void acpi_scan_lock_acquire (void ); +void acpi_scan_lock_release (void );int acpi_scan_add_handler (struct acpi_scan_handler *handler);int acpi_bus_register_driver (struct acpi_driver *driver);void acpi_bus_unregister_driver (struct acpi_driver *driver);
drivers/acpi/acpi_memhotplug.c +37 -19
116 lines not on this page · raw message
drivers/acpi/processor_driver.c +17 -7
@@ -683,8 +683,11 @@ static void acpi_processor_hotplug_notifstruct acpi_device *device =NULL ;struct acpi_eject_event *ej_event =NULL ;u32 ost_code =ACPI_OST_SC_NON_SPECIFIC_FAILURE ;/* default */ +acpi_status status;int result; +acpi_scan_lock_acquire (); +switch (event) {case ACPI_NOTIFY_BUS_CHECK :case ACPI_NOTIFY_DEVICE_CHECK :
@@ -733,25 +736,32 @@ static void acpi_processor_hotplug_notifbreak ; } +get_device (&device->dev); ej_event->device = device; ej_event->event =ACPI_NOTIFY_EJECT_REQUEST ; -acpi_os_hotplug_execute (acpi_bus_hot_remove_device, - (void *)ej_event); - -/* eject is performed asynchronously */ -return ; +/* The eject is carried out asynchronously. */ + status =acpi_os_hotplug_execute (acpi_bus_hot_remove_device, + ej_event); +if (ACPI_FAILURE (status)) { +put_device (&device->dev); +kfree (ej_event); +break ; + } + goto out;default :ACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"Unsupported event [0x%x]\n" , event));/* non-hotplug event; possibly handled by other handler */ -return ; + goto out; }/* Inform firmware that the hotplug operation has completed */ (void )acpi_evaluate_hotplug_ost (handle, event, ost_code,NULL ); -return ; + + out: +acpi_scan_lock_release (); }static acpi_status is_processor_device (acpi_handle handle)
drivers/acpi/container.c +8 -4
@@ -88,6 +88,8 @@ static void container_notify_cb(acpi_hanacpi_status status;u32 ost_code =ACPI_OST_SC_NON_SPECIFIC_FAILURE ;/* default */ +acpi_scan_lock_acquire (); +switch (type) {case ACPI_NOTIFY_BUS_CHECK :/* Fall through */
@@ -103,7 +105,7 @@ static void container_notify_cb(acpi_han/* device exist and this is a remove request */ device->flags.eject_pending =1 ;kobject_uevent (&device->dev.kobj,KOBJ_OFFLINE ); -return ; + goto out; }break ; }
@@ -130,18 +132,20 @@ static void container_notify_cb(acpi_hanif (!acpi_bus_get_device (handle, &device) && device) { device->flags.eject_pending =1 ;kobject_uevent (&device->dev.kobj,KOBJ_OFFLINE ); -return ; + goto out; }break ;default :/* non-hotplug event; possibly handled by other handler */ -return ; + goto out; }/* Inform firmware that the hotplug operation has completed */ (void )acpi_evaluate_hotplug_ost (handle, type, ost_code,NULL ); -return ; + + out: +acpi_scan_lock_release (); }static bool is_container (acpi_handle handle)
drivers/acpi/dock.c +16 -3
@@ -744,7 +744,9 @@ static void acpi_dock_deferred_cb(void * {struct dock_data *data = context; +acpi_scan_lock_acquire ();dock_notify (data->handle, data->event, data->ds); +acpi_scan_lock_release ();kfree (data); }
@@ -757,20 +759,31 @@ static int acpi_dock_notifier_call(strucif (event !=ACPI_NOTIFY_BUS_CHECK && event !=ACPI_NOTIFY_DEVICE_CHECK && event !=ACPI_NOTIFY_EJECT_REQUEST )return 0 ; + +acpi_scan_lock_acquire (); +list_for_each_entry (dock_station, &dock_stations, sibling) {if (dock_station->handle == handle) {struct dock_data *dd; +acpi_status status; dd =kmalloc (sizeof (*dd),GFP_KERNEL );if (!dd) -return 0 ; +break ; + dd->handle = handle; dd->event = event; dd->ds = dock_station; -acpi_os_hotplug_execute (acpi_dock_deferred_cb, dd); -return 0 ; + status =acpi_os_hotplug_execute (acpi_dock_deferred_cb, + dd); +if (ACPI_FAILURE (status)) +kfree (dd); + +break ; } } + +acpi_scan_lock_release ();return 0 ; }
drivers/pci/hotplug/acpiphp_glue.c +6 -0
@@ -1218,6 +1218,8 @@ static void _handle_hotplug_event_bridge handle = hp_work->handle; type = hp_work->type; +acpi_scan_lock_acquire (); +if (acpi_bus_get_device (handle, &device)) {/* This bridge must have just been physically inserted */ handle_bridge_insertion (handle, type);
@@ -1295,6 +1297,7 @@ static void _handle_hotplug_event_bridge } out: +acpi_scan_lock_release ();kfree (hp_work);/* allocated in handle_hotplug_event_bridge */ }
@@ -1341,6 +1344,8 @@ static void _handle_hotplug_event_func(s func = (struct acpiphp_func *)context; +acpi_scan_lock_acquire (); +switch (type) {case ACPI_NOTIFY_BUS_CHECK :/* bus re-enumerate */
@@ -1371,6 +1376,7 @@ static void _handle_hotplug_event_func(sbreak ; } +acpi_scan_lock_release ();kfree (hp_work);/* allocated in handle_hotplug_event_func */ }
drivers/pci/hotplug/sgi_hotplug.c +4 -1
@@ -425,6 +425,7 @@ static int enable_slot(struct hotplug_sl pdevice =NULL ; } +acpi_scan_lock_acquire ();/* * Walk the rootbus node's immediate children looking for * the slot's device node(s). There can be more than
@@ -458,6 +459,7 @@ static int enable_slot(struct hotplug_sl } } } +acpi_scan_lock_release (); }/* Call the driver for the new device */
@@ -508,6 +510,7 @@ static int disable_slot(struct hotplug_s/* Get the rootbus node pointer */ phandle =PCI_CONTROLLER (slot->pci_bus)->acpi_handle; +acpi_scan_lock_acquire ();/* * Walk the rootbus node's immediate children looking for * the slot's device node(s). There can be more than
@@ -538,7 +541,7 @@ static int disable_slot(struct hotplug_sacpi_bus_trim (device); } } - +acpi_scan_lock_release (); }/* Free the SN resources assigned to the Linux device.*/
Quoted 40 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
This changeset is aimed at fixing a few different but related problems in the ACPI hotplug infrastructure.
First of all, since notify handlers may be run in parallel with
acpi_bus_scan(),acpi_bus_trim()andacpi_bus_hot_remove_device()and some of them are installed for ACPI handles that have no structacpi_deviceobjects attached (i.e. before those objects are created), those notify handlers have to takeacpi_scan_lockto prevent races from taking place (e.g. a structacpi_deviceis found to be present for the given ACPI handle, but right after that it is removed byacpi_bus_trim()running in parallel to the given notify handler). Moreover, since some of them callacpi_bus_scan()andacpi_bus_trim(), this leads to the conclusion thatacpi_scan_lockshould be acquired by the callers of these two funtions rather by these functions themselves.For these reasons, make all notify handlers that can handle device addition and eject events take
acpi_scan_lockand remove theacpi_scan_locklocking fromacpi_bus_scan()andacpi_bus_trim(). Accordingly, update all of their users to make sure that they are always called underacpi_scan_lock.Furthermore, since eject operations are carried out asynchronously with respect to the notify events that trigger them, with the help of
acpi_bus_hot_remove_device(), even if notify handlers take the ACPI scan lock, it still is possible that, for example,acpi_bus_trim()will run betweenacpi_bus_hot_remove_device()and the notify handler that scheduled its execution and thatacpi_bus_trim()will remove the device node passed toacpi_bus_hot_remove_device()for ejection. In that case, the structacpi_deviceobject obtained byacpi_bus_hot_remove_device()will be invalid and not-so-funny things will ensue. To protect agaist that, make the users ofacpi_bus_hot_remove_device()runget_device()on ACPI device node objects that are about to be passed to it and makeacpi_bus_hot_remove_device()runput_device()on them and check if their ACPI handles are not NULL (makeacpi_device_unregister()clear the device nodes' ACPI handles for that check to work).
Quoted 5 lines
Finally, observe that
acpi_os_hotplug_execute()actually can fail, in which case its caller ought to free memory allocated for the context object to prevent leaks from happening. It also needs to runput_device()on the device node that it ranget_device()on previously in that case. Modify the code accordingly.
I am concerned with this approach. ACPICA calls notify handlers through kacpi_notify_wq, which has the max active set to 1. We then use kacpi_hotplug_wq (which also has the max active set to 1) so that a hotplug procedure does not block the notify handlers since they can be used for non-hotplug events as well. Acquiring the scan lock in a notify handler means that a hotplug procedure can block any notify events.
So, I'd prefer the following approach.
- Change all hot-plug procedures (i.e. both add and delete) to proceed
under kacpi_hotplug_wq by calling acpi_os_hotplug_execute(). This serializes all hotplug procedures, and prevents blocking other notify events. (Ideally, we should also run all online/offline procedures under a same work-queue, just like my RFC patchset did, but this is a different topic for now.)
- Revert 5993c4670 unless this change is absolutely necessary. From
the change log, it is not clear to me why this change was needed. It changed acpi_bus_hot_remove_device() to take an acpi_device, instead of an acpi_handle, which introduced a race condition with acpi_device. acpi_bus_hot_remove_device() should take an acpi_handle, and then obtain its acpi_device from the acpi_handle since this function is serialized.
- Remove sanity checks with an acpi_device in the notify handlers,
which have a race condition with acpi_device. These type-specific checks will need to be removed when we have a common notify handler anyway. The notify handler can continue to check the status of ACPI device object with an acpi_handle. Type-specific sanity checks / validations can be performed within a hotplug procedure, instead.
Thanks,
-Toshi
Quoted 47 lines · Toshi Kani
Quoted 40 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
This changeset is aimed at fixing a few different but related problems in the ACPI hotplug infrastructure.
First of all, since notify handlers may be run in parallel with
acpi_bus_scan(),acpi_bus_trim()andacpi_bus_hot_remove_device()and some of them are installed for ACPI handles that have no structacpi_deviceobjects attached (i.e. before those objects are created), those notify handlers have to takeacpi_scan_lockto prevent races from taking place (e.g. a structacpi_deviceis found to be present for the given ACPI handle, but right after that it is removed byacpi_bus_trim()running in parallel to the given notify handler). Moreover, since some of them callacpi_bus_scan()andacpi_bus_trim(), this leads to the conclusion thatacpi_scan_lockshould be acquired by the callers of these two funtions rather by these functions themselves.For these reasons, make all notify handlers that can handle device addition and eject events take
acpi_scan_lockand remove theacpi_scan_locklocking fromacpi_bus_scan()andacpi_bus_trim(). Accordingly, update all of their users to make sure that they are always called underacpi_scan_lock.Furthermore, since eject operations are carried out asynchronously with respect to the notify events that trigger them, with the help of
acpi_bus_hot_remove_device(), even if notify handlers take the ACPI scan lock, it still is possible that, for example,acpi_bus_trim()will run betweenacpi_bus_hot_remove_device()and the notify handler that scheduled its execution and thatacpi_bus_trim()will remove the device node passed toacpi_bus_hot_remove_device()for ejection. In that case, the structacpi_deviceobject obtained byacpi_bus_hot_remove_device()will be invalid and not-so-funny things will ensue. To protect agaist that, make the users ofacpi_bus_hot_remove_device()runget_device()on ACPI device node objects that are about to be passed to it and makeacpi_bus_hot_remove_device()runput_device()on them and check if their ACPI handles are not NULL (makeacpi_device_unregister()clear the device nodes' ACPI handles for that check to work).Quoted 5 lines
Finally, observe that
acpi_os_hotplug_execute()actually can fail, in which case its caller ought to free memory allocated for the context object to prevent leaks from happening. It also needs to runput_device()on the device node that it ranget_device()on previously in that case. Modify the code accordingly.
Quoted 5 lines
I am concerned with this approach. ACPICA calls notify handlers through
kacpi_notify_wq, which has the max active set to 1. We then usekacpi_hotplug_wq(which also has the max active set to 1) so that a hotplug procedure does not block the notify handlers since they can be used for non-hotplug events as well.
In fact we use kacpi_hotplug_wq for a different reason. Please read the comment in __acpi_os_execute() for more details.
Quoted 2 lines
Acquiring the scan lock in a notify handler means that a hotplug procedure can block any notify events.
Yes, it can.
Quoted 6 lines
So, I'd prefer the following approach.
- Change all hot-plug procedures (i.e. both add and delete) to proceed
underkacpi_hotplug_wqby callingacpi_os_hotplug_execute(). This serializes all hotplug procedures, and prevents blocking other notify events.
Yes, we can do that. I was thinking about doing that change, but not in v3.9. There are simply too many notify handlers already there to do that so late in the cycle. And doing that for acpiphp, for example, won't be straightforward at all.
Please think about the $subject patch as a temporary measure until we can do something better (which we need to do anyway to reduce code duplication among other things).
Quoted 3 lines
(Ideally, we should also run all online/offline procedures under a same work-queue, just like my RFC patchset did, but this is a different topic for now.)
No, I don't think it is appropriate to run online/offline from _any_ workqueue. In my opinion they should be run from user space.
Quoted 6 lines
- Revert
5993c4670unless this change is absolutely necessary. From
the change log, it is not clear to me why this change was needed. It changedacpi_bus_hot_remove_device()to take anacpi_device, instead of anacpi_handle, which introduced a race condition withacpi_device.acpi_bus_hot_remove_device()should take anacpi_handle, and then obtain itsacpi_devicefrom theacpi_handlesince this function is serialized.
I thought about that, but actually there's no guarantee that the handle will be valid after _EJ0 as far as I can say. So the race condition is going to be there anyway and using struct acpi_device just makes it easier to avoid it.
Quoted 6 lines
- Remove sanity checks with an
acpi_devicein the notify handlers,
which have a race condition withacpi_device. These type-specific checks will need to be removed when we have a common notify handler anyway. The notify handler can continue to check the status of ACPI device object with anacpi_handle. Type-specific sanity checks / validations can be performed within a hotplug procedure, instead.
Well, the sanest approach here would be to queue up a work item on kacpi_hotplug_wq if the event is of a "hotplug" type and let that work item do all checks, run acpi_bus_scan() etc. But not in v3.9.
For v3.9, the most straightforward and least intrusive change we can do is the $subject patch as far as I can say. If you can suggest something less intrusive and more straightforward, please do.
Thanks,
Rafael
Signature · 2 lines
I speak only for myself. Rafael J. Wysocki, Intel Open Source Technology Center.
Quoted 48 lines · Rafael J. Wysocki
Quoted 47 lines · Toshi Kani
Quoted 45 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
This changeset is aimed at fixing a few different but related problems in the ACPI hotplug infrastructure.
First of all, since notify handlers may be run in parallel with
acpi_bus_scan(),acpi_bus_trim()andacpi_bus_hot_remove_device()and some of them are installed for ACPI handles that have no structacpi_deviceobjects attached (i.e. before those objects are created), those notify handlers have to takeacpi_scan_lockto prevent races from taking place (e.g. a structacpi_deviceis found to be present for the given ACPI handle, but right after that it is removed byacpi_bus_trim()running in parallel to the given notify handler). Moreover, since some of them callacpi_bus_scan()andacpi_bus_trim(), this leads to the conclusion thatacpi_scan_lockshould be acquired by the callers of these two funtions rather by these functions themselves.For these reasons, make all notify handlers that can handle device addition and eject events take
acpi_scan_lockand remove theacpi_scan_locklocking fromacpi_bus_scan()andacpi_bus_trim(). Accordingly, update all of their users to make sure that they are always called underacpi_scan_lock.Furthermore, since eject operations are carried out asynchronously with respect to the notify events that trigger them, with the help of
acpi_bus_hot_remove_device(), even if notify handlers take the ACPI scan lock, it still is possible that, for example,acpi_bus_trim()will run betweenacpi_bus_hot_remove_device()and the notify handler that scheduled its execution and thatacpi_bus_trim()will remove the device node passed toacpi_bus_hot_remove_device()for ejection. In that case, the structacpi_deviceobject obtained byacpi_bus_hot_remove_device()will be invalid and not-so-funny things will ensue. To protect agaist that, make the users ofacpi_bus_hot_remove_device()runget_device()on ACPI device node objects that are about to be passed to it and makeacpi_bus_hot_remove_device()runput_device()on them and check if their ACPI handles are not NULL (makeacpi_device_unregister()clear the device nodes' ACPI handles for that check to work).Finally, observe that
acpi_os_hotplug_execute()actually can fail, in which case its caller ought to free memory allocated for the context object to prevent leaks from happening. It also needs to runput_device()on the device node that it ranget_device()on previously in that case. Modify the code accordingly.
Quoted 8 lines
Quoted 5 lines
I am concerned with this approach. ACPICA calls notify handlers through
kacpi_notify_wq, which has the max active set to 1. We then usekacpi_hotplug_wq(which also has the max active set to 1) so that a hotplug procedure does not block the notify handlers since they can be used for non-hotplug events as well.In fact we use
kacpi_hotplug_wqfor a different reason. Please read the comment in__acpi_os_execute()for more details.
Yes, I am aware of the issue as well.
Quoted 12 lines
Quoted 2 lines
Acquiring the scan lock in a notify handler means that a hotplug procedure can block any notify events.
Yes, it can.
Quoted 6 lines
So, I'd prefer the following approach.
- Change all hot-plug procedures (i.e. both add and delete) to proceed
underkacpi_hotplug_wqby callingacpi_os_hotplug_execute(). This serializes all hotplug procedures, and prevents blocking other notify events.
Quoted 4 lines
Yes, we can do that. I was thinking about doing that change, but not in v3.9. There are simply too many notify handlers already there to do that so late in the cycle. And doing that for acpiphp, for example, won't be straightforward at all.
Right. I was not suggesting this approach for v3.9.
Quoted 3 lines
Please think about the $subject patch as a temporary measure until we can do something better (which we need to do anyway to reduce code duplication among other things).
I am fine with the scan lock as long as it is internal. This patch publishes the locking interfaces to other modules, which made me worried that this might become a long term solution. If we need to fix this issue for v3.9, I am OK with it as you clarified this as a temporary solution.
Quoted 6 lines
Quoted 3 lines
(Ideally, we should also run all online/offline procedures under a same work-queue, just like my RFC patchset did, but this is a different topic for now.)
No, I don't think it is appropriate to run online/offline from _any_ workqueue. In my opinion they should be run from user space.
I think there are pros and cons for this. If we use a user thread to run online/offline procedure, we can return a result directly. However, if an operation takes a long time, it will block the user thread until it is done. In addition, we have race conditions between hotplug and online/offline operations. So, we may need to come up with other type of locking if we do not use a workqueue to address it. Having both the scan lock and other lock in the callers would not be good.
Quoted 11 lines
Quoted 6 lines
- Revert
5993c4670unless this change is absolutely necessary. From
the change log, it is not clear to me why this change was needed. It changedacpi_bus_hot_remove_device()to take anacpi_device, instead of anacpi_handle, which introduced a race condition withacpi_device.acpi_bus_hot_remove_device()should take anacpi_handle, and then obtain itsacpi_devicefrom theacpi_handlesince this function is serialized.I thought about that, but actually there's no guarantee that the handle will be valid after
_EJ0as far as I can say. So the race condition is going to be there anyway and using structacpi_devicejust makes it easier to avoid it.
In theory, yes, a stale handle could be a problem, if _EJ0 performs unload table and if ACPICA frees up its internal data structure pointed by the handle as a result. But we should not see such issue now since we do not support dynamic ACPI namespace yet.
Quoted 10 lines
Quoted 6 lines
- Remove sanity checks with an
acpi_devicein the notify handlers,
which have a race condition withacpi_device. These type-specific checks will need to be removed when we have a common notify handler anyway. The notify handler can continue to check the status of ACPI device object with anacpi_handle. Type-specific sanity checks / validations can be performed within a hotplug procedure, instead.Well, the sanest approach here would be to queue up a work item on
kacpi_hotplug_wqif the event is of a "hotplug" type and let that work item do all checks, runacpi_bus_scan()etc. But not in v3.9.
Agreed, and that's what I meant.
Quoted 3 lines
For v3.9, the most straightforward and least intrusive change we can do is the $subject patch as far as I can say. If you can suggest something less intrusive and more straightforward, please do.
My suggestion is to keep the scan lock internal for v3.9 and implement a new hotplug framework (i.e. the one with user space approach) for v3.10 with a proper locking mechanism. But, since you clarified this as a temporary solution, I am OK with it if we need to fix it now.
Thanks,
-Toshi
Quoted 72 lines · Toshi Kani
Quoted 48 lines · Rafael J. Wysocki
Quoted 47 lines · Toshi Kani
Quoted 45 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
This changeset is aimed at fixing a few different but related problems in the ACPI hotplug infrastructure.
First of all, since notify handlers may be run in parallel with
acpi_bus_scan(),acpi_bus_trim()andacpi_bus_hot_remove_device()and some of them are installed for ACPI handles that have no structacpi_deviceobjects attached (i.e. before those objects are created), those notify handlers have to takeacpi_scan_lockto prevent races from taking place (e.g. a structacpi_deviceis found to be present for the given ACPI handle, but right after that it is removed byacpi_bus_trim()running in parallel to the given notify handler). Moreover, since some of them callacpi_bus_scan()andacpi_bus_trim(), this leads to the conclusion thatacpi_scan_lockshould be acquired by the callers of these two funtions rather by these functions themselves.For these reasons, make all notify handlers that can handle device addition and eject events take
acpi_scan_lockand remove theacpi_scan_locklocking fromacpi_bus_scan()andacpi_bus_trim(). Accordingly, update all of their users to make sure that they are always called underacpi_scan_lock.Furthermore, since eject operations are carried out asynchronously with respect to the notify events that trigger them, with the help of
acpi_bus_hot_remove_device(), even if notify handlers take the ACPI scan lock, it still is possible that, for example,acpi_bus_trim()will run betweenacpi_bus_hot_remove_device()and the notify handler that scheduled its execution and thatacpi_bus_trim()will remove the device node passed toacpi_bus_hot_remove_device()for ejection. In that case, the structacpi_deviceobject obtained byacpi_bus_hot_remove_device()will be invalid and not-so-funny things will ensue. To protect agaist that, make the users ofacpi_bus_hot_remove_device()runget_device()on ACPI device node objects that are about to be passed to it and makeacpi_bus_hot_remove_device()runput_device()on them and check if their ACPI handles are not NULL (makeacpi_device_unregister()clear the device nodes' ACPI handles for that check to work).Finally, observe that
acpi_os_hotplug_execute()actually can fail, in which case its caller ought to free memory allocated for the context object to prevent leaks from happening. It also needs to runput_device()on the device node that it ranget_device()on previously in that case. Modify the code accordingly.Quoted 8 lines
Quoted 5 lines
I am concerned with this approach. ACPICA calls notify handlers through
kacpi_notify_wq, which has the max active set to 1. We then usekacpi_hotplug_wq(which also has the max active set to 1) so that a hotplug procedure does not block the notify handlers since they can be used for non-hotplug events as well.In fact we use
kacpi_hotplug_wqfor a different reason. Please read the comment in__acpi_os_execute()for more details.Yes, I am aware of the issue as well.
Quoted 12 lines
Quoted 2 lines
Acquiring the scan lock in a notify handler means that a hotplug procedure can block any notify events.
Yes, it can.
Quoted 6 lines
So, I'd prefer the following approach.
- Change all hot-plug procedures (i.e. both add and delete) to proceed
underkacpi_hotplug_wqby callingacpi_os_hotplug_execute(). This serializes all hotplug procedures, and prevents blocking other notify events.
Quoted 6 lines
Quoted 4 lines
Yes, we can do that. I was thinking about doing that change, but not in v3.9. There are simply too many notify handlers already there to do that so late in the cycle. And doing that for acpiphp, for example, won't be straightforward at all.
Right. I was not suggesting this approach for v3.9.
OK
Quoted 9 lines
Quoted 3 lines
Please think about the $subject patch as a temporary measure until we can do something better (which we need to do anyway to reduce code duplication among other things).
I am fine with the scan lock as long as it is internal. This patch publishes the locking interfaces to other modules, which made me worried that this might become a long term solution. If we need to fix this issue for v3.9, I am OK with it as you clarified this as a temporary solution.
Yes, I'm not going to allow anyone to use acpi_scan_lock anywhere else. :-) I'm also going to make it internal again in v3.10 if possible.
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
(Ideally, we should also run all online/offline procedures under a same work-queue, just like my RFC patchset did, but this is a different topic for now.)
No, I don't think it is appropriate to run online/offline from _any_ workqueue. In my opinion they should be run from user space.
Quoted 7 lines
I think there are pros and cons for this. If we use a user thread to run online/offline procedure, we can return a result directly. However, if an operation takes a long time, it will block the user thread until it is done. In addition, we have race conditions between hotplug and online/offline operations. So, we may need to come up with other type of locking if we do not use a workqueue to address it. Having both the scan lock and other lock in the callers would not be good.
Well, we definitely need to make offline/online and hotplug mutually exclusive, this way or another. I'm hoping that the scan lock will be sufficient for that, but I may be wrong.
Quoted 12 lines
Quoted 11 lines
Quoted 6 lines
- Revert
5993c4670unless this change is absolutely necessary. From
the change log, it is not clear to me why this change was needed. It changedacpi_bus_hot_remove_device()to take anacpi_device, instead of anacpi_handle, which introduced a race condition withacpi_device.acpi_bus_hot_remove_device()should take anacpi_handle, and then obtain itsacpi_devicefrom theacpi_handlesince this function is serialized.I thought about that, but actually there's no guarantee that the handle will be valid after
_EJ0as far as I can say. So the race condition is going to be there anyway and using structacpi_devicejust makes it easier to avoid it.
Quoted 4 lines
In theory, yes, a stale handle could be a problem, if
_EJ0performs unload table and if ACPICA frees up its internal data structure pointed by the handle as a result. But we should not see such issue now since we do not support dynamic ACPI namespace yet.
I'm waiting for information from Bob about that. If we can assume ACPI handles to be always valid, that will simplify things quite a bit.
Quoted 13 lines
Quoted 10 lines
Quoted 6 lines
- Remove sanity checks with an
acpi_devicein the notify handlers,
which have a race condition withacpi_device. These type-specific checks will need to be removed when we have a common notify handler anyway. The notify handler can continue to check the status of ACPI device object with anacpi_handle. Type-specific sanity checks / validations can be performed within a hotplug procedure, instead.Well, the sanest approach here would be to queue up a work item on
kacpi_hotplug_wqif the event is of a "hotplug" type and let that work item do all checks, runacpi_bus_scan()etc. But not in v3.9.Agreed, and that's what I meant.
Quoted 8 lines
Quoted 3 lines
For v3.9, the most straightforward and least intrusive change we can do is the $subject patch as far as I can say. If you can suggest something less intrusive and more straightforward, please do.
My suggestion is to keep the scan lock internal for v3.9 and implement a new hotplug framework (i.e. the one with user space approach) for v3.10 with a proper locking mechanism. But, since you clarified this as a temporary solution, I am OK with it if we need to fix it now.
Well, honestly, I wouldn't have posted this patch if I hadn't thought that we needed a fix for v3.9. :-)
Thanks,
Rafael
Signature · 2 lines
I speak only for myself. Rafael J. Wysocki, Intel Open Source Technology Center.
Quoted 86 lines · Rafael J. Wysocki
Quoted 72 lines · Toshi Kani
Quoted 48 lines · Rafael J. Wysocki
Quoted 47 lines · Toshi Kani
Quoted 45 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
This changeset is aimed at fixing a few different but related problems in the ACPI hotplug infrastructure.
First of all, since notify handlers may be run in parallel with
acpi_bus_scan(),acpi_bus_trim()andacpi_bus_hot_remove_device()and some of them are installed for ACPI handles that have no structacpi_deviceobjects attached (i.e. before those objects are created), those notify handlers have to takeacpi_scan_lockto prevent races from taking place (e.g. a structacpi_deviceis found to be present for the given ACPI handle, but right after that it is removed byacpi_bus_trim()running in parallel to the given notify handler). Moreover, since some of them callacpi_bus_scan()andacpi_bus_trim(), this leads to the conclusion thatacpi_scan_lockshould be acquired by the callers of these two funtions rather by these functions themselves.For these reasons, make all notify handlers that can handle device addition and eject events take
acpi_scan_lockand remove theacpi_scan_locklocking fromacpi_bus_scan()andacpi_bus_trim(). Accordingly, update all of their users to make sure that they are always called underacpi_scan_lock.Furthermore, since eject operations are carried out asynchronously with respect to the notify events that trigger them, with the help of
acpi_bus_hot_remove_device(), even if notify handlers take the ACPI scan lock, it still is possible that, for example,acpi_bus_trim()will run betweenacpi_bus_hot_remove_device()and the notify handler that scheduled its execution and thatacpi_bus_trim()will remove the device node passed toacpi_bus_hot_remove_device()for ejection. In that case, the structacpi_deviceobject obtained byacpi_bus_hot_remove_device()will be invalid and not-so-funny things will ensue. To protect agaist that, make the users ofacpi_bus_hot_remove_device()runget_device()on ACPI device node objects that are about to be passed to it and makeacpi_bus_hot_remove_device()runput_device()on them and check if their ACPI handles are not NULL (makeacpi_device_unregister()clear the device nodes' ACPI handles for that check to work).Finally, observe that
acpi_os_hotplug_execute()actually can fail, in which case its caller ought to free memory allocated for the context object to prevent leaks from happening. It also needs to runput_device()on the device node that it ranget_device()on previously in that case. Modify the code accordingly.Quoted 8 lines
Quoted 5 lines
I am concerned with this approach. ACPICA calls notify handlers through
kacpi_notify_wq, which has the max active set to 1. We then usekacpi_hotplug_wq(which also has the max active set to 1) so that a hotplug procedure does not block the notify handlers since they can be used for non-hotplug events as well.In fact we use
kacpi_hotplug_wqfor a different reason. Please read the comment in__acpi_os_execute()for more details.Yes, I am aware of the issue as well.
Quoted 12 lines
Quoted 2 lines
Acquiring the scan lock in a notify handler means that a hotplug procedure can block any notify events.
Yes, it can.
Quoted 6 lines
So, I'd prefer the following approach.
- Change all hot-plug procedures (i.e. both add and delete) to proceed
underkacpi_hotplug_wqby callingacpi_os_hotplug_execute(). This serializes all hotplug procedures, and prevents blocking other notify events.Quoted 6 lines
Quoted 4 lines
Yes, we can do that. I was thinking about doing that change, but not in v3.9. There are simply too many notify handlers already there to do that so late in the cycle. And doing that for acpiphp, for example, won't be straightforward at all.
Right. I was not suggesting this approach for v3.9.
OK
Quoted 4 lines
Quoted 3 lines
Please think about the $subject patch as a temporary measure until we can do something better (which we need to do anyway to reduce code duplication among other things).
Quoted 8 lines
Quoted 5 lines
I am fine with the scan lock as long as it is internal. This patch publishes the locking interfaces to other modules, which made me worried that this might become a long term solution. If we need to fix this issue for v3.9, I am OK with it as you clarified this as a temporary solution.
Yes, I'm not going to allow anyone to use
acpi_scan_lockanywhere else. :-) I'm also going to make it internal again in v3.10 if possible.
Sounds good. :-)
Quoted 15 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
(Ideally, we should also run all online/offline procedures under a same work-queue, just like my RFC patchset did, but this is a different topic for now.)
No, I don't think it is appropriate to run online/offline from _any_ workqueue. In my opinion they should be run from user space.
Quoted 7 lines
I think there are pros and cons for this. If we use a user thread to run online/offline procedure, we can return a result directly. However, if an operation takes a long time, it will block the user thread until it is done. In addition, we have race conditions between hotplug and online/offline operations. So, we may need to come up with other type of locking if we do not use a workqueue to address it. Having both the scan lock and other lock in the callers would not be good.
Quoted 3 lines
Well, we definitely need to make offline/online and hotplug mutually exclusive, this way or another. I'm hoping that the scan lock will be sufficient for that, but I may be wrong.
Online/offline operations are independent from ACPI, so I do not think the scan lock will be effective on this.
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Quoted 6 lines
- Revert
5993c4670unless this change is absolutely necessary. From
the change log, it is not clear to me why this change was needed. It changedacpi_bus_hot_remove_device()to take anacpi_device, instead of anacpi_handle, which introduced a race condition withacpi_device.acpi_bus_hot_remove_device()should take anacpi_handle, and then obtain itsacpi_devicefrom theacpi_handlesince this function is serialized.I thought about that, but actually there's no guarantee that the handle will be valid after
_EJ0as far as I can say. So the race condition is going to be there anyway and using structacpi_devicejust makes it easier to avoid it.
Quoted 7 lines
Quoted 4 lines
In theory, yes, a stale handle could be a problem, if
_EJ0performs unload table and if ACPICA frees up its internal data structure pointed by the handle as a result. But we should not see such issue now since we do not support dynamic ACPI namespace yet.I'm waiting for information from Bob about that. If we can assume ACPI handles to be always valid, that will simplify things quite a bit.
Thanks for checking on this.
Quoted 17 lines
Quoted 17 lines
Quoted 10 lines
Quoted 6 lines
- Remove sanity checks with an
acpi_devicein the notify handlers,
which have a race condition withacpi_device. These type-specific checks will need to be removed when we have a common notify handler anyway. The notify handler can continue to check the status of ACPI device object with anacpi_handle. Type-specific sanity checks / validations can be performed within a hotplug procedure, instead.Well, the sanest approach here would be to queue up a work item on
kacpi_hotplug_wqif the event is of a "hotplug" type and let that work item do all checks, runacpi_bus_scan()etc. But not in v3.9.Agreed, and that's what I meant.
Quoted 3 lines
For v3.9, the most straightforward and least intrusive change we can do is the $subject patch as far as I can say. If you can suggest something less intrusive and more straightforward, please do.
Quoted 7 lines
Quoted 4 lines
My suggestion is to keep the scan lock internal for v3.9 and implement a new hotplug framework (i.e. the one with user space approach) for v3.10 with a proper locking mechanism. But, since you clarified this as a temporary solution, I am OK with it if we need to fix it now.
Well, honestly, I wouldn't have posted this patch if I hadn't thought that we needed a fix for v3.9. :-)
Very true. :-)
Thanks,
-Toshi
PiA+ID4gSSB0aG91Z2h0IGFib3V0IHRoYXQsIGJ1dCBhY3R1YWxseSB0aGVyZSdzIG5vIGd1YXJh bnRlZSB0aGF0IHRoZQ0KPiA+ID4gaGFuZGxlIHdpbGwgYmUgdmFsaWQgYWZ0ZXIgX0VKMCBhcyBm YXIgYXMgSSBjYW4gc2F5LiAgU28gdGhlIHJhY2UNCj4gPiA+IGNvbmRpdGlvbiBpcyBnb2luZyB0 byBiZSB0aGVyZSBhbnl3YXkgYW5kIHVzaW5nIHN0cnVjdCBhY3BpX2RldmljZQ0KPiA+ID4ganVz dCBtYWtlcyBpdCBlYXNpZXIgdG8gYXZvaWQgaXQuDQo+ID4NCj4gPiBJbiB0aGVvcnksIHllcywg YSBzdGFsZSBoYW5kbGUgY291bGQgYmUgYSBwcm9ibGVtLCBpZiBfRUowIHBlcmZvcm1zDQo+ID4g dW5sb2FkIHRhYmxlIGFuZCBpZiBBQ1BJQ0EgZnJlZXMgdXAgaXRzIGludGVybmFsIGRhdGEgc3Ry dWN0dXJlDQo+ID4gcG9pbnRlZCBieSB0aGUgaGFuZGxlIGFzIGEgcmVzdWx0LiAgQnV0IHdlIHNo b3VsZCBub3Qgc2VlIHN1Y2ggaXNzdWUNCj4gPiBub3cgc2luY2Ugd2UgZG8gbm90IHN1cHBvcnQg ZHluYW1pYyBBQ1BJIG5hbWVzcGFjZSB5ZXQuDQo+IA0KPiBJJ20gd2FpdGluZyBmb3IgaW5mb3Jt YXRpb24gZnJvbSBCb2IgYWJvdXQgdGhhdC4gIElmIHdlIGNhbiBhc3N1bWUgQUNQSQ0KPiBoYW5k bGVzIHRvIGJlIGFsd2F5cyB2YWxpZCwgdGhhdCB3aWxsIHNpbXBsaWZ5IHRoaW5ncyBxdWl0ZSBh IGJpdC4NCg0KSWYgYSB0YWJsZSBpcyB1bmxvYWRlZCwgYWxsIHRoZSBuYW1lc3BhY2Ugbm9kZXMg Zm9yIHRoYXQgdGFibGUgYXJlIHJlbW92ZWQgZnJvbSB0aGUgbmFtZXNwYWNlLCBhbmQgdGh1cyBh bnkgQUNQSV9IQU5ETEUgcG9pbnRlcnMgZ28gc3RhbGUgYW5kIGludmFsaWQuDQoNCkJvYg0KDQo=
Quoted 10 lines · Robert
Quoted 10 lines
Quoted 9 lines
Quoted 4 lines
I thought about that, but actually there's no guarantee that the handle will be valid after
_EJ0as far as I can say. So the race condition is going to be there anyway and using structacpi_devicejust makes it easier to avoid it.In theory, yes, a stale handle could be a problem, if
_EJ0performs unload table and if ACPICA frees up its internal data structure pointed by the handle as a result. But we should not see such issue now since we do not support dynamic ACPI namespace yet.
Quoted 5 lines
Quoted 2 lines
I'm waiting for information from Bob about that. If we can assume ACPI handles to be always valid, that will simplify things quite a bit.
If a table is unloaded, all the namespace nodes for that table are removed from the namespace, and thus any
ACPI_HANDLEpointers go stale and invalid.
OK, thanks!
To me this means that we cannot assume a handle to stay valid between a notify handler and acpi_bus_hot_remove_device() run from a workqueue.
Is there a mechanism in ACPICA to ensure that a handle won't become stale while a notify handler is running for it or is the OS responsible for ensuring that _EJ0 won't be run in parallel with notify handlers for device objects being ejected?
Rafael
Signature · 2 lines
I speak only for myself. Rafael J. Wysocki, Intel Open Source Technology Center.
Quoted 33 lines
-----Original Message-----
From: Rafael J. Wysocki [mailto:[email protected]]
Sent: Thursday, February 14, 2013 4:04 AM
To: Moore, Robert
Cc: Toshi Kani; ACPI Devel Maling List; LKML; Bjorn Helgaas; Jiang Liu; Yinghai Lu; Yasuaki Ishimatsu; Myron Stowe; [email protected]
Subject: Re: [Update][PATCH] ACPI / hotplug: Fix concurrency issues and memory leaksQuoted 9 lines · Robert
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines
I thought about that, but actually there's no guarantee that the handle will be valid after
_EJ0as far as I can say. So the race condition is going to be there anyway and using structacpi_devicejust makes it easier to avoid it.In theory, yes, a stale handle could be a problem, if
_EJ0performs unload table and if ACPICA frees up its internal data structure pointed by the handle as a result. But we should not see such issue now since we do not support dynamic ACPI namespaceyet.
Quoted 3 lines
Quoted 3 lines
I'm waiting for information from Bob about that. If we can assume ACPI handles to be always valid, that will simplify things quite a
bit.
Quoted 3 lines
If a table is unloaded, all the namespace nodes for that table are removed from the namespace, and thus any
ACPI_HANDLEpointers go staleand invalid.
OK, thanks!
Quoted 9 lines
To me this means that we cannot assume a handle to stay valid between a notify handler and
acpi_bus_hot_remove_device()run from a workqueue.Is there a mechanism in ACPICA to ensure that a handle won't become stale while a notify handler is running for it or is the OS responsible for ensuring that
_EJ0won't be run in parallel with notify handlers for device objects being ejected?
It is up to the host.
Bob
Quoted 6 lines
Rafael
--
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.
Quoted 35 lines · Robert
Quoted 35 lines
-----Original Message-----
From: Rafael J. Wysocki [mailto:[email protected]]
Sent: Thursday, February 14, 2013 4:04 AM
To: Moore, Robert
Cc: Toshi Kani; ACPI Devel Maling List; LKML; Bjorn Helgaas; Jiang Liu; Yinghai Lu; Yasuaki Ishimatsu; Myron Stowe; [email protected]
Subject: Re: [Update][PATCH] ACPI / hotplug: Fix concurrency issues and memory leaksQuoted 9 lines · Robert
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines
I thought about that, but actually there's no guarantee that the handle will be valid after
_EJ0as far as I can say. So the race condition is going to be there anyway and using structacpi_devicejust makes it easier to avoid it.In theory, yes, a stale handle could be a problem, if
_EJ0performs unload table and if ACPICA frees up its internal data structure pointed by the handle as a result. But we should not see such issue now since we do not support dynamic ACPI namespaceyet.
Quoted 3 lines
Quoted 3 lines
I'm waiting for information from Bob about that. If we can assume ACPI handles to be always valid, that will simplify things quite a
bit.
Quoted 3 lines
If a table is unloaded, all the namespace nodes for that table are removed from the namespace, and thus any
ACPI_HANDLEpointers go staleand invalid.
OK, thanks!
To me this means that we cannot assume a handle to stay valid between a notify handler and
acpi_bus_hot_remove_device()run from a workqueue.
Quoted 8 lines
Quoted 6 lines
Is there a mechanism in ACPICA to ensure that a handle won't become stale while a notify handler is running for it or is the OS responsible for ensuring that
_EJ0won't be run in parallel with notify handlers for device objects being ejected?It is up to the host.
I was afraid that that might be the case. :-)
So far the (Linux) host has been happily ignoring that potential problem, so I guess it can still be ignored for a while, although we'll need to address it eventually at one point.
Thanks,
Rafael
Signature · 2 lines
I speak only for myself. Rafael J. Wysocki, Intel Open Source Technology Center.
Quoted 59 lines
-----Original Message-----
From: Rafael J. Wysocki [mailto:[email protected]]
Sent: Thursday, February 14, 2013 12:59 PM
To: Moore, Robert
Cc: Toshi Kani; ACPI Devel Maling List; LKML; Bjorn Helgaas; Jiang Liu; Yinghai Lu; Yasuaki Ishimatsu; Myron Stowe; [email protected]
Subject: Re: [Update][PATCH] ACPI / hotplug: Fix concurrency issues and memory leaksQuoted 32 lines · Robert
Quoted 32 lines
-----Original Message-----
From: Rafael J. Wysocki [mailto:[email protected]]
Sent: Thursday, February 14, 2013 4:04 AM
To: Moore, Robert
Cc: Toshi Kani; ACPI Devel Maling List; LKML; Bjorn Helgaas; Jiang Liu; Yinghai Lu; Yasuaki Ishimatsu; Myron Stowe; [email protected]
Subject: Re: [Update][PATCH] ACPI / hotplug: Fix concurrency issues and memory leaksQuoted 10 lines · Robert
Quoted 10 lines
Quoted 10 lines
Quoted 4 lines
I thought about that, but actually there's no guarantee that the handle will be valid after
_EJ0as far as I can say. So the race condition is going to be there anyway and using structacpi_devicejust makes it easier to avoid it.In theory, yes, a stale handle could be a problem, if
_EJ0performs unload table and if ACPICA frees up its internal data structure pointed by the handle as a result. But we should not see such issue now since we do not support dynamic ACPI namespaceyet.
Quoted 4 lines
Quoted 4 lines
I'm waiting for information from Bob about that. If we can assume ACPI handles to be always valid, that will simplify things quite a
bit.
Quoted 4 lines
If a table is unloaded, all the namespace nodes for that table are removed from the namespace, and thus any
ACPI_HANDLEpointers go staleQuoted 6 lines
Quoted 6 lines
and invalid.
OK, thanks!
To me this means that we cannot assume a handle to stay valid between a notify handler and
acpi_bus_hot_remove_device()run from aworkqueue.
Quoted 8 lines
Quoted 7 lines
Is there a mechanism in ACPICA to ensure that a handle won't become stale while a notify handler is running for it or is the OS responsible for ensuring that
_EJ0won't be run in parallel with notify handlers for device objects being ejected?
Quoted 7 lines
Quoted 1 line
It is up to the host.
I was afraid that that might be the case. :-)
So far the (Linux) host has been happily ignoring that potential problem, so I guess it can still be ignored for a while, although we'll need to address it eventually at one point.
I would think it should be fairly simple to setup a mechanism to either tell the driver or for the driver to figure it out -- such that the driver knows that all handles associated with the device are now invalid. Another way to look at it is that when the device is re-installed, the driver should reinitialize such that it obtains new handles for the devices and subobjects in question.
Bob
Quoted 8 lines
Thanks,
Rafael--
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.
Quoted 66 lines · Robert
Quoted 58 lines
-----Original Message-----
From: Rafael J. Wysocki [mailto:[email protected]]
Sent: Thursday, February 14, 2013 12:59 PM
To: Moore, Robert
Cc: Toshi Kani; ACPI Devel Maling List; LKML; Bjorn Helgaas; Jiang Liu; Yinghai Lu; Yasuaki Ishimatsu; Myron Stowe; [email protected]
Subject: Re: [Update][PATCH] ACPI / hotplug: Fix concurrency issues and memory leaksQuoted 32 lines · Robert
Quoted 32 lines
-----Original Message-----
From: Rafael J. Wysocki [mailto:[email protected]]
Sent: Thursday, February 14, 2013 4:04 AM
To: Moore, Robert
Cc: Toshi Kani; ACPI Devel Maling List; LKML; Bjorn Helgaas; Jiang Liu; Yinghai Lu; Yasuaki Ishimatsu; Myron Stowe; [email protected]
Subject: Re: [Update][PATCH] ACPI / hotplug: Fix concurrency issues and memory leaksQuoted 10 lines · Robert
Quoted 10 lines
Quoted 10 lines
Quoted 4 lines
I thought about that, but actually there's no guarantee that the handle will be valid after
_EJ0as far as I can say. So the race condition is going to be there anyway and using structacpi_devicejust makes it easier to avoid it.In theory, yes, a stale handle could be a problem, if
_EJ0performs unload table and if ACPICA frees up its internal data structure pointed by the handle as a result. But we should not see such issue now since we do not support dynamic ACPI namespaceyet.
Quoted 4 lines
Quoted 4 lines
I'm waiting for information from Bob about that. If we can assume ACPI handles to be always valid, that will simplify things quite a
bit.
Quoted 4 lines
If a table is unloaded, all the namespace nodes for that table are removed from the namespace, and thus any
ACPI_HANDLEpointers go staleQuoted 6 lines
Quoted 6 lines
and invalid.
OK, thanks!
To me this means that we cannot assume a handle to stay valid between a notify handler and
acpi_bus_hot_remove_device()run from aworkqueue.
Quoted 8 lines
Quoted 7 lines
Is there a mechanism in ACPICA to ensure that a handle won't become stale while a notify handler is running for it or is the OS responsible for ensuring that
_EJ0won't be run in parallel with notify handlers for device objects being ejected?Quoted 7 lines
Quoted 1 line
It is up to the host.
I was afraid that that might be the case. :-)
So far the (Linux) host has been happily ignoring that potential problem, so I guess it can still be ignored for a while, although we'll need to address it eventually at one point.
Quoted 6 lines
I would think it should be fairly simple to setup a mechanism to either tell the driver or for the driver to figure it out -- such that the driver knows that all handles associated with the device are now invalid. Another way to look at it is that when the device is re-installed, the driver should reinitialize such that it obtains new handles for the devices and subobjects in question.
Unfortunately, there is quite strong assumption in our code that ACPI handles will not become stale before the device objects associated with them are removed. For this reason, we need to know in advance which handles will become stale as a result of a table unload and remove their device objects beforehand.
Moreover, when there's a notify handler installed for a given ACPI handle and that handle becomes stale while the notify handler is running, we'll be in trouble. To avoid that we need to ensure that table unloads and notifies will always be mutually exclusive.
Thanks,
Rafael
Signature · 2 lines
I speak only for myself. Rafael J. Wysocki, Intel Open Source Technology Center.
Quoted 80 lines · Rafael J. Wysocki
Quoted 66 lines · Robert
Quoted 58 lines
-----Original Message-----
From: Rafael J. Wysocki [mailto:[email protected]]
Sent: Thursday, February 14, 2013 12:59 PM
To: Moore, Robert
Cc: Toshi Kani; ACPI Devel Maling List; LKML; Bjorn Helgaas; Jiang Liu; Yinghai Lu; Yasuaki Ishimatsu; Myron Stowe; [email protected]
Subject: Re: [Update][PATCH] ACPI / hotplug: Fix concurrency issues and memory leaksQuoted 32 lines · Robert
Quoted 32 lines
-----Original Message-----
From: Rafael J. Wysocki [mailto:[email protected]]
Sent: Thursday, February 14, 2013 4:04 AM
To: Moore, Robert
Cc: Toshi Kani; ACPI Devel Maling List; LKML; Bjorn Helgaas; Jiang Liu; Yinghai Lu; Yasuaki Ishimatsu; Myron Stowe; [email protected]
Subject: Re: [Update][PATCH] ACPI / hotplug: Fix concurrency issues and memory leaksQuoted 10 lines · Robert
Quoted 10 lines
Quoted 10 lines
Quoted 4 lines
I thought about that, but actually there's no guarantee that the handle will be valid after
_EJ0as far as I can say. So the race condition is going to be there anyway and using structacpi_devicejust makes it easier to avoid it.In theory, yes, a stale handle could be a problem, if
_EJ0performs unload table and if ACPICA frees up its internal data structure pointed by the handle as a result. But we should not see such issue now since we do not support dynamic ACPI namespaceyet.
Quoted 4 lines
Quoted 4 lines
I'm waiting for information from Bob about that. If we can assume ACPI handles to be always valid, that will simplify things quite a
bit.
Quoted 4 lines
If a table is unloaded, all the namespace nodes for that table are removed from the namespace, and thus any
ACPI_HANDLEpointers go staleQuoted 6 lines
Quoted 6 lines
and invalid.
OK, thanks!
To me this means that we cannot assume a handle to stay valid between a notify handler and
acpi_bus_hot_remove_device()run from aworkqueue.
Quoted 8 lines
Quoted 7 lines
Is there a mechanism in ACPICA to ensure that a handle won't become stale while a notify handler is running for it or is the OS responsible for ensuring that
_EJ0won't be run in parallel with notify handlers for device objects being ejected?Quoted 7 lines
Quoted 1 line
It is up to the host.
I was afraid that that might be the case. :-)
So far the (Linux) host has been happily ignoring that potential problem, so I guess it can still be ignored for a while, although we'll need to address it eventually at one point.
Quoted 6 lines
I would think it should be fairly simple to setup a mechanism to either tell the driver or for the driver to figure it out -- such that the driver knows that all handles associated with the device are now invalid. Another way to look at it is that when the device is re-installed, the driver should reinitialize such that it obtains new handles for the devices and subobjects in question.
Unfortunately, there is quite strong assumption in our code that ACPI handles will not become stale before the device objects associated with them are removed. For this reason, we need to know in advance which handles will become stale as a result of a table unload and remove their device objects beforehand.
Quoted 4 lines
Moreover, when there's a notify handler installed for a given ACPI handle and that handle becomes stale while the notify handler is running, we'll be in trouble. To avoid that we need to ensure that table unloads and notifies will always be mutually exclusive.
I wonder if we can make acpi_ns_validate_handle() to actually be able to verify if a given handle is valid. This way, ACPICA can fail gracefully (AE_BAD_PARAMETER) when a stable handle is passed to the interfaces.
Thanks,
-Toshi
Quoted 81 lines · Toshi Kani
Quoted 80 lines · Rafael J. Wysocki
Quoted 66 lines · Robert
Quoted 58 lines
-----Original Message-----
From: Rafael J. Wysocki [mailto:[email protected]]
Sent: Thursday, February 14, 2013 12:59 PM
To: Moore, Robert
Cc: Toshi Kani; ACPI Devel Maling List; LKML; Bjorn Helgaas; Jiang Liu; Yinghai Lu; Yasuaki Ishimatsu; Myron Stowe; [email protected]
Subject: Re: [Update][PATCH] ACPI / hotplug: Fix concurrency issues and memory leaksQuoted 32 lines · Robert
Quoted 32 lines
-----Original Message-----
From: Rafael J. Wysocki [mailto:[email protected]]
Sent: Thursday, February 14, 2013 4:04 AM
To: Moore, Robert
Cc: Toshi Kani; ACPI Devel Maling List; LKML; Bjorn Helgaas; Jiang Liu; Yinghai Lu; Yasuaki Ishimatsu; Myron Stowe; [email protected]
Subject: Re: [Update][PATCH] ACPI / hotplug: Fix concurrency issues and memory leaksQuoted 10 lines · Robert
Quoted 10 lines
Quoted 10 lines
Quoted 4 lines
I thought about that, but actually there's no guarantee that the handle will be valid after
_EJ0as far as I can say. So the race condition is going to be there anyway and using structacpi_devicejust makes it easier to avoid it.In theory, yes, a stale handle could be a problem, if
_EJ0performs unload table and if ACPICA frees up its internal data structure pointed by the handle as a result. But we should not see such issue now since we do not support dynamic ACPI namespaceyet.
Quoted 4 lines
Quoted 4 lines
I'm waiting for information from Bob about that. If we can assume ACPI handles to be always valid, that will simplify things quite a
bit.
Quoted 4 lines
If a table is unloaded, all the namespace nodes for that table are removed from the namespace, and thus any
ACPI_HANDLEpointers go staleQuoted 6 lines
Quoted 6 lines
and invalid.
OK, thanks!
To me this means that we cannot assume a handle to stay valid between a notify handler and
acpi_bus_hot_remove_device()run from aworkqueue.
Quoted 8 lines
Quoted 7 lines
Is there a mechanism in ACPICA to ensure that a handle won't become stale while a notify handler is running for it or is the OS responsible for ensuring that
_EJ0won't be run in parallel with notify handlers for device objects being ejected?Quoted 7 lines
Quoted 1 line
It is up to the host.
I was afraid that that might be the case. :-)
So far the (Linux) host has been happily ignoring that potential problem, so I guess it can still be ignored for a while, although we'll need to address it eventually at one point.
Quoted 6 lines
I would think it should be fairly simple to setup a mechanism to either tell the driver or for the driver to figure it out -- such that the driver knows that all handles associated with the device are now invalid. Another way to look at it is that when the device is re-installed, the driver should reinitialize such that it obtains new handles for the devices and subobjects in question.
Unfortunately, there is quite strong assumption in our code that ACPI handles will not become stale before the device objects associated with them are removed. For this reason, we need to know in advance which handles will become stale as a result of a table unload and remove their device objects beforehand.
Quoted 8 lines
Quoted 4 lines
Moreover, when there's a notify handler installed for a given ACPI handle and that handle becomes stale while the notify handler is running, we'll be in trouble. To avoid that we need to ensure that table unloads and notifies will always be mutually exclusive.
I wonder if we can make
acpi_ns_validate_handle()to actually be able to verify if a given handle is valid. This way, ACPICA can fail gracefully (AE_BAD_PARAMETER) when a stable handle is passed to the interfaces.
That'd be good, but to implement it, I think, it would be necessary to introduce some reference counting of namespace objects such that the given object would only be deleted after the last reference to it had been dropped. On table unload it would just be marked as invalid, but it would stay in memory as long as there were any references to it.
So, for example, a notify handler would start from something like acpi_add_reference(handle), which would guarantee that the object pointed to by handle would stay in memory, and it would finish by doing acpi_drop_reference(handle) or a work item scheduled by it would do that.
We do that for objects based on struct device and it works well.
Thanks,
Rafael
Signature · 2 lines
I speak only for myself. Rafael J. Wysocki, Intel Open Source Technology Center.
Quoted 97 lines · Rafael J. Wysocki
Quoted 81 lines · Toshi Kani
Quoted 80 lines · Rafael J. Wysocki
Quoted 66 lines · Robert
Quoted 58 lines
-----Original Message-----
From: Rafael J. Wysocki [mailto:[email protected]]
Sent: Thursday, February 14, 2013 12:59 PM
To: Moore, Robert
Cc: Toshi Kani; ACPI Devel Maling List; LKML; Bjorn Helgaas; Jiang Liu; Yinghai Lu; Yasuaki Ishimatsu; Myron Stowe; [email protected]
Subject: Re: [Update][PATCH] ACPI / hotplug: Fix concurrency issues and memory leaksQuoted 32 lines · Robert
Quoted 32 lines
-----Original Message-----
From: Rafael J. Wysocki [mailto:[email protected]]
Sent: Thursday, February 14, 2013 4:04 AM
To: Moore, Robert
Cc: Toshi Kani; ACPI Devel Maling List; LKML; Bjorn Helgaas; Jiang Liu; Yinghai Lu; Yasuaki Ishimatsu; Myron Stowe; [email protected]
Subject: Re: [Update][PATCH] ACPI / hotplug: Fix concurrency issues and memory leaksQuoted 10 lines · Robert
Quoted 10 lines
Quoted 10 lines
Quoted 4 lines
I thought about that, but actually there's no guarantee that the handle will be valid after
_EJ0as far as I can say. So the race condition is going to be there anyway and using structacpi_devicejust makes it easier to avoid it.In theory, yes, a stale handle could be a problem, if
_EJ0performs unload table and if ACPICA frees up its internal data structure pointed by the handle as a result. But we should not see such issue now since we do not support dynamic ACPI namespaceyet.
Quoted 4 lines
Quoted 4 lines
I'm waiting for information from Bob about that. If we can assume ACPI handles to be always valid, that will simplify things quite a
bit.
Quoted 4 lines
If a table is unloaded, all the namespace nodes for that table are removed from the namespace, and thus any
ACPI_HANDLEpointers go staleQuoted 6 lines
Quoted 6 lines
and invalid.
OK, thanks!
To me this means that we cannot assume a handle to stay valid between a notify handler and
acpi_bus_hot_remove_device()run from aworkqueue.
Quoted 8 lines
Quoted 7 lines
Is there a mechanism in ACPICA to ensure that a handle won't become stale while a notify handler is running for it or is the OS responsible for ensuring that
_EJ0won't be run in parallel with notify handlers for device objects being ejected?Quoted 7 lines
Quoted 1 line
It is up to the host.
I was afraid that that might be the case. :-)
So far the (Linux) host has been happily ignoring that potential problem, so I guess it can still be ignored for a while, although we'll need to address it eventually at one point.
Quoted 6 lines
I would think it should be fairly simple to setup a mechanism to either tell the driver or for the driver to figure it out -- such that the driver knows that all handles associated with the device are now invalid. Another way to look at it is that when the device is re-installed, the driver should reinitialize such that it obtains new handles for the devices and subobjects in question.
Unfortunately, there is quite strong assumption in our code that ACPI handles will not become stale before the device objects associated with them are removed. For this reason, we need to know in advance which handles will become stale as a result of a table unload and remove their device objects beforehand.
Quoted 8 lines
Quoted 4 lines
Moreover, when there's a notify handler installed for a given ACPI handle and that handle becomes stale while the notify handler is running, we'll be in trouble. To avoid that we need to ensure that table unloads and notifies will always be mutually exclusive.
I wonder if we can make
acpi_ns_validate_handle()to actually be able to verify if a given handle is valid. This way, ACPICA can fail gracefully (AE_BAD_PARAMETER) when a stable handle is passed to the interfaces.That'd be good, but to implement it, I think, it would be necessary to introduce some reference counting of namespace objects such that the given object would only be deleted after the last reference to it had been dropped. On table unload it would just be marked as invalid, but it would stay in memory as long as there were any references to it.
Quoted 6 lines
So, for example, a notify handler would start from something like
acpi_add_reference(handle), which would guarantee that the object pointed to by handle would stay in memory, and it would finish by doingacpi_drop_reference(handle)or a work item scheduled by it would do that.We do that for objects based on struct device and it works well.
There is other way to implement it. Since acpi_handle is defined as an opaque value, this could be changed to an index to an array of pointers, instead of a direct pointer. Then we can safely invalidate an index by invalidating the pointer associated with the index.
Thanks,
-Toshi
Quoted 53 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Is there a mechanism in ACPICA to ensure that a handle won't become stale while a notify handler is running for it or is the OS responsible for ensuring that
_EJ0won't be run in parallel with notify handlers for device objects being ejected?Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
It is up to the host.
I was afraid that that might be the case. :-)
So far the (Linux) host has been happily ignoring that potential problem, so I guess it can still be ignored for a while, although we'll need to address it eventually at one
point.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
I would think it should be fairly simple to setup a mechanism to either tell the driver or for the driver to figure it out -- such that the driver knows that all handles associated with the device are now invalid. Another way to look at it is that when the device is re-installed, the driver should reinitialize such that it obtains new handles for the devices and subobjects in
question.
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Unfortunately, there is quite strong assumption in our code that ACPI handles will not become stale before the device objects associated with them are removed. For this reason, we need to know in advance which handles will become stale as a result of a table unload and remove their device objects beforehand.
Moreover, when there's a notify handler installed for a given ACPI handle and that handle becomes stale while the notify handler is running, we'll be in trouble. To avoid that we need to ensure that table unloads and notifies will always be mutually exclusive.
Quoted 8 lines
Quoted 4 lines
I wonder if we can make
acpi_ns_validate_handle()to actually be able to verify if a given handle is valid. This way, ACPICA can fail gracefully
(AE_BAD_PARAMETER) when a stable handle is passed to the interfaces.That'd be good, but to implement it, I think, it would be necessary to introduce some reference counting of namespace objects such that the given object would only be deleted after the last reference to it had
been dropped.
Quoted 8 lines
On table unload it would just be marked as invalid, but it would stay in memory as long as there were any references to it.
So, for example, a notify handler would start from something like
acpi_add_reference(handle), which would guarantee that the object pointed to by handle would stay in memory, and it would finish by doingacpi_drop_reference(handle)or a work item scheduled by it would do
Quoted 8 lines
that.
Quoted 2 lines
We do that for objects based on struct device and it works well.
There is other way to implement it. Since
acpi_handleis defined as an opaque value, this could be changed to an index to an array of pointers, instead of a direct pointer. Then we can safely invalidate an index by invalidating the pointer associated with the index.
We have of course thought about adding a mechanism to validate/invalidate acpica namespace handles. In fact, this is why the ACPI_HANDLE data type was introduced in the first place, so that if we ever wanted or needed to implement something like this, it would not break a lot of existing code.
However, we have never had a real need to implement such a mechanism, nor has the ever been a request from any of the operating systems that run ACPICA.
The existing model is that the host has knowledge of what objects will go away when a table is unloaded, and can simply assume that all related acpi handles will go bad after the unload (and table unload is the only case where parts of the namespace go away, as far as I remember). Upon a reload of the table, the host knows to reinitialize all handles associated with the table/device.
ACPCI has a table handler mechanism, where the host handler is invoked *before* an ACPI table is actually unloaded, so the host can prepare for the unload. For example, before returning from the table handler, the host may want to synchronize by waiting for any outstanding notifies to complete, then simply ignoring any further notifies from any devices associated with the table.
In summary, it has always been felt that the fairly large overhead of implementing a mechanism like this is not worth the cost, as well as not really needed.
I suggest that the implementation of eject support proceed by using the existing mechanisms such as the table handler. If additional support/interfaces are needed in ACPICA, we can discuss it. However, just about the last thing I would like to do is add a level of indirection between the ACPI_HANDLE and the ACPI_NAMESPACE_NODE -- which would require a large, global change to ACPICA that would be only applicable for a single rather rare issue, the unloading of an ACPI table. Just the fact that we are discussing this in 2013 and ACPICA has been running since 1999 should confirm the rarity of this case and/or that the existing mechanism has been sufficient for other hosts that run ACPICA.
Bob
Quoted 67 lines · Robert
Quoted 52 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Is there a mechanism in ACPICA to ensure that a handle won't become stale while a notify handler is running for it or is the OS responsible for ensuring that
_EJ0won't be run in parallel with notify handlers for device objects being ejected?Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
It is up to the host.
I was afraid that that might be the case. :-)
So far the (Linux) host has been happily ignoring that potential problem, so I guess it can still be ignored for a while, although we'll need to address it eventually at one
point.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
I would think it should be fairly simple to setup a mechanism to either tell the driver or for the driver to figure it out -- such that the driver knows that all handles associated with the device are now invalid. Another way to look at it is that when the device is re-installed, the driver should reinitialize such that it obtains new handles for the devices and subobjects in
question.
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Unfortunately, there is quite strong assumption in our code that ACPI handles will not become stale before the device objects associated with them are removed. For this reason, we need to know in advance which handles will become stale as a result of a table unload and remove their device objects beforehand.
Moreover, when there's a notify handler installed for a given ACPI handle and that handle becomes stale while the notify handler is running, we'll be in trouble. To avoid that we need to ensure that table unloads and notifies will always be mutually exclusive.
Quoted 8 lines
Quoted 4 lines
I wonder if we can make
acpi_ns_validate_handle()to actually be able to verify if a given handle is valid. This way, ACPICA can fail gracefully
(AE_BAD_PARAMETER) when a stable handle is passed to the interfaces.That'd be good, but to implement it, I think, it would be necessary to introduce some reference counting of namespace objects such that the given object would only be deleted after the last reference to it had
been dropped.
Quoted 8 lines
On table unload it would just be marked as invalid, but it would stay in memory as long as there were any references to it.
So, for example, a notify handler would start from something like
acpi_add_reference(handle), which would guarantee that the object pointed to by handle would stay in memory, and it would finish by doingacpi_drop_reference(handle)or a work item scheduled by it would doQuoted 8 lines
that.
Quoted 2 lines
We do that for objects based on struct device and it works well.
There is other way to implement it. Since
acpi_handleis defined as an opaque value, this could be changed to an index to an array of pointers, instead of a direct pointer. Then we can safely invalidate an index by invalidating the pointer associated with the index.We have of course thought about adding a mechanism to validate/invalidate acpica namespace handles. In fact, this is why the
ACPI_HANDLEdata type was introduced in the first place, so that if we ever wanted or needed to implement something like this, it would not break a lot of existing code.However, we have never had a real need to implement such a mechanism, nor has the ever been a request from any of the operating systems that run ACPICA.
Quoted 8 lines
The existing model is that the host has knowledge of what objects will go away when a table is unloaded, and can simply assume that all related acpi handles will go bad after the unload (and table unload is the only case where parts of the namespace go away, as far as I remember). Upon a reload of the table, the host knows to reinitialize all handles associated with the table/device.
ACPCI has a table handler mechanism, where the host handler is invoked *before* an ACPI table is actually unloaded, so the host can prepare for the unload. For example, before returning from the table handler, the host may want to synchronize by waiting for any outstanding notifies to complete, then simply ignoring any further notifies from any devices associated with the table.
In summary, it has always been felt that the fairly large overhead of implementing a mechanism like this is not worth the cost, as well as not really needed.
I suggest that the implementation of eject support proceed by using the existing mechanisms such as the table handler. If additional support/interfaces are needed in ACPICA, we can discuss it. However, just about the last thing I would like to do is add a level of indirection between the
ACPI_HANDLEand theACPI_NAMESPACE_NODE-- which would require a large, global change to ACPICA that would be only applicable for a single rather rare issue, the unloading of an ACPI table. Just the fact that we are discussing this in 2013 and ACPICA has been running since 1999 should confirm the rarity of this case and/or that the existing mechanism has been sufficient for other hosts that run ACPICA.
Thanks for the info. I understand that making such changes requires a lot of effort. This is just a brainstorming, and as you said, I do not think there is any platform that can cause this issue on Linux today. We are still in the process of handling load/unload table properly in the kernel. Given your input, Rafael's approach of using reference counting on struct device seems to be the best choice for us.
BTW, I did work on other OS that supports load/unload table (which might be the first OS that supported this feature.) It protects from this race condition with serialization between the OS and FW with _OST. However, we cannot expect all platforms to do the same for Linux.
Thanks,
-Toshi
Quoted 46 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
This changeset is aimed at fixing a few different but related problems in the ACPI hotplug infrastructure.
First of all, since notify handlers may be run in parallel with
acpi_bus_scan(),acpi_bus_trim()andacpi_bus_hot_remove_device()and some of them are installed for ACPI handles that have no structacpi_deviceobjects attached (i.e. before those objects are created), those notify handlers have to takeacpi_scan_lockto prevent races from taking place (e.g. a structacpi_deviceis found to be present for the given ACPI handle, but right after that it is removed byacpi_bus_trim()running in parallel to the given notify handler). Moreover, since some of them callacpi_bus_scan()andacpi_bus_trim(), this leads to the conclusion thatacpi_scan_lockshould be acquired by the callers of these two funtions rather by these functions themselves.For these reasons, make all notify handlers that can handle device addition and eject events take
acpi_scan_lockand remove theacpi_scan_locklocking fromacpi_bus_scan()andacpi_bus_trim(). Accordingly, update all of their users to make sure that they are always called underacpi_scan_lock.Furthermore, since eject operations are carried out asynchronously with respect to the notify events that trigger them, with the help of
acpi_bus_hot_remove_device(), even if notify handlers take the ACPI scan lock, it still is possible that, for example,acpi_bus_trim()will run betweenacpi_bus_hot_remove_device()and the notify handler that scheduled its execution and thatacpi_bus_trim()will remove the device node passed toacpi_bus_hot_remove_device()for ejection. In that case, the structacpi_deviceobject obtained byacpi_bus_hot_remove_device()will be invalid and not-so-funny things will ensue. To protect agaist that, make the users ofacpi_bus_hot_remove_device()runget_device()on ACPI device node objects that are about to be passed to it and makeacpi_bus_hot_remove_device()runput_device()on them and check if their ACPI handles are not NULL (makeacpi_device_unregister()clear the device nodes' ACPI handles for that check to work).Finally, observe that
acpi_os_hotplug_execute()actually can fail, in which case its caller ought to free memory allocated for the context object to prevent leaks from happening. It also needs to runput_device()on the device node that it ranget_device()on previously in that case. Modify the code accordingly.
Quoted 6 lines
Signed-off-by Rafael J. Wysocki <[email protected]>Acked-by Yinghai Lu <[email protected]>---
This includes fixes for two issues spotted by Yasuaki Ishimatsu.
this one will make pci/next and pm/linux-next conflicts
Please check if attached fix is right.
Thanks
Yinghai
Quoted 567 lines
Thanks,
Rafael--- drivers/acpi/acpi_memhotplug.c | 56 +++++++++++++++++++----------- drivers/acpi/container.c | 12 ++++-- drivers/acpi/dock.c | 19 ++++++++-- drivers/acpi/processor_driver.c | 24 +++++++++--- drivers/acpi/scan.c | 69 +++++++++++++++++++++++++------------ drivers/pci/hotplug/acpiphp_glue.c | 6 +++ drivers/pci/hotplug/sgi_hotplug.c | 5 ++ include/acpi/acpi_bus.h | 3 + 8 files changed, 139 insertions(+), 55 deletions(-)Index:
test/drivers/acpi/scan.c
===================================================================drivers/acpi/scan.c +48
-21@@ -42,6 +42,18 @@ struct acpi_device_bus_id{struct list_head node; }; +void acpi_scan_lock_acquire (void ) +{ +mutex_lock (&acpi_scan_lock); +} +EXPORT_SYMBOL_GPL (acpi_scan_lock_acquire); + +void acpi_scan_lock_release (void ) +{ +mutex_unlock (&acpi_scan_lock); +} +EXPORT_SYMBOL_GPL (acpi_scan_lock_release); +int acpi_scan_add_handler (struct acpi_scan_handler *handler) {if (!handler || !handler->attach)@@ -95,8 +107,6 @@ acpi_device_modalias_show(struct device } static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL); -static void __acpi_bus_trim (struct acpi_device *start); -/** * acpi_bus_hot_remove_device: hot-remove a device and its children * @context: struct acpi_eject_event pointer (freed in this func) @@ -107,7 +117,7 @@ static void __acpi_bus_trim(struct acpi_*/ void acpi_bus_hot_remove_device (void *context) { -struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context; +struct acpi_eject_event *ej_event = context;struct acpi_device *device = ej_event->device;acpi_handle handle = device->handle;acpi_handle temp;@@ -118,11 +128,19 @@ void acpi_bus_hot_remove_device(void *comutex_lock (&acpi_scan_lock); +/* If there is no handle, the device node has been unregistered. */ +if (!device->handle) { +dev_dbg (&device->dev,"ACPI handle missing\n" ); +put_device (&device->dev); + goto out; + } +ACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"Hot-removing device %s...\n" ,dev_name (&device->dev))); -__acpi_bus_trim (device); -/* Device node has been released. */ +acpi_bus_trim (device); +/* Device node has been unregistered. */ +put_device (&device->dev); device =NULL ;if (ACPI_SUCCESS (acpi_get_handle (handle,"_LCK" , &temp))) {@@ -151,6 +169,7 @@ void acpi_bus_hot_remove_device(void *co ost_code, NULL); } + out:mutex_unlock (&acpi_scan_lock);kfree (context);return ;@@ -212,6 +231,7 @@ acpi_eject_store(struct device *d, struc goto err; } +get_device (&acpi_device->dev); ej_event->device = acpi_device;if (acpi_device->flags.eject_pending) {/* event originated from ACPI eject notification */ @@ -224,7 +244,11 @@ acpi_eject_store(struct device *d, struc ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL); } -acpi_os_hotplug_execute (acpi_bus_hot_remove_device, (void *)ej_event); + status =acpi_os_hotplug_execute (acpi_bus_hot_remove_device, ej_event); +if (ACPI_FAILURE (status)) { +put_device (&acpi_device->dev); +kfree (ej_event); + } err:return ret; }@@ -779,6 +803,7 @@ static void acpi_device_unregister(struc* no more references. */ acpi_device_set_power (device,ACPI_STATE_D3_COLD ); + device->handle =NULL ;put_device (&device->dev); }@@ -1626,14 +1651,14 @@ static acpi_status acpi_bus_device_attac* there has been a real error. There just have been no suitable ACPI objects * in the table trunk from which the kernel could create a device and add an * appropriate driver. +* +* Must be called under acpi_scan_lock. */ int acpi_bus_scan (acpi_handle handle) {void *device =NULL ;int error =0 ; -mutex_lock (&acpi_scan_lock); -if (ACPI_SUCCESS (acpi_bus_check_add (handle,0 ,NULL , &device)))acpi_walk_namespace (ACPI_TYPE_ANY , handle,ACPI_UINT32_MAX , acpi_bus_check_add,NULL ,NULL , &device);@@ -1644,7 +1669,6 @@ int acpi_bus_scan(acpi_handle handle)acpi_walk_namespace (ACPI_TYPE_ANY , handle,ACPI_UINT32_MAX , acpi_bus_device_attach,NULL ,NULL ,NULL ); -mutex_unlock (&acpi_scan_lock);return error; }EXPORT_SYMBOL (acpi_bus_scan);@@ -1681,7 +1705,13 @@ static acpi_status acpi_bus_remove(acpi_return AE_OK ; } -static void __acpi_bus_trim (struct acpi_device *start) +/** +* acpi_bus_trim - Remove ACPI device node and all of its descendants +* @start: Root of the ACPI device nodes subtree to remove. +* +* Must be called under acpi_scan_lock. +*/ +void acpi_bus_trim (struct acpi_device *start) {/* * Execute acpi_bus_device_detach() as a post-order callback to detach @@ -1698,13 +1728,6 @@ static void __acpi_bus_trim(struct acpi_ acpi_bus_remove, NULL, NULL);acpi_bus_remove (start->handle,0 ,NULL ,NULL ); } - -void acpi_bus_trim (struct acpi_device *start) -{ -mutex_lock (&acpi_scan_lock); -__acpi_bus_trim (start); -mutex_unlock (&acpi_scan_lock); -}EXPORT_SYMBOL_GPL (acpi_bus_trim);static int acpi_bus_scan_fixed (void )@@ -1761,23 +1784,27 @@ int __init acpi_scan_init(void)acpi_csrt_init ();acpi_container_init (); +mutex_lock (&acpi_scan_lock);/* * Enumerate devices in the ACPI namespace. */ result =acpi_bus_scan (ACPI_ROOT_OBJECT );if (result) -return result; + goto out; result =acpi_bus_get_device (ACPI_ROOT_OBJECT , &acpi_root);if (result) -return result; + goto out; result =acpi_bus_scan_fixed ();if (result) {acpi_device_unregister (acpi_root); -return result; + goto out; }acpi_update_all_gpes (); -return 0 ; + + out: +mutex_unlock (&acpi_scan_lock); +return result; }Index:
test/include/acpi/acpi_bus.h
===================================================================include/acpi/acpi_bus.h +3
-0@@ -395,6 +395,9 @@ int acpi_bus_receive_event(struct acpi_bstatic inline int acpi_bus_generate_proc_event (struct acpi_device *device,u8 type,int data) {return 0 ; }#endif + +void acpi_scan_lock_acquire (void ); +void acpi_scan_lock_release (void );int acpi_scan_add_handler (struct acpi_scan_handler *handler);int acpi_bus_register_driver (struct acpi_driver *driver);void acpi_bus_unregister_driver (struct acpi_driver *driver);Index:
test/drivers/acpi/acpi_memhotplug.c
===================================================================drivers/acpi/acpi_memhotplug.c +37
-19@@ -153,14 +153,16 @@ acpi_memory_get_device_resources(structreturn 0 ; } -static int -acpi_memory_get_device (acpi_handle handle, -struct acpi_memory_device **mem_device) +static int acpi_memory_get_device (acpi_handle handle, +struct acpi_memory_device **mem_device) {struct acpi_device *device =NULL ; -int result; +int result =0 ; + +acpi_scan_lock_acquire (); -if (!acpi_bus_get_device (handle, &device) && device) +acpi_bus_get_device (handle, &device); +if (device) goto end;/* @@ -169,23 +171,28 @@ acpi_memory_get_device(acpi_handle handl*/ result =acpi_bus_scan (handle);if (result) { -acpi_handle_warn (handle,"Cannot add acpi bus\n" ); -return -EINVAL ; +acpi_handle_warn (handle,"ACPI namespace scan failed\n" ); + result = -EINVAL ; + goto out; } result =acpi_bus_get_device (handle, &device);if (result) {acpi_handle_warn (handle,"Missing device object\n" ); -return -EINVAL ; + result = -EINVAL ; + goto out; } - end: + end: *mem_device =acpi_driver_data (device);if (!(*mem_device)) {dev_err (&device->dev,"driver data not found\n" ); -return -ENODEV ; + result = -ENODEV ; + goto out; } -return 0 ; + out: +acpi_scan_lock_release (); +return result; }static int acpi_memory_check_device (struct acpi_memory_device *mem_device)@@ -305,6 +312,7 @@ static void acpi_memory_device_notify(acstruct acpi_device *device;struct acpi_eject_event *ej_event =NULL ;u32 ost_code =ACPI_OST_SC_NON_SPECIFIC_FAILURE ;/* default */ +acpi_status status;switch (event) {case ACPI_NOTIFY_BUS_CHECK :@@ -327,29 +335,40 @@ static void acpi_memory_device_notify(acACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"\nReceived EJECT REQUEST notification for device\n" )); + status =AE_ERROR ; +acpi_scan_lock_acquire (); +if (acpi_bus_get_device (handle, &device)) {acpi_handle_err (handle,"Device doesn't exist\n" ); -break ; + goto unlock; } mem_device =acpi_driver_data (device);if (!mem_device) {acpi_handle_err (handle,"Driver Data is NULL\n" ); -break ; + goto unlock; } ej_event =kmalloc (sizeof (*ej_event),GFP_KERNEL );if (!ej_event) {pr_err (PREFIX "No memory, dropping EJECT\n" ); -break ; + goto unlock; } +get_device (&device->dev); ej_event->device = device; ej_event->event =ACPI_NOTIFY_EJECT_REQUEST ; -acpi_os_hotplug_execute (acpi_bus_hot_remove_device, - (void *)ej_event); +/* The eject is carried out asynchronously. */ + status =acpi_os_hotplug_execute (acpi_bus_hot_remove_device, + ej_event); +if (ACPI_FAILURE (status)) { +put_device (&device->dev); +kfree (ej_event); + } -/* eject is performed asynchronously */ -return ; + unlock: +acpi_scan_lock_release (); +if (ACPI_SUCCESS (status)) +return ;default :ACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"Unsupported event [0x%x]\n" , event));@@ -360,7 +379,6 @@ static void acpi_memory_device_notify(ac/* Inform firmware that the hotplug operation has completed */ (void )acpi_evaluate_hotplug_ost (handle, event, ost_code,NULL ); -return ; }static void acpi_memory_device_free (struct acpi_memory_device *mem_device)Index:
test/drivers/acpi/processor_driver.c
===================================================================drivers/acpi/processor_driver.c +17
-7@@ -683,8 +683,11 @@ static void acpi_processor_hotplug_notifstruct acpi_device *device =NULL ;struct acpi_eject_event *ej_event =NULL ;u32 ost_code =ACPI_OST_SC_NON_SPECIFIC_FAILURE ;/* default */ +acpi_status status;int result; +acpi_scan_lock_acquire (); +switch (event) {case ACPI_NOTIFY_BUS_CHECK :case ACPI_NOTIFY_DEVICE_CHECK :@@ -733,25 +736,32 @@ static void acpi_processor_hotplug_notifbreak ; } +get_device (&device->dev); ej_event->device = device; ej_event->event =ACPI_NOTIFY_EJECT_REQUEST ; -acpi_os_hotplug_execute (acpi_bus_hot_remove_device, - (void *)ej_event); - -/* eject is performed asynchronously */ -return ; +/* The eject is carried out asynchronously. */ + status =acpi_os_hotplug_execute (acpi_bus_hot_remove_device, + ej_event); +if (ACPI_FAILURE (status)) { +put_device (&device->dev); +kfree (ej_event); +break ; + } + goto out;default :ACPI_DEBUG_PRINT ((ACPI_DB_INFO ,"Unsupported event [0x%x]\n" , event));/* non-hotplug event; possibly handled by other handler */ -return ; + goto out; }/* Inform firmware that the hotplug operation has completed */ (void )acpi_evaluate_hotplug_ost (handle, event, ost_code,NULL ); -return ; + + out: +acpi_scan_lock_release (); }static acpi_status is_processor_device (acpi_handle handle)Index:
test/drivers/acpi/container.c
===================================================================drivers/acpi/container.c +8
-4@@ -88,6 +88,8 @@ static void container_notify_cb(acpi_hanacpi_status status;u32 ost_code =ACPI_OST_SC_NON_SPECIFIC_FAILURE ;/* default */ +acpi_scan_lock_acquire (); +switch (type) {case ACPI_NOTIFY_BUS_CHECK :/* Fall through */ @@ -103,7 +105,7 @@ static void container_notify_cb(acpi_han/* device exist and this is a remove request */ device->flags.eject_pending =1 ;kobject_uevent (&device->dev.kobj,KOBJ_OFFLINE ); -return ; + goto out; }break ; }@@ -130,18 +132,20 @@ static void container_notify_cb(acpi_hanif (!acpi_bus_get_device (handle, &device) && device) { device->flags.eject_pending =1 ;kobject_uevent (&device->dev.kobj,KOBJ_OFFLINE ); -return ; + goto out; }break ;default :/* non-hotplug event; possibly handled by other handler */ -return ; + goto out; }/* Inform firmware that the hotplug operation has completed */ (void )acpi_evaluate_hotplug_ost (handle, type, ost_code,NULL ); -return ; + + out: +acpi_scan_lock_release (); }static bool is_container (acpi_handle handle)Index:
test/drivers/acpi/dock.c
===================================================================drivers/acpi/dock.c +16
-3@@ -744,7 +744,9 @@ static void acpi_dock_deferred_cb(void * {struct dock_data *data = context; +acpi_scan_lock_acquire ();dock_notify (data->handle, data->event, data->ds); +acpi_scan_lock_release ();kfree (data); }@@ -757,20 +759,31 @@ static int acpi_dock_notifier_call(strucif (event !=ACPI_NOTIFY_BUS_CHECK && event !=ACPI_NOTIFY_DEVICE_CHECK && event !=ACPI_NOTIFY_EJECT_REQUEST )return 0 ; + +acpi_scan_lock_acquire (); +list_for_each_entry (dock_station, &dock_stations, sibling) {if (dock_station->handle == handle) {struct dock_data *dd; +acpi_status status; dd =kmalloc (sizeof (*dd),GFP_KERNEL );if (!dd) -return 0 ; +break ; + dd->handle = handle; dd->event = event; dd->ds = dock_station; -acpi_os_hotplug_execute (acpi_dock_deferred_cb, dd); -return 0 ; + status =acpi_os_hotplug_execute (acpi_dock_deferred_cb, + dd); +if (ACPI_FAILURE (status)) +kfree (dd); + +break ; } } + +acpi_scan_lock_release ();return 0 ; }Index:
test/drivers/pci/hotplug/acpiphp_glue.c
===================================================================drivers/pci/hotplug/acpiphp_glue.c +6
-0@@ -1218,6 +1218,8 @@ static void _handle_hotplug_event_bridge handle = hp_work->handle; type = hp_work->type; +acpi_scan_lock_acquire (); +if (acpi_bus_get_device (handle, &device)) {/* This bridge must have just been physically inserted */ handle_bridge_insertion (handle, type);@@ -1295,6 +1297,7 @@ static void _handle_hotplug_event_bridge } out: +acpi_scan_lock_release ();kfree (hp_work);/* allocated in handle_hotplug_event_bridge */ }@@ -1341,6 +1344,8 @@ static void _handle_hotplug_event_func(s func = (struct acpiphp_func *)context; +acpi_scan_lock_acquire (); +switch (type) {case ACPI_NOTIFY_BUS_CHECK :/* bus re-enumerate */ @@ -1371,6 +1376,7 @@ static void _handle_hotplug_event_func(sbreak ; } +acpi_scan_lock_release ();kfree (hp_work);/* allocated in handle_hotplug_event_func */ }Index:
test/drivers/pci/hotplug/sgi_hotplug.c
===================================================================drivers/pci/hotplug/sgi_hotplug.c +4
-2@@ -425,6 +425,7 @@ static int enable_slot(struct hotplug_sl pdevice =NULL ; } +acpi_scan_lock_acquire ();/* * Walk the rootbus node's immediate children looking for * the slot's device node(s). There can be more than @@ -458,6 +459,7 @@ static int enable_slot(struct hotplug_sl } } } +acpi_scan_lock_release (); }/* Call the driver for the new device */ @@ -508,6 +510,7 @@ static int disable_slot(struct hotplug_s/* Get the rootbus node pointer */ phandle =PCI_CONTROLLER (slot->pci_bus)->acpi_handle; +acpi_scan_lock_acquire ();/* * Walk the rootbus node's immediate children looking for * the slot's device node(s). There can be more than @@ -538,7 +541,7 @@ static int disable_slot(struct hotplug_sacpi_bus_trim (device); } } - +acpi_scan_lock_release (); }/* Free the SN resources assigned to the Linux device.*/ --
Quoted 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-pci" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
--- drivers/acpi/internal.h | 4 ---- drivers/acpi/scan.c | 4 ---- drivers/pci/hotplug/acpiphp_glue.c | 29 ----------------------------- 3 files changed, 37 deletions(-)
drivers/acpi/internal.h +0 -4
@@ -94,11 +94,7 @@ struct acpi_ec {extern struct acpi_ec *first_ec; -<<<<<<< HEAD -int acpi_pci_root_init(void);void acpi_pci_root_hp_init (void ); -======= ->>>>>>> pm/linux-nextint acpi_ec_init (void );int acpi_ec_ecdt_probe (void );int acpi_boot_ec_enable (void );
drivers/acpi/scan.c +0 -4
@@ -1804,13 +1804,9 @@ int __init acpi_scan_init(void)acpi_update_all_gpes (); -<<<<<<< HEADacpi_pci_root_hp_init (); -return 0 ; -======= out:mutex_unlock (&acpi_scan_lock);return result; ->>>>>>> pm/linux-next }
drivers/pci/hotplug/acpiphp_glue.c +0 -29
@@ -1135,30 +1135,10 @@ static void _handle_hotplug_event_bridge hp_work = container_of(work, struct acpi_hp_work, work); handle = hp_work->handle; type = hp_work->type; -<<<<<<< HEAD bridge = (struct acpiphp_bridge *)hp_work->context; -=======acpi_scan_lock_acquire (); -if (acpi_bus_get_device (handle, &device)) { -/* This bridge must have just been physically inserted */ -handle_bridge_insertion (handle, type); - goto out; - } - - bridge =acpiphp_handle_to_bridge (handle); -if (type ==ACPI_NOTIFY_BUS_CHECK ) { -acpi_walk_namespace (ACPI_TYPE_DEVICE , handle,ACPI_UINT32_MAX , - count_sub_bridges,NULL , &num_sub_bridges,NULL ); - } - -if (!bridge && !num_sub_bridges) { -err ("cannot get bridge info\n" ); - goto out; - } ->>>>>>> pm/linux-next -acpi_get_name (handle,ACPI_FULL_PATHNAME , &buffer);switch (type) {
@@ -1213,11 +1193,7 @@ static void _handle_hotplug_event_bridgebreak ; } -<<<<<<< HEAD -======= -out:acpi_scan_lock_release (); ->>>>>>> pm/linux-nextkfree (hp_work);/* allocated in handle_hotplug_event_bridge */ }
@@ -1260,13 +1236,8 @@ static void _handle_hotplug_event_func(sacpi_get_name (handle,ACPI_FULL_PATHNAME , &buffer); -<<<<<<< HEAD -======= - func = (struct acpiphp_func *)context; -acpi_scan_lock_acquire (); ->>>>>>> pm/linux-nextswitch (type) {case ACPI_NOTIFY_BUS_CHECK :/* bus re-enumerate */
Quoted 51 lines · Yinghai Lu
Quoted 50 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
This changeset is aimed at fixing a few different but related problems in the ACPI hotplug infrastructure.
First of all, since notify handlers may be run in parallel with
acpi_bus_scan(),acpi_bus_trim()andacpi_bus_hot_remove_device()and some of them are installed for ACPI handles that have no structacpi_deviceobjects attached (i.e. before those objects are created), those notify handlers have to takeacpi_scan_lockto prevent races from taking place (e.g. a structacpi_deviceis found to be present for the given ACPI handle, but right after that it is removed byacpi_bus_trim()running in parallel to the given notify handler). Moreover, since some of them callacpi_bus_scan()andacpi_bus_trim(), this leads to the conclusion thatacpi_scan_lockshould be acquired by the callers of these two funtions rather by these functions themselves.For these reasons, make all notify handlers that can handle device addition and eject events take
acpi_scan_lockand remove theacpi_scan_locklocking fromacpi_bus_scan()andacpi_bus_trim(). Accordingly, update all of their users to make sure that they are always called underacpi_scan_lock.Furthermore, since eject operations are carried out asynchronously with respect to the notify events that trigger them, with the help of
acpi_bus_hot_remove_device(), even if notify handlers take the ACPI scan lock, it still is possible that, for example,acpi_bus_trim()will run betweenacpi_bus_hot_remove_device()and the notify handler that scheduled its execution and thatacpi_bus_trim()will remove the device node passed toacpi_bus_hot_remove_device()for ejection. In that case, the structacpi_deviceobject obtained byacpi_bus_hot_remove_device()will be invalid and not-so-funny things will ensue. To protect agaist that, make the users ofacpi_bus_hot_remove_device()runget_device()on ACPI device node objects that are about to be passed to it and makeacpi_bus_hot_remove_device()runput_device()on them and check if their ACPI handles are not NULL (makeacpi_device_unregister()clear the device nodes' ACPI handles for that check to work).Finally, observe that
acpi_os_hotplug_execute()actually can fail, in which case its caller ought to free memory allocated for the context object to prevent leaks from happening. It also needs to runput_device()on the device node that it ranget_device()on previously in that case. Modify the code accordingly.Signed-off-by Rafael J. Wysocki <[email protected]>Acked-by Yinghai Lu <[email protected]>---
Quoted 6 lines
Quoted 2 lines
This includes fixes for two issues spotted by Yasuaki Ishimatsu.
this one will make pci/next and pm/linux-next conflicts
Please check if attached fix is right.
Looks correct to me.
Thanks,
Rafael
Signature · 2 lines
I speak only for myself. Rafael J. Wysocki, Intel Open Source Technology Center.