aboutsummaryrefslogtreecommitdiffstats
path: root/mm
diff options
context:
space:
mode:
authorTejun Heo <tj@kernel.org>2016-05-25 11:48:25 -0400
committerTejun Heo <tj@kernel.org>2016-05-25 11:48:25 -0400
commit6710e594f71ccaad8101bc64321152af7cd9ea28 (patch)
tree526322970c44a03be3b46d27e45644fa188886fb /mm
parent4f996e234dad488e5d9ba0858bc1bae12eff82c3 (diff)
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 <tj@kernel.org> Reported-and-tested-by: Alexei Starovoitov <alexei.starovoitov@gmail.com> Reported-by: Vlastimil Babka <vbabka@suse.cz> Reported-by: Sasha Levin <sasha.levin@oracle.com> Cc: stable@vger.kernel.org # v3.18+ Fixes: 1a4d76076cda ("percpu: implement asynchronous chunk population")
Diffstat (limited to 'mm')
-rw-r--r--mm/percpu.c16
1 files changed, 8 insertions, 8 deletions
diff --git a/mm/percpu.c b/mm/percpu.c
index b1d2a3844792..9903830aaebb 100644
--- a/mm/percpu.c
+++ b/mm/percpu.c
@@ -162,7 +162,7 @@ static struct pcpu_chunk *pcpu_reserved_chunk;
162static int pcpu_reserved_chunk_limit; 162static int pcpu_reserved_chunk_limit;
163 163
164static DEFINE_SPINLOCK(pcpu_lock); /* all internal data structures */ 164static DEFINE_SPINLOCK(pcpu_lock); /* all internal data structures */
165static DEFINE_MUTEX(pcpu_alloc_mutex); /* chunk create/destroy, [de]pop */ 165static DEFINE_MUTEX(pcpu_alloc_mutex); /* chunk create/destroy, [de]pop, map ext */
166 166
167static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */ 167static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */
168 168
@@ -444,6 +444,8 @@ static int pcpu_extend_area_map(struct pcpu_chunk *chunk, int new_alloc)
444 size_t old_size = 0, new_size = new_alloc * sizeof(new[0]); 444 size_t old_size = 0, new_size = new_alloc * sizeof(new[0]);
445 unsigned long flags; 445 unsigned long flags;
446 446
447 lockdep_assert_held(&pcpu_alloc_mutex);
448
447 new = pcpu_mem_zalloc(new_size); 449 new = pcpu_mem_zalloc(new_size);
448 if (!new) 450 if (!new)
449 return -ENOMEM; 451 return -ENOMEM;
@@ -890,6 +892,9 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
890 return NULL; 892 return NULL;
891 } 893 }
892 894
895 if (!is_atomic)
896 mutex_lock(&pcpu_alloc_mutex);
897
893 spin_lock_irqsave(&pcpu_lock, flags); 898 spin_lock_irqsave(&pcpu_lock, flags);
894 899
895 /* serve reserved allocations from the reserved chunk if available */ 900 /* serve reserved allocations from the reserved chunk if available */
@@ -962,12 +967,9 @@ restart:
962 if (is_atomic) 967 if (is_atomic)
963 goto fail; 968 goto fail;
964 969
965 mutex_lock(&pcpu_alloc_mutex);
966
967 if (list_empty(&pcpu_slot[pcpu_nr_slots - 1])) { 970 if (list_empty(&pcpu_slot[pcpu_nr_slots - 1])) {
968 chunk = pcpu_create_chunk(); 971 chunk = pcpu_create_chunk();
969 if (!chunk) { 972 if (!chunk) {
970 mutex_unlock(&pcpu_alloc_mutex);
971 err = "failed to allocate new chunk"; 973 err = "failed to allocate new chunk";
972 goto fail; 974 goto fail;
973 } 975 }
@@ -978,7 +980,6 @@ restart:
978 spin_lock_irqsave(&pcpu_lock, flags); 980 spin_lock_irqsave(&pcpu_lock, flags);
979 } 981 }
980 982
981 mutex_unlock(&pcpu_alloc_mutex);
982 goto restart; 983 goto restart;
983 984
984area_found: 985area_found:
@@ -988,8 +989,6 @@ area_found:
988 if (!is_atomic) { 989 if (!is_atomic) {
989 int page_start, page_end, rs, re; 990 int page_start, page_end, rs, re;
990 991
991 mutex_lock(&pcpu_alloc_mutex);
992
993 page_start = PFN_DOWN(off); 992 page_start = PFN_DOWN(off);
994 page_end = PFN_UP(off + size); 993 page_end = PFN_UP(off + size);
995 994
@@ -1000,7 +999,6 @@ area_found:
1000 999
1001 spin_lock_irqsave(&pcpu_lock, flags); 1000 spin_lock_irqsave(&pcpu_lock, flags);
1002 if (ret) { 1001 if (ret) {
1003 mutex_unlock(&pcpu_alloc_mutex);
1004 pcpu_free_area(chunk, off, &occ_pages); 1002 pcpu_free_area(chunk, off, &occ_pages);
1005 err = "failed to populate"; 1003 err = "failed to populate";
1006 goto fail_unlock; 1004 goto fail_unlock;
@@ -1040,6 +1038,8 @@ fail:
1040 /* see the flag handling in pcpu_blance_workfn() */ 1038 /* see the flag handling in pcpu_blance_workfn() */
1041 pcpu_atomic_alloc_failed = true; 1039 pcpu_atomic_alloc_failed = true;
1042 pcpu_schedule_balance_work(); 1040 pcpu_schedule_balance_work();
1041 } else {
1042 mutex_unlock(&pcpu_alloc_mutex);
1043 } 1043 }
1044 return NULL; 1044 return NULL;
1045} 1045}