^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Fast Userspace Mutexes (which I call "Futexes!").
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * (C) Rusty Russell, IBM 2002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Generalized futexes, futex requeueing, misc fixes by Ingo Molnar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * (C) Copyright 2003 Red Hat Inc, All Rights Reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Removed page pinning, fix privately mapped COW pages and other cleanups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * (C) Copyright 2003, 2004 Jamie Lokier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Robust futex support started by Ingo Molnar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * (C) Copyright 2006 Red Hat Inc, All Rights Reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Thanks to Thomas Gleixner for suggestions, analysis and fixes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * PI-futex support started by Ingo Molnar and Thomas Gleixner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Copyright (C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * PRIVATE futexes by Eric Dumazet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Copyright (C) 2007 Eric Dumazet <dada1@cosmosbay.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Requeue-PI support by Darren Hart <dvhltc@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Copyright (C) IBM Corporation, 2009
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * Thanks to Thomas Gleixner for conceptual design and careful reviews.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * enough at me, Linus for the original (flawed) idea, Matthew
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * Kirkwood for proof-of-concept implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * "The futexes are also cursed."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * "But they come in a choice of three flavours!"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/jhash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/freezer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <linux/fault-inject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <linux/time_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <asm/futex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include "locking/rtmutex_common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <trace/hooks/futex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * READ this before attempting to hack on futexes!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * Basic futex operation and ordering guarantees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * =============================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * The waiter reads the futex value in user space and calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * futex_wait(). This function computes the hash bucket and acquires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * the hash bucket lock. After that it reads the futex user space value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * again and verifies that the data has not changed. If it has not changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * it enqueues itself into the hash bucket, releases the hash bucket lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * and schedules.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * The waker side modifies the user space value of the futex and calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * futex_wake(). This function computes the hash bucket and acquires the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * hash bucket lock. Then it looks for waiters on that futex in the hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * bucket and wakes them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * In futex wake up scenarios where no tasks are blocked on a futex, taking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * the hb spinlock can be avoided and simply return. In order for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * optimization to work, ordering guarantees must exist so that the waiter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * being added to the list is acknowledged when the list is concurrently being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * checked by the waker, avoiding scenarios like the following:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * CPU 0 CPU 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * val = *futex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * sys_futex(WAIT, futex, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * futex_wait(futex, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * uval = *futex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * *futex = newval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * sys_futex(WAKE, futex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * futex_wake(futex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * if (queue_empty())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * if (uval == val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * lock(hash_bucket(futex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * queue();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * unlock(hash_bucket(futex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * This would cause the waiter on CPU 0 to wait forever because it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * missed the transition of the user space value from val to newval
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * and the waker did not find the waiter in the hash bucket queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * The correct serialization ensures that a waiter either observes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * the changed user space value before blocking or is woken by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * concurrent waker:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * CPU 0 CPU 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * val = *futex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * sys_futex(WAIT, futex, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * futex_wait(futex, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * waiters++; (a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * smp_mb(); (A) <-- paired with -.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * lock(hash_bucket(futex)); |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * uval = *futex; |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * | *futex = newval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * | sys_futex(WAKE, futex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * | futex_wake(futex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * `--------> smp_mb(); (B)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * if (uval == val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * queue();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * unlock(hash_bucket(futex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * schedule(); if (waiters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * lock(hash_bucket(futex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * else wake_waiters(futex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * waiters--; (b) unlock(hash_bucket(futex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * Where (A) orders the waiters increment and the futex value read through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * atomic operations (see hb_waiters_inc) and where (B) orders the write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * to futex and the waiters read (see hb_waiters_pending()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * This yields the following case (where X:=waiters, Y:=futex):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * X = Y = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * w[X]=1 w[Y]=1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * MB MB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * r[Y]=y r[X]=x
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * Which guarantees that x==0 && y==0 is impossible; which translates back into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * the guarantee that we cannot both miss the futex variable change and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * enqueue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * Note that a new waiter is accounted for in (a) even when it is possible that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * the wait call can return error, in which case we backtrack from it in (b).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * Refer to the comment in queue_lock().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * Similarly, in order to account for waiters being requeued on another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * address we always increment the waiters for the destination bucket before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * acquiring the lock. It then decrements them again after releasing it -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * the code that actually moves the futex(es) between hash buckets (requeue_futex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * will do the additional required waiter count housekeeping. This is done for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * double_lock_hb() and double_unlock_hb(), respectively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #ifdef CONFIG_HAVE_FUTEX_CMPXCHG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #define futex_cmpxchg_enabled 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static int __read_mostly futex_cmpxchg_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * Futex flags used to encode options to functions and preserve them across
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * restarts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) # define FLAGS_SHARED 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * NOMMU does not have per process address space. Let the compiler optimize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * code away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) # define FLAGS_SHARED 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) #define FLAGS_CLOCKRT 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) #define FLAGS_HAS_TIMEOUT 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * Priority Inheritance state:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct futex_pi_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * list of 'owned' pi_state instances - these have to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * cleaned up in do_exit() if the task exits prematurely:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * The PI object:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct rt_mutex pi_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct task_struct *owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) refcount_t refcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) union futex_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) } __randomize_layout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * struct futex_q - The hashed futex queue entry, one per waiting task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * @list: priority-sorted list of tasks waiting on this futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * @task: the task waiting on the futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * @lock_ptr: the hash bucket lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * @key: the key the futex is hashed on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * @pi_state: optional priority inheritance state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * @rt_waiter: rt_waiter storage for use with requeue_pi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * @requeue_pi_key: the requeue_pi target futex key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * @bitset: bitset for the optional bitmasked wakeup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * We use this hashed waitqueue, instead of a normal wait_queue_entry_t, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * we can wake only the relevant ones (hashed queues may be shared).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * A futex_q has a woken state, just like tasks have TASK_RUNNING.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * The order of wakeup is always to make the first condition true, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * the second.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * PI futexes are typically woken before they are removed from the hash list via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * the rt_mutex code. See unqueue_me_pi().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct futex_q {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct plist_node list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) spinlock_t *lock_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) union futex_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct futex_pi_state *pi_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct rt_mutex_waiter *rt_waiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) union futex_key *requeue_pi_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) u32 bitset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) } __randomize_layout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static const struct futex_q futex_q_init = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /* list gets initialized in queue_me()*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) .key = FUTEX_KEY_INIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) .bitset = FUTEX_BITSET_MATCH_ANY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * Hash buckets are shared by all the futex_keys that hash to the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * location. Each key may have multiple futex_q structures, one for each task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * waiting on a futex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct futex_hash_bucket {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) atomic_t waiters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) struct plist_head chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) } ____cacheline_aligned_in_smp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * The base of the bucket array and its size are always used together
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * (after initialization only in hash_futex()), so ensure that they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * reside in the same cacheline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct futex_hash_bucket *queues;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) unsigned long hashsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) } __futex_data __read_mostly __aligned(2*sizeof(long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) #define futex_queues (__futex_data.queues)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) #define futex_hashsize (__futex_data.hashsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * Fault injections for futexes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) #ifdef CONFIG_FAIL_FUTEX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct fault_attr attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) bool ignore_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) } fail_futex = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) .attr = FAULT_ATTR_INITIALIZER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) .ignore_private = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static int __init setup_fail_futex(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return setup_fault_attr(&fail_futex.attr, str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) __setup("fail_futex=", setup_fail_futex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static bool should_fail_futex(bool fshared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (fail_futex.ignore_private && !fshared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return should_fail(&fail_futex.attr, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static int __init fail_futex_debugfs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct dentry *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) dir = fault_create_debugfs_attr("fail_futex", NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) &fail_futex.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (IS_ERR(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return PTR_ERR(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) debugfs_create_bool("ignore-private", mode, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) &fail_futex.ignore_private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) late_initcall(fail_futex_debugfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static inline bool should_fail_futex(bool fshared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) #endif /* CONFIG_FAIL_FUTEX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static void compat_exit_robust_list(struct task_struct *curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static inline void compat_exit_robust_list(struct task_struct *curr) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * Reflects a new waiter being added to the waitqueue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static inline void hb_waiters_inc(struct futex_hash_bucket *hb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) atomic_inc(&hb->waiters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * Full barrier (A), see the ordering comment above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) smp_mb__after_atomic();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * Reflects a waiter being removed from the waitqueue by wakeup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * paths.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static inline void hb_waiters_dec(struct futex_hash_bucket *hb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) atomic_dec(&hb->waiters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static inline int hb_waiters_pending(struct futex_hash_bucket *hb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * Full barrier (B), see the ordering comment above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return atomic_read(&hb->waiters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * hash_futex - Return the hash bucket in the global hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * @key: Pointer to the futex key for which the hash is calculated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * We hash on the keys returned from get_futex_key (see below) and return the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * corresponding hash bucket in the global hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static struct futex_hash_bucket *hash_futex(union futex_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) u32 hash = jhash2((u32 *)key, offsetof(typeof(*key), both.offset) / 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) key->both.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return &futex_queues[hash & (futex_hashsize - 1)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * match_futex - Check whether two futex keys are equal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * @key1: Pointer to key1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * @key2: Pointer to key2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * Return 1 if two futex_keys are equal, 0 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static inline int match_futex(union futex_key *key1, union futex_key *key2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return (key1 && key2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) && key1->both.word == key2->both.word
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) && key1->both.ptr == key2->both.ptr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) && key1->both.offset == key2->both.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) enum futex_access {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) FUTEX_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) FUTEX_WRITE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * futex_setup_timer - set up the sleeping hrtimer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * @time: ptr to the given timeout value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * @timeout: the hrtimer_sleeper structure to be set up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * @flags: futex flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * @range_ns: optional range in ns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * Return: Initialized hrtimer_sleeper structure or NULL if no timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * value given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) static inline struct hrtimer_sleeper *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) futex_setup_timer(ktime_t *time, struct hrtimer_sleeper *timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) int flags, u64 range_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (!time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) hrtimer_init_sleeper_on_stack(timeout, (flags & FLAGS_CLOCKRT) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) CLOCK_REALTIME : CLOCK_MONOTONIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) HRTIMER_MODE_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * If range_ns is 0, calling hrtimer_set_expires_range_ns() is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * effectively the same as calling hrtimer_set_expires().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) hrtimer_set_expires_range_ns(&timeout->timer, *time, range_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * Generate a machine wide unique identifier for this inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * This relies on u64 not wrapping in the life-time of the machine; which with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * 1ns resolution means almost 585 years.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * This further relies on the fact that a well formed program will not unmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * the file while it has a (shared) futex waiting on it. This mapping will have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * a file reference which pins the mount and inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * If for some reason an inode gets evicted and read back in again, it will get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) * a new sequence number and will _NOT_ match, even though it is the exact same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) * file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * It is important that match_futex() will never have a false-positive, esp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * for PI futexes that can mess up the state. The above argues that false-negatives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) * are only possible for malformed programs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) static u64 get_inode_sequence_number(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static atomic64_t i_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) u64 old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) /* Does the inode already have a sequence number? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) old = atomic64_read(&inode->i_sequence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if (likely(old))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) return old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) u64 new = atomic64_add_return(1, &i_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (WARN_ON_ONCE(!new))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) old = atomic64_cmpxchg_relaxed(&inode->i_sequence, 0, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) return old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) return new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * get_futex_key() - Get parameters which are the keys for a futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * @uaddr: virtual address of the futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * @fshared: false for a PROCESS_PRIVATE futex, true for PROCESS_SHARED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * @key: address where result is stored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * @rw: mapping needs to be read/write (values: FUTEX_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * FUTEX_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * Return: a negative error code or 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * The key words are stored in @key on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * For shared mappings (when @fshared), the key is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * ( inode->i_sequence, page->index, offset_within_page )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * [ also see get_inode_sequence_number() ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * For private mappings (or when !@fshared), the key is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * ( current->mm, address, 0 )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * This allows (cross process, where applicable) identification of the futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * without keeping the page pinned for the duration of the FUTEX_WAIT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * lock_page() might sleep, the caller should not hold a spinlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) static int get_futex_key(u32 __user *uaddr, bool fshared, union futex_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) enum futex_access rw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) unsigned long address = (unsigned long)uaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) struct mm_struct *mm = current->mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) struct page *page, *tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct address_space *mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) int err, ro = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) * The futex address must be "naturally" aligned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) key->both.offset = address % PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) if (unlikely((address % sizeof(u32)) != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) address -= key->both.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) if (unlikely(!access_ok(uaddr, sizeof(u32))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (unlikely(should_fail_futex(fshared)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) * PROCESS_PRIVATE futexes are fast.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) * As the mm cannot disappear under us and the 'key' only needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) * virtual address, we dont even have to find the underlying vma.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) * Note : We do have to check 'uaddr' is a valid user address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * but access_ok() should be faster than find_vma()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) if (!fshared) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) key->private.mm = mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) key->private.address = address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) /* Ignore any VERIFY_READ mapping (futex common case) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) if (unlikely(should_fail_futex(true)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) err = get_user_pages_fast(address, 1, FOLL_WRITE, &page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * If write access is not required (eg. FUTEX_WAIT), try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * and get read-only access.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (err == -EFAULT && rw == FUTEX_READ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) err = get_user_pages_fast(address, 1, 0, &page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) ro = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) * The treatment of mapping from this point on is critical. The page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * lock protects many things but in this context the page lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * stabilizes mapping, prevents inode freeing in the shared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * file-backed region case and guards against movement to swap cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) * Strictly speaking the page lock is not needed in all cases being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) * considered here and page lock forces unnecessarily serialization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * From this point on, mapping will be re-verified if necessary and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * page lock will be acquired only if it is unavoidable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * Mapping checks require the head page for any compound page so the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * head page and mapping is looked up now. For anonymous pages, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * does not matter if the page splits in the future as the key is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * based on the address. For filesystem-backed pages, the tail is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * required as the index of the page determines the key. For
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * base pages, there is no tail page and tail == page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) tail = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) page = compound_head(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) mapping = READ_ONCE(page->mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) * If page->mapping is NULL, then it cannot be a PageAnon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) * page; but it might be the ZERO_PAGE or in the gate area or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) * in a special mapping (all cases which we are happy to fail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) * or it may have been a good file page when get_user_pages_fast
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) * found it, but truncated or holepunched or subjected to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * invalidate_complete_page2 before we got the page lock (also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) * cases which we are happy to fail). And we hold a reference,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) * so refcount care in invalidate_complete_page's remove_mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * prevents drop_caches from setting mapping to NULL beneath us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * The case we do have to guard against is when memory pressure made
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) * shmem_writepage move it from filecache to swapcache beneath us:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) * an unlikely race, but we do need to retry for page->mapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (unlikely(!mapping)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) int shmem_swizzled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * Page lock is required to identify which special case above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * applies. If this is really a shmem page then the page lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * will prevent unexpected transitions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) shmem_swizzled = PageSwapCache(page) || page->mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) put_user_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) if (shmem_swizzled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) * Private mappings are handled in a simple way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) * If the futex key is stored on an anonymous page, then the associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) * object is the mm which is implicitly pinned by the calling process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) * NOTE: When userspace waits on a MAP_SHARED mapping, even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) * it's a read-only handle, it's expected that futexes attach to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) * the object not the particular process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (PageAnon(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) * A RO anonymous page will never change and thus doesn't make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * sense for futex operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (unlikely(should_fail_futex(true)) || ro) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) key->private.mm = mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) key->private.address = address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) * The associated futex object in this case is the inode and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) * the page->mapping must be traversed. Ordinarily this should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) * be stabilised under page lock but it's not strictly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) * necessary in this case as we just want to pin the inode, not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) * update the radix tree or anything like that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) * The RCU read lock is taken as the inode is finally freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) * under RCU. If the mapping still matches expectations then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) * mapping->host can be safely accessed as being a valid inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) if (READ_ONCE(page->mapping) != mapping) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) put_user_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) inode = READ_ONCE(mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) put_user_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) key->both.offset |= FUT_OFF_INODE; /* inode-based key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) key->shared.i_seq = get_inode_sequence_number(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) key->shared.pgoff = page_to_pgoff(tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) put_user_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) * fault_in_user_writeable() - Fault in user address and verify RW access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) * @uaddr: pointer to faulting user space address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) * Slow path to fixup the fault we just took in the atomic write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) * access to @uaddr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) * We have no generic implementation of a non-destructive write to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) * user address. We know that we faulted in the atomic pagefault
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) * disabled section so we can as well avoid the #PF overhead by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) * calling get_user_pages() right away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) static int fault_in_user_writeable(u32 __user *uaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) struct mm_struct *mm = current->mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) mmap_read_lock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) ret = fixup_user_fault(mm, (unsigned long)uaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) FAULT_FLAG_WRITE, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) mmap_read_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) return ret < 0 ? ret : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) * futex_top_waiter() - Return the highest priority waiter on a futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) * @hb: the hash bucket the futex_q's reside in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) * @key: the futex key (to distinguish it from other futex futex_q's)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) * Must be called with the hb lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) static struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) union futex_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) struct futex_q *this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) plist_for_each_entry(this, &hb->chain, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (match_futex(&this->key, key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) return this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) static int cmpxchg_futex_value_locked(u32 *curval, u32 __user *uaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) u32 uval, u32 newval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) pagefault_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) ret = futex_atomic_cmpxchg_inatomic(curval, uaddr, uval, newval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) pagefault_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) static int get_futex_value_locked(u32 *dest, u32 __user *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) pagefault_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) ret = __get_user(*dest, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) pagefault_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) return ret ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) * PI code:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) static int refill_pi_state_cache(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) struct futex_pi_state *pi_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) if (likely(current->pi_state_cache))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) if (!pi_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) INIT_LIST_HEAD(&pi_state->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) /* pi_mutex gets initialized later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) pi_state->owner = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) refcount_set(&pi_state->refcount, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) pi_state->key = FUTEX_KEY_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) current->pi_state_cache = pi_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) static struct futex_pi_state *alloc_pi_state(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) struct futex_pi_state *pi_state = current->pi_state_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) WARN_ON(!pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) current->pi_state_cache = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) return pi_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) static void pi_state_update_owner(struct futex_pi_state *pi_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) struct task_struct *new_owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) struct task_struct *old_owner = pi_state->owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) lockdep_assert_held(&pi_state->pi_mutex.wait_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) if (old_owner) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) raw_spin_lock(&old_owner->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) WARN_ON(list_empty(&pi_state->list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) list_del_init(&pi_state->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) raw_spin_unlock(&old_owner->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (new_owner) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) raw_spin_lock(&new_owner->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) WARN_ON(!list_empty(&pi_state->list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) list_add(&pi_state->list, &new_owner->pi_state_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) pi_state->owner = new_owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) raw_spin_unlock(&new_owner->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) static void get_pi_state(struct futex_pi_state *pi_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) WARN_ON_ONCE(!refcount_inc_not_zero(&pi_state->refcount));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) * Drops a reference to the pi_state object and frees or caches it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) * when the last reference is gone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) static void put_pi_state(struct futex_pi_state *pi_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) if (!pi_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) if (!refcount_dec_and_test(&pi_state->refcount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) * If pi_state->owner is NULL, the owner is most probably dying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) * and has cleaned up the pi_state already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) if (pi_state->owner) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) raw_spin_lock_irqsave(&pi_state->pi_mutex.wait_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) pi_state_update_owner(pi_state, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) rt_mutex_proxy_unlock(&pi_state->pi_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) raw_spin_unlock_irqrestore(&pi_state->pi_mutex.wait_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) if (current->pi_state_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) kfree(pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) * pi_state->list is already empty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) * clear pi_state->owner.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) * refcount is at 0 - put it back to 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) pi_state->owner = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) refcount_set(&pi_state->refcount, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) current->pi_state_cache = pi_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) #ifdef CONFIG_FUTEX_PI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) * This task is holding PI mutexes at exit time => bad.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) * Kernel cleans up PI-state, but userspace is likely hosed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) * (Robust-futex cleanup is separate and might save the day for userspace.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) static void exit_pi_state_list(struct task_struct *curr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) struct list_head *next, *head = &curr->pi_state_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) struct futex_pi_state *pi_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) struct futex_hash_bucket *hb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) union futex_key key = FUTEX_KEY_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) if (!futex_cmpxchg_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) * We are a ZOMBIE and nobody can enqueue itself on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) * pi_state_list anymore, but we have to be careful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) * versus waiters unqueueing themselves:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) raw_spin_lock_irq(&curr->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) while (!list_empty(head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) next = head->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) pi_state = list_entry(next, struct futex_pi_state, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) key = pi_state->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) hb = hash_futex(&key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) * We can race against put_pi_state() removing itself from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) * list (a waiter going away). put_pi_state() will first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) * decrement the reference count and then modify the list, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) * its possible to see the list entry but fail this reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) * acquire.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) * In that case; drop the locks to let put_pi_state() make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) * progress and retry the loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) if (!refcount_inc_not_zero(&pi_state->refcount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) raw_spin_unlock_irq(&curr->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) cpu_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) raw_spin_lock_irq(&curr->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) raw_spin_unlock_irq(&curr->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) spin_lock(&hb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) raw_spin_lock(&curr->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) * We dropped the pi-lock, so re-check whether this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) * task still owns the PI-state:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) if (head->next != next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) /* retain curr->pi_lock for the loop invariant */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) spin_unlock(&hb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) put_pi_state(pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) WARN_ON(pi_state->owner != curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) WARN_ON(list_empty(&pi_state->list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) list_del_init(&pi_state->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) pi_state->owner = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) raw_spin_unlock(&curr->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) spin_unlock(&hb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) rt_mutex_futex_unlock(&pi_state->pi_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) put_pi_state(pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) raw_spin_lock_irq(&curr->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) raw_spin_unlock_irq(&curr->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) static inline void exit_pi_state_list(struct task_struct *curr) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) * We need to check the following states:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) * Waiter | pi_state | pi->owner | uTID | uODIED | ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) * [1] NULL | --- | --- | 0 | 0/1 | Valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) * [2] NULL | --- | --- | >0 | 0/1 | Valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) * [3] Found | NULL | -- | Any | 0/1 | Invalid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) * [4] Found | Found | NULL | 0 | 1 | Valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) * [5] Found | Found | NULL | >0 | 1 | Invalid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) * [6] Found | Found | task | 0 | 1 | Valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) * [7] Found | Found | NULL | Any | 0 | Invalid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) * [8] Found | Found | task | ==taskTID | 0/1 | Valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) * [9] Found | Found | task | 0 | 0 | Invalid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) * [10] Found | Found | task | !=taskTID | 0/1 | Invalid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) * [1] Indicates that the kernel can acquire the futex atomically. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) * came here due to a stale FUTEX_WAITERS/FUTEX_OWNER_DIED bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) * [2] Valid, if TID does not belong to a kernel thread. If no matching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) * thread is found then it indicates that the owner TID has died.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) * [3] Invalid. The waiter is queued on a non PI futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) * [4] Valid state after exit_robust_list(), which sets the user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) * value to FUTEX_WAITERS | FUTEX_OWNER_DIED.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) * [5] The user space value got manipulated between exit_robust_list()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) * and exit_pi_state_list()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) * [6] Valid state after exit_pi_state_list() which sets the new owner in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) * the pi_state but cannot access the user space value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) * [7] pi_state->owner can only be NULL when the OWNER_DIED bit is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) * [8] Owner and user space value match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) * [9] There is no transient state which sets the user space TID to 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) * except exit_robust_list(), but this is indicated by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) * FUTEX_OWNER_DIED bit. See [4]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) * [10] There is no transient state which leaves owner and user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) * TID out of sync. Except one error case where the kernel is denied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) * write access to the user address, see fixup_pi_state_owner().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) * Serialization and lifetime rules:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) * hb->lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) * hb -> futex_q, relation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) * futex_q -> pi_state, relation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) * (cannot be raw because hb can contain arbitrary amount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) * of futex_q's)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) * pi_mutex->wait_lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) * {uval, pi_state}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) * (and pi_mutex 'obviously')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) * p->pi_lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) * p->pi_state_list -> pi_state->list, relation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) * pi_state->refcount:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) * pi_state lifetime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) * Lock order:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) * hb->lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) * pi_mutex->wait_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) * p->pi_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) * Validate that the existing waiter has a pi_state and sanity check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) * the pi_state against the user space value. If correct, attach to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) * it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) static int attach_to_pi_state(u32 __user *uaddr, u32 uval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) struct futex_pi_state *pi_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) struct futex_pi_state **ps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) pid_t pid = uval & FUTEX_TID_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) u32 uval2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) * Userspace might have messed up non-PI and PI futexes [3]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) if (unlikely(!pi_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) * We get here with hb->lock held, and having found a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) * futex_top_waiter(). This means that futex_lock_pi() of said futex_q
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) * has dropped the hb->lock in between queue_me() and unqueue_me_pi(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) * which in turn means that futex_lock_pi() still has a reference on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) * our pi_state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) * The waiter holding a reference on @pi_state also protects against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) * the unlocked put_pi_state() in futex_unlock_pi(), futex_lock_pi()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) * and futex_wait_requeue_pi() as it cannot go to 0 and consequently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) * free pi_state before we can take a reference ourselves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) WARN_ON(!refcount_read(&pi_state->refcount));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) * Now that we have a pi_state, we can acquire wait_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) * and do the state validation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) * Since {uval, pi_state} is serialized by wait_lock, and our current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) * uval was read without holding it, it can have changed. Verify it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) * still is what we expect it to be, otherwise retry the entire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) * operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) if (get_futex_value_locked(&uval2, uaddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) goto out_efault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) if (uval != uval2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) goto out_eagain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) * Handle the owner died case:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) if (uval & FUTEX_OWNER_DIED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) * exit_pi_state_list sets owner to NULL and wakes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) * topmost waiter. The task which acquires the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) * pi_state->rt_mutex will fixup owner.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) if (!pi_state->owner) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) * No pi state owner, but the user space TID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) * is not 0. Inconsistent state. [5]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) if (pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) goto out_einval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) * Take a ref on the state and return success. [4]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) goto out_attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) * If TID is 0, then either the dying owner has not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) * yet executed exit_pi_state_list() or some waiter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) * acquired the rtmutex in the pi state, but did not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) * yet fixup the TID in user space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) * Take a ref on the state and return success. [6]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) if (!pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) goto out_attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) * If the owner died bit is not set, then the pi_state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) * must have an owner. [7]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) if (!pi_state->owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) goto out_einval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) * Bail out if user space manipulated the futex value. If pi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) * state exists then the owner TID must be the same as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) * user space TID. [9/10]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) if (pid != task_pid_vnr(pi_state->owner))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) goto out_einval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) out_attach:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) get_pi_state(pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) *ps = pi_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) out_einval:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) out_eagain:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) out_efault:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) out_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) * wait_for_owner_exiting - Block until the owner has exited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) * @ret: owner's current futex lock status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) * @exiting: Pointer to the exiting task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) * Caller must hold a refcount on @exiting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) static void wait_for_owner_exiting(int ret, struct task_struct *exiting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) if (ret != -EBUSY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) WARN_ON_ONCE(exiting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) if (WARN_ON_ONCE(ret == -EBUSY && !exiting))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) mutex_lock(&exiting->futex_exit_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) * No point in doing state checking here. If the waiter got here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) * while the task was in exec()->exec_futex_release() then it can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) * have any FUTEX_STATE_* value when the waiter has acquired the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) * mutex. OK, if running, EXITING or DEAD if it reached exit()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) * already. Highly unlikely and not a problem. Just one more round
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) * through the futex maze.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) mutex_unlock(&exiting->futex_exit_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) put_task_struct(exiting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) static int handle_exit_race(u32 __user *uaddr, u32 uval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) u32 uval2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) * If the futex exit state is not yet FUTEX_STATE_DEAD, tell the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) * caller that the alleged owner is busy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) if (tsk && tsk->futex_state != FUTEX_STATE_DEAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) * Reread the user space value to handle the following situation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) * CPU0 CPU1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) * sys_exit() sys_futex()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) * do_exit() futex_lock_pi()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) * futex_lock_pi_atomic()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) * exit_signals(tsk) No waiters:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) * tsk->flags |= PF_EXITING; *uaddr == 0x00000PID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) * mm_release(tsk) Set waiter bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) * exit_robust_list(tsk) { *uaddr = 0x80000PID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) * Set owner died attach_to_pi_owner() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) * *uaddr = 0xC0000000; tsk = get_task(PID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) * } if (!tsk->flags & PF_EXITING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) * ... attach();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) * tsk->futex_state = } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) * FUTEX_STATE_DEAD; if (tsk->futex_state !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) * FUTEX_STATE_DEAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) * return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) * return -ESRCH; <--- FAIL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) * }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) * Returning ESRCH unconditionally is wrong here because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) * user space value has been changed by the exiting task.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) * The same logic applies to the case where the exiting task is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) * already gone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) if (get_futex_value_locked(&uval2, uaddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) /* If the user space value has changed, try again. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) if (uval2 != uval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) * The exiting task did not have a robust list, the robust list was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) * corrupted or the user space value in *uaddr is simply bogus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) * Give up and tell user space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) return -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) * Lookup the task for the TID provided from user space and attach to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) * it after doing proper sanity checks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) static int attach_to_pi_owner(u32 __user *uaddr, u32 uval, union futex_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) struct futex_pi_state **ps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) struct task_struct **exiting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) pid_t pid = uval & FUTEX_TID_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) struct futex_pi_state *pi_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) struct task_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) * We are the first waiter - try to look up the real owner and attach
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) * the new pi_state to it, but bail out when TID = 0 [1]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) * The !pid check is paranoid. None of the call sites should end up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) * with pid == 0, but better safe than sorry. Let the caller retry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) if (!pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) p = find_get_task_by_vpid(pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) return handle_exit_race(uaddr, uval, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) if (unlikely(p->flags & PF_KTHREAD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) put_task_struct(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) * We need to look at the task state to figure out, whether the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) * task is exiting. To protect against the change of the task state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) * in futex_exit_release(), we do this protected by p->pi_lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) raw_spin_lock_irq(&p->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) if (unlikely(p->futex_state != FUTEX_STATE_OK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) * The task is on the way out. When the futex state is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) * FUTEX_STATE_DEAD, we know that the task has finished
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) * the cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) int ret = handle_exit_race(uaddr, uval, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) raw_spin_unlock_irq(&p->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) * If the owner task is between FUTEX_STATE_EXITING and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) * FUTEX_STATE_DEAD then store the task pointer and keep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) * the reference on the task struct. The calling code will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) * drop all locks, wait for the task to reach
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) * FUTEX_STATE_DEAD and then drop the refcount. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) * required to prevent a live lock when the current task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) * preempted the exiting task between the two states.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) if (ret == -EBUSY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) *exiting = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) put_task_struct(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) * No existing pi state. First waiter. [2]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) * This creates pi_state, we have hb->lock held, this means nothing can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) * observe this state, wait_lock is irrelevant.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) pi_state = alloc_pi_state();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) * Initialize the pi_mutex in locked state and make @p
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) * the owner of it:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) /* Store the key for possible exit cleanups: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) pi_state->key = *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) WARN_ON(!list_empty(&pi_state->list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) list_add(&pi_state->list, &p->pi_state_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) * Assignment without holding pi_state->pi_mutex.wait_lock is safe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) * because there is no concurrency as the object is not published yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) pi_state->owner = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) raw_spin_unlock_irq(&p->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) put_task_struct(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) *ps = pi_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) static int lookup_pi_state(u32 __user *uaddr, u32 uval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) struct futex_hash_bucket *hb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) union futex_key *key, struct futex_pi_state **ps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) struct task_struct **exiting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) struct futex_q *top_waiter = futex_top_waiter(hb, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) * If there is a waiter on that futex, validate it and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) * attach to the pi_state when the validation succeeds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) if (top_waiter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) * We are the first waiter - try to look up the owner based on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) * @uval and attach to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) return attach_to_pi_owner(uaddr, uval, key, ps, exiting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) u32 curval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) if (unlikely(should_fail_futex(true)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) err = cmpxchg_futex_value_locked(&curval, uaddr, uval, newval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) /* If user space value changed, let the caller retry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) return curval != uval ? -EAGAIN : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) * futex_lock_pi_atomic() - Atomic work required to acquire a pi aware futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) * @uaddr: the pi futex user address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) * @hb: the pi futex hash bucket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) * @key: the futex key associated with uaddr and hb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) * @ps: the pi_state pointer where we store the result of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) * lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) * @task: the task to perform the atomic lock work for. This will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) * be "current" except in the case of requeue pi.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) * @exiting: Pointer to store the task pointer of the owner task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) * which is in the middle of exiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) * - 0 - ready to wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) * - 1 - acquired the lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) * - <0 - error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) * The hb->lock and futex_key refs shall be held by the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) * @exiting is only set when the return value is -EBUSY. If so, this holds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) * a refcount on the exiting task on return and the caller needs to drop it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) * after waiting for the exit to complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) union futex_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) struct futex_pi_state **ps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) struct task_struct *task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) struct task_struct **exiting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) int set_waiters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) u32 uval, newval, vpid = task_pid_vnr(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) struct futex_q *top_waiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) * Read the user space value first so we can validate a few
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) * things before proceeding further.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) if (get_futex_value_locked(&uval, uaddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) if (unlikely(should_fail_futex(true)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) * Detect deadlocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) if ((unlikely((uval & FUTEX_TID_MASK) == vpid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) return -EDEADLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) if ((unlikely(should_fail_futex(true))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) return -EDEADLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) * Lookup existing state first. If it exists, try to attach to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) * its pi_state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) top_waiter = futex_top_waiter(hb, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) if (top_waiter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) * No waiter and user TID is 0. We are here because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) * waiters or the owner died bit is set or called from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) * requeue_cmp_pi or for whatever reason something took the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) * syscall.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) if (!(uval & FUTEX_TID_MASK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) * We take over the futex. No other waiters and the user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) * TID is 0. We preserve the owner died bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) newval = uval & FUTEX_OWNER_DIED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) newval |= vpid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) /* The futex requeue_pi code can enforce the waiters bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) if (set_waiters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) newval |= FUTEX_WAITERS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) ret = lock_pi_update_atomic(uaddr, uval, newval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) /* If the take over worked, return 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) return ret < 0 ? ret : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) * First waiter. Set the waiters bit before attaching ourself to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) * the owner. If owner tries to unlock, it will be forced into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) * the kernel and blocked on hb->lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) newval = uval | FUTEX_WAITERS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) ret = lock_pi_update_atomic(uaddr, uval, newval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) * If the update of the user space value succeeded, we try to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) * attach to the owner. If that fails, no harm done, we only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) * set the FUTEX_WAITERS bit in the user space variable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) return attach_to_pi_owner(uaddr, newval, key, ps, exiting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) * __unqueue_futex() - Remove the futex_q from its futex_hash_bucket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) * @q: The futex_q to unqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) * The q->lock_ptr must not be NULL and must be held by the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) static void __unqueue_futex(struct futex_q *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) struct futex_hash_bucket *hb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) if (WARN_ON_SMP(!q->lock_ptr) || WARN_ON(plist_node_empty(&q->list)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) lockdep_assert_held(q->lock_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) plist_del(&q->list, &hb->chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) hb_waiters_dec(hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) * The hash bucket lock must be held when this is called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) * Afterwards, the futex_q must not be accessed. Callers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) * must ensure to later call wake_up_q() for the actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) * wakeups to occur.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) struct task_struct *p = q->task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) get_task_struct(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) __unqueue_futex(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) * The waiting task can free the futex_q as soon as q->lock_ptr = NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) * is written, without taking any locks. This is possible in the event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) * of a spurious wakeup, for example. A memory barrier is required here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) * to prevent the following store to lock_ptr from getting ahead of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) * plist_del in __unqueue_futex().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) smp_store_release(&q->lock_ptr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) * Queue the task for later wakeup for after we've released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) * the hb->lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) wake_q_add_safe(wake_q, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) * Caller must hold a reference on @pi_state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_pi_state *pi_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) u32 curval, newval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) struct task_struct *new_owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) bool postunlock = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) DEFINE_WAKE_Q(wake_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) if (WARN_ON_ONCE(!new_owner)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) * As per the comment in futex_unlock_pi() this should not happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) * When this happens, give up our locks and try again, giving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) * the futex_lock_pi() instance time to complete, either by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) * waiting on the rtmutex or removing itself from the futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) * queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) * We pass it to the next owner. The WAITERS bit is always kept
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) * enabled while there is PI state around. We cleanup the owner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) * died bit, because we are the owner.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) if (unlikely(should_fail_futex(true))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) ret = cmpxchg_futex_value_locked(&curval, uaddr, uval, newval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) if (!ret && (curval != uval)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) * If a unconditional UNLOCK_PI operation (user space did not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) * try the TID->0 transition) raced with a waiter setting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) * FUTEX_WAITERS flag between get_user() and locking the hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) * bucket lock, retry the operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) if ((FUTEX_TID_MASK & curval) == uval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) * This is a point of no return; once we modified the uval
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) * there is no going back and subsequent operations must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) * not fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) pi_state_update_owner(pi_state, new_owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) postunlock = __rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) if (postunlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) rt_mutex_postunlock(&wake_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) * Express the locking dependencies for lockdep:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) if (hb1 <= hb2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) spin_lock(&hb1->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) if (hb1 < hb2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) } else { /* hb1 > hb2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) spin_lock(&hb2->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) spin_unlock(&hb1->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) if (hb1 != hb2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) spin_unlock(&hb2->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) * Wake up waiters matching bitset queued on this futex (uaddr).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) struct futex_hash_bucket *hb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) struct futex_q *this, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) union futex_key key = FUTEX_KEY_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) DEFINE_WAKE_Q(wake_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) if (!bitset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, FUTEX_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) if (unlikely(ret != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) hb = hash_futex(&key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) /* Make sure we really have tasks to wakeup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) if (!hb_waiters_pending(hb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) spin_lock(&hb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) plist_for_each_entry_safe(this, next, &hb->chain, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) if (match_futex (&this->key, &key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) if (this->pi_state || this->rt_waiter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) /* Check if one of the bits is set in both bitsets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) if (!(this->bitset & bitset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) mark_wake_futex(&wake_q, this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) if (++ret >= nr_wake)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) spin_unlock(&hb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) wake_up_q(&wake_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) static int futex_atomic_op_inuser(unsigned int encoded_op, u32 __user *uaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) unsigned int op = (encoded_op & 0x70000000) >> 28;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) unsigned int cmp = (encoded_op & 0x0f000000) >> 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) int oparg = sign_extend32((encoded_op & 0x00fff000) >> 12, 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) int cmparg = sign_extend32(encoded_op & 0x00000fff, 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) int oldval, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) if (oparg < 0 || oparg > 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) char comm[sizeof(current->comm)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) * kill this print and return -EINVAL when userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) * is sane again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) pr_info_ratelimited("futex_wake_op: %s tries to shift op by %d; fix this program\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) get_task_comm(comm, current), oparg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) oparg &= 31;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) oparg = 1 << oparg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) pagefault_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) ret = arch_futex_atomic_op_inuser(op, oparg, &oldval, uaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) pagefault_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) switch (cmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) case FUTEX_OP_CMP_EQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) return oldval == cmparg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) case FUTEX_OP_CMP_NE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) return oldval != cmparg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) case FUTEX_OP_CMP_LT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) return oldval < cmparg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) case FUTEX_OP_CMP_GE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) return oldval >= cmparg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) case FUTEX_OP_CMP_LE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) return oldval <= cmparg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) case FUTEX_OP_CMP_GT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) return oldval > cmparg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) * Wake up all waiters hashed on the physical page that is mapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) * to this virtual address:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) int nr_wake, int nr_wake2, int op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) struct futex_hash_bucket *hb1, *hb2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) struct futex_q *this, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) int ret, op_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) DEFINE_WAKE_Q(wake_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, FUTEX_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) if (unlikely(ret != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, FUTEX_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) if (unlikely(ret != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) hb1 = hash_futex(&key1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) hb2 = hash_futex(&key2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) retry_private:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) double_lock_hb(hb1, hb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) op_ret = futex_atomic_op_inuser(op, uaddr2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) if (unlikely(op_ret < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) double_unlock_hb(hb1, hb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) if (!IS_ENABLED(CONFIG_MMU) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) unlikely(op_ret != -EFAULT && op_ret != -EAGAIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) * we don't get EFAULT from MMU faults if we don't have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) * an MMU, but we might get them from range checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) ret = op_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) if (op_ret == -EFAULT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) ret = fault_in_user_writeable(uaddr2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) if (!(flags & FLAGS_SHARED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) goto retry_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) plist_for_each_entry_safe(this, next, &hb1->chain, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) if (match_futex (&this->key, &key1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) if (this->pi_state || this->rt_waiter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) mark_wake_futex(&wake_q, this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) if (++ret >= nr_wake)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) if (op_ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) op_ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) plist_for_each_entry_safe(this, next, &hb2->chain, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) if (match_futex (&this->key, &key2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) if (this->pi_state || this->rt_waiter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) mark_wake_futex(&wake_q, this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) if (++op_ret >= nr_wake2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) ret += op_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) double_unlock_hb(hb1, hb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) wake_up_q(&wake_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) * requeue_futex() - Requeue a futex_q from one hb to another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) * @q: the futex_q to requeue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) * @hb1: the source hash_bucket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) * @hb2: the target hash_bucket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) * @key2: the new key for the requeued futex_q
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) static inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) struct futex_hash_bucket *hb2, union futex_key *key2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) * If key1 and key2 hash to the same bucket, no need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) * requeue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) if (likely(&hb1->chain != &hb2->chain)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) plist_del(&q->list, &hb1->chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) hb_waiters_dec(hb1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) hb_waiters_inc(hb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) plist_add(&q->list, &hb2->chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) q->lock_ptr = &hb2->lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) q->key = *key2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) * @q: the futex_q
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) * @key: the key of the requeue target futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) * @hb: the hash_bucket of the requeue target futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) * During futex_requeue, with requeue_pi=1, it is possible to acquire the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) * target futex if it is uncontended or via a lock steal. Set the futex_q key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) * to the requeue target futex so the waiter can detect the wakeup on the right
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) * futex, but remove it from the hb and NULL the rt_waiter so it can detect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) * atomic lock acquisition. Set the q->lock_ptr to the requeue target hb->lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) * to protect access to the pi_state to fixup the owner later. Must be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) * with both q->lock_ptr and hb->lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) static inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) struct futex_hash_bucket *hb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) q->key = *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) __unqueue_futex(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) WARN_ON(!q->rt_waiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) q->rt_waiter = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) q->lock_ptr = &hb->lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) wake_up_state(q->task, TASK_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) * @pifutex: the user address of the to futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) * @hb1: the from futex hash bucket, must be locked by the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) * @hb2: the to futex hash bucket, must be locked by the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) * @key1: the from futex key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) * @key2: the to futex key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) * @ps: address to store the pi_state pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) * @exiting: Pointer to store the task pointer of the owner task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) * which is in the middle of exiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) * Try and get the lock on behalf of the top waiter if we can do it atomically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) * Wake the top waiter if we succeed. If the caller specified set_waiters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) * hb1 and hb2 must be held by the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) * @exiting is only set when the return value is -EBUSY. If so, this holds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) * a refcount on the exiting task on return and the caller needs to drop it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) * after waiting for the exit to complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) * - 0 - failed to acquire the lock atomically;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) * - >0 - acquired the lock, return value is vpid of the top_waiter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) * - <0 - error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) futex_proxy_trylock_atomic(u32 __user *pifutex, struct futex_hash_bucket *hb1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) struct futex_hash_bucket *hb2, union futex_key *key1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) union futex_key *key2, struct futex_pi_state **ps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) struct task_struct **exiting, int set_waiters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) struct futex_q *top_waiter = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) u32 curval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) int ret, vpid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) if (get_futex_value_locked(&curval, pifutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) if (unlikely(should_fail_futex(true)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) * Find the top_waiter and determine if there are additional waiters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) * If the caller intends to requeue more than 1 waiter to pifutex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) * as we have means to handle the possible fault. If not, don't set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) * the bit unecessarily as it will force the subsequent unlock to enter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) * the kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) top_waiter = futex_top_waiter(hb1, key1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) /* There are no waiters, nothing for us to do. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) if (!top_waiter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) /* Ensure we requeue to the expected futex. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) if (!match_futex(top_waiter->requeue_pi_key, key2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) * Try to take the lock for top_waiter. Set the FUTEX_WAITERS bit in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) * the contended case or if set_waiters is 1. The pi_state is returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) * in ps in contended cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) vpid = task_pid_vnr(top_waiter->task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) exiting, set_waiters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) if (ret == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) requeue_pi_wake_futex(top_waiter, key2, hb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) return vpid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) * @uaddr1: source futex user address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) * @flags: futex flags (FLAGS_SHARED, etc.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) * @uaddr2: target futex user address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) * @nr_wake: number of waiters to wake (must be 1 for requeue_pi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) * @nr_requeue: number of waiters to requeue (0-INT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) * @cmpval: @uaddr1 expected value (or %NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) * @requeue_pi: if we are attempting to requeue from a non-pi futex to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) * pi futex (pi to pi requeue is not supported)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) * uaddr2 atomically on behalf of the top waiter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) * - >=0 - on success, the number of tasks requeued or woken;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) * - <0 - on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) u32 __user *uaddr2, int nr_wake, int nr_requeue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) u32 *cmpval, int requeue_pi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) int task_count = 0, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) struct futex_pi_state *pi_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) struct futex_hash_bucket *hb1, *hb2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) struct futex_q *this, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) DEFINE_WAKE_Q(wake_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) if (nr_wake < 0 || nr_requeue < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) * When PI not supported: return -ENOSYS if requeue_pi is true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) * consequently the compiler knows requeue_pi is always false past
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) * this point which will optimize away all the conditional code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) * further down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) if (!IS_ENABLED(CONFIG_FUTEX_PI) && requeue_pi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) if (requeue_pi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) * Requeue PI only works on two distinct uaddrs. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) * check is only valid for private futexes. See below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) if (uaddr1 == uaddr2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) * requeue_pi requires a pi_state, try to allocate it now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) * without any locks in case it fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) if (refill_pi_state_cache())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) * requeue_pi must wake as many tasks as it can, up to nr_wake
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) * + nr_requeue, since it acquires the rt_mutex prior to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) * returning to userspace, so as to not leave the rt_mutex with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) * waiters and no owner. However, second and third wake-ups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) * cannot be predicted as they involve race conditions with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) * first wake and a fault while looking up the pi_state. Both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) * pthread_cond_signal() and pthread_cond_broadcast() should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) * use nr_wake=1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) if (nr_wake != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, FUTEX_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) if (unlikely(ret != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) requeue_pi ? FUTEX_WRITE : FUTEX_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) if (unlikely(ret != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) * The check above which compares uaddrs is not sufficient for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) * shared futexes. We need to compare the keys:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) if (requeue_pi && match_futex(&key1, &key2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) hb1 = hash_futex(&key1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) hb2 = hash_futex(&key2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) retry_private:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) hb_waiters_inc(hb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) double_lock_hb(hb1, hb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) if (likely(cmpval != NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) u32 curval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) ret = get_futex_value_locked(&curval, uaddr1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) if (unlikely(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) double_unlock_hb(hb1, hb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) hb_waiters_dec(hb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) ret = get_user(curval, uaddr1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) if (!(flags & FLAGS_SHARED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) goto retry_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) if (curval != *cmpval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) struct task_struct *exiting = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) * Attempt to acquire uaddr2 and wake the top waiter. If we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) * intend to requeue waiters, force setting the FUTEX_WAITERS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) * bit. We force this here where we are able to easily handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) * faults rather in the requeue loop below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) &key2, &pi_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) &exiting, nr_requeue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) * At this point the top_waiter has either taken uaddr2 or is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) * waiting on it. If the former, then the pi_state will not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) * exist yet, look it up one more time to ensure we have a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) * reference to it. If the lock was taken, ret contains the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) * vpid of the top waiter task.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) * If the lock was not taken, we have pi_state and an initial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) * refcount on it. In case of an error we have nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) WARN_ON(pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) task_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) * If we acquired the lock, then the user space value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) * of uaddr2 should be vpid. It cannot be changed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) * the top waiter as it is blocked on hb2 lock if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) * tries to do so. If something fiddled with it behind
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) * our back the pi state lookup might unearth it. So
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) * we rather use the known value than rereading and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) * handing potential crap to lookup_pi_state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) * If that call succeeds then we have pi_state and an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) * initial refcount on it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) ret = lookup_pi_state(uaddr2, ret, hb2, &key2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) &pi_state, &exiting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) /* We hold a reference on the pi state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) /* If the above failed, then pi_state is NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) case -EFAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) double_unlock_hb(hb1, hb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) hb_waiters_dec(hb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) ret = fault_in_user_writeable(uaddr2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) case -EBUSY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) case -EAGAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) * Two reasons for this:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) * - EBUSY: Owner is exiting and we just wait for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) * exit to complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) * - EAGAIN: The user space value changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) double_unlock_hb(hb1, hb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) hb_waiters_dec(hb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) * Handle the case where the owner is in the middle of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) * exiting. Wait for the exit to complete otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) * this task might loop forever, aka. live lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) wait_for_owner_exiting(ret, exiting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) plist_for_each_entry_safe(this, next, &hb1->chain, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) if (task_count - nr_wake >= nr_requeue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) if (!match_futex(&this->key, &key1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) * FUTEX_WAIT_REQEUE_PI and FUTEX_CMP_REQUEUE_PI should always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) * be paired with each other and no other futex ops.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) * We should never be requeueing a futex_q with a pi_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) * which is awaiting a futex_unlock_pi().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) if ((requeue_pi && !this->rt_waiter) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) (!requeue_pi && this->rt_waiter) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) this->pi_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) * Wake nr_wake waiters. For requeue_pi, if we acquired the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) * lock, we already woke the top_waiter. If not, it will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) * woken by futex_unlock_pi().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) if (++task_count <= nr_wake && !requeue_pi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) mark_wake_futex(&wake_q, this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) /* Ensure we requeue to the expected futex for requeue_pi. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) if (requeue_pi && !match_futex(this->requeue_pi_key, &key2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) * Requeue nr_requeue waiters and possibly one more in the case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) * of requeue_pi if we couldn't acquire the lock atomically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) if (requeue_pi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) * Prepare the waiter to take the rt_mutex. Take a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) * refcount on the pi_state and store the pointer in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) * the futex_q object of the waiter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) get_pi_state(pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) this->pi_state = pi_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) this->rt_waiter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) this->task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) if (ret == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) * We got the lock. We do neither drop the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) * refcount on pi_state nor clear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) * this->pi_state because the waiter needs the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) * pi_state for cleaning up the user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) * value. It will drop the refcount after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) * doing so.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) requeue_pi_wake_futex(this, &key2, hb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) } else if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) * rt_mutex_start_proxy_lock() detected a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) * potential deadlock when we tried to queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) * that waiter. Drop the pi_state reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) * which we took above and remove the pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) * to the state from the waiters futex_q
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) * object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) this->pi_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) put_pi_state(pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) * We stop queueing more waiters and let user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) * space deal with the mess.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) requeue_futex(this, hb1, hb2, &key2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) * We took an extra initial reference to the pi_state either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) * in futex_proxy_trylock_atomic() or in lookup_pi_state(). We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) * need to drop it here again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) put_pi_state(pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) double_unlock_hb(hb1, hb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) wake_up_q(&wake_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) hb_waiters_dec(hb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) return ret ? ret : task_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) /* The key must be already stored in q->key. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) __acquires(&hb->lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) struct futex_hash_bucket *hb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) hb = hash_futex(&q->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) * Increment the counter before taking the lock so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) * a potential waker won't miss a to-be-slept task that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) * waiting for the spinlock. This is safe as all queue_lock()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) * users end up calling queue_me(). Similarly, for housekeeping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) * decrement the counter at queue_unlock() when some error has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) * occurred and we don't end up adding the task to the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) hb_waiters_inc(hb); /* implies smp_mb(); (A) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) q->lock_ptr = &hb->lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) spin_lock(&hb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) return hb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) queue_unlock(struct futex_hash_bucket *hb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) __releases(&hb->lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) spin_unlock(&hb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) hb_waiters_dec(hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) int prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) bool already_on_hb = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) * The priority used to register this element is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) * - either the real thread-priority for the real-time threads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) * (i.e. threads with a priority lower than MAX_RT_PRIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) * - or MAX_RT_PRIO for non-RT threads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) * Thus, all RT-threads are woken first in priority order, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) * the others are woken last, in FIFO order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) prio = min(current->normal_prio, MAX_RT_PRIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) plist_node_init(&q->list, prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) trace_android_vh_alter_futex_plist_add(&q->list, &hb->chain, &already_on_hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) if (!already_on_hb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) plist_add(&q->list, &hb->chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) q->task = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) * queue_me() - Enqueue the futex_q on the futex_hash_bucket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) * @q: The futex_q to enqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) * @hb: The destination hash bucket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) * The hb->lock must be held by the caller, and is released here. A call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) * queue_me() is typically paired with exactly one call to unqueue_me(). The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) * exceptions involve the PI related operations, which may use unqueue_me_pi()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) * or nothing if the unqueue is done as part of the wake process and the unqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) * state is implicit in the state of woken task (see futex_wait_requeue_pi() for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) * an example).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) __releases(&hb->lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) __queue_me(q, hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) spin_unlock(&hb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) * unqueue_me() - Remove the futex_q from its futex_hash_bucket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) * @q: The futex_q to unqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) * The q->lock_ptr must not be held by the caller. A call to unqueue_me() must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) * be paired with exactly one earlier call to queue_me().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) * - 1 - if the futex_q was still queued (and we removed unqueued it);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) * - 0 - if the futex_q was already removed by the waking thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) static int unqueue_me(struct futex_q *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) spinlock_t *lock_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) /* In the common case we don't take the spinlock, which is nice. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) * q->lock_ptr can change between this read and the following spin_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) * Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) * optimizing lock_ptr out of the logic below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) lock_ptr = READ_ONCE(q->lock_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) if (lock_ptr != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) spin_lock(lock_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) * q->lock_ptr can change between reading it and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) * spin_lock(), causing us to take the wrong lock. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) * corrects the race condition.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) * Reasoning goes like this: if we have the wrong lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) * q->lock_ptr must have changed (maybe several times)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) * between reading it and the spin_lock(). It can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) * change again after the spin_lock() but only if it was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) * already changed before the spin_lock(). It cannot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) * however, change back to the original value. Therefore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) * we can detect whether we acquired the correct lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) if (unlikely(lock_ptr != q->lock_ptr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) spin_unlock(lock_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) __unqueue_futex(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) BUG_ON(q->pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) spin_unlock(lock_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) * PI futexes can not be requeued and must remove themself from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) * and dropped here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) static void unqueue_me_pi(struct futex_q *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) __releases(q->lock_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) __unqueue_futex(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) BUG_ON(!q->pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) put_pi_state(q->pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) q->pi_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) spin_unlock(q->lock_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) static int __fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) struct task_struct *argowner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) struct futex_pi_state *pi_state = q->pi_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) struct task_struct *oldowner, *newowner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) u32 uval, curval, newval, newtid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) oldowner = pi_state->owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) * We are here because either:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) * - we stole the lock and pi_state->owner needs updating to reflect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) * that (@argowner == current),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) * or:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) * - someone stole our lock and we need to fix things to point to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) * new owner (@argowner == NULL).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) * Either way, we have to replace the TID in the user space variable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) * This must be atomic as we have to preserve the owner died bit here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) * Note: We write the user space value _before_ changing the pi_state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) * because we can fault here. Imagine swapped out pages or a fork
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) * that marked all the anonymous memory readonly for cow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) * Modifying pi_state _before_ the user space value would leave the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) * pi_state in an inconsistent state when we fault here, because we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) * need to drop the locks to handle the fault. This might be observed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) * in the PID check in lookup_pi_state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) if (!argowner) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) if (oldowner != current) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) * We raced against a concurrent self; things are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) * already fixed up. Nothing to do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) if (__rt_mutex_futex_trylock(&pi_state->pi_mutex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) /* We got the lock. pi_state is correct. Tell caller. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) * The trylock just failed, so either there is an owner or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) * there is a higher priority waiter than this one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) newowner = rt_mutex_owner(&pi_state->pi_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) * If the higher priority waiter has not yet taken over the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) * rtmutex then newowner is NULL. We can't return here with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) * that state because it's inconsistent vs. the user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) * state. So drop the locks and try again. It's a valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) * situation and not any different from the other retry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) * conditions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) if (unlikely(!newowner)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) goto handle_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) WARN_ON_ONCE(argowner != current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) if (oldowner == current) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) * We raced against a concurrent self; things are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) * already fixed up. Nothing to do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) newowner = argowner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) /* Owner died? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) if (!pi_state->owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) newtid |= FUTEX_OWNER_DIED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) err = get_futex_value_locked(&uval, uaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) goto handle_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) newval = (uval & FUTEX_OWNER_DIED) | newtid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) err = cmpxchg_futex_value_locked(&curval, uaddr, uval, newval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) goto handle_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) if (curval == uval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) uval = curval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) * We fixed up user space. Now we need to fix the pi_state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) * itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) pi_state_update_owner(pi_state, newowner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) return argowner == current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) * In order to reschedule or handle a page fault, we need to drop the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) * locks here. In the case of a fault, this gives the other task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) * (either the highest priority waiter itself or the task which stole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) * the rtmutex) the chance to try the fixup of the pi_state. So once we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) * are back from handling the fault we need to check the pi_state after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) * reacquiring the locks and before trying to do another fixup. When
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) * the fixup has been done already we simply return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) * Note: we hold both hb->lock and pi_mutex->wait_lock. We can safely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) * drop hb->lock since the caller owns the hb -> futex_q relation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) * Dropping the pi_mutex->wait_lock requires the state revalidate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) handle_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) spin_unlock(q->lock_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) switch (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) case -EFAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) err = fault_in_user_writeable(uaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) case -EAGAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) spin_lock(q->lock_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) * Check if someone else fixed it for us:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) if (pi_state->owner != oldowner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) return argowner == current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) /* Retry if err was -EAGAIN or the fault in succeeded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) * fault_in_user_writeable() failed so user state is immutable. At
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) * best we can make the kernel state consistent but user state will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) * be most likely hosed and any subsequent unlock operation will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) * rejected due to PI futex rule [10].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) * Ensure that the rtmutex owner is also the pi_state owner despite
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) * the user space value claiming something different. There is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) * point in unlocking the rtmutex if current is the owner as it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) * would need to wait until the next waiter has taken the rtmutex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) * to guarantee consistent state. Keep it simple. Userspace asked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) * for this wreckaged state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) * The rtmutex has an owner - either current or some other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) * task. See the EAGAIN loop above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) pi_state_update_owner(pi_state, rt_mutex_owner(&pi_state->pi_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) struct task_struct *argowner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) struct futex_pi_state *pi_state = q->pi_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) lockdep_assert_held(q->lock_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) ret = __fixup_pi_state_owner(uaddr, q, argowner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) static long futex_wait_restart(struct restart_block *restart);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) * fixup_owner() - Post lock pi_state and corner case management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) * @uaddr: user address of the futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) * @q: futex_q (contains pi_state and access to the rt_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) * After attempting to lock an rt_mutex, this function is called to cleanup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) * the pi_state owner as well as handle race conditions that may allow us to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) * acquire the lock. Must be called with the hb lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) * - 1 - success, lock taken;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) * - 0 - success, lock not taken;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) * - <0 - on error (-EFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) if (locked) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) * Got the lock. We might not be the anticipated owner if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) * did a lock-steal - fix up the PI-state in that case:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) * Speculative pi_state->owner read (we don't hold wait_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) * since we own the lock pi_state->owner == current is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) * stable state, anything else needs more attention.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) if (q->pi_state->owner != current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) return fixup_pi_state_owner(uaddr, q, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) * If we didn't get the lock; check if anybody stole it from us. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) * that case, we need to fix up the uval to point to them instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) * us, otherwise bad things happen. [10]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) * Another speculative read; pi_state->owner == current is unstable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) * but needs our attention.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) if (q->pi_state->owner == current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) return fixup_pi_state_owner(uaddr, q, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) * Paranoia check. If we did not take the lock, then we should not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) * the owner of the rt_mutex. Warn and establish consistent state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) if (WARN_ON_ONCE(rt_mutex_owner(&q->pi_state->pi_mutex) == current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) return fixup_pi_state_owner(uaddr, q, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) * @hb: the futex hash bucket, must be locked by the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) * @q: the futex_q to queue up on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) * @timeout: the prepared hrtimer_sleeper, or null for no timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) struct hrtimer_sleeper *timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) * The task state is guaranteed to be set before another task can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) * wake it. set_current_state() is implemented using smp_store_mb() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) * queue_me() calls spin_unlock() upon completion, both serializing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) * access to the hash list and forcing another memory barrier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) queue_me(q, hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) /* Arm the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) if (timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) hrtimer_sleeper_start_expires(timeout, HRTIMER_MODE_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) * If we have been removed from the hash list, then another task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) * has tried to wake us, and we can skip the call to schedule().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) if (likely(!plist_node_empty(&q->list))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) * If the timer has already expired, current will already be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) * flagged for rescheduling. Only call schedule if there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) * is no timeout, or if it has yet to expire.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) if (!timeout || timeout->task) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) trace_android_vh_futex_sleep_start(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) freezable_schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) * futex_wait_setup() - Prepare to wait on a futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) * @uaddr: the futex userspace address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) * @val: the expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) * @flags: futex flags (FLAGS_SHARED, etc.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) * @q: the associated futex_q
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) * @hb: storage for hash_bucket pointer to be returned to caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) * Setup the futex_q and locate the hash_bucket. Get the futex value and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) * compare it with the expected value. Handle atomic faults internally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) * Return with the hb lock held and a q.key reference on success, and unlocked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) * with no q.key reference on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) * - 0 - uaddr contains val and hb has been locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) * - <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlocked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) static int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) struct futex_q *q, struct futex_hash_bucket **hb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) u32 uval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) * Access the page AFTER the hash-bucket is locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) * Order is important:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) * The basic logical guarantee of a futex is that it blocks ONLY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) * if cond(var) is known to be true at the time of blocking, for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) * any cond. If we locked the hash-bucket after testing *uaddr, that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) * would open a race condition where we could block indefinitely with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) * cond(var) false, which would violate the guarantee.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) * On the other hand, we insert q and release the hash-bucket only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) * after testing *uaddr. This guarantees that futex_wait() will NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) * absorb a wakeup if *uaddr does not match the desired values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) * while the syscall executes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, FUTEX_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) if (unlikely(ret != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) retry_private:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) *hb = queue_lock(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) ret = get_futex_value_locked(&uval, uaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) queue_unlock(*hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) ret = get_user(uval, uaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) if (!(flags & FLAGS_SHARED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) goto retry_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) if (uval != val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) queue_unlock(*hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) ret = -EWOULDBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) ktime_t *abs_time, u32 bitset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) struct hrtimer_sleeper timeout, *to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) struct restart_block *restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) struct futex_hash_bucket *hb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) struct futex_q q = futex_q_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) if (!bitset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) q.bitset = bitset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) to = futex_setup_timer(abs_time, &timeout, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) current->timer_slack_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) * Prepare to wait on uaddr. On success, holds hb lock and increments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) * q.key refs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) /* queue_me and wait for wakeup, timeout, or a signal. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) futex_wait_queue_me(hb, &q, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) /* If we were woken (and unqueued), we succeeded, whatever. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) /* unqueue_me() drops q.key ref */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) if (!unqueue_me(&q))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) ret = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) if (to && !to->task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) * We expect signal_pending(current), but we might be the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) * victim of a spurious wakeup as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) if (!signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) if (!abs_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) restart = ¤t->restart_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) restart->futex.uaddr = uaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) restart->futex.val = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) restart->futex.time = *abs_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) restart->futex.bitset = bitset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) restart->futex.flags = flags | FLAGS_HAS_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) ret = set_restart_fn(restart, futex_wait_restart);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) if (to) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) hrtimer_cancel(&to->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) destroy_hrtimer_on_stack(&to->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) static long futex_wait_restart(struct restart_block *restart)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) u32 __user *uaddr = restart->futex.uaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) ktime_t t, *tp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) t = restart->futex.time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) tp = &t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) restart->fn = do_no_restart_syscall;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) return (long)futex_wait(uaddr, restart->futex.flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) restart->futex.val, tp, restart->futex.bitset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) * Userspace tried a 0 -> TID atomic transition of the futex value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) * and failed. The kernel side here does the whole locking operation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) * if there are waiters then it will block as a consequence of relying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) * on rt-mutexes, it does PI, etc. (Due to races the kernel might see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) * a 0 value of the futex too.).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) * Also serves as futex trylock_pi()'ing, and due semantics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) static int futex_lock_pi(u32 __user *uaddr, unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) ktime_t *time, int trylock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) struct hrtimer_sleeper timeout, *to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) struct task_struct *exiting = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) struct rt_mutex_waiter rt_waiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) struct futex_hash_bucket *hb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) struct futex_q q = futex_q_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) int res, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) if (!IS_ENABLED(CONFIG_FUTEX_PI))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) if (refill_pi_state_cache())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) to = futex_setup_timer(time, &timeout, FLAGS_CLOCKRT, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, FUTEX_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) if (unlikely(ret != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) retry_private:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) hb = queue_lock(&q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) &exiting, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) if (unlikely(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) * Atomic work succeeded and we got the lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) * or failed. Either way, we do _not_ block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) /* We got the lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) goto out_unlock_put_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) case -EFAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) goto uaddr_faulted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) case -EBUSY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) case -EAGAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) * Two reasons for this:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) * - EBUSY: Task is exiting and we just wait for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) * exit to complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) * - EAGAIN: The user space value changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) queue_unlock(hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) * Handle the case where the owner is in the middle of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) * exiting. Wait for the exit to complete otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) * this task might loop forever, aka. live lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) wait_for_owner_exiting(ret, exiting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) goto out_unlock_put_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) WARN_ON(!q.pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) * Only actually queue now that the atomic ops are done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) __queue_me(&q, hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) if (trylock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) ret = rt_mutex_futex_trylock(&q.pi_state->pi_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) /* Fixup the trylock return value: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) ret = ret ? 0 : -EWOULDBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) goto no_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) rt_mutex_init_waiter(&rt_waiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) * On PREEMPT_RT_FULL, when hb->lock becomes an rt_mutex, we must not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) * hold it while doing rt_mutex_start_proxy(), because then it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) * include hb->lock in the blocking chain, even through we'll not in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) * fact hold it while blocking. This will lead it to report -EDEADLK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) * and BUG when futex_unlock_pi() interleaves with this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) * Therefore acquire wait_lock while holding hb->lock, but drop the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) * latter before calling __rt_mutex_start_proxy_lock(). This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) * interleaves with futex_unlock_pi() -- which does a similar lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) * handoff -- such that the latter can observe the futex_q::pi_state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) * before __rt_mutex_start_proxy_lock() is done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) raw_spin_lock_irq(&q.pi_state->pi_mutex.wait_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) spin_unlock(q.lock_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) * __rt_mutex_start_proxy_lock() unconditionally enqueues the @rt_waiter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) * such that futex_unlock_pi() is guaranteed to observe the waiter when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) * it sees the futex_q::pi_state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) ret = __rt_mutex_start_proxy_lock(&q.pi_state->pi_mutex, &rt_waiter, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) raw_spin_unlock_irq(&q.pi_state->pi_mutex.wait_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) if (unlikely(to))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) hrtimer_sleeper_start_expires(to, HRTIMER_MODE_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) ret = rt_mutex_wait_proxy_lock(&q.pi_state->pi_mutex, to, &rt_waiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) spin_lock(q.lock_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) * If we failed to acquire the lock (deadlock/signal/timeout), we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) * first acquire the hb->lock before removing the lock from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) * rt_mutex waitqueue, such that we can keep the hb and rt_mutex wait
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) * lists consistent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) * In particular; it is important that futex_unlock_pi() can not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) * observe this inconsistency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) if (ret && !rt_mutex_cleanup_proxy_lock(&q.pi_state->pi_mutex, &rt_waiter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) no_block:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) * Fixup the pi_state owner and possibly acquire the lock if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) * haven't already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) res = fixup_owner(uaddr, &q, !ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) * If fixup_owner() returned an error, proprogate that. If it acquired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) * the lock, clear our -ETIMEDOUT or -EINTR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) ret = (res < 0) ? res : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) /* Unqueue and drop the lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) unqueue_me_pi(&q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) out_unlock_put_key:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) queue_unlock(hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) if (to) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) hrtimer_cancel(&to->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) destroy_hrtimer_on_stack(&to->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) return ret != -EINTR ? ret : -ERESTARTNOINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) uaddr_faulted:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) queue_unlock(hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) ret = fault_in_user_writeable(uaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) if (!(flags & FLAGS_SHARED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) goto retry_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) * Userspace attempted a TID -> 0 atomic transition, and failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) * This is the in-kernel slowpath: we look up the PI state (if any),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) * and do the rt-mutex unlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) u32 curval, uval, vpid = task_pid_vnr(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) union futex_key key = FUTEX_KEY_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) struct futex_hash_bucket *hb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) struct futex_q *top_waiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) if (!IS_ENABLED(CONFIG_FUTEX_PI))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) if (get_user(uval, uaddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) * We release only a lock we actually own:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) if ((uval & FUTEX_TID_MASK) != vpid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, FUTEX_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) hb = hash_futex(&key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) spin_lock(&hb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) * Check waiters first. We do not trust user space values at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) * all and we at least want to know if user space fiddled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) * with the futex value instead of blindly unlocking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) top_waiter = futex_top_waiter(hb, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) if (top_waiter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) struct futex_pi_state *pi_state = top_waiter->pi_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) if (!pi_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) * If current does not own the pi_state then the futex is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) * inconsistent and user space fiddled with the futex value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) if (pi_state->owner != current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) get_pi_state(pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) * By taking wait_lock while still holding hb->lock, we ensure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) * there is no point where we hold neither; and therefore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) * wake_futex_pi() must observe a state consistent with what we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) * observed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) * In particular; this forces __rt_mutex_start_proxy() to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) * complete such that we're guaranteed to observe the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) * rt_waiter. Also see the WARN in wake_futex_pi().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) spin_unlock(&hb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) /* drops pi_state->pi_mutex.wait_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) ret = wake_futex_pi(uaddr, uval, pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) put_pi_state(pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) * Success, we're done! No tricky corner cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) goto out_putkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) * The atomic access to the futex value generated a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) * pagefault, so retry the user-access and the wakeup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) if (ret == -EFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) goto pi_faulted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) * A unconditional UNLOCK_PI op raced against a waiter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) * setting the FUTEX_WAITERS bit. Try again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) if (ret == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) goto pi_retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) * wake_futex_pi has detected invalid state. Tell user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) * space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) goto out_putkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) * We have no kernel internal state, i.e. no waiters in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) * kernel. Waiters which are about to queue themselves are stuck
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) * on hb->lock. So we can safely ignore them. We do neither
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) * preserve the WAITERS bit not the OWNER_DIED one. We are the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) * owner.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) if ((ret = cmpxchg_futex_value_locked(&curval, uaddr, uval, 0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) spin_unlock(&hb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) case -EFAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) goto pi_faulted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) case -EAGAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) goto pi_retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) goto out_putkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) * If uval has changed, let user space handle it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) ret = (curval == uval) ? 0 : -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) spin_unlock(&hb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) out_putkey:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) pi_retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) pi_faulted:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) ret = fault_in_user_writeable(uaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) * handle_early_requeue_pi_wakeup() - Detect early wakeup on the initial futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) * @hb: the hash_bucket futex_q was original enqueued on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) * @q: the futex_q woken while waiting to be requeued
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) * @key2: the futex_key of the requeue target futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) * @timeout: the timeout associated with the wait (NULL if none)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) * Detect if the task was woken on the initial futex as opposed to the requeue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) * target futex. If so, determine if it was a timeout or a signal that caused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) * the wakeup and return the appropriate error code to the caller. Must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) * called with the hb lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) * - 0 = no early wakeup detected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) * - <0 = -ETIMEDOUT or -ERESTARTNOINTR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) static inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) struct futex_q *q, union futex_key *key2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) struct hrtimer_sleeper *timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) * With the hb lock held, we avoid races while we process the wakeup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) * We only need to hold hb (and not hb2) to ensure atomicity as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) * It can't be requeued from uaddr2 to something else since we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) * support a PI aware source futex for requeue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) if (!match_futex(&q->key, key2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) * We were woken prior to requeue by a timeout or a signal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) * Unqueue the futex_q and determine which it was.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) plist_del(&q->list, &hb->chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) hb_waiters_dec(hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) /* Handle spurious wakeups gracefully */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) ret = -EWOULDBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) if (timeout && !timeout->task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) ret = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) else if (signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) ret = -ERESTARTNOINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) * @uaddr: the futex we initially wait on (non-pi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) * the same type, no requeueing from private to shared, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) * @val: the expected value of uaddr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) * @abs_time: absolute timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) * @bitset: 32 bit wakeup bitset set by userspace, defaults to all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) * @uaddr2: the pi futex we will take prior to returning to user-space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) * The caller will wait on uaddr and will be requeued by futex_requeue() to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) * userspace. This ensures the rt_mutex maintains an owner when it has waiters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) * without one, the pi logic would not know which task to boost/deboost, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) * there was a need to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) * We call schedule in futex_wait_queue_me() when we enqueue and return there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) * via the following--
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) * 2) wakeup on uaddr2 after a requeue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) * 3) signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) * 4) timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) * If 3, cleanup and return -ERESTARTNOINTR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) * If 2, we may then block on trying to take the rt_mutex and return via:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) * 5) successful lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) * 6) signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) * 7) timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) * 8) other lock acquisition failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) * If 6, return -EWOULDBLOCK (restarting the syscall would do the same).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) * If 4 or 7, we cleanup and return with -ETIMEDOUT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) * - 0 - On success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) * - <0 - On error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) u32 val, ktime_t *abs_time, u32 bitset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) u32 __user *uaddr2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) struct hrtimer_sleeper timeout, *to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) struct rt_mutex_waiter rt_waiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) struct futex_hash_bucket *hb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) union futex_key key2 = FUTEX_KEY_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) struct futex_q q = futex_q_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) int res, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) if (!IS_ENABLED(CONFIG_FUTEX_PI))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) if (uaddr == uaddr2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) if (!bitset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) to = futex_setup_timer(abs_time, &timeout, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) current->timer_slack_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) * The waiter is allocated on our stack, manipulated by the requeue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) * code while we sleep on uaddr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) rt_mutex_init_waiter(&rt_waiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, FUTEX_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) if (unlikely(ret != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) q.bitset = bitset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) q.rt_waiter = &rt_waiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) q.requeue_pi_key = &key2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) * Prepare to wait on uaddr. On success, increments q.key (key1) ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) * count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) * The check above which compares uaddrs is not sufficient for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) * shared futexes. We need to compare the keys:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) if (match_futex(&q.key, &key2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) queue_unlock(hb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) /* Queue the futex_q, drop the hb lock, wait for wakeup. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) futex_wait_queue_me(hb, &q, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) spin_lock(&hb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) spin_unlock(&hb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) * In order for us to be here, we know our q.key == key2, and since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) * we took the hb->lock above, we also know that futex_requeue() has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) * completed and we no longer have to concern ourselves with a wakeup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) * race with the atomic proxy lock acquisition by the requeue code. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) * futex_requeue dropped our key1 reference and incremented our key2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) * reference count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) /* Check if the requeue code acquired the second futex for us. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) if (!q.rt_waiter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) * Got the lock. We might not be the anticipated owner if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) * did a lock-steal - fix up the PI-state in that case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) if (q.pi_state && (q.pi_state->owner != current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) spin_lock(q.lock_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) ret = fixup_pi_state_owner(uaddr2, &q, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) * Drop the reference to the pi state which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) * the requeue_pi() code acquired for us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) put_pi_state(q.pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) spin_unlock(q.lock_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) * Adjust the return value. It's either -EFAULT or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) * success (1) but the caller expects 0 for success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) ret = ret < 0 ? ret : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) struct rt_mutex *pi_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) * We have been woken up by futex_unlock_pi(), a timeout, or a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) * signal. futex_unlock_pi() will not destroy the lock_ptr nor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) * the pi_state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) WARN_ON(!q.pi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) pi_mutex = &q.pi_state->pi_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) ret = rt_mutex_wait_proxy_lock(pi_mutex, to, &rt_waiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) spin_lock(q.lock_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) if (ret && !rt_mutex_cleanup_proxy_lock(pi_mutex, &rt_waiter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) debug_rt_mutex_free_waiter(&rt_waiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) * Fixup the pi_state owner and possibly acquire the lock if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) * haven't already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) res = fixup_owner(uaddr2, &q, !ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) * If fixup_owner() returned an error, proprogate that. If it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) * acquired the lock, clear -ETIMEDOUT or -EINTR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) ret = (res < 0) ? res : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) /* Unqueue and drop the lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) unqueue_me_pi(&q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) if (ret == -EINTR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) * We've already been requeued, but cannot restart by calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) * futex_lock_pi() directly. We could restart this syscall, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) * it would detect that the user space "val" changed and return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) * -EWOULDBLOCK. Save the overhead of the restart and return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) * -EWOULDBLOCK directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) ret = -EWOULDBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) if (to) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) hrtimer_cancel(&to->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) destroy_hrtimer_on_stack(&to->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) * Support for robust futexes: the kernel cleans up held futexes at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) * thread exit time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) * Implementation: user-space maintains a per-thread list of locks it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) * is holding. Upon do_exit(), the kernel carefully walks this list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) * and marks all locks that are owned by this thread with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) * always manipulated with the lock held, so the list is private and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) * per-thread. Userspace also maintains a per-thread 'list_op_pending'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) * field, to allow the kernel to clean up if the thread dies after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) * acquiring the lock, but just before it could have added itself to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) * the list. There can only be one such pending lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) * sys_set_robust_list() - Set the robust-futex list head of a task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) * @head: pointer to the list-head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) * @len: length of the list-head, as userspace expects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) size_t, len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) if (!futex_cmpxchg_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) * The kernel knows only one size for now:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) if (unlikely(len != sizeof(*head)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) current->robust_list = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) * sys_get_robust_list() - Get the robust-futex list head of a task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) * @pid: pid of the process [zero for current task]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) * @head_ptr: pointer to a list-head pointer, the kernel fills it in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) * @len_ptr: pointer to a length field, the kernel fills in the header size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) SYSCALL_DEFINE3(get_robust_list, int, pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) struct robust_list_head __user * __user *, head_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) size_t __user *, len_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) struct robust_list_head __user *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) unsigned long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) struct task_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) if (!futex_cmpxchg_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) ret = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) if (!pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) p = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) p = find_task_by_vpid(pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) head = p->robust_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) if (put_user(sizeof(*head), len_ptr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) return put_user(head, head_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) err_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) /* Constants for the pending_op argument of handle_futex_death */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) #define HANDLE_DEATH_PENDING true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) #define HANDLE_DEATH_LIST false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) * Process a futex-list entry, check whether it's owned by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) * dying task, and do notification if so:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) static int handle_futex_death(u32 __user *uaddr, struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) bool pi, bool pending_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) u32 uval, nval, mval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) /* Futex address must be 32bit aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) if ((((unsigned long)uaddr) % sizeof(*uaddr)) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) if (get_user(uval, uaddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) * Special case for regular (non PI) futexes. The unlock path in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) * user space has two race scenarios:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) * 1. The unlock path releases the user space futex value and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) * before it can execute the futex() syscall to wake up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) * waiters it is killed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) * 2. A woken up waiter is killed before it can acquire the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) * futex in user space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) * In both cases the TID validation below prevents a wakeup of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) * potential waiters which can cause these waiters to block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) * forever.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) * In both cases the following conditions are met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) * 1) task->robust_list->list_op_pending != NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) * @pending_op == true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) * 2) User space futex value == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) * 3) Regular futex: @pi == false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) * If these conditions are met, it is safe to attempt waking up a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) * potential waiter without touching the user space futex value and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) * trying to set the OWNER_DIED bit. The user space futex value is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) * uncontended and the rest of the user space mutex state is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) * consistent, so a woken waiter will just take over the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) * uncontended futex. Setting the OWNER_DIED bit would create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) * inconsistent state and malfunction of the user space owner died
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) * handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) if (pending_op && !pi && !uval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) if ((uval & FUTEX_TID_MASK) != task_pid_vnr(curr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) * Ok, this dying thread is truly holding a futex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) * of interest. Set the OWNER_DIED bit atomically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) * via cmpxchg, and if the value had FUTEX_WAITERS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) * set, wake up a waiter (if any). (We have to do a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) * futex_wake() even if OWNER_DIED is already set -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) * to handle the rare but possible case of recursive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) * thread-death.) The rest of the cleanup is done in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) * userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) * We are not holding a lock here, but we want to have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) * the pagefault_disable/enable() protection because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) * we want to handle the fault gracefully. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) * access fails we try to fault in the futex with R/W
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) * verification via get_user_pages. get_user() above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) * does not guarantee R/W access. If that fails we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) * give up and leave the futex locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) if ((err = cmpxchg_futex_value_locked(&nval, uaddr, uval, mval))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) switch (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) case -EFAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) if (fault_in_user_writeable(uaddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) case -EAGAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) if (nval != uval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) * Wake robust non-PI futexes here. The wakeup of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) * PI futexes happens in exit_pi_state():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) if (!pi && (uval & FUTEX_WAITERS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) * Fetch a robust-list pointer. Bit 0 signals PI futexes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) static inline int fetch_robust_entry(struct robust_list __user **entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) struct robust_list __user * __user *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) unsigned int *pi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) unsigned long uentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) if (get_user(uentry, (unsigned long __user *)head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) *entry = (void __user *)(uentry & ~1UL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) *pi = uentry & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) * Walk curr->robust_list (very carefully, it's a userspace list!)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) * and mark any locks found there dead, and notify any waiters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) * We silently return on any sign of list-walking problem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) static void exit_robust_list(struct task_struct *curr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) struct robust_list_head __user *head = curr->robust_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) struct robust_list __user *entry, *next_entry, *pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) unsigned int next_pi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) unsigned long futex_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) if (!futex_cmpxchg_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) * Fetch the list head (which was registered earlier, via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) * sys_set_robust_list()):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) if (fetch_robust_entry(&entry, &head->list.next, &pi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) * Fetch the relative futex offset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) if (get_user(futex_offset, &head->futex_offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) * Fetch any possibly pending lock-add first, and handle it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) * if it exists:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) next_entry = NULL; /* avoid warning with gcc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) while (entry != &head->list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) * Fetch the next entry in the list before calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) * handle_futex_death:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) * A pending lock might already be on the list, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) * don't process it twice:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) if (entry != pending) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) if (handle_futex_death((void __user *)entry + futex_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) curr, pi, HANDLE_DEATH_LIST))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) entry = next_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) pi = next_pi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) * Avoid excessively long or circular lists:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) if (!--limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) if (pending) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) handle_futex_death((void __user *)pending + futex_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) curr, pip, HANDLE_DEATH_PENDING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) static void futex_cleanup(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) if (unlikely(tsk->robust_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) exit_robust_list(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) tsk->robust_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) if (unlikely(tsk->compat_robust_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) compat_exit_robust_list(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) tsk->compat_robust_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) if (unlikely(!list_empty(&tsk->pi_state_list)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) exit_pi_state_list(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) * futex_exit_recursive - Set the tasks futex state to FUTEX_STATE_DEAD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) * @tsk: task to set the state on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) * Set the futex exit state of the task lockless. The futex waiter code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) * observes that state when a task is exiting and loops until the task has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) * actually finished the futex cleanup. The worst case for this is that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) * waiter runs through the wait loop until the state becomes visible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) * This is called from the recursive fault handling path in do_exit().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) * This is best effort. Either the futex exit code has run already or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) * not. If the OWNER_DIED bit has been set on the futex then the waiter can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) * take it over. If not, the problem is pushed back to user space. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) * futex exit code did not run yet, then an already queued waiter might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) * block forever, but there is nothing which can be done about that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) void futex_exit_recursive(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) /* If the state is FUTEX_STATE_EXITING then futex_exit_mutex is held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) if (tsk->futex_state == FUTEX_STATE_EXITING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) mutex_unlock(&tsk->futex_exit_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) tsk->futex_state = FUTEX_STATE_DEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) static void futex_cleanup_begin(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) * Prevent various race issues against a concurrent incoming waiter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) * including live locks by forcing the waiter to block on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) * tsk->futex_exit_mutex when it observes FUTEX_STATE_EXITING in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) * attach_to_pi_owner().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) mutex_lock(&tsk->futex_exit_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) * Switch the state to FUTEX_STATE_EXITING under tsk->pi_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) * This ensures that all subsequent checks of tsk->futex_state in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) * attach_to_pi_owner() must observe FUTEX_STATE_EXITING with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) * tsk->pi_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) * It guarantees also that a pi_state which was queued right before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) * the state change under tsk->pi_lock by a concurrent waiter must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) * be observed in exit_pi_state_list().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) raw_spin_lock_irq(&tsk->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) tsk->futex_state = FUTEX_STATE_EXITING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) raw_spin_unlock_irq(&tsk->pi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) static void futex_cleanup_end(struct task_struct *tsk, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) * Lockless store. The only side effect is that an observer might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) * take another loop until it becomes visible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) tsk->futex_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) * Drop the exit protection. This unblocks waiters which observed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) * FUTEX_STATE_EXITING to reevaluate the state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) mutex_unlock(&tsk->futex_exit_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) void futex_exec_release(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) * The state handling is done for consistency, but in the case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) * exec() there is no way to prevent futher damage as the PID stays
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) * the same. But for the unlikely and arguably buggy case that a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) * futex is held on exec(), this provides at least as much state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) * consistency protection which is possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) futex_cleanup_begin(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) futex_cleanup(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) * Reset the state to FUTEX_STATE_OK. The task is alive and about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) * exec a new binary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) futex_cleanup_end(tsk, FUTEX_STATE_OK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) void futex_exit_release(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) futex_cleanup_begin(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) futex_cleanup(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) futex_cleanup_end(tsk, FUTEX_STATE_DEAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) u32 __user *uaddr2, u32 val2, u32 val3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) int cmd = op & FUTEX_CMD_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) unsigned int flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) if (!(op & FUTEX_PRIVATE_FLAG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) flags |= FLAGS_SHARED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) if (op & FUTEX_CLOCK_REALTIME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) flags |= FLAGS_CLOCKRT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) if (cmd != FUTEX_WAIT_BITSET && cmd != FUTEX_WAIT_REQUEUE_PI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) case FUTEX_LOCK_PI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) case FUTEX_UNLOCK_PI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) case FUTEX_TRYLOCK_PI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) case FUTEX_WAIT_REQUEUE_PI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) case FUTEX_CMP_REQUEUE_PI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) if (!futex_cmpxchg_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) case FUTEX_WAIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) val3 = FUTEX_BITSET_MATCH_ANY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) case FUTEX_WAIT_BITSET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) return futex_wait(uaddr, flags, val, timeout, val3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) case FUTEX_WAKE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) val3 = FUTEX_BITSET_MATCH_ANY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) case FUTEX_WAKE_BITSET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) return futex_wake(uaddr, flags, val, val3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) case FUTEX_REQUEUE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) return futex_requeue(uaddr, flags, uaddr2, val, val2, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) case FUTEX_CMP_REQUEUE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) case FUTEX_WAKE_OP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) case FUTEX_LOCK_PI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) return futex_lock_pi(uaddr, flags, timeout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) case FUTEX_UNLOCK_PI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) return futex_unlock_pi(uaddr, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) case FUTEX_TRYLOCK_PI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) return futex_lock_pi(uaddr, flags, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) case FUTEX_WAIT_REQUEUE_PI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) val3 = FUTEX_BITSET_MATCH_ANY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) uaddr2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) case FUTEX_CMP_REQUEUE_PI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) struct __kernel_timespec __user *, utime, u32 __user *, uaddr2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) u32, val3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) struct timespec64 ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) ktime_t t, *tp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) u32 val2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) int cmd = op & FUTEX_CMD_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) cmd == FUTEX_WAIT_BITSET ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) cmd == FUTEX_WAIT_REQUEUE_PI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) if (unlikely(should_fail_futex(!(op & FUTEX_PRIVATE_FLAG))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) if (get_timespec64(&ts, utime))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) if (!timespec64_valid(&ts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) t = timespec64_to_ktime(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) if (cmd == FUTEX_WAIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) t = ktime_add_safe(ktime_get(), t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) else if (cmd != FUTEX_LOCK_PI && !(op & FUTEX_CLOCK_REALTIME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) t = timens_ktime_to_host(CLOCK_MONOTONIC, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) tp = &t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) * requeue parameter in 'utime' if cmd == FUTEX_*_REQUEUE_*.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) val2 = (u32) (unsigned long) utime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) * Fetch a robust-list pointer. Bit 0 signals PI futexes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) compat_fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) compat_uptr_t __user *head, unsigned int *pi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) if (get_user(*uentry, head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) *entry = compat_ptr((*uentry) & ~1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) *pi = (unsigned int)(*uentry) & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) static void __user *futex_uaddr(struct robust_list __user *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) compat_long_t futex_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) compat_uptr_t base = ptr_to_compat(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) void __user *uaddr = compat_ptr(base + futex_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) return uaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) * Walk curr->robust_list (very carefully, it's a userspace list!)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) * and mark any locks found there dead, and notify any waiters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) * We silently return on any sign of list-walking problem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) static void compat_exit_robust_list(struct task_struct *curr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) struct compat_robust_list_head __user *head = curr->compat_robust_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) struct robust_list __user *entry, *next_entry, *pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) unsigned int next_pi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) compat_uptr_t uentry, next_uentry, upending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) compat_long_t futex_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) if (!futex_cmpxchg_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) * Fetch the list head (which was registered earlier, via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) * sys_set_robust_list()):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) if (compat_fetch_robust_entry(&uentry, &entry, &head->list.next, &pi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) * Fetch the relative futex offset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) if (get_user(futex_offset, &head->futex_offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) * Fetch any possibly pending lock-add first, and handle it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) * if it exists:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) if (compat_fetch_robust_entry(&upending, &pending,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) &head->list_op_pending, &pip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) next_entry = NULL; /* avoid warning with gcc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) while (entry != (struct robust_list __user *) &head->list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) * Fetch the next entry in the list before calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) * handle_futex_death:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) rc = compat_fetch_robust_entry(&next_uentry, &next_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) (compat_uptr_t __user *)&entry->next, &next_pi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880) * A pending lock might already be on the list, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) * dont process it twice:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) if (entry != pending) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) void __user *uaddr = futex_uaddr(entry, futex_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) if (handle_futex_death(uaddr, curr, pi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) HANDLE_DEATH_LIST))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) uentry = next_uentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) entry = next_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) pi = next_pi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) * Avoid excessively long or circular lists:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) if (!--limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) if (pending) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) void __user *uaddr = futex_uaddr(pending, futex_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) handle_futex_death(uaddr, curr, pip, HANDLE_DEATH_PENDING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) COMPAT_SYSCALL_DEFINE2(set_robust_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) struct compat_robust_list_head __user *, head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) compat_size_t, len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) if (!futex_cmpxchg_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) if (unlikely(len != sizeof(*head)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) current->compat_robust_list = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) COMPAT_SYSCALL_DEFINE3(get_robust_list, int, pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) compat_uptr_t __user *, head_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) compat_size_t __user *, len_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) struct compat_robust_list_head __user *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) unsigned long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) struct task_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) if (!futex_cmpxchg_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) ret = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) if (!pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) p = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) p = find_task_by_vpid(pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) head = p->compat_robust_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) if (put_user(sizeof(*head), len_ptr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) return put_user(ptr_to_compat(head), head_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) err_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) #endif /* CONFIG_COMPAT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) #ifdef CONFIG_COMPAT_32BIT_TIME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) SYSCALL_DEFINE6(futex_time32, u32 __user *, uaddr, int, op, u32, val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) struct old_timespec32 __user *, utime, u32 __user *, uaddr2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) u32, val3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) struct timespec64 ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) ktime_t t, *tp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) int val2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) int cmd = op & FUTEX_CMD_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) cmd == FUTEX_WAIT_BITSET ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) cmd == FUTEX_WAIT_REQUEUE_PI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) if (get_old_timespec32(&ts, utime))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) if (!timespec64_valid(&ts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) t = timespec64_to_ktime(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) if (cmd == FUTEX_WAIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) t = ktime_add_safe(ktime_get(), t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) else if (cmd != FUTEX_LOCK_PI && !(op & FUTEX_CLOCK_REALTIME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) t = timens_ktime_to_host(CLOCK_MONOTONIC, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) tp = &t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) val2 = (int) (unsigned long) utime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994) return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) #endif /* CONFIG_COMPAT_32BIT_TIME */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) static void __init futex_detect_cmpxchg(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) #ifndef CONFIG_HAVE_FUTEX_CMPXCHG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) u32 curval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) * This will fail and we want it. Some arch implementations do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) * runtime detection of the futex_atomic_cmpxchg_inatomic()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) * functionality. We want to know that before we call in any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) * of the complex code paths. Also we want to prevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) * registration of robust lists in that case. NULL is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) * guaranteed to fault and we get -EFAULT on functional
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) * implementation, the non-functional ones will return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) * -ENOSYS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) futex_cmpxchg_enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) static int __init futex_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) unsigned int futex_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) #if CONFIG_BASE_SMALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) futex_hashsize = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) futex_hashsize = roundup_pow_of_two(256 * num_possible_cpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) futex_queues = alloc_large_system_hash("futex", sizeof(*futex_queues),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) futex_hashsize, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) futex_hashsize < 256 ? HASH_SMALL : 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) &futex_shift, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) futex_hashsize, futex_hashsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) futex_hashsize = 1UL << futex_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) futex_detect_cmpxchg();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) for (i = 0; i < futex_hashsize; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) atomic_set(&futex_queues[i].waiters, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) plist_head_init(&futex_queues[i].chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) spin_lock_init(&futex_queues[i].lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) core_initcall(futex_init);