From patchwork Mon Sep 2 12:03:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13787182 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 56BC9CA0ED3 for ; Mon, 2 Sep 2024 12:09:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 169EC8D00CF; Mon, 2 Sep 2024 08:09:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 11A8E8D0098; Mon, 2 Sep 2024 08:09:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DC4038D00CF; Mon, 2 Sep 2024 08:09:24 -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 BCA218D0098 for ; Mon, 2 Sep 2024 08:09:24 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 75EBAAAFCF for ; Mon, 2 Sep 2024 12:09:24 +0000 (UTC) X-FDA: 82519678248.24.2044EC7 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf10.hostedemail.com (Postfix) with ESMTP id 07DCBC000B for ; Mon, 2 Sep 2024 12:09:21 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf10.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725278870; 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=wlQTDkcHDCt4mI05r2JYiIHRyxCDNb/OG0bqFpOia/w=; b=LlyT1DQHgdkcPbAK7n2WRxVcKQOG+mwR13IaNWzSVmV21XLAjyuy5nZNxc2B2+Eet+uX1N toB+N3MsQWtUdLYJcLYBWe7RUQ43ZFmO1w7a1g23oJam2LSBU35pmm0eUt14rQy1ugOwer m22rlzRnpPW2WBDlsKkwza9TffPPn/g= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725278870; a=rsa-sha256; cv=none; b=lKFQTvdli4QjclVmRfJb5I8pKLa0nUfG/8GJCIJLzvYYXgRlqHnQU5IpndJ2HyYngk3SWG lgzrpeH6HP9eDEuaQ6FjtJHpTJ4+cXMwfevN6QflQD0Aw3vL72YX5nWOR/4A+xCzr71sRa 8GutGB1qTW0naK0CbRgvreC+BK3kFxM= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf10.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com Received: from mail.maildlp.com (unknown [172.19.88.163]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4Wy6x92llgz1j7n0; Mon, 2 Sep 2024 20:09:01 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 0EE5218001B; Mon, 2 Sep 2024 20:09:19 +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; Mon, 2 Sep 2024 20:09:18 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v17 07/14] mm: page_frag: some minor refactoring before adding new API Date: Mon, 2 Sep 2024 20:03:06 +0800 Message-ID: <20240902120314.508180-8-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240902120314.508180-1-linyunsheng@huawei.com> References: <20240902120314.508180-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-Rspamd-Queue-Id: 07DCBC000B X-Stat-Signature: fts1fixayxozuatr1945xaakwy176rwk X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1725278961-80237 X-HE-Meta: U2FsdGVkX1+9lvCtnUT59fncX1Sl5WMgQil/5Lr4rrClMXTbYtwkDqZjCG35d4itUYG+XuMSQXSG20SfpwA4zVKXPE4vLEBoZTrhyHocLEkscCH70WndOoMwCy1YpPSVTyv7VrrQEpa49jMVrMuwrdawPXEuT7xXSB6qT5xcQ0o7mWLPtAa+2sVNAMhLqlBKekub4XBY/8eH2vfdZEBwZK+lX9XbOdm1hJsdwU7E+kOceZHOdTd0XsASqmFbSW1FCzFVYS79U+2LzBopPCGzVUeMFSjOHe2yL4QP9Pl90PnqmBVvOhbpUC7qqiRgJSTJI8wXj4EzcYqzHhf+kGdXBdzsOsb64Y7P8G3Y8XMVsOhKlXWE6PBFNCbedcf7J105N8UKzrLsb2Qdm0mYA7O7ENAuCApUYfgZouzlDVOS8oEeNAcYb7LRJhJ6T4ZH5036eTwVeBNK3fMBsaHXhbt4XTO+3bzHEyiCBTlunYC6oZKCDyNd30vntYHQOkD/33ZgnaLkb4JVrtI2Zae9/nEVxFLMa5bzYYdhdED3DW0f7j/j7prg9FTWfzcWVZcGokWiz/jPyHET0gusUS9NCUpvCp/u+ZtDMg1coaLJiASivnewzl9WFg/d4nXj9e7iRif7WJX0fymr2R4UxcUikOcVWIJ3yKYk5kES0yI99pdha1Mdo10cOHtfG5EprpwG6IWAtQraQwNmH/dsplFMAPCjrqODLGyZXusbiP0PMsB4iTzSBqxsxvySKo+pXH+Rlq6Tjogx8YyxdT35NDsQ7ceeMmu+1wpcdnm5qzf3VaxO8qf7zB3jwQeGwSx+tdwKM3k+9410hOQGucv2336V0oC9/EC+JZ5oIKcc3g7VTZQIdPR0f6yEAhH1aOb5QEP+CWgMDBC3f7vWJ8hP+rexe1+r8F5kdsbFBqYlYtrrs0zhW98avrrdt6PZWz2G+u8GI7qVbHU3sI8mNBmYfy8ZjwQ WDtRzRPB ZDysyrdmAKFs862FxQ6bscsB5Ljv3/suGpOPgC8PylqHW545FKQK4L5nX5KAVGA2m+LumjN5X/zpCZztrTnJ0mwo+8hwru4GooMwHW+EGOWnRIyKrJ9q4UaID5saqUnk/fDFHNDG8w+c4Jdu5LgTGV+0VM1VMIrJSyQX7KN3aMXs2mJ4G/1DtbyoDfa4vsbTwLYqWM9cWdwOskVrjaHw1r1r8wtrq8zGJY0V8ZCQ7Uekmur1TUmb0TQGUyKpOFxUY1yWaQ64tX5R1X6QwpVdTbTCqxAJHqWnABRWH 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 | 51 +++++++++++++++++++++++++++++++-- mm/page_frag_cache.c | 21 +++++++------- 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index cb89cd792fcc..24835ec8c891 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,54 @@ 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 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.