Skip to contentSkip to conversation

bpf: Lift hashtab key_size limit

PATCH Superseded Florian Lehner 3 participants 6 messages Sep 22, 2020 – Oct 23, 2020 1 message not in the archive

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bpf: Lift hashtab key_size limit

Currently key_size of hashtab is limited to MAX_BPF_STACK. As the key of hashtab can also be a value from a per cpu map it can be larger than MAX_BPF_STACK.

Changelog:

v2:
- Add a test for bpf side

---
 kernel/bpf/hashtab.c                          | 16 +++----
 .../selftests/bpf/prog_tests/hash_large_key.c | 28 ++++++++++++
 .../selftests/bpf/progs/test_hash_large_key.c | 45 +++++++++++++++++++
 tools/testing/selftests/bpf/test_maps.c       |  2 +-
 4 files changed, 79 insertions(+), 12 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/hash_large_key.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_hash_large_key.c
kernel/bpf/hashtab.c +5 -11
index fe0e06284..fcac16cd4 100644
@@ -390,17 +390,11 @@ static int htab_map_alloc_check(union bpf_attr *attr)
 	    attr->value_size == 0)
 		return -EINVAL;
 
-	if (attr->key_size > MAX_BPF_STACK)
-		/* eBPF programs initialize keys on stack, so they cannot be
-		 * larger than max stack size
-		 */
-		return -E2BIG;
-
-	if (attr->value_size >= KMALLOC_MAX_SIZE -
-	    MAX_BPF_STACK - sizeof(struct htab_elem))
-		/* if value_size is bigger, the user space won't be able to
-		 * access the elements via bpf syscall. This check also makes
-		 * sure that the elem_size doesn't overflow and it's
+	if ((attr->key_size + attr->value_size) >= KMALLOC_MAX_SIZE -
+	    sizeof(struct htab_elem))
+		/* if key_size + value_size is bigger, the user space won't be
+		 * able to access the elements via bpf syscall. This check
+		 * also makes sure that the elem_size doesn't overflow and it's
 		 * kmalloc-able later in htab_map_update_elem()
 		 */
 		return -E2BIG;
tools/testing/selftests/bpf/prog_tests/hash_large_key.c new file +28 -0
new file mode 100644
index 000000000..962f56060
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2020 Florian Lehner
+
+#include <test_progs.h>
+
+void test_hash_large_key(void)
+{
+	const char *file = "./test_hash_large_key.o";
+	int prog_fd, map_fd[2];
+	struct bpf_object *obj = NULL;
+	int err = 0;
+
+	err = bpf_prog_load(file, BPF_PROG_TYPE_CGROUP_SKB, &obj, &prog_fd);
+	if (CHECK_FAIL(err)) {
+		printf("test_hash_large_key: bpf_prog_load errno %d", errno);
+		goto close_prog;
+	}
+
+	map_fd[0] = bpf_find_map(__func__, obj, "hash_map");
+	if (CHECK_FAIL(map_fd[0] < 0))
+		goto close_prog;
+	map_fd[1] = bpf_find_map(__func__, obj, "key_map");
+	if (CHECK_FAIL(map_fd[1] < 0))
+		goto close_prog;
+
+close_prog:
+	bpf_object__close(obj);
+}
tools/testing/selftests/bpf/progs/test_hash_large_key.c new file +45 -0

45 lines not on this page · raw message

tools/testing/selftests/bpf/test_maps.c +1 -1
index 754cf6117..9b2a096f0 100644
@@ -1225,7 +1225,7 @@ static void test_map_large(void)
 {
 	struct bigkey {
 		int a;
-		char b[116];
+		char b[4096];
 		long long c;
 	} key;
 	int fd, i, value;
Signature · 1 line
2.26.2
#1 lore raw
Quoted 11 lines · Florian Lehner

Currently key_size of hashtab is limited to MAX_BPF_STACK. As the key of hashtab can also be a value from a per cpu map it can be larger than MAX_BPF_STACK.

Changelog:

v2:
- Add a test for bpf side

---

I think this is OK, but just curious is there a real use-case that has keys bigger than stack size or is this just an in-theory observation.

Thanks,
John

#2 lore raw
Quoted 3 lines · John Fastabend

I think this is OK, but just curious is there a real use-case that has keys bigger than stack size or is this just an in-theory observation.

The use-case for this patch originates to implement allow/disallow lists for files and file paths. The maximum length of file paths is defined by PATH_MAX with 4096 chars including nul.
This limit exceeds MAX_BPF_STACK.

Thanks,
Florian

#4 lore raw
In reply to a message not in the archive
Quoted 5 lines · Florian Lehner
Quoted 3 lines · John Fastabend

I think this is OK, but just curious is there a real use-case that has keys bigger than stack size or is this just an in-theory observation.

Quoted 7 lines

The use-case for this patch originates to implement allow/disallow lists for files and file paths. The maximum length of file paths is defined by PATH_MAX with 4096 chars including nul.
This limit exceeds MAX_BPF_STACK.

Thanks,
Florian

OK the check appears unnecessary. It seems a bit excessive to have such large keys though.

Daniel, Alexei I couldn't find this patch in patchworks, not sure where it went.

#5 lore raw
Quoted 8 lines · John Fastabend

OK the check appears unnecessary. It seems a bit excessive to have such large keys though.

Daniel, Alexei I couldn't find this patch in patchworks, not sure where it went.

Is there something left I have missed to address? Or should I rebase the patch and send it in again?

Thanks,
Florian

#6 lore raw
Quoted 6 lines · Florian Lehner
Quoted 4 lines · John Fastabend

OK the check appears unnecessary. It seems a bit excessive to have such large keys though.

Quoted 7 lines
Quoted 4 lines

Daniel, Alexei I couldn't find this patch in patchworks, not sure where it went.

Is there something left I have missed to address? Or should I rebase the patch and send it in again?

Yeah, please rebase and resubmit. There were some problems with patchworks and a bunch of patches didn't make it to the system. Yours might have been one of them.

Quoted 3 lines

Thanks,
Florian