Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 1 | #include <linux/context_tracking.h> |
| 2 | #include <linux/rcupdate.h> |
| 3 | #include <linux/sched.h> |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 4 | #include <linux/hardirq.h> |
| 5 | |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 6 | |
Frederic Weisbecker | 95a79fd | 2013-01-07 18:12:14 +0100 | [diff] [blame^] | 7 | DEFINE_PER_CPU(struct context_tracking, context_tracking) = { |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 8 | #ifdef CONFIG_CONTEXT_TRACKING_FORCE |
| 9 | .active = true, |
| 10 | #endif |
| 11 | }; |
| 12 | |
| 13 | void user_enter(void) |
| 14 | { |
| 15 | unsigned long flags; |
| 16 | |
| 17 | /* |
| 18 | * Some contexts may involve an exception occuring in an irq, |
| 19 | * leading to that nesting: |
| 20 | * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit() |
| 21 | * This would mess up the dyntick_nesting count though. And rcu_irq_*() |
| 22 | * helpers are enough to protect RCU uses inside the exception. So |
| 23 | * just return immediately if we detect we are in an IRQ. |
| 24 | */ |
| 25 | if (in_interrupt()) |
| 26 | return; |
| 27 | |
| 28 | WARN_ON_ONCE(!current->mm); |
| 29 | |
| 30 | local_irq_save(flags); |
| 31 | if (__this_cpu_read(context_tracking.active) && |
| 32 | __this_cpu_read(context_tracking.state) != IN_USER) { |
| 33 | __this_cpu_write(context_tracking.state, IN_USER); |
| 34 | rcu_user_enter(); |
| 35 | } |
| 36 | local_irq_restore(flags); |
| 37 | } |
| 38 | |
| 39 | void user_exit(void) |
| 40 | { |
| 41 | unsigned long flags; |
| 42 | |
| 43 | /* |
| 44 | * Some contexts may involve an exception occuring in an irq, |
| 45 | * leading to that nesting: |
| 46 | * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit() |
| 47 | * This would mess up the dyntick_nesting count though. And rcu_irq_*() |
| 48 | * helpers are enough to protect RCU uses inside the exception. So |
| 49 | * just return immediately if we detect we are in an IRQ. |
| 50 | */ |
| 51 | if (in_interrupt()) |
| 52 | return; |
| 53 | |
| 54 | local_irq_save(flags); |
| 55 | if (__this_cpu_read(context_tracking.state) == IN_USER) { |
| 56 | __this_cpu_write(context_tracking.state, IN_KERNEL); |
| 57 | rcu_user_exit(); |
| 58 | } |
| 59 | local_irq_restore(flags); |
| 60 | } |
| 61 | |
| 62 | void context_tracking_task_switch(struct task_struct *prev, |
| 63 | struct task_struct *next) |
| 64 | { |
| 65 | if (__this_cpu_read(context_tracking.active)) { |
| 66 | clear_tsk_thread_flag(prev, TIF_NOHZ); |
| 67 | set_tsk_thread_flag(next, TIF_NOHZ); |
| 68 | } |
| 69 | } |