From patchwork Wed May 8 13:33: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: 13658776 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 E5C94C04FFE for ; Wed, 8 May 2024 13:36:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2A09A6B0083; Wed, 8 May 2024 09:36:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2508F6B0085; Wed, 8 May 2024 09:36:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 119F06B0088; Wed, 8 May 2024 09:36:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E90F56B0083 for ; Wed, 8 May 2024 09:36:46 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4B54C1C16EB for ; Wed, 8 May 2024 13:36:46 +0000 (UTC) X-FDA: 82095328812.06.1A7BA1D Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf21.hostedemail.com (Postfix) with ESMTP id E169C1C001B for ; Wed, 8 May 2024 13:36:42 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf21.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.190 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=1715175404; 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=Y1Ct3Q97njTwqrArmYTfVauBblfv2jOpYa3QvowCi0Y=; b=GZ41UjWemQqtCcF4PUmdsbPjqBZEpa4h0qhbfOlo9GqYspVn4ihUCTxaKu9hwcZWGYaNm8 AZQPQkWBB77X/KA0Tk6WjUl14oEpCfxozVo+inwspB9MkqAyM8rDHfijTM+JKGtsLEPEcg 11+j0dUw3NLhY4jEyEvE+YFt7gxkxU0= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf21.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715175404; a=rsa-sha256; cv=none; b=sfrZOsq+dnZQCsTnJtuml0XGbLBAp6bJGaFdja0EuvNunkmcqaJhyPtTWrhRPdkBX6IbxR usC5zQMTN2dYrurZFnFW1re9x/Pm5/5i6A7tzGFUME1PRQ52KequuitxNXNri7vg+YaDEl /fk1gFmgRJAnb1uPkADiDj/5tX8b748= Received: from mail.maildlp.com (unknown [172.19.88.163]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4VZGM35wqJz1yn1J; Wed, 8 May 2024 21:33:51 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 439F7180062; Wed, 8 May 2024 21:36:38 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Wed, 8 May 2024 21:36:37 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v3 01/13] mm: page_frag: add a test module for page_frag Date: Wed, 8 May 2024 21:33:56 +0800 Message-ID: <20240508133408.54708-2-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240508133408.54708-1-linyunsheng@huawei.com> References: <20240508133408.54708-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500005.china.huawei.com (7.185.36.74) X-Stat-Signature: 9a99z3ka99ahzr97ewwf9rujci4iyboo X-Rspamd-Queue-Id: E169C1C001B X-Rspamd-Server: rspam10 X-Rspam-User: X-HE-Tag: 1715175402-539825 X-HE-Meta: U2FsdGVkX19Wf9gME9zy7LdZS0CJ0hCQkMm5V15O0I+DxELIh9nlB4Xx6zkAJnF2o71/eT+YvJobYqvK4wbvxkWX00PV/TAfKPS94vUXCBEKjf7JCHevo2KsUiXKF+IyxFjICM5DYW3EkSaduT02FRLzEZmXbDlDIieL3d9ijo37W8I55tgM8lv7u6cXpBAo4Yq/UKV5ciNaW3CeFCvUEfB2YYekz6jZDQZWW2AV0Stm8Frz1FVOYc8x/5m3x2APi9qriE/s8urarVNmvRR0vygxtJnJsXP5L0RRh31llwKoxgRGxapuiglxHqzMr0DC444WKf1/WT2nGEKGhYj031ErFcYhVwXQSj088pRkA79NdS04PkD3gwfna8SFVmN3QVRnhID+G/USc2pQomADYMtnYymlp29xh7Qg6+rev6V2oO7cDLUy1TBEnIDcVqKvoD1Da3uNNiUDiEEF0h/+7rikR6G5jRtsNOihoTz08P4LJp3ZYnq935Rw1jvQwNZ1/dxGyOtLYUFa0DKUhs/icx50n4obwXWyxhXcmPsKQzLChxSkLEoslhqSDEdPPOUw7sb5clIwJquuRQEM2nFjpCItNeIb0Ym7/DkbKqr5IBnpSQFpBjMw6J710R0vwgZ41vGWXPdtBXOnBEFjE7ODsg0IqaByjL9iO2N3Lfbc3nzJOXawXf16n/5uxl+0XmDgqf8LffSt2TPfNTr9l0cxB8Ktuq3EcCEFqlPOn6fpzcqo12Ayl8ycdcFhYkkisJLwf0oiZs25P/RPJGC0UBEqHBeJGJBrsO4gMihNSqgIzr28blOoDP3K8NtJcxku13okFiw4uNY5STJNiOEjMrJ6yavy6+HyjA38f50h2QTVuA9xqyNvF8aQ12+8YjLV4O2h27W/i3ApRbWpTaHJHvMWyQcaRQ1GyYoZN24G3/WcUg7i9EqBC5CpqhMYdygGeT56SeOU8zFExCq7y89o9Av O7so/Mwh v/oQifaVQ6Ei1RRlsHn8lMy9TzkqlL0/Fv1n9Wjq4eaEMEG8IzswB7J1c4UN6AdRom7fkvxStubCLVFQy8Hy/sspjQr5VpbzCns5L2pGjKN+HENDHMgfSXpWac+E4IH/U3DEDE+l7Irpn/MZMdgyM8kgxQSqR6N+F6C+NLXCvQFg7mTrWxRivCBnfM9WLf2sMhaDsvTeuGc9/k+nsKEEisoaZpgh+yawB0gmXIsGhjphj0CU/VGGXAbgUzcLNAUfrDX7YICsEd3hpDXZEX3BehzZdaKRjRj2zlTLSyVDX5R70t8TuS4lYa7uVsa0KwJGe8T1K 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 fragments allocated from a frag_frag_cache instance is pushed into a ptrpool instance in a kthread binded to the first cpu, and a kthread binded to the current node will pop the fragmemt from the ptrpool and free the fragmemt. 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 | 377 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 386 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 4abb40b911ec..5a14e6992f44 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -101,6 +101,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..dc6e75656e42 --- /dev/null +++ b/mm/page_frag_test.c @@ -0,0 +1,377 @@ +// 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 int nr_test = 5120000; +static bool test_align; +static atomic_t nthreads; +static struct completion wait; +static struct page_frag_cache test_frag; + +module_param(nr_test, int, 0600); +MODULE_PARM_DESC(nr_test, "number of iterations to test"); + +module_param(test_align, bool, 0600); +MODULE_PARM_DESC(bool, "use align API for testing"); + +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) { + unsigned int size = get_random_u32(); + void *va; + int ret; + + size = clamp(size, 1U, PAGE_SIZE); + if (test_align) + va = page_frag_alloc_align(&test_frag, size, GFP_KERNEL, + SMP_CACHE_BYTES); + else + va = page_frag_alloc(&test_frag, size, 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, + cpumask_first(cpu_online_mask), + "page_frag_push"); + if (IS_ERR(tsk_push)) + return PTR_ERR(tsk_push); + + tsk_pop = kthread_create(page_frag_pop_thread, &ptr_pool, + "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 May 8 13:33: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: 13658777 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 603B3C25B5F for ; Wed, 8 May 2024 13:36:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0094A6B0085; Wed, 8 May 2024 09:36:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EFABC6B0088; Wed, 8 May 2024 09:36:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D74966B0089; Wed, 8 May 2024 09:36:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id BA09B6B0085 for ; Wed, 8 May 2024 09:36:47 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 745731610AE for ; Wed, 8 May 2024 13:36:47 +0000 (UTC) X-FDA: 82095328854.25.5D65D68 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf06.hostedemail.com (Postfix) with ESMTP id 1CC7D18000F for ; Wed, 8 May 2024 13:36:44 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf06.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=1715175405; 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=lliFZr6GKraR8G24zn6kAgvIajI96YnfDePdDqHBH4E=; b=5NXVxYbrDNrDEEc7xmSss9ZH9ddHtTuM9sFXY9IeSQ1ucdOzrdl7XpNTKrP3TRrX1GzDJL KLFfcAqOy6FY0C/oAXlVYyRaYYywJsSAsJkfr7QCVxweNNTbIHw3DfPJMBs5qlj2zx5r8c Dmyipt3grPVk7GviecOXTp0DCEFavd0= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf06.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=1715175405; a=rsa-sha256; cv=none; b=i0zK/Dt9pgUT3ZnfU7OxEIglRzHlpSetKsuOF1NL2SmBA2BdAqJUQZt6nlP9jWXiG1vvz8 xySk2uyIZyq1Lc7+7HfP5jUmh2qWdThD0YGAc7AD65v30vtWHFo0EfxZQi0ScxhIN21v3T NKBMS5r4KCJmayLr0tLFLSvq8i47K/Q= Received: from mail.maildlp.com (unknown [172.19.163.17]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4VZGLX6Lg4z1j1Kh; Wed, 8 May 2024 21:33:24 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 621BB1A0188; Wed, 8 May 2024 21:36:41 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Wed, 8 May 2024 21:36:41 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v3 03/13] mm: page_frag: use free_unref_page() to free page fragment Date: Wed, 8 May 2024 21:33:58 +0800 Message-ID: <20240508133408.54708-4-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240508133408.54708-1-linyunsheng@huawei.com> References: <20240508133408.54708-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500005.china.huawei.com (7.185.36.74) X-Stat-Signature: oiqoiu7fkgfptqitqhtwkz8es7rckkyq X-Rspamd-Queue-Id: 1CC7D18000F X-Rspamd-Server: rspam10 X-Rspam-User: X-HE-Tag: 1715175404-690829 X-HE-Meta: U2FsdGVkX1/0pNNcSgiiJ6XpBaXThUgnxLtZO4mzv3UFrnNHHA68RDWBJyX3v16zKZe2eXFy0zG10jQ2/aPRICM6fs2l26zVIy2HEEsKux40JjMu0cRamQZ63Zp/I91jKoFwwzygMmBvbCYtQo6kazssqICgnLHq0Jp44T5YD7Rh80UKL+qsd9idcMKqcj/AY3x1niUMq0ls3X60QiUeTiQTe3rj5F6vu//EVJ6vNKhasK4cpAsKe2WblGUwI5nIIP/DVnhrXMTBu1nBXj6pw9WUeIlp2mnZTxO+5FWegJmaz8SZK50NEH0NKoYvN9WTwN1sn34/6RmomjM/z8jTIiUl6WXxeG2/hJTDk5+s3SsFfbq4SvO5FwEM2CLUcfCKsiOg/Ufp39Wea5BP1PzQWvk3d8x2Lpf3YkK/nfUz7wWuRblVLv0Cekb38HcjrH4+AnyTurpRTnCzVjfRdYYvoQw+2wcUOkwL7Q74fgnILM5QSIJQIPnv4ajxXyk0u+w2z9PllJd48RLM2zt7tZB0D0GntxfaO5rwrZrV0Mmdv+eDNbtyAThWxgHi5EetouAm9LB0BLtnhNlfqArfsmXGs9Gw1+eMWbiwDJComyBfq0PM3+630Hyb8urUIraiug8yj4+on8785mDON57Ob1ZLxkOaoy23FyHhZ1Bng1H3KV7ZGdvOcB7uQZAGGi6uWy2rYUtkvLtqaczWkM7YRC+dVpPa8hb7kbeT/w1SH6U0yccbfH4y4BVNaru0iP+9QkngBYv/LGnAY59r82qoQAYC/R3nFjo00usZNtH47vyVL3cBlQbHK4zumxl/ftrgX6NQfdsTkWgVgpy6Bz0bT0iiLR6z8dBVKGjBlKUkG2Hinf10dX9gToTsFiTbWLMBxybKhjwlyyN9j9RFjcs8wPGzX9KUSS2tY2awVRBAziT6F+31v7rzq3AYN3vBqPRXJos3iVKuBajnQxrZCkRHeAe O0h7cftY aqM8HhTw7Wi/uqvEfXh6Q53EovxaAvIIlo88DzZfzADbSfjA1O5aXohNFCb3++B33h2qoj/ho+QGjxIwGuaEWzDhUgs+VnXoFT8ucW9DGzECnyZ5FteYj2o7Yy9EkyXegrVTLH3/zH1sFB97X5kVgDwpERquNNDFI4AxDIa6keK0S4qNVDD9uHBJBj7/ZSPsSCUJ1DU9dXRBkmLJpxY32UFGMwuAU0raFcjxOP2lS35XG8mdTf08cUsQ/dhpd/CkOrMI5If6bNWY0ZqbdUprtj4iPmToMuAGhrfg6 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 | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 14d39f34d336..7adb29f8f364 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4693,6 +4693,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, @@ -4722,7 +4725,7 @@ 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_the_page(page, compound_order(page)); + free_unref_page(page, compound_order(page)); } EXPORT_SYMBOL(__page_frag_cache_drain); @@ -4763,7 +4766,7 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, goto refill; if (unlikely(nc->pfmemalloc)) { - free_the_page(page, compound_order(page)); + free_unref_page(page, compound_order(page)); goto refill; } @@ -4807,7 +4810,7 @@ void page_frag_free(void *addr) struct page *page = virt_to_head_page(addr); if (unlikely(put_page_testzero(page))) - free_the_page(page, compound_order(page)); + free_unref_page(page, compound_order(page)); } EXPORT_SYMBOL(page_frag_free); From patchwork Wed May 8 13:33: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: 13658778 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 8FDF7C19F4F for ; Wed, 8 May 2024 13:36:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 476576B007B; Wed, 8 May 2024 09:36:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4273B6B0088; Wed, 8 May 2024 09:36:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 252A96B0089; Wed, 8 May 2024 09:36:51 -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 F408A6B007B for ; Wed, 8 May 2024 09:36:50 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 895EE80B69 for ; Wed, 8 May 2024 13:36:50 +0000 (UTC) X-FDA: 82095328980.04.ADC3D11 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf30.hostedemail.com (Postfix) with ESMTP id 0591B80006 for ; Wed, 8 May 2024 13:36:47 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=none; spf=pass (imf30.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715175408; a=rsa-sha256; cv=none; b=ZvTQCq+3T62tOzdEdpHjmX2KsBs3/tthtWH+iytv/Db/bLyEagjQ81VGA+3OTB6iQP1gRj 4WewHcv8cJdwS8mVABE11eIBipnZibWzTlUaOvX0Lmy4ay3g0X1N83/6QFOL+s3C1WJiot l5Vw0lB9VY5v6WfUFX2X/duBktRzsoo= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=none; spf=pass (imf30.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=1715175408; 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=uCoMehum2lnSZ+K+SXMa5Idyq/W+xCdgqRSpuNPb8ws=; b=4tpqLg9YwH709Brf2nnVgVFxt9H+ymbA1IncOanrqP57EiK6qyw0NIsoE8jDTRY1BM9yFo gTTZvDCKkMNk8cFirgS5bUqwbTarVI21YcAFk7ZsUZM/jL/YUc0cQ4Pu+xqWfqcBWt5kMe oQV+v/Jgnl6NfQGHbAFY3j1qzXUYBVA= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4VZGLX6l8gz1RCbm; Wed, 8 May 2024 21:33:24 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id DE26814011A; Wed, 8 May 2024 21:36:44 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Wed, 8 May 2024 21:36:44 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , David Howells , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v3 04/13] mm: move the page fragment allocator from page_alloc into its own file Date: Wed, 8 May 2024 21:33:59 +0800 Message-ID: <20240508133408.54708-5-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240508133408.54708-1-linyunsheng@huawei.com> References: <20240508133408.54708-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500005.china.huawei.com (7.185.36.74) X-Stat-Signature: yzoko45yj7hi1n37yowd3q9kkyeou5t5 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 0591B80006 X-HE-Tag: 1715175407-573172 X-HE-Meta: U2FsdGVkX1+bocvIYtHbIzZxYOpBcJvDObtOhiRktuzHn/RDnljjN6Wiuw4MtvyvT0Cm7wUzzz7H5HAUzjcNxgmBYocHf0aHS/gQcMgTRn5Za94rgkvsGo1iQNYZfEu4aKCAsXiWpD60vV09wzPLTB2Imc+sWMBIUNiW6gzQQNJuwSCOSCjfgs7fD8KUuzbXoJWjcMsdkFrC42vAR5IZghfblMkQRALJp5ubnxe3n6ajXEgIkJRIK4wFD5HK+4/813qqB4fXwgtxRAqnbrPP9ul9AYM/sK3/tMdvij392k/0gszls2eWFe8tZJgMCN+NjYpxHvRIGTb2vPEJmByV7U120j6xThL4BKg/GlCNkpMqHEhuq5tG+0HM+5h9tsj+/+mojwZRzn3vaba0wWxlHDHu7IsCVYMzo5oqYRFGgzVU2ThbC6Uye8rSivciP8kjL5eBAxXgOPPfVzXYOrIV6UVN+Mk39MZripDe90lmJ/24/JDInjo73eVIAyXT+Qgpn6sJJeuOiuLxqCh0P5StGurHlfOUoMqO6HWQwzYg0jX1Il0l5+xAXRX0OYyqZGVlxmuMsfc/JAN8cPYIlTlvZPU+U+Pl/5xgu/sDm9ldPoVvaqTdAZ8xEoa536fjI1EjtaU8DNG47VE6NAzlXm2sfrGEMBAaUZ5mIK36HjDqNogFZdZstXJFMQWZXMO9RRVaugpWVuyHNSr7upj5uTTcklPHsZCNjar5gs3yKE9frLQ3W0eKC/K5e8y924PvRZ4tFYyIw+YyQtSKOvbi1u4Qb6WAxMrifctKaaPv1kF30uNgvhutTWqa/dCDG1zGVu7svAWOi6oNP02I3Sz5mkH9xAs7CgQPMnH5RJTK1CUe7zkjGVNplJQZ9BYOGUqNqxotMbXoGujV8I+Y1QCOeBFMQhX3U7JKkzs352YMoTh0Jk5DNw6CybVHapx5t567vL+1788bzMpe3PBPo/iXI8h szASHZds wZnOpjDmV/EqxpDWS+YHuD2mx15ttBOUqZAdgIkaG6WGYKL8oqERQLwWZkrawIw6lT73OVeOA9Pw1d+I9opuIqhLr3nJzd7x6JmATrWzcVSvMom9eAA6u0SDQBHsoOwPBVfc5AZ/VG4Fbm1cYDC+ILqExdB59tCnRXj6KcU2Bi+BNQwiluTX6VZt7lY2UathL+7QBMzmSoSk3RFblXMMtA+lEynIvcxGdk3wsOShrlGRmyw8UGXAlIrVWLp3q4n08rxEw8To4sOB5EuBp210GDIWtPTfl9V6LMAwMRlFwvTPSsXJmlVF612HnAzr5BdDzmVggrBRrinu0p1/UyRLYtY3BHsyHP1+vD6Z8WUVzRj+zuwJhfK9Qxmj4mQ== 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 c775ea3c6015..5afeab2b906f 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -310,28 +310,6 @@ __meminit void *alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask) __al 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 5240bd7bca33..78a92b4475a7 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -504,9 +504,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 @@ -525,21 +522,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..635b67ceb939 --- /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 36b133f04d30..ce077d14eab6 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 5a14e6992f44..8b62f5de48a7 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -59,6 +59,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 7adb29f8f364..2308360d78eb 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4675,145 +4675,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 dc6e75656e42..f1c861709551 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 May 8 13:34: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: 13658779 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 D6002C04FFE for ; Wed, 8 May 2024 13:36:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CE2006B0089; Wed, 8 May 2024 09:36:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C3EB86B008C; Wed, 8 May 2024 09:36:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A41CD6B0092; Wed, 8 May 2024 09:36:52 -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 803986B0089 for ; Wed, 8 May 2024 09:36:52 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3AFB9810FF for ; Wed, 8 May 2024 13:36:52 +0000 (UTC) X-FDA: 82095329064.07.A5B39E5 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf25.hostedemail.com (Postfix) with ESMTP id B3C8BA0007 for ; Wed, 8 May 2024 13:36:49 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; spf=pass (imf25.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=1715175410; 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=yCjipTYX6NeIbyvwvOFcZRg33GjA6EnXlF/ICoHJ5Fw=; b=1FoMTvASKDWwH9BJKJVFi7LJd6dq9cQyV3XOHOkIX0E4Gj65i0vSWsyCEfsI5jOd8F4r4R mKgbnO66G9uPpnVYHnCS63F1BIafCUwvo/lDyrGkap1kbQhTBBjAwLG8Azay65oHh4nPiU fYRg8CN2HO0qjLpnUC4TgGWzVUW5FJY= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715175410; a=rsa-sha256; cv=none; b=t+a5L4AWlgUVxuw3Ftcv1C4Qay3LUVFazreNXxiJdnMQ1IBV/ODNhLAvCdsVbtdi1UERo3 Q17efYnuwczVSRBgZbmUljzjTI19nQ2XCl5ohvDkDcm2PZ+i3XunKRZjxRkk13gDsWsaa4 Fu12y0IcEgWwwI798cj+7D/1xfLfwTA= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VZGKz65lVzYnCc; Wed, 8 May 2024 21:32:55 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id BD5C514011A; Wed, 8 May 2024 21:36:46 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Wed, 8 May 2024 21:36:46 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v3 05/13] mm: page_frag: use initial zero offset for page_frag_alloc_align() Date: Wed, 8 May 2024 21:34:00 +0800 Message-ID: <20240508133408.54708-6-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240508133408.54708-1-linyunsheng@huawei.com> References: <20240508133408.54708-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Queue-Id: B3C8BA0007 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: xng9ymjzcaom51oi5j13xxbc9j4b64pk X-HE-Tag: 1715175409-814452 X-HE-Meta: U2FsdGVkX18+wuz4byNAgqRGssBDXmABxI4tbI2khDLMU6e4mCKdY40iCiD07eg+2OebcmtUAJe/yZKf/8kyKiGQx7SS3tURZBptpLLzVgW1EMdaSHHFBgmNHYL5eeJ3ck2IoyYnxmp0LszS3Z7HN5zEdulENIBplD95swXejPTDVGJiUlUMrtLWxKAj7dHqnF2SaFxWTxnnsa8THZez0N5CF4rcLBPy18tz/XYIANBt8gleagjpo10HUTwj1ZEVd/rUb3RBJWSSXFmnvyB1YIk7zIHu02XKbkYV+ZOIqrmq5AQ+0ivn1J3I4Qq+LHPrRxPYkEyqtw/iKdzDzFw5fqBTAde7ly0fZP2yO/kaCUMBi8dLbDsZSMQhsgUR7xwzeRXsoofiW3YIhzxncg1bycvBsg+f1VtVjFeplTVyRTGAkdElGq6cxxrPYXSa1PyXDov3ENCtAqVugnO8DRFmqXlGPwESSDRVrGNr3LPAGbbWT3JWpxU9lhnxpxxq4Vr8k9JjaXC752+bqu+7IjsHsOLA6LiIsUsl9yCIpSiyemNQaiQpyoK1BPX2v0vs56fgWRNn0CUR1M4xh/YDF0HWHkGFKgahtLlKB8bGLT26vK6gNbHcfuyfsNBfvj4LFEQ5w3mr0X4ft1oXSf66zgWRboxmMcw5XO3/wxddEnlpvOUCarHX8pRM1OW7xVWOaCdqZpu2K+mWTwPdFUAB6OA+SOvwGkfeUXmsH8aG1tTs+eB424J0A7PF39KGGIviFAuIzv2TIOyfGy2NDs+wfXhY7aSyRr/G6GhQlTrgxW/c6oM6eKEgVjb8dw1SvIPcdhRdj8wp3kIiqr2yzFkUzJqShUu6PH1j22bQPvZFiD95VTdrrUMd/GaMLLpv++R53AltNF9cY93Ne8luoVT9y7OgbtObPqlgtHbwuqyViQMOXTevlTZ5e4a1qVueQEtppQGhiqisaFPa8KIHt0gFYet YHGA84p1 xECUfhwEURnsVYwu/yfuylmNvpds9PrmZKN5pu9ImQ8ofTzhi8Fh0nOcEyI5LNf2h51MUjUXdwhIdWe3Dlnc3tiwCj6mHjjVohRzLfMrlO19OFOtQvU2DKfgrXMdBi0VNixzz7537cPtKuIUcu/JtGCwosyomSi4YjKgx3pLRAXQ2FV3lb9HCGw5SGSW4Kh/XKzXHlzNIOdS0LiTTVNuoWJdRbb9euhFQWDWEMLogHRHNluwJbUvZ7kFX6tN3QgoStzqe3LFI8ab4NkQL53Y1Li8smEGD0yC1FNf7CwvfAucuPkrMGH+CpmzRMfJGYwZFoEh2CltIn/OowUQiHqRPWi+zng== 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 +- mm/page_frag_cache.c | 48 ++++++++++++++------------------- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 635b67ceb939..9da7cbd0ee47 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/mm/page_frag_cache.c b/mm/page_frag_cache.c index 64993b5d1243..152ae5dec58a 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -65,9 +65,8 @@ 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; + unsigned int size, offset; struct page *page; - int offset; if (unlikely(!nc->va)) { refill: @@ -75,10 +74,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 +82,25 @@ 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; +#else + size = PAGE_SIZE; +#endif + + offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); + if (unlikely(offset + fragsz > size)) { + /* fragsz is not supposed to be bigger than PAGE_SIZE as we are + * allowing order 3 page allocation to fail easily under low + * memory condition. + */ + if (WARN_ON_ONCE(fragsz > PAGE_SIZE)) + return NULL; + page = virt_to_page(nc->va); if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) @@ -102,33 +111,16 @@ 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)) { - /* - * 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; - } + offset = 0; } nc->pagecnt_bias--; - offset &= align_mask; - nc->offset = offset; + nc->offset = offset + fragsz; return nc->va + offset; } From patchwork Wed May 8 13:34:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13658780 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 E0883C04FFE for ; Wed, 8 May 2024 13:36:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 75E746B0095; Wed, 8 May 2024 09:36:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6E7146B0096; Wed, 8 May 2024 09:36:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 512CE6B0098; Wed, 8 May 2024 09:36:59 -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 270736B0095 for ; Wed, 8 May 2024 09:36:59 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id DFA9AA100C for ; Wed, 8 May 2024 13:36:58 +0000 (UTC) X-FDA: 82095329316.17.E5A70A8 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf01.hostedemail.com (Postfix) with ESMTP id 5EAED4001C for ; Wed, 8 May 2024 13:36:55 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf01.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=1715175417; 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=/TxnzJQKCh9q6U47JvkwZo10jI4vVyNfc4kYz3f+KDA=; b=mNzTumjzuXd5TVlUkuAzTJr9ClgiJWV0N0HbrahMJWvVVRB4bX+0NCKEJ6uaj6FncRCouE teTOpDs7KK8b665ZZwGkIlOM7kCXJ/A2IOYleiWdXiotXsdKJaMKGrDAmApOjf0zok3EHp GG37L7XR8tXzSkMrz7vYeN7bnmXXVAE= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf01.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=1715175417; a=rsa-sha256; cv=none; b=H4LFM9WqOqyPk/UzqUaHuGoL8Ft3IftR6aCnT7PFeJFtngswxIXMeiXwZmdwewtYsjMN3a ft2IrmVX9Pc0YShn1h7omu2C6kcELtuxIYzg0W4yr2TJ8E1+pBh5JQ7uvcRcVUimFdt+xK EJd+4LOFxBlq9A0au/7kqNOJJh7pO8w= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VZGP945DczcdyP; Wed, 8 May 2024 21:35:41 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id AE93814011A; Wed, 8 May 2024 21:36:52 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Wed, 8 May 2024 21:36:52 +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 , 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 v3 06/13] mm: page_frag: add '_va' suffix to page_frag API Date: Wed, 8 May 2024 21:34:01 +0800 Message-ID: <20240508133408.54708-7-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240508133408.54708-1-linyunsheng@huawei.com> References: <20240508133408.54708-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Server: rspam01 X-Stat-Signature: 67oqaox6dtcaoo7xo88ywyizn3xadxr9 X-Rspam-User: X-Rspamd-Queue-Id: 5EAED4001C X-HE-Tag: 1715175415-814891 X-HE-Meta: U2FsdGVkX19V4Ahw93A0lmzlS22BI8kgchoPdTenH9fAmoVkTKHFxd2KNSP9xjN8aJHNtmIy7oqqKPhGUwhDEEUmbT79VWnypEby48vQoPg9Bac8JZ+svmpERzSO+zGE21ETkdNIaEtK+WUnCaO+gCJpZ3LBJbkgI28l2ZAzg/GBXAFngK2q9VIVCIQD6NEI+eXKjK1TOPl1JiNR0zjPblchUNnePdwusS8pgfFxPex//rb90RJ4N0ZqSJjJrCmwtL2+fNrm4U7pgXTotXmnpzop4q8u/bJqFl0UTEFRoWr3yWWJc6h3fk2ANmBYS8/GeGERnwPDfL2O5czd90ek4QiV3JxLE9T6jfjiG7eeT71I71D5jF7F9Hy1le/cD+BRpp2cZ0gGg9B3lTI972gaIbj+446okH4gXAsfV7AC9M7w3bjVRMHeJztAGTCMGiCw9yv0D/vo6e9xI1Xwnyw5QgeMMYU59arvTKvJuaNhPv7sJy6csuqii9/RLF1tMkbfxm2dCF1OK8ovgkuy8/KezZI8LHfQeNHouqP8yVhzCvSB9boo5mmTuuHKzna4H/ToLA1P5Y9t6nb/oG8IqgiWtOHz2T0bHBG7jMRYO2iCHKRTPWQl7MAiLVbDCQxU3lwZPESrfkbH4G1OuZiL33fCxM+dLRLJK6j76bqgnxV3Uo51yQLFDQIwp0+d38fvkJYlZrNYGVFseyo9RlPzhXNsGV7lh7vohPt72i/Q177eL/mthzT6k3TJg6e0/2GOZ9jJfm5u/dNaI3qtfkq48L/pk8I0L5A63y4m7o5IcJv85DuU3xPaX0XKj+5NHAyXvJjC134Lp1DSDyun0wqFx76/7bit7Zn3lK0EV2fyKU7G/jhbATbOdik4AkKXud2kkGEw3R/IuOGJpFw6wUFQoZqPmwOtH25avGtuo3H+fTrWvi5S69s8PMw/4EEeOLRZAZjlT3mGBFuOtp6yqJg5gpo t0POHMBt glhLBkQuVKjeNwlQlQ/JHHrBOZjy2iUM5TA2IXFJFlKz6VmRCLTz62oeezlT4+Ci0Wjtcz/0yI3Q2eIXufpVfRGz1/bgHl2Y+LCiURzhA6WnnUO2/BfkxOvS5LSAcJchiC8vlN2JfY5ygTOkQoGObhq6mOujLhXPmXi11F6f7E43l55J6gm+NQv615QLsHXaGff7wkoIBaYzdS/9qkKLBUJhDc7uO9pSHrvhNY7T9b75P6cUIGBhi0xD8bWMAUOfavLYD9Q7+i7T4jkQtaoSUpSfhW1wnQl2kk3u6 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 | 11 +++++----- net/core/skbuff.c | 18 +++++++-------- net/core/xdp.c | 2 +- net/rxrpc/txbuf.c | 15 +++++++------ net/sunrpc/svcsock.c | 6 ++--- 19 files changed, 74 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 fdbcdcedcee9..79eddd74bfbb 100644 --- a/drivers/nvme/host/tcp.c +++ b/drivers/nvme/host/tcp.c @@ -500,7 +500,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, @@ -514,7 +514,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) @@ -1331,7 +1331,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) @@ -1340,7 +1340,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 a5422e2c979a..ea356ce22672 100644 --- a/drivers/nvme/target/tcp.c +++ b/drivers/nvme/target/tcp.c @@ -1462,24 +1462,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; @@ -1494,20 +1494,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 9da7cbd0ee47..a5747cf7a3a1 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 ce077d14eab6..adaaa478fdce 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -3337,7 +3337,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 152ae5dec58a..c0ecfa733727 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, offset; struct page *page; @@ -124,16 +124,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 f1c861709551..92eb288aab75 100644 --- a/mm/page_frag_test.c +++ b/mm/page_frag_test.c @@ -265,7 +265,7 @@ static int page_frag_pop_thread(void *arg) if (obj) { nr--; - page_frag_free(obj); + page_frag_free_va(obj); } else { cond_resched(); } @@ -295,17 +295,18 @@ static int page_frag_push_thread(void *arg) size = clamp(size, 1U, PAGE_SIZE); if (test_align) - va = page_frag_alloc_align(&test_frag, size, GFP_KERNEL, - SMP_CACHE_BYTES); + va = page_frag_alloc_va_align(&test_frag, size, + GFP_KERNEL, + SMP_CACHE_BYTES); else - va = page_frag_alloc(&test_frag, size, GFP_KERNEL); + va = page_frag_alloc_va(&test_frag, size, 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 May 8 13:34: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: 13658781 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 AAC9CC25B5F for ; Wed, 8 May 2024 13:37:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CDE6B6B0099; Wed, 8 May 2024 09:37:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C3CE26B009A; Wed, 8 May 2024 09:37:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AB6256B009B; Wed, 8 May 2024 09:37:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 886856B0099 for ; Wed, 8 May 2024 09:37:02 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2E0531A13B4 for ; Wed, 8 May 2024 13:37:02 +0000 (UTC) X-FDA: 82095329484.15.C095399 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf11.hostedemail.com (Postfix) with ESMTP id 5C70940019 for ; Wed, 8 May 2024 13:36:58 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf11.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=1715175420; 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=jmdhPoZFOhEQRldy0KvCYacoiZ3gOY6zmhp8dnvz+ek=; b=OB15k1yHrvE8Pnc/+ZQQFOo+mbquGJxWPWZcxnD564KiydEf6anFbjEKykGmVlZf48PlBX lXrtxiiAUeUYD5LDKwJ+R3FZVYnr4p2gWhPrFd9KBV68I64QF/wlG9WrrsBsYqnzU9WeaJ 6Y0hL0blcVmKWXOppmBcJHiQ6I912tw= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf11.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=1715175420; a=rsa-sha256; cv=none; b=JO61W1ln2GiupmdvC4eVZLhPONxRFld5tHtaC2YNFidGJEEbKzIt47m+RkAezbQ07GAKWz +mS5S/AJewWAXU77Ix/9CO/XLKvgT+YFYrFC9NZhUF2LrpcflgoFurEm2UuG3XebgbwY9z w9ftTx7caTRCpyX3m6VpaUtT4HqHY34= Received: from mail.maildlp.com (unknown [172.19.162.254]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VZGLd3YXtztTBx; Wed, 8 May 2024 21:33:29 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id B419C1800B8; Wed, 8 May 2024 21:36:55 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Wed, 8 May 2024 21:36:55 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , "Michael S. Tsirkin" , Jason Wang , Andrew Morton , Eric Dumazet , David Howells , Marc Dionne , Trond Myklebust , Anna Schumaker , Chuck Lever , Jeff Layton , Neil Brown , Olga Kornievskaia , Dai Ngo , Tom Talpey , , , , , Subject: [PATCH net-next v3 07/13] mm: page_frag: avoid caller accessing 'page_frag_cache' directly Date: Wed, 8 May 2024 21:34:02 +0800 Message-ID: <20240508133408.54708-8-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240508133408.54708-1-linyunsheng@huawei.com> References: <20240508133408.54708-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 5C70940019 X-Stat-Signature: h175g9xrs63tet8hm351zwm7ywp7qwet X-HE-Tag: 1715175418-756289 X-HE-Meta: U2FsdGVkX1/H0Lycm3pYQZeqLb67aT1eohOCoIj0ZiFgrPfVQ83O/PiERHXBChsIUyS18eE7S06GeOsRg1tUxAaLQP1TtEBO/zBw3AV/RqDAHKQ1MlhpvCGo2/Uj7F3Zp2yAJUBWfM+sFprY0Y51aCLnBk+WX8GF5C84rnEBIQQdqQpTg14Zm1FajQ8S9BWCOavNVf+YQA7HaCek4vHn1wabqzg70ZCP1m5WZx+ggZZtJDOjEnI328W2tkXUvajDBCNjnX0pwdbH0P6rhzdW8LSbOefngkx5HzsLULMso2iLsG6P1KAlEMCmkTI8X2JG5t23YGDwWkBCeI49WkIde90gBZdlxv88VgCGwxP2ziPdp4asnnxAr1T8FtHxCTIYWbVO5qzZ+/ToTIaq5ffDjAAvKaD9vV/L+7PMRvhVzUdWArrfqcRpj4ynwYHI+Mgo4YlJsxjKW93x0IceNqPv4LCGIJ5EXHVfcqL9UAcIdDpDlcybx3tU78imPEUHUne+LFWqw4ZbH3iaO7HoYysM1ElczOL/vnn/jojHTpR4Ql1rwYWNfPMLc5dP/37dQ0z56O7eweqwBcH6mgKAw2HglK785MzG8NNSOe6VO2hnUf8GcJswHSNsnwJ+u2+iymVDfKfBjREhjuGCvGQD+nGZ1vCSgv68W77jUcPWs0lAj1jTs4HnGBd8YunH/NobUZ7mQoltLn0CMfjJ/wgyfJp43nCQ0GR2GTx25J+ekLgNySpF20FD5TcgTdmxKUSpZko057xnmeGDJiGgBAXRNAmnVHK1vbdp4D1nRGvU0U2pi9WbauA1iHSiJACjbY3ooOYmuS61CfqIimwUhOvf3W9iRxS+DYVfBKo7gg0EPYmKFKJggmRqS1n3Pm3beD2Bc+CppRhcVCV9jwu4GUUj0puYQCITQnfSpjdE31XEVxZlBxwhVXhjdjZD1tqACa2NZs9qo6irFmV8TEU2HzZsPjo BhiTXb3b sbkKXuqS1Dlr/fmJ+RL8GgXEjNWjSsLBqElt4td+cOJA9KnvPEfiRJGDvRa8fz4Ijsx30PBBwCJj9iUQ62H3JBkSARlinHyuV2B38JmnmBUkxAV56OQ7/kIVlhhJT6/Wvhg4TPZ0yIFBVZbVQIbf2U+KJsKSzTsKe8yFemoHPwdXa8D3SqekXgBvHgZzg8FBsGRAsDreEGswqh45Gt5FwwZ2ypiAINTxvZL60C4kQe1nXtAFCPaJ7tE1c7EB5keoKHmeo8rzDGCJDpxKe4BgMUJvHJDHy4vnEbgd9 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 a5747cf7a3a1..024ff73a7ea4 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 92eb288aab75..8a974d0588bf 100644 --- a/mm/page_frag_test.c +++ b/mm/page_frag_test.c @@ -329,7 +329,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 May 8 13:34:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13658782 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 28583C19F4F for ; Wed, 8 May 2024 13:37:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1DFD46B009A; Wed, 8 May 2024 09:37:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 18E8D6B009B; Wed, 8 May 2024 09:37:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E88CC6B009C; Wed, 8 May 2024 09:37:03 -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 CDEE86B009A for ; Wed, 8 May 2024 09:37:03 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 34AAC1610C1 for ; Wed, 8 May 2024 13:37:03 +0000 (UTC) X-FDA: 82095329526.13.E861A9E Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf30.hostedemail.com (Postfix) with ESMTP id C22FC80019 for ; Wed, 8 May 2024 13:37:00 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=none; spf=pass (imf30.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715175421; a=rsa-sha256; cv=none; b=KLfJALd6OvwbecNgw/GXLtO2clhk/nXDe4hJjj1+UFO/tGjPuyzdhWPicfnlrJnaiqoU5v N10VDl7A2k+x7wePyFRR8QXgDn815hCF7AtE9MfML6+t2o4xRPyTu/PtKzb33U+uvsPOL9 HLFjWnYIBpb2IJHciAz3C4xNAWuXbQQ= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=none; spf=pass (imf30.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=1715175421; 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=jz6DdCwaHkVa1xVR2gHKLiUsYrKVMN4s2C9LLLAJkak=; b=Oh5mo9VRdneaAf9gx4WKF7E6DtXgKSBCYD59C6HWl5oiu7x0iTGgdkcrq0Aj0Powys7z// ClIng7TUmootDVKP98xTXVrtnYNLpkeZdf2Sq/2pI020RIZC0V1X+7ip0Vohg5M+2CdvY1 Wto3eRe1cQafiBc7rFVqzP+wWclmkB4= Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4VZGLn55CNz1RBtL; Wed, 8 May 2024 21:33:37 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id A857D18007E; Wed, 8 May 2024 21:36:57 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Wed, 8 May 2024 21:36:57 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v3 08/13] mm: page_frag: reuse existing space for 'size' and 'pfmemalloc' Date: Wed, 8 May 2024 21:34:03 +0800 Message-ID: <20240508133408.54708-9-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240508133408.54708-1-linyunsheng@huawei.com> References: <20240508133408.54708-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500005.china.huawei.com (7.185.36.74) X-Stat-Signature: bn795yi6puud6qf7cgr8oxqqa44f56pi X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: C22FC80019 X-HE-Tag: 1715175420-642902 X-HE-Meta: U2FsdGVkX1/ho3htByVHgsjpSz1ajMfCPVlOXNy31uaLiBoxOKmD0qEl3Cr5nuDn/VL+rG2plvWhXi6O4WTmjRZLPIi3llDoyXxy9lmfBCq3gERauRFT5B+2DQJGZUuXm8qXkwfGFlxkAuecoZgXLwDfMDMCl7Y6ea8IoORoFwJtzpcbHW6OdFoG50iYbXXanOIrBn/NWeNCfG6fsVtm4TwjpGDPjElJgiRfKEPmCoxO9drf6cvlHKG/XNPLeTAzhhdDqWYhz5B+P8FPDTVccrJhxhSS/UEZDJZmb0QceGhl0qt1t0BiYacX1uVtTfUpI92kLLWVShDIMt0qkDw8DfXZAd1iy9US8LzIS6a4sszDjiOZqMP6z4KTq/GSKMQUxQCgyW+YHypQEcaGc1YOjolEbDmzeEayu7Zyf16KeYpT8dkcMptovX8m1ro6XqI7K4OVPQfPxTRYRBspN8L6hdzMV4vd7Gpa69EKQvfi6Mj+l3bNDUTNGMEBec6L+ibGbNF49IA1LrgPCXaMPk/2i/mrGPvHNjj0sVGy1qj3mYDP+xjEIV2UW2OA3l6I2ap/0CUFv4nEwJT4WTfL18rq2w7yAPPzRgGXUrKWfdsl1mjjIW+zb23+a+DCHzJGc8wvCPy/Awv9OyTte+rmsmSWqSoRRb9B7fP1rnmtIgH4m+02nxarOcA8kKV2jESAw0PJ4uiAwNWNXmLr7aXu/iN/zM3CwLUKcKNYO1aWg+rIhMyD+uNxn6RhdHlTnfOLbsFSwGN6ZbpIt47WaaPGn36vcUQQDYdLrBYjeu22FP3iaWpBmPwSSd3qr/UvYbV2wRzHb2CNGEM6+zF55S68Uy6VwHJk7ihKin/+3Uvjtl50k1Dd1EajoISkNQ0Dp8MKtpdZULf4YeJeRjl7yzYRzyhEuEpnZr1H+ZMgCc0+Ev4zmefBm7RAHPf1S/rNVszuGhoGKzHeED1LT9BV5RWm30U DAvXtCDk WcIf/HCnejjihYJrBv6zC49YNxz/bSvw3Zv4CN7ZIMV9Bw0abzHdkEz3BouE8VJr5m6v0GcqjNhJoUDxDb7CgGBhAe3oDQtiMUq7zm5DQ8EmfZbkw177wha1sxanrBxiRp2JURGMvUNM2hFvLOYT2M0XTqi+ZXik2TDzXaz12djKA0kSDZz5J3d6o72wx8BF/gBjUXxUJqKzNdclXD2CW/+mj/PVQDRICGT6IQ8pfbDdsSO+XathtrASLP+E9g9cjgRvfHT6qT+TbihSflQ9KcmGKi1Wd8+OASnlz 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 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 | 62 +++++++++++++++++---- mm/page_frag_cache.c | 98 ++++++++++++++++++++------------- 2 files changed, 111 insertions(+), 49 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 024ff73a7ea4..88e91ee57b91 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -8,29 +8,67 @@ #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 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) +{ + return (struct encoded_va *)((unsigned long)va | order | + pfmemalloc << PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT); +} + +static inline unsigned long encoded_page_order(struct encoded_va *encoded_va) +{ + return PAGE_FRAG_CACHE_ORDER_MASK & (unsigned long)encoded_va; +} + +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 { - void *va; -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - __u16 offset; - __u16 size; + struct encoded_va *encoded_va; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) && (BITS_PER_LONG <= 32) + u16 pagecnt_bias; + u16 remaining; #else - __u32 offset; + 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); } 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 c0ecfa733727..4542d72e7b01 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -22,6 +22,7 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, gfp_t gfp_mask) { struct page *page = NULL; + unsigned int size, order; gfp_t gfp = gfp_mask; #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) @@ -32,23 +33,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)) + goto alloc_failed; + + size = PAGE_SIZE; + order = 0; + } else { + size = PAGE_FRAG_CACHE_MAX_SIZE; + order = PAGE_FRAG_CACHE_MAX_ORDER; + } - nc->va = page ? page_address(page) : NULL; + nc->encoded_va = encode_aligned_va(page_address(page), order, + page_is_pfmemalloc(page)); + nc->remaining = size; + page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); + nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; return page; + +alloc_failed: + nc->encoded_va = NULL; + nc->remaining = 0; + return NULL; } 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,35 +84,32 @@ 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, offset; + unsigned int remaining, page_size; + struct encoded_va *encoded_va; +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + unsigned long page_order; +#endif struct page *page; - if (unlikely(!nc->va)) { -refill: - page = __page_frag_cache_refill(nc, gfp_mask); - if (!page) - 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; - } - +alloc_fragment: + remaining = nc->remaining & align_mask; + 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; + page_order = encoded_page_order(encoded_va); + page_size = PAGE_SIZE << page_order; #else - size = PAGE_SIZE; + page_size = PAGE_SIZE; #endif - offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); - if (unlikely(offset + fragsz > size)) { + if (unlikely(fragsz > remaining)) { + if (unlikely(!encoded_va)) { + page = __page_frag_cache_refill(nc, gfp_mask); + if (page) + goto alloc_fragment; + + return NULL; + } + /* fragsz is not supposed to be bigger than PAGE_SIZE as we are * allowing order 3 page allocation to fail easily under low * memory condition. @@ -101,14 +117,22 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, if (WARN_ON_ONCE(fragsz > PAGE_SIZE)) return NULL; - page = virt_to_page(nc->va); + page = virt_to_page(encoded_va); + if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) { + page = __page_frag_cache_refill(nc, gfp_mask); + if (page) + goto alloc_fragment; - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) - goto refill; + return NULL; + } - if (unlikely(nc->pfmemalloc)) { + if (unlikely(encoded_page_pfmemalloc(encoded_va))) { free_unref_page(page, compound_order(page)); - goto refill; + page = __page_frag_cache_refill(nc, gfp_mask); + if (page) + goto alloc_fragment; + + return NULL; } /* OK, page count is 0, we can safely set it */ @@ -116,13 +140,13 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset = 0; + remaining = page_size; } + nc->remaining = remaining - fragsz; nc->pagecnt_bias--; - nc->offset = offset + fragsz; - return nc->va + offset; + return encoded_page_address(encoded_va) + (page_size - remaining); } EXPORT_SYMBOL(__page_frag_alloc_va_align); From patchwork Wed May 8 13:34:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13658783 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 59233C25B77 for ; Wed, 8 May 2024 13:37:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C2436B009C; Wed, 8 May 2024 09:37:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 64A9D6B009D; Wed, 8 May 2024 09:37:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 474DD6B009E; Wed, 8 May 2024 09:37:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 2389F6B009C for ; Wed, 8 May 2024 09:37:07 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id CB05480B69 for ; Wed, 8 May 2024 13:37:06 +0000 (UTC) X-FDA: 82095329652.26.98F0E65 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf26.hostedemail.com (Postfix) with ESMTP id 5DF0B140018 for ; Wed, 8 May 2024 13:37:03 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=none; spf=pass (imf26.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=1715175425; 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=XmxFd4GVUpHCH3X0wrdYhDw48JyEbQ+ZDWgVHv/4wK4=; b=DAG9U1rbKumaN/LC82JdICVYPfZT1Tp179ImEvhr2lagHW67ZvDEIlannq0pciBV91udao mm11HtTMe0s4t2CYaMIgHBulhpTYc4qSP5eUDvKKfZNslSKmDEBdt/4783Qz0yEG/ocUTM GLDh0l4vzDjuXS73qQAsT5q0266WEH0= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; spf=pass (imf26.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=1715175425; a=rsa-sha256; cv=none; b=SiJxWPa57zFIaFPI59RycLWDWRB5T2h5myA0dxulRYEUOy7xmsbRTOMre4aARydXO8ob7N pzlmWu+YAw55/6bYroWZX1RBkenqscGVx3++LjvQgvHIFwYZjdNNtqg30VGiD4+tj2HZ2/ 2/PYCqHPtwfwa2XtyP7yHMkrRcKhRXc= Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VZGLl2BwMztT5f; Wed, 8 May 2024 21:33:35 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 89D8218007B; Wed, 8 May 2024 21:37: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.35; Wed, 8 May 2024 21:37:01 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v3 10/13] mm: page_frag: introduce prepare/probe/commit API Date: Wed, 8 May 2024 21:34:05 +0800 Message-ID: <20240508133408.54708-11-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240508133408.54708-1-linyunsheng@huawei.com> References: <20240508133408.54708-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspam-User: X-Rspamd-Queue-Id: 5DF0B140018 X-Rspamd-Server: rspam06 X-Stat-Signature: baabrmrncwfhjtqjxaajh9csogh8oh78 X-HE-Tag: 1715175423-716361 X-HE-Meta: U2FsdGVkX1/ClBmJbQl4+f9A47uiLicDirchHmn69/t1POTF7FnatphJ37jcdkPDA2QjxV9nXpT8aXB0Hfhl1hX5Zw95IMfCtSdOE/4pXVDGvAWYZbtV5hvtRGE9Qaq+DUUG412inauVmZCnwPKpvp2hy4NlvJcW7cdLJTlRRUyNfpcYy1lx/HTTpUHadxsswb/U8IctdgdOwN/MvChzwxUQAX4UDlRGyVReQcV4XokjYaNj7IfrSGILoioOMd4id7/t35+vEfYFaY0L++rKOBzk1ty3Wv7KwMhmqLCqrYNqsZtkMpz+0zid8XEUEgBb3gM1wQsLVbGYzgfW8pYEglLpwtsAXppt5fjPmmGlsiLGbl/mh+u7OhUhr0lyC+uBCPdZ8pud+fIBeXkqzS/samz3KV/iewzZc/H/6sJdnHovxBZxa3NPvqI0dgAIfRSNWdxbvFFrLiEJagPTsHS2uNiKQZae+7JzTFSl04qXM0JyU6feEFdrZjaRpAoebfO3BfxhueCj7Xke4FJfcXv3Sd5vUw7kKtU3TH/ARfpbEKW213MzDZfMP2H8jJq3jcSMhZ+1KAgs6YKhZoJ1D/oNnjoyvhUewy1z897ZpUGlMHzChC8TsQTau+FR+QSw5xCcOigTN/TR/88Q9xnlAmj+/fuohrbK6WrvQufn4gV3RfzDAfLFqyCqfxR4eA+9kuSDNi2V9DoLbzKA3w8NIEACjV2eMOrOoTJoAdPoVgCvcTIPQqj2BcqkOflNdWQDCGQeV4V/Uv8VfmgHCR7hQLEGuOy74aKioxQFKSptXMKJnEvO4ocAQVTZ2Z5x7a7yBBiw30oT//f8/8WKsNe8TZUpuwjv6kN3HIA62lhsAtJYWrFw5tMbB1+1DJuGYMrcTJj+IQE5rXyTF/zV/ite0zXxSdFgO3OVfl8BnM9YjotmqXICZGAs7TQCGkWRsA0mxCbRWDEUSp6Vlox/ZNfmgm2 8cuw6Liv i7l8wAqxGpVeEBTfgg4awr3RxdslLzSIM2q5qEIBWcXHIBJruzNhVy71IVf6YiSuNO0AW0jiwDG1j1y2BunuXOpYFcFG1DcJ5g+4uJh9abySIIRfzX8QQNhZRGH8s1XNMJk02j7Ghc4yV2j+cmT7+C+g1yKI0S6RVWm5zHPrbafb00bXqAEZk2DKaqKaiPcslWo5yBln4DqlgCCSziBUeL+0iCxjeWtcsaJmIaAIPRk0jejfuVk+QqKW4JmCwGB+9lA7iN374jneYSrhh0e7QeqPUxqex9ewc9NQv2H06MdwMXKyKrA1iBPIn+VPmYTCeoh2D 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 progressing, 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 usecases, caller need 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 | 86 ++++++++++++++++++++++++ mm/page_frag_cache.c | 113 ++++++++++++++++++++++++++++++++ 2 files changed, 199 insertions(+) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 88e91ee57b91..30893638155b 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -71,6 +71,21 @@ static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc) return encoded_page_pfmemalloc(nc->encoded_va); } +static inline unsigned int page_frag_cache_page_size(struct encoded_va *encoded_va) +{ +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + return PAGE_SIZE << encoded_page_order(encoded_va); +#else + return PAGE_SIZE; +#endif +} + +static inline unsigned int __page_frag_cache_page_offset(struct encoded_va *encoded_va, + unsigned int remaining) +{ + return page_frag_cache_page_size(encoded_va) - remaining; +} + 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, @@ -85,12 +100,83 @@ static inline void *page_frag_alloc_va_align(struct page_frag_cache *nc, return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, -align); } +static inline unsigned int page_frag_cache_page_offset(const struct page_frag_cache *nc) +{ + return __page_frag_cache_page_offset(nc->encoded_va, nc->remaining); +} + static inline void *page_frag_alloc_va(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask) { return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, ~0u); } +void *page_frag_alloc_va_prepare(struct page_frag_cache *nc, unsigned int *fragsz, + gfp_t gfp); + +static inline void *page_frag_alloc_va_prepare_align(struct page_frag_cache *nc, + unsigned int *fragsz, + gfp_t gfp, + unsigned int align) +{ + WARN_ON_ONCE(!is_power_of_2(align) || align > PAGE_SIZE); + nc->remaining = nc->remaining & -align; + return page_frag_alloc_va_prepare(nc, fragsz, gfp); +} + +struct page *page_frag_alloc_pg_prepare(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *fragsz, gfp_t gfp); + +struct page *page_frag_alloc_prepare(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *fragsz, + void **va, gfp_t gfp); + +static inline struct encoded_va *__page_frag_alloc_probe(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *fragsz, + void **va) +{ + struct encoded_va *encoded_va; + + *fragsz = nc->remaining; + encoded_va = nc->encoded_va; + *offset = __page_frag_cache_page_offset(encoded_va, *fragsz); + *va = encoded_page_address(encoded_va) + *offset; + + return encoded_va; +} + +#define page_frag_alloc_probe(nc, offset, fragsz, va) \ +({ \ + struct encoded_va *__encoded_va; \ + struct page *__page = NULL; \ + \ + if (likely((nc)->remaining)) \ + __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; +} + void page_frag_free_va(void *addr); #endif diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 4542d72e7b01..eb8bf59b26bb 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -60,6 +60,119 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, return NULL; } +static struct page *page_frag_cache_refill(struct page_frag_cache *nc, + gfp_t gfp_mask) +{ + struct encoded_va *encoded_va = nc->encoded_va; + + if (likely(encoded_va)) { + struct page *page = virt_to_page(encoded_va); + + if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) + return __page_frag_cache_refill(nc, gfp_mask); + + if (unlikely(encoded_page_pfmemalloc(encoded_va))) { + free_unref_page(page, compound_order(page)); + return __page_frag_cache_refill(nc, gfp_mask); + } + + /* OK, page count is 0, we can safely set it */ + set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); + + /* reset page count bias and offset to start of new frag */ + nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; + nc->remaining = page_frag_cache_page_size(encoded_va); + + return page; + } + + return __page_frag_cache_refill(nc, gfp_mask); +} + +void *page_frag_alloc_va_prepare(struct page_frag_cache *nc, + unsigned int *fragsz, gfp_t gfp) +{ + struct encoded_va *encoded_va; + unsigned int remaining; + + remaining = nc->remaining; + if (unlikely(*fragsz > remaining)) { + if (WARN_ON_ONCE(*fragsz > PAGE_SIZE) || + !page_frag_cache_refill(nc, gfp)) + return NULL; + + remaining = nc->remaining; + } + + encoded_va = nc->encoded_va; + *fragsz = remaining; + return encoded_page_address(encoded_va) + + __page_frag_cache_page_offset(encoded_va, remaining); +} +EXPORT_SYMBOL(page_frag_alloc_va_prepare); + +struct page *page_frag_alloc_pg_prepare(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *fragsz, gfp_t gfp) +{ + struct encoded_va *encoded_va; + unsigned int remaining; + struct page *page; + + remaining = nc->remaining; + if (unlikely(*fragsz > remaining)) { + if (WARN_ON_ONCE(*fragsz > PAGE_SIZE)) { + *fragsz = 0; + return NULL; + } + + page = page_frag_cache_refill(nc, gfp); + remaining = nc->remaining; + encoded_va = nc->encoded_va; + } else { + encoded_va = nc->encoded_va; + page = virt_to_page(encoded_va); + } + + *offset = __page_frag_cache_page_offset(encoded_va, remaining); + *fragsz = remaining; + + return page; +} +EXPORT_SYMBOL(page_frag_alloc_pg_prepare); + +struct page *page_frag_alloc_prepare(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *fragsz, + void **va, gfp_t gfp) +{ + struct encoded_va *encoded_va; + unsigned int remaining; + struct page *page; + + remaining = nc->remaining; + if (unlikely(*fragsz > remaining)) { + if (WARN_ON_ONCE(*fragsz > PAGE_SIZE)) { + *fragsz = 0; + return NULL; + } + + page = page_frag_cache_refill(nc, gfp); + remaining = nc->remaining; + encoded_va = nc->encoded_va; + } else { + encoded_va = nc->encoded_va; + page = virt_to_page(encoded_va); + } + + *offset = __page_frag_cache_page_offset(encoded_va, remaining); + *fragsz = remaining; + *va = encoded_page_address(encoded_va) + *offset; + + return page; +} +EXPORT_SYMBOL(page_frag_alloc_prepare); + void page_frag_cache_drain(struct page_frag_cache *nc) { if (!nc->encoded_va) From patchwork Wed May 8 13:34:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13658784 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 81E23C25B5F for ; Wed, 8 May 2024 13:37:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E629A6B00A0; Wed, 8 May 2024 09:37:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DEF8E6B00A3; Wed, 8 May 2024 09:37:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C72EF6B00A0; Wed, 8 May 2024 09:37:16 -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 9E5856B00A0 for ; Wed, 8 May 2024 09:37:16 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 056CC1A01D9 for ; Wed, 8 May 2024 13:37:15 +0000 (UTC) X-FDA: 82095330072.30.43772A6 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf27.hostedemail.com (Postfix) with ESMTP id 79D8B40021 for ; Wed, 8 May 2024 13:37:13 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf27.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=1715175434; 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=++lYCyycjKwTelcxQZykLT1jlUBj2DdbFG0CK280oZY=; b=Byk2C/6653nfFh3GfPEezynja6g4bnyO5cNEVmr7tCRCkp6n2hsr70smkgK2impq0asQ3W kwj2oJv0xmbTTgtXaE+p+J2Omq6zA/wBJp/jKwiTo0zhGDi79KELzzfxa7He2QlXqsZlx6 EzETjySnj9h3AIVT1s4Gw7lh6fhgaJo= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf27.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=1715175434; a=rsa-sha256; cv=none; b=qh+D834k5N1/hZKxipcjLoo7u17xdmkf6DB1jSNe/NLEPqXXuuhR1WqPKSsszCIl65NJSI fDx8vkDogNnU1HwEp+2h76SWAqA06hEYshe5690JDCPWWlnwMdblq0pS15vx8QTo2l0JhY BL5osq03/+4Z/6Lpi0BlfHH9phFNmik= Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VZGLw4HtLztT5f; Wed, 8 May 2024 21:33:44 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id C8D86180A9F; Wed, 8 May 2024 21:37: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.35; Wed, 8 May 2024 21:37:10 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Jonathan Corbet , Andrew Morton , , Subject: [PATCH net-next v3 12/13] mm: page_frag: update documentation for page_frag Date: Wed, 8 May 2024 21:34:07 +0800 Message-ID: <20240508133408.54708-13-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240508133408.54708-1-linyunsheng@huawei.com> References: <20240508133408.54708-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500005.china.huawei.com (7.185.36.74) X-Rspamd-Server: rspam01 X-Stat-Signature: t1i5yozrs13354hkd3mesahirm8jzjhk X-Rspam-User: X-Rspamd-Queue-Id: 79D8B40021 X-HE-Tag: 1715175433-484625 X-HE-Meta: U2FsdGVkX18fhaCdbCCAG/aCzMbsv6IRZZPeZOK0r/J8CDiVugg9l8wW1NxE1XP6xqCjbo31vAgSByYAhSPhkeKqaDvQ3LIBuTSEP7gUfu8cVl3UZF3RG7QA4KMYpWBMdAwuhyol3oIo6C8wOODwdPHNYCW8c7RnpEe6UenMGYIj57+ZVeYuldfbQ4K4VW9vEqU0iYvffkx5dVp3ey7kStdw3BiEOLKEAFkOVaH8m5OGV1lChYTQEeOvLm2VcGGD0X6+AmjZ/lOLHwHRbnvcOCzBA1F3OcAtmBgUlCTY2KCPFYjhqGMElsLbH+JVnVDfY4TvWSWOhS7PBUiqfY68BNU7h48U1pzCTiI/7CNKALTnDDKqE5xzbfCp44gwvrvBB8Ye1ogYWCt2m3HXIzwOBvCA/fn5P1st49LPFwVPbRxlo+UM7sduiQhLXxVy5G2fYQfJvIwHCxgk5QUZ9mcweUuJmrltBpkhTGj2zx3DUfLQuyW+M5IbmJKBZc8wErORUa+33EMIf8LJp9+T8ETfe2H9UeacL/Cba58Jm1/7eMbOopKR82+UKvCm2Eutu31DmjUmSa4KCUniBPpe5heYeZCskojqEMlyN4f3WMpSzmxxCtFsQ5MiNKLXmuEb3Kxya/WzuztnaBzIqg+XPsV2pw1jQOFyc+5loY0e3BH8LDaaAwSucRQkA/oEajzsL27DKXwmij+PiTp+ezMr1pGcF25ndb/onwy/Bnzn1zY+Nr8XGnquKFU1Mzpmi2jvNapfXvp1Lxnkxlt6WUUWyuUa57ZEvzOEt04bLoomJN5kXX77nY3RamXTqlLvJ65XfbzHfDZCC7rEbm1PSZ7d0c4EkcUWsjt352/bAhDAPEOPrEbc+8zv1rXZXIOh4LjrBu7XfK2Gp6BxswDusoZlNPTLEfnhLHnQAiE9mbuXeCTkX9G6xFRcWavf9dtE5qINVnEnzlWyVR80CUdixkYnlas J0QjL1EF m4VAkprJDJ6UhknCOWHCUldNXhhUV9R+DuhAh+7kGeEzK1zUqPulx6sHVMQyvAfo0lzN6H1v2NFU/1OkcU+psW+LHDjKOEpbHRpGHcOuZjGynQV2UzR2NwxWGBciWkC92/0O1VXMYYsK3EtmzMj0LI/D3PRrq5FdiJ1IcxNiX/otL23zv1I6onzwES0dMFzbedWtOMU4YbYIWnVMbT1o2tMvM8wd40a5IFbzyEMyhMXGcRp5bLwpgBUkemfWZlPTCJZ/uwdZq3ccFvSkVzo0tEIjLbt9cM684nr2OanAJBLwpneK65/BUP9Op72J5u/qZvB/Ta8sDCZ81Ss8= 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 | 156 +++++++++++++++++++++++++++++++- include/linux/page_frag_cache.h | 96 ++++++++++++++++++++ mm/page_frag_cache.c | 65 ++++++++++++- 3 files changed, 314 insertions(+), 3 deletions(-) diff --git a/Documentation/mm/page_frags.rst b/Documentation/mm/page_frags.rst index 503ca6cdb804..9c25c0fd81f0 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,156 @@ 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 | + | Cache empty v | + | +-----------------+ | + | | drain old cache | | + | +-----------------+ | + | ^ | + | | | + v v | + +----------------------------------+ | + | refill cache with order 3 page | | + +----------------------------------+ | + ^ ^ | + | | | + | | Refill failed | + | | | Cache is enough + | | | + | 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 need to provide a aligned +fragsz if there is a 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 need minimum memory in order for forward +progressing, 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 need 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 + +.. kernel-doc:: mm/page_frag_cache.c + :identifiers: __page_frag_alloc_va_align 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(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 30893638155b..8925397262a1 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -61,11 +61,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: + * 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); @@ -92,6 +109,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 need to be refilled + * @align: the requested aligning requirement for 'va' + * + * WARN_ON_ONCE() checking for 'align' before allocing a page fragment from + * page_frag cache with aligning requirement for 'va'. + * + * Return: + * Return va 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) @@ -100,11 +130,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: + * 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_offset(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: + * Return va 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) { @@ -114,6 +165,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 for 'va' + * + * 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: + * Return va 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, @@ -148,6 +214,19 @@ static inline struct encoded_va *__page_frag_alloc_probe(struct page_frag_cache return encoded_va; } +/** + * page_frag_alloc_probe - Probe the avaiable 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 the cache is empty, return NULL. + * + * Return: + * Return the page fragment, otherwise return NULL. + */ #define page_frag_alloc_probe(nc, offset, fragsz, va) \ ({ \ struct encoded_va *__encoded_va; \ @@ -162,6 +241,13 @@ 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 alloc preparing by passing the actual used size. + */ static inline void page_frag_alloc_commit(struct page_frag_cache *nc, unsigned int fragsz) { @@ -170,6 +256,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 by passing the actual used size, but not taking + * page refcount. Mostly used for fragmemt coaleasing case when the current + * fragmemt can share the same refcount with previous fragmemt. + */ static inline void page_frag_alloc_commit_noref(struct page_frag_cache *nc, unsigned int fragsz) { diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index eb8bf59b26bb..85e23d5cbdcc 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -89,6 +89,18 @@ static struct page *page_frag_cache_refill(struct page_frag_cache *nc, return __page_frag_cache_refill(nc, gfp_mask); } +/** + * 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 need 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: + * Return va of the page fragment, otherwise return NULL. + */ void *page_frag_alloc_va_prepare(struct page_frag_cache *nc, unsigned int *fragsz, gfp_t gfp) { @@ -111,6 +123,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 need 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: + * 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) @@ -141,6 +166,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 need 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 'page' and 'va' + * of the fragment to the caller. + * + * Return: + * Return the page fragment, otherwise return NULL. + */ struct page *page_frag_alloc_prepare(struct page_frag_cache *nc, unsigned int *offset, unsigned int *fragsz, @@ -173,6 +213,10 @@ struct page *page_frag_alloc_prepare(struct page_frag_cache *nc, } EXPORT_SYMBOL(page_frag_alloc_prepare); +/** + * 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) @@ -193,6 +237,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 +320,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) {