Skip to content

Commit 55adc1d

Browse files
heicarsttorvalds
authored andcommitted
mm: add private lock to serialize memory hotplug operations
Commit bfc8c90 ("mem-hotplug: implement get/put_online_mems") introduced new functions get/put_online_mems() and mem_hotplug_begin/end() in order to allow similar semantics for memory hotplug like for cpu hotplug. The corresponding functions for cpu hotplug are get/put_online_cpus() and cpu_hotplug_begin/done() for cpu hotplug. The commit however missed to introduce functions that would serialize memory hotplug operations like they are done for cpu hotplug with cpu_maps_update_begin/done(). This basically leaves mem_hotplug.active_writer unprotected and allows concurrent writers to modify it, which may lead to problems as outlined by commit f931ab4 ("mm: fix devm_memremap_pages crash, use mem_hotplug_{begin, done}"). That commit was extended again with commit b5d24fd ("mm, devm_memremap_pages: hold device_hotplug lock over mem_hotplug_{begin, done}") which serializes memory hotplug operations for some call sites by using the device_hotplug lock. In addition with commit 3fc2192 ("mm: validate device_hotplug is held for memory hotplug") a sanity check was added to mem_hotplug_begin() to verify that the device_hotplug lock is held. This in turn triggers the following warning on s390: WARNING: CPU: 6 PID: 1 at drivers/base/core.c:643 assert_held_device_hotplug+0x4a/0x58 Call Trace: assert_held_device_hotplug+0x40/0x58) mem_hotplug_begin+0x34/0xc8 add_memory_resource+0x7e/0x1f8 add_memory+0xda/0x130 add_memory_merged+0x15c/0x178 sclp_detect_standby_memory+0x2ae/0x2f8 do_one_initcall+0xa2/0x150 kernel_init_freeable+0x228/0x2d8 kernel_init+0x2a/0x140 kernel_thread_starter+0x6/0xc One possible fix would be to add more lock_device_hotplug() and unlock_device_hotplug() calls around each call site of mem_hotplug_begin/end(). But that would give the device_hotplug lock additional semantics it better should not have (serialize memory hotplug operations). Instead add a new memory_add_remove_lock which has the similar semantics like cpu_add_remove_lock for cpu hotplug. To keep things hopefully a bit easier the lock will be locked and unlocked within the mem_hotplug_begin/end() functions. Link: http://lkml.kernel.org/r/20170314125226.16779-2-heiko.carstens@de.ibm.com Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com> Reported-by: Sebastian Ott <sebott@linux.vnet.ibm.com> Acked-by: Dan Williams <dan.j.williams@intel.com> Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Vladimir Davydov <vdavydov.dev@gmail.com> Cc: Ben Hutchings <ben@decadent.org.uk> Cc: Gerald Schaefer <gerald.schaefer@de.ibm.com> Cc: Martin Schwidefsky <schwidefsky@de.ibm.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 171012f commit 55adc1d

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

‎kernel/memremap.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,9 @@ static void devm_memremap_pages_release(struct device *dev, void *data)
247247
align_start = res->start & ~(SECTION_SIZE - 1);
248248
align_size = ALIGN(resource_size(res), SECTION_SIZE);
249249

250-
lock_device_hotplug();
251250
mem_hotplug_begin();
252251
arch_remove_memory(align_start, align_size);
253252
mem_hotplug_done();
254-
unlock_device_hotplug();
255253

256254
untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
257255
pgmap_radix_release(res);
@@ -364,11 +362,9 @@ void *devm_memremap_pages(struct device *dev, struct resource *res,
364362
if (error)
365363
goto err_pfn_remap;
366364

367-
lock_device_hotplug();
368365
mem_hotplug_begin();
369366
error = arch_add_memory(nid, align_start, align_size, true);
370367
mem_hotplug_done();
371-
unlock_device_hotplug();
372368
if (error)
373369
goto err_add_memory;
374370

‎mm/memory_hotplug.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,12 @@ void put_online_mems(void)
125125

126126
}
127127

128+
/* Serializes write accesses to mem_hotplug.active_writer. */
129+
static DEFINE_MUTEX(memory_add_remove_lock);
130+
128131
void mem_hotplug_begin(void)
129132
{
130-
assert_held_device_hotplug();
133+
mutex_lock(&memory_add_remove_lock);
131134

132135
mem_hotplug.active_writer = current;
133136

@@ -147,6 +150,7 @@ void mem_hotplug_done(void)
147150
mem_hotplug.active_writer = NULL;
148151
mutex_unlock(&mem_hotplug.lock);
149152
memhp_lock_release();
153+
mutex_unlock(&memory_add_remove_lock);
150154
}
151155

152156
/* add this memory to iomem resource */

0 commit comments

Comments
 (0)