usb_alloc_noncoherent(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’– usb_alloc_noncoherent (Kernel API Manual, bestie!!) πŸ’–

okay so picture this: you're deep in the kernel, you need a DMA buffer that the hardware is not allowed to assume is cache-coherent, and you need the whole thing to be tracked properly so the USB core can do the sync dance for you. that's literally this function. it lives in the Kernel API Manual and it's a whole vibe πŸ’…


✨ SYNOPSIS ✨

void * usb_alloc_noncoherent (struct usb_device *dev , size_t size ,
       gfp_t mem_flags , dma_addr_t *dma , enum dma_data_direction dir , struct
       sg_table **table );

like, it hands you back a void * β€” either your shiny new buffer or null β€” and that's it, no cap 🫢


πŸŽ€ ARGUMENTS πŸŽ€

  • dev β€” the device this buffer is gonna be used with. yes, that one. πŸ‘‘
  • size β€” how big you want the buffer. the requested buffer size, that's the whole thing.
  • mem_flags β€” these affect whether the allocation is allowed to block. so like, choose wisely, bestie.
  • dma β€” you pass a pointer, and it gets filled in with the DMA address of the buffer. output parameter energy ✨
  • dir β€” the DMA transfer direction. which way the bytes are flowing, iykyk.
  • table β€” pointer-shaped, and it comes back holding the sg_table of the allocated memory. πŸ¦‹

🌸 DESCRIPTION 🌸

so here's the tea: to explicitly manage memory ownership for the kernel vs the device by USB core, you the user need to save that sg_table to urb->sgt. then USB core will do the DMA sync for CPU and device properly. that's the deal. that's how it works. periodt. πŸ’€

and when the buffer is no longer used?? free it with usb_free_noncoherent. do not be out here leaking, i'm begging 😭

⭐ RETURN ⭐

either null (which means no buffer could be allocated, sad, we've all been there) β€” or the cpu-space pointer to a buffer that may be used to perform DMA to the specified device. such cpu-space buffers are returned along with the DMA address (through the pointer provided). so you get both at once, iconic behaviour honestly πŸŽ€


πŸ’œ SEE ALSO πŸ’œ

okay the whole squad is here, kernel file ./drivers/usb/core/usb.c, plus:


and that's the page!! mwah πŸ’‹ this one's from ./drivers/usb/core, dated October 2026 β€” go forth and allocate noncoherently, bestie, you've got this 🌈🫢

πŸ“¦ where this page comes from