updated on Mon Jan 23 20:11:11 UTC 2012
[aur-mirror.git] / linux-fedora / utrace.patch
blob060d351e54b4342cec09aef0048c0d811065f45d
1 From davej Wed Aug 3 15:16:11 2011
2 From oleg@redhat.com Mon Nov 21 15:06:14 2011
3 Return-Path: oleg@redhat.com
4 Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
5 zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
6 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
7 15:06:14 -0500 (EST)
8 Received: from localhost (localhost.localdomain [127.0.0.1])
9 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 9A1F8D50F1;
10 Mon, 21 Nov 2011 15:06:14 -0500 (EST)
11 Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
12 by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
13 with ESMTP id gJzpul4rDNnA; Mon, 21 Nov 2011 15:06:14 -0500 (EST)
14 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25])
15 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 7DD4BD50EA;
16 Mon, 21 Nov 2011 15:06:14 -0500 (EST)
17 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
18 by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6BJJ022074;
19 Mon, 21 Nov 2011 15:06:12 -0500
20 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
21 oleg@redhat.com; Mon, 21 Nov 2011 21:01:32 +0100 (CET)
22 Date: Mon, 21 Nov 2011 21:01:30 +0100
23 From: Oleg Nesterov <oleg@redhat.com>
24 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
25 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
26 Kyle McMartin <kmcmartin@redhat.com>
27 Cc: kernel@lists.fedoraproject.org
28 Subject: [PATCH 01/33] utrace core
29 Message-ID: <20111121200130.GA27756@redhat.com>
30 MIME-Version: 1.0
31 Content-Type: text/plain; charset=us-ascii
32 Content-Disposition: inline
33 In-Reply-To: <20111121200039.GA27699@redhat.com>
34 User-Agent: Mutt/1.5.18 (2008-05-17)
35 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25
36 Status: RO
37 Content-Length: 139951
38 Lines: 3867
40 From: Roland McGrath <roland@redhat.com>
42 This adds the utrace facility, a new modular interface in the kernel for
43 implementing user thread tracing and debugging. This fits on top of the
44 tracehook_* layer, so the new code is well-isolated.
46 The new interface is in <linux/utrace.h> and the DocBook utrace book
47 describes it. It allows for multiple separate tracing engines to work in
48 parallel without interfering with each other. Higher-level tracing
49 facilities can be implemented as loadable kernel modules using this layer.
51 The new facility is made optional under CONFIG_UTRACE.
52 When this is not enabled, no new code is added.
53 It can only be enabled on machines that have all the
54 prerequisites and select CONFIG_HAVE_ARCH_TRACEHOOK.
56 In this initial version, utrace and ptrace do not play together at all,
57 the next patches try to fix this.
59 This is is same/old utrace-core patch except:
61 - use task->jobctl/JOBCTL_STOP_DEQUEUED instead of removed
62 signal->flags/SIGNAL_STOP_DEQUEUED in utrace_get_signal()
64 - do not include the tracehook changes, this comes with the
65 next patches
67 Signed-off-by: Roland McGrath <roland@redhat.com>
68 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
69 ---
70 Documentation/DocBook/Makefile | 2 +-
71 Documentation/DocBook/utrace.tmpl | 589 +++++++++
72 fs/proc/array.c | 3 +
73 include/linux/sched.h | 5 +
74 include/linux/utrace.h | 692 +++++++++++
75 init/Kconfig | 9 +
76 kernel/Makefile | 1 +
77 kernel/utrace.c | 2440 +++++++++++++++++++++++++++++++++++++
78 8 files changed, 3740 insertions(+), 1 deletions(-)
79 create mode 100644 Documentation/DocBook/utrace.tmpl
80 create mode 100644 include/linux/utrace.h
81 create mode 100644 kernel/utrace.c
83 diff --git a/Documentation/DocBook/Makefile b/Documentation/DocBook/Makefile
84 index 66725a3..08ed954 100644
85 --- a/Documentation/DocBook/Makefile
86 +++ b/Documentation/DocBook/Makefile
87 @@ -14,7 +14,7 @@ DOCBOOKS := z8530book.xml mcabook.xml device-drivers.xml \
88 genericirq.xml s390-drivers.xml uio-howto.xml scsi.xml \
89 80211.xml debugobjects.xml sh.xml regulator.xml \
90 alsa-driver-api.xml writing-an-alsa-driver.xml \
91 - tracepoint.xml drm.xml media_api.xml
92 + tracepoint.xml utrace.xml drm.xml media_api.xml
94 include $(srctree)/Documentation/DocBook/media/Makefile
96 diff --git a/Documentation/DocBook/utrace.tmpl b/Documentation/DocBook/utrace.tmpl
97 new file mode 100644
98 index 0000000..0c40add
99 --- /dev/null
100 +++ b/Documentation/DocBook/utrace.tmpl
101 @@ -0,0 +1,589 @@
102 +<?xml version="1.0" encoding="UTF-8"?>
103 +<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
104 +"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
106 +<book id="utrace">
107 + <bookinfo>
108 + <title>The utrace User Debugging Infrastructure</title>
109 + </bookinfo>
111 + <toc></toc>
113 + <chapter id="concepts"><title>utrace concepts</title>
115 + <sect1 id="intro"><title>Introduction</title>
117 + <para>
118 + <application>utrace</application> is infrastructure code for tracing
119 + and controlling user threads. This is the foundation for writing
120 + tracing engines, which can be loadable kernel modules.
121 + </para>
123 + <para>
124 + The basic actors in <application>utrace</application> are the thread
125 + and the tracing engine. A tracing engine is some body of code that
126 + calls into the <filename>&lt;linux/utrace.h&gt;</filename>
127 + interfaces, represented by a <structname>struct
128 + utrace_engine_ops</structname>. (Usually it's a kernel module,
129 + though the legacy <function>ptrace</function> support is a tracing
130 + engine that is not in a kernel module.) The interface operates on
131 + individual threads (<structname>struct task_struct</structname>).
132 + If an engine wants to treat several threads as a group, that is up
133 + to its higher-level code.
134 + </para>
136 + <para>
137 + Tracing begins by attaching an engine to a thread, using
138 + <function>utrace_attach_task</function> or
139 + <function>utrace_attach_pid</function>. If successful, it returns a
140 + pointer that is the handle used in all other calls.
141 + </para>
143 + </sect1>
145 + <sect1 id="callbacks"><title>Events and Callbacks</title>
147 + <para>
148 + An attached engine does nothing by default. An engine makes something
149 + happen by requesting callbacks via <function>utrace_set_events</function>
150 + and poking the thread with <function>utrace_control</function>.
151 + The synchronization issues related to these two calls
152 + are discussed further below in <xref linkend="teardown"/>.
153 + </para>
155 + <para>
156 + Events are specified using the macro
157 + <constant>UTRACE_EVENT(<replaceable>type</replaceable>)</constant>.
158 + Each event type is associated with a callback in <structname>struct
159 + utrace_engine_ops</structname>. A tracing engine can leave unused
160 + callbacks <constant>NULL</constant>. The only callbacks required
161 + are those used by the event flags it sets.
162 + </para>
164 + <para>
165 + Many engines can be attached to each thread. When a thread has an
166 + event, each engine gets a callback if it has set the event flag for
167 + that event type. For most events, engines are called in the order they
168 + attached. Engines that attach after the event has occurred do not get
169 + callbacks for that event. This includes any new engines just attached
170 + by an existing engine's callback function. Once the sequence of
171 + callbacks for that one event has completed, such new engines are then
172 + eligible in the next sequence that starts when there is another event.
173 + </para>
175 + <para>
176 + Event reporting callbacks have details particular to the event type,
177 + but are all called in similar environments and have the same
178 + constraints. Callbacks are made from safe points, where no locks
179 + are held, no special resources are pinned (usually), and the
180 + user-mode state of the thread is accessible. So, callback code has
181 + a pretty free hand. But to be a good citizen, callback code should
182 + never block for long periods. It is fine to block in
183 + <function>kmalloc</function> and the like, but never wait for i/o or
184 + for user mode to do something. If you need the thread to wait, use
185 + <constant>UTRACE_STOP</constant> and return from the callback
186 + quickly. When your i/o finishes or whatever, you can use
187 + <function>utrace_control</function> to resume the thread.
188 + </para>
190 + <para>
191 + The <constant>UTRACE_EVENT(SYSCALL_ENTRY)</constant> event is a special
192 + case. While other events happen in the kernel when it will return to
193 + user mode soon, this event happens when entering the kernel before it
194 + will proceed with the work requested from user mode. Because of this
195 + difference, the <function>report_syscall_entry</function> callback is
196 + special in two ways. For this event, engines are called in reverse of
197 + the normal order (this includes the <function>report_quiesce</function>
198 + call that precedes a <function>report_syscall_entry</function> call).
199 + This preserves the semantics that the last engine to attach is called
200 + "closest to user mode"--the engine that is first to see a thread's user
201 + state when it enters the kernel is also the last to see that state when
202 + the thread returns to user mode. For the same reason, if these
203 + callbacks use <constant>UTRACE_STOP</constant> (see the next section),
204 + the thread stops immediately after callbacks rather than only when it's
205 + ready to return to user mode; when allowed to resume, it will actually
206 + attempt the system call indicated by the register values at that time.
207 + </para>
209 + </sect1>
211 + <sect1 id="safely"><title>Stopping Safely</title>
213 + <sect2 id="well-behaved"><title>Writing well-behaved callbacks</title>
215 + <para>
216 + Well-behaved callbacks are important to maintain two essential
217 + properties of the interface. The first of these is that unrelated
218 + tracing engines should not interfere with each other. If your engine's
219 + event callback does not return quickly, then another engine won't get
220 + the event notification in a timely manner. The second important
221 + property is that tracing should be as noninvasive as possible to the
222 + normal operation of the system overall and of the traced thread in
223 + particular. That is, attached tracing engines should not perturb a
224 + thread's behavior, except to the extent that changing its user-visible
225 + state is explicitly what you want to do. (Obviously some perturbation
226 + is unavoidable, primarily timing changes, ranging from small delays due
227 + to the overhead of tracing, to arbitrary pauses in user code execution
228 + when a user stops a thread with a debugger for examination.) Even when
229 + you explicitly want the perturbation of making the traced thread block,
230 + just blocking directly in your callback has more unwanted effects. For
231 + example, the <constant>CLONE</constant> event callbacks are called when
232 + the new child thread has been created but not yet started running; the
233 + child can never be scheduled until the <constant>CLONE</constant>
234 + tracing callbacks return. (This allows engines tracing the parent to
235 + attach to the child.) If a <constant>CLONE</constant> event callback
236 + blocks the parent thread, it also prevents the child thread from
237 + running (even to process a <constant>SIGKILL</constant>). If what you
238 + want is to make both the parent and child block, then use
239 + <function>utrace_attach_task</function> on the child and then use
240 + <constant>UTRACE_STOP</constant> on both threads. A more crucial
241 + problem with blocking in callbacks is that it can prevent
242 + <constant>SIGKILL</constant> from working. A thread that is blocking
243 + due to <constant>UTRACE_STOP</constant> will still wake up and die
244 + immediately when sent a <constant>SIGKILL</constant>, as all threads
245 + should. Relying on the <application>utrace</application>
246 + infrastructure rather than on private synchronization calls in event
247 + callbacks is an important way to help keep tracing robustly
248 + noninvasive.
249 + </para>
251 + </sect2>
253 + <sect2 id="UTRACE_STOP"><title>Using <constant>UTRACE_STOP</constant></title>
255 + <para>
256 + To control another thread and access its state, it must be stopped
257 + with <constant>UTRACE_STOP</constant>. This means that it is
258 + stopped and won't start running again while we access it. When a
259 + thread is not already stopped, <function>utrace_control</function>
260 + returns <constant>-EINPROGRESS</constant> and an engine must wait
261 + for an event callback when the thread is ready to stop. The thread
262 + may be running on another CPU or may be blocked. When it is ready
263 + to be examined, it will make callbacks to engines that set the
264 + <constant>UTRACE_EVENT(QUIESCE)</constant> event bit. To wake up an
265 + interruptible wait, use <constant>UTRACE_INTERRUPT</constant>.
266 + </para>
268 + <para>
269 + As long as some engine has used <constant>UTRACE_STOP</constant> and
270 + not called <function>utrace_control</function> to resume the thread,
271 + then the thread will remain stopped. <constant>SIGKILL</constant>
272 + will wake it up, but it will not run user code. When the stop is
273 + cleared with <function>utrace_control</function> or a callback
274 + return value, the thread starts running again.
275 + (See also <xref linkend="teardown"/>.)
276 + </para>
278 + </sect2>
280 + </sect1>
282 + <sect1 id="teardown"><title>Tear-down Races</title>
284 + <sect2 id="SIGKILL"><title>Primacy of <constant>SIGKILL</constant></title>
285 + <para>
286 + Ordinarily synchronization issues for tracing engines are kept fairly
287 + straightforward by using <constant>UTRACE_STOP</constant>. You ask a
288 + thread to stop, and then once it makes the
289 + <function>report_quiesce</function> callback it cannot do anything else
290 + that would result in another callback, until you let it with a
291 + <function>utrace_control</function> call. This simple arrangement
292 + avoids complex and error-prone code in each one of a tracing engine's
293 + event callbacks to keep them serialized with the engine's other
294 + operations done on that thread from another thread of control.
295 + However, giving tracing engines complete power to keep a traced thread
296 + stuck in place runs afoul of a more important kind of simplicity that
297 + the kernel overall guarantees: nothing can prevent or delay
298 + <constant>SIGKILL</constant> from making a thread die and release its
299 + resources. To preserve this important property of
300 + <constant>SIGKILL</constant>, it as a special case can break
301 + <constant>UTRACE_STOP</constant> like nothing else normally can. This
302 + includes both explicit <constant>SIGKILL</constant> signals and the
303 + implicit <constant>SIGKILL</constant> sent to each other thread in the
304 + same thread group by a thread doing an exec, or processing a fatal
305 + signal, or making an <function>exit_group</function> system call. A
306 + tracing engine can prevent a thread from beginning the exit or exec or
307 + dying by signal (other than <constant>SIGKILL</constant>) if it is
308 + attached to that thread, but once the operation begins, no tracing
309 + engine can prevent or delay all other threads in the same thread group
310 + dying.
311 + </para>
312 + </sect2>
314 + <sect2 id="reap"><title>Final callbacks</title>
315 + <para>
316 + The <function>report_reap</function> callback is always the final event
317 + in the life cycle of a traced thread. Tracing engines can use this as
318 + the trigger to clean up their own data structures. The
319 + <function>report_death</function> callback is always the penultimate
320 + event a tracing engine might see; it's seen unless the thread was
321 + already in the midst of dying when the engine attached. Many tracing
322 + engines will have no interest in when a parent reaps a dead process,
323 + and nothing they want to do with a zombie thread once it dies; for
324 + them, the <function>report_death</function> callback is the natural
325 + place to clean up data structures and detach. To facilitate writing
326 + such engines robustly, given the asynchrony of
327 + <constant>SIGKILL</constant>, and without error-prone manual
328 + implementation of synchronization schemes, the
329 + <application>utrace</application> infrastructure provides some special
330 + guarantees about the <function>report_death</function> and
331 + <function>report_reap</function> callbacks. It still takes some care
332 + to be sure your tracing engine is robust to tear-down races, but these
333 + rules make it reasonably straightforward and concise to handle a lot of
334 + corner cases correctly.
335 + </para>
336 + </sect2>
338 + <sect2 id="refcount"><title>Engine and task pointers</title>
339 + <para>
340 + The first sort of guarantee concerns the core data structures
341 + themselves. <structname>struct utrace_engine</structname> is
342 + a reference-counted data structure. While you hold a reference, an
343 + engine pointer will always stay valid so that you can safely pass it to
344 + any <application>utrace</application> call. Each call to
345 + <function>utrace_attach_task</function> or
346 + <function>utrace_attach_pid</function> returns an engine pointer with a
347 + reference belonging to the caller. You own that reference until you
348 + drop it using <function>utrace_engine_put</function>. There is an
349 + implicit reference on the engine while it is attached. So if you drop
350 + your only reference, and then use
351 + <function>utrace_attach_task</function> without
352 + <constant>UTRACE_ATTACH_CREATE</constant> to look up that same engine,
353 + you will get the same pointer with a new reference to replace the one
354 + you dropped, just like calling <function>utrace_engine_get</function>.
355 + When an engine has been detached, either explicitly with
356 + <constant>UTRACE_DETACH</constant> or implicitly after
357 + <function>report_reap</function>, then any references you hold are all
358 + that keep the old engine pointer alive.
359 + </para>
361 + <para>
362 + There is nothing a kernel module can do to keep a <structname>struct
363 + task_struct</structname> alive outside of
364 + <function>rcu_read_lock</function>. When the task dies and is reaped
365 + by its parent (or itself), that structure can be freed so that any
366 + dangling pointers you have stored become invalid.
367 + <application>utrace</application> will not prevent this, but it can
368 + help you detect it safely. By definition, a task that has been reaped
369 + has had all its engines detached. All
370 + <application>utrace</application> calls can be safely called on a
371 + detached engine if the caller holds a reference on that engine pointer,
372 + even if the task pointer passed in the call is invalid. All calls
373 + return <constant>-ESRCH</constant> for a detached engine, which tells
374 + you that the task pointer you passed could be invalid now. Since
375 + <function>utrace_control</function> and
376 + <function>utrace_set_events</function> do not block, you can call those
377 + inside a <function>rcu_read_lock</function> section and be sure after
378 + they don't return <constant>-ESRCH</constant> that the task pointer is
379 + still valid until <function>rcu_read_unlock</function>. The
380 + infrastructure never holds task references of its own. Though neither
381 + <function>rcu_read_lock</function> nor any other lock is held while
382 + making a callback, it's always guaranteed that the <structname>struct
383 + task_struct</structname> and the <structname>struct
384 + utrace_engine</structname> passed as arguments remain valid
385 + until the callback function returns.
386 + </para>
388 + <para>
389 + The common means for safely holding task pointers that is available to
390 + kernel modules is to use <structname>struct pid</structname>, which
391 + permits <function>put_pid</function> from kernel modules. When using
392 + that, the calls <function>utrace_attach_pid</function>,
393 + <function>utrace_control_pid</function>,
394 + <function>utrace_set_events_pid</function>, and
395 + <function>utrace_barrier_pid</function> are available.
396 + </para>
397 + </sect2>
399 + <sect2 id="reap-after-death">
400 + <title>
401 + Serialization of <constant>DEATH</constant> and <constant>REAP</constant>
402 + </title>
403 + <para>
404 + The second guarantee is the serialization of
405 + <constant>DEATH</constant> and <constant>REAP</constant> event
406 + callbacks for a given thread. The actual reaping by the parent
407 + (<function>release_task</function> call) can occur simultaneously
408 + while the thread is still doing the final steps of dying, including
409 + the <function>report_death</function> callback. If a tracing engine
410 + has requested both <constant>DEATH</constant> and
411 + <constant>REAP</constant> event reports, it's guaranteed that the
412 + <function>report_reap</function> callback will not be made until
413 + after the <function>report_death</function> callback has returned.
414 + If the <function>report_death</function> callback itself detaches
415 + from the thread, then the <function>report_reap</function> callback
416 + will never be made. Thus it is safe for a
417 + <function>report_death</function> callback to clean up data
418 + structures and detach.
419 + </para>
420 + </sect2>
422 + <sect2 id="interlock"><title>Interlock with final callbacks</title>
423 + <para>
424 + The final sort of guarantee is that a tracing engine will know for sure
425 + whether or not the <function>report_death</function> and/or
426 + <function>report_reap</function> callbacks will be made for a certain
427 + thread. These tear-down races are disambiguated by the error return
428 + values of <function>utrace_set_events</function> and
429 + <function>utrace_control</function>. Normally
430 + <function>utrace_control</function> called with
431 + <constant>UTRACE_DETACH</constant> returns zero, and this means that no
432 + more callbacks will be made. If the thread is in the midst of dying,
433 + it returns <constant>-EALREADY</constant> to indicate that the
434 + <constant>report_death</constant> callback may already be in progress;
435 + when you get this error, you know that any cleanup your
436 + <function>report_death</function> callback does is about to happen or
437 + has just happened--note that if the <function>report_death</function>
438 + callback does not detach, the engine remains attached until the thread
439 + gets reaped. If the thread is in the midst of being reaped,
440 + <function>utrace_control</function> returns <constant>-ESRCH</constant>
441 + to indicate that the <function>report_reap</function> callback may
442 + already be in progress; this means the engine is implicitly detached
443 + when the callback completes. This makes it possible for a tracing
444 + engine that has decided asynchronously to detach from a thread to
445 + safely clean up its data structures, knowing that no
446 + <function>report_death</function> or <function>report_reap</function>
447 + callback will try to do the same. <constant>utrace_detach</constant>
448 + returns <constant>-ESRCH</constant> when the <structname>struct
449 + utrace_engine</structname> has already been detached, but is
450 + still a valid pointer because of its reference count. A tracing engine
451 + can use this to safely synchronize its own independent multiple threads
452 + of control with each other and with its event callbacks that detach.
453 + </para>
455 + <para>
456 + In the same vein, <function>utrace_set_events</function> normally
457 + returns zero; if the target thread was stopped before the call, then
458 + after a successful call, no event callbacks not requested in the new
459 + flags will be made. It fails with <constant>-EALREADY</constant> if
460 + you try to clear <constant>UTRACE_EVENT(DEATH)</constant> when the
461 + <function>report_death</function> callback may already have begun, or if
462 + you try to newly set <constant>UTRACE_EVENT(DEATH)</constant> or
463 + <constant>UTRACE_EVENT(QUIESCE)</constant> when the target is already
464 + dead or dying. Like <function>utrace_control</function>, it returns
465 + <constant>-ESRCH</constant> when the <function>report_reap</function>
466 + callback may already have begun, or the thread has already been detached
467 + (including forcible detach on reaping). This lets the tracing engine
468 + know for sure which event callbacks it will or won't see after
469 + <function>utrace_set_events</function> has returned. By checking for
470 + errors, it can know whether to clean up its data structures immediately
471 + or to let its callbacks do the work.
472 + </para>
473 + </sect2>
475 + <sect2 id="barrier"><title>Using <function>utrace_barrier</function></title>
476 + <para>
477 + When a thread is safely stopped, calling
478 + <function>utrace_control</function> with <constant>UTRACE_DETACH</constant>
479 + or calling <function>utrace_set_events</function> to disable some events
480 + ensures synchronously that your engine won't get any more of the callbacks
481 + that have been disabled (none at all when detaching). But these can also
482 + be used while the thread is not stopped, when it might be simultaneously
483 + making a callback to your engine. For this situation, these calls return
484 + <constant>-EINPROGRESS</constant> when it's possible a callback is in
485 + progress. If you are not prepared to have your old callbacks still run,
486 + then you can synchronize to be sure all the old callbacks are finished,
487 + using <function>utrace_barrier</function>. This is necessary if the
488 + kernel module containing your callback code is going to be unloaded.
489 + </para>
490 + <para>
491 + After using <constant>UTRACE_DETACH</constant> once, further calls to
492 + <function>utrace_control</function> with the same engine pointer will
493 + return <constant>-ESRCH</constant>. In contrast, after getting
494 + <constant>-EINPROGRESS</constant> from
495 + <function>utrace_set_events</function>, you can call
496 + <function>utrace_set_events</function> again later and if it returns zero
497 + then know the old callbacks have finished.
498 + </para>
499 + <para>
500 + Unlike all other calls, <function>utrace_barrier</function> (and
501 + <function>utrace_barrier_pid</function>) will accept any engine pointer you
502 + hold a reference on, even if <constant>UTRACE_DETACH</constant> has already
503 + been used. After any <function>utrace_control</function> or
504 + <function>utrace_set_events</function> call (these do not block), you can
505 + call <function>utrace_barrier</function> to block until callbacks have
506 + finished. This returns <constant>-ESRCH</constant> only if the engine is
507 + completely detached (finished all callbacks). Otherwise it waits
508 + until the thread is definitely not in the midst of a callback to this
509 + engine and then returns zero, but can return
510 + <constant>-ERESTARTSYS</constant> if its wait is interrupted.
511 + </para>
512 + </sect2>
514 +</sect1>
516 +</chapter>
518 +<chapter id="core"><title>utrace core API</title>
520 +<para>
521 + The utrace API is declared in <filename>&lt;linux/utrace.h&gt;</filename>.
522 +</para>
524 +!Iinclude/linux/utrace.h
525 +!Ekernel/utrace.c
527 +</chapter>
529 +<chapter id="machine"><title>Machine State</title>
531 +<para>
532 + The <function>task_current_syscall</function> function can be used on any
533 + valid <structname>struct task_struct</structname> at any time, and does
534 + not even require that <function>utrace_attach_task</function> was used at all.
535 +</para>
537 +<para>
538 + The other ways to access the registers and other machine-dependent state of
539 + a task can only be used on a task that is at a known safe point. The safe
540 + points are all the places where <function>utrace_set_events</function> can
541 + request callbacks (except for the <constant>DEATH</constant> and
542 + <constant>REAP</constant> events). So at any event callback, it is safe to
543 + examine <varname>current</varname>.
544 +</para>
546 +<para>
547 + One task can examine another only after a callback in the target task that
548 + returns <constant>UTRACE_STOP</constant> so that task will not return to user
549 + mode after the safe point. This guarantees that the task will not resume
550 + until the same engine uses <function>utrace_control</function>, unless the
551 + task dies suddenly. To examine safely, one must use a pair of calls to
552 + <function>utrace_prepare_examine</function> and
553 + <function>utrace_finish_examine</function> surrounding the calls to
554 + <structname>struct user_regset</structname> functions or direct examination
555 + of task data structures. <function>utrace_prepare_examine</function> returns
556 + an error if the task is not properly stopped, or is dead. After a
557 + successful examination, the paired <function>utrace_finish_examine</function>
558 + call returns an error if the task ever woke up during the examination. If
559 + so, any data gathered may be scrambled and should be discarded. This means
560 + there was a spurious wake-up (which should not happen), or a sudden death.
561 +</para>
563 +<sect1 id="regset"><title><structname>struct user_regset</structname></title>
565 +<para>
566 + The <structname>struct user_regset</structname> API
567 + is declared in <filename>&lt;linux/regset.h&gt;</filename>.
568 +</para>
570 +!Finclude/linux/regset.h
572 +</sect1>
574 +<sect1 id="task_current_syscall">
575 + <title><filename>System Call Information</filename></title>
577 +<para>
578 + This function is declared in <filename>&lt;linux/ptrace.h&gt;</filename>.
579 +</para>
581 +!Elib/syscall.c
583 +</sect1>
585 +<sect1 id="syscall"><title><filename>System Call Tracing</filename></title>
587 +<para>
588 + The arch API for system call information is declared in
589 + <filename>&lt;asm/syscall.h&gt;</filename>.
590 + Each of these calls can be used only at system call entry tracing,
591 + or can be used only at system call exit and the subsequent safe points
592 + before returning to user mode.
593 + At system call entry tracing means either during a
594 + <structfield>report_syscall_entry</structfield> callback,
595 + or any time after that callback has returned <constant>UTRACE_STOP</constant>.
596 +</para>
598 +!Finclude/asm-generic/syscall.h
600 +</sect1>
602 +</chapter>
604 +<chapter id="internals"><title>Kernel Internals</title>
606 +<para>
607 + This chapter covers the interface to the tracing infrastructure
608 + from the core of the kernel and the architecture-specific code.
609 + This is for maintainers of the kernel and arch code, and not relevant
610 + to using the tracing facilities described in preceding chapters.
611 +</para>
613 +<sect1 id="tracehook"><title>Core Calls In</title>
615 +<para>
616 + These calls are declared in <filename>&lt;linux/tracehook.h&gt;</filename>.
617 + The core kernel calls these functions at various important places.
618 +</para>
620 +!Finclude/linux/tracehook.h
622 +</sect1>
624 +<sect1 id="arch"><title>Architecture Calls Out</title>
626 +<para>
627 + An arch that has done all these things sets
628 + <constant>CONFIG_HAVE_ARCH_TRACEHOOK</constant>.
629 + This is required to enable the <application>utrace</application> code.
630 +</para>
632 +<sect2 id="arch-ptrace"><title><filename>&lt;asm/ptrace.h&gt;</filename></title>
634 +<para>
635 + An arch defines these in <filename>&lt;asm/ptrace.h&gt;</filename>
636 + if it supports hardware single-step or block-step features.
637 +</para>
639 +!Finclude/linux/ptrace.h arch_has_single_step arch_has_block_step
640 +!Finclude/linux/ptrace.h user_enable_single_step user_enable_block_step
641 +!Finclude/linux/ptrace.h user_disable_single_step
643 +</sect2>
645 +<sect2 id="arch-syscall">
646 + <title><filename>&lt;asm/syscall.h&gt;</filename></title>
648 + <para>
649 + An arch provides <filename>&lt;asm/syscall.h&gt;</filename> that
650 + defines these as inlines, or declares them as exported functions.
651 + These interfaces are described in <xref linkend="syscall"/>.
652 + </para>
654 +</sect2>
656 +<sect2 id="arch-tracehook">
657 + <title><filename>&lt;linux/tracehook.h&gt;</filename></title>
659 + <para>
660 + An arch must define <constant>TIF_NOTIFY_RESUME</constant>
661 + and <constant>TIF_SYSCALL_TRACE</constant>
662 + in its <filename>&lt;asm/thread_info.h&gt;</filename>.
663 + The arch code must call the following functions, all declared
664 + in <filename>&lt;linux/tracehook.h&gt;</filename> and
665 + described in <xref linkend="tracehook"/>:
667 + <itemizedlist>
668 + <listitem>
669 + <para><function>tracehook_notify_resume</function></para>
670 + </listitem>
671 + <listitem>
672 + <para><function>tracehook_report_syscall_entry</function></para>
673 + </listitem>
674 + <listitem>
675 + <para><function>tracehook_report_syscall_exit</function></para>
676 + </listitem>
677 + <listitem>
678 + <para><function>tracehook_signal_handler</function></para>
679 + </listitem>
680 + </itemizedlist>
682 + </para>
684 +</sect2>
686 +</sect1>
688 +</chapter>
690 +</book>
691 diff --git a/fs/proc/array.c b/fs/proc/array.c
692 index 3a1dafd..f0c0ea2 100644
693 --- a/fs/proc/array.c
694 +++ b/fs/proc/array.c
695 @@ -81,6 +81,7 @@
696 #include <linux/pid_namespace.h>
697 #include <linux/ptrace.h>
698 #include <linux/tracehook.h>
699 +#include <linux/utrace.h>
701 #include <asm/pgtable.h>
702 #include <asm/processor.h>
703 @@ -192,6 +193,8 @@ static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
704 cred->uid, cred->euid, cred->suid, cred->fsuid,
705 cred->gid, cred->egid, cred->sgid, cred->fsgid);
707 + task_utrace_proc_status(m, p);
709 task_lock(p);
710 if (p->files)
711 fdt = files_fdtable(p->files);
712 diff --git a/include/linux/sched.h b/include/linux/sched.h
713 index 68daf4f..4d45f93 100644
714 --- a/include/linux/sched.h
715 +++ b/include/linux/sched.h
716 @@ -1403,6 +1403,11 @@ struct task_struct {
717 #endif
718 seccomp_t seccomp;
720 +#ifdef CONFIG_UTRACE
721 + struct utrace *utrace;
722 + unsigned long utrace_flags;
723 +#endif
725 /* Thread group tracking */
726 u32 parent_exec_id;
727 u32 self_exec_id;
728 diff --git a/include/linux/utrace.h b/include/linux/utrace.h
729 new file mode 100644
730 index 0000000..f251efe
731 --- /dev/null
732 +++ b/include/linux/utrace.h
733 @@ -0,0 +1,692 @@
735 + * utrace infrastructure interface for debugging user processes
737 + * Copyright (C) 2006-2009 Red Hat, Inc. All rights reserved.
739 + * This copyrighted material is made available to anyone wishing to use,
740 + * modify, copy, or redistribute it subject to the terms and conditions
741 + * of the GNU General Public License v.2.
743 + * Red Hat Author: Roland McGrath.
745 + * This interface allows for notification of interesting events in a
746 + * thread. It also mediates access to thread state such as registers.
747 + * Multiple unrelated users can be associated with a single thread.
748 + * We call each of these a tracing engine.
750 + * A tracing engine starts by calling utrace_attach_task() or
751 + * utrace_attach_pid() on the chosen thread, passing in a set of hooks
752 + * (&struct utrace_engine_ops), and some associated data. This produces a
753 + * &struct utrace_engine, which is the handle used for all other
754 + * operations. An attached engine has its ops vector, its data, and an
755 + * event mask controlled by utrace_set_events().
757 + * For each event bit that is set, that engine will get the
758 + * appropriate ops->report_*() callback when the event occurs. The
759 + * &struct utrace_engine_ops need not provide callbacks for an event
760 + * unless the engine sets one of the associated event bits.
761 + */
763 +#ifndef _LINUX_UTRACE_H
764 +#define _LINUX_UTRACE_H 1
766 +#include <linux/list.h>
767 +#include <linux/kref.h>
768 +#include <linux/signal.h>
769 +#include <linux/sched.h>
771 +struct linux_binprm;
772 +struct pt_regs;
773 +struct utrace;
774 +struct user_regset;
775 +struct user_regset_view;
778 + * Event bits passed to utrace_set_events().
779 + * These appear in &struct task_struct.@utrace_flags
780 + * and &struct utrace_engine.@flags.
781 + */
782 +enum utrace_events {
783 + _UTRACE_EVENT_QUIESCE, /* Thread is available for examination. */
784 + _UTRACE_EVENT_REAP, /* Zombie reaped, no more tracing possible. */
785 + _UTRACE_EVENT_CLONE, /* Successful clone/fork/vfork just done. */
786 + _UTRACE_EVENT_EXEC, /* Successful execve just completed. */
787 + _UTRACE_EVENT_EXIT, /* Thread exit in progress. */
788 + _UTRACE_EVENT_DEATH, /* Thread has died. */
789 + _UTRACE_EVENT_SYSCALL_ENTRY, /* User entered kernel for system call. */
790 + _UTRACE_EVENT_SYSCALL_EXIT, /* Returning to user after system call. */
791 + _UTRACE_EVENT_SIGNAL, /* Signal delivery will run a user handler. */
792 + _UTRACE_EVENT_SIGNAL_IGN, /* No-op signal to be delivered. */
793 + _UTRACE_EVENT_SIGNAL_STOP, /* Signal delivery will suspend. */
794 + _UTRACE_EVENT_SIGNAL_TERM, /* Signal delivery will terminate. */
795 + _UTRACE_EVENT_SIGNAL_CORE, /* Signal delivery will dump core. */
796 + _UTRACE_EVENT_JCTL, /* Job control stop or continue completed. */
797 + _UTRACE_NEVENTS
799 +#define UTRACE_EVENT(type) (1UL << _UTRACE_EVENT_##type)
802 + * All the kinds of signal events.
803 + * These all use the @report_signal() callback.
804 + */
805 +#define UTRACE_EVENT_SIGNAL_ALL (UTRACE_EVENT(SIGNAL) \
806 + | UTRACE_EVENT(SIGNAL_IGN) \
807 + | UTRACE_EVENT(SIGNAL_STOP) \
808 + | UTRACE_EVENT(SIGNAL_TERM) \
809 + | UTRACE_EVENT(SIGNAL_CORE))
811 + * Both kinds of syscall events; these call the @report_syscall_entry()
812 + * and @report_syscall_exit() callbacks, respectively.
813 + */
814 +#define UTRACE_EVENT_SYSCALL \
815 + (UTRACE_EVENT(SYSCALL_ENTRY) | UTRACE_EVENT(SYSCALL_EXIT))
818 + * The event reports triggered synchronously by task death.
819 + */
820 +#define _UTRACE_DEATH_EVENTS (UTRACE_EVENT(DEATH) | UTRACE_EVENT(QUIESCE))
823 + * Hooks in <linux/tracehook.h> call these entry points to the utrace dispatch.
824 + */
825 +void utrace_free_task(struct task_struct *);
826 +bool utrace_interrupt_pending(void);
827 +void utrace_resume(struct task_struct *, struct pt_regs *);
828 +void utrace_finish_stop(void);
829 +void utrace_maybe_reap(struct task_struct *, struct utrace *, bool);
830 +int utrace_get_signal(struct task_struct *, struct pt_regs *,
831 + siginfo_t *, struct k_sigaction *);
832 +void utrace_report_clone(unsigned long, struct task_struct *);
833 +void utrace_finish_vfork(struct task_struct *);
834 +void utrace_report_exit(long *exit_code);
835 +void utrace_report_death(struct task_struct *, struct utrace *, bool, int);
836 +void utrace_report_jctl(int notify, int type);
837 +void utrace_report_exec(struct linux_binfmt *, struct linux_binprm *,
838 + struct pt_regs *regs);
839 +bool utrace_report_syscall_entry(struct pt_regs *);
840 +void utrace_report_syscall_exit(struct pt_regs *);
841 +void utrace_signal_handler(struct task_struct *, int);
843 +#ifndef CONFIG_UTRACE
846 + * <linux/tracehook.h> uses these accessors to avoid #ifdef CONFIG_UTRACE.
847 + */
848 +static inline unsigned long task_utrace_flags(struct task_struct *task)
850 + return 0;
852 +static inline struct utrace *task_utrace_struct(struct task_struct *task)
854 + return NULL;
856 +static inline void utrace_init_task(struct task_struct *child)
860 +static inline void task_utrace_proc_status(struct seq_file *m,
861 + struct task_struct *p)
865 +#else /* CONFIG_UTRACE */
867 +static inline unsigned long task_utrace_flags(struct task_struct *task)
869 + return task->utrace_flags;
872 +static inline struct utrace *task_utrace_struct(struct task_struct *task)
874 + struct utrace *utrace;
876 + /*
877 + * This barrier ensures that any prior load of task->utrace_flags
878 + * is ordered before this load of task->utrace. We use those
879 + * utrace_flags checks in the hot path to decide to call into
880 + * the utrace code. The first attach installs task->utrace before
881 + * setting task->utrace_flags nonzero with implicit barrier in
882 + * between, see utrace_add_engine().
883 + */
884 + smp_rmb();
885 + utrace = task->utrace;
887 + smp_read_barrier_depends(); /* See utrace_task_alloc(). */
888 + return utrace;
891 +static inline void utrace_init_task(struct task_struct *task)
893 + task->utrace_flags = 0;
894 + task->utrace = NULL;
897 +void task_utrace_proc_status(struct seq_file *m, struct task_struct *p);
901 + * Version number of the API defined in this file. This will change
902 + * whenever a tracing engine's code would need some updates to keep
903 + * working. We maintain this here for the benefit of tracing engine code
904 + * that is developed concurrently with utrace API improvements before they
905 + * are merged into the kernel, making LINUX_VERSION_CODE checks unwieldy.
906 + */
907 +#define UTRACE_API_VERSION 20091216
909 +/**
910 + * enum utrace_resume_action - engine's choice of action for a traced task
911 + * @UTRACE_STOP: Stay quiescent after callbacks.
912 + * @UTRACE_INTERRUPT: Make @report_signal() callback soon.
913 + * @UTRACE_REPORT: Make some callback soon.
914 + * @UTRACE_SINGLESTEP: Resume in user mode for one instruction.
915 + * @UTRACE_BLOCKSTEP: Resume in user mode until next branch.
916 + * @UTRACE_RESUME: Resume normally in user mode.
917 + * @UTRACE_DETACH: Detach my engine (implies %UTRACE_RESUME).
919 + * See utrace_control() for detailed descriptions of each action. This is
920 + * encoded in the @action argument and the return value for every callback
921 + * with a &u32 return value.
923 + * The order of these is important. When there is more than one engine,
924 + * each supplies its choice and the smallest value prevails.
925 + */
926 +enum utrace_resume_action {
927 + UTRACE_STOP,
928 + UTRACE_INTERRUPT,
929 + UTRACE_REPORT,
930 + UTRACE_SINGLESTEP,
931 + UTRACE_BLOCKSTEP,
932 + UTRACE_RESUME,
933 + UTRACE_DETACH,
934 + UTRACE_RESUME_MAX
936 +#define UTRACE_RESUME_BITS (ilog2(UTRACE_RESUME_MAX) + 1)
937 +#define UTRACE_RESUME_MASK ((1 << UTRACE_RESUME_BITS) - 1)
939 +/**
940 + * utrace_resume_action - &enum utrace_resume_action from callback action
941 + * @action: &u32 callback @action argument or return value
943 + * This extracts the &enum utrace_resume_action from @action,
944 + * which is the @action argument to a &struct utrace_engine_ops
945 + * callback or the return value from one.
946 + */
947 +static inline enum utrace_resume_action utrace_resume_action(u32 action)
949 + return action & UTRACE_RESUME_MASK;
952 +/**
953 + * enum utrace_signal_action - disposition of signal
954 + * @UTRACE_SIGNAL_DELIVER: Deliver according to sigaction.
955 + * @UTRACE_SIGNAL_IGN: Ignore the signal.
956 + * @UTRACE_SIGNAL_TERM: Terminate the process.
957 + * @UTRACE_SIGNAL_CORE: Terminate with core dump.
958 + * @UTRACE_SIGNAL_STOP: Deliver as absolute stop.
959 + * @UTRACE_SIGNAL_TSTP: Deliver as job control stop.
960 + * @UTRACE_SIGNAL_REPORT: Reporting before pending signals.
961 + * @UTRACE_SIGNAL_HANDLER: Reporting after signal handler setup.
963 + * This is encoded in the @action argument and the return value for
964 + * a @report_signal() callback. It says what will happen to the
965 + * signal described by the &siginfo_t parameter to the callback.
967 + * The %UTRACE_SIGNAL_REPORT value is used in an @action argument when
968 + * a tracing report is being made before dequeuing any pending signal.
969 + * If this is immediately after a signal handler has been set up, then
970 + * %UTRACE_SIGNAL_HANDLER is used instead. A @report_signal callback
971 + * that uses %UTRACE_SIGNAL_DELIVER|%UTRACE_SINGLESTEP will ensure
972 + * it sees a %UTRACE_SIGNAL_HANDLER report.
973 + */
974 +enum utrace_signal_action {
975 + UTRACE_SIGNAL_DELIVER = 0x00,
976 + UTRACE_SIGNAL_IGN = 0x10,
977 + UTRACE_SIGNAL_TERM = 0x20,
978 + UTRACE_SIGNAL_CORE = 0x30,
979 + UTRACE_SIGNAL_STOP = 0x40,
980 + UTRACE_SIGNAL_TSTP = 0x50,
981 + UTRACE_SIGNAL_REPORT = 0x60,
982 + UTRACE_SIGNAL_HANDLER = 0x70
984 +#define UTRACE_SIGNAL_MASK 0xf0
985 +#define UTRACE_SIGNAL_HOLD 0x100 /* Flag, push signal back on queue. */
987 +/**
988 + * utrace_signal_action - &enum utrace_signal_action from callback action
989 + * @action: @report_signal callback @action argument or return value
991 + * This extracts the &enum utrace_signal_action from @action, which
992 + * is the @action argument to a @report_signal callback or the
993 + * return value from one.
994 + */
995 +static inline enum utrace_signal_action utrace_signal_action(u32 action)
997 + return action & UTRACE_SIGNAL_MASK;
1000 +/**
1001 + * enum utrace_syscall_action - disposition of system call attempt
1002 + * @UTRACE_SYSCALL_RUN: Run the system call.
1003 + * @UTRACE_SYSCALL_ABORT: Don't run the system call.
1005 + * This is encoded in the @action argument and the return value for
1006 + * a @report_syscall_entry callback.
1007 + */
1008 +enum utrace_syscall_action {
1009 + UTRACE_SYSCALL_RUN = 0x00,
1010 + UTRACE_SYSCALL_ABORT = 0x10
1012 +#define UTRACE_SYSCALL_MASK 0xf0
1013 +#define UTRACE_SYSCALL_RESUMED 0x100 /* Flag, report_syscall_entry() repeats */
1015 +/**
1016 + * utrace_syscall_action - &enum utrace_syscall_action from callback action
1017 + * @action: @report_syscall_entry callback @action or return value
1019 + * This extracts the &enum utrace_syscall_action from @action, which
1020 + * is the @action argument to a @report_syscall_entry callback or the
1021 + * return value from one.
1022 + */
1023 +static inline enum utrace_syscall_action utrace_syscall_action(u32 action)
1025 + return action & UTRACE_SYSCALL_MASK;
1029 + * Flags for utrace_attach_task() and utrace_attach_pid().
1030 + */
1031 +#define UTRACE_ATTACH_MATCH_OPS 0x0001 /* Match engines on ops. */
1032 +#define UTRACE_ATTACH_MATCH_DATA 0x0002 /* Match engines on data. */
1033 +#define UTRACE_ATTACH_MATCH_MASK 0x000f
1034 +#define UTRACE_ATTACH_CREATE 0x0010 /* Attach a new engine. */
1035 +#define UTRACE_ATTACH_EXCLUSIVE 0x0020 /* Refuse if existing match. */
1037 +/**
1038 + * struct utrace_engine - per-engine structure
1039 + * @ops: &struct utrace_engine_ops pointer passed to utrace_attach_task()
1040 + * @data: engine-private &void * passed to utrace_attach_task()
1041 + * @flags: event mask set by utrace_set_events() plus internal flag bits
1043 + * The task itself never has to worry about engines detaching while
1044 + * it's doing event callbacks. These structures are removed from the
1045 + * task's active list only when it's stopped, or by the task itself.
1047 + * utrace_engine_get() and utrace_engine_put() maintain a reference count.
1048 + * When it drops to zero, the structure is freed. One reference is held
1049 + * implicitly while the engine is attached to its task.
1050 + */
1051 +struct utrace_engine {
1052 +/* private: */
1053 + struct kref kref;
1054 + void (*release)(void *);
1055 + struct list_head entry;
1057 +/* public: */
1058 + const struct utrace_engine_ops *ops;
1059 + void *data;
1061 + unsigned long flags;
1064 +/**
1065 + * utrace_engine_get - acquire a reference on a &struct utrace_engine
1066 + * @engine: &struct utrace_engine pointer
1068 + * You must hold a reference on @engine, and you get another.
1069 + */
1070 +static inline void utrace_engine_get(struct utrace_engine *engine)
1072 + kref_get(&engine->kref);
1075 +void __utrace_engine_release(struct kref *);
1077 +/**
1078 + * utrace_engine_put - release a reference on a &struct utrace_engine
1079 + * @engine: &struct utrace_engine pointer
1081 + * You must hold a reference on @engine, and you lose that reference.
1082 + * If it was the last one, @engine becomes an invalid pointer.
1083 + */
1084 +static inline void utrace_engine_put(struct utrace_engine *engine)
1086 + kref_put(&engine->kref, __utrace_engine_release);
1089 +/**
1090 + * struct utrace_engine_ops - tracing engine callbacks
1092 + * Each @report_*() callback corresponds to an %UTRACE_EVENT(*) bit.
1093 + * utrace_set_events() calls on @engine choose which callbacks will
1094 + * be made to @engine from @task.
1096 + * Most callbacks take an @action argument, giving the resume action
1097 + * chosen by other tracing engines. All callbacks take an @engine
1098 + * argument. The @report_reap callback takes a @task argument that
1099 + * might or might not be @current. All other @report_* callbacks
1100 + * report an event in the @current task.
1102 + * For some calls, @action also includes bits specific to that event
1103 + * and utrace_resume_action() is used to extract the resume action.
1104 + * This shows what would happen if @engine wasn't there, or will if
1105 + * the callback's return value uses %UTRACE_RESUME. This always
1106 + * starts as %UTRACE_RESUME when no other tracing is being done on
1107 + * this task.
1109 + * All return values contain &enum utrace_resume_action bits. For
1110 + * some calls, other bits specific to that kind of event are added to
1111 + * the resume action bits with OR. These are the same bits used in
1112 + * the @action argument. The resume action returned by a callback
1113 + * does not override previous engines' choices, it only says what
1114 + * @engine wants done. What @current actually does is the action that's
1115 + * most constrained among the choices made by all attached engines.
1116 + * See utrace_control() for more information on the actions.
1118 + * When %UTRACE_STOP is used in @report_syscall_entry, then @current
1119 + * stops before attempting the system call. In this case, another
1120 + * @report_syscall_entry callback will follow after @current resumes if
1121 + * %UTRACE_REPORT or %UTRACE_INTERRUPT was returned by some callback
1122 + * or passed to utrace_control(). In a second or later callback,
1123 + * %UTRACE_SYSCALL_RESUMED is set in the @action argument to indicate
1124 + * a repeat callback still waiting to attempt the same system call
1125 + * invocation. This repeat callback gives each engine an opportunity
1126 + * to reexamine registers another engine might have changed while
1127 + * @current was held in %UTRACE_STOP.
1129 + * In other cases, the resume action does not take effect until @current
1130 + * is ready to check for signals and return to user mode. If there
1131 + * are more callbacks to be made, the last round of calls determines
1132 + * the final action. A @report_quiesce callback with @event zero, or
1133 + * a @report_signal callback, will always be the last one made before
1134 + * @current resumes. Only %UTRACE_STOP is "sticky"--if @engine returned
1135 + * %UTRACE_STOP then @current stays stopped unless @engine returns
1136 + * different from a following callback.
1138 + * The report_death() and report_reap() callbacks do not take @action
1139 + * arguments, and only %UTRACE_DETACH is meaningful in the return value
1140 + * from a report_death() callback. None of the resume actions applies
1141 + * to a dead thread.
1143 + * All @report_*() hooks are called with no locks held, in a generally
1144 + * safe environment when we will be returning to user mode soon (or just
1145 + * entered the kernel). It is fine to block for memory allocation and
1146 + * the like, but all hooks are asynchronous and must not block on
1147 + * external events! If you want the thread to block, use %UTRACE_STOP
1148 + * in your hook's return value; then later wake it up with utrace_control().
1150 + * @report_quiesce:
1151 + * Requested by %UTRACE_EVENT(%QUIESCE).
1152 + * This does not indicate any event, but just that @current is in a
1153 + * safe place for examination. This call is made before each specific
1154 + * event callback, except for @report_reap. The @event argument gives
1155 + * the %UTRACE_EVENT(@which) value for the event occurring. This
1156 + * callback might be made for events @engine has not requested, if
1157 + * some other engine is tracing the event; calling utrace_set_events()
1158 + * call here can request the immediate callback for this occurrence of
1159 + * @event. @event is zero when there is no other event, @current is
1160 + * now ready to check for signals and return to user mode, and some
1161 + * engine has used %UTRACE_REPORT or %UTRACE_INTERRUPT to request this
1162 + * callback. For this case, if @report_signal is not %NULL, the
1163 + * @report_quiesce callback may be replaced with a @report_signal
1164 + * callback passing %UTRACE_SIGNAL_REPORT in its @action argument,
1165 + * whenever @current is entering the signal-check path anyway.
1167 + * @report_signal:
1168 + * Requested by %UTRACE_EVENT(%SIGNAL_*) or %UTRACE_EVENT(%QUIESCE).
1169 + * Use utrace_signal_action() and utrace_resume_action() on @action.
1170 + * The signal action is %UTRACE_SIGNAL_REPORT when some engine has
1171 + * used %UTRACE_REPORT or %UTRACE_INTERRUPT; the callback can choose
1172 + * to stop or to deliver an artificial signal, before pending signals.
1173 + * It's %UTRACE_SIGNAL_HANDLER instead when signal handler setup just
1174 + * finished (after a previous %UTRACE_SIGNAL_DELIVER return); this
1175 + * serves in lieu of any %UTRACE_SIGNAL_REPORT callback requested by
1176 + * %UTRACE_REPORT or %UTRACE_INTERRUPT, and is also implicitly
1177 + * requested by %UTRACE_SINGLESTEP or %UTRACE_BLOCKSTEP into the
1178 + * signal delivery. The other signal actions indicate a signal about
1179 + * to be delivered; the previous engine's return value sets the signal
1180 + * action seen by the the following engine's callback. The @info data
1181 + * can be changed at will, including @info->si_signo. The settings in
1182 + * @return_ka determines what %UTRACE_SIGNAL_DELIVER does. @orig_ka
1183 + * is what was in force before other tracing engines intervened, and
1184 + * it's %NULL when this report began as %UTRACE_SIGNAL_REPORT or
1185 + * %UTRACE_SIGNAL_HANDLER. For a report without a new signal, @info
1186 + * is left uninitialized and must be set completely by an engine that
1187 + * chooses to deliver a signal; if there was a previous @report_signal
1188 + * callback ending in %UTRACE_STOP and it was just resumed using
1189 + * %UTRACE_REPORT or %UTRACE_INTERRUPT, then @info is left unchanged
1190 + * from the previous callback. In this way, the original signal can
1191 + * be left in @info while returning %UTRACE_STOP|%UTRACE_SIGNAL_IGN
1192 + * and then found again when resuming with %UTRACE_INTERRUPT.
1193 + * The %UTRACE_SIGNAL_HOLD flag bit can be OR'd into the return value,
1194 + * and might be in @action if the previous engine returned it. This
1195 + * flag asks that the signal in @info be pushed back on @current's queue
1196 + * so that it will be seen again after whatever action is taken now.
1198 + * @report_clone:
1199 + * Requested by %UTRACE_EVENT(%CLONE).
1200 + * Event reported for parent, before the new task @child might run.
1201 + * @clone_flags gives the flags used in the clone system call, or
1202 + * equivalent flags for a fork() or vfork() system call. This
1203 + * function can use utrace_attach_task() on @child. Then passing
1204 + * %UTRACE_STOP to utrace_control() on @child here keeps the child
1205 + * stopped before it ever runs in user mode, %UTRACE_REPORT or
1206 + * %UTRACE_INTERRUPT ensures a callback from @child before it
1207 + * starts in user mode.
1209 + * @report_jctl:
1210 + * Requested by %UTRACE_EVENT(%JCTL).
1211 + * Job control event; @type is %CLD_STOPPED or %CLD_CONTINUED,
1212 + * indicating whether we are stopping or resuming now. If @notify
1213 + * is nonzero, @current is the last thread to stop and so will send
1214 + * %SIGCHLD to its parent after this callback; @notify reflects
1215 + * what the parent's %SIGCHLD has in @si_code, which can sometimes
1216 + * be %CLD_STOPPED even when @type is %CLD_CONTINUED.
1218 + * @report_exec:
1219 + * Requested by %UTRACE_EVENT(%EXEC).
1220 + * An execve system call has succeeded and the new program is about to
1221 + * start running. The initial user register state is handy to be tweaked
1222 + * directly in @regs. @fmt and @bprm gives the details of this exec.
1224 + * @report_syscall_entry:
1225 + * Requested by %UTRACE_EVENT(%SYSCALL_ENTRY).
1226 + * Thread has entered the kernel to request a system call.
1227 + * The user register state is handy to be tweaked directly in @regs.
1228 + * The @action argument contains an &enum utrace_syscall_action,
1229 + * use utrace_syscall_action() to extract it. The return value
1230 + * overrides the last engine's action for the system call.
1231 + * If the final action is %UTRACE_SYSCALL_ABORT, no system call
1232 + * is made. The details of the system call being attempted can
1233 + * be fetched here with syscall_get_nr() and syscall_get_arguments().
1234 + * The parameter registers can be changed with syscall_set_arguments().
1235 + * See above about the %UTRACE_SYSCALL_RESUMED flag in @action.
1236 + * Use %UTRACE_REPORT in the return value to guarantee you get
1237 + * another callback (with %UTRACE_SYSCALL_RESUMED flag) in case
1238 + * @current stops with %UTRACE_STOP before attempting the system call.
1240 + * @report_syscall_exit:
1241 + * Requested by %UTRACE_EVENT(%SYSCALL_EXIT).
1242 + * Thread is about to leave the kernel after a system call request.
1243 + * The user register state is handy to be tweaked directly in @regs.
1244 + * The results of the system call attempt can be examined here using
1245 + * syscall_get_error() and syscall_get_return_value(). It is safe
1246 + * here to call syscall_set_return_value() or syscall_rollback().
1248 + * @report_exit:
1249 + * Requested by %UTRACE_EVENT(%EXIT).
1250 + * Thread is exiting and cannot be prevented from doing so,
1251 + * but all its state is still live. The @code value will be
1252 + * the wait result seen by the parent, and can be changed by
1253 + * this engine or others. The @orig_code value is the real
1254 + * status, not changed by any tracing engine. Returning %UTRACE_STOP
1255 + * here keeps @current stopped before it cleans up its state and dies,
1256 + * so it can be examined by other processes. When @current is allowed
1257 + * to run, it will die and get to the @report_death callback.
1259 + * @report_death:
1260 + * Requested by %UTRACE_EVENT(%DEATH).
1261 + * Thread is really dead now. It might be reaped by its parent at
1262 + * any time, or self-reap immediately. Though the actual reaping
1263 + * may happen in parallel, a report_reap() callback will always be
1264 + * ordered after a report_death() callback.
1266 + * @report_reap:
1267 + * Requested by %UTRACE_EVENT(%REAP).
1268 + * Called when someone reaps the dead task (parent, init, or self).
1269 + * This means the parent called wait, or else this was a detached
1270 + * thread or a process whose parent ignores SIGCHLD.
1271 + * No more callbacks are made after this one.
1272 + * The engine is always detached.
1273 + * There is nothing more a tracing engine can do about this thread.
1274 + * After this callback, the @engine pointer will become invalid.
1275 + * The @task pointer may become invalid if get_task_struct() hasn't
1276 + * been used to keep it alive.
1277 + * An engine should always request this callback if it stores the
1278 + * @engine pointer or stores any pointer in @engine->data, so it
1279 + * can clean up its data structures.
1280 + * Unlike other callbacks, this can be called from the parent's context
1281 + * rather than from the traced thread itself--it must not delay the
1282 + * parent by blocking.
1284 + * @release:
1285 + * If not %NULL, this is called after the last utrace_engine_put()
1286 + * call for a &struct utrace_engine, which could be implicit after
1287 + * a %UTRACE_DETACH return from another callback. Its argument is
1288 + * the engine's @data member.
1289 + */
1290 +struct utrace_engine_ops {
1291 + u32 (*report_quiesce)(u32 action, struct utrace_engine *engine,
1292 + unsigned long event);
1293 + u32 (*report_signal)(u32 action, struct utrace_engine *engine,
1294 + struct pt_regs *regs,
1295 + siginfo_t *info,
1296 + const struct k_sigaction *orig_ka,
1297 + struct k_sigaction *return_ka);
1298 + u32 (*report_clone)(u32 action, struct utrace_engine *engine,
1299 + unsigned long clone_flags,
1300 + struct task_struct *child);
1301 + u32 (*report_jctl)(u32 action, struct utrace_engine *engine,
1302 + int type, int notify);
1303 + u32 (*report_exec)(u32 action, struct utrace_engine *engine,
1304 + const struct linux_binfmt *fmt,
1305 + const struct linux_binprm *bprm,
1306 + struct pt_regs *regs);
1307 + u32 (*report_syscall_entry)(u32 action, struct utrace_engine *engine,
1308 + struct pt_regs *regs);
1309 + u32 (*report_syscall_exit)(u32 action, struct utrace_engine *engine,
1310 + struct pt_regs *regs);
1311 + u32 (*report_exit)(u32 action, struct utrace_engine *engine,
1312 + long orig_code, long *code);
1313 + u32 (*report_death)(struct utrace_engine *engine,
1314 + bool group_dead, int signal);
1315 + void (*report_reap)(struct utrace_engine *engine,
1316 + struct task_struct *task);
1317 + void (*release)(void *data);
1320 +/**
1321 + * struct utrace_examiner - private state for using utrace_prepare_examine()
1323 + * The members of &struct utrace_examiner are private to the implementation.
1324 + * This data type holds the state from a call to utrace_prepare_examine()
1325 + * to be used by a call to utrace_finish_examine().
1326 + */
1327 +struct utrace_examiner {
1328 +/* private: */
1329 + long state;
1330 + unsigned long ncsw;
1334 + * These are the exported entry points for tracing engines to use.
1335 + * See kernel/utrace.c for their kerneldoc comments with interface details.
1336 + */
1337 +struct utrace_engine *utrace_attach_task(struct task_struct *, int,
1338 + const struct utrace_engine_ops *,
1339 + void *);
1340 +struct utrace_engine *utrace_attach_pid(struct pid *, int,
1341 + const struct utrace_engine_ops *,
1342 + void *);
1343 +int __must_check utrace_control(struct task_struct *,
1344 + struct utrace_engine *,
1345 + enum utrace_resume_action);
1346 +int __must_check utrace_set_events(struct task_struct *,
1347 + struct utrace_engine *,
1348 + unsigned long eventmask);
1349 +int __must_check utrace_barrier(struct task_struct *,
1350 + struct utrace_engine *);
1351 +int __must_check utrace_prepare_examine(struct task_struct *,
1352 + struct utrace_engine *,
1353 + struct utrace_examiner *);
1354 +int __must_check utrace_finish_examine(struct task_struct *,
1355 + struct utrace_engine *,
1356 + struct utrace_examiner *);
1358 +/**
1359 + * utrace_control_pid - control a thread being traced by a tracing engine
1360 + * @pid: thread to affect
1361 + * @engine: attached engine to affect
1362 + * @action: &enum utrace_resume_action for thread to do
1364 + * This is the same as utrace_control(), but takes a &struct pid
1365 + * pointer rather than a &struct task_struct pointer. The caller must
1366 + * hold a ref on @pid, but does not need to worry about the task
1367 + * staying valid. If it's been reaped so that @pid points nowhere,
1368 + * then this call returns -%ESRCH.
1369 + */
1370 +static inline __must_check int utrace_control_pid(
1371 + struct pid *pid, struct utrace_engine *engine,
1372 + enum utrace_resume_action action)
1374 + /*
1375 + * We don't bother with rcu_read_lock() here to protect the
1376 + * task_struct pointer, because utrace_control will return
1377 + * -ESRCH without looking at that pointer if the engine is
1378 + * already detached. A task_struct pointer can't die before
1379 + * all the engines are detached in release_task() first.
1380 + */
1381 + struct task_struct *task = pid_task(pid, PIDTYPE_PID);
1382 + return unlikely(!task) ? -ESRCH : utrace_control(task, engine, action);
1385 +/**
1386 + * utrace_set_events_pid - choose which event reports a tracing engine gets
1387 + * @pid: thread to affect
1388 + * @engine: attached engine to affect
1389 + * @eventmask: new event mask
1391 + * This is the same as utrace_set_events(), but takes a &struct pid
1392 + * pointer rather than a &struct task_struct pointer. The caller must
1393 + * hold a ref on @pid, but does not need to worry about the task
1394 + * staying valid. If it's been reaped so that @pid points nowhere,
1395 + * then this call returns -%ESRCH.
1396 + */
1397 +static inline __must_check int utrace_set_events_pid(
1398 + struct pid *pid, struct utrace_engine *engine, unsigned long eventmask)
1400 + struct task_struct *task = pid_task(pid, PIDTYPE_PID);
1401 + return unlikely(!task) ? -ESRCH :
1402 + utrace_set_events(task, engine, eventmask);
1405 +/**
1406 + * utrace_barrier_pid - synchronize with simultaneous tracing callbacks
1407 + * @pid: thread to affect
1408 + * @engine: engine to affect (can be detached)
1410 + * This is the same as utrace_barrier(), but takes a &struct pid
1411 + * pointer rather than a &struct task_struct pointer. The caller must
1412 + * hold a ref on @pid, but does not need to worry about the task
1413 + * staying valid. If it's been reaped so that @pid points nowhere,
1414 + * then this call returns -%ESRCH.
1415 + */
1416 +static inline __must_check int utrace_barrier_pid(struct pid *pid,
1417 + struct utrace_engine *engine)
1419 + struct task_struct *task = pid_task(pid, PIDTYPE_PID);
1420 + return unlikely(!task) ? -ESRCH : utrace_barrier(task, engine);
1423 +#endif /* CONFIG_UTRACE */
1425 +#endif /* linux/utrace.h */
1426 diff --git a/init/Kconfig b/init/Kconfig
1427 index 43298f9..3f670e5 100644
1428 --- a/init/Kconfig
1429 +++ b/init/Kconfig
1430 @@ -372,6 +372,15 @@ config AUDIT_TREE
1431 depends on AUDITSYSCALL
1432 select FSNOTIFY
1434 +config UTRACE
1435 + bool "Infrastructure for tracing and debugging user processes"
1436 + depends on EXPERIMENTAL
1437 + depends on HAVE_ARCH_TRACEHOOK
1438 + help
1439 + Enable the utrace process tracing interface. This is an internal
1440 + kernel interface exported to kernel modules, to track events in
1441 + user threads, extract and change user thread state.
1443 source "kernel/irq/Kconfig"
1445 menu "RCU Subsystem"
1446 diff --git a/kernel/Makefile b/kernel/Makefile
1447 index e898c5b..e43bbfb 100644
1448 --- a/kernel/Makefile
1449 +++ b/kernel/Makefile
1450 @@ -68,6 +68,7 @@ obj-$(CONFIG_IKCONFIG) += configs.o
1451 obj-$(CONFIG_RESOURCE_COUNTERS) += res_counter.o
1452 obj-$(CONFIG_SMP) += stop_machine.o
1453 obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o
1454 +obj-$(CONFIG_UTRACE) += utrace.o
1455 obj-$(CONFIG_AUDIT) += audit.o auditfilter.o
1456 obj-$(CONFIG_AUDITSYSCALL) += auditsc.o
1457 obj-$(CONFIG_AUDIT_WATCH) += audit_watch.o
1458 diff --git a/kernel/utrace.c b/kernel/utrace.c
1459 new file mode 100644
1460 index 0000000..ef856c9
1461 --- /dev/null
1462 +++ b/kernel/utrace.c
1463 @@ -0,0 +1,2440 @@
1465 + * utrace infrastructure interface for debugging user processes
1467 + * Copyright (C) 2006-2010 Red Hat, Inc. All rights reserved.
1469 + * This copyrighted material is made available to anyone wishing to use,
1470 + * modify, copy, or redistribute it subject to the terms and conditions
1471 + * of the GNU General Public License v.2.
1473 + * Red Hat Author: Roland McGrath.
1474 + */
1476 +#include <linux/utrace.h>
1477 +#include <linux/tracehook.h>
1478 +#include <linux/regset.h>
1479 +#include <asm/syscall.h>
1480 +#include <linux/ptrace.h>
1481 +#include <linux/err.h>
1482 +#include <linux/sched.h>
1483 +#include <linux/freezer.h>
1484 +#include <linux/module.h>
1485 +#include <linux/init.h>
1486 +#include <linux/slab.h>
1487 +#include <linux/seq_file.h>
1491 + * Per-thread structure private to utrace implementation.
1492 + * If task_struct.utrace_flags is nonzero, task_struct.utrace
1493 + * has always been allocated first. Once allocated, it is
1494 + * never freed until free_task().
1496 + * The common event reporting loops are done by the task making the
1497 + * report without ever taking any locks. To facilitate this, the two
1498 + * lists @attached and @attaching work together for smooth asynchronous
1499 + * attaching with low overhead. Modifying either list requires @lock.
1500 + * The @attaching list can be modified any time while holding @lock.
1501 + * New engines being attached always go on this list.
1503 + * The @attached list is what the task itself uses for its reporting
1504 + * loops. When the task itself is not quiescent, it can use the
1505 + * @attached list without taking any lock. Nobody may modify the list
1506 + * when the task is not quiescent. When it is quiescent, that means
1507 + * that it won't run again without taking @lock itself before using
1508 + * the list.
1510 + * At each place where we know the task is quiescent (or it's current),
1511 + * while holding @lock, we call splice_attaching(), below. This moves
1512 + * the @attaching list members on to the end of the @attached list.
1513 + * Since this happens at the start of any reporting pass, any new
1514 + * engines attached asynchronously go on the stable @attached list
1515 + * in time to have their callbacks seen.
1516 + */
1517 +struct utrace {
1518 + spinlock_t lock;
1519 + struct list_head attached, attaching;
1521 + struct task_struct *cloning;
1523 + struct utrace_engine *reporting;
1525 + enum utrace_resume_action resume:UTRACE_RESUME_BITS;
1526 + unsigned int signal_handler:1;
1527 + unsigned int vfork_stop:1; /* need utrace_stop() before vfork wait */
1528 + unsigned int death:1; /* in utrace_report_death() now */
1529 + unsigned int reap:1; /* release_task() has run */
1530 + unsigned int pending_attach:1; /* need splice_attaching() */
1533 +static struct kmem_cache *utrace_cachep;
1534 +static struct kmem_cache *utrace_engine_cachep;
1535 +static const struct utrace_engine_ops utrace_detached_ops; /* forward decl */
1537 +static int __init utrace_init(void)
1539 + utrace_cachep = KMEM_CACHE(utrace, SLAB_PANIC);
1540 + utrace_engine_cachep = KMEM_CACHE(utrace_engine, SLAB_PANIC);
1541 + return 0;
1543 +module_init(utrace_init);
1546 + * Set up @task.utrace for the first time. We can have races
1547 + * between two utrace_attach_task() calls here. The task_lock()
1548 + * governs installing the new pointer. If another one got in first,
1549 + * we just punt the new one we allocated.
1551 + * This returns false only in case of a memory allocation failure.
1552 + */
1553 +static bool utrace_task_alloc(struct task_struct *task)
1555 + struct utrace *utrace = kmem_cache_zalloc(utrace_cachep, GFP_KERNEL);
1556 + if (unlikely(!utrace))
1557 + return false;
1558 + spin_lock_init(&utrace->lock);
1559 + INIT_LIST_HEAD(&utrace->attached);
1560 + INIT_LIST_HEAD(&utrace->attaching);
1561 + utrace->resume = UTRACE_RESUME;
1562 + task_lock(task);
1563 + if (likely(!task->utrace)) {
1564 + /*
1565 + * This barrier makes sure the initialization of the struct
1566 + * precedes the installation of the pointer. This pairs
1567 + * with smp_read_barrier_depends() in task_utrace_struct().
1568 + */
1569 + smp_wmb();
1570 + task->utrace = utrace;
1572 + task_unlock(task);
1574 + if (unlikely(task->utrace != utrace))
1575 + kmem_cache_free(utrace_cachep, utrace);
1576 + return true;
1580 + * This is called via tracehook_free_task() from free_task()
1581 + * when @task is being deallocated.
1582 + */
1583 +void utrace_free_task(struct task_struct *task)
1585 + kmem_cache_free(utrace_cachep, task->utrace);
1589 + * This is calledwhen the task is safely quiescent, i.e. it won't consult
1590 + * utrace->attached without the lock. Move any engines attached
1591 + * asynchronously from @utrace->attaching onto the @utrace->attached list.
1592 + */
1593 +static void splice_attaching(struct utrace *utrace)
1595 + lockdep_assert_held(&utrace->lock);
1596 + list_splice_tail_init(&utrace->attaching, &utrace->attached);
1597 + utrace->pending_attach = 0;
1601 + * This is the exported function used by the utrace_engine_put() inline.
1602 + */
1603 +void __utrace_engine_release(struct kref *kref)
1605 + struct utrace_engine *engine = container_of(kref, struct utrace_engine,
1606 + kref);
1607 + BUG_ON(!list_empty(&engine->entry));
1608 + if (engine->release)
1609 + (*engine->release)(engine->data);
1610 + kmem_cache_free(utrace_engine_cachep, engine);
1612 +EXPORT_SYMBOL_GPL(__utrace_engine_release);
1614 +static bool engine_matches(struct utrace_engine *engine, int flags,
1615 + const struct utrace_engine_ops *ops, void *data)
1617 + if ((flags & UTRACE_ATTACH_MATCH_OPS) && engine->ops != ops)
1618 + return false;
1619 + if ((flags & UTRACE_ATTACH_MATCH_DATA) && engine->data != data)
1620 + return false;
1621 + return engine->ops && engine->ops != &utrace_detached_ops;
1624 +static struct utrace_engine *find_matching_engine(
1625 + struct utrace *utrace, int flags,
1626 + const struct utrace_engine_ops *ops, void *data)
1628 + struct utrace_engine *engine;
1629 + list_for_each_entry(engine, &utrace->attached, entry)
1630 + if (engine_matches(engine, flags, ops, data))
1631 + return engine;
1632 + list_for_each_entry(engine, &utrace->attaching, entry)
1633 + if (engine_matches(engine, flags, ops, data))
1634 + return engine;
1635 + return NULL;
1639 + * Enqueue @engine, or maybe don't if UTRACE_ATTACH_EXCLUSIVE.
1640 + */
1641 +static int utrace_add_engine(struct task_struct *target,
1642 + struct utrace *utrace,
1643 + struct utrace_engine *engine,
1644 + int flags,
1645 + const struct utrace_engine_ops *ops,
1646 + void *data)
1648 + int ret;
1650 + spin_lock(&utrace->lock);
1652 + ret = -EEXIST;
1653 + if ((flags & UTRACE_ATTACH_EXCLUSIVE) &&
1654 + unlikely(find_matching_engine(utrace, flags, ops, data)))
1655 + goto unlock;
1657 + /*
1658 + * In case we had no engines before, make sure that
1659 + * utrace_flags is not zero. Since we did unlock+lock
1660 + * at least once after utrace_task_alloc() installed
1661 + * ->utrace, we have the necessary barrier which pairs
1662 + * with rmb() in task_utrace_struct().
1663 + */
1664 + ret = -ESRCH;
1665 + if (!target->utrace_flags) {
1666 + target->utrace_flags = UTRACE_EVENT(REAP);
1667 + /*
1668 + * If we race with tracehook_prepare_release_task()
1669 + * make sure that either it sees utrace_flags != 0
1670 + * or we see exit_state == EXIT_DEAD.
1671 + */
1672 + smp_mb();
1673 + if (unlikely(target->exit_state == EXIT_DEAD)) {
1674 + target->utrace_flags = 0;
1675 + goto unlock;
1679 + /*
1680 + * Put the new engine on the pending ->attaching list.
1681 + * Make sure it gets onto the ->attached list by the next
1682 + * time it's examined. Setting ->pending_attach ensures
1683 + * that start_report() takes the lock and splices the lists
1684 + * before the next new reporting pass.
1686 + * When target == current, it would be safe just to call
1687 + * splice_attaching() right here. But if we're inside a
1688 + * callback, that would mean the new engine also gets
1689 + * notified about the event that precipitated its own
1690 + * creation. This is not what the user wants.
1691 + */
1692 + list_add_tail(&engine->entry, &utrace->attaching);
1693 + utrace->pending_attach = 1;
1694 + utrace_engine_get(engine);
1695 + ret = 0;
1696 +unlock:
1697 + spin_unlock(&utrace->lock);
1699 + return ret;
1702 +/**
1703 + * utrace_attach_task - attach new engine, or look up an attached engine
1704 + * @target: thread to attach to
1705 + * @flags: flag bits combined with OR, see below
1706 + * @ops: callback table for new engine
1707 + * @data: engine private data pointer
1709 + * The caller must ensure that the @target thread does not get freed,
1710 + * i.e. hold a ref or be its parent. It is always safe to call this
1711 + * on @current, or on the @child pointer in a @report_clone callback.
1712 + * For most other cases, it's easier to use utrace_attach_pid() instead.
1714 + * UTRACE_ATTACH_CREATE:
1715 + * Create a new engine. If %UTRACE_ATTACH_CREATE is not specified, you
1716 + * only look up an existing engine already attached to the thread.
1718 + * UTRACE_ATTACH_EXCLUSIVE:
1719 + * Attempting to attach a second (matching) engine fails with -%EEXIST.
1721 + * UTRACE_ATTACH_MATCH_OPS: Only consider engines matching @ops.
1722 + * UTRACE_ATTACH_MATCH_DATA: Only consider engines matching @data.
1724 + * Calls with neither %UTRACE_ATTACH_MATCH_OPS nor %UTRACE_ATTACH_MATCH_DATA
1725 + * match the first among any engines attached to @target. That means that
1726 + * %UTRACE_ATTACH_EXCLUSIVE in such a call fails with -%EEXIST if there
1727 + * are any engines on @target at all.
1728 + */
1729 +struct utrace_engine *utrace_attach_task(
1730 + struct task_struct *target, int flags,
1731 + const struct utrace_engine_ops *ops, void *data)
1733 + struct utrace *utrace = task_utrace_struct(target);
1734 + struct utrace_engine *engine;
1735 + int ret;
1737 + if (!(flags & UTRACE_ATTACH_CREATE)) {
1738 + if (unlikely(!utrace))
1739 + return ERR_PTR(-ENOENT);
1740 + spin_lock(&utrace->lock);
1741 + engine = find_matching_engine(utrace, flags, ops, data);
1742 + if (engine)
1743 + utrace_engine_get(engine);
1744 + spin_unlock(&utrace->lock);
1745 + return engine ?: ERR_PTR(-ENOENT);
1748 + if (unlikely(!ops) || unlikely(ops == &utrace_detached_ops))
1749 + return ERR_PTR(-EINVAL);
1751 + if (unlikely(target->flags & PF_KTHREAD))
1752 + /*
1753 + * Silly kernel, utrace is for users!
1754 + */
1755 + return ERR_PTR(-EPERM);
1757 + if (!utrace) {
1758 + if (unlikely(!utrace_task_alloc(target)))
1759 + return ERR_PTR(-ENOMEM);
1760 + utrace = task_utrace_struct(target);
1763 + engine = kmem_cache_alloc(utrace_engine_cachep, GFP_KERNEL);
1764 + if (unlikely(!engine))
1765 + return ERR_PTR(-ENOMEM);
1767 + /*
1768 + * Initialize the new engine structure. It starts out with one ref
1769 + * to return. utrace_add_engine() adds another for being attached.
1770 + */
1771 + kref_init(&engine->kref);
1772 + engine->flags = 0;
1773 + engine->ops = ops;
1774 + engine->data = data;
1775 + engine->release = ops->release;
1777 + ret = utrace_add_engine(target, utrace, engine, flags, ops, data);
1779 + if (unlikely(ret)) {
1780 + kmem_cache_free(utrace_engine_cachep, engine);
1781 + engine = ERR_PTR(ret);
1785 + return engine;
1787 +EXPORT_SYMBOL_GPL(utrace_attach_task);
1789 +/**
1790 + * utrace_attach_pid - attach new engine, or look up an attached engine
1791 + * @pid: &struct pid pointer representing thread to attach to
1792 + * @flags: flag bits combined with OR, see utrace_attach_task()
1793 + * @ops: callback table for new engine
1794 + * @data: engine private data pointer
1796 + * This is the same as utrace_attach_task(), but takes a &struct pid
1797 + * pointer rather than a &struct task_struct pointer. The caller must
1798 + * hold a ref on @pid, but does not need to worry about the task
1799 + * staying valid. If it's been reaped so that @pid points nowhere,
1800 + * then this call returns -%ESRCH.
1801 + */
1802 +struct utrace_engine *utrace_attach_pid(
1803 + struct pid *pid, int flags,
1804 + const struct utrace_engine_ops *ops, void *data)
1806 + struct utrace_engine *engine = ERR_PTR(-ESRCH);
1807 + struct task_struct *task = get_pid_task(pid, PIDTYPE_PID);
1808 + if (task) {
1809 + engine = utrace_attach_task(task, flags, ops, data);
1810 + put_task_struct(task);
1812 + return engine;
1814 +EXPORT_SYMBOL_GPL(utrace_attach_pid);
1817 + * When an engine is detached, the target thread may still see it and
1818 + * make callbacks until it quiesces. We install a special ops vector
1819 + * with these two callbacks. When the target thread quiesces, it can
1820 + * safely free the engine itself. For any event we will always get
1821 + * the report_quiesce() callback first, so we only need this one
1822 + * pointer to be set. The only exception is report_reap(), so we
1823 + * supply that callback too.
1824 + */
1825 +static u32 utrace_detached_quiesce(u32 action, struct utrace_engine *engine,
1826 + unsigned long event)
1828 + return UTRACE_DETACH;
1831 +static void utrace_detached_reap(struct utrace_engine *engine,
1832 + struct task_struct *task)
1836 +static const struct utrace_engine_ops utrace_detached_ops = {
1837 + .report_quiesce = &utrace_detached_quiesce,
1838 + .report_reap = &utrace_detached_reap
1842 + * The caller has to hold a ref on the engine. If the attached flag is
1843 + * true (all but utrace_barrier() calls), the engine is supposed to be
1844 + * attached. If the attached flag is false (utrace_barrier() only),
1845 + * then return -ERESTARTSYS for an engine marked for detach but not yet
1846 + * fully detached. The task pointer can be invalid if the engine is
1847 + * detached.
1849 + * Get the utrace lock for the target task.
1850 + * Returns the struct if locked, or ERR_PTR(-errno).
1852 + * This has to be robust against races with:
1853 + * utrace_control(target, UTRACE_DETACH) calls
1854 + * UTRACE_DETACH after reports
1855 + * utrace_report_death
1856 + * utrace_release_task
1857 + */
1858 +static struct utrace *get_utrace_lock(struct task_struct *target,
1859 + struct utrace_engine *engine,
1860 + bool attached)
1861 + __acquires(utrace->lock)
1863 + struct utrace *utrace;
1865 + rcu_read_lock();
1867 + /*
1868 + * If this engine was already detached, bail out before we look at
1869 + * the task_struct pointer at all. If it's detached after this
1870 + * check, then RCU is still keeping this task_struct pointer valid.
1872 + * The ops pointer is NULL when the engine is fully detached.
1873 + * It's &utrace_detached_ops when it's marked detached but still
1874 + * on the list. In the latter case, utrace_barrier() still works,
1875 + * since the target might be in the middle of an old callback.
1876 + */
1877 + if (unlikely(!engine->ops)) {
1878 + rcu_read_unlock();
1879 + return ERR_PTR(-ESRCH);
1882 + if (unlikely(engine->ops == &utrace_detached_ops)) {
1883 + rcu_read_unlock();
1884 + return attached ? ERR_PTR(-ESRCH) : ERR_PTR(-ERESTARTSYS);
1887 + utrace = task_utrace_struct(target);
1888 + spin_lock(&utrace->lock);
1889 + if (unlikely(utrace->reap) || unlikely(!engine->ops) ||
1890 + unlikely(engine->ops == &utrace_detached_ops)) {
1891 + /*
1892 + * By the time we got the utrace lock,
1893 + * it had been reaped or detached already.
1894 + */
1895 + spin_unlock(&utrace->lock);
1896 + utrace = ERR_PTR(-ESRCH);
1897 + if (!attached && engine->ops == &utrace_detached_ops)
1898 + utrace = ERR_PTR(-ERESTARTSYS);
1900 + rcu_read_unlock();
1902 + return utrace;
1906 + * Now that we don't hold any locks, run through any
1907 + * detached engines and free their references. Each
1908 + * engine had one implicit ref while it was attached.
1909 + */
1910 +static void put_detached_list(struct list_head *list)
1912 + struct utrace_engine *engine, *next;
1913 + list_for_each_entry_safe(engine, next, list, entry) {
1914 + list_del_init(&engine->entry);
1915 + utrace_engine_put(engine);
1920 + * We use an extra bit in utrace_engine.flags past the event bits,
1921 + * to record whether the engine is keeping the target thread stopped.
1923 + * This bit is set in task_struct.utrace_flags whenever it is set in any
1924 + * engine's flags. Only utrace_reset() resets it in utrace_flags.
1925 + */
1926 +#define ENGINE_STOP (1UL << _UTRACE_NEVENTS)
1928 +static void mark_engine_wants_stop(struct task_struct *task,
1929 + struct utrace_engine *engine)
1931 + engine->flags |= ENGINE_STOP;
1932 + task->utrace_flags |= ENGINE_STOP;
1935 +static void clear_engine_wants_stop(struct utrace_engine *engine)
1937 + engine->flags &= ~ENGINE_STOP;
1940 +static bool engine_wants_stop(struct utrace_engine *engine)
1942 + return (engine->flags & ENGINE_STOP) != 0;
1945 +/**
1946 + * utrace_set_events - choose which event reports a tracing engine gets
1947 + * @target: thread to affect
1948 + * @engine: attached engine to affect
1949 + * @events: new event mask
1951 + * This changes the set of events for which @engine wants callbacks made.
1953 + * This fails with -%EALREADY and does nothing if you try to clear
1954 + * %UTRACE_EVENT(%DEATH) when the @report_death callback may already have
1955 + * begun, or if you try to newly set %UTRACE_EVENT(%DEATH) or
1956 + * %UTRACE_EVENT(%QUIESCE) when @target is already dead or dying.
1958 + * This fails with -%ESRCH if you try to clear %UTRACE_EVENT(%REAP) when
1959 + * the @report_reap callback may already have begun, or when @target has
1960 + * already been detached, including forcible detach on reaping.
1962 + * If @target was stopped before the call, then after a successful call,
1963 + * no event callbacks not requested in @events will be made; if
1964 + * %UTRACE_EVENT(%QUIESCE) is included in @events, then a
1965 + * @report_quiesce callback will be made when @target resumes.
1967 + * If @target was not stopped and @events excludes some bits that were
1968 + * set before, this can return -%EINPROGRESS to indicate that @target
1969 + * may have been making some callback to @engine. When this returns
1970 + * zero, you can be sure that no event callbacks you've disabled in
1971 + * @events can be made. If @events only sets new bits that were not set
1972 + * before on @engine, then -%EINPROGRESS will never be returned.
1974 + * To synchronize after an -%EINPROGRESS return, see utrace_barrier().
1976 + * When @target is @current, -%EINPROGRESS is not returned. But note
1977 + * that a newly-created engine will not receive any callbacks related to
1978 + * an event notification already in progress. This call enables @events
1979 + * callbacks to be made as soon as @engine becomes eligible for any
1980 + * callbacks, see utrace_attach_task().
1982 + * These rules provide for coherent synchronization based on %UTRACE_STOP,
1983 + * even when %SIGKILL is breaking its normal simple rules.
1984 + */
1985 +int utrace_set_events(struct task_struct *target,
1986 + struct utrace_engine *engine,
1987 + unsigned long events)
1989 + struct utrace *utrace;
1990 + unsigned long old_flags, old_utrace_flags;
1991 + int ret = -EALREADY;
1993 + /*
1994 + * We just ignore the internal bit, so callers can use
1995 + * engine->flags to seed bitwise ops for our argument.
1996 + */
1997 + events &= ~ENGINE_STOP;
1999 + utrace = get_utrace_lock(target, engine, true);
2000 + if (unlikely(IS_ERR(utrace)))
2001 + return PTR_ERR(utrace);
2003 + old_utrace_flags = target->utrace_flags;
2004 + old_flags = engine->flags & ~ENGINE_STOP;
2006 + /*
2007 + * If utrace_report_death() is already progress now,
2008 + * it's too late to clear the death event bits.
2009 + */
2010 + if (((old_flags & ~events) & _UTRACE_DEATH_EVENTS) && utrace->death)
2011 + goto unlock;
2013 + /*
2014 + * When setting these flags, it's essential that we really
2015 + * synchronize with exit_notify(). They cannot be set after
2016 + * exit_notify() takes the tasklist_lock. By holding the read
2017 + * lock here while setting the flags, we ensure that the calls
2018 + * to tracehook_notify_death() and tracehook_report_death() will
2019 + * see the new flags. This ensures that utrace_release_task()
2020 + * knows positively that utrace_report_death() will be called or
2021 + * that it won't.
2022 + */
2023 + if ((events & ~old_flags) & _UTRACE_DEATH_EVENTS) {
2024 + read_lock(&tasklist_lock);
2025 + if (unlikely(target->exit_state)) {
2026 + read_unlock(&tasklist_lock);
2027 + goto unlock;
2029 + target->utrace_flags |= events;
2030 + read_unlock(&tasklist_lock);
2033 + engine->flags = events | (engine->flags & ENGINE_STOP);
2034 + target->utrace_flags |= events;
2036 + if ((events & UTRACE_EVENT_SYSCALL) &&
2037 + !(old_utrace_flags & UTRACE_EVENT_SYSCALL))
2038 + set_tsk_thread_flag(target, TIF_SYSCALL_TRACE);
2040 + ret = 0;
2041 + if ((old_flags & ~events) && target != current &&
2042 + !task_is_stopped_or_traced(target) && !target->exit_state) {
2043 + /*
2044 + * This barrier ensures that our engine->flags changes
2045 + * have hit before we examine utrace->reporting,
2046 + * pairing with the barrier in start_callback(). If
2047 + * @target has not yet hit finish_callback() to clear
2048 + * utrace->reporting, we might be in the middle of a
2049 + * callback to @engine.
2050 + */
2051 + smp_mb();
2052 + if (utrace->reporting == engine)
2053 + ret = -EINPROGRESS;
2055 +unlock:
2056 + spin_unlock(&utrace->lock);
2058 + return ret;
2060 +EXPORT_SYMBOL_GPL(utrace_set_events);
2063 + * Asynchronously mark an engine as being detached.
2065 + * This must work while the target thread races with us doing
2066 + * start_callback(), defined below. It uses smp_rmb() between checking
2067 + * @engine->flags and using @engine->ops. Here we change @engine->ops
2068 + * first, then use smp_wmb() before changing @engine->flags. This ensures
2069 + * it can check the old flags before using the old ops, or check the old
2070 + * flags before using the new ops, or check the new flags before using the
2071 + * new ops, but can never check the new flags before using the old ops.
2072 + * Hence, utrace_detached_ops might be used with any old flags in place.
2073 + * It has report_quiesce() and report_reap() callbacks to handle all cases.
2074 + */
2075 +static void mark_engine_detached(struct utrace_engine *engine)
2077 + engine->ops = &utrace_detached_ops;
2078 + smp_wmb();
2079 + engine->flags = UTRACE_EVENT(QUIESCE);
2083 + * Get @target to stop and return true if it is already stopped now.
2084 + * If we return false, it will make some event callback soonish.
2085 + * Called with @utrace locked.
2086 + */
2087 +static bool utrace_do_stop(struct task_struct *target, struct utrace *utrace)
2089 + if (task_is_stopped(target)) {
2090 + /*
2091 + * Stopped is considered quiescent; when it wakes up, it will
2092 + * go through utrace_finish_stop() before doing anything else.
2093 + */
2094 + spin_lock_irq(&target->sighand->siglock);
2095 + if (likely(task_is_stopped(target)))
2096 + __set_task_state(target, TASK_TRACED);
2097 + spin_unlock_irq(&target->sighand->siglock);
2098 + } else if (utrace->resume > UTRACE_REPORT) {
2099 + utrace->resume = UTRACE_REPORT;
2100 + set_notify_resume(target);
2103 + return task_is_traced(target);
2107 + * If the target is not dead it should not be in tracing
2108 + * stop any more. Wake it unless it's in job control stop.
2109 + */
2110 +static void utrace_wakeup(struct task_struct *target, struct utrace *utrace)
2112 + lockdep_assert_held(&utrace->lock);
2113 + spin_lock_irq(&target->sighand->siglock);
2114 + if (target->signal->flags & SIGNAL_STOP_STOPPED ||
2115 + target->signal->group_stop_count)
2116 + target->state = TASK_STOPPED;
2117 + else
2118 + wake_up_state(target, __TASK_TRACED);
2119 + spin_unlock_irq(&target->sighand->siglock);
2123 + * This is called when there might be some detached engines on the list or
2124 + * some stale bits in @task->utrace_flags. Clean them up and recompute the
2125 + * flags. Returns true if we're now fully detached.
2127 + * Called with @utrace->lock held, returns with it released.
2128 + * After this returns, @utrace might be freed if everything detached.
2129 + */
2130 +static bool utrace_reset(struct task_struct *task, struct utrace *utrace)
2131 + __releases(utrace->lock)
2133 + struct utrace_engine *engine, *next;
2134 + unsigned long flags = 0;
2135 + LIST_HEAD(detached);
2137 + splice_attaching(utrace);
2139 + /*
2140 + * Update the set of events of interest from the union
2141 + * of the interests of the remaining tracing engines.
2142 + * For any engine marked detached, remove it from the list.
2143 + * We'll collect them on the detached list.
2144 + */
2145 + list_for_each_entry_safe(engine, next, &utrace->attached, entry) {
2146 + if (engine->ops == &utrace_detached_ops) {
2147 + engine->ops = NULL;
2148 + list_move(&engine->entry, &detached);
2149 + } else {
2150 + flags |= engine->flags | UTRACE_EVENT(REAP);
2154 + if (task->exit_state) {
2155 + /*
2156 + * Once it's already dead, we never install any flags
2157 + * except REAP. When ->exit_state is set and events
2158 + * like DEATH are not set, then they never can be set.
2159 + * This ensures that utrace_release_task() knows
2160 + * positively that utrace_report_death() can never run.
2161 + */
2162 + BUG_ON(utrace->death);
2163 + flags &= UTRACE_EVENT(REAP);
2164 + } else if (!(flags & UTRACE_EVENT_SYSCALL) &&
2165 + test_tsk_thread_flag(task, TIF_SYSCALL_TRACE)) {
2166 + clear_tsk_thread_flag(task, TIF_SYSCALL_TRACE);
2169 + if (!flags) {
2170 + /*
2171 + * No more engines, cleared out the utrace.
2172 + */
2173 + utrace->resume = UTRACE_RESUME;
2174 + utrace->signal_handler = 0;
2177 + /*
2178 + * If no more engines want it stopped, wake it up.
2179 + */
2180 + if (task_is_traced(task) && !(flags & ENGINE_STOP)) {
2181 + /*
2182 + * It just resumes, so make sure single-step
2183 + * is not left set.
2184 + */
2185 + if (utrace->resume == UTRACE_RESUME)
2186 + user_disable_single_step(task);
2187 + utrace_wakeup(task, utrace);
2190 + /*
2191 + * In theory spin_lock() doesn't imply rcu_read_lock().
2192 + * Once we clear ->utrace_flags this task_struct can go away
2193 + * because tracehook_prepare_release_task() path does not take
2194 + * utrace->lock when ->utrace_flags == 0.
2195 + */
2196 + rcu_read_lock();
2197 + task->utrace_flags = flags;
2198 + spin_unlock(&utrace->lock);
2199 + rcu_read_unlock();
2201 + put_detached_list(&detached);
2203 + return !flags;
2206 +void utrace_finish_stop(void)
2208 + /*
2209 + * If we were task_is_traced() and then SIGKILL'ed, make
2210 + * sure we do nothing until the tracer drops utrace->lock.
2211 + */
2212 + if (unlikely(__fatal_signal_pending(current))) {
2213 + struct utrace *utrace = task_utrace_struct(current);
2214 + spin_unlock_wait(&utrace->lock);
2219 + * Perform %UTRACE_STOP, i.e. block in TASK_TRACED until woken up.
2220 + * @task == current, @utrace == current->utrace, which is not locked.
2221 + * Return true if we were woken up by SIGKILL even though some utrace
2222 + * engine may still want us to stay stopped.
2223 + */
2224 +static void utrace_stop(struct task_struct *task, struct utrace *utrace,
2225 + enum utrace_resume_action action)
2227 +relock:
2228 + spin_lock(&utrace->lock);
2230 + if (action < utrace->resume) {
2231 + /*
2232 + * Ensure a reporting pass when we're resumed.
2233 + */
2234 + utrace->resume = action;
2235 + if (action == UTRACE_INTERRUPT)
2236 + set_thread_flag(TIF_SIGPENDING);
2237 + else
2238 + set_thread_flag(TIF_NOTIFY_RESUME);
2241 + /*
2242 + * If the ENGINE_STOP bit is clear in utrace_flags, that means
2243 + * utrace_reset() ran after we processed some UTRACE_STOP return
2244 + * values from callbacks to get here. If all engines have detached
2245 + * or resumed us, we don't stop. This check doesn't require
2246 + * siglock, but it should follow the interrupt/report bookkeeping
2247 + * steps (this can matter for UTRACE_RESUME but not UTRACE_DETACH).
2248 + */
2249 + if (unlikely(!(task->utrace_flags & ENGINE_STOP))) {
2250 + utrace_reset(task, utrace);
2251 + if (task->utrace_flags & ENGINE_STOP)
2252 + goto relock;
2253 + return;
2256 + /*
2257 + * The siglock protects us against signals. As well as SIGKILL
2258 + * waking us up, we must synchronize with the signal bookkeeping
2259 + * for stop signals and SIGCONT.
2260 + */
2261 + spin_lock_irq(&task->sighand->siglock);
2263 + if (unlikely(__fatal_signal_pending(task))) {
2264 + spin_unlock_irq(&task->sighand->siglock);
2265 + spin_unlock(&utrace->lock);
2266 + return;
2269 + __set_current_state(TASK_TRACED);
2271 + /*
2272 + * If there is a group stop in progress,
2273 + * we must participate in the bookkeeping.
2274 + */
2275 + if (unlikely(task->signal->group_stop_count) &&
2276 + !--task->signal->group_stop_count)
2277 + task->signal->flags = SIGNAL_STOP_STOPPED;
2279 + spin_unlock_irq(&task->sighand->siglock);
2280 + spin_unlock(&utrace->lock);
2282 + schedule();
2284 + utrace_finish_stop();
2286 + /*
2287 + * While in TASK_TRACED, we were considered "frozen enough".
2288 + * Now that we woke up, it's crucial if we're supposed to be
2289 + * frozen that we freeze now before running anything substantial.
2290 + */
2291 + try_to_freeze();
2293 + /*
2294 + * While we were in TASK_TRACED, complete_signal() considered
2295 + * us "uninterested" in signal wakeups. Now make sure our
2296 + * TIF_SIGPENDING state is correct for normal running.
2297 + */
2298 + spin_lock_irq(&task->sighand->siglock);
2299 + recalc_sigpending();
2300 + spin_unlock_irq(&task->sighand->siglock);
2304 + * Called by release_task() with @reap set to true.
2305 + * Called by utrace_report_death() with @reap set to false.
2306 + * On reap, make report_reap callbacks and clean out @utrace
2307 + * unless still making callbacks. On death, update bookkeeping
2308 + * and handle the reap work if release_task() came in first.
2309 + */
2310 +void utrace_maybe_reap(struct task_struct *target, struct utrace *utrace,
2311 + bool reap)
2313 + struct utrace_engine *engine, *next;
2314 + struct list_head attached;
2316 + spin_lock(&utrace->lock);
2318 + if (reap) {
2319 + /*
2320 + * If the target will do some final callbacks but hasn't
2321 + * finished them yet, we know because it clears these event
2322 + * bits after it's done. Instead of cleaning up here and
2323 + * requiring utrace_report_death() to cope with it, we
2324 + * delay the REAP report and the teardown until after the
2325 + * target finishes its death reports.
2326 + */
2327 + utrace->reap = 1;
2329 + if (target->utrace_flags & _UTRACE_DEATH_EVENTS) {
2330 + spin_unlock(&utrace->lock);
2331 + return;
2333 + } else {
2334 + /*
2335 + * After we unlock with this flag clear, any competing
2336 + * utrace_control/utrace_set_events calls know that we've
2337 + * finished our callbacks and any detach bookkeeping.
2338 + */
2339 + utrace->death = 0;
2341 + if (!utrace->reap) {
2342 + /*
2343 + * We're just dead, not reaped yet. This will
2344 + * reset @target->utrace_flags so the later call
2345 + * with @reap set won't hit the check above.
2346 + */
2347 + utrace_reset(target, utrace);
2348 + return;
2352 + /*
2353 + * utrace_add_engine() checks ->utrace_flags != 0. Since
2354 + * @utrace->reap is set, nobody can set or clear UTRACE_EVENT(REAP)
2355 + * in @engine->flags or change @engine->ops and nobody can change
2356 + * @utrace->attached after we drop the lock.
2357 + */
2358 + target->utrace_flags = 0;
2360 + /*
2361 + * We clear out @utrace->attached before we drop the lock so
2362 + * that find_matching_engine() can't come across any old engine
2363 + * while we are busy tearing it down.
2364 + */
2365 + list_replace_init(&utrace->attached, &attached);
2366 + list_splice_tail_init(&utrace->attaching, &attached);
2368 + spin_unlock(&utrace->lock);
2370 + list_for_each_entry_safe(engine, next, &attached, entry) {
2371 + if (engine->flags & UTRACE_EVENT(REAP))
2372 + engine->ops->report_reap(engine, target);
2374 + engine->ops = NULL;
2375 + engine->flags = 0;
2376 + list_del_init(&engine->entry);
2378 + utrace_engine_put(engine);
2383 + * You can't do anything to a dead task but detach it.
2384 + * If release_task() has been called, you can't do that.
2386 + * On the exit path, DEATH and QUIESCE event bits are set only
2387 + * before utrace_report_death() has taken the lock. At that point,
2388 + * the death report will come soon, so disallow detach until it's
2389 + * done. This prevents us from racing with it detaching itself.
2391 + * Called only when @target->exit_state is nonzero.
2392 + */
2393 +static inline int utrace_control_dead(struct task_struct *target,
2394 + struct utrace *utrace,
2395 + enum utrace_resume_action action)
2397 + lockdep_assert_held(&utrace->lock);
2399 + if (action != UTRACE_DETACH || unlikely(utrace->reap))
2400 + return -ESRCH;
2402 + if (unlikely(utrace->death))
2403 + /*
2404 + * We have already started the death report. We can't
2405 + * prevent the report_death and report_reap callbacks,
2406 + * so tell the caller they will happen.
2407 + */
2408 + return -EALREADY;
2410 + return 0;
2413 +/**
2414 + * utrace_control - control a thread being traced by a tracing engine
2415 + * @target: thread to affect
2416 + * @engine: attached engine to affect
2417 + * @action: &enum utrace_resume_action for thread to do
2419 + * This is how a tracing engine asks a traced thread to do something.
2420 + * This call is controlled by the @action argument, which has the
2421 + * same meaning as the &enum utrace_resume_action value returned by
2422 + * event reporting callbacks.
2424 + * If @target is already dead (@target->exit_state nonzero),
2425 + * all actions except %UTRACE_DETACH fail with -%ESRCH.
2427 + * The following sections describe each option for the @action argument.
2429 + * UTRACE_DETACH:
2431 + * After this, the @engine data structure is no longer accessible,
2432 + * and the thread might be reaped. The thread will start running
2433 + * again if it was stopped and no longer has any attached engines
2434 + * that want it stopped.
2436 + * If the @report_reap callback may already have begun, this fails
2437 + * with -%ESRCH. If the @report_death callback may already have
2438 + * begun, this fails with -%EALREADY.
2440 + * If @target is not already stopped, then a callback to this engine
2441 + * might be in progress or about to start on another CPU. If so,
2442 + * then this returns -%EINPROGRESS; the detach happens as soon as
2443 + * the pending callback is finished. To synchronize after an
2444 + * -%EINPROGRESS return, see utrace_barrier().
2446 + * If @target is properly stopped before utrace_control() is called,
2447 + * then after successful return it's guaranteed that no more callbacks
2448 + * to the @engine->ops vector will be made.
2450 + * The only exception is %SIGKILL (and exec or group-exit by another
2451 + * thread in the group), which can cause asynchronous @report_death
2452 + * and/or @report_reap callbacks even when %UTRACE_STOP was used.
2453 + * (In that event, this fails with -%ESRCH or -%EALREADY, see above.)
2455 + * UTRACE_STOP:
2457 + * This asks that @target stop running. This returns 0 only if
2458 + * @target is already stopped, either for tracing or for job
2459 + * control. Then @target will remain stopped until another
2460 + * utrace_control() call is made on @engine; @target can be woken
2461 + * only by %SIGKILL (or equivalent, such as exec or termination by
2462 + * another thread in the same thread group).
2464 + * This returns -%EINPROGRESS if @target is not already stopped.
2465 + * Then the effect is like %UTRACE_REPORT. A @report_quiesce or
2466 + * @report_signal callback will be made soon. Your callback can
2467 + * then return %UTRACE_STOP to keep @target stopped.
2469 + * This does not interrupt system calls in progress, including ones
2470 + * that sleep for a long time. For that, use %UTRACE_INTERRUPT.
2471 + * To interrupt system calls and then keep @target stopped, your
2472 + * @report_signal callback can return %UTRACE_STOP.
2474 + * UTRACE_RESUME:
2476 + * Just let @target continue running normally, reversing the effect
2477 + * of a previous %UTRACE_STOP. If another engine is keeping @target
2478 + * stopped, then it remains stopped until all engines let it resume.
2479 + * If @target was not stopped, this has no effect.
2481 + * UTRACE_REPORT:
2483 + * This is like %UTRACE_RESUME, but also ensures that there will be
2484 + * a @report_quiesce or @report_signal callback made soon. If
2485 + * @target had been stopped, then there will be a callback before it
2486 + * resumes running normally. If another engine is keeping @target
2487 + * stopped, then there might be no callbacks until all engines let
2488 + * it resume.
2490 + * Since this is meaningless unless @report_quiesce callbacks will
2491 + * be made, it returns -%EINVAL if @engine lacks %UTRACE_EVENT(%QUIESCE).
2493 + * UTRACE_INTERRUPT:
2495 + * This is like %UTRACE_REPORT, but ensures that @target will make a
2496 + * @report_signal callback before it resumes or delivers signals.
2497 + * If @target was in a system call or about to enter one, work in
2498 + * progress will be interrupted as if by %SIGSTOP. If another
2499 + * engine is keeping @target stopped, then there might be no
2500 + * callbacks until all engines let it resume.
2502 + * This gives @engine an opportunity to introduce a forced signal
2503 + * disposition via its @report_signal callback.
2505 + * UTRACE_SINGLESTEP:
2507 + * It's invalid to use this unless arch_has_single_step() returned true.
2508 + * This is like %UTRACE_RESUME, but resumes for one user instruction only.
2510 + * Note that passing %UTRACE_SINGLESTEP or %UTRACE_BLOCKSTEP to
2511 + * utrace_control() or returning it from an event callback alone does
2512 + * not necessarily ensure that stepping will be enabled. If there are
2513 + * more callbacks made to any engine before returning to user mode,
2514 + * then the resume action is chosen only by the last set of callbacks.
2515 + * To be sure, enable %UTRACE_EVENT(%QUIESCE) and look for the
2516 + * @report_quiesce callback with a zero event mask, or the
2517 + * @report_signal callback with %UTRACE_SIGNAL_REPORT.
2519 + * Since this is not robust unless @report_quiesce callbacks will
2520 + * be made, it returns -%EINVAL if @engine lacks %UTRACE_EVENT(%QUIESCE).
2522 + * UTRACE_BLOCKSTEP:
2524 + * It's invalid to use this unless arch_has_block_step() returned true.
2525 + * This is like %UTRACE_SINGLESTEP, but resumes for one whole basic
2526 + * block of user instructions.
2528 + * Since this is not robust unless @report_quiesce callbacks will
2529 + * be made, it returns -%EINVAL if @engine lacks %UTRACE_EVENT(%QUIESCE).
2531 + * %UTRACE_BLOCKSTEP devolves to %UTRACE_SINGLESTEP when another
2532 + * tracing engine is using %UTRACE_SINGLESTEP at the same time.
2533 + */
2534 +int utrace_control(struct task_struct *target,
2535 + struct utrace_engine *engine,
2536 + enum utrace_resume_action action)
2538 + struct utrace *utrace;
2539 + bool reset;
2540 + int ret;
2542 + if (unlikely(action >= UTRACE_RESUME_MAX)) {
2543 + WARN(1, "invalid action argument to utrace_control()!");
2544 + return -EINVAL;
2547 + /*
2548 + * This is a sanity check for a programming error in the caller.
2549 + * Their request can only work properly in all cases by relying on
2550 + * a follow-up callback, but they didn't set one up! This check
2551 + * doesn't do locking, but it shouldn't matter. The caller has to
2552 + * be synchronously sure the callback is set up to be operating the
2553 + * interface properly.
2554 + */
2555 + if (action >= UTRACE_REPORT && action < UTRACE_RESUME &&
2556 + unlikely(!(engine->flags & UTRACE_EVENT(QUIESCE)))) {
2557 + WARN(1, "utrace_control() with no QUIESCE callback in place!");
2558 + return -EINVAL;
2561 + utrace = get_utrace_lock(target, engine, true);
2562 + if (unlikely(IS_ERR(utrace)))
2563 + return PTR_ERR(utrace);
2565 + reset = task_is_traced(target);
2566 + ret = 0;
2568 + /*
2569 + * ->exit_state can change under us, this doesn't matter.
2570 + * We do not care about ->exit_state in fact, but we do
2571 + * care about ->reap and ->death. If either flag is set,
2572 + * we must also see ->exit_state != 0.
2573 + */
2574 + if (unlikely(target->exit_state)) {
2575 + ret = utrace_control_dead(target, utrace, action);
2576 + if (ret) {
2577 + spin_unlock(&utrace->lock);
2578 + return ret;
2580 + reset = true;
2583 + switch (action) {
2584 + case UTRACE_STOP:
2585 + mark_engine_wants_stop(target, engine);
2586 + if (!reset && !utrace_do_stop(target, utrace))
2587 + ret = -EINPROGRESS;
2588 + reset = false;
2589 + break;
2591 + case UTRACE_DETACH:
2592 + if (engine_wants_stop(engine))
2593 + target->utrace_flags &= ~ENGINE_STOP;
2594 + mark_engine_detached(engine);
2595 + reset = reset || utrace_do_stop(target, utrace);
2596 + if (!reset) {
2597 + /*
2598 + * As in utrace_set_events(), this barrier ensures
2599 + * that our engine->flags changes have hit before we
2600 + * examine utrace->reporting, pairing with the barrier
2601 + * in start_callback(). If @target has not yet hit
2602 + * finish_callback() to clear utrace->reporting, we
2603 + * might be in the middle of a callback to @engine.
2604 + */
2605 + smp_mb();
2606 + if (utrace->reporting == engine)
2607 + ret = -EINPROGRESS;
2609 + break;
2611 + case UTRACE_RESUME:
2612 + clear_engine_wants_stop(engine);
2613 + break;
2615 + case UTRACE_BLOCKSTEP:
2616 + /*
2617 + * Resume from stopped, step one block.
2618 + * We fall through to treat it like UTRACE_SINGLESTEP.
2619 + */
2620 + if (unlikely(!arch_has_block_step())) {
2621 + WARN(1, "UTRACE_BLOCKSTEP when !arch_has_block_step()");
2622 + action = UTRACE_SINGLESTEP;
2625 + case UTRACE_SINGLESTEP:
2626 + /*
2627 + * Resume from stopped, step one instruction.
2628 + * We fall through to the UTRACE_REPORT case.
2629 + */
2630 + if (unlikely(!arch_has_single_step())) {
2631 + WARN(1,
2632 + "UTRACE_SINGLESTEP when !arch_has_single_step()");
2633 + reset = false;
2634 + ret = -EOPNOTSUPP;
2635 + break;
2638 + case UTRACE_REPORT:
2639 + /*
2640 + * Make the thread call tracehook_notify_resume() soon.
2641 + * But don't bother if it's already been interrupted.
2642 + * In that case, utrace_get_signal() will be reporting soon.
2643 + */
2644 + clear_engine_wants_stop(engine);
2645 + if (action < utrace->resume) {
2646 + utrace->resume = action;
2647 + set_notify_resume(target);
2649 + break;
2651 + case UTRACE_INTERRUPT:
2652 + /*
2653 + * Make the thread call tracehook_get_signal() soon.
2654 + */
2655 + clear_engine_wants_stop(engine);
2656 + if (utrace->resume == UTRACE_INTERRUPT)
2657 + break;
2658 + utrace->resume = UTRACE_INTERRUPT;
2660 + /*
2661 + * If it's not already stopped, interrupt it now. We need
2662 + * the siglock here in case it calls recalc_sigpending()
2663 + * and clears its own TIF_SIGPENDING. By taking the lock,
2664 + * we've serialized any later recalc_sigpending() after our
2665 + * setting of utrace->resume to force it on.
2666 + */
2667 + if (reset) {
2668 + /*
2669 + * This is really just to keep the invariant that
2670 + * TIF_SIGPENDING is set with UTRACE_INTERRUPT.
2671 + * When it's stopped, we know it's always going
2672 + * through utrace_get_signal() and will recalculate.
2673 + */
2674 + set_tsk_thread_flag(target, TIF_SIGPENDING);
2675 + } else {
2676 + struct sighand_struct *sighand;
2677 + unsigned long irqflags;
2678 + sighand = lock_task_sighand(target, &irqflags);
2679 + if (likely(sighand)) {
2680 + signal_wake_up(target, 0);
2681 + unlock_task_sighand(target, &irqflags);
2684 + break;
2686 + default:
2687 + BUG(); /* We checked it on entry. */
2690 + /*
2691 + * Let the thread resume running. If it's not stopped now,
2692 + * there is nothing more we need to do.
2693 + */
2694 + if (reset)
2695 + utrace_reset(target, utrace);
2696 + else
2697 + spin_unlock(&utrace->lock);
2699 + return ret;
2701 +EXPORT_SYMBOL_GPL(utrace_control);
2703 +/**
2704 + * utrace_barrier - synchronize with simultaneous tracing callbacks
2705 + * @target: thread to affect
2706 + * @engine: engine to affect (can be detached)
2708 + * This blocks while @target might be in the midst of making a callback to
2709 + * @engine. It can be interrupted by signals and will return -%ERESTARTSYS.
2710 + * A return value of zero means no callback from @target to @engine was
2711 + * in progress. Any effect of its return value (such as %UTRACE_STOP) has
2712 + * already been applied to @engine.
2714 + * It's not necessary to keep the @target pointer alive for this call.
2715 + * It's only necessary to hold a ref on @engine. This will return
2716 + * safely even if @target has been reaped and has no task refs.
2718 + * A successful return from utrace_barrier() guarantees its ordering
2719 + * with respect to utrace_set_events() and utrace_control() calls. If
2720 + * @target was not properly stopped, event callbacks just disabled might
2721 + * still be in progress; utrace_barrier() waits until there is no chance
2722 + * an unwanted callback can be in progress.
2723 + */
2724 +int utrace_barrier(struct task_struct *target, struct utrace_engine *engine)
2726 + struct utrace *utrace;
2727 + int ret = -ERESTARTSYS;
2729 + if (unlikely(target == current))
2730 + return 0;
2732 + do {
2733 + utrace = get_utrace_lock(target, engine, false);
2734 + if (unlikely(IS_ERR(utrace))) {
2735 + ret = PTR_ERR(utrace);
2736 + if (ret != -ERESTARTSYS)
2737 + break;
2738 + } else {
2739 + /*
2740 + * All engine state changes are done while
2741 + * holding the lock, i.e. before we get here.
2742 + * Since we have the lock, we only need to
2743 + * worry about @target making a callback.
2744 + * When it has entered start_callback() but
2745 + * not yet gotten to finish_callback(), we
2746 + * will see utrace->reporting == @engine.
2747 + * When @target doesn't take the lock, it uses
2748 + * barriers to order setting utrace->reporting
2749 + * before it examines the engine state.
2750 + */
2751 + if (utrace->reporting != engine)
2752 + ret = 0;
2753 + spin_unlock(&utrace->lock);
2754 + if (!ret)
2755 + break;
2757 + schedule_timeout_interruptible(1);
2758 + } while (!signal_pending(current));
2760 + return ret;
2762 +EXPORT_SYMBOL_GPL(utrace_barrier);
2765 + * This is local state used for reporting loops, perhaps optimized away.
2766 + */
2767 +struct utrace_report {
2768 + u32 result;
2769 + enum utrace_resume_action action;
2770 + enum utrace_resume_action resume_action;
2771 + bool detaches;
2772 + bool spurious;
2775 +#define INIT_REPORT(var) \
2776 + struct utrace_report var = { \
2777 + .action = UTRACE_RESUME, \
2778 + .resume_action = UTRACE_RESUME, \
2779 + .spurious = true \
2783 + * We are now making the report, so clear the flag saying we need one.
2784 + * When there is a new attach, ->pending_attach is set just so we will
2785 + * know to do splice_attaching() here before the callback loop.
2786 + */
2787 +static enum utrace_resume_action start_report(struct utrace *utrace)
2789 + enum utrace_resume_action resume = utrace->resume;
2790 + if (utrace->pending_attach ||
2791 + (resume > UTRACE_INTERRUPT && resume < UTRACE_RESUME)) {
2792 + spin_lock(&utrace->lock);
2793 + splice_attaching(utrace);
2794 + resume = utrace->resume;
2795 + if (resume > UTRACE_INTERRUPT)
2796 + utrace->resume = UTRACE_RESUME;
2797 + spin_unlock(&utrace->lock);
2799 + return resume;
2802 +static inline void finish_report_reset(struct task_struct *task,
2803 + struct utrace *utrace,
2804 + struct utrace_report *report)
2806 + if (unlikely(report->spurious || report->detaches)) {
2807 + spin_lock(&utrace->lock);
2808 + if (utrace_reset(task, utrace))
2809 + report->action = UTRACE_RESUME;
2814 + * Complete a normal reporting pass, pairing with a start_report() call.
2815 + * This handles any UTRACE_DETACH or UTRACE_REPORT or UTRACE_INTERRUPT
2816 + * returns from engine callbacks. If @will_not_stop is true and any
2817 + * engine's last callback used UTRACE_STOP, we do UTRACE_REPORT here to
2818 + * ensure we stop before user mode. If there were no callbacks made, it
2819 + * will recompute @task->utrace_flags to avoid another false-positive.
2820 + */
2821 +static void finish_report(struct task_struct *task, struct utrace *utrace,
2822 + struct utrace_report *report, bool will_not_stop)
2824 + enum utrace_resume_action resume = report->action;
2826 + if (resume == UTRACE_STOP)
2827 + resume = will_not_stop ? UTRACE_REPORT : UTRACE_RESUME;
2829 + if (resume < utrace->resume) {
2830 + spin_lock(&utrace->lock);
2831 + utrace->resume = resume;
2832 + if (resume == UTRACE_INTERRUPT)
2833 + set_tsk_thread_flag(task, TIF_SIGPENDING);
2834 + else
2835 + set_tsk_thread_flag(task, TIF_NOTIFY_RESUME);
2836 + spin_unlock(&utrace->lock);
2839 + finish_report_reset(task, utrace, report);
2842 +static void finish_callback_report(struct task_struct *task,
2843 + struct utrace *utrace,
2844 + struct utrace_report *report,
2845 + struct utrace_engine *engine,
2846 + enum utrace_resume_action action)
2848 + if (action == UTRACE_DETACH) {
2849 + /*
2850 + * By holding the lock here, we make sure that
2851 + * utrace_barrier() (really get_utrace_lock()) sees the
2852 + * effect of this detach. Otherwise utrace_barrier() could
2853 + * return 0 after this callback had returned UTRACE_DETACH.
2854 + * This way, a 0 return is an unambiguous indicator that any
2855 + * callback returning UTRACE_DETACH has indeed caused detach.
2856 + */
2857 + spin_lock(&utrace->lock);
2858 + engine->ops = &utrace_detached_ops;
2859 + spin_unlock(&utrace->lock);
2862 + /*
2863 + * If utrace_control() was used, treat that like UTRACE_DETACH here.
2864 + */
2865 + if (engine->ops == &utrace_detached_ops) {
2866 + report->detaches = true;
2867 + return;
2870 + if (action < report->action)
2871 + report->action = action;
2873 + if (action != UTRACE_STOP) {
2874 + if (action < report->resume_action)
2875 + report->resume_action = action;
2877 + if (engine_wants_stop(engine)) {
2878 + spin_lock(&utrace->lock);
2879 + clear_engine_wants_stop(engine);
2880 + spin_unlock(&utrace->lock);
2883 + return;
2886 + if (!engine_wants_stop(engine)) {
2887 + spin_lock(&utrace->lock);
2888 + /*
2889 + * If utrace_control() came in and detached us
2890 + * before we got the lock, we must not stop now.
2891 + */
2892 + if (unlikely(engine->ops == &utrace_detached_ops))
2893 + report->detaches = true;
2894 + else
2895 + mark_engine_wants_stop(task, engine);
2896 + spin_unlock(&utrace->lock);
2901 + * Apply the return value of one engine callback to @report.
2902 + * Returns true if @engine detached and should not get any more callbacks.
2903 + */
2904 +static bool finish_callback(struct task_struct *task, struct utrace *utrace,
2905 + struct utrace_report *report,
2906 + struct utrace_engine *engine,
2907 + u32 ret)
2909 + report->result = ret & ~UTRACE_RESUME_MASK;
2910 + finish_callback_report(task, utrace, report, engine,
2911 + utrace_resume_action(ret));
2913 + /*
2914 + * Now that we have applied the effect of the return value,
2915 + * clear this so that utrace_barrier() can stop waiting.
2916 + * A subsequent utrace_control() can stop or resume @engine
2917 + * and know this was ordered after its callback's action.
2919 + * We don't need any barriers here because utrace_barrier()
2920 + * takes utrace->lock. If we touched engine->flags above,
2921 + * the lock guaranteed this change was before utrace_barrier()
2922 + * examined utrace->reporting.
2923 + */
2924 + utrace->reporting = NULL;
2926 + /*
2927 + * We've just done an engine callback. These are allowed to sleep,
2928 + * though all well-behaved ones restrict that to blocking kalloc()
2929 + * or quickly-acquired mutex_lock() and the like. This is a good
2930 + * place to make sure tracing engines don't introduce too much
2931 + * latency under voluntary preemption.
2932 + */
2933 + might_sleep();
2935 + return engine->ops == &utrace_detached_ops;
2939 + * Start the callbacks for @engine to consider @event (a bit mask).
2940 + * This makes the report_quiesce() callback first. If @engine wants
2941 + * a specific callback for @event, we return the ops vector to use.
2942 + * If not, we return NULL. The return value from the ops->callback
2943 + * function called should be passed to finish_callback().
2944 + */
2945 +static const struct utrace_engine_ops *start_callback(
2946 + struct utrace *utrace, struct utrace_report *report,
2947 + struct utrace_engine *engine, struct task_struct *task,
2948 + unsigned long event)
2950 + const struct utrace_engine_ops *ops;
2951 + unsigned long want;
2953 + /*
2954 + * This barrier ensures that we've set utrace->reporting before
2955 + * we examine engine->flags or engine->ops. utrace_barrier()
2956 + * relies on this ordering to indicate that the effect of any
2957 + * utrace_control() and utrace_set_events() calls is in place
2958 + * by the time utrace->reporting can be seen to be NULL.
2959 + */
2960 + utrace->reporting = engine;
2961 + smp_mb();
2963 + /*
2964 + * This pairs with the barrier in mark_engine_detached().
2965 + * It makes sure that we never see the old ops vector with
2966 + * the new flags, in case the original vector had no report_quiesce.
2967 + */
2968 + want = engine->flags;
2969 + smp_rmb();
2970 + ops = engine->ops;
2972 + if ((want & UTRACE_EVENT(QUIESCE)) || ops == &utrace_detached_ops) {
2973 + if (finish_callback(task, utrace, report, engine,
2974 + (*ops->report_quiesce)(report->action,
2975 + engine, event)))
2976 + return NULL;
2978 + if (!event) {
2979 + /* We only got here to report QUIESCE */
2980 + report->spurious = false;
2981 + return NULL;
2984 + /*
2985 + * finish_callback() reset utrace->reporting after the
2986 + * quiesce callback. Now we set it again (as above)
2987 + * before re-examining engine->flags, which could have
2988 + * been changed synchronously by ->report_quiesce or
2989 + * asynchronously by utrace_control() or utrace_set_events().
2990 + */
2991 + utrace->reporting = engine;
2992 + smp_mb();
2993 + want = engine->flags;
2996 + if (want & ENGINE_STOP)
2997 + report->action = UTRACE_STOP;
2999 + if (want & event) {
3000 + report->spurious = false;
3001 + return ops;
3004 + utrace->reporting = NULL;
3005 + return NULL;
3009 + * Do a normal reporting pass for engines interested in @event.
3010 + * @callback is the name of the member in the ops vector, and remaining
3011 + * args are the extras it takes after the standard three args.
3012 + */
3013 +#define REPORT_CALLBACKS(rev, task, utrace, report, event, callback, ...) \
3014 + do { \
3015 + struct utrace_engine *engine; \
3016 + const struct utrace_engine_ops *ops; \
3017 + list_for_each_entry##rev(engine, &utrace->attached, entry) { \
3018 + ops = start_callback(utrace, report, engine, task, \
3019 + event); \
3020 + if (!ops) \
3021 + continue; \
3022 + finish_callback(task, utrace, report, engine, \
3023 + (*ops->callback)(__VA_ARGS__)); \
3024 + } \
3025 + } while (0)
3026 +#define REPORT(task, utrace, report, event, callback, ...) \
3027 + do { \
3028 + start_report(utrace); \
3029 + REPORT_CALLBACKS(, task, utrace, report, event, callback, \
3030 + (report)->action, engine, ## __VA_ARGS__); \
3031 + finish_report(task, utrace, report, true); \
3032 + } while (0)
3035 + * Called iff UTRACE_EVENT(EXEC) flag is set.
3036 + */
3037 +void utrace_report_exec(struct linux_binfmt *fmt, struct linux_binprm *bprm,
3038 + struct pt_regs *regs)
3040 + struct task_struct *task = current;
3041 + struct utrace *utrace = task_utrace_struct(task);
3042 + INIT_REPORT(report);
3044 + REPORT(task, utrace, &report, UTRACE_EVENT(EXEC),
3045 + report_exec, fmt, bprm, regs);
3048 +static u32 do_report_syscall_entry(struct pt_regs *regs,
3049 + struct task_struct *task,
3050 + struct utrace *utrace,
3051 + struct utrace_report *report,
3052 + u32 resume_report)
3054 + start_report(utrace);
3055 + REPORT_CALLBACKS(_reverse, task, utrace, report,
3056 + UTRACE_EVENT(SYSCALL_ENTRY), report_syscall_entry,
3057 + resume_report | report->result | report->action,
3058 + engine, regs);
3059 + finish_report(task, utrace, report, false);
3061 + if (report->action != UTRACE_STOP)
3062 + return 0;
3064 + utrace_stop(task, utrace, report->resume_action);
3066 + if (fatal_signal_pending(task)) {
3067 + /*
3068 + * We are continuing despite UTRACE_STOP because of a
3069 + * SIGKILL. Don't let the system call actually proceed.
3070 + */
3071 + report->result = UTRACE_SYSCALL_ABORT;
3072 + } else if (utrace->resume <= UTRACE_REPORT) {
3073 + /*
3074 + * If we've been asked for another report after our stop,
3075 + * go back to report (and maybe stop) again before we run
3076 + * the system call. The second (and later) reports are
3077 + * marked with the UTRACE_SYSCALL_RESUMED flag so that
3078 + * engines know this is a second report at the same
3079 + * entry. This gives them the chance to examine the
3080 + * registers anew after they might have been changed
3081 + * while we were stopped.
3082 + */
3083 + report->detaches = false;
3084 + report->spurious = true;
3085 + report->action = report->resume_action = UTRACE_RESUME;
3086 + return UTRACE_SYSCALL_RESUMED;
3089 + return 0;
3093 + * Called iff UTRACE_EVENT(SYSCALL_ENTRY) flag is set.
3094 + * Return true to prevent the system call.
3095 + */
3096 +bool utrace_report_syscall_entry(struct pt_regs *regs)
3098 + struct task_struct *task = current;
3099 + struct utrace *utrace = task_utrace_struct(task);
3100 + INIT_REPORT(report);
3101 + u32 resume_report = 0;
3103 + do {
3104 + resume_report = do_report_syscall_entry(regs, task, utrace,
3105 + &report, resume_report);
3106 + } while (resume_report);
3108 + return utrace_syscall_action(report.result) == UTRACE_SYSCALL_ABORT;
3112 + * Called iff UTRACE_EVENT(SYSCALL_EXIT) flag is set.
3113 + */
3114 +void utrace_report_syscall_exit(struct pt_regs *regs)
3116 + struct task_struct *task = current;
3117 + struct utrace *utrace = task_utrace_struct(task);
3118 + INIT_REPORT(report);
3120 + REPORT(task, utrace, &report, UTRACE_EVENT(SYSCALL_EXIT),
3121 + report_syscall_exit, regs);
3125 + * Called iff UTRACE_EVENT(CLONE) flag is set.
3126 + * This notification call blocks the wake_up_new_task call on the child.
3127 + * So we must not quiesce here. tracehook_report_clone_complete will do
3128 + * a quiescence check momentarily.
3129 + */
3130 +void utrace_report_clone(unsigned long clone_flags, struct task_struct *child)
3132 + struct task_struct *task = current;
3133 + struct utrace *utrace = task_utrace_struct(task);
3134 + INIT_REPORT(report);
3136 + /*
3137 + * We don't use the REPORT() macro here, because we need
3138 + * to clear utrace->cloning before finish_report().
3139 + * After finish_report(), utrace can be a stale pointer
3140 + * in cases when report.action is still UTRACE_RESUME.
3141 + */
3142 + start_report(utrace);
3143 + utrace->cloning = child;
3145 + REPORT_CALLBACKS(, task, utrace, &report,
3146 + UTRACE_EVENT(CLONE), report_clone,
3147 + report.action, engine, clone_flags, child);
3149 + utrace->cloning = NULL;
3150 + finish_report(task, utrace, &report, !(clone_flags & CLONE_VFORK));
3152 + /*
3153 + * For a vfork, we will go into an uninterruptible block waiting
3154 + * for the child. We need UTRACE_STOP to happen before this, not
3155 + * after. For CLONE_VFORK, utrace_finish_vfork() will be called.
3156 + */
3157 + if (report.action == UTRACE_STOP && (clone_flags & CLONE_VFORK)) {
3158 + spin_lock(&utrace->lock);
3159 + utrace->vfork_stop = 1;
3160 + spin_unlock(&utrace->lock);
3165 + * We're called after utrace_report_clone() for a CLONE_VFORK.
3166 + * If UTRACE_STOP was left from the clone report, we stop here.
3167 + * After this, we'll enter the uninterruptible wait_for_completion()
3168 + * waiting for the child.
3169 + */
3170 +void utrace_finish_vfork(struct task_struct *task)
3172 + struct utrace *utrace = task_utrace_struct(task);
3174 + if (utrace->vfork_stop) {
3175 + spin_lock(&utrace->lock);
3176 + utrace->vfork_stop = 0;
3177 + spin_unlock(&utrace->lock);
3178 + utrace_stop(task, utrace, UTRACE_RESUME); /* XXX */
3183 + * Called iff UTRACE_EVENT(JCTL) flag is set.
3185 + * Called with siglock held.
3186 + */
3187 +void utrace_report_jctl(int notify, int what)
3189 + struct task_struct *task = current;
3190 + struct utrace *utrace = task_utrace_struct(task);
3191 + INIT_REPORT(report);
3193 + spin_unlock_irq(&task->sighand->siglock);
3195 + REPORT(task, utrace, &report, UTRACE_EVENT(JCTL),
3196 + report_jctl, what, notify);
3198 + spin_lock_irq(&task->sighand->siglock);
3202 + * Called iff UTRACE_EVENT(EXIT) flag is set.
3203 + */
3204 +void utrace_report_exit(long *exit_code)
3206 + struct task_struct *task = current;
3207 + struct utrace *utrace = task_utrace_struct(task);
3208 + INIT_REPORT(report);
3209 + long orig_code = *exit_code;
3211 + REPORT(task, utrace, &report, UTRACE_EVENT(EXIT),
3212 + report_exit, orig_code, exit_code);
3214 + if (report.action == UTRACE_STOP)
3215 + utrace_stop(task, utrace, report.resume_action);
3219 + * Called iff UTRACE_EVENT(DEATH) or UTRACE_EVENT(QUIESCE) flag is set.
3221 + * It is always possible that we are racing with utrace_release_task here.
3222 + * For this reason, utrace_release_task checks for the event bits that get
3223 + * us here, and delays its cleanup for us to do.
3224 + */
3225 +void utrace_report_death(struct task_struct *task, struct utrace *utrace,
3226 + bool group_dead, int signal)
3228 + INIT_REPORT(report);
3230 + BUG_ON(!task->exit_state);
3232 + /*
3233 + * We are presently considered "quiescent"--which is accurate
3234 + * inasmuch as we won't run any more user instructions ever again.
3235 + * But for utrace_control and utrace_set_events to be robust, they
3236 + * must be sure whether or not we will run any more callbacks. If
3237 + * a call comes in before we do, taking the lock here synchronizes
3238 + * us so we don't run any callbacks just disabled. Calls that come
3239 + * in while we're running the callbacks will see the exit.death
3240 + * flag and know that we are not yet fully quiescent for purposes
3241 + * of detach bookkeeping.
3242 + */
3243 + spin_lock(&utrace->lock);
3244 + BUG_ON(utrace->death);
3245 + utrace->death = 1;
3246 + utrace->resume = UTRACE_RESUME;
3247 + splice_attaching(utrace);
3248 + spin_unlock(&utrace->lock);
3250 + REPORT_CALLBACKS(, task, utrace, &report, UTRACE_EVENT(DEATH),
3251 + report_death, engine, group_dead, signal);
3253 + utrace_maybe_reap(task, utrace, false);
3257 + * Finish the last reporting pass before returning to user mode.
3258 + */
3259 +static void finish_resume_report(struct task_struct *task,
3260 + struct utrace *utrace,
3261 + struct utrace_report *report)
3263 + finish_report_reset(task, utrace, report);
3265 + switch (report->action) {
3266 + case UTRACE_STOP:
3267 + utrace_stop(task, utrace, report->resume_action);
3268 + break;
3270 + case UTRACE_INTERRUPT:
3271 + if (!signal_pending(task))
3272 + set_tsk_thread_flag(task, TIF_SIGPENDING);
3273 + break;
3275 + case UTRACE_BLOCKSTEP:
3276 + if (likely(arch_has_block_step())) {
3277 + user_enable_block_step(task);
3278 + break;
3281 + /*
3282 + * This means some callback is to blame for failing
3283 + * to check arch_has_block_step() itself. Warn and
3284 + * then fall through to treat it as SINGLESTEP.
3285 + */
3286 + WARN(1, "UTRACE_BLOCKSTEP when !arch_has_block_step()");
3288 + case UTRACE_SINGLESTEP:
3289 + if (likely(arch_has_single_step())) {
3290 + user_enable_single_step(task);
3291 + } else {
3292 + /*
3293 + * This means some callback is to blame for failing
3294 + * to check arch_has_single_step() itself. Spew
3295 + * about it so the loser will fix his module.
3296 + */
3297 + WARN(1,
3298 + "UTRACE_SINGLESTEP when !arch_has_single_step()");
3300 + break;
3302 + case UTRACE_REPORT:
3303 + case UTRACE_RESUME:
3304 + default:
3305 + user_disable_single_step(task);
3306 + break;
3311 + * This is called when TIF_NOTIFY_RESUME had been set (and is now clear).
3312 + * We are close to user mode, and this is the place to report or stop.
3313 + * When we return, we're going to user mode or into the signals code.
3314 + */
3315 +void utrace_resume(struct task_struct *task, struct pt_regs *regs)
3317 + struct utrace *utrace = task_utrace_struct(task);
3318 + INIT_REPORT(report);
3319 + struct utrace_engine *engine;
3321 + /*
3322 + * Some machines get here with interrupts disabled. The same arch
3323 + * code path leads to calling into get_signal_to_deliver(), which
3324 + * implicitly reenables them by virtue of spin_unlock_irq.
3325 + */
3326 + local_irq_enable();
3328 + /*
3329 + * If this flag is still set it's because there was a signal
3330 + * handler setup done but no report_signal following it. Clear
3331 + * the flag before we get to user so it doesn't confuse us later.
3332 + */
3333 + if (unlikely(utrace->signal_handler)) {
3334 + spin_lock(&utrace->lock);
3335 + utrace->signal_handler = 0;
3336 + spin_unlock(&utrace->lock);
3339 + /*
3340 + * Update our bookkeeping even if there are no callbacks made here.
3341 + */
3342 + report.action = start_report(utrace);
3344 + switch (report.action) {
3345 + case UTRACE_RESUME:
3346 + /*
3347 + * Anything we might have done was already handled by
3348 + * utrace_get_signal(), or this is an entirely spurious
3349 + * call. (The arch might use TIF_NOTIFY_RESUME for other
3350 + * purposes as well as calling us.)
3351 + */
3352 + return;
3353 + case UTRACE_REPORT:
3354 + if (unlikely(!(task->utrace_flags & UTRACE_EVENT(QUIESCE))))
3355 + break;
3356 + /*
3357 + * Do a simple reporting pass, with no specific
3358 + * callback after report_quiesce.
3359 + */
3360 + report.action = UTRACE_RESUME;
3361 + list_for_each_entry(engine, &utrace->attached, entry)
3362 + start_callback(utrace, &report, engine, task, 0);
3363 + break;
3364 + default:
3365 + /*
3366 + * Even if this report was truly spurious, there is no need
3367 + * for utrace_reset() now. TIF_NOTIFY_RESUME was already
3368 + * cleared--it doesn't stay spuriously set.
3369 + */
3370 + report.spurious = false;
3371 + break;
3374 + /*
3375 + * Finish the report and either stop or get ready to resume.
3376 + * If utrace->resume was not UTRACE_REPORT, this applies its
3377 + * effect now (i.e. step or interrupt).
3378 + */
3379 + finish_resume_report(task, utrace, &report);
3383 + * Return true if current has forced signal_pending().
3385 + * This is called only when current->utrace_flags is nonzero, so we know
3386 + * that current->utrace must be set. It's not inlined in tracehook.h
3387 + * just so that struct utrace can stay opaque outside this file.
3388 + */
3389 +bool utrace_interrupt_pending(void)
3391 + return task_utrace_struct(current)->resume == UTRACE_INTERRUPT;
3395 + * Take the siglock and push @info back on our queue.
3396 + * Returns with @task->sighand->siglock held.
3397 + */
3398 +static void push_back_signal(struct task_struct *task, siginfo_t *info)
3399 + __acquires(task->sighand->siglock)
3401 + struct sigqueue *q;
3403 + if (unlikely(!info->si_signo)) { /* Oh, a wise guy! */
3404 + spin_lock_irq(&task->sighand->siglock);
3405 + return;
3408 + q = sigqueue_alloc();
3409 + if (likely(q)) {
3410 + q->flags = 0;
3411 + copy_siginfo(&q->info, info);
3414 + spin_lock_irq(&task->sighand->siglock);
3416 + sigaddset(&task->pending.signal, info->si_signo);
3417 + if (likely(q))
3418 + list_add(&q->list, &task->pending.list);
3420 + set_tsk_thread_flag(task, TIF_SIGPENDING);
3424 + * This is the hook from the signals code, called with the siglock held.
3425 + * Here is the ideal place to stop. We also dequeue and intercept signals.
3426 + */
3427 +int utrace_get_signal(struct task_struct *task, struct pt_regs *regs,
3428 + siginfo_t *info, struct k_sigaction *return_ka)
3429 + __releases(task->sighand->siglock)
3430 + __acquires(task->sighand->siglock)
3432 + struct utrace *utrace;
3433 + struct k_sigaction *ka;
3434 + INIT_REPORT(report);
3435 + struct utrace_engine *engine;
3436 + const struct utrace_engine_ops *ops;
3437 + unsigned long event, want;
3438 + u32 ret;
3439 + int signr;
3441 + utrace = task_utrace_struct(task);
3442 + if (utrace->resume < UTRACE_RESUME ||
3443 + utrace->pending_attach || utrace->signal_handler) {
3444 + enum utrace_resume_action resume;
3446 + /*
3447 + * We've been asked for an explicit report before we
3448 + * even check for pending signals.
3449 + */
3451 + spin_unlock_irq(&task->sighand->siglock);
3453 + spin_lock(&utrace->lock);
3455 + splice_attaching(utrace);
3457 + report.result = utrace->signal_handler ?
3458 + UTRACE_SIGNAL_HANDLER : UTRACE_SIGNAL_REPORT;
3459 + utrace->signal_handler = 0;
3461 + resume = utrace->resume;
3462 + utrace->resume = UTRACE_RESUME;
3464 + spin_unlock(&utrace->lock);
3466 + /*
3467 + * Make sure signal_pending() only returns true
3468 + * if there are real signals pending.
3469 + */
3470 + if (signal_pending(task)) {
3471 + spin_lock_irq(&task->sighand->siglock);
3472 + recalc_sigpending();
3473 + spin_unlock_irq(&task->sighand->siglock);
3476 + if (resume > UTRACE_REPORT) {
3477 + /*
3478 + * We only got here to process utrace->resume.
3479 + * Despite no callbacks, this report is not spurious.
3480 + */
3481 + report.action = resume;
3482 + report.spurious = false;
3483 + finish_resume_report(task, utrace, &report);
3484 + return -1;
3485 + } else if (!(task->utrace_flags & UTRACE_EVENT(QUIESCE))) {
3486 + /*
3487 + * We only got here to clear utrace->signal_handler.
3488 + */
3489 + return -1;
3492 + /*
3493 + * Do a reporting pass for no signal, just for EVENT(QUIESCE).
3494 + * The engine callbacks can fill in *info and *return_ka.
3495 + * We'll pass NULL for the @orig_ka argument to indicate
3496 + * that there was no original signal.
3497 + */
3498 + event = 0;
3499 + ka = NULL;
3500 + memset(return_ka, 0, sizeof *return_ka);
3501 + } else if (!(task->utrace_flags & UTRACE_EVENT_SIGNAL_ALL) ||
3502 + unlikely(task->signal->group_stop_count)) {
3503 + /*
3504 + * If no engine is interested in intercepting signals or
3505 + * we must stop, let the caller just dequeue them normally
3506 + * or participate in group-stop.
3507 + */
3508 + return 0;
3509 + } else {
3510 + /*
3511 + * Steal the next signal so we can let tracing engines
3512 + * examine it. From the signal number and sigaction,
3513 + * determine what normal delivery would do. If no
3514 + * engine perturbs it, we'll do that by returning the
3515 + * signal number after setting *return_ka.
3516 + */
3517 + signr = dequeue_signal(task, &task->blocked, info);
3518 + if (signr == 0)
3519 + return signr;
3520 + BUG_ON(signr != info->si_signo);
3522 + ka = &task->sighand->action[signr - 1];
3523 + *return_ka = *ka;
3525 + /*
3526 + * We are never allowed to interfere with SIGKILL.
3527 + * Just punt after filling in *return_ka for our caller.
3528 + */
3529 + if (signr == SIGKILL)
3530 + return signr;
3532 + if (ka->sa.sa_handler == SIG_IGN) {
3533 + event = UTRACE_EVENT(SIGNAL_IGN);
3534 + report.result = UTRACE_SIGNAL_IGN;
3535 + } else if (ka->sa.sa_handler != SIG_DFL) {
3536 + event = UTRACE_EVENT(SIGNAL);
3537 + report.result = UTRACE_SIGNAL_DELIVER;
3538 + } else if (sig_kernel_coredump(signr)) {
3539 + event = UTRACE_EVENT(SIGNAL_CORE);
3540 + report.result = UTRACE_SIGNAL_CORE;
3541 + } else if (sig_kernel_ignore(signr)) {
3542 + event = UTRACE_EVENT(SIGNAL_IGN);
3543 + report.result = UTRACE_SIGNAL_IGN;
3544 + } else if (signr == SIGSTOP) {
3545 + event = UTRACE_EVENT(SIGNAL_STOP);
3546 + report.result = UTRACE_SIGNAL_STOP;
3547 + } else if (sig_kernel_stop(signr)) {
3548 + event = UTRACE_EVENT(SIGNAL_STOP);
3549 + report.result = UTRACE_SIGNAL_TSTP;
3550 + } else {
3551 + event = UTRACE_EVENT(SIGNAL_TERM);
3552 + report.result = UTRACE_SIGNAL_TERM;
3555 + /*
3556 + * Now that we know what event type this signal is, we
3557 + * can short-circuit if no engines care about those.
3558 + */
3559 + if ((task->utrace_flags & (event | UTRACE_EVENT(QUIESCE))) == 0)
3560 + return signr;
3562 + /*
3563 + * We have some interested engines, so tell them about
3564 + * the signal and let them change its disposition.
3565 + */
3566 + spin_unlock_irq(&task->sighand->siglock);
3569 + /*
3570 + * This reporting pass chooses what signal disposition we'll act on.
3571 + */
3572 + list_for_each_entry(engine, &utrace->attached, entry) {
3573 + /*
3574 + * See start_callback() comment about this barrier.
3575 + */
3576 + utrace->reporting = engine;
3577 + smp_mb();
3579 + /*
3580 + * This pairs with the barrier in mark_engine_detached(),
3581 + * see start_callback() comments.
3582 + */
3583 + want = engine->flags;
3584 + smp_rmb();
3585 + ops = engine->ops;
3587 + if ((want & (event | UTRACE_EVENT(QUIESCE))) == 0) {
3588 + utrace->reporting = NULL;
3589 + continue;
3592 + if (ops->report_signal)
3593 + ret = (*ops->report_signal)(
3594 + report.result | report.action, engine,
3595 + regs, info, ka, return_ka);
3596 + else
3597 + ret = (report.result | (*ops->report_quiesce)(
3598 + report.action, engine, event));
3600 + /*
3601 + * Avoid a tight loop reporting again and again if some
3602 + * engine is too stupid.
3603 + */
3604 + switch (utrace_resume_action(ret)) {
3605 + default:
3606 + break;
3607 + case UTRACE_INTERRUPT:
3608 + case UTRACE_REPORT:
3609 + ret = (ret & ~UTRACE_RESUME_MASK) | UTRACE_RESUME;
3610 + break;
3613 + finish_callback(task, utrace, &report, engine, ret);
3616 + /*
3617 + * We express the chosen action to the signals code in terms
3618 + * of a representative signal whose default action does it.
3619 + * Our caller uses our return value (signr) to decide what to
3620 + * do, but uses info->si_signo as the signal number to report.
3621 + */
3622 + switch (utrace_signal_action(report.result)) {
3623 + case UTRACE_SIGNAL_TERM:
3624 + signr = SIGTERM;
3625 + break;
3627 + case UTRACE_SIGNAL_CORE:
3628 + signr = SIGQUIT;
3629 + break;
3631 + case UTRACE_SIGNAL_STOP:
3632 + signr = SIGSTOP;
3633 + break;
3635 + case UTRACE_SIGNAL_TSTP:
3636 + signr = SIGTSTP;
3637 + break;
3639 + case UTRACE_SIGNAL_DELIVER:
3640 + signr = info->si_signo;
3642 + if (return_ka->sa.sa_handler == SIG_DFL) {
3643 + /*
3644 + * We'll do signr's normal default action.
3645 + * For ignore, we'll fall through below.
3646 + * For stop/death, break locks and returns it.
3647 + */
3648 + if (likely(signr) && !sig_kernel_ignore(signr))
3649 + break;
3650 + } else if (return_ka->sa.sa_handler != SIG_IGN &&
3651 + likely(signr)) {
3652 + /*
3653 + * Complete the bookkeeping after the report.
3654 + * The handler will run. If an engine wanted to
3655 + * stop or step, then make sure we do another
3656 + * report after signal handler setup.
3657 + */
3658 + if (report.action != UTRACE_RESUME)
3659 + report.action = UTRACE_INTERRUPT;
3660 + finish_report(task, utrace, &report, true);
3662 + if (unlikely(report.result & UTRACE_SIGNAL_HOLD))
3663 + push_back_signal(task, info);
3664 + else
3665 + spin_lock_irq(&task->sighand->siglock);
3667 + /*
3668 + * We do the SA_ONESHOT work here since the
3669 + * normal path will only touch *return_ka now.
3670 + */
3671 + if (unlikely(return_ka->sa.sa_flags & SA_ONESHOT)) {
3672 + return_ka->sa.sa_flags &= ~SA_ONESHOT;
3673 + if (likely(valid_signal(signr))) {
3674 + ka = &task->sighand->action[signr - 1];
3675 + ka->sa.sa_handler = SIG_DFL;
3679 + return signr;
3682 + /* Fall through for an ignored signal. */
3684 + case UTRACE_SIGNAL_IGN:
3685 + case UTRACE_SIGNAL_REPORT:
3686 + default:
3687 + /*
3688 + * If the signal is being ignored, then we are on the way
3689 + * directly back to user mode. We can stop here, or step,
3690 + * as in utrace_resume(), above. After we've dealt with that,
3691 + * our caller will relock and come back through here.
3692 + */
3693 + finish_resume_report(task, utrace, &report);
3695 + if (unlikely(fatal_signal_pending(task))) {
3696 + /*
3697 + * The only reason we woke up now was because of a
3698 + * SIGKILL. Don't do normal dequeuing in case it
3699 + * might get a signal other than SIGKILL. That would
3700 + * perturb the death state so it might differ from
3701 + * what the debugger would have allowed to happen.
3702 + * Instead, pluck out just the SIGKILL to be sure
3703 + * we'll die immediately with nothing else different
3704 + * from the quiescent state the debugger wanted us in.
3705 + */
3706 + sigset_t sigkill_only;
3707 + siginitsetinv(&sigkill_only, sigmask(SIGKILL));
3708 + spin_lock_irq(&task->sighand->siglock);
3709 + signr = dequeue_signal(task, &sigkill_only, info);
3710 + BUG_ON(signr != SIGKILL);
3711 + *return_ka = task->sighand->action[SIGKILL - 1];
3712 + return signr;
3715 + if (unlikely(report.result & UTRACE_SIGNAL_HOLD)) {
3716 + push_back_signal(task, info);
3717 + spin_unlock_irq(&task->sighand->siglock);
3720 + return -1;
3723 + /*
3724 + * Complete the bookkeeping after the report.
3725 + * This sets utrace->resume if UTRACE_STOP was used.
3726 + */
3727 + finish_report(task, utrace, &report, true);
3729 + return_ka->sa.sa_handler = SIG_DFL;
3731 + /*
3732 + * If this signal is fatal, si_signo gets through as exit_code.
3733 + * We can't allow a completely bogus value there or else core
3734 + * kernel code can freak out. (If an engine wants to control
3735 + * the exit_code value exactly, it can do so in report_exit.)
3736 + * We'll produce a big complaint in dmesg, but won't crash.
3737 + * That's nicer for debugging your utrace engine.
3738 + */
3739 + if (unlikely(info->si_signo & 0x80)) {
3740 + WARN(1, "utrace engine left bogus si_signo value!");
3741 + info->si_signo = SIGTRAP;
3744 + if (unlikely(report.result & UTRACE_SIGNAL_HOLD))
3745 + push_back_signal(task, info);
3746 + else
3747 + spin_lock_irq(&task->sighand->siglock);
3749 + if (sig_kernel_stop(signr))
3750 + task->jobctl |= JOBCTL_STOP_DEQUEUED;
3752 + return signr;
3756 + * This gets called after a signal handler has been set up.
3757 + * We set a flag so the next report knows it happened.
3758 + * If we're already stepping, make sure we do a report_signal.
3759 + * If not, make sure we get into utrace_resume() where we can
3760 + * clear the signal_handler flag before resuming.
3761 + */
3762 +void utrace_signal_handler(struct task_struct *task, int stepping)
3764 + struct utrace *utrace = task_utrace_struct(task);
3766 + spin_lock(&utrace->lock);
3768 + utrace->signal_handler = 1;
3769 + if (utrace->resume > UTRACE_INTERRUPT) {
3770 + if (stepping) {
3771 + utrace->resume = UTRACE_INTERRUPT;
3772 + set_tsk_thread_flag(task, TIF_SIGPENDING);
3773 + } else if (utrace->resume == UTRACE_RESUME) {
3774 + set_tsk_thread_flag(task, TIF_NOTIFY_RESUME);
3778 + spin_unlock(&utrace->lock);
3781 +/**
3782 + * utrace_prepare_examine - prepare to examine thread state
3783 + * @target: thread of interest, a &struct task_struct pointer
3784 + * @engine: engine pointer returned by utrace_attach_task()
3785 + * @exam: temporary state, a &struct utrace_examiner pointer
3787 + * This call prepares to safely examine the thread @target using
3788 + * &struct user_regset calls, or direct access to thread-synchronous fields.
3790 + * When @target is current, this call is superfluous. When @target is
3791 + * another thread, it must be held stopped via %UTRACE_STOP by @engine.
3793 + * This call may block the caller until @target stays stopped, so it must
3794 + * be called only after the caller is sure @target is about to unschedule.
3795 + * This means a zero return from a utrace_control() call on @engine giving
3796 + * %UTRACE_STOP, or a report_quiesce() or report_signal() callback to
3797 + * @engine that used %UTRACE_STOP in its return value.
3799 + * Returns -%ESRCH if @target is dead or -%EINVAL if %UTRACE_STOP was
3800 + * not used. If @target has started running again despite %UTRACE_STOP
3801 + * (for %SIGKILL or a spurious wakeup), this call returns -%EAGAIN.
3803 + * When this call returns zero, it's safe to use &struct user_regset
3804 + * calls and task_user_regset_view() on @target and to examine some of
3805 + * its fields directly. When the examination is complete, a
3806 + * utrace_finish_examine() call must follow to check whether it was
3807 + * completed safely.
3808 + */
3809 +int utrace_prepare_examine(struct task_struct *target,
3810 + struct utrace_engine *engine,
3811 + struct utrace_examiner *exam)
3813 + int ret = 0;
3815 + if (unlikely(target == current))
3816 + return 0;
3818 + rcu_read_lock();
3819 + if (unlikely(!engine_wants_stop(engine)))
3820 + ret = -EINVAL;
3821 + else if (unlikely(target->exit_state))
3822 + ret = -ESRCH;
3823 + else {
3824 + exam->state = target->state;
3825 + if (unlikely(exam->state == TASK_RUNNING))
3826 + ret = -EAGAIN;
3827 + else
3828 + get_task_struct(target);
3830 + rcu_read_unlock();
3832 + if (likely(!ret)) {
3833 + exam->ncsw = wait_task_inactive(target, exam->state);
3834 + put_task_struct(target);
3835 + if (unlikely(!exam->ncsw))
3836 + ret = -EAGAIN;
3839 + return ret;
3841 +EXPORT_SYMBOL_GPL(utrace_prepare_examine);
3843 +/**
3844 + * utrace_finish_examine - complete an examination of thread state
3845 + * @target: thread of interest, a &struct task_struct pointer
3846 + * @engine: engine pointer returned by utrace_attach_task()
3847 + * @exam: pointer passed to utrace_prepare_examine() call
3849 + * This call completes an examination on the thread @target begun by a
3850 + * paired utrace_prepare_examine() call with the same arguments that
3851 + * returned success (zero).
3853 + * When @target is current, this call is superfluous. When @target is
3854 + * another thread, this returns zero if @target has remained unscheduled
3855 + * since the paired utrace_prepare_examine() call returned zero.
3857 + * When this returns an error, any examination done since the paired
3858 + * utrace_prepare_examine() call is unreliable and the data extracted
3859 + * should be discarded. The error is -%EINVAL if @engine is not
3860 + * keeping @target stopped, or -%EAGAIN if @target woke up unexpectedly.
3861 + */
3862 +int utrace_finish_examine(struct task_struct *target,
3863 + struct utrace_engine *engine,
3864 + struct utrace_examiner *exam)
3866 + int ret = 0;
3868 + if (unlikely(target == current))
3869 + return 0;
3871 + rcu_read_lock();
3872 + if (unlikely(!engine_wants_stop(engine)))
3873 + ret = -EINVAL;
3874 + else if (unlikely(target->state != exam->state))
3875 + ret = -EAGAIN;
3876 + else
3877 + get_task_struct(target);
3878 + rcu_read_unlock();
3880 + if (likely(!ret)) {
3881 + unsigned long ncsw = wait_task_inactive(target, exam->state);
3882 + if (unlikely(ncsw != exam->ncsw))
3883 + ret = -EAGAIN;
3884 + put_task_struct(target);
3887 + return ret;
3889 +EXPORT_SYMBOL_GPL(utrace_finish_examine);
3892 + * This is declared in linux/regset.h and defined in machine-dependent
3893 + * code. We put the export here to ensure no machine forgets it.
3894 + */
3895 +EXPORT_SYMBOL_GPL(task_user_regset_view);
3898 + * Called with rcu_read_lock() held.
3899 + */
3900 +void task_utrace_proc_status(struct seq_file *m, struct task_struct *p)
3902 + seq_printf(m, "Utrace:\t%lx\n", p->utrace_flags);
3905 1.5.5.1
3908 From oleg@redhat.com Mon Nov 21 15:06:17 2011
3909 Return-Path: oleg@redhat.com
3910 Received: from zmta02.collab.prod.int.phx2.redhat.com (LHLO
3911 zmta02.collab.prod.int.phx2.redhat.com) (10.5.5.32) by
3912 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
3913 15:06:17 -0500 (EST)
3914 Received: from localhost (localhost.localdomain [127.0.0.1])
3915 by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 1CCED128D6B;
3916 Mon, 21 Nov 2011 15:06:17 -0500 (EST)
3917 Received: from zmta02.collab.prod.int.phx2.redhat.com ([127.0.0.1])
3918 by localhost (zmta02.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
3919 with ESMTP id FWdJDV74o-ay; Mon, 21 Nov 2011 15:06:17 -0500 (EST)
3920 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23])
3921 by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 082081285E4;
3922 Mon, 21 Nov 2011 15:06:17 -0500 (EST)
3923 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
3924 by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6E7d015251;
3925 Mon, 21 Nov 2011 15:06:15 -0500
3926 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
3927 oleg@redhat.com; Mon, 21 Nov 2011 21:01:35 +0100 (CET)
3928 Date: Mon, 21 Nov 2011 21:01:33 +0100
3929 From: Oleg Nesterov <oleg@redhat.com>
3930 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
3931 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
3932 Kyle McMartin <kmcmartin@redhat.com>
3933 Cc: kernel@lists.fedoraproject.org
3934 Subject: [PATCH 02/33] utrace: add utrace_init_task/utrace_free_task calls
3935 Message-ID: <20111121200133.GA27759@redhat.com>
3936 MIME-Version: 1.0
3937 Content-Type: text/plain; charset=us-ascii
3938 Content-Disposition: inline
3939 In-Reply-To: <20111121200039.GA27699@redhat.com>
3940 User-Agent: Mutt/1.5.18 (2008-05-17)
3941 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23
3942 Status: RO
3943 Content-Length: 1295
3944 Lines: 47
3946 Add the necessary copy_process()->utrace_init_task() and
3947 free_task()->utrace_free_task() calls.
3949 Originally this was the part of "utrace core" patch, but since
3950 tracehooks are dying it doesn't make sense to reintroduce them.
3951 Instead, just call the utrace_ helpers directly. This is fine
3952 even without CONFIG_UTRACE, gcc is smart enough to optimize out
3953 the code in free_task().
3955 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
3957 kernel/fork.c | 5 +++++
3958 1 files changed, 5 insertions(+), 0 deletions(-)
3960 diff --git a/kernel/fork.c b/kernel/fork.c
3961 index ba0d172..b8719c2 100644
3962 --- a/kernel/fork.c
3963 +++ b/kernel/fork.c
3964 @@ -66,6 +66,7 @@
3965 #include <linux/user-return-notifier.h>
3966 #include <linux/oom.h>
3967 #include <linux/khugepaged.h>
3968 +#include <linux/utrace.h>
3970 #include <asm/pgtable.h>
3971 #include <asm/pgalloc.h>
3972 @@ -167,6 +168,8 @@ void free_task(struct task_struct *tsk)
3973 free_thread_info(tsk->stack);
3974 rt_mutex_debug_task_free(tsk);
3975 ftrace_graph_exit_task(tsk);
3976 + if (task_utrace_struct(tsk))
3977 + utrace_free_task(tsk);
3978 free_task_struct(tsk);
3980 EXPORT_SYMBOL(free_task);
3981 @@ -1093,6 +1096,8 @@ static struct task_struct *copy_process(unsigned long clone_flags,
3982 if (!p)
3983 goto fork_out;
3985 + utrace_init_task(p);
3987 ftrace_graph_init_task(p);
3989 rt_mutex_init_task(p);
3991 1.5.5.1
3994 From oleg@redhat.com Mon Nov 21 15:06:20 2011
3995 Return-Path: oleg@redhat.com
3996 Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
3997 zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
3998 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
3999 15:06:20 -0500 (EST)
4000 Received: from localhost (localhost.localdomain [127.0.0.1])
4001 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 8779AD87BC;
4002 Mon, 21 Nov 2011 15:06:20 -0500 (EST)
4003 Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
4004 by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
4005 with ESMTP id umzAMwRd7rMt; Mon, 21 Nov 2011 15:06:20 -0500 (EST)
4006 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23])
4007 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 72C98D8707;
4008 Mon, 21 Nov 2011 15:06:20 -0500 (EST)
4009 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
4010 by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6HAj015261;
4011 Mon, 21 Nov 2011 15:06:18 -0500
4012 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
4013 oleg@redhat.com; Mon, 21 Nov 2011 21:01:39 +0100 (CET)
4014 Date: Mon, 21 Nov 2011 21:01:36 +0100
4015 From: Oleg Nesterov <oleg@redhat.com>
4016 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
4017 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
4018 Kyle McMartin <kmcmartin@redhat.com>
4019 Cc: kernel@lists.fedoraproject.org
4020 Subject: [PATCH 03/33] tracehooks: add utrace hooks
4021 Message-ID: <20111121200136.GA27766@redhat.com>
4022 MIME-Version: 1.0
4023 Content-Type: text/plain; charset=us-ascii
4024 Content-Disposition: inline
4025 In-Reply-To: <20111121200039.GA27699@redhat.com>
4026 User-Agent: Mutt/1.5.18 (2008-05-17)
4027 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23
4028 Status: RO
4029 Content-Length: 2310
4030 Lines: 75
4032 Add the necessary utrace hooks in the tracehooks which were not
4033 removed yet.
4035 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
4037 include/linux/tracehook.h | 22 +++++++++++++++++++++-
4038 1 files changed, 21 insertions(+), 1 deletions(-)
4040 diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
4041 index a71a292..8cc28bc 100644
4042 --- a/include/linux/tracehook.h
4043 +++ b/include/linux/tracehook.h
4044 @@ -49,6 +49,7 @@
4045 #include <linux/sched.h>
4046 #include <linux/ptrace.h>
4047 #include <linux/security.h>
4048 +#include <linux/utrace.h>
4049 struct linux_binprm;
4052 @@ -96,6 +97,9 @@ static inline void ptrace_report_syscall(struct pt_regs *regs)
4053 static inline __must_check int tracehook_report_syscall_entry(
4054 struct pt_regs *regs)
4056 + if ((task_utrace_flags(current) & UTRACE_EVENT(SYSCALL_ENTRY)) &&
4057 + utrace_report_syscall_entry(regs))
4058 + return 1;
4059 ptrace_report_syscall(regs);
4060 return 0;
4062 @@ -119,6 +123,9 @@ static inline __must_check int tracehook_report_syscall_entry(
4064 static inline void tracehook_report_syscall_exit(struct pt_regs *regs, int step)
4066 + if (task_utrace_flags(current) & UTRACE_EVENT(SYSCALL_EXIT))
4067 + utrace_report_syscall_exit(regs);
4069 if (step) {
4070 siginfo_t info;
4071 user_single_step_siginfo(current, regs, &info);
4072 @@ -148,6 +155,8 @@ static inline void tracehook_signal_handler(int sig, siginfo_t *info,
4073 const struct k_sigaction *ka,
4074 struct pt_regs *regs, int stepping)
4076 + if (task_utrace_flags(current))
4077 + utrace_signal_handler(current, stepping);
4078 if (stepping)
4079 ptrace_notify(SIGTRAP);
4081 @@ -179,10 +188,21 @@ static inline void set_notify_resume(struct task_struct *task)
4082 * asynchronously, this will be called again before we return to
4083 * user mode.
4085 - * Called without locks.
4086 + * Called without locks. However, on some machines this may be
4087 + * called with interrupts disabled.
4089 static inline void tracehook_notify_resume(struct pt_regs *regs)
4091 + struct task_struct *task = current;
4092 + /*
4093 + * Prevent the following store/load from getting ahead of the
4094 + * caller which clears TIF_NOTIFY_RESUME. This pairs with the
4095 + * implicit mb() before setting TIF_NOTIFY_RESUME in
4096 + * set_notify_resume().
4097 + */
4098 + smp_mb();
4099 + if (task_utrace_flags(task))
4100 + utrace_resume(task, regs);
4102 #endif /* TIF_NOTIFY_RESUME */
4105 1.5.5.1
4108 From oleg@redhat.com Mon Nov 21 15:06:23 2011
4109 Return-Path: oleg@redhat.com
4110 Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
4111 zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
4112 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
4113 15:06:23 -0500 (EST)
4114 Received: from localhost (localhost.localdomain [127.0.0.1])
4115 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 6559BD4FDE;
4116 Mon, 21 Nov 2011 15:06:23 -0500 (EST)
4117 Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
4118 by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
4119 with ESMTP id 6BjISiO+cS1U; Mon, 21 Nov 2011 15:06:23 -0500 (EST)
4120 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22])
4121 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 50366D4C39;
4122 Mon, 21 Nov 2011 15:06:23 -0500 (EST)
4123 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
4124 by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6KT9017533;
4125 Mon, 21 Nov 2011 15:06:21 -0500
4126 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
4127 oleg@redhat.com; Mon, 21 Nov 2011 21:01:42 +0100 (CET)
4128 Date: Mon, 21 Nov 2011 21:01:39 +0100
4129 From: Oleg Nesterov <oleg@redhat.com>
4130 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
4131 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
4132 Kyle McMartin <kmcmartin@redhat.com>
4133 Cc: kernel@lists.fedoraproject.org
4134 Subject: [PATCH 04/33] tracehooks: reintroduce
4135 tracehook_consider_fatal_signal()
4136 Message-ID: <20111121200139.GA27769@redhat.com>
4137 MIME-Version: 1.0
4138 Content-Type: text/plain; charset=us-ascii
4139 Content-Disposition: inline
4140 In-Reply-To: <20111121200039.GA27699@redhat.com>
4141 User-Agent: Mutt/1.5.18 (2008-05-17)
4142 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22
4143 Status: RO
4144 Content-Length: 3257
4145 Lines: 90
4147 Add the killed tracehook_consider_fatal_signal() back. It has multiple
4148 callers and it is not easy add the necessary checks inline.
4150 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
4152 arch/s390/kernel/traps.c | 4 ++--
4153 include/linux/tracehook.h | 22 ++++++++++++++++++++++
4154 kernel/signal.c | 4 ++--
4155 3 files changed, 26 insertions(+), 4 deletions(-)
4157 diff --git a/arch/s390/kernel/traps.c b/arch/s390/kernel/traps.c
4158 index a9807dd..f506e1b 100644
4159 --- a/arch/s390/kernel/traps.c
4160 +++ b/arch/s390/kernel/traps.c
4161 @@ -329,7 +329,7 @@ void __kprobes do_per_trap(struct pt_regs *regs)
4163 if (notify_die(DIE_SSTEP, "sstep", regs, 0, 0, SIGTRAP) == NOTIFY_STOP)
4164 return;
4165 - if (!current->ptrace)
4166 + if (!tracehook_consider_fatal_signal(current, SIGTRAP))
4167 return;
4168 info.si_signo = SIGTRAP;
4169 info.si_errno = 0;
4170 @@ -429,7 +429,7 @@ static void __kprobes illegal_op(struct pt_regs *regs, long pgm_int_code,
4171 if (get_user(*((__u16 *) opcode), (__u16 __user *) location))
4172 return;
4173 if (*((__u16 *) opcode) == S390_BREAKPOINT_U16) {
4174 - if (current->ptrace) {
4175 + if (tracehook_consider_fatal_signal(current, SIGTRAP)) {
4176 info.si_signo = SIGTRAP;
4177 info.si_errno = 0;
4178 info.si_code = TRAP_BRKPT;
4179 diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
4180 index 8cc28bc..ec2af67 100644
4181 --- a/include/linux/tracehook.h
4182 +++ b/include/linux/tracehook.h
4183 @@ -161,6 +161,28 @@ static inline void tracehook_signal_handler(int sig, siginfo_t *info,
4184 ptrace_notify(SIGTRAP);
4187 +/**
4188 + * tracehook_consider_fatal_signal - suppress special handling of fatal signal
4189 + * @task: task receiving the signal
4190 + * @sig: signal number being sent
4192 + * Return nonzero to prevent special handling of this termination signal.
4193 + * Normally handler for signal is %SIG_DFL. It can be %SIG_IGN if @sig is
4194 + * ignored, in which case force_sig() is about to reset it to %SIG_DFL.
4195 + * When this returns zero, this signal might cause a quick termination
4196 + * that does not give the debugger a chance to intercept the signal.
4198 + * Called with or without @task->sighand->siglock held.
4199 + */
4200 +static inline int tracehook_consider_fatal_signal(struct task_struct *task,
4201 + int sig)
4203 + if (unlikely(task_utrace_flags(task) & (UTRACE_EVENT(SIGNAL_TERM) |
4204 + UTRACE_EVENT(SIGNAL_CORE))))
4205 + return 1;
4206 + return task->ptrace != 0;
4209 #ifdef TIF_NOTIFY_RESUME
4211 * set_notify_resume - cause tracehook_notify_resume() to be called
4212 diff --git a/kernel/signal.c b/kernel/signal.c
4213 index b3f78d0..d7b90cd 100644
4214 --- a/kernel/signal.c
4215 +++ b/kernel/signal.c
4216 @@ -494,7 +494,7 @@ int unhandled_signal(struct task_struct *tsk, int sig)
4217 if (handler != SIG_IGN && handler != SIG_DFL)
4218 return 0;
4219 /* if ptraced, let the tracer determine */
4220 - return !tsk->ptrace;
4221 + return !tracehook_consider_fatal_signal(tsk, sig);
4225 @@ -982,7 +982,7 @@ static void complete_signal(int sig, struct task_struct *p, int group)
4226 if (sig_fatal(p, sig) &&
4227 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
4228 !sigismember(&t->real_blocked, sig) &&
4229 - (sig == SIGKILL || !t->ptrace)) {
4230 + (sig == SIGKILL || !tracehook_consider_fatal_signal(t, sig))) {
4232 * This signal will be fatal to the whole group.
4235 1.5.5.1
4238 From oleg@redhat.com Mon Nov 21 15:06:26 2011
4239 Return-Path: oleg@redhat.com
4240 Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
4241 zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
4242 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
4243 15:06:25 -0500 (EST)
4244 Received: from localhost (localhost.localdomain [127.0.0.1])
4245 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id E7A2DD498A;
4246 Mon, 21 Nov 2011 15:06:25 -0500 (EST)
4247 Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
4248 by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
4249 with ESMTP id 4L9zskFVsr4I; Mon, 21 Nov 2011 15:06:25 -0500 (EST)
4250 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22])
4251 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id D3487D4C39;
4252 Mon, 21 Nov 2011 15:06:25 -0500 (EST)
4253 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
4254 by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6Nnp017552;
4255 Mon, 21 Nov 2011 15:06:24 -0500
4256 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
4257 oleg@redhat.com; Mon, 21 Nov 2011 21:01:44 +0100 (CET)
4258 Date: Mon, 21 Nov 2011 21:01:42 +0100
4259 From: Oleg Nesterov <oleg@redhat.com>
4260 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
4261 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
4262 Kyle McMartin <kmcmartin@redhat.com>
4263 Cc: kernel@lists.fedoraproject.org
4264 Subject: [PATCH 05/33] add utrace hooks into sig_ignored() and
4265 recalc_sigpending()
4266 Message-ID: <20111121200142.GA27777@redhat.com>
4267 MIME-Version: 1.0
4268 Content-Type: text/plain; charset=us-ascii
4269 Content-Disposition: inline
4270 In-Reply-To: <20111121200039.GA27699@redhat.com>
4271 User-Agent: Mutt/1.5.18 (2008-05-17)
4272 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22
4273 Status: RO
4274 Content-Length: 1508
4275 Lines: 51
4277 Add the necessary and somewhat "special" hooks into sig_ignored() and
4278 recalc_sigpending(). Basically this restores _force_sigpending() and
4279 _consider_ignored_signal() tracehook logic.
4281 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
4283 include/linux/utrace.h | 2 ++
4284 kernel/signal.c | 7 ++++++-
4285 2 files changed, 8 insertions(+), 1 deletions(-)
4287 diff --git a/include/linux/utrace.h b/include/linux/utrace.h
4288 index f251efe..1b8da1c 100644
4289 --- a/include/linux/utrace.h
4290 +++ b/include/linux/utrace.h
4291 @@ -107,6 +107,8 @@ bool utrace_report_syscall_entry(struct pt_regs *);
4292 void utrace_report_syscall_exit(struct pt_regs *);
4293 void utrace_signal_handler(struct task_struct *, int);
4295 +#define UTRACE_FLAG(task, ev) (task_utrace_flags(task) & UTRACE_EVENT(ev))
4297 #ifndef CONFIG_UTRACE
4300 diff --git a/kernel/signal.c b/kernel/signal.c
4301 index d7b90cd..8594cb2 100644
4302 --- a/kernel/signal.c
4303 +++ b/kernel/signal.c
4304 @@ -87,7 +87,7 @@ static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
4306 * Tracers may want to know about even ignored signals.
4308 - return !t->ptrace;
4309 + return !t->ptrace && !UTRACE_FLAG(t, SIGNAL_IGN);
4313 @@ -150,6 +150,11 @@ void recalc_sigpending_and_wake(struct task_struct *t)
4315 void recalc_sigpending(void)
4317 + if (task_utrace_flags(current) && utrace_interrupt_pending()) {
4318 + set_thread_flag(TIF_SIGPENDING);
4319 + return;
4322 if (!recalc_sigpending_tsk(current) && !freezing(current))
4323 clear_thread_flag(TIF_SIGPENDING);
4326 1.5.5.1
4329 From oleg@redhat.com Mon Nov 21 15:06:28 2011
4330 Return-Path: oleg@redhat.com
4331 Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
4332 zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
4333 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
4334 15:06:28 -0500 (EST)
4335 Received: from localhost (localhost.localdomain [127.0.0.1])
4336 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 6E4A6D4F4A;
4337 Mon, 21 Nov 2011 15:06:28 -0500 (EST)
4338 Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
4339 by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
4340 with ESMTP id vEeGLyK1+rX9; Mon, 21 Nov 2011 15:06:28 -0500 (EST)
4341 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])
4342 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 5AD50D4C39;
4343 Mon, 21 Nov 2011 15:06:28 -0500 (EST)
4344 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
4345 by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK6QOB029543;
4346 Mon, 21 Nov 2011 15:06:26 -0500
4347 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
4348 oleg@redhat.com; Mon, 21 Nov 2011 21:01:47 +0100 (CET)
4349 Date: Mon, 21 Nov 2011 21:01:45 +0100
4350 From: Oleg Nesterov <oleg@redhat.com>
4351 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
4352 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
4353 Kyle McMartin <kmcmartin@redhat.com>
4354 Cc: kernel@lists.fedoraproject.org
4355 Subject: [PATCH 06/33] restore the EXEC/EXIT/CLONE utrace hooks
4356 Message-ID: <20111121200145.GA27780@redhat.com>
4357 MIME-Version: 1.0
4358 Content-Type: text/plain; charset=us-ascii
4359 Content-Disposition: inline
4360 In-Reply-To: <20111121200039.GA27699@redhat.com>
4361 User-Agent: Mutt/1.5.18 (2008-05-17)
4362 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12
4363 Status: RO
4364 Content-Length: 2383
4365 Lines: 83
4367 Restore the "trivial" EXEC/EXIT/CLONE utrace hooks. Add the
4368 simple helper, UTRACE_HOOK(), to minimize the changes.
4370 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
4372 fs/exec.c | 5 ++++-
4373 include/linux/utrace.h | 6 ++++++
4374 kernel/exit.c | 1 +
4375 kernel/fork.c | 4 ++++
4376 4 files changed, 15 insertions(+), 1 deletions(-)
4378 diff --git a/fs/exec.c b/fs/exec.c
4379 index 3625464..7d8f8bd 100644
4380 --- a/fs/exec.c
4381 +++ b/fs/exec.c
4382 @@ -1397,9 +1397,12 @@ int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
4384 bprm->recursion_depth = depth;
4385 if (retval >= 0) {
4386 - if (depth == 0)
4387 + if (depth == 0) {
4388 + UTRACE_HOOK(current, EXEC,
4389 + report_exec(fmt, bprm, regs));
4390 ptrace_event(PTRACE_EVENT_EXEC,
4391 old_pid);
4393 put_binfmt(fmt);
4394 allow_write_access(bprm->file);
4395 if (bprm->file)
4396 diff --git a/include/linux/utrace.h b/include/linux/utrace.h
4397 index 1b8da1c..9ac0b1b 100644
4398 --- a/include/linux/utrace.h
4399 +++ b/include/linux/utrace.h
4400 @@ -109,6 +109,12 @@ void utrace_signal_handler(struct task_struct *, int);
4402 #define UTRACE_FLAG(task, ev) (task_utrace_flags(task) & UTRACE_EVENT(ev))
4404 +#define UTRACE_HOOK(task, ev, callback) \
4405 + do { \
4406 + if (UTRACE_FLAG(task, ev)) \
4407 + utrace_ ## callback; \
4408 + } while (0)
4410 #ifndef CONFIG_UTRACE
4413 diff --git a/kernel/exit.c b/kernel/exit.c
4414 index d0b7d98..f5eba3b 100644
4415 --- a/kernel/exit.c
4416 +++ b/kernel/exit.c
4417 @@ -911,6 +911,7 @@ NORET_TYPE void do_exit(long code)
4419 set_fs(USER_DS);
4421 + UTRACE_HOOK(current, EXIT, report_exit(&code));
4422 ptrace_event(PTRACE_EVENT_EXIT, code);
4424 validate_creds_for_do_exit(tsk);
4425 diff --git a/kernel/fork.c b/kernel/fork.c
4426 index b8719c2..09d5049 100644
4427 --- a/kernel/fork.c
4428 +++ b/kernel/fork.c
4429 @@ -1535,6 +1535,8 @@ long do_fork(unsigned long clone_flags,
4431 audit_finish_fork(p);
4433 + UTRACE_HOOK(current, CLONE, report_clone(clone_flags, p));
4436 * We set PF_STARTING at creation in case tracing wants to
4437 * use this to distinguish a fully live task from one that
4438 @@ -1546,6 +1548,8 @@ long do_fork(unsigned long clone_flags,
4439 wake_up_new_task(p);
4441 /* forking complete and child started to run, tell ptracer */
4442 + if (clone_flags & CLONE_VFORK)
4443 + UTRACE_HOOK(current, CLONE, finish_vfork(current));
4444 if (unlikely(trace))
4445 ptrace_event(trace, nr);
4448 1.5.5.1
4451 From oleg@redhat.com Mon Nov 21 15:06:30 2011
4452 Return-Path: oleg@redhat.com
4453 Received: from zmta02.collab.prod.int.phx2.redhat.com (LHLO
4454 zmta02.collab.prod.int.phx2.redhat.com) (10.5.5.32) by
4455 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
4456 15:06:30 -0500 (EST)
4457 Received: from localhost (localhost.localdomain [127.0.0.1])
4458 by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id D248D1284E0;
4459 Mon, 21 Nov 2011 15:06:30 -0500 (EST)
4460 Received: from zmta02.collab.prod.int.phx2.redhat.com ([127.0.0.1])
4461 by localhost (zmta02.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
4462 with ESMTP id divvekTmLScS; Mon, 21 Nov 2011 15:06:30 -0500 (EST)
4463 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])
4464 by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id BDC9412800E;
4465 Mon, 21 Nov 2011 15:06:30 -0500 (EST)
4466 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
4467 by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK6Shj029546;
4468 Mon, 21 Nov 2011 15:06:29 -0500
4469 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
4470 oleg@redhat.com; Mon, 21 Nov 2011 21:01:49 +0100 (CET)
4471 Date: Mon, 21 Nov 2011 21:01:47 +0100
4472 From: Oleg Nesterov <oleg@redhat.com>
4473 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
4474 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
4475 Kyle McMartin <kmcmartin@redhat.com>
4476 Cc: kernel@lists.fedoraproject.org
4477 Subject: [PATCH 07/33] utrace: utrace_report_death() can use
4478 task_utrace_struct()
4479 Message-ID: <20111121200147.GA27787@redhat.com>
4480 MIME-Version: 1.0
4481 Content-Type: text/plain; charset=us-ascii
4482 Content-Disposition: inline
4483 In-Reply-To: <20111121200039.GA27699@redhat.com>
4484 User-Agent: Mutt/1.5.18 (2008-05-17)
4485 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12
4486 Status: RO
4487 Content-Length: 1692
4488 Lines: 44
4490 utrace_report_death() assumes that the caller (exit_notify) should
4491 pass task->utrace as an argument. This is no longer needed, it can
4492 safely do task_utrace_struct(). This way we avoid the nasty changes
4493 in exit_notify().
4495 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
4497 include/linux/utrace.h | 2 +-
4498 kernel/utrace.c | 5 +++--
4499 2 files changed, 4 insertions(+), 3 deletions(-)
4501 diff --git a/include/linux/utrace.h b/include/linux/utrace.h
4502 index 9ac0b1b..9a2e2f4 100644
4503 --- a/include/linux/utrace.h
4504 +++ b/include/linux/utrace.h
4505 @@ -99,7 +99,7 @@ int utrace_get_signal(struct task_struct *, struct pt_regs *,
4506 void utrace_report_clone(unsigned long, struct task_struct *);
4507 void utrace_finish_vfork(struct task_struct *);
4508 void utrace_report_exit(long *exit_code);
4509 -void utrace_report_death(struct task_struct *, struct utrace *, bool, int);
4510 +void utrace_report_death(struct task_struct *, bool, int);
4511 void utrace_report_jctl(int notify, int type);
4512 void utrace_report_exec(struct linux_binfmt *, struct linux_binprm *,
4513 struct pt_regs *regs);
4514 diff --git a/kernel/utrace.c b/kernel/utrace.c
4515 index ef856c9..1e750ad 100644
4516 --- a/kernel/utrace.c
4517 +++ b/kernel/utrace.c
4518 @@ -1759,9 +1759,10 @@ void utrace_report_exit(long *exit_code)
4519 * For this reason, utrace_release_task checks for the event bits that get
4520 * us here, and delays its cleanup for us to do.
4522 -void utrace_report_death(struct task_struct *task, struct utrace *utrace,
4523 - bool group_dead, int signal)
4524 +void utrace_report_death(struct task_struct *task, bool group_dead, int signal)
4526 + struct utrace *utrace = task_utrace_struct(task);
4528 INIT_REPORT(report);
4530 BUG_ON(!task->exit_state);
4532 1.5.5.1
4535 From oleg@redhat.com Mon Nov 21 15:06:33 2011
4536 Return-Path: oleg@redhat.com
4537 Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
4538 zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
4539 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
4540 15:06:33 -0500 (EST)
4541 Received: from localhost (localhost.localdomain [127.0.0.1])
4542 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 62E1BD50A3;
4543 Mon, 21 Nov 2011 15:06:33 -0500 (EST)
4544 Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
4545 by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
4546 with ESMTP id FB36YkOAHDc5; Mon, 21 Nov 2011 15:06:33 -0500 (EST)
4547 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25])
4548 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 4D161D4C39;
4549 Mon, 21 Nov 2011 15:06:33 -0500 (EST)
4550 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
4551 by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6VBW022130;
4552 Mon, 21 Nov 2011 15:06:31 -0500
4553 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
4554 oleg@redhat.com; Mon, 21 Nov 2011 21:01:52 +0100 (CET)
4555 Date: Mon, 21 Nov 2011 21:01:50 +0100
4556 From: Oleg Nesterov <oleg@redhat.com>
4557 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
4558 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
4559 Kyle McMartin <kmcmartin@redhat.com>
4560 Cc: kernel@lists.fedoraproject.org
4561 Subject: [PATCH 08/33] restore the DEATH/REAP utrace hooks
4562 Message-ID: <20111121200150.GA27790@redhat.com>
4563 MIME-Version: 1.0
4564 Content-Type: text/plain; charset=us-ascii
4565 Content-Disposition: inline
4566 In-Reply-To: <20111121200039.GA27699@redhat.com>
4567 User-Agent: Mutt/1.5.18 (2008-05-17)
4568 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25
4569 Status: RO
4570 Content-Length: 2346
4571 Lines: 70
4573 Restore the necessary hooks in release_task() and exit_notify(),
4574 add the corresponding helpers into utrace.h.
4576 Note: the @signal argument passed to ->report_death() does not
4577 match the previous behaviour. I think this shouldn't affect the
4578 current users, and I bet nobody can really understand what this
4579 magic argument should actually mean anyway.
4581 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
4583 include/linux/utrace.h | 22 ++++++++++++++++++++++
4584 kernel/exit.c | 4 ++++
4585 2 files changed, 26 insertions(+), 0 deletions(-)
4587 diff --git a/include/linux/utrace.h b/include/linux/utrace.h
4588 index 9a2e2f4..cf13839 100644
4589 --- a/include/linux/utrace.h
4590 +++ b/include/linux/utrace.h
4591 @@ -697,4 +697,26 @@ static inline __must_check int utrace_barrier_pid(struct pid *pid,
4593 #endif /* CONFIG_UTRACE */
4595 +static inline void utrace_release_task(struct task_struct *task)
4597 + /* see utrace_add_engine() about this barrier */
4598 + smp_mb();
4599 + if (task_utrace_flags(task))
4600 + utrace_maybe_reap(task, task_utrace_struct(task), true);
4603 +static inline void utrace_exit_notify(struct task_struct *task,
4604 + int signal, int group_dead)
4606 + /*
4607 + * If utrace_set_events() was just called to enable
4608 + * UTRACE_EVENT(DEATH), then we are obliged to call
4609 + * utrace_report_death() and not miss it. utrace_set_events()
4610 + * checks @task->exit_state under tasklist_lock to synchronize
4611 + * with exit_notify(), the caller.
4612 + */
4613 + if (task_utrace_flags(task) & _UTRACE_DEATH_EVENTS)
4614 + utrace_report_death(task, group_dead, signal);
4617 #endif /* linux/utrace.h */
4618 diff --git a/kernel/exit.c b/kernel/exit.c
4619 index f5eba3b..746c5df 100644
4620 --- a/kernel/exit.c
4621 +++ b/kernel/exit.c
4622 @@ -168,6 +168,8 @@ void release_task(struct task_struct * p)
4623 struct task_struct *leader;
4624 int zap_leader;
4625 repeat:
4626 + utrace_release_task(p);
4628 /* don't need to get the RCU readlock here - the process is dead and
4629 * can't be modifying its own credentials. But shut RCU-lockdep up */
4630 rcu_read_lock();
4631 @@ -858,6 +860,8 @@ static void exit_notify(struct task_struct *tsk, int group_dead)
4632 wake_up_process(tsk->signal->group_exit_task);
4633 write_unlock_irq(&tasklist_lock);
4635 + utrace_exit_notify(tsk, autoreap ? -1 : SIGCHLD, group_dead);
4637 /* If the process is dead, release it - nobody will wait for it */
4638 if (autoreap)
4639 release_task(tsk);
4641 1.5.5.1
4644 From oleg@redhat.com Mon Nov 21 15:06:35 2011
4645 Return-Path: oleg@redhat.com
4646 Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
4647 zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
4648 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
4649 15:06:35 -0500 (EST)
4650 Received: from localhost (localhost.localdomain [127.0.0.1])
4651 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id D4AB3D8761;
4652 Mon, 21 Nov 2011 15:06:35 -0500 (EST)
4653 Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
4654 by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
4655 with ESMTP id TiShkH1f6rnj; Mon, 21 Nov 2011 15:06:35 -0500 (EST)
4656 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23])
4657 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id BCB85D877F;
4658 Mon, 21 Nov 2011 15:06:35 -0500 (EST)
4659 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
4660 by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6XNu015312;
4661 Mon, 21 Nov 2011 15:06:34 -0500
4662 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
4663 oleg@redhat.com; Mon, 21 Nov 2011 21:01:54 +0100 (CET)
4664 Date: Mon, 21 Nov 2011 21:01:52 +0100
4665 From: Oleg Nesterov <oleg@redhat.com>
4666 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
4667 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
4668 Kyle McMartin <kmcmartin@redhat.com>
4669 Cc: kernel@lists.fedoraproject.org
4670 Subject: [PATCH 09/33] utrace: remove jobctl bits
4671 Message-ID: <20111121200152.GA27793@redhat.com>
4672 MIME-Version: 1.0
4673 Content-Type: text/plain; charset=us-ascii
4674 Content-Disposition: inline
4675 In-Reply-To: <20111121200039.GA27699@redhat.com>
4676 User-Agent: Mutt/1.5.18 (2008-05-17)
4677 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23
4678 Status: RO
4679 Content-Length: 1888
4680 Lines: 56
4682 - change utrace_get_signal() to check JOBCTL_STOP_PENDING instead of
4683 signal->group_stop_count. With the recent changes group_stop_count
4684 doesn't necessarily mean this task should participate in group stop.
4686 - remove the "participate in group stop" code from utrace_wakeup() and
4687 utrace_stop(), this is no longer needed and wrong.
4689 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
4691 kernel/utrace.c | 16 ++--------------
4692 1 files changed, 2 insertions(+), 14 deletions(-)
4694 diff --git a/kernel/utrace.c b/kernel/utrace.c
4695 index 1e750ad..5d3974e 100644
4696 --- a/kernel/utrace.c
4697 +++ b/kernel/utrace.c
4698 @@ -648,11 +648,7 @@ static void utrace_wakeup(struct task_struct *target, struct utrace *utrace)
4700 lockdep_assert_held(&utrace->lock);
4701 spin_lock_irq(&target->sighand->siglock);
4702 - if (target->signal->flags & SIGNAL_STOP_STOPPED ||
4703 - target->signal->group_stop_count)
4704 - target->state = TASK_STOPPED;
4705 - else
4706 - wake_up_state(target, __TASK_TRACED);
4707 + wake_up_state(target, __TASK_TRACED);
4708 spin_unlock_irq(&target->sighand->siglock);
4711 @@ -805,14 +801,6 @@ relock:
4713 __set_current_state(TASK_TRACED);
4715 - /*
4716 - * If there is a group stop in progress,
4717 - * we must participate in the bookkeeping.
4718 - */
4719 - if (unlikely(task->signal->group_stop_count) &&
4720 - !--task->signal->group_stop_count)
4721 - task->signal->flags = SIGNAL_STOP_STOPPED;
4723 spin_unlock_irq(&task->sighand->siglock);
4724 spin_unlock(&utrace->lock);
4726 @@ -2037,7 +2025,7 @@ int utrace_get_signal(struct task_struct *task, struct pt_regs *regs,
4727 ka = NULL;
4728 memset(return_ka, 0, sizeof *return_ka);
4729 } else if (!(task->utrace_flags & UTRACE_EVENT_SIGNAL_ALL) ||
4730 - unlikely(task->signal->group_stop_count)) {
4731 + unlikely(task->jobctl & JOBCTL_STOP_PENDING)) {
4733 * If no engine is interested in intercepting signals or
4734 * we must stop, let the caller just dequeue them normally
4736 1.5.5.1
4739 From oleg@redhat.com Mon Nov 21 15:06:38 2011
4740 Return-Path: oleg@redhat.com
4741 Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
4742 zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
4743 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
4744 15:06:38 -0500 (EST)
4745 Received: from localhost (localhost.localdomain [127.0.0.1])
4746 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 425CED8707;
4747 Mon, 21 Nov 2011 15:06:38 -0500 (EST)
4748 Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
4749 by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
4750 with ESMTP id 9paRC+dwSflh; Mon, 21 Nov 2011 15:06:38 -0500 (EST)
4751 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11])
4752 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 2DDBCD8410;
4753 Mon, 21 Nov 2011 15:06:38 -0500 (EST)
4754 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
4755 by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK6a31031355;
4756 Mon, 21 Nov 2011 15:06:36 -0500
4757 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
4758 oleg@redhat.com; Mon, 21 Nov 2011 21:01:56 +0100 (CET)
4759 Date: Mon, 21 Nov 2011 21:01:55 +0100
4760 From: Oleg Nesterov <oleg@redhat.com>
4761 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
4762 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
4763 Kyle McMartin <kmcmartin@redhat.com>
4764 Cc: kernel@lists.fedoraproject.org
4765 Subject: [PATCH 10/33] ptrace: take ->siglock around s/TRACED/RUNNING/
4766 Message-ID: <20111121200155.GA27801@redhat.com>
4767 MIME-Version: 1.0
4768 Content-Type: text/plain; charset=us-ascii
4769 Content-Disposition: inline
4770 In-Reply-To: <20111121200039.GA27699@redhat.com>
4771 User-Agent: Mutt/1.5.18 (2008-05-17)
4772 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11
4773 Status: RO
4774 Content-Length: 1624
4775 Lines: 56
4777 change ptrace_resume() and ptrace_stop() to take ->siglock around changing
4778 task->state from TRACED to RUNNING.
4780 With this patch __TASK_TRACED/STOPPED bits are fully protected by ->siglock,
4781 nobody can set or clear these bits without ->siglock held.
4783 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
4785 kernel/ptrace.c | 8 +++++++-
4786 kernel/signal.c | 3 +++
4787 2 files changed, 10 insertions(+), 1 deletions(-)
4789 diff --git a/kernel/ptrace.c b/kernel/ptrace.c
4790 index 24d0447..daf47bc 100644
4791 --- a/kernel/ptrace.c
4792 +++ b/kernel/ptrace.c
4793 @@ -589,6 +589,8 @@ static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
4794 static int ptrace_resume(struct task_struct *child, long request,
4795 unsigned long data)
4797 + unsigned long flags;
4799 if (!valid_signal(data))
4800 return -EIO;
4802 @@ -617,7 +619,11 @@ static int ptrace_resume(struct task_struct *child, long request,
4805 child->exit_code = data;
4806 - wake_up_state(child, __TASK_TRACED);
4808 + if (lock_task_sighand(child, &flags)) {
4809 + wake_up_state(child, __TASK_TRACED);
4810 + unlock_task_sighand(child, &flags);
4813 return 0;
4815 diff --git a/kernel/signal.c b/kernel/signal.c
4816 index 8594cb2..4512bb4 100644
4817 --- a/kernel/signal.c
4818 +++ b/kernel/signal.c
4819 @@ -1886,7 +1886,10 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
4820 if (gstop_done)
4821 do_notify_parent_cldstop(current, false, why);
4823 + spin_lock_irq(&current->sighand->siglock);
4824 __set_current_state(TASK_RUNNING);
4825 + spin_unlock_irq(&current->sighand->siglock);
4827 if (clear_code)
4828 current->exit_code = 0;
4829 read_unlock(&tasklist_lock);
4831 1.5.5.1
4834 From oleg@redhat.com Mon Nov 21 15:06:40 2011
4835 Return-Path: oleg@redhat.com
4836 Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
4837 zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
4838 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
4839 15:06:40 -0500 (EST)
4840 Received: from localhost (localhost.localdomain [127.0.0.1])
4841 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id B0D19D877F;
4842 Mon, 21 Nov 2011 15:06:40 -0500 (EST)
4843 Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
4844 by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
4845 with ESMTP id 3jv-cDBpMP5B; Mon, 21 Nov 2011 15:06:40 -0500 (EST)
4846 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])
4847 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 9C98DD8410;
4848 Mon, 21 Nov 2011 15:06:40 -0500 (EST)
4849 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
4850 by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK6cHF029573;
4851 Mon, 21 Nov 2011 15:06:39 -0500
4852 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
4853 oleg@redhat.com; Mon, 21 Nov 2011 21:01:59 +0100 (CET)
4854 Date: Mon, 21 Nov 2011 21:01:57 +0100
4855 From: Oleg Nesterov <oleg@redhat.com>
4856 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
4857 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
4858 Kyle McMartin <kmcmartin@redhat.com>
4859 Cc: kernel@lists.fedoraproject.org
4860 Subject: [PATCH 11/33] introduce wake_up_quiescent()
4861 Message-ID: <20111121200157.GA27805@redhat.com>
4862 MIME-Version: 1.0
4863 Content-Type: text/plain; charset=us-ascii
4864 Content-Disposition: inline
4865 In-Reply-To: <20111121200039.GA27699@redhat.com>
4866 User-Agent: Mutt/1.5.18 (2008-05-17)
4867 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12
4868 Status: RO
4869 Content-Length: 3186
4870 Lines: 93
4872 No functional changes. Add the new helper, wake_up_quiescent(task, state),
4873 which simply returns wake_up_state(task, state). Change all callers which
4874 do wake_up_state(STOPPED/TRACED) to use the new helper. ptrace_stop() is
4875 a bit special, it does __set_current_state(RUNNING) in the very unlikely
4876 case, change it as well.
4878 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
4880 include/linux/signal.h | 2 ++
4881 kernel/ptrace.c | 2 +-
4882 kernel/signal.c | 12 ++++++++++--
4883 kernel/utrace.c | 2 +-
4884 4 files changed, 14 insertions(+), 4 deletions(-)
4886 diff --git a/include/linux/signal.h b/include/linux/signal.h
4887 index a822300..2be3712 100644
4888 --- a/include/linux/signal.h
4889 +++ b/include/linux/signal.h
4890 @@ -239,6 +239,8 @@ static inline int valid_signal(unsigned long sig)
4891 struct timespec;
4892 struct pt_regs;
4894 +extern int wake_up_quiescent(struct task_struct *p, unsigned int state);
4896 extern int next_signal(struct sigpending *pending, sigset_t *mask);
4897 extern int do_send_sig_info(int sig, struct siginfo *info,
4898 struct task_struct *p, bool group);
4899 diff --git a/kernel/ptrace.c b/kernel/ptrace.c
4900 index daf47bc..8439ef1 100644
4901 --- a/kernel/ptrace.c
4902 +++ b/kernel/ptrace.c
4903 @@ -621,7 +621,7 @@ static int ptrace_resume(struct task_struct *child, long request,
4904 child->exit_code = data;
4906 if (lock_task_sighand(child, &flags)) {
4907 - wake_up_state(child, __TASK_TRACED);
4908 + wake_up_quiescent(child, __TASK_TRACED);
4909 unlock_task_sighand(child, &flags);
4912 diff --git a/kernel/signal.c b/kernel/signal.c
4913 index 4512bb4..99a6008 100644
4914 --- a/kernel/signal.c
4915 +++ b/kernel/signal.c
4916 @@ -702,6 +702,14 @@ void signal_wake_up(struct task_struct *t, int resume)
4920 + * wakes up the STOPPED/TRACED task, must be called with ->siglock held.
4921 + */
4922 +int wake_up_quiescent(struct task_struct *p, unsigned int state)
4924 + return wake_up_state(p, state);
4928 * Remove signals in mask from the pending set and queue.
4929 * Returns 1 if any signals were found.
4931 @@ -888,7 +896,7 @@ static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
4932 task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
4933 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
4934 if (likely(!(t->ptrace & PT_SEIZED)))
4935 - wake_up_state(t, __TASK_STOPPED);
4936 + wake_up_quiescent(t, __TASK_STOPPED);
4937 else
4938 ptrace_trap_notify(t);
4939 } while_each_thread(p, t);
4940 @@ -1887,7 +1895,7 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
4941 do_notify_parent_cldstop(current, false, why);
4943 spin_lock_irq(&current->sighand->siglock);
4944 - __set_current_state(TASK_RUNNING);
4945 + wake_up_quiescent(current, __TASK_TRACED);
4946 spin_unlock_irq(&current->sighand->siglock);
4948 if (clear_code)
4949 diff --git a/kernel/utrace.c b/kernel/utrace.c
4950 index 5d3974e..cebc390 100644
4951 --- a/kernel/utrace.c
4952 +++ b/kernel/utrace.c
4953 @@ -648,7 +648,7 @@ static void utrace_wakeup(struct task_struct *target, struct utrace *utrace)
4955 lockdep_assert_held(&utrace->lock);
4956 spin_lock_irq(&target->sighand->siglock);
4957 - wake_up_state(target, __TASK_TRACED);
4958 + wake_up_quiescent(target, __TASK_TRACED);
4959 spin_unlock_irq(&target->sighand->siglock);
4963 1.5.5.1
4966 From oleg@redhat.com Mon Nov 21 15:06:43 2011
4967 Return-Path: oleg@redhat.com
4968 Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
4969 zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
4970 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
4971 15:06:43 -0500 (EST)
4972 Received: from localhost (localhost.localdomain [127.0.0.1])
4973 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 6488BD514C;
4974 Mon, 21 Nov 2011 15:06:43 -0500 (EST)
4975 Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
4976 by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
4977 with ESMTP id TKQUj5mU4NYM; Mon, 21 Nov 2011 15:06:43 -0500 (EST)
4978 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23])
4979 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 4EC82D5000;
4980 Mon, 21 Nov 2011 15:06:43 -0500 (EST)
4981 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
4982 by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6fp6015343;
4983 Mon, 21 Nov 2011 15:06:41 -0500
4984 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
4985 oleg@redhat.com; Mon, 21 Nov 2011 21:02:02 +0100 (CET)
4986 Date: Mon, 21 Nov 2011 21:01:59 +0100
4987 From: Oleg Nesterov <oleg@redhat.com>
4988 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
4989 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
4990 Kyle McMartin <kmcmartin@redhat.com>
4991 Cc: kernel@lists.fedoraproject.org
4992 Subject: [PATCH 12/33] introduce ptrace_signal_wake_up()
4993 Message-ID: <20111121200159.GA27812@redhat.com>
4994 MIME-Version: 1.0
4995 Content-Type: text/plain; charset=us-ascii
4996 Content-Disposition: inline
4997 In-Reply-To: <20111121200039.GA27699@redhat.com>
4998 User-Agent: Mutt/1.5.18 (2008-05-17)
4999 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23
5000 Status: RO
5001 Content-Length: 3670
5002 Lines: 113
5004 Add the new helper, ptrace_signal_wake_up(), change ptrace.c/signal.c
5005 to use it instead of signal_wake_up() to wake up a STOPPED/TRACED task.
5007 The new helper does almost the same, except:
5009 - it doesn't use the TASK_WAKEKILL bit to wake up the TRACED
5010 or STOPPED task, it uses __TASK_STOPPED | __TASK_TRACED
5011 explicitly. This is what ptrace actually wants, it should
5012 never wake up a TASK_KILLABLE task.
5014 This should be cleanuped upatream, signal_wake_up() should
5015 take the state as an argument, not a boolean. Until then
5016 we add a new static helper.
5018 - it uses wake_up_quiescent() instead of wake_up_state().
5020 Thereafter every change from STOPPED/TRACED to RUNNING is done via
5021 wake_up_quiescent().
5023 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
5025 include/linux/ptrace.h | 1 +
5026 kernel/ptrace.c | 20 ++++++++++++++++----
5027 kernel/signal.c | 2 +-
5028 3 files changed, 18 insertions(+), 5 deletions(-)
5030 diff --git a/include/linux/ptrace.h b/include/linux/ptrace.h
5031 index 800f113..6d9282a 100644
5032 --- a/include/linux/ptrace.h
5033 +++ b/include/linux/ptrace.h
5034 @@ -113,6 +113,7 @@
5035 #include <linux/compiler.h> /* For unlikely. */
5036 #include <linux/sched.h> /* For struct task_struct. */
5038 +extern void ptrace_signal_wake_up(struct task_struct *p, int quiescent);
5040 extern long arch_ptrace(struct task_struct *child, long request,
5041 unsigned long addr, unsigned long data);
5042 diff --git a/kernel/ptrace.c b/kernel/ptrace.c
5043 index 8439ef1..a464ab5 100644
5044 --- a/kernel/ptrace.c
5045 +++ b/kernel/ptrace.c
5046 @@ -25,6 +25,18 @@
5047 #include <linux/hw_breakpoint.h>
5048 #include <linux/cn_proc.h>
5050 +void ptrace_signal_wake_up(struct task_struct *p, int quiescent)
5052 + unsigned int state;
5054 + set_tsk_thread_flag(p, TIF_SIGPENDING);
5056 + state = TASK_INTERRUPTIBLE;
5057 + if (quiescent)
5058 + state |= (__TASK_STOPPED | __TASK_TRACED);
5059 + if (!wake_up_quiescent(p, state))
5060 + kick_process(p);
5063 static int ptrace_trapping_sleep_fn(void *flags)
5065 @@ -106,7 +118,7 @@ void __ptrace_unlink(struct task_struct *child)
5066 * TASK_KILLABLE sleeps.
5068 if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child))
5069 - signal_wake_up(child, task_is_traced(child));
5070 + ptrace_signal_wake_up(child, task_is_traced(child));
5072 spin_unlock(&child->sighand->siglock);
5074 @@ -296,7 +308,7 @@ static int ptrace_attach(struct task_struct *task, long request,
5076 if (task_is_stopped(task) &&
5077 task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING))
5078 - signal_wake_up(task, 1);
5079 + ptrace_signal_wake_up(task, 1);
5081 spin_unlock(&task->sighand->siglock);
5083 @@ -731,7 +743,7 @@ int ptrace_request(struct task_struct *child, long request,
5084 * tracee into STOP.
5086 if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP)))
5087 - signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
5088 + ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
5090 unlock_task_sighand(child, &flags);
5091 ret = 0;
5092 @@ -757,7 +769,7 @@ int ptrace_request(struct task_struct *child, long request,
5093 * start of this trap and now. Trigger re-trap.
5095 if (child->jobctl & JOBCTL_TRAP_NOTIFY)
5096 - signal_wake_up(child, true);
5097 + ptrace_signal_wake_up(child, true);
5098 ret = 0;
5100 unlock_task_sighand(child, &flags);
5101 diff --git a/kernel/signal.c b/kernel/signal.c
5102 index 99a6008..7a47a93 100644
5103 --- a/kernel/signal.c
5104 +++ b/kernel/signal.c
5105 @@ -854,7 +854,7 @@ static void ptrace_trap_notify(struct task_struct *t)
5106 assert_spin_locked(&t->sighand->siglock);
5108 task_set_jobctl_pending(t, JOBCTL_TRAP_NOTIFY);
5109 - signal_wake_up(t, t->jobctl & JOBCTL_LISTENING);
5110 + ptrace_signal_wake_up(t, t->jobctl & JOBCTL_LISTENING);
5115 1.5.5.1
5118 From oleg@redhat.com Mon Nov 21 15:06:46 2011
5119 Return-Path: oleg@redhat.com
5120 Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
5121 zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
5122 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
5123 15:06:46 -0500 (EST)
5124 Received: from localhost (localhost.localdomain [127.0.0.1])
5125 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 3A7AED8410;
5126 Mon, 21 Nov 2011 15:06:46 -0500 (EST)
5127 Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
5128 by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
5129 with ESMTP id jhYAwDyYmdhg; Mon, 21 Nov 2011 15:06:46 -0500 (EST)
5130 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])
5131 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 25D5FD889A;
5132 Mon, 21 Nov 2011 15:06:46 -0500 (EST)
5133 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
5134 by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK6h29029619;
5135 Mon, 21 Nov 2011 15:06:44 -0500
5136 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
5137 oleg@redhat.com; Mon, 21 Nov 2011 21:02:04 +0100 (CET)
5138 Date: Mon, 21 Nov 2011 21:02:02 +0100
5139 From: Oleg Nesterov <oleg@redhat.com>
5140 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
5141 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
5142 Kyle McMartin <kmcmartin@redhat.com>
5143 Cc: kernel@lists.fedoraproject.org
5144 Subject: [PATCH 13/33] wait_task_inactive: treat task->state and
5145 match_state as bitmasks
5146 Message-ID: <20111121200202.GA27816@redhat.com>
5147 MIME-Version: 1.0
5148 Content-Type: text/plain; charset=us-ascii
5149 Content-Disposition: inline
5150 In-Reply-To: <20111121200039.GA27699@redhat.com>
5151 User-Agent: Mutt/1.5.18 (2008-05-17)
5152 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12
5153 Status: RO
5154 Content-Length: 1558
5155 Lines: 39
5157 Change wait_task_inactive() to check "state & match_state" instead of
5158 "state == match_state". This should not make any difference, but this
5159 allows us to add more "stopped" bits which can be set or cleared
5160 independently.
5162 IOW. wait_task_inactive() assumes that if task->state != 0, it can
5163 only be changed to TASK_RUNNING. Currently this is true, and in this
5164 case "state & match_state" continues to work. But, unlike the current
5165 check, it also works if task->state has other bits set while the caller
5166 is only interested in, say, __TASK_TRACED.
5168 Note: I think wait_task_inactive() should be cleanuped upstrean anyway,
5169 nowadays we have TASK_WAKING and task->state != 0 doesn't necessarily
5170 mean it is TASK_RUNNING. It also makes sense to exclude the !TASK_REPORT
5171 bits during the check. Finally, probably this patch makes sense anyway
5172 even without utrace. For example, a stopped _and_ traced thread could
5173 have task->state = TASK_STOPPED | TASK_TRACED, this can be useful.
5175 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
5177 kernel/sched.c | 2 +-
5178 1 files changed, 1 insertions(+), 1 deletions(-)
5180 diff --git a/kernel/sched.c b/kernel/sched.c
5181 index 0e9344a..84ecc0a 100644
5182 --- a/kernel/sched.c
5183 +++ b/kernel/sched.c
5184 @@ -2443,7 +2443,7 @@ unsigned long wait_task_inactive(struct task_struct *p, long match_state)
5185 * is actually now running somewhere else!
5187 while (task_running(rq, p)) {
5188 - if (match_state && unlikely(p->state != match_state))
5189 + if (match_state && !likely(p->state & match_state))
5190 return 0;
5191 cpu_relax();
5194 1.5.5.1
5197 From oleg@redhat.com Mon Nov 21 15:06:48 2011
5198 Return-Path: oleg@redhat.com
5199 Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
5200 zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
5201 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
5202 15:06:48 -0500 (EST)
5203 Received: from localhost (localhost.localdomain [127.0.0.1])
5204 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id C2934D5000;
5205 Mon, 21 Nov 2011 15:06:48 -0500 (EST)
5206 Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
5207 by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
5208 with ESMTP id ThT2PyhztDpQ; Mon, 21 Nov 2011 15:06:48 -0500 (EST)
5209 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25])
5210 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id ACE02D4F84;
5211 Mon, 21 Nov 2011 15:06:48 -0500 (EST)
5212 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
5213 by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6kdb022201;
5214 Mon, 21 Nov 2011 15:06:47 -0500
5215 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
5216 oleg@redhat.com; Mon, 21 Nov 2011 21:02:07 +0100 (CET)
5217 Date: Mon, 21 Nov 2011 21:02:05 +0100
5218 From: Oleg Nesterov <oleg@redhat.com>
5219 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
5220 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
5221 Kyle McMartin <kmcmartin@redhat.com>
5222 Cc: kernel@lists.fedoraproject.org
5223 Subject: [PATCH 14/33] introduce TASK_UTRACED state
5224 Message-ID: <20111121200205.GA27823@redhat.com>
5225 MIME-Version: 1.0
5226 Content-Type: text/plain; charset=us-ascii
5227 Content-Disposition: inline
5228 In-Reply-To: <20111121200039.GA27699@redhat.com>
5229 User-Agent: Mutt/1.5.18 (2008-05-17)
5230 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25
5231 Status: RO
5232 Content-Length: 2913
5233 Lines: 84
5235 Introduce TASK_UTRACED state, will be used by utrace instead of TASK_TRACED.
5237 Note: this state is reported as "t (tracing stop)" to the user-space to
5238 avoid the confusion. IOW, it looks like TASK_TRACED in /proc/pid/status.
5240 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
5242 fs/proc/array.c | 11 ++++++-----
5243 include/linux/sched.h | 20 +++++++++++---------
5244 2 files changed, 17 insertions(+), 14 deletions(-)
5246 diff --git a/fs/proc/array.c b/fs/proc/array.c
5247 index f0c0ea2..e0daec4 100644
5248 --- a/fs/proc/array.c
5249 +++ b/fs/proc/array.c
5250 @@ -138,11 +138,12 @@ static const char * const task_state_array[] = {
5251 "D (disk sleep)", /* 2 */
5252 "T (stopped)", /* 4 */
5253 "t (tracing stop)", /* 8 */
5254 - "Z (zombie)", /* 16 */
5255 - "X (dead)", /* 32 */
5256 - "x (dead)", /* 64 */
5257 - "K (wakekill)", /* 128 */
5258 - "W (waking)", /* 256 */
5259 + "t (tracing stop)", /* 16 (stopped by utrace) */
5260 + "Z (zombie)", /* 32 */
5261 + "X (dead)", /* 64 */
5262 + "x (dead)", /* 128 */
5263 + "K (wakekill)", /* 256 */
5264 + "W (waking)", /* 512 */
5267 static inline const char *get_task_state(struct task_struct *tsk)
5268 diff --git a/include/linux/sched.h b/include/linux/sched.h
5269 index 4d45f93..746b4d3 100644
5270 --- a/include/linux/sched.h
5271 +++ b/include/linux/sched.h
5272 @@ -185,16 +185,17 @@ print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
5273 #define TASK_UNINTERRUPTIBLE 2
5274 #define __TASK_STOPPED 4
5275 #define __TASK_TRACED 8
5276 +#define __TASK_UTRACED 16
5277 /* in tsk->exit_state */
5278 -#define EXIT_ZOMBIE 16
5279 -#define EXIT_DEAD 32
5280 +#define EXIT_ZOMBIE 32
5281 +#define EXIT_DEAD 64
5282 /* in tsk->state again */
5283 -#define TASK_DEAD 64
5284 -#define TASK_WAKEKILL 128
5285 -#define TASK_WAKING 256
5286 -#define TASK_STATE_MAX 512
5287 +#define TASK_DEAD 128
5288 +#define TASK_WAKEKILL 256
5289 +#define TASK_WAKING 512
5290 +#define TASK_STATE_MAX 1024
5292 -#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKW"
5293 +#define TASK_STATE_TO_CHAR_STR "RSDTtUZXxKW"
5295 extern char ___assert_task_state[1 - 2*!!(
5296 sizeof(TASK_STATE_TO_CHAR_STR)-1 != ilog2(TASK_STATE_MAX)+1)];
5297 @@ -203,15 +204,16 @@ extern char ___assert_task_state[1 - 2*!!(
5298 #define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
5299 #define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED)
5300 #define TASK_TRACED (TASK_WAKEKILL | __TASK_TRACED)
5301 +#define TASK_UTRACED (TASK_WAKEKILL | __TASK_UTRACED)
5303 /* Convenience macros for the sake of wake_up */
5304 #define TASK_NORMAL (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
5305 -#define TASK_ALL (TASK_NORMAL | __TASK_STOPPED | __TASK_TRACED)
5306 +#define TASK_ALL (TASK_NORMAL | __TASK_STOPPED | __TASK_TRACED | __TASK_UTRACED)
5308 /* get_task_state() */
5309 #define TASK_REPORT (TASK_RUNNING | TASK_INTERRUPTIBLE | \
5310 TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
5311 - __TASK_TRACED)
5312 + __TASK_TRACED | __TASK_UTRACED)
5314 #define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
5315 #define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
5317 1.5.5.1
5320 From oleg@redhat.com Mon Nov 21 15:06:51 2011
5321 Return-Path: oleg@redhat.com
5322 Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
5323 zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
5324 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
5325 15:06:51 -0500 (EST)
5326 Received: from localhost (localhost.localdomain [127.0.0.1])
5327 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 48F35D889C;
5328 Mon, 21 Nov 2011 15:06:51 -0500 (EST)
5329 Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
5330 by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
5331 with ESMTP id ZXkDvXaDg0cN; Mon, 21 Nov 2011 15:06:51 -0500 (EST)
5332 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25])
5333 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 34485D889A;
5334 Mon, 21 Nov 2011 15:06:51 -0500 (EST)
5335 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
5336 by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6nlT022211;
5337 Mon, 21 Nov 2011 15:06:49 -0500
5338 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
5339 oleg@redhat.com; Mon, 21 Nov 2011 21:02:09 +0100 (CET)
5340 Date: Mon, 21 Nov 2011 21:02:08 +0100
5341 From: Oleg Nesterov <oleg@redhat.com>
5342 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
5343 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
5344 Kyle McMartin <kmcmartin@redhat.com>
5345 Cc: kernel@lists.fedoraproject.org
5346 Subject: [PATCH 15/33] utrace: use TASK_UTRACED instead of TASK_TRACED
5347 Message-ID: <20111121200208.GA27826@redhat.com>
5348 MIME-Version: 1.0
5349 Content-Type: text/plain; charset=us-ascii
5350 Content-Disposition: inline
5351 In-Reply-To: <20111121200039.GA27699@redhat.com>
5352 User-Agent: Mutt/1.5.18 (2008-05-17)
5353 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25
5354 Status: RO
5355 Content-Length: 4316
5356 Lines: 130
5358 Change utrace.c to use TASK_UTRACED instead of TASK_TRACED.
5360 - utrace_stop/utrace_wakeup: simply use the new state
5362 - utrace_do_stop: do not clear STOPPED/TRACED, but add the new
5363 __TASK_UTRACED bit to state the fact that both ptrace and utrace
5364 want this task to be stopped
5366 - naturally, do not use task_is_traced() to check if this task was
5367 stopped by utrace, use the new task_is_utraced() helper which
5368 checks __TASK_UTRACED.
5370 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
5372 kernel/utrace.c | 26 ++++++++++++++------------
5373 1 files changed, 14 insertions(+), 12 deletions(-)
5375 diff --git a/kernel/utrace.c b/kernel/utrace.c
5376 index cebc390..2097103 100644
5377 --- a/kernel/utrace.c
5378 +++ b/kernel/utrace.c
5379 @@ -462,6 +462,8 @@ static void put_detached_list(struct list_head *list)
5381 #define ENGINE_STOP (1UL << _UTRACE_NEVENTS)
5383 +#define task_is_utraced(task) ((task->state & __TASK_UTRACED) != 0)
5385 static void mark_engine_wants_stop(struct task_struct *task,
5386 struct utrace_engine *engine)
5388 @@ -576,7 +578,7 @@ int utrace_set_events(struct task_struct *target,
5390 ret = 0;
5391 if ((old_flags & ~events) && target != current &&
5392 - !task_is_stopped_or_traced(target) && !target->exit_state) {
5393 + !task_is_utraced(target) && !target->exit_state) {
5395 * This barrier ensures that our engine->flags changes
5396 * have hit before we examine utrace->reporting,
5397 @@ -623,21 +625,21 @@ static void mark_engine_detached(struct utrace_engine *engine)
5399 static bool utrace_do_stop(struct task_struct *target, struct utrace *utrace)
5401 - if (task_is_stopped(target)) {
5402 + if (task_is_stopped_or_traced(target)) {
5404 * Stopped is considered quiescent; when it wakes up, it will
5405 * go through utrace_finish_stop() before doing anything else.
5407 spin_lock_irq(&target->sighand->siglock);
5408 - if (likely(task_is_stopped(target)))
5409 - __set_task_state(target, TASK_TRACED);
5410 + if (likely(task_is_stopped_or_traced(target)))
5411 + target->state |= TASK_UTRACED;
5412 spin_unlock_irq(&target->sighand->siglock);
5413 } else if (utrace->resume > UTRACE_REPORT) {
5414 utrace->resume = UTRACE_REPORT;
5415 set_notify_resume(target);
5418 - return task_is_traced(target);
5419 + return task_is_utraced(target);
5423 @@ -648,7 +650,7 @@ static void utrace_wakeup(struct task_struct *target, struct utrace *utrace)
5425 lockdep_assert_held(&utrace->lock);
5426 spin_lock_irq(&target->sighand->siglock);
5427 - wake_up_quiescent(target, __TASK_TRACED);
5428 + wake_up_quiescent(target, __TASK_UTRACED);
5429 spin_unlock_irq(&target->sighand->siglock);
5432 @@ -710,7 +712,7 @@ static bool utrace_reset(struct task_struct *task, struct utrace *utrace)
5434 * If no more engines want it stopped, wake it up.
5436 - if (task_is_traced(task) && !(flags & ENGINE_STOP)) {
5437 + if (task_is_utraced(task) && !(flags & ENGINE_STOP)) {
5439 * It just resumes, so make sure single-step
5440 * is not left set.
5441 @@ -749,7 +751,7 @@ void utrace_finish_stop(void)
5445 - * Perform %UTRACE_STOP, i.e. block in TASK_TRACED until woken up.
5446 + * Perform %UTRACE_STOP, i.e. block in TASK_UTRACED until woken up.
5447 * @task == current, @utrace == current->utrace, which is not locked.
5448 * Return true if we were woken up by SIGKILL even though some utrace
5449 * engine may still want us to stay stopped.
5450 @@ -799,7 +801,7 @@ relock:
5451 return;
5454 - __set_current_state(TASK_TRACED);
5455 + __set_current_state(TASK_UTRACED);
5457 spin_unlock_irq(&task->sighand->siglock);
5458 spin_unlock(&utrace->lock);
5459 @@ -809,14 +811,14 @@ relock:
5460 utrace_finish_stop();
5463 - * While in TASK_TRACED, we were considered "frozen enough".
5464 + * While in TASK_UTRACED, we were considered "frozen enough".
5465 * Now that we woke up, it's crucial if we're supposed to be
5466 * frozen that we freeze now before running anything substantial.
5468 try_to_freeze();
5471 - * While we were in TASK_TRACED, complete_signal() considered
5472 + * While we were in TASK_UTRACED, complete_signal() considered
5473 * us "uninterested" in signal wakeups. Now make sure our
5474 * TIF_SIGPENDING state is correct for normal running.
5476 @@ -1087,7 +1089,7 @@ int utrace_control(struct task_struct *target,
5477 if (unlikely(IS_ERR(utrace)))
5478 return PTR_ERR(utrace);
5480 - reset = task_is_traced(target);
5481 + reset = task_is_utraced(target);
5482 ret = 0;
5486 1.5.5.1
5489 From oleg@redhat.com Mon Nov 21 15:06:53 2011
5490 Return-Path: oleg@redhat.com
5491 Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
5492 zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
5493 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
5494 15:06:53 -0500 (EST)
5495 Received: from localhost (localhost.localdomain [127.0.0.1])
5496 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id B8F54D5296;
5497 Mon, 21 Nov 2011 15:06:53 -0500 (EST)
5498 Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
5499 by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
5500 with ESMTP id PX4Y3H1mw0hD; Mon, 21 Nov 2011 15:06:53 -0500 (EST)
5501 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])
5502 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id A4B5CD528E;
5503 Mon, 21 Nov 2011 15:06:53 -0500 (EST)
5504 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
5505 by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK6pXd029643;
5506 Mon, 21 Nov 2011 15:06:52 -0500
5507 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
5508 oleg@redhat.com; Mon, 21 Nov 2011 21:02:12 +0100 (CET)
5509 Date: Mon, 21 Nov 2011 21:02:10 +0100
5510 From: Oleg Nesterov <oleg@redhat.com>
5511 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
5512 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
5513 Kyle McMartin <kmcmartin@redhat.com>
5514 Cc: kernel@lists.fedoraproject.org
5515 Subject: [PATCH 16/33] reintroduce tracehook_finish_jctl() as
5516 utrace_end_stop()
5517 Message-ID: <20111121200210.GA27829@redhat.com>
5518 MIME-Version: 1.0
5519 Content-Type: text/plain; charset=us-ascii
5520 Content-Disposition: inline
5521 In-Reply-To: <20111121200039.GA27699@redhat.com>
5522 User-Agent: Mutt/1.5.18 (2008-05-17)
5523 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12
5524 Status: RO
5525 Content-Length: 2401
5526 Lines: 77
5528 utrace_finish_stop() is needed to avoid the races with SIGKILL which
5529 wakes up UTRACED task, and thus it should be called every time after
5530 the STOPPED/TRACED/UTRACED returns from schedule(), remember that
5531 TASK_UTRACED can be added while the task is STOPPED/UTRACED.
5533 - change do_signal_state() to call this helper right after schedule(),
5534 otherwise this logic is broken by the upstream changes
5536 - now that utrace doesn't control TASK_TRACED bit, ptrace_stop() must
5537 call this helper too.
5539 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
5541 include/linux/utrace.h | 11 +++++++++++
5542 kernel/signal.c | 5 +++++
5543 kernel/utrace.c | 2 +-
5544 3 files changed, 17 insertions(+), 1 deletions(-)
5546 diff --git a/include/linux/utrace.h b/include/linux/utrace.h
5547 index cf13839..0279c74 100644
5548 --- a/include/linux/utrace.h
5549 +++ b/include/linux/utrace.h
5550 @@ -719,4 +719,15 @@ static inline void utrace_exit_notify(struct task_struct *task,
5551 utrace_report_death(task, group_dead, signal);
5554 +/**
5555 + * utrace_end_stop - report about return from STOPPED/TRACED
5557 + * This is called by do_signal_stop() and ptrace_stop after wakeup.
5558 + */
5559 +static inline void utrace_end_stop(void)
5561 + if (task_utrace_flags(current))
5562 + utrace_finish_stop();
5565 #endif /* linux/utrace.h */
5566 diff --git a/kernel/signal.c b/kernel/signal.c
5567 index 7a47a93..ba46eab 100644
5568 --- a/kernel/signal.c
5569 +++ b/kernel/signal.c
5570 @@ -1903,6 +1903,8 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
5571 read_unlock(&tasklist_lock);
5574 + utrace_end_stop();
5577 * While in TASK_TRACED, we were considered "frozen enough".
5578 * Now that we woke up, it's crucial if we're supposed to be
5579 @@ -2067,6 +2069,9 @@ static bool do_signal_stop(int signr)
5581 /* Now we don't run again until woken by SIGCONT or SIGKILL */
5582 schedule();
5584 + utrace_end_stop();
5586 return true;
5587 } else {
5589 diff --git a/kernel/utrace.c b/kernel/utrace.c
5590 index 2097103..d41b982 100644
5591 --- a/kernel/utrace.c
5592 +++ b/kernel/utrace.c
5593 @@ -741,7 +741,7 @@ static bool utrace_reset(struct task_struct *task, struct utrace *utrace)
5594 void utrace_finish_stop(void)
5597 - * If we were task_is_traced() and then SIGKILL'ed, make
5598 + * If we were task_is_utraced() and then SIGKILL'ed, make
5599 * sure we do nothing until the tracer drops utrace->lock.
5601 if (unlikely(__fatal_signal_pending(current))) {
5603 1.5.5.1
5606 From oleg@redhat.com Mon Nov 21 15:06:56 2011
5607 Return-Path: oleg@redhat.com
5608 Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
5609 zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
5610 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
5611 15:06:56 -0500 (EST)
5612 Received: from localhost (localhost.localdomain [127.0.0.1])
5613 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 4A487D83A1;
5614 Mon, 21 Nov 2011 15:06:56 -0500 (EST)
5615 Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
5616 by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
5617 with ESMTP id 3vHogPyhojlJ; Mon, 21 Nov 2011 15:06:56 -0500 (EST)
5618 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22])
5619 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 358A0D88B0;
5620 Mon, 21 Nov 2011 15:06:56 -0500 (EST)
5621 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
5622 by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6sHk017679;
5623 Mon, 21 Nov 2011 15:06:54 -0500
5624 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
5625 oleg@redhat.com; Mon, 21 Nov 2011 21:02:14 +0100 (CET)
5626 Date: Mon, 21 Nov 2011 21:02:12 +0100
5627 From: Oleg Nesterov <oleg@redhat.com>
5628 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
5629 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
5630 Kyle McMartin <kmcmartin@redhat.com>
5631 Cc: kernel@lists.fedoraproject.org
5632 Subject: [PATCH 17/33] teach wake_up_quiescent() to do "selective" wake_up
5633 Message-ID: <20111121200212.GA27836@redhat.com>
5634 MIME-Version: 1.0
5635 Content-Type: text/plain; charset=us-ascii
5636 Content-Disposition: inline
5637 In-Reply-To: <20111121200039.GA27699@redhat.com>
5638 User-Agent: Mutt/1.5.18 (2008-05-17)
5639 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22
5640 Status: RO
5641 Content-Length: 1335
5642 Lines: 46
5644 Both utrace and ptrace can want the same thread to be quiescent, in this
5645 case its state is TASK_TRACED | TASK_UTRACED. And this also means that
5646 this task must not run unless both utrace and ptrace resume it.
5648 Change wake_up_quiescent(p, state) to do "p->state &= ~state" and return
5649 false unless there is no more "quiescent" bits in task->state.
5651 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
5653 kernel/signal.c | 15 +++++++++++++++
5654 1 files changed, 15 insertions(+), 0 deletions(-)
5656 diff --git a/kernel/signal.c b/kernel/signal.c
5657 index ba46eab..e06f795 100644
5658 --- a/kernel/signal.c
5659 +++ b/kernel/signal.c
5660 @@ -701,11 +701,26 @@ void signal_wake_up(struct task_struct *t, int resume)
5661 kick_process(t);
5664 +#define STATE_QUIESCENT (__TASK_STOPPED | __TASK_TRACED | __TASK_UTRACED)
5666 * wakes up the STOPPED/TRACED task, must be called with ->siglock held.
5668 int wake_up_quiescent(struct task_struct *p, unsigned int state)
5670 + unsigned int quiescent = (p->state & STATE_QUIESCENT);
5672 + WARN_ON(state & ~(STATE_QUIESCENT | TASK_INTERRUPTIBLE));
5674 + if (quiescent) {
5675 + state &= ~TASK_INTERRUPTIBLE;
5676 + if ((quiescent & ~state) != 0) {
5677 + p->state &= ~state;
5678 + WARN_ON(!(p->state & STATE_QUIESCENT));
5679 + WARN_ON(!(p->state & TASK_WAKEKILL));
5680 + return 0;
5684 return wake_up_state(p, state);
5688 1.5.5.1
5691 From oleg@redhat.com Mon Nov 21 15:06:59 2011
5692 Return-Path: oleg@redhat.com
5693 Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
5694 zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
5695 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
5696 15:06:58 -0500 (EST)
5697 Received: from localhost (localhost.localdomain [127.0.0.1])
5698 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id C88E2D528B;
5699 Mon, 21 Nov 2011 15:06:58 -0500 (EST)
5700 Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
5701 by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
5702 with ESMTP id nNrAxBD5KOME; Mon, 21 Nov 2011 15:06:58 -0500 (EST)
5703 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11])
5704 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id B2114D51C1;
5705 Mon, 21 Nov 2011 15:06:58 -0500 (EST)
5706 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
5707 by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK6uQQ031440;
5708 Mon, 21 Nov 2011 15:06:57 -0500
5709 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
5710 oleg@redhat.com; Mon, 21 Nov 2011 21:02:17 +0100 (CET)
5711 Date: Mon, 21 Nov 2011 21:02:15 +0100
5712 From: Oleg Nesterov <oleg@redhat.com>
5713 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
5714 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
5715 Kyle McMartin <kmcmartin@redhat.com>
5716 Cc: kernel@lists.fedoraproject.org
5717 Subject: [PATCH 18/33] ptrace_stop: do not assume the task is running after
5718 wake_up_quiescent()
5719 Message-ID: <20111121200215.GA27839@redhat.com>
5720 MIME-Version: 1.0
5721 Content-Type: text/plain; charset=us-ascii
5722 Content-Disposition: inline
5723 In-Reply-To: <20111121200039.GA27699@redhat.com>
5724 User-Agent: Mutt/1.5.18 (2008-05-17)
5725 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11
5726 Status: RO
5727 Content-Length: 956
5728 Lines: 31
5730 If ptrace_stop() sets TASK_TRACED and then detects we should not stop,
5731 it can race with utrace_do_stop() which can see TASK_TRACED and add
5732 TASK_UTRACED. In this case we should stop for utrace needs.
5734 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
5736 kernel/signal.c | 8 ++++++++
5737 1 files changed, 8 insertions(+), 0 deletions(-)
5739 diff --git a/kernel/signal.c b/kernel/signal.c
5740 index e06f795..9348da6 100644
5741 --- a/kernel/signal.c
5742 +++ b/kernel/signal.c
5743 @@ -1916,6 +1916,14 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
5744 if (clear_code)
5745 current->exit_code = 0;
5746 read_unlock(&tasklist_lock);
5748 + /*
5749 + * It is possible that __TASK_UTRACED was added by utrace
5750 + * while we were __TASK_TRACED and before we take ->siglock
5751 + * for wake_up_quiescent(), we need to block in this case.
5752 + * Otherwise this is unnecessary but absolutely harmless.
5753 + */
5754 + schedule();
5757 utrace_end_stop();
5759 1.5.5.1
5762 From oleg@redhat.com Mon Nov 21 15:07:01 2011
5763 Return-Path: oleg@redhat.com
5764 Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
5765 zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
5766 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
5767 15:07:01 -0500 (EST)
5768 Received: from localhost (localhost.localdomain [127.0.0.1])
5769 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 4BF37D889A;
5770 Mon, 21 Nov 2011 15:07:01 -0500 (EST)
5771 Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
5772 by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
5773 with ESMTP id 5p8tvlXAqUHr; Mon, 21 Nov 2011 15:07:01 -0500 (EST)
5774 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11])
5775 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 37C22D8757;
5776 Mon, 21 Nov 2011 15:07:01 -0500 (EST)
5777 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
5778 by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK6xFJ031449;
5779 Mon, 21 Nov 2011 15:06:59 -0500
5780 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
5781 oleg@redhat.com; Mon, 21 Nov 2011 21:02:19 +0100 (CET)
5782 Date: Mon, 21 Nov 2011 21:02:18 +0100
5783 From: Oleg Nesterov <oleg@redhat.com>
5784 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
5785 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
5786 Kyle McMartin <kmcmartin@redhat.com>
5787 Cc: kernel@lists.fedoraproject.org
5788 Subject: [PATCH 19/33] get_signal_to_deliver: restore/restructure
5789 utrace/ptrace signal reporting
5790 Message-ID: <20111121200218.GA27846@redhat.com>
5791 MIME-Version: 1.0
5792 Content-Type: text/plain; charset=us-ascii
5793 Content-Disposition: inline
5794 In-Reply-To: <20111121200039.GA27699@redhat.com>
5795 User-Agent: Mutt/1.5.18 (2008-05-17)
5796 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11
5797 Status: RO
5798 Content-Length: 3556
5799 Lines: 115
5801 - Reintroduce tracehook_get_signal() as utrace_hook_signal().
5803 - Change get_signal_to_deliver() to call utrace_hook_signal() first,
5804 before dequeue_signal()
5806 - Always call ptrace_signal() if signal != SIGKILL, no matter whether
5807 this signal comes from utrace or not.
5809 Since this can change signr again, update "struct k_sigaction *ka"
5810 in this case.
5812 IOW, roughly, ptrace acts as if it is the last attached engine, it
5813 takes the final decision about the signal.
5815 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
5817 include/linux/utrace.h | 31 +++++++++++++++++++++++++++++++
5818 kernel/signal.c | 30 ++++++++++++++++++++----------
5819 2 files changed, 51 insertions(+), 10 deletions(-)
5821 diff --git a/include/linux/utrace.h b/include/linux/utrace.h
5822 index 0279c74..63103e2 100644
5823 --- a/include/linux/utrace.h
5824 +++ b/include/linux/utrace.h
5825 @@ -730,4 +730,35 @@ static inline void utrace_end_stop(void)
5826 utrace_finish_stop();
5829 +/**
5830 + * utrace_hook_signal - deliver synthetic signal to traced task
5831 + * @task: @current
5832 + * @regs: task_pt_regs(@current)
5833 + * @info: details of synthetic signal
5834 + * @return_ka: sigaction for synthetic signal
5836 + * Return zero to check for a real pending signal normally.
5837 + * Return -1 after releasing the siglock to repeat the check.
5838 + * Return a signal number to induce an artificial signal delivery,
5839 + * setting *@info and *@return_ka to specify its details and behavior.
5841 + * The @return_ka->sa_handler value controls the disposition of the
5842 + * signal, no matter the signal number. For %SIG_DFL, the return value
5843 + * is a representative signal to indicate the behavior (e.g. %SIGTERM
5844 + * for death, %SIGQUIT for core dump, %SIGSTOP for job control stop,
5845 + * %SIGTSTP for stop unless in an orphaned pgrp), but the signal number
5846 + * reported will be @info->si_signo instead.
5848 + * Called with @task->sighand->siglock held, before dequeuing pending signals.
5849 + */
5850 +static inline int utrace_hook_signal(struct task_struct *task,
5851 + struct pt_regs *regs,
5852 + siginfo_t *info,
5853 + struct k_sigaction *return_ka)
5855 + if (unlikely(task_utrace_flags(task)))
5856 + return utrace_get_signal(task, regs, info, return_ka);
5857 + return 0;
5860 #endif /* linux/utrace.h */
5861 diff --git a/kernel/signal.c b/kernel/signal.c
5862 index 9348da6..38ea4e6 100644
5863 --- a/kernel/signal.c
5864 +++ b/kernel/signal.c
5865 @@ -2242,17 +2242,27 @@ relock:
5866 for (;;) {
5867 struct k_sigaction *ka;
5869 - if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
5870 - do_signal_stop(0))
5871 + signr = utrace_hook_signal(current, regs, info, return_ka);
5872 + if (unlikely(signr < 0))
5873 goto relock;
5875 - if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
5876 - do_jobctl_trap();
5877 - spin_unlock_irq(&sighand->siglock);
5878 - goto relock;
5880 + if (unlikely(signr != 0))
5881 + ka = return_ka;
5882 + else {
5883 + if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
5884 + do_signal_stop(0))
5885 + goto relock;
5887 - signr = dequeue_signal(current, &current->blocked, info);
5888 + if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
5889 + do_jobctl_trap();
5890 + spin_unlock_irq(&sighand->siglock);
5891 + goto relock;
5894 + signr = dequeue_signal(current, &current->blocked, info);
5896 + ka = &sighand->action[signr-1];
5899 if (!signr)
5900 break; /* will return 0 */
5901 @@ -2262,9 +2272,9 @@ relock:
5902 regs, cookie);
5903 if (!signr)
5904 continue;
5907 - ka = &sighand->action[signr-1];
5908 + ka = &sighand->action[signr-1];
5911 /* Trace actually delivered signals. */
5912 trace_signal_deliver(signr, info, ka);
5914 1.5.5.1
5917 From oleg@redhat.com Mon Nov 21 15:07:03 2011
5918 Return-Path: oleg@redhat.com
5919 Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
5920 zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
5921 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
5922 15:07:03 -0500 (EST)
5923 Received: from localhost (localhost.localdomain [127.0.0.1])
5924 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id CB362D89E9;
5925 Mon, 21 Nov 2011 15:07:03 -0500 (EST)
5926 Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
5927 by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
5928 with ESMTP id wm2JqccPnhsV; Mon, 21 Nov 2011 15:07:03 -0500 (EST)
5929 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11])
5930 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id B6D25D89DE;
5931 Mon, 21 Nov 2011 15:07:03 -0500 (EST)
5932 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
5933 by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK715f031455;
5934 Mon, 21 Nov 2011 15:07:02 -0500
5935 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
5936 oleg@redhat.com; Mon, 21 Nov 2011 21:02:22 +0100 (CET)
5937 Date: Mon, 21 Nov 2011 21:02:20 +0100
5938 From: Oleg Nesterov <oleg@redhat.com>
5939 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
5940 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
5941 Kyle McMartin <kmcmartin@redhat.com>
5942 Cc: kernel@lists.fedoraproject.org
5943 Subject: [PATCH 20/33] utrace_get_signal:
5944 s/JOBCTL_STOP_PENDING/JOBCTL_PENDING_MASK/
5945 Message-ID: <20111121200220.GA27849@redhat.com>
5946 MIME-Version: 1.0
5947 Content-Type: text/plain; charset=us-ascii
5948 Content-Disposition: inline
5949 In-Reply-To: <20111121200039.GA27699@redhat.com>
5950 User-Agent: Mutt/1.5.18 (2008-05-17)
5951 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11
5952 Status: RO
5953 Content-Length: 1204
5954 Lines: 33
5956 utrace_get_signal() checks JOBCTL_STOP_PENDING to detect the
5957 case when we should not try to dequeue the signal but should
5958 try to participate in the group-stop.
5960 With the recent changes this is not enough, everything which
5961 contrbutes to recalc_sigpending_tsk() should be respected.
5963 Check JOBCTL_PENDING_MASK instead. This matches the
5964 JOBCTL_STOP_PENDING | JOBCTL_TRAP_MASK code in the caller,
5965 get_signal_to_deliver(). Note that this code won't run if
5966 utrace_get_signal() returns signr > 0.
5968 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
5970 kernel/utrace.c | 2 +-
5971 1 files changed, 1 insertions(+), 1 deletions(-)
5973 diff --git a/kernel/utrace.c b/kernel/utrace.c
5974 index d41b982..0bb0a06 100644
5975 --- a/kernel/utrace.c
5976 +++ b/kernel/utrace.c
5977 @@ -2027,7 +2027,7 @@ int utrace_get_signal(struct task_struct *task, struct pt_regs *regs,
5978 ka = NULL;
5979 memset(return_ka, 0, sizeof *return_ka);
5980 } else if (!(task->utrace_flags & UTRACE_EVENT_SIGNAL_ALL) ||
5981 - unlikely(task->jobctl & JOBCTL_STOP_PENDING)) {
5982 + unlikely(task->jobctl & JOBCTL_PENDING_MASK)) {
5984 * If no engine is interested in intercepting signals or
5985 * we must stop, let the caller just dequeue them normally
5987 1.5.5.1
5990 From oleg@redhat.com Mon Nov 21 15:07:07 2011
5991 Return-Path: oleg@redhat.com
5992 Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
5993 zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
5994 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
5995 15:07:06 -0500 (EST)
5996 Received: from localhost (localhost.localdomain [127.0.0.1])
5997 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 8C14BD4F84;
5998 Mon, 21 Nov 2011 15:07:06 -0500 (EST)
5999 Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
6000 by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
6001 with ESMTP id 39eM-KKZxrZ3; Mon, 21 Nov 2011 15:07:06 -0500 (EST)
6002 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25])
6003 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 7628FD4FF4;
6004 Mon, 21 Nov 2011 15:07:06 -0500 (EST)
6005 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
6006 by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK74dN022253;
6007 Mon, 21 Nov 2011 15:07:04 -0500
6008 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
6009 oleg@redhat.com; Mon, 21 Nov 2011 21:02:25 +0100 (CET)
6010 Date: Mon, 21 Nov 2011 21:02:23 +0100
6011 From: Oleg Nesterov <oleg@redhat.com>
6012 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
6013 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
6014 Kyle McMartin <kmcmartin@redhat.com>
6015 Cc: kernel@lists.fedoraproject.org
6016 Subject: [PATCH 21/33] introduce ptrace_set_syscall_trace()
6017 Message-ID: <20111121200223.GA27856@redhat.com>
6018 MIME-Version: 1.0
6019 Content-Type: text/plain; charset=us-ascii
6020 Content-Disposition: inline
6021 In-Reply-To: <20111121200039.GA27699@redhat.com>
6022 User-Agent: Mutt/1.5.18 (2008-05-17)
6023 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25
6024 Status: RO
6025 Content-Length: 1549
6026 Lines: 52
6028 No functional changes. Add the new helper, ptrace_set_syscall_trace(),
6029 which should be used to set/clear TIF_SYSCALL_TRACE in ptrace code.
6030 Currently it does nothing more.
6032 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
6034 kernel/ptrace.c | 15 ++++++++++-----
6035 1 files changed, 10 insertions(+), 5 deletions(-)
6037 diff --git a/kernel/ptrace.c b/kernel/ptrace.c
6038 index a464ab5..43357e5 100644
6039 --- a/kernel/ptrace.c
6040 +++ b/kernel/ptrace.c
6041 @@ -38,6 +38,14 @@ void ptrace_signal_wake_up(struct task_struct *p, int quiescent)
6042 kick_process(p);
6045 +static void ptrace_set_syscall_trace(struct task_struct *p, bool on)
6047 + if (on)
6048 + set_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
6049 + else
6050 + clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
6053 static int ptrace_trapping_sleep_fn(void *flags)
6055 schedule();
6056 @@ -418,7 +426,7 @@ static int ptrace_detach(struct task_struct *child, unsigned int data)
6058 /* Architecture-specific hardware disable .. */
6059 ptrace_disable(child);
6060 - clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
6061 + ptrace_set_syscall_trace(child, false);
6063 write_lock_irq(&tasklist_lock);
6065 @@ -606,10 +614,7 @@ static int ptrace_resume(struct task_struct *child, long request,
6066 if (!valid_signal(data))
6067 return -EIO;
6069 - if (request == PTRACE_SYSCALL)
6070 - set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
6071 - else
6072 - clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
6073 + ptrace_set_syscall_trace(child, request == PTRACE_SYSCALL);
6075 #ifdef TIF_SYSCALL_EMU
6076 if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
6078 1.5.5.1
6081 From oleg@redhat.com Mon Nov 21 15:07:09 2011
6082 Return-Path: oleg@redhat.com
6083 Received: from zmta02.collab.prod.int.phx2.redhat.com (LHLO
6084 zmta02.collab.prod.int.phx2.redhat.com) (10.5.5.32) by
6085 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
6086 15:07:09 -0500 (EST)
6087 Received: from localhost (localhost.localdomain [127.0.0.1])
6088 by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 1ADF01285E4;
6089 Mon, 21 Nov 2011 15:07:09 -0500 (EST)
6090 Received: from zmta02.collab.prod.int.phx2.redhat.com ([127.0.0.1])
6091 by localhost (zmta02.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
6092 with ESMTP id rF+XegsxVXnL; Mon, 21 Nov 2011 15:07:09 -0500 (EST)
6093 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25])
6094 by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 067E41287D0;
6095 Mon, 21 Nov 2011 15:07:09 -0500 (EST)
6096 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
6097 by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK76tD022270;
6098 Mon, 21 Nov 2011 15:07:07 -0500
6099 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
6100 oleg@redhat.com; Mon, 21 Nov 2011 21:02:27 +0100 (CET)
6101 Date: Mon, 21 Nov 2011 21:02:25 +0100
6102 From: Oleg Nesterov <oleg@redhat.com>
6103 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
6104 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
6105 Kyle McMartin <kmcmartin@redhat.com>
6106 Cc: kernel@lists.fedoraproject.org
6107 Subject: [PATCH 22/33] introduce PT_SYSCALL_TRACE flag
6108 Message-ID: <20111121200225.GA27860@redhat.com>
6109 MIME-Version: 1.0
6110 Content-Type: text/plain; charset=us-ascii
6111 Content-Disposition: inline
6112 In-Reply-To: <20111121200039.GA27699@redhat.com>
6113 User-Agent: Mutt/1.5.18 (2008-05-17)
6114 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25
6115 Status: RO
6116 Content-Length: 2268
6117 Lines: 73
6119 Currently tracehooks assume that if the ptraced task has
6120 TIF_SYSCALL_TRACE set, the tracee should report the syscall.
6121 This is not true, this thread flag can be set by utrace.
6123 Add the new internal ptrace flag, PT_SYSCALL_TRACE. Change
6124 ptrace_set_syscall_trace() to set/clear this bit along with
6125 TIF_SYSCALL_TRACE, change ptrace_report_syscall() to check
6126 this flag instead of PT_PTRACED.
6128 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
6130 include/linux/ptrace.h | 3 +++
6131 include/linux/tracehook.h | 2 +-
6132 kernel/ptrace.c | 7 +++++--
6133 3 files changed, 9 insertions(+), 3 deletions(-)
6135 diff --git a/include/linux/ptrace.h b/include/linux/ptrace.h
6136 index 6d9282a..c10f610 100644
6137 --- a/include/linux/ptrace.h
6138 +++ b/include/linux/ptrace.h
6139 @@ -104,6 +104,8 @@
6141 #define PT_TRACE_MASK 0x000003f4
6143 +#define PT_SYSCALL_TRACE 0x00020000
6145 /* single stepping state bits (used on ARM and PA-RISC) */
6146 #define PT_SINGLESTEP_BIT 31
6147 #define PT_SINGLESTEP (1<<PT_SINGLESTEP_BIT)
6148 @@ -227,6 +229,7 @@ static inline void ptrace_init_task(struct task_struct *child, bool ptrace)
6150 if (unlikely(ptrace) && current->ptrace) {
6151 child->ptrace = current->ptrace;
6152 + child->ptrace &= ~PT_SYSCALL_TRACE;
6153 __ptrace_link(child, current->parent);
6155 if (child->ptrace & PT_SEIZED)
6156 diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
6157 index ec2af67..eb9fe30 100644
6158 --- a/include/linux/tracehook.h
6159 +++ b/include/linux/tracehook.h
6160 @@ -59,7 +59,7 @@ static inline void ptrace_report_syscall(struct pt_regs *regs)
6162 int ptrace = current->ptrace;
6164 - if (!(ptrace & PT_PTRACED))
6165 + if (!(ptrace & PT_SYSCALL_TRACE))
6166 return;
6168 ptrace_notify(SIGTRAP | ((ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
6169 diff --git a/kernel/ptrace.c b/kernel/ptrace.c
6170 index 43357e5..1ac03eb 100644
6171 --- a/kernel/ptrace.c
6172 +++ b/kernel/ptrace.c
6173 @@ -40,10 +40,13 @@ void ptrace_signal_wake_up(struct task_struct *p, int quiescent)
6175 static void ptrace_set_syscall_trace(struct task_struct *p, bool on)
6177 - if (on)
6178 + if (on) {
6179 + p->ptrace |= PT_SYSCALL_TRACE;
6180 set_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
6181 - else
6182 + } else {
6183 + p->ptrace &= ~PT_SYSCALL_TRACE;
6184 clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
6188 static int ptrace_trapping_sleep_fn(void *flags)
6190 1.5.5.1
6193 From oleg@redhat.com Mon Nov 21 15:07:11 2011
6194 Return-Path: oleg@redhat.com
6195 Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
6196 zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
6197 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
6198 15:07:11 -0500 (EST)
6199 Received: from localhost (localhost.localdomain [127.0.0.1])
6200 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id B7D62D4DCC;
6201 Mon, 21 Nov 2011 15:07:11 -0500 (EST)
6202 Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
6203 by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
6204 with ESMTP id 1ENpGO166I-W; Mon, 21 Nov 2011 15:07:11 -0500 (EST)
6205 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22])
6206 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id A3A55D498A;
6207 Mon, 21 Nov 2011 15:07:11 -0500 (EST)
6208 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
6209 by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK79AS017728;
6210 Mon, 21 Nov 2011 15:07:10 -0500
6211 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
6212 oleg@redhat.com; Mon, 21 Nov 2011 21:02:30 +0100 (CET)
6213 Date: Mon, 21 Nov 2011 21:02:28 +0100
6214 From: Oleg Nesterov <oleg@redhat.com>
6215 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
6216 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
6217 Kyle McMartin <kmcmartin@redhat.com>
6218 Cc: kernel@lists.fedoraproject.org
6219 Subject: [PATCH 23/33] utrace: don't clear TIF_SYSCALL_TRACE if it is
6220 needed by ptrace
6221 Message-ID: <20111121200228.GA27863@redhat.com>
6222 MIME-Version: 1.0
6223 Content-Type: text/plain; charset=us-ascii
6224 Content-Disposition: inline
6225 In-Reply-To: <20111121200039.GA27699@redhat.com>
6226 User-Agent: Mutt/1.5.18 (2008-05-17)
6227 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22
6228 Status: RO
6229 Content-Length: 797
6230 Lines: 24
6232 TIF_SYSCALL_TRACE should be cleared only if both ptrace and utrace do
6233 not want it, change utrace_reset() to check PT_SYSCALL_TRACE before
6234 clear_tsk_thread_flag(TIF_SYSCALL_TRACE).
6236 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
6238 kernel/utrace.c | 1 +
6239 1 files changed, 1 insertions(+), 0 deletions(-)
6241 diff --git a/kernel/utrace.c b/kernel/utrace.c
6242 index 0bb0a06..bebf6de 100644
6243 --- a/kernel/utrace.c
6244 +++ b/kernel/utrace.c
6245 @@ -697,6 +697,7 @@ static bool utrace_reset(struct task_struct *task, struct utrace *utrace)
6246 BUG_ON(utrace->death);
6247 flags &= UTRACE_EVENT(REAP);
6248 } else if (!(flags & UTRACE_EVENT_SYSCALL) &&
6249 + !(task->ptrace & PT_SYSCALL_TRACE) &&
6250 test_tsk_thread_flag(task, TIF_SYSCALL_TRACE)) {
6251 clear_tsk_thread_flag(task, TIF_SYSCALL_TRACE);
6254 1.5.5.1
6257 From oleg@redhat.com Mon Nov 21 15:07:14 2011
6258 Return-Path: oleg@redhat.com
6259 Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
6260 zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
6261 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
6262 15:07:14 -0500 (EST)
6263 Received: from localhost (localhost.localdomain [127.0.0.1])
6264 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 5376AD8774;
6265 Mon, 21 Nov 2011 15:07:14 -0500 (EST)
6266 Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
6267 by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
6268 with ESMTP id 5Oc8ErfFfJSs; Mon, 21 Nov 2011 15:07:14 -0500 (EST)
6269 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23])
6270 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 3F2CFD81E9;
6271 Mon, 21 Nov 2011 15:07:14 -0500 (EST)
6272 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
6273 by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK7Cw3015467;
6274 Mon, 21 Nov 2011 15:07:12 -0500
6275 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
6276 oleg@redhat.com; Mon, 21 Nov 2011 21:02:32 +0100 (CET)
6277 Date: Mon, 21 Nov 2011 21:02:30 +0100
6278 From: Oleg Nesterov <oleg@redhat.com>
6279 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
6280 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
6281 Kyle McMartin <kmcmartin@redhat.com>
6282 Cc: kernel@lists.fedoraproject.org
6283 Subject: [PATCH 24/33] introduce task_utrace_lock/task_utrace_unlock
6284 Message-ID: <20111121200230.GA27870@redhat.com>
6285 MIME-Version: 1.0
6286 Content-Type: text/plain; charset=us-ascii
6287 Content-Disposition: inline
6288 In-Reply-To: <20111121200039.GA27699@redhat.com>
6289 User-Agent: Mutt/1.5.18 (2008-05-17)
6290 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23
6291 Status: RO
6292 Content-Length: 2190
6293 Lines: 81
6295 - Add task_utrace_lock(task). It simply takes task->utrace->lock if
6296 this task was ever utraced. Otherwise it takes task_lock(), this
6297 serializes with utrace_attach_task()->utrace_task_alloc(). In both
6298 case the caller can be sure it can't race with anything which needs
6299 utrace->lock.
6301 - Add task_utrace_unlock(task), it releases the corresponding lock.
6303 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
6305 include/linux/utrace.h | 9 +++++++++
6306 kernel/utrace.c | 26 ++++++++++++++++++++++++++
6307 2 files changed, 35 insertions(+), 0 deletions(-)
6309 diff --git a/include/linux/utrace.h b/include/linux/utrace.h
6310 index 63103e2..f37373b 100644
6311 --- a/include/linux/utrace.h
6312 +++ b/include/linux/utrace.h
6313 @@ -117,6 +117,12 @@ void utrace_signal_handler(struct task_struct *, int);
6315 #ifndef CONFIG_UTRACE
6317 +static inline void task_utrace_lock(struct task_struct *task)
6320 +static inline void task_utrace_unlock(struct task_struct *task)
6324 * <linux/tracehook.h> uses these accessors to avoid #ifdef CONFIG_UTRACE.
6326 @@ -139,6 +145,9 @@ static inline void task_utrace_proc_status(struct seq_file *m,
6328 #else /* CONFIG_UTRACE */
6330 +extern void task_utrace_lock(struct task_struct *task);
6331 +extern void task_utrace_unlock(struct task_struct *task);
6333 static inline unsigned long task_utrace_flags(struct task_struct *task)
6335 return task->utrace_flags;
6336 diff --git a/kernel/utrace.c b/kernel/utrace.c
6337 index bebf6de..960dd9e 100644
6338 --- a/kernel/utrace.c
6339 +++ b/kernel/utrace.c
6340 @@ -79,6 +79,32 @@ static int __init utrace_init(void)
6342 module_init(utrace_init);
6344 +void task_utrace_lock(struct task_struct *task)
6346 + struct utrace *utrace = task_utrace_struct(task);
6348 + if (!utrace) {
6349 + task_lock(task);
6350 + utrace = task_utrace_struct(task);
6351 + if (!utrace)
6352 + return;
6354 + task_unlock(task);
6357 + spin_lock(&utrace->lock);
6360 +void task_utrace_unlock(struct task_struct *task)
6362 + struct utrace *utrace = task_utrace_struct(task);
6364 + if (utrace)
6365 + spin_unlock(&utrace->lock);
6366 + else
6367 + task_unlock(task);
6371 * Set up @task.utrace for the first time. We can have races
6372 * between two utrace_attach_task() calls here. The task_lock()
6374 1.5.5.1
6377 From oleg@redhat.com Mon Nov 21 15:07:17 2011
6378 Return-Path: oleg@redhat.com
6379 Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
6380 zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
6381 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
6382 15:07:17 -0500 (EST)
6383 Received: from localhost (localhost.localdomain [127.0.0.1])
6384 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 01034D498A;
6385 Mon, 21 Nov 2011 15:07:17 -0500 (EST)
6386 Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
6387 by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
6388 with ESMTP id BglmrU3H2hJr; Mon, 21 Nov 2011 15:07:16 -0500 (EST)
6389 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23])
6390 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id E0D31D4FF4;
6391 Mon, 21 Nov 2011 15:07:16 -0500 (EST)
6392 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
6393 by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK7Egx015474;
6394 Mon, 21 Nov 2011 15:07:15 -0500
6395 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
6396 oleg@redhat.com; Mon, 21 Nov 2011 21:02:35 +0100 (CET)
6397 Date: Mon, 21 Nov 2011 21:02:33 +0100
6398 From: Oleg Nesterov <oleg@redhat.com>
6399 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
6400 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
6401 Kyle McMartin <kmcmartin@redhat.com>
6402 Cc: kernel@lists.fedoraproject.org
6403 Subject: [PATCH 25/33] teach ptrace_set_syscall_trace() to play well with
6404 utrace
6405 Message-ID: <20111121200233.GA27873@redhat.com>
6406 MIME-Version: 1.0
6407 Content-Type: text/plain; charset=us-ascii
6408 Content-Disposition: inline
6409 In-Reply-To: <20111121200039.GA27699@redhat.com>
6410 User-Agent: Mutt/1.5.18 (2008-05-17)
6411 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23
6412 Status: RO
6413 Content-Length: 1545
6414 Lines: 49
6416 1. ptrace_set_syscall_trace(true)->set_tsk_thread_flag(TIF_SYSCALL_TRACE)
6417 can race with utrace_control()->utrace_reset() path which can miss
6418 PT_SYSCALL_TRACE and clear TIF_SYSCALL_TRACE after it was already set.
6420 2. ptrace_set_syscall_trace(false) clears TIF_SYSCALL_TRACE and this is
6421 not utrace-friendly, it can need this flag.
6423 Change ptrace_set_syscall_trace() to take task_utrace_lock(), this is
6424 enough to fix the 1st problem. Check task_utrace_flags() before clearing
6425 TIF_SYSCALL_TRACE, this fixes 2.
6427 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
6429 kernel/ptrace.c | 6 +++++-
6430 1 files changed, 5 insertions(+), 1 deletions(-)
6432 diff --git a/kernel/ptrace.c b/kernel/ptrace.c
6433 index 1ac03eb..739183a 100644
6434 --- a/kernel/ptrace.c
6435 +++ b/kernel/ptrace.c
6436 @@ -24,6 +24,7 @@
6437 #include <linux/regset.h>
6438 #include <linux/hw_breakpoint.h>
6439 #include <linux/cn_proc.h>
6440 +#include <linux/utrace.h>
6442 void ptrace_signal_wake_up(struct task_struct *p, int quiescent)
6444 @@ -40,13 +41,16 @@ void ptrace_signal_wake_up(struct task_struct *p, int quiescent)
6446 static void ptrace_set_syscall_trace(struct task_struct *p, bool on)
6448 + task_utrace_lock(p);
6449 if (on) {
6450 p->ptrace |= PT_SYSCALL_TRACE;
6451 set_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
6452 } else {
6453 p->ptrace &= ~PT_SYSCALL_TRACE;
6454 - clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
6455 + if (!(task_utrace_flags(p) & UTRACE_EVENT_SYSCALL))
6456 + clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
6458 + task_utrace_unlock(p);
6461 static int ptrace_trapping_sleep_fn(void *flags)
6463 1.5.5.1
6466 From oleg@redhat.com Mon Nov 21 15:07:19 2011
6467 Return-Path: oleg@redhat.com
6468 Received: from zmta02.collab.prod.int.phx2.redhat.com (LHLO
6469 zmta02.collab.prod.int.phx2.redhat.com) (10.5.5.32) by
6470 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
6471 15:07:19 -0500 (EST)
6472 Received: from localhost (localhost.localdomain [127.0.0.1])
6473 by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 8F87F128EAA;
6474 Mon, 21 Nov 2011 15:07:19 -0500 (EST)
6475 Received: from zmta02.collab.prod.int.phx2.redhat.com ([127.0.0.1])
6476 by localhost (zmta02.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
6477 with ESMTP id yittEjCd+X4F; Mon, 21 Nov 2011 15:07:19 -0500 (EST)
6478 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11])
6479 by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 7BC701287D0;
6480 Mon, 21 Nov 2011 15:07:19 -0500 (EST)
6481 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
6482 by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK7HbH031553;
6483 Mon, 21 Nov 2011 15:07:18 -0500
6484 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
6485 oleg@redhat.com; Mon, 21 Nov 2011 21:02:38 +0100 (CET)
6486 Date: Mon, 21 Nov 2011 21:02:36 +0100
6487 From: Oleg Nesterov <oleg@redhat.com>
6488 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
6489 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
6490 Kyle McMartin <kmcmartin@redhat.com>
6491 Cc: kernel@lists.fedoraproject.org
6492 Subject: [PATCH 26/33] introduce PT_SINGLE_STEP and PT_SINGLE_BLOCK
6493 Message-ID: <20111121200236.GA27880@redhat.com>
6494 MIME-Version: 1.0
6495 Content-Type: text/plain; charset=us-ascii
6496 Content-Disposition: inline
6497 In-Reply-To: <20111121200039.GA27699@redhat.com>
6498 User-Agent: Mutt/1.5.18 (2008-05-17)
6499 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11
6500 Status: RO
6501 Content-Length: 3723
6502 Lines: 108
6504 Add the new internal ptrace flags, PT_SINGLE_STEP and PT_SINGLE_BLOCK.
6506 Like PT_SYSCALL_TRACE, this is needed to avoid the unnecessary ptrace
6507 reports when TIF_SINGLESTEP was set by another engine, not by ptrace.
6508 Also, we need these bits to coordinate the user_*_single_step() calls
6509 from ptrace and utrace.
6511 TODO: update the !x86 ptrace code which does user_disable_single_step().
6513 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
6515 arch/x86/kernel/ptrace.c | 1 +
6516 include/linux/ptrace.h | 5 ++++-
6517 include/linux/tracehook.h | 7 +++++--
6518 kernel/ptrace.c | 3 +++
6519 4 files changed, 13 insertions(+), 3 deletions(-)
6521 diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
6522 index 8252879..d1557dc 100644
6523 --- a/arch/x86/kernel/ptrace.c
6524 +++ b/arch/x86/kernel/ptrace.c
6525 @@ -808,6 +808,7 @@ static int ioperm_get(struct task_struct *target,
6527 void ptrace_disable(struct task_struct *child)
6529 + child->ptrace &= ~(PT_SINGLE_STEP | PT_SINGLE_BLOCK);
6530 user_disable_single_step(child);
6531 #ifdef TIF_SYSCALL_EMU
6532 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
6533 diff --git a/include/linux/ptrace.h b/include/linux/ptrace.h
6534 index c10f610..2743315 100644
6535 --- a/include/linux/ptrace.h
6536 +++ b/include/linux/ptrace.h
6537 @@ -105,6 +105,8 @@
6538 #define PT_TRACE_MASK 0x000003f4
6540 #define PT_SYSCALL_TRACE 0x00020000
6541 +#define PT_SINGLE_STEP 0x00040000
6542 +#define PT_SINGLE_BLOCK 0x00080000
6544 /* single stepping state bits (used on ARM and PA-RISC) */
6545 #define PT_SINGLESTEP_BIT 31
6546 @@ -229,7 +231,8 @@ static inline void ptrace_init_task(struct task_struct *child, bool ptrace)
6548 if (unlikely(ptrace) && current->ptrace) {
6549 child->ptrace = current->ptrace;
6550 - child->ptrace &= ~PT_SYSCALL_TRACE;
6551 + child->ptrace &=
6552 + ~(PT_SYSCALL_TRACE | PT_SINGLE_STEP | PT_SINGLE_BLOCK);
6553 __ptrace_link(child, current->parent);
6555 if (child->ptrace & PT_SEIZED)
6556 diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
6557 index eb9fe30..21c8ca2 100644
6558 --- a/include/linux/tracehook.h
6559 +++ b/include/linux/tracehook.h
6560 @@ -104,6 +104,9 @@ static inline __must_check int tracehook_report_syscall_entry(
6561 return 0;
6564 +#define ptrace_wants_step() \
6565 + (current->ptrace & (PT_SINGLE_STEP | PT_SINGLE_BLOCK))
6568 * tracehook_report_syscall_exit - task has just finished a system call
6569 * @regs: user register state of current task
6570 @@ -126,7 +129,7 @@ static inline void tracehook_report_syscall_exit(struct pt_regs *regs, int step)
6571 if (task_utrace_flags(current) & UTRACE_EVENT(SYSCALL_EXIT))
6572 utrace_report_syscall_exit(regs);
6574 - if (step) {
6575 + if (step && ptrace_wants_step()) {
6576 siginfo_t info;
6577 user_single_step_siginfo(current, regs, &info);
6578 force_sig_info(SIGTRAP, &info, current);
6579 @@ -157,7 +160,7 @@ static inline void tracehook_signal_handler(int sig, siginfo_t *info,
6581 if (task_utrace_flags(current))
6582 utrace_signal_handler(current, stepping);
6583 - if (stepping)
6584 + if (stepping && ptrace_wants_step())
6585 ptrace_notify(SIGTRAP);
6588 diff --git a/kernel/ptrace.c b/kernel/ptrace.c
6589 index 739183a..792080d 100644
6590 --- a/kernel/ptrace.c
6591 +++ b/kernel/ptrace.c
6592 @@ -630,13 +630,16 @@ static int ptrace_resume(struct task_struct *child, long request,
6593 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
6594 #endif
6596 + child->ptrace &= ~(PT_SINGLE_STEP | PT_SINGLE_BLOCK);
6597 if (is_singleblock(request)) {
6598 if (unlikely(!arch_has_block_step()))
6599 return -EIO;
6600 + child->ptrace |= PT_SINGLE_BLOCK;
6601 user_enable_block_step(child);
6602 } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
6603 if (unlikely(!arch_has_single_step()))
6604 return -EIO;
6605 + child->ptrace |= PT_SINGLE_STEP;
6606 user_enable_single_step(child);
6607 } else {
6608 user_disable_single_step(child);
6610 1.5.5.1
6613 From oleg@redhat.com Mon Nov 21 15:07:22 2011
6614 Return-Path: oleg@redhat.com
6615 Received: from zmta02.collab.prod.int.phx2.redhat.com (LHLO
6616 zmta02.collab.prod.int.phx2.redhat.com) (10.5.5.32) by
6617 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
6618 15:07:22 -0500 (EST)
6619 Received: from localhost (localhost.localdomain [127.0.0.1])
6620 by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 0C136128FB0;
6621 Mon, 21 Nov 2011 15:07:22 -0500 (EST)
6622 Received: from zmta02.collab.prod.int.phx2.redhat.com ([127.0.0.1])
6623 by localhost (zmta02.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
6624 with ESMTP id GNbS20gY5Jq9; Mon, 21 Nov 2011 15:07:21 -0500 (EST)
6625 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])
6626 by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id EB5821287D0;
6627 Mon, 21 Nov 2011 15:07:21 -0500 (EST)
6628 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
6629 by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK7Kn1029766;
6630 Mon, 21 Nov 2011 15:07:20 -0500
6631 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
6632 oleg@redhat.com; Mon, 21 Nov 2011 21:02:40 +0100 (CET)
6633 Date: Mon, 21 Nov 2011 21:02:38 +0100
6634 From: Oleg Nesterov <oleg@redhat.com>
6635 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
6636 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
6637 Kyle McMartin <kmcmartin@redhat.com>
6638 Cc: kernel@lists.fedoraproject.org
6639 Subject: [PATCH 27/33] utrace: finish_resume_report: don't do
6640 user_xxx_step() if ptrace_wants_step()
6641 Message-ID: <20111121200238.GA27883@redhat.com>
6642 MIME-Version: 1.0
6643 Content-Type: text/plain; charset=us-ascii
6644 Content-Disposition: inline
6645 In-Reply-To: <20111121200039.GA27699@redhat.com>
6646 User-Agent: Mutt/1.5.18 (2008-05-17)
6647 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12
6648 Status: RO
6649 Content-Length: 2741
6650 Lines: 84
6652 finish_resume_report() should not enable/disable the stepping if
6653 ptrace_wants_step() == T. If ptrace wants block_step while utrace
6654 wants single_step we could "promote" the stepping, but I do not
6655 think this really makes sense.
6657 Unless the tracee is killed this can't race with ptrace, this is
6658 called by the tracee itself. If it is killed we do not care.
6660 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
6662 include/linux/tracehook.h | 8 ++++----
6663 kernel/utrace.c | 9 ++++++---
6664 2 files changed, 10 insertions(+), 7 deletions(-)
6666 diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
6667 index 21c8ca2..b6812d4 100644
6668 --- a/include/linux/tracehook.h
6669 +++ b/include/linux/tracehook.h
6670 @@ -104,8 +104,8 @@ static inline __must_check int tracehook_report_syscall_entry(
6671 return 0;
6674 -#define ptrace_wants_step() \
6675 - (current->ptrace & (PT_SINGLE_STEP | PT_SINGLE_BLOCK))
6676 +#define ptrace_wants_step(task) \
6677 + ((task)->ptrace & (PT_SINGLE_STEP | PT_SINGLE_BLOCK))
6680 * tracehook_report_syscall_exit - task has just finished a system call
6681 @@ -129,7 +129,7 @@ static inline void tracehook_report_syscall_exit(struct pt_regs *regs, int step)
6682 if (task_utrace_flags(current) & UTRACE_EVENT(SYSCALL_EXIT))
6683 utrace_report_syscall_exit(regs);
6685 - if (step && ptrace_wants_step()) {
6686 + if (step && ptrace_wants_step(current)) {
6687 siginfo_t info;
6688 user_single_step_siginfo(current, regs, &info);
6689 force_sig_info(SIGTRAP, &info, current);
6690 @@ -160,7 +160,7 @@ static inline void tracehook_signal_handler(int sig, siginfo_t *info,
6692 if (task_utrace_flags(current))
6693 utrace_signal_handler(current, stepping);
6694 - if (stepping && ptrace_wants_step())
6695 + if (stepping && ptrace_wants_step(current))
6696 ptrace_notify(SIGTRAP);
6699 diff --git a/kernel/utrace.c b/kernel/utrace.c
6700 index 960dd9e..05e8532 100644
6701 --- a/kernel/utrace.c
6702 +++ b/kernel/utrace.c
6703 @@ -1829,7 +1829,8 @@ static void finish_resume_report(struct task_struct *task,
6705 case UTRACE_BLOCKSTEP:
6706 if (likely(arch_has_block_step())) {
6707 - user_enable_block_step(task);
6708 + if (!ptrace_wants_step(task))
6709 + user_enable_block_step(task);
6710 break;
6713 @@ -1842,7 +1843,8 @@ static void finish_resume_report(struct task_struct *task,
6715 case UTRACE_SINGLESTEP:
6716 if (likely(arch_has_single_step())) {
6717 - user_enable_single_step(task);
6718 + if (!ptrace_wants_step(task))
6719 + user_enable_single_step(task);
6720 } else {
6722 * This means some callback is to blame for failing
6723 @@ -1857,7 +1859,8 @@ static void finish_resume_report(struct task_struct *task,
6724 case UTRACE_REPORT:
6725 case UTRACE_RESUME:
6726 default:
6727 - user_disable_single_step(task);
6728 + if (!ptrace_wants_step(task))
6729 + user_disable_single_step(task);
6730 break;
6734 1.5.5.1
6737 From oleg@redhat.com Mon Nov 21 15:07:24 2011
6738 Return-Path: oleg@redhat.com
6739 Received: from zmta02.collab.prod.int.phx2.redhat.com (LHLO
6740 zmta02.collab.prod.int.phx2.redhat.com) (10.5.5.32) by
6741 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
6742 15:07:24 -0500 (EST)
6743 Received: from localhost (localhost.localdomain [127.0.0.1])
6744 by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 8954F12914D;
6745 Mon, 21 Nov 2011 15:07:24 -0500 (EST)
6746 Received: from zmta02.collab.prod.int.phx2.redhat.com ([127.0.0.1])
6747 by localhost (zmta02.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
6748 with ESMTP id 0lU8wXKC78yu; Mon, 21 Nov 2011 15:07:24 -0500 (EST)
6749 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25])
6750 by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 759B61287D0;
6751 Mon, 21 Nov 2011 15:07:24 -0500 (EST)
6752 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
6753 by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK7Mi7022329;
6754 Mon, 21 Nov 2011 15:07:23 -0500
6755 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
6756 oleg@redhat.com; Mon, 21 Nov 2011 21:02:43 +0100 (CET)
6757 Date: Mon, 21 Nov 2011 21:02:41 +0100
6758 From: Oleg Nesterov <oleg@redhat.com>
6759 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
6760 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
6761 Kyle McMartin <kmcmartin@redhat.com>
6762 Cc: kernel@lists.fedoraproject.org
6763 Subject: [PATCH 28/33] ptrace: shift user_*_step() from ptrace_resume() to
6764 ptrace_stop()
6765 Message-ID: <20111121200241.GA27890@redhat.com>
6766 MIME-Version: 1.0
6767 Content-Type: text/plain; charset=us-ascii
6768 Content-Disposition: inline
6769 In-Reply-To: <20111121200039.GA27699@redhat.com>
6770 User-Agent: Mutt/1.5.18 (2008-05-17)
6771 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25
6772 Status: RO
6773 Content-Length: 2374
6774 Lines: 70
6776 1. ptrace_resume() plays with the stopped task which can be also
6777 task_is_utraced(). In this case user_enable_xxx_step() can race
6778 with utrace_reset()->user_disable_single_step().
6780 We could change utrace_reset() to check ptrace_wants_step() and
6781 add the task_utrace_lock + unlock barrier after ptrace_resume()
6782 sets PT_SINGLE_STEP.
6784 But it is better to reassign enable/desable from the tracer to
6785 the tracee, it can check its PT_SINGLE_ bits after wakeup. This
6786 also makes sense because enable_step(task) can be faster if
6787 task == current.
6789 2. ptrace can do user_disable_single_step() while utrace needs the
6790 stepping.
6792 Set TIF_NOTIFY_RESUME after user_disable_single_step() to ensure
6793 the tracee can't return to the user-space without utrace_resume().
6794 Any correct engine which wants the stepping should reassert it.
6796 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
6798 kernel/ptrace.c | 4 ----
6799 kernel/signal.c | 11 +++++++++++
6800 2 files changed, 11 insertions(+), 4 deletions(-)
6802 diff --git a/kernel/ptrace.c b/kernel/ptrace.c
6803 index 792080d..eba9a22 100644
6804 --- a/kernel/ptrace.c
6805 +++ b/kernel/ptrace.c
6806 @@ -635,14 +635,10 @@ static int ptrace_resume(struct task_struct *child, long request,
6807 if (unlikely(!arch_has_block_step()))
6808 return -EIO;
6809 child->ptrace |= PT_SINGLE_BLOCK;
6810 - user_enable_block_step(child);
6811 } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
6812 if (unlikely(!arch_has_single_step()))
6813 return -EIO;
6814 child->ptrace |= PT_SINGLE_STEP;
6815 - user_enable_single_step(child);
6816 - } else {
6817 - user_disable_single_step(child);
6820 child->exit_code = data;
6821 diff --git a/kernel/signal.c b/kernel/signal.c
6822 index 38ea4e6..b13d2bc 100644
6823 --- a/kernel/signal.c
6824 +++ b/kernel/signal.c
6825 @@ -1928,6 +1928,17 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
6827 utrace_end_stop();
6829 + if (current->ptrace & PT_SINGLE_BLOCK)
6830 + user_enable_block_step(current);
6831 + else if (current->ptrace & PT_SINGLE_STEP)
6832 + user_enable_single_step(current);
6833 + else {
6834 + user_disable_single_step(current);
6835 + /* if utrace needs the stepping it should reassert */
6836 + if (task_utrace_flags(current))
6837 + set_thread_flag(TIF_NOTIFY_RESUME);
6841 * While in TASK_TRACED, we were considered "frozen enough".
6842 * Now that we woke up, it's crucial if we're supposed to be
6844 1.5.5.1
6847 From oleg@redhat.com Mon Nov 21 15:07:27 2011
6848 Return-Path: oleg@redhat.com
6849 Received: from zmta02.collab.prod.int.phx2.redhat.com (LHLO
6850 zmta02.collab.prod.int.phx2.redhat.com) (10.5.5.32) by
6851 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
6852 15:07:27 -0500 (EST)
6853 Received: from localhost (localhost.localdomain [127.0.0.1])
6854 by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 043581287D0;
6855 Mon, 21 Nov 2011 15:07:27 -0500 (EST)
6856 Received: from zmta02.collab.prod.int.phx2.redhat.com ([127.0.0.1])
6857 by localhost (zmta02.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
6858 with ESMTP id 4WO1El1jiIrJ; Mon, 21 Nov 2011 15:07:26 -0500 (EST)
6859 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22])
6860 by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id E3FCA129045;
6861 Mon, 21 Nov 2011 15:07:26 -0500 (EST)
6862 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
6863 by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK7OdV017836;
6864 Mon, 21 Nov 2011 15:07:25 -0500
6865 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
6866 oleg@redhat.com; Mon, 21 Nov 2011 21:02:45 +0100 (CET)
6867 Date: Mon, 21 Nov 2011 21:02:43 +0100
6868 From: Oleg Nesterov <oleg@redhat.com>
6869 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
6870 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
6871 Kyle McMartin <kmcmartin@redhat.com>
6872 Cc: kernel@lists.fedoraproject.org
6873 Subject: [PATCH 29/33] ptrace_disable: no need to disable stepping
6874 Message-ID: <20111121200243.GA27893@redhat.com>
6875 MIME-Version: 1.0
6876 Content-Type: text/plain; charset=us-ascii
6877 Content-Disposition: inline
6878 In-Reply-To: <20111121200039.GA27699@redhat.com>
6879 User-Agent: Mutt/1.5.18 (2008-05-17)
6880 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22
6881 Status: RO
6882 Content-Length: 945
6883 Lines: 29
6885 ptrace_disable() is called when the tracee is quiescent and we
6886 are going to untrace. This means we are going to clear ->ptrace
6887 and wake up the tracee. Now that ptrace_stop() checks PT_ bits
6888 and does user_disable_single_step() we can remove this code from
6889 ptrace_disable(), it is unneeded and not utrace-friendly.
6891 TODO: change !x86 code.
6893 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
6895 arch/x86/kernel/ptrace.c | 2 --
6896 1 files changed, 0 insertions(+), 2 deletions(-)
6898 diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
6899 index d1557dc..96d315a 100644
6900 --- a/arch/x86/kernel/ptrace.c
6901 +++ b/arch/x86/kernel/ptrace.c
6902 @@ -808,8 +808,6 @@ static int ioperm_get(struct task_struct *target,
6904 void ptrace_disable(struct task_struct *child)
6906 - child->ptrace &= ~(PT_SINGLE_STEP | PT_SINGLE_BLOCK);
6907 - user_disable_single_step(child);
6908 #ifdef TIF_SYSCALL_EMU
6909 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
6910 #endif
6912 1.5.5.1
6915 From oleg@redhat.com Mon Nov 21 15:07:29 2011
6916 Return-Path: oleg@redhat.com
6917 Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
6918 zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
6919 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
6920 15:07:29 -0500 (EST)
6921 Received: from localhost (localhost.localdomain [127.0.0.1])
6922 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 8A6E8D4C39;
6923 Mon, 21 Nov 2011 15:07:29 -0500 (EST)
6924 Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
6925 by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
6926 with ESMTP id NLURBx2yQes5; Mon, 21 Nov 2011 15:07:29 -0500 (EST)
6927 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23])
6928 by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 7660DD498A;
6929 Mon, 21 Nov 2011 15:07:29 -0500 (EST)
6930 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
6931 by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK7RnP015578;
6932 Mon, 21 Nov 2011 15:07:27 -0500
6933 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
6934 oleg@redhat.com; Mon, 21 Nov 2011 21:02:48 +0100 (CET)
6935 Date: Mon, 21 Nov 2011 21:02:46 +0100
6936 From: Oleg Nesterov <oleg@redhat.com>
6937 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
6938 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
6939 Kyle McMartin <kmcmartin@redhat.com>
6940 Cc: kernel@lists.fedoraproject.org
6941 Subject: [PATCH 30/33] ptrace_report_syscall: check TIF_SYSCALL_EMU
6942 Message-ID: <20111121200246.GA27896@redhat.com>
6943 MIME-Version: 1.0
6944 Content-Type: text/plain; charset=us-ascii
6945 Content-Disposition: inline
6946 In-Reply-To: <20111121200039.GA27699@redhat.com>
6947 User-Agent: Mutt/1.5.18 (2008-05-17)
6948 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23
6949 Status: RO
6950 Content-Length: 858
6951 Lines: 28
6953 4d16a64 "introduce PT_SYSCALL_TRACE flag" breaks PTRACE_SYSEMU
6954 which doesn't set PT_SYSCALL_TRACE.
6956 Change ptrace_report_syscall() to check TIF_SYSCALL_EMU as well.
6957 This can't conflict with utrace, this flag can only be set by
6958 ptrace.
6960 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
6962 include/linux/tracehook.h | 2 +-
6963 1 files changed, 1 insertions(+), 1 deletions(-)
6965 diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
6966 index b6812d4..90ca578 100644
6967 --- a/include/linux/tracehook.h
6968 +++ b/include/linux/tracehook.h
6969 @@ -59,7 +59,7 @@ static inline void ptrace_report_syscall(struct pt_regs *regs)
6971 int ptrace = current->ptrace;
6973 - if (!(ptrace & PT_SYSCALL_TRACE))
6974 + if (!(ptrace & PT_SYSCALL_TRACE) && !test_thread_flag(TIF_SYSCALL_EMU))
6975 return;
6977 ptrace_notify(SIGTRAP | ((ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
6979 1.5.5.1
6982 From oleg@redhat.com Mon Nov 21 15:07:32 2011
6983 Return-Path: oleg@redhat.com
6984 Received: from zmta02.collab.prod.int.phx2.redhat.com (LHLO
6985 zmta02.collab.prod.int.phx2.redhat.com) (10.5.5.32) by
6986 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
6987 15:07:32 -0500 (EST)
6988 Received: from localhost (localhost.localdomain [127.0.0.1])
6989 by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 10D1312916C;
6990 Mon, 21 Nov 2011 15:07:32 -0500 (EST)
6991 Received: from zmta02.collab.prod.int.phx2.redhat.com ([127.0.0.1])
6992 by localhost (zmta02.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
6993 with ESMTP id Z14plX1oxaYL; Mon, 21 Nov 2011 15:07:32 -0500 (EST)
6994 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25])
6995 by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id F12FF128E1D;
6996 Mon, 21 Nov 2011 15:07:31 -0500 (EST)
6997 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
6998 by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK7T0q022460;
6999 Mon, 21 Nov 2011 15:07:30 -0500
7000 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
7001 oleg@redhat.com; Mon, 21 Nov 2011 21:02:50 +0100 (CET)
7002 Date: Mon, 21 Nov 2011 21:02:48 +0100
7003 From: Oleg Nesterov <oleg@redhat.com>
7004 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
7005 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
7006 Kyle McMartin <kmcmartin@redhat.com>
7007 Cc: kernel@lists.fedoraproject.org
7008 Subject: [PATCH 31/33] utrace_resume: check irqs_disabled() to shut up
7009 lockdep
7010 Message-ID: <20111121200248.GA27903@redhat.com>
7011 MIME-Version: 1.0
7012 Content-Type: text/plain; charset=us-ascii
7013 Content-Disposition: inline
7014 In-Reply-To: <20111121200039.GA27699@redhat.com>
7015 User-Agent: Mutt/1.5.18 (2008-05-17)
7016 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25
7017 Status: RO
7018 Content-Length: 823
7019 Lines: 26
7021 utrace_resume() enables irqs unconditionally. With the recent changes
7022 in lockdep.c this triggers the warning. Check irqs_disabled() before
7023 local_irq_enable().
7025 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
7027 kernel/utrace.c | 3 ++-
7028 1 files changed, 2 insertions(+), 1 deletions(-)
7030 diff --git a/kernel/utrace.c b/kernel/utrace.c
7031 index 05e8532..c817a46 100644
7032 --- a/kernel/utrace.c
7033 +++ b/kernel/utrace.c
7034 @@ -1881,7 +1881,8 @@ void utrace_resume(struct task_struct *task, struct pt_regs *regs)
7035 * code path leads to calling into get_signal_to_deliver(), which
7036 * implicitly reenables them by virtue of spin_unlock_irq.
7038 - local_irq_enable();
7039 + if (irqs_disabled()) /* make trace_hardirqs_on() happy */
7040 + local_irq_enable();
7043 * If this flag is still set it's because there was a signal
7045 1.5.5.1
7048 From oleg@redhat.com Mon Nov 21 15:07:34 2011
7049 Return-Path: oleg@redhat.com
7050 Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
7051 zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
7052 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
7053 15:07:34 -0500 (EST)
7054 Received: from localhost (localhost.localdomain [127.0.0.1])
7055 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 860BCD8A3E;
7056 Mon, 21 Nov 2011 15:07:34 -0500 (EST)
7057 Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
7058 by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
7059 with ESMTP id pVTbN1oF62dg; Mon, 21 Nov 2011 15:07:34 -0500 (EST)
7060 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])
7061 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 6FBFFD87A3;
7062 Mon, 21 Nov 2011 15:07:34 -0500 (EST)
7063 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
7064 by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK7W0B029871;
7065 Mon, 21 Nov 2011 15:07:32 -0500
7066 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
7067 oleg@redhat.com; Mon, 21 Nov 2011 21:02:53 +0100 (CET)
7068 Date: Mon, 21 Nov 2011 21:02:51 +0100
7069 From: Oleg Nesterov <oleg@redhat.com>
7070 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
7071 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
7072 Kyle McMartin <kmcmartin@redhat.com>
7073 Cc: kernel@lists.fedoraproject.org
7074 Subject: [PATCH 32/33] ptrace_report_syscall: check if TIF_SYSCALL_EMU is
7075 defined
7076 Message-ID: <20111121200251.GA27906@redhat.com>
7077 MIME-Version: 1.0
7078 Content-Type: text/plain; charset=us-ascii
7079 Content-Disposition: inline
7080 In-Reply-To: <20111121200039.GA27699@redhat.com>
7081 User-Agent: Mutt/1.5.18 (2008-05-17)
7082 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12
7083 Status: RO
7084 Content-Length: 839
7085 Lines: 31
7087 From: Tony Breeds <tony@bakeyournoodle.com>
7089 TIF_SYSCALL_EMU is x86 only, add ifdef into ptrace_report_syscall().
7091 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
7093 include/linux/tracehook.h | 8 ++++++--
7094 1 files changed, 6 insertions(+), 2 deletions(-)
7096 diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
7097 index 90ca578..a1bac95 100644
7098 --- a/include/linux/tracehook.h
7099 +++ b/include/linux/tracehook.h
7100 @@ -59,8 +59,12 @@ static inline void ptrace_report_syscall(struct pt_regs *regs)
7102 int ptrace = current->ptrace;
7104 - if (!(ptrace & PT_SYSCALL_TRACE) && !test_thread_flag(TIF_SYSCALL_EMU))
7105 - return;
7106 + if (!(ptrace & PT_SYSCALL_TRACE)) {
7107 +#ifdef TIF_SYSCALL_EMU
7108 + if (!test_thread_flag(TIF_SYSCALL_EMU))
7109 +#endif
7110 + return;
7113 ptrace_notify(SIGTRAP | ((ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
7116 1.5.5.1
7119 From oleg@redhat.com Mon Nov 21 15:07:37 2011
7120 Return-Path: oleg@redhat.com
7121 Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
7122 zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
7123 zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
7124 15:07:37 -0500 (EST)
7125 Received: from localhost (localhost.localdomain [127.0.0.1])
7126 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 33405D8A50;
7127 Mon, 21 Nov 2011 15:07:37 -0500 (EST)
7128 Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
7129 by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
7130 with ESMTP id 3WH8FhvizjEA; Mon, 21 Nov 2011 15:07:37 -0500 (EST)
7131 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22])
7132 by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 1274FD89BB;
7133 Mon, 21 Nov 2011 15:07:37 -0500 (EST)
7134 Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
7135 by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK7YHr017898;
7136 Mon, 21 Nov 2011 15:07:35 -0500
7137 Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
7138 oleg@redhat.com; Mon, 21 Nov 2011 21:02:55 +0100 (CET)
7139 Date: Mon, 21 Nov 2011 21:02:53 +0100
7140 From: Oleg Nesterov <oleg@redhat.com>
7141 To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
7142 Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
7143 Kyle McMartin <kmcmartin@redhat.com>
7144 Cc: kernel@lists.fedoraproject.org
7145 Subject: [PATCH 33/33] utrace: s390: fix the compile problem with traps.c
7146 Message-ID: <20111121200253.GA27913@redhat.com>
7147 MIME-Version: 1.0
7148 Content-Type: text/plain; charset=us-ascii
7149 Content-Disposition: inline
7150 In-Reply-To: <20111121200039.GA27699@redhat.com>
7151 User-Agent: Mutt/1.5.18 (2008-05-17)
7152 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22
7153 Status: RO
7154 Content-Length: 725
7155 Lines: 25
7157 d99e60e5 "tracehooks: reintroduce tracehook_consider_fatal_signal()"
7158 breaks the compilation of arch/s390/kernel/traps.c. Restore the
7159 necessary include removed by upstream 73b7d40f commit.
7161 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
7163 arch/s390/kernel/traps.c | 2 +-
7164 1 files changed, 1 insertions(+), 1 deletions(-)
7166 diff --git a/arch/s390/kernel/traps.c b/arch/s390/kernel/traps.c
7167 index f506e1b..3132498 100644
7168 --- a/arch/s390/kernel/traps.c
7169 +++ b/arch/s390/kernel/traps.c
7170 @@ -18,7 +18,7 @@
7171 #include <linux/kernel.h>
7172 #include <linux/string.h>
7173 #include <linux/errno.h>
7174 -#include <linux/ptrace.h>
7175 +#include <linux/tracehook.h>
7176 #include <linux/timer.h>
7177 #include <linux/mm.h>
7178 #include <linux/smp.h>
7180 1.5.5.1