From patchwork Mon Aug 26 12:40: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: 13777857 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 4F141C5321E for ; Mon, 26 Aug 2024 12:46:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BF94B6B04B5; Mon, 26 Aug 2024 08:46:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B86BD6B04B2; Mon, 26 Aug 2024 08:46:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9F96A6B04B5; Mon, 26 Aug 2024 08:46:18 -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 7E3746B04A9 for ; Mon, 26 Aug 2024 08:46:18 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 3760A41103 for ; Mon, 26 Aug 2024 12:46:18 +0000 (UTC) X-FDA: 82494369636.24.9825765 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf03.hostedemail.com (Postfix) with ESMTP id 111C820025 for ; Mon, 26 Aug 2024 12:46:14 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; spf=pass (imf03.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724676334; 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=EC9+E4rKYBva1XZZL/Kcl5lVQf71apC+HwduGUlt5GU=; b=F14Bc4mk2gF3y8oxECoczhoJ3XPDJV3nKr++BalABiClsRme7GrYAwNNXsRN+m3Mdpbcy9 eAf1YO0D9tjakbpSp7z9QVb2X5QYot/KyzQalL5HUactGKD6Cw6rK19xHvl5exE36g27cY 8TJdzP7vx+Q9FWXM6tUPefJryicoQUI= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; spf=pass (imf03.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724676334; a=rsa-sha256; cv=none; b=JZK2Cwc4O01A4gG6H/F+qqDfXKUrNf0vCkUs8VpPKDdhL4sUiGBzUXy3bmdPhf84BVHQg5 k9R9FbNU+c5v0XeNgeVMwmWfxyxGhRqU2DldhTGkpY15ZDud7wjlE0m1nOWOPRYKkvvcl9 oU+vRX5q3aa3c3cNyRs1Hkmbnq7YN5o= Received: from mail.maildlp.com (unknown [172.19.88.105]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Wsr3J4H3vzpTgQ; Mon, 26 Aug 2024 20:44:28 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id ADD1A14037E; Mon, 26 Aug 2024 20:46:07 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 26 Aug 2024 20:46:07 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Shuah Khan , , Subject: [PATCH net-next v15 01/13] mm: page_frag: add a test module for page_frag Date: Mon, 26 Aug 2024 20:40:08 +0800 Message-ID: <20240826124021.2635705-2-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240826124021.2635705-1-linyunsheng@huawei.com> References: <20240826124021.2635705-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 111C820025 X-Stat-Signature: 6nr58p5jc8pru83g3f8rgrnwycixygpu X-HE-Tag: 1724676374-982808 X-HE-Meta: U2FsdGVkX18ni0I2F/Tep0IISzWdRddkFlidN4ByXjgulqSDFi3EKDoaqkV849kkE1h+sEHD7nUN6vv8vH25GPhC+N7OaY+yQ6Cy+TXd4OpvVHyB8Bm62CsHUPBRe8dwde+C/PBtvrrkCV30qv0ioUUYszzghgQ0X8dzO9BLcYJjwts22/LKg4yfFMLj5G4MjlJ3kggxEOkqD65tvB3xbYke4JqLefLMvy8pLfulJs49CBUvdZhwfaMF52W+Cr9sd/Q5bLuUM0aNs/2RLJOJHuHRrzZLasNa1ElaoAb4oZgyd5veWI2QFIZ6bi73r1sqHMGa18kS4v7QBnXSt9pLy4v6se0di5PWEom/z1qrq+hKof+IWiY5UJNpz61/ilnK9xmPPFrpTTJOSRQwNjnDJvy2+I9APRn8XnecfTLznFXBTXlCqUKD6cxGZqNfv3L1yF9wscIhyG2UwriC4KrQEMUneKxBXasXTqH+r2/UfZo6wY3onhLVb5r4LfU/hjjPT/3hlimFo0jy1pO04I0QV1RKFW3+4onC0j7JDgDrhtL1CkyINOFbPObK6YEioCgwqUbAi/qaXIQDaDoBlrSUtDfvIEsHnPfwIo0dG4jLhuXq7nb+a26KW6p5KVTf5HfcSEzRqNNmH4DDzf9Kv09WBY/UmWTrJpgtEa2//LOkmQXS6UI7QKDqXe3bqE9B1mTWqbeuXa2VfbICuA5D1yNo3EZXNErnB5eBGhy2ba1nhQeicT2JHoalYFvQS7AXvrzDNDHXX6ear8G5is1VkMjqnMqz/pjQLnH5xazlu+N8JWkrPJ3wPPshdEIS9SBv7S9u8hMtujAB/FnPHqU/0MPrlpXrPs/5XMneyy7vjvKSxQm56RVFqPKgIzDWh7GgElhgLBgOz+RuS21+ZLu/JW+aD98H8ylA+fLeNGMF2JYVKlSHleeZ3ZK8h3bZTNYSo8ow9t29j/DkBL8SVqzGkm+ SuZMUpvB zvOVhxlIcxYSvGwYn1R178L7baYds+8rdfLnImc+/YFKi90zxOTGZ1BPDc1FHx7fiPrpvQyACu+kfuFqB/WM0eVaG3DxHS5oO1XhE7lxL4PnckVNI44t+4kDs8sYC2MwXGZGrA3/gHFxu/LquMGcEVO4zpKoitPzV4b6S689RUiUHNf8WMpQK7DrIpIR88JQMdgQY51Vhs032NL+jWOCLj4B1TwHFPgEKtgiIxvLEXda/Xq9rZAInqUcjE83hgg31w0c4gEQJqObQbJUZ66QxSyDIbOamGljZwxzl 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: The testing is done by ensuring that the fragment allocated from a frag_frag_cache instance is pushed into a ptr_ring instance in a kthread binded to a specified cpu, and a kthread binded to a specified cpu will pop the fragment from the ptr_ring and free the fragment. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- tools/testing/selftests/mm/Makefile | 2 + tools/testing/selftests/mm/page_frag/Makefile | 18 ++ .../selftests/mm/page_frag/page_frag_test.c | 170 ++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 9 +- 4 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/mm/page_frag/Makefile create mode 100644 tools/testing/selftests/mm/page_frag/page_frag_test.c diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile index cfad627e8d94..ed196901b9ca 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -36,6 +36,8 @@ MAKEFLAGS += --no-builtin-rules CFLAGS = -Wall -I $(top_srcdir) $(EXTRA_CFLAGS) $(KHDR_INCLUDES) $(TOOLS_INCLUDES) LDLIBS = -lrt -lpthread -lm +TEST_GEN_MODS_DIR := page_frag + TEST_GEN_FILES = cow TEST_GEN_FILES += compaction_test TEST_GEN_FILES += gup_longterm diff --git a/tools/testing/selftests/mm/page_frag/Makefile b/tools/testing/selftests/mm/page_frag/Makefile new file mode 100644 index 000000000000..58dda74d50a3 --- /dev/null +++ b/tools/testing/selftests/mm/page_frag/Makefile @@ -0,0 +1,18 @@ +PAGE_FRAG_TEST_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))) +KDIR ?= $(abspath $(PAGE_FRAG_TEST_DIR)/../../../../..) + +ifeq ($(V),1) +Q = +else +Q = @ +endif + +MODULES = page_frag_test.ko + +obj-m += page_frag_test.o + +all: + +$(Q)make -C $(KDIR) M=$(PAGE_FRAG_TEST_DIR) modules + +clean: + +$(Q)make -C $(KDIR) M=$(PAGE_FRAG_TEST_DIR) clean diff --git a/tools/testing/selftests/mm/page_frag/page_frag_test.c b/tools/testing/selftests/mm/page_frag/page_frag_test.c new file mode 100644 index 000000000000..0e803db1ad79 --- /dev/null +++ b/tools/testing/selftests/mm/page_frag/page_frag_test.c @@ -0,0 +1,170 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Test module for page_frag cache + * + * Copyright: linyunsheng@huawei.com + */ + +#include +#include +#include +#include +#include +#include + +static struct ptr_ring ptr_ring; +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, 0); +MODULE_PARM_DESC(nr_test, "number of iterations to test"); + +static bool test_align; +module_param(test_align, bool, 0); +MODULE_PARM_DESC(test_align, "use align API for testing"); + +static int test_alloc_len = 2048; +module_param(test_alloc_len, int, 0); +MODULE_PARM_DESC(test_alloc_len, "alloc len for testing"); + +static int test_push_cpu; +module_param(test_push_cpu, int, 0); +MODULE_PARM_DESC(test_push_cpu, "test cpu for pushing fragment"); + +static int test_pop_cpu; +module_param(test_pop_cpu, int, 0); +MODULE_PARM_DESC(test_pop_cpu, "test cpu for popping fragment"); + +static int page_frag_pop_thread(void *arg) +{ + struct ptr_ring *ring = 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 = __ptr_ring_consume(ring); + + 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 ptr_ring *ring = 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); + + WARN_ONCE((unsigned long)va & (SMP_CACHE_BYTES - 1), + "unaligned va returned\n"); + } else { + va = page_frag_alloc(&test_frag, test_alloc_len, GFP_KERNEL); + } + + if (!va) + continue; + + ret = __ptr_ring_produce(ring, va); + 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); + + if (test_alloc_len > PAGE_SIZE || test_alloc_len <= 0 || + !cpu_active(test_push_cpu) || !cpu_active(test_pop_cpu)) + return -EINVAL; + + ret = ptr_ring_init(&ptr_ring, nr_objs, GFP_KERNEL); + if (ret) + return ret; + + tsk_push = kthread_create_on_cpu(page_frag_push_thread, &ptr_ring, + 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_ring, + 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); + + ptr_ring_cleanup(&ptr_ring, NULL); + 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"); diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/selftests/mm/run_vmtests.sh index 36045edb10de..9a788d5f3f28 100755 --- a/tools/testing/selftests/mm/run_vmtests.sh +++ b/tools/testing/selftests/mm/run_vmtests.sh @@ -75,6 +75,8 @@ separated by spaces: read-only VMAs - mdwe test prctl(PR_SET_MDWE, ...) +- page_frag + test handling of page fragment allocation and freeing example: ./run_vmtests.sh -t "hmm mmap ksm" EOF @@ -231,7 +233,8 @@ run_test() { ("$@" 2>&1) | tap_prefix local ret=${PIPESTATUS[0]} count_total=$(( count_total + 1 )) - if [ $ret -eq 0 ]; then + # page_frag_test.ko returns 11(EAGAIN) when insmod'ing to avoid rmmod + if [ $ret -eq 0 ] | [ $ret -eq 11 -a ${CATEGORY} == "page_frag" ]; then count_pass=$(( count_pass + 1 )) echo "[PASS]" | tap_prefix echo "ok ${count_total} ${test}" | tap_output @@ -456,6 +459,10 @@ CATEGORY="mkdirty" run_test ./mkdirty CATEGORY="mdwe" run_test ./mdwe_test +CATEGORY="page_frag" run_test insmod ./page_frag/page_frag_test.ko + +CATEGORY="page_frag" run_test insmod ./page_frag/page_frag_test.ko test_alloc_len=12 test_align=1 + echo "SUMMARY: PASS=${count_pass} SKIP=${count_skip} FAIL=${count_fail}" | tap_prefix echo "1..${count_total}" | tap_output From patchwork Mon Aug 26 12:40: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: 13777858 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 331BAC5321D for ; Mon, 26 Aug 2024 12:46:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 086546B04A9; Mon, 26 Aug 2024 08:46:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0358F6B04B2; Mon, 26 Aug 2024 08:46:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BF56B6B04A9; Mon, 26 Aug 2024 08:46:18 -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 911B26B04AA for ; Mon, 26 Aug 2024 08:46:18 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 42650A0FAE for ; Mon, 26 Aug 2024 12:46:18 +0000 (UTC) X-FDA: 82494369636.22.60D00A3 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf29.hostedemail.com (Postfix) with ESMTP id 46E15120004 for ; Mon, 26 Aug 2024 12:46:14 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf29.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=1724676292; 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=jmuJuqjtQ4wUkf/ARwWH/mEx2Vk1bYe7Z85hZsbjeIw=; b=KPulQmofquPhlyoi+wtS22cjdQURh1QdgLA3G/LgvDP/LepkXIEg4YPUmIPVElEwbPcs8d B2dCBzcVLaXr4CPeYOM2d65Be9zWzEm+pVA8SedxvwAYNAZmuN9sIksfHIrcwAlSxcUDYo QiCBlQb/2f875+TIHnzoKwNGtY+WRE8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724676292; a=rsa-sha256; cv=none; b=S/PbAe2keaoOy35Eoji6sQUA0KLFqy2Bb0jVJd4qtgHF49kKlT92cWPL87uM06NQKQ4YMp 6UN5p0C+r6KRNYPlGkns6PwkJbyVv4rjfQN990/uvuV3Biz8xPTQmUcX0io8Ivod8unwcm du9bTJh0xCgItPwLnj6aGSMYShlxEoo= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf29.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com Received: from mail.maildlp.com (unknown [172.19.163.44]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4Wsr1T5GWkz1HHR3; Mon, 26 Aug 2024 20:42:53 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 5F3D61402E1; Mon, 26 Aug 2024 20:46:11 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 26 Aug 2024 20:46:11 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , David Howells , Alexander Duyck , Andrew Morton , Eric Dumazet , Shuah Khan , , Subject: [PATCH net-next v15 02/13] mm: move the page fragment allocator from page_alloc into its own file Date: Mon, 26 Aug 2024 20:40:09 +0800 Message-ID: <20240826124021.2635705-3-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240826124021.2635705-1-linyunsheng@huawei.com> References: <20240826124021.2635705-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Queue-Id: 46E15120004 X-Stat-Signature: xwxkq6yx6kwj8cqi1wwbs34rowcmr4gx X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1724676374-537315 X-HE-Meta: U2FsdGVkX1+8PHZ5M9obwVHWrtYe/4unRhFEcIShv+mazDY6u3wfD7v3TyccyRUhKH39uDkg0ars48fU0otrh+4IHwr0WG2rCQust9lwjtmSyq01PC9QK2UF7ZsfxeIaHRAt/IpTk7Id8krQvmP+ZwEP+hWcpqnAzywX+nukUxITUMDDwzegEyoGStDg2GTla1hUxdjbA/JK90nUqiTavDN+cCeIkokT5ozPn1ZULtaQ+idnmEN62TpGOHmk+a9nZ0AdZENtDyBS9tyZWsZ+nw3zRtxdl4dof/PeWPzVcoVO1cuyx8Ro4VOK6k6Xc1VP9ftAZmdkYGNEFP3QhfDoMAWEOQyhWzAjJNGWu3iaj4aVsi4YBf5lRgxQ2PkSvnNqvKnw5ZUDfbfUZpPE+YdU8QHRaKAa3tHISmfYPu0aB1godlARjdIg2vHwDtNa2uB5+18zeOHbhIkQRwBkulcim34qpM1G/pBZYzn6D80eCF7XD+wP9uFBT0GQz+nWQ+nXZ7ubA+E0ODgOpZMJ/+qgBTsBrVPv81MXaot7uW6Zl+HA0b3KRO3+DA/h103AKNc1QD6HaYosRD/dsFYMHzczdpgZCvzWpi4OXESo/AS21I85MyybTj1+zgDGVNqGqEEpwUrWscppbAwxyK/lXnsmQje/wBFZG78HQ3vHonEpbUkoWNyxBlA+QI5Zx6EpVRhFiNNRAVOdIQ6H5Cy79qa273rVCqy69z7MDHjYDeE14xjJQ5pWLiRN+yomEvZ5RdrHLowPq+tEIP79mVxzlbI1riBRXSYerz0j2Qzm01lJarGOyihOYv9mgZ3ldtH5elEUkeT+xE4+V4d4gYIAY+urThrLu1/RlDY/yangsiGEzNFAbtB5vIRqJM8rCOT5gCXv0XOO1m3ZIsE/8VU83LdI+eWuFOQCa+GfSgczs2HzUsGgU7JeRAFCpJT5YlHpG6M1GrMqiUmjx1wMvy+zy/U 0WNkU94k bEq7p7kFsb3RyTOBlqx+zvs/nfcNEUgiG8PcVmu0DxyH82mYrEK0SUIp7itvIAAAB2zD8pp+Vz8Or757DCS8tOInXJl/LHq2zebI53YSG+4weACZT07tk4auM/qYPnhx6EIsh4b8LfIwR6uqApMHoIacMhuEoXabSstUp9gpBFxU4tTc/1cVbPXhA+CNCvDPG5e7NeKY6VRB/dZydNELlnKRrrLEMGWlxggusqWaf013g9wUjWZoQLWg80Rkjvs/ObEbGZhLWIOmiSfLvoTd4VL/W7c+H9uyjYu4EBvdhRO4buYnWVY8Nrv2HpcXrYaHvVTQ2IQoJNrdlVZFvUk1tgFI3CzO9AncwWoA0Q3YLgrrwZMdYP02L2/8etS/utwzpmiWWbJoMHKYwb1vAZZ9OrTYThdMTfyG71Ef/xzqgxatOTc4= 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 As this patchset is going to replace 'struct page_frag' with 'struct page_frag_cache' in sched.h, including page_frag_cache.h in sched.h has a compiler error caused by interdependence between mm_types.h and mm.h for asm-offsets.c, see [2]. So avoid the compiler error by moving 'struct page_frag_cache' to mm_types_task.h as suggested by Alexander, see [3]. 1. https://lore.kernel.org/all/20230411160902.4134381-3-dhowells@redhat.com/ 2. https://lore.kernel.org/all/15623dac-9358-4597-b3ee-3694a5956920@gmail.com/ 3. https://lore.kernel.org/all/CAKgT0UdH1yD=LSCXFJ=YM_aiA4OomD-2wXykO42bizaWMt_HOA@mail.gmail.com/ CC: David Howells CC: Alexander Duyck Signed-off-by: Yunsheng Lin Acked-by: Andrew Morton --- include/linux/gfp.h | 22 --- include/linux/mm_types.h | 18 --- include/linux/mm_types_task.h | 18 +++ include/linux/page_frag_cache.h | 31 ++++ include/linux/skbuff.h | 1 + mm/Makefile | 1 + mm/page_alloc.c | 136 ---------------- mm/page_frag_cache.c | 145 ++++++++++++++++++ .../selftests/mm/page_frag/page_frag_test.c | 2 +- 9 files changed, 197 insertions(+), 177 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 f53f76e0b17e..01a49be7c98d 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -371,28 +371,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 485424979254..843d75412105 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -521,9 +521,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 @@ -542,21 +539,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/mm_types_task.h b/include/linux/mm_types_task.h index a2f6179b672b..cdc1e3696439 100644 --- a/include/linux/mm_types_task.h +++ b/include/linux/mm_types_task.h @@ -8,6 +8,7 @@ * (These are defined separately to decouple sched.h from mm_types.h as much as possible.) */ +#include #include #include @@ -46,6 +47,23 @@ struct page_frag { #endif }; +#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; +}; + /* Track pages that require TLB flushes */ struct tlbflush_unmap_batch { #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h new file mode 100644 index 000000000000..67ac8626ed9b --- /dev/null +++ b/include/linux/page_frag_cache.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _LINUX_PAGE_FRAG_CACHE_H +#define _LINUX_PAGE_FRAG_CACHE_H + +#include +#include +#include + +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 cf8f6ce06742..7482997c719f 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 d2915f8c9dc0..e9d342fa8058 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -65,6 +65,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 c565de8f48e9..d0e88aa6eb0d 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4798,142 +4798,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) - 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..609a485cd02a --- /dev/null +++ b/mm/page_frag_cache.c @@ -0,0 +1,145 @@ +// 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 +#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) + 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/tools/testing/selftests/mm/page_frag/page_frag_test.c b/tools/testing/selftests/mm/page_frag/page_frag_test.c index 0e803db1ad79..4a009122991e 100644 --- a/tools/testing/selftests/mm/page_frag/page_frag_test.c +++ b/tools/testing/selftests/mm/page_frag/page_frag_test.c @@ -6,12 +6,12 @@ * Copyright: linyunsheng@huawei.com */ -#include #include #include #include #include #include +#include static struct ptr_ring ptr_ring; static int nr_objs = 512; From patchwork Mon Aug 26 12:40: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: 13777859 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 92B92C5321E for ; Mon, 26 Aug 2024 12:46:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 53FFD6B04AA; Mon, 26 Aug 2024 08:46:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4F0646B04B2; Mon, 26 Aug 2024 08:46:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3E1A76B04B6; Mon, 26 Aug 2024 08:46:19 -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 0F4BD6B04AA for ; Mon, 26 Aug 2024 08:46:19 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 84069141079 for ; Mon, 26 Aug 2024 12:46:18 +0000 (UTC) X-FDA: 82494369636.29.9AB1FAF Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf15.hostedemail.com (Postfix) with ESMTP id E7692A000E for ; Mon, 26 Aug 2024 12:46:15 +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.189 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724676307; a=rsa-sha256; cv=none; b=zkKHTGXGmlCmMTeIgfbLroxX1bi8SDtC3rRLiVPP53O7kBKiAUeMgnVF5lZvxweLwIQ9SR 4oF7NvzgroRJ5W5XCmOw4XnplXFMbWx1XGAP2VJ3pq7WbLQ7UMI2RZFQCbvvgdRrG6TYPv mRGTEI9jHPoy6f43go9fVvAHa0M3XW4= 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.189 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724676307; 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=kPIYPG/OrE/t2+FUNllMox/LOqC+P4YoIlou5mKg7a8=; b=xYBHxA0hKCkDRKE3TEOn26TDqrKJkw5f7B3np27Wj4uw80qcgWPQAwlPU+puvDTB/kB0F3 WkRke4km5g/71lAUn6nD69voYJ3KdZ/KTBOuT6VJh6jCUGhZ8AUakORs3FgGuodF8F7+qk CvnVXmV4S4IQ7n9KKKJLAXOnL4Tw9zU= Received: from mail.maildlp.com (unknown [172.19.88.194]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4Wsqzn0vWpzQqhg; Mon, 26 Aug 2024 20:41:25 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id C66221401F0; Mon, 26 Aug 2024 20:46:12 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 26 Aug 2024 20:46:12 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v15 03/13] mm: page_frag: use initial zero offset for page_frag_alloc_align() Date: Mon, 26 Aug 2024 20:40:10 +0800 Message-ID: <20240826124021.2635705-4-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240826124021.2635705-1-linyunsheng@huawei.com> References: <20240826124021.2635705-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: E7692A000E X-Stat-Signature: hthrhqmcxwnor9oh5oujjw1f5xae3fei X-Rspam-User: X-HE-Tag: 1724676375-381431 X-HE-Meta: U2FsdGVkX19KkM5BfsBglo1VD+XfkQtlkCvwHu2cIquuyXu8cCknc9JpmMTH8yIKZ8WQJhJC3ETTJIOyPH3oFCBL+vlqNxjFI44tsazIY5nRXGfN9SnCKO1IpWzwNLqu+xfxCiCR2OprXxJnRXD2wPdWC2Va52xWOQxb1e4yQg10MHxB2uHidjTLOEYRbq1ZiF4vs1QCgRA/ICpeFCGyAVENH+rXw9Q0sW4yji4u2mb+EyXlqwyRFmOAFujPstcEirTc1JDRjHe7IsK0ewWla7fPfkZX5YTwkraNWDx+JUMlNx/MmvAyyjkPJ2Ldp+Xin8TjLO9aVWWOEUIYi0WPhOpiCmossJz1JLzXPjIXa4DYQG8+NyOwOWTNYAuAG0v6u7LB4UrK8j0PXn0ZKPtrpKRcQlJF7pShneJEseGfPi/6xQbRD77OKZ0RZymLH3+xVApCk05FDrGUB5KexvExK+6ylHrJrpagkAWHXgVVw3KdFHy0sA1QvT5O0qIX+dS4+x0VH7TBlsVqu40zA6Zlp5XfoyAhZ5rxQ0mt3QZEnNkUkl6WipUBvRWn6GHuF5VJFdLbFgxQRSrHTgJI81GF711vFi8wv9W3Kvs21Pd3XHMF5UQNGYgtzKcmZgEorNvQuOJRzerfCl6t2fBqCBFAiWUd75z20lK2G3r5ESYnMr67N5G53pQV620hyDjscKCGQc9W683aRe74ltxNi8Xsxvs4sQXCgkyN5PJg4+gu3alJntl+lzer1FMULx8KTuSQNvzuS4AintSVxhyQSBoee9kfegIgLtwGvvJ6flWK2h1gLKIdxztWbooXApdFHpbSgrAEp6Qnw0XG7KY756QnBWRkTT7/ueRqMUHwYq4RxwLep1FQXcIzw8exTttk42NbJpZ9nIuMybYvZMHM8/5o6yxZogIDCnY7ow/GLH1n1Tb/cE2VNKpa7LpkkmcDHlNBaqNQ1tLkH/d7qw2MRMA UWlMvkmg ERv7V0XI5qZArTVpNolxyxBtoaSukK1nhxNH+Z8NxESuyBcxntDMmd/7eqBP5jY0dANManN6Cri97yWK/YxHW6Y6mCVQz1Wti9WgrI/n3aukc3HIrkOocbvDAFudUhBrava2Abphh8RPUPCnnE3lJ4/lOGPzNOj2U4p7RkdoL50HTzI3bKJVXoD/nzEc+F1Uaw02phjPD5enPjXUt7xEZCPiaDOfCZHL96Vvo11rghwp2qCBTWEHvdO90FuCpi8FPkRg2xc7mUKmhm+i9Qhwco1Bm090BbJx44ertLXGZrLbv4ZAjaBU5JDZxnVmH1Akc2HhlGTkBXSnab8/fETBCH+qmoA== 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 about 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 coalescing 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 avoid the disadvantages as much as possible. 1. https://lore.kernel.org/all/f4abe71b3439b39d17a6fb2d410180f367cadf5c.camel@gmail.com/ CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- mm/page_frag_cache.c | 46 ++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 609a485cd02a..4c8e04379cb3 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -63,9 +63,13 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) { +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + unsigned int size = nc->size; +#else unsigned int size = PAGE_SIZE; +#endif + unsigned int offset; struct page *page; - int offset; if (unlikely(!nc->va)) { refill: @@ -85,11 +89,24 @@ 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)) { + offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); + if (unlikely(offset + fragsz > size)) { + if (unlikely(fragsz > PAGE_SIZE)) { + /* + * 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; + } + page = virt_to_page(nc->va); if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) @@ -100,33 +117,16 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, goto refill; } -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size = nc->size; -#endif /* OK, page count is 0, we can safely set it */ set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset = size - fragsz; - if (unlikely(offset < 0)) { - /* - * The caller is trying to allocate a fragment - * with fragsz > PAGE_SIZE but the cache isn't big - * enough to satisfy the request, this may - * happen in low memory conditions. - * We don't release the cache page because - * it could make memory pressure worse - * so we simply return NULL here. - */ - return NULL; - } + offset = 0; } nc->pagecnt_bias--; - offset &= align_mask; - nc->offset = offset; + nc->offset = offset + fragsz; return nc->va + offset; } From patchwork Mon Aug 26 12:40: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: 13777860 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 E9126C5472C for ; Mon, 26 Aug 2024 12:46:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC15B6B04AC; Mon, 26 Aug 2024 08:46:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A22746B04AF; Mon, 26 Aug 2024 08:46:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 801F96B04B0; Mon, 26 Aug 2024 08:46:22 -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 5B2096B04AC for ; Mon, 26 Aug 2024 08:46:22 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 15047121076 for ; Mon, 26 Aug 2024 12:46:22 +0000 (UTC) X-FDA: 82494369804.03.2413EF0 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf12.hostedemail.com (Postfix) with ESMTP id 880504000A for ; Mon, 26 Aug 2024 12:46:18 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=none; spf=pass (imf12.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724676293; 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=gV6KFTQV31H/w8/zs6OOQYJCUldLshy5X43K7n5w55I=; b=45Z9JyRrEnWJ1n+umDaFct/yTmYNlfisYtbz1iHMZUKw9Z9yoaTJEzhhyosTpbZtKhByw/ OCra7D5fEFPPdj8YUPa4OWJOqaf1S15pHSIzxKJn1JVTX6mXpgO7IALSCdWMIeluLbVU2x SH1mc8OqpO5yqINayDr5mA/I/ShWPtE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724676293; a=rsa-sha256; cv=none; b=Dw62JeHcWsMqWH8/TRXiZV16HQsmilkObgVSsHJoDdE9yGT+h8kB4294oZU4aBa7gKGybC noeUtZ1FkvrLbSUroBwsFnmVVe7F+eS3HU+XupZkQFikPZgJv4cqkFmicx5dH77EnJxSqD LKkNvacrTfBPMzYvgTQKWHTGD3c2KBc= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=none; spf=pass (imf12.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Wsr304ZsSzfZ36; Mon, 26 Aug 2024 20:44:12 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 976E81800A5; Mon, 26 Aug 2024 20:46:15 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 26 Aug 2024 20:46:15 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Alexander Duyck , Chuck Lever , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Andrew Morton , Eric Dumazet , David Howells , Marc Dionne , Trond Myklebust , Anna Schumaker , Jeff Layton , Neil Brown , Olga Kornievskaia , Dai Ngo , Tom Talpey , Shuah Khan , , , , , , Subject: [PATCH net-next v15 04/13] mm: page_frag: avoid caller accessing 'page_frag_cache' directly Date: Mon, 26 Aug 2024 20:40:11 +0800 Message-ID: <20240826124021.2635705-5-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240826124021.2635705-1-linyunsheng@huawei.com> References: <20240826124021.2635705-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 880504000A X-Stat-Signature: yg9du3b5trmw4g4ea7j95mid7nnqb7bm X-HE-Tag: 1724676378-905500 X-HE-Meta: U2FsdGVkX1/Bni7beqrrOIIEM3Nhf1ycxiIZ9W5/nmrJGTt9qP6EkE9/JevC0gDdbP1Woq1k1KMElSSwt7PG3KvNpaSPb+B/XCeuhriVeMEbTA6twya8amGpi2P4opW/LQpEcivZdi44yNqrQ5SxWxK6cnI02FYj5p10EGjEp9vB+x7uhVkTMgq9jAg5GPzbI1J1c351zN19OrhXxtXwSucclHHlB03D9O4aF9Q1s7vOFYJZhu1u0BklnmEiWAZNfWoVZ2yCboxBK/6pE+phqhD4ILHNGLK/5G3/LYQ6rWmovlALuMA/dRGjA+bCIvt9SWYu44t5rIrQqdv1Ow7URZvckeE0npddj1K/zcTEwui3OMKnNg67DEiEsiYHvbJAG/DDCi03k4sx5fvaZqbciRu61bUVkLF18e+bdvr+rtadAdgWAYbG7rJPNjsNfMihmnxIE3U47Ujx9W+VxD+QPknw0k6VWJ5i9Uykn41uKYt9yr2Vs9j8HZLSP+dePuhS0i1XSVl8LKMaFxu8eea3pDg9a+TPirbDsnq5WSORd5orHQcgr2r51GPWASCAmyFjbtEl0ijgHLDtcu/sFTlv+J/hJxeUopptxThQeZT+3qK7go7pQZOlD7am1tmEZnR1hAKiUGYUa/HV3mLSaONmgIBiqGOp/CuzmZpqIs2TDKg36N1ZZWATFnKx0a/F/XLiIc4BX7B0aPNdOGsmuinlhmOje4+CQX+nqZlqhLLC3VB6FaKIHGVzHa1Ov6fPblTF31pVVmpg6PJ5ikcwFwayu02FFmvykKyvDndo+DdGwZ7U8ZoRkvrpl7A5DwKTu5NXEfKb1EASpkCLf6ldcKx2HPbDXf9r/wvqI1gLKQJh8j7vnvwMFkHLVxtioXvC/HfZ1VVt8AVQ5NCNxpH085tOQNZZzRjH3iX2FoTsmqHv8FZY11NnmI+Ap4bz6Tmq3WrBKV5smNsdqhyA6tdh6X4 uI8vAXav NBL9XknwxU24OS3UU0eebh8YEBC8JhO4k91ah/vfqPjv2ty4Z7RjGSEA9ekWFExCgDCJnU7TFUGSsOI6Ko78f9jnHSKwBd7ZBt6J//vu7k1RDLeW6ZYlYUKv8D0Bxw69dUaS5SW68FWFuAc9vbblYczzIi6eICAb7nxib8eeOVC3mYg1yPa8/QHptqNEG/CS6aCIheHkUcWpDH9r277CSYSwGI/dFXxab6Od1aOWTWRzjksS2C3Szrx/sgavbPDkSLDYe/6MR9m7sV2C1X7bi55M7g8Zu3L3yr3exy7oit0CdtfeQpUXkOlsS+OUuB836o0xrdarLWM1zhovcGDO9poimDdbEFp+db2Ztqo6I9itcczi70E685dV7jg== 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 Reviewed-by: Alexander Duyck Acked-by: Chuck Lever --- drivers/vhost/net.c | 2 +- include/linux/page_frag_cache.h | 10 ++++++++++ net/core/skbuff.c | 6 +++--- net/rxrpc/conn_object.c | 4 +--- net/rxrpc/local_object.c | 4 +--- net/sunrpc/svcsock.c | 6 ++---- tools/testing/selftests/mm/page_frag/page_frag_test.c | 2 +- 7 files changed, 19 insertions(+), 15 deletions(-) diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c index f16279351db5..9ad37c012189 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 67ac8626ed9b..0a52f7a179c8 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -7,6 +7,16 @@ #include #include +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_align(struct page_frag_cache *nc, unsigned int fragsz, diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 1748673e1fe0..9352fcf8cda3 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -752,14 +752,14 @@ 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); - pfmemalloc = nc->pfmemalloc; + pfmemalloc = page_frag_cache_is_pfmemalloc(nc); } else { local_bh_disable(); local_lock_nested_bh(&napi_alloc_cache.bh_lock); nc = this_cpu_ptr(&napi_alloc_cache.page); data = page_frag_alloc(nc, len, gfp_mask); - pfmemalloc = nc->pfmemalloc; + pfmemalloc = page_frag_cache_is_pfmemalloc(nc); local_unlock_nested_bh(&napi_alloc_cache.bh_lock); local_bh_enable(); @@ -849,7 +849,7 @@ struct sk_buff *napi_alloc_skb(struct napi_struct *napi, unsigned int len) len = SKB_HEAD_ALIGN(len); data = page_frag_alloc(&nc->page, len, gfp_mask); - pfmemalloc = nc->page.pfmemalloc; + pfmemalloc = page_frag_cache_is_pfmemalloc(&nc->page); } local_unlock_nested_bh(&napi_alloc_cache.bh_lock); 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 6b3f01beb294..dcfd84cf0694 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); } diff --git a/tools/testing/selftests/mm/page_frag/page_frag_test.c b/tools/testing/selftests/mm/page_frag/page_frag_test.c index 4a009122991e..c52598eaf7e7 100644 --- a/tools/testing/selftests/mm/page_frag/page_frag_test.c +++ b/tools/testing/selftests/mm/page_frag/page_frag_test.c @@ -117,7 +117,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); From patchwork Mon Aug 26 12:40: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: 13777861 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 7D0CAC54731 for ; Mon, 26 Aug 2024 12:46:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 345DA6B04B6; Mon, 26 Aug 2024 08:46:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2A8466B04B7; Mon, 26 Aug 2024 08:46:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D2E66B04B8; Mon, 26 Aug 2024 08:46:24 -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 D66266B04B6 for ; Mon, 26 Aug 2024 08:46:24 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 98CCE141042 for ; Mon, 26 Aug 2024 12:46:24 +0000 (UTC) X-FDA: 82494369888.21.77A5830 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf20.hostedemail.com (Postfix) with ESMTP id 2B25F1C0018 for ; Mon, 26 Aug 2024 12:46:21 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf20.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724676313; a=rsa-sha256; cv=none; b=aMLulCe8k6/eOSjrK28ARz/NeOSKdb9Q8acCPTT4WE99gN8XqOjRWT3qVxp0d27ubc1fRZ arazfHQK/fplnS4M9EpYw0bXlyfjmIJ05NSZrxMoMngwv6cuILYwRfU2FIJaBZ5p7AYJjz tvTm/9IlFuoppfnfiN56Ucoj73jNjg8= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf20.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724676313; 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=vq3OZUXFU1xnLVLmAu4B27D+KoxniE+ptc/x9pklNZM=; b=iqmnJAymmqc1LkUGEnKgZ0sCngN6FDVY+LNww0ztasdHCFMvHsge4+WerErOmNDI4IWqxd b5K7zxmT7Zkz3pOVQDv1iN0nVx4Y4NlLzbFn7ycae93kEFE5KUMlj9PpaLwJb43PgQOlZb ZA+naqSccedvhqV75w7MzjWhjqmNz30= Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Wsr3W6RyyzpTgj; Mon, 26 Aug 2024 20:44:39 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 04DFA1800A5; Mon, 26 Aug 2024 20:46:19 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 26 Aug 2024 20:46:18 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v15 06/13] mm: page_frag: reuse existing space for 'size' and 'pfmemalloc' Date: Mon, 26 Aug 2024 20:40:13 +0800 Message-ID: <20240826124021.2635705-7-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240826124021.2635705-1-linyunsheng@huawei.com> References: <20240826124021.2635705-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 2B25F1C0018 X-Stat-Signature: x86eya8ps5m3nqae7wra5ck5q7sr5sjb X-Rspam-User: X-HE-Tag: 1724676381-234395 X-HE-Meta: U2FsdGVkX1/rHjoS5EZWt3gnwdTj6EsBQvsKLA/CYbid9NN2rO+UmNM7CJ/vIaabw5SobuFB/YYB7ABZY2wvwu3djOgT6fVqNpZqyUNEu6FHpRTVU8ZVxh8y1USypDB3lI0Aatyv8o/LOH1sy9AcUMlF8d/rTXQAXO8NMs1i642H5Rht2eZsUDHjIPwI4ZNF0SOFeWrLwBxxEBZ2WB/OQ4J0bz6pgis9C/3LD409ds0tck5363zPFlC6wgydQh+qtMmGtNjXun+yI1aeNRHckw/bDa7j94prIaPNVJujD77TN0dtiSg/yFLtLVixRaUN/GFKYC1G71awaXyfhbz2UdjvnYfDx7w4aWe22shKVDq7n6XmQmXM3pHelKrDaD1DZ8xAkWgDH100GUu+QePwJPHrY7utCU/yRDEp/cgCLf0urSxgPm+9I9eguETqsN7Lq1J6vZUcNA6CF/UV3LX3koOSoHRPCXdp4fYxCVEEZDaRT9iQW/F7AuTktTNjAcF7XPulZZw+JXh4R9KSpzrctSRpu+tFYoXPEk554vtf/zDETUqgeJS1jeJ4Pwu6m/EmvtZ7r+SE8dc7Jd0jViE2BLq9x+V3byoDM5G7avWjqrMTY+yMyZPSAgexMg3vbhmZJ/S+dp3R6pCzId2ilTvt/eqoBL8FJcXqaTlhhmOW/R4NuRoeBrWmuUJfydRV240Q8IJjzJEX1abHLr70L0SllnM6iowU43vJ8rLR756/LKMUL15OMKYRamuZ0co0F0W7wj8hos4zED6rWx2xvymF66AEA9/mX5Sv+nYjvFXfPNX3sj9B3pz9PxZlJEuqinZIw0M/Amd6DLvDElG5JTD2wB+PVqYWtRc2fWHe2r/eeK0JdqcuF8/3KNCXHqRpDB/YYSaBrK5rNWyJtznTdcTzVLSFLNwBfpLIDPWmJAS8u3OOjT7TnlSOWyryZN6J+Z/viPL5p17DGgrAgEZxH7E cl7hSeKm KcIOsdRwoZM/aSoAWzTlNpMqNNZ9cgW6XFPCtiNGd99uNgREoR13whbysAckcaJtuwXbdWFdk2azFQybAsciH8vVMcvXwKi5hkvuU6O8Ebq/6cVAXZIPwh7kQwhL5XJMCL8AfuTvwMQPcY/Xpyr2M5NQTqaNpI0bw2IqjLMBJCPhMLeRfCSxktCLdVw62fm0I48qATaVRX/4+kTc1hzYX8lm/W6TlWh+Nry2BZTISW/iqzj0K096ruIInI9/R2g2i8CPFS2BwMl4VEFM3x8bGz8kkX2SLlN8c6l6s 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. 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/mm_types_task.h | 19 ++++++----- include/linux/page_frag_cache.h | 60 +++++++++++++++++++++++++++++++-- mm/page_frag_cache.c | 51 +++++++++++++++------------- 3 files changed, 97 insertions(+), 33 deletions(-) diff --git a/include/linux/mm_types_task.h b/include/linux/mm_types_task.h index cdc1e3696439..a8635460e027 100644 --- a/include/linux/mm_types_task.h +++ b/include/linux/mm_types_task.h @@ -50,18 +50,21 @@ struct page_frag { #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) + /* encoded_page consists of the virtual address, pfmemalloc bit and order + * of a page. + */ + unsigned long encoded_page; + + /* we maintain a pagecount bias, so that we dont dirty cache line + * containing page->_refcount every time we allocate a fragment. + */ +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) && (BITS_PER_LONG <= 32) __u16 offset; - __u16 size; + __u16 pagecnt_bias; #else __u32 offset; + __u32 pagecnt_bias; #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; }; /* Track pages that require TLB flushes */ diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 0a52f7a179c8..372d6ed7e20a 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -3,18 +3,74 @@ #ifndef _LINUX_PAGE_FRAG_CACHE_H #define _LINUX_PAGE_FRAG_CACHE_H +#include +#include #include +#include #include #include +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) +/* Use a full byte here to enable assembler optimization as the shift + * operation is usually expecting a byte. + */ +#define PAGE_FRAG_CACHE_ORDER_MASK GENMASK(7, 0) +#define PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT 8 +#define PAGE_FRAG_CACHE_PFMEMALLOC_BIT BIT(PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT) +#else +/* Compiler should be able to figure out we don't read things as any value + * ANDed with 0 is 0. + */ +#define PAGE_FRAG_CACHE_ORDER_MASK 0 +#define PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT 0 +#define PAGE_FRAG_CACHE_PFMEMALLOC_BIT BIT(PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT) +#endif + +static inline unsigned long page_frag_encode_page(struct page *page, + unsigned int order, + bool pfmemalloc) +{ + BUILD_BUG_ON(PAGE_FRAG_CACHE_MAX_ORDER > PAGE_FRAG_CACHE_ORDER_MASK); + BUILD_BUG_ON(PAGE_FRAG_CACHE_PFMEMALLOC_BIT >= PAGE_SIZE); + + return (unsigned long)page_address(page) | + (order & PAGE_FRAG_CACHE_ORDER_MASK) | + ((unsigned long)pfmemalloc << PAGE_FRAG_CACHE_PFMEMALLOC_SHIFT); +} + +static inline unsigned long page_frag_encoded_page_order(unsigned long encoded_page) +{ + return encoded_page & PAGE_FRAG_CACHE_ORDER_MASK; +} + +static inline bool page_frag_encoded_page_pfmemalloc(unsigned long encoded_page) +{ + return !!(encoded_page & PAGE_FRAG_CACHE_PFMEMALLOC_BIT); +} + +static inline void *page_frag_encoded_page_address(unsigned long encoded_page) +{ + return (void *)(encoded_page & PAGE_MASK); +} + +static inline struct page *page_frag_encoded_page_ptr(unsigned long encoded_page) +{ + return virt_to_page((void *)encoded_page); +} + static inline void page_frag_cache_init(struct page_frag_cache *nc) { - nc->va = NULL; + nc->encoded_page = 0; } static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc) { - return !!nc->pfmemalloc; + return page_frag_encoded_page_pfmemalloc(nc->encoded_page); +} + +static inline unsigned int page_frag_cache_page_size(unsigned long encoded_page) +{ + return PAGE_SIZE << page_frag_encoded_page_order(encoded_page); } 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 4c8e04379cb3..228cff9a4cdb 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -15,13 +15,13 @@ #include #include #include -#include #include #include "internal.h" static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, gfp_t gfp_mask) { + unsigned long order = PAGE_FRAG_CACHE_MAX_ORDER; struct page *page = NULL; gfp_t gfp = gfp_mask; @@ -30,23 +30,31 @@ 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)) { + nc->encoded_page = 0; + return NULL; + } - nc->va = page ? page_address(page) : NULL; + order = 0; + } + + nc->encoded_page = page_frag_encode_page(page, order, + page_is_pfmemalloc(page)); return page; } void page_frag_cache_drain(struct page_frag_cache *nc) { - if (!nc->va) + if (!nc->encoded_page) return; - __page_frag_cache_drain(virt_to_head_page(nc->va), nc->pagecnt_bias); - nc->va = NULL; + __page_frag_cache_drain(page_frag_encoded_page_ptr(nc->encoded_page), + nc->pagecnt_bias); + nc->encoded_page = 0; } EXPORT_SYMBOL(page_frag_cache_drain); @@ -63,31 +71,27 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) { -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - unsigned int size = nc->size; -#else - unsigned int size = PAGE_SIZE; -#endif - unsigned int offset; + unsigned long encoded_page = nc->encoded_page; + unsigned int size, offset; struct page *page; - if (unlikely(!nc->va)) { + size = page_frag_cache_page_size(encoded_page); + + if (unlikely(!encoded_page)) { 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 + encoded_page = nc->encoded_page; + size = page_frag_cache_page_size(encoded_page); + /* 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; } @@ -107,13 +111,14 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, return NULL; } - page = virt_to_page(nc->va); + page = page_frag_encoded_page_ptr(encoded_page); 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(page_frag_encoded_page_pfmemalloc(encoded_page))) { + free_unref_page(page, + page_frag_encoded_page_order(encoded_page)); goto refill; } @@ -128,7 +133,7 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, nc->pagecnt_bias--; nc->offset = offset + fragsz; - return nc->va + offset; + return page_frag_encoded_page_address(encoded_page) + offset; } EXPORT_SYMBOL(__page_frag_alloc_align); From patchwork Mon Aug 26 12:40:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13777862 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 E8E28C5321D for ; Mon, 26 Aug 2024 12:46:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7AEE46B04B7; Mon, 26 Aug 2024 08:46:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 679F66B04B9; Mon, 26 Aug 2024 08:46:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 455F76B04BA; Mon, 26 Aug 2024 08:46:26 -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 13C696B04B7 for ; Mon, 26 Aug 2024 08:46:26 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C48F7121076 for ; Mon, 26 Aug 2024 12:46:25 +0000 (UTC) X-FDA: 82494369930.19.2FC8428 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf27.hostedemail.com (Postfix) with ESMTP id 5959B4000D for ; Mon, 26 Aug 2024 12:46:22 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.191 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=1724676270; 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=gNl1cpEVOeSfQXwVUyo2lvHkQahe05XKrmCRSgP0KAM=; b=wmeq07JMa/+M77tNqPS3w4xEBfUiJCrb5cQWH06dJI1N5boy1PVwe19j4WL06x85ZZgLJ9 nhkhM41olJk3+qMUJrZy+Kp/023EL+HfHLxzRv8F1I0RIY+LFi/yMJiAP4riRIRCjGxKvC q508UF6kqIwK9QbqGW5VOpFH5yhX3Nc= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.191 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=1724676270; a=rsa-sha256; cv=none; b=WvAhSKxsEgHpZ6ffd1PHTbUY2cY/Mz4g+NdQAD7L2ghY/M/pwGofntOHxzAFQ/ljzYB5k2 364R9lb/Y1ZuHuNQopTUXzkNwJ8Xlyf8uR0dcQsKmycf4Du0ic0ZU8m2g8FjmavaxaqSnp Qntsd6jOiB4bc+gov6J4x/Aii2pKsHw= Received: from mail.maildlp.com (unknown [172.19.163.44]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4Wsr5H6p44z1j7Kn; Mon, 26 Aug 2024 20:46:11 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 99F121402E1; Mon, 26 Aug 2024 20:46:20 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 26 Aug 2024 20:46:20 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v15 07/13] mm: page_frag: some minor refactoring before adding new API Date: Mon, 26 Aug 2024 20:40:14 +0800 Message-ID: <20240826124021.2635705-8-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240826124021.2635705-1-linyunsheng@huawei.com> References: <20240826124021.2635705-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 5959B4000D X-Stat-Signature: sibbo76mzk9wdu15tfhbpgjfttbxecja X-Rspam-User: X-HE-Tag: 1724676382-128329 X-HE-Meta: U2FsdGVkX19tVvX6MejNbDNQ13nwTT+IXh9cIxIC40qhFSxkDDL9iTEc9clcfXXSg6HcQL02eDTsP9YHBl50a0ld0qWcQqzDzddldnrzYbcPbq9LXXSenZ3f827gSl3ZgNXuOkzbO5thH3viF1Mrix0iP5RJstsZoWB8izHQjQhc2shdLUkxA1y2V92vx+5XJf14AkciYHWdB92wWUkM7obDsR5naKNorwKxQRMs8SNgWQ1pYdFidpJpOLA1buTJdkgPtBxBT2DD1gO7zx8Roa/+AvOaOVTOpcvq1AKV1xqFhLlTohGL4DT2dn93yQQbJVjE6/esy0sKPxiYI6eRvcBwwNrg9IoSIj6bNIaHy8JpcnNVaYoyiNwi53RQMLr1BGJFGyZy1QbCTOcRT5ulf8zyJ2wveaIAt/K/rspxUStR+0XL5va90r2GsAwexw+TQ9FBeZL0fbu/RvWAxrwdw9AeLET+NSFiaeIDWv9eCoNhm8jjxe01o5pE6d4dM88rEU/BOuYzLUkp7ksvQAHWIibaa++6u32blMdPHSzP8RcuCK6ARqKbP9e7WnzUsrZSvEIyV6Phn7O57/NJUfk24Ywd+Tawy2M4QkCzMkMRz7+0RfTfXcxO8cyDb+dftf7Xj8YNtmmQOMKfispDdDFmu6tPtEWEqFfcscPH+7eVz+2tXcB8tAiaefWdnqBySKt8SAN96wa3NEdHN8J08v+gftx7JoxWYDLPEB8H1blnPVAa5G1q2ovLGVsZKuvv/aVwK9ArEZqQ7x8x245V2g6AkwHisgm9YW0xW/rMCAkizgP3HSkfHTxPIgCui+vdvkPsySPxud/ZUotcPXDxJhi8c5EI1a7GaQ4mRON3HPi+/7lOgRAo0oEloAKEPKQYIP4/2pgVlhX0mqYKoISDwNe1mS2GKwsTZsUPIffTKD6//dyJCKxfQESDLSSrqonsajmWEVXNKUaGZ5em29pSPih D4nfC+sw V/h8jOmSTRvPV9OPrZXqbgnBiEQRJPqoR7aDrcm+YZe4HjgYI54gr31alvYmFw0Bvxwq5rEERYdovUXM2X8I36bm4LD8XmQMyznJ72ktOIhI4yc1PlbW1YgoCgHq1h3urfaRAwzr1jm4uTuVo5f9A0ymKd2gUpLr+UehmFT/27QO0/TxmVj4dDWDju14bJgox9I9SzSM2Lk/sCfKXIAz2KWzBz0wX+rT8XqU0wEM/4PrsRo+YL5LCxgyNa6B8nwUiDOQUpaLbMafGPJV8rufoBRSoCRCvYAVtgozr X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Refactor common codes from __page_frag_alloc_va_align() to __page_frag_cache_prepare() and __page_frag_cache_commit(), so that the new API can make use of them. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 51 +++++++++++++++++++++++++++++++-- mm/page_frag_cache.c | 20 ++++++------- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 372d6ed7e20a..2cc18a525936 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -75,8 +76,54 @@ static inline unsigned int page_frag_cache_page_size(unsigned long encoded_page) void page_frag_cache_drain(struct page_frag_cache *nc); void __page_frag_cache_drain(struct page *page, unsigned int count); -void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, - gfp_t gfp_mask, unsigned int align_mask); +void *__page_frag_cache_prepare(struct page_frag_cache *nc, unsigned int fragsz, + struct page_frag *pfrag, gfp_t gfp_mask, + unsigned int align_mask); + +static inline void __page_frag_cache_commit(struct page_frag_cache *nc, + struct page_frag *pfrag, bool referenced, + unsigned int used_sz) +{ + if (referenced) { + VM_BUG_ON(!nc->pagecnt_bias); + nc->pagecnt_bias--; + } + + VM_BUG_ON(used_sz > pfrag->size); + VM_BUG_ON(pfrag->page != page_frag_encoded_page_ptr(nc->encoded_page)); + + /* nc->offset is not reset when reusing an old page, so do not check for the + * first fragment. + * Committed offset might be bigger than the current offset due to alignment + */ + VM_BUG_ON(pfrag->offset && nc->offset > pfrag->offset); + VM_BUG_ON(pfrag->offset && + pfrag->offset + pfrag->size > page_frag_cache_page_size(nc->encoded_page)); + + pfrag->size = used_sz; + + /* Calculate true size for the fragment due to alignment, nc->offset is not + * reset for the first fragment when reusing an old page. + */ + pfrag->size += pfrag->offset ? (pfrag->offset - nc->offset) : 0; + + nc->offset = pfrag->offset + used_sz; +} + +static inline void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, + gfp_t gfp_mask, unsigned int align_mask) +{ + struct page_frag page_frag; + void *va; + + va = __page_frag_cache_prepare(nc, fragsz, &page_frag, gfp_mask, align_mask); + if (unlikely(!va)) + return NULL; + + __page_frag_cache_commit(nc, &page_frag, true, fragsz); + + return va; +} static inline void *page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index 228cff9a4cdb..bba59c87d478 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -67,16 +67,14 @@ 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_cache_prepare(struct page_frag_cache *nc, unsigned int fragsz, + struct page_frag *pfrag, gfp_t gfp_mask, + unsigned int align_mask) { unsigned long encoded_page = nc->encoded_page; unsigned int size, offset; struct page *page; - size = page_frag_cache_page_size(encoded_page); - if (unlikely(!encoded_page)) { refill: page = __page_frag_cache_refill(nc, gfp_mask); @@ -94,6 +92,9 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; nc->offset = 0; + } else { + size = page_frag_cache_page_size(encoded_page); + page = page_frag_encoded_page_ptr(encoded_page); } offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); @@ -111,8 +112,6 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, return NULL; } - page = page_frag_encoded_page_ptr(encoded_page); - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) goto refill; @@ -130,12 +129,13 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, offset = 0; } - nc->pagecnt_bias--; - nc->offset = offset + fragsz; + pfrag->page = page; + pfrag->offset = offset; + pfrag->size = size - offset; return page_frag_encoded_page_address(encoded_page) + offset; } -EXPORT_SYMBOL(__page_frag_alloc_align); +EXPORT_SYMBOL(__page_frag_cache_prepare); /* * Frees a page fragment allocated out of either a compound or order 0 page. From patchwork Mon Aug 26 12:40: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: 13777863 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 7E964C5321D for ; Mon, 26 Aug 2024 12:46:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 604146B0271; Mon, 26 Aug 2024 08:46:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5B3466B028D; Mon, 26 Aug 2024 08:46:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 42CE96B02B3; Mon, 26 Aug 2024 08:46:27 -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 21FAF6B0271 for ; Mon, 26 Aug 2024 08:46:27 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id DEF85A10F0 for ; Mon, 26 Aug 2024 12:46:26 +0000 (UTC) X-FDA: 82494369972.13.C663C23 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf07.hostedemail.com (Postfix) with ESMTP id C89644001D for ; Mon, 26 Aug 2024 12:46:24 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; spf=pass (imf07.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724676317; 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=XG0AMfntjar8R2wbYDjeRSL9npEsHL8uxsywldNm/NE=; b=wa/YAQ8aFWHt+Lh/CLCAbUnOy+NbbAcqLs6LD+zBsD7sAs256fbK5tfWTwGVGd8OlBZn4h RFNKgcApfcFRGAE8kt3qDrMFdBbOXrhl1OJRt62NR3sOtNF0KL9V6ndOvuD7CsXNRGPnMz iFuYsPQWGdHobXaDKlfRKcNDXdOcuF8= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; spf=pass (imf07.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724676317; a=rsa-sha256; cv=none; b=tYIeHJoB6uJGyu9mCXAQwVHDVV6cYOfVpPDwffgkbByMNFduTcHZ3iK2kq/7MGo5XMQ9UN vwCASE4ndsAeBngr9Ylqaf0nJmJZIvMKTb3shRuH9YWT4soFrDzviOT9RBhppspOul3VyS wbz1xB2LtBsSkdwRbHbJm21ozDnYWlI= Received: from mail.maildlp.com (unknown [172.19.88.194]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Wsr366t5rzfbC7; Mon, 26 Aug 2024 20:44:18 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id EAB501401F0; Mon, 26 Aug 2024 20:46:21 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 26 Aug 2024 20:46:21 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v15 08/13] mm: page_frag: use __alloc_pages() to replace alloc_pages_node() Date: Mon, 26 Aug 2024 20:40:15 +0800 Message-ID: <20240826124021.2635705-9-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240826124021.2635705-1-linyunsheng@huawei.com> References: <20240826124021.2635705-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemf200006.china.huawei.com (7.185.36.61) X-Stat-Signature: njyc3imiuyjowu53d64zjys8yark1k64 X-Rspam-User: X-Rspamd-Queue-Id: C89644001D X-Rspamd-Server: rspam02 X-HE-Tag: 1724676384-153658 X-HE-Meta: U2FsdGVkX18G3qAHbIuaVMzfXNF4f6zCrxuouTZb3uQ5DFrCnGKQ4R06SLghSrVHiGRjR1z85DO8pkSVBBculCJJ7MYBB9PESy77pmQi5TVK9Kky7FlZz/E1BxiLa+bVg/TSxdPjoJnbR40yFRLXS0h1JtLLK/gbH2ebK9dzEiJ8RyYaP+nzbicxO7LPPdUuQ6VNHVHME9OWQvLZW3gDHaEoEGE1f8nPrqQaXgb8ufdsteWsIMyo+6AWNrlLGle02k/0e8zFfma6oo5ENwjvgMUvkWh+xLhPSC0A97g+9hqhDUvCDMwjJjft//aByLQMTXMqrpPwz4L01oeztZx8dYfjFwLEbY0EMKoP6XvS8QhQh3ADOK+EuW7Tpus20zd7xt/mHOXYbkB0RGcplof4ON6zd3rAkbfiL/j366cAHDCWwTasBdaHZnhdtO9CBDE+68F5agvynHEwUrMIfltEFhRuuZM4UHxGCI1CjChdKfqaAuMU5aggBvytbjVXRM9Ye88dQPe2kd0ELw6XHZEMv+5tDJ9L7XWikihQgag3yMuVYUPDayywJihFmTIDqYyD3I0sg0ccHrnaObhA1LaDI1I45VuhcXUaeuJ4ouJ8v5Sxn7GDzPUGvqmnIA5c9OFTvS7qaTVWQyR27sZnDVmj1/H9keMEXva2Y9cXd7o1r8P+aJWD9ITiqzoJWLQtmj+LYsqZKuHt62cdMMwViBS5BdpQqprWyruPS2xPcfnf52JeYKt72ELfb5tVWqJf2WE9M2Y4ZCkiDX0IjjxWWFWqsE3J3uDi4ZcYXskSFoslBQFxBUxnQ07m1jfqDFp/CWRuJpccFm19N3mtQYcifBB4zHjI/Km4gO/GE5U8Yqd4lBLGj3Ojay/jfggAI702TSkVj4tBZO4rxyQZNfZLpq7KfaO/s6JTOfmPeFt4XToKEnr6/AmWrmX0FbxxkqGVWAz/fr7A6EcQz/EtH0JFFTn QcyTTVOr K9EOCEUR1f9HbuS3PtMZJzfu7micbpUahL6rk/YB2MPlNFoWgFbwtU9DSp34Frya2CQ+aT7dF7aUcbRw1tzMOoVV/Olk+Ks/PrDnqelP7NX1lZDIsf62NZrnW466yOWj5Bot4v3hGbZcOpX2IkKho/eLCiYNf7csOTigMzSjFhfNIXdrjbHwASJauGzJ7R2EM3lAInJy9d2ueyrBcB4XZBZ7KykrdpS66y2hyCCZNBs7ZTij0OaALbpE5ZpQ5VgN1NLNUE+CTia7l11/coi8SX/sKz9zPDzhhoU2E 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: It seems there is about 24Bytes binary size increase for __page_frag_cache_refill() after refactoring 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 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 bba59c87d478..e0ad3de11249 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -28,11 +28,11 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) 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)) { nc->encoded_page = 0; return NULL; From patchwork Mon Aug 26 12:40: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: 13777864 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 52636C5321D for ; Mon, 26 Aug 2024 12:46:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D566C6B04BE; Mon, 26 Aug 2024 08:46:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CE1436B04BF; Mon, 26 Aug 2024 08:46:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B828B6B04C0; Mon, 26 Aug 2024 08:46:33 -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 85BD26B04BE for ; Mon, 26 Aug 2024 08:46:33 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 34194810EF for ; Mon, 26 Aug 2024 12:46:33 +0000 (UTC) X-FDA: 82494370266.26.B77E7FA Received: from szxga06-in.huawei.com (szxga06-in.huawei.com [45.249.212.32]) by imf24.hostedemail.com (Postfix) with ESMTP id 9917F18001D for ; Mon, 26 Aug 2024 12:46:30 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf24.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.32 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724676372; a=rsa-sha256; cv=none; b=23QiV/8KFS6n7UJ++rx0IfXodxUHQP2CjxRzEotJTNqA4ElB8q/edJ2bjIQnmwZvAxjhoF uFTcsfPf4/6NaTHR5vfIAfJ5mYFWU5UAOONrH8frAozVhx8nmEegDIfJtM7bv1L7HizyDH VLmEwdphZp5PYDJP6jUnWJTHcSQNF5M= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf24.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.32 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=1724676372; 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=aXgDnx9gLdfjuniRTOHfcp8ChSvJwSzYYed7J0cqXLg=; b=Pykzbbh7uPjAw4oXWfnrQ69ufONF0sZ75twVMPELPynnwBooqAo3fdaaNXciMUT5+LFtSy GTXz0UConhhkK3RDYUu0hxuPhbcum6iL51Y8IZWj+M/WXo1hyGbzU1dbYlSEy15Sy0rKCc RaPZDFj05GSbq+2PtnKt1xA3zyzvm6Y= Received: from mail.maildlp.com (unknown [172.19.163.44]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4Wsr3K70TLz1xvNc; Mon, 26 Aug 2024 20:44:29 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id C063D1402E1; Mon, 26 Aug 2024 20:46:26 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 26 Aug 2024 20:46:24 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v15 10/13] mm: page_frag: introduce prepare/probe/commit API Date: Mon, 26 Aug 2024 20:40:17 +0800 Message-ID: <20240826124021.2635705-11-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240826124021.2635705-1-linyunsheng@huawei.com> References: <20240826124021.2635705-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspam-User: X-Rspamd-Queue-Id: 9917F18001D X-Rspamd-Server: rspam01 X-Stat-Signature: osfc4f1694jirpceusrg6nhh9r388qzf X-HE-Tag: 1724676390-647912 X-HE-Meta: U2FsdGVkX18jtBXlLrWm0YpV570CFhJmK7SVUlz8M2bIYM9TJJ5yuDvLXEuo4LHkAu6I92NDaOxisJfcpY6Kzlp1C2QIcnrvY99lxeEtMUCJtZMdhkjwkt8Gvhr7VlGyC4I5Triv6sX5X2QlSzDlWHAl8rwGD5qQsbJj/g+/wKZNtRw76a3Zm7u6nQPcF2yiDqAhHQqDya767QJ5EyufNJbSUsNTN5TYNtV2w/5gNNM9s9uty2SZxghoFb3qnAszgKIPzLj6RzPCHAKpGxfpxrGsBrcvChePw6D0BacHGs2x4xNFo9IEuz8GIhhAvYZGYGSZE3vtxkh3hrjHHv4ARfxAgDluUrZr0lrfdgayNbKWJ4kzPgt0O7lVNBBxbfK6lg9iWmKyxPgsYKx0xVF/mX8T0vAXq6ztuPMNW/WyaQLj7i4JGV47saAy0MXlLh8QY+zNFj7M6ShrrKmkSpJ2N+MWVomcdx1bmVtly/TfkwYxB7ChRjopQjB/s34ygnv4rK9yDJOOiQqp3cwOX75/atuar85VtRQhioOEoyGF+iXHYf2aWGKym1rEsWBPjXlJCrSc8ZcOolK4y9th20RJ5uTFtqvfppocQieRQi0rK27VtYmK5TKvGG1RpS1oI9wH71BHKMEcTSYZyY9r57YzRHrjt2kxIWzcmZl2Rr55pYFT+oh8vcPE3WBDExXJEs7RstGX34RVCuCMW0CYW57gEDslw0nmnxy4D4EbzQ5t9ohjmKqpP5ZLktp3pIKPlG0aKf8J4d13r4PZJH+KW/KP530PlbXfyMChBBlbDikmNzvNXBko9KoR6Fkf9Cv6jMHVVbcAqHWyQhKpZh/JeIzRuUJ/S9PTnr7tkS0cjN3vaXBvzslJIQzhl6EzSudIxVkNBvlirfbpzqlulpvApXmV3bkvU0S3n6g82TQZVMTX3pmafjGMgxJpf2Tvkybx4RX52yln42f5/30mUu51GM9 amcXY8vU E/8btfgX5nY3fIK3hyJ0LJcLkX8GkPctyAZqy0JtxPUVLRniKqIkGJ8VLI6TovPZh6/3PCkoa6VwqFVgaTczupu6Eias72TNPsxmCGa5xX1xABwM400nQfxYLqdX2nVAH7NWuc3cKYqjs2dzEdyrGngSKEak8+DLQnSC0iZTiQilU5JQSbVAiLX6MU7zrQWj3u4Lr4+DhQ/blLNyOfpVMcMLHxsD7S5aitKUdFSMzR0idpVM14I0XBUvwExORmAsEWM0gbaNMnarOUDoz3010qHVSKVPAJU9MFWh7 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. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- include/linux/page_frag_cache.h | 138 ++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 2cc18a525936..9d2a7dd39d64 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -74,6 +74,11 @@ static inline unsigned int page_frag_cache_page_size(unsigned long encoded_page) return PAGE_SIZE << page_frag_encoded_page_order(encoded_page); } +static inline unsigned int page_frag_cache_page_offset(const struct page_frag_cache *nc) +{ + return nc->offset; +} + void page_frag_cache_drain(struct page_frag_cache *nc); void __page_frag_cache_drain(struct page *page, unsigned int count); void *__page_frag_cache_prepare(struct page_frag_cache *nc, unsigned int fragsz, @@ -139,6 +144,139 @@ static inline void *page_frag_alloc(struct page_frag_cache *nc, return __page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u); } +static inline bool __page_frag_refill_align(struct page_frag_cache *nc, unsigned int fragsz, + struct page_frag *pfrag, gfp_t gfp_mask, + unsigned int align_mask) +{ + if (unlikely(!__page_frag_cache_prepare(nc, fragsz, pfrag, gfp_mask, align_mask))) + return false; + + __page_frag_cache_commit(nc, pfrag, true, fragsz); + return true; +} + +static inline bool page_frag_refill_align(struct page_frag_cache *nc, unsigned int fragsz, + struct page_frag *pfrag, gfp_t gfp_mask, + unsigned int align) +{ + WARN_ON_ONCE(!is_power_of_2(align)); + return __page_frag_refill_align(nc, fragsz, pfrag, gfp_mask, -align); +} + +static inline bool page_frag_refill(struct page_frag_cache *nc, unsigned int fragsz, + struct page_frag *pfrag, gfp_t gfp_mask) +{ + return __page_frag_refill_align(nc, fragsz, pfrag, gfp_mask, ~0u); +} + +static inline bool __page_frag_refill_prepare_align(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + gfp_t gfp_mask, + unsigned int align_mask) +{ + return !!__page_frag_cache_prepare(nc, fragsz, pfrag, gfp_mask, align_mask); +} + +static inline bool page_frag_refill_prepare_align(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + gfp_t gfp_mask, + unsigned int align) +{ + WARN_ON_ONCE(!is_power_of_2(align)); + return __page_frag_refill_prepare_align(nc, fragsz, pfrag, gfp_mask, -align); +} + +static inline bool page_frag_refill_prepare(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + gfp_t gfp_mask) +{ + return __page_frag_refill_prepare_align(nc, fragsz, pfrag, gfp_mask, ~0u); +} + +static inline void *__page_frag_alloc_refill_prepare_align(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + gfp_t gfp_mask, + unsigned int align_mask) +{ + return __page_frag_cache_prepare(nc, fragsz, pfrag, gfp_mask, align_mask); +} + +static inline void *page_frag_alloc_refill_prepare_align(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + gfp_t gfp_mask, + unsigned int align) +{ + WARN_ON_ONCE(!is_power_of_2(align)); + return __page_frag_alloc_refill_prepare_align(nc, fragsz, pfrag, gfp_mask, -align); +} + +static inline void *page_frag_alloc_refill_prepare(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + gfp_t gfp_mask) +{ + return __page_frag_alloc_refill_prepare_align(nc, fragsz, pfrag, gfp_mask, ~0u); +} + +static inline void *__page_frag_alloc_refill_probe_align(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag, + unsigned int align_mask) +{ + unsigned long encoded_page = nc->encoded_page; + unsigned int size, offset; + + size = page_frag_cache_page_size(encoded_page); + offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask); + if (unlikely(!encoded_page || offset + fragsz > size)) + return NULL; + + pfrag->page = page_frag_encoded_page_ptr(encoded_page); + pfrag->size = size - offset; + pfrag->offset = offset; + + return page_frag_encoded_page_address(encoded_page) + offset; +} + +static inline void *page_frag_alloc_refill_probe(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag) +{ + return __page_frag_alloc_refill_probe_align(nc, fragsz, pfrag, ~0u); +} + +static inline bool page_frag_refill_probe(struct page_frag_cache *nc, + unsigned int fragsz, + struct page_frag *pfrag) +{ + return !!page_frag_alloc_refill_probe(nc, fragsz, pfrag); +} + +static inline void page_frag_commit(struct page_frag_cache *nc, struct page_frag *pfrag, + unsigned int used_sz) +{ + __page_frag_cache_commit(nc, pfrag, true, used_sz); +} + +static inline void page_frag_commit_noref(struct page_frag_cache *nc, + struct page_frag *pfrag, unsigned int used_sz) +{ + __page_frag_cache_commit(nc, pfrag, false, used_sz); +} + +static inline void page_frag_alloc_abort(struct page_frag_cache *nc, unsigned int fragsz) +{ + VM_BUG_ON(fragsz > nc->offset); + + nc->pagecnt_bias++; + nc->offset -= fragsz; +} + void page_frag_free(void *addr); #endif From patchwork Mon Aug 26 12:40:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13777865 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 3EC0FC5321D for ; Mon, 26 Aug 2024 12:46:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C6CE86B04C2; Mon, 26 Aug 2024 08:46:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C1C156B04C3; Mon, 26 Aug 2024 08:46:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A96566B04C4; Mon, 26 Aug 2024 08:46:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 818BE6B04C2 for ; Mon, 26 Aug 2024 08:46:43 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 1E56A410AC for ; Mon, 26 Aug 2024 12:46:43 +0000 (UTC) X-FDA: 82494370686.30.F2D7C94 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf10.hostedemail.com (Postfix) with ESMTP id 83C2FC0020 for ; Mon, 26 Aug 2024 12:46:39 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=none; spf=pass (imf10.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=1724676357; 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=EFsX67/k+GCg8GVo8VWQCASAtjcG67l+dEsCbJSRxR8=; b=dsczX7sZx+TrIu7SZgIaxSf5TVWstqwhjbtOCJyO0+PdtFJNycO1EfXpk1rlakj5MunTNz vrhTFsNh3LVy73UF+DJmDoPRK82SO+B+44hbP3pjZ7yBllJiIrdjD5IMVk28/mwAzZ+tGc jTlrsuIuu9fCSYal/c/asHquU8xr3SI= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=none; spf=pass (imf10.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724676357; a=rsa-sha256; cv=none; b=Lf0IMK04ZK8fOvDi2anDYAHQ/DzTCc3qNJkHC4GphZPk7Rzlo0HKO3I41+hP/Re0m3gQm7 cVBPjcDrMCccihATrwGAgxxe5ZYfF06r5oy28Fv1PTcicrIMUVMY2LK6d1/G9bXvcq1MVc Nabv01WWquyyo5Uhvh/JJWG6+2mlbLs= Received: from mail.maildlp.com (unknown [172.19.88.234]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4Wsr0D6wbSz20mjC; Mon, 26 Aug 2024 20:41:48 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 35F701402E1; Mon, 26 Aug 2024 20:46:36 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 26 Aug 2024 20:46:35 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Jonathan Corbet , Andrew Morton , , Subject: [PATCH net-next v15 12/13] mm: page_frag: update documentation for page_frag Date: Mon, 26 Aug 2024 20:40:19 +0800 Message-ID: <20240826124021.2635705-13-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240826124021.2635705-1-linyunsheng@huawei.com> References: <20240826124021.2635705-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 83C2FC0020 X-Stat-Signature: itwyi5y4jg9kgzzcbek6g7953x9p1n61 X-HE-Tag: 1724676399-975427 X-HE-Meta: U2FsdGVkX18HS/C3Fd4K/XIodh3QUAq7eO4MMR1a9G9XhcGeQpn+AmV2uWp2gpdtyThdE3i2aYmt4/ppkOrHV+HvEXm2hxbofjVdNB6W9IsoYlUA9oIXsH/bwRRRtan+cX/x3qobtx0MQH/uMtECHzdybwsMnTL4FBEvGUvIu5+Si+jRDJrGqp/zjTcCI55bQ7WBmIMSa3kx67Sr4V7SQim06jUhUfB3/idyuvsHt08HhsXVjt3XZDT/KiKARFWTIV/VqdJvOqminInhd9VYIza+QZvD0eedg0L3yjt7AehlsaPiusBrIMWDzwXx5kKcsC7TRRDsPgBZbksNGtoV/NX6Ic9ZwbEzW8Q+oQOwndKprRhCsuFAOoiShaTf2HYMs39BpZtSAxGf0RUKRsaTBMV/BFTOarnHrTVOtorpSQ5chdyufL/ggbKXiwYLuBE4RGbbQ8DDE697kzxn/ZgxCKfWBpkcZfXzDr97GtOsZeUBmHmPzS634GYgoLPPasZqelRc0NEL4sezXUYGsryscM8vnW9yXyRXCfzoImSsgPuUpVOuPLW3u3Jf/Og5967iVAKgQrvjzDOzujGOikDIejaKj89kbchVnN1yTZ9KPU445fUqwTzbxemgYFpqKgOtFJLinyp+VCAwjbDGFKQKchXcogU3gQlvVVT9AG+GWt0gnrnJov5cQ8m1HRbDYVCrtRVZfjx3Slf1SX5APSJKfQeqWYk/dS2xQ2MSFIoVxvnll87/teoHoRX1W8HcoNtT3ZWMzlkYkiq23xXUue++tJGItdMtc6tT0uVhwI1UIsApnzLBDc3wN1JviAJ3TsgK4qY/0kmDyZM/TIaQdeTxgt7r7wLg3+ZcSMxLCQ2iCNvjlIB4T6um9w+gBGX66GXk5f5NL+CLyxIFaZl7oUZ06esbNix0usRN0PF919C85l1XFkWwm1YrXGbLqUcebGfBr3vE2q2EwsuDj7ll7Tp ZG5kC1gt vMzsR0nDnXQXDTBz76Oi5MxgZ0W/6n1t9gyqCEknm0+89BfZ+5QFoStEhzcUgokcN3QOyh6ZEdfA0UGwVlh/1A2kuahaHYStjU7LwwDTQBe83tqsAgtzQuyx2E8DMu69M2P0PSwdA+q2rD8avJomF1scdSeleVfFOXWxRZZrHHubkPqDUfgUh8iu4S+lGu+9d9KmwNNCrtk1ryApAF8qL+w97ppAXMQrDmKDY1EUczpfwl23hGvnkEMyh0tP7liWvk0nEFc2IIIugJnJU6+HdRuqd4sf6KGmuCeTVfIq/A5a2Tp6in8gKuR6rSAZYETs5mbEroY9QIeJJTGipX6yoW+CvrLkh6pT83I1LJsBBIJL7W2JRq1gF5LHHx00nIZZFhxx7gMGXUDZCprdPY6p7kerEuU6Kohx/3hG3 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 | 173 +++++++++++++++++++++- include/linux/page_frag_cache.h | 251 ++++++++++++++++++++++++++++++++ mm/page_frag_cache.c | 12 +- 3 files changed, 433 insertions(+), 3 deletions(-) diff --git a/Documentation/mm/page_frags.rst b/Documentation/mm/page_frags.rst index 503ca6cdb804..d80f44cb471e 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,173 @@ 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 | + | | | + | +-----------------+ | + | | reuse old cache |--Usable-->| + | +-----------------+ | + | | | + | Not usable | + | | | + | 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_*_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, page_frag_refill, or +page_frag_alloc_refill 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_*_prepare() and +page_frag_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_align + page_frag_alloc_align page_frag_alloc + __page_frag_refill_align page_frag_refill_align + page_frag_refill __page_frag_refill_prepare_align + page_frag_refill_prepare_align page_frag_refill_prepare + __page_frag_alloc_refill_prepare_align + page_frag_alloc_refill_prepare_align + page_frag_alloc_refill_prepare + __page_frag_alloc_refill_probe_align + page_frag_alloc_refill_probe page_frag_refill_probe + page_frag_commit page_frag_commit_noref + page_frag_alloc_abort + +.. kernel-doc:: mm/page_frag_cache.c + :identifiers: page_frag_cache_drain page_frag_free + +Coding examples +=============== + +Init & Drain API +---------------- + +.. code-block:: c + + page_frag_cache_init(nc); + ... + page_frag_cache_drain(nc); + + +Alloc & Free API +---------------- + +.. code-block:: c + + void *va; + + va = page_frag_alloc_align(nc, size, gfp, align); + if (!va) + goto do_error; + + err = do_something(va, size); + if (err) { + page_frag_free(va); + goto do_error; + } + +Prepare & Commit API +-------------------- + +.. code-block:: c + + struct page_frag page_frag, *pfrag; + bool merge = true; + void *va; + + pfrag = &page_frag; + va = page_frag_alloc_refill_prepare(nc, 32U, pfrag, GFP_KERNEL); + if (!va) + goto wait_for_space; + + copy = min_t(unsigned int, copy, pfrag->size); + if (!skb_can_coalesce(skb, i, pfrag->page, pfrag->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_commit_noref(nc, pfrag, copy); + } else { + skb_fill_page_desc(skb, i, pfrag->page, pfrag->offset, copy); + page_frag_commit(nc, pfrag, copy); + } diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index 9d2a7dd39d64..01a0d7db3ac0 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -59,11 +59,28 @@ static inline struct page *page_frag_encoded_page_ptr(unsigned long encoded_page return virt_to_page((void *)encoded_page); } +/** + * 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) { nc->encoded_page = 0; } +/** + * 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 expectation 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 page_frag_encoded_page_pfmemalloc(nc->encoded_page); @@ -74,6 +91,16 @@ static inline unsigned int page_frag_cache_page_size(unsigned long encoded_page) return PAGE_SIZE << page_frag_encoded_page_order(encoded_page); } +/** + * 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 nc->offset; @@ -115,6 +142,19 @@ static inline void __page_frag_cache_commit(struct page_frag_cache *nc, nc->offset = pfrag->offset + used_sz; } +/** + * __page_frag_alloc_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' + * + * Alloc 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_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) { @@ -130,6 +170,19 @@ static inline void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned return va; } +/** + * page_frag_alloc_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 the 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_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align) @@ -138,12 +191,36 @@ static inline void *page_frag_alloc_align(struct page_frag_cache *nc, return __page_frag_alloc_align(nc, fragsz, gfp_mask, -align); } +/** + * page_frag_alloc() - 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 + * + * Alloc a page fragment from page_frag cache. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ 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); } +/** + * __page_frag_refill_align() - Refill a page_frag with aligning requirement. + * @nc: page_frag cache from which to refill + * @fragsz: the requested fragment size + * @pfrag: the page_frag to be refilled. + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * @align_mask: the requested aligning requirement for the fragment + * + * Refill a page_frag from page_frag cache with aligning requirement. + * + * Return: + * True if refill succeeds, otherwise return false. + */ static inline bool __page_frag_refill_align(struct page_frag_cache *nc, unsigned int fragsz, struct page_frag *pfrag, gfp_t gfp_mask, unsigned int align_mask) @@ -155,6 +232,20 @@ static inline bool __page_frag_refill_align(struct page_frag_cache *nc, unsigned return true; } +/** + * page_frag_refill_align() - Refill a page_frag with aligning requirement. + * @nc: page_frag cache from which to refill + * @fragsz: the requested fragment size + * @pfrag: the page_frag to be refilled. + * @gfp_mask: the allocation gfp to use when cache needs to be refilled + * @align: the requested aligning requirement for the fragment + * + * WARN_ON_ONCE() checking for @align before refilling a page_frag from + * page_frag cache with aligning requirement. + * + * Return: + * True if refill succeeds, otherwise return false. + */ static inline bool page_frag_refill_align(struct page_frag_cache *nc, unsigned int fragsz, struct page_frag *pfrag, gfp_t gfp_mask, unsigned int align) @@ -163,12 +254,38 @@ static inline bool page_frag_refill_align(struct page_frag_cache *nc, unsigned i return __page_frag_refill_align(nc, fragsz, pfrag, gfp_mask, -align); } +/** + * page_frag_refill() - Refill a page_frag. + * @nc: page_frag cache from which to refill + * @fragsz: the requested fragment size + * @pfrag: the page_frag to be refilled. + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * + * Refill a page_frag from page_frag cache. + * + * Return: + * True if refill succeeds, otherwise return false. + */ static inline bool page_frag_refill(struct page_frag_cache *nc, unsigned int fragsz, struct page_frag *pfrag, gfp_t gfp_mask) { return __page_frag_refill_align(nc, fragsz, pfrag, gfp_mask, ~0u); } +/** + * __page_frag_refill_prepare_align() - Prepare refilling a page_frag with aligning + * requirement. + * @nc: page_frag cache from which to refill + * @fragsz: the requested fragment size + * @pfrag: the page_frag to be refilled. + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * @align_mask: the requested aligning requirement for the fragment + * + * Prepare refill a page_frag from page_frag cache with aligning requirement. + * + * Return: + * True if prepare refilling succeeds, otherwise return false. + */ static inline bool __page_frag_refill_prepare_align(struct page_frag_cache *nc, unsigned int fragsz, struct page_frag *pfrag, @@ -178,6 +295,21 @@ static inline bool __page_frag_refill_prepare_align(struct page_frag_cache *nc, return !!__page_frag_cache_prepare(nc, fragsz, pfrag, gfp_mask, align_mask); } +/** + * page_frag_refill_prepare_align() - Prepare refilling a page_frag with aligning + * requirement. + * @nc: page_frag cache from which to refill + * @fragsz: the requested fragment size + * @pfrag: the page_frag to be refilled. + * @gfp_mask: the allocation gfp to use when cache needs to be refilled + * @align: the requested aligning requirement for the fragment + * + * WARN_ON_ONCE() checking for @align before prepare refilling a page_frag from + * page_frag cache with aligning requirement. + * + * Return: + * True if prepare refilling succeeds, otherwise return false. + */ static inline bool page_frag_refill_prepare_align(struct page_frag_cache *nc, unsigned int fragsz, struct page_frag *pfrag, @@ -188,6 +320,18 @@ static inline bool page_frag_refill_prepare_align(struct page_frag_cache *nc, return __page_frag_refill_prepare_align(nc, fragsz, pfrag, gfp_mask, -align); } +/** + * page_frag_refill_prepare() - Prepare refilling a page_frag. + * @nc: page_frag cache from which to refill + * @fragsz: the requested fragment size + * @pfrag: the page_frag to be refilled. + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * + * Prepare refilling a page_frag from page_frag cache. + * + * Return: + * True if refill succeeds, otherwise return false. + */ static inline bool page_frag_refill_prepare(struct page_frag_cache *nc, unsigned int fragsz, struct page_frag *pfrag, @@ -196,6 +340,20 @@ static inline bool page_frag_refill_prepare(struct page_frag_cache *nc, return __page_frag_refill_prepare_align(nc, fragsz, pfrag, gfp_mask, ~0u); } +/** + * __page_frag_alloc_refill_prepare_align() - Prepare allocing a fragment and + * refilling a page_frag with aligning requirement. + * @nc: page_frag cache from which to allocate and refill + * @fragsz: the requested fragment size + * @pfrag: the page_frag to be refilled. + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * @align_mask: the requested aligning requirement for the fragment. + * + * Prepare allocing a fragment and refilling a page_frag from page_frag cache. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ static inline void *__page_frag_alloc_refill_prepare_align(struct page_frag_cache *nc, unsigned int fragsz, struct page_frag *pfrag, @@ -205,6 +363,21 @@ static inline void *__page_frag_alloc_refill_prepare_align(struct page_frag_cach return __page_frag_cache_prepare(nc, fragsz, pfrag, gfp_mask, align_mask); } +/** + * page_frag_alloc_refill_prepare_align() - Prepare allocing a fragment and + * refilling a page_frag with aligning requirement. + * @nc: page_frag cache from which to allocate and refill + * @fragsz: the requested fragment size + * @pfrag: the page_frag to be refilled. + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * @align: the requested aligning requirement for the fragment. + * + * WARN_ON_ONCE() checking for @align before prepare allocing a fragment and + * refilling a page_frag from page_frag cache. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ static inline void *page_frag_alloc_refill_prepare_align(struct page_frag_cache *nc, unsigned int fragsz, struct page_frag *pfrag, @@ -215,6 +388,19 @@ static inline void *page_frag_alloc_refill_prepare_align(struct page_frag_cache return __page_frag_alloc_refill_prepare_align(nc, fragsz, pfrag, gfp_mask, -align); } +/** + * page_frag_alloc_refill_prepare() - Prepare allocing a fragment and refilling + * a page_frag. + * @nc: page_frag cache from which to allocate and refill + * @fragsz: the requested fragment size + * @pfrag: the page_frag to be refilled. + * @gfp_mask: the allocation gfp to use when cache need to be refilled + * + * Prepare allocing a fragment and refilling a page_frag from page_frag cache. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ static inline void *page_frag_alloc_refill_prepare(struct page_frag_cache *nc, unsigned int fragsz, struct page_frag *pfrag, @@ -223,6 +409,20 @@ static inline void *page_frag_alloc_refill_prepare(struct page_frag_cache *nc, return __page_frag_alloc_refill_prepare_align(nc, fragsz, pfrag, gfp_mask, ~0u); } +/** + * __page_frag_alloc_refill_probe_align() - Probe allocing a fragment and refilling + * a page_frag with aligning requirement. + * @nc: page_frag cache from which to allocate and refill + * @fragsz: the requested fragment size + * @pfrag: the page_frag to be refilled. + * @align_mask: the requested aligning requirement for the fragment. + * + * Probe allocing a fragment and refilling a page_frag from page_frag cache with + * aligning requirement. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ static inline void *__page_frag_alloc_refill_probe_align(struct page_frag_cache *nc, unsigned int fragsz, struct page_frag *pfrag, @@ -243,6 +443,18 @@ static inline void *__page_frag_alloc_refill_probe_align(struct page_frag_cache return page_frag_encoded_page_address(encoded_page) + offset; } +/** + * page_frag_alloc_refill_probe() - Probe allocing a fragment and refilling + * a page_frag. + * @nc: page_frag cache from which to allocate and refill + * @fragsz: the requested fragment size + * @pfrag: the page_frag to be refilled + * + * Probe allocing a fragment and refilling a page_frag from page_frag cache. + * + * Return: + * virtual address of the page fragment, otherwise return NULL. + */ static inline void *page_frag_alloc_refill_probe(struct page_frag_cache *nc, unsigned int fragsz, struct page_frag *pfrag) @@ -250,6 +462,17 @@ static inline void *page_frag_alloc_refill_probe(struct page_frag_cache *nc, return __page_frag_alloc_refill_probe_align(nc, fragsz, pfrag, ~0u); } +/** + * page_frag_refill_probe() - Probe refilling a page_frag. + * @nc: page_frag cache from which to refill + * @fragsz: the requested fragment size + * @pfrag: the page_frag to be refilled + * + * Probe refilling a page_frag from page_frag cache. + * + * Return: + * True if refill succeeds, otherwise return false. + */ static inline bool page_frag_refill_probe(struct page_frag_cache *nc, unsigned int fragsz, struct page_frag *pfrag) @@ -257,18 +480,46 @@ static inline bool page_frag_refill_probe(struct page_frag_cache *nc, return !!page_frag_alloc_refill_probe(nc, fragsz, pfrag); } +/** + * page_frag_commit - Commit allocing a page fragment. + * @nc: page_frag cache from which to commit + * @pfrag: the page_frag to be committed + * @used_sz: 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_commit(struct page_frag_cache *nc, struct page_frag *pfrag, unsigned int used_sz) { __page_frag_cache_commit(nc, pfrag, true, used_sz); } +/** + * page_frag_commit_noref - Commit allocing a page fragment without taking + * page refcount. + * @nc: page_frag cache from which to commit + * @pfrag: the page_frag to be committed + * @used_sz: 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_commit_noref(struct page_frag_cache *nc, struct page_frag *pfrag, unsigned int used_sz) { __page_frag_cache_commit(nc, pfrag, false, used_sz); } +/** + * 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) { VM_BUG_ON(fragsz > nc->offset); diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c index e0ad3de11249..48e3aeb86942 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -47,6 +47,10 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, return page; } +/** + * 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_page) @@ -137,8 +141,12 @@ void *__page_frag_cache_prepare(struct page_frag_cache *nc, unsigned int fragsz, } EXPORT_SYMBOL(__page_frag_cache_prepare); -/* - * Frees a page fragment allocated out of either a compound or order 0 page. +/** + * page_frag_free - 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(void *addr) {