From nobody Thu Apr 9 12:50:36 2026 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4BFF93368B2 for ; Mon, 9 Mar 2026 02:46:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773024380; cv=none; b=OP29Kd0w57tF5p81igpqARpSpISp9+SBspM/zamH17+1pHk+x0N6X1OQi0UIS/AOyAm/umP163pF0pzL9T7sStXxD0Hyce5TNLJKScQXkI2QrwJ+aMzEsWPEKOPPNN+JtW2NCZezWqY4ne6Eyl8rUzaiYnfezX0JQas9mGieuH4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773024380; c=relaxed/simple; bh=Sg1D9HjBO3m/odg94MJ84wn+zeMo6wLYIVUrLbdEDLE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oucrn6cXd9FWu0oYK4mDbxJOM80wnrR+xEDnLOz7UPW2EiFpJMIBdIRx7RQNohbyibXI7iOvUReQ0uDkq9O1Tn1fhRovrfF1YG8p6qGLfqxnWhwpGtW6+6GE5cWboiV0hwp14fCSGg29MHOc+MxTBO+TiR/58a5w3Zv3619SGVU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=cs8Vp7Zg; arc=none smtp.client-ip=209.85.216.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cs8Vp7Zg" Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-358ed696623so4590342a91.0 for ; Sun, 08 Mar 2026 19:46:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773024378; x=1773629178; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/FyvztfjZ5OoSdXXV3mRbyT+pJ/bhNLbffqVWxVVswQ=; b=cs8Vp7Zg/nVCAhB5xAb3gQRXshcyZC5JOPvejocCAFxmNSDyS+OY/87RlYW2jB6JrX jnRgDmSViVC7qi5cLCn9vxBv7DO5A47rd4OGR4KrVATvCTiKdF5yIvN24FCg37Qgei0x GUKL4PDfU1Ki5Vl5nKs+18EEFjMd62Zfha8W8zX1cmdL0fTYGcVrPkuvyyPJcmOabg/8 X7hNoH4SxAxXO7IrW4MM0dKcyjhpGXPl5NbGQbKaXVEJ6jeFAEpftQmtmjivDwHK7McB 4fg6xmGjPUhY+hBdafvER3Gdz1zCoITvwpGInvVMGUT6IIAxB3AHsgYJlsrYcrJrflxT 4GLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773024378; x=1773629178; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=/FyvztfjZ5OoSdXXV3mRbyT+pJ/bhNLbffqVWxVVswQ=; b=JGWTQ3OY4QGmK8hnx8y7HBhR73Qz7ec7xufZaTqNG5oQ1XoTHho/CMOd+kWofUNLpR g3OwgGsAlS9UwDsQVanpd9dDh4LbeqLau0x4yCWJi0XHtzeS+GluX6cOKXg358DCPwRl KLJdgsMDhwIZmjsXf0e4Uz2Pt0ax7E8OI8MQSF1nOl544y8sOJsIIkQnGC9DnIwecHNv gfb4jvZsvTU/DCkUel62f/CMyWSZQvSe9MgB6faqHFvkxVHK0JA+7Nuu/3ygDBDHGDTp CQWo7DLLhPq+uT1VJUeG5otMdcTBI85i3PqzWFDsi+H1FvO+6j6CtZ9W6DfBO3rCU/f9 8EKg== X-Gm-Message-State: AOJu0YwsMUiWs32UdwqAtl1YDI3HL9AxlJsyf9IcY0O7ZtRIgjIcOfjy IATqqSDzQi3yz9N0ScrQzPb99eBNQ4BwGx/NsW65fquT3NXkNYI+gdi+ X-Gm-Gg: ATEYQzxZzuUB16JLYx3o8HhaQ6s+80YTyv6+tdFSsI9m9mrlg8qWHkA1Z2NnHg4hutQ wPoW63qZhtNnjbOYF3Ix2lSqnx28Y62r6qf69uv+pLC6f258q/6NaenID6Ts9MbIX18uM8Dr45/ 30IM2lJ6CKDRHeR2FvCnuATOX/YmMCAUY0i9uguvGtEiku+4ZqhVaX1j864MPVCR4//1SqXNy0D ZggTDB9wj0iWKFKy+08hmcKZMDwzMLVqsYyzC8xqQBghicnvP/I/aMs0I9H9Gc08obidUn6qg9J ZfyIFy3MXqLN7dRGRyY+OYYmSgodIa9A/vNwoKB+1fiyWWbiUPon5nXXYKV52/yIcJrW8i1U7el lfrQF8F9fvNaa+fFb10IP7MK7x1nkqCuB+pe3viddWQCpoH/Z8SG2FrGV0DSX1gKOQT3TmbYsFF UIQb+rssnOGHSTikvrKZqaQ6eTL3rAXnNYx/6EUOn8zcT5Vf6fCd/c3AaAsiDoHq0nEHdiy8vi/ Z4Au6KTRiUi2A== X-Received: by 2002:a17:90b:4c09:b0:359:97d3:5c57 with SMTP id 98e67ed59e1d1-359be34986emr8694554a91.19.1773024378509; Sun, 08 Mar 2026 19:46:18 -0700 (PDT) Received: from mi-HP-ProDesk-680-G6-PCI-Microtower-PC.mioffice.cn ([43.224.245.226]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2ae83fa2fa5sm97986595ad.70.2026.03.08.19.46.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Mar 2026 19:46:18 -0700 (PDT) From: zhidao su X-Google-Original-From: zhidao su To: sched-ext@lists.linux.dev Cc: linux-kernel@vger.kernel.org, tj@kernel.org, void@manifault.com, arighi@nvidia.com, changwoo@igalia.com, peterz@infradead.org, mingo@redhat.com, zhidao su Subject: [PATCH v3] sched_ext: Use WRITE_ONCE() for the write side of scx_enable helper pointer Date: Mon, 9 Mar 2026 10:46:12 +0800 Message-ID: <20260309024612.530874-1-suzhidao@xiaomi.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260306141718.2721073-1-suzhidao@xiaomi.com> References: <20260306141718.2721073-1-suzhidao@xiaomi.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" scx_enable() uses double-checked locking to lazily initialize a static kthread_worker pointer. The fast path reads helper locklessly: if (!READ_ONCE(helper)) { // lockless read -- no helper_mutex The write side initializes helper under helper_mutex, but previously used a plain assignment: helper =3D kthread_run_worker(0, "scx_enable_helper"); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ plain write -- KCSAN data race with READ_ONCE() above Since READ_ONCE() on the fast path and the plain write on the initialization path access the same variable without a common lock, they constitute a data race. KCSAN requires that all sides of a lock-free access use READ_ONCE()/WRITE_ONCE() consistently. Use a temporary variable to stage the result of kthread_run_worker(), and only WRITE_ONCE() into helper after confirming the pointer is valid. This avoids a window where a concurrent caller on the fast path could observe an ERR pointer via READ_ONCE(helper) before the error check completes. Fixes: b06ccbabe250 ("sched_ext: Fix starvation of scx_enable() under fair-= class saturation") Signed-off-by: zhidao su --- v3: Use a temporary variable to stage kthread_run_worker() result; only write to helper after confirming validity, eliminating the window where an ERR pointer could be observed via the lockless fast path (Tejun Heo) v2: Add missing Fixes: tag (Andrea Righi) --- kernel/sched/ext.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c index 9a1471ad5ae7..63dc7cf1e2a7 100644 --- a/kernel/sched/ext.c +++ b/kernel/sched/ext.c @@ -5355,13 +5355,14 @@ static int scx_enable(struct sched_ext_ops *ops, st= ruct bpf_link *link) if (!READ_ONCE(helper)) { mutex_lock(&helper_mutex); if (!helper) { - helper =3D kthread_run_worker(0, "scx_enable_helper"); - if (IS_ERR_OR_NULL(helper)) { - helper =3D NULL; + struct kthread_worker *w =3D + kthread_run_worker(0, "scx_enable_helper"); + if (IS_ERR_OR_NULL(w)) { mutex_unlock(&helper_mutex); return -ENOMEM; } - sched_set_fifo(helper->task); + sched_set_fifo(w->task); + WRITE_ONCE(helper, w); } mutex_unlock(&helper_mutex); } --=20 2.43.0