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 机制的更多相关文章

  1. Linux内核同步机制--转发自蜗窝科技

    Linux内核同步机制之(一):原子操作 http://www.wowotech.net/linux_kenrel/atomic.html 一.源由 我们的程序逻辑经常遇到这样的操作序列: 1.读一个 ...

  2. Linux内核同步机制

    http://blog.csdn.net/bullbat/article/details/7376424 Linux内核同步控制方法有很多,信号量.锁.原子量.RCU等等,不同的实现方法应用于不同的环 ...

  3. Linux中断管理 (1)Linux中断管理机制

    目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机制> <Linux中断管理 (2)软中断和tasklet> <Linux中断管 ...

  4. Linux中断管理 (1)Linux中断管理机制【转】

    转自:https://www.cnblogs.com/arnoldlu/p/8659981.html 目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机 ...

  5. Linux kernel 同步机制

    Linux kernel同步机制(上篇) https://mp.weixin.qq.com/s/mosYi_W-Rp1-HgdtxUqSEgLinux kernel 同步机制(下篇) https:// ...

  6. 浅谈Linux内存管理机制

    经常遇到一些刚接触Linux的新手会问内存占用怎么那么多?在Linux中经常发现空闲内存很少,似乎所有的内存都被系统占用了,表面感觉是内存不够用了,其实不然.这是Linux内存管理的一个优秀特性,在这 ...

  7. Linux的io机制

    Linux的io机制 Buffered-IO 和Direct-IO Linux磁盘I/O分为Buffered IO和Direct IO,这两者有何区别呢? 对于Buffered IO: 当应用程序尝试 ...

  8. [内核同步]浅析Linux内核同步机制

    转自:http://blog.csdn.net/fzubbsc/article/details/37736683?utm_source=tuicool&utm_medium=referral ...

  9. 了解linux内存管理机制(转)

    今天了解了下linux内存管理机制,在这里记录下,原文在这里http://ixdba.blog.51cto.com/2895551/541355 根据自己的理解画了张图: 下面是转载的内容: 一 物理 ...

随机推荐

  1. python django 更改模型字段出错时的一个解决办法

    python/django 框架自带的 orm 无疑是django框架最拿得出手的一个亮点,orm无疑极大的方便了项目的开发,提高了开发的效率. 在实际的项目开发过程中,我们有时候需要修改模型的字段, ...

  2. jzoj4424

    20%:暴力枚舉每一條邊有沒有被選到,然後使用并查集判斷聯通性 這樣子有20分,但是我考試寫掛了所以1分也沒有 100%:這道題2000的數據範圍,使用指數級搜索會tle,需要更加好的方法 這道題中, ...

  3. kali Linux 上编译并使用RFID核弹——proxmark3

    你还在在Windows下使用proxmark3?弱爆了! 本文作者:i春秋签约作家——冰尘 作为一个标准的日天日地日空气的(单身贵族泰迪)物理黑客Proxmark3这么高大上的东西应该是在键盘敲打声中 ...

  4. MFC多线程技术

    MFC中有两类线程,分别称之为工作者线程和用户界面线程.二者的主要区别在于工作者线程没有消息循环,而用户界面线程有自己的消息队列和消息循环. 工作者线程没笑消息机制,通常用来执行后台计算和维护任务,如 ...

  5. 【xsy1116】数学题 奥数题

    真实奥数题 题目大意:给你正整数k$,r$.问你存在多少对$(x,y)$,满足$x<y$且$x^2+y^2=kz^2$,并将所有符合条件的数对输出. 数据范围:$r≤1e9$,$k={1,2,3 ...

  6. pymysql模块使用

    一.写函数的原因 写这个函数的原因就是为了能够不每次在用Python用数据库的时候还要在写一遍  做个通用函数做保留,也给大家做个小小的分享,函数不是最好的,希望有更好的代码的朋友能提出 互相学习 二 ...

  7. 服务器集群的session管理

    应用服务器的高可用架构设计主要基于服务无状态这一特性,但是事实上,业务总是有状态的,单机情况下,Session可由部署在服务器上的web容器(如Jboss)管理.在使用负载均衡的集群环境中,由于负载均 ...

  8. C#获取获取北京时间多种方法

    #region 获取网络时间 ///<summary> /// 获取中国国家授时中心网络服务器时间发布的当前时间 ///</summary> ///<returns> ...

  9. (转)Python标准库:内置函数print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

    原文:https://blog.csdn.net/caimouse/article/details/44133241 https://www.cnblogs.com/owasp/p/5372476.h ...

  10. 极高效内存池实现 (cpu-cache)

    视频请看 : http://edu.csdn.net/course/detail/627 1.内存池的目的 提高程序的效率 减少运行时间 避免内存碎片 2.原理   要解决上述两个问题,最好的方法就是 ...