From patchwork Mon Aug 26 12:40:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13777862 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 E8E28C5321D for ; Mon, 26 Aug 2024 12:46:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7AEE46B04B7; Mon, 26 Aug 2024 08:46:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 679F66B04B9; Mon, 26 Aug 2024 08:46:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 455F76B04BA; Mon, 26 Aug 2024 08:46:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 13C696B04B7 for ; Mon, 26 Aug 2024 08:46:26 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C48F7121076 for ; Mon, 26 Aug 2024 12:46:25 +0000 (UTC) X-FDA: 82494369930.19.2FC8428 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf27.hostedemail.com (Postfix) with ESMTP id 5959B4000D for ; Mon, 26 Aug 2024 12:46:22 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724676270; 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=gNl1cpEVOeSfQXwVUyo2lvHkQahe05XKrmCRSgP0KAM=; b=wmeq07JMa/+M77tNqPS3w4xEBfUiJCrb5cQWH06dJI1N5boy1PVwe19j4WL06x85ZZgLJ9 nhkhM41olJk3+qMUJrZy+Kp/023EL+HfHLxzRv8F1I0RIY+LFi/yMJiAP4riRIRCjGxKvC q508UF6kqIwK9QbqGW5VOpFH5yhX3Nc= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724676270; a=rsa-sha256; cv=none; b=WvAhSKxsEgHpZ6ffd1PHTbUY2cY/Mz4g+NdQAD7L2ghY/M/pwGofntOHxzAFQ/ljzYB5k2 364R9lb/Y1ZuHuNQopTUXzkNwJ8Xlyf8uR0dcQsKmycf4Du0ic0ZU8m2g8FjmavaxaqSnp Qntsd6jOiB4bc+gov6J4x/Aii2pKsHw= Received: from mail.maildlp.com (unknown [172.19.163.44]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4Wsr5H6p44z1j7Kn; Mon, 26 Aug 2024 20:46:11 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 99F121402E1; Mon, 26 Aug 2024 20:46:20 +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:20 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v15 07/13] mm: page_frag: some minor refactoring before adding new API Date: Mon, 26 Aug 2024 20:40:14 +0800 Message-ID: <20240826124021.2635705-8-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-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 5959B4000D X-Stat-Signature: sibbo76mzk9wdu15tfhbpgjfttbxecja X-Rspam-User: X-HE-Tag: 1724676382-128329 X-HE-Meta: U2FsdGVkX19tVvX6MejNbDNQ13nwTT+IXh9cIxIC40qhFSxkDDL9iTEc9clcfXXSg6HcQL02eDTsP9YHBl50a0ld0qWcQqzDzddldnrzYbcPbq9LXXSenZ3f827gSl3ZgNXuOkzbO5thH3viF1Mrix0iP5RJstsZoWB8izHQjQhc2shdLUkxA1y2V92vx+5XJf14AkciYHWdB92wWUkM7obDsR5naKNorwKxQRMs8SNgWQ1pYdFidpJpOLA1buTJdkgPtBxBT2DD1gO7zx8Roa/+AvOaOVTOpcvq1AKV1xqFhLlTohGL4DT2dn93yQQbJVjE6/esy0sKPxiYI6eRvcBwwNrg9IoSIj6bNIaHy8JpcnNVaYoyiNwi53RQMLr1BGJFGyZy1QbCTOcRT5ulf8zyJ2wveaIAt/K/rspxUStR+0XL5va90r2GsAwexw+TQ9FBeZL0fbu/RvWAxrwdw9AeLET+NSFiaeIDWv9eCoNhm8jjxe01o5pE6d4dM88rEU/BOuYzLUkp7ksvQAHWIibaa++6u32blMdPHSzP8RcuCK6ARqKbP9e7WnzUsrZSvEIyV6Phn7O57/NJUfk24Ywd+Tawy2M4QkCzMkMRz7+0RfTfXcxO8cyDb+dftf7Xj8YNtmmQOMKfispDdDFmu6tPtEWEqFfcscPH+7eVz+2tXcB8tAiaefWdnqBySKt8SAN96wa3NEdHN8J08v+gftx7JoxWYDLPEB8H1blnPVAa5G1q2ovLGVsZKuvv/aVwK9ArEZqQ7x8x245V2g6AkwHisgm9YW0xW/rMCAkizgP3HSkfHTxPIgCui+vdvkPsySPxud/ZUotcPXDxJhi8c5EI1a7GaQ4mRON3HPi+/7lOgRAo0oEloAKEPKQYIP4/2pgVlhX0mqYKoISDwNe1mS2GKwsTZsUPIffTKD6//dyJCKxfQESDLSSrqonsajmWEVXNKUaGZ5em29pSPih D4nfC+sw V/h8jOmSTRvPV9OPrZXqbgnBiEQRJPqoR7aDrcm+YZe4HjgYI54gr31alvYmFw0Bvxwq5rEERYdovUXM2X8I36bm4LD8XmQMyznJ72ktOIhI4yc1PlbW1YgoCgHq1h3urfaRAwzr1jm4uTuVo5f9A0ymKd2gUpLr+UehmFT/27QO0/TxmVj4dDWDju14bJgox9I9SzSM2Lk/sCfKXIAz2KWzBz0wX+rT8XqU0wEM/4PrsRo+YL5LCxgyNa6B8nwUiDOQUpaLbMafGPJV8rufoBRSoCRCvYAVtgozr 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: Refactor common codes from __page_frag_alloc_va_align() to __page_frag_cache_prepare() and __page_frag_cache_commit(), so that the new API can make use of them. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 51 +++++++++++++++++++++++++++++++-- mm/page_frag_cache.c | 20 ++++++------- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 372d6ed7e20a..2cc18a525936 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -75,8 +76,54 @@ static inline unsigned int page_frag_cache_page_size(unsigned long encoded_page) void page_frag_cache_drain(struct page_frag_cache *nc); void __page_frag_cache_drain(struct page *page, unsigned int count); -void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, - gfp_t gfp_mask, unsigned int align_mask); +void *__page_frag_cache_prepare(struct page_frag_cache *nc, unsigned int fragsz, + struct page_frag *pfrag, gfp_t gfp_mask, + unsigned int align_mask); + +static inline void __page_frag_cache_commit(struct page_frag_cache *nc, + struct page_frag *pfrag, bool referenced, + unsigned int used_sz) +{ + if (referenced) { + VM_BUG_ON(!nc->pagecnt_bias); + nc->pagecnt_bias--; + } + + VM_BUG_ON(used_sz > pfrag->size); + VM_BUG_ON(pfrag->page != page_frag_encoded_page_ptr(nc->encoded_page)); + + /* nc->offset is not reset when reusing an old page, so do not check for the + * first fragment. + * Committed offset might be bigger than the current offset due to alignment + */ + VM_BUG_ON(pfrag->offset && nc->offset > pfrag->offset); + VM_BUG_ON(pfrag->offset && + pfrag->offset + pfrag->size > page_frag_cache_page_size(nc->encoded_page)); + + pfrag->size = used_sz; + + /* Calculate true size for the fragment due to alignment, nc->offset is not + * reset for the first fragment when reusing an old page. + */ + pfrag->size += pfrag->offset ? (pfrag->offset - nc->offset) : 0; + + nc->offset = pfrag->offset + used_sz; +} + +static inline void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, + gfp_t gfp_mask, unsigned int align_mask) +{ + struct page_frag page_frag; + void *va; + + va = __page_frag_cache_prepare(nc, fragsz, &page_frag, gfp_mask, align_mask); + if (unlikely(!va)) + return NULL; + + __page_frag_cache_commit(nc, &page_frag, true, fragsz); + + return va; +} static inline void *page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 228cff9a4cdb..bba59c87d478 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -67,16 +67,14 @@ void __page_frag_cache_drain(struct page *page, unsigned int count) } EXPORT_SYMBOL(__page_frag_cache_drain); -void *__page_frag_alloc_align(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask, - unsigned int align_mask) +void *__page_frag_cache_prepare(struct page_frag_cache *nc, unsigned int fragsz, + struct page_frag *pfrag, gfp_t gfp_mask, + unsigned int align_mask) { unsigned long encoded_page = nc->encoded_page; unsigned int size, offset; struct page *page; - size = page_frag_cache_page_size(encoded_page); - if (unlikely(!encoded_page)) { refill: page = __page_frag_cache_refill(nc, gfp_mask); @@ -94,6 +92,9 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; nc->offset = 0; + } else { + size = page_frag_cache_page_size(encoded_page); + page = page_frag_encoded_page_ptr(encoded_page); } offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); @@ -111,8 +112,6 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, return NULL; } - page = page_frag_encoded_page_ptr(encoded_page); - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) goto refill; @@ -130,12 +129,13 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, offset = 0; } - nc->pagecnt_bias--; - nc->offset = offset + fragsz; + pfrag->page = page; + pfrag->offset = offset; + pfrag->size = size - offset; return page_frag_encoded_page_address(encoded_page) + offset; } -EXPORT_SYMBOL(__page_frag_alloc_align); +EXPORT_SYMBOL(__page_frag_cache_prepare); /* * Frees a page fragment allocated out of either a compound or order 0 page.