From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2075.outbound.protection.outlook.com [40.107.220.75]) (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 A8953145A18 for ; Tue, 21 Jan 2025 08:09:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.75 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737446993; cv=fail; b=Q0MSZnzE4UXG7LXq6cbPbLAu3QvkUW9X9yczMj466zd+6OJNt/XaLCFfvte2m3FKAVlhw1v+jycCZbEtylK8uPNjfetQ8BgU/IA6LZutzMY6LhwJqHY5aiYFeWj9Kq1fqkoI/VAUpwmhmC30hufjqK+/uA0ninl/jdF/VaAkgnA= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737446993; c=relaxed/simple; bh=0zsWnTxF3InP2G8RZSkLY2BROupA3fxRWQ03XDlhxz8=; h=Message-ID:Date:MIME-Version:Subject:To:CC:References:From: In-Reply-To:Content-Type; b=mu0cj4wrY+s35+kcescml/FhC9PxbEsyiCnHIhBVvEmc2sxmIOYQ26LhLscWHANH5v1NSiuTUHQoMCPZrMpU6XgLlkm4KPKVj7beeLoo/p+85MNMbTaq4OzJHpBGw5xydMqbFBAe8pQv8UJA42MLpS6JX4MqOFreJQhuaaBARQY= 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=rOdZARZQ; arc=fail smtp.client-ip=40.107.220.75 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="rOdZARZQ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=leid+wFFKLVrzodMXywNId2xoRh0HQLaQ+O+gVtdFDsj8ESplALda3j+xGMasfRQMQx6bPKVhAyuAgka431rlKFB0FAjseycyIiqhZSbk6s3GE66dT1sOZ82Y8vImQVu3rR5zXSMimFlWYDzpjV+RKC8YlMWT1f29md4h0rTZ+6Bh7/+V52wzGJROzTMxfPUwvc6yP0MBzTm6USt1HDBXwqhr9zUXT39vcZwAPue3EsBq26QlW96DGkx9Rtfj1KpO8olrD28Y8sDRhc7x86apZZK5TS8Pmd+aGwlZZo0ZiCW+NsH3TxdWZJ5ZQ+IZr5bQsIwgfMYVumlXCP92RtGlg== 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=O4639PyRiXvydZu8qs/ZQhKIQ530QBjeXuwfQrq4eZs=; b=bY1vMv7WZcx8csS+qq5W9/f1LQ7BR7boSj9j4Uv9UrWN09FotVdRdPIasJfD8CEZKLFlsIbOjGuOKKVZzVUPO9nL3VeVxqMYsivWrrUE6V46xloD5HIv/s0t53IZqbtOFuIz5KcFoNxVYMwQZPWKamI2SqHMOtAA+KydCFwkhNdw/QhqN/NVFjkROWVN5rW5rm+fS1+vlU/9s+OjH7OZ/OBnbxU/Mld3erxaZ5+ImJncfUOjIu+W3iYtJqKEc50PoQ25oS6AuEavVIo6N3wCWYZlAWtyQqdsSsW08edXu7g+KBTaF8IHHxy3ZT/IIwJRQZ+YWjWhuG3HZYmCol5FOA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=linaro.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=O4639PyRiXvydZu8qs/ZQhKIQ530QBjeXuwfQrq4eZs=; b=rOdZARZQaipqwaBeo90xv1zpMwNF46AyddjdBq5+MyqP7/CXdZBNkoOh0YZIYgkSF+QbopLIcVCX8REYHtB1mH2ErU6xDagZ+hPszrI50XkJ/QomQjJbD5DWWwtjImHy9G2ZYgu85nYKQETnCur4WLeOIUiH5pgh6qqR6B76qhA= Received: from PH2PEPF00003856.namprd17.prod.outlook.com (2603:10b6:518:1::78) by SJ0PR12MB6759.namprd12.prod.outlook.com (2603:10b6:a03:44b::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8377.16; Tue, 21 Jan 2025 08:09:46 +0000 Received: from SN1PEPF000397B2.namprd05.prod.outlook.com (2a01:111:f403:f90f::3) by PH2PEPF00003856.outlook.office365.com (2603:1036:903:48::3) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8356.21 via Frontend Transport; Tue, 21 Jan 2025 08:09:46 +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=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SN1PEPF000397B2.mail.protection.outlook.com (10.167.248.56) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8377.8 via Frontend Transport; Tue, 21 Jan 2025 08:09:46 +0000 Received: from [10.136.36.22] (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 21 Jan 2025 02:09:43 -0600 Message-ID: Date: Tue, 21 Jan 2025 13:39:40 +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] sched/fair: Fix inaccurate h_nr_runnable accounting with delayed dequeue To: Vincent Guittot CC: Ingo Molnar , Peter Zijlstra , Juri Lelli , , "Dietmar Eggemann" , Steven Rostedt , Ben Segall , Mel Gorman , "Valentin Schneider" , "Gautham R. Shenoy" , Swapnil Sapkal References: <20250117105852.23908-1-kprateek.nayak@amd.com> <79cef0b2-4ed2-4141-9a66-150d04b82476@amd.com> Content-Language: en-US From: K Prateek Nayak In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SN1PEPF000397B2:EE_|SJ0PR12MB6759:EE_ X-MS-Office365-Filtering-Correlation-Id: 22cb84ee-78e5-4ba6-ed63-08dd39f2f849 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|36860700013|376014|1800799024|7416014; X-Microsoft-Antispam-Message-Info: =?utf-8?B?Q29TRWFyR3VINGdYOGxveW4vL3QxTEVEc1lHeDhYalEvcGhLNGFXMVdlQ3JH?= =?utf-8?B?TGlKRENmQUxsMUI2eXJKMFQrT2d2RGQ5YndsN3hlVDlHMW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yaXErR1h1cmdhbVNNaHBGUFlDSkxLQUpITG1yWHZHZkJuZUpq?= =?utf-8?B?amF0RDZsTERYdGR5c0UzVUdzeEZyZ2xpWjl4TlNzdGxkeVg5Z1M5RUZ6ZUNF?= =?utf-8?Q?P+5+ZnkMptIxW5Zn/WoLVK+kZB0l7s=3D?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(36860700013)(376014)(1800799024)(7416014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jan 2025 08:09:46.6160 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 22cb84ee-78e5-4ba6-ed63-08dd39f2f849 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=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SN1PEPF000397B2.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6759 Hello Vincent, On 1/18/2025 2:00 PM, Vincent Guittot wrote: > On Fri, 17 Jan 2025 at 16:59, K Prateek Nayak wrote: >> >> Hello Vincent, >> >> On 1/17/2025 6:55 PM, Vincent Guittot wrote: >>> Hi Prateek, >>> >>> On Fri, 17 Jan 2025 at 11:59, K Prateek Nayak wrote: >>>> >>>> set_delayed() adjusts cfs_rq->h_nr_runnable for the hierarchy when an >>>> entity is delayed irrespective of whether the entity corresponds to a >>>> task or a cfs_rq. >>>> >>>> Consider the following scenario: >>>> >>>> root >>>> / \ >>>> A B (*) delayed since B is no longer eligible on root >>>> | | >>>> Task0 Task1 <--- dequeue_task_fair() - task blocks >>>> >>>> When Task1 blocks (dequeue_entity() for task's se returns true), >>>> dequeue_entities() will continue adjusting cfs_rq->h_nr_* for the >>>> hierarchy of Task1. However, when the sched_entity corresponding to >>>> cfs_rq B is delayed, set_delayed() will adjust the h_nr_runnable for the >>>> hierarchy too leading to both dequeue_entity() and set_delayed() >>>> decrementing h_nr_runnable for the dequeue of the same task. >>>> >>>> A SCHED_WARN_ON() to inspect h_nr_runnable post its update in >>>> dequeue_entities() like below: >>>> >>>> cfs_rq->h_nr_runnable -= h_nr_runnable; >>>> SCHED_WARN_ON(((int) cfs_rq->h_nr_runnable) < 0); >>>> >>>> is consistently tripped when running wakeup intensive workloads like >>>> hackbench in a cgroup. >>>> >>>> This error is self correcting since cfs_rq are per-cpu and cannot >>>> migrate. The entitiy is either picked for full dequeue or is requeued >>>> when a task wakes up below it. Both those paths call clear_delayed() >>>> which again increments h_nr_runnable of the hierarchy without >>>> considering if the entity corresponds to a task or not. >>>> >>>> h_nr_runnable will eventually reflect the correct value however in the >>>> interim, the incorrect values can still influence PELT calculation which >>>> uses se->runnable_weight or cfs_rq->h_nr_runnable. >>>> >>>> Since only delayed tasks take the early return path in >>>> dequeue_entities() and enqueue_task_fair(), adjust the >>>> h_nr_runnable in {set,clear}_delayed() only when a task is delayed as >>>> this path skips the h_nr_* update loops and returns early. >>>> >>>> For entities corresponding to cfs_rq, the h_nr_* update loop in the >>>> caller will do the right thing. >>>> >>>> Fixes: 76f2f783294d ("sched/eevdf: More PELT vs DELAYED_DEQUEUE") >>> >>> You probably mean c2a295bffeaf ("sched/fair: Add new cfs_rq.h_nr_runnable") >> >> You are right! I had done a git blame on set_delayed() ad landed at >> commit 76f2f783294d but you are right, it should be c2a295bffeaf >> ("sched/fair: Add new cfs_rq.h_nr_runnable") when the accounting was >> inverted to account runnable tasks. Thank you for pointing that out. >> >>> Before we were tracking the opposite h_nr_delayed. Did you see the >>> problem only on tip/sched/core or also before the rework which added >>> h_nr_runnable and removed h_nr_delayed >> >> The problem is on tip:sched/core. I did not encounter any anomalies on >> 76f2f783294d ("sched/eevdf: More PELT vs DELAYED_DEQUEUE") >> >> "h_nr_delayed" was only adjusted in dequeue_entities() for "!seep && >> !delayed" which would imply migration or a save + restore type operation >> and the whole "h_nr_delayed" adjusting was contained in >> {set,clear}_delayed() for delayed dequeue, finish delayed dequeue, and >> requeue. So I was looking at it wrong when I was investigating on commit 76f2f783294d ("sched/eevdf: More PELT vs DELAYED_DEQUEUE") h_nr_delayed can never be larger than h_nr_running (h_nr_queued upstream) since the number of delayed tasks can never cross number of tasks queued below the given cfs_rq but with the following: diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 97ee48c8bf5e..8e713f241483 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -7145,6 +7145,7 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags) cfs_rq->h_nr_running -= h_nr_running; cfs_rq->idle_h_nr_running -= idle_h_nr_running; cfs_rq->h_nr_delayed -= h_nr_delayed; + SCHED_WARN_ON(cfs_rq->h_nr_delayed > cfs_rq->h_nr_running); if (cfs_rq_is_idle(cfs_rq)) idle_h_nr_running = h_nr_running; @@ -7185,6 +7186,8 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags) cfs_rq->idle_h_nr_running -= idle_h_nr_running; cfs_rq->h_nr_delayed -= h_nr_delayed; + SCHED_WARN_ON(cfs_rq->h_nr_delayed > cfs_rq->h_nr_running); + if (cfs_rq_is_idle(cfs_rq)) idle_h_nr_running = h_nr_running; -- I can again consistently hit the warning without the fix on 76f2f783294d ("sched/eevdf: More PELT vs DELAYED_DEQUEUE") I think that the original "Fixes:" tag is indeed right. >> >>> >>> I'm going to have a closer look > > Your fix looks good to me. I also run some tests after re-adding > h_nr_delayed and checking that h_nr_queued = h_nr_runnable + > h_nr_delayed after each update and I didn't get any warning with your > patch whereas I got one during boot without it (but none after that > during my tests) Could it be the case that h_nr_delayed counts a tiny bit higher than the actual number and h_nr_runnable counts a tiny bit lower by the same amount and they both correct each other to give the correct h_nr_queued? > > Thanks for catching this > > Reviewed-by: Vincent Guittot Thank you for reviewing the patch! > >> >> Thank you! >> >>> >>> >>>> Reviewed-by: Gautham R. Shenoy >>>> Tested-by: Swapnil Sapkal >>>> Signed-off-by: K Prateek Nayak >>>> --- >>>> >>>> [..snip..] >>>> >> >> -- >> Thanks and Regards, >> Prateek >> -- Thanks and Regards, Prateek