Skip to content

Commit 683058e

Browse files
committed
ACPI / hotplug: Use device offline/online for graceful hot-removal
Modify the generic ACPI hotplug code to be able to check if devices scheduled for hot-removal may be gracefully removed from the system using the device offline/online mechanism introduced previously. Namely, make acpi_scan_hot_remove() handling device hot-removal call device_offline() for all physical companions of the ACPI device nodes involved in the operation and check the results. If any of the device_offline() calls fails, the function will not progress to the removal phase (which cannot be aborted), unless its (new) force argument is set (in case of a failing offline it will put the devices offlined by it back online). In support of 'forced' device hot-removal, add a new sysfs attribute 'force_remove' that will reside under /sys/firmware/acpi/hotplug/. Signed-off-by: Rafael J. Wysocki <[email protected]> Reviewed-by: Toshi Kani <[email protected]>
1 parent 0902a90 commit 683058e

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

Documentation/ABI/testing/sysfs-firmware-acpi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ Description:
4444
or 0 (unset). Attempts to write any other values to it will
4545
cause -EINVAL to be returned.
4646

47+
What: /sys/firmware/acpi/hotplug/force_remove
48+
Date: May 2013
49+
Contact: Rafael J. Wysocki <[email protected]>
50+
Description:
51+
The number in this file (0 or 1) determines whether (1) or not
52+
(0) the ACPI subsystem will allow devices to be hot-removed even
53+
if they cannot be put offline gracefully (from the kernel's
54+
viewpoint). That number can be changed by writing a boolean
55+
value to this file.
56+
4757
What: /sys/firmware/acpi/interrupts/
4858
Date: February 2008
4959
Contact: Len Brown <[email protected]>

drivers/acpi/internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ void acpi_memory_hotplug_init(void);
4747
static inline void acpi_memory_hotplug_init(void) {}
4848
#endif
4949

50+
extern bool acpi_force_hot_remove;
51+
5052
void acpi_sysfs_add_hotplug_profile(struct acpi_hotplug_profile *hotplug,
5153
const char *name);
5254
int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,

drivers/acpi/scan.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ extern struct acpi_device *acpi_root;
2727

2828
#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
2929

30+
/*
31+
* If set, devices will be hot-removed even if they cannot be put offline
32+
* gracefully (from the kernel's standpoint).
33+
*/
34+
bool acpi_force_hot_remove;
35+
3036
static const char *dummy_hid = "device";
3137

3238
static LIST_HEAD(acpi_device_list);
@@ -120,6 +126,59 @@ acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, cha
120126
}
121127
static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
122128

129+
static acpi_status acpi_bus_offline_companions(acpi_handle handle, u32 lvl,
130+
void *data, void **ret_p)
131+
{
132+
struct acpi_device *device = NULL;
133+
struct acpi_device_physical_node *pn;
134+
acpi_status status = AE_OK;
135+
136+
if (acpi_bus_get_device(handle, &device))
137+
return AE_OK;
138+
139+
mutex_lock(&device->physical_node_lock);
140+
141+
list_for_each_entry(pn, &device->physical_node_list, node) {
142+
int ret;
143+
144+
ret = device_offline(pn->dev);
145+
if (acpi_force_hot_remove)
146+
continue;
147+
148+
if (ret < 0) {
149+
status = AE_ERROR;
150+
break;
151+
}
152+
pn->put_online = !ret;
153+
}
154+
155+
mutex_unlock(&device->physical_node_lock);
156+
157+
return status;
158+
}
159+
160+
static acpi_status acpi_bus_online_companions(acpi_handle handle, u32 lvl,
161+
void *data, void **ret_p)
162+
{
163+
struct acpi_device *device = NULL;
164+
struct acpi_device_physical_node *pn;
165+
166+
if (acpi_bus_get_device(handle, &device))
167+
return AE_OK;
168+
169+
mutex_lock(&device->physical_node_lock);
170+
171+
list_for_each_entry(pn, &device->physical_node_list, node)
172+
if (pn->put_online) {
173+
device_online(pn->dev);
174+
pn->put_online = false;
175+
}
176+
177+
mutex_unlock(&device->physical_node_lock);
178+
179+
return AE_OK;
180+
}
181+
123182
static int acpi_scan_hot_remove(struct acpi_device *device)
124183
{
125184
acpi_handle handle = device->handle;
@@ -136,10 +195,33 @@ static int acpi_scan_hot_remove(struct acpi_device *device)
136195
return -EINVAL;
137196
}
138197

198+
lock_device_hotplug();
199+
200+
status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
201+
NULL, acpi_bus_offline_companions, NULL,
202+
NULL);
203+
if (ACPI_SUCCESS(status) || acpi_force_hot_remove)
204+
status = acpi_bus_offline_companions(handle, 0, NULL, NULL);
205+
206+
if (ACPI_FAILURE(status) && !acpi_force_hot_remove) {
207+
acpi_bus_online_companions(handle, 0, NULL, NULL);
208+
acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
209+
acpi_bus_online_companions, NULL, NULL,
210+
NULL);
211+
212+
unlock_device_hotplug();
213+
214+
put_device(&device->dev);
215+
return -EBUSY;
216+
}
217+
139218
ACPI_DEBUG_PRINT((ACPI_DB_INFO,
140219
"Hot-removing device %s...\n", dev_name(&device->dev)));
141220

142221
acpi_bus_trim(device);
222+
223+
unlock_device_hotplug();
224+
143225
/* Device node has been unregistered. */
144226
put_device(&device->dev);
145227
device = NULL;
@@ -236,6 +318,7 @@ static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
236318
int error;
237319

238320
mutex_lock(&acpi_scan_lock);
321+
lock_device_hotplug();
239322

240323
acpi_bus_get_device(handle, &device);
241324
if (device) {
@@ -259,6 +342,7 @@ static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
259342
kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
260343

261344
out:
345+
unlock_device_hotplug();
262346
acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
263347
mutex_unlock(&acpi_scan_lock);
264348
}

drivers/acpi/sysfs.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,33 @@ void acpi_sysfs_add_hotplug_profile(struct acpi_hotplug_profile *hotplug,
780780
pr_err(PREFIX "Unable to add hotplug profile '%s'\n", name);
781781
}
782782

783+
static ssize_t force_remove_show(struct kobject *kobj,
784+
struct kobj_attribute *attr, char *buf)
785+
{
786+
return sprintf(buf, "%d\n", !!acpi_force_hot_remove);
787+
}
788+
789+
static ssize_t force_remove_store(struct kobject *kobj,
790+
struct kobj_attribute *attr,
791+
const char *buf, size_t size)
792+
{
793+
bool val;
794+
int ret;
795+
796+
ret = strtobool(buf, &val);
797+
if (ret < 0)
798+
return ret;
799+
800+
lock_device_hotplug();
801+
acpi_force_hot_remove = val;
802+
unlock_device_hotplug();
803+
return size;
804+
}
805+
806+
static const struct kobj_attribute force_remove_attr =
807+
__ATTR(force_remove, S_IRUGO | S_IWUSR, force_remove_show,
808+
force_remove_store);
809+
783810
int __init acpi_sysfs_init(void)
784811
{
785812
int result;
@@ -789,6 +816,10 @@ int __init acpi_sysfs_init(void)
789816
return result;
790817

791818
hotplug_kobj = kobject_create_and_add("hotplug", acpi_kobj);
819+
result = sysfs_create_file(hotplug_kobj, &force_remove_attr.attr);
820+
if (result)
821+
return result;
822+
792823
result = sysfs_create_file(acpi_kobj, &pm_profile_attr.attr);
793824
return result;
794825
}

include/acpi/acpi_bus.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ struct acpi_device_physical_node {
286286
u8 node_id;
287287
struct list_head node;
288288
struct device *dev;
289+
bool put_online:1;
289290
};
290291

291292
/* set maximum of physical nodes to 32 for expansibility */

0 commit comments

Comments
 (0)