Blame view

kernel/task_work.c 3.36 KB
e73f8959a   Oleg Nesterov   task_work_add: ge...
1
2
3
  #include <linux/spinlock.h>
  #include <linux/task_work.h>
  #include <linux/tracehook.h>
9da33de62   Oleg Nesterov   task_work: task_w...
4
  static struct callback_head work_exited; /* all we need is ->next == NULL */
892f6668f   Oleg Nesterov   task_work: docume...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  /**
   * task_work_add - ask the @task to execute @work->func()
   * @task: the task which should run the callback
   * @work: the callback to run
   * @notify: send the notification if true
   *
   * Queue @work for task_work_run() below and notify the @task if @notify.
   * Fails if the @task is exiting/exited and thus it can't process this @work.
   * Otherwise @work->func() will be called when the @task returns from kernel
   * mode or exits.
   *
   * This is like the signal handler which runs in kernel mode, but it doesn't
   * try to wake up the @task.
   *
   * RETURNS:
   * 0 if succeeds or -ESRCH.
   */
e73f8959a   Oleg Nesterov   task_work_add: ge...
22
  int
ac3d0da8f   Oleg Nesterov   task_work: Make t...
23
  task_work_add(struct task_struct *task, struct callback_head *work, bool notify)
e73f8959a   Oleg Nesterov   task_work_add: ge...
24
  {
ac3d0da8f   Oleg Nesterov   task_work: Make t...
25
  	struct callback_head *head;
9da33de62   Oleg Nesterov   task_work: task_w...
26

ac3d0da8f   Oleg Nesterov   task_work: Make t...
27
28
  	do {
  		head = ACCESS_ONCE(task->task_works);
9da33de62   Oleg Nesterov   task_work: task_w...
29
30
  		if (unlikely(head == &work_exited))
  			return -ESRCH;
ac3d0da8f   Oleg Nesterov   task_work: Make t...
31
32
  		work->next = head;
  	} while (cmpxchg(&task->task_works, head, work) != head);
e73f8959a   Oleg Nesterov   task_work_add: ge...
33

ed3e694d7   Al Viro   move exit_task_wo...
34
  	if (notify)
e73f8959a   Oleg Nesterov   task_work_add: ge...
35
  		set_notify_resume(task);
ed3e694d7   Al Viro   move exit_task_wo...
36
  	return 0;
e73f8959a   Oleg Nesterov   task_work_add: ge...
37
  }
892f6668f   Oleg Nesterov   task_work: docume...
38
39
40
41
42
43
44
45
46
47
48
  /**
   * task_work_cancel - cancel a pending work added by task_work_add()
   * @task: the task which should execute the work
   * @func: identifies the work to remove
   *
   * Find the last queued pending work with ->func == @func and remove
   * it from queue.
   *
   * RETURNS:
   * The found work or NULL if not found.
   */
67d121455   Al Viro   merge task_work a...
49
  struct callback_head *
e73f8959a   Oleg Nesterov   task_work_add: ge...
50
51
  task_work_cancel(struct task_struct *task, task_work_func_t func)
  {
ac3d0da8f   Oleg Nesterov   task_work: Make t...
52
  	struct callback_head **pprev = &task->task_works;
205e550a0   Oleg Nesterov   task_work: minor ...
53
  	struct callback_head *work;
e73f8959a   Oleg Nesterov   task_work_add: ge...
54
  	unsigned long flags;
ac3d0da8f   Oleg Nesterov   task_work: Make t...
55
56
57
58
  	/*
  	 * If cmpxchg() fails we continue without updating pprev.
  	 * Either we raced with task_work_add() which added the
  	 * new entry before this work, we will find it again. Or
9da33de62   Oleg Nesterov   task_work: task_w...
59
  	 * we raced with task_work_run(), *pprev == NULL/exited.
ac3d0da8f   Oleg Nesterov   task_work: Make t...
60
  	 */
e73f8959a   Oleg Nesterov   task_work_add: ge...
61
  	raw_spin_lock_irqsave(&task->pi_lock, flags);
ac3d0da8f   Oleg Nesterov   task_work: Make t...
62
  	while ((work = ACCESS_ONCE(*pprev))) {
205e550a0   Oleg Nesterov   task_work: minor ...
63
  		smp_read_barrier_depends();
ac3d0da8f   Oleg Nesterov   task_work: Make t...
64
65
66
67
  		if (work->func != func)
  			pprev = &work->next;
  		else if (cmpxchg(pprev, work, work->next) == work)
  			break;
e73f8959a   Oleg Nesterov   task_work_add: ge...
68
  	}
e73f8959a   Oleg Nesterov   task_work_add: ge...
69
  	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
ac3d0da8f   Oleg Nesterov   task_work: Make t...
70
71
  
  	return work;
e73f8959a   Oleg Nesterov   task_work_add: ge...
72
  }
892f6668f   Oleg Nesterov   task_work: docume...
73
74
75
76
77
78
79
80
  /**
   * task_work_run - execute the works added by task_work_add()
   *
   * Flush the pending works. Should be used by the core kernel code.
   * Called before the task returns to the user-mode or stops, or when
   * it exits. In the latter case task_work_add() can no longer add the
   * new work after task_work_run() returns.
   */
e73f8959a   Oleg Nesterov   task_work_add: ge...
81
82
83
  void task_work_run(void)
  {
  	struct task_struct *task = current;
ac3d0da8f   Oleg Nesterov   task_work: Make t...
84
  	struct callback_head *work, *head, *next;
e73f8959a   Oleg Nesterov   task_work_add: ge...
85

ac3d0da8f   Oleg Nesterov   task_work: Make t...
86
  	for (;;) {
9da33de62   Oleg Nesterov   task_work: task_w...
87
88
89
90
91
92
93
94
95
  		/*
  		 * work->func() can do task_work_add(), do not set
  		 * work_exited unless the list is empty.
  		 */
  		do {
  			work = ACCESS_ONCE(task->task_works);
  			head = !work && (task->flags & PF_EXITING) ?
  				&work_exited : NULL;
  		} while (cmpxchg(&task->task_works, work, head) != work);
ac3d0da8f   Oleg Nesterov   task_work: Make t...
96
97
98
99
100
101
102
103
104
  		if (!work)
  			break;
  		/*
  		 * Synchronize with task_work_cancel(). It can't remove
  		 * the first entry == work, cmpxchg(task_works) should
  		 * fail, but it can play with *work and other entries.
  		 */
  		raw_spin_unlock_wait(&task->pi_lock);
  		smp_mb();
e73f8959a   Oleg Nesterov   task_work_add: ge...
105

ac3d0da8f   Oleg Nesterov   task_work: Make t...
106
107
108
109
110
111
112
113
  		/* Reverse the list to run the works in fifo order */
  		head = NULL;
  		do {
  			next = work->next;
  			work->next = head;
  			head = work;
  			work = next;
  		} while (work);
e73f8959a   Oleg Nesterov   task_work_add: ge...
114

ac3d0da8f   Oleg Nesterov   task_work: Make t...
115
116
117
118
119
  		work = head;
  		do {
  			next = work->next;
  			work->func(work);
  			work = next;
f341861fb   Eric Dumazet   task_work: add a ...
120
  			cond_resched();
ac3d0da8f   Oleg Nesterov   task_work: Make t...
121
  		} while (work);
e73f8959a   Oleg Nesterov   task_work_add: ge...
122
123
  	}
  }