文章来源:http://blog.chinaunix.net/uid-22278460-id-1777659.html
</pre><pre name="code" class="html">
所有的系统调用,基于都可以在它的名字前加上“
sys_
”前缀,这就是它在内核中对应的函数。比如系统调用
open
、
read
、
write
、
poll
,与之对应的内核函数为:
sys_open
、
sys_read
、
sys_write
、
sys_poll
。
一、内核框架:
对于系统调用 poll 或 select ,它们对应的内核函数都是 sys_poll 。分析 sys_poll ,即可理解 poll 机制。
1.
sys_poll 函数位于 fs/select.c 文件中,代码如下:
asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
long timeout_msecs)
{
s64 timeout_jiffies;
if (timeout_msecs > 0) {
#if HZ > 1000
/* We can only overflow if HZ > 1000 */
if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
timeout_jiffies = -1;
else
#endif
timeout_jiffies = msecs_to_jiffies(timeout_msecs);
} else {
/* Infinite (< 0) or no (0) timeout */
timeout_jiffies = timeout_msecs;
}
return do_sys_poll(ufds, nfds, &timeout_jiffies);
}
它对超时参数稍作处理后,直接调用 do_sys_poll 。
2.
do_sys_poll 函数也位于位于 fs/select.c 文件中,我们忽略其他代码:
int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
{
……
poll_initwait(&table);
……
fdcount = do_poll(nfds, head, &table, timeout);
……
}
poll_initwait 函数非常简单,它初始化一个 poll_wqueues 变量 table :
poll_initwait > init_poll_funcptr(&pwq->pt, __pollwait); > pt->qproc = qproc;
即 table->pt->qproc = __pollwait , __pollwait 将在驱动的 poll 函数里用到。
3.
do_sys_poll 函数位于 fs/select.c 文件中,代码如下:
static int do_poll(unsigned int nfds,
struct poll_list *list,
struct poll_wqueues *wait, s64 *timeout)
{
01 ……
02
for (;;) {
03 ……
04
if (do_pollfd(pfd, pt)) {
05
count++;
06
pt = NULL;
07
}
08 ……
09
if (count || !*timeout || signal_pending(current))
10
break;
11
count = wait->error;
12
if (count)
13
break;
14
15
if (*timeout < 0) {
16
/* Wait indefinitely */
17
__timeout = MAX_SCHEDULE_TIMEOUT;
18
} else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
19
/*
20
* Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
21
* a loop
22
*/
23
__timeout = MAX_SCHEDULE_TIMEOUT - 1;
24
*timeout -= __timeout;
25
} else {
26
__timeout = *timeout;
27
*timeout = 0;
28
}
29
30
__timeout = schedule_timeout(__timeout);
31
if (*timeout >= 0)
32
*timeout += __timeout;
33
}
34
__set_current_state(TASK_RUNNING);
35
return count;
36 }
分析其中的代码,可以发现,它的作用如下:
①
从 02 行可以知道,这是个循环,它退出的条件为:
a.
09 行的 3 个条件之一 (count 非 0 ,超时、有信号等待处理 )
count 顺 0 表示 04 行的 do_pollfd 至少有一个成功。
b.
11 、 12 行:发生错误
②
重点在 do_pollfd 函数,后面再分析
③
第 30 行,让本进程休眠一段时间,注意:应用程序执行 poll 调用后,如果①②的条件不满足,进程就会进入休眠。那么,谁唤醒呢?除了休眠到指定时间被系统唤醒外,还可以被驱动程序唤醒──记住这点,这就是为什么驱动的 poll 里要调用 poll_wait 的原因,后面分析。
4.
do_pollfd 函数位于 fs/select.c 文件中,代码如下:
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 函数的 30 行“ __timeout = schedule_timeout(__timeout) ”。
poll_wait 只是把本进程挂入某个队列,应用程序调用 poll > sys_poll > do_sys_poll > poll_initwait , do_poll > do_pollfd > 我们自己写的 poll 函数后,再调用 schedule_timeout 进入休眠。如果我们的驱动程序发现情况就绪,可以把这个队列上挂着的进程唤醒。可见, poll_wait 的作用,只是为了让驱动程序能找到要唤醒的进程。即使不用 poll_wait ,我们的程序也有机会被唤醒: chedule_timeout(__timeout) ,只是休眠 __time_out 这段时间。
现在来总结一下 poll 机制:
1. poll > sys_poll > do_sys_poll > poll_initwait , poll_initwait 函数注册一下回调函数 __pollwait ,它就是我们的驱动程序执行 poll_wait 时,真正被调用的函数。
2. 接下来执行 file->f_op->poll ,即我们驱动程序里自己实现的 poll 函数
它会调用 poll_wait 把自己挂入某个队列,这个队列也是我们的驱动自己定义的;
它还判断一下设备是否就绪。
3. 如果设备未就绪, do_sys_poll 里会让进程休眠一定时间
4. 进程被唤醒的条件有 2 :一是上面说的“一定时间”到了,二是被驱动程序唤醒。驱动程序发现条件就绪时,就把“某个队列”上挂着的进程唤醒,这个队列,就是前面通过 poll_wait 把本进程挂过去的队列。
5. 如果驱动程序没有去唤醒进程,那么 chedule_timeout(__timeou) 超时后,会重复 2 、 3 动作,直到应用程序的 poll 调用传入的时间到达。
一、内核框架:
对于系统调用 poll 或 select ,它们对应的内核函数都是 sys_poll 。分析 sys_poll ,即可理解 poll 机制。
1.
sys_poll 函数位于 fs/select.c 文件中,代码如下:
asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
long timeout_msecs)
{
s64 timeout_jiffies;
if (timeout_msecs > 0) {
#if HZ > 1000
/* We can only overflow if HZ > 1000 */
if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
timeout_jiffies = -1;
else
#endif
timeout_jiffies = msecs_to_jiffies(timeout_msecs);
} else {
/* Infinite (< 0) or no (0) timeout */
timeout_jiffies = timeout_msecs;
}
return do_sys_poll(ufds, nfds, &timeout_jiffies);
}
它对超时参数稍作处理后,直接调用 do_sys_poll 。
2.
do_sys_poll 函数也位于位于 fs/select.c 文件中,我们忽略其他代码:
int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
{
……
poll_initwait(&table);
……
fdcount = do_poll(nfds, head, &table, timeout);
……
}
poll_initwait 函数非常简单,它初始化一个 poll_wqueues 变量 table :
poll_initwait > init_poll_funcptr(&pwq->pt, __pollwait); > pt->qproc = qproc;
即 table->pt->qproc = __pollwait , __pollwait 将在驱动的 poll 函数里用到。
3.
do_sys_poll 函数位于 fs/select.c 文件中,代码如下:
static int do_poll(unsigned int nfds,
struct poll_list *list,
struct poll_wqueues *wait, s64 *timeout)
{
01 ……
02
for (;;) {
03 ……
04
if (do_pollfd(pfd, pt)) {
05
count++;
06
pt = NULL;
07
}
08 ……
09
if (count || !*timeout || signal_pending(current))
10
break;
11
count = wait->error;
12
if (count)
13
break;
14
15
if (*timeout < 0) {
16
/* Wait indefinitely */
17
__timeout = MAX_SCHEDULE_TIMEOUT;
18
} else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
19
/*
20
* Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
21
* a loop
22
*/
23
__timeout = MAX_SCHEDULE_TIMEOUT - 1;
24
*timeout -= __timeout;
25
} else {
26
__timeout = *timeout;
27
*timeout = 0;
28
}
29
30
__timeout = schedule_timeout(__timeout);
31
if (*timeout >= 0)
32
*timeout += __timeout;
33
}
34
__set_current_state(TASK_RUNNING);
35
return count;
36 }
分析其中的代码,可以发现,它的作用如下:
①
从 02 行可以知道,这是个循环,它退出的条件为:
a.
09 行的 3 个条件之一 (count 非 0 ,超时、有信号等待处理 )
count 顺 0 表示 04 行的 do_pollfd 至少有一个成功。
b.
11 、 12 行:发生错误
②
重点在 do_pollfd 函数,后面再分析
③
第 30 行,让本进程休眠一段时间,注意:应用程序执行 poll 调用后,如果①②的条件不满足,进程就会进入休眠。那么,谁唤醒呢?除了休眠到指定时间被系统唤醒外,还可以被驱动程序唤醒──记住这点,这就是为什么驱动的 poll 里要调用 poll_wait 的原因,后面分析。
4.
do_pollfd 函数位于 fs/select.c 文件中,代码如下:
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 函数的 30 行“ __timeout = schedule_timeout(__timeout) ”。
poll_wait 只是把本进程挂入某个队列,应用程序调用 poll > sys_poll > do_sys_poll > poll_initwait , do_poll > do_pollfd > 我们自己写的 poll 函数后,再调用 schedule_timeout 进入休眠。如果我们的驱动程序发现情况就绪,可以把这个队列上挂着的进程唤醒。可见, poll_wait 的作用,只是为了让驱动程序能找到要唤醒的进程。即使不用 poll_wait ,我们的程序也有机会被唤醒: chedule_timeout(__timeout) ,只是休眠 __time_out 这段时间。
现在来总结一下 poll 机制:
1. poll > sys_poll > do_sys_poll > poll_initwait , poll_initwait 函数注册一下回调函数 __pollwait ,它就是我们的驱动程序执行 poll_wait 时,真正被调用的函数。
2. 接下来执行 file->f_op->poll ,即我们驱动程序里自己实现的 poll 函数
它会调用 poll_wait 把自己挂入某个队列,这个队列也是我们的驱动自己定义的;
它还判断一下设备是否就绪。
3. 如果设备未就绪, do_sys_poll 里会让进程休眠一定时间
4. 进程被唤醒的条件有 2 :一是上面说的“一定时间”到了,二是被驱动程序唤醒。驱动程序发现条件就绪时,就把“某个队列”上挂着的进程唤醒,这个队列,就是前面通过 poll_wait 把本进程挂过去的队列。
5. 如果驱动程序没有去唤醒进程,那么 chedule_timeout(__timeou) 超时后,会重复 2 、 3 动作,直到应用程序的 poll 调用传入的时间到达。