我正在研究基于pcie的网络驱动程序。不同的示例使用 pci_alloc_consistent 或 dma_alloc_coherent 之一来获取传输和接收描述符的内存。哪一个更好(如果有的话),两者之间有什么区别?
答案 0 :(得分:14)
差异很微妙但非常重要。
pci_alloc_consistent()
是两者的旧功能,旧版驱动程序仍在使用它。
现在,pci_alloc_consistent()
只需拨打dma_alloc_coherent()
。
区别?已分配内存的类型。
pci_alloc_consistent()
- 分配类型为GFP_ATOMIC
的内存。
分配不睡觉,用于例如中断处理程序,底部
半。
dma_alloc_coherent()
- 您自己指定了什么类型的内存
分配。您不应该使用高优先级GFP_ATOMIC
内存
除非你需要它,在大多数情况下,你会没事的
GFP_KERNEL
分配。
pci_alloc_consistent()
的内核3.18定义非常简单,即:
static inline void *
pci_alloc_consistent(struct pci_dev *hwdev, size_t size,
dma_addr_t *dma_handle)
{
return dma_alloc_coherent(hwdev == NULL ? NULL : &hwdev->dev, size, dma_handle, GFP_ATOMIC);
}
简而言之,请使用dma_alloc_coherent()
。