user-return-notifier.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <linux/user-return-notifier.h>
  2. #include <linux/percpu.h>
  3. #include <linux/sched.h>
  4. #include <linux/export.h>
  5. static DEFINE_PER_CPU(struct hlist_head, return_notifier_list);
  6. /*
  7. * Request a notification when the current cpu returns to userspace. Must be
  8. * called in atomic context. The notifier will also be called in atomic
  9. * context.
  10. */
  11. void user_return_notifier_register(struct user_return_notifier *urn)
  12. {
  13. set_tsk_thread_flag(current, TIF_USER_RETURN_NOTIFY);
  14. hlist_add_head(&urn->link, this_cpu_ptr(&return_notifier_list));
  15. }
  16. EXPORT_SYMBOL_GPL(user_return_notifier_register);
  17. /*
  18. * Removes a registered user return notifier. Must be called from atomic
  19. * context, and from the same cpu registration occurred in.
  20. */
  21. void user_return_notifier_unregister(struct user_return_notifier *urn)
  22. {
  23. hlist_del(&urn->link);
  24. if (hlist_empty(this_cpu_ptr(&return_notifier_list)))
  25. clear_tsk_thread_flag(current, TIF_USER_RETURN_NOTIFY);
  26. }
  27. EXPORT_SYMBOL_GPL(user_return_notifier_unregister);
  28. /* Calls registered user return notifiers */
  29. void fire_user_return_notifiers(void)
  30. {
  31. struct user_return_notifier *urn;
  32. struct hlist_node *tmp2;
  33. struct hlist_head *head;
  34. head = &get_cpu_var(return_notifier_list);
  35. hlist_for_each_entry_safe(urn, tmp2, head, link)
  36. urn->on_user_return(urn);
  37. put_cpu_var(return_notifier_list);
  38. }