Subject: [PATCH AUTOSEL 4.14 04/56] powerpc/powernv: hold device_hotplug_lock when calling memtrace_offline_pages()

From: Sasha Levin <[email-masked]>
Date: Fri, 18 Oct 2019 18:07:01 -0400

From: David Hildenbrand <[email-masked]>

[ Upstream commit 5666848774ef43d3db5151ec518f1deb63515c20 ]

Let's perform all checking + offlining + removing under
device_hotplug_lock, so nobody can mess with these devices via sysfs
concurrently.

[[email-masked]: take device_hotplug_lock outside of loop]
  Link: http://lkml.kernel.org/r/[email-masked]
Link: http://lkml.kernel.org/r/[email-masked]
Signed-off-by: David Hildenbrand <[email-masked]>
Reviewed-by: Pavel Tatashin <[email-masked]>
Reviewed-by: Rashmica Gupta <[email-masked]>
Acked-by: Balbir Singh <[email-masked]>
Cc: Benjamin Herrenschmidt <[email-masked]>
Cc: Paul Mackerras <[email-masked]>
Cc: Michael Ellerman <[email-masked]>
Cc: Rashmica Gupta <[email-masked]>
Cc: Michael Neuling <[email-masked]>
Cc: Boris Ostrovsky <[email-masked]>
Cc: Dan Williams <[email-masked]>
Cc: Greg Kroah-Hartman <[email-masked]>
Cc: Haiyang Zhang <[email-masked]>
Cc: Heiko Carstens <[email-masked]>
Cc: John Allen <[email-masked]>
Cc: Jonathan Corbet <[email-masked]>
Cc: Joonsoo Kim <[email-masked]>
Cc: Juergen Gross <[email-masked]>
Cc: Kate Stewart <[email-masked]>
Cc: "K. Y. Srinivasan" <[email-masked]>
Cc: Len Brown <[email-masked]>
Cc: Martin Schwidefsky <[email-masked]>
Cc: Mathieu Malaterre <[email-masked]>
Cc: Michal Hocko <[email-masked]>
Cc: Nathan Fontenot <[email-masked]>
Cc: Oscar Salvador <[email-masked]>
Cc: Philippe Ombredanne <[email-masked]>
Cc: Rafael J. Wysocki <[email-masked]>
Cc: "Rafael J. Wysocki" <[email-masked]>
Cc: Stephen Hemminger <[email-masked]>
Cc: Thomas Gleixner <[email-masked]>
Cc: Vlastimil Babka <[email-masked]>
Cc: YASUAKI ISHIMATSU <[email-masked]>
Signed-off-by: Andrew Morton <[email-masked]>
Signed-off-by: Linus Torvalds <[email-masked]>
Signed-off-by: Sasha Levin <[email-masked]>
---
 arch/powerpc/platforms/powernv/memtrace.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/platforms/powernv/memtrace.c b/arch/powerpc/platforms/powernv/memtrace.c
index c9a6d4f3403ce..cfbd242c3e011 100644
--- a/arch/powerpc/platforms/powernv/memtrace.c
+++ b/arch/powerpc/platforms/powernv/memtrace.c
@@ -99,6 +99,7 @@ static int change_memblock_state(struct memory_block *mem, void *arg)
 	return 0;
 }
 
+/* called with device_hotplug_lock held */
 static bool memtrace_offline_pages(u32 nid, u64 start_pfn, u64 nr_pages)
 {
 	u64 end_pfn = start_pfn + nr_pages - 1;
@@ -139,6 +140,7 @@ static u64 memtrace_alloc_node(u32 nid, u64 size)
 	/* Trace memory needs to be aligned to the size */
 	end_pfn = round_down(end_pfn - nr_pages, nr_pages);
 
+	lock_device_hotplug();
 	for (base_pfn = end_pfn; base_pfn > start_pfn; base_pfn -= nr_pages) {
 		if (memtrace_offline_pages(nid, base_pfn, nr_pages) == true) {
 			/*
@@ -147,7 +149,6 @@ static u64 memtrace_alloc_node(u32 nid, u64 size)
 			 * we never try to remove memory that spans two iomem
 			 * resources.
 			 */
-			lock_device_hotplug();
 			end_pfn = base_pfn + nr_pages;
 			for (pfn = base_pfn; pfn < end_pfn; pfn += bytes>> PAGE_SHIFT) {
 				remove_memory(nid, pfn << PAGE_SHIFT, bytes);
@@ -156,6 +157,7 @@ static u64 memtrace_alloc_node(u32 nid, u64 size)
 			return base_pfn << PAGE_SHIFT;
 		}
 	}
+	unlock_device_hotplug();
 
 	return 0;
 }
-- 
2.20.1