From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=213.97.179.56 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796273824; b=aJa5ybUNiojGfxau7wW7A1AkOw1XRIxsQa1SGHZKiBzDOae35Aw2lbUARoblNxWZcOg4 bGFtyehs3gEwvUD0wTqh2FBbxr37sxLNRs+E7k5Mvp+GYGZI9Dtzf0DOnOwhiB6HrUMw0 QP/1kxM62jTGz3aNRw/X9YD2c3h9HH1E9lBDEG2yZ9WCSiY/h2mncFiBU/Rgf4ZEkSOHe r8+ABwmB1tV8GVluaNs9Q7wIikdEsvV7o7+ZZ/VAqhXMz2yDVvsNOpanYV4bPuad4oTLO e1B/RczTIrSFCgO64aJCn1YWdfJPiMhNj5GIS/SSiybz8fF8v6kS9v2UpTc0BW4V1/w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796273824; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=CuyQ3cQH6mgGXdGHk3sKZQkXv8Iq6IFMXq2nM7+FGXU=; b=kceMz+Bs9B4YiJC7lP/GRguVRsNHAchv8Wqv48eSzlJaJ62T0ZdS25LjAOYXm8YFs2xy CyuaH1Uk7cQ9SjPvKwXmPq+v36VOnNIHfild1+tRCOqflhYgqAlxiyd6Gz8UAi8viiupg erjkGxDO8+kc0rx1qRe+XPHKFzOXF7vRN18ibsiKT5NiQNwXyXA6+FGTNkM37s4cqw01K Ypwx7eEKpAyd8jGHLUV+zfbmGrHOSWpX59IMtIGRgUdsgCbdQWEzW0INPOL+dsjD+InSK 4v8ytGZb6N5QlYD6H6v0GbUWl15uM2XJGjDZynp3gOwcF/plJvslw+sFWtyDLVJP2Fw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=SVZ0O1LL; arc=none smtp.remote-ip=213.97.179.56 Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 271F0606A32C for ; Thu, 3 Dec 2026 04:57:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 271F0606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=SVZ0O1LL DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject: Cc:To:From:From:Reply-To; bh=CuyQ3cQH6mgGXdGHk3sKZQkXv8Iq6IFMXq2nM7+FGXU=; t=1796273823; x=2111633823; b=SVZ0O1LLlQQ44EYKiaeWgGfmbusUF88yJ7eiXfRH5AR3E+c D7ohAeoxRy9buwmQPzEA9KyLlXiFdXcirVK/LclG57STABnMzKL/RQ2xiGfsYCfCWg7EuquVgurGN aY6rCh0WRa2vMXo84DrdBVEJ0kA/MDjgaaJ8SuundyU6lEZrspk3hJ9FuiCfFryRDT4BuOm5EwBdI 83LDm3GhnZOSUwOfxIiUA8eVqzJCFr/lSi0DfpmbBGOzhOAElBJ7TueVXt1n1s6Xo3EVOBs4Lndmu WDIyv+8PqNNoC28DYLTDhNzX9ouvnRDV6vcZpSk9KGBgnlXQ6NhlhBZRA78Jz/aQ==; Received: from [58.29.145.179] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) (envelope-from ) id 1xYysN-00000001Tyv-2Thi; Thu, 03 Dec 2026 05:57:00 +0100 From: Changwoo Min To: tj@kernel.org, void@manifault.com, arighi@nvidia.com, changwoo@igalia.com Cc: david.dai@linux.dev, Gavin Guo , kernel-dev@igalia.com, sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v2 7/8] selftests/sched_ext: Test p->scx.bpf_priv across class changes Date: Thu, 3 Dec 2026 13:56:17 +0900 Message-ID: <20261203045618.982539-8-changwoo@igalia.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261203045618.982539-1-changwoo@igalia.com> References: <20261203045618.982539-1-changwoo@igalia.com> 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 Content-Transfer-Encoding: 8bit Under a partial-switch scheduler that sets p->scx.bpf_priv from ops.enable(), move the test task through SCHED_EXT, SCHED_FIFO and SCHED_EXT again, then unload the scheduler. After each step, a syscall program reads the value and sets it again without the rq lock. The value must be set while the task is on SCHED_EXT. Otherwise it must be cleared, and the write must fail. The task also forks while on SCHED_EXT. ops.enable() must find the value cleared for every task it enables, the child included. Signed-off-by: Changwoo Min --- tools/testing/selftests/sched_ext/Makefile | 1 + .../selftests/sched_ext/task_bpf_priv.bpf.c | 64 ++++++++ .../selftests/sched_ext/task_bpf_priv.c | 147 ++++++++++++++++++ 3 files changed, 212 insertions(+) create mode 100644 tools/testing/selftests/sched_ext/task_bpf_priv.bpf.c create mode 100644 tools/testing/selftests/sched_ext/task_bpf_priv.c diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile index ef59c463cd33..1ab274250403 100644 --- a/tools/testing/selftests/sched_ext/Makefile +++ b/tools/testing/selftests/sched_ext/Makefile @@ -201,6 +201,7 @@ auto-test-targets := \ sched_id \ sub_caps_kfunc \ sub_core_caps \ + task_bpf_priv \ test_example \ total_bw \ unlocked_kfunc \ diff --git a/tools/testing/selftests/sched_ext/task_bpf_priv.bpf.c b/tools/testing/selftests/sched_ext/task_bpf_priv.bpf.c new file mode 100644 index 000000000000..ae57494afbaf --- /dev/null +++ b/tools/testing/selftests/sched_ext/task_bpf_priv.bpf.c @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * A BPF program for testing p->scx.bpf_priv. + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * Author: Changwoo Min + */ +#include + +char _license[] SEC("license") = "GPL"; + +UEI_DEFINE(uei); /* Error handling */ + +#define PRIV_MAGIC 0x5c7da7a000000000ULL + +/* The task to probe, set by user space */ +s32 target_pid; + +/* Results of ops.enable() */ +u64 nr_enable, nr_enable_nonzero, nr_enable_set_fail; + +/* Results of the last probe() run, overwritten by every run */ +u64 probe_priv; +bool probe_set_ret; + +void BPF_STRUCT_OPS(task_bpf_priv_enable, struct task_struct *p) +{ + /* The value must be cleared whenever a task comes in, forked or not */ + __sync_fetch_and_add(&nr_enable, 1); + if (p->scx.bpf_priv) + __sync_fetch_and_add(&nr_enable_nonzero, 1); + + if (!scx_bpf_task_set_priv(p, PRIV_MAGIC | (u32)p->pid)) + __sync_fetch_and_add(&nr_enable_set_fail, 1); +} + +void BPF_STRUCT_OPS(task_bpf_priv_exit, struct scx_exit_info *ei) +{ + UEI_RECORD(uei, ei); +} + +/* Read the value, then set it again without the rq lock */ +SEC("syscall") +int probe(void *ctx) +{ + struct task_struct *p; + + p = bpf_task_from_pid(target_pid); + if (!p) + return -ESRCH; + + probe_priv = p->scx.bpf_priv; + probe_set_ret = scx_bpf_task_set_priv(p, PRIV_MAGIC | (u32)p->pid); + bpf_task_release(p); + return 0; +} + +SEC(".struct_ops.link") +struct sched_ext_ops task_bpf_priv_ops = { + .enable = (void *)task_bpf_priv_enable, + .exit = (void *)task_bpf_priv_exit, + .flags = SCX_OPS_SWITCH_PARTIAL, + .name = "task_bpf_priv", +}; diff --git a/tools/testing/selftests/sched_ext/task_bpf_priv.c b/tools/testing/selftests/sched_ext/task_bpf_priv.c new file mode 100644 index 000000000000..7500fc5a4992 --- /dev/null +++ b/tools/testing/selftests/sched_ext/task_bpf_priv.c @@ -0,0 +1,147 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Test for p->scx.bpf_priv across class changes, fork and unload. + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * Author: Changwoo Min + */ +#include +#include +#include +#include +#include +#include "task_bpf_priv.bpf.skel.h" +#include "scx_test.h" + +#define SCHED_EXT 7 +/* Must match task_bpf_priv.bpf.c */ +#define PRIV_MAGIC 0x5c7da7a000000000ULL + +static int set_policy(int policy, int prio) +{ + struct sched_param param = { .sched_priority = prio }; + + return sched_setscheduler(0, policy, ¶m); +} + +/* Run probe() on this task and return its result */ +static int probe(struct task_bpf_priv *skel) +{ + LIBBPF_OPTS(bpf_test_run_opts, topts); + + return bpf_prog_test_run_opts(bpf_program__fd(skel->progs.probe), + &topts) ?: topts.retval; +} + +static enum scx_test_status steps(struct task_bpf_priv *skel, + struct bpf_link **link) +{ + u64 priv = PRIV_MAGIC | (u32)getpid(); + int status; + pid_t pid; + + skel->bss->target_pid = getpid(); + + /* Step 1: Switch this task to SCHED_EXT */ + SCX_EQ(set_policy(SCHED_EXT, 0), 0); + + /* + * %SCX_OPS_SWITCH_PARTIAL takes only SCHED_EXT tasks. Switching makes + * ops.enable() set the value. probe() must read it back, and its own + * set must succeed because the task is enabled. + */ + SCX_EQ(probe(skel), 0); + SCX_EQ(skel->bss->probe_priv, priv); + SCX_EQ(skel->bss->probe_set_ret, true); + + /* Step 2: Fork a child on SCHED_EXT */ + pid = fork(); + SCX_FAIL_IF(pid < 0, "Failed to fork"); + if (pid == 0) + _exit(0); + SCX_EQ(waitpid(pid, &status, 0), pid); + + /* + * The child must not inherit the value, which ops.enable() checks for + * every task it enables. The fork must leave this task's value intact. + */ + SCX_EQ(probe(skel), 0); + SCX_EQ(skel->bss->probe_priv, priv); + SCX_EQ(skel->bss->probe_set_ret, true); + + /* Step 3: Switch this task to SCHED_FIFO */ + SCX_EQ(set_policy(SCHED_FIFO, 1), 0); + + /* + * Leaving the ext class disables the task, and the kernel clears the + * value. probe() must read 0, and its set must fail because the task + * is no longer enabled. + */ + SCX_EQ(probe(skel), 0); + SCX_EQ(skel->bss->probe_priv, 0); + SCX_EQ(skel->bss->probe_set_ret, false); + + /* Step 4: Switch this task back to SCHED_EXT */ + SCX_EQ(set_policy(SCHED_EXT, 0), 0); + + /* ops.enable() runs again and sets the value, as in step 1 */ + SCX_EQ(probe(skel), 0); + SCX_EQ(skel->bss->probe_priv, priv); + SCX_EQ(skel->bss->probe_set_ret, true); + + /* Step 5: Unload the scheduler */ + SCX_ASSERT_ALIVE(skel, uei); + bpf_link__destroy(*link); + *link = NULL; + + /* + * The scheduler must exit by unregistration, not by an error. The task + * leaves the ext class, so probe() must read 0 and its set must fail, + * as in step 3. + */ + SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG)); + SCX_EQ(probe(skel), 0); + SCX_EQ(skel->bss->probe_priv, 0); + SCX_EQ(skel->bss->probe_set_ret, false); + + /* + * ops.enable() ran twice for this task and once for the child. Each + * run must have found the value cleared and set it successfully. + */ + SCX_EQ(skel->bss->nr_enable, 3); + SCX_EQ(skel->bss->nr_enable_nonzero, 0); + SCX_EQ(skel->bss->nr_enable_set_fail, 0); + + return SCX_TEST_PASS; +} + +static enum scx_test_status run(void *ctx) +{ + enum scx_test_status status; + struct task_bpf_priv *skel; + struct bpf_link *link; + + skel = task_bpf_priv__open(); + SCX_FAIL_IF(!skel, "Failed to open skel"); + SCX_ENUM_INIT(skel); + SCX_FAIL_IF(task_bpf_priv__load(skel), "Failed to load skel"); + + link = bpf_map__attach_struct_ops(skel->maps.task_bpf_priv_ops); + SCX_FAIL_IF(!link, "Failed to attach struct_ops"); + + status = steps(skel, &link); + + /* Restore the policy even if a step failed */ + set_policy(SCHED_OTHER, 0); + bpf_link__destroy(link); + task_bpf_priv__destroy(skel); + + return status; +} + +struct scx_test task_bpf_priv = { + .name = "task_bpf_priv", + .description = "Verify p->scx.bpf_priv across class changes", + .run = run, +}; +REGISTER_SCX_TEST(&task_bpf_priv) -- 2.56.1