Skip to content

Commit 9490428

Browse files
mairacanalakpm00
authored andcommitted
mm: shmem: control THP support through the kernel command line
Patch series "mm: add more kernel parameters to control mTHP", v5. This series introduces four patches related to the kernel parameters controlling mTHP and a fifth patch replacing `strcpy()` for `strscpy()` in the file `mm/huge_memory.c`. The first patch is a straightforward documentation update, correcting the format of the kernel parameter ``thp_anon=``. The second, third, and fourth patches focus on controlling THP support for shmem via the kernel command line. The second patch introduces a parameter to control the global default huge page allocation policy for the internal shmem mount. The third patch moves a piece of code to a shared header to ease the implementation of the fourth patch. Finally, the fourth patch implements a parameter similar to ``thp_anon=``, but for shmem. The goal of these changes is to simplify the configuration of systems that rely on mTHP support for shmem. For instance, a platform with a GPU that benefits from huge pages may want to enable huge pages for shmem. Having these kernel parameters streamlines the configuration process and ensures consistency across setups. This patch (of 4): Add a new kernel command line to control the hugepage allocation policy for the internal shmem mount, ``transparent_hugepage_shmem``. The parameter is similar to ``transparent_hugepage`` and has the following format: transparent_hugepage_shmem=<policy> where ``<policy>`` is one of the seven valid policies available for shmem. Configuring the default huge page allocation policy for the internal shmem mount can be beneficial for DRM GPU drivers. Just as CPU architectures, GPUs can also take advantage of huge pages, but this is possible only if DRM GEM objects are backed by huge pages. Since GEM uses shmem to allocate anonymous pageable memory, having control over the default huge page allocation policy allows for the exploration of huge pages use on GPUs that rely on GEM objects backed by shmem. Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Maíra Canal <[email protected]> Reviewed-by: Baolin Wang <[email protected]> Acked-by: David Hildenbrand <[email protected]> Cc: Barry Song <[email protected]> Cc: [email protected] Cc: Hugh Dickins <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: [email protected] Cc: Lance Yang <[email protected]> Cc: Ryan Roberts <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent e3d37a6 commit 9490428

File tree

3 files changed

+62
-23
lines changed

3 files changed

+62
-23
lines changed

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6926,6 +6926,13 @@
69266926
See Documentation/admin-guide/mm/transhuge.rst
69276927
for more details.
69286928

6929+
transparent_hugepage_shmem= [KNL]
6930+
Format: [always|within_size|advise|never|deny|force]
6931+
Can be used to control the hugepage allocation policy for
6932+
the internal shmem mount.
6933+
See Documentation/admin-guide/mm/transhuge.rst
6934+
for more details.
6935+
69296936
trusted.source= [KEYS]
69306937
Format: <string>
69316938
This parameter identifies the trust source as a backend

Documentation/admin-guide/mm/transhuge.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,12 @@ PMD_ORDER THP policy will be overridden. If the policy for PMD_ORDER
326326
is not defined within a valid ``thp_anon``, its policy will default to
327327
``never``.
328328

329+
Similarly to ``transparent_hugepage``, you can control the hugepage
330+
allocation policy for the internal shmem mount by using the kernel parameter
331+
``transparent_hugepage_shmem=<policy>``, where ``<policy>`` is one of the
332+
seven valid policies for shmem (``always``, ``within_size``, ``advise``,
333+
``never``, ``deny``, and ``force``).
334+
329335
Hugepages in tmpfs/shmem
330336
========================
331337

mm/shmem.c

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -582,24 +582,39 @@ static bool shmem_huge_global_enabled(struct inode *inode, pgoff_t index,
582582
}
583583
}
584584

585-
#if defined(CONFIG_SYSFS)
586585
static int shmem_parse_huge(const char *str)
587586
{
587+
int huge;
588+
589+
if (!str)
590+
return -EINVAL;
591+
588592
if (!strcmp(str, "never"))
589-
return SHMEM_HUGE_NEVER;
590-
if (!strcmp(str, "always"))
591-
return SHMEM_HUGE_ALWAYS;
592-
if (!strcmp(str, "within_size"))
593-
return SHMEM_HUGE_WITHIN_SIZE;
594-
if (!strcmp(str, "advise"))
595-
return SHMEM_HUGE_ADVISE;
596-
if (!strcmp(str, "deny"))
597-
return SHMEM_HUGE_DENY;
598-
if (!strcmp(str, "force"))
599-
return SHMEM_HUGE_FORCE;
600-
return -EINVAL;
593+
huge = SHMEM_HUGE_NEVER;
594+
else if (!strcmp(str, "always"))
595+
huge = SHMEM_HUGE_ALWAYS;
596+
else if (!strcmp(str, "within_size"))
597+
huge = SHMEM_HUGE_WITHIN_SIZE;
598+
else if (!strcmp(str, "advise"))
599+
huge = SHMEM_HUGE_ADVISE;
600+
else if (!strcmp(str, "deny"))
601+
huge = SHMEM_HUGE_DENY;
602+
else if (!strcmp(str, "force"))
603+
huge = SHMEM_HUGE_FORCE;
604+
else
605+
return -EINVAL;
606+
607+
if (!has_transparent_hugepage() &&
608+
huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
609+
return -EINVAL;
610+
611+
/* Do not override huge allocation policy with non-PMD sized mTHP */
612+
if (huge == SHMEM_HUGE_FORCE &&
613+
huge_shmem_orders_inherit != BIT(HPAGE_PMD_ORDER))
614+
return -EINVAL;
615+
616+
return huge;
601617
}
602-
#endif
603618

604619
#if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS)
605620
static const char *shmem_format_huge(int huge)
@@ -5065,15 +5080,7 @@ static ssize_t shmem_enabled_store(struct kobject *kobj,
50655080

50665081
huge = shmem_parse_huge(tmp);
50675082
if (huge == -EINVAL)
5068-
return -EINVAL;
5069-
if (!has_transparent_hugepage() &&
5070-
huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
5071-
return -EINVAL;
5072-
5073-
/* Do not override huge allocation policy with non-PMD sized mTHP */
5074-
if (huge == SHMEM_HUGE_FORCE &&
5075-
huge_shmem_orders_inherit != BIT(HPAGE_PMD_ORDER))
5076-
return -EINVAL;
5083+
return huge;
50775084

50785085
shmem_huge = huge;
50795086
if (shmem_huge > SHMEM_HUGE_DENY)
@@ -5170,6 +5177,25 @@ struct kobj_attribute thpsize_shmem_enabled_attr =
51705177
__ATTR(shmem_enabled, 0644, thpsize_shmem_enabled_show, thpsize_shmem_enabled_store);
51715178
#endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */
51725179

5180+
#if defined(CONFIG_TRANSPARENT_HUGEPAGE)
5181+
5182+
static int __init setup_transparent_hugepage_shmem(char *str)
5183+
{
5184+
int huge;
5185+
5186+
huge = shmem_parse_huge(str);
5187+
if (huge == -EINVAL) {
5188+
pr_warn("transparent_hugepage_shmem= cannot parse, ignored\n");
5189+
return huge;
5190+
}
5191+
5192+
shmem_huge = huge;
5193+
return 1;
5194+
}
5195+
__setup("transparent_hugepage_shmem=", setup_transparent_hugepage_shmem);
5196+
5197+
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
5198+
51735199
#else /* !CONFIG_SHMEM */
51745200

51755201
/*

0 commit comments

Comments
 (0)