bpf: Add bits iterator
authorYafang Shao <laoar.shao@gmail.com>
Fri, 17 May 2024 02:30:33 +0000 (10:30 +0800)
committerAndrii Nakryiko <andrii@kernel.org>
Wed, 29 May 2024 23:01:47 +0000 (16:01 -0700)
Add three new kfuncs for the bits iterator:
- bpf_iter_bits_new
  Initialize a new bits iterator for a given memory area. Due to the
  limitation of bpf memalloc, the max number of words (8-byte units) that
  can be iterated over is limited to (4096 / 8).
- bpf_iter_bits_next
  Get the next bit in a bpf_iter_bits
- bpf_iter_bits_destroy
  Destroy a bpf_iter_bits

The bits iterator facilitates the iteration of the bits of a memory area,
such as cpumask. It can be used in any context and on any address.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20240517023034.48138-2-laoar.shao@gmail.com
kernel/bpf/helpers.c

index 2a69a9a..6f1abcb 100644 (file)
@@ -2744,6 +2744,122 @@ __bpf_kfunc void bpf_preempt_enable(void)
        preempt_enable();
 }
 
+struct bpf_iter_bits {
+       __u64 __opaque[2];
+} __aligned(8);
+
+struct bpf_iter_bits_kern {
+       union {
+               unsigned long *bits;
+               unsigned long bits_copy;
+       };
+       u32 nr_bits;
+       int bit;
+} __aligned(8);
+
+/**
+ * bpf_iter_bits_new() - Initialize a new bits iterator for a given memory area
+ * @it: The new bpf_iter_bits to be created
+ * @unsafe_ptr__ign: A pointer pointing to a memory area to be iterated over
+ * @nr_words: The size of the specified memory area, measured in 8-byte units.
+ * Due to the limitation of memalloc, it can't be greater than 512.
+ *
+ * This function initializes a new bpf_iter_bits structure for iterating over
+ * a memory area which is specified by the @unsafe_ptr__ign and @nr_words. It
+ * copies the data of the memory area to the newly created bpf_iter_bits @it for
+ * subsequent iteration operations.
+ *
+ * On success, 0 is returned. On failure, ERR is returned.
+ */
+__bpf_kfunc int
+bpf_iter_bits_new(struct bpf_iter_bits *it, const u64 *unsafe_ptr__ign, u32 nr_words)
+{
+       struct bpf_iter_bits_kern *kit = (void *)it;
+       u32 nr_bytes = nr_words * sizeof(u64);
+       u32 nr_bits = BYTES_TO_BITS(nr_bytes);
+       int err;
+
+       BUILD_BUG_ON(sizeof(struct bpf_iter_bits_kern) != sizeof(struct bpf_iter_bits));
+       BUILD_BUG_ON(__alignof__(struct bpf_iter_bits_kern) !=
+                    __alignof__(struct bpf_iter_bits));
+
+       kit->nr_bits = 0;
+       kit->bits_copy = 0;
+       kit->bit = -1;
+
+       if (!unsafe_ptr__ign || !nr_words)
+               return -EINVAL;
+
+       /* Optimization for u64 mask */
+       if (nr_bits == 64) {
+               err = bpf_probe_read_kernel_common(&kit->bits_copy, nr_bytes, unsafe_ptr__ign);
+               if (err)
+                       return -EFAULT;
+
+               kit->nr_bits = nr_bits;
+               return 0;
+       }
+
+       /* Fallback to memalloc */
+       kit->bits = bpf_mem_alloc(&bpf_global_ma, nr_bytes);
+       if (!kit->bits)
+               return -ENOMEM;
+
+       err = bpf_probe_read_kernel_common(kit->bits, nr_bytes, unsafe_ptr__ign);
+       if (err) {
+               bpf_mem_free(&bpf_global_ma, kit->bits);
+               return err;
+       }
+
+       kit->nr_bits = nr_bits;
+       return 0;
+}
+
+/**
+ * bpf_iter_bits_next() - Get the next bit in a bpf_iter_bits
+ * @it: The bpf_iter_bits to be checked
+ *
+ * This function returns a pointer to a number representing the value of the
+ * next bit in the bits.
+ *
+ * If there are no further bits available, it returns NULL.
+ */
+__bpf_kfunc int *bpf_iter_bits_next(struct bpf_iter_bits *it)
+{
+       struct bpf_iter_bits_kern *kit = (void *)it;
+       u32 nr_bits = kit->nr_bits;
+       const unsigned long *bits;
+       int bit;
+
+       if (nr_bits == 0)
+               return NULL;
+
+       bits = nr_bits == 64 ? &kit->bits_copy : kit->bits;
+       bit = find_next_bit(bits, nr_bits, kit->bit + 1);
+       if (bit >= nr_bits) {
+               kit->nr_bits = 0;
+               return NULL;
+       }
+
+       kit->bit = bit;
+       return &kit->bit;
+}
+
+/**
+ * bpf_iter_bits_destroy() - Destroy a bpf_iter_bits
+ * @it: The bpf_iter_bits to be destroyed
+ *
+ * Destroy the resource associated with the bpf_iter_bits.
+ */
+__bpf_kfunc void bpf_iter_bits_destroy(struct bpf_iter_bits *it)
+{
+       struct bpf_iter_bits_kern *kit = (void *)it;
+
+       if (kit->nr_bits <= 64)
+               return;
+       bpf_mem_free(&bpf_global_ma, kit->bits);
+}
+
 __bpf_kfunc_end_defs();
 
 BTF_KFUNCS_START(generic_btf_ids)
@@ -2826,6 +2942,9 @@ BTF_ID_FLAGS(func, bpf_wq_set_callback_impl)
 BTF_ID_FLAGS(func, bpf_wq_start)
 BTF_ID_FLAGS(func, bpf_preempt_disable)
 BTF_ID_FLAGS(func, bpf_preempt_enable)
+BTF_ID_FLAGS(func, bpf_iter_bits_new, KF_ITER_NEW)
+BTF_ID_FLAGS(func, bpf_iter_bits_next, KF_ITER_NEXT | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_iter_bits_destroy, KF_ITER_DESTROY)
 BTF_KFUNCS_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {