Linux block layer
 help / color / mirror / Atom feed
* Per-bio cost of BLK_CGROUP=y, measured after 7693e1350b06 ("memcg: make MEMCG select BLK_CGROUP")
@ 2026-12-03 13:02 Fengnan Chang
  2026-12-03 16:43 ` Tejun Heo
  2026-12-03 17:59 ` Jens Axboe
  0 siblings, 2 replies; 3+ messages in thread
From: Fengnan Chang @ 2026-12-03 13:02 UTC (permalink / raw)
  To: Christian Brauner, Tejun Heo
  Cc: Shakeel Butt, Jens Axboe, Christoph Hellwig, Josef Bacik,
	Johannes Weiner, Michal Hocko, Roman Gushchin, Michal Koutný,
	cgroups, linux-block

Hi,

Since v7.4-rc3, commit 7693e1350b06 ("memcg: make MEMCG select
BLK_CGROUP") forces CONFIG_BLK_CGROUP (and with it
CONFIG_CGROUP_WRITEBACK) on for every CONFIG_MEMCG=y build.

After it landed, our block/io_uring performance test kernels got 4-14%
slower on single-core io_uring 4K random reads: 3.8-7.4% of IOPS with
the task in the root cgroup and 7.1-13.8% in a non-root cgroup, on
cgroup v1 and v2 alike.  Where the core is the bottleneck that is
+65..+172 cycles and +208..+590 instructions per I/O.

To be clear about the setup: MEMCG=y with BLK_CGROUP=n is only the
configuration we use for performance testing of these fast paths, not
what we run in production.  Our production kernels, like
x86_64_defconfig and most distro configs, already have BLK_CGROUP=y, so
for them nothing changes and this is not a regression in any real
deployment we know of.  What the numbers do show is how much
BLK_CGROUP=y costs per bio, even with no blkcg policy built in (no
throttling, iocost, iolatency, ioprio or BFQ): the commit simply made
that cost visible in our test setup.

I'm not questioning the deadlock fix.  I'd like to check two things:

  - Is this per-bio cost of BLK_CGROUP=y expected and known?
  - Is making BLK_CGROUP mandatory for MEMCG meant to stay, or is there
    room to address the deadlock without forcing BLK_CGROUP on?

In case 7693e1350b06 has to stay, we are already working on reducing
the overhead itself (see the end).  Since that work targets the
BLK_CGROUP=y paths, it would help every BLK_CGROUP=y kernel, production
and distro ones included, not just our test configuration.


Setup
=====

Kernels, both v7.4-rc4 built from one config:
    A: v7.4-rc4                             BLK_CGROUP=y, CGROUP_WRITEBACK=y
    B: v7.4-rc4 + revert of 7693e1350b06    BLK_CGROUP=n
  The two .config files differ only in BLK_CGROUP, CGROUP_WRITEBACK and
  the unset BLK_CGROUP_* / BLK_DEV_THROTTLING sub-options.
  The config is our performance test config: derived from our x86_64
  production config, with BLK_CGROUP and the blkcg policies turned off
  (production has them on).  It is not defconfig.  Relevant options:
  PREEMPT_LAZY=y, PREEMPT_DYNAMIC=y, PREEMPT_RCU=y, HZ=1000,
  BLK_DEV_THROTTLING, BLK_CGROUP_{IOLATENCY,IOCOST,IOPRIO} and
  IOSCHED_BFQ all off.  The full .config is available on request.

Boot parameters:
    ro transparent_hugepage=madvise net.ifnames=1 nopti
    nvme_core.default_ps_max_latency_us=0 acpi=nocmcff quiet
    nvme.poll_queues=16
  plus, for cgroup v1: systemd.legacy_systemd_cgroup_controller=1
         for cgroup v2: systemd.unified_cgroup_hierarchy=1
  Kernels were switched with kexec.

Hardware:
  - CPU: Intel Xeon Platinum 8457C.  All runs use one core (CPU 30),
    performance governor, ~3.1 GHz under load.
  - Two PCIe NVMe SSDs on the same NUMA node as CPU 30, I/O scheduler
    "none".  SSD1 serves the raw block device and XFS; SSD2 serves ext4
    (noblock_validity,noatime).
  - The XFS file is 128 GiB in 18 extents; the ext4 file is 64 GiB.

Workload: fio's t/io_uring, one submitter thread pinned to CPU 30:
    taskset -c 30 t/io_uring -b4096 -s32 -c32 -F1 -B1 -R1 -X1 -n1 \
        -p1 -d512 <file|dev>                   (polled, QD512)
    taskset -c 30 t/io_uring ... -p0 -d128|-d512 <dev>   (IRQ)
  For the raw device runs -P0 is passed.  With -P1 t/io_uring moves
  itself to all CPUs of the device's NUMA node, overriding the taskset;
  thread affinity was checked during every raw run.  For files t/io_uring
  finds no NUMA node, so the file runs stay on CPU 30 either way.

cgroups:
  - cgroup v1: root = /sys/fs/cgroup/blkio,
               non-root = /sys/fs/cgroup/blkio/user.slice
  - cgroup v2: root = /sys/fs/cgroup,
               non-root = /sys/fs/cgroup/blkbench, with "+io" in the root's
               cgroup.subtree_control
  Kernel B has no blkio hierarchy on v1, so its "non-root" column there
  is just a repeat of root.  On v2 the non-root cgroup also has cpuset,
  cpu, memory and pids enabled.  Kernel B shows no root/non-root
  difference on v2, so those controllers don't skew the comparison.

Method:
  - Every data point is one 8 s run.  t/io_uring prints one IOPS value
    per second; the first two values are dropped and the next five are
    averaged.  The printed IOPS has 2-3 significant digits, about 0.4%
    resolution.
  - Each boot runs 3 rounds of every case, root and non-root back to
    back.  Raw device runs alternate which of the two goes first; the
    filesystem runs always do root first.
  - Raw device runs: boots A, B, A, B on both cgroup v1 and v2, so n=6
    per cell.
  - Filesystem runs: boots A, B, A, B on cgroup v1 (n=6), one boot per
    kernel on cgroup v2 (n=3).
  - cycles and instructions per I/O come from "perf stat -a -C 30" over
    4 s of a separate run, one sample per boot.  So n=2 everywhere except
    the v2 filesystem rows, where n=1.  When the core is saturated,
    cycles/IO is roughly frequency/IOPS; instructions/IO is the more
    independent measure.


Results: IOPS (millions, median [min-max])
==========================================

cgroup v1                       B: BLK_CGROUP=n    A: BLK_CGROUP=y     A vs B
raw, IRQ QD128      root        1.687 [1.68-1.69]  1.600 [1.60-1.60]   -5.2%
                    non-root    1.685 [1.68-1.69]  1.550 [1.54-1.55]   -8.0%
raw, IRQ QD512      root        1.690 [1.68-1.69]  1.610 [1.61-1.61]   -4.7%
                    non-root    1.690 [1.69-1.69]  1.560 [1.56-1.56]   -7.7%
raw, polled QD512   root        2.940 [2.88-2.94]  2.722 [2.70-2.73]   -7.4% (*)
                    non-root    2.940 [2.89-2.94]  2.565 [2.54-2.57]  -12.8% (*)
ext4, polled QD512  root        2.146 [2.14-2.16]  2.033 [2.02-2.04]   -5.3%
                    non-root    2.150 [2.14-2.15]  1.945 [1.91-1.95]   -9.5%
xfs, polled QD512   root        2.050 [2.05-2.05]  1.946 [1.93-1.95]   -5.1%
                    non-root    2.050 [2.05-2.05]  1.869 [1.87-1.87]   -8.8%

cgroup v2                       B: BLK_CGROUP=n    A: BLK_CGROUP=y     A vs B
raw, IRQ QD128      root        1.680 [1.68-1.69]  1.605 [1.60-1.61]   -4.5%
                    non-root    1.680 [1.68-1.69]  1.540 [1.54-1.55]   -8.3%
raw, IRQ QD512      root        1.690 [1.69-1.69]  1.610 [1.61-1.61]   -4.7%
                    non-root    1.690 [1.69-1.69]  1.550 [1.55-1.56]   -8.3%
raw, polled QD512   root        2.910 [2.88-2.94]  2.726 [2.69-2.74]   -6.3% (*)
                    non-root    2.940 [2.88-2.94]  2.534 [2.50-2.55]  -13.8% (*)
ext4, polled QD512  root        2.120 [2.10-2.17]  2.040 [2.04-2.04]   -3.8% (+)
                    non-root    2.120 [2.06-2.18]  1.970 [1.97-1.97]   -7.1% (+)
xfs, polled QD512   root        2.040 [2.04-2.04]  1.960 [1.96-1.96]   -3.9%
                    non-root    2.040 [2.04-2.04]  1.870 [1.87-1.87]   -8.3%

In every cell the best A run is below the worst B run.

The IRQ runs are core-bound with both kernels: B sits at ~1.69M at both
QD128 and QD512, at ~1835 cycles/IO, which is the full ~3.1 GHz core.
They are the cleanest comparison.

(*) Polled raw reads with BLK_CGROUP=n reach ~2.94M, close to what the
    device delivers, with the core partly spinning in the poll loop.  The
    IOPS loss therefore understates the CPU cost.
(+) v2 filesystem rows are from a single boot per kernel.  The ext4 B
    runs were noisy (2.06-2.18M, falling from round to round), so treat
    the v2 ext4 numbers as approximate.


Results: cost per I/O
=====================

                                 cycles/IO            instructions/IO
                                 B      A    delta    B      A    delta
v1  raw IRQ QD128    root        1839   1934    +95   4336   4552   +216
                     non-root    1842   2002   +160   4342   4685   +343
    raw IRQ QD512    root        1831   1922    +91   4322   4540   +218
                     non-root    1835   1984   +149   4330   4676   +346
    raw polled QD512 root        1062   1139    +77   3953   4332   +379 (*)
                     non-root    1063   1210   +147   3957   4488   +531 (*)
    ext4 polled      root        1442   1536    +94   4756   4964   +208
                     non-root    1442   1614   +172   4750   5130   +380
    xfs polled       root        1516   1591    +75   5140   5350   +210
                     non-root    1517   1656   +139   5142   5505   +363
v2  raw IRQ QD128    root        1842   1928    +86   4336   4550   +214
                     non-root    1842   2010   +168   4336   4740   +404
    raw IRQ QD512    root        1831   1922    +91   4320   4552   +232
                     non-root    1832   1992   +160   4322   4718   +396
    raw polled QD512 root        1071   1136    +65   3954   4340   +386 (*)
                     non-root    1074   1217   +143   3958   4548   +590 (*)
    xfs polled       root        1519   1585    +66   5137   5365   +228
                     non-root    1517   1655   +138   5134   5550   +416

(*) These include instructions spent spinning in the poll loop, which B
    does more of, so the deltas are lower bounds.
The v2 ext4 rows are left out because of the noisy B runs (see above).


Where the time goes
===================

perf record -F 4999 on CPU 30, kernel A, cgroup v2, XFS polled QD512,
self time.  No matching profile of B was taken, so the second group
mixes blkcg cost with work that also happens with BLK_CGROUP=n.

Symbols that only exist with BLK_CGROUP=y:
                                    root cgroup    non-root cgroup
  bio_associate_blkg                   0.93%           1.27%
  bio_associate_blkg_from_css          1.01%           0.91%
  blk_cgroup_bio_start                 0.35%           0.70%
  __radix_tree_lookup                    -             0.53%
  radix_tree_lookup                      -             0.13%
  __css_rstat_updated                    -             0.24%
  kthread_blkcg                        0.19%           0.14%

Symbols that blkcg adds work to but that also run with BLK_CGROUP=n:
  __rcu_read_lock                      0.76%           1.37%
  __rcu_read_unlock                    0.70%           1.24%
  blk_rq_merge_ok                      1.18%           0.78%
  bio_endio (includes blkg_put)        0.68%           0.60%
  bio_uninit                           0.15%           0.14%

Reading the v7.4-rc4 code, the extra per-bio work is:

1. RCU read sections.  With PREEMPT_RCU, rcu_read_lock() and
   rcu_read_unlock() are out-of-line calls.  bio_associate_blkg() enters
   one RCU section for the css lookup and blkg_tryget_closest() another
   for the blkg lookup.  blkg_get()/blkg_tryget() and the blkg_put() in
   bio_endio() each enter one more inside percpu_ref.

2. References per bio.  A non-root cgroup takes and drops a css
   reference with css_tryget_online()/css_put() around the lookup; for
   the root css these are no-ops.  Every bio also takes a percpu_ref on
   its blkg and drops it again at completion.

3. The radix tree lookup on every bio in a non-root cgroup.  blkg_hint is
   only written in blkg_lookup_create(), when that function finds an
   existing blkg.  It is not set when a blkg is created, and
   blkg_lookup() never writes it.  After the first bio has created the
   blkg, every later bio succeeds through blkg_lookup()'s radix tree
   fallback and never comes back to refresh the hint.  In practice the
   hint of a non-root cgroup stays NULL or stale and every bio walks the
   radix tree.

4. Merge checks.  blk_attempt_bio_merge() calls blk_rq_merge_ok() before
   blk_try_merge().  blk_rq_merge_ok() reads the request's first bio
   (bi_write_hint, bi_write_stream, bi_ioprio) with or without blkcg;
   BLK_CGROUP=y adds the bi_blkg comparison in blk_cgroup_mergeable().
   Random I/O almost never finds an adjacent request, so for it all of
   this is wasted.  Checking adjacency first would help BLK_CGROUP=n as
   well.

5. blk_cgroup_bio_start() dereferences bio->bi_blkg and computes the I/O
   type before it checks whether the io controller is on the default
   hierarchy.  On cgroup v1 the function then does nothing.  On cgroup v2
   in a non-root cgroup it also does the per-cpu io.stat update.


Reducing the cost
=================

We have been working on reducing this per-bio overhead without
reverting the commit.  Removing most of it takes a series of roughly
8-9 patches across blk-cgroup, percpu-refcount and the block merge
path, and it is still being tested.  The overhead it removes is in the
BLK_CGROUP=y paths, so it also benefits kernels that have always run
with BLK_CGROUP=y.  We will post it once testing is done, and would
welcome comments on whether that direction makes sense if a revert is
not an option.

Thanks,
Fengnan

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-03 17:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 13:02 Per-bio cost of BLK_CGROUP=y, measured after 7693e1350b06 ("memcg: make MEMCG select BLK_CGROUP") Fengnan Chang
2026-12-03 16:43 ` Tejun Heo
2026-12-03 17:59 ` Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox