Skip to contentSkip to conversation

NFSv4/pNFS: Clear NFS_INO_LAYOUTCOMMIT in pnfs_mark_layout_stateid_invalid

PATCH New Jonathan Curley 1 participant 2 messages Nov 12, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover NFSv4/pNFS: Clear NFS_INO_LAYOUTCOMMIT in pnfs_mark_layout_stateid_invalid

This diff fixes a segfault in pnfs_layoutcommit_inode when nfsi->layout is null during the core critical section. My initial fix was to check for null here:

if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))

       goto out_unlock;

This fix worked but I suspected it wouldn't pass code review since it doesn't explain how we got in this inconsistent state.

Around the time I was looking at this I also saw Haihua Yang's diff for ensuring layout commit happens before layout return. If such an invariant could be established then it seemed like it would also fix this issue. When I tested that strategy with stress tests it resulted in deadlocks. Adding Haihua to the cc in case they're seeing a similar issue and may find this diff helpful.

Over time I studied the history of how the NFS_INO_LAYOUTCOMMIT, NFS_LSEG_LAYOUTCOMMIT and NFS_INO_LAYOUTCOMMITTING work in conjunction with layout and lseg reference counting to ensure layouts and lsegs stick around for the commit operation.

The next fix I tried was to hold a reference on the layout in addition to the lseg during set_layoutcommit. This worked but building a complete fix was looking complicated. It was later noted that lsegs hold a reference to the layout as well, so such logic appeared to be redundant. At that point I searched for a place where lseg reference gets removed without also clearing NFS_INO_LAYOUTCOMMIT and I stumbled upon this path. With this fix the crash no longer happens during stress tests.

I added a fixes label since it seems to be introduced from the referenced patch.

Jonathan Curley (1):
NFSv4/pNFS: Clear NFS_INO_LAYOUTCOMMIT in
pnfs_mark_layout_stateid_invalid

 fs/nfs/pnfs.c | 1 +
 1 file changed, 1 insertion(+)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/1 NFSv4/pNFS: Clear NFS_INO_LAYOUTCOMMIT in pnfs_mark_layout_stateid_invalid

Fixes a crash when layout is null during this call stack:

write_inode

    -> nfs4_write_inode
        -> pnfs_layoutcommit_inode

pnfs_set_layoutcommit relies on the lseg refcount to keep the layout around. Need to clear NFS_INO_LAYOUTCOMMIT otherwise we might attempt to reference a null layout.

Fixes fe1cf9469d7bc ("pNFS: Clear all layout segment state in pnfs_mark_layout_stateid_invalid")
---
 fs/nfs/pnfs.c | 1 +
 1 file changed, 1 insertion(+)
fs/nfs/pnfs.c +1 -0
index 5f582713bf05..358b04ec5867 100644
@@ -466,6 +466,7 @@ pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo,
 	struct pnfs_layout_segment *lseg, *next;
 
 	set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
+	clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(lo->plh_inode)->flags);
 	list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
 		pnfs_clear_lseg_state(lseg, lseg_list);
 	pnfs_clear_layoutreturn_info(lo);
Signature · 1 line
2.34.1