futex.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_GENERIC_FUTEX_H
  3. #define _ASM_GENERIC_FUTEX_H
  4. #include <linux/futex.h>
  5. #include <linux/uaccess.h>
  6. #include <asm/errno.h>
  7. #ifndef CONFIG_SMP
  8. /*
  9. * The following implementation only for uniprocessor machines.
  10. * It relies on preempt_disable() ensuring mutual exclusion.
  11. *
  12. */
  13. /**
  14. * arch_futex_atomic_op_inuser() - Atomic arithmetic operation with constant
  15. * argument and comparison of the previous
  16. * futex value with another constant.
  17. *
  18. * @encoded_op: encoded operation to execute
  19. * @uaddr: pointer to user space address
  20. *
  21. * Return:
  22. * 0 - On success
  23. * -EFAULT - User access resulted in a page fault
  24. * -EAGAIN - Atomic operation was unable to complete due to contention
  25. * -ENOSYS - Operation not supported
  26. */
  27. static inline int
  28. arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr)
  29. {
  30. int oldval, ret;
  31. u32 tmp;
  32. preempt_disable();
  33. ret = -EFAULT;
  34. if (unlikely(get_user(oldval, uaddr) != 0))
  35. goto out_pagefault_enable;
  36. ret = 0;
  37. tmp = oldval;
  38. switch (op) {
  39. case FUTEX_OP_SET:
  40. tmp = oparg;
  41. break;
  42. case FUTEX_OP_ADD:
  43. tmp += oparg;
  44. break;
  45. case FUTEX_OP_OR:
  46. tmp |= oparg;
  47. break;
  48. case FUTEX_OP_ANDN:
  49. tmp &= ~oparg;
  50. break;
  51. case FUTEX_OP_XOR:
  52. tmp ^= oparg;
  53. break;
  54. default:
  55. ret = -ENOSYS;
  56. }
  57. if (ret == 0 && unlikely(put_user(tmp, uaddr) != 0))
  58. ret = -EFAULT;
  59. out_pagefault_enable:
  60. preempt_enable();
  61. if (ret == 0)
  62. *oval = oldval;
  63. return ret;
  64. }
  65. /**
  66. * futex_atomic_cmpxchg_inatomic() - Compare and exchange the content of the
  67. * uaddr with newval if the current value is
  68. * oldval.
  69. * @uval: pointer to store content of @uaddr
  70. * @uaddr: pointer to user space address
  71. * @oldval: old value
  72. * @newval: new value to store to @uaddr
  73. *
  74. * Return:
  75. * 0 - On success
  76. * -EFAULT - User access resulted in a page fault
  77. * -EAGAIN - Atomic operation was unable to complete due to contention
  78. * -ENOSYS - Function not implemented (only if !HAVE_FUTEX_CMPXCHG)
  79. */
  80. static inline int
  81. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  82. u32 oldval, u32 newval)
  83. {
  84. u32 val;
  85. preempt_disable();
  86. if (unlikely(get_user(val, uaddr) != 0)) {
  87. preempt_enable();
  88. return -EFAULT;
  89. }
  90. if (val == oldval && unlikely(put_user(newval, uaddr) != 0)) {
  91. preempt_enable();
  92. return -EFAULT;
  93. }
  94. *uval = val;
  95. preempt_enable();
  96. return 0;
  97. }
  98. #else
  99. static inline int
  100. arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr)
  101. {
  102. return -ENOSYS;
  103. }
  104. static inline int
  105. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  106. u32 oldval, u32 newval)
  107. {
  108. return -ENOSYS;
  109. }
  110. #endif /* CONFIG_SMP */
  111. #endif