preempt.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_PREEMPT_H
  3. #define __ASM_PREEMPT_H
  4. #include <linux/jump_label.h>
  5. #include <linux/thread_info.h>
  6. #define PREEMPT_NEED_RESCHED BIT(32)
  7. #define PREEMPT_ENABLED (PREEMPT_NEED_RESCHED)
  8. static inline int preempt_count(void)
  9. {
  10. return READ_ONCE(current_thread_info()->preempt.count);
  11. }
  12. static inline void preempt_count_set(u64 pc)
  13. {
  14. /* Preserve existing value of PREEMPT_NEED_RESCHED */
  15. WRITE_ONCE(current_thread_info()->preempt.count, pc);
  16. }
  17. #define init_task_preempt_count(p) do { \
  18. task_thread_info(p)->preempt_count = FORK_PREEMPT_COUNT; \
  19. } while (0)
  20. #define init_idle_preempt_count(p, cpu) do { \
  21. task_thread_info(p)->preempt_count = PREEMPT_DISABLED; \
  22. } while (0)
  23. static inline void set_preempt_need_resched(void)
  24. {
  25. current_thread_info()->preempt.need_resched = 0;
  26. }
  27. static inline void clear_preempt_need_resched(void)
  28. {
  29. current_thread_info()->preempt.need_resched = 1;
  30. }
  31. static inline bool test_preempt_need_resched(void)
  32. {
  33. return !current_thread_info()->preempt.need_resched;
  34. }
  35. static inline void __preempt_count_add(int val)
  36. {
  37. u32 pc = READ_ONCE(current_thread_info()->preempt.count);
  38. pc += val;
  39. WRITE_ONCE(current_thread_info()->preempt.count, pc);
  40. }
  41. static inline void __preempt_count_sub(int val)
  42. {
  43. u32 pc = READ_ONCE(current_thread_info()->preempt.count);
  44. pc -= val;
  45. WRITE_ONCE(current_thread_info()->preempt.count, pc);
  46. }
  47. static inline bool __preempt_count_dec_and_test(void)
  48. {
  49. struct thread_info *ti = current_thread_info();
  50. u64 pc = READ_ONCE(ti->preempt_count);
  51. /* Update only the count field, leaving need_resched unchanged */
  52. WRITE_ONCE(ti->preempt.count, --pc);
  53. /*
  54. * If we wrote back all zeroes, then we're preemptible and in
  55. * need of a reschedule. Otherwise, we need to reload the
  56. * preempt_count in case the need_resched flag was cleared by an
  57. * interrupt occurring between the non-atomic READ_ONCE/WRITE_ONCE
  58. * pair.
  59. */
  60. return !pc || !READ_ONCE(ti->preempt_count);
  61. }
  62. static inline bool should_resched(int preempt_offset)
  63. {
  64. u64 pc = READ_ONCE(current_thread_info()->preempt_count);
  65. return pc == preempt_offset;
  66. }
  67. #ifdef CONFIG_PREEMPTION
  68. void preempt_schedule(void);
  69. void preempt_schedule_notrace(void);
  70. #ifdef CONFIG_PREEMPT_DYNAMIC
  71. DECLARE_STATIC_KEY_TRUE(sk_dynamic_irqentry_exit_cond_resched);
  72. void dynamic_preempt_schedule(void);
  73. #define __preempt_schedule() dynamic_preempt_schedule()
  74. void dynamic_preempt_schedule_notrace(void);
  75. #define __preempt_schedule_notrace() dynamic_preempt_schedule_notrace()
  76. #else /* CONFIG_PREEMPT_DYNAMIC */
  77. #define __preempt_schedule() preempt_schedule()
  78. #define __preempt_schedule_notrace() preempt_schedule_notrace()
  79. #endif /* CONFIG_PREEMPT_DYNAMIC */
  80. #endif /* CONFIG_PREEMPTION */
  81. #endif /* __ASM_PREEMPT_H */