From patchwork Fri May 19 13:33:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13248357 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3F6C6DDD2 for ; Fri, 19 May 2023 13:34:22 +0000 (UTC) Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC955C2 for ; Fri, 19 May 2023 06:34:20 -0700 (PDT) Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-96f683e8855so103005066b.2 for ; Fri, 19 May 2023 06:34:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684503259; x=1687095259; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gKg0AFt/2baDk2QwOs1gDGEvUDueTmQNCWQTAs1fOBA=; b=iAKPH7FI6sHFsjAiSB9F8ngNygYgqVilqUxDUm2b3DdB3jFuzAdbccv3IzF8GbNvbV KBPPb8I5ICAB7YRDkZRiP6SNhZoK6ApaarLlJ/uSdxaaqwVsb6jRIMwChMTTTnimzNnR G1xuiCwiEXMc5obaUPM6odiTn6E6L+1oQYsp4Cg42Dezn128Vm2EBJhK+yxksOtTHFWG iwFh5LR5GNgPR/CeYzJKRu0iVKK39G859t0DiRf8GjmpeOtBiyI2xRZr8QYvAJZfm2q5 QuxY9mZu5+jTEuwbJS9IXQit0Npiru7AvDh9vEwefUXG4lUQIT7gf7BP/UXoK2xgCUOt jMew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684503259; x=1687095259; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gKg0AFt/2baDk2QwOs1gDGEvUDueTmQNCWQTAs1fOBA=; b=CDsc4221qbBy2n5leE23/DTMMfl5wiL1fKv28/FgNgMIAIZV6cDWsD7xUBllOkAdUj aGLFgDmmaZfOWAPqY8kyDb8ZZ6XsBc9FJXuC+l0xqjZZNRM0/sXcRkBfo8FRg/AE0auC rz+cFsZBY42LrMl5Ootnn/U9eMLRvYp7gdIWF9dDxyFmOmW6YtUm9DV1L1UHL4oz9zyh 6f9sOMakJUN8zU+exg4RPW2k7XWY8SK8Arft3Fbe9S/FHZGWZ7sWVs8Ts7OYfAIrNoNo yMSdcYL8tX6rRkLbwcAjrNCM52tNwYO88inGuvGkLCtqwf49cz1cDQY3u4w81fA0FjeD +T+g== X-Gm-Message-State: AC+VfDy3nXEHAr00AzA+02WPgJfRRhSf8N11uYPKeHTw+PzEpyEPg7T8 i56VgtRFLDDDykmA95FY6rEIu59M4b0= X-Google-Smtp-Source: ACHHUZ7l2/0w3RytZGN8Ct5ytsd83W/4bYtoNqt7A7x9zw68umY6EE+rHaxqjRza10qU3i2k7r+VIA== X-Received: by 2002:a17:906:fe45:b0:94a:5d5c:fe6f with SMTP id wz5-20020a170906fe4500b0094a5d5cfe6fmr1570967ejb.47.1684503258793; Fri, 19 May 2023 06:34:18 -0700 (PDT) Received: from 127.com ([2620:10d:c092:600::2:a04d]) by smtp.gmail.com with ESMTPSA id l7-20020a170906938700b00947ed087a2csm2279270ejx.154.2023.05.19.06.34.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 May 2023 06:34:18 -0700 (PDT) From: Pavel Begunkov To: netdev@vger.kernel.org, edumazet@google.com, davem@davemloft.net, dsahern@kernel.org, pabeni@redhat.com, kuba@kernel.org Cc: Pavel Begunkov Subject: [PATCH net-next 1/2] net/tcp: optimise locking for blocking splice Date: Fri, 19 May 2023 14:33:04 +0100 Message-Id: X-Mailer: git-send-email 2.40.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: kuba@kernel.org Even when tcp_splice_read() reads all it was asked for, for blocking sockets it'll release and immediately regrab the socket lock, loop around and break on the while check. Check tss.len right after we adjust it, and return if we're done. That saves us one release_sock(); lock_sock(); pair per successful blocking splice read. Signed-off-by: Pavel Begunkov --- net/ipv4/tcp.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 4d6392c16b7a..bf7627f37e69 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -789,13 +789,15 @@ ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos, */ if (unlikely(*ppos)) return -ESPIPE; + if (unlikely(!tss.len)) + return 0; ret = spliced = 0; lock_sock(sk); timeo = sock_rcvtimeo(sk, sock->file->f_flags & O_NONBLOCK); - while (tss.len) { + while (true) { ret = __tcp_splice_read(sk, &tss); if (ret < 0) break; @@ -835,10 +837,10 @@ ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos, } continue; } - tss.len -= ret; spliced += ret; + tss.len -= ret; - if (!timeo) + if (!tss.len || !timeo) break; release_sock(sk); lock_sock(sk); From patchwork Fri May 19 13:33:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13248358 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 42CC6DDD2 for ; Fri, 19 May 2023 13:34:23 +0000 (UTC) Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E27B12C for ; Fri, 19 May 2023 06:34:21 -0700 (PDT) Received: by mail-ej1-x62e.google.com with SMTP id a640c23a62f3a-96f850b32caso6054966b.3 for ; Fri, 19 May 2023 06:34:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684503260; x=1687095260; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Rd+XFWzERIuNvnwKCj7UX3DHrtWrVL2BPTWeu2EKRas=; b=CIWT6L5Mh2pPJIiJWEGFcjrDPFYbUjF7oO2hB+UPwGC6rXCny43PE9G0O1/IdJaXEH VoDHRcbo4YcKaRhB7cBpU4JgLa0kQnciIJ0vZP9jzsFffML+9Q3p8qEfQ/e/T3/TxVIV v+Woe80Lw17i5DFVIp6AqaV0huJeq7Nmh9V6+fL58OxFW21RQBPbS/3nU7t46CFMFTAX 4Wc7S9YtLfCwHTuOjS0b8KbUL0tv6KBCUsoynoVELqHqGyIeYyYJ7oa1FneJ20jZqHzZ 2rSyM85+PL6DxzhWHrozG81QJLg9y9imsVm7uyQLJ5LKowTkojmDlsuIGafCUuLhtgwb 2AKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684503260; x=1687095260; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Rd+XFWzERIuNvnwKCj7UX3DHrtWrVL2BPTWeu2EKRas=; b=GgPBHuqqeEvSk14TMPFkP8B25uNeLDWgGS8MnkF5LZS7ed9iL6/GCdozgzdEG1eVzF hpqItc87kGYyZ7se6LpNnF0vIgHC8Fa2lhxXF49c8IZY9Yi5c3d26H57LPkuzS+ZqHRQ 77ADgSUtRzgiaKsLEAfaPWPBpGPELyreBwdsiBmt4N6snVTtg4YXSgrV94R2ZKBHoKQi beC2LMaGlfY0/TmCgWdZhY8xpGlHZYDER2LVJ/6QVX35v8GEasPA6eONeCETGedKQexF OoqBzzzIIqxdda42YFFmxnhi91g6Mr3pKcBDmK/Lrovq7msQ+HFJ98BRr1T5gGa8NRGS QCEQ== X-Gm-Message-State: AC+VfDwD3PtAhx9IfBTAbtBj4SYGhpF06GwKQ4rsh4jscjvPmk+56/Hq zHQPwVnxMZxGWKTgZe/Z9PrUw6UeaiA= X-Google-Smtp-Source: ACHHUZ7D+Zmu/tN9LHKi3YKtCZ4BYeNgeU6XFojFiNTto7q/KeDPHPkJIZhlmrJ54AYLg/k2Roz+7g== X-Received: by 2002:a17:907:7da7:b0:96a:e022:6486 with SMTP id oz39-20020a1709077da700b0096ae0226486mr2076119ejc.2.1684503259420; Fri, 19 May 2023 06:34:19 -0700 (PDT) Received: from 127.com ([2620:10d:c092:600::2:a04d]) by smtp.gmail.com with ESMTPSA id l7-20020a170906938700b00947ed087a2csm2279270ejx.154.2023.05.19.06.34.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 May 2023 06:34:19 -0700 (PDT) From: Pavel Begunkov To: netdev@vger.kernel.org, edumazet@google.com, davem@davemloft.net, dsahern@kernel.org, pabeni@redhat.com, kuba@kernel.org Cc: Pavel Begunkov Subject: [PATCH net-next 2/2] net/tcp: optimise non error splice paths Date: Fri, 19 May 2023 14:33:05 +0100 Message-Id: <0ad3bdc1e5e4ccef2ee0b36fb3b068e61b1b1a53.1684501922.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: kuba@kernel.org Move post __tcp_splice_read() error checking inside the "nothing to read" block. That removes an extra if from the path where it has successfully spliced some bytes. Signed-off-by: Pavel Begunkov --- net/ipv4/tcp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index bf7627f37e69..0139b2c70ed4 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -799,9 +799,9 @@ ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos, timeo = sock_rcvtimeo(sk, sock->file->f_flags & O_NONBLOCK); while (true) { ret = __tcp_splice_read(sk, &tss); - if (ret < 0) - break; - else if (!ret) { + if (ret <= 0) { + if (unlikely(ret < 0)) + break; if (spliced) break; if (sock_flag(sk, SOCK_DONE))