futex.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * <0 - On error
  24. */
  25. static inline int
  26. arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr)
  27. {
  28. int oldval, ret;
  29. u32 tmp;
  30. preempt_disable();
  31. pagefault_disable();
  32. ret = -EFAULT;
  33. if (unlikely(get_user(oldval, uaddr) != 0))
  34. goto out_pagefault_enable;
  35. ret = 0;
  36. tmp = oldval;
  37. switch (op) {
  38. case FUTEX_OP_SET:
  39. tmp = oparg;
  40. break;
  41. case FUTEX_OP_ADD:
  42. tmp += oparg;
  43. break;
  44. case FUTEX_OP_OR:
  45. tmp |= oparg;
  46. break;
  47. case FUTEX_OP_ANDN:
  48. tmp &= ~oparg;
  49. break;
  50. case FUTEX_OP_XOR:
  51. tmp ^= oparg;
  52. break;
  53. default:
  54. ret = -ENOSYS;
  55. }
  56. if (ret == 0 && unlikely(put_user(tmp, uaddr) != 0))
  57. ret = -EFAULT;
  58. out_pagefault_enable:
  59. 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. * <0 - On error
  77. */
  78. static inline int
  79. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  80. u32 oldval, u32 newval)
  81. {
  82. u32 val;
  83. preempt_disable();
  84. if (unlikely(get_user(val, uaddr) != 0)) {
  85. preempt_enable();
  86. return -EFAULT;
  87. }
  88. if (val == oldval && unlikely(put_user(newval, uaddr) != 0)) {
  89. preempt_enable();
  90. return -EFAULT;
  91. }
  92. *uval = val;
  93. preempt_enable();
  94. return 0;
  95. }
  96. #else
  97. static inline int
  98. arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr)
  99. {
  100. int oldval = 0, ret;
  101. pagefault_disable();
  102. switch (op) {
  103. case FUTEX_OP_SET:
  104. case FUTEX_OP_ADD:
  105. case FUTEX_OP_OR:
  106. case FUTEX_OP_ANDN:
  107. case FUTEX_OP_XOR:
  108. default:
  109. ret = -ENOSYS;
  110. }
  111. pagefault_enable();
  112. if (!ret)
  113. *oval = oldval;
  114. return ret;
  115. }
  116. static inline int
  117. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  118. u32 oldval, u32 newval)
  119. {
  120. return -ENOSYS;
  121. }
  122. #endif /* CONFIG_SMP */
  123. #endif