From nobody Fri Dec 19 08:59:16 2025 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE3C71ACDF5; Fri, 30 Aug 2024 11:24:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725017095; cv=none; b=vAJZtIMmBEjz+10HKVIvJ7z7tNoly3JtoAlRG8SmhbqCFlRGqYwodLN4Q93e8RC9zTgeNtHKYl+ZJgdVpselO0hYkBQh0u0putdE1G2d6vFsTK1uYI3J19llCEKS9PsTMawGVE1FcoRT7EizV8QWWsN4QMeE6EtPA3rMazJwnLU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725017095; c=relaxed/simple; bh=P+uiIbqnMTjKaRwQYGeYsAcGndfE64sC5Ls/FWpWlEE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=nJPsq28utr5PdNFFGnvIL+DKxV4i2QdZp+z+SSzoyCPdlsTTSDh09eRokPSjgt7uDUsZxVcu42cHs5osQpAIHYeJ/1ilGOk5uCLqpegBIpllCGNR0Wwi0/0Mjfe1qeYC79HtjpkDl3lr4qxeA5uN0hw+G5OHQzmrgIyF2FiMDbs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4WwG4y01vRzyR63; Fri, 30 Aug 2024 19:24:18 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 904BC1800D1; Fri, 30 Aug 2024 19:24:50 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 30 Aug 2024 19:24:50 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v16 07/14] mm: page_frag: some minor refactoring before adding new API Date: Fri, 30 Aug 2024 19:18:37 +0800 Message-ID: <20240830111845.1593542-8-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240830111845.1593542-1-linyunsheng@huawei.com> References: <20240830111845.1593542-1-linyunsheng@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemf200006.china.huawei.com (7.185.36.61) Content-Type: text/plain; charset="utf-8" Refactor common codes from __page_frag_alloc_va_align() to __page_frag_cache_prepare() and __page_frag_cache_commit(), so that the new API can make use of them. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 47 +++++++++++++++++++++++++++++++-- mm/page_frag_cache.c | 21 ++++++++------- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cach= e.h index cb89cd792fcc..f786ec572aa9 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include =20 @@ -62,8 +63,50 @@ static inline unsigned int page_frag_cache_page_size(uns= igned long encoded_page) =20 void page_frag_cache_drain(struct page_frag_cache *nc); void __page_frag_cache_drain(struct page *page, unsigned int count); -void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fra= gsz, - gfp_t gfp_mask, unsigned int align_mask); +void *__page_frag_cache_prepare(struct page_frag_cache *nc, unsigned int f= ragsz, + struct page_frag *pfrag, gfp_t gfp_mask, + unsigned int align_mask); + +static inline void __page_frag_cache_commit(struct page_frag_cache *nc, + struct page_frag *pfrag, bool referenced, + unsigned int used_sz) +{ + unsigned int committed_offset; + + if (referenced) { + VM_BUG_ON(!nc->pagecnt_bias); + nc->pagecnt_bias--; + } + + VM_BUG_ON(used_sz > pfrag->size); + VM_BUG_ON(pfrag->page !=3D page_frag_encoded_page_ptr(nc->encoded_page)); + VM_BUG_ON(pfrag->offset + pfrag->size > page_frag_cache_page_size(nc->enc= oded_page)); + + /* pfrag->offset might be bigger than the nc->offset due to alignment */ + VM_BUG_ON(nc->offset > pfrag->offset); + + committed_offset =3D pfrag->offset + used_sz; + + /* Return the true size back to caller considering the offset alignment */ + pfrag->size =3D (committed_offset - nc->offset); + + nc->offset =3D committed_offset; +} + +static inline void *__page_frag_alloc_align(struct page_frag_cache *nc, un= signed int fragsz, + gfp_t gfp_mask, unsigned int align_mask) +{ + struct page_frag page_frag; + void *va; + + va =3D __page_frag_cache_prepare(nc, fragsz, &page_frag, gfp_mask, align_= mask); + if (unlikely(!va)) + return NULL; + + __page_frag_cache_commit(nc, &page_frag, true, fragsz); + + return va; +} =20 static inline void *page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index a5c5373cb70e..62d8cf33f27a 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -79,16 +79,14 @@ void __page_frag_cache_drain(struct page *page, unsigne= d int count) } EXPORT_SYMBOL(__page_frag_cache_drain); =20 -void *__page_frag_alloc_align(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask, - unsigned int align_mask) +void *__page_frag_cache_prepare(struct page_frag_cache *nc, unsigned int f= ragsz, + struct page_frag *pfrag, gfp_t gfp_mask, + unsigned int align_mask) { unsigned long encoded_page =3D nc->encoded_page; unsigned int size, offset; struct page *page; =20 - size =3D page_frag_cache_page_size(encoded_page); - if (unlikely(!encoded_page)) { refill: page =3D __page_frag_cache_refill(nc, gfp_mask); @@ -106,6 +104,9 @@ void *__page_frag_alloc_align(struct page_frag_cache *n= c, /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; nc->offset =3D 0; + } else { + size =3D page_frag_cache_page_size(encoded_page); + page =3D page_frag_encoded_page_ptr(encoded_page); } =20 offset =3D __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); @@ -123,8 +124,6 @@ void *__page_frag_alloc_align(struct page_frag_cache *n= c, return NULL; } =20 - page =3D page_frag_encoded_page_ptr(encoded_page); - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) goto refill; =20 @@ -139,15 +138,17 @@ void *__page_frag_alloc_align(struct page_frag_cache = *nc, =20 /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; + nc->offset =3D 0; offset =3D 0; } =20 - nc->pagecnt_bias--; - nc->offset =3D offset + fragsz; + pfrag->page =3D page; + pfrag->offset =3D offset; + pfrag->size =3D size - offset; =20 return page_frag_encoded_page_address(encoded_page) + offset; } -EXPORT_SYMBOL(__page_frag_alloc_align); +EXPORT_SYMBOL(__page_frag_cache_prepare); =20 /* * Frees a page fragment allocated out of either a compound or order 0 pag= e. --=20 2.33.0