提交 ce07b017 编写于 作者: M Mathieu Desnoyers 提交者: Yang Yingliang

sched/membarrier: Return -ENOMEM to userspace on memory allocation failure

mainline inclusion
from mainline-5.4-rc1
commit c172e0a3e8e65a4c6fffec5bc4d6de08d6f894f7
category: bugfix
bugzilla: 28332
CVE: NA

-------------------------------------------------

Remove the IPI fallback code from membarrier to deal with very
infrequent cpumask memory allocation failure. Use GFP_KERNEL rather
than GFP_NOWAIT, and relax the blocking guarantees for the expedited
membarrier system call commands, allowing it to block if waiting for
memory to be made available.

In addition, now -ENOMEM can be returned to user-space if the cpumask
memory allocation fails.
Signed-off-by: NMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: NPeter Zijlstra (Intel) <peterz@infradead.org>
Cc: Chris Metcalf <cmetcalf@ezchip.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: Kirill Tkhai <tkhai@yandex.ru>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Paul E. McKenney <paulmck@linux.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Russell King - ARM Linux admin <linux@armlinux.org.uk>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: https://lkml.kernel.org/r/20190919173705.2181-8-mathieu.desnoyers@efficios.comSigned-off-by: NIngo Molnar <mingo@kernel.org>
Signed-off-by: NCheng Jian <cj.chengjian@huawei.com>
Reviewed-By: NXie XiuQi <xiexiuqi@huawei.com>
Signed-off-by: NYang Yingliang <yangyingliang@huawei.com>
上级 9697f3c0
...@@ -75,7 +75,6 @@ void membarrier_exec_mmap(struct mm_struct *mm) ...@@ -75,7 +75,6 @@ void membarrier_exec_mmap(struct mm_struct *mm)
static int membarrier_global_expedited(void) static int membarrier_global_expedited(void)
{ {
int cpu; int cpu;
bool fallback = false;
cpumask_var_t tmpmask; cpumask_var_t tmpmask;
if (num_online_cpus() == 1) if (num_online_cpus() == 1)
...@@ -87,15 +86,8 @@ static int membarrier_global_expedited(void) ...@@ -87,15 +86,8 @@ static int membarrier_global_expedited(void)
*/ */
smp_mb(); /* system call entry is not a mb. */ smp_mb(); /* system call entry is not a mb. */
/* if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
* Expedited membarrier commands guarantee that they won't return -ENOMEM;
* block, hence the GFP_NOWAIT allocation flag and fallback
* implementation.
*/
if (!zalloc_cpumask_var(&tmpmask, GFP_NOWAIT)) {
/* Fallback for OOM. */
fallback = true;
}
cpus_read_lock(); cpus_read_lock();
rcu_read_lock(); rcu_read_lock();
...@@ -126,18 +118,15 @@ static int membarrier_global_expedited(void) ...@@ -126,18 +118,15 @@ static int membarrier_global_expedited(void)
if (p->flags & PF_KTHREAD) if (p->flags & PF_KTHREAD)
continue; continue;
if (!fallback) __cpumask_set_cpu(cpu, tmpmask);
__cpumask_set_cpu(cpu, tmpmask);
else
smp_call_function_single(cpu, ipi_mb, NULL, 1);
} }
rcu_read_unlock(); rcu_read_unlock();
if (!fallback) {
preempt_disable(); preempt_disable();
smp_call_function_many(tmpmask, ipi_mb, NULL, 1); smp_call_function_many(tmpmask, ipi_mb, NULL, 1);
preempt_enable(); preempt_enable();
free_cpumask_var(tmpmask);
} free_cpumask_var(tmpmask);
cpus_read_unlock(); cpus_read_unlock();
/* /*
...@@ -152,7 +141,6 @@ static int membarrier_global_expedited(void) ...@@ -152,7 +141,6 @@ static int membarrier_global_expedited(void)
static int membarrier_private_expedited(int flags) static int membarrier_private_expedited(int flags)
{ {
int cpu; int cpu;
bool fallback = false;
cpumask_var_t tmpmask; cpumask_var_t tmpmask;
struct mm_struct *mm = current->mm; struct mm_struct *mm = current->mm;
...@@ -177,15 +165,8 @@ static int membarrier_private_expedited(int flags) ...@@ -177,15 +165,8 @@ static int membarrier_private_expedited(int flags)
*/ */
smp_mb(); /* system call entry is not a mb. */ smp_mb(); /* system call entry is not a mb. */
/* if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
* Expedited membarrier commands guarantee that they won't return -ENOMEM;
* block, hence the GFP_NOWAIT allocation flag and fallback
* implementation.
*/
if (!zalloc_cpumask_var(&tmpmask, GFP_NOWAIT)) {
/* Fallback for OOM. */
fallback = true;
}
cpus_read_lock(); cpus_read_lock();
rcu_read_lock(); rcu_read_lock();
...@@ -204,20 +185,16 @@ static int membarrier_private_expedited(int flags) ...@@ -204,20 +185,16 @@ static int membarrier_private_expedited(int flags)
continue; continue;
rcu_read_lock(); rcu_read_lock();
p = task_rcu_dereference(&cpu_rq(cpu)->curr); p = task_rcu_dereference(&cpu_rq(cpu)->curr);
if (p && p->mm == mm) { if (p && p->mm == mm)
if (!fallback) __cpumask_set_cpu(cpu, tmpmask);
__cpumask_set_cpu(cpu, tmpmask);
else
smp_call_function_single(cpu, ipi_mb, NULL, 1);
}
} }
rcu_read_unlock(); rcu_read_unlock();
if (!fallback) {
preempt_disable(); preempt_disable();
smp_call_function_many(tmpmask, ipi_mb, NULL, 1); smp_call_function_many(tmpmask, ipi_mb, NULL, 1);
preempt_enable(); preempt_enable();
free_cpumask_var(tmpmask);
} free_cpumask_var(tmpmask);
cpus_read_unlock(); cpus_read_unlock();
/* /*
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册