Introduce flag for batch operations to continue batch deletes when missing keys are encountered. This allows to flush maps at once without the need to keep track of the keys in a map. Signed-off-by: Florian Lehner <dev@xxxxxxxxxxx> --- include/uapi/linux/bpf.h | 5 +++++ kernel/bpf/syscall.c | 14 +++++++++++--- tools/include/uapi/linux/bpf.h | 5 +++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 4162afc6b5d0..b38884cf6fe3 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1423,6 +1423,11 @@ enum { */ #define BPF_F_QUERY_EFFECTIVE (1U << 0) +/* Flags for batch operations. */ + +/* If set, continue with batch operation even if a key is missing. */ +#define BPF_F_BATCH_IGNORE_MISSING_KEY (1U << 1) + /* Flags for BPF_PROG_TEST_RUN */ /* If set, run the test on the cpu specified by bpf_attr.test.cpu */ diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 58190ca724a2..860d6dc0c6d9 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -1851,7 +1851,7 @@ int generic_map_delete_batch(struct bpf_map *map, union bpf_attr __user *uattr) { void __user *keys = u64_to_user_ptr(attr->batch.keys); - u32 cp, max_count; + u32 count, cp, max_count; int err = 0; void *key; @@ -1874,6 +1874,7 @@ int generic_map_delete_batch(struct bpf_map *map, if (!key) return -ENOMEM; + count = 0; for (cp = 0; cp < max_count; cp++) { err = -EFAULT; if (copy_from_user(key, keys + cp * map->key_size, @@ -1890,11 +1891,18 @@ int generic_map_delete_batch(struct bpf_map *map, err = map->ops->map_delete_elem(map, key); rcu_read_unlock(); bpf_enable_instrumentation(); - if (err) + if (err) { + if (err == -ENOENT && + (attr->batch.flags & BPF_F_BATCH_IGNORE_MISSING_KEY)) { + cond_resched(); + continue; + } break; + } cond_resched(); + count++; } - if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) + if (copy_to_user(&uattr->batch.count, &count, sizeof(count))) err = -EFAULT; kvfree(key); diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 4162afc6b5d0..ea03e7e8272b 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1423,6 +1423,11 @@ enum { */ #define BPF_F_QUERY_EFFECTIVE (1U << 0) +/* Flags for batch operations. */ + +/* If set, continue with batch operation even if a key is missing. */ +#define BPF_F_BATCH_IGNORE_MISSING_KEY (1U << 1) + /* Flags for BPF_PROG_TEST_RUN */ /* If set, run the test on the cpu specified by bpf_attr.test.cpu */ -- 2.47.0