Skip to content

Commit 6710e59

Browse files
committed
percpu: fix synchronization between synchronous map extension and chunk destruction
For non-atomic allocations, pcpu_alloc() can try to extend the area map synchronously after dropping pcpu_lock; however, the extension wasn't synchronized against chunk destruction and the chunk might get freed while extension is in progress. This patch fixes the bug by putting most of non-atomic allocations under pcpu_alloc_mutex to synchronize against pcpu_balance_work which is responsible for async chunk management including destruction. Signed-off-by: Tejun Heo <[email protected]> Reported-and-tested-by: Alexei Starovoitov <[email protected]> Reported-by: Vlastimil Babka <[email protected]> Reported-by: Sasha Levin <[email protected]> Cc: [email protected] # v3.18+ Fixes: 1a4d760 ("percpu: implement asynchronous chunk population")
1 parent 4f996e2 commit 6710e59

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

mm/percpu.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static struct pcpu_chunk *pcpu_reserved_chunk;
162162
static int pcpu_reserved_chunk_limit;
163163

164164
static DEFINE_SPINLOCK(pcpu_lock); /* all internal data structures */
165-
static DEFINE_MUTEX(pcpu_alloc_mutex); /* chunk create/destroy, [de]pop */
165+
static DEFINE_MUTEX(pcpu_alloc_mutex); /* chunk create/destroy, [de]pop, map ext */
166166

167167
static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */
168168

@@ -444,6 +444,8 @@ static int pcpu_extend_area_map(struct pcpu_chunk *chunk, int new_alloc)
444444
size_t old_size = 0, new_size = new_alloc * sizeof(new[0]);
445445
unsigned long flags;
446446

447+
lockdep_assert_held(&pcpu_alloc_mutex);
448+
447449
new = pcpu_mem_zalloc(new_size);
448450
if (!new)
449451
return -ENOMEM;
@@ -890,6 +892,9 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
890892
return NULL;
891893
}
892894

895+
if (!is_atomic)
896+
mutex_lock(&pcpu_alloc_mutex);
897+
893898
spin_lock_irqsave(&pcpu_lock, flags);
894899

895900
/* serve reserved allocations from the reserved chunk if available */
@@ -962,12 +967,9 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
962967
if (is_atomic)
963968
goto fail;
964969

965-
mutex_lock(&pcpu_alloc_mutex);
966-
967970
if (list_empty(&pcpu_slot[pcpu_nr_slots - 1])) {
968971
chunk = pcpu_create_chunk();
969972
if (!chunk) {
970-
mutex_unlock(&pcpu_alloc_mutex);
971973
err = "failed to allocate new chunk";
972974
goto fail;
973975
}
@@ -978,7 +980,6 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
978980
spin_lock_irqsave(&pcpu_lock, flags);
979981
}
980982

981-
mutex_unlock(&pcpu_alloc_mutex);
982983
goto restart;
983984

984985
area_found:
@@ -988,8 +989,6 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
988989
if (!is_atomic) {
989990
int page_start, page_end, rs, re;
990991

991-
mutex_lock(&pcpu_alloc_mutex);
992-
993992
page_start = PFN_DOWN(off);
994993
page_end = PFN_UP(off + size);
995994

@@ -1000,7 +999,6 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
1000999

10011000
spin_lock_irqsave(&pcpu_lock, flags);
10021001
if (ret) {
1003-
mutex_unlock(&pcpu_alloc_mutex);
10041002
pcpu_free_area(chunk, off, &occ_pages);
10051003
err = "failed to populate";
10061004
goto fail_unlock;
@@ -1040,6 +1038,8 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
10401038
/* see the flag handling in pcpu_blance_workfn() */
10411039
pcpu_atomic_alloc_failed = true;
10421040
pcpu_schedule_balance_work();
1041+
} else {
1042+
mutex_unlock(&pcpu_alloc_mutex);
10431043
}
10441044
return NULL;
10451045
}

0 commit comments

Comments
 (0)