ibv_alloc_buf(3)
okay so this is ibv_alloc_buf(3) from the Libibverbs Programmerβs Manual β the page for allocating provider-aware buffers and registering them as memory regions. Buckle up, bestie. π
πΈ NAME πΈ
ibv_alloc_buf, ibv_free_buf, ibv_reg_buf_mr, ibv_export_buf_dmabuf_fd β allocate provider-aware buffers and register them as memory regions. The whole squad is here, and yes they are iconic. π
β¨ SYNOPSIS β¨
okay, hereβs the literal include and the function signatures β keep these exactly as written, iykyk:
#include <infiniband/verbs.h>
void *ibv_alloc_buf(struct ibv_pd *pd, size_t size, struct ibv_buf **buf);
void ibv_free_buf(struct ibv_buf *buf);
struct ibv_mr *ibv_reg_buf_mr(struct ibv_pd *pd, struct ibv_buf *buf, void *addr,
size_t length, int access);
int ibv_export_buf_dmabuf_fd(struct ibv_buf *buf);
π¦ DESCRIPTION π¦
ibv_alloc_buf() allocates a buffer using the allocation method selected by the provider for the protection domain pd. On success it returns the mapped address and stores an opaque buffer handle in buf. That handle is used by ibv_free_buf(), ibv_reg_buf_mr(), ibv_reg_mr_ex() with IBV_REG_MR_MASK_BUF, and ibv_export_buf_dmabuf_fd(); it must not be interpreted by applications. Like, do not poke the mystery handle, bestie β itβs not for you. π
ibv_free_buf() releases a buffer handle returned by ibv_alloc_buf(). The protection domain used for allocation must remain valid until the buffer is freed. Periodt.
ibv_reg_buf_mr() registers a memory region for a buffer returned by ibv_alloc_buf(). Applications can register the same buffer through ibv_reg_mr_ex() by setting both IBV_REG_MR_MASK_BUF and IBV_REG_MR_MASK_ADDR, passing the buffer handle in mr_init_attr->buf and the address to register in mr_init_attr->addr.
bestie warning: whenIBV_REG_MR_MASK_BUFis set, the caller must not setIBV_REG_MR_MASK_FDorIBV_REG_MR_MASK_FD_OFFSET, and, for a DMA-buf backed buffer, must not setIBV_REG_MR_MASK_IOVAβlibibverbsderives these from the buffer handle and otherwise fails withEINVAL. no cap, thatβs the rule. π
The pd argument must be the same protection domain that was used to allocate the buffer. If a different protection domain is supplied, registration fails with EINVAL. For ordinary memory it behaves like ibv_reg_mr(). For provider allocations backed by a DMA-buf, it registers the corresponding DMA-buf range using the metadata stored in the opaque buf handle. The way that works is lowkey elegant. β¨
ibv_export_buf_dmabuf_fd() exports a new file descriptor for a buffer that is backed by a DMA-buf. This allows applications to pass provider-allocated memory to other APIs that import DMA-buf file descriptors. The returned file descriptor is owned by the caller and should be closed with close(2) when no longer needed. Calling ibv_export_buf_dmabuf_fd() does not transfer ownership of the buffer handle or the internal descriptor used by libibverbs. So close your fd, but donβt get it twisted about ownership. π«Ά
π ARGUMENTS π
pdβ Foribv_alloc_buf(), the protection domain (or parent domain) to allocate from; its provider selects the bufferβs backing allocation method. It must remain valid until the buffer is freed withibv_free_buf(). Foribv_reg_buf_mr(), the same protection domain that allocatedbuf(otherwise registration fails withEINVAL).sizeβ Size of the buffer to allocate, in bytes (ibv_alloc_buf()).bufβ Foribv_alloc_buf(), an output parameter set on success to an opaque buffer handle. Foribv_free_buf()andibv_reg_buf_mr(), the buffer handle returned byibv_alloc_buf()to be released or registered, respectively. Foribv_export_buf_dmabuf_fd(), the buffer handle whose DMA-buf fd should be exported.addrβ The start address to register (ibv_reg_buf_mr()): the buffer base returned byibv_alloc_buf()or an address within that buffer.lengthβ Length in bytes to register (ibv_reg_buf_mr());addr + lengthmust stay within the buffer.accessβ Access flags for the memory region (ibv_reg_buf_mr()), the same as foribv_reg_mr().
π RETURN VALUE π
ibv_alloc_buf()returns the mapped buffer address on success, orNULLif the request fails.ibv_reg_buf_mr()returns a pointer to the registered MR on success, orNULLif the request fails.ibv_free_buf()does not return a value.ibv_export_buf_dmabuf_fd()returns a file descriptor>= 0on success, or-1if the request fails witherrnoset. Ifbufis not backed by a DMA-buf,errnois set toENODATA.
π NOTES π
Applications running in a CoCo guest that need unprotected/shared memory should first create a parent domain with IBV_PARENT_DOMAIN_INIT_ATTR_ALLOW_CC_UNPROTECTED_ALLOC, then use that parent domain as the pd argument to ibv_alloc_buf() and ibv_reg_buf_mr() (or ibv_reg_mr_ex() with IBV_REG_MR_MASK_BUF). Thatβs the whole glow-up path, bestie. π§
β SEE ALSO β
ibv_alloc_parent_domain(3)ibv_reg_mr(3)ibv_reg_mr_ex(3)ibv_reg_dmabuf_mr(3)
xoxo, source/version: libibverbs, date: 2026-05-29 β go be iconic with your buffers π
β¨
π¦ where this page comes from
- arch rolling: rdma-core 65.0-1 Β· GPL-2.0-or-later OR LicenseRef-OpenIB.org-BSD-MIT AND GPL-2.0-or-later OR LicenseRef-OpenIB.org-BSD-FreeBSD AND GPL-2.0-or-later OR LicenseRef-Intel-BSD-3-Clause AND GPL-2.0-or-later OR LicenseRef-PathScale-BSD-Patent-License AND CC0-1.0 AND MIT AND BSD-2-Clause AND CPL-1.0 OR BSD-2-Clause OR GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms