Skip to content

Commit 381eab4

Browse files
davidhildenbrandtorvalds
authored andcommitted
mm/memory_hotplug: fix online/offline_pages called w.o. mem_hotplug_lock
There seem to be some problems as result of 30467e0 ("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 30467e0. 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>
1 parent 8df1d0e commit 381eab4

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

drivers/base/memory.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ static bool pages_correctly_probed(unsigned long start_pfn)
228228
/*
229229
* MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
230230
* OK to have direct references to sparsemem variables in here.
231-
* Must already be protected by mem_hotplug_begin().
232231
*/
233232
static int
234233
memory_block_action(unsigned long phys_index, unsigned long action, int online_type)
@@ -294,7 +293,6 @@ static int memory_subsys_online(struct device *dev)
294293
if (mem->online_type < 0)
295294
mem->online_type = MMOP_ONLINE_KEEP;
296295

297-
/* Already under protection of mem_hotplug_begin() */
298296
ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
299297

300298
/* clear online_type */
@@ -341,19 +339,11 @@ store_mem_state(struct device *dev,
341339
goto err;
342340
}
343341

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-
353342
switch (online_type) {
354343
case MMOP_ONLINE_KERNEL:
355344
case MMOP_ONLINE_MOVABLE:
356345
case MMOP_ONLINE_KEEP:
346+
/* mem->online_type is protected by device_hotplug_lock */
357347
mem->online_type = online_type;
358348
ret = device_online(&mem->dev);
359349
break;
@@ -364,7 +354,6 @@ store_mem_state(struct device *dev,
364354
ret = -EINVAL; /* should never happen */
365355
}
366356

367-
mem_hotplug_done();
368357
err:
369358
unlock_device_hotplug();
370359

mm/memory_hotplug.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,6 @@ static struct zone * __meminit move_pfn_range(int online_type, int nid,
838838
return zone;
839839
}
840840

841-
/* Must be protected by mem_hotplug_begin() or a device_lock */
842841
int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
843842
{
844843
unsigned long flags;
@@ -850,6 +849,8 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ
850849
struct memory_notify arg;
851850
struct memory_block *mem;
852851

852+
mem_hotplug_begin();
853+
853854
/*
854855
* We can't use pfn_to_nid() because nid might be stored in struct page
855856
* which is not yet initialized. Instead, we find nid from memory block.
@@ -914,13 +915,15 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ
914915

915916
if (onlined_pages)
916917
memory_notify(MEM_ONLINE, &arg);
918+
mem_hotplug_done();
917919
return 0;
918920

919921
failed_addition:
920922
pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
921923
(unsigned long long) pfn << PAGE_SHIFT,
922924
(((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
923925
memory_notify(MEM_CANCEL_ONLINE, &arg);
926+
mem_hotplug_done();
924927
return ret;
925928
}
926929
#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
@@ -1125,20 +1128,20 @@ int __ref add_memory_resource(int nid, struct resource *res, bool online)
11251128
/* create new memmap entry */
11261129
firmware_map_add_hotplug(start, start + size, "System RAM");
11271130

1131+
/* device_online() will take the lock when calling online_pages() */
1132+
mem_hotplug_done();
1133+
11281134
/* online pages if requested */
11291135
if (online)
11301136
walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
11311137
NULL, online_memory_block);
11321138

1133-
goto out;
1134-
1139+
return ret;
11351140
error:
11361141
/* rollback pgdat allocation and others */
11371142
if (new_node)
11381143
rollback_node_hotadd(nid);
11391144
memblock_remove(start, size);
1140-
1141-
out:
11421145
mem_hotplug_done();
11431146
return ret;
11441147
}
@@ -1555,10 +1558,16 @@ static int __ref __offline_pages(unsigned long start_pfn,
15551558
return -EINVAL;
15561559
if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
15571560
return -EINVAL;
1561+
1562+
mem_hotplug_begin();
1563+
15581564
/* This makes hotplug much easier...and readable.
15591565
we assume this for now. .*/
1560-
if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
1566+
if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start,
1567+
&valid_end)) {
1568+
mem_hotplug_done();
15611569
return -EINVAL;
1570+
}
15621571

15631572
zone = page_zone(pfn_to_page(valid_start));
15641573
node = zone_to_nid(zone);
@@ -1567,8 +1576,10 @@ static int __ref __offline_pages(unsigned long start_pfn,
15671576
/* set above range as isolated */
15681577
ret = start_isolate_page_range(start_pfn, end_pfn,
15691578
MIGRATE_MOVABLE, true);
1570-
if (ret)
1579+
if (ret) {
1580+
mem_hotplug_done();
15711581
return ret;
1582+
}
15721583

15731584
arg.start_pfn = start_pfn;
15741585
arg.nr_pages = nr_pages;
@@ -1639,6 +1650,7 @@ static int __ref __offline_pages(unsigned long start_pfn,
16391650
writeback_set_ratelimit();
16401651

16411652
memory_notify(MEM_OFFLINE, &arg);
1653+
mem_hotplug_done();
16421654
return 0;
16431655

16441656
failed_removal:
@@ -1648,10 +1660,10 @@ static int __ref __offline_pages(unsigned long start_pfn,
16481660
memory_notify(MEM_CANCEL_OFFLINE, &arg);
16491661
/* pushback to free area */
16501662
undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1663+
mem_hotplug_done();
16511664
return ret;
16521665
}
16531666

1654-
/* Must be protected by mem_hotplug_begin() or a device_lock */
16551667
int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
16561668
{
16571669
return __offline_pages(start_pfn, start_pfn + nr_pages);

0 commit comments

Comments
 (0)