mm: shmem/tmpfs hugepage defaults config choice

Allow to override defaults for shemem and tmpfs at config time.  This is
consistent with how transparent hugepages can be configured.

Same results can be achieved with the existing
'transparent_hugepage_shmem' and 'transparent_hugepage_tmpfs' settings in
the kernel command line, but it is more convenient to define basic
settings at config time instead of changing kernel command line later.

Defaults for shmem and tmpfs were not changed.  They are remained the same
as before: 'never' for both cases.  Options 'deny' and 'force' are omitted
intentionally since these are special values and supposed to be used for
emergencies or testing and are not expected to be permanent ones.

Primary motivation for adding config option is to enable policy
enforcement at build time.  In large-scale production environments (Meta's
for example), the kernel configuration is often maintained centrally close
to the kernel code itself and owned by the kernel engineers, while boot
parameters are managed independently (e.g.  by provisioning systems).  In
such setups, the kernel build defines the supported and expected behavior
in a single place, but there is no reliable or uniform control over the
kernel command line options.

A build-time default allows kernel integrators to enforce a predictable
hugepage policy for shmem/tmpfs on a base layer, ensuring reproducible
behavior and avoiding configuration drift caused by possible boot-time
differences.

In short, primary benefit is mostly operational: it provides a way to
codify preferred policy in the kernel configuration, which is versioned,
reviewed, and tested as part of the kernel build process, rather than
depending on potentially variable boot parameters.

[d@ilvokhin.com: v2]
  Link: https://lkml.kernel.org/r/aQECPpjd-fU_TC79@shell.ilvokhin.com
Link: https://lkml.kernel.org/r/aPpv8sAa2sYgNu3L@shell.ilvokhin.com
Signed-off-by: Dmitry Ilvokhin <d@ilvokhin.com>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Acked-by: Michal Hocko <mhocko@suse.com>
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Acked-by: Kiryl Shutsemau <kas@kernel.org>
Cc: David Hildenbrand <david@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
Dmitry Ilvokhin
2025-10-23 18:12:02 +00:00
committed by Andrew Morton
parent f46dbea0d9
commit e97d7c5165
3 changed files with 127 additions and 2 deletions

View File

@@ -381,6 +381,11 @@ hugepage allocation policy for the tmpfs mount by using the kernel parameter
four valid policies for tmpfs (``always``, ``within_size``, ``advise``,
``never``). The tmpfs mount default policy is ``never``.
Additionally, Kconfig options are available to set the default hugepage
policies for shmem (``CONFIG_TRANSPARENT_HUGEPAGE_SHMEM_HUGE_*``) and tmpfs
(``CONFIG_TRANSPARENT_HUGEPAGE_TMPFS_HUGE_*``) at build time. Refer to the
Kconfig help for more details.
In the same manner as ``thp_anon`` controls each supported anonymous THP
size, ``thp_shmem`` controls each supported shmem THP size. ``thp_shmem``
has the same format as ``thp_anon``, but also supports the policy

View File

@@ -853,6 +853,97 @@ choice
enabled at runtime via sysfs.
endchoice
choice
prompt "Shmem hugepage allocation defaults"
depends on TRANSPARENT_HUGEPAGE
default TRANSPARENT_HUGEPAGE_SHMEM_HUGE_NEVER
help
Selects the hugepage allocation policy defaults for
the internal shmem mount.
The selection made here can be overridden by using the kernel
command line 'transparent_hugepage_shmem=' option.
config TRANSPARENT_HUGEPAGE_SHMEM_HUGE_NEVER
bool "never"
help
Disable hugepage allocation for shmem mount by default. It can
still be enabled with the kernel command line
'transparent_hugepage_shmem=' option or at runtime via sysfs
knob. Note that madvise(MADV_COLLAPSE) can still cause
transparent huge pages to be obtained even if this mode is
specified.
config TRANSPARENT_HUGEPAGE_SHMEM_HUGE_ALWAYS
bool "always"
help
Always attempt to allocate hugepage for shmem mount, can
increase the memory footprint of applications without a
guaranteed benefit but it will work automatically for all
applications.
config TRANSPARENT_HUGEPAGE_SHMEM_HUGE_WITHIN_SIZE
bool "within_size"
help
Enable hugepage allocation for shmem mount if the allocation
will be fully within the i_size. This configuration also takes
into account any madvise(MADV_HUGEPAGE) hints that may be
provided by the applications.
config TRANSPARENT_HUGEPAGE_SHMEM_HUGE_ADVISE
bool "advise"
help
Enable hugepage allocation for the shmem mount exclusively when
applications supply the madvise(MADV_HUGEPAGE) hint.
This ensures that hugepages are used only in response to explicit
requests from applications.
endchoice
choice
prompt "Tmpfs hugepage allocation defaults"
depends on TRANSPARENT_HUGEPAGE
default TRANSPARENT_HUGEPAGE_TMPFS_HUGE_NEVER
help
Selects the hugepage allocation policy defaults for
the tmpfs mount.
The selection made here can be overridden by using the kernel
command line 'transparent_hugepage_tmpfs=' option.
config TRANSPARENT_HUGEPAGE_TMPFS_HUGE_NEVER
bool "never"
help
Disable hugepage allocation for tmpfs mount by default. It can
still be enabled with the kernel command line
'transparent_hugepage_tmpfs=' option. Note that
madvise(MADV_COLLAPSE) can still cause transparent huge pages
to be obtained even if this mode is specified.
config TRANSPARENT_HUGEPAGE_TMPFS_HUGE_ALWAYS
bool "always"
help
Always attempt to allocate hugepage for tmpfs mount, can
increase the memory footprint of applications without a
guaranteed benefit but it will work automatically for all
applications.
config TRANSPARENT_HUGEPAGE_TMPFS_HUGE_WITHIN_SIZE
bool "within_size"
help
Enable hugepage allocation for tmpfs mount if the allocation
will be fully within the i_size. This configuration also takes
into account any madvise(MADV_HUGEPAGE) hints that may be
provided by the applications.
config TRANSPARENT_HUGEPAGE_TMPFS_HUGE_ADVISE
bool "advise"
help
Enable hugepage allocation for the tmpfs mount exclusively when
applications supply the madvise(MADV_HUGEPAGE) hint.
This ensures that hugepages are used only in response to explicit
requests from applications.
endchoice
config THP_SWAP
def_bool y
depends on TRANSPARENT_HUGEPAGE && ARCH_WANTS_THP_SWAP && SWAP && 64BIT

View File

@@ -570,8 +570,37 @@ static int shmem_confirm_swap(struct address_space *mapping, pgoff_t index,
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
/* ifdef here to avoid bloating shmem.o when not necessary */
static int shmem_huge __read_mostly = SHMEM_HUGE_NEVER;
static int tmpfs_huge __read_mostly = SHMEM_HUGE_NEVER;
#if defined(CONFIG_TRANSPARENT_HUGEPAGE_SHMEM_HUGE_NEVER)
#define SHMEM_HUGE_DEFAULT SHMEM_HUGE_NEVER
#elif defined(CONFIG_TRANSPARENT_HUGEPAGE_SHMEM_HUGE_ALWAYS)
#define SHMEM_HUGE_DEFAULT SHMEM_HUGE_ALWAYS
#elif defined(CONFIG_TRANSPARENT_HUGEPAGE_SHMEM_HUGE_WITHIN_SIZE)
#define SHMEM_HUGE_DEFAULT SHMEM_HUGE_WITHIN_SIZE
#elif defined(CONFIG_TRANSPARENT_HUGEPAGE_SHMEM_HUGE_ADVISE)
#define SHMEM_HUGE_DEFAULT SHMEM_HUGE_ADVISE
#else
#define SHMEM_HUGE_DEFAULT SHMEM_HUGE_NEVER
#endif
static int shmem_huge __read_mostly = SHMEM_HUGE_DEFAULT;
#undef SHMEM_HUGE_DEFAULT
#if defined(CONFIG_TRANSPARENT_HUGEPAGE_TMPFS_HUGE_NEVER)
#define TMPFS_HUGE_DEFAULT SHMEM_HUGE_NEVER
#elif defined(CONFIG_TRANSPARENT_HUGEPAGE_TMPFS_HUGE_ALWAYS)
#define TMPFS_HUGE_DEFAULT SHMEM_HUGE_ALWAYS
#elif defined(CONFIG_TRANSPARENT_HUGEPAGE_TMPFS_HUGE_WITHIN_SIZE)
#define TMPFS_HUGE_DEFAULT SHMEM_HUGE_WITHIN_SIZE
#elif defined(CONFIG_TRANSPARENT_HUGEPAGE_TMPFS_HUGE_ADVISE)
#define TMPFS_HUGE_DEFAULT SHMEM_HUGE_ADVISE
#else
#define TMPFS_HUGE_DEFAULT SHMEM_HUGE_NEVER
#endif
static int tmpfs_huge __read_mostly = TMPFS_HUGE_DEFAULT;
#undef TMPFS_HUGE_DEFAULT
static unsigned int shmem_get_orders_within_size(struct inode *inode,
unsigned long within_size_orders, pgoff_t index,