From nobody Thu Apr 9 12:46:08 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 EA923395DAE for ; Mon, 9 Mar 2026 11:08:21 +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=1773054503; cv=none; b=eW5bJOlXVWtibmVfm7GzEuiKraqqhqK10JHT97SyXEKHmoRUdezMVEBxL0lV+nv09WZ+GCMY+M5NLUzXUeqns/SEVO8wJQbWdj5tmtfOU4A92Pw5tUFeL7b6vDB3QJVIWyLwkj9YQQzq8KiK+wRolIH1lmuCmovFJiDaBkFB614= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773054503; c=relaxed/simple; bh=Rps73ws/DtMzsKWxDZkfpvyl2AU0CyVMps0aEftLyqQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=t6iJ1+avRrvfO6a/GP5zzuRGvrOm2dNINv3QMgS8JIPCoWyFyCWM1BeT1S0+gzPDnWNdio5NtFBxxQSVT1DwI9/KHJf3cewkW72nS8Z3yEDxL2HVeRd808eMTihuu6UAuHhZlYgbHlXgk4V83LWCkVXaLomfp/7qQr5nTivdJQA= 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=dzrNIntp; 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="dzrNIntp" 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=N4HGQ0yYz7DIk6VexLHFU9Nzgo9+u3GkTdEP1uFtaE0=; b=dzrNIntpVxT6cyfGOLdS/AuFBn KoPTQiOlGIlz4i0FfjWTL1rfst+Cfpbs8MX8Gs0OPGcvUivBzWYnV6rwV5V9wmRKbNdjlIz0UlTE+ eXRAZo5/MqR1jBHSNZY8WEt4Hbd+hBFgMqB9FinSel9Q8+5w+JEKoX8l548D2mD8dEeFao86z6wvd fSno6VKnc+Liem6MWw45zcRapK4xezH4G//BySGJSiFcJ8RR+SfMwkWTAQt7k4ZREOw/MRsL7GkOH Oj/XtT4lulUVLKrfRiGBHoXrrL8oX2vTJLKFgliuBOifPPX4ruhHA+OsYyvKYhD2kI5s5DX2rwQAe 2hqhjrPg==; 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 1vzYT2-002Awp-P8; Mon, 09 Mar 2026 11:08:09 +0000 From: Breno Leitao Date: Mon, 09 Mar 2026 04:07:32 -0700 Subject: [PATCH v4 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: <20260309-thp_logs-v4-3-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=3458; i=leitao@debian.org; h=from:subject:message-id; bh=Rps73ws/DtMzsKWxDZkfpvyl2AU0CyVMps0aEftLyqQ=; b=owEBbQKS/ZANAwAIATWjk5/8eHdtAcsmYgBprqn/YQ4/idw1iXzDtS07ZJOflm8xgjZE7IOdp gm18zxehfaJAjMEAAEIAB0WIQSshTmm6PRnAspKQ5s1o5Of/Hh3bQUCaa6p/wAKCRA1o5Of/Hh3 bUKmEACusVurwYRevmZNNAslBZC0bm0mW0N9BmG03cGa4uzjS6kmskhapX3Y+NlLXVlbPel5yr3 pa+OBl2fHWgenUWzN3oKRpyKPJeGAlfiiWe8r8LckwJgSRYzt+o5iebL0M5GD/Hk1JQEi0PsAny 7gP8LBLlvJjKCFN6+VvL1HCHuNHSgXaDA4fOmhGqQPZhXOL2DIPawMqlQfnFqTBO5/5AbXFdFpB Wyuj0goQDWhZ7FBqo3sFTN9YuBqqueNsFCsnNkulvA98XdbL1Ze4waumD5Rx2glA3rj1SpMCTch E6yQWbdDOp0bj9XGmmvsaOddvh2JvH5n7SNOUfg/IyFJA8dg37NFkWLfHGjvEHBlurGfWLBQx37 aiKt/BlxFsJEHHFaCzn6e/xZtNDSL/8+mpjmCvseB6KxRq7soR4cokFy8PUzsSDPPUwUKdJPQSs UxruOJJdP0NdeYJZPJW1CcALD+PUjC7Zmd5MvBxqizC/03n4UJqB5SDnV30sA+4uMG2lf5UXz5z jxHBCvqhA22Yb8KkyJJu18UXwCe77XUzt4YoW0lKh8CBxud9F0z3nSdTk8H1kqP+sRI1F5y08RJ vzQ/mbyyTa+DFEUrxuqIUW5fgBitzezIWKr++pQgWWr6n5lsHZrPk5egP+W5slxNu7g07Yrxy52 +KEm4oWBwGgjbbQ== 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 2d5b05a416dab..be42a28da31d8 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 change_enabled(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 (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