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