Skip to contentSkip to conversation

mm/khugepaged: Factor out common logic in [scan,alloc]_sleep_millisecs_store()

PATCH Superseded Leon Hwang 4 participants 5 messages Oct 20, 2025 – Oct 21, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Leon Hwang

author
#0 lore raw

PATCH 1/1 mm/khugepaged: Factor out common logic in [scan,alloc]_sleep_millisecs_store()

Both scan_sleep_millisecs_store() and alloc_sleep_millisecs_store() perform the same operations: parse the input value, update their respective sleep interval, reset khugepaged_sleep_expire, and wake up the khugepaged thread.

Factor out this duplicated logic into a helper function __sleep_millisecs_store(), and simplify both store functions.

No functional change intended.

---
 mm/khugepaged.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)
mm/khugepaged.c +14 -15
index 0341c3d13e9e..0b7915015e9e 100644
@@ -129,9 +129,8 @@ static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
 	return sysfs_emit(buf, "%u\n", khugepaged_scan_sleep_millisecs);
 }
 
-static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
-					  struct kobj_attribute *attr,
-					  const char *buf, size_t count)
+static ssize_t __sleep_millisecs_store(const char *buf, size_t count,
+				       unsigned int *millisecs)
 {
 	unsigned int msecs;
 	int err;
@@ -140,12 +139,21 @@ static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
 	if (err)
 		return -EINVAL;
 
-	khugepaged_scan_sleep_millisecs = msecs;
+	*millisecs = msecs;
 	khugepaged_sleep_expire = 0;
 	wake_up_interruptible(&khugepaged_wait);
 
 	return count;
 }
+
+static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
+					  struct kobj_attribute *attr,
+					  const char *buf, size_t count)
+{
+	unsigned int *millisecs = &khugepaged_scan_sleep_millisecs;
+
+	return __sleep_millisecs_store(buf, count, millisecs);
+}
 static struct kobj_attribute scan_sleep_millisecs_attr =
 	__ATTR_RW(scan_sleep_millisecs);
 
@@ -160,18 +168,9 @@ static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
 					   struct kobj_attribute *attr,
 					   const char *buf, size_t count)
 {
-	unsigned int msecs;
-	int err;
-
-	err = kstrtouint(buf, 10, &msecs);
-	if (err)
-		return -EINVAL;
+	unsigned int *millisecs = &khugepaged_alloc_sleep_millisecs;
 
-	khugepaged_alloc_sleep_millisecs = msecs;
-	khugepaged_sleep_expire = 0;
-	wake_up_interruptible(&khugepaged_wait);
-
-	return count;
+	return __sleep_millisecs_store(buf, count, millisecs);
 }
 static struct kobj_attribute alloc_sleep_millisecs_attr =
 	__ATTR_RW(alloc_sleep_millisecs);
Signature · 1 line
2.51.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Leon Hwang

Both scan_sleep_millisecs_store() and alloc_sleep_millisecs_store() perform the same operations: parse the input value, update their respective sleep interval, reset khugepaged_sleep_expire, and wake up the khugepaged thread.

Factor out this duplicated logic into a helper function __sleep_millisecs_store(), and simplify both store functions.

No functional change intended.

Thanks this is a decent cleanup, with the nit(s0 below addressed LGTM, so:

Quoted 29 lines
---
 mm/khugepaged.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)
mm/khugepaged.c +3 -4
index 0341c3d13e9e..0b7915015e9e 100644
@@ -129,9 +129,8 @@ static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
 	return sysfs_emit(buf, "%u\n", khugepaged_scan_sleep_millisecs);
 }
 
-static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
-					  struct kobj_attribute *attr,
-					  const char *buf, size_t count)
+static ssize_t __sleep_millisecs_store(const char *buf, size_t count,
+				       unsigned int *millisecs)
 {
 	unsigned int msecs;
 	int err;
@@ -140,12 +139,21 @@ static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
 	if (err)
 		return -EINVAL;
 
-	khugepaged_scan_sleep_millisecs = msecs;
+	*millisecs = msecs;
 	khugepaged_sleep_expire = 0;
 	wake_up_interruptible(&khugepaged_wait);
 

mm/khugepaged.c static ssize_t scan_sleep_millisecs_store(struct kobject *kobj, · line 155

Quoted 10 lines
mm/khugepaged.c +8 -0
 	return count;
 }
+
+static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
+					  struct kobj_attribute *attr,
+					  const char *buf, size_t count)
+{
+	unsigned int *millisecs = &khugepaged_scan_sleep_millisecs;
+
+	return __sleep_millisecs_store(buf, count, millisecs);

I think this'd be much clearer as:

return __sleep_millisecs_store(buf, count, &khugepaged_alloc_scan_millisecs);

Quoted 13 lines
mm/khugepaged.c +1 -5
+}
 static struct kobj_attribute scan_sleep_millisecs_attr =
 	__ATTR_RW(scan_sleep_millisecs);
 
@@ -160,18 +168,9 @@ static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
 					   struct kobj_attribute *attr,
 					   const char *buf, size_t count)
 {
-	unsigned int msecs;
-	int err;
-
-	err = kstrtouint(buf, 10, &msecs);
-	if (err)

mm/khugepaged.c static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj, · line 174

Quoted 10 lines
mm/khugepaged.c +2 -6
-		return -EINVAL;
+	unsigned int *millisecs = &khugepaged_alloc_sleep_millisecs;
 
-	khugepaged_alloc_sleep_millisecs = msecs;
-	khugepaged_sleep_expire = 0;
-	wake_up_interruptible(&khugepaged_wait);
-
-	return count;
+	return __sleep_millisecs_store(buf, count, millisecs);
 }

I think this'd be much clearer as:

return __sleep_millisecs_store(buf, count, &khugepaged_alloc_sleep_millisecs);

Quoted 5 lines
mm/khugepaged.c +0 -1
 static struct kobj_attribute alloc_sleep_millisecs_attr =
 	__ATTR_RW(alloc_sleep_millisecs);
--

2.51.0

author
#2 lore raw
Quoted 9 lines · Lorenzo Stoakes
Quoted 8 lines · Leon Hwang

Both scan_sleep_millisecs_store() and alloc_sleep_millisecs_store() perform the same operations: parse the input value, update their respective sleep interval, reset khugepaged_sleep_expire, and wake up the khugepaged thread.

Factor out this duplicated logic into a helper function __sleep_millisecs_store(), and simplify both store functions.

Quoted 8 lines
Quoted 3 lines

No functional change intended.

Thanks this is a decent cleanup, with the nit(s0 below addressed LGTM, so:

Thanks for the review, Lorenzo.

Quoted 29 lines
Quoted 29 lines
---
 mm/khugepaged.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)
mm/khugepaged.c +3 -4
index 0341c3d13e9e..0b7915015e9e 100644
@@ -129,9 +129,8 @@ static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
 	return sysfs_emit(buf, "%u\n", khugepaged_scan_sleep_millisecs);
 }
 
-static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
-					  struct kobj_attribute *attr,
-					  const char *buf, size_t count)
+static ssize_t __sleep_millisecs_store(const char *buf, size_t count,
+				       unsigned int *millisecs)
 {
 	unsigned int msecs;
 	int err;
@@ -140,12 +139,21 @@ static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
 	if (err)
 		return -EINVAL;
 
-	khugepaged_scan_sleep_millisecs = msecs;
+	*millisecs = msecs;
 	khugepaged_sleep_expire = 0;
 	wake_up_interruptible(&khugepaged_wait);
 
Quoted 13 lines

mm/khugepaged.c static ssize_t scan_sleep_millisecs_store(struct kobject *kobj, · line 155

Quoted 10 lines
mm/khugepaged.c +8 -0
 	return count;
 }
+
+static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
+					  struct kobj_attribute *attr,
+					  const char *buf, size_t count)
+{
+	unsigned int *millisecs = &khugepaged_scan_sleep_millisecs;
+
+	return __sleep_millisecs_store(buf, count, millisecs);

I think this'd be much clearer as:

Quoted 1 line
mm/khugepaged.c +0 -0
 return __sleep_millisecs_store(buf, count,

&khugepaged_alloc_scan_millisecs);

Quoted 27 lines
Quoted 14 lines
mm/khugepaged.c +1 -5
+}
 static struct kobj_attribute scan_sleep_millisecs_attr =
 	__ATTR_RW(scan_sleep_millisecs);
@@ -160,18 +168,9 @@ static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
 					   struct kobj_attribute *attr,
 					   const char *buf, size_t count)
 {
-	unsigned int msecs;
-	int err;
-
-	err = kstrtouint(buf, 10, &msecs);
-	if (err)

mm/khugepaged.c static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj, · line 174

Quoted 10 lines
mm/khugepaged.c +2 -6
-		return -EINVAL;
+	unsigned int *millisecs = &khugepaged_alloc_sleep_millisecs;
 
-	khugepaged_alloc_sleep_millisecs = msecs;
-	khugepaged_sleep_expire = 0;
-	wake_up_interruptible(&khugepaged_wait);
-
-	return count;
+	return __sleep_millisecs_store(buf, count, millisecs);
 }

I think this'd be much clearer as:

Quoted 2 lines

return __sleep_millisecs_store(buf, count, &khugepaged_alloc_sleep_millisecs);

Indeed, your suggestion makes the code clearer.

I'll update both occurrences in the next revision.

Thanks,
Leon

Quoted 5 lines
Quoted 5 lines
mm/khugepaged.c +0 -1
 static struct kobj_attribute alloc_sleep_millisecs_attr =
 	__ATTR_RW(alloc_sleep_millisecs);
--

2.51.0

reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Leon Hwang
Quoted 8 lines · Leon Hwang

Both scan_sleep_millisecs_store() and alloc_sleep_millisecs_store() perform the same operations: parse the input value, update their respective sleep interval, reset khugepaged_sleep_expire, and wake up the khugepaged thread.

Factor out this duplicated logic into a helper function __sleep_millisecs_store(), and simplify both store functions.

Quoted 7 lines

No functional change intended.

---
 mm/khugepaged.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)

With the changes suggested by Lorenzo, feel free to add

--
Best Regards,
Yan, Zi

reviewer Reviewed-by on v1 1/1
#4 lore raw
In reply to Leon Hwang
Quoted 7 lines · Leon Hwang

Both scan_sleep_millisecs_store() and alloc_sleep_millisecs_store() perform the same operations: parse the input value, update their respective sleep interval, reset khugepaged_sleep_expire, and wake up the khugepaged thread.

Factor out this duplicated logic into a helper function __sleep_millisecs_store(), and simplify both store functions.

Thanks, less is more ;)

Quoted 5 lines

No functional change intended.

---

After tweaking as Lorenzo suggested, LGTM.