Skip to contentSkip to conversation

NFS: fix RCU and tracing pointer safety

PATCH In review Sean Chang 2 participants 4 messages Apr 19 – Apr 21

Subsystems (2)
lore
Share & tools
lore

Sean Chang

author
#0 lore raw

v3 cover NFS: fix RCU and tracing pointer safety

This series addresses two Sparse static analysis warnings in the NFS client related to RCU safety and pointer attributes.

The first patch resolves a "dereferencing noderef expression" warning within the nfs_page_class tracepoint by removing a redundant __private attribute that was causing Sparse to complain during trace-buffer assignments.

The second patch fixes an RCU-unsafe dereference in nfs_compare_super_address. It wraps cl_xprt access with rcu_read_lock() and rcu_dereference(). Following reviewer feedback, the RCU critical section is kept minimal, covering only the transport and network namespace checks.

v3:
- Patch 2: Removed the redundant XPRT_CONNECTED and pointer existence checks as suggested by Benjamin Coddington. Since the comparison is performed while holding sb_lock, the transport's existence is guaranteed, and its connection state does not affect the server's identity.

v2:
- Patch 1: Instead of changing the 'req' field type to unsigned long (as in v1), simply remove the redundant __private attribute. This resolves the Sparse warning while preserving the original pointer type.
- Patch 2: Reduced RCU read-side critical section scope to cover only the necessary transport/net-ns checks, as suggested by reviewers.

Sean Chang (2):
NFS: remove redundant __private attribute from nfs_page_class
NFS: Fix RCU dereference of cl_xprt in nfs_compare_super_address

 fs/nfs/nfstrace.h |  2 +-
 fs/nfs/super.c    | 16 +++++++++++++---
 2 files changed, 14 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 1/2 NFS: remove redundant __private attribute from nfs_page_class

The nfs_page_class tracepoint uses a pointer for the 'req' field marked with the __private attribute. This causes Sparse to complain about dereferencing a private pointer within the trace ring buffer context, specifically during the TP_fast_assign() operation.

This fixes a Sparse warning introduced in commit b6ef079fd984 ("nfs:
more in-depth tracing of writepage events") by removing the redundant __private attribute from the 'req' field.

---
 fs/nfs/nfstrace.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/nfs/nfstrace.h +1 -1
index 9f9ce4a565ea..ff467959f733 100644
@@ -1496,7 +1496,7 @@ DECLARE_EVENT_CLASS(nfs_page_class,
 			__field(dev_t, dev)
 			__field(u32, fhandle)
 			__field(u64, fileid)
-			__field(const struct nfs_page *__private, req)
+			__field(const struct nfs_page *, req)
 			__field(loff_t, offset)
 			__field(unsigned int, count)
 			__field(unsigned long, flags)
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v3 2/2 NFS: Fix RCU dereference of cl_xprt in nfs_compare_super_address

In reply to Sean Chang

The cl_xprt pointer in struct rpc_clnt is marked as __rcu. Accessing it directly in nfs_compare_super_address() is unsafe and triggers Sparse warnings.

Fix this by using rcu_dereference() within an RCU read-side critical section to retrieve the transport pointer. This addresses the sparse warning and ensures atomic access to the pointer, as the transport can be updated via transport switching even while the superblock remains active under sb_lock.

Fixes 7e3fcf61abde ("nfs: don't share mounts between network namespaces")
---
 fs/nfs/super.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
fs/nfs/super.c +13 -3
index 7a318581f85b..4cd420b14ce3 100644
@@ -1166,12 +1166,18 @@ static int nfs_set_super(struct super_block *s, struct fs_context *fc)
 static int nfs_compare_super_address(struct nfs_server *server1,
 				     struct nfs_server *server2)
 {
+	struct rpc_xprt *xprt1, *xprt2;
 	struct sockaddr *sap1, *sap2;
-	struct rpc_xprt *xprt1 = server1->client->cl_xprt;
-	struct rpc_xprt *xprt2 = server2->client->cl_xprt;
+
+	rcu_read_lock();
+
+	xprt1 = rcu_dereference(server1->client->cl_xprt);
+	xprt2 = rcu_dereference(server2->client->cl_xprt);
 
 	if (!net_eq(xprt1->xprt_net, xprt2->xprt_net))
-		return 0;
+		goto out_unlock;
+
+	rcu_read_unlock();
 
 	sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
 	sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
@@ -1203,6 +1209,10 @@ static int nfs_compare_super_address(struct nfs_server *server1,
 	}
 
 	return 1;
+
+out_unlock:
+	rcu_read_unlock();
+	return 0;
 }
 
 static int nfs_compare_userns(const struct nfs_server *old,
Signature · 1 line
2.43.0
Reviewed-by on v3 2/2
#3 lore raw

Re: [PATCH v3 2/2] NFS: Fix RCU dereference of cl_xprt in nfs_compare_super_address

Quoted 4 lines · Sean Chang

The cl_xprt pointer in struct rpc_clnt is marked as __rcu. Accessing it directly in nfs_compare_super_address() is unsafe and triggers Sparse warnings.

Quoted 8 lines

Fix this by using rcu_dereference() within an RCU read-side critical section to retrieve the transport pointer. This addresses the sparse warning and ensures atomic access to the pointer, as the transport can be updated via transport switching even while the superblock remains active under sb_lock.

Fixes 7e3fcf61abde ("nfs: don't share mounts between network namespaces")

Ben