[PATCH 4.10 045/167] mm, devm_memremap_pages: hold device_hotplug lock over mem_hotplug_{begin, done}

From: Greg Kroah-Hartman
Date: Fri Mar 10 2017 - 05:19:50 EST

4.10-stable review patch. If anyone has any objections, please let me know.


From: Dan Williams <dan.j.williams@xxxxxxxxx>

commit b5d24fda9c3dce51fcb4eee459550a458eaaf1e2 upstream.

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.

[dan.j.williams@xxxxxxxxx: v2]
Link: http://lkml.kernel.org/r/148728203365.38457.17804568297887708345.stgit@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Link: http://lkml.kernel.org/r/148693885680.16345.17802627926777862337.stgit@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Fixes: f931ab479dd2 ("mm: fix devm_memremap_pages crash, use mem_hotplug_{begin, done}")
Signed-off-by: Dan Williams <dan.j.williams@xxxxxxxxx>
Reported-by: Ben Hutchings <ben@xxxxxxxxxxxxxxx>
Cc: Michal Hocko <mhocko@xxxxxxxx>
Cc: Toshi Kani <toshi.kani@xxxxxxx>
Cc: Vlastimil Babka <vbabka@xxxxxxx>
Cc: Logan Gunthorpe <logang@xxxxxxxxxxxx>
Cc: Masayoshi Mizuma <m.mizuma@xxxxxxxxxxxxxx>
Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Signed-off-by: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>

kernel/memremap.c | 6 ++++++
1 file changed, 6 insertions(+)

--- a/kernel/memremap.c
+++ b/kernel/memremap.c
@@ -246,9 +246,13 @@ static void devm_memremap_pages_release(
/* pages are dead and unused, undo the arch mapping */
align_start = res->start & ~(SECTION_SIZE - 1);
align_size = ALIGN(resource_size(res), SECTION_SIZE);
+ lock_device_hotplug();
arch_remove_memory(align_start, align_size);
+ unlock_device_hotplug();
untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
dev_WARN_ONCE(dev, pgmap->altmap && pgmap->altmap->alloc,
@@ -360,9 +364,11 @@ void *devm_memremap_pages(struct device
if (error)
goto err_pfn_remap;

+ lock_device_hotplug();
error = arch_add_memory(nid, align_start, align_size, true);
+ unlock_device_hotplug();
if (error)
goto err_add_memory;