From patchwork Mon Oct 28 11:58:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13853384 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 42B23D1359D for ; Mon, 28 Oct 2024 12:05:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CA3466B00A9; Mon, 28 Oct 2024 08:05:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C511D6B00AA; Mon, 28 Oct 2024 08:05:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B4ABA6B00AB; Mon, 28 Oct 2024 08:05:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 94EB36B00A9 for ; Mon, 28 Oct 2024 08:05:55 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 8EEF21A1DC4 for ; Mon, 28 Oct 2024 12:05:14 +0000 (UTC) X-FDA: 82722881808.18.E63E4FF Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf24.hostedemail.com (Postfix) with ESMTP id 4BF41180020 for ; Mon, 28 Oct 2024 12:05:48 +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.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730117099; a=rsa-sha256; cv=none; b=1WVCrH9Qv+TpwWvlET9T+7hVZfzocfQiOnT0pHZkXsnVVpXiXcIsko3mqP/SGomhjFXs8q bCGFN/DhOSASE+HtLFbVosI5MCCeFztYLuc1qooQgEQCmGEomDeIhOOQ9azvApiKC3iqlS iAGKCBhi0MjoQpPRilzkNGKOHMhRams= 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.187 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=1730117099; 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=ooXczewp08QSJqELbo5IIik6VDJf32qiF685p0eWDnA=; b=veJCCDDckoOC8NpZSLwvLABj3y/qLPkFG8HXh1OFOdSXLJvAb3ILkcUEVk0C8m0E20mTLg QlOPWpFkqCr7f5P1+b4dyYFSVHfjZdAmFg6dp+nPtllHDQY/dDs+oGwNr+dbXxW+XQ4Dfj DszWvP3bhsnQ2pr/1n1LTPowHw695Yo= Received: from mail.maildlp.com (unknown [172.19.88.105]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4XcX8Z47YSz10P7T; Mon, 28 Oct 2024 20:03:10 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 24E20140202; Mon, 28 Oct 2024 20:05:19 +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, 28 Oct 2024 20:05:18 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Linux-MM , Jonathan Corbet , Subject: [PATCH RFC 06/10] mm: page_frag: introduce alloc_refill prepare & commit API Date: Mon, 28 Oct 2024 19:58:46 +0800 Message-ID: <20241028115850.3409893-7-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20241028115850.3409893-1-linyunsheng@huawei.com> References: <20241028115850.3409893-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-Rspam-User: X-Rspamd-Queue-Id: 4BF41180020 X-Rspamd-Server: rspam01 X-Stat-Signature: p9m5xrpi7om1z89ituirxfsxiw5gtps9 X-HE-Tag: 1730117148-666875 X-HE-Meta: U2FsdGVkX19c22TzBaBkIJs5yqFoigw1auHQqhcF9yWyoarb+BJdK+/XmKAlJBDvhSyrBxY6opRJcoz5XeRa6dkr4GzWEGgUhOFExp2j7D/7dy6nYaOTAWb93Nu/toh1gYd3DJssU+2vLb3MBfyAyhWj9MK1IcZQkyvmA7ohDm/X+CfgarewkZ6ln2dUnXcbN6fUu9nCpCmdf1hMMkvdq9+fIZw7V1On/VWk7USXhKRblAVgz8aGgC/0vPNdSfz5JNVqIGtzPbTuTP9zv/YSyibC2TP+XPzSBIRxGDrFA+zqZpFNiCJ+tlo/SQlRpR610zPVB/TAVl3o9Q/GWaI8WQtXi2jS8mF4ku8L0DIKPnSWMFnWJCyhZo5NLdt5AaaotZ0LkWWVUO/zQNuRpV2KN+k/WSlzHA+Bc/AyE+VGN8Ilffrw36NPROcF0wOfBjxodHT/fEy3qCM3IU18NhiZgGQZT7V2cqq3YRQf8ZAsbK4W/sMPi/b5SCJSnX3WrAxjdjOpYqbNVN1n/4HN1jha3W6RiauiPUQlGB//JDfI4uENuNKMutfOWb1H0/upfzRw6diQ5p/WW8RLjueKDJlwEsrTczyUK1bqQoptw3hGh3ewSmp8/pdwVJ3Qa+8LO5fe4VGz9vPQ3eW9FthE9PLvM/wHiCCEVOUVDnUKvzRDACVz1cH95kWaPBZGgx6dd5jbL1Qe/u+VIjZ8FICwdHd5Zdi+etehp39Kn7MVopN+yDypg/VbW3etMN/Ker24YbqjFMuEdtFTCCqPplL3TE9hYoNSt4wcdn1702YTW8q5Sd1k3djreEBpHeLV2xUUwi5zZqUUKyO10ozZ2+AOpAgtLZhyykxpAvfM16Y01LOZsmCJqd0+slaBIoQImCDsFwMRscsWmfiLIo1n6u15sedtKD+WgKt4qsX52JxBPYTMMtnBgFQLWNfQHK/qTfcG6d86IAOWXYC6Zt+27U7rNwQ yh+S/JX3 3stjQVoe3NM1kU7AP9SCAq0ttKtcbq8BiaJeu/1B2OyzcrSAvckPU9cbIXJ0aDuc7t17WnLkcUDn/mDtUdFDR1lASAlgnnM9tZ9MGm6zWCrzgPlSB7rgRJmojnhm9rLRZD3LONn4isf3iqe7NAteuz8jKo4oFEjWveUutYNhNgGd8vGbjlczZDVk/f3u0JlRx1qTpp71vkIVS6V4j49eDggFSeNUMvHlsB90N8PwzopNHm7cjTbCyk2V65Dzg3uKh+FOSGEyy4kP/A6WWXHaGkL0afinQAIQ9g2KSfJ7B1LEnN/+vBl/rZre1eckQDqTC7rd5IxRqYtiTUP2HdCVAtxJhsPY5+Yw60VA7juRfDeiS6QrsQ/WGwGLmGad75scMTCD+jPVzNPNz1VwS4KsaOGSuuA== 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..dcfceee3b923 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, or both va +and page may call alloc 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