From nobody Fri Sep 25 18:24:27 2026 Received: from mail-yx2-f8.google.com (mail-yx2-f8.google.com [74.125.224.136]) (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 E2F8D56C659 for ; Wed, 9 Sep 2026 14:52:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=74.125.224.136 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788965578; cv=pass; b=ZQ11+yl/1JKtxqr9sHEo6+737nzGhJ1AEAOlHBcUOIaFPwK0Sz1/kPapLynMXQTYSqu+sizJbHmv+rzQXAaA0HgKJrownPSDComksnfyVU11K3XX9kSm5INO6gRJcjrreFcf877exHkf4H5xKT3dIzbYVPKJ5grOtmPEBv1QzrM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788965578; c=relaxed/simple; bh=IvuVgXtzg+PW3WsJyb8eWnV+a5ycZtkTPhA0W31/AqE=; h=MIME-Version:From:Date:Message-ID:Subject:To:Cc:Content-Type; b=sJ7acnYGrstUmTAIREhVnaEdU5EmpC+U1He3WkAFais49sxg3alYyVdZkToDocC5Q7WqhEIM0o4l77RhV5LMPd12NbAy2wTPxIu2XRZGsYDKmgWKoq2uFGHZlQWNflDOjU3YhTdUNBlX+OX4EscsrQqxGg03YvQNK0MRlOnCCfc= ARC-Authentication-Results: i=2; 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=D+Ox5B5k; arc=pass smtp.client-ip=74.125.224.136 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="D+Ox5B5k" Received: by mail-yx2-f8.google.com with SMTP id 00721157ae682-854f090ff98so30794527b3.0 for ; Wed, 09 Sep 2026 07:52:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1788965573; cv=none; d=google.com; s=arc-20260327; b=N4rXTPXeoTFMaav9EoPS65isj1TUaDtudVRjAd+p4S52fbd69fFyweVXXebZXWYzPK 8dq8ZZCtT0KfF9azP9jZQT/QtvKXxvHpfqorJvmK6D4A9RYxcOgPF3LLfgSiRUD7TFR/ uJU1UvLE/Wltv31/fd5qW1i4WKbfCmar9o6NijjgglMTqRUM3f+9d+sECOVHc6BlSyBK Bvpi/gh4i5AdZiYfgMCTogncKxoDIYOWHD5sVCAQagVlk1k0UrmQpX0XsrpPStkBOgNj Rn0QMYz7bnuJRSsMFMMlxyHvKV021yGeHjBTd8/IcqzqQLh35DUp9DxdPNqm5Gp14m81 9e3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=cc:to:subject:message-id:date:from:mime-version:dkim-signature; bh=RwJ9BaoNRw2XUxKdRuqOWTaCFPLrZEbkBSC1yAfl2n8=; fh=itnlQjBruW0AZx5mXBS/OIWI1SZDQp8iYk6kXBhbq2I=; b=ZMk/YpZl5T3/CRAsQ7OXSjdntYb+8qZrdeiWCxTvM/YcDaa1OMjxHHCFprUkZvXiZ4 lDeLMQub7sQeR+o/ad4VDRJi7qKf3S+FdrnU3a8pvoUg7XAFhdgjOT3w2qSWZuieaSl0 vlxS9fbIbQe34SjVVdZv5vAae+8s/aJ+7h5+NZgEyCPVvidyGhgoa2980/7BNkhSJsCt bHtaQtL19sUGRUUF5mZFyWnRYocjRn0YfpE/haOze/pimi6l3EPJcuBgfBTVu4TzKJue Hfwyn/ewsCVG4Vy8pT1fTME5u6NoAtoPdn7+ab++1V5dZ5HNqhVUJEUO6TmGhpzHiZof TZcA==; darn=vger.kernel.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788965573; x=1789570373; darn=vger.kernel.org; h=content-type:cc:to:subject:message-id:date:from:mime-version:from :to:cc:subject:date:message-id:reply-to:content-type; bh=RwJ9BaoNRw2XUxKdRuqOWTaCFPLrZEbkBSC1yAfl2n8=; b=D+Ox5B5k11jjdIriBLGwL805Pn8ZG7VxJ0ao6SNLVvil4GQ0yQhTIMAZ6r+nGFTOjh ksp3yQlRbs5hCgz0c6EyjhAQ6I2tI1jHhNMrADjD13QvHrsq0kWr9/YNs0SEBJb0P+UM 5fCbJtRirJm0Dw8XzdbJYmV3nhUjP1OWrmvg369/759SNxRv/GWkpFU+G+mAGo0YFvKK +g4QT3d5nOfgJmQX2x1RDMo7W28p0vO+OqPBuexMpLYVFr2ONPYqAYLcfzzCp/D74czx 1fIRQdaz87eq9xNq3T5Qj6CybaVjX8uUMQF3ugC75CJUotWhjkI3whdz2Sk1CCxGZCVB eQ7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788965573; x=1789570373; h=content-type:cc:to:subject:message-id:date:from:mime-version :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=RwJ9BaoNRw2XUxKdRuqOWTaCFPLrZEbkBSC1yAfl2n8=; b=AydBhnHYx4eXeJ1RtyMXBg9qqeJiY3DpoThoKGFKYu6EV5bw6W/fzyP2eL5tpmPX0t e9dF6ipmT3omF9xMov3h5cw1sLCNBjaXGNzzRJB7+2IiS+h6b4y73kWqRvzo9/jBhbeD hMUH4DN/bF38RqvwF7ieq4Gb+eQ7SKMLBMWq4kaZucvq4gW946YfrzFkk/pmZQC5WNwW d6p+lLFv6ibPTGzh6ZbvxxMTdDO1YUt8QQuJZYIJYSHTA/NtE/ygksG3pQgEtNDTrIiw 1V0ZT+l+jQfvMQfjT4nHIKAPCDhQGGrUzmM1CfL0qwqJa7Rg3Uvw7XmX7+DFNaYqVKPf t9rg== X-Forwarded-Encrypted: i=1; AKwUvBzwyw1+whypTtKq9yrHS4XzxhTSW3Qit/00nD8Qa2ibUM30VtIQCrGGD2e+0uxbIHKIpOvEplobsziTcHI=@vger.kernel.org X-Gm-Message-State: AFuF++mnUIZT/ttaOuf7UdrHLR2t+aYgoCKmNFihn6uyPGKtSvBg/rTy CM1xLcWi5qIT0eVRxSbvBhCeyq0hVKcv0V/Q6na7DVR4kjaEFyRC36gqPnXdC57WJtV6+309rz3 2b6alGHy+OnNo0yj+ZyGlhoDyV2M180o= X-Gm-Gg: AYBFou1Ae667ONpZ0F4W1htbx2wz5StR6mBBG67sHH9iq81K75gdlTjllLhvuY5PpdI RatVWTR/wf0jD+GfUULgc3DSbHKdKVgjzTRFUcCsZ0jEKiYPun/r0h759koTnH/1DuWf25VxRlA 0RO2I49k0knJjQwy1qKwK4FWYULJ9u+J9P5ik6LQCwEOx+R030FRENS4NwtJ2c+4VAOF+Wo1Zh8 ETPNi5UJm41vNhufLI8Ttb/P6KzsEs7Vx8/8Q/4Hd67o2VQeFRtid+lC+sTGcR4SXIONDUmdMyf PjwWjIgvoLdw9K7Gt0d3NYxgpo3UW4sxz6Qfw8hm7frA2qTrXI8DVg== X-Received: by 2002:a53:a44c:0:b0:66f:c1bc:402b with SMTP id 956f58d0204a3-66fc1bc4611mr7449425d50.88.1788965573319; Wed, 09 Sep 2026 07:52:53 -0700 (PDT) Received: from 77377267392 named unknown by gmailapi.google.com with HTTPREST; Wed, 9 Sep 2026 07:52:52 -0700 Received: from 77377267392 named unknown by gmailapi.google.com with HTTPREST; Wed, 9 Sep 2026 07:52:52 -0700 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Tianyi Chen Date: Wed, 9 Sep 2026 07:52:52 -0700 X-Gm-Features: AcwNN1W1BAWoXddrkNiAeAET4QY2y25VwsVKw_MOPnWrmOuHsPqYEsTulYrFU6Y Message-ID: Subject: [PATCH v2 RESEND] selftests/sched_ext: Validate select_cpu_and mask constraints To: Tejun Heo Cc: Shuah Khan , sched-ext@lists.linux.dev, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Tianyi Chen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Tianyi Chen Date: Sun, 06 Sep 2026 22:34:48 +0800 The syscall test only prints the CPU selection result after checking whether BPF_PROG_TEST_RUN succeeds. Require -EBUSY for empty and affinity-disjoint custom masks, and allow a legal singleton candidate to be selected or busy. Reject unexpected errors and selections outside the custom mask or configured affinity. Check cpus_mask because migration disabling can temporarily narrow cpus_ptr under CONFIG_PREEMPT_RCU. Grow dynamically allocated affinity masks until sched_getaffinity() succeeds, retaining that allocation size for pinning and restoration. Restore affinity on all exits after pinning, and skip only the disjoint case when fewer than two CPUs are allowed. Assisted-by: LLM Signed-off-by: Tianyi Chen --- Resending from my Gmail address because delivery from my previous address failed for the kernel.org and kernel mailing-list recipients. The patch is unchanged from v2 and still applies to sched_ext/for-7.4. Changes in v2: - Check configured affinity through p->cpus_mask, avoiding temporary cpus_ptr narrowing when migration is disabled. - Use zeroed, dynamically allocated affinity masks, retrying EINVAL with a larger size and handling restoration and cleanup on failure paths. - Rebase onto sched_ext/for-7.4. Validation: - Build and allowed_cpus tests passed with CONFIG_PREEMPT_RCU, including single-CPU affinity and ten runs under load with preempt=3Dfull. - A guest with 2048 possible CPUs and two online CPUs reproduced v1's EINVAL. V2 grew the mask from 128 to 256 bytes and passed with both full and single-CPU affinity. - Injected affinity-read, pinning, restoration and BPF test-run errors exercised failure handling, including restoration after a BPF failure. Forced mask growth with malloc perturbation also passed. v1: https://lore.kernel.org/r/20260906144029.848978-1-hi@tychen.cc Review: https://lore.kernel.org/r/53d04980eecb0e5f6eeea25a5cf0dcf5@kernel.o= rg .../selftests/sched_ext/allowed_cpus.bpf.c | 24 +++- .../selftests/sched_ext/allowed_cpus.c | 129 ++++++++++++++++-- 2 files changed, 135 insertions(+), 18 deletions(-) diff --git a/tools/testing/selftests/sched_ext/allowed_cpus.bpf.c b/tools/testing/selftests/sched_ext/allowed_cpus.bpf.c index 9dd72d0da29..f14d7e5bef9 100644 --- a/tools/testing/selftests/sched_ext/allowed_cpus.bpf.c +++ b/tools/testing/selftests/sched_ext/allowed_cpus.bpf.c @@ -147,23 +147,41 @@ void BPF_STRUCT_OPS(allowed_cpus_exit, struct scx_exit_info *ei) } struct task_cpu_arg { - pid_t pid; + u64 pid; + s64 custom_cpu; }; SEC("syscall") int select_cpu_from_user(struct task_cpu_arg *input) { struct task_struct *p; - int cpu; + struct bpf_cpumask *mask; + s32 cpu; p =3D bpf_task_from_pid(input->pid); if (!p) return -EINVAL; + mask =3D bpf_cpumask_create(); + if (!mask) { + bpf_task_release(p); + return -ENOMEM; + } + + /* A negative custom_cpu leaves the custom mask empty. */ + if (input->custom_cpu >=3D 0) + bpf_cpumask_set_cpu(input->custom_cpu, mask); + bpf_rcu_read_lock(); - cpu =3D scx_bpf_select_cpu_and(p, bpf_get_smp_processor_id(), 0, p->cpus_ptr, 0); + cpu =3D scx_bpf_select_cpu_and(p, bpf_get_smp_processor_id(), 0, + cast_mask(mask), 0); + if (cpu >=3D 0 && + (!bpf_cpumask_test_cpu(cpu, cast_mask(mask)) || + !bpf_cpumask_test_cpu(cpu, &p->cpus_mask))) + cpu =3D -ERANGE; bpf_rcu_read_unlock(); + bpf_cpumask_release(mask); bpf_task_release(p); return cpu; diff --git a/tools/testing/selftests/sched_ext/allowed_cpus.c b/tools/testing/selftests/sched_ext/allowed_cpus.c index 093f285ab4b..773699d120e 100644 --- a/tools/testing/selftests/sched_ext/allowed_cpus.c +++ b/tools/testing/selftests/sched_ext/allowed_cpus.c @@ -2,7 +2,10 @@ /* * Copyright (c) 2025 Andrea Righi */ +#define _GNU_SOURCE #include +#include +#include #include #include #include @@ -23,17 +26,19 @@ static enum scx_test_status setup(void **ctx) return SCX_TEST_PASS; } -static int test_select_cpu_from_user(const struct allowed_cpus *skel) +static int test_select_cpu_from_user(const struct allowed_cpus *skel, + const char *name, int custom_cpu, + bool expect_busy) { int fd, ret; - __u64 args[1]; + __s32 cpu; + __u64 args[] =3D { getpid(), (__u64)(__s64)custom_cpu }; LIBBPF_OPTS(bpf_test_run_opts, attr, .ctx_in =3D args, .ctx_size_in =3D sizeof(args), ); - args[0] =3D getpid(); fd =3D bpf_program__fd(skel->progs.select_cpu_from_user); if (fd < 0) return fd; @@ -42,29 +47,123 @@ static int test_select_cpu_from_user(const struct allowed_cpus *skel) if (ret < 0) return ret; - fprintf(stderr, "%s: CPU %d\n", __func__, attr.retval); + /* test_run returns the signed BPF result through an unsigned field. */ + cpu =3D (__s32)attr.retval; + if ((expect_busy && cpu !=3D -EBUSY) || + (!expect_busy && cpu !=3D -EBUSY && cpu !=3D custom_cpu)) { + SCX_ERR("%s: unexpected CPU selection result %d", name, cpu); + return -EINVAL; + } return 0; } +/* Grow until the mask covers the kernel's CPU range, including offline CPUs. */ +static int alloc_affinity(cpu_set_t **mask, size_t *size) +{ + int nr_cpus =3D CPU_SETSIZE; + cpu_set_t *cpus; + int err; + + for (;;) { + *size =3D CPU_ALLOC_SIZE(nr_cpus); + cpus =3D CPU_ALLOC(nr_cpus); + if (!cpus) + return -ENOMEM; + CPU_ZERO_S(*size, cpus); + if (!sched_getaffinity(0, *size, cpus)) { + *mask =3D cpus; + return nr_cpus; + } + err =3D errno; + CPU_FREE(cpus); + if (err !=3D EINVAL) + return -err; + if (nr_cpus > INT_MAX / 2) + return -EOVERFLOW; + nr_cpus *=3D 2; + } +} + static enum scx_test_status run(void *ctx) { struct allowed_cpus *skel =3D ctx; - struct bpf_link *link; + enum scx_test_status status =3D SCX_TEST_FAIL; + cpu_set_t *original =3D NULL, *pinned =3D NULL; + bool affinity_changed =3D false; + size_t size; + int first =3D -1, second =3D -1, cpu, nr_cpus; + struct bpf_link *link =3D NULL; + + nr_cpus =3D alloc_affinity(&original, &size); + if (nr_cpus < 0) { + SCX_ERR("Failed to get affinity (%d)", -nr_cpus); + goto out; + } + pinned =3D CPU_ALLOC(nr_cpus); + if (!pinned) { + SCX_ERR("Failed to allocate affinity mask"); + goto out; + } + for (cpu =3D 0; cpu < nr_cpus; cpu++) { + if (!CPU_ISSET_S(cpu, size, original)) + continue; + if (first < 0) { + first =3D cpu; + } else { + second =3D cpu; + break; + } + } + if (first < 0) { + SCX_ERR("No CPU in affinity mask"); + goto out; + } link =3D bpf_map__attach_struct_ops(skel->maps.allowed_cpus_ops); - SCX_FAIL_IF(!link, "Failed to attach scheduler"); - - /* Pick an idle CPU from user-space */ - SCX_FAIL_IF(test_select_cpu_from_user(skel), "Failed to pick idle CPU"); - - /* Just sleeping is fine, plenty of scheduling events happening */ + if (!link) { + SCX_ERR("Failed to attach scheduler"); + goto out; + } + + if (test_select_cpu_from_user(skel, "empty mask", -1, true)) + goto out; + + /* A legal candidate may be busy; selection need not succeed. */ + if (test_select_cpu_from_user(skel, "legal candidate", first, false)) + goto out; + + if (second >=3D 0) { + CPU_ZERO_S(size, pinned); + CPU_SET_S(first, size, pinned); + if (sched_setaffinity(0, size, pinned)) { + SCX_ERR("Failed to pin task (%d)", errno); + goto out; + } + affinity_changed =3D true; + if (test_select_cpu_from_user(skel, "disjoint masks", second, true)) + goto out; + } else { + fprintf(stderr, "Skipping disjoint masks: need two allowed CPUs\n"); + } + + /* Just sleeping is fine, plenty of scheduling events happening. */ sleep(1); - - SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_NONE)); + if (skel->data->uei.kind !=3D EXIT_KIND(SCX_EXIT_NONE)) { + SCX_ERR("Scheduler exited unexpectedly"); + goto out; + } + status =3D SCX_TEST_PASS; + +out: + if (affinity_changed && sched_setaffinity(0, size, original)) { + SCX_ERR("Failed to restore affinity (%d)", errno); + status =3D SCX_TEST_FAIL; + } bpf_link__destroy(link); - - return SCX_TEST_PASS; + CPU_FREE(pinned); + CPU_FREE(original); + return status; } static void cleanup(void *ctx) --=20 2.55.0