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 9B4C4396584 for ; Thu, 5 Mar 2026 14:05:43 +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=1772719544; cv=none; b=lUxOFI2KDUNSD7wQsjZe65+nPfYI+1+tQExnrDhy2u9RqMgTDlKX4zGWyNu6HM0I/Bz8QvRhQCRd1O+wggfmAuT+cb4pAFwjDpDBSl05xkX9wQW3GT5KEhPUotjE3+Jx3w/2kJLoK5psuMjJyy1yTIy8KcJ/wB8AvoUGreZWxLk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772719544; c=relaxed/simple; bh=vPEbT2v0zbvHi9a317nXlZeg63vRGu0w/r+o1EvSR20=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=PplFGUgO0x4kMUihqf4/DiDDug1IGQUymZuViFi+u4+ZE2HKmcQkEivi6chI2Z/D/lBAqrb2CpFkKpIOWiNx8nfXgajhvHDRP5dz7CaE/IggvUTzou2D/I4dgarxYr5w8jajj537POK7HGEl1ArT7prZeLyEh27aDlG72+1rIPQ= 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=sdJhfNPp; 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="sdJhfNPp" 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=fjzA29kHyaTu0CdBE4NiO/4Dgo4yhaY+vy+XeTjQsZ8=; b=sdJhfNPpD7+8Cj3rgbiQVD8cKt isnVz1owe1lg0hf9QufeKy2IgsOb7KV8JTH/sUGfJZHpb1SADvGHECEQ7+DGMyBeVylzcdSmAjEE3 ACrFXaQLo19u41WFGhpERD6FllJoS3JAe8BbBJLviWCH5ZanlkyvQIxItX3qE8lt0SjhemUuWE3FL zexFgy4wMxftbTkrD1oNGd31z2P5ZhGfjVQqsSXGEPeoFiGF4m+5mK/DXJX4QV2KlthXjDG+rtTX5 yN1zDuYyO8/a7zfi7Lj95iu+wOc45k437XoMAHfbuOf/MR684cfyql1LrkanOrRcFkZzR7pmMxBpm 1c/unbjw==; 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 1vy9KX-00GmQG-CM; Thu, 05 Mar 2026 14:05:33 +0000 From: Breno Leitao Date: Thu, 05 Mar 2026 06:04:55 -0800 Subject: [PATCH v2 3/3] 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: <20260305-thp_logs-v2-3-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=2816; i=leitao@debian.org; h=from:subject:message-id; bh=vPEbT2v0zbvHi9a317nXlZeg63vRGu0w/r+o1EvSR20=; b=owEBbQKS/ZANAwAIATWjk5/8eHdtAcsmYgBpqY2VLe8cFEQJ0smLQoRFwTTZeGaiiBLP5OWCE ofHndUML7eJAjMEAAEIAB0WIQSshTmm6PRnAspKQ5s1o5Of/Hh3bQUCaamNlQAKCRA1o5Of/Hh3 bbbTD/sFCO8p2fCAXqoAiQj1ygh1y7Uv1RIfgfZU/0tkA3h5dl+rWGjq/t+DEGTWWFqucssKNcC Nbh4Z8fvN10bHoYCu52NpP4ezHnnbYUzhOs6u5nYiww4U73gKnSlHTHrDOXtLnJtQn6JrjJyQRG xqtl3yIXMc8bsrPfW75NrQkhM4bsou2oJWg6KZ052aJPXYpNJcVYWFSTJCxaEEQV/CH0fDV5uMY sp7nt7a+KILFxOSVr6X9HT1I4bYJl9fHMi9Ec3XanoXcQll6QooiOhcFjJuPZ1mFmqA2bF/Iyyl CkJw4MQir7yAkk3c58c9QUaUPizTMJZ0DBU261SaWMz6aQzlUUzLLdk9TAhX4ysHTyh+o3mVcls DN27tAstnhR4K8JBrug1GPlaR1PjO/GEsanP7UjptMwqTgc6tB4dMlHGwA2yaqTuHw39yLq2ku4 dZPOVZTMnHmeyoWtN2KFd8NLCajUnyTkRqaOKJywpFm9qYGtIFbrILH+3xUDD6dThMHeeA9WGd1 ktnt7Eji1zGSTvLYKyV1+PT4tVVwrP7oPfUeCtwsaLHIbFURevBXyy0gOyZBeyMaHE/cAaEKAwU /1msT/A3Hy27mGT+aBh9d1RILKiyGV86MqGR8TY7m1iCAuMD2B+94b7QSPa/FLNzSdZrE9boK6e 9VNRKmOq/XgyeSg== 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 that reuses the shared enum enabled_mode and enabled_mode_strings[] introduced in the previous commit. 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. ENABLED_INHERIT is rejected since it is not a valid mode for the global THP setting. Signed-off-by: Breno Leitao Reviewed-by: Lorenzo Stoakes (Oracle) --- mm/huge_memory.c | 51 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 19619213f54d1..dd5011cf36839 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -330,30 +330,51 @@ static const char * const enabled_mode_strings[] =3D { [ENABLED_NEVER] =3D "never", }; =20 +static bool change_enabled(enum 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(enabled_mode_strings, buf); + if (mode < 0 || mode =3D=3D ENABLED_INHERIT) + 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