aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexei Starovoitov <ast@kernel.org>2018-02-26 23:11:23 -0500
committerAlexei Starovoitov <ast@kernel.org>2018-02-26 23:11:24 -0500
commit6f1b5a2b58d8470e5a8b25ab29f5fdb4616ffff8 (patch)
treec29881120083e22acbc01c27966d735a32af8a40
parent3808b51911fe1a2bf8d6f4f2836d4c51aa29a6fd (diff)
parentb33eb735836224e55cd8182aff9bbb7ddc17f38b (diff)
Merge branch 'bpf-kselftest-improvements'
Daniel Borkmann says: ==================== First one unifies the often repeated rlimit handling and the second one enables and adds run-time tests for BPF tail calls which provides useful coverage in particular for JITs. ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
-rw-r--r--tools/testing/selftests/bpf/bpf_rlimit.h28
-rw-r--r--tools/testing/selftests/bpf/test_align.c6
-rw-r--r--tools/testing/selftests/bpf/test_dev_cgroup.c6
-rw-r--r--tools/testing/selftests/bpf/test_lpm_map.c14
-rw-r--r--tools/testing/selftests/bpf/test_lru_map.c6
-rw-r--r--tools/testing/selftests/bpf/test_maps.c7
-rw-r--r--tools/testing/selftests/bpf/test_progs.c7
-rw-r--r--tools/testing/selftests/bpf/test_tag.c4
-rw-r--r--tools/testing/selftests/bpf/test_tcpbpf_user.c6
-rw-r--r--tools/testing/selftests/bpf/test_verifier.c123
-rw-r--r--tools/testing/selftests/bpf/test_verifier_log.c8
11 files changed, 154 insertions, 61 deletions
diff --git a/tools/testing/selftests/bpf/bpf_rlimit.h b/tools/testing/selftests/bpf/bpf_rlimit.h
new file mode 100644
index 000000000000..9dac9b30f8ef
--- /dev/null
+++ b/tools/testing/selftests/bpf/bpf_rlimit.h
@@ -0,0 +1,28 @@
1#include <sys/resource.h>
2#include <stdio.h>
3
4static __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}
diff --git a/tools/testing/selftests/bpf/test_align.c b/tools/testing/selftests/bpf/test_align.c
index ff8bd7e3e50c..6b1b302310fe 100644
--- a/tools/testing/selftests/bpf/test_align.c
+++ b/tools/testing/selftests/bpf/test_align.c
@@ -9,8 +9,6 @@
9#include <stddef.h> 9#include <stddef.h>
10#include <stdbool.h> 10#include <stdbool.h>
11 11
12#include <sys/resource.h>
13
14#include <linux/unistd.h> 12#include <linux/unistd.h>
15#include <linux/filter.h> 13#include <linux/filter.h>
16#include <linux/bpf_perf_event.h> 14#include <linux/bpf_perf_event.h>
@@ -19,6 +17,7 @@
19#include <bpf/bpf.h> 17#include <bpf/bpf.h>
20 18
21#include "../../../include/linux/filter.h" 19#include "../../../include/linux/filter.h"
20#include "bpf_rlimit.h"
22 21
23#ifndef ARRAY_SIZE 22#ifndef ARRAY_SIZE
24# define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 23# define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
@@ -702,9 +701,6 @@ static int do_test(unsigned int from, unsigned int to)
702int main(int argc, char **argv) 701int main(int argc, char **argv)
703{ 702{
704 unsigned int from = 0, to = ARRAY_SIZE(tests); 703 unsigned int from = 0, to = ARRAY_SIZE(tests);
705 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
706
707 setrlimit(RLIMIT_MEMLOCK, &rinf);
708 704
709 if (argc == 3) { 705 if (argc == 3) {
710 unsigned int l = atoi(argv[argc - 2]); 706 unsigned int l = atoi(argv[argc - 2]);
diff --git a/tools/testing/selftests/bpf/test_dev_cgroup.c b/tools/testing/selftests/bpf/test_dev_cgroup.c
index 3489cc283433..9c8b50bac7e0 100644
--- a/tools/testing/selftests/bpf/test_dev_cgroup.c
+++ b/tools/testing/selftests/bpf/test_dev_cgroup.c
@@ -11,13 +11,13 @@
11#include <errno.h> 11#include <errno.h>
12#include <assert.h> 12#include <assert.h>
13#include <sys/time.h> 13#include <sys/time.h>
14#include <sys/resource.h>
15 14
16#include <linux/bpf.h> 15#include <linux/bpf.h>
17#include <bpf/bpf.h> 16#include <bpf/bpf.h>
18#include <bpf/libbpf.h> 17#include <bpf/libbpf.h>
19 18
20#include "cgroup_helpers.h" 19#include "cgroup_helpers.h"
20#include "bpf_rlimit.h"
21 21
22#define DEV_CGROUP_PROG "./dev_cgroup.o" 22#define DEV_CGROUP_PROG "./dev_cgroup.o"
23 23
@@ -25,15 +25,11 @@
25 25
26int main(int argc, char **argv) 26int main(int argc, char **argv)
27{ 27{
28 struct rlimit limit = { RLIM_INFINITY, RLIM_INFINITY };
29 struct bpf_object *obj; 28 struct bpf_object *obj;
30 int error = EXIT_FAILURE; 29 int error = EXIT_FAILURE;
31 int prog_fd, cgroup_fd; 30 int prog_fd, cgroup_fd;
32 __u32 prog_cnt; 31 __u32 prog_cnt;
33 32
34 if (setrlimit(RLIMIT_MEMLOCK, &limit) < 0)
35 perror("Unable to lift memlock rlimit");
36
37 if (bpf_prog_load(DEV_CGROUP_PROG, BPF_PROG_TYPE_CGROUP_DEVICE, 33 if (bpf_prog_load(DEV_CGROUP_PROG, BPF_PROG_TYPE_CGROUP_DEVICE,
38 &obj, &prog_fd)) { 34 &obj, &prog_fd)) {
39 printf("Failed to load DEV_CGROUP program\n"); 35 printf("Failed to load DEV_CGROUP program\n");
diff --git a/tools/testing/selftests/bpf/test_lpm_map.c b/tools/testing/selftests/bpf/test_lpm_map.c
index 2be87e9ee28d..147e34cfceb7 100644
--- a/tools/testing/selftests/bpf/test_lpm_map.c
+++ b/tools/testing/selftests/bpf/test_lpm_map.c
@@ -22,10 +22,11 @@
22#include <unistd.h> 22#include <unistd.h>
23#include <arpa/inet.h> 23#include <arpa/inet.h>
24#include <sys/time.h> 24#include <sys/time.h>
25#include <sys/resource.h>
26 25
27#include <bpf/bpf.h> 26#include <bpf/bpf.h>
27
28#include "bpf_util.h" 28#include "bpf_util.h"
29#include "bpf_rlimit.h"
29 30
30struct tlpm_node { 31struct tlpm_node {
31 struct tlpm_node *next; 32 struct tlpm_node *next;
@@ -736,17 +737,11 @@ static void test_lpm_multi_thread(void)
736 737
737int main(void) 738int main(void)
738{ 739{
739 struct rlimit limit = { RLIM_INFINITY, RLIM_INFINITY }; 740 int i;
740 int i, ret;
741 741
742 /* we want predictable, pseudo random tests */ 742 /* we want predictable, pseudo random tests */
743 srand(0xf00ba1); 743 srand(0xf00ba1);
744 744
745 /* allow unlimited locked memory */
746 ret = setrlimit(RLIMIT_MEMLOCK, &limit);
747 if (ret < 0)
748 perror("Unable to lift memlock rlimit");
749
750 test_lpm_basic(); 745 test_lpm_basic();
751 test_lpm_order(); 746 test_lpm_order();
752 747
@@ -755,11 +750,8 @@ int main(void)
755 test_lpm_map(i); 750 test_lpm_map(i);
756 751
757 test_lpm_ipaddr(); 752 test_lpm_ipaddr();
758
759 test_lpm_delete(); 753 test_lpm_delete();
760
761 test_lpm_get_next_key(); 754 test_lpm_get_next_key();
762
763 test_lpm_multi_thread(); 755 test_lpm_multi_thread();
764 756
765 printf("test_lpm: OK\n"); 757 printf("test_lpm: OK\n");
diff --git a/tools/testing/selftests/bpf/test_lru_map.c b/tools/testing/selftests/bpf/test_lru_map.c
index 8c10c9180c1a..781c7de343be 100644
--- a/tools/testing/selftests/bpf/test_lru_map.c
+++ b/tools/testing/selftests/bpf/test_lru_map.c
@@ -16,10 +16,11 @@
16#include <time.h> 16#include <time.h>
17 17
18#include <sys/wait.h> 18#include <sys/wait.h>
19#include <sys/resource.h>
20 19
21#include <bpf/bpf.h> 20#include <bpf/bpf.h>
21
22#include "bpf_util.h" 22#include "bpf_util.h"
23#include "bpf_rlimit.h"
23 24
24#define LOCAL_FREE_TARGET (128) 25#define LOCAL_FREE_TARGET (128)
25#define PERCPU_FREE_TARGET (4) 26#define PERCPU_FREE_TARGET (4)
@@ -613,7 +614,6 @@ static void test_lru_sanity6(int map_type, int map_flags, int tgt_free)
613 614
614int main(int argc, char **argv) 615int main(int argc, char **argv)
615{ 616{
616 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
617 int map_types[] = {BPF_MAP_TYPE_LRU_HASH, 617 int map_types[] = {BPF_MAP_TYPE_LRU_HASH,
618 BPF_MAP_TYPE_LRU_PERCPU_HASH}; 618 BPF_MAP_TYPE_LRU_PERCPU_HASH};
619 int map_flags[] = {0, BPF_F_NO_COMMON_LRU}; 619 int map_flags[] = {0, BPF_F_NO_COMMON_LRU};
@@ -621,8 +621,6 @@ int main(int argc, char **argv)
621 621
622 setbuf(stdout, NULL); 622 setbuf(stdout, NULL);
623 623
624 assert(!setrlimit(RLIMIT_MEMLOCK, &r));
625
626 nr_cpus = bpf_num_possible_cpus(); 624 nr_cpus = bpf_num_possible_cpus();
627 assert(nr_cpus != -1); 625 assert(nr_cpus != -1);
628 printf("nr_cpus:%d\n\n", nr_cpus); 626 printf("nr_cpus:%d\n\n", nr_cpus);
diff --git a/tools/testing/selftests/bpf/test_maps.c b/tools/testing/selftests/bpf/test_maps.c
index 9e03a4c356a4..1238733c5b33 100644
--- a/tools/testing/selftests/bpf/test_maps.c
+++ b/tools/testing/selftests/bpf/test_maps.c
@@ -17,13 +17,14 @@
17#include <stdlib.h> 17#include <stdlib.h>
18 18
19#include <sys/wait.h> 19#include <sys/wait.h>
20#include <sys/resource.h>
21 20
22#include <linux/bpf.h> 21#include <linux/bpf.h>
23 22
24#include <bpf/bpf.h> 23#include <bpf/bpf.h>
25#include <bpf/libbpf.h> 24#include <bpf/libbpf.h>
25
26#include "bpf_util.h" 26#include "bpf_util.h"
27#include "bpf_rlimit.h"
27 28
28static int map_flags; 29static int map_flags;
29 30
@@ -1126,10 +1127,6 @@ static void run_all_tests(void)
1126 1127
1127int main(void) 1128int main(void)
1128{ 1129{
1129 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
1130
1131 setrlimit(RLIMIT_MEMLOCK, &rinf);
1132
1133 map_flags = 0; 1130 map_flags = 0;
1134 run_all_tests(); 1131 run_all_tests();
1135 1132
diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
index b549308abd19..27ad5404389e 100644
--- a/tools/testing/selftests/bpf/test_progs.c
+++ b/tools/testing/selftests/bpf/test_progs.c
@@ -26,7 +26,6 @@ typedef __u16 __sum16;
26 26
27#include <sys/ioctl.h> 27#include <sys/ioctl.h>
28#include <sys/wait.h> 28#include <sys/wait.h>
29#include <sys/resource.h>
30#include <sys/types.h> 29#include <sys/types.h>
31#include <fcntl.h> 30#include <fcntl.h>
32 31
@@ -34,9 +33,11 @@ typedef __u16 __sum16;
34#include <linux/err.h> 33#include <linux/err.h>
35#include <bpf/bpf.h> 34#include <bpf/bpf.h>
36#include <bpf/libbpf.h> 35#include <bpf/libbpf.h>
36
37#include "test_iptunnel_common.h" 37#include "test_iptunnel_common.h"
38#include "bpf_util.h" 38#include "bpf_util.h"
39#include "bpf_endian.h" 39#include "bpf_endian.h"
40#include "bpf_rlimit.h"
40 41
41static int error_cnt, pass_cnt; 42static int error_cnt, pass_cnt;
42 43
@@ -965,10 +966,6 @@ out:
965 966
966int main(void) 967int main(void)
967{ 968{
968 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
969
970 setrlimit(RLIMIT_MEMLOCK, &rinf);
971
972 test_pkt_access(); 969 test_pkt_access();
973 test_xdp(); 970 test_xdp();
974 test_l4lb_all(); 971 test_l4lb_all();
diff --git a/tools/testing/selftests/bpf/test_tag.c b/tools/testing/selftests/bpf/test_tag.c
index 8b201895c569..6272c784ca2a 100644
--- a/tools/testing/selftests/bpf/test_tag.c
+++ b/tools/testing/selftests/bpf/test_tag.c
@@ -12,7 +12,6 @@
12#include <assert.h> 12#include <assert.h>
13 13
14#include <sys/socket.h> 14#include <sys/socket.h>
15#include <sys/resource.h>
16 15
17#include <linux/filter.h> 16#include <linux/filter.h>
18#include <linux/bpf.h> 17#include <linux/bpf.h>
@@ -21,6 +20,7 @@
21#include <bpf/bpf.h> 20#include <bpf/bpf.h>
22 21
23#include "../../../include/linux/filter.h" 22#include "../../../include/linux/filter.h"
23#include "bpf_rlimit.h"
24 24
25static struct bpf_insn prog[BPF_MAXINSNS]; 25static struct bpf_insn prog[BPF_MAXINSNS];
26 26
@@ -184,11 +184,9 @@ static void do_test(uint32_t *tests, int start_insns, int fd_map,
184 184
185int main(void) 185int main(void)
186{ 186{
187 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
188 uint32_t tests = 0; 187 uint32_t tests = 0;
189 int i, fd_map; 188 int i, fd_map;
190 189
191 setrlimit(RLIMIT_MEMLOCK, &rinf);
192 fd_map = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(int), 190 fd_map = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(int),
193 sizeof(int), 1, BPF_F_NO_PREALLOC); 191 sizeof(int), 1, BPF_F_NO_PREALLOC);
194 assert(fd_map > 0); 192 assert(fd_map > 0);
diff --git a/tools/testing/selftests/bpf/test_tcpbpf_user.c b/tools/testing/selftests/bpf/test_tcpbpf_user.c
index 5d73db416460..84ab5163c828 100644
--- a/tools/testing/selftests/bpf/test_tcpbpf_user.c
+++ b/tools/testing/selftests/bpf/test_tcpbpf_user.c
@@ -12,13 +12,13 @@
12#include <linux/bpf.h> 12#include <linux/bpf.h>
13#include <sys/ioctl.h> 13#include <sys/ioctl.h>
14#include <sys/time.h> 14#include <sys/time.h>
15#include <sys/resource.h>
16#include <sys/types.h> 15#include <sys/types.h>
17#include <sys/stat.h> 16#include <sys/stat.h>
18#include <fcntl.h> 17#include <fcntl.h>
19#include <bpf/bpf.h> 18#include <bpf/bpf.h>
20#include <bpf/libbpf.h> 19#include <bpf/libbpf.h>
21#include "bpf_util.h" 20#include "bpf_util.h"
21#include "bpf_rlimit.h"
22#include <linux/perf_event.h> 22#include <linux/perf_event.h>
23#include "test_tcpbpf.h" 23#include "test_tcpbpf.h"
24 24
@@ -44,7 +44,6 @@ static int bpf_find_map(const char *test, struct bpf_object *obj,
44 44
45int main(int argc, char **argv) 45int main(int argc, char **argv)
46{ 46{
47 struct rlimit limit = { RLIM_INFINITY, RLIM_INFINITY };
48 const char *file = "test_tcpbpf_kern.o"; 47 const char *file = "test_tcpbpf_kern.o";
49 struct tcpbpf_globals g = {0}; 48 struct tcpbpf_globals g = {0};
50 int cg_fd, prog_fd, map_fd; 49 int cg_fd, prog_fd, map_fd;
@@ -57,9 +56,6 @@ int main(int argc, char **argv)
57 int pid; 56 int pid;
58 int rv; 57 int rv;
59 58
60 if (setrlimit(RLIMIT_MEMLOCK, &limit) < 0)
61 perror("Unable to lift memlock rlimit");
62
63 if (argc > 1 && strcmp(argv[1], "-d") == 0) 59 if (argc > 1 && strcmp(argv[1], "-d") == 0)
64 debug_flag = true; 60 debug_flag = true;
65 61
diff --git a/tools/testing/selftests/bpf/test_verifier.c b/tools/testing/selftests/bpf/test_verifier.c
index 2164d218322e..9eb05f3135ac 100644
--- a/tools/testing/selftests/bpf/test_verifier.c
+++ b/tools/testing/selftests/bpf/test_verifier.c
@@ -24,7 +24,6 @@
24#include <limits.h> 24#include <limits.h>
25 25
26#include <sys/capability.h> 26#include <sys/capability.h>
27#include <sys/resource.h>
28 27
29#include <linux/unistd.h> 28#include <linux/unistd.h>
30#include <linux/filter.h> 29#include <linux/filter.h>
@@ -41,7 +40,7 @@
41# define CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1 40# define CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1
42# endif 41# endif
43#endif 42#endif
44 43#include "bpf_rlimit.h"
45#include "../../../include/linux/filter.h" 44#include "../../../include/linux/filter.h"
46 45
47#ifndef ARRAY_SIZE 46#ifndef ARRAY_SIZE
@@ -2590,17 +2589,74 @@ static struct bpf_test tests[] = {
2590 .result = ACCEPT, 2589 .result = ACCEPT,
2591 }, 2590 },
2592 { 2591 {
2592 "runtime/jit: tail_call within bounds, prog once",
2593 .insns = {
2594 BPF_MOV64_IMM(BPF_REG_3, 0),
2595 BPF_LD_MAP_FD(BPF_REG_2, 0),
2596 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
2597 BPF_FUNC_tail_call),
2598 BPF_MOV64_IMM(BPF_REG_0, 1),
2599 BPF_EXIT_INSN(),
2600 },
2601 .fixup_prog = { 1 },
2602 .result = ACCEPT,
2603 .retval = 42,
2604 },
2605 {
2606 "runtime/jit: tail_call within bounds, prog loop",
2607 .insns = {
2608 BPF_MOV64_IMM(BPF_REG_3, 1),
2609 BPF_LD_MAP_FD(BPF_REG_2, 0),
2610 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
2611 BPF_FUNC_tail_call),
2612 BPF_MOV64_IMM(BPF_REG_0, 1),
2613 BPF_EXIT_INSN(),
2614 },
2615 .fixup_prog = { 1 },
2616 .result = ACCEPT,
2617 .retval = 41,
2618 },
2619 {
2620 "runtime/jit: tail_call within bounds, no prog",
2621 .insns = {
2622 BPF_MOV64_IMM(BPF_REG_3, 2),
2623 BPF_LD_MAP_FD(BPF_REG_2, 0),
2624 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
2625 BPF_FUNC_tail_call),
2626 BPF_MOV64_IMM(BPF_REG_0, 1),
2627 BPF_EXIT_INSN(),
2628 },
2629 .fixup_prog = { 1 },
2630 .result = ACCEPT,
2631 .retval = 1,
2632 },
2633 {
2634 "runtime/jit: tail_call out of bounds",
2635 .insns = {
2636 BPF_MOV64_IMM(BPF_REG_3, 256),
2637 BPF_LD_MAP_FD(BPF_REG_2, 0),
2638 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
2639 BPF_FUNC_tail_call),
2640 BPF_MOV64_IMM(BPF_REG_0, 2),
2641 BPF_EXIT_INSN(),
2642 },
2643 .fixup_prog = { 1 },
2644 .result = ACCEPT,
2645 .retval = 2,
2646 },
2647 {
2593 "runtime/jit: pass negative index to tail_call", 2648 "runtime/jit: pass negative index to tail_call",
2594 .insns = { 2649 .insns = {
2595 BPF_MOV64_IMM(BPF_REG_3, -1), 2650 BPF_MOV64_IMM(BPF_REG_3, -1),
2596 BPF_LD_MAP_FD(BPF_REG_2, 0), 2651 BPF_LD_MAP_FD(BPF_REG_2, 0),
2597 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 2652 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
2598 BPF_FUNC_tail_call), 2653 BPF_FUNC_tail_call),
2599 BPF_MOV64_IMM(BPF_REG_0, 0), 2654 BPF_MOV64_IMM(BPF_REG_0, 2),
2600 BPF_EXIT_INSN(), 2655 BPF_EXIT_INSN(),
2601 }, 2656 },
2602 .fixup_prog = { 1 }, 2657 .fixup_prog = { 1 },
2603 .result = ACCEPT, 2658 .result = ACCEPT,
2659 .retval = 2,
2604 }, 2660 },
2605 { 2661 {
2606 "runtime/jit: pass > 32bit index to tail_call", 2662 "runtime/jit: pass > 32bit index to tail_call",
@@ -2609,11 +2665,12 @@ static struct bpf_test tests[] = {
2609 BPF_LD_MAP_FD(BPF_REG_2, 0), 2665 BPF_LD_MAP_FD(BPF_REG_2, 0),
2610 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 2666 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
2611 BPF_FUNC_tail_call), 2667 BPF_FUNC_tail_call),
2612 BPF_MOV64_IMM(BPF_REG_0, 0), 2668 BPF_MOV64_IMM(BPF_REG_0, 2),
2613 BPF_EXIT_INSN(), 2669 BPF_EXIT_INSN(),
2614 }, 2670 },
2615 .fixup_prog = { 2 }, 2671 .fixup_prog = { 2 },
2616 .result = ACCEPT, 2672 .result = ACCEPT,
2673 .retval = 42,
2617 }, 2674 },
2618 { 2675 {
2619 "stack pointer arithmetic", 2676 "stack pointer arithmetic",
@@ -11279,16 +11336,61 @@ static int create_map(uint32_t size_value, uint32_t max_elem)
11279 return fd; 11336 return fd;
11280} 11337}
11281 11338
11339static int create_prog_dummy1(void)
11340{
11341 struct bpf_insn prog[] = {
11342 BPF_MOV64_IMM(BPF_REG_0, 42),
11343 BPF_EXIT_INSN(),
11344 };
11345
11346 return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog,
11347 ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
11348}
11349
11350static int create_prog_dummy2(int mfd, int idx)
11351{
11352 struct bpf_insn prog[] = {
11353 BPF_MOV64_IMM(BPF_REG_3, idx),
11354 BPF_LD_MAP_FD(BPF_REG_2, mfd),
11355 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
11356 BPF_FUNC_tail_call),
11357 BPF_MOV64_IMM(BPF_REG_0, 41),
11358 BPF_EXIT_INSN(),
11359 };
11360
11361 return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog,
11362 ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
11363}
11364
11282static int create_prog_array(void) 11365static int create_prog_array(void)
11283{ 11366{
11284 int fd; 11367 int p1key = 0, p2key = 1;
11368 int mfd, p1fd, p2fd;
11285 11369
11286 fd = bpf_create_map(BPF_MAP_TYPE_PROG_ARRAY, sizeof(int), 11370 mfd = bpf_create_map(BPF_MAP_TYPE_PROG_ARRAY, sizeof(int),
11287 sizeof(int), 4, 0); 11371 sizeof(int), 4, 0);
11288 if (fd < 0) 11372 if (mfd < 0) {
11289 printf("Failed to create prog array '%s'!\n", strerror(errno)); 11373 printf("Failed to create prog array '%s'!\n", strerror(errno));
11374 return -1;
11375 }
11290 11376
11291 return fd; 11377 p1fd = create_prog_dummy1();
11378 p2fd = create_prog_dummy2(mfd, p2key);
11379 if (p1fd < 0 || p2fd < 0)
11380 goto out;
11381 if (bpf_map_update_elem(mfd, &p1key, &p1fd, BPF_ANY) < 0)
11382 goto out;
11383 if (bpf_map_update_elem(mfd, &p2key, &p2fd, BPF_ANY) < 0)
11384 goto out;
11385 close(p2fd);
11386 close(p1fd);
11387
11388 return mfd;
11389out:
11390 close(p2fd);
11391 close(p1fd);
11392 close(mfd);
11393 return -1;
11292} 11394}
11293 11395
11294static int create_map_in_map(void) 11396static int create_map_in_map(void)
@@ -11543,8 +11645,6 @@ static int do_test(bool unpriv, unsigned int from, unsigned int to)
11543 11645
11544int main(int argc, char **argv) 11646int main(int argc, char **argv)
11545{ 11647{
11546 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
11547 struct rlimit rlim = { 1 << 20, 1 << 20 };
11548 unsigned int from = 0, to = ARRAY_SIZE(tests); 11648 unsigned int from = 0, to = ARRAY_SIZE(tests);
11549 bool unpriv = !is_admin(); 11649 bool unpriv = !is_admin();
11550 11650
@@ -11572,6 +11672,5 @@ int main(int argc, char **argv)
11572 return EXIT_FAILURE; 11672 return EXIT_FAILURE;
11573 } 11673 }
11574 11674
11575 setrlimit(RLIMIT_MEMLOCK, unpriv ? &rlim : &rinf);
11576 return do_test(unpriv, from, to); 11675 return do_test(unpriv, from, to);
11577} 11676}
diff --git a/tools/testing/selftests/bpf/test_verifier_log.c b/tools/testing/selftests/bpf/test_verifier_log.c
index e9626cf5607a..8d6918c3b4a2 100644
--- a/tools/testing/selftests/bpf/test_verifier_log.c
+++ b/tools/testing/selftests/bpf/test_verifier_log.c
@@ -4,7 +4,6 @@
4#include <string.h> 4#include <string.h>
5#include <unistd.h> 5#include <unistd.h>
6#include <sys/time.h> 6#include <sys/time.h>
7#include <sys/resource.h>
8 7
9#include <linux/bpf.h> 8#include <linux/bpf.h>
10#include <linux/filter.h> 9#include <linux/filter.h>
@@ -12,6 +11,8 @@
12 11
13#include <bpf/bpf.h> 12#include <bpf/bpf.h>
14 13
14#include "bpf_rlimit.h"
15
15#define LOG_SIZE (1 << 20) 16#define LOG_SIZE (1 << 20)
16 17
17#define err(str...) printf("ERROR: " str) 18#define err(str...) printf("ERROR: " str)
@@ -133,16 +134,11 @@ static void test_log_bad(char *log, size_t log_len, int log_level)
133 134
134int main(int argc, char **argv) 135int main(int argc, char **argv)
135{ 136{
136 struct rlimit limit = { RLIM_INFINITY, RLIM_INFINITY };
137 char full_log[LOG_SIZE]; 137 char full_log[LOG_SIZE];
138 char log[LOG_SIZE]; 138 char log[LOG_SIZE];
139 size_t want_len; 139 size_t want_len;
140 int i; 140 int i;
141 141
142 /* allow unlimited locked memory to have more consistent error code */
143 if (setrlimit(RLIMIT_MEMLOCK, &limit) < 0)
144 perror("Unable to lift memlock rlimit");
145
146 memset(log, 1, LOG_SIZE); 142 memset(log, 1, LOG_SIZE);
147 143
148 /* Test incorrect attr */ 144 /* Test incorrect attr */