[PATCH 1/2] mm, devm_memremap_pages: hold device_hotplug lock over mem_hotplug_{begin, done}

From: Dan Williams
Date: Sun Feb 12 2017 - 17:39:05 EST


The mem_hotplug_{begin,done} lock coordinates with
{get,put}_online_mems() to hold off "readers" of the current state of
memory from new hotplug actions. mem_hotplug_begin() expects exclusive
access, via the device_hotplug lock, to set mem_hotplug.active_writer.
Calling mem_hotplug_begin() without locking device_hotplug can lead to
corrupting mem_hotplug.refcount and missed wakeups / soft lockups.

Cc: <stable@xxxxxxxxxxxxxxx>
Cc: Michal Hocko <mhocko@xxxxxxxx>
Cc: Toshi Kani <toshi.kani@xxxxxxx>
Cc: Vlastimil Babka <vbabka@xxxxxxx>
Cc: Logan Gunthorpe <logang@xxxxxxxxxxxx>
Fixes: f931ab479dd2 ("mm: fix devm_memremap_pages crash, use mem_hotplug_{begin, done}")
Reported-by: Ben Hutchings <ben@xxxxxxxxxxxxxxx>
Signed-off-by: Dan Williams <dan.j.williams@xxxxxxxxx>
---
kernel/memremap.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/kernel/memremap.c b/kernel/memremap.c
index e6476a8e8b6a..16640415c150 100644
--- a/kernel/memremap.c
+++ b/kernel/memremap.c
@@ -264,9 +264,12 @@ static void devm_memremap_pages_release(struct device *dev, void *data)
}

/* pages are dead and unused, undo the arch mapping */
+ lock_device_hotplug();
mem_hotplug_begin();
arch_remove_memory(res->start, resource_size(res));
mem_hotplug_done();
+ unlock_device_hotplug();
+
untrack_pfn(NULL, PHYS_PFN(res->start), resource_size(res));
pgmap_radix_release(res);
dev_WARN_ONCE(dev, pgmap->altmap && pgmap->altmap->alloc,
@@ -374,9 +377,11 @@ void *devm_memremap_pages(struct device *dev, struct resource *res,
if (error)
goto err_pfn_remap;

+ lock_device_hotplug();
mem_hotplug_begin();
error = arch_add_memory(nid, res->start, resource_size(res), true);
mem_hotplug_done();
+ unlock_device_hotplug();
if (error)
goto err_add_memory;