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 5CDC7C3DA4A for ; Wed, 14 Aug 2024 08:54:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 842896B009D; Wed, 14 Aug 2024 04:54:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7F3C56B009E; Wed, 14 Aug 2024 04:54:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 693806B009F; Wed, 14 Aug 2024 04:54:27 -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 4C0F46B009D for ; Wed, 14 Aug 2024 04:54:27 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id CB325140D40 for ; Wed, 14 Aug 2024 08:54:26 +0000 (UTC) X-FDA: 82450239732.16.2CB9576 Received: from mail-oa1-f49.google.com (mail-oa1-f49.google.com [209.85.160.49]) by imf26.hostedemail.com (Postfix) with ESMTP id D6EF9140008 for ; Wed, 14 Aug 2024 08:54:24 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=iVv8s0dx; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf26.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.160.49 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723625652; a=rsa-sha256; cv=none; b=oLcgpHPElYLiEqaMWdpoI1/JoKJyAh19LrzR3nmoCtwokjey4XOvC7wWzgm875fvjwlrba y/E+Mk/AlsF8LiZnnMoFz6kXtTFHvVuTOzD9FyitMxdtm8JMQ81SBo9nqQ9/H43SuTEpZO QGJ9+YZR46w2SxU2IBEVDN5UhjD1xz4= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=iVv8s0dx; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf26.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.160.49 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723625652; 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=pkMx4p1nJS669ykPG42HHrqlvkwWooNpild6e+mzsQs=; b=Ru65dmJe5w0u+TCk3YAsm1dQYwxBquYwj+hz+Pe6h9c8FRkzKGTXto1NH+ifZWkoLIOCDw gwqPKVyuK45Lyfl4lbf8+esdssdm9igz0rilyBAC+WRru9xJDpWQkl2oOZeWPJOWOIX+U2 4uNw0T8WP/lZzB9D3uy5iwIYYx/4wag= Received: by mail-oa1-f49.google.com with SMTP id 586e51a60fabf-26927819823so4383163fac.2 for ; Wed, 14 Aug 2024 01:54:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723625664; x=1724230464; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pkMx4p1nJS669ykPG42HHrqlvkwWooNpild6e+mzsQs=; b=iVv8s0dxCw0KgBInU59sI9L3e1sc3c7SQe1wpCzpnR/tneAS1mNOKTjGyPBn93jqJH 10MV1wz5fyAeXNEjBVaYCqb3UOGkLfnPJnXch4skoV+vZzGWninpI0ld+fpvsAt886Zd nzN4zP/Ylu5ju9zmRfsRDP+3j0YHzUr/j07r/mQX44/CDz8nJIiOibXgzGsX2S0N/4Hd ob4bQH4NBJ9Azhdq3i3whss7MTtpmpTfRS3H0p/GhT+p7mt5hpTx2IcyzNIcEjgzwt3/ hcGeYUULcCYJ2CYmYEH1PsjL/p5ToQVubqMFP0L70W/792wduRiAYQxRJA1FK5w7qo3L zM2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723625664; x=1724230464; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pkMx4p1nJS669ykPG42HHrqlvkwWooNpild6e+mzsQs=; b=JMOG0qwr7eVBPbsmjt02y3hMwey43WpKMWDudxyfQSS+SHfnFsXwXHoOdYLTsUdptf iXTS341P9JVQKHVM2J+hJxmQRows0VwGqPXGec6rMSBDXMkxGnQHuEcKyGgpulOxx5Ef 5xRjP9L87duOPLpTw/ArIs+q4uwTTwZ7SnY+IKdqY3lSHRjgfNzygvvks7me4oCuNOpm 8ALhzIC4oyv1lm9xk6kklnOAFsTrEas10eV6AKhTjt/KbXGBcivrHfwvyKi8SUbhipD2 kFTICDOAVe/8YxgsO5856ObKN//STpiIVbvzOaiR4VrP1zh8SobpCdilB9XYU+UeHdHc vQBQ== X-Forwarded-Encrypted: i=1; AJvYcCUemoJ2EcDq6lanX7iReN26b0Spps1PZUa4xl3+PSPzoHelbbgq/O1eYB1Jy5nZc8npY02/uHxMfQOIQoqs9OHut9c= X-Gm-Message-State: AOJu0Yx7iGS3yJPQ0YhnxuAU93Ks0eagsqO/tbKri+Z8kP/ML8jdZjCl 4wiRFZeU9w2G4AeXIL2W04WT/zYmrP7YwuWWYO//Kt6yzYpqASAE X-Google-Smtp-Source: AGHT+IH+GATAJY6H4OqhoorVin3vKHpP3+mvZviBR56XQ6yy+kBvmtt3OK9zcFpDUHtBWu2J4W233Q== X-Received: by 2002:a05:6870:4690:b0:25e:7a1:ea8f with SMTP id 586e51a60fabf-26fe5c65bfcmr2595496fac.47.1723625663692; Wed, 14 Aug 2024 01:54:23 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-710e5ab352fsm6918088b3a.189.2024.08.14.01.54.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 01:54:23 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: david@redhat.com Cc: akpm@linux-foundation.org, baohua@kernel.org, baolin.wang@linux.alibaba.com, corbet@lwn.net, ioworker0@gmail.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, ryan.roberts@arm.com, v-songbaohua@oppo.com Subject: Re: [PATCH v4] mm: Override mTHP "enabled" defaults at kernel cmdline Date: Wed, 14 Aug 2024 20:54:09 +1200 Message-Id: <20240814085409.124466-1-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Queue-Id: D6EF9140008 X-Rspamd-Server: rspam01 X-Stat-Signature: 8fitfmr84ppmogp77384bmnbdm34fbfj X-HE-Tag: 1723625664-415399 X-HE-Meta: U2FsdGVkX19SVIZSNX7VyWDYtRGt3P58DEz7LMVkF4qNaxFjNDUPtl4S335CHV7sB8K5vkuBf2MbZnDKW7AIU3OAenLddyR6kAhZABdj2W6/wKRQ5SVn2YSaU3CBLwwOa5/ccy0kMFMB/aUFAqeIjy7fmOjVzonaRzWa8l5YoikeWTP2AUP9U5vQurW8iNxZ5jPmkhtRkDTsB4YR1w90+Gk7LFbkBmQt537PxCS/NkNcSjupMHV4uPKXU5g5EItLZRJdzbtoVcJsO7hpFG7hbMTRiGfHJUoTETw0yrEKRqCIaHQTqxuR9KzuaRdj+EZ1Oh9qkj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18KPSvvwYQd+N146dvdtmA5wAjknQXtz3Igvfvt++rR6UTKDFzN1KTeseUr9grwpmtuTmgOy8ZfRR1Mfzk+c8V80SSLGUYPx6H9yS9cWJaTpyxkoD4Dh8BXDgsTa2FfQn2RaZ82/0LDOb2UJJXkVJnQ= 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: On Wed, Aug 14, 2024 at 8:18 PM David Hildenbrand wrote: > > On 14.08.24 04:02, Barry Song wrote: > > From: Ryan Roberts > > > > Add thp_anon= cmdline parameter to allow specifying the default > > enablement of each supported anon THP size. The parameter accepts the > > following format and can be provided multiple times to configure each > > size: > > > > thp_anon=,[KMG]:;-[KMG]: > > > > An example: > > > > thp_anon=16K-64K:always;128K,512K:inherit;256K:madvise;1M-2M:never > > > > See Documentation/admin-guide/mm/transhuge.rst for more details. > > > > Configuring the defaults at boot time is useful to allow early user > > space to take advantage of mTHP before its been configured through > > sysfs. > > > > Signed-off-by: Ryan Roberts > > Co-developed-by: Barry Song > > Signed-off-by: Barry Song > > --- > >   -v4: > >   * use bitmap APIs to set and clear bits. thanks very much for > >     David's comment! > > > >   .../admin-guide/kernel-parameters.txt         |  9 ++ > >   Documentation/admin-guide/mm/transhuge.rst    | 37 +++++-- > >   mm/huge_memory.c                              | 96 ++++++++++++++++++- > >   3 files changed, 134 insertions(+), 8 deletions(-) > > > > diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt > > index f0057bac20fb..d0d141d50638 100644 > > --- a/Documentation/admin-guide/kernel-parameters.txt > > +++ b/Documentation/admin-guide/kernel-parameters.txt > > @@ -6629,6 +6629,15 @@ > >                       : poll all this frequency > >                       0: no polling (default) > >   > > +     thp_anon=       [KNL] > > +                     Format: ,[KMG]:;-[KMG]: > > +                     state is one of "always", "madvise", "never" or "inherit". > > +                     Can be used to control the default behavior of the > > +                     system with respect to anonymous transparent hugepages. > > +                     Can be used multiple times for multiple anon THP sizes. > > +                     See Documentation/admin-guide/mm/transhuge.rst for more > > +                     details. > > + > >       threadirqs      [KNL,EARLY] > >                       Force threading of all interrupt handlers except those > >                       marked explicitly IRQF_NO_THREAD. > > diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation/admin-guide/mm/transhuge.rst > > index 7072469de8a8..528e1a19d63f 100644 > > --- a/Documentation/admin-guide/mm/transhuge.rst > > +++ b/Documentation/admin-guide/mm/transhuge.rst > > @@ -284,13 +284,36 @@ that THP is shared. Exceeding the number would block the collapse:: > >   > >   A higher value may increase memory footprint for some workloads. > >   > > -Boot parameter > > -============== > > - > > -You can change the sysfs boot time defaults of Transparent Hugepage > > -Support by passing the parameter ``transparent_hugepage=always`` or > > -``transparent_hugepage=madvise`` or ``transparent_hugepage=never`` > > -to the kernel command line. > > +Boot parameters > > +=============== > > + > > +You can change the sysfs boot time default for the top-level "enabled" > > +control by passing the parameter ``transparent_hugepage=always`` or > > +``transparent_hugepage=madvise`` or ``transparent_hugepage=never`` to the > > +kernel command line. > > + > > +Alternatively, each supported anonymous THP size can be controlled by > > +passing ``thp_anon=,[KMG]:;-[KMG]:``, > > +where ```` is the THP size and ```` is one of ``always``, > > +``madvise``, ``never`` or ``inherit``. > > + > > +For example, the following will set 16K, 32K, 64K THP to ``always``, > > +set 128K, 512K to ``inherit``, set 256K to ``madvise`` and 1M, 2M > > +to ``never``:: > > + > > +     thp_anon=16K-64K:always;128K,512K:inherit;256K:madvise;1M-2M:never > > + > > +``thp_anon=`` may be specified multiple times to configure all THP sizes as > > +required. If ``thp_anon=`` is specified at least once, any anon THP sizes > > +not explicitly configured on the command line are implicitly set to > > +``never``. > > + > > +``transparent_hugepage`` setting only affects the global toggle. If > > +``thp_anon`` is not specified, PMD_ORDER THP will default to ``inherit``. > > +However, if a valid ``thp_anon`` setting is provided by the user, the > > +PMD_ORDER THP policy will be overridden. If the policy for PMD_ORDER > > +is not defined within a valid ``thp_anon``, its policy will default to > > +``never``. > >   > >   Hugepages in tmpfs/shmem > >   ======================== > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > > index 1a12c011e2df..c5f4e97b49de 100644 > > --- a/mm/huge_memory.c > > +++ b/mm/huge_memory.c > > @@ -81,6 +81,7 @@ unsigned long huge_zero_pfn __read_mostly = ~0UL; > >   unsigned long huge_anon_orders_always __read_mostly; > >   unsigned long huge_anon_orders_madvise __read_mostly; > >   unsigned long huge_anon_orders_inherit __read_mostly; > > +static bool anon_orders_configured; > >   > >   unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma, > >                                        unsigned long vm_flags, > > @@ -737,7 +738,10 @@ static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj) > >        * disable all other sizes. powerpc's PMD_ORDER isn't a compile-time > >        * constant so we have to do this here. > >        */ > > -     huge_anon_orders_inherit = BIT(PMD_ORDER); > > +     if (!anon_orders_configured) { > > +             huge_anon_orders_inherit = BIT(PMD_ORDER); > > +             anon_orders_configured = true; > > +     } > >   > >       *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj); > >       if (unlikely(!*hugepage_kobj)) { > > @@ -922,6 +926,96 @@ static int __init setup_transparent_hugepage(char *str) > >   } > >   __setup("transparent_hugepage=", setup_transparent_hugepage); > >   > > +static inline int get_order_from_str(const char *size_str) > > +{ > > +     unsigned long size; > > +     char *endptr; > > +     int order; > > + > > +     size = memparse(size_str, &endptr); > > Do we have to also test if is_power_of_2(), and refuse if not? For > example, what if someone would pass 3K, would the existing check catch it? no, the existing check can't catch it. I passed thp_anon=15K-64K:always, then I got 16K enabled: / # cat /sys/kernel/mm/transparent_hugepage/hugepages-16kB/enabled [always] inherit madvise never I can actually check that by: static inline int get_order_from_str(const char *size_str) { unsigned long size; char *endptr; int order; size = memparse(size_str, &endptr); if (!is_power_of_2(size >> PAGE_SHIFT)) goto err; order = get_order(size); if ((1 << order) & ~THP_ORDERS_ALL_ANON) goto err; return order; err: pr_err("invalid size %s in thp_anon boot parameter\n", size_str); return -EINVAL; } > > > +     order = fls(size >> PAGE_SHIFT) - 1; > > Is this a fancy way of writing > > order = log2(size >> PAGE_SHIFT); > > ? :) I think ilog2 is implemented by fls ? > > Anyhow, if get_order() wraps that, all good. I guess it doesn't check power of 2? > > > +     if ((1 << order) & ~THP_ORDERS_ALL_ANON) { > > +             pr_err("invalid size %s(order %d) in thp_anon boot parameter\n", > > +                     size_str, order); > > +             return -EINVAL; > > +     } > > + > > +     return order; > > +} > > Apart from that, nothing jumped at me. Please take a look at the new get_order_from_str() before I send v5 :-) > > -- > Cheers, > > David / dhildenb > Thanks Barry