^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Generic wait-for-completion handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * It differs from semaphores in that their default case is the opposite,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * wait_for_completion default blocks whereas semaphore default non-block. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * interface also makes it easy to 'complete' multiple waiting threads,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * something which isn't entirely natural for semaphores.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * But more importantly, the primitive documents the usage. Semaphores would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * typically be used for exclusion which gives rise to priority inversion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Waiting for completion is a typically sync point, but not an exclusion point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "sched.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * complete: - signals a single thread waiting on this completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * @x: holds the state of this particular completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * This will wake up a single thread waiting on this completion. Threads will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * awakened in the same order in which they were queued.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * See also complete_all(), wait_for_completion() and related routines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * If this function wakes up a task, it executes a full memory barrier before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * accessing the task state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) void complete(struct completion *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) raw_spin_lock_irqsave(&x->wait.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (x->done != UINT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) x->done++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) swake_up_locked(&x->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) raw_spin_unlock_irqrestore(&x->wait.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) EXPORT_SYMBOL(complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * complete_all: - signals all threads waiting on this completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * @x: holds the state of this particular completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * This will wake up all threads waiting on this particular completion event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * If this function wakes up a task, it executes a full memory barrier before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * accessing the task state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * Since complete_all() sets the completion of @x permanently to done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * to allow multiple waiters to finish, a call to reinit_completion()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * must be used on @x if @x is to be used again. The code must make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * sure that all waiters have woken and finished before reinitializing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * @x. Also note that the function completion_done() can not be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * to know if there are still waiters after complete_all() has been called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) void complete_all(struct completion *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) lockdep_assert_RT_in_threaded_ctx();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) raw_spin_lock_irqsave(&x->wait.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) x->done = UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) swake_up_all_locked(&x->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) raw_spin_unlock_irqrestore(&x->wait.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) EXPORT_SYMBOL(complete_all);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static inline long __sched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) do_wait_for_common(struct completion *x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) long (*action)(long), long timeout, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (!x->done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) DECLARE_SWAITQUEUE(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (signal_pending_state(state, current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) timeout = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) __prepare_to_swait(&x->wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) __set_current_state(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) raw_spin_unlock_irq(&x->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) timeout = action(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) raw_spin_lock_irq(&x->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) } while (!x->done && timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) __finish_swait(&x->wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!x->done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (x->done != UINT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) x->done--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return timeout ?: 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static inline long __sched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) __wait_for_common(struct completion *x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) long (*action)(long), long timeout, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) might_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) complete_acquire(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) raw_spin_lock_irq(&x->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) timeout = do_wait_for_common(x, action, timeout, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) raw_spin_unlock_irq(&x->wait.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) complete_release(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static long __sched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) wait_for_common(struct completion *x, long timeout, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return __wait_for_common(x, schedule_timeout, timeout, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static long __sched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) wait_for_common_io(struct completion *x, long timeout, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return __wait_for_common(x, io_schedule_timeout, timeout, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * wait_for_completion: - waits for completion of a task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * @x: holds the state of this particular completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * This waits to be signaled for completion of a specific task. It is NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * interruptible and there is no timeout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * and interrupt capability. Also see complete().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) void __sched wait_for_completion(struct completion *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) EXPORT_SYMBOL(wait_for_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * @x: holds the state of this particular completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * @timeout: timeout value in jiffies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * This waits for either a completion of a specific task to be signaled or for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * specified timeout to expire. The timeout is in jiffies. It is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * interruptible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * Return: 0 if timed out, and positive (at least 1, or number of jiffies left
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * till timeout) if completed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) unsigned long __sched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) wait_for_completion_timeout(struct completion *x, unsigned long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) EXPORT_SYMBOL(wait_for_completion_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * wait_for_completion_io: - waits for completion of a task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * @x: holds the state of this particular completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * This waits to be signaled for completion of a specific task. It is NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * interruptible and there is no timeout. The caller is accounted as waiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * for IO (which traditionally means blkio only).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) void __sched wait_for_completion_io(struct completion *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) wait_for_common_io(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) EXPORT_SYMBOL(wait_for_completion_io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * wait_for_completion_io_timeout: - waits for completion of a task (w/timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * @x: holds the state of this particular completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * @timeout: timeout value in jiffies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * This waits for either a completion of a specific task to be signaled or for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * specified timeout to expire. The timeout is in jiffies. It is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * interruptible. The caller is accounted as waiting for IO (which traditionally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * means blkio only).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * Return: 0 if timed out, and positive (at least 1, or number of jiffies left
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * till timeout) if completed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) unsigned long __sched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) wait_for_completion_io_timeout(struct completion *x, unsigned long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return wait_for_common_io(x, timeout, TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) EXPORT_SYMBOL(wait_for_completion_io_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * @x: holds the state of this particular completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * This waits for completion of a specific task to be signaled. It is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * interruptible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * Return: -ERESTARTSYS if interrupted, 0 if completed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int __sched wait_for_completion_interruptible(struct completion *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (t == -ERESTARTSYS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) EXPORT_SYMBOL(wait_for_completion_interruptible);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * @x: holds the state of this particular completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * @timeout: timeout value in jiffies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * This waits for either a completion of a specific task to be signaled or for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * specified timeout to expire. It is interruptible. The timeout is in jiffies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * Return: -ERESTARTSYS if interrupted, 0 if timed out, positive (at least 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * or number of jiffies left till timeout) if completed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) long __sched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) wait_for_completion_interruptible_timeout(struct completion *x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) unsigned long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * wait_for_completion_killable: - waits for completion of a task (killable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * @x: holds the state of this particular completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * This waits to be signaled for completion of a specific task. It can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * interrupted by a kill signal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * Return: -ERESTARTSYS if interrupted, 0 if completed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) int __sched wait_for_completion_killable(struct completion *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (t == -ERESTARTSYS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) EXPORT_SYMBOL(wait_for_completion_killable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * @x: holds the state of this particular completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * @timeout: timeout value in jiffies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * This waits for either a completion of a specific task to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * signaled or for a specified timeout to expire. It can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * interrupted by a kill signal. The timeout is in jiffies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * Return: -ERESTARTSYS if interrupted, 0 if timed out, positive (at least 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * or number of jiffies left till timeout) if completed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) long __sched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) wait_for_completion_killable_timeout(struct completion *x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) unsigned long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return wait_for_common(x, timeout, TASK_KILLABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) EXPORT_SYMBOL(wait_for_completion_killable_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * try_wait_for_completion - try to decrement a completion without blocking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * @x: completion structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * Return: 0 if a decrement cannot be done without blocking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * 1 if a decrement succeeded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * If a completion is being used as a counting completion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * attempt to decrement the counter without blocking. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * enables us to avoid waiting if the resource the completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * is protecting is not available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) bool try_wait_for_completion(struct completion *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) bool ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * Since x->done will need to be locked only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * in the non-blocking case, we check x->done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * first without taking the lock so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * return early in the blocking case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (!READ_ONCE(x->done))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) raw_spin_lock_irqsave(&x->wait.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (!x->done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) else if (x->done != UINT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) x->done--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) raw_spin_unlock_irqrestore(&x->wait.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) EXPORT_SYMBOL(try_wait_for_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * completion_done - Test to see if a completion has any waiters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * @x: completion structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * Return: 0 if there are waiters (wait_for_completion() in progress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * 1 if there are no waiters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * Note, this will always return true if complete_all() was called on @X.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) bool completion_done(struct completion *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (!READ_ONCE(x->done))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * If ->done, we need to wait for complete() to release ->wait.lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * otherwise we can end up freeing the completion before complete()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * is done referencing it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) raw_spin_lock_irqsave(&x->wait.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) raw_spin_unlock_irqrestore(&x->wait.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) EXPORT_SYMBOL(completion_done);