简体   繁体   English

Linux内核空间中的“魔环缓冲”实现?

[英]“Magic ring buffer” implementation in Linux kernel space?

I'm aware of the "magic ring buffer" trick that involves mirroring the underlying buffer in the process's address space to allow chunks of data to be enqueued with a single memcpy() without worrying about wraparound. 我知道“魔术环缓冲区”技巧涉及镜像进程地址空间中的底层缓冲区,以允许数据块与单个memcpy()排队,而不必担心环绕。

I'd like to accomplish the same thing but in a Linux kernel module. 我想在Linux内核模块中完成同样的事情。 Assume I have a buffer created with dma_alloc_coherent() , its virtual address is V and its length is N . 假设我有一个用dma_alloc_coherent()创建的缓冲区,它的虚拟地址是V ,它的长度是N How do I create the mapping such that its virtual addresses [V+N,V+2N) map to the same underlying pages as [V,V+N) ? 如何创建映射,使其虚拟地址[V+N,V+2N)映射到与[V,V+N)相同的基础页面?

Note: this is in 32-bit ARM Linux. 注意:这是在32位ARM Linux中。

drivers/firewire/ohci.c maps some pages of the Asynchronous Receive ring buffer twice to allow easier access to received packets that wrap around: drivers / firewire / ohci.c两次映射异步接收环缓冲区的一些页面,以便更容易地访问收到的包裹的数据包:

    for (i = 0; i < AR_BUFFERS; i++) {
            ctx->pages[i] = alloc_page(GFP_KERNEL | GFP_DMA32);
            ...
            dma_addr = dma_map_page(ohci->card.device, ctx->pages[i],
                                    0, PAGE_SIZE, DMA_FROM_DEVICE);
            ...
    }
    for (i = 0; i < AR_BUFFERS; i++)
            pages[i]              = ctx->pages[i];
    for (i = 0; i < AR_WRAPAROUND_PAGES; i++)
            pages[AR_BUFFERS + i] = ctx->pages[i];
    ctx->buffer = vmap(pages, ARRAY_SIZE(pages), VM_MAP, PAGE_KERNEL);
    ...

As far as I can see, there is no similar API for coherent DMA memory. 据我所知,相干DMA内存没有类似的API。 You might be able to remap the pages returned by dma_alloc_coherent() if you know how your architecture handles this. 如果您知道架构如何处理此页面,则可以重新映射dma_alloc_coherent()返回的页面。

Please note that some architectures can have problems with their caches if you are using multiple virtual addresses to modify the same physical address; 请注意,如果您使用多个虚拟地址来修改相同的物理地址,某些体系结构可能会出现缓存问题。 even if you manage to map it, you have to check if coherent DMA memory is cachable in your specific arch. 即使你设法映射它,你也必须检查一致的DMA存储器是否可以在你的特定拱门中缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM