]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
bpf: add kfuncs and helpers support for file dynptrs
authorMykyta Yatsenko <yatsenko@meta.com>
Sun, 26 Oct 2025 20:38:50 +0000 (20:38 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 27 Oct 2025 16:56:27 +0000 (09:56 -0700)
Add support for file dynptr.

Introduce struct bpf_dynptr_file_impl to hold internal state for file
dynptrs, with 64-bit size and offset support.

Introduce lifecycle management kfuncs:
  - bpf_dynptr_from_file() for initialization
  - bpf_dynptr_file_discard() for destruction

Extend existing helpers to support file dynptrs in:
  - bpf_dynptr_read()
  - bpf_dynptr_slice()

Write helpers (bpf_dynptr_write() and bpf_dynptr_data()) are not
modified, as file dynptr is read-only.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20251026203853.135105-8-mykyta.yatsenko5@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/helpers.c

index bf65b7fb761f58569fba0f2e24835f91250d951e..99a7def0b978f78e212bb31884e8b198fe765f15 100644 (file)
@@ -28,6 +28,7 @@
 #include <linux/verification.h>
 #include <linux/task_work.h>
 #include <linux/irq_work.h>
+#include <linux/buildid.h>
 
 #include "../../lib/kstrtox.h"
 
@@ -1656,6 +1657,13 @@ static const struct bpf_func_proto bpf_kptr_xchg_proto = {
        .arg2_btf_id  = BPF_PTR_POISON,
 };
 
+struct bpf_dynptr_file_impl {
+       struct freader freader;
+       /* 64 bit offset and size overriding 32 bit ones in bpf_dynptr_kern */
+       u64 offset;
+       u64 size;
+};
+
 /* Since the upper 8 bits of dynptr->size is reserved, the
  * maximum supported size is 2^24 - 1.
  */
@@ -1686,13 +1694,36 @@ static enum bpf_dynptr_type bpf_dynptr_get_type(const struct bpf_dynptr_kern *pt
 
 u64 __bpf_dynptr_size(const struct bpf_dynptr_kern *ptr)
 {
+       if (bpf_dynptr_get_type(ptr) == BPF_DYNPTR_TYPE_FILE) {
+               struct bpf_dynptr_file_impl *df = ptr->data;
+
+               return df->size;
+       }
+
        return ptr->size & DYNPTR_SIZE_MASK;
 }
 
+static void bpf_dynptr_advance_offset(struct bpf_dynptr_kern *ptr, u64 off)
+{
+       if (bpf_dynptr_get_type(ptr) == BPF_DYNPTR_TYPE_FILE) {
+               struct bpf_dynptr_file_impl *df = ptr->data;
+
+               df->offset += off;
+               return;
+       }
+       ptr->offset += off;
+}
+
 static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u64 new_size)
 {
        u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK;
 
+       if (bpf_dynptr_get_type(ptr) == BPF_DYNPTR_TYPE_FILE) {
+               struct bpf_dynptr_file_impl *df = ptr->data;
+
+               df->size = new_size;
+               return;
+       }
        ptr->size = (u32)new_size | metadata;
 }
 
@@ -1701,6 +1732,25 @@ int bpf_dynptr_check_size(u64 size)
        return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
 }
 
+static int bpf_file_fetch_bytes(struct bpf_dynptr_file_impl *df, u64 offset, void *buf, u64 len)
+{
+       const void *ptr;
+
+       if (!buf)
+               return -EINVAL;
+
+       df->freader.buf = buf;
+       df->freader.buf_sz = len;
+       ptr = freader_fetch(&df->freader, offset + df->offset, len);
+       if (!ptr)
+               return df->freader.err;
+
+       if (ptr != buf) /* Force copying into the buffer */
+               memcpy(buf, ptr, len);
+
+       return 0;
+}
+
 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
                     enum bpf_dynptr_type type, u32 offset, u32 size)
 {
@@ -1781,6 +1831,8 @@ static int __bpf_dynptr_read(void *dst, u64 len, const struct bpf_dynptr_kern *s
        case BPF_DYNPTR_TYPE_SKB_META:
                memmove(dst, bpf_skb_meta_pointer(src->data, src->offset + offset), len);
                return 0;
+       case BPF_DYNPTR_TYPE_FILE:
+               return bpf_file_fetch_bytes(src->data, offset, dst, len);
        default:
                WARN_ONCE(true, "bpf_dynptr_read: unknown dynptr type %d\n", type);
                return -EFAULT;
@@ -2719,6 +2771,9 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr *p, u64 offset,
        }
        case BPF_DYNPTR_TYPE_SKB_META:
                return bpf_skb_meta_pointer(ptr->data, ptr->offset + offset);
+       case BPF_DYNPTR_TYPE_FILE:
+               err = bpf_file_fetch_bytes(ptr->data, offset, buffer__opt, buffer__szk);
+               return err ? NULL : buffer__opt;
        default:
                WARN_ONCE(true, "unknown dynptr type %d\n", type);
                return NULL;
@@ -2813,7 +2868,7 @@ __bpf_kfunc int bpf_dynptr_adjust(const struct bpf_dynptr *p, u64 start, u64 end
        if (start > size || end > size)
                return -ERANGE;
 
-       ptr->offset += start;
+       bpf_dynptr_advance_offset(ptr, start);
        bpf_dynptr_set_size(ptr, end - start);
 
        return 0;
@@ -4252,13 +4307,46 @@ __bpf_kfunc int bpf_task_work_schedule_resume(struct task_struct *task, struct b
        return bpf_task_work_schedule(task, tw, map__map, callback, aux__prog, TWA_RESUME);
 }
 
-__bpf_kfunc int bpf_dynptr_from_file(struct file *file, u32 flags, struct bpf_dynptr *ptr__uninit)
+static int make_file_dynptr(struct file *file, u32 flags, bool may_sleep,
+                           struct bpf_dynptr_kern *ptr)
 {
+       struct bpf_dynptr_file_impl *state;
+
+       /* flags is currently unsupported */
+       if (flags) {
+               bpf_dynptr_set_null(ptr);
+               return -EINVAL;
+       }
+
+       state = bpf_mem_alloc(&bpf_global_ma, sizeof(struct bpf_dynptr_file_impl));
+       if (!state) {
+               bpf_dynptr_set_null(ptr);
+               return -ENOMEM;
+       }
+       state->offset = 0;
+       state->size = U64_MAX; /* Don't restrict size, as file may change anyways */
+       freader_init_from_file(&state->freader, NULL, 0, file, may_sleep);
+       bpf_dynptr_init(ptr, state, BPF_DYNPTR_TYPE_FILE, 0, 0);
+       bpf_dynptr_set_rdonly(ptr);
        return 0;
 }
 
+__bpf_kfunc int bpf_dynptr_from_file(struct file *file, u32 flags, struct bpf_dynptr *ptr__uninit)
+{
+       return make_file_dynptr(file, flags, false, (struct bpf_dynptr_kern *)ptr__uninit);
+}
+
 __bpf_kfunc int bpf_dynptr_file_discard(struct bpf_dynptr *dynptr)
 {
+       struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)dynptr;
+       struct bpf_dynptr_file_impl *df = ptr->data;
+
+       if (!df)
+               return 0;
+
+       freader_cleanup(&df->freader);
+       bpf_mem_free(&bpf_global_ma, df);
+       bpf_dynptr_set_null(ptr);
        return 0;
 }