From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754202AbbBNSBU (ORCPT ); Sat, 14 Feb 2015 13:01:20 -0500 Received: from smtp2.provo.novell.com ([137.65.250.81]:42291 "EHLO smtp2.provo.novell.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754011AbbBNSBT (ORCPT ); Sat, 14 Feb 2015 13:01:19 -0500 Message-ID: <1423936866.2046.88.camel@stgolabs.net> Subject: Re: [PATCH 1/1] futex: check PF_KTHREAD rather than !p->mm to filter out kthreads From: Davidlohr Bueso To: Oleg Nesterov Cc: Darren Hart , Peter Zijlstra , Thomas Gleixner , Jerome Marchand , Larry Woodman , Mateusz Guzik , linux-kernel@vger.kernel.org Date: Sat, 14 Feb 2015 10:01:06 -0800 In-Reply-To: <20150202140536.GA26406@redhat.com> References: <20150202140515.GA26398@redhat.com> <20150202140536.GA26406@redhat.com> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.12.9 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 2015-02-02 at 15:05 +0100, Oleg Nesterov wrote: > attach_to_pi_owner() checks p->mm to prevent attaching to kthreads and > this looks doubly wrong: > > 1. It should actually check PF_KTHREAD, kthread can do use_mm(). > > 2. If this task is not kthread and it is actually the lock owner we can > wrongly return -EPERM instead of -ESRCH or retry-if-EAGAIN. > > And note that this wrong EPERM is the likely case unless the exiting > task is (auto)reaped quickly, we check ->mm before PF_EXITING. > > Signed-off-by: Oleg Nesterov > --- > kernel/futex.c | 2 +- > 1 files changed, 1 insertions(+), 1 deletions(-) > > diff --git a/kernel/futex.c b/kernel/futex.c > index 63678b5..b101381 100644 > --- a/kernel/futex.c > +++ b/kernel/futex.c > @@ -900,7 +900,7 @@ static int attach_to_pi_owner(u32 uval, union futex_key *key, > if (!p) > return -ESRCH; > > - if (!p->mm) { > + if (unlikely(p->flags & PF_KTHREAD)) { > put_task_struct(p); > return -EPERM; > } Futexes aren't the only naughty checkers, a quick search shows that, at least, the oom killer and proc have this same problem. Should we make this generic and update accordingly? ie: diff --git a/include/linux/sched.h b/include/linux/sched.h index 8db31ef..b0d37d6 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1991,6 +1991,11 @@ extern void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, #define tsk_used_math(p) ((p)->flags & PF_USED_MATH) #define used_math() tsk_used_math(current) +static inline bool task_is_kthread(struct task_struct *task) +{ + return task->flags & PF_KTHREAD; +} + /* __GFP_IO isn't allowed if PF_MEMALLOC_NOIO is set in current->flags * __GFP_FS is also cleared as it implies __GFP_IO. */ Thanks, Davidlohr