From 7c2e17714e190b2ef857e7e842464fb47ceca146 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Tue, 4 Feb 2014 00:42:46 +0100 Subject: ACPICA: Introduce acpi_get_data_full() and rework acpi_get_data() Introduce a new function, acpi_get_data_full(), working in analogy with acpi_get_data() except that it can execute a callback provided as its 4th argument right after acpi_ns_get_attached_data() has returned a success. That will allow Linux to reference count the object pointed to by *data before the namespace mutex is released so as to ensure that it will not be freed going forward until the reference to it acquired by acpi_get_data_full() is dropped. Signed-off-by: Rafael J. Wysocki Tested-by: Mika Westerberg --- include/acpi/acpixf.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'include/acpi') diff --git a/include/acpi/acpixf.h b/include/acpi/acpixf.h index fea6773..34bad45 100644 --- a/include/acpi/acpixf.h +++ b/include/acpi/acpixf.h @@ -230,6 +230,10 @@ acpi_attach_data(acpi_handle object, acpi_object_handler handler, void *data); acpi_status acpi_detach_data(acpi_handle object, acpi_object_handler handler); acpi_status +acpi_get_data_full(acpi_handle object, acpi_object_handler handler, void **data, + void (*callback)(void *)); + +acpi_status acpi_get_data(acpi_handle object, acpi_object_handler handler, void **data); acpi_status -- cgit v1.1 From 78ea4639a7647f2fcc957c3a532bde49df9895c7 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Tue, 4 Feb 2014 00:43:05 +0100 Subject: ACPI / hotplug: Fix potential race in acpi_bus_notify() There is a slight possibility for the ACPI device object pointed to by adev in acpi_hotplug_notify_cb() to become invalid between the acpi_bus_get_device() that it comes from and the subsequent dereference of that pointer under get_device(). Namely, if acpi_scan_drop_device() runs in parallel with acpi_hotplug_notify_cb(), acpi_device_del_work_fn() queued up by it may delete the device object in question right after a successful execution of acpi_bus_get_device() in acpi_bus_notify(). An analogous problem is present in acpi_bus_notify() where the device pointer coming from acpi_bus_get_device() may become invalid before it subsequent dereference in the "if" block. To prevent that from happening, introduce a new function, acpi_bus_get_acpi_device(), working analogously to acpi_bus_get_device() except that it will grab a reference to the ACPI device object returned by it and it will do that under the ACPICA's namespace mutex. Then, make both acpi_hotplug_notify_cb() and acpi_bus_notify() use acpi_bus_get_acpi_device() instead of acpi_bus_get_device() so as to ensure that the pointers used by them will not become stale at one point. In addition to that, introduce acpi_bus_put_acpi_device() as a wrapper around put_device() to be used along with acpi_bus_get_acpi_device() and make the (new) users of the latter use acpi_bus_put_acpi_device() too. Signed-off-by: Rafael J. Wysocki Tested-by: Mika Westerberg --- include/acpi/acpi_bus.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'include/acpi') diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index 8256eb4..94fd61a 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -381,6 +381,8 @@ extern int unregister_acpi_notifier(struct notifier_block *); */ int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device); +struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle); +void acpi_bus_put_acpi_device(struct acpi_device *adev); acpi_status acpi_bus_get_status_handle(acpi_handle handle, unsigned long long *sta); int acpi_bus_get_status(struct acpi_device *device); -- cgit v1.1 From e525506fcb67a9bbd94f01eac84af802139004eb Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Tue, 4 Feb 2014 00:43:17 +0100 Subject: ACPI / hotplug / PCI: Define hotplug context lock in the core Subsequent changes will require the ACPI core to acquire the lock protecting the ACPIPHP hotplug contexts, so move the definition of the lock to the core and change its name to be more generic. Signed-off-by: Rafael J. Wysocki Tested-by: Mika Westerberg --- include/acpi/acpi_bus.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'include/acpi') diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index 94fd61a..0c82708 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -404,6 +404,8 @@ static inline bool acpi_bus_can_wakeup(acpi_handle handle) { return false; } void acpi_scan_lock_acquire(void); void acpi_scan_lock_release(void); +void acpi_lock_hp_context(void); +void acpi_unlock_hp_context(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); -- cgit v1.1 From 3c2cc7ff9e2522e42468f8e81a7277be386c5ec4 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 6 Feb 2014 17:31:37 +0100 Subject: ACPI / hotplug / PCI: Consolidate ACPIPHP with ACPI core hotplug The ACPI-based PCI hotplug (ACPIPHP) code currently attaches its hotplug context objects directly to ACPI namespace nodes representing hotplug devices. However, after recent changes causing struct acpi_device to be created for every namespace node representing a device (regardless of its status), that is not necessary any more. Moreover, it's vulnerable to the theoretical issue that the ACPI handle passed in the context between handle_hotplug_event() and hotplug_event_work() may become invalid in the meantime (as a result of a concurrent table unload). In principle, this issue might be addressed by adding a non-empty release handler for ACPIPHP hotplug context objects analogous to acpi_scan_drop_device(), but that would duplicate the code in that function and in acpi_device_del_work_fn(). For this reason, it's better to modify ACPIPHP to attach its device hotplug contexts to struct device objects representing hotplug devices and make it use acpi_hotplug_notify_cb() as its notify handler. At the same time, acpi_device_hotplug() can be modified to dispatch the new .hp.event() callback pointing to acpiphp_hotplug_event() from ACPI device objects associated with PCI devices or use the generic ACPI device hotplug code for device objects with matching scan handlers. This allows the existing code duplication between ACPIPHP and the ACPI core to be reduced too and makes further ACPI-based device hotplug consolidation possible. Signed-off-by: Rafael J. Wysocki --- include/acpi/acpi_bus.h | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'include/acpi') diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index 0c82708..53ce357 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -137,6 +137,16 @@ struct acpi_scan_handler { }; /* + * ACPI Hotplug Context + * -------------------- + */ + +struct acpi_hotplug_context { + struct acpi_device *self; + int (*event)(struct acpi_device *, u32); +}; + +/* * ACPI Driver * ----------- */ @@ -190,7 +200,8 @@ struct acpi_device_flags { u32 initialized:1; u32 visited:1; u32 no_hotplug:1; - u32 reserved:24; + u32 hotplug_notify:1; + u32 reserved:23; }; /* File System */ @@ -329,6 +340,7 @@ struct acpi_device { struct acpi_device_perf performance; struct acpi_device_dir dir; struct acpi_scan_handler *handler; + struct acpi_hotplug_context *hp; struct acpi_driver *driver; void *driver_data; struct device dev; @@ -351,6 +363,15 @@ static inline void acpi_set_device_status(struct acpi_device *adev, u32 sta) *((u32 *)&adev->status) = sta; } +static inline void acpi_set_hp_context(struct acpi_device *adev, + struct acpi_hotplug_context *hp, + int (*event)(struct acpi_device *, u32)) +{ + hp->self = adev; + hp->event = event; + adev->hp = hp; +} + /* acpi_device.dev.bus == &acpi_bus_type */ extern struct bus_type acpi_bus_type; @@ -425,6 +446,8 @@ static inline bool acpi_device_enumerated(struct acpi_device *adev) typedef void (*acpi_hp_callback)(void *data, u32 src); acpi_status acpi_hotplug_execute(acpi_hp_callback func, void *data, u32 src); +void acpi_install_hotplug_notify_handler(acpi_handle handle, void *data); +void acpi_remove_hotplug_notify_handler(acpi_handle handle); /** * module_acpi_driver(acpi_driver) - Helper macro for registering an ACPI driver -- cgit v1.1 From 5e6f236c263117cef5f0d68e3fec241ba2adc4fc Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 6 Feb 2014 13:57:58 +0100 Subject: ACPI / hotplug / PCI: Simplify acpi_install_hotplug_notify_handler() Since acpi_hotplug_notify_cb() does not use its data argument any more, the second argument of acpi_install_hotplug_notify_handler() can be dropped, so do that and update its callers accordingly. Signed-off-by: Rafael J. Wysocki --- include/acpi/acpi_bus.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/acpi') diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index 53ce357..907d507 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -446,7 +446,7 @@ static inline bool acpi_device_enumerated(struct acpi_device *adev) typedef void (*acpi_hp_callback)(void *data, u32 src); acpi_status acpi_hotplug_execute(acpi_hp_callback func, void *data, u32 src); -void acpi_install_hotplug_notify_handler(acpi_handle handle, void *data); +void acpi_install_hotplug_notify_handler(acpi_handle handle); void acpi_remove_hotplug_notify_handler(acpi_handle handle); /** -- cgit v1.1 From 1a699476e25814343766342672c655fb135224cc Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 6 Feb 2014 13:58:13 +0100 Subject: ACPI / hotplug / PCI: Hotplug notifications from acpi_bus_notify() Since acpi_bus_notify() is executed on all notifications for all devices anyway, make it execute acpi_device_hotplug() for all hotplug events instead of installing notify handlers pointing to the same function for all hotplug devices. This change reduces both the size and complexity of ACPI-based device hotplug code. Moreover, since acpi_device_hotplug() only does significant things for devices that have either an ACPI scan handler, or a hotplug context with .eject() defined, and those devices had notify handlers pointing to acpi_hotplug_notify_cb() installed before anyway, this modification shouldn't change functionality. Signed-off-by: Rafael J. Wysocki --- include/acpi/acpi_bus.h | 2 -- 1 file changed, 2 deletions(-) (limited to 'include/acpi') diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index 907d507..32f90c7 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -446,8 +446,6 @@ static inline bool acpi_device_enumerated(struct acpi_device *adev) typedef void (*acpi_hp_callback)(void *data, u32 src); acpi_status acpi_hotplug_execute(acpi_hp_callback func, void *data, u32 src); -void acpi_install_hotplug_notify_handler(acpi_handle handle); -void acpi_remove_hotplug_notify_handler(acpi_handle handle); /** * module_acpi_driver(acpi_driver) - Helper macro for registering an ACPI driver -- cgit v1.1