1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_PREEMPT_H
3 #define __ASM_PREEMPT_H
5 #include <linux/jump_label.h>
6 #include <linux/thread_info.h>
8 #define PREEMPT_NEED_RESCHED BIT(32)
9 #define PREEMPT_ENABLED (PREEMPT_NEED_RESCHED)
11 static inline int preempt_count(void)
13 return READ_ONCE(current_thread_info()->preempt
.count
);
16 static inline void preempt_count_set(u64 pc
)
18 /* Preserve existing value of PREEMPT_NEED_RESCHED */
19 WRITE_ONCE(current_thread_info()->preempt
.count
, pc
);
22 #define init_task_preempt_count(p) do { \
23 task_thread_info(p)->preempt_count = FORK_PREEMPT_COUNT; \
26 #define init_idle_preempt_count(p, cpu) do { \
27 task_thread_info(p)->preempt_count = PREEMPT_DISABLED; \
30 static inline void set_preempt_need_resched(void)
32 current_thread_info()->preempt
.need_resched
= 0;
35 static inline void clear_preempt_need_resched(void)
37 current_thread_info()->preempt
.need_resched
= 1;
40 static inline bool test_preempt_need_resched(void)
42 return !current_thread_info()->preempt
.need_resched
;
45 static inline void __preempt_count_add(int val
)
47 u32 pc
= READ_ONCE(current_thread_info()->preempt
.count
);
49 WRITE_ONCE(current_thread_info()->preempt
.count
, pc
);
52 static inline void __preempt_count_sub(int val
)
54 u32 pc
= READ_ONCE(current_thread_info()->preempt
.count
);
56 WRITE_ONCE(current_thread_info()->preempt
.count
, pc
);
59 static inline bool __preempt_count_dec_and_test(void)
61 struct thread_info
*ti
= current_thread_info();
62 u64 pc
= READ_ONCE(ti
->preempt_count
);
64 /* Update only the count field, leaving need_resched unchanged */
65 WRITE_ONCE(ti
->preempt
.count
, --pc
);
68 * If we wrote back all zeroes, then we're preemptible and in
69 * need of a reschedule. Otherwise, we need to reload the
70 * preempt_count in case the need_resched flag was cleared by an
71 * interrupt occurring between the non-atomic READ_ONCE/WRITE_ONCE
74 return !pc
|| !READ_ONCE(ti
->preempt_count
);
77 static inline bool should_resched(int preempt_offset
)
79 u64 pc
= READ_ONCE(current_thread_info()->preempt_count
);
80 return pc
== preempt_offset
;
83 #ifdef CONFIG_PREEMPTION
85 void preempt_schedule(void);
86 void preempt_schedule_notrace(void);
88 #ifdef CONFIG_PREEMPT_DYNAMIC
90 DECLARE_STATIC_KEY_TRUE(sk_dynamic_irqentry_exit_cond_resched
);
91 void dynamic_preempt_schedule(void);
92 #define __preempt_schedule() dynamic_preempt_schedule()
93 void dynamic_preempt_schedule_notrace(void);
94 #define __preempt_schedule_notrace() dynamic_preempt_schedule_notrace()
96 #else /* CONFIG_PREEMPT_DYNAMIC */
98 #define __preempt_schedule() preempt_schedule()
99 #define __preempt_schedule_notrace() preempt_schedule_notrace()
101 #endif /* CONFIG_PREEMPT_DYNAMIC */
102 #endif /* CONFIG_PREEMPTION */
104 #endif /* __ASM_PREEMPT_H */