[PATCH] mm/memory-hotplug: Add sysfs hot-remove trigger

From: Robin Murphy
Date: Wed Feb 06 2019 - 12:04:01 EST


ARCH_MEMORY_PROBE is a useful thing for testing and debugging hotplug,
but being able to exercise the (arguably trickier) hot-remove path would
be even more useful. Extend the feature to allow removal of offline
sections to be triggered manually to aid development.

Signed-off-by: Robin Murphy <robin.murphy@xxxxxxx>
---

This is inspired by a previous proposal[1], but in coming up with a
more robust interface I ended up rewriting the whole thing from
scratch. The lack of documentation is semi-deliberate, since I don't
like the idea of anyone actually relying on this interface as ABI, but
as a handy tool it felt useful enough to be worth sharing :)

Robin.

[1] https://lore.kernel.org/lkml/22d34fe30df0fbacbfceeb47e20cb1184af73585.1511433386.git.ar@xxxxxxxxxxxxxxxxxx/

drivers/base/memory.c | 42 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 41 insertions(+), 1 deletion(-)

diff --git a/drivers/base/memory.c b/drivers/base/memory.c
index 048cbf7d5233..26344cb9f045 100644
--- a/drivers/base/memory.c
+++ b/drivers/base/memory.c
@@ -521,7 +521,44 @@ static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
}

static DEVICE_ATTR_WO(probe);
-#endif
+
+#ifdef CONFIG_MEMORY_HOTREMOVE
+static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct memory_block *mem = to_memory_block(dev);
+ unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
+ bool remove;
+ int ret;
+
+ ret = kstrtobool(buf, &remove);
+ if (ret)
+ return ret;
+ if (!remove)
+ return count;
+
+ if (mem->state != MEM_OFFLINE)
+ return -EBUSY;
+
+ ret = lock_device_hotplug_sysfs();
+ if (ret)
+ return ret;
+
+ if (device_remove_file_self(dev, attr)) {
+ __remove_memory(pfn_to_nid(start_pfn), PFN_PHYS(start_pfn),
+ MIN_MEMORY_BLOCK_SIZE * sections_per_block);
+ ret = count;
+ } else {
+ ret = -EBUSY;
+ }
+
+ unlock_device_hotplug();
+ return ret;
+}
+
+static DEVICE_ATTR_WO(remove);
+#endif /* CONFIG_MEMORY_HOTREMOVE */
+#endif /* CONFIG_ARCH_MEMORY_PROBE */

#ifdef CONFIG_MEMORY_FAILURE
/*
@@ -615,6 +652,9 @@ static struct attribute *memory_memblk_attrs[] = {
&dev_attr_removable.attr,
#ifdef CONFIG_MEMORY_HOTREMOVE
&dev_attr_valid_zones.attr,
+#ifdef CONFIG_ARCH_MEMORY_PROBE
+ &dev_attr_remove.attr,
+#endif
#endif
NULL
};
--
2.20.1.dirty