Skip to content

Commit 1054270

Browse files
mrpreKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
selftests/bpf: Add test to cover sockmap with ktls
The selftest can reproduce an issue where we miss the uncharge operation when freeing msg, which will cause the following warning. We fixed the issue and added this reproducer to selftest to ensure it will not happen again. ------------[ cut here ]------------ WARNING: CPU: 1 PID: 40 at net/ipv4/af_inet.c inet_sock_destruct+0x173/0x1d5 Tainted: [W]=WARN Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.15.0-1 04/01/2014 Workqueue: events sk_psock_destroy RIP: 0010:inet_sock_destruct+0x173/0x1d5 RSP: 0018:ffff8880085cfc18 EFLAGS: 00010202 RAX: 1ffff11003dbfc00 RBX: ffff88801edfe3e8 RCX: ffffffff822f5af4 RDX: 0000000000000007 RSI: dffffc0000000000 RDI: ffff88801edfe16c RBP: ffff88801edfe184 R08: ffffed1003dbfc31 R09: 0000000000000000 R10: ffffffff822f5ab7 R11: ffff88801edfe187 R12: ffff88801edfdec0 R13: ffff888020376ac0 R14: ffff888020376ac0 R15: ffff888020376a60 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000556365155830 CR3: 000000001d6aa000 CR4: 0000000000350ef0 Call Trace: <TASK> __sk_destruct+0x46/0x222 sk_psock_destroy+0x22f/0x242 process_one_work+0x504/0x8a8 ? process_one_work+0x39d/0x8a8 ? __pfx_process_one_work+0x10/0x10 ? worker_thread+0x44/0x2ae ? __list_add_valid_or_report+0x83/0xea ? srso_return_thunk+0x5/0x5f ? __list_add+0x45/0x52 process_scheduled_works+0x73/0x82 worker_thread+0x1ce/0x2ae Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
1 parent 417f454 commit 1054270

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

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

+76
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,80 @@ static void test_sockmap_ktls_tx_cork(int family, int sotype, bool push)
305305
test_sockmap_ktls__destroy(skel);
306306
}
307307

308+
static void test_sockmap_ktls_tx_no_buf(int family, int sotype, bool push)
309+
{
310+
int c = 0, p = 0, one = 1, two = 2;
311+
struct test_sockmap_ktls *skel;
312+
unsigned char *data = NULL;
313+
struct msghdr msg = {0};
314+
struct iovec iov[2];
315+
int prog_fd, map_fd;
316+
int txrx_buf = 1024;
317+
int iov_length = 8192;
318+
int err;
319+
320+
skel = test_sockmap_ktls__open_and_load();
321+
if (!ASSERT_TRUE(skel, "open ktls skel"))
322+
return;
323+
324+
err = create_pair(family, sotype, &c, &p);
325+
if (!ASSERT_OK(err, "create_pair()"))
326+
goto out;
327+
328+
err = setsockopt(c, SOL_SOCKET, SO_RCVBUFFORCE, &txrx_buf, sizeof(int));
329+
err |= setsockopt(p, SOL_SOCKET, SO_SNDBUFFORCE, &txrx_buf, sizeof(int));
330+
if (!ASSERT_OK(err, "set buf limit"))
331+
goto out;
332+
333+
prog_fd = bpf_program__fd(skel->progs.prog_sk_policy_redir);
334+
map_fd = bpf_map__fd(skel->maps.sock_map);
335+
336+
err = bpf_prog_attach(prog_fd, map_fd, BPF_SK_MSG_VERDICT, 0);
337+
if (!ASSERT_OK(err, "bpf_prog_attach sk msg"))
338+
goto out;
339+
340+
err = bpf_map_update_elem(map_fd, &one, &c, BPF_NOEXIST);
341+
if (!ASSERT_OK(err, "bpf_map_update_elem(c)"))
342+
goto out;
343+
344+
err = bpf_map_update_elem(map_fd, &two, &p, BPF_NOEXIST);
345+
if (!ASSERT_OK(err, "bpf_map_update_elem(p)"))
346+
goto out;
347+
348+
skel->bss->apply_bytes = 1024;
349+
350+
err = init_ktls_pairs(c, p);
351+
if (!ASSERT_OK(err, "init_ktls_pairs(c, p)"))
352+
goto out;
353+
354+
data = calloc(iov_length, sizeof(char));
355+
if (!data)
356+
goto out;
357+
358+
iov[0].iov_base = data;
359+
iov[0].iov_len = iov_length;
360+
iov[1].iov_base = data;
361+
iov[1].iov_len = iov_length;
362+
msg.msg_iov = iov;
363+
msg.msg_iovlen = 2;
364+
365+
for (;;) {
366+
err = sendmsg(c, &msg, MSG_DONTWAIT);
367+
if (err <= 0)
368+
break;
369+
}
370+
371+
out:
372+
if (data)
373+
free(data);
374+
if (c)
375+
close(c);
376+
if (p)
377+
close(p);
378+
379+
test_sockmap_ktls__destroy(skel);
380+
}
381+
308382
static void run_tests(int family, enum bpf_map_type map_type)
309383
{
310384
int map;
@@ -329,6 +403,8 @@ static void run_ktls_test(int family, int sotype)
329403
test_sockmap_ktls_tx_cork(family, sotype, false);
330404
if (test__start_subtest("tls tx cork with push"))
331405
test_sockmap_ktls_tx_cork(family, sotype, true);
406+
if (test__start_subtest("tls tx egress with no buf"))
407+
test_sockmap_ktls_tx_no_buf(family, sotype, true);
332408
}
333409

334410
void test_sockmap_ktls(void)

tools/testing/selftests/bpf/progs/test_sockmap_ktls.c

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
int cork_byte;
77
int push_start;
88
int push_end;
9+
int apply_bytes;
910

1011
struct {
1112
__uint(type, BPF_MAP_TYPE_SOCKMAP);
@@ -24,3 +25,12 @@ int prog_sk_policy(struct sk_msg_md *msg)
2425

2526
return SK_PASS;
2627
}
28+
29+
SEC("sk_msg")
30+
int prog_sk_policy_redir(struct sk_msg_md *msg)
31+
{
32+
int two = 2;
33+
34+
bpf_msg_apply_bytes(msg, apply_bytes);
35+
return bpf_msg_redirect_map(msg, &sock_map, two, 0);
36+
}

0 commit comments

Comments
 (0)