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 D39FAD1358B for ; Mon, 28 Oct 2024 01:22:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 42C746B0088; Sun, 27 Oct 2024 21:22:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3DBE16B0089; Sun, 27 Oct 2024 21:22:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 27C8E6B008A; Sun, 27 Oct 2024 21:22:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0B5816B0088 for ; Sun, 27 Oct 2024 21:22:16 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 91BEAADE59 for ; Mon, 28 Oct 2024 01:21:31 +0000 (UTC) X-FDA: 82721258718.20.E2DD5F1 Received: from mail-ed1-f42.google.com (mail-ed1-f42.google.com [209.85.208.42]) by imf18.hostedemail.com (Postfix) with ESMTP id ADFA11C000A for ; Mon, 28 Oct 2024 01:22:03 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AA68PdQt; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf18.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.208.42 as permitted sender) smtp.mailfrom=ioworker0@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730078405; a=rsa-sha256; cv=none; b=j5PPifQz/WD5Ihn6I7nb8IJcSfBDbsQ06jzdYrhZtLZZ+0XU8bO9rA+FB3qP/tf7uScnj7 D2B3ueMVx6b6PjCqQMGgqUlslDs+wJX2aSicpgpinFbrdDqDx+NWIw89mGfyLb3uE5eoIm WBBxjmmVxa7VnLQcTQuFhgtx6N1mUtw= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AA68PdQt; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf18.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.208.42 as permitted sender) smtp.mailfrom=ioworker0@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730078405; 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=a6f/AWr5zkxqWz29d7KMm4yvU9ly5dS9dQi/PANUNoQ=; b=uR6q9U6hFDQuZHTI39mtHLenYAq5WyqkY3z/A3m01PCzKVOszL/rYGI9heKYoea7SBEs1e yzCBGWA2nVjYzbapg0jyaHYofY1+V2x1M+M48hCBOhL6SKy1mfQNahSHBrAS0nHZERKY4Q wBgw4/8cq0y5MD9YpNTsGqOUtuHOLxE= Received: by mail-ed1-f42.google.com with SMTP id 4fb4d7f45d1cf-5c9c28c1e63so4560785a12.0 for ; Sun, 27 Oct 2024 18:22:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730078532; x=1730683332; 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=a6f/AWr5zkxqWz29d7KMm4yvU9ly5dS9dQi/PANUNoQ=; b=AA68PdQt2ZHRmBp1iBkfGqW8nCk+6NI9xhQDu70vRLkvgnk4uQ6h9Tto/FObo/EUSi VVlhLX5+l+ziD/id1Ah4wavSvViXn9VNkSi3il/iqVxX4h179SynF5kcr9N2b277VcYi n7oquPMMWECHfG4O9FdrZldOoW5h+RD7dMwIv2sb/awT6aqbiqbeL6AXbl93LYKtPuwZ go0AC7NOF4UCiCQ3K9BgTg6dDPLtIyfRxudkRu52dwuduMAUMgoEgdgG+zUpxr0avqvy QcPgDovEkdON91Jo1PIV6ypXmtpm7scitxhHivkybI2tdkhXvZq6bmNAxoQuYNO/5emj 49ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730078532; x=1730683332; 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=a6f/AWr5zkxqWz29d7KMm4yvU9ly5dS9dQi/PANUNoQ=; b=uWkkgGOPzTVGMxJPfU1WcuLwkmiwE2uKSrHEghqOgQPHSOduFM/mAtkj+9bCHg2DFi fbR5i/2Z2JdvcM5FgWhJJ1itpibh2W9xyPCGVgHZZyKswaIzAW75wm+kxWpU6aCmloqs BmNqjNDA4JS1uTmXwWrc0VVRVXXgZyz+z3seqwpYu4GfTQKpRvQO0R/zz7NheYcQ9CLx 2v9O/Vh9InfhshJMaaXrJ69lhJ6dRZvLLFWFRGKS+UbyWRa+emkWmBez46anBiUvRgz4 4aX1i8UY0Pd9iNz0niZT8A1wgE8Iaz7/QPImuJ0SkhwbkhVxCEajeFnXce5zyJ8NEuDw O1/A== X-Forwarded-Encrypted: i=1; AJvYcCVY/c5xmF0BCKr0eWzcjt5ya6gCruvHYWSsXhYAlZ45KMREJuoUSKRPORMDXGWl76LjE/m5h1vgOw==@kvack.org X-Gm-Message-State: AOJu0YzZhq8YWWUEC9ViFicbKAU0T+edU+gwr587EruF62F5PGR3zUBt kL4rPm9Lb/N+Ym8J3BflOiuaoyhqmKq/1yl8MB69m7d9yTZX1opk0M1I5++UCGj3Vzfune5rpcz g9Y99JXxSLYMSlJfDMKHMeyiltJ0= X-Google-Smtp-Source: AGHT+IGHIspl0PUcPKyvAKfduJM0LgJ3OcZ24A+1rCnVANY0jKnUh36q4516dA33L2ENFJ94s6JdhUhSiEdZvRPlhYc= X-Received: by 2002:a05:6402:2803:b0:5c9:6ae4:332e with SMTP id 4fb4d7f45d1cf-5cbbf8b11ffmr5063792a12.8.1730078531832; Sun, 27 Oct 2024 18:22:11 -0700 (PDT) MIME-Version: 1.0 References: <20241027175743.1056710-1-mcanal@igalia.com> <20241027175743.1056710-4-mcanal@igalia.com> In-Reply-To: From: Lance Yang Date: Mon, 28 Oct 2024 09:21:35 +0800 Message-ID: Subject: Re: [PATCH 3/3] mm: shmem: override mTHP shmem default with a kernel parameter To: Barry Song <21cnbao@gmail.com> Cc: =?UTF-8?B?TWHDrXJhIENhbmFs?= , Jonathan Corbet , Andrew Morton , Hugh Dickins , David Hildenbrand , Ryan Roberts , Baolin Wang , 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: rspam12 X-Rspamd-Queue-Id: ADFA11C000A X-Stat-Signature: nzkrntastwejeuuuj4p1ruz6dudog934 X-Rspam-User: X-HE-Tag: 1730078523-449706 X-HE-Meta: U2FsdGVkX18rDXXlWiilLX2DnwOjXW1CGQb4fap/XTmRonN8HlzHpy4X45vyV9qOLM6gXBThtr+lUy2BZPacPzTwRdTIyIGaV75bvqWUPfqFAlVBqb+7sMObRexqi5H/ShJoQiZ0tDButNG1Xho9Lnm+R8s/EFpc1IcjdT1gQU4sM5NapoM17GLEY8CLPefxHI5tNkOH21542bnfehZRuJDPXTTPgmqM/S6OaQedjcAjAFwIUQRZiyW8LPUMrlLtI0Oh5nDadeDFhlOha6kRTL7M2PqAGDrYTGgiSCVXkS3y8KQWbA8aa2PxQ5LQdIn4CuqTmMbNI7Wrn/A2k4so2ozqDkpS44eranfrYwj2oE43GJMOeqCq3Jq0hK2plPo+MR+FTmNFKRPwKE1SNnyNMVNizqi+CvEoUGBX7Q/oQz06kj/wFcEsRAy4KBR45Er7gpGDswd2LZg5Ln4FirkixP0dBF9uICC50hBaTdAtCDx5/dDn50NMurQN9HNoUNFrNhdwuuaNaXPiOOtKIXP35sBE1euHDyf5SNhh5keXvssLkih8nIA4mVwqoqLqS07/HkwK9cmT/5ZyTiKGSYu3W/rWKPBTxWZtLxbke/x3drfXrzovuLG9YB8Js9jE8Nwh0fUbnJRPCGtJ+YiMgu6E0AJj1hLvMAWQFe/6lS5BKkEASJZln+mOiq8RS4CUGomS0p0ODcJSz1gOg085Dz0Qo2Vga9ROmsMsl+7cr3/ml0ytgz1ENIU9wM2pCkQD7F8E3FC7f6yMiDYBDPXKJhNN+8omdYFHRoFZ/v4K3eghGCTlQKiy+wRT2naKLPON0uu/txkK6fvaFwxGQQ5PzxACrr5B16FzOnDStvYeAYxvzbcA21sREUexa6xeRMB9YT4OGUns05PVcrC1kc5xB+6rVk0WedVONPno5+iQb2aEHd6dqaoBb0Uo2oGoVuohncguPzTouubHS5wrjrBLZsH X1wYV64A 9ukDAfTSZu4eLfTHnFzG8go7lXHN6MSB5NXV24Cw4xj0DBshA+a2DExKyZWe/uQgvtjHGk0tOetBB+k1y8/NsHNgFmt5cNKTULbqRL255GImr5Bd+WizEathwFFe2nnVPrPUp4sz6A7xTco8xejH0bNZnIIAPX7Ahmu3GZ/JBtNUSzRFF7WfbdBhJjIZbq/yN+WJhIEL5J1j3E1BqlazUrXywnL673XqIx7J4ydn3EpB6BLNQQ3HDYdYDflbyfn5jOahhMoRGwdfHPkmnMX1bWt9eG5ffR8B4xwqKDC+i+/LnisFv4v/3zfXE2X0t6NxylXbJLPox6b8CwJ0p+NcMrLqOHDRJo/tx8SJyVAgrX3VVvFGUoKi6sbXTNwWEOZsbmytrneMvv1ZaS+xyeXpR0UwrSg== 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: Hi Ma=C3=ADra, On Mon, Oct 28, 2024 at 5:54=E2=80=AFAM Barry Song <21cnbao@gmail.com> wrot= e: > > 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 parame= ter > > accepts the following format: > > > > thp_shmem=3D[KMG],[KMG]:;[KMG]-[KMG]: > > > > For example, > > > > thp_shmem=3D16K-64K:always;128K,512K:inherit;256K:advise;1M-2M:never;4M= -8M:within_size > > > > By configuring the default policy of several shmem huge pages, the user > > 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/Document= ation/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 excep= t those > > marked explicitly IRQF_NO_THREAD. > > > > + shmem_anon=3D [KNL] > > + Format: [KMG],[KMG]:;= [KMG]-[KMG]: > > + Control the default policy of each hugepage siz= e for the > > + internal shmem mount. is one of polici= es available > > + for the shmem mount ("always", "inherit", "neve= r", "within_size", > > + and "advise"). > > + It can be used multiple times for multiple shme= m 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"? +1 IHMO, it seems like 'thp_shmem' would be better, as it appears to fit well with 'thp_anon' in naming style ;) Thanks, Lance > > > + > > topology=3D [S390,EARLY] > > Format: {off | on} > > Specify if the kernel should make use of the cp= u > > diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation= /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``, ``advise`= `, > > ``never``, ``deny``, and ``force``). > > > > +In the same manner as ``thp_anon`` controls each supported anonymous T= HP > > +size, ``thp_shmem`` controls each supported shmem THP size. ``thp_shme= m`` > > +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 shmem= THP > > +sizes not explicitly configured on the command line are implicitly set= 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 will > > +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 __rea= d_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 setti= ng 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_attr= =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_st= r); > > + 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_shm= em(char *str) > > } > > __setup("transparent_hugepage_shmem=3D", setup_transparent_hugepage_sh= mem); > > > > +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_size= ); > > + end =3D get_order_from_str(end_size); > > + } else { > > + start =3D end =3D get_order_from_str(su= btoken); > > + } > > + > > + 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 setting= \n", str); > > + return 0; > > +} > > Can we share source code with thp_anon since there's a lot of duplication= ? > > > +__setup("thp_shmem=3D", setup_thp_shmem); > > + > > #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ > > > > #else /* !CONFIG_SHMEM */ > > -- > > 2.46.2 > > > > Thanks > barry