Skip to contentSkip to conversation

move_pages_support: use only allowed nodes

PATCH In review Jan Stancek 2 participants 2 messages May 25, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 move_pages_support: use only allowed nodes

Some setups can lack memory on arbitrary nodes, use only nodes returned by get_mempolicy(..., MPOL_F_MEMS_ALLOWED).

---
 .../syscalls/move_pages/move_pages_support.c       |   74 ++++++++++++++++----
 .../syscalls/move_pages/move_pages_support.h       |    1 +
 2 files changed, 62 insertions(+), 13 deletions(-)
testcases/kernel/syscalls/move_pages/move_pages_support.c +61 -13
index 320486f..83a841b 100644
@@ -22,6 +22,7 @@
 #include <semaphore.h>
 #include "test.h"
 #include "usctest.h"
+#include "safe_macros.h"
 #include "move_pages_support.h"
 
 long get_page_size()
@@ -98,6 +99,46 @@ int alloc_pages_on_nodes(void **pages, unsigned int num, int *nodes)
 }
 
 /*
+ * get_allowed_nodes - get number and array of available nodes
+ * @num_allowed_nodes: pointer where number of available nodes will be stored
+ * @allowed_nodes: array of available node ids, this is MPOL_F_MEMS_ALLOWED
+ *                 node bitmask compacted (without holes), so that each field
+ *                 contains node number. If NULL only num_allowed_nodes is
+ *                 returned, otherwise it cotains new allocated array,
+ *                 which caller is responsible to free.
+ */
+void get_allowed_nodes(int *num_allowed_nodes, int **allowed_nodes)
+{
+#if HAVE_NUMA_H
+	int i;
+	struct bitmask *allowed_nodemask = NULL;
+
+	*num_allowed_nodes = 0;
+	allowed_nodemask = numa_allocate_nodemask();
+	if (allowed_nodes)
+		*allowed_nodes = SAFE_MALLOC(NULL,
+			sizeof(int)*allowed_nodemask->size);
+	/*
+	 * avoid numa_get_mems_allowed(), because of bug in getpol()
+	 * utility function in older versions:
+	 * http://www.spinics.net/lists/linux-numa/msg00849.html
+	 */
+	if (syscall(__NR_get_mempolicy, NULL, allowed_nodemask->maskp,
+		allowed_nodemask->size, 0, MPOL_F_MEMS_ALLOWED) < 0)
+		tst_resm(TFAIL|TERRNO, "get_mempolicy: MPOL_F_MEMS_ALLOWED");
+
+	for (i = 0; i <	allowed_nodemask->size; i++) {
+		if (numa_bitmask_isbitset(allowed_nodemask, i)) {
+			if (allowed_nodes)
+				(*allowed_nodes)[*num_allowed_nodes] = i;
+			(*num_allowed_nodes)++;
+		}
+	}
+	numa_bitmask_free(allowed_nodemask);
+#endif
+}
+
+/*
  * alloc_pages_linear() - allocate pages in each NUMA node
  * @pages: array in which the page pointers will be stored
  * @num: no. of pages to allocate
@@ -114,16 +155,18 @@ int alloc_pages_linear(void **pages, unsigned int num)
 
 #if HAVE_NUMA_H
 	unsigned int i;
-	unsigned int n;
+	unsigned int n = 0;
+	int num_allowed_nodes;
+	int *allowed_nodes;
 
-	n = 0;
+	get_allowed_nodes(&num_allowed_nodes, &allowed_nodes);
 	for (i = 0; i < num; i++) {
-		nodes[i] = n;
-
+		nodes[i] = allowed_nodes[n];
 		n++;
-		if (n > numa_max_node())
+		if (n >= num_allowed_nodes)
 			n = 0;
 	}
+	free(allowed_nodes);
 #endif
 
 	return alloc_pages_on_nodes(pages, num, nodes);
@@ -210,18 +253,19 @@ void verify_pages_linear(void **pages, int *status, unsigned int num)
 {
 #if HAVE_NUMA_H
 	unsigned int i;
-	unsigned int n;
+	unsigned int n = 0;
 	int nodes[num];
+	int num_allowed_nodes;
+	int *allowed_nodes;
 
-	n = 0;
-
+	get_allowed_nodes(&num_allowed_nodes, &allowed_nodes);
 	for (i = 0; i < num; i++) {
-		nodes[i] = i;
-
+		nodes[i] = allowed_nodes[n];
 		n++;
-		if (n > numa_max_node())
+		if (n >= num_allowed_nodes)
 			n = 0;
 	}
+	free(allowed_nodes);
 
 	verify_pages_on_nodes(pages, status, num, nodes);
 #endif
@@ -381,10 +425,14 @@ void free_sem(sem_t * sem, int num)
 void check_config(unsigned int min_nodes)
 {
 #if HAVE_NUMA_H && HAVE_NUMAIF_H
+	int num_allowed_nodes;
+	get_allowed_nodes(&num_allowed_nodes, NULL);
+
 	if (numa_available() < 0) {
 		tst_brkm(TCONF, NULL, "NUMA support is not available");
-	} else if (numa_max_node() < (min_nodes - 1)) {
-		tst_brkm(TCONF, NULL, "atleast 2 NUMA nodes are required");
+	} else if (num_allowed_nodes < min_nodes) {
+		tst_brkm(TCONF, NULL, "at least %d allowed NUMA nodes"
+		    " are required", min_nodes);
 	} else if (tst_kvercmp(2, 6, 18) < 0) {
 		tst_brkm(TCONF, NULL, "2.6.18 or greater kernel required");
 	}
testcases/kernel/syscalls/move_pages/move_pages_support.h +1 -0
index fd78572..a10293b 100644
@@ -29,6 +29,7 @@
 #include <semaphore.h>
 
 long get_page_size();
+void get_allowed_nodes(int *num_allowed_nodes, int **allowed_nodes);
 
 void free_pages(void **pages, unsigned int num);
 

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/

_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#1 lore raw
Quoted 9 lines · Jan Stancek

Some setups can lack memory on arbitrary nodes, use only nodes returned by get_mempolicy(..., MPOL_F_MEMS_ALLOWED).

---
 .../syscalls/move_pages/move_pages_support.c       |   74 ++++++++++++++++---- .../syscalls/move_pages/move_pages_support.h       |    1 +

 2 files changed, 62 insertions(+), 13 deletions(-)

+ if (syscall(__NR_get_mempolicy, NULL, allowed_nodemask->maskp,
+ allowed_nodemask->size, 0, MPOL_F_MEMS_ALLOWED) < 0)
+ tst_resm(TFAIL|TERRNO, "get_mempolicy: MPOL_F_MEMS_ALLOWED");
+

Would tst_brkm be better here? Also, what happens in the negative case (HAVE_NUMA_H is not defined)? Wouldn't that segfault accessing random memory or something along those lines?

I think it would be better to design the API (get_allowed_nodes) to return an integer value to denote success or failure, then test for that in main and report errors there.

Thanks!
-Garrett

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list