pNFS: fix tasks hung forever on NFS_LAYOUT_DRAIN
Subsystems (2)
Olga Kornievskaia
reviewercover pNFS: fix tasks hung forever on NFS_LAYOUT_DRAIN
Tasks hang indefinitely in pnfs_update_layout() waiting on NFS_LAYOUT_DRAIN, with the layout header left in
plh_flags = 0x640 = HASHED | INVALID_STID | DRAIN
plh_outstanding = 0
NFS_LAYOUT_DRAIN is cleared in exactly one place, nfs_layoutget_end(), and only when the last outstanding LAYOUTGET completes. With plh_outstanding already at zero there is no LAYOUTGET left that could run it, so the bit stays set and the waiter is never woken.
This was seen in the field on a filelayout mount against a NetApp server, on a kernel that already carries the two previous fixes for the same end state (38e55db5c06c and 5248d8474e59), so there is a third path into it.
Three actors are involved, all of them writes to the same file, but at three different stages of the write path.
The starting state is a layout that an earlier DS error or CB_LAYOUTRECALL has already invalidated: the segments are gone, plh_flags has INVALID_STID and DRAIN, and plh_outstanding is 0. DRAIN being set here is harmless on its own, since the next LAYOUTGET will clear it.
- B is a write that needs a layout before it can place its pages. It finds no segment and the stateid invalid, so it claims FIRST_LAYOUTGET, calls nfs_layoutget_begin() - taking plh_outstanding from 0 to 1 - and sends a LAYOUTGET.
- A, the victim, is a second write, flushing a page out of write_cache_pages() and asking pnfs_update_layout() which data server to send it to. It sees DRAIN set and plh_outstanding != 0, concludes that B's LAYOUTGET is being forgotten and that it should wait for the drain, drops i_lock and heads for wait_on_bit(). It is still holding the page lock taken by write_cache_pages().
- B's LAYOUTGET reply arrives. pnfs_layout_process() accepts it - DRAIN would normally make it forget the reply, but a first layoutget is exempt - and pnfs_set_layout_stateid() clears INVALID_STID. Returning up the stack, nfs_layoutget_end() drops plh_outstanding to 0, clears DRAIN and FIRST_LAYOUTGET, and wakes. Nobody is on the wait queue yet: A has not reached its test_bit().
- C is a third write, already past the layout lookup and out on the wire. Its WRITE to a data server comes back with an error that means the layout is no longer usable - ESTALE, EBADHANDLE, NFS4ERR_PNFS_NO_LAYOUT and friends - and filelayout_async_handle_error() calls pnfs_destroy_layout(), which sets INVALID_STID and DRAIN again, now with plh_outstanding == 0. (C need not be a separate task; it can be B itself, later, writing with the layout it just fetched.)
- A finally evaluates the bit in wait_on_bit(), sees DRAIN set, and sleeps forever.
Patch 1 is the fix. Patch 2 is independent of it and only adds
tracing: the DRAIN branch of pnfs_update_layout() currently emits no tracepoint at all, and the pnfs_update_layout event reports neither plh_flags nor plh_outstanding, which are precisely the two values that decide whether a task enters that wait and when it leaves. Working out why a task is parked there means reading them out of a crash dump.
Olga Kornievskaia (2):
pNFS: don't set NFS_LAYOUT_DRAIN when there is nothing to drain
pNFS: trace the NFS_LAYOUT_DRAIN wait in pnfs_update_layout()
fs/nfs/callback_proc.c | 2 +- fs/nfs/nfs4trace.h | 42 +++++++++++++++++++++++++++++++++++++++++- fs/nfs/pnfs.c | 36 +++++++++++++++++++++++++++++++++++- fs/nfs/pnfs.h | 1 + include/linux/nfs4.h | 1 + 5 files changed, 79 insertions(+), 3 deletions(-)
Signature · 1 line
2.55.0
Olga Kornievskaia
reviewerPATCH 1/2 pNFS: don't set NFS_LAYOUT_DRAIN when there is nothing to drain
A task can get stuck indefinitely in pnfs_update_layout() waiting on NFS_LAYOUT_DRAIN, with the layout header showing plh_flags = 0x640 (NFS_LAYOUT_INVALID_STID | NFS_LAYOUT_HASHED | NFS_LAYOUT_DRAIN) and plh_outstanding = 0.
NFS_LAYOUT_DRAIN is only ever cleared by nfs_layoutget_end(), when the last outstanding LAYOUTGET completes. Setting it once plh_outstanding has already reached zero leaves it set with nothing left that could clear it.
Commit 5248d8474e59 ("pNFS: fix a missing wake up while waiting on NFS_LAYOUT_DRAIN") addressed one instance of this by making pnfs_mark_layout_stateid_invalid() a no-op when NFS_LAYOUT_INVALID_STID was already set, so the bit could not be set twice for an already invalid layout. That guard is lifted as soon as a LAYOUTGET succeeds:pnfs_layout_process() calls pnfs_set_layout_stateid(), which clears NFS_LAYOUT_INVALID_STID, and it does so before pnfs_update_layout() calls nfs_layoutget_end(). The original race then reopens:
1. cpu a: waiter tests NFS_LAYOUT_DRAIN (1) and plh_outstanding (1)
2. cpu b: pnfs_layout_process() clears NFS_LAYOUT_INVALID_STID, then nfs_layoutget_end() clears NFS_LAYOUT_DRAIN and wakes
3. cpu c: pnfs_mark_layout_stateid_invalid() sets NFS_LAYOUT_DRAIN
again, now with plh_outstanding == 0
4. cpu a: calls wait_on_bit() and sleeps forever
In the vmcore this was found in, cpu c was a data server I/O completing with an "invalidate layout" error, reaching pnfs_mark_layout_stateid_invalid() through filelayout_async_handle_error() -> pnfs_destroy_layout(). The surviving layout segment had pls_seq == 1, matching both plh_stateid.seqid and plh_barrier, showing a LAYOUTGET had completed successfully and cleared NFS_LAYOUT_INVALID_STID before it was set again.
Rather than extending the NFS_LAYOUT_INVALID_STID guard, enforce the invariant the bit relies on: only set NFS_LAYOUT_DRAIN when there is an outstanding LAYOUTGET to drain. Callers hold inode->i_lock, which stops plh_outstanding from growing, but nfs_layoutget_end() runs without it and may be dropping the last reference concurrently, so re-check plh_outstanding after setting the bit and clear it again if that LAYOUTGET got there first.
initiate_file_draining() sets the bit under the same lock with the same constraint, so convert it as well.
--- fs/nfs/callback_proc.c | 2 +- fs/nfs/pnfs.c | 34 +++++++++++++++++++++++++++++++++- fs/nfs/pnfs.h | 1 + 3 files changed, 35 insertions(+), 2 deletions(-)
fs/nfs/callback_proc.c +1 -1
@@ -299,7 +299,7 @@ static u32 initiate_file_draining(struct nfs_client *clp, rv =NFS4_OK ;break ;case -ENOENT : -set_bit (NFS_LAYOUT_DRAIN , &lo->plh_flags); +pnfs_set_layout_drain (lo);/* Embrace your forgetfulness! */ rv =NFS4ERR_NOMATCHING_LAYOUT ;
fs/nfs/pnfs.c +33 -1
@@ -446,6 +446,38 @@ bool nfs4_layout_refresh_old_stateid(nfs4_stateid *dst,return ret; } +/* +* Ask any LAYOUTGETs that are still in flight to be forgotten, and make +* new callers of pnfs_update_layout() wait for them. +* +* NFS_LAYOUT_DRAIN is only ever cleared by nfs_layoutget_end(), when the +* last outstanding LAYOUTGET completes. Setting it once plh_outstanding +* has already reached zero would leave it set with nothing left that +* could clear it, and a waiter that reaches wait_on_bit() would never be +* woken again. +* +* Callers hold the inode->i_lock, so plh_outstanding cannot grow here, +* but nfs_layoutget_end() runs without it and may be dropping the last +* reference concurrently. Re-check once the bit is visible, and clear it +* again if that LAYOUTGET got there first. +* +* Note that caller must hold inode->i_lock. +*/ +void pnfs_set_layout_drain (struct pnfs_layout_hdr *lo) +{ +if (atomic_read (&lo->plh_outstanding) ==0 ) +return ; +set_bit (NFS_LAYOUT_DRAIN , &lo->plh_flags); +/* Pairs with the atomic_dec_and_test() in nfs_layoutget_end() */ +smp_mb__after_atomic (); +if (atomic_read (&lo->plh_outstanding) ==0 && +test_and_clear_bit (NFS_LAYOUT_DRAIN , &lo->plh_flags)) { +/* Order the clear against the waitqueue check in wake_up_bit() */ +smp_mb__after_atomic (); +wake_up_bit (&lo->plh_flags,NFS_LAYOUT_DRAIN ); + } +} +/* * Mark a pnfs_layout_hdr and all associated layout segments as invalid *
@@ -471,7 +503,7 @@ pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo,pnfs_clear_lseg_state (lseg, lseg_list);pnfs_clear_layoutreturn_info (lo);pnfs_free_returned_lsegs (lo, lseg_list, &range,0 ); -set_bit (NFS_LAYOUT_DRAIN , &lo->plh_flags); +pnfs_set_layout_drain (lo);if (test_bit (NFS_LAYOUT_RETURN , &lo->plh_flags) && !test_and_set_bit (NFS_LAYOUT_RETURN_LOCK , &lo->plh_flags))pnfs_clear_layoutreturn_waitbit (lo);
fs/nfs/pnfs.h +1 -0
@@ -325,6 +325,7 @@ int pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo, const struct nfs4_deviceid *devid);int pnfs_mark_layout_stateid_invalid (struct pnfs_layout_hdr *lo,struct list_head *lseg_list); +void pnfs_set_layout_drain (struct pnfs_layout_hdr *lo);bool pnfs_roc (struct inode *ino,struct nfs4_layoutreturn_args *args,struct nfs4_layoutreturn_res *res,const struct cred *cred,bool sync);
Signature · 1 line
2.55.0
Olga Kornievskaia
reviewerPATCH 2/2 pNFS: trace the NFS_LAYOUT_DRAIN wait in pnfs_update_layout()
In reply to Olga Kornievskaiapnfs_update_layout() can park a task on NFS_LAYOUT_DRAIN until the last outstanding LAYOUTGET completes, but that branch emits no tracepoint, so there is no record of a task entering the wait. The pnfs_update_layout trace event also reports neither plh_flags nor plh_outstanding, which are the two values that decide whether the wait is entered and when it ends. Working out why a task is stuck there currently means reading plh_flags and plh_outstanding out of a crash dump.
Add a PNFS_UPDATE_LAYOUT_DRAIN reason and emit it before wait_on_bit(), and report plh_flags and plh_outstanding from the trace event.
--- fs/nfs/nfs4trace.h | 42 +++++++++++++++++++++++++++++++++++++++++- fs/nfs/pnfs.c | 2 ++ include/linux/nfs4.h | 1 + 3 files changed, 44 insertions(+), 1 deletion(-)
fs/nfs/nfs4trace.h +41 -1
@@ -1982,11 +1982,38 @@ TRACE_DEFINE_ENUM(PNFS_UPDATE_LAYOUT_EXIT); { PNFS_UPDATE_LAYOUT_FOUND_CACHED, "found cached" }, \ { PNFS_UPDATE_LAYOUT_RETURN, "layoutreturn" }, \ { PNFS_UPDATE_LAYOUT_BLOCKED, "layouts blocked" }, \ + { PNFS_UPDATE_LAYOUT_DRAIN, "draining" }, \ { PNFS_UPDATE_LAYOUT_INVALID_OPEN, "invalid open" }, \ { PNFS_UPDATE_LAYOUT_RETRY, "retrying" }, \ { PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET, "sent layoutget" }, \ { PNFS_UPDATE_LAYOUT_EXIT, "exit" }) +TRACE_DEFINE_ENUM (NFS_LAYOUT_RO_FAILED ); +TRACE_DEFINE_ENUM (NFS_LAYOUT_RW_FAILED ); +TRACE_DEFINE_ENUM (NFS_LAYOUT_BULK_RECALL ); +TRACE_DEFINE_ENUM (NFS_LAYOUT_RETURN ); +TRACE_DEFINE_ENUM (NFS_LAYOUT_RETURN_LOCK ); +TRACE_DEFINE_ENUM (NFS_LAYOUT_RETURN_REQUESTED ); +TRACE_DEFINE_ENUM (NFS_LAYOUT_INVALID_STID ); +TRACE_DEFINE_ENUM (NFS_LAYOUT_FIRST_LAYOUTGET ); +TRACE_DEFINE_ENUM (NFS_LAYOUT_INODE_FREEING ); +TRACE_DEFINE_ENUM (NFS_LAYOUT_HASHED ); +TRACE_DEFINE_ENUM (NFS_LAYOUT_DRAIN ); + +#define show_pnfs_layout_flags (flags) \ + __print_flags(flags, "|", \ + { BIT(NFS_LAYOUT_RO_FAILED), "RO_FAILED" }, \ + { BIT(NFS_LAYOUT_RW_FAILED), "RW_FAILED" }, \ + { BIT(NFS_LAYOUT_BULK_RECALL), "BULK_RECALL" }, \ + { BIT(NFS_LAYOUT_RETURN), "RETURN" }, \ + { BIT(NFS_LAYOUT_RETURN_LOCK), "RETURN_LOCK" }, \ + { BIT(NFS_LAYOUT_RETURN_REQUESTED), "RETURN_REQUESTED" }, \ + { BIT(NFS_LAYOUT_INVALID_STID), "INVALID_STID" }, \ + { BIT(NFS_LAYOUT_FIRST_LAYOUTGET), "FIRST_LAYOUTGET" }, \ + { BIT(NFS_LAYOUT_INODE_FREEING), "INODE_FREEING" }, \ + { BIT(NFS_LAYOUT_HASHED), "HASHED" }, \ + { BIT(NFS_LAYOUT_DRAIN), "DRAIN" }) + TRACE_EVENT(pnfs_update_layout, TP_PROTO(struct inode *inode, loff_t pos,
@@ -2006,6 +2033,8 @@ TRACE_EVENT(pnfs_update_layout, __field(enum pnfs_iomode, iomode) __field(int, layoutstateid_seq) __field(u32, layoutstateid_hash) + __field(unsigned long, layoutflags) + __field(int, outstanding) __field(long, lseg) __field(enum pnfs_update_layout_reason, reason) ),
@@ -2017,6 +2046,14 @@ TRACE_EVENT(pnfs_update_layout, __entry->count = count; __entry->iomode = iomode; __entry->reason = reason; +if (lo !=NULL ) { + __entry->layoutflags = lo->plh_flags; + __entry->outstanding = +atomic_read (&lo->plh_outstanding); + }else { + __entry->layoutflags =0 ; + __entry->outstanding =0 ; + }if (lo !=NULL &&pnfs_layout_is_valid (lo)) { __entry->layoutstateid_seq =be32_to_cpu (lo->plh_stateid.seqid);
@@ -2031,7 +2068,8 @@ TRACE_EVENT(pnfs_update_layout, TP_printk( "fileid=%02x:%02x:%llu fhandle=0x%08x " "iomode=%s pos=%llu count=%llu " - "layoutstateid=%d:0x%08x lseg=0x%lx (%s)", + "layoutstateid=%d:0x%08x layoutflags=%s " + "outstanding=%d lseg=0x%lx (%s)", MAJOR(__entry->dev), MINOR(__entry->dev), (unsigned long long)__entry->fileid, __entry->fhandle,
@@ -2039,6 +2077,8 @@ TRACE_EVENT(pnfs_update_layout, (unsigned long long )__entry->pos, (unsigned long long )__entry->count, __entry->layoutstateid_seq, __entry->layoutstateid_hash, +show_pnfs_layout_flags (__entry->layoutflags), + __entry->outstanding, __entry->lseg,show_pnfs_update_layout_reason (__entry->reason) )
fs/nfs/pnfs.c +2 -0
@@ -2261,6 +2261,8 @@ pnfs_update_layout(struct inode *ino,*/ if (test_bit (NFS_LAYOUT_DRAIN , &lo->plh_flags) &&atomic_read (&lo->plh_outstanding) !=0 ) { +trace_pnfs_update_layout (ino, pos, count, iomode, lo, lseg, +PNFS_UPDATE_LAYOUT_DRAIN );spin_unlock (&ino->i_lock); lseg =ERR_PTR (wait_on_bit (&lo->plh_flags,NFS_LAYOUT_DRAIN ,TASK_KILLABLE ));
include/linux/nfs4.h +1 -0
@@ -712,6 +712,7 @@ enum pnfs_update_layout_reason {PNFS_UPDATE_LAYOUT_RETURN ,PNFS_UPDATE_LAYOUT_RETRY ,PNFS_UPDATE_LAYOUT_BLOCKED , +PNFS_UPDATE_LAYOUT_DRAIN ,PNFS_UPDATE_LAYOUT_INVALID_OPEN ,PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET , PNFS_UPDATE_LAYOUT_EXIT,
Signature · 1 line
2.55.0