From nobody Tue Dec 16 21:09:29 2025 Received: from CY7PR03CU001.outbound.protection.outlook.com (mail-westcentralusazon11020079.outbound.protection.outlook.com [40.93.198.79]) (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 97F7E2FC009 for ; Tue, 9 Dec 2025 09:46:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.198.79 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765273580; cv=fail; b=K50Xb8+kEt/PzJUycVbzgfq/706FjHmQl9FvcjdaUVy9PZB7HHAU/T82O4/ZFKHoTvsT43vrVp5PZ+YQB20JrI1lzkvxyBBdTHmR5GXh7PWCsvJIRllDLz9LSgdmpTkJ0DAItYkNTQTaCpvoicBFpyQDSTVWm5siAy5mWJbQuIg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765273580; c=relaxed/simple; bh=LLDOL6qAKBY6pjX+Mye1lXZa98ghFYSGqfV86hNLGF4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=j3sgRhqLDfI9ZVVRwdSCtGb3UyN/hJ8rwB4IdZJ8X6PvM7QsOIeM75aqeASKUmsIOH87s2ZOa+ENPsQh0wmiRAzglmr69nLHTVwA8Qypei+BST3jaAdHm3NU/dOaJ4pKY4Gl1syf3Wq/O6/SOmq+FHS7mKwmnzKVl1AgJDMGZZg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=os.amperecomputing.com; spf=pass smtp.mailfrom=os.amperecomputing.com; dkim=pass (1024-bit key) header.d=os.amperecomputing.com header.i=@os.amperecomputing.com header.b=E+nGxvjP; arc=fail smtp.client-ip=40.93.198.79 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=os.amperecomputing.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=os.amperecomputing.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=os.amperecomputing.com header.i=@os.amperecomputing.com header.b="E+nGxvjP" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Ji8ymErZwsdyukwDmqfwSnEfnUb5T0q+3Hvuc0zDpUHUcnJscsY+UQ3Vy/OSpewCg1CD71fTZLmvY8p6RaiHx5bhuAIDgz1zQDk9UvRCHzsECqzfn18TcXPRrPFabQP0hMWI0aIrFy+2pVooTHkXwAJ6T1ebqOzbBu0WW9kPI+g/H6HiFXwC14SQvlJvD6sSxPX3HuJzr9NjuWJJHXGeK/bM/hbcEzAxtJmjM/ribkGiVq8HT4ozjO0J12JqQuI8IUU7k9rCjgP993oweU2z0kOOfEDq1ffhhrytcUV2rn5aGo7fBUC9zZMPRsWY33/Mn9vgwI1rV1SXtLU2eRDFDw== 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=J8r2948M01gPZXdN1KPb0ZJ86B5ynXe4ZT3mgB9gzvo=; b=u+XFppn4g8vubrCQn78qmCjAyEsKJicveMJgtKChY978Bo0s8IwcG1j50ZyTtUg3sYu2r1fNRbxOGeNVPi1PZjDJfH4YmJ733hWBydSz055DFbKBg4kJhM1d/B0qlnVL1BMw/UKj37aFUXl6ir2t0kuhNWejy0wUsCPNI4ajT/z78MMiz1on0gRn2v/K09Wzl9JJa8HsygvjnJmM5DqpmDmwbtTpC73S9jTHkaf5mGLvKv32z7o8OJ++/npGf841N9fUV28EZXUU8Qw6piven44l3UoEdWw0eIpo89tSIG3VW+eSUjdkuR/OcQQjgWHHJH2pHN3WBhI75HAiMJDxog== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=J8r2948M01gPZXdN1KPb0ZJ86B5ynXe4ZT3mgB9gzvo=; b=E+nGxvjPtvBdWWSxCxEqvheqoSeWkG7ekM9z2N2jbLri6r63s6POVwr4h6UMp64VBujP9zardeiIEaxmX0d9u280Jsj4/MEN15UFeCCXoDKSaCweyJzfgTOlvku4bvC2KzIQyroEn0BDh7FmHZj4RFl/zRVA9waYWiLUcLtMfII= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=os.amperecomputing.com; Received: from PH0PR01MB7975.prod.exchangelabs.com (2603:10b6:510:26d::15) by CO1PR01MB6677.prod.exchangelabs.com (2603:10b6:303:d8::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9412.7; Tue, 9 Dec 2025 09:46:16 +0000 Received: from PH0PR01MB7975.prod.exchangelabs.com ([fe80::6926:a627:118e:8050]) by PH0PR01MB7975.prod.exchangelabs.com ([fe80::6926:a627:118e:8050%4]) with mapi id 15.20.9412.005; Tue, 9 Dec 2025 09:46:16 +0000 From: Huang Shijie To: mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org Cc: patches@amperecomputing.com, cl@linux.com, Shubhang@os.amperecomputing.com, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, linux-kernel@vger.kernel.org, vschneid@redhat.com, vineethr@linux.ibm.com, kprateek.nayak@amd.com, Huang Shijie Subject: [PATCH v6 1/2] sched/fair: set rq->idle_stamp at the end of the sched_balance_newidle Date: Tue, 9 Dec 2025 17:45:07 +0800 Message-Id: <20251209094508.570049-2-shijie@os.amperecomputing.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20251209094508.570049-1-shijie@os.amperecomputing.com> References: <20251209094508.570049-1-shijie@os.amperecomputing.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SG2PR03CA0092.apcprd03.prod.outlook.com (2603:1096:4:7c::20) To PH0PR01MB7975.prod.exchangelabs.com (2603:10b6:510:26d::15) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH0PR01MB7975:EE_|CO1PR01MB6677:EE_ X-MS-Office365-Filtering-Correlation-Id: 376209b8-cc18-4953-3d16-08de3707cbf1 X-MS-Exchange-AtpMessageProperties: SA X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|52116014|1800799024|366016|38350700014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?jaYx81N/hY3AxYZFklob2zfKOzUgX8iZiQQ017L8Wgj6mQ401HK8WPPoKGCR?= =?us-ascii?Q?YlaxpTdUwcGWvrBL1wna5R4diAEpp3DsRM5Cxn9syq2gk09YhJmMJRyqJwuP?= =?us-ascii?Q?lQbMS46Dagi42Oi7fVVMbYsp69tmIfxcqGfhiAL4HpYeQA281rvhY3R+i5Jn?= =?us-ascii?Q?5XmqjS/CQVAEhqF4ULMtqy3XRY4dfRPPHLySk/SBtsW8HrQG1YWA0DV+Q0T1?= =?us-ascii?Q?ayNU88Nd9VauKSwzVuwPRJiE2bVjMIheu5DrSL6pO5KbuHGGpvbOYRyHc+g7?= =?us-ascii?Q?51ltw+/41q/LoKDqsjqzXpOYUvCjL3kKIbKY99a9SBvJr5mJvTG95hrIqyAO?= =?us-ascii?Q?vOZ3gIZ2T1lw/fdgjGoKj9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?us-ascii?Q?5Bp8SRw+3eCr69TLwLNc6bwBnfHmUhqEcIRELRqgnHfZBMNuUYsauL/312UX?= =?us-ascii?Q?BmQZ6+uFFmFtsw8OBe221EPBkABrAe69eR1iMJtUSeg8WGuvBmwkE/ffNNlF?= =?us-ascii?Q?Syk0AeWCci+yWDcUimH5cHiadj3D6/qkHvmt3xiUjCvazdtK+CGt3cjsJj+3?= =?us-ascii?Q?noOVMeQWDCewTM/kA4F8C9KMKIr3Ot/I1EP0k7sNspJublx9ATJ0BtBhBPUT?= =?us-ascii?Q?6ljU/Pw0wOeU6JPPnXRkRarXamWTgS2LFA7f?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PH0PR01MB7975.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(7416014)(52116014)(1800799024)(366016)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?Wscb/CKQee+rzsh4PLB2v6vKchAJs6ObIoQQKWKTn+hHZ+DncJUwDZ+BVN5r?= =?us-ascii?Q?AgZ53rVNzaHU/hdr3W4jsofxUsD8qO07LCei/gVbemop+vUrdgeESlzLPNH6?= =?us-ascii?Q?nhlFRWmcI0Nhi9ZkHu/605iiQKGeqnY4l1cfSsAp3ZdBUJRSqHBFgGzIAXAX?= =?us-ascii?Q?lobXNat1z3DyuxdD7EvpQsnTdvaKphA5qhuge17X4bLGXrp79SQlSEjYEXf/?= =?us-ascii?Q?6/OmBoXYTSYhPZavkW2E6pxIFwS76osOUS0js7gE/HEXc+rn549n3pWCXl+0?= =?us-ascii?Q?HGF9Wd786UtNNwdtqsUVA/YZZLbKS+betC+Z0fsZ1Vsh4FlIuqBJLNzIii2P?= =?us-ascii?Q?s/nYaWBsaOVpLG62GdPj5CLYKBqwbQO/Q9yxI2E69TzvDLzzpSiPNZ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qcflxWW5J+fn4SoXQa6TTm958qDwuk+00Z3?= =?us-ascii?Q?/4zNsDxaJY3DHC6MB7xKAJcb9Ouz+5yoE5dCOnVk28EdyWmqI0pCdCZhTtHX?= =?us-ascii?Q?DxqWFn5uhQfriJPXH1dqD48J+Q+PkOzLQ7X8l0Ci+mNpSCCROhq5Os48Jt3z?= =?us-ascii?Q?tFRYX2JkgxdwXwunrPV+l/jIuTpkqk+QV3WcvTyFA8W2jKLQoM7X3EB7SrYN?= =?us-ascii?Q?qj3WMB3VSlap9InpwaynPtmgxmnuBy2elQblv+qB394l9++uFuwQ7SaeG3oA?= =?us-ascii?Q?6uyg5ujz7FAxFw25kRk/tpX7JTa90EQScUJOZKlt4AhOXV295MRi4oqggtvd?= =?us-ascii?Q?GPz7AHJPeTizaUq7WK4Dg30=3D?= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: 376209b8-cc18-4953-3d16-08de3707cbf1 X-MS-Exchange-CrossTenant-AuthSource: PH0PR01MB7975.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Dec 2025 09:46:16.2829 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: zKcWohLjNmTcNeGVrH5Pn5Mw3c6A+UevLN+/1N9hqjUdjFFODLvnVC1vo2uGlXsPcqaeOH9VejyFwR3KkrqHNp856JfiKXllVAZ7F9u6RgYTonlE1IQJB8ymSDmU1+hy X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO1PR01MB6677 Content-Type: text/plain; charset="utf-8" In current newidle balance, the rq->idle_stamp may set to a non-zero value if it cannot pull any task. In the wakeup, it will detect the rq->idle_stamp, and updates the rq->avg_idle, then ends the CPU idle status by setting rq->idle_stamp to zero. Besides the wakeup, current code does not end the CPU idle status when a task is moved to the idle CPU, such as fork/clone, execve, or other cases. In order to fix this issue, we want to add a hook(update_rq_avg_idle()) in the enqueue_task(). With this hook, if a task is moved to the idle CPU, it will update the rq->avg_idle. Unfortunately, this hook is also called in the newidle balance: sched_balance_newidle() --> sched_balance_rq() --> ... --> enqueue_task() If we still set rq->idle_stamp at the beginning of sched_balance_newidle(), the rq->avg_idle will not be updated correctly. In order to make it work correctly, save the idle_stamp at the beginning of sched_balance_newidle(). If newidle balance cannot pull any task, set the saved value for rq->idle_stamp. With this method, the newidle balance still work correctly, and the hook in enqueue_task() also works correctly. Signed-off-by: Huang Shijie --- kernel/sched/fair.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 1855975b8248..c3b4895f8e50 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -12865,6 +12865,7 @@ static int sched_balance_newidle(struct rq *this_rq= , struct rq_flags *rf) u64 t0, t1, curr_cost =3D 0; struct sched_domain *sd; int pulled_task =3D 0; + u64 idle_stamp; =20 update_misfit_status(NULL, this_rq); =20 @@ -12880,7 +12881,7 @@ static int sched_balance_newidle(struct rq *this_rq= , struct rq_flags *rf) * for CPU_NEWLY_IDLE, such that we measure the this duration * as idle time. */ - this_rq->idle_stamp =3D rq_clock(this_rq); + idle_stamp =3D rq_clock(this_rq); =20 /* * Do not pull tasks towards !active CPUs... @@ -12992,10 +12993,13 @@ static int sched_balance_newidle(struct rq *this_= rq, struct rq_flags *rf) if (time_after(this_rq->next_balance, next_balance)) this_rq->next_balance =3D next_balance; =20 - if (pulled_task) + if (pulled_task) { this_rq->idle_stamp =3D 0; - else + } else { + /* Set it here on purpose. */ + this_rq->idle_stamp =3D idle_stamp; nohz_newidle_balance(this_rq); + } =20 rq_repin_lock(this_rq, rf); =20 --=20 2.40.1 From nobody Tue Dec 16 21:09:29 2025 Received: from CY7PR03CU001.outbound.protection.outlook.com (mail-westcentralusazon11020138.outbound.protection.outlook.com [40.93.198.138]) (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 9538E2FD1A1 for ; Tue, 9 Dec 2025 09:46:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.198.138 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765273591; cv=fail; b=b1/r/iNc6LnjK5ckgAyRmJXGeI/kOvddChcnaJuykgQ0mdvSezjVB3iak+9U/JbKixtBCugrp0t8pVx+xgPSdst+AAYJ64f2fVPGjFDbDfMao3snzRjHFs4QnfOQdzrt/gLBs72X/H32kebE02dndyMyCXeEG/xHUdaEMbgwCiM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765273591; c=relaxed/simple; bh=82R6SrMgnXcmeLSw9G2xDkOjkTE6lm68CDwZbxrIe80=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=t920mgHsuYffo32QHY59Mr/U1uekUxpjxuRB6hRiSU1eQgx2xZheQSzJOm/OCgkK6v6rqcyC2Lb/qDxrdfiXrBESLgss/N0lUm4LdrV60gV86zts9FilPnfFLUWkx/GV3vpPFDRMbc2pqMeHBzZBKBWp6/MFc6n7IbpqFGPSc+g= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=os.amperecomputing.com; spf=pass smtp.mailfrom=os.amperecomputing.com; dkim=pass (1024-bit key) header.d=os.amperecomputing.com header.i=@os.amperecomputing.com header.b=tw3SDV/3; arc=fail smtp.client-ip=40.93.198.138 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=os.amperecomputing.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=os.amperecomputing.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=os.amperecomputing.com header.i=@os.amperecomputing.com header.b="tw3SDV/3" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=IWfKfQRyRBTKDuM2erx7JIkgtTYbabt8YnC/982q5Ye4cgXuTG9jw6M3fvTrdTK14ANREEzxlv818d+DQbGe/O3Siambb3b2PCNuoL0Rei8T7+ht1m/doCov9rIsfJcLwDZ9h2/V29AsXIJVSIqffD+lM3+Yp6OMFuY6ffujv36YBj7LVD5pdHxDCVzbQLHJp2u/YQaRSet5B6k53+PlMHqcMi4c9sn+g/PV4TUx0JJvf7MBdgjg90CSNpW2Xb+yR8AXkg6XkqKs0cVZjWi4tfx8uraYZNNF/4fb9emM2vQvS8MNDIDpbTDD+1NaB0dw8kKP51VUr7FOnFLw94fkcQ== 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=m2eiIPAXVhzK+Oj56QEWashonR/mFhEYhQYcOWvNrt4=; b=qvlhdJhh/sHP1QYcj3py9bttxOSWWpYOXo+FDdLaaBlILkIcW+t+QyJKb4oXM/PV/P6ZtQebyUccHW82XuQfvdHZBNe0JETksTMIRxaLcyThE33DTGnDVDOBBLeD213j9BbGinWQLn78incbefvYbFCj3bljagFD6S1menkiEPAntVtqJUUwkY6NgNKxlDpiKokVHRQZ0j+ohJVGlXC4V1Z2OEHFzTRaxa6JENiizw7DCjrPo2XiHhNu58Qu1dHWs2RvaoaxQvXxuYMWQsUpawbrfwbnF5FQY+ZA8CNkVR6fUtGBvGTQw7PMx1SHrnx7xaTqWGlEuygyonuYjMwJQg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=m2eiIPAXVhzK+Oj56QEWashonR/mFhEYhQYcOWvNrt4=; b=tw3SDV/3SmKtTb6Z0EBHEFZtxZf7i105AHCN8VJcaYM1Cuu84QCPQAW8BLs8MEFpZLlX+zlvVQJnLGCs9wdlv38+Kzdnel6BtgzAcbeNG+ivJjLrk2VA2b7B4WCn56UOAFWJvSb4lnWvXUESbkwVHeO1geB8HMB5PlUCfgqQBJ8= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=os.amperecomputing.com; Received: from PH0PR01MB7975.prod.exchangelabs.com (2603:10b6:510:26d::15) by CO1PR01MB6677.prod.exchangelabs.com (2603:10b6:303:d8::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9412.7; Tue, 9 Dec 2025 09:46:27 +0000 Received: from PH0PR01MB7975.prod.exchangelabs.com ([fe80::6926:a627:118e:8050]) by PH0PR01MB7975.prod.exchangelabs.com ([fe80::6926:a627:118e:8050%4]) with mapi id 15.20.9412.005; Tue, 9 Dec 2025 09:46:27 +0000 From: Huang Shijie To: mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org Cc: patches@amperecomputing.com, cl@linux.com, Shubhang@os.amperecomputing.com, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, linux-kernel@vger.kernel.org, vschneid@redhat.com, vineethr@linux.ibm.com, kprateek.nayak@amd.com, Huang Shijie Subject: [PATCH v6 2/2] sched: update the rq->avg_idle when a task is moved to an idle CPU Date: Tue, 9 Dec 2025 17:45:08 +0800 Message-Id: <20251209094508.570049-3-shijie@os.amperecomputing.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20251209094508.570049-1-shijie@os.amperecomputing.com> References: <20251209094508.570049-1-shijie@os.amperecomputing.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SG2PR03CA0092.apcprd03.prod.outlook.com (2603:1096:4:7c::20) To PH0PR01MB7975.prod.exchangelabs.com (2603:10b6:510:26d::15) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH0PR01MB7975:EE_|CO1PR01MB6677:EE_ X-MS-Office365-Filtering-Correlation-Id: 1b127f77-bfed-4951-0deb-08de3707d270 X-MS-Exchange-AtpMessageProperties: SA X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|52116014|1800799024|366016|38350700014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?pU1GOSx2CJVOg1ETDD5EFWMmMiWkQktOllyuyJ+bC2AZKPE/vj62GSSiaCAB?= =?us-ascii?Q?aWvCYIhhcg8wt/t42ooYaK7uTLEHvmZTm7vMMM9rwFcHqk1lYDfCHpbjM1G1?= =?us-ascii?Q?AWModhaYYmcyuZszdNgG5T+D/Qg61tmP8v55Lq7QgJEZ+m/US6dBbcHuKcPO?= =?us-ascii?Q?qfuGjj6kGtCaXu9Hj9uPMFVRJbsTGpaQQqAxCRVZEP0qN06Q95Zr/4CwjhgX?= =?us-ascii?Q?D4P6PSeRxoFR0E2AUal6QpYz1j1E30E5T4O+pc1mHEl5t5cpHpd4DUEDYSex?= =?us-ascii?Q?Ea2vV44b0G0/w3E7yOAVFRFYZNwi1Kx5w0Ket6vLTUEwdr5/9IW8Nc+vLheu?= =?us-ascii?Q?QVRa0hRG5MgPibabwJutX4toXkCjKhFnBLYhFrNA74TB6FWwWQw5zBo9BO38?= =?us-ascii?Q?DJsShI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TPu9Jk3FJFjy74ey?= =?us-ascii?Q?9OLqyhtSwIQMNDHc9y6m14xQtS3u+6oIBjQrJf7AttU7GjpBrPdhPkojmonl?= =?us-ascii?Q?SyHGeS8C7iyY38iDILmUYznB5NRKXYqMsI0NajVFJ/uUF8Ol/L6QuXSCi9lS?= =?us-ascii?Q?werlHnB0p3Ba+1zzvmi0hhdddxsha909GInBw6L2zGxEqiyndteWdit/Idks?= =?us-ascii?Q?0zl6BXydejF89pjEehjv4sCDPlDrT7sl4mpL?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PH0PR01MB7975.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(7416014)(52116014)(1800799024)(366016)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?GTRnUICHTOWiBSZFMLLOipVwwPDcok/cbMUbF1afb92l01eGiYr7qcq+pTs0?= =?us-ascii?Q?pdIhx+iqRgTyAW7Z60XNKrD3gxEqu7bzUtgSpXe8KIgqmMiwxPjyqr00yPYV?= =?us-ascii?Q?RgtxQsu+UgQ56xEIqalhMl1CpEtmeUao+PAun19MB7xTW9wasrL4rn0HCmOg?= =?us-ascii?Q?q/6I7KKX9k0jZ/Zm2TiGMI8ZSPNeX0qZpuWJpGS85EqqD03hkIrudJ0JSTfB?= =?us-ascii?Q?t6mif4jCwJgnV5lJIQPKwCScBOdn0wdg6pJDOoH+ClOsh/6wz8Q7heNhRZXi?= =?us-ascii?Q?TYxGVQuaRWFUs7pv+gl5riJ8Cx7/yZWddIapSRl8d+lRJxktgxsHB4DPL0bF?= =?us-ascii?Q?8R8e5zYLmTz/2yUSqjDBA1q0eihQ4Wt9yPKjl1T6pstWsMnOYU3A7NBMJDG7?= =?us-ascii?Q?vP25NeG5h8E3ByUxpSzJuOjW6x3flIh9GpCnQc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aMs8n0LhFOh0q0idEaw4zNs+CVIBmXx1vz0Pw5l1xGh?= =?us-ascii?Q?bDTHr2YM4vV7JqrIC/gMaFmjUZUMEjOSP4f70Zl87/CkXyqXk89Wgk6UecyG?= =?us-ascii?Q?2xIwT+4IeCGNbwVrUyPJFme9Uf5fHhiWplEvc8kFVDms8HWLUzh6+GHpxFXF?= =?us-ascii?Q?8Ji7xIH0lEButOHxyrpFmABvUipxiFPGdfntz2qanpFFeVqsGvx95K8k5oRV?= =?us-ascii?Q?1X7KweTlE6xTstLlRtDRAjVaAaekims9HBtMuXRbJFBFSnupMhAWvdvr2oKz?= =?us-ascii?Q?Q3c0vOSBPSPADnO+VbHI2Xk=3D?= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1b127f77-bfed-4951-0deb-08de3707d270 X-MS-Exchange-CrossTenant-AuthSource: PH0PR01MB7975.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Dec 2025 09:46:27.1585 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: vwsB4uj9cXXbd4Lj/tgjODuIDEPbikK2j2Fbuf4L0FQb+H2rwlyT8rQmWW/u6y6oDtpsuiaiP1diKSwEq0xuxpkkJZ3zMpxWX4dahtmFVG2VIVF4PY9mh+kzGtxy9W1a X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO1PR01MB6677 Content-Type: text/plain; charset="utf-8" In the newidle balance, the rq->idle_stamp may set to a non-zero value if it cannot pull any task. In the wakeup, it will detect the rq->idle_stamp, and updates the rq->avg_idle, then ends the CPU idle status by setting rq->idle_stamp to zero. Besides the wakeup, current code does not end the CPU idle status when a task is moved to the idle CPU, such as fork/clone, execve, or other cases. In order to get more accurate rq->avg_idle, we need to update it at more places(not only the wakeup). This patch introduces a helper: update_rq_avg_idle(). And uses it in enqueue_task(), so it will update the rq->avg_idle when a task is moved to an idle CPU at: -- wakeup -- fork/clone -- execve -- idle balance -- other cases Signed-off-by: Huang Shijie --- kernel/sched/core.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 9f10cfbdc228..2e3c4043de51 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2078,6 +2078,21 @@ unsigned long get_wchan(struct task_struct *p) return ip; } =20 +static void update_rq_avg_idle(struct rq *rq) +{ + if (rq->idle_stamp) { + u64 delta =3D rq_clock(rq) - rq->idle_stamp; + u64 max =3D 2*rq->max_idle_balance_cost; + + update_avg(&rq->avg_idle, delta); + + if (rq->avg_idle > max) + rq->avg_idle =3D max; + + rq->idle_stamp =3D 0; + } +} + void enqueue_task(struct rq *rq, struct task_struct *p, int flags) { if (!(flags & ENQUEUE_NOCLOCK)) @@ -2100,6 +2115,8 @@ void enqueue_task(struct rq *rq, struct task_struct *= p, int flags) =20 if (sched_core_enabled(rq)) sched_core_enqueue(rq, p); + + update_rq_avg_idle(rq); } =20 /* @@ -3645,18 +3662,6 @@ ttwu_do_activate(struct rq *rq, struct task_struct *= p, int wake_flags, p->sched_class->task_woken(rq, p); rq_repin_lock(rq, rf); } - - if (rq->idle_stamp) { - u64 delta =3D rq_clock(rq) - rq->idle_stamp; - u64 max =3D 2*rq->max_idle_balance_cost; - - update_avg(&rq->avg_idle, delta); - - if (rq->avg_idle > max) - rq->avg_idle =3D max; - - rq->idle_stamp =3D 0; - } } =20 /* --=20 2.40.1