Skip to contentSkip to conversation

ksm: perform cleanup actions only when they are actually required

PATCH New Stanislav Kholmanskikh 2 participants 6 messages Mar 19, 2018 – Mar 20, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ksm: perform cleanup actions only when they are actually required

This one is similar to commit 8ae3916674fa
("mem/oom, tunable: perform cleanup actions only when they are actually required") i.e. we are making sure that ksm tests do not revert any not yet performed actions.

---
 testcases/kernel/mem/ksm/ksm01.c |    6 +++++-
 testcases/kernel/mem/ksm/ksm02.c |   11 +++++++++--
 testcases/kernel/mem/ksm/ksm03.c |   11 +++++++++--
 testcases/kernel/mem/ksm/ksm04.c |   16 +++++++++++++---
 testcases/kernel/mem/ksm/ksm06.c |    5 ++++-
 5 files changed, 40 insertions(+), 9 deletions(-)
testcases/kernel/mem/ksm/ksm01.c +5 -1
index ce96983..0961927 100644
@@ -59,6 +59,8 @@
 #include "mem.h"
 #include "ksm_common.h"
 
+static int max_page_sharing_saved;
+
 static void verify_ksm(void)
 {
 	create_same_memory(size, num, unit);
@@ -70,6 +72,7 @@ static void setup(void)
 		tst_brk(TCONF, "KSM configuration is not enabled");
 
 	save_max_page_sharing();
+	max_page_sharing_saved = 1;
 
 	parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
 
@@ -93,7 +96,8 @@ static void cleanup(void)
 		FILE_PRINTF(PATH_KSM "merge_across_nodes",
 				 "%d", merge_across_nodes);
 
-	restore_max_page_sharing();
+	if (max_page_sharing_saved)
+		restore_max_page_sharing();
 }
 
 static struct tst_test test = {
testcases/kernel/mem/ksm/ksm02.c +9 -2
index 03b44c0..014ac1c 100644
@@ -59,6 +59,9 @@
 #ifdef HAVE_NUMA_V2
 #include <numaif.h>
 
+static int max_page_sharing_saved;
+static int cpuset_mounted;
+
 static void verify_ksm(void)
 {
 	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
@@ -86,9 +89,11 @@ static void cleanup(void)
 		FILE_PRINTF(PATH_KSM "merge_across_nodes",
 				 "%d", merge_across_nodes);
 
-	restore_max_page_sharing();
+	if (max_page_sharing_saved)
+		restore_max_page_sharing();
 
-	umount_mem(CPATH, CPATH_NEW);
+	if (cpuset_mounted)
+		umount_mem(CPATH, CPATH_NEW);
 }
 
 static void setup(void)
@@ -96,6 +101,7 @@ static void setup(void)
 	if (access(PATH_KSM, F_OK) == -1)
 		tst_brk(TCONF, "KSM configuration is not enabled");
 	save_max_page_sharing();
+	max_page_sharing_saved = 1;
 
 	parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
 
@@ -106,6 +112,7 @@ static void setup(void)
 	}
 
 	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
+	cpuset_mounted = 1;
 }
 
 static struct tst_test test = {
testcases/kernel/mem/ksm/ksm03.c +9 -2
index 51cc923..79a8f87 100644
@@ -59,6 +59,9 @@
 #include "mem.h"
 #include "ksm_common.h"
 
+static int max_page_sharing_saved;
+static int memcg_mounted;
+
 static void verify_ksm(void)
 {
 	write_memcg();
@@ -77,8 +80,10 @@ static void setup(void)
 	}
 
 	save_max_page_sharing();
+	max_page_sharing_saved = 1;
 	parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
 	mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
+	memcg_mounted = 1;
 }
 
 static void cleanup(void)
@@ -87,9 +92,11 @@ static void cleanup(void)
 		FILE_PRINTF(PATH_KSM "merge_across_nodes",
 				 "%d", merge_across_nodes);
 
-	restore_max_page_sharing();
+	if (max_page_sharing_saved)
+		restore_max_page_sharing();
 
-	umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
+	if (memcg_mounted)
+		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 }
 
 static struct tst_test test = {
testcases/kernel/mem/ksm/ksm04.c +13 -3
index 835222f..a79e649 100644
@@ -59,6 +59,10 @@
 #ifdef HAVE_NUMA_V2
 #include <numaif.h>
 
+static int max_page_sharing_saved;
+static int cpuset_mounted;
+static int memcg_mounted;
+
 static void verify_ksm(void)
 {
 	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
@@ -88,10 +92,13 @@ static void cleanup(void)
 		FILE_PRINTF(PATH_KSM "merge_across_nodes",
 				 "%d", merge_across_nodes);
 
-	restore_max_page_sharing();
+	if (max_page_sharing_saved)
+		restore_max_page_sharing();
 
-	umount_mem(CPATH, CPATH_NEW);
-	umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
+	if (cpuset_mounted)
+		umount_mem(CPATH, CPATH_NEW);
+	if (memcg_mounted)
+		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 }
 
 static void setup(void)
@@ -106,10 +113,13 @@ static void setup(void)
 	}
 
 	save_max_page_sharing();
+	max_page_sharing_saved = 1;
 	parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
 
 	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
+	cpuset_mounted = 1;
 	mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
+	memcg_mounted = 1;
 }
 
 static struct tst_test test = {
testcases/kernel/mem/ksm/ksm06.c +4 -1
index 53d532c..92a36c1 100644
@@ -42,6 +42,7 @@
 static int run = -1;
 static int sleep_millisecs = -1;
 static int merge_across_nodes = -1;
+static int max_page_sharing_saved;
 static unsigned long nr_pages;
 
 static char *n_opt;
@@ -66,6 +67,7 @@ static void setup(void)
 		tst_brk(TCONF, "no merge_across_nodes sysfs knob");
 
 	save_max_page_sharing();
+	max_page_sharing_saved = 1;
 
 	if (!is_numa(NULL, NH_MEMS, 2))
 		tst_brk(TCONF, "The case needs a NUMA system.");
@@ -91,7 +93,8 @@ static void cleanup(void)
 	if (run != -1)
 		FILE_PRINTF(PATH_KSM "run", "%d", run);
 
-	restore_max_page_sharing();
+	if (max_page_sharing_saved)
+		restore_max_page_sharing();
 }
 
 static struct tst_test test = {
Signature · 1 line
1.7.1
#1 lore raw

PATCH 1/1 ksm: perform cleanup actions only when they are actually required

Hi!

Review static void setup(void) · line 75

Quoted 10 lines
(quoted diff) +3 -0
+static int max_page_sharing_saved;
+
 static void verify_ksm(void)
 {
 	create_same_memory(size, num, unit);
@@ -70,6 +72,7 @@ static void setup(void)
 		tst_brk(TCONF, "KSM configuration is not enabled");
 
 	save_max_page_sharing();
+	max_page_sharing_saved = 1;

Given that we have the save_max_page_sharing() and restore_max_page_sharing() functions it would be cleaner to put the flag that tells us if we should restore it to the lib/mem.c so that we do not have to repeat the code in all testcases.

Looking into kernel documentation is says that minimum for the max_page_sharing is 2 so we may as well just do:

testcases/kernel/mem/lib/mem.c +3 -0
index 4992843f4..eedfe260f 100644
@@ -250,6 +250,9 @@ void save_max_page_sharing(void)
 
 void restore_max_page_sharing(void)
 {
+       if (!max_page_sharing)
+               return;
+
        if (access(PATH_KSM "max_page_sharing", F_OK) == 0)
                FILE_PRINTF(PATH_KSM "max_page_sharing",
                                 "%d", max_page_sharing);
Signature · 1 line
[email protected]
#2 lore raw

PATCH v2 1/2 restore_max_page_sharing: restore the value only if it was saved

Documentation/vm/ksm.txt states that the minimum value for max_page_sharing is 2, so we can use it as a flag to test if save_max_page_sharing() was called before.

---
Changes to V1:
- made the change to restore_max_page_sharing() following Cyril's proposal
- split V1 into two patches, one - restore_max_page_sharing(), the other - ksm tests

 testcases/kernel/mem/lib/mem.c |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)
testcases/kernel/mem/lib/mem.c +9 -0
index 4992843..f36b33a 100644
@@ -250,6 +250,15 @@ void save_max_page_sharing(void)
 
 void restore_max_page_sharing(void)
 {
+	/*
+	 * Documentation/vm/ksm.txt states that the minimum
+	 * value for max_page_sharing is 2, so on
+	 * max_page_sharing != 0 after save_max_page_sharing()
+	 * returns.
+	 */
+	if (!max_page_sharing)
+		return;
+
 	if (access(PATH_KSM "max_page_sharing", F_OK) == 0)
 	        FILE_PRINTF(PATH_KSM "max_page_sharing",
 	                         "%d", max_page_sharing);
Signature · 1 line
1.7.1
#3 lore raw

PATCH v2 2/2 ksm: perform cleanup actions only when they are actually required

This one is similar to commit 8ae3916674fa
("mem/oom, tunable: perform cleanup actions only when they are actually required") i.e. we are making sure that ksm tests do not revert any not yet performed actions.

---
 testcases/kernel/mem/ksm/ksm02.c |    6 +++++-
 testcases/kernel/mem/ksm/ksm03.c |    6 +++++-
 testcases/kernel/mem/ksm/ksm04.c |   11 +++++++++--
 3 files changed, 19 insertions(+), 4 deletions(-)
testcases/kernel/mem/ksm/ksm02.c +5 -1
index 03b44c0..22c78cb 100644
@@ -59,6 +59,8 @@
 #ifdef HAVE_NUMA_V2
 #include <numaif.h>
 
+static int cpuset_mounted;
+
 static void verify_ksm(void)
 {
 	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
@@ -88,7 +90,8 @@ static void cleanup(void)
 
 	restore_max_page_sharing();
 
-	umount_mem(CPATH, CPATH_NEW);
+	if (cpuset_mounted)
+		umount_mem(CPATH, CPATH_NEW);
 }
 
 static void setup(void)
@@ -106,6 +109,7 @@ static void setup(void)
 	}
 
 	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
+	cpuset_mounted = 1;
 }
 
 static struct tst_test test = {
testcases/kernel/mem/ksm/ksm03.c +5 -1
index 51cc923..284f4ec 100644
@@ -59,6 +59,8 @@
 #include "mem.h"
 #include "ksm_common.h"
 
+static int memcg_mounted;
+
 static void verify_ksm(void)
 {
 	write_memcg();
@@ -79,6 +81,7 @@ static void setup(void)
 	save_max_page_sharing();
 	parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
 	mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
+	memcg_mounted = 1;
 }
 
 static void cleanup(void)
@@ -89,7 +92,8 @@ static void cleanup(void)
 
 	restore_max_page_sharing();
 
-	umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
+	if (memcg_mounted)
+		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 }
 
 static struct tst_test test = {
testcases/kernel/mem/ksm/ksm04.c +9 -2
index 835222f..9ffef4d 100644
@@ -59,6 +59,9 @@
 #ifdef HAVE_NUMA_V2
 #include <numaif.h>
 
+static int cpuset_mounted;
+static int memcg_mounted;
+
 static void verify_ksm(void)
 {
 	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
@@ -90,8 +93,10 @@ static void cleanup(void)
 
 	restore_max_page_sharing();
 
-	umount_mem(CPATH, CPATH_NEW);
-	umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
+	if (cpuset_mounted)
+		umount_mem(CPATH, CPATH_NEW);
+	if (memcg_mounted)
+		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 }
 
 static void setup(void)
@@ -109,7 +114,9 @@ static void setup(void)
 	parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
 
 	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
+	cpuset_mounted = 1;
 	mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
+	memcg_mounted = 1;
 }
 
 static struct tst_test test = {
Signature · 1 line
1.7.1
#5 lore raw

PATCH v2 1/2 restore_max_page_sharing: restore the value only if it was saved

Thank you very much. Pushed.

Quoted 3 lines · Cyril Hrubis

Hi!
Both patches looks good, acked.