Skip to content

os/bluestore: add bluestore_page_cache_read - #72884

Draft
dvanders wants to merge 3 commits into
ceph:mainfrom
dvanders:wip-lowmem-osd
Draft

dvanders wants to merge 3 commits into
ceph:mainfrom
dvanders:wip-lowmem-osd

Conversation

@dvanders

@dvanders dvanders commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

BlueStore opens the block device O_DIRECT, so object data is cached only by
BlueStore itself, inside osd_memory_target. Running small OSDs means a small
target, and the data cache shrinks with it.

This adds bluestore_page_cache_read (default off). Object data reads then go
through the buffered descriptor, so the kernel page cache can act as the data
cache. It is sized by memory the host isn't otherwise using and given back under
pressure. This is what bluefs_buffered_io already does for RocksDB reads,
extended to object data.

How it works

  • blk: aio_read() gains a buffered flag (default false). A buffered read
    is a synchronous pread() on the buffered fd: libaio would block at submit on
    a non-O_DIRECT fd. io_uring is not used.
  • os/bluestore: only blobs with a checksum are read through the page
    cache. A stale page, for example after a racing O_DIRECT write, then fails
    verification instead of being returned. When a page-cache read fails its
    checksum, the cached pages are dropped and the read is retried O_DIRECT;
    retries never use the page cache. Reads hinted NOCACHE/DONTNEED stay O_DIRECT.
  • New perf counters: page_cache_read_bytes, page_cache_csum_retries.
  • To avoid caching data twice, shrink BlueStore's own data cache when enabling
    this, e.g. bluestore_cache_meta_ratio=0.5, bluestore_cache_kv_ratio=0.45,
    bluestore_cache_kv_onode_ratio=0.04.

Measurements

vstart with 6 OSDs, 256 PGs/OSD, an rbd pool at size 3 with a 24 GiB image
(72 GiB of replicas), bluestore_debug_enforce_settings=hdd, local NVMe. All
OSDs ran in one cgroup with a hard memory.max that also counts the page
cache. Phases: sequential fill, 3 GiB of 4K random writes, two passes of 4K
random reads, osd.0 out/in recovery, deep scrub.

osd_memory_target / cgroup cap per OSD 4K randread / re-read ops/s, off → on
1.5 GiB / 2 GiB 31,207 / 31,523 → 33,313 / 33,974 (+7% / +8%)
1 GiB / 1 GiB (*) 30,436 / 30,601 → 32,870 / 33,823 (+8% / +11%)

(*) With a shorter PG log and smaller RocksDB memtables; at defaults, a 1 GiB
target OOMs on this workload whether or not this option is enabled.

  • The page cache filled whatever the cap left free, and the kernel reclaimed it
    at the limit with no OOM kills.
  • Random writes and per-OSD RSS were unchanged.
  • 0 checksum retries in about 125 GiB read through the page cache across
    all runs.

Resolved: memory left after recovery

In 2 of 3 runs with this option and the low data-cache ratios together, the OSD that was marked out and back in ended recovery with ~80 MiB in the bluestore_writing mempool. This was not caused by this change. The OSD op history kept the last 20 recovery pushes and their data, because MOSDPGPush did not drop its buffers once an op was done. It reproduces with this option off, on a 3-OSD cluster with default settings. Fixed separately in #72885 (tracker 81945).

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

KernelDevice already keeps both an O_DIRECT and a buffered descriptor and
already honours a buffered flag in read() and read_random() via
choose_fd(). aio_read() had no such flag and always used
fd_directs[WRITE_LIFE_NOT_SET], so the async path - the one that serves
BlueStore object reads - could not use the page cache at all.

Add the flag, defaulted to false so existing callers are unaffected.

libaio cannot submit buffered I/O without blocking: io_submit() on a
descriptor that was not opened O_DIRECT degrades to a synchronous submit
and would stall every read batched with it. So a buffered read is always
done synchronously, with read() on the buffered descriptor. Callers wait
for their aios anyway, so this only gives up the parallelism across the
extents of one read, and a page cache hit returns at once. io_uring is
deliberately not used for this: BlueStore shares one ring between threads
under a submission lock, and it is not used in the field.

SPDK bypasses the kernel, so NVMEDevice takes the flag and ignores it.

Assisted-by: Claude-Code-v2.1.303:claude-opus-5-5
Signed-off-by: Dan van der Ster <[email protected]>
BlueStore opens the block device O_DIRECT, so object data is cached only
by BlueStore itself, inside osd_memory_target. To keep an OSD small, that
target has to be small, and the data cache shrinks with it: on a
rotational OSD every read it misses is another seek, where FileStore on
XFS would have been served from the page cache.

Add an option to issue object data reads through the buffered descriptor,
so the page cache can act as the data cache instead, sized by whatever
memory the host is not otherwise using and given back under pressure.
This is the mechanism bluefs_buffered_io has applied to RocksDB reads by
default for years, extended to object data. It relies on an O_DIRECT
write invalidating the page cache over the range it writes.

The existing per-read buffered flag is reused, so reads a client hints
NOCACHE or DONTNEED do not fill the page cache either. Blobs without a
checksum are always read O_DIRECT. If a page cache read fails checksum
validation, as one left stale by a racing O_DIRECT write would, the pages
it came from are dropped and the read is retried O_DIRECT: the existing
retry would otherwise read the same stale pages again. Two perf counters,
page_cache_read_bytes and page_cache_csum_retries, show how much is read
this way and whether that ever happens.

Defaults to off. The costs are documented on the option: data is held
twice unless BlueStore's own data cache is shrunk to match, page cache
pressure can provoke the swapping stalls known from bluefs_buffered_io,
and buffered reads are synchronous.

Assisted-by: Claude-Code-v2.1.303:claude-opus-5-5
Signed-off-by: Dan van der Ster <[email protected]>
Add unit coverage for reads issued through the non-O_DIRECT descriptor,
which is what bluestore_page_cache_read turns on.

At the device layer (unittest_bdev):

 - a buffered aio_read returns what a direct write put there, and returns
   the same bytes a direct read does
 - a buffered read is not handed to libaio: it completes inline and
   leaves nothing queued on the IOContext, where a direct read is queued
   for async submission. This is the observable difference between the
   two paths, so assert on it rather than only on the data, otherwise the
   tests would pass even if the flag were ignored
 - a buffered and a direct read may share one IOContext, with the
   buffered one completing inline while the direct one is still queued
 - page cache coherence: a range pulled into the page cache by a buffered
   read must not be readable in its stale form after an O_DIRECT write
   supersedes it. The whole feature rests on this, as bluefs_buffered_io
   already does by default, so test it rather than assume it

At the store layer (store_test):

 - with bluestore_page_cache_read enabled, write, remount to empty
   BlueStore's own caches so the read has to reach the device, and read
   back; then overwrite, remount, and confirm the previous contents do
   not reappear. Also read a subrange of a range already cached
 - a page cache read that fails its checksum is retried O_DIRECT: with
   every checksum failing, page_cache_csum_retries counts exactly one
   (only the first attempt used the page cache) and the read ends in EIO;
   with injection off, it reads back correctly

Assisted-by: Claude-Code-v2.1.303:claude-opus-5-5
Signed-off-by: Dan van der Ster <[email protected]>
@github-actions

Copy link
Copy Markdown

Config Diff Tool Output

+ added: bluestore_page_cache_read (global.yaml.in)

The above configuration changes are found in the PR. Please update the relevant release documentation if necessary.
Ignore this comment if docs are already updated. To make the "Check ceph config changes" CI check pass, please comment /config check ok and re-run the test.

@ming1

ming1 commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Thanks for this. The motivation is a really good one: an OSD that has to run
in 1–2 GiB loses its data cache first, and that hurts most on rotational
disks. I'm glad someone is pushing on it, and the measurements under a hard
cgroup cap are useful on their own.

That said, I have doubts that the page cache is the right place to get that
cache back, and I'd like to suggest we look at other options before going
down this route. My concerns:

  • Cache coherence becomes a real problem. Today there is one data cache
    and BlueStore invalidates it itself. Now there are two, and the kernel only
    half-keeps its side coherent with our writes: for a libaio O_DIRECT write
    it drops the cached pages before the DMA, never after (block/fops.c). A
    buffered read racing into that window caches stale data for good. The PR
    relies on the checksum to catch it, which is detect-and-retry rather than
    prevention, and the coherence test can't hit the race: it waits for the
    write and runs on a file.
  • Double caching by default. The page-cache path requires buffered
    (BlueStore.cc:13522) and the same flag drives did_read() (:13586, :13345),
    so every page-cache read is also inserted into bluestore_cache_data. The
    workaround is three ratio knobs, and bluestore_default_buffered_read=false
    silently disables the feature.
  • An extra copy on every read, and a page-cache walk on every write.
    O_DIRECT DMAs straight into the bufferlist. This path is disk → page cache
    → pread() memcpy (KernelDevice.cc:1459), so on a miss the data crosses
    memory twice, and even a hit costs a full memcpy where a BlueStore cache
    hit today costs a refcount. Every O_DIRECT write now also pays the
    invalidate over its range.
  • Reads go serial. A cold N-blob read becomes N synchronous pread()s in
    the op thread instead of one aio batch, and _do_readv() serializes across
    intervals too. With 64 KiB blobs on flash, a 4 MiB object is 64 round trips.
    4K randread can't show this.
  • The cache lives outside osd_memory_target. The tuner can't see it,
    OSDs on a host compete with no fairness, reclaim runs inside the OSD's read
    path, and the +7–11% is under a cgroup cap most deployments don't set.
  • Smaller points, for completeness: the buffered error path doesn't follow
    _aio_thread()'s bluestore_fail_eio policy and leaves queued aios
    unsubmitted; buffer_miss_lat stops recording; recovery pushes are not
    excluded (RecoveryHandle::cache_dont_need is never set, EC shard reads
    carry flags 0), contrary to the description.

The data cache sits at its 128 MiB floor on a 1 GiB OSD because the rest of
the budget goes to things the tuner doesn't manage: PG log, RocksDB
memtables/WAL, in-flight client messages. Trimming those with existing options
took a 1 GiB OSD to ~600 MiB peak in the same capped test, with no throughput
loss. I think that memory belongs to PriorityCache, not to a second cache
holding a second copy. If "grow when the host is idle, shrink under pressure"
is the goal, I'd make osd_memory_target elastic between a floor and a
ceiling and let MempoolThread (pcm->balance()/tune_memory(),
:5657/:5666) do the shrinking. One cache, one owner, no extra copies, no new
consistency argument. Happy to help work that out.

@ifed01

ifed01 commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

My concern (mostly speculative) is that CRC32 is pretty weak and it has good enough chances to miss the error in this scenario. Prior to this change we used it for data-at-rest error detection which are not that frequent per se. Now it goes as a mean to avoid races between read and write.Which could be dangerous.

@ifed01

ifed01 commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

My concern (mostly speculative) is that CRC32 is pretty weak and it has good enough chances to miss the error in this scenario. Prior to this change we used it for data-at-rest error detection which are not that frequent per se. Now it goes as a mean to avoid races between read and write.Which could be dangerous.

AI suggest that you might want to use RWF_DONTCACHE flag (needs fresh kernels and likely no libaio) or switch to XXH3-64 checksum alg

@dvanders

Copy link
Copy Markdown
Contributor Author

Thanks both, this is a really useful review.

@ming1 I checked your points and you're right on all of them. In blkdev_direct_write() the post-write invalidate only runs when the write completes synchronously, so a libaio write only gets the invalidate before the DMA, and a buffered read in that window caches stale data. With buffered driving both the page-cache path and did_read(), every page-cache read is also cached by BlueStore; and recovery reads aren't excluded, since nothing sets RecoveryHandle::cache_dont_need. Detect-and-retry on a checksum isn't a good enough answer to the first one, which is @ifed01's point too: crc32c was meant to catch data-at-rest errors, not to arbitrate races.

I also agree with your reading of the numbers: the real win in the capped tests came from trimming what the tuner doesn't manage (PG log, RocksDB memtables/WAL, in-flight client messages), not from the page cache. So I'll move this to draft and work on that instead:

  1. Scale those consumers with osd_memory_target, so a small target doesn't need hand tuning. At a 1 GiB target, the defaults alone OOM'd (max_total_wal_size is 1 GiB and osd_client_message_size_cap is 500 MiB).
  2. Your elastic target: a floor and a ceiling, with tune_memory() growing the target while the host has memory to spare and shrinking it under pressure. One cache, one owner. I'd be glad to work that out with you.

@ifed01 agreed. RWF_DONTCACHE would also mean leaving libaio, and if we drop the page-cache path it's moot.

@dvanders
dvanders marked this pull request as draft October 21, 2026 16:44
@dvanders

Copy link
Copy Markdown
Contributor Author

The first part of the plan from my last comment is up as #72964: the uncached limits now scale with osd_memory_target, so a 1 GiB OSD stays under 1 GiB without hand tuning.

@aclamk

aclamk commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

@dvanders Great effort.
@ming1 Superb analysis.

Currently we have:

  1. bluestore_default_buffered_write
    Controls how aio_write executes its action; specifically, switches from aio+O_DIRECT to _sync_write() when buffered=true.
    Controls if BlueStore is preserving the data in buffer_cache.
  2. bluestore_default_buffered_read
    Does not control how read is executed, it always goes to aio+O_DIRECT.
    Controls if BlueStore is preserving the data in buffer_cache.

Proposal:

  1. create bluestore_default_buffered_io = controls if the fd that is used for aio is opened in O_DIRECT mode vs opened in linux page cache buffered mode.
  2. modify bluestore_default_buffered_write = controls if BlueStore keeps data in its buffer cache after write
  3. modify bluestore_default_buffered_read = controls if BlueStore keeps data it just read in buffer cache when data is uncompressed
  4. create bluestore_default_buffered_read_compressed = controls if BlueStore keeps data it just read in buffer cache when data is compressed
    bluestore_default_buffered_io=true and bluestore_default_buffered_read=true will obviously be wasteful.

The proposal make it impossible to cache on reads, but do not cache on writes. I am not sure whether it was a goal of the PR.
In any case, one could deploy multiple OSDs on single host, leave some extra memory unassigned, and let OSDs compete for it via linux page cache.
Downside: now the fdatasync() would be the place that collects latency. Maybe in buffered mode we should call sync_file_range.
Extra benefit: there are powerpc machines that have 64KB page; they sometimes suffer from clash between BlueFS buffered io and BlueStore's O_DIRECT. This should help.

@github-actions

Copy link
Copy Markdown

This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants