Skip to contentSkip to conversation

mm/filemap: plain buffered read evicts a folio left by RWF_DONTCACHE

DISCUSSION Tianyi Chen 1 participant 1 message Nov 19

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi Jens,

On current mainline (4405c18c95d2, after v7.4-rc3), a plain pread() can evict the page it just read if another reader brought that page in with RWF_DONTCACHE.

On ext4 I opened a cold file twice. A read the first 64 KiB with RWF_DONTCACHE and closed its fd. B then read the next 4 KiB, which readahead had brought in for A but A never read. The page was resident before B's read and gone afterwards, so B's next read of it went to the device again.

As a control, A did a plain read and then FADV_DONTNEED on the 64 KiB it had read, so both runs had the same mincore() bitmap before B started. In that case B's read left the page in the cache.

When B read the 48 prefetched pages sequentially three times, its second pass over those 192 KiB issued 432 KiB of device reads (5 requests), against none in the control. The third pass did no I/O in either case. The results were the same in five runs in an x86-64 KVM guest. Pages that were cached before the uncached reader started were not affected in my tests: with a warm SQLite query running next to an RWF_DONTCACHE scan, the query thread did no extra reads.

On a cached lookup, __filemap_get_folio_mpol() clears dropbehind unless FGP_DONTCACHE is set. filemap_get_read_batch() doesn't, and filemap_read() still calls filemap_end_dropbehind_read() on the folios in the batch. In the v8 FGP_DONTCACHE patch you noted that when there are competing uncached and cached users of a folio, "it should not get pruned":

https://lore.kernel.org/all/[email protected]/

Should a plain buffered read hit clear dropbehind as well?

A reproducer is below. Run it with python3 in a writable directory on ext4 with 4 KiB pages. On this tree it prints after=0 for the RWF_DONTCACHE run and after=1 for the plain run.

Thanks,
Tianyi

Assisted-by LLM

---8<---
#!/usr/bin/env python3
"""Use an empty temporary file per arm on an RWF_DONTCACHE-capable regular filesystem.""" import ctypes
import mmap
import os
import tempfile
import time

P = os.sysconf('SC_PAGESIZE')
assert P == 4096, 'example uses 4 KiB pages'
count = 1024
libc = ctypes.CDLL(None, use_errno=True)
def resident(m):
v = (ctypes.c_ubyte * count)()
addr = ctypes.addressof(ctypes.c_char.from_buffer(m))
if libc.mincore(ctypes.c_void_p(addr), ctypes.c_size_t(len(m)), v):
raise OSError(ctypes.get_errno(), 'mincore')
return bytes(x & 1 for x in v)

def run(flag):
with tempfile.NamedTemporaryFile(dir='.') as f:
f.write(b'x' * (P * count)); f.flush(); os.fsync(f.fileno()) os.posix_fadvise(f.fileno(), 0, 0, os.POSIX_FADV_DONTNEED)
with open(f.name, 'rb', buffering=0) as b, mmap.mmap(
b.fileno(), 0, access=mmap.ACCESS_COPY) as m:
assert not any(resident(m)), 'fixture not cold'
with open(f.name, 'rb', buffering=0) as a:
buf = bytearray(16 * P)
assert os.preadv(a.fileno(), [buf], 0, flag) == len(buf)
if not flag: # Match residency, without advising the target.
os.posix_fadvise(a.fileno(), 0, len(buf), os.POSIX_FADV_DONTNEED)
time.sleep(.05)
before = resident(m)
target = before.find(b'\1', 16)
assert target >= 16, 'no prefetched target outside A read' assert os.pread(b.fileno(), P, target * P) == b'x' * P after = resident(m)
print(f'A flags={flag:#x} page={target} before=1 after={after[target]}') return before, target

dc = run(0x80) # RWF_DONTCACHE
ordinary = run(0)
assert dc == ordinary, 'initial page-cache residency or target did not match'