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

bpf: unify rlimit handling in selftests

Unify memlock handling into bpf_rlimit.h and replace all occurences
in BPF kselftests with it.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Daniel Borkmann and committed by
Alexei Starovoitov
fe8d662a 3808b519

+44 -54
+28
tools/testing/selftests/bpf/bpf_rlimit.h
··· 1 + #include <sys/resource.h> 2 + #include <stdio.h> 3 + 4 + static __attribute__((constructor)) void bpf_rlimit_ctor(void) 5 + { 6 + struct rlimit rlim_old, rlim_new = { 7 + .rlim_cur = RLIM_INFINITY, 8 + .rlim_max = RLIM_INFINITY, 9 + }; 10 + 11 + getrlimit(RLIMIT_MEMLOCK, &rlim_old); 12 + /* For the sake of running the test cases, we temporarily 13 + * set rlimit to infinity in order for kernel to focus on 14 + * errors from actual test cases and not getting noise 15 + * from hitting memlock limits. The limit is on per-process 16 + * basis and not a global one, hence destructor not really 17 + * needed here. 18 + */ 19 + if (setrlimit(RLIMIT_MEMLOCK, &rlim_new) < 0) { 20 + perror("Unable to lift memlock rlimit"); 21 + /* Trying out lower limit, but expect potential test 22 + * case failures from this! 23 + */ 24 + rlim_new.rlim_cur = rlim_old.rlim_cur + (1UL << 20); 25 + rlim_new.rlim_max = rlim_old.rlim_max + (1UL << 20); 26 + setrlimit(RLIMIT_MEMLOCK, &rlim_new); 27 + } 28 + }
+1 -5
tools/testing/selftests/bpf/test_align.c
··· 9 9 #include <stddef.h> 10 10 #include <stdbool.h> 11 11 12 - #include <sys/resource.h> 13 - 14 12 #include <linux/unistd.h> 15 13 #include <linux/filter.h> 16 14 #include <linux/bpf_perf_event.h> ··· 17 19 #include <bpf/bpf.h> 18 20 19 21 #include "../../../include/linux/filter.h" 22 + #include "bpf_rlimit.h" 20 23 21 24 #ifndef ARRAY_SIZE 22 25 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) ··· 701 702 int main(int argc, char **argv) 702 703 { 703 704 unsigned int from = 0, to = ARRAY_SIZE(tests); 704 - struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY }; 705 - 706 - setrlimit(RLIMIT_MEMLOCK, &rinf); 707 705 708 706 if (argc == 3) { 709 707 unsigned int l = atoi(argv[argc - 2]);
+1 -5
tools/testing/selftests/bpf/test_dev_cgroup.c
··· 11 11 #include <errno.h> 12 12 #include <assert.h> 13 13 #include <sys/time.h> 14 - #include <sys/resource.h> 15 14 16 15 #include <linux/bpf.h> 17 16 #include <bpf/bpf.h> 18 17 #include <bpf/libbpf.h> 19 18 20 19 #include "cgroup_helpers.h" 20 + #include "bpf_rlimit.h" 21 21 22 22 #define DEV_CGROUP_PROG "./dev_cgroup.o" 23 23 ··· 25 25 26 26 int main(int argc, char **argv) 27 27 { 28 - struct rlimit limit = { RLIM_INFINITY, RLIM_INFINITY }; 29 28 struct bpf_object *obj; 30 29 int error = EXIT_FAILURE; 31 30 int prog_fd, cgroup_fd; 32 31 __u32 prog_cnt; 33 - 34 - if (setrlimit(RLIMIT_MEMLOCK, &limit) < 0) 35 - perror("Unable to lift memlock rlimit"); 36 32 37 33 if (bpf_prog_load(DEV_CGROUP_PROG, BPF_PROG_TYPE_CGROUP_DEVICE, 38 34 &obj, &prog_fd)) {
+3 -11
tools/testing/selftests/bpf/test_lpm_map.c
··· 22 22 #include <unistd.h> 23 23 #include <arpa/inet.h> 24 24 #include <sys/time.h> 25 - #include <sys/resource.h> 26 25 27 26 #include <bpf/bpf.h> 27 + 28 28 #include "bpf_util.h" 29 + #include "bpf_rlimit.h" 29 30 30 31 struct tlpm_node { 31 32 struct tlpm_node *next; ··· 737 736 738 737 int main(void) 739 738 { 740 - struct rlimit limit = { RLIM_INFINITY, RLIM_INFINITY }; 741 - int i, ret; 739 + int i; 742 740 743 741 /* we want predictable, pseudo random tests */ 744 742 srand(0xf00ba1); 745 - 746 - /* allow unlimited locked memory */ 747 - ret = setrlimit(RLIMIT_MEMLOCK, &limit); 748 - if (ret < 0) 749 - perror("Unable to lift memlock rlimit"); 750 743 751 744 test_lpm_basic(); 752 745 test_lpm_order(); ··· 750 755 test_lpm_map(i); 751 756 752 757 test_lpm_ipaddr(); 753 - 754 758 test_lpm_delete(); 755 - 756 759 test_lpm_get_next_key(); 757 - 758 760 test_lpm_multi_thread(); 759 761 760 762 printf("test_lpm: OK\n");
+2 -4
tools/testing/selftests/bpf/test_lru_map.c
··· 16 16 #include <time.h> 17 17 18 18 #include <sys/wait.h> 19 - #include <sys/resource.h> 20 19 21 20 #include <bpf/bpf.h> 21 + 22 22 #include "bpf_util.h" 23 + #include "bpf_rlimit.h" 23 24 24 25 #define LOCAL_FREE_TARGET (128) 25 26 #define PERCPU_FREE_TARGET (4) ··· 614 613 615 614 int main(int argc, char **argv) 616 615 { 617 - struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; 618 616 int map_types[] = {BPF_MAP_TYPE_LRU_HASH, 619 617 BPF_MAP_TYPE_LRU_PERCPU_HASH}; 620 618 int map_flags[] = {0, BPF_F_NO_COMMON_LRU}; 621 619 int t, f; 622 620 623 621 setbuf(stdout, NULL); 624 - 625 - assert(!setrlimit(RLIMIT_MEMLOCK, &r)); 626 622 627 623 nr_cpus = bpf_num_possible_cpus(); 628 624 assert(nr_cpus != -1);
+2 -5
tools/testing/selftests/bpf/test_maps.c
··· 17 17 #include <stdlib.h> 18 18 19 19 #include <sys/wait.h> 20 - #include <sys/resource.h> 21 20 22 21 #include <linux/bpf.h> 23 22 24 23 #include <bpf/bpf.h> 25 24 #include <bpf/libbpf.h> 25 + 26 26 #include "bpf_util.h" 27 + #include "bpf_rlimit.h" 27 28 28 29 static int map_flags; 29 30 ··· 1127 1126 1128 1127 int main(void) 1129 1128 { 1130 - struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY }; 1131 - 1132 - setrlimit(RLIMIT_MEMLOCK, &rinf); 1133 - 1134 1129 map_flags = 0; 1135 1130 run_all_tests(); 1136 1131
+2 -5
tools/testing/selftests/bpf/test_progs.c
··· 26 26 27 27 #include <sys/ioctl.h> 28 28 #include <sys/wait.h> 29 - #include <sys/resource.h> 30 29 #include <sys/types.h> 31 30 #include <fcntl.h> 32 31 ··· 33 34 #include <linux/err.h> 34 35 #include <bpf/bpf.h> 35 36 #include <bpf/libbpf.h> 37 + 36 38 #include "test_iptunnel_common.h" 37 39 #include "bpf_util.h" 38 40 #include "bpf_endian.h" 41 + #include "bpf_rlimit.h" 39 42 40 43 static int error_cnt, pass_cnt; 41 44 ··· 966 965 967 966 int main(void) 968 967 { 969 - struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY }; 970 - 971 - setrlimit(RLIMIT_MEMLOCK, &rinf); 972 - 973 968 test_pkt_access(); 974 969 test_xdp(); 975 970 test_l4lb_all();
+1 -3
tools/testing/selftests/bpf/test_tag.c
··· 12 12 #include <assert.h> 13 13 14 14 #include <sys/socket.h> 15 - #include <sys/resource.h> 16 15 17 16 #include <linux/filter.h> 18 17 #include <linux/bpf.h> ··· 20 21 #include <bpf/bpf.h> 21 22 22 23 #include "../../../include/linux/filter.h" 24 + #include "bpf_rlimit.h" 23 25 24 26 static struct bpf_insn prog[BPF_MAXINSNS]; 25 27 ··· 184 184 185 185 int main(void) 186 186 { 187 - struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY }; 188 187 uint32_t tests = 0; 189 188 int i, fd_map; 190 189 191 - setrlimit(RLIMIT_MEMLOCK, &rinf); 192 190 fd_map = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(int), 193 191 sizeof(int), 1, BPF_F_NO_PREALLOC); 194 192 assert(fd_map > 0);
+1 -5
tools/testing/selftests/bpf/test_tcpbpf_user.c
··· 12 12 #include <linux/bpf.h> 13 13 #include <sys/ioctl.h> 14 14 #include <sys/time.h> 15 - #include <sys/resource.h> 16 15 #include <sys/types.h> 17 16 #include <sys/stat.h> 18 17 #include <fcntl.h> 19 18 #include <bpf/bpf.h> 20 19 #include <bpf/libbpf.h> 21 20 #include "bpf_util.h" 21 + #include "bpf_rlimit.h" 22 22 #include <linux/perf_event.h> 23 23 #include "test_tcpbpf.h" 24 24 ··· 44 44 45 45 int main(int argc, char **argv) 46 46 { 47 - struct rlimit limit = { RLIM_INFINITY, RLIM_INFINITY }; 48 47 const char *file = "test_tcpbpf_kern.o"; 49 48 struct tcpbpf_globals g = {0}; 50 49 int cg_fd, prog_fd, map_fd; ··· 55 56 __u32 key = 0; 56 57 int pid; 57 58 int rv; 58 - 59 - if (setrlimit(RLIMIT_MEMLOCK, &limit) < 0) 60 - perror("Unable to lift memlock rlimit"); 61 59 62 60 if (argc > 1 && strcmp(argv[1], "-d") == 0) 63 61 debug_flag = true;
+1 -5
tools/testing/selftests/bpf/test_verifier.c
··· 24 24 #include <limits.h> 25 25 26 26 #include <sys/capability.h> 27 - #include <sys/resource.h> 28 27 29 28 #include <linux/unistd.h> 30 29 #include <linux/filter.h> ··· 40 41 # define CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1 41 42 # endif 42 43 #endif 43 - 44 + #include "bpf_rlimit.h" 44 45 #include "../../../include/linux/filter.h" 45 46 46 47 #ifndef ARRAY_SIZE ··· 11542 11543 11543 11544 int main(int argc, char **argv) 11544 11545 { 11545 - struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY }; 11546 - struct rlimit rlim = { 1 << 20, 1 << 20 }; 11547 11546 unsigned int from = 0, to = ARRAY_SIZE(tests); 11548 11547 bool unpriv = !is_admin(); 11549 11548 ··· 11569 11572 return EXIT_FAILURE; 11570 11573 } 11571 11574 11572 - setrlimit(RLIMIT_MEMLOCK, unpriv ? &rlim : &rinf); 11573 11575 return do_test(unpriv, from, to); 11574 11576 }
+2 -6
tools/testing/selftests/bpf/test_verifier_log.c
··· 4 4 #include <string.h> 5 5 #include <unistd.h> 6 6 #include <sys/time.h> 7 - #include <sys/resource.h> 8 7 9 8 #include <linux/bpf.h> 10 9 #include <linux/filter.h> 11 10 #include <linux/unistd.h> 12 11 13 12 #include <bpf/bpf.h> 13 + 14 + #include "bpf_rlimit.h" 14 15 15 16 #define LOG_SIZE (1 << 20) 16 17 ··· 134 133 135 134 int main(int argc, char **argv) 136 135 { 137 - struct rlimit limit = { RLIM_INFINITY, RLIM_INFINITY }; 138 136 char full_log[LOG_SIZE]; 139 137 char log[LOG_SIZE]; 140 138 size_t want_len; 141 139 int i; 142 - 143 - /* allow unlimited locked memory to have more consistent error code */ 144 - if (setrlimit(RLIMIT_MEMLOCK, &limit) < 0) 145 - perror("Unable to lift memlock rlimit"); 146 140 147 141 memset(log, 1, LOG_SIZE); 148 142