From patchwork Tue May 28 12:56:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13676637 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 C0D8BC25B78 for ; Tue, 28 May 2024 12:59:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 199246B0083; Tue, 28 May 2024 08:59:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 14A3E6B00A3; Tue, 28 May 2024 08:59:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EB6176B00A4; Tue, 28 May 2024 08:59:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C64DC6B0083 for ; Tue, 28 May 2024 08:59:17 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6D62C1602CE for ; Tue, 28 May 2024 12:59:17 +0000 (UTC) X-FDA: 82167810354.10.CF532D8 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf04.hostedemail.com (Postfix) with ESMTP id 06CCB4000A for ; Tue, 28 May 2024 12:59:14 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf04.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=1716901155; 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=jOGHzlkKOh/jgXnkWvfVWos5YuAf8lv7NEqEzKa8HW8=; b=8eAe03uC3jXA6c7T5oj6xBrJdl+KLcY6JMtZFnKJkAttBA5c/tgP8ZeSR5GdYRgCUEUebu P5GWkkn1i06MNwNZQ5F6NZ28BDbInXzerkiPI0+Mjh24I5lGdM7KF384gAAJMEP6VPn/us zqgHf6QBF4/h/+Bgy3P7G95OnoxjQA4= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf04.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=1716901155; a=rsa-sha256; cv=none; b=bsVsc+ClHYOLgcgtjuyeLR49XzxxcuVgH2g4E3X16iKVKB+Ue+PHQ1MEvuJRxaiMbqkOBm kn3cnrPdEyv81aY+QyfLGWKXedO6v9KmuXm+rrKnbNLxkZq2Et2e6QjosKkM2+KGr71h51 0ukX4uRcHkhUZFLzyxukjICf95bgdIo= Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VpXcJ16FszckSY; Tue, 28 May 2024 20:57:52 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 946CB180085; Tue, 28 May 2024 20:59:12 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Tue, 28 May 2024 20:59:12 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v5 10/13] mm: page_frag: introduce prepare/probe/commit API Date: Tue, 28 May 2024 20:56:00 +0800 Message-ID: <20240528125604.63048-11-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240528125604.63048-1-linyunsheng@huawei.com> References: <20240528125604.63048-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 06CCB4000A X-Stat-Signature: 9j89qokjzegk7m3keff1k1h76jy1a7pm X-HE-Tag: 1716901154-988655 X-HE-Meta: U2FsdGVkX18RAsJDhI4Av0kNpmJt3sAwJjOTLloFPyLUZzwg9ACrhlFNLleafs5RJZWloZA+eDOkMd5P4CBPkmFTeNR6JIbPXcdnPFWk5/vuuSavWEbaEAqCEhWFjHmLcqhkjg/CyXnqId9pGpuyMn1BMBflohOxyDLSoX52INH8urv3IP7pBCusP2AQlKQltrzrphu238BwPrRymSAvcHMSepBZASpPnzNhNsXwdPkhOxjh6X01VJQPgN41eNoL/u6sjwU5tEckwTJqeSFlYVhDYZBI58v61g1jxEp7PHulggz39YlFNeso3uWndDr+9wVWfGGumCig8Lh40Bqmj+PNg5jhnJ2nvCtpedmumyZYAUG5LzIwG3Nrv6pvq4u5nQttPB1PzCMF8P95XQAFnsLVApf+Kulj2phqopAP8BV3T+Ze/uJ66V2ltF8IdDOObU0+X10ByIMWABVSLG37PWOIIviT+gQ1xX+LIfjIShmChWVHs24/VhdwuFtXCEdZ2fRYe+HdMKGEy+dug9IqeLUMsOKjAELVfYNNw3CgB1BlALMvsito6m8hnnntXnOe6qEKXEPgnUMSqA65NEZnQ2s9q8mcghJD4Nr8jVwyRK4UB5RsoyerMjJcY+woIj494yNguJN3Qs1VR7k6IG1elyHFgTh5hj5tUojY1tOrcg8NdDcQjML1qj1jDPIf65wqWVu0neeEUi6oqU0O4aGMCf9I9nSvcViqBscsX1a/jE40gVwN8bA4BwqXjQcaSh+EsQsSxdrJZr3M8EV5IgbzXKCewvsJ4UTUxYl+55PHFnf/A1h2Ah2yj/OYjghRqX/G5oQC4dCsgPzrFhWXgdVIbh3uuisWI4YStMHqmVsiPrbp2oETACVeHtfl76wMOeXqcX6MSIXT7PkkuEZJJNBDMgbqr8KREhgMh7KQZt6inHMNLwQhEZ468C9aEswRkELq8HgTZgZcy/i9UZloMo6 HjC9iw5K dDNjE7h81U9YzYDNdDzPfb5GUkLSogyrUiors8l+lj1ewjeHaHJjf7vUbmit81oH+82y0cENxIosC7kv4brCtyADQPGbSGRTx44FIwi8h//BvYepbUeD+OSoiQHHb9e1FYI45vP7USDrTpDaH19Fc7uMAO/vvBMp3DJgmIdMS7AbUBf+Ns15Y5zltvp09UbFIlW65E+FYqnALUSw32LL8hmtZVLUBOMPO7GNNmvyB/1wXmWEkd0OSdzlNwM/bt+JviT1hNAlctWVPOn3dLpjQntx5+72lp6oo9v9N 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: There are many use cases that need minimum memory in order for forward progress, but more performant if more memory is available or need to probe the cache info to use any memory available for frag caoleasing reason. Currently skb_page_frag_refill() API is used to solve the above use cases, but caller needs to know about the internal detail and access the data field of 'struct page_frag' to meet the requirement of the above use cases and its implementation is similar to the one in mm subsystem. To unify those two page_frag implementations, introduce a prepare API to ensure minimum memory is satisfied and return how much the actual memory is available to the caller and a probe API to report the current available memory to caller without doing cache refilling. The caller needs to either call the commit API to report how much memory it actually uses, or not do so if deciding to not use any memory. As next patch is about to replace 'struct page_frag' with 'struct page_frag_cache' in linux/sched.h, which is included by the asm-offsets.s, using the virt_to_page() in the inline helper of page_frag_cache.h cause a "'vmemmap' undeclared" compiling error for asm-offsets.s, use a macro for probe API to avoid that compiling error. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 81 ++++++++++++++++++ mm/page_frag_cache.c | 145 ++++++++++++++++++++++++++++++++ 2 files changed, 226 insertions(+) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 5228e3025ba1..365302787012 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -93,6 +93,9 @@ static inline unsigned int __page_frag_cache_page_offset(struct encoded_va *enco void page_frag_cache_drain(struct page_frag_cache *nc); void __page_frag_cache_drain(struct page *page, unsigned int count); +struct page *page_frag_alloc_pg(struct page_frag_cache *nc, + unsigned int *offset, unsigned int fragsz, + gfp_t gfp); void *__page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask); @@ -105,12 +108,90 @@ static inline void *page_frag_alloc_va_align(struct page_frag_cache *nc, return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, -align); } +static inline unsigned int page_frag_cache_page_offset(const struct page_frag_cache *nc) +{ + return __page_frag_cache_page_offset(nc->encoded_va, nc->remaining); +} + static inline void *page_frag_alloc_va(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask) { return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, ~0u); } +void *page_frag_alloc_va_prepare(struct page_frag_cache *nc, unsigned int *fragsz, + gfp_t gfp); + +static inline void *page_frag_alloc_va_prepare_align(struct page_frag_cache *nc, + unsigned int *fragsz, + gfp_t gfp, + unsigned int align) +{ + WARN_ON_ONCE(!is_power_of_2(align) || align > PAGE_SIZE); + nc->remaining = nc->remaining & -align; + return page_frag_alloc_va_prepare(nc, fragsz, gfp); +} + +struct page *page_frag_alloc_pg_prepare(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *fragsz, gfp_t gfp); + +struct page *page_frag_alloc_prepare(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *fragsz, + void **va, gfp_t gfp); + +static inline struct encoded_va *__page_frag_alloc_probe(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *fragsz, + void **va) +{ + struct encoded_va *encoded_va; + + *fragsz = nc->remaining; + encoded_va = nc->encoded_va; + *offset = __page_frag_cache_page_offset(encoded_va, *fragsz); + *va = encoded_page_address(encoded_va) + *offset; + + return encoded_va; +} + +#define page_frag_alloc_probe(nc, offset, fragsz, va) \ +({ \ + struct page *__page = NULL; \ + \ + VM_BUG_ON(!*(fragsz)); \ + if (likely((nc)->remaining >= *(fragsz))) \ + __page = virt_to_page(__page_frag_alloc_probe(nc, \ + offset, \ + fragsz, \ + va)); \ + \ + __page; \ +}) + +static inline void page_frag_alloc_commit(struct page_frag_cache *nc, + unsigned int fragsz) +{ + VM_BUG_ON(fragsz > nc->remaining || !nc->pagecnt_bias); + nc->pagecnt_bias--; + nc->remaining -= fragsz; +} + +static inline void page_frag_alloc_commit_noref(struct page_frag_cache *nc, + unsigned int fragsz) +{ + VM_BUG_ON(fragsz > nc->remaining); + nc->remaining -= fragsz; +} + +static inline void page_frag_alloc_abort(struct page_frag_cache *nc, + unsigned int fragsz) +{ + nc->pagecnt_bias++; + nc->remaining += fragsz; +} + void page_frag_free_va(void *addr); #endif diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 7842f0f2f672..cde7f16e589f 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -60,6 +60,151 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, return page; } +static struct page *page_frag_cache_refill(struct page_frag_cache *nc, + gfp_t gfp_mask) +{ + struct encoded_va *encoded_va = nc->encoded_va; + struct page *page; + + if (unlikely(!encoded_va)) + return __page_frag_cache_refill(nc, gfp_mask); + + page = virt_to_page(encoded_va); + + if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) + return __page_frag_cache_refill(nc, gfp_mask); + + if (unlikely(encoded_page_pfmemalloc(encoded_va))) { + VM_BUG_ON(compound_order(page) != + encoded_page_order(encoded_va)); + free_unref_page(page, encoded_page_order(encoded_va)); + return __page_frag_cache_refill(nc, gfp_mask); + } + + /* OK, page count is 0, we can safely set it */ + set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); + + /* reset page count bias and remaining of new frag */ + nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; + nc->remaining = page_frag_cache_page_size(encoded_va); + + return page; +} + +void *page_frag_alloc_va_prepare(struct page_frag_cache *nc, + unsigned int *fragsz, gfp_t gfp) +{ + struct encoded_va *encoded_va; + unsigned int remaining; + + remaining = nc->remaining; + if (unlikely(*fragsz > remaining)) { + if (WARN_ON_ONCE(*fragsz > PAGE_SIZE) || + unlikely(!page_frag_cache_refill(nc, gfp))) + return NULL; + + remaining = nc->remaining; + } + + encoded_va = nc->encoded_va; + *fragsz = remaining; + return encoded_page_address(encoded_va) + + __page_frag_cache_page_offset(encoded_va, remaining); +} +EXPORT_SYMBOL(page_frag_alloc_va_prepare); + +struct page *page_frag_alloc_pg_prepare(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *fragsz, gfp_t gfp) +{ + struct encoded_va *encoded_va; + unsigned int remaining; + struct page *page; + + remaining = nc->remaining; + if (unlikely(*fragsz > remaining)) { + if (WARN_ON_ONCE(*fragsz > PAGE_SIZE)) { + *fragsz = 0; + return NULL; + } + + page = page_frag_cache_refill(nc, gfp); + remaining = nc->remaining; + encoded_va = nc->encoded_va; + } else { + encoded_va = nc->encoded_va; + page = virt_to_page(encoded_va); + } + + *offset = __page_frag_cache_page_offset(encoded_va, remaining); + *fragsz = remaining; + + return page; +} +EXPORT_SYMBOL(page_frag_alloc_pg_prepare); + +struct page *page_frag_alloc_prepare(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *fragsz, + void **va, gfp_t gfp) +{ + struct encoded_va *encoded_va; + unsigned int remaining; + struct page *page; + + remaining = nc->remaining; + if (unlikely(*fragsz > remaining)) { + if (WARN_ON_ONCE(*fragsz > PAGE_SIZE)) { + *fragsz = 0; + return NULL; + } + + page = page_frag_cache_refill(nc, gfp); + remaining = nc->remaining; + encoded_va = nc->encoded_va; + } else { + encoded_va = nc->encoded_va; + page = virt_to_page(encoded_va); + } + + *offset = __page_frag_cache_page_offset(encoded_va, remaining); + *fragsz = remaining; + *va = encoded_page_address(encoded_va) + *offset; + + return page; +} +EXPORT_SYMBOL(page_frag_alloc_prepare); + +struct page *page_frag_alloc_pg(struct page_frag_cache *nc, + unsigned int *offset, unsigned int fragsz, + gfp_t gfp) +{ + struct page *page; + + if (unlikely(fragsz > nc->remaining)) { + if (WARN_ON_ONCE(fragsz > PAGE_SIZE)) + return NULL; + + page = page_frag_cache_refill(nc, gfp); + if (unlikely(!page)) + return NULL; + + *offset = 0; + } else { + struct encoded_va *encoded_va = nc->encoded_va; + + page = virt_to_page(encoded_va); + *offset = __page_frag_cache_page_offset(encoded_va, + nc->remaining); + } + + nc->remaining -= fragsz; + nc->pagecnt_bias--; + + return page; +} +EXPORT_SYMBOL(page_frag_alloc_pg); + void page_frag_cache_drain(struct page_frag_cache *nc) { if (!nc->encoded_va)