1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef LINUX_RESUME_USER_MODE_H
4 #define LINUX_RESUME_USER_MODE_H
5 
6 #include <linux/sched.h>
7 #include <linux/task_work.h>
8 #include <linux/memcontrol.h>
9 #include <linux/blk-cgroup.h>
10 
11 /**
12  * set_notify_resume - cause resume_user_mode_work() to be called
13  * @task:		task that will call resume_user_mode_work()
14  *
15  * Calling this arranges that @task will call resume_user_mode_work()
16  * before returning to user mode.  If it's already running in user mode,
17  * it will enter the kernel and call resume_user_mode_work() soon.
18  * If it's blocked, it will not be woken.
19  */
set_notify_resume(struct task_struct * task)20 static inline void set_notify_resume(struct task_struct *task)
21 {
22 	if (!test_and_set_tsk_thread_flag(task, TIF_NOTIFY_RESUME))
23 		kick_process(task);
24 }
25 
26 
27 /**
28  * resume_user_mode_work - Perform work before returning to user mode
29  * @regs:		user-mode registers of @current task
30  *
31  * This is called when %TIF_NOTIFY_RESUME has been set.  Now we are
32  * about to return to user mode, and the user state in @regs can be
33  * inspected or adjusted.  The caller in arch code has cleared
34  * %TIF_NOTIFY_RESUME before the call.  If the flag gets set again
35  * asynchronously, this will be called again before we return to
36  * user mode.
37  *
38  * Called without locks.
39  */
resume_user_mode_work(struct pt_regs * regs)40 static inline void resume_user_mode_work(struct pt_regs *regs)
41 {
42 	clear_thread_flag(TIF_NOTIFY_RESUME);
43 	/*
44 	 * This barrier pairs with task_work_add()->set_notify_resume() after
45 	 * hlist_add_head(task->task_works);
46 	 */
47 	smp_mb__after_atomic();
48 	if (unlikely(task_work_pending(current)))
49 		task_work_run();
50 
51 #ifdef CONFIG_KEYS_REQUEST_CACHE
52 	if (unlikely(current->cached_requested_key)) {
53 		key_put(current->cached_requested_key);
54 		current->cached_requested_key = NULL;
55 	}
56 #endif
57 
58 	mem_cgroup_handle_over_high();
59 	blkcg_maybe_throttle_current();
60 
61 	rseq_handle_notify_resume(NULL, regs);
62 }
63 
64 #endif /* LINUX_RESUME_USER_MODE_H */
65