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