Skip to content

Commit 6939d43

Browse files
Yang ShiEmanuelCN
authored andcommitted
mm: introduce arg_lock to protect arg_start|end and env_start|end in mm_struct
mmap_sem is on the hot path of kernel, and it very contended, but it is abused too. It is used to protect arg_start|end and evn_start|end when reading /proc/$PID/cmdline and /proc/$PID/environ, but it doesn't make sense since those proc files just expect to read 4 values atomically and not related to VM, they could be set to arbitrary values by C/R. And, the mmap_sem contention may cause unexpected issue like below: INFO: task ps:14018 blocked for more than 120 seconds. Tainted: G E 4.9.79-009.ali3000.alios7.x86_64 #1 "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. ps D 0 14018 1 0x00000004 Call Trace: schedule+0x36/0x80 rwsem_down_read_failed+0xf0/0x150 call_rwsem_down_read_failed+0x18/0x30 down_read+0x20/0x40 proc_pid_cmdline_read+0xd9/0x4e0 __vfs_read+0x37/0x150 vfs_read+0x96/0x130 SyS_read+0x55/0xc0 entry_SYSCALL_64_fastpath+0x1a/0xc5 Both Alexey Dobriyan and Michal Hocko suggested to use dedicated lock for them to mitigate the abuse of mmap_sem. So, introduce a new spinlock in mm_struct to protect the concurrent access to arg_start|end, env_start|end and others, as well as replace write map_sem to read to protect the race condition between prctl and sys_brk which might break check_data_rlimit(), and makes prctl more friendly to other VM operations. This patch just eliminates the abuse of mmap_sem, but it can't resolve the above hung task warning completely since the later access_remote_vm() call needs acquire mmap_sem. The mmap_sem scalability issue will be solved in the future. [yang.shi@linux.alibaba.com: add comment about mmap_sem and arg_lock] Link: http://lkml.kernel.org/r/1524077799-80690-1-git-send-email-yang.shi@linux.alibaba.com Link: http://lkml.kernel.org/r/1523730291-109696-1-git-send-email-yang.shi@linux.alibaba.com Signed-off-by: Yang Shi <yang.shi@linux.alibaba.com> Reviewed-by: Cyrill Gorcunov <gorcunov@openvz.org> Acked-by: Michal Hocko <mhocko@suse.com> Cc: Alexey Dobriyan <adobriyan@gmail.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: Mateusz Guzik <mguzik@redhat.com> Cc: Kirill Tkhai <ktkhai@virtuozzo.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c2e9f14 commit 6939d43

5 files changed

Lines changed: 16 additions & 6 deletions

File tree

fs/proc/base.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,12 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
234234
goto out_mmput;
235235
}
236236

237-
down_read(&mm->mmap_sem);
237+
spin_lock(&mm->arg_lock);
238238
arg_start = mm->arg_start;
239239
arg_end = mm->arg_end;
240240
env_start = mm->env_start;
241241
env_end = mm->env_end;
242-
up_read(&mm->mmap_sem);
242+
spin_unlock(&mm->arg_lock);
243243

244244
BUG_ON(arg_start > arg_end);
245245
BUG_ON(env_start > env_end);
@@ -974,10 +974,10 @@ static ssize_t environ_read(struct file *file, char __user *buf,
974974
if (!atomic_inc_not_zero(&mm->mm_users))
975975
goto free;
976976

977-
down_read(&mm->mmap_sem);
977+
spin_lock(&mm->arg_lock);
978978
env_start = mm->env_start;
979979
env_end = mm->env_end;
980-
up_read(&mm->mmap_sem);
980+
spin_unlock(&mm->arg_lock);
981981

982982
while (count > 0) {
983983
size_t this_len, max_len;

include/linux/mm_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ struct mm_struct {
452452
unsigned long exec_vm; /* VM_EXEC & ~VM_WRITE & ~VM_STACK */
453453
unsigned long stack_vm; /* VM_STACK */
454454
unsigned long def_flags;
455+
456+
spinlock_t arg_lock; /* protect the below fields */
455457
unsigned long start_code, end_code, start_data, end_data;
456458
unsigned long start_brk, brk, start_stack;
457459
unsigned long arg_start, arg_end, env_start, env_end;

kernel/fork.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
789789
mm->pinned_vm = 0;
790790
memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
791791
spin_lock_init(&mm->page_table_lock);
792+
spin_lock_init(&mm->arg_lock);
792793
mm_init_cpumask(mm);
793794
mm_init_aio(mm);
794795
mm_init_owner(mm, p);

kernel/sys.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,11 @@ static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data
18601860
return error;
18611861
}
18621862

1863-
down_write(&mm->mmap_sem);
1863+
/*
1864+
* arg_lock protects concurent updates but we still need mmap_sem for
1865+
* read to exclude races with sys_brk.
1866+
*/
1867+
down_read(&mm->mmap_sem);
18641868

18651869
/*
18661870
* We don't validate if these members are pointing to
@@ -1874,6 +1878,7 @@ static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data
18741878
* to any problem in kernel itself
18751879
*/
18761880

1881+
spin_lock(&mm->arg_lock);
18771882
mm->start_code = prctl_map.start_code;
18781883
mm->end_code = prctl_map.end_code;
18791884
mm->start_data = prctl_map.start_data;
@@ -1885,6 +1890,7 @@ static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data
18851890
mm->arg_end = prctl_map.arg_end;
18861891
mm->env_start = prctl_map.env_start;
18871892
mm->env_end = prctl_map.env_end;
1893+
spin_unlock(&mm->arg_lock);
18881894

18891895
/*
18901896
* Note this update of @saved_auxv is lockless thus
@@ -1897,7 +1903,7 @@ static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data
18971903
if (prctl_map.auxv_size)
18981904
memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
18991905

1900-
up_write(&mm->mmap_sem);
1906+
up_read(&mm->mmap_sem);
19011907
return 0;
19021908
}
19031909
#endif /* CONFIG_CHECKPOINT_RESTORE */

mm/init-mm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct mm_struct init_mm = {
2424
.mm_count = ATOMIC_INIT(1),
2525
.mmap_sem = __RWSEM_INITIALIZER(init_mm.mmap_sem),
2626
.page_table_lock = __SPIN_LOCK_UNLOCKED(init_mm.page_table_lock),
27+
.arg_lock = __SPIN_LOCK_UNLOCKED(init_mm.arg_lock),
2728
.mmlist = LIST_HEAD_INIT(init_mm.mmlist),
2829
.user_ns = &init_user_ns,
2930
INIT_MM_CONTEXT(init_mm)

0 commit comments

Comments
 (0)