Skip to contentSkip to conversation

docs: memory-hotplug: add details about locking internals

PATCH Applied Mike Rapoport 3 participants 7 messages Oct 11, 2018 – Dec 3, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover docs: memory-hotplug: add details about locking internals

Hi,

As discussed at [1], the latest updates to memory hotplug documentation are causing a conflict between docs and mmotm trees.
These patches resolve the conflict.

[1] https://lkml.org/lkml/2018/10/8/227

David Hildenbrand (1):
docs/core-api: memory-hotplug: add some details about locking internals

Mike Rapoport (1):
docs/core-api: rename memory-hotplug-notifier to memory-hotplug

 Documentation/core-api/index.rst                   |   2 +-
 Documentation/core-api/memory-hotplug-notifier.rst |  84 --------------
 Documentation/core-api/memory-hotplug.rst          | 125 +++++++++++++++++++++
 3 files changed, 126 insertions(+), 85 deletions(-)
 delete mode 100644 Documentation/core-api/memory-hotplug-notifier.rst
 create mode 100644 Documentation/core-api/memory-hotplug.rst
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/2 docs/core-api: rename memory-hotplug-notifier to memory-hotplug

From: Mike Rapoport <[email protected]>

to allow additions of new documentation about memory hotplug under the same roof.

---
 Documentation/core-api/index.rst                   |  2 +-
 Documentation/core-api/memory-hotplug-notifier.rst | 84 ---------------------
 Documentation/core-api/memory-hotplug.rst          | 87 ++++++++++++++++++++++
 3 files changed, 88 insertions(+), 85 deletions(-)
 delete mode 100644 Documentation/core-api/memory-hotplug-notifier.rst
 create mode 100644 Documentation/core-api/memory-hotplug.rst
Documentation/core-api/index.rst +1 -1
index 4f8a426..29c790f 100644
@@ -32,7 +32,7 @@ Core utilities
    gfp_mask-from-fs-io
    timekeeping
    boot-time-mm
-   memory-hotplug-notifier
+   memory-hotplug
 
 
 Interfaces for kernel debugging
Documentation/core-api/memory-hotplug-notifier.rst deleted +0 -84

84 lines not on this page · raw message

Documentation/core-api/memory-hotplug.rst new file +87 -0

87 lines not on this page · raw message

Signature · 1 line
2.7.4
#3 lore raw

PATCH 2/2 docs/core-api: memory-hotplug: add some details about locking internals

In reply to Mike Rapoport

From: David Hildenbrand <[email protected]>

Let's document the magic a bit, especially why device_hotplug_lock is required when adding/removing memory and how it all play together with requests to online/offline memory from user space.

[ rppt: moved the text to Documentation/core-api/memory-hotplug.rst ]

Cc Jonathan Corbet <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Balbir Singh <[email protected]>
Cc Benjamin Herrenschmidt <[email protected]>
Cc Boris Ostrovsky <[email protected]>
Cc Dan Williams <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Haiyang Zhang <[email protected]>
Cc Heiko Carstens <[email protected]>
Cc John Allen <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Kate Stewart <[email protected]>
Cc "K. Y. Srinivasan" <[email protected]>
Cc Len Brown <[email protected]>
Cc Martin Schwidefsky <[email protected]>
Cc Mathieu Malaterre <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Michael Neuling <[email protected]>
Cc Nathan Fontenot <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Philippe Ombredanne <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Stephen Hemminger <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc YASUAKI ISHIMATSU <[email protected]>
---
 Documentation/core-api/memory-hotplug.rst | 38 +++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
Documentation/core-api/memory-hotplug.rst +38 -0
index a99f2f2..de7467e 100644
@@ -85,3 +85,41 @@ MEM_ONLINE, or MEM_OFFLINE action to cancel hotplugging. It stops
 further processing of the notification queue.
 
 NOTIFY_STOP stops further processing of the notification queue.
+
+Locking Internals
+=================
+
+When adding/removing memory that uses memory block devices (i.e. ordinary RAM),
+the device_hotplug_lock should be held to:
+
+- synchronize against online/offline requests (e.g. via sysfs). This way, memory
+  block devices can only be accessed (.online/.state attributes) by user
+  space once memory has been fully added. And when removing memory, we
+  know nobody is in critical sections.
+- synchronize against CPU hotplug and similar (e.g. relevant for ACPI and PPC)
+
+Especially, there is a possible lock inversion that is avoided using
+device_hotplug_lock when adding memory and user space tries to online that
+memory faster than expected:
+
+- device_online() will first take the device_lock(), followed by
+  mem_hotplug_lock
+- add_memory_resource() will first take the mem_hotplug_lock, followed by
+  the device_lock() (while creating the devices, during bus_add_device()).
+
+As the device is visible to user space before taking the device_lock(), this
+can result in a lock inversion.
+
+onlining/offlining of memory should be done via device_online()/
+device_offline() - to make sure it is properly synchronized to actions
+via sysfs. Holding device_hotplug_lock is advised (to e.g. protect online_type)
+
+When adding/removing/onlining/offlining memory or adding/removing
+heterogeneous/device memory, we should always hold the mem_hotplug_lock in
+write mode to serialise memory hotplug (e.g. access to global/zone
+variables).
+
+In addition, mem_hotplug_lock (in contrast to device_hotplug_lock) in read
+mode allows for a quite efficient get_online_mems/put_online_mems
+implementation, so code accessing memory can protect from that memory
+vanishing.
Signature · 1 line
2.7.4
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] docs/core-api: rename memory-hotplug-notifier to memory-hotplug

In reply to Mike Rapoport
Quoted 201 lines · Mike Rapoport

From: Mike Rapoport <[email protected]>

to allow additions of new documentation about memory hotplug under the same roof.

---
 Documentation/core-api/index.rst                   |  2 +-
 Documentation/core-api/memory-hotplug-notifier.rst | 84 ---------------------
 Documentation/core-api/memory-hotplug.rst          | 87 ++++++++++++++++++++++
 3 files changed, 88 insertions(+), 85 deletions(-)
 delete mode 100644 Documentation/core-api/memory-hotplug-notifier.rst
 create mode 100644 Documentation/core-api/memory-hotplug.rst
Documentation/core-api/index.rst +1 -1
index 4f8a426..29c790f 100644
@@ -32,7 +32,7 @@ Core utilities
    gfp_mask-from-fs-io
    timekeeping
    boot-time-mm
-   memory-hotplug-notifier
+   memory-hotplug
 
 
 Interfaces for kernel debugging
Documentation/core-api/memory-hotplug-notifier.rst deleted +0 -84
deleted file mode 100644
index 35347cc..0000000
@@ -1,84 +0,0 @@
-.. _memory_hotplug_notifier:
-
-=============================
-Memory hotplug event notifier
-=============================
-
-Hotplugging events are sent to a notification queue.
-
-There are six types of notification defined in ``include/linux/memory.h``:
-
-MEM_GOING_ONLINE
-  Generated before new memory becomes available in order to be able to
-  prepare subsystems to handle memory. The page allocator is still unable
-  to allocate from the new memory.
-
-MEM_CANCEL_ONLINE
-  Generated if MEM_GOING_ONLINE fails.
-
-MEM_ONLINE
-  Generated when memory has successfully brought online. The callback may
-  allocate pages from the new memory.
-
-MEM_GOING_OFFLINE
-  Generated to begin the process of offlining memory. Allocations are no
-  longer possible from the memory but some of the memory to be offlined
-  is still in use. The callback can be used to free memory known to a
-  subsystem from the indicated memory block.
-
-MEM_CANCEL_OFFLINE
-  Generated if MEM_GOING_OFFLINE fails. Memory is available again from
-  the memory block that we attempted to offline.
-
-MEM_OFFLINE
-  Generated after offlining memory is complete.
-
-A callback routine can be registered by calling::
-
-  hotplug_memory_notifier(callback_func, priority)
-
-Callback functions with higher values of priority are called before callback
-functions with lower values.
-
-A callback function must have the following prototype::
-
-  int callback_func(
-    struct notifier_block *self, unsigned long action, void *arg);
-
-The first argument of the callback function (self) is a pointer to the block
-of the notifier chain that points to the callback function itself.
-The second argument (action) is one of the event types described above.
-The third argument (arg) passes a pointer of struct memory_notify::
-
-	struct memory_notify {
-		unsigned long start_pfn;
-		unsigned long nr_pages;
-		int status_change_nid_normal;
-		int status_change_nid_high;
-		int status_change_nid;
-	}
-
-- start_pfn is start_pfn of online/offline memory.
-- nr_pages is # of pages of online/offline memory.
-- status_change_nid_normal is set node id when N_NORMAL_MEMORY of nodemask
-  is (will be) set/clear, if this is -1, then nodemask status is not changed.
-- status_change_nid_high is set node id when N_HIGH_MEMORY of nodemask
-  is (will be) set/clear, if this is -1, then nodemask status is not changed.
-- status_change_nid is set node id when N_MEMORY of nodemask is (will be)
-  set/clear. It means a new(memoryless) node gets new memory by online and a
-  node loses all memory. If this is -1, then nodemask status is not changed.
-
-  If status_changed_nid* >= 0, callback should create/discard structures for the
-  node if necessary.
-
-The callback routine shall return one of the values
-NOTIFY_DONE, NOTIFY_OK, NOTIFY_BAD, NOTIFY_STOP
-defined in ``include/linux/notifier.h``
-
-NOTIFY_DONE and NOTIFY_OK have no effect on the further processing.
-
-NOTIFY_BAD is used as response to the MEM_GOING_ONLINE, MEM_GOING_OFFLINE,
-MEM_ONLINE, or MEM_OFFLINE action to cancel hotplugging. It stops
-further processing of the notification queue.
-
-NOTIFY_STOP stops further processing of the notification queue.
Documentation/core-api/memory-hotplug.rst new file +77 -0
new file mode 100644
index 0000000..a99f2f2
@@ -0,0 +1,87 @@
+.. _memory_hotplug:
+
+==============
+Memory hotplug
+==============
+
+Memory hotplug event notifier
+=============================
+
+Hotplugging events are sent to a notification queue.
+
+There are six types of notification defined in ``include/linux/memory.h``:
+
+MEM_GOING_ONLINE
+  Generated before new memory becomes available in order to be able to
+  prepare subsystems to handle memory. The page allocator is still unable
+  to allocate from the new memory.
+
+MEM_CANCEL_ONLINE
+  Generated if MEM_GOING_ONLINE fails.
+
+MEM_ONLINE
+  Generated when memory has successfully brought online. The callback may
+  allocate pages from the new memory.
+
+MEM_GOING_OFFLINE
+  Generated to begin the process of offlining memory. Allocations are no
+  longer possible from the memory but some of the memory to be offlined
+  is still in use. The callback can be used to free memory known to a
+  subsystem from the indicated memory block.
+
+MEM_CANCEL_OFFLINE
+  Generated if MEM_GOING_OFFLINE fails. Memory is available again from
+  the memory block that we attempted to offline.
+
+MEM_OFFLINE
+  Generated after offlining memory is complete.
+
+A callback routine can be registered by calling::
+
+  hotplug_memory_notifier(callback_func, priority)
+
+Callback functions with higher values of priority are called before callback
+functions with lower values.
+
+A callback function must have the following prototype::
+
+  int callback_func(
+    struct notifier_block *self, unsigned long action, void *arg);
+
+The first argument of the callback function (self) is a pointer to the block
+of the notifier chain that points to the callback function itself.
+The second argument (action) is one of the event types described above.
+The third argument (arg) passes a pointer of struct memory_notify::
+
+	struct memory_notify {
+		unsigned long start_pfn;
+		unsigned long nr_pages;
+		int status_change_nid_normal;
+		int status_change_nid_high;
+		int status_change_nid;
+	}
+
+- start_pfn is start_pfn of online/offline memory.
+- nr_pages is # of pages of online/offline memory.
+- status_change_nid_normal is set node id when N_NORMAL_MEMORY of nodemask
+  is (will be) set/clear, if this is -1, then nodemask status is not changed.
+- status_change_nid_high is set node id when N_HIGH_MEMORY of nodemask
+  is (will be) set/clear, if this is -1, then nodemask status is not changed.
+- status_change_nid is set node id when N_MEMORY of nodemask is (will be)
+  set/clear. It means a new(memoryless) node gets new memory by online and a
+  node loses all memory. If this is -1, then nodemask status is not changed.
+
+  If status_changed_nid* >= 0, callback should create/discard structures for the
+  node if necessary.
+
+The callback routine shall return one of the values

Documentation/core-api/memory-hotplug.rst line 87

Quoted 10 lines
Documentation/core-api/memory-hotplug.rst new file +10 -0
+NOTIFY_DONE, NOTIFY_OK, NOTIFY_BAD, NOTIFY_STOP
+defined in ``include/linux/notifier.h``
+
+NOTIFY_DONE and NOTIFY_OK have no effect on the further processing.
+
+NOTIFY_BAD is used as response to the MEM_GOING_ONLINE, MEM_GOING_OFFLINE,
+MEM_ONLINE, or MEM_OFFLINE action to cancel hotplugging. It stops
+further processing of the notification queue.
+
+NOTIFY_STOP stops further processing of the notification queue.
Signature · 3 lines
Thanks,

David / dhildenb
#4 lore raw

Re: [PATCH 2/2] docs/core-api: memory-hotplug: add some details about locking internals

In reply to Mike Rapoport
Quoted 84 lines · Mike Rapoport

From: David Hildenbrand <[email protected]>

Let's document the magic a bit, especially why device_hotplug_lock is required when adding/removing memory and how it all play together with requests to online/offline memory from user space.

[ rppt: moved the text to Documentation/core-api/memory-hotplug.rst ]

Cc Jonathan Corbet <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Balbir Singh <[email protected]>
Cc Benjamin Herrenschmidt <[email protected]>
Cc Boris Ostrovsky <[email protected]>
Cc Dan Williams <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Haiyang Zhang <[email protected]>
Cc Heiko Carstens <[email protected]>
Cc John Allen <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Kate Stewart <[email protected]>
Cc "K. Y. Srinivasan" <[email protected]>
Cc Len Brown <[email protected]>
Cc Martin Schwidefsky <[email protected]>
Cc Mathieu Malaterre <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Michael Neuling <[email protected]>
Cc Nathan Fontenot <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Philippe Ombredanne <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Stephen Hemminger <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc YASUAKI ISHIMATSU <[email protected]>
---
 Documentation/core-api/memory-hotplug.rst | 38 +++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
Documentation/core-api/memory-hotplug.rst +28 -0
index a99f2f2..de7467e 100644
@@ -85,3 +85,41 @@ MEM_ONLINE, or MEM_OFFLINE action to cancel hotplugging. It stops
 further processing of the notification queue.
 
 NOTIFY_STOP stops further processing of the notification queue.
+
+Locking Internals
+=================
+
+When adding/removing memory that uses memory block devices (i.e. ordinary RAM),
+the device_hotplug_lock should be held to:
+
+- synchronize against online/offline requests (e.g. via sysfs). This way, memory
+  block devices can only be accessed (.online/.state attributes) by user
+  space once memory has been fully added. And when removing memory, we
+  know nobody is in critical sections.
+- synchronize against CPU hotplug and similar (e.g. relevant for ACPI and PPC)
+
+Especially, there is a possible lock inversion that is avoided using
+device_hotplug_lock when adding memory and user space tries to online that
+memory faster than expected:
+
+- device_online() will first take the device_lock(), followed by
+  mem_hotplug_lock
+- add_memory_resource() will first take the mem_hotplug_lock, followed by
+  the device_lock() (while creating the devices, during bus_add_device()).
+
+As the device is visible to user space before taking the device_lock(), this
+can result in a lock inversion.
+
+onlining/offlining of memory should be done via device_online()/
+device_offline() - to make sure it is properly synchronized to actions
+via sysfs. Holding device_hotplug_lock is advised (to e.g. protect online_type)

Documentation/core-api/memory-hotplug.rst MEM_ONLINE, or MEM_OFFLINE action to cancel hotplugging. It stops · line 125

Quoted 10 lines
Documentation/core-api/memory-hotplug.rst +10 -0
+
+When adding/removing/onlining/offlining memory or adding/removing
+heterogeneous/device memory, we should always hold the mem_hotplug_lock in
+write mode to serialise memory hotplug (e.g. access to global/zone
+variables).
+
+In addition, mem_hotplug_lock (in contrast to device_hotplug_lock) in read
+mode allows for a quite efficient get_online_mems/put_online_mems
+implementation, so code accessing memory can protect from that memory
+vanishing.

Looks good to me.

Signature · 3 lines
Thanks,

David / dhildenb
maintainer Applied
#5 lore raw
In reply to Mike Rapoport
Quoted 11 lines · On Thu, 11 Oct 2018 07:58:15 +0300 Mike Rapoport <[email protected]> wrote:

As discussed at [1], the latest updates to memory hotplug documentation are causing a conflict between docs and mmotm trees.
These patches resolve the conflict.

[1] https://lkml.org/lkml/2018/10/8/227

David Hildenbrand (1):
docs/core-api: memory-hotplug: add some details about locking internals

Mike Rapoport (1):
docs/core-api: rename memory-hotplug-notifier to memory-hotplug

I've applied the pair, thanks.

jon

#6 lore raw
Quoted 12 lines · Jonathan Corbet
Quoted 9 lines · On Thu, 11 Oct 2018 07:58:15 +0300 Mike Rapoport <[email protected]> wrote:

As discussed at [1], the latest updates to memory hotplug documentation are causing a conflict between docs and mmotm trees.
These patches resolve the conflict.

[1] https://lkml.org/lkml/2018/10/8/227

David Hildenbrand (1):
docs/core-api: memory-hotplug: add some details about locking internals

Quoted 7 lines
Quoted 2 lines

Mike Rapoport (1):
docs/core-api: rename memory-hotplug-notifier to memory-hotplug

I've applied the pair, thanks.

jon

Looking at linux-next, we now have duplicate documentation:

$ git grep mem_hotplug_lock
Documentation/admin-guide/mm/memory-hotplug.rst:  mem_hotplug_lock
Documentation/admin-guide/mm/memory-hotplug.rst:- add_memory_resource()
will first take the mem_hotplug_lock, followed by
Documentation/admin-guide/mm/memory-hotplug.rst:heterogeneous/device
memory, we should always hold the mem_hotplug_lock in
Documentation/admin-guide/mm/memory-hotplug.rst:In addition,
mem_hotplug_lock (in contrast to device_hotplug_lock) in read

Documentation/core-api/memory-hotplug.rst: mem_hotplug_lock Documentation/core-api/memory-hotplug.rst:- add_memory_resource() will first take the mem_hotplug_lock, followed by Documentation/core-api/memory-hotplug.rst:heterogeneous/device memory, we should always hold the mem_hotplug_lock in Documentation/core-api/memory-hotplug.rst:In addition, mem_hotplug_lock (in contrast to device_hotplug_lock) in read

It really only should go to Documentation/core-api/memory-hotplug.rst

Should I send a patch or who can fix that up?

Signature · 3 lines
Thanks,

David / dhildenb