From mboxrd@z Thu Jan 1 00:00:00 1970 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 C16B5D13588 for ; Mon, 28 Oct 2024 11:09:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3EA156B0083; Mon, 28 Oct 2024 07:09:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 399E56B0085; Mon, 28 Oct 2024 07:09:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 239BC6B0088; Mon, 28 Oct 2024 07:09:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 06FC36B0083 for ; Mon, 28 Oct 2024 07:09:52 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5E652408B4 for ; Mon, 28 Oct 2024 11:09:39 +0000 (UTC) X-FDA: 82722740016.27.EB1D3D5 Received: from mail-vk1-f172.google.com (mail-vk1-f172.google.com [209.85.221.172]) by imf13.hostedemail.com (Postfix) with ESMTP id AAFD02000F for ; Mon, 28 Oct 2024 11:09:25 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=edQlSGG2; spf=pass (imf13.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.221.172 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730113710; 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:dkim-signature; bh=6DLP4K9pPqTa9mg1HyxWl56HscCu6UOOsW3HKODy+ow=; b=fyXD78sFvmoMXBzVq7tBwSHBw4vGgTGc9e8vNviDVfwICqObpxzTEQFbH2YxzcJPpQ5sU5 sMuS6nTFyoqVb+Gi6ikpR+PbO1Y/65gbogeD7QBvJM6zczqx/5Q0SiIQCkU9aYxj0l8Lmp itAPIzIKg/330jNDCg827i0VtQPXQ24= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=edQlSGG2; spf=pass (imf13.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.221.172 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730113710; a=rsa-sha256; cv=none; b=nyNHv8d9+3uUm0buKtcKfr+S2irRBQalLwANlU31XPC4iX+hT6SKPl++sJo45C3rHC1OyW yE79yrKrQv9mQoCL9CBiKTRjJDb9Gh6Zm9UAMRjb/3lyFggesV06r7aIuVr03mD4A99V1N iV8Vf18vMnTi2pwAdp2nTMrH3RF32Sg= Received: by mail-vk1-f172.google.com with SMTP id 71dfb90a1353d-50d431b0ae2so2776365e0c.1 for ; Mon, 28 Oct 2024 04:09:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730113789; x=1730718589; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=6DLP4K9pPqTa9mg1HyxWl56HscCu6UOOsW3HKODy+ow=; b=edQlSGG2S9uHGcaa0+XRzuNVAfKa7NsGyrxi03S/Oy4fs3nWFEgCShOaSXFFt1vbUi 7TmLm/DAbJSXfr8mSPH5DyGzhrbMMqbOFcNSGMkGmzd+yH8VFAUEUKMx7hIqymyIIwbI k0t77MIadp/uD8oKxq96xMlFBMsnZBg+LFSgl/OGcOVMuZqi13VuPkhNiSl9M4skZR4m SW78y9eyRiLmSMq6GxfjchUcGcDZFzCNmX1p1SyXKK7kQ0sGfsGepA9dkNymdCZh2KIM Iia0w7IIrDfI/rvxKYJcHgM5MMgU33ARTnGkyi7YZ7ONpA79IS9yqCLk9GqnMctGgNqC HRoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730113789; x=1730718589; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6DLP4K9pPqTa9mg1HyxWl56HscCu6UOOsW3HKODy+ow=; b=dUS6RFeSLNsSNb49faS2ctFT0DRr+xkb/TxYucMMN1pC/WyBecMLnhxy/tBfiPg99g kPMj8KriIkMismRvx7GDqPVfA/rRcIcmijZ5Hur6Xe9/SPOlOt5qCkqL59ic+Yx/lIAx 2RubWNRoyvuEFb6UhCAsxD+EfXPlbFlEGTuQtY/9rTznZBZ4SrIsqcg/o9a0S1VT++SW AbzbS8OElKQJcFC+ShCNrlkuoEY3H6fbCVuuFGpm6L2nOsEM+kup64h7Qt0nKGZjAcoF 1SLdaF3M/6H8lHjpfnGwnvV64TKKAfii7Nxx14vQbbHtJOCHfvsU9UiuuO+vb56t8TXo GHow== X-Forwarded-Encrypted: i=1; AJvYcCUFE4Pfa8kjPrxX3VjejyHPedLv/h2gio8tT+Vb71FSc1U5yTqI3fN6fbH6CK6Unbhay+WqScoE4Q==@kvack.org X-Gm-Message-State: AOJu0YzTQS4T55qOC9qroj6AL1UnDa1gHcznDLpDhSLLkMREKLy8uFKA GmJj209MVC4MP2qTcVCgOG8Pi59Q3oOkQo69czT8k8NpJo4B3JbQfj89XrTGd4ov0s6+kmVv4ok GdgffOCOPICdelsIBdNUWoJhUpro= X-Google-Smtp-Source: AGHT+IFTG/DHIoyWwfm3i/7TVknzqpD9DN97MwLJKo6kniJxde0K916lgZsLVYrQTYhIIfNvhqs3HFHATappFb845l8= X-Received: by 2002:a05:6122:7cc:b0:50c:ef20:6bed with SMTP id 71dfb90a1353d-51013858e9cmr4429271e0c.3.1730113788620; Mon, 28 Oct 2024 04:09:48 -0700 (PDT) MIME-Version: 1.0 References: <20241027175743.1056710-1-mcanal@igalia.com> <20241027175743.1056710-4-mcanal@igalia.com> <2505d52c-3454-4892-8c90-e3d9b2f0c84f@igalia.com> In-Reply-To: <2505d52c-3454-4892-8c90-e3d9b2f0c84f@igalia.com> From: Barry Song <21cnbao@gmail.com> Date: Mon, 28 Oct 2024 19:09:37 +0800 Message-ID: Subject: Re: [PATCH 3/3] mm: shmem: override mTHP shmem default with a kernel parameter To: =?UTF-8?B?TWHDrXJhIENhbmFs?= Cc: Jonathan Corbet , Andrew Morton , Hugh Dickins , David Hildenbrand , Ryan Roberts , Baolin Wang , Lance Yang , linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-dev@igalia.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: AAFD02000F X-Stat-Signature: mphi44p4j8x9n5zfrauf9md4e5u86w34 X-HE-Tag: 1730113765-373903 X-HE-Meta: U2FsdGVkX18F7R2Sj7s5pX++VItmtXtlV7rv3uBYn7LkhiJoZzIx+h9MLHOpx9uNUp95zIcXsCXtOgYHNG6SvMEI8OyfH6wRudX0D9IVEQT0NMwP5f+WpTyM/LEQJF7ZuAdLtUr3pFsb4g4zDtOUTRg7lyMfJdt+oHqQn9ltXpr0f3j4OJrM0pQlpdZzJIQc1RGKdLNtlPXXY/uCX2fOQVPAoldHd3bsvt7AJeYGW9Z5lrwmmOFSFjQqC6lecs21nsU1PsI5wIFNosTnp8snpD3LKHLmoygs9VwV+sE3CBtLLJYgh6CMghpadXWTr33dB7lAMsBMjRBkRtEt8h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version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Mon, Oct 28, 2024 at 6:10=E2=80=AFPM Ma=C3=ADra Canal wrote: > > Hi Barry, > > On 27/10/24 18:54, Barry Song wrote: > > On Mon, Oct 28, 2024 at 6:58=E2=80=AFAM Ma=C3=ADra Canal wrote: > >> > >> Add the ``thp_shmem=3D`` kernel command line to allow specifying the > >> default policy of each supported shmem hugepage size. The kernel param= eter > >> accepts the following format: > >> > >> thp_shmem=3D[KMG],[KMG]:;[KMG]-[KMG]:<= policy> > >> > >> For example, > >> > >> thp_shmem=3D16K-64K:always;128K,512K:inherit;256K:advise;1M-2M:never;4= M-8M:within_size > >> > >> By configuring the default policy of several shmem huge pages, the use= r > >> can take advantage of mTHP before it's been configured through sysfs. > >> > >> Signed-off-by: Ma=C3=ADra Canal > >> --- > >> .../admin-guide/kernel-parameters.txt | 10 ++ > >> Documentation/admin-guide/mm/transhuge.rst | 17 +++ > >> mm/shmem.c | 109 ++++++++++++++++= +- > >> 3 files changed, 135 insertions(+), 1 deletion(-) > >> > > > > Hi Ma=C3=ADra, > > > >> diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documen= tation/admin-guide/kernel-parameters.txt > >> index acabb04d0dd4..595fa096e28b 100644 > >> --- a/Documentation/admin-guide/kernel-parameters.txt > >> +++ b/Documentation/admin-guide/kernel-parameters.txt > >> @@ -6700,6 +6700,16 @@ > >> Force threading of all interrupt handlers exc= ept those > >> marked explicitly IRQF_NO_THREAD. > >> > >> + shmem_anon=3D [KNL] > >> + Format: [KMG],[KMG]:;[KMG]-[KMG]: > >> + Control the default policy of each hugepage si= ze for the > >> + internal shmem mount. is one of polic= ies available > >> + for the shmem mount ("always", "inherit", "nev= er", "within_size", > >> + and "advise"). > >> + It can be used multiple times for multiple shm= em THP sizes. > >> + See Documentation/admin-guide/mm/transhuge.rst= for more > >> + details. > > > > I'm not sure this is the right name. How about "thp_shmem"? > > Oops, sorry about that. > > > > >> + > >> topology=3D [S390,EARLY] > >> Format: {off | on} > >> Specify if the kernel should make use of the = cpu > >> diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentatio= n/admin-guide/mm/transhuge.rst > >> index 9b5b02c4d1ab..47e7fc30e22d 100644 > >> --- a/Documentation/admin-guide/mm/transhuge.rst > >> +++ b/Documentation/admin-guide/mm/transhuge.rst > >> @@ -332,6 +332,23 @@ allocation policy for the internal shmem mount by= using the kernel parameter > >> seven valid policies for shmem (``always``, ``within_size``, ``advis= e``, > >> ``never``, ``deny``, and ``force``). > >> > >> +In the same manner as ``thp_anon`` controls each supported anonymous = THP > >> +size, ``thp_shmem`` controls each supported shmem THP size. ``thp_shm= em`` > >> +has the same format as ``thp_anon``, but also supports the policy > >> +``within_size``. > >> + > >> +``thp_shmem=3D`` may be specified multiple times to configure all THP= sizes > >> +as required. If ``thp_shmem=3D`` is specified at least once, any shme= m THP > >> +sizes not explicitly configured on the command line are implicitly se= t to > >> +``never``. > >> + > >> +``transparent_hugepage_shmem`` setting only affects the global toggle= . If > >> +``thp_shmem`` is not specified, PMD_ORDER hugepage will default to > >> +``inherit``. However, if a valid ``thp_shmem`` setting is provided by= the > >> +user, the PMD_ORDER hugepage policy will be overridden. If the policy= for > >> +PMD_ORDER is not defined within a valid ``thp_shmem``, its policy wil= l > >> +default to ``never``. > >> + > >> Hugepages in tmpfs/shmem > >> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D > >> > >> diff --git a/mm/shmem.c b/mm/shmem.c > >> index 24cdeafd8260..0a7a7d04f725 100644 > >> --- a/mm/shmem.c > >> +++ b/mm/shmem.c > >> @@ -136,6 +136,7 @@ static unsigned long huge_shmem_orders_always __re= ad_mostly; > >> static unsigned long huge_shmem_orders_madvise __read_mostly; > >> static unsigned long huge_shmem_orders_inherit __read_mostly; > >> static unsigned long huge_shmem_orders_within_size __read_mostly; > >> +static bool shmem_orders_configured __initdata; > >> #endif > >> > >> #ifdef CONFIG_TMPFS > >> @@ -5013,7 +5014,8 @@ void __init shmem_init(void) > >> * Default to setting PMD-sized THP to inherit the global set= ting and > >> * disable all other multi-size THPs. > >> */ > >> - huge_shmem_orders_inherit =3D BIT(HPAGE_PMD_ORDER); > >> + if (!shmem_orders_configured) > >> + huge_shmem_orders_inherit =3D BIT(HPAGE_PMD_ORDER); > >> #endif > >> return; > >> > >> @@ -5174,6 +5176,26 @@ struct kobj_attribute thpsize_shmem_enabled_att= r =3D > >> > >> #if defined(CONFIG_TRANSPARENT_HUGEPAGE) > >> > >> +static inline int get_order_from_str(const char *size_str) > >> +{ > >> + unsigned long size; > >> + char *endptr; > >> + int order; > >> + > >> + size =3D memparse(size_str, &endptr); > >> + > >> + if (!is_power_of_2(size)) > >> + goto err; > >> + order =3D get_order(size); > >> + if (BIT(order) & ~THP_ORDERS_ALL_FILE_DEFAULT) > >> + goto err; > >> + > >> + return order; > >> +err: > >> + pr_err("invalid size %s in thp_shmem boot parameter\n", size_s= tr); > >> + return -EINVAL; > >> +} > >> + > >> static int __init setup_transparent_hugepage_shmem(char *str) > >> { > >> int huge, ret =3D 0; > >> @@ -5206,6 +5228,91 @@ static int __init setup_transparent_hugepage_sh= mem(char *str) > >> } > >> __setup("transparent_hugepage_shmem=3D", setup_transparent_hugepage_= shmem); > >> > >> +static char str_dup[PAGE_SIZE] __initdata; > >> +static int __init setup_thp_shmem(char *str) > >> +{ > >> + char *token, *range, *policy, *subtoken; > >> + unsigned long always, inherit, madvise, within_size; > >> + char *start_size, *end_size; > >> + int start, end, nr; > >> + char *p; > >> + > >> + if (!str || strlen(str) + 1 > PAGE_SIZE) > >> + goto err; > >> + strcpy(str_dup, str); > >> + > >> + always =3D huge_shmem_orders_always; > >> + inherit =3D huge_shmem_orders_inherit; > >> + madvise =3D huge_shmem_orders_madvise; > >> + within_size =3D huge_shmem_orders_within_size; > >> + p =3D str_dup; > >> + while ((token =3D strsep(&p, ";")) !=3D NULL) { > >> + range =3D strsep(&token, ":"); > >> + policy =3D token; > >> + > >> + if (!policy) > >> + goto err; > >> + > >> + while ((subtoken =3D strsep(&range, ",")) !=3D NULL) { > >> + if (strchr(subtoken, '-')) { > >> + start_size =3D strsep(&subtoken, "-"); > >> + end_size =3D subtoken; > >> + > >> + start =3D get_order_from_str(start_siz= e); > >> + end =3D get_order_from_str(end_size); > >> + } else { > >> + start =3D end =3D get_order_from_str(s= ubtoken); > >> + } > >> + > >> + if (start < 0 || end < 0 || start > end) > >> + goto err; > >> + > >> + nr =3D end - start + 1; > >> + if (!strcmp(policy, "always")) { > >> + bitmap_set(&always, start, nr); > >> + bitmap_clear(&inherit, start, nr); > >> + bitmap_clear(&madvise, start, nr); > >> + bitmap_clear(&within_size, start, nr); > >> + } else if (!strcmp(policy, "advise")) { > >> + bitmap_set(&madvise, start, nr); > >> + bitmap_clear(&inherit, start, nr); > >> + bitmap_clear(&always, start, nr); > >> + bitmap_clear(&within_size, start, nr); > >> + } else if (!strcmp(policy, "inherit")) { > >> + bitmap_set(&inherit, start, nr); > >> + bitmap_clear(&madvise, start, nr); > >> + bitmap_clear(&always, start, nr); > >> + bitmap_clear(&within_size, start, nr); > >> + } else if (!strcmp(policy, "within_size")) { > >> + bitmap_set(&within_size, start, nr); > >> + bitmap_clear(&inherit, start, nr); > >> + bitmap_clear(&madvise, start, nr); > >> + bitmap_clear(&always, start, nr); > >> + } else if (!strcmp(policy, "never")) { > >> + bitmap_clear(&inherit, start, nr); > >> + bitmap_clear(&madvise, start, nr); > >> + bitmap_clear(&always, start, nr); > >> + bitmap_clear(&within_size, start, nr); > >> + } else { > >> + pr_err("invalid policy %s in thp_shmem= boot parameter\n", policy); > >> + goto err; > >> + } > >> + } > >> + } > >> + > >> + huge_shmem_orders_always =3D always; > >> + huge_shmem_orders_madvise =3D madvise; > >> + huge_shmem_orders_inherit =3D inherit; > >> + huge_shmem_orders_within_size =3D within_size; > >> + shmem_orders_configured =3D true; > >> + return 1; > >> + > >> +err: > >> + pr_warn("thp_shmem=3D%s: error parsing string, ignoring settin= g\n", str); > >> + return 0; > >> +} > > > > Can we share source code with thp_anon since there's a lot of duplicati= on? > > I'm not a regular mm contributor and I'm most usually around drivers, so > I don't know exactly here I could add shared code. Should I add the > headers to "internal.h"? My comment isn't related to drivers or memory management. It's solely about avoiding code duplication. For example, we could create a shared function t= o handle both controls, reducing redundant code :-) > > Best Regards, > - Ma=C3=ADra > > > > >> +__setup("thp_shmem=3D", setup_thp_shmem); > >> + > >> #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ > >> > >> #else /* !CONFIG_SHMEM */ > >> -- > >> 2.46.2 > >> > > > > Thanks > > barry >