]>
Commit | Line | Data |
---|---|---|
00e5a55c BS |
1 | From: Roland McGrath <roland@redhat.com> |
2 | Date: Mon Aug 25 17:22:07 2008 -0700 | |
3 | Subject: utrace core | |
4 | References: FATE#304321 | |
5 | Patch-mainline: no | |
6 | ||
7 | This adds the utrace facility, a new modular interface in the kernel for | |
8 | implementing user thread tracing and debugging. This fits on top of the | |
9 | tracehook_* layer, so the new code is well-isolated. | |
10 | ||
11 | The new interface is in <linux/utrace.h> and the DocBook utrace book | |
12 | describes it. It allows for multiple separate tracing engines to work in | |
13 | parallel without interfering with each other. Higher-level tracing | |
14 | facilities can be implemented as loadable kernel modules using this layer. | |
15 | ||
16 | The new facility is made optional under CONFIG_UTRACE. | |
17 | When this is not enabled, no new code is added. | |
18 | It can only be enabled on machines that have all the | |
19 | prerequisites and select CONFIG_HAVE_ARCH_TRACEHOOK. | |
20 | ||
21 | In this initial version, utrace and ptrace do not play well together. | |
22 | If both utrace and ptrace are attached to the same thread, they can | |
23 | confuse each other about the stopping and resuming of that thread. | |
24 | The old ptrace code is unchanged and nothing using ptrace should be | |
25 | affected by this patch as long as utrace is not used at the same time. | |
26 | A later patch will make them cooperate properly. | |
27 | ||
28 | Signed-off-by: Roland McGrath <roland@redhat.com> | |
29 | Signed-off-by: Petr Tesarik <ptesarik@suse.cz> | |
30 | ||
31 | --- | |
32 | Documentation/DocBook/Makefile | 2 | |
33 | Documentation/DocBook/utrace.tmpl | 566 ++++++++ | |
34 | fs/proc/array.c | 3 | |
35 | include/linux/sched.h | 5 | |
36 | include/linux/tracehook.h | 71 + | |
37 | include/linux/utrace.h | 711 ++++++++++ | |
38 | init/Kconfig | 10 | |
39 | kernel/Makefile | 1 | |
40 | kernel/utrace.c | 2495 ++++++++++++++++++++++++++++++++++++++ | |
41 | 9 files changed, 3862 insertions(+), 2 deletions(-) | |
42 | ||
43 | --- a/Documentation/DocBook/Makefile | |
44 | +++ b/Documentation/DocBook/Makefile | |
45 | @@ -7,7 +7,7 @@ | |
46 | # list of DOCBOOKS. | |
47 | ||
48 | DOCBOOKS := wanbook.xml z8530book.xml mcabook.xml videobook.xml \ | |
49 | - kernel-hacking.xml kernel-locking.xml deviceiobook.xml \ | |
50 | + kernel-hacking.xml kernel-locking.xml deviceiobook.xml utrace.xml \ | |
51 | procfs-guide.xml writing_usb_driver.xml networking.xml \ | |
52 | kernel-api.xml filesystems.xml lsm.xml usb.xml kgdb.xml \ | |
53 | gadget.xml libata.xml mtdnand.xml librs.xml rapidio.xml \ | |
54 | --- /dev/null | |
55 | +++ b/Documentation/DocBook/utrace.tmpl | |
56 | @@ -0,0 +1,566 @@ | |
57 | +<?xml version="1.0" encoding="UTF-8"?> | |
58 | +<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" | |
59 | +"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []> | |
60 | + | |
61 | +<book id="utrace"> | |
62 | + <bookinfo> | |
63 | + <title>The utrace User Debugging Infrastructure</title> | |
64 | + </bookinfo> | |
65 | + | |
66 | + <toc></toc> | |
67 | + | |
68 | + <chapter id="concepts"><title>utrace concepts</title> | |
69 | + | |
70 | + <sect1 id="intro"><title>Introduction</title> | |
71 | + | |
72 | + <para> | |
73 | + <application>utrace</application> is infrastructure code for tracing | |
74 | + and controlling user threads. This is the foundation for writing | |
75 | + tracing engines, which can be loadable kernel modules. | |
76 | + </para> | |
77 | + | |
78 | + <para> | |
79 | + The basic actors in <application>utrace</application> are the thread | |
80 | + and the tracing engine. A tracing engine is some body of code that | |
81 | + calls into the <filename><linux/utrace.h></filename> | |
82 | + interfaces, represented by a <structname>struct | |
83 | + utrace_engine_ops</structname>. (Usually it's a kernel module, | |
84 | + though the legacy <function>ptrace</function> support is a tracing | |
85 | + engine that is not in a kernel module.) The interface operates on | |
86 | + individual threads (<structname>struct task_struct</structname>). | |
87 | + If an engine wants to treat several threads as a group, that is up | |
88 | + to its higher-level code. | |
89 | + </para> | |
90 | + | |
91 | + <para> | |
92 | + Tracing begins by attaching an engine to a thread, using | |
93 | + <function>utrace_attach_task</function> or | |
94 | + <function>utrace_attach_pid</function>. If successful, it returns a | |
95 | + pointer that is the handle used in all other calls. | |
96 | + </para> | |
97 | + | |
98 | + </sect1> | |
99 | + | |
100 | + <sect1 id="callbacks"><title>Events and Callbacks</title> | |
101 | + | |
102 | + <para> | |
103 | + An attached engine does nothing by default. An engine makes something | |
104 | + happen by requesting callbacks via <function>utrace_set_events</function> | |
105 | + and poking the thread with <function>utrace_control</function>. | |
106 | + The synchronization issues related to these two calls | |
107 | + are discussed further below in <xref linkend="teardown"/>. | |
108 | + </para> | |
109 | + | |
110 | + <para> | |
111 | + Events are specified using the macro | |
112 | + <constant>UTRACE_EVENT(<replaceable>type</replaceable>)</constant>. | |
113 | + Each event type is associated with a callback in <structname>struct | |
114 | + utrace_engine_ops</structname>. A tracing engine can leave unused | |
115 | + callbacks <constant>NULL</constant>. The only callbacks required | |
116 | + are those used by the event flags it sets. | |
117 | + </para> | |
118 | + | |
119 | + <para> | |
120 | + Many engines can be attached to each thread. When a thread has an | |
121 | + event, each engine gets a callback if it has set the event flag for | |
122 | + that event type. Engines are called in the order they attached. | |
123 | + </para> | |
124 | + | |
125 | + <para> | |
126 | + Event reporting callbacks have details particular to the event type, | |
127 | + but are all called in similar environments and have the same | |
128 | + constraints. Callbacks are made from safe points, where no locks | |
129 | + are held, no special resources are pinned (usually), and the | |
130 | + user-mode state of the thread is accessible. So, callback code has | |
131 | + a pretty free hand. But to be a good citizen, callback code should | |
132 | + never block for long periods. It is fine to block in | |
133 | + <function>kmalloc</function> and the like, but never wait for i/o or | |
134 | + for user mode to do something. If you need the thread to wait, use | |
135 | + <constant>UTRACE_STOP</constant> and return from the callback | |
136 | + quickly. When your i/o finishes or whatever, you can use | |
137 | + <function>utrace_control</function> to resume the thread. | |
138 | + </para> | |
139 | + | |
140 | + </sect1> | |
141 | + | |
142 | + <sect1 id="safely"><title>Stopping Safely</title> | |
143 | + | |
144 | + <sect2 id="well-behaved"><title>Writing well-behaved callbacks</title> | |
145 | + | |
146 | + <para> | |
147 | + Well-behaved callbacks are important to maintain two essential | |
148 | + properties of the interface. The first of these is that unrelated | |
149 | + tracing engines should not interfere with each other. If your engine's | |
150 | + event callback does not return quickly, then another engine won't get | |
151 | + the event notification in a timely manner. The second important | |
152 | + property is that tracing should be as noninvasive as possible to the | |
153 | + normal operation of the system overall and of the traced thread in | |
154 | + particular. That is, attached tracing engines should not perturb a | |
155 | + thread's behavior, except to the extent that changing its user-visible | |
156 | + state is explicitly what you want to do. (Obviously some perturbation | |
157 | + is unavoidable, primarily timing changes, ranging from small delays due | |
158 | + to the overhead of tracing, to arbitrary pauses in user code execution | |
159 | + when a user stops a thread with a debugger for examination.) Even when | |
160 | + you explicitly want the perturbation of making the traced thread block, | |
161 | + just blocking directly in your callback has more unwanted effects. For | |
162 | + example, the <constant>CLONE</constant> event callbacks are called when | |
163 | + the new child thread has been created but not yet started running; the | |
164 | + child can never be scheduled until the <constant>CLONE</constant> | |
165 | + tracing callbacks return. (This allows engines tracing the parent to | |
166 | + attach to the child.) If a <constant>CLONE</constant> event callback | |
167 | + blocks the parent thread, it also prevents the child thread from | |
168 | + running (even to process a <constant>SIGKILL</constant>). If what you | |
169 | + want is to make both the parent and child block, then use | |
170 | + <function>utrace_attach_task</function> on the child and then use | |
171 | + <constant>UTRACE_STOP</constant> on both threads. A more crucial | |
172 | + problem with blocking in callbacks is that it can prevent | |
173 | + <constant>SIGKILL</constant> from working. A thread that is blocking | |
174 | + due to <constant>UTRACE_STOP</constant> will still wake up and die | |
175 | + immediately when sent a <constant>SIGKILL</constant>, as all threads | |
176 | + should. Relying on the <application>utrace</application> | |
177 | + infrastructure rather than on private synchronization calls in event | |
178 | + callbacks is an important way to help keep tracing robustly | |
179 | + noninvasive. | |
180 | + </para> | |
181 | + | |
182 | + </sect2> | |
183 | + | |
184 | + <sect2 id="UTRACE_STOP"><title>Using <constant>UTRACE_STOP</constant></title> | |
185 | + | |
186 | + <para> | |
187 | + To control another thread and access its state, it must be stopped | |
188 | + with <constant>UTRACE_STOP</constant>. This means that it is | |
189 | + stopped and won't start running again while we access it. When a | |
190 | + thread is not already stopped, <function>utrace_control</function> | |
191 | + returns <constant>-EINPROGRESS</constant> and an engine must wait | |
192 | + for an event callback when the thread is ready to stop. The thread | |
193 | + may be running on another CPU or may be blocked. When it is ready | |
194 | + to be examined, it will make callbacks to engines that set the | |
195 | + <constant>UTRACE_EVENT(QUIESCE)</constant> event bit. To wake up an | |
196 | + interruptible wait, use <constant>UTRACE_INTERRUPT</constant>. | |
197 | + </para> | |
198 | + | |
199 | + <para> | |
200 | + As long as some engine has used <constant>UTRACE_STOP</constant> and | |
201 | + not called <function>utrace_control</function> to resume the thread, | |
202 | + then the thread will remain stopped. <constant>SIGKILL</constant> | |
203 | + will wake it up, but it will not run user code. When the stop is | |
204 | + cleared with <function>utrace_control</function> or a callback | |
205 | + return value, the thread starts running again. | |
206 | + (See also <xref linkend="teardown"/>.) | |
207 | + </para> | |
208 | + | |
209 | + </sect2> | |
210 | + | |
211 | + </sect1> | |
212 | + | |
213 | + <sect1 id="teardown"><title>Tear-down Races</title> | |
214 | + | |
215 | + <sect2 id="SIGKILL"><title>Primacy of <constant>SIGKILL</constant></title> | |
216 | + <para> | |
217 | + Ordinarily synchronization issues for tracing engines are kept fairly | |
218 | + straightforward by using <constant>UTRACE_STOP</constant>. You ask a | |
219 | + thread to stop, and then once it makes the | |
220 | + <function>report_quiesce</function> callback it cannot do anything else | |
221 | + that would result in another callback, until you let it with a | |
222 | + <function>utrace_control</function> call. This simple arrangement | |
223 | + avoids complex and error-prone code in each one of a tracing engine's | |
224 | + event callbacks to keep them serialized with the engine's other | |
225 | + operations done on that thread from another thread of control. | |
226 | + However, giving tracing engines complete power to keep a traced thread | |
227 | + stuck in place runs afoul of a more important kind of simplicity that | |
228 | + the kernel overall guarantees: nothing can prevent or delay | |
229 | + <constant>SIGKILL</constant> from making a thread die and release its | |
230 | + resources. To preserve this important property of | |
231 | + <constant>SIGKILL</constant>, it as a special case can break | |
232 | + <constant>UTRACE_STOP</constant> like nothing else normally can. This | |
233 | + includes both explicit <constant>SIGKILL</constant> signals and the | |
234 | + implicit <constant>SIGKILL</constant> sent to each other thread in the | |
235 | + same thread group by a thread doing an exec, or processing a fatal | |
236 | + signal, or making an <function>exit_group</function> system call. A | |
237 | + tracing engine can prevent a thread from beginning the exit or exec or | |
238 | + dying by signal (other than <constant>SIGKILL</constant>) if it is | |
239 | + attached to that thread, but once the operation begins, no tracing | |
240 | + engine can prevent or delay all other threads in the same thread group | |
241 | + dying. | |
242 | + </para> | |
243 | + </sect2> | |
244 | + | |
245 | + <sect2 id="reap"><title>Final callbacks</title> | |
246 | + <para> | |
247 | + The <function>report_reap</function> callback is always the final event | |
248 | + in the life cycle of a traced thread. Tracing engines can use this as | |
249 | + the trigger to clean up their own data structures. The | |
250 | + <function>report_death</function> callback is always the penultimate | |
251 | + event a tracing engine might see; it's seen unless the thread was | |
252 | + already in the midst of dying when the engine attached. Many tracing | |
253 | + engines will have no interest in when a parent reaps a dead process, | |
254 | + and nothing they want to do with a zombie thread once it dies; for | |
255 | + them, the <function>report_death</function> callback is the natural | |
256 | + place to clean up data structures and detach. To facilitate writing | |
257 | + such engines robustly, given the asynchrony of | |
258 | + <constant>SIGKILL</constant>, and without error-prone manual | |
259 | + implementation of synchronization schemes, the | |
260 | + <application>utrace</application> infrastructure provides some special | |
261 | + guarantees about the <function>report_death</function> and | |
262 | + <function>report_reap</function> callbacks. It still takes some care | |
263 | + to be sure your tracing engine is robust to tear-down races, but these | |
264 | + rules make it reasonably straightforward and concise to handle a lot of | |
265 | + corner cases correctly. | |
266 | + </para> | |
267 | + </sect2> | |
268 | + | |
269 | + <sect2 id="refcount"><title>Engine and task pointers</title> | |
270 | + <para> | |
271 | + The first sort of guarantee concerns the core data structures | |
272 | + themselves. <structname>struct utrace_attached_engine</structname> is | |
273 | + a reference-counted data structure. While you hold a reference, an | |
274 | + engine pointer will always stay valid so that you can safely pass it to | |
275 | + any <application>utrace</application> call. Each call to | |
276 | + <function>utrace_attach_task</function> or | |
277 | + <function>utrace_attach_pid</function> returns an engine pointer with a | |
278 | + reference belonging to the caller. You own that reference until you | |
279 | + drop it using <function>utrace_engine_put</function>. There is an | |
280 | + implicit reference on the engine while it is attached. So if you drop | |
281 | + your only reference, and then use | |
282 | + <function>utrace_attach_task</function> without | |
283 | + <constant>UTRACE_ATTACH_CREATE</constant> to look up that same engine, | |
284 | + you will get the same pointer with a new reference to replace the one | |
285 | + you dropped, just like calling <function>utrace_engine_get</function>. | |
286 | + When an engine has been detached, either explicitly with | |
287 | + <constant>UTRACE_DETACH</constant> or implicitly after | |
288 | + <function>report_reap</function>, then any references you hold are all | |
289 | + that keep the old engine pointer alive. | |
290 | + </para> | |
291 | + | |
292 | + <para> | |
293 | + There is nothing a kernel module can do to keep a <structname>struct | |
294 | + task_struct</structname> alive outside of | |
295 | + <function>rcu_read_lock</function>. When the task dies and is reaped | |
296 | + by its parent (or itself), that structure can be freed so that any | |
297 | + dangling pointers you have stored become invalid. | |
298 | + <application>utrace</application> will not prevent this, but it can | |
299 | + help you detect it safely. By definition, a task that has been reaped | |
300 | + has had all its engines detached. All | |
301 | + <application>utrace</application> calls can be safely called on a | |
302 | + detached engine if the caller holds a reference on that engine pointer, | |
303 | + even if the task pointer passed in the call is invalid. All calls | |
304 | + return <constant>-ESRCH</constant> for a detached engine, which tells | |
305 | + you that the task pointer you passed could be invalid now. Since | |
306 | + <function>utrace_control</function> and | |
307 | + <function>utrace_set_events</function> do not block, you can call those | |
308 | + inside a <function>rcu_read_lock</function> section and be sure after | |
309 | + they don't return <constant>-ESRCH</constant> that the task pointer is | |
310 | + still valid until <function>rcu_read_unlock</function>. The | |
311 | + infrastructure never holds task references of its own. Though neither | |
312 | + <function>rcu_read_lock</function> nor any other lock is held while | |
313 | + making a callback, it's always guaranteed that the <structname>struct | |
314 | + task_struct</structname> and the <structname>struct | |
315 | + utrace_attached_engine</structname> passed as arguments remain valid | |
316 | + until the callback function returns. | |
317 | + </para> | |
318 | + | |
319 | + <para> | |
320 | + The common means for safely holding task pointers that is available to | |
321 | + kernel modules is to use <structname>struct pid</structname>, which | |
322 | + permits <function>put_pid</function> from kernel modules. When using | |
323 | + that, the calls <function>utrace_attach_pid</function>, | |
324 | + <function>utrace_control_pid</function>, | |
325 | + <function>utrace_set_events_pid</function>, and | |
326 | + <function>utrace_barrier_pid</function> are available. | |
327 | + </para> | |
328 | + </sect2> | |
329 | + | |
330 | + <sect2 id="reap-after-death"> | |
331 | + <title> | |
332 | + Serialization of <constant>DEATH</constant> and <constant>REAP</constant> | |
333 | + </title> | |
334 | + <para> | |
335 | + The second guarantee is the serialization of | |
336 | + <constant>DEATH</constant> and <constant>REAP</constant> event | |
337 | + callbacks for a given thread. The actual reaping by the parent | |
338 | + (<function>release_task</function> call) can occur simultaneously | |
339 | + while the thread is still doing the final steps of dying, including | |
340 | + the <function>report_death</function> callback. If a tracing engine | |
341 | + has requested both <constant>DEATH</constant> and | |
342 | + <constant>REAP</constant> event reports, it's guaranteed that the | |
343 | + <function>report_reap</function> callback will not be made until | |
344 | + after the <function>report_death</function> callback has returned. | |
345 | + If the <function>report_death</function> callback itself detaches | |
346 | + from the thread, then the <function>report_reap</function> callback | |
347 | + will never be made. Thus it is safe for a | |
348 | + <function>report_death</function> callback to clean up data | |
349 | + structures and detach. | |
350 | + </para> | |
351 | + </sect2> | |
352 | + | |
353 | + <sect2 id="interlock"><title>Interlock with final callbacks</title> | |
354 | + <para> | |
355 | + The final sort of guarantee is that a tracing engine will know for sure | |
356 | + whether or not the <function>report_death</function> and/or | |
357 | + <function>report_reap</function> callbacks will be made for a certain | |
358 | + thread. These tear-down races are disambiguated by the error return | |
359 | + values of <function>utrace_set_events</function> and | |
360 | + <function>utrace_control</function>. Normally | |
361 | + <function>utrace_control</function> called with | |
362 | + <constant>UTRACE_DETACH</constant> returns zero, and this means that no | |
363 | + more callbacks will be made. If the thread is in the midst of dying, | |
364 | + it returns <constant>-EALREADY</constant> to indicate that the | |
365 | + <constant>report_death</constant> callback may already be in progress; | |
366 | + when you get this error, you know that any cleanup your | |
367 | + <function>report_death</function> callback does is about to happen or | |
368 | + has just happened--note that if the <function>report_death</function> | |
369 | + callback does not detach, the engine remains attached until the thread | |
370 | + gets reaped. If the thread is in the midst of being reaped, | |
371 | + <function>utrace_control</function> returns <constant>-ESRCH</constant> | |
372 | + to indicate that the <function>report_reap</function> callback may | |
373 | + already be in progress; this means the engine is implicitly detached | |
374 | + when the callback completes. This makes it possible for a tracing | |
375 | + engine that has decided asynchronously to detach from a thread to | |
376 | + safely clean up its data structures, knowing that no | |
377 | + <function>report_death</function> or <function>report_reap</function> | |
378 | + callback will try to do the same. <constant>utrace_detach</constant> | |
379 | + returns <constant>-ESRCH</constant> when the <structname>struct | |
380 | + utrace_attached_engine</structname> has already been detached, but is | |
381 | + still a valid pointer because of its reference count. A tracing engine | |
382 | + can use this to safely synchronize its own independent multiple threads | |
383 | + of control with each other and with its event callbacks that detach. | |
384 | + </para> | |
385 | + | |
386 | + <para> | |
387 | + In the same vein, <function>utrace_set_events</function> normally | |
388 | + returns zero; if the target thread was stopped before the call, then | |
389 | + after a successful call, no event callbacks not requested in the new | |
390 | + flags will be made. It fails with <constant>-EALREADY</constant> if | |
391 | + you try to clear <constant>UTRACE_EVENT(DEATH)</constant> when the | |
392 | + <function>report_death</function> callback may already have begun, if | |
393 | + you try to clear <constant>UTRACE_EVENT(REAP)</constant> when the | |
394 | + <function>report_reap</function> callback may already have begun, or if | |
395 | + you try to newly set <constant>UTRACE_EVENT(DEATH)</constant> or | |
396 | + <constant>UTRACE_EVENT(QUIESCE)</constant> when the target is already | |
397 | + dead or dying. Like <function>utrace_control</function>, it returns | |
398 | + <constant>-ESRCH</constant> when the thread has already been detached | |
399 | + (including forcible detach on reaping). This lets the tracing engine | |
400 | + know for sure which event callbacks it will or won't see after | |
401 | + <function>utrace_set_events</function> has returned. By checking for | |
402 | + errors, it can know whether to clean up its data structures immediately | |
403 | + or to let its callbacks do the work. | |
404 | + </para> | |
405 | + </sect2> | |
406 | + | |
407 | + <sect2 id="barrier"><title>Using <function>utrace_barrier</function></title> | |
408 | + <para> | |
409 | + When a thread is safely stopped, calling | |
410 | + <function>utrace_control</function> with <constant>UTRACE_DETACH</constant> | |
411 | + or calling <function>utrace_set_events</function> to disable some events | |
412 | + ensures synchronously that your engine won't get any more of the callbacks | |
413 | + that have been disabled (none at all when detaching). But these can also | |
414 | + be used while the thread is not stopped, when it might be simultaneously | |
415 | + making a callback to your engine. For this situation, these calls return | |
416 | + <constant>-EINPROGRESS</constant> when it's possible a callback is in | |
417 | + progress. If you are not prepared to have your old callbacks still run, | |
418 | + then you can synchronize to be sure all the old callbacks are finished, | |
419 | + using <function>utrace_barrier</function>. This is necessary if the | |
420 | + kernel module containing your callback code is going to be unloaded. | |
421 | + </para> | |
422 | + <para> | |
423 | + After using <constant>UTRACE_DETACH</constant> once, further calls to | |
424 | + <function>utrace_control</function> with the same engine pointer will | |
425 | + return <constant>-ESRCH</constant>. In contrast, after getting | |
426 | + <constant>-EINPROGRESS</constant> from | |
427 | + <function>utrace_set_events</function>, you can call | |
428 | + <function>utrace_set_events</function> again later and if it returns zero | |
429 | + then know the old callbacks have finished. | |
430 | + </para> | |
431 | + <para> | |
432 | + Unlike all other calls, <function>utrace_barrier</function> (and | |
433 | + <function>utrace_barrier_pid</function>) will accept any engine pointer you | |
434 | + hold a reference on, even if <constant>UTRACE_DETACH</constant> has already | |
435 | + been used. After any <function>utrace_control</function> or | |
436 | + <function>utrace_set_events</function> call (these do not block), you can | |
437 | + call <function>utrace_barrier</function> to block until callbacks have | |
438 | + finished. This returns <constant>-ESRCH</constant> only if the engine is | |
439 | + completely detached (finished all callbacks). Otherwise returns it waits | |
440 | + until the thread is definitely not in the midst of a callback to this | |
441 | + engine and then returns zero, but can return | |
442 | + <constant>-ERESTARTSYS</constant> if its wait is interrupted. | |
443 | + </para> | |
444 | + </sect2> | |
445 | + | |
446 | +</sect1> | |
447 | + | |
448 | +</chapter> | |
449 | + | |
450 | +<chapter id="core"><title>utrace core API</title> | |
451 | + | |
452 | +<para> | |
453 | + The utrace API is declared in <filename><linux/utrace.h></filename>. | |
454 | +</para> | |
455 | + | |
456 | +!Iinclude/linux/utrace.h | |
457 | +!Ekernel/utrace.c | |
458 | + | |
459 | +</chapter> | |
460 | + | |
461 | +<chapter id="machine"><title>Machine State</title> | |
462 | + | |
463 | +<para> | |
464 | + The <function>task_current_syscall</function> function can be used on any | |
465 | + valid <structname>struct task_struct</structname> at any time, and does | |
466 | + not even require that <function>utrace_attach_task</function> was used at all. | |
467 | +</para> | |
468 | + | |
469 | +<para> | |
470 | + The other ways to access the registers and other machine-dependent state of | |
471 | + a task can only be used on a task that is at a known safe point. The safe | |
472 | + points are all the places where <function>utrace_set_events</function> can | |
473 | + request callbacks (except for the <constant>DEATH</constant> and | |
474 | + <constant>REAP</constant> events). So at any event callback, it is safe to | |
475 | + examine <varname>current</varname>. | |
476 | +</para> | |
477 | + | |
478 | +<para> | |
479 | + One task can examine another only after a callback in the target task that | |
480 | + returns <constant>UTRACE_STOP</constant> so that task will not return to user | |
481 | + mode after the safe point. This guarantees that the task will not resume | |
482 | + until the same engine uses <function>utrace_control</function>, unless the | |
483 | + task dies suddenly. To examine safely, one must use a pair of calls to | |
484 | + <function>utrace_prepare_examine</function> and | |
485 | + <function>utrace_finish_examine</function> surrounding the calls to | |
486 | + <structname>struct user_regset</structname> functions or direct examination | |
487 | + of task data structures. <function>utrace_prepare_examine</function> returns | |
488 | + an error if the task is not properly stopped and not dead. After a | |
489 | + successful examination, the paired <function>utrace_finish_examine</function> | |
490 | + call returns an error if the task ever woke up during the examination. If | |
491 | + so, any data gathered may be scrambled and should be discarded. This means | |
492 | + there was a spurious wake-up (which should not happen), or a sudden death. | |
493 | +</para> | |
494 | + | |
495 | +<sect1 id="regset"><title><structname>struct user_regset</structname></title> | |
496 | + | |
497 | +<para> | |
498 | + The <structname>struct user_regset</structname> API | |
499 | + is declared in <filename><linux/regset.h></filename>. | |
500 | +</para> | |
501 | + | |
502 | +!Finclude/linux/regset.h | |
503 | + | |
504 | +</sect1> | |
505 | + | |
506 | +<sect1 id="task_current_syscall"> | |
507 | + <title><filename>System Call Information</filename></title> | |
508 | + | |
509 | +<para> | |
510 | + This function is declared in <filename><linux/ptrace.h></filename>. | |
511 | +</para> | |
512 | + | |
513 | +!Elib/syscall.c | |
514 | + | |
515 | +</sect1> | |
516 | + | |
517 | +<sect1 id="syscall"><title><filename>System Call Tracing</filename></title> | |
518 | + | |
519 | +<para> | |
520 | + The arch API for system call information is declared in | |
521 | + <filename><asm/syscall.h></filename>. | |
522 | + Each of these calls can be used only at system call entry tracing, | |
523 | + or can be used only at system call exit and the subsequent safe points | |
524 | + before returning to user mode. | |
525 | + At system call entry tracing means either during a | |
526 | + <structfield>report_syscall_entry</structfield> callback, | |
527 | + or any time after that callback has returned <constant>UTRACE_STOP</constant>. | |
528 | +</para> | |
529 | + | |
530 | +!Finclude/asm-generic/syscall.h | |
531 | + | |
532 | +</sect1> | |
533 | + | |
534 | +</chapter> | |
535 | + | |
536 | +<chapter id="internals"><title>Kernel Internals</title> | |
537 | + | |
538 | +<para> | |
539 | + This chapter covers the interface to the tracing infrastructure | |
540 | + from the core of the kernel and the architecture-specific code. | |
541 | + This is for maintainers of the kernel and arch code, and not relevant | |
542 | + to using the tracing facilities described in preceding chapters. | |
543 | +</para> | |
544 | + | |
545 | +<sect1 id="tracehook"><title>Core Calls In</title> | |
546 | + | |
547 | +<para> | |
548 | + These calls are declared in <filename><linux/tracehook.h></filename>. | |
549 | + The core kernel calls these functions at various important places. | |
550 | +</para> | |
551 | + | |
552 | +!Finclude/linux/tracehook.h | |
553 | + | |
554 | +</sect1> | |
555 | + | |
556 | +<sect1 id="arch"><title>Architecture Calls Out</title> | |
557 | + | |
558 | +<para> | |
559 | + An arch that has done all these things sets | |
560 | + <constant>CONFIG_HAVE_ARCH_TRACEHOOK</constant>. | |
561 | + This is required to enable the <application>utrace</application> code. | |
562 | +</para> | |
563 | + | |
564 | +<sect2 id="arch-ptrace"><title><filename><asm/ptrace.h></filename></title> | |
565 | + | |
566 | +<para> | |
567 | + An arch defines these in <filename><asm/ptrace.h></filename> | |
568 | + if it supports hardware single-step or block-step features. | |
569 | +</para> | |
570 | + | |
571 | +!Finclude/linux/ptrace.h arch_has_single_step arch_has_block_step | |
572 | +!Finclude/linux/ptrace.h user_enable_single_step user_enable_block_step | |
573 | +!Finclude/linux/ptrace.h user_disable_single_step | |
574 | + | |
575 | +</sect2> | |
576 | + | |
577 | +<sect2 id="arch-syscall"> | |
578 | + <title><filename><asm/syscall.h></filename></title> | |
579 | + | |
580 | + <para> | |
581 | + An arch provides <filename><asm/syscall.h></filename> that | |
582 | + defines these as inlines, or declares them as exported functions. | |
583 | + These interfaces are described in <xref linkend="syscall"/>. | |
584 | + </para> | |
585 | + | |
586 | +</sect2> | |
587 | + | |
588 | +<sect2 id="arch-tracehook"> | |
589 | + <title><filename><linux/tracehook.h></filename></title> | |
590 | + | |
591 | + <para> | |
592 | + An arch must define <constant>TIF_NOTIFY_RESUME</constant> | |
593 | + and <constant>TIF_SYSCALL_TRACE</constant> | |
594 | + in its <filename><asm/thread_info.h></filename>. | |
595 | + The arch code must call the following functions, all declared | |
596 | + in <filename><linux/tracehook.h></filename> and | |
597 | + described in <xref linkend="tracehook"/>: | |
598 | + | |
599 | + <itemizedlist> | |
600 | + <listitem> | |
601 | + <para><function>tracehook_notify_resume</function></para> | |
602 | + </listitem> | |
603 | + <listitem> | |
604 | + <para><function>tracehook_report_syscall_entry</function></para> | |
605 | + </listitem> | |
606 | + <listitem> | |
607 | + <para><function>tracehook_report_syscall_exit</function></para> | |
608 | + </listitem> | |
609 | + <listitem> | |
610 | + <para><function>tracehook_signal_handler</function></para> | |
611 | + </listitem> | |
612 | + </itemizedlist> | |
613 | + | |
614 | + </para> | |
615 | + | |
616 | +</sect2> | |
617 | + | |
618 | +</sect1> | |
619 | + | |
620 | +</chapter> | |
621 | + | |
622 | +</book> | |
623 | --- a/fs/proc/array.c | |
624 | +++ b/fs/proc/array.c | |
625 | @@ -82,6 +82,7 @@ | |
626 | #include <linux/pid_namespace.h> | |
627 | #include <linux/ptrace.h> | |
628 | #include <linux/tracehook.h> | |
629 | +#include <linux/utrace.h> | |
630 | ||
631 | #include <asm/pgtable.h> | |
632 | #include <asm/processor.h> | |
633 | @@ -191,6 +192,8 @@ static inline void task_state(struct seq | |
634 | p->uid, p->euid, p->suid, p->fsuid, | |
635 | p->gid, p->egid, p->sgid, p->fsgid); | |
636 | ||
637 | + task_utrace_proc_status(m, p); | |
638 | + | |
639 | task_lock(p); | |
640 | if (p->files) | |
641 | fdt = files_fdtable(p->files); | |
642 | --- a/include/linux/sched.h | |
643 | +++ b/include/linux/sched.h | |
644 | @@ -1202,6 +1202,11 @@ struct task_struct { | |
645 | #endif | |
646 | seccomp_t seccomp; | |
647 | ||
648 | +#ifdef CONFIG_UTRACE | |
649 | + struct utrace *utrace; | |
650 | + unsigned long utrace_flags; | |
651 | +#endif | |
652 | + | |
653 | /* Thread group tracking */ | |
654 | u32 parent_exec_id; | |
655 | u32 self_exec_id; | |
656 | --- a/include/linux/tracehook.h | |
657 | +++ b/include/linux/tracehook.h | |
658 | @@ -49,6 +49,7 @@ | |
659 | #include <linux/sched.h> | |
660 | #include <linux/ptrace.h> | |
661 | #include <linux/security.h> | |
662 | +#include <linux/utrace.h> | |
663 | struct linux_binprm; | |
664 | ||
665 | /** | |
666 | @@ -63,6 +64,8 @@ struct linux_binprm; | |
667 | */ | |
668 | static inline int tracehook_expect_breakpoints(struct task_struct *task) | |
669 | { | |
670 | + if (unlikely(task_utrace_flags(task) & UTRACE_EVENT(SIGNAL_CORE))) | |
671 | + return 1; | |
672 | return (task_ptrace(task) & PT_PTRACED) != 0; | |
673 | } | |
674 | ||
675 | @@ -111,6 +114,9 @@ static inline void ptrace_report_syscall | |
676 | static inline __must_check int tracehook_report_syscall_entry( | |
677 | struct pt_regs *regs) | |
678 | { | |
679 | + if ((task_utrace_flags(current) & UTRACE_EVENT(SYSCALL_ENTRY)) && | |
680 | + utrace_report_syscall_entry(regs)) | |
681 | + return 1; | |
682 | ptrace_report_syscall(regs); | |
683 | return 0; | |
684 | } | |
685 | @@ -134,6 +140,8 @@ static inline __must_check int tracehook | |
686 | */ | |
687 | static inline void tracehook_report_syscall_exit(struct pt_regs *regs, int step) | |
688 | { | |
689 | + if (task_utrace_flags(current) & UTRACE_EVENT(SYSCALL_EXIT)) | |
690 | + utrace_report_syscall_exit(regs); | |
691 | ptrace_report_syscall(regs); | |
692 | } | |
693 | ||
694 | @@ -155,6 +163,8 @@ static inline int tracehook_unsafe_exec( | |
695 | else | |
696 | unsafe |= LSM_UNSAFE_PTRACE; | |
697 | } | |
698 | + if (unlikely(task_utrace_flags(task))) | |
699 | + unsafe |= utrace_unsafe_exec(task); | |
700 | return unsafe; | |
701 | } | |
702 | ||
703 | @@ -173,6 +183,8 @@ static inline struct task_struct *traceh | |
704 | { | |
705 | if (task_ptrace(tsk) & PT_PTRACED) | |
706 | return rcu_dereference(tsk->parent); | |
707 | + if (unlikely(task_utrace_flags(tsk))) | |
708 | + return utrace_tracer_task(tsk); | |
709 | return NULL; | |
710 | } | |
711 | ||
712 | @@ -194,6 +206,8 @@ static inline void tracehook_report_exec | |
713 | struct linux_binprm *bprm, | |
714 | struct pt_regs *regs) | |
715 | { | |
716 | + if (unlikely(task_utrace_flags(current) & UTRACE_EVENT(EXEC))) | |
717 | + utrace_report_exec(fmt, bprm, regs); | |
718 | if (!ptrace_event(PT_TRACE_EXEC, PTRACE_EVENT_EXEC, 0) && | |
719 | unlikely(task_ptrace(current) & PT_PTRACED)) | |
720 | send_sig(SIGTRAP, current, 0); | |
721 | @@ -211,6 +225,8 @@ static inline void tracehook_report_exec | |
722 | */ | |
723 | static inline void tracehook_report_exit(long *exit_code) | |
724 | { | |
725 | + if (unlikely(task_utrace_flags(current) & UTRACE_EVENT(EXIT))) | |
726 | + utrace_report_exit(exit_code); | |
727 | ptrace_event(PT_TRACE_EXIT, PTRACE_EVENT_EXIT, *exit_code); | |
728 | } | |
729 | ||
730 | @@ -254,6 +270,7 @@ static inline int tracehook_prepare_clon | |
731 | static inline void tracehook_finish_clone(struct task_struct *child, | |
732 | unsigned long clone_flags, int trace) | |
733 | { | |
734 | + utrace_init_task(child); | |
735 | ptrace_init_task(child, (clone_flags & CLONE_PTRACE) || trace); | |
736 | } | |
737 | ||
738 | @@ -280,6 +297,8 @@ static inline void tracehook_report_clon | |
739 | unsigned long clone_flags, | |
740 | pid_t pid, struct task_struct *child) | |
741 | { | |
742 | + if (unlikely(task_utrace_flags(current) & UTRACE_EVENT(CLONE))) | |
743 | + utrace_report_clone(clone_flags, child); | |
744 | if (unlikely(trace) || unlikely(clone_flags & CLONE_PTRACE)) { | |
745 | /* | |
746 | * The child starts up with an immediate SIGSTOP. | |
747 | @@ -345,6 +364,11 @@ static inline void tracehook_report_vfor | |
748 | */ | |
749 | static inline void tracehook_prepare_release_task(struct task_struct *task) | |
750 | { | |
751 | +#ifdef CONFIG_UTRACE | |
752 | + smp_mb(); | |
753 | + if (task_utrace_struct(task) != NULL) | |
754 | + utrace_release_task(task); | |
755 | +#endif | |
756 | } | |
757 | ||
758 | /** | |
759 | @@ -358,7 +382,25 @@ static inline void tracehook_prepare_rel | |
760 | */ | |
761 | static inline void tracehook_finish_release_task(struct task_struct *task) | |
762 | { | |
763 | +#ifdef CONFIG_UTRACE | |
764 | + int bad = 0; | |
765 | +#endif | |
766 | ptrace_release_task(task); | |
767 | +#ifdef CONFIG_UTRACE | |
768 | + BUG_ON(task->exit_state != EXIT_DEAD); | |
769 | + if (unlikely(task_utrace_struct(task) != NULL)) { | |
770 | + /* | |
771 | + * In a race condition, utrace_attach() will temporarily set | |
772 | + * it, but then check @task->exit_state and clear it. It does | |
773 | + * all this under task_lock(), so we take the lock to check | |
774 | + * that there is really a bug and not just that known race. | |
775 | + */ | |
776 | + task_lock(task); | |
777 | + bad = unlikely(task_utrace_struct(task) != NULL); | |
778 | + task_unlock(task); | |
779 | + } | |
780 | + BUG_ON(bad); | |
781 | +#endif | |
782 | } | |
783 | ||
784 | /** | |
785 | @@ -380,6 +422,8 @@ static inline void tracehook_signal_hand | |
786 | const struct k_sigaction *ka, | |
787 | struct pt_regs *regs, int stepping) | |
788 | { | |
789 | + if (task_utrace_flags(current)) | |
790 | + utrace_signal_handler(current, stepping); | |
791 | if (stepping) | |
792 | ptrace_notify(SIGTRAP); | |
793 | } | |
794 | @@ -400,6 +444,8 @@ static inline int tracehook_consider_ign | |
795 | int sig, | |
796 | void __user *handler) | |
797 | { | |
798 | + if (unlikely(task_utrace_flags(task) & UTRACE_EVENT(SIGNAL_IGN))) | |
799 | + return 1; | |
800 | return (task_ptrace(task) & PT_PTRACED) != 0; | |
801 | } | |
802 | ||
803 | @@ -421,6 +467,9 @@ static inline int tracehook_consider_fat | |
804 | int sig, | |
805 | void __user *handler) | |
806 | { | |
807 | + if (unlikely(task_utrace_flags(task) & (UTRACE_EVENT(SIGNAL_TERM) | | |
808 | + UTRACE_EVENT(SIGNAL_CORE)))) | |
809 | + return 1; | |
810 | return (task_ptrace(task) & PT_PTRACED) != 0; | |
811 | } | |
812 | ||
813 | @@ -435,6 +484,8 @@ static inline int tracehook_consider_fat | |
814 | */ | |
815 | static inline int tracehook_force_sigpending(void) | |
816 | { | |
817 | + if (unlikely(task_utrace_flags(current))) | |
818 | + return utrace_interrupt_pending(); | |
819 | return 0; | |
820 | } | |
821 | ||
822 | @@ -464,6 +515,8 @@ static inline int tracehook_get_signal(s | |
823 | siginfo_t *info, | |
824 | struct k_sigaction *return_ka) | |
825 | { | |
826 | + if (unlikely(task_utrace_flags(task))) | |
827 | + return utrace_get_signal(task, regs, info, return_ka); | |
828 | return 0; | |
829 | } | |
830 | ||
831 | @@ -484,6 +537,8 @@ static inline int tracehook_get_signal(s | |
832 | */ | |
833 | static inline int tracehook_notify_jctl(int notify, int why) | |
834 | { | |
835 | + if (task_utrace_flags(current) & UTRACE_EVENT(JCTL)) | |
836 | + utrace_report_jctl(notify, why); | |
837 | return notify || (current->ptrace & PT_PTRACED); | |
838 | } | |
839 | ||
840 | @@ -507,6 +562,10 @@ static inline int tracehook_notify_jctl( | |
841 | static inline int tracehook_notify_death(struct task_struct *task, | |
842 | void **death_cookie, int group_dead) | |
843 | { | |
844 | +#ifdef CONFIG_UTRACE | |
845 | + *death_cookie = task_utrace_struct(task); | |
846 | +#endif | |
847 | + | |
848 | if (task->exit_signal == -1) | |
849 | return task->ptrace ? SIGCHLD : DEATH_REAP; | |
850 | ||
851 | @@ -543,6 +602,12 @@ static inline void tracehook_report_deat | |
852 | int signal, void *death_cookie, | |
853 | int group_dead) | |
854 | { | |
855 | +#ifdef CONFIG_UTRACE | |
856 | + smp_mb(); | |
857 | + if (task_utrace_flags(task) & (UTRACE_EVENT(DEATH) | | |
858 | + UTRACE_EVENT(QUIESCE))) | |
859 | + utrace_report_death(task, death_cookie, group_dead, signal); | |
860 | +#endif | |
861 | } | |
862 | ||
863 | #ifdef TIF_NOTIFY_RESUME | |
864 | @@ -572,10 +637,14 @@ static inline void set_notify_resume(str | |
865 | * asynchronously, this will be called again before we return to | |
866 | * user mode. | |
867 | * | |
868 | - * Called without locks. | |
869 | + * Called without locks. However, on some machines this may be | |
870 | + * called with interrupts disabled. | |
871 | */ | |
872 | static inline void tracehook_notify_resume(struct pt_regs *regs) | |
873 | { | |
874 | + struct task_struct *task = current; | |
875 | + if (task_utrace_flags(task)) | |
876 | + utrace_resume(task, regs); | |
877 | } | |
878 | #endif /* TIF_NOTIFY_RESUME */ | |
879 | ||
880 | --- /dev/null | |
881 | +++ b/include/linux/utrace.h | |
882 | @@ -0,0 +1,711 @@ | |
883 | +/* | |
884 | + * utrace infrastructure interface for debugging user processes | |
885 | + * | |
886 | + * Copyright (C) 2006, 2007, 2008 Red Hat, Inc. All rights reserved. | |
887 | + * | |
888 | + * This copyrighted material is made available to anyone wishing to use, | |
889 | + * modify, copy, or redistribute it subject to the terms and conditions | |
890 | + * of the GNU General Public License v.2. | |
891 | + * | |
892 | + * Red Hat Author: Roland McGrath. | |
893 | + * | |
894 | + * This interface allows for notification of interesting events in a | |
895 | + * thread. It also mediates access to thread state such as registers. | |
896 | + * Multiple unrelated users can be associated with a single thread. | |
897 | + * We call each of these a tracing engine. | |
898 | + * | |
899 | + * A tracing engine starts by calling utrace_attach_task() or | |
900 | + * utrace_attach_pid() on the chosen thread, passing in a set of hooks | |
901 | + * (&struct utrace_engine_ops), and some associated data. This produces a | |
902 | + * &struct utrace_attached_engine, which is the handle used for all other | |
903 | + * operations. An attached engine has its ops vector, its data, and an | |
904 | + * event mask controlled by utrace_set_events(). | |
905 | + * | |
906 | + * For each event bit that is set, that engine will get the | |
907 | + * appropriate ops->report_*() callback when the event occurs. The | |
908 | + * &struct utrace_engine_ops need not provide callbacks for an event | |
909 | + * unless the engine sets one of the associated event bits. | |
910 | + */ | |
911 | + | |
912 | +#ifndef _LINUX_UTRACE_H | |
913 | +#define _LINUX_UTRACE_H 1 | |
914 | + | |
915 | +#include <linux/list.h> | |
916 | +#include <linux/kref.h> | |
917 | +#include <linux/signal.h> | |
918 | +#include <linux/sched.h> | |
919 | + | |
920 | +struct linux_binprm; | |
921 | +struct pt_regs; | |
922 | +struct utrace; | |
923 | +struct utrace_engine_ops; | |
924 | +struct utrace_attached_engine; | |
925 | +struct utrace_examiner; | |
926 | +struct user_regset; | |
927 | +struct user_regset_view; | |
928 | +enum utrace_resume_action; | |
929 | + | |
930 | +/* | |
931 | + * Event bits passed to utrace_set_events(). | |
932 | + * These appear in &struct task_struct.@utrace_flags | |
933 | + * and &struct utrace_attached_engine.@flags. | |
934 | + */ | |
935 | +enum utrace_events { | |
936 | + _UTRACE_EVENT_QUIESCE, /* Thread is available for examination. */ | |
937 | + _UTRACE_EVENT_REAP, /* Zombie reaped, no more tracing possible. */ | |
938 | + _UTRACE_EVENT_CLONE, /* Successful clone/fork/vfork just done. */ | |
939 | + _UTRACE_EVENT_EXEC, /* Successful execve just completed. */ | |
940 | + _UTRACE_EVENT_EXIT, /* Thread exit in progress. */ | |
941 | + _UTRACE_EVENT_DEATH, /* Thread has died. */ | |
942 | + _UTRACE_EVENT_SYSCALL_ENTRY, /* User entered kernel for system call. */ | |
943 | + _UTRACE_EVENT_SYSCALL_EXIT, /* Returning to user after system call. */ | |
944 | + _UTRACE_EVENT_SIGNAL, /* Signal delivery will run a user handler. */ | |
945 | + _UTRACE_EVENT_SIGNAL_IGN, /* No-op signal to be delivered. */ | |
946 | + _UTRACE_EVENT_SIGNAL_STOP, /* Signal delivery will suspend. */ | |
947 | + _UTRACE_EVENT_SIGNAL_TERM, /* Signal delivery will terminate. */ | |
948 | + _UTRACE_EVENT_SIGNAL_CORE, /* Signal delivery will dump core. */ | |
949 | + _UTRACE_EVENT_JCTL, /* Job control stop or continue completed. */ | |
950 | + _UTRACE_NEVENTS | |
951 | +}; | |
952 | +#define UTRACE_EVENT(type) (1UL << _UTRACE_EVENT_##type) | |
953 | + | |
954 | +/* | |
955 | + * All the kinds of signal events. | |
956 | + * These all use the @report_signal() callback. | |
957 | + */ | |
958 | +#define UTRACE_EVENT_SIGNAL_ALL (UTRACE_EVENT(SIGNAL) \ | |
959 | + | UTRACE_EVENT(SIGNAL_IGN) \ | |
960 | + | UTRACE_EVENT(SIGNAL_STOP) \ | |
961 | + | UTRACE_EVENT(SIGNAL_TERM) \ | |
962 | + | UTRACE_EVENT(SIGNAL_CORE)) | |
963 | +/* | |
964 | + * Both kinds of syscall events; these call the @report_syscall_entry() | |
965 | + * and @report_syscall_exit() callbacks, respectively. | |
966 | + */ | |
967 | +#define UTRACE_EVENT_SYSCALL \ | |
968 | + (UTRACE_EVENT(SYSCALL_ENTRY) | UTRACE_EVENT(SYSCALL_EXIT)) | |
969 | + | |
970 | +/* | |
971 | + * Hooks in <linux/tracehook.h> call these entry points to the | |
972 | + * utrace dispatch. They are weak references here only so | |
973 | + * tracehook.h doesn't need to #ifndef CONFIG_UTRACE them to | |
974 | + * avoid external references in case of unoptimized compilation. | |
975 | + */ | |
976 | +void utrace_release_task(struct task_struct *) | |
977 | + __attribute__((weak)); | |
978 | +bool utrace_interrupt_pending(void) | |
979 | + __attribute__((weak)); | |
980 | +void utrace_resume(struct task_struct *, struct pt_regs *) | |
981 | + __attribute__((weak)); | |
982 | +int utrace_get_signal(struct task_struct *, struct pt_regs *, | |
983 | + siginfo_t *, struct k_sigaction *) | |
984 | + __attribute__((weak)); | |
985 | +void utrace_report_clone(unsigned long, struct task_struct *) | |
986 | + __attribute__((weak)); | |
987 | +void utrace_report_exit(long *exit_code) | |
988 | + __attribute__((weak)); | |
989 | +void utrace_report_death(struct task_struct *, struct utrace *, bool, int) | |
990 | + __attribute__((weak)); | |
991 | +void utrace_report_jctl(int notify, int type) | |
992 | + __attribute__((weak)); | |
993 | +void utrace_report_exec(struct linux_binfmt *, struct linux_binprm *, | |
994 | + struct pt_regs *regs) | |
995 | + __attribute__((weak)); | |
996 | +bool utrace_report_syscall_entry(struct pt_regs *) | |
997 | + __attribute__((weak)); | |
998 | +void utrace_report_syscall_exit(struct pt_regs *) | |
999 | + __attribute__((weak)); | |
1000 | +struct task_struct *utrace_tracer_task(struct task_struct *) | |
1001 | + __attribute__((weak)); | |
1002 | +int utrace_unsafe_exec(struct task_struct *) | |
1003 | + __attribute__((weak)); | |
1004 | +void utrace_signal_handler(struct task_struct *, int) | |
1005 | + __attribute__((weak)); | |
1006 | + | |
1007 | +#ifndef CONFIG_UTRACE | |
1008 | + | |
1009 | +/* | |
1010 | + * <linux/tracehook.h> uses these accessors to avoid #ifdef CONFIG_UTRACE. | |
1011 | + */ | |
1012 | +static inline unsigned long task_utrace_flags(struct task_struct *task) | |
1013 | +{ | |
1014 | + return 0; | |
1015 | +} | |
1016 | +static inline struct utrace *task_utrace_struct(struct task_struct *task) | |
1017 | +{ | |
1018 | + return NULL; | |
1019 | +} | |
1020 | +static inline void utrace_init_task(struct task_struct *child) | |
1021 | +{ | |
1022 | +} | |
1023 | + | |
1024 | +static inline void task_utrace_proc_status(struct seq_file *m, | |
1025 | + struct task_struct *p) | |
1026 | +{ | |
1027 | +} | |
1028 | + | |
1029 | +#else /* CONFIG_UTRACE */ | |
1030 | + | |
1031 | +static inline unsigned long task_utrace_flags(struct task_struct *task) | |
1032 | +{ | |
1033 | + return task->utrace_flags; | |
1034 | +} | |
1035 | + | |
1036 | +static inline struct utrace *task_utrace_struct(struct task_struct *task) | |
1037 | +{ | |
1038 | + return task->utrace; | |
1039 | +} | |
1040 | + | |
1041 | +static inline void utrace_init_task(struct task_struct *child) | |
1042 | +{ | |
1043 | + child->utrace_flags = 0; | |
1044 | + child->utrace = NULL; | |
1045 | +} | |
1046 | + | |
1047 | +void task_utrace_proc_status(struct seq_file *m, struct task_struct *p); | |
1048 | + | |
1049 | +/* | |
1050 | + * These are the exported entry points for tracing engines to use. | |
1051 | + * See kernel/utrace.c for their kerneldoc comments with interface details. | |
1052 | + */ | |
1053 | +struct utrace_attached_engine *utrace_attach_task( | |
1054 | + struct task_struct *, int, const struct utrace_engine_ops *, void *); | |
1055 | +struct utrace_attached_engine *utrace_attach_pid( | |
1056 | + struct pid *, int, const struct utrace_engine_ops *, void *); | |
1057 | +int __must_check utrace_control(struct task_struct *, | |
1058 | + struct utrace_attached_engine *, | |
1059 | + enum utrace_resume_action); | |
1060 | +int __must_check utrace_set_events(struct task_struct *, | |
1061 | + struct utrace_attached_engine *, | |
1062 | + unsigned long eventmask); | |
1063 | +int __must_check utrace_barrier(struct task_struct *, | |
1064 | + struct utrace_attached_engine *); | |
1065 | +int __must_check utrace_prepare_examine(struct task_struct *, | |
1066 | + struct utrace_attached_engine *, | |
1067 | + struct utrace_examiner *); | |
1068 | +int __must_check utrace_finish_examine(struct task_struct *, | |
1069 | + struct utrace_attached_engine *, | |
1070 | + struct utrace_examiner *); | |
1071 | +void __utrace_engine_release(struct kref *); | |
1072 | + | |
1073 | +/** | |
1074 | + * enum utrace_resume_action - engine's choice of action for a traced task | |
1075 | + * @UTRACE_STOP: Stay quiescent after callbacks. | |
1076 | + * @UTRACE_REPORT: Make some callback soon. | |
1077 | + * @UTRACE_INTERRUPT: Make @report_signal() callback soon. | |
1078 | + * @UTRACE_SINGLESTEP: Resume in user mode for one instruction. | |
1079 | + * @UTRACE_BLOCKSTEP: Resume in user mode until next branch. | |
1080 | + * @UTRACE_RESUME: Resume normally in user mode. | |
1081 | + * @UTRACE_DETACH: Detach my engine (implies %UTRACE_RESUME). | |
1082 | + * | |
1083 | + * See utrace_control() for detailed descriptions of each action. This is | |
1084 | + * encoded in the @action argument and the return value for every callback | |
1085 | + * with a &u32 return value. | |
1086 | + * | |
1087 | + * The order of these is important. When there is more than one engine, | |
1088 | + * each supplies its choice and the smallest value prevails. | |
1089 | + */ | |
1090 | +enum utrace_resume_action { | |
1091 | + UTRACE_STOP, | |
1092 | + UTRACE_REPORT, | |
1093 | + UTRACE_INTERRUPT, | |
1094 | + UTRACE_SINGLESTEP, | |
1095 | + UTRACE_BLOCKSTEP, | |
1096 | + UTRACE_RESUME, | |
1097 | + UTRACE_DETACH | |
1098 | +}; | |
1099 | +#define UTRACE_RESUME_MASK 0x0f | |
1100 | + | |
1101 | +/** | |
1102 | + * utrace_resume_action - &enum utrace_resume_action from callback action | |
1103 | + * @action: &u32 callback @action argument or return value | |
1104 | + * | |
1105 | + * This extracts the &enum utrace_resume_action from @action, | |
1106 | + * which is the @action argument to a &struct utrace_engine_ops | |
1107 | + * callback or the return value from one. | |
1108 | + */ | |
1109 | +static inline enum utrace_resume_action utrace_resume_action(u32 action) | |
1110 | +{ | |
1111 | + return action & UTRACE_RESUME_MASK; | |
1112 | +} | |
1113 | + | |
1114 | +/** | |
1115 | + * enum utrace_signal_action - disposition of signal | |
1116 | + * @UTRACE_SIGNAL_DELIVER: Deliver according to sigaction. | |
1117 | + * @UTRACE_SIGNAL_IGN: Ignore the signal. | |
1118 | + * @UTRACE_SIGNAL_TERM: Terminate the process. | |
1119 | + * @UTRACE_SIGNAL_CORE: Terminate with core dump. | |
1120 | + * @UTRACE_SIGNAL_STOP: Deliver as absolute stop. | |
1121 | + * @UTRACE_SIGNAL_TSTP: Deliver as job control stop. | |
1122 | + * @UTRACE_SIGNAL_REPORT: Reporting before pending signals. | |
1123 | + * @UTRACE_SIGNAL_HANDLER: Reporting after signal handler setup. | |
1124 | + * | |
1125 | + * This is encoded in the @action argument and the return value for | |
1126 | + * a @report_signal() callback. It says what will happen to the | |
1127 | + * signal described by the &siginfo_t parameter to the callback. | |
1128 | + * | |
1129 | + * The %UTRACE_SIGNAL_REPORT value is used in an @action argument when | |
1130 | + * a tracing report is being made before dequeuing any pending signal. | |
1131 | + * If this is immediately after a signal handler has been set up, then | |
1132 | + * %UTRACE_SIGNAL_HANDLER is used instead. A @report_signal callback | |
1133 | + * that uses %UTRACE_SIGNAL_DELIVER|%UTRACE_SINGLESTEP will ensure | |
1134 | + * it sees a %UTRACE_SIGNAL_HANDLER report. | |
1135 | + */ | |
1136 | +enum utrace_signal_action { | |
1137 | + UTRACE_SIGNAL_DELIVER = 0x00, | |
1138 | + UTRACE_SIGNAL_IGN = 0x10, | |
1139 | + UTRACE_SIGNAL_TERM = 0x20, | |
1140 | + UTRACE_SIGNAL_CORE = 0x30, | |
1141 | + UTRACE_SIGNAL_STOP = 0x40, | |
1142 | + UTRACE_SIGNAL_TSTP = 0x50, | |
1143 | + UTRACE_SIGNAL_REPORT = 0x60, | |
1144 | + UTRACE_SIGNAL_HANDLER = 0x70 | |
1145 | +}; | |
1146 | +#define UTRACE_SIGNAL_MASK 0xf0 | |
1147 | +#define UTRACE_SIGNAL_HOLD 0x100 /* Flag, push signal back on queue. */ | |
1148 | + | |
1149 | +/** | |
1150 | + * utrace_signal_action - &enum utrace_signal_action from callback action | |
1151 | + * @action: @report_signal callback @action argument or return value | |
1152 | + * | |
1153 | + * This extracts the &enum utrace_signal_action from @action, which | |
1154 | + * is the @action argument to a @report_signal callback or the | |
1155 | + * return value from one. | |
1156 | + */ | |
1157 | +static inline enum utrace_signal_action utrace_signal_action(u32 action) | |
1158 | +{ | |
1159 | + return action & UTRACE_SIGNAL_MASK; | |
1160 | +} | |
1161 | + | |
1162 | +/** | |
1163 | + * enum utrace_syscall_action - disposition of system call attempt | |
1164 | + * @UTRACE_SYSCALL_RUN: Run the system call. | |
1165 | + * @UTRACE_SYSCALL_ABORT: Don't run the system call. | |
1166 | + * | |
1167 | + * This is encoded in the @action argument and the return value for | |
1168 | + * a @report_syscall_entry callback. | |
1169 | + */ | |
1170 | +enum utrace_syscall_action { | |
1171 | + UTRACE_SYSCALL_RUN = 0x00, | |
1172 | + UTRACE_SYSCALL_ABORT = 0x10 | |
1173 | +}; | |
1174 | +#define UTRACE_SYSCALL_MASK 0xf0 | |
1175 | + | |
1176 | +/** | |
1177 | + * utrace_syscall_action - &enum utrace_syscall_action from callback action | |
1178 | + * @action: @report_syscall_entry callback @action or return value | |
1179 | + * | |
1180 | + * This extracts the &enum utrace_syscall_action from @action, which | |
1181 | + * is the @action argument to a @report_syscall_entry callback or the | |
1182 | + * return value from one. | |
1183 | + */ | |
1184 | +static inline enum utrace_syscall_action utrace_syscall_action(u32 action) | |
1185 | +{ | |
1186 | + return action & UTRACE_SYSCALL_MASK; | |
1187 | +} | |
1188 | + | |
1189 | +/* | |
1190 | + * Flags for utrace_attach_task() and utrace_attach_pid(). | |
1191 | + */ | |
1192 | +#define UTRACE_ATTACH_CREATE 0x0010 /* Attach a new engine. */ | |
1193 | +#define UTRACE_ATTACH_EXCLUSIVE 0x0020 /* Refuse if existing match. */ | |
1194 | +#define UTRACE_ATTACH_MATCH_OPS 0x0001 /* Match engines on ops. */ | |
1195 | +#define UTRACE_ATTACH_MATCH_DATA 0x0002 /* Match engines on data. */ | |
1196 | +#define UTRACE_ATTACH_MATCH_MASK 0x000f | |
1197 | + | |
1198 | +/** | |
1199 | + * struct utrace_attached_engine - per-engine structure | |
1200 | + * @ops: &struct utrace_engine_ops pointer passed to utrace_attach_task() | |
1201 | + * @data: engine-private &void * passed to utrace_attach_task() | |
1202 | + * @flags: event mask set by utrace_set_events() plus internal flag bits | |
1203 | + * | |
1204 | + * The task itself never has to worry about engines detaching while | |
1205 | + * it's doing event callbacks. These structures are removed from the | |
1206 | + * task's active list only when it's stopped, or by the task itself. | |
1207 | + * | |
1208 | + * utrace_engine_get() and utrace_engine_put() maintain a reference count. | |
1209 | + * When it drops to zero, the structure is freed. One reference is held | |
1210 | + * implicitly while the engine is attached to its task. | |
1211 | + */ | |
1212 | +struct utrace_attached_engine { | |
1213 | +/* private: */ | |
1214 | + struct kref kref; | |
1215 | + struct list_head entry; | |
1216 | + | |
1217 | +/* public: */ | |
1218 | + const struct utrace_engine_ops *ops; | |
1219 | + void *data; | |
1220 | + | |
1221 | + unsigned long flags; | |
1222 | +}; | |
1223 | + | |
1224 | +/** | |
1225 | + * utrace_engine_get - acquire a reference on a &struct utrace_attached_engine | |
1226 | + * @engine: &struct utrace_attached_engine pointer | |
1227 | + * | |
1228 | + * You must hold a reference on @engine, and you get another. | |
1229 | + */ | |
1230 | +static inline void utrace_engine_get(struct utrace_attached_engine *engine) | |
1231 | +{ | |
1232 | + kref_get(&engine->kref); | |
1233 | +} | |
1234 | + | |
1235 | +/** | |
1236 | + * utrace_engine_put - release a reference on a &struct utrace_attached_engine | |
1237 | + * @engine: &struct utrace_attached_engine pointer | |
1238 | + * | |
1239 | + * You must hold a reference on @engine, and you lose that reference. | |
1240 | + * If it was the last one, @engine becomes an invalid pointer. | |
1241 | + */ | |
1242 | +static inline void utrace_engine_put(struct utrace_attached_engine *engine) | |
1243 | +{ | |
1244 | + kref_put(&engine->kref, __utrace_engine_release); | |
1245 | +} | |
1246 | + | |
1247 | +/** | |
1248 | + * struct utrace_engine_ops - tracing engine callbacks | |
1249 | + * | |
1250 | + * Each @report_*() callback corresponds to an %UTRACE_EVENT(*) bit. | |
1251 | + * utrace_set_events() calls on @engine choose which callbacks will be made | |
1252 | + * to @engine from @task. | |
1253 | + * | |
1254 | + * Most callbacks take an @action argument, giving the resume action | |
1255 | + * chosen by other tracing engines. All callbacks take an @engine | |
1256 | + * argument, and a @task argument, which is always equal to @current. | |
1257 | + * For some calls, @action also includes bits specific to that event | |
1258 | + * and utrace_resume_action() is used to extract the resume action. | |
1259 | + * This shows what would happen if @engine wasn't there, or will if | |
1260 | + * the callback's return value uses %UTRACE_RESUME. This always | |
1261 | + * starts as %UTRACE_RESUME when no other tracing is being done on | |
1262 | + * this task. | |
1263 | + * | |
1264 | + * All return values contain &enum utrace_resume_action bits. For | |
1265 | + * some calls, other bits specific to that kind of event are added to | |
1266 | + * the resume action bits with OR. These are the same bits used in | |
1267 | + * the @action argument. The resume action returned by a callback | |
1268 | + * does not override previous engines' choices, it only says what | |
1269 | + * @engine wants done. What @task actually does is the action that's | |
1270 | + * most constrained among the choices made by all attached engines. | |
1271 | + * See utrace_control() for more information on the actions. | |
1272 | + * | |
1273 | + * When %UTRACE_STOP is used in @report_syscall_entry, then @task | |
1274 | + * stops before attempting the system call. In other cases, the | |
1275 | + * resume action does not take effect until @task is ready to check | |
1276 | + * for signals and return to user mode. If there are more callbacks | |
1277 | + * to be made, the last round of calls determines the final action. | |
1278 | + * A @report_quiesce callback with @event zero, or a @report_signal | |
1279 | + * callback, will always be the last one made before @task resumes. | |
1280 | + * Only %UTRACE_STOP is "sticky"--if @engine returned %UTRACE_STOP | |
1281 | + * then @task stays stopped unless @engine returns different from a | |
1282 | + * following callback. | |
1283 | + * | |
1284 | + * The report_death() and report_reap() callbacks do not take @action | |
1285 | + * arguments, and only %UTRACE_DETACH is meaningful in the return value | |
1286 | + * from a report_death() callback. None of the resume actions applies | |
1287 | + * to a dead thread. | |
1288 | + * | |
1289 | + * All @report_*() hooks are called with no locks held, in a generally | |
1290 | + * safe environment when we will be returning to user mode soon (or just | |
1291 | + * entered the kernel). It is fine to block for memory allocation and | |
1292 | + * the like, but all hooks are asynchronous and must not block on | |
1293 | + * external events! If you want the thread to block, use %UTRACE_STOP | |
1294 | + * in your hook's return value; then later wake it up with utrace_control(). | |
1295 | + * | |
1296 | + * The @unsafe_exec and @tracer_task hooks are not associated with | |
1297 | + * event reports. These may be %NULL if the engine has nothing to say. | |
1298 | + * These hooks are called in more constrained environments and should | |
1299 | + * not block or do very much. | |
1300 | + * | |
1301 | + * @report_quiesce: | |
1302 | + * Requested by %UTRACE_EVENT(%QUIESCE). | |
1303 | + * This does not indicate any event, but just that @task (the current | |
1304 | + * thread) is in a safe place for examination. This call is made | |
1305 | + * before each specific event callback, except for @report_reap. | |
1306 | + * The @event argument gives the %UTRACE_EVENT(@which) value for | |
1307 | + * the event occurring. This callback might be made for events @engine | |
1308 | + * has not requested, if some other engine is tracing the event; | |
1309 | + * calling utrace_set_events() call here can request the immediate | |
1310 | + * callback for this occurrence of @event. @event is zero when there | |
1311 | + * is no other event, @task is now ready to check for signals and | |
1312 | + * return to user mode, and some engine has used %UTRACE_REPORT or | |
1313 | + * %UTRACE_INTERRUPT to request this callback. For this case, | |
1314 | + * if @report_signal is not %NULL, the @report_quiesce callback | |
1315 | + * may be replaced with a @report_signal callback passing | |
1316 | + * %UTRACE_SIGNAL_REPORT in its @action argument, whenever @task is | |
1317 | + * entering the signal-check path anyway. | |
1318 | + * | |
1319 | + * @report_signal: | |
1320 | + * Requested by %UTRACE_EVENT(%SIGNAL_*) or %UTRACE_EVENT(%QUIESCE). | |
1321 | + * Use utrace_signal_action() and utrace_resume_action() on @action. | |
1322 | + * The signal action is %UTRACE_SIGNAL_REPORT when some engine has | |
1323 | + * used %UTRACE_REPORT or %UTRACE_INTERRUPT; the callback can choose | |
1324 | + * to stop or to deliver an artificial signal, before pending signals. | |
1325 | + * It's %UTRACE_SIGNAL_HANDLER instead when signal handler setup just | |
1326 | + * finished (after a previous %UTRACE_SIGNAL_DELIVER return); this | |
1327 | + * serves in lieu of any %UTRACE_SIGNAL_REPORT callback requested by | |
1328 | + * %UTRACE_REPORT or %UTRACE_INTERRUPT, and is also implicitly | |
1329 | + * requested by %UTRACE_SINGLESTEP or %UTRACE_BLOCKSTEP into the | |
1330 | + * signal delivery. The other signal actions indicate a signal about | |
1331 | + * to be delivered; the previous engine's return value sets the signal | |
1332 | + * action seen by the the following engine's callback. The @info data | |
1333 | + * can be changed at will, including @info->si_signo. The settings in | |
1334 | + * @return_ka determines what %UTRACE_SIGNAL_DELIVER does. @orig_ka | |
1335 | + * is what was in force before other tracing engines intervened, and | |
1336 | + * it's %NULL when this report began as %UTRACE_SIGNAL_REPORT or | |
1337 | + * %UTRACE_SIGNAL_HANDLER. For a report without a new signal, @info | |
1338 | + * is left uninitialized and must be set completely by an engine that | |
1339 | + * chooses to deliver a signal; if there was a previous @report_signal | |
1340 | + * callback ending in %UTRACE_STOP and it was just resumed using | |
1341 | + * %UTRACE_REPORT or %UTRACE_INTERRUPT, then @info is left unchanged | |
1342 | + * from the previous callback. In this way, the original signal can | |
1343 | + * be left in @info while returning %UTRACE_STOP|%UTRACE_SIGNAL_IGN | |
1344 | + * and then found again when resuming @task with %UTRACE_INTERRUPT. | |
1345 | + * The %UTRACE_SIGNAL_HOLD flag bit can be OR'd into the return value, | |
1346 | + * and might be in @action if the previous engine returned it. This | |
1347 | + * flag asks that the signal in @info be pushed back on @task's queue | |
1348 | + * so that it will be seen again after whatever action is taken now. | |
1349 | + * | |
1350 | + * @report_clone: | |
1351 | + * Requested by %UTRACE_EVENT(%CLONE). | |
1352 | + * Event reported for parent, before the new task @child might run. | |
1353 | + * @clone_flags gives the flags used in the clone system call, | |
1354 | + * or equivalent flags for a fork() or vfork() system call. | |
1355 | + * This function can use utrace_attach_task() on @child. It's guaranteed | |
1356 | + * that asynchronous utrace_attach_task() calls will be ordered after | |
1357 | + * any calls in @report_clone callbacks for the parent. Thus | |
1358 | + * when using %UTRACE_ATTACH_EXCLUSIVE in the asynchronous calls, | |
1359 | + * you can be sure that the parent's @report_clone callback has | |
1360 | + * already attached to @child or chosen not to. Passing %UTRACE_STOP | |
1361 | + * to utrace_control() on @child here keeps the child stopped before | |
1362 | + * it ever runs in user mode, %UTRACE_REPORT or %UTRACE_INTERRUPT | |
1363 | + * ensures a callback from @child before it starts in user mode. | |
1364 | + * | |
1365 | + * @report_jctl: | |
1366 | + * Requested by %UTRACE_EVENT(%JCTL). | |
1367 | + * Job control event; @type is %CLD_STOPPED or %CLD_CONTINUED, | |
1368 | + * indicating whether we are stopping or resuming now. If @notify | |
1369 | + * is nonzero, @task is the last thread to stop and so will send | |
1370 | + * %SIGCHLD to its parent after this callback; @notify reflects | |
1371 | + * what the parent's %SIGCHLD has in @si_code, which can sometimes | |
1372 | + * be %CLD_STOPPED even when @type is %CLD_CONTINUED. | |
1373 | + * | |
1374 | + * @report_exec: | |
1375 | + * Requested by %UTRACE_EVENT(%EXEC). | |
1376 | + * An execve system call has succeeded and the new program is about to | |
1377 | + * start running. The initial user register state is handy to be tweaked | |
1378 | + * directly in @regs. @fmt and @bprm gives the details of this exec. | |
1379 | + * | |
1380 | + * @report_syscall_entry: | |
1381 | + * Requested by %UTRACE_EVENT(%SYSCALL_ENTRY). | |
1382 | + * Thread has entered the kernel to request a system call. | |
1383 | + * The user register state is handy to be tweaked directly in @regs. | |
1384 | + * The @action argument contains an &enum utrace_syscall_action, | |
1385 | + * use utrace_syscall_action() to extract it. The return value | |
1386 | + * overrides the last engine's action for the system call. | |
1387 | + * If the final action is %UTRACE_SYSCALL_ABORT, no system call | |
1388 | + * is made. The details of the system call being attempted can | |
1389 | + * be fetched here with syscall_get_nr() and syscall_get_arguments(). | |
1390 | + * The parameter registers can be changed with syscall_set_arguments(). | |
1391 | + * | |
1392 | + * @report_syscall_exit: | |
1393 | + * Requested by %UTRACE_EVENT(%SYSCALL_EXIT). | |
1394 | + * Thread is about to leave the kernel after a system call request. | |
1395 | + * The user register state is handy to be tweaked directly in @regs. | |
1396 | + * The results of the system call attempt can be examined here using | |
1397 | + * syscall_get_error() and syscall_get_return_value(). It is safe | |
1398 | + * here to call syscall_set_return_value() or syscall_rollback(). | |
1399 | + * | |
1400 | + * @report_exit: | |
1401 | + * Requested by %UTRACE_EVENT(%EXIT). | |
1402 | + * Thread is exiting and cannot be prevented from doing so, | |
1403 | + * but all its state is still live. The @code value will be | |
1404 | + * the wait result seen by the parent, and can be changed by | |
1405 | + * this engine or others. The @orig_code value is the real | |
1406 | + * status, not changed by any tracing engine. Returning %UTRACE_STOP | |
1407 | + * here keeps @task stopped before it cleans up its state and dies, | |
1408 | + * so it can be examined by other processes. When @task is allowed | |
1409 | + * to run, it will die and get to the @report_death callback. | |
1410 | + * | |
1411 | + * @report_death: | |
1412 | + * Requested by %UTRACE_EVENT(%DEATH). | |
1413 | + * Thread is really dead now. It might be reaped by its parent at | |
1414 | + * any time, or self-reap immediately. Though the actual reaping | |
1415 | + * may happen in parallel, a report_reap() callback will always be | |
1416 | + * ordered after a report_death() callback. | |
1417 | + * | |
1418 | + * @report_reap: | |
1419 | + * Requested by %UTRACE_EVENT(%REAP). | |
1420 | + * Called when someone reaps the dead task (parent, init, or self). | |
1421 | + * This means the parent called wait, or else this was a detached | |
1422 | + * thread or a process whose parent ignores SIGCHLD. | |
1423 | + * No more callbacks are made after this one. | |
1424 | + * The engine is always detached. | |
1425 | + * There is nothing more a tracing engine can do about this thread. | |
1426 | + * After this callback, the @engine pointer will become invalid. | |
1427 | + * The @task pointer may become invalid if get_task_struct() hasn't | |
1428 | + * been used to keep it alive. | |
1429 | + * An engine should always request this callback if it stores the | |
1430 | + * @engine pointer or stores any pointer in @engine->data, so it | |
1431 | + * can clean up its data structures. | |
1432 | + * Unlike other callbacks, this can be called from the parent's context | |
1433 | + * rather than from the traced thread itself--it must not delay the | |
1434 | + * parent by blocking. | |
1435 | + * | |
1436 | + * @unsafe_exec: | |
1437 | + * Used if not %NULL. | |
1438 | + * Return %LSM_UNSAFE_* bits that apply to the exec in progress | |
1439 | + * due to tracing done by this engine. These bits indicate that | |
1440 | + * someone is able to examine the process and so a set-UID or similar | |
1441 | + * privilege escalation may not be safe to permit. | |
1442 | + * Called with task_lock() held. | |
1443 | + * | |
1444 | + * @tracer_task: | |
1445 | + * Used if not %NULL. | |
1446 | + * Return the &struct task_struct for the task using ptrace() on | |
1447 | + * @task, or %NULL. Always called with rcu_read_lock() held to | |
1448 | + * keep the returned struct alive. At exec time, this may be | |
1449 | + * called with task_lock() still held from when unsafe_exec() was | |
1450 | + * just called. In that case it must give results consistent | |
1451 | + * with those unsafe_exec() results, i.e. non-%NULL if any | |
1452 | + * %LSM_UNSAFE_PTRACE_* bits were set. The value is also used to | |
1453 | + * display after "TracerPid:" in /proc/PID/status, where it is | |
1454 | + * called with only rcu_read_lock() held. If this engine returns | |
1455 | + * %NULL, another engine may supply the result. | |
1456 | + */ | |
1457 | +struct utrace_engine_ops { | |
1458 | + u32 (*report_quiesce)(enum utrace_resume_action action, | |
1459 | + struct utrace_attached_engine *engine, | |
1460 | + struct task_struct *task, | |
1461 | + unsigned long event); | |
1462 | + u32 (*report_signal)(u32 action, | |
1463 | + struct utrace_attached_engine *engine, | |
1464 | + struct task_struct *task, | |
1465 | + struct pt_regs *regs, | |
1466 | + siginfo_t *info, | |
1467 | + const struct k_sigaction *orig_ka, | |
1468 | + struct k_sigaction *return_ka); | |
1469 | + u32 (*report_clone)(enum utrace_resume_action action, | |
1470 | + struct utrace_attached_engine *engine, | |
1471 | + struct task_struct *parent, | |
1472 | + unsigned long clone_flags, | |
1473 | + struct task_struct *child); | |
1474 | + u32 (*report_jctl)(enum utrace_resume_action action, | |
1475 | + struct utrace_attached_engine *engine, | |
1476 | + struct task_struct *task, | |
1477 | + int type, int notify); | |
1478 | + u32 (*report_exec)(enum utrace_resume_action action, | |
1479 | + struct utrace_attached_engine *engine, | |
1480 | + struct task_struct *task, | |
1481 | + const struct linux_binfmt *fmt, | |
1482 | + const struct linux_binprm *bprm, | |
1483 | + struct pt_regs *regs); | |
1484 | + u32 (*report_syscall_entry)(u32 action, | |
1485 | + struct utrace_attached_engine *engine, | |
1486 | + struct task_struct *task, | |
1487 | + struct pt_regs *regs); | |
1488 | + u32 (*report_syscall_exit)(enum utrace_resume_action action, | |
1489 | + struct utrace_attached_engine *engine, | |
1490 | + struct task_struct *task, | |
1491 | + struct pt_regs *regs); | |
1492 | + u32 (*report_exit)(enum utrace_resume_action action, | |
1493 | + struct utrace_attached_engine *engine, | |
1494 | + struct task_struct *task, | |
1495 | + long orig_code, long *code); | |
1496 | + u32 (*report_death)(struct utrace_attached_engine *engine, | |
1497 | + struct task_struct *task, | |
1498 | + bool group_dead, int signal); | |
1499 | + void (*report_reap)(struct utrace_attached_engine *engine, | |
1500 | + struct task_struct *task); | |
1501 | + | |
1502 | + int (*unsafe_exec)(struct utrace_attached_engine *engine, | |
1503 | + struct task_struct *task); | |
1504 | + struct task_struct *(*tracer_task)( | |
1505 | + struct utrace_attached_engine *engine, | |
1506 | + struct task_struct *task); | |
1507 | +}; | |
1508 | + | |
1509 | +/** | |
1510 | + * struct utrace_examiner - private state for using utrace_prepare_examine() | |
1511 | + * @dummy: all fields are private, none described here | |
1512 | + * | |
1513 | + * The members of &struct utrace_examiner are private to the implementation. | |
1514 | + * This data type holds the state from a call to utrace_prepare_examine() | |
1515 | + * to be used by a call to utrace_finish_examine(). | |
1516 | + */ | |
1517 | +struct utrace_examiner { | |
1518 | +/* private: */ | |
1519 | + long state; /* cache of task_struct.state */ | |
1520 | + unsigned long ncsw; /* cache of wait_task_inactive() return value */ | |
1521 | +/* public: */ | |
1522 | + struct {} dummy; | |
1523 | +}; | |
1524 | + | |
1525 | +/** | |
1526 | + * utrace_control_pid - control a thread being traced by a tracing engine | |
1527 | + * @pid: thread to affect | |
1528 | + * @engine: attached engine to affect | |
1529 | + * @action: &enum utrace_resume_action for thread to do | |
1530 | + * | |
1531 | + * This is the same as utrace_control(), but takes a &struct pid | |
1532 | + * pointer rather than a &struct task_struct pointer. The caller must | |
1533 | + * hold a ref on @pid, but does not need to worry about the task | |
1534 | + * staying valid. If it's been reaped so that @pid points nowhere, | |
1535 | + * then this call returns -%ESRCH. | |
1536 | + */ | |
1537 | +static inline __must_check int utrace_control_pid( | |
1538 | + struct pid *pid, struct utrace_attached_engine *engine, | |
1539 | + enum utrace_resume_action action) | |
1540 | +{ | |
1541 | + /* | |
1542 | + * We don't bother with rcu_read_lock() here to protect the | |
1543 | + * task_struct pointer, because utrace_control will return | |
1544 | + * -ESRCH without looking at that pointer if the engine is | |
1545 | + * already detached. A task_struct pointer can't die before | |
1546 | + * all the engines are detached in release_task() first. | |
1547 | + */ | |
1548 | + struct task_struct *task = pid_task(pid, PIDTYPE_PID); | |
1549 | + return unlikely(!task) ? -ESRCH : utrace_control(task, engine, action); | |
1550 | +} | |
1551 | + | |
1552 | +/** | |
1553 | + * utrace_set_events_pid - choose which event reports a tracing engine gets | |
1554 | + * @pid: thread to affect | |
1555 | + * @engine: attached engine to affect | |
1556 | + * @eventmask: new event mask | |
1557 | + * | |
1558 | + * This is the same as utrace_set_events(), but takes a &struct pid | |
1559 | + * pointer rather than a &struct task_struct pointer. The caller must | |
1560 | + * hold a ref on @pid, but does not need to worry about the task | |
1561 | + * staying valid. If it's been reaped so that @pid points nowhere, | |
1562 | + * then this call returns -%ESRCH. | |
1563 | + */ | |
1564 | +static inline __must_check int utrace_set_events_pid( | |
1565 | + struct pid *pid, struct utrace_attached_engine *engine, | |
1566 | + unsigned long eventmask) | |
1567 | +{ | |
1568 | + struct task_struct *task = pid_task(pid, PIDTYPE_PID); | |
1569 | + return unlikely(!task) ? -ESRCH : | |
1570 | + utrace_set_events(task, engine, eventmask); | |
1571 | +} | |
1572 | + | |
1573 | +/** | |
1574 | + * utrace_barrier_pid - synchronize with simultaneous tracing callbacks | |
1575 | + * @pid: thread to affect | |
1576 | + * @engine: engine to affect (can be detached) | |
1577 | + * | |
1578 | + * This is the same as utrace_barrier(), but takes a &struct pid | |
1579 | + * pointer rather than a &struct task_struct pointer. The caller must | |
1580 | + * hold a ref on @pid, but does not need to worry about the task | |
1581 | + * staying valid. If it's been reaped so that @pid points nowhere, | |
1582 | + * then this call returns -%ESRCH. | |
1583 | + */ | |
1584 | +static inline __must_check int utrace_barrier_pid( | |
1585 | + struct pid *pid, struct utrace_attached_engine *engine) | |
1586 | +{ | |
1587 | + struct task_struct *task = pid_task(pid, PIDTYPE_PID); | |
1588 | + return unlikely(!task) ? -ESRCH : utrace_barrier(task, engine); | |
1589 | +} | |
1590 | + | |
1591 | +#endif /* CONFIG_UTRACE */ | |
1592 | + | |
1593 | +#endif /* linux/utrace.h */ | |
1594 | --- a/init/Kconfig | |
1595 | +++ b/init/Kconfig | |
1596 | @@ -916,6 +916,16 @@ config STOP_MACHINE | |
1597 | help | |
1598 | Need stop_machine() primitive. | |
1599 | ||
1600 | +menuconfig UTRACE | |
1601 | + bool "Infrastructure for tracing and debugging user processes" | |
1602 | + depends on EXPERIMENTAL | |
1603 | + depends on HAVE_ARCH_TRACEHOOK | |
1604 | + depends on MODULES | |
1605 | + help | |
1606 | + Enable the utrace process tracing interface. This is an internal | |
1607 | + kernel interface exported to kernel modules, to track events in | |
1608 | + user threads, extract and change user thread state. | |
1609 | + | |
1610 | source "block/Kconfig" | |
1611 | ||
1612 | config PREEMPT_NOTIFIERS | |
1613 | --- a/kernel/Makefile | |
1614 | +++ b/kernel/Makefile | |
1615 | @@ -65,6 +65,7 @@ obj-$(CONFIG_IKCONFIG) += configs.o | |
1616 | obj-$(CONFIG_RESOURCE_COUNTERS) += res_counter.o | |
1617 | obj-$(CONFIG_STOP_MACHINE) += stop_machine.o | |
1618 | obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o | |
1619 | +obj-$(CONFIG_UTRACE) += utrace.o | |
1620 | obj-$(CONFIG_AUDIT) += audit.o auditfilter.o | |
1621 | obj-$(CONFIG_AUDITSYSCALL) += auditsc.o | |
1622 | obj-$(CONFIG_AUDIT_TREE) += audit_tree.o | |
1623 | --- /dev/null | |
1624 | +++ b/kernel/utrace.c | |
1625 | @@ -0,0 +1,2495 @@ | |
1626 | +/* | |
1627 | + * utrace infrastructure interface for debugging user processes | |
1628 | + * | |
1629 | + * Copyright (C) 2006, 2007, 2008 Red Hat, Inc. All rights reserved. | |
1630 | + * | |
1631 | + * This copyrighted material is made available to anyone wishing to use, | |
1632 | + * modify, copy, or redistribute it subject to the terms and conditions | |
1633 | + * of the GNU General Public License v.2. | |
1634 | + * | |
1635 | + * Red Hat Author: Roland McGrath. | |
1636 | + */ | |
1637 | + | |
1638 | +#include <linux/utrace.h> | |
1639 | +#include <linux/tracehook.h> | |
1640 | +#include <linux/regset.h> | |
1641 | +#include <asm/syscall.h> | |
1642 | +#include <linux/ptrace.h> | |
1643 | +#include <linux/err.h> | |
1644 | +#include <linux/sched.h> | |
1645 | +#include <linux/freezer.h> | |
1646 | +#include <linux/module.h> | |
1647 | +#include <linux/init.h> | |
1648 | +#include <linux/slab.h> | |
1649 | +#include <linux/seq_file.h> | |
1650 | + | |
1651 | + | |
1652 | +#define UTRACE_DEBUG 1 | |
1653 | +#ifdef UTRACE_DEBUG | |
1654 | +#define CHECK_INIT(p) atomic_set(&(p)->check_dead, 1) | |
1655 | +#define CHECK_DEAD(p) BUG_ON(!atomic_dec_and_test(&(p)->check_dead)) | |
1656 | +#else | |
1657 | +#define CHECK_INIT(p) do { } while (0) | |
1658 | +#define CHECK_DEAD(p) do { } while (0) | |
1659 | +#endif | |
1660 | + | |
1661 | +/* | |
1662 | + * Per-thread structure task_struct.utrace points to. | |
1663 | + * | |
1664 | + * The task itself never has to worry about this going away after | |
1665 | + * some event is found set in task_struct.utrace_flags. | |
1666 | + * Once created, this pointer is changed only when the task is quiescent | |
1667 | + * (TASK_TRACED or TASK_STOPPED with the siglock held, or dead). | |
1668 | + * | |
1669 | + * For other parties, the pointer to this is protected by RCU and | |
1670 | + * task_lock. Since call_rcu is never used while the thread is alive and | |
1671 | + * using this struct utrace, we can overlay the RCU data structure used | |
1672 | + * only for a dead struct with some local state used only for a live utrace | |
1673 | + * on an active thread. | |
1674 | + * | |
1675 | + * The two lists @attached and @attaching work together for smooth | |
1676 | + * asynchronous attaching with low overhead. Modifying either list | |
1677 | + * requires @lock. The @attaching list can be modified any time while | |
1678 | + * holding @lock. New engines being attached always go on this list. | |
1679 | + * | |
1680 | + * The @attached list is what the task itself uses for its reporting | |
1681 | + * loops. When the task itself is not quiescent, it can use the | |
1682 | + * @attached list without taking any lock. Noone may modify the list | |
1683 | + * when the task is not quiescent. When it is quiescent, that means | |
1684 | + * that it won't run again without taking @lock itself before using | |
1685 | + * the list. | |
1686 | + * | |
1687 | + * At each place where we know the task is quiescent (or it's current), | |
1688 | + * while holding @lock, we call splice_attaching(), below. This moves | |
1689 | + * the @attaching list members on to the end of the @attached list. | |
1690 | + * Since this happens at the start of any reporting pass, any new | |
1691 | + * engines attached asynchronously go on the stable @attached list | |
1692 | + * in time to have their callbacks seen. | |
1693 | + */ | |
1694 | +struct utrace { | |
1695 | + union { | |
1696 | + struct rcu_head dead; | |
1697 | + struct { | |
1698 | + struct task_struct *cloning; | |
1699 | + } live; | |
1700 | + } u; | |
1701 | + | |
1702 | + struct list_head attached, attaching; | |
1703 | + spinlock_t lock; | |
1704 | +#ifdef UTRACE_DEBUG | |
1705 | + atomic_t check_dead; | |
1706 | +#endif | |
1707 | + | |
1708 | + struct utrace_attached_engine *reporting; | |
1709 | + | |
1710 | + unsigned int stopped:1; | |
1711 | + unsigned int report:1; | |
1712 | + unsigned int interrupt:1; | |
1713 | + unsigned int signal_handler:1; | |
1714 | + unsigned int death:1; /* in utrace_report_death() now */ | |
1715 | + unsigned int reap:1; /* release_task() has run */ | |
1716 | +}; | |
1717 | + | |
1718 | +static struct kmem_cache *utrace_cachep; | |
1719 | +static struct kmem_cache *utrace_engine_cachep; | |
1720 | +static const struct utrace_engine_ops utrace_detached_ops; /* forward decl */ | |
1721 | + | |
1722 | +static int __init utrace_init(void) | |
1723 | +{ | |
1724 | + utrace_cachep = KMEM_CACHE(utrace, SLAB_PANIC); | |
1725 | + utrace_engine_cachep = KMEM_CACHE(utrace_attached_engine, SLAB_PANIC); | |
1726 | + return 0; | |
1727 | +} | |
1728 | +subsys_initcall(utrace_init); | |
1729 | + | |
1730 | + | |
1731 | +/* | |
1732 | + * Make sure target->utrace is allocated, and return with it locked on | |
1733 | + * success. This function mediates startup races. The creating parent | |
1734 | + * task has priority, and other callers will delay here to let its call | |
1735 | + * succeed and take the new utrace lock first. | |
1736 | + */ | |
1737 | +static struct utrace *utrace_first_engine(struct task_struct *target, | |
1738 | + struct utrace_attached_engine *engine) | |
1739 | + __acquires(utrace->lock) | |
1740 | +{ | |
1741 | + struct utrace *utrace; | |
1742 | + | |
1743 | + /* | |
1744 | + * If this is a newborn thread and we are not the creator, | |
1745 | + * we have to wait for it. The creator gets the first chance | |
1746 | + * to attach. The PF_STARTING flag is cleared after its | |
1747 | + * report_clone hook has had a chance to run. | |
1748 | + */ | |
1749 | + if (target->flags & PF_STARTING) { | |
1750 | + utrace = current->utrace; | |
1751 | + if (utrace == NULL || utrace->u.live.cloning != target) { | |
1752 | + yield(); | |
1753 | + if (signal_pending(current)) | |
1754 | + return ERR_PTR(-ERESTARTNOINTR); | |
1755 | + return NULL; | |
1756 | + } | |
1757 | + } | |
1758 | + | |
1759 | + utrace = kmem_cache_zalloc(utrace_cachep, GFP_KERNEL); | |
1760 | + if (unlikely(utrace == NULL)) | |
1761 | + return ERR_PTR(-ENOMEM); | |
1762 | + | |
1763 | + INIT_LIST_HEAD(&utrace->attached); | |
1764 | + INIT_LIST_HEAD(&utrace->attaching); | |
1765 | + list_add(&engine->entry, &utrace->attached); | |
1766 | + spin_lock_init(&utrace->lock); | |
1767 | + CHECK_INIT(utrace); | |
1768 | + | |
1769 | + spin_lock(&utrace->lock); | |
1770 | + task_lock(target); | |
1771 | + if (likely(target->utrace == NULL)) { | |
1772 | + rcu_assign_pointer(target->utrace, utrace); | |
1773 | + | |
1774 | + /* | |
1775 | + * The task_lock protects us against another thread doing | |
1776 | + * the same thing. We might still be racing against | |
1777 | + * tracehook_release_task. It's called with ->exit_state | |
1778 | + * set to EXIT_DEAD and then checks ->utrace with an | |
1779 | + * smp_mb() in between. If EXIT_DEAD is set, then | |
1780 | + * release_task might have checked ->utrace already and saw | |
1781 | + * it NULL; we can't attach. If we see EXIT_DEAD not yet | |
1782 | + * set after our barrier, then we know release_task will | |
1783 | + * see our target->utrace pointer. | |
1784 | + */ | |
1785 | + smp_mb(); | |
1786 | + if (likely(target->exit_state != EXIT_DEAD)) { | |
1787 | + task_unlock(target); | |
1788 | + return utrace; | |
1789 | + } | |
1790 | + | |
1791 | + /* | |
1792 | + * The target has already been through release_task. | |
1793 | + * Our caller will restart and notice it's too late now. | |
1794 | + */ | |
1795 | + target->utrace = NULL; | |
1796 | + } | |
1797 | + | |
1798 | + /* | |
1799 | + * Another engine attached first, so there is a struct already. | |
1800 | + * A null return says to restart looking for the existing one. | |
1801 | + */ | |
1802 | + task_unlock(target); | |
1803 | + spin_unlock(&utrace->lock); | |
1804 | + kmem_cache_free(utrace_cachep, utrace); | |
1805 | + | |
1806 | + return NULL; | |
1807 | +} | |
1808 | + | |
1809 | +static void utrace_free(struct rcu_head *rhead) | |
1810 | +{ | |
1811 | + struct utrace *utrace = container_of(rhead, struct utrace, u.dead); | |
1812 | + kmem_cache_free(utrace_cachep, utrace); | |
1813 | +} | |
1814 | + | |
1815 | +/* | |
1816 | + * Called with utrace locked. Clean it up and free it via RCU. | |
1817 | + */ | |
1818 | +static void rcu_utrace_free(struct utrace *utrace) | |
1819 | + __releases(utrace->lock) | |
1820 | +{ | |
1821 | + CHECK_DEAD(utrace); | |
1822 | + spin_unlock(&utrace->lock); | |
1823 | + call_rcu(&utrace->u.dead, utrace_free); | |
1824 | +} | |
1825 | + | |
1826 | +/* | |
1827 | + * This is the exported function used by the utrace_engine_put() inline. | |
1828 | + */ | |
1829 | +void __utrace_engine_release(struct kref *kref) | |
1830 | +{ | |
1831 | + struct utrace_attached_engine *engine = | |
1832 | + container_of(kref, struct utrace_attached_engine, kref); | |
1833 | + BUG_ON(!list_empty(&engine->entry)); | |
1834 | + kmem_cache_free(utrace_engine_cachep, engine); | |
1835 | +} | |
1836 | +EXPORT_SYMBOL_GPL(__utrace_engine_release); | |
1837 | + | |
1838 | +static bool engine_matches(struct utrace_attached_engine *engine, int flags, | |
1839 | + const struct utrace_engine_ops *ops, void *data) | |
1840 | +{ | |
1841 | + if ((flags & UTRACE_ATTACH_MATCH_OPS) && engine->ops != ops) | |
1842 | + return false; | |
1843 | + if ((flags & UTRACE_ATTACH_MATCH_DATA) && engine->data != data) | |
1844 | + return false; | |
1845 | + return engine->ops && engine->ops != &utrace_detached_ops; | |
1846 | +} | |
1847 | + | |
1848 | +static struct utrace_attached_engine *matching_engine( | |
1849 | + struct utrace *utrace, int flags, | |
1850 | + const struct utrace_engine_ops *ops, void *data) | |
1851 | +{ | |
1852 | + struct utrace_attached_engine *engine; | |
1853 | + list_for_each_entry(engine, &utrace->attached, entry) | |
1854 | + if (engine_matches(engine, flags, ops, data)) | |
1855 | + return engine; | |
1856 | + list_for_each_entry(engine, &utrace->attaching, entry) | |
1857 | + if (engine_matches(engine, flags, ops, data)) | |
1858 | + return engine; | |
1859 | + return NULL; | |
1860 | +} | |
1861 | + | |
1862 | +/* | |
1863 | + * Allocate a new engine structure. It starts out with two refs: | |
1864 | + * one ref for utrace_attach_task() to return, and ref for being attached. | |
1865 | + */ | |
1866 | +static struct utrace_attached_engine *alloc_engine(void) | |
1867 | +{ | |
1868 | + struct utrace_attached_engine *engine; | |
1869 | + engine = kmem_cache_alloc(utrace_engine_cachep, GFP_KERNEL); | |
1870 | + if (likely(engine)) { | |
1871 | + engine->flags = 0; | |
1872 | + kref_set(&engine->kref, 2); | |
1873 | + } | |
1874 | + return engine; | |
1875 | +} | |
1876 | + | |
1877 | +/** | |
1878 | + * utrace_attach_task - attach new engine, or look up an attached engine | |
1879 | + * @target: thread to attach to | |
1880 | + * @flags: flag bits combined with OR, see below | |
1881 | + * @ops: callback table for new engine | |
1882 | + * @data: engine private data pointer | |
1883 | + * | |
1884 | + * The caller must ensure that the @target thread does not get freed, | |
1885 | + * i.e. hold a ref or be its parent. It is always safe to call this | |
1886 | + * on @current, or on the @child pointer in a @report_clone callback. | |
1887 | + * For most other cases, it's easier to use utrace_attach_pid() instead. | |
1888 | + * | |
1889 | + * UTRACE_ATTACH_CREATE: | |
1890 | + * Create a new engine. If %UTRACE_ATTACH_CREATE is not specified, you | |
1891 | + * only look up an existing engine already attached to the thread. | |
1892 | + * | |
1893 | + * UTRACE_ATTACH_EXCLUSIVE: | |
1894 | + * Attempting to attach a second (matching) engine fails with -%EEXIST. | |
1895 | + * | |
1896 | + * UTRACE_ATTACH_MATCH_OPS: Only consider engines matching @ops. | |
1897 | + * UTRACE_ATTACH_MATCH_DATA: Only consider engines matching @data. | |
1898 | + */ | |
1899 | +struct utrace_attached_engine *utrace_attach_task( | |
1900 | + struct task_struct *target, int flags, | |
1901 | + const struct utrace_engine_ops *ops, void *data) | |
1902 | +{ | |
1903 | + struct utrace *utrace; | |
1904 | + struct utrace_attached_engine *engine; | |
1905 | + | |
1906 | +restart: | |
1907 | + rcu_read_lock(); | |
1908 | + utrace = rcu_dereference(target->utrace); | |
1909 | + smp_rmb(); | |
1910 | + if (unlikely(target->exit_state == EXIT_DEAD)) { | |
1911 | + /* | |
1912 | + * The target has already been reaped. | |
1913 | + * Check this first; a race with reaping may lead to restart. | |
1914 | + */ | |
1915 | + rcu_read_unlock(); | |
1916 | + if (!(flags & UTRACE_ATTACH_CREATE)) | |
1917 | + return ERR_PTR(-ENOENT); | |
1918 | + return ERR_PTR(-ESRCH); | |
1919 | + } | |
1920 | + | |
1921 | + if (utrace == NULL) { | |
1922 | + rcu_read_unlock(); | |
1923 | + | |
1924 | + if (!(flags & UTRACE_ATTACH_CREATE)) | |
1925 | + return ERR_PTR(-ENOENT); | |
1926 | + | |
1927 | + if (unlikely(target->flags & PF_KTHREAD)) | |
1928 | + /* | |
1929 | + * Silly kernel, utrace is for users! | |
1930 | + */ | |
1931 | + return ERR_PTR(-EPERM); | |
1932 | + | |
1933 | + engine = alloc_engine(); | |
1934 | + if (unlikely(!engine)) | |
1935 | + return ERR_PTR(-ENOMEM); | |
1936 | + | |
1937 | + goto first; | |
1938 | + } | |
1939 | + | |
1940 | + if (!(flags & UTRACE_ATTACH_CREATE)) { | |
1941 | + spin_lock(&utrace->lock); | |
1942 | + engine = matching_engine(utrace, flags, ops, data); | |
1943 | + if (engine) | |
1944 | + utrace_engine_get(engine); | |
1945 | + spin_unlock(&utrace->lock); | |
1946 | + rcu_read_unlock(); | |
1947 | + return engine ?: ERR_PTR(-ENOENT); | |
1948 | + } | |
1949 | + rcu_read_unlock(); | |
1950 | + | |
1951 | + if (unlikely(!ops) || unlikely(ops == &utrace_detached_ops)) | |
1952 | + return ERR_PTR(-EINVAL); | |
1953 | + | |
1954 | + engine = alloc_engine(); | |
1955 | + if (unlikely(!engine)) | |
1956 | + return ERR_PTR(-ENOMEM); | |
1957 | + | |
1958 | + rcu_read_lock(); | |
1959 | + utrace = rcu_dereference(target->utrace); | |
1960 | + if (unlikely(utrace == NULL)) { /* Race with detach. */ | |
1961 | + rcu_read_unlock(); | |
1962 | + goto first; | |
1963 | + } | |
1964 | + spin_lock(&utrace->lock); | |
1965 | + | |
1966 | + if (flags & UTRACE_ATTACH_EXCLUSIVE) { | |
1967 | + struct utrace_attached_engine *old; | |
1968 | + old = matching_engine(utrace, flags, ops, data); | |
1969 | + if (old) { | |
1970 | + spin_unlock(&utrace->lock); | |
1971 | + rcu_read_unlock(); | |
1972 | + kmem_cache_free(utrace_engine_cachep, engine); | |
1973 | + return ERR_PTR(-EEXIST); | |
1974 | + } | |
1975 | + } | |
1976 | + | |
1977 | + if (unlikely(rcu_dereference(target->utrace) != utrace)) { | |
1978 | + /* | |
1979 | + * We lost a race with other CPUs doing a sequence | |
1980 | + * of detach and attach before we got in. | |
1981 | + */ | |
1982 | + spin_unlock(&utrace->lock); | |
1983 | + rcu_read_unlock(); | |
1984 | + kmem_cache_free(utrace_engine_cachep, engine); | |
1985 | + goto restart; | |
1986 | + } | |
1987 | + rcu_read_unlock(); | |
1988 | + | |
1989 | + list_add_tail(&engine->entry, &utrace->attaching); | |
1990 | + utrace->report = 1; | |
1991 | + goto finish; | |
1992 | + | |
1993 | +first: | |
1994 | + utrace = utrace_first_engine(target, engine); | |
1995 | + if (IS_ERR(utrace) || unlikely(utrace == NULL)) { | |
1996 | + kmem_cache_free(utrace_engine_cachep, engine); | |
1997 | + if (unlikely(utrace == NULL)) /* Race condition. */ | |
1998 | + goto restart; | |
1999 | + return ERR_PTR(PTR_ERR(utrace)); | |
2000 | + } | |
2001 | + | |
2002 | +finish: | |
2003 | + engine->ops = ops; | |
2004 | + engine->data = data; | |
2005 | + | |
2006 | + spin_unlock(&utrace->lock); | |
2007 | + | |
2008 | + return engine; | |
2009 | +} | |
2010 | +EXPORT_SYMBOL_GPL(utrace_attach_task); | |
2011 | + | |
2012 | +/** | |
2013 | + * utrace_attach_pid - attach new engine, or look up an attached engine | |
2014 | + * @pid: &struct pid pointer representing thread to attach to | |
2015 | + * @flags: flag bits combined with OR, see utrace_attach_task() | |
2016 | + * @ops: callback table for new engine | |
2017 | + * @data: engine private data pointer | |
2018 | + * | |
2019 | + * This is the same as utrace_attach_task(), but takes a &struct pid | |
2020 | + * pointer rather than a &struct task_struct pointer. The caller must | |
2021 | + * hold a ref on @pid, but does not need to worry about the task | |
2022 | + * staying valid. If it's been reaped so that @pid points nowhere, | |
2023 | + * then this call returns -%ESRCH. | |
2024 | + */ | |
2025 | +struct utrace_attached_engine *utrace_attach_pid( | |
2026 | + struct pid *pid, int flags, | |
2027 | + const struct utrace_engine_ops *ops, void *data) | |
2028 | +{ | |
2029 | + struct utrace_attached_engine *engine = ERR_PTR(-ESRCH); | |
2030 | + struct task_struct *task = get_pid_task(pid, PIDTYPE_PID); | |
2031 | + if (task) { | |
2032 | + engine = utrace_attach_task(task, flags, ops, data); | |
2033 | + put_task_struct(task); | |
2034 | + } | |
2035 | + return engine; | |
2036 | +} | |
2037 | +EXPORT_SYMBOL_GPL(utrace_attach_pid); | |
2038 | + | |
2039 | +/* | |
2040 | + * This is called with @utrace->lock held when the task is safely | |
2041 | + * quiescent, i.e. it won't consult utrace->attached without the lock. | |
2042 | + * Move any engines attached asynchronously from @utrace->attaching | |
2043 | + * onto the @utrace->attached list. | |
2044 | + */ | |
2045 | +static void splice_attaching(struct utrace *utrace) | |
2046 | +{ | |
2047 | + list_splice_tail_init(&utrace->attaching, &utrace->attached); | |
2048 | +} | |
2049 | + | |
2050 | +/* | |
2051 | + * When an engine is detached, the target thread may still see it and | |
2052 | + * make callbacks until it quiesces. We install a special ops vector | |
2053 | + * with these two callbacks. When the target thread quiesces, it can | |
2054 | + * safely free the engine itself. For any event we will always get | |
2055 | + * the report_quiesce() callback first, so we only need this one | |
2056 | + * pointer to be set. The only exception is report_reap(), so we | |
2057 | + * supply that callback too. | |
2058 | + */ | |
2059 | +static u32 utrace_detached_quiesce(enum utrace_resume_action action, | |
2060 | + struct utrace_attached_engine *engine, | |
2061 | + struct task_struct *task, | |
2062 | + unsigned long event) | |
2063 | +{ | |
2064 | + return UTRACE_DETACH; | |
2065 | +} | |
2066 | + | |
2067 | +static void utrace_detached_reap(struct utrace_attached_engine *engine, | |
2068 | + struct task_struct *task) | |
2069 | +{ | |
2070 | +} | |
2071 | + | |
2072 | +static const struct utrace_engine_ops utrace_detached_ops = { | |
2073 | + .report_quiesce = &utrace_detached_quiesce, | |
2074 | + .report_reap = &utrace_detached_reap | |
2075 | +}; | |
2076 | + | |
2077 | +/* | |
2078 | + * Only these flags matter any more for a dead task (exit_state set). | |
2079 | + * We use this mask on flags installed in ->utrace_flags after | |
2080 | + * exit_notify (and possibly utrace_report_death) has run. | |
2081 | + * This ensures that utrace_release_task knows positively that | |
2082 | + * utrace_report_death will not run later. | |
2083 | + */ | |
2084 | +#define DEAD_FLAGS_MASK (UTRACE_EVENT(REAP)) | |
2085 | +#define LIVE_FLAGS_MASK (~0UL) | |
2086 | + | |
2087 | +/* | |
2088 | + * Perform %UTRACE_STOP, i.e. block in TASK_TRACED until woken up. | |
2089 | + * @task == current, @utrace == current->utrace, which is not locked. | |
2090 | + * Return true if we were woken up by SIGKILL even though some utrace | |
2091 | + * engine may still want us to stay stopped. | |
2092 | + */ | |
2093 | +static bool utrace_stop(struct task_struct *task, struct utrace *utrace) | |
2094 | +{ | |
2095 | + /* | |
2096 | + * @utrace->stopped is the flag that says we are safely | |
2097 | + * inside this function. It should never be set on entry. | |
2098 | + */ | |
2099 | + BUG_ON(utrace->stopped); | |
2100 | + | |
2101 | + /* | |
2102 | + * The siglock protects us against signals. As well as SIGKILL | |
2103 | + * waking us up, we must synchronize with the signal bookkeeping | |
2104 | + * for stop signals and SIGCONT. | |
2105 | + */ | |
2106 | + spin_lock(&utrace->lock); | |
2107 | + spin_lock_irq(&task->sighand->siglock); | |
2108 | + | |
2109 | + if (unlikely(sigismember(&task->pending.signal, SIGKILL))) { | |
2110 | + spin_unlock_irq(&task->sighand->siglock); | |
2111 | + spin_unlock(&utrace->lock); | |
2112 | + return true; | |
2113 | + } | |
2114 | + | |
2115 | + utrace->stopped = 1; | |
2116 | + __set_current_state(TASK_TRACED); | |
2117 | + | |
2118 | + /* | |
2119 | + * If there is a group stop in progress, | |
2120 | + * we must participate in the bookkeeping. | |
2121 | + */ | |
2122 | + if (task->signal->group_stop_count > 0) | |
2123 | + --task->signal->group_stop_count; | |
2124 | + | |
2125 | + spin_unlock_irq(&task->sighand->siglock); | |
2126 | + spin_unlock(&utrace->lock); | |
2127 | + | |
2128 | + schedule(); | |
2129 | + | |
2130 | + /* | |
2131 | + * While in TASK_TRACED, we were considered "frozen enough". | |
2132 | + * Now that we woke up, it's crucial if we're supposed to be | |
2133 | + * frozen that we freeze now before running anything substantial. | |
2134 | + */ | |
2135 | + try_to_freeze(); | |
2136 | + | |
2137 | + /* | |
2138 | + * utrace_wakeup() clears @utrace->stopped before waking us up. | |
2139 | + * We're officially awake if it's clear. | |
2140 | + */ | |
2141 | + if (likely(!utrace->stopped)) | |
2142 | + return false; | |
2143 | + | |
2144 | + /* | |
2145 | + * If we're here with it still set, it must have been | |
2146 | + * signal_wake_up() instead, waking us up for a SIGKILL. | |
2147 | + */ | |
2148 | + spin_lock(&utrace->lock); | |
2149 | + utrace->stopped = 0; | |
2150 | + spin_unlock(&utrace->lock); | |
2151 | + return true; | |
2152 | +} | |
2153 | + | |
2154 | +/* | |
2155 | + * The caller has to hold a ref on the engine. If the attached flag is | |
2156 | + * true (all but utrace_barrier() calls), the engine is supposed to be | |
2157 | + * attached. If the attached flag is false (utrace_barrier() only), | |
2158 | + * then return -ERESTARTSYS for an engine marked for detach but not yet | |
2159 | + * fully detached. The task pointer can be invalid if the engine is | |
2160 | + * detached. | |
2161 | + * | |
2162 | + * Get the utrace lock for the target task. | |
2163 | + * Returns the struct if locked, or ERR_PTR(-errno). | |
2164 | + * | |
2165 | + * This has to be robust against races with: | |
2166 | + * utrace_control(target, UTRACE_DETACH) calls | |
2167 | + * UTRACE_DETACH after reports | |
2168 | + * utrace_report_death | |
2169 | + * utrace_release_task | |
2170 | + */ | |
2171 | +static struct utrace *get_utrace_lock(struct task_struct *target, | |
2172 | + struct utrace_attached_engine *engine, | |
2173 | + bool attached) | |
2174 | + __acquires(utrace->lock) | |
2175 | +{ | |
2176 | + struct utrace *utrace; | |
2177 | + | |
2178 | + /* | |
2179 | + * You must hold a ref to be making a call. A call from within | |
2180 | + * a report_* callback in @target might only have the ref for | |
2181 | + * being attached, not a second one of its own. | |
2182 | + */ | |
2183 | + if (unlikely(atomic_read(&engine->kref.refcount) < 1)) | |
2184 | + return ERR_PTR(-EINVAL); | |
2185 | + | |
2186 | + rcu_read_lock(); | |
2187 | + | |
2188 | + /* | |
2189 | + * If this engine was already detached, bail out before we look at | |
2190 | + * the task_struct pointer at all. If it's detached after this | |
2191 | + * check, then RCU is still keeping this task_struct pointer valid. | |
2192 | + * | |
2193 | + * The ops pointer is NULL when the engine is fully detached. | |
2194 | + * It's &utrace_detached_ops when it's marked detached but still | |
2195 | + * on the list. In the latter case, utrace_barrier() still works, | |
2196 | + * since the target might be in the middle of an old callback. | |
2197 | + */ | |
2198 | + if (unlikely(!engine->ops)) { | |
2199 | + rcu_read_unlock(); | |
2200 | + return ERR_PTR(-ESRCH); | |
2201 | + } | |
2202 | + | |
2203 | + if (unlikely(engine->ops == &utrace_detached_ops)) { | |
2204 | + rcu_read_unlock(); | |
2205 | + return attached ? ERR_PTR(-ESRCH) : ERR_PTR(-ERESTARTSYS); | |
2206 | + } | |
2207 | + | |
2208 | + utrace = rcu_dereference(target->utrace); | |
2209 | + smp_rmb(); | |
2210 | + if (unlikely(!utrace) || unlikely(target->exit_state == EXIT_DEAD)) { | |
2211 | + /* | |
2212 | + * If all engines detached already, utrace is clear. | |
2213 | + * Otherwise, we're called after utrace_release_task might | |
2214 | + * have started. A call to this engine's report_reap | |
2215 | + * callback might already be in progress. | |
2216 | + */ | |
2217 | + utrace = ERR_PTR(-ESRCH); | |
2218 | + } else { | |
2219 | + spin_lock(&utrace->lock); | |
2220 | + if (unlikely(rcu_dereference(target->utrace) != utrace) || | |
2221 | + unlikely(!engine->ops) || | |
2222 | + unlikely(engine->ops == &utrace_detached_ops)) { | |
2223 | + /* | |
2224 | + * By the time we got the utrace lock, | |
2225 | + * it had been reaped or detached already. | |
2226 | + */ | |
2227 | + spin_unlock(&utrace->lock); | |
2228 | + utrace = ERR_PTR(-ESRCH); | |
2229 | + if (!attached && engine->ops == &utrace_detached_ops) | |
2230 | + utrace = ERR_PTR(-ERESTARTSYS); | |
2231 | + } | |
2232 | + } | |
2233 | + rcu_read_unlock(); | |
2234 | + | |
2235 | + return utrace; | |
2236 | +} | |
2237 | + | |
2238 | +/* | |
2239 | + * Now that we don't hold any locks, run through any | |
2240 | + * detached engines and free their references. Each | |
2241 | + * engine had one implicit ref while it was attached. | |
2242 | + */ | |
2243 | +static void put_detached_list(struct list_head *list) | |
2244 | +{ | |
2245 | + struct utrace_attached_engine *engine, *next; | |
2246 | + list_for_each_entry_safe(engine, next, list, entry) { | |
2247 | + list_del_init(&engine->entry); | |
2248 | + utrace_engine_put(engine); | |
2249 | + } | |
2250 | +} | |
2251 | + | |
2252 | +/* | |
2253 | + * Called with utrace->lock held. | |
2254 | + * Notify and clean up all engines, then free utrace. | |
2255 | + */ | |
2256 | +static void utrace_reap(struct task_struct *target, struct utrace *utrace) | |
2257 | + __releases(utrace->lock) | |
2258 | +{ | |
2259 | + struct utrace_attached_engine *engine, *next; | |
2260 | + const struct utrace_engine_ops *ops; | |
2261 | + LIST_HEAD(detached); | |
2262 | + | |
2263 | +restart: | |
2264 | + splice_attaching(utrace); | |
2265 | + list_for_each_entry_safe(engine, next, &utrace->attached, entry) { | |
2266 | + ops = engine->ops; | |
2267 | + engine->ops = NULL; | |
2268 | + list_move(&engine->entry, &detached); | |
2269 | + | |
2270 | + /* | |
2271 | + * If it didn't need a callback, we don't need to drop | |
2272 | + * the lock. Now nothing else refers to this engine. | |
2273 | + */ | |
2274 | + if (!(engine->flags & UTRACE_EVENT(REAP))) | |
2275 | + continue; | |
2276 | + | |
2277 | + utrace->reporting = engine; | |
2278 | + spin_unlock(&utrace->lock); | |
2279 | + | |
2280 | + (*ops->report_reap)(engine, target); | |
2281 | + | |
2282 | + utrace->reporting = NULL; | |
2283 | + | |
2284 | + put_detached_list(&detached); | |
2285 | + | |
2286 | + spin_lock(&utrace->lock); | |
2287 | + goto restart; | |
2288 | + } | |
2289 | + | |
2290 | + rcu_utrace_free(utrace); /* Releases the lock. */ | |
2291 | + | |
2292 | + put_detached_list(&detached); | |
2293 | +} | |
2294 | + | |
2295 | +#define DEATH_EVENTS (UTRACE_EVENT(DEATH) | UTRACE_EVENT(QUIESCE)) | |
2296 | + | |
2297 | +/* | |
2298 | + * Called by release_task. After this, target->utrace must be cleared. | |
2299 | + */ | |
2300 | +void utrace_release_task(struct task_struct *target) | |
2301 | +{ | |
2302 | + struct utrace *utrace; | |
2303 | + | |
2304 | + task_lock(target); | |
2305 | + utrace = rcu_dereference(target->utrace); | |
2306 | + rcu_assign_pointer(target->utrace, NULL); | |
2307 | + task_unlock(target); | |
2308 | + | |
2309 | + if (unlikely(utrace == NULL)) | |
2310 | + return; | |
2311 | + | |
2312 | + spin_lock(&utrace->lock); | |
2313 | + /* | |
2314 | + * If the list is empty, utrace is already on its way to be freed. | |
2315 | + * We raced with detach and we won the task_lock race but lost the | |
2316 | + * utrace->lock race. All we have to do is let RCU run. | |
2317 | + */ | |
2318 | + if (likely(!list_empty(&utrace->attached))) { | |
2319 | + utrace->reap = 1; | |
2320 | + | |
2321 | + if (!(target->utrace_flags & DEATH_EVENTS)) { | |
2322 | + utrace_reap(target, utrace); /* Unlocks and frees. */ | |
2323 | + return; | |
2324 | + } | |
2325 | + | |
2326 | + /* | |
2327 | + * The target will do some final callbacks but hasn't | |
2328 | + * finished them yet. We know because it clears these | |
2329 | + * event bits after it's done. Instead of cleaning up here | |
2330 | + * and requiring utrace_report_death to cope with it, we | |
2331 | + * delay the REAP report and the teardown until after the | |
2332 | + * target finishes its death reports. | |
2333 | + */ | |
2334 | + } | |
2335 | + spin_unlock(&utrace->lock); | |
2336 | +} | |
2337 | + | |
2338 | +/* | |
2339 | + * We use an extra bit in utrace_attached_engine.flags past the event bits, | |
2340 | + * to record whether the engine is keeping the target thread stopped. | |
2341 | + */ | |
2342 | +#define ENGINE_STOP (1UL << _UTRACE_NEVENTS) | |
2343 | + | |
2344 | +static void mark_engine_wants_stop(struct utrace_attached_engine *engine) | |
2345 | +{ | |
2346 | + engine->flags |= ENGINE_STOP; | |
2347 | +} | |
2348 | + | |
2349 | +static void clear_engine_wants_stop(struct utrace_attached_engine *engine) | |
2350 | +{ | |
2351 | + engine->flags &= ~ENGINE_STOP; | |
2352 | +} | |
2353 | + | |
2354 | +static bool engine_wants_stop(struct utrace_attached_engine *engine) | |
2355 | +{ | |
2356 | + return (engine->flags & ENGINE_STOP) != 0; | |
2357 | +} | |
2358 | + | |
2359 | +/** | |
2360 | + * utrace_set_events - choose which event reports a tracing engine gets | |
2361 | + * @target: thread to affect | |
2362 | + * @engine: attached engine to affect | |
2363 | + * @events: new event mask | |
2364 | + * | |
2365 | + * This changes the set of events for which @engine wants callbacks made. | |
2366 | + * | |
2367 | + * This fails with -%EALREADY and does nothing if you try to clear | |
2368 | + * %UTRACE_EVENT(%DEATH) when the @report_death callback may already have | |
2369 | + * begun, if you try to clear %UTRACE_EVENT(%REAP) when the @report_reap | |
2370 | + * callback may already have begun, or if you try to newly set | |
2371 | + * %UTRACE_EVENT(%DEATH) or %UTRACE_EVENT(%QUIESCE) when @target is | |
2372 | + * already dead or dying. | |
2373 | + * | |
2374 | + * This can fail with -%ESRCH when @target has already been detached, | |
2375 | + * including forcible detach on reaping. | |
2376 | + * | |
2377 | + * If @target was stopped before the call, then after a successful call, | |
2378 | + * no event callbacks not requested in @events will be made; if | |
2379 | + * %UTRACE_EVENT(%QUIESCE) is included in @events, then a @report_quiesce | |
2380 | + * callback will be made when @target resumes. If @target was not stopped, | |
2381 | + * and was about to make a callback to @engine, this returns -%EINPROGRESS. | |
2382 | + * In this case, the callback in progress might be one excluded from the | |
2383 | + * new @events setting. When this returns zero, you can be sure that no | |
2384 | + * event callbacks you've disabled in @events can be made. | |
2385 | + * | |
2386 | + * To synchronize after an -%EINPROGRESS return, see utrace_barrier(). | |
2387 | + * | |
2388 | + * These rules provide for coherent synchronization based on %UTRACE_STOP, | |
2389 | + * even when %SIGKILL is breaking its normal simple rules. | |
2390 | + */ | |
2391 | +int utrace_set_events(struct task_struct *target, | |
2392 | + struct utrace_attached_engine *engine, | |
2393 | + unsigned long events) | |
2394 | +{ | |
2395 | + struct utrace *utrace; | |
2396 | + unsigned long old_flags, old_utrace_flags, set_utrace_flags; | |
2397 | + struct sighand_struct *sighand; | |
2398 | + unsigned long flags; | |
2399 | + int ret; | |
2400 | + | |
2401 | + utrace = get_utrace_lock(target, engine, true); | |
2402 | + if (unlikely(IS_ERR(utrace))) | |
2403 | + return PTR_ERR(utrace); | |
2404 | + | |
2405 | + old_utrace_flags = target->utrace_flags; | |
2406 | + set_utrace_flags = events; | |
2407 | + old_flags = engine->flags; | |
2408 | + | |
2409 | + if (target->exit_state && | |
2410 | + (((events & ~old_flags) & DEATH_EVENTS) || | |
2411 | + (utrace->death && ((old_flags & ~events) & DEATH_EVENTS)) || | |
2412 | + (utrace->reap && ((old_flags & ~events) & UTRACE_EVENT(REAP))))) { | |
2413 | + spin_unlock(&utrace->lock); | |
2414 | + return -EALREADY; | |
2415 | + } | |
2416 | + | |
2417 | + /* | |
2418 | + * When it's in TASK_STOPPED state and UTRACE_EVENT(JCTL) is set, | |
2419 | + * utrace_do_stop() will think it is still running and needs to | |
2420 | + * finish utrace_report_jctl() before it's really stopped. But | |
2421 | + * if the bit wasn't already set, it can't be running in there | |
2422 | + * and really is quiescent now in its existing job control stop. | |
2423 | + */ | |
2424 | + if (!utrace->stopped && | |
2425 | + ((set_utrace_flags & ~old_utrace_flags) & UTRACE_EVENT(JCTL))) { | |
2426 | + sighand = lock_task_sighand(target, &flags); | |
2427 | + if (likely(sighand)) { | |
2428 | + if (task_is_stopped(target)) | |
2429 | + utrace->stopped = 1; | |
2430 | + unlock_task_sighand(target, &flags); | |
2431 | + } | |
2432 | + } | |
2433 | + | |
2434 | + /* | |
2435 | + * When setting these flags, it's essential that we really | |
2436 | + * synchronize with exit_notify(). They cannot be set after | |
2437 | + * exit_notify() takes the tasklist_lock. By holding the read | |
2438 | + * lock here while setting the flags, we ensure that the calls | |
2439 | + * to tracehook_notify_death() and tracehook_report_death() will | |
2440 | + * see the new flags. This ensures that utrace_release_task() | |
2441 | + * knows positively that utrace_report_death() will be called or | |
2442 | + * that it won't. | |
2443 | + */ | |
2444 | + if ((set_utrace_flags & ~old_utrace_flags) & DEATH_EVENTS) { | |
2445 | + read_lock(&tasklist_lock); | |
2446 | + if (unlikely(target->exit_state)) { | |
2447 | + read_unlock(&tasklist_lock); | |
2448 | + spin_unlock(&utrace->lock); | |
2449 | + return -EALREADY; | |
2450 | + } | |
2451 | + target->utrace_flags |= set_utrace_flags; | |
2452 | + read_unlock(&tasklist_lock); | |
2453 | + } | |
2454 | + | |
2455 | + engine->flags = events | (engine->flags & ENGINE_STOP); | |
2456 | + target->utrace_flags |= set_utrace_flags; | |
2457 | + | |
2458 | + if ((set_utrace_flags & UTRACE_EVENT_SYSCALL) && | |
2459 | + !(old_utrace_flags & UTRACE_EVENT_SYSCALL)) | |
2460 | + set_tsk_thread_flag(target, TIF_SYSCALL_TRACE); | |
2461 | + | |
2462 | + ret = 0; | |
2463 | + if (!utrace->stopped && target != current) { | |
2464 | + smp_mb(); | |
2465 | + if (utrace->reporting == engine) | |
2466 | + ret = -EINPROGRESS; | |
2467 | + } | |
2468 | + | |
2469 | + spin_unlock(&utrace->lock); | |
2470 | + | |
2471 | + return ret; | |
2472 | +} | |
2473 | +EXPORT_SYMBOL_GPL(utrace_set_events); | |
2474 | + | |
2475 | +/* | |
2476 | + * Asynchronously mark an engine as being detached. | |
2477 | + * | |
2478 | + * This must work while the target thread races with us doing | |
2479 | + * start_callback(), defined below. It uses smp_rmb() between checking | |
2480 | + * @engine->flags and using @engine->ops. Here we change @engine->ops | |
2481 | + * first, then use smp_wmb() before changing @engine->flags. This ensures | |
2482 | + * it can check the old flags before using the old ops, or check the old | |
2483 | + * flags before using the new ops, or check the new flags before using the | |
2484 | + * new ops, but can never check the new flags before using the old ops. | |
2485 | + * Hence, utrace_detached_ops might be used with any old flags in place. | |
2486 | + * It has report_quiesce() and report_reap() callbacks to handle all cases. | |
2487 | + */ | |
2488 | +static void mark_engine_detached(struct utrace_attached_engine *engine) | |
2489 | +{ | |
2490 | + engine->ops = &utrace_detached_ops; | |
2491 | + smp_wmb(); | |
2492 | + engine->flags = UTRACE_EVENT(QUIESCE); | |
2493 | +} | |
2494 | + | |
2495 | +/* | |
2496 | + * Get @target to stop and return true if it is already stopped now. | |
2497 | + * If we return false, it will make some event callback soonish. | |
2498 | + * Called with @utrace locked. | |
2499 | + */ | |
2500 | +static bool utrace_do_stop(struct task_struct *target, struct utrace *utrace) | |
2501 | +{ | |
2502 | + bool stopped; | |
2503 | + | |
2504 | + /* | |
2505 | + * If it will call utrace_report_jctl() but has not gotten | |
2506 | + * through it yet, then don't consider it quiescent yet. | |
2507 | + * utrace_report_jctl() will take @utrace->lock and | |
2508 | + * set @utrace->stopped itself once it finishes. After that, | |
2509 | + * it is considered quiescent; when it wakes up, it will go | |
2510 | + * through utrace_get_signal() before doing anything else. | |
2511 | + */ | |
2512 | + if (task_is_stopped(target) && | |
2513 | + !(target->utrace_flags & UTRACE_EVENT(JCTL))) { | |
2514 | + utrace->stopped = 1; | |
2515 | + return true; | |
2516 | + } | |
2517 | + | |
2518 | + stopped = false; | |
2519 | + spin_lock_irq(&target->sighand->siglock); | |
2520 | + if (unlikely(target->exit_state)) { | |
2521 | + /* | |
2522 | + * On the exit path, it's only truly quiescent | |
2523 | + * if it has already been through | |
2524 | + * utrace_report_death(), or never will. | |
2525 | + */ | |
2526 | + if (!(target->utrace_flags & DEATH_EVENTS)) | |
2527 | + utrace->stopped = stopped = true; | |
2528 | + } else if (task_is_stopped(target)) { | |
2529 | + if (!(target->utrace_flags & UTRACE_EVENT(JCTL))) | |
2530 | + utrace->stopped = stopped = true; | |
2531 | + } else if (!utrace->report && !utrace->interrupt) { | |
2532 | + utrace->report = 1; | |
2533 | + set_notify_resume(target); | |
2534 | + } | |
2535 | + spin_unlock_irq(&target->sighand->siglock); | |
2536 | + | |
2537 | + return stopped; | |
2538 | +} | |
2539 | + | |
2540 | +/* | |
2541 | + * If the target is not dead it should not be in tracing | |
2542 | + * stop any more. Wake it unless it's in job control stop. | |
2543 | + * | |
2544 | + * Called with @utrace->lock held and @utrace->stopped set. | |
2545 | + */ | |
2546 | +static void utrace_wakeup(struct task_struct *target, struct utrace *utrace) | |
2547 | +{ | |
2548 | + struct sighand_struct *sighand; | |
2549 | + unsigned long irqflags; | |
2550 | + | |
2551 | + utrace->stopped = 0; | |
2552 | + | |
2553 | + sighand = lock_task_sighand(target, &irqflags); | |
2554 | + if (unlikely(!sighand)) | |
2555 | + return; | |
2556 | + | |
2557 | + if (likely(task_is_stopped_or_traced(target))) { | |
2558 | + if (target->signal->flags & SIGNAL_STOP_STOPPED) | |
2559 | + target->state = TASK_STOPPED; | |
2560 | + else | |
2561 | + wake_up_state(target, __TASK_STOPPED | __TASK_TRACED); | |
2562 | + } | |
2563 | + | |
2564 | + unlock_task_sighand(target, &irqflags); | |
2565 | +} | |
2566 | + | |
2567 | +/* | |
2568 | + * This is called when there might be some detached engines on the list or | |
2569 | + * some stale bits in @task->utrace_flags. Clean them up and recompute the | |
2570 | + * flags. | |
2571 | + * | |
2572 | + * @wake is false when @task is current. @wake is true when @task is | |
2573 | + * stopped and @utrace->stopped is set; wake it up if it should not be. | |
2574 | + * | |
2575 | + * Called with @utrace->lock held, returns with it released. | |
2576 | + */ | |
2577 | +static void utrace_reset(struct task_struct *task, struct utrace *utrace, | |
2578 | + bool wake) | |
2579 | + __releases(utrace->lock) | |
2580 | +{ | |
2581 | + struct utrace_attached_engine *engine, *next; | |
2582 | + unsigned long flags = 0; | |
2583 | + LIST_HEAD(detached); | |
2584 | + | |
2585 | + splice_attaching(utrace); | |
2586 | + | |
2587 | + /* | |
2588 | + * Update the set of events of interest from the union | |
2589 | + * of the interests of the remaining tracing engines. | |
2590 | + * For any engine marked detached, remove it from the list. | |
2591 | + * We'll collect them on the detached list. | |
2592 | + */ | |
2593 | + list_for_each_entry_safe(engine, next, &utrace->attached, entry) { | |
2594 | + if (engine->ops == &utrace_detached_ops) { | |
2595 | + engine->ops = NULL; | |
2596 | + list_move(&engine->entry, &detached); | |
2597 | + } else { | |
2598 | + flags |= engine->flags | UTRACE_EVENT(REAP); | |
2599 | + wake = wake && !engine_wants_stop(engine); | |
2600 | + } | |
2601 | + } | |
2602 | + | |
2603 | + if (task->exit_state) { | |
2604 | + BUG_ON(utrace->death); | |
2605 | + flags &= DEAD_FLAGS_MASK; | |
2606 | + wake = false; | |
2607 | + } else if (!(flags & UTRACE_EVENT_SYSCALL) && | |
2608 | + test_tsk_thread_flag(task, TIF_SYSCALL_TRACE)) { | |
2609 | + clear_tsk_thread_flag(task, TIF_SYSCALL_TRACE); | |
2610 | + } | |
2611 | + | |
2612 | + task->utrace_flags = flags; | |
2613 | + | |
2614 | + if (wake) | |
2615 | + utrace_wakeup(task, utrace); | |
2616 | + | |
2617 | + /* | |
2618 | + * If any engines are left, we're done. | |
2619 | + */ | |
2620 | + if (flags) { | |
2621 | + spin_unlock(&utrace->lock); | |
2622 | + goto done; | |
2623 | + } | |
2624 | + | |
2625 | + /* | |
2626 | + * No more engines, clear out the utrace. Here we can race with | |
2627 | + * utrace_release_task(). If it gets task_lock() first, then it | |
2628 | + * cleans up this struct for us. | |
2629 | + */ | |
2630 | + | |
2631 | + task_lock(task); | |
2632 | + | |
2633 | + if (unlikely(task->utrace != utrace)) { | |
2634 | + task_unlock(task); | |
2635 | + spin_unlock(&utrace->lock); | |
2636 | + goto done; | |
2637 | + } | |
2638 | + | |
2639 | + rcu_assign_pointer(task->utrace, NULL); | |
2640 | + | |
2641 | + task_unlock(task); | |
2642 | + | |
2643 | + rcu_utrace_free(utrace); | |
2644 | + | |
2645 | +done: | |
2646 | + put_detached_list(&detached); | |
2647 | +} | |
2648 | + | |
2649 | +/** | |
2650 | + * utrace_control - control a thread being traced by a tracing engine | |
2651 | + * @target: thread to affect | |
2652 | + * @engine: attached engine to affect | |
2653 | + * @action: &enum utrace_resume_action for thread to do | |
2654 | + * | |
2655 | + * This is how a tracing engine asks a traced thread to do something. | |
2656 | + * This call is controlled by the @action argument, which has the | |
2657 | + * same meaning as the &enum utrace_resume_action value returned by | |
2658 | + * event reporting callbacks. | |
2659 | + * | |
2660 | + * If @target is already dead (@target->exit_state nonzero), | |
2661 | + * all actions except %UTRACE_DETACH fail with -%ESRCH. | |
2662 | + * | |
2663 | + * The following sections describe each option for the @action argument. | |
2664 | + * | |
2665 | + * UTRACE_DETACH: | |
2666 | + * | |
2667 | + * After this, the @engine data structure is no longer accessible, | |
2668 | + * and the thread might be reaped. The thread will start running | |
2669 | + * again if it was stopped and no longer has any attached engines | |
2670 | + * that want it stopped. | |
2671 | + * | |
2672 | + * If the @report_reap callback may already have begun, this fails | |
2673 | + * with -%ESRCH. If the @report_death callback may already have | |
2674 | + * begun, this fails with -%EALREADY. | |
2675 | + * | |
2676 | + * If @target is not already stopped, then a callback to this engine | |
2677 | + * might be in progress or about to start on another CPU. If so, | |
2678 | + * then this returns -%EINPROGRESS; the detach happens as soon as | |
2679 | + * the pending callback is finished. To synchronize after an | |
2680 | + * -%EINPROGRESS return, see utrace_barrier(). | |
2681 | + * | |
2682 | + * If @target is properly stopped before utrace_control() is called, | |
2683 | + * then after successful return it's guaranteed that no more callbacks | |
2684 | + * to the @engine->ops vector will be made. | |
2685 | + * | |
2686 | + * The only exception is %SIGKILL (and exec or group-exit by another | |
2687 | + * thread in the group), which can cause asynchronous @report_death | |
2688 | + * and/or @report_reap callbacks even when %UTRACE_STOP was used. | |
2689 | + * (In that event, this fails with -%ESRCH or -%EALREADY, see above.) | |
2690 | + * | |
2691 | + * UTRACE_STOP: | |
2692 | + * This asks that @target stop running. This returns 0 only if | |
2693 | + * @target is already stopped, either for tracing or for job | |
2694 | + * control. Then @target will remain stopped until another | |
2695 | + * utrace_control() call is made on @engine; @target can be woken | |
2696 | + * only by %SIGKILL (or equivalent, such as exec or termination by | |
2697 | + * another thread in the same thread group). | |
2698 | + * | |
2699 | + * This returns -%EINPROGRESS if @target is not already stopped. | |
2700 | + * Then the effect is like %UTRACE_REPORT. A @report_quiesce or | |
2701 | + * @report_signal callback will be made soon. Your callback can | |
2702 | + * then return %UTRACE_STOP to keep @target stopped. | |
2703 | + * | |
2704 | + * This does not interrupt system calls in progress, including ones | |
2705 | + * that sleep for a long time. For that, use %UTRACE_INTERRUPT. | |
2706 | + * To interrupt system calls and then keep @target stopped, your | |
2707 | + * @report_signal callback can return %UTRACE_STOP. | |
2708 | + * | |
2709 | + * UTRACE_RESUME: | |
2710 | + * | |
2711 | + * Just let @target continue running normally, reversing the effect | |
2712 | + * of a previous %UTRACE_STOP. If another engine is keeping @target | |
2713 | + * stopped, then it remains stopped until all engines let it resume. | |
2714 | + * If @target was not stopped, this has no effect. | |
2715 | + * | |
2716 | + * UTRACE_REPORT: | |
2717 | + * | |
2718 | + * This is like %UTRACE_RESUME, but also ensures that there will be | |
2719 | + * a @report_quiesce or @report_signal callback made soon. If | |
2720 | + * @target had been stopped, then there will be a callback before it | |
2721 | + * resumes running normally. If another engine is keeping @target | |
2722 | + * stopped, then there might be no callbacks until all engines let | |
2723 | + * it resume. | |
2724 | + * | |
2725 | + * UTRACE_INTERRUPT: | |
2726 | + * | |
2727 | + * This is like %UTRACE_REPORT, but ensures that @target will make a | |
2728 | + * @report_signal callback before it resumes or delivers signals. | |
2729 | + * If @target was in a system call or about to enter one, work in | |
2730 | + * progress will be interrupted as if by %SIGSTOP. If another | |
2731 | + * engine is keeping @target stopped, then there might be no | |
2732 | + * callbacks until all engines let it resume. | |
2733 | + * | |
2734 | + * This gives @engine an opportunity to introduce a forced signal | |
2735 | + * disposition via its @report_signal callback. | |
2736 | + * | |
2737 | + * UTRACE_SINGLESTEP: | |
2738 | + * | |
2739 | + * It's invalid to use this unless arch_has_single_step() returned true. | |
2740 | + * This is like %UTRACE_RESUME, but resumes for one user instruction | |
2741 | + * only. It's invalid to use this in utrace_control() unless @target | |
2742 | + * had been stopped by @engine previously. | |
2743 | + * | |
2744 | + * Note that passing %UTRACE_SINGLESTEP or %UTRACE_BLOCKSTEP to | |
2745 | + * utrace_control() or returning it from an event callback alone does | |
2746 | + * not necessarily ensure that stepping will be enabled. If there are | |
2747 | + * more callbacks made to any engine before returning to user mode, | |
2748 | + * then the resume action is chosen only by the last set of callbacks. | |
2749 | + * To be sure, enable %UTRACE_EVENT(%QUIESCE) and look for the | |
2750 | + * @report_quiesce callback with a zero event mask, or the | |
2751 | + * @report_signal callback with %UTRACE_SIGNAL_REPORT. | |
2752 | + * | |
2753 | + * UTRACE_BLOCKSTEP: | |
2754 | + * | |
2755 | + * It's invalid to use this unless arch_has_block_step() returned true. | |
2756 | + * This is like %UTRACE_SINGLESTEP, but resumes for one whole basic | |
2757 | + * block of user instructions. | |
2758 | + * | |
2759 | + * %UTRACE_BLOCKSTEP devolves to %UTRACE_SINGLESTEP when another | |
2760 | + * tracing engine is using %UTRACE_SINGLESTEP at the same time. | |
2761 | + */ | |
2762 | +int utrace_control(struct task_struct *target, | |
2763 | + struct utrace_attached_engine *engine, | |
2764 | + enum utrace_resume_action action) | |
2765 | +{ | |
2766 | + struct utrace *utrace; | |
2767 | + bool resume; | |
2768 | + int ret; | |
2769 | + | |
2770 | + if (unlikely(action > UTRACE_DETACH)) | |
2771 | + return -EINVAL; | |
2772 | + | |
2773 | + utrace = get_utrace_lock(target, engine, true); | |
2774 | + if (unlikely(IS_ERR(utrace))) | |
2775 | + return PTR_ERR(utrace); | |
2776 | + | |
2777 | + if (target->exit_state) { | |
2778 | + /* | |
2779 | + * You can't do anything to a dead task but detach it. | |
2780 | + * If release_task() has been called, you can't do that. | |
2781 | + * | |
2782 | + * On the exit path, DEATH and QUIESCE event bits are | |
2783 | + * set only before utrace_report_death() has taken the | |
2784 | + * lock. At that point, the death report will come | |
2785 | + * soon, so disallow detach until it's done. This | |
2786 | + * prevents us from racing with it detaching itself. | |
2787 | + */ | |
2788 | + if (action != UTRACE_DETACH || | |
2789 | + unlikely(utrace->reap)) { | |
2790 | + spin_unlock(&utrace->lock); | |
2791 | + return -ESRCH; | |
2792 | + } else if (unlikely(target->utrace_flags & DEATH_EVENTS) || | |
2793 | + unlikely(utrace->death)) { | |
2794 | + /* | |
2795 | + * We have already started the death report, or | |
2796 | + * are about to very soon. We can't prevent | |
2797 | + * the report_death and report_reap callbacks, | |
2798 | + * so tell the caller they will happen. | |
2799 | + */ | |
2800 | + spin_unlock(&utrace->lock); | |
2801 | + return -EALREADY; | |
2802 | + } | |
2803 | + } | |
2804 | + | |
2805 | + resume = utrace->stopped; | |
2806 | + ret = 0; | |
2807 | + | |
2808 | + clear_engine_wants_stop(engine); | |
2809 | + switch (action) { | |
2810 | + case UTRACE_STOP: | |
2811 | + mark_engine_wants_stop(engine); | |
2812 | + if (!resume && !utrace_do_stop(target, utrace)) | |
2813 | + ret = -EINPROGRESS; | |
2814 | + resume = false; | |
2815 | + break; | |
2816 | + | |
2817 | + case UTRACE_DETACH: | |
2818 | + mark_engine_detached(engine); | |
2819 | + resume = resume || utrace_do_stop(target, utrace); | |
2820 | + if (!resume) { | |
2821 | + smp_mb(); | |
2822 | + if (utrace->reporting == engine) | |
2823 | + ret = -EINPROGRESS; | |
2824 | + break; | |
2825 | + } | |
2826 | + /* Fall through. */ | |
2827 | + | |
2828 | + case UTRACE_RESUME: | |
2829 | + /* | |
2830 | + * This and all other cases imply resuming if stopped. | |
2831 | + * There might not be another report before it just | |
2832 | + * resumes, so make sure single-step is not left set. | |
2833 | + */ | |
2834 | + if (likely(resume)) | |
2835 | + user_disable_single_step(target); | |
2836 | + break; | |
2837 | + | |
2838 | + case UTRACE_REPORT: | |
2839 | + /* | |
2840 | + * Make the thread call tracehook_notify_resume() soon. | |
2841 | + * But don't bother if it's already been stopped or | |
2842 | + * interrupted. In those cases, utrace_get_signal() | |
2843 | + * will be reporting soon. | |
2844 | + */ | |
2845 | + if (!utrace->report && !utrace->interrupt && !utrace->stopped) { | |
2846 | + utrace->report = 1; | |
2847 | + set_notify_resume(target); | |
2848 | + } | |
2849 | + break; | |
2850 | + | |
2851 | + case UTRACE_INTERRUPT: | |
2852 | + /* | |
2853 | + * Make the thread call tracehook_get_signal() soon. | |
2854 | + */ | |
2855 | + if (utrace->interrupt) | |
2856 | + break; | |
2857 | + utrace->interrupt = 1; | |
2858 | + | |
2859 | + /* | |
2860 | + * If it's not already stopped, interrupt it now. | |
2861 | + * We need the siglock here in case it calls | |
2862 | + * recalc_sigpending() and clears its own | |
2863 | + * TIF_SIGPENDING. By taking the lock, we've | |
2864 | + * serialized any later recalc_sigpending() after | |
2865 | + * our setting of utrace->interrupt to force it on. | |
2866 | + */ | |
2867 | + if (resume) { | |
2868 | + /* | |
2869 | + * This is really just to keep the invariant | |
2870 | + * that TIF_SIGPENDING is set with utrace->interrupt. | |
2871 | + * When it's stopped, we know it's always going | |
2872 | + * through utrace_get_signal and will recalculate. | |
2873 | + */ | |
2874 | + set_tsk_thread_flag(target, TIF_SIGPENDING); | |
2875 | + } else { | |
2876 | + struct sighand_struct *sighand; | |
2877 | + unsigned long irqflags; | |
2878 | + sighand = lock_task_sighand(target, &irqflags); | |
2879 | + if (likely(sighand)) { | |
2880 | + signal_wake_up(target, 0); | |
2881 | + unlock_task_sighand(target, &irqflags); | |
2882 | + } | |
2883 | + } | |
2884 | + break; | |
2885 | + | |
2886 | + case UTRACE_BLOCKSTEP: | |
2887 | + /* | |
2888 | + * Resume from stopped, step one block. | |
2889 | + */ | |
2890 | + if (unlikely(!arch_has_block_step())) { | |
2891 | + WARN_ON(1); | |
2892 | + /* Fall through to treat it as SINGLESTEP. */ | |
2893 | + } else if (likely(resume)) { | |
2894 | + user_enable_block_step(target); | |
2895 | + break; | |
2896 | + } | |
2897 | + | |
2898 | + case UTRACE_SINGLESTEP: | |
2899 | + /* | |
2900 | + * Resume from stopped, step one instruction. | |
2901 | + */ | |
2902 | + if (unlikely(!arch_has_single_step())) { | |
2903 | + WARN_ON(1); | |
2904 | + resume = false; | |
2905 | + ret = -EOPNOTSUPP; | |
2906 | + break; | |
2907 | + } | |
2908 | + | |
2909 | + if (likely(resume)) | |
2910 | + user_enable_single_step(target); | |
2911 | + else | |
2912 | + /* | |
2913 | + * You were supposed to stop it before asking | |
2914 | + * it to step. | |
2915 | + */ | |
2916 | + ret = -EAGAIN; | |
2917 | + break; | |
2918 | + } | |
2919 | + | |
2920 | + /* | |
2921 | + * Let the thread resume running. If it's not stopped now, | |
2922 | + * there is nothing more we need to do. | |
2923 | + */ | |
2924 | + if (resume) | |
2925 | + utrace_reset(target, utrace, true); | |
2926 | + else | |
2927 | + spin_unlock(&utrace->lock); | |
2928 | + | |
2929 | + return ret; | |
2930 | +} | |
2931 | +EXPORT_SYMBOL_GPL(utrace_control); | |
2932 | + | |
2933 | +/** | |
2934 | + * utrace_barrier - synchronize with simultaneous tracing callbacks | |
2935 | + * @target: thread to affect | |
2936 | + * @engine: engine to affect (can be detached) | |
2937 | + * | |
2938 | + * This blocks while @target might be in the midst of making a callback to | |
2939 | + * @engine. It can be interrupted by signals and will return -%ERESTARTSYS. | |
2940 | + * A return value of zero means no callback from @target to @engine was | |
2941 | + * in progress. | |
2942 | + * | |
2943 | + * It's not necessary to keep the @target pointer alive for this call. | |
2944 | + * It's only necessary to hold a ref on @engine. This will return | |
2945 | + * safely even if @target has been reaped and has no task refs. | |
2946 | + * | |
2947 | + * A successful return from utrace_barrier() guarantees its ordering | |
2948 | + * with respect to utrace_set_events() and utrace_control() calls. If | |
2949 | + * @target was not properly stopped, event callbacks just disabled might | |
2950 | + * still be in progress; utrace_barrier() waits until there is no chance | |
2951 | + * an unwanted callback can be in progress. | |
2952 | + */ | |
2953 | +int utrace_barrier(struct task_struct *target, | |
2954 | + struct utrace_attached_engine *engine) | |
2955 | +{ | |
2956 | + struct utrace *utrace; | |
2957 | + int ret = -ERESTARTSYS; | |
2958 | + | |
2959 | + if (unlikely(target == current)) | |
2960 | + return 0; | |
2961 | + | |
2962 | + do { | |
2963 | + utrace = get_utrace_lock(target, engine, false); | |
2964 | + if (unlikely(IS_ERR(utrace))) { | |
2965 | + ret = PTR_ERR(utrace); | |
2966 | + if (ret != -ERESTARTSYS) | |
2967 | + break; | |
2968 | + } else { | |
2969 | + if (utrace->stopped || utrace->reporting != engine) | |
2970 | + ret = 0; | |
2971 | + spin_unlock(&utrace->lock); | |
2972 | + if (!ret) | |
2973 | + break; | |
2974 | + } | |
2975 | + schedule_timeout_interruptible(1); | |
2976 | + } while (!signal_pending(current)); | |
2977 | + | |
2978 | + return ret; | |
2979 | +} | |
2980 | +EXPORT_SYMBOL_GPL(utrace_barrier); | |
2981 | + | |
2982 | +/* | |
2983 | + * This is local state used for reporting loops, perhaps optimized away. | |
2984 | + */ | |
2985 | +struct utrace_report { | |
2986 | + enum utrace_resume_action action; | |
2987 | + u32 result; | |
2988 | + bool detaches; | |
2989 | + bool takers; | |
2990 | + bool killed; | |
2991 | +}; | |
2992 | + | |
2993 | +#define INIT_REPORT(var) \ | |
2994 | + struct utrace_report var = { UTRACE_RESUME, 0, false, false, false } | |
2995 | + | |
2996 | +/* | |
2997 | + * We are now making the report, so clear the flag saying we need one. | |
2998 | + */ | |
2999 | +static void start_report(struct utrace *utrace) | |
3000 | +{ | |
3001 | + BUG_ON(utrace->stopped); | |
3002 | + if (utrace->report) { | |
3003 | + spin_lock(&utrace->lock); | |
3004 | + utrace->report = 0; | |
3005 | + splice_attaching(utrace); | |
3006 | + spin_unlock(&utrace->lock); | |
3007 | + } | |
3008 | +} | |
3009 | + | |
3010 | +/* | |
3011 | + * Complete a normal reporting pass, pairing with a start_report() call. | |
3012 | + * This handles any UTRACE_DETACH or UTRACE_REPORT or UTRACE_INTERRUPT | |
3013 | + * returns from engine callbacks. If any engine's last callback used | |
3014 | + * UTRACE_STOP, we do UTRACE_REPORT here to ensure we stop before user | |
3015 | + * mode. If there were no callbacks made, it will recompute | |
3016 | + * @task->utrace_flags to avoid another false-positive. | |
3017 | + */ | |
3018 | +static void finish_report(struct utrace_report *report, | |
3019 | + struct task_struct *task, struct utrace *utrace) | |
3020 | +{ | |
3021 | + bool clean = (report->takers && !report->detaches); | |
3022 | + | |
3023 | + if (report->action <= UTRACE_REPORT && !utrace->report) { | |
3024 | + spin_lock(&utrace->lock); | |
3025 | + utrace->report = 1; | |
3026 | + set_tsk_thread_flag(task, TIF_NOTIFY_RESUME); | |
3027 | + } else if (report->action == UTRACE_INTERRUPT && !utrace->interrupt) { | |
3028 | + spin_lock(&utrace->lock); | |
3029 | + utrace->interrupt = 1; | |
3030 | + set_tsk_thread_flag(task, TIF_SIGPENDING); | |
3031 | + } else if (clean) { | |
3032 | + return; | |
3033 | + } else { | |
3034 | + spin_lock(&utrace->lock); | |
3035 | + } | |
3036 | + | |
3037 | + if (clean) | |
3038 | + spin_unlock(&utrace->lock); | |
3039 | + else | |
3040 | + utrace_reset(task, utrace, false); | |
3041 | +} | |
3042 | + | |
3043 | +/* | |
3044 | + * Apply the return value of one engine callback to @report. | |
3045 | + * Returns true if @engine detached and should not get any more callbacks. | |
3046 | + */ | |
3047 | +static bool finish_callback(struct utrace *utrace, | |
3048 | + struct utrace_report *report, | |
3049 | + struct utrace_attached_engine *engine, | |
3050 | + u32 ret) | |
3051 | +{ | |
3052 | + enum utrace_resume_action action = utrace_resume_action(ret); | |
3053 | + | |
3054 | + utrace->reporting = NULL; | |
3055 | + | |
3056 | + /* | |
3057 | + * This is a good place to make sure tracing engines don't | |
3058 | + * introduce too much latency under voluntary preemption. | |
3059 | + */ | |
3060 | + if (need_resched()) | |
3061 | + cond_resched(); | |
3062 | + | |
3063 | + report->result = ret & ~UTRACE_RESUME_MASK; | |
3064 | + | |
3065 | + /* | |
3066 | + * If utrace_control() was used, treat that like UTRACE_DETACH here. | |
3067 | + */ | |
3068 | + if (action == UTRACE_DETACH || engine->ops == &utrace_detached_ops) { | |
3069 | + engine->ops = &utrace_detached_ops; | |
3070 | + report->detaches = true; | |
3071 | + return true; | |
3072 | + } | |
3073 | + | |
3074 | + if (action < report->action) | |
3075 | + report->action = action; | |
3076 | + | |
3077 | + if (action == UTRACE_STOP) { | |
3078 | + if (!engine_wants_stop(engine)) { | |
3079 | + spin_lock(&utrace->lock); | |
3080 | + mark_engine_wants_stop(engine); | |
3081 | + spin_unlock(&utrace->lock); | |
3082 | + } | |
3083 | + } else if (engine_wants_stop(engine)) { | |
3084 | + spin_lock(&utrace->lock); | |
3085 | + clear_engine_wants_stop(engine); | |
3086 | + spin_unlock(&utrace->lock); | |
3087 | + } | |
3088 | + | |
3089 | + return false; | |
3090 | +} | |
3091 | + | |
3092 | +/* | |
3093 | + * Start the callbacks for @engine to consider @event (a bit mask). | |
3094 | + * This makes the report_quiesce() callback first. If @engine wants | |
3095 | + * a specific callback for @event, we return the ops vector to use. | |
3096 | + * If not, we return NULL. The return value from the ops->callback | |
3097 | + * function called should be passed to finish_callback(). | |
3098 | + */ | |
3099 | +static const struct utrace_engine_ops *start_callback( | |
3100 | + struct utrace *utrace, struct utrace_report *report, | |
3101 | + struct utrace_attached_engine *engine, struct task_struct *task, | |
3102 | + unsigned long event) | |
3103 | +{ | |
3104 | + const struct utrace_engine_ops *ops; | |
3105 | + unsigned long want; | |
3106 | + | |
3107 | + utrace->reporting = engine; | |
3108 | + smp_mb(); | |
3109 | + | |
3110 | + /* | |
3111 | + * This pairs with the barrier in mark_engine_detached(). | |
3112 | + * It makes sure that we never see the old ops vector with | |
3113 | + * the new flags, in case the original vector had no report_quiesce. | |
3114 | + */ | |
3115 | + want = engine->flags; | |
3116 | + smp_rmb(); | |
3117 | + ops = engine->ops; | |
3118 | + | |
3119 | + if (want & UTRACE_EVENT(QUIESCE)) { | |
3120 | + if (finish_callback(utrace, report, engine, | |
3121 | + (*ops->report_quiesce)(report->action, | |
3122 | + engine, task, | |
3123 | + event))) | |
3124 | + goto nocall; | |
3125 | + | |
3126 | + utrace->reporting = engine; | |
3127 | + smp_mb(); | |
3128 | + want = engine->flags; | |
3129 | + } | |
3130 | + | |
3131 | + if (want & ENGINE_STOP) | |
3132 | + report->action = UTRACE_STOP; | |
3133 | + | |
3134 | + if (want & event) { | |
3135 | + report->takers = true; | |
3136 | + return ops; | |
3137 | + } | |
3138 | + | |
3139 | +nocall: | |
3140 | + utrace->reporting = NULL; | |
3141 | + return NULL; | |
3142 | +} | |
3143 | + | |
3144 | +/* | |
3145 | + * Do a normal reporting pass for engines interested in @event. | |
3146 | + * @callback is the name of the member in the ops vector, and remaining | |
3147 | + * args are the extras it takes after the standard three args. | |
3148 | + */ | |
3149 | +#define REPORT(task, utrace, report, event, callback, ...) \ | |
3150 | + do { \ | |
3151 | + start_report(utrace); \ | |
3152 | + REPORT_CALLBACKS(task, utrace, report, event, callback, \ | |
3153 | + (report)->action, engine, current, \ | |
3154 | + ## __VA_ARGS__); \ | |
3155 | + finish_report(report, task, utrace); \ | |
3156 | + } while (0) | |
3157 | +#define REPORT_CALLBACKS(task, utrace, report, event, callback, ...) \ | |
3158 | + do { \ | |
3159 | + struct utrace_attached_engine *engine, *next; \ | |
3160 | + const struct utrace_engine_ops *ops; \ | |
3161 | + list_for_each_entry_safe(engine, next, \ | |
3162 | + &utrace->attached, entry) { \ | |
3163 | + ops = start_callback(utrace, report, engine, task, \ | |
3164 | + event); \ | |
3165 | + if (!ops) \ | |
3166 | + continue; \ | |
3167 | + finish_callback(utrace, report, engine, \ | |
3168 | + (*ops->callback)(__VA_ARGS__)); \ | |
3169 | + } \ | |
3170 | + } while (0) | |
3171 | + | |
3172 | +/* | |
3173 | + * Called iff UTRACE_EVENT(EXEC) flag is set. | |
3174 | + */ | |
3175 | +void utrace_report_exec(struct linux_binfmt *fmt, struct linux_binprm *bprm, | |
3176 | + struct pt_regs *regs) | |
3177 | +{ | |
3178 | + struct task_struct *task = current; | |
3179 | + struct utrace *utrace = task->utrace; | |
3180 | + INIT_REPORT(report); | |
3181 | + | |
3182 | + REPORT(task, utrace, &report, UTRACE_EVENT(EXEC), | |
3183 | + report_exec, fmt, bprm, regs); | |
3184 | +} | |
3185 | + | |
3186 | +/* | |
3187 | + * Called iff UTRACE_EVENT(SYSCALL_ENTRY) flag is set. | |
3188 | + * Return true to prevent the system call. | |
3189 | + */ | |
3190 | +bool utrace_report_syscall_entry(struct pt_regs *regs) | |
3191 | +{ | |
3192 | + struct task_struct *task = current; | |
3193 | + struct utrace *utrace = task->utrace; | |
3194 | + INIT_REPORT(report); | |
3195 | + | |
3196 | + start_report(utrace); | |
3197 | + REPORT_CALLBACKS(task, utrace, &report, UTRACE_EVENT(SYSCALL_ENTRY), | |
3198 | + report_syscall_entry, report.result | report.action, | |
3199 | + engine, current, regs); | |
3200 | + finish_report(&report, task, utrace); | |
3201 | + | |
3202 | + if (report.action == UTRACE_STOP && unlikely(utrace_stop(task, utrace))) | |
3203 | + /* | |
3204 | + * We are continuing despite UTRACE_STOP because of a | |
3205 | + * SIGKILL. Don't let the system call actually proceed. | |
3206 | + */ | |
3207 | + return true; | |
3208 | + | |
3209 | + if (unlikely(report.result == UTRACE_SYSCALL_ABORT)) | |
3210 | + return true; | |
3211 | + | |
3212 | + if (signal_pending(task)) { | |
3213 | + /* | |
3214 | + * Clear TIF_SIGPENDING if it no longer needs to be set. | |
3215 | + * It may have been set as part of quiescence, and won't | |
3216 | + * ever have been cleared by another thread. For other | |
3217 | + * reports, we can just leave it set and will go through | |
3218 | + * utrace_get_signal() to reset things. But here we are | |
3219 | + * about to enter a syscall, which might bail out with an | |
3220 | + * -ERESTART* error if it's set now. | |
3221 | + */ | |
3222 | + spin_lock_irq(&task->sighand->siglock); | |
3223 | + recalc_sigpending(); | |
3224 | + spin_unlock_irq(&task->sighand->siglock); | |
3225 | + } | |
3226 | + | |
3227 | + return false; | |
3228 | +} | |
3229 | + | |
3230 | +/* | |
3231 | + * Called iff UTRACE_EVENT(SYSCALL_EXIT) flag is set. | |
3232 | + */ | |
3233 | +void utrace_report_syscall_exit(struct pt_regs *regs) | |
3234 | +{ | |
3235 | + struct task_struct *task = current; | |
3236 | + struct utrace *utrace = task->utrace; | |
3237 | + INIT_REPORT(report); | |
3238 | + | |
3239 | + REPORT(task, utrace, &report, UTRACE_EVENT(SYSCALL_EXIT), | |
3240 | + report_syscall_exit, regs); | |
3241 | +} | |
3242 | + | |
3243 | +/* | |
3244 | + * Called iff UTRACE_EVENT(CLONE) flag is set. | |
3245 | + * This notification call blocks the wake_up_new_task call on the child. | |
3246 | + * So we must not quiesce here. tracehook_report_clone_complete will do | |
3247 | + * a quiescence check momentarily. | |
3248 | + */ | |
3249 | +void utrace_report_clone(unsigned long clone_flags, struct task_struct *child) | |
3250 | +{ | |
3251 | + struct task_struct *task = current; | |
3252 | + struct utrace *utrace = task->utrace; | |
3253 | + INIT_REPORT(report); | |
3254 | + | |
3255 | + utrace->u.live.cloning = child; | |
3256 | + | |
3257 | + REPORT(task, utrace, &report, UTRACE_EVENT(CLONE), | |
3258 | + report_clone, clone_flags, child); | |
3259 | + | |
3260 | + utrace->u.live.cloning = NULL; | |
3261 | +} | |
3262 | + | |
3263 | +/* | |
3264 | + * Called iff UTRACE_EVENT(JCTL) flag is set. | |
3265 | + */ | |
3266 | +void utrace_report_jctl(int notify, int what) | |
3267 | +{ | |
3268 | + struct task_struct *task = current; | |
3269 | + struct utrace *utrace = task->utrace; | |
3270 | + INIT_REPORT(report); | |
3271 | + bool was_stopped = task_is_stopped(task); | |
3272 | + | |
3273 | + /* | |
3274 | + * We get here with CLD_STOPPED when we've just entered | |
3275 | + * TASK_STOPPED, or with CLD_CONTINUED when we've just come | |
3276 | + * out but not yet been through utrace_get_signal() again. | |
3277 | + * | |
3278 | + * While in TASK_STOPPED, we can be considered safely | |
3279 | + * stopped by utrace_do_stop() and detached asynchronously. | |
3280 | + * If we woke up and checked task->utrace_flags before that | |
3281 | + * was finished, we might be here with utrace already | |
3282 | + * removed or in the middle of being removed. | |
3283 | + * | |
3284 | + * RCU makes it safe to get the utrace->lock even if it's | |
3285 | + * being freed. Once we have that lock, either an external | |
3286 | + * detach has finished and this struct has been freed, or | |
3287 | + * else we know we are excluding any other detach attempt. | |
3288 | + * | |
3289 | + * If we are indeed attached, then make sure we are no | |
3290 | + * longer considered stopped while we run callbacks. | |
3291 | + */ | |
3292 | + rcu_read_lock(); | |
3293 | + utrace = rcu_dereference(task->utrace); | |
3294 | + if (unlikely(!utrace)) { | |
3295 | + rcu_read_unlock(); | |
3296 | + return; | |
3297 | + } | |
3298 | + spin_lock(&utrace->lock); | |
3299 | + utrace->stopped = 0; | |
3300 | + utrace->report = 0; | |
3301 | + spin_unlock(&utrace->lock); | |
3302 | + rcu_read_unlock(); | |
3303 | + | |
3304 | + REPORT(task, utrace, &report, UTRACE_EVENT(JCTL), | |
3305 | + report_jctl, was_stopped ? CLD_STOPPED : CLD_CONTINUED, what); | |
3306 | + | |
3307 | + if (was_stopped && !task_is_stopped(task)) { | |
3308 | + /* | |
3309 | + * The event report hooks could have blocked, though | |
3310 | + * it should have been briefly. Make sure we're in | |
3311 | + * TASK_STOPPED state again to block properly, unless | |
3312 | + * we've just come back out of job control stop. | |
3313 | + */ | |
3314 | + spin_lock_irq(&task->sighand->siglock); | |
3315 | + if (task->signal->flags & SIGNAL_STOP_STOPPED) | |
3316 | + __set_current_state(TASK_STOPPED); | |
3317 | + spin_unlock_irq(&task->sighand->siglock); | |
3318 | + } | |
3319 | + | |
3320 | + if (task_is_stopped(current)) { | |
3321 | + /* | |
3322 | + * While in TASK_STOPPED, we can be considered safely | |
3323 | + * stopped by utrace_do_stop() only once we set this. | |
3324 | + */ | |
3325 | + spin_lock(&utrace->lock); | |
3326 | + utrace->stopped = 1; | |
3327 | + spin_unlock(&utrace->lock); | |
3328 | + } | |
3329 | +} | |
3330 | + | |
3331 | +/* | |
3332 | + * Called iff UTRACE_EVENT(EXIT) flag is set. | |
3333 | + */ | |
3334 | +void utrace_report_exit(long *exit_code) | |
3335 | +{ | |
3336 | + struct task_struct *task = current; | |
3337 | + struct utrace *utrace = task->utrace; | |
3338 | + INIT_REPORT(report); | |
3339 | + long orig_code = *exit_code; | |
3340 | + | |
3341 | + REPORT(task, utrace, &report, UTRACE_EVENT(EXIT), | |
3342 | + report_exit, orig_code, exit_code); | |
3343 | + | |
3344 | + if (report.action == UTRACE_STOP) | |
3345 | + utrace_stop(task, utrace); | |
3346 | +} | |
3347 | + | |
3348 | +/* | |
3349 | + * Called iff UTRACE_EVENT(DEATH) or UTRACE_EVENT(QUIESCE) flag is set. | |
3350 | + * | |
3351 | + * It is always possible that we are racing with utrace_release_task here. | |
3352 | + * For this reason, utrace_release_task checks for the event bits that get | |
3353 | + * us here, and delays its cleanup for us to do. | |
3354 | + */ | |
3355 | +void utrace_report_death(struct task_struct *task, struct utrace *utrace, | |
3356 | + bool group_dead, int signal) | |
3357 | +{ | |
3358 | + INIT_REPORT(report); | |
3359 | + | |
3360 | + BUG_ON(!task->exit_state); | |
3361 | + | |
3362 | + /* | |
3363 | + * We are presently considered "quiescent"--which is accurate | |
3364 | + * inasmuch as we won't run any more user instructions ever again. | |
3365 | + * But for utrace_control and utrace_set_events to be robust, they | |
3366 | + * must be sure whether or not we will run any more callbacks. If | |
3367 | + * a call comes in before we do, taking the lock here synchronizes | |
3368 | + * us so we don't run any callbacks just disabled. Calls that come | |
3369 | + * in while we're running the callbacks will see the exit.death | |
3370 | + * flag and know that we are not yet fully quiescent for purposes | |
3371 | + * of detach bookkeeping. | |
3372 | + */ | |
3373 | + spin_lock(&utrace->lock); | |
3374 | + BUG_ON(utrace->death); | |
3375 | + utrace->death = 1; | |
3376 | + utrace->report = 0; | |
3377 | + utrace->interrupt = 0; | |
3378 | + spin_unlock(&utrace->lock); | |
3379 | + | |
3380 | + REPORT_CALLBACKS(task, utrace, &report, UTRACE_EVENT(DEATH), | |
3381 | + report_death, engine, task, group_dead, signal); | |
3382 | + | |
3383 | + spin_lock(&utrace->lock); | |
3384 | + | |
3385 | + /* | |
3386 | + * After we unlock (possibly inside utrace_reap for callbacks) with | |
3387 | + * this flag clear, competing utrace_control/utrace_set_events calls | |
3388 | + * know that we've finished our callbacks and any detach bookkeeping. | |
3389 | + */ | |
3390 | + utrace->death = 0; | |
3391 | + | |
3392 | + if (utrace->reap) | |
3393 | + /* | |
3394 | + * utrace_release_task() was already called in parallel. | |
3395 | + * We must complete its work now. | |
3396 | + */ | |
3397 | + utrace_reap(task, utrace); | |
3398 | + else | |
3399 | + utrace_reset(task, utrace, false); | |
3400 | +} | |
3401 | + | |
3402 | +/* | |
3403 | + * Finish the last reporting pass before returning to user mode. | |
3404 | + * | |
3405 | + * Returns true if we might have been in TASK_TRACED and then resumed. | |
3406 | + * In that event, signal_pending() might not be set when it should be, | |
3407 | + * as the signals code passes us over while we're in TASK_TRACED. | |
3408 | + */ | |
3409 | +static bool finish_resume_report(struct utrace_report *report, | |
3410 | + struct task_struct *task, | |
3411 | + struct utrace *utrace) | |
3412 | +{ | |
3413 | + if (report->detaches || !report->takers) { | |
3414 | + spin_lock(&utrace->lock); | |
3415 | + utrace_reset(task, utrace, false); | |
3416 | + } | |
3417 | + | |
3418 | + switch (report->action) { | |
3419 | + case UTRACE_INTERRUPT: | |
3420 | + if (!signal_pending(task)) | |
3421 | + set_tsk_thread_flag(task, TIF_SIGPENDING); | |
3422 | + break; | |
3423 | + | |
3424 | + case UTRACE_SINGLESTEP: | |
3425 | + user_enable_single_step(task); | |
3426 | + break; | |
3427 | + | |
3428 | + case UTRACE_BLOCKSTEP: | |
3429 | + user_enable_block_step(task); | |
3430 | + break; | |
3431 | + | |
3432 | + case UTRACE_STOP: | |
3433 | + report->killed = utrace_stop(task, utrace); | |
3434 | + return likely(!report->killed); | |
3435 | + | |
3436 | + case UTRACE_REPORT: | |
3437 | + case UTRACE_RESUME: | |
3438 | + default: | |
3439 | + user_disable_single_step(task); | |
3440 | + break; | |
3441 | + } | |
3442 | + | |
3443 | + return false; | |
3444 | +} | |
3445 | + | |
3446 | +/* | |
3447 | + * This is called when TIF_NOTIFY_RESUME had been set (and is now clear). | |
3448 | + * We are close to user mode, and this is the place to report or stop. | |
3449 | + * When we return, we're going to user mode or into the signals code. | |
3450 | + */ | |
3451 | +void utrace_resume(struct task_struct *task, struct pt_regs *regs) | |
3452 | +{ | |
3453 | + struct utrace *utrace = task->utrace; | |
3454 | + INIT_REPORT(report); | |
3455 | + struct utrace_attached_engine *engine, *next; | |
3456 | + | |
3457 | + /* | |
3458 | + * Some machines get here with interrupts disabled. The same arch | |
3459 | + * code path leads to calling into get_signal_to_deliver(), which | |
3460 | + * implicitly reenables them by virtue of spin_unlock_irq. | |
3461 | + */ | |
3462 | + local_irq_enable(); | |
3463 | + | |
3464 | + /* | |
3465 | + * If this flag is still set it's because there was a signal | |
3466 | + * handler setup done but no report_signal following it. Clear | |
3467 | + * the flag before we get to user so it doesn't confuse us later. | |
3468 | + */ | |
3469 | + if (unlikely(utrace->signal_handler)) { | |
3470 | + int skip; | |
3471 | + spin_lock(&utrace->lock); | |
3472 | + utrace->signal_handler = 0; | |
3473 | + skip = !utrace->report; | |
3474 | + spin_unlock(&utrace->lock); | |
3475 | + if (skip) | |
3476 | + return; | |
3477 | + } | |
3478 | + | |
3479 | + /* | |
3480 | + * If UTRACE_INTERRUPT was just used, we don't bother with a | |
3481 | + * report here. We will report and stop in utrace_get_signal(). | |
3482 | + */ | |
3483 | + if (unlikely(utrace->interrupt)) { | |
3484 | + BUG_ON(!signal_pending(task)); | |
3485 | + return; | |
3486 | + } | |
3487 | + | |
3488 | + /* | |
3489 | + * Do a simple reporting pass, with no callback after report_quiesce. | |
3490 | + */ | |
3491 | + start_report(utrace); | |
3492 | + | |
3493 | + list_for_each_entry_safe(engine, next, &utrace->attached, entry) | |
3494 | + start_callback(utrace, &report, engine, task, 0); | |
3495 | + | |
3496 | + /* | |
3497 | + * Finish the report and either stop or get ready to resume. | |
3498 | + * If we stop and then signal_pending() is clear, we | |
3499 | + * should recompute it before returning to user mode. | |
3500 | + */ | |
3501 | + if (finish_resume_report(&report, task, utrace) && | |
3502 | + !signal_pending(task)) { | |
3503 | + spin_lock_irq(&task->sighand->siglock); | |
3504 | + recalc_sigpending(); | |
3505 | + spin_unlock_irq(&task->sighand->siglock); | |
3506 | + } | |
3507 | +} | |
3508 | + | |
3509 | +/* | |
3510 | + * Return true if current has forced signal_pending(). | |
3511 | + * | |
3512 | + * This is called only when current->utrace_flags is nonzero, so we know | |
3513 | + * that current->utrace must be set. It's not inlined in tracehook.h | |
3514 | + * just so that struct utrace can stay opaque outside this file. | |
3515 | + */ | |
3516 | +bool utrace_interrupt_pending(void) | |
3517 | +{ | |
3518 | + return current->utrace->interrupt; | |
3519 | +} | |
3520 | + | |
3521 | +/* | |
3522 | + * Take the siglock and push @info back on our queue. | |
3523 | + * Returns with @task->sighand->siglock held. | |
3524 | + */ | |
3525 | +static void push_back_signal(struct task_struct *task, siginfo_t *info) | |
3526 | + __acquires(task->sighand->siglock) | |
3527 | +{ | |
3528 | + struct sigqueue *q; | |
3529 | + | |
3530 | + if (unlikely(!info->si_signo)) { /* Oh, a wise guy! */ | |
3531 | + spin_lock_irq(&task->sighand->siglock); | |
3532 | + return; | |
3533 | + } | |
3534 | + | |
3535 | + q = sigqueue_alloc(); | |
3536 | + if (likely(q)) { | |
3537 | + q->flags = 0; | |
3538 | + copy_siginfo(&q->info, info); | |
3539 | + } | |
3540 | + | |
3541 | + spin_lock_irq(&task->sighand->siglock); | |
3542 | + | |
3543 | + sigaddset(&task->pending.signal, info->si_signo); | |
3544 | + if (likely(q)) | |
3545 | + list_add(&q->list, &task->pending.list); | |
3546 | + | |
3547 | + set_tsk_thread_flag(task, TIF_SIGPENDING); | |
3548 | +} | |
3549 | + | |
3550 | +/* | |
3551 | + * This is the hook from the signals code, called with the siglock held. | |
3552 | + * Here is the ideal place to stop. We also dequeue and intercept signals. | |
3553 | + */ | |
3554 | +int utrace_get_signal(struct task_struct *task, struct pt_regs *regs, | |
3555 | + siginfo_t *info, struct k_sigaction *return_ka) | |
3556 | + __releases(task->sighand->siglock) | |
3557 | + __acquires(task->sighand->siglock) | |
3558 | +{ | |
3559 | + struct utrace *utrace; | |
3560 | + struct k_sigaction *ka; | |
3561 | + INIT_REPORT(report); | |
3562 | + struct utrace_attached_engine *engine, *next; | |
3563 | + const struct utrace_engine_ops *ops; | |
3564 | + unsigned long event, want; | |
3565 | + u32 ret; | |
3566 | + int signr; | |
3567 | + | |
3568 | + /* | |
3569 | + * We could have been considered quiescent while we were in | |
3570 | + * TASK_STOPPED, and detached asynchronously. If we woke up | |
3571 | + * and checked task->utrace_flags before that was finished, | |
3572 | + * we might be here with utrace already removed or in the | |
3573 | + * middle of being removed. | |
3574 | + */ | |
3575 | + rcu_read_lock(); | |
3576 | + utrace = rcu_dereference(task->utrace); | |
3577 | + if (unlikely(utrace == NULL)) { | |
3578 | + rcu_read_unlock(); | |
3579 | + return 0; | |
3580 | + } | |
3581 | + | |
3582 | + if (utrace->interrupt || utrace->report || utrace->signal_handler) { | |
3583 | + /* | |
3584 | + * We've been asked for an explicit report before we | |
3585 | + * even check for pending signals. | |
3586 | + */ | |
3587 | + | |
3588 | + spin_unlock_irq(&task->sighand->siglock); | |
3589 | + | |
3590 | + /* | |
3591 | + * RCU makes it safe to get the utrace->lock even if | |
3592 | + * it's being freed. Once we have that lock, either an | |
3593 | + * external detach has finished and this struct has been | |
3594 | + * freed, or else we know we are excluding any other | |
3595 | + * detach attempt. | |
3596 | + */ | |
3597 | + spin_lock(&utrace->lock); | |
3598 | + rcu_read_unlock(); | |
3599 | + | |
3600 | + if (unlikely(task->utrace != utrace)) { | |
3601 | + spin_unlock(&utrace->lock); | |
3602 | + cond_resched(); | |
3603 | + return -1; | |
3604 | + } | |
3605 | + | |
3606 | + splice_attaching(utrace); | |
3607 | + | |
3608 | + if (unlikely(!utrace->interrupt) && unlikely(!utrace->report)) | |
3609 | + report.result = UTRACE_SIGNAL_IGN; | |
3610 | + else if (utrace->signal_handler) | |
3611 | + report.result = UTRACE_SIGNAL_HANDLER; | |
3612 | + else | |
3613 | + report.result = UTRACE_SIGNAL_REPORT; | |
3614 | + | |
3615 | + /* | |
3616 | + * We are now making the report and it's on the | |
3617 | + * interrupt path, so clear the flags asking for those. | |
3618 | + */ | |
3619 | + utrace->interrupt = utrace->report = utrace->signal_handler = 0; | |
3620 | + | |
3621 | + /* | |
3622 | + * Make sure signal_pending() only returns true | |
3623 | + * if there are real signals pending. | |
3624 | + */ | |
3625 | + if (signal_pending(task)) { | |
3626 | + spin_lock_irq(&task->sighand->siglock); | |
3627 | + recalc_sigpending(); | |
3628 | + spin_unlock_irq(&task->sighand->siglock); | |
3629 | + } | |
3630 | + | |
3631 | + spin_unlock(&utrace->lock); | |
3632 | + | |
3633 | + if (unlikely(report.result == UTRACE_SIGNAL_IGN)) | |
3634 | + /* | |
3635 | + * We only got here to clear utrace->signal_handler. | |
3636 | + */ | |
3637 | + return -1; | |
3638 | + | |
3639 | + /* | |
3640 | + * Do a reporting pass for no signal, just for EVENT(QUIESCE). | |
3641 | + * The engine callbacks can fill in *info and *return_ka. | |
3642 | + * We'll pass NULL for the @orig_ka argument to indicate | |
3643 | + * that there was no original signal. | |
3644 | + */ | |
3645 | + event = 0; | |
3646 | + ka = NULL; | |
3647 | + memset(return_ka, 0, sizeof *return_ka); | |
3648 | + } else if ((task->utrace_flags & UTRACE_EVENT_SIGNAL_ALL) == 0) { | |
3649 | + /* | |
3650 | + * If noone is interested in intercepting signals, | |
3651 | + * let the caller just dequeue them normally. | |
3652 | + */ | |
3653 | + rcu_read_unlock(); | |
3654 | + return 0; | |
3655 | + } else { | |
3656 | + if (unlikely(utrace->stopped)) { | |
3657 | + /* | |
3658 | + * We were just in TASK_STOPPED, so we have to | |
3659 | + * check for the race mentioned above. | |
3660 | + * | |
3661 | + * RCU makes it safe to get the utrace->lock even | |
3662 | + * if it's being freed. Once we have that lock, | |
3663 | + * either an external detach has finished and this | |
3664 | + * struct has been freed, or else we know we are | |
3665 | + * excluding any other detach attempt. Since we | |
3666 | + * are no longer in TASK_STOPPED now, all we needed | |
3667 | + * the lock for was to order any utrace_do_stop() | |
3668 | + * call after us. | |
3669 | + */ | |
3670 | + spin_unlock_irq(&task->sighand->siglock); | |
3671 | + spin_lock(&utrace->lock); | |
3672 | + rcu_read_unlock(); | |
3673 | + if (unlikely(task->utrace != utrace)) { | |
3674 | + spin_unlock(&utrace->lock); | |
3675 | + cond_resched(); | |
3676 | + return -1; | |
3677 | + } | |
3678 | + utrace->stopped = 0; | |
3679 | + spin_unlock(&utrace->lock); | |
3680 | + spin_lock_irq(&task->sighand->siglock); | |
3681 | + } else { | |
3682 | + rcu_read_unlock(); | |
3683 | + } | |
3684 | + | |
3685 | + /* | |
3686 | + * Steal the next signal so we can let tracing engines | |
3687 | + * examine it. From the signal number and sigaction, | |
3688 | + * determine what normal delivery would do. If no | |
3689 | + * engine perturbs it, we'll do that by returning the | |
3690 | + * signal number after setting *return_ka. | |
3691 | + */ | |
3692 | + signr = dequeue_signal(task, &task->blocked, info); | |
3693 | + if (signr == 0) | |
3694 | + return signr; | |
3695 | + BUG_ON(signr != info->si_signo); | |
3696 | + | |
3697 | + ka = &task->sighand->action[signr - 1]; | |
3698 | + *return_ka = *ka; | |
3699 | + | |
3700 | + /* | |
3701 | + * We are never allowed to interfere with SIGKILL. | |
3702 | + * Just punt after filling in *return_ka for our caller. | |
3703 | + */ | |
3704 | + if (signr == SIGKILL) | |
3705 | + return signr; | |
3706 | + | |
3707 | + if (ka->sa.sa_handler == SIG_IGN) { | |
3708 | + event = UTRACE_EVENT(SIGNAL_IGN); | |
3709 | + report.result = UTRACE_SIGNAL_IGN; | |
3710 | + } else if (ka->sa.sa_handler != SIG_DFL) { | |
3711 | + event = UTRACE_EVENT(SIGNAL); | |
3712 | + report.result = UTRACE_SIGNAL_DELIVER; | |
3713 | + } else if (sig_kernel_coredump(signr)) { | |
3714 | + event = UTRACE_EVENT(SIGNAL_CORE); | |
3715 | + report.result = UTRACE_SIGNAL_CORE; | |
3716 | + } else if (sig_kernel_ignore(signr)) { | |
3717 | + event = UTRACE_EVENT(SIGNAL_IGN); | |
3718 | + report.result = UTRACE_SIGNAL_IGN; | |
3719 | + } else if (signr == SIGSTOP) { | |
3720 | + event = UTRACE_EVENT(SIGNAL_STOP); | |
3721 | + report.result = UTRACE_SIGNAL_STOP; | |
3722 | + } else if (sig_kernel_stop(signr)) { | |
3723 | + event = UTRACE_EVENT(SIGNAL_STOP); | |
3724 | + report.result = UTRACE_SIGNAL_TSTP; | |
3725 | + } else { | |
3726 | + event = UTRACE_EVENT(SIGNAL_TERM); | |
3727 | + report.result = UTRACE_SIGNAL_TERM; | |
3728 | + } | |
3729 | + | |
3730 | + /* | |
3731 | + * Now that we know what event type this signal is, | |
3732 | + * we can short-circuit if noone cares about those. | |
3733 | + */ | |
3734 | + if ((task->utrace_flags & (event | UTRACE_EVENT(QUIESCE))) == 0) | |
3735 | + return signr; | |
3736 | + | |
3737 | + /* | |
3738 | + * We have some interested engines, so tell them about | |
3739 | + * the signal and let them change its disposition. | |
3740 | + */ | |
3741 | + spin_unlock_irq(&task->sighand->siglock); | |
3742 | + } | |
3743 | + | |
3744 | + /* | |
3745 | + * This reporting pass chooses what signal disposition we'll act on. | |
3746 | + */ | |
3747 | + list_for_each_entry_safe(engine, next, &utrace->attached, entry) { | |
3748 | + utrace->reporting = engine; | |
3749 | + smp_mb(); | |
3750 | + | |
3751 | + /* | |
3752 | + * This pairs with the barrier in mark_engine_detached(), | |
3753 | + * see start_callback() comments. | |
3754 | + */ | |
3755 | + want = engine->flags; | |
3756 | + smp_rmb(); | |
3757 | + ops = engine->ops; | |
3758 | + | |
3759 | + if ((want & (event | UTRACE_EVENT(QUIESCE))) == 0) { | |
3760 | + utrace->reporting = NULL; | |
3761 | + continue; | |
3762 | + } | |
3763 | + | |
3764 | + if (ops->report_signal) | |
3765 | + ret = (*ops->report_signal)( | |
3766 | + report.result | report.action, engine, task, | |
3767 | + regs, info, ka, return_ka); | |
3768 | + else | |
3769 | + ret = (report.result | (*ops->report_quiesce)( | |
3770 | + report.action, engine, task, event)); | |
3771 | + | |
3772 | + /* | |
3773 | + * Avoid a tight loop reporting again and again if some | |
3774 | + * engine is too stupid. | |
3775 | + */ | |
3776 | + switch (utrace_resume_action(ret)) { | |
3777 | + default: | |
3778 | + break; | |
3779 | + case UTRACE_INTERRUPT: | |
3780 | + case UTRACE_REPORT: | |
3781 | + ret = (ret & ~UTRACE_RESUME_MASK) | UTRACE_RESUME; | |
3782 | + break; | |
3783 | + } | |
3784 | + | |
3785 | + finish_callback(utrace, &report, engine, ret); | |
3786 | + } | |
3787 | + | |
3788 | + /* | |
3789 | + * We express the chosen action to the signals code in terms | |
3790 | + * of a representative signal whose default action does it. | |
3791 | + * Our caller uses our return value (signr) to decide what to | |
3792 | + * do, but uses info->si_signo as the signal number to report. | |
3793 | + */ | |
3794 | + switch (utrace_signal_action(report.result)) { | |
3795 | + case UTRACE_SIGNAL_TERM: | |
3796 | + signr = SIGTERM; | |
3797 | + break; | |
3798 | + | |
3799 | + case UTRACE_SIGNAL_CORE: | |
3800 | + signr = SIGQUIT; | |
3801 | + break; | |
3802 | + | |
3803 | + case UTRACE_SIGNAL_STOP: | |
3804 | + signr = SIGSTOP; | |
3805 | + break; | |
3806 | + | |
3807 | + case UTRACE_SIGNAL_TSTP: | |
3808 | + signr = SIGTSTP; | |
3809 | + break; | |
3810 | + | |
3811 | + case UTRACE_SIGNAL_DELIVER: | |
3812 | + signr = info->si_signo; | |
3813 | + | |
3814 | + if (return_ka->sa.sa_handler == SIG_DFL) { | |
3815 | + /* | |
3816 | + * We'll do signr's normal default action. | |
3817 | + * For ignore, we'll fall through below. | |
3818 | + * For stop/death, break locks and returns it. | |
3819 | + */ | |
3820 | + if (likely(signr) && !sig_kernel_ignore(signr)) | |
3821 | + break; | |
3822 | + } else if (return_ka->sa.sa_handler != SIG_IGN && | |
3823 | + likely(signr)) { | |
3824 | + /* | |
3825 | + * The handler will run. If an engine wanted to | |
3826 | + * stop or step, then make sure we do another | |
3827 | + * report after signal handler setup. | |
3828 | + */ | |
3829 | + if (report.action != UTRACE_RESUME) { | |
3830 | + spin_lock(&utrace->lock); | |
3831 | + utrace->interrupt = 1; | |
3832 | + spin_unlock(&utrace->lock); | |
3833 | + set_tsk_thread_flag(task, TIF_SIGPENDING); | |
3834 | + } | |
3835 | + | |
3836 | + if (unlikely(report.result & UTRACE_SIGNAL_HOLD)) | |
3837 | + push_back_signal(task, info); | |
3838 | + else | |
3839 | + spin_lock_irq(&task->sighand->siglock); | |
3840 | + | |
3841 | + /* | |
3842 | + * We do the SA_ONESHOT work here since the | |
3843 | + * normal path will only touch *return_ka now. | |
3844 | + */ | |
3845 | + if (unlikely(return_ka->sa.sa_flags & SA_ONESHOT)) { | |
3846 | + return_ka->sa.sa_flags &= ~SA_ONESHOT; | |
3847 | + if (likely(valid_signal(signr))) { | |
3848 | + ka = &task->sighand->action[signr - 1]; | |
3849 | + ka->sa.sa_handler = SIG_DFL; | |
3850 | + } | |
3851 | + } | |
3852 | + | |
3853 | + return signr; | |
3854 | + } | |
3855 | + | |
3856 | + /* Fall through for an ignored signal. */ | |
3857 | + | |
3858 | + case UTRACE_SIGNAL_IGN: | |
3859 | + case UTRACE_SIGNAL_REPORT: | |
3860 | + default: | |
3861 | + /* | |
3862 | + * If the signal is being ignored, then we are on the way | |
3863 | + * directly back to user mode. We can stop here, or step, | |
3864 | + * as in utrace_resume(), above. After we've dealt with that, | |
3865 | + * our caller will relock and come back through here. | |
3866 | + */ | |
3867 | + finish_resume_report(&report, task, utrace); | |
3868 | + | |
3869 | + if (unlikely(report.killed)) { | |
3870 | + /* | |
3871 | + * The only reason we woke up now was because of a | |
3872 | + * SIGKILL. Don't do normal dequeuing in case it | |
3873 | + * might get a signal other than SIGKILL. That would | |
3874 | + * perturb the death state so it might differ from | |
3875 | + * what the debugger would have allowed to happen. | |
3876 | + * Instead, pluck out just the SIGKILL to be sure | |
3877 | + * we'll die immediately with nothing else different | |
3878 | + * from the quiescent state the debugger wanted us in. | |
3879 | + */ | |
3880 | + sigset_t sigkill_only; | |
3881 | + siginitsetinv(&sigkill_only, sigmask(SIGKILL)); | |
3882 | + spin_lock_irq(&task->sighand->siglock); | |
3883 | + signr = dequeue_signal(task, &sigkill_only, info); | |
3884 | + BUG_ON(signr != SIGKILL); | |
3885 | + *return_ka = task->sighand->action[SIGKILL - 1]; | |
3886 | + return signr; | |
3887 | + } | |
3888 | + | |
3889 | + if (unlikely(report.result & UTRACE_SIGNAL_HOLD)) { | |
3890 | + push_back_signal(task, info); | |
3891 | + spin_unlock_irq(&task->sighand->siglock); | |
3892 | + } | |
3893 | + | |
3894 | + return -1; | |
3895 | + } | |
3896 | + | |
3897 | + return_ka->sa.sa_handler = SIG_DFL; | |
3898 | + | |
3899 | + if (unlikely(report.result & UTRACE_SIGNAL_HOLD)) | |
3900 | + push_back_signal(task, info); | |
3901 | + else | |
3902 | + spin_lock_irq(&task->sighand->siglock); | |
3903 | + | |
3904 | + if (sig_kernel_stop(signr)) | |
3905 | + task->signal->flags |= SIGNAL_STOP_DEQUEUED; | |
3906 | + | |
3907 | + return signr; | |
3908 | +} | |
3909 | + | |
3910 | +/* | |
3911 | + * This gets called after a signal handler has been set up. | |
3912 | + * We set a flag so the next report knows it happened. | |
3913 | + * If we're already stepping, make sure we do a report_signal. | |
3914 | + * If not, make sure we get into utrace_resume() where we can | |
3915 | + * clear the signal_handler flag before resuming. | |
3916 | + */ | |
3917 | +void utrace_signal_handler(struct task_struct *task, int stepping) | |
3918 | +{ | |
3919 | + struct utrace *utrace = task->utrace; | |
3920 | + | |
3921 | + spin_lock(&utrace->lock); | |
3922 | + | |
3923 | + utrace->signal_handler = 1; | |
3924 | + if (stepping) { | |
3925 | + utrace->interrupt = 1; | |
3926 | + set_tsk_thread_flag(task, TIF_SIGPENDING); | |
3927 | + } else { | |
3928 | + set_notify_resume(task); | |
3929 | + } | |
3930 | + | |
3931 | + spin_unlock(&utrace->lock); | |
3932 | +} | |
3933 | + | |
3934 | +/** | |
3935 | + * utrace_prepare_examine - prepare to examine thread state | |
3936 | + * @target: thread of interest, a &struct task_struct pointer | |
3937 | + * @engine: engine pointer returned by utrace_attach_task() | |
3938 | + * @exam: temporary state, a &struct utrace_examiner pointer | |
3939 | + * | |
3940 | + * This call prepares to safely examine the thread @target using | |
3941 | + * &struct user_regset calls, or direct access to thread-synchronous fields. | |
3942 | + * | |
3943 | + * When @target is current, this call is superfluous. When @target is | |
3944 | + * another thread, it must held stopped via %UTRACE_STOP by @engine. | |
3945 | + * | |
3946 | + * This call may block the caller until @target stays stopped, so it must | |
3947 | + * be called only after the caller is sure @target is about to unschedule. | |
3948 | + * This means a zero return from a utrace_control() call on @engine giving | |
3949 | + * %UTRACE_STOP, or a report_quiesce() or report_signal() callback to | |
3950 | + * @engine that used %UTRACE_STOP in its return value. | |
3951 | + * | |
3952 | + * Returns -%ESRCH if @target is dead or -%EINVAL if %UTRACE_STOP was | |
3953 | + * not used. If @target has started running again despite %UTRACE_STOP | |
3954 | + * (for %SIGKILL or a spurious wakeup), this call returns -%EAGAIN. | |
3955 | + * | |
3956 | + * When this call returns zero, it's safe to use &struct user_regset | |
3957 | + * calls and task_user_regset_view() on @target and to examine some of | |
3958 | + * its fields directly. When the examination is complete, a | |
3959 | + * utrace_finish_examine() call must follow to check whether it was | |
3960 | + * completed safely. | |
3961 | + */ | |
3962 | +int utrace_prepare_examine(struct task_struct *target, | |
3963 | + struct utrace_attached_engine *engine, | |
3964 | + struct utrace_examiner *exam) | |
3965 | +{ | |
3966 | + int ret = 0; | |
3967 | + | |
3968 | + if (unlikely(target == current)) | |
3969 | + return 0; | |
3970 | + | |
3971 | + rcu_read_lock(); | |
3972 | + if (unlikely(!engine_wants_stop(engine))) | |
3973 | + ret = -EINVAL; | |
3974 | + else if (unlikely(target->exit_state)) | |
3975 | + ret = -ESRCH; | |
3976 | + else { | |
3977 | + exam->state = target->state; | |
3978 | + if (unlikely(exam->state == TASK_RUNNING)) | |
3979 | + ret = -EAGAIN; | |
3980 | + else | |
3981 | + get_task_struct(target); | |
3982 | + } | |
3983 | + rcu_read_unlock(); | |
3984 | + | |
3985 | + if (likely(!ret)) { | |
3986 | + exam->ncsw = wait_task_inactive(target, exam->state); | |
3987 | + put_task_struct(target); | |
3988 | + if (unlikely(!exam->ncsw)) | |
3989 | + ret = -EAGAIN; | |
3990 | + } | |
3991 | + | |
3992 | + return ret; | |
3993 | +} | |
3994 | +EXPORT_SYMBOL_GPL(utrace_prepare_examine); | |
3995 | + | |
3996 | +/** | |
3997 | + * utrace_finish_examine - complete an examination of thread state | |
3998 | + * @target: thread of interest, a &struct task_struct pointer | |
3999 | + * @engine: engine pointer returned by utrace_attach_task() | |
4000 | + * @exam: pointer passed to utrace_prepare_examine() call | |
4001 | + * | |
4002 | + * This call completes an examination on the thread @target begun by a | |
4003 | + * paired utrace_prepare_examine() call with the same arguments that | |
4004 | + * returned success (zero). | |
4005 | + * | |
4006 | + * When @target is current, this call is superfluous. When @target is | |
4007 | + * another thread, this returns zero if @target has remained unscheduled | |
4008 | + * since the paired utrace_prepare_examine() call returned zero. | |
4009 | + * | |
4010 | + * When this returns an error, any examination done since the paired | |
4011 | + * utrace_prepare_examine() call is unreliable and the data extracted | |
4012 | + * should be discarded. The error is -%EINVAL if @engine is not | |
4013 | + * keeping @target stopped, or -%EAGAIN if @target woke up unexpectedly. | |
4014 | + */ | |
4015 | +int utrace_finish_examine(struct task_struct *target, | |
4016 | + struct utrace_attached_engine *engine, | |
4017 | + struct utrace_examiner *exam) | |
4018 | +{ | |
4019 | + int ret = 0; | |
4020 | + | |
4021 | + if (unlikely(target == current)) | |
4022 | + return 0; | |
4023 | + | |
4024 | + rcu_read_lock(); | |
4025 | + if (unlikely(!engine_wants_stop(engine))) | |
4026 | + ret = -EINVAL; | |
4027 | + else if (unlikely(target->state != exam->state)) | |
4028 | + ret = -EAGAIN; | |
4029 | + else | |
4030 | + get_task_struct(target); | |
4031 | + rcu_read_unlock(); | |
4032 | + | |
4033 | + if (likely(!ret)) { | |
4034 | + unsigned long ncsw = wait_task_inactive(target, exam->state); | |
4035 | + if (unlikely(ncsw != exam->ncsw)) | |
4036 | + ret = -EAGAIN; | |
4037 | + put_task_struct(target); | |
4038 | + } | |
4039 | + | |
4040 | + return ret; | |
4041 | +} | |
4042 | +EXPORT_SYMBOL_GPL(utrace_finish_examine); | |
4043 | + | |
4044 | +/* | |
4045 | + * This is declared in linux/regset.h and defined in machine-dependent | |
4046 | + * code. We put the export here to ensure no machine forgets it. | |
4047 | + */ | |
4048 | +EXPORT_SYMBOL_GPL(task_user_regset_view); | |
4049 | + | |
4050 | +/* | |
4051 | + * Return the &struct task_struct for the task using ptrace on this one, | |
4052 | + * or %NULL. Must be called with rcu_read_lock() held to keep the returned | |
4053 | + * struct alive. | |
4054 | + * | |
4055 | + * At exec time, this may be called with task_lock() still held from when | |
4056 | + * tracehook_unsafe_exec() was just called. In that case it must give | |
4057 | + * results consistent with those unsafe_exec() results, i.e. non-%NULL if | |
4058 | + * any %LSM_UNSAFE_PTRACE_* bits were set. | |
4059 | + * | |
4060 | + * The value is also used to display after "TracerPid:" in /proc/PID/status, | |
4061 | + * where it is called with only rcu_read_lock() held. | |
4062 | + */ | |
4063 | +struct task_struct *utrace_tracer_task(struct task_struct *target) | |
4064 | +{ | |
4065 | + struct utrace *utrace; | |
4066 | + struct task_struct *tracer = NULL; | |
4067 | + | |
4068 | + utrace = rcu_dereference(target->utrace); | |
4069 | + if (utrace != NULL) { | |
4070 | + struct list_head *pos, *next; | |
4071 | + struct utrace_attached_engine *engine; | |
4072 | + const struct utrace_engine_ops *ops; | |
4073 | + list_for_each_safe(pos, next, &utrace->attached) { | |
4074 | + engine = list_entry(pos, struct utrace_attached_engine, | |
4075 | + entry); | |
4076 | + ops = rcu_dereference(engine->ops); | |
4077 | + if (ops->tracer_task) { | |
4078 | + tracer = (*ops->tracer_task)(engine, target); | |
4079 | + if (tracer != NULL) | |
4080 | + break; | |
4081 | + } | |
4082 | + } | |
4083 | + } | |
4084 | + | |
4085 | + return tracer; | |
4086 | +} | |
4087 | + | |
4088 | +/* | |
4089 | + * Called on the current task to return LSM_UNSAFE_* bits implied by tracing. | |
4090 | + * Called with task_lock() held. | |
4091 | + */ | |
4092 | +int utrace_unsafe_exec(struct task_struct *task) | |
4093 | +{ | |
4094 | + struct utrace *utrace = task->utrace; | |
4095 | + struct utrace_attached_engine *engine, *next; | |
4096 | + const struct utrace_engine_ops *ops; | |
4097 | + int unsafe = 0; | |
4098 | + | |
4099 | + list_for_each_entry_safe(engine, next, &utrace->attached, entry) { | |
4100 | + ops = rcu_dereference(engine->ops); | |
4101 | + if (ops->unsafe_exec) | |
4102 | + unsafe |= (*ops->unsafe_exec)(engine, task); | |
4103 | + } | |
4104 | + | |
4105 | + return unsafe; | |
4106 | +} | |
4107 | + | |
4108 | +/* | |
4109 | + * Called with rcu_read_lock() held. | |
4110 | + */ | |
4111 | +void task_utrace_proc_status(struct seq_file *m, struct task_struct *p) | |
4112 | +{ | |
4113 | + struct utrace *utrace = rcu_dereference(p->utrace); | |
4114 | + if (unlikely(utrace)) | |
4115 | + seq_printf(m, "Utrace: %lx%s%s%s\n", | |
4116 | + p->utrace_flags, | |
4117 | + utrace->stopped ? " (stopped)" : "", | |
4118 | + utrace->report ? " (report)" : "", | |
4119 | + utrace->interrupt ? " (interrupt)" : ""); | |
4120 | +} |