From nobody Thu Apr 9 12:54:02 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 5D1DA396B8A for ; Mon, 9 Mar 2026 11:08:16 +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=1773054497; cv=none; b=OABZHRONI8Mu5h96VsFkHMlAP2/5DaDz8juiMsbes+vVtX53/1u8+y17l8SjfiiLaT0ERuDY1e17ZbPbFULhQO3HKhnyf8AE/xiWIQTFtsJZ7svEXOC4kb7uBrxShSbFKW3XTLChR5JoHsN9j7HassaxW0maRGfDD+mSiPZAEXA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773054497; c=relaxed/simple; bh=0o3mclXhx2QxfkKcINCvef62gv9C6nChRQktfQw550o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cfcf1n265l0x5Ym/2dXEQDcBR6vxbwV4rWM35BI6KoieIOmk60RdLbiBqWRwrUggCegu3/Lj+b2FUk8YiRex8t2cua8Ma2R9n+0d1YraDmSZbXOMOB0qBu6ZOVeX/D5V7rLtyHt0S/VcmuwKtntrgep4RACNlYEeDzXH/rlT+bE= 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=aJzk43P5; 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="aJzk43P5" 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=vDF64xxZzJ7cjs3+cnvijW6JKtdwRCzUCIJnt/1dnjg=; b=aJzk43P5l/5qDBrMnOPgN+KrlX AQBPF2JgaxiMCcO3LXDwmIXQWXLfGBdxeOIiQ1ZZvd/DmkXKZKg096kmoDB8HlBzwdnViag37651c cF7wY4nfX66lPUKnHytO3Qn/mu0fWkMxy4ZAi3xKLVqrvvi7OW99YObl8ACPV6qhWHYxcL5PB1qYb QW6LBwvhbag0pqw5WplP9emxj1krIjNhIb1fVkb38paf/iuFkLMYl4sp/2YgQukIOkw+9fEQfq5pj LHmf2UuIoEijkYDFdOisUKq1cF9fXQmUbr9PKzu8KfAvVX22CGMIFom3NXIaXfIb+0CBwU4jbilrR /pOKJnPQ==; 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 1vzYSw-002AwY-Kj; Mon, 09 Mar 2026 11:08:03 +0000 From: Breno Leitao Date: Mon, 09 Mar 2026 04:07:31 -0700 Subject: [PATCH v4 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: <20260309-thp_logs-v4-2-926b9840083e@debian.org> References: <20260309-thp_logs-v4-0-926b9840083e@debian.org> In-Reply-To: <20260309-thp_logs-v4-0-926b9840083e@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=4428; i=leitao@debian.org; h=from:subject:message-id; bh=0o3mclXhx2QxfkKcINCvef62gv9C6nChRQktfQw550o=; b=owEBbQKS/ZANAwAIATWjk5/8eHdtAcsmYgBprqn+AeoIXBGWkxLKEcTZncPU7aYGAQAdEASX2 BOq9g76TpuJAjMEAAEIAB0WIQSshTmm6PRnAspKQ5s1o5Of/Hh3bQUCaa6p/gAKCRA1o5Of/Hh3 bR9iEACr0Gx5HEv/nW2mooEN34Yye3riL+2G1zLa7DFoqSg3g0ghDMjnXKNf/jm6074lXwEqFp1 mSFCL4U17ZCIyasG73+lRhzDl8RvYlOM9f50Wvt0o/9zkrjvxGAsblskzElCdsSRokqsgGCpqJ1 R2lR+CyErBfvMz0A05PNRxkjjA+DiwulGVh5SJs0eXFJcGfqTpDGHuboRHq8qMpBEFiCoSCVIoQ WtdO8GdzMfZaKPD6it2vV6BLt6pCaWGO8bbonSGekSevOqTpXu8TGJ68jGcy6F5RdhgsuJdlavj Q7L6BZDNDQyRRCmVuD0CS1fEu4HluJufR/w0Wv4dA40v9e+YYgUnhY7+6zcOI1D1T+d7tPWpLQ+ 4QztZzAAoNjek1IDON//L4DMVHDfyRKA453v1NnRIFlBt5rvlBff365DWbhSuL41kFh46v9lnZH A03UOPjHUenBllemwNnde0kR0aTsuKXkRWJyoXvT88JinUkuIEfG8qhlpFSw05wOxgWwIo5PmlL Ugcy/Rj8HkwBteRwLIipr9tGoEWfD5Oo3UBTJbxePWEOwy8Ef/sSCuSr4XQzMk33Am2ixKwUCae AF5VYC5n6YIsWUyem4CSaD3ZuYSC7/LWemzEcx7wDSNzfLjvAgGnS/RhxvWmDbRmoEjG6nVefz4 skxojNfuFFwZhUQ== 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..2d5b05a416dab 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_MADVISE =3D 1, + ANON_ENABLED_INHERIT =3D 2, + ANON_ENABLED_NEVER =3D 3, +}; + +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, + }; + enum anon_enabled_mode m; + bool changed =3D false; + + spin_lock(&huge_anon_orders_lock); + for (m =3D 0; m < ARRAY_SIZE(orders); m++) { + if (m =3D=3D mode) + changed |=3D !test_and_set_bit(order, orders[m]); + else + changed |=3D test_and_clear_bit(order, 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 (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