github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/mempolicy.cc (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include <errno.h> 16 #include <sys/syscall.h> 17 18 #include "gtest/gtest.h" 19 #include "absl/memory/memory.h" 20 #include "test/util/cleanup.h" 21 #include "test/util/memory_util.h" 22 #include "test/util/test_util.h" 23 #include "test/util/thread_util.h" 24 25 namespace gvisor { 26 namespace testing { 27 28 namespace { 29 30 #define BITS_PER_BYTE 8 31 32 #define MPOL_F_STATIC_NODES (1 << 15) 33 #define MPOL_F_RELATIVE_NODES (1 << 14) 34 #define MPOL_DEFAULT 0 35 #define MPOL_PREFERRED 1 36 #define MPOL_BIND 2 37 #define MPOL_INTERLEAVE 3 38 #define MPOL_LOCAL 4 39 #define MPOL_F_NODE (1 << 0) 40 #define MPOL_F_ADDR (1 << 1) 41 #define MPOL_F_MEMS_ALLOWED (1 << 2) 42 #define MPOL_MF_STRICT (1 << 0) 43 #define MPOL_MF_MOVE (1 << 1) 44 #define MPOL_MF_MOVE_ALL (1 << 2) 45 46 int get_mempolicy(int* policy, uint64_t* nmask, uint64_t maxnode, void* addr, 47 int flags) { 48 return syscall(SYS_get_mempolicy, policy, nmask, maxnode, addr, flags); 49 } 50 51 int set_mempolicy(int mode, uint64_t* nmask, uint64_t maxnode) { 52 return syscall(SYS_set_mempolicy, mode, nmask, maxnode); 53 } 54 55 int mbind(void* addr, unsigned long len, int mode, 56 const unsigned long* nodemask, unsigned long maxnode, 57 unsigned flags) { 58 return syscall(SYS_mbind, addr, len, mode, nodemask, maxnode, flags); 59 } 60 61 // Creates a cleanup object that resets the calling thread's mempolicy to the 62 // system default when the calling scope ends. 63 Cleanup ScopedMempolicy() { 64 return Cleanup([] { 65 EXPECT_THAT(set_mempolicy(MPOL_DEFAULT, nullptr, 0), SyscallSucceeds()); 66 }); 67 } 68 69 // Temporarily change the memory policy for the calling thread within the 70 // caller's scope. 71 PosixErrorOr<Cleanup> ScopedSetMempolicy(int mode, uint64_t* nmask, 72 uint64_t maxnode) { 73 if (set_mempolicy(mode, nmask, maxnode)) { 74 return PosixError(errno, "set_mempolicy"); 75 } 76 return ScopedMempolicy(); 77 } 78 79 TEST(MempolicyTest, CheckDefaultPolicy) { 80 int mode = 0; 81 uint64_t nodemask = 0; 82 ASSERT_THAT(get_mempolicy(&mode, &nodemask, sizeof(nodemask) * BITS_PER_BYTE, 83 nullptr, 0), 84 SyscallSucceeds()); 85 86 EXPECT_EQ(MPOL_DEFAULT, mode); 87 EXPECT_EQ(0x0, nodemask); 88 } 89 90 TEST(MempolicyTest, PolicyPreservedAfterSetMempolicy) { 91 uint64_t nodemask = 0x1; 92 auto cleanup = ASSERT_NO_ERRNO_AND_VALUE(ScopedSetMempolicy( 93 MPOL_BIND, &nodemask, sizeof(nodemask) * BITS_PER_BYTE)); 94 95 int mode = 0; 96 uint64_t nodemask_after = 0x0; 97 ASSERT_THAT(get_mempolicy(&mode, &nodemask_after, 98 sizeof(nodemask_after) * BITS_PER_BYTE, nullptr, 0), 99 SyscallSucceeds()); 100 EXPECT_EQ(MPOL_BIND, mode); 101 EXPECT_EQ(0x1, nodemask_after); 102 103 // Try throw in some mode flags. 104 for (auto mode_flag : {MPOL_F_STATIC_NODES, MPOL_F_RELATIVE_NODES}) { 105 auto cleanup2 = ASSERT_NO_ERRNO_AND_VALUE( 106 ScopedSetMempolicy(MPOL_INTERLEAVE | mode_flag, &nodemask, 107 sizeof(nodemask) * BITS_PER_BYTE)); 108 mode = 0; 109 nodemask_after = 0x0; 110 ASSERT_THAT( 111 get_mempolicy(&mode, &nodemask_after, 112 sizeof(nodemask_after) * BITS_PER_BYTE, nullptr, 0), 113 SyscallSucceeds()); 114 EXPECT_EQ(MPOL_INTERLEAVE | mode_flag, mode); 115 EXPECT_EQ(0x1, nodemask_after); 116 } 117 } 118 119 TEST(MempolicyTest, SetMempolicyRejectsInvalidInputs) { 120 auto cleanup = ScopedMempolicy(); 121 uint64_t nodemask; 122 123 if (IsRunningOnGvisor()) { 124 // Invalid nodemask, we only support a single node on gvisor. 125 nodemask = 0x4; 126 ASSERT_THAT(set_mempolicy(MPOL_DEFAULT, &nodemask, 127 sizeof(nodemask) * BITS_PER_BYTE), 128 SyscallFailsWithErrno(EINVAL)); 129 } 130 131 nodemask = 0x1; 132 133 // Invalid mode. 134 ASSERT_THAT(set_mempolicy(7439, &nodemask, sizeof(nodemask) * BITS_PER_BYTE), 135 SyscallFailsWithErrno(EINVAL)); 136 137 // Invalid nodemask size. 138 ASSERT_THAT(set_mempolicy(MPOL_DEFAULT, &nodemask, 0), 139 SyscallFailsWithErrno(EINVAL)); 140 141 // Invalid mode flag. 142 ASSERT_THAT( 143 set_mempolicy(MPOL_DEFAULT | MPOL_F_STATIC_NODES | MPOL_F_RELATIVE_NODES, 144 &nodemask, sizeof(nodemask) * BITS_PER_BYTE), 145 SyscallFailsWithErrno(EINVAL)); 146 147 // MPOL_INTERLEAVE with empty nodemask. 148 nodemask = 0x0; 149 ASSERT_THAT(set_mempolicy(MPOL_INTERLEAVE, &nodemask, 150 sizeof(nodemask) * BITS_PER_BYTE), 151 SyscallFailsWithErrno(EINVAL)); 152 } 153 154 // The manpages specify that the nodemask provided to set_mempolicy are 155 // considered empty if the nodemask pointer is null, or if the nodemask size is 156 // 0. We use a policy which accepts both empty and non-empty nodemasks 157 // (MPOL_PREFERRED), a policy which requires a non-empty nodemask (MPOL_BIND), 158 // and a policy which completely ignores the nodemask (MPOL_DEFAULT) to verify 159 // argument checking around nodemasks. 160 TEST(MempolicyTest, EmptyNodemaskOnSet) { 161 auto cleanup = ScopedMempolicy(); 162 163 EXPECT_THAT(set_mempolicy(MPOL_DEFAULT, nullptr, 1), SyscallSucceeds()); 164 EXPECT_THAT(set_mempolicy(MPOL_BIND, nullptr, 1), 165 SyscallFailsWithErrno(EINVAL)); 166 EXPECT_THAT(set_mempolicy(MPOL_PREFERRED, nullptr, 1), SyscallSucceeds()); 167 168 uint64_t nodemask = 0x1; 169 EXPECT_THAT(set_mempolicy(MPOL_DEFAULT, &nodemask, 0), 170 SyscallFailsWithErrno(EINVAL)); 171 EXPECT_THAT(set_mempolicy(MPOL_BIND, &nodemask, 0), 172 SyscallFailsWithErrno(EINVAL)); 173 EXPECT_THAT(set_mempolicy(MPOL_PREFERRED, &nodemask, 0), 174 SyscallFailsWithErrno(EINVAL)); 175 } 176 177 TEST(MempolicyTest, QueryAvailableNodes) { 178 uint64_t nodemask = 0; 179 ASSERT_THAT( 180 get_mempolicy(nullptr, &nodemask, sizeof(nodemask) * BITS_PER_BYTE, 181 nullptr, MPOL_F_MEMS_ALLOWED), 182 SyscallSucceeds()); 183 // We can only be sure there is a single node if running on gvisor. 184 if (IsRunningOnGvisor()) { 185 EXPECT_EQ(0x1, nodemask); 186 } 187 188 // MPOL_F_ADDR and MPOL_F_NODE flags may not be combined with 189 // MPOL_F_MEMS_ALLLOWED. 190 for (auto flags : 191 {MPOL_F_MEMS_ALLOWED | MPOL_F_ADDR, MPOL_F_MEMS_ALLOWED | MPOL_F_NODE, 192 MPOL_F_MEMS_ALLOWED | MPOL_F_ADDR | MPOL_F_NODE}) { 193 ASSERT_THAT(get_mempolicy(nullptr, &nodemask, 194 sizeof(nodemask) * BITS_PER_BYTE, nullptr, flags), 195 SyscallFailsWithErrno(EINVAL)); 196 } 197 } 198 199 TEST(MempolicyTest, GetMempolicyQueryNodeForAddress) { 200 uint64_t dummy_stack_address; 201 auto dummy_heap_address = absl::make_unique<uint64_t>(); 202 int mode; 203 204 for (auto ptr : {&dummy_stack_address, dummy_heap_address.get()}) { 205 mode = -1; 206 ASSERT_THAT( 207 get_mempolicy(&mode, nullptr, 0, ptr, MPOL_F_ADDR | MPOL_F_NODE), 208 SyscallSucceeds()); 209 // If we're not running on gvisor, the address may be allocated on a 210 // different numa node. 211 if (IsRunningOnGvisor()) { 212 EXPECT_EQ(0, mode); 213 } 214 } 215 216 void* invalid_address = reinterpret_cast<void*>(-1); 217 218 // Invalid address. 219 ASSERT_THAT(get_mempolicy(&mode, nullptr, 0, invalid_address, 220 MPOL_F_ADDR | MPOL_F_NODE), 221 SyscallFailsWithErrno(EFAULT)); 222 223 // Invalid mode pointer. 224 ASSERT_THAT(get_mempolicy(reinterpret_cast<int*>(invalid_address), nullptr, 0, 225 &dummy_stack_address, MPOL_F_ADDR | MPOL_F_NODE), 226 SyscallFailsWithErrno(EFAULT)); 227 } 228 229 TEST(MempolicyTest, GetMempolicyCanOmitPointers) { 230 int mode; 231 uint64_t nodemask; 232 233 // Omit nodemask pointer. 234 ASSERT_THAT(get_mempolicy(&mode, nullptr, 0, nullptr, 0), SyscallSucceeds()); 235 // Omit mode pointer. 236 ASSERT_THAT(get_mempolicy(nullptr, &nodemask, 237 sizeof(nodemask) * BITS_PER_BYTE, nullptr, 0), 238 SyscallSucceeds()); 239 // Omit both pointers. 240 ASSERT_THAT(get_mempolicy(nullptr, nullptr, 0, nullptr, 0), 241 SyscallSucceeds()); 242 } 243 244 TEST(MempolicyTest, GetMempolicyNextInterleaveNode) { 245 int mode; 246 // Policy for thread not yet set to MPOL_INTERLEAVE, can't query for 247 // the next node which will be used for allocation. 248 ASSERT_THAT(get_mempolicy(&mode, nullptr, 0, nullptr, MPOL_F_NODE), 249 SyscallFailsWithErrno(EINVAL)); 250 251 // Set default policy for thread to MPOL_INTERLEAVE. 252 uint64_t nodemask = 0x1; 253 auto cleanup = ASSERT_NO_ERRNO_AND_VALUE(ScopedSetMempolicy( 254 MPOL_INTERLEAVE, &nodemask, sizeof(nodemask) * BITS_PER_BYTE)); 255 256 mode = -1; 257 ASSERT_THAT(get_mempolicy(&mode, nullptr, 0, nullptr, MPOL_F_NODE), 258 SyscallSucceeds()); 259 EXPECT_EQ(0, mode); 260 } 261 262 TEST(MempolicyTest, Mbind) { 263 // Temporarily set the thread policy to MPOL_PREFERRED. 264 const auto cleanup_thread_policy = 265 ASSERT_NO_ERRNO_AND_VALUE(ScopedSetMempolicy(MPOL_PREFERRED, nullptr, 0)); 266 267 const auto mapping = ASSERT_NO_ERRNO_AND_VALUE( 268 MmapAnon(kPageSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS)); 269 270 // vmas default to MPOL_DEFAULT irrespective of the thread policy (currently 271 // MPOL_PREFERRED). 272 int mode; 273 ASSERT_THAT(get_mempolicy(&mode, nullptr, 0, mapping.ptr(), MPOL_F_ADDR), 274 SyscallSucceeds()); 275 EXPECT_EQ(mode, MPOL_DEFAULT); 276 277 // Set MPOL_PREFERRED for the vma and read it back. 278 ASSERT_THAT( 279 mbind(mapping.ptr(), mapping.len(), MPOL_PREFERRED, nullptr, 0, 0), 280 SyscallSucceeds()); 281 ASSERT_THAT(get_mempolicy(&mode, nullptr, 0, mapping.ptr(), MPOL_F_ADDR), 282 SyscallSucceeds()); 283 EXPECT_EQ(mode, MPOL_PREFERRED); 284 } 285 286 } // namespace 287 288 } // namespace testing 289 } // namespace gvisor