Skip to contentSkip to conversation

Hash support for sock

PATCH Superseded John Fastabend 2 participants 7 messages Apr 29, 2018

Subsystems (9)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

John Fastabend

author reviewer
#0 lore raw

v3 cover Hash support for sock

In the original sockmap implementation we got away with using an array similar to devmap. However, unlike devmap where an ifindex has a nice 1:1 function into the map we have found some use cases with sockets that need to be referenced using longer keys.

This series adds support for a sockhash map reusing as much of the sockmap code as possible. I made the decision to add sockhash specific helpers vs trying to generalize the existing helpers because (a) they have sockmap in the name and (b) the keys are different types. I prefer to be explicit here rather than play type games or do something else tricky.

To test this we duplicate all the sockmap testing except swap out the sockmap with a sockhash.

v2: fix file stats and add v2 tag
v3: move tool updates into test patch, move bpftool updates into
its own patch, and fixup the test patch stats to catch the renamed file and provide only diffs +/- on that.

John Fastabend (4):
bpf: sockmap, refactor sockmap routines to work with hashmap
bpf: sockmap, add hash map support
bpf: bpftool, support for sockhash
bpf: selftest additions for SOCKHASH

 include/linux/bpf.h                                |   8 +
 include/linux/bpf_types.h                          |   1 +
 include/linux/filter.h                             |   3 +-
 include/net/tcp.h                                  |   3 +-
 include/uapi/linux/bpf.h                           |   6 +-
 kernel/bpf/core.c                                  |   1 +
 kernel/bpf/sockmap.c                               | 638 ++++++++++++++++++---
 kernel/bpf/verifier.c                              |  14 +-
 net/core/filter.c                                  |  89 ++-
 tools/bpf/bpftool/map.c                            |   1 +
 tools/include/uapi/linux/bpf.h                     |   6 +-
 tools/testing/selftests/bpf/Makefile               |   3 +-
 tools/testing/selftests/bpf/test_sockhash_kern.c   |   4 +
 tools/testing/selftests/bpf/test_sockmap.c         |  27 +-
 .../{test_sockmap_kern.c => test_sockmap_kern.h}   |   6 +-
 15 files changed, 695 insertions(+), 115 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/test_sockhash_kern.c
 rename tools/testing/selftests/bpf/{test_sockmap_kern.c => test_sockmap_kern.h} (98%)
Signature · 1 line
1.9.1
author reviewer
#1 lore raw

PATCH v3 1/4 bpf: sockmap, refactor sockmap routines to work with hashmap

This patch only refactors the existing sockmap code. This will allow much of the psock initialization code path and bpf helper codes to work for both sockmap bpf map types that are backed by an array, the currently supported type, and the new hash backed bpf map type sockhash.

Most the fallout comes from three changes,

- Pushing bpf programs into an independent structure so we can use it from the htab struct in the next patch.
- Generalizing helpers to use void *key instead of the hardcoded

    u32.

- Instead of passing map/key through the metadata we now do the lookup inline. This avoids storing the key in the metadata which will be useful when keys can be longer than 4 bytes. We rename the sk pointers to sk_redir at this point as well to avoid any confusion between the current sk pointer and the redirect pointer sk_redir.

---
 include/linux/filter.h |   3 +-
 include/net/tcp.h      |   3 +-
 kernel/bpf/sockmap.c   | 148 +++++++++++++++++++++++++++++--------------------
 net/core/filter.c      |  31 +++--------
 4 files changed, 98 insertions(+), 87 deletions(-)
include/linux/filter.h +1 -2
index 4da8b23..31cdfe8 100644
@@ -512,9 +512,8 @@ struct sk_msg_buff {
 	int sg_end;
 	struct scatterlist sg_data[MAX_SKB_FRAGS];
 	bool sg_copy[MAX_SKB_FRAGS];
-	__u32 key;
 	__u32 flags;
-	struct bpf_map *map;
+	struct sock *sk_redir;
 	struct sk_buff *skb;
 	struct list_head list;
 };
include/net/tcp.h +1 -2
index 833154e..089185a 100644
@@ -814,9 +814,8 @@ struct tcp_skb_cb {
 #endif
 		} header;	/* For incoming skbs */
 		struct {
-			__u32 key;
 			__u32 flags;
-			struct bpf_map *map;
+			struct sock *sk_redir;
 			void *data_end;
 		} bpf;
 	};
kernel/bpf/sockmap.c +88 -60

281 lines not on this page · raw message

net/core/filter.c +8 -23
index d3781da..5623dc8 100644
@@ -1860,9 +1860,10 @@ int skb_do_redirect(struct sk_buff *skb)
 	if (unlikely(flags & ~(BPF_F_INGRESS)))
 		return SK_DROP;
 
-	tcb->bpf.key = key;
 	tcb->bpf.flags = flags;
-	tcb->bpf.map = map;
+	tcb->bpf.sk_redir = __sock_map_lookup_elem(map, key);
+	if (!tcb->bpf.sk_redir)
+		return SK_DROP;
 
 	return SK_PASS;
 }
@@ -1870,16 +1871,8 @@ int skb_do_redirect(struct sk_buff *skb)
 struct sock *do_sk_redirect_map(struct sk_buff *skb)
 {
 	struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
-	struct sock *sk = NULL;
-
-	if (tcb->bpf.map) {
-		sk = __sock_map_lookup_elem(tcb->bpf.map, tcb->bpf.key);
 
-		tcb->bpf.key = 0;
-		tcb->bpf.map = NULL;
-	}
-
-	return sk;
+	return tcb->bpf.sk_redir;
 }
 
 static const struct bpf_func_proto bpf_sk_redirect_map_proto = {
@@ -1899,25 +1892,17 @@ struct sock *do_sk_redirect_map(struct sk_buff *skb)
 	if (unlikely(flags & ~(BPF_F_INGRESS)))
 		return SK_DROP;
 
-	msg->key = key;
 	msg->flags = flags;
-	msg->map = map;
+	msg->sk_redir = __sock_map_lookup_elem(map, key);
+	if (!msg->sk_redir)
+		return SK_DROP;
 
 	return SK_PASS;
 }
 
 struct sock *do_msg_redirect_map(struct sk_msg_buff *msg)
 {
-	struct sock *sk = NULL;
-
-	if (msg->map) {
-		sk = __sock_map_lookup_elem(msg->map, msg->key);
-
-		msg->key = 0;
-		msg->map = NULL;
-	}
-
-	return sk;
+	return msg->sk_redir;
 }
 
 static const struct bpf_func_proto bpf_msg_redirect_map_proto = {
Signature · 1 line
1.9.1
author reviewer
#2 lore raw

PATCH v3 2/4 bpf: sockmap, add hash map support

In reply to John Fastabend

Sockmap is currently backed by an array and enforces keys to be four bytes. This works well for many use cases and was originally modeled after devmap which also uses four bytes keys. However, this has become limiting in larger use cases where a hash would be more appropriate. For example users may want to use the 5-tuple of the socket as the lookup key.

To support this add hash support.

---
 include/linux/bpf.h       |   8 +
 include/linux/bpf_types.h |   1 +
 include/uapi/linux/bpf.h  |   6 +-
 kernel/bpf/core.c         |   1 +
 kernel/bpf/sockmap.c      | 494 ++++++++++++++++++++++++++++++++++++++++++++--
 kernel/bpf/verifier.c     |  14 +-
 net/core/filter.c         |  58 ++++++
 7 files changed, 564 insertions(+), 18 deletions(-)
include/linux/bpf.h +8 -0
index 38ebbc6..add768a 100644
@@ -661,6 +661,7 @@ static inline void bpf_map_offload_map_free(struct bpf_map *map)
 
 #if defined(CONFIG_STREAM_PARSER) && defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_INET)
 struct sock  *__sock_map_lookup_elem(struct bpf_map *map, u32 key);
+struct sock  *__sock_hash_lookup_elem(struct bpf_map *map, void *key);
 int sock_map_prog(struct bpf_map *map, struct bpf_prog *prog, u32 type);
 #else
 static inline struct sock  *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
@@ -668,6 +669,12 @@ static inline struct sock  *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
 	return NULL;
 }
 
+static inline struct sock  *__sock_hash_lookup_elem(struct bpf_map *map,
+						    void *key)
+{
+	return NULL;
+}
+
 static inline int sock_map_prog(struct bpf_map *map,
 				struct bpf_prog *prog,
 				u32 type)
@@ -693,6 +700,7 @@ static inline int sock_map_prog(struct bpf_map *map,
 extern const struct bpf_func_proto bpf_skb_vlan_pop_proto;
 extern const struct bpf_func_proto bpf_get_stackid_proto;
 extern const struct bpf_func_proto bpf_sock_map_update_proto;
+extern const struct bpf_func_proto bpf_sock_hash_update_proto;
 
 /* Shared helpers among cBPF and eBPF. */
 void bpf_user_rnd_init_once(void);
include/linux/bpf_types.h +1 -0
index 2b28fcf..3101118 100644
@@ -47,6 +47,7 @@
 BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP, dev_map_ops)
 #if defined(CONFIG_STREAM_PARSER) && defined(CONFIG_INET)
 BPF_MAP_TYPE(BPF_MAP_TYPE_SOCKMAP, sock_map_ops)
+BPF_MAP_TYPE(BPF_MAP_TYPE_SOCKHASH, sock_hash_ops)
 #endif
 BPF_MAP_TYPE(BPF_MAP_TYPE_CPUMAP, cpu_map_ops)
 #endif
include/uapi/linux/bpf.h +5 -1
index da77a93..5cb983d 100644
@@ -116,6 +116,7 @@ enum bpf_map_type {
 	BPF_MAP_TYPE_DEVMAP,
 	BPF_MAP_TYPE_SOCKMAP,
 	BPF_MAP_TYPE_CPUMAP,
+	BPF_MAP_TYPE_SOCKHASH,
 };
 
 enum bpf_prog_type {
@@ -1835,7 +1836,10 @@ struct bpf_stack_build_id {
 	FN(msg_pull_data),		\
 	FN(bind),			\
 	FN(xdp_adjust_tail),		\
-	FN(skb_get_xfrm_state),
+	FN(skb_get_xfrm_state),		\
+	FN(sock_hash_update),		\
+	FN(msg_redirect_hash),		\
+	FN(sk_redirect_hash),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
kernel/bpf/core.c +1 -0
index ba03ec3..5917cc1 100644
@@ -1782,6 +1782,7 @@ void bpf_user_rnd_init_once(void)
 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
 const struct bpf_func_proto bpf_get_current_comm_proto __weak;
 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
+const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
 
 const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
 {
kernel/bpf/sockmap.c +479 -15

588 lines not on this page · raw message

kernel/bpf/verifier.c +12 -2
index eb1a596..cd3966d 100644
@@ -2078,6 +2078,13 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
 		    func_id != BPF_FUNC_msg_redirect_map)
 			goto error;
 		break;
+	case BPF_MAP_TYPE_SOCKHASH:
+		if (func_id != BPF_FUNC_sk_redirect_hash &&
+		    func_id != BPF_FUNC_sock_hash_update &&
+		    func_id != BPF_FUNC_map_delete_elem &&
+		    func_id != BPF_FUNC_msg_redirect_hash)
+			goto error;
+		break;
 	default:
 		break;
 	}
@@ -2114,11 +2121,14 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
 		break;
 	case BPF_FUNC_sk_redirect_map:
 	case BPF_FUNC_msg_redirect_map:
+	case BPF_FUNC_sock_map_update:
 		if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
 			goto error;
 		break;
-	case BPF_FUNC_sock_map_update:
-		if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
+	case BPF_FUNC_sk_redirect_hash:
+	case BPF_FUNC_msg_redirect_hash:
+	case BPF_FUNC_sock_hash_update:
+		if (map->map_type != BPF_MAP_TYPE_SOCKHASH)
 			goto error;
 		break;
 	default:
net/core/filter.c +58 -0

88 lines not on this page · raw message

Signature · 1 line
1.9.1
author reviewer
#5 lore raw

PATCH v3 3/4 bpf: selftest additions for SOCKHASH

In reply to John Fastabend

This runs existing SOCKMAP tests with SOCKHASH map type. To do this we push programs into include file and build two BPF programs. One for SOCKHASH and one for SOCKMAP.

We then run the entire test suite with each type.

---
 tools/include/uapi/linux/bpf.h                     |  6 ++++-
 tools/testing/selftests/bpf/Makefile               |  3 ++-
 tools/testing/selftests/bpf/test_sockhash_kern.c   |  4 ++++
 tools/testing/selftests/bpf/test_sockmap.c         | 27 ++++++++++++++++------
 .../{test_sockmap_kern.c => test_sockmap_kern.h}   |  6 ++---
 5 files changed, 34 insertions(+), 12 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/test_sockhash_kern.c
 rename tools/testing/selftests/bpf/{test_sockmap_kern.c => test_sockmap_kern.h} (98%)
tools/include/uapi/linux/bpf.h +5 -1
index da77a93..5cb983d 100644
@@ -116,6 +116,7 @@ enum bpf_map_type {
 	BPF_MAP_TYPE_DEVMAP,
 	BPF_MAP_TYPE_SOCKMAP,
 	BPF_MAP_TYPE_CPUMAP,
+	BPF_MAP_TYPE_SOCKHASH,
 };
 
 enum bpf_prog_type {
@@ -1835,7 +1836,10 @@ struct bpf_stack_build_id {
 	FN(msg_pull_data),		\
 	FN(bind),			\
 	FN(xdp_adjust_tail),		\
-	FN(skb_get_xfrm_state),
+	FN(skb_get_xfrm_state),		\
+	FN(sock_hash_update),		\
+	FN(msg_redirect_hash),		\
+	FN(sk_redirect_hash),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
tools/testing/selftests/bpf/Makefile +2 -1
index b64a7a3..03f9bf3 100644
@@ -32,7 +32,8 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test
 	test_l4lb_noinline.o test_xdp_noinline.o test_stacktrace_map.o \
 	sample_map_ret0.o test_tcpbpf_kern.o test_stacktrace_build_id.o \
 	sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o test_adjust_tail.o \
-	test_btf_haskv.o test_btf_nokv.o test_sockmap_kern.o test_tunnel_kern.o
+	test_btf_haskv.o test_btf_nokv.o test_sockmap_kern.o test_tunnel_kern.o \
+	test_sockmap_kern.o test_sockhash_kern.o
 
 # Order correspond to 'make run_tests' order
 TEST_PROGS := test_kmod.sh \
tools/testing/selftests/bpf/test_sockhash_kern.c new file +4 -0
new file mode 100644
index 0000000..3bf4ad4
@@ -0,0 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2018 Covalent IO, Inc. http://covalent.io
+#define TEST_MAP_TYPE BPF_MAP_TYPE_SOCKHASH
+#include "./test_sockmap_kern.h"
tools/testing/selftests/bpf/test_sockmap.c +20 -7
index 29c022d..df7afc7 100644
@@ -47,7 +47,8 @@
 #define S1_PORT 10000
 #define S2_PORT 10001
 
-#define BPF_FILENAME "test_sockmap_kern.o"
+#define BPF_SOCKMAP_FILENAME "test_sockmap_kern.o"
+#define BPF_SOCKHASH_FILENAME "test_sockmap_kern.o"
 #define CG_PATH "/sockmap"
 
 /* global sockets */
@@ -1260,9 +1261,8 @@ static int test_start_end(int cgrp)
 	BPF_PROG_TYPE_SK_MSG,
 };
 
-static int populate_progs(void)
+static int populate_progs(char *bpf_file)
 {
-	char *bpf_file = BPF_FILENAME;
 	struct bpf_program *prog;
 	struct bpf_object *obj;
 	int i = 0;
@@ -1306,11 +1306,11 @@ static int populate_progs(void)
 	return 0;
 }
 
-static int test_suite(void)
+static int __test_suite(char *bpf_file)
 {
 	int cg_fd, err;
 
-	err = populate_progs();
+	err = populate_progs(bpf_file);
 	if (err < 0) {
 		fprintf(stderr, "ERROR: (%i) load bpf failed\n", err);
 		return err;
@@ -1347,17 +1347,30 @@ static int test_suite(void)
 
 out:
 	printf("Summary: %i PASSED %i FAILED\n", passed, failed);
+	cleanup_cgroup_environment();
 	close(cg_fd);
 	return err;
 }
 
+static int test_suite(void)
+{
+	int err;
+
+	err = __test_suite(BPF_SOCKMAP_FILENAME);
+	if (err)
+		goto out;
+	err = __test_suite(BPF_SOCKHASH_FILENAME);
+out:
+	return err;
+}
+
 int main(int argc, char **argv)
 {
 	struct rlimit r = {10 * 1024 * 1024, RLIM_INFINITY};
 	int iov_count = 1, length = 1024, rate = 1;
 	struct sockmap_options options = {0};
 	int opt, longindex, err, cg_fd = 0;
-	char *bpf_file = BPF_FILENAME;
+	char *bpf_file = BPF_SOCKMAP_FILENAME;
 	int test = PING_PONG;
 
 	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
@@ -1438,7 +1451,7 @@ int main(int argc, char **argv)
 		return -1;
 	}
 
-	err = populate_progs();
+	err = populate_progs(bpf_file);
 	if (err) {
 		fprintf(stderr, "populate program: (%s) %s\n",
 			bpf_file, strerror(errno));
tools/testing/selftests/bpf/test_sockmap_kern.h renamed from tools/testing/selftests/bpf/test_sockmap_kern.c +3 -3
similarity index 98%
rename from tools/testing/selftests/bpf/test_sockmap_kern.c
rename to tools/testing/selftests/bpf/test_sockmap_kern.h
index 33de97e..0ea602f 100644
@@ -36,21 +36,21 @@
 })
 
 struct bpf_map_def SEC("maps") sock_map = {
-	.type = BPF_MAP_TYPE_SOCKMAP,
+	.type = TEST_MAP_TYPE,
 	.key_size = sizeof(int),
 	.value_size = sizeof(int),
 	.max_entries = 20,
 };
 
 struct bpf_map_def SEC("maps") sock_map_txmsg = {
-	.type = BPF_MAP_TYPE_SOCKMAP,
+	.type = TEST_MAP_TYPE,
 	.key_size = sizeof(int),
 	.value_size = sizeof(int),
 	.max_entries = 20,
 };
 
 struct bpf_map_def SEC("maps") sock_map_redir = {
-	.type = BPF_MAP_TYPE_SOCKMAP,
+	.type = TEST_MAP_TYPE,
 	.key_size = sizeof(int),
 	.value_size = sizeof(int),
 	.max_entries = 20,
Signature · 1 line
1.9.1
author reviewer
#6 lore raw

PATCH v3 4/4 bpf: bpftool, support for sockhash

In reply to John Fastabend

This adds the SOCKHASH map type to bpftools so that we get correct pretty printing.

---
 tools/bpf/bpftool/map.c | 1 +
 1 file changed, 1 insertion(+)
tools/bpf/bpftool/map.c +1 -0
index a6cdb64..4420b1a 100644
@@ -67,6 +67,7 @@
 	[BPF_MAP_TYPE_DEVMAP]		= "devmap",
 	[BPF_MAP_TYPE_SOCKMAP]		= "sockmap",
 	[BPF_MAP_TYPE_CPUMAP]		= "cpumap",
+	[BPF_MAP_TYPE_SOCKHASH]		= "sockhash",
 };
 
 static unsigned int get_possible_cpus(void)
Signature · 1 line
1.9.1
#3 lore raw

Re: [PATCH bpf-next v3 2/4] bpf: sockmap, add hash map support

In reply to John Fastabend
Quoted 10 lines · John Fastabend

Sockmap is currently backed by an array and enforces keys to be four bytes. This works well for many use cases and was originally modeled after devmap which also uses four bytes keys. However, this has become limiting in larger use cases where a hash would be more appropriate. For example users may want to use the 5-tuple of the socket as the lookup key.

To support this add hash support.

...

include/uapi/linux/bpf.h struct bpf_stack_build_id { · line 1842

Quoted 10 lines

enum bpf_prog_type {

include/uapi/linux/bpf.h +4 -1
@@ -1835,7 +1836,10 @@ struct bpf_stack_build_id {
 	FN(msg_pull_data),		\
 	FN(bind),			\
 	FN(xdp_adjust_tail),		\
-	FN(skb_get_xfrm_state),
+	FN(skb_get_xfrm_state),		\
+	FN(sock_hash_update),		\
+	FN(msg_redirect_hash),		\
+	FN(sk_redirect_hash),

Documentation for new helpers is missing. Please add it in this commit.

Also running test_sockmap in the latest bpf-next I see:
[TEST 81]: (10, 1024, 256, sendpage, pass,apply,): [ 14.227128] WARNING: CPU: 1 PID: 202 at ../net/core/stream.c:206 sk_stream_kill_queues+0x3ca/0x540

[   14.228209] Modules linked in:
[   14.228583] CPU: 1 PID: 202 Comm: test_sockmap Not tainted 4.17.0-rc2-00438-gfcf85729d8ef #941
[   14.229595] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.9.3-1.el7.centos 04/01/2014
[   14.230649] RIP: 0010:sk_stream_kill_queues+0x3ca/0x540
[   14.231251] RSP: 0018:ffff880111717d00 EFLAGS: 00010202
[   14.231855] RAX: 0000000000000000 RBX: ffff8801127f8978 RCX: 1ffff100224ff138
[   14.232664] RDX: 0000000000000000 RSI: 0000000000000f01 RDI: ffff8801127f89c0
[   14.233521] RBP: ffff8801127f8948 R08: ffffed00224ff122 R09: ffffed00224ff121
[   14.234342] R10: ffff8801127f890b R11: ffffed00224ff122 R12: ffff8801127f8998
[   14.235169] R13: ffff8801127f88a8 R14: ffff8801127f8880 R15: ffff8801127f8930
[   14.235987] FS:  00007f3d46898700(0000) GS:ffff88011b080000(0000) knlGS:0000000000000000
[   14.236946] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   14.237607] CR2: 00007f3d45e3caf0 CR3: 0000000112da0001 CR4: 00000000000606e0
[   14.238433] Call Trace:
[   14.238741]  ? tcp_v4_destroy_sock+0x273/0x4d0
[   14.239259]  inet_csk_destroy_sock+0x157/0x390
[   14.239815]  tcp_close+0x683/0xf60
[   14.240214]  inet_release+0xd6/0x1b0
[   14.240636]  sock_release+0x7e/0x190
[   14.241090]  sock_close+0xe/0x20
[   14.241593]  __fput+0x22f/0x720
[   14.241981]  task_work_run+0x10b/0x190
[   14.242425]  exit_to_usermode_loop+0xd2/0xf0
[   14.242963]  do_syscall_64+0x1d5/0x270
[   14.243410]  ? page_fault+0x8/0x30
[   14.243830]  entry_SYSCALL_64_after_hwframe+0x44/0xa9

and

[TEST 84]: (1, 1, 1, sendmsg, pass,cork,): [ 14.317100] WARNING: CPU: 1 PID: 66 at ../net/ipv4/af_inet.c:156 inet_sock_destruct+0x570/0x7b0

[   14.318147] Modules linked in:
[   14.318522] CPU: 1 PID: 66 Comm: kworker/1:1 Tainted: G        W         4.17.0-rc2-00438-gfcf85729d8ef #941
[   14.319674] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.9.3-1.el7.centos 04/01/2014
[   14.320764] Workqueue: events smap_gc_work
[   14.321262] RIP: 0010:inet_sock_destruct+0x570/0x7b0
[   14.321829] RSP: 0018:ffff880118617d08 EFLAGS: 00010206
[   14.322415] RAX: 0000000000000ffc RBX: ffff8801127f9980 RCX: ffffffff82c7ac64
[   14.323221] RDX: 1ffff100224ff34f RSI: 0000000000000004 RDI: ffff8801127f9a78
[   14.324054] RBP: ffff8801127f9ac4 R08: ffffed00224ff359 R09: ffffed00224ff358
[   14.324860] R10: ffff8801127f9ac7 R11: ffffed00224ff359 R12: ffff880111107688
[   14.325661] R13: dffffc0000000000 R14: ffff880111107688 R15: dead000000000100
[   14.326503] FS:  0000000000000000(0000) GS:ffff88011b080000(0000) knlGS:0000000000000000
[   14.327413] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   14.328091] CR2: 00007f3d45e3caf0 CR3: 0000000003a0e004 CR4: 00000000000606e0
[   14.328936] Call Trace:
[   14.329225]  __sk_destruct+0x45/0x460
[   14.329646]  smap_gc_work+0x69f/0x950
[   14.330102]  process_one_work+0x7be/0x1230
[   14.330600]  worker_thread+0xd9/0x1080
[   14.331059]  ? rescuer_thread+0xd70/0xd70
[   14.331543]  kthread+0x29e/0x390
[   14.331945]  ? kthread_create_worker_on_cpu+0xb0/0xb0
[   14.332555]  ret_from_fork+0x1f/0x30

Please send a fix for this issue first.
I'd like to see test_sockmap being clean before adding hash support to sockmap.

author reviewer
#4 lore raw

Re: [PATCH bpf-next v3 2/4] bpf: sockmap, add hash map support

Quoted 12 lines · Alexei Starovoitov
Quoted 10 lines · John Fastabend

Sockmap is currently backed by an array and enforces keys to be four bytes. This works well for many use cases and was originally modeled after devmap which also uses four bytes keys. However, this has become limiting in larger use cases where a hash would be more appropriate. For example users may want to use the 5-tuple of the socket as the lookup key.

To support this add hash support.

...

Quoted 13 lines

include/uapi/linux/bpf.h struct bpf_stack_build_id { · line 1842

Quoted 10 lines

enum bpf_prog_type {

include/uapi/linux/bpf.h +4 -1
@@ -1835,7 +1836,10 @@ struct bpf_stack_build_id {
 	FN(msg_pull_data),		\
 	FN(bind),			\
 	FN(xdp_adjust_tail),		\
-	FN(skb_get_xfrm_state),
+	FN(skb_get_xfrm_state),		\
+	FN(sock_hash_update),		\
+	FN(msg_redirect_hash),		\
+	FN(sk_redirect_hash),

Documentation for new helpers is missing. Please add it in this commit.

OK.

Quoted 56 lines

Also running test_sockmap in the latest bpf-next I see:
[TEST 81]: (10, 1024, 256, sendpage, pass,apply,): [ 14.227128] WARNING: CPU: 1 PID: 202 at ../net/core/stream.c:206 sk_stream_kill_queues+0x3ca/0x540

[   14.228209] Modules linked in:
[   14.228583] CPU: 1 PID: 202 Comm: test_sockmap Not tainted 4.17.0-rc2-00438-gfcf85729d8ef #941
[   14.229595] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.9.3-1.el7.centos 04/01/2014
[   14.230649] RIP: 0010:sk_stream_kill_queues+0x3ca/0x540
[   14.231251] RSP: 0018:ffff880111717d00 EFLAGS: 00010202
[   14.231855] RAX: 0000000000000000 RBX: ffff8801127f8978 RCX: 1ffff100224ff138
[   14.232664] RDX: 0000000000000000 RSI: 0000000000000f01 RDI: ffff8801127f89c0
[   14.233521] RBP: ffff8801127f8948 R08: ffffed00224ff122 R09: ffffed00224ff121
[   14.234342] R10: ffff8801127f890b R11: ffffed00224ff122 R12: ffff8801127f8998
[   14.235169] R13: ffff8801127f88a8 R14: ffff8801127f8880 R15: ffff8801127f8930
[   14.235987] FS:  00007f3d46898700(0000) GS:ffff88011b080000(0000) knlGS:0000000000000000
[   14.236946] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   14.237607] CR2: 00007f3d45e3caf0 CR3: 0000000112da0001 CR4: 00000000000606e0
[   14.238433] Call Trace:
[   14.238741]  ? tcp_v4_destroy_sock+0x273/0x4d0
[   14.239259]  inet_csk_destroy_sock+0x157/0x390
[   14.239815]  tcp_close+0x683/0xf60
[   14.240214]  inet_release+0xd6/0x1b0
[   14.240636]  sock_release+0x7e/0x190
[   14.241090]  sock_close+0xe/0x20
[   14.241593]  __fput+0x22f/0x720
[   14.241981]  task_work_run+0x10b/0x190
[   14.242425]  exit_to_usermode_loop+0xd2/0xf0
[   14.242963]  do_syscall_64+0x1d5/0x270
[   14.243410]  ? page_fault+0x8/0x30
[   14.243830]  entry_SYSCALL_64_after_hwframe+0x44/0xa9

and

[TEST 84]: (1, 1, 1, sendmsg, pass,cork,): [ 14.317100] WARNING: CPU: 1 PID: 66 at ../net/ipv4/af_inet.c:156 inet_sock_destruct+0x570/0x7b0

[   14.318147] Modules linked in:
[   14.318522] CPU: 1 PID: 66 Comm: kworker/1:1 Tainted: G        W         4.17.0-rc2-00438-gfcf85729d8ef #941
[   14.319674] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.9.3-1.el7.centos 04/01/2014
[   14.320764] Workqueue: events smap_gc_work
[   14.321262] RIP: 0010:inet_sock_destruct+0x570/0x7b0
[   14.321829] RSP: 0018:ffff880118617d08 EFLAGS: 00010206
[   14.322415] RAX: 0000000000000ffc RBX: ffff8801127f9980 RCX: ffffffff82c7ac64
[   14.323221] RDX: 1ffff100224ff34f RSI: 0000000000000004 RDI: ffff8801127f9a78
[   14.324054] RBP: ffff8801127f9ac4 R08: ffffed00224ff359 R09: ffffed00224ff358
[   14.324860] R10: ffff8801127f9ac7 R11: ffffed00224ff359 R12: ffff880111107688
[   14.325661] R13: dffffc0000000000 R14: ffff880111107688 R15: dead000000000100
[   14.326503] FS:  0000000000000000(0000) GS:ffff88011b080000(0000) knlGS:0000000000000000
[   14.327413] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   14.328091] CR2: 00007f3d45e3caf0 CR3: 0000000003a0e004 CR4: 00000000000606e0
[   14.328936] Call Trace:
[   14.329225]  __sk_destruct+0x45/0x460
[   14.329646]  smap_gc_work+0x69f/0x950
[   14.330102]  process_one_work+0x7be/0x1230
[   14.330600]  worker_thread+0xd9/0x1080
[   14.331059]  ? rescuer_thread+0xd70/0xd70
[   14.331543]  kthread+0x29e/0x390
[   14.331945]  ? kthread_create_worker_on_cpu+0xb0/0xb0
[   14.332555]  ret_from_fork+0x1f/0x30
Quoted 3 lines

Please send a fix for this issue first.
I'd like to see test_sockmap being clean before adding hash support to sockmap.

Sure, I'll have a fix shortly.