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 7F95948B364 for ; Tue, 5 May 2026 16:10:44 +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=1777997446; cv=fail; b=qPkl9K+279pBADbcgrOYPBcLYGdjWDjuX0TYfE2KMtif1bANJFQexrG4Pg7EPFVDuq7FeAxk6xB7zx2bjrjZawimtZXUn7VDxPsk9Izg/7zIBZclxJfO3a4JNvGlBDMQD5Zic6tqtnuUn8Xfl3utFGfTSBYT7KlpJaXRnqgcNbQ= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777997446; c=relaxed/simple; bh=yZzwzZMWRfPXYm/XQKYIAh0zjCYj1s+2NfLFl4KKHS8=; h=Message-ID:Date:MIME-Version:Subject:To:References:From: In-Reply-To:Content-Type; b=n9F8b7GJG99Chd3U4Py8WvZfKYSid7/5ckvebUVvcnyIbOY41d7EQ0CtfonZeXw4/CBScpzLEZrqiXKJRqAGytq1KrtOz7XU7JPj4K7Hdi/Ei4d8Y2Voh7BzcdCY7dfgxp8dmDPBHt2zi0gR2J7OgAXTVyIZdsDLoG55LpGITuo= 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=SNZQVhOn; 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="SNZQVhOn" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=OkkPd7dbKLbQstXmhKdJT+F0Ai00Q3uRK3oCUfc8phzAwY2mKBTsum9dDPxpU7khf5NAWyA49HMXXVJe3g5eGuDsqsERNKm0eJ5k1wt/31Z9R1c9nYth9QRQmL88k9pb1qLroREN5qcF4UtkQWLep2o/fl49yO1aBfBa11Ng79/sZG/g48O6H1H5z/dfImzgmv4zOYxt25tEfzSouBKlqFy/N6HRtRDFNYRDFVl1FpYcNp2PbfN2VCVcmklqsiF4FOrynERldmhdB4MVBHhgJvddwtgycX7gkBHZbgVTi0jjNlTwxQaQ4UgwuWpADY0SD3O4Cy8o7RzI6FU/vR/BZg== 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=vUhgeJKL0ZavrfJbbHyFFZ4nFFiDvVedyonRkS28eps=; b=LcqwepJRIxbZQcQUuWxXf/5V24P8aCR08bzPhxE8Q7DZggaD99jPKkRWx/0aWogmRuKXC4jtLP311S2Q/bllkPcheHrDFM2kNcqqZkJNEiYLYoDjRDP6UHKNFJW4fPMTJZ3fbKezIDhvfUfhNPYmXZKRU6tb91Plk+kb0CZOD3BkpUipVD38EpiRoLndYqkGyxLjASgHUjqBdqa494K/WgteYS51qKEFBluAVsUXf2YcQgEljPRk2lZlUxov+A+FjVcODhIcQ6lOby4dS5WZgR7lf1BNi+iGvMhZZXZ8q0PyrUWtOt2H8pv6pCx6XggjnFb1TsdJaitiZeI6D4eIww== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=baidu.com 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=vUhgeJKL0ZavrfJbbHyFFZ4nFFiDvVedyonRkS28eps=; b=SNZQVhOnbW6XA5ToPFQGDp0x5pL76mMWI9UxCOXgTjbJUPGrXcJki69+Wft9xzqGU2uB4s3hcBedQHxGX4KXfZyxgGvYfRbQ9URQKD4dL6Ahw+FK503k55oI/kzLXXoObk7k7OR2WOy0QrZtSok9kwL32hjDR91tD4SDD+90yKs= Received: from PH5P220CA0006.NAMP220.PROD.OUTLOOK.COM (2603:10b6:510:34a::7) by SA1PR12MB9470.namprd12.prod.outlook.com (2603:10b6:806:459::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9870.25; Tue, 5 May 2026 16:10:38 +0000 Received: from CY4PEPF0000EE34.namprd05.prod.outlook.com (2603:10b6:510:34a:cafe::2f) by PH5P220CA0006.outlook.office365.com (2603:10b6:510:34a::7) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9891.15 via Frontend Transport; Tue, 5 May 2026 16:10:38 +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 CY4PEPF0000EE34.mail.protection.outlook.com (10.167.242.40) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9891.9 via Frontend Transport; Tue, 5 May 2026 16:10:37 +0000 Received: from SATLEXMB03.amd.com (10.181.40.144) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.17; Tue, 5 May 2026 11:10:24 -0500 Received: from satlexmb07.amd.com (10.181.42.216) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 5 May 2026 11:05:55 -0500 Received: from [172.31.184.125] (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server id 15.2.2562.17 via Frontend Transport; Tue, 5 May 2026 11:05:53 -0500 Message-ID: <693ebc80-9f8e-48b7-82cc-aa513adbbe6e@amd.com> Date: Tue, 5 May 2026 21:35:53 +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/swait: Reduce lock contention in swake_up_all To: lirongqing , Ingo Molnar , "Peter Zijlstra" , Juri Lelli , "Vincent Guittot" , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , References: <20260505090431.2423-1-lirongqing@baidu.com> Content-Language: en-US From: K Prateek Nayak In-Reply-To: <20260505090431.2423-1-lirongqing@baidu.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit Received-SPF: None (SATLEXMB03.amd.com: kprateek.nayak@amd.com does not designate permitted sender hosts) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000EE34:EE_|SA1PR12MB9470:EE_ X-MS-Office365-Filtering-Correlation-Id: 59c23bb3-884c-4fce-b70b-08deaac0d864 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|82310400026|7416014|376014|36860700016|921020|56012099003|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: P6syqgcu7yhO6q9YzrL3sP/RcaAbt0KN+OYW3htyFsisFg2DVn4OUTEZG+IXn7kRo3nberQmankiD1VQMjrixhAFuX5UyuCmadRR0kf2vXtrWTGboJWQ/aNKjml5ZBazFJJr1tiH8LXaDmKL+BwLQnfwmw7Pq0sNNu/7bmUPwXVhDRgL9qt24Z7L4ElEenjTJ6mTswALEGOf+5qFsyw2nF1VO2mlv/y5Ebmx9O8z583zLhhwoYZXG1ePsrW42h5V73NtuhoVLL0K3CyB0fAoGDSH8hb97DOTPyhiANPxzvXbLjDfJoMl7bU+1Vxaa4jCW1kBnAgCPYl5g7rR8LqcGnNIHU+2Vpel859T11bkP8zAhXR/jRzq92f7eZHDxl7iiLX3Rwr7pwVVEzjJKfzmMkCC8jAcfB3S38SAC21KZYwrBU3/4CXNOSQI8kZEPzGN34spqbl+yxNK783hiHuupzHF56JTIxFDVlZw1oN0zIkX94WeoqveC9h7FuAtACos/5oJRU+mqIpOwNMJYI/SqxNNv4a5ormOloHzWuyKjVR6z2Jn+nmQ/OsQIvk1MzrinFdsxArbmale5pJef4NIHkivt8mWECEVkF5YwwFirnHQ09OKaD6ycevm8wQAdy5Gs3TLoDHb1W7B6mE2Bg2evbYpfxWpQPcaTcd1HMUheVvt4WjYQST6U/WmMpfSzu9heyVMftiX49lJc3w2H2S5L9J9rv81wO6LJggZ0neyjxHv2SKYPLXoNct+/AO1oYO49N4Q7RDymHenHP1/qFRl/w== 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)(1800799024)(82310400026)(7416014)(376014)(36860700016)(921020)(56012099003)(18002099003)(22082099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: j0FbhNrdlLveXIgZzHlZR8wfxeYnuVOGH+S8FzBsebzpaXACLwV9dPQO02vmIQnjs1xu6W6UaV2VgHe22Ye+1ko5TTJFeQ3up1PD7ofodsnKNQAQmfir+19ArN8ewdZnyWwA1q0aWg+0oKFfYCQTaesZVn8z5FMXTpE4Pg/9QGyxEunyoI7Yfw3GCPiJ5N9asBCLny1ND0cPti7n/b+/vEzkmMoaQETcSz/s9FpF45VA0kekw7SHaYZNP1OuSlfeG9UXDbQXBqy6va3WKJQ1IY1WNqNMn1qamJTUvYy0gQdmn5PwW2nxxWogl+/oNAKTvyW+WAbTeHOv3QtSZLnct6PHpg8oXFxoXbeut8Y+FYprNHtsHJZ9w4H2F6kvJbt/i1qTH7+se1+B2nZxCyjUEQM2cs91o494S+sdW7OUTMdcroqBevwCFmLlEYG6/ivw X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 May 2026 16:10:37.3238 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 59c23bb3-884c-4fce-b70b-08deaac0d864 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: CY4PEPF0000EE34.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB9470 Hello Li, On 5/5/2026 2:34 PM, lirongqing wrote: > From: Li RongQing > > The entire task list have been moved a local list under the lock, > it is unnecessary to hold the lock to wake tasks, This reduces lock > operations from O(n) to O(1). > > Move list_del_init before wake_up_state to prevent potential > use-after-free if the woken task exits immediately and releases > its memory. > > Signed-off-by: Li RongQing > --- > kernel/sched/swait.c | 10 ++-------- > 1 file changed, 2 insertions(+), 8 deletions(-) > > diff --git a/kernel/sched/swait.c b/kernel/sched/swait.c > index 0fef649..ee4e658 100644 > --- a/kernel/sched/swait.c > +++ b/kernel/sched/swait.c > @@ -66,19 +66,13 @@ void swake_up_all(struct swait_queue_head *q) > > raw_spin_lock_irq(&q->lock); > list_splice_init(&q->task_list, &tmp); > + raw_spin_unlock_irq(&q->lock); > while (!list_empty(&tmp)) { > curr = list_first_entry(&tmp, typeof(*curr), task_list); > > - wake_up_state(curr->task, TASK_NORMAL); > list_del_init(&curr->task_list); > - > - if (list_empty(&tmp)) > - break; > - > - raw_spin_unlock_irq(&q->lock); > - raw_spin_lock_irq(&q->lock); > + wake_up_state(curr->task, TASK_NORMAL); So I'm not fully convinced this is safe. Quick scenario I can think of is: CPU0: swake_up_all() CPU1: Signal task "curr" ==================== ======================== swake_up_all(q) ... list_splice_init(&q->task_list, &tmp); raw_spin_unlock_irq(&q->lock); while (!list_empty(&tmp)) { curr = ...; /* Task curr gets a signal */ ======> Interrupted wake_up_task(curr) /* same as curr */ <====== curr switches in finish_swait() list_del_init(&curr->task_list) __list_del_entry(&curr->task_list.prev, &curr->task_list.next) next->prev = prev; prev->next = next; INIT_LIST_HEAD(&curr->task_list) WRITE_ONCE(curr->task_list.next, &curr->task_list); ========> Interrupted /* * At this point curr->task_list, looks like: * * curr->task_list.next = &curr->task_list * curr->task_list.prev = &tmp */ <===== Interrupt return list_del_init(&curr->task_list); __list_del_entry(&curr->task_list.prev, &curr->task_list.next) next->prev = prev; /* Write &tmp back to curr->task_list.prev */ prev->next = next; /* Writes tmp's next as curr's list head */ INIT_LIST_HEAD(&curr->task_list) WRITE_ONCE(curr->task_list.next, &curr->task_list); WRITE_ONCE(curr->task_list.prev, &curr->task_list); So at this point, your list looks like: tmp: prev = /* tail of list */ next = &curr->task_list curr->task_list: prev = &curr->task_list next = &curr->task_list actual_next: prev = &tmp next = /* Next element */ ... which seems like a list corruption unless I'm missing something. I think the wakeup can be done outside of the "&q->lock" but the list removal, even on the tmp list, has to be synchronized by &q->lock at the very least but I think there is some ordering required wrt wakeup and the list removal. I'll let others comment if there are more subtleties involved wrt the task wakeup itself - perhaps there are cases where the task wakes up, decides to wait in an exclusive mode for a condition on another wake queue, then: - New wake queue gets a swake_up_one() for the head. - previous swake_up_all() finishes and wakes up this task. - Both tasks see "condition" and begin running even though they opted for exclusive wait and perhaps break some assumption. > } > - raw_spin_unlock_irq(&q->lock); > } > EXPORT_SYMBOL(swake_up_all); > -- Thanks and Regards, Prateek