All of lore.kernel.org
 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: [RFC PATCH v3 2/5] selftests/bpf: add iocost cost model test
Date: Mon, 14 Sep 2026 08:04:03 +0000	[thread overview]
Message-ID: <20260914080404.5BF031F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260914073356.791518-3-cui.tao@linux.dev>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Error path for cgroup storage allocation failure skips IOCOST_COST_F_MERGE adjustment.
- [Low] Multi-line comments do not follow the BPF subsystem strict guidelines.
- [Low] IOC_PAGE_SHIFT is undeclared in the BPF program, causing a compilation failure.
--

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

selftests/bpf: add iocost cost model test

This commit adds an example cost model test implementing the builtin linear
HDD formula at double cost. It registers the model via struct_ops and binds
it to a device using io.cost.model, verifying the readback and testing write
errors during restoration.

> 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..bf9f245e29a52
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/iocost_model.c
[ ... ]
> +static int bind_model(const char *dev, const char *name)
> +{
> +	char buf[300], got[64];
> +	int err;
> +
> +	snprintf(buf, sizeof(buf), "%s model=%s\n", dev, name);
> +	err = write_cost_model(buf);
> +	if (err)
> +		return err > 0 ? -err : -EIO;
> +	err = readback_model(dev, got, sizeof(got));
> +	if (!err && !strcmp(got, name)) {
> +		/* restore the builtin linear model on every path so the
> +		 * device is not left bound to a model the caller may
> +		 * unregister right after; the write is checked too */

[Severity: Low]
This isn't a bug, but does this violate the BPF subsystem comment style
guidelines? The BPF subsystem strict guidelines require multi-line comments
to have the opening /* on its own line.

> +		snprintf(buf, sizeof(buf), "%s model=linear\n", dev);
> +		err = write_cost_model(buf);
> +		return err > 0 ? -err : (err ? -EIO : 0);
> +	}
> +
> +	/* bind failed or does not read back: still restore; prefer
> +	 * the original failure, but surface the restore error too */

[Severity: Low]
This isn't a bug, but this comment also places the opening /* on the same
line as the text, which doesn't match the required BPF multi-line comment
style.

> +	snprintf(buf, sizeof(buf), "%s model=linear\n", dev);
> +	{
> +		int rerr = write_cost_model(buf);
> +
> +		return err ? (err > 0 ? -err : -EIO)
> +			   : (rerr > 0 ? -rerr : -EIO);
> +	}
> +}
[ ... ]
> diff --git a/tools/testing/selftests/bpf/progs/iocost_model.c b/tools/testing/selftests/bpf/progs/iocost_model.c
> new file mode 100644
> index 0000000000000..81928f897297c
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/iocost_model.c
[ ... ]
> +#define IOC_PAGE_SIZE		4096
> +#define IOC_SECT_TO_PAGE_SHIFT	3	/* 512B sectors to 4kB pages */
> +#define LCOEF_RANDIO_PAGES	4096	/* 16MB seek threshold */
> +#define IOCOST_COST_F_MERGE	(1ULL << 0)	/* not in BTF: a plain macro */
> +#define IOCOST_REQ_OP_MASK	0xff		/* REQ_OP_MASK, not in BTF */

[Severity: Low]
Is IOC_PAGE_SHIFT missing from these manual constant definitions?

> +
> +/*
> + * DIV64_U64_ROUND_UP / DIV_ROUND_UP_ULL equivalents, folded at
> + * compile time
> + */
[ ... ]
> +SEC("struct_ops")
> +u64 BPF_PROG(iocost_2x_calc_cost, u64 opf, u64 nbytes, u64 sector,
> +	     struct blkcg *blkcg, u64 model_flags)
> +{
> +	u64 pages, seek_pages = 0, base, coef_page, randio, cost;
> +
> +	/* builtin truncates: max(sectors >> IOC_SECT_TO_PAGE_SHIFT, 1) */
> +	pages = nbytes >> IOC_PAGE_SHIFT;

[Severity: Low]
Does this code compile? IOC_PAGE_SHIFT is defined in an anonymous enum within
block/blk-iocost.c and is not exported to vmlinux.h. Attempting to use it
here directly seems like it would cause a build error.

> +	if (!pages)
> +		pages = 1;
> +
> +	if ((opf & IOCOST_REQ_OP_MASK) == REQ_OP_READ) {
[ ... ]
> +	{
> +		__u64 *cursor, cur;
> +		int priced = (opf & IOCOST_REQ_OP_MASK) == REQ_OP_READ ||
> +			     (opf & IOCOST_REQ_OP_MASK) == REQ_OP_WRITE;
> +
> +		cursor = bpf_cgrp_storage_get(&cursor_store,
> +					      blkcg->css.cgroup, NULL,
> +					      BPF_LOCAL_STORAGE_GET_F_CREATE);
> +		if (!cursor)
> +			return 2 * (base + pages * coef_page);

[Severity: Medium]
Does this early return miss the IOCOST_COST_F_MERGE adjustment?

If bpf_cgrp_storage_get() returns NULL, we return immediately here, which
skips the subsequent logic that zeros out base when model_flags has
IOCOST_COST_F_MERGE set. Could this result in merged operations being
incorrectly charged a base cost under failure conditions?

> +		cur = *cursor;
> +		if (cur && priced) {
> +			seek_pages = sector > cur ? sector - cur
> +						   : cur - sector;
> +			seek_pages >>= IOC_SECT_TO_PAGE_SHIFT;
> +			if (seek_pages > LCOEF_RANDIO_PAGES)
> +				base = randio;
> +		}
> +		if (priced && nbytes &&
> +		    !(model_flags & IOCOST_COST_F_MERGE))
> +			*cursor = sector + (nbytes >> 9);
> +	}
> +
> +	if (model_flags & IOCOST_COST_F_MERGE)
> +		base = 0;
> +
> +	cost = 2 * (base + pages * coef_page);
> +	return cost;
> +}

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

  reply	other threads:[~2026-09-14  8:04 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-14  7:33 [RFC PATCH v3 0/5] blk-iocost: BPF struct_ops cost model Tao Cui
2026-09-14  7:33 ` [RFC PATCH v3 1/5] blk-iocost: add BPF struct_ops cost model support Tao Cui
2026-09-14  7:49   ` sashiko-bot
2026-09-14 23:02   ` Amery Hung
2026-09-15  2:47     ` Tao Cui
2026-09-14  7:33 ` [RFC PATCH v3 2/5] selftests/bpf: add iocost cost model test Tao Cui
2026-09-14  8:04   ` sashiko-bot [this message]
2026-09-14  7:33 ` [RFC PATCH v3 3/5] blk-iocost: add iocost_ioc_tick tracepoint for per-period device summary Tao Cui
2026-09-14  8:11   ` sashiko-bot
2026-09-14  7:33 ` [RFC PATCH v3 4/5] selftests/bpf: add multi-stream sequentiality example model Tao Cui
2026-09-14  7:33 ` [RFC PATCH v3 5/5] docs: cgroup-v2: document io.cost model=<name> binding Tao Cui

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=20260914080404.5BF031F00893@smtp.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.