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=1796104174; b=07RTljoEz1NGqa9AKsqQNl7eMVNcPW/Vr0kb8/GqQIsnr0LTBusTQuvBptEA+ZFYV0W8 6Ij2W0f5QduF0z2g4A+nlw4Jktn+wFh4WP5VPuae9YiaUrHZRNhkfACluIchHGX2cr781 EW0Ol9xKJGrDBafhYKatlpCrdkhsFEbVHWy8F9yLDDMoiQtXoIat6qXULeZ3Ia+Mr7grj 0gRANXQ5Q1KfGWlb7jPkCvJlauT1FB8pE4crWl8rvMx5Vcprq0icyq82rLM1aoVCjcJMI +hITV0dPledBVndgHNIVosllHftmpZGa6UVra/Woc8TMYG6JW1AXqJR7gU7UPFK+QeA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796104174; 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=foGGW7b8i0k+REWUcf326XUoICP1A6JAzjYqE4KhsX8=; b=K/Fs2ur2JSgqmscILXU41ChXhmFlzfKb+HuL2gmWU+nE+zrS6oglkHbVGFohsByk3B/i zu/KADdvlfRcg/n17hquQrPoscrEKluXXZ+3TobsGflRucVzt/b0KGjK1t5TbjtYgh83l 82o/d8YKqUltOCc5nbIPXuJyIpTbAtnmOTbyhWh7E+aUUgSOgTXXXKzIK6lEp9EgDsq5i xgryCZyk/Cq9gBYIMIL2x2zN0trR+zM6TqJa40qFlPPXalFmYHf+dQBE0REYVbnavPWwC NJZ9Mohc7wB97LNbLlBit0Ihx4o6ycuPnfw7FCZMs5pp9E6IC3llVIG52P9ukHJDv+Q== 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=WcBWSBB2; 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 17D5B606A320 for ; Tue, 1 Dec 2026 05:49:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 17D5B606A320 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=WcBWSBB2 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=foGGW7b8i0k+REWUcf326XUoICP1A6JAzjYqE4KhsX8=; t=1796104173; x=2111464173; b=WcBWSBB2IIpBkuH56SRj2s1uBKZ4cXLTyjcBfo0aoWY8rHU S9qxkOTEl5s1KN754uTn2Myh0vpuEqnsIT67Sc06mWONhnOQaY/PW1qBNCRNE4LqIhM5Rf60V+Cda EgVsk+jT67nyG9luQI6Jz6T/JRLxkmk1BX4XiJV5SaXVBafgZwxDk5wc/sBwyML/Ctf0Q0zVCXCmW OTDEyjEVAxmyDsiiHYflAUHv4qhsSda0gMC/DRyit2MA3mRnqUiZ5BQX4lo8fKfqLKLbx/viIHRpi XTIF7INybXAVwOc/CcIgUsU2/AsPz//y1ynXUPr89Q2R+8pLT0x5+Zsu/QxHraSw==; 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) id 1xYGk6-00000000Ugq-1UB0; Tue, 01 Dec 2026 06:49:30 +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 5/5] selftests/sched_ext: Test p->scx.bpf_data across class changes Date: Tue, 1 Dec 2026 14:48:50 +0900 Message-ID: <20261201054850.780664-6-changwoo@igalia.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261201054850.780664-1-changwoo@igalia.com> References: <20261201054850.780664-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_data 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_data.bpf.c | 64 +++++++++ .../selftests/sched_ext/task_bpf_data.c | 123 ++++++++++++++++++ 3 files changed, 188 insertions(+) create mode 100644 tools/testing/selftests/sched_ext/task_bpf_data.bpf.c create mode 100644 tools/testing/selftests/sched_ext/task_bpf_data.c diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile index ef59c463cd33..c0a3046a36ab 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_data \ test_example \ total_bw \ unlocked_kfunc \ diff --git a/tools/testing/selftests/sched_ext/task_bpf_data.bpf.c b/tools/testing/selftests/sched_ext/task_bpf_data.bpf.c new file mode 100644 index 000000000000..f7044f0030ab --- /dev/null +++ b/tools/testing/selftests/sched_ext/task_bpf_data.bpf.c @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * A BPF program for testing p->scx.bpf_data. + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * Author: Changwoo Min + */ +#include + +char _license[] SEC("license") = "GPL"; + +UEI_DEFINE(uei); /* Error handling */ + +#define DATA_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_data; +bool probe_set_ret; + +void BPF_STRUCT_OPS(task_bpf_data_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_data) + __sync_fetch_and_add(&nr_enable_nonzero, 1); + + if (!scx_bpf_task_set_data(p, DATA_MAGIC | (u32)p->pid)) + __sync_fetch_and_add(&nr_enable_set_fail, 1); +} + +void BPF_STRUCT_OPS(task_bpf_data_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_data = p->scx.bpf_data; + probe_set_ret = scx_bpf_task_set_data(p, DATA_MAGIC | (u32)p->pid); + bpf_task_release(p); + return 0; +} + +SEC(".struct_ops.link") +struct sched_ext_ops task_bpf_data_ops = { + .enable = (void *)task_bpf_data_enable, + .exit = (void *)task_bpf_data_exit, + .flags = SCX_OPS_SWITCH_PARTIAL, + .name = "task_bpf_data", +}; diff --git a/tools/testing/selftests/sched_ext/task_bpf_data.c b/tools/testing/selftests/sched_ext/task_bpf_data.c new file mode 100644 index 000000000000..29c6e803283e --- /dev/null +++ b/tools/testing/selftests/sched_ext/task_bpf_data.c @@ -0,0 +1,123 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Test for p->scx.bpf_data 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_data.bpf.skel.h" +#include "scx_test.h" + +#define SCHED_EXT 7 +/* Must match task_bpf_data.bpf.c */ +#define DATA_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_data *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_data *skel, + struct bpf_link **link) +{ + u64 data = DATA_MAGIC | (u32)getpid(); + int status; + pid_t pid; + + skel->bss->target_pid = getpid(); + + /* Step 1: ops.enable() sets the value on SCHED_EXT */ + SCX_EQ(set_policy(SCHED_EXT, 0), 0); + SCX_EQ(probe(skel), 0); + SCX_EQ(skel->bss->probe_data, data); + SCX_EQ(skel->bss->probe_set_ret, true); + + /* Step 2: A child forked on SCHED_EXT doesn't inherit the value */ + pid = fork(); + SCX_FAIL_IF(pid < 0, "Failed to fork"); + if (pid == 0) + _exit(0); + SCX_EQ(waitpid(pid, &status, 0), pid); + SCX_EQ(probe(skel), 0); + SCX_EQ(skel->bss->probe_data, data); + SCX_EQ(skel->bss->probe_set_ret, true); + + /* + * Step 3: Switching to SCHED_FIFO disables the task, and the kernel + * clears the value. probe() then reads 0 into probe_data, and its set + * fails because the task is no longer enabled. + */ + SCX_EQ(set_policy(SCHED_FIFO, 1), 0); + SCX_EQ(probe(skel), 0); + SCX_EQ(skel->bss->probe_data, 0); + SCX_EQ(skel->bss->probe_set_ret, false); + + /* Step 4: Coming back sets the value again */ + SCX_EQ(set_policy(SCHED_EXT, 0), 0); + SCX_EQ(probe(skel), 0); + SCX_EQ(skel->bss->probe_data, data); + SCX_EQ(skel->bss->probe_set_ret, true); + + /* Step 5: Unloading the scheduler clears the value */ + SCX_ASSERT_ALIVE(skel, uei); + bpf_link__destroy(*link); + *link = NULL; + SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG)); + SCX_EQ(probe(skel), 0); + SCX_EQ(skel->bss->probe_data, 0); + SCX_EQ(skel->bss->probe_set_ret, false); + + /* This task came in twice and the child once, all with no value */ + 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_data *skel; + struct bpf_link *link; + + skel = task_bpf_data__open(); + SCX_FAIL_IF(!skel, "Failed to open skel"); + SCX_ENUM_INIT(skel); + SCX_FAIL_IF(task_bpf_data__load(skel), "Failed to load skel"); + + link = bpf_map__attach_struct_ops(skel->maps.task_bpf_data_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_data__destroy(skel); + + return status; +} + +struct scx_test task_bpf_data = { + .name = "task_bpf_data", + .description = "Verify p->scx.bpf_data across class changes", + .run = run, +}; +REGISTER_SCX_TEST(&task_bpf_data) -- 2.56.1