From patchwork Sat Aug 17 04:55:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13766982 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 01558C52D7F for ; Sat, 17 Aug 2024 04:56:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21A326B03FF; Sat, 17 Aug 2024 00:56:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1CAEF6B0400; Sat, 17 Aug 2024 00:56:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 092566B0401; Sat, 17 Aug 2024 00:56:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E0EDD6B03FF for ; Sat, 17 Aug 2024 00:56:08 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id EC247A04E7 for ; Sat, 17 Aug 2024 04:56:07 +0000 (UTC) X-FDA: 82460525574.23.1491861 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) by imf19.hostedemail.com (Postfix) with ESMTP id 2BB141A0007 for ; Sat, 17 Aug 2024 04:56:05 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ml3lydS5; spf=pass (imf19.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723870466; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=90Dn5cjzDzLtyzWrS/a8MBAYblFtGSEi8TOi7CgPMA0=; b=NJqZuCNkyCMctJTJbNnQ8lckklYoux8MiFTVR/LcBEly/VLMSLoSQvdx7FqBDAqBrYnBPa WbrkQj5qndQkqv2r24q8xZnp/d8T3KDmFI1fqWEWWLzuNsWp8uL7qvcAWX55Q4YQo+M//p PQ4yzpoKp7l2qgPVEn6j7U/JJoIFgJM= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ml3lydS5; spf=pass (imf19.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723870466; a=rsa-sha256; cv=none; b=Yn/H/nIDsGKOmMDBwenOx+E781LfNiiO2eNrbZNAgwvxtDsxBEaD0GyK/uGxkgJoaubg7S CtCkDBKiyVlSYuhrg3oyMpOZ4MHSnacifTkDDG9WVVYJA6l7MkDJ6kei9nkpbmFiZvy0OZ s53i2GMEBp5hqKl8G9fMVSwMp5wOZvA= Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-70d25b5b6b0so2103138b3a.2 for ; Fri, 16 Aug 2024 21:56:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723870565; x=1724475365; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=90Dn5cjzDzLtyzWrS/a8MBAYblFtGSEi8TOi7CgPMA0=; b=ml3lydS5YoyIFSqzFdOUyyR75VyE5fz/AZWnmWY060b3S7daEuKa7egRIHVTcZJ7Tp O8F8xQI82CxEPr5lEdE8/Q/Of32wVExFekux7cMHJxHofPBsqVvln7h4hUpV7jywveFB 1Vd3XKuCE3Ra85QaH/yLJnTb2ZR5WgV670s0jVNO+0k4nekiSFH2giu5RC3pP5aQi2Ym eMHrRFN3lvoIkAiMEAM/QRsxJ6IIHgssE9AyNLRAbcWnnRtu2pQRKZF5kljuLd9iiFYi MkkiqPZHtvHtTBxWHY2br/NkYB+F8rjnQvb/1ZrlRJvO3qsF0FGt7KrLnySuFJvqauoY VX8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723870565; x=1724475365; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=90Dn5cjzDzLtyzWrS/a8MBAYblFtGSEi8TOi7CgPMA0=; b=LQ3H4KyzCBi65SJBBd7AdW/W+ixRd/J09rY96OPMLkjb46B5tS8VR/nnD5/Tu07nuV TXDU93tAOo63kdzV/J/Jpebe+jhL/Y4hw9WKk23UCBPoyoUUDZcNNPAlePPNMfkHhDgi QSGCR1gDQS0fXQtTHkVJEyQFJ8IsNDNFKkyHQOmGekb8F7MfGhuqaJSxbBzklrKo1LQZ smgNfUODUhoTloFQ2u9J9qRS6pHH9zAOXJHJ+BqvUsnE7uv7zyxc9HsRu+R09zAkscRt n4BezyuTv/qmWQ/YzD4ivUzYuJ+yvNzsrVwKKLAFIDszLvRf35nrfYNb6Cg/HPNnXlaI 7R5Q== X-Forwarded-Encrypted: i=1; AJvYcCWyU2C4wM72t4TSgFkfjxjbQbWOJQGc7JSh2SCeZXAMgKT9TSACkeVXigdCbP5WS4BoPd5sSxThqcpc0exztC2k6VA= X-Gm-Message-State: AOJu0YxhhCaNEPG1CTO3gytwrlOpfZmFyqIRRa/9x6E+SFJRPUl6Ix0c wYtUoX5RFjOhmQsuI4KKP91Iy8H6XYu7L/ni/G+pHSkKxK1EOFRc X-Google-Smtp-Source: AGHT+IHNKqt8weUm9VZF1ryifXfv4BG6d1BYy0/Vgx1L5ZhHaR/NdgwqOBdZQNBPwmxij2Y4NjwLsw== X-Received: by 2002:a05:6a00:3d01:b0:70d:2497:383 with SMTP id d2e1a72fcca58-713c4e72710mr6199987b3a.18.1723870564663; Fri, 16 Aug 2024 21:56:04 -0700 (PDT) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:fd84:292a:c6d0:8b67]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7c6b61dc763sm3809014a12.39.2024.08.16.21.56.00 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 16 Aug 2024 21:56:04 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: baohua@kernel.org, baolin.wang@linux.alibaba.com, corbet@lwn.net, david@redhat.com, ioworker0@gmail.com, linux-kernel@vger.kernel.org, ryan.roberts@arm.com, v-songbaohua@oppo.com Subject: [PATCH v5] mm: override mTHP "enabled" defaults at kernel cmdline Date: Sat, 17 Aug 2024 16:55:16 +1200 Message-Id: <20240817045516.58037-1-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 2BB141A0007 X-Stat-Signature: d75zj1rg53y6tndf44gxxspzdjyf55wc X-Rspam-User: X-HE-Tag: 1723870565-125529 X-HE-Meta: U2FsdGVkX1+KRByxelHQC826E4VxcfR/tBrTxOTmH04afqbk3o1MA1MvJ7nh8vbt9AaNlb92KXcRFSMbuBHIeFz9Um3TWX/VFGTPdXPHZ6HqnPFOviVYa8TvT4naQygQSlC0cCiNqqAmPJ9Hx7S2ss7CU9rANmnWN8j54kmSEUNYj65kMjOZFlxImPWgRmQkPGywSZkP9sltEYCezCSVp/a4tJzk7w1XmclEji6ArNVXhNg7Tvq8ifOEhTalecOv9ZdMtu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oeqjbPJrVOQ1cP1xHFM37kuhWQm4QU08C43NefK16nKG9FW3CM6ji1dqPf7z50MgnhhXJcNTXDUM3Pmn+HTVxDZcsWiAKIfY1sJIhhncY8zkBJZ8wPo3jmGOrfIR/FqYF2kzcM5Vjp8vRbvyKMySwyLNyGCrRPQ1zRM49jmkycAfaujAHkCIt8rj3NZkYVD3cB5a1j8fjobdOCHM+7rsUGt7GLNq3y/3ExxTzWPc= 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: 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 Reviewed-by: Baolin Wang Tested-by: Baolin Wang Cc: David Hildenbrand Cc: Jonathan Corbet Cc: Lance Yang Acked-by: David Hildenbrand --- -v5: * collect Baolin's reviewed-by and tested-by tags, thanks! * use get_order and check size is power 2, David, Baolin; * use proper __initdata .../admin-guide/kernel-parameters.txt | 9 ++ Documentation/admin-guide/mm/transhuge.rst | 38 +++++-- mm/huge_memory.c | 100 +++++++++++++++++- 3 files changed, 139 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 60522f49178b..4468851b6ecb 100644 --- a/Documentation/admin-guide/mm/transhuge.rst +++ b/Documentation/admin-guide/mm/transhuge.rst @@ -284,13 +284,37 @@ 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 (must be a power of 2 of PAGE_SIZE and +supported anonymous THP) 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 69cef10ed9aa..c8ca577526cf 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -82,6 +82,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 __initdata; unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma, unsigned long vm_flags, @@ -756,7 +757,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)) { @@ -941,6 +945,100 @@ 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); + + if (!is_power_of_2(size)) + 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; +} + +static char str_dup[PAGE_SIZE] __initdata; +static int __init setup_thp_anon(char *str) +{ + char *token, *range, *policy, *subtoken; + unsigned long always, inherit, madvise; + 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 = huge_anon_orders_always; + madvise = huge_anon_orders_madvise; + inherit = huge_anon_orders_inherit; + p = str_dup; + while ((token = strsep(&p, ";")) != NULL) { + range = strsep(&token, ":"); + policy = token; + + if (!policy) + goto err; + + while ((subtoken = strsep(&range, ",")) != NULL) { + if (strchr(subtoken, '-')) { + start_size = strsep(&subtoken, "-"); + end_size = subtoken; + + start = get_order_from_str(start_size); + end = get_order_from_str(end_size); + } else { + start = end = get_order_from_str(subtoken); + } + + if (start < 0 || end < 0 || start > end) + goto err; + + nr = end - start + 1; + if (!strcmp(policy, "always")) { + bitmap_set(&always, start, nr); + bitmap_clear(&inherit, start, nr); + bitmap_clear(&madvise, start, nr); + } else if (!strcmp(policy, "madvise")) { + bitmap_set(&madvise, start, nr); + bitmap_clear(&inherit, start, nr); + bitmap_clear(&always, start, nr); + } else if (!strcmp(policy, "inherit")) { + bitmap_set(&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); + } else { + pr_err("invalid policy %s in thp_anon boot parameter\n", policy); + goto err; + } + } + } + + huge_anon_orders_always = always; + huge_anon_orders_madvise = madvise; + huge_anon_orders_inherit = inherit; + anon_orders_configured = true; + return 1; + +err: + pr_warn("thp_anon=%s: cannot parse, ignored\n", str); + return 0; +} +__setup("thp_anon=", setup_thp_anon); + pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) { if (likely(vma->vm_flags & VM_WRITE))