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

selftests/mm: mlock-random-test: conform test to TAP format output

Conform the layout, informational and status messages to TAP. No
functional change is intended other than the layout of output messages.

Link: https://lkml.kernel.org/r/20240202113119.2047740-5-usama.anjum@collabora.com
Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Muhammad Usama Anjum and committed by
Andrew Morton
244ae271 7ef98513

+54 -82
+54 -82
tools/testing/selftests/mm/mlock-random-test.c
··· 13 13 #include <sys/ipc.h> 14 14 #include <sys/shm.h> 15 15 #include <time.h> 16 + #include "../kselftest.h" 16 17 #include "mlock2.h" 17 18 18 19 #define CHUNK_UNIT (128 * 1024) ··· 32 31 new.rlim_cur = max; 33 32 new.rlim_max = max; 34 33 if (setrlimit(RLIMIT_MEMLOCK, &new)) { 35 - perror("setrlimit() returns error\n"); 34 + ksft_perror("setrlimit() returns error\n"); 36 35 return -1; 37 36 } 38 37 39 38 /* drop capabilities including CAP_IPC_LOCK */ 40 39 if (cap_set_proc(cap)) { 41 - perror("cap_set_proc() returns error\n"); 42 - return -2; 40 + ksft_perror("cap_set_proc() returns error\n"); 41 + return -1; 43 42 } 44 43 45 44 return 0; ··· 53 52 unsigned long lock_size = 0; 54 53 55 54 f = fopen("/proc/self/status", "r"); 56 - if (!f) { 57 - perror("fopen"); 58 - return -1; 59 - } 55 + if (!f) 56 + ksft_exit_fail_msg("fopen: %s\n", strerror(errno)); 60 57 61 58 while (fgets(line, 1024, f)) { 62 59 if (strstr(line, "VmLck")) { 63 60 ret = sscanf(line, "VmLck:\t%8lu kB", &lock_size); 64 61 if (ret <= 0) { 65 - printf("sscanf() on VmLck error: %s: %d\n", 66 - line, ret); 67 62 fclose(f); 68 - return -1; 63 + ksft_exit_fail_msg("sscanf() on VmLck error: %s: %d\n", 64 + line, ret); 69 65 } 70 66 fclose(f); 71 67 return (int)(lock_size << 10); 72 68 } 73 69 } 74 70 75 - perror("cannot parse VmLck in /proc/self/status\n"); 76 71 fclose(f); 72 + ksft_exit_fail_msg("cannot parse VmLck in /proc/self/status: %s\n", strerror(errno)); 77 73 return -1; 78 74 } 79 75 ··· 89 91 size_t size; 90 92 91 93 smaps = seek_to_smaps_entry(addr); 92 - if (!smaps) { 93 - printf("Unable to parse /proc/self/smaps\n"); 94 - return 0; 95 - } 94 + if (!smaps) 95 + ksft_exit_fail_msg("Unable to parse /proc/self/smaps\n"); 96 96 97 97 while (getline(&line, &size, smaps) > 0) { 98 98 if (!strstr(line, "MMUPageSize")) { ··· 101 105 } 102 106 103 107 /* found the MMUPageSize of this section */ 104 - if (sscanf(line, "MMUPageSize: %8lu kB", 105 - &mmupage_size) < 1) { 106 - printf("Unable to parse smaps entry for Size:%s\n", 107 - line); 108 - break; 109 - } 108 + if (sscanf(line, "MMUPageSize: %8lu kB", &mmupage_size) < 1) 109 + ksft_exit_fail_msg("Unable to parse smaps entry for Size:%s\n", 110 + line); 110 111 111 112 } 112 113 free(line); ··· 129 136 * return value: 0 - success 130 137 * else: failure 131 138 */ 132 - int test_mlock_within_limit(char *p, int alloc_size) 139 + static void test_mlock_within_limit(char *p, int alloc_size) 133 140 { 134 141 int i; 135 142 int ret = 0; ··· 138 145 int page_size = 0; 139 146 140 147 getrlimit(RLIMIT_MEMLOCK, &cur); 141 - if (cur.rlim_cur < alloc_size) { 142 - printf("alloc_size[%d] < %u rlimit,lead to mlock failure\n", 143 - alloc_size, (unsigned int)cur.rlim_cur); 144 - return -1; 145 - } 148 + if (cur.rlim_cur < alloc_size) 149 + ksft_exit_fail_msg("alloc_size[%d] < %u rlimit,lead to mlock failure\n", 150 + alloc_size, (unsigned int)cur.rlim_cur); 146 151 147 152 srand(time(NULL)); 148 153 for (i = 0; i < TEST_LOOP; i++) { ··· 160 169 ret = mlock2_(p + start_offset, lock_size, 161 170 MLOCK_ONFAULT); 162 171 163 - if (ret) { 164 - printf("%s() failure at |%p(%d)| mlock:|%p(%d)|\n", 165 - is_mlock ? "mlock" : "mlock2", 166 - p, alloc_size, 167 - p + start_offset, lock_size); 168 - return ret; 169 - } 172 + if (ret) 173 + ksft_exit_fail_msg("%s() failure at |%p(%d)| mlock:|%p(%d)|\n", 174 + is_mlock ? "mlock" : "mlock2", 175 + p, alloc_size, 176 + p + start_offset, lock_size); 170 177 } 171 178 172 179 /* ··· 172 183 */ 173 184 locked_vm_size = get_proc_locked_vm_size(); 174 185 page_size = get_proc_page_size((unsigned long)p); 175 - if (page_size == 0) { 176 - printf("cannot get proc MMUPageSize\n"); 177 - return -1; 178 - } 179 186 180 - if (locked_vm_size > PAGE_ALIGN(alloc_size, page_size) + page_size) { 181 - printf("test_mlock_within_limit() left VmLck:%d on %d chunk\n", 182 - locked_vm_size, alloc_size); 183 - return -1; 184 - } 187 + if (locked_vm_size > PAGE_ALIGN(alloc_size, page_size) + page_size) 188 + ksft_exit_fail_msg("%s left VmLck:%d on %d chunk\n", 189 + __func__, locked_vm_size, alloc_size); 185 190 186 - return 0; 191 + ksft_test_result_pass("%s\n", __func__); 187 192 } 188 193 189 194 ··· 196 213 * return value: 0 - success 197 214 * else: failure 198 215 */ 199 - int test_mlock_outof_limit(char *p, int alloc_size) 216 + static void test_mlock_outof_limit(char *p, int alloc_size) 200 217 { 201 218 int i; 202 219 int ret = 0; ··· 204 221 struct rlimit cur; 205 222 206 223 getrlimit(RLIMIT_MEMLOCK, &cur); 207 - if (cur.rlim_cur >= alloc_size) { 208 - printf("alloc_size[%d] >%u rlimit, violates test condition\n", 209 - alloc_size, (unsigned int)cur.rlim_cur); 210 - return -1; 211 - } 224 + if (cur.rlim_cur >= alloc_size) 225 + ksft_exit_fail_msg("alloc_size[%d] >%u rlimit, violates test condition\n", 226 + alloc_size, (unsigned int)cur.rlim_cur); 212 227 213 228 old_locked_vm_size = get_proc_locked_vm_size(); 214 229 srand(time(NULL)); ··· 221 240 else 222 241 ret = mlock2_(p + start_offset, lock_size, 223 242 MLOCK_ONFAULT); 224 - if (ret == 0) { 225 - printf("%s() succeeds? on %p(%d) mlock%p(%d)\n", 226 - is_mlock ? "mlock" : "mlock2", 227 - p, alloc_size, 228 - p + start_offset, lock_size); 229 - return -1; 230 - } 243 + if (ret == 0) 244 + ksft_exit_fail_msg("%s() succeeds? on %p(%d) mlock%p(%d)\n", 245 + is_mlock ? "mlock" : "mlock2", 246 + p, alloc_size, p + start_offset, lock_size); 231 247 } 232 248 233 249 locked_vm_size = get_proc_locked_vm_size(); 234 - if (locked_vm_size != old_locked_vm_size) { 235 - printf("tests leads to new mlocked page: old[%d], new[%d]\n", 236 - old_locked_vm_size, 237 - locked_vm_size); 238 - return -1; 239 - } 250 + if (locked_vm_size != old_locked_vm_size) 251 + ksft_exit_fail_msg("tests leads to new mlocked page: old[%d], new[%d]\n", 252 + old_locked_vm_size, 253 + locked_vm_size); 240 254 241 - return 0; 255 + ksft_test_result_pass("%s\n", __func__); 242 256 } 243 257 244 258 int main(int argc, char **argv) 245 259 { 246 260 char *p = NULL; 247 - int ret = 0; 261 + 262 + ksft_print_header(); 248 263 249 264 if (set_cap_limits(MLOCK_RLIMIT_SIZE)) 250 - return -1; 265 + ksft_finished(); 266 + 267 + ksft_set_plan(2); 251 268 252 269 p = malloc(MLOCK_WITHIN_LIMIT_SIZE); 253 - if (p == NULL) { 254 - perror("malloc() failure\n"); 255 - return -1; 256 - } 257 - ret = test_mlock_within_limit(p, MLOCK_WITHIN_LIMIT_SIZE); 258 - if (ret) 259 - return ret; 270 + if (p == NULL) 271 + ksft_exit_fail_msg("malloc() failure: %s\n", strerror(errno)); 272 + 273 + test_mlock_within_limit(p, MLOCK_WITHIN_LIMIT_SIZE); 260 274 munlock(p, MLOCK_WITHIN_LIMIT_SIZE); 261 275 free(p); 262 276 263 - 264 277 p = malloc(MLOCK_OUTOF_LIMIT_SIZE); 265 - if (p == NULL) { 266 - perror("malloc() failure\n"); 267 - return -1; 268 - } 269 - ret = test_mlock_outof_limit(p, MLOCK_OUTOF_LIMIT_SIZE); 270 - if (ret) 271 - return ret; 278 + if (p == NULL) 279 + ksft_exit_fail_msg("malloc() failure: %s\n", strerror(errno)); 280 + 281 + test_mlock_outof_limit(p, MLOCK_OUTOF_LIMIT_SIZE); 272 282 munlock(p, MLOCK_OUTOF_LIMIT_SIZE); 273 283 free(p); 274 284 275 - return 0; 285 + ksft_finished(); 276 286 }