Avi Kivity | 7c68af6 | 2009-09-19 09:40:22 +0300 | [diff] [blame] | 1 | |
| 2 | #include <linux/user-return-notifier.h> |
| 3 | #include <linux/percpu.h> |
| 4 | #include <linux/sched.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 5 | #include <linux/export.h> |
Avi Kivity | 7c68af6 | 2009-09-19 09:40:22 +0300 | [diff] [blame] | 6 | |
| 7 | static DEFINE_PER_CPU(struct hlist_head, return_notifier_list); |
| 8 | |
Avi Kivity | 7c68af6 | 2009-09-19 09:40:22 +0300 | [diff] [blame] | 9 | /* |
| 10 | * Request a notification when the current cpu returns to userspace. Must be |
| 11 | * called in atomic context. The notifier will also be called in atomic |
| 12 | * context. |
| 13 | */ |
| 14 | void user_return_notifier_register(struct user_return_notifier *urn) |
| 15 | { |
| 16 | set_tsk_thread_flag(current, TIF_USER_RETURN_NOTIFY); |
Avi Kivity | 1786bf0 | 2009-11-30 12:54:51 +0200 | [diff] [blame] | 17 | hlist_add_head(&urn->link, &__get_cpu_var(return_notifier_list)); |
Avi Kivity | 7c68af6 | 2009-09-19 09:40:22 +0300 | [diff] [blame] | 18 | } |
| 19 | EXPORT_SYMBOL_GPL(user_return_notifier_register); |
| 20 | |
| 21 | /* |
| 22 | * Removes a registered user return notifier. Must be called from atomic |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 23 | * context, and from the same cpu registration occurred in. |
Avi Kivity | 7c68af6 | 2009-09-19 09:40:22 +0300 | [diff] [blame] | 24 | */ |
| 25 | void user_return_notifier_unregister(struct user_return_notifier *urn) |
| 26 | { |
| 27 | hlist_del(&urn->link); |
Avi Kivity | 1786bf0 | 2009-11-30 12:54:51 +0200 | [diff] [blame] | 28 | if (hlist_empty(&__get_cpu_var(return_notifier_list))) |
Avi Kivity | 7c68af6 | 2009-09-19 09:40:22 +0300 | [diff] [blame] | 29 | clear_tsk_thread_flag(current, TIF_USER_RETURN_NOTIFY); |
| 30 | } |
| 31 | EXPORT_SYMBOL_GPL(user_return_notifier_unregister); |
| 32 | |
| 33 | /* Calls registered user return notifiers */ |
| 34 | void fire_user_return_notifiers(void) |
| 35 | { |
| 36 | struct user_return_notifier *urn; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 37 | struct hlist_node *tmp2; |
Avi Kivity | 7c68af6 | 2009-09-19 09:40:22 +0300 | [diff] [blame] | 38 | struct hlist_head *head; |
| 39 | |
| 40 | head = &get_cpu_var(return_notifier_list); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 41 | hlist_for_each_entry_safe(urn, tmp2, head, link) |
Avi Kivity | 7c68af6 | 2009-09-19 09:40:22 +0300 | [diff] [blame] | 42 | urn->on_user_return(urn); |
Stephen Rothwell | 3c912b6 | 2009-11-02 16:17:22 +1100 | [diff] [blame] | 43 | put_cpu_var(return_notifier_list); |
Avi Kivity | 7c68af6 | 2009-09-19 09:40:22 +0300 | [diff] [blame] | 44 | } |