From patchwork Fri Aug 30 11:18:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13784986 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 44FAFCA0EFC for ; Fri, 30 Aug 2024 11:24:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0DE8E6B0117; Fri, 30 Aug 2024 07:24:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 015FF6B0118; Fri, 30 Aug 2024 07:24:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E1EE16B0119; Fri, 30 Aug 2024 07:24:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id BF58E6B0117 for ; Fri, 30 Aug 2024 07:24:56 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 47E94817AE for ; Fri, 30 Aug 2024 11:24:56 +0000 (UTC) X-FDA: 82508679792.13.C6969DE Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf10.hostedemail.com (Postfix) with ESMTP id A1509C0021 for ; Fri, 30 Aug 2024 11:24:53 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=none; spf=pass (imf10.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725017003; 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; bh=CMPc38kE0xudaGFU2qyxansFCuO8I1V8U9TvoFPvt2A=; b=f2zsEgDfUPzELf94wsgOejqIwveuLOP2RTvt+rITeOGjiQNYcmOGquSF0eFrJj+LbDZWa3 Xng2xkWadj4WdrZiGfjQDHwdNeuH041BswagbrdqSIyPV9h33ugMMjh8E3U+LOkI/jbv8s 4Y6VO3ru05U+J6Kl5dkp09uF6QaeK0g= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725017003; a=rsa-sha256; cv=none; b=sRqXw4fYdTtgvfEiokQxj1+ACbs1loC6NVKo+0H5XzOq4U0UwlXuR/kHgS5L8YiWAqaU6H EtR87Qg6D40bqdbYno7SN2asZmeWHIR0HxxcTNn74YcY4ypiM7dxuIfWabgCPwapYvnEh5 mBMiWc6rl+GkbD2S6VhB/TeFobcFGZE= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=none; spf=pass (imf10.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=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> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemf200006.china.huawei.com (7.185.36.61) X-Stat-Signature: 7j48mf4k3s8zonweuriai6j9ergdtsfa X-Rspamd-Queue-Id: A1509C0021 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1725017093-569049 X-HE-Meta: U2FsdGVkX1/ElwL6xuLUdXVdOvSld1q6diFR+ss7KilfKQhntkCNx578dz7WIbOEDE1+Rp8fb+eqptL8HCs90EoPv8ds8FRZ4ZkZC6NLRl73bo/x6GTiemgTVmeIYyRN97gJq7huOM2+rEdUP7Q1Cl0/6HA23TlRT+VirAvkDroG0saP4wbp5wkIJVNR47mP5NwEvCgETTjnW1bowI4B2m2/jjcZIimqU2N+nmIoQ6AxqFo0rrKG+QBLnE+q9OCSTw/Dkc71BR+GEPtUjtC8fv1/QzWvrWIqkOQqDiRnofoDHXgOxyVCgoRlwLZXTOea2HK2xlt9xLiq5DD7jLUeGuMEJYxFq+t0y0HwqCwwr/fPo9XUuHxbUOPI13H6ze2j2dgWSFIYRdLdyeKIZiJaKZ8kGq5IdD/VYq5//Ei+Json5QbAUgoopizG9iuA94O9xi0nYxqypfQAz31cV24i3R8QVW6mfzKHmFfIFld0XIaAOMbbMBoN8xG+O160YdYdQYMZWRgvUatQtT6o0TL1p/qHBlpggiFU2TV/yyAuOQ+1it/5Ic8bcUd/UTWKil5D93gJVvNp6QaA+4pPV/AlYfwKEPFyZ6mBWiKlLbRw2VvNfySNYrvqPA1Ie0qm1vDIs5FIzOwIVjQWqr8ffPLCyv1J1Pj4vNRhs2YGVpbtfF2hXXroRIb4ceuiZLguPKkLC8rbxeHDL0sVhdfA8JsUDx4EHO/G6K2F+4NoYbaS/EL1IiZUAVabfU+geX342+QAwpJDFRpatfPnCmk0WWEIjmIiAmweFDNjLOpNUw0BCQkb9xEl3tEgRmvMjiD7GjGZCMcfKF3Fzvg+ITlazFFCM68MZxcb40eHVxJ1fjw1R0LImYJQLhG5CDmwbgOm3wIqsUkbVLMMd8nKOQa2ChwqTOIDQ0aXiEw6qWES21QuT6DiBIMWIKm42SKepXLvQKDOHjwyJe0f4xUwqaqSlXz nrnRwrPk KPrfXDTvmpQDyrjm60b86a11t3T62bItkSSBPM4Ao/cnYhEf+VArRfN9WYdtByfCX1d8jtFKF0PnpseMOkJbxKj6WBnP1pJgw1cBzH9XWIzF9LLK7hT+iVC9vyqpncdlNAhZT7Z5/GHWCESNa9A6kKfiYph3fnDJ4OgJjixgvTr+oh/EpxFIV0iA6U6wzcCuhA5icey9aiww1iPZC8yhsOvIdlFR+3ZTXk3PCBZjEn3nrL7YWgtNE8LNKFo4+5L2IgQhS9hIjhXLaAw74OiKmA4QRYKxVQ7kcOKHl 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: 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_cache.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 @@ -62,8 +63,50 @@ static inline unsigned int page_frag_cache_page_size(unsigned long encoded_page) 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 fragsz, - gfp_t gfp_mask, unsigned int align_mask); +void *__page_frag_cache_prepare(struct page_frag_cache *nc, unsigned int fragsz, + 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 != page_frag_encoded_page_ptr(nc->encoded_page)); + VM_BUG_ON(pfrag->offset + pfrag->size > page_frag_cache_page_size(nc->encoded_page)); + + /* pfrag->offset might be bigger than the nc->offset due to alignment */ + VM_BUG_ON(nc->offset > pfrag->offset); + + committed_offset = pfrag->offset + used_sz; + + /* Return the true size back to caller considering the offset alignment */ + pfrag->size = (committed_offset - nc->offset); + + nc->offset = committed_offset; +} + +static inline void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, + gfp_t gfp_mask, unsigned int align_mask) +{ + struct page_frag page_frag; + void *va; + + va = __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; +} 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, unsigned int count) } EXPORT_SYMBOL(__page_frag_cache_drain); -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 fragsz, + struct page_frag *pfrag, gfp_t gfp_mask, + unsigned int align_mask) { unsigned long encoded_page = nc->encoded_page; unsigned int size, offset; struct page *page; - size = page_frag_cache_page_size(encoded_page); - if (unlikely(!encoded_page)) { refill: page = __page_frag_cache_refill(nc, gfp_mask); @@ -106,6 +104,9 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; nc->offset = 0; + } else { + size = page_frag_cache_page_size(encoded_page); + page = page_frag_encoded_page_ptr(encoded_page); } offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); @@ -123,8 +124,6 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, return NULL; } - page = page_frag_encoded_page_ptr(encoded_page); - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) goto refill; @@ -139,15 +138,17 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; + nc->offset = 0; offset = 0; } - nc->pagecnt_bias--; - nc->offset = offset + fragsz; + pfrag->page = page; + pfrag->offset = offset; + pfrag->size = size - offset; return page_frag_encoded_page_address(encoded_page) + offset; } -EXPORT_SYMBOL(__page_frag_alloc_align); +EXPORT_SYMBOL(__page_frag_cache_prepare); /* * Frees a page fragment allocated out of either a compound or order 0 page.