Skip to content

Commit 3f906ba

Browse files
KAGA-KOKOtorvalds
authored andcommitted
mm/memory-hotplug: switch locking to a percpu rwsem
Andrey reported a potential deadlock with the memory hotplug lock and the cpu hotplug lock. The reason is that memory hotplug takes the memory hotplug lock and then calls stop_machine() which calls get_online_cpus(). That's the reverse lock order to get_online_cpus(); get_online_mems(); in mm/slub_common.c The problem has been there forever. The reason why this was never reported is that the cpu hotplug locking had this homebrewn recursive reader writer semaphore construct which due to the recursion evaded the full lock dep coverage. The memory hotplug code copied that construct verbatim and therefor has similar issues. Three steps to fix this: 1) Convert the memory hotplug locking to a per cpu rwsem so the potential issues get reported proper by lockdep. 2) Lock the online cpus in mem_hotplug_begin() before taking the memory hotplug rwsem and use stop_machine_cpuslocked() in the page_alloc code to avoid recursive locking. 3) The cpu hotpluck locking in #2 causes a recursive locking of the cpu hotplug lock via __offline_pages() -> lru_add_drain_all(). Solve this by invoking lru_add_drain_all_cpuslocked() instead. Link: http://lkml.kernel.org/r/[email protected] Reported-by: Andrey Ryabinin <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Acked-by: Michal Hocko <[email protected]> Acked-by: Vlastimil Babka <[email protected]> Cc: Vladimir Davydov <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Davidlohr Bueso <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent a47fed5 commit 3f906ba

File tree

2 files changed

+16
-75
lines changed

2 files changed

+16
-75
lines changed

mm/memory_hotplug.c

Lines changed: 15 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,17 @@ static void generic_online_page(struct page *page);
5252
static online_page_callback_t online_page_callback = generic_online_page;
5353
static DEFINE_MUTEX(online_page_callback_lock);
5454

55-
/* The same as the cpu_hotplug lock, but for memory hotplug. */
56-
static struct {
57-
struct task_struct *active_writer;
58-
struct mutex lock; /* Synchronizes accesses to refcount, */
59-
/*
60-
* Also blocks the new readers during
61-
* an ongoing mem hotplug operation.
62-
*/
63-
int refcount;
55+
DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock);
6456

65-
#ifdef CONFIG_DEBUG_LOCK_ALLOC
66-
struct lockdep_map dep_map;
67-
#endif
68-
} mem_hotplug = {
69-
.active_writer = NULL,
70-
.lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
71-
.refcount = 0,
72-
#ifdef CONFIG_DEBUG_LOCK_ALLOC
73-
.dep_map = {.name = "mem_hotplug.lock" },
74-
#endif
75-
};
57+
void get_online_mems(void)
58+
{
59+
percpu_down_read(&mem_hotplug_lock);
60+
}
7661

77-
/* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
78-
#define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
79-
#define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
80-
#define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
62+
void put_online_mems(void)
63+
{
64+
percpu_up_read(&mem_hotplug_lock);
65+
}
8166

8267
bool movable_node_enabled = false;
8368

@@ -99,60 +84,16 @@ static int __init setup_memhp_default_state(char *str)
9984
}
10085
__setup("memhp_default_state=", setup_memhp_default_state);
10186

102-
void get_online_mems(void)
103-
{
104-
might_sleep();
105-
if (mem_hotplug.active_writer == current)
106-
return;
107-
memhp_lock_acquire_read();
108-
mutex_lock(&mem_hotplug.lock);
109-
mem_hotplug.refcount++;
110-
mutex_unlock(&mem_hotplug.lock);
111-
112-
}
113-
114-
void put_online_mems(void)
115-
{
116-
if (mem_hotplug.active_writer == current)
117-
return;
118-
mutex_lock(&mem_hotplug.lock);
119-
120-
if (WARN_ON(!mem_hotplug.refcount))
121-
mem_hotplug.refcount++; /* try to fix things up */
122-
123-
if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
124-
wake_up_process(mem_hotplug.active_writer);
125-
mutex_unlock(&mem_hotplug.lock);
126-
memhp_lock_release();
127-
128-
}
129-
130-
/* Serializes write accesses to mem_hotplug.active_writer. */
131-
static DEFINE_MUTEX(memory_add_remove_lock);
132-
13387
void mem_hotplug_begin(void)
13488
{
135-
mutex_lock(&memory_add_remove_lock);
136-
137-
mem_hotplug.active_writer = current;
138-
139-
memhp_lock_acquire();
140-
for (;;) {
141-
mutex_lock(&mem_hotplug.lock);
142-
if (likely(!mem_hotplug.refcount))
143-
break;
144-
__set_current_state(TASK_UNINTERRUPTIBLE);
145-
mutex_unlock(&mem_hotplug.lock);
146-
schedule();
147-
}
89+
cpus_read_lock();
90+
percpu_down_write(&mem_hotplug_lock);
14891
}
14992

15093
void mem_hotplug_done(void)
15194
{
152-
mem_hotplug.active_writer = NULL;
153-
mutex_unlock(&mem_hotplug.lock);
154-
memhp_lock_release();
155-
mutex_unlock(&memory_add_remove_lock);
95+
percpu_up_write(&mem_hotplug_lock);
96+
cpus_read_unlock();
15697
}
15798

15899
/* add this memory to iomem resource */
@@ -1725,7 +1666,7 @@ static int __ref __offline_pages(unsigned long start_pfn,
17251666
goto failed_removal;
17261667
ret = 0;
17271668
if (drain) {
1728-
lru_add_drain_all();
1669+
lru_add_drain_all_cpuslocked();
17291670
cond_resched();
17301671
drain_all_pages(zone);
17311672
}
@@ -1746,7 +1687,7 @@ static int __ref __offline_pages(unsigned long start_pfn,
17461687
}
17471688
}
17481689
/* drain all zone's lru pagevec, this is asynchronous... */
1749-
lru_add_drain_all();
1690+
lru_add_drain_all_cpuslocked();
17501691
yield();
17511692
/* drain pcp pages, this is synchronous. */
17521693
drain_all_pages(zone);

mm/page_alloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5278,7 +5278,7 @@ void __ref build_all_zonelists(pg_data_t *pgdat, struct zone *zone)
52785278
#endif
52795279
/* we have to stop all cpus to guarantee there is no user
52805280
of zonelist */
5281-
stop_machine(__build_all_zonelists, pgdat, NULL);
5281+
stop_machine_cpuslocked(__build_all_zonelists, pgdat, NULL);
52825282
/* cpuset refresh routine should be here */
52835283
}
52845284
vm_total_pages = nr_free_pagecache_pages();

0 commit comments

Comments
 (0)