From patchwork Tue Aug 20 10:52:44 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: 13769949 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 9EA5FC531DF for ; Tue, 20 Aug 2024 10:53:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1C1416B0083; Tue, 20 Aug 2024 06:53:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 172016B0085; Tue, 20 Aug 2024 06:53:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 038D76B0088; Tue, 20 Aug 2024 06:53:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D7CBA6B0083 for ; Tue, 20 Aug 2024 06:53:06 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8E5D016158B for ; Tue, 20 Aug 2024 10:53:06 +0000 (UTC) X-FDA: 82472311572.08.A705BE8 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by imf14.hostedemail.com (Postfix) with ESMTP id 4663C10002A for ; Tue, 20 Aug 2024 10:53:03 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cTrOvMyw; spf=pass (imf14.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.174 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=1724151105; 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=rcW/bXC7hiuTbCcrDe23N8Cg3ZczxDN7qEUeTv5fisY=; b=BaeCD7HE14fKfM6LXPA158rDbfM0VY7oqxaU7oot4hGWL09vC2nnyGfr95s+bA9m+N1clR Bql2Za4RvUmiaOesNfcR7yoPO4YmtTKigOsadKP8qTGDgRZCbZfGFkOenfb3RrNS7fWIKx 3ecsfS7ekphdssLg4ciyx//O9I7lV8g= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724151105; a=rsa-sha256; cv=none; b=M1t+X+1mxDJngu1xiUkKHfFaDkHSsLR3vU4ZhUtsFgikxnUxt6ZthZMUWpRCiR7aTqZrKm uNWZcaD5ZaQFjsCOCnnn11h1IEQBBWAgXXsdHoN0tjhLzVDqLxZRM1W1uDRkLeW1sEX/bg bMvVF+3Y4fj0XmRVIilqTSLISyDiY54= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cTrOvMyw; spf=pass (imf14.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-7141285db14so211914b3a.1 for ; Tue, 20 Aug 2024 03:53:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724151182; x=1724755982; 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=rcW/bXC7hiuTbCcrDe23N8Cg3ZczxDN7qEUeTv5fisY=; b=cTrOvMywNBiRBviMcm5hXfYVpG4tftcZuwsFf0MUufu1wnc/XJQmrRLwnBwvWjXO+c zuqQ7scHP2CialH35OIqwPoWPRCRLED0fYyCsItELh4fKJh7WFgPMr1PNKBzyG4TdQY7 PkBqThGuNtQCvlAxcOWmUAKXM1AIN1o5V1MJtQnzKc/fnjxnS3C5l20IsLHqRSt1dT7O hziSfO164RHOokkB6iCq6uHL0DKNZnMIHDVUOUWtz8XgartXbxyzPYDy5AtNRcM0N8gV e5kM2VOUlDx5EKBej9q8IJsDEiJxCyHBibfEqemSr2x/HKd/cJOv2jsdiJnRZdD3/L11 4AKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724151182; x=1724755982; 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=rcW/bXC7hiuTbCcrDe23N8Cg3ZczxDN7qEUeTv5fisY=; b=RyPH7D3N6oLKqwojuAEpQWYlWpHI5LPUN1SOiYGD2doP8BIcK7aCDd8Svbul1oQFBI DRK7csIO8aqNl9aveEZJqHyslVpcH91QkXDmLyylBrneLPz9p1+GZUncmo6KFDDplxWI 6SfHQ0qBKmPHzs70D90/zmRs/Po5HGLIOe6uoyXzTGXPPfZKG4JezIApUgRHTtph+8Jn t1iGrgWW3ykl2Dq2b7DLXVmOwU8L3eQQofVhKY9mgdxUTpFXsb4BfH+8vASUcR5oo5TG OpIjSXC0UGdqaNa/LvRBai/ZzpYnnNltrw2lz2OiaiqHzqs9K5xonJVU354Q86tZqfNL bAgw== X-Forwarded-Encrypted: i=1; AJvYcCVQgcTTTDNaGqhGFUFZgL7wj8i6fMRrZP0xG0qmM+/itGSVjHN7kpmzEz/Iu0yvhJ6YsbZFXk+E+A==@kvack.org X-Gm-Message-State: AOJu0Yysi678OHYrjnJf6+UFyXxyZj5cTg/1QH46oMtXEEf78f/d12lp bNziTdV9xoXKu2iYPmzMJPwWqTplQSHXjC9iWOSG4S4Kmz4yHuBt X-Google-Smtp-Source: AGHT+IF38pbSlDGIT7/HZwuM+owas8KMnZZ0O9atxXpxvjUqeDab00Pf4MaOZPDuvlR15sLjij9KDg== X-Received: by 2002:a05:6a20:2d29:b0:1c2:8cc4:9084 with SMTP id adf61e73a8af0-1c905025ed4mr19724289637.34.1724151181677; Tue, 20 Aug 2024 03:53:01 -0700 (PDT) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:9018:5cd2:579f:48d2]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d3e3d97f2asm9051571a91.53.2024.08.20.03.52.57 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 20 Aug 2024 03:53:01 -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 v6] mm: override mTHP "enabled" defaults at kernel cmdline Date: Tue, 20 Aug 2024 22:52:44 +1200 Message-Id: <20240820105244.62703-1-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) MIME-Version: 1.0 X-Rspamd-Queue-Id: 4663C10002A X-Stat-Signature: hgt3kex5f83o87946txs4ijncnbrnoqc X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1724151183-129487 X-HE-Meta: U2FsdGVkX1/DIfqHtSekioU0wX9p+SFb1pdkSg3W46jo7SyC0OZ6vgjaM5MDPbTuQKpSeO2XEEEAsTfIOa9n8bPD0cZzQ2Qj08DcJBZkbgt3cxkjWoT9yxgkWeIA8PAaWBDQ4gM+InbHnUo/a38tYLDPlAYSbwODs+DjHKFit/MLEq0j5W8FdLGxcahBGI8YvpejY7fnRYTWmYZubhaoiYL4WScQh8Zw/zMYzNyiYgO5sOBXZiO3dF07LDoBpPwBWyiQrd8oW5t/8TTOKHcnYqnMvLsaD8Q8G+aX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loG640lzQbLeM4rB+eNpIHHWD9PFXNj7TcsKISyST2j/r+k1x+ojpGHCQ15x6cStMLzBZyeE4VelJ8b5IjO4Opv9wPb5aRe0JEpskHDLZH8+G7k/lgGGcLbntxrJlKzJFlPZ6uxsDO5fwHFJIUi91eg3eFacMb1F9RjXWt/QmSzixb0DA48GCCSwMb9jZrKUs6gocXd4= 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 Acked-by: David Hildenbrand Cc: Jonathan Corbet Cc: Lance Yang --- -v6: * collect acked-by of David, thanks! * some minor cleanup according to David's comments .../admin-guide/kernel-parameters.txt | 9 ++ Documentation/admin-guide/mm/transhuge.rst | 38 +++++-- mm/huge_memory.c | 98 ++++++++++++++++++- 3 files changed, 137 insertions(+), 8 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index f0057bac20fb..8dd0aefea01e 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". + 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 058485daf186..79435c537e21 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 d90d6e94a800..df3199e1715c 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 __initdata; unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma, unsigned long vm_flags, @@ -733,7 +734,8 @@ 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); *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj); if (unlikely(!*hugepage_kobj)) { @@ -918,6 +920,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 (BIT(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: error parsing string, ignoring setting\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))