xref: /linux/arch/x86/include/asm/preempt.h (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_PREEMPT_H
3 #define __ASM_PREEMPT_H
4 
5 #include <asm/rmwcc.h>
6 #include <asm/percpu.h>
7 
8 #include <linux/static_call_types.h>
9 
10 DECLARE_PER_CPU_CACHE_HOT(unsigned long, __preempt_count);
11 
12 /*
13  * We use the MSB for PREEMPT_NEED_RESCHED mostly because it is available.
14  */
15 #define PREEMPT_NEED_RESCHED	(~(((unsigned long)-1L) >> 1))
16 
17 #ifdef CONFIG_HAS_SEPARATE_PREEMPT_RESCHED_BITS
18 #define __pc_dec		"decq"
19 #define __pc_op(op, ...)	raw_cpu_##op##_8(__VA_ARGS__)
20 #else
21 #define __pc_dec		"decl"
22 #define __pc_op(op, ...)	raw_cpu_##op##_4(__VA_ARGS__)
23 #endif
24 
25 /*
26  * We use the PREEMPT_NEED_RESCHED bit as an inverted NEED_RESCHED such
27  * that a decrement hitting 0 means we can and should reschedule.
28  */
29 #define PREEMPT_ENABLED	(0 + PREEMPT_NEED_RESCHED)
30 
31 /*
32  * We mask the PREEMPT_NEED_RESCHED bit so as not to confuse all current users
33  * that think a non-zero value indicates we cannot preempt.
34  */
35 static __always_inline int preempt_count(void)
36 {
37 	return __pc_op(read, __preempt_count) & ~PREEMPT_NEED_RESCHED;
38 }
39 
40 /*
41  * unsigned long preempt count parameter works for both 32bit and 64bit cases:
42  *
43  * - For 32bit, "int" (the return of preempt_count()) and "unsigned long" have
44  *   the same size.
45  * - For 64bit, the effective bits of a preempt count sit in 32bit, and we
46  *   preserve the NEED_RESCHED bit from the old count.
47  */
48 static __always_inline void preempt_count_set(unsigned long pc)
49 {
50 	unsigned long old, new;
51 
52 	old = __pc_op(read, __preempt_count);
53 	do {
54 		new = (old & PREEMPT_NEED_RESCHED) |
55 			(pc & ~PREEMPT_NEED_RESCHED);
56 	} while (!__pc_op(try_cmpxchg, __preempt_count, &old, new));
57 }
58 
59 /*
60  * must be macros to avoid header recursion hell
61  */
62 #define init_task_preempt_count(p) do { } while (0)
63 
64 #define init_idle_preempt_count(p, cpu) do { \
65 	per_cpu(__preempt_count, (cpu)) = PREEMPT_DISABLED; \
66 } while (0)
67 
68 /*
69  * We fold the NEED_RESCHED bit into the preempt count such that
70  * preempt_enable() can decrement and test for needing to reschedule with a
71  * single instruction.
72  *
73  * We invert the actual bit, so that when the decrement hits 0 we know we both
74  * need to resched (the bit is cleared) and can resched (no preempt count).
75  */
76 
77 static __always_inline void set_preempt_need_resched(void)
78 {
79 	__pc_op(and, __preempt_count, ~PREEMPT_NEED_RESCHED);
80 }
81 
82 static __always_inline void clear_preempt_need_resched(void)
83 {
84 	__pc_op(or, __preempt_count, PREEMPT_NEED_RESCHED);
85 }
86 
87 static __always_inline bool test_preempt_need_resched(void)
88 {
89 	return !(__pc_op(read, __preempt_count) & PREEMPT_NEED_RESCHED);
90 }
91 
92 /*
93  * The various preempt_count add/sub methods
94  */
95 
96 static __always_inline void __preempt_count_add(int val)
97 {
98 	__pc_op(add, __preempt_count, val);
99 }
100 
101 static __always_inline void __preempt_count_sub(int val)
102 {
103 	__pc_op(add, __preempt_count, -val);
104 }
105 
106 static __always_inline int __preempt_count_add_return(int val)
107 {
108 	return __pc_op(add_return, __preempt_count, val);
109 }
110 
111 static __always_inline int __preempt_count_sub_return(int val)
112 {
113 	return __pc_op(add_return, __preempt_count, -val);
114 }
115 
116 /*
117  * Because we keep PREEMPT_NEED_RESCHED set when we do _not_ need to reschedule
118  * a decrement which hits zero means we have no preempt_count and should
119  * reschedule.
120  */
121 static __always_inline bool __preempt_count_dec_and_test(void)
122 {
123 	return GEN_UNARY_RMWcc(__pc_dec, __my_cpu_var(__preempt_count), e,
124 			       __percpu_arg([var]));
125 }
126 
127 /*
128  * Returns true when we need to resched and can (barring IRQ state).
129  */
130 static __always_inline bool should_resched(int preempt_offset)
131 {
132 	return unlikely(__pc_op(read, __preempt_count) == preempt_offset);
133 }
134 
135 #ifdef CONFIG_PREEMPTION
136 
137 extern asmlinkage void preempt_schedule(void);
138 extern asmlinkage void preempt_schedule_thunk(void);
139 
140 #define preempt_schedule_dynamic_enabled	preempt_schedule_thunk
141 #define preempt_schedule_dynamic_disabled	NULL
142 
143 extern asmlinkage void preempt_schedule_notrace(void);
144 extern asmlinkage void preempt_schedule_notrace_thunk(void);
145 
146 #define preempt_schedule_notrace_dynamic_enabled	preempt_schedule_notrace_thunk
147 #define preempt_schedule_notrace_dynamic_disabled	NULL
148 
149 #ifdef CONFIG_PREEMPT_DYNAMIC
150 
151 DECLARE_STATIC_CALL(preempt_schedule, preempt_schedule_dynamic_enabled);
152 
153 #define __preempt_schedule() \
154 do { \
155 	__STATIC_CALL_MOD_ADDRESSABLE(preempt_schedule); \
156 	asm volatile ("call " STATIC_CALL_TRAMP_STR(preempt_schedule) : ASM_CALL_CONSTRAINT); \
157 } while (0)
158 
159 DECLARE_STATIC_CALL(preempt_schedule_notrace, preempt_schedule_notrace_dynamic_enabled);
160 
161 #define __preempt_schedule_notrace() \
162 do { \
163 	__STATIC_CALL_MOD_ADDRESSABLE(preempt_schedule_notrace); \
164 	asm volatile ("call " STATIC_CALL_TRAMP_STR(preempt_schedule_notrace) : ASM_CALL_CONSTRAINT); \
165 } while (0)
166 
167 #else /* PREEMPT_DYNAMIC */
168 
169 #define __preempt_schedule() \
170 	asm volatile ("call preempt_schedule_thunk" : ASM_CALL_CONSTRAINT);
171 
172 #define __preempt_schedule_notrace() \
173 	asm volatile ("call preempt_schedule_notrace_thunk" : ASM_CALL_CONSTRAINT);
174 
175 #endif /* PREEMPT_DYNAMIC */
176 
177 #endif /* PREEMPTION */
178 
179 #undef __pc_op
180 #undef __pc_dec
181 
182 #endif /* __ASM_PREEMPT_H */
183