From patchwork Thu Apr 28 10:58:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 12830461 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F41FEC433FE for ; Thu, 28 Apr 2022 10:59:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345267AbiD1LDC (ORCPT ); Thu, 28 Apr 2022 07:03:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345216AbiD1LCo (ORCPT ); Thu, 28 Apr 2022 07:02:44 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E37FA144F; Thu, 28 Apr 2022 03:58:57 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id 1-20020a05600c248100b00393fbf11a05so4051165wms.3; Thu, 28 Apr 2022 03:58:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=s+kT6lElZGiyXPNFYxpX9dPigb8JGxp1Ty3OtCWIYZk=; b=KAsXqQiXisUum9sr3vDWSaGkLIRSGm9RTKRR66bEjq2G75ZFXff4MTatYHQFZiRovd X4d1MfhVT76VMxJ1LBwPkeRJiJ3cCOpChI6+YON/M6w6JEEhpdOvJfE6CfEcjbsAWoDV jWy4P9D7PshHJynG5LjSSuIIzeSq9K3vRNMnBGGfdsmLhOx0J3HHWqHsQ1fe1ATH7iqn Gw3GMzcL96MMruiho87+F/RQmG3NavWTDdsGR5xwunqnfcFotiRHetAemSGTHOFXsqf7 xReGqk9clcO5qDR1t9GyVoDelpgJGe2CNgOI0sgPNb2biAbp7Uz8ztMr+mWtN2O6E1fv ZsZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=s+kT6lElZGiyXPNFYxpX9dPigb8JGxp1Ty3OtCWIYZk=; b=Ko9OKZB9YhMWOyG1WNaYpGeZ/r8MsAsJ73OzixH72ZfsjvzPBN3Qi/jhtcQ4H6Y87w ldDRLhNkBeF0DWtrA9r1Od1AjygZ3GwUPt8lmNDz5jLV0FIvapsuikpplGKryWnmBzgu /TmY/OiZfQ/WnARjX+mK8VKfMdVjml39moYcxwKqBagerP6PeTRc5zX5IvaTuzjsWJze YanoqEqxYPkiTUb3g7HyhCWQitNHQcfTS+Ojt6Li1wmRieCqT4xgsyY3t1vyzBlwCl1E c/IB8uFOPHQNCop8f1W/NzgluUKCnaXSGixU+3iQlztvDGI5mbBIWbnO2C/YdLy5yLUs xprg== X-Gm-Message-State: AOAM5303OZ8RLaoLvq3ggzLDKwETKPn4TePB38rRlfvRAyUphjzUgBOk WeHNlZMa9+BxJvqWkbaTiIZuX2kWlIU= X-Google-Smtp-Source: ABdhPJwX28qMZnoY4F1Mv77Ag5sVbXb3Rj3kDgaPd57XubYoJBHS5TrQISuueViQvDdvqOi0U5a6Uw== X-Received: by 2002:a05:600c:ad1:b0:394:1585:a164 with SMTP id c17-20020a05600c0ad100b003941585a164mr1597209wmr.101.1651143535845; Thu, 28 Apr 2022 03:58:55 -0700 (PDT) Received: from 127.0.0.1localhost (82-132-230-8.dab.02.net. [82.132.230.8]) by smtp.gmail.com with ESMTPSA id p4-20020a1c5444000000b00391ca5976c8sm4628139wmi.0.2022.04.28.03.58.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 03:58:55 -0700 (PDT) From: Pavel Begunkov To: netdev@vger.kernel.org, "David S . Miller" , Jakub Kicinski Cc: David Ahern , Eric Dumazet , linux-kernel@vger.kernel.org, Pavel Begunkov Subject: [PATCH net-next 1/3] sock: dedup sock_def_write_space wmem_alloc checks Date: Thu, 28 Apr 2022 11:58:17 +0100 Message-Id: <95d349fee6504dc8bc1e8123d80915d1fe9ce91a.1650891417.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Except for minor rounding differences the first ->sk_wmem_alloc test in sock_def_write_space() is a hand coded version of sock_writeable(). Replace it with the helper, and also kill the following if duplicating the check. Signed-off-by: Pavel Begunkov --- net/core/sock.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/net/core/sock.c b/net/core/sock.c index 29abec3eabd8..976ff871969e 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -3194,15 +3194,14 @@ static void sock_def_write_space(struct sock *sk) /* Do not wake up a writer until he can make "significant" * progress. --DaveM */ - if ((refcount_read(&sk->sk_wmem_alloc) << 1) <= READ_ONCE(sk->sk_sndbuf)) { + if (sock_writeable(sk)) { wq = rcu_dereference(sk->sk_wq); if (skwq_has_sleeper(wq)) wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND); /* Should agree with poll, otherwise some programs break */ - if (sock_writeable(sk)) - sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); + sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); } rcu_read_unlock(); From patchwork Thu Apr 28 10:58:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 12830462 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0C9E6C433F5 for ; Thu, 28 Apr 2022 10:59:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345227AbiD1LDF (ORCPT ); Thu, 28 Apr 2022 07:03:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344894AbiD1LCs (ORCPT ); Thu, 28 Apr 2022 07:02:48 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 920AAA1457; Thu, 28 Apr 2022 03:58:59 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id q23so6255022wra.1; Thu, 28 Apr 2022 03:58:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SrhHhC4OSzNzBDK0GAKFTGso0tFf64ad1cEfyYa4l14=; b=n5Tt+aHeTdA3GyiiN/j4yGT4hJ5/W1FWa95Mq1/ALsVVWONE8D4sN5YjHHd2L+3tZb H4ZxhqjR0QMmRBYLdKt1w8ZtRTnemQBlUH/xvgzJwghSgRAe2fLHRrDdEKf8A+SlPQHF IrwcTGZz9/d5OlFC8hFXswRdcoNFmp5Q9QXliD69zK+wZ9wDf+LXzGcF4aBGZnKxZ57V A+/YTcdPA9oE5laLAe8xn84zD0ygX4tFvloUaHuCzTVV+kIVSEL9lb2jthVffx60svD+ hG7KTrvWLie36ioh3tuQjDRNCkGcyOG5U8hf8f8gHmnnQuFCGfWu7ne+66imaUAH8rVL obRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SrhHhC4OSzNzBDK0GAKFTGso0tFf64ad1cEfyYa4l14=; b=wEdZEzD4uts4SCN2pqRwHRJmrZxQYCmdZwjxE7Zj4B5/ONIzx0U0CqFsyn1Zfy5REX Xg16QoeQuumfi4OdgwuRWZUXiX6owbPPTFEfbe7pyMpBGdWUxZXfgEG8JnhDPUtOI9vV lyvcdhEqV57jvgPGBOVnewbfnIXtHlnqOw+8RxiA9SFXvAr9RJ++H+fCB5EYwDEpRwHQ sIn3n8047tnJbjTu/AZrb1wvtFZzC3hxi1OqWgRJw1C/9g5KQXdCSgEMp+z24HHuSJoq DBgHOfubkeWM4h7wkCEcsagvqeS38mbimbunHxP0z7YCt8iRCFIEnz9yYEtA9QAvOpYH Oi+Q== X-Gm-Message-State: AOAM533zrvCGoMLL2lM5scEtw4skiNKZ+FpydDazuXyMJYbtg1pv5daS euk4cx8A7qZDopXVPNviP+WwYQaB77A= X-Google-Smtp-Source: ABdhPJzxkVXH4VMZUgFhULZAk+43zKGd1vXLm+7Vk9zs4sqRwQ80/Es+zJXS1CjGvGHsNFmPWX3RIg== X-Received: by 2002:a5d:64a3:0:b0:20a:ef5c:adfe with SMTP id m3-20020a5d64a3000000b0020aef5cadfemr6316488wrp.146.1651143537984; Thu, 28 Apr 2022 03:58:57 -0700 (PDT) Received: from 127.0.0.1localhost (82-132-230-8.dab.02.net. [82.132.230.8]) by smtp.gmail.com with ESMTPSA id p4-20020a1c5444000000b00391ca5976c8sm4628139wmi.0.2022.04.28.03.58.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 03:58:57 -0700 (PDT) From: Pavel Begunkov To: netdev@vger.kernel.org, "David S . Miller" , Jakub Kicinski Cc: David Ahern , Eric Dumazet , linux-kernel@vger.kernel.org, Pavel Begunkov Subject: [PATCH net-next 2/3] sock: optimise UDP sock_wfree() refcounting Date: Thu, 28 Apr 2022 11:58:18 +0100 Message-Id: <8b7878e1f57af29e4a693c588d3a0cc70dcf5003.1650891417.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org For non SOCK_USE_WRITE_QUEUE sockets, sock_wfree() (atomically) puts ->sk_wmem_alloc twice. It's needed to keep the socket alive while calling ->sk_write_space() after the first put. However, some sockets, such as UDP, are freed by RCU (i.e. SOCK_RCU_FREE) and use already RCU-safe sock_def_write_space(). Carve a fast path for such sockets, put down all refs in one go before calling sock_def_write_space() but guard the socket from being freed by an RCU read section. note: because TCP sockets are marked with SOCK_USE_WRITE_QUEUE it doesn't add extra checks in its path. Signed-off-by: Pavel Begunkov --- net/core/sock.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/net/core/sock.c b/net/core/sock.c index 976ff871969e..4ad4d6dd940e 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -146,6 +146,8 @@ static DEFINE_MUTEX(proto_list_mutex); static LIST_HEAD(proto_list); +static void sock_def_write_space(struct sock *sk); + /** * sk_ns_capable - General socket capability test * @sk: Socket to use a capability on or through @@ -2320,8 +2322,20 @@ void sock_wfree(struct sk_buff *skb) { struct sock *sk = skb->sk; unsigned int len = skb->truesize; + bool free; if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE)) { + if (sock_flag(sk, SOCK_RCU_FREE) && + sk->sk_write_space == sock_def_write_space) { + rcu_read_lock(); + free = refcount_sub_and_test(len, &sk->sk_wmem_alloc); + sock_def_write_space(sk); + rcu_read_unlock(); + if (unlikely(free)) + __sk_free(sk); + return; + } + /* * Keep a reference on sk_wmem_alloc, this will be released * after sk_write_space() call From patchwork Thu Apr 28 10:58:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 12830463 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CA2BDC433F5 for ; Thu, 28 Apr 2022 10:59:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345219AbiD1LDI (ORCPT ); Thu, 28 Apr 2022 07:03:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345099AbiD1LCu (ORCPT ); Thu, 28 Apr 2022 07:02:50 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85ACE9F3A1; Thu, 28 Apr 2022 03:59:01 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id m62so2713241wme.5; Thu, 28 Apr 2022 03:59:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=l7MRQY3AOdXRAcNH/9Z4xyz/tFFAixxWrYfyB94jPJ4=; b=bS/Df47ABvzpUrrBEZHO4AEMa9aD0mTGxqbkMhXx7MzhcRrQNxmOM+NvrEUKQnUDJi O8ai2W8akt3urkgm46EjCtZ0iAjamuwA65XKJ3fHNis0P0FO5aX+Fx56EAgw2nqe05pq ZLSPLTtReqTJ8Y2dYq98LP7tPooOOW204Spk1AW2cn9w5nA34lEO4o9MsqIWGUJWDvvf +yreBpuYnbkPwwU9azXNuPXw9E/VPcWVJ/7Z8YjEsTuJ1j1XlDN/ea/mkVwJxxcfbPia jqoTzthVPeVsO8SmXj/ksg3/wFT8yvtA83zYIVSbMpqz95V5P9dBskKiHErhN95AvCLv Ixzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=l7MRQY3AOdXRAcNH/9Z4xyz/tFFAixxWrYfyB94jPJ4=; b=orCJX2fGqwO2XFoOuXht0MF1jphXHS3MMdxE72DMRb9HpkmoOCQpS+ueZfgV+taory oH0mkKnTkH8jBE7QiTUma0qJtb3uERJp90ykVuVkYG9kUN1wEGpldUnNus2a/+9AWHIU TQMzcewoHtev0lDIMidcMhTyCzUnawuiNCAJ/yqd8XmdK2xHxt8Myi/FLtnHBjMChPr5 txqKPg1/4nBA9AuL+ERgnWihSKjszOy9mKmmcalUTiO8TuUcFNBnpc0R4Y++DEOx3UAq SiGip3ji0zAoTJCcg+re9B1A3VaxqWOyF045Sjm5IZY4bLyItxBzEyN8lliHQI0qEBHU w25Q== X-Gm-Message-State: AOAM530MuFRbL2x19s0Y+Qw4FL/bZhdDf1h/ubT5SCb/PRDSD0O6+JbX xJPyxC567H2jMySXAzdSV+mpNOb5I6A= X-Google-Smtp-Source: ABdhPJzh5xPJYIFuTf08eMtuhVLm3V7WRAIwzhlYMGI7mYBUFFcfnO3Wr7ivSW9Vmn9hCxCyyx1znQ== X-Received: by 2002:a05:600c:48a8:b0:393:ff87:faf0 with SMTP id j40-20020a05600c48a800b00393ff87faf0mr8094114wmp.88.1651143539844; Thu, 28 Apr 2022 03:58:59 -0700 (PDT) Received: from 127.0.0.1localhost (82-132-230-8.dab.02.net. [82.132.230.8]) by smtp.gmail.com with ESMTPSA id p4-20020a1c5444000000b00391ca5976c8sm4628139wmi.0.2022.04.28.03.58.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 03:58:59 -0700 (PDT) From: Pavel Begunkov To: netdev@vger.kernel.org, "David S . Miller" , Jakub Kicinski Cc: David Ahern , Eric Dumazet , linux-kernel@vger.kernel.org, Pavel Begunkov Subject: [PATCH net-next 3/3] sock: optimise sock_def_write_space barriers Date: Thu, 28 Apr 2022 11:58:19 +0100 Message-Id: <654332b3765fa728e1e149523bf7fb59c563a186.1650891417.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Now we have a separate path for sock_def_write_space() and can go one step further. When it's called from sock_wfree() we know that there is a preceding atomic for putting down ->sk_wmem_alloc. We can use it to replace to replace smb_mb() with a less expensive smp_mb__after_atomic(). It also removes an extra RCU read lock/unlock as a small bonus. Signed-off-by: Pavel Begunkov --- net/core/sock.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/net/core/sock.c b/net/core/sock.c index 4ad4d6dd940e..85d0b04c7bc5 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -146,6 +146,7 @@ static DEFINE_MUTEX(proto_list_mutex); static LIST_HEAD(proto_list); +static void sock_def_write_space_wfree(struct sock *sk); static void sock_def_write_space(struct sock *sk); /** @@ -2329,7 +2330,7 @@ void sock_wfree(struct sk_buff *skb) sk->sk_write_space == sock_def_write_space) { rcu_read_lock(); free = refcount_sub_and_test(len, &sk->sk_wmem_alloc); - sock_def_write_space(sk); + sock_def_write_space_wfree(sk); rcu_read_unlock(); if (unlikely(free)) __sk_free(sk); @@ -3221,6 +3222,29 @@ static void sock_def_write_space(struct sock *sk) rcu_read_unlock(); } +/* An optimised version of sock_def_write_space(), should only be called + * for SOCK_RCU_FREE sockets under RCU read section and after putting + * ->sk_wmem_alloc. + */ +static void sock_def_write_space_wfree(struct sock *sk) +{ + /* Do not wake up a writer until he can make "significant" + * progress. --DaveM + */ + if (sock_writeable(sk)) { + struct socket_wq *wq = rcu_dereference(sk->sk_wq); + + /* rely on refcount_sub from sock_wfree() */ + smp_mb__after_atomic(); + if (wq && waitqueue_active(&wq->wait)) + wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT | + EPOLLWRNORM | EPOLLWRBAND); + + /* Should agree with poll, otherwise some programs break */ + sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); + } +} + static void sock_def_destruct(struct sock *sk) { }