xref: /linux-6.15/include/linux/futex.h (revision 3d4775df)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_FUTEX_H
3 #define _LINUX_FUTEX_H
4 
5 #include <linux/sched.h>
6 #include <linux/ktime.h>
7 
8 #include <uapi/linux/futex.h>
9 
10 struct inode;
11 struct mm_struct;
12 struct task_struct;
13 
14 /*
15  * Futexes are matched on equal values of this key.
16  * The key type depends on whether it's a shared or private mapping.
17  * Don't rearrange members without looking at hash_futex().
18  *
19  * offset is aligned to a multiple of sizeof(u32) (== 4) by definition.
20  * We use the two low order bits of offset to tell what is the kind of key :
21  *  00 : Private process futex (PTHREAD_PROCESS_PRIVATE)
22  *       (no reference on an inode or mm)
23  *  01 : Shared futex (PTHREAD_PROCESS_SHARED)
24  *	mapped on a file (reference on the underlying inode)
25  *  10 : Shared futex (PTHREAD_PROCESS_SHARED)
26  *       (but private mapping on an mm, and reference taken on it)
27 */
28 
29 #define FUT_OFF_INODE    1 /* We set bit 0 if key has a reference on inode */
30 #define FUT_OFF_MMSHARED 2 /* We set bit 1 if key has a reference on mm */
31 
32 union futex_key {
33 	struct {
34 		unsigned long pgoff;
35 		struct inode *inode;
36 		int offset;
37 	} shared;
38 	struct {
39 		unsigned long address;
40 		struct mm_struct *mm;
41 		int offset;
42 	} private;
43 	struct {
44 		unsigned long word;
45 		void *ptr;
46 		int offset;
47 	} both;
48 };
49 
50 #define FUTEX_KEY_INIT (union futex_key) { .both = { .ptr = NULL } }
51 
52 #ifdef CONFIG_FUTEX
53 enum {
54 	FUTEX_STATE_OK,
55 	FUTEX_STATE_DEAD,
56 };
57 
58 static inline void futex_init_task(struct task_struct *tsk)
59 {
60 	tsk->robust_list = NULL;
61 #ifdef CONFIG_COMPAT
62 	tsk->compat_robust_list = NULL;
63 #endif
64 	INIT_LIST_HEAD(&tsk->pi_state_list);
65 	tsk->pi_state_cache = NULL;
66 	tsk->futex_state = FUTEX_STATE_OK;
67 }
68 
69 /**
70  * futex_exit_done - Sets the tasks futex state to FUTEX_STATE_DEAD
71  * @tsk:	task to set the state on
72  *
73  * Set the futex exit state of the task lockless. The futex waiter code
74  * observes that state when a task is exiting and loops until the task has
75  * actually finished the futex cleanup. The worst case for this is that the
76  * waiter runs through the wait loop until the state becomes visible.
77  *
78  * This has two callers:
79  *
80  * - futex_mm_release() after the futex exit cleanup has been done
81  *
82  * - do_exit() from the recursive fault handling path.
83  *
84  * In case of a recursive fault this is best effort. Either the futex exit
85  * code has run already or not. If the OWNER_DIED bit has been set on the
86  * futex then the waiter can take it over. If not, the problem is pushed
87  * back to user space. If the futex exit code did not run yet, then an
88  * already queued waiter might block forever, but there is nothing which
89  * can be done about that.
90  */
91 static inline void futex_exit_done(struct task_struct *tsk)
92 {
93 	tsk->futex_state = FUTEX_STATE_DEAD;
94 }
95 
96 void futex_mm_release(struct task_struct *tsk);
97 
98 long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
99 	      u32 __user *uaddr2, u32 val2, u32 val3);
100 #else
101 static inline void futex_init_task(struct task_struct *tsk) { }
102 static inline void futex_mm_release(struct task_struct *tsk) { }
103 static inline void futex_exit_done(struct task_struct *tsk) { }
104 static inline long do_futex(u32 __user *uaddr, int op, u32 val,
105 			    ktime_t *timeout, u32 __user *uaddr2,
106 			    u32 val2, u32 val3)
107 {
108 	return -EINVAL;
109 }
110 #endif
111 
112 #endif
113