From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=cOnIjQx8; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTPS id 8E3495A061B for ; Fri, 31 Jul 2026 18:23:42 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1785515021; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Uq5O8pcpS3cZymJkYpFM7KMtSCecSVs0Sp/39+Uok1o=; b=cOnIjQx8K1vsv31fFHlQGdXhnIrYQIn6y/9h4XaVC9HX9mEo+JutHvOlIYgzQi1stUgmQo MAIH4XZNnu9INDh9d90fmxRP1BAtaVnmHZXL1Eju4sWNNs5N96JUA19oDX2gEM5C4h1tVP BxpzjQLwB6TXctAvKuDiGBAp0zE1fZQ= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-53-Xxfqx1RKMciB03dd4MMNpg-1; Fri, 31 Jul 2026 12:23:39 -0400 X-MC-Unique: Xxfqx1RKMciB03dd4MMNpg-1 X-Mimecast-MFC-AGG-ID: Xxfqx1RKMciB03dd4MMNpg_1785515019 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0441D1800348 for ; Fri, 31 Jul 2026 16:23:39 +0000 (UTC) Received: from lenovo-t14s.redhat.corp (headnet05.pony-001.prod.iad2.dc.redhat.com [10.2.32.117]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 40B5B1956044; Fri, 31 Jul 2026 16:23:38 +0000 (UTC) From: Laurent Vivier To: passt-dev@passt.top Subject: [PATCH v2 07/10] tcp: Protect init socket pools with mutex for thread safety Date: Fri, 31 Jul 2026 18:23:26 +0200 Message-ID: <20260731162329.3552800-8-lvivier@redhat.com> In-Reply-To: <20260731162329.3552800-1-lvivier@redhat.com> References: <20260731162329.3552800-1-lvivier@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 4sv-azfATmlHPLV2S7GjHPaSB3j6HNuy6c3HBQJbGWE_1785515019 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: KRZ7QIAUD6G5FJMJWOCRNUZL2RMSX7DP X-Message-ID-Hash: KRZ7QIAUD6G5FJMJWOCRNUZL2RMSX7DP X-MailFrom: lvivier@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Laurent Vivier X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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. Signed-off-by: Laurent Vivier --- 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 #include #include +#include #include #include @@ -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