From patchwork Fri Jun 7 12:38:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13689827 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 52757C27C53 for ; Fri, 7 Jun 2024 12:41:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D2F736B00A1; Fri, 7 Jun 2024 08:41:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CDF816B00A6; Fri, 7 Jun 2024 08:41:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B81546B00A5; Fri, 7 Jun 2024 08:41:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 9BA296B0082 for ; Fri, 7 Jun 2024 08:41:15 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 57616A360F for ; Fri, 7 Jun 2024 12:41:15 +0000 (UTC) X-FDA: 82204052910.17.6FBA5D8 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf27.hostedemail.com (Postfix) with ESMTP id 6067B40016 for ; Fri, 7 Jun 2024 12:41:12 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717764073; 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=UPvuztxdqx5cJtBbGNXwBv5MJr8uqNXPU7nDB/1d2ZM=; b=PO+Ofs1ZFlobzF2BB5WseI2YaGhfpkQ94I4w1sEKNLLDqJuHizmqR1pfktxBmr8GneNyUU iid55UOth6OuXy/DOtnbDf/OaNuBHDY0tlE4fmSmKlkZsmTTpJ+fp4RGOgr0DYOTV9k1U5 AjzcolMNZ/DMBXEvUg+e0EftG2Q0hRE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717764073; a=rsa-sha256; cv=none; b=jzzt2C8ylJmqYofIpmzpGQ/5Er4hye2rnbeEgPBIrQFvIeZ5OWnmy36ty39zEYd2B7UGo5 gAwPrp4Je9+gx641PrbZ9ymw/tnOBQv4dVv55byLPmGQ4HqbDat2dn0dXBKhhnyAfQOJHU jkDonatebIwQZv1yFV09zkPeIwOvDG8= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4Vwggm1z6qz1S4tR; Fri, 7 Jun 2024 20:37:08 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 79C7E14011A; Fri, 7 Jun 2024 20:41:07 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 7 Jun 2024 20:41:07 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v7 01/15] mm: page_frag: add a test module for page_frag Date: Fri, 7 Jun 2024 20:38:04 +0800 Message-ID: <20240607123819.40694-2-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240607123819.40694-1-linyunsheng@huawei.com> References: <20240607123819.40694-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspam-User: X-Stat-Signature: wdjhk3kt8hbkiohyric7y1xu5w4gi9yx X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 6067B40016 X-HE-Tag: 1717764072-108492 X-HE-Meta: U2FsdGVkX18HX99ZZlkUmdiRAWEQpazEcish74s1ds0ye+yPCj04zGyb2SInCHwRy1/Fnm+1wP0Gm7aV1REitfx6Q4ibY+8hHWxoFT6bn0ScisPoRG8pVmuae6/UEdcbLGkjBQFgRIBtStASpl4Mdh84TS5f5UA8yO1w27xmwMr4GTP22jbGc/sX7tDVqxYoXWkx/e8sZrM/21dwcN2uiI/UAI8gDcuA+enbHvYhpwODnROSNVS3OLxZIhHrZFan7RAPo0AaQ22HZyprYeysb9BxL7bE39/pJogoPcWHU/x04bM0tL871lBlAzuZeLDcOqbsqEdRTXlQa2DicEDIgjHOVK+gcJjUXxgAII+FGLjg4fKhHEhNHx/zjnZNvhh4i8b9aHJrnQbkSXtZ/Fl7mUmO5f57jYV4mm7vInUVaBQQRGefstJ9y4Tqc5IHPqz2o1is/OJobOzAErQzu1gwJJB7SF5DjrwFS65sukh9FF1s+R/X7CxobUddJkR2X0l4AEVzignu973c0G6crYFjM7GIJEc4WxmRvataVGG3KdXxYsMxTKgyuT7m8XsTW+dG+D+Z396CLxPjtaWSNDVtaHwr38XIWenqKcn4DxLDjDeVwOb6RhL75XN97aNQek6WU7UyRoIoI1TPgVUA+TmL12gyH+1oTNsXlfW9OPND7Jap2MMz+TmSDTMQ0DVUGywNFbHLyoI3JoYpF7JmyvZ+j9qTGHXetQGOKavtoqEpgsBmn7BU6geAjZEeMBbvnSTcXOyvLHgbPuzVbACgnBkI5h0FmtRwdK4h6YPxd6npOhtSE6ZSKKMqqKNyuVkgsFjr5k6/0yV+Pv6g02R8N8lBnDOX4Btu+xWQ164qWHu/6msa0mZnGBAFZ46bu+XlKLIYQJHZCLEl+tCPyLCzh0DUPtVlWMRKfAWDzigzhYrSrsun02k68zAEpdFmnZAkfJ8VyA2q8saFn+6ABn9t+FF wBWjG44d ZKAZYGwsxNb4E9tfYzovlu+QXB4Dmw8sR0yyvawne0LW6YHHMXbD6BRKF2yFC1AhGItlNSZKgcqN5CDuE5l0faERHZbxvMpIxV82QBBiAzy5aoUop5Ict8UyrCHNZnpPRTT59FDTEOyM7IZG7eV7TfkektnU1ft2ZhXycsteuU0wahi9U9vFZ5ghpjPzALwAdcah0700mXWOUoa8PTtzSdRTOtNeh6IDJLcMIWOOlyyr1uYX9fPUfNp1320oTCbtqDjsYT7MOTu8NYYtTV3Zg2+XHtr6l3xVTxe06fcu3qJO8to5tqvBOszRySQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Basing on the lib/objpool.c, change it to something like a ptrpool, so that we can utilize that to test the correctness and performance of the page_frag. The testing is done by ensuring that the fragment allocated from a frag_frag_cache instance is pushed into a ptrpool instance in a kthread binded to a specified cpu, and a kthread binded to a specified cpu will pop the fragment from the ptrpool and free the fragment. We may refactor out the common part between objpool and ptrpool if this ptrpool thing turns out to be helpful for other place. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- mm/Kconfig.debug | 8 + mm/Makefile | 1 + mm/page_frag_test.c | 386 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 395 insertions(+) create mode 100644 mm/page_frag_test.c diff --git a/mm/Kconfig.debug b/mm/Kconfig.debug index afc72fde0f03..1ebcd45f47d4 100644 --- a/mm/Kconfig.debug +++ b/mm/Kconfig.debug @@ -142,6 +142,14 @@ config DEBUG_PAGE_REF kernel code. However the runtime performance overhead is virtually nil until the tracepoints are actually enabled. +config DEBUG_PAGE_FRAG_TEST + tristate "Test module for page_frag" + default n + depends on m && DEBUG_KERNEL + help + This builds the "page_frag_test" module that is used to test the + correctness and performance of page_frag's implementation. + config DEBUG_RODATA_TEST bool "Testcase for the marking rodata read-only" depends on STRICT_KERNEL_RWX diff --git a/mm/Makefile b/mm/Makefile index 8fb85acda1b1..29d9f7618a33 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -106,6 +106,7 @@ obj-$(CONFIG_MEMORY_FAILURE) += memory-failure.o obj-$(CONFIG_HWPOISON_INJECT) += hwpoison-inject.o obj-$(CONFIG_DEBUG_KMEMLEAK) += kmemleak.o obj-$(CONFIG_DEBUG_RODATA_TEST) += rodata_test.o +obj-$(CONFIG_DEBUG_PAGE_FRAG_TEST) += page_frag_test.o obj-$(CONFIG_DEBUG_VM_PGTABLE) += debug_vm_pgtable.o obj-$(CONFIG_PAGE_OWNER) += page_owner.o obj-$(CONFIG_MEMORY_ISOLATION) += page_isolation.o diff --git a/mm/page_frag_test.c b/mm/page_frag_test.c new file mode 100644 index 000000000000..a3605f1a8b2b --- /dev/null +++ b/mm/page_frag_test.c @@ -0,0 +1,386 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Test module for page_frag cache + * + * Copyright: linyunsheng@huawei.com + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define OBJPOOL_NR_OBJECT_MAX BIT(24) + +struct objpool_slot { + u32 head; + u32 tail; + u32 last; + u32 mask; + void *entries[]; +} __packed; + +struct objpool_head { + int nr_cpus; + int capacity; + struct objpool_slot **cpu_slots; +}; + +/* initialize percpu objpool_slot */ +static void objpool_init_percpu_slot(struct objpool_head *pool, + struct objpool_slot *slot) +{ + /* initialize elements of percpu objpool_slot */ + slot->mask = pool->capacity - 1; +} + +/* allocate and initialize percpu slots */ +static int objpool_init_percpu_slots(struct objpool_head *pool, + int nr_objs, gfp_t gfp) +{ + int i; + + for (i = 0; i < pool->nr_cpus; i++) { + struct objpool_slot *slot; + int size; + + /* skip the cpu node which could never be present */ + if (!cpu_possible(i)) + continue; + + size = struct_size(slot, entries, pool->capacity); + + /* + * here we allocate percpu-slot & objs together in a single + * allocation to make it more compact, taking advantage of + * warm caches and TLB hits. in default vmalloc is used to + * reduce the pressure of kernel slab system. as we know, + * minimal size of vmalloc is one page since vmalloc would + * always align the requested size to page size + */ + if (gfp & GFP_ATOMIC) + slot = kmalloc_node(size, gfp, cpu_to_node(i)); + else + slot = __vmalloc_node(size, sizeof(void *), gfp, + cpu_to_node(i), + __builtin_return_address(0)); + if (!slot) + return -ENOMEM; + + memset(slot, 0, size); + pool->cpu_slots[i] = slot; + + objpool_init_percpu_slot(pool, slot); + } + + return 0; +} + +/* cleanup all percpu slots of the object pool */ +static void objpool_fini_percpu_slots(struct objpool_head *pool) +{ + int i; + + if (!pool->cpu_slots) + return; + + for (i = 0; i < pool->nr_cpus; i++) + kvfree(pool->cpu_slots[i]); + kfree(pool->cpu_slots); +} + +/* initialize object pool and pre-allocate objects */ +static int objpool_init(struct objpool_head *pool, int nr_objs, gfp_t gfp) +{ + int rc, capacity, slot_size; + + /* check input parameters */ + if (nr_objs <= 0 || nr_objs > OBJPOOL_NR_OBJECT_MAX) + return -EINVAL; + + /* calculate capacity of percpu objpool_slot */ + capacity = roundup_pow_of_two(nr_objs); + if (!capacity) + return -EINVAL; + + gfp = gfp & ~__GFP_ZERO; + + /* initialize objpool pool */ + memset(pool, 0, sizeof(struct objpool_head)); + pool->nr_cpus = nr_cpu_ids; + pool->capacity = capacity; + slot_size = pool->nr_cpus * sizeof(struct objpool_slot *); + pool->cpu_slots = kzalloc(slot_size, gfp); + if (!pool->cpu_slots) + return -ENOMEM; + + /* initialize per-cpu slots */ + rc = objpool_init_percpu_slots(pool, nr_objs, gfp); + if (rc) + objpool_fini_percpu_slots(pool); + + return rc; +} + +/* adding object to slot, abort if the slot was already full */ +static int objpool_try_add_slot(void *obj, struct objpool_head *pool, int cpu) +{ + struct objpool_slot *slot = pool->cpu_slots[cpu]; + u32 head, tail; + + /* loading tail and head as a local snapshot, tail first */ + tail = READ_ONCE(slot->tail); + + do { + head = READ_ONCE(slot->head); + /* fault caught: something must be wrong */ + if (unlikely(tail - head >= pool->capacity)) + return -ENOSPC; + } while (!try_cmpxchg_acquire(&slot->tail, &tail, tail + 1)); + + /* now the tail position is reserved for the given obj */ + WRITE_ONCE(slot->entries[tail & slot->mask], obj); + /* update sequence to make this obj available for pop() */ + smp_store_release(&slot->last, tail + 1); + + return 0; +} + +/* reclaim an object to object pool */ +static int objpool_push(void *obj, struct objpool_head *pool) +{ + unsigned long flags; + int rc; + + /* disable local irq to avoid preemption & interruption */ + raw_local_irq_save(flags); + rc = objpool_try_add_slot(obj, pool, raw_smp_processor_id()); + raw_local_irq_restore(flags); + + return rc; +} + +/* try to retrieve object from slot */ +static void *objpool_try_get_slot(struct objpool_head *pool, int cpu) +{ + struct objpool_slot *slot = pool->cpu_slots[cpu]; + /* load head snapshot, other cpus may change it */ + u32 head = smp_load_acquire(&slot->head); + + while (head != READ_ONCE(slot->last)) { + void *obj; + + /* + * data visibility of 'last' and 'head' could be out of + * order since memory updating of 'last' and 'head' are + * performed in push() and pop() independently + * + * before any retrieving attempts, pop() must guarantee + * 'last' is behind 'head', that is to say, there must + * be available objects in slot, which could be ensured + * by condition 'last != head && last - head <= nr_objs' + * that is equivalent to 'last - head - 1 < nr_objs' as + * 'last' and 'head' are both unsigned int32 + */ + if (READ_ONCE(slot->last) - head - 1 >= pool->capacity) { + head = READ_ONCE(slot->head); + continue; + } + + /* obj must be retrieved before moving forward head */ + obj = READ_ONCE(slot->entries[head & slot->mask]); + + /* move head forward to mark it's consumption */ + if (try_cmpxchg_release(&slot->head, &head, head + 1)) + return obj; + } + + return NULL; +} + +/* allocate an object from object pool */ +static void *objpool_pop(struct objpool_head *pool) +{ + void *obj = NULL; + unsigned long flags; + int i, cpu; + + /* disable local irq to avoid preemption & interruption */ + raw_local_irq_save(flags); + + cpu = raw_smp_processor_id(); + for (i = 0; i < num_possible_cpus(); i++) { + obj = objpool_try_get_slot(pool, cpu); + if (obj) + break; + cpu = cpumask_next_wrap(cpu, cpu_possible_mask, -1, 1); + } + raw_local_irq_restore(flags); + + return obj; +} + +/* release whole objpool forcely */ +static void objpool_free(struct objpool_head *pool) +{ + if (!pool->cpu_slots) + return; + + /* release percpu slots */ + objpool_fini_percpu_slots(pool); +} + +static struct objpool_head ptr_pool; +static int nr_objs = 512; +static atomic_t nthreads; +static struct completion wait; +static struct page_frag_cache test_frag; + +static int nr_test = 5120000; +module_param(nr_test, int, 0600); +MODULE_PARM_DESC(nr_test, "number of iterations to test"); + +static bool test_align; +module_param(test_align, bool, 0600); +MODULE_PARM_DESC(test_align, "use align API for testing"); + +static int test_alloc_len = 2048; +module_param(test_alloc_len, int, 0600); +MODULE_PARM_DESC(test_alloc_len, "alloc len for testing"); + +static int test_push_cpu; +module_param(test_push_cpu, int, 0600); +MODULE_PARM_DESC(test_push_cpu, "test cpu for pushing fragment"); + +static int test_pop_cpu; +module_param(test_pop_cpu, int, 0600); +MODULE_PARM_DESC(test_pop_cpu, "test cpu for popping fragment"); + +static int page_frag_pop_thread(void *arg) +{ + struct objpool_head *pool = arg; + int nr = nr_test; + + pr_info("page_frag pop test thread begins on cpu %d\n", + smp_processor_id()); + + while (nr > 0) { + void *obj = objpool_pop(pool); + + if (obj) { + nr--; + page_frag_free(obj); + } else { + cond_resched(); + } + } + + if (atomic_dec_and_test(&nthreads)) + complete(&wait); + + pr_info("page_frag pop test thread exits on cpu %d\n", + smp_processor_id()); + + return 0; +} + +static int page_frag_push_thread(void *arg) +{ + struct objpool_head *pool = arg; + int nr = nr_test; + + pr_info("page_frag push test thread begins on cpu %d\n", + smp_processor_id()); + + while (nr > 0) { + void *va; + int ret; + + if (test_align) + va = page_frag_alloc_align(&test_frag, test_alloc_len, + GFP_KERNEL, SMP_CACHE_BYTES); + else + va = page_frag_alloc(&test_frag, test_alloc_len, GFP_KERNEL); + + if (!va) + continue; + + ret = objpool_push(va, pool); + if (ret) { + page_frag_free(va); + cond_resched(); + } else { + nr--; + } + } + + pr_info("page_frag push test thread exits on cpu %d\n", + smp_processor_id()); + + if (atomic_dec_and_test(&nthreads)) + complete(&wait); + + return 0; +} + +static int __init page_frag_test_init(void) +{ + struct task_struct *tsk_push, *tsk_pop; + ktime_t start; + u64 duration; + int ret; + + test_frag.va = NULL; + atomic_set(&nthreads, 2); + init_completion(&wait); + + ret = objpool_init(&ptr_pool, nr_objs, GFP_KERNEL); + if (ret) + return ret; + + tsk_push = kthread_create_on_cpu(page_frag_push_thread, &ptr_pool, + test_push_cpu, "page_frag_push"); + if (IS_ERR(tsk_push)) + return PTR_ERR(tsk_push); + + tsk_pop = kthread_create_on_cpu(page_frag_pop_thread, &ptr_pool, + test_pop_cpu, "page_frag_pop"); + if (IS_ERR(tsk_pop)) { + kthread_stop(tsk_push); + return PTR_ERR(tsk_pop); + } + + start = ktime_get(); + wake_up_process(tsk_push); + wake_up_process(tsk_pop); + + pr_info("waiting for test to complete\n"); + wait_for_completion(&wait); + + duration = (u64)ktime_us_delta(ktime_get(), start); + pr_info("%d of iterations for %s testing took: %lluus\n", nr_test, + test_align ? "aligned" : "non-aligned", duration); + + objpool_free(&ptr_pool); + page_frag_cache_drain(&test_frag); + + return -EAGAIN; +} + +static void __exit page_frag_test_exit(void) +{ +} + +module_init(page_frag_test_init); +module_exit(page_frag_test_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Yunsheng Lin "); +MODULE_DESCRIPTION("Test module for page_frag"); From patchwork Fri Jun 7 12:38:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13689828 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 49E81C27C5F for ; Fri, 7 Jun 2024 12:41:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D2F716B00A5; Fri, 7 Jun 2024 08:41:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CDF0F6B00A6; Fri, 7 Jun 2024 08:41:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BA6F56B00A7; Fri, 7 Jun 2024 08:41:23 -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 991C06B00A5 for ; Fri, 7 Jun 2024 08:41:23 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3C552A37BC for ; Fri, 7 Jun 2024 12:41:23 +0000 (UTC) X-FDA: 82204053246.07.60604B9 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf10.hostedemail.com (Postfix) with ESMTP id BDF04C0016 for ; Fri, 7 Jun 2024 12:41:20 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf10.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=1717764081; 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=Q3hJAP/ILoQv4jSclozK3qUQH7oW8TBjuZOAcQqQ6Bk=; b=SjTFHNSGyg2SKUQdVUw16qN+UM88P3Aj3PaGccmOFoC91aCszADCklnH9exkuKTainsIDc QgciXeszKXYWT1roNgWuhZB1+Tb8366Ots2Wbg/roFC516tbcIy2S4F6UuYTMe10thubGJ 2av1lUXU+ajkctXzwaEXVix2gb69aK0= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf10.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=1717764081; a=rsa-sha256; cv=none; b=11WGKYpbLe8GpfXBMJRyB+CcW9wLWWE8tgqS3e137yA9rl/vqU6GgHyS5b2ZdYiGwiOOtT KdhBgYYGX8kRiNo/secIKbsPzXaWQu45lqIINl4MBU9pIgyqqcDL541DzWqmbzpICYz9bH K/2LGfDvXMrUfD9fUCSZ/Htocg+9WeY= Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Vwggx68sszwSDv; Fri, 7 Jun 2024 20:37:17 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 3C97318007A; Fri, 7 Jun 2024 20:41:17 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 7 Jun 2024 20:41:16 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , David Howells , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v7 04/15] mm: move the page fragment allocator from page_alloc into its own file Date: Fri, 7 Jun 2024 20:38:07 +0800 Message-ID: <20240607123819.40694-5-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240607123819.40694-1-linyunsheng@huawei.com> References: <20240607123819.40694-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Queue-Id: BDF04C0016 X-Stat-Signature: 5a9cuxtaf3s8fejrb93a47kwqkxdqs9i X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1717764080-187860 X-HE-Meta: U2FsdGVkX1+kNqI6Hto73QIDYulG+znq164q7bce8nmNX3NmpcfN4VlDQPsMuHjhbMcNJ24/AFDidK2wIO6DUjvWbC7ke+ZiQRJAEbm9UPK4Xp5m0g1aywaG10DWIlkgINTTx6sQwKsq+ZIm/3pdrVcaANSH1hKBYuIF01O0v+wYe2P2QXtQ2j4hyFTIBihw2qLX7upqn9Lz08Sne9m4vud/ppbSjryMSXqAN94WwCd9FGmdRdY6zoALu1Bhtjbr5TfVE08RLvY4qjq3HQxPtqh1FQHOvRSzvaiAnwKeNd8B+BmSWmbqBzeVtPkRikIN4R5YpHHiJHWuXPIk+J/zP6MVdBpR5BTuQ00j32EU/elH1WDtW+Ng4UEpYb0iY/ve7Ck3/g41bzhdfMwnd4rfu4b3l8mzfVTX3lWb7ytMS4jNs6Uzr827Pkvtc9sHewNd/x6uHOAv6viFJ89WMvt+DwF131Gd7bfdX9/llhuPfh0w+1YAJd8LhZe9hOYR1NER+APxuTIEQT3wQGy6kybSMXoZpT+vlHviUNdZzmobWahLrjjTs+rhKyNaGRXGysXFUt2jUqqoJrsqguh1S6NWZMgCZnxboRv1jYQELx/eHnc8E5wSZkl+YFGkQwFyW9q999JaEZbPUeOvoRTzgMzOioOpMyEmVNToOSPxD7RvZTMn70Nw8WCYmPtix1j6phe5+oxswCfRPFxMTg6GHN10CLlZ8zWPFANS4d3LsjioazZuxbnMl6KVROyE7bR66ETho4VMtSFS1dKEp3XItJpwGJLzQYFuHzJ6KaA4JTfxUJr8231H/Qy1+vi2xx19RjkwVBB63StQhzBP4XHL+IUXhq2Vb7RdoXArl87TLwD9OCfn593YkohKt1j8IJUg1v3Q/TQ5oB2o0ds8U7iooVSN2jKWx8Nr+U0D4U2/H9qhkMPylyvxC9ZWsUXNWyZIGwZtH+otlZrhnmjLqtC3sGi m3HcrSWw zHBFjzgbvgM/ymE1jWOxQ1xDSGFwEj6qVx8Y22WUSPVSIOh//CYA0TVobVGyXgztjPPItaxRhU287fhlf7f451wtopsLJ8izI4Xuynyiab3UpfNZM85KNbS+U9CHKJYeeeDWFE6Zgs5Z2kwMqYBZPx4qcmeDVQmMX0TnI0r0eLW1eqTMcHw1PKjuhDR7zXT1cFE3Y7r7HhUR1U2boAe3LGarpEf3SmOBl5+xa7Ktxk2A0O8n1IV1qX/N/ACLNvi/jJ7T3RNoEwfJb3wbqeEWskBTvHSPV0fZbxYkOIKJgW1uF6TXRlfEYbGwkr3/iwV+XO5pe9Yw+BQIJgI6s7gcQbQ9Fm8hTH5Zf28ZIJwxyVUdUMcseJxSdsyeJaXKY2UoVX/tcpwrwK1IyCQ1L8AoGOMmyr+JsVlEtT1TfyoIncHH6ryh2MKsk0FFhA8MEs5V1UQoaFLFLXyAbUYYdHzGMj9UAYSB6cU7GdRUcLaXgDCFtAWaZZL24UEqmKeLOyAGa3T+/XujxPoiGN7AMcbh+jQYWtQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Inspired by [1], move the page fragment allocator from page_alloc into its own c file and header file, as we are about to make more change for it to replace another page_frag implementation in sock.c 1. https://lore.kernel.org/all/20230411160902.4134381-3-dhowells@redhat.com/ CC: David Howells CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/gfp.h | 22 ----- include/linux/mm_types.h | 18 ---- include/linux/page_frag_cache.h | 47 ++++++++++ include/linux/skbuff.h | 1 + mm/Makefile | 1 + mm/page_alloc.c | 139 ------------------------------ mm/page_frag_cache.c | 147 ++++++++++++++++++++++++++++++++ mm/page_frag_test.c | 1 + 8 files changed, 197 insertions(+), 179 deletions(-) create mode 100644 include/linux/page_frag_cache.h create mode 100644 mm/page_frag_cache.c diff --git a/include/linux/gfp.h b/include/linux/gfp.h index 7f9691d375f0..3d8f9dc6c6ee 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -363,28 +363,6 @@ __meminit void *alloc_pages_exact_nid_noprof(int nid, size_t size, gfp_t gfp_mas extern void __free_pages(struct page *page, unsigned int order); extern void free_pages(unsigned long addr, unsigned int order); -struct page_frag_cache; -void page_frag_cache_drain(struct page_frag_cache *nc); -extern void __page_frag_cache_drain(struct page *page, unsigned int count); -void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, - gfp_t gfp_mask, unsigned int align_mask); - -static inline void *page_frag_alloc_align(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask, - unsigned int align) -{ - WARN_ON_ONCE(!is_power_of_2(align)); - return __page_frag_alloc_align(nc, fragsz, gfp_mask, -align); -} - -static inline void *page_frag_alloc(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask) -{ - return __page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u); -} - -extern void page_frag_free(void *addr); - #define __free_page(page) __free_pages((page), 0) #define free_page(addr) free_pages((addr), 0) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 24323c7d0bd4..5b99dbec91d7 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -505,9 +505,6 @@ static_assert(sizeof(struct ptdesc) <= sizeof(struct page)); */ #define STRUCT_PAGE_MAX_SHIFT (order_base_2(sizeof(struct page))) -#define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK) -#define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE) - /* * page_private can be used on tail pages. However, PagePrivate is only * checked by the VM on the head page. So page_private on the tail pages @@ -526,21 +523,6 @@ static inline void *folio_get_private(struct folio *folio) return folio->private; } -struct page_frag_cache { - void * va; -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - __u16 offset; - __u16 size; -#else - __u32 offset; -#endif - /* we maintain a pagecount bias, so that we dont dirty cache line - * containing page->_refcount every time we allocate a fragment. - */ - unsigned int pagecnt_bias; - bool pfmemalloc; -}; - typedef unsigned long vm_flags_t; /* diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h new file mode 100644 index 000000000000..3a44bfc99750 --- /dev/null +++ b/include/linux/page_frag_cache.h @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _LINUX_PAGE_FRAG_CACHE_H +#define _LINUX_PAGE_FRAG_CACHE_H + +#include + +#define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK) +#define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE) + +struct page_frag_cache { + void *va; +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + __u16 offset; + __u16 size; +#else + __u32 offset; +#endif + /* we maintain a pagecount bias, so that we dont dirty cache line + * containing page->_refcount every time we allocate a fragment. + */ + unsigned int pagecnt_bias; + bool pfmemalloc; +}; + +void page_frag_cache_drain(struct page_frag_cache *nc); +void __page_frag_cache_drain(struct page *page, unsigned int count); +void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, + gfp_t gfp_mask, unsigned int align_mask); + +static inline void *page_frag_alloc_align(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask, + unsigned int align) +{ + WARN_ON_ONCE(!is_power_of_2(align)); + return __page_frag_alloc_align(nc, fragsz, gfp_mask, -align); +} + +static inline void *page_frag_alloc(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask) +{ + return __page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u); +} + +void page_frag_free(void *addr); + +#endif diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index fe7d8dbef77e..35f6e3f12565 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 29d9f7618a33..3080257a0a75 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -64,6 +64,7 @@ page-alloc-$(CONFIG_SHUFFLE_PAGE_ALLOCATOR) += shuffle.o memory-hotplug-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o obj-y += page-alloc.o +obj-y += page_frag_cache.o obj-y += init-mm.o obj-y += memblock.o obj-y += $(memory-hotplug-y) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 0511c30bc265..2990d170b487 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4763,145 +4763,6 @@ void free_pages(unsigned long addr, unsigned int order) EXPORT_SYMBOL(free_pages); -/* - * Page Fragment: - * An arbitrary-length arbitrary-offset area of memory which resides - * within a 0 or higher order page. Multiple fragments within that page - * are individually refcounted, in the page's reference counter. - * - * The page_frag functions below provide a simple allocation framework for - * page fragments. This is used by the network stack and network device - * drivers to provide a backing region of memory for use as either an - * sk_buff->head, or to be used in the "frags" portion of skb_shared_info. - */ -static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, - gfp_t gfp_mask) -{ - struct page *page = NULL; - gfp_t gfp = gfp_mask; - -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* Ensure free_unref_page() can be used to free the page fragment */ - BUILD_BUG_ON(PAGE_FRAG_CACHE_MAX_ORDER > PAGE_ALLOC_COSTLY_ORDER); - - gfp_mask = (gfp_mask & ~__GFP_DIRECT_RECLAIM) | __GFP_COMP | - __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; - page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, - PAGE_FRAG_CACHE_MAX_ORDER); - nc->size = page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; -#endif - if (unlikely(!page)) - page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); - - nc->va = page ? page_address(page) : NULL; - - return page; -} - -void page_frag_cache_drain(struct page_frag_cache *nc) -{ - if (!nc->va) - return; - - __page_frag_cache_drain(virt_to_head_page(nc->va), nc->pagecnt_bias); - nc->va = NULL; -} -EXPORT_SYMBOL(page_frag_cache_drain); - -void __page_frag_cache_drain(struct page *page, unsigned int count) -{ - VM_BUG_ON_PAGE(page_ref_count(page) == 0, page); - - if (page_ref_sub_and_test(page, count)) - free_unref_page(page, compound_order(page)); -} -EXPORT_SYMBOL(__page_frag_cache_drain); - -void *__page_frag_alloc_align(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask, - unsigned int align_mask) -{ - unsigned int size = PAGE_SIZE; - struct page *page; - int offset; - - if (unlikely(!nc->va)) { -refill: - page = __page_frag_cache_refill(nc, gfp_mask); - if (!page) - return NULL; - -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif - /* Even if we own the page, we do not use atomic_set(). - * This would break get_page_unless_zero() users. - */ - page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); - - /* reset page count bias and offset to start of new frag */ - nc->pfmemalloc = page_is_pfmemalloc(page); - nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset = size; - } - - offset = nc->offset - fragsz; - if (unlikely(offset < 0)) { - page = virt_to_page(nc->va); - - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) - goto refill; - - if (unlikely(nc->pfmemalloc)) { - free_unref_page(page, compound_order(page)); - goto refill; - } - -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif - /* OK, page count is 0, we can safely set it */ - set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); - - /* reset page count bias and offset to start of new frag */ - nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset = size - fragsz; - if (unlikely(offset < 0)) { - /* - * The caller is trying to allocate a fragment - * with fragsz > PAGE_SIZE but the cache isn't big - * enough to satisfy the request, this may - * happen in low memory conditions. - * We don't release the cache page because - * it could make memory pressure worse - * so we simply return NULL here. - */ - return NULL; - } - } - - nc->pagecnt_bias--; - offset &= align_mask; - nc->offset = offset; - - return nc->va + offset; -} -EXPORT_SYMBOL(__page_frag_alloc_align); - -/* - * Frees a page fragment allocated out of either a compound or order 0 page. - */ -void page_frag_free(void *addr) -{ - struct page *page = virt_to_head_page(addr); - - if (unlikely(put_page_testzero(page))) - free_unref_page(page, compound_order(page)); -} -EXPORT_SYMBOL(page_frag_free); - static void *make_alloc_exact(unsigned long addr, unsigned int order, size_t size) { diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c new file mode 100644 index 000000000000..64993b5d1243 --- /dev/null +++ b/mm/page_frag_cache.c @@ -0,0 +1,147 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Page fragment allocator + * + * Page Fragment: + * An arbitrary-length arbitrary-offset area of memory which resides within a + * 0 or higher order page. Multiple fragments within that page are + * individually refcounted, in the page's reference counter. + * + * The page_frag functions provide a simple allocation framework for page + * fragments. This is used by the network stack and network device drivers to + * provide a backing region of memory for use as either an sk_buff->head, or to + * be used in the "frags" portion of skb_shared_info. + */ + +#include +#include +#include +#include +#include "internal.h" + +static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, + gfp_t gfp_mask) +{ + struct page *page = NULL; + gfp_t gfp = gfp_mask; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + /* Ensure free_unref_page() can be used to free the page fragment */ + BUILD_BUG_ON(PAGE_FRAG_CACHE_MAX_ORDER > PAGE_ALLOC_COSTLY_ORDER); + + gfp_mask = (gfp_mask & ~__GFP_DIRECT_RECLAIM) | __GFP_COMP | + __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; + page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, + PAGE_FRAG_CACHE_MAX_ORDER); + nc->size = page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; +#endif + if (unlikely(!page)) + page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); + + nc->va = page ? page_address(page) : NULL; + + return page; +} + +void page_frag_cache_drain(struct page_frag_cache *nc) +{ + if (!nc->va) + return; + + __page_frag_cache_drain(virt_to_head_page(nc->va), nc->pagecnt_bias); + nc->va = NULL; +} +EXPORT_SYMBOL(page_frag_cache_drain); + +void __page_frag_cache_drain(struct page *page, unsigned int count) +{ + VM_BUG_ON_PAGE(page_ref_count(page) == 0, page); + + if (page_ref_sub_and_test(page, count)) + free_unref_page(page, compound_order(page)); +} +EXPORT_SYMBOL(__page_frag_cache_drain); + +void *__page_frag_alloc_align(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask, + unsigned int align_mask) +{ + unsigned int size = PAGE_SIZE; + struct page *page; + int offset; + + if (unlikely(!nc->va)) { +refill: + page = __page_frag_cache_refill(nc, gfp_mask); + if (!page) + return NULL; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + /* if size can vary use size else just use PAGE_SIZE */ + size = nc->size; +#endif + /* Even if we own the page, we do not use atomic_set(). + * This would break get_page_unless_zero() users. + */ + page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); + + /* reset page count bias and offset to start of new frag */ + nc->pfmemalloc = page_is_pfmemalloc(page); + nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; + nc->offset = size; + } + + offset = nc->offset - fragsz; + if (unlikely(offset < 0)) { + page = virt_to_page(nc->va); + + if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) + goto refill; + + if (unlikely(nc->pfmemalloc)) { + free_unref_page(page, compound_order(page)); + goto refill; + } + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + /* if size can vary use size else just use PAGE_SIZE */ + size = nc->size; +#endif + /* OK, page count is 0, we can safely set it */ + set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); + + /* reset page count bias and offset to start of new frag */ + nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; + offset = size - fragsz; + if (unlikely(offset < 0)) { + /* + * The caller is trying to allocate a fragment + * with fragsz > PAGE_SIZE but the cache isn't big + * enough to satisfy the request, this may + * happen in low memory conditions. + * We don't release the cache page because + * it could make memory pressure worse + * so we simply return NULL here. + */ + return NULL; + } + } + + nc->pagecnt_bias--; + offset &= align_mask; + nc->offset = offset; + + return nc->va + offset; +} +EXPORT_SYMBOL(__page_frag_alloc_align); + +/* + * Frees a page fragment allocated out of either a compound or order 0 page. + */ +void page_frag_free(void *addr) +{ + struct page *page = virt_to_head_page(addr); + + if (unlikely(put_page_testzero(page))) + free_unref_page(page, compound_order(page)); +} +EXPORT_SYMBOL(page_frag_free); diff --git a/mm/page_frag_test.c b/mm/page_frag_test.c index a3605f1a8b2b..1349f6c6b521 100644 --- a/mm/page_frag_test.c +++ b/mm/page_frag_test.c @@ -16,6 +16,7 @@ #include #include #include +#include #define OBJPOOL_NR_OBJECT_MAX BIT(24) From patchwork Fri Jun 7 12:38:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13689829 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 B67F7C27C53 for ; Fri, 7 Jun 2024 12:41:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 47FE36B00A7; Fri, 7 Jun 2024 08:41:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 42DEE6B00A8; Fri, 7 Jun 2024 08:41:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2F6E16B00A9; Fri, 7 Jun 2024 08:41:26 -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 08EE86B00A7 for ; Fri, 7 Jun 2024 08:41:26 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 8AE0C14184A for ; Fri, 7 Jun 2024 12:41:25 +0000 (UTC) X-FDA: 82204053330.24.D7D3934 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf01.hostedemail.com (Postfix) with ESMTP id DFA774001F for ; Fri, 7 Jun 2024 12:41:22 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.190 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=1717764083; 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=7SPNZMzmZwzE/snTI4LAwNFjAVMQpBbCTMUnLeNfsPk=; b=Moojqy89bD/ozKfCnBPIe0XE0h6JYk1tDv5cfcvNPOu+i4Kw5ceuSmqwPcoGIwMIZgnFDs +rqXY73dZiuW5y4jgZNt+KRQ54pu9pBcS4FKLceqB61Wm6xlozEhrMX9OgyYYeqdgw/yTb sniZ1vFYsdThqDb7NmMHlCJl71aCRgY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717764083; a=rsa-sha256; cv=none; b=2NCjul7QdymKzJB09hpAhdXXPCkdcukiVodKwTLVZSYhDOUBUCJUNzh1yFQXKI20spSPOm w0nJDcFjkme+H+zncugPk64d5NTSJF0F9GLEHnB6r71+bW/mEV6ox7JMLkxBW+dlS6xlB6 COeFS4wSfZEsCJrAXbUGoKBx8+7N3m8= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.162.112]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4Vwghm4V3zz1yt5C; Fri, 7 Jun 2024 20:38:00 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id DB5941400D3; Fri, 7 Jun 2024 20:41:19 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 7 Jun 2024 20:41:19 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v7 05/15] mm: page_frag: use initial zero offset for page_frag_alloc_align() Date: Fri, 7 Jun 2024 20:38:08 +0800 Message-ID: <20240607123819.40694-6-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240607123819.40694-1-linyunsheng@huawei.com> References: <20240607123819.40694-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspam-User: X-Stat-Signature: 8cuan6b6yn7dy9yioqdg6f5o7myicksm X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: DFA774001F X-HE-Tag: 1717764082-808175 X-HE-Meta: U2FsdGVkX18eo0Eizj/sZVcwcV0TLBkm/iGsHT6F8HbUqry1MeDz1aUFFj3P8ZGFKns9k/lVm8wrllL4yHnR2i2gRSup7HTnOXX4Sjf41l8isg5jKEkBlaT824uJL4GG6NK7BcJTB/cKrb1Eed3bMSgTJ0CHNVOIlVP2f3RtujJYtFxYMUNzgSeP3tjPNZPGHVImQ356/KxDu75LmpUMACqoc5J0oSfCTUAUQGP/gKASFi1UyNeOFQvhQwM9lq39HCrKaUoPKtTTWupCiIL3OG3rEeGnpgkxuoQQhfto8zwJ5BlD9AtM6g2WDhu640SJGJiTV6uWKWMmR1NL/GaA3zWqbwOCRFu8Eg05xItHxV79ORZvU4YxvjJeWkXWpwq2BLVR8sf0huuDFNRqqK3TihW42FzVsSNM/ZHrXX1Dbz8N9HfP8f9yDCJTCyhEmrT2Ogcyk21P98+mb68SEBMSewnmoHjPI5c447Jj2X6YQYSnkwKg5Bp7ToKlg6PaSS4DzpdAcWfxK1Hcv7913dqbpS14DQymgNk7Mv0idgYTa5s3HyNk4HcsRRUKHPSWsnIuhj/wQZ/l9jwQ1dQtbzr1O75WY2QQdcEOEB1QDGR93UFIl0fXne8YBBhwUbxqat/drSWYcNmh1NlYxkmwuel7LhDwcnqbLv94QG9xSMjoKxO28l76HfIjISIPoX/9sCoXCj5Dpji1//5d0csUB9qZgiNns48d9ofVAPNseVy7RyUITw4hByn7Hz0eEudF1Vyn2NuvfaB54uzAb2CA5GThO2Ah6KFZTHveVWVpv3X3Z/DOOq0bcWX4M7fkQ1F5RQAtEc/0JPsho+KfeftYVaEOD+E0qKz/sKWdQJtitoAZU4SkpUUgw60SGSyL7P4W5ARjDhmnyOINqLgekeGiX8HymkIY8VMX3tjaXCnQT1UgqyehoBOtU3vs2jTiVRKAPyRrxAVlByUihwvBWXu+Orv v1S//n75 WBWdG66TTHdY6GOJrjLmwVCkNp+wcMpPiIBfw7GRfM4y1HAvyIvqtyanTLdff9JDBW6eL7MU+Aoen50kYJB2awgRc2zNOP0fg6G39FH0qR3GQDAPNmr45pVeWDl0Y7Ef7oaFPD3wtJnbM0dgur38/5faq+Nh5k9UadyiV9K7d7mk+ogta6pnDaFYUo+O0R0KrANqAAdDS35UgGZY+ynMq3yR/sCRZyKIizd3blveY+PoEGYn41kVki2HLjH+uHmFlbRAkNSuBZdfOSYRjgZbI5othBLJ0LSuMgvOzTZA11rkwCM+FwJ3IRjvCahFrMbGpLmVRr9tHJwTaykjy2ku8Ato/Vw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: We are above to use page_frag_alloc_*() API to not just allocate memory for skb->data, but also use them to do the memory allocation for skb frag too. Currently the implementation of page_frag in mm subsystem is running the offset as a countdown rather than count-up value, there may have several advantages to that as mentioned in [1], but it may have some disadvantages, for example, it may disable skb frag coaleasing and more correct cache prefetching We have a trade-off to make in order to have a unified implementation and API for page_frag, so use a initial zero offset in this patch, and the following patch will try to make some optimization to aovid the disadvantages as much as possible. As offsets is added due to alignment requirement before actually checking if the cache is enough, which might make it exploitable if caller passes a align value bigger than 32K mistakenly. As we are allowing order 3 page allocation to fail easily under low memory condition, align value bigger than PAGE_SIZE is not really allowed, so add a 'align > PAGE_SIZE' checking in page_frag_alloc_va_align() to catch that. 1. https://lore.kernel.org/all/f4abe71b3439b39d17a6fb2d410180f367cadf5c.camel@gmail.com/ CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 2 +- include/linux/skbuff.h | 4 ++-- mm/page_frag_cache.c | 26 +++++++++++--------------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 3a44bfc99750..b9411f0db25a 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -32,7 +32,7 @@ static inline void *page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align) { - WARN_ON_ONCE(!is_power_of_2(align)); + WARN_ON_ONCE(!is_power_of_2(align) || align > PAGE_SIZE); return __page_frag_alloc_align(nc, fragsz, gfp_mask, -align); } diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 35f6e3f12565..6587dc1b3f70 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -3300,7 +3300,7 @@ static inline void *netdev_alloc_frag(unsigned int fragsz) static inline void *netdev_alloc_frag_align(unsigned int fragsz, unsigned int align) { - WARN_ON_ONCE(!is_power_of_2(align)); + WARN_ON_ONCE(!is_power_of_2(align) || align > PAGE_SIZE); return __netdev_alloc_frag_align(fragsz, -align); } @@ -3371,7 +3371,7 @@ static inline void *napi_alloc_frag(unsigned int fragsz) static inline void *napi_alloc_frag_align(unsigned int fragsz, unsigned int align) { - WARN_ON_ONCE(!is_power_of_2(align)); + WARN_ON_ONCE(!is_power_of_2(align) || align > PAGE_SIZE); return __napi_alloc_frag_align(fragsz, -align); } diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 64993b5d1243..d569021376d4 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -75,10 +75,6 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, if (!page) return NULL; -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif /* Even if we own the page, we do not use atomic_set(). * This would break get_page_unless_zero() users. */ @@ -87,11 +83,16 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, /* reset page count bias and offset to start of new frag */ nc->pfmemalloc = page_is_pfmemalloc(page); nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset = size; + nc->offset = 0; } - offset = nc->offset - fragsz; - if (unlikely(offset < 0)) { +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + /* if size can vary use size else just use PAGE_SIZE */ + size = nc->size; +#endif + + offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); + if (unlikely(offset + fragsz > size)) { page = virt_to_page(nc->va); if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) @@ -102,17 +103,13 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, goto refill; } -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif /* OK, page count is 0, we can safely set it */ set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset = size - fragsz; - if (unlikely(offset < 0)) { + offset = 0; + if (unlikely(fragsz > PAGE_SIZE)) { /* * The caller is trying to allocate a fragment * with fragsz > PAGE_SIZE but the cache isn't big @@ -127,8 +124,7 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, } nc->pagecnt_bias--; - offset &= align_mask; - nc->offset = offset; + nc->offset = offset + fragsz; return nc->va + offset; } From patchwork Fri Jun 7 12:38:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13689830 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 71F11C27C53 for ; Fri, 7 Jun 2024 12:41:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0FA656B00AA; Fri, 7 Jun 2024 08:41:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0AB216B00AB; Fri, 7 Jun 2024 08:41:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E8DD96B00AC; Fri, 7 Jun 2024 08:41:33 -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 C5FF16B00AA for ; Fri, 7 Jun 2024 08:41:33 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 8C910A377A for ; Fri, 7 Jun 2024 12:41:33 +0000 (UTC) X-FDA: 82204053666.28.6AFF55E Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf19.hostedemail.com (Postfix) with ESMTP id 20A201A0006 for ; Fri, 7 Jun 2024 12:41:30 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=none; spf=pass (imf19.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717764091; a=rsa-sha256; cv=none; b=Baqk7reEXfpPUvZUX8K7wKIR//w3X+fEQtxh7/xWm9I5eyUH7K4ATHVZbm9WuDzy/zV0T1 hLj7uH+DsJmEXxF5xArFaRMnePfZNiIXFg5MYyoaKV5kDv9k3e1GzCfmBlCmJK3SRaQM1y eAl49yjHzhAQvZ4c7mfHNML2CPqG7co= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none; spf=pass (imf19.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717764091; 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=7aDy6+7EorSUlEL5sPketF7Mc1ktKNYZNBqqy425IgU=; b=UA8hsb2vLPUQVMN5LaxQrSnVFG+h4Wd0UPzmIpcjNokoHS2BF85YaIKHwbqFrxUGuYLcEB DFXXRlUIeSnL2uKhaUhrbLP5eG/Bd5IsgoKRIELnZdHC4W7vfJD3S35HyUCUxQAOWS96dP 1v26GclkN3dprf7G3VvltgHi+RFMutM= Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4Vwght45tFzPptQ; Fri, 7 Jun 2024 20:38:06 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 382D5180085; Fri, 7 Jun 2024 20:41:26 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 7 Jun 2024 20:41:25 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Jeroen de Borst , Praveen Kaligineedi , Shailend Chand , Eric Dumazet , Jesse Brandeburg , Tony Nguyen , Sunil Goutham , Geetha sowjanya , Subbaraya Sundeep , hariprasad , Felix Fietkau , Sean Wang , Mark Lee , Lorenzo Bianconi , Matthias Brugger , AngeloGioacchino Del Regno , Keith Busch , Jens Axboe , Christoph Hellwig , Sagi Grimberg , Chaitanya Kulkarni , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Andrew Morton , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , David Howells , Marc Dionne , Trond Myklebust , Anna Schumaker , Chuck Lever , Jeff Layton , Neil Brown , Olga Kornievskaia , Dai Ngo , Tom Talpey , , , , , , , , , , Subject: [PATCH net-next v7 06/15] mm: page_frag: add '_va' suffix to page_frag API Date: Fri, 7 Jun 2024 20:38:09 +0800 Message-ID: <20240607123819.40694-7-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240607123819.40694-1-linyunsheng@huawei.com> References: <20240607123819.40694-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 20A201A0006 X-Stat-Signature: jqqayy59ojk6pgiibnzbppdn4yhxqeyg X-HE-Tag: 1717764090-272805 X-HE-Meta: U2FsdGVkX19jI7jEa5UwZZqiNnua8xzL61K18nQcioiZr2QGcub235KwOrRdUlonL29e+6UKrMY8/JywGg9h/7PO0a09Om8UqAGCxBNmDTJSqVXul/3YL28m8kXf4exhG7MIwYCSr049zoqMneOZfA0fxQmkJJqFgKks54Ueo1Y3sxXoEhZRc7hwGYZER8H6zSA91evCKhLtVe/3Wdig9Opx15/J74ruNcDZ2dk8kJpUCwxzVpi479h7O7sD36GYB3CZWMQY98rAJ58Hx22QmVhD+CbT30lhftF7DKQLqUuyEg5QZlx2oGNwhGjmLfdhdtPrc85Dkw0p7JGZJM1fjAyqqkAG0u/W8udVDJE84vqG79miLVvBncAQSajGl3YoK7phzjGq0Yv+r/Onr0hgxmPvGJAex51Jc4/AUBtud13stNADSWUOe+bd4yIajsfIhPSaoJXW3wM1TK3t2Ql8of0Ew3XS0zWJRifY6G5x0fNg1qkp09ludWlMEG0fCJ7nkFO5OSuP3e87ZBm3V+vkbMLOSy8CbZHmFU6+9GU7SCE9QcmmEUTgG7xj32tgFw6WBllHubj7nofV0OJCE5EzXhBtoti5vSEoDy/tU3IKMENVud+T7clGaK+yg7Ex/Uk2kE7Zb1++XOP1eR0LZqg8o+SJEFiVjMMFpk/Y6zFbn/z1HT+5htd5CJCjeNBV4jYO6VaF5mOuErxVKRre5E/DxwptSUCsVOpNnFh8gyjE/BjSg7yUke3knTPSg4ijZ+ljaIVme6N8TlYUExGPAHuILbG+48C+V3uWMKCD8Bst4tpv12hKEbkLCLs0MjN2OSvBN50Ij/8jV/JE5WZks8obQDX6kv8IB5V9KiYdtJhqibQAPTwiKbUv6Way1u7sTXwM3QWnnRBMLsfoDOGDGaNGzXMeiyBOrSI7w3btcitrElWRuquaCuyKV5iWkjZgFXH/YhiZ0AAFs+3P66UGjQy YmzajKJp uhrGSf3Cbn3EYjZURwr8iLhXoubDqPwAzv6uMK6RBuvaT7pspJV8BhMPw3gDrh0SFW55yxTYCvc6Wk01d8Qf6ZLW5wBdEH+8fEhqJGk+ObO09KEEX+O2GuFotUe/h5Ucdjtd7PDL3YDzAPi+ryLq4MyAIogdh+wob3a+Le9NTzGgQMjhDclVdhB3G5TQY6hjvWjkDH/bD/uwOn01W6g9YmMNaeO8dxZOwyzrnraPMcfniI+WPiv6tNyqv4pxUkaPbZQUkKYAswrCpgYV8rc392lNyeMr7TLrk7jcr X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Currently the page_frag API is returning 'virtual address' or 'va' when allocing and expecting 'virtual address' or 'va' as input when freeing. As we are about to support new use cases that the caller need to deal with 'struct page' or need to deal with both 'va' and 'struct page'. In order to differentiate the API handling between 'va' and 'struct page', add '_va' suffix to the corresponding API mirroring the page_pool_alloc_va() API of the page_pool. So that callers expecting to deal with va, page or both va and page may call page_frag_alloc_va*, page_frag_alloc_pg*, or page_frag_alloc* API accordingly. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- drivers/net/ethernet/google/gve/gve_rx.c | 4 ++-- drivers/net/ethernet/intel/ice/ice_txrx.c | 2 +- drivers/net/ethernet/intel/ice/ice_txrx.h | 2 +- drivers/net/ethernet/intel/ice/ice_txrx_lib.c | 2 +- .../net/ethernet/intel/ixgbevf/ixgbevf_main.c | 4 ++-- .../marvell/octeontx2/nic/otx2_common.c | 2 +- drivers/net/ethernet/mediatek/mtk_wed_wo.c | 4 ++-- drivers/nvme/host/tcp.c | 8 +++---- drivers/nvme/target/tcp.c | 22 +++++++++---------- drivers/vhost/net.c | 6 ++--- include/linux/page_frag_cache.h | 21 +++++++++--------- include/linux/skbuff.h | 2 +- kernel/bpf/cpumap.c | 2 +- mm/page_frag_cache.c | 12 +++++----- mm/page_frag_test.c | 13 ++++++----- net/core/skbuff.c | 18 +++++++-------- net/core/xdp.c | 2 +- net/rxrpc/txbuf.c | 15 +++++++------ net/sunrpc/svcsock.c | 6 ++--- 19 files changed, 76 insertions(+), 71 deletions(-) diff --git a/drivers/net/ethernet/google/gve/gve_rx.c b/drivers/net/ethernet/google/gve/gve_rx.c index acb73d4d0de6..b6c10100e462 100644 --- a/drivers/net/ethernet/google/gve/gve_rx.c +++ b/drivers/net/ethernet/google/gve/gve_rx.c @@ -729,7 +729,7 @@ static int gve_xdp_redirect(struct net_device *dev, struct gve_rx_ring *rx, total_len = headroom + SKB_DATA_ALIGN(len) + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); - frame = page_frag_alloc(&rx->page_cache, total_len, GFP_ATOMIC); + frame = page_frag_alloc_va(&rx->page_cache, total_len, GFP_ATOMIC); if (!frame) { u64_stats_update_begin(&rx->statss); rx->xdp_alloc_fails++; @@ -742,7 +742,7 @@ static int gve_xdp_redirect(struct net_device *dev, struct gve_rx_ring *rx, err = xdp_do_redirect(dev, &new, xdp_prog); if (err) - page_frag_free(frame); + page_frag_free_va(frame); return err; } diff --git a/drivers/net/ethernet/intel/ice/ice_txrx.c b/drivers/net/ethernet/intel/ice/ice_txrx.c index 8bb743f78fcb..399b317c509d 100644 --- a/drivers/net/ethernet/intel/ice/ice_txrx.c +++ b/drivers/net/ethernet/intel/ice/ice_txrx.c @@ -126,7 +126,7 @@ ice_unmap_and_free_tx_buf(struct ice_tx_ring *ring, struct ice_tx_buf *tx_buf) dev_kfree_skb_any(tx_buf->skb); break; case ICE_TX_BUF_XDP_TX: - page_frag_free(tx_buf->raw_buf); + page_frag_free_va(tx_buf->raw_buf); break; case ICE_TX_BUF_XDP_XMIT: xdp_return_frame(tx_buf->xdpf); diff --git a/drivers/net/ethernet/intel/ice/ice_txrx.h b/drivers/net/ethernet/intel/ice/ice_txrx.h index feba314a3fe4..6379f57d8228 100644 --- a/drivers/net/ethernet/intel/ice/ice_txrx.h +++ b/drivers/net/ethernet/intel/ice/ice_txrx.h @@ -148,7 +148,7 @@ static inline int ice_skb_pad(void) * @ICE_TX_BUF_DUMMY: dummy Flow Director packet, unmap and kfree() * @ICE_TX_BUF_FRAG: mapped skb OR &xdp_buff frag, only unmap DMA * @ICE_TX_BUF_SKB: &sk_buff, unmap and consume_skb(), update stats - * @ICE_TX_BUF_XDP_TX: &xdp_buff, unmap and page_frag_free(), stats + * @ICE_TX_BUF_XDP_TX: &xdp_buff, unmap and page_frag_free_va(), stats * @ICE_TX_BUF_XDP_XMIT: &xdp_frame, unmap and xdp_return_frame(), stats * @ICE_TX_BUF_XSK_TX: &xdp_buff on XSk queue, xsk_buff_free(), stats */ diff --git a/drivers/net/ethernet/intel/ice/ice_txrx_lib.c b/drivers/net/ethernet/intel/ice/ice_txrx_lib.c index 2719f0e20933..a1a41a14df0d 100644 --- a/drivers/net/ethernet/intel/ice/ice_txrx_lib.c +++ b/drivers/net/ethernet/intel/ice/ice_txrx_lib.c @@ -250,7 +250,7 @@ ice_clean_xdp_tx_buf(struct device *dev, struct ice_tx_buf *tx_buf, switch (tx_buf->type) { case ICE_TX_BUF_XDP_TX: - page_frag_free(tx_buf->raw_buf); + page_frag_free_va(tx_buf->raw_buf); break; case ICE_TX_BUF_XDP_XMIT: xdp_return_frame_bulk(tx_buf->xdpf, bq); diff --git a/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c b/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c index b938dc06045d..fcd1b149a45d 100644 --- a/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c +++ b/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c @@ -303,7 +303,7 @@ static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector, /* free the skb */ if (ring_is_xdp(tx_ring)) - page_frag_free(tx_buffer->data); + page_frag_free_va(tx_buffer->data); else napi_consume_skb(tx_buffer->skb, napi_budget); @@ -2413,7 +2413,7 @@ static void ixgbevf_clean_tx_ring(struct ixgbevf_ring *tx_ring) /* Free all the Tx ring sk_buffs */ if (ring_is_xdp(tx_ring)) - page_frag_free(tx_buffer->data); + page_frag_free_va(tx_buffer->data); else dev_kfree_skb_any(tx_buffer->skb); diff --git a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c index a85ac039d779..8eb5820b8a70 100644 --- a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c +++ b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c @@ -553,7 +553,7 @@ static int __otx2_alloc_rbuf(struct otx2_nic *pfvf, struct otx2_pool *pool, *dma = dma_map_single_attrs(pfvf->dev, buf, pool->rbsize, DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC); if (unlikely(dma_mapping_error(pfvf->dev, *dma))) { - page_frag_free(buf); + page_frag_free_va(buf); return -ENOMEM; } diff --git a/drivers/net/ethernet/mediatek/mtk_wed_wo.c b/drivers/net/ethernet/mediatek/mtk_wed_wo.c index 7063c78bd35f..c4228719f8a4 100644 --- a/drivers/net/ethernet/mediatek/mtk_wed_wo.c +++ b/drivers/net/ethernet/mediatek/mtk_wed_wo.c @@ -142,8 +142,8 @@ mtk_wed_wo_queue_refill(struct mtk_wed_wo *wo, struct mtk_wed_wo_queue *q, dma_addr_t addr; void *buf; - buf = page_frag_alloc(&q->cache, q->buf_size, - GFP_ATOMIC | GFP_DMA32); + buf = page_frag_alloc_va(&q->cache, q->buf_size, + GFP_ATOMIC | GFP_DMA32); if (!buf) break; diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c index 8b5e4327fe83..4b7a897897fc 100644 --- a/drivers/nvme/host/tcp.c +++ b/drivers/nvme/host/tcp.c @@ -506,7 +506,7 @@ static void nvme_tcp_exit_request(struct blk_mq_tag_set *set, { struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); - page_frag_free(req->pdu); + page_frag_free_va(req->pdu); } static int nvme_tcp_init_request(struct blk_mq_tag_set *set, @@ -520,7 +520,7 @@ static int nvme_tcp_init_request(struct blk_mq_tag_set *set, struct nvme_tcp_queue *queue = &ctrl->queues[queue_idx]; u8 hdgst = nvme_tcp_hdgst_len(queue); - req->pdu = page_frag_alloc(&queue->pf_cache, + req->pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(struct nvme_tcp_cmd_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!req->pdu) @@ -1337,7 +1337,7 @@ static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl) { struct nvme_tcp_request *async = &ctrl->async_req; - page_frag_free(async->pdu); + page_frag_free_va(async->pdu); } static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl) @@ -1346,7 +1346,7 @@ static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl) struct nvme_tcp_request *async = &ctrl->async_req; u8 hdgst = nvme_tcp_hdgst_len(queue); - async->pdu = page_frag_alloc(&queue->pf_cache, + async->pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(struct nvme_tcp_cmd_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!async->pdu) diff --git a/drivers/nvme/target/tcp.c b/drivers/nvme/target/tcp.c index 380f22ee3ebb..bea3aa79ef43 100644 --- a/drivers/nvme/target/tcp.c +++ b/drivers/nvme/target/tcp.c @@ -1463,24 +1463,24 @@ static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_queue *queue, c->queue = queue; c->req.port = queue->port->nport; - c->cmd_pdu = page_frag_alloc(&queue->pf_cache, + c->cmd_pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(*c->cmd_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!c->cmd_pdu) return -ENOMEM; c->req.cmd = &c->cmd_pdu->cmd; - c->rsp_pdu = page_frag_alloc(&queue->pf_cache, + c->rsp_pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(*c->rsp_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!c->rsp_pdu) goto out_free_cmd; c->req.cqe = &c->rsp_pdu->cqe; - c->data_pdu = page_frag_alloc(&queue->pf_cache, + c->data_pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(*c->data_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!c->data_pdu) goto out_free_rsp; - c->r2t_pdu = page_frag_alloc(&queue->pf_cache, + c->r2t_pdu = page_frag_alloc_va(&queue->pf_cache, sizeof(*c->r2t_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); if (!c->r2t_pdu) goto out_free_data; @@ -1495,20 +1495,20 @@ static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_queue *queue, return 0; out_free_data: - page_frag_free(c->data_pdu); + page_frag_free_va(c->data_pdu); out_free_rsp: - page_frag_free(c->rsp_pdu); + page_frag_free_va(c->rsp_pdu); out_free_cmd: - page_frag_free(c->cmd_pdu); + page_frag_free_va(c->cmd_pdu); return -ENOMEM; } static void nvmet_tcp_free_cmd(struct nvmet_tcp_cmd *c) { - page_frag_free(c->r2t_pdu); - page_frag_free(c->data_pdu); - page_frag_free(c->rsp_pdu); - page_frag_free(c->cmd_pdu); + page_frag_free_va(c->r2t_pdu); + page_frag_free_va(c->data_pdu); + page_frag_free_va(c->rsp_pdu); + page_frag_free_va(c->cmd_pdu); } static int nvmet_tcp_alloc_cmds(struct nvmet_tcp_queue *queue) diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c index f16279351db5..6691fac01e0d 100644 --- a/drivers/vhost/net.c +++ b/drivers/vhost/net.c @@ -686,8 +686,8 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq, return -ENOSPC; buflen += SKB_DATA_ALIGN(len + pad); - buf = page_frag_alloc_align(&net->pf_cache, buflen, GFP_KERNEL, - SMP_CACHE_BYTES); + buf = page_frag_alloc_va_align(&net->pf_cache, buflen, GFP_KERNEL, + SMP_CACHE_BYTES); if (unlikely(!buf)) return -ENOMEM; @@ -734,7 +734,7 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq, return 0; err: - page_frag_free(buf); + page_frag_free_va(buf); return ret; } diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index b9411f0db25a..c6fde197a6eb 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -25,23 +25,24 @@ struct page_frag_cache { void page_frag_cache_drain(struct page_frag_cache *nc); void __page_frag_cache_drain(struct page *page, unsigned int count); -void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, - gfp_t gfp_mask, unsigned int align_mask); +void *__page_frag_alloc_va_align(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask, + unsigned int align_mask); -static inline void *page_frag_alloc_align(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask, - unsigned int align) +static inline void *page_frag_alloc_va_align(struct page_frag_cache *nc, + unsigned int fragsz, + gfp_t gfp_mask, unsigned int align) { WARN_ON_ONCE(!is_power_of_2(align) || align > PAGE_SIZE); - return __page_frag_alloc_align(nc, fragsz, gfp_mask, -align); + return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, -align); } -static inline void *page_frag_alloc(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask) +static inline void *page_frag_alloc_va(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask) { - return __page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u); + return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, ~0u); } -void page_frag_free(void *addr); +void page_frag_free_va(void *addr); #endif diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 6587dc1b3f70..c328700834cb 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -3358,7 +3358,7 @@ static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev, static inline void skb_free_frag(void *addr) { - page_frag_free(addr); + page_frag_free_va(addr); } void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask); diff --git a/kernel/bpf/cpumap.c b/kernel/bpf/cpumap.c index a8e34416e960..3a6a237e7dd3 100644 --- a/kernel/bpf/cpumap.c +++ b/kernel/bpf/cpumap.c @@ -322,7 +322,7 @@ static int cpu_map_kthread_run(void *data) /* Bring struct page memory area to curr CPU. Read by * build_skb_around via page_is_pfmemalloc(), and when - * freed written by page_frag_free call. + * freed written by page_frag_free_va call. */ prefetchw(page); } diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index d569021376d4..e5fd4acae003 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -61,9 +61,9 @@ void __page_frag_cache_drain(struct page *page, unsigned int count) } EXPORT_SYMBOL(__page_frag_cache_drain); -void *__page_frag_alloc_align(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask, - unsigned int align_mask) +void *__page_frag_alloc_va_align(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask, + unsigned int align_mask) { unsigned int size = PAGE_SIZE; struct page *page; @@ -128,16 +128,16 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, return nc->va + offset; } -EXPORT_SYMBOL(__page_frag_alloc_align); +EXPORT_SYMBOL(__page_frag_alloc_va_align); /* * Frees a page fragment allocated out of either a compound or order 0 page. */ -void page_frag_free(void *addr) +void page_frag_free_va(void *addr) { struct page *page = virt_to_head_page(addr); if (unlikely(put_page_testzero(page))) free_unref_page(page, compound_order(page)); } -EXPORT_SYMBOL(page_frag_free); +EXPORT_SYMBOL(page_frag_free_va); diff --git a/mm/page_frag_test.c b/mm/page_frag_test.c index 1349f6c6b521..c3cfce87fbbf 100644 --- a/mm/page_frag_test.c +++ b/mm/page_frag_test.c @@ -277,7 +277,7 @@ static int page_frag_pop_thread(void *arg) if (obj) { nr--; - page_frag_free(obj); + page_frag_free_va(obj); } else { cond_resched(); } @@ -305,17 +305,20 @@ static int page_frag_push_thread(void *arg) int ret; if (test_align) - va = page_frag_alloc_align(&test_frag, test_alloc_len, - GFP_KERNEL, SMP_CACHE_BYTES); + va = page_frag_alloc_va_align(&test_frag, + test_alloc_len, + GFP_KERNEL, + SMP_CACHE_BYTES); else - va = page_frag_alloc(&test_frag, test_alloc_len, GFP_KERNEL); + va = page_frag_alloc_va(&test_frag, test_alloc_len, + GFP_KERNEL); if (!va) continue; ret = objpool_push(va, pool); if (ret) { - page_frag_free(va); + page_frag_free_va(va); cond_resched(); } else { nr--; diff --git a/net/core/skbuff.c b/net/core/skbuff.c index c8ac79851cd6..6842fa6a71a5 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 Fri Jun 7 12:38:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13689831 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 D9C8DC27C5F for ; Fri, 7 Jun 2024 12:41:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4CC316B0088; Fri, 7 Jun 2024 08:41:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 47C786B00AB; Fri, 7 Jun 2024 08:41:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 31BF66B00AC; Fri, 7 Jun 2024 08:41:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 13F5D6B0088 for ; Fri, 7 Jun 2024 08:41:35 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 86CBA403F8 for ; Fri, 7 Jun 2024 12:41:34 +0000 (UTC) X-FDA: 82204053708.04.B1E5202 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf23.hostedemail.com (Postfix) with ESMTP id 2C90E14001F for ; Fri, 7 Jun 2024 12:41:31 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf23.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=1717764092; 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=wbJ2qSC9AHYT0hTg1VGtrN0o849L6WrA3O2jJfDdywQ=; b=5RwULCji0eugsREomNHDkPjxZ7HHr1g1qGRxYJaf9rdiJHXZHFxsSiBOgb+y5r3Nnc3H03 qR1uvjr+TsEROOb/3nMWF+3LNVGJuuFzRxeX36KDIWCGAO2fNcYWW+k82KaLIW54AjFLnI cu6l6jLTDuEybmZhspYAWJD4hFTOoMY= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf23.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=1717764092; a=rsa-sha256; cv=none; b=HulaGsoCXwhHm5zIGTRZeMq9xfisiz0UkIhcDeGiQy+hKG8LCpLiNz9ZTXGnsewqRm7dhk jKe6rcWMg+pdrRxcXCoZjgr0PUa/ZYZrLGGDfTUW+b3OWKdRz6zufCB87nFC6fSWVH9CS7 FX/ljMaNbalcv3Z4SrAcJmi792hK4Ck= Received: from mail.maildlp.com (unknown [172.19.88.105]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VwghB2pvkzsTCv; Fri, 7 Jun 2024 20:37:30 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 6ECE6140336; Fri, 7 Jun 2024 20:41:29 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 7 Jun 2024 20:41:29 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Andrew Morton , Eric Dumazet , David Howells , Marc Dionne , Chuck Lever , Jeff Layton , Neil Brown , Olga Kornievskaia , Dai Ngo , Tom Talpey , Trond Myklebust , Anna Schumaker , , , , , Subject: [PATCH net-next v7 07/15] mm: page_frag: avoid caller accessing 'page_frag_cache' directly Date: Fri, 7 Jun 2024 20:38:10 +0800 Message-ID: <20240607123819.40694-8-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240607123819.40694-1-linyunsheng@huawei.com> References: <20240607123819.40694-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 2C90E14001F X-Stat-Signature: iw6urjzz4xmkybrbo6fm6r8t7exnfycj X-HE-Tag: 1717764091-965371 X-HE-Meta: U2FsdGVkX1/d9t2082/mCgLY8A7On6gYpcgYY2kDaOjG6Xiuue5GqdHYUxBv99yJzZpqbvpMc3s7M+fwlwsvcBgg/FSgMRaKHrBE0Hsq/KRzs83hJ+JQ0kk5p5oNLBx/4F+0ZouhC7xLmdpSiQ0NCGIL9aTD9O+MlV1gUCl1P6MvVJ/9CFenA8ljjmFkCI5IIxESnoKj7Ziddyu0SXnnjgT2hGrFKgp0sLMjdLyzPeaetUmUdENHtl99NuGEb3yPE+oaUkfio5kYPSv9B4LzHV5lErww4CCYqqKTlZeawp9Uth2VtYbCccmpFbGHC7/ChRebtPwHhtynvHp0byIboycmYlSFElm6qMZwz6Viqhx3JoRLWaGEDC5lflDNBwa8V0VXfWB8dBIP6jY2NKpcQgyrShcTEGEY8rxNDA/1wycC3Jr10LD2f1k++zwIogd2g0GPUWoreMOyhM7EY9LM2R6G6B+APLU2OY74/LuW7ps2no0VkA2uNkemsAL+IYmoYLKtelFppvMf9FQ0N1NOO+RsE3iRKdsSCUlhUKv952mDBBbrx+o0+gBruaKMA70v+98jD8PcPkUQTyk1HSMFSb96QUebjr3Qdy7pceUPys0YE9jchPHJHCGxSafO+LeytI1oVDC9Zv+W0VJcap2rioh+znStDFdyk2LqC0SAfXzLrcr5hbfUJOnB+3I99NC5SPsqv3B3McQOYg5kaHObkNcp8GBSBOatjLV1Nm6s9CzD5Z3u7YrVqiS/SEpHojRv3uUXQh8ZDm3yYuAJB7VMR4/lRPG8EP3XindEd921J7/hyEnteQbq5xRmujswTN6pMMGZjlM6DvXLisB/CQMMSwKBUj6TXnct1hwaEzm2adT39ykM9VMYUWhJCzau4mW0EQNWEhIBHXHvEfI/vVRMNtNa8H8Uhr6S3FvA1m6c0jtaIm+qX2orjQYsi/i989GbEwkXFfD7j7ycFzHXtpq jQPehp6d BYDuvhMfT5O0Sj7J/PShSXeL27Gl6s4E217EHNp8EVWYfDE8qx5LTeth0NohM9cQVaBaXq0fHU+II8eCN2R8+Bza18BweYqN8r8i/S0zzPkZfZ7tFqw3c5s4z6Jw52EMdY5UC5U0J43mJyHuV7zuj3p6BtfkvW5bifJX0ojYVh0o5yHLRw+d2n0Xh3iX3iYKpjuClDQnUjP83S7NRUzpiPGn0xYxvZqT19FYjsFL8tOqB8018N7SHYMZ22V56FM7cdf6q0aCkYMpy6xz5UOZpAhJ4lhT09PPv20ki X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Use appropriate frag_page API instead of caller accessing 'page_frag_cache' directly. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- drivers/vhost/net.c | 2 +- include/linux/page_frag_cache.h | 10 ++++++++++ mm/page_frag_test.c | 2 +- net/core/skbuff.c | 6 +++--- net/rxrpc/conn_object.c | 4 +--- net/rxrpc/local_object.c | 4 +--- net/sunrpc/svcsock.c | 6 ++---- 7 files changed, 19 insertions(+), 15 deletions(-) diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c index 6691fac01e0d..b2737dc0dc50 100644 --- a/drivers/vhost/net.c +++ b/drivers/vhost/net.c @@ -1325,7 +1325,7 @@ static int vhost_net_open(struct inode *inode, struct file *f) vqs[VHOST_NET_VQ_RX]); f->private_data = n; - n->pf_cache.va = NULL; + page_frag_cache_init(&n->pf_cache); return 0; } diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index c6fde197a6eb..6ac3a25089d1 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -23,6 +23,16 @@ struct page_frag_cache { bool pfmemalloc; }; +static inline void page_frag_cache_init(struct page_frag_cache *nc) +{ + nc->va = NULL; +} + +static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc) +{ + return !!nc->pfmemalloc; +} + void page_frag_cache_drain(struct page_frag_cache *nc); void __page_frag_cache_drain(struct page *page, unsigned int count); void *__page_frag_alloc_va_align(struct page_frag_cache *nc, diff --git a/mm/page_frag_test.c b/mm/page_frag_test.c index c3cfce87fbbf..8b259e422fae 100644 --- a/mm/page_frag_test.c +++ b/mm/page_frag_test.c @@ -341,7 +341,7 @@ static int __init page_frag_test_init(void) u64 duration; int ret; - test_frag.va = NULL; + page_frag_cache_init(&test_frag); atomic_set(&nthreads, 2); init_completion(&wait); diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 6842fa6a71a5..ac5fe61c0ff2 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 Fri Jun 7 12:38:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13689832 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 C5950C27C6E for ; Fri, 7 Jun 2024 12:41:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 286DD6B00AC; Fri, 7 Jun 2024 08:41:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1E6306B00AD; Fri, 7 Jun 2024 08:41:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0AF676B00AE; Fri, 7 Jun 2024 08:41:37 -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 E04746B00AC for ; Fri, 7 Jun 2024 08:41:36 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 882AE1204AA for ; Fri, 7 Jun 2024 12:41:36 +0000 (UTC) X-FDA: 82204053792.23.3BA9BF2 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf02.hostedemail.com (Postfix) with ESMTP id 29DA780005 for ; Fri, 7 Jun 2024 12:41:33 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=none; spf=pass (imf02.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=1717764094; a=rsa-sha256; cv=none; b=o5k+Vf3qHWbpIYHhmpwd3fvmzc8NAnitPF5+JbDRMF1tYr/ucC9ngzS4PBi4nF4BwJ0z0o l9Il6A3/VrboRj+dI18Kq+HP1gY1hJlNukA/eeYJsTTK8Y0zmdJ7kNM6KpbaTVNG2XoSPx ehpqMFQ7T/leJhHosQbfFcdcWXh8Xb0= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; spf=pass (imf02.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=1717764094; 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=pIexgbne/U+n9PjZCJykNzJopagK/QnEYuCoL9LEXeo=; b=PMnKXFIZN/+S5+xC8+sudbkh3n0SptmJfFOkPelD/mU2H/8GKFebS730jnRiNVK1QMSs6Z ndWDiGKch+TclQehx7xmIZkFeIJKH1NdFnda2B/e/OXWZJhNrHi2I0Gwf1XpTUlnfcL0DO qBMhrLiAg8H7wF0JrEfCK7epcrydhOY= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VwghD4QhVzsR5Q; Fri, 7 Jun 2024 20:37:32 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id A7DE914011A; Fri, 7 Jun 2024 20:41:31 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 7 Jun 2024 20:41:31 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v7 08/15] mm: page_frag: reuse existing space for 'size' and 'pfmemalloc' Date: Fri, 7 Jun 2024 20:38:11 +0800 Message-ID: <20240607123819.40694-9-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240607123819.40694-1-linyunsheng@huawei.com> References: <20240607123819.40694-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 29DA780005 X-Stat-Signature: ooufe71fdodzci3d6im4h5fu8eeccj8f X-HE-Tag: 1717764093-467758 X-HE-Meta: U2FsdGVkX18G0xf5fAVDQl6sR/qalGZUUDycBtUPMRWySm3K2j2fHEHXUR3fpRiwAgKpqiir1pVotQLNixtDjEGryourpnJohnRQ6VoAAaDwmp0ph78geYzoPKu3oA1NhDoRrjPdUFnmVCYs7D/WFrA2UFYrJOX200Pot3UjgjyXQTDL2K/8C273yserp85MZBxiXDRIEtSY3ig9shEHarJjo2u+XN3whC83xE8RqcRV+70q+1NZG7mleMyQLKX1XU1oq20nBSbMmocR5lfXcWAaFx9N8AyfaPh6XUlT5y2QO3yEjE44+5DupgBZtG0oM22bGK6Upj1fuwziGVRrefIEgFN8kicYfA5EhW38OCAO/R+3V1Hki3JPSoyMREW+bjGkwhfHfi4SpbX6jHKqmlsPykBfT9JSLlIlZEm5FzDMSXyde6GFM/KW5CgIRd+fkP9ZpSJg5ordomEVFRBv/G+J2Z0N4AnBNvZ7bsUvCh+ZVmd43NuSTBN/TR5Cx0eWwZPV049NJHRjH0fwGe/337H4Px+RsYwiFx0wOtS+vmbPEdYzZwiSfQ2qumyxyO+s86f0W6ZC9bLjSA76MGtAHeSbZ2c3Po6/M+o502Cgssw8KSuVNUwa1LR1vIDSsXl905Xzi9FpMLVWq1C3T5gGTY9Ki1P1JmokZMe8wfNLLqu31InZw+iCarodX8r+qMdaXolCvwU7PBicyf2WnEcAv2BC5n60KuqlMcMZtFowIZLaZvjSy4EphQrE0ytRJU47dAEqpEF0LLXnvbVVoAc083HfRForcZioJP09/Jd1n0QfgjDMOMGvvVCs0hjQ/rOtxcBWmYJmfRt/XV8v2rl4Ytb4YCvr/bx74t8TsyPD8a7GbokKeeU6CZadCv4XgKqgv2s0oS7iwpDdo+5G78DJd02I1glz9VNsh2BZ5Fx9wHQeDmoO7BC6osWS4sM/iVzz5LIlkYGqoZ/l3Kdr733 AAlk35XZ /usgoIQeVQDoD7lFddV9WeADzqV66+N8TxLsqTsTOuBIBhhg2dSdZQTix6n2Vdvr052QxUMyeMD5DzjGshnEyTvhw8nnPRuJ10E1gJ79ZbXl8w6Y/wcTWl3ZdPnwMfocQKE8hgqO900WgepJ7eyG+nj+BOu1g7HMa0SEsrUFnGZ+/z+SCFe9fTUNU/Scaa9TxwhfdLgfUt9nEqcw4MgU2SnA9y8gV626YnSMPvi76ahPhU9obRWZYkgQIBae3LlxCj55TVy4DHU7jiqgPEXtfAEQWEBuOupQdjdgb X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Currently there is one 'struct page_frag' for every 'struct sock' and 'struct task_struct', we are about to replace the 'struct page_frag' with 'struct page_frag_cache' for them. Before begin the replacing, we need to ensure the size of 'struct page_frag_cache' is not bigger than the size of 'struct page_frag', as there may be tens of thousands of 'struct sock' and 'struct task_struct' instances in the system. By or'ing the page order & pfmemalloc with lower bits of 'va' instead of using 'u16' or 'u32' for page size and 'u8' for pfmemalloc, we are able to avoid 3 or 5 bytes space waste. And page address & pfmemalloc & order is unchanged for the same page in the same 'page_frag_cache' instance, it makes sense to fit them together. Also, it is better to replace 'offset' with 'remaining', which is the remaining size for the cache in a 'page_frag_cache' instance, we are able to do a single 'fragsz > remaining' checking for the case of cache not being enough, which should be the fast path if we ensure size is zoro when 'va' == NULL by memset'ing 'struct page_frag_cache' in page_frag_cache_init() and page_frag_cache_drain(). After this patch, the size of 'struct page_frag_cache' should be the same as the size of 'struct page_frag'. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 76 +++++++++++++++++++++++----- mm/page_frag_cache.c | 90 ++++++++++++++++++++------------- 2 files changed, 118 insertions(+), 48 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 6ac3a25089d1..b33904d4494f 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -8,29 +8,81 @@ #define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK) #define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE) -struct page_frag_cache { - void *va; +/* + * struct encoded_va - a nonexistent type marking this pointer + * + * An 'encoded_va' pointer is a pointer to a aligned virtual address, which is + * at least aligned to PAGE_SIZE, that means there are at least 12 lower bits + * space available for other purposes. + * + * Currently we use the lower 8 bits and bit 9 for the order and PFMEMALLOC + * flag of the page this 'va' is corresponding to. + * + * Use the supplied helper functions to endcode/decode the pointer and bits. + */ +struct encoded_va; + +#define PAGE_FRAG_CACHE_ORDER_MASK GENMASK(7, 0) +#define PAGE_FRAG_CACHE_PFMEMALLOC_BIT BIT(8) +#define PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT 8 + +static inline struct encoded_va *encode_aligned_va(void *va, + unsigned int order, + bool pfmemalloc) +{ #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - __u16 offset; - __u16 size; + return (struct encoded_va *)((unsigned long)va | order | + pfmemalloc << PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT); #else - __u32 offset; + return (struct encoded_va *)((unsigned long)va | + pfmemalloc << PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT); +#endif +} + +static inline unsigned long encoded_page_order(struct encoded_va *encoded_va) +{ +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + return PAGE_FRAG_CACHE_ORDER_MASK & (unsigned long)encoded_va; +#else + return 0; +#endif +} + +static inline bool encoded_page_pfmemalloc(struct encoded_va *encoded_va) +{ + return PAGE_FRAG_CACHE_PFMEMALLOC_BIT & (unsigned long)encoded_va; +} + +static inline void *encoded_page_address(struct encoded_va *encoded_va) +{ + return (void *)((unsigned long)encoded_va & PAGE_MASK); +} + +struct page_frag_cache { + struct encoded_va *encoded_va; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) && (BITS_PER_LONG <= 32) + u16 pagecnt_bias; + u16 remaining; +#else + u32 pagecnt_bias; + u32 remaining; #endif - /* we maintain a pagecount bias, so that we dont dirty cache line - * containing page->_refcount every time we allocate a fragment. - */ - unsigned int pagecnt_bias; - bool pfmemalloc; }; static inline void page_frag_cache_init(struct page_frag_cache *nc) { - nc->va = NULL; + memset(nc, 0, sizeof(*nc)); } static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc) { - return !!nc->pfmemalloc; + return encoded_page_pfmemalloc(nc->encoded_va); +} + +static inline unsigned int page_frag_cache_page_size(struct encoded_va *encoded_va) +{ + return PAGE_SIZE << encoded_page_order(encoded_va); } void page_frag_cache_drain(struct page_frag_cache *nc); diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index e5fd4acae003..756ef68cce21 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -18,11 +18,20 @@ #include #include "internal.h" +static void *page_frag_cache_current_va(struct page_frag_cache *nc) +{ + struct encoded_va *encoded_va = nc->encoded_va; + + return (void *)(((unsigned long)encoded_va & PAGE_MASK) | + (page_frag_cache_page_size(encoded_va) - nc->remaining)); +} + static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, gfp_t gfp_mask) { struct page *page = NULL; gfp_t gfp = gfp_mask; + unsigned int order; #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) /* Ensure free_unref_page() can be used to free the page fragment */ @@ -32,23 +41,41 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, PAGE_FRAG_CACHE_MAX_ORDER); - nc->size = page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; #endif - if (unlikely(!page)) + if (unlikely(!page)) { page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); + if (unlikely(!page)) { + memset(nc, 0, sizeof(*nc)); + return NULL; + } + + order = 0; + nc->remaining = PAGE_SIZE; + } else { + order = PAGE_FRAG_CACHE_MAX_ORDER; + nc->remaining = PAGE_FRAG_CACHE_MAX_SIZE; + } - nc->va = page ? page_address(page) : NULL; + /* Even if we own the page, we do not use atomic_set(). + * This would break get_page_unless_zero() users. + */ + page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); + /* reset page count bias of new frag */ + nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; + nc->encoded_va = encode_aligned_va(page_address(page), order, + page_is_pfmemalloc(page)); return page; } void page_frag_cache_drain(struct page_frag_cache *nc) { - if (!nc->va) + if (!nc->encoded_va) return; - __page_frag_cache_drain(virt_to_head_page(nc->va), nc->pagecnt_bias); - nc->va = NULL; + __page_frag_cache_drain(virt_to_head_page(nc->encoded_va), + nc->pagecnt_bias); + memset(nc, 0, sizeof(*nc)); } EXPORT_SYMBOL(page_frag_cache_drain); @@ -65,51 +92,41 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) { - unsigned int size = PAGE_SIZE; + struct encoded_va *encoded_va = nc->encoded_va; struct page *page; - int offset; + int remaining; + void *va; - if (unlikely(!nc->va)) { + if (unlikely(!encoded_va)) { refill: - page = __page_frag_cache_refill(nc, gfp_mask); - if (!page) + if (unlikely(!__page_frag_cache_refill(nc, gfp_mask))) return NULL; - /* Even if we own the page, we do not use atomic_set(). - * This would break get_page_unless_zero() users. - */ - page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); - - /* reset page count bias and offset to start of new frag */ - nc->pfmemalloc = page_is_pfmemalloc(page); - nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset = 0; + encoded_va = nc->encoded_va; } -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif - - offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); - if (unlikely(offset + fragsz > size)) { - page = virt_to_page(nc->va); - + remaining = nc->remaining & align_mask; + remaining -= fragsz; + if (unlikely(remaining < 0)) { + page = virt_to_page(encoded_va); if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) goto refill; - if (unlikely(nc->pfmemalloc)) { - free_unref_page(page, compound_order(page)); + if (unlikely(encoded_page_pfmemalloc(encoded_va))) { + VM_BUG_ON(compound_order(page) != + encoded_page_order(encoded_va)); + free_unref_page(page, encoded_page_order(encoded_va)); goto refill; } /* OK, page count is 0, we can safely set it */ set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); - /* reset page count bias and offset to start of new frag */ + /* reset page count bias and remaining of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset = 0; - if (unlikely(fragsz > PAGE_SIZE)) { + nc->remaining = remaining = page_frag_cache_page_size(encoded_va); + remaining -= fragsz; + if (unlikely(remaining < 0)) { /* * The caller is trying to allocate a fragment * with fragsz > PAGE_SIZE but the cache isn't big @@ -123,10 +140,11 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, } } + va = page_frag_cache_current_va(nc); nc->pagecnt_bias--; - nc->offset = offset + fragsz; + nc->remaining = remaining; - return nc->va + offset; + return va; } EXPORT_SYMBOL(__page_frag_alloc_va_align); From patchwork Fri Jun 7 12:38:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13689833 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 50FF6C27C53 for ; Fri, 7 Jun 2024 12:41:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D7FBD6B00AE; Fri, 7 Jun 2024 08:41:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D0AB86B00AF; Fri, 7 Jun 2024 08:41:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B5A406B00B1; Fri, 7 Jun 2024 08:41:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 9763B6B00AE for ; Fri, 7 Jun 2024 08:41:39 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 5FA51A04CB for ; Fri, 7 Jun 2024 12:41:39 +0000 (UTC) X-FDA: 82204053918.20.2F6DC26 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf06.hostedemail.com (Postfix) with ESMTP id B2767180015 for ; Fri, 7 Jun 2024 12:41:36 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=none; spf=pass (imf06.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717764097; 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=1CGtCMHvPErhP/ku6clFd74qAjNGPU/Wh2Wb/FHnvmg=; b=qjSCOCuo89CzyT/yltZKXSmcAEqrh8TY7vqGgMKFeTvVbWebJOIe2ava7cmpMIQfbuc20l fLBuvDeoBgi6AOlOg50VMPuBczbvLGorZy04ek6Tr1e+6x7G5v4iEQPjBsS1tMAQor9OeK iiA1K9dKsCdI3n4dujFEvjN6yZSK+RQ= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=none; spf=pass (imf06.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717764097; a=rsa-sha256; cv=none; b=Vo/TBvLSo5X6Q/8zE4nojsMmsd9vMdaCULy81FR+0bbD2Zj41IbG7am6+8F43mwRVVbDAp lfziTR27HJq7pXyxa3YBUbuAEMu+2t1BFA/OIqto0Ljj0F8Gl+oOUFJi8Z/YyS6JFkGCdl +HAkomrU2LITLabznkPnnYcGdMc8Doc= Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VwghG1fDHzwSJh; Fri, 7 Jun 2024 20:37:34 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 950F718007A; Fri, 7 Jun 2024 20:41:33 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 7 Jun 2024 20:41:33 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v7 09/15] mm: page_frag: some minor refactoring before adding new API Date: Fri, 7 Jun 2024 20:38:12 +0800 Message-ID: <20240607123819.40694-10-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240607123819.40694-1-linyunsheng@huawei.com> References: <20240607123819.40694-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: B2767180015 X-Stat-Signature: cki5yyftj3sc4gubyinos436uygxfmbf X-HE-Tag: 1717764096-238567 X-HE-Meta: U2FsdGVkX18ZZmS6n0SfTCOqEq0+4ul4V37HL+XoNiQMsI7hJbdUcpztsKULSNXqq1nqLgeLA4HOBhZjRJZKIZf2nNiyp9xiQRwdPe5dDjwKrwC3tPCRINd6046j5+wF+wyDp6Pd50qWHIBbDW8KhDpjsiehaqt8GhSaqpyB16qdiYndNJWuA7DslbB0tcWNiQN7Qm1bL8+VkODs5r1+zunbkiQ3uQkxVrLw4QxGNb64gjSMejg7b0DXyqh+5NAoShxf0BNUZl01r0jHRKhsmtEI0isCKh65EFn5MTL/sGHq2l+zzeFtwwIEf7HGTeq8ll49WGIpQxV4qyieURx/oWiOgy3Su9PTmNAbkUgt1hwevQ/s/xXej4LcGlIlx69mpGHsyRyPVYPqZ652f9tfyk1ZJAaYVa8h5QyBqCd0SG+YHK+WXWyIIEXUCuww0SGoD/6+/R4Uu8pjKiGjqiw5HCWIcyxMUJoG5zbFAG8U9VVjQpWe6GUwAmWXxMu0HMcDwhE3JaRwTEQZP3dL2LwC+SNira0Q3c6IPP4MpDjS3xIPW75/WuKR3SFyCLMYhKZ2egXVOWz5OJmtlg2kRZsbaVGQ0OWqNC/F0OSD5Kqc9o4WLEIxnQteA67EexvmM+JnUlFrLvtK5pDPSZZeDSoWQUR/bkJj8DeDLIBsEBCBqeHy3iMnA3dwVTxn+HeeItdagorg8YBuNejp/TPQv6FNoKMeEnNbVAxb5fs0RLsSW+UDspE53flzB55P/IdR88FckT/UtDmiJa3/ScN2zajri5NlkcP875tgW1o+671zIE6wLxAAvgeTvtTWOJiNICVSsMebEuzetQJSdOwSHXqXoBTy5gJpjMxsCN4E5IPJL4dM7H3Enbg0XGUuPtij/oBKfD+LL86KE3p1lM6vf0v8FermsPbNlAUli6va+l6hABjRUAVHXbYIgkyyPnlcbwCuXJCk+4mS+9h/Qb+gO9V kcvydrmn 0i7q/2C13xaoJe+bLMAi/NgtB8VM5GUYp5a7Qy/7wGe/XhAU2SHVrMNK4aQHfi9n2qaebKxiGVytXq0lr1RPhNSUItzg2rJTXKy7b+GACnZa8zs+xUnCkZuA1JXASQM9vKVAOoyApe3vDwizFA7tq630HIT1fWIE7s/6k2AOpUSbMO2ivkknAsbswz/644FOpTChAwxT6QGCQl3kzYGaQTFGw51C1ATqKhdQLmcMwcaRgOOj1FKuwj/rkTVhBvdabB4QKzNj2ruIGjd6yDc7QJ8v8ngNYfgYpaiA2 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Refactor common codes from __page_frag_alloc_va_align() to __page_frag_cache_refill(), so that the new API can make use of them. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- mm/page_frag_cache.c | 61 ++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 756ef68cce21..b5ad6e9d316d 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -29,10 +29,36 @@ static void *page_frag_cache_current_va(struct page_frag_cache *nc) static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, gfp_t gfp_mask) { - struct page *page = NULL; + struct encoded_va *encoded_va = nc->encoded_va; gfp_t gfp = gfp_mask; unsigned int order; + struct page *page; + + if (unlikely(!encoded_va)) + goto alloc; + + page = virt_to_page(encoded_va); + if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) + goto alloc; + + if (unlikely(encoded_page_pfmemalloc(encoded_va))) { + VM_BUG_ON(compound_order(page) != + encoded_page_order(encoded_va)); + free_unref_page(page, encoded_page_order(encoded_va)); + goto alloc; + } + + /* OK, page count is 0, we can safely set it */ + set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); + + /* reset page count bias and remaining of new frag */ + nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; + nc->remaining = page_frag_cache_page_size(encoded_va); + + return page; +alloc: + page = NULL; #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) /* Ensure free_unref_page() can be used to free the page fragment */ BUILD_BUG_ON(PAGE_FRAG_CACHE_MAX_ORDER > PAGE_ALLOC_COSTLY_ORDER); @@ -92,40 +118,15 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) { - struct encoded_va *encoded_va = nc->encoded_va; - struct page *page; - int remaining; + int remaining = nc->remaining & align_mask; void *va; - if (unlikely(!encoded_va)) { -refill: - if (unlikely(!__page_frag_cache_refill(nc, gfp_mask))) - return NULL; - - encoded_va = nc->encoded_va; - } - - remaining = nc->remaining & align_mask; remaining -= fragsz; if (unlikely(remaining < 0)) { - page = virt_to_page(encoded_va); - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) - goto refill; - - if (unlikely(encoded_page_pfmemalloc(encoded_va))) { - VM_BUG_ON(compound_order(page) != - encoded_page_order(encoded_va)); - free_unref_page(page, encoded_page_order(encoded_va)); - goto refill; - } - - /* OK, page count is 0, we can safely set it */ - set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); + if (unlikely(!__page_frag_cache_refill(nc, gfp_mask))) + return NULL; - /* reset page count bias and remaining of new frag */ - nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->remaining = remaining = page_frag_cache_page_size(encoded_va); - remaining -= fragsz; + remaining = nc->remaining - fragsz; if (unlikely(remaining < 0)) { /* * The caller is trying to allocate a fragment From patchwork Fri Jun 7 12:38:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13689834 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 05C46C27C6F for ; Fri, 7 Jun 2024 12:41:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E90086B00AF; Fri, 7 Jun 2024 08:41:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E3F8F6B00B1; Fri, 7 Jun 2024 08:41:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CE28D6B00B2; Fri, 7 Jun 2024 08:41:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 9F7B56B00AF for ; Fri, 7 Jun 2024 08:41:41 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 5D4A3A37DE for ; Fri, 7 Jun 2024 12:41:41 +0000 (UTC) X-FDA: 82204054002.02.5F71B66 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf28.hostedemail.com (Postfix) with ESMTP id F3A5FC001C for ; Fri, 7 Jun 2024 12:41:38 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf28.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=1717764099; 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=ytpSUSdXQSTx1QUBqfE54CwlVUd8fK2vWO74sKZDsPk=; b=H3j5LEmDoEpUWbL8ShJ0JGZzaNYA7HZBc/w1FlXRA5ZJ6OytRJII8g0BVRgpZcZc/5Ld9B YzAdW++kNHIdPGFvRKd/UPNNv/hYATLRvRHEgov5o5iSqnwk6tCDOqxkvWyZvcHzI+1egh GgQ3yj+jfZi8QoQSTPIEFFfKubh/Pf4= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf28.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=1717764099; a=rsa-sha256; cv=none; b=j04UvUxNx04oF3yvZOJAFPq+GLYGtbED4MHxmjscFYQJgJei+JvauMZ6meswGeyXkaWYLI pHrJlt5bS9FxPFefMVCqboGf3Imjche64AbfXlTrQO2xclBDQ7XUSzsRLhdbPaNfPx4iI2 ObDAfSERLD592tv2VCUuIN1Nk4oJHwc= Received: from mail.maildlp.com (unknown [172.19.162.112]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4Vwghc2pprz355Yr; Fri, 7 Jun 2024 20:37:52 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 7116D1400D3; Fri, 7 Jun 2024 20:41:35 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 7 Jun 2024 20:41:35 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v7 10/15] mm: page_frag: use __alloc_pages() to replace alloc_pages_node() Date: Fri, 7 Jun 2024 20:38:13 +0800 Message-ID: <20240607123819.40694-11-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240607123819.40694-1-linyunsheng@huawei.com> References: <20240607123819.40694-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Queue-Id: F3A5FC001C X-Stat-Signature: pdyb67bodnpb7i6epmn9nisado4zz1o8 X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1717764098-8180 X-HE-Meta: U2FsdGVkX18qeFNsgeb+MUz4XoK7C4a+wMZ9W7tzYtYHXxlEjD3rpIGJGX3kP0Lp3fBzP55ToqoCoKsnOhi5B+fCHoqn+5yfXgMruYTGOW31KRyeY0kOk59wFMZHjI1bKcjvHZtkT+n3YyYxyf80Ww5JWVHuEk8E0qQ5nzu9HRTonuNjbPOfYVvPme8XJ88MmsWt5aOHnMPL03lWkQ5kzcDRQi5M1W8qeUnO04xIJS5hxcS7alpcdL1mhxb5p0J77IWG1Wo0sV3u0AY+esR9Zb4yKF3lOlfKJYIDz284oOOBUcEAb9uGlTUsEruuHfBFTYEjnm+g4ATDkFhjtfVDoesXFRLTfzAvfHTGq/um/ODVDol5hPt2oS1H+MNdBcQ64P565cWgxBPiaz3Y7XRh0mmodbqt9k1LNVqLVurVV24tTCVgpe997Cl16JOPpA1lkukfG2B7SNY9fyV3wAPAGRVwQV9ApUUpVDQ8DEcjD0GLKaEOqJ/o+bOGXnUS2sbfKVNY5M8OOX34SGlZEo7HB32pIQLEUqcGxlZJ7YWOCe95gWxgTVjKbnQah0pMxMAl6A0VmaHbiYXGzL4TC3SqT0KGfXD+INLMFoqi2npC/vLyoBZJ3Lo/Y1Mt2rfocR8YqeV1sX5JRCf3itx1WDpERyWCV7hym3m7drtB65qxsr5jnD49OdjIe4YGUesxZKvI0RSjJP4HYKO4aXnHri4hL0uhHYzV3c+hI3zE+ybX592zmH2eUY9FXZ9xcXXY3wjrpadVK2q2pp16DQgVxkCCekgr4GbfO2r1AQD0KjuoVDoCn0uI/VN5rEKKoIsc++nt3eksLcQFFKqffntAwta1W/BMI+vQpjmn4u2agpSpYme4Ng5vapZPfqzbjykUY1dcB217QSnjWYOYsIVcn1ew4I03ZwBqw5P1uD0IceMBPPEe7fnPfoIQYDvpavjQGF0R3LhXx8pdrhj2g1YoZwQ l3eiSF6P PzCLJ470mkqD4YmPwMPdRfxjcUVY7ruNAAdF+l3Ed84oDEag7wCXNPFzKNSZDhFwlssQ85mywJacBxlPl2x2RzSwFPGDvjKSbQ1st4PEOTtofxPa6NQsz9zF93wCoFjO8ZHv1LPSWx4QSemLhTrLq8m7sE7BmSdK0QV2HYANZ/uLATBx18fQi4+4RVf0YOmQMUuZlCDElp8/mjD2Jefbrg4WL6zLUZiNar6z7tbjmeExuL5WFpt0SWUQmAVGigUee3TS9Voh5u6o7rYqnnMKnkuK1TkD6qlw6RLC9 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: There are more new APIs calling __page_frag_cache_refill() in this patchset, which may cause compiler not being able to inline __page_frag_cache_refill() into __page_frag_alloc_va_align(). Not being able to do the inlining seems to casue some notiable performance degradation in arm64 system with 64K PAGE_SIZE after adding new API calling __page_frag_cache_refill(). It seems there is about 24Bytes binary size increase for __page_frag_cache_refill() and __page_frag_cache_refill() in arm64 system with 64K PAGE_SIZE. By doing the gdb disassembling, It seems we can have more than 100Bytes decrease for the binary size by using __alloc_pages() to replace alloc_pages_node(), as there seems to be some unnecessary checking for nid being NUMA_NO_NODE, especially when page_frag is still part of the mm system. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- mm/page_frag_cache.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index b5ad6e9d316d..525b577b03a9 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -65,11 +65,11 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, gfp_mask = (gfp_mask & ~__GFP_DIRECT_RECLAIM) | __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; - page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, - PAGE_FRAG_CACHE_MAX_ORDER); + page = __alloc_pages(gfp_mask, PAGE_FRAG_CACHE_MAX_ORDER, + numa_mem_id(), NULL); #endif if (unlikely(!page)) { - page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); + page = __alloc_pages(gfp, 0, numa_mem_id(), NULL); if (unlikely(!page)) { memset(nc, 0, sizeof(*nc)); return NULL; From patchwork Fri Jun 7 12:38:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13689835 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 D7684C27C53 for ; Fri, 7 Jun 2024 12:41:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5DE136B00B3; Fri, 7 Jun 2024 08:41:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 58D456B00B4; Fri, 7 Jun 2024 08:41:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4098E6B00B5; Fri, 7 Jun 2024 08:41:45 -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 1D6D76B00B3 for ; Fri, 7 Jun 2024 08:41:45 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id CCB3914047D for ; Fri, 7 Jun 2024 12:41:44 +0000 (UTC) X-FDA: 82204054128.26.009BE60 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf15.hostedemail.com (Postfix) with ESMTP id 6560BA0004 for ; Fri, 7 Jun 2024 12:41:42 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf15.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=1717764103; 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=LAM7fkxS0eW5kauOyLML4k0MuzeYOhhEhnKziaG//wI=; b=nLlnRG9oBu0WMekKYKalPrdF6uw/jVpc7zgCQq/LSxEkbJ9m/IkD25/RAaeQLi8Lr8crSa 62JkrHxp1/+fk5uMwnu/70Fijo+kl25WEs/pNCSstyGWRKImnPnsDrrYN/klpIf0b5/3hH vPyXpyhIVhDMX7qMD7GPOdk1nCwGo4Q= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717764103; a=rsa-sha256; cv=none; b=2bQwRasgTzlPjqPlNN9Im0RPftwiylog8vslKELIZQLga6ACZJEPjdzFRv98eeKUYiZySy RK8wEdNnMLtKSBFs7wSjtoP73/J+P7+9nNiQY4mo+7SZLtx557UZ7ZPcQKKryIgtoYwNj4 r1tMcwwBzL8KNNpS671qZbH8Uir/ftM= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf15.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VwghN3SD9zwSJH; Fri, 7 Jun 2024 20:37:40 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id D544B18007A; Fri, 7 Jun 2024 20:41:39 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 7 Jun 2024 20:41:39 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v7 12/15] mm: page_frag: introduce prepare/probe/commit API Date: Fri, 7 Jun 2024 20:38:15 +0800 Message-ID: <20240607123819.40694-13-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240607123819.40694-1-linyunsheng@huawei.com> References: <20240607123819.40694-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemf200006.china.huawei.com (7.185.36.61) X-Stat-Signature: oi6zk1cud448fspqqpucayyhiwxdo819 X-Rspamd-Queue-Id: 6560BA0004 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1717764102-398554 X-HE-Meta: U2FsdGVkX19z7LVnjvdLDABrc7VT6JnK/fHJ5/sCJpdpYcOfjbzBPNTJ64DeVnB6Q2QVs9ydgQ5I4aWLLuadKcKT0WB434jv/3cirHpiaeQPqFbv5CyrrOxzc3zLlzb/GVtAjboIFip1RiwyfknSXkipvwpj/PFoPolREj2G/aGqNgIGzd/miam9/T3i2IRuRa3bB+5WaQSdD5NJKBur7gipyKYXujhfE8lm3HE+7cRK7v3WZF30ruw8diaMxNKtFmhES7NZG22q/+M+lJlCQ+Zm7lnIxuZoGiCuhG7z6t6g23hqv1L7clUu1iFYnL3mNJb21B72WMAokPlN919QmTzZXPbhCH5JJuRlEI93TXrx7Xpn8x5KejMlmuax8Q2xggbBZiPcKLCEp/ZL15nDrekmwThhMU+WNfsaigVzWRQrEi66Qeyg8y/DzGD09sRDdyiX2uz+fd+gjqMLsRzSRtNniK1WrePcQLUdwk3Enyw+BI1xrV9hHSme7uoVOW4TMxn/Gkc6bgaPvG0TiCA9AaTiuWDHOD5UqWj/g9GqvluEm2Ig/0djeFpKF7k4qGKduLccEDKlVT+dlJTR1TSB65ulOcobGhMRTMWTqM9F8F2b8bT3INGr2Zs9ybrrYqUdM8zr3U1hs3N/4LR4k1xg2o7QjWIXGo36Lr2co2A9mXezJkEmA2DaGjJM014LUaVE/6TEv6dZX+DuYOHv/xR8EphJculresLvRN0qk/tcT9O/66mCbabtvBEIIks51DEjkkjbWZ73ussGtwnDXFVeELSpKM5Qy4KZ/s0n521EwfDCdqK1LddWpQppq4m8GffD70xyQZ73ACb+p43lnsthjItOHJD94fxG/cPsYc8VQum50Fm6WtPRXKLz/LTrB7oNf0PlmdAVZZwq0UosZ12LU6hCNUjthTUDA5AIh10z5i+CeuceR+hrL8WwtyHhcDh15IzDOsaic4nQ4cozW+T EUJhnwjC b79oanLNDo34lOruOPw1DThAQg183VfstY6kb7YBA76gN8xQNqKz7sXgHxRXDT0/vSzoAjkA/oULS5jOecqa6hL3Q/MMKpdIfTFks5/xqzn8C8vpDkg8opx0yMp4WRacY7fHoSfLMXlyyFP4LDpCiCTXQaieYoPRuWC9rBZK4Y3DIGG0qQWnBp9TPN5r6n0K6Z6CR4bec6Y0Pc6Mu5urvVxVHXzcNI8akxRfCTW9N57nwcaZJgTe3CkfrAO3ae9uKf5D5s/IK492a2zKDvzvcxL+p0RN/HuUzXwZN X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: There are many use cases that need minimum memory in order for forward progress, but more performant if more memory is available or need to probe the cache info to use any memory available for frag caoleasing reason. Currently skb_page_frag_refill() API is used to solve the above use cases, but caller needs to know about the internal detail and access the data field of 'struct page_frag' to meet the requirement of the above use cases and its implementation is similar to the one in mm subsystem. To unify those two page_frag implementations, introduce a prepare API to ensure minimum memory is satisfied and return how much the actual memory is available to the caller and a probe API to report the current available memory to caller without doing cache refilling. The caller needs to either call the commit API to report how much memory it actually uses, or not do so if deciding to not use any memory. As next patch is about to replace 'struct page_frag' with 'struct page_frag_cache' in linux/sched.h, which is included by the asm-offsets.s, using the virt_to_page() in the inline helper of page_frag_cache.h cause a "'vmemmap' undeclared" compiling error for asm-offsets.s, use a macro for probe API to avoid that compiling error. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 82 +++++++++++++++++++++++ mm/page_frag_cache.c | 114 ++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index b33904d4494f..e95d44a36ec9 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -4,6 +4,7 @@ #define _LINUX_PAGE_FRAG_CACHE_H #include +#include #define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK) #define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE) @@ -87,6 +88,9 @@ static inline unsigned int page_frag_cache_page_size(struct encoded_va *encoded_ void page_frag_cache_drain(struct page_frag_cache *nc); void __page_frag_cache_drain(struct page *page, unsigned int count); +struct page *page_frag_alloc_pg(struct page_frag_cache *nc, + unsigned int *offset, unsigned int fragsz, + gfp_t gfp); void *__page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask); @@ -99,12 +103,90 @@ static inline void *page_frag_alloc_va_align(struct page_frag_cache *nc, return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, -align); } +static inline unsigned int page_frag_cache_page_offset(const struct page_frag_cache *nc) +{ + return page_frag_cache_page_size(nc->encoded_va) - nc->remaining; +} + static inline void *page_frag_alloc_va(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask) { return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, ~0u); } +void *page_frag_alloc_va_prepare(struct page_frag_cache *nc, unsigned int *fragsz, + gfp_t gfp); + +static inline void *page_frag_alloc_va_prepare_align(struct page_frag_cache *nc, + unsigned int *fragsz, + gfp_t gfp, + unsigned int align) +{ + WARN_ON_ONCE(!is_power_of_2(align) || align > PAGE_SIZE); + nc->remaining = nc->remaining & -align; + return page_frag_alloc_va_prepare(nc, fragsz, gfp); +} + +struct page *page_frag_alloc_pg_prepare(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *fragsz, gfp_t gfp); + +struct page *page_frag_alloc_prepare(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *fragsz, + void **va, gfp_t gfp); + +static inline struct encoded_va *__page_frag_alloc_probe(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *fragsz, + void **va) +{ + struct encoded_va *encoded_va; + + *fragsz = nc->remaining; + encoded_va = nc->encoded_va; + *offset = page_frag_cache_page_size(encoded_va) - *fragsz; + *va = encoded_page_address(encoded_va) + *offset; + + return encoded_va; +} + +#define page_frag_alloc_probe(nc, offset, fragsz, va) \ +({ \ + struct page *__page = NULL; \ + \ + VM_BUG_ON(!*(fragsz)); \ + if (likely((nc)->remaining >= *(fragsz))) \ + __page = virt_to_page(__page_frag_alloc_probe(nc, \ + offset, \ + fragsz, \ + va)); \ + \ + __page; \ +}) + +static inline void page_frag_alloc_commit(struct page_frag_cache *nc, + unsigned int fragsz) +{ + VM_BUG_ON(fragsz > nc->remaining || !nc->pagecnt_bias); + nc->pagecnt_bias--; + nc->remaining -= fragsz; +} + +static inline void page_frag_alloc_commit_noref(struct page_frag_cache *nc, + unsigned int fragsz) +{ + VM_BUG_ON(fragsz > nc->remaining); + nc->remaining -= fragsz; +} + +static inline void page_frag_alloc_abort(struct page_frag_cache *nc, + unsigned int fragsz) +{ + nc->pagecnt_bias++; + nc->remaining += fragsz; +} + void page_frag_free_va(void *addr); #endif diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 525b577b03a9..9f86aa15bbeb 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -94,6 +94,120 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, return page; } +void *page_frag_alloc_va_prepare(struct page_frag_cache *nc, + unsigned int *fragsz, gfp_t gfp) +{ + struct encoded_va *encoded_va; + unsigned int remaining; + + remaining = nc->remaining; + if (unlikely(*fragsz > remaining)) { + if (unlikely(!__page_frag_cache_refill(nc, gfp) || + *fragsz > PAGE_SIZE)) + return NULL; + + remaining = nc->remaining; + } + + encoded_va = nc->encoded_va; + *fragsz = remaining; + return encoded_page_address(encoded_va) + + page_frag_cache_page_size(encoded_va) - remaining; +} +EXPORT_SYMBOL(page_frag_alloc_va_prepare); + +struct page *page_frag_alloc_pg_prepare(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *fragsz, gfp_t gfp) +{ + struct encoded_va *encoded_va; + unsigned int remaining; + struct page *page; + + remaining = nc->remaining; + if (unlikely(*fragsz > remaining)) { + if (unlikely(*fragsz > PAGE_SIZE)) { + *fragsz = 0; + return NULL; + } + + page = __page_frag_cache_refill(nc, gfp); + remaining = nc->remaining; + encoded_va = nc->encoded_va; + } else { + encoded_va = nc->encoded_va; + page = virt_to_page(encoded_va); + } + + *offset = page_frag_cache_page_size(encoded_va) - remaining; + *fragsz = remaining; + + return page; +} +EXPORT_SYMBOL(page_frag_alloc_pg_prepare); + +struct page *page_frag_alloc_prepare(struct page_frag_cache *nc, + unsigned int *offset, + unsigned int *fragsz, + void **va, gfp_t gfp) +{ + struct encoded_va *encoded_va; + unsigned int remaining; + struct page *page; + + remaining = nc->remaining; + if (unlikely(*fragsz > remaining)) { + if (unlikely(*fragsz > PAGE_SIZE)) { + *fragsz = 0; + return NULL; + } + + page = __page_frag_cache_refill(nc, gfp); + remaining = nc->remaining; + encoded_va = nc->encoded_va; + } else { + encoded_va = nc->encoded_va; + page = virt_to_page(encoded_va); + } + + *offset = page_frag_cache_page_size(encoded_va) - remaining; + *fragsz = remaining; + *va = encoded_page_address(encoded_va) + *offset; + + return page; +} +EXPORT_SYMBOL(page_frag_alloc_prepare); + +struct page *page_frag_alloc_pg(struct page_frag_cache *nc, + unsigned int *offset, unsigned int fragsz, + gfp_t gfp) +{ + struct page *page; + + if (unlikely(fragsz > nc->remaining)) { + if (unlikely(fragsz > PAGE_SIZE)) + return NULL; + + page = __page_frag_cache_refill(nc, gfp); + if (unlikely(!page)) + return NULL; + + *offset = 0; + } else { + struct encoded_va *encoded_va = nc->encoded_va; + + page = virt_to_page(encoded_va); + *offset = page_frag_cache_page_size(encoded_va) - + nc->remaining; + } + + nc->remaining -= fragsz; + nc->pagecnt_bias--; + + return page; +} +EXPORT_SYMBOL(page_frag_alloc_pg); + void page_frag_cache_drain(struct page_frag_cache *nc) { if (!nc->encoded_va) From patchwork Fri Jun 7 12:38:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13689836 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 26490C27C5F for ; Fri, 7 Jun 2024 12:41:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B0D1E6B00B7; Fri, 7 Jun 2024 08:41:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ABD196B00B8; Fri, 7 Jun 2024 08:41:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 95E076B00B9; Fri, 7 Jun 2024 08:41:57 -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 72CF16B00B7 for ; Fri, 7 Jun 2024 08:41:57 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 30FA0401AC for ; Fri, 7 Jun 2024 12:41:57 +0000 (UTC) X-FDA: 82204054674.19.EEC1032 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf07.hostedemail.com (Postfix) with ESMTP id B2B014000D for ; Fri, 7 Jun 2024 12:41:54 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf07.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=1717764115; 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=qfMxGVbnGxX2ZllnMDW5azdtVkEHPUA0+PjyFLkPh2g=; b=LTSS8CO3vCJwgRwM6U3hS2ftpMowzXipyBiv4Huw7bwaKwP/44mwozlJev14IBWqbBkKE1 gdNdpS3dkNy+V7OugqZSwuLp62WfWC6O2Pf1rB6op/RyryZiIZjVGBhxLXMlsKIKEpPa9B izO4ZOlIfa+SfPj9wL0pXe3oRKVDE7c= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717764115; a=rsa-sha256; cv=none; b=OL7EY99hL7mYvYv/xA8/D/M6yVPsLEKPLmz81/yA9F2WnoCUeUyUP9W0hHczZ4e5MGpaa0 yvA4MkGnCoOHx10g+nuPbAZe4fhFNFwPwQKNO/Au/oEcpNX58DwhRt+gHEF3NhvtIRIguY mgGtmIBMLFydTo46qHnIXht6qA96gdA= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf07.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Vwghd0MQHzsTZf; Fri, 7 Jun 2024 20:37:53 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 1B09F14011A; Fri, 7 Jun 2024 20:41:52 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 7 Jun 2024 20:41:51 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Jonathan Corbet , Andrew Morton , , Subject: [PATCH net-next v7 14/15] mm: page_frag: update documentation for page_frag Date: Fri, 7 Jun 2024 20:38:17 +0800 Message-ID: <20240607123819.40694-15-linyunsheng@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240607123819.40694-1-linyunsheng@huawei.com> References: <20240607123819.40694-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemf200006.china.huawei.com (7.185.36.61) X-Stat-Signature: x5m6uufxpctdhe1snfz5sry8qnk8t9nb X-Rspamd-Queue-Id: B2B014000D X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1717764114-344570 X-HE-Meta: U2FsdGVkX18FyQRMJnfZtANJqubMNn6Ms9CIfaIwQGcn+3J8uNzvcNUZpr97gJfDd7GLtU4ITSNQUu6UrTmDXIfHSrwn2VxtxLj8yz2QHmHrDtZt49xYJo2lqTWldAfZwdaQBAFy3FS/EQI2eaH3jy4/KMABG4SXafs8uIGEKL+v0isFq+f9lqrgXXKStz6pFXhyKgrTIVLVaBup8WmY7fi37Fkdk/gCeHmArv9GjutIKOsA2OtZE+rto9tpMgSC58TvfNCBi0BKcytT/gDXuxOsPS/6K3assXkHFHWBU6HJHtAZwpx/QPF16NynkP9ZWWFcp/W1JEiTrC07tGlvnJAmBHYtt1WNVLrZEBJ0u8wIAf1sFpoPHO6cQUM83eaTztEsMGlWnAjdMW2kh/dANXPl3guTVb2n3QbMvu256eJqCUWO+WZ5WLJ8ZCxxJ2YktZYmGv6weWRy6DAoIdiFpl22KKIb3pRzhzDcDpAICstcIIwBnLXgKeipZvZgFJz5D+ktLAwDfLq1GnmiVZqGVYo/QgqR2vynM3GDZQrHvIZuj07AxZe0Ug7T3zR7sQwNC72kMCxgjJIVyp/kLG0qpwuNyRof3qo0aA1bbQE9c/6Ctf0vsf1gf7+Mcqy+EjlqnWVyVjYdd/Waghkh+E5evQbArpeSsKvH+PxWUtsslBBX1YA4WSK2cCzWh1piT8shOySAd4KQeqZj7mV4aVGhKCsWjUrWsVbJCJDwi9rY2NaVXIzX11jdLZM3Gvraqv1eEZ6nqTVKBjV6fhnJ/zyEQSRXkuYrBQ969hsrxqcXMVqNdUFmIkiN6TvMzA3dIhn8Wsvqv/8Pi88+HVvImvtUtfS8jXHAV2dC82RmrNT2aReWS0SI4dKLyylQ6n7VS1EqpKAoPjtGCPXs/F3r8nPrYSXtvm207P0TAURRR7iE6NsPnWaTGaq1ZWWZINVIp8tXwxb9fdrtsfpqbOMwYnk Z6tQq3W6 NA7QaCmjKZsANhigz24p9M7wUiz7xG/ituxHT7orX2LAUu0PpeTCk5erO8wcaRuObAzOJOEAtxBUXT/b7NmLCBb2RsBYgEwA5r7tor4xxDQ+XKVLN5uTZCY5b1PZF/bynr6/vF+noyf2u2NIzlGasGMWl7uRVDrCdZJysso4HUqEkVr97pDhUVchtslmy+Nw0AQ42DLHIa8viADaVIylk+vFU/u2+optR3gl+DdIQcmM8pKyFQu6RXPPGUme0RnkrnhW+pnLmY7WzKgoJWDAI0Z2xAP6F7+iQzjaD X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Update documentation about design, implementation and API usages for page_frag. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- Documentation/mm/page_frags.rst | 163 +++++++++++++++++++++++++++++++- include/linux/page_frag_cache.h | 107 +++++++++++++++++++++ mm/page_frag_cache.c | 77 ++++++++++++++- 3 files changed, 344 insertions(+), 3 deletions(-) diff --git a/Documentation/mm/page_frags.rst b/Documentation/mm/page_frags.rst index 503ca6cdb804..6a4ac2616098 100644 --- a/Documentation/mm/page_frags.rst +++ b/Documentation/mm/page_frags.rst @@ -1,3 +1,5 @@ +.. SPDX-License-Identifier: GPL-2.0 + ============== Page fragments ============== @@ -40,4 +42,163 @@ page via a single call. The advantage to doing this is that it allows for cleaning up the multiple references that were added to a page in order to avoid calling get_page per allocation. -Alexander Duyck, Nov 29, 2016. + +Architecture overview +===================== + +.. code-block:: none + + +----------------------+ + | page_frag API caller | + +----------------------+ + | + | + v + +---------------------------------------------------------------+ + | request page fragment | + +---------------------------------------------------------------+ + | | | + | | | + | Cache not enough | + | | | + | v | + Cache empty +-----------------+ | + | | drain old cache | | + | +-----------------+ | + | | | + v_________________________________v | + | | + | | + _________________v_______________ | + | | Cache is enough + | | | + PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE | | + | | | + | PAGE_SIZE >= PAGE_FRAG_CACHE_MAX_SIZE | + v | | + +----------------------------------+ | | + | refill cache with order > 0 page | | | + +----------------------------------+ | | + | | | | + | | | | + | Refill failed | | + | | | | + | v v | + | +------------------------------------+ | + | | refill cache with order 0 page | | + | +----------------------------------=-+ | + | | | + Refill succeed | | + | Refill succeed | + | | | + v v v + +---------------------------------------------------------------+ + | allocate fragment from cache | + +---------------------------------------------------------------+ + +API interface +============= +As the design and implementation of page_frag API implies, the allocation side +does not allow concurrent calling. Instead it is assumed that the caller must +ensure there is not concurrent alloc calling to the same page_frag_cache +instance by using its own lock or rely on some lockless guarantee like NAPI +softirq. + +Depending on different aligning requirement, the page_frag API caller may call +page_frag_alloc*_align*() to ensure the returned virtual address or offset of +the page is aligned according to the 'align/alignment' parameter. Note the size +of the allocated fragment is not aligned, the caller needs to provide an aligned +fragsz if there is an alignment requirement for the size of the fragment. + +Depending on different use cases, callers expecting to deal with va, page or +both va and page for them may call page_frag_alloc_va*, page_frag_alloc_pg*, +or page_frag_alloc* API accordingly. + +There is also a use case that needs minimum memory in order for forward progress, +but more performant if more memory is available. Using page_frag_alloc_prepare() +and page_frag_alloc_commit() related API, the caller requests the minimum memory +it needs and the prepare API will return the maximum size of the fragment +returned. The caller needs to either call the commit API to report how much +memory it actually uses, or not do so if deciding to not use any memory. + +.. kernel-doc:: include/linux/page_frag_cache.h + :identifiers: page_frag_cache_init page_frag_cache_is_pfmemalloc + page_frag_cache_page_offset page_frag_alloc_va + page_frag_alloc_va_align page_frag_alloc_va_prepare_align + page_frag_alloc_probe page_frag_alloc_commit + page_frag_alloc_commit_noref page_frag_alloc_abort + +.. kernel-doc:: mm/page_frag_cache.c + :identifiers: __page_frag_alloc_va_align page_frag_alloc_pg + page_frag_alloc_va_prepare page_frag_alloc_pg_prepare + page_frag_alloc_prepare page_frag_cache_drain + page_frag_free_va + +Coding examples +=============== + +Init & Drain API +---------------- + +.. code-block:: c + + page_frag_cache_init(pfrag); + ... + page_frag_cache_drain(pfrag); + + +Alloc & Free API +---------------- + +.. code-block:: c + + void *va; + + va = page_frag_alloc_va_align(pfrag, size, gfp, align); + if (!va) + goto do_error; + + err = do_something(va, size); + if (err) { + page_frag_free_va(va); + goto do_error; + } + +Prepare & Commit API +-------------------- + +.. code-block:: c + + unsigned int offset, size; + bool merge = true; + struct page *page; + void *va; + + size = 32U; + page = page_frag_alloc_prepare(pfrag, &offset, &size, &va); + if (!page) + goto wait_for_space; + + copy = min_t(unsigned int, copy, size); + if (!skb_can_coalesce(skb, i, page, offset)) { + if (i >= max_skb_frags) + goto new_segment; + + merge = false; + } + + copy = mem_schedule(copy); + if (!copy) + goto wait_for_space; + + err = copy_from_iter_full_nocache(va, copy, iter); + if (err) + goto do_error; + + if (merge) { + skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); + page_frag_alloc_commit_noref(pfrag, offset, copy); + } else { + skb_fill_page_desc(skb, i, page, offset, copy); + page_frag_alloc_commit(pfrag, offset, copy); + } diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index e95d44a36ec9..234b9b1c6f63 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -71,11 +71,28 @@ struct page_frag_cache { #endif }; +/** + * page_frag_cache_init() - Init page_frag cache. + * @nc: page_frag cache from which to init + * + * Inline helper to init the page_frag cache. + */ static inline void page_frag_cache_init(struct page_frag_cache *nc) { memset(nc, 0, sizeof(*nc)); } +/** + * page_frag_cache_is_pfmemalloc() - Check for pfmemalloc. + * @nc: page_frag cache from which to check + * + * Used to check if the current page in page_frag cache is pfmemalloc'ed. + * It has the same calling context expection as the alloc API. + * + * Return: + * true if the current page in page_frag cache is pfmemalloc'ed, otherwise + * return false. + */ static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc) { return encoded_page_pfmemalloc(nc->encoded_va); @@ -95,6 +112,19 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask); +/** + * page_frag_alloc_va_align() - Alloc a page fragment with aligning requirement. + * @nc: page_frag cache from which to allocate + * @fragsz: the requested fragment size + * @gfp_mask: the allocation gfp to use when cache needs to be refilled + * @align: the requested aligning requirement for virtual address of fragment + * + * WARN_ON_ONCE() checking for @align before allocing a page fragment from + * page_frag cache with aligning requirement. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ static inline void *page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align) @@ -103,11 +133,32 @@ static inline void *page_frag_alloc_va_align(struct page_frag_cache *nc, return __page_frag_alloc_va_align(nc, fragsz, gfp_mask, -align); } +/** + * page_frag_cache_page_offset() - Return the current page fragment's offset. + * @nc: page_frag cache from which to check + * + * The API is only used in net/sched/em_meta.c for historical reason, do not use + * it for new caller unless there is a strong reason. + * + * Return: + * the offset of the current page fragment in the page_frag cache. + */ static inline unsigned int page_frag_cache_page_offset(const struct page_frag_cache *nc) { return page_frag_cache_page_size(nc->encoded_va) - nc->remaining; } +/** + * page_frag_alloc_va() - Alloc a page fragment. + * @nc: page_frag cache from which to allocate + * @fragsz: the requested fragment size + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * + * Get a page fragment from page_frag cache. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ static inline void *page_frag_alloc_va(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask) { @@ -117,6 +168,21 @@ static inline void *page_frag_alloc_va(struct page_frag_cache *nc, void *page_frag_alloc_va_prepare(struct page_frag_cache *nc, unsigned int *fragsz, gfp_t gfp); +/** + * page_frag_alloc_va_prepare_align() - Prepare allocing a page fragment with + * aligning requirement. + * @nc: page_frag cache from which to prepare + * @fragsz: in as the requested size, out as the available size + * @gfp: the allocation gfp to use when cache need to be refilled + * @align: the requested aligning requirement + * + * WARN_ON_ONCE() checking for @align before preparing an aligned page fragment + * with minimum size of @fragsz, @fragsz is also used to report the maximum size + * of the page fragment the caller can use. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ static inline void *page_frag_alloc_va_prepare_align(struct page_frag_cache *nc, unsigned int *fragsz, gfp_t gfp, @@ -151,6 +217,21 @@ static inline struct encoded_va *__page_frag_alloc_probe(struct page_frag_cache return encoded_va; } +/** + * page_frag_alloc_probe - Probe the available page fragment. + * @nc: page_frag cache from which to probe + * @offset: out as the offset of the page fragment + * @fragsz: in as the requested size, out as the available size + * @va: out as the virtual address of the returned page fragment + * + * Probe the current available memory to caller without doing cache refilling. + * If no space is available in the page_frag cache, return NULL. + * If the requested space is available, up to @fragsz bytes may be added to the + * fragment using commit API. + * + * Return: + * the page fragment, otherwise return NULL. + */ #define page_frag_alloc_probe(nc, offset, fragsz, va) \ ({ \ struct page *__page = NULL; \ @@ -165,6 +246,14 @@ static inline struct encoded_va *__page_frag_alloc_probe(struct page_frag_cache __page; \ }) +/** + * page_frag_alloc_commit - Commit allocing a page fragment. + * @nc: page_frag cache from which to commit + * @fragsz: size of the page fragment has been used + * + * Commit the actual used size for the allocation that was either prepared or + * probed. + */ static inline void page_frag_alloc_commit(struct page_frag_cache *nc, unsigned int fragsz) { @@ -173,6 +262,16 @@ static inline void page_frag_alloc_commit(struct page_frag_cache *nc, nc->remaining -= fragsz; } +/** + * page_frag_alloc_commit_noref - Commit allocing a page fragment without taking + * page refcount. + * @nc: page_frag cache from which to commit + * @fragsz: size of the page fragment has been used + * + * Commit the alloc preparing or probing by passing the actual used size, but + * not taking refcount. Mostly used for fragmemt coalescing case when the + * current fragment can share the same refcount with previous fragment. + */ static inline void page_frag_alloc_commit_noref(struct page_frag_cache *nc, unsigned int fragsz) { @@ -180,6 +279,14 @@ static inline void page_frag_alloc_commit_noref(struct page_frag_cache *nc, nc->remaining -= fragsz; } +/** + * page_frag_alloc_abort - Abort the page fragment allocation. + * @nc: page_frag cache to which the page fragment is aborted back + * @fragsz: size of the page fragment to be aborted + * + * It is expected to be called from the same context as the alloc API. + * Mostly used for error handling cases where the fragment is no longer needed. + */ static inline void page_frag_alloc_abort(struct page_frag_cache *nc, unsigned int fragsz) { diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 9f86aa15bbeb..e76f035e63de 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -94,6 +94,18 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, return page; } +/** + * page_frag_alloc_va_prepare() - Prepare allocing a page fragment. + * @nc: page_frag cache from which to prepare + * @fragsz: in as the requested size, out as the available size + * @gfp: the allocation gfp to use when cache needs to be refilled + * + * Prepare a page fragment with minimum size of @fragsz, @fragsz is also used + * to report the maximum size of the page fragment the caller can use. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ void *page_frag_alloc_va_prepare(struct page_frag_cache *nc, unsigned int *fragsz, gfp_t gfp) { @@ -116,6 +128,19 @@ void *page_frag_alloc_va_prepare(struct page_frag_cache *nc, } EXPORT_SYMBOL(page_frag_alloc_va_prepare); +/** + * page_frag_alloc_pg_prepare - Prepare allocing a page fragment. + * @nc: page_frag cache from which to prepare + * @offset: out as the offset of the page fragment + * @fragsz: in as the requested size, out as the available size + * @gfp: the allocation gfp to use when cache needs to be refilled + * + * Prepare a page fragment with minimum size of @fragsz, @fragsz is also used + * to report the maximum size of the page fragment the caller can use. + * + * Return: + * the page fragment, otherwise return NULL. + */ struct page *page_frag_alloc_pg_prepare(struct page_frag_cache *nc, unsigned int *offset, unsigned int *fragsz, gfp_t gfp) @@ -146,6 +171,21 @@ struct page *page_frag_alloc_pg_prepare(struct page_frag_cache *nc, } EXPORT_SYMBOL(page_frag_alloc_pg_prepare); +/** + * page_frag_alloc_prepare - Prepare allocing a page fragment. + * @nc: page_frag cache from which to prepare + * @offset: out as the offset of the page fragment + * @fragsz: in as the requested size, out as the available size + * @va: out as the virtual address of the returned page fragment + * @gfp: the allocation gfp to use when cache needs to be refilled + * + * Prepare a page fragment with minimum size of @fragsz, @fragsz is also used + * to report the maximum size of the page fragment. Return both 'struct page' + * and virtual address of the fragment to the caller. + * + * Return: + * the page fragment, otherwise return NULL. + */ struct page *page_frag_alloc_prepare(struct page_frag_cache *nc, unsigned int *offset, unsigned int *fragsz, @@ -178,6 +218,18 @@ struct page *page_frag_alloc_prepare(struct page_frag_cache *nc, } EXPORT_SYMBOL(page_frag_alloc_prepare); +/** + * page_frag_alloc_pg - Alloce a page fragment. + * @nc: page_frag cache from which to alloce + * @offset: out as the offset of the page fragment + * @fragsz: the requested fragment size + * @gfp: the allocation gfp to use when cache needs to be refilled + * + * Get a page fragment from page_frag cache. + * + * Return: + * the page fragment, otherwise return NULL. + */ struct page *page_frag_alloc_pg(struct page_frag_cache *nc, unsigned int *offset, unsigned int fragsz, gfp_t gfp) @@ -208,6 +260,10 @@ struct page *page_frag_alloc_pg(struct page_frag_cache *nc, } EXPORT_SYMBOL(page_frag_alloc_pg); +/** + * page_frag_cache_drain - Drain the current page from page_frag cache. + * @nc: page_frag cache from which to drain + */ void page_frag_cache_drain(struct page_frag_cache *nc) { if (!nc->encoded_va) @@ -228,6 +284,19 @@ void __page_frag_cache_drain(struct page *page, unsigned int count) } EXPORT_SYMBOL(__page_frag_cache_drain); +/** + * __page_frag_alloc_va_align() - Alloc a page fragment with aligning + * requirement. + * @nc: page_frag cache from which to allocate + * @fragsz: the requested fragment size + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * @align_mask: the requested aligning requirement for the 'va' + * + * Get a page fragment from page_frag cache with aligning requirement. + * + * Return: + * Return va of the page fragment, otherwise return NULL. + */ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) @@ -263,8 +332,12 @@ void *__page_frag_alloc_va_align(struct page_frag_cache *nc, } EXPORT_SYMBOL(__page_frag_alloc_va_align); -/* - * Frees a page fragment allocated out of either a compound or order 0 page. +/** + * page_frag_free_va - Free a page fragment. + * @addr: va of page fragment to be freed + * + * Free a page fragment allocated out of either a compound or order 0 page by + * virtual address. */ void page_frag_free_va(void *addr) {