[RFC bpf-next 1/3] bpf: add BPF_F_COMPARE flag and compare fields to map elem UAPI
From: Gyutae Bae
Date: Mon Jun 22 2026 - 03:49:00 EST
From: Gyutae Bae <gyutae.bae@xxxxxxxxxxxxx>
Introduce the UAPI surface for a compare-and-delete primitive: a new map
flag BPF_F_COMPARE plus compare/compare_offset/compare_size fields let
a delete request name an expected value region. Behaviour is wired up in
the following patch; this adds the flag and fields (kernel + tools/ copy)
only.
Co-developed-by: Minsu Jeon <minsu.jeon@xxxxxxxxxxxxx>
Signed-off-by: Minsu Jeon <minsu.jeon@xxxxxxxxxxxxx>
Co-developed-by: Siwan Kim <siwan.kim@xxxxxxxxxxxxx>
Signed-off-by: Siwan Kim <siwan.kim@xxxxxxxxxxxxx>
Co-developed-by: Jonghyeon Kim <jong-hyeon.kim@xxxxxxxxxxxxx>
Signed-off-by: Jonghyeon Kim <jong-hyeon.kim@xxxxxxxxxxxxx>
Signed-off-by: Gyutae Bae <gyutae.bae@xxxxxxxxxxxxx>
---
include/uapi/linux/bpf.h | 6 +++++-
tools/include/uapi/linux/bpf.h | 6 +++++-
2 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 89b36de5fdbb..4705b02fb6a4 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -1397,7 +1397,7 @@ enum bpf_addr_space_cast {
BPF_ADDR_SPACE_CAST = 1,
};
-/* flags for BPF_MAP_UPDATE_ELEM command */
+/* flags for BPF_MAP_UPDATE_ELEM / BPF_MAP_DELETE_ELEM commands */
enum {
BPF_ANY = 0, /* create new element or update existing */
BPF_NOEXIST = 1, /* create new element if it didn't exist */
@@ -1405,6 +1405,7 @@ enum {
BPF_F_LOCK = 4, /* spin_lock-ed map_lookup/map_update */
BPF_F_CPU = 8, /* cpu flag for percpu maps, upper 32-bit of flags is a cpu number */
BPF_F_ALL_CPUS = 16, /* update value across all CPUs for percpu maps */
+ BPF_F_COMPARE = 32, /* compare-and-delete: delete elem only if value region == compare */
};
/* flags for BPF_MAP_CREATE command */
@@ -1586,6 +1587,9 @@ union bpf_attr {
__aligned_u64 next_key;
};
__u64 flags;
+ __aligned_u64 compare; /* user ptr to expected bytes (BPF_F_COMPARE) */
+ __u32 compare_offset; /* start offset within value */
+ __u32 compare_size; /* bytes to compare; 0 == whole value */
};
struct { /* struct used by BPF_MAP_*_BATCH commands */
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 89b36de5fdbb..4705b02fb6a4 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -1397,7 +1397,7 @@ enum bpf_addr_space_cast {
BPF_ADDR_SPACE_CAST = 1,
};
-/* flags for BPF_MAP_UPDATE_ELEM command */
+/* flags for BPF_MAP_UPDATE_ELEM / BPF_MAP_DELETE_ELEM commands */
enum {
BPF_ANY = 0, /* create new element or update existing */
BPF_NOEXIST = 1, /* create new element if it didn't exist */
@@ -1405,6 +1405,7 @@ enum {
BPF_F_LOCK = 4, /* spin_lock-ed map_lookup/map_update */
BPF_F_CPU = 8, /* cpu flag for percpu maps, upper 32-bit of flags is a cpu number */
BPF_F_ALL_CPUS = 16, /* update value across all CPUs for percpu maps */
+ BPF_F_COMPARE = 32, /* compare-and-delete: delete elem only if value region == compare */
};
/* flags for BPF_MAP_CREATE command */
@@ -1586,6 +1587,9 @@ union bpf_attr {
__aligned_u64 next_key;
};
__u64 flags;
+ __aligned_u64 compare; /* user ptr to expected bytes (BPF_F_COMPARE) */
+ __u32 compare_offset; /* start offset within value */
+ __u32 compare_size; /* bytes to compare; 0 == whole value */
};
struct { /* struct used by BPF_MAP_*_BATCH commands */
--
2.39.5 (Apple Git-154)