Is it possible to call dma_unmap_single after dma_sync_single_for_cpu?

I have a situation where I need to call dma_unmap_single after dma_sync_single_for_cpu . Below is my script.

  • Allocate memory for buffer using kmalloc()
  • Map the buffer to the device using dma_map_single with the direction DMA_FROM_DEVICE .
  • After receiving the packet, obtain ownership of the buffer using dma_sync_single_for_cpu .
  • After some time, if some other function in the kernel uses the same buffer, cancel the buffer from the device using dma_unmap_single with the direction DMA_FROM_DEVICE .

Are all of the above steps possible?

+5
source share
1 answer

Buffer ownership (CPU or device) matters only to decide who is allowed access to the contents of the buffer.

dma_unmap_single() does not care about the content; it can be called in any state.

Note that after decoupling the DMA, the memory again β€œbelongs” to the CPU. If you perform only one DMA transfer, you do not need to call dma_sync_single_for_cpu() ; you can just read the buffer after calling dma_unmap_single() .

+1
source

Source: https://habr.com/ru/post/1238600/


All Articles