Skip to content

Commit 5d3919a

Browse files
jsitnickiborkmann
authored andcommitted
selftests/bpf: Test freeing sockmap/sockhash with a socket in it
Commit 7e81a35 ("bpf: Sockmap, ensure sock lock held during tear down") introduced sleeping issues inside RCU critical sections and while holding a spinlock on sockmap/sockhash tear-down. There has to be at least one socket in the map for the problem to surface. This adds a test that triggers the warnings for broken locking rules. Not a fix per se, but rather tooling to verify the accompanying fixes. Run on a VM with 1 vCPU to reproduce the warnings. Fixes: 7e81a35 ("bpf: Sockmap, ensure sock lock held during tear down") Signed-off-by: Jakub Sitnicki <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: John Fastabend <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 0b2dc83 commit 5d3919a

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (c) 2020 Cloudflare
3+
4+
#include "test_progs.h"
5+
6+
static int connected_socket_v4(void)
7+
{
8+
struct sockaddr_in addr = {
9+
.sin_family = AF_INET,
10+
.sin_port = htons(80),
11+
.sin_addr = { inet_addr("127.0.0.1") },
12+
};
13+
socklen_t len = sizeof(addr);
14+
int s, repair, err;
15+
16+
s = socket(AF_INET, SOCK_STREAM, 0);
17+
if (CHECK_FAIL(s == -1))
18+
goto error;
19+
20+
repair = TCP_REPAIR_ON;
21+
err = setsockopt(s, SOL_TCP, TCP_REPAIR, &repair, sizeof(repair));
22+
if (CHECK_FAIL(err))
23+
goto error;
24+
25+
err = connect(s, (struct sockaddr *)&addr, len);
26+
if (CHECK_FAIL(err))
27+
goto error;
28+
29+
repair = TCP_REPAIR_OFF_NO_WP;
30+
err = setsockopt(s, SOL_TCP, TCP_REPAIR, &repair, sizeof(repair));
31+
if (CHECK_FAIL(err))
32+
goto error;
33+
34+
return s;
35+
error:
36+
perror(__func__);
37+
close(s);
38+
return -1;
39+
}
40+
41+
/* Create a map, populate it with one socket, and free the map. */
42+
static void test_sockmap_create_update_free(enum bpf_map_type map_type)
43+
{
44+
const int zero = 0;
45+
int s, map, err;
46+
47+
s = connected_socket_v4();
48+
if (CHECK_FAIL(s == -1))
49+
return;
50+
51+
map = bpf_create_map(map_type, sizeof(int), sizeof(int), 1, 0);
52+
if (CHECK_FAIL(map == -1)) {
53+
perror("bpf_create_map");
54+
goto out;
55+
}
56+
57+
err = bpf_map_update_elem(map, &zero, &s, BPF_NOEXIST);
58+
if (CHECK_FAIL(err)) {
59+
perror("bpf_map_update");
60+
goto out;
61+
}
62+
63+
out:
64+
close(map);
65+
close(s);
66+
}
67+
68+
void test_sockmap_basic(void)
69+
{
70+
if (test__start_subtest("sockmap create_update_free"))
71+
test_sockmap_create_update_free(BPF_MAP_TYPE_SOCKMAP);
72+
if (test__start_subtest("sockhash create_update_free"))
73+
test_sockmap_create_update_free(BPF_MAP_TYPE_SOCKHASH);
74+
}

0 commit comments

Comments
 (0)