From mboxrd@z Thu Jan 1 00:00:00 1970 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 39FD4C433EF for ; Thu, 12 May 2022 22:53:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359331AbiELWxN (ORCPT ); Thu, 12 May 2022 18:53:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356737AbiELWxM (ORCPT ); Thu, 12 May 2022 18:53:12 -0400 Received: from mail-pg1-x530.google.com (mail-pg1-x530.google.com [IPv6:2607:f8b0:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A6AB5268646 for ; Thu, 12 May 2022 15:53:08 -0700 (PDT) Received: by mail-pg1-x530.google.com with SMTP id h186so3224261pgc.3 for ; Thu, 12 May 2022 15:53:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fastly.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=+VaX5bEkt76Upj3MV7wwCv/6t4DebBt0Na7sEL1aFWk=; b=RLIUxCf7DyHsfTc9r+srDbeWsNAe7EbL+jFZYOZRR4BWW+qLVAuoHKGuWgN/hyuWSh XqGnKskIo2YPhI43QZSG0tP+o/2rDa+mHXKqbquaFpk5J+QzlCr+uiLn2DlvD54vaeol RRsMufH6It/H/btgUCKnq7wQ4dOP5Bq0BUSjA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=+VaX5bEkt76Upj3MV7wwCv/6t4DebBt0Na7sEL1aFWk=; b=i5VsXfd3E5LBJSHnU4WJl4m06Mcf1DFPTwFuY1GZXgh05w7826tQPy7kbCPsOqZauc gnSmqYmQgxPxoj3/gMOrC31JuOEvl30mV9FqEjoNnnHOtYKVlm+D7SLlNNiDVeSPJkJ1 ZdNB9jgWhxpLnVAxG9asGZcnuBx8FUAHZ1XZkJ2UISXULjpLdIFIIJmqyFBmkZ2hq8ZT oBZjcv0LwChe7sEz5OvxokEvhEDHYRmcExyHzYmfRdEm+r8YwGYCcOvQEEzJpyt79GnP AS5h2L8/Pz8loDPa9hrsesB//Q6/onlVHxKaQhMzNcILBSLYOoZqSHgloXPB3cEa9VfO wpEw== X-Gm-Message-State: AOAM533DJYVjKV+wrfxPMDjRoWqF26UYyJ9RpCQe+zyXOu4pGCUHhUAS +YHfAHkJhSWwhlF9hyIAz92wxQ== X-Google-Smtp-Source: ABdhPJw8D9Yn2NvhdDbwVV4UNIZuZQOhxhAbXeSXhinVOmBc47nuHD4rdSNx10K2su40x/WsCjhq2Q== X-Received: by 2002:a63:a553:0:b0:3db:48a0:f506 with SMTP id r19-20020a63a553000000b003db48a0f506mr1444062pgu.456.1652395988124; Thu, 12 May 2022 15:53:08 -0700 (PDT) Received: from fastly.com (c-73-223-190-181.hsd1.ca.comcast.net. [73.223.190.181]) by smtp.gmail.com with ESMTPSA id w20-20020a170902ca1400b0015f391f56b7sm358600pld.305.2022.05.12.15.53.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 May 2022 15:53:07 -0700 (PDT) Date: Thu, 12 May 2022 15:53:05 -0700 From: Joe Damato To: Jakub Kicinski Cc: netdev@vger.kernel.org, davem@davemloft.net, linux-kernel@vger.kernel.org, x86@kernel.org Subject: Re: [RFC,net-next,x86 0/6] Nontemporal copies in unix socket write path Message-ID: <20220512225302.GA74948@fastly.com> References: <1652241268-46732-1-git-send-email-jdamato@fastly.com> <20220511162520.6174f487@kernel.org> <20220512010153.GA74055@fastly.com> <20220512124608.452d3300@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220512124608.452d3300@kernel.org> User-Agent: Mutt/1.5.24 (2015-08-30) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 12, 2022 at 12:46:08PM -0700, Jakub Kicinski wrote: > On Wed, 11 May 2022 18:01:54 -0700 Joe Damato wrote: > > > Is there a practical use case? > > > > Yes; for us there seems to be - especially with AMD Zen2. I'll try to > > describe such a setup and my synthetic HTTP benchmark results. > > > > Imagine a program, call it storageD, which is responsible for storing and > > retrieving data from a data store. Other programs can request data from > > storageD via communicating with it on a Unix socket. > > > > One such program that could request data via the Unix socket is an HTTP > > daemon. For some client connections that the HTTP daemon receives, the > > daemon may determine that responses can be sent in plain text. > > > > In this case, the HTTP daemon can use splice to move data from the unix > > socket connection with storageD directly to the client TCP socket via a > > pipe. splice saves CPU cycles and avoids incurring any memory access > > latency since the data itself is not accessed. > > > > Because we'll use splice (instead of accessing the data and potentially > > affecting the CPU cache) it is advantageous for storageD to use NT copies > > when it writes to the Unix socket to avoid evicting hot data from the CPU > > cache. After all, once the data is copied into the kernel on the unix > > socket write path, it won't be touched again; only spliced. > > > > In my synthetic HTTP benchmarks for this setup, we've been able to increase > > network throughput of the the HTTP daemon by roughly 30% while reducing > > the system time of storageD. We're still collecting data on production > > workloads. > > > > The motivation, IMHO, is very similar to the motivation for > > NETIF_F_NOCACHE_COPY, as far I understand. > > > > In some cases, when an application writes to a network socket the data > > written to the socket won't be accessed again once it is copied into the > > kernel. In these cases, NETIF_F_NOCACHE_COPY can improve performance and > > helps to preserve the CPU cache and avoid evicting hot data. > > > > We get a sizable benefit from this option, too, in situations where we > > can't use splice and have to call write to transmit data to client > > connections. We want to get the same benefit of NETIF_F_NOCACHE_COPY, but > > when writing to Unix sockets as well. > > > > Let me know if that makes it more clear. > > Makes sense, thanks for the explainer. > > > > The patches look like a lot of extra indirect calls. > > > > Yup. As I mentioned in the cover letter this was mostly a PoC that seems to > > work and increases network throughput in a real world scenario. > > > > If this general line of thinking (NT copies on write to a Unix socket) is > > acceptable, I'm happy to refactor the code however you (and others) would > > like to get it to an acceptable state. > > My only concern is that in post-spectre world the indirect calls are > going to be more expensive than an branch would be. But I'm not really > a mirco-optimization expert :) Makes sense; neither am I, FWIW :) For whatever reason, on AMD Zen2 it seems that using non-temporal instructions when copying data sizes above the L2 size is a huge performance win (compared to the kernel's normal temporal copy code) even if that size fits in L3. This is why both NETIF_F_NOCACHE_COPY and MSG_NTCOPY from this series seem to have such a large, measurable impact in the contrived benchmark I included in the cover letter and also in synthetic HTTP workloads. I'll plan on including numbers from the benchmark program on a few other CPUs I have access to in the cover letter for any follow-up RFCs or revisions. As a data point, there has been similar-ish work done in glibc [1] to determine when non-temporal copies should be used on Zen2 based on the size of the copy. I'm certainly not a micro-arch expert by any stretch, but the glibc work plus the benchmark results I've measured seem to suggest that NT-copies can be very helpful on Zen2. Two questions for you: 1. Do you have any strong opinions on the sendmsg flag vs a socket option? 2. If I can think of a way to avoid the indirect calls, do you think this series is ready for a v1? I'm not sure if there's anything major that needs to be addressed aside from the indirect calls. I'll include some documentation and cosmetic cleanup in the v1, as well. Thanks, Joe [1]: https://sourceware.org/pipermail/libc-alpha/2020-October/118895.html