futex.h 2.6 KB

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