Skip to contentSkip to conversation

NFSD: More minor XDR restructuring

PATCH In review Chuck Lever 2 participants 8 messages Nov 19 – Nov 20

Subsystems (3)
lore
Share & tools
lore

Chuck Lever

author maintainer
#0 lore raw

cover NFSD: More minor XDR restructuring

The main change in this series replaces the use of the svc_rqst's rq_arg and rq_arg_stream fields with local stack variables, but as usual, agentic review discovered a number of pre-existing issues in this area that needed attention.

---
Chuck Lever (4):
NFSD: Reject layoutupdate extent counts that wrap the length check
NFSD: Allocate the layoutupdate extent array with kvzalloc_objs()
NFSD: Decode block and SCSI layoutupdates from a local xdr_stream
NFSD: Remove the svc_rqst argument from proc_layoutcommit

 fs/nfsd/blocklayout.c    | 32 +++++++++++++++++++-------------
 fs/nfsd/blocklayoutxdr.c | 24 ++++++++++++------------
 fs/nfsd/flexfilelayout.c |  4 ++--
 fs/nfsd/nfs4proc.c       |  2 +-
 fs/nfsd/pnfs.h           |  3 +--
 5 files changed, 35 insertions(+), 30 deletions(-)

---
base-commit: 8489270d1cb9568fc88f3cda4626a19807300d77
change-id: 20261119-blocklayout-decode-stream-f38f4e015555

Best regards,

Signature · 1 line
Chuck Lever <[email protected]>
author maintainer
#1 lore raw

PATCH 1/4 NFSD: Reject layoutupdate extent counts that wrap the length check

nfsd4_block_decode_layoutupdate() and nfsd4_scsi_decode_layoutupdate() validate the extent count in a layoutupdate4 body by multiplying the count by the size of one extent and comparing the product to the length of the body. The product is stored in a u32, so a count of 0x40000000 block extents or 0x10000000 SCSI ranges wraps to zero and matches a body that carries no extents at all. Both decoders then pass the count to kzalloc_objs(). The request exceeds KMALLOC_MAX_SIZE, so the allocation fails with a warning. NFSD then returns NFS4ERR_DELAY for a LAYOUTCOMMIT it should reject with NFS4ERR_BADXDR.

Fixes f963cf2b91a3 ("NFSD: Implement large extent array support in pNFS")
Fixes f99d4fbdae67 ("nfsd: add SCSI layout support")
---
 fs/nfsd/blocklayoutxdr.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
fs/nfsd/blocklayoutxdr.c +8 -8
index a6589f5c878a..ea9a3f04a560 100644
@@ -164,15 +164,15 @@ nfsd4_block_decode_layoutupdate(struct xdr_stream *xdr, struct iomap **iomapp,
 		int *nr_iomapsp, u32 block_size)
 {
 	struct iomap *iomaps;
-	u32 nr_iomaps, expected, len, i;
+	u32 nr_iomaps, len, i;
 	__be32 nfserr;
 
 	if (xdr_stream_decode_u32(xdr, &nr_iomaps))
 		return nfserr_bad_xdr;
 
-	len = sizeof(__be32) + xdr_stream_remaining(xdr);
-	expected = sizeof(__be32) + nr_iomaps * PNFS_BLOCK_EXTENT_SIZE;
-	if (len != expected)
+	len = xdr_stream_remaining(xdr);
+	if (len % PNFS_BLOCK_EXTENT_SIZE ||
+	    nr_iomaps != len / PNFS_BLOCK_EXTENT_SIZE)
 		return nfserr_bad_xdr;
 
 	iomaps = kzalloc_objs(*iomaps, nr_iomaps);
@@ -258,15 +258,15 @@ nfsd4_scsi_decode_layoutupdate(struct xdr_stream *xdr, struct iomap **iomapp,
 		int *nr_iomapsp, u32 block_size)
 {
 	struct iomap *iomaps;
-	u32 nr_iomaps, expected, len, i;
+	u32 nr_iomaps, len, i;
 	__be32 nfserr;
 
 	if (xdr_stream_decode_u32(xdr, &nr_iomaps))
 		return nfserr_bad_xdr;
 
-	len = sizeof(__be32) + xdr_stream_remaining(xdr);
-	expected = sizeof(__be32) + nr_iomaps * PNFS_SCSI_RANGE_SIZE;
-	if (len != expected)
+	len = xdr_stream_remaining(xdr);
+	if (len % PNFS_SCSI_RANGE_SIZE ||
+	    nr_iomaps != len / PNFS_SCSI_RANGE_SIZE)
 		return nfserr_bad_xdr;
 
 	iomaps = kzalloc_objs(*iomaps, nr_iomaps);
Signature · 1 line
2.56.0
author maintainer
#3 lore raw

PATCH 2/4 NFSD: Allocate the layoutupdate extent array with kvzalloc_objs()

In reply to Chuck Lever

The block and SCSI layoutupdate decoders allocate one struct iomap per extent with kzalloc_objs(). A struct iomap is larger than a 44-byte block extent or a 16-byte SCSI range, so a lou_body that fits in the maximum request size can still need an array larger than KMALLOC_MAX_SIZE. kmalloc() then warns that the request exceeds KMALLOC_MAX_SIZE, and the decoder returns nfserr_delay. A client that retries after NFS4ERR_DELAY resends a LAYOUTCOMMIT that NFSD can never decode. Found by inspection.

The session's ca_maxrequestsize still bounds the size of the array.

Fixes 8650b8a05850 ("nfsd: pNFS block layout driver")
Fixes f99d4fbdae67 ("nfsd: add SCSI layout support")
---
 fs/nfsd/blocklayout.c    | 2 +-
 fs/nfsd/blocklayoutxdr.c | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
fs/nfsd/blocklayout.c +1 -1
index b3bb2831cff3..06564f247795 100644
@@ -228,7 +228,7 @@ nfsd4_block_commit_blocks(struct inode *inode, struct nfsd4_layoutcommit *lcp,
 	error = inode->i_sb->s_export_op->block_ops->commit_blocks(inode,
 			iomaps, nr_iomaps,
 			lcp->lc_size_chg ? lcp->lc_newsize : 0);
-	kfree(iomaps);
+	kvfree(iomaps);
 	return nfserrno(error);
 }
 
fs/nfsd/blocklayoutxdr.c +4 -4
index ea9a3f04a560..90ccc07801ca 100644
@@ -175,7 +175,7 @@ nfsd4_block_decode_layoutupdate(struct xdr_stream *xdr, struct iomap **iomapp,
 	    nr_iomaps != len / PNFS_BLOCK_EXTENT_SIZE)
 		return nfserr_bad_xdr;
 
-	iomaps = kzalloc_objs(*iomaps, nr_iomaps);
+	iomaps = kvzalloc_objs(*iomaps, nr_iomaps);
 	if (!iomaps)
 		return nfserr_delay;
 
@@ -231,7 +231,7 @@ nfsd4_block_decode_layoutupdate(struct xdr_stream *xdr, struct iomap **iomapp,
 	*nr_iomapsp = nr_iomaps;
 	return nfs_ok;
 fail:
-	kfree(iomaps);
+	kvfree(iomaps);
 	return nfserr;
 }
 
@@ -269,7 +269,7 @@ nfsd4_scsi_decode_layoutupdate(struct xdr_stream *xdr, struct iomap **iomapp,
 	    nr_iomaps != len / PNFS_SCSI_RANGE_SIZE)
 		return nfserr_bad_xdr;
 
-	iomaps = kzalloc_objs(*iomaps, nr_iomaps);
+	iomaps = kvzalloc_objs(*iomaps, nr_iomaps);
 	if (!iomaps)
 		return nfserr_delay;
 
@@ -301,6 +301,6 @@ nfsd4_scsi_decode_layoutupdate(struct xdr_stream *xdr, struct iomap **iomapp,
 	*nr_iomapsp = nr_iomaps;
 	return nfs_ok;
 fail:
-	kfree(iomaps);
+	kvfree(iomaps);
 	return nfserr;
 }
Signature · 1 line
2.56.0
author maintainer
#5 lore raw

PATCH 3/4 NFSD: Decode block and SCSI layoutupdates from a local xdr_stream

In reply to Chuck Lever

nfsd4_decode_layoutupdate4() leaves the body of a layoutupdate4 undecoded in lc_up_layout. nfsd4_block_proc_layoutcommit() and nfsd4_scsi_proc_layoutcommit() decode that body during the pc_func step by overwriting rqstp->rq_arg and re-initializing rq_arg_stream. Nothing reads those fields after pc_decode today, but the svc_rqst should be free to keep state in them until the request completes.

Decode the layoutupdate4 body from an xdr_stream and scratch buffer on the stack instead, leaving rq_arg and rq_arg_stream untouched. Decoding layoutupdate4 during the pc_decode step would be a better design, but that larger change is outside the scope of this patch.

---
 fs/nfsd/blocklayout.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
fs/nfsd/blocklayout.c +14 -8
index 06564f247795..c3b614b25278 100644
@@ -269,15 +269,18 @@ static __be32
 nfsd4_block_proc_layoutcommit(struct inode *inode, struct svc_rqst *rqstp,
 		struct nfsd4_layoutcommit *lcp)
 {
+	__be32 scratch[XDR_QUADLEN(NFS4_DEVICEID4_SIZE)];
+	struct xdr_stream xdr;
 	struct iomap *iomaps;
 	int nr_iomaps;
 	__be32 nfserr;
 
-	rqstp->rq_arg = lcp->lc_up_layout;
-	svcxdr_init_decode(rqstp);
+	xdr_init_decode(&xdr, &lcp->lc_up_layout, NULL, NULL);
+	xdr_set_scratch_buffer(&xdr, scratch, sizeof(scratch));
 
-	nfserr = nfsd4_block_decode_layoutupdate(&rqstp->rq_arg_stream,
-			&iomaps, &nr_iomaps, i_blocksize(inode));
+	nfserr = nfsd4_block_decode_layoutupdate(&xdr, &iomaps, &nr_iomaps,
+						 i_blocksize(inode));
+	xdr_finish_decode(&xdr);
 	if (nfserr != nfs_ok)
 		return nfserr;
 
@@ -461,15 +464,18 @@ static __be32
 nfsd4_scsi_proc_layoutcommit(struct inode *inode, struct svc_rqst *rqstp,
 		struct nfsd4_layoutcommit *lcp)
 {
+	__be32 scratch[XDR_QUADLEN(NFS4_DEVICEID4_SIZE)];
+	struct xdr_stream xdr;
 	struct iomap *iomaps;
 	int nr_iomaps;
 	__be32 nfserr;
 
-	rqstp->rq_arg = lcp->lc_up_layout;
-	svcxdr_init_decode(rqstp);
+	xdr_init_decode(&xdr, &lcp->lc_up_layout, NULL, NULL);
+	xdr_set_scratch_buffer(&xdr, scratch, sizeof(scratch));
 
-	nfserr = nfsd4_scsi_decode_layoutupdate(&rqstp->rq_arg_stream,
-			&iomaps, &nr_iomaps, i_blocksize(inode));
+	nfserr = nfsd4_scsi_decode_layoutupdate(&xdr, &iomaps, &nr_iomaps,
+						i_blocksize(inode));
+	xdr_finish_decode(&xdr);
 	if (nfserr != nfs_ok)
 		return nfserr;
 
Signature · 1 line
2.56.0
#6 lore raw

Re: [PATCH 3/4] NFSD: Decode block and SCSI layoutupdates from a local xdr_stream

fs/nfsd/blocklayout.c static __be32 · line 282

Quoted 5 lines · Chuck Lever
fs/nfsd/blocklayout.c +2 -2
-	nfserr = nfsd4_block_decode_layoutupdate(&rqstp->rq_arg_stream,
-			&iomaps, &nr_iomaps, i_blocksize(inode));
+	nfserr = nfsd4_block_decode_layoutupdate(&xdr, &iomaps, &nr_iomaps,
+						 i_blocksize(inode));

Can we please keep the sane tab-based alignment instead of the stupid after pening brace one that creates nothing but constant churn for every change in the function signature while also massively reducing readability?

The rest looks good.

author maintainer
#7 lore raw

PATCH 4/4 NFSD: Remove the svc_rqst argument from proc_layoutcommit

In reply to Chuck Lever

Commit f963cf2b91a3 ("NFSD: Implement large extent array support in pNFS") added an svc_rqst argument to the proc_layoutcommit layout operation so the block and SCSI layout drivers could decode the layoutupdate4 body with rq_arg_stream. Those drivers decode from a local xdr_stream, and no proc_layoutcommit implementation reads the svc_rqst. Leaving the argument in place invites a layout driver to borrow rq_arg again.

Remove the svc_rqst argument from the operation and from its three implementations.

---
 fs/nfsd/blocklayout.c    | 8 ++++----
 fs/nfsd/flexfilelayout.c | 4 ++--
 fs/nfsd/nfs4proc.c       | 2 +-
 fs/nfsd/pnfs.h           | 3 +--
 4 files changed, 8 insertions(+), 9 deletions(-)
fs/nfsd/blocklayout.c +4 -4
index c3b614b25278..23f2365787eb 100644
@@ -266,8 +266,8 @@ nfsd4_block_proc_getdeviceinfo(struct super_block *sb,
 }
 
 static __be32
-nfsd4_block_proc_layoutcommit(struct inode *inode, struct svc_rqst *rqstp,
-		struct nfsd4_layoutcommit *lcp)
+nfsd4_block_proc_layoutcommit(struct inode *inode,
+			      struct nfsd4_layoutcommit *lcp)
 {
 	__be32 scratch[XDR_QUADLEN(NFS4_DEVICEID4_SIZE)];
 	struct xdr_stream xdr;
@@ -461,8 +461,8 @@ nfsd4_scsi_proc_getdeviceinfo(struct super_block *sb,
 	return nfserrno(nfsd4_block_get_device_info_scsi(sb, clp, gdp));
 }
 static __be32
-nfsd4_scsi_proc_layoutcommit(struct inode *inode, struct svc_rqst *rqstp,
-		struct nfsd4_layoutcommit *lcp)
+nfsd4_scsi_proc_layoutcommit(struct inode *inode,
+			     struct nfsd4_layoutcommit *lcp)
 {
 	__be32 scratch[XDR_QUADLEN(NFS4_DEVICEID4_SIZE)];
 	struct xdr_stream xdr;
fs/nfsd/flexfilelayout.c +2 -2
index 0deb913493a3..c97caeec4024 100644
@@ -130,8 +130,8 @@ nfsd4_ff_proc_getdeviceinfo(struct super_block *sb, struct svc_rqst *rqstp,
 }
 
 static __be32
-nfsd4_ff_proc_layoutcommit(struct inode *inode, struct svc_rqst *rqstp,
-		struct nfsd4_layoutcommit *lcp)
+nfsd4_ff_proc_layoutcommit(struct inode *inode,
+			   struct nfsd4_layoutcommit *lcp)
 {
 	return nfs_ok;
 }
fs/nfsd/nfs4proc.c +1 -1
index 71d6a473c1bc..2f25acb1ac92 100644
@@ -3119,7 +3119,7 @@ nfsd4_layoutcommit(struct svc_rqst *rqstp,
 		mutex_unlock(&ls->ls_mutex);
 	}
 
-	nfserr = ops->proc_layoutcommit(inode, rqstp, lcp);
+	nfserr = ops->proc_layoutcommit(inode, lcp);
 
 	if (!lcp->lc_reclaim) {
 		nfsd4_file_mark_deleg_written(ls->ls_stid.sc_file);
fs/nfsd/pnfs.h +1 -2
index e5f2e11c880a..a3d558ccdbdf 100644
@@ -38,8 +38,7 @@ struct nfsd4_layout_ops {
 			const struct nfsd4_layoutget *lgp);
 
 	__be32 (*proc_layoutcommit)(struct inode *inode,
-			struct svc_rqst *rqstp,
-			struct nfsd4_layoutcommit *lcp);
+				    struct nfsd4_layoutcommit *lcp);
 
 	bool (*fence_client)(struct nfs4_layout_stateid *ls,
 			     struct nfsd_file *file);
Signature · 1 line
2.56.0