STRUCT VB2_MEM_OPS(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… struct vb2_mem_ops(9) β€” the Kernel API Manual, bestie edition πŸŽ€

okay so this is a page from the Kernel API Manual and it's about struct vb2_mem_ops, which is literally the memory handling / memory allocator operations vibe. no cap, it's the plug for how videobuf2 does memory. let's get into it ✨


🌸 SYNOPSIS 🌸

struct vb2_mem_ops {
    void *(*alloc)(struct vb2_buffer *vb, struct device *dev, unsigned
long size);
    void (*put)(void *buf_priv);
    struct dma_buf *(*get_dmabuf)(struct vb2_buffer *vb, void *buf_priv,
unsigned long flags);
    void *(*get_userptr)(struct vb2_buffer *vb, struct device *dev,
unsigned long vaddr, unsigned long size);
    void (*put_userptr)(void *buf_priv);
    void (*prepare)(void *buf_priv);
    void (*finish)(void *buf_priv);
    void *(*attach_dmabuf)(struct vb2_buffer *vb, struct device *dev,
struct dma_buf *dbuf, unsigned long size);
    void (*detach_dmabuf)(void *buf_priv);
    int (*map_dmabuf)(void *buf_priv);
    void (*unmap_dmabuf)(void *buf_priv);
    void *(*vaddr)(struct vb2_buffer *vb, void *buf_priv);
    void *(*cookie)(struct vb2_buffer *vb, void *buf_priv);
    unsigned int    (*num_users)(void *buf_priv);
    int (*mmap)(void *buf_priv, struct vm_area_struct *vma);
 };

like, that's the whole squad of function pointers right there πŸ‘‘


πŸ”₯ Members πŸ”₯

  • alloc β€” allocates video memory and, if you want, some allocator private data too πŸ’–. returns ERR_PTR on failure, or a pointer to the allocator's private, per-buffer data on success. and the way that returned private struct then gets passed around as the buf_priv argument to the other ops in this structure?? iconic. also heads up: the size argument to this function shall be page aligned. not "should probably be", shall be. bestie, page align it 🫢
  • put β€” tells the allocator the buffer is done being used, no longer needed. usually this ends with the allocator freeing the buffer (if no other users of this buffer are present). the buf_priv argument is that allocator private per-buffer structure that alloc handed you earlier. bye bye buffer πŸ’€
  • get_dmabuf β€” acquires userspace memory for a hardware operation. used for DMABUF memory types ✨
  • get_userptr β€” acquires userspace memory for a hardware operation, used for USERPTR memory types. vaddr is the address that gets passed to the videobuf2 layer when queueing a video buffer of USERPTR type. on success it should return an allocator private per-buffer structure associated with the buffer, and ERR_PTR on failure β€” and yes, that returned private structure then gets passed as the buf_priv argument to the other ops in this structure 😌
  • put_userptr β€” informs the allocator that a USERPTR buffer will no longer be used. that's it, that's the whole job πŸŽ€
  • prepare β€” called every time the buffer is passed from userspace to the driver. useful for cache synchronisation. it's optional, ngl πŸ™ˆ
  • finish β€” called every time the buffer is passed back from the driver to the userspace. also optional 🌸
  • attach_dmabuf β€” attaches a shared struct dma_buf for a hardware operation, used for DMABUF memory types. dev is the alloc device, dbuf is the shared dma_buf. returns ERR_PTR on failure, and the allocator private per-buffer structure on success. this needs to be used for further accesses to the buffer β€” like, don't skip it ✨
  • detach_dmabuf β€” informs the exporter of the buffer that the current DMABUF buffer is no longer used. the buf_priv argument is that allocator private per-buffer structure you got back from the attach_dmabuf callback πŸ¦‹
  • map_dmabuf β€” requests access to the dmabuf from the allocator. the allocator of the dmabuf gets informed that this driver is going to use the dmabuf πŸ‘‘
  • unmap_dmabuf β€” releases access control to the dmabuf, and the allocator is notified that this driver is done using the dmabuf for now. closing the tab, basically 😭
  • vaddr β€” returns a kernel virtual address to a given memory buffer associated with the passed private structure, or NULL if no such mapping exists πŸ’œ
  • cookie β€” returns an allocator specific cookie for a given memory buffer associated with the passed private structure, or NULL if it's not available πŸ“
  • num_users β€” returns the current number of users of a memory buffer; returns 1 if the videobuf2 layer (or actually the driver using it) is the only user ⭐
  • mmap β€” sets up a userspace mapping for a given memory buffer under the provided virtual memory region 🧚

✨ Description ✨

Those operations are used by the videobuf2 core to implement the memory handling / memory allocators for each type of supported streaming I/O method. so yeah, this struct is the main character of memory management πŸ’…

NOTE: required ops, by type β€” write these down bestie, fr fr πŸ“ - Required ops for USERPTR types: get_userptr, put_userptr. - Required ops for MMAP types: alloc, put, num_users, mmap. - Required ops for read/write access types: alloc, put, num_users, vaddr. - Required ops for DMABUF types: attach_dmabuf, detach_dmabuf, map_dmabuf, unmap_dmabuf.

πŸ¦‹ SEE ALSO πŸ¦‹

kernel file ./include/media/videobuf2-core.h, and the whole extended friend group:


and that's the tea β˜• this page lives in ./include/media, it's the October 2026 drop, and it's filed under struct vb2_mem_ops(9) β€” go read the whole flock of vb2_* pages, they ate πŸ’–βœ¨πŸ‘‘

πŸ“¦ where this page comes from