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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 687B7C4345F for ; Tue, 16 Apr 2024 15:51:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D31E56B0087; Tue, 16 Apr 2024 11:51:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CE1DF6B0088; Tue, 16 Apr 2024 11:51:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B81DE6B0089; Tue, 16 Apr 2024 11:51:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 99D2B6B0087 for ; Tue, 16 Apr 2024 11:51:12 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 506371C1261 for ; Tue, 16 Apr 2024 15:51:12 +0000 (UTC) X-FDA: 82015833984.14.20E2D9A Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) by imf26.hostedemail.com (Postfix) with ESMTP id 4415E140003 for ; Tue, 16 Apr 2024 15:51:10 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=NXRX3Jhl; spf=pass (imf26.hostedemail.com: domain of alexander.duyck@gmail.com designates 209.85.215.179 as permitted sender) smtp.mailfrom=alexander.duyck@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713282670; h=from:from:sender: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:dkim-signature; bh=ebpJMn/gZAY2DoBsMCYW/AsLUXHW/Jt0ZHuYBVYkNHo=; b=XWaILbaxEDcrHeszdUsLU+0aRpP4vw/wHyQW8dbkYxh33dU47OMpfSvrZaTYezmtcjC9hT im4/J9/jek5OKBGFva/ZjhBoV8meLNt4hmMjZMmiEqfIeb13xP9WCqto9hRuprLseu84Ms ymqZREpuwD4QFG/fuJ2xNVgrNsiE1ZY= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=NXRX3Jhl; spf=pass (imf26.hostedemail.com: domain of alexander.duyck@gmail.com designates 209.85.215.179 as permitted sender) smtp.mailfrom=alexander.duyck@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713282670; a=rsa-sha256; cv=none; b=5swNj2PeDNVmAvwWA3I8h1lGTf8AmsObYjI6I5zmqJH9/OLXctvL9yn6jtO81POrieHrZu lxTkhmRE267GG0zWnEVAIiSAWTXoiJPiWpmlysexNFORxfOzeXMjUFnG/pdwk/4lVjEd2P D0ogp35Kj6u05B/W666tznTXgQaq1m8= Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-5cf2d73a183so3401012a12.1 for ; Tue, 16 Apr 2024 08:51:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713282669; x=1713887469; darn=kvack.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=ebpJMn/gZAY2DoBsMCYW/AsLUXHW/Jt0ZHuYBVYkNHo=; b=NXRX3Jhl7OyS4hRk4C2epu/+AQXucypRQTudLRSOyNltddZnXDGcHUHmoPEOwqjiBL 2z5dLvGF0HTv0MJgsVV9W4Hn1SybyEDKuPUjCbKitb4+iuCpjW0P42uuMkc9f0qQezu7 OwqE93lKR0o8YfAA84OLgupjN/j0tp7hNVtiKe1Wa3fB/fEYCf5yS5YkK5vsXSUW3XY/ CHezUEXeNGxM4vDlvgZ9UBV7HemyqzC7oEx+tzVjaA6tn4VyIhOe3pDUE8BefZcjpcMx y5igz8HmgoYaKcAFOIvm2UXYt3XqfP9Yzmans1kQ2MPTMSehesj4MP1G1RJ7uovFh647 +PmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713282669; x=1713887469; h=mime-version:user-agent:content-transfer-encoding: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=ebpJMn/gZAY2DoBsMCYW/AsLUXHW/Jt0ZHuYBVYkNHo=; b=HkotQcv4GEIUD/p2BqmpUUpBKBnzUvfDCdbRP2vu+bshAQN23Br5Nm6Qhq2vMZdaNH vA8BeOyyV61BBWdoACHsdVJjJLp6/JWJ3O4RcSEhRzpifjJf8aGAqZIZpsyEbpa7gqde uksE01cCYwMcMBhanZ9MFm7eFp7Bf0hC83GL+9x8i/10yHhNKZD+Ve0yf/eciO74BP+O p4oobrl6wa9GBAd+3L6zzhyCOVG7xSfY770jAgqBljSYL9iKNz0xJ2zX7kr1FoO11llA t/QiNzxv40FFq+3husYnjC9zJGeSK/r//Z7zS2eOfk+c9cLHPMBGieGLW00+lw8gnOW2 2dXg== X-Forwarded-Encrypted: i=1; AJvYcCWnSgNXRm94ABJsFh4Vcu4I1vlJrOu+G7bLpzUiDHhUkZw6rwYVCCOV1576ytIVzpjLpk+tqTTeEYgzHDbPnymxhsA= X-Gm-Message-State: AOJu0YzseXfejY78dnTki53izzCakvlMOt2M/gUIT23HU9nBGM6QE30E QRgd+zt9SIbsbBkmH8cbxScw2Mjif+fxIt0W65MX8xR4smPAWE+JVzZvYw== X-Google-Smtp-Source: AGHT+IEotYvaCEGeYx4z8+15FTd9fFl61UtLdLb61HwB++g8hWZAYYPVmJxXAX7qXanWRr1VpPhjWg== X-Received: by 2002:a17:90a:db42:b0:2aa:c389:c9f0 with SMTP id u2-20020a17090adb4200b002aac389c9f0mr2284171pjx.12.1713282668936; Tue, 16 Apr 2024 08:51:08 -0700 (PDT) Received: from ?IPv6:2605:59c8:43f:400:82ee:73ff:fe41:9a02? ([2605:59c8:43f:400:82ee:73ff:fe41:9a02]) by smtp.googlemail.com with ESMTPSA id g10-20020a63374a000000b005e4666261besm9034432pgn.50.2024.04.16.08.51.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 08:51:08 -0700 (PDT) Message-ID: <712b14031ca37a12c1871d1745794b1f0be0498f.camel@gmail.com> Subject: Re: [PATCH net-next v2 05/15] mm: page_frag: use initial zero offset for page_frag_alloc_align() From: Alexander H Duyck To: Yunsheng Lin , davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Andrew Morton , linux-mm@kvack.org Date: Tue, 16 Apr 2024 08:51:07 -0700 In-Reply-To: <6a78b9ad-0d20-a495-52ca-fac180408658@huawei.com> References: <20240415131941.51153-1-linyunsheng@huawei.com> <20240415131941.51153-6-linyunsheng@huawei.com> <6a78b9ad-0d20-a495-52ca-fac180408658@huawei.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.48.4 (3.48.4-1.fc38) MIME-Version: 1.0 X-Stat-Signature: y9mfutk31n1jgt7sjggj8kujydhmhato X-Rspamd-Queue-Id: 4415E140003 X-Rspamd-Server: rspam02 X-Rspam-User: X-HE-Tag: 1713282670-520736 X-HE-Meta: U2FsdGVkX1/zLvf2ptlNsNdv5aPxCBz5CZkAAGnVltCIl2V38WsucvXdURMD01uIPRbDXXPiajZQyQmo6VPf9qNUybP/p+Zg9q/te56oTTCl5jKCKhdJnnAjUbdYt0bayh9+C426UwWCZsGHA56rfnM3bYxgbG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CfUdlNzyOXIxg2VH6QPEXpA8NHhV43jH+H854+LgVr9/uLzv2xO7Jwy8UqAGZuek0NC6qA2VQ6iEPM9ajiWrUKO6dIWGSqlV0B2K0UtopevPt1Kb301BaZk+DvD12MFCWb5MxKzb+CrKzZioO+Sm7AqzByEz3GWGDk3wpEjVNVY4OWx7plasuSKUrHFSe8YtUxlDb2BzpzAdI8blMbCcM9GW8aRVe6Rck2eSYfF6vNuSsGWNr6AUVqUqI5b5WCofPweyT7qHzRf3e4WkcsrJTNpA+KBx4cBNic9ElyoWsy+lJg8vzyPOPgOAt5lJR8qV2XOwxpQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Tue, 2024-04-16 at 21:11 +0800, Yunsheng Lin wrote: > On 2024/4/16 7:55, Alexander H Duyck wrote: > > On Mon, 2024-04-15 at 21:19 +0800, Yunsheng Lin wrote: > > > We are above to use page_frag_alloc_*() API to not just > > > allocate memory for skb->data, but also use them to do > > > the memory allocation for skb frag too. Currently the > > > implementation of page_frag in mm subsystem is running > > > the offset as a countdown rather than count-up value, > > > there may have several advantages to that as mentioned > > > in [1], but it may have some disadvantages, for example, > > > it may disable skb frag coaleasing and more correct cache > > > prefetching > > >=20 > > > We have a trade-off to make in order to have a unified > > > implementation and API for page_frag, so use a initial zero > > > offset in this patch, and the following patch will try to > > > make some optimization to aovid the disadvantages as much > > > as possible. > > >=20 > > > 1. https://lore.kernel.org/all/f4abe71b3439b39d17a6fb2d410180f367cadf= 5c.camel@gmail.com/ > > >=20 > > > CC: Alexander Duyck > > > Signed-off-by: Yunsheng Lin > > > --- > > > mm/page_frag_cache.c | 31 ++++++++++++++----------------- > > > 1 file changed, 14 insertions(+), 17 deletions(-) > > >=20 > > > diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c > > > index 64993b5d1243..dc864ee09536 100644 > > > --- a/mm/page_frag_cache.c > > > +++ b/mm/page_frag_cache.c > > > @@ -65,9 +65,8 @@ void *__page_frag_alloc_align(struct page_frag_cach= e *nc, > > > unsigned int fragsz, gfp_t gfp_mask, > > > unsigned int align_mask) > > > { > > > - unsigned int size =3D PAGE_SIZE; > > > + unsigned int size, offset; > > > struct page *page; > > > - int offset; > > > =20 > > > if (unlikely(!nc->va)) { > > > refill: > > > @@ -75,10 +74,6 @@ void *__page_frag_alloc_align(struct page_frag_cac= he *nc, > > > if (!page) > > > return NULL; > > > =20 > > > -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) > > > - /* if size can vary use size else just use PAGE_SIZE */ > > > - size =3D nc->size; > > > -#endif > > > /* Even if we own the page, we do not use atomic_set(). > > > * This would break get_page_unless_zero() users. > > > */ > > > @@ -87,11 +82,18 @@ void *__page_frag_alloc_align(struct page_frag_ca= che *nc, > > > /* reset page count bias and offset to start of new frag */ > > > nc->pfmemalloc =3D page_is_pfmemalloc(page); > > > nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; > > > - nc->offset =3D size; > > > + nc->offset =3D 0; > > > } > > > =20 > > > - offset =3D nc->offset - fragsz; > > > - if (unlikely(offset < 0)) { > > > +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) > > > + /* if size can vary use size else just use PAGE_SIZE */ > > > + size =3D nc->size; > > > +#else > > > + size =3D PAGE_SIZE; > > > +#endif > > > + > > > + offset =3D ALIGN(nc->offset, -align_mask); > >=20 > > I am not sure if using -align_mask here with the ALIGN macro is really > > to your benefit. I would be curious what the compiler is generating. > >=20 > > Again, I think you would be much better off with: > > offset =3D __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); > >=20 > > That will save you a number of conversions as the use of the ALIGN > > macro gives you: > > offset =3D (nc->offset + (-align_mask - 1)) & ~(-align_mask - > > 1); > >=20 > > whereas what I am suggesting gives you: > > offset =3D (nc->offset + ~align_mask) & ~(~align_mask)); > >=20 > > My main concern is that I am not sure the compiler will optimize around > > the combination of bit operations and arithmetic operations. It seems > > much cleaner to me to stick to the bitwise operations for the alignment > > than to force this into the vhost approach which requires a power of 2 > > aligned mask. >=20 > My argument about the above is in [1]. But since you seems to not be work= ing > through the next patch yet, I might just do it as you suggested in the ne= xt > version so that I don't have to repeat my argument again:( >=20 > 1. https://lore.kernel.org/all/df826acf-8867-7eb6-e7f0-962c106bc28b@huawe= i.com/ Sorry, I didn't have time to go digging through the mailing list to review all the patches from the last set. I was only Cced on a few of them as I recall. As you know I have the fbnic patches I also have been trying to get pushed out so that was my primary focus the last couple weeks. That said, this just goes into my earlier complaints. You are now optimizing for the non-aligned paths. There are few callers that are asking for this to provide non-aligned segments. In most cases they are at least cache aligned. Specifically the __netdev_alloc_frag_align and __napi_alloc_frag_align are aligning things at a minimum to SMP_CACHE_BYTES by aligning the fragsz argument using SKB_DATA_ALIGN. Perhaps it would be better to actually incorporate that alignment guarantee into the calls themselves by doing an &=3D with the align_mask request for those two functions to make this more transparent. > >=20 > > Also the old code was aligning on the combination of offset AND fragsz. > > This new logic is aligning on offset only. Do we run the risk of > > overwriting blocks of neighbouring fragments if two users of > > napi_alloc_frag_align end up passing arguments that have different > > alignment values? >=20 > I am not sure I understand the question here. > As my understanding, both the old code and new code is aligning on > the offset, and both might have space reserved before the offset > due to aligning. The memory returned to the caller is in the range > of [offset, offset + fragsz). Am I missing something obvious here? My main concern is that by aligning offset - fragsz by the alignment mask we were taking care of all our variables immediately ourselves. If we didn't provide a correct value it was all traceable to one call as the assumption was that fragsz would be a multiple of the alignment value. With your change the alignment is done in the following call. So now it splits up the alignment of fragsz from the alignment of the offset. As such we will probably need to add additional overhead to guarantee fragsz is a multiple of the alignment. > >=20 > > > + if (unlikely(offset + fragsz > size)) { > > > page =3D virt_to_page(nc->va); > > > =20 > > > if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) > > > @@ -102,17 +104,13 @@ void *__page_frag_alloc_align(struct page_frag_= cache *nc, > > > goto refill; > > > } > > > =20 > > > -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) > > > - /* if size can vary use size else just use PAGE_SIZE */ > > > - size =3D nc->size; > > > -#endif > > > /* OK, page count is 0, we can safely set it */ > > > set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); > > > =20 > > > /* reset page count bias and offset to start of new frag */ > > > nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; > > > - offset =3D size - fragsz; > > > - if (unlikely(offset < 0)) { > > > + offset =3D 0; > > > + if (unlikely(fragsz > size)) { > >=20 > > This check can probably be moved now. It was placed here to optimize > > things as a check of offset < 0 was a single jump command based on the > > signed flag being set as a result of the offset calculation. > >=20 > > It might make sense to pull this out of here and instead place it at > > the start of this block after the initial check with offset + fragsz > > > size since that would shorten the need to carry the size variable. >=20 > Yes, that is better. >=20 > But does it make more sense to just do the 'fragsz > PAGE_SIZE' checking > alongside with the aligning checking, as we have a better chance of > succeding in allocating order 0 page than order 3 page, so it seems the > caller is not allowed to pass a fragsz being bigger than PAGE_SIZE anyway= ? Yeah, that should be fine. > >=20 > > > /* > > > * The caller is trying to allocate a fragment > > > * with fragsz > PAGE_SIZE but the cache isn't big > > > @@ -127,8 +125,7 @@ void *__page_frag_alloc_align(struct page_frag_ca= che *nc, > > > } > > > =20 > > > nc->pagecnt_bias--; > > > - offset &=3D align_mask; > > > - nc->offset =3D offset; > > > + nc->offset =3D offset + fragsz; > > > =20 > > > return nc->va + offset; > > > } > >=20 > > . > >=20