BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Tao Cui" <cui.tao@linux.dev>
Cc: bpf@vger.kernel.org, tj@kernel.org, hannes@cmpxchg.org, mkoutny@suse.com
Subject: Re: [PATCH v15 3/4] selftests/bpf: add iocost cost model test
Date: Thu, 03 Dec 2026 15:32:37 +0000	[thread overview]
Message-ID: <sashiko-outbox-248116@kernel.org> (raw)
In-Reply-To: <20261203150338.2625598-4-cui.tao@linux.dev>

Thank you for your contribution! Sashiko AI review found 10 potential
issues to consider:
- [Medium] selftests/bpf: iocost_test_env() skips all tests when
  CONFIG_KALLSYMS_ALL=n
- [Low] selftests/bpf: contradictory ASSERT_OK() and test__skip() in
  iocost_model.c
- [Low] selftests/bpf: mount ns pollution and close(-1) in
  iocost_test_env()
- [Low] selftests/bpf: file_contains() duplicates libbpf BTF and
  kallsyms helpers
- [Low] selftests/bpf: duplicate write and rollback code in
  iocost_model.c
- [Low] selftests/bpf: unconditional close(orig_cwd) in
  iocost_test_env()
- [Low] selftests/bpf: fixed sleep(3) calls stall serial iocost tests
  for 6s
- [Low] selftests/bpf: open_nullb_dev() leaks manually created /dev
  nodes
- [Low] selftests/bpf: udev TOCTOU race on mknod() in open_nullb_dev()
- [Low] selftests/bpf: unpaired cleanup_cgroup_environment() on setup
  failure
--

commit 1da50772d61bfc9d55dc63e068869c415d99ec21
Author: Tao Cui <cuitao@kylinos.cn>

selftests/bpf: add iocost cost model test

This commit adds two BPF struct_ops iocost cost model examples and serial
selftests that attach them to configfs-created null_blk devices to test
model switching, IO pricing, tracepoint observation, and device removal.

> diff --git a/tools/testing/selftests/bpf/prog_tests/iocost_model.c b/tools/testing/selftests/bpf/prog_tests/iocost_model.c
> new file mode 100644
> index 0000000000000..11bcb33a1eb5e
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/iocost_model.c
> @@ -0,0 +1,1021 @@

[ ... ]

> +static int file_contains(const char *path, const char *needle)
> +{

[ ... ]

> +static int iocost_bpf_supported(void)
> +{
> +	static int cached = -1;
> +
> +	if (cached >= 0)
> +		return cached;
> +
> +	cached = file_contains("/sys/kernel/btf/vmlinux",
> +			       "bpf_struct_ops_iocost_model_ops");
> +	return cached;
> +}

[Severity: Low]
Can file_contains() be dropped in favor of the existing BTF and kallsyms
helpers?

Probing /sys/kernel/btf/vmlinux by slurping raw bytes with memmem()
duplicates btf__load_vmlinux_btf() and btf__find_by_name_kind(...,
BTF_KIND_STRUCT).  Similarly, the /proc/kallsyms lookup in iocost_test_env()
reads the entire file into heap memory on every test invocation instead of
reusing kallsyms_find() from trace_helpers.h.

[ ... ]

> +	if (unlink(path) && errno != ENOENT)
> +		return -errno;
> +	if (mknod(path, S_IFBLK | 0600, dev))
> +		return -errno;

[Severity: Low]
Can udev racing between unlink() and mknod() cause open_nullb_dev() to fail
with -EEXIST?

If udev processes the KOBJ_ADD uevent and creates /dev/<name> after the
initial open() or unlink() and before mknod(), mknod() fails with EEXIST and
the test fails its ASSERT_GE() instead of retrying open() and validating
fstat().

> +	fd = open(path, flags);
> +	if (fd < 0)
> +		unlink(path);
> +	return fd;
> +}

[Severity: Low]
Does open_nullb_dev() leak manually created /dev/<name> nodes on systems
without devtmpfs or udev?

iocost_test_env() does not mount a private /dev, and when mknod() succeeds
neither the callers nor nullb_remove() unlink the node on teardown, leaving
a stale block device node in /dev.

[ ... ]

> +	if (!ready) {
> +		orig_cwd = open(".", O_RDONLY | O_DIRECTORY);
> +		orig_ns = open("/proc/self/ns/mnt", O_RDONLY);
> +		if (unshare(CLONE_NEWNS) ||
> +		    mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) {

[Severity: Low]
Does iocost_test_env() leave the main test_progs process in a modified mount
namespace for all later serial tests?

All serial_test_* functions run sequentially in the main test_progs process,
and on success orig_ns is closed without restoring it, leaving /sys/fs/cgroup
and /sys/kernel/config altered for subsequent tests.  Both
serial_test_iocost_model() and serial_test_iocost_model_streams() already
call cgroup_setup_and_join(), which calls unshare(CLONE_NEWNS) per test
anyway.

[ ... ]

> +	/*
> +	 * the tp_btf attach goes through the raw-tracepoint syscall and
> +	 * needs no tracefs mount; probe the tracepoint by its kallsyms
> +	 * entry instead. A probe error skips like an absent tracepoint:
> +	 * transient failures are retried on the next run.
> +	 */
> +	if (file_contains("/proc/kallsyms",
> +			  "__tracepoint_iocost_ioc_tick") <= 0) {
> +		printf("%s:SKIP:needs the iocost_ioc_tick tracepoint\n",
> +		       __func__);
> +		test__skip();
> +		goto out;
> +	}

[Severity: Medium]
Does this skip all three iocost tests on kernels built with
CONFIG_KALLSYMS_ALL=n?

__DEFINE_TRACE_EXT() places __tracepoint_iocost_ioc_tick in the
__tracepoints data section, which scripts/kallsyms.c omits unless
--all-symbols (CONFIG_KALLSYMS_ALL) is enabled, and
tools/testing/selftests/bpf/config does not enable CONFIG_KALLSYMS_ALL.
Probing vmlinux BTF for btf_trace_iocost_ioc_tick (or /proc/kallsyms for the
text symbol __traceiter_iocost_ioc_tick) works regardless of
CONFIG_KALLSYMS_ALL.

[ ... ]

> +out:
> +	if (!ok)
> +		ready = false;
> +	if (orig_ns >= 0) {
> +		if (!ok)
> +			setns(orig_ns, CLONE_NEWNS);
> +		close(orig_ns);
> +	}
> +	if (orig_cwd >= 0)
> +		fchdir(orig_cwd);
> +	close(orig_cwd);
> +	return ok;
> +}

[Severity: Low]
Are braces missing around the orig_cwd >= 0 block?

As written, close(orig_cwd) runs unconditionally, calling close(-1) on every
invocation after initialization or when open(".") fails, and fchdir(orig_cwd)
runs even on the success path where setns() was not called.

[ ... ]

> +static int write_cost_model(const char *dev, const char *what)
> +{
> +	char buf[128];
> +	FILE *fp;
> +	int err = 0;
> +
> +	snprintf(buf, sizeof(buf), "%s %s", dev, what);
> +	fp = fopen("/sys/fs/cgroup/io.cost.model", "w");
> +	if (!fp)
> +		return errno ? -errno : -EACCES;
> +	if (fwrite(buf, 1, strlen(buf), fp) != strlen(buf))
> +		err = ferror(fp) ? (errno ? -errno : -EIO) : -EIO;
> +	if (fclose(fp) && !err)
> +		err = -errno;
> +	return err;
> +}

[Severity: Low]
Can write_cost_model() delegate to write_file("/sys/fs/cgroup/io.cost.model",
buf) instead of duplicating the fopen/fwrite/fclose error handling?

Similarly, in nullb_create(), the error path after read_file_token()
duplicates the power-off and rmdir sequence at the err_rmdir label directly
below it instead of using goto err_rmdir.

[ ... ]

> +		/*
> +		 * the usage stat is flushed by the iocost timer, give
> +		 * the current period a chance to end before reading
> +		 */
> +		sleep(3);
> +		ASSERT_OK(read_cost_usage("/iocost_pairing",
> +					  dev, &u2), "cost_usage_post");
> +		ASSERT_GT(u2, u1, "cost_charged");
> +		ASSERT_GT(skel->bss->ioc_ticks, 0, "tick_seen");

[Severity: Low]
Can this and the matching sleep(3) in serial_test_iocost_model_streams()
poll with a bounded deadline instead of sleeping for 6 seconds total?

All serial_test_* functions run sequentially on the main test_progs thread
after parallel workers finish, so unconditional sleeps extend the overall
critical-path runtime.  On non-rotational null_blk (AUTOP_SSD_DFL),
ioc_refresh_period_us() uses a 50 ms period (and MAX_PERIOD is 1 s), so
polling read_cost_usage() and skel->bss->ioc_ticks up to a 3-second deadline
would typically finish in ~50-100 ms.

[ ... ]

> +		cg = cgroup_setup_and_join("/iocost_ms");
> +		if (!ASSERT_GE(cg, 0, "setup_cg_ms")) {
> +			iocost_ms__destroy(skel);
> +			nullb_remove(IOCOST_NULLB);
> +			cleanup_cgroup_environment();
> +			return;
> +		}

[Severity: Low]
Should cleanup_cgroup_environment() be omitted when cgroup_setup_and_join()
fails?

cgroup_setup_and_join() either fails before mounting /mnt or calls
cleanup_cgroup_environment() itself before returning an error, so calling it
again runs join_cgroup_from_top("/mnt") on an unmounted /mnt and logs
spurious errors (serial_test_iocost_model() omits it on the same path).

[ ... ]

> +			if (!ASSERT_OK(posix_memalign(&iobuf, 4096, 4096),
> +				       "memalign_iobuf")) {
> +				printf("%s:SKIP:cannot allocate the aligned IO buffer\n",
> +				       __func__);
> +				test__skip();
> +			} else {

[Severity: Low]
Should this branch either fail via ASSERT_OK() or skip via test__skip(),
rather than doing both?

When posix_memalign() fails, ASSERT_OK() increments test_state->error_cnt
and test__skip() increments test_state->skip_cnt; test_progs.c then reports
the test as failed while also adding it to the summary skip count.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203150338.2625598-1-cui.tao@linux.dev?part=3

  reply	other threads:[~2026-12-03 15:32 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 15:03 [PATCH v15 0/4] blk-iocost: BPF struct_ops cost model Tao Cui
2026-12-03 15:03 ` [PATCH v15 1/4] blk-iocost: add BPF struct_ops cost model support Tao Cui
2026-12-03 15:44   ` bot+bpf-ci
2026-12-03 15:46   ` sashiko-bot
2026-12-03 15:03 ` [PATCH v15 2/4] blk-iocost: add iocost_ioc_tick tracepoint for per-period device summary Tao Cui
2026-12-03 15:03 ` [PATCH v15 3/4] selftests/bpf: add iocost cost model test Tao Cui
2026-12-03 15:32   ` sashiko-bot [this message]
2026-12-03 15:33   ` bot+bpf-ci
2026-12-03 15:03 ` [PATCH v15 4/4] docs: cgroup-v2: document the iocost BPF cost model attachment Tao Cui
2026-12-03 15:19   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-248116@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=cui.tao@linux.dev \
    --cc=hannes@cmpxchg.org \
    --cc=mkoutny@suse.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox