From patchwork Tue Oct 29 00:13:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ma=C3=ADra_Canal?= X-Patchwork-Id: 13854296 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 9D986D5B851 for ; Tue, 29 Oct 2024 00:24:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8AF586B00BE; Mon, 28 Oct 2024 20:24:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 832226B00BF; Mon, 28 Oct 2024 20:24:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 65F506B00C0; Mon, 28 Oct 2024 20:24:11 -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 34AB06B00BE for ; Mon, 28 Oct 2024 20:24:11 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id CB224140839 for ; Tue, 29 Oct 2024 00:24:10 +0000 (UTC) X-FDA: 82724741904.21.1CF6950 Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by imf13.hostedemail.com (Postfix) with ESMTP id 713F420006 for ; Tue, 29 Oct 2024 00:23:44 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=igalia.com header.s=20170329 header.b=Sdshq8td; spf=pass (imf13.hostedemail.com: domain of mcanal@igalia.com designates 178.60.130.6 as permitted sender) smtp.mailfrom=mcanal@igalia.com; dmarc=pass (policy=none) header.from=igalia.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730161369; a=rsa-sha256; cv=none; b=7dzDDaM43LvH6g/OoCZNM86R4yC2vA5q7YWIfee/kzbPrC9q+f2hYBjz3I1lPMHlsG7L3/ 3hypbDvVa/6EvmxbOG0P4vSopw0X4ouxzi5YNuhkPumqjbKGKlTF/oW2Z5o+MzQqEz2pqA SArlKrZbm1ThA9kZ7uI9+p6VsvuHbgs= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=igalia.com header.s=20170329 header.b=Sdshq8td; spf=pass (imf13.hostedemail.com: domain of mcanal@igalia.com designates 178.60.130.6 as permitted sender) smtp.mailfrom=mcanal@igalia.com; dmarc=pass (policy=none) header.from=igalia.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730161369; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=w+ns6OSoZMKNPdhNYQfwqi4ljcYi8WU+6sPV1i33TG4=; b=LqRiTgUwhhTWE/gFmPNpApWZNCjXq038XF+f1qMET5J1ShwOFXCzUiMaHAF7Z9mUNMRVlj oZ2q2lyxr5UslRgKczMeO/eeZtSG3Hvsub5O2t7CZs5C4oluwIbNi9doEFnVWeT2H7wrD0 n4GecBYpQPUz5O2T3l7RLGONA9UlNsw= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=w+ns6OSoZMKNPdhNYQfwqi4ljcYi8WU+6sPV1i33TG4=; b=Sdshq8tdeTIIZrwGdLocfz8Z1U /2xLHI03EpXyzG964+QjuqUdm7LDnIh4Fdiefj+fzZ0ATq0IkgA2c9zEpJ3pJlnMgD7k9smvtGXkj hNzBnBZuh21B65Mq9xtip4Ughf7wt4IwyKJ+siF42LigZgZe1s35ifUaMxw+H8QVKh+5hFekWf6pV 9LdZlYmo9CCSvlkAMg8AqIn6xg6TPWB1FxT54SgAP8jMTLU+4xmdR9e6Gs9IAuDAyfdgJtWaa1QbK uP+1i0vmjaqC8pP3eE0NtDIBhZvDEBPSmyiTJav4TEdkcRhY6TL1w/oFvVRNU0g9q/mNQLqWviQ3N PxRkKfNw==; Received: from [187.36.213.55] (helo=morissey..) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1t5a1f-00GIrd-8G; Tue, 29 Oct 2024 01:23:59 +0100 From: =?utf-8?q?Ma=C3=ADra_Canal?= To: Jonathan Corbet , Andrew Morton , Hugh Dickins , Barry Song , David Hildenbrand , Ryan Roberts , Baolin Wang , Lance Yang Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-dev@igalia.com, =?utf-8?q?Ma=C3=ADra_C?= =?utf-8?q?anal?= Subject: [PATCH v2 3/4] mm: generalize the implementation of ``thp_anon=`` Date: Mon, 28 Oct 2024 21:13:07 -0300 Message-ID: <20241029002324.1062723-4-mcanal@igalia.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241029002324.1062723-1-mcanal@igalia.com> References: <20241029002324.1062723-1-mcanal@igalia.com> MIME-Version: 1.0 X-Stat-Signature: dqf617q9igzenmfpgmqzphoh6qeathjf X-Rspamd-Queue-Id: 713F420006 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1730161424-857403 X-HE-Meta: U2FsdGVkX18aMpAkcfShqHimosyKOaSXi5B/lKHXVBwMibMitHOe6zTj0nv7szCZVRK4Dt2+MVol0NwqzqbxTeTN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kMBW7c9yvQu1yV9/d7XvlVOncAh804q7Nt1gnpd1tF0oKsFy2j+oV8rYEn3NMMCFZymMSGUO7MV6MQY3dfOhUwUek/srGwEy9OzHsTe5tsoyO0aFZXQUJCnid5ZmqLTZ7FxDHNJdaPEcu4eNQqlJzQBu2q5X3/aWwh8g1iYceUr/ldp0MoCqrRJ67TFyXMcL4yhFRuV+f 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: Currently, the function ``setup_thp_anon()`` is extremely tied to the policies and bitmaps that currently exist for anon THP. This means that we can't reuse the function if we implement a ``thp_shmem=``. This commit extracts the behavior of the function to a new generic function. The new function is exposed in common headers for future use by mm/shmem.c. Signed-off-by: MaĆ­ra Canal --- include/linux/huge_mm.h | 29 +++++++++++ mm/huge_memory.c | 109 ++++++++++++++++++---------------------- 2 files changed, 78 insertions(+), 60 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index b94c2e8ee918..b82e9379f2bf 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -471,6 +471,35 @@ void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, pmd_t *pmdp, struct folio *folio); +static inline int get_order_from_str(const char *size_str, + const unsigned long thp_orders) +{ + 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) + goto err; + + return order; +err: + pr_err("invalid size %s in boot parameter\n", size_str); + return -EINVAL; +} + +struct thp_policy_bitmap { + const char *policy; + unsigned long bitmap; +}; + +int parse_huge_orders(char *p, struct thp_policy_bitmap *policies, + const int num_policies, const unsigned long thp_orders); #else /* CONFIG_TRANSPARENT_HUGEPAGE */ static inline bool folio_test_pmd_mappable(struct folio *folio) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 832ca761b4c3..c61f4481cb6a 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -958,91 +958,80 @@ 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) +int parse_huge_orders(char *p, struct thp_policy_bitmap *policies, + const int num_policies, const unsigned long thp_orders) { char *token, *range, *policy, *subtoken; - unsigned long always, inherit, madvise; char *start_size, *end_size; - int start, end, nr; - char *p; + int start, end, nr, i; + bool policy_set; - 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; + return 0; while ((subtoken = strsep(&range, ",")) != NULL) { + policy_set = false; + if (strchr(subtoken, '-')) { start_size = strsep(&subtoken, "-"); end_size = subtoken; - start = get_order_from_str(start_size); - end = get_order_from_str(end_size); + start = get_order_from_str(start_size, thp_orders); + end = get_order_from_str(end_size, thp_orders); } else { - start = end = get_order_from_str(subtoken); + start = end = get_order_from_str(subtoken, thp_orders); } if (start < 0 || end < 0 || start > end) - goto err; + return 0; 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; + + for (i = 0; i < num_policies; i++) { + if (!strcmp(policy, policies[i].policy)) { + bitmap_set(&policies[i].bitmap, start, nr); + policy_set = true; + } else + bitmap_clear(&policies[i].bitmap, start, nr); + } + + if (!policy_set && strcmp(policy, "never")) { + pr_err("invalid policy %s in boot parameter\n", policy); + return 0; } } } - huge_anon_orders_always = always; - huge_anon_orders_madvise = madvise; - huge_anon_orders_inherit = inherit; + return 1; +} + +static char str_dup[PAGE_SIZE] __initdata; +static int __init setup_thp_anon(char *str) +{ + struct thp_policy_bitmap policies[] = { + { "always", huge_anon_orders_always }, + { "madvise", huge_anon_orders_madvise }, + { "inherit", huge_anon_orders_inherit }, + }; + char *p; + + if (!str || strlen(str) + 1 > PAGE_SIZE) + goto err; + + strscpy(str_dup, str); + p = str_dup; + + if (!parse_huge_orders(p, policies, ARRAY_SIZE(policies), + THP_ORDERS_ALL_ANON)) + goto err; + + huge_anon_orders_always = policies[0].bitmap; + huge_anon_orders_madvise = policies[1].bitmap; + huge_anon_orders_inherit = policies[2].bitmap; anon_orders_configured = true; return 1;