In the read times measured by an application, the median stays where it usually is, but only the upper percentiles such as p99 (the value at the 99% position when the times are sorted from fastest to slowest) jump to around 100ms.

iostat -x for the same period shows an r_await (the average wait per read) far below the values that jumped, and a low %util (the fraction of time the disk was processing something).

If you see this combination, suspect a cgroup read limit. A cgroup is the kernel mechanism that groups processes and places limits on their CPU, memory, and I/O. Each systemd service and each Docker container is one cgroup.

I reproduced this combination by placing a limit on a virtual disk, and verified it.

What is happening

How the limit is written

Each cgroup has a file named io.max, where per-disk I/O limits are written.1 A limit on the number of reads is a single line like this:

<device number> riops=1000 

riops=1000 limits reads from that disk to 1000 per second. It is the read part of IOPS (I/O operations per second).

When you set IOReadIOPSMax= on a systemd service, or start a Docker container with --device-read-iops, systemd or Docker writes a line to this file.

Where reads are held

A read issued by a process enters the disk's queue (the list of reads and writes lined up to be handed to the disk), and the disk processes it and returns it. The kernel checks the limit before adding the read to the queue.2

If the limit has been exceeded, the kernel holds the read inside the kernel instead of adding it to the queue, and adds it once the limit has room again. The part of the kernel responsible for this is called blk-throttle.

r_await in iostat -x counts the time from when a read enters the queue until it returns. The time a read spends held comes before that, so it is not included in r_await.3 A held read has not reached the disk, so %util does not go up either.

Part of the read Time measured by the application r_await
While held by the limit Included Not included
From entering the queue until it returns Included Included

How reads are counted

The kernel does not enforce "1000 per second" by letting up to 1000 reads through each second. Instead, it divides time into shorter windows and lets through as many reads as each window's length allows. The kernel source calls this window a slice. With a 100ms window, the number of reads allowed per window is one tenth of 1000, which is 100.

Once a window's allowance is used up, every read issued before the window ends is held and waits until the next window starts. The window length cannot be changed by configuration; the kernel decides it. In current kernels it is 100ms regardless of the disk.4

Which reads are held

The following traces one window with small numbers. Suppose 10 reads are allowed per window (a limit of 100 per second), and two threads, A and B, read without pause.

Each thread issues one read and issues the next only after that one returns, so each thread always has exactly one read in flight. The disk is fast and returns each read in about 0.05ms.

When the window starts, A and B each read once every 0.05ms. After about 0.25ms, A has passed 5 reads and B has passed 5, 10 in total, which uses up the window's allowance. The 6th read of A and the 6th read of B, which they had issued at that point, are held.

The diagram below shows the first 0.3ms of the window. Each character is 0.025ms. A number shows the order in which that read was let through within the window, and . is time spent held.

ms 0.0 0.1 0.2 0.3 | | | | A 1 3 5 7 9 .. B 2 4 6 8 10.. 

Neither A nor B can issue its next read until the held read returns, so neither makes progress for about 100ms, until the window ends. When the next window starts, the two held reads go through. These two reads waited about 100ms. The next 8 reads go through immediately, and again two are held.

The same flow over 200ms is shown below. Each character is 5ms. # is time spent passing reads (about 0.25ms, drawn as one character), and . is time spent held.

ms 0 20 40 60 80 100 120 140 160 180 200 | | | A #...................#...................# B #...................#...................# 

With 16 threads and a limit of 1000 per second (100 reads allowed per window), the shape is the same. In each window 100 reads go through; 16 of them wait about 100ms and 84 return immediately.

Sorted from fastest to slowest, the first 84% of read times are about 0.05ms and the last 16% are about 100ms. The median (the 50% position) falls within the fast 84%, so it does not change. p90 and p99 fall within the slow 16%, so they are about 100ms.

When reads run without pause at the limit, the fraction of reads held equals the number of concurrent reads (the number of threads, or the number of reads in flight with asynchronous I/O) divided by the number of reads allowed per window. A held read waits for the rest of the window, which is nearly one full window. When the number of concurrent reads exceeds the number allowed per window, nearly every read is held.

Reads issued in batches

Reads can be held even when the average number of reads per second is below the limit. This happens when reads are issued in batches with gaps in between, and each batch is larger than the per-window allowance. With a limit of 1000 per second, issuing 120 reads at a time lets 100 of them through immediately, and the remaining 20 wait about 100ms for the next window (after a gap, the kernel starts counting again from a new window5).

Measurements

I read a virtual disk with fio and ran each condition for 10 seconds. fio is a tool that issues reads and writes to a disk and measures the time of each one; here it issued only reads. The time fio measured corresponds to the read time an application measures, and I compared it with iostat -x for the same period.6 Where a limit was applied, it was 1000 per second.

Held time and r_await

Time spent held should be included in the time fio measures but not in r_await. To check this, I had 16 threads each read one at a time without pause, and compared runs with and without the limit. If the limit makes only fio's time grow, and r_await does not grow by the same amount, then the held time is not included in r_await.

p50 measured by fio p99 measured by fio iostat r_await Reads held Expected
No limit 0.072ms 0.232ms 0.050ms 0% 0%
With limit 0.111ms 103.285ms 0.082ms 15.9% 16.0%

With the limit, fio's p99 jumped from 0.232ms to 103ms. r_await went from 0.050ms to 0.082ms and stayed under 0.1ms. p50 stayed around 0.1ms, and the fraction of reads held matched the value expected from the mechanism (16 out of 100).

Batched reads and r/s

r/s in iostat is the number of reads the disk completed per second. When reads run without pause and hit the limit, r/s levels off at the limit of 1000.

If the mechanism works as described, though, batched reads are held even when r/s is below the limit. I kept the limit at 1000 per second and changed only how the reads were issued.

How reads are issued iostat r/s Reads held Expected
One at a time, without pause 1000.0 (equal to the limit) 15.9% 16.0%
120 at a time, with 250ms gaps 335.6 (below the limit) 16.7% 16.7%

With batched reads, r/s was below the limit, yet the fraction of reads held matched the expected value (20 out of 120).

Checking your own environment

If your system shows p99 alone jumping to around 100ms while r_await and %util stay low, check the following in this order: whether a limit is in place, where it was set, and whether any reads are being held. On LVM, the iostat row to look at is different; this is covered in the note at the end of the article.

Whether a limit is in place, and where it was set

Find the cgroup from the PID of the process issuing the reads, and read that cgroup's io.max. If there is a line with a number after riops=, a limit is in place. If there is no such line, the number of reads is not limited.7

When fio runs as a systemd service with the limit set by IOReadIOPSMax=/dev/loop0 1000, the output looks like this. 7:0 is the device number (the <major>:<minor> number the kernel assigns to each disk) of the loop device that the limit was applied to.

$ cat /proc/<PID>/cgroup 0::/system.slice/iomax-p5_systemd.service $ cat /sys/fs/cgroup/system.slice/iomax-p5_systemd.service/io.max 7:0 rbps=max wbps=max riops=1000 wiops=max 

The part after 0:: is the cgroup path, and io.max is in that directory under /sys/fs/cgroup.8

The end of the path tells you where the limit was set. A path ending in .service is a systemd service. A Docker container is docker-<container ID>.scope when Docker is configured to create cgroups through systemd. The limit can be read from the systemd and Docker settings.

$ systemctl show -p IOReadIOPSMax iomax-p5_systemd.service IOReadIOPSMax=/dev/loop0 1000 $ docker inspect -f '{{json .HostConfig.BlkioDeviceReadIOps}}' iomax-p6_docker [{"Path":"/dev/loop0","Rate":1000}] 

The docker inspect output is for the container iomax-p6_docker, which was started with --device-read-iops /dev/loop0:1000.9

Counting held reads

A limit in io.max is not necessarily the cause of the slowness. If reads do not exceed the per-window allowance, the kernel does not hold a single read. The number of held reads appears neither in the cgroup files nor in iostat,10 so count them with kernel tracing.

Use the blk tracer. It is a feature of tracefs (the mechanism for controlling kernel tracing through files, at /sys/kernel/tracing), and it writes a line containing throtl [R] bio. each time the limit holds a read.11 While reads are slow, start recording as root, and then immediately read the cgroup's io.stat.12

$ echo blk > /sys/kernel/tracing/current_tracer $ echo queue,complete,notify > /sys/block/loop0/trace/act_mask $ echo 1 > /sys/block/loop0/trace/enable $ echo 1 > /sys/kernel/tracing/tracing_on $ cat /sys/kernel/tracing/trace_pipe > ftrace.txt & $ grep '^7:0 ' /sys/fs/cgroup/iomax-test/io.stat 7:0 rbytes=89423872 wbytes=0 rios=21832 wios=0 dbytes=0 dios=0 

In your own environment, replace loop0 with the name of the disk the limit applies to, 7:0 with the number at the start of the io.max line, and /sys/fs/cgroup/iomax-test (the cgroup whose io.max the limit was written to directly in my test) with the cgroup directory you found from /proc/<PID>/cgroup. lsblk -o NAME,KNAME,MAJ:MIN lists disk names next to their numbers. The KNAME column is the name shown in the iostat rows.

Wait a while (6 seconds in my test), read io.stat again, then stop recording and count the lines.

$ grep '^7:0 ' /sys/fs/cgroup/iomax-test/io.stat 7:0 rbytes=113999872 wbytes=0 rios=27832 wios=0 dbytes=0 dios=0 $ echo 0 > /sys/block/loop0/trace/enable $ echo nop > /sys/kernel/tracing/current_tracer $ grep -c 'throtl \[R\] bio\.' ftrace.txt 960 

rios= is the number of reads this cgroup has made from that disk. Each line corresponds to one held read,13 so the fraction of reads held is the line count divided by the difference between the two rios values: 960 / 6000 = 16.0%. If reads are running without pause, multiplying this fraction by the per-window allowance (one tenth of riops=, since the window is 100ms) gives the number of concurrent reads. In my test, 16.0% × 100 = 16, which matches the number of fio threads.

Fix

When reads run without pause, the fraction held is the number of concurrent reads divided by the per-window allowance. There are two ways to lower it: raise the limit, or reduce the number of concurrent reads.

Raising the limit

I raised the limit from 1000 to 4000 per second and compared.14

Limit Reads held p50 p90 p99
1000 per second 16.0% 0.075ms 95.945ms 103.285ms
4000 per second 4.0% 0.064ms 0.099ms 102.236ms

With 4 times the limit, the fraction of reads held dropped to 4.0% and p90 came back to 0.099ms, but p99 stayed at 102.236ms.

The cost is that the limit does less of what it was set for (such as leaving read capacity for other services that use the same disk).

Reducing the number of concurrent reads

I kept the limit at 1000 per second, reduced the number of concurrent reads from 16 to 1, and compared.

Concurrent reads Reads held p50 p90 p99
16 16.0% 0.075ms 95.945ms 103.285ms
1 1.0% 0.029ms 0.046ms 0.232ms

With 1 concurrent read, the fraction of reads held dropped to 1.0% and p99 came back to 0.232ms.

In both cases, reads completed per second stayed at about 1000, the limit.15 While reads are at the limit, increasing the number of concurrent reads does not increase how many reads complete; it only increases the number of reads held.

Reducing the number of concurrent reads cuts the wait only when it actually reduces the reads issued at the same time. If you reduce only the threads while the volume of reads stays the same, the wait moves into the application.

Note: on LVM, look at the row for the underlying disk

LVM is a mechanism that combines disks and presents them as a single disk called a logical volume. iostat shows a row for the logical volume (with a name like dm-0) and separate rows for the disks that make up the logical volume (the underlying disks).16

If you set IOReadIOPSMax= in systemd to the path of a file on a logical volume, the limit applies to the underlying disk.17 The held time is counted in the logical volume's row, so that row looks saturated while the underlying disk's row alone looks idle.18 If you look only at the logical volume's row, the disk appears to be slow. If the logical volume's row is saturated and the underlying disk's row is idle, suspect the limit.

Setting IOReadIOPSMax= to the file /mnt/iomax-p9/data on the logical volume gives the following.

$ cat /sys/fs/cgroup/system.slice/iomax-p9_dm.service/io.max 7:1 rbps=max wbps=max riops=1000 wiops=max $ lsblk -o NAME,KNAME,MAJ:MIN /dev/loop1 NAME KNAME MAJ:MIN loop1 loop1 7:1 └─iomax-lin dm-0 252:0 

└─ marks the logical volume created on top of loop1 (iomax-lin is the name given to the logical volume). The 7:1 in io.max is the underlying disk, loop1.

iostat row r/s r_await %util
dm-0 (logical volume) 1000.0 15.911ms 100.00%
loop1 (underlying disk, where the limit applied) 1000.0 0.098ms 1.04%

If p99 alone jumps, I want to read io.max before comparing the iostat rows. That is the faster way to find out whether a limit is in place.

  1. The kernel 6.17 cgroup v2 documentation on io.max. For systemd, IOReadIOPSMax= in systemd.resource-control. ↩

  2. In kernel 6.17, the limit check (blk_throtl_bio) comes before the read is added to the queue. The iostat wait time is counted from the timestamp recorded when a request enters the queue (start_time_ns). ↩

  3. IOPS limits on cloud block storage (such as EBS limits) are applied on the disk side, so that wait is included in r_await. I did not verify this on real hardware. ↩

  4. In kernels 6.10 through 6.18 that do not have the fix that set the window back to 100ms (f76581f9f1d2, in mainline 6.19), the window for disks treated as SSDs (/sys/block/<device>/queue/rotational is 0) is 20ms. The fix is also in stable: 6.12.63, 6.17.13, and 6.18.5 include it. This comes from reading the source; I did not verify the behavior with a 20ms window. The fix commit is f76581f9f1d2, and the definitions before and after the fix are in v6.17.12 and v6.17.13. ↩

  5. tg_update_slice in kernel 6.17. It starts a new window when the current window has ended and no reads are being held. ↩

  6. The virtual disk is a file in memory (/dev/shm) set up as a loop device (a mechanism that presents a file as a disk). The reads are 4KiB random reads that bypass the page cache (O_DIRECT). One-at-a-time reads used fio's psync (which waits for a read to return before issuing the next), and batched reads used io_uring (a mechanism that can issue many reads at once). The fraction of reads held is the fraction of reads recorded by fio that took longer than 50ms (half of the 100ms window). The iostat values are averages of 9 one-second samples (r_await weighted by r/s). Run on GitHub Actions ubuntu-24.04 (kernel 6.17.0-1022-azure) with fio 3.36. On this kernel, the window was 100ms both for a loop device with rotational 0 (treated as an SSD) and for one with rotational 1. The code is in show-your-work, and the results are in the CI run. ↩

  7. A limit set in a parent cgroup's io.max also holds reads from the cgroups below it. When a read passes its own cgroup's limit, the kernel also checks the parent's limit (blk-throttle.c in kernel 6.17). In systemd, IOReadIOPSMax= can also be set on the slice that groups services (systemd.resource-control). If your own cgroup has no such line, also read io.max in each parent directory up to /sys/fs/cgroup. I did not verify the case where the limit is set on a parent. ↩

  8. The format of /proc/<PID>/cgroup is described in the kernel 6.17 cgroup v2 documentation on /proc/$PID/cgroup. In my test environment, the cgroup directories were under /sys/fs/cgroup. ↩

  9. Docker writes the device number of the path passed to --device-read-iops directly to io.max (moby's daemon_unix.go). Whether cgroups are created through systemd is switched by cgroupDriver in the same file. ↩

  10. In kernel 6.17, the only cgroup file the limit code provides is io.max (blk-throttle.c). io.stat reports only read and write counts and byte counts (blk-cgroup.c). ↩

  11. The location of tracefs is described in the kernel 6.17 documentation ftrace.rst. The line is written at the point where reads over the limit are held (blk-throttle.c). ↩

  12. act_mask is the set of event types to record; the lines were recorded when these three were written to it. The line is written only when notify is included (throtl_log in blk-throttle.c, blk_trace_note_message_enabled). ↩

  13. In a run that recorded from the start of fio to the end, there were 1600 lines, the same as the 1600 reads that took longer than 50ms in fio. With the blktrace command at its defaults, there were 0 such lines (blktrace -d /dev/loop0 -o trace followed by blkparse -i trace | grep -c throtl). ↩

  14. As in the measurements above, fio read one at a time without pause, and I changed either the limit or the number of concurrent reads (the number of fio threads), measuring each condition for 10 seconds. All other conditions, and the way the fraction of reads held was counted, were the same as in the measurements. In both tables, the row with 16 threads and a limit of 1000 per second is the same condition as the limit row in the measurements table, but it was measured in a separate job, so p50 and the fraction held differ slightly. ↩

  15. Reads per second were calculated as the number of concurrent reads divided by fio's average time per read. With a limit of 1000 per second, 16 threads gave 16 ÷ 15.976ms and 1 thread gave 1 ÷ 0.999ms, both about 1000. ↩

  16. dm stands for device-mapper (the kernel mechanism LVM uses to create logical volumes). In my test, I did not use LVM commands. Instead, I created a dm-linear device (a type of dm device that presents a range of the underlying disk as a single disk, unchanged) directly on top of a loop device, and read a file on the ext4 file system on it. dm-linear is the basic building block LVM uses to create logical volumes (the kernel 6.17 documentation linear.rst). ↩

  17. If the path passed to IOReadIOPSMax= is a file, systemd looks up the disk the file is on, and if that is a dm device, follows it down to the underlying disk (systemd's cgroup.c). The change that made systemd follow it down to the underlying disk is systemd/systemd#15024 (reported in systemd/systemd#15008). ↩

  18. Read time in the dm row is counted from when dm receives the read (dm.c). The reads dm passes to the underlying disk are treated as belonging to the same cgroup as the read dm received (bio.c), and are held by the underlying disk's limit. The held time is counted in the dm row, not in the underlying disk's row. ↩