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