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=52.101.193.25 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796544959; b=SXwHn6MBapUtBs5PHPLTFgacY1sJZh9cE3LjPaTNbcjsR2JNan+LxIRNhv+nhhzG+c+V fW2QbTbMZaB2eYrMDj7YD26JR1TLgweU6zaaJEU8dXpkrSDQvd3G+HCIfwtKZXCq/QI3J EmNkha8UfG6Etl2w62JeNxg9QvgZG4oPwA+mHdMazqKlGtyPrAUc5che+wnfd7SjT9Qny g/WL3ypUkpE90fgq4qpM23NHRwUPb5lnDjV5EHnXUYkgkPkY1Q+bH4geoxW6XQ+fHWXYb b5qG9k4tSEp10+54bnDMmJWVjCnLguU7rHpksTXpPnHYGhmrY/K5ID23CoeCbCVgW7w== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796544959; 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=Xll8GQh4ihWkjXH+u0xC32dln74ubqmQNbT7o2dExzI=; b=XUVCA8lQt53Y4LnPzn9lYng4gCPonu09xP+udwX5xICVIC9xMMFq+CWLXdTB/Xz013Lh PdaYIuw4ORun6rvxxXdUuyhGstUqQqHI8dAe8En4A+Jrc7C6uKMQTHOUvrbv2EBvvg9bY LzvxhNXchmZrq37ArbRhLwqy6lrT1NQSyIYulCxQoLGpEhzEVQLvUTLG+N0jdO07Tzl1P xZkrcMfgca1poyOLaivqnVDzClQuhrKBTBuFbVSdThWJmkAG5aI8A8OjMk/E54MIK9RFQ pu1UYScjteZMGAAPkIXwIwqCZrHF+3NffcvpuozEco+ednrULKXfa8+YpUaQ6pc1d5w== 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=rSv68NP7; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.193.25 Received: from CH1PR05CU001.outbound.protection.outlook.com (mail-northcentralusazon11010025.outbound.protection.outlook.com [52.101.193.25]) (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 61433606A31F for ; Sun, 6 Dec 2026 08:15:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 61433606A31F 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=rSv68NP7 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=UzhsTkhindBKkbF2+8PuuAGuDR0/Bq+EgGhcii8Yhg1jl7EKNJHmtWmtdSS3owMiEL09lpCGWOL+aqmR+5vruSxImxGeoxHNfEs+if6RSSHIy6N3jSkww5sWlXlkrGvDaFCpDZqQQyUR7ruhkOR4Oh/E996mKs/XFwFrkabjFxBwhwVnran0z3sVRLTCcqFnDLSoaVcu+9VZLhZHht0M2I0MooohxbLM/+/QCFxZGw+ek/zXSVA5kMU/g9iIMWq+JjMnBq43Kk8BMrgzm9zB8xtIlf58aN/jxvZp0sxM2kEIpoJfHDhCVQZPOQNjEUytXQts4lCbHaPFsiAwC685xQ== 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=Xll8GQh4ihWkjXH+u0xC32dln74ubqmQNbT7o2dExzI=; b=OlysVcqfTDFoga3TcdNmsLa4KcK0rqPOHLY0Hzp473Is+AIJ0GuQb/GQ3E4Bar3ysAgp5g/ByaY0e/GBRNjCsM00B+WjzwzP7lJSQ3ClTQZ+drxguoLYbpB/ZPwnl8qTbXwUCmeCSbdfv3RH3OIAavKuNbX3dayckzoo5JZ2v7MU/2AKodcouUnlo1BOYceU3P5KJDUgRLkVAd3HAzPEFu+YNvNoNXWqEGZQG3pfU/boFhdbi613V2I9KpRIVadBli+3bsRV2sP4mdvmxqr4PjNHMurss+/j9v30biwcZ3+eSCIFqE+uDmsu6LEIesd8+idZSj7EyLMBEAFvwAL0xw== 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=Xll8GQh4ihWkjXH+u0xC32dln74ubqmQNbT7o2dExzI=; b=rSv68NP7asKwDe9t58uAp4MoCd9zHDuAA9AKPJ069V0/BlBudzk+XV6G/iog/BCflt6q9A9CFVl4HnIbbfDVWnKqkefMJRdTA58A6iQA+qISClPjex/JMzvdxcLaG6zSfwOZ8N5g/QsOVJKvzTVXnKfEqqzrgN4VpwnzFE7up4/qmLmBaiuldnzkURh+sMvhx2nErP/u0IPZ9bOryW8UttUQsrY1CiPhuncQk7RjKxaKXM5STB+NnfEZ1vvUUyd25a/DxA+4fQ3oSBUP+0N5n7OtpHxztvNPCmNQMd+M1p/TwurKneRIQfw1eZdrhk7UnnoQrxjcnB4x4p5a4o8nLg== 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 CH8PR12MB999203.namprd12.prod.outlook.com (2603:10b6:610:35a::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.15; Sun, 6 Dec 2026 08:15:54 +0000 Received: from BL1PR12MB5174.namprd12.prod.outlook.com ([fe80::58de:822b:868:96e9]) by BL1PR12MB5174.namprd12.prod.outlook.com ([fe80::58de:822b:868:96e9%5]) with mapi id 15.21.0688.013; Sun, 6 Dec 2026 08:15:54 +0000 Date: Sun, 6 Dec 2026 09:15:40 +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 v3 3/6] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Message-ID: References: <20261206053728.2026272-1-changwoo@igalia.com> <20261206053728.2026272-4-changwoo@igalia.com> Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261206053728.2026272-4-changwoo@igalia.com> X-ClientProxiedBy: MI1P293CA0006.ITAP293.PROD.OUTLOOK.COM (2603:10a6:290:2::16) To BN9PR12MB5179.namprd12.prod.outlook.com (2603:10b6:408:11c::18) 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_|CH8PR12MB999203:EE_ X-MS-Office365-Filtering-Correlation-Id: 807965cd-5c09-4ef4-77e3-08df53712571 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|23010399003|376014|1800799024|5023799004|11063799006|4143699003|6133799003|261009223027099003|261021121427099003|261021135927099003|22082099003|18002099003|261102123227099003|56012099006|261022105027099003|261022075727099003|261026121527099003|261026140227099003|261028144727099003|10067099003; X-Microsoft-Antispam-Message-Info: wz/W1Ar+PnJqvN429Izko0Bee6ttOG9K8TwkZqBcCALIpqotyzZzGGohcGQrsRKmDJDWVX7UiEt3IpZOMH2tT/weej+htpWxIeuHU3CHluVuEPHd/mB+PVv18MCozPZKbbLgwdkTWGcRHrJ1/MAGibTqNyMF3+rq/q20s2IPwI95kjXijnD4nVyQTqTDWpSaRte2xQcadF2/BmhADuMvlRivy88jqLBv4niVkVVrYmVcrf9djemtQXe2vo1+iaZidu02mowg2/GEU4GeOQmesct9h1Zbita5Unin900bXNGyNBwzYNy1bb2taq2HoneQEshgBpk5kX1hz1JmUnTiFK4RR84X1iCCn/xtKrfQbeZKhdeccxOF3dYxB6It1nFPio60Pn++3g6bEYPjfBdQADxtjlV1RgnjLP7u3RhwFXCJHIZww+dTwfLzLF0jmAwTQe6xSuiff+7qb3yFo+DPssS4riSlvM2bbjlsgX1Wdwlz9WFqP7TvHKumg+0DH8GiRnL9se9SJoR0+AkOpCb1rSoIVLGFOl4Uy1yjHYlC8x5+vzRtyKOFa/oFmXmOvuYO08vrt1tvez7cvVUK4Jh9PncaxSS4aMli9fxoYgSkj6vmfYXp2hiN6iSkiGm9JywRXrcmtiLd2D+lNJ7WKOKvojjpTdDHtH4cXQ47i9VGBR8= 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)(366016)(23010399003)(376014)(1800799024)(5023799004)(11063799006)(4143699003)(6133799003)(261009223027099003)(261021121427099003)(261021135927099003)(22082099003)(18002099003)(261102123227099003)(56012099006)(261022105027099003)(261022075727099003)(261026121527099003)(261026140227099003)(261028144727099003)(10067099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?R7VW3d3EjjylP7x6aF63I2Ehnt47uT8omaWNl4q33L9Je68uMiJ82PX7Tmir?= =?us-ascii?Q?p77iSGhWSgGWyJ05lakX97W2EQJy/CBqOQSGcyQIiSCNX8DZPDQAyD+8EqyP?= =?us-ascii?Q?ri8PNsviZlpyEHnIc/tLtGNz/lwKDpgwtrZir1WoNu6p7yGxUTh+nDhaKlgG?= =?us-ascii?Q?XEN16bGpl3pQa9VkOmdSJLjg5tpglmHnbKxI6O+kHDinrzoe0md0xydk8Fl0?= =?us-ascii?Q?il3kRLi7BborTF6DQ2O/jWae6By/99Z4KDWs+mCfDHmXjGGjIfHJOhZ+C0SW?= =?us-ascii?Q?kCx/vusLYFExbj2sCIOmePiQdijl+wGQhVVPg39b1DjnL3O4QjeTE9bAk15V?= =?us-ascii?Q?UzZo+W1KvL0ygEuPly1ov4ptTPr/zq3hlKUmvUkR/OxWjMwLIUPVibQc0wb6?= =?us-ascii?Q?TMU7Zf5qedoCmV6V9q0cxcmpSdW10qD7B4hVn1jnyRDolkX19Hg8kgyCEkE9?= =?us-ascii?Q?srZYhvQc9lG9hwReAlimynEpj0+AaXrI8cDc6LQjefK0/5rxTgdXNcZ1o+5Y?= =?us-ascii?Q?mu80dRFbhfkfiV03RPIT98QtGxNx29hE/8CJN7CM1qvHd4I+Ehu9M3a0XkZp?= =?us-ascii?Q?ptKY7v7+zz/25LWZOdLdoZDySGRL0obvw8OUMMVaHH/GgEFRn7wzWx/evA1w?= =?us-ascii?Q?rY2txzN2x12e0vLjLLTbTS4QhyxUNLLapkXPdvcQpWnPMCwotmk+QwBIs5BV?= =?us-ascii?Q?VLs7XDrPl2hzGiXzrIcHIichOl+qu7+tVsGdaARMFzbwZRMM/5irmTPiySro?= =?us-ascii?Q?HhdOm8ULxn6QGkFU4AKVnHqrBEODSNBWGLDl9+bmbcfHnK0MOvGz7gjEymvg?= =?us-ascii?Q?0fKLU2d68pRiLtqXpsKAibwjgoafHkzRhH8sx/pXrvsd8RGqX4ENUcyfhqNu?= =?us-ascii?Q?KOhKFBrBP4FTn1j3X5F5f2XeOmyn85sFSQl6YdDX9gO41+TVSdOK01tIMlwo?= =?us-ascii?Q?Z9/7zH2exkq0FfLeTzY8PmRMeVUOJdas+H8tNIbk9e+V9drGnB3JA0iUWoho?= =?us-ascii?Q?xqDsEAi4CwQRmHrLB33yHLdVEr2FG03EkGcLQf8VmEVIQZSHvDc5zG6dRRrk?= =?us-ascii?Q?F2ExOEdQ6H1BjbQdWmo2SlzEwQIhbyvTd8T16n19NjCLuqH3kkMUJHDeWhrI?= =?us-ascii?Q?Q+hLMVjELMDAWX/lTPbj/xVxpfywxCHMvepCDFDM9J4+UjjqIPEF3b3EuQQn?= =?us-ascii?Q?90xPlELf6GZ0E0vLoGIKT1s4qOdv1Ae0IHkeb44B3Uw7fjW5fwSzH9QbiE3i?= =?us-ascii?Q?BwIc0Cj6C5TBl2sTcxbIoftb+TVQya8bhg0GIJ/g6xWf/N9J1A8NWKwofepb?= =?us-ascii?Q?o0ZqzgNgZi90QqmnJZ8R+leIBMjv4UNgP2N2Fn4B8izx8vxu7+psb3U6CREF?= =?us-ascii?Q?r+FrBEsfw1uRcsgyLm/exvRVYKsnEPhX7ZHNhRJXGUCq1eU7U5Lq7P2RkQBG?= =?us-ascii?Q?pU3gFWNZsnysT4jgIFvgMV+KD0LQ8CaLRz4Aw0DYStmnHUypqjPXWvEOR6qI?= =?us-ascii?Q?rkXz2Yvbff4Yb5NGNTSHl7GYkos9ZzMozynvfEt05480anCs221+hGGFZbJ1?= =?us-ascii?Q?KzxyoKLGptKfYdCpZGWymkX9o+DeEz/B/hBuflBbTjY1SbxGxA1K+I2xrDzc?= =?us-ascii?Q?pQVA8mmUdD2zy9PP4YDNDP4PDPUofucGmrU8mtJ1Ra0V3ttx36kXlTDvEC91?= =?us-ascii?Q?bCqT94omgs+qRJ9yfIHVfcRLbgTlOY/9npbPL2+TRbuNSbOHy+3kV4Trz2fE?= =?us-ascii?Q?3m0mI18psw=3D=3D?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 807965cd-5c09-4ef4-77e3-08df53712571 X-MS-Exchange-CrossTenant-AuthSource: BN9PR12MB5179.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Dec 2026 08:15:53.9383 (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: 0fWxJW5o86ObgVTe+fxl+xXQVBEwwdddVfuRo8mfPS6636rHL6roU/ZJ8I7xzV9wpzyiz4gLZGdDPp1tKEaZPQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH8PR12MB999203 Hi Changwoo, On Sun, Dec 06, 2026 at 02:37:25PM +0900, Changwoo Min wrote: > BPF schedulers keep their per-task context in task local storage, or in > an arena allocation reached through it, and pay a bpf_task_storage_get() > call per lookup. > > Add p->scx.bpf_priv, an opaque u64 that the task's scheduler sets with > scx_bpf_task_set_priv(), typically to an arena pointer to its context. > Any BPF program reads it with a plain load. > > The value belongs to the scheduler that owns @p, from after > ops.init_task() until ops.exit_task(). The kernel clears it after > ops.exit_task(), so it never reaches the next scheduler. It isn't reset > with the other task fields when @p leaves the ext class, as the > scheduler still owns @p then. > > scx_bpf_task_set_priv() checks the ownership under @p's locks, so a > write can't race @p's move to another scheduler. Like > scx_bpf_cpuperf_set(), it works from an op called for @p, which holds > one of those locks, or with no lock held, where it takes them. It fails > from any other op, and tracing programs, which may run under any lock, > can't call it. For lockless readers, the store is ordered after the > stores that set up what the value points to. > > With sub-schedulers, a task that a scheduler reaches as a waker, from a > tracing program or in ops.init_task() may belong to another one. A > reader holding the task's locks compares scx_task_sched_id() with its > scx_bpf_sched_id(). A lockless reader tags the value with its id > instead, since only the owner stores and the kernel clears the value > before the task moves. > > Signed-off-by: Changwoo Min > --- > include/linux/sched/ext.h | 11 +++ > kernel/sched/ext/ext.c | 89 ++++++++++++++++++++++++ > tools/sched_ext/include/scx/compat.bpf.h | 24 +++++++ > 3 files changed, 124 insertions(+) > > diff --git a/include/linux/sched/ext.h b/include/linux/sched/ext.h > index f3564581c317..a1793ab5f0c3 100644 > --- a/include/linux/sched/ext.h > +++ b/include/linux/sched/ext.h > @@ -277,6 +277,17 @@ 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_priv() while @p is on the scheduler, > + * from after ops.init_task() until ops.exit_task(), and cleared > + * after ops.exit_task(). Any BPF program can read it, so a tracing > + * program can reach the context of the current task with a single > + * load. > + */ > + u64 bpf_priv; As reported by sashiko, on 32-bit architectures a u64 store/load isn't single-copy atomic, so a lockless reader could see a torn value. Arena pointers don't exist on 32-bit anyway (arena is 64BIT && MMU only), so maybe we can simply restrict bpf_priv to get/set 32-bit values there and add a check like this in scx_bpf_task_set_priv(): if (!IS_ENABLED(CONFIG_64BIT) && unlikely(upper_32_bits(priv))) { scx_error(sch, "bpf_priv 0x%llx exceeds 32 bits", priv); return false; } Thoughts? Thanks, -Andrea > + > /* > * 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 343d82f328ef..4f575a19df76 100644 > --- a/kernel/sched/ext/ext.c > +++ b/kernel/sched/ext/ext.c > @@ -4833,6 +4833,12 @@ void __scx_disable_and_exit_task(struct scx_sched *sch, struct task_struct *p) > * ops.exit_task() can still write them while @p is on @sch. > */ > scx_reset_task_fields(task_rq(p), p); > + > + /* > + * Not in scx_reset_task_fields(), which also runs when @p leaves the > + * ext class. The value stays until @p leaves @sch. > + */ > + WRITE_ONCE(p->scx.bpf_priv, 0); > } > > /* > @@ -10912,6 +10918,84 @@ __bpf_kfunc bool scx_bpf_task_set_lazy_resched(struct task_struct *p, bool lazy, > return true; > } > > +/** > + * scx_bpf_task_set_priv - Set the BPF scheduler's private value of a task > + * @p: task of interest > + * @priv: opaque value to store in @p->scx.bpf_priv > + * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs > + * > + * Store @priv in @p->scx.bpf_priv, where any BPF program can read it with a > + * plain load. The calling scheduler can set it while @p is on it, from after > + * ops.init_task() until ops.exit_task(), whatever @p's class. The kernel > + * clears it after ops.exit_task(). > + * > + * @p may be on another scheduler, e.g. as a waker, in a tracing program or > + * in ops.init_task(), and the value is then that scheduler's. So check the > + * owner before using it. With @p's rq lock or pi_lock held, compare > + * scx_task_sched_id() of @p, in compat.bpf.h, with scx_bpf_sched_id(). > + * Without them, @p may move in between, so tag the value with > + * scx_bpf_sched_id() and check the tag. > + * > + * Can be called from an op called for @p, or with no rq lock held, e.g. from > + * an unlocked op, a BPF timer or a syscall program, in which case @p's locks > + * are taken. Fails from any other op, such as one called for another task. > + * Tracing programs can't call it. > + * > + * Return %true if @priv was stored, %false if @p is not on the calling > + * scheduler or the caller is an op not called for @p. > + */ > +__bpf_kfunc bool scx_bpf_task_set_priv(struct task_struct *p, u64 priv, > + const struct bpf_prog_aux *aux) > +{ > + struct scx_sched *sch; > + struct rq_flags rf; > + struct rq *rq = NULL; > + bool locked, ret = false; > + u32 state; > + > + guard(rcu)(); > + sch = scx_prog_sched(aux); > + if (unlikely(!sch || !scx_kf_allowed_ctx(sch))) > + return false; > + > + /* Fast path, rechecked under @p's locks below */ > + if (!scx_task_on_sched(sch, p)) > + return false; > + > + /* > + * @p's owner and state change only under both @p's pi_lock and rq lock. > + * An op called for @p holds one of them, the rq lock or, for > + * ops.select_cpu(), the pi_lock. Any other lock doesn't pin @p, and > + * taking @p's locks under it can deadlock, so restrict the call to @p's > + * own op when a lock is held. > + */ > + locked = p == current->scx.kf_tasks[0] || p == current->scx.kf_tasks[1]; > + if (!locked && (scx_locked_rq() || current->scx.kf_tasks[0])) > + return false; > + > + /* If no lock is held, pin @p by taking its locks */ > + if (!locked) > + rq = task_rq_lock(p, &rf); > + > + /* The sched owns @p from after ops.init_task() until ops.exit_task() */ > + state = scx_get_task_state(p); > + if (scx_task_on_sched(sch, p) && > + (state == SCX_TASK_INIT || state == SCX_TASK_READY || > + state == SCX_TASK_ENABLED)) { > + /* > + * Lockless readers may dereference @priv. Order the stores that > + * set up what @priv points to before the store that publishes it. > + */ > + smp_wmb(); > + WRITE_ONCE(p->scx.bpf_priv, priv); > + ret = true; > + } > + > + if (!locked) > + task_rq_unlock(rq, p, &rf); > + return ret; > +} > + > void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags) > { > struct scx_sched_pcpu *pcpu; > @@ -12267,6 +12351,7 @@ BTF_KFUNCS_START(scx_kfunc_ids_any) > BTF_ID_FLAGS(func, scx_bpf_task_set_slice, KF_IMPLICIT_ARGS | KF_RCU); > BTF_ID_FLAGS(func, scx_bpf_task_set_dsq_vtime, KF_IMPLICIT_ARGS | KF_RCU); > BTF_ID_FLAGS(func, scx_bpf_task_set_lazy_resched, KF_IMPLICIT_ARGS | KF_RCU); > +BTF_ID_FLAGS(func, scx_bpf_task_set_priv, KF_IMPLICIT_ARGS | KF_RCU); > BTF_ID_FLAGS(func, scx_bpf_kick_cpu, KF_IMPLICIT_ARGS) > BTF_ID_FLAGS(func, scx_bpf_kick_cid, KF_IMPLICIT_ARGS) > BTF_ID_FLAGS(func, scx_bpf_dsq_nr_queued, KF_IMPLICIT_ARGS) > @@ -12330,6 +12415,9 @@ static const struct btf_kfunc_id_set scx_kfunc_set_any = { > * when scx_locked_rq() returns NULL. Taking it while the context already holds > * an rq lock, the same one or another cpu's, can deadlock. > * > + * scx_bpf_task_set_priv() takes the task's pi_lock and rq lock outside an op > + * called for the task, with the same deadlock. > + * > * scx_bpf_destroy_dsq() takes a user DSQ lock. So do > * bpf_iter_scx_dsq_next() and bpf_iter_scx_dsq_destroy(), the latter only > * after a successful next() has linked the cursor. A tracing program can > @@ -12347,6 +12435,7 @@ static const struct btf_kfunc_id_set scx_kfunc_set_any = { > BTF_KFUNCS_START(scx_kfunc_ids_no_tracing) > BTF_ID_FLAGS(func, scx_bpf_cpuperf_set, KF_IMPLICIT_ARGS) > BTF_ID_FLAGS(func, scx_bpf_cidperf_set, KF_IMPLICIT_ARGS) > +BTF_ID_FLAGS(func, scx_bpf_task_set_priv, KF_IMPLICIT_ARGS | KF_RCU) > BTF_ID_FLAGS(func, scx_bpf_sub_grant, KF_IMPLICIT_ARGS) > BTF_ID_FLAGS(func, scx_bpf_sub_revoke, KF_IMPLICIT_ARGS) > BTF_ID_FLAGS(func, scx_bpf_destroy_dsq, KF_IMPLICIT_ARGS) > diff --git a/tools/sched_ext/include/scx/compat.bpf.h b/tools/sched_ext/include/scx/compat.bpf.h > index 4d2298bd7ac5..e8cb5f35eed0 100644 > --- a/tools/sched_ext/include/scx/compat.bpf.h > +++ b/tools/sched_ext/include/scx/compat.bpf.h > @@ -487,6 +487,30 @@ static inline void scx_bpf_task_set_lazy_resched(struct task_struct *p, bool laz > scx_bpf_task_set_lazy_resched___new(p, lazy); > } > > +/* v7.5: Add p->scx.bpf_priv and scx_bpf_task_set_priv(). */ > +bool scx_bpf_task_set_priv___new(struct task_struct *p, u64 priv) __ksym __weak; > + > +static inline bool scx_bpf_task_set_priv(struct task_struct *p, u64 priv) > +{ > + if (bpf_ksym_exists(scx_bpf_task_set_priv___new)) > + return scx_bpf_task_set_priv___new(p, priv); > + return false; > +} > + > +struct sched_ext_entity___bpf_priv { > + u64 bpf_priv; > +} __attribute__((preserve_access_index)); > + > +/* p->scx.bpf_priv, or 0 on kernels without it */ > +static __always_inline u64 scx_bpf_task_priv(struct task_struct *p) > +{ > + struct sched_ext_entity___bpf_priv *scx = (void *)&p->scx; > + > + if (bpf_core_field_exists(scx->bpf_priv)) > + return READ_ONCE(scx->bpf_priv); > + return 0; > +} > + > /* > * v7.5: Add scx_bpf_sched_id(). Ids start at 1, increase monotonically and are > * never reused. 0 means no scheduler, including on kernels without the kfunc. > -- > 2.56.1 >