From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A1C653EFFA1 for ; Wed, 1 Jul 2026 08:49:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782895790; cv=none; b=lr5yc9PcIAelHmlmcugM8my4oaXQAT14UIGOhnmgw25S4c/lJ0BXPTg/b4fQKFFnurs3qwOOMjAYW9HdkTiIHhl3h5EoEHeNa5nYucxtDzBrmlqbx6UFpUgPGLBL9YwRuDkuMT+Z+MWhtt7zKBgV6mGtOcmI+KtCINGt8INazrs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782895790; c=relaxed/simple; bh=UJGSMkfC/a+LGl1L5A1+9U5pkPG95z+tmBQzIhZ/I9U=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=WqRyVuvvm5Dyc/WJfGTgq9/cG9GVbB/O/4z57uicIzAD/8KO5DmxnrFrgjj/GXS4QF9HejMdHB1cv6ksotOczyJpImcuf8UJRVXk+biFT/Tp8IDFvbRMUHjieZW+MLruYb5XsbNT2ykl0tK6kOg9mrL9vpJZJPjnZuAVyV1X6Dc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org; spf=pass smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=m8Yc48Qf; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="m8Yc48Qf" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=hMc71Aibky6VNAGfJKLdii+gETCmEGXO/J7hZ6iQhL4=; b=m8Yc48QfdTOX1JuEuegYBe/qx+ 0iOQcBgbkdLDNeVPST9Hx6q62Cg/0mofpm083pKkimcU09hckfKkKr8ZVX90zJL4YTcxv7c4U1JQ2 k17WtHrgQbkeGfUhOVa20fuPgp/fZP80MIaW2fu2ApKj3YG/UnbnjSSVR7zu8ykvtZ3wwdi4QjJ4X xNBbQz4FuC1U+1b6/2vWKaPcZS3xmC0N/nVWFOkdLGuhba8OUwZis8OtDaaQXt2cAga2wBrapxiaj UcSP+EUVtI68mcsENBX/Iip1yYZ8rzzG0LA5RieSqDD1VVuPrOxdpOo0pQy47Cw5NGZMj4y+l+Q2A mW6q2Wwg==; Received: from 77-249-17-252.cable.dynamic.v4.ziggo.nl ([77.249.17.252] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.99.2 #2 (Red Hat Linux)) id 1weqdY-00000002X6s-3Lry; Wed, 01 Jul 2026 08:49:41 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id 93C513031AF; Wed, 01 Jul 2026 10:49:39 +0200 (CEST) Date: Wed, 1 Jul 2026 10:49:39 +0200 From: Peter Zijlstra To: K Prateek Nayak Cc: Gabriele Monaco , Sechang Lim , Ingo Molnar , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , linux-kernel@vger.kernel.org, Mark Rutland , Will Deacon Subject: Re: [PATCH v2] sched: set TIF_NEED_RESCHED before calling __trace_set_need_resched() Message-ID: <20260701084939.GE751831@noisy.programming.kicks-ass.net> References: <20260627081657.499781-1-rhkrqnwk98@gmail.com> <6cbcc3ea742b92b6a34b5994f5498c7611beac7e.camel@redhat.com> <5f47566b-88f4-458d-87a4-7794e6ba8203@amd.com> <20260630203408.GG48970@noisy.programming.kicks-ass.net> <20260701065447.GI48970@noisy.programming.kicks-ass.net> <14abaec8-05ca-4034-b153-b993ed60a1fb@amd.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <14abaec8-05ca-4034-b153-b993ed60a1fb@amd.com> On Wed, Jul 01, 2026 at 01:39:23PM +0530, K Prateek Nayak wrote: > Hello Peter, > > On 7/1/2026 12:24 PM, Peter Zijlstra wrote: > >> @@ -1204,16 +1217,16 @@ static void __resched_curr(struct rq *rq, int tif) > >> cpu = cpu_of(rq); > >> > >> if (cpu == smp_processor_id()) { > >> - set_ti_thread_flag(cti, tif); > >> + int set = test_and_set_ti_thread_flag(cti, tif); > >> + > >> + if (trace_sched_set_need_resched_tp_enabled() && !set) > >> + trace_call__sched_set_need_resched_tp(curr, cpu, tif); > >> if (tif == TIF_NEED_RESCHED) > >> set_preempt_need_resched(); > >> - trace_sched_set_need_resched_tp(curr, cpu, tif); > >> return; > >> } > > > > I can't help but notice that the local and !POLLING cases show > > remarkable similarity. Just not sure extracting that isn't going to make > > a mess. > > > > Anyway, yes this looks about right. > > If the fetch_or() based path is okay for !POLLING and local cases which > uses a slightly (vastly?) worse instruction to set the ti->flags, we can > instead do: > Tempting, but at least arm64 would have to agree I think. That said, I wonder why ARM64 doesn't use LDXR+WFE for idle, just like we have MONITOR+MWAIT. Probably too damn many idle flavours to deal with or somesuch. > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index bd2f7fb87dc93..ea793e8a94a8f 100644 > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -1029,6 +1029,15 @@ static inline void hrtick_schedule_enter(struct rq *rq) { } > static inline void hrtick_schedule_exit(struct rq *rq) { } > #endif /* !CONFIG_SCHED_HRTICK */ > > +#ifndef TIF_POLLING_NRFLAG > +/* > + * If arch doesn't define _TIF_POLLING_NRFLAG, set it 0 to > + * allow compilers to optimize (val & _TIF_POLLING_NRFLAG) > + * based branches during build. > + */ > +#define _TIF_POLLING_NRFLAG 0U > +#endif > + > /* > * try_cmpxchg based fetch_or() macro so it works for different integer types: > */ > @@ -1043,7 +1052,6 @@ static inline void hrtick_schedule_exit(struct rq *rq) { } > _val; \ > }) > > -#ifdef TIF_POLLING_NRFLAG > /* > * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG, > * this avoids any races wrt polling state changes and thereby avoids > @@ -1083,25 +1091,6 @@ static bool set_nr_if_polling(struct task_struct *p) > return true; > } > > -#else > -static inline bool set_nr_and_not_polling(struct rq *rq, int tif) > -{ > - struct task_struct *curr = rq->curr; > - struct thread_info *ti = task_thread_info(curr); > - int set = test_and_set_ti_thread_flag(ti, tif); > - > - if (trace_sched_set_need_resched_tp_enabled() && !set) > - trace_call__sched_set_need_resched_tp(curr, cpu_of(rq), tif); > - > - return true; > -} > - > -static inline bool set_nr_if_polling(struct task_struct *p) > -{ > - return false; > -} > -#endif > - > static bool __wake_q_add(struct wake_q_head *head, struct task_struct *task) > { > struct wake_q_node *node = &task->wake_q; > @@ -1216,19 +1205,19 @@ static void __resched_curr(struct rq *rq, int tif) > > cpu = cpu_of(rq); > > - if (cpu == smp_processor_id()) { > - int set = test_and_set_ti_thread_flag(cti, tif); > - > - if (trace_sched_set_need_resched_tp_enabled() && !set) > - trace_call__sched_set_need_resched_tp(curr, cpu, tif); > - if (tif == TIF_NEED_RESCHED) > - set_preempt_need_resched(); > - return; > - } > - > if (set_nr_and_not_polling(rq, tif)) { > - if (tif == TIF_NEED_RESCHED) > - smp_send_reschedule(cpu); > + if (tif != TIF_NEED_RESCHED) > + return; > + /* > + * For local CPU, folding the NEED_RESCHED > + * into preempt_count() is sufficient. > + */ > + if (cpu == smp_processor_id()) { > + set_preempt_need_resched(); > + return; > + } > + /* Use an IPI for remote CPUs. */ > + smp_send_reschedule(cpu); > } else { > trace_sched_wake_idle_without_ipi(cpu); > } > --- > > set_nr_and_not_polling() will always return true for !POLLING which will > go down the above path in __resched_curr() that deals appropriately with > local CPU case. > > set_nr_if_polling() will always return false for !POLLING from the first > condition in the do-while loop. > > Thoughts? > > -- > Thanks and Regards, > Prateek >