linux-stable/arch
David Hildenbrand a3cf10bf73 mm/memory_hotplug: make remove_memory() take the device_hotplug_lock
commit d15e59260f upstream.

Patch series "mm: online/offline_pages called w.o. mem_hotplug_lock", v3.

Reading through the code and studying how mem_hotplug_lock is to be used,
I noticed that there are two places where we can end up calling
device_online()/device_offline() - online_pages()/offline_pages() without
the mem_hotplug_lock.  And there are other places where we call
device_online()/device_offline() without the device_hotplug_lock.

While e.g.
	echo "online" > /sys/devices/system/memory/memory9/state
is fine, e.g.
	echo 1 > /sys/devices/system/memory/memory9/online
Will not take the mem_hotplug_lock. However the device_lock() and
device_hotplug_lock.

E.g.  via memory_probe_store(), we can end up calling
add_memory()->online_pages() without the device_hotplug_lock.  So we can
have concurrent callers in online_pages().  We e.g.  touch in
online_pages() basically unprotected zone->present_pages then.

Looks like there is a longer history to that (see Patch #2 for details),
and fixing it to work the way it was intended is not really possible.  We
would e.g.  have to take the mem_hotplug_lock in device/base/core.c, which
sounds wrong.

Summary: We had a lock inversion on mem_hotplug_lock and device_lock().
More details can be found in patch 3 and patch 6.

I propose the general rules (documentation added in patch 6):

1. add_memory/add_memory_resource() must only be called with
   device_hotplug_lock.
2. remove_memory() must only be called with device_hotplug_lock. This is
   already documented and holds for all callers.
3. device_online()/device_offline() must only be called with
   device_hotplug_lock. This is already documented and true for now in core
   code. Other callers (related to memory hotplug) have to be fixed up.
4. mem_hotplug_lock is taken inside of add_memory/remove_memory/
   online_pages/offline_pages.

To me, this looks way cleaner than what we have right now (and easier to
verify).  And looking at the documentation of remove_memory, using
lock_device_hotplug also for add_memory() feels natural.

This patch (of 6):

remove_memory() is exported right now but requires the
device_hotplug_lock, which is not exported.  So let's provide a variant
that takes the lock and only export that one.

The lock is already held in
	arch/powerpc/platforms/pseries/hotplug-memory.c
	drivers/acpi/acpi_memhotplug.c
	arch/powerpc/platforms/powernv/memtrace.c

Apart from that, there are not other users in the tree.

Link: http://lkml.kernel.org/r/20180925091457.28651-2-david@redhat.com
Signed-off-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Pavel Tatashin <pavel.tatashin@microsoft.com>
Reviewed-by: Rafael J. Wysocki <rafael.j.wysocki@intel.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: Rashmica Gupta <rashmica.g@gmail.com>
Cc: Michael Neuling <mikey@neuling.org>
Cc: Balbir Singh <bsingharora@gmail.com>
Cc: Nathan Fontenot <nfont@linux.vnet.ibm.com>
Cc: John Allen <jallen@linux.vnet.ibm.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: YASUAKI ISHIMATSU <yasu.isimatu@gmail.com>
Cc: Mathieu Malaterre <malat@debian.org>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Juergen Gross <jgross@suse.com>
Cc: Kate Stewart <kstewart@linuxfoundation.org>
Cc: "K. Y. Srinivasan" <kys@microsoft.com>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Philippe Ombredanne <pombredanne@nexb.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-01-29 16:43:24 +01:00
..
alpha
arc ARC: IOC: panic if kernel was started with previously enabled IOC 2019-12-13 08:51:12 +01:00
arm hwrng: omap3-rom - Fix missing clock by probing with device tree 2020-01-27 14:51:20 +01:00
arm64 arm64: dts: meson-gxm-khadas-vim2: fix uart_A bluetooth node 2020-01-27 14:51:23 +01:00
c6x
h8300 h8300: use cc-cross-prefix instead of hardcoding h8300-unknown-linux- 2019-04-05 22:32:55 +02:00
hexagon hexagon: work around compiler crash 2020-01-17 19:47:17 +01:00
ia64 signal/ia64: Use the force_sig(SIGSEGV,...) in ia64_rt_sigreturn 2020-01-27 14:49:56 +01:00
m68k m68k: Call timer_interrupt() with interrupts disabled 2020-01-27 14:51:23 +01:00
microblaze microblaze: fix multiple bugs in arch/microblaze/boot/Makefile 2019-12-05 09:20:13 +01:00
mips mips: avoid explicit UB in assignment of mips_io_port_base 2020-01-27 14:51:07 +01:00
nds32 nds32: Fix the items of hwcap_str ordering issue. 2019-12-13 08:51:35 +01:00
nios2 nios2: ksyms: Add missing symbol exports 2020-01-27 14:50:30 +01:00
openrisc openrisc: Fix broken paths to arch/or32 2019-12-05 09:20:40 +01:00
parisc parisc: Fix compiler warnings in debug_core.c 2020-01-12 12:17:22 +01:00
powerpc mm/memory_hotplug: make remove_memory() take the device_hotplug_lock 2020-01-29 16:43:24 +01:00
riscv riscv: ftrace: correct the condition logic in function graph tracer 2020-01-09 10:19:02 +01:00
s390 s390/kexec_file: Fix potential segment overlap in ELF loader 2020-01-27 14:50:42 +01:00
sh media: sh: migor: Include missing dma-mapping header 2020-01-27 14:50:24 +01:00
sparc sparc: Correct ctx->saw_frame_pointer logic. 2019-12-13 08:51:52 +01:00
um um: Fix off by one error in IRQ enumeration 2020-01-27 14:51:13 +01:00
unicore32
x86 x86, perf: Fix the dependency of the x86 insn decoder selftest 2020-01-27 14:51:08 +01:00
xtensa xtensa: fix TLB sanity checker 2019-12-21 10:57:25 +01:00
.gitignore
Kconfig jump_label: move 'asm goto' support test to Kconfig 2019-06-04 08:02:34 +02:00