Skip to contentSkip to conversation

sunrpc: Reduce lock contention for NFSD TCP sockets

PATCH New Chuck Lever 1 participant 9 messages Feb 10

Subsystems (4)
lore
Share & tools
lore

Chuck Lever

author maintainer
#0 lore raw

v2 cover sunrpc: Reduce lock contention for NFSD TCP sockets

From: Chuck Lever <[email protected]>

High-throughput NFSD workloads exhibit significant lock contention on TCP connections. Worker threads compete for the socket lock during receives and serialize on xpt_mutex during sends, limiting scalability.

This series addresses both paths:

- Receive: A dedicated kernel thread per TCP connection owns all sock_recvmsg() calls and queues complete RPC messages for workers via lock-free llist. This eliminates socket lock contention among workers.

- Transmit: Flat combining allows one thread to send on behalf of multiple waiters. Threads enqueue requests; the mutex holder ("combiner") processes the batch, amortizing lock acquisition and enabling TCP segment coalescing via MSG_MORE.

Supporting changes include a page recycling pool for receive buffers, and explicit TCP buffer sizing for high bandwidth-delay product networks.

Base commit: v6.19
URL: https://git.kernel.org/pub/scm/linux/kernel/git/cel/linux.git/log/?h=svctcp-next

---

Changes since RFC:
- Drop the affinity scope patch
- Skip user memory hardening for kernel-to-kernel copies
- Avoid invoking wake_up when the receive is already running
- Refactor svc_tcp_receiver_thread() for legibility
- Do not set MSG_MORE during batched sends

Chuck Lever (8):
sunrpc: Add XPT flags missing from SVC_XPRT_FLAG_LIST
net: datagram: bypass usercopy checks for kernel iterators
sunrpc: split svc_data_ready into protocol-specific callbacks
sunrpc: add per-transport page recycling pool
sunrpc: add dedicated TCP receiver thread
sunrpc: implement flat combining for TCP socket sends
sunrpc: unify fore and backchannel server TCP send paths
sunrpc: Set explicit TCP socket buffer sizes for NFSD

 include/linux/sunrpc/svc.h      |   1 +
 include/linux/sunrpc/svc_xprt.h |  32 ++
 include/linux/sunrpc/svcsock.h  |  40 ++
 include/trace/events/sunrpc.h   |   7 +-
 net/core/datagram.c             |  15 +-
 net/sunrpc/svc.c                |  13 +
 net/sunrpc/svc_xprt.c           | 151 ++++++
 net/sunrpc/svcsock.c            | 802 +++++++++++++++++++++++++++++---
 net/sunrpc/xprtsock.c           |  60 +--
 9 files changed, 999 insertions(+), 122 deletions(-)
Signature · 1 line
2.52.0
author maintainer
#1 lore raw

PATCH v2 1/8 sunrpc: Add XPT flags missing from SVC_XPRT_FLAG_LIST

From: Chuck Lever <[email protected]>

Commit eccbbc7c00a5 ("nfsd: don't use sv_nrthreads in connection limiting calculations.") and commit 898374fdd7f0 ("nfsd: unregister with rpcbind when deleting a transport") added new XPT flags but neglected to update the show_svc_xprt_flags() macro.

---
 include/trace/events/sunrpc.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
include/trace/events/sunrpc.h +3 -1
index 750ecce56930..076182ae19ec 100644
@@ -1933,7 +1933,9 @@ TRACE_EVENT(svc_stats_latency,
 	svc_xprt_flag(CONG_CTRL)					\
 	svc_xprt_flag(HANDSHAKE)					\
 	svc_xprt_flag(TLS_SESSION)					\
-	svc_xprt_flag_end(PEER_AUTH)
+	svc_xprt_flag(PEER_AUTH)					\
+	svc_xprt_flag(XPT_PEER_VALID)					\
+	svc_xprt_flag_end(XPT_RPCB_UNREG)
 
 #undef svc_xprt_flag
 #undef svc_xprt_flag_end
Signature · 1 line
2.52.0
author maintainer
#2 lore raw

PATCH v2 2/8 net: datagram: bypass usercopy checks for kernel iterators

In reply to Chuck Lever

From: Chuck Lever <[email protected]>

Profiling NFSD under an iozone workload showed that hardened usercopy checks consume roughly 1.3% of CPU in the TCP receive path. These checks validate memory regions during copies, but provide no security benefit when both source (skb data) and destination (kernel pages in BVEC/KVEC iterators) reside in kernel address space.

Modify simple_copy_to_iter() and crc32c_and_copy_to_iter() to call _copy_to_iter() directly when the destination is a kernel-only iterator, bypassing the usercopy hardening validation. User-backed iterators (ITER_UBUF, ITER_IOVEC) continue to use copy_to_iter() with full validation.

This benefits kernel consumers of TCP receive such as NFSD (SUNRPC) and NVMe-TCP, which use ITER_BVEC for their receive buffers.

---
 net/core/datagram.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
net/core/datagram.c +13 -2
index c285c6465923..df6b87d7c415 100644
@@ -490,7 +490,10 @@ static size_t crc32c_and_copy_to_iter(const void *addr, size_t bytes,
 	u32 *crcp = _crcp;
 	size_t copied;
 
-	copied = copy_to_iter(addr, bytes, i);
+	if (user_backed_iter(i))
+		copied = copy_to_iter(addr, bytes, i);
+	else
+		copied = _copy_to_iter(addr, bytes, i);
 	*crcp = crc32c(*crcp, addr, copied);
 	return copied;
 }
@@ -515,10 +518,18 @@ int skb_copy_and_crc32c_datagram_iter(const struct sk_buff *skb, int offset,
 EXPORT_SYMBOL(skb_copy_and_crc32c_datagram_iter);
 #endif /* CONFIG_NET_CRC32C */
 
+/*
+ * For kernel-only iterators (BVEC, KVEC, etc.), bypass usercopy
+ * hardening checks. Both the source (skb data) and destination
+ * (kernel pages/buffers) are kernel memory, so the checks add
+ * overhead without security benefit.
+ */
 static size_t simple_copy_to_iter(const void *addr, size_t bytes,
 		void *data __always_unused, struct iov_iter *i)
 {
-	return copy_to_iter(addr, bytes, i);
+	if (user_backed_iter(i))
+		return copy_to_iter(addr, bytes, i);
+	return _copy_to_iter(addr, bytes, i);
 }
 
 /**
Signature · 1 line
2.52.0
author maintainer
#3 lore raw

PATCH v2 3/8 sunrpc: split svc_data_ready into protocol-specific callbacks

In reply to Chuck Lever

From: Chuck Lever <[email protected]>

Separate data-ready callbacks enable protocol-specific optimizations. UDP and TCP transports already have different
requirements: currently UDP sockets do not implement DTLS, so the XPT_HANDSHAKE check is unnecessary overhead for them.

Prepare the server-side socket infrastructure for additional changes to TCP's data_ready callback.

---
 include/trace/events/sunrpc.h |  3 ++-
 net/sunrpc/svcsock.c          | 39 +++++++++++++++++++++++++++++------
 2 files changed, 35 insertions(+), 7 deletions(-)
include/trace/events/sunrpc.h +2 -1
index 076182ae19ec..c5a15b7a321d 100644
@@ -2313,7 +2313,8 @@ DEFINE_SVCSOCK_EVENT(tcp_send);
 DEFINE_SVCSOCK_EVENT(tcp_recv);
 DEFINE_SVCSOCK_EVENT(tcp_recv_eagain);
 DEFINE_SVCSOCK_EVENT(tcp_recv_err);
-DEFINE_SVCSOCK_EVENT(data_ready);
+DEFINE_SVCSOCK_EVENT(udp_data_ready);
+DEFINE_SVCSOCK_EVENT(tcp_data_ready);
 DEFINE_SVCSOCK_EVENT(write_space);
 
 TRACE_EVENT(svcsock_tcp_recv_short,
net/sunrpc/svcsock.c +33 -6
index d61cd9b40491..73644f3b63c7 100644
@@ -397,10 +397,14 @@ static void svc_sock_secure_port(struct svc_rqst *rqstp)
 		clear_bit(RQ_SECURE, &rqstp->rq_flags);
 }
 
-/*
- * INET callback when data has been received on the socket.
+/**
+ * svc_udp_data_ready - sk->sk_data_ready callback for UDP sockets
+ * @sk: socket whose receive buffer contains data
+ *
+ * This implementation does not yet support DTLS, so the
+ * XPT_HANDSHAKE check is not needed here.
  */
-static void svc_data_ready(struct sock *sk)
+static void svc_udp_data_ready(struct sock *sk)
 {
 	struct svc_sock	*svsk = (struct svc_sock *)sk->sk_user_data;
 
@@ -410,7 +414,30 @@ static void svc_data_ready(struct sock *sk)
 		/* Refer to svc_setup_socket() for details. */
 		rmb();
 		svsk->sk_odata(sk);
-		trace_svcsock_data_ready(&svsk->sk_xprt, 0);
+		trace_svcsock_udp_data_ready(&svsk->sk_xprt, 0);
+		if (!test_and_set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags))
+			svc_xprt_enqueue(&svsk->sk_xprt);
+	}
+}
+
+/**
+ * svc_tcp_data_ready - sk->sk_data_ready callback for TCP sockets
+ * @sk: socket whose receive buffer contains data
+ *
+ * Data ingest is skipped while a TLS handshake is in progress
+ * (XPT_HANDSHAKE).
+ */
+static void svc_tcp_data_ready(struct sock *sk)
+{
+	struct svc_sock	*svsk = (struct svc_sock *)sk->sk_user_data;
+
+	trace_sk_data_ready(sk);
+
+	if (svsk) {
+		/* Refer to svc_setup_socket() for details. */
+		rmb();
+		svsk->sk_odata(sk);
+		trace_svcsock_tcp_data_ready(&svsk->sk_xprt, 0);
 		if (test_bit(XPT_HANDSHAKE, &svsk->sk_xprt.xpt_flags))
 			return;
 		if (!test_and_set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags))
@@ -835,7 +862,7 @@ static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
 	svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class,
 		      &svsk->sk_xprt, serv);
 	clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
-	svsk->sk_sk->sk_data_ready = svc_data_ready;
+	svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
 	svsk->sk_sk->sk_write_space = svc_write_space;
 
 	/* initialise setting must have enough space to
@@ -1368,7 +1395,7 @@ static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
 		set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
 	} else {
 		sk->sk_state_change = svc_tcp_state_change;
-		sk->sk_data_ready = svc_data_ready;
+		sk->sk_data_ready = svc_tcp_data_ready;
 		sk->sk_write_space = svc_write_space;
 
 		svsk->sk_marker = xdr_zero;
Signature · 1 line
2.52.0
author maintainer
#4 lore raw

PATCH v2 4/8 sunrpc: add per-transport page recycling pool

In reply to Chuck Lever

From: Chuck Lever <[email protected]>

RPC server transports allocate pages for receiving incoming data on every request. Under high load, this repeated allocation and freeing creates unnecessary overhead in the page allocator hot path.

Introduce svc_page_pool, a lock-free page recycling mechanism that enables efficient page reuse between receive operations. A follow-up commit wires this into the TCP transport's receive path; svcrdma's RDMA Read path might also make use of this mechanism some day.

The pool uses llist for lock-free producer-consumer handoff: worker threads returning pages after RPC processing act as producers, while receiver threads allocating pages for incoming data act as consumers. Pages are linked via page->pcp_llist, which is safe because these pages are owned exclusively by the transport.

Each pool tracks its NUMA node affinity, allowing page allocations to target the same node as the transport's receiver thread. Provide svc_pool_node() to enable transports to determine the NUMA node associated with a service pool for NUMA-aware resource allocation.

---
 include/linux/sunrpc/svc.h      |   1 +
 include/linux/sunrpc/svc_xprt.h |  32 +++++++
 net/sunrpc/svc.c                |  13 +++
 net/sunrpc/svc_xprt.c           | 151 ++++++++++++++++++++++++++++++++
 4 files changed, 197 insertions(+)
include/linux/sunrpc/svc.h +1 -0
index 5506d20857c3..f4efe60f4dad 100644
@@ -457,6 +457,7 @@ void		   svc_wake_up(struct svc_serv *);
 void		   svc_reserve(struct svc_rqst *rqstp, int space);
 void		   svc_pool_wake_idle_thread(struct svc_pool *pool);
 struct svc_pool   *svc_pool_for_cpu(struct svc_serv *serv);
+int		   svc_pool_node(struct svc_pool *pool);
 char *		   svc_print_addr(struct svc_rqst *, char *, size_t);
 const char *	   svc_proc_name(const struct svc_rqst *rqstp);
 int		   svc_encode_result_payload(struct svc_rqst *rqstp,
include/linux/sunrpc/svc_xprt.h +32 -0
index da2a2531e110..e60c2936b1ce 100644
@@ -9,9 +9,33 @@
 #define SUNRPC_SVC_XPRT_H
 
 #include <linux/sunrpc/svc.h>
+#include <linux/llist.h>
 
 struct module;
 
+/**
+ * struct svc_page_pool - per-transport page recycling pool
+ * @pp_pages: lock-free list of recycled pages
+ * @pp_count: number of pages currently in pool
+ * @pp_numa_node: NUMA node for page allocations
+ * @pp_max: maximum pages to retain in pool
+ *
+ * Lock-free page recycling between producers (svc threads returning
+ * pages) and a single consumer (the thread allocating pages for
+ * receives). Uses llist for efficient producer-consumer handoff
+ * without spinlocks.
+ *
+ * Callers must serialize calls to svc_page_pool_get(); multiple
+ * concurrent consumers are not supported.
+ * Allocate with svc_page_pool_alloc(); free with svc_page_pool_free().
+ */
+struct svc_page_pool {
+	struct llist_head	pp_pages;
+	atomic_t		pp_count;
+	int			pp_numa_node;
+	unsigned int		pp_max;
+};
+
 struct svc_xprt_ops {
 	struct svc_xprt	*(*xpo_create)(struct svc_serv *,
 				       struct net *net,
@@ -187,6 +211,14 @@ void	svc_add_new_perm_xprt(struct svc_serv *serv, struct svc_xprt *xprt);
 void	svc_age_temp_xprts_now(struct svc_serv *, struct sockaddr *);
 void	svc_xprt_deferred_close(struct svc_xprt *xprt);
 
+/* Page pool helpers */
+struct svc_page_pool *svc_page_pool_alloc(int numa_node, unsigned int max);
+void	svc_page_pool_free(struct svc_page_pool *pool);
+void	svc_page_pool_put(struct svc_page_pool *pool, struct page *page);
+void	svc_page_pool_put_bulk(struct svc_page_pool *pool,
+			       struct page **pages, unsigned int count);
+struct page *svc_page_pool_get(struct svc_page_pool *pool);
+
 static inline void svc_xprt_get(struct svc_xprt *xprt)
 {
 	kref_get(&xprt->xpt_ref);
net/sunrpc/svc.c +13 -0
index 4704dce7284e..6b350cb7d539 100644
@@ -418,6 +418,19 @@ struct svc_pool *svc_pool_for_cpu(struct svc_serv *serv)
 	return &serv->sv_pools[pidx % serv->sv_nrpools];
 }
 
+/**
+ * svc_pool_node - Return the NUMA node affinity of a service pool
+ * @pool: the service pool
+ *
+ * Return value:
+ *   The NUMA node the pool is associated with, or the local node
+ *   if no explicit mapping exists
+ */
+int svc_pool_node(struct svc_pool *pool)
+{
+	return svc_pool_map_get_node(pool->sp_id);
+}
+
 static int svc_rpcb_setup(struct svc_serv *serv, struct net *net)
 {
 	int err;
net/sunrpc/svc_xprt.c +151 -0

155 lines not on this page · raw message

Signature · 1 line
2.52.0
author maintainer
#5 lore raw

PATCH v2 5/8 sunrpc: add dedicated TCP receiver thread

In reply to Chuck Lever

From: Chuck Lever <[email protected]>

Receive-side socket lock contention on NFS server TCP connections is eliminated by dedicating one kernel thread per TCP socket to handle all receives.

In the current architecture, multiple nfsd threads compete for the same socket, serializing on the socket lock inside sock_recvmsg(). A single receiver kthread per TCP connection issues all sock_recvmsg() calls and enqueues complete RPC messages for nfsd threads to process.

Architecture:

Before:
nfsd 1 --+ +-- sock_recvmsg() --+
nfsd 2 --+-- compete for xprt-+-- sock_recvmsg() --+- CONTENTION
nfsd 3 --+ +-- sock_recvmsg() --+

After:
Receiver kthread - sock_recvmsg() -+- nfsd 1 - process - send
(no contention) +- nfsd 2 - process - send
+- nfsd 3 - process - send

A lock-free llist queue passes complete RPC messages from the receiver to nfsd threads, avoiding spinlock overhead in the fast path. Flow control limits queue depth to SVC_TCP_MSG_QUEUE_MAX (64) messages per socket to bound memory usage.

This mirrors the svcrdma architecture, where RDMA completion handlers enqueue received messages for nfsd threads rather than having nfsd threads compete for hardware resources.

NUMA Affinity:

The receiver kthread is placed on the NUMA node associated with the service pool handling the accept, matching the NUMA placement strategy used for nfsd threads. Page allocations for receive buffers explicitly target this node via __alloc_pages_bulk(), providing memory locality for the receive path. This mirrors how svcrdma allocates resources on the RNIC's NUMA node.

svc_tcp_data_ready() now wakes the dedicated receiver kthread instead of enqueueing the transport for nfsd threads. If receiver kthread creation fails during connection accept, the connection is rejected; the client retries.

---
 include/linux/sunrpc/svcsock.h |  36 +++
 net/sunrpc/svcsock.c           | 502 ++++++++++++++++++++++++++++++---
 2 files changed, 498 insertions(+), 40 deletions(-)
include/linux/sunrpc/svcsock.h +36 -0
index de37069aba90..b94096eeb890 100644
@@ -12,6 +12,32 @@
 
 #include <linux/sunrpc/svc.h>
 #include <linux/sunrpc/svc_xprt.h>
+#include <linux/cache.h>
+#include <linux/llist.h>
+#include <linux/wait.h>
+#include <linux/completion.h>
+#include <linux/ktime.h>
+
+/* Maximum queued messages per TCP socket before backpressure */
+#define SVC_TCP_MSG_QUEUE_MAX	64
+
+/**
+ * struct svc_tcp_msg - queued RPC message ready for processing
+ * @tm_node: lock-free queue linkage
+ * @tm_len: total message length
+ * @tm_npages: number of pages holding message data
+ * @tm_pages: flexible array of pages containing the message
+ *
+ * Complete RPC messages are enqueued using these structures after
+ * reception. Page ownership transfers from the receiver's rqstp to
+ * this structure, then to an nfsd thread's rqstp during dequeue.
+ */
+struct svc_tcp_msg {
+	struct llist_node	tm_node;
+	size_t			tm_len;
+	unsigned int		tm_npages;
+	struct page		*tm_pages[];
+};
 
 /*
  * RPC server socket.
@@ -43,6 +69,16 @@ struct svc_sock {
 
 	struct completion	sk_handshake_done;
 
+	/* Dedicated receiver thread (TCP only) */
+	struct task_struct	*sk_receiver;
+	struct llist_head	sk_msg_queue;
+	wait_queue_head_t	sk_receiver_wq;
+	struct completion	sk_receiver_exit;
+	struct svc_page_pool	*sk_page_pool;
+	ktime_t			sk_partial_record_time;
+
+	atomic_t		sk_msg_count ____cacheline_aligned_in_smp;
+
 	/* received data */
 	unsigned long		sk_maxpages;
 	struct page *		sk_pages[] __counted_by(sk_maxpages);
net/sunrpc/svcsock.c +462 -40

596 lines not on this page · raw message

Signature · 1 line
2.52.0
author maintainer
#6 lore raw

PATCH v2 6/8 sunrpc: implement flat combining for TCP socket sends

In reply to Chuck Lever

From: Chuck Lever <[email protected]>

High contention on xpt_mutex during TCP reply transmission limits nfsd scalability. When multiple threads send replies on the same connection, mutex handoff triggers optimistic spinning that consumes substantial CPU time. Profiling on high-throughput workloads shows approximately 15% of cycles spent in the spin-wait path.

The flat combining pattern addresses this by allowing one thread to perform send operations on behalf of multiple waiting threads. Rather than each thread acquiring xpt_mutex independently, threads enqueue their send requests to a lock-free llist. The first thread to acquire the mutex becomes the combiner and processes all pending sends in a batch. Other threads wait on a per-request completion structure instead of spinning on the lock.

The combiner continues processing the queue until empty before releasing xpt_mutex, amortizing acquisition cost across multiple sends. TCP auto-corking naturally coalesces segments once the first send places data in flight, avoiding the need for explicit corking hints that would delay initial replies.

---
 include/linux/sunrpc/svcsock.h |   2 +
 net/sunrpc/svcsock.c           | 181 ++++++++++++++++++++++++++++-----
 2 files changed, 158 insertions(+), 25 deletions(-)
include/linux/sunrpc/svcsock.h +2 -0
index b94096eeb890..fae760eaa9f7 100644
@@ -54,6 +54,8 @@ struct svc_sock {
 
 	/* For sends (protected by xpt_mutex) */
 	struct bio_vec		*sk_bvec;
+	struct llist_head	sk_send_queue;	/* queued sends awaiting batch processing */
+	u64			sk_drain_avg_ns; /* EMA of batch drain time */
 
 	/* private TCP part */
 	/* On-the-wire fragment header: */
net/sunrpc/svcsock.c +156 -25

206 lines not on this page · raw message

Signature · 1 line
2.52.0
author maintainer
#7 lore raw

PATCH v2 7/8 sunrpc: unify fore and backchannel server TCP send paths

In reply to Chuck Lever

From: Chuck Lever <[email protected]>

High latency in callback processing for NFSv4.1+ sessions can occur when the fore channel sustains heavy request traffic. The backchannel implementation acquires xpt_mutex directly while the fore channel uses flat combining to batch socket operations. A thread in the combining loop processes queued sends continuously until the llist empties, which under load means backchannel threads block on xpt_mutex for extended periods waiting for a turn at the socket. Delegation recalls and other callback operations carry time constraints that make this starvation problematic.

Routing backchannel sends through the same flat combining infrastructure eliminates this starvation; a shared llist queue replaces direct mutex acquisition and separate code paths. The struct svc_pending_send now holds an xdr_buf pointer instead of svc_rqst, decoupling the queueing mechanism from RPC request structures. A new svc_tcp_send() function accepts an xprt, xdr_buf, and marker, then either enters the combining loop or enqueues for processing by an active combiner.

The fore channel path through svc_tcp_sendto() now calls svc_tcp_send() after preparing its xdr_buf. The backchannel bc_send_request() similarly calls svc_tcp_send() in place of its former mutex acquisition and direct bc_sendto() invocation. Both channels queue into the same llist, so backchannel operations receive fair treatment in the send ordering. When a backchannel send queues behind fore channel traffic, the combining loop processes both together with shared socket lock acquisition and MSG_MORE coalescing where applicable.

Maintenance burden decreases with a single code path for TCP sends. The backchannel gains batching benefits when concurrent with fore channel load, and starvation no longer occurs because queueing provides deterministic ordering independent of mutex contention timing.

---
 include/linux/sunrpc/svcsock.h |  2 +
 net/sunrpc/svcsock.c           | 76 +++++++++++++++++++++++++---------
 net/sunrpc/xprtsock.c          | 60 ++++++---------------------
 3 files changed, 71 insertions(+), 67 deletions(-)
include/linux/sunrpc/svcsock.h +2 -0
index fae760eaa9f7..07619bf2131c 100644
@@ -101,6 +101,8 @@ static inline u32 svc_sock_final_rec(struct svc_sock *svsk)
  */
 void		svc_recv(struct svc_rqst *rqstp);
 void		svc_send(struct svc_rqst *rqstp);
+int		svc_tcp_send(struct svc_xprt *xprt, struct xdr_buf *xdr,
+			     rpc_fraghdr marker);
 int		svc_addsock(struct svc_serv *serv, struct net *net,
 			    const int fd, char *name_return, const size_t len,
 			    const struct cred *cred);
net/sunrpc/svcsock.c +56 -20

143 lines not on this page · raw message

net/sunrpc/xprtsock.c +13 -47

78 lines not on this page · raw message

Signature · 1 line
2.52.0
author maintainer
#8 lore raw

PATCH v2 8/8 sunrpc: Set explicit TCP socket buffer sizes for NFSD

In reply to Chuck Lever

From: Chuck Lever <[email protected]>

NFSD TCP sockets currently rely on system defaults with TCP auto-tuning. On networks with large bandwidth-delay products, the default maximum buffer sizes (6MB receive, 4MB send) can throttle throughput. Administrators must resort to system-wide sysctl adjustments (tcp_rmem/tcp_wmem), which affect all TCP connections rather than just NFS traffic.

This change sets explicit buffer sizes for NFSD TCP data sockets. The buffer size is set to 4 * sv_max_mesg, yielding approximately 16MB with default NFS payload sizes. On memory-constrained systems, the buffer size is capped at 1/1024 of physical RAM, with a hard ceiling of 16MB. SOCK_SNDBUF_LOCK and SOCK_RCVBUF_LOCK disable auto-tuning, providing predictable memory consumption.

The existing svc_sock_setbufsize() is renamed to svc_udp_setbufsize() to reflect its UDP-specific purpose, and a new svc_tcp_setbufsize() handles TCP data connections. Listener sockets remain unaffected, as listeners do not transfer data.

This approach improves throughput on high-speed networks without requiring system-wide configuration changes, while automatically scaling down buffer sizes on small systems.

---
 net/sunrpc/svcsock.c | 52 ++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 48 insertions(+), 4 deletions(-)
net/sunrpc/svcsock.c +48 -4
index 8b2e9f524506..91a472021d0a 100644
@@ -50,6 +50,7 @@
 #include <net/handshake.h>
 #include <linux/uaccess.h>
 #include <linux/highmem.h>
+#include <linux/mm.h>
 #include <asm/ioctls.h>
 #include <linux/key.h>
 
@@ -377,9 +378,12 @@ static ssize_t svc_tcp_read_msg(struct svc_rqst *rqstp, size_t buflen,
 }
 
 /*
- * Set socket snd and rcv buffer lengths
+ * Set socket snd and rcv buffer lengths for UDP sockets.
+ *
+ * UDP sockets need large buffers because pending requests remain
+ * in the receive buffer until processed by a worker thread.
  */
-static void svc_sock_setbufsize(struct svc_sock *svsk, unsigned int nreqs)
+static void svc_udp_setbufsize(struct svc_sock *svsk, unsigned int nreqs)
 {
 	unsigned int max_mesg = svsk->sk_xprt.xpt_server->sv_max_mesg;
 	struct socket *sock = svsk->sk_sock;
@@ -393,6 +397,45 @@ static void svc_sock_setbufsize(struct svc_sock *svsk, unsigned int nreqs)
 	release_sock(sock->sk);
 }
 
+/* Accommodate high bandwidth-delay product connections */
+#define SVC_TCP_SNDBUF_MAX	(16 * 1024 * 1024)
+#define SVC_TCP_RCVBUF_MAX	(16 * 1024 * 1024)
+
+/*
+ * Set socket snd and rcv buffer lengths for TCP data sockets.
+ *
+ * Buffers are sized to accommodate high-bandwidth data transfers on
+ * high-latency networks (large bandwidth-delay product). Automatic
+ * buffer tuning is disabled to allow control of server memory
+ * consumption.
+ */
+static void svc_tcp_setbufsize(struct svc_sock *svsk)
+{
+	struct svc_serv *serv = svsk->sk_xprt.xpt_server;
+	struct socket *sock = svsk->sk_sock;
+	unsigned long mem_cap, ideal;
+	unsigned int sndbuf, rcvbuf;
+
+	/* Buffer multiple in-flight RPC messages */
+	ideal = serv->sv_max_mesg * 4;
+
+	/* Memory-based cap: 1/1024 of physical RAM */
+	mem_cap = (totalram_pages() >> 10) << PAGE_SHIFT;
+
+	sndbuf = clamp_t(unsigned long, ideal,
+			 serv->sv_max_mesg, min(mem_cap, SVC_TCP_SNDBUF_MAX));
+	rcvbuf = clamp_t(unsigned long, ideal,
+			 serv->sv_max_mesg, min(mem_cap, SVC_TCP_RCVBUF_MAX));
+
+	lock_sock(sock->sk);
+	sock->sk->sk_sndbuf = sndbuf;
+	sock->sk->sk_rcvbuf = rcvbuf;
+	sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
+	sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
+	sock->sk->sk_write_space(sock->sk);
+	release_sock(sock->sk);
+}
+
 static void svc_sock_secure_port(struct svc_rqst *rqstp)
 {
 	if (svc_port_is_privileged(svc_addr(rqstp)))
@@ -668,7 +711,7 @@ static int svc_udp_recvfrom(struct svc_rqst *rqstp)
 	     * provides an upper bound on the number of threads
 	     * which will access the socket.
 	     */
-	    svc_sock_setbufsize(svsk, serv->sv_nrthreads + 3);
+	    svc_udp_setbufsize(svsk, serv->sv_nrthreads + 3);
 
 	clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
 	err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
@@ -884,7 +927,7 @@ static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
 	 * receive and respond to one request.
 	 * svc_udp_recvfrom will re-adjust if necessary
 	 */
-	svc_sock_setbufsize(svsk, 3);
+	svc_udp_setbufsize(svsk, 3);
 
 	/* data might have come in before data_ready set up */
 	set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
@@ -1993,6 +2036,7 @@ static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
 		       svsk->sk_maxpages * sizeof(struct page *));
 
 		tcp_sock_set_nodelay(sk);
+		svc_tcp_setbufsize(svsk);
 
 		switch (sk->sk_state) {
 		case TCP_SYN_RECV:
Signature · 1 line
2.52.0