Merge tag 'drm-misc-next-2019-04-10' of git://anongit.freedesktop.org/drm/drm-misc...
[linux-block.git] / include / linux / tracehook.h
CommitLineData
88ac2921
RM
1/*
2 * Tracing hooks
3 *
ae6d2ed7 4 * Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved.
88ac2921
RM
5 *
6 * This copyrighted material is made available to anyone wishing to use,
7 * modify, copy, or redistribute it subject to the terms and conditions
8 * of the GNU General Public License v.2.
9 *
10 * This file defines hook entry points called by core code where
11 * user tracing/debugging support might need to do something. These
12 * entry points are called tracehook_*(). Each hook declared below
13 * has a detailed kerneldoc comment giving the context (locking et
14 * al) from which it is called, and the meaning of its return value.
15 *
16 * Each function here typically has only one call site, so it is ok
17 * to have some nontrivial tracehook_*() inlines. In all cases, the
18 * fast path when no tracing is enabled should be very short.
19 *
20 * The purpose of this file and the tracehook_* layer is to consolidate
21 * the interface that the kernel core and arch code uses to enable any
22 * user debugging or tracing facility (such as ptrace). The interfaces
23 * here are carefully documented so that maintainers of core and arch
24 * code do not need to think about the implementation details of the
25 * tracing facilities. Likewise, maintainers of the tracing code do not
26 * need to understand all the calling core or arch code in detail, just
27 * documented circumstances of each call, such as locking conditions.
28 *
29 * If the calling core code changes so that locking is different, then
30 * it is ok to change the interface documented here. The maintainer of
31 * core code changing should notify the maintainers of the tracing code
32 * that they need to work out the change.
33 *
34 * Some tracehook_*() inlines take arguments that the current tracing
35 * implementations might not necessarily use. These function signatures
36 * are chosen to pass in all the information that is on hand in the
37 * caller and might conceivably be relevant to a tracer, so that the
38 * core code won't have to be updated when tracing adds more features.
39 * If a call site changes so that some of those parameters are no longer
40 * already on hand without extra work, then the tracehook_* interface
41 * can change so there is no make-work burden on the core code. The
42 * maintainer of core code changing should notify the maintainers of the
43 * tracing code that they need to work out the change.
44 */
45
46#ifndef _LINUX_TRACEHOOK_H
47#define _LINUX_TRACEHOOK_H 1
48
49#include <linux/sched.h>
50#include <linux/ptrace.h>
6341c393 51#include <linux/security.h>
e73f8959 52#include <linux/task_work.h>
b23afb93 53#include <linux/memcontrol.h>
d09d8df3 54#include <linux/blk-cgroup.h>
6341c393
RM
55struct linux_binprm;
56
283d7559
RM
57/*
58 * ptrace report for syscall entry and exit looks identical.
59 */
15cab952 60static inline int ptrace_report_syscall(struct pt_regs *regs)
283d7559 61{
d21142ec 62 int ptrace = current->ptrace;
283d7559
RM
63
64 if (!(ptrace & PT_PTRACED))
15cab952 65 return 0;
283d7559
RM
66
67 ptrace_notify(SIGTRAP | ((ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
68
69 /*
70 * this isn't the same as continuing with a signal, but it will do
71 * for normal use. strace only continues with a signal if the
72 * stopping signal is not SIGTRAP. -brl
73 */
74 if (current->exit_code) {
75 send_sig(current->exit_code, current, 1);
76 current->exit_code = 0;
77 }
15cab952
ON
78
79 return fatal_signal_pending(current);
283d7559
RM
80}
81
82/**
83 * tracehook_report_syscall_entry - task is about to attempt a system call
84 * @regs: user register state of current task
85 *
d19f9130
EK
86 * This will be called if %TIF_SYSCALL_TRACE or %TIF_SYSCALL_EMU have been set,
87 * when the current task has just entered the kernel for a system call.
283d7559
RM
88 * Full user register state is available here. Changing the values
89 * in @regs can affect the system call number and arguments to be tried.
90 * It is safe to block here, preventing the system call from beginning.
91 *
92 * Returns zero normally, or nonzero if the calling arch code should abort
93 * the system call. That must prevent normal entry so no system call is
94 * made. If @task ever returns to user mode after this, its register state
95 * is unspecified, but should be something harmless like an %ENOSYS error
828c365c
RM
96 * return. It should preserve enough information so that syscall_rollback()
97 * can work (see asm-generic/syscall.h).
283d7559
RM
98 *
99 * Called without locks, just after entering kernel mode.
100 */
101static inline __must_check int tracehook_report_syscall_entry(
102 struct pt_regs *regs)
103{
15cab952 104 return ptrace_report_syscall(regs);
283d7559
RM
105}
106
107/**
108 * tracehook_report_syscall_exit - task has just finished a system call
109 * @regs: user register state of current task
110 * @step: nonzero if simulating single-step or block-step
111 *
112 * This will be called if %TIF_SYSCALL_TRACE has been set, when the
113 * current task has just finished an attempted system call. Full
114 * user register state is available here. It is safe to block here,
115 * preventing signals from being processed.
116 *
117 * If @step is nonzero, this report is also in lieu of the normal
118 * trap that would follow the system call instruction because
119 * user_enable_block_step() or user_enable_single_step() was used.
120 * In this case, %TIF_SYSCALL_TRACE might not be set.
121 *
122 * Called without locks, just before checking for pending signals.
123 */
124static inline void tracehook_report_syscall_exit(struct pt_regs *regs, int step)
125{
efc463ad
EB
126 if (step)
127 user_single_step_report(regs);
128 else
129 ptrace_report_syscall(regs);
283d7559
RM
130}
131
c45aea27
RM
132/**
133 * tracehook_signal_handler - signal handler setup is complete
c45aea27
RM
134 * @stepping: nonzero if debugger single-step or block-step in use
135 *
136 * Called by the arch code after a signal handler has been set up.
137 * Register and stack state reflects the user handler about to run.
138 * Signal mask changes have already been made.
139 *
140 * Called without locks, shortly before returning to user mode
141 * (or handling more signals).
142 */
df5601f9 143static inline void tracehook_signal_handler(int stepping)
c45aea27
RM
144{
145 if (stepping)
146 ptrace_notify(SIGTRAP);
147}
148
64b1208d
RM
149/**
150 * set_notify_resume - cause tracehook_notify_resume() to be called
151 * @task: task that will call tracehook_notify_resume()
152 *
153 * Calling this arranges that @task will call tracehook_notify_resume()
154 * before returning to user mode. If it's already running in user mode,
155 * it will enter the kernel and call tracehook_notify_resume() soon.
156 * If it's blocked, it will not be woken.
157 */
158static inline void set_notify_resume(struct task_struct *task)
159{
e73f8959 160#ifdef TIF_NOTIFY_RESUME
64b1208d
RM
161 if (!test_and_set_tsk_thread_flag(task, TIF_NOTIFY_RESUME))
162 kick_process(task);
e73f8959 163#endif
64b1208d
RM
164}
165
166/**
167 * tracehook_notify_resume - report when about to return to user mode
168 * @regs: user-mode registers of @current task
169 *
170 * This is called when %TIF_NOTIFY_RESUME has been set. Now we are
171 * about to return to user mode, and the user state in @regs can be
172 * inspected or adjusted. The caller in arch code has cleared
173 * %TIF_NOTIFY_RESUME before the call. If the flag gets set again
174 * asynchronously, this will be called again before we return to
175 * user mode.
176 *
177 * Called without locks.
178 */
179static inline void tracehook_notify_resume(struct pt_regs *regs)
180{
e73f8959
ON
181 /*
182 * The caller just cleared TIF_NOTIFY_RESUME. This barrier
183 * pairs with task_work_add()->set_notify_resume() after
184 * hlist_add_head(task->task_works);
185 */
4e857c58 186 smp_mb__after_atomic();
158e1645 187 if (unlikely(current->task_works))
e73f8959 188 task_work_run();
b23afb93
TH
189
190 mem_cgroup_handle_over_high();
d09d8df3 191 blkcg_maybe_throttle_current();
64b1208d 192}
64b1208d 193
88ac2921 194#endif /* <linux/tracehook.h> */