blob: c3046c9200630b15f9ced04213c8415258dd1c81 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Peter Zijlstraa7878702013-08-14 14:55:40 +02002#ifndef __ASM_PREEMPT_H
3#define __ASM_PREEMPT_H
4
5#include <linux/thread_info.h>
6
Peter Zijlstraba1f14f2013-11-28 14:26:41 +01007#define PREEMPT_ENABLED (0)
8
Peter Zijlstraa7878702013-08-14 14:55:40 +02009static __always_inline int preempt_count(void)
10{
Peter Zijlstra2e636d52016-05-16 15:01:11 +020011 return READ_ONCE(current_thread_info()->preempt_count);
Peter Zijlstraa7878702013-08-14 14:55:40 +020012}
13
Peter Zijlstra2e636d52016-05-16 15:01:11 +020014static __always_inline volatile int *preempt_count_ptr(void)
Peter Zijlstraa7878702013-08-14 14:55:40 +020015{
16 return &current_thread_info()->preempt_count;
17}
18
Peter Zijlstraa7878702013-08-14 14:55:40 +020019static __always_inline void preempt_count_set(int pc)
20{
21 *preempt_count_ptr() = pc;
22}
23
24/*
Peter Zijlstra01028742013-08-14 14:55:46 +020025 * must be macros to avoid header recursion hell
26 */
Peter Zijlstra01028742013-08-14 14:55:46 +020027#define init_task_preempt_count(p) do { \
Peter Zijlstra609ca062015-09-28 17:52:18 +020028 task_thread_info(p)->preempt_count = FORK_PREEMPT_COUNT; \
Peter Zijlstra01028742013-08-14 14:55:46 +020029} while (0)
30
31#define init_idle_preempt_count(p, cpu) do { \
32 task_thread_info(p)->preempt_count = PREEMPT_ENABLED; \
33} while (0)
34
Peter Zijlstraa7878702013-08-14 14:55:40 +020035static __always_inline void set_preempt_need_resched(void)
36{
Peter Zijlstraa7878702013-08-14 14:55:40 +020037}
38
39static __always_inline void clear_preempt_need_resched(void)
40{
Peter Zijlstraa7878702013-08-14 14:55:40 +020041}
42
43static __always_inline bool test_preempt_need_resched(void)
44{
Peter Zijlstraba1f14f2013-11-28 14:26:41 +010045 return false;
Peter Zijlstraa7878702013-08-14 14:55:40 +020046}
47
Peter Zijlstrabdb43802013-09-10 12:15:23 +020048/*
49 * The various preempt_count add/sub methods
50 */
51
52static __always_inline void __preempt_count_add(int val)
53{
54 *preempt_count_ptr() += val;
55}
56
57static __always_inline void __preempt_count_sub(int val)
58{
59 *preempt_count_ptr() -= val;
60}
61
62static __always_inline bool __preempt_count_dec_and_test(void)
63{
Peter Zijlstraba1f14f2013-11-28 14:26:41 +010064 /*
65 * Because of load-store architectures cannot do per-cpu atomic
66 * operations; we cannot use PREEMPT_NEED_RESCHED because it might get
67 * lost.
68 */
69 return !--*preempt_count_ptr() && tif_need_resched();
Peter Zijlstrabdb43802013-09-10 12:15:23 +020070}
71
72/*
Peter Zijlstrabdb43802013-09-10 12:15:23 +020073 * Returns true when we need to resched and can (barring IRQ state).
74 */
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +030075static __always_inline bool should_resched(int preempt_offset)
Peter Zijlstrabdb43802013-09-10 12:15:23 +020076{
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +030077 return unlikely(preempt_count() == preempt_offset &&
78 tif_need_resched());
Peter Zijlstrabdb43802013-09-10 12:15:23 +020079}
80
Peter Zijlstra1a338ac2013-08-14 14:51:00 +020081#ifdef CONFIG_PREEMPT
82extern asmlinkage void preempt_schedule(void);
83#define __preempt_schedule() preempt_schedule()
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +020084extern asmlinkage void preempt_schedule_notrace(void);
85#define __preempt_schedule_notrace() preempt_schedule_notrace()
Peter Zijlstra1a338ac2013-08-14 14:51:00 +020086#endif /* CONFIG_PREEMPT */
87
Peter Zijlstraa7878702013-08-14 14:55:40 +020088#endif /* __ASM_PREEMPT_H */