Skip to content

Commit b858ba8

Browse files
laoaranakryiko
authored andcommitted
selftests/bpf: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK
We have switched to memcg-based memory accouting and thus the rlimit is not needed any more. LIBBPF_STRICT_AUTO_RLIMIT_MEMLOCK was introduced in libbpf for backward compatibility, so we can use it instead now. After this change, the header tools/testing/selftests/bpf/bpf_rlimit.h can be removed. This patch also removes the useless header sys/resource.h from many files in tools/testing/selftests/bpf/. Signed-off-by: Yafang Shao <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent b25acda commit b858ba8

21 files changed

+45
-61
lines changed

tools/testing/selftests/bpf/bench.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <fcntl.h>
99
#include <pthread.h>
1010
#include <sys/sysinfo.h>
11-
#include <sys/resource.h>
1211
#include <signal.h>
1312
#include "bench.h"
1413
#include "testing_helpers.h"

tools/testing/selftests/bpf/bpf_rlimit.h

Lines changed: 0 additions & 28 deletions
This file was deleted.

tools/testing/selftests/bpf/flow_dissector_load.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <bpf/bpf.h>
1212
#include <bpf/libbpf.h>
1313

14-
#include "bpf_rlimit.h"
1514
#include "flow_dissector_load.h"
1615

1716
const char *cfg_pin_path = "/sys/fs/bpf/flow_dissector";
@@ -25,9 +24,8 @@ static void load_and_attach_program(void)
2524
int prog_fd, ret;
2625
struct bpf_object *obj;
2726

28-
ret = libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
29-
if (ret)
30-
error(1, 0, "failed to enable libbpf strict mode: %d", ret);
27+
/* Use libbpf 1.0 API mode */
28+
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
3129

3230
ret = bpf_flow_load(&obj, cfg_path_name, cfg_prog_name,
3331
cfg_map_name, NULL, &prog_fd, NULL);

tools/testing/selftests/bpf/get_cgroup_id_user.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#include "cgroup_helpers.h"
2222
#include "testing_helpers.h"
23-
#include "bpf_rlimit.h"
2423

2524
#define CHECK(condition, tag, format...) ({ \
2625
int __ret = !!(condition); \
@@ -67,6 +66,9 @@ int main(int argc, char **argv)
6766
if (CHECK(cgroup_fd < 0, "cgroup_setup_and_join", "err %d errno %d\n", cgroup_fd, errno))
6867
return 1;
6968

69+
/* Use libbpf 1.0 API mode */
70+
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
71+
7072
err = bpf_prog_test_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
7173
if (CHECK(err, "bpf_prog_test_load", "err %d errno %d\n", err, errno))
7274
goto cleanup_cgroup_env;

tools/testing/selftests/bpf/prog_tests/btf.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <linux/filter.h>
99
#include <linux/unistd.h>
1010
#include <bpf/bpf.h>
11-
#include <sys/resource.h>
1211
#include <libelf.h>
1312
#include <gelf.h>
1413
#include <string.h>

tools/testing/selftests/bpf/test_cgroup_storage.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <stdlib.h>
77
#include <sys/sysinfo.h>
88

9-
#include "bpf_rlimit.h"
109
#include "bpf_util.h"
1110
#include "cgroup_helpers.h"
1211
#include "testing_helpers.h"
@@ -52,6 +51,9 @@ int main(int argc, char **argv)
5251
goto err;
5352
}
5453

54+
/* Use libbpf 1.0 API mode */
55+
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
56+
5557
map_fd = bpf_map_create(BPF_MAP_TYPE_CGROUP_STORAGE, NULL, sizeof(key),
5658
sizeof(value), 0, NULL);
5759
if (map_fd < 0) {

tools/testing/selftests/bpf/test_dev_cgroup.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
#include "cgroup_helpers.h"
1717
#include "testing_helpers.h"
18-
#include "bpf_rlimit.h"
1918

2019
#define DEV_CGROUP_PROG "./dev_cgroup.o"
2120

@@ -28,6 +27,9 @@ int main(int argc, char **argv)
2827
int prog_fd, cgroup_fd;
2928
__u32 prog_cnt;
3029

30+
/* Use libbpf 1.0 API mode */
31+
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
32+
3133
if (bpf_prog_test_load(DEV_CGROUP_PROG, BPF_PROG_TYPE_CGROUP_DEVICE,
3234
&obj, &prog_fd)) {
3335
printf("Failed to load DEV_CGROUP program\n");

tools/testing/selftests/bpf/test_lpm_map.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <bpf/bpf.h>
2727

2828
#include "bpf_util.h"
29-
#include "bpf_rlimit.h"
3029

3130
struct tlpm_node {
3231
struct tlpm_node *next;
@@ -791,6 +790,9 @@ int main(void)
791790
/* we want predictable, pseudo random tests */
792791
srand(0xf00ba1);
793792

793+
/* Use libbpf 1.0 API mode */
794+
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
795+
794796
test_lpm_basic();
795797
test_lpm_order();
796798

tools/testing/selftests/bpf/test_lru_map.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <bpf/libbpf.h>
1919

2020
#include "bpf_util.h"
21-
#include "bpf_rlimit.h"
2221
#include "../../../include/linux/filter.h"
2322

2423
#define LOCAL_FREE_TARGET (128)
@@ -878,6 +877,9 @@ int main(int argc, char **argv)
878877
assert(nr_cpus != -1);
879878
printf("nr_cpus:%d\n\n", nr_cpus);
880879

880+
/* Use libbpf 1.0 API mode */
881+
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
882+
881883
for (f = 0; f < ARRAY_SIZE(map_flags); f++) {
882884
unsigned int tgt_free = (map_flags[f] & BPF_F_NO_COMMON_LRU) ?
883885
PERCPU_FREE_TARGET : LOCAL_FREE_TARGET;

tools/testing/selftests/bpf/test_skb_cgroup_id_user.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <bpf/bpf.h>
1616
#include <bpf/libbpf.h>
1717

18-
#include "bpf_rlimit.h"
1918
#include "cgroup_helpers.h"
2019

2120
#define CGROUP_PATH "/skb_cgroup_test"
@@ -160,6 +159,9 @@ int main(int argc, char **argv)
160159
exit(EXIT_FAILURE);
161160
}
162161

162+
/* Use libbpf 1.0 API mode */
163+
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
164+
163165
cgfd = cgroup_setup_and_join(CGROUP_PATH);
164166
if (cgfd < 0)
165167
goto err;

tools/testing/selftests/bpf/test_sock.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include "cgroup_helpers.h"
1616
#include <bpf/bpf_endian.h>
17-
#include "bpf_rlimit.h"
1817
#include "bpf_util.h"
1918

2019
#define CG_PATH "/foo"
@@ -541,6 +540,9 @@ int main(int argc, char **argv)
541540
if (cgfd < 0)
542541
goto err;
543542

543+
/* Use libbpf 1.0 API mode */
544+
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
545+
544546
if (run_tests(cgfd))
545547
goto err;
546548

tools/testing/selftests/bpf/test_sock_addr.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <bpf/libbpf.h>
2020

2121
#include "cgroup_helpers.h"
22-
#include "bpf_rlimit.h"
2322
#include "bpf_util.h"
2423

2524
#ifndef ENOTSUPP
@@ -1418,6 +1417,9 @@ int main(int argc, char **argv)
14181417
if (cgfd < 0)
14191418
goto err;
14201419

1420+
/* Use libbpf 1.0 API mode */
1421+
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
1422+
14211423
if (run_tests(cgfd))
14221424
goto err;
14231425

tools/testing/selftests/bpf/test_sockmap.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <sched.h>
1919

2020
#include <sys/time.h>
21-
#include <sys/resource.h>
2221
#include <sys/types.h>
2322
#include <sys/sendfile.h>
2423

@@ -37,7 +36,6 @@
3736
#include <bpf/libbpf.h>
3837

3938
#include "bpf_util.h"
40-
#include "bpf_rlimit.h"
4139
#include "cgroup_helpers.h"
4240

4341
int running;
@@ -2017,6 +2015,9 @@ int main(int argc, char **argv)
20172015
cg_created = 1;
20182016
}
20192017

2018+
/* Use libbpf 1.0 API mode */
2019+
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
2020+
20202021
if (test == SELFTESTS) {
20212022
err = test_selftest(cg_fd, &options);
20222023
goto out;

tools/testing/selftests/bpf/test_sysctl.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <bpf/libbpf.h>
1515

1616
#include <bpf/bpf_endian.h>
17-
#include "bpf_rlimit.h"
1817
#include "bpf_util.h"
1918
#include "cgroup_helpers.h"
2019
#include "testing_helpers.h"
@@ -1618,6 +1617,9 @@ int main(int argc, char **argv)
16181617
if (cgfd < 0)
16191618
goto err;
16201619

1620+
/* Use libbpf 1.0 API mode */
1621+
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
1622+
16211623
if (run_tests(cgfd))
16221624
goto err;
16231625

tools/testing/selftests/bpf/test_tag.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <bpf/bpf.h>
2121

2222
#include "../../../include/linux/filter.h"
23-
#include "bpf_rlimit.h"
2423
#include "testing_helpers.h"
2524

2625
static struct bpf_insn prog[BPF_MAXINSNS];
@@ -189,6 +188,9 @@ int main(void)
189188
uint32_t tests = 0;
190189
int i, fd_map;
191190

191+
/* Use libbpf 1.0 API mode */
192+
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
193+
192194
fd_map = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(int),
193195
sizeof(int), 1, &opts);
194196
assert(fd_map > 0);

tools/testing/selftests/bpf/test_tcp_check_syncookie_user.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <bpf/bpf.h>
1616
#include <bpf/libbpf.h>
1717

18-
#include "bpf_rlimit.h"
1918
#include "cgroup_helpers.h"
2019

2120
static int start_server(const struct sockaddr *addr, socklen_t len, bool dual)
@@ -235,6 +234,9 @@ int main(int argc, char **argv)
235234
exit(1);
236235
}
237236

237+
/* Use libbpf 1.0 API mode */
238+
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
239+
238240
results = get_map_fd_by_prog_id(atoi(argv[1]), &xdp);
239241
if (results < 0) {
240242
log_err("Can't get map");

tools/testing/selftests/bpf/test_tcpnotify_user.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <linux/perf_event.h>
2020
#include <linux/err.h>
2121

22-
#include "bpf_rlimit.h"
2322
#include "bpf_util.h"
2423
#include "cgroup_helpers.h"
2524

tools/testing/selftests/bpf/test_verifier_log.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
#include <bpf/bpf.h>
1313

14-
#include "bpf_rlimit.h"
15-
1614
#define LOG_SIZE (1 << 20)
1715

1816
#define err(str...) printf("ERROR: " str)
@@ -141,6 +139,9 @@ int main(int argc, char **argv)
141139

142140
memset(log, 1, LOG_SIZE);
143141

142+
/* Use libbpf 1.0 API mode */
143+
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
144+
144145
/* Test incorrect attr */
145146
printf("Test log_level 0...\n");
146147
test_log_bad(log, LOG_SIZE, 0);

tools/testing/selftests/bpf/xdp_redirect_multi.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <net/if.h>
1111
#include <unistd.h>
1212
#include <libgen.h>
13-
#include <sys/resource.h>
1413
#include <sys/ioctl.h>
1514
#include <sys/types.h>
1615
#include <sys/socket.h>

tools/testing/selftests/bpf/xdping.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <string.h>
1313
#include <unistd.h>
1414
#include <libgen.h>
15-
#include <sys/resource.h>
1615
#include <net/if.h>
1716
#include <sys/types.h>
1817
#include <sys/socket.h>
@@ -89,7 +88,6 @@ int main(int argc, char **argv)
8988
{
9089
__u32 mode_flags = XDP_FLAGS_DRV_MODE | XDP_FLAGS_SKB_MODE;
9190
struct addrinfo *a, hints = { .ai_family = AF_INET };
92-
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
9391
__u16 count = XDPING_DEFAULT_COUNT;
9492
struct pinginfo pinginfo = { 0 };
9593
const char *optstr = "c:I:NsS";
@@ -167,10 +165,8 @@ int main(int argc, char **argv)
167165
freeaddrinfo(a);
168166
}
169167

170-
if (setrlimit(RLIMIT_MEMLOCK, &r)) {
171-
perror("setrlimit(RLIMIT_MEMLOCK)");
172-
return 1;
173-
}
168+
/* Use libbpf 1.0 API mode */
169+
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
174170

175171
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
176172

tools/testing/selftests/bpf/xdpxceiver.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
#include <string.h>
9191
#include <stddef.h>
9292
#include <sys/mman.h>
93-
#include <sys/resource.h>
9493
#include <sys/types.h>
9594
#include <sys/queue.h>
9695
#include <time.h>
@@ -1448,14 +1447,13 @@ static void ifobject_delete(struct ifobject *ifobj)
14481447

14491448
int main(int argc, char **argv)
14501449
{
1451-
struct rlimit _rlim = { RLIM_INFINITY, RLIM_INFINITY };
14521450
struct pkt_stream *pkt_stream_default;
14531451
struct ifobject *ifobj_tx, *ifobj_rx;
14541452
struct test_spec test;
14551453
u32 i, j;
14561454

1457-
if (setrlimit(RLIMIT_MEMLOCK, &_rlim))
1458-
exit_with_error(errno);
1455+
/* Use libbpf 1.0 API mode */
1456+
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
14591457

14601458
ifobj_tx = ifobject_create();
14611459
if (!ifobj_tx)

0 commit comments

Comments
 (0)