Path: blob/master/tools/testing/selftests/kvm/include/numaif.h
38245 views
/* SPDX-License-Identifier: GPL-2.0-only */1/* Copyright (C) 2020, Google LLC. */23#ifndef SELFTEST_KVM_NUMAIF_H4#define SELFTEST_KVM_NUMAIF_H56#include <dirent.h>78#include <linux/mempolicy.h>910#include "kvm_syscalls.h"1112KVM_SYSCALL_DEFINE(get_mempolicy, 5, int *, policy, const unsigned long *, nmask,13unsigned long, maxnode, void *, addr, int, flags);1415KVM_SYSCALL_DEFINE(set_mempolicy, 3, int, mode, const unsigned long *, nmask,16unsigned long, maxnode);1718KVM_SYSCALL_DEFINE(set_mempolicy_home_node, 4, unsigned long, start,19unsigned long, len, unsigned long, home_node,20unsigned long, flags);2122KVM_SYSCALL_DEFINE(migrate_pages, 4, int, pid, unsigned long, maxnode,23const unsigned long *, frommask, const unsigned long *, tomask);2425KVM_SYSCALL_DEFINE(move_pages, 6, int, pid, unsigned long, count, void *, pages,26const int *, nodes, int *, status, int, flags);2728KVM_SYSCALL_DEFINE(mbind, 6, void *, addr, unsigned long, size, int, mode,29const unsigned long *, nodemask, unsigned long, maxnode,30unsigned int, flags);3132static inline int get_max_numa_node(void)33{34struct dirent *de;35int max_node = 0;36DIR *d;3738/*39* Assume there's a single node if the kernel doesn't support NUMA,40* or if no nodes are found.41*/42d = opendir("/sys/devices/system/node");43if (!d)44return 0;4546while ((de = readdir(d)) != NULL) {47int node_id;48char *endptr;4950if (strncmp(de->d_name, "node", 4) != 0)51continue;5253node_id = strtol(de->d_name + 4, &endptr, 10);54if (*endptr != '\0')55continue;5657if (node_id > max_node)58max_node = node_id;59}60closedir(d);6162return max_node;63}6465static bool is_numa_available(void)66{67/*68* Probe for NUMA by doing a dummy get_mempolicy(). If the syscall69* fails with ENOSYS, then the kernel was built without NUMA support.70* if the syscall fails with EPERM, then the process/user lacks the71* necessary capabilities (CAP_SYS_NICE).72*/73return !get_mempolicy(NULL, NULL, 0, NULL, 0) ||74(errno != ENOSYS && errno != EPERM);75}7677static inline bool is_multi_numa_node_system(void)78{79return is_numa_available() && get_max_numa_node() >= 1;80}8182#endif /* SELFTEST_KVM_NUMAIF_H */838485