From: Xiubo Li Date: Wed, 02 Dec 2026 14:18:20 +0100 Subject: [PATCH v3 1/3] ceph: do not read ahead on an mmap fault without Fc MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261202-ceph-append-excl-v3-1-68fdbef33f32@clyso.com> References: <20261202-ceph-append-excl-v3-0-68fdbef33f32@clyso.com> In-Reply-To: <20261202-ceph-append-excl-v3-0-68fdbef33f32@clyso.com> To: Ilya Dryomov , Alex Markuze , Viacheslav Dubeyko Cc: ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org, Anthony D'Atri , Xiubo Li X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796217507; l=3437; i=xiubo.li@clyso.com; s=20260625; h=from:subject:message-id; bh=BBAFrI3BKcwBa18DsjULW6cdSvYY9YQs79FFqzdS5gA=; b=7aq5uzcuXFlQ89NVZxtenk0dNkiVG3IIS2NMcVrT2nqiP6OUgj6bghGMW8IHwNrKyzkkBWkqT xCbGgKhMi0aDK4G/e9uasUwuczBsJlXfBibkHm9KkQFLHzW5OLGHlGD X-Developer-Key: i=xiubo.li@clyso.com; a=ed25519; pk=V3NGr0AgAopiUhaLY51ipBkLN5LlcLhjOEfLEq1RoZ8= X-Endpoint-Received: by B4 Relay for xiubo.li@clyso.com/20260625 with auth_id=840 List-Id: B4 Relay Submissions 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 --- 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