1. dmabuf与poll事件唤醒机制解析
在Linux内核的DMA-BUF(Direct Memory Access Buffer)子系统中,poll机制扮演着关键角色。DMA-BUF是Linux内核中用于跨设备共享内存缓冲区的框架,而poll系统调用则允许应用程序监视多个文件描述符的状态变化。当这两者结合时,就形成了高效的异步事件通知机制。
DMA-BUF本质上是一个共享内存区域,可以被多个设备或进程访问。它的核心数据结构是struct dma_buf,其中包含了文件操作集合dma_buf_ops。在这个操作集合中,poll回调函数正是实现事件唤醒的关键:
c复制struct dma_buf_ops {
...
unsigned long (*poll)(struct file *file, struct poll_table_struct *poll);
...
};
当应用程序调用poll()或select()系统调用监视DMA-BUF文件描述符时,内核最终会调用这个poll回调函数。该函数需要完成两项核心工作:
- 将当前进程添加到等待队列中
- 返回当前的文件状态标志(可读/可写/异常等)
2. poll事件唤醒的实现细节
2.1 等待队列的注册机制
在DMA-BUF驱动实现中,poll回调通常会使用poll_wait()函数将当前进程注册到等待队列:
c复制static unsigned long dma_buf_poll(struct file *file,
struct poll_table_struct *poll)
{
struct dma_buf *dmabuf = file->private_data;
poll_wait(file, &dmabuf->poll_wait, poll);
return dma_buf_poll_status(dmabuf);
}
这里的poll_wait并不会真正阻塞进程,它只是将当前进程添加到dmabuf->poll_wait等待队列中。当DMA传输完成或其他事件发生时,驱动需要
