usb_alloc_coherent和kzalloc / kmalloc之间的区别

时间:2021-10-17 16:10:06

What is the fundamental difference between using usb_alloc_coherent and kzalloc/kmalloc in context of USB driver. Both does the same, allocate a memory area for URB buffer. But what is the difference of them. Is there any benefit of using usb_alloc_coherent instead of kzalloc/kmalloc?

在USB驱动程序的上下文中使用usb_alloc_coherent和kzalloc / kmalloc之间的根本区别是什么。两者都是一样的,为URB缓冲区分配一个内存区域。但它们有什么不同。使用usb_alloc_coherent代替kzalloc / kmalloc有什么好处吗?

2 个解决方案

#1


Drivers are device (endpoint) centric but memory allocation must consult capabilities of the USB controller. This is because it is the controller which performs the DMA from memory onto the USB bus. So usb_alloc_coherent basically wraps the generic dma_alloc_coherent but calls it for the controller, not the endpoint. Using DMA-API instead of just kmalloc ensures that no bounce-buffers will be required.

驱动程序以设备(端点)为中心,但内存分配必须参考USB控制器的功能。这是因为它是从存储器到USB总线执行DMA的控制器。所以usb_alloc_coherent基本上包装了泛型dma_alloc_coherent但是为控制器而不是端点调用它。使用DMA-API而不仅仅是kmalloc可确保不需要反弹缓冲区。

This saves device driver writes from code ugliness (breaking abstractions) and handling of some corner cases. usb_alloc_coherent also uses a memory poll to speed things up a bit.

这样可以避免设备驱动程序写入代码丑陋(破坏抽象)和处理某些极端情况。 usb_alloc_coherent还使用内存轮询来加快速度。

#2


The documentation says:

文件说:

usb_alloc_coherent - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP

usb_alloc_coherent - 为URB_NO_xxx_DMA_MAP分配dma一致性缓冲区

These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags to avoid behaviors like using "DMA bounce buffers", or thrashing IOMMU hardware during URB completion/resubmit.

这些缓冲区与urb-> transfer_flags中设置的URB_NO_xxx_DMA_MAP一起使用,以避免在URB完成/重新提交期间使用“DMA跳出缓冲区”或颠簸IOMMU硬件等行为。

#1


Drivers are device (endpoint) centric but memory allocation must consult capabilities of the USB controller. This is because it is the controller which performs the DMA from memory onto the USB bus. So usb_alloc_coherent basically wraps the generic dma_alloc_coherent but calls it for the controller, not the endpoint. Using DMA-API instead of just kmalloc ensures that no bounce-buffers will be required.

驱动程序以设备(端点)为中心,但内存分配必须参考USB控制器的功能。这是因为它是从存储器到USB总线执行DMA的控制器。所以usb_alloc_coherent基本上包装了泛型dma_alloc_coherent但是为控制器而不是端点调用它。使用DMA-API而不仅仅是kmalloc可确保不需要反弹缓冲区。

This saves device driver writes from code ugliness (breaking abstractions) and handling of some corner cases. usb_alloc_coherent also uses a memory poll to speed things up a bit.

这样可以避免设备驱动程序写入代码丑陋(破坏抽象)和处理某些极端情况。 usb_alloc_coherent还使用内存轮询来加快速度。

#2


The documentation says:

文件说:

usb_alloc_coherent - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP

usb_alloc_coherent - 为URB_NO_xxx_DMA_MAP分配dma一致性缓冲区

These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags to avoid behaviors like using "DMA bounce buffers", or thrashing IOMMU hardware during URB completion/resubmit.

这些缓冲区与urb-> transfer_flags中设置的URB_NO_xxx_DMA_MAP一起使用,以避免在URB完成/重新提交期间使用“DMA跳出缓冲区”或颠簸IOMMU硬件等行为。