blob: 5cc3fed27d4c2e142d2edf1d582785c5902561e1 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_FUTEX_H
3#define _LINUX_FUTEX_H
4
David Brazdil0f672f62019-12-10 10:32:29 +00005#include <linux/sched.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006#include <linux/ktime.h>
David Brazdil0f672f62019-12-10 10:32:29 +00007
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008#include <uapi/linux/futex.h>
9
10struct inode;
11struct mm_struct;
12struct task_struct;
13
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014/*
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 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
David Brazdil0f672f62019-12-10 10:32:29 +000053enum {
54 FUTEX_STATE_OK,
55 FUTEX_STATE_EXITING,
56 FUTEX_STATE_DEAD,
57};
58
59static inline void futex_init_task(struct task_struct *tsk)
60{
61 tsk->robust_list = NULL;
62#ifdef CONFIG_COMPAT
63 tsk->compat_robust_list = NULL;
64#endif
65 INIT_LIST_HEAD(&tsk->pi_state_list);
66 tsk->pi_state_cache = NULL;
67 tsk->futex_state = FUTEX_STATE_OK;
68 mutex_init(&tsk->futex_exit_mutex);
69}
70
71void futex_exit_recursive(struct task_struct *tsk);
72void futex_exit_release(struct task_struct *tsk);
73void futex_exec_release(struct task_struct *tsk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000074
75long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
76 u32 __user *uaddr2, u32 val2, u32 val3);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077#else
David Brazdil0f672f62019-12-10 10:32:29 +000078static inline void futex_init_task(struct task_struct *tsk) { }
79static inline void futex_exit_recursive(struct task_struct *tsk) { }
80static inline void futex_exit_release(struct task_struct *tsk) { }
81static inline void futex_exec_release(struct task_struct *tsk) { }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000082static inline long do_futex(u32 __user *uaddr, int op, u32 val,
83 ktime_t *timeout, u32 __user *uaddr2,
84 u32 val2, u32 val3)
85{
86 return -EINVAL;
87}
88#endif
89
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000090#endif