From nobody Sat Feb 7 18:21:03 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6B68B12F5B1 for ; Thu, 8 Aug 2024 10:17:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723112231; cv=none; b=XglyY5jrf/FsoN7cyVcXPZ9NkydoEE/Fhre1m88Z4jrrZawNkMwVPyAcJ5meJAa7OCJsukIML6Cq5GAGuvxWr1TwYp1npLpVl8phbrxlwG4mdjrPzepzKjPTmuNIppZBtGBl5xnYHEZDcoHbzv40uxt8z9/OLMu7u1i95DXBCGI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723112231; c=relaxed/simple; bh=eKlxAn3MykGZ1MtaksQ5eHbuEVwkUA+gtiqS1gNeapc=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=MH2fgX/SiwWIO5sVjzR9dyKgZQvMsbsyAlg5SvE8fEWggbuCVweucO0L4LktIvsfhYSJIuGCVg+y0CriEZ+DV3u6no+Mj+khnYCVDS0mxMNsqu00uRe98i6ZyXp4Z6TO4W0q2aXbw+ZT0ngSWXaEPdQIu7hq0zbKhtTwMgromOs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A0443FEC; Thu, 8 Aug 2024 03:17:34 -0700 (PDT) Received: from e125769.cambridge.arm.com (e125769.cambridge.arm.com [10.1.196.27]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id AA0EF3F766; Thu, 8 Aug 2024 03:17:07 -0700 (PDT) From: Ryan Roberts To: Andrew Morton , Jonathan Corbet , David Hildenbrand , Barry Song , Lance Yang , Baolin Wang Cc: Ryan Roberts , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2] mm: Override mTHP "enabled" defaults at kernel cmdline Date: Thu, 8 Aug 2024 11:16:59 +0100 Message-ID: <20240808101700.571701-1-ryan.roberts@arm.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add thp_anon=3D 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=3D[KMG]: 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 Reviewed-by: Baolin Wang Reviewed-by: Barry Song Tested-by: Barry Song --- Hi All, I've split this off from my RFC at [1] because Barry highlighted that he wo= uld benefit from it immediately [2]. There are no changes vs the version in that series. It applies against today's mm-unstable (275d686abcb59). (although I had to = fix a minor build bug in stackdepot.c due to MIN() not being defined in this tree= ). Thanks, Ryan .../admin-guide/kernel-parameters.txt | 8 +++ Documentation/admin-guide/mm/transhuge.rst | 26 +++++++-- mm/huge_memory.c | 55 ++++++++++++++++++- 3 files changed, 82 insertions(+), 7 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentatio= n/admin-guide/kernel-parameters.txt index bcdee8984e1f0..5c79b58c108ec 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -6631,6 +6631,14 @@ : poll all this frequency 0: no polling (default) + thp_anon=3D [KNL] + Format: [KMG]:always|madvise|never|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/adm= in-guide/mm/transhuge.rst index 24eec1c03ad88..f63b0717366c6 100644 --- a/Documentation/admin-guide/mm/transhuge.rst +++ b/Documentation/admin-guide/mm/transhuge.rst @@ -284,13 +284,27 @@ that THP is shared. Exceeding the number would block = the collapse:: A higher value may increase memory footprint for some workloads. -Boot parameter -=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Boot parameters +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D -You can change the sysfs boot time defaults of Transparent Hugepage -Support by passing the parameter ``transparent_hugepage=3Dalways`` or -``transparent_hugepage=3Dmadvise`` or ``transparent_hugepage=3Dnever`` -to the kernel command line. +You can change the sysfs boot time default for the top-level "enabled" +control by passing the parameter ``transparent_hugepage=3Dalways`` or +``transparent_hugepage=3Dmadvise`` or ``transparent_hugepage=3Dnever`` to = the +kernel command line. + +Alternatively, each supported anonymous THP size can be controlled by +passing ``thp_anon=3D[KMG]:``, where ```` is the THP si= ze +and ```` is one of ``always``, ``madvise``, ``never`` or +``inherit``. + +For example, the following will set 64K THP to ``always``:: + + thp_anon=3D64K:always + +``thp_anon=3D`` may be specified multiple times to configure all THP sizes= as +required. If ``thp_anon=3D`` is specified at least once, any anon THP sizes +not explicitly configured on the command line are implicitly set 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/huge_memory.c b/mm/huge_memory.c index 0c3075ee00012..c2c0da1eb94e6 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -82,6 +82,7 @@ unsigned long huge_zero_pfn __read_mostly =3D ~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, @@ -672,7 +673,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 =3D BIT(PMD_ORDER); + if (!anon_orders_configured) { + huge_anon_orders_inherit =3D BIT(PMD_ORDER); + anon_orders_configured =3D true; + } *hugepage_kobj =3D kobject_create_and_add("transparent_hugepage", mm_kobj= ); if (unlikely(!*hugepage_kobj)) { @@ -857,6 +861,55 @@ static int __init setup_transparent_hugepage(char *str) } __setup("transparent_hugepage=3D", setup_transparent_hugepage); +static int __init setup_thp_anon(char *str) +{ + unsigned long size; + char *state; + int order; + int ret =3D 0; + + if (!str) + goto out; + + size =3D (unsigned long)memparse(str, &state); + order =3D ilog2(size >> PAGE_SHIFT); + if (*state !=3D ':' || !is_power_of_2(size) || size <=3D PAGE_SIZE || + !(BIT(order) & THP_ORDERS_ALL_ANON)) + goto out; + + state++; + + if (!strcmp(state, "always")) { + clear_bit(order, &huge_anon_orders_inherit); + clear_bit(order, &huge_anon_orders_madvise); + set_bit(order, &huge_anon_orders_always); + ret =3D 1; + } else if (!strcmp(state, "inherit")) { + clear_bit(order, &huge_anon_orders_always); + clear_bit(order, &huge_anon_orders_madvise); + set_bit(order, &huge_anon_orders_inherit); + ret =3D 1; + } else if (!strcmp(state, "madvise")) { + clear_bit(order, &huge_anon_orders_always); + clear_bit(order, &huge_anon_orders_inherit); + set_bit(order, &huge_anon_orders_madvise); + ret =3D 1; + } else if (!strcmp(state, "never")) { + clear_bit(order, &huge_anon_orders_always); + clear_bit(order, &huge_anon_orders_inherit); + clear_bit(order, &huge_anon_orders_madvise); + ret =3D 1; + } + + if (ret) + anon_orders_configured =3D true; +out: + if (!ret) + pr_warn("thp_anon=3D%s: cannot parse, ignored\n", str); + return ret; +} +__setup("thp_anon=3D", setup_thp_anon); + pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) { if (likely(vma->vm_flags & VM_WRITE)) -- 2.43.0