From patchwork Fri Aug 30 11:18:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13784988 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 33D4BCA0EFC for ; Fri, 30 Aug 2024 11:25:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D127B6B011C; Fri, 30 Aug 2024 07:25:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CBFF76B011E; Fri, 30 Aug 2024 07:25:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B3A296B011D; Fri, 30 Aug 2024 07:25:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 8E0156B011B for ; Fri, 30 Aug 2024 07:25:02 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 4E4ECA173C for ; Fri, 30 Aug 2024 11:25:01 +0000 (UTC) X-FDA: 82508680002.12.729CC36 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf27.hostedemail.com (Postfix) with ESMTP id B354C40014 for ; Fri, 30 Aug 2024 11:24:58 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.191 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=1725017009; 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=FUCeeBgVqwaeXscTtetjTtkEljm6bwsDMBXon+v1SBs=; b=JR6OqK5KGFydGOul5EocnpPx9gmFz0nVD3swiHbcr6fhldL7q8s+YkS/GTIu1N7Es2iBJc 0osgLBJDmeKTqmfq7DmPqhU6lTQoPiA4fzhMKSO3dP2M+z71Z4e+rhbhdV1GFhCwra2eM3 DvSKJV2M1o2Wk7qVZodGZtZkercUrR8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725017009; a=rsa-sha256; cv=none; b=pUQD5JPAxhAirG3xsvMk6zcRTBatWIZ37LIZJ3DUCET8juQi93CF+5KCJV2fSqbstUNrYz MBY2IiHB68p3VLHjIkl6rz7oYpXs0Y3cTjcqF57fWpYhvGtjv22xHHg/AT36/sZmLkWrVm G1hPGN94RaGc8+tawD+sMt70lMI8k7U= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.162.112]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4WwG5P5C24z1j7RC; Fri, 30 Aug 2024 19:24:41 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 7945F140136; Fri, 30 Aug 2024 19:24:55 +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:55 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v16 10/14] mm: page_frag: introduce prepare/probe/commit API Date: Fri, 30 Aug 2024 19:18:40 +0800 Message-ID: <20240830111845.1593542-11-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: 81s8rq3igmz965fbc8qrc6uw8bc3fng7 X-Rspamd-Queue-Id: B354C40014 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1725017098-486651 X-HE-Meta: U2FsdGVkX18ffo2eAZm6ZefkqYOgt/8peTO1S1eSXnuu5fx4lsc54ZfCBPkWoihoi/dQNUFZLmMjHTCoq9LzfR/6jk4skmIYXGPf5DK8ZmjzQKXREPA0yxAahsoaZjgMoT4lcJLAI7TjQo3j0nxQJ3UDwn6gUZ0JGK+5rCV6Y6aYwjUD2dy7dESbujyq9OfwH9PXhtr8s+Y7u9xY9GsVTjPDnz7vWrx/4FT95fHUOkbCBVS15utIZToExCZiHF8uvAjXHcnOmiEEIcY9BAfPzUvTA9bXHD9zhWVzXRP8iyaJRYew5RbKan2gPskYuSJySCAnUoN5nFxChEgaHuu5YTC8XlxqFKnjczfp9ukH/qqP8ConYSg4JVOTrvTk+LUCLGyhXqFQpxjYQzPbug++cKxPIpwihkhyHbRNj017gxVb1E/4LmROQSjGPYnT0aR8ikNMDsYe4siODTuIGBPJSXVx5Q6u7b2gz0kNlZGFlzhV0ggxWwULJpMMV/HCo04ZMgxE0iyqC0uptzlJzj2qiVlxnb2dS/SVHEDJ/KOk7wXb0sgGNnwP48z9T33XDhtzV/4qvSaNsV8iupCvTpUgp1wsZoYi39kSQ+DxQTqa2unbP9ctJDQPyLlIPRj3VR1gEwkTG8b1nXCfpKjTozJXNNqh1C1/2vTHVOVVnakOnzyDfzuUNihjU9UxuDA4AzF5keJxHmelSv8B0Q3yWjrHl4bwBjuQI5ORNo6jalWPaCftz0Gjr9oaZ2rFGxBCcHHTV9jUC98UFtpep8GKAly1us/CUnNZcwc9rg4RNNkWJSvqVFPXDKzpGGEE3F/u/EmaDF+ASWS2GA1ns9WByPuRYxyns0mD2J/O50oOe9IFSJ+B7k8rbvAV9+mlEhd1zhHjQtiRdgziSujH7ck8eZzKILJxoV2iVzqV1EzsCt2pNgAFxy+7J3GKNyRRsfzosfzFbRU80P03TRNn4DHZEKv ZpJbbVAK lUIt19b+yWHUMB2rTqRE4dg4fLPMsH00VvTS7TGE07Lx+85gAmbpOX1eNiTh1KF79KMeNigjpnPJhnbjqzvv/HrBUbKC3/o6WrveQSgi9FIvjp1/W1h6WZpMKAgx+2OY6BjmnoZNRY+aUxhbOFtEtwWv3BSwmfffJagVUAdI66cGnr/ifaj8cBluPSx50S6RTTVR7lyS7K7bF7WwwLkrXcD+nigICOK/U1SUSYjHFN72JxIWA+G6GTDM86ks9xoDF1CspbbYEjvHL/SkvazOtlzfLuYMmtONgUDFZ 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 | 138 ++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index f786ec572aa9..48a88ef8b5c7 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -61,6 +61,11 @@ static inline unsigned int page_frag_cache_page_size(unsigned long encoded_page) return PAGE_SIZE << page_frag_encoded_page_order(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, @@ -122,6 +127,139 @@ 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, true, 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_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_frag_cache_page_size(encoded_page); + offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); + if (unlikely(!encoded_page || offset + fragsz > size)) + return NULL; + + pfrag->page = page_frag_encoded_page_ptr(encoded_page); + pfrag->size = size - offset; + pfrag->offset = offset; + + return page_frag_encoded_page_address(encoded_page) + offset; +} + +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, true, 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(nc, pfrag, false, 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