Skip to contentSkip to conversation

pNFS/flexfiles: mirrored writes that fall back to the MDS

PATCH Superseded Benjamin Coddington 1 participant 4 messages Nov 20

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover pNFS/flexfiles: mirrored writes that fall back to the MDS

From: Benjamin Coddington <[email protected]>

ff_layout_pg_init_write() can fall back to the MDS from a pg_init in the middle of a mirrored descriptor: when pnfs_generic_pg_check_layout() drops the layout segment, or when a mirror's DS can't be prepared. pg_init runs for each mirror whenever that mirror's list is empty, so by then the other mirrors can already hold requests. nfs_pageio_reset_write_mds() replaces pg_ops with nfs_pgio_rw_ops before it completes the descriptor, and nfs_pgio_rw_ops has no pg_get_mirror, so the completion sees only mirror 0. The requests on the other mirrors are never sent or cleaned up, the mirror array they are linked on is freed, and their folios never leave writeback:
the next fsync hangs in folio_wait_writeback(). nfs_do_recoalesce() and pnfs_write_through_mds() also write to the freed array.

Patch 3 keeps a mirrored descriptor that still holds requests off the MDS:
pg_init returns -EAGAIN, as it does for a mirror count change, and error cleanup redirties or requeues the requests with the flexfiles ops still in place. The next pg_init starts from an empty descriptor and can switch safely. ff_layout_write_pagelist() returns PNFS_TRY_AGAIN instead of PNFS_NOT_ATTEMPTED for a mirrored layout and leaves the fallback to pg_init of the retry.

Patches 1 and 2 fix the generic code that path goes through. O_DIRECT error cleanup releases -EAGAIN requests without writing them, so after patch 3 an O_DIRECT write would report success with some mirrors left holding the old data; patch 1 requeues them for the reschedule instead. Patch 2 makes nfs_do_recoalesce() look its mirror up again after pg_init, and stops nfs_pageio_complete() from sending the next mirror after a failed pg_init released the layout segment.

Sashiko found the bug while reviewing the FF_FLAGS_NO_IO_THRU_MDS series:
https://lore.kernel.org/linux-nfs/[email protected]/
These patches don't depend on that series, and the two apply in either order.

Tested against a reffs MDS and DS with a six-way mirrored layout, on a KASAN kernel, with client fault injection that fails a mirror's pg_init while other mirrors hold requests, fails the pg_init that a retry runs during completion, and fails a mirror's DS setup in write_pagelist. Without the series, fsync hangs in folio_wait_writeback() and KASAN reports use-after-free in nfs_do_recoalesce() and pnfs_write_through_mds(). Without patch 1, an O_DIRECT write returns success while five of the six mirrors keep the old data. With the series, buffered and O_DIRECT writes complete, the data is on all six mirrors, and KASAN reports nothing.

The series is based on v7.4-rc3 with three flexfiles fixes that were posted separately for stable, and also applies to v7.4-rc3 alone:

pNFS/flexfiles: read the layout hdr under i_lock in pg_get_mirror_count_write
https://lore.kernel.org/linux-nfs/23435d398481f51ce2680053cd3a151fe673f91c.1795108831.git.bcodding@hammerspace.com/
pNFS/flexfiles: don't delay or restart an rpc_task that LOCALIO never ran
https://lore.kernel.org/linux-nfs/9c46fda0f2cd6d3843b198dac304b4722514b8e3.1795108831.git.bcodding@hammerspace.com/
pNFS/flexfiles: restart a write that the DS error handler retries
https://lore.kernel.org/linux-nfs/8ee107c9d0c68b6d0ee7305605ff7edbe0ffa160.1795108832.git.bcodding@hammerspace.com/

Benjamin Coddington (3):
NFS: resend O_DIRECT write requests that error cleanup gets with
-EAGAIN
NFS: don't use a mirror after pg_init can free it pNFS/flexfiles: don't switch a busy mirrored write descriptor to the
MDS

 fs/nfs/direct.c                        | 48 ++++++++++++++++----------
 fs/nfs/flexfilelayout/flexfilelayout.c | 48 ++++++++++++++++++++++++--
 fs/nfs/pagelist.c                      | 15 +++++---
 fs/nfs/read.c                          |  3 +-
 fs/nfs/write.c                         |  5 +--
 include/linux/nfs_xdr.h                |  3 +-
 6 files changed, 93 insertions(+), 29 deletions(-)

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
prerequisite-patch-id: 71097d575f7ba74684462ab64b028ff8815dff63
prerequisite-patch-id: 5a73a1fb9d47a31d4ec337ec0264eadbcaa9fd4e
prerequisite-patch-id: 2663ab8e1949d6a11d957cc2784161bf2816cac2

Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/3 NFS: resend O_DIRECT write requests that error cleanup gets with -EAGAIN

From: Benjamin Coddington <[email protected]>

A layout driver's pg_init can fail a descriptor with -EAGAIN while it still holds queued requests; flexfiles does this when the mirror count changes. nfs_pageio_error_cleanup() hands those requests to error_cleanup, and for O_DIRECT writes nfs_write_sync_pgio_error() just unlocks and releases them. nfs_direct_write_schedule_iovec() treats -EAGAIN as soft and defers only the request it was adding, so the queued requests, already counted in requested_bytes, are never written and no error is reported.

Pass the descriptor's nfs_direct_req to error_cleanup. On -EAGAIN, put the O_DIRECT write requests on the commit list and set NFS_ODIRECT_RESCHED_WRITES so the write completion resends them. Move that code out of nfs_direct_write_reschedule_io() into nfs_direct_write_requeue() and use it in both places.

Fixes 954998b60caa ("NFS: Fix error handling for O_DIRECT write scheduling")
---
 fs/nfs/direct.c         | 48 +++++++++++++++++++++++++----------------
 fs/nfs/pagelist.c       |  7 +++---
 fs/nfs/read.c           |  3 ++-
 fs/nfs/write.c          |  5 +++--
 include/linux/nfs_xdr.h |  3 ++-
 5 files changed, 41 insertions(+), 25 deletions(-)
fs/nfs/direct.c +30 -18
index a3c6e8f4ea06..c771bb1b0730 100644
@@ -330,7 +330,8 @@ static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
 	hdr->release(hdr);
 }
 
-static void nfs_read_sync_pgio_error(struct list_head *head, int error)
+static void nfs_read_sync_pgio_error(struct list_head *head, int error,
+				     struct nfs_direct_req *dreq)
 {
 	nfs_release_request_list(head);
 }
@@ -818,39 +819,50 @@ static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
 	hdr->release(hdr);
 }
 
-static void nfs_write_sync_pgio_error(struct list_head *head, int error)
+static void nfs_direct_write_requeue(struct nfs_direct_req *dreq,
+				     struct list_head *head)
 {
+	struct nfs_commit_info cinfo;
 	struct nfs_page *req;
 
+	nfs_init_cinfo_from_dreq(&cinfo, dreq);
+	spin_lock(&dreq->lock);
+	if (dreq->error == 0)
+		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
+	spin_unlock(&dreq->lock);
 	while (!list_empty(head)) {
 		req = nfs_list_entry(head->next);
 		nfs_list_remove_request(req);
-		nfs_unlock_and_release_request(req);
+		nfs_unlock_request(req);
+		nfs_mark_request_commit(req, NULL, &cinfo, 0);
 	}
 }
 
-static void nfs_direct_write_reschedule_io(struct nfs_pgio_header *hdr)
+static void nfs_write_sync_pgio_error(struct list_head *head, int error,
+				      struct nfs_direct_req *dreq)
 {
-	struct nfs_direct_req *dreq = hdr->dreq;
 	struct nfs_page *req;
-	struct nfs_commit_info cinfo;
-
-	trace_nfs_direct_write_reschedule_io(dreq);
 
-	nfs_init_cinfo_from_dreq(&cinfo, dreq);
-	spin_lock(&dreq->lock);
-	if (dreq->error == 0)
-		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
-	set_bit(NFS_IOHDR_REDO, &hdr->flags);
-	spin_unlock(&dreq->lock);
-	while (!list_empty(&hdr->pages)) {
-		req = nfs_list_entry(hdr->pages.next);
+	/* -EAGAIN: requests already counted by the caller must be resent */
+	if (error == -EAGAIN) {
+		nfs_direct_write_requeue(dreq, head);
+		return;
+	}
+	while (!list_empty(head)) {
+		req = nfs_list_entry(head->next);
 		nfs_list_remove_request(req);
-		nfs_unlock_request(req);
-		nfs_mark_request_commit(req, NULL, &cinfo, 0);
+		nfs_unlock_and_release_request(req);
 	}
 }
 
+static void nfs_direct_write_reschedule_io(struct nfs_pgio_header *hdr)
+{
+	trace_nfs_direct_write_reschedule_io(hdr->dreq);
+
+	set_bit(NFS_IOHDR_REDO, &hdr->flags);
+	nfs_direct_write_requeue(hdr->dreq, &hdr->pages);
+}
+
 static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
 	.error_cleanup = nfs_write_sync_pgio_error,
 	.init_hdr = nfs_direct_pgio_init,
fs/nfs/pagelist.c +4 -3
index 71f0ce2bc4ea..bdf8b7305e61 100644
@@ -1208,7 +1208,8 @@ nfs_pageio_cleanup_request(struct nfs_pageio_descriptor *desc,
 	LIST_HEAD(head);
 
 	nfs_list_move_request(req, &head);
-	desc->pg_completion_ops->error_cleanup(&head, desc->pg_error);
+	desc->pg_completion_ops->error_cleanup(&head, desc->pg_error,
+					       desc->pg_dreq);
 }
 
 /**
@@ -1337,7 +1338,7 @@ static void nfs_pageio_error_cleanup(struct nfs_pageio_descriptor *desc)
 	for (midx = 0; midx < desc->pg_mirror_count; midx++) {
 		mirror = nfs_pgio_get_mirror(desc, midx);
 		desc->pg_completion_ops->error_cleanup(&mirror->pg_list,
-				desc->pg_error);
+				desc->pg_error, desc->pg_dreq);
 	}
 }
 
@@ -1440,7 +1441,7 @@ int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
 	nfs_pageio_complete(desc);
 	if (!list_empty(&pages)) {
 		int err = desc->pg_error < 0 ? desc->pg_error : -EIO;
-		hdr->completion_ops->error_cleanup(&pages, err);
+		hdr->completion_ops->error_cleanup(&pages, err, hdr->dreq);
 		nfs_set_pgio_error(hdr, err, hdr->io_start);
 		return err;
 	}
fs/nfs/read.c +2 -1
index e7497b029d6c..7047883ddb6b 100644
@@ -220,7 +220,8 @@ static void nfs_initiate_read(struct nfs_pgio_header *hdr,
 }
 
 static void
-nfs_async_read_error(struct list_head *head, int error)
+nfs_async_read_error(struct list_head *head, int error,
+		     struct nfs_direct_req *dreq)
 {
 	struct nfs_page	*req;
 
fs/nfs/write.c +3 -2
index 0b42d31f7a75..ab94b8a833f3 100644
@@ -1373,7 +1373,8 @@ static void nfs_redirty_request(struct nfs_page *req)
 	nfs_release_request(req);
 }
 
-static void nfs_async_write_error(struct list_head *head, int error)
+static void nfs_async_write_error(struct list_head *head, int error,
+				  struct nfs_direct_req *dreq)
 {
 	struct nfs_page	*req;
 
@@ -1389,7 +1390,7 @@ static void nfs_async_write_error(struct list_head *head, int error)
 
 static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
 {
-	nfs_async_write_error(&hdr->pages, 0);
+	nfs_async_write_error(&hdr->pages, 0, NULL);
 }
 
 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
include/linux/nfs_xdr.h +2 -1
index c0e29b4dfa62..fce0e773bd7b 100644
@@ -1742,7 +1742,8 @@ struct nfs_commit_data {
 };
 
 struct nfs_pgio_completion_ops {
-	void	(*error_cleanup)(struct list_head *head, int);
+	void	(*error_cleanup)(struct list_head *head, int error,
+				 struct nfs_direct_req *dreq);
 	void	(*init_hdr)(struct nfs_pgio_header *hdr);
 	void	(*completion)(struct nfs_pgio_header *hdr);
 	void	(*reschedule_io)(struct nfs_pgio_header *hdr);
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/3 NFS: don't use a mirror after pg_init can free it

In reply to Benjamin Coddington

From: Benjamin Coddington <[email protected]>

When a layout driver's pg_init switches a mirrored descriptor to the MDS, nfs_pageio_reset_write_mds() frees pg_mirrors_dynamic. nfs_do_recoalesce() keeps the mirror pointer it took before calling __nfs_pageio_add_request() and, on error, splices the remaining requests onto it and sets pg_recoalesce, writing to freed memory. Look up the current mirror again after the add.

nfs_pageio_complete() also keeps completing the remaining mirrors after one of them fails. A pg_init that fails during completion may have released the layout segment, and the next mirror's pg_doio then sends with a NULL pg_lseg. Stop completing mirrors once pg_error is set; error cleanup then handles the requests still queued on every mirror.

Fixes 03d5eb65b538 ("NFS: Fix a memory leak in nfs_do_recoalesce")
Fixes a7d42ddb3099 ("nfs: add mirroring support to pgio layer")
---
 fs/nfs/pagelist.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/nfs/pagelist.c +7 -1
index bdf8b7305e61..3bdf3f308c8e 100644
@@ -1299,6 +1299,8 @@ static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
 			req = list_first_entry(&head, struct nfs_page, wb_list);
 			if (__nfs_pageio_add_request(desc, req))
 				continue;
+			/* pg_init may have switched to the MDS and freed the mirrors */
+			mirror = nfs_pgio_current_mirror(desc);
 			if (desc->pg_error < 0) {
 				list_splice_tail(&head, &mirror->pg_list);
 				mirror->pg_recoalesce = 1;
@@ -1457,8 +1459,12 @@ void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
 {
 	u32 midx;
 
-	for (midx = 0; midx < desc->pg_mirror_count; midx++)
+	for (midx = 0; midx < desc->pg_mirror_count; midx++) {
+		/* A failed pg_init may have released what the next mirror needs */
+		if (desc->pg_error < 0)
+			break;
 		nfs_pageio_complete_mirror(desc, midx);
+	}
 
 	if (desc->pg_error < 0)
 		nfs_pageio_error_cleanup(desc);
Signature · 1 line
2.53.0
#3 lore raw

PATCH 3/3 pNFS/flexfiles: don't switch a busy mirrored write descriptor to the MDS

In reply to Benjamin Coddington

From: Benjamin Coddington <[email protected]>

nfs_pageio_reset_write_mds() replaces pg_ops before it completes the descriptor, so the completion sees only mirror 0. Requests queued on the other mirrors are never sent or cleaned up, the mirror array they are linked on is freed, and their folios never leave writeback.

ff_layout_pg_init_write() falls back to the MDS from a later pg_init while other mirrors still hold requests: when the layout segment is gone, or when a mirror's DS cannot be prepared. In that case return -EAGAIN as for a mirror count change, so error cleanup redirties the queued requests, or requeues them for an O_DIRECT write, with the flexfiles ops still in place; the next pg_init starts from an empty descriptor and can switch safely.

ff_layout_write_pagelist() returning PNFS_NOT_ATTEMPTED has the same problem through pnfs_write_through_mds(). It does that when a DS of the layout is unavailable or when preparing the DS failed fatally. For a mirrored layout, return PNFS_TRY_AGAIN instead and leave the MDS fallback to pg_init of the retry. pg_init now also takes it, through the check above, when the DS it would use for a mirror is marked unavailable; a failed prepare_ds fails there again. Other failures, such as creating the DS client or finding a credential, are not visible to pg_init, so mark that DS unavailable before retrying, unless the failure was a fatal one from preparing the DS. A single-mirror descriptor keeps using
PNFS_NOT_ATTEMPTED: pnfs_write_through_mds() switches it safely, since its only mirror is not freed.

Fixes d67ae825a59d ("pnfs/flexfiles: Add the FlexFile Layout Driver")
---
 fs/nfs/flexfilelayout/flexfilelayout.c | 48 ++++++++++++++++++++++++--
 1 file changed, 45 insertions(+), 3 deletions(-)
fs/nfs/flexfilelayout/flexfilelayout.c +45 -3
index 3adfcc7d0cfc..111662f14524 100644
@@ -1133,6 +1133,22 @@ ff_layout_pg_init_read(struct nfs_pageio_descriptor *pgio,
 	nfs_pageio_reset_read_mds(pgio);
 }
 
+/*
+ * Switching to the MDS discards the mirror array, so it must not happen while
+ * any mirror still holds requests.
+ */
+static bool ff_layout_pg_mirrors_busy(struct nfs_pageio_descriptor *pgio)
+{
+	u32 i;
+
+	if (pgio->pg_mirror_count == 1)
+		return false;
+	for (i = 0; i < pgio->pg_mirror_count; i++)
+		if (!list_empty(&pgio->pg_mirrors[i].pg_list))
+			return true;
+	return false;
+}
+
 static void
 ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio,
 			struct nfs_page *req)
@@ -1172,6 +1188,17 @@ ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio,
 			req_offset(req));
 		mirror_ds = ff_layout_get_mirror_ds(pgio->pg_lseg->pls_layout,
 						    mirror, dss_id);
+		/*
+		 * A mirrored write cannot fall back to the MDS from
+		 * write_pagelist, so do it here for an unavailable DS.
+		 */
+		if (pgio->pg_mirror_count > 1 &&
+		    !ff_layout_no_fallback_to_mds(pgio->pg_lseg) &&
+		    !IS_ERR_OR_NULL(mirror_ds) &&
+		    nfs4_test_deviceid_unavailable(&mirror_ds->id_node)) {
+			nfs4_ff_layout_put_deviceid(mirror_ds);
+			goto out_mds;
+		}
 		ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, mirror,
 					       mirror_ds, dss_id, OP_WRITE);
 		if (IS_ERR(ds)) {
@@ -1198,6 +1225,8 @@ ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio,
 	pgio->pg_error = -EAGAIN;
 	return;
 out_mds:
+	if (ff_layout_pg_mirrors_busy(pgio))
+		goto out_eagain;
 	trace_pnfs_mds_fallback_pg_init_write(pgio->pg_inode,
 			0, NFS4_MAX_UINT64, IOMODE_RW,
 			NFS_I(pgio->pg_inode)->layout,
@@ -2455,9 +2484,8 @@ ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
 	return PNFS_ATTEMPTED;
 
 out_failed:
-	nfs4_ff_layout_put_deviceid(mirror_ds);
 	if (ff_layout_avoid_mds_available_ds(lseg) && !ds_fatal_error)
-		return PNFS_TRY_AGAIN;
+		goto out_try_again;
 	if (ff_layout_no_fallback_to_mds(lseg)) {
 		/*
 		 * FF_FLAGS_NO_IO_THRU_MDS: force fresh LAYOUTGET,
@@ -2465,12 +2493,26 @@ ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
 		 */
 		pnfs_error_mark_layout_for_return(hdr->inode, lseg,
 						  &mirror->dss[dss_id].devid);
-		return PNFS_TRY_AGAIN;
+		goto out_try_again;
+	}
+	/*
+	 * pnfs_write_through_mds() cannot switch a mirrored descriptor.  Mark
+	 * this DS unavailable so pg_init of the retry uses the MDS; a failed
+	 * prepare_ds fails again there.
+	 */
+	if (FF_LAYOUT_MIRROR_COUNT(lseg) > 1) {
+		if (!ds_fatal_error && !IS_ERR_OR_NULL(mirror_ds))
+			nfs4_mark_deviceid_unavailable(&mirror_ds->id_node);
+		goto out_try_again;
 	}
+	nfs4_ff_layout_put_deviceid(mirror_ds);
 	trace_pnfs_mds_fallback_write_pagelist(hdr->inode,
 			hdr->args.offset, hdr->args.count,
 			IOMODE_RW, NFS_I(hdr->inode)->layout, lseg);
 	return PNFS_NOT_ATTEMPTED;
+out_try_again:
+	nfs4_ff_layout_put_deviceid(mirror_ds);
+	return PNFS_TRY_AGAIN;
 }
 
 static struct nfs_fh *
Signature · 1 line
2.53.0