From patchwork Mon Aug 12 10:24:45 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: 13760390 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 7DC51C52D7C for ; Mon, 12 Aug 2024 10:25:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B446F6B008A; Mon, 12 Aug 2024 06:25:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AF4536B008C; Mon, 12 Aug 2024 06:25:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9947D6B0092; Mon, 12 Aug 2024 06:25:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 784FE6B008A for ; Mon, 12 Aug 2024 06:25:04 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 380141A09B7 for ; Mon, 12 Aug 2024 10:25:04 +0000 (UTC) X-FDA: 82443210528.18.CEA5779 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) by imf26.hostedemail.com (Postfix) with ESMTP id 67464140016 for ; Mon, 12 Aug 2024 10:25:01 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Y/GqBVdQ"; spf=pass (imf26.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.216.41 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=1723458209; 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=iSwvP6dNgKH28DNmEeTEiBm7zurXGy8bxZZfYwW8Zsw=; b=YPXPsL/9NvanFWrgzRumxZ6eFsxtxoCPGj3n8ytkU4TSxiUBA1ctWR3OBRo0jVd1hJyJqK ZdoWhXgI2FzrCrGTM1ILbMofCvuyW7JbO9PmVWbZ1deF3YTifrm3aCA42pps0lzgX5eQVb IBwwhjo9Z5Ac4dbXaVH30tAHR0T/M4A= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Y/GqBVdQ"; spf=pass (imf26.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.216.41 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=1723458209; a=rsa-sha256; cv=none; b=YsTfW4eyNHeNDHEmujM8Vk66B55d3QMUcrzsLk9RkXkCNbj3AwmZUtVtAFa/MBv8CbOZka FTA30WZbQDOLMre/yJ1DGE/De5PpK5h+pRdjc9ccnc2jMMmnBcR0wS8P+nx3YQBaCY3Wvv l2RR/LKhBAhQZlsyQLJL+H3BKo8i1sw= Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-2cb4c584029so3229265a91.3 for ; Mon, 12 Aug 2024 03:25:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723458300; x=1724063100; 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=iSwvP6dNgKH28DNmEeTEiBm7zurXGy8bxZZfYwW8Zsw=; b=Y/GqBVdQ/TPNx/pIqP3BoJu062I+RjdEbSKnF6G+9i9fXnS9xYuYs5/cFGJfvGpZxu Tpp6BTM18oRzYUSby3EmGxSjg9cSN7TIvRbeBoMOwMuCiec0N2CVtJ/pmlqWrxZsGqHb eTxXbLhDbbyFVUmiHzSxLc2rFky6t6XZckFqOWPw3KMu0HFBNyZzv9oCAFQeS+W1glk9 TSlz9dNNW9VCIXOy1XFZ+PA8ce9UUsplzHm0WoOAyvUo+GSfPifDONZWLada8eUB9Bfq AREg+TIQISzSyBFb1X0yIGmvzLMBjvLtDKcEcqM4/d8cw3WF1KJmX236itZCPwlqY3pX yc4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723458300; x=1724063100; 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=iSwvP6dNgKH28DNmEeTEiBm7zurXGy8bxZZfYwW8Zsw=; b=VxoqwYkyreqEaYw79creTCv4z75EHbngXZTXDk2Ouyf7ecS4nCMl+i0Q0CEfHhL1rS YAJA19qiqMrO/m6UOfpPXTLKLaS8GUqchGEIw6h/BT9AI/udHF5bCPsXzpr7BAFardl6 GmR2I+uY9IrRZp0mEnI67g819TjpYNOVlsmqeOp6ZRmKahWdl34wwmvdNv42rlItXKMw GrkOwa69vKVKBYBaDVkLkQo+Qocq4YiPeAUALDCX2wuYnmJ9YaRLNRSFn7zzsxVrzRQ2 N7rG9rMDLboMYAA453o1t8aKacZrDgul2VcfYXNlE+4k09Oc6RceXD/C54151oMyOHDE grTg== X-Forwarded-Encrypted: i=1; AJvYcCV67Xi8H7R36wWpKPPH1hAnCYXYp4amiKTcLPJmhWGWbNHqgGWE8uuNJpqb62hUrqi47uDRbJXZSBnh9hrDb59Baq0= X-Gm-Message-State: AOJu0YyhRK29q2buDaiUSBQchmhg8iUbi3ehAGtmLSalkrbYOFJ543wK 6PhAFR35jICLMALOtN9x8IMXahDEfroQJkvsfor4Tr7gvSYBN/uE X-Google-Smtp-Source: AGHT+IEXr7/VY2siBiPSeb5Eccp77TnE0XKvKtCrJQj3OxZSPs+ep5Cn4wVehtUADo6Hucz4Ay5wrw== X-Received: by 2002:a17:90a:1582:b0:2cb:3749:7baf with SMTP id 98e67ed59e1d1-2d1e7fc113cmr9861198a91.17.1723458299750; Mon, 12 Aug 2024 03:24:59 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d1c9dc8b58sm8566977a91.53.2024.08.12.03.24.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Aug 2024 03:24:59 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: ryan.roberts@arm.com, baohua@kernel.org, baolin.wang@linux.alibaba.com, corbet@lwn.net, david@redhat.com, ioworker0@gmail.com, linux-kernel@vger.kernel.org, Barry Song Subject: [PATCH v3] mm: Override mTHP "enabled" defaults at kernel cmdline Date: Mon, 12 Aug 2024 22:24:45 +1200 Message-Id: <20240812102445.162087-1-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 67464140016 X-Stat-Signature: r1jpty959ffkkao8odx8x8kksykt3rjr X-Rspam-User: X-HE-Tag: 1723458301-473666 X-HE-Meta: U2FsdGVkX18bHF+5XrzqkjbzNiS5RHQlgxCHyayo1gYEVMz3W0lulgYR6mm992cYM7kxlaHYeD3EIQkqZv85rdRANCq1+0fxd6VVV+gBkwrmI0om2VsWmmNjT45nm0cItOD+KrJbBIvAri6jQnHmQuo288qtVxx5muVBhksWECfJnF7x68u9PugZYBNfjKR5VlnZFw7pIu8Vnfx/aQgf6YUwDaCSHSDbJhL46WfS+WW51jVFWATG/5+QA7ZPWyABZt7L1lDz81xYOCajFRSWdHdtaGRxoZqNPzE8VqFtgSzQz8w0L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rrrE1zXmKKgRmSHLUQpXr+kZhhsemZTwf/kdIqrtKLe5B4DHy7k7et4Bloo16sHm/7vw1p/az7wKYWOA027PQ3Iz0apPMOfGsEM04XiMus= 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 --- -v3: * support formats as below, Per David's suggestion thp_anon=16K-64K:always;128K,512K:inherit;256K:madvise;1M-2M:never * document the relationship between transparent_hugepage and thp_anon better, Per David; * Hi Baolin, I am taking your Reviewed-by because the code has changed quite a lot. .../admin-guide/kernel-parameters.txt | 9 ++ Documentation/admin-guide/mm/transhuge.rst | 37 ++++-- mm/huge_memory.c | 112 +++++++++++++++++- 3 files changed, 150 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..e8eeb8111f6e 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,112 @@ 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); + order = fls(size >> PAGE_SHIFT) - 1; + 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; +} + +static inline void set_bits(unsigned long *var, int start, int end) +{ + int i; + + for (i = start; i <= end; i++) + set_bit(i, var); +} + +static inline void clear_bits(unsigned long *var, int start, int end) +{ + int i; + + for (i = start; i <= end; i++) + clear_bit(i, var); +} + +static char str_dup[PAGE_SIZE] __meminitdata; +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; + 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; + + if (!strcmp(policy, "always")) { + set_bits(&always, start, end); + clear_bits(&inherit, start, end); + clear_bits(&madvise, start, end); + } else if (!strcmp(policy, "madvise")) { + set_bits(&madvise, start, end); + clear_bits(&inherit, start, end); + clear_bits(&always, start, end); + } else if (!strcmp(policy, "inherit")) { + set_bits(&inherit, start, end); + clear_bits(&madvise, start, end); + clear_bits(&always, start, end); + } else if (!strcmp(policy, "never")) { + clear_bits(&inherit, start, end); + clear_bits(&madvise, start, end); + clear_bits(&always, start, end); + } 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))