From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.openvz.org (mail.openvz.org [69.168.225.77]) by lore.virtuozzo.com (Postfix) with ESMTPS id 3FBEC8006B for ; Thu, 26 Nov 2026 06:28:33 +0000 (UTC) Received: from mail.openvz.org (localhost [127.0.0.1]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AQ6REjw007803; Thu, 26 Nov 2026 09:27:15 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AQ6REjw007803 Authentication-Results: mail.openvz.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="SmutJZc6" Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AQ6Qu06007724 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Thu, 26 Nov 2026 09:26:56 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AQ6Qu06007724 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-c50657eaef0so548558466b.3 for ; Wed, 25 Nov 2026 22:26:56 -0800 (PST) X-Gm-Message-State: APuU4Z/VmfYkGPFS4VlAWOZOcP/vQlwX8QIxG1IYWydlJmGQDgOedrR5 fEmEOF2HkU9bGcVJaHIfhwdhm+gwb0MgPqwL+zXNd725XJwTKbfKMSRXmfGjFGMjQRJtr1AOvyF IMMOqwEqydO/HYvh7JHiseGzzbjEZqJLSaKFsOz8WXfPggveB2RsEAQ== X-Gm-Gg: AZFdTZcr5z4CuWDsOVH0AM0v+CTGcc8rQAm8DVDLZctQGnJBskmhHh09bIuJNl6r7iL oUtMG4v1u3KO2lSi+T8u3+6cWfiuoVtIxlzVrzIy5tpDHuYyeZZSG4acBIRiZbOgDUybGNvUR8w wnGKKKFrFQlywakU5MTR5K3rFf2PZBrFxRj4zMJPuyZyrjP0Ul3vnx+4vDg2ZCo49ZEVP7lRzE5 JHFDScLUN6b5WrC7E1FNswLRcc8iVrYVf8Mtue6gonZTdSJh/poI/At8DLayQXkoSpYSdAeQuDP zyXTQ1bb2ziTlqjH0ADZ0OgQOepg0KShIxhD2/WQTLImoXGNMJJOyvL5i3lieF0YR0zl2p9w1ed dIR+pKBKZnBOu9pzBtTbsNgaUjg== X-Received: by 2002:a17:906:4fc7:b0:c51:37ec:380f with SMTP id a640c23a62f3a-c513a1df6e5mr1647468566b.0.1795674415896; Wed, 25 Nov 2026 22:26:55 -0800 (PST) X-Received: by 2002:a17:906:4fc7:b0:c51:37ec:380f with SMTP id a640c23a62f3a-c513a1df6e5mr1647466766b.0.1795674415429; Wed, 25 Nov 2026 22:26:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1795674415; cv=none; d=google.com; s=arc-20260327; b=E60IX/rhU7r4aHWIWgyqLyw/9m5eUSEaRHvd6PRVfqyYtyyg7gsWMntRUhhrrbRr1S srLPWweVd+w1FphUJrneNbI21tLQxM5UPPbd3eRGD1owv8qJEhd9zcuOCTAnRW+Br5Ry vkGkZU3rxDqHOz1aAk5MFeomfTMOb05ujvJE6iNS6y+mKwR5DJjeYhiVt2o6YPs1BPl4 h3JW3/Rj5hbWDqRsDeYYIKnO5LNRzn+VLvJuLIFCpIhPuSL8CgOCYzkHyf1x48EE8LHE H+KBiBQY9Qee00HgTTwkFK67g4vfrhzJEuzeYrlvc/seAZOiNqs7nWL32uDtaG3VBQ5d 22+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=na/N2AFVlY7YkEXhz9Bq7HUQOSUQoYC+yYNGO8a6zdo=; fh=/QSWnKFFIPNLStgUoDwCQRaXnWcMIG7fyKJgHY+VEpU=; b=J5RHfz0WNucRzgSOFYlci0vpCQu24uTxeelpHK9IwfzEPTcL0BZmyd+cK6jggJ/VKc 18jBokAvJBxexuUzQe24TtOYfKMm4Cu3H55UgvhauZBr8uNaaSoJvq02IKSuh7Ltv931 GQPNn5JCoUFdDsomtHul1FaZHrECtM9KfJe6ANM082Tw+s6MNfjA5Ce4ZtRG/8kwWXv+ DxbZb1la61k683nqkUZ+nIcBhj3VOBTW6WmuJxkBi6L3qd0aXPj3KMVby+zze9NuxQg/ Bs3S+mSyfhH6oA75xdYbLEDMi3pXfzZvpbNmHP6brnN2KBt+F4fgt7RJ9yJKdZc6JHcV UoTw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=SmutJZc6; spf=pass (google.com: domain of eva.kurchatova@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=eva.kurchatova@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com Received: from relay.virtuozzo.com (relay.virtuozzo.com. [130.117.225.111]) by mx.google.com with ESMTPS id a640c23a62f3a-c513afe4327si1290455166b.44.2026.11.25.22.26.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Nov 2026 22:26:55 -0800 (PST) Received-SPF: pass (google.com: domain of eva.kurchatova@virtuozzo.com designates 130.117.225.111 as permitted sender) client-ip=130.117.225.111; Authentication-Results: mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=SmutJZc6; spf=pass (google.com: domain of eva.kurchatova@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=eva.kurchatova@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-ID:Date:Subject:From: Content-Type; bh=na/N2AFVlY7YkEXhz9Bq7HUQOSUQoYC+yYNGO8a6zdo=; b=SmutJZc6f3Qc dEfBXuvJbEWPLJq5UlFRQLYacA5hC6WidofCmihoCgq7wXid2rAG6rtUZGUkIcrr5WRlaEQllq0Ec jEkVhjVg2O7oJP+qWMZ+TqB1Z2U0WdH5uqe0utLZzGKJsCau9xW/2yRF0xOOw1Kp+OQaYqV/RJeB1 /i+1tT1rAWfIGVIRCLNYAKd34X8m7r0TeA+SjXJCgSSEQTSsrzXUxrlYZPY7e0ITSR834ejRprOP5 HQE2nFvOXARjqf4USpg4erLnAhATQRWR1ZnNMdw2ryzlp+FvfpZTd8NYhiP8xydDCQJXqCYPpEB5/ tvaN4RPsZCqrznwk5R56lg==; Received: from ch-vpn.virtuozzo.com ([130.117.225.6] helo=LekKit-T14) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1xWSwM-00EFGZ-21; Thu, 26 Nov 2026 07:26:53 +0100 From: Eva Kurchatova To: khorenko@virtuozzo.com, vladimir.riabchun@virtuozzo.com Date: Thu, 26 Nov 2026 08:11:04 +0200 Message-ID: <20261126062617.2133758-6-eva.kurchatova@virtuozzo.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261126062617.2133758-2-eva.kurchatova@virtuozzo.com> References: <20261126062617.2133758-2-eva.kurchatova@virtuozzo.com> MIME-Version: 1.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH hci-8.2 v3 04/10] ms/net: restore refcount ordering for lockless socket access X-BeenThere: devel@openvz.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: OpenVZ development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: devel-bounces@openvz.org Errors-To: devel-bounces@openvz.org From: Hyunwoo Kim [ Upstream commit 5b5d8f351dd96f56a58bb516a80e2d4d742fe4e2 ] Before commit fb041bb7c0a9 ("locking/refcount: Consolidate implementations of refcount_t"), default builds implemented refcount_inc_not_zero() using atomic_add_unless(), which provided full memory ordering on success. The commit removed that ordering on weakly ordered architectures, allowing accesses after a successful reference acquisition to be reordered before it. Use refcount_inc_not_zero_acquire() in the IPv4 and IPv6 established lookups and in inet_twsk_purge(), where the socket is validated again after taking a reference. bpf_sk_assign() can receive a socket pointer obtained through BTF without a reference. Use acquire ordering when it takes its own reference for a refcounted socket. Use refcount_set_release() when setting refcounts in the SYN cookie, TCP Fast Open, request migration, and TIME_WAIT paths. Keep the existing TIME_WAIT smp_wmb(), which separately orders the refcount store before ehash publication. Fixes: fb041bb7c0a9 ("locking/refcount: Consolidate implementations of refcount_t") Fixes: 27e5203bd9c5 ("bpf: Change bpf_sk_assign to accept ARG_PTR_TO_BTF_ID_SOCK_COMMON") Cc: stable@vger.kernel.org Signed-off-by: Hyunwoo Kim Reviewed-by: Kuniyuki Iwashima Link: https://patch.msgid.link/20261029214219.1035176-3-imv4bel@gmail.com Signed-off-by: Paolo Abeni Signed-off-by: Sasha Levin (cherry picked from commit 6f4fc93f76cd8ebd3c89a064573715503048d1ad) https://virtuozzo.atlassian.net/browse/VSTOR-156045 Feature: fix ms/net Signed-off-by: Eva Kurchatova Reviewed-by: Sashiko --- net/core/filter.c | 2 +- net/ipv4/inet_connection_sock.c | 6 +++--- net/ipv4/inet_hashtables.c | 2 +- net/ipv4/inet_timewait_sock.c | 4 ++-- net/ipv4/syncookies.c | 2 +- net/ipv4/tcp_fastopen.c | 2 +- net/ipv6/inet6_hashtables.c | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/net/core/filter.c b/net/core/filter.c index b976eaabedd2..3b0ca58da7b5 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -7564,7 +7564,7 @@ BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags) if (sk_unhashed(sk)) return -EOPNOTSUPP; if (sk_is_refcounted(sk) && - unlikely(!refcount_inc_not_zero(&sk->sk_refcnt))) + unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt))) return -ENOENT; skb_orphan(skb); diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c index acbc104e6709..6dff5a7145bc 100644 --- a/net/ipv4/inet_connection_sock.c +++ b/net/ipv4/inet_connection_sock.c @@ -1070,8 +1070,8 @@ static void reqsk_timer_handler(struct timer_list *t) * hold another count to prevent use-after-free and * call reqsk_put() just before return. */ - refcount_set(&nreq->rsk_refcnt, 2 + 1); timer_setup(&nreq->rsk_timer, reqsk_timer_handler, TIMER_PINNED); + refcount_set_release(&nreq->rsk_refcnt, 2 + 1); reqsk_queue_migrated(&inet_csk(nsk)->icsk_accept_queue, req); req = nreq; @@ -1415,7 +1415,7 @@ struct sock *inet_csk_complete_hashdance(struct sock *sk, struct sock *child, goto child_put; } - refcount_set(&nreq->rsk_refcnt, 1); + refcount_set_release(&nreq->rsk_refcnt, 1); if (inet_csk_reqsk_queue_add(sk, nreq, child)) { __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMIGRATEREQSUCCESS); reqsk_migrate_reset(req); @@ -1469,7 +1469,7 @@ void inet_csk_listen_stop(struct sock *sk) if (nsk) { nreq = inet_reqsk_clone(req, nsk); if (nreq) { - refcount_set(&nreq->rsk_refcnt, 1); + refcount_set_release(&nreq->rsk_refcnt, 1); rcu_read_lock(); if (inet_csk_reqsk_queue_add(nsk, nreq, child)) { diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c index b0f0c7c45917..866c758af08f 100644 --- a/net/ipv4/inet_hashtables.c +++ b/net/ipv4/inet_hashtables.c @@ -510,7 +510,7 @@ struct sock *__inet_lookup_established(const struct net *net, if (sk->sk_hash != hash) continue; if (likely(inet_match(net, sk, acookie, ports, dif, sdif))) { - if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt))) + if (unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt))) goto out; if (unlikely(hlist_nulls_unhashed_lockless(&sk->sk_nulls_node) || !inet_match(net, sk, acookie, ports, dif, diff --git a/net/ipv4/inet_timewait_sock.c b/net/ipv4/inet_timewait_sock.c index cf84a3c7de14..943693040a8c 100644 --- a/net/ipv4/inet_timewait_sock.c +++ b/net/ipv4/inet_timewait_sock.c @@ -139,7 +139,7 @@ void inet_twsk_hashdance_schedule(struct inet_timewait_sock *tw, * Also note that after this point, we lost our implicit reference * so we are not allowed to use tw anymore. */ - refcount_set(&tw->tw_refcnt, 3); + refcount_set_release(&tw->tw_refcnt, 3); /* Ensure tw_refcnt has been set before tw is published. * smp_wmb() provides the necessary memory barrier to enforce this @@ -324,7 +324,7 @@ void inet_twsk_purge(struct inet_hashinfo *hashinfo) if (refcount_read(&sock_net(sk)->ns.count)) continue; - if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt))) + if (unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt))) continue; if (refcount_read(&sock_net(sk)->ns.count)) { diff --git a/net/ipv4/syncookies.c b/net/ipv4/syncookies.c index 153559eb679f..bba484725a1e 100644 --- a/net/ipv4/syncookies.c +++ b/net/ipv4/syncookies.c @@ -204,7 +204,7 @@ struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb, child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst, NULL, &own_req, NULL); if (child) { - refcount_set(&req->rsk_refcnt, 1); + refcount_set_release(&req->rsk_refcnt, 1); sock_rps_save_rxhash(child, skb); if (rsk_drop_req(req)) { diff --git a/net/ipv4/tcp_fastopen.c b/net/ipv4/tcp_fastopen.c index 9c1173f1bddd..9764f72aeaf0 100644 --- a/net/ipv4/tcp_fastopen.c +++ b/net/ipv4/tcp_fastopen.c @@ -278,7 +278,7 @@ static struct sock *tcp_fastopen_create_child(struct sock *sk, tcp_reset_xmit_timer(child, ICSK_TIME_RETRANS, req->timeout, false); - refcount_set(&req->rsk_refcnt, 2); + refcount_set_release(&req->rsk_refcnt, 2); sk_mark_napi_id_set(child, skb); diff --git a/net/ipv6/inet6_hashtables.c b/net/ipv6/inet6_hashtables.c index 70633fed6e6a..dee00ba290f6 100644 --- a/net/ipv6/inet6_hashtables.c +++ b/net/ipv6/inet6_hashtables.c @@ -71,7 +71,7 @@ struct sock *__inet6_lookup_established(const struct net *net, continue; if (!inet6_match(net, sk, saddr, daddr, ports, dif, sdif)) continue; - if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt))) + if (unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt))) goto out; if (unlikely(hlist_nulls_unhashed_lockless(&sk->sk_nulls_node) || -- 2.56.1 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel