pNFS/flexfiles: mirrored writes that fall back to the MDS
Subsystems (2)
v2 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 4 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. When a mirror's DS fails in ff_layout_write_pagelist(), it no longer returns PNFS_NOT_ATTEMPTED for a mirrored layout. Instead it returns the layout with the failing device and retries with a new one, as RFC 8435 section 7 asks and as FF_FLAGS_NO_IO_THRU_MDS already does; section 8.2.3 leaves it to the server to decide, in that layout, whether the errant mirror stays. The write goes to the MDS only if the server has no layout to give, or the DS can't be prepared again.
Patches 1 to 3 fix the generic code that path goes through. O_DIRECT error cleanup releases -EAGAIN requests without writing them, so 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. Patch 3 sends every mirror when one of them can take no more, so the mirrors stay in step and the pg_init for the next request, for example past the end of a layout segment, finds the descriptor empty.
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, fails a mirror's DS setup in write_pagelist, and ends the layout segment partway through a write. 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 v1 of the series, a write that crosses the end of a layout segment loops in writeback. With this version, buffered and O_DIRECT writes complete, a DS failure in write_pagelist is followed by a LAYOUTRETURN and a new LAYOUTGET instead of an MDS write, 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/
Changes since v1:
https://lore.kernel.org/linux-nfs/[email protected]/
- New patch 3: send every mirror when one of them can take no more. With v1, a write crossing the end of a layout segment, where the next range has no layout, looped: the busy check redirtied the previous request every pass (sashiko).
- Patch 4 (was 3): a DS failure in write_pagelist returns the layout and retries with a new one instead of marking the deviceid unavailable and falling back to the MDS; pg_init no longer checks for an unavailable DS. The mark affected every file on the client, and a marked device made each new descriptor fail its first request until the mark expired (sashiko). A fatal error from preparing the DS retries without returning the layout.
- Patch 2: Fixes tag 862f35c94730, which made the free reachable, instead of 03d5eb65b538.
- Patch 1: the changelog lists the other -EAGAIN cleanups that now requeue.
Benjamin Coddington (4):
NFS: resend O_DIRECT write requests that error cleanup gets with
-EAGAIN
NFS: don't use a mirror after pg_init can free it
NFS: send every mirror when one of them can take no more pNFS/flexfiles: don't switch a busy mirrored write descriptor to the
MDS
fs/nfs/direct.c | 48 +++++++++++++++---------- fs/nfs/flexfilelayout/flexfilelayout.c | 39 +++++++++++++++++--- fs/nfs/pagelist.c | 49 +++++++++++++++++++++++--- fs/nfs/read.c | 3 +- fs/nfs/write.c | 5 +-- include/linux/nfs_xdr.h | 3 +- 6 files changed, 115 insertions(+), 32 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
prerequisite-patch-id: 71097d575f7ba74684462ab64b028ff8815dff63
prerequisite-patch-id: 5a73a1fb9d47a31d4ec337ec0264eadbcaa9fd4e
prerequisite-patch-id: 2663ab8e1949d6a11d957cc2784161bf2816cac2
Signature · 1 line
2.53.0
PATCH v2 1/4 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.
This also changes the other -EAGAIN cleanups of O_DIRECT write requests:
the requests queued in nfs_direct_write_reschedule()'s descriptor, a mirror copy cleaned up by nfs_pageio_cleanup_request(), and the pages left over when nfs_pageio_resend() fails with -EAGAIN are all requeued rather than released.
--- 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH v2 2/4 NFS: don't use a mirror after pg_init can free it
In reply to Benjamin CoddingtonFrom: 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.
--- fs/nfs/pagelist.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
fs/nfs/pagelist.c +7 -1
@@ -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
PATCH v2 3/4 NFS: send every mirror when one of them can take no more
In reply to Benjamin CoddingtonFrom: Benjamin Coddington <[email protected]>
When a request doesn't fit, __nfs_pageio_add_request() sends only the current mirror. The other mirrors keep their requests until they fill too or the descriptor completes, so a pg_init for the next request runs while they still hold the previous ones. If that pg_init has to give up the mirrors, as flexfiles does when the next range has no layout segment and its write falls back to the MDS, the requests still queued on them are stranded.
Send every mirror at that point. With the same wsize on each data server the mirrors fill at the same request anyway; otherwise a mirror is sent before it is full. Either way the copies go out together and the mirrors stay in step.
Only the current mirror is recoalesced on this path, so if sending another mirror requeues its requests for a retry, fail the descriptor with -EAGAIN; error cleanup then redirties or requeues the requests of every mirror for the next pass.
--- fs/nfs/pagelist.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)
fs/nfs/pagelist.c +33 -1
@@ -1201,6 +1201,38 @@ static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc) } } +/* +* When one mirror can take no more, send every mirror: the copies stay in +* step, and a pg_init for the next request finds the descriptor empty. +*/ +static void nfs_pageio_doio_mirrors (struct nfs_pageio_descriptor *desc) +{ +u32 cur = desc->pg_mirror_idx; +u32 midx, restore_idx; + +nfs_pageio_doio (desc); +/* A single mirror can have a nonzero pg_mirror_idx (flexfiles reads) */ +if (desc->pg_mirror_count ==1 ) +return ; +for (midx =0 ; midx < desc->pg_mirror_count; midx++) { +if (desc->pg_error <0 ) +break ; +if (midx == cur) +continue ; + restore_idx =nfs_pgio_set_current_mirror (desc, midx); +nfs_pageio_doio (desc); +/* +* Only the current mirror is recoalesced from here, so fail a +* retry on another one; error cleanup then redirties or +* requeues the requests of every mirror. +*/ +if (desc->pg_error >=0 && +nfs_pgio_current_mirror (desc)->pg_recoalesce) + desc->pg_error = -EAGAIN ; +nfs_pgio_set_current_mirror (desc, restore_idx); + } +} +static void nfs_pageio_cleanup_request (struct nfs_pageio_descriptor *desc,struct nfs_page *req)
@@ -1262,7 +1294,7 @@ static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,/* Can't coalesce any more, so do I/O */ nfs_page_group_unlock (req); desc->pg_moreio =1 ; -nfs_pageio_doio (desc); +nfs_pageio_doio_mirrors (desc);if (desc->pg_error <0 || mirror->pg_recoalesce)return 0 ;/* retry add_request for this subreq */
Signature · 1 line
2.53.0
PATCH v2 4/4 pNFS/flexfiles: don't switch a busy mirrored write descriptor to the MDS
In reply to Benjamin CoddingtonFrom: 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. After the previous patch the mirrors are sent together, so this happens when one mirror's request is retried during completion while the others still hold theirs, or when pg_init fails partway through the mirror copies of a request. 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(). For a mirrored layout, handle the failure the way FF_FLAGS_NO_IO_THRU_MDS already does: mark the layout for return with the failing device and return PNFS_TRY_AGAIN. RFC 8435 section 7 asks the client to retry with a new layout before falling back to the MDS, and section 8.2.3 leaves it to the server to decide, in that layout, whether the errant mirror stays. The retry's pg_init then uses the new layout, or goes to the MDS if the server has none to give. A fatal error preparing the DS, such as -ENOMEM or a signal, is no evidence against the device, so retry that one without returning the
layout: pg_init prepares the DS again and falls back to the MDS if that fails too. A single-mirror descriptor keeps using PNFS_NOT_ATTEMPTED:pnfs_write_through_mds() switches it safely, since its only mirror is not freed.
--- fs/nfs/flexfilelayout/flexfilelayout.c | 39 ++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 5 deletions(-)
fs/nfs/flexfilelayout/flexfilelayout.c +34 -5
@@ -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)
@@ -1198,6 +1214,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,
@@ -2458,15 +2476,26 @@ ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)nfs4_ff_layout_put_deviceid (mirror_ds);if (ff_layout_avoid_mds_available_ds (lseg) && !ds_fatal_error)return PNFS_TRY_AGAIN ; -if (ff_layout_no_fallback_to_mds (lseg)) { -/* -* FF_FLAGS_NO_IO_THRU_MDS: force fresh LAYOUTGET, -* never fall through to MDS I/O. -*/ +/* +* FF_FLAGS_NO_IO_THRU_MDS forbids MDS I/O, and +* pnfs_write_through_mds() cannot switch a mirrored descriptor. +* Return the layout and retry with a fresh LAYOUTGET instead; a +* mirrored write reaches the MDS only if the server then has no +* layout to give, or pg_init cannot prepare the DS. +*/ +if (ff_layout_no_fallback_to_mds (lseg) || + (FF_LAYOUT_MIRROR_COUNT (lseg) >1 && !ds_fatal_error)) {pnfs_error_mark_layout_for_return (hdr->inode, lseg, &mirror->dss[dss_id].devid);return PNFS_TRY_AGAIN ; } +/* +* A fatal error preparing the DS is no evidence against it; pg_init +* of the retry prepares it again and falls back to the MDS if that +* fails too. +*/ +if (FF_LAYOUT_MIRROR_COUNT (lseg) >1 ) +return PNFS_TRY_AGAIN ;trace_pnfs_mds_fallback_write_pagelist (hdr->inode, hdr->args.offset, hdr->args.count,IOMODE_RW ,NFS_I (hdr->inode)->layout, lseg);
Signature · 1 line
2.53.0