GNU Linux-libre 4.14.303-gnu1
[releases.git] / include / linux / futex.h
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 long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
15               u32 __user *uaddr2, u32 val2, u32 val3);
16
17 /*
18  * Futexes are matched on equal values of this key.
19  * The key type depends on whether it's a shared or private mapping.
20  * Don't rearrange members without looking at hash_futex().
21  *
22  * offset is aligned to a multiple of sizeof(u32) (== 4) by definition.
23  * We use the two low order bits of offset to tell what is the kind of key :
24  *  00 : Private process futex (PTHREAD_PROCESS_PRIVATE)
25  *       (no reference on an inode or mm)
26  *  01 : Shared futex (PTHREAD_PROCESS_SHARED)
27  *      mapped on a file (reference on the underlying inode)
28  *  10 : Shared futex (PTHREAD_PROCESS_SHARED)
29  *       (but private mapping on an mm, and reference taken on it)
30 */
31
32 #define FUT_OFF_INODE    1 /* We set bit 0 if key has a reference on inode */
33 #define FUT_OFF_MMSHARED 2 /* We set bit 1 if key has a reference on mm */
34
35 union futex_key {
36         struct {
37                 u64 i_seq;
38                 unsigned long pgoff;
39                 unsigned int offset;
40         } shared;
41         struct {
42                 union {
43                         struct mm_struct *mm;
44                         u64 __tmp;
45                 };
46                 unsigned long address;
47                 unsigned int offset;
48         } private;
49         struct {
50                 u64 ptr;
51                 unsigned long word;
52                 unsigned int offset;
53         } both;
54 };
55
56 #define FUTEX_KEY_INIT (union futex_key) { .both = { .ptr = 0ULL } }
57
58 #ifdef CONFIG_FUTEX
59 enum {
60         FUTEX_STATE_OK,
61         FUTEX_STATE_EXITING,
62         FUTEX_STATE_DEAD,
63 };
64
65 static inline void futex_init_task(struct task_struct *tsk)
66 {
67         tsk->robust_list = NULL;
68 #ifdef CONFIG_COMPAT
69         tsk->compat_robust_list = NULL;
70 #endif
71         INIT_LIST_HEAD(&tsk->pi_state_list);
72         tsk->pi_state_cache = NULL;
73         tsk->futex_state = FUTEX_STATE_OK;
74         mutex_init(&tsk->futex_exit_mutex);
75 }
76
77 void futex_exit_recursive(struct task_struct *tsk);
78 void futex_exit_release(struct task_struct *tsk);
79 void futex_exec_release(struct task_struct *tsk);
80
81 long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
82               u32 __user *uaddr2, u32 val2, u32 val3);
83 #else
84 static inline void futex_init_task(struct task_struct *tsk) { }
85 static inline void futex_exit_recursive(struct task_struct *tsk) { }
86 static inline void futex_exit_release(struct task_struct *tsk) { }
87 static inline void futex_exec_release(struct task_struct *tsk) { }
88 #endif
89
90 #endif