From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=40.93.195.39 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796111308; b=QMN6YPDlXztOu8gdPwvtTmRs6NoJjKOAuGEDh9JUHIeDft9TGzLw9OAhEeuwG0h1Pv1M OKxXQBv0f/gcd+HNBvCAdsfsyPsN/2Vh4VHt5zTt/HxYhx9dKAZYhYbXYOi2qvXxFfns6 rlh9RqAkz8iRUUwIVz/JgbnKDF0M680HDqox6SuGPZ0Tl/rsiabr+wpoTDuSllo1xj33T q74IC5P+EOuqhEfSCw7Yc7ph0hbgQrhQU4UqCw3fN74Rw0/WRQQyULLFZxw290PL5mNae AG30SfXwXzWrb8ZKE8zYyWKHFbIzPQmnlTJUviNpStfwHOCcXMhGM2SRuKfu4FmlOhA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796111308; h=DMARC-Filter:DKIM-Signature:Received:Received:Date:From:To:Cc: Subject:Message-ID:References:Content-Type:Content-Disposition: In-Reply-To:X-ClientProxiedBy:MIME-Version:X-MS-PublicTrafficType: X-MS-TrafficTypeDiagnostic:X-MS-Office365-Filtering-Correlation-Id: X-MS-Exchange-SenderADCheck:X-MS-Exchange-AntiSpam-Relay: X-Microsoft-Antispam:X-Microsoft-Antispam-Message-Info: X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-CrossTenant-Id:X-MS-Exchange-CrossTenant-MailboxType: X-MS-Exchange-CrossTenant-UserPrincipalName: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=Ku2l08qbMPhOFICRhNBQGMIDts1p8su1MxDLJihEzBg=; b=kzpoji4Rk0W3lW1kBRexJxGpAMZWaF6OrOQziRj8A9Z9BZaYkrhnPSHYlClQ5uEGENBl EYwBc3fwgVOtWAXcZMzLBQ/dMjBmcASIZRolMcBMHQQl//coTs5M2qJqEJoWC486poUQb crVBgsN4KFF5RXRfwg8S3qwLSkF7g1L+Eyfy+8XQzH31pC5gq5CIiMUkhMfxL7Jcm0tMU p9v1Ec14HnTphehmpHKxCbPkOdy0mrsugTo+Rqd660Xfbuje0BrqDlZDaZldmbSLB2Dq0 P/Rbzbb41/qVKJjmHB1RMRhmj4+E/4BUvKpiiSvh9JJVqdAzSiR4JI1OCc3PFlOwJTQ== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=ok2C0eum; arc=pass header.oldest-pass=0 smtp.remote-ip=40.93.195.39 Received: from SN4PR2101CU001.outbound.protection.outlook.com (mail-southcentralusazon11012039.outbound.protection.outlook.com [40.93.195.39]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A51ED606A331 for ; Tue, 1 Dec 2026 07:48:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A51ED606A331 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=ok2C0eum ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=U59jaYNjYQ91TpjvsxQdOdksDkERW3s9lWbKw3SK2QpMLA0FOU5OEUGl5SDQEcBSB907pRsXA9pmsbwLCFQsdJMPF/ePwEtM41zVn39o2nnWgCfYWnKyBIIAvHI7dF58v54qAWxBVOK20K56embZkHeiWNzcERZFXgqY+kFVWRyK3V1ei1+mWNBA01wfqvovCSymkjda6MNqiIj5uIInEPVXhOUqLEGMJW1W0hrbhgEgV0BQEvt7HzKahtkrJ2FandNPI53TZjoFLwneTsff2MhbzL67xt6SZvS+1XmiAvC3EotiYAKN2LVm8MUASz25iOVkrakVtFkCogWdRp0K6A== 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=Ku2l08qbMPhOFICRhNBQGMIDts1p8su1MxDLJihEzBg=; b=ROztiyHi46BHYvnciHV5VyP97+GXgXVd/sHRlOCrTj1LwvLfWtwXDG08gfKeBIJAi5n9/B0FY/oPXg31gClofIa0zsYLe7O41W3Cre+v6i77uvAcP1sfsmylEZPK/1zhSbw2TUkqk5s0LsDNaG1pjpr0kKIPp0nihozvtOqjeZOho0KccbiuVhbmXPkG/nr5z1DIt2/zufUetdWR2AsCG9qZpTBQ8AqMGF92dWMSWw0eIa0hlfwkn9E0hXT1aM7puefdCf5FmSTaNwIQZO/DbW4QbZAZkfOWx4eD0z/50wvfJr2/eP+Wx2qLMWP7Xp0XfAhObz0Qzrj7pc8UAjmDgA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Ku2l08qbMPhOFICRhNBQGMIDts1p8su1MxDLJihEzBg=; b=ok2C0eumjci0N7TgGyZxtTnTmDLfH6kHwfQvV5adp0ys+phnHCp39DJATFzZN2LOg1+BYMUODDlEUKiG5VZx/jV2xzL861C7BtHM2W1p2C9uVCbwzj2CTDOUOfjkgK4aeCfMpv1EhlLb3SFp86a7b71dwd/QsELWDW0Jlq4atmVVCzqEqRGLip9evnRopQI+BQYUrIAt13Zd7zY4A/KTu2RvEQX9u00EChqnCUS7QqF2OIXqpGNUBpSbCEuiJb6ehuDcwfEOy/xoOpvBL9dJQIrjz0lQGbwfrFnu/iqVtHfWkRXMkxMMC/iaXkB+4fh0+MXef6ydvIB6bBCQJ80b0w== Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from BL1PR12MB5174.namprd12.prod.outlook.com (2603:10b6:208:31c::19) by AT6PR12MB484643.namprd12.prod.outlook.com (2603:10b6:170:111::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.10; Tue, 1 Dec 2026 07:48:23 +0000 Received: from BL1PR12MB5174.namprd12.prod.outlook.com ([fe80::58de:822b:868:96e9]) by BL1PR12MB5174.namprd12.prod.outlook.com ([fe80::58de:822b:868:96e9%6]) with mapi id 15.21.0662.012; Tue, 1 Dec 2026 07:48:16 +0000 Date: Tue, 1 Dec 2026 08:48:08 +0100 From: Andrea Righi To: Changwoo Min Cc: tj@kernel.org, void@manifault.com, david.dai@linux.dev, Gavin Guo , kernel-dev@igalia.com, sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org Subject: Re: [PATCH 3/5] sched_ext: Add p->scx.bpf_data for O(1) task context lookup Message-ID: References: <20261201054850.780664-1-changwoo@igalia.com> <20261201054850.780664-4-changwoo@igalia.com> Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261201054850.780664-4-changwoo@igalia.com> X-ClientProxiedBy: MI2P293CA0007.ITAP293.PROD.OUTLOOK.COM (2603:10a6:290:45::7) To BL1PR12MB5174.namprd12.prod.outlook.com (2603:10b6:208:31c::19) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL1PR12MB5174:EE_|AT6PR12MB484643:EE_ X-MS-Office365-Filtering-Correlation-Id: ee58a3ae-2d7e-48bc-cda0-08df4f7f7594 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|1800799024|366016|23010399003|5023799004|11063799006|4143699003|6133799003|261009223027099003|10067099003|56012099006|261102123227099003|261021123027099003|261021121427099003|261026121527099003|261026140227099003|261028144727099003|261022075727099003|261022105027099003|22082099003|18002099003; X-Microsoft-Antispam-Message-Info: w88ldws/2YDFX7iJL7IFHs+OR21RgP/DoIfAhzxKFyKeZVDs1il76T/ltUrDNuXDgqFvB5SRCeeuLlvKUAI3+1GXCS7yaT5kYNCrA/7H8aDGIppCowwBYgtxxMI9Cg/ple/FLitTUNWLKj40vXh+lUE9wEdhnBLXBxRiMWgwA09jfm++PgbDy+niPoH4HWZsOt0/BSnaLjmV/Nsm+mePVNONycLb0feseik0vwEddnUuAofkq3tUx+qs2fkVttyW1gVQVJ4vg1COIjjet+9t/XnX9fPVyyBQGGSWUHNlT2nYDGHO7DI8gW0g/nGF9x+wtYzF/mZ5fpFeKBgx6K3RgifvBGE3Wg42sKxhi4TNFqBwicdxr6r4kH6/nJIOU15Drt0J+1Rudl9uVK5FvVL7dZin9z17kjSePtqXYuYwNNWT1IdY9PS0E+RWcdCffItPlWvpj1jZrdo25wbFp/T0KIn2kQWSCxqBbzxKaehtYw1jYeUrBMlvO/r7YjXoa89h+yA9F8aGSbop28TsdN3VPJkyo4zLPBDB9TYy0qDzb5247gvjKH9rje2BBAXVlQPgOCcGKPnn7W0z0nsS8cu//YbPDcc6FQ/pBcEijHrUHYnBzDMpn3JlYE6KrC8MWRDy9F9O/kvXuIt6wpz6H9FrAci20xLgD+VDQIfiO4CH1Ks= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BL1PR12MB5174.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(1800799024)(366016)(23010399003)(5023799004)(11063799006)(4143699003)(6133799003)(261009223027099003)(10067099003)(56012099006)(261102123227099003)(261021123027099003)(261021121427099003)(261026121527099003)(261026140227099003)(261028144727099003)(261022075727099003)(261022105027099003)(22082099003)(18002099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?fAWmnhHwq7Kd1z+G4k51foIMDLjtMFWpmqlGyjRXn0YhsRgKM4hFetsMCNtO?= =?us-ascii?Q?SCbIY3jQMiYKBvS987LI9OHcTkOwSfAMQodEwCllL0bodAE6RXSoxzw8trzq?= =?us-ascii?Q?DNmiujrIFzYNCtmih0wkw+ksEygKBoTfKjP4Kp+n1IHCnAJ7kKULBKAA13Ch?= =?us-ascii?Q?jSQS+/+jmgHtOAPMwkBF9Lt4s+5/5qDf0IZVj6EgKfXxERPAZ+KkHosbHs/g?= =?us-ascii?Q?QXYJlBhUjgmG5YI3IircieUKTL5XJJOLcUsM7apRYmofGfniWafosgTUvLfk?= =?us-ascii?Q?qnX2vIExkpDWJ8YFZaNKfs5dvj3hDfjl0Dk6a7KfZGTww3TSq7atiBhXNqQO?= =?us-ascii?Q?MQ19Kr0ifgybVfIfzHmuke47ukufPxmaot7kKU4IYTltOSpX4PIhWVMmMYyI?= =?us-ascii?Q?hV9U0ndtXk5pQGPUSbkdRkP8i2ERCT1XOGT3FEXsm/r7+EFGVpx2CeVSXcsb?= =?us-ascii?Q?5kf1yvpXC01eXYXBGQWJd6oOQxUrKiNV0QKlzuPuePZg5VSTYBpux4JhrUFz?= =?us-ascii?Q?hZRp351+Z0HpXWKOdlSjLOyYiApm3Qn6WAfpjB6JaatYOKNH823UY3E7Tm+q?= =?us-ascii?Q?ElgGGcrbpBhs2Dn9GZz9czlu2JkibUMZQH5kKzjh4ftlMh5CHmAcrsw/CQBE?= =?us-ascii?Q?HP4fS6Wobhuf5moLP3/RzteeWd+IXLIr6RPWN342Yzuc2rFipddnIkr3IhDY?= =?us-ascii?Q?++9dWxdYqt6I2oQmmjIY4ResLWbF/HIcxaApAHlJ0DL+qap/YqPy674w9aTR?= =?us-ascii?Q?EfQpmZxIgQsgn+cuWYOVZSGzjIjJH0J6PIYfQTpQ/5McJb4mo92kkua1nO2Z?= =?us-ascii?Q?HdCWLhX/uEvH6kDKbivZHv2E3bWBv1eQ7co5VP2v51fiFxnoIFrYbDuA9kqt?= =?us-ascii?Q?JuJcYyu/cr1jbCvYAZBKsECDztB8flUBwOETfTg21q02hoXcPPd4514fTEGO?= =?us-ascii?Q?NmXHMr1ab0U5kD0c0Qpwg62+hEai/3rnxjW9UNKW9/GPuTwzwSDKcpL1ATRV?= =?us-ascii?Q?WVk88/DC/U5MyhJfpQghX6mA7F0ce2IltrmgBXCtAbWOdGglTY4Q/eBgL43m?= =?us-ascii?Q?O3p0hL2xco60NU/jG/U6+N6K0dp463IVmcPUI1oVYMaPDV4NLCb2CuIwX3TW?= =?us-ascii?Q?5nDx05/Q3hvekVt7wHX924N8EnxrlABh4oT2F413PPuHps8dgl9NgQNwqSlZ?= =?us-ascii?Q?EjnnWkAMjkyHCMH9R8tJj1PNivo197i62Ixmtmu8qLXQDpxRK7whMZdNzwXu?= =?us-ascii?Q?N3AcSFL3SiSVB5dhxYah47utYJ6ITxyf8p22EMvcSYBLCHKkr+gRAsj8EsPh?= =?us-ascii?Q?IKCGlOJuNF38KJRBCUc/1HQrZDAhY3tqlA88pRP35rXInEbp0oF2JAzJjz/7?= =?us-ascii?Q?YYNwW4ETKEF7Yu/G/bCZa0OnAzpetscWyCQC+SMaRga6AXhaiMi+UVBpph8r?= =?us-ascii?Q?ngCYV5Jaxo+jIT1qYHbqgxac+OzsJ552kueWEptpeVkc06obv5qz0fpA5PSn?= =?us-ascii?Q?8+z/zU3h5hPEwKu7h3I5k4o/pyd30tGptrY1MevhOVQvupg4NqYQcrFmFPkl?= =?us-ascii?Q?EAEdTdo2Q/JmTuN8UK3SmwzSCTmPAVBPOIEVifFApeTwV9LrqTgEzr76zzSp?= =?us-ascii?Q?r7FpKLRBDlnbfVw3Bf4xFozJnijoIogpEvHprzDtzjILkdiEJq/WyFMJLNmy?= =?us-ascii?Q?jhHNYZajDAQbnYKp0kpB50mm/VxvahyxdCOFh06dOa40Owdc?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ee58a3ae-2d7e-48bc-cda0-08df4f7f7594 X-MS-Exchange-CrossTenant-AuthSource: BL1PR12MB5174.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Dec 2026 07:48:16.7636 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: xgUPhE6JTp8zWB+nfk3MTFkzrUTYmBmXEjlebv4iVUB67Zd6pRpoypGC0gwrXQWglqd+vlWcDkE9K/1ennmJCQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AT6PR12MB484643 Hi Changwoo, thanks for working at this! Then I can finally remove the ugly p->scx.dsq_vtime hack. :) Comments below. On Tue, Dec 01, 2026 at 02:48:48PM +0900, Changwoo Min wrote: > BPF schedulers keep their per-task context in task local storage, or in > an arena allocation reached through it. Each lookup costs a > bpf_task_storage_get() call. > > Add an opaque u64, p->scx.bpf_data, that the task's scheduler sets with > scx_bpf_task_set_data(), typically to an arena pointer to its context. > Any BPF program reads it with a plain load. The kernel never dereferences > it. > > The value is valid while @p is enabled on the scheduler that set it. > scx_reset_task_fields() clears it after ops.disable(), which covers @p > leaving the ext class, moving to another sub-scheduler and exiting. > ops.enable() runs again whenever @p comes back. > > scx_bpf_task_set_data() follows the rules of scx_bpf_task_set_dsq_vtime(). > It accepts any caller and refuses a task that is not enabled on the calling > scheduler, except in ops.enable(), where @p is still SCX_TASK_READY. A > write can race @p leaving and outlive the clear, and synchronizing such > writes is left to the scheduler, as for dsq_vtime. > > The value means something only to the scheduler that set it. With > sub-schedulers, a task a scheduler reaches as a waker or from a tracing > program may belong to another one. A scheduler can tag the value with > its scx_bpf_sched_id() to recognize its own. > > Signed-off-by: Changwoo Min > --- > include/linux/sched/ext.h | 12 ++++++ > kernel/sched/ext/ext.c | 54 ++++++++++++++++++++++-- > tools/sched_ext/include/scx/compat.bpf.h | 24 +++++++++++ > 3 files changed, 86 insertions(+), 4 deletions(-) > > diff --git a/include/linux/sched/ext.h b/include/linux/sched/ext.h > index f3564581c317..2c2ce144601d 100644 > --- a/include/linux/sched/ext.h > +++ b/include/linux/sched/ext.h > @@ -277,6 +277,18 @@ struct sched_ext_entity { > */ > atomic64_t slice_oob; > > + /* > + * Opaque value owned by the task's scheduler, typically an arena > + * pointer to its per-task context. The kernel never dereferences it. > + * Set with scx_bpf_task_set_data() while @p is enabled on the > + * scheduler, from ops.enable() on, and cleared when @p leaves the > + * scheduler's control, after ops.disable(). Any BPF program can read > + * it, so a tracing program can reach the context of the current task > + * with a single load. A write from outside @p's own ops can outlive > + * the clear, see scx_bpf_task_set_data(). > + */ > + u64 bpf_data; Should we rename this p->scx.bpf_priv, with scx_bpf_task_set_priv() and scx_bpf_task_priv(), to make the scheduler-private ownership clearer? > + > /* > * Sub-sched cap rejected reenq context, valid only while > * %SCX_TASK_REENQ_CAP is set. @reenq_reason_caps is the SCX_CAP_* bits > diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c > index cdceeffbcc65..bca27ced9d8d 100644 > --- a/kernel/sched/ext/ext.c > +++ b/kernel/sched/ext/ext.c > @@ -4749,16 +4749,17 @@ void scx_enable_task(struct scx_sched *sch, struct task_struct *p) > * @rq: @p's rq, locked > * @p: task leaving its sched's control > * > - * The vtime, the slice and what rides on it, the reenqueue count and the > - * watchdog wait belong to the sched while it controls @p. The control ends when > - * @p is taken off the sched or leaves the ext class while staying on the sched. > - * Reset them so that @p starts clean wherever it goes. > + * The vtime, the slice and what rides on it, the reenqueue count, the BPF data > + * and the watchdog wait belong to the sched while it controls @p. The control > + * ends when @p is taken off the sched or leaves the ext class while staying on > + * the sched. Reset them so that @p starts clean wherever it goes. > */ > static void scx_reset_task_fields(struct rq *rq, struct task_struct *p) > { > lockdep_assert_rq_held(rq); > > p->scx.dsq_vtime = 0; > + p->scx.bpf_data = 0; > scx_task_slice_ended(rq, p); > scx_set_task_slice(p, 0); > p->scx.reenq_cnt = 0; > @@ -10886,6 +10887,50 @@ __bpf_kfunc bool scx_bpf_task_set_lazy_resched(struct task_struct *p, bool lazy, > return true; > } > > +/** > + * scx_bpf_task_set_data - Set the BPF data of a task > + * @p: task of interest > + * @data: opaque value to store in @p->scx.bpf_data > + * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs > + * > + * Store @data in @p->scx.bpf_data, where any BPF program can read it with a > + * plain load. The value is valid while @p is enabled on the calling scheduler. > + * The kernel clears it when @p leaves the scheduler's control, after > + * ops.disable(), and ops.enable() runs again when @p comes back. > + * > + * From @p's own ops, ops.enable() included, @p can't leave the scheduler > + * concurrently. Elsewhere, e.g. from a timer or a tracing program, the write > + * can race @p leaving and outlive the clear, as with > + * scx_bpf_task_set_dsq_vtime(). Synchronizing such writes is the BPF > + * scheduler's responsibility. > + * > + * Return %true if @data was stored, %false if @p is not on the calling > + * scheduler or is neither enabled on it nor being enabled by ops.enable(). > + */ > +__bpf_kfunc bool scx_bpf_task_set_data(struct task_struct *p, u64 data, > + const struct bpf_prog_aux *aux) > +{ > + struct scx_sched *sch; > + u32 state; > + > + guard(rcu)(); > + sch = scx_prog_sched(aux); > + if (unlikely(!sch || !scx_task_on_sched(sch, p))) > + return false; > + > + /* > + * ops.enable() runs while @p is still %SCX_TASK_READY but already on the > + * ext class. A %SCX_TASK_READY @p on another class is not enabled. > + */ > + state = scx_get_task_state(p); > + if (state != SCX_TASK_ENABLED && > + (state != SCX_TASK_READY || p->sched_class != &ext_sched_class)) > + return false; > + > + p->scx.bpf_data = data; > + return true; > +} An ops.exit_task() callback can use this kfunc to repopulate bpf_data after the kernel has cleared it. During sub-scheduler handoff, the task is still READY/ext and associated with the outgoing scheduler, so this check accepts the write. The incoming scheduler's ops.enable() then receives the outgoing scheduler's value instead of zero. I reproduced this without any concurrent writer. Can we move the ENABLED transition into __scx_enable_task(), immediately before the enable callback and fold away the wrapper? ops.enable() can't fail, so this would let the check here become simply: if (scx_get_task_state(p) != SCX_TASK_ENABLED) return false; I reproduced this inside virtme-ng and verified that this change fixes it. Thoughts? Thanks, -Andrea kernel/sched/ext/ext.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c index bca27ced9d8da..7ed1319c12dcf 100644 --- a/kernel/sched/ext/ext.c +++ b/kernel/sched/ext/ext.c @@ -4686,7 +4686,7 @@ int __scx_init_task(struct scx_sched *sch, struct task_struct *p, return 0; } -static void __scx_enable_task(struct scx_sched *sch, struct task_struct *p) +void scx_enable_task(struct scx_sched *sch, struct task_struct *p) { struct rq *rq = task_rq(p); u32 weight; @@ -4712,6 +4712,9 @@ static void __scx_enable_task(struct scx_sched *sch, struct task_struct *p) p->scx.weight = sched_weight_to_cgroup(weight); p->scx.lazy_resched = sch->ops.flags & SCX_OPS_LAZY_RESCHED; + /* Task kfuncs must recognize @p as enabled from ops.enable() on. */ + scx_set_task_state(p, SCX_TASK_ENABLED); + if (SCX_HAS_OP(sch, enable)) { if (scx_is_cid_type()) { struct scx_cmask *cmask = scx_fill_cmask_scratch(sch, p->cpus_ptr); @@ -4738,12 +4741,6 @@ static void __scx_enable_task(struct scx_sched *sch, struct task_struct *p) SCX_CALL_OP_TASK(sch, set_weight, rq, p, p->scx.weight); } -void scx_enable_task(struct scx_sched *sch, struct task_struct *p) -{ - __scx_enable_task(sch, p); - scx_set_task_state(p, SCX_TASK_ENABLED); -} - /** * scx_reset_task_fields - Reset what @p carries from its sched's control * @rq: @p's rq, locked @@ -10904,27 +10901,20 @@ __bpf_kfunc bool scx_bpf_task_set_lazy_resched(struct task_struct *p, bool lazy, * scx_bpf_task_set_dsq_vtime(). Synchronizing such writes is the BPF * scheduler's responsibility. * - * Return %true if @data was stored, %false if @p is not on the calling - * scheduler or is neither enabled on it nor being enabled by ops.enable(). + * Return %true if @data was stored, %false if @p is not enabled on the calling + * scheduler. */ __bpf_kfunc bool scx_bpf_task_set_data(struct task_struct *p, u64 data, const struct bpf_prog_aux *aux) { struct scx_sched *sch; - u32 state; guard(rcu)(); sch = scx_prog_sched(aux); if (unlikely(!sch || !scx_task_on_sched(sch, p))) return false; - /* - * ops.enable() runs while @p is still %SCX_TASK_READY but already on the - * ext class. A %SCX_TASK_READY @p on another class is not enabled. - */ - state = scx_get_task_state(p); - if (state != SCX_TASK_ENABLED && - (state != SCX_TASK_READY || p->sched_class != &ext_sched_class)) + if (scx_get_task_state(p) != SCX_TASK_ENABLED) return false; p->scx.bpf_data = data;