From patchwork Mon Aug 26 12:40:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13777864 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 52636C5321D for ; Mon, 26 Aug 2024 12:46:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D566C6B04BE; Mon, 26 Aug 2024 08:46:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CE1436B04BF; Mon, 26 Aug 2024 08:46:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B828B6B04C0; Mon, 26 Aug 2024 08:46:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 85BD26B04BE for ; Mon, 26 Aug 2024 08:46:33 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 34194810EF for ; Mon, 26 Aug 2024 12:46:33 +0000 (UTC) X-FDA: 82494370266.26.B77E7FA Received: from szxga06-in.huawei.com (szxga06-in.huawei.com [45.249.212.32]) by imf24.hostedemail.com (Postfix) with ESMTP id 9917F18001D for ; Mon, 26 Aug 2024 12:46:30 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf24.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.32 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724676372; a=rsa-sha256; cv=none; b=23QiV/8KFS6n7UJ++rx0IfXodxUHQP2CjxRzEotJTNqA4ElB8q/edJ2bjIQnmwZvAxjhoF uFTcsfPf4/6NaTHR5vfIAfJ5mYFWU5UAOONrH8frAozVhx8nmEegDIfJtM7bv1L7HizyDH VLmEwdphZp5PYDJP6jUnWJTHcSQNF5M= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf24.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.32 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=1724676372; 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=aXgDnx9gLdfjuniRTOHfcp8ChSvJwSzYYed7J0cqXLg=; b=Pykzbbh7uPjAw4oXWfnrQ69ufONF0sZ75twVMPELPynnwBooqAo3fdaaNXciMUT5+LFtSy GTXz0UConhhkK3RDYUu0hxuPhbcum6iL51Y8IZWj+M/WXo1hyGbzU1dbYlSEy15Sy0rKCc RaPZDFj05GSbq+2PtnKt1xA3zyzvm6Y= Received: from mail.maildlp.com (unknown [172.19.163.44]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4Wsr3K70TLz1xvNc; Mon, 26 Aug 2024 20:44:29 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id C063D1402E1; Mon, 26 Aug 2024 20:46: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; Mon, 26 Aug 2024 20:46:24 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v15 10/13] mm: page_frag: introduce prepare/probe/commit API Date: Mon, 26 Aug 2024 20:40:17 +0800 Message-ID: <20240826124021.2635705-11-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240826124021.2635705-1-linyunsheng@huawei.com> References: <20240826124021.2635705-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspam-User: X-Rspamd-Queue-Id: 9917F18001D X-Rspamd-Server: rspam01 X-Stat-Signature: osfc4f1694jirpceusrg6nhh9r388qzf X-HE-Tag: 1724676390-647912 X-HE-Meta: U2FsdGVkX18jtBXlLrWm0YpV570CFhJmK7SVUlz8M2bIYM9TJJ5yuDvLXEuo4LHkAu6I92NDaOxisJfcpY6Kzlp1C2QIcnrvY99lxeEtMUCJtZMdhkjwkt8Gvhr7VlGyC4I5Triv6sX5X2QlSzDlWHAl8rwGD5qQsbJj/g+/wKZNtRw76a3Zm7u6nQPcF2yiDqAhHQqDya767QJ5EyufNJbSUsNTN5TYNtV2w/5gNNM9s9uty2SZxghoFb3qnAszgKIPzLj6RzPCHAKpGxfpxrGsBrcvChePw6D0BacHGs2x4xNFo9IEuz8GIhhAvYZGYGSZE3vtxkh3hrjHHv4ARfxAgDluUrZr0lrfdgayNbKWJ4kzPgt0O7lVNBBxbfK6lg9iWmKyxPgsYKx0xVF/mX8T0vAXq6ztuPMNW/WyaQLj7i4JGV47saAy0MXlLh8QY+zNFj7M6ShrrKmkSpJ2N+MWVomcdx1bmVtly/TfkwYxB7ChRjopQjB/s34ygnv4rK9yDJOOiQqp3cwOX75/atuar85VtRQhioOEoyGF+iXHYf2aWGKym1rEsWBPjXlJCrSc8ZcOolK4y9th20RJ5uTFtqvfppocQieRQi0rK27VtYmK5TKvGG1RpS1oI9wH71BHKMEcTSYZyY9r57YzRHrjt2kxIWzcmZl2Rr55pYFT+oh8vcPE3WBDExXJEs7RstGX34RVCuCMW0CYW57gEDslw0nmnxy4D4EbzQ5t9ohjmKqpP5ZLktp3pIKPlG0aKf8J4d13r4PZJH+KW/KP530PlbXfyMChBBlbDikmNzvNXBko9KoR6Fkf9Cv6jMHVVbcAqHWyQhKpZh/JeIzRuUJ/S9PTnr7tkS0cjN3vaXBvzslJIQzhl6EzSudIxVkNBvlirfbpzqlulpvApXmV3bkvU0S3n6g82TQZVMTX3pmafjGMgxJpf2Tvkybx4RX52yln42f5/30mUu51GM9 amcXY8vU E/8btfgX5nY3fIK3hyJ0LJcLkX8GkPctyAZqy0JtxPUVLRniKqIkGJ8VLI6TovPZh6/3PCkoa6VwqFVgaTczupu6Eias72TNPsxmCGa5xX1xABwM400nQfxYLqdX2nVAH7NWuc3cKYqjs2dzEdyrGngSKEak8+DLQnSC0iZTiQilU5JQSbVAiLX6MU7zrQWj3u4Lr4+DhQ/blLNyOfpVMcMLHxsD7S5aitKUdFSMzR0idpVM14I0XBUvwExORmAsEWM0gbaNMnarOUDoz3010qHVSKVPAJU9MFWh7 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 2cc18a525936..9d2a7dd39d64 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -74,6 +74,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, @@ -139,6 +144,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