patch-5.15.79-rt54.patch
[platform/kernel/linux-rpi.git] / kernel / sched / swait.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * <linux/swait.h> (simple wait queues ) implementation:
4  */
5 #include "sched.h"
6
7 void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
8                              struct lock_class_key *key)
9 {
10         raw_spin_lock_init(&q->lock);
11         lockdep_set_class_and_name(&q->lock, key, name);
12         INIT_LIST_HEAD(&q->task_list);
13 }
14 EXPORT_SYMBOL(__init_swait_queue_head);
15
16 /*
17  * The thing about the wake_up_state() return value; I think we can ignore it.
18  *
19  * If for some reason it would return 0, that means the previously waiting
20  * task is already running, so it will observe condition true (or has already).
21  */
22 void swake_up_locked(struct swait_queue_head *q)
23 {
24         struct swait_queue *curr;
25
26         if (list_empty(&q->task_list))
27                 return;
28
29         curr = list_first_entry(&q->task_list, typeof(*curr), task_list);
30         wake_up_process(curr->task);
31         list_del_init(&curr->task_list);
32 }
33 EXPORT_SYMBOL(swake_up_locked);
34
35 /*
36  * Wake up all waiters. This is an interface which is solely exposed for
37  * completions and not for general usage.
38  *
39  * It is intentionally different from swake_up_all() to allow usage from
40  * hard interrupt context and interrupt disabled regions.
41  */
42 void swake_up_all_locked(struct swait_queue_head *q)
43 {
44         while (!list_empty(&q->task_list))
45                 swake_up_locked(q);
46 }
47
48 void swake_up_one(struct swait_queue_head *q)
49 {
50         unsigned long flags;
51
52         raw_spin_lock_irqsave(&q->lock, flags);
53         swake_up_locked(q);
54         raw_spin_unlock_irqrestore(&q->lock, flags);
55 }
56 EXPORT_SYMBOL(swake_up_one);
57
58 /*
59  * Does not allow usage from IRQ disabled, since we must be able to
60  * release IRQs to guarantee bounded hold time.
61  */
62 void swake_up_all(struct swait_queue_head *q)
63 {
64         struct swait_queue *curr;
65         LIST_HEAD(tmp);
66
67         WARN_ON(irqs_disabled());
68         raw_spin_lock_irq(&q->lock);
69         list_splice_init(&q->task_list, &tmp);
70         while (!list_empty(&tmp)) {
71                 curr = list_first_entry(&tmp, typeof(*curr), task_list);
72
73                 wake_up_state(curr->task, TASK_NORMAL);
74                 list_del_init(&curr->task_list);
75
76                 if (list_empty(&tmp))
77                         break;
78
79                 raw_spin_unlock_irq(&q->lock);
80                 raw_spin_lock_irq(&q->lock);
81         }
82         raw_spin_unlock_irq(&q->lock);
83 }
84 EXPORT_SYMBOL(swake_up_all);
85
86 void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait)
87 {
88         wait->task = current;
89         if (list_empty(&wait->task_list))
90                 list_add_tail(&wait->task_list, &q->task_list);
91 }
92
93 void prepare_to_swait_exclusive(struct swait_queue_head *q, struct swait_queue *wait, int state)
94 {
95         unsigned long flags;
96
97         raw_spin_lock_irqsave(&q->lock, flags);
98         __prepare_to_swait(q, wait);
99         set_current_state(state);
100         raw_spin_unlock_irqrestore(&q->lock, flags);
101 }
102 EXPORT_SYMBOL(prepare_to_swait_exclusive);
103
104 long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state)
105 {
106         unsigned long flags;
107         long ret = 0;
108
109         raw_spin_lock_irqsave(&q->lock, flags);
110         if (signal_pending_state(state, current)) {
111                 /*
112                  * See prepare_to_wait_event(). TL;DR, subsequent swake_up_one()
113                  * must not see us.
114                  */
115                 list_del_init(&wait->task_list);
116                 ret = -ERESTARTSYS;
117         } else {
118                 __prepare_to_swait(q, wait);
119                 set_current_state(state);
120         }
121         raw_spin_unlock_irqrestore(&q->lock, flags);
122
123         return ret;
124 }
125 EXPORT_SYMBOL(prepare_to_swait_event);
126
127 void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
128 {
129         __set_current_state(TASK_RUNNING);
130         if (!list_empty(&wait->task_list))
131                 list_del_init(&wait->task_list);
132 }
133
134 void finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
135 {
136         unsigned long flags;
137
138         __set_current_state(TASK_RUNNING);
139
140         if (!list_empty_careful(&wait->task_list)) {
141                 raw_spin_lock_irqsave(&q->lock, flags);
142                 list_del_init(&wait->task_list);
143                 raw_spin_unlock_irqrestore(&q->lock, flags);
144         }
145 }
146 EXPORT_SYMBOL(finish_swait);