futex.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. pagefault_disable();
  34. ret = -EFAULT;
  35. if (unlikely(get_user(oldval, uaddr) != 0))
  36. goto out_pagefault_enable;
  37. ret = 0;
  38. tmp = oldval;
  39. switch (op) {
  40. case FUTEX_OP_SET:
  41. tmp = oparg;
  42. break;
  43. case FUTEX_OP_ADD:
  44. tmp += oparg;
  45. break;
  46. case FUTEX_OP_OR:
  47. tmp |= oparg;
  48. break;
  49. case FUTEX_OP_ANDN:
  50. tmp &= ~oparg;
  51. break;
  52. case FUTEX_OP_XOR:
  53. tmp ^= oparg;
  54. break;
  55. default:
  56. ret = -ENOSYS;
  57. }
  58. if (ret == 0 && unlikely(put_user(tmp, uaddr) != 0))
  59. ret = -EFAULT;
  60. out_pagefault_enable:
  61. pagefault_enable();
  62. preempt_enable();
  63. if (ret == 0)
  64. *oval = oldval;
  65. return ret;
  66. }
  67. /**
  68. * futex_atomic_cmpxchg_inatomic() - Compare and exchange the content of the
  69. * uaddr with newval if the current value is
  70. * oldval.
  71. * @uval: pointer to store content of @uaddr
  72. * @uaddr: pointer to user space address
  73. * @oldval: old value
  74. * @newval: new value to store to @uaddr
  75. *
  76. * Return:
  77. * 0 - On success
  78. * -EFAULT - User access resulted in a page fault
  79. * -EAGAIN - Atomic operation was unable to complete due to contention
  80. * -ENOSYS - Function not implemented (only if !HAVE_FUTEX_CMPXCHG)
  81. */
  82. static inline int
  83. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  84. u32 oldval, u32 newval)
  85. {
  86. u32 val;
  87. preempt_disable();
  88. if (unlikely(get_user(val, uaddr) != 0)) {
  89. preempt_enable();
  90. return -EFAULT;
  91. }
  92. if (val == oldval && unlikely(put_user(newval, uaddr) != 0)) {
  93. preempt_enable();
  94. return -EFAULT;
  95. }
  96. *uval = val;
  97. preempt_enable();
  98. return 0;
  99. }
  100. #else
  101. static inline int
  102. arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr)
  103. {
  104. int oldval = 0, ret;
  105. pagefault_disable();
  106. switch (op) {
  107. case FUTEX_OP_SET:
  108. case FUTEX_OP_ADD:
  109. case FUTEX_OP_OR:
  110. case FUTEX_OP_ANDN:
  111. case FUTEX_OP_XOR:
  112. default:
  113. ret = -ENOSYS;
  114. }
  115. pagefault_enable();
  116. if (!ret)
  117. *oval = oldval;
  118. return ret;
  119. }
  120. static inline int
  121. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  122. u32 oldval, u32 newval)
  123. {
  124. return -ENOSYS;
  125. }
  126. #endif /* CONFIG_SMP */
  127. #endif