futex.h 2.4 KB

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