preempt.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_PREEMPT_H
  3. #define __ASM_PREEMPT_H
  4. #include <asm/rmwcc.h>
  5. #include <asm/percpu.h>
  6. #include <linux/thread_info.h>
  7. #include <linux/static_call_types.h>
  8. DECLARE_PER_CPU(int, __preempt_count);
  9. /* We use the MSB mostly because its available */
  10. #define PREEMPT_NEED_RESCHED 0x80000000
  11. /*
  12. * We use the PREEMPT_NEED_RESCHED bit as an inverted NEED_RESCHED such
  13. * that a decrement hitting 0 means we can and should reschedule.
  14. */
  15. #define PREEMPT_ENABLED (0 + PREEMPT_NEED_RESCHED)
  16. /*
  17. * We mask the PREEMPT_NEED_RESCHED bit so as not to confuse all current users
  18. * that think a non-zero value indicates we cannot preempt.
  19. */
  20. static __always_inline int preempt_count(void)
  21. {
  22. return raw_cpu_read_4(__preempt_count) & ~PREEMPT_NEED_RESCHED;
  23. }
  24. static __always_inline void preempt_count_set(int pc)
  25. {
  26. int old, new;
  27. do {
  28. old = raw_cpu_read_4(__preempt_count);
  29. new = (old & PREEMPT_NEED_RESCHED) |
  30. (pc & ~PREEMPT_NEED_RESCHED);
  31. } while (raw_cpu_cmpxchg_4(__preempt_count, old, new) != old);
  32. }
  33. /*
  34. * must be macros to avoid header recursion hell
  35. */
  36. #define init_task_preempt_count(p) do { } while (0)
  37. #define init_idle_preempt_count(p, cpu) do { \
  38. per_cpu(__preempt_count, (cpu)) = PREEMPT_DISABLED; \
  39. } while (0)
  40. /*
  41. * We fold the NEED_RESCHED bit into the preempt count such that
  42. * preempt_enable() can decrement and test for needing to reschedule with a
  43. * single instruction.
  44. *
  45. * We invert the actual bit, so that when the decrement hits 0 we know we both
  46. * need to resched (the bit is cleared) and can resched (no preempt count).
  47. */
  48. static __always_inline void set_preempt_need_resched(void)
  49. {
  50. raw_cpu_and_4(__preempt_count, ~PREEMPT_NEED_RESCHED);
  51. }
  52. static __always_inline void clear_preempt_need_resched(void)
  53. {
  54. raw_cpu_or_4(__preempt_count, PREEMPT_NEED_RESCHED);
  55. }
  56. static __always_inline bool test_preempt_need_resched(void)
  57. {
  58. return !(raw_cpu_read_4(__preempt_count) & PREEMPT_NEED_RESCHED);
  59. }
  60. /*
  61. * The various preempt_count add/sub methods
  62. */
  63. static __always_inline void __preempt_count_add(int val)
  64. {
  65. raw_cpu_add_4(__preempt_count, val);
  66. }
  67. static __always_inline void __preempt_count_sub(int val)
  68. {
  69. raw_cpu_add_4(__preempt_count, -val);
  70. }
  71. /*
  72. * Because we keep PREEMPT_NEED_RESCHED set when we do _not_ need to reschedule
  73. * a decrement which hits zero means we have no preempt_count and should
  74. * reschedule.
  75. */
  76. static __always_inline bool __preempt_count_dec_and_test(void)
  77. {
  78. return GEN_UNARY_RMWcc("decl", __preempt_count, e, __percpu_arg([var]));
  79. }
  80. /*
  81. * Returns true when we need to resched and can (barring IRQ state).
  82. */
  83. static __always_inline bool should_resched(int preempt_offset)
  84. {
  85. return unlikely(raw_cpu_read_4(__preempt_count) == preempt_offset);
  86. }
  87. #ifdef CONFIG_PREEMPTION
  88. extern asmlinkage void preempt_schedule(void);
  89. extern asmlinkage void preempt_schedule_thunk(void);
  90. #define preempt_schedule_dynamic_enabled preempt_schedule_thunk
  91. #define preempt_schedule_dynamic_disabled NULL
  92. extern asmlinkage void preempt_schedule_notrace(void);
  93. extern asmlinkage void preempt_schedule_notrace_thunk(void);
  94. #define preempt_schedule_notrace_dynamic_enabled preempt_schedule_notrace_thunk
  95. #define preempt_schedule_notrace_dynamic_disabled NULL
  96. #ifdef CONFIG_PREEMPT_DYNAMIC
  97. DECLARE_STATIC_CALL(preempt_schedule, preempt_schedule_dynamic_enabled);
  98. #define __preempt_schedule() \
  99. do { \
  100. __STATIC_CALL_MOD_ADDRESSABLE(preempt_schedule); \
  101. asm volatile ("call " STATIC_CALL_TRAMP_STR(preempt_schedule) : ASM_CALL_CONSTRAINT); \
  102. } while (0)
  103. DECLARE_STATIC_CALL(preempt_schedule_notrace, preempt_schedule_notrace_dynamic_enabled);
  104. #define __preempt_schedule_notrace() \
  105. do { \
  106. __STATIC_CALL_MOD_ADDRESSABLE(preempt_schedule_notrace); \
  107. asm volatile ("call " STATIC_CALL_TRAMP_STR(preempt_schedule_notrace) : ASM_CALL_CONSTRAINT); \
  108. } while (0)
  109. #else /* PREEMPT_DYNAMIC */
  110. #define __preempt_schedule() \
  111. asm volatile ("call preempt_schedule_thunk" : ASM_CALL_CONSTRAINT);
  112. #define __preempt_schedule_notrace() \
  113. asm volatile ("call preempt_schedule_notrace_thunk" : ASM_CALL_CONSTRAINT);
  114. #endif /* PREEMPT_DYNAMIC */
  115. #endif /* PREEMPTION */
  116. #endif /* __ASM_PREEMPT_H */