From patchwork Wed Aug 14 02:02:47 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: 13762736 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 DC5D1C52D7C for ; Wed, 14 Aug 2024 02:03:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4F40F6B0085; Tue, 13 Aug 2024 22:03:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4A4936B0088; Tue, 13 Aug 2024 22:03:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 393956B0089; Tue, 13 Aug 2024 22:03:16 -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 17EAF6B0085 for ; Tue, 13 Aug 2024 22:03:16 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C5236160B30 for ; Wed, 14 Aug 2024 02:03:15 +0000 (UTC) X-FDA: 82449203550.22.281452F Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by imf29.hostedemail.com (Postfix) with ESMTP id F2CF912001F for ; Wed, 14 Aug 2024 02:03:13 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cT0+uhv4; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723600936; a=rsa-sha256; cv=none; b=duw1kpzs7TdpWF5SmUWGKS5a1SbkzAqBN8SCBICyJ1lctH8qJLLCkJM8KI0ZHRxxJXohx8 ptfRfm82ak097HIU+xm0iVA4Emf0rY36gVWK/iK3B0PvZrSX8fs+MbKG8aLcPyTFJkCjmn W9UjAghcx8qKrfD4pnvFGrsvoV5W+qY= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cT0+uhv4; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723600936; 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=O8pN1/0/watTUQfYOs3nJpvPLJBzfi893UM4WQLVxvc=; b=qYYvqb1Nw04MxdQEX3EIeeZ4sZA2CfB5yy/T/012llnoLouctBkzuwAnZTVVMUtZjwK4dh 0e+tqCl2SpA5YqjqPKWnQF/ZBhvZf2Boze4+IugTGdYelYZeySruH6GJKH9UZuNW0C72SD xyGBsMcXQDRkdP7WE1wHPK0IBQ6Ifes= Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1fee6435a34so46262625ad.0 for ; Tue, 13 Aug 2024 19:03:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723600992; x=1724205792; 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=O8pN1/0/watTUQfYOs3nJpvPLJBzfi893UM4WQLVxvc=; b=cT0+uhv4ynm3HQmdsWJRvDKQzWE1yxY+sBsma2HwGG4zrAzzEka/wKTfAyfv/DA8JW c1cHhztWa7/Nb/qoZZo0t/p2zS+0TCyZZJeRHDmXo0oq7Dg7hafQHxD/9CRjz+mDrd7y r9Nv8D16SAVnaP3aqMrwKpoK+JzmHFod3UhRRNMeZWdr/tuFduv86HavTW+IuHXRvo7+ fx84Ci1CjH0HfZULGP2Olf/r6/rVrdsOtGffIZPSd7XMQG2JaCvjxYHV05PCMiyidY0V w3XIfjb9dkWI1DyMn8/5VnEAdA1z43GNrhT6KcTvrX5pXfpD+QVzRxVvNg19HaC0Dtj7 QWcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723600992; x=1724205792; 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=O8pN1/0/watTUQfYOs3nJpvPLJBzfi893UM4WQLVxvc=; b=CyUBMdLST/O8c08ib/vHSuDXQOjDt74zK1bqUjXilDUSqC9Td4raQEgFBBtIDP5AV4 d1DBAODnRRftzJdebjkFxFVJHu3RhJSDCk8w9tSFKhwVr+PLT3DB5BvWuO4P0FI19wMr OE7d11n3iuJQ2RYofEbCgO+QOnUd/VJBymuJwptWKSrNpl/2KHwXwLSi7sMU0lEbwnzo QBEsGP1s6aEaSXZM4sbPnI+AO3MeekXJImxa86xu+T5CNrPD/7CK0HPU7EM5lLbKtZVQ SJrFNNVf/kbM8C93fXDdC/zle3v5DknyL5qK7GOLdcdQvTljHCl/l+IxU0HrTCrjcyvm Ok0w== X-Forwarded-Encrypted: i=1; AJvYcCU7BRdTNP6bXNJj6Prf6pGEiATy0zEqrzqdTSK8T2a9xafRPpfZCcyNKhsFtBMh30JVWVq+nlCLRO2U19fgmhuXW7Y= X-Gm-Message-State: AOJu0Yyx7OFdx/LJGnQutyeSn+kdRLB1gw9aljc/y6ByB3PZj0t3tFXs Pfl8bopdvvmVW6gzMHTBQNjq8tYTkMxB8dc4GRS38dqCSPCUbcgY X-Google-Smtp-Source: AGHT+IFBNlYMlCcz/DGQe5EopNsp5sX1l8GX98RpBxhXVTVvNuB838o6ZTSivCDHHrKiErh8z5GlKA== X-Received: by 2002:a17:902:c943:b0:1fd:9269:72f0 with SMTP id d9443c01a7336-201d64a5b41mr16614065ad.47.1723600992349; Tue, 13 Aug 2024 19:03:12 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201cd12f0bfsm19898655ad.61.2024.08.13.19.03.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Aug 2024 19:03:11 -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 v4] mm: Override mTHP "enabled" defaults at kernel cmdline Date: Wed, 14 Aug 2024 14:02:47 +1200 Message-Id: <20240814020247.67297-1-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: F2CF912001F X-Stat-Signature: 7cmea6tged78tuyoun64md3zsahn3byq X-Rspam-User: X-HE-Tag: 1723600993-855332 X-HE-Meta: U2FsdGVkX1/I1xbyMiZppb7//a1dZRhEGikhpFHZ382t1K6hxs0f9/bXKhtNakNrWt2eWkH+wdRKJnhXOHnM49SjsCmAsU+57vl7rAiyT7loy6Kj5NUYvY/q0c5r/gaktB0cfW+6CYVpHKr5h7l+TjaP8B7dJx2EcCymRAUlFrs6QQJkTiE/KX6pyqOHKaQp/8x30F9Eibok3UdRky+47ZMMUDasAAESPtyDy0rlNzdJkOsvIjvyAKVVQv3OLpw+57UQ4JzWFB0t5iS9zkROeQZcLT8Pn2TG6LHOI8X7xdyaWtQGZXB/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NCgdsE7Xy54+OPySCul1Y2+tqr34NQJtLb2uw7BFBdq4UjIcJU7hADORnkIHHviaqXzcFOIXDWorRjYsa3V8= 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 Signed-off-by: Barry Song --- -v4: * use bitmap APIs to set and clear bits. thanks very much for David's comment! .../admin-guide/kernel-parameters.txt | 9 ++ Documentation/admin-guide/mm/transhuge.rst | 37 +++++-- mm/huge_memory.c | 96 ++++++++++++++++++- 3 files changed, 134 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..c5f4e97b49de 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,96 @@ 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 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")) { + bitmap_set(&always, start, end - start + 1); + bitmap_clear(&inherit, start, end - start + 1); + bitmap_clear(&madvise, start, end - start + 1); + } else if (!strcmp(policy, "madvise")) { + bitmap_set(&madvise, start, end - start + 1); + bitmap_clear(&inherit, start, end - start + 1); + bitmap_clear(&always, start, end - start + 1); + } else if (!strcmp(policy, "inherit")) { + bitmap_set(&inherit, start, end - start + 1); + bitmap_clear(&madvise, start, end - start + 1); + bitmap_clear(&always, start, end - start + 1); + } else if (!strcmp(policy, "never")) { + bitmap_clear(&inherit, start, end - start + 1); + bitmap_clear(&madvise, start, end - start + 1); + bitmap_clear(&always, start, end - start + 1); + } 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))