From nobody Thu Apr 9 21:54: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 0A9B439FCC2 for ; Thu, 5 Mar 2026 14:05:42 +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=FRwslz2G9y11MvodUEk6+6a1qTftfCZAigMcLZnyQz2DkxeycltRCw14gb6/VdbsJmtIOZVp03gwO3hVpz+XAmUMPI89qAKNn+rKhEPbtedM2eQBpei8KuC/ykAHwEVkRj5UVxWpF0imEuH5pPCQ4uwo/+LQgsqnc6X+0cOh8iI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772719544; c=relaxed/simple; bh=EG6CHi+pTEhduAjBnWyYsnqK4E7lPm6jKVUTFHdusKg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=b/LYChGYPSS9K+4vygTM50w8jxZTaNzABWI65x+u9MM7jMMrKrQSTOuGAnkvBCLq2kDWiOIkl4JBZR0TpfL6l2NcLaKR5pGz+0phyzqO9Ks+bSO/1I/xNzQWqXRx/CNdthlj+XPBWCYSKE3M/6H34e+nqtWPkPou56s+YRbZvOI= 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=QuKWDsVD; 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="QuKWDsVD" 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=OUBy+8sEPr5bdVEJymY5zJ8s1k1i9PEe1Y7EMp5ZRRA=; b=QuKWDsVDQz1shNyQeTGgni08ME 2elu9jXTJamicv8b8VQSFT+5vNVjpBataWcO+4O1ZZKvdZuJf8/1N20rzDu5aTzRXdfj1zmG6KO9Y 741KivVh0/g4zQ8/dNYDBoEIScuk/0mOjZIKFW43tEJL4xbUMmfjXiCgryCMUd2DnvkyuapFuan7i ix7tgnLQp1mb4e+2ThJqwD2DS2EXWhXAEXoKynpwZ2JNlwEnsAcCYoR8SKHwPDS4FaZCBc05YFVfF 1Cee2xFpbg0U8wFXBcJmVwZ9S575VTE0mvQEPRJV49pWdORyi0ukS9CzszSSKvQW/OcvCOsGCZ+zt uMWA0vwg==; 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 1vy9KM-00GmPB-Kq; Thu, 05 Mar 2026 14:05:22 +0000 From: Breno Leitao Date: Thu, 05 Mar 2026 06:04:53 -0800 Subject: [PATCH v2 1/3] mm: khugepaged: export set_recommended_min_free_kbytes() 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-1-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, "Lorenzo Stoakes (Oracle)" , Breno Leitao X-Mailer: b4 0.15-dev-363b9 X-Developer-Signature: v=1; a=openpgp-sha256; l=1489; i=leitao@debian.org; h=from:subject:message-id; bh=EG6CHi+pTEhduAjBnWyYsnqK4E7lPm6jKVUTFHdusKg=; b=owEBbQKS/ZANAwAIATWjk5/8eHdtAcsmYgBpqY2VjAIvXSLlnWrKVfNCZo+uzQhZEW+Il7PQ1 UZ7i6s1niWJAjMEAAEIAB0WIQSshTmm6PRnAspKQ5s1o5Of/Hh3bQUCaamNlQAKCRA1o5Of/Hh3 bZFRD/4zhlJBeP5EInd/GLNKjV8cfjlhT8PyECvyRyoA55Wl3Z8luO/HaJrgGVq/Yd2L21ra3TC LNCSCp5SxnkyxIFZ4PEthgv+ZOhXDmBph6yZACSx+OihrFWzEmhQY8O9qaQK9oo9mDtRmnYOHKf NJiGL5H7dGueNVy4vFFBT0hMbmHnHxkBzJwLV66JAZjB1LWPCLE3f/U7mm04C6kFJ/lTtaPlkah TkQ9G1re0o2C45UphPJqFyUpviadH7iG3tkLSW+cWDKkGZziHXzmw/oAoR3LVdZ/mC5SKx1ukH3 xsg7eSGPZVDZYA/WYJuJyDjCj7XESMg2YgMNQJaKUFjj1dvHfOieL6njMb6sDafl+uob2PzgJwV fzhJwXkQvxq5UFUoXhZSkRdmdaGZr+Z4WcLaVLmZqvEtmzhAnqJcdzrmrbLmzPWmU2gWfGAKfku RShrQTdRdLggi5NIWVgtGcd69At/IbA4J1nWLLpGqwpmXGHyfXICh69KNSXaIm/Dzh2psjLp/qa mZRNY5NgFu94NGiaDm1ygHhmDcikvwTPEbr9640ZM4AatLyGceNA1H/lkMW3+PJdsFK52GJ8TjT iAeedAOfEI1NmiwmGLHnwzFNfGvJ6+gqVpOkzkPRiAUdZWWF7aJhrgph3wqcxD6nGuLPJeXY5Rh KqAYJmlFqjgNF1g== X-Developer-Key: i=leitao@debian.org; a=openpgp; fpr=AC8539A6E8F46702CA4A439B35A3939FFC78776D X-Debian-User: leitao Make set_recommended_min_free_kbytes() callable from outside khugepaged.c by removing the static qualifier and adding a declaration in include/linux/khugepaged.h. This allows callers that change THP settings to recalculate watermarks without going through start_stop_khugepaged(). Suggested-by: Lorenzo Stoakes (Oracle) Signed-off-by: Breno Leitao Reviewed-by: Lorenzo Stoakes (Oracle) --- include/linux/khugepaged.h | 1 + mm/khugepaged.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/include/linux/khugepaged.h b/include/linux/khugepaged.h index d7a9053ff4fed..76252865fca4b 100644 --- a/include/linux/khugepaged.h +++ b/include/linux/khugepaged.h @@ -16,6 +16,7 @@ extern void __khugepaged_exit(struct mm_struct *mm); extern void khugepaged_enter_vma(struct vm_area_struct *vma, vm_flags_t vm_flags); extern void khugepaged_min_free_kbytes_update(void); +extern void set_recommended_min_free_kbytes(void); extern bool current_is_khugepaged(void); void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, bool install_pmd); diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 1dd3cfca610db..56a41c21b44c9 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2630,7 +2630,7 @@ static int khugepaged(void *none) return 0; } =20 -static void set_recommended_min_free_kbytes(void) +void set_recommended_min_free_kbytes(void) { struct zone *zone; int nr_zones =3D 0; --=20 2.47.3 From nobody Thu Apr 9 21:54: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 From nobody Thu Apr 9 21:54: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