diff mbox series

[v2] mm, thp: fix defrag setting if newline is not used

Message ID alpine.DEB.2.21.2001171411020.56385@chino.kir.corp.google.com (mailing list archive)
State New, archived
Headers show
Series [v2] mm, thp: fix defrag setting if newline is not used | expand

Commit Message

David Rientjes Jan. 17, 2020, 10:11 p.m. UTC
If thp defrag setting "defer" is used and a newline is *not* used when
writing to the sysfs file, this is interpreted as the "defer+madvise"
option.

This is because we do prefix matching and if five characters are written
without a newline, the current code ends up comparing to the first five
bytes of the "defer+madvise" option and using that instead.

Use the more appropriate sysfs_streq() that handles the trailing newline
for us.  Since this doubles as a nice cleanup, do it in enabled_store()
as well.

Fixes: 21440d7eb904 ("mm, thp: add new defer+madvise defrag option")
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Mel Gorman <mgorman@techsingularity.net>
Suggested-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: David Rientjes <rientjes@google.com>
---
 Latest 5.5-rc6 doesn't boot for me, something to be debugged separately,
 so this was tested on 5.4.  No changes in this area, however, between
 the two kernels.

 mm/huge_memory.c | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)

Comments

Vlastimil Babka Jan. 18, 2020, 10:54 a.m. UTC | #1
On 1/17/20 11:11 PM, David Rientjes wrote:
> If thp defrag setting "defer" is used and a newline is *not* used when
> writing to the sysfs file, this is interpreted as the "defer+madvise"
> option.
> 
> This is because we do prefix matching and if five characters are written
> without a newline, the current code ends up comparing to the first five
> bytes of the "defer+madvise" option and using that instead.
> 
> Use the more appropriate sysfs_streq() that handles the trailing newline
> for us.  Since this doubles as a nice cleanup, do it in enabled_store()
> as well.
> 
> Fixes: 21440d7eb904 ("mm, thp: add new defer+madvise defrag option")
> Cc: Vlastimil Babka <vbabka@suse.cz>
> Cc: Mel Gorman <mgorman@techsingularity.net>
> Suggested-by: Andrew Morton <akpm@linux-foundation.org>
> Signed-off-by: David Rientjes <rientjes@google.com>

Acked-by: Vlastimil Babka <vbabka@suse.cz>
Thanks.
Andrew Morton Jan. 19, 2020, 1:04 a.m. UTC | #2
On Fri, 17 Jan 2020 14:11:48 -0800 (PST) David Rientjes <rientjes@google.com> wrote:

> If thp defrag setting "defer" is used and a newline is *not* used when
> writing to the sysfs file, this is interpreted as the "defer+madvise"
> option.
> 
> This is because we do prefix matching and if five characters are written
> without a newline, the current code ends up comparing to the first five
> bytes of the "defer+madvise" option and using that instead.
> 
> Use the more appropriate sysfs_streq() that handles the trailing newline
> for us.  Since this doubles as a nice cleanup, do it in enabled_store()
> as well.

I can't really I really understand this prefix-matching thing that
we're taking away.  Documentation/admin-guide/mm/transhuge.rst doesn't
appear to mention it.  Could we please add a paragraph to the changelog
to spell all this out.  Bonus points for formally describing the
behaviour which we're removing!

Thanks.
David Rientjes Jan. 19, 2020, 9:57 p.m. UTC | #3
On Sat, 18 Jan 2020, Andrew Morton wrote:

> > If thp defrag setting "defer" is used and a newline is *not* used when
> > writing to the sysfs file, this is interpreted as the "defer+madvise"
> > option.
> > 
> > This is because we do prefix matching and if five characters are written
> > without a newline, the current code ends up comparing to the first five
> > bytes of the "defer+madvise" option and using that instead.
> > 
> > Use the more appropriate sysfs_streq() that handles the trailing newline
> > for us.  Since this doubles as a nice cleanup, do it in enabled_store()
> > as well.
> 
> I can't really I really understand this prefix-matching thing that
> we're taking away.  Documentation/admin-guide/mm/transhuge.rst doesn't
> appear to mention it.  Could we please add a paragraph to the changelog
> to spell all this out.  Bonus points for formally describing the
> behaviour which we're removing!
> 

The current implementation relies on prefix matching: the number of bytes 
compared is either the number of bytes written or the length of the option 
being compared.  With a newline, "defer\n" does not match 
"defer+"madvise"; without a newline, however, "defer" is considered to 
match "defer+madvise" (prefix matching is only comparing the first five 
bytes).  End result is that writing "defer" is broken unless it has an 
additional trailing character.

This means that writing "madv" in the past would match and set "madvise".  
With strict checking, that no longer is the case but it is unlikely 
anybody is currently doing this.
diff mbox series

Patch

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 13cc93785006..1c61dea937bc 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -177,16 +177,13 @@  static ssize_t enabled_store(struct kobject *kobj,
 {
 	ssize_t ret = count;
 
-	if (!memcmp("always", buf,
-		    min(sizeof("always")-1, count))) {
+	if (sysfs_streq(buf, "always")) {
 		clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
 		set_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
-	} else if (!memcmp("madvise", buf,
-			   min(sizeof("madvise")-1, count))) {
+	} 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 (!memcmp("never", buf,
-			   min(sizeof("never")-1, count))) {
+	} else if (sysfs_streq(buf, "never")) {
 		clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
 		clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
 	} else
@@ -250,32 +247,27 @@  static ssize_t defrag_store(struct kobject *kobj,
 			    struct kobj_attribute *attr,
 			    const char *buf, size_t count)
 {
-	if (!memcmp("always", buf,
-		    min(sizeof("always")-1, count))) {
+	if (sysfs_streq(buf, "always")) {
 		clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
 		clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
 		clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
 		set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
-	} else if (!memcmp("defer+madvise", buf,
-		    min(sizeof("defer+madvise")-1, count))) {
+	} else if (sysfs_streq(buf, "defer+madvise")) {
 		clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
 		clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
 		clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
 		set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
-	} else if (!memcmp("defer", buf,
-		    min(sizeof("defer")-1, count))) {
+	} else if (sysfs_streq(buf, "defer")) {
 		clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
 		clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
 		clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
 		set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
-	} else if (!memcmp("madvise", buf,
-			   min(sizeof("madvise")-1, count))) {
+	} else if (sysfs_streq(buf, "madvise")) {
 		clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
 		clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
 		clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
 		set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
-	} else if (!memcmp("never", buf,
-			   min(sizeof("never")-1, count))) {
+	} else if (sysfs_streq(buf, "never")) {
 		clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
 		clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
 		clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);