mm/memory_hotplug: create memory block devices after arch_add_memory()

Only memory to be added to the buddy and to be onlined/offlined by user
space using /sys/devices/system/memory/...  needs (and should have!)
memory block devices.

Factor out creation of memory block devices.  Create all devices after
arch_add_memory() succeeded.  We can later drop the want_memblock
parameter, because it is now effectively stale.

Only after memory block devices have been added, memory can be onlined
by user space.  This implies, that memory is not visible to user space
at all before arch_add_memory() succeeded.

While at it
 - use WARN_ON_ONCE instead of BUG_ON in moved unregister_memory()
 - introduce find_memory_block_by_id() to search via block id
 - Use find_memory_block_by_id() in init_memory_block() to catch

Signed-off-by: David Hildenbrand <>
Reviewed-by: Pavel Tatashin <>
Acked-by: Michal Hocko <>
Cc: Greg Kroah-Hartman <>
Cc: "Rafael J. Wysocki" <>
Cc: David Hildenbrand <>
Cc: "" <>
Cc: Ingo Molnar <>
Cc: Andrew Banman <>
Cc: Oscar Salvador <>
Cc: Qian Cai <>
Cc: Wei Yang <>
Cc: Arun KS <>
Cc: Mathieu Malaterre <>
Cc: Alex Deucher <>
Cc: Andy Lutomirski <>
Cc: Anshuman Khandual <>
Cc: Ard Biesheuvel <>
Cc: Baoquan He <>
Cc: Benjamin Herrenschmidt <>
Cc: Borislav Petkov <>
Cc: Catalin Marinas <>
Cc: Chintan Pandya <>
Cc: Christophe Leroy <>
Cc: Chris Wilson <>
Cc: Dan Williams <>
Cc: Dave Hansen <>
Cc: "David S. Miller" <>
Cc: Fenghua Yu <>
Cc: Heiko Carstens <>
Cc: "H. Peter Anvin" <>
Cc: Jonathan Cameron <>
Cc: Joonsoo Kim <>
Cc: Jun Yao <>
Cc: "Kirill A. Shutemov" <>
Cc: Logan Gunthorpe <>
Cc: Mark Brown <>
Cc: Mark Rutland <>
Cc: Masahiro Yamada <>
Cc: Michael Ellerman <>
Cc: Mike Rapoport <>
Cc: Nicholas Piggin <>
Cc: Oscar Salvador <>
Cc: Paul Mackerras <>
Cc: Peter Zijlstra <>
Cc: Rich Felker <>
Cc: Rob Herring <>
Cc: Robin Murphy <>
Cc: Thomas Gleixner <>
Cc: Tony Luck <>
Cc: Vasily Gorbik <>
Cc: Will Deacon <>
Cc: Yoshinori Sato <>
Cc: Yu Zhao <>
Signed-off-by: Andrew Morton <>
Signed-off-by: Linus Torvalds <>
3 files changed