From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751689AbdFTNwx (ORCPT ); Tue, 20 Jun 2017 09:52:53 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:35106 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751094AbdFTNww (ORCPT ); Tue, 20 Jun 2017 09:52:52 -0400 From: Daniel Axtens To: Ingo Molnar Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, mingo@redhat.com, Konstantin Khlebnikov , Ben Segall , Thomas Gleixner Subject: Re: [PATCH] sched/debug: Fix SCHED_WARN_ON() to return a value on !CONFIG_SCHED_DEBUG as well In-Reply-To: <20170620102742.p3qta5lrefhbyeen@gmail.com> References: <20170510201139.16236-1-dja@axtens.net> <20170620102742.p3qta5lrefhbyeen@gmail.com> Date: Tue, 20 Jun 2017 23:52:46 +1000 Message-ID: <87fueupxzl.fsf@linkitivity.dja.id.au> MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Ingo, Good catch - thanks for picking that up and taking the patch! Regards, Daniel > * Daniel Axtens wrote: > >> If we set a next or last buddy for a se that is not on_rq, we will >> end up taking a NULL pointer dereference in wakeup_preempt_entity >> via pick_next_task_fair. >> >> Detect when we would be about to do that, throw a warning and >> then refuse to actually set it. >> >> This has been suggested at least twice[0][1]: just do it. >> >> [0] https://marc.info/?l=linux-kernel&m=146651668921468&w=2 >> [1] https://lkml.org/lkml/2016/6/16/663 >> >> Cc: Konstantin Khlebnikov >> Cc: Ben Segall >> Cc: Peter Zijlstra >> Cc: Thomas Gleixner >> Signed-off-by: Daniel Axtens >> >> --- >> >> I recently had to debug a problem with these (we hadn't backported >> Konstantin's patches in this area) and this would have saved a lot >> of time/pain. >> >> v2: use SCHED_WARN_ON to restrict when the test is run. This is a >> macro for WARN_ON_ONCE, which is convenient. >> --- >> kernel/sched/fair.c | 10 ++++++++-- >> 1 file changed, 8 insertions(+), 2 deletions(-) >> >> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c >> index d71109321841..44b94cfe02cb 100644 >> --- a/kernel/sched/fair.c >> +++ b/kernel/sched/fair.c >> @@ -6168,8 +6168,11 @@ static void set_last_buddy(struct sched_entity *se) >> if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) >> return; >> >> - for_each_sched_entity(se) >> + for_each_sched_entity(se) { >> + if (SCHED_WARN_ON(!se->on_rq)) >> + return; >> cfs_rq_of(se)->last = se; >> + } > > This won't build in the !CONFIG_SCHED_DEBUG case, because of the naive definition > in sched.h: > > #define SCHED_WARN_ON(x) ((void)(x)) > > That should be changed to something like: > > #define SCHED_WARN_ON(x) ((void)(x)) > > I've applied the fix below. (untested at the moment) > > Thanks, > > Ingo > > ========================> > From 6d3aed3d8a0573d0a6eb1160ccd0a0713f4dbc2f Mon Sep 17 00:00:00 2001 > From: Ingo Molnar > Date: Tue, 20 Jun 2017 12:24:42 +0200 > Subject: [PATCH] sched/debug: Fix SCHED_WARN_ON() to return a value on !CONFIG_SCHED_DEBUG as well > > This definition of SCHED_WARN_ON(): > > #define SCHED_WARN_ON(x) ((void)(x)) > > is not fully compatible with the 'real' WARN_ON_ONCE() primitive, as it > has no return value, so it cannot be used in conditionals. > > Fix it. > > Cc: Daniel Axtens > Cc: Konstantin Khlebnikov > Cc: Peter Zijlstra > Cc: Mike Galbraith > Cc: Thomas Gleixner > Cc: Linus Torvalds > Cc: linux-kernel@vger.kernel.org > Signed-off-by: Ingo Molnar > --- > kernel/sched/sched.h | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h > index f2ef759a4cb6..e0329d10bdb8 100644 > --- a/kernel/sched/sched.h > +++ b/kernel/sched/sched.h > @@ -39,9 +39,9 @@ > #include "cpuacct.h" > > #ifdef CONFIG_SCHED_DEBUG > -#define SCHED_WARN_ON(x) WARN_ONCE(x, #x) > +# define SCHED_WARN_ON(x) WARN_ONCE(x, #x) > #else > -#define SCHED_WARN_ON(x) ((void)(x)) > +# define SCHED_WARN_ON(x) ({ (void)(x), 0; }) > #endif > > struct rq;