linux poll机制窥探
内核框架
对于系统调用poll或select,它们对应的内核函数都是sys_poll,文件位于fs/select.c
sys_poll 源码
SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,
int, timeout_msecs)
{
struct timespec64 end_time, *to = NULL;
int ret;
if (timeout_msecs >= 0) {
to = &end_time;
poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC,
NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC));
}
ret = do_sys_poll(ufds, nfds, to); //我们需要关注的
....
return ret;
}
它对超时参数稍作处理后,直接调用do_sys_poll
static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
struct timespec64 *end_time)
{
....
poll_initwait(&table);
fdcount = do_poll(head, &table, end_time);//我们需要关注的
....
}
//注册__pollwait函数,在poll_wait里面最终会掉这个函数
void poll_initwait(struct poll_wqueues *pwq)
{
init_poll_funcptr(&pwq->pt, __pollwait);
pwq->polling_task = current;
pwq->triggered = 0;
pwq->error = 0;
pwq->table = NULL;
pwq->inline_index = 0;
}
static inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc)
{
pt->_qproc = qproc;
pt->_key = ~(__poll_t)0; /* all events enabled */
}
static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
{
if (p && p->_qproc && wait_address)
p->_qproc(filp, wait_address, p);
}
poll_initwait 函数非常简单,它初始化一个poll_wqueues变量table:
poll_initwait > init_poll_funcptr(&pwq->pt, __pollwait); > pt->qproc = qproc;
即table->pt->qproc = __pollwait,__pollwait将在驱动的poll函数里会用到。poll_wait->__pollwait
do_poll函数位于fs/select.c文件中,代码如下:
static int do_poll(struct poll_list *list, struct poll_wqueues *wait,
struct timespec64 *end_time)
{
poll_table* pt = &wait->pt;
ktime_t expire, *to = NULL;
int timed_out = 0, count = 0;
u64 slack = 0;
__poll_t busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;
unsigned long busy_start = 0;
/* Optimise the no-wait case */
if (end_time && !end_time->tv_sec && !end_time->tv_nsec)
{
pt->_qproc = NULL;
timed_out = 1;
}
if (end_time && !timed_out)
slack = select_estimate_accuracy(end_time);
for (;;) {//死循环,注意退出的条件
struct poll_list *walk;
bool can_busy_loop = false;
for (walk = list; walk != NULL; walk = walk->next) {
struct pollfd * pfd, * pfd_end;
pfd = walk->entries;
pfd_end = pfd + walk->len;
for (; pfd != pfd_end; pfd++) {
if (do_pollfd(pfd, pt, &can_busy_loop,
busy_flag)) {
count++;
pt->_qproc = NULL;
/* found something, stop busy polling */
busy_flag = 0;
can_busy_loop = false;
}
}
}
pt->_qproc = NULL;
if (!count) {
count = wait->error;
if (signal_pending(current))
count = -ERESTARTNOHAND;
}
if (count || timed_out)
break; //退出死循环
if (can_busy_loop && !need_resched()) {
if (!busy_start) {
busy_start = busy_loop_current_time();
continue;
}
if (!busy_loop_timeout(busy_start))
continue;
}
busy_flag = 0;
if (end_time && !to) {
expire = timespec64_to_ktime(*end_time);
to = &expire;
}
//睡眠当前进程
//让本进程休眠一段时间,注意:应用程序执行poll调用后,
//如果count || timed_out的条件不满足,进程就会进入休眠。那么,谁唤醒呢?
//除了休眠到指定时间被系统唤醒外,还可以被驱动程序唤醒
//──记住这点,这就是为什么驱动的poll里要调用poll_wait
//的原因,poll_wait 就是让驱动自己将任务挂在自己创建的等待队列里面,如果有信号来了,就可以调用poll_wake唤醒任务。
if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))
timed_out = 1;
}
return count;
}
do_pollfd 源码如下
static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
{
……
if (file->f_op && file->f_op->poll)
mask = file->f_op->poll(file, pwait);
……
}
可见,它就是调用我们的驱动程序里注册的poll函数。
驱动程序
驱动程序里与poll相关的地方有两处:一是构造file_operation结构时,要定义自己的poll函数。二是通过poll_wait来调用上面说到的__pollwait函数,pollwait的代码如下:
static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
{
if (p && wait_address)
p->qproc(filp, wait_address, p);
}
p->qproc就是__pollwait函数,从它的代码可知,它只是把当前进程挂入我们驱动程序里定义的一个队列里而已。它的代码如下:
static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
poll_table *p)
{
struct poll_table_entry *entry = poll_get_entry(p);
if (!entry)
return;
get_file(filp);
entry->filp = filp;
entry->wait_address = wait_address;
init_waitqueue_entry(&entry->wait, current);
add_wait_queue(wait_address, &entry->wait);
}
执行到驱动程序的poll_wait函数时,进程并没有休眠,我们的驱动程序里实现的poll函数是不会引起休眠的。让进程进入休眠,是前面分析的do_sys_poll函数的“poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack)”。
poll_wait只是把本进程挂入等待队列里面,应用程序调用poll > sys_poll > do_sys_poll > poll_initwait,do_poll > do_pollfd > 我们自己写的poll函数后,再调用poll_schedule_timeout进入休眠。如果我们的驱动程序发现情况就绪,可以把这个队列上挂着的进程唤醒(poll_wake)。可见,poll_wait的作用,只是为了让驱动程序能找到要唤醒的进程。即使不用poll_wait,我们的程序也有机会被唤醒:poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack),只是休眠到时间to。
现在来总结一下poll机制:
-
poll > sys_poll > do_sys_poll > poll_initwait,poll_initwait函数注册一下回调函数__pollwait,它就是我们的驱动程序执行poll_wait时,真正被调用的函数。
-
接下来执行file->f_op->poll,即我们驱动程序里自己实现的poll函数
它会调用poll_wait把自己挂入某个队列,这个队列也是我们的驱动自己定义的;
它还判断一下设备是否就绪。
//if (listener->event_count != atomic_read(&idev->event)) -
如果设备未就绪,do_sys_poll里会让进程休眠一定时间
-
进程被唤醒的条件有2:一是上面说的“一定时间”到了,二是被驱动程序唤醒。驱动程序发现条件就绪时,就把“某个队列”上挂着的进程唤醒,这个队列,就是前面通过poll_wait把本进程挂过去的队列。
-
如果驱动程序没有去唤醒进程,那么poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack)超时后,会重复上面2、3动作,直到应用程序的poll调用传入的时间到达(超时唤醒)。
//poll_wait 就是让驱动自己将任务挂在自己创建的等待队列里面,如果有信号来了,就可以调用poll_wake唤醒任务,否则就要等到超时唤醒
UIO框架里面的poll实现
首先定义好file_operations
static const struct file_operations uio_fops = {
.owner = THIS_MODULE,
.open = uio_open,
.release = uio_release,
.read = uio_read,
.write = uio_write,
.mmap = uio_mmap,
.poll = uio_poll,
.fasync = uio_fasync,
.llseek = noop_llseek,
};
其中我们关注的是驱动poll的实现。
static __poll_t uio_poll(struct file *filep, poll_table *wait)
{
struct uio_listener *listener = filep->private_data;
struct uio_device *idev = listener->dev;
__poll_t ret = 0;
mutex_lock(&idev->info_lock);
if (!idev->info || !idev->info->irq)
ret = -EIO;
mutex_unlock(&idev->info_lock);
if (ret)
return ret;
poll_wait(filep, &idev->wait, wait);//将该进程加入驱动的等待队列里面,方便驱动后续唤醒
if (listener->event_count != atomic_read(&idev->event))
return EPOLLIN | EPOLLRDNORM;
return 0;
}
一般唤醒是通过中断来唤醒的。
static irqreturn_t uio_interrupt(int irq, void *dev_id)
{
struct uio_device *idev = (struct uio_device *)dev_id;
irqreturn_t ret;
ret = idev->info->handler(irq, idev->info); //uio设备自己注册的处理函数
if (ret == IRQ_HANDLED)
uio_event_notify(idev->info);//重点在这
return ret;
}
void uio_event_notify(struct uio_info *info)
{
struct uio_device *idev = info->uio_dev;
atomic_inc(&idev->event); //加加计数变量,然后poll驱动里面就会执行下面的处理,最后系统调用的do_poll函数退出,唤醒我们的进程
//if (listener->event_count != atomic_read(&idev->event))
return EPOLLIN | EPOLLRDNORM;
wake_up_interruptible(&idev->wait);
kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
}