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

selftests/powerpc: Refactor the AUXV routines

Refactor the AUXV routines so they are more composable. In a future test
we want to look for many AUXV entries and we don't want to have to read
/proc/self/auxv each time.

Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>

+41 -18
+5 -1
tools/testing/selftests/powerpc/include/utils.h
··· 24 24 25 25 void test_harness_set_timeout(uint64_t time); 26 26 int test_harness(int (test_function)(void), char *name); 27 - extern void *get_auxv_entry(int type); 27 + 28 + int read_auxv(char *buf, ssize_t buf_size); 29 + void *find_auxv_entry(int type, char *auxv); 30 + void *get_auxv_entry(int type); 31 + 28 32 int pick_online_cpu(void); 29 33 30 34 static inline bool have_hwcap(unsigned long ftr)
+36 -17
tools/testing/selftests/powerpc/utils.c
··· 19 19 20 20 static char auxv[4096]; 21 21 22 - void *get_auxv_entry(int type) 22 + int read_auxv(char *buf, ssize_t buf_size) 23 23 { 24 - ElfW(auxv_t) *p; 25 - void *result; 26 24 ssize_t num; 27 - int fd; 25 + int rc, fd; 28 26 29 27 fd = open("/proc/self/auxv", O_RDONLY); 30 28 if (fd == -1) { 31 29 perror("open"); 32 - return NULL; 30 + return -errno; 33 31 } 34 32 35 - result = NULL; 36 - 37 - num = read(fd, auxv, sizeof(auxv)); 33 + num = read(fd, buf, buf_size); 38 34 if (num < 0) { 39 35 perror("read"); 36 + rc = -EIO; 40 37 goto out; 41 38 } 42 39 43 - if (num > sizeof(auxv)) { 44 - printf("Overflowed auxv buffer\n"); 40 + if (num > buf_size) { 41 + printf("overflowed auxv buffer\n"); 42 + rc = -EOVERFLOW; 45 43 goto out; 46 44 } 45 + 46 + rc = 0; 47 + out: 48 + close(fd); 49 + return rc; 50 + } 51 + 52 + void *find_auxv_entry(int type, char *auxv) 53 + { 54 + ElfW(auxv_t) *p; 47 55 48 56 p = (ElfW(auxv_t) *)auxv; 49 57 50 58 while (p->a_type != AT_NULL) { 51 - if (p->a_type == type) { 52 - result = (void *)p->a_un.a_val; 53 - break; 54 - } 59 + if (p->a_type == type) 60 + return p; 55 61 56 62 p++; 57 63 } 58 - out: 59 - close(fd); 60 - return result; 64 + 65 + return NULL; 66 + } 67 + 68 + void *get_auxv_entry(int type) 69 + { 70 + ElfW(auxv_t) *p; 71 + 72 + if (read_auxv(auxv, sizeof(auxv))) 73 + return NULL; 74 + 75 + p = find_auxv_entry(type, auxv); 76 + if (p) 77 + return (void *)p->a_un.a_val; 78 + 79 + return NULL; 61 80 } 62 81 63 82 int pick_online_cpu(void)