From patchwork Sat Oct 12 11:23:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13833399 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 DDAA0CF2564 for ; Sat, 12 Oct 2024 11:30:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 69AE86B009A; Sat, 12 Oct 2024 07:30:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 676A16B009D; Sat, 12 Oct 2024 07:30:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C3FB6B009C; Sat, 12 Oct 2024 07:30:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 2A0866B009A for ; Sat, 12 Oct 2024 07:30:01 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id BF5C7C1D07 for ; Sat, 12 Oct 2024 11:29:54 +0000 (UTC) X-FDA: 82664730918.28.002C353 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf29.hostedemail.com (Postfix) with ESMTP id 7B9E5120018 for ; Sat, 12 Oct 2024 11:29:53 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf29.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.189 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=1728732460; 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=M4MElfOdOKPXikLc136kjFUqweVuuhZYgYs2LvRVMWo=; b=j6ylht2Ow6iqruXTvE42zUmeSKQ4kXI+iWrSMhSyxI17najhwT+Vk4TneI5Cr7PEQq/Efq nLWghxr3xjW/CTrxgwYRoTuiVLUBXL6KBrTUTrtl8ZJAytIF+rA+83pPVnXdm5HB/t3KhJ Nv6zcmCpaeS1VGv30AIYvA5PjPL0W4E= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728732460; a=rsa-sha256; cv=none; b=B+XlFyEMsIwfDzqwKj18d27lPSWoRAAVK5c62+GkL9vV1qSXjDtKo6K00Fty1sts4CyawW OS6znj+Y9rd6NWxku/Z6tZkdNG7U+PyTqFPOftlHscPvEM4mWPnPh0ndZ92LWhPaMOfRgP Ms5tOAN+tBQYb9r7ljHjFoUGn5g5lRg= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf29.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4XQh8n2VjszQrcZ; Sat, 12 Oct 2024 19:29:13 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 65C0D180087; Sat, 12 Oct 2024 19:29:53 +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; Sat, 12 Oct 2024 19:29:53 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v21 10/14] mm: page_frag: introduce prepare/probe/commit API Date: Sat, 12 Oct 2024 19:23:16 +0800 Message-ID: <20241012112320.2503906-11-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20241012112320.2503906-1-linyunsheng@huawei.com> References: <20241012112320.2503906-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Queue-Id: 7B9E5120018 X-Stat-Signature: 4w4je3oba61hbqac7yroebf36o3w1q8r X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1728732593-194476 X-HE-Meta: U2FsdGVkX1/HJ4eMGVWggoVdaG51Lw/Xh3gw80glXxeFIyTU3fBlCRAnOHO+UZ4bjGHPSbQsa5lFe5o8iwtSPwML09kG0FFtl0AJc21KKrc01K6F8qI5j9Cr3djfpjUBLkyZS6LddVDQLXaPBD6hYYHcUDflW1KSx4okIfLqNkW0M1ahsjT/47R0AIdf66UD74ZzrGXUFZKbxqusa17QzKwti04EFKx0KUdE84ronbgfABQXfLhH1oOqm8My43W0nlLLNBwgQke5HkvoAVcaEFkGuScw7/89U0AE9z2VQdPUpPJNz09t5eLhGMjYG3JKiMwh7ZtUjhWb5Pj60YiijzIW2RUSetcxT3Iw1W48DlH9CQ1rJoHbcbO1ZyAUmcDTe3J6oVP2dGJsekd67YNT2z1bxvizqrV/bdLuah0UAhZda8IP4YygnE8vcGwBI6+XY3wk/Bj6WeiXQ5sOiKlbAJAlZocQXQZDUlcN6vCawv6Edyee8ckRLAUOe0JF64WUIDB3RV2ViAk/7vuYOnSJYHfAklj7QIg67g6Kp0ku1IdZ+ss+024xH+90w2pROBZcCcaDHsSqaNPTBunI0n+rQdacxF7ku3VsG1mCTyEvEmFpsuq1eE13a+Y0EwJntrBjD5qVd6ffzMZX5bJF6sqjyO7LBbhhbWZ24GnviPljB5ewFOvY8dTTPIXWhaV58XbkracB1DODMhYCeDzloJxvotw4digGe4vn0KEWNLKlV/6DF7ga5S1sxT1mZDzR10Y6EOqoSOUmXUGHpzMKxDOdTYt31OOy25P451neSe4jZici5lxWfkiDtQ+DyK0cCLcffh9QZhIN7CXYvXH1r3hzDEJuyVguHqgXISOsAYVmvTtxm1ssjy5nVBX6+TpUNnXsMW9jkkF6jf18Zp1CTAHgO+m04gB5+TzkQltt9QnLm83jfLxZU0PJ1hApBSJxn2Wndzo0EE8TSJStNb37QIk e0plDv45 GLa4Pd3+X5wlco7zb8ETGgT6iEVUH5IK/mmKv8TybHxFDE2tzsd3ULIux7K0gnBlkZITDmU3qldhnl/o+aq3OZtuqrU//UcEmCkaglzohaWuPB2UNnlOzgOhXdC2fGiu5YDI/Wg5ZghKskqTjox0nEANAgokAX5dGPaUYWMmR72rEl9svfChCF37Sih2ugRfFGO9L7Zu9EcEDEl1B3/QQ0uYfnQ6XOVR+iUEdOti2KCwuGbVTq4LlTDHaCUzAglOwqdXk4J2uKKX64bVSTtHD9RgLsB9SlApuh3A+uu7rELXvaX8kdu4+J7nhUH3jn/Qr1ahBDCEDHPfD6ZkgjfO38IkyqBFZ9Co/j70RHL0ns9OB9pWsXfHkUJAZL2SqSfJ8FlGpNDp2vxFhQsJ1Qk7s/iypciL7Bjj451F/GfjZPMWdT302WjgptnrUq5mT6/u1F7UN 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: There are many use cases that need minimum memory in order for forward progress, but more performant if more memory is available or need to probe the cache info to use any memory available for frag caoleasing reason. Currently skb_page_frag_refill() API is used to solve the above use cases, but caller needs to know about the internal detail and access the data field of 'struct page_frag' to meet the requirement of the above use cases and its implementation is similar to the one in mm subsystem. To unify those two page_frag implementations, introduce a prepare API to ensure minimum memory is satisfied and return how much the actual memory is available to the caller and a probe API to report the current available memory to caller without doing cache refilling. The caller needs to either call the commit API to report how much memory it actually uses, or not do so if deciding to not use any memory. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 135 ++++++++++++++++++++++++++++++++ mm/page_frag_cache.c | 21 +++++ 2 files changed, 156 insertions(+) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index feed99d0cddb..e21688e7edc8 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -38,6 +38,11 @@ static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc) return encoded_page_decode_pfmemalloc(nc->encoded_page); } +static inline unsigned int page_frag_cache_page_offset(const struct page_frag_cache *nc) +{ + return nc->offset; +} + void page_frag_cache_drain(struct page_frag_cache *nc); void __page_frag_cache_drain(struct page *page, unsigned int count); void *__page_frag_cache_prepare(struct page_frag_cache *nc, unsigned int fragsz, @@ -46,6 +51,10 @@ void *__page_frag_cache_prepare(struct page_frag_cache *nc, unsigned int fragsz, unsigned int __page_frag_cache_commit_noref(struct page_frag_cache *nc, struct page_frag *pfrag, unsigned int used_sz); +void *__page_frag_alloc_refill_probe_align(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + unsigned int align_mask); static inline unsigned int __page_frag_cache_commit(struct page_frag_cache *nc, struct page_frag *pfrag, @@ -88,6 +97,132 @@ static inline void *page_frag_alloc(struct page_frag_cache *nc, return __page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u); } +static inline bool __page_frag_refill_align(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + gfp_t gfp_mask, + unsigned int align_mask) +{ + if (unlikely(!__page_frag_cache_prepare(nc, fragsz, pfrag, gfp_mask, + align_mask))) + return false; + + __page_frag_cache_commit(nc, pfrag, fragsz); + return true; +} + +static inline bool page_frag_refill_align(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + gfp_t gfp_mask, unsigned int align) +{ + WARN_ON_ONCE(!is_power_of_2(align)); + return __page_frag_refill_align(nc, fragsz, pfrag, gfp_mask, -align); +} + +static inline bool page_frag_refill(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, gfp_t gfp_mask) +{ + return __page_frag_refill_align(nc, fragsz, pfrag, gfp_mask, ~0u); +} + +static inline bool __page_frag_refill_prepare_align(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + gfp_t gfp_mask, + unsigned int align_mask) +{ + return !!__page_frag_cache_prepare(nc, fragsz, pfrag, gfp_mask, + align_mask); +} + +static inline bool page_frag_refill_prepare_align(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + gfp_t gfp_mask, + unsigned int align) +{ + WARN_ON_ONCE(!is_power_of_2(align)); + return __page_frag_refill_prepare_align(nc, fragsz, pfrag, gfp_mask, + -align); +} + +static inline bool page_frag_refill_prepare(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + gfp_t gfp_mask) +{ + return __page_frag_refill_prepare_align(nc, fragsz, pfrag, gfp_mask, + ~0u); +} + +static inline void *__page_frag_alloc_refill_prepare_align(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + gfp_t gfp_mask, + unsigned int align_mask) +{ + return __page_frag_cache_prepare(nc, fragsz, pfrag, gfp_mask, align_mask); +} + +static inline void *page_frag_alloc_refill_prepare_align(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + gfp_t gfp_mask, + unsigned int align) +{ + WARN_ON_ONCE(!is_power_of_2(align)); + return __page_frag_alloc_refill_prepare_align(nc, fragsz, pfrag, + gfp_mask, -align); +} + +static inline void *page_frag_alloc_refill_prepare(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + gfp_t gfp_mask) +{ + return __page_frag_alloc_refill_prepare_align(nc, fragsz, pfrag, + gfp_mask, ~0u); +} + +static inline void *page_frag_alloc_refill_probe(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag) +{ + return __page_frag_alloc_refill_probe_align(nc, fragsz, pfrag, ~0u); +} + +static inline bool page_frag_refill_probe(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag) +{ + return !!page_frag_alloc_refill_probe(nc, fragsz, pfrag); +} + +static inline void page_frag_commit(struct page_frag_cache *nc, + struct page_frag *pfrag, + unsigned int used_sz) +{ + __page_frag_cache_commit(nc, pfrag, used_sz); +} + +static inline void page_frag_commit_noref(struct page_frag_cache *nc, + struct page_frag *pfrag, + unsigned int used_sz) +{ + __page_frag_cache_commit_noref(nc, pfrag, used_sz); +} + +static inline void page_frag_alloc_abort(struct page_frag_cache *nc, + unsigned int fragsz) +{ + VM_BUG_ON(fragsz > nc->offset); + + nc->pagecnt_bias++; + nc->offset -= fragsz; +} + void page_frag_free(void *addr); #endif diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index f55d34cf7d43..5ea4b663ab8e 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -112,6 +112,27 @@ unsigned int __page_frag_cache_commit_noref(struct page_frag_cache *nc, } EXPORT_SYMBOL(__page_frag_cache_commit_noref); +void *__page_frag_alloc_refill_probe_align(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + unsigned int align_mask) +{ + unsigned long encoded_page = nc->encoded_page; + unsigned int size, offset; + + size = PAGE_SIZE << encoded_page_decode_order(encoded_page); + offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); + if (unlikely(!encoded_page || offset + fragsz > size)) + return NULL; + + pfrag->page = encoded_page_decode_page(encoded_page); + pfrag->size = size - offset; + pfrag->offset = offset; + + return encoded_page_decode_virt(encoded_page) + offset; +} +EXPORT_SYMBOL(__page_frag_alloc_refill_probe_align); + 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)