Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2701581rwb; Fri, 9 Dec 2022 05:29:06 -0800 (PST) X-Google-Smtp-Source: AA0mqf70Ro9u8J/9IzmWW3WwLvAgjv3n1+a8gxoN49AgutyKJDSSu9Wd0EEYUvUdskE8q/sb/Qqj X-Received: by 2002:a17:906:e283:b0:7c0:be4d:46d6 with SMTP id gg3-20020a170906e28300b007c0be4d46d6mr4620636ejb.59.1670592545871; Fri, 09 Dec 2022 05:29:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670592545; cv=none; d=google.com; s=arc-20160816; b=Q9k7h3uYDtfjb5WNRliBOO36eO/ws0xgwfPZ2OR0Eh4wwQSM6lR9Nt11a1j2YBhasf /ubdE0tXDmkdQWPWyL8zMiOKqJWuTunF8N4g795LOfJkwZYxRMxa+WqOhUAXMU39ubSa XZ1LqeEWEP8gxjhhigtB089py1lASbM2EIUgSnbDQXnvzDqM3EYd0BG4R5hg+wu7ABEc SoaDY1l4eVsk9qaMnb53pHDmPEFvsPWoDzbPUnBF3f8JQZLJkiFh4zk67NQqjhKB5446 XjhyXi0xdjVWZk70GaiEzrQyhElnV3sgyIz8jG19kh9BsPpOt5MtvT85PiOXMSRPY5Fk dahQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=9QQYI9IjkkIUTVxaX3edo06SmbOZzGyRFi5ji2As464=; b=GgMBOQ42iBK2XrdxBtAqp8kiDXPG3a9I5BWQHYMe/e8aGgWNQTa2T5kZddLwFmkdCG lgfdaYC46+oVd0SwD3L5MJ+QtMJh0l1Glo4LHddmaRxg21LCJ/fzRw3Tjsu1S29bjUAF sp2OXDjoAi3fpqVEzgBO5OE8r/9mBaniHveW6MweP/bKXReenWSOfZ2iwkCzB0pojVlS DFOj8btyEVlLxRxWdL50Evw6MLWV9cbNkbAc3jIMj2WbdSK/OpJpLvhrbrYRxeSEv6uj swPRGT6xHI6WBN//JYvLQKLgdfkDeOXDmaE8oBHfvfW10rpjjx5weEx/GUIyRnD64FQ2 ehWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dQvBZqWs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y22-20020a170906449600b007c0e3900f27si954019ejo.43.2022.12.09.05.28.47; Fri, 09 Dec 2022 05:29:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dQvBZqWs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229714AbiLIMLI (ORCPT + 75 others); Fri, 9 Dec 2022 07:11:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229735AbiLIMKw (ORCPT ); Fri, 9 Dec 2022 07:10:52 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5604A389FF for ; Fri, 9 Dec 2022 04:09:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670587796; 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=9QQYI9IjkkIUTVxaX3edo06SmbOZzGyRFi5ji2As464=; b=dQvBZqWse9q/I4IxdWmRKRRf7bK8A2HTMX9775i1oyauagq0qrhkaze+n87mjS9itP7gCi 7sUMt8U3nz1qs27GLFPzPzp+lYI1MCkA0fHEC0C3sMzJvgWWqsWSeJOPFH4E78GYoJXXzs cTShJ/S8hw1RteAm9q1W5pzT1y0e00c= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-164-LhF4NpVbNM6eYPnOIdrnlA-1; Fri, 09 Dec 2022 07:09:54 -0500 X-MC-Unique: LhF4NpVbNM6eYPnOIdrnlA-1 Received: by mail-wm1-f72.google.com with SMTP id m38-20020a05600c3b2600b003d1fc5f1f80so3237413wms.1 for ; Fri, 09 Dec 2022 04:09:53 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=9QQYI9IjkkIUTVxaX3edo06SmbOZzGyRFi5ji2As464=; b=SapJ72nim1/HjVi4AuInbOviBLTUZDJ98k7cZ3LoXosJqG/Eyd11FJjpRQReXkte98 8LyxIGAfUkbWt5AkNP0RN6LfYFzV1tJKEsBs1lhdQnJQWujEjLvlKVxj9YqF86N8rZjN TT1ngNdvQTGEonfLKYFvRfEAhhdYx1MLrhUKnEWgqwIXiNT2B5wOfTvrlhPOHy3Uj6B7 KlUqMkmf6/u2VncKufShwn2tBPDdJM5PPSORS11zWzHKPLRCzSp2odDoEdyjY7R8Aa+c F5SAsg5jQjT4sKPWbIOM14zdxuejHHgOuLN8fANVSWz9zEaJ3gKFwJctBBX0k9eN8mBl u+Xw== X-Gm-Message-State: ANoB5plmtDskHSlLcHeCLbDN3WDxhEOc4JHPzhMgX/6hsg6kTeYHfkxK 6rvZP+ldpzcPHhkDCxEzZ48AGqagP2CcA/hH92DbfeUyugC2DC2e/qquFWoupFGFpFDkxhviMeB i/llDtegPcBJ39mx6/BaSiXbJ X-Received: by 2002:a05:600c:3d8f:b0:3cf:a18d:399c with SMTP id bi15-20020a05600c3d8f00b003cfa18d399cmr4694842wmb.1.1670587792796; Fri, 09 Dec 2022 04:09:52 -0800 (PST) X-Received: by 2002:a05:600c:3d8f:b0:3cf:a18d:399c with SMTP id bi15-20020a05600c3d8f00b003cfa18d399cmr4694822wmb.1.1670587792506; Fri, 09 Dec 2022 04:09:52 -0800 (PST) Received: from gerbillo.redhat.com (146-241-106-22.dyn.eolo.it. [146.241.106.22]) by smtp.gmail.com with ESMTPSA id iv7-20020a05600c548700b003d1f3e9df3csm9221828wmb.7.2022.12.09.04.09.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Dec 2022 04:09:52 -0800 (PST) Message-ID: Subject: Re: [PATCH v1 1/3] net: Introduce sk_use_task_frag in struct sock. From: Paolo Abeni To: Benjamin Coddington , netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Soheil Hassas Yeganeh , Shakeel Butt , Pavel Begunkov , Kuniyuki Iwashima , Maciej =?UTF-8?Q?=C5=BBenczykowski?= , Menglong Dong , Akhmat Karakotov , Alexander Duyck Date: Fri, 09 Dec 2022 13:09:50 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.42.4 (3.42.4-2.fc35) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 2022-11-21 at 08:35 -0500, Benjamin Coddington wrote: > From: Guillaume Nault > > Sockets that can be used while recursing into memory reclaim, like > those used by network block devices and file systems, mustn't use > current->task_frag: if the current process is already using it, then > the inner memory reclaim call would corrupt the task_frag structure. > > To avoid this, sk_page_frag() uses ->sk_allocation to detect sockets > that mustn't use current->task_frag, assuming that those used during > memory reclaim had their allocation constraints reflected in > ->sk_allocation. > > This unfortunately doesn't cover all cases: in an attempt to remove all > usage of GFP_NOFS and GFP_NOIO, sunrpc stopped setting these flags in > ->sk_allocation, and used memalloc_nofs critical sections instead. > This breaks the sk_page_frag() heuristic since the allocation > constraints are now stored in current->flags, which sk_page_frag() > can't read without risking triggering a cache miss and slowing down > TCP's fast path. > > This patch creates a new field in struct sock, named sk_use_task_frag, > which sockets with memory reclaim constraints can set to false if they > can't safely use current->task_frag. In such cases, sk_page_frag() now > always returns the socket's page_frag (->sk_frag). The first user is > sunrpc, which needs to avoid using current->task_frag but can keep > ->sk_allocation set to GFP_KERNEL otherwise. > > Eventually, it might be possible to simplify sk_page_frag() by only > testing ->sk_use_task_frag and avoid relying on the ->sk_allocation > heuristic entirely (assuming other sockets will set ->sk_use_task_frag > according to their constraints in the future). > > The new ->sk_use_task_frag field is placed in a hole in struct sock and > belongs to a cache line shared with ->sk_shutdown. Therefore it should > be hot and shouldn't have negative performance impacts on TCP's fast > path (sk_shutdown is tested just before the while() loop in > tcp_sendmsg_locked()). > > Link: https://lore.kernel.org/netdev/b4d8cb09c913d3e34f853736f3f5628abfd7f4b6.1656699567.git.gnault@redhat.com/ > Signed-off-by: Guillaume Nault > --- > include/net/sock.h | 11 +++++++++-- > net/core/sock.c | 1 + > 2 files changed, 10 insertions(+), 2 deletions(-) > > diff --git a/include/net/sock.h b/include/net/sock.h > index d08cfe190a78..ffba9e95470d 100644 > --- a/include/net/sock.h > +++ b/include/net/sock.h > @@ -318,6 +318,9 @@ struct sk_filter; > * @sk_stamp: time stamp of last packet received > * @sk_stamp_seq: lock for accessing sk_stamp on 32 bit architectures only > * @sk_tsflags: SO_TIMESTAMPING flags > + * @sk_use_task_frag: allow sk_page_frag() to use current->task_frag. > + Sockets that can be used under memory reclaim should > + set this to false. > * @sk_bind_phc: SO_TIMESTAMPING bind PHC index of PTP virtual clock > * for timestamping > * @sk_tskey: counter to disambiguate concurrent tstamp requests > @@ -504,6 +507,7 @@ struct sock { > #endif > u16 sk_tsflags; > u8 sk_shutdown; > + bool sk_use_task_frag; > atomic_t sk_tskey; > atomic_t sk_zckey; I think the above should be fine from a data locality PoV, as the used cacheline should be hot at sk_page_frag_refill() usage time, as sk_tsflags has been accessed just before. @Eric, does the above fit with the planned sock fields reordering? Jakub noted we could use a bitfield here to be future proof for additional flags addition. I think in this specific case a bool is preferable, because we actually wont to discourage people to add more of such flags, and the search for holes (or the bool -> bitflag conversion) should give to such eventual future changes some additional thoughts. Thanks! Paolo