Skip to content

Commit

Permalink
libnuma: add numa_set_mempolicy_home_node API
Browse files Browse the repository at this point in the history
add numa_set_mempolicy_home_node to set home_node for mbind or
preferred_many policy.

link: https://lore.kernel.org/all/20211202123810.267175-3-aneesh.kumar@linux.ibm.com/T/#u
Signed-off-by: Chunsheng Luo <luochunsheng@ustc.edu>
  • Loading branch information
luochenglcs authored and andikleen committed Feb 1, 2024
1 parent 63befa8 commit 8f2ffc8
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
34 changes: 34 additions & 0 deletions libnuma.c
Original file line number Diff line number Diff line change
Expand Up @@ -2237,3 +2237,37 @@ struct bitmask * numa_parse_cpustring_all(const char *s)
{
return __numa_parse_cpustring(s, numa_possible_cpus_ptr);
}

int numa_has_home_node(void)
{
void *mem;
static int has_home_node = -1;
int page_size = numa_pagesize();
struct bitmask *tmp = numa_get_mems_allowed();

if (has_home_node >= 0)
goto out;

has_home_node = 0;
/* Detect whether home_node is supported */
mem = mmap(0, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (mem != MAP_FAILED) {
dombind(mem, page_size, MPOL_BIND, tmp);
if (set_mempolicy_home_node(mem, page_size, numa_find_first(tmp), 0) == 0)
has_home_node = 1;
munmap(mem, page_size);
}

out:
return has_home_node;
}

int numa_set_mempolicy_home_node(void *start, unsigned long len, int home_node, int flags)
{
if (set_mempolicy_home_node(start, len, home_node, flags)) {
numa_error("set_mempolicy_home_node");
return -1;
}

return 0;
}
13 changes: 13 additions & 0 deletions numa.3
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ numa \- NUMA policy library
.br
.BI "void numa_set_preferred_many(struct bitmask *" nodemask );
.br
.BI "int numa_has_home_node(void);
.br
.BI "int numa_set_mempolicy_home_node(void *start, unsigned long len, int home_node, int flags);
.br
.BI "int numa_get_interleave_node(void);
.br
.B struct bitmask *numa_get_interleave_mask(void);
Expand Down Expand Up @@ -465,6 +469,15 @@ multiple preferred nodes.
The caller is responsible for freeing the mask with
.BR numa_bitmask_free ().

.BR numa_has_home_node()
Returns 1 if the system supports setting home_node for mbind and preferred_many.

.BR numa_set_mempolicy_home_node()
set the home node for a VMA policy present in the task's address range.
A home node is the NUMA node closest to which page allocation will come from.
Users should use it after setting up a mbind or perfered_many memory policy
for the specified range.

.BR numa_get_interleave_mask ()
returns the current interleave mask if the task's memory allocation policy
is page interleaved.
Expand Down
9 changes: 9 additions & 0 deletions numa.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ struct bitmask *numa_parse_cpustring(const char *);
* dependency */
struct bitmask *numa_parse_cpustring_all(const char *);

/* Returns whether or not the system supports setting home_node for mbind
* and preferred_many.
*/
int numa_has_home_node(void);

/* set the home node for a VMA policy present in the task's address range */
int numa_set_mempolicy_home_node(void *start, unsigned long len,
int home_node, int flags);

/*
* The following functions are for source code compatibility
* with releases prior to version 2.
Expand Down
3 changes: 3 additions & 0 deletions numaif.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ extern long migrate_pages(int pid, unsigned long maxnode,
extern long move_pages(int pid, unsigned long count,
void **pages, const int *nodes, int *status, int flags);

extern int set_mempolicy_home_node(void *start, unsigned long len,
int home_node, int flag);

/* Policies */
#define MPOL_DEFAULT 0
#define MPOL_PREFERRED 1
Expand Down
15 changes: 15 additions & 0 deletions syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@

#endif

#if !defined(__NR_set_mempolicy_home_node)

#if defined(__x86_64__) || defined(__aarch64__)
#define __NR_set_mempolicy_home_node 450
#else
#error "Add syscalls for your architecture or update kernel headers"
#endif

#endif

#ifndef __GLIBC_PREREQ
# define __GLIBC_PREREQ(x,y) 0
#endif
Expand Down Expand Up @@ -249,6 +259,11 @@ long WEAK move_pages(int pid, unsigned long count,
return syscall(__NR_move_pages, pid, count, pages, nodes, status, flags);
}

int WEAK set_mempolicy_home_node(void *start, unsigned long len, int home_node, int flags)
{
return syscall(__NR_set_mempolicy_home_node, start, len, home_node, flags);
}

/* SLES8 glibc doesn't define those */
SYMVER("numa_sched_setaffinity_v1", "numa_sched_setaffinity@libnuma_1.1")
int numa_sched_setaffinity_v1(pid_t pid, unsigned len, const unsigned long *mask)
Expand Down
8 changes: 8 additions & 0 deletions versions.ldscript
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,11 @@ libnuma_1.6{
local:
*;
} libnuma_1.5;

libnuma_1.7{
global:
numa_has_home_node;
numa_set_mempolicy_home_node;
local:
*;
} libnuma_1.6;

0 comments on commit 8f2ffc8

Please sign in to comment.