Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: selftests: Add helpers to probe for NUMA support, and multi-node systems

Add NUMA helpers to probe for support/availability and to check if the
test is running on a multi-node system. The APIs will be used to verify
guest_memfd NUMA support.

Signed-off-by: Shivank Garg <shivankg@amd.com>
[sean: land helpers in numaif.h, add comments, tweak names]
Link: https://lore.kernel.org/r/20251016172853.52451-11-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>

authored by

Shivank Garg and committed by
Sean Christopherson
e698e89b fe7baebb

+52
+52
tools/testing/selftests/kvm/include/numaif.h
··· 4 4 #ifndef SELFTEST_KVM_NUMAIF_H 5 5 #define SELFTEST_KVM_NUMAIF_H 6 6 7 + #include <dirent.h> 8 + 7 9 #include <linux/mempolicy.h> 8 10 9 11 #include "kvm_syscalls.h" ··· 29 27 KVM_SYSCALL_DEFINE(mbind, 6, void *, addr, unsigned long, size, int, mode, 30 28 const unsigned long *, nodemask, unsigned long, maxnode, 31 29 unsigned int, flags); 30 + 31 + static inline int get_max_numa_node(void) 32 + { 33 + struct dirent *de; 34 + int max_node = 0; 35 + DIR *d; 36 + 37 + /* 38 + * Assume there's a single node if the kernel doesn't support NUMA, 39 + * or if no nodes are found. 40 + */ 41 + d = opendir("/sys/devices/system/node"); 42 + if (!d) 43 + return 0; 44 + 45 + while ((de = readdir(d)) != NULL) { 46 + int node_id; 47 + char *endptr; 48 + 49 + if (strncmp(de->d_name, "node", 4) != 0) 50 + continue; 51 + 52 + node_id = strtol(de->d_name + 4, &endptr, 10); 53 + if (*endptr != '\0') 54 + continue; 55 + 56 + if (node_id > max_node) 57 + max_node = node_id; 58 + } 59 + closedir(d); 60 + 61 + return max_node; 62 + } 63 + 64 + static bool is_numa_available(void) 65 + { 66 + /* 67 + * Probe for NUMA by doing a dummy get_mempolicy(). If the syscall 68 + * fails with ENOSYS, then the kernel was built without NUMA support. 69 + * if the syscall fails with EPERM, then the process/user lacks the 70 + * necessary capabilities (CAP_SYS_NICE). 71 + */ 72 + return !get_mempolicy(NULL, NULL, 0, NULL, 0) || 73 + (errno != ENOSYS && errno != EPERM); 74 + } 75 + 76 + static inline bool is_multi_numa_node_system(void) 77 + { 78 + return is_numa_available() && get_max_numa_node() >= 1; 79 + } 32 80 33 81 #endif /* SELFTEST_KVM_NUMAIF_H */