From patchwork Thu Nov 14 12:16:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13875063 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 0387ED41D46 for ; Thu, 14 Nov 2024 12:23:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4247F6B0096; Thu, 14 Nov 2024 07:22:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2C3266B0098; Thu, 14 Nov 2024 07:22:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 13B116B0099; Thu, 14 Nov 2024 07:22:59 -0500 (EST) 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 ED9D46B0096 for ; Thu, 14 Nov 2024 07:22:58 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A99501C6DA5 for ; Thu, 14 Nov 2024 12:22:58 +0000 (UTC) X-FDA: 82784613786.15.C705837 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf30.hostedemail.com (Postfix) with ESMTP id 034A980004 for ; Thu, 14 Nov 2024 12:21:32 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf30.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731586913; a=rsa-sha256; cv=none; b=z8TVklmTKKBBA2bxjyZUV9jTvdIZSJ16GVaBpDmCNDYyUwPGdV+p/6nGuNEjeBh4ayDRa+ zMcYEsf4r0aAOhoQ/i2/wmv6Ea1SvVaqHLyWJ3fLt6Rcif0F3/FaapaccA8oezSPhLrJeD vcl8g0/Feea7YaXfXm+u78WkFCtjIA4= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf30.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.190 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=1731586913; 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=lvuyRUjEeqKVfMNpsbMVljnBq4TEBQUgRXvscG9RWWs=; b=TgSH3Sfq3J8gZtJz8ql2Inpi+fWr2uKxWg8Wy2UivyCm3yna9XsV2Znz3Z867DannYB7jU mJGedrFh7oPWWqdZ1YS2zdualfRWu9K8XMp6duF0DfsZOz9eajKrjcu9YxIusgZTsbU9vw gdZtiR4EHnEMapMlMdODvb1NtgKP/C8= Received: from mail.maildlp.com (unknown [172.19.163.17]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4Xpzm16HH1z21l8Z; Thu, 14 Nov 2024 20:21:37 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 8E7881A0188; Thu, 14 Nov 2024 20:22: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; Thu, 14 Nov 2024 20:22:53 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Linux-MM , Jonathan Corbet , Subject: [PATCH net-next v1 06/10] mm: page_frag: introduce alloc_refill prepare & commit API Date: Thu, 14 Nov 2024 20:16:01 +0800 Message-ID: <20241114121606.3434517-7-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20241114121606.3434517-1-linyunsheng@huawei.com> References: <20241114121606.3434517-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspam-User: X-Rspamd-Queue-Id: 034A980004 X-Rspamd-Server: rspam11 X-Stat-Signature: 71mr755mufgdwfbnfp5h8ztbcu7a54mz X-HE-Tag: 1731586892-299668 X-HE-Meta: U2FsdGVkX1+i9ynjk96GGzttl05SBnkYJ697SuXCIYEB3EbIbw0yYIFcelTVFuri7xgU+Cy3lKgYD+HciQ8znGa0mDZ4IW0nCTPIgq7LqAfnlkWAq7YT7rI5EuYsU9O91fyFsWjlORZQ+xAbS+/HwBHnsPtCjqZWrEaC7bowZaoiGKcE9xTzPV19E8tYnBY/NsnejkhhK6RpA1GoRbAb/e3eDNPiTyBBb1PTIN3fTyxZmT62mWR0X5KrR8LSN4moSFhPH1vLKpyVLFKkG1EhuPJnktoMRSgH/AsaopxBYUpMKWGHK7dB6DMI9ohof+i/uUhNlgFUTANB4eIG4UAn4vlSKABzGXYVFI8yIXtIT9EzifmoSAVq2tuQp96S9HKjUe/afwM4voNi12JbRCh1QfFeFANXrYBvjhw6K+fv2pA4aWiEPJ4M441YDnAEYNaFrABq3IFtMrzwWV8VSSN030Ch4Vo2IM/lWicruhh7xSu7QSFSGJIboxZ1A6oCnufmgL6MKYKAi7tlnkiVEIS+pEeMFksILH62ZtzqhJ+jPHBNySgeEGKxUeiCtaoqB7PYed4S2tpgyJWDAaBYpWEYZGCTygsLP+4XBHxwhos8vzxzMbK+01qNZdCZIxEMsrIV8fPxwhurTEWFEyZ6furwFYrBupoc2bxos54mfGhjCoPBeFjUp8jcwT2uBe0/LXE+dy+N3J5Fe4Ok8vuCxg/Tj0NC04VKH3qoJxCe+bLUABGHG3VE4ht5KsBi5BssjaLOT/+6F7eJoF1z793g9aX1v7ldrIj51uOwEsISL9h1TeZX1KVSgiJgcKuI5T/syxdfhng4N60n9HX9T4lxtwtY4kIARHa2Ej9Umz5HQMwfcBqJWLAMzJ6ZJutsTFCf2liTiwhINSFMSxh9HGBVqflZI75KD7qXw+tWTgZRUNQaXARBkgdqJ+eiFjGppNc6HbJ8rmVfnMKJ8CEG9zZklrP y7OrT/CP CpFbzzWuqpOZcYrdH4bP0H5vCqiShM1YozbZUbupHLH5oVDTWZ5cuYNrvVT7Fvu3xkTwb6lQf38/71u+816LwmQ/+HlU7lxRPKcYYpaSr2sQ60Fw/BL4SiSVbCumhPOGr7NaMvCN2a1Nl7xvVZshgOtxVgUAraY+67Y6Q+yHvCcfvFHeN+RQMdpDvZMKGXYHywH+IitI0oNzRlvLJ3ECoSg/WkNA8SIhnO03ZaM2dyEoqb9ncNRA1RrA3/C753sl31y6XTozeB4S6vlZSu2yrv9H3N3Inym4IoyZV7SpanFYprOTpBDttcc2U8xiEVkihLBIHlx6Im6q3ZQzJ0dVJxKHbcOlNP0sZZbgZKpWjdDTFO3Kkr+XTSBfs/yYkux8uhBLy8gCU9ljQoiNsrQfWvW8Nsw== 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: Currently alloc related API returns virtual address of the allocated fragment and refill related API returns page info of the allocated fragment through 'struct page_frag'. There are use cases that need both the virtual address and page info of the allocated fragment. Introduce alloc_refill API for those use cases. CC: Alexander Duyck CC: Andrew Morton CC: Linux-MM Signed-off-by: Yunsheng Lin --- Documentation/mm/page_frags.rst | 45 +++++++++++++++++++++ include/linux/page_frag_cache.h | 71 +++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/Documentation/mm/page_frags.rst b/Documentation/mm/page_frags.rst index 4cfdbe7db55a..1c98f7090d92 100644 --- a/Documentation/mm/page_frags.rst +++ b/Documentation/mm/page_frags.rst @@ -111,6 +111,9 @@ 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. +Depending on different use cases, callers expecting to deal with va, page or +both va and page may call alloc, refill or alloc_refill API accordingly. + There is a use case that needs minimum memory in order for forward progress, but more performant if more memory is available. By using the prepare and commit related API, the caller calls prepare API to requests the minimum memory it @@ -123,6 +126,9 @@ uses, or not do so if deciding to not use any memory. __page_frag_alloc_align page_frag_alloc_align page_frag_alloc page_frag_alloc_abort __page_frag_refill_prepare_align page_frag_refill_prepare_align page_frag_refill_prepare + __page_frag_alloc_refill_prepare_align + page_frag_alloc_refill_prepare_align + page_frag_alloc_refill_prepare .. kernel-doc:: mm/page_frag_cache.c :identifiers: page_frag_cache_drain page_frag_free page_frag_alloc_abort_ref @@ -193,3 +199,42 @@ Refill Preparation & committing API skb_fill_page_desc(skb, i, pfrag->page, pfrag->offset, copy); page_frag_refill_commit(nc, pfrag, copy); } + + +Alloc_Refill Preparation & committing API +----------------------------------------- + +.. code-block:: c + + struct page_frag page_frag, *pfrag; + bool merge = true; + void *va; + + pfrag = &page_frag; + va = page_frag_alloc_refill_prepare(nc, 32U, pfrag, GFP_KERNEL); + if (!va) + goto wait_for_space; + + copy = min_t(unsigned int, copy, pfrag->size); + if (!skb_can_coalesce(skb, i, pfrag->page, pfrag->offset)) { + if (i >= max_skb_frags) + goto new_segment; + + merge = false; + } + + copy = mem_schedule(copy); + if (!copy) + goto wait_for_space; + + err = copy_from_iter_full_nocache(va, copy, iter); + if (err) + goto do_error; + + if (merge) { + skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); + page_frag_refill_commit_noref(nc, pfrag, copy); + } else { + skb_fill_page_desc(skb, i, pfrag->page, pfrag->offset, copy); + page_frag_refill_commit(nc, pfrag, copy); + } diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 1e699334646a..329390afbe78 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -211,6 +211,77 @@ static inline bool page_frag_refill_prepare(struct page_frag_cache *nc, ~0u); } +/** + * __page_frag_alloc_refill_prepare_align() - Prepare allocating a fragment and + * refilling a page_frag with aligning requirement. + * @nc: page_frag cache from which to allocate and refill + * @fragsz: the requested fragment size + * @pfrag: the page_frag to be refilled. + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * @align_mask: the requested aligning requirement for the fragment. + * + * Prepare allocating a fragment and refilling a page_frag from page_frag cache. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ +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); +} + +/** + * page_frag_alloc_refill_prepare_align() - Prepare allocating a fragment and + * refilling a page_frag with aligning requirement. + * @nc: page_frag cache from which to allocate and refill + * @fragsz: the requested fragment size + * @pfrag: the page_frag to be refilled. + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * @align: the requested aligning requirement for the fragment. + * + * WARN_ON_ONCE() checking for @align before prepare allocating a fragment and + * refilling a page_frag from page_frag cache. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ +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); +} + +/** + * page_frag_alloc_refill_prepare() - Prepare allocating a fragment and + * refilling a page_frag. + * @nc: page_frag cache from which to allocate and refill + * @fragsz: the requested fragment size + * @pfrag: the page_frag to be refilled. + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * + * Prepare allocating a fragment and refilling a page_frag from page_frag cache. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ +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); +} + /** * page_frag_refill_commit - Commit a prepare refilling. * @nc: page_frag cache from which to commit