From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from BL0PR03CU003.outbound.protection.outlook.com (mail-eastusazon11012029.outbound.protection.outlook.com [52.101.53.29]) (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 5B08B3AD526 for ; Wed, 1 Jul 2026 06:51:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.53.29 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782888690; cv=fail; b=B6BtvMiKnK95EI0KC9TFY3RyEvfTMJRWsyRT9/b1y2O4CF1TsEePcF5pCXGnTiCuG7jzwkgne+arCrDPGLfpwMdNBzlE46ACUyUUASTpDE/64k7kP2xvqYlnwjVbJ+ZtEZyhffVznC2QQ9etQcc4oLvLZPeJT21uRhb048rt2WQ= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782888690; c=relaxed/simple; bh=AZDd4tFdTrOW9Ek87tISgpl906Cr0KVP/qRqfBc73S8=; h=Message-ID:Date:MIME-Version:Subject:To:CC:References:From: In-Reply-To:Content-Type; b=r1wJQflhKHxvqgC6o/9VYcAclpunVGIcKRsj1oUHwvbvqA75XXpSCUrPIVI36DDFDC7FhvhCCc/px4Q6VkxgvlrGO2WOei+pDMId87cTXj301PfvBrHEFzy65biG/Dv53KYBGEg1krMucm206i1zox+1D5jHDTj+j//eZcjlItU= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=hVdIPjCq; arc=fail smtp.client-ip=52.101.53.29 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="hVdIPjCq" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=f1l7BpW6fTVAZg/h+KS89j+cMDhZbDjUIHbCTcwrbIAAXAXmqHvRyxZ0Lk5XafL1ClTLPmBCFM3c+O/MnXAseWfki19zEeZb82cnB7rl30pHQtaxbPy5FOYFAS2o2Y1IYdhoub2LTdgNvOpghyfIYXTN5bsVV+bBGPQWNcWTTf8IBrdxj0GYCh1SVqypQvbe+AsELcLeXEwHgj3w9KoGJJ+ZL/1Mw0RwVHlxxN/NWZLc39cifr6YyF29+urBQn5DZoRTRqts9V0XkmA8hiLVhOVFMfkFz6nesVBgB7/+iZC16pDdWHXoFdO5Rxk9dJDeW2TxvhJn2LphemEwlMhOWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Rvtcr+UwByTKkLpXy2DRf0du/IDZMTNrAFNXhsI84jg=; b=m3Q3WvXoFZ9G4JKybOastIG204eMqJBcDdjhon9Fr2uaYgUlmyON+eG/xjZT4vhAS/j7/C6g6reFGU0L7Lg6Wfh+J5h1MHHCYbkReAJTf04nERcyLRR3nMOBD5MM3ka7IkwILDKmAZKIRma4mqQRwB+pjGfpudvEmp1XErIfclGXrkwZKFJTZHAFosI8QtyDYWrmNiwkvExg927efhCsbqN0w5rsmRCfFNONpSkFyuriFLvPcOBQYBbkIXb55A1U+1DExF8gN8qKyhhtPo5eDZf9Zls2YR+WkRlKczySpjbEPbpc1adsidngw7U1o1e//MR/0Dhvd9Xw0xm/Ku8gCQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=infradead.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Rvtcr+UwByTKkLpXy2DRf0du/IDZMTNrAFNXhsI84jg=; b=hVdIPjCqToY8blo4zYQ14Eb503Dk1VgCAzqOhGk4hb2sEGErdkLXqbe5yHRBiyxvaqeglPlXW4lnKBtEsTGZCSZ/CW+1hPRcljHp+QO7+FGJV5cDsbqxdpjEM3rHolcSuWd32BWJwXdRqIxLMqbIUG7RyThY3qGLvmaE5omXhTI= Received: from SN7PR04CA0170.namprd04.prod.outlook.com (2603:10b6:806:125::25) by CY8PR12MB7219.namprd12.prod.outlook.com (2603:10b6:930:59::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.181.8; Wed, 1 Jul 2026 06:51:24 +0000 Received: from SA2PEPF00003AE4.namprd02.prod.outlook.com (2603:10b6:806:125:cafe::7b) by SN7PR04CA0170.outlook.office365.com (2603:10b6:806:125::25) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.181.8 via Frontend Transport; Wed, 1 Jul 2026 06:51:24 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=satlexmb08.amd.com; pr=C Received: from satlexmb08.amd.com (165.204.84.17) by SA2PEPF00003AE4.mail.protection.outlook.com (10.167.248.4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.181.6 via Frontend Transport; Wed, 1 Jul 2026 06:51:23 +0000 Received: from Satlexmb09.amd.com (10.181.42.218) by satlexmb08.amd.com (10.181.42.217) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.41; Wed, 1 Jul 2026 01:51:23 -0500 Received: from satlexmb07.amd.com (10.181.42.216) by satlexmb09.amd.com (10.181.42.218) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.41; Tue, 30 Jun 2026 23:51:23 -0700 Received: from [10.136.39.208] (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server id 15.2.2562.41 via Frontend Transport; Wed, 1 Jul 2026 01:51:20 -0500 Message-ID: Date: Wed, 1 Jul 2026 12:21:14 +0530 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] sched: set TIF_NEED_RESCHED before calling __trace_set_need_resched() To: Peter Zijlstra , Gabriele Monaco CC: Sechang Lim , Ingo Molnar , "Juri Lelli" , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , 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> Content-Language: en-US From: K Prateek Nayak In-Reply-To: <20260630203408.GG48970@noisy.programming.kicks-ass.net> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA2PEPF00003AE4:EE_|CY8PR12MB7219:EE_ X-MS-Office365-Filtering-Correlation-Id: 010b6ccb-d28d-482b-832c-08ded73d2a80 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|36860700016|7416014|376014|23010399003|1800799024|13003099007|11063799006|18002099003|22082099003|4143699003|56012099006|6133799003; X-Microsoft-Antispam-Message-Info: 62QBdwYEdFO8ujeP11CqUZqYbDC05U1TrY385hpt64N3KrZ4GpB1RqFKzu4Kmsoy9UXWGRrMzlyTBqM0J/ogKIoPHTv3ouxlxrlWQI9XTv3OoGI+Obf4Ovb+H4HyE2MHgif4FAOo4avHl3OEyK0sg/eDy7ck/kr2O+1PFTk7ooMMcMuWgQzJE1C6Hyml7v+6KIIQOS6jIH36aLco9QtHVMA2cGcPdn7mBK6FT0btyk5c1xFIZCcJZKtbCMfg3QTwgREVkFz1YXgKmQOZWU+eiLLzLUF5Gki13Hbc0MyJj7JTLn/tD5oGvtJusDlr1E/Q5arXNiE7tJCthDimGARITEka/UbjjemjVMUQwX8JmbPz+DAyi6k+1yS2yuM7LKJsAd0HsvBNdSPSGvwSPF0LlLKjKIJeic5vHSlgEutwrxGU+jgN5JKpO1gzTVbmJr/DtOHlGAxsa4oSrtYHUZ+b606G5YTKHpsg7+09ImAk1IthIi3GhHLGJpaOHZnCNFo9LReY1HiR/brT6wTNAQorZXJWLUNoTPcjbc7BN7egNKwONyG4lV76yVX399kneqpgIiGKiuSWIyENgI+zo2wEvFXsi1P9f+eHVgn4cgYmmMRbwcpd77QZfoB6/dTIAxH/cGfrhLizJhUnKrhKe5oFW/xGFB478NzS8vxtxzghZgDSwHERMZ0bWoB3y1bsrkEVnu7x81CGjs06lvCjWhDgrA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb08.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(36860700016)(7416014)(376014)(23010399003)(1800799024)(13003099007)(11063799006)(18002099003)(22082099003)(4143699003)(56012099006)(6133799003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: AhfTFv2ERb0BKH8edGfmnphs6xRFc0J0aSfpNMJhhUpHTnPE3PKALO+KvYMcK9bSPl6OZvd182vtgOVojaXvTy2+R5BrVQPie7f+I4UgKGlGrSKjgUBs6BT+5rclPnLoxV5CUoRtPFFGV152SVwcQ3vrBGWz3ofw2CFZ6quEk/U56H+MzXYtZq4NSUzsEwKKUswgLhKNVGNwAL2wWZqc3aTuOSqQqRJkSFJVc3gD5GKjGKB+RJXTSXaDfjDphI8Y4vR6aFws0Gn3ceuMUGF9ndwJQrewTJ5ehZGC01jAqPx3/jHmQD5OIbhoZjHu4CJkYEghW4C0ipp9qEwGuu5ncflTDwXJRCUhj8lIesnvqcT82j2NUWQtWa2M2c5AtMOsDePmdgoM4kPX+BV3IOoFGoi4jFY4c2gq3VYNltDKa89oDlXQgyOCAFfyRtFYjv2j X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Jul 2026 06:51:23.8569 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 010b6ccb-d28d-482b-832c-08ded73d2a80 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[satlexmb08.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SA2PEPF00003AE4.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7219 Hello Peter, Gabriele, On 7/1/2026 2:04 AM, Peter Zijlstra wrote: >> -static inline bool set_nr_and_not_polling(struct thread_info *ti, int tif) >> +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); >> + >> set_ti_thread_flag(ti, tif); >> return true; >> } > > This !POLLING thing also needs tracing, no? Indeed! I'm stupid. Here is the full diff with set_nr_if_polling() covered too on top of tip:sched/core + Sechang's v3 at https://lore.kernel.org/lkml/20260630084750.2792851-1-rhkrqnwk98@gmail.com/ for completeness. Let me know if you prefer folding it in or if this needs to be a separate patch. (lightly testet) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 7cbd541f656f..bd2f7fb87dc9 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1049,9 +1049,16 @@ static inline void hrtick_schedule_exit(struct rq *rq) { } * this avoids any races wrt polling state changes and thereby avoids * spurious IPIs. */ -static inline bool set_nr_and_not_polling(struct thread_info *ti, int tif) +static inline bool set_nr_and_not_polling(struct rq *rq, int tif) { - return !(fetch_or(&ti->flags, 1 << tif) & _TIF_POLLING_NRFLAG); + struct task_struct *curr = rq->curr; + struct thread_info *ti = task_thread_info(curr); + unsigned long old_flags = fetch_or(&ti->flags, 1 << tif); + + if (trace_sched_set_need_resched_tp_enabled() && !(old_flags & (1 << tif))) + trace_call__sched_set_need_resched_tp(curr, cpu_of(rq), tif); + + return !(old_flags & _TIF_POLLING_NRFLAG); } /* @@ -1072,13 +1079,20 @@ static bool set_nr_if_polling(struct task_struct *p) return true; } while (!try_cmpxchg(&ti->flags, &val, val | _TIF_NEED_RESCHED)); + trace_sched_set_need_resched_tp(p, task_cpu(p), TIF_NEED_RESCHED); return true; } #else -static inline bool set_nr_and_not_polling(struct thread_info *ti, int tif) +static inline bool set_nr_and_not_polling(struct rq *rq, int tif) { - set_ti_thread_flag(ti, 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; } @@ -1186,7 +1200,6 @@ static void __resched_curr(struct rq *rq, int tif) { struct task_struct *curr = rq->curr; struct thread_info *cti = task_thread_info(curr); - bool need_ipi; int cpu; lockdep_assert_rq_held(rq); @@ -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; } - need_ipi = set_nr_and_not_polling(cti, tif); - trace_sched_set_need_resched_tp(curr, cpu, tif); - if (need_ipi) { + if (set_nr_and_not_polling(rq, tif)) { if (tif == TIF_NEED_RESCHED) smp_send_reschedule(cpu); } else { @@ -1353,7 +1366,7 @@ static void wake_up_idle_cpu(int cpu) * and testing of the above solutions didn't appear to report * much benefits. */ - if (set_nr_and_not_polling(task_thread_info(rq->idle), TIF_NEED_RESCHED)) + if (set_nr_and_not_polling(rq, TIF_NEED_RESCHED)) smp_send_reschedule(cpu); else trace_sched_wake_idle_without_ipi(cpu); --- Now, on a separate note to Gabriele, this trips up RV's sched:nrp monitor when running sched-messaging because of the following race: CPU0 CPU1 ==== ==== ... /* Wakes up a task on CPU0 */ rq_lock(rq0) __resched_curr(rq0) set = test_and_set_ti_thread_flag(curr, 0, TIF_NEED_RESCHED); <... interrupted by something before tracepoint hits> !!! rq0->curr has NEED_RESCHED set but trace_sched_set_need_resched_tp() is not called !!! raw_irqentry_exit_cond_resched() if (!preempt_count() /* Masks PREEMPT_NEED_RESCHED */) { if (need_resched() /* Only check TIF flags */) { preempt_schedule_irq() schedule(SM_PREEMPT) trace_sched_entry_tp(true /* SM_PREEMPT */) !!! SPLAT: rv: monitor nrp does not allow event schedule_entry_preempt on state any_thread_running !!! Easy way to solve this is by moving the trace_sched_entry_tp() within the rq_lock critical section. SM_PREEMPT has to happen on a !POLLING CPU and __resched_curr() is always under the rq_lock() so the __schedule() on a remote CPU cannot race with it before trace_sched_set_need_resched_tp() is executed. (lightly tested again) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index bd2f7fb87dc93..961c325feca84 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -7086,9 +7086,6 @@ static void __sched notrace __schedule(int sched_mode) struct rq *rq; int cpu; - /* Trace preemptions consistently with task switches */ - trace_sched_entry_tp(sched_mode == SM_PREEMPT); - cpu = smp_processor_id(); rq = cpu_rq(cpu); prev = rq->curr; @@ -7121,6 +7118,9 @@ static void __sched notrace __schedule(int sched_mode) rq_lock(rq, &rf); smp_mb__after_spinlock(); + /* Trace preemptions consistently with task switches */ + trace_sched_entry_tp(sched_mode == SM_PREEMPT); + hrtick_schedule_enter(rq); /* Promote REQ to ACT */ --- I'm not sure if this is a problem with current mainline but I did trip it in my testing and the above seems to solve it - your mileage may vary :-) -- Thanks and Regards, Prateek