From nobody Mon Apr 6 23:36:05 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 57D533054EC for ; Tue, 17 Mar 2026 16:10:43 +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=1773763844; cv=none; b=eWncKWTW9NGFBswNvVyM/1o6dLlU+NCCb5b5xl8ukuNzuDoV73DsCJz+MJhIYWRF5nxkrBoZW/uccbYKN2gT/A0GIzPi5sucWHQdtzpLVqu8Kv/d/BYk8N/aE/uZkQkU/ZWy/X4StsjFIzFj2SAnO8LjHWF8fHiLKXNWIMCBwic= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773763844; c=relaxed/simple; bh=uVMygvKt3H9/z7XcvkSzsG4Gn2PVtGxvuzCVVJiXtVM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UcvuxUiaE3ESsRfow86wuc0N0Sv12pVQyAkkHbeIiKL3XHKJMiDPmn8DTl6wApmwL05zfE+lNB0hqG9+gjt+ypOj7Y1VK46Lqr0UyXvWI+MrJwqVs+t53XiuTxhz0FhBi69MgtyWACOeoxqbdEAD8KRZdVSCb8xuzscGbPWLQsM= 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=CtSe43tP; 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="CtSe43tP" 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=0FFhVCvaiK4sCPHtuUiD0P0I+e7xIUEsukRzL1BWPx8=; b=CtSe43tPytaRWUYVmPZ3ImM0bd jctNcyYVgObxGlyQdHxG/Jk1Afsl8kk9gTvJnGMq3vHRYQRHICN1DMF/bFBRnomPhAwpeZmKl0FQI fL6qPvGnwVtBvwPtGNpcjzenlDNwzCVX7R4Gk5lxcEfhP1/fkjtzZgDQf6HUFYlOnTAvPJuEwbbh5 KeTbQzTPtQpZMUaLljjvh6HFyiuQpkP92uduNo/TJ0CtFR81alBmOAJkMa29ddkD4BX9cZQyNrGw4 P0sz/jszwNtpaQ7P2WFfLY6WyQVsG6RXqjouO5YpRFm54x6TH3ock6KjXxw4b1ZzC6PTOvYAPWkGZ TYcTyzHg==; 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 1w2X0B-0030Co-9e; Tue, 17 Mar 2026 16:10:38 +0000 From: Breno Leitao Date: Tue, 17 Mar 2026 08:33:57 -0700 Subject: [PATCH v7 2/4] mm: huge_memory: refactor anon_enabled_store() with set_anon_enabled_mode() 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: <20260317-thp_logs-v7-2-31eb98fa5a8b@debian.org> References: <20260317-thp_logs-v7-0-31eb98fa5a8b@debian.org> In-Reply-To: <20260317-thp_logs-v7-0-31eb98fa5a8b@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=4477; i=leitao@debian.org; h=from:subject:message-id; bh=uVMygvKt3H9/z7XcvkSzsG4Gn2PVtGxvuzCVVJiXtVM=; b=owEBbQKS/ZANAwAIATWjk5/8eHdtAcsmYgBpuXzqHmsvblOOL0RHrdyBTRoL1xrQNiU5cacMj qU3CerKM3CJAjMEAAEIAB0WIQSshTmm6PRnAspKQ5s1o5Of/Hh3bQUCabl86gAKCRA1o5Of/Hh3 bfHHD/418gZu10Ilig+OwpDl3ktYXmF0E6sx4ody6Jz+y5mfGOLHu9xplhIyoBrx7N+KdRcBBZ/ f4fOyBpf6eKsapIiR4wI39KiOpZ3MMU8Val+gr1Oepj03YGGgld77RaZNhs7jddK3OfiN3mXn7q uOOpOsN4BWxRaCpXWtAufAA9FvUDLNJPHfswzqDLKoj9G8EggkyMp3cLPVh28u1gXwYkGFAOU/V JRoccNZ0IPhbygTmx9aeKSpbXMPfLLkwsuwezNHCJ7dcaUFysMKQsYjMvtdxWkGJvYT7sD+O9DS k8cHbV67LLSTLqXF54KuH9yzdRseb9WwuNB89a3FAqX0Y3BkYZ2kgcK+p08ZJ952Tm7XRo9WGnx UwjqY0bZhvP1qJ/elndvioRaDwFAbhgKbacL5VGjiDRog5miAB2qhrTPTJwUVWYs21+NH0MoV6P ahv1/phBBTWlfK3MnJT7xD69IYKRbuQSFtrAfcJJZ+C0sXrSWd9vOKaN8hx5ozQbt3AGLoqMoJA AAus2c7H6D7LrMKQmt1iZ8VCO36BNJ/O+Uh+6wV4jfU0pog6Jd/UUd9HUuzwjtiuK+d1NNn3V+k SKaHEaPkSPgUnTybPtWZTCtAI1qlqbvHwqv4cl3/achR7oGN74p2NMJzpA2+2CPnH2cfwJU3agC PFT3JdJeocGfiPw== 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 set_anon_enabled_mode() 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) Acked-by: David Hildenbrand (Arm) --- 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..f6af90e6cf05d 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 =3D 0, + ANON_ENABLED_INHERIT =3D 1, + ANON_ENABLED_MADVISE =3D 2, + ANON_ENABLED_NEVER =3D 3, +}; + +static const char * const anon_enabled_mode_strings[] =3D { + [ANON_ENABLED_ALWAYS] =3D "always", + [ANON_ENABLED_INHERIT] =3D "inherit", + [ANON_ENABLED_MADVISE] =3D "madvise", + [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 set_anon_enabled_mode(int order, enum anon_enabled_mode mode) +{ + static unsigned long *enabled_orders[] =3D { + &huge_anon_orders_always, + &huge_anon_orders_inherit, + &huge_anon_orders_madvise, + }; + enum anon_enabled_mode m; + bool changed =3D false; + + spin_lock(&huge_anon_orders_lock); + for (m =3D 0; m < ARRAY_SIZE(enabled_orders); m++) { + if (m =3D=3D mode) + changed |=3D !__test_and_set_bit(order, enabled_orders[m]); + else + changed |=3D __test_and_clear_bit(order, enabled_orders[m]); + } + 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 (set_anon_enabled_mode(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.52.0