[PATCH 2/2] mm: thp: avoid calling start_stop_khugepaged() in enabled_store()

Breno Leitao posted 2 patches 1 month, 1 week ago
There is a newer version of this series
[PATCH 2/2] mm: thp: avoid calling start_stop_khugepaged() in enabled_store()
Posted by Breno Leitao 1 month, 1 week ago
Avoid calling start_stop_khugepaged() at the top-level enabled_store()
for /sys/kernel/mm/transparent_hugepage/enabled. Use test_and_set_bit()
and test_and_clear_bit() to detect whether the configuration
actually changed before calling start_stop_khugepaged().

This avoid calling start_stop_khugepaged() unnecessary.

Signed-off-by: Breno Leitao <leitao@debian.org>
---
 mm/huge_memory.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 9abfb115e9329..b6ed44b6e8c02 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -320,21 +320,28 @@ static ssize_t enabled_store(struct kobject *kobj,
 			     struct kobj_attribute *attr,
 			     const char *buf, size_t count)
 {
+	bool changed = false;
 	ssize_t ret = count;
 
 	if (sysfs_streq(buf, "always")) {
-		clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
-		set_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
+		changed = test_and_clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
+					     &transparent_hugepage_flags);
+		changed |= !test_and_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);
+		changed = test_and_clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
+					     &transparent_hugepage_flags);
+		changed |= !test_and_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_flags);
+		changed = test_and_clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
+					     &transparent_hugepage_flags);
+		changed |= test_and_clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
+					     &transparent_hugepage_flags);
 	} else
 		ret = -EINVAL;
 
-	if (ret > 0) {
+	if (ret > 0 && changed) {
 		int err = start_stop_khugepaged();
 		if (err)
 			ret = err;

-- 
2.47.3
Re: [PATCH 2/2] mm: thp: avoid calling start_stop_khugepaged() in enabled_store()
Posted by Lorenzo Stoakes (Oracle) 1 month, 1 week ago
On Wed, Mar 04, 2026 at 02:22:34AM -0800, Breno Leitao wrote:
> Avoid calling start_stop_khugepaged() at the top-level enabled_store()
> for /sys/kernel/mm/transparent_hugepage/enabled. Use test_and_set_bit()
> and test_and_clear_bit() to detect whether the configuration
> actually changed before calling start_stop_khugepaged().
>
> This avoid calling start_stop_khugepaged() unnecessary.
>
> Signed-off-by: Breno Leitao <leitao@debian.org>

Similar comments to 1/2 ofc.

> ---
>  mm/huge_memory.c | 21 ++++++++++++++-------
>  1 file changed, 14 insertions(+), 7 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 9abfb115e9329..b6ed44b6e8c02 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -320,21 +320,28 @@ static ssize_t enabled_store(struct kobject *kobj,
>  			     struct kobj_attribute *attr,
>  			     const char *buf, size_t count)
>  {
> +	bool changed = false;
>  	ssize_t ret = count;
>
>  	if (sysfs_streq(buf, "always")) {
> -		clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
> -		set_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
> +		changed = test_and_clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
> +					     &transparent_hugepage_flags);
> +		changed |= !test_and_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);
> +		changed = test_and_clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
> +					     &transparent_hugepage_flags);
> +		changed |= !test_and_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_flags);
> +		changed = test_and_clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
> +					     &transparent_hugepage_flags);
> +		changed |= test_and_clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
> +					     &transparent_hugepage_flags);
>  	} else
>  		ret = -EINVAL;
>
> -	if (ret > 0) {
> +	if (ret > 0 && changed) {
>  		int err = start_stop_khugepaged();
>  		if (err)
>  			ret = err;
>
> --
> 2.47.3
>