From nobody Thu Apr 9 23:26:11 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 26C533A6F07 for ; Thu, 5 Mar 2026 14:05:46 +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=1772719547; cv=none; b=id+lJZXBH2FP2dCYnwmg9EFEwHKOVV78ds8HJoGpjNAqe4vZt4iyjPmz4zW1TDr7HbSKpKgtdKZML4W1uq11hiNmzdFN7LMSJLRpd2N91JmmNNdLT12tmP6oza/Uv6dZrayIVRuk/NQkIXNfzmE57H5C4g8h+6UdJFThJY9sDTY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772719547; c=relaxed/simple; bh=8MvG288THhLWfJJ4mUaYo/3vClGLv+WQIbkWm1MV/5g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=A90Hvuo5gBp158XSfeJJqteCPAn7mHWXOCI4rCAOxP2BBZxrPddVTcN/ItUC22fdRsu24oTncEL4GXMuaGWcfXhGqzx616zklI6M+aqUH4xq9UKA7jPt12VeN0cemaKBHdhhy0GCfDemeA5g2d0l9QCnS+BRwKVXgod1Bwd/loU= 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=nGT27+Xr; 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="nGT27+Xr" 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=yzjxtLVX4aZ9Xn4loaE8IU9XMnkErcOMpEom5jghF4I=; b=nGT27+XrEKQ+huhZhpfxS0syN9 k5VL6LypWaEv5PGfKlW2uvE4ED2Mhw1gbdQ3n1vaokEQNV5VBWkIZOwp1R5V+xab2eHSqSO1OnnLQ v5zfEdjzDDxNY0rGueBCN4Fj0DhZoxdTb/GxEHpECxA6y5gUmLNBt8YOKM2qSojGfHOsUYxbMwbqy kfYcZxT2DogeWbaTT93Il/oIUV++Jsi64GHhnbjvDjLPQbtvsXUPUFnu7IGdT7lwlMK1Y3IQ5Ysr5 Mt15Zhkvz1mutWR4rUe74xdd7IRyX5bc9Ds6UGC4y0YGMYIPVgY2K7TyCeFvioEwWJ8OzcoK/007i //R5zWWQ==; 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 1vy9KR-00GmPM-Sc; Thu, 05 Mar 2026 14:05:28 +0000 From: Breno Leitao Date: Thu, 05 Mar 2026 06:04:54 -0800 Subject: [PATCH v2 2/3] 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: <20260305-thp_logs-v2-2-96b3ad795894@debian.org> References: <20260305-thp_logs-v2-0-96b3ad795894@debian.org> In-Reply-To: <20260305-thp_logs-v2-0-96b3ad795894@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 Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, usamaarif642@gmail.com, kas@kernel.org, kernel-team@meta.com, Breno Leitao X-Mailer: b4 0.15-dev-363b9 X-Developer-Signature: v=1; a=openpgp-sha256; l=4284; i=leitao@debian.org; h=from:subject:message-id; bh=8MvG288THhLWfJJ4mUaYo/3vClGLv+WQIbkWm1MV/5g=; b=owEBbQKS/ZANAwAIATWjk5/8eHdtAcsmYgBpqY2VV0LVf2oYQNaI+auUogab27ry65Y9dWDR8 BU2P6Yfy9qJAjMEAAEIAB0WIQSshTmm6PRnAspKQ5s1o5Of/Hh3bQUCaamNlQAKCRA1o5Of/Hh3 banID/9UReNmdhAtIZuZaJRtVCeXACeRbunZZfigbiFNNDGMGi8v5jiVxQXIk8fxr39QS+miVE9 bEusC1qDWuse5TWtbybwP3w/AfdKED/W/zVdhzckn12kXlIAG8SYmv77oJ2GvpUDrfX5NKKU7TO moK9pJLUF/617hEyjPiTwi65510wL5PE8pEsZke+Gzydr0Lw1OBI6hukmGU6EYc9RytuUezdv+5 yjpiqOMS9TGQMqsFBZwGZUv6StA7/O5L4v8avBS+u16s5cfVMrtnFL6LKp6T7monXPJO5c1ly1K ZAWY/F7rfNtBzjWMwKpAjrGrMKkaoP5y1L1q3CVH+3GiDSb5Hml81XW1pIbCsJ4GWDhUe81PzfZ Cr8XGJdVGHY26Dpyqf/wKCz7ECztK/SotG2uLv9/B0whwZoXnD0uRBe6oGYqeC0qZI13BGiskGf gH8HmkFZyc+YYC/BWzoSn7sJ75clslKyKFxRzoB6CSuawfyxbXuhFu9RTS8EMIPvOwYFHHYHdgP 1YhX+2y6NxmQVRTB4hBTzbh5ynQOyLNtXvRhXv24LSi/TbBzvpks1B+19Qdp3qsKu7GDF4Uhea3 Sbk35K/TwcyAapxYvnn3I/+95hywmneMVM4mmqFujSLr6M75oA7iTMdQsZIAsPXOyhDwpwxBfSi 0/w+qeowpgy8gTA== 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 enabled_mode and enabled_mode_strings[] to be shared with enabled_store() in a subsequent patch. Use sysfs_match_string() with the 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..19619213f54d1 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 enabled_mode { + ENABLED_ALWAYS, + ENABLED_MADVISE, + ENABLED_INHERIT, + ENABLED_NEVER, +}; + +static const char * const enabled_mode_strings[] =3D { + [ENABLED_ALWAYS] =3D "always", + [ENABLED_MADVISE] =3D "madvise", + [ENABLED_INHERIT] =3D "inherit", + [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 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(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