Skip to contentSkip to conversation

pNFS/flexfiles: read the layout hdr under i_lock in pg_get_mirror_count_write

PATCH New Benjamin Coddington 1 participant 1 message Nov 19

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

PATCH 1/1 pNFS/flexfiles: read the layout hdr under i_lock in pg_get_mirror_count_write

From: Benjamin Coddington <[email protected]>

When a write has no layout segment, ff_layout_pg_get_mirror_count_write() reads NFS_I(inode)->layout twice to test the hdr's NO_IO_THRU_MDS bit, without i_lock and without a reference. No lseg pins the hdr at that point, so pnfs_put_layout_hdr() can detach it under i_lock and free it in between: the bit test then reads freed memory, or the second read finds NULL and the bit test dereferences it.

Read the pointer once under i_lock and test the bit before dropping it. The hdr is detached under i_lock before it is freed, so a pointer found there is still valid.

Fixes 1d62e659c0bf ("NFSv4/flexfiles: honor FF_FLAGS_NO_IO_THRU_MDS in pg_get_mirror_count_write")
---
 fs/nfs/flexfilelayout/flexfilelayout.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
fs/nfs/flexfilelayout/flexfilelayout.c +11 -2
index 5c289737366f..a797a2a8e688 100644
@@ -1211,6 +1211,10 @@ static unsigned int
 ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio,
 				    struct nfs_page *req)
 {
+	struct inode *inode = pgio->pg_inode;
+	struct pnfs_layout_hdr *lo;
+	bool no_fallback;
+
 	if (!pgio->pg_lseg) {
 		pgio->pg_lseg =
 			pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
@@ -1229,8 +1233,13 @@ ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio,
 			0, NFS4_MAX_UINT64, IOMODE_RW,
 			NFS_I(pgio->pg_inode)->layout,
 			pgio->pg_lseg);
-	if (NFS_I(pgio->pg_inode)->layout &&
-	    ff_layout_hdr_no_fallback_to_mds(NFS_I(pgio->pg_inode)->layout)) {
+
+	/* No lseg pins the layout hdr, and it is detached under i_lock */
+	spin_lock(&inode->i_lock);
+	lo = NFS_I(inode)->layout;
+	no_fallback = lo && ff_layout_hdr_no_fallback_to_mds(lo);
+	spin_unlock(&inode->i_lock);
+	if (no_fallback) {
 		/*
 		 * FF_FLAGS_NO_IO_THRU_MDS: no current lseg but the server's
 		 * policy forbids MDS fallback.  Surface -EAGAIN so writeback
Signature · 1 line
2.53.0