From patchwork Tue Oct 1 07:58:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13895537 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 A1ED3E7716C for ; Thu, 5 Dec 2024 15:20:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B465D6B00CF; Thu, 5 Dec 2024 10:19:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9C7056B00CC; Thu, 5 Dec 2024 10:19:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 57BAF6B00C4; Thu, 5 Dec 2024 10:19:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id A3156280036 for ; Tue, 1 Oct 2024 03:59:28 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 45C8B40B15 for ; Tue, 1 Oct 2024 07:59:28 +0000 (UTC) X-FDA: 82624283616.30.CBEE913 Received: from mail-pg1-f195.google.com (mail-pg1-f195.google.com [209.85.215.195]) by imf14.hostedemail.com (Postfix) with ESMTP id 566DC100008 for ; Tue, 1 Oct 2024 07:59:26 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=M9TOA6Jb; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf14.hostedemail.com: domain of yunshenglin0825@gmail.com designates 209.85.215.195 as permitted sender) smtp.mailfrom=yunshenglin0825@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727769426; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jYCVdOudHYSEPLdfqa+bP8zA1NgdXBDfEGsBksPcr7U=; b=D//cJ9j0Q0+A0Z/MbiFcU0bATHtZg+lV6b681Dhcml4aQhIScAKjlhN7rjZrsBK14G9zal k2bHQ2AvL17sowCEl/pY71Ba96ekyLKaKj+D2SkYpLoCuoM8ARiJxd0dUDwaKSAXhHbw8X Tni5KEcZ5mY0VXzKrX+ymHjuCE66IMs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727769426; a=rsa-sha256; cv=none; b=gNVddmKSBKiorJgyTvlNTrquzjAwMTVYy3R7wy78gX8N8Gzxxh9+P7zbP2nNIuBUH57Nf0 0nKiYxsLRWbao6X3UfNRZfmBjFxzn21nOzLW2Ix7tK0XZlcZdP6uq1872IlCEM7IZjrQ9F xqMroN2RBhsH5DOAiayAkzHRvzMP2DA= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=M9TOA6Jb; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf14.hostedemail.com: domain of yunshenglin0825@gmail.com designates 209.85.215.195 as permitted sender) smtp.mailfrom=yunshenglin0825@gmail.com Received: by mail-pg1-f195.google.com with SMTP id 41be03b00d2f7-7c1324be8easo4690486a12.1 for ; Tue, 01 Oct 2024 00:59:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727769565; x=1728374365; darn=kvack.org; 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=jYCVdOudHYSEPLdfqa+bP8zA1NgdXBDfEGsBksPcr7U=; b=M9TOA6JbpCUeogcZ3y0BNZ72JUBMQXZbD5LBXWAKn1Yz4LIkNrG1WAJldN5RHCnccJ NyyM/TNyi/+TS6ePawNeKY48wZ8sLJ3qwXLS7sjplpdCmub8k7WN7GHZQg2nxWDQpKrE 5Kw9DcJ75u742WZ7IIt2bajrHZuHA9kziy7hhmAyBxk+yfMgGLWiqCicg3+LDtAkSQV2 U4uirKI7rAcvu7zMKZ2FzMjAW5lyKePCDtzOWjrr0uOAKWYPZqu3uWBJof6AX78s0F0Y 5b9evFnLGZLaNrYe5PLuMwTLanBizWBHEvIHqHI/dcDaXgxTZ1ayj7bYKqUL+69+58WP mPTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727769565; x=1728374365; 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=jYCVdOudHYSEPLdfqa+bP8zA1NgdXBDfEGsBksPcr7U=; b=U+L4irYuDNrSiX3DNSFMRmN4XyXpz+W0prJPtALKkLC0C9jVSN93enE0FzTWHjIEkY CNAWy4LijdC+3Ga6ofElGZZ8wUoEcDai+8WeM1IjKdfhdVOl9XYM5tHiBSddtTqrsSLv 9enRIPvZ4gzbC4ZdOP5AcT0TvMR4gK2Wi83rR9z1DwdJIt7ZxrNKkAVzpQWY+3kr34Dg yQtijI4vJwNhc5md1RFYyrCnZ0FGN/AFB7CbOx8eDLQrhnCXx/Fdfy1hbN7bfLT3Sn3M 2xSLhL0LorVR76Lh0Kh+3ocmrHfmpbHL+1B/MhTHMXKWakgLeu4OcIDGHIjY+ahZ7vPm /aDA== X-Forwarded-Encrypted: i=1; AJvYcCX3eTvo3S5un1iFt+AXqXJtSRW7NhzfFhzM9Ohmow5BSbxZ7GTQxjszpYAXToblb6K8DBBR637Hbg==@kvack.org X-Gm-Message-State: AOJu0Ywq+ewW4fy4avfGTRlpmzBlLjJcgeRZol333MgCZTZ1cWxmMg5y PzHADALAwc4zqW870RhTqmnbdRgIJsSQtc6+WBAZq/kCgyYOBrlw X-Google-Smtp-Source: AGHT+IGyubSkW0LS2Q/mlw0qfA+FvSuoj1p26auGSTL84vixBHJ3lx7Y9nlk/IPOdCecjBEq2pxNbw== X-Received: by 2002:a17:90b:4d04:b0:2e0:a9e8:bb95 with SMTP id 98e67ed59e1d1-2e15a19ec2dmr3403921a91.3.1727769564970; Tue, 01 Oct 2024 00:59:24 -0700 (PDT) Received: from yunshenglin-MS-7549.. ([2409:8a55:301b:e120:88bd:a0fb:c6d6:c4a2]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e06e16d6d2sm13168168a91.2.2024.10.01.00.59.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2024 00:59:24 -0700 (PDT) From: Yunsheng Lin X-Google-Original-From: Yunsheng Lin To: davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Yunsheng Lin , Alexander Duyck , Alexander Duyck , Andrew Morton , linux-mm@kvack.org Subject: [PATCH net-next v19 03/14] mm: page_frag: use initial zero offset for page_frag_alloc_align() Date: Tue, 1 Oct 2024 15:58:46 +0800 Message-Id: <20241001075858.48936-4-linyunsheng@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241001075858.48936-1-linyunsheng@huawei.com> References: <20241001075858.48936-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 566DC100008 X-Stat-Signature: 8rtg7m1wobgx3s4bgtz3xxmeoib7zpqf X-Rspam-User: X-HE-Tag: 1727769566-746842 X-HE-Meta: U2FsdGVkX1/Z4XXRal1swZ5xiCgrYWb606pjHpaxok5ZgL6bMLuT7KsDucG2SeVmU00ccLRzAuVrdghGi7PC8VG5T8O8b58PThogXd9j/pZMUVXRUlXcOPjvdF83dflXzETzpTXjL21/tgRGdu/DmcwmPHI/CMADxQfidBeYjitT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za1QYBZ8ueR7M0Tz3vGiaAqFC7yZ9jwjWbFMRiLF7+mp9lReO7oB3A8v3oNDoyvN0Fk5RalHVhPePFNQJvydTtnJU/R1xqu8W4qCCRc7wwT4leXEjrIFj/btcDW3aNu2dsxOPbxOoekrEvitEV6Zmq47mLzms+VrYFoZEb9ATNaoyRv1MWlTIk9bifpWoW7r6oURF90g5Xs+QWFgfOrKpAYXqw31jSD0y28tq+4PcdmYYHJCg5TOND6YmTUfFiqw3bKsK22qQcZdYgLVIuDFxerHTsx0GUwIXRamnN22LwYVqBTGkwcfyq8UvwSvP0CXICM+VB4UsVerjYJjQy11BYdG4lSNbf5wJk2wmGfqjvm/rKIbD9uiGHewn/yNqRCB5PmA== 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: We are about 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 coalescing 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 avoid the disadvantages as much as possible. 1. https://lore.kernel.org/all/f4abe71b3439b39d17a6fb2d410180f367cadf5c.camel@gmail.com/ CC: Alexander Duyck Signed-off-by: Yunsheng Lin Reviewed-by: Alexander Duyck --- mm/page_frag_cache.c | 46 ++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 609a485cd02a..4c8e04379cb3 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -63,9 +63,13 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) { +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + unsigned int size = nc->size; +#else unsigned int size = PAGE_SIZE; +#endif + unsigned int offset; struct page *page; - int offset; if (unlikely(!nc->va)) { refill: @@ -85,11 +89,24 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, /* reset page count bias and offset to start of new frag */ nc->pfmemalloc = page_is_pfmemalloc(page); nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset = size; + nc->offset = 0; } - offset = nc->offset - fragsz; - if (unlikely(offset < 0)) { + offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); + if (unlikely(offset + fragsz > size)) { + if (unlikely(fragsz > PAGE_SIZE)) { + /* + * The caller is trying to allocate a fragment + * with fragsz > PAGE_SIZE but the cache isn't big + * enough to satisfy the request, this may + * happen in low memory conditions. + * We don't release the cache page because + * it could make memory pressure worse + * so we simply return NULL here. + */ + return NULL; + } + page = virt_to_page(nc->va); if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) @@ -100,33 +117,16 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, goto refill; } -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif /* OK, page count is 0, we can safely set it */ set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset = size - fragsz; - if (unlikely(offset < 0)) { - /* - * The caller is trying to allocate a fragment - * with fragsz > PAGE_SIZE but the cache isn't big - * enough to satisfy the request, this may - * happen in low memory conditions. - * We don't release the cache page because - * it could make memory pressure worse - * so we simply return NULL here. - */ - return NULL; - } + offset = 0; } nc->pagecnt_bias--; - offset &= align_mask; - nc->offset = offset; + nc->offset = offset + fragsz; return nc->va + offset; }