From patchwork Fri Dec 6 12:25:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13897124 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 7FBBAE7717D for ; Fri, 6 Dec 2024 12:32:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F1F726B026D; Fri, 6 Dec 2024 07:32:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ECFC36B026F; Fri, 6 Dec 2024 07:32:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D4A186B0270; Fri, 6 Dec 2024 07:32:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AE0F66B026D for ; Fri, 6 Dec 2024 07:32:47 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5CEC642720 for ; Fri, 6 Dec 2024 12:32:32 +0000 (UTC) X-FDA: 82864472040.26.43F8EC0 Received: from szxga06-in.huawei.com (szxga06-in.huawei.com [45.249.212.32]) by imf08.hostedemail.com (Postfix) with ESMTP id 5B88B160014 for ; Fri, 6 Dec 2024 12:32:18 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=none; spf=pass (imf08.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.32 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=1733488338; 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=VyfAy/0F+66WfZkRUvQ5hPfL76WheTY9mTIz1fIBfxM=; b=NV7BL0IVewzbYbXebOWhgbech4Nh/myd4rffNvAYBFzGd1zA6eJ4QL08mWyJgMrb9gA/hD SRhA9eoPR+EtGRCCoeNtxfeNtA1cB6qpxOIwEFw6GpBh11+1ij/A/ZlhJAQHy6X64RjyEx Lk7vQeCcVsaVcSx1BtHhu9r79gwFwak= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=none; spf=pass (imf08.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.32 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733488338; a=rsa-sha256; cv=none; b=vlqPOL44YsIupBiowZQRNwI2X1Td5hIsdTdx60MegkquU83crQHIq7OBy0BfoP/HtswSRZ LHWTfW0araw7KxXASClxz/DRG/lDlPUoTZEh/0U3Y83IMBMs2tULeYST3oTk4WFwsfgyvk ELQEf8JaHDeuq4243BMa+rXwUkKSdTo= Received: from mail.maildlp.com (unknown [172.19.88.214]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4Y4Vyf3L7fz1yrgk; Fri, 6 Dec 2024 20:32:42 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 708021A016C; Fri, 6 Dec 2024 20:32:26 +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, 6 Dec 2024 20:32:26 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Linux-MM , Jonathan Corbet , Subject: [PATCH net-next v2 03/10] mm: page_frag: update documentation for page_frag Date: Fri, 6 Dec 2024 20:25:26 +0800 Message-ID: <20241206122533.3589947-4-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20241206122533.3589947-1-linyunsheng@huawei.com> References: <20241206122533.3589947-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Server: rspam05 X-Stat-Signature: rrmps7w7111qbiw48gmbntqtmgd4yqmx X-Rspamd-Queue-Id: 5B88B160014 X-Rspam-User: X-HE-Tag: 1733488338-39913 X-HE-Meta: U2FsdGVkX19IwiPs98TS6pRoU5tZIz4rn0hta37LWoLTaj/7GP4tsUqbgTQtnRl8nHemsMWKfZ2ywLTR5ov13K9dA48Wm9sVt6W2NWQKpKga7/HeVimfMnmOrgOhrLQReT3HYT0y8uY+yBYT7so5K3+hFM4g4YbGw6Tvz95V/kltghJZA2is0bgPq2cdLGjpORi5TT4ca97r2aeSwVao6n4GIKoN6qp7Pk446upCXG5Iqis+r3NrjQffCGkFEKtzkeaaScfJBwu6n6pVlTlznRE8fkEmM7uOO6diOB2W8CaJvsxiyzTTkn8y7wvg49mkw9Bkvlg0ufibg6j8SS25QXL3UgaNEjzsiEHuf60Z/VmNiJzwxzIENWvIBfPipANt5Xhexc/d9PCid17oSk7e0h+laA5s9Lp4z26uidCQVUeTtcP89ILYas+wdHoS9zCWG6FLxaPgIMJ5uwCpZXRRf88kvWj0IL9RjU5q75L/QPSxj0R8a1hftLTRKiogVmJMAzogU+DLNDrqMK7d9H0LrbpIEM6MqFgJN8p/nUiBkY0qFIsi+5KGSIATYZKQDo3WSDVfnhMFUKev3jjnAbiePOTrppU+OVJBLtoz414nBxr0M6mfwrV6s2ls9J15R+JlHr6QmY44ZLCiPLSwN9gQ+NYsDMV6STlJ8WoT/2g43tgR43rS25lZoolMH9BExu1yjqP0b/PhaJw1a/YB8CRu7hyQ2MkWh5pC9LUtb8J5hsmCTu2YGB+lwfzShPMdSibtDJ9zKY+Srvvc3tFtQ9Mjn5esIaA1eEKNoz60O+vZ8RFcLmX7FL1QCy8NuSJwtiOmywzO8MG8UYZrLWBTR633uflVWtzyFYrj/opXb2BWBBBGTajHzLhgHjZdBlqRBMy3snh31SXIDJpjZ4UDjyPsNHQu7wmfBEFpR9+fuhWv0qyFfDFsbX8LxRvfty22SHHpP8CKIcC6nD/x+tJGw82 ljvnHBkc HeCHj9T5q7GoPy8UMDOA5HSQdSce7kxg2yHmtmxzl/LXR6239VoUSrnGyTljp5phBfI2btLWHtx11hKF1fxTM1vB2mfVumTPDoLn1jtrRMYtwZZtR9eiaBoA90XRmAa55OdBjZGHmYNaNrzwpwMMgKgjF0sT1Gc3ELW0dJ1aP19vIqJVm676A7WhMcP2RdzKufOpNtumE5HQ9cg0E4dA5s+OBdczdHrlxCsiSMHCET4RrhNQV9hr86530nuN+DOP8/vy20esbzqtwU7f4iwnGmzQZNq2imE2Tup0B/O0mXUeZmbCxThGZf3Y+ltyNaWSIsud4stzX1fP4wn6NIuFT6hXz250SjByYHabjTkq2+67uDP281v2Ts/wvFlFTXxxAyHj44Z9oZrDk7CieWvxy+5YxZg== 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: Update documentation about design, implementation and API usages for page_frag. CC: Alexander Duyck CC: Andrew Morton CC: Linux-MM Signed-off-by: Yunsheng Lin --- Documentation/mm/page_frags.rst | 110 +++++++++++++++++++++++++++++++- include/linux/page_frag_cache.h | 54 ++++++++++++++++ mm/page_frag_cache.c | 12 +++- 3 files changed, 173 insertions(+), 3 deletions(-) diff --git a/Documentation/mm/page_frags.rst b/Documentation/mm/page_frags.rst index 503ca6cdb804..34e654c2956e 100644 --- a/Documentation/mm/page_frags.rst +++ b/Documentation/mm/page_frags.rst @@ -1,3 +1,5 @@ +.. SPDX-License-Identifier: GPL-2.0 + ============== Page fragments ============== @@ -40,4 +42,110 @@ page via a single call. The advantage to doing this is that it allows for cleaning up the multiple references that were added to a page in order to avoid calling get_page per allocation. -Alexander Duyck, Nov 29, 2016. + +Architecture overview +===================== + +.. code-block:: none + + +----------------------+ + | page_frag API caller | + +----------------------+ + | + | + v + +------------------------------------------------------------------+ + | request page fragment | + +------------------------------------------------------------------+ + | | | + | | | + | Cache not enough | + | | | + | +-----------------+ | + | | reuse old cache |--Usable-->| + | +-----------------+ | + | | | + | Not usable | + | | | + | v | + Cache empty +-----------------+ | + | | drain old cache | | + | +-----------------+ | + | | | + v_________________________________v | + | | + | | + _________________v_______________ | + | | Cache is enough + | | | + PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE | | + | | | + | PAGE_SIZE >= PAGE_FRAG_CACHE_MAX_SIZE | + v | | + +----------------------------------+ | | + | refill cache with order > 0 page | | | + +----------------------------------+ | | + | | | | + | | | | + | Refill failed | | + | | | | + | v v | + | +------------------------------------+ | + | | refill cache with order 0 page | | + | +----------------------------------=-+ | + | | | + Refill succeed | | + | Refill succeed | + | | | + v v v + +------------------------------------------------------------------+ + | allocate fragment from cache | + +------------------------------------------------------------------+ + +API interface +============= + +Depending on different aligning requirement, the page_frag API caller may call +page_frag_*_align*() to ensure the returned virtual address or offset of the +page is aligned according to the 'align/alignment' parameter. Note the size of +the allocated fragment is not aligned, the caller needs to provide an aligned +fragsz if there is an alignment requirement for the size of the fragment. + +.. kernel-doc:: include/linux/page_frag_cache.h + :identifiers: page_frag_cache_init page_frag_cache_is_pfmemalloc + __page_frag_alloc_align page_frag_alloc_align page_frag_alloc + +.. kernel-doc:: mm/page_frag_cache.c + :identifiers: page_frag_cache_drain page_frag_free + +Coding examples +=============== + +Initialization and draining API +------------------------------- + +.. code-block:: c + + page_frag_cache_init(nc); + ... + page_frag_cache_drain(nc); + + +Allocation & freeing API +------------------------ + +.. code-block:: c + + void *va; + + va = page_frag_alloc_align(nc, size, gfp, align); + if (!va) + goto do_error; + + err = do_something(va, size); + if (err) + goto do_error; + + ... + + page_frag_free(va); diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 5ae97f93a0a1..a2b1127e8ac8 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -28,11 +28,28 @@ static inline bool encoded_page_decode_pfmemalloc(unsigned long encoded_page) return !!(encoded_page & PAGE_FRAG_CACHE_PFMEMALLOC_BIT); } +/** + * page_frag_cache_init() - Init page_frag cache. + * @nc: page_frag cache from which to init + * + * Inline helper to initialize the page_frag cache. + */ static inline void page_frag_cache_init(struct page_frag_cache *nc) { nc->encoded_page = 0; } +/** + * page_frag_cache_is_pfmemalloc() - Check for pfmemalloc. + * @nc: page_frag cache from which to check + * + * Check if the current page in page_frag cache is allocated from the pfmemalloc + * reserves. It has the same calling context expectation as the allocation API. + * + * Return: + * true if the current page in page_frag cache is allocated from the pfmemalloc + * reserves, otherwise return false. + */ static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc) { return encoded_page_decode_pfmemalloc(nc->encoded_page); @@ -57,6 +74,19 @@ static inline unsigned int __page_frag_cache_commit(struct page_frag_cache *nc, return __page_frag_cache_commit_noref(nc, pfrag, used_sz); } +/** + * __page_frag_alloc_align() - Allocate a page fragment with aligning + * requirement. + * @nc: page_frag cache from which to allocate + * @fragsz: the requested fragment size + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * @align_mask: the requested aligning requirement for the 'va' + * + * Allocate a page fragment from page_frag cache with aligning requirement. + * + * Return: + * Virtual address of the page fragment, otherwise return NULL. + */ static inline void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) @@ -72,6 +102,19 @@ static inline void *__page_frag_alloc_align(struct page_frag_cache *nc, return va; } +/** + * page_frag_alloc_align() - Allocate a page fragment with aligning requirement. + * @nc: page_frag cache from which to allocate + * @fragsz: the requested fragment size + * @gfp_mask: the allocation gfp to use when cache needs to be refilled + * @align: the requested aligning requirement for the fragment + * + * WARN_ON_ONCE() checking for @align before allocating a page fragment from + * page_frag cache with aligning requirement. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ static inline void *page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align) @@ -80,6 +123,17 @@ static inline void *page_frag_alloc_align(struct page_frag_cache *nc, return __page_frag_alloc_align(nc, fragsz, gfp_mask, -align); } +/** + * page_frag_alloc() - Allocate a page fragment. + * @nc: page_frag cache from which to allocate + * @fragsz: the requested fragment size + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * + * Allocate a page fragment from page_frag cache. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ static inline void *page_frag_alloc(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 f55d34cf7d43..d014130fb893 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -70,6 +70,10 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, return page; } +/** + * page_frag_cache_drain - Drain the current page from page_frag cache. + * @nc: page_frag cache from which to drain + */ void page_frag_cache_drain(struct page_frag_cache *nc) { if (!nc->encoded_page) @@ -182,8 +186,12 @@ void *__page_frag_cache_prepare(struct page_frag_cache *nc, unsigned int fragsz, } EXPORT_SYMBOL(__page_frag_cache_prepare); -/* - * Frees a page fragment allocated out of either a compound or order 0 page. +/** + * page_frag_free - Free a page fragment. + * @addr: va of page fragment to be freed + * + * Free a page fragment allocated out of either a compound or order 0 page by + * virtual address. */ void page_frag_free(void *addr) {