GNU Linux-libre 4.14.266-gnu1
[releases.git] / kernel / sched / swait.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/sched/signal.h>
3 #include <linux/swait.h>
4
5 void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
6                              struct lock_class_key *key)
7 {
8         raw_spin_lock_init(&q->lock);
9         lockdep_set_class_and_name(&q->lock, key, name);
10         INIT_LIST_HEAD(&q->task_list);
11 }
12 EXPORT_SYMBOL(__init_swait_queue_head);
13
14 /*
15  * The thing about the wake_up_state() return value; I think we can ignore it.
16  *
17  * If for some reason it would return 0, that means the previously waiting
18  * task is already running, so it will observe condition true (or has already).
19  */
20 void swake_up_locked(struct swait_queue_head *q)
21 {
22         struct swait_queue *curr;
23
24         if (list_empty(&q->task_list))
25                 return;
26
27         curr = list_first_entry(&q->task_list, typeof(*curr), task_list);
28         wake_up_process(curr->task);
29         list_del_init(&curr->task_list);
30 }
31 EXPORT_SYMBOL(swake_up_locked);
32
33 void swake_up(struct swait_queue_head *q)
34 {
35         unsigned long flags;
36
37         raw_spin_lock_irqsave(&q->lock, flags);
38         swake_up_locked(q);
39         raw_spin_unlock_irqrestore(&q->lock, flags);
40 }
41 EXPORT_SYMBOL(swake_up);
42
43 /*
44  * Does not allow usage from IRQ disabled, since we must be able to
45  * release IRQs to guarantee bounded hold time.
46  */
47 void swake_up_all(struct swait_queue_head *q)
48 {
49         struct swait_queue *curr;
50         LIST_HEAD(tmp);
51
52         raw_spin_lock_irq(&q->lock);
53         list_splice_init(&q->task_list, &tmp);
54         while (!list_empty(&tmp)) {
55                 curr = list_first_entry(&tmp, typeof(*curr), task_list);
56
57                 wake_up_state(curr->task, TASK_NORMAL);
58                 list_del_init(&curr->task_list);
59
60                 if (list_empty(&tmp))
61                         break;
62
63                 raw_spin_unlock_irq(&q->lock);
64                 raw_spin_lock_irq(&q->lock);
65         }
66         raw_spin_unlock_irq(&q->lock);
67 }
68 EXPORT_SYMBOL(swake_up_all);
69
70 void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait)
71 {
72         wait->task = current;
73         if (list_empty(&wait->task_list))
74                 list_add(&wait->task_list, &q->task_list);
75 }
76
77 void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state)
78 {
79         unsigned long flags;
80
81         raw_spin_lock_irqsave(&q->lock, flags);
82         __prepare_to_swait(q, wait);
83         set_current_state(state);
84         raw_spin_unlock_irqrestore(&q->lock, flags);
85 }
86 EXPORT_SYMBOL(prepare_to_swait);
87
88 long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state)
89 {
90         if (signal_pending_state(state, current))
91                 return -ERESTARTSYS;
92
93         prepare_to_swait(q, wait, state);
94
95         return 0;
96 }
97 EXPORT_SYMBOL(prepare_to_swait_event);
98
99 void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
100 {
101         __set_current_state(TASK_RUNNING);
102         if (!list_empty(&wait->task_list))
103                 list_del_init(&wait->task_list);
104 }
105
106 void finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
107 {
108         unsigned long flags;
109
110         __set_current_state(TASK_RUNNING);
111
112         if (!list_empty_careful(&wait->task_list)) {
113                 raw_spin_lock_irqsave(&q->lock, flags);
114                 list_del_init(&wait->task_list);
115                 raw_spin_unlock_irqrestore(&q->lock, flags);
116         }
117 }
118 EXPORT_SYMBOL(finish_swait);