From patchwork Fri Aug 30 11:18:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13784981 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 25C68CA0FE0 for ; Fri, 30 Aug 2024 11:24:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8C9516B010E; Fri, 30 Aug 2024 07:24:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 878E86B010F; Fri, 30 Aug 2024 07:24:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 740B26B0110; Fri, 30 Aug 2024 07:24:45 -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 5805F6B010E for ; Fri, 30 Aug 2024 07:24:45 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E5882160ECA for ; Fri, 30 Aug 2024 11:24:44 +0000 (UTC) X-FDA: 82508679288.04.581AED2 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf19.hostedemail.com (Postfix) with ESMTP id 964561A000C for ; Fri, 30 Aug 2024 11:24:41 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf19.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.255 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=1725016982; 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=dfp9kKnT6EBpi6VR3LyqBEm82LohB4VomznWpLaPYGY=; b=kWSvER0Brc0b9p4ek+90YgHvQMh3MSKNBkYTzRYh8z1JB9ZMK7c5Ju5u67IK4hLJ2CRg7d 7JqczUVdO9hk9KJBExOkDmbr2bWOpoVjwHYbq/McPueEhRzmw+m/jP4mLXuGVk/IOX1LzI OOv4jzj/tfAD0hDbkqutEJAAD7kY+n4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725016982; a=rsa-sha256; cv=none; b=P70bZjUkMpBooYX9MDiOqZcgDpfkVqC0qi6R3p/R9Lr6PGHX3dJZJVLq9pRB05jjGUh/LB HCzS/dYrJBW1tQLk7Hs6wEME464JGlgRYlQwH2Kk31ui/UbviqfebzStwuZfZ3vU7vaCbg 3kPylX/W868yTyqXr4HTzOmThxmQSw4= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf19.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4WwG4L4nFzz13cbk; Fri, 30 Aug 2024 19:23:46 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 823D2140258; Fri, 30 Aug 2024 19:24:37 +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; Fri, 30 Aug 2024 19:24:37 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Shuah Khan , , Subject: [PATCH net-next v16 01/14] mm: page_frag: add a test module for page_frag Date: Fri, 30 Aug 2024 19:18:31 +0800 Message-ID: <20240830111845.1593542-2-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240830111845.1593542-1-linyunsheng@huawei.com> References: <20240830111845.1593542-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 964561A000C X-Stat-Signature: g6mym4j6zytqjnfmocwrjefnoepnadai X-Rspam-User: X-HE-Tag: 1725017081-42328 X-HE-Meta: U2FsdGVkX1+I49mMP10Y7zeD5rMGkb5SmQk3YeqSIdNQ/fFLJ7REd8bOWGidM3FjDspTnXmLcbMr9gZcH4hLXdmuYpBVgRWb/UCN2X6uS/MQA5i8hu9nX1TmHMguXTEWQvvLhEUIKTjkxHVUBYywcVjdXX8MdGKF/KCBzAxt3HMvM5bq4o7vFGzQjWJnTU3R1TNKnQdyM+QmWzHK9XIKNXZQ9ANHDPB9Jc3WV9GZ4Dze02lre5vx/aSk/TSeLs5VX4Sf0zzqL67zL56t236gVED/sQ7V1pCkeZs6vc74KFgfDXG2NETAsS3ZIbPuMBDfsuxq/YMqpIsOw/bqf/CTSg0Ac6Ya+u5txbcmE/IGSNrD5cCi7VJV6ZfnGADb8EdeBk4wE85yhnN+uKOkDxX1Brts/lWZuaVhD3MnruyhC8Dg04lqn6WmmkPv0PLsBLJzW8Ruju2SNsgcPzWP/N8/NTo07C9nhpczo8HvVUQtzVFQOhC7gblxXSzxRdzk5frSis0ybNlcXEXhMEbwfTuj2SfDNN6uBAP0q9Qg1Zf8sBTFz2dslgsxOjNutyxS5FsShz9aw+M/+7GF6sCPMWnOCmhCef+vtU8J/jeSsDy//pUoLk/N9Yea4ZbenCG4UfnXL6UyMb8YIZNqPfaYuFmRiKAGgeSyvJqUMeeMaTpvTk2ImVyZiBHrwOl9d2OIDazuqAWxNvrWBsB05ZC2842NcQOppCULkIDWWP11Raq/40Gmi+ZPHC8ZcWL8iBx8NLwPReYo0XNsdroXAVVu6p/fhV6C4Qscak4Gg845Vk6ptyOJEwcbS9IkJG6qMq4afEEIE6iayDQA0rvqxrxZZmHQDzxsrRV19PlxSma5JdMToI79gc3dxFe/RQfYwTPtvKHnskmx52bY5JqtgY/e2WJt+4O1OdNhwZS+/wUnjQk+R3jWc0cRJ47xFRBa7xVwBR92jgR2rLZXiFlmO33YRVb /23unUtI fQ/COw9sxMH/c/ii7/Otbm0I+i3C1rb6up92ZlVRAluEtwtN3uQaQouzExcAwfTwIab5TRIdCzYFq07hz6ZEd1j1tjS3yAR7U3nSX4a09YuM/TK6OGQZUNZ8HAuBUty2tUmK/V0riqzEXfTmbXjUmSTYORsBvbdfVCpO567QybZWF27JXfJ1SO9GmBCHy2RkMerlGK2JUVc22DI6XaH5IS1qJjh51yIvHR/pm214H4IUTEiN8sF9pJcJktQmqjAV8psz6PJDs0fn3eNFbv1eWlqAxfmTf9+ARBGFZ 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 | 8 + tools/testing/selftests/mm/test_page_frag.sh | 167 +++++++++++++++++ 5 files changed, 365 insertions(+) create mode 100644 tools/testing/selftests/mm/page_frag/Makefile create mode 100644 tools/testing/selftests/mm/page_frag/page_frag_test.c create mode 100755 tools/testing/selftests/mm/test_page_frag.sh 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..1c9070423420 --- /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 (C) 2024 Yunsheng Lin + */ + +#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_nc; + +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_nc, 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_nc, 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_nc.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_nc); + + 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..96fd470b9f51 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 @@ -456,6 +458,12 @@ CATEGORY="mkdirty" run_test ./mkdirty CATEGORY="mdwe" run_test ./mdwe_test +CATEGORY="page_frag" run_test ./test_page_frag.sh smoke + +CATEGORY="page_frag" run_test ./test_page_frag.sh aligned + +CATEGORY="page_frag" run_test ./test_page_frag.sh nonaligned + echo "SUMMARY: PASS=${count_pass} SKIP=${count_skip} FAIL=${count_fail}" | tap_prefix echo "1..${count_total}" | tap_output diff --git a/tools/testing/selftests/mm/test_page_frag.sh b/tools/testing/selftests/mm/test_page_frag.sh new file mode 100755 index 000000000000..aad55e0ca2cd --- /dev/null +++ b/tools/testing/selftests/mm/test_page_frag.sh @@ -0,0 +1,167 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2024 Yunsheng Lin +# Copyright (C) 2018 Uladzislau Rezki (Sony) +# +# This is a test script for the kernel test driver to test the +# correctness and performance of page_frag's implementation. +# Therefore it is just a kernel module loader. You can specify +# and pass different parameters in order to: +# a) analyse performance of page fragment allocations; +# b) stressing and stability check of page_frag subsystem. + +DRIVER="./page_frag/page_frag_test.ko" +NUM_CPUS=`grep -c ^processor /proc/cpuinfo` +TEST_CPU_0=0 +if [ $NUM_CPUS -gt 1 ]; then + TEST_CPU_1=1 +else + TEST_CPU_1=0 +fi + +# 1 if fails +exitcode=1 + +# Kselftest framework requirement - SKIP code is 4. +ksft_skip=4 + +# +# Static templates for testing of page_frag APIs. +# Also it is possible to pass any supported parameters manually. +# +NONALIGNED_PARAM="test_push_cpu=$TEST_CPU_0 test_pop_cpu=$TEST_CPU_1 test_alloc_len=12 nr_test=512000000" +ALIGNED_PARAM="$NONALIGNED_PARAM test_align=1" + +check_test_requirements() +{ + uid=$(id -u) + if [ $uid -ne 0 ]; then + echo "$0: Must be run as root" + exit $ksft_skip + fi + + if ! which insmod > /dev/null 2>&1; then + echo "$0: You need insmod installed" + exit $ksft_skip + fi + + if [ ! -f $DRIVER ]; then + echo "$0: You need to compile page_frag_test module" + exit $ksft_skip + fi +} + +run_nonaligned_check() +{ + echo "Run performance tests to evaluate how fast nonaligned alloc API is." + + insmod $DRIVER $NONALIGNED_PARAM > /dev/null 2>&1 + echo "Done." + echo "Ccheck the kernel ring buffer to see the summary." +} + +run_aligned_check() +{ + echo "Run performance tests to evaluate how fast aligned alloc API is." + + insmod $DRIVER $ALIGNED_PARAM > /dev/null 2>&1 + echo "Done." + echo "Check the kernel ring buffer to see the summary." +} + +run_smoke_check() +{ + echo "Run smoke test." + + insmod $DRIVER > /dev/null 2>&1 + echo "Done." + echo "Check the kernel ring buffer to see the summary." +} + +usage() +{ + echo -n "Usage: $0 [ aligned ] | [ nonaligned ] | | [ smoke ] | " + echo "manual parameters" + echo + echo "Valid tests and parameters:" + echo + modinfo $DRIVER + echo + echo "Example usage:" + echo + echo "# Shows help message" + echo "$0" + echo + echo "# Smoke testing" + echo "$0 smoke" + echo + echo "# Performance testing for nonaligned alloc API" + echo "$0 nonaligned" + echo + echo "# Performance testing for aligned alloc API" + echo "$0 aligned" + echo + exit 0 +} + +function validate_passed_args() +{ + VALID_ARGS=`modinfo $DRIVER | awk '/parm:/ {print $2}' | sed 's/:.*//'` + + # + # Something has been passed, check it. + # + for passed_arg in $@; do + key=${passed_arg//=*/} + valid=0 + + for valid_arg in $VALID_ARGS; do + if [[ $key = $valid_arg ]]; then + valid=1 + break + fi + done + + if [[ $valid -ne 1 ]]; then + echo "Error: key is not correct: ${key}" + exit $exitcode + fi + done +} + +function run_manual_check() +{ + # + # Validate passed parameters. If there is wrong one, + # the script exists and does not execute further. + # + validate_passed_args $@ + + echo "Run the test with following parameters: $@" + insmod $DRIVER $@ > /dev/null 2>&1 + echo "Done." + echo "Check the kernel ring buffer to see the summary." +} + +function run_test() +{ + if [ $# -eq 0 ]; then + usage + else + if [[ "$1" = "smoke" ]]; then + run_smoke_check + elif [[ "$1" = "nonaligned" ]]; then + run_nonaligned_check + elif [[ "$1" = "aligned" ]]; then + run_aligned_check + else + run_manual_check $@ + fi + fi +} + +check_test_requirements +run_test $@ + +exit 0 From patchwork Fri Aug 30 11:18:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13784982 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 9EB3ACA0FE1 for ; Fri, 30 Aug 2024 11:24:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2FDBD6B0110; Fri, 30 Aug 2024 07:24:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2D5B16B0111; Fri, 30 Aug 2024 07:24:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 14DD36B0112; Fri, 30 Aug 2024 07:24:49 -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 E65706B0110 for ; Fri, 30 Aug 2024 07:24:48 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A44B1C16C4 for ; Fri, 30 Aug 2024 11:24:48 +0000 (UTC) X-FDA: 82508679456.28.6EF7B15 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf06.hostedemail.com (Postfix) with ESMTP id B942F18001F for ; Fri, 30 Aug 2024 11:24:45 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf06.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725017020; a=rsa-sha256; cv=none; b=wMsATcUGHYgytzwxlPeQk+cHoh6SNWCFWxNRfICNW1UZax6LJC43zDM5//DiP1DPpghT3u tJ0U0rP8ZLX6x9tbDn0O7cO6mXlJQpSisneuPrLl2ANfeHw53736oP9IoVLgxSFDPg7pw8 MnLVC+1ggdnWOS7NNwDF41IONRHVUIA= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf06.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725017020; 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=NDMNnFurCNjd70lBTuWoWvp3blJxoOlPc3RW5EVwOhM=; b=2SiK14VGYzivxHq/T3hSFnrbxL48APR56/eAG0bEOrRx4H77xFz8qQ3h2/4FLAA/UqXw12 Ni7zRHxAOyLU77QxTYhaK17d4s7DEZjEL5Ti+cdK1G7vqCTkrx1HxZEym9vF4uJ5NV+1k1 /Nwqqvlw42R7U59FtaMkVaT2ukXtQR4= Received: from mail.maildlp.com (unknown [172.19.163.17]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4WwFzn5Chpz20mDj; Fri, 30 Aug 2024 19:19:49 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 52DF71A0188; Fri, 30 Aug 2024 19:24:41 +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; Fri, 30 Aug 2024 19:24:41 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , David Howells , Alexander Duyck , Andrew Morton , Eric Dumazet , Shuah Khan , , Subject: [PATCH net-next v16 02/14] mm: move the page fragment allocator from page_alloc into its own file Date: Fri, 30 Aug 2024 19:18:32 +0800 Message-ID: <20240830111845.1593542-3-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240830111845.1593542-1-linyunsheng@huawei.com> References: <20240830111845.1593542-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Queue-Id: B942F18001F X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: qf7mo7nnzhmic5j7iqpj4bjmnsqwuo19 X-HE-Tag: 1725017085-472956 X-HE-Meta: U2FsdGVkX19prLk/Rmauk38RQqwnD0DIspUr/IwSpNLpsvawY2LI62Hf227sfSBd4slqJfPjUVSZBMRpjmQjDZGcM91P2ZoXn+8rlvfqGG3OJp5nmnRQE+99Wa2nwxqccyzaH4SF1GNiC2Qc4APQ53tCRB1OTalkJVMblgD0U9L8QcNRUZdw1ETBhnKOSKqj/AgbSHR3rSb5SLxaC51lvhyi9hs9tjC1JZH4p5O4UHr41DVj94/D9XQT10ZY03ouXDN8dvqvHJs01dvHRENvOShxBsT0cGcVODu/0ednQb89kd6MDqFQMeq/+OfMWs4JQdjmEuA+gIqSJvSTNlYp7s4uP6PQOMOJxYlNY1gDdnvWxM/32eVCiOqorTWBGKaJz5OURHv0bHfXTOHl/3+ZEqT9t38ZaR5KHdS/Vddz8tn6ltGprQqpDJLleXjKUpVVWOsFM3g3Wf/a8sD/f/F8XkVgNsjGsGcD9ZtgjvpwfV1ExEUW1WDNcwE3CAi7AMTA0QLebKHXUApJ8ifV67/e4udVnNXHtR631rU7s2Ofi0Rslc+VslPgL6eA6dZ6DK52/GXfUWNY/6qsh/kqdnqoo+NyvW+8jjriTsBGtCaJD2rt8AzJ2ISVEeSaSPnlnU2X809o5+OPFGcCda/rcL+eUrB2eoQiZ5sTR419/ChXe+O1PDIC2w9sZ8bYd5aKc3pDgSxx5F59ooeQKwzstFXFwTK7LtsDX+yOoJR6F38PyL2Py/HDI97bxlL/61wtmfxdfcM22dJ6V2q3Tn9v08k3BFIn8NCekpE7tnSE6qwCXWPkM0lW/80ZcO9tNaNUYLQb5vnUT3A5hM/wPlVkSSWkXm1Ho/uR9GHSs1S2JUPPZXjYOg8vGI65OT4DVsLBequuQRoeCRlmIQSMR6uZFgHCcZEeENcQFxBauff+pHZscgoXpvrfM7P6vljE6jbRDyjTGzBJbLwcd2Yw5zGPEB1 0R3ZmAiN /XZuEOSas49Lzdpd5z2/vvsWjSWqirARscebW1LvlNXvJlL5KHmlRFWT6zcg9PIRth0lKAoVvUqr8W4Th8rRaO8EW8PEftZ0xS8ut7x9f+SLTUw9a8eG4O26yPNASR+wFI4aag0VKvvWtD0Jhk73SiG7NYhzaBl1pigo7ABYKEQNg1NazB64oXN1/WzrNVp6z3oDF+URzedo6BEwSeb7Eq5q9JqA+uyhNa6P+3FMitVAZDuIsPV2XvkoKHzgwarRB3S5e7lFRCTKGg6cMByWBeugDJ/QbjoDcWMn1gwWgfAUPRc+ljMN5tcOiK/7OemBo9SXSUx755y7rQsOqTdc4Vm0lKmvzQVdTrvvp9L2xqzSt0V4teJopnyF7l+BHe6cZs8kLFVTzIKo4Iaq12kDCDvt7C93e5LZ5xd3Uttbr/Xl4jXMF1skwa2lV7g== 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 1c9070423420..72a3861c2de1 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 (C) 2024 Yunsheng Lin */ -#include #include #include #include #include #include +#include static struct ptr_ring ptr_ring; static int nr_objs = 512; From patchwork Fri Aug 30 11:18:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13784983 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 848DECA0FE0 for ; Fri, 30 Aug 2024 11:24:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CE98E6B0111; Fri, 30 Aug 2024 07:24:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C97D26B0112; Fri, 30 Aug 2024 07:24:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B38B76B0113; Fri, 30 Aug 2024 07:24:49 -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 9496D6B0111 for ; Fri, 30 Aug 2024 07:24:49 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id BC7CF417E4 for ; Fri, 30 Aug 2024 11:24:48 +0000 (UTC) X-FDA: 82508679456.18.1AB299A Received: from szxga07-in.huawei.com (szxga07-in.huawei.com [45.249.212.35]) by imf27.hostedemail.com (Postfix) with ESMTP id 380C440007 for ; Fri, 30 Aug 2024 11:24:45 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.35 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=1725016996; 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=hCSmgUw5AsCX2iI+aZOW+yA359aYOsS2/g8+DO2CShb5JoSS1DK6QIHjszRJfbZXvEbsfy KOKqxqyDbS6DY+5cnLMc2srnzvPR+H/QMjKtJtWKRcu/Z+4QLXICffaOqaaaDi0U9N+1mb VL6pN66NHh2CCWveKfzHGIjJzb9bkik= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725016996; a=rsa-sha256; cv=none; b=e22KnVnQrecmWvNRTVq+GxgJRaxe2yUGadrO3oVAnAc2TV3Y5m4k2U3bTpNqWc9zxVOinM mHXxGyV1WNVZjBYqEgiH8BLpsYWAWY8ySbHSOtkNo3NmtveIJRaS8aPGWqaWcGu05qxOtD PLBCneQLE0CEAWlz9xFeLlM03VAaQok= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.35 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.163]) by szxga07-in.huawei.com (SkyGuard) with ESMTP id 4WwG581plJz1S9VV; Fri, 30 Aug 2024 19:24:28 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id C0655180042; Fri, 30 Aug 2024 19:24:42 +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; Fri, 30 Aug 2024 19:24:42 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v16 03/14] mm: page_frag: use initial zero offset for page_frag_alloc_align() Date: Fri, 30 Aug 2024 19:18:33 +0800 Message-ID: <20240830111845.1593542-4-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240830111845.1593542-1-linyunsheng@huawei.com> References: <20240830111845.1593542-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 380C440007 X-Stat-Signature: ggcuij44nezcdws64jr7jdcson591tm8 X-HE-Tag: 1725017085-870459 X-HE-Meta: U2FsdGVkX1+Wv+YFW52HYylgBIHQovljyUgg9uScS1cEKBQ5H9cK7N973le5h14QuW9oJNAf0NRQEM1RqeE5+kI3nu4u9f8Btfu5JUfrk6zCP33bUH8paUFtMYMTFpCb4P7+VBO6acYlAxwLiPUoSNn3AyfshsS5j6WlasKZ8UbBq1souLBg4f7DtvPyAoI16oLDlGSK+xe2yP1lYaxy/Tk45kLbh2o/u2eD6Ik6iHAU1+FPZBnsfhgWCrGFD9cNuhN5r36TvU5fKzJ6CrEIZ550PlIyp3sOssdvWd98nWIo/ci+ZjX1gW9Pblz08acwtXNDZXv9RfcA2a/Y27l1FEXMc8Z+gpvHs4F2oV4e7PxNVUxJ2y9Bw619a39dzaKCNhagG5V5UewsA2UefHf20hFVnZAfqLp/n/ryDOssUouL/ScO7ZJUnLg7XBAo9N3HtS8V8G9GkosP3vdpZWdCRqVZuM9uxgTypPy6ppl3vaTocIJ4MapvowKO+Xy/4EC28SSz2PyWPZUT3OV3K1sG8vjyWNLdnEgy5KWs2IRZSWo8x6LyDH+G86jVsWkWuIjWDclZ/US1htNBSdKsfk0CjsalNvMfjyqZax3v67dVbosOOVCOFwN4hmATXEHOKcg0C0RRrzCDLq8dTpLqN1JUGuz5+pDNRf+zB3XYLv60XHZx2XHCnwcA2TY0y0aHxp/QRsk2wgzn8GLfxlPWw1/HHM7uJVkhqWpxM7D75LlhebO2SeCOp0++8/azxGYwJ33fErfht5jdDzzKJlzyN1aLAA/wrcNnRRg3rUVXGxVZbkqzgbAE8U2kFxvWzKltiuqT1HWPnJkfvR1s+QXzW+WsVwy/Tu0VshpuGp+dS4BihVNlGKB+bgE189HMyauh4kl1dRaXzhkbd93jr4XQ0HcdmqeVb+W79jJY8LxXN/P1FYbPLA6oMtJxk3PdaBttww/seFAtkhyhJx4jZWCqhc8 Fru1Io11 ZT2I5L5kcEaUm/KTRQ/dPxpn3DueA2Ikbiav5ovVYK5h+MbREU9TjZPvwsAcSNkwxCjJon4R8tA91c9Ic+BlBslCdB7emwisTdqr82kBDsqwpgBck7f3QHUI35lENu9Hxpk9+MJjWhcOrbulwyPKhvEnOXcP61mr/YlfMKo/a4gEh3LlSIu2TBd2JoKPCWlUhEJkQYKNZAt0G5nR0T3AHmO/7RuTNHOMmVMhYLcwHDmlJ7PBftBaArQHXAG5g1Ur7jHEc+EOAztOgUm6YApbdvQ1P8ZBEbq+Js5aS0HPkYchDNVYNWJnfOGIlZA== 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 Fri Aug 30 11:18:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13784984 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 1B906CA0FE1 for ; Fri, 30 Aug 2024 11:24:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0B6E36B0112; Fri, 30 Aug 2024 07:24:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0165E6B0113; Fri, 30 Aug 2024 07:24:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DAC946B0114; Fri, 30 Aug 2024 07:24:51 -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 B17326B0112 for ; Fri, 30 Aug 2024 07:24:51 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 621361A0F48 for ; Fri, 30 Aug 2024 11:24:51 +0000 (UTC) X-FDA: 82508679582.16.56FA314 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf26.hostedemail.com (Postfix) with ESMTP id DA360140022 for ; Fri, 30 Aug 2024 11:24:48 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=none; spf=pass (imf26.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=1725017044; 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=aXbGFzP01IbB1a0lS1BZMeOEqPq6LnY6OHbxeTf5nlo=; b=U+M5D7Sl3HPfQ71lZSUsESQkyp2RJWowXYMeGLrtWhlnBJt79M2eQ5/WAZ50NJRg8LuMbh KsAKGXmbHNECQgglZbo3uzSu7KhTtZAm//3kDYfqEv/9m4X55oJrFykNy7coAFGx+LH8t0 NcvpQUSF7ko4PvnzYQ2bIwJMmk3zVqA= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; spf=pass (imf26.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=1725017044; a=rsa-sha256; cv=none; b=TfbGocTaxYyDZ8JMG047ANqB3PXnmQhU+X0VfSjs0RTx03mT6SpsxY5/eIFAJ23yxCCNff wOmoq7/sdqqtjNJC5inJFtcfNtoLsMuwHvDeaVPlT5MFeCnnyp6Flgk2jRN0tqefJbMP1X 5eVJo8eWwandwrx7c34BKsmcqeG/npk= Received: from mail.maildlp.com (unknown [172.19.88.105]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4WwG3S505QzpTtx; Fri, 30 Aug 2024 19:23:00 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id C54F1140360; Fri, 30 Aug 2024 19:24:45 +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; Fri, 30 Aug 2024 19:24:45 +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 v16 04/14] mm: page_frag: avoid caller accessing 'page_frag_cache' directly Date: Fri, 30 Aug 2024 19:18:34 +0800 Message-ID: <20240830111845.1593542-5-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240830111845.1593542-1-linyunsheng@huawei.com> References: <20240830111845.1593542-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: DA360140022 X-Stat-Signature: gk9xmfp5ozfzozwukw3ko9gkier47us1 X-HE-Tag: 1725017088-994835 X-HE-Meta: U2FsdGVkX1+BeSJa32xzyUISC37znf3Id8sY/epdQix38JHgUkavcbVutPUIraRf/z5+X6SqM4kVEG8HdZZ2IHWsErOdfmX/Ie7Mk8e0P1FQHpWumYFx1/tHVe7HW+f5zylml+tXLrx0s6J1/V20NdZQpc1lHyIha3nYMw0u7yitjA0d7Ku/RIClRYpGUzVl210eRm3lAEnKSARd/KlKzBfyjKi0cVM97KMgXg2kCeiUmZHXLfvNSuKSWUS0H09faHdxznVFqjXAcO8xSUQo8AlfGmuYyNdei0vPMs/E+4/L09Gh6k0Xbzl5kuuog84W/9s9/5GdCo40CeMeEiF4zvP9O+20iEqE66YTfE0adwvXPm7lWvhiGDJauhwdvON5GGNdf28GmIjAZh8io5fLRXNfCJ+SqsZEGMOUZGI/5w8kujCMpOYuasHLdkd4+m4IguenXdD++wSvyVYLEuZAF8l4UlfsNTCmW5ZypoqOp+hBmP9FdCYqRnDjHYgyyme1i7qAwrlKPEjTO5ykNZHJgDX8SPyQVz73RjYaCaCzT0P/PeW0Ytq7lt/muuUYFjHmRh1AFu9XjqqQ400anp1OPfuuqOx/9lC4OHV7aAUbnUpvd0I/OUBXGJIhwqdAQ6UmE/cbQOcIY+jZEtJl30j3qHclUrexxysqDEWQ8C/n8ezRjNNeAtgMPFa3AoDgAn5uYrPhy5PT8lro/C+sNDnxgemt/zK5hZYLr+r5jD88OkdTedJblvVZkW97zRty6UNphvWhXHUtf6xMfU08mc3rw6/xsgi+4tT2TpQsh5UlFwEJbjSYbP74RhAaoMzjMlVNsn8mt9K3zZGOK70ZSE6VSS+ZkTtWV6jCeyqnZK1jRm31of7Y2+Gmn5ku6B47ex01YfKF2iQyZhvdN63GewSE+H2LMzC69aRWpbVCVswM9FcGog6555QvnIVYoSi9wm4CBKR8fLa0y5zokE3vTkL QaJfhUy3 ZEf/OaifRJCQ/6tEDZ6cWx7vVRw38rEjZkqnMIraFwB1yLBMEeDeavEVD23jEdTGXojWppIfjX2bDvKzcAOv6lDQHIy5t7HhP5NlCako6jy+ogSmOF8nUNEzVMeNkUbuGDS8b7yWEXnQ+JRYLyWAp2vV75jUl12XwkJUMctxEIQOQQ0OOvQhjFGJxc+c9M0hf8qvCJrMgOoe1UUBcJRarMBtgj5oW2/OhBBD84amsNNKyWmYVugag58FpnVlHWDLywg8JCk4dzeLZahKwvk8p5orqK881HCgxS3QwuWv9SRwkaiTnbMkPvXKcnIC/UjzEjys4gvwqK9399FCsHgxiw91ESw== 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 a52638363ea5..a5f8e4e0c649 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 72a3861c2de1..e21a22b1d70b 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_nc.va = NULL; + page_frag_cache_init(&test_nc); atomic_set(&nthreads, 2); init_completion(&wait); From patchwork Fri Aug 30 11:18:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13784985 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 C33B6CA0FE0 for ; Fri, 30 Aug 2024 11:24:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 792776B0116; Fri, 30 Aug 2024 07:24:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 741E06B0117; Fri, 30 Aug 2024 07:24:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5E2116B0118; Fri, 30 Aug 2024 07:24:55 -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 3B9D86B0116 for ; Fri, 30 Aug 2024 07:24:55 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E8C86A1720 for ; Fri, 30 Aug 2024 11:24:54 +0000 (UTC) X-FDA: 82508679708.17.590E6F6 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf17.hostedemail.com (Postfix) with ESMTP id 826D24000E for ; Fri, 30 Aug 2024 11:24:52 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf17.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=1725017072; a=rsa-sha256; cv=none; b=iSnksZswb8PeUEeYrCN/GFBaVUxWXztoevl5tHzOimxbDRjgQpXg5AUEozn+wiB9U8LPf3 kO6g0wX+Yf38NidECo07754GFYR63VNqhxXSPXay0R5sJ4+1o7kEB4LWjUNjsTyD1Qaqtk bYEMOvc2RssbBqclmBp09QAWBOptDWE= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf17.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=1725017072; 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=Ltu5KS8hGU7mKA2WTvoh6c/9y5Iv9/BDvN8LfE5tp8c=; b=eGQoevVLfcXtPacXNeU6tkkfr6fWNhGs8RceYtnEHRdIwBkeoF+ckxtMvFLsdT1HfIdir2 xu6Xf+zsQSswRklgzRxQRGH9fRlW0ZFGI9FByuXT7XhwQ0gY6iyqXflz9oA12BtWbxRjhe iNN4Jf9F51ad9hXiFcxd3C8RdgfHw6M= Received: from mail.maildlp.com (unknown [172.19.162.254]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4WwG3838rjzLqrC; Fri, 30 Aug 2024 19:22:44 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 4FBCF180100; Fri, 30 Aug 2024 19:24:49 +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; Fri, 30 Aug 2024 19:24:49 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v16 06/14] mm: page_frag: reuse existing space for 'size' and 'pfmemalloc' Date: Fri, 30 Aug 2024 19:18:36 +0800 Message-ID: <20240830111845.1593542-7-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240830111845.1593542-1-linyunsheng@huawei.com> References: <20240830111845.1593542-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspam-User: X-Rspamd-Queue-Id: 826D24000E X-Rspamd-Server: rspam01 X-Stat-Signature: dpbyhp86nrrpdr3q7bqw19kwp3663hyj X-HE-Tag: 1725017092-248379 X-HE-Meta: U2FsdGVkX19LWXGIoFihPUnL2ynBclWzoN5LxcuyKjGi95NSoNtLQYRWUrSLhtQ+R4wJKkHSEO5H7sqB5cg30hdt7J+IrbjieLN55K3CD1x3S6DSbLqy5nWGJil7hb8G452Mbcis2SkrIsmqzabWKudGHm+Ytvs318Idisr3/ITTwza7Ya/hQawsBYIumzAq/nhWwzbzSn/iH/7Cd2tpPFJLS7YDY+YTzgrTR1llT/fNeFXzvV2/vd2P7Rlz6piZa9EJw3ZLN++0LBvS8fVJFZf3qnI8/v9OgOq/QAHq8bcjqejeNXSciQAl9nm/f3IbUHeM1G26A5xqdUqbKmoSX83OMUeDfxQrQvn4uscx23HRhGIYqcq0CDnJmyx++lly1TpcFOoEvjgai5F1VVHbp/nfOYtadzpgU7RPAa8z9x0eqBjT8c8nrcUbFPWd8UOiL43eJPTzuL36tm7T0AzS2DOcc5W9C2iJgl5B/26hex0k4KktuagHxpFi5TiCKKTADpXL+h1uSWXN5bMi/UM4Q3eij1M1OFoCThakykSp1FHbzzmBWQkEmXZpcpicXSiWh41fCETmjuE9CNAFmqb9nC0BujY67oMr24GYQ3nV8eB2DswJCIct5oMfkLQAUVzTyTvuuPmZcUtplqzPSBarCJy9kYJynrQFqb9q4WJCr5uVOWhHzcBOOuqSJSs/9QizunHXaEt/HJYx7VtUqEo5JafjwiobMA/M/zE2zZESiIjRGtUSOQVTWQEw09hvkCluw4aE0Wc8YlYKldGYRZIaiyjGF7loa8840YYIMwCL/veszz9a1/nDxKMCtaJ+qBIby2aCPDrlEd8vM37XlGa2PVDv6AMYbZCigiEn8zMAv3+AmZJrREctGGBmYTNlitrxXvbj/U94vzLFoofuoFcz0QKLeXUQPFI/6f0u+odZ0b0Tc1vU8Nh6UEHRic6s0cZueCieGMNkNaJJwRbL9Ik 7I06ZvV+ jDfGZ72BnDRCHOe/d45eQbH7HOzwvmqwCzEPvhXBfgV8DYV7+J3pARI3C2TAN74iMUhlL5E9UVWCLRL5w6wAjoHLoR78Q30z8R9VBRo6yZ9ZQHi6OCOlh89s1Keo8useH1zSmz4YSuUeZLAjsLsVPIIZ3L/2xYwfo4KJkif/4C5pdJlpnTHWFE3Ila9ksI/yLd7ryNFHnr0P2MGqSpy4A9GYI1hrC5dmcDPz8LzC2EhWWzf5dw0vFvYkcFWB477fzLCRDTUzZd5gOnHkJ/7rb8kZteLKnrLec0lk9 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 | 47 ++++++++++++++++++++++-- mm/page_frag_cache.c | 63 +++++++++++++++++++++------------ 3 files changed, 96 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..cb89cd792fcc 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -3,18 +3,61 @@ #ifndef _LINUX_PAGE_FRAG_CACHE_H #define _LINUX_PAGE_FRAG_CACHE_H +#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_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..a5c5373cb70e 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -12,16 +12,28 @@ * be used in the "frags" portion of skb_shared_info. */ +#include #include #include #include -#include #include #include "internal.h" +static 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 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 +42,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; + } + + order = 0; + } - nc->va = page ? page_address(page) : NULL; + 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 +83,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 +123,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 +145,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 Fri Aug 30 11:18:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13784986 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 44FAFCA0EFC for ; Fri, 30 Aug 2024 11:24:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0DE8E6B0117; Fri, 30 Aug 2024 07:24:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 015FF6B0118; Fri, 30 Aug 2024 07:24:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E1EE16B0119; Fri, 30 Aug 2024 07:24:56 -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 BF58E6B0117 for ; Fri, 30 Aug 2024 07:24:56 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 47E94817AE for ; Fri, 30 Aug 2024 11:24:56 +0000 (UTC) X-FDA: 82508679792.13.C6969DE Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf10.hostedemail.com (Postfix) with ESMTP id A1509C0021 for ; Fri, 30 Aug 2024 11:24:53 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=none; spf=pass (imf10.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725017003; 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=CMPc38kE0xudaGFU2qyxansFCuO8I1V8U9TvoFPvt2A=; b=f2zsEgDfUPzELf94wsgOejqIwveuLOP2RTvt+rITeOGjiQNYcmOGquSF0eFrJj+LbDZWa3 Xng2xkWadj4WdrZiGfjQDHwdNeuH041BswagbrdqSIyPV9h33ugMMjh8E3U+LOkI/jbv8s 4Y6VO3ru05U+J6Kl5dkp09uF6QaeK0g= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725017003; a=rsa-sha256; cv=none; b=sRqXw4fYdTtgvfEiokQxj1+ACbs1loC6NVKo+0H5XzOq4U0UwlXuR/kHgS5L8YiWAqaU6H EtR87Qg6D40bqdbYno7SN2asZmeWHIR0HxxcTNn74YcY4ypiM7dxuIfWabgCPwapYvnEh5 mBMiWc6rl+GkbD2S6VhB/TeFobcFGZE= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=none; spf=pass (imf10.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4WwG4y01vRzyR63; Fri, 30 Aug 2024 19:24:18 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 904BC1800D1; Fri, 30 Aug 2024 19:24:50 +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; Fri, 30 Aug 2024 19:24:50 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v16 07/14] mm: page_frag: some minor refactoring before adding new API Date: Fri, 30 Aug 2024 19:18:37 +0800 Message-ID: <20240830111845.1593542-8-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240830111845.1593542-1-linyunsheng@huawei.com> References: <20240830111845.1593542-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemf200006.china.huawei.com (7.185.36.61) X-Stat-Signature: 7j48mf4k3s8zonweuriai6j9ergdtsfa X-Rspamd-Queue-Id: A1509C0021 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1725017093-569049 X-HE-Meta: U2FsdGVkX1/ElwL6xuLUdXVdOvSld1q6diFR+ss7KilfKQhntkCNx578dz7WIbOEDE1+Rp8fb+eqptL8HCs90EoPv8ds8FRZ4ZkZC6NLRl73bo/x6GTiemgTVmeIYyRN97gJq7huOM2+rEdUP7Q1Cl0/6HA23TlRT+VirAvkDroG0saP4wbp5wkIJVNR47mP5NwEvCgETTjnW1bowI4B2m2/jjcZIimqU2N+nmIoQ6AxqFo0rrKG+QBLnE+q9OCSTw/Dkc71BR+GEPtUjtC8fv1/QzWvrWIqkOQqDiRnofoDHXgOxyVCgoRlwLZXTOea2HK2xlt9xLiq5DD7jLUeGuMEJYxFq+t0y0HwqCwwr/fPo9XUuHxbUOPI13H6ze2j2dgWSFIYRdLdyeKIZiJaKZ8kGq5IdD/VYq5//Ei+Json5QbAUgoopizG9iuA94O9xi0nYxqypfQAz31cV24i3R8QVW6mfzKHmFfIFld0XIaAOMbbMBoN8xG+O160YdYdQYMZWRgvUatQtT6o0TL1p/qHBlpggiFU2TV/yyAuOQ+1it/5Ic8bcUd/UTWKil5D93gJVvNp6QaA+4pPV/AlYfwKEPFyZ6mBWiKlLbRw2VvNfySNYrvqPA1Ie0qm1vDIs5FIzOwIVjQWqr8ffPLCyv1J1Pj4vNRhs2YGVpbtfF2hXXroRIb4ceuiZLguPKkLC8rbxeHDL0sVhdfA8JsUDx4EHO/G6K2F+4NoYbaS/EL1IiZUAVabfU+geX342+QAwpJDFRpatfPnCmk0WWEIjmIiAmweFDNjLOpNUw0BCQkb9xEl3tEgRmvMjiD7GjGZCMcfKF3Fzvg+ITlazFFCM68MZxcb40eHVxJ1fjw1R0LImYJQLhG5CDmwbgOm3wIqsUkbVLMMd8nKOQa2ChwqTOIDQ0aXiEw6qWES21QuT6DiBIMWIKm42SKepXLvQKDOHjwyJe0f4xUwqaqSlXz nrnRwrPk KPrfXDTvmpQDyrjm60b86a11t3T62bItkSSBPM4Ao/cnYhEf+VArRfN9WYdtByfCX1d8jtFKF0PnpseMOkJbxKj6WBnP1pJgw1cBzH9XWIzF9LLK7hT+iVC9vyqpncdlNAhZT7Z5/GHWCESNa9A6kKfiYph3fnDJ4OgJjixgvTr+oh/EpxFIV0iA6U6wzcCuhA5icey9aiww1iPZC8yhsOvIdlFR+3ZTXk3PCBZjEn3nrL7YWgtNE8LNKFo4+5L2IgQhS9hIjhXLaAw74OiKmA4QRYKxVQ7kcOKHl 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 | 47 +++++++++++++++++++++++++++++++-- mm/page_frag_cache.c | 21 ++++++++------- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h index cb89cd792fcc..f786ec572aa9 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -62,8 +63,50 @@ 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) +{ + unsigned int committed_offset; + + 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)); + VM_BUG_ON(pfrag->offset + pfrag->size > page_frag_cache_page_size(nc->encoded_page)); + + /* pfrag->offset might be bigger than the nc->offset due to alignment */ + VM_BUG_ON(nc->offset > pfrag->offset); + + committed_offset = pfrag->offset + used_sz; + + /* Return the true size back to caller considering the offset alignment */ + pfrag->size = (committed_offset - nc->offset); + + nc->offset = committed_offset; +} + +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 a5c5373cb70e..62d8cf33f27a 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -79,16 +79,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); @@ -106,6 +104,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); @@ -123,8 +124,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; @@ -139,15 +138,17 @@ 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; 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 Fri Aug 30 11:18:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13784987 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 C33ECCA0FE0 for ; Fri, 30 Aug 2024 11:25:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7E1CD6B0119; Fri, 30 Aug 2024 07:24:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7913B6B011A; Fri, 30 Aug 2024 07:24:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 658DE6B011B; Fri, 30 Aug 2024 07:24:59 -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 409936B0119 for ; Fri, 30 Aug 2024 07:24:59 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id F0ED5A170C for ; Fri, 30 Aug 2024 11:24:58 +0000 (UTC) X-FDA: 82508679876.17.67F8145 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf19.hostedemail.com (Postfix) with ESMTP id D19091A001B for ; Fri, 30 Aug 2024 11:24:55 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf19.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=1725017029; a=rsa-sha256; cv=none; b=aPvDr6QfATiwVYpl4+6bC7FxYxttI5dIGxbCs/NEjPukdb+2QAMVFFMQidOPIPxZo2T7B5 scS24XyITntQP/t75wFpEZxJ8jEKs2oKrifuziiWPgpJuD2dYHcTBk28STvvqj6ukvdv6P OzgfMr3eCsSbRVtzgjtt/96kl78Evbo= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf19.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=1725017029; 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=CDet5QsG0tzjksLvwf1qxRJfVDUyEDL2+uSpA5j8caA=; b=v6fuuBwSqKC3I9xB1bSgzM4XYk9AyqIUISrWVQdEGpDMz2DWM8lbqNVmL2UO0LxPqP/YK2 Shx5LzeHLlRGHJWIzbn8MTBBroZpOYewOs30/XjApsEbCC1SSqAXz6s7NmdRTna5vTVcoJ a4wSbQKyNGR6njSXpeyV9NF4wSjHJEY= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4WwFzz73HQzQr5M; Fri, 30 Aug 2024 19:19:59 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 09009140258; Fri, 30 Aug 2024 19:24:52 +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; Fri, 30 Aug 2024 19:24:51 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v16 08/14] mm: page_frag: use __alloc_pages() to replace alloc_pages_node() Date: Fri, 30 Aug 2024 19:18:38 +0800 Message-ID: <20240830111845.1593542-9-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240830111845.1593542-1-linyunsheng@huawei.com> References: <20240830111845.1593542-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Queue-Id: D19091A001B X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 8igakz6p8o81z3bkiuk6drrxms7tixw6 X-HE-Tag: 1725017095-577638 X-HE-Meta: U2FsdGVkX1/nhr2bMsB3j72sZ+JWw+uW9bGE8/FxgRkvPVNA1m7+3+eR0c2d7/AvvyCuRNHPi5gSybe++Kf10brzUVq3AKfxTg7TuXcpE9qPN0AMJFuwYQJ+U4sNiDPZH7sIkStpFVWPbk5b/gUgVGFb47YJzg4cxX6nDleyxEIMiH/5FEcOa3WtlPEwjAAiBez/bXLw8oipDgghpNsMbrKVVE+KFRhqT1QWtlBz+0VGYcKPPXjjcRhcJBr8+YLI4vDHLeyqgj/BBtNHHyBywCbDZJqbnBII7KLXrIvgyHL2Hnn2lGIk4IsmJ3AVVhr3qouFB1dl3UitnRuTvC6mgf9nT3TESSgQqTO9OaGePwwat6LM40Eq3l2Z6O4e5o6jiuT4j3d9tZD3K1dHf81LZ0r2LmVUMB04WKJeWKqtfL1GTbEXbGbPXRYOo622EiLcgDx67VsEUmxMpr5LixYpZLZR7BSIvdmMlWOcGF5jbyVjm8F3/HCFeMPHteaigYed7W3MWcJ3pgSRDmGweq+cEozYwI2vj6tL0G2CU4XeWtezmYf66zzuOzLSB3pMigSn9HVfoKJ+8CvYdFfoF8LFjmB987lIJ2MRsXGBn/vzIhorrFag+y5tzyctwRJkb/bAvAMvEpeHpFMjuQH92NkGmsrYlkS4XoD+wARxe2pj0KoTJahYy3Ky3vAqd9Dt0hPbBvah6714NUzYqxRmV9uBXieQUgln9mRaKCat2qEPGa5Oh42bfSEJAPOGA1eAwZYpWQqC9EfDY6vv/isTi3pFkrRiExuaRomeBMXMiWeIvvh1f00FOfPCVJE0kTxFNrq8HZswnl4c5kNg72eNpO8q+FhJkUgsagDNFFQZjVYAWbnPf0s8S/zyuNArO9uSZO/SkrYwGmN0c8xfiXo1pDLDCvEqaTr/UL9AsmRZAeNZq7nC1CVOl74+dZRSK+oW62ytthaq4uexCZtswe+3T/E lACORhXG wKy9OqXyaBQRKD/ECw3JPCV0wS/oux79SJmwHgEzi3CxIetkgHVE+4TDt7xdQTXrWqH1ggOFaWveI99y9jYWFLGu8OIEfLvqyOlnmnXocB0VLrVZXBGEa4DYJHqg7uDOCVX9ePU5f7WFn1Ij50f0ewNhXZ67dllpZeca2IPv2i2T4c7bCs5d/og0gLxTRyvEGT+aZcbqYsZogthMVTbf9kaK15Ev0R82bxFlEPR8lI1HudRRopHjsjXYKylIViweOaZDDjwv4+IZzPAFiCyhztxt7eCl5kvjekiU3 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 62d8cf33f27a..994b85e7df67 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -40,11 +40,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 Fri Aug 30 11:18:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13784988 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 33D4BCA0EFC for ; Fri, 30 Aug 2024 11:25:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D127B6B011C; Fri, 30 Aug 2024 07:25:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CBFF76B011E; Fri, 30 Aug 2024 07:25:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B3A296B011D; Fri, 30 Aug 2024 07:25:02 -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 8E0156B011B for ; Fri, 30 Aug 2024 07:25:02 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 4E4ECA173C for ; Fri, 30 Aug 2024 11:25:01 +0000 (UTC) X-FDA: 82508680002.12.729CC36 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf27.hostedemail.com (Postfix) with ESMTP id B354C40014 for ; Fri, 30 Aug 2024 11:24:58 +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=1725017009; 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=FUCeeBgVqwaeXscTtetjTtkEljm6bwsDMBXon+v1SBs=; b=JR6OqK5KGFydGOul5EocnpPx9gmFz0nVD3swiHbcr6fhldL7q8s+YkS/GTIu1N7Es2iBJc 0osgLBJDmeKTqmfq7DmPqhU6lTQoPiA4fzhMKSO3dP2M+z71Z4e+rhbhdV1GFhCwra2eM3 DvSKJV2M1o2Wk7qVZodGZtZkercUrR8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725017009; a=rsa-sha256; cv=none; b=pUQD5JPAxhAirG3xsvMk6zcRTBatWIZ37LIZJ3DUCET8juQi93CF+5KCJV2fSqbstUNrYz MBY2IiHB68p3VLHjIkl6rz7oYpXs0Y3cTjcqF57fWpYhvGtjv22xHHg/AT36/sZmLkWrVm G1hPGN94RaGc8+tawD+sMt70lMI8k7U= 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 Received: from mail.maildlp.com (unknown [172.19.162.112]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4WwG5P5C24z1j7RC; Fri, 30 Aug 2024 19:24:41 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 7945F140136; Fri, 30 Aug 2024 19:24:55 +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; Fri, 30 Aug 2024 19:24:55 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Subject: [PATCH net-next v16 10/14] mm: page_frag: introduce prepare/probe/commit API Date: Fri, 30 Aug 2024 19:18:40 +0800 Message-ID: <20240830111845.1593542-11-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240830111845.1593542-1-linyunsheng@huawei.com> References: <20240830111845.1593542-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemf200006.china.huawei.com (7.185.36.61) X-Stat-Signature: 81s8rq3igmz965fbc8qrc6uw8bc3fng7 X-Rspamd-Queue-Id: B354C40014 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1725017098-486651 X-HE-Meta: U2FsdGVkX18ffo2eAZm6ZefkqYOgt/8peTO1S1eSXnuu5fx4lsc54ZfCBPkWoihoi/dQNUFZLmMjHTCoq9LzfR/6jk4skmIYXGPf5DK8ZmjzQKXREPA0yxAahsoaZjgMoT4lcJLAI7TjQo3j0nxQJ3UDwn6gUZ0JGK+5rCV6Y6aYwjUD2dy7dESbujyq9OfwH9PXhtr8s+Y7u9xY9GsVTjPDnz7vWrx/4FT95fHUOkbCBVS15utIZToExCZiHF8uvAjXHcnOmiEEIcY9BAfPzUvTA9bXHD9zhWVzXRP8iyaJRYew5RbKan2gPskYuSJySCAnUoN5nFxChEgaHuu5YTC8XlxqFKnjczfp9ukH/qqP8ConYSg4JVOTrvTk+LUCLGyhXqFQpxjYQzPbug++cKxPIpwihkhyHbRNj017gxVb1E/4LmROQSjGPYnT0aR8ikNMDsYe4siODTuIGBPJSXVx5Q6u7b2gz0kNlZGFlzhV0ggxWwULJpMMV/HCo04ZMgxE0iyqC0uptzlJzj2qiVlxnb2dS/SVHEDJ/KOk7wXb0sgGNnwP48z9T33XDhtzV/4qvSaNsV8iupCvTpUgp1wsZoYi39kSQ+DxQTqa2unbP9ctJDQPyLlIPRj3VR1gEwkTG8b1nXCfpKjTozJXNNqh1C1/2vTHVOVVnakOnzyDfzuUNihjU9UxuDA4AzF5keJxHmelSv8B0Q3yWjrHl4bwBjuQI5ORNo6jalWPaCftz0Gjr9oaZ2rFGxBCcHHTV9jUC98UFtpep8GKAly1us/CUnNZcwc9rg4RNNkWJSvqVFPXDKzpGGEE3F/u/EmaDF+ASWS2GA1ns9WByPuRYxyns0mD2J/O50oOe9IFSJ+B7k8rbvAV9+mlEhd1zhHjQtiRdgziSujH7ck8eZzKILJxoV2iVzqV1EzsCt2pNgAFxy+7J3GKNyRRsfzosfzFbRU80P03TRNn4DHZEKv ZpJbbVAK lUIt19b+yWHUMB2rTqRE4dg4fLPMsH00VvTS7TGE07Lx+85gAmbpOX1eNiTh1KF79KMeNigjpnPJhnbjqzvv/HrBUbKC3/o6WrveQSgi9FIvjp1/W1h6WZpMKAgx+2OY6BjmnoZNRY+aUxhbOFtEtwWv3BSwmfffJagVUAdI66cGnr/ifaj8cBluPSx50S6RTTVR7lyS7K7bF7WwwLkrXcD+nigICOK/U1SUSYjHFN72JxIWA+G6GTDM86ks9xoDF1CspbbYEjvHL/SkvazOtlzfLuYMmtONgUDFZ 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 f786ec572aa9..48a88ef8b5c7 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -61,6 +61,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, @@ -122,6 +127,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 Fri Aug 30 11:18:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13784989 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 E8F33CA0FE0 for ; Fri, 30 Aug 2024 11:25:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5959A6B011D; Fri, 30 Aug 2024 07:25:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 546CB6B011E; Fri, 30 Aug 2024 07:25:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 349BC6B011F; Fri, 30 Aug 2024 07:25:03 -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 1817E6B011D for ; Fri, 30 Aug 2024 07:25:03 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C0AAF41830 for ; Fri, 30 Aug 2024 11:25:02 +0000 (UTC) X-FDA: 82508680044.14.6FCC4F4 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf03.hostedemail.com (Postfix) with ESMTP id 614A72000C for ; Fri, 30 Aug 2024 11:24:59 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf03.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=1725017034; a=rsa-sha256; cv=none; b=omm7u/C8LFEF/VkrAXxPRR4H2Ry2FKrCzfiNYfUfDdEWNC+JQZNfO5e4ZRyKozQC8uRHn4 ryDMHOEFFNxn9HQpy6J0J7TJ6MyIb0+yxp/F4fUcljMecl7mnSLW8JmvEtqfUNfFvTbbNO T73kCSJd/xG/ptF5OQOtxVirXxE1ubs= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf03.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=1725017034; 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=o5PzVX9irsgWPVjJ9MtCi3riUjaxFx8elnQzLB+zs80=; b=hmWWUDMHZDwlGxs6oLJKbmLx7bysyRsXa8tH0/K8Yx0OSpxFp+8g/PbwEGc82YmltUghW8 SxTSvheyNK3fOm61CT5BWmjkuQ8C8banl2uSLaUJgwh1ODpsE+eOHUjT2LgBWmFuhjwCj/ yoaOR3nfRCzhNHvYg9Ee0ZWJ0ehy384= Received: from mail.maildlp.com (unknown [172.19.162.254]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4WwG052v3VzQr55; Fri, 30 Aug 2024 19:20:05 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 6D6CC180100; Fri, 30 Aug 2024 19:24:57 +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; Fri, 30 Aug 2024 19:24:57 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Andrew Morton , Shuah Khan , , Subject: [PATCH net-next v16 11/14] mm: page_frag: add testing for the newly added prepare API Date: Fri, 30 Aug 2024 19:18:41 +0800 Message-ID: <20240830111845.1593542-12-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240830111845.1593542-1-linyunsheng@huawei.com> References: <20240830111845.1593542-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspamd-Queue-Id: 614A72000C X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 59xz4dcfk7ix6z5w59pmsq1p18qt5e93 X-HE-Tag: 1725017099-309662 X-HE-Meta: U2FsdGVkX188g6IYxunaYw2e7ZUvO31u8ABr+RWfT+a8xieHfoAw6Fs8yspPzddmQ3fjxhS6nMJvZdq1arf2LOlaY+Gqz6e5N0qDwKDXZvf0eSXUbwA151PgWuz8f43+iDD4KnOn7LTEl0JN2nS2pLeBfKLCQLA72qJncPGxmnS6HsTvl9bbLTcsKCGI9PVywsXZ8aM88t785PESAN/S2Be13z1LXS4B+vpMuFSu/ux/S+hdTVQkBdkCjpIBh4v3jcXRD5riIzX1rR2Eybw9Ep/V7jFyDpVqEGNXsTmkq+zFGoUCVeRh7KI2wi61Lmd/l/pzNR6VGII0//GYJCA0coAzyQGNDomaTbc4OCV22BT+FnxxeftkBq7tvPcXW6W9tIbEWUVLgUcsZwIMu8OeRBFHETkLxmu48Hjn00GfzIGBiOWgU+wJFoqlJaJHd8h67JDJipkSABhDKfjEWnDkSiEGCwcpU9w3k3IzQqPrAAs2zZNj1VtgA8PXaxsIRv3XKB1YHyUFouKGJXnTZ0MYsaiLGPkJ0Mui9puYl5MLgbJFi24gRhPDojp3+SqAtGduHBfxc9Qte720MjkjO4b9dMlm+5tJj02mT10aMTlqKdgIV/T+uB2BoTzTPfPI09OLbwGVmXhpXJRM3S/OIIiNQxa9tdEbkcvhi81fU3aWXckuw1L0H+3az6xfMGUch5AHvEMYmwdHbg3TLE8P6taeb2Aa9yR5FxZae4FvsGgJJmOcQdDW/aVICFTLMysMLv2TLT6ViWY15M8AFyN7ZoRuz6IAjozoOFQBj8rFWmTRvBoUewSTfRvjM52Ek4xq/7zQRR0Aw0TBw7vS1zO0Twio4JHuyM9kDU4j/HXEEMkZB5wJSHoFjS4PGpFWpy8Pzzy3YcBB5sZNg8+baqQblu5+iFI0XyG0BcrMW1DKC6KwJjY8aLVTNDXsveMIoNb8A9d/86TecITnIdo6LBBs+8x mp+3RX+l XKSZsSTx0XDSK+E/WiHlxlaxTFvTVONbqTnWis4qXvdRWK4sk1C++J3IHsCDn39CH2XNcbONbXJNEwWRbxgX5kk1bqeVj7x0YFpBBkdIOkKW37dTnO/n7K/E0/V10Nrad7X1F6xb+i3D9nmh/EwajIfn/wwT74rLVAgoRV4HxN3HRKsNBDz9fiNC55tFBy3hE56zLbKA7vms/1cc/HmJWCwBVfPmoCxeS1iVXu/i+wVOW/ow/o2VnQDJkBTAqwQzMwjjH3KORD9ieRA8g+GgUbGk6ZDLjUNy0W2H1 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: Add testing for the newly added prepare API, for both aligned and non-aligned API, also probe API is also tested along with prepare API. CC: Alexander Duyck Signed-off-by: Yunsheng Lin --- .../selftests/mm/page_frag/page_frag_test.c | 66 +++++++++++++++++-- tools/testing/selftests/mm/run_vmtests.sh | 4 ++ tools/testing/selftests/mm/test_page_frag.sh | 31 +++++++++ 3 files changed, 96 insertions(+), 5 deletions(-) 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 e21a22b1d70b..856eacdd1c90 100644 --- a/tools/testing/selftests/mm/page_frag/page_frag_test.c +++ b/tools/testing/selftests/mm/page_frag/page_frag_test.c @@ -27,6 +27,10 @@ static bool test_align; module_param(test_align, bool, 0); MODULE_PARM_DESC(test_align, "use align API for testing"); +static bool test_prepare; +module_param(test_prepare, bool, 0); +MODULE_PARM_DESC(test_prepare, "use prepare 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"); @@ -67,6 +71,18 @@ static int page_frag_pop_thread(void *arg) return 0; } +static void frag_frag_test_commit(struct page_frag_cache *nc, + struct page_frag *prepare_pfrag, + struct page_frag *probe_pfrag, + unsigned int used_sz) +{ + WARN_ON_ONCE(prepare_pfrag->page != probe_pfrag->page || + prepare_pfrag->offset != probe_pfrag->offset || + prepare_pfrag->size != probe_pfrag->size); + + page_frag_commit(nc, prepare_pfrag, used_sz); +} + static int page_frag_push_thread(void *arg) { struct ptr_ring *ring = arg; @@ -80,13 +96,52 @@ static int page_frag_push_thread(void *arg) int ret; if (test_align) { - va = page_frag_alloc_align(&test_nc, test_alloc_len, - GFP_KERNEL, SMP_CACHE_BYTES); + if (test_prepare) { + struct page_frag prepare_frag, probe_frag; + void *probe_va; + + va = page_frag_alloc_refill_prepare_align(&test_nc, + test_alloc_len, + &prepare_frag, + GFP_KERNEL, + SMP_CACHE_BYTES); + + probe_va = __page_frag_alloc_refill_probe_align(&test_nc, + test_alloc_len, + &probe_frag, + -SMP_CACHE_BYTES); + WARN_ON_ONCE(va != probe_va); + + if (likely(va)) + frag_frag_test_commit(&test_nc, &prepare_frag, + &probe_frag, test_alloc_len); + } else { + va = page_frag_alloc_align(&test_nc, + 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_nc, test_alloc_len, GFP_KERNEL); + if (test_prepare) { + struct page_frag prepare_frag, probe_frag; + void *probe_va; + + va = page_frag_alloc_refill_prepare(&test_nc, test_alloc_len, + &prepare_frag, GFP_KERNEL); + + probe_va = page_frag_alloc_refill_probe(&test_nc, test_alloc_len, + &probe_frag); + + WARN_ON_ONCE(va != probe_va); + if (likely(va)) + frag_frag_test_commit(&test_nc, &prepare_frag, + &probe_frag, test_alloc_len); + } else { + va = page_frag_alloc(&test_nc, test_alloc_len, GFP_KERNEL); + } } if (!va) @@ -149,8 +204,9 @@ static int __init page_frag_test_init(void) 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); + pr_info("%d of iterations for %s %s API testing took: %lluus\n", nr_test, + test_align ? "aligned" : "non-aligned", + test_prepare ? "prepare" : "alloc", duration); ptr_ring_cleanup(&ptr_ring, NULL); page_frag_cache_drain(&test_nc); diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/selftests/mm/run_vmtests.sh index 96fd470b9f51..e4a36231bbea 100755 --- a/tools/testing/selftests/mm/run_vmtests.sh +++ b/tools/testing/selftests/mm/run_vmtests.sh @@ -464,6 +464,10 @@ CATEGORY="page_frag" run_test ./test_page_frag.sh aligned CATEGORY="page_frag" run_test ./test_page_frag.sh nonaligned +CATEGORY="page_frag" run_test ./test_page_frag.sh aligned_prepare + +CATEGORY="page_frag" run_test ./test_page_frag.sh nonaligned_prepare + echo "SUMMARY: PASS=${count_pass} SKIP=${count_skip} FAIL=${count_fail}" | tap_prefix echo "1..${count_total}" | tap_output diff --git a/tools/testing/selftests/mm/test_page_frag.sh b/tools/testing/selftests/mm/test_page_frag.sh index aad55e0ca2cd..753ec4b6fdc3 100755 --- a/tools/testing/selftests/mm/test_page_frag.sh +++ b/tools/testing/selftests/mm/test_page_frag.sh @@ -32,6 +32,8 @@ ksft_skip=4 # NONALIGNED_PARAM="test_push_cpu=$TEST_CPU_0 test_pop_cpu=$TEST_CPU_1 test_alloc_len=12 nr_test=512000000" ALIGNED_PARAM="$NONALIGNED_PARAM test_align=1" +NONALIGNED_PREPARE_PARAM="$NONALIGNED_PARAM test_prepare=1" +ALIGNED_PREPARE_PARAM="$ALIGNED_PARAM test_prepare=1" check_test_requirements() { @@ -70,6 +72,24 @@ run_aligned_check() echo "Check the kernel ring buffer to see the summary." } +run_nonaligned_prepare_check() +{ + echo "Run performance tests to evaluate how fast nonaligned prepare API is." + + insmod $DRIVER $NONALIGNED_PREPARE_PARAM > /dev/null 2>&1 + echo "Done." + echo "Ccheck the kernel ring buffer to see the summary." +} + +run_aligned_prepare_check() +{ + echo "Run performance tests to evaluate how fast aligned prepare API is." + + insmod $DRIVER $ALIGNED_PREPARE_PARAM > /dev/null 2>&1 + echo "Done." + echo "Check the kernel ring buffer to see the summary." +} + run_smoke_check() { echo "Run smoke test." @@ -82,6 +102,7 @@ run_smoke_check() usage() { echo -n "Usage: $0 [ aligned ] | [ nonaligned ] | | [ smoke ] | " + echo "[ aligned_prepare ] | [ nonaligned_prepare ] | " echo "manual parameters" echo echo "Valid tests and parameters:" @@ -102,6 +123,12 @@ usage() echo "# Performance testing for aligned alloc API" echo "$0 aligned" echo + echo "# Performance testing for nonaligned prepare API" + echo "$0 nonaligned_prepare" + echo + echo "# Performance testing for aligned prepare API" + echo "$0 aligned_prepare" + echo exit 0 } @@ -155,6 +182,10 @@ function run_test() run_nonaligned_check elif [[ "$1" = "aligned" ]]; then run_aligned_check + elif [[ "$1" = "nonaligned_prepare" ]]; then + run_nonaligned_prepare_check + elif [[ "$1" = "aligned_prepare" ]]; then + run_aligned_prepare_check else run_manual_check $@ fi From patchwork Fri Aug 30 11:18:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunsheng Lin X-Patchwork-Id: 13784990 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 A07B2CA0EFC for ; Fri, 30 Aug 2024 11:25:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2FD6B6B0120; Fri, 30 Aug 2024 07:25:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2ACBA6B0121; Fri, 30 Aug 2024 07:25:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 14DD36B0122; Fri, 30 Aug 2024 07:25:12 -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 E58016B0120 for ; Fri, 30 Aug 2024 07:25:11 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id AE29B4184E for ; Fri, 30 Aug 2024 11:25:11 +0000 (UTC) X-FDA: 82508680422.08.66A16C4 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf05.hostedemail.com (Postfix) with ESMTP id 23CA2100018 for ; Fri, 30 Aug 2024 11:25:08 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=none; spf=pass (imf05.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725017088; 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=R2NRlA0Guw6KnQO7XMX8uFYumMpbZVgueBPhZcMigq4=; b=tYxqvtRmmYXG4XG6zABBX6mQdksZRLMHfq+/2e+N0nJ/3zDJ4aTgjFtg1lQTUZgKbwdZjc d+G2fztKKOpxD2vAKSnw0DOznh8yZeIC08dJN+ZP4LbLtvhfNE+eURQ5jFTJBK5CYcXt8h /aw7sC2cJFpiw1CX0RanCa3XvH3lv/U= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; spf=pass (imf05.hostedemail.com: domain of linyunsheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linyunsheng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725017088; a=rsa-sha256; cv=none; b=HNibt9WucaVZCAf+o4cKYGaFfa3rlPJ9DfGrgTze50QUl9Ap2CaEIpEyxLeFIzsRuPQSxE dmr2AAS63FH3SN4ZV0DksbgPKv96rPMlcE9Wmy1M4/ujC1PaJ+BSy1DRqahv+tviuU6SQT zCm+BHmEbbHErBzPxooJsGVsAYaLb3c= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4WwG4v2d6gzyQqH; Fri, 30 Aug 2024 19:24:15 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 7D4A7140258; Fri, 30 Aug 2024 19:25:06 +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; Fri, 30 Aug 2024 19:25:06 +0800 From: Yunsheng Lin To: , , CC: , , Yunsheng Lin , Alexander Duyck , Jonathan Corbet , Andrew Morton , , Subject: [PATCH net-next v16 13/14] mm: page_frag: update documentation for page_frag Date: Fri, 30 Aug 2024 19:18:43 +0800 Message-ID: <20240830111845.1593542-14-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20240830111845.1593542-1-linyunsheng@huawei.com> References: <20240830111845.1593542-1-linyunsheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.30.45] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemf200006.china.huawei.com (7.185.36.61) X-Rspam-User: X-Stat-Signature: aireimznbpfkegjke6arhgn8yopaingy X-Rspamd-Queue-Id: 23CA2100018 X-Rspamd-Server: rspam11 X-HE-Tag: 1725017108-508546 X-HE-Meta: U2FsdGVkX1+n6VHY3uDQDgT9GYKEahKYVP45SoD+e3aUE0MHYaqxTQDO+0BVmbkuENTDJ0qX0Wqq6OFwcKKuKUSLbNwgQT5ddVtZzsg7+kIfLq20fLMFMilVqNwky8Gzf1JmSLRDvJeju/mRi173xFoPrCB/XF6xpotfz6QQ0N681ZFS7DxyShG3kmzx762Mtj7amM8qo+XVUmKAh1z8kn2AlB/evuvxvkEZ0Iogu6Yp3iJtkBS7ylPoDCoCMxHrmZmaBUX/7Di45zd1JupyNCVC0B6qtWuqc6c/LYTnn9sXrS3gxFUP89hc20FbxttFeCPigHJMxi4EDAadhPEoVyVE1LJ+GqP7xswfX4uKYYPSieeTE1pDo8n4rPt40eEeiKw4lYkHk6M5oSUVsZeJfaxSrOpK4+gMqtWm+8y30XpuwPb2ldS80+Qf2nNhUfO1MkEce8u2aGIVD4TvhnMAIiAgHk1ksbA1vwAyoBfhWSfBQq8edZRbSn/0bvUs2xC/uu5Y3OLWw7K0xFsM/Aqe1tRkRiL0jxzL6NG0zndUHwhcN7Iwe+V9i2kQAISqMEpTPfAKDKsyTWIYf5hQBSCBC5RU7++Zaf1FHetIREtiHK9VN/jAaftHkWCwHF48blvaPxU5ctblqEThsgH5TlN6FXvSfcTw09Nxd57r0i90oZcKMb7v5V2c/ZRLheh/PNPs+FsCi15LBOGJ3P3oqsM0fH1MUJBhUj8mgo4BfmGIGtv9hXKuIn/3KKS6F7paNrKnlnKKQhFFZP+0AOuOhUtMnYEbmR/CLRgp21t9AGeC0JHbl1uZbe5oyPpivDV9t3MaLu5L36VZ9OgPWVIPMGMtqHxnFKBTRbOIKWgqURVVMBAxNNFYJJzXno/D7u5v2WUlcw2lkRivQdXoSt237sFemJewfyzbMYbDEibnNuVg7dMirIyyKYGrw5cGsdTsy/vQWAvUJ9qWQvtm2xwQNO+ owN5TDVJ 22VdSpzJRemswHYdJAs19yM62qEBnFilogYoM2DDfLz8Fxx+jYnj2GxA1IR+/p0oVZiNTgaL40xmH85B+MFtgYEH2eXA9DteFKXx0fpbe1BIay2svJNrcmWrguZJT0a7ivBwULJkqznxEmz9jWWH/YY6z5+/zzGNQmt5Q5nr0WCFRZYZuoOHvqkaTBKujYRViA+OtFG9Bq0FLzuDKSfnPmTrRoblQPhX7duSk5j4g9QovrAiK6mFHwVxpl6S/uKg07Zl7VfeKlMR55RL7b3NcPrX0q1NDBmpBf52e 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..e4950b8d8705 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_abort(nc, size); + 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 48a88ef8b5c7..5cca3f5ef1c5 100644 --- a/include/linux/page_frag_cache.h +++ b/include/linux/page_frag_cache.h @@ -46,11 +46,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); @@ -61,6 +78,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; @@ -98,6 +125,19 @@ static inline void __page_frag_cache_commit(struct page_frag_cache *nc, nc->offset = committed_offset; } +/** + * __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) { @@ -113,6 +153,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) @@ -121,12 +174,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) @@ -138,6 +215,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) @@ -146,12 +237,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, @@ -161,6 +278,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, @@ -171,6 +303,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, @@ -179,6 +323,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, @@ -188,6 +346,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, @@ -198,6 +371,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, @@ -206,6 +392,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, @@ -226,6 +426,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) @@ -233,6 +445,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) @@ -240,18 +463,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 994b85e7df67..6d236e8f6f33 100644 --- a/mm/page_frag_cache.c +++ b/mm/page_frag_cache.c @@ -59,6 +59,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) @@ -150,8 +154,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) {