[PATCH v3 1/3] ceph: do not read ahead on an mmap fault without Fc

From: Xiubo Li via B4 Relay

Date: Wed Dec 02 2026 - 08:18:45 EST


From: Xiubo Li <xiubo.li@xxxxxxxxx>

ceph_filemap_fault() faults a page in through filemap_fault() also when
it got Fr but not Fc, e.g. while another client writes the file and the
filelock is in MIX. It registers an rw context first, and
ceph_init_request() lets every request with such a context through, so
the read-around of that fault fills the page cache with the folios next
to the faulting one as well, without Fc.

Nothing drops those folios: we do not hold Fc, so no revoke of it
invalidates them when another client writes the file. Once we get Fc
and Fb again, a buffered write into one of them uses it as it is and
writes the whole folio back, with the old data over what the other
client wrote in between. Seen with an mmap writer on one client while
two clients took turns appending to the file: on every turn change a
few records of the other client were overwritten with zeroes; with
MADV_RANDOM on the mapping (no read-around) none were.

Fail the readahead of a request whose rw context holds neither Fc nor
LAZYIO, as when there is no rw context at all: the fault then reads
only the folio it needs.

Tested on a vstart cluster, KASAN and lockdep enabled, two mounts, 4
O_APPEND writers on each taking turns on Fx (patch 3 of this series),
150 records of 64 bytes per writer, and an mmap writer rewriting the
first page of the file on one of the mounts:

+--------------------------------------+------+-------------------+
| case | runs | records zeroed |
+--------------------------------------+------+-------------------+
| without this patch | 12 | 62-88 of 1200 |
| without this patch, MADV_RANDOM | 4 | 0 |
| with this patch | 3 | 0 |
+--------------------------------------+------+-------------------+

Signed-off-by: Xiubo Li <xiubo.li@xxxxxxxxx>
---
fs/ceph/addr.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/fs/ceph/addr.c b/fs/ceph/addr.c
index 49b58f85dc17..d14121694051 100644
--- a/fs/ceph/addr.c
+++ b/fs/ceph/addr.c
@@ -754,6 +754,7 @@ static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
struct ceph_file_info *fi = file ? file->private_data : NULL;
int got = 0, want = CEPH_CAP_FILE_CACHE;
struct ceph_netfs_request_data *priv;
+ struct ceph_rw_context *rw_ctx;
int ret = 0;

/* [DEPRECATED] Use PG_private_2 to mark folio being written to the cache. */
@@ -775,7 +776,22 @@ static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
* ceph_read_iter() and ceph_filemap_fault() hold caps and
* register an rw context before entering the page cache.
*/
- if (ceph_find_rw_context(fi)) {
+ rw_ctx = ceph_find_rw_context(fi);
+ if (rw_ctx) {
+ /*
+ * ceph_filemap_fault() may hold Fr only. Then read
+ * just the folio that faulted, not ahead of it: no
+ * revoke of Fc would drop the folios read ahead when
+ * another client writes the file, and once we get
+ * Fc and Fb a buffered write would write them back
+ * stale over that client's data.
+ */
+ if (rreq->origin == NETFS_READAHEAD &&
+ !(rw_ctx->caps & (CEPH_CAP_FILE_CACHE |
+ CEPH_CAP_FILE_LAZYIO))) {
+ ret = -EACCES;
+ goto out;
+ }
rreq->netfs_priv = priv;
return 0;
}

--
2.53.0