Skip to content

KFunc bpf_iter_dmabuf_new

v6.16

This function initializes a iterator for DMA buffers.

Definition

Parameters

it: A pointer to a stack allocated struct bpf_iter_dmabuf that is used to iterate over DMA buffers.

Returns

0 on success, a negative error code on failure

Signature

int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it)

Note

This function may sleep, and therefore can only be used from sleepable programs.

Usage

Docs could be improved

This part of the docs is incomplete, contributions are very welcome

Program types

The following program types can make use of this kfunc:

Example

// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2025 Google LLC */
#include <vmlinux.h>
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_helpers.h>

/* From uapi/linux/dma-buf.h */
#define DMA_BUF_NAME_LEN 32

char _license[] SEC("license") = "GPL";

struct {
    __uint(type, BPF_MAP_TYPE_HASH);
    __uint(key_size, DMA_BUF_NAME_LEN);
    __type(value, bool);
    __uint(max_entries, 5);
} testbuf_hash SEC(".maps");

SEC("syscall")
int iter_dmabuf_for_each(const void *ctx)
{
    struct dma_buf *d;

    bpf_for_each(dmabuf, d) {
        char name[DMA_BUF_NAME_LEN];
        const char *pname;
        bool *found;
        long len;
        int i;

        if (bpf_core_read(&pname, sizeof(pname), &d->name))
            return 1;

        /* Buffers are not required to be named */
        if (!pname)
            continue;

        len = bpf_probe_read_kernel_str(name, sizeof(name), pname);
        if (len < 0)
            return 1;

        /*
         * The entire name buffer is used as a map key.
         * Zeroize any uninitialized trailing bytes after the NUL.
         */
        bpf_for(i, len, DMA_BUF_NAME_LEN)
            name[i] = 0;

        found = bpf_map_lookup_elem(&testbuf_hash, name);
        if (found) {
            bool t = true;

            bpf_map_update_elem(&testbuf_hash, name, &t, BPF_EXIST);
        }
    }

    return 0;
}