From patchwork Mon Oct 28 11:58:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13853379 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 110C7D1359D for ; Mon, 28 Oct 2024 12:05:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7716D6B009D; Mon, 28 Oct 2024 08:05:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6F9C96B009E; Mon, 28 Oct 2024 08:05:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5994B6B009F; Mon, 28 Oct 2024 08:05:22 -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 40F286B009D for ; Mon, 28 Oct 2024 08:05:22 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6EAB0121D82 for ; Mon, 28 Oct 2024 12:05:01 +0000 (UTC) X-FDA: 82722880296.15.479AA1B Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf21.hostedemail.com (Postfix) with ESMTP id D73011C0029 for ; Mon, 28 Oct 2024 12:04:34 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf21.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=1730117077; a=rsa-sha256; cv=none; b=PwjM1nahLBgjn3lbOwbDmS1MJgLdtM1V4qiaPwvDTBvNcDnED1aCu86driTN0DohJWLR3L GTrjHf0VRZpxmUUtqby9hdy4kc59MeI0nZFLBr2ARIXxQsLxCKMZLCVwA6kpIgzGaUjG5v X3Jj64WIgbl8QH+8n/1CqJC82Z4V9xk= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf21.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=1730117077; 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=TeMc0bg7v3F6lsUGeNIKppPiq5P8HaB3Z8UblrmQTG4=; b=SytOrvQGKEAnktlecp9qkaciWOUjqLkj0Ih7S7fj2T96wnZpmtF/kRku38ijLRHIydoQDs pN2RB286lzRadIcYmh1ZiGAEsGTiqq86cUg7/GplB9y7/91CB1KmEZzBlIz1dcysoyCJuj sRIXBPQlU33m019vvCPYkAyy3OWd0qE= Received: from mail.maildlp.com (unknown [172.19.163.44]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4XcX9F73TGz2Ddk5; Mon, 28 Oct 2024 20:03:45 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 9DEDC14010C; Mon, 28 Oct 2024 20:05:15 +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:15 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Linux-MM , Jonathan Corbet , Subject: [PATCH RFC 04/10] mm: page_frag: introduce page_frag_alloc_abort() related API Date: Mon, 28 Oct 2024 19:58:44 +0800 Message-ID: <20241028115850.3409893-5-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-Stat-Signature: 8s9c17h1nmydztifs157bwg7tsy8tu6j X-Rspamd-Queue-Id: D73011C0029 X-Rspamd-Server: rspam02 X-HE-Tag: 1730117074-720513 X-HE-Meta: U2FsdGVkX1+9oG5EAcK7KKUKgFGDZxAmxuJEIw7jOwLORmfLkgnHnh/jRXUnDLEnxpSpTY1FPcfyMsxHXtNyl2hWLdfDJophCS5JrlEEY0ILexCL6PsHYnsf8KEs/kOVtH2/9FyvG/e/+WB3r0fJPFeeSGuOkHByvmB8EFoealZz43bP+GGK7OnOkgxbYc7zUP1wbSiEadXhi+mgzmpiqUIw3WnL6ggALMNMFD7JRq89jUF76SInDLsDiQywm5BZdKR7ZleETniPZw/1MMEV5eF+OBS/llYw5yJdmDfOkl+tuRB5N/Rvhvupulqz3VMtUy0XN6av/v4CX5om1NG+SxdBSjdwQ6t9j9frAlo5GjBjkJH85977MqZaQER7egDDY+mP9Wpb/rLE0nffWUcgnD6DSBLtcnUtg7EkWEKkujLP9FYTcKuhYRZiYyrpettF/Kycu91XShn48moQQD2PBs9QOmoDrkjq75CpRTC7sWmiduAeZvmTNSU9eKCJDroHe6j4qknc32hkJPOwtXyCY1ZJDA9XxmM9gnYsPO5z46ZnbGXyCd7ooGjr4RvYE6j6hQ0FXxBCf5FuiVF4XkqPHil7bkpNA18fmPItdk6ZHlmbjgHeSBpOD5WvNoI7ItE6vUjtRlFTjAQIYh2BNW89gq/M7p0Wmc1qgovbEm8i9b9d1JzSNVPMok5Pid+HQBSPkjLA7DdB0G8XPRyV38cGAjcFtGE6Tm50tOuoYIYN2ldTVHO0zoCVBuTZVAiGTfiLKYnWpPqbUYt3AQOKKQryTd4XFpwiTtcyLklYjnpsvgYXT2Pj+Ec7M477ZI3J6IjxJSTbos3oAwNIb6uWJYUIFrBcN8pA0TEl+k9e2aB7v5uA+QfZh+iRVM/TMqmkHqPjIQNP2bsnDOgNUTM+iwIbQIwBYyID9zBGHhioopbhz5XM6dLLUs+mkWGkEQPTlVq8UdxmHMFwwrp73jlNlJA s+aMjIRG khie6Rj92C8prK5tuTgTKl20uVcFCcUR1oK8v9eAPgbGQ4dNvMmPeXCg0ZQbFIV28xzbW5KArzJc2EfINzIH/mQ/h31aTpZKVu0qZwBGRRnjWlyAIuEAvaT62U/VK/+1X3eKQcvtggNyHih/A1MNQ9ytS5O3hhUTiX4rlNjrkyUWlQKnSGoNRjg4uU5LkbzS5HNhyPvS+9k90MVkDzheEdBIh/xNq2osf7z+z7ukV37jPjESln/hfsYXVw6fnzimGL+BCpbIlsIsabchQyB0D5K4Ybx4e8Qn9ui0BFM7bzdnE7yblOlQsFG34g+2+g9ot33Pg8BT3+xy/E9RPTtNXT0SbfSmnadj/HDvAv4WlgK2Y/r2qM4WpvzhTJ/05IwKO8F8A0o2hRMITDnDFl3+xOgDKRw== 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: For some case as tun_build_skb() without the needing of using complicated prepare & commit API, add the abort API to abort the operation of page_frag_alloc_*() related API for error handling knowing that no one else is taking extra reference to the just allocated fragment, and add abort_ref API to only abort the reference counting of the allocated fragment if it is already referenced by someone else. CC: Alexander Duyck CC: Andrew Morton CC: Linux-MM Signed-off-by: Yunsheng Lin --- Documentation/mm/page_frags.rst | 7 +++++-- include/linux/page_frag_cache.h | 20 ++++++++++++++++++++ mm/page_frag_cache.c | 21 +++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/Documentation/mm/page_frags.rst b/Documentation/mm/page_frags.rst index 34e654c2956e..339e641beb53 100644 --- a/Documentation/mm/page_frags.rst +++ b/Documentation/mm/page_frags.rst @@ -114,9 +114,10 @@ fragsz if there is an alignment requirement for the size of the fragment. .. kernel-doc:: include/linux/page_frag_cache.h :identifiers: page_frag_cache_init page_frag_cache_is_pfmemalloc __page_frag_alloc_align page_frag_alloc_align page_frag_alloc + page_frag_alloc_abort .. kernel-doc:: mm/page_frag_cache.c - :identifiers: page_frag_cache_drain page_frag_free + :identifiers: page_frag_cache_drain page_frag_free page_frag_alloc_abort_ref Coding examples =============== @@ -143,8 +144,10 @@ Allocation & freeing API goto do_error; err = do_something(va, size); - if (err) + if (err) { + page_frag_alloc_abort(nc, va, size); goto do_error; + } ... diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index a2b1127e8ac8..c3347c97522c 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -141,5 +141,25 @@ static inline void *page_frag_alloc(struct page_frag_cache *nc, } void page_frag_free(void *addr); +void page_frag_alloc_abort_ref(struct page_frag_cache *nc, void *va, + unsigned int fragsz); + +/** + * page_frag_alloc_abort - Abort the page fragment allocation. + * @nc: page_frag cache to which the page fragment is aborted back + * @va: virtual address of page fragment to be aborted + * @fragsz: size of the page fragment to be aborted + * + * It is expected to be called from the same context as the allocation API. + * Mostly used for error handling cases to abort the fragment allocation knowing + * that no one else is taking extra reference to the just aborted fragment, so + * that the aborted fragment can be reused. + */ +static inline void page_frag_alloc_abort(struct page_frag_cache *nc, void *va, + unsigned int fragsz) +{ + page_frag_alloc_abort_ref(nc, va, fragsz); + nc->offset -= fragsz; +} #endif diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index d014130fb893..4d5626da42ed 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -201,3 +201,24 @@ void page_frag_free(void *addr) free_unref_page(page, compound_order(page)); } EXPORT_SYMBOL(page_frag_free); + +/** + * page_frag_alloc_abort_ref - Abort the reference of allocated fragment. + * @nc: page_frag cache to which the page fragment is aborted back + * @va: virtual address of page fragment to be aborted + * @fragsz: size of the page fragment to be aborted + * + * It is expected to be called from the same context as the allocation API. + * Mostly used for error handling cases to abort the reference of allocated + * fragment if the fragment has been referenced for other usages, to aovid the + * atomic operation of page_frag_free() API. + */ +void page_frag_alloc_abort_ref(struct page_frag_cache *nc, void *va, + unsigned int fragsz) +{ + VM_BUG_ON(va + fragsz != + encoded_page_decode_virt(nc->encoded_page) + nc->offset); + + nc->pagecnt_bias++; +} +EXPORT_SYMBOL(page_frag_alloc_abort_ref);