From patchwork Wed Jun 5 13:32:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13686901 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 26DF0C25B76 for ; Wed, 5 Jun 2024 13:36:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9D4246B0083; Wed, 5 Jun 2024 09:36:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9832C6B0085; Wed, 5 Jun 2024 09:36:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 84BC26B0089; Wed, 5 Jun 2024 09:36:10 -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 673D76B0083 for ; Wed, 5 Jun 2024 09:36:10 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id F02ABC1405 for ; Wed, 5 Jun 2024 13:36:09 +0000 (UTC) X-FDA: 82196933658.02.0C80E31 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf27.hostedemail.com (Postfix) with ESMTP id F0BFB40018 for ; Wed, 5 Jun 2024 13:36:06 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.255 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=1717594568; 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=k/m7UycD2NerOb/mCoVTW7tG1UHiQV6Li+30iPDMQR8=; b=ohd8ZLfMlkFkRSHQO+pZyCAHCcrkpgC35afFzThl7Sg/eViRt88g2qAOYQ9BoSOM+mzCNA fcSQiwhbTi4DvTQGrXmt7wPTv5MqeBzVxE2Z5MO5PLx9uSRcfwTu9rFmTaofIimYCBsHVd tuwqXExsqEekE4eosUHDJ0Sf4EpcMLE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717594568; a=rsa-sha256; cv=none; b=5pTSoukqXnfzaQEmeJ6dzDuh2zN1ySK7kdVc2/yAwpa9eVXHJDrNq/p28KWR+MpBqKSZHj 7AgktP9wtU6izmtIeYH+01nSd0PB9vfneq5moHM2AvQrrcnJdVJjppC6ddc4KQcuCSRtfC /32DVgTq5hQaI0P0DQ0x792pTHFdf00= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4VvT044w2Pz1S996; Wed, 5 Jun 2024 21:32:04 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 4CCAE18007A; Wed, 5 Jun 2024 21:36:01 +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.39; Wed, 5 Jun 2024 21:36:00 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v6 01/15] mm: page_frag: add a test module for page_frag Date: Wed, 5 Jun 2024 21:32:51 +0800 Message-ID: <20240605133306.11272-2-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240605133306.11272-1-linyunsheng@huawei.com> References: <20240605133306.11272-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: F0BFB40018 X-Rspam-User: X-Stat-Signature: uabik4xbfzxy7rxjoa8p7g9y51tob7sk X-HE-Tag: 1717594566-958059 X-HE-Meta: U2FsdGVkX19Ee7QG4/iF35+Qq4EAbIWbtOy6r3sI0YYMOZIfNUTMUHyxwdLaJrKoTXCcplMh0MuS3Q6JMW6ETvjbgfefXAg1MtrOUYZvZkA6jjGdqsPkAfVhOF0U6hCSe2HBlOo62CpKwe6Zxzw6PyB+V5y0kWg+pX0V0hjGIc+1vqWsdHlnIuqdbj7garNZadVurFgT9GHFL2JlDhaOyJscyp+iZIRHyBW2QDCm9Uwg2PeCAOqlT+TKEe2Q6gybvFotdovcX60ZBieNpAKRQ3Be/rt8Z+Oh2CM9yYJz17x2ljjMaDsNuC3R/akTjiCItoluYGn8zYfVLAN99/KA0oxeY4KG/CAySu6ikLMh7jE8du4ScfK8AFr/saVDMafO8xPQyFOim65gpRgELFDrNBk0PzeLUxQ5f+Xp9aSGY+2vvAthOVKEZRReWSVL5BzSIF06PDnOxs+MJdvIQPDOZxTxbfOxziusf/XBW36JYTbxvXHZbwGnZ30F/K83IZvK0MTDIs/vUlRZtnRhTWDIz0AVkQTsgAy2C06v0d1qhv53YCDdhAulGGwYZtCxDAqia0MBGI+rHDyXvFvDrBHrgCkjIZCApm0CAaYf5zpMJ0C73KUjfGS+HFFYbPGgqWN73ga9Ofg678VBTt5/MD/+8kg6Q6w1S4rn8sGT9ELaWGyt8kFpGI/v2KXd7Vjp2+B+149oKKVKZ5WO+f+gkHUkSQsXzp4KcWIPebi2LMZWMdlxLaFMJldE3V4yQPI4AS1ugLeGCt6R+lrd6If+42fab8qAyquwj3cYlXRLQ9qY8xjHhALmvz9GT8ocRtJgcOa5/R5PKOmSmum9/b/uwsCkiatml3hlUGgCZ8PSdUyfKlL2+JtTwIDL0hZt1PknpnldGu2TWdOARXxPBlXAnaO5LI0FpuC2/XhQ2QezoOSPtjqQxFDycRAvkWbIynNzALVMi4ewEY9f4iADqP6DTeo XtnLpLCH fcS3vZ6yLq7z7kJYZA3nlL4Xh/zAGQe16JfgGXe0uDXmEqDF1mvbphdrqdXNd8HQF+Ugls3TNwj2M1mN9c7QuFa8WT9KniRCreIWVXUHEGoytVqNIIQFW4P+HB5IZ7wf8CLPeVPntHi8VjDTsAgmmpqaEE+ooFQBauyP83aUjRWtgzw/L+NFvvKP4Ny1eBIwaO2t7GOnSvW8xPJkksLvg2dYlCYfuWmdQ2XBAkWMZretY9Bnf9g/8rahM0exiM+8z5ul9Oi1MUmFRvude8VeoBVQDILk89j71MOnNhI0X5Z+/AGLnOSiYiWWUMg== 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: Basing on the lib/objpool.c, change it to something like a ptrpool, so that we can utilize that to test the correctness and performance of the page_frag. The testing is done by ensuring that the fragment allocated from a frag_frag_cache instance is pushed into a ptrpool instance in a kthread binded to a specified cpu, and a kthread binded to a specified cpu will pop the fragment from the ptrpool and free the fragment. We may refactor out the common part between objpool and ptrpool if this ptrpool thing turns out to be helpful for other place. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- mm/Kconfig.debug | 8 + mm/Makefile | 1 + mm/page_frag_test.c | 386 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 395 insertions(+) create mode 100644 mm/page_frag_test.c diff --git a/mm/Kconfig.debug b/mm/Kconfig.debug index afc72fde0f03..1ebcd45f47d4 100644 --- a/mm/Kconfig.debug +++ b/mm/Kconfig.debug @@ -142,6 +142,14 @@ config DEBUG_PAGE_REF kernel code. However the runtime performance overhead is virtually nil until the tracepoints are actually enabled. +config DEBUG_PAGE_FRAG_TEST + tristate "Test module for page_frag" + default n + depends on m && DEBUG_KERNEL + help + This builds the "page_frag_test" module that is used to test the + correctness and performance of page_frag's implementation. + config DEBUG_RODATA_TEST bool "Testcase for the marking rodata read-only" depends on STRICT_KERNEL_RWX diff --git a/mm/Makefile b/mm/Makefile index 85f29ef7bedd..d2bb17d06b88 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -102,6 +102,7 @@ obj-$(CONFIG_MEMORY_FAILURE) += memory-failure.o obj-$(CONFIG_HWPOISON_INJECT) += hwpoison-inject.o obj-$(CONFIG_DEBUG_KMEMLEAK) += kmemleak.o obj-$(CONFIG_DEBUG_RODATA_TEST) += rodata_test.o +obj-$(CONFIG_DEBUG_PAGE_FRAG_TEST) += page_frag_test.o obj-$(CONFIG_DEBUG_VM_PGTABLE) += debug_vm_pgtable.o obj-$(CONFIG_PAGE_OWNER) += page_owner.o obj-$(CONFIG_MEMORY_ISOLATION) += page_isolation.o diff --git a/mm/page_frag_test.c b/mm/page_frag_test.c new file mode 100644 index 000000000000..a3605f1a8b2b --- /dev/null +++ b/mm/page_frag_test.c @@ -0,0 +1,386 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Test module for page_frag cache + * + * Copyright: linyunsheng@huawei.com + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define OBJPOOL_NR_OBJECT_MAX BIT(24) + +struct objpool_slot { + u32 head; + u32 tail; + u32 last; + u32 mask; + void *entries[]; +} __packed; + +struct objpool_head { + int nr_cpus; + int capacity; + struct objpool_slot **cpu_slots; +}; + +/* initialize percpu objpool_slot */ +static void objpool_init_percpu_slot(struct objpool_head *pool, + struct objpool_slot *slot) +{ + /* initialize elements of percpu objpool_slot */ + slot->mask = pool->capacity - 1; +} + +/* allocate and initialize percpu slots */ +static int objpool_init_percpu_slots(struct objpool_head *pool, + int nr_objs, gfp_t gfp) +{ + int i; + + for (i = 0; i < pool->nr_cpus; i++) { + struct objpool_slot *slot; + int size; + + /* skip the cpu node which could never be present */ + if (!cpu_possible(i)) + continue; + + size = struct_size(slot, entries, pool->capacity); + + /* + * here we allocate percpu-slot & objs together in a single + * allocation to make it more compact, taking advantage of + * warm caches and TLB hits. in default vmalloc is used to + * reduce the pressure of kernel slab system. as we know, + * minimal size of vmalloc is one page since vmalloc would + * always align the requested size to page size + */ + if (gfp & GFP_ATOMIC) + slot = kmalloc_node(size, gfp, cpu_to_node(i)); + else + slot = __vmalloc_node(size, sizeof(void *), gfp, + cpu_to_node(i), + __builtin_return_address(0)); + if (!slot) + return -ENOMEM; + + memset(slot, 0, size); + pool->cpu_slots[i] = slot; + + objpool_init_percpu_slot(pool, slot); + } + + return 0; +} + +/* cleanup all percpu slots of the object pool */ +static void objpool_fini_percpu_slots(struct objpool_head *pool) +{ + int i; + + if (!pool->cpu_slots) + return; + + for (i = 0; i < pool->nr_cpus; i++) + kvfree(pool->cpu_slots[i]); + kfree(pool->cpu_slots); +} + +/* initialize object pool and pre-allocate objects */ +static int objpool_init(struct objpool_head *pool, int nr_objs, gfp_t gfp) +{ + int rc, capacity, slot_size; + + /* check input parameters */ + if (nr_objs <= 0 || nr_objs > OBJPOOL_NR_OBJECT_MAX) + return -EINVAL; + + /* calculate capacity of percpu objpool_slot */ + capacity = roundup_pow_of_two(nr_objs); + if (!capacity) + return -EINVAL; + + gfp = gfp & ~__GFP_ZERO; + + /* initialize objpool pool */ + memset(pool, 0, sizeof(struct objpool_head)); + pool->nr_cpus = nr_cpu_ids; + pool->capacity = capacity; + slot_size = pool->nr_cpus * sizeof(struct objpool_slot *); + pool->cpu_slots = kzalloc(slot_size, gfp); + if (!pool->cpu_slots) + return -ENOMEM; + + /* initialize per-cpu slots */ + rc = objpool_init_percpu_slots(pool, nr_objs, gfp); + if (rc) + objpool_fini_percpu_slots(pool); + + return rc; +} + +/* adding object to slot, abort if the slot was already full */ +static int objpool_try_add_slot(void *obj, struct objpool_head *pool, int cpu) +{ + struct objpool_slot *slot = pool->cpu_slots[cpu]; + u32 head, tail; + + /* loading tail and head as a local snapshot, tail first */ + tail = READ_ONCE(slot->tail); + + do { + head = READ_ONCE(slot->head); + /* fault caught: something must be wrong */ + if (unlikely(tail - head >= pool->capacity)) + return -ENOSPC; + } while (!try_cmpxchg_acquire(&slot->tail, &tail, tail + 1)); + + /* now the tail position is reserved for the given obj */ + WRITE_ONCE(slot->entries[tail & slot->mask], obj); + /* update sequence to make this obj available for pop() */ + smp_store_release(&slot->last, tail + 1); + + return 0; +} + +/* reclaim an object to object pool */ +static int objpool_push(void *obj, struct objpool_head *pool) +{ + unsigned long flags; + int rc; + + /* disable local irq to avoid preemption & interruption */ + raw_local_irq_save(flags); + rc = objpool_try_add_slot(obj, pool, raw_smp_processor_id()); + raw_local_irq_restore(flags); + + return rc; +} + +/* try to retrieve object from slot */ +static void *objpool_try_get_slot(struct objpool_head *pool, int cpu) +{ + struct objpool_slot *slot = pool->cpu_slots[cpu]; + /* load head snapshot, other cpus may change it */ + u32 head = smp_load_acquire(&slot->head); + + while (head != READ_ONCE(slot->last)) { + void *obj; + + /* + * data visibility of 'last' and 'head' could be out of + * order since memory updating of 'last' and 'head' are + * performed in push() and pop() independently + * + * before any retrieving attempts, pop() must guarantee + * 'last' is behind 'head', that is to say, there must + * be available objects in slot, which could be ensured + * by condition 'last != head && last - head <= nr_objs' + * that is equivalent to 'last - head - 1 < nr_objs' as + * 'last' and 'head' are both unsigned int32 + */ + if (READ_ONCE(slot->last) - head - 1 >= pool->capacity) { + head = READ_ONCE(slot->head); + continue; + } + + /* obj must be retrieved before moving forward head */ + obj = READ_ONCE(slot->entries[head & slot->mask]); + + /* move head forward to mark it's consumption */ + if (try_cmpxchg_release(&slot->head, &head, head + 1)) + return obj; + } + + return NULL; +} + +/* allocate an object from object pool */ +static void *objpool_pop(struct objpool_head *pool) +{ + void *obj = NULL; + unsigned long flags; + int i, cpu; + + /* disable local irq to avoid preemption & interruption */ + raw_local_irq_save(flags); + + cpu = raw_smp_processor_id(); + for (i = 0; i < num_possible_cpus(); i++) { + obj = objpool_try_get_slot(pool, cpu); + if (obj) + break; + cpu = cpumask_next_wrap(cpu, cpu_possible_mask, -1, 1); + } + raw_local_irq_restore(flags); + + return obj; +} + +/* release whole objpool forcely */ +static void objpool_free(struct objpool_head *pool) +{ + if (!pool->cpu_slots) + return; + + /* release percpu slots */ + objpool_fini_percpu_slots(pool); +} + +static struct objpool_head ptr_pool; +static int nr_objs = 512; +static atomic_t nthreads; +static struct completion wait; +static struct page_frag_cache test_frag; + +static int nr_test = 5120000; +module_param(nr_test, int, 0600); +MODULE_PARM_DESC(nr_test, "number of iterations to test"); + +static bool test_align; +module_param(test_align, bool, 0600); +MODULE_PARM_DESC(test_align, "use align API for testing"); + +static int test_alloc_len = 2048; +module_param(test_alloc_len, int, 0600); +MODULE_PARM_DESC(test_alloc_len, "alloc len for testing"); + +static int test_push_cpu; +module_param(test_push_cpu, int, 0600); +MODULE_PARM_DESC(test_push_cpu, "test cpu for pushing fragment"); + +static int test_pop_cpu; +module_param(test_pop_cpu, int, 0600); +MODULE_PARM_DESC(test_pop_cpu, "test cpu for popping fragment"); + +static int page_frag_pop_thread(void *arg) +{ + struct objpool_head *pool = arg; + int nr = nr_test; + + pr_info("page_frag pop test thread begins on cpu %d\n", + smp_processor_id()); + + while (nr > 0) { + void *obj = objpool_pop(pool); + + if (obj) { + nr--; + page_frag_free(obj); + } else { + cond_resched(); + } + } + + if (atomic_dec_and_test(&nthreads)) + complete(&wait); + + pr_info("page_frag pop test thread exits on cpu %d\n", + smp_processor_id()); + + return 0; +} + +static int page_frag_push_thread(void *arg) +{ + struct objpool_head *pool = arg; + int nr = nr_test; + + pr_info("page_frag push test thread begins on cpu %d\n", + smp_processor_id()); + + while (nr > 0) { + void *va; + int ret; + + if (test_align) + va = page_frag_alloc_align(&test_frag, test_alloc_len, + GFP_KERNEL, SMP_CACHE_BYTES); + else + va = page_frag_alloc(&test_frag, test_alloc_len, GFP_KERNEL); + + if (!va) + continue; + + ret = objpool_push(va, pool); + if (ret) { + page_frag_free(va); + cond_resched(); + } else { + nr--; + } + } + + pr_info("page_frag push test thread exits on cpu %d\n", + smp_processor_id()); + + if (atomic_dec_and_test(&nthreads)) + complete(&wait); + + return 0; +} + +static int __init page_frag_test_init(void) +{ + struct task_struct *tsk_push, *tsk_pop; + ktime_t start; + u64 duration; + int ret; + + test_frag.va = NULL; + atomic_set(&nthreads, 2); + init_completion(&wait); + + ret = objpool_init(&ptr_pool, nr_objs, GFP_KERNEL); + if (ret) + return ret; + + tsk_push = kthread_create_on_cpu(page_frag_push_thread, &ptr_pool, + test_push_cpu, "page_frag_push"); + if (IS_ERR(tsk_push)) + return PTR_ERR(tsk_push); + + tsk_pop = kthread_create_on_cpu(page_frag_pop_thread, &ptr_pool, + test_pop_cpu, "page_frag_pop"); + if (IS_ERR(tsk_pop)) { + kthread_stop(tsk_push); + return PTR_ERR(tsk_pop); + } + + start = ktime_get(); + wake_up_process(tsk_push); + wake_up_process(tsk_pop); + + pr_info("waiting for test to complete\n"); + wait_for_completion(&wait); + + duration = (u64)ktime_us_delta(ktime_get(), start); + pr_info("%d of iterations for %s testing took: %lluus\n", nr_test, + test_align ? "aligned" : "non-aligned", duration); + + objpool_free(&ptr_pool); + page_frag_cache_drain(&test_frag); + + return -EAGAIN; +} + +static void __exit page_frag_test_exit(void) +{ +} + +module_init(page_frag_test_init); +module_exit(page_frag_test_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Yunsheng Lin "); +MODULE_DESCRIPTION("Test module for page_frag"); From patchwork Wed Jun 5 13:32:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13686900 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 3C4BBC27C5E for ; Wed, 5 Jun 2024 13:36:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D3A06B0085; Wed, 5 Jun 2024 09:36:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 00D716B0089; Wed, 5 Jun 2024 09:36:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E1E3B6B008A; Wed, 5 Jun 2024 09:36:10 -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 C5C1E6B0085 for ; Wed, 5 Jun 2024 09:36:10 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 7B7531C2377 for ; Wed, 5 Jun 2024 13:36:10 +0000 (UTC) X-FDA: 82196933700.04.88150EC Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf02.hostedemail.com (Postfix) with ESMTP id 31AC980018 for ; Wed, 5 Jun 2024 13:36:07 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=none; spf=pass (imf02.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=1717594568; 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=9HN8cRj40Iu8RSEwpFttnmunyuNRfYQ9Oos9G3z5jqA=; b=gby5ITpP6TWrGmqTH4yAqPKsgDsiBOqp0/Ewd8o3zyIcmrmS+FywzA2V42QxQXEIpgR6uE BEk7ybNsbQ06CHucRDKNgigXjmvsUNHSJMOf2fVVTNkWo+oRZm5lXsxWS1di27iOG+05Oz K6y5lScaIdz3sfIhGXok/hJYx+pqFXU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717594568; a=rsa-sha256; cv=none; b=0SM6BmOhe7XJvVh/mM2EayPzDw6udCz1ZGkIYvYWgjX4zmmeMSstSrUccNVmEM/34VB86s ne0XnQYLiKbV+w6nphIE+w4WLULAl49p3w5/gj0fumtPMV+eHgeJoEDAgLaptWOVakTtY1 ZndV66caLYOIbgqZDUlsyy7cd6c6/X8= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; spf=pass (imf02.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 Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VvSzQ6mJyzmXlr; Wed, 5 Jun 2024 21:31:30 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 52820180085; Wed, 5 Jun 2024 21:36:04 +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.39; Wed, 5 Jun 2024 21:36:04 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v6 03/15] mm: page_frag: use free_unref_page() to free page fragment Date: Wed, 5 Jun 2024 21:32:53 +0800 Message-ID: <20240605133306.11272-4-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240605133306.11272-1-linyunsheng@huawei.com> References: <20240605133306.11272-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspam-User: X-Stat-Signature: swysr6rj9gjdo1gqqkx1qtcyw46r55ic X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 31AC980018 X-HE-Tag: 1717594567-146458 X-HE-Meta: U2FsdGVkX1/fV7A6c301AZ6q0UGCQZS3zmfIvQRAbBe4ByjqDI6Bv3CWaIxUF4jTQ7IdQ8kXrj2G89IgXWa6lFgIHtzXsDpc58HQe7qLoK7pmZ+FnT6fEP5I/2bLtHZe3tEr+oZ1BXL1u6leg9hryKxkYFXsdqAcd40vEqHY7jeKDmqYwY8OpJuRrNY4xeqh3M/4fkrF83JERNaEu4cHoXpJPZaVCSy/HS6HDH7+YnPiTe84+UVEFgyULV28QiTG4UZElyRKMmKNGjvRnsZOz4Xi/xiIIZfgZLl83smHRZs4WO9RC3kogdLiluDGKIs78mKl8eGT81aZ4PPJE69+PCTzUDJZ8yqSnY6mCNC7uRuBohC1nUMWoCGjzDU/uyhfixHa9FGGpIucVcXvf8HbFxAORG3vFUsGLqlOEMPwEJr0PCjClb7Fbsf/eAo4qMd2/2UliIXnc5qgO9yUaO9rMy3HdDOOzKLNIeNok3lgFQkaraaUYDXAO09az0ylD5iWzahUF6Dl+XecMjp1xy4cWREjWB7BHGYCC+D2wT5w8QV3aNjReIAcIvtKjt4IMdwuTRa7aMSuPgA1+4IP7mLfqJB1xFedhqab9ZkWJ86AqhP1R/BJGbriLQ/hnIQk25zBUlFFlx40tzrBJqlMT1bNU+nt8T7dnc/1EUVZgNaHg3kV/lOtLmPXLxgbV8IXxV35wbiNh9Pyj+3Snkmv8kp9eGhERsDP1ZkGhoFq3L4e/Q0TMtnGAwZPfyX6yuTw+2xUavxMC4DnDsYmBhugcGEFmBZ0Zt1ecvGRkfiKLW+eJdbovdilpEHVTRYycKflR5a6t9BtQuDZHmo36duEHOm4sWoz8W/K3TJRGw7TQzQ3v/E4SizyVuPH7pCIKWABqsqAxk3AvffMzqlv+8UA7azH0BK5S8kM6t0Phr6eE+C6w3IJWkaYyPENq6CjM84CGnnf+VTfBVs/pnoCGqjdEDK uC5auEdJ dwveAknWVunHARYBMU76Tgmc0INIgDT6rMygK8gmCy3l/SWZw2B2y6fWLsO7ZwaA9TNIfXrwrCZCZ1Ldg+HIT2uGES9g01rNKo+nb8k3wSXAV64I25l/xwDAOX23TuAyQ8/f/vxzTag/qHdxQWyc2OtR+p3RqtJ3VN3N2qH+06x607ly7inOLYoYRic+oQz3gxl1H6+HVdFJKFDeyYssqZmB2OuxTF9n9iFVBi1l4xyyWSRhOSlyoFBzcx0uYYZZznmzpqBNeL5HUGmMN7iYdIDn8xYcJ477e8imq 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: free_the_page() used by page_frag call free_unref_page() or __free_pages_ok() depending on pcp_allowed_order(), as the max order of page allocated for page_frag is 3, the checking in pcp_allowed_order() is unnecessary. So call free_unref_page() directly to free a page_frag page to aovid the unnecessary checking. As the free_the_page() is a static function in page_alloc.c, using the new one also allow moving page_frag related code to a new file in the next patch. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- mm/page_alloc.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 2e22ce5675ca..0511c30bc265 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4781,6 +4781,9 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, gfp_t gfp = gfp_mask; #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + /* Ensure free_unref_page() can be used to free the page fragment */ + BUILD_BUG_ON(PAGE_FRAG_CACHE_MAX_ORDER > PAGE_ALLOC_COSTLY_ORDER); + gfp_mask = (gfp_mask & ~__GFP_DIRECT_RECLAIM) | __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, From patchwork Wed Jun 5 13:32:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13686902 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 99741C25B76 for ; Wed, 5 Jun 2024 13:36:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 281556B0089; Wed, 5 Jun 2024 09:36:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2094F6B008A; Wed, 5 Jun 2024 09:36:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0AA456B008C; Wed, 5 Jun 2024 09:36:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id DF7E66B0089 for ; Wed, 5 Jun 2024 09:36:15 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 15646414DD for ; Wed, 5 Jun 2024 13:36:15 +0000 (UTC) X-FDA: 82196933910.22.5EB4B36 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf21.hostedemail.com (Postfix) with ESMTP id A33981C0023 for ; Wed, 5 Jun 2024 13:36:11 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; spf=pass (imf21.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=1717594572; 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=WAIkLCKKbvtIVjBf7Vk4ivml+U7Dw5eMtag5rIhHm7g=; b=bFiXj9u1SzspfB3KnYwbXDy5HWT6y8iBuE9zvjMt+1aMQO9L3ntdLncpfi2X/DfxYcEpu/ SeLTDh2Q4aav9yboRcWbv7de+M4+5JGK9XiQC6bYa+XlScEY2he3vLBu7V6OzM3+DLKd0z c2kwelx3L3uF+aRnTISA6KKX5g6JaLQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717594572; a=rsa-sha256; cv=none; b=26C9d/bWWP0prXnhhM5OUJ/RXqN4A00lj3K3RFocmly78FhfMkqvboTd126OUAvLrKjlMb 7mbFs0875odeSn2NHnSVSalYJtr90jpcY9KFX/FU4Gk8IW8w4qOhkoRjK/V9RvrfJXEOhz nlVsEIskHPsfeizq37FBmrYIJHKFiMA= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; spf=pass (imf21.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 Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VvSzV4pvzzmY4q; Wed, 5 Jun 2024 21:31:34 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 12A2714041A; Wed, 5 Jun 2024 21:36:08 +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.39; Wed, 5 Jun 2024 21:36:07 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , David Howells , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v6 04/15] mm: move the page fragment allocator from page_alloc into its own file Date: Wed, 5 Jun 2024 21:32:54 +0800 Message-ID: <20240605133306.11272-5-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240605133306.11272-1-linyunsheng@huawei.com> References: <20240605133306.11272-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: A33981C0023 X-Rspam-User: X-Stat-Signature: i5dki3fezgo8so63jucucdkehxyatkgq X-HE-Tag: 1717594571-920816 X-HE-Meta: U2FsdGVkX1/6K+gPLq3C3HgVOuNsFKZJncdj+mcaBD/vPxhb3n3CcdaCZdVC5j/kpdtVCW7qecCSzT7idMwtXdfv+4Un/xskPGQtgT6zhPJru+e1HzuC9EfXnhCft/Pd/Y3TAoEC2+gh1Uvds37htY517oyl0diw5tmUkgkM+FTJCQFUPe7ns4IQqVk1/NTd+aUnUHOfnQ9DqeEmfEcwduc/39WRtutO6LhXvndRhayx8GV3tf4NRO4HssSEzxbrT3If+oNvZPXevDbeAtVGRoXUI5DKfaO5EaoQSJP1hgEbeoSkYqJaMbxNh7xZi6FbDHy+TXuFtUGPkakL/ebrGLingFjuXbndELUVSXxceP8x80tHP+B5/e/l2ikPRGV7n9E8zUvp968oa28ca2osO0YB1yhelfKNgJWn4uKGhoX7fcj8ZRZJNT9dBNJfdOdlRVooN9tWnmvkrvyyMRWgKb/vl0NJJWGacFnETNunYTvD/cSY/wV0n8fnBRs7HP38+DJGOJhbCMS1qioF2f/GQNiaPo31R23wTs2yiJd6fLFVLQ2QeNxAG5FhGOOeySrsVhTyee0NaJVkusaTsqWWdtJsT+EEQsvGznU4LF+Y9Vps3Z0Kd6lfrF1INUCHfHtLuUn0W/KCRbVAhyAcZcmrx7B+qF1Goul9LU/Cf5CbXhOIEONLubV/x4Sv8cnE3nvyhM8xu3QenulO92Hx2GhvOuWHtvu+W0cmkjavooBE74AEb8/CIB5LEBYzrrVh6jMgIbmVzTDGOejpapiAcBFEEXfHaNPaJdCq1DjNeEobltDb/qKcyVfJw6WQoIajqDXJ9sh6Pf8vi1coFrG55ZxkZNSzxX09Zty8fchfqLLBwCVluhn856WrewZt5S+zRrYo+iGV8nKhs4Eiui/yVIYGvB5Kbak6P2O58TZKBixVeriK7URfS9HS82vjqoEsqTbk4G8VrCxAPVeqnBDULmj N3Jw3UQm UcAw+672WLoqXPwl6Op3ZqQ1RHcqpjbvGoQ7jN103JsiZXDptwXCgI9t/E04YFNFPeTOx7PS3LYXjgPC5oG4qfkDO/raMQdwst2pPgR3yceBK3R625jViejBuGRPEmnllFv4YsLx56UeQbGUI4X8DahaFOv7yLYNJoUvIhuk7rf9OcCrkJ8noLvl/R17fNtd/sYH10PBGj2trGf3fEFmxsC7FRZw9yf3PS8cTD1QMWH+HYdIIWiYbTg+P76TlwAyNqEIsQQqcCUfZl5pK6jmyY8Zmb9ZeLzbdSsMd++Zh0HFCPwOBbP7droxa3qPpqDiTKPpXqXJgxcBqdo9CmwJ2qMUxZQnT0PfuXJ2fU6LNt8XWc/DD3AfdcZfSVsdfCZaAyzbyzcoguXDQoGbUoELzrWQDeSw+7Hkukq1aJh/9HkqXQpaD/97JGtShjCcA1ICu7jsir9Wok4KMpY0SInRNgtfd+0TJOXw/HYN6Zs/S4LZF/FaouA+hVskWfuJ7fliArJD+c90HFLY0LEN/wzYIdWQOwA== 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: Inspired by [1], move the page fragment allocator from page_alloc into its own c file and header file, as we are about to make more change for it to replace another page_frag implementation in sock.c 1. https://lore.kernel.org/all/20230411160902.4134381-3-dhowells@redhat.com/ CC: David Howells CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/gfp.h | 22 ----- include/linux/mm_types.h | 18 ---- include/linux/page_frag_cache.h | 47 ++++++++++ include/linux/skbuff.h | 1 + mm/Makefile | 1 + mm/page_alloc.c | 139 ------------------------------ mm/page_frag_cache.c | 147 ++++++++++++++++++++++++++++++++ mm/page_frag_test.c | 1 + 8 files changed, 197 insertions(+), 179 deletions(-) create mode 100644 include/linux/page_frag_cache.h create mode 100644 mm/page_frag_cache.c diff --git a/include/linux/gfp.h b/include/linux/gfp.h index 7f9691d375f0..3d8f9dc6c6ee 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -363,28 +363,6 @@ __meminit void *alloc_pages_exact_nid_noprof(int nid, size_t size, gfp_t gfp_mas extern void __free_pages(struct page *page, unsigned int order); extern void free_pages(unsigned long addr, unsigned int order); -struct page_frag_cache; -void page_frag_cache_drain(struct page_frag_cache *nc); -extern 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); - -static inline void *page_frag_alloc_align(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask, - unsigned int align) -{ - WARN_ON_ONCE(!is_power_of_2(align)); - return __page_frag_alloc_align(nc, fragsz, gfp_mask, -align); -} - -static inline void *page_frag_alloc(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask) -{ - return __page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u); -} - -extern void page_frag_free(void *addr); - #define __free_page(page) __free_pages((page), 0) #define free_page(addr) free_pages((addr), 0) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 24323c7d0bd4..5b99dbec91d7 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -505,9 +505,6 @@ static_assert(sizeof(struct ptdesc) <= sizeof(struct page)); */ #define STRUCT_PAGE_MAX_SHIFT (order_base_2(sizeof(struct page))) -#define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK) -#define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE) - /* * page_private can be used on tail pages. However, PagePrivate is only * checked by the VM on the head page. So page_private on the tail pages @@ -526,21 +523,6 @@ static inline void *folio_get_private(struct folio *folio) return folio->private; } -struct page_frag_cache { - void * va; -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - __u16 offset; - __u16 size; -#else - __u32 offset; -#endif - /* we maintain a pagecount bias, so that we dont dirty cache line - * containing page->_refcount every time we allocate a fragment. - */ - unsigned int pagecnt_bias; - bool pfmemalloc; -}; - typedef unsigned long vm_flags_t; /* diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h new file mode 100644 index 000000000000..3a44bfc99750 --- /dev/null +++ b/include/linux/page_frag_cache.h @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _LINUX_PAGE_FRAG_CACHE_H +#define _LINUX_PAGE_FRAG_CACHE_H + +#include + +#define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK) +#define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE) + +struct page_frag_cache { + void *va; +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + __u16 offset; + __u16 size; +#else + __u32 offset; +#endif + /* we maintain a pagecount bias, so that we dont dirty cache line + * containing page->_refcount every time we allocate a fragment. + */ + unsigned int pagecnt_bias; + bool pfmemalloc; +}; + +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); + +static inline void *page_frag_alloc_align(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask, + unsigned int align) +{ + WARN_ON_ONCE(!is_power_of_2(align)); + return __page_frag_alloc_align(nc, fragsz, gfp_mask, -align); +} + +static inline void *page_frag_alloc(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask) +{ + return __page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u); +} + +void page_frag_free(void *addr); + +#endif diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 1c2902eaebd3..bc1a64c6a436 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -31,6 +31,7 @@ #include #include #include +#include #include #if IS_ENABLED(CONFIG_NF_CONNTRACK) #include diff --git a/mm/Makefile b/mm/Makefile index d2bb17d06b88..a6b913f11b1a 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -60,6 +60,7 @@ page-alloc-$(CONFIG_SHUFFLE_PAGE_ALLOCATOR) += shuffle.o memory-hotplug-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o obj-y += page-alloc.o +obj-y += page_frag_cache.o obj-y += init-mm.o obj-y += memblock.o obj-y += $(memory-hotplug-y) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 0511c30bc265..2990d170b487 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4763,145 +4763,6 @@ void free_pages(unsigned long addr, unsigned int order) EXPORT_SYMBOL(free_pages); -/* - * Page Fragment: - * An arbitrary-length arbitrary-offset area of memory which resides - * within a 0 or higher order page. Multiple fragments within that page - * are individually refcounted, in the page's reference counter. - * - * The page_frag functions below provide a simple allocation framework for - * page fragments. This is used by the network stack and network device - * drivers to provide a backing region of memory for use as either an - * sk_buff->head, or to be used in the "frags" portion of skb_shared_info. - */ -static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, - gfp_t gfp_mask) -{ - struct page *page = NULL; - gfp_t gfp = gfp_mask; - -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* Ensure free_unref_page() can be used to free the page fragment */ - BUILD_BUG_ON(PAGE_FRAG_CACHE_MAX_ORDER > PAGE_ALLOC_COSTLY_ORDER); - - gfp_mask = (gfp_mask & ~__GFP_DIRECT_RECLAIM) | __GFP_COMP | - __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; - page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, - PAGE_FRAG_CACHE_MAX_ORDER); - nc->size = page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; -#endif - if (unlikely(!page)) - page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); - - nc->va = page ? page_address(page) : NULL; - - return page; -} - -void page_frag_cache_drain(struct page_frag_cache *nc) -{ - if (!nc->va) - return; - - __page_frag_cache_drain(virt_to_head_page(nc->va), nc->pagecnt_bias); - nc->va = NULL; -} -EXPORT_SYMBOL(page_frag_cache_drain); - -void __page_frag_cache_drain(struct page *page, unsigned int count) -{ - VM_BUG_ON_PAGE(page_ref_count(page) == 0, page); - - if (page_ref_sub_and_test(page, count)) - free_unref_page(page, compound_order(page)); -} -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 size = PAGE_SIZE; - struct page *page; - int offset; - - if (unlikely(!nc->va)) { -refill: - page = __page_frag_cache_refill(nc, gfp_mask); - if (!page) - return NULL; - -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif - /* Even if we own the page, we do not use atomic_set(). - * This would break get_page_unless_zero() users. - */ - page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); - - /* reset page count bias and offset to start of new frag */ - nc->pfmemalloc = page_is_pfmemalloc(page); - nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset = size; - } - - offset = nc->offset - fragsz; - if (unlikely(offset < 0)) { - page = virt_to_page(nc->va); - - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) - goto refill; - - if (unlikely(nc->pfmemalloc)) { - free_unref_page(page, compound_order(page)); - goto refill; - } - -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif - /* 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 offset to start of new frag */ - nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset = size - fragsz; - if (unlikely(offset < 0)) { - /* - * The caller is trying to allocate a fragment - * with fragsz > PAGE_SIZE but the cache isn't big - * enough to satisfy the request, this may - * happen in low memory conditions. - * We don't release the cache page because - * it could make memory pressure worse - * so we simply return NULL here. - */ - return NULL; - } - } - - nc->pagecnt_bias--; - offset &= align_mask; - nc->offset = offset; - - return nc->va + offset; -} -EXPORT_SYMBOL(__page_frag_alloc_align); - -/* - * Frees a page fragment allocated out of either a compound or order 0 page. - */ -void page_frag_free(void *addr) -{ - struct page *page = virt_to_head_page(addr); - - if (unlikely(put_page_testzero(page))) - free_unref_page(page, compound_order(page)); -} -EXPORT_SYMBOL(page_frag_free); - static void *make_alloc_exact(unsigned long addr, unsigned int order, size_t size) { diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c new file mode 100644 index 000000000000..64993b5d1243 --- /dev/null +++ b/mm/page_frag_cache.c @@ -0,0 +1,147 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Page fragment allocator + * + * Page Fragment: + * An arbitrary-length arbitrary-offset area of memory which resides within a + * 0 or higher order page. Multiple fragments within that page are + * individually refcounted, in the page's reference counter. + * + * The page_frag functions provide a simple allocation framework for page + * fragments. This is used by the network stack and network device drivers to + * provide a backing region of memory for use as either an sk_buff->head, or to + * be used in the "frags" portion of skb_shared_info. + */ + +#include +#include +#include +#include +#include "internal.h" + +static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, + gfp_t gfp_mask) +{ + struct page *page = NULL; + gfp_t gfp = gfp_mask; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + /* Ensure free_unref_page() can be used to free the page fragment */ + BUILD_BUG_ON(PAGE_FRAG_CACHE_MAX_ORDER > PAGE_ALLOC_COSTLY_ORDER); + + gfp_mask = (gfp_mask & ~__GFP_DIRECT_RECLAIM) | __GFP_COMP | + __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; + page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, + PAGE_FRAG_CACHE_MAX_ORDER); + nc->size = page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; +#endif + if (unlikely(!page)) + page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); + + nc->va = page ? page_address(page) : NULL; + + return page; +} + +void page_frag_cache_drain(struct page_frag_cache *nc) +{ + if (!nc->va) + return; + + __page_frag_cache_drain(virt_to_head_page(nc->va), nc->pagecnt_bias); + nc->va = NULL; +} +EXPORT_SYMBOL(page_frag_cache_drain); + +void __page_frag_cache_drain(struct page *page, unsigned int count) +{ + VM_BUG_ON_PAGE(page_ref_count(page) == 0, page); + + if (page_ref_sub_and_test(page, count)) + free_unref_page(page, compound_order(page)); +} +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 size = PAGE_SIZE; + struct page *page; + int offset; + + if (unlikely(!nc->va)) { +refill: + page = __page_frag_cache_refill(nc, gfp_mask); + if (!page) + return NULL; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + /* if size can vary use size else just use PAGE_SIZE */ + size = nc->size; +#endif + /* Even if we own the page, we do not use atomic_set(). + * This would break get_page_unless_zero() users. + */ + page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); + + /* reset page count bias and offset to start of new frag */ + nc->pfmemalloc = page_is_pfmemalloc(page); + nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; + nc->offset = size; + } + + offset = nc->offset - fragsz; + if (unlikely(offset < 0)) { + page = virt_to_page(nc->va); + + if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) + goto refill; + + if (unlikely(nc->pfmemalloc)) { + free_unref_page(page, compound_order(page)); + goto refill; + } + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + /* if size can vary use size else just use PAGE_SIZE */ + size = nc->size; +#endif + /* 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 offset to start of new frag */ + nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; + offset = size - fragsz; + if (unlikely(offset < 0)) { + /* + * The caller is trying to allocate a fragment + * with fragsz > PAGE_SIZE but the cache isn't big + * enough to satisfy the request, this may + * happen in low memory conditions. + * We don't release the cache page because + * it could make memory pressure worse + * so we simply return NULL here. + */ + return NULL; + } + } + + nc->pagecnt_bias--; + offset &= align_mask; + nc->offset = offset; + + return nc->va + offset; +} +EXPORT_SYMBOL(__page_frag_alloc_align); + +/* + * Frees a page fragment allocated out of either a compound or order 0 page. + */ +void page_frag_free(void *addr) +{ + struct page *page = virt_to_head_page(addr); + + if (unlikely(put_page_testzero(page))) + free_unref_page(page, compound_order(page)); +} +EXPORT_SYMBOL(page_frag_free); diff --git a/mm/page_frag_test.c b/mm/page_frag_test.c index a3605f1a8b2b..1349f6c6b521 100644 --- a/mm/page_frag_test.c +++ b/mm/page_frag_test.c @@ -16,6 +16,7 @@ #include #include #include +#include #define OBJPOOL_NR_OBJECT_MAX BIT(24) From patchwork Wed Jun 5 13:32:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13686903 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 A3DCCC27C5E for ; Wed, 5 Jun 2024 13:36:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6EFC56B008A; Wed, 5 Jun 2024 09:36:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 69DF86B008C; Wed, 5 Jun 2024 09:36:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 564E56B0092; Wed, 5 Jun 2024 09:36:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 355136B008C for ; Wed, 5 Jun 2024 09:36:16 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D0218140D82 for ; Wed, 5 Jun 2024 13:36:15 +0000 (UTC) X-FDA: 82196933910.25.F7DF074 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf09.hostedemail.com (Postfix) with ESMTP id 2D14514000C for ; Wed, 5 Jun 2024 13:36:12 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; spf=pass (imf09.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 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=1717594573; a=rsa-sha256; cv=none; b=vzj0SXe7L+noTuV99DlPD4irR0y06GXRJ8clhmSZ9w/KN414IjRjsr6HP1/J59E2PLtHtP ZzkbDzCXS0XRVW8kngOKduKIPuTT9GUjRdlJDZrQbgMunfJSw11SjsSGGeH2DZAJmfzv22 KdjWrqjc/vWItcdSUwcbiIGsGGRdJlY= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; spf=pass (imf09.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 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=1717594573; 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=651YPQJY1ccuAoTSZ6mx1jH9FKczANbdWKeYsrewnwc=; b=A9nAhRykzozUyDGAHT6yKhdav8buqM4xICaQ+iMZVOxS4yPcCxVqmfmD8Hr5fBryr5oqFW WdWcMzgwocNC6xqS8rgrJSnu6w1T/wfI1e3sjO9kYFxzRYd8h2HseHxvfDjgUycNjx+yOK zRlmw6BqW9tl0ST5pH7riMAT6VOLWaA= Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VvT0F1TzXzsSlt; Wed, 5 Jun 2024 21:32:13 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 1D360180AA6; Wed, 5 Jun 2024 21:36:10 +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.39; Wed, 5 Jun 2024 21:36:09 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v6 05/15] mm: page_frag: use initial zero offset for page_frag_alloc_align() Date: Wed, 5 Jun 2024 21:32:55 +0800 Message-ID: <20240605133306.11272-6-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240605133306.11272-1-linyunsheng@huawei.com> References: <20240605133306.11272-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Queue-Id: 2D14514000C X-Rspam-User: X-Rspamd-Server: rspam12 X-Stat-Signature: 96qg8pfo3wqg3771gzfxmeqwmso3y4sk X-HE-Tag: 1717594572-213123 X-HE-Meta: U2FsdGVkX1+kNoTd76QaLjdY9Y/Kh0CY8pnD4eG/FFKhuQ5t2+FkGabA2z9DpbI0n1fYPN/54KJ27vfYSX5MRfID4r6AcHqsYXGK6BAqrEq5czabBT2ZWdm6JJ8otbM0Epocwjw+Kt9Kgy8ipNZFmvI1UBdO6gzS3qmL8oVdNo4XSYOARUiGrKGBLt6WtGSwdh9PenlMW9fT/YqttoJXy0TqUWPhfI26jko4xM69H3ak9xg0ZHEFHMS2ugDjHxT4Gh6wRjWZQ2uQao1I4sjrpgaAA7mpLntAQyOBI4hVP8ZhH2K0aEhoS9rI5RP97G3FkU2Pbo2YiINhTt/2pycAgb/ibKRQ8c8h5Y2DAXvdThFzuE1Yzi12jOcuDTZhmIrf64ea4yNhQQh29B+emXDfVLy0sfilz7/YYUrfeIsq9oMu8bv4YoGkqvuqpfBTAL3jOj5sdTOO6NPOsrmqATrMXtfURKAtxrwb2KILQKHbPcoyJcf/nC9ITpfv41ReC7qpDi6K4QnNM3Cahn44bv+j9LgVtxLf4AEqI0qBtP62F8gTcOEVKvtxlVSzKVSAt7R4pq9ogKZY+ZISIYUV2UVNtO60rRhFFfLhfV0nYM49Scwel7B/iMx98kYsfhhxoEAIcn3ZNGO+1PW5d9pOzZtwXLCKBi7POaFRpcaWjknnPUBAT5RyrCI4YeqTvdf0SyT0e5PBitF2oSJZCT4Avd3wwCn0cmdDfwsDAw4znCB8TqZFdKPnjxagrUCaMV2W1yFziFF20q29MBr1bjL4dzspN7hxkqgyEeHt1tbeejZMM68712hGqE6T2MRvh2C5AoUqNBniZb8eYV+p8WScL0+dtTEt5bZdR67GuIdp6HCVvK8bOs78kogVths1imzlRjZ/NL++ZguHiJcUK7+gsj7EZF/oG356abWdSQX6+pFjFGAlviw+XFy1lkIuNVcrcPLi5BvTIsLfxd2QqlpOATN zqGUoJQj O2wxwSRkQB1q+J6Y2jlQ148zvV396rkBXONw4k0VNOZ7qe9v96l615yqYKV9EMjY9JY86h/ZnYDAGADsD4V5Nyz1MfjSApsdWwre0nCiX5sIfnP3t+oitGnuDMYjMj3KzbAEWrJmxlJampKQmHFLp3o3gUmp9BL/CYe5FxuiQNg7YIMzIYE17BQUicMyR9hWLGyF1+CyYykeBi1RsJKyeRCUR2/tIxK9cH+6eiq+SwbhWxEdpfmrKzFHIzn+85iwhqM15hqul5rWPIpeq6HKJxaufr3+VqjIQH/07c7SPScTruEzA27WiAhh+cfMH1R8+EZ+yeAFG45c61L1iGK/HGouZfA== 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: We are above to use page_frag_alloc_*() API to not just allocate memory for skb->data, but also use them to do the memory allocation for skb frag too. Currently the implementation of page_frag in mm subsystem is running the offset as a countdown rather than count-up value, there may have several advantages to that as mentioned in [1], but it may have some disadvantages, for example, it may disable skb frag coaleasing and more correct cache prefetching We have a trade-off to make in order to have a unified implementation and API for page_frag, so use a initial zero offset in this patch, and the following patch will try to make some optimization to aovid the disadvantages as much as possible. As offsets is added due to alignment requirement before actually checking if the cache is enough, which might make it exploitable if caller passes a align value bigger than 32K mistakenly. As we are allowing order 3 page allocation to fail easily under low memory condition, align value bigger than PAGE_SIZE is not really allowed, so add a 'align > PAGE_SIZE' checking in page_frag_alloc_va_align() to catch that. 1. https://lore.kernel.org/all/f4abe71b3439b39d17a6fb2d410180f367cadf5c.camel@gmail.com/ CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 2 +- include/linux/skbuff.h | 4 ++-- mm/page_frag_cache.c | 26 +++++++++++--------------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 3a44bfc99750..b9411f0db25a 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -32,7 +32,7 @@ static inline void *page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align) { - WARN_ON_ONCE(!is_power_of_2(align)); + WARN_ON_ONCE(!is_power_of_2(align) || align > PAGE_SIZE); return __page_frag_alloc_align(nc, fragsz, gfp_mask, -align); } diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index bc1a64c6a436..fbff80a03224 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -3294,7 +3294,7 @@ static inline void *netdev_alloc_frag(unsigned int fragsz) static inline void *netdev_alloc_frag_align(unsigned int fragsz, unsigned int align) { - WARN_ON_ONCE(!is_power_of_2(align)); + WARN_ON_ONCE(!is_power_of_2(align) || align > PAGE_SIZE); return __netdev_alloc_frag_align(fragsz, -align); } @@ -3365,7 +3365,7 @@ static inline void *napi_alloc_frag(unsigned int fragsz) static inline void *napi_alloc_frag_align(unsigned int fragsz, unsigned int align) { - WARN_ON_ONCE(!is_power_of_2(align)); + WARN_ON_ONCE(!is_power_of_2(align) || align > PAGE_SIZE); return __napi_alloc_frag_align(fragsz, -align); } diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 64993b5d1243..d569021376d4 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -75,10 +75,6 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, if (!page) return NULL; -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif /* Even if we own the page, we do not use atomic_set(). * This would break get_page_unless_zero() users. */ @@ -87,11 +83,16 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, /* reset page count bias and offset to start of new frag */ nc->pfmemalloc = page_is_pfmemalloc(page); nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset = size; + nc->offset = 0; } - offset = nc->offset - fragsz; - if (unlikely(offset < 0)) { +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + /* if size can vary use size else just use PAGE_SIZE */ + size = nc->size; +#endif + + offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); + if (unlikely(offset + fragsz > size)) { page = virt_to_page(nc->va); if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) @@ -102,17 +103,13 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, goto refill; } -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif /* 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 offset to start of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset = size - fragsz; - if (unlikely(offset < 0)) { + offset = 0; + if (unlikely(fragsz > PAGE_SIZE)) { /* * The caller is trying to allocate a fragment * with fragsz > PAGE_SIZE but the cache isn't big @@ -127,8 +124,7 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, } nc->pagecnt_bias--; - offset &= align_mask; - nc->offset = offset; + nc->offset = offset + fragsz; return nc->va + offset; } From patchwork Wed Jun 5 13:32:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13686904 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 33BB7C41513 for ; Wed, 5 Jun 2024 13:36:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B0C366B0093; Wed, 5 Jun 2024 09:36:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AE2F06B0095; Wed, 5 Jun 2024 09:36:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 95CDA6B0096; Wed, 5 Jun 2024 09:36:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 736BE6B0093 for ; Wed, 5 Jun 2024 09:36:22 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 362BFA126B for ; Wed, 5 Jun 2024 13:36:22 +0000 (UTC) X-FDA: 82196934204.05.00E5729 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf13.hostedemail.com (Postfix) with ESMTP id C5E6720017 for ; Wed, 5 Jun 2024 13:36:19 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf13.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 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=1717594580; 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=vDBzazFC5bDVy8CKs7O9XYop81ERKehD4ewqZxPAJY0=; b=vZ+pcFGBwfKhybs1rG18Cx7ErfdnXoxraacq3eq2HXOw7F4/kclowJ6zZYwVo8lG83wMfF UtPekDta/7EXVyjSlYsnAOlOa71TmA+48n5rxqy+whJizXMU/IIHyJdBAkSqjOaAPv5pCb tk428jhWbN0kRl6i8CKJlJFC69yBqs0= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf13.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717594580; a=rsa-sha256; cv=none; b=y5BYCSZYxIiNJCxqewUtcqyecUuSjkJcbchxq9awvDVjMXZ6b7EVNibOLitklCddOFB7bY D5VJg4bgEizWpyByjL8XpPRxO8bIKnNAij1ZaVwxmgZYAinduu7vsFWkgMAXVg3/zxsS10 oAJQBWZt0l9kM+2ccBkpSRI4/YP5fCc= Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VvT0M2nGFzwRqB; Wed, 5 Jun 2024 21:32:19 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 253B318007A; Wed, 5 Jun 2024 21:36:16 +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.39; Wed, 5 Jun 2024 21:36:15 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Jeroen de Borst , Praveen Kaligineedi , Shailend Chand , Eric Dumazet , Jesse Brandeburg , Tony Nguyen , Sunil Goutham , Geetha sowjanya , Subbaraya Sundeep , hariprasad , Felix Fietkau , Sean Wang , Mark Lee , Lorenzo Bianconi , Matthias Brugger , AngeloGioacchino Del Regno , Keith Busch , Jens Axboe , Christoph Hellwig , Sagi Grimberg , Chaitanya Kulkarni , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Andrew Morton , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , David Howells , Marc Dionne , Chuck Lever , Jeff Layton , Neil Brown , Olga Kornievskaia , Dai Ngo , Tom Talpey , Trond Myklebust , Anna Schumaker , , , , , , , , , , Subject: [PATCH net-next v6 06/15] mm: page_frag: add '_va' suffix to page_frag API Date: Wed, 5 Jun 2024 21:32:56 +0800 Message-ID: <20240605133306.11272-7-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240605133306.11272-1-linyunsheng@huawei.com> References: <20240605133306.11272-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500005.china.huawei.com (7.185.36.74) X-Stat-Signature: tiq9z7rgzhff6ow9mjigy1rs8nyhawsf X-Rspamd-Queue-Id: C5E6720017 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1717594579-98925 X-HE-Meta: U2FsdGVkX1+n8Py1s9ulZvYRRYeP/rpeGfGdHpNMT3PQafmHMGCC7OZaDJ76mtgLRiVCykHb+YyOuMwDS+L88xXWr3rsbz0yY2Ng+khkUBv2sOPvAen8REJ97kn1m+5VSDbN7w4VanKj4p+VM9jTc++cIinHSUbuSNLDfeuDr4AYf2Jv6SNMKwfUwG707uHuBoN4yfybKOyyUqk5zZR8iqmc+PiVvBUizX9asif+jId4XzWmcJWmyV1xU2xL1mM4t89POGOV0C8IoUK1XOTOo2BldO3qdOIGi7ZAhznyGuxroYMUck7d0fKxtum+0fRN3t1sjqYJmzHjQNcBuekUJ1pjf782BlKa0vZHoLEv8mAX32MbuKPlDC1QzP/FAq0IkMLgN+FG8rVLzAn00KX3fprj5nvk3c/KDA/c2ZIpP/2nbeCjwB5CKGbIPuo9nbrzmsPF/ndiNnghGhdcXLbGGPV9QeqTtWyD9NurHDAWnngFRFRFhmrIlBys5uXDuoXwhhD6t35CsZwh0/gUeIpFKkUrlhXUKXjVdKHE9NgqVvHaj0/G7Bx9czY5gwgvXFYls6Gvb7rWe5gF0bh78E+Cv+jhOo6YlH69v7idlPWxw8NNc3KIzAO2AdPOep0MgIwDNvUloOkEQ3rXRMtdDSpFNnnDanMq+WG0t38q+FHKQ8G4i0zmUxTrMPZSuJgmAo9JmkCiaBrvIRwaKkwZFYu+51deJ5srA7dcZcMMfZ9vd6mRdZFPS9nl2l8ssD7IqyHcSlere0/mY4msAMHmEVhDdNX3Uqpsptd5ZLv6PmJNoxzvbGyATP/N/45KtzakEhrWHQdZa6BiJtKMaUk5P9IiB+ouFBnCRJ0LZK29GomjiC930i+jpPFJHaiw7tCNgDHckNx3pSa2nE3vPxkKXeZMvIj1/5W/RAliv1sdruXjes+zjRPaikUSgA5yXxMOud7AOOzdnZNYMHWQOcrbH9X gLM1vjyN ZbaKBooYzpJp9lYSj4cuzGdUp2PH3kSpETIHNDQ9LYxOmPefDmM80Om72S6feDslVueXXdh3G//EW3NoDkGM0z8TnBNoNshE6fEbJiWdGOnsqvBeruP60OpBkcouMy3+Vz2JsEURGdz+7u2YoQeAOknw1eV7evF6+NbuDg0QtZuUJ9qT5lkIQEL/F0vRUGk9fDAt1rb8e0aEXu3MVHBx/cdw/nAD4KV2vGcMA4fFLqSBV7f4oBRADZvCpKEiqc92iwKUUU+kLS81YC6vDgkynGVRK3i5DhdooWxk3 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: Currently the page_frag API is returning 'virtual address' or 'va' when allocing and expecting 'virtual address' or 'va' as input when freeing. As we are about to support new use cases that the caller need to deal with 'struct page' or need to deal with both 'va' and 'struct page'. In order to differentiate the API handling between 'va' and 'struct page', add '_va' suffix to the corresponding API mirroring the page_pool_alloc_va() API of the page_pool. So that callers expecting to deal with va, page or both va and page may call page_frag_alloc_va*, page_frag_alloc_pg*, or page_frag_alloc* API accordingly. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- drivers/net/ethernet/google/gve/gve_rx.c | 4 ++-- drivers/net/ethernet/intel/ice/ice_txrx.c | 2 +- drivers/net/ethernet/intel/ice/ice_txrx.h | 2 +- drivers/net/ethernet/intel/ice/ice_txrx_lib.c | 2 +- .../net/ethernet/intel/ixgbevf/ixgbevf_main.c | 4 ++-- .../marvell/octeontx2/nic/otx2_common.c | 2 +- drivers/net/ethernet/mediatek/mtk_wed_wo.c | 4 ++-- drivers/nvme/host/tcp.c | 8 +++---- drivers/nvme/target/tcp.c | 22 +++++++++---------- drivers/vhost/net.c | 6 ++--- include/linux/page_frag_cache.h | 21 +++++++++--------- include/linux/skbuff.h | 2 +- kernel/bpf/cpumap.c | 2 +- mm/page_frag_cache.c | 12 +++++----- mm/page_frag_test.c | 13 ++++++----- net/core/skbuff.c | 18 +++++++-------- net/core/xdp.c | 2 +- net/rxrpc/txbuf.c | 15 +++++++------ net/sunrpc/svcsock.c | 6 ++--- 19 files changed, 76 insertions(+), 71 deletions(-) diff --git a/drivers/net/ethernet/google/gve/gve_rx.c b/drivers/net/ethernet/google/gve/gve_rx.c index acb73d4d0de6..b6c10100e462 100644 --- a/drivers/net/ethernet/google/gve/gve_rx.c +++ b/drivers/net/ethernet/google/gve/gve_rx.c @@ -729,7 +729,7 @@ static int gve_xdp_redirect(struct net_device *dev, struct gve_rx_ring *rx, total_len = headroom + SKB_DATA_ALIGN(len) + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); - frame = page_frag_alloc(&rx->page_cache, total_len, GFP_ATOMIC); + frame = page_frag_alloc_va(&rx->page_cache, total_len, GFP_ATOMIC); if (!frame) { u64_stats_update_begin(&rx->statss); rx->xdp_alloc_fails++; @@ -742,7 +742,7 @@ static int gve_xdp_redirect(struct net_device *dev, struct gve_rx_ring *rx, err = xdp_do_redirect(dev, &new, xdp_prog); if (err) - page_frag_free(frame); + page_frag_free_va(frame); return err; } diff --git a/drivers/net/ethernet/intel/ice/ice_txrx.c b/drivers/net/ethernet/intel/ice/ice_txrx.c index 8bb743f78fcb..399b317c509d 100644 --- a/drivers/net/ethernet/intel/ice/ice_txrx.c +++ b/drivers/net/ethernet/intel/ice/ice_txrx.c @@ -126,7 +126,7 @@ ice_unmap_and_free_tx_buf(struct ice_tx_ring *ring, struct ice_tx_buf *tx_buf) dev_kfree_skb_any(tx_buf->skb); break; case ICE_TX_BUF_XDP_TX: - page_frag_free(tx_buf->raw_buf); + page_frag_free_va(tx_buf->raw_buf); break; case ICE_TX_BUF_XDP_XMIT: xdp_return_frame(tx_buf->xdpf); diff --git a/drivers/net/ethernet/intel/ice/ice_txrx.h b/drivers/net/ethernet/intel/ice/ice_txrx.h index feba314a3fe4..6379f57d8228 100644 --- a/drivers/net/ethernet/intel/ice/ice_txrx.h +++ b/drivers/net/ethernet/intel/ice/ice_txrx.h @@ -148,7 +148,7 @@ static inline int ice_skb_pad(void) * @ICE_TX_BUF_DUMMY: dummy Flow Director packet, unmap and kfree() * @ICE_TX_BUF_FRAG: mapped skb OR &xdp_buff frag, only unmap DMA * @ICE_TX_BUF_SKB: &sk_buff, unmap and consume_skb(), update stats - * @ICE_TX_BUF_XDP_TX: &xdp_buff, unmap and page_frag_free(), stats + * @ICE_TX_BUF_XDP_TX: &xdp_buff, unmap and page_frag_free_va(), stats * @ICE_TX_BUF_XDP_XMIT: &xdp_frame, unmap and xdp_return_frame(), stats * @ICE_TX_BUF_XSK_TX: &xdp_buff on XSk queue, xsk_buff_free(), stats */ diff --git a/drivers/net/ethernet/intel/ice/ice_txrx_lib.c b/drivers/net/ethernet/intel/ice/ice_txrx_lib.c index 2719f0e20933..a1a41a14df0d 100644 --- a/drivers/net/ethernet/intel/ice/ice_txrx_lib.c +++ b/drivers/net/ethernet/intel/ice/ice_txrx_lib.c @@ -250,7 +250,7 @@ ice_clean_xdp_tx_buf(struct device *dev, struct ice_tx_buf *tx_buf, switch (tx_buf->type) { case ICE_TX_BUF_XDP_TX: - page_frag_free(tx_buf->raw_buf); + page_frag_free_va(tx_buf->raw_buf); break; case ICE_TX_BUF_XDP_XMIT: xdp_return_frame_bulk(tx_buf->xdpf, bq); diff --git a/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c b/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c index b938dc06045d..fcd1b149a45d 100644 --- a/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c +++ b/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c @@ -303,7 +303,7 @@ static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector, /* free the skb */ if (ring_is_xdp(tx_ring)) - page_frag_free(tx_buffer->data); + page_frag_free_va(tx_buffer->data); else napi_consume_skb(tx_buffer->skb, napi_budget); @@ -2413,7 +2413,7 @@ static void ixgbevf_clean_tx_ring(struct ixgbevf_ring *tx_ring) /* Free all the Tx ring sk_buffs */ if (ring_is_xdp(tx_ring)) - page_frag_free(tx_buffer->data); + page_frag_free_va(tx_buffer->data); else dev_kfree_skb_any(tx_buffer->skb); diff --git a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c index a85ac039d779..8eb5820b8a70 100644 --- a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c +++ b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c @@ -553,7 +553,7 @@ static int __otx2_alloc_rbuf(struct otx2_nic *pfvf, struct otx2_pool *pool, *dma = dma_map_single_attrs(pfvf->dev, buf, pool->rbsize, DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC); if (unlikely(dma_mapping_error(pfvf->dev, *dma))) { - page_frag_free(buf); + page_frag_free_va(buf); return -ENOMEM; } diff --git a/drivers/net/ethernet/mediatek/mtk_wed_wo.c b/drivers/net/ethernet/mediatek/mtk_wed_wo.c index 7063c78bd35f..c4228719f8a4 100644 --- a/drivers/net/ethernet/mediatek/mtk_wed_wo.c +++ b/drivers/net/ethernet/mediatek/mtk_wed_wo.c @@ -142,8 +142,8 @@ mtk_wed_wo_queue_refill(struct mtk_wed_wo *wo, struct mtk_wed_wo_queue *q, dma_addr_t addr; void *buf; - buf = page_frag_alloc(&q->cache, q->buf_size, - GFP_ATOMIC | GFP_DMA32); + buf = page_frag_alloc_va(&q->cache, q->buf_size, + GFP_ATOMIC | GFP_DMA32); if (!buf) break; diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c index 28bc2f373cfa..5f0af3191477 100644 --- a/drivers/nvme/host/tcp.c +++ b/drivers/nvme/host/tcp.c @@ -506,7 +506,7 @@ static void nvme_tcp_exit_request(struct blk_mq_tag_set *set, { struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); - page_frag_free(req->pdu); + page_frag_free_va(req->pdu); } static int nvme_tcp_init_request(struct blk_mq_tag_set *set, @@ -520,7 +520,7 @@ static int nvme_tcp_init_request(struct blk_mq_tag_set *set, struct nvme_tcp_queue *queue = &ctrl->queues[queue_idx]; u8 hdgst = nvme_tcp_hdgst_len(queue); - req->pdu = page_frag_alloc(&queue->pf_cache, + req->pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(struct nvme_tcp_cmd_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!req->pdu) @@ -1337,7 +1337,7 @@ static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl) { struct nvme_tcp_request *async = &ctrl->async_req; - page_frag_free(async->pdu); + page_frag_free_va(async->pdu); } static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl) @@ -1346,7 +1346,7 @@ static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl) struct nvme_tcp_request *async = &ctrl->async_req; u8 hdgst = nvme_tcp_hdgst_len(queue); - async->pdu = page_frag_alloc(&queue->pf_cache, + async->pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(struct nvme_tcp_cmd_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!async->pdu) diff --git a/drivers/nvme/target/tcp.c b/drivers/nvme/target/tcp.c index 380f22ee3ebb..bea3aa79ef43 100644 --- a/drivers/nvme/target/tcp.c +++ b/drivers/nvme/target/tcp.c @@ -1463,24 +1463,24 @@ static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_queue *queue, c->queue = queue; c->req.port = queue->port->nport; - c->cmd_pdu = page_frag_alloc(&queue->pf_cache, + c->cmd_pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(*c->cmd_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!c->cmd_pdu) return -ENOMEM; c->req.cmd = &c->cmd_pdu->cmd; - c->rsp_pdu = page_frag_alloc(&queue->pf_cache, + c->rsp_pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(*c->rsp_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!c->rsp_pdu) goto out_free_cmd; c->req.cqe = &c->rsp_pdu->cqe; - c->data_pdu = page_frag_alloc(&queue->pf_cache, + c->data_pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(*c->data_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!c->data_pdu) goto out_free_rsp; - c->r2t_pdu = page_frag_alloc(&queue->pf_cache, + c->r2t_pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(*c->r2t_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!c->r2t_pdu) goto out_free_data; @@ -1495,20 +1495,20 @@ static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_queue *queue, return 0; out_free_data: - page_frag_free(c->data_pdu); + page_frag_free_va(c->data_pdu); out_free_rsp: - page_frag_free(c->rsp_pdu); + page_frag_free_va(c->rsp_pdu); out_free_cmd: - page_frag_free(c->cmd_pdu); + page_frag_free_va(c->cmd_pdu); return -ENOMEM; } static void nvmet_tcp_free_cmd(struct nvmet_tcp_cmd *c) { - page_frag_free(c->r2t_pdu); - page_frag_free(c->data_pdu); - page_frag_free(c->rsp_pdu); - page_frag_free(c->cmd_pdu); + page_frag_free_va(c->r2t_pdu); + page_frag_free_va(c->data_pdu); + page_frag_free_va(c->rsp_pdu); + page_frag_free_va(c->cmd_pdu); } static int nvmet_tcp_alloc_cmds(struct nvmet_tcp_queue *queue) diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c index f16279351db5..6691fac01e0d 100644 --- a/drivers/vhost/net.c +++ b/drivers/vhost/net.c @@ -686,8 +686,8 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq, return -ENOSPC; buflen += SKB_DATA_ALIGN(len + pad); - buf = page_frag_alloc_align(&net->pf_cache, buflen, GFP_KERNEL, - SMP_CACHE_BYTES); + buf = page_frag_alloc_va_align(&net->pf_cache, buflen, GFP_KERNEL, + SMP_CACHE_BYTES); if (unlikely(!buf)) return -ENOMEM; @@ -734,7 +734,7 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq, return 0; err: - page_frag_free(buf); + page_frag_free_va(buf); return ret; } diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index b9411f0db25a..c6fde197a6eb 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -25,23 +25,24 @@ struct page_frag_cache { 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_alloc_va_align(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask, + unsigned int align_mask); -static inline void *page_frag_alloc_align(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask, - unsigned int align) +static inline void *page_frag_alloc_va_align(struct page_frag_cache *nc, + unsigned int fragsz, + gfp_t gfp_mask, unsigned int align) { WARN_ON_ONCE(!is_power_of_2(align) || align > PAGE_SIZE); - return __page_frag_alloc_align(nc, fragsz, gfp_mask, -align); + return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, -align); } -static inline void *page_frag_alloc(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask) +static inline void *page_frag_alloc_va(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask) { - return __page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u); + return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, ~0u); } -void page_frag_free(void *addr); +void page_frag_free_va(void *addr); #endif diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index fbff80a03224..f0e50182bf2c 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -3352,7 +3352,7 @@ static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev, static inline void skb_free_frag(void *addr) { - page_frag_free(addr); + page_frag_free_va(addr); } void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask); diff --git a/kernel/bpf/cpumap.c b/kernel/bpf/cpumap.c index a8e34416e960..3a6a237e7dd3 100644 --- a/kernel/bpf/cpumap.c +++ b/kernel/bpf/cpumap.c @@ -322,7 +322,7 @@ static int cpu_map_kthread_run(void *data) /* Bring struct page memory area to curr CPU. Read by * build_skb_around via page_is_pfmemalloc(), and when - * freed written by page_frag_free call. + * freed written by page_frag_free_va call. */ prefetchw(page); } diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index d569021376d4..e5fd4acae003 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -61,9 +61,9 @@ 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_alloc_va_align(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask, + unsigned int align_mask) { unsigned int size = PAGE_SIZE; struct page *page; @@ -128,16 +128,16 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, return nc->va + offset; } -EXPORT_SYMBOL(__page_frag_alloc_align); +EXPORT_SYMBOL(__page_frag_alloc_va_align); /* * Frees a page fragment allocated out of either a compound or order 0 page. */ -void page_frag_free(void *addr) +void page_frag_free_va(void *addr) { struct page *page = virt_to_head_page(addr); if (unlikely(put_page_testzero(page))) free_unref_page(page, compound_order(page)); } -EXPORT_SYMBOL(page_frag_free); +EXPORT_SYMBOL(page_frag_free_va); diff --git a/mm/page_frag_test.c b/mm/page_frag_test.c index 1349f6c6b521..c3cfce87fbbf 100644 --- a/mm/page_frag_test.c +++ b/mm/page_frag_test.c @@ -277,7 +277,7 @@ static int page_frag_pop_thread(void *arg) if (obj) { nr--; - page_frag_free(obj); + page_frag_free_va(obj); } else { cond_resched(); } @@ -305,17 +305,20 @@ static int page_frag_push_thread(void *arg) int ret; if (test_align) - va = page_frag_alloc_align(&test_frag, test_alloc_len, - GFP_KERNEL, SMP_CACHE_BYTES); + va = page_frag_alloc_va_align(&test_frag, + test_alloc_len, + GFP_KERNEL, + SMP_CACHE_BYTES); else - va = page_frag_alloc(&test_frag, test_alloc_len, GFP_KERNEL); + va = page_frag_alloc_va(&test_frag, test_alloc_len, + GFP_KERNEL); if (!va) continue; ret = objpool_push(va, pool); if (ret) { - page_frag_free(va); + page_frag_free_va(va); cond_resched(); } else { nr--; diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 466999a7515e..dca4e7445348 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -309,8 +309,8 @@ void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) fragsz = SKB_DATA_ALIGN(fragsz); - return __page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, - align_mask); + return __page_frag_alloc_va_align(&nc->page, fragsz, GFP_ATOMIC, + align_mask); } EXPORT_SYMBOL(__napi_alloc_frag_align); @@ -322,15 +322,15 @@ void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) if (in_hardirq() || irqs_disabled()) { struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache); - data = __page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, - align_mask); + data = __page_frag_alloc_va_align(nc, fragsz, GFP_ATOMIC, + align_mask); } else { struct napi_alloc_cache *nc; local_bh_disable(); nc = this_cpu_ptr(&napi_alloc_cache); - data = __page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, - align_mask); + data = __page_frag_alloc_va_align(&nc->page, fragsz, GFP_ATOMIC, + align_mask); local_bh_enable(); } return data; @@ -740,12 +740,12 @@ struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, if (in_hardirq() || irqs_disabled()) { nc = this_cpu_ptr(&netdev_alloc_cache); - data = page_frag_alloc(nc, len, gfp_mask); + data = page_frag_alloc_va(nc, len, gfp_mask); pfmemalloc = nc->pfmemalloc; } else { local_bh_disable(); nc = this_cpu_ptr(&napi_alloc_cache.page); - data = page_frag_alloc(nc, len, gfp_mask); + data = page_frag_alloc_va(nc, len, gfp_mask); pfmemalloc = nc->pfmemalloc; local_bh_enable(); } @@ -833,7 +833,7 @@ struct sk_buff *napi_alloc_skb(struct napi_struct *napi, unsigned int len) } else { len = SKB_HEAD_ALIGN(len); - data = page_frag_alloc(&nc->page, len, gfp_mask); + data = page_frag_alloc_va(&nc->page, len, gfp_mask); pfmemalloc = nc->page.pfmemalloc; } diff --git a/net/core/xdp.c b/net/core/xdp.c index 41693154e426..245a2d011aeb 100644 --- a/net/core/xdp.c +++ b/net/core/xdp.c @@ -391,7 +391,7 @@ void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct, page_pool_put_full_page(page->pp, page, napi_direct); break; case MEM_TYPE_PAGE_SHARED: - page_frag_free(data); + page_frag_free_va(data); break; case MEM_TYPE_PAGE_ORDER0: page = virt_to_page(data); /* Assumes order0 page*/ diff --git a/net/rxrpc/txbuf.c b/net/rxrpc/txbuf.c index c3913d8a50d3..dccb0353ee84 100644 --- a/net/rxrpc/txbuf.c +++ b/net/rxrpc/txbuf.c @@ -33,8 +33,8 @@ struct rxrpc_txbuf *rxrpc_alloc_data_txbuf(struct rxrpc_call *call, size_t data_ data_align = umax(data_align, L1_CACHE_BYTES); mutex_lock(&call->conn->tx_data_alloc_lock); - buf = page_frag_alloc_align(&call->conn->tx_data_alloc, total, gfp, - data_align); + buf = page_frag_alloc_va_align(&call->conn->tx_data_alloc, total, gfp, + data_align); mutex_unlock(&call->conn->tx_data_alloc_lock); if (!buf) { kfree(txb); @@ -96,17 +96,18 @@ struct rxrpc_txbuf *rxrpc_alloc_ack_txbuf(struct rxrpc_call *call, size_t sack_s if (!txb) return NULL; - buf = page_frag_alloc(&call->local->tx_alloc, - sizeof(*whdr) + sizeof(*ack) + 1 + 3 + sizeof(*trailer), gfp); + buf = page_frag_alloc_va(&call->local->tx_alloc, + sizeof(*whdr) + sizeof(*ack) + 1 + 3 + sizeof(*trailer), gfp); if (!buf) { kfree(txb); return NULL; } if (sack_size) { - buf2 = page_frag_alloc(&call->local->tx_alloc, sack_size, gfp); + buf2 = page_frag_alloc_va(&call->local->tx_alloc, sack_size, + gfp); if (!buf2) { - page_frag_free(buf); + page_frag_free_va(buf); kfree(txb); return NULL; } @@ -180,7 +181,7 @@ static void rxrpc_free_txbuf(struct rxrpc_txbuf *txb) rxrpc_txbuf_free); for (i = 0; i < txb->nr_kvec; i++) if (txb->kvec[i].iov_base) - page_frag_free(txb->kvec[i].iov_base); + page_frag_free_va(txb->kvec[i].iov_base); kfree(txb); atomic_dec(&rxrpc_nr_txbuf); } diff --git a/net/sunrpc/svcsock.c b/net/sunrpc/svcsock.c index 6b3f01beb294..42d20412c1c3 100644 --- a/net/sunrpc/svcsock.c +++ b/net/sunrpc/svcsock.c @@ -1222,8 +1222,8 @@ static int svc_tcp_sendmsg(struct svc_sock *svsk, struct svc_rqst *rqstp, /* The stream record marker is copied into a temporary page * fragment buffer so that it can be included in rq_bvec. */ - buf = page_frag_alloc(&svsk->sk_frag_cache, sizeof(marker), - GFP_KERNEL); + buf = page_frag_alloc_va(&svsk->sk_frag_cache, sizeof(marker), + GFP_KERNEL); if (!buf) return -ENOMEM; memcpy(buf, &marker, sizeof(marker)); @@ -1235,7 +1235,7 @@ static int svc_tcp_sendmsg(struct svc_sock *svsk, struct svc_rqst *rqstp, iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, rqstp->rq_bvec, 1 + count, sizeof(marker) + rqstp->rq_res.len); ret = sock_sendmsg(svsk->sk_sock, &msg); - page_frag_free(buf); + page_frag_free_va(buf); if (ret < 0) return ret; *sentp += ret; From patchwork Wed Jun 5 13:32:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13686905 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 8934CC27C5F for ; Wed, 5 Jun 2024 13:36:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 149356B0098; Wed, 5 Jun 2024 09:36:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0DEDB6B0099; Wed, 5 Jun 2024 09:36:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EB6726B009A; Wed, 5 Jun 2024 09:36:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id CC9CC6B0098 for ; Wed, 5 Jun 2024 09:36:25 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 908A7A11AA for ; Wed, 5 Jun 2024 13:36:25 +0000 (UTC) X-FDA: 82196934330.08.7E904F1 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf05.hostedemail.com (Postfix) with ESMTP id 11749100021 for ; Wed, 5 Jun 2024 13:36:22 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=none; spf=pass (imf05.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.189 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=1717594583; 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=UWDEFcVWt3PUwT1zHgBlwDTWAF6g0ZroxpvhPQP4WAc=; b=yF1xh82TCDvv5XweeuOcCe5GSB2GZh0rQEGHjT/FozclMfekDUg/xIkuoAvO8O33BK7GCB xjM1PrUq2Wx9xFEWhCTJkbJ60ZsmslLuBCj3FmEFuhHMkMh8Gbe7n4MmP82v+dqC68bU5L SPxGnIiHZ1zgpaxmI/zwZKlmiOBWI8s= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717594583; a=rsa-sha256; cv=none; b=ph89DU2DrJaIxAyo00X71D2fpo1bGtLNjEkJw/eMvvJyHaHc3ggHVLxYLww0ofYK88Teyj OX4fuRpes2AMD4qOEDaVaaT7kU/hLfnH7u/o4V/WXE/My/O3rs0lgnJPOgy9lP9ubPJv3u wYuc2q5FkNo2Q0gzPvYSawf9LbPsvtg= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; spf=pass (imf05.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4VvT194Sh5zPnsm; Wed, 5 Jun 2024 21:33:01 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 26CD618007A; Wed, 5 Jun 2024 21:36:19 +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.39; Wed, 5 Jun 2024 21:36:18 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Andrew Morton , Eric Dumazet , David Howells , Marc Dionne , Chuck Lever , Jeff Layton , Neil Brown , Olga Kornievskaia , Dai Ngo , Tom Talpey , Trond Myklebust , Anna Schumaker , , , , , Subject: [PATCH net-next v6 07/15] mm: page_frag: avoid caller accessing 'page_frag_cache' directly Date: Wed, 5 Jun 2024 21:32:57 +0800 Message-ID: <20240605133306.11272-8-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240605133306.11272-1-linyunsheng@huawei.com> References: <20240605133306.11272-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 11749100021 X-Rspam-User: X-Stat-Signature: sdjcqqwhszxsjz9oat4duyisc6trbg4j X-HE-Tag: 1717594582-570813 X-HE-Meta: U2FsdGVkX1925y3dXw8XfCf5ehR/l3F4GPZttk4+57Ec0zHLJc4loqD+vB+Bl/moyNcAkC0+/5VHqPA0V0q6psq8wRUX38ul12+RYnm7oZ5NosogOpX4jkcwD4YBiXwW5vvF98pmwuV00PjM4qVTfiE/JD3OUMTWsyFU4xJDeM1P0Fo/tFvJbNRndTeaI+vqHG48A3x1fckg4XqjjPYVP20QBb5yq2o0ZzWVAiMWiLmptRuRj+wcMckS6finKAGlFgYyqD/PIG2Wlg3ic/6buH4fzcKAnZlHDssdXNmi9HiU8yx5EVVz3uB2Q/lNb1PIqP9Ax7gA+ULLzwaUF4HpUvelpqgWGqpfPAnLKpr3FIao1B+Jb76ewCx+wn+Fg64RmDRC4ZR/ThsZgVi82aX+puHk2eVhDIrs7lf28sYuiB7bb0Zy2cbLrP9eWNpMrHwohzDbZiX/qTT6gAxeI1o5oS44oj3w4zPq7iC5XLMntkKjALP8BY5nixFHPfMXjljxg2mvShqnYluLV2MDBQ8NUyeLewUUDYgYmows4pdK0x7LOyTEhR6qT5Ks9QcerkgeymvyZiGTg5vvAOSeNWwqM0wC8UcAshHfoqfdY1iLVrwLwLN+6tC1deOhIOndqG5kBI9sNvgwqNOjrUo31bJZOT7MbxyScDiyrTYKeLdvfJvZB/d4haAHCgZWxTHX2lHjSPPpT12Zt7ODGPMLNEx1j3Az0BPI+sdvYK4ep39etsBqH5J8G0YR6MHbSHSQjkLIGqwJzYDGLbwp7pwZN+lsHGd6Li11uPtCoc/0cdwKdM7x5vuNz0wcOv3XAj1pZDjRcesQGz1USn6frgFfjatzV3G6grZEpuWnDHKYaorRI9vEAJArkKgugoL5/vZk/c1kyMhNuz5PcIb9xoKRSWhXt+I0y0cy/QdxgIFuai52VMfzRmRlLptMuvlfTpB5QqQWhscPqQ3Dy8YlYogzZjR De+PZfJe xPA5Z9kPte38ESx0Rvf0IPZ5C8DmQmRmdREQQho0RxDeAUO1JHnSA+lk+w70cjJ2G1+8hot72nTcuC5Yo9tJq7tSQbd0AzPAwGZXVsu6keDQ+x+28KuncFCcVbFyfKv8ea0A87p3pOW0wmjzZTdW/YV4uR2sAr8JScEtd/Z5MpCIr860TwOsIBkdu6Ml8oOpfcw3A8X7+HSAk08oiwciSBzks3wt6Zt8jNCBm+WHO3JDEZJ6zbFReWpubo/09xLdyHQ1t3fQblg8+0xuF9FQM0p2kHk7Lvy230Ozab2FoRyRi6ao= 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: Use appropriate frag_page API instead of caller accessing 'page_frag_cache' directly. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- drivers/vhost/net.c | 2 +- include/linux/page_frag_cache.h | 10 ++++++++++ mm/page_frag_test.c | 2 +- net/core/skbuff.c | 6 +++--- net/rxrpc/conn_object.c | 4 +--- net/rxrpc/local_object.c | 4 +--- net/sunrpc/svcsock.c | 6 ++---- 7 files changed, 19 insertions(+), 15 deletions(-) diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c index 6691fac01e0d..b2737dc0dc50 100644 --- a/drivers/vhost/net.c +++ b/drivers/vhost/net.c @@ -1325,7 +1325,7 @@ static int vhost_net_open(struct inode *inode, struct file *f) vqs[VHOST_NET_VQ_RX]); f->private_data = n; - n->pf_cache.va = NULL; + page_frag_cache_init(&n->pf_cache); return 0; } diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index c6fde197a6eb..6ac3a25089d1 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -23,6 +23,16 @@ struct page_frag_cache { bool pfmemalloc; }; +static inline void page_frag_cache_init(struct page_frag_cache *nc) +{ + nc->va = NULL; +} + +static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc) +{ + return !!nc->pfmemalloc; +} + 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_va_align(struct page_frag_cache *nc, diff --git a/mm/page_frag_test.c b/mm/page_frag_test.c index c3cfce87fbbf..8b259e422fae 100644 --- a/mm/page_frag_test.c +++ b/mm/page_frag_test.c @@ -341,7 +341,7 @@ static int __init page_frag_test_init(void) u64 duration; int ret; - test_frag.va = NULL; + page_frag_cache_init(&test_frag); atomic_set(&nthreads, 2); init_completion(&wait); diff --git a/net/core/skbuff.c b/net/core/skbuff.c index dca4e7445348..caee22db1cc7 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -741,12 +741,12 @@ struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, if (in_hardirq() || irqs_disabled()) { nc = this_cpu_ptr(&netdev_alloc_cache); data = page_frag_alloc_va(nc, len, gfp_mask); - pfmemalloc = nc->pfmemalloc; + pfmemalloc = page_frag_cache_is_pfmemalloc(nc); } else { local_bh_disable(); nc = this_cpu_ptr(&napi_alloc_cache.page); data = page_frag_alloc_va(nc, len, gfp_mask); - pfmemalloc = nc->pfmemalloc; + pfmemalloc = page_frag_cache_is_pfmemalloc(nc); local_bh_enable(); } @@ -834,7 +834,7 @@ struct sk_buff *napi_alloc_skb(struct napi_struct *napi, unsigned int len) len = SKB_HEAD_ALIGN(len); data = page_frag_alloc_va(&nc->page, len, gfp_mask); - pfmemalloc = nc->page.pfmemalloc; + pfmemalloc = page_frag_cache_is_pfmemalloc(&nc->page); } if (unlikely(!data)) diff --git a/net/rxrpc/conn_object.c b/net/rxrpc/conn_object.c index 1539d315afe7..694c4df7a1a3 100644 --- a/net/rxrpc/conn_object.c +++ b/net/rxrpc/conn_object.c @@ -337,9 +337,7 @@ static void rxrpc_clean_up_connection(struct work_struct *work) */ rxrpc_purge_queue(&conn->rx_queue); - if (conn->tx_data_alloc.va) - __page_frag_cache_drain(virt_to_page(conn->tx_data_alloc.va), - conn->tx_data_alloc.pagecnt_bias); + page_frag_cache_drain(&conn->tx_data_alloc); call_rcu(&conn->rcu, rxrpc_rcu_free_connection); } diff --git a/net/rxrpc/local_object.c b/net/rxrpc/local_object.c index 504453c688d7..a8cffe47cf01 100644 --- a/net/rxrpc/local_object.c +++ b/net/rxrpc/local_object.c @@ -452,9 +452,7 @@ void rxrpc_destroy_local(struct rxrpc_local *local) #endif rxrpc_purge_queue(&local->rx_queue); rxrpc_purge_client_connections(local); - if (local->tx_alloc.va) - __page_frag_cache_drain(virt_to_page(local->tx_alloc.va), - local->tx_alloc.pagecnt_bias); + page_frag_cache_drain(&local->tx_alloc); } /* diff --git a/net/sunrpc/svcsock.c b/net/sunrpc/svcsock.c index 42d20412c1c3..4b1e87187614 100644 --- a/net/sunrpc/svcsock.c +++ b/net/sunrpc/svcsock.c @@ -1609,7 +1609,6 @@ static void svc_tcp_sock_detach(struct svc_xprt *xprt) static void svc_sock_free(struct svc_xprt *xprt) { struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); - struct page_frag_cache *pfc = &svsk->sk_frag_cache; struct socket *sock = svsk->sk_sock; trace_svcsock_free(svsk, sock); @@ -1619,8 +1618,7 @@ static void svc_sock_free(struct svc_xprt *xprt) sockfd_put(sock); else sock_release(sock); - if (pfc->va) - __page_frag_cache_drain(virt_to_head_page(pfc->va), - pfc->pagecnt_bias); + + page_frag_cache_drain(&svsk->sk_frag_cache); kfree(svsk); } From patchwork Wed Jun 5 13:32:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13686906 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 63855C27C6D for ; Wed, 5 Jun 2024 13:36:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 36CA76B0099; Wed, 5 Jun 2024 09:36:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2F5616B009A; Wed, 5 Jun 2024 09:36:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1BDC16B009B; Wed, 5 Jun 2024 09:36:27 -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 EA8AB6B0099 for ; Wed, 5 Jun 2024 09:36:26 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 70C7E40A15 for ; Wed, 5 Jun 2024 13:36:26 +0000 (UTC) X-FDA: 82196934372.02.22C39F4 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf06.hostedemail.com (Postfix) with ESMTP id 01E95180004 for ; Wed, 5 Jun 2024 13:36:23 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=none; spf=pass (imf06.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 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=1717594584; a=rsa-sha256; cv=none; b=2MrZ7zAu5k8SED2R7BbZKQYiYA0iaG5RJFSoUr9ObcSN42wf5ZO0BsCOO1sirrE7I8rwAh RjWAn8fIVSPbLhMTnl176C2dQRBNuBgLjDSTSlXsYOml8GkQG4HUTSX9HaiPktinKgMg2e O0+oOQYaeO5uDooI5ICphlT1mvSs2k4= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=none; spf=pass (imf06.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 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=1717594584; 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=dZHlde2qBhEfHKyQSRX7IyQL22W1SJACtNT8HVmYrhM=; b=AcRvjSqmA6Tt6f56BApbTqgY8qv12YA15dzQRAc/34M9WLTkwiTmjm/DTHpY8OB88BXKvp q3yoo6Bmhr+9ydXoc5dE7vLvUBG03S+8Dja6bxGQC43dfkIo7SnSnOYLmByL2o706y15eT OaHx8MqESiBfm7P0+lp/SS7tcq0d+ic= Received: from mail.maildlp.com (unknown [172.19.88.194]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VvT0S1b8zzwRqm; Wed, 5 Jun 2024 21:32:24 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 07CB214037F; Wed, 5 Jun 2024 21:36:21 +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.39; Wed, 5 Jun 2024 21:36:20 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v6 08/15] mm: page_frag: reuse existing space for 'size' and 'pfmemalloc' Date: Wed, 5 Jun 2024 21:32:58 +0800 Message-ID: <20240605133306.11272-9-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240605133306.11272-1-linyunsheng@huawei.com> References: <20240605133306.11272-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 01E95180004 X-Stat-Signature: e5dd4p47kxctkqcsj8troh6kuaxgxqn8 X-HE-Tag: 1717594583-814138 X-HE-Meta: U2FsdGVkX1+cVsV4V2hGlZdfKbJ6IZyQehcaNUbaIbZtkmQ0xsEIczfHA/EaRYI02fCOXUJS8IMwuX4CNcd9vD8Yxp6le2ysZqR1Ax95oiNPAhn+8S2Hhg2Y+hcDoiuFa3vUo8CwB0UA0GJYoabert7iNh51wRMGA919qqii1ZqkoYAkKrpDOmvlbQWNP6C0WZICwBHGC1eNz/IAVbhLrxJGS5maEOO14hmTHOOhqXhM6Zhsn7fD1ngCKimL34Yfb2x2WS1R5Na93xndbBqDHhFeNBY4BaUduEmLwZIUJEYW0zbD+57RzyQZz/lKe57hLwgso3nEf8VhLfmFqDB8h/a6yw6mDs3u0kw6pGL5jFhBsahzpTVqMsHgMqhfZG4y2Z95umbrarP4469mGNUo7pE/WV0l1kG/S4+HRwiNxMOqgI/w0YVckfFEphOQ/UyibCwvn2jNivFZx/ypKuuOjMaM28doidHyHt2XGPE2HM4bUrokvLiobyL1eqqhU+d3onO6k1NfXQwnnPwzxNgqQ7BHwwWUSBZdPqo3weoZVXs7toeevdJG21kRj3ihxWM+NpW6ESc9kqpFq80/4fhAwsCke3HFL/Oi49sRHGX0/B9JPezAWnbb1z2nEkaXVau6VrsUX2uSHwBpsNUgdDHUHAYyM4AG5hco7l6Dg6uDjJMXolTMHudkc44L/OVHunEEjL3sKjrJUgsk2EaHVfrLFKloLXYRHzESPOwVVd76xiW2QNV+/2dKtK3BZ6oDFx98Wpq2Nx+QAGCNfnRtTSU8QUcW1o5p+yj+FQK57Ky18Zy4qyNnx+kSwKP8ELBd5+otQH/p+/5eM2SEmrv8y30W29Bjr18PzKkfCr4jdOcIwywfc6nDDrDky5xvwNcfatEuItEBqz8OuE8RbbqH/RsmejLZeGjuTzmdZpkqBf4ciU9rSr5ZPnN4ApHwU15f7dFzZYCk2X4jfF3QtGBIrjj KRIBISyn TYkaDwqr0HNcWu0E6bn15PZd3dpimMInPhPxRDQTJV7bb5wNI0xxm2w8fBfdsEmTA4l/PlO1i60zG4O/LqnzSbhUvFyHyWorGv3aZpAM1c2D1Woniu9EPSP2pgp6VRluf5+IrbmpWujVHVId1Ef0WxmSENqSFB+c7JmFr7n/6ov/HiXzg0p63xOCVK9b9eLGOu4iEJnB4A/VxUZCi+weKkWOEkCEIK2sg4XLUCwI5pJkt42RT45A72zMyHDIIObvxyrlYUcd6LJ+UAdWoiWcHiMPTrCGWt8q7uJB0 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: Currently there is one 'struct page_frag' for every 'struct sock' and 'struct task_struct', we are about to replace the 'struct page_frag' with 'struct page_frag_cache' for them. Before begin the replacing, we need to ensure the size of 'struct page_frag_cache' is not bigger than the size of 'struct page_frag', as there may be tens of thousands of 'struct sock' and 'struct task_struct' instances in the system. By or'ing the page order & pfmemalloc with lower bits of 'va' instead of using 'u16' or 'u32' for page size and 'u8' for pfmemalloc, we are able to avoid 3 or 5 bytes space waste. And page address & pfmemalloc & order is unchanged for the same page in the same 'page_frag_cache' instance, it makes sense to fit them together. Also, it is better to replace 'offset' with 'remaining', which is the remaining size for the cache in a 'page_frag_cache' instance, we are able to do a single 'fragsz > remaining' checking for the case of cache not being enough, which should be the fast path if we ensure size is zoro when 'va' == NULL by memset'ing 'struct page_frag_cache' in page_frag_cache_init() and page_frag_cache_drain(). After this patch, the size of 'struct page_frag_cache' should be the same as the size of 'struct page_frag'. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 76 +++++++++++++++++++++++----- mm/page_frag_cache.c | 90 ++++++++++++++++++++------------- 2 files changed, 118 insertions(+), 48 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 6ac3a25089d1..b33904d4494f 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -8,29 +8,81 @@ #define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK) #define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE) -struct page_frag_cache { - void *va; +/* + * struct encoded_va - a nonexistent type marking this pointer + * + * An 'encoded_va' pointer is a pointer to a aligned virtual address, which is + * at least aligned to PAGE_SIZE, that means there are at least 12 lower bits + * space available for other purposes. + * + * Currently we use the lower 8 bits and bit 9 for the order and PFMEMALLOC + * flag of the page this 'va' is corresponding to. + * + * Use the supplied helper functions to endcode/decode the pointer and bits. + */ +struct encoded_va; + +#define PAGE_FRAG_CACHE_ORDER_MASK GENMASK(7, 0) +#define PAGE_FRAG_CACHE_PFMEMALLOC_BIT BIT(8) +#define PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT 8 + +static inline struct encoded_va *encode_aligned_va(void *va, + unsigned int order, + bool pfmemalloc) +{ #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - __u16 offset; - __u16 size; + return (struct encoded_va *)((unsigned long)va | order | + pfmemalloc << PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT); #else - __u32 offset; + return (struct encoded_va *)((unsigned long)va | + pfmemalloc << PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT); +#endif +} + +static inline unsigned long encoded_page_order(struct encoded_va *encoded_va) +{ +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + return PAGE_FRAG_CACHE_ORDER_MASK & (unsigned long)encoded_va; +#else + return 0; +#endif +} + +static inline bool encoded_page_pfmemalloc(struct encoded_va *encoded_va) +{ + return PAGE_FRAG_CACHE_PFMEMALLOC_BIT & (unsigned long)encoded_va; +} + +static inline void *encoded_page_address(struct encoded_va *encoded_va) +{ + return (void *)((unsigned long)encoded_va & PAGE_MASK); +} + +struct page_frag_cache { + struct encoded_va *encoded_va; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) && (BITS_PER_LONG <= 32) + u16 pagecnt_bias; + u16 remaining; +#else + u32 pagecnt_bias; + u32 remaining; #endif - /* we maintain a pagecount bias, so that we dont dirty cache line - * containing page->_refcount every time we allocate a fragment. - */ - unsigned int pagecnt_bias; - bool pfmemalloc; }; static inline void page_frag_cache_init(struct page_frag_cache *nc) { - nc->va = NULL; + memset(nc, 0, sizeof(*nc)); } static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc) { - return !!nc->pfmemalloc; + return encoded_page_pfmemalloc(nc->encoded_va); +} + +static inline unsigned int page_frag_cache_page_size(struct encoded_va *encoded_va) +{ + return PAGE_SIZE << encoded_page_order(encoded_va); } void page_frag_cache_drain(struct page_frag_cache *nc); diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index e5fd4acae003..756ef68cce21 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -18,11 +18,20 @@ #include #include "internal.h" +static void *page_frag_cache_current_va(struct page_frag_cache *nc) +{ + struct encoded_va *encoded_va = nc->encoded_va; + + return (void *)(((unsigned long)encoded_va & PAGE_MASK) | + (page_frag_cache_page_size(encoded_va) - nc->remaining)); +} + static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, gfp_t gfp_mask) { struct page *page = NULL; gfp_t gfp = gfp_mask; + unsigned int order; #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) /* Ensure free_unref_page() can be used to free the page fragment */ @@ -32,23 +41,41 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, PAGE_FRAG_CACHE_MAX_ORDER); - nc->size = page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; #endif - if (unlikely(!page)) + if (unlikely(!page)) { page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); + if (unlikely(!page)) { + memset(nc, 0, sizeof(*nc)); + return NULL; + } + + order = 0; + nc->remaining = PAGE_SIZE; + } else { + order = PAGE_FRAG_CACHE_MAX_ORDER; + nc->remaining = PAGE_FRAG_CACHE_MAX_SIZE; + } - nc->va = page ? page_address(page) : NULL; + /* Even if we own the page, we do not use atomic_set(). + * This would break get_page_unless_zero() users. + */ + page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); + /* reset page count bias of new frag */ + nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; + nc->encoded_va = encode_aligned_va(page_address(page), order, + page_is_pfmemalloc(page)); return page; } void page_frag_cache_drain(struct page_frag_cache *nc) { - if (!nc->va) + if (!nc->encoded_va) return; - __page_frag_cache_drain(virt_to_head_page(nc->va), nc->pagecnt_bias); - nc->va = NULL; + __page_frag_cache_drain(virt_to_head_page(nc->encoded_va), + nc->pagecnt_bias); + memset(nc, 0, sizeof(*nc)); } EXPORT_SYMBOL(page_frag_cache_drain); @@ -65,51 +92,41 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) { - unsigned int size = PAGE_SIZE; + struct encoded_va *encoded_va = nc->encoded_va; struct page *page; - int offset; + int remaining; + void *va; - if (unlikely(!nc->va)) { + if (unlikely(!encoded_va)) { refill: - page = __page_frag_cache_refill(nc, gfp_mask); - if (!page) + if (unlikely(!__page_frag_cache_refill(nc, gfp_mask))) return NULL; - /* Even if we own the page, we do not use atomic_set(). - * This would break get_page_unless_zero() users. - */ - page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); - - /* reset page count bias and offset to start of new frag */ - nc->pfmemalloc = page_is_pfmemalloc(page); - nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset = 0; + encoded_va = nc->encoded_va; } -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif - - offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); - if (unlikely(offset + fragsz > size)) { - page = virt_to_page(nc->va); - + remaining = nc->remaining & align_mask; + remaining -= fragsz; + if (unlikely(remaining < 0)) { + page = virt_to_page(encoded_va); if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) goto refill; - if (unlikely(nc->pfmemalloc)) { - free_unref_page(page, compound_order(page)); + 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)); goto refill; } /* 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 offset to start of new frag */ + /* reset page count bias and remaining of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset = 0; - if (unlikely(fragsz > PAGE_SIZE)) { + nc->remaining = remaining = page_frag_cache_page_size(encoded_va); + remaining -= fragsz; + if (unlikely(remaining < 0)) { /* * The caller is trying to allocate a fragment * with fragsz > PAGE_SIZE but the cache isn't big @@ -123,10 +140,11 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, } } + va = page_frag_cache_current_va(nc); nc->pagecnt_bias--; - nc->offset = offset + fragsz; + nc->remaining = remaining; - return nc->va + offset; + return va; } EXPORT_SYMBOL(__page_frag_alloc_va_align); From patchwork Wed Jun 5 13:32:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13686907 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 028E0C27C53 for ; Wed, 5 Jun 2024 13:36:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5409D6B009A; Wed, 5 Jun 2024 09:36:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4F20A6B009C; Wed, 5 Jun 2024 09:36:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 36CF06B009D; Wed, 5 Jun 2024 09:36:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 067AA6B009A for ; Wed, 5 Jun 2024 09:36:28 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id B7AA11C253B for ; Wed, 5 Jun 2024 13:36:27 +0000 (UTC) X-FDA: 82196934414.28.C9C03C2 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf14.hostedemail.com (Postfix) with ESMTP id 539FC100010 for ; Wed, 5 Jun 2024 13:36:24 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf14.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.189 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=1717594586; 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=1pdm9htmyggMxEWZN2tsXxEoWBHZYMVStyWtOzDyjJw=; b=jhuEw0lacMeYh2lZ0W6BYUjkBClmImOOnxiQrXQn5WIRQVgQGP/ivLefyAs95hbYOy816z dPoDIrcbSagpAZ5HrReI8itKwIbCrFQzIfCklU5FEFzPdVt8ZG9g0yIl7rN8hcoOk+z5H4 9tKvXy7ezNbNVRU7Sl9q24LDYvuFKr0= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf14.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717594586; a=rsa-sha256; cv=none; b=Bd6hc/eoTE3KgupR6nwUihL2EpzGgL9836MTE3tznTTLsgUS8Rsy+tRlk2+rd4Sa5U8iYj g36F+ucnGbn/cKljJRF9jlLfwOq2e3fYQr7jOAY2MKRl56oCSj++71tNeCn2AGF4TJVBm7 I5HunINeY0c4ISJTu15XJhDwlH+Nh2g= Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4VvT1F1JlYzPpHw; Wed, 5 Jun 2024 21:33:05 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id B480518007A; Wed, 5 Jun 2024 21:36:22 +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.39; Wed, 5 Jun 2024 21:36:22 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v6 09/15] mm: page_frag: some minor refactoring before adding new API Date: Wed, 5 Jun 2024 21:32:59 +0800 Message-ID: <20240605133306.11272-10-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240605133306.11272-1-linyunsheng@huawei.com> References: <20240605133306.11272-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 539FC100010 X-Stat-Signature: oiqsncsrkgsqteuuttgdmbpofyyuupyc X-Rspam-User: X-HE-Tag: 1717594584-951520 X-HE-Meta: U2FsdGVkX1/j9xbSd6LE3fjrF/KmpVS8nUdAzVNuS3s6so+iwrnEsn5Bzh1+ZIdViEDH7z7vFwXJaB2xvkxnBvSYLR4ZP8Uge9sK8FltT5cRmGMKAftrtTa3vXXEC7q4yh2cQgPgzHLfhLt9qEqz8izK4BVqYraxx5zMCDdF3/1GdVoN6T3FGd/yspCz9ariOEW1XIxVxhQatPQFqH4HHkciVkbQ8DbsBa8E47Gw33kO8RLwueoTmbuPFTzf8thsWh/6izTals+6m5Ws4RZmIL2cuExHY7dAnYUiMU7KCpLDtCrmXpr+buoOjhjJGsWUeb6L+KlosVumaLdgcQgxJPZlLnI8RiYN3XPg/QzBIdR+TreB4v+NhnvnXqNCwP6dj9YttbW4GJz5h82HQV8NyZHDRtAGfuQEFW1StmoCUoucMFaBUarTp9B1wdoU8sBYezvC/UMDOPg3vIQPpO95lm3v5pIBcJDJhMnAK5a0LCay/bTqbEFtnAddBAB78g6wxAon6u/zpIyiwQdHf6e6QlmbcFSbJCapTVHSiaKz67lRZbI/cPf3ahRiCeFf6b6SuQfuKDT5Br8xxbcVMQ1nSorvotvYpiG15yLtSrtoYLo7PJ4qjT2FMgAqHekbwBLPbvq/2YMjYlmhTim+zZA9m4AWFelDz3gajeCLqA4ZzhZCmOTKM8V+d1XEv3nzV1Q0UITPZEQCMU1G+cGQuKozOC0X5/ZhLZ+s0jFb77x3CvC8gJtzpffAv6Xhm3xDZ/cQV33uyoyPnOd0ZjgNXrLhTjLhXltzbkfLCKnhIAtCgIjeDqslp8t7WAd6C9VD8Unoclm6/E9aDpZwapH3gZA4iwtmloNBTNzBsOkDqCoc/gx0x1iMpMEnhrOWnn/ZUeAee/IxrSsqaOJ/jKoZ2thUDZiX7yW/+7s19+sYC+179+8MuU+EybTb3xnWFafUp9L96cpWHQrVJHfn4QToURq iXqB5DzT LEPjmYjbKHUjuNfJhje8eza7AsGvgyLqZM/Bg4NdmFb+3aJPR4BT3lO8DQbBy6Bs9062vmMsSzcaBr+vZrg4l8/Fh7vcOY4kh3m1b9bQA9V1yQp+PAWJZLLibqeHf0wCqpFCFl0jMLMESq2jnniI3E7rFIu392zVDk79inRDWEgBhLxcybEcIRYIrZNk9/NNR+h9iLVIFr1eMVGnhPrLazG9SEM37IdnZT32/2LZaBaMd9rUGERtt3HjbmHvY3OHNK45dCzdBJcprIY4NgY/7U6LTvxOReKW4y3x6 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_refill(), so that the new API can make use of them. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- mm/page_frag_cache.c | 61 ++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 756ef68cce21..b5ad6e9d316d 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -29,10 +29,36 @@ static void *page_frag_cache_current_va(struct page_frag_cache *nc) static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, gfp_t gfp_mask) { - struct page *page = NULL; + struct encoded_va *encoded_va = nc->encoded_va; gfp_t gfp = gfp_mask; unsigned int order; + struct page *page; + + if (unlikely(!encoded_va)) + goto alloc; + + page = virt_to_page(encoded_va); + if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) + goto alloc; + + 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)); + goto alloc; + } + + /* 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; +alloc: + page = NULL; #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) /* Ensure free_unref_page() can be used to free the page fragment */ BUILD_BUG_ON(PAGE_FRAG_CACHE_MAX_ORDER > PAGE_ALLOC_COSTLY_ORDER); @@ -92,40 +118,15 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) { - struct encoded_va *encoded_va = nc->encoded_va; - struct page *page; - int remaining; + int remaining = nc->remaining & align_mask; void *va; - if (unlikely(!encoded_va)) { -refill: - if (unlikely(!__page_frag_cache_refill(nc, gfp_mask))) - return NULL; - - encoded_va = nc->encoded_va; - } - - remaining = nc->remaining & align_mask; remaining -= fragsz; if (unlikely(remaining < 0)) { - page = virt_to_page(encoded_va); - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) - goto refill; - - 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)); - goto refill; - } - - /* OK, page count is 0, we can safely set it */ - set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); + if (unlikely(!__page_frag_cache_refill(nc, gfp_mask))) + return NULL; - /* reset page count bias and remaining of new frag */ - nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->remaining = remaining = page_frag_cache_page_size(encoded_va); - remaining -= fragsz; + remaining = nc->remaining - fragsz; if (unlikely(remaining < 0)) { /* * The caller is trying to allocate a fragment From patchwork Wed Jun 5 13:33: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: 13686934 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 3A7B0C25B76 for ; Wed, 5 Jun 2024 13:36:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5DE226B009D; Wed, 5 Jun 2024 09:36:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 540886B009E; Wed, 5 Jun 2024 09:36:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3432A6B009F; Wed, 5 Jun 2024 09:36:32 -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 17A476B009D for ; Wed, 5 Jun 2024 09:36:32 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id D384A1A0BEF for ; Wed, 5 Jun 2024 13:36:31 +0000 (UTC) X-FDA: 82196934582.18.7215CAC Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf25.hostedemail.com (Postfix) with ESMTP id 58981A0004 for ; Wed, 5 Jun 2024 13:36:28 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf25.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.191 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=1717594589; 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=Q5lygQpHsNfD53k8GJVdoMYQB6GAVLoj7xg8lfwJgtQ=; b=8cEdB7ee8zfoVEUxc12dX0JXWri/LFXYpnPop/xtVaTcnUl6OARSM+PlE7TM4bjCESY71e 9PX0LDHzYFKbaeQhrIzPPZeEkPC/WmTO//Oe1aKhKF8a6/X6BEevNGTD4OlU0cI8JErX/a r/6cnz5VTkq+rH5voadt+58sBRX1T7Q= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf25.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717594589; a=rsa-sha256; cv=none; b=AuUxiFV+Hmf6Ii1mRIvSUNfy36YuLx1yjj+Oc6czh5I4B4CBhqoZao1tpUr23kqYufpOjI vFu2Pvy7Hkzcz40lTIky+DVoBBSXDy+rdOVFhg3YW5WDR9lxdo+YLb/l7b9kpPDpO8OvxK E1g1FYDpm3QeQiH6aTHxsKBpdb1yizU= Received: from mail.maildlp.com (unknown [172.19.162.112]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4VvT311tfpz1HD9v; Wed, 5 Jun 2024 21:34:37 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id C0F3314011B; Wed, 5 Jun 2024 21:36:24 +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.39; Wed, 5 Jun 2024 21:36:24 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v6 10/15] mm: page_frag: use __alloc_pages() to replace alloc_pages_node() Date: Wed, 5 Jun 2024 21:33:00 +0800 Message-ID: <20240605133306.11272-11-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240605133306.11272-1-linyunsheng@huawei.com> References: <20240605133306.11272-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Queue-Id: 58981A0004 X-Stat-Signature: nt8ght1tn54yeouk1t8gr1xeyt79u1nw X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1717594588-281779 X-HE-Meta: U2FsdGVkX1+w5VoP6/7wh4WSt7mc4GFXDQ6au/kWo7V/rH+PaVLC5i4mJW6deTG9C2pEVKDkVWuQ0NrYrWHBUNHmV7WIYAF7anJ5AE8YEUML6FFjRaP+T3lyGxX0EciErHnJoL9fWD3cX3Sot8ZW9kI1yFKBdwU+ut0toUEw667xdzibwbhOYuovVrCmg3J9auM8wMsWhWm0ajP2M8Ik2lCdcrKsJJPTXcFULOVsTLK9qjxYIM+ru6TxlicJmRB0dp5FLxCaOi9yOBqqm1mBzkk05Gjahs2WDg8QQWfT94SipNTXWK1DLMy2DTaaPANzu/PzBj1veposBZimaSl+2Pg3UI4ZPFYSN0pqiNAuBfoSFzTuDvx1O6GgcqbUmuPDVd1hCUmNEIfS0l4EiixTnJ0bdZC6djDUBeyXZq84INKxTMBoYULskAHpILunyheik031jnN7ozNabT/IdD8yaU47cFc5pYWezPN9oOZ1V3x6wKFAFPTrFR+lMtY4nBIH3I93bDhLntG2qvMOMpi9oQP30s0bISM6vSKha5S+epekEnOl5uiTbU7XqD9hhy8GrZpYoCevX8aIa1sjlK+P45gdzEEE4JCcw/Z/wLUX2esAz5gd0H1pXxVYReWtEHg/2QnbMotsYt5xRzCqVNb1k/Z0M7NiImv6ZBIdcsHQhm2yPXoGBbpE4LdUHvfBF2O5nrxReE9s7JGB6RCDdFHjA9PQNm7VWBnL4JIuofc1DpPpaY9oQrf5XevjR58XZiKnvV9is6luecs+s5H9JvygXAHVrDq53orgu4OMXZHdzmL5tl2FLKIMMfqhrC6HWTFtFcN9hWzXk1LWBCRjCMNoNSiIaBaErKt90I9xIXiLFFknZln+VcfZTNW1qd9SUBFiosKAzCoMMPT8QnrmBAI2XYmlFH3JVRpkZpUjNCDmBw24PyD2ldheEyBle2cqDuYvHeYIUK79QY7I0MVt8Ds QlvVa4/B bqAKjiFApaUGHhzl94FbEldqLlM1TcVVyUwvOgYe0h0SkU6R/yQZXI+/1oqY8KiR6i9Y5w9pVXUwIG6I16Tj1c7SLSRjxsId+Xw4blT/qI3Z8LbP2QQ/zzBZcqzCTD4TNQM/CVh8hfZznaTuHzQOeI2JdC7Rv5s/I89oDuMbXjVU2OJH5PWoHXBrsv7zKylSYqJBsT23NvP7w8XpxKJzW97TukoNgcOIBK/R7QpuXSbXSeBfkUWAgJMUNU0UAL68PV/ivWJcoLSm0KLhZ65MbAnfvWMjwRH3+vL0y 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 more new APIs calling __page_frag_cache_refill() in this patchset, which may cause compiler not being able to inline __page_frag_cache_refill() into __page_frag_alloc_va_align(). Not being able to do the inlining seems to casue some notiable performance degradation in arm64 system with 64K PAGE_SIZE after adding new API calling __page_frag_cache_refill(). It seems there is about 24Bytes binary size increase for __page_frag_cache_refill() and __page_frag_cache_refill() in arm64 system with 64K PAGE_SIZE. By doing the gdb disassembling, It seems we can have more than 100Bytes decrease for the binary size by using __alloc_pages() to replace alloc_pages_node(), as there seems to be some unnecessary checking for nid being NUMA_NO_NODE, especially when page_frag is still part of the mm system. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- mm/page_frag_cache.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index b5ad6e9d316d..525b577b03a9 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -65,11 +65,11 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, gfp_mask = (gfp_mask & ~__GFP_DIRECT_RECLAIM) | __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; - page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, - PAGE_FRAG_CACHE_MAX_ORDER); + page = __alloc_pages(gfp_mask, PAGE_FRAG_CACHE_MAX_ORDER, + numa_mem_id(), NULL); #endif if (unlikely(!page)) { - page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); + page = __alloc_pages(gfp, 0, numa_mem_id(), NULL); if (unlikely(!page)) { memset(nc, 0, sizeof(*nc)); return NULL; From patchwork Wed Jun 5 13:33:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13686935 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 BCE43C25B76 for ; Wed, 5 Jun 2024 13:36:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BAA886B009E; Wed, 5 Jun 2024 09:36:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B33DC6B009F; Wed, 5 Jun 2024 09:36:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 939336B00A0; Wed, 5 Jun 2024 09:36:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 6E6CE6B009E for ; Wed, 5 Jun 2024 09:36:33 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 36B271A12F3 for ; Wed, 5 Jun 2024 13:36:33 +0000 (UTC) X-FDA: 82196934666.11.876B439 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf04.hostedemail.com (Postfix) with ESMTP id D68B740011 for ; Wed, 5 Jun 2024 13:36:30 +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.187 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=1717594591; 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=uUuh2lZVreLRmH1S4LPZsE57oVa+RJyK6o7hUOgboto=; b=KLzJZ6XFileOQGeQx1zrlUM4BybJBIERUiJRXc3D57141fEfu40eZW4DIb2AywW/VZcs71 Vf2XIb85VkK4BtLSdJ55LgGv0HlU4QZEYokpjjeYlfcVmpLGD6tGZFVgj1m/SJrKHsfMse fRTTJggFR95cT5HaKn1cJRKj+joqnEs= 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.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717594591; a=rsa-sha256; cv=none; b=Rvgm86NvabqhTKOIg8rsNDSjEjrc91+Sw4l8nwrjemJz4rdGmevN1MfR/gvD3nRLBmi122 XRpeA8fIezY9pBUpwMjCaaTfa+HowRad+z1dVrdG52qIhj6t1eOFyL+T5M9CKD2RWl/iD5 aSBw5kn2CgKTp/VqO/s5wn06GvNqRAQ= Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VvT0b3ZhCzwRrf; Wed, 5 Jun 2024 21:32:31 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 4A84118007A; Wed, 5 Jun 2024 21:36:28 +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.39; Wed, 5 Jun 2024 21:36:28 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v6 12/15] mm: page_frag: introduce prepare/probe/commit API Date: Wed, 5 Jun 2024 21:33:02 +0800 Message-ID: <20240605133306.11272-13-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240605133306.11272-1-linyunsheng@huawei.com> References: <20240605133306.11272-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: D68B740011 X-Stat-Signature: dpytjsctkzj3poymfsfu38f5yzpsb3bf X-Rspam-User: X-HE-Tag: 1717594590-29530 X-HE-Meta: U2FsdGVkX1/V+9ABuoYg6UzW2HKRsiwbhU73nZH9WJK8QoszCD5HThXfaOPv5T3dbNw73R+VGZxopiu+6ykxJHE0gQhiicFThYrl9EZd47CICxVxI27RkGlgJXuVl6DHS34tR8s6Dc8dcsirQP3Zco5B46JNgaz2R/rTUx3/zUQACqQ+ZMSTaVfwDXqSSFvLKRflXn7xGX//7Ul4oG7mtXw/XtqkKcRl0iARpFIJQVpKzdeaARaf+JZOn9Ew4gWChngLd1RhYPglXg6XdNw82xPgmzhcSiu6YA8R2dQYkWwNfDMo9azc5rM0oDuTdYNUxrqtsHoAtBgb8rgtOXbl79qkBn+Ge9TTIfHEPKP/BKbZlh99Y3M5aPQzOcAB0nhyL/wZXj8/D+tqIKKL2LNdoU81agn391AA0kV1erEceJGOtDqMoyXqNdPjMJssxImdV2TtXU4xM5xfyUJilnDhQLNDsmX1z+TSEPXbRz82ec0B9XNQeDMmsoi8331OueO2c6/I8iHPRmT/Vi0HuXAjSKmtVS1495GlXJviOSuYRqslq/I772/tG/1sUt3NcwpGN9cX5WNzN5UPtr3FJUOC0Tm15uDnfiK0H00Y1GS/vTDkrJAqhoutV5n5zW1D4/SuOUOWSLrT7KUk0HvL8DYQQMtUBJ/r2nMnheyZFc1ASkEyGtzlmcSOEgz/YblSMDs+b+YIlOaAZTmx8W6LxFZwTwo0kg/t83Jco0RCXgsBf8wCBvC0ouQU60kz8VgAISqW85b0rD1ElNpZ4jASOtHaXAKgFkGsRDv+OvzXyQAjJGGLrLi3wBy52FHsEl8eK79KbHcAxhfKE1J8pOstVS0JNCRnLEvR87bEMGfZrPoIbTKHrrPXvxVasAeOe9qx//2oVpS+x++t50HTh0wFRt8Nyrxn5lWbrIv9TpMS5NpSoDush2xQ4EkgJE+hvJ3XyRfI6as6li3nENMwgJGF2QH wpSHdjmV nPFa9sA+SBevwy8lS6rMjrjS8ZEDoDW7L+JVj7KS9434ayqO+gZyBbcE7Oftg5IO5uCkykU6zx6OxLZJKNIV06cjILRs6Y2gATX9tVFyHBa86T3sTCmYmkW6Lf3xFmcv+7lTLG6wLwbglKwCzuJzxapx7EMsh/+PZTMBM+X1X6V5cxu6iA9v9kzVbZpGi9IVGfv9Gwwo6MNwF5UbMNX9yQVlK73xQRXosxz4/Ouk19TxmVgAHbAEdEPJ/gghBR6vmJHtj263gr4U8rE/Ny4xfGGPbFGmWS1ug2/Fr 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 | 82 +++++++++++++++++++++++ mm/page_frag_cache.c | 114 ++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index b33904d4494f..e95d44a36ec9 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -4,6 +4,7 @@ #define _LINUX_PAGE_FRAG_CACHE_H #include +#include #define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK) #define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE) @@ -87,6 +88,9 @@ static inline unsigned int page_frag_cache_page_size(struct encoded_va *encoded_ 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); @@ -99,12 +103,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_size(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_size(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 525b577b03a9..9f86aa15bbeb 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -94,6 +94,120 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, 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 (unlikely(!__page_frag_cache_refill(nc, gfp) || + *fragsz > PAGE_SIZE)) + return NULL; + + remaining = nc->remaining; + } + + encoded_va = nc->encoded_va; + *fragsz = remaining; + return encoded_page_address(encoded_va) + + page_frag_cache_page_size(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 (unlikely(*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_size(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 (unlikely(*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_size(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 (unlikely(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_size(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) From patchwork Wed Jun 5 13:33:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13686936 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 E888FC25B76 for ; Wed, 5 Jun 2024 13:36:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 800646B00A3; Wed, 5 Jun 2024 09:36:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7AE826B00A4; Wed, 5 Jun 2024 09:36:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 677EE6B00A5; Wed, 5 Jun 2024 09:36:42 -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 44AEA6B00A3 for ; Wed, 5 Jun 2024 09:36:42 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 09E50140568 for ; Wed, 5 Jun 2024 13:36:42 +0000 (UTC) X-FDA: 82196935044.20.8802499 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf20.hostedemail.com (Postfix) with ESMTP id 8CF831C0020 for ; Wed, 5 Jun 2024 13:36:39 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf20.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=1717594600; 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=1HPZndlSVkoy03jpff4qu70ZSU+Wf/20iHswsLsvTi8=; b=5Xyk+IRhIT1QJ5p3oBQ4m89qsgus/e6PzSg3PzFRc6JIt6+pQwtvKPmZIDoaTckSiPkq8w wKHezyRE3iT9fvZtOCk85oVwdBAkcugrfLl+loJFa6Cm2wIyAJZlyZRJQ8nRsJpHDNKvSm ZRyhiNm/wm6ajzxQ8nk4cSSzVTbwMP0= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf20.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=1717594600; a=rsa-sha256; cv=none; b=k3P/u3vjxlOiF96VHvERyOyFYY51pNyRRruwpCj+vznoJcCpFdR93cDB2pHxGb91stHinU 6Z4Srq2ZM4vsKCMr+Hg/5sBPT2I81sa1XSDPHz4Php/UaTqvD1h454867Uo2LeTt11iTzb n6V8ECGMGxjcvu5vt54iCS8HdCc34KU= Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VvT3h58Jkzclln; Wed, 5 Jun 2024 21:35:12 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id DAE7318007A; Wed, 5 Jun 2024 21:36:36 +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.39; Wed, 5 Jun 2024 21:36:36 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Jonathan Corbet , Andrew Morton , , Subject: [PATCH net-next v6 14/15] mm: page_frag: update documentation for page_frag Date: Wed, 5 Jun 2024 21:33:04 +0800 Message-ID: <20240605133306.11272-15-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240605133306.11272-1-linyunsheng@huawei.com> References: <20240605133306.11272-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Queue-Id: 8CF831C0020 X-Stat-Signature: khubihonkekppanak8gdfnrw7ba7whab X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1717594599-361937 X-HE-Meta: U2FsdGVkX18IWivikDcNNYCMdKZTBgb6pbyZpNaEL+Cw+i7HrIC5B6FVaeoa1du9D/y+sGeIDM13a9OFYORrnV+2C//OuRa/ABxN2vyZd7Wm/HNpoXDU8MoCeddZYuonl5FbuAByKiNdq0AreWidTQul+vSBebe/nEgga1WN5NfKcMvxaxiCo4OF6Q5DeremE/dIBof4RN6KseWNZ/mUc1ABfMEKY7CysGHemvwMiRFZWXbkkRjsPwr85fxc7MVcbHP6dLZqP2HhTTAT8Gxw+4B0PGBKEWW0Xq7ebK1HhxU7EY8crcjQ3FAYQEhYsQCraM11l6FynYoczMQSxkNktWuaBq88fpg8i40VDmKocSUTDPUvrup+G1IAlDmsKiUWznWO50OLBkWR1Jop6Ak+bE8oRbxOyGjxE5OMI+SS0LVNU8qCwzuihGvHLcFY3gJR7oJAMWjGivaTnTZyZLV82+278ovMbOsFKrJbZK0X1/C1k8pRO2Xz/xVxKf8+7S83trSs5UgLxFGDAGX/FWijwit48QR7tU8ch5X/qSTLMyTRp0AgA1ciELSVnkdFni6dsRhNjT1P+8OSHOwWlbYUg+vA3gZwVrm1zDFzOTVEsctjRsjEmJtSTcqo8p3t9kpP0Rh/iSRL9YS6ldCAtMYlGMsm/yoRmbAB6u2UnJyYMJ3sro59MroyhjVVC2o6hIVicfuawuXMshnGNGdqiuoiUowj8qvPBS+kNTdY33j2ymXgfInusQVuD1XcseO0mQFpz4TVoUVyVi3bp6CUIUcl52CRrxmaSQNTOfX1gFCn15ovWfYZk/pFOjf4aekfEcEjvlmLMKxylwIuL9pKZWBBrb6fTmF0oXlT9TQcHtilVuIA4cpZdUzVJA3U05pJr8zSDPb2PIL3FOGcv+MXo1HBX7ILDDmTAIJIbfKjIb6s49yHKeok5m3y/SWNE5m408Ga/bCo5AyQ3S0RpaSq+CU i4DRBiCP e9iPgePLhvfq9SPsmt5SF1tTm1KCXOpSaCF1MLaihXku1gL6LCb1KJDCAWmA5AV/YoKS2D8v/2Wgi8CgluyLtZyYqvYuzehYozOwqHRxJPCayr+LykC6XiFSQp4ccwvFSqAORyPNOOzpcf5fvqva+2KVoKfC0NSb/i6Xo1JjvgKsRWH2Mf5RfesqDAQIA5lp4OKfdFSTaLvNOyYGPqK9s4q5/lasK1Yb8NEZYyhfVHpnvRHHoLKkrfFdUhOsgivEN/ahgXb1uqr0M3tD2aF/0bJklCaxFoHgHRNoV 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: Update documentation about design, implementation and API usages for page_frag. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- Documentation/mm/page_frags.rst | 163 +++++++++++++++++++++++++++++++- include/linux/page_frag_cache.h | 108 +++++++++++++++++++++ mm/page_frag_cache.c | 77 ++++++++++++++- 3 files changed, 345 insertions(+), 3 deletions(-) diff --git a/Documentation/mm/page_frags.rst b/Documentation/mm/page_frags.rst index 503ca6cdb804..50360516a139 100644 --- a/Documentation/mm/page_frags.rst +++ b/Documentation/mm/page_frags.rst @@ -1,3 +1,5 @@ +.. SPDX-License-Identifier: GPL-2.0 + ============== Page fragments ============== @@ -40,4 +42,163 @@ page via a single call. The advantage to doing this is that it allows for cleaning up the multiple references that were added to a page in order to avoid calling get_page per allocation. -Alexander Duyck, Nov 29, 2016. + +Architecture overview +===================== + +.. code-block:: none + + +----------------------+ + | page_frag API caller | + +----------------------+ + ^ + | + v + +------------------------------------------------------------+ + | request page fragment | + +------------------------------------------------------------+ + ^ ^ ^ + | | | + | Cache not enough | + | | | + | v | + Cache empty +-----------------+ | + | | drain old cache | | + | +-----------------+ | + | ^ | + |_________________________________| | + ^ | + | | + _________________v_______________ | + | | Cache is enough + | | | +PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE | | + | | | + | PAGE_SIZE >= PAGE_FRAG_CACHE_MAX_SIZE | + v | | + +----------------------------------+ | | + | refill cache with order > 0 page | | | + +----------------------------------+ | | + ^ ^ | | + | | | | + | Refill failed | | + | | | | + | v v | + | +----------------------------------+ | + | | refill cache with order 0 page | | + | +----------------------------------+ | + | ^ | +Refill succeed | | + | Refill succeed | + | | | + v v v + +------------------------------------------------------------+ + | allocate fragment from cache | + +------------------------------------------------------------+ + +API interface +============= +As the design and implementation of page_frag API implies, the allocation side +does not allow concurrent calling. Instead it is assumed that the caller must +ensure there is not concurrent alloc calling to the same page_frag_cache +instance by using its own lock or rely on some lockless guarantee like NAPI +softirq. + +Depending on different aligning requirement, the page_frag API caller may call +page_frag_alloc*_align*() to ensure the returned virtual address or offset of +the page is aligned according to the 'align/alignment' parameter. Note the size +of the allocated fragment is not aligned, the caller needs to provide an aligned +fragsz if there is an alignment requirement for the size of the fragment. + +Depending on different use cases, callers expecting to deal with va, page or +both va and page for them may call page_frag_alloc_va*, page_frag_alloc_pg*, +or page_frag_alloc* API accordingly. + +There is also a use case that needs minimum memory in order for forward progress, +but more performant if more memory is available. Using page_frag_alloc_prepare() +and page_frag_alloc_commit() related API, the caller requests the minimum memory +it needs and the prepare API will return the maximum size of the fragment +returned. 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. + +.. kernel-doc:: include/linux/page_frag_cache.h + :identifiers: page_frag_cache_init page_frag_cache_is_pfmemalloc + page_frag_cache_page_offset page_frag_alloc_va + page_frag_alloc_va_align page_frag_alloc_va_prepare_align + page_frag_alloc_probe page_frag_alloc_commit + page_frag_alloc_commit_noref page_frag_alloc_abort + +.. kernel-doc:: mm/page_frag_cache.c + :identifiers: __page_frag_alloc_va_align page_frag_alloc_pg + page_frag_alloc_va_prepare page_frag_alloc_pg_prepare + page_frag_alloc_prepare page_frag_cache_drain + page_frag_free_va + +Coding examples +=============== + +Init & Drain API +---------------- + +.. code-block:: c + + page_frag_cache_init(pfrag); + ... + page_frag_cache_drain(pfrag); + + +Alloc & Free API +---------------- + +.. code-block:: c + + void *va; + + va = page_frag_alloc_va_align(pfrag, size, gfp, align); + if (!va) + goto do_error; + + err = do_something(va, size); + if (err) { + page_frag_free_va(va); + goto do_error; + } + +Prepare & Commit API +-------------------- + +.. code-block:: c + + unsigned int offset, size; + bool merge = true; + struct page *page; + void *va; + + size = 32U; + page = page_frag_alloc_prepare(pfrag, &offset, &size, &va); + if (!page) + goto wait_for_space; + + copy = min_t(unsigned int, copy, size); + if (!skb_can_coalesce(skb, i, page, offset)) { + if (i >= max_skb_frags) + goto new_segment; + + merge = false; + } + + copy = mem_schedule(copy); + if (!copy) + goto wait_for_space; + + err = copy_from_iter_full_nocache(va, copy, iter); + if (err) + goto do_error; + + if (merge) { + skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); + page_frag_alloc_commit_noref(pfrag, offset, copy); + } else { + skb_fill_page_desc(skb, i, page, offset, copy); + page_frag_alloc_commit(pfrag, offset, copy); + } diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index e95d44a36ec9..7ee9e188c09d 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -71,11 +71,28 @@ struct page_frag_cache { #endif }; +/** + * page_frag_cache_init() - Init page_frag cache. + * @nc: page_frag cache from which to init + * + * Inline helper to init the page_frag cache. + */ static inline void page_frag_cache_init(struct page_frag_cache *nc) { memset(nc, 0, sizeof(*nc)); } +/** + * page_frag_cache_is_pfmemalloc() - Check for pfmemalloc. + * @nc: page_frag cache from which to check + * + * Used to check if the current page in page_frag cache is pfmemalloc'ed. + * It has the same calling context expection as the alloc API. + * + * Return: + * true if the current page in page_frag cache is pfmemalloc'ed, otherwise + * return false. + */ static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc) { return encoded_page_pfmemalloc(nc->encoded_va); @@ -95,6 +112,19 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask); +/** + * page_frag_alloc_va_align() - Alloc a page fragment with aligning requirement. + * @nc: page_frag cache from which to allocate + * @fragsz: the requested fragment size + * @gfp_mask: the allocation gfp to use when cache needs to be refilled + * @align: the requested aligning requirement for virtual address of fragment + * + * WARN_ON_ONCE() checking for @align before allocing a page fragment from + * page_frag cache with aligning requirement. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ static inline void *page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align) @@ -103,11 +133,32 @@ static inline void *page_frag_alloc_va_align(struct page_frag_cache *nc, return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, -align); } +/** + * page_frag_cache_page_offset() - Return the current page fragment's offset. + * @nc: page_frag cache from which to check + * + * The API is only used in net/sched/em_meta.c for historical reason, do not use + * it for new caller unless there is a strong reason. + * + * Return: + * the offset of the current page fragment in the page_frag cache. + */ static inline unsigned int page_frag_cache_page_offset(const struct page_frag_cache *nc) { return page_frag_cache_page_size(nc->encoded_va) - nc->remaining; } +/** + * page_frag_alloc_va() - Alloc a page fragment. + * @nc: page_frag cache from which to allocate + * @fragsz: the requested fragment size + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * + * Get a page fragment from page_frag cache. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ static inline void *page_frag_alloc_va(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask) { @@ -117,6 +168,21 @@ static inline void *page_frag_alloc_va(struct page_frag_cache *nc, void *page_frag_alloc_va_prepare(struct page_frag_cache *nc, unsigned int *fragsz, gfp_t gfp); +/** + * page_frag_alloc_va_prepare_align() - Prepare allocing a page fragment with + * aligning requirement. + * @nc: page_frag cache from which to prepare + * @fragsz: in as the requested size, out as the available size + * @gfp: the allocation gfp to use when cache need to be refilled + * @align: the requested aligning requirement + * + * WARN_ON_ONCE() checking for @align before preparing an aligned page fragment + * with minimum size of @fragsz, @fragsz is also used to report the maximum size + * of the page fragment the caller can use. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ static inline void *page_frag_alloc_va_prepare_align(struct page_frag_cache *nc, unsigned int *fragsz, gfp_t gfp, @@ -151,6 +217,21 @@ static inline struct encoded_va *__page_frag_alloc_probe(struct page_frag_cache return encoded_va; } +/** + * page_frag_alloc_probe - Probe the available page fragment. + * @nc: page_frag cache from which to probe + * @offset: out as the offset of the page fragment + * @fragsz: in as the requested size, out as the available size + * @va: out as the virtual address of the returned page fragment + * + * Probe the current available memory to caller without doing cache refilling. + * If no space is available in the page_frag cache, return NULL. + * If the requested space is available, up to @fragsz bytes may be added to the + * fragment using commit API. + * + * Return: + * the page fragment, otherwise return NULL. + */ #define page_frag_alloc_probe(nc, offset, fragsz, va) \ ({ \ struct page *__page = NULL; \ @@ -165,6 +246,14 @@ static inline struct encoded_va *__page_frag_alloc_probe(struct page_frag_cache __page; \ }) +/** + * page_frag_alloc_commit - Commit allocing a page fragment. + * @nc: page_frag cache from which to commit + * @fragsz: size of the page fragment has been used + * + * Commit the actual used size for the allocation that was either prepared or + * probed. + */ static inline void page_frag_alloc_commit(struct page_frag_cache *nc, unsigned int fragsz) { @@ -173,6 +262,16 @@ static inline void page_frag_alloc_commit(struct page_frag_cache *nc, nc->remaining -= fragsz; } +/** + * page_frag_alloc_commit_noref - Commit allocing a page fragment without taking + * page refcount. + * @nc: page_frag cache from which to commit + * @fragsz: size of the page fragment has been used + * + * Commit the alloc preparing or probing by passing the actual used size, but + * not taking refcount. Mostly used for fragmemt coalescing case when the + * current fragment can share the same refcount with previous fragment. + */ static inline void page_frag_alloc_commit_noref(struct page_frag_cache *nc, unsigned int fragsz) { @@ -180,6 +279,15 @@ static inline void page_frag_alloc_commit_noref(struct page_frag_cache *nc, nc->remaining -= fragsz; } +/** + * page_frag_alloc_abort - Abort the page fragment alloced using page_frag_alloc() + * related API back to the page_frag cache. + * @nc: page_frag cache to which the page fragment is aborted back + * @fragsz: size of the page fragment to be aborted + * + * It is expected to be called from the same context as the alloc API. + * Mostly used for error handling cases where the fragment is no longer needed. + */ static inline void page_frag_alloc_abort(struct page_frag_cache *nc, unsigned int fragsz) { diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 9f86aa15bbeb..e76f035e63de 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -94,6 +94,18 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, return page; } +/** + * page_frag_alloc_va_prepare() - Prepare allocing a page fragment. + * @nc: page_frag cache from which to prepare + * @fragsz: in as the requested size, out as the available size + * @gfp: the allocation gfp to use when cache needs to be refilled + * + * Prepare a page fragment with minimum size of @fragsz, @fragsz is also used + * to report the maximum size of the page fragment the caller can use. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ void *page_frag_alloc_va_prepare(struct page_frag_cache *nc, unsigned int *fragsz, gfp_t gfp) { @@ -116,6 +128,19 @@ void *page_frag_alloc_va_prepare(struct page_frag_cache *nc, } EXPORT_SYMBOL(page_frag_alloc_va_prepare); +/** + * page_frag_alloc_pg_prepare - Prepare allocing a page fragment. + * @nc: page_frag cache from which to prepare + * @offset: out as the offset of the page fragment + * @fragsz: in as the requested size, out as the available size + * @gfp: the allocation gfp to use when cache needs to be refilled + * + * Prepare a page fragment with minimum size of @fragsz, @fragsz is also used + * to report the maximum size of the page fragment the caller can use. + * + * Return: + * the page fragment, otherwise return NULL. + */ struct page *page_frag_alloc_pg_prepare(struct page_frag_cache *nc, unsigned int *offset, unsigned int *fragsz, gfp_t gfp) @@ -146,6 +171,21 @@ struct page *page_frag_alloc_pg_prepare(struct page_frag_cache *nc, } EXPORT_SYMBOL(page_frag_alloc_pg_prepare); +/** + * page_frag_alloc_prepare - Prepare allocing a page fragment. + * @nc: page_frag cache from which to prepare + * @offset: out as the offset of the page fragment + * @fragsz: in as the requested size, out as the available size + * @va: out as the virtual address of the returned page fragment + * @gfp: the allocation gfp to use when cache needs to be refilled + * + * Prepare a page fragment with minimum size of @fragsz, @fragsz is also used + * to report the maximum size of the page fragment. Return both 'struct page' + * and virtual address of the fragment to the caller. + * + * Return: + * the page fragment, otherwise return NULL. + */ struct page *page_frag_alloc_prepare(struct page_frag_cache *nc, unsigned int *offset, unsigned int *fragsz, @@ -178,6 +218,18 @@ struct page *page_frag_alloc_prepare(struct page_frag_cache *nc, } EXPORT_SYMBOL(page_frag_alloc_prepare); +/** + * page_frag_alloc_pg - Alloce a page fragment. + * @nc: page_frag cache from which to alloce + * @offset: out as the offset of the page fragment + * @fragsz: the requested fragment size + * @gfp: the allocation gfp to use when cache needs to be refilled + * + * Get a page fragment from page_frag cache. + * + * Return: + * the page fragment, otherwise return NULL. + */ struct page *page_frag_alloc_pg(struct page_frag_cache *nc, unsigned int *offset, unsigned int fragsz, gfp_t gfp) @@ -208,6 +260,10 @@ struct page *page_frag_alloc_pg(struct page_frag_cache *nc, } EXPORT_SYMBOL(page_frag_alloc_pg); +/** + * page_frag_cache_drain - Drain the current page from page_frag cache. + * @nc: page_frag cache from which to drain + */ void page_frag_cache_drain(struct page_frag_cache *nc) { if (!nc->encoded_va) @@ -228,6 +284,19 @@ void __page_frag_cache_drain(struct page *page, unsigned int count) } EXPORT_SYMBOL(__page_frag_cache_drain); +/** + * __page_frag_alloc_va_align() - Alloc a page fragment with aligning + * requirement. + * @nc: page_frag cache from which to allocate + * @fragsz: the requested fragment size + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * @align_mask: the requested aligning requirement for the 'va' + * + * Get a page fragment from page_frag cache with aligning requirement. + * + * Return: + * Return va of the page fragment, otherwise return NULL. + */ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) @@ -263,8 +332,12 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, } EXPORT_SYMBOL(__page_frag_alloc_va_align); -/* - * Frees a page fragment allocated out of either a compound or order 0 page. +/** + * page_frag_free_va - Free a page fragment. + * @addr: va of page fragment to be freed + * + * Free a page fragment allocated out of either a compound or order 0 page by + * virtual address. */ void page_frag_free_va(void *addr) {