[PATCH 1/3] mm: Rework {enabled,defrag}_store()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The suffix of the input buffer is truncated in {enabled,defrag}_store().
With that, the illegal settings can be accepted, as the following example
shows.

   # cat /sys/kernel/mm/transparent_hugepage/enabled
   always [madvise] never
   # echo neveroff > /sys/kernel/mm/transparent_hugepage/enabled
   # cat /sys/kernel/mm/transparent_hugepage/enabled
   always madvise [never]

This reworks {enabled,defrag}_store() to fix the issue and make the code
a bit more extendible. With this applied, the illegal settings will be
rejected:

   # echo neveroff > /sys/kernel/mm/transparent_hugepage/enabled
   sh: write error: Invalid argument

Signed-off-by: Gavin Shan <gshan@xxxxxxxxxx>
---
 mm/huge_memory.c | 114 ++++++++++++++++++++++++++---------------------
 1 file changed, 62 insertions(+), 52 deletions(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index a88093213674..7bd573f1631d 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -160,6 +160,15 @@ static struct shrinker huge_zero_page_shrinker = {
 };
 
 #ifdef CONFIG_SYSFS
+static const char * const enabled_strings[] = {
+	"always", "madvise", "never"
+};
+static unsigned int enabled_bits[] = {
+	TRANSPARENT_HUGEPAGE_FLAG,
+	TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
+	UINT_MAX
+};
+
 static ssize_t enabled_show(struct kobject *kobj,
 			    struct kobj_attribute *attr, char *buf)
 {
@@ -175,29 +184,29 @@ static ssize_t enabled_store(struct kobject *kobj,
 			     struct kobj_attribute *attr,
 			     const char *buf, size_t count)
 {
-	ssize_t ret = count;
-
-	if (!memcmp("always", buf,
-		    min(sizeof("always")-1, count))) {
-		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))) {
-		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))) {
-		clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
-		clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
-	} else
-		ret = -EINVAL;
+	int i, ret;
 
-	if (ret > 0) {
-		int err = start_stop_khugepaged();
-		if (err)
-			ret = err;
+	for (i = 0; i < ARRAY_SIZE(enabled_strings); i++) {
+		if ((count - 1) == strlen(enabled_strings[i]) &&
+		    !memcmp(buf, enabled_strings[i], count - 1))
+			break;
 	}
-	return ret;
+
+	if (i >= ARRAY_SIZE(enabled_strings))
+		return -EINVAL;
+
+	clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
+		  &transparent_hugepage_flags);
+	clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
+		  &transparent_hugepage_flags);
+	if (enabled_bits[i] != UINT_MAX)
+		set_bit(enabled_bits[i], &transparent_hugepage_flags);
+
+	ret = start_stop_khugepaged();
+	if (ret)
+		return ret;
+
+	return count;
 }
 static struct kobj_attribute enabled_attr =
 	__ATTR(enabled, 0644, enabled_show, enabled_store);
@@ -232,6 +241,17 @@ ssize_t single_hugepage_flag_store(struct kobject *kobj,
 	return count;
 }
 
+static const char * const defrag_strings[] = {
+	"always", "defer+madvise", "defer", "madvise", "never"
+};
+static unsigned int defrag_bits[] = {
+	TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
+	TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG,
+	TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
+	TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
+	UINT_MAX
+};
+
 static ssize_t defrag_show(struct kobject *kobj,
 			   struct kobj_attribute *attr, char *buf)
 {
@@ -250,39 +270,29 @@ 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))) {
-		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))) {
-		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))) {
-		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))) {
-		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))) {
-		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);
-		clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
-	} else
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(defrag_strings); i++) {
+		if ((count - 1) == strlen(defrag_strings[i]) &&
+		    !memcmp(buf, defrag_strings[i], count - 1))
+			break;
+	}
+
+	if (i >= ARRAY_SIZE(defrag_strings))
 		return -EINVAL;
 
+	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);
+	clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
+		  &transparent_hugepage_flags);
+
+	if (defrag_bits[i] != UINT_MAX)
+		set_bit(defrag_bits[i], &transparent_hugepage_flags);
+
 	return count;
 }
 static struct kobj_attribute defrag_attr =
-- 
2.23.0






[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux