Skip to contentSkip to conversation

blk-iocost: BPF struct_ops cost model

RFC PATCH In review Tao Cui 3 participants 9 messages Nov 18

Subsystems (8)
lore
Share & tools
lore

Tao Cui

author
#0 lore raw

v14 cover blk-iocost: BPF struct_ops cost model

From: Tao Cui <[email protected]>

This series lets a BPF program take over the cost model of one device. The BPF model provides calc_cost() for charged IOs plus two transfer coefficients for the completion-time sizing, io.cost.model selects between the attached model and the builtin one, and enabling and disabling the controller stays with io.cost.qos.

The builtin model is a linear formula derived from a handful of device parameters, and the right formula is device specific. A BPF model can be adapted to the device and updated without rebuilding the kernel.

The series is based on next-20261117.

Changes since v13:
- p1: stop updating attached and attached_link under ioc->lock; rq_qos_mutex already orders all their accessors (review from tj)
- p1: name the calc_cost flags enum iocost_calc_flags and rename ioc_model()/model to ioc_bpf_model()/bpf_model so that the builtin model isn't called a NULL model (review from tj)
- p1: document why iocost holds its own model reference, the field locking and the merge flag semantics, and reword the commit message to describe guarantees instead of code paths (review from tj)
- p1: clear the autop stepping timestamps when a BPF model is attached; without this, the timestamps from before the attach survive until a model=linear switchback and can trigger an immediate profile step on that stale pre-attach evidence (report from sashiko)
- p1: align the BPF pricing scope with the builtin model:
dataless bios with no flush state and ops other than reads, writes and zone appends cost nothing, completion-time sizing prices reads, writes and zone appends, and a dead owning map no longer reports -EBUSY (report from sashiko)
- p3: probe the iocost_ioc_tick tracepoint, unwind the private mount namespace on setup failure, report fopen errors faithfully and fix an inverted posix_memalign() assertion that made the streams test skip its whole IO block (report from sashiko)
- p2/p4: document the iocost_ioc_tick fields in the trace header and extend the io.cost.model docs with the dev supply and the -ENODEV attach conditions (report from sashiko)

Tested on next-20261117 (qemu x86_64): the selftests pass, and the 2x example model doubles the charged cost over the builtin on a rotational device (2.00x measured), returning to 1.00x after "model=linear".

Tao Cui (4):
blk-iocost: add BPF struct_ops cost model support
blk-iocost: add iocost_ioc_tick tracepoint for per-period device
summary
selftests/bpf: add iocost cost model test
docs: cgroup-v2: document the iocost BPF cost model attachment

 Documentation/admin-guide/cgroup-v2.rst       |  55 +-
 MAINTAINERS                                   |   1 +
 block/Kconfig                                 |   4 +
 block/blk-iocost.c                            | 360 ++++++-
 include/trace/events/iocost.h                 |  57 ++
 tools/testing/selftests/bpf/config            |   2 +
 .../selftests/bpf/prog_tests/iocost_model.c   | 947 ++++++++++++++++++
 .../selftests/bpf/progs/iocost_model.c        | 188 ++++
 tools/testing/selftests/bpf/progs/iocost_ms.c | 195 ++++
 9 files changed, 1797 insertions(+), 12 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/iocost_model.c
 create mode 100644 tools/testing/selftests/bpf/progs/iocost_model.c
 create mode 100644 tools/testing/selftests/bpf/progs/iocost_ms.c
author
#1 lore raw

RFC PATCH v14 1/4 blk-iocost: add BPF struct_ops cost model support

From: Tao Cui <[email protected]>

iocost prices IO with a linear model derived from a handful of device parameters. The real cost of an IO depends on device internals the formula cannot capture and the right formula is device specific. This lets a BPF program take over pricing for a device so that the model can be adapted to it without rebuilding the kernel.

Attaching an iocost_model_ops struct_ops binds the model to one
device: each device has at most one attached model, and attaching switches the device's pricing to it while io.cost.qos keeps controlling whether the device is throttled at all. The model remains the pricing authority across io.cost.model
writes: "model=linear" and "model=bpf" only select between the attached model and the builtin one. The model steps down when the struct_ops link is deleted or the device is removed, at which point the device reverts to the builtin model. Attaching does not enable the controller.

Attaching also guarantees that the model cannot go away while it is pricing: iocost holds its own reference on the model, so neither deleting the link nor removing the device in the middle of a removal race can free a model whose prices are still being applied, and the model is released only after in-flight calc_cost() calls have finished. Models without calc_cost or with zero or out-of-range transfer coefficients are rejected when attached, and the transfer coefficients drive the completion-time request sizing, so latency tracking and vrate adjustment follow the model's pricing.

The kernel keeps no per-cgroup state for the model; models which need per-cgroup data keep it in their own maps, created and freed with the cgroups. Without CONFIG_BLK_CGROUP_IOCOST_BPF the struct_ops is not built and the builtin model is the only choice.

---
 block/Kconfig      |   4 +
 block/blk-iocost.c | 342 ++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 342 insertions(+), 4 deletions(-)
block/Kconfig +4 -0
index 70e4a66d941f..26badf54a477 100644
@@ -161,6 +161,10 @@ config BLK_CGROUP_IOCOST
 	distributes IO capacity between different groups based on
 	their share of the overall weight distribution.
 
+config BLK_CGROUP_IOCOST_BPF
+	def_bool y
+	depends on BLK_CGROUP_IOCOST && BPF_SYSCALL && BPF_JIT && DEBUG_INFO_BTF
+
 config BLK_CGROUP_IOPRIO
 	bool "Cgroup I/O controller for assigning an I/O priority class"
 	depends on BLK_CGROUP
block/blk-iocost.c +338 -4

442 lines not on this page · raw message

Signature · 1 line
2.53.0
author
#2 lore raw

RFC PATCH v14 2/4 blk-iocost: add iocost_ioc_tick tracepoint for per-period device summary

In reply to Tao Cui

From: Tao Cui <[email protected]>

Add iocost_ioc_tick, a tracepoint emitted once per period with a summary of the controller's overall state.

Observing this state today requires drgn scripts to dig into ioc internals. Exposing the per-period summary through a tracepoint lets the common monitoring paths use standard tools (perf, bpftrace) instead, and gives them an interface that does not shift with the internal data layout. The emission points and the exact event fields are documented in include/trace/events/iocost.h.

---
 MAINTAINERS                   |  1 +
 block/blk-iocost.c            | 18 +++++++++++
 include/trace/events/iocost.h | 57 +++++++++++++++++++++++++++++++++++
 3 files changed, 76 insertions(+)
MAINTAINERS +1 -0
index 7d4f4312a2c2..cb9837e3bda7 100644
@@ -6910,6 +6910,7 @@ F:	block/blk-iocost.c
 F:	block/blk-iolatency.c
 F:	block/blk-throttle.c
 F:	include/linux/blk-cgroup.h
+F:	include/trace/events/iocost.h
 
 CONTROL GROUP - CPUSET
 M:	Waiman Long <[email protected]>
block/blk-iocost.c +18 -0
index 8398a406f7d3..607f886803b9 100644
@@ -2378,6 +2378,7 @@ static void ioc_timer_fn(struct timer_list *timer)
 	struct ioc_now now;
 	LIST_HEAD(surpluses);
 	int nr_debtors, nr_shortages = 0, nr_lagging = 0;
+	int nr_active = 0;
 	u64 usage_us_sum = 0;
 	u32 ppm_rthr;
 	u32 ppm_wthr;
@@ -2575,6 +2576,23 @@ static void ioc_timer_fn(struct timer_list *timer)
 
 	ioc->busy_level = clamp(ioc->busy_level, -1000, 1000);
 
+	/*
+	 * busy_level, cur_period, nr_active and usage_us_sum are complete
+	 * here. usage is wall time over wall time, summed over iocgs, and
+	 * is not capped at 100. vrate and period_us reflect the current
+	 * controller state, which an io.cost.qos write may have changed
+	 * mid-period. Emit before the refresh and the period transition.
+	 */
+	if (trace_iocost_ioc_tick_enabled()) {
+		struct ioc_gq *active_iocg;
+
+		list_for_each_entry(active_iocg, &ioc->active_iocgs,
+				    active_list)
+			nr_active++;
+
+		trace_iocost_ioc_tick(ioc, &now, nr_active, usage_us_sum);
+	}
+
 	ioc_adjust_base_vrate(ioc, rq_wait_pct, nr_lagging, nr_shortages,
 			      prev_busy_level, missed_ppm);
 
include/trace/events/iocost.h +57 -0

63 lines not on this page · raw message

Signature · 1 line
2.53.0
author
#4 lore raw

RFC PATCH v14 3/4 selftests/bpf: add iocost cost model test

In reply to Tao Cui

From: Tao Cui <[email protected]>

Add two example iocost cost models and tests which attach them to a null_blk instance the tests create through configfs, so everything runs without external setup.

The first model implements the builtin linear HDD formula at double cost. The second implements multi-stream sequentiality detection: it keeps a per-cgroup table of stream slots instead of the single builtin cursor, so concurrent sequential streams in one cgroup are priced sequentially. Both set the target device through the struct_ops member before load, and the maps are declared ".struct_ops.link" so that a test dying in between does not leave the model attached.

The tests verify the model=bpf readback while attached, that ctrl=bpf is rejected, that a second model on the same device fails with -EBUSY, that model=linear and model=bpf switch between the attached and builtin models without detaching, and that detaching restores the builtin model. With the controller enabled through io.cost.qos, they also verify that IOs are priced through calc_cost() and that the iocost_ioc_tick tracepoint is seen from BPF. They also verify that device removal under the attached model ejects it, with a recreated device attachable again.

---
 tools/testing/selftests/bpf/config            |   2 +
 .../selftests/bpf/prog_tests/iocost_model.c   | 947 ++++++++++++++++++
 .../selftests/bpf/progs/iocost_model.c        | 188 ++++
 tools/testing/selftests/bpf/progs/iocost_ms.c | 195 ++++
 4 files changed, 1332 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/iocost_model.c
 create mode 100644 tools/testing/selftests/bpf/progs/iocost_model.c
 create mode 100644 tools/testing/selftests/bpf/progs/iocost_ms.c
tools/testing/selftests/bpf/config +2 -0
index 30b744844ef5..4d84ec6904d0 100644
@@ -142,3 +142,5 @@ CONFIG_SMC_HS_CTRL_BPF=y
 CONFIG_DIBS=y
 CONFIG_DIBS_LO=y
 CONFIG_PM_WAKELOCKS=y
+CONFIG_BLK_CGROUP_IOCOST=y
+CONFIG_BLK_DEV_NULL_BLK=y
tools/testing/selftests/bpf/prog_tests/iocost_model.c new file +947 -0

947 lines not on this page · raw message

tools/testing/selftests/bpf/progs/iocost_model.c new file +188 -0

188 lines not on this page · raw message

tools/testing/selftests/bpf/progs/iocost_ms.c new file +195 -0

195 lines not on this page · raw message

Signature · 1 line
2.53.0
author
#7 lore raw

RFC PATCH v14 4/4 docs: cgroup-v2: document the iocost BPF cost model attachment

In reply to Tao Cui

From: Tao Cui <[email protected]>

Document the BPF cost model attachment in the io.cost.model section of the cgroup v2 documentation: attaching an iocost_model_ops struct_ops to a device by its major:minor, the model=bpf readback while attached (ctrl keeps describing the coefficients), the builtin model coming back on detach, and ctrl writes never selecting a model.

---
 Documentation/admin-guide/cgroup-v2.rst | 55 +++++++++++++++++++++----
 1 file changed, 47 insertions(+), 8 deletions(-)
Documentation/admin-guide/cgroup-v2.rst +47 -8
index 9e3c67b02ae2..a3c402f03ae9 100644
@@ -2056,8 +2056,9 @@ IO Interface Files
 	currently implements "io.weight" proportional control.  Lines
 	are keyed by $MAJ:$MIN device numbers and not ordered.  The
 	line for a given device is populated on the first write for
-	the device on "io.cost.qos" or "io.cost.model".  The following
-	nested keys are defined.
+	the device on "io.cost.qos" or "io.cost.model", or when a BPF
+	cost model is attached to it.  The following nested keys are
+	defined.
 
 	  ======	=====================================
 	  enable	Weight-based control enable
@@ -2111,13 +2112,50 @@ IO Interface Files
 	implements "io.weight" proportional control.  Lines are keyed
 	by $MAJ:$MIN device numbers and not ordered.  The line for a
 	given device is populated on the first write for the device on
-	"io.cost.qos" or "io.cost.model".  The following nested keys
-	are defined.
+	"io.cost.qos" or "io.cost.model", or when a BPF cost model is
+	attached to it.  The following nested keys are defined.
 
-	  =====		================================
+	  =====		==========================================
 	  ctrl		"auto" or "user"
-	  model		The cost model in use - "linear"
-	  =====		================================
+	  model		The cost model in use - "linear" or "bpf"
+	  =====		==========================================
+
+	When BPF cost model support is available, a BPF cost model
+	can be bound to a device by attaching an
+	"iocost_model_ops" struct_ops carrying the whole disk's
+	major:minor as encoded by makedev(3) in its "dev" member (a
+	partition's major:minor is rejected).  The loader supplies
+	"dev" when the map is created and it cannot be changed
+	afterward.  Attaching creates the controller if needed, like an
+	io.cost.model write does, switches the device's pricing to the
+	model and populates the device's line in this file and in
+	"io.cost.qos" if it did not exist yet.  Attaching fails with
+	-EBUSY if the device already has a model attached, with
+	-EINVAL if the model fails its attach-time validation, with
+	-EOPNOTSUPP if dev names a partition or a device without a
+	multiqueue queue, and with -ENODEV if dev does not name an
+	existing device or if the device has gone away.  Updating the
+	link to a different model is not supported and fails with
+	-EINVAL or -EOPNOTSUPP; switching models requires detaching
+	first.  The
+	attached model prices IOs only while the controller is enabled
+	through "io.cost.qos"; attaching does not enable it.  IOs issued
+	directly by the root cgroup bypass the model's per-IO pricing;
+	the completion-time sizing of their requests still uses the
+	attached model's transfer coefficients.
+
+	While a model is attached, "model" selects between it and the
+	builtin model: "model=bpf" switches to the attached model and
+	"model=linear" switches back to the builtin model, and neither
+	detaches the struct_ops.
+
+	Detaching removes the model, after which "model=bpf"
+	fails and the device prices with the builtin model again.
+	Device removal ejects the attached model as well, while the
+	struct_ops link remains until userspace destroys it.  "ctrl"
+	accepts only "auto" and "user" and never selects a model.  It
+	keeps describing the builtin coefficients, which take effect
+	again when switched back.
 
 	When "ctrl" is "auto", the kernel may change all parameters
 	dynamically.  When "ctrl" is set to "user" or any other
@@ -2125,7 +2163,8 @@ IO Interface Files
 	automatic changes are disabled.
 
 	When "model" is "linear", the following model parameters are
-	defined.
+	defined.  They remain readable and writable while "model" is
+	"bpf".
 
 	  =============	========================================
 	  [r|w]bps	The maximum sequential IO throughput
Signature · 1 line
2.53.0
author
#8 lore raw
In reply to Tao Cui

Hello, Alexei,

Quoted 11 lines · Tao Cui

This series lets a BPF program take over the cost model of one device. The BPF model provides calc_cost() for charged IOs plus two transfer coefficients for the completion-time sizing, io.cost.model selects between the attached model and the builtin one, and enabling and disabling the controller stays with io.cost.qos.

The builtin model is a linear formula derived from a handful of device parameters, and the right formula is device specific. A BPF model can be adapted to the device and updated without rebuilding the kernel.

The series is based on next-20261117.

p1 changed quite a bit since the version you acked, mostly from tj's review and Sashiko's testing reports, so here's a quick summary.

The flag enum and the model accessors are renamed. The merge flag is now enum iocost_calc_flags with IOCOST_CALC_MERGE, calc_cost() takes a calc_flags parameter, and ioc_model()/model become ioc_bpf_model()/bpf_model so the builtin model is no longer represented as a NULL model. The named enum also makes the flags discoverable through BTF.

calc_cost() now has the same pricing scope as the builtin model:
bios with no data and no flush state and ops other than reads, writes and zone appends cost nothing, flushes are passed through so the model can price them from the bio flags, and completion-time sizing now follows the model's transfer coefficients.

On attach iocost holds its own reference on the struct_ops map, so a model cannot go away while it is pricing. A dead owning map reports -ENODEV instead of -EBUSY now, and the autop stepping timestamps are cleared at attach so a stale pre-attach profile cannot fire a step at the model=linear switchback.

The attached and attached_link updates also moved out of ioc->lock: rq_qos_mutex already orders all their accessors, and ioc->lock is only needed for publishing bpf_model to the timer and show paths.

I'm still carrying your Acked-by. Since p1 changed quite a bit, I'm happy to drop it until you've had a chance to take another look if you'd prefer.

Thanks,
Tao

#5 lore raw

Re: [RFC PATCH v14 3/4] selftests/bpf: add iocost cost model test

In reply to Tao Cui
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] selftests/bpf: iocost_test_env() hardcodes /sys/fs/cgroup without mounting - [Low] selftests/bpf…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] selftests/bpf: iocost_test_env() hardcodes /sys/fs/cgroup without mounting
- [Low] selftests/bpf: open_nullb_dev() callers check -errno instead of fd
--

commit 7c8a11566574f6e4224eb8a8a97b22da0b3eb991
Author: Tao Cui <[email protected]>

selftests/bpf: add iocost cost model test

This commit adds two BPF struct_ops iocost cost models and selftests that attach them to a configfs-created null_blk device and verify model selection, IO pricing, tracepoint firing, and device removal.

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/iocost_model.c new file +0 -0
new file mode 100644
index 0000000000000..0fbaeb7c5b7ef

[ ... ]

Quoted 24 lines
tools/testing/selftests/bpf/prog_tests/iocost_model.c new file +24 -0
+/* open the nullb device node, creating it manually when udev hasn't */
+static int open_nullb_dev(const char *name, int flags)
+{
+	char path[32], syspath[64], devtok[32];
+	unsigned int maj, min;
+	dev_t dev;
+	struct stat st;
+	int fd;
+
+	snprintf(syspath, sizeof(syspath), "/sys/block/%s/dev", name);
+	if (read_file_token(syspath, devtok, sizeof(devtok)) ||
+	    sscanf(devtok, "%u:%u", &maj, &min) != 2)
+		return -1;
+	dev = makedev(maj, min);
+
+	snprintf(path, sizeof(path), "/dev/%s", name);
+	fd = open(path, flags);
+	if (fd >= 0 && fstat(fd, &st) == 0 && S_ISBLK(st.st_mode) &&
+	    st.st_rdev == dev)
+		return fd;
+	if (fd >= 0) {
+		/* a stale node from a previous run, drop and recreate */
+		close(fd);
+		fd = -1;

tools/testing/selftests/bpf/prog_tests/iocost_model.c line 235

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/iocost_model.c new file +10 -0
+	}
+	if (unlink(path) && errno != ENOENT)
+		return -1;
+	if (mknod(path, S_IFBLK | 0600, dev))
+		return -1;
+	fd = open(path, flags);
+	if (fd < 0)
+		unlink(path);
+	return fd;
+}

[Severity: Low]
Can ASSERT_OK(devfd < 0 ? -errno : 0, ...) in serial_test_iocost_model() and serial_test_iocost_model_streams() pass when open_nullb_dev() fails?

If read_file_token() hits EOF without setting errno, sscanf() fails to match two tokens, or unlink(path) succeeds and leaves errno at 0 after the second open() fails, open_nullb_dev() returns -1 with errno == 0. The callers then pass 0 to ASSERT_OK() and proceed to call posix_fadvise(), read(), pread(), and close() on fd -1. Using ASSERT_OK_FD(devfd, ...) in the callers (or returning a negative error code from open_nullb_dev()) would catch any failure directly.

[ ... ]

Quoted 20 lines
tools/testing/selftests/bpf/prog_tests/iocost_model.c new file +20 -0
+static bool iocost_test_env(void)
+{
+	static bool ready;
+	struct statfs sf;
+	int orig_ns = -1, supported;
+	bool ok = false;
+
+	if (geteuid() != 0) {
+		printf("%s:SKIP:needs root\n", __func__);
+		test__skip();
+		return false;
+	}
+
+	if (statfs("/sys/fs/cgroup", &sf) ||
+	    sf.f_type != CGROUP2_SUPER_MAGIC) {
+		printf("%s:SKIP:needs cgroup v2 at /sys/fs/cgroup\n",
+		       __func__);
+		test__skip();
+		return false;
+	}

[ ... ]

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/iocost_model.c new file +5 -0
+	/*
+	 * Isolate the configfs and cgroup mounts from the host. A
+	 * failed setup unwinds the namespace so that a later retry does
+	 * not stack private namespaces.
+	 */

tools/testing/selftests/bpf/prog_tests/iocost_model.c line 353

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/iocost_model.c new file +10 -0
+	orig_ns = open("/proc/self/ns/mnt", O_RDONLY);
+	if (unshare(CLONE_NEWNS) ||
+	    mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
+		printf("%s:SKIP:cannot make mounts private: %s\n",
+		       __func__, strerror(errno));
+		test__skip();
+		goto out;
+	}
+	mkdir("/sys/kernel/config", 0755);
+	(void)mount("none", "/sys/kernel/config", "configfs", 0, NULL);

[Severity: Medium]
Will all three iocost tests skip when /sys/fs/cgroup is not already a cgroup2 mount or after an earlier cgroup v1 test runs in the same test_progs process?

iocost_test_env(), cgroupfs_has_controller(), readback_model(), write_cost_model(), and the io.cost.qos writes all access /sys/fs/cgroup directly without mounting cgroup2 there, while cgroup_setup_and_join() in tools/testing/selftests/bpf/cgroup_helpers.c mounts cgroup2 at /mnt (CGROUP_MOUNT_PATH). In addition, setup_classid_environment() in cgroup_helpers.c mounts tmpfs at /sys/fs/cgroup (CGROUP_MOUNT_DFLT) and cleanup_classid_environment() leaves that tmpfs mounted:

tools/testing/selftests/bpf/cgroup_helpers.c:setup_classid_environment() {
	...
	if (mount("tmpfs", CGROUP_MOUNT_DFLT, "tmpfs", 0, NULL) &&
	    errno != EBUSY) {
		log_err("mount cgroup base");
		return 1;
	}
	...
}

When a test using setup_classid_environment() runs before serial_test_iocost_model*, statfs("/sys/fs/cgroup", &sf) sees TMPFS_MAGIC instead of CGROUP2_SUPER_MAGIC and skips the tests. Using the cgroup2 mount managed by cgroup_helpers.c (via get_root_cgroup() or write_cgroup_file("", ...) / read_cgroup_file("", ...)), or mounting cgroup2 inside the private mount namespace created by iocost_test_env(), would avoid relying on the host's /sys/fs/cgroup mount state.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#3 lore raw

Re: [RFC PATCH v14 2/4] blk-iocost: add iocost_ioc_tick tracepoint for per-period device summary

In reply to Tao Cui
TL;DR: The new iocost_ioc_tick tracepoint comment describes vrate as relative to VTIME_PER_SEC, but it is likely vtime per usec (VTIME_PER_USEC is 100%), so users following it would get values 10^6…
Quoted 1 line

blk-iocost: add iocost_ioc_tick tracepoint for per-period device summary

TL;DR: The new iocost_ioc_tick tracepoint comment describes vrate as relative to VTIME_PER_SEC, but it is likely vtime per usec (VTIME_PER_USEC is 100%), so users following it would get values 10^6 too small.

Quoted 3 lines
include/trace/events/iocost.h +0 -0

[ ... ]

Quoted 6 lines

+/*
+ * Periodic per-device summary, emitted once per period before the vrate [ ... ]
+ * vrate is the current vrate in vtime units, where one IO time second
+ * is VTIME_PER_SEC (2^37) vtime and cost.vrate in io.stat reports the
+ * same value as a percentage of that base.

Is this description of vrate accurate? The field is filled from ioc->vtime_base_rate, which is vtime per wall-clock microsecond:

    __entry->vrate = ioc->vtime_base_rate;

and io.stat computes cost.vrate as a percentage of VTIME_PER_USEC, not VTIME_PER_SEC:

    unsigned vp10k = DIV64_U64_ROUND_CLOSEST(
            ioc->vtime_base_rate * 10000,
            VTIME_PER_USEC);

ioc_refresh_vrate() does the same with div64_u64(ioc->vtime_base_rate * 100, VTIME_PER_USEC).

Someone following the comment would compute vrate * 100 / 2^37 and get a value 10^6 too small, since the nominal 100% value is 137438 (2^37 / 10^6).

The commit message points readers to this header for the exact meaning of the fields. Could the comment say that vrate is in vtime per wall-clock usec, with VTIME_PER_USEC being 100% as reported by cost.vrate in io.stat?

[ ... ]

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44312765238

#6 lore raw

Re: [RFC PATCH v14 3/4] selftests/bpf: add iocost cost model test

In reply to Tao Cui
TL;DR: In the iocost selftest, the setns() unwind on the skip path may reset test_progs' cwd to "/" (and possibly leave a chroot), so later unrelated tests could fail with -ENOENT when null_blk is…
Quoted 1 line

selftests/bpf: add iocost cost model test

TL;DR: In the iocost selftest, the setns() unwind on the skip path may reset test_progs' cwd to "/" (and possibly leave a chroot), so later unrelated tests could fail with -ENOENT when null_blk is unavailable.

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/iocost_model.c new file +0 -0
new file mode 100644

[ ... ]

Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/iocost_model.c new file +2 -0
+static bool iocost_test_env(void)
+{

[ ... ]

Quoted 24 lines
tools/testing/selftests/bpf/prog_tests/iocost_model.c new file +24 -0
+	/*
+	 * Isolate the configfs and cgroup mounts from the host. A
+	 * failed setup unwinds the namespace so that a later retry does
+	 * not stack private namespaces.
+	 */
+	orig_ns = open("/proc/self/ns/mnt", O_RDONLY);
+	if (unshare(CLONE_NEWNS) ||
+	    mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
+		printf("%s:SKIP:cannot make mounts private: %s\n",
+		       __func__, strerror(errno));
+		test__skip();
+		goto out;
+	}
+	mkdir("/sys/kernel/config", 0755);
+	(void)mount("none", "/sys/kernel/config", "configfs", 0, NULL);
+	SYS_NOFAIL("modprobe null_blk nr_devices=0");
+	if (access("/sys/kernel/config/nullb", W_OK)) {
+		printf("%s:SKIP:needs null_blk with configfs support\n",
+		       __func__);
+		test__skip();
+		goto out;
+	}
+
+	ready = true;

tools/testing/selftests/bpf/prog_tests/iocost_model.c line 372

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/iocost_model.c new file +10 -0
+	ok = true;
+
+out:
+	if (orig_ns >= 0) {
+		if (!ok)
+			setns(orig_ns, CLONE_NEWNS);
+		close(orig_ns);
+	}
+	return ok;
+}

When a prerequisite check fails after unshare(CLONE_NEWNS), for example when access("/sys/kernel/config/nullb", W_OK) fails because null_blk is not built, not installed, or modprobe is missing, this goes back with setns(orig_ns, CLONE_NEWNS).

Can this change the cwd of the test_progs process? mntns_install() in fs/namespace.c ends with:

	/* Update the pwd and root */
	set_fs_pwd(fs, &root);
	set_fs_root(fs, &root);

unshare() keeps the cwd, so only this skip path moves the process to "/".

The serial tests run in the test_progs main process, so the new cwd persists for every test that runs afterwards. Many of those open objects by cwd-relative path, for example serial_test_kfree_skb() loading "./test_pkt_access.bpf.o", select_reuseport.c opening "test_select_reuseport_kern.bpf.o", and l4lb_all.c using "test_l4lb.bpf.o". The flavored runners also lose the chdir() done by cd_flavor_subdir().

So on a kernel with the iocost BPF support but no usable null_blk, would the iocost tests report SKIP and then unrelated tests fail with -ENOENT? If test_progs runs inside a chroot, does the root reset also leave the chroot?

The comment says the unwind exists "so that a later retry does not stack private namespaces", but serial_test_iocost_model() and serial_test_iocost_model_streams() call cgroup_setup_and_join(), and setup_cgroup_environment() calls unshare(CLONE_NEWNS) every time, so namespaces stack regardless.

Would it work to drop the setns() unwind and cache the failure in a static, or to save the cwd with open(".", O_RDONLY | O_DIRECTORY) before unshare() and fchdir() back to it after setns()?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44312765238