Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __ASM_PREEMPT_H
0003 #define __ASM_PREEMPT_H
0004 
0005 #include <asm/rmwcc.h>
0006 #include <asm/percpu.h>
0007 #include <linux/thread_info.h>
0008 #include <linux/static_call_types.h>
0009 
0010 DECLARE_PER_CPU(int, __preempt_count);
0011 
0012 /* We use the MSB mostly because its available */
0013 #define PREEMPT_NEED_RESCHED    0x80000000
0014 
0015 /*
0016  * We use the PREEMPT_NEED_RESCHED bit as an inverted NEED_RESCHED such
0017  * that a decrement hitting 0 means we can and should reschedule.
0018  */
0019 #define PREEMPT_ENABLED (0 + PREEMPT_NEED_RESCHED)
0020 
0021 /*
0022  * We mask the PREEMPT_NEED_RESCHED bit so as not to confuse all current users
0023  * that think a non-zero value indicates we cannot preempt.
0024  */
0025 static __always_inline int preempt_count(void)
0026 {
0027     return raw_cpu_read_4(__preempt_count) & ~PREEMPT_NEED_RESCHED;
0028 }
0029 
0030 static __always_inline void preempt_count_set(int pc)
0031 {
0032     int old, new;
0033 
0034     do {
0035         old = raw_cpu_read_4(__preempt_count);
0036         new = (old & PREEMPT_NEED_RESCHED) |
0037             (pc & ~PREEMPT_NEED_RESCHED);
0038     } while (raw_cpu_cmpxchg_4(__preempt_count, old, new) != old);
0039 }
0040 
0041 /*
0042  * must be macros to avoid header recursion hell
0043  */
0044 #define init_task_preempt_count(p) do { } while (0)
0045 
0046 #define init_idle_preempt_count(p, cpu) do { \
0047     per_cpu(__preempt_count, (cpu)) = PREEMPT_DISABLED; \
0048 } while (0)
0049 
0050 /*
0051  * We fold the NEED_RESCHED bit into the preempt count such that
0052  * preempt_enable() can decrement and test for needing to reschedule with a
0053  * single instruction.
0054  *
0055  * We invert the actual bit, so that when the decrement hits 0 we know we both
0056  * need to resched (the bit is cleared) and can resched (no preempt count).
0057  */
0058 
0059 static __always_inline void set_preempt_need_resched(void)
0060 {
0061     raw_cpu_and_4(__preempt_count, ~PREEMPT_NEED_RESCHED);
0062 }
0063 
0064 static __always_inline void clear_preempt_need_resched(void)
0065 {
0066     raw_cpu_or_4(__preempt_count, PREEMPT_NEED_RESCHED);
0067 }
0068 
0069 static __always_inline bool test_preempt_need_resched(void)
0070 {
0071     return !(raw_cpu_read_4(__preempt_count) & PREEMPT_NEED_RESCHED);
0072 }
0073 
0074 /*
0075  * The various preempt_count add/sub methods
0076  */
0077 
0078 static __always_inline void __preempt_count_add(int val)
0079 {
0080     raw_cpu_add_4(__preempt_count, val);
0081 }
0082 
0083 static __always_inline void __preempt_count_sub(int val)
0084 {
0085     raw_cpu_add_4(__preempt_count, -val);
0086 }
0087 
0088 /*
0089  * Because we keep PREEMPT_NEED_RESCHED set when we do _not_ need to reschedule
0090  * a decrement which hits zero means we have no preempt_count and should
0091  * reschedule.
0092  */
0093 static __always_inline bool __preempt_count_dec_and_test(void)
0094 {
0095     return GEN_UNARY_RMWcc("decl", __preempt_count, e, __percpu_arg([var]));
0096 }
0097 
0098 /*
0099  * Returns true when we need to resched and can (barring IRQ state).
0100  */
0101 static __always_inline bool should_resched(int preempt_offset)
0102 {
0103     return unlikely(raw_cpu_read_4(__preempt_count) == preempt_offset);
0104 }
0105 
0106 #ifdef CONFIG_PREEMPTION
0107 
0108 extern asmlinkage void preempt_schedule(void);
0109 extern asmlinkage void preempt_schedule_thunk(void);
0110 
0111 #define preempt_schedule_dynamic_enabled    preempt_schedule_thunk
0112 #define preempt_schedule_dynamic_disabled   NULL
0113 
0114 extern asmlinkage void preempt_schedule_notrace(void);
0115 extern asmlinkage void preempt_schedule_notrace_thunk(void);
0116 
0117 #define preempt_schedule_notrace_dynamic_enabled    preempt_schedule_notrace_thunk
0118 #define preempt_schedule_notrace_dynamic_disabled   NULL
0119 
0120 #ifdef CONFIG_PREEMPT_DYNAMIC
0121 
0122 DECLARE_STATIC_CALL(preempt_schedule, preempt_schedule_dynamic_enabled);
0123 
0124 #define __preempt_schedule() \
0125 do { \
0126     __STATIC_CALL_MOD_ADDRESSABLE(preempt_schedule); \
0127     asm volatile ("call " STATIC_CALL_TRAMP_STR(preempt_schedule) : ASM_CALL_CONSTRAINT); \
0128 } while (0)
0129 
0130 DECLARE_STATIC_CALL(preempt_schedule_notrace, preempt_schedule_notrace_dynamic_enabled);
0131 
0132 #define __preempt_schedule_notrace() \
0133 do { \
0134     __STATIC_CALL_MOD_ADDRESSABLE(preempt_schedule_notrace); \
0135     asm volatile ("call " STATIC_CALL_TRAMP_STR(preempt_schedule_notrace) : ASM_CALL_CONSTRAINT); \
0136 } while (0)
0137 
0138 #else /* PREEMPT_DYNAMIC */
0139 
0140 #define __preempt_schedule() \
0141     asm volatile ("call preempt_schedule_thunk" : ASM_CALL_CONSTRAINT);
0142 
0143 #define __preempt_schedule_notrace() \
0144     asm volatile ("call preempt_schedule_notrace_thunk" : ASM_CALL_CONSTRAINT);
0145 
0146 #endif /* PREEMPT_DYNAMIC */
0147 
0148 #endif /* PREEMPTION */
0149 
0150 #endif /* __ASM_PREEMPT_H */