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=1796535469; b=j8Sgl28xHCTn+k9OnRUEdJGq8mpGbRH17ul0CCUsmGbqDHJh+EB3OeIk6PSLdPosm/FU f/xJGj1zOOrKtZ4Vt2SntgeqxHeLPOrwI34W+LdqdRj2Sf29LZya+hGbXh5oMUyL1LIFS XLRGH/cggyGeBwZhlDT8QKlw8+wdfiAafOpomdiKD4fuGTZ6xyotzhAqIQhqlSIehr1QL JgXjfHVltv7XZEsYhPY60WFJxkZbxtuJXYwGCtUevc8gUrhYq+CujdxQSaOxx2zLxxTSW z2xCdxWLge54REFz7KCsB3Lzqry0gXXSdBsNamxxq8SKrGYpIBiPUu9W/mGXbeBpjjA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796535469; 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=bqrCTyaQdkak7PJV8bUd8sUnVaT/XDnp1/Qu9/V98+o=; b=D7wrfajl4VZNIkMpuUgjQnWAjBFj8R5Sqg/ZiM2sD2YupOLGdKkzsqbUyAQnI3LQ3nqp mD1tfseUDhKWIj/RIkRwzOiv708Iouobkca7PFuyF+CJo9f3NXImy/A0nM7noOZ21qlHo byu9RbKPjmffBPUJC23edRDJ7P3PuZ/C/o/D+jr5OCxbrYBXM1neTb+xhGLSUxq99xeNC 6PTIhyH2NGIC5LVHBZDrlVbYVw9BcfpiZLc6zSabmoVX8phEAxTXNJ/LhSMHRL1l1L/F7 TmlzCrQsndbG74UZQ2bgUKgEC4XapMFYd22MKEfjyqZgTWafponURWWozrZdS3fZE0Q== 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="YfW/bdN0"; 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 428EB606A31F for ; Sun, 6 Dec 2026 05:37:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 428EB606A31F 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=YfW/bdN0 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=bqrCTyaQdkak7PJV8bUd8sUnVaT/XDnp1/Qu9/V98+o=; t=1796535468; x=2111895468; b=YfW/bdN0VOkh/q/bJ7RO6MUDwKRJAx1pdEOY/vpWxPa9KJt 3bMR7RYoQxVb2ZbLDB6jbGLmNc0YsFzroNwTtxzwt1mDXj5X8tTJaNuSfWc+3+xR/P6vCgbbN1Uhb TFMaw1qVlRm4FJNbRbi8iode3QctQNPKwG/OvqNXz969AYfSsktg8Em+LIPu9hetgK303zAQfAZnD FyiJHzzGZGmH2zlr/17OBe74LwJKgSLH5zKG0A0nVGgfFVvn7B3BYMlWgQGa1PQtnmT691VFNj7Ux u4MHhtSS0CVWckQ2BG2Jjze+PMyAcmqRV4EApY8VQ3G1hIBDVR7dFG05LIjpucFQ==; 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 1xa4wT-00000002dNI-1bBt; Sun, 06 Dec 2026 06:37:46 +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 v3 2/6] selftests/sched_ext: Test scx_bpf_sched_id() and scx_task_sched_id() Date: Sun, 6 Dec 2026 14:37:24 +0900 Message-ID: <20261206053728.2026272-3-changwoo@igalia.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261206053728.2026272-1-changwoo@igalia.com> References: <20261206053728.2026272-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 Load a partial-switch scheduler. The test task's owner id must equal the scheduler's id and be nonzero, before and after the task switches to SCHED_EXT, since a task that isn't SCHED_EXT is on the scheduler too. Both must read 0 once the scheduler is unloaded. A second instance must get a larger id. Signed-off-by: Changwoo Min --- tools/testing/selftests/sched_ext/Makefile | 1 + .../selftests/sched_ext/sched_id.bpf.c | 43 ++++++ tools/testing/selftests/sched_ext/sched_id.c | 129 ++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 tools/testing/selftests/sched_ext/sched_id.bpf.c create mode 100644 tools/testing/selftests/sched_ext/sched_id.c diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile index fe63dbd53f56..ef59c463cd33 100644 --- a/tools/testing/selftests/sched_ext/Makefile +++ b/tools/testing/selftests/sched_ext/Makefile @@ -198,6 +198,7 @@ auto-test-targets := \ select_cpu_dispatch_dbl_dsp \ select_cpu_vtime \ rt_stall \ + sched_id \ sub_caps_kfunc \ sub_core_caps \ test_example \ diff --git a/tools/testing/selftests/sched_ext/sched_id.bpf.c b/tools/testing/selftests/sched_ext/sched_id.bpf.c new file mode 100644 index 000000000000..661a61f28343 --- /dev/null +++ b/tools/testing/selftests/sched_ext/sched_id.bpf.c @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * A BPF program for testing scx_bpf_sched_id() and scx_task_sched_id(). + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * Author: Changwoo Min + */ +#include + +char _license[] SEC("license") = "GPL"; + +UEI_DEFINE(uei); /* Error handling */ + +/* Results of the last probe() run, overwritten by every run */ +u64 probe_sched_id; +u64 probe_task_sched_id; + +void BPF_STRUCT_OPS(sched_id_exit, struct scx_exit_info *ei) +{ + UEI_RECORD(uei, ei); +} + +/* + * Read this scheduler's id and the id of the calling task's scheduler. Only + * BPF_PROG_TEST_RUN from the test runs this program, synchronously in the + * test task, so the current task is the task under test. + */ +SEC("syscall") +int probe(void *ctx) +{ + struct task_struct *p = bpf_get_current_task_btf(); + + probe_sched_id = scx_bpf_sched_id(); + probe_task_sched_id = scx_task_sched_id(p); + return 0; +} + +SEC(".struct_ops.link") +struct sched_ext_ops sched_id_ops = { + .exit = (void *)sched_id_exit, + .flags = SCX_OPS_SWITCH_PARTIAL, + .name = "sched_id", +}; diff --git a/tools/testing/selftests/sched_ext/sched_id.c b/tools/testing/selftests/sched_ext/sched_id.c new file mode 100644 index 000000000000..f0568b2c5f0d --- /dev/null +++ b/tools/testing/selftests/sched_ext/sched_id.c @@ -0,0 +1,129 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Test for scx_bpf_sched_id() and scx_task_sched_id(). + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * Author: Changwoo Min + */ +#include +#include +#include +#include +#include "sched_id.bpf.skel.h" +#include "scx_test.h" + +#define SCHED_EXT 7 + +static int set_policy(int policy) +{ + struct sched_param param = {}; + + return sched_setscheduler(0, policy, ¶m); +} + +/* Run probe() on this task and return its result */ +static int probe(struct sched_id *skel) +{ + LIBBPF_OPTS(bpf_test_run_opts, topts); + + return bpf_prog_test_run_opts(bpf_program__fd(skel->progs.probe), + &topts) ?: topts.retval; +} + +/* + * Load a scheduler, move this task onto it and return the scheduler's id, after + * checking that the task reports the same owner and that both read 0 once the + * scheduler is unloaded. + */ +static enum scx_test_status load_and_check(u64 *id) +{ + enum scx_test_status status = SCX_TEST_FAIL; + struct sched_id *skel; + struct bpf_link *link = NULL; + + skel = sched_id__open(); + SCX_FAIL_IF(!skel, "Failed to open skel"); + SCX_ENUM_INIT(skel); + if (sched_id__load(skel)) { + SCX_ERR("Failed to load skel"); + goto out; + } + if (bpf_program__assoc_struct_ops(skel->progs.probe, + skel->maps.sched_id_ops, NULL)) { + SCX_ERR("Failed to associate probe with sched_id_ops"); + goto out; + } + + link = bpf_map__attach_struct_ops(skel->maps.sched_id_ops); + if (!link) { + SCX_ERR("Failed to attach struct_ops"); + goto out; + } + + /* + * Step 1: Under %SCX_OPS_SWITCH_PARTIAL, a task that isn't SCHED_EXT + * is on the scheduler too. The task must report the scheduler's id + * before and after switching to SCHED_EXT. + */ + if (probe(skel)) { + SCX_ERR("Failed to probe"); + goto out; + } + *id = skel->bss->probe_sched_id; + if (!*id || skel->bss->probe_task_sched_id != *id) { + SCX_ERR("sched_id=%llu task_sched_id=%llu", + (unsigned long long)*id, + (unsigned long long)skel->bss->probe_task_sched_id); + goto out; + } + if (set_policy(SCHED_EXT) || probe(skel)) { + SCX_ERR("Failed to switch to SCHED_EXT and probe"); + goto out; + } + if (skel->bss->probe_task_sched_id != *id) { + SCX_ERR("On SCHED_EXT: task_sched_id=%llu", + (unsigned long long)skel->bss->probe_task_sched_id); + goto out; + } + + /* Step 2: Both read 0 after the scheduler is unloaded */ + bpf_link__destroy(link); + link = NULL; + if (probe(skel) || skel->bss->probe_sched_id || + skel->bss->probe_task_sched_id) { + SCX_ERR("After unload: sched_id=%llu task_sched_id=%llu", + (unsigned long long)skel->bss->probe_sched_id, + (unsigned long long)skel->bss->probe_task_sched_id); + goto out; + } + + status = SCX_TEST_PASS; +out: + set_policy(SCHED_OTHER); + bpf_link__destroy(link); + sched_id__destroy(skel); + return status; +} + +static enum scx_test_status run(void *ctx) +{ + enum scx_test_status status; + u64 id1 = 0, id2 = 0; + + status = load_and_check(&id1); + SCX_EQ(status, SCX_TEST_PASS); + + /* Step 3: A new instance gets a larger id */ + status = load_and_check(&id2); + SCX_EQ(status, SCX_TEST_PASS); + SCX_GT(id2, id1); + + return SCX_TEST_PASS; +} + +struct scx_test sched_id = { + .name = "sched_id", + .description = "Verify scx_bpf_sched_id() and scx_task_sched_id()", + .run = run, +}; +REGISTER_SCX_TEST(&sched_id) -- 2.56.1