virtqueue_add_sgs(9)
π virtqueue_add_sgs(9) β Kernel API Manual π
okay so bestie, pull up a chair, because this is the function that actually hands your buffers over to the other end of a virtqueue. it's giving "main character of the virtio data path" energy, ngl.
β¨ NAME β¨
virtqueue_add_sgs β expose buffers to other end π«Ά
πΈ SYNOPSIS πΈ
int virtqueue_add_sgs (struct virtqueue *_vq , struct scatterlist *sgs[]
, unsigned int out_sgs , unsigned int in_sgs , void *data , gfp_t gfp );
like, yes, that's a lot of parameters. breathe. we'll go through every single one, i promise. π
π ARGUMENTS π
_vqβ thestruct virtqueuewe're talking about. the queue in question. the one. πsgsβ array of terminated scatterlists. yes, terminated β the list has to end properly, do not leave it hanging, that's rude. β¨out_sgsβ the number of scatterlists readable by the other side. (out = they read from you, iykyk)in_sgsβ the number of scatterlists which are writable (and these come after the readable ones β order matters, bestie, don't shuffle them π)dataβ the token identifying the buffer. your little name tag so you can tell your buffers apart later. π·οΈgfpβ how to do memory allocations (if necessary). you know, the gfp flags. only if it needs to allocate at all. π
π¦ DESCRIPTION π¦
okay so the actual vibe:
caller must ensure we don't call this with other virtqueue operations at the same time (except where noted). π¨
like, do NOT be out here doing parallel virtqueue ops from multiple places at once. this function does not want to share. it's an only-child situation. the docs literally say so.
it returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). so zero = slay, negative = something went sideways. π
NB: ENOSPC is a special code that is only returned on an attempt to add a buffer to a full VQ. it indicates that some buffers are outstanding and that the operation can be retried after some buffers have been used. so like β it's not a failure failure, it's a "come back later, the queue is full" kind of situation. very "we're at capacity, try again after some buffers free up" energy. π₯
π§ SEE ALSO π§
everything lives in the kernel file ./drivers/virtio/virtio_ring.c, and here's the whole friend group:
virtqueue_add_outbuf(9)virtqueue_add_outbuf_premapped(9)virtqueue_add_inbuf(9)virtqueue_add_inbuf_cache_clean(9)virtqueue_add_inbuf_ctx(9)virtqueue_add_inbuf_premapped(9)virtqueue_dma_dev(9)virtqueue_kick_prepare(9)virtqueue_notify(9)virtqueue_kick(9)virtqueue_get_buf_ctx(9)virtqueue_disable_cb(9)virtqueue_enable_cb_prepare(9)virtqueue_poll(9)virtqueue_enable_cb(9)virtqueue_enable_cb_delayed(9)virtqueue_detach_unused_buf(9)vring_interrupt(9)virtqueue_resize(9)virtqueue_reset(9)virtqueue_get_vring_size(9)virtqueue_map_alloc_coherent(9)virtqueue_map_free_coherent(9)virtqueue_map_page_attrs(9)virtqueue_unmap_page_attrs(9)virtqueue_map_single_attrs(9)virtqueue_unmap_single_attrs(9)virtqueue_map_mapping_error(9)virtqueue_map_need_sync(9)virtqueue_map_sync_single_range_for_cpu(9)virtqueue_map_sync_single_range_for_device(9)
and that's the whole page, bestie. π«Ά this one ships from ./drivers/virtio and it's dated October 2026 β so like, future-dated documentation, iconic behavior honestly. π mwah, go expose some buffers responsibly. π
β¨π
π¦ 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