From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from SN4PR2101CU001.outbound.protection.outlook.com (mail-southcentralusazon11012000.outbound.protection.outlook.com [40.93.195.0]) (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 6A3173C3C1E for ; Wed, 1 Jul 2026 08:11:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.195.0 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782893467; cv=fail; b=ZnbxEBykixWAXh30W+OWxbdR8+ybTZlcshro7Y022n31rxnMgSAth7cygmBwOyF2jjbuHpGdVSDdvpTeZFlJDJXbiPkeoRuTQTdNntzF1oCoYOzkKxmaChXlS+L6G5mgTr/kZey/o/Lqopn8atsnlV4jvjQ7VzV/fgZqcPNMwzo= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782893467; c=relaxed/simple; bh=dUWPkQNZ+C7wEK1gZhPxKPjmI/gNPCIxTQkRV6zdzVA=; h=Message-ID:Date:MIME-Version:Subject:To:CC:References:From: In-Reply-To:Content-Type; b=Jwqdxz2vHtEfEP7C0mT4uxuPbgY9lOIBoGBQS6B/PQKE9e/AWIPqpnrq4rcOWy0eyw78u0ygQFWnMSw3YNQ4k1D2VCRNkWRruWmAf5pZe7UruR/DNkDfYetjuOWyjAJ3kXu25XGl9KahpSR5Sw1x2cYS92BDNQIFKctjtA8Qauc= 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=4DzSFjw5; arc=fail smtp.client-ip=40.93.195.0 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="4DzSFjw5" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=mlepupvIcTtydEoQg2Np6aP8iIx75w/GSso/B2Ea3cMPoW008yqOqpMAK0JKkuC3icEWmeTp611gK2GUI0e65m9Zhec1Mp4MeNKVT/3bvoVNYKUDhJyEt6RRiDzFgJn1my8hSNiOOqtKkPj9V/KTjlNHWoB+5MHsRrK+WRqGZ0J8/zEzWOn9TDGzSrbKZBFoFC6smrFxCtT4EyfH/xuDw5WZ8eLy6jp12SyBMh1MXTFReS/RWIla1YECvZ7OtWHd4usLOHGt3eKDdxL1I2dTYAPPP3P5nzqr6nSm5WRjfdpcobK8mfIPQOSuuQkYo7SMdY7+aqcTBhAM6lmAy0K6UA== 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=kqiJj8yihHT2IvEUAxnBq0mVlhsSdjXV8yOcxvB1xSs=; b=bz8FLYDEY72tudhVQfeOXvf5oVg50BtzR4x+no9QfOP44yOMjH9eJ3hGyltmcXGqIdJM+er7JOZHx77+IBKFIYU0WQLGXFQsGnZGzJjec2iYLsLKzaCqh9FLGjwS0CXJNhoUrS5k9lQXvIylrnOa9VTsrWl5ZCq8ucXd6amInKTJO2VIo3Dtu/NPfL1qrKhCqCwwVWpp25s9KO4mZQx3lscl5ZmqXhWFX5ONVCjLrKRgGlb7PwvX/gjuk60e9KLw/FX+yh9RUV79vC3+g79+waJNR4l0rDptpCPDKZnLCdha+FSOd7TdnY5goRDwCFCVr9W72ZZQoIkbUl1p45J2AA== 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=kqiJj8yihHT2IvEUAxnBq0mVlhsSdjXV8yOcxvB1xSs=; b=4DzSFjw5E3IgTav7B1+KfIkr/3SvBEUQbV2sdkLoKUCsvIQV0xmRoCd5mbGHryOMhjrl8usHJZpO9B9ism2DlhmYnIbQ/GV6w0BMbUf6Jj7VEnAAMM9RZsd/0jAJ06uZ+5caS9gyQphuodjGigjAuXbpXgfKah50ckTo2IpzVFs= Received: from BL1PR13CA0204.namprd13.prod.outlook.com (2603:10b6:208:2be::29) by CH3PR12MB8260.namprd12.prod.outlook.com (2603:10b6:610:12a::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.181.9; Wed, 1 Jul 2026 08:10:59 +0000 Received: from BL6PEPF00020E64.namprd04.prod.outlook.com (2603:10b6:208:2be:cafe::6e) by BL1PR13CA0204.outlook.office365.com (2603:10b6:208:2be::29) 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 08:10:59 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL6PEPF00020E64.mail.protection.outlook.com (10.167.249.25) 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 08:10:59 +0000 Received: from satlexmb10.amd.com (10.181.42.219) by satlexmb07.amd.com (10.181.42.216) 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 03:10:57 -0500 Received: from satlexmb08.amd.com (10.181.42.217) by satlexmb10.amd.com (10.181.42.219) 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 03:09:33 -0500 Received: from [10.136.39.208] (10.180.168.240) by satlexmb08.amd.com (10.181.42.217) with Microsoft SMTP Server id 15.2.2562.41 via Frontend Transport; Wed, 1 Jul 2026 03:09:30 -0500 Message-ID: <14abaec8-05ca-4034-b153-b993ed60a1fb@amd.com> Date: Wed, 1 Jul 2026 13:39:23 +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 CC: Gabriele Monaco , 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> <20260701065447.GI48970@noisy.programming.kicks-ass.net> Content-Language: en-US From: K Prateek Nayak In-Reply-To: <20260701065447.GI48970@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: BL6PEPF00020E64:EE_|CH3PR12MB8260:EE_ X-MS-Office365-Filtering-Correlation-Id: d2903f17-fb45-4461-76b5-08ded74848b5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|36860700016|376014|7416014|23010399003|1800799024|22082099003|18002099003|4143699003|3023799007|11063799006|56012099006; X-Microsoft-Antispam-Message-Info: lZE9dFB4RIt2ddLju+XZfqWp/3lswruSHPbhIOZ588T72SMocg7YpAXO37xNpYfvcZZY+1slzAfIXhE0Ma3ATV+uThLqMUFEtgcHCC2n2WRIiQL26KQg66am9323MiwiZ5G4WwRdwWXkFxrrDTlAX0lYg36JWGaQQe3x84DQmKUmSAFFaxr4U1u5o7CeqCELwqukmh9AkJ3dZfeOvMI/ialEyo61oEmCURc8AP4Oo8eqyG9ijk2PtZ3WihPfR14aCCftUoQwY96zvBLCoPv18Aa/OXR8c7c6xuKTn9FwEFXxGWbOpOXlBypCNlUMyRp2SWKbaFp2bH85hi0SHYs3JEZnZAaYEg9L0vxu7Bh8PEczvByY713bRF3dR+Y2in1vfbY1+fK5PcXEtzlI248EdexbHaLa5+uEFu5bQgbPxktTOYSlOREFl+WxYoI2IgMuKDBCczFCcTlTQcDjIz5/XB7gn9ENUbi1E/+jQnV1IlEhulbc50uRRFp4Ki7QRYio68YHOLRNXb3riS6GFV/MWU0BnGuqmNiqCa/q9Dx9KJo7g+BOw4Lb3LPhta1fUAFW4naSLPpNqVqAADnoClT5zSJxj8ObDFRbaONnp1YS8i8NlDDIN5o+OQHDpxzXTV0qYJV/n4Q2ZIhqB5DPApJwwgG9gEz/Fm0/5SK/+6nRYvR8POYMwqC31Yhm//2U0JyXYeJ3KDuLt5QwVUqdhJBZCA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(36860700016)(376014)(7416014)(23010399003)(1800799024)(22082099003)(18002099003)(4143699003)(3023799007)(11063799006)(56012099006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: k73hBwwYpEujSwzTc953U/cBwTjabTk6u2xwcWAW608gTx/GLVHGPdqS4AeVPtRkrFCGzE/3BhicrKhd+6VPxmPomZXSO4K0I2srhEMycRMKntpsJEWI9kisB86i7yhSTpabou72O2PQTlaJUw8ZRy6TxRJ4GYNi3+jDiithL6UF/UujTVgjndRUgUDO5+z1uUupHUNXurkloIASHT4NDzX7bCCvq+6oZvIaV83eQcdH6HT0kq6rMS+KKiZ6vRRPEtVuXGXBT6spPrWPALpZJECPHqSM4U40lzcLs4v+vbvEanwpr6Uh/dVV5uqxeYDCSVDjloxxQLpUcXS09Iq7APOBNT40nGudQFYmi9smrRdRPXzBxFSJruaENv5lMH/o698Aza2wNf90PEW5HrkmMewNzBl9rPtjtjJXH8U5MBpmi4RgIp/t8CnhTJYcudsV X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Jul 2026 08:10:59.0173 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d2903f17-fb45-4461-76b5-08ded74848b5 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=[satlexmb07.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BL6PEPF00020E64.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8260 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: (Lightly tested after removing "HAVE_TIF_POLLING_NRFLAG" for x86) 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