STRUCT VB2_MEM_OPS(9)
π 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 π. returnsERR_PTRon 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 thebuf_privargument to the other ops in this structure?? iconic. also heads up: thesizeargument 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). thebuf_privargument is that allocator private per-buffer structure thatallochanded 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.vaddris 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, andERR_PTRon failure β and yes, that returned private structure then gets passed as thebuf_privargument 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 sharedstruct dma_buffor a hardware operation, used for DMABUF memory types.devis the alloc device,dbufis the shareddma_buf. returnsERR_PTRon 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. thebuf_privargument is that allocator private per-buffer structure you got back from theattach_dmabufcallback π¦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, orNULLif no such mapping exists πcookieβ returns an allocator specific cookie for a given memory buffer associated with the passed private structure, orNULLif it's not available πnum_usersβ returns the current number of users of a memory buffer; returns1if 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:
enum vb2_memory(9),struct vb2_plane(9),enum vb2_io_modes(9),enum vb2_buffer_state(9)struct vb2_buffer(9),struct vb2_ops(9),struct vb2_buf_ops(9),struct vb2_queue(9)vb2_queue_allows_cache_hints(9),vb2_plane_vaddr(9),vb2_plane_cookie(9)vb2_buffer_done(9),vb2_discard_done(9),vb2_wait_for_all_buffers(9)vb2_core_querybuf(9),vb2_core_reqbufs(9),vb2_core_create_bufs(9),vb2_core_prepare_buf(9),vb2_core_remove_bufs(9),vb2_core_qbuf(9),vb2_core_dqbuf(9)vb2_core_streamon(9),vb2_core_streamoff(9),vb2_core_expbuf(9)vb2_core_queue_init(9),vb2_core_queue_release(9),vb2_queue_error(9)vb2_mmap(9),vb2_get_unmapped_area(9),vb2_core_poll(9)vb2_read(9),vb2_write(9)vb2_thread_fnc(9),vb2_thread_start(9),vb2_thread_stop(9)vb2_is_streaming(9),vb2_fileio_is_active(9),vb2_get_num_buffers(9),vb2_is_busy(9),vb2_get_drv_priv(9)vb2_set_plane_payload(9),vb2_get_plane_payload(9),vb2_plane_size(9)vb2_start_streaming_called(9),vb2_clear_last_buffer_dequeued(9),vb2_get_buffer(9)vb2_buffer_in_use(9),vb2_verify_memory_type(9),vb2_request_object_is_buffer(9),vb2_request_buffer_cnt(9)
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
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms