Skip to contentSkip to conversation

bpf: add lookup_and_delete_elem support to hashtab

PATCH Superseded Denis Salopek 3 participants 7 messages Jan 27, 2021 – Feb 23, 2021 1 message not in the archive

Subsystems (4)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: add lookup_and_delete_elem support to hashtab

Extend the existing bpf_map_lookup_and_delete_elem() functionality to hashtab maps, in addition to stacks and queues.
Create a new hashtab bpf_map_ops function that does lookup and deletion of the element under the same bucket lock and add the created map_ops to bpf.h.
Add the appropriate test case to 'maps' selftests.

Cc Juraj Vijtiuk <[email protected]>
Cc Luka Oreskovic <[email protected]>
Cc Luka Perkov <[email protected]>
---
 include/linux/bpf.h                     |  1 +
 kernel/bpf/hashtab.c                    | 38 +++++++++++++++++++++++++
 kernel/bpf/syscall.c                    |  9 ++++++
 tools/testing/selftests/bpf/test_maps.c |  7 +++++
 4 files changed, 55 insertions(+)
include/linux/bpf.h +1 -0
index 1aac2af12fed..003c1505f0e3 100644
@@ -77,6 +77,7 @@ struct bpf_map_ops {
 
 	/* funcs callable from userspace and from eBPF programs */
 	void *(*map_lookup_elem)(struct bpf_map *map, void *key);
+	int (*map_lookup_and_delete_elem)(struct bpf_map *map, void *key, void *value);
 	int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
 	int (*map_delete_elem)(struct bpf_map *map, void *key);
 	int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
kernel/bpf/hashtab.c +38 -0
index c1ac7f964bc9..8d8463e0ea34 100644
@@ -973,6 +973,43 @@ static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
 	return 0;
 }
 
+/* Called from syscall or from eBPF program */
+static int htab_map_lookup_and_delete_elem(struct bpf_map *map, void *key, void *value)
+{
+	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
+	struct hlist_nulls_head *head;
+	struct bucket *b;
+	struct htab_elem *l;
+	unsigned long flags;
+	u32 hash, key_size;
+	int ret;
+
+	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held());
+
+	key_size = map->key_size;
+
+	hash = htab_map_hash(key, key_size, htab->hashrnd);
+	b = __select_bucket(htab, hash);
+	head = &b->head;
+
+	ret = htab_lock_bucket(htab, b, hash, &flags);
+	if (ret)
+		return ret;
+
+	l = lookup_elem_raw(head, hash, key, key_size);
+
+	if (l) {
+		copy_map_value(map, value, l->key + round_up(key_size, 8));
+		hlist_nulls_del_rcu(&l->hash_node);
+		free_htab_elem(htab, l);
+	} else {
+		ret = -ENOENT;
+	}
+
+	htab_unlock_bucket(htab, b, hash, flags);
+	return ret;
+}
+
 /* Called from syscall or from eBPF program */
 static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
 				u64 map_flags)
@@ -1877,6 +1914,7 @@ const struct bpf_map_ops htab_map_ops = {
 	.map_free = htab_map_free,
 	.map_get_next_key = htab_map_get_next_key,
 	.map_lookup_elem = htab_map_lookup_elem,
+	.map_lookup_and_delete_elem = htab_map_lookup_and_delete_elem,
 	.map_update_elem = htab_map_update_elem,
 	.map_delete_elem = htab_map_delete_elem,
 	.map_gen_lookup = htab_map_gen_lookup,
kernel/bpf/syscall.c +9 -0
index e5999d86c76e..4ff45c8d1077 100644
@@ -1505,6 +1505,15 @@ static int map_lookup_and_delete_elem(union bpf_attr *attr)
 	if (map->map_type == BPF_MAP_TYPE_QUEUE ||
 	    map->map_type == BPF_MAP_TYPE_STACK) {
 		err = map->ops->map_pop_elem(map, value);
+	} else if (map->map_type == BPF_MAP_TYPE_HASH) {
+		if (!bpf_map_is_dev_bound(map)) {
+			bpf_disable_instrumentation();
+			rcu_read_lock();
+			err = map->ops->map_lookup_and_delete_elem(map, key, value);
+			rcu_read_unlock();
+			bpf_enable_instrumentation();
+			maybe_wait_bpf_programs(map);
+		}
 	} else {
 		err = -ENOTSUPP;
 	}
tools/testing/selftests/bpf/test_maps.c +7 -0
index 51adc42b2b40..3e1900e46e1d 100644
@@ -65,6 +65,13 @@ static void test_hashmap(unsigned int task, void *data)
 	assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
 
 	key = 2;
+	value = 1234;
+	/* Insert key=2 element. */
+	assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
+
+	/* Check that key=2 matches the value and delete it */
+	assert(bpf_map_lookup_and_delete_elem(fd, &key, &value) == 0 && value == 1234);
+
 	/* Check that key=2 is not found. */
 	assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
 
Signature · 1 line
2.26.2
#1 lore raw
Quoted 8 lines · Denis Salopek

Extend the existing bpf_map_lookup_and_delete_elem() functionality to hashtab maps, in addition to stacks and queues.
Create a new hashtab bpf_map_ops function that does lookup and deletion of the element under the same bucket lock and add the created map_ops to bpf.h.
Add the appropriate test case to 'maps' selftests.

Quoted 5 lines
Cc Juraj Vijtiuk <[email protected]>
Cc Luka Oreskovic <[email protected]>
Cc Luka Perkov <[email protected]>

---

I think this patch somehow got lost, even though it seems like a good addition. I'd recommend rebasing and re-submitting to let people take a fresh look at this.

It would also be nice to have a test_progs test added, not just test_maps. I'd also look at supporting lookup_and_delete for other kinds of hash maps (LRU, per-CPU), so that the support is more complete. Thanks!

Quoted 6 lines
 include/linux/bpf.h                     |  1 +
 kernel/bpf/hashtab.c                    | 38 +++++++++++++++++++++++++
 kernel/bpf/syscall.c                    |  9 ++++++
 tools/testing/selftests/bpf/test_maps.c |  7 +++++
 4 files changed, 55 insertions(+)

[...]

#5 lore raw
In reply to a message not in the archive
Quoted 102 lines · Daniel Borkmann
Quoted 11 lines · Denis Salopek

Extend the existing bpf_map_lookup_and_delete_elem() functionality to hashtab maps, in addition to stacks and queues.
Create a new hashtab bpf_map_ops function that does lookup and deletion of the element under the same bucket lock and add the created map_ops to bpf.h.
Add the appropriate test case to 'maps' selftests.

Cc Juraj Vijtiuk <[email protected]>
Cc Luka Oreskovic <[email protected]>
Cc Luka Perkov <[email protected]>

This is already possible in a more generic form, that is, via bpf(2) cmd BPF_MAP_LOOKUP_AND_DELETE_BATCH which is implemented by the different htab map flavors which also take the bucket lock. Did you have a try at that?

Quoted 85 lines
---
  include/linux/bpf.h                     |  1 +
  kernel/bpf/hashtab.c                    | 38 +++++++++++++++++++++++++
  kernel/bpf/syscall.c                    |  9 ++++++
  tools/testing/selftests/bpf/test_maps.c |  7 +++++
  4 files changed, 55 insertions(+)
include/linux/bpf.h +1 -0
index 1aac2af12fed..003c1505f0e3 100644
@@ -77,6 +77,7 @@ struct bpf_map_ops {
  	/* funcs callable from userspace and from eBPF programs */
  	void *(*map_lookup_elem)(struct bpf_map *map, void *key);
+	int (*map_lookup_and_delete_elem)(struct bpf_map *map, void *key, void *value);
  	int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
  	int (*map_delete_elem)(struct bpf_map *map, void *key);
  	int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
kernel/bpf/hashtab.c +38 -0
index c1ac7f964bc9..8d8463e0ea34 100644
@@ -973,6 +973,43 @@ static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
  	return 0;
  }
+/* Called from syscall or from eBPF program */
+static int htab_map_lookup_and_delete_elem(struct bpf_map *map, void *key, void *value)
+{
+	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
+	struct hlist_nulls_head *head;
+	struct bucket *b;
+	struct htab_elem *l;
+	unsigned long flags;
+	u32 hash, key_size;
+	int ret;
+
+	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held());
+
+	key_size = map->key_size;
+
+	hash = htab_map_hash(key, key_size, htab->hashrnd);
+	b = __select_bucket(htab, hash);
+	head = &b->head;
+
+	ret = htab_lock_bucket(htab, b, hash, &flags);
+	if (ret)
+		return ret;
+
+	l = lookup_elem_raw(head, hash, key, key_size);
+
+	if (l) {
+		copy_map_value(map, value, l->key + round_up(key_size, 8));
+		hlist_nulls_del_rcu(&l->hash_node);
+		free_htab_elem(htab, l);
+	} else {
+		ret = -ENOENT;
+	}
+
+	htab_unlock_bucket(htab, b, hash, flags);
+	return ret;
+}
+
  /* Called from syscall or from eBPF program */
  static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
  				u64 map_flags)
@@ -1877,6 +1914,7 @@ const struct bpf_map_ops htab_map_ops = {
  	.map_free = htab_map_free,
  	.map_get_next_key = htab_map_get_next_key,
  	.map_lookup_elem = htab_map_lookup_elem,
+	.map_lookup_and_delete_elem = htab_map_lookup_and_delete_elem,
  	.map_update_elem = htab_map_update_elem,
  	.map_delete_elem = htab_map_delete_elem,
  	.map_gen_lookup = htab_map_gen_lookup,
kernel/bpf/syscall.c +4 -0
index e5999d86c76e..4ff45c8d1077 100644
@@ -1505,6 +1505,15 @@ static int map_lookup_and_delete_elem(union bpf_attr *attr)
  	if (map->map_type == BPF_MAP_TYPE_QUEUE ||
  	    map->map_type == BPF_MAP_TYPE_STACK) {
  		err = map->ops->map_pop_elem(map, value);
+	} else if (map->map_type == BPF_MAP_TYPE_HASH) {
+		if (!bpf_map_is_dev_bound(map)) {
+			bpf_disable_instrumentation();
+			rcu_read_lock();
Quoted 8 lines

kernel/bpf/syscall.c static int map_lookup_and_delete_elem(union bpf_attr *attr) · line 1519

Quoted 8 lines
kernel/bpf/syscall.c +5 -0
+			err = map->ops->map_lookup_and_delete_elem(map, key, value);
+			rcu_read_unlock();
+			bpf_enable_instrumentation();
+			maybe_wait_bpf_programs(map);
+		}
  	} else {
  		err = -ENOTSUPP;
  	}

Hi,

Is there something wrong with adding hashmap functionality to the map_lookup_and_delete_elem() function? As per commit bd513cd08f10cbe28856f99ae951e86e86803861, adding this functionality for other map types was the plan at the time this function was implemented, so wouldn't this addition be useful? Otherwise, this function would only be used for stack/queue popping. So why does it even exist in the first place, if the _batch function can be used instead?

I understand the _batch function can do the same job, but is there a reason why using it would be a better option? There is also a performance impact in the _batch function when compared to my function - not too big, but still worth the mention. In my benchmarks, the _batch function takes 15% longer for lookup and deletion of one element.

Denis

maintainer
#6 lore raw
Quoted 122 lines · Denis Salopek
Quoted 102 lines · Daniel Borkmann
Quoted 11 lines · Denis Salopek

Extend the existing bpf_map_lookup_and_delete_elem() functionality to hashtab maps, in addition to stacks and queues.
Create a new hashtab bpf_map_ops function that does lookup and deletion of the element under the same bucket lock and add the created map_ops to bpf.h.
Add the appropriate test case to 'maps' selftests.

Cc Juraj Vijtiuk <[email protected]>
Cc Luka Oreskovic <[email protected]>
Cc Luka Perkov <[email protected]>

This is already possible in a more generic form, that is, via bpf(2) cmd BPF_MAP_LOOKUP_AND_DELETE_BATCH which is implemented by the different htab map flavors which also take the bucket lock. Did you have a try at that?

Quoted 85 lines
---
   include/linux/bpf.h                     |  1 +
   kernel/bpf/hashtab.c                    | 38 +++++++++++++++++++++++++
   kernel/bpf/syscall.c                    |  9 ++++++
   tools/testing/selftests/bpf/test_maps.c |  7 +++++
   4 files changed, 55 insertions(+)
include/linux/bpf.h +1 -0
index 1aac2af12fed..003c1505f0e3 100644
@@ -77,6 +77,7 @@ struct bpf_map_ops {
   	/* funcs callable from userspace and from eBPF programs */
   	void *(*map_lookup_elem)(struct bpf_map *map, void *key);
+	int (*map_lookup_and_delete_elem)(struct bpf_map *map, void *key, void *value);
   	int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
   	int (*map_delete_elem)(struct bpf_map *map, void *key);
   	int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
kernel/bpf/hashtab.c +38 -0
index c1ac7f964bc9..8d8463e0ea34 100644
@@ -973,6 +973,43 @@ static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
   	return 0;
   }
+/* Called from syscall or from eBPF program */
+static int htab_map_lookup_and_delete_elem(struct bpf_map *map, void *key, void *value)
+{
+	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
+	struct hlist_nulls_head *head;
+	struct bucket *b;
+	struct htab_elem *l;
+	unsigned long flags;
+	u32 hash, key_size;
+	int ret;
+
+	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held());
+
+	key_size = map->key_size;
+
+	hash = htab_map_hash(key, key_size, htab->hashrnd);
+	b = __select_bucket(htab, hash);
+	head = &b->head;
+
+	ret = htab_lock_bucket(htab, b, hash, &flags);
+	if (ret)
+		return ret;
+
+	l = lookup_elem_raw(head, hash, key, key_size);
+
+	if (l) {
+		copy_map_value(map, value, l->key + round_up(key_size, 8));
+		hlist_nulls_del_rcu(&l->hash_node);
+		free_htab_elem(htab, l);
+	} else {
+		ret = -ENOENT;
+	}
+
+	htab_unlock_bucket(htab, b, hash, flags);
+	return ret;
+}
+
   /* Called from syscall or from eBPF program */
   static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
   				u64 map_flags)
@@ -1877,6 +1914,7 @@ const struct bpf_map_ops htab_map_ops = {
   	.map_free = htab_map_free,
   	.map_get_next_key = htab_map_get_next_key,
   	.map_lookup_elem = htab_map_lookup_elem,
+	.map_lookup_and_delete_elem = htab_map_lookup_and_delete_elem,
   	.map_update_elem = htab_map_update_elem,
   	.map_delete_elem = htab_map_delete_elem,
   	.map_gen_lookup = htab_map_gen_lookup,
kernel/bpf/syscall.c +4 -0
index e5999d86c76e..4ff45c8d1077 100644
@@ -1505,6 +1505,15 @@ static int map_lookup_and_delete_elem(union bpf_attr *attr)
   	if (map->map_type == BPF_MAP_TYPE_QUEUE ||
   	    map->map_type == BPF_MAP_TYPE_STACK) {
   		err = map->ops->map_pop_elem(map, value);
+	} else if (map->map_type == BPF_MAP_TYPE_HASH) {
+		if (!bpf_map_is_dev_bound(map)) {
+			bpf_disable_instrumentation();
+			rcu_read_lock();
Quoted 8 lines

kernel/bpf/syscall.c static int map_lookup_and_delete_elem(union bpf_attr *attr) · line 1519

Quoted 8 lines
kernel/bpf/syscall.c +5 -0
+			err = map->ops->map_lookup_and_delete_elem(map, key, value);
+			rcu_read_unlock();
+			bpf_enable_instrumentation();
+			maybe_wait_bpf_programs(map);
+		}
   	} else {
   		err = -ENOTSUPP;
   	}

Hi,

Is there something wrong with adding hashmap functionality to the map_lookup_and_delete_elem() function? As per commit bd513cd08f10cbe28856f99ae951e86e86803861, adding this functionality for other map types was the plan at the time this function was implemented, so wouldn't this addition be useful? Otherwise, this function would only be used for stack/queue popping. So why does it even exist in the first place, if the _batch function can be used instead?

Quoted 5 lines

I understand the _batch function can do the same job, but is there a reason why using it would be a better option? There is also a performance impact in the _batch function when compared to my function - not too big, but still worth the mention. In my benchmarks, the _batch function takes 15% longer for lookup and deletion of one element.

Thanks for the benchmark, out of curiosity do you happen to have numbers at what point it pays off (2/3/more elems)? I don't think anything speaks against single lookup + delete functionality given the cmd API is already set in stone and it might be easier to use (though libbpf could also hide this behind api), but it would be nice if we could ideally reuse the batch lookup + delete code (e.g. bpf_map_do_batch()) in order to get this transparently for free for the various other map types as well.

Thanks,
Daniel

#2 lore raw
In reply to Andrii Nakryiko
Quoted 15 lines · Andrii Nakryiko
Quoted 8 lines · Denis Salopek

Extend the existing bpf_map_lookup_and_delete_elem() functionality to hashtab maps, in addition to stacks and queues.
Create a new hashtab bpf_map_ops function that does lookup and deletion of the element under the same bucket lock and add the created map_ops to bpf.h.
Add the appropriate test case to 'maps' selftests.

Quoted 5 lines
Cc Juraj Vijtiuk <[email protected]>
Cc Luka Oreskovic <[email protected]>
Cc Luka Perkov <[email protected]>

---

Quoted 9 lines

I think this patch somehow got lost, even though it seems like a good addition. I'd recommend rebasing and re-submitting to let people take a fresh look at this.

It would also be nice to have a test_progs test added, not just test_maps. I'd also look at supporting lookup_and_delete for other kinds of hash maps (LRU, per-CPU), so that the support is more complete. Thanks!

Hi Andrii,

I'll also implement the LRU and per-CPU ones and resubmit. I don't quite understand the test_progs, what kind of test(s) exactly should I add there?

Denis

Quoted 8 lines
Quoted 6 lines
 include/linux/bpf.h                     |  1 +
 kernel/bpf/hashtab.c                    | 38 +++++++++++++++++++++++++
 kernel/bpf/syscall.c                    |  9 ++++++
 tools/testing/selftests/bpf/test_maps.c |  7 +++++
 4 files changed, 55 insertions(+)

[...]

#7 lore raw
In reply to Daniel Borkmann
Quoted 137 lines · Daniel Borkmann
Quoted 122 lines · Denis Salopek
Quoted 102 lines · Daniel Borkmann
Quoted 11 lines · Denis Salopek

Extend the existing bpf_map_lookup_and_delete_elem() functionality to hashtab maps, in addition to stacks and queues.
Create a new hashtab bpf_map_ops function that does lookup and deletion of the element under the same bucket lock and add the created map_ops to bpf.h.
Add the appropriate test case to 'maps' selftests.

Cc Juraj Vijtiuk <[email protected]>
Cc Luka Oreskovic <[email protected]>
Cc Luka Perkov <[email protected]>

This is already possible in a more generic form, that is, via bpf(2) cmd BPF_MAP_LOOKUP_AND_DELETE_BATCH which is implemented by the different htab map flavors which also take the bucket lock. Did you have a try at that?

Quoted 85 lines
---
   include/linux/bpf.h                     |  1 +
   kernel/bpf/hashtab.c                    | 38 +++++++++++++++++++++++++
   kernel/bpf/syscall.c                    |  9 ++++++
   tools/testing/selftests/bpf/test_maps.c |  7 +++++
   4 files changed, 55 insertions(+)
include/linux/bpf.h +1 -0
index 1aac2af12fed..003c1505f0e3 100644
@@ -77,6 +77,7 @@ struct bpf_map_ops {
   	/* funcs callable from userspace and from eBPF programs */
   	void *(*map_lookup_elem)(struct bpf_map *map, void *key);
+	int (*map_lookup_and_delete_elem)(struct bpf_map *map, void *key, void *value);
   	int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
   	int (*map_delete_elem)(struct bpf_map *map, void *key);
   	int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
kernel/bpf/hashtab.c +38 -0
index c1ac7f964bc9..8d8463e0ea34 100644
@@ -973,6 +973,43 @@ static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
   	return 0;
   }
+/* Called from syscall or from eBPF program */
+static int htab_map_lookup_and_delete_elem(struct bpf_map *map, void *key, void *value)
+{
+	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
+	struct hlist_nulls_head *head;
+	struct bucket *b;
+	struct htab_elem *l;
+	unsigned long flags;
+	u32 hash, key_size;
+	int ret;
+
+	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held());
+
+	key_size = map->key_size;
+
+	hash = htab_map_hash(key, key_size, htab->hashrnd);
+	b = __select_bucket(htab, hash);
+	head = &b->head;
+
+	ret = htab_lock_bucket(htab, b, hash, &flags);
+	if (ret)
+		return ret;
+
+	l = lookup_elem_raw(head, hash, key, key_size);
+
+	if (l) {
+		copy_map_value(map, value, l->key + round_up(key_size, 8));
+		hlist_nulls_del_rcu(&l->hash_node);
+		free_htab_elem(htab, l);
+	} else {
+		ret = -ENOENT;
+	}
+
+	htab_unlock_bucket(htab, b, hash, flags);
+	return ret;
+}
+
   /* Called from syscall or from eBPF program */
   static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
   				u64 map_flags)
@@ -1877,6 +1914,7 @@ const struct bpf_map_ops htab_map_ops = {
   	.map_free = htab_map_free,
   	.map_get_next_key = htab_map_get_next_key,
   	.map_lookup_elem = htab_map_lookup_elem,
+	.map_lookup_and_delete_elem = htab_map_lookup_and_delete_elem,
   	.map_update_elem = htab_map_update_elem,
   	.map_delete_elem = htab_map_delete_elem,
   	.map_gen_lookup = htab_map_gen_lookup,
kernel/bpf/syscall.c +4 -0
index e5999d86c76e..4ff45c8d1077 100644
@@ -1505,6 +1505,15 @@ static int map_lookup_and_delete_elem(union bpf_attr *attr)
   	if (map->map_type == BPF_MAP_TYPE_QUEUE ||
   	    map->map_type == BPF_MAP_TYPE_STACK) {
   		err = map->ops->map_pop_elem(map, value);
+	} else if (map->map_type == BPF_MAP_TYPE_HASH) {
+		if (!bpf_map_is_dev_bound(map)) {
+			bpf_disable_instrumentation();
+			rcu_read_lock();
Quoted 8 lines

kernel/bpf/syscall.c static int map_lookup_and_delete_elem(union bpf_attr *attr) · line 1519

Quoted 8 lines
kernel/bpf/syscall.c +5 -0
+			err = map->ops->map_lookup_and_delete_elem(map, key, value);
+			rcu_read_unlock();
+			bpf_enable_instrumentation();
+			maybe_wait_bpf_programs(map);
+		}
   	} else {
   		err = -ENOTSUPP;
   	}

Hi,

Is there something wrong with adding hashmap functionality to the map_lookup_and_delete_elem() function? As per commit bd513cd08f10cbe28856f99ae951e86e86803861, adding this functionality for other map types was the plan at the time this function was implemented, so wouldn't this addition be useful? Otherwise, this function would only be used for stack/queue popping. So why does it even exist in the first place, if the _batch function can be used instead?

Quoted 5 lines

I understand the _batch function can do the same job, but is there a reason why using it would be a better option? There is also a performance impact in the _batch function when compared to my function - not too big, but still worth the mention. In my benchmarks, the _batch function takes 15% longer for lookup and deletion of one element.

Thanks for the benchmark, out of curiosity do you happen to have numbers at what point it pays off (2/3/more elems)? I don't think anything speaks against single lookup + delete functionality given the cmd API is already set in stone and it might be easier to use (though libbpf could also hide this behind api), but it would be nice if we could ideally reuse the batch lookup + delete code (e.g. bpf_map_do_batch()) in order to get this transparently for free for the various other map types as well.

Quoted 2 lines

Thanks,
Daniel

Hi Daniel,

I made a couple of benchmarks with different batch sizes and noticed that the _batch function performance depends on the size of the map we're working on - the number of elements it "pays off" depends on the max entries of the map.
For a map with 10 elements, 100,000 L&Ds of 1 element takes:
batch : 0.055s
nobatch : 0.050s
Same map, 100,000 L&Ds of 3 elements, batch is faster:
batch : 0.111s
nobatch : 0.138s

A map with 1,000 elements, 100,000 L&Ds of 1 element takes:
batch : 0.149s
nobatch : 0.050s
Same map, 100,000 L&Ds of 9 elements, batch gets faster:
batch : 0.316s
nobatch : 0.418s

A map with 10,000 elements, 100,000 L&Ds of 1 element takes:
batch : 1.690s
nobatch : 0.049s
Same map, 100,000 L&Ds of 125 elements, batch gets faster:
batch : 5.032s
nobatch : 5.041s

My benchmark is simple: I load an XDP program with a hashmap, and run some dummy "statistics" program L&D-ing the same map in a loop and updating it with some elements so it's never empty, i.e. the L&D never returns ENOENT.

I'm not sure how "realistic" usecase this is, or are there any cache hits/misses influencing the results, so I don't know if this is the best benchmark.

Denis

#3 lore raw
In reply to Denis Salopek
Quoted 27 lines · Denis Salopek
Quoted 15 lines · Andrii Nakryiko
Quoted 8 lines · Denis Salopek

Extend the existing bpf_map_lookup_and_delete_elem() functionality to hashtab maps, in addition to stacks and queues.
Create a new hashtab bpf_map_ops function that does lookup and deletion of the element under the same bucket lock and add the created map_ops to bpf.h.
Add the appropriate test case to 'maps' selftests.

Quoted 5 lines
Cc Juraj Vijtiuk <[email protected]>
Cc Luka Oreskovic <[email protected]>
Cc Luka Perkov <[email protected]>

---

Quoted 9 lines

I think this patch somehow got lost, even though it seems like a good addition. I'd recommend rebasing and re-submitting to let people take a fresh look at this.

It would also be nice to have a test_progs test added, not just test_maps. I'd also look at supporting lookup_and_delete for other kinds of hash maps (LRU, per-CPU), so that the support is more complete. Thanks!

Quoted 4 lines

Hi Andrii,

I'll also implement the LRU and per-CPU ones and resubmit. I don't quite understand the test_progs, what kind of test(s) exactly should I add there?

test_progs is our preferred test runner. It's a collection of independent tests (sometimes including subtests). See any of the recently added tests to get a feel for it. test_progs' tests resemble real-world applications much closer than test_verifier or test_maps tests, so it serves both as a readable test and as an API demo.

Quoted 11 lines

Denis

Quoted 8 lines
Quoted 6 lines
 include/linux/bpf.h                     |  1 +
 kernel/bpf/hashtab.c                    | 38 +++++++++++++++++++++++++
 kernel/bpf/syscall.c                    |  9 ++++++
 tools/testing/selftests/bpf/test_maps.c |  7 +++++
 4 files changed, 55 insertions(+)

[...]