From nobody Tue Apr 7 23:42:41 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 675983C9ED3 for ; Wed, 11 Mar 2026 10:19:47 +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=1773224388; cv=none; b=pxitO5C0xLfEDWg2WLAr+SXcRwN5mxcDFTIGx8ML8VeTn3feqbAvHDn5ckDn8Y13uRd7SViM/MQXquxa8koHKdLvVWfZ+0ukwpckWSvtL/MehaVQPpF0y0EIuOKy3ixw1YnhE+KAAStFiMmCu3PocdCQOQNAgkN8KtGHlLyyn4Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773224388; c=relaxed/simple; bh=EdbjNIc4MU4kE7FF3eW32wB/WCGu/OZa4I2oST0exss=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=eZ9SsA0LSPwpHHoqykA3qpfkqCZfkSVufzbwGC2lWVPxOxaB2+6uywGTg3XQh9TSM11LaBDr+iW21VldhIguCh73aEZu0kfnfH4PNxgfS25oFwVVG9K0ROIL4Wo+uokAaE02F5GvwzGo7kdtsBjwBek7GOl91/YdqNNPkjbtBtw= 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=gbdGfQ72; 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="gbdGfQ72" 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=MFthAOwBMbfcPyydbWyKKsxIBB7Wpv1CRiBxp3jBdA8=; b=gbdGfQ72Pbj/yEZ+HcSy0He0BU THMv8H8sgx5QLH5+QNvjCwQ7O1bwTSV/Y/VtxS3fX29hK4Qd7jOdQwH1pP5p70wxrpvF/hBp++PVn uE/eSHxFSqxODC0Zjmx+e3pr+CVMWYiqw76MY0pMF5pUSJdC5hkX4NWfWLzNx+JILAntUY9LIfVBF mSPEHcEkz1NXVD4Z4mpmK5zeGr6qTXcfbUFPJB7tibAhEwV1ubTGdFenkKkAvLU7wf9VtovKdlj8O TyT7bfMtarLTr2qGC99g8v2xEK8od+ql8WRDTYxK7LvxmtPiREsS2BY2/bL4pFWw9r2NG2SrAocSQ uxcPo9Ow==; 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 1w0Gf3-003hKl-6U; Wed, 11 Mar 2026 10:19:29 +0000 From: Breno Leitao Date: Wed, 11 Mar 2026 03:17:49 -0700 Subject: [PATCH v6 3/4] mm: huge_memory: refactor enabled_store() with change_enabled() 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: <20260311-thp_logs-v6-3-421e30d881e0@debian.org> References: <20260311-thp_logs-v6-0-421e30d881e0@debian.org> In-Reply-To: <20260311-thp_logs-v6-0-421e30d881e0@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, "Lorenzo Stoakes (Oracle)" , Wei Yang , Breno Leitao X-Mailer: b4 0.15-dev-363b9 X-Developer-Signature: v=1; a=openpgp-sha256; l=3631; i=leitao@debian.org; h=from:subject:message-id; bh=EdbjNIc4MU4kE7FF3eW32wB/WCGu/OZa4I2oST0exss=; b=owEBbQKS/ZANAwAIATWjk5/8eHdtAcsmYgBpsUGZAPQLhkqPGTMyt0/1aIWw63IFWqb8fSKPJ 0SODewov9iJAjMEAAEIAB0WIQSshTmm6PRnAspKQ5s1o5Of/Hh3bQUCabFBmQAKCRA1o5Of/Hh3 bXU+D/9e57RxcBJQU/pf0opwkVMFvv5vy3sgdffVbMJgRkBmPaYK/7UJawozT2FS8nu+qdYqydL iFEPCb67wviiCZF5c0KPF0yxBNC/VugLcW7wXIgKOaAx/cv9EOS7M1sOwAp3VvLESblVIC/4xyO vdQqkQotDbrkA0VjaR+4Pd7b8M+OQquusCVPYCYbBCQfG8SK9zkkv36Rjw5fqNXMY6febQKijDD b1VN9k7rTxpP3yr9QZ4sGix6f4txLiDdSbJEhYLupIC3xMEsurgPHJo7GQq3dBeiO+VopPP3V3Q ios1pY9I6EmNkdhF7sGYbZbmDuiG6UPyCTnWvBA8ZLOvhjQmi0nfep3lvGa/Z+O3qGXGzqGAN/q i3KchWhMtTR5y0TA/Fm5ihINJZNeZzVLezfsBRdCKucit7o0Et8FUqc2PdkDqwL9SioSDIDziK8 4g3ikHY3emF3En81FczhKlbY5KYkmKJFqP9SPJjrCaBUqUM3vSqv7PnW9mG9fEWsHjvoYZPlEqv NjutbbaEKmkfztuvnTcpnyaRRFDPGZDWlmfcIJAO4utF3iC5fX4nGPtQgwtUQb28gqadOjLc5fA 5fWnpEUMP8zuq5HulJbyBvu4Lxpo0DMLuMWhyde7oxPcm5a+x89nmhCNkIzB+eG3VoySlzSkXhC o2gzwAQCYVRUs+g== X-Developer-Key: i=leitao@debian.org; a=openpgp; fpr=AC8539A6E8F46702CA4A439B35A3939FFC78776D X-Debian-User: leitao Refactor enabled_store() to use a new change_enabled() helper. Introduce a separate enum global_enabled_mode and global_enabled_mode_strings[], mirroring the anon_enabled_mode pattern from the previous commit. A separate enum is necessary because the global THP setting does not support "inherit", only "always", "madvise", and "never". Reusing anon_enabled_mode would leave a NULL gap in the string array, causing sysfs_match_string() to stop early and fail to match entries after the gap. The helper uses the same loop pattern as set_anon_enabled_mode(), iterating over an array of flag bit positions and using __test_and_set_bit()/__test_and_clear_bit() to track whether the state actually changed. Reviewed-by: Lorenzo Stoakes (Oracle) Reviewed-by: Zi Yan Reviewed-by: Baolin Wang Reviewed-by: Wei Yang Signed-off-by: Breno Leitao Tested-by: Lance Yang --- mm/huge_memory.c | 63 ++++++++++++++++++++++++++++++++++++++++++----------= ---- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index f6af90e6cf05d..8949393de7a51 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -330,30 +330,63 @@ static const char * const anon_enabled_mode_strings[]= =3D { [ANON_ENABLED_NEVER] =3D "never", }; =20 +enum global_enabled_mode { + GLOBAL_ENABLED_ALWAYS =3D 0, + GLOBAL_ENABLED_MADVISE =3D 1, + GLOBAL_ENABLED_NEVER =3D 2, +}; + +static const char * const global_enabled_mode_strings[] =3D { + [GLOBAL_ENABLED_ALWAYS] =3D "always", + [GLOBAL_ENABLED_MADVISE] =3D "madvise", + [GLOBAL_ENABLED_NEVER] =3D "never", +}; + +static bool set_global_enabled_mode(enum global_enabled_mode mode) +{ + static const unsigned long thp_flags[] =3D { + TRANSPARENT_HUGEPAGE_FLAG, + TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, + }; + enum global_enabled_mode m; + bool changed =3D false; + + for (m =3D 0; m < ARRAY_SIZE(thp_flags); m++) { + if (m =3D=3D mode) + changed |=3D !__test_and_set_bit(thp_flags[m], + &transparent_hugepage_flags); + else + changed |=3D __test_and_clear_bit(thp_flags[m], + &transparent_hugepage_flags); + } + + return changed; +} + static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { - ssize_t ret =3D count; + int mode; =20 - if (sysfs_streq(buf, "always")) { - clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flag= s); - set_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags); - } else if (sysfs_streq(buf, "madvise")) { - clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags); - } else if (sysfs_streq(buf, "never")) { - clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flag= s); - } else - ret =3D -EINVAL; + mode =3D sysfs_match_string(global_enabled_mode_strings, buf); + if (mode < 0) + return -EINVAL; =20 - if (ret > 0) { + if (set_global_enabled_mode(mode)) { int 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 enabled_attr =3D __ATTR_RW(enabled); --=20 2.52.0