From nobody Thu Apr 9 18:03:04 2026 Received: from stravinsky.debian.org (stravinsky.debian.org [82.195.75.108]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5936D30EF9A for ; Sat, 7 Mar 2026 16:08:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=82.195.75.108 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772899732; cv=none; b=KRfkN5mBN6/ER2Top9kVwqFloZG7iGypiW7niBsKqHyGAmCcdDXuJMDjfg+AQPdyBDtw73TJapJcORqBkHSqT5jqlj0d9IzC6PQMY9mfF3LLNS3QDmqQnS5NDE7rwCpq9GGYd3HthoMNKoPYA4QtcrnnqetZWYJbw5u7/ltfDeo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772899732; c=relaxed/simple; bh=/1eLIdHPUDcvIpxBXzEH0x3IBBqIpnLjC8GGQWUs0vI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=JqHzDeLjajBPWUoFtmzdd+TiuFvltfhM3KQKTTcsIfa8geV7lqTuYPc/3d7jI+5jEwgOESkn+fpnMsqKvGJR6onPiGIzdt7gJQ3R8LRHlLMzZG8LvbytUpOeUTSD1XyXCGShvDz07B7mvKYpvSccouOSRFu7nnp6e9RgTHsVivU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org; spf=none smtp.mailfrom=debian.org; dkim=pass (2048-bit key) header.d=debian.org header.i=@debian.org header.b=RpL+LiTn; arc=none smtp.client-ip=82.195.75.108 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=debian.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=debian.org header.i=@debian.org header.b="RpL+LiTn" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debian.org; s=smtpauto.stravinsky; h=X-Debian-User:Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Reply-To:Content-ID:Content-Description; bh=MmDCySQR75AVk0G7ci1h/kEC5miQyqWVJmvwkT82m+0=; b=RpL+LiTnoiDsgV78/8WS8ttgTb ZsmBF9ssOwmpVcJlDEZKbeS+gtSQZVlX/sp9Pzaexm/PQ8bCXwBe+PKAEb7Y7DOHBcFOt3NgySK4Y KGRiZrzGYmZXWS/bxOGeAZ8ccEsZgULBWbwBe/xBU//J9kaSj6HuZDZI6cfUpGHFfIr1NykXCh9f2 6WzeeUg56bBtiDLxStoKQKwoqNsWmoARKN6Drn7Y02tgeg6BwFS4kLI7HMOinhnBTNDcfj46943ge 8GnG+13pfC2Yx+w5U8CdMATDHxlux0G8agRf9xawP/UA62J9YZMKFSIXEhUC5LOuAydsbV8XJBYXP 6SUJ0KSw==; Received: from authenticated user by stravinsky.debian.org with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.94.2) (envelope-from ) id 1vyuCp-000nEg-CY; Sat, 07 Mar 2026 16:08:43 +0000 From: Breno Leitao Date: Sat, 07 Mar 2026 08:08:06 -0800 Subject: [PATCH v3 2/4] mm: huge_memory: refactor anon_enabled_store() with change_anon_orders() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260307-thp_logs-v3-2-a45d2c8f3685@debian.org> References: <20260307-thp_logs-v3-0-a45d2c8f3685@debian.org> In-Reply-To: <20260307-thp_logs-v3-0-a45d2c8f3685@debian.org> To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Zi Yan , Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Suren Baghdasaryan , Michal Hocko , Brendan Jackman , Johannes Weiner , Mike Rapoport Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, usamaarif642@gmail.com, kas@kernel.org, kernel-team@meta.com, Breno Leitao , "Lorenzo Stoakes (Oracle)" X-Mailer: b4 0.15-dev-363b9 X-Developer-Signature: v=1; a=openpgp-sha256; l=4393; i=leitao@debian.org; h=from:subject:message-id; bh=/1eLIdHPUDcvIpxBXzEH0x3IBBqIpnLjC8GGQWUs0vI=; b=owEBbQKS/ZANAwAIATWjk5/8eHdtAcsmYgBprE14ggv8eqItj5fMOtP7NI8WiFHHpkVrd8wfU TZwIe4ToJ+JAjMEAAEIAB0WIQSshTmm6PRnAspKQ5s1o5Of/Hh3bQUCaaxNeAAKCRA1o5Of/Hh3 bYzMD/92I3HOSF/fOgXxnBNEfHeYuzs1HW42exZ5kHHQkpteZS92Ko3J5hADsYJlPZNUV+taKBj Vx3SmbJLfn34uCfU7hposW1qqhrmGAVZKZv9VL88hNtFpjv0ryqTU4HPpADkCvTg375TCzQyIy5 YaqOkGj2mTacqGZk+9QrfwcfziakMA4GJ52pihbWktcxd9Z2IywFp08P8NF1MJ8GF2nlGEfTRqi TkXaKCEvD5EU/DfpyLbKTLvPMX0LNgrH7iFcVYL0hdREE8p8lCefYnr1AKjynzCt1KmAqZCPr0h TNCQsQXmDzbACkSxJpoX192zsKtCWhyK9Dv666ky3tbvFp7rRzhvexjEcry0lPiWRaOPtFQwomX tOqmLA0cKVqmrxniyd6gN14LHULh9mRuCBUDFpj7hCRuqFRe1ce8ayLIekLrqCLFbAaW0iALa5u EKgHNOUL00Gn8LXYEIe62shrQHs1Us6XMoUTJzFxm4Sdb5AFv2f1yvWn+XTs1qy3Pf170XZEiZw SmPDHR8cUGNTMkuxv2T0fWbZFb+q7USUs2iZKdg8y9vJFArx+UBHJhNWqWUIwHqS4oEQqIPyBmn 5yzXQuxR/WyudBuWUGebEGk6HyvQXQZwTy2Rpjn5Sj87G1BI2GggFqGyJ9NlzLJqsJkXdGeXUY9 GGeXafGfOTgHzKw== X-Developer-Key: i=leitao@debian.org; a=openpgp; fpr=AC8539A6E8F46702CA4A439B35A3939FFC78776D X-Debian-User: leitao Consolidate the repeated spin_lock/set_bit/clear_bit pattern in anon_enabled_store() into a new change_anon_orders() helper that loops over an orders[] array, setting the bit for the selected mode and clearing the others. Introduce enum anon_enabled_mode and anon_enabled_mode_strings[] for the per-order anon THP setting. Use sysfs_match_string() with the anon_enabled_mode_strings[] table to replace the if/else chain of sysfs_streq() calls. The helper uses test_and_set_bit()/test_and_clear_bit() to track whether the state actually changed, so start_stop_khugepaged() is only called when needed. When the mode is unchanged, set_recommended_min_free_kbytes() is called directly to preserve the watermark recalculation behavior of the original code. Signed-off-by: Breno Leitao Reviewed-by: Lorenzo Stoakes (Oracle) --- mm/huge_memory.c | 84 +++++++++++++++++++++++++++++++++++-----------------= ---- 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 8e2746ea74adf..17ce6a16702bf 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -316,6 +316,20 @@ static ssize_t enabled_show(struct kobject *kobj, return sysfs_emit(buf, "%s\n", output); } =20 +enum anon_enabled_mode { + ANON_ENABLED_ALWAYS, + ANON_ENABLED_MADVISE, + ANON_ENABLED_INHERIT, + ANON_ENABLED_NEVER, +}; + +static const char * const anon_enabled_mode_strings[] =3D { + [ANON_ENABLED_ALWAYS] =3D "always", + [ANON_ENABLED_MADVISE] =3D "madvise", + [ANON_ENABLED_INHERIT] =3D "inherit", + [ANON_ENABLED_NEVER] =3D "never", +}; + static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) @@ -515,48 +529,54 @@ static ssize_t anon_enabled_show(struct kobject *kobj, return sysfs_emit(buf, "%s\n", output); } =20 +static bool change_anon_orders(int order, enum anon_enabled_mode mode) +{ + static unsigned long *orders[] =3D { + &huge_anon_orders_always, + &huge_anon_orders_madvise, + &huge_anon_orders_inherit, + }; + bool changed =3D false; + int i; + + spin_lock(&huge_anon_orders_lock); + for (i =3D 0; i < ARRAY_SIZE(orders); i++) { + if (i =3D=3D mode) + changed |=3D !test_and_set_bit(order, orders[i]); + else + changed |=3D test_and_clear_bit(order, orders[i]); + } + spin_unlock(&huge_anon_orders_lock); + + return changed; +} + static ssize_t anon_enabled_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { int order =3D to_thpsize(kobj)->order; - ssize_t ret =3D count; + int mode; =20 - if (sysfs_streq(buf, "always")) { - spin_lock(&huge_anon_orders_lock); - clear_bit(order, &huge_anon_orders_inherit); - clear_bit(order, &huge_anon_orders_madvise); - set_bit(order, &huge_anon_orders_always); - spin_unlock(&huge_anon_orders_lock); - } else if (sysfs_streq(buf, "inherit")) { - spin_lock(&huge_anon_orders_lock); - clear_bit(order, &huge_anon_orders_always); - clear_bit(order, &huge_anon_orders_madvise); - set_bit(order, &huge_anon_orders_inherit); - spin_unlock(&huge_anon_orders_lock); - } else if (sysfs_streq(buf, "madvise")) { - spin_lock(&huge_anon_orders_lock); - clear_bit(order, &huge_anon_orders_always); - clear_bit(order, &huge_anon_orders_inherit); - set_bit(order, &huge_anon_orders_madvise); - spin_unlock(&huge_anon_orders_lock); - } else if (sysfs_streq(buf, "never")) { - spin_lock(&huge_anon_orders_lock); - clear_bit(order, &huge_anon_orders_always); - clear_bit(order, &huge_anon_orders_inherit); - clear_bit(order, &huge_anon_orders_madvise); - spin_unlock(&huge_anon_orders_lock); - } else - ret =3D -EINVAL; + mode =3D sysfs_match_string(anon_enabled_mode_strings, buf); + if (mode < 0) + return -EINVAL; =20 - if (ret > 0) { - int err; + if (change_anon_orders(order, mode)) { + int err =3D start_stop_khugepaged(); =20 - err =3D start_stop_khugepaged(); if (err) - ret =3D err; + return err; + } else { + /* + * Recalculate watermarks even when the mode didn't + * change, as the previous code always called + * start_stop_khugepaged() which does this internally. + */ + set_recommended_min_free_kbytes(); } - return ret; + + return count; } =20 static struct kobj_attribute anon_enabled_attr =3D --=20 2.47.3