aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base
diff options
context:
space:
mode:
authorDavid Hildenbrand <david@redhat.com>2018-10-30 18:10:29 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2018-10-31 11:54:17 -0400
commit381eab4a6ee81266f8dddc62e57376c7e584e5b8 (patch)
treedf4cf8ea76a7d8d85f13576bb95a75ec6b974612 /drivers/base
parent8df1d0e4a265f25dc1e7e7624ccdbcb4a6630c89 (diff)
mm/memory_hotplug: fix online/offline_pages called w.o. mem_hotplug_lock
There seem to be some problems as result of 30467e0b3be ("mm, hotplug: fix concurrent memory hot-add deadlock"), which tried to fix a possible lock inversion reported and discussed in [1] due to the two locks a) device_lock() b) mem_hotplug_lock While add_memory() first takes b), followed by a) during bus_probe_device(), onlining of memory from user space first took a), followed by b), exposing a possible deadlock. In [1], and it was decided to not make use of device_hotplug_lock, but rather to enforce a locking order. The problems I spotted related to this: 1. Memory block device attributes: While .state first calls mem_hotplug_begin() and the calls device_online() - which takes device_lock() - .online does no longer call mem_hotplug_begin(), so effectively calls online_pages() without mem_hotplug_lock. 2. device_online() should be called under device_hotplug_lock, however onlining memory during add_memory() does not take care of that. In addition, I think there is also something wrong about the locking in 3. arch/powerpc/platforms/powernv/memtrace.c calls offline_pages() without locks. This was introduced after 30467e0b3be. And skimming over the code, I assume it could need some more care in regards to locking (e.g. device_online() called without device_hotplug_lock. This will be addressed in the following patches. Now that we hold the device_hotplug_lock when - adding memory (e.g. via add_memory()/add_memory_resource()) - removing memory (e.g. via remove_memory()) - device_online()/device_offline() We can move mem_hotplug_lock usage back into online_pages()/offline_pages(). Why is mem_hotplug_lock still needed? Essentially to make get_online_mems()/put_online_mems() be very fast (relying on device_hotplug_lock would be very slow), and to serialize against addition of memory that does not create memory block devices (hmm). [1] http://driverdev.linuxdriverproject.org/pipermail/ driverdev-devel/ 2015-February/065324.html This patch is partly based on a patch by Vitaly Kuznetsov. Link: http://lkml.kernel.org/r/20180925091457.28651-4-david@redhat.com Signed-off-by: David Hildenbrand <david@redhat.com> Reviewed-by: Pavel Tatashin <pavel.tatashin@microsoft.com> Reviewed-by: Rashmica Gupta <rashmica.g@gmail.com> Reviewed-by: Oscar Salvador <osalvador@suse.de> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Paul Mackerras <paulus@samba.org> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: "Rafael J. Wysocki" <rjw@rjwysocki.net> Cc: Len Brown <lenb@kernel.org> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: "K. Y. Srinivasan" <kys@microsoft.com> Cc: Haiyang Zhang <haiyangz@microsoft.com> Cc: Stephen Hemminger <sthemmin@microsoft.com> Cc: Martin Schwidefsky <schwidefsky@de.ibm.com> Cc: Heiko Carstens <heiko.carstens@de.ibm.com> Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com> Cc: Juergen Gross <jgross@suse.com> Cc: Rashmica Gupta <rashmica.g@gmail.com> Cc: Michael Neuling <mikey@neuling.org> Cc: Balbir Singh <bsingharora@gmail.com> Cc: Kate Stewart <kstewart@linuxfoundation.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Philippe Ombredanne <pombredanne@nexb.com> Cc: Pavel Tatashin <pavel.tatashin@microsoft.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Oscar Salvador <osalvador@suse.de> Cc: YASUAKI ISHIMATSU <yasu.isimatu@gmail.com> Cc: Mathieu Malaterre <malat@debian.org> Cc: John Allen <jallen@linux.vnet.ibm.com> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Nathan Fontenot <nfont@linux.vnet.ibm.com> Cc: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/base')
-rw-r--r--drivers/base/memory.c13
1 files changed, 1 insertions, 12 deletions
diff --git a/drivers/base/memory.c b/drivers/base/memory.c
index 40cac122ec73..0e5985682642 100644
--- a/drivers/base/memory.c
+++ b/drivers/base/memory.c
@@ -228,7 +228,6 @@ static bool pages_correctly_probed(unsigned long start_pfn)
228/* 228/*
229 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is 229 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
230 * OK to have direct references to sparsemem variables in here. 230 * OK to have direct references to sparsemem variables in here.
231 * Must already be protected by mem_hotplug_begin().
232 */ 231 */
233static int 232static int
234memory_block_action(unsigned long phys_index, unsigned long action, int online_type) 233memory_block_action(unsigned long phys_index, unsigned long action, int online_type)
@@ -294,7 +293,6 @@ static int memory_subsys_online(struct device *dev)
294 if (mem->online_type < 0) 293 if (mem->online_type < 0)
295 mem->online_type = MMOP_ONLINE_KEEP; 294 mem->online_type = MMOP_ONLINE_KEEP;
296 295
297 /* Already under protection of mem_hotplug_begin() */
298 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE); 296 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
299 297
300 /* clear online_type */ 298 /* clear online_type */
@@ -341,19 +339,11 @@ store_mem_state(struct device *dev,
341 goto err; 339 goto err;
342 } 340 }
343 341
344 /*
345 * Memory hotplug needs to hold mem_hotplug_begin() for probe to find
346 * the correct memory block to online before doing device_online(dev),
347 * which will take dev->mutex. Take the lock early to prevent an
348 * inversion, memory_subsys_online() callbacks will be implemented by
349 * assuming it's already protected.
350 */
351 mem_hotplug_begin();
352
353 switch (online_type) { 342 switch (online_type) {
354 case MMOP_ONLINE_KERNEL: 343 case MMOP_ONLINE_KERNEL:
355 case MMOP_ONLINE_MOVABLE: 344 case MMOP_ONLINE_MOVABLE:
356 case MMOP_ONLINE_KEEP: 345 case MMOP_ONLINE_KEEP:
346 /* mem->online_type is protected by device_hotplug_lock */
357 mem->online_type = online_type; 347 mem->online_type = online_type;
358 ret = device_online(&mem->dev); 348 ret = device_online(&mem->dev);
359 break; 349 break;
@@ -364,7 +354,6 @@ store_mem_state(struct device *dev,
364 ret = -EINVAL; /* should never happen */ 354 ret = -EINVAL; /* should never happen */
365 } 355 }
366 356
367 mem_hotplug_done();
368err: 357err:
369 unlock_device_hotplug(); 358 unlock_device_hotplug();
370 359