]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | #ifndef __LINUX_PREEMPT_H |
2 | #define __LINUX_PREEMPT_H | |
3 | ||
4 | /* | |
5 | * include/linux/preempt.h - macros for accessing and manipulating | |
6 | * preempt_count (used for kernel preemption, interrupt count, etc.) | |
7 | */ | |
8 | ||
f037360f | 9 | #include <linux/thread_info.h> |
1da177e4 | 10 | #include <linux/linkage.h> |
e107be36 | 11 | #include <linux/list.h> |
1da177e4 | 12 | |
4a2b4b22 PZ |
13 | static __always_inline int preempt_count(void) |
14 | { | |
15 | return current_thread_info()->preempt_count; | |
16 | } | |
17 | ||
18 | static __always_inline int *preempt_count_ptr(void) | |
19 | { | |
20 | return ¤t_thread_info()->preempt_count; | |
21 | } | |
22 | ||
23 | static __always_inline void preempt_count_set(int pc) | |
24 | { | |
25 | *preempt_count_ptr() = pc; | |
26 | } | |
27 | ||
6cd8a4bb | 28 | #if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_PREEMPT_TRACER) |
ec701584 HH |
29 | extern void add_preempt_count(int val); |
30 | extern void sub_preempt_count(int val); | |
1da177e4 | 31 | #else |
4a2b4b22 PZ |
32 | # define add_preempt_count(val) do { *preempt_count_ptr() += (val); } while (0) |
33 | # define sub_preempt_count(val) do { *preempt_count_ptr() -= (val); } while (0) | |
1da177e4 LT |
34 | #endif |
35 | ||
36 | #define inc_preempt_count() add_preempt_count(1) | |
37 | #define dec_preempt_count() sub_preempt_count(1) | |
38 | ||
1da177e4 LT |
39 | #ifdef CONFIG_PREEMPT |
40 | ||
41 | asmlinkage void preempt_schedule(void); | |
42 | ||
bdd4e85d FW |
43 | #define preempt_check_resched() \ |
44 | do { \ | |
45 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) \ | |
46 | preempt_schedule(); \ | |
47 | } while (0) | |
48 | ||
29bb9e5a SR |
49 | #ifdef CONFIG_CONTEXT_TRACKING |
50 | ||
51 | void preempt_schedule_context(void); | |
52 | ||
53 | #define preempt_check_resched_context() \ | |
54 | do { \ | |
55 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) \ | |
56 | preempt_schedule_context(); \ | |
57 | } while (0) | |
58 | #else | |
59 | ||
60 | #define preempt_check_resched_context() preempt_check_resched() | |
61 | ||
62 | #endif /* CONFIG_CONTEXT_TRACKING */ | |
63 | ||
bdd4e85d FW |
64 | #else /* !CONFIG_PREEMPT */ |
65 | ||
66 | #define preempt_check_resched() do { } while (0) | |
29bb9e5a | 67 | #define preempt_check_resched_context() do { } while (0) |
bdd4e85d FW |
68 | |
69 | #endif /* CONFIG_PREEMPT */ | |
70 | ||
71 | ||
72 | #ifdef CONFIG_PREEMPT_COUNT | |
73 | ||
1da177e4 LT |
74 | #define preempt_disable() \ |
75 | do { \ | |
76 | inc_preempt_count(); \ | |
77 | barrier(); \ | |
78 | } while (0) | |
79 | ||
ba74c144 | 80 | #define sched_preempt_enable_no_resched() \ |
1da177e4 LT |
81 | do { \ |
82 | barrier(); \ | |
83 | dec_preempt_count(); \ | |
84 | } while (0) | |
85 | ||
ba74c144 TG |
86 | #define preempt_enable_no_resched() sched_preempt_enable_no_resched() |
87 | ||
1da177e4 LT |
88 | #define preempt_enable() \ |
89 | do { \ | |
90 | preempt_enable_no_resched(); \ | |
d6f02913 | 91 | barrier(); \ |
1da177e4 LT |
92 | preempt_check_resched(); \ |
93 | } while (0) | |
94 | ||
50282528 SR |
95 | /* For debugging and tracer internals only! */ |
96 | #define add_preempt_count_notrace(val) \ | |
4a2b4b22 | 97 | do { *preempt_count_ptr() += (val); } while (0) |
50282528 | 98 | #define sub_preempt_count_notrace(val) \ |
4a2b4b22 | 99 | do { *preempt_count_ptr() -= (val); } while (0) |
50282528 SR |
100 | #define inc_preempt_count_notrace() add_preempt_count_notrace(1) |
101 | #define dec_preempt_count_notrace() sub_preempt_count_notrace(1) | |
102 | ||
103 | #define preempt_disable_notrace() \ | |
104 | do { \ | |
105 | inc_preempt_count_notrace(); \ | |
106 | barrier(); \ | |
107 | } while (0) | |
108 | ||
109 | #define preempt_enable_no_resched_notrace() \ | |
110 | do { \ | |
111 | barrier(); \ | |
112 | dec_preempt_count_notrace(); \ | |
113 | } while (0) | |
114 | ||
115 | /* preempt_check_resched is OK to trace */ | |
116 | #define preempt_enable_notrace() \ | |
117 | do { \ | |
118 | preempt_enable_no_resched_notrace(); \ | |
119 | barrier(); \ | |
29bb9e5a | 120 | preempt_check_resched_context(); \ |
50282528 SR |
121 | } while (0) |
122 | ||
bdd4e85d | 123 | #else /* !CONFIG_PREEMPT_COUNT */ |
1da177e4 | 124 | |
386afc91 LT |
125 | /* |
126 | * Even if we don't have any preemption, we need preempt disable/enable | |
127 | * to be barriers, so that we don't have things like get_user/put_user | |
128 | * that can cause faults and scheduling migrate into our preempt-protected | |
129 | * region. | |
130 | */ | |
131 | #define preempt_disable() barrier() | |
132 | #define sched_preempt_enable_no_resched() barrier() | |
133 | #define preempt_enable_no_resched() barrier() | |
134 | #define preempt_enable() barrier() | |
135 | ||
136 | #define preempt_disable_notrace() barrier() | |
137 | #define preempt_enable_no_resched_notrace() barrier() | |
138 | #define preempt_enable_notrace() barrier() | |
50282528 | 139 | |
bdd4e85d | 140 | #endif /* CONFIG_PREEMPT_COUNT */ |
1da177e4 | 141 | |
e107be36 AK |
142 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
143 | ||
144 | struct preempt_notifier; | |
145 | ||
146 | /** | |
147 | * preempt_ops - notifiers called when a task is preempted and rescheduled | |
148 | * @sched_in: we're about to be rescheduled: | |
149 | * notifier: struct preempt_notifier for the task being scheduled | |
150 | * cpu: cpu we're scheduled on | |
151 | * @sched_out: we've just been preempted | |
152 | * notifier: struct preempt_notifier for the task being preempted | |
153 | * next: the task that's kicking us out | |
8592e648 TH |
154 | * |
155 | * Please note that sched_in and out are called under different | |
156 | * contexts. sched_out is called with rq lock held and irq disabled | |
157 | * while sched_in is called without rq lock and irq enabled. This | |
158 | * difference is intentional and depended upon by its users. | |
e107be36 AK |
159 | */ |
160 | struct preempt_ops { | |
161 | void (*sched_in)(struct preempt_notifier *notifier, int cpu); | |
162 | void (*sched_out)(struct preempt_notifier *notifier, | |
163 | struct task_struct *next); | |
164 | }; | |
165 | ||
166 | /** | |
167 | * preempt_notifier - key for installing preemption notifiers | |
168 | * @link: internal use | |
169 | * @ops: defines the notifier functions to be called | |
170 | * | |
171 | * Usually used in conjunction with container_of(). | |
172 | */ | |
173 | struct preempt_notifier { | |
174 | struct hlist_node link; | |
175 | struct preempt_ops *ops; | |
176 | }; | |
177 | ||
178 | void preempt_notifier_register(struct preempt_notifier *notifier); | |
179 | void preempt_notifier_unregister(struct preempt_notifier *notifier); | |
180 | ||
181 | static inline void preempt_notifier_init(struct preempt_notifier *notifier, | |
182 | struct preempt_ops *ops) | |
183 | { | |
184 | INIT_HLIST_NODE(¬ifier->link); | |
185 | notifier->ops = ops; | |
186 | } | |
187 | ||
188 | #endif | |
189 | ||
1da177e4 | 190 | #endif /* __LINUX_PREEMPT_H */ |