hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/kernel/sched/wait.c
....@@ -1,9 +1,11 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Generic waiting primitives.
34 *
45 * (C) 2004 Nadia Yvette Chambers, Oracle
56 */
67 #include "sched.h"
8
+#include <trace/hooks/sched.h>
79
810 void __init_waitqueue_head(struct wait_queue_head *wq_head, const char *name, struct lock_class_key *key)
911 {
....@@ -117,16 +119,12 @@
117119 bookmark.func = NULL;
118120 INIT_LIST_HEAD(&bookmark.entry);
119121
120
- spin_lock_irqsave(&wq_head->lock, flags);
121
- nr_exclusive = __wake_up_common(wq_head, mode, nr_exclusive, wake_flags, key, &bookmark);
122
- spin_unlock_irqrestore(&wq_head->lock, flags);
123
-
124
- while (bookmark.flags & WQ_FLAG_BOOKMARK) {
122
+ do {
125123 spin_lock_irqsave(&wq_head->lock, flags);
126124 nr_exclusive = __wake_up_common(wq_head, mode, nr_exclusive,
127125 wake_flags, key, &bookmark);
128126 spin_unlock_irqrestore(&wq_head->lock, flags);
129
- }
127
+ } while (bookmark.flags & WQ_FLAG_BOOKMARK);
130128 }
131129
132130 /**
....@@ -172,7 +170,6 @@
172170 * __wake_up_sync_key - wake up threads blocked on a waitqueue.
173171 * @wq_head: the waitqueue
174172 * @mode: which threads
175
- * @nr_exclusive: how many wake-one or wake-many threads to wake up
176173 * @key: opaque value to be passed to wakeup targets
177174 *
178175 * The sync wakeup differs that the waker knows that it will schedule
....@@ -186,26 +183,47 @@
186183 * accessing the task state.
187184 */
188185 void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode,
189
- int nr_exclusive, void *key)
186
+ void *key)
190187 {
191
- int wake_flags = 1; /* XXX WF_SYNC */
188
+ int wake_flags = WF_SYNC;
192189
193190 if (unlikely(!wq_head))
194191 return;
195192
196
- if (unlikely(nr_exclusive != 1))
197
- wake_flags = 0;
198
-
199
- __wake_up_common_lock(wq_head, mode, nr_exclusive, wake_flags, key);
193
+ trace_android_vh_set_wake_flags(&wake_flags, &mode);
194
+ __wake_up_common_lock(wq_head, mode, 1, wake_flags, key);
200195 }
201196 EXPORT_SYMBOL_GPL(__wake_up_sync_key);
197
+
198
+/**
199
+ * __wake_up_locked_sync_key - wake up a thread blocked on a locked waitqueue.
200
+ * @wq_head: the waitqueue
201
+ * @mode: which threads
202
+ * @key: opaque value to be passed to wakeup targets
203
+ *
204
+ * The sync wakeup differs in that the waker knows that it will schedule
205
+ * away soon, so while the target thread will be woken up, it will not
206
+ * be migrated to another CPU - ie. the two threads are 'synchronized'
207
+ * with each other. This can prevent needless bouncing between CPUs.
208
+ *
209
+ * On UP it can prevent extra preemption.
210
+ *
211
+ * If this function wakes up a task, it executes a full memory barrier before
212
+ * accessing the task state.
213
+ */
214
+void __wake_up_locked_sync_key(struct wait_queue_head *wq_head,
215
+ unsigned int mode, void *key)
216
+{
217
+ __wake_up_common(wq_head, mode, 1, WF_SYNC, key, NULL);
218
+}
219
+EXPORT_SYMBOL_GPL(__wake_up_locked_sync_key);
202220
203221 /*
204222 * __wake_up_sync - see __wake_up_sync_key()
205223 */
206
-void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode, int nr_exclusive)
224
+void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode)
207225 {
208
- __wake_up_sync_key(wq_head, mode, nr_exclusive, NULL);
226
+ __wake_up_sync_key(wq_head, mode, NULL);
209227 }
210228 EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
211229
....@@ -242,17 +260,22 @@
242260 }
243261 EXPORT_SYMBOL(prepare_to_wait);
244262
245
-void
263
+/* Returns true if we are the first waiter in the queue, false otherwise. */
264
+bool
246265 prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state)
247266 {
248267 unsigned long flags;
268
+ bool was_empty = false;
249269
250270 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
251271 spin_lock_irqsave(&wq_head->lock, flags);
252
- if (list_empty(&wq_entry->entry))
272
+ if (list_empty(&wq_entry->entry)) {
273
+ was_empty = list_empty(&wq_head->head);
253274 __add_wait_queue_entry_tail(wq_head, wq_entry);
275
+ }
254276 set_current_state(state);
255277 spin_unlock_irqrestore(&wq_head->lock, flags);
278
+ return was_empty;
256279 }
257280 EXPORT_SYMBOL(prepare_to_wait_exclusive);
258281
....@@ -271,7 +294,7 @@
271294 long ret = 0;
272295
273296 spin_lock_irqsave(&wq_head->lock, flags);
274
- if (unlikely(signal_pending_state(state, current))) {
297
+ if (signal_pending_state(state, current)) {
275298 /*
276299 * Exclusive waiter must not fail if it was selected by wakeup,
277300 * it should "consume" the condition we were waiting for.
....@@ -377,13 +400,13 @@
377400 }
378401 EXPORT_SYMBOL(finish_wait);
379402
380
-int __sched autoremove_wake_function(struct wait_queue_entry *wq_entry,
381
- unsigned int mode, int sync, void *key)
403
+__sched int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned int mode,
404
+ int sync, void *key)
382405 {
383406 int ret = default_wake_function(wq_entry, mode, sync, key);
384407
385408 if (ret)
386
- list_del_init(&wq_entry->entry);
409
+ list_del_init_careful(&wq_entry->entry);
387410
388411 return ret;
389412 }
....@@ -414,8 +437,7 @@
414437 * } smp_mb(); // C
415438 * remove_wait_queue(&wq_head, &wait); wq_entry->flags |= WQ_FLAG_WOKEN;
416439 */
417
-long __sched wait_woken(struct wait_queue_entry *wq_entry, unsigned int mode,
418
- long timeout)
440
+__sched long wait_woken(struct wait_queue_entry *wq_entry, unsigned int mode, long timeout)
419441 {
420442 /*
421443 * The below executes an smp_mb(), which matches with the full barrier
....@@ -440,8 +462,8 @@
440462 }
441463 EXPORT_SYMBOL(wait_woken);
442464
443
-int __sched woken_wake_function(struct wait_queue_entry *wq_entry,
444
- unsigned int mode, int sync, void *key)
465
+__sched int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned int mode,
466
+ int sync, void *key)
445467 {
446468 /* Pairs with the smp_store_mb() in wait_woken(). */
447469 smp_mb(); /* C */