Commit eca03d12 authored by Tejun Heo's avatar Tejun Heo Committed by Kamal Mostafa

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: default avatarTejun Heo <tj@kernel.org>
Reported-and-tested-by: default avatarAlexei Starovoitov <alexei.starovoitov@gmail.com>
Reported-by: default avatarVlastimil Babka <vbabka@suse.cz>
Reported-by: default avatarSasha Levin <sasha.levin@oracle.com>
Cc: stable@vger.kernel.org # v3.18+
Fixes: 1a4d7607 ("percpu: implement asynchronous chunk population")
(cherry picked from commit 6710e594)
CVE-2016-4794
BugLink: https://bugs.launchpad.net/bugs/1581871Signed-off-by: default avatarLuis Henriques <luis.henriques@canonical.com>
Acked-by: default avatarChristopher Arges <chris.j.arges@canonical.com>
Signed-off-by: default avatarKamal Mostafa <kamal@canonical.com>
parent 77848738
...@@ -160,7 +160,7 @@ static struct pcpu_chunk *pcpu_reserved_chunk; ...@@ -160,7 +160,7 @@ static struct pcpu_chunk *pcpu_reserved_chunk;
static int pcpu_reserved_chunk_limit; static int pcpu_reserved_chunk_limit;
static DEFINE_SPINLOCK(pcpu_lock); /* all internal data structures */ static DEFINE_SPINLOCK(pcpu_lock); /* all internal data structures */
static DEFINE_MUTEX(pcpu_alloc_mutex); /* chunk create/destroy, [de]pop */ static DEFINE_MUTEX(pcpu_alloc_mutex); /* chunk create/destroy, [de]pop, map ext */
static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */ static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */
...@@ -446,6 +446,8 @@ static int pcpu_extend_area_map(struct pcpu_chunk *chunk, int new_alloc) ...@@ -446,6 +446,8 @@ static int pcpu_extend_area_map(struct pcpu_chunk *chunk, int new_alloc)
size_t old_size = 0, new_size = new_alloc * sizeof(new[0]); size_t old_size = 0, new_size = new_alloc * sizeof(new[0]);
unsigned long flags; unsigned long flags;
lockdep_assert_held(&pcpu_alloc_mutex);
new = pcpu_mem_zalloc(new_size); new = pcpu_mem_zalloc(new_size);
if (!new) if (!new)
return -ENOMEM; return -ENOMEM;
...@@ -892,6 +894,9 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved, ...@@ -892,6 +894,9 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
return NULL; return NULL;
} }
if (!is_atomic)
mutex_lock(&pcpu_alloc_mutex);
spin_lock_irqsave(&pcpu_lock, flags); spin_lock_irqsave(&pcpu_lock, flags);
/* serve reserved allocations from the reserved chunk if available */ /* serve reserved allocations from the reserved chunk if available */
...@@ -964,12 +969,9 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved, ...@@ -964,12 +969,9 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
if (is_atomic) if (is_atomic)
goto fail; goto fail;
mutex_lock(&pcpu_alloc_mutex);
if (list_empty(&pcpu_slot[pcpu_nr_slots - 1])) { if (list_empty(&pcpu_slot[pcpu_nr_slots - 1])) {
chunk = pcpu_create_chunk(); chunk = pcpu_create_chunk();
if (!chunk) { if (!chunk) {
mutex_unlock(&pcpu_alloc_mutex);
err = "failed to allocate new chunk"; err = "failed to allocate new chunk";
goto fail; goto fail;
} }
...@@ -980,7 +982,6 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved, ...@@ -980,7 +982,6 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
spin_lock_irqsave(&pcpu_lock, flags); spin_lock_irqsave(&pcpu_lock, flags);
} }
mutex_unlock(&pcpu_alloc_mutex);
goto restart; goto restart;
area_found: area_found:
...@@ -990,8 +991,6 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved, ...@@ -990,8 +991,6 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
if (!is_atomic) { if (!is_atomic) {
int page_start, page_end, rs, re; int page_start, page_end, rs, re;
mutex_lock(&pcpu_alloc_mutex);
page_start = PFN_DOWN(off); page_start = PFN_DOWN(off);
page_end = PFN_UP(off + size); page_end = PFN_UP(off + size);
...@@ -1002,7 +1001,6 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved, ...@@ -1002,7 +1001,6 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
spin_lock_irqsave(&pcpu_lock, flags); spin_lock_irqsave(&pcpu_lock, flags);
if (ret) { if (ret) {
mutex_unlock(&pcpu_alloc_mutex);
pcpu_free_area(chunk, off, &occ_pages); pcpu_free_area(chunk, off, &occ_pages);
err = "failed to populate"; err = "failed to populate";
goto fail_unlock; goto fail_unlock;
...@@ -1042,6 +1040,8 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved, ...@@ -1042,6 +1040,8 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
/* see the flag handling in pcpu_blance_workfn() */ /* see the flag handling in pcpu_blance_workfn() */
pcpu_atomic_alloc_failed = true; pcpu_atomic_alloc_failed = true;
pcpu_schedule_balance_work(); pcpu_schedule_balance_work();
} else {
mutex_unlock(&pcpu_alloc_mutex);
} }
return NULL; return NULL;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment