From patchwork Fri Dec 6 12:25:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13897120 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 42DE9E77173 for ; Fri, 6 Dec 2024 12:32:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B2BBE6B025F; Fri, 6 Dec 2024 07:32:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AB0A46B0261; Fri, 6 Dec 2024 07:32:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 978F26B0266; Fri, 6 Dec 2024 07:32:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 73ED96B025F for ; Fri, 6 Dec 2024 07:32:35 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id E7D03B0157 for ; Fri, 6 Dec 2024 12:32:34 +0000 (UTC) X-FDA: 82864472250.05.C02F90C Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf08.hostedemail.com (Postfix) with ESMTP id 125A016000E for ; Fri, 6 Dec 2024 12:32:20 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf08.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 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=1733488340; 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=tDKWn9PXBBfdu4Yz/x4AvaY5+qtAqpsrigULpBMtHIs=; b=XgCZzgSZzlMnq8lhQVOmQ0FKX+cwg5bmOqLk8jjF1WBJmxd30n6FBpYPl+erfk/+xgD72d R0ge7JY0oHUA5If45fErsfWhqd+hZAk5sJXI9krxbQDXusrkcSEX6sOJoziAkGZZadqc9Q CnaMazwVNWCkX9EhZiI52ycgSFHX1cA= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf08.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733488340; a=rsa-sha256; cv=none; b=P6oowrPPhe0RLBy220DVtaZv7hrXnkn/mVkhkK+Lp9BMJIUxRM+nXVWIvm0VlFaG5SsSyn HIu/lxyMW83V0rZGnF0sPM8i7UJxS9dqCRQChD9p6VGsSsb79ETjZFyoDf9mDzPXQTrt0a 32hoyFkBF4FA6kAYfUESTQNDcMQ9sUw= Received: from mail.maildlp.com (unknown [172.19.88.105]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Y4VwJ0q7kzqTfH; Fri, 6 Dec 2024 20:30:40 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 57E6C1401F3; Fri, 6 Dec 2024 20:32:28 +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, 6 Dec 2024 20:32:28 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Linux-MM , Jonathan Corbet , Subject: [PATCH net-next v2 04/10] mm: page_frag: introduce page_frag_alloc_abort() related API Date: Fri, 6 Dec 2024 20:25:27 +0800 Message-ID: <20241206122533.3589947-5-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20241206122533.3589947-1-linyunsheng@huawei.com> References: <20241206122533.3589947-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-Rspamd-Queue-Id: 125A016000E X-Rspamd-Server: rspam12 X-Stat-Signature: u4j8tohh63g46uyjo5aekhemj3oof7dm X-Rspam-User: X-HE-Tag: 1733488340-674072 X-HE-Meta: U2FsdGVkX19ufuXr4sM53I7tkvWGHn+xGjhMuo7JSZ0/mpsOybgQo0dyVjorUeU1nTnxDVRAkCU8Gx0eZEc/m41h8WfqejlOLTXGhcDW5AItq9IgcEJGTjsLja6wX9XZp8mGV9rQ5S47lKGUhZf2AIqabD9g0Oj17dO1DCrBhAxHmfh/GbIGrO/+Bnxn/FR/iw2qzdHNu0jGJMMOOvURmDRE64NGEQP6VGmtqRXJDqydmRBMo9+0LwIQZvPFYzTSo/m8kP9KWg+3JbWJVt8n4YoBDVjLwClGrYxn33yQ5h0INjHmBmGWYPpuExaMvxmpoYlZZNTbNwPNPWDxiCqBKjeZ/2qbvk+QOCTAeBV7qOl8gbgSzXBCAH+2GuqZS9TjsXy3D1zZhddgh6jWT+j718CehMNCq/fqhM0rBEmR9pt0RaJU2uGPl6wRckdMY4W+65kacWf/9pgIsGnmpbXGQ/jDyDrEsTuhdwWA/3B+TD9zzYD60MsTqc2TIkQhQMQifqOzVd2SMG0fAsAQmWUFFtEn+Ea8J7G7R/q0MqOfoxeT9ZEzRsJ19w8erbU3XZcwTAAKJcMWa+F8nfvgzuCOuu7vga6ytQAbG4gW+QAEiF3qUya+RlNHjTDk80flfqo+SnhrWb6rnMA1q8i4dzCn2T/vy+POiG4Q0USUB/mlMx4XiydwYk5hglrIvY6glBeXwiwzxO6qDvDzSb74kFAbhGvtdDAd5Skmxd0sSyEUMPLaik4jlwG3i575+ePTjOVLgbj263Lu85UR0Mg0pDIi8pKo8j6pXBqZ86Psd+0YIT1vdborA/iFvykRSMJHWPCsQ/uNg7qy30QjA5NHHHi4kWOHuKGnexSYLHbaLKSKdKxZcGcqKZHqk65MKPhBWssjhKWqzqIg00UT2pmrnUM4kSVjX3Hq55fC/h3zMqvMEdGUZ3j5ITa+Zp8n9f9RXMPm8/TNrmnF9MwpSJP6y7H oPPgN4cz le3RPDJgH334UNQNdeqwiB8eo9r6/5Kap0RZgS2JWEL/zczyAKRemv3EuLdRRu1gAs4FkOE+REhQxyp0o09XqWl/2KTQOQx+jksw5vPuqgQ8V6bzcuIaQDgOC8K1cH/N0FEWYNYmA9SXezML99cuXerkG60jEzGSEMZ7ENdPLOVy8M50vqsSYTT9gNLO83JYD1Gzd/TOmswQ5cxnJkd1FmRoP6oQxiLrRIJKX+TsWBFKROMpFh9BO0Jc7qiUO1YMs5LbHxJVnEfDdK3tgV5vdgySL4DIWggQqXL/ircENIh2XsadX1wOqZ/ctk+zFerrcvfuG8WEAUS0XzhbTnVrjq6KEN/fr4lDBMEoNCrtPe2CkRTVQshPLhxyBwKFQjp4FqPeVz2xrWhN5NKpgxbFHLy+UWw== 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..8c3cfdbe8c2b 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 avoid 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);