Linux : task work 机制
task work机制可以在内核中向指定的进程添加一些任务函数,这些任务函数会在进程返回用户态时执行,使用的是该进程的上下文。包括下面的这些API:
- task_work_add
- task_work_cancel
- task_work_run
进程对象task_struct中有个字段用来存储这些待进行的任务列表头即task_works,这个结构体包含一个next指针和需要执行的函数指针。
205 /**
206 * struct callback_head - callback structure for use with RCU and task_work
207 * @next: next update requests in a list
208 * @func: actual update function to call after the grace period.
209 */
210 struct callback_head {
211 struct callback_head *next;
212 void (*func)(struct callback_head *head);
213 };
4
5 static struct callback_head work_exited; /* all we need is ->next == NULL */
6
7 /**
8 * task_work_add - ask the @task to execute @work->func()
9 * @task: the task which should run the callback
10 * @work: the callback to run
11 * @notify: send the notification if true
12 *
13 * Queue @work for task_work_run() below and notify the @task if @notify.
14 * Fails if the @task is exiting/exited and thus it can't process this @work.
15 * Otherwise @work->func() will be called when the @task returns from kernel
16 * mode or exits.
17 *
18 * This is like the signal handler which runs in kernel mode, but it doesn't
19 * try to wake up the @task.
20 *
21 * RETURNS:
22 * 0 if succeeds or -ESRCH.
23 */
24 int
25 task_work_add(struct task_struct *task, struct callback_head *work, bool notify)
26 {
27 struct callback_head *head;
28
29 do {
30 head = ACCESS_ONCE(task->task_works);
31 if (unlikely(head == &work_exited))
32 return -ESRCH;
33 work->next = head;
34 } while (cmpxchg(&task->task_works, head, work) != head);
35
36 if (notify)
37 set_notify_resume(task);
38 return 0;
39 }
主要工作:
1. 通过CAS以无锁的形式添加了一个链表元素。(新元素排在原有链表头部)
2. set_notify_resume函数向指定的进程设置了一个_TIF_NOTIFY_RESUME标记。
task_work_run执行时机
在返回用户态之前会对当前进程的标记检查,如果相关标记置位则会调用do_notify_resume
595 int_signal:
596 testl $_TIF_DO_NOTIFY_MASK,%edx
597 jz 1f
598 movq %rsp,%rdi # &ptregs -> arg1
599 xorl %esi,%esi # oldset -> arg2
600 call do_notify_resume
601 1: movl $_TIF_WORK_MASK,%edi
602 int_restore_rest:
603 RESTORE_REST
604 DISABLE_INTERRUPTS(CLBR_NONE)
605 TRACE_IRQS_OFF
606 jmp int_with_check
607 CFI_ENDPROC
608 END(system_call)
以上文件为entry_64.S,而标记定义在thread_info.c中
130 /* work to do on interrupt/exception return */
131 #define _TIF_WORK_MASK \
132 (0x0000FFFF & \
133 ~(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT| \
134 _TIF_SINGLESTEP|_TIF_SECCOMP|_TIF_SYSCALL_EMU))
70 #define TIF_SYSCALL_TRACE 0 /* syscall trace active */
71 #define TIF_NOTIFY_RESUME 1 /* callback before returning to user */
72 #define TIF_SIGPENDING 2 /* signal pending */
73 #define TIF_NEED_RESCHED 3 /* rescheduling necessary */
74 #define TIF_SINGLESTEP 4 /* reenable singlestep on user return*/
75 #define TIF_SYSCALL_EMU 6 /* syscall emulation active */
76 #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
77 #define TIF_SECCOMP 8 /* secure computing */
78 #define TIF_MCE_NOTIFY 10 /* notify userspace of an MCE */
79 #define TIF_USER_RETURN_NOTIFY 11 /* notify kernel of userspace return */
80 #define TIF_UPROBE 12 /* breakpointed or singlestepping */
81 #define TIF_NOTSC 16 /* TSC is not accessible in userland */
82 #define TIF_IA32 17 /* IA32 compatibility process */
83 #define TIF_FORK 18 /* ret_from_fork */
84 #define TIF_NOHZ 19 /* in adaptive nohz mode */
85 #define TIF_MEMDIE 20 /* is terminating due to OOM killer */
86 #define TIF_POLLING_NRFLAG 21 /* idle is polling for TIF_NEED_RESCHED */
87 #define TIF_IO_BITMAP 22 /* uses I/O bitmap */
88 #define TIF_FORCED_TF 24 /* true if TF in eflags artificially */
89 #define TIF_BLOCKSTEP 25 /* set when we want DEBUGCTLMSR_BTF */
90 #define TIF_LAZY_MMU_UPDATES 27 /* task is updating the mmu lazily */
91 #define TIF_SYSCALL_TRACEPOINT 28 /* syscall tracepoint instrumentation */
92 #define TIF_ADDR32 29 /* 32-bit address space on 64 bits */
93 #define TIF_X32 30 /* 32-bit native x86-64 binary */
94
即_TIF_WORK_MASK表示除开(_TIF_SYSCALL_TRACE, _TIF_SYSCALL_AUDIT, _TIF_SINGLESTEP, _TIF_SECCOMP, _TIF_SYSCALL_EMU)之外的所有标记。自然包括了_TIF_NOTIFY_RESUME标记。
do_notify_resume函数
729 /*
730 * notification of userspace execution resumption
731 * - triggered by the TIF_WORK_MASK flags
732 */
733 __visible void
734 do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
735 {
736 user_exit();
737
738 #ifdef CONFIG_X86_MCE
739 /* notify userspace of pending MCEs */
740 if (thread_info_flags & _TIF_MCE_NOTIFY)
741 mce_notify_process();
742 #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
743
744 if (thread_info_flags & _TIF_UPROBE)
745 uprobe_notify_resume(regs);
746
747 /* deal with pending signal delivery */
748 if (thread_info_flags & _TIF_SIGPENDING)
749 do_signal(regs);
750
751 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
752 clear_thread_flag(TIF_NOTIFY_RESUME);
753 tracehook_notify_resume(regs);
754 }
755 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
756 fire_user_return_notifiers();
757
758 user_enter();
759 }
可以看到在其中调用tracehook_notify_resume函数,也包括其他一些如信号处理相关的函数。
tracehook_notify_resume
174 /**
175 * tracehook_notify_resume - report when about to return to user mode
176 * @regs: user-mode registers of @current task
177 *
178 * This is called when %TIF_NOTIFY_RESUME has been set. Now we are
179 * about to return to user mode, and the user state in @regs can be
180 * inspected or adjusted. The caller in arch code has cleared
181 * %TIF_NOTIFY_RESUME before the call. If the flag gets set again
182 * asynchronously, this will be called again before we return to
183 * user mode.
184 *
185 * Called without locks.
186 */
187 static inline void tracehook_notify_resume(struct pt_regs *regs)
188 {
189 /*
190 * The caller just cleared TIF_NOTIFY_RESUME. This barrier
191 * pairs with task_work_add()->set_notify_resume() after
192 * hlist_add_head(task->task_works);
193 */
194 smp_mb__after_atomic();
195 if (unlikely(current->task_works))
196 task_work_run();
197 }
在进程对象的task_works不为null的情况下才有任务需要执行。
task_work_run
77 /**
78 * task_work_run - execute the works added by task_work_add()
79 *
80 * Flush the pending works. Should be used by the core kernel code.
81 * Called before the task returns to the user-mode or stops, or when
82 * it exits. In the latter case task_work_add() can no longer add the
83 * new work after task_work_run() returns.
84 */
85 void task_work_run(void)
86 {
87 struct task_struct *task = current;
88 struct callback_head *work, *head, *next;
89
90 for (;;) {
91 /*
92 * work->func() can do task_work_add(), do not set
93 * work_exited unless the list is empty.
94 */
95 do {
96 work = ACCESS_ONCE(task->task_works);
97 head = !work && (task->flags & PF_EXITING) ?
98 &work_exited : NULL;
99 } while (cmpxchg(&task->task_works, work, head) != work);
100
101 if (!work)
102 break;
103 /*
104 * Synchronize with task_work_cancel(). It can't remove
105 * the first entry == work, cmpxchg(task_works) should
106 * fail, but it can play with *work and other entries.
107 */
108 raw_spin_unlock_wait(&task->pi_lock);
109 smp_mb();
110
111 /* Reverse the list to run the works in fifo order */
112 head = NULL;
113 do {
114 next = work->next;
115 work->next = head;
116 head = work;
117 work = next;
118 } while (work);
119
120 work = head;
121 do {
122 next = work->next;
123 work->func(work);
124 work = next;
125 cond_resched();
126 } while (work);
127 }
128 }
1. 通过CAS,以无锁的方式取得task_works链表
2. 因为原链表是按元素添加到链表的时间逆序排列的(见task_work_add),先把链表反转一遍
3. 反转链表后,遍历链表,执行各个元素的任务函数即work->func(work)
Linux : task work 机制的更多相关文章
- Linux内核同步机制--转发自蜗窝科技
Linux内核同步机制之(一):原子操作 http://www.wowotech.net/linux_kenrel/atomic.html 一.源由 我们的程序逻辑经常遇到这样的操作序列: 1.读一个 ...
- Linux内核同步机制
http://blog.csdn.net/bullbat/article/details/7376424 Linux内核同步控制方法有很多,信号量.锁.原子量.RCU等等,不同的实现方法应用于不同的环 ...
- Linux中断管理 (1)Linux中断管理机制
目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机制> <Linux中断管理 (2)软中断和tasklet> <Linux中断管 ...
- Linux中断管理 (1)Linux中断管理机制【转】
转自:https://www.cnblogs.com/arnoldlu/p/8659981.html 目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机 ...
- Linux kernel 同步机制
Linux kernel同步机制(上篇) https://mp.weixin.qq.com/s/mosYi_W-Rp1-HgdtxUqSEgLinux kernel 同步机制(下篇) https:// ...
- 浅谈Linux内存管理机制
经常遇到一些刚接触Linux的新手会问内存占用怎么那么多?在Linux中经常发现空闲内存很少,似乎所有的内存都被系统占用了,表面感觉是内存不够用了,其实不然.这是Linux内存管理的一个优秀特性,在这 ...
- Linux的io机制
Linux的io机制 Buffered-IO 和Direct-IO Linux磁盘I/O分为Buffered IO和Direct IO,这两者有何区别呢? 对于Buffered IO: 当应用程序尝试 ...
- [内核同步]浅析Linux内核同步机制
转自:http://blog.csdn.net/fzubbsc/article/details/37736683?utm_source=tuicool&utm_medium=referral ...
- 了解linux内存管理机制(转)
今天了解了下linux内存管理机制,在这里记录下,原文在这里http://ixdba.blog.51cto.com/2895551/541355 根据自己的理解画了张图: 下面是转载的内容: 一 物理 ...
随机推荐
- Codeforces Round #427 (Div. 2) B. The number on the board
引子: A题过于简单导致不敢提交,拖拖拉拉10多分钟还是决定交,太冲动交错了CE一发,我就知道又要错过一次涨分的机会.... B题还是过了,根据题意目测数组大小开1e5,居然蒙对,感觉用vector更 ...
- cas单点登陆系统-casServer搭建
最近工作比较忙,空闲的时间在搞单点登陆系统,自己写了一套SSO在GitHub上,过程走通了.通过这个例子,自己熟悉了流程,而且破天荒的使用了抽象设计模式,并且熟悉了cookies和session的使用 ...
- 【13】JMicro微服务-ID生成与Redis
如非授权,禁止用于商业用途,转载请注明出处作者:mynewworldyyl 往下看前,建议完成前面1到12小节 1. 微服务中ID地位 如果说前面小节的功能点是微服务的大脑,那么全局唯一ID则是微服务 ...
- 【GDKOI2017】 两个胖子萌萌哒 小学奥数题
题目大意:给你一个$n\times m$的网格,你要在这个网格上画三角形. 三角形的顶点只能在网格的整点上,且至少有一条边平行于$x$或$y$轴,且三角形面积为整数.问你能画多少个不同的三角形. 两个 ...
- OS之进程管理---进程调度和多线程调度
进程调度基本概念 多道程序的目标就是始终允许某个进程运行以最大化CPU利用率,多个进程通时存在于内存中,操作系统通过进程调度程序按特定的调度算法来调度就绪队列中的进程到CPU,从而最大限度的利用CPU ...
- Excel中复杂跨行跨列数据
XSSFWorkbook wb = new XSSFWorkbook(); // 工作表 XSSFSheet sheet = wb.createSheet("车辆使用情况统计"); ...
- 剑指offer三十一之连数中1出现的次数(从1到n整数中1出现的次数
一.题目 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...
- django第四课 标签的用法(if/else、for、ifequal、过滤器、注释等)
if/else {% if %} <p>内容</P> {% endif %} {% else %}是可选标签 {% if %} <p>内容</P> {% ...
- 解决waveInOpen录音编译x64程序出错的问题
1.之前也碰到过x86程序升级为x64程序,关键点是类型大小的使用. 之前同事碰到过一个用int表示指针的程序,程序改为x64会出错,找原因找了半天. 2.今天我也碰到了,使用aveInOpen录音, ...
- 如何在虚拟机安装的Win10系统里快速打开【此电脑】图标?(图文详解)
不多说,直接上干货! 为什么要写写这篇博客? 技多不压身,很多小技巧很重要,方便自己. 比如,对于这样的工具,个人来讲,玩过试用期,意味着你若不找点法子,是不行的.否则你没得玩了. 全网最详细的Tab ...