1 /*- 2 * Copyright (c) 2022 Dmitry Chagin <dchagin@FreeBSD.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 #include <sys/cdefs.h> 7 #include <sys/types.h> 8 #include <sys/stdint.h> 9 #include <sys/sysctl.h> 10 11 #include <errno.h> 12 #include <sched.h> 13 14 #include <atf-c.h> 15 16 static uint32_t maxcpuid; 17 static uint32_t maxcpus; 18 static uint32_t cpus; 19 20 static uint32_t 21 support_getcpus(void) 22 { 23 uint32_t val; 24 size_t sz = sizeof(val); 25 26 ATF_REQUIRE(sysctlbyname("kern.smp.cpus", &val, &sz, NULL, 0) == 0); 27 return (val); 28 } 29 30 static uint32_t 31 support_getmaxcpus(void) 32 { 33 uint32_t val; 34 size_t sz = sizeof(val); 35 36 ATF_REQUIRE(sysctlbyname("kern.smp.maxcpus", &val, &sz, NULL, 0) == 0); 37 return (val); 38 } 39 40 static uint32_t 41 support_getmaxcpuid(void) 42 { 43 cpuset_t *set; 44 int setsize, rv; 45 uint32_t i, id; 46 47 for (i = 1; i < maxcpus; i++) { 48 setsize = CPU_ALLOC_SIZE(i); 49 set = CPU_ALLOC(i); 50 ATF_REQUIRE(set != NULL); 51 CPU_ZERO_S(setsize, set); 52 rv = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, 53 -1, setsize, set); 54 if (rv == 0) { 55 id = __BIT_FLS(i, set) - 1; 56 CPU_FREE(set); 57 break; 58 } 59 CPU_FREE(set); 60 } 61 ATF_REQUIRE(rv == 0); 62 return (id); 63 } 64 65 ATF_TC_WITHOUT_HEAD(test_setinvalidcpu); 66 ATF_TC_BODY(test_setinvalidcpu, tc) 67 { 68 size_t cpusetsize; 69 cpuset_t *set; 70 71 cpusetsize = CPU_ALLOC_SIZE(maxcpuid + 1); 72 set = CPU_ALLOC(maxcpuid + 1); 73 ATF_REQUIRE(set != NULL); 74 CPU_ZERO_S(cpusetsize, set); 75 CPU_SET_S(maxcpuid + 1, cpusetsize, set); 76 CPU_SET_S(maxcpuid - 1, cpusetsize, set); 77 ATF_REQUIRE(sched_setaffinity(0, cpusetsize, set) == 0); 78 CPU_FREE(set); 79 80 cpusetsize = CPU_ALLOC_SIZE(maxcpus + 1); 81 set = CPU_ALLOC(maxcpus + 1); 82 ATF_REQUIRE(set != NULL); 83 CPU_ZERO_S(cpusetsize, set); 84 CPU_SET_S(maxcpuid + 1, cpusetsize, set); 85 CPU_SET_S(maxcpuid - 1, cpusetsize, set); 86 ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, 87 -1, cpusetsize, set) == -1); 88 ATF_REQUIRE_EQ(errno, EINVAL); 89 CPU_FREE(set); 90 } 91 92 ATF_TC_WITHOUT_HEAD(test_setvalidcpu); 93 ATF_TC_BODY(test_setvalidcpu, tc) 94 { 95 size_t cpusetsize; 96 cpuset_t *set; 97 int cpu; 98 99 ATF_REQUIRE(maxcpuid < maxcpus); 100 cpu = maxcpuid > 1 ? maxcpuid - 1 : 0; 101 102 cpusetsize = CPU_ALLOC_SIZE(maxcpus + 1); 103 set = CPU_ALLOC(maxcpus + 1); 104 ATF_REQUIRE(set != NULL); 105 CPU_ZERO_S(cpusetsize, set); 106 CPU_SET_S(cpu, cpusetsize, set); 107 ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, 108 -1, cpusetsize, set) == 0); 109 ATF_REQUIRE_EQ(cpu, sched_getcpu()); 110 CPU_FREE(set); 111 } 112 113 ATF_TC_WITHOUT_HEAD(test_setzeroset1); 114 ATF_TC_BODY(test_setzeroset1, tc) 115 { 116 size_t cpusetsize; 117 cpuset_t *set; 118 119 cpusetsize = CPU_ALLOC_SIZE(maxcpuid + 1); 120 set = CPU_ALLOC(maxcpuid + 1); 121 ATF_REQUIRE(set != NULL); 122 CPU_ZERO_S(cpusetsize, set); 123 ATF_REQUIRE(sched_setaffinity(0, cpusetsize, set) == -1); 124 ATF_REQUIRE_EQ(errno, EINVAL); 125 CPU_FREE(set); 126 } 127 128 ATF_TC_WITHOUT_HEAD(test_setzeroset2); 129 ATF_TC_BODY(test_setzeroset2, tc) 130 { 131 size_t cpusetsize; 132 cpuset_t *set; 133 134 cpusetsize = CPU_ALLOC_SIZE(maxcpuid + 1); 135 set = CPU_ALLOC(maxcpuid + 1); 136 ATF_REQUIRE(set != NULL); 137 CPU_ZERO_S(cpusetsize, set); 138 ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, 139 -1, cpusetsize, set) == -1); 140 ATF_REQUIRE_EQ(errno, EDEADLK); 141 CPU_FREE(set); 142 } 143 144 ATF_TC_WITHOUT_HEAD(test_setmaxsetsize); 145 ATF_TC_BODY(test_setmaxsetsize, tc) 146 { 147 size_t cpusetsize; 148 cpuset_t *set; 149 150 cpusetsize = CPU_ALLOC_SIZE(maxcpus * 2); 151 set = CPU_ALLOC(maxcpus * 2); 152 ATF_REQUIRE(set != NULL); 153 CPU_ZERO_S(cpusetsize, set); 154 ATF_REQUIRE(CPU_COUNT_S(cpusetsize, set) == 0); 155 CPU_SET_S(0, cpusetsize, set); 156 ATF_REQUIRE(CPU_COUNT_S(cpusetsize, set) == 1); 157 ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, 158 -1, cpusetsize, set) == 0); 159 160 CPU_ZERO_S(cpusetsize, set); 161 CPU_SET_S(maxcpuid, cpusetsize, set); 162 ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, 163 -1, cpusetsize, set) == 0); 164 165 CPU_ZERO_S(cpusetsize, set); 166 CPU_SET_S(maxcpuid + 1, cpusetsize, set); 167 ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, 168 -1, cpusetsize, set) == -1); 169 ATF_REQUIRE_EQ(errno, EINVAL); 170 CPU_FREE(set); 171 } 172 173 ATF_TC_WITHOUT_HEAD(test_setminsetsize); 174 ATF_TC_BODY(test_setminsetsize, tc) 175 { 176 size_t cpusetsize = 1; 177 int8_t set; 178 179 if (cpus <= 8) 180 return; 181 182 set = 1; 183 ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, 184 -1, cpusetsize, (const cpuset_t *)&set) == 0); 185 set = 0; 186 ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, 187 -1, cpusetsize, (const cpuset_t *)&set) == -1); 188 ATF_REQUIRE_EQ(errno, EDEADLK); 189 } 190 191 ATF_TC_WITHOUT_HEAD(test_getminsetsize); 192 ATF_TC_BODY(test_getminsetsize, tc) 193 { 194 size_t cpusetsize = 1; 195 int8_t set = 0; 196 197 if (cpus < 9) 198 return; 199 ATF_REQUIRE(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, 200 -1, cpusetsize, (cpuset_t *)&set) == -1); 201 ATF_REQUIRE_EQ(errno, ERANGE); 202 } 203 204 ATF_TC_WITHOUT_HEAD(test_getsetsize); 205 ATF_TC_BODY(test_getsetsize, tc) 206 { 207 size_t cpusetsize; 208 cpuset_t *set; 209 210 cpusetsize = CPU_ALLOC_SIZE(maxcpuid + 1); 211 set = CPU_ALLOC(maxcpuid + 1); 212 ATF_REQUIRE(set != NULL); 213 CPU_ZERO_S(cpusetsize, set); 214 ATF_REQUIRE(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, 215 -1, cpusetsize, set) == 0); 216 CPU_FREE(set); 217 } 218 219 ATF_TC_WITHOUT_HEAD(test_holes); 220 ATF_TC_BODY(test_holes, tc) 221 { 222 cpuset_t *set; 223 int cpusetsize; 224 225 cpusetsize = CPU_ALLOC_SIZE(maxcpus * 2); 226 set = CPU_ALLOC(maxcpus * 2); 227 ATF_REQUIRE(set != NULL); 228 CPU_ZERO_S(cpusetsize, set); 229 ATF_REQUIRE(CPU_COUNT_S(cpusetsize, set) == 0); 230 CPU_SET_S(maxcpuid, cpusetsize, set); 231 ATF_REQUIRE(CPU_COUNT_S(cpusetsize, set) == 1); 232 ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, 233 -1, cpusetsize, set) == 0); 234 235 CPU_ZERO_S(cpusetsize, set); 236 ATF_REQUIRE(CPU_COUNT_S(cpusetsize, set) == 0); 237 CPU_SET_S(maxcpuid + 1, cpusetsize, set); 238 ATF_REQUIRE(CPU_COUNT_S(cpusetsize, set) == 1); 239 ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, 240 -1, cpusetsize, set) == -1); 241 ATF_REQUIRE_EQ(errno, EINVAL); 242 243 ATF_REQUIRE(CPU_COUNT_S(cpusetsize, set) == 1); 244 ATF_REQUIRE(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, 245 -1, cpusetsize, set) == 0); 246 ATF_REQUIRE(CPU_ISSET_S(maxcpuid + 1, cpusetsize, set) == false); 247 ATF_REQUIRE(CPU_ISSET_S(maxcpuid, cpusetsize, set) == true); 248 ATF_REQUIRE_EQ(maxcpuid, (uint32_t)sched_getcpu()); 249 } 250 251 ATF_TP_ADD_TCS(tp) 252 { 253 254 cpus = support_getcpus(); 255 maxcpus = support_getmaxcpus(); 256 maxcpuid = support_getmaxcpuid(); 257 258 ATF_TP_ADD_TC(tp, test_setinvalidcpu); 259 ATF_TP_ADD_TC(tp, test_setvalidcpu); 260 ATF_TP_ADD_TC(tp, test_setzeroset1); 261 ATF_TP_ADD_TC(tp, test_setzeroset2); 262 263 ATF_TP_ADD_TC(tp, test_setminsetsize); 264 ATF_TP_ADD_TC(tp, test_setmaxsetsize); 265 266 ATF_TP_ADD_TC(tp, test_getminsetsize); 267 ATF_TP_ADD_TC(tp, test_getsetsize); 268 269 ATF_TP_ADD_TC(tp, test_holes); 270 271 return (atf_no_error()); 272 } 273