From nobody Sat Apr 11 08:55:19 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 27A35303A12 for ; Sat, 7 Mar 2026 16:08:52 +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=1772899733; cv=none; b=mop7/ku2qyyOQtSeFuDMkvbQvmzYgVUTUlJFpKxOgocrZq5qGl2ysJE4LBKWryj3JhJso84v77xcjDXHt6O2V0uwFCYZgauc6eqJa13h1xdrEI2uYvzn53ojTCcHx+oVa8kK6AKe30PnL2MgOJc2K1xq0/snUcB3p/Q27flVmYw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772899733; c=relaxed/simple; bh=zlsaZ8kaCXcEEWIfMU8ynz2khbS/0r7SuYWkIjlj6TQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gGLvu8cTMP6tU8N9ByORR9aVr1bgnGqNrhOtt+Reoxeh7n4Mx1asyE4aOgIO3e44L7VPgSa2fSDJh+LC7SidSLaXb81Wu+qwLquiwroBvEjz0/QK8HStneyUd305HBgd7gjYR5Tzol2onPL1wrpbojZrP0jP2KKulyO/xb2haEI= 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=EI/k/aux; 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="EI/k/aux" 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=3BNLcjJkZMBW12wECnG4ZYadEB66BxAZDl3NHar6Yso=; b=EI/k/auxY1T02XN/UP4PqDzHrL slPATE+jk30i/S8biwszCh2iE4Zb9ARBGw3CwesLF2KbuWagwu2y6Ij5TVb5x+sF4ifvze9eXfOSV iDc13K6BCgrHCxqorCEn1VptlJ636ttS64jUZi8c5JHGOryv/v/dVLra6H1eQkapFGWlgnWp3WS41 hzP8625taHtht+cUPTPSD4Z5vGBX3WdedzDYXjyDJhCxaIcQWx1iZWpW1WeED0/z8rsNsJNA0xvPz K0JG9XlVQ5kpMWDv7vnUwCOego5wQasxcOv8tPAZB/AxTnOnvfPSgl5K14QG67sM5H+Niuu4xRHCs Sq9yzDgw==; 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 1vyuCv-000nEt-Hx; Sat, 07 Mar 2026 16:08:49 +0000 From: Breno Leitao Date: Sat, 07 Mar 2026 08:08:07 -0800 Subject: [PATCH v3 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: <20260307-thp_logs-v3-3-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=3425; i=leitao@debian.org; h=from:subject:message-id; bh=zlsaZ8kaCXcEEWIfMU8ynz2khbS/0r7SuYWkIjlj6TQ=; b=owEBbQKS/ZANAwAIATWjk5/8eHdtAcsmYgBprE14/7kDvB5SOmDe7Zro8AP41H6s3+3zeHxI+ EMzX91akimJAjMEAAEIAB0WIQSshTmm6PRnAspKQ5s1o5Of/Hh3bQUCaaxNeAAKCRA1o5Of/Hh3 bUytD/468PRW2gj7l60eHMlh0rUTgGs0T/PmzAmGB06mt60u9k2lCaZFJIkJb48s0st40J0Ayrk sEOPprlA5VYar59IcpJdlYNtT9w3y5XRYKM4b+KDdtiUq5I2Q5sP66AXoaN+NJ7tbKU0Wzn4Kl3 pqtnSEzHF+Zt/AdlWunrLSj2MLYrGXl/9FWm2YJV4rWOD4MKQ+UYJ4UA9wFvdRO1h0JTjxQwwlS zZeoKNAYP3JznzCuKAgWwKcO+YtGwwXq7PjzW7U3zmFOZueCAHhpkPQrKSkjcVusWszmquhbQa6 iAq90h29OzPM1mKC7pAgoMy65q82M3FLS9VtRdD2n/HS/eepPrVGioeD1fs1+RbBkESsRtL8jCB liFSaAXvCcnTG5kwcRwXZ19LYlzB0IlllVLwX4pHOIrVtASck9UwexFkSowU01AT3jPd2Wugkk7 JT1TY1NUpFxPRRistC1Oz5oZEC2axEnRscDVkQGRrjAKVQ8o6CR6+VmbnfedB2SvZsZ/1ReP/bH 7cQCszoDQXgkLAOftYf9PkK2YCrvUpIh3VOlosNdND1PbYpWFzGubSeDoBdfp4sbAPY/87GLdxi NufArKiPTDzlbNpkbq/THj1QonDLcwrrCKYOgY3FO+2bMMALJqUbHJeuFOOX//K5qybGHQ/gpmc 0chCT0kx5PyeUXw== 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 change_anon_orders(), 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. Signed-off-by: Breno Leitao Reviewed-by: Lorenzo Stoakes (Oracle) --- 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 17ce6a16702bf..c38c86e6d0c02 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, + GLOBAL_ENABLED_MADVISE, + GLOBAL_ENABLED_NEVER, +}; + +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 change_enabled(enum global_enabled_mode mode) +{ + static const unsigned long thp_flags[] =3D { + TRANSPARENT_HUGEPAGE_FLAG, + TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, + }; + bool changed =3D false; + int i; + + for (i =3D 0; i < ARRAY_SIZE(thp_flags); i++) { + if (i =3D=3D mode) + changed |=3D !test_and_set_bit(thp_flags[i], + &transparent_hugepage_flags); + else + changed |=3D test_and_clear_bit(thp_flags[i], + &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 (change_enabled(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.47.3