]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blobdiff - include/linux/bpf.h
bpf: introduce bpf_spin_lock
[mirror_ubuntu-hirsute-kernel.git] / include / linux / bpf.h
index 0394f1f9213bf2542eb1de2190420e5047da51c7..2ae615b48bb81a3b4fdfc2ca6eb6dbdaf97a0dd7 100644 (file)
@@ -72,14 +72,15 @@ struct bpf_map {
        u32 value_size;
        u32 max_entries;
        u32 map_flags;
-       u32 pages;
+       int spin_lock_off; /* >=0 valid offset, <0 error */
        u32 id;
        int numa_node;
        u32 btf_key_type_id;
        u32 btf_value_type_id;
        struct btf *btf;
+       u32 pages;
        bool unpriv_array;
-       /* 55 bytes hole */
+       /* 51 bytes hole */
 
        /* The 3rd and 4th cacheline with misc members to avoid false sharing
         * particularly with refcounting.
@@ -91,6 +92,34 @@ struct bpf_map {
        char name[BPF_OBJ_NAME_LEN];
 };
 
+static inline bool map_value_has_spin_lock(const struct bpf_map *map)
+{
+       return map->spin_lock_off >= 0;
+}
+
+static inline void check_and_init_map_lock(struct bpf_map *map, void *dst)
+{
+       if (likely(!map_value_has_spin_lock(map)))
+               return;
+       *(struct bpf_spin_lock *)(dst + map->spin_lock_off) =
+               (struct bpf_spin_lock){};
+}
+
+/* copy everything but bpf_spin_lock */
+static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
+{
+       if (unlikely(map_value_has_spin_lock(map))) {
+               u32 off = map->spin_lock_off;
+
+               memcpy(dst, src, off);
+               memcpy(dst + off + sizeof(struct bpf_spin_lock),
+                      src + off + sizeof(struct bpf_spin_lock),
+                      map->value_size - off - sizeof(struct bpf_spin_lock));
+       } else {
+               memcpy(dst, src, map->value_size);
+       }
+}
+
 struct bpf_offload_dev;
 struct bpf_offloaded_map;
 
@@ -162,6 +191,7 @@ enum bpf_arg_type {
        ARG_PTR_TO_CTX,         /* pointer to context */
        ARG_ANYTHING,           /* any (initialized) argument is ok */
        ARG_PTR_TO_SOCKET,      /* pointer to bpf_sock */
+       ARG_PTR_TO_SPIN_LOCK,   /* pointer to bpf_spin_lock */
 };
 
 /* type of values returned from helper functions */
@@ -879,7 +909,8 @@ extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
 extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
-
+extern const struct bpf_func_proto bpf_spin_lock_proto;
+extern const struct bpf_func_proto bpf_spin_unlock_proto;
 extern const struct bpf_func_proto bpf_get_local_storage_proto;
 
 /* Shared helpers among cBPF and eBPF. */