These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / kernel / sched / swait.c
1 #include <linux/sched.h>
2 #include <linux/swait.h>
3 #include <linux/suspend.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_all_locked(struct swait_queue_head *q)
34 {
35         struct swait_queue *curr;
36         int wakes = 0;
37
38         while (!list_empty(&q->task_list)) {
39
40                 curr = list_first_entry(&q->task_list, typeof(*curr),
41                                         task_list);
42                 wake_up_process(curr->task);
43                 list_del_init(&curr->task_list);
44                 wakes++;
45         }
46         if (pm_in_action)
47                 return;
48         WARN(wakes > 2, "complate_all() with %d waiters\n", wakes);
49 }
50 EXPORT_SYMBOL(swake_up_all_locked);
51
52 void swake_up(struct swait_queue_head *q)
53 {
54         unsigned long flags;
55
56         if (!swait_active(q))
57                 return;
58
59         raw_spin_lock_irqsave(&q->lock, flags);
60         swake_up_locked(q);
61         raw_spin_unlock_irqrestore(&q->lock, flags);
62 }
63 EXPORT_SYMBOL(swake_up);
64
65 /*
66  * Does not allow usage from IRQ disabled, since we must be able to
67  * release IRQs to guarantee bounded hold time.
68  */
69 void swake_up_all(struct swait_queue_head *q)
70 {
71         struct swait_queue *curr;
72         LIST_HEAD(tmp);
73
74         if (!swait_active(q))
75                 return;
76
77         raw_spin_lock_irq(&q->lock);
78         list_splice_init(&q->task_list, &tmp);
79         while (!list_empty(&tmp)) {
80                 curr = list_first_entry(&tmp, typeof(*curr), task_list);
81
82                 wake_up_state(curr->task, TASK_NORMAL);
83                 list_del_init(&curr->task_list);
84
85                 if (list_empty(&tmp))
86                         break;
87
88                 raw_spin_unlock_irq(&q->lock);
89                 raw_spin_lock_irq(&q->lock);
90         }
91         raw_spin_unlock_irq(&q->lock);
92 }
93 EXPORT_SYMBOL(swake_up_all);
94
95 void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait)
96 {
97         wait->task = current;
98         if (list_empty(&wait->task_list))
99                 list_add(&wait->task_list, &q->task_list);
100 }
101
102 void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state)
103 {
104         unsigned long flags;
105
106         raw_spin_lock_irqsave(&q->lock, flags);
107         __prepare_to_swait(q, wait);
108         set_current_state(state);
109         raw_spin_unlock_irqrestore(&q->lock, flags);
110 }
111 EXPORT_SYMBOL(prepare_to_swait);
112
113 long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state)
114 {
115         if (signal_pending_state(state, current))
116                 return -ERESTARTSYS;
117
118         prepare_to_swait(q, wait, state);
119
120         return 0;
121 }
122 EXPORT_SYMBOL(prepare_to_swait_event);
123
124 void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
125 {
126         __set_current_state(TASK_RUNNING);
127         if (!list_empty(&wait->task_list))
128                 list_del_init(&wait->task_list);
129 }
130
131 void finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
132 {
133         unsigned long flags;
134
135         __set_current_state(TASK_RUNNING);
136
137         if (!list_empty_careful(&wait->task_list)) {
138                 raw_spin_lock_irqsave(&q->lock, flags);
139                 list_del_init(&wait->task_list);
140                 raw_spin_unlock_irqrestore(&q->lock, flags);
141         }
142 }
143 EXPORT_SYMBOL(finish_swait);