mm/khugepaged: Factor out common logic in [scan,alloc]_sleep_millisecs_store()
Subsystems (2)
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
@@ -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
Quoted 11 lines · Leon Hwang
Both
scan_sleep_millisecs_store()andalloc_sleep_millisecs_store()perform the same operations: parse the input value, update their respective sleep interval, resetkhugepaged_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.
Signed-off-by Leon Hwang <[email protected]>
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@@ -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
-0return 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
-1static struct kobj_attribute alloc_sleep_millisecs_attr =__ATTR_RW (alloc_sleep_millisecs); --2.51.0
Quoted 9 lines · Lorenzo Stoakes
Quoted 8 lines · Leon Hwang
Both
scan_sleep_millisecs_store()andalloc_sleep_millisecs_store()perform the same operations: parse the input value, update their respective sleep interval, resetkhugepaged_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.
Signed-off-by Leon Hwang <[email protected]>Thanks this is a decent cleanup, with the nit(s0 below addressed LGTM, so:
Reviewed-by Lorenzo Stoakes <[email protected]>
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@@ -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
-0return 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
-0return __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
-1static struct kobj_attribute alloc_sleep_millisecs_attr =__ATTR_RW (alloc_sleep_millisecs); --2.51.0
Quoted 8 lines · Leon Hwang
Both
scan_sleep_millisecs_store()andalloc_sleep_millisecs_store()perform the same operations: parse the input value, update their respective sleep interval, resetkhugepaged_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.
Signed-off-by Leon Hwang <[email protected]>--- 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
Quoted 7 lines · Leon Hwang
Both
scan_sleep_millisecs_store()andalloc_sleep_millisecs_store()perform the same operations: parse the input value, update their respective sleep interval, resetkhugepaged_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.
Signed-off-by Leon Hwang <[email protected]>---
After tweaking as Lorenzo suggested, LGTM.