Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1450891lqe; Mon, 8 Apr 2024 09:12:42 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUla4OLreEwkErnZzt7D1/lMv0HywpdO3AQHSowSah4Wl8Vf1ZIpDNkKOGB67eNAXdA7g7VaPOrrS4fsIKscKgMylAtKBjnUljOK/AcjQ== X-Google-Smtp-Source: AGHT+IGODoagHy1Kyk00XHwQYZxg8/EhCGitX8Svdeg0ES2o7lIR9ZL4Lit5ILoQajZuGIyVlW1e X-Received: by 2002:a05:6102:a4b:b0:47a:eeb:16b1 with SMTP id i11-20020a0561020a4b00b0047a0eeb16b1mr1256940vss.6.1712592762153; Mon, 08 Apr 2024 09:12:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712592762; cv=pass; d=google.com; s=arc-20160816; b=rdBB6GzOD9Vfg9AMQBy9ZuWFC37T9FI/zwR2LRm6PIfPlploLKg5+I3y+mRcrvYTP0 7FP1+LIJN9W5/QfH3ANBGBC0M8nFQ8+1TT4ySG6+eYMMHVWv8kAjxc4viab6CVjXoZxh MsTG07CQbDpbCloZ8DcJDtx3FyEOK0yzruL5uh6URWOwMe211oW3PXbEBMimJqzoadqH nHWubMX8mns2Ynkzih4eQlyrLn/Z2JEz1c22rH9HtKvBg1hAmoBP3+SRKIgSyMOEekpI jRxP/rTUiqnehkt+tPvzW8/BSF0y2mTp72l/T7gJuSag3lwW7wRKE2k1sf5QLbZ2vonN fYJQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=uENSkFYk7LJQAn7mCGOHXBkbAqtqsej2uPu661a41kA=; fh=yQAD/jY3X59WPMet2TqvrXvTds/9Ce5XzKMzi1I9Vek=; b=DI2BCEg8Gyqibc4EvAS2mVEvzTddJ608Qo1TF9yyTzL5vQQnfpqPoZ402Qa+Q/aJta onIo/+OwVsAUn4D4MU1UFGdFgVHvzFnXEmEg5j8d7HUn8rG64TlsC7jbF+8ISRNKGn/j fJ9Qn3Eo+KXBUo6LJg+qQcJMRW0WWJE6DtbokHOAhMkp5e93jW/Qjk5otqRPhAbMS1y8 HCzJ5y4yj8sBTuqmhoxL2JsPHfaNtmZ6mjjECoQRPlMId1Ev4OLQelPNBUheutJ0EiJd ko9uAILRyy8JTXzwTi0/qEs7GKEwzVpyN3da7VC+e3260JiYynlrMmT6R8pi0Or75144 fnGg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Y0PmQVPZ; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-135660-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135660-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id v9-20020a0561020f0900b0047a11c5894csi102240vss.671.2024.04.08.09.12.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 09:12:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-135660-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Y0PmQVPZ; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-135660-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135660-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 88BCC1C243DB for ; Mon, 8 Apr 2024 16:12:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A04591422D6; Mon, 8 Apr 2024 16:12:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Y0PmQVPZ" Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E294C757FF; Mon, 8 Apr 2024 16:12:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712592727; cv=none; b=njwVEHgzyWIvdWuCegXGMZGKQUn1qmYsFK6AVb+mXS0/3aH4jTucI/Fd8gWKhXDHZZaSzFsRIi8jqpzQ+kAusHjdX4cQXHVwW5sJH9Mk8t84nOl5GxKKfA0+AGTwNaZbbwJ+9RkHFAMU7J6Zn7s2mrnUiRlQvO76uvMUltYF1/c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712592727; c=relaxed/simple; bh=PzUvBEDSNmkvNMiv+D4xXdtXE7vcJ/e3P7TeoZzTE98=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=uqBDU57kSz0qnDnqbvP7IZR8bdW88TV1uQT/2wrf0S9fPNmryl7OvrdOxaZ3HP6BKy2yKeBK5BC+zjk7+3J9ZbYuLrrwp8wIukF6Spv0wJb+z5WbiRZeXobnd3jGS/PKuaODy4X9qncbXdj6pq+owmw/Wqwbf2fz1k6FbuIpX3o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Y0PmQVPZ; arc=none smtp.client-ip=209.85.128.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-41550858cabso30988645e9.2; Mon, 08 Apr 2024 09:12:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712592724; x=1713197524; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=uENSkFYk7LJQAn7mCGOHXBkbAqtqsej2uPu661a41kA=; b=Y0PmQVPZJ93VGPyTxJT9oTLOViLusi53gJWRnfqIfcH2fPcwsJMypaWt+f0ah7LAm5 cHkOzUezV5pIO6pihYpf3NJjT9ajK8tACqRN6AApA0CcjVC09dHlu01lGKxlh5I8IDoq +Z6gZYVqAMdXeEt+d8rBmKhvspidjyV8ojlUGPdE1Uiv4cJhB83WxEkevIu8VjUrl8Ot vW53uWZ1tKCquYDaGoO6U3DM/H3cSTF54pgdjyuAp8IEsh3sXMytUfsSoVrVD51sQoHw hXWAAqTQ8Je1cOvd6YxCIXJMQofbIKkjb00c2iam++uUpIdix35mE968Ka97G1k78FWn kK5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712592724; x=1713197524; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uENSkFYk7LJQAn7mCGOHXBkbAqtqsej2uPu661a41kA=; b=RQF/mi1enhQ+o6V16qwmRdmA94c247rxfPxKD2Qu9sfKqw68QRqD8vSwa1vg+gAA/g mT5w/QXDGwbDxbENuzXpeFtyJnpRxRgoHxjOeS+bKUIt5CtWi15xtnKhSs8EL7yvqGnl KBkU4H1wzOX7fxKWQNh1dj4u/CCzc5opL98c9hpgycfW20uCH/pLCs1VmkCiUOU1DmG+ Mh7ycPVphkex4+GOf5T7GlSlCgdRis+VLHfXu9pvpUFk7oJHsazOT+Rn1Cok3MbVmEUJ P7ZURpG6lfU/5RQ4e2Crb+cKwwk23DHkHnB44yTa3pnpxrUqHQAEcJYVQpYkMHWg/0ll ZVQA== X-Forwarded-Encrypted: i=1; AJvYcCXLFpGFPfGpu31/jHTCcU5eIw6i9cDht1RZ2EGokEh9JcSnONhCXsYGGjmteQdEDnVcmrOJGkCLsl/fcFJqKAwAwDoOB0TJkjK7flTf4L00wELrt2o2TWeJ2TNW1XtNd0221azG X-Gm-Message-State: AOJu0Yy/veSvO8H/8DzpQSXdR247AalQ9iBt3EtLq+y6pdKLcCoumJ2q pLwHH0EGUZLhBPL4wf+GDepp9ybx9kBQXtkty5eDIyDdF1InLQht+dU+0/FVRh76lltj32jYK6x cOnXz0q9+bYiW16dQYXUmqdYisTp3cDJZ X-Received: by 2002:a05:600c:45cc:b0:415:45ea:9922 with SMTP id s12-20020a05600c45cc00b0041545ea9922mr7458671wmo.35.1712592724107; Mon, 08 Apr 2024 09:12:04 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240407130850.19625-1-linyunsheng@huawei.com> <20240407130850.19625-3-linyunsheng@huawei.com> <43d99616cd4a2a6fce6a6b97f73d08ebc5361a61.camel@gmail.com> In-Reply-To: From: Alexander Duyck Date: Mon, 8 Apr 2024 09:11:27 -0700 Message-ID: Subject: Re: [PATCH net-next v1 02/12] mm: page_frag: use initial zero offset for page_frag_alloc_align() To: Yunsheng Lin Cc: davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Andrew Morton , linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Apr 8, 2024 at 6:39=E2=80=AFAM Yunsheng Lin wrote: > > On 2024/4/8 1:52, Alexander H Duyck wrote: > > On Sun, 2024-04-07 at 21:08 +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 > >> > >> 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. > >> > >> 1. https://lore.kernel.org/all/f4abe71b3439b39d17a6fb2d410180f367cadf5= c.camel@gmail.com/ > >> > >> CC: Alexander Duyck > >> Signed-off-by: Yunsheng Lin > >> --- > >> mm/page_frag_cache.c | 31 ++++++++++++++----------------- > >> 1 file changed, 14 insertions(+), 17 deletions(-) > >> > >> diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c > >> index a0f90ba25200..3e3e88d9af90 100644 > >> --- a/mm/page_frag_cache.c > >> +++ b/mm/page_frag_cache.c > >> @@ -67,9 +67,8 @@ void *__page_frag_alloc_align(struct page_frag_cache= *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; > >> > >> if (unlikely(!nc->va)) { > >> refill: > >> @@ -77,10 +76,6 @@ void *__page_frag_alloc_align(struct page_frag_cach= e *nc, > >> if (!page) > >> return NULL; > >> > >> -#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. > >> */ > >> @@ -89,11 +84,18 @@ void *__page_frag_alloc_align(struct page_frag_cac= he *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; > >> } > >> > >> - 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); > >> + if (unlikely(offset + fragsz > size)) { > > > > Rather than using "ALIGN" with a negative value it would probably make > > more sense to use __ALIGN_KERNEL_MASK with ~align_mask. I am not sure > > how well the compiler sorts out the use of negatives to flip values > > that are then converted to masks with the "(a) - 1". > > The next patch will remove the '-' in '-align_mask' as the 'ALIGN' operat= ion > is done in the inline helper. I am not sure that matter much to use > __ALIGN_KERNEL_MASK with ~align_mask? It is a matter of making the negations more obvious. Basically you could achieve the same alignment by doing: (offset + (~align_mask)) & ~(~align_mask) rather than: (offset + ((-align_mask) - 1)) & ~((-align_mask) - 1) I'm not sure the compiler will pick up on the fact that the two are identical and can save a number of operations. Also my suggested approach is closer to how it used to work. Technically the one you are using only works if align_mask is a negative power of 2.