From patchwork Mon Oct 28 11:58:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13853381 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 98F1FD1359E for ; Mon, 28 Oct 2024 12:05:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 269016B00A5; Mon, 28 Oct 2024 08:05:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 216F16B00A6; Mon, 28 Oct 2024 08:05:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 091AF6B00A7; Mon, 28 Oct 2024 08:05:43 -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 E24F66B00A5 for ; Mon, 28 Oct 2024 08:05:42 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9C276AB807 for ; Mon, 28 Oct 2024 12:04:57 +0000 (UTC) X-FDA: 82722880590.03.6D30752 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf28.hostedemail.com (Postfix) with ESMTP id 4459AC002A for ; Mon, 28 Oct 2024 12:05:15 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; spf=pass (imf28.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 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=1730116930; 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=3xiqcNUxqGifLj5ro6FSlzRyYoA7Pm6KCQOaDJzaKxk=; b=fO99flNCx/wtLJ2ZRlwFagW08KQLVMkv3zDoZ4hHGppRGHlQkQ3pfDcP44kWhPF85UoKtt 2vSCXhiN21yRgAQtiEjZYhF6ckPLHu3VF3m8qslrpmgUX74mi5djbDYAh37TRMXS/ZJq97 H4smcpxo/Ndjv0tVleUHM07DkdmCm7k= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; spf=pass (imf28.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 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=1730116930; a=rsa-sha256; cv=none; b=gm6TkFFomVbn8UM3DhqW0gWsfDj1AVlVbZAPfSKGV04I5JSoIaaFmUPiA4Fg0JwkDU0fCr EQCXq0elVJYrV8DvDeBfBgGXnrUY70+fQZc3kqbXAu1S8ZL7XT87kLjvGL//3C+Qn7Skoz 5MKAGDn/YsIkDfhCfYg18rvtrpTVnZY= Received: from mail.maildlp.com (unknown [172.19.88.194]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4XcX8d5bshzpWPn; Mon, 28 Oct 2024 20:03:13 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id B9F5A1402CB; Mon, 28 Oct 2024 20:05:09 +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:09 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Linux-MM Subject: [PATCH RFC 01/10] mm: page_frag: some minor refactoring before adding new API Date: Mon, 28 Oct 2024 19:58:41 +0800 Message-ID: <20241028115850.3409893-2-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-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 4459AC002A X-Stat-Signature: 6jok5ynpigy8z9b86kn6y4bibu9o51dz X-Rspam-User: X-HE-Tag: 1730117115-407703 X-HE-Meta: U2FsdGVkX1/mHVcPi8QehLvLp8KWHaMwPDjv5TJgNq3P9Q6mzwWs4SCY4jZfqzaCIk/fz8FchFmNrgDif2/2ElfES0tXQW47JfP2JSqQaNFaznEIh63bHwLB3Fv5cK3k1nP7/iVX34GMYjanWsnThP5KriMCfQaCFAMMcSXGoAKe1AYxQkPy+j9KBI8y8Us32uBUX9H1huNJhHlGzX2IL2GAaMSlSDG0rtqzqFkwN2QR5K2svWoyykGMEQfvwXwo93qbDnzRNWEsvR7iJVZdN/0DDsERSThpsDtrxaKt9TBvEXjsb6PYfKJ+ndrSfvxVDkjzTB1CWNRUXL6YK+FGXYW7h+hVw+ukdM+VQOlIkuS2FCvh+uYO8+LrJ3Hlj536PWTVp7pjN+kLmfEly8h86VWPpGTkn4/aR+T2zJa48JHctpiNu8J3Z1NkfmNjTRASYyZ5c4mG3GxO8rpWyA4Zh1b9lbak2NGlSe5IaBXkaVcfF8QG3uz8Fr1sR9wIrAdOrBL+MMxqlIQN+gAnlf0jTowoU9phEFOKt74dtRgGtoXgv/tAFP41Yty2gx/erB+ChNVZkdAZI9dE8vWOTKfPOStg6mqcy+PgE9Ne4iBO79ee39WPLrbPwrZU44Hb7ecciBkph2j6BC04eKrQ/PrdvLLaCZPM5MIIMIQ06g1Bbol9qtJvkBO7ihrx+Jr1a/b9zIsCGOcCGcT1jg/wLVBAjPbmRA7uwydc03duSD3JfxgLg5NvfD58bc22tTlgCfL4CJwws7oL9dL9VE0wq5/wvr8lqDPUM7ylOJZNJ0oIcAjfbSG7pAScIYUP4Px0grLv5MPogENcs1ACdq0l3Tn/OSynHhuzZhXt/R34CeTbguO1kTSS/FjxmqNrn22S7UDUUqB1sjPc0OmBtGD47dhc/+kaby5oaoVXZqAduMqap53p4CFTss3mXwRyVrO4isNcQw8FgBNxk9mrpNS0xHv Jtnt3ywi YaBIeOX7hwvfjP7FaJDC1oH0kd8KYhi3FBU8vQbF8XDmzbcBXP/d2jMBhKMhq53NWVuteGHYh7woARS6embDVXDgFc8A4W53z7HuZ2L2LmUb+Tf+UcpDl1vnqAtmr9SViyWnKeFhltaXVpsUqrAyhve/Mixka7ys8VT0gqPRH+43QfsoNdWR70kvC/L/f8sGlMxpVNzmRjCk0h6TciJMR/09HUlVXmHqWRnYQCgrVzmDMIlanBsfL4H3OVxNUtdzaMubh6GckXlQT8aXC3S3KK+9HArv/bwwM2fKZ8BSwJZUqFooFCVO/2SQ1+WxPawjNfI65LiIvOCLrDQnC1MfkEF1ja4Or98oneO2V0deYBoP/WJQE3Dm6eS+3mBQfhF4tAj62jwGAqFcwGt7dLk7Z5HZ+5g== 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 CC: Andrew Morton CC: Linux-MM Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 34 ++++++++++++++++++++++++++-- mm/page_frag_cache.c | 40 ++++++++++++++++++++++++++------- 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 41a91df82631..5ae97f93a0a1 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -39,8 +40,37 @@ static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc) 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); +unsigned int __page_frag_cache_commit_noref(struct page_frag_cache *nc, + struct page_frag *pfrag, + unsigned int used_sz); + +static inline unsigned int __page_frag_cache_commit(struct page_frag_cache *nc, + struct page_frag *pfrag, + unsigned int used_sz) +{ + VM_BUG_ON(!nc->pagecnt_bias); + nc->pagecnt_bias--; + + return __page_frag_cache_commit_noref(nc, pfrag, 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 (likely(va)) + __page_frag_cache_commit(nc, &page_frag, 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 3f7a203d35c6..f55d34cf7d43 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -90,9 +90,31 @@ 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) +unsigned int __page_frag_cache_commit_noref(struct page_frag_cache *nc, + struct page_frag *pfrag, + unsigned int used_sz) +{ + unsigned int orig_offset; + + VM_BUG_ON(used_sz > pfrag->size); + VM_BUG_ON(pfrag->page != encoded_page_decode_page(nc->encoded_page)); + VM_BUG_ON(pfrag->offset + pfrag->size > + (PAGE_SIZE << encoded_page_decode_order(nc->encoded_page))); + + /* pfrag->offset might be bigger than the nc->offset due to alignment */ + VM_BUG_ON(nc->offset > pfrag->offset); + + orig_offset = nc->offset; + nc->offset = pfrag->offset + used_sz; + + /* Return true size back to caller considering the offset alignment */ + return nc->offset - orig_offset; +} +EXPORT_SYMBOL(__page_frag_cache_commit_noref); + +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; @@ -114,6 +136,8 @@ 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 { + page = encoded_page_decode_page(encoded_page); } size = PAGE_SIZE << encoded_page_decode_order(encoded_page); @@ -132,8 +156,6 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, return NULL; } - page = encoded_page_decode_page(encoded_page); - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) goto refill; @@ -148,15 +170,17 @@ 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; offset = 0; } - nc->pagecnt_bias--; - nc->offset = offset + fragsz; + pfrag->page = page; + pfrag->offset = offset; + pfrag->size = size - offset; return encoded_page_decode_virt(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.