blob: 5cc3fed27d4c2e142d2edf1d582785c5902561e1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _LINUX_FUTEX_H
3#define _LINUX_FUTEX_H
4
Thomas Gleixnerba31c1a42019-11-06 22:55:36 +01005#include <linux/sched.h>
Thomas Gleixner2456e852016-12-25 11:38:40 +01006#include <linux/ktime.h>
Thomas Gleixnerba31c1a42019-11-06 22:55:36 +01007
David Howells607ca462012-10-13 10:46:48 +01008#include <uapi/linux/futex.h>
Ingo Molnar0771dfe2006-03-27 01:16:22 -08009
Mike Frysinger9064a672009-09-23 15:57:23 -070010struct inode;
11struct mm_struct;
12struct task_struct;
Mike Frysinger9064a672009-09-23 15:57:23 -070013
Rusty Russell9adef582007-05-08 00:26:42 -070014/*
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.
Eric Dumazet34f01cc2007-05-09 02:35:04 -070020 * 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
Rusty Russell9adef582007-05-08 00:26:42 -070032union 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};
Rusty Russell9adef582007-05-08 00:26:42 -070049
Peter Zijlstra38d47c12008-09-26 19:32:20 +020050#define FUTEX_KEY_INIT (union futex_key) { .both = { .ptr = NULL } }
51
Ingo Molnar0771dfe2006-03-27 01:16:22 -080052#ifdef CONFIG_FUTEX
Thomas Gleixner3d4775d2019-11-06 22:55:37 +010053enum {
54 FUTEX_STATE_OK,
Thomas Gleixner18f69432019-11-06 22:55:41 +010055 FUTEX_STATE_EXITING,
Thomas Gleixner3d4775d2019-11-06 22:55:37 +010056 FUTEX_STATE_DEAD,
57};
Thomas Gleixnerba31c1a42019-11-06 22:55:36 +010058
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;
Thomas Gleixner3d4775d2019-11-06 22:55:37 +010067 tsk->futex_state = FUTEX_STATE_OK;
Thomas Gleixner3f186d92019-11-06 22:55:44 +010068 mutex_init(&tsk->futex_exit_mutex);
Thomas Gleixner3d4775d2019-11-06 22:55:37 +010069}
70
Thomas Gleixner18f69432019-11-06 22:55:41 +010071void futex_exit_recursive(struct task_struct *tsk);
Thomas Gleixner150d7152019-11-06 22:55:39 +010072void futex_exit_release(struct task_struct *tsk);
73void futex_exec_release(struct task_struct *tsk);
Dominik Brodowski2de0db92018-03-11 11:34:26 +010074
75long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
76 u32 __user *uaddr2, u32 val2, u32 val3);
Ingo Molnar0771dfe2006-03-27 01:16:22 -080077#else
Thomas Gleixnerba31c1a42019-11-06 22:55:36 +010078static inline void futex_init_task(struct task_struct *tsk) { }
Thomas Gleixner18f69432019-11-06 22:55:41 +010079static inline void futex_exit_recursive(struct task_struct *tsk) { }
Thomas Gleixner150d7152019-11-06 22:55:39 +010080static inline void futex_exit_release(struct task_struct *tsk) { }
81static inline void futex_exec_release(struct task_struct *tsk) { }
Dominik Brodowski2de0db92018-03-11 11:34:26 +010082static 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}
Nicolas Pitrebc2eecd2017-08-01 00:31:32 -040088#endif
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#endif