public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Laurent Vivier <lvivier@redhat.com>
Cc: passt-dev@passt.top
Subject: Re: [PATCH v2 07/10] tcp: Protect init socket pools with mutex for thread safety
Date: Mon, 3 Aug 2026 16:53:27 +1000	[thread overview]
Message-ID: <anA63lypWu7yzOy3@zatzit> (raw)
In-Reply-To: <20260731162329.3552800-8-lvivier@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 3635 bytes --]

On Fri, Jul 31, 2026 at 06:23:26PM +0200, Laurent Vivier wrote:
> The pre-opened socket pools init_sock_pool4/6 are consumed by
> tcp_conn_pool_sock() when creating new connections from any worker
> thread, and refilled by tcp_sock_refill_pool() from tcp_timer() in
> post_handler(). These can run concurrently on different threads.
> 
> Add a mutex protecting both operations in tcp_conn_sock() and
> tcp_sock_refill_init(), where init namespace pools are accessed.

I'm guessing you're going with a mutex rather than a per-thread socket
pool for simplicity?  That might be the right choice, but I do wonder
a bit about it.  The pool exists to avoid the latency of creating a
new socket for a new connection.  If the latency of taking the lock
exceeds that of creating a socket, there's no longer any point to the
pool.  In the unconstest case, that's almost certainly not the case -
a happy path futex() lock should be be much faster than a syscall.  If
the lock _is_ contested, I suspect opening a socket directly might win
- at least for host ns sockets.  For guest ns sockets, the latency is
higher because we need vfork()/setns()/etc.

Then again... taking a socket from the pool also involves writing the
pool, which potentially means a cacheline pingpong.  That might incur
a pretty substantial latency.

In theory we could trylock() and open a socket directly if we don't
get the lock immediately, but at that point it would probably be
simpler to have per-thread socket pools anyway.

I guess, since there is no meaningful shared state in the socket pool,
a per-thread pool seems like the more natural approach to me.

> 
> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> ---
>  tcp.c | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/tcp.c b/tcp.c
> index ab7cbfa5de83..aef689faf031 100644
> --- a/tcp.c
> +++ b/tcp.c
> @@ -293,6 +293,7 @@
>  #include <sys/uio.h>
>  #include <time.h>
>  #include <arpa/inet.h>
> +#include <pthread.h>
>  
>  #include <linux/sockios.h>
>  #include <linux/sock_diag.h>
> @@ -439,6 +440,7 @@ static socklen_t tcp_info_size;
>  /* Pools for pre-opened sockets (in init) */
>  int init_sock_pool4		[TCP_SOCK_POOL_SIZE];
>  int init_sock_pool6		[TCP_SOCK_POOL_SIZE];
> +static pthread_mutex_t sock_pool_lock = PTHREAD_MUTEX_INITIALIZER;
>  
>  /**
>   * conn_at_sidx() - Get TCP connection specific flow at given sidx
> @@ -1581,7 +1583,11 @@ int tcp_conn_sock(sa_family_t af)
>  	int *pool = af == AF_INET6 ? init_sock_pool6 : init_sock_pool4;
>  	int s;
>  
> -	if ((s = tcp_conn_pool_sock(pool)) >= 0)
> +	pthread_mutex_lock(&sock_pool_lock);
> +	s = tcp_conn_pool_sock(pool);
> +	pthread_mutex_unlock(&sock_pool_lock);
> +
> +	if (s >= 0)
>  		return s;
>  
>  	/* If the pool is empty we just open a new one without refilling the
> @@ -2858,6 +2864,7 @@ int tcp_sock_refill_pool(int pool[], sa_family_t af)
>   */
>  static void tcp_sock_refill_init(const struct ctx *c)
>  {
> +	pthread_mutex_lock(&sock_pool_lock);
>  	if (c->ifi4) {
>  		int rc = tcp_sock_refill_pool(init_sock_pool4, AF_INET);
>  		if (rc < 0)
> @@ -2870,6 +2877,7 @@ static void tcp_sock_refill_init(const struct ctx *c)
>  			warn("TCP: Error refilling IPv6 host socket pool: %s",
>  			     strerror_(-rc));
>  	}
> +	pthread_mutex_unlock(&sock_pool_lock);
>  }
>  
>  /**
> -- 
> 2.54.0
> 

-- 
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2026-08-03  6:54 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-31 16:23 [PATCH v2 00/10] multithreading: Prepare data structures for concurrent queue pair workers Laurent Vivier
2026-07-31 16:23 ` [PATCH v2 01/10] tap: Convert packet pools to per-queue-pair arrays for multiqueue Laurent Vivier
2026-08-03  5:41   ` David Gibson
2026-07-31 16:23 ` [PATCH v2 02/10] tap: Make L4 sequence pools per-qpair for thread safety Laurent Vivier
2026-08-03  5:43   ` David Gibson
2026-08-03  5:55     ` David Gibson
2026-07-31 16:23 ` [PATCH v2 03/10] tcp: Make static buffers stack-local " Laurent Vivier
2026-08-03  5:59   ` David Gibson
2026-07-31 16:23 ` [PATCH v2 04/10] udp_vu: Make virtqueue " Laurent Vivier
2026-08-03  6:00   ` David Gibson
2026-07-31 16:23 ` [PATCH v2 05/10] flow: Make flow timer per-caller " Laurent Vivier
2026-08-03  6:11   ` David Gibson
2026-08-03  6:35     ` David Gibson
2026-07-31 16:23 ` [PATCH v2 06/10] tcp: Make TCP timer state per-caller and guard global tasks Laurent Vivier
2026-08-03  6:40   ` David Gibson
2026-07-31 16:23 ` [PATCH v2 07/10] tcp: Protect init socket pools with mutex for thread safety Laurent Vivier
2026-08-03  6:53   ` David Gibson [this message]
2026-07-31 16:23 ` [PATCH v2 08/10] tcp: Extract tcp_timer_epoll_add() helper Laurent Vivier
2026-08-03  6:54   ` David Gibson
2026-07-31 16:23 ` [PATCH v2 09/10] flow: Add locking, per-qpair filtering, and intermediate state handling Laurent Vivier
2026-08-03  9:58   ` David Gibson
2026-07-31 16:23 ` [PATCH v2 10/10] flow: Add lazy, lock-free flow migration between queue pairs Laurent Vivier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=anA63lypWu7yzOy3@zatzit \
    --to=david@gibson.dropbear.id.au \
    --cc=lvivier@redhat.com \
    --cc=passt-dev@passt.top \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).