linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1][cover-letter] mm/mempolicy.c: Fix get_nodes() off-by-one error.
@ 2017-10-06 13:36 Luis Felipe Sandoval Castro
  2017-10-06 13:36 ` [PATCH v1] " Luis Felipe Sandoval Castro
  2017-10-06 22:12 ` [PATCH v1][cover-letter] " Andi Kleen
  0 siblings, 2 replies; 11+ messages in thread
From: Luis Felipe Sandoval Castro @ 2017-10-06 13:36 UTC (permalink / raw)
  To: linux-mm, linux-kernel
  Cc: akpm, vbabka, mhocko, mingo, rientjes, n-horiguchi, salls,
	Luis Felipe Sandoval Castro

According to mbind() and set_mempolicy()'s man pages the argument "maxnode"
specifies the max number of bits in the "nodemask" (which is also to be passed
to these functions) that should be considered for the memory policy. If maxnode
= 2, only two bits are to be considered thus valid node masks are: 0b00, 0b01,
0b10 and 0b11.

In systems with multiple NUMA nodes, sometimes it is useful to set strict
memory policies like MPOL_BIND to restric memory allocations to a single node
maybe because it is the closest node or because is a high bandwidth node,
however an off-by-one error in get_nodes() the function that copies the node
mask from user space requires users to pass maxnode = actual_maxnode + 1 to
mbind()/set_mempolicy(), for instance with 2 nodes maxnode = 3.

Below some code to exemplify this behavior, on a system with 2 NUMA nodes to
force memory allocation on node 1, nodemask = 2 (0b10) and maxnode should be 2,
however if maxnode = 2 set_mempolicy() fails with error code 22, to make this
code work maxnode = 3.  The proposed patch fixes this issue, allowing users to
use maxnode = 2.


// compile with  gcc -std=c99 -lnuma test.c -o test

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sched.h>
#include <numa.h>
#include <numaif.h>

#define NUMBER_OF_LETTERS 26

int main() {
	int cpu = sched_getcpu();
	int node = numa_node_of_cpu(cpu);
	printf("process running on CPU %d numa node %d\n", cpu, node);

	// 2 == 0b10 allocate memory on NUMA node 1
	unsigned long nodemask = 2; 

	// with maxnode = 3 this code works on a system with 2 NUMA nodes
	unsigned long maxnode = 2;

	if (set_mempolicy(MPOL_BIND, &nodemask, maxnode)) {
		printf("set_mempolicy() failed with error code: %d, error string: %s\n",
			errno, strerror(errno));
		exit(-1);
	}

	char *ptr = (char*)malloc(NUMBER_OF_LETTERS * sizeof(char));

	for (int i = 0; i < NUMBER_OF_LETTERS; i++)
	ptr[i] = i + 'a';

	printf("freeing memory...\n");
	free(ptr);

	return 0;
}

Luis Felipe Sandoval Castro (1):
  mm/mempolicy.c: Fix get_nodes() off-by-one error.

 mm/mempolicy.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

-- 
1.8.3.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 11+ messages in thread
* [PATCH v1] mm/mempolicy.c: Fix get_nodes() off-by-one error.
@ 2017-07-18 13:39 Luis Felipe Sandoval Castro
  0 siblings, 0 replies; 11+ messages in thread
From: Luis Felipe Sandoval Castro @ 2017-07-18 13:39 UTC (permalink / raw)
  To: linux-mm, linux-kernel; +Cc: Luis Felipe Sandoval Castro

set_mempolicy() and mbind() take as argument a pointer to a bit mask
(nodemask) and the number of bits in the mask the kernel will use
(maxnode), among others.  For instace on a system with 2 NUMA nodes valid
masks are: 0b00, 0b01, 0b10 and 0b11 it's clear maxnode=2, however an
off-by-one error in get_nodes() the function that copies the node mask from
user space requires users to pass maxnode = 3 in this example and maxnode =
actual_maxnode + 1 in the general case. This patch fixes such error.

Signed-off-by: Luis Felipe Sandoval Castro <luis.felipe.sandoval.castro@intel.com>
---
 mm/mempolicy.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index d911fa5..5274e9d2 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -1208,11 +1208,10 @@ static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
 	unsigned long nlongs;
 	unsigned long endmask;
 
-	--maxnode;
 	nodes_clear(*nodes);
-	if (maxnode == 0 || !nmask)
+	if (maxnode == 1 || !nmask)
 		return 0;
-	if (maxnode > PAGE_SIZE*BITS_PER_BYTE)
+	if (maxnode - 1 > PAGE_SIZE * BITS_PER_BYTE)
 		return -EINVAL;
 
 	nlongs = BITS_TO_LONGS(maxnode);
-- 
1.8.3.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2017-10-19  4:29 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-06 13:36 [PATCH v1][cover-letter] mm/mempolicy.c: Fix get_nodes() off-by-one error Luis Felipe Sandoval Castro
2017-10-06 13:36 ` [PATCH v1] " Luis Felipe Sandoval Castro
2017-10-12  8:46   ` Michal Hocko
2017-10-12  9:14     ` Vlastimil Babka
2017-10-12  9:29       ` Michal Hocko
2017-10-12 15:28     ` Andi Kleen
2017-10-13  8:04       ` Michal Hocko
2017-10-19  3:48         ` Sandoval Castro, Luis Felipe
2017-10-19  4:28           ` Andi Kleen
2017-10-06 22:12 ` [PATCH v1][cover-letter] " Andi Kleen
  -- strict thread matches above, loose matches on Subject: below --
2017-07-18 13:39 [PATCH v1] " Luis Felipe Sandoval Castro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).