aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/linux/preempt.h47
-rw-r--r--include/linux/sched.h7
-rw-r--r--kernel/cpu/idle.c7
-rw-r--r--kernel/sched/core.c20
4 files changed, 69 insertions, 12 deletions
diff --git a/include/linux/preempt.h b/include/linux/preempt.h
index eaac52a8fe6a..92e341853e4b 100644
--- a/include/linux/preempt.h
+++ b/include/linux/preempt.h
@@ -10,9 +10,19 @@
10#include <linux/linkage.h> 10#include <linux/linkage.h>
11#include <linux/list.h> 11#include <linux/list.h>
12 12
13/*
14 * We use the MSB mostly because its available; see <linux/preempt_mask.h> for
15 * the other bits -- can't include that header due to inclusion hell.
16 */
17#define PREEMPT_NEED_RESCHED 0x80000000
18
19/*
20 * We mask the PREEMPT_NEED_RESCHED bit so as not to confuse all current users
21 * that think a non-zero value indicates we cannot preempt.
22 */
13static __always_inline int preempt_count(void) 23static __always_inline int preempt_count(void)
14{ 24{
15 return current_thread_info()->preempt_count; 25 return current_thread_info()->preempt_count & ~PREEMPT_NEED_RESCHED;
16} 26}
17 27
18static __always_inline int *preempt_count_ptr(void) 28static __always_inline int *preempt_count_ptr(void)
@@ -20,11 +30,40 @@ static __always_inline int *preempt_count_ptr(void)
20 return &current_thread_info()->preempt_count; 30 return &current_thread_info()->preempt_count;
21} 31}
22 32
33/*
34 * We now loose PREEMPT_NEED_RESCHED and cause an extra reschedule; however the
35 * alternative is loosing a reschedule. Better schedule too often -- also this
36 * should be a very rare operation.
37 */
23static __always_inline void preempt_count_set(int pc) 38static __always_inline void preempt_count_set(int pc)
24{ 39{
25 *preempt_count_ptr() = pc; 40 *preempt_count_ptr() = pc;
26} 41}
27 42
43/*
44 * We fold the NEED_RESCHED bit into the preempt count such that
45 * preempt_enable() can decrement and test for needing to reschedule with a
46 * single instruction.
47 *
48 * We invert the actual bit, so that when the decrement hits 0 we know we both
49 * need to resched (the bit is cleared) and can resched (no preempt count).
50 */
51
52static __always_inline void set_preempt_need_resched(void)
53{
54 *preempt_count_ptr() &= ~PREEMPT_NEED_RESCHED;
55}
56
57static __always_inline void clear_preempt_need_resched(void)
58{
59 *preempt_count_ptr() |= PREEMPT_NEED_RESCHED;
60}
61
62static __always_inline bool test_preempt_need_resched(void)
63{
64 return !(*preempt_count_ptr() & PREEMPT_NEED_RESCHED);
65}
66
28#if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_PREEMPT_TRACER) 67#if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_PREEMPT_TRACER)
29 extern void add_preempt_count(int val); 68 extern void add_preempt_count(int val);
30 extern void sub_preempt_count(int val); 69 extern void sub_preempt_count(int val);
@@ -42,7 +81,7 @@ asmlinkage void preempt_schedule(void);
42 81
43#define preempt_check_resched() \ 82#define preempt_check_resched() \
44do { \ 83do { \
45 if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) \ 84 if (unlikely(!*preempt_count_ptr())) \
46 preempt_schedule(); \ 85 preempt_schedule(); \
47} while (0) 86} while (0)
48 87
@@ -52,7 +91,7 @@ void preempt_schedule_context(void);
52 91
53#define preempt_check_resched_context() \ 92#define preempt_check_resched_context() \
54do { \ 93do { \
55 if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) \ 94 if (unlikely(!*preempt_count_ptr())) \
56 preempt_schedule_context(); \ 95 preempt_schedule_context(); \
57} while (0) 96} while (0)
58#else 97#else
@@ -88,7 +127,6 @@ do { \
88#define preempt_enable() \ 127#define preempt_enable() \
89do { \ 128do { \
90 preempt_enable_no_resched(); \ 129 preempt_enable_no_resched(); \
91 barrier(); \
92 preempt_check_resched(); \ 130 preempt_check_resched(); \
93} while (0) 131} while (0)
94 132
@@ -116,7 +154,6 @@ do { \
116#define preempt_enable_notrace() \ 154#define preempt_enable_notrace() \
117do { \ 155do { \
118 preempt_enable_no_resched_notrace(); \ 156 preempt_enable_no_resched_notrace(); \
119 barrier(); \
120 preempt_check_resched_context(); \ 157 preempt_check_resched_context(); \
121} while (0) 158} while (0)
122 159
diff --git a/include/linux/sched.h b/include/linux/sched.h
index e783ec52295a..9fa151fb968e 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -22,6 +22,7 @@ struct sched_param {
22#include <linux/errno.h> 22#include <linux/errno.h>
23#include <linux/nodemask.h> 23#include <linux/nodemask.h>
24#include <linux/mm_types.h> 24#include <linux/mm_types.h>
25#include <linux/preempt.h>
25 26
26#include <asm/page.h> 27#include <asm/page.h>
27#include <asm/ptrace.h> 28#include <asm/ptrace.h>
@@ -434,7 +435,9 @@ struct task_cputime {
434 * We include PREEMPT_ACTIVE to avoid cond_resched() from working 435 * We include PREEMPT_ACTIVE to avoid cond_resched() from working
435 * before the scheduler is active -- see should_resched(). 436 * before the scheduler is active -- see should_resched().
436 */ 437 */
437#define INIT_PREEMPT_COUNT (1 + PREEMPT_ACTIVE) 438#define INIT_PREEMPT_COUNT (1 + PREEMPT_ACTIVE + PREEMPT_NEED_RESCHED)
439#define PREEMPT_ENABLED (PREEMPT_NEED_RESCHED)
440#define PREEMPT_DISABLED (1 + PREEMPT_NEED_RESCHED)
438 441
439/** 442/**
440 * struct thread_group_cputimer - thread group interval timer counts 443 * struct thread_group_cputimer - thread group interval timer counts
@@ -2408,7 +2411,7 @@ static inline int signal_pending_state(long state, struct task_struct *p)
2408 2411
2409static inline int need_resched(void) 2412static inline int need_resched(void)
2410{ 2413{
2411 return unlikely(test_thread_flag(TIF_NEED_RESCHED)); 2414 return unlikely(test_preempt_need_resched());
2412} 2415}
2413 2416
2414/* 2417/*
diff --git a/kernel/cpu/idle.c b/kernel/cpu/idle.c
index c261409500e4..988573a9a387 100644
--- a/kernel/cpu/idle.c
+++ b/kernel/cpu/idle.c
@@ -105,6 +105,13 @@ static void cpu_idle_loop(void)
105 __current_set_polling(); 105 __current_set_polling();
106 } 106 }
107 arch_cpu_idle_exit(); 107 arch_cpu_idle_exit();
108 /*
109 * We need to test and propagate the TIF_NEED_RESCHED
110 * bit here because we might not have send the
111 * reschedule IPI to idle tasks.
112 */
113 if (tif_need_resched())
114 set_preempt_need_resched();
108 } 115 }
109 tick_nohz_idle_exit(); 116 tick_nohz_idle_exit();
110 schedule_preempt_disabled(); 117 schedule_preempt_disabled();
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index fe89afac4d09..ee61f5affd20 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -525,8 +525,10 @@ void resched_task(struct task_struct *p)
525 set_tsk_need_resched(p); 525 set_tsk_need_resched(p);
526 526
527 cpu = task_cpu(p); 527 cpu = task_cpu(p);
528 if (cpu == smp_processor_id()) 528 if (cpu == smp_processor_id()) {
529 set_preempt_need_resched();
529 return; 530 return;
531 }
530 532
531 /* NEED_RESCHED must be visible before we test polling */ 533 /* NEED_RESCHED must be visible before we test polling */
532 smp_mb(); 534 smp_mb();
@@ -1391,6 +1393,14 @@ static void sched_ttwu_pending(void)
1391 1393
1392void scheduler_ipi(void) 1394void scheduler_ipi(void)
1393{ 1395{
1396 /*
1397 * Fold TIF_NEED_RESCHED into the preempt_count; anybody setting
1398 * TIF_NEED_RESCHED remotely (for the first time) will also send
1399 * this IPI.
1400 */
1401 if (tif_need_resched())
1402 set_preempt_need_resched();
1403
1394 if (llist_empty(&this_rq()->wake_list) 1404 if (llist_empty(&this_rq()->wake_list)
1395 && !tick_nohz_full_cpu(smp_processor_id()) 1405 && !tick_nohz_full_cpu(smp_processor_id())
1396 && !got_nohz_idle_kick()) 1406 && !got_nohz_idle_kick())
@@ -1714,7 +1724,7 @@ void sched_fork(struct task_struct *p)
1714#endif 1724#endif
1715#ifdef CONFIG_PREEMPT_COUNT 1725#ifdef CONFIG_PREEMPT_COUNT
1716 /* Want to start with kernel preemption disabled. */ 1726 /* Want to start with kernel preemption disabled. */
1717 task_thread_info(p)->preempt_count = 1; 1727 task_thread_info(p)->preempt_count = PREEMPT_DISABLED;
1718#endif 1728#endif
1719#ifdef CONFIG_SMP 1729#ifdef CONFIG_SMP
1720 plist_node_init(&p->pushable_tasks, MAX_PRIO); 1730 plist_node_init(&p->pushable_tasks, MAX_PRIO);
@@ -2425,6 +2435,7 @@ need_resched:
2425 put_prev_task(rq, prev); 2435 put_prev_task(rq, prev);
2426 next = pick_next_task(rq); 2436 next = pick_next_task(rq);
2427 clear_tsk_need_resched(prev); 2437 clear_tsk_need_resched(prev);
2438 clear_preempt_need_resched();
2428 rq->skip_clock_update = 0; 2439 rq->skip_clock_update = 0;
2429 2440
2430 if (likely(prev != next)) { 2441 if (likely(prev != next)) {
@@ -2536,11 +2547,10 @@ EXPORT_SYMBOL(preempt_schedule);
2536 */ 2547 */
2537asmlinkage void __sched preempt_schedule_irq(void) 2548asmlinkage void __sched preempt_schedule_irq(void)
2538{ 2549{
2539 struct thread_info *ti = current_thread_info();
2540 enum ctx_state prev_state; 2550 enum ctx_state prev_state;
2541 2551
2542 /* Catch callers which need to be fixed */ 2552 /* Catch callers which need to be fixed */
2543 BUG_ON(ti->preempt_count || !irqs_disabled()); 2553 BUG_ON(preempt_count() || !irqs_disabled());
2544 2554
2545 prev_state = exception_enter(); 2555 prev_state = exception_enter();
2546 2556
@@ -4207,7 +4217,7 @@ void init_idle(struct task_struct *idle, int cpu)
4207 raw_spin_unlock_irqrestore(&rq->lock, flags); 4217 raw_spin_unlock_irqrestore(&rq->lock, flags);
4208 4218
4209 /* Set the preempt count _outside_ the spinlocks! */ 4219 /* Set the preempt count _outside_ the spinlocks! */
4210 task_thread_info(idle)->preempt_count = 0; 4220 task_thread_info(idle)->preempt_count = PREEMPT_ENABLED;
4211 4221
4212 /* 4222 /*
4213 * The idle tasks have their own, simple scheduling class: 4223 * The idle tasks have their own, simple scheduling class: