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#include <linux/mm_types.h>
8
9#include <uapi/linux/futex.h>
10
11struct inode;
12struct 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
32union futex_key {
33 struct {
34 u64 i_seq;
35 unsigned long pgoff;
36 unsigned int offset;
37 /* unsigned int node; */
38 } shared;
39 struct {
40 union {
41 struct mm_struct *mm;
42 u64 __tmp;
43 };
44 unsigned long address;
45 unsigned int offset;
46 /* unsigned int node; */
47 } private;
48 struct {
49 u64 ptr;
50 unsigned long word;
51 unsigned int offset;
52 unsigned int node; /* NOT hashed! */
53 } both;
54};
55
56#define FUTEX_KEY_INIT (union futex_key) { .both = { .ptr = 0ULL } }
57
58#ifdef CONFIG_FUTEX
59enum {
60 FUTEX_STATE_OK,
61 FUTEX_STATE_EXITING,
62 FUTEX_STATE_DEAD,
63};
64
65static 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(list: &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
77void futex_exit_recursive(struct task_struct *tsk);
78void futex_exit_release(struct task_struct *tsk);
79void futex_exec_release(struct task_struct *tsk);
80
81long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
82 u32 __user *uaddr2, u32 val2, u32 val3);
83int futex_hash_prctl(unsigned long arg2, unsigned long arg3, unsigned long arg4);
84
85#ifdef CONFIG_FUTEX_PRIVATE_HASH
86int futex_hash_allocate_default(void);
87void futex_hash_free(struct mm_struct *mm);
88int futex_mm_init(struct mm_struct *mm);
89
90#else /* !CONFIG_FUTEX_PRIVATE_HASH */
91static inline int futex_hash_allocate_default(void) { return 0; }
92static inline int futex_hash_free(struct mm_struct *mm) { return 0; }
93static inline int futex_mm_init(struct mm_struct *mm) { return 0; }
94#endif /* CONFIG_FUTEX_PRIVATE_HASH */
95
96#else /* !CONFIG_FUTEX */
97static inline void futex_init_task(struct task_struct *tsk) { }
98static inline void futex_exit_recursive(struct task_struct *tsk) { }
99static inline void futex_exit_release(struct task_struct *tsk) { }
100static inline void futex_exec_release(struct task_struct *tsk) { }
101static inline long do_futex(u32 __user *uaddr, int op, u32 val,
102 ktime_t *timeout, u32 __user *uaddr2,
103 u32 val2, u32 val3)
104{
105 return -EINVAL;
106}
107static inline int futex_hash_prctl(unsigned long arg2, unsigned long arg3, unsigned long arg4)
108{
109 return -EINVAL;
110}
111static inline int futex_hash_allocate_default(void)
112{
113 return 0;
114}
115static inline int futex_hash_free(struct mm_struct *mm) { return 0; }
116static inline int futex_mm_init(struct mm_struct *mm) { return 0; }
117
118#endif
119
120#endif
121