Skip to contentSkip to conversation

bpf: introduce timeout map

PATCH Superseded Cong Wang 3 participants 9 messages Dec 11, 2020 – Dec 14, 2020

Subsystems (6)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Cong Wang

author
#0 lore raw

cover bpf: introduce timeout map

From: Cong Wang <[email protected]>

This patchset introduces a new bpf hash map which has timeout. Patch 1 is a preparation, patch 2 is the implementation of timeout map, patch 3 contains a test case for timeout map. Please check each patch description for more details.

---
Cong Wang (3):
bpf: use index instead of hash for map_locked[]
bpf: introduce timeout map
tools: add a test case for bpf timeout map

 include/linux/bpf_types.h               |   1 +
 include/uapi/linux/bpf.h                |   3 +-
 kernel/bpf/hashtab.c                    | 296 +++++++++++++++++++++---
 kernel/bpf/syscall.c                    |   3 +-
 tools/include/uapi/linux/bpf.h          |   1 +
 tools/testing/selftests/bpf/test_maps.c |  41 ++++
 6 files changed, 314 insertions(+), 31 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 1/3 bpf: use index instead of hash for map_locked[]

From: Cong Wang <[email protected]>

Commit 20b6cc34ea74 ("bpf: Avoid hashtab deadlock with map_locked") introduced a percpu counter map_locked to bail out NMI case. It uses the hash of each bucket for indexing, which requires callers of htab_lock_bucket()/htab_unlock_bucket() to pass it in. But hash value is not always available, especially when we traverse the whole hash table where we do not have keys to compute the hash. We can just compute the index of each bucket with its address and use index instead.

This is a prerequisite for the following timeout map patch.

Cc Song Liu <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Dongdong Wang <[email protected]>
---
 kernel/bpf/hashtab.c | 57 +++++++++++++++++++++++---------------------
 1 file changed, 30 insertions(+), 27 deletions(-)
kernel/bpf/hashtab.c +30 -27
index 7e848200cd26..f0b7b54fa3a8 100644
@@ -156,16 +156,17 @@ static void htab_init_buckets(struct bpf_htab *htab)
 }
 
 static inline int htab_lock_bucket(const struct bpf_htab *htab,
-				   struct bucket *b, u32 hash,
-				   unsigned long *pflags)
+				   struct bucket *b, unsigned long *pflags)
 {
 	unsigned long flags;
+	unsigned int index;
 
-	hash = hash & HASHTAB_MAP_LOCK_MASK;
+	index = b - htab->buckets;
+	index &= HASHTAB_MAP_LOCK_MASK;
 
 	migrate_disable();
-	if (unlikely(__this_cpu_inc_return(*(htab->map_locked[hash])) != 1)) {
-		__this_cpu_dec(*(htab->map_locked[hash]));
+	if (unlikely(__this_cpu_inc_return(*(htab->map_locked[index])) != 1)) {
+		__this_cpu_dec(*(htab->map_locked[index]));
 		migrate_enable();
 		return -EBUSY;
 	}
@@ -180,15 +181,17 @@ static inline int htab_lock_bucket(const struct bpf_htab *htab,
 }
 
 static inline void htab_unlock_bucket(const struct bpf_htab *htab,
-				      struct bucket *b, u32 hash,
-				      unsigned long flags)
+				      struct bucket *b, unsigned long flags)
 {
-	hash = hash & HASHTAB_MAP_LOCK_MASK;
+	unsigned int index;
+
+	index = b - htab->buckets;
+	index &= HASHTAB_MAP_LOCK_MASK;
 	if (htab_use_raw_lock(htab))
 		raw_spin_unlock_irqrestore(&b->raw_lock, flags);
 	else
 		spin_unlock_irqrestore(&b->lock, flags);
-	__this_cpu_dec(*(htab->map_locked[hash]));
+	__this_cpu_dec(*(htab->map_locked[index]));
 	migrate_enable();
 }
 
@@ -710,7 +713,7 @@ static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
 	b = __select_bucket(htab, tgt_l->hash);
 	head = &b->head;
 
-	ret = htab_lock_bucket(htab, b, tgt_l->hash, &flags);
+	ret = htab_lock_bucket(htab, b, &flags);
 	if (ret)
 		return false;
 
@@ -720,7 +723,7 @@ static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
 			break;
 		}
 
-	htab_unlock_bucket(htab, b, tgt_l->hash, flags);
+	htab_unlock_bucket(htab, b, flags);
 
 	return l == tgt_l;
 }
@@ -1019,7 +1022,7 @@ static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
 		 */
 	}
 
-	ret = htab_lock_bucket(htab, b, hash, &flags);
+	ret = htab_lock_bucket(htab, b, &flags);
 	if (ret)
 		return ret;
 
@@ -1062,7 +1065,7 @@ static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
 	}
 	ret = 0;
 err:
-	htab_unlock_bucket(htab, b, hash, flags);
+	htab_unlock_bucket(htab, b, flags);
 	return ret;
 }
 
@@ -1100,7 +1103,7 @@ static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
 		return -ENOMEM;
 	memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
 
-	ret = htab_lock_bucket(htab, b, hash, &flags);
+	ret = htab_lock_bucket(htab, b, &flags);
 	if (ret)
 		return ret;
 
@@ -1121,7 +1124,7 @@ static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
 	ret = 0;
 
 err:
-	htab_unlock_bucket(htab, b, hash, flags);
+	htab_unlock_bucket(htab, b, flags);
 
 	if (ret)
 		bpf_lru_push_free(&htab->lru, &l_new->lru_node);
@@ -1156,7 +1159,7 @@ static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
 	b = __select_bucket(htab, hash);
 	head = &b->head;
 
-	ret = htab_lock_bucket(htab, b, hash, &flags);
+	ret = htab_lock_bucket(htab, b, &flags);
 	if (ret)
 		return ret;
 
@@ -1181,7 +1184,7 @@ static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
 	}
 	ret = 0;
 err:
-	htab_unlock_bucket(htab, b, hash, flags);
+	htab_unlock_bucket(htab, b, flags);
 	return ret;
 }
 
@@ -1221,7 +1224,7 @@ static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
 			return -ENOMEM;
 	}
 
-	ret = htab_lock_bucket(htab, b, hash, &flags);
+	ret = htab_lock_bucket(htab, b, &flags);
 	if (ret)
 		return ret;
 
@@ -1245,7 +1248,7 @@ static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
 	}
 	ret = 0;
 err:
-	htab_unlock_bucket(htab, b, hash, flags);
+	htab_unlock_bucket(htab, b, flags);
 	if (l_new)
 		bpf_lru_push_free(&htab->lru, &l_new->lru_node);
 	return ret;
@@ -1283,7 +1286,7 @@ static int htab_map_delete_elem(struct bpf_map *map, void *key)
 	b = __select_bucket(htab, hash);
 	head = &b->head;
 
-	ret = htab_lock_bucket(htab, b, hash, &flags);
+	ret = htab_lock_bucket(htab, b, &flags);
 	if (ret)
 		return ret;
 
@@ -1296,7 +1299,7 @@ static int htab_map_delete_elem(struct bpf_map *map, void *key)
 		ret = -ENOENT;
 	}
 
-	htab_unlock_bucket(htab, b, hash, flags);
+	htab_unlock_bucket(htab, b, flags);
 	return ret;
 }
 
@@ -1318,7 +1321,7 @@ static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
 	b = __select_bucket(htab, hash);
 	head = &b->head;
 
-	ret = htab_lock_bucket(htab, b, hash, &flags);
+	ret = htab_lock_bucket(htab, b, &flags);
 	if (ret)
 		return ret;
 
@@ -1329,7 +1332,7 @@ static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
 	else
 		ret = -ENOENT;
 
-	htab_unlock_bucket(htab, b, hash, flags);
+	htab_unlock_bucket(htab, b, flags);
 	if (l)
 		bpf_lru_push_free(&htab->lru, &l->lru_node);
 	return ret;
@@ -1480,7 +1483,7 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *map,
 	head = &b->head;
 	/* do not grab the lock unless need it (bucket_cnt > 0). */
 	if (locked) {
-		ret = htab_lock_bucket(htab, b, batch, &flags);
+		ret = htab_lock_bucket(htab, b, &flags);
 		if (ret)
 			goto next_batch;
 	}
@@ -1500,7 +1503,7 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *map,
 		/* Note that since bucket_cnt > 0 here, it is implicit
 		 * that the locked was grabbed, so release it.
 		 */
-		htab_unlock_bucket(htab, b, batch, flags);
+		htab_unlock_bucket(htab, b, flags);
 		rcu_read_unlock();
 		bpf_enable_instrumentation();
 		goto after_loop;
@@ -1511,7 +1514,7 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *map,
 		/* Note that since bucket_cnt > 0 here, it is implicit
 		 * that the locked was grabbed, so release it.
 		 */
-		htab_unlock_bucket(htab, b, batch, flags);
+		htab_unlock_bucket(htab, b, flags);
 		rcu_read_unlock();
 		bpf_enable_instrumentation();
 		kvfree(keys);
@@ -1564,7 +1567,7 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *map,
 		dst_val += value_size;
 	}
 
-	htab_unlock_bucket(htab, b, batch, flags);
+	htab_unlock_bucket(htab, b, flags);
 	locked = false;
 
 	while (node_to_free) {
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH 2/3 bpf: introduce timeout map

In reply to Cong Wang

From: Cong Wang <[email protected]>

This borrows the idea from conntrack and will be used for conntrack in bpf too. Each element in a timeout map has a user-specified timeout in secs, after it expires it will be automatically removed from the map.

There are two cases here:

1. When the timeout map is idle, that is, no one updates or accesses it, we rely on the idle work to scan the whole hash table and remove these expired. The idle work is scheduled every 1 sec.

2. When the timeout map is actively accessed, we could reach expired elements before the idle work kicks in, we can simply skip them and schedule another work to do the actual removal work. We avoid taking locks on fast path.

The timeout of each element can be set or updated via bpf_map_update_elem() and we reuse the upper 32-bit of the 64-bit flag for the timeout value.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Dongdong Wang <[email protected]>
---
 include/linux/bpf_types.h      |   1 +
 include/uapi/linux/bpf.h       |   3 +-
 kernel/bpf/hashtab.c           | 239 ++++++++++++++++++++++++++++++++-
 kernel/bpf/syscall.c           |   3 +-
 tools/include/uapi/linux/bpf.h |   1 +
 5 files changed, 243 insertions(+), 4 deletions(-)
include/linux/bpf_types.h +1 -0
index 99f7fd657d87..901c4e01d726 100644
@@ -90,6 +90,7 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_CGROUP_STORAGE, cgroup_storage_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE, cgroup_storage_map_ops)
 #endif
 BPF_MAP_TYPE(BPF_MAP_TYPE_HASH, htab_map_ops)
+BPF_MAP_TYPE(BPF_MAP_TYPE_TIMEOUT_HASH, htab_timeout_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_PERCPU_HASH, htab_percpu_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_LRU_HASH, htab_lru_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_LRU_PERCPU_HASH, htab_lru_percpu_map_ops)
include/uapi/linux/bpf.h +2 -1
index 30b477a26482..dedb47bc3f52 100644
@@ -158,6 +158,7 @@ enum bpf_map_type {
 	BPF_MAP_TYPE_RINGBUF,
 	BPF_MAP_TYPE_INODE_STORAGE,
 	BPF_MAP_TYPE_TASK_STORAGE,
+	BPF_MAP_TYPE_TIMEOUT_HASH,
 };
 
 /* Note that tracing related programs such as
@@ -393,7 +394,7 @@ enum bpf_link_type {
  */
 #define BPF_PSEUDO_CALL		1
 
-/* flags for BPF_MAP_UPDATE_ELEM command */
+/* flags for BPF_MAP_UPDATE_ELEM command, upper 32 bits are timeout */
 enum {
 	BPF_ANY		= 0, /* create new element or update existing */
 	BPF_NOEXIST	= 1, /* create new element if it didn't exist */
kernel/bpf/hashtab.c +237 -2

353 lines not on this page · raw message

kernel/bpf/syscall.c +2 -1
index 287be337d5f6..9ebd2e380a57 100644
@@ -778,7 +778,8 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf,
 		    map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
 		    map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
 		    map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
-		    map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
+		    map->map_type != BPF_MAP_TYPE_TASK_STORAGE &&
+		    map->map_type != BPF_MAP_TYPE_TIMEOUT_HASH)
 			return -ENOTSUPP;
 		if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
 		    map->value_size) {
tools/include/uapi/linux/bpf.h +1 -0
index 30b477a26482..684b8011a97a 100644
@@ -158,6 +158,7 @@ enum bpf_map_type {
 	BPF_MAP_TYPE_RINGBUF,
 	BPF_MAP_TYPE_INODE_STORAGE,
 	BPF_MAP_TYPE_TASK_STORAGE,
+	BPF_MAP_TYPE_TIMEOUT_HASH,
 };
 
 /* Note that tracing related programs such as
Signature · 1 line
2.25.1
author
#3 lore raw

PATCH 3/3 tools: add a test case for bpf timeout map

In reply to Cong Wang

From: Cong Wang <[email protected]>

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Dongdong Wang <[email protected]>
---
 tools/testing/selftests/bpf/test_maps.c | 41 +++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
tools/testing/selftests/bpf/test_maps.c +41 -0
index 0ad3e6305ff0..9fc7b5424fdf 100644
@@ -1723,6 +1723,45 @@ static void test_reuseport_array(void)
 	close(map_fd);
 }
 
+static void test_timeout_map(void)
+{
+	int val1 = 1, val2 = 2, val3 = 3;
+	int key1 = 1, key2 = 2, key3 = 3;
+	int fd;
+
+	fd = bpf_create_map(BPF_MAP_TYPE_TIMEOUT_HASH, sizeof(int), sizeof(int),
+			    3, map_flags);
+	if (fd < 0) {
+		printf("Failed to create timeout map '%s'!\n", strerror(errno));
+		exit(1);
+	}
+
+	/* Timeout after 1 secs */
+	assert(bpf_map_update_elem(fd, &key1, &val1, (u64)1<<32) == 0);
+	/* Timeout after 2 secs */
+	assert(bpf_map_update_elem(fd, &key2, &val2, (u64)2<<32) == 0);
+	/* Timeout after 10 secs */
+	assert(bpf_map_update_elem(fd, &key3, &val3, (u64)10<<32) == 0);
+
+	sleep(1);
+	assert(bpf_map_lookup_elem(fd, &key1, &val1) != 0);
+	val2 = 0;
+	assert(bpf_map_lookup_elem(fd, &key2, &val2) == 0 && val2 == 2);
+
+	sleep(1);
+	assert(bpf_map_lookup_elem(fd, &key1, &val1) != 0);
+	assert(bpf_map_lookup_elem(fd, &key2, &val2) != 0);
+
+	/* Modify timeout to expire it earlier */
+	val3 = 0;
+	assert(bpf_map_lookup_elem(fd, &key3, &val3) == 0 && val3 == 3);
+	assert(bpf_map_update_elem(fd, &key3, &val3, (u64)1<<32) == 0);
+	sleep(1);
+	assert(bpf_map_lookup_elem(fd, &key3, &val3) != 0);
+
+	close(fd);
+}
+
 static void run_all_tests(void)
 {
 	test_hashmap(0, NULL);
@@ -1752,6 +1791,8 @@ static void run_all_tests(void)
 	test_stackmap(0, NULL);
 
 	test_map_in_map();
+
+	test_timeout_map();
 }
 
 #define DEFINE_TEST(name) extern void test_##name(void);
Signature · 1 line
2.25.1
#4 lore raw
In reply to Cong Wang
Quoted 9 lines · Cong Wang

From: Cong Wang <[email protected]>

This patchset introduces a new bpf hash map which has timeout. Patch 1 is a preparation, patch 2 is the implementation of timeout map, patch 3 contains a test case for timeout map. Please check each patch description for more details.

---

This patch set seems to be breaking existing selftests. Please take a look ([0]).
Also patch #3 should have a commit message, even if pretty trivial one.

[0] https://travis-ci.com/github/kernel-patches/bpf/builds/207928289

Quoted 16 lines

Cong Wang (3):
bpf: use index instead of hash for map_locked[]
bpf: introduce timeout map
tools: add a test case for bpf timeout map

 include/linux/bpf_types.h               |   1 +
 include/uapi/linux/bpf.h                |   3 +-
 kernel/bpf/hashtab.c                    | 296 +++++++++++++++++++++---
 kernel/bpf/syscall.c                    |   3 +-
 tools/include/uapi/linux/bpf.h          |   1 +
 tools/testing/selftests/bpf/test_maps.c |  41 ++++
 6 files changed, 314 insertions(+), 31 deletions(-)

--
2.25.1

author
#5 lore raw
Quoted 10 lines · Andrii Nakryiko
Quoted 8 lines · Cong Wang

From: Cong Wang <[email protected]>

This patchset introduces a new bpf hash map which has timeout. Patch 1 is a preparation, patch 2 is the implementation of timeout map, patch 3 contains a test case for timeout map. Please check each patch description for more details.

Quoted 4 lines
Quoted 1 line

---

This patch set seems to be breaking existing selftests. Please take a look ([0]).

Interesting, looks unrelated to my patches but let me double check.

Quoted 1 line

Also patch #3 should have a commit message, even if pretty trivial one.

Yeah, I thought its subject is sufficient for a trivial patch.

Thanks.

author
#6 lore raw
Quoted 13 lines · Cong Wang
Quoted 10 lines · Andrii Nakryiko
Quoted 8 lines · Cong Wang

From: Cong Wang <[email protected]>

This patchset introduces a new bpf hash map which has timeout. Patch 1 is a preparation, patch 2 is the implementation of timeout map, patch 3 contains a test case for timeout map. Please check each patch description for more details.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

---

This patch set seems to be breaking existing selftests. Please take a look ([0]).

Interesting, looks unrelated to my patches but let me double check.

Cc'ing Andrey...

Looks like the failure is due to the addition of a new member to struct htab_elem. Any reason why it is hard-coded as 64 in check_hash()? And what's the point of verifying its size? htab_elem should be only visible to the kernel itself.

I can certainly change 64 to whatever its new size is, but I do wonder why the test is there.

Thanks.

#7 lore raw

Cong Wang <[email protected]> [Sat, 2020-12-12 15:18 -0800]:

Quoted 23 lines · Cong Wang
Quoted 13 lines
Quoted 10 lines · Andrii Nakryiko
Quoted 8 lines · Cong Wang

From: Cong Wang <[email protected]>

This patchset introduces a new bpf hash map which has timeout. Patch 1 is a preparation, patch 2 is the implementation of timeout map, patch 3 contains a test case for timeout map. Please check each patch description for more details.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

---

This patch set seems to be breaking existing selftests. Please take a look ([0]).

Interesting, looks unrelated to my patches but let me double check.

Cc'ing Andrey...

Quoted 7 lines

Looks like the failure is due to the addition of a new member to struct htab_elem. Any reason why it is hard-coded as 64 in check_hash()? And what's the point of verifying its size? htab_elem should be only visible to the kernel itself.

I can certainly change 64 to whatever its new size is, but I do wonder why the test is there.

Cong, the test is there to make sure that access to map pointers from BPF program works.

Please see (41c48f3a9823 "bpf: Support access to bpf map fields") for more details on what "access to map pointer" means, but it's basically a way to access any field (e.g. max_entries) of common struct bpf_map or any type-specific struct like struct bpf_htab from BPF program, i.e. these structs are visible to not only kernel but also to BPF programs.

The point of the test is to access a few fields from every map struct and make sure it works. Changing struct htab_elem indeed breaks the VERIFY(hash->elem_size == 64); check. But it can be easily updated (from 64 to whatever new size is) or replaced by some other field check. htab->elem_size was chosen semi-randomly since any bpf_htab-specific field would work for the test's purposes.

Hope it clarifies.

Also since you add a new map type it would be great to cover it in tools/testing/selftests/bpf/progs/map_ptr_kern.c as well.

Signature · 1 line
Andrey Ignatov
author
#8 lore raw
Quoted 33 lines · Andrey Ignatov

Cong Wang <[email protected]> [Sat, 2020-12-12 15:18 -0800]:

Quoted 23 lines · Cong Wang
Quoted 13 lines
Quoted 10 lines · Andrii Nakryiko
Quoted 8 lines · Cong Wang

From: Cong Wang <[email protected]>

This patchset introduces a new bpf hash map which has timeout. Patch 1 is a preparation, patch 2 is the implementation of timeout map, patch 3 contains a test case for timeout map. Please check each patch description for more details.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

---

This patch set seems to be breaking existing selftests. Please take a look ([0]).

Interesting, looks unrelated to my patches but let me double check.

Cc'ing Andrey...

Quoted 7 lines

Looks like the failure is due to the addition of a new member to struct htab_elem. Any reason why it is hard-coded as 64 in check_hash()? And what's the point of verifying its size? htab_elem should be only visible to the kernel itself.

I can certainly change 64 to whatever its new size is, but I do wonder why the test is there.

Quoted 8 lines

Cong, the test is there to make sure that access to map pointers from BPF program works.

Please see (41c48f3a9823 "bpf: Support access to bpf map fields") for more details on what "access to map pointer" means, but it's basically a way to access any field (e.g. max_entries) of common struct bpf_map or any type-specific struct like struct bpf_htab from BPF program, i.e. these structs are visible to not only kernel but also to BPF programs.

I see, I was not aware of this.

Quoted 7 lines

The point of the test is to access a few fields from every map struct and make sure it works. Changing struct htab_elem indeed breaks the VERIFY(hash->elem_size == 64); check. But it can be easily updated (from 64 to whatever new size is) or replaced by some other field check. htab->elem_size was chosen semi-randomly since any bpf_htab-specific field would work for the test's purposes.

Good to know it is useful, I will have to change 64 to 72, as I tried to use sizeof but struct htab_elem is not visible to that test.

Quoted 5 lines

Hope it clarifies.

Also since you add a new map type it would be great to cover it in tools/testing/selftests/bpf/progs/map_ptr_kern.c as well.

Yeah, will do.

Thanks.