CHROMIUM: coredump: only SIGKILL should interrupt the coredumping task
authorOleg Nesterov <oleg@redhat.com>
Sun, 17 Feb 2013 19:18:49 +0000 (20:18 +0100)
committerChromeBot <chrome-bot@google.com>
Wed, 20 Feb 2013 19:58:17 +0000 (11:58 -0800)
There are 2 well known and ancient problems with coredump/signals,
and a lot of related bug reports:

- do_coredump() clears TIF_SIGPENDING but of course this can't help
  if, say, SIGCHLD comes after that.

  In this case the coredump can fail unexpectedly. See for example
  wait_for_dump_helper()->signal_pending() check but there are other
  reasons.

- At the same time, dumping a huge core on the slow media can take a
  lot of time/resources and there is no way to kill the coredumping
  task reliably. In particular this is not oom_kill-friendly.

This patch tries to fix the 1st problem, and makes the preparation
for the next changes.

We add the new SIGNAL_GROUP_COREDUMP flag set by zap_threads() to
indicate that this process dumps the core. prepare_signal() checks
this flag and nacks any signal except SIGKILL.

Note that this check tries to be conservative, in the long term we
should probably treat the SIGNAL_GROUP_EXIT case equally but this
needs more discussion. See marc.info/?l=linux-kernel&m=120508897917439

Notes:
- recalc_sigpending() doesn't check SIGNAL_GROUP_COREDUMP.
  The patch assumes that dump_write/etc paths should never
  call it, but we can change it as well.

- There is another source of TIF_SIGPENDING, freezer. This
  will be addressed separately.

Under review upstream:

http://thread.gmane.org/gmane.linux.kernel/1442910

BUG=chrome-os-partner:16961
TEST=See "coredump: prevent crash pipe reader from blocking suspend."

Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Mandeep Singh Baines <msb@chromium.org>
Change-Id: I2a660ef2e61d5132dad2aedbcd543d08f1003beb
Reviewed-on: https://gerrit.chromium.org/gerrit/43578
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
fs/exec.c
include/linux/sched.h
kernel/signal.c

index 6ae85f6..72f73ae 100644 (file)
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1855,8 +1855,8 @@ static int zap_process(struct task_struct *start, int exit_code)
        return nr;
 }
 
-static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
-                               struct core_state *core_state, int exit_code)
+static int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
+                       struct core_state *core_state, int exit_code)
 {
        struct task_struct *g, *p;
        unsigned long flags;
@@ -1866,6 +1866,9 @@ static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
        if (!signal_group_exit(tsk->signal)) {
                mm->core_state = core_state;
                nr = zap_process(tsk, exit_code);
+               /* ignore all signals except SIGKILL, see prepare_signal() */
+               tsk->signal->flags |= SIGNAL_GROUP_COREDUMP;
+               clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
        }
        spin_unlock_irq(&tsk->sighand->siglock);
        if (unlikely(nr < 0))
@@ -2142,12 +2145,6 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs)
 
        old_cred = override_creds(cred);
 
-       /*
-        * Clear any false indication of pending signals that might
-        * be seen by the filesystem code called to write the core file.
-        */
-       clear_thread_flag(TIF_SIGPENDING);
-
        ispipe = format_corename(&cn, signr);
 
        if (ispipe) {
index f457137..a1c9e4a 100644 (file)
@@ -685,6 +685,7 @@ struct signal_struct {
 #define SIGNAL_STOP_STOPPED    0x00000001 /* job control stop in effect */
 #define SIGNAL_STOP_CONTINUED  0x00000002 /* SIGCONT since WCONTINUED reap */
 #define SIGNAL_GROUP_EXIT      0x00000004 /* group exit in progress */
+#define SIGNAL_GROUP_COREDUMP  0x00000008 /* coredump in progress */
 /*
  * Pending notifications to parent.
  */
index 89684c1..5b8bbc6 100644 (file)
@@ -849,12 +849,14 @@ static void ptrace_trap_notify(struct task_struct *t)
  * Returns true if the signal should be actually delivered, otherwise
  * it should be dropped.
  */
-static int prepare_signal(int sig, struct task_struct *p, bool force)
+static bool prepare_signal(int sig, struct task_struct *p, bool force)
 {
        struct signal_struct *signal = p->signal;
        struct task_struct *t;
 
-       if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
+       if (signal->flags & (SIGNAL_GROUP_EXIT | SIGNAL_GROUP_COREDUMP)) {
+               if (signal->flags & SIGNAL_GROUP_COREDUMP)
+                       return sig == SIGKILL;
                /*
                 * The process is in the middle of dying, nothing to do.
                 */