Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1696632imm; Wed, 8 Aug 2018 23:58:42 -0700 (PDT) X-Google-Smtp-Source: AA+uWPyc33rbh2vobPiuCHsc0JGasOxci3Z2LfE3mBuqMQ588BACFW1eNMe4aVT2PMqKPEY2ojAP X-Received: by 2002:a63:f414:: with SMTP id g20-v6mr893284pgi.407.1533797922633; Wed, 08 Aug 2018 23:58:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533797922; cv=none; d=google.com; s=arc-20160816; b=08+aBKt9YE7TOcQtH+0GNPIBkAz5O38IaD4BwNkIo34577zJoj+99hDKxLgLj7dgbO s9OIN98Y2jV6wOSfkDmLT1sX/s0ozmwL3QFtogxyemFl4kACcFrwZvyrPL/69YFi9jPS qF4gyawT3N0JVdyC90AIkh+GTpQS3Hn4mjVIEPi0ODl3oVysa73hHE/Nm1en634cAVTz 36GytxoswTcpjhoMxIxmNSKrtEici6/AmtLf4nQItlbvlV9qdomQ5GI0T6hnllyO3FoA hhf4oBCeV9NPYjZ3NBAglM/oG8t+BwnQNmRWwescB5BBMueZAwt2bls4f+uTmN6puaxO Amyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:subject:references:in-reply-to:message-id :date:cc:to:from:arc-authentication-results; bh=awbE6A6f+pF0MFs22yYWsTM8CZmxOvMOhJdxUoDkTuI=; b=Qv4esIp65s+TAj0C9fQzSWRwKJt6i2hzOn2lD1dfp5hJvYDkWCQWcF6MS1Yhf8vzGx JoF3SjUYNwexF6IUXkE5HEKuHmcVSuffolazbt3ntsdFYldHMzO6scSVQkAh7YKbBwXe eorP6RpUckXpQtOXNNkqX7ZSYrle2yJPJmmc1J6BRoTTuFyQl6cqZqT04dfR2HnFrRm6 W4BEUkvdnr1+qUi8kxtmawurs5u1MerGB3uaacXU8LRwl2C/y1zOXliM6Tqee4n+M31i 3pUxmc9E7lMaQM9ihN4eTteS2ZOLpwaWrsoi6Kvp80mfpZrERP0US1MVsL0UEe7LUJ/M OljA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w4-v6si7231351pfb.52.2018.08.08.23.58.28; Wed, 08 Aug 2018 23:58:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728978AbeHIJVA (ORCPT + 99 others); Thu, 9 Aug 2018 05:21:00 -0400 Received: from out01.mta.xmission.com ([166.70.13.231]:43876 "EHLO out01.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725878AbeHIJU7 (ORCPT ); Thu, 9 Aug 2018 05:20:59 -0400 Received: from in02.mta.xmission.com ([166.70.13.52]) by out01.mta.xmission.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.87) (envelope-from ) id 1fnet4-0008Jd-QV; Thu, 09 Aug 2018 00:57:34 -0600 Received: from [97.119.167.31] (helo=x220.int.ebiederm.org) by in02.mta.xmission.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_CBC_SHA256:128) (Exim 4.87) (envelope-from ) id 1fnet4-00054s-59; Thu, 09 Aug 2018 00:57:34 -0600 From: "Eric W. Biederman" To: Oleg Nesterov Cc: Andrew Morton , linux-kernel@vger.kernel.org, Wen Yang , majiang , Linus Torvalds , "Eric W. Biederman" Date: Thu, 9 Aug 2018 01:56:04 -0500 Message-Id: <20180809065605.32345-5-ebiederm@xmission.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <87wot0yqsx.fsf_-_@xmission.com> References: <87wot0yqsx.fsf_-_@xmission.com> X-XM-SPF: eid=1fnet4-00054s-59;;;mid=<20180809065605.32345-5-ebiederm@xmission.com>;;;hst=in02.mta.xmission.com;;;ip=97.119.167.31;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX19ww3GyS0DV1xt2NGGGjhg+7q0DiDflhAE= X-SA-Exim-Connect-IP: 97.119.167.31 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on sa08.xmission.com X-Spam-Level: X-Spam-Status: No, score=0.5 required=8.0 tests=ALL_TRUSTED,BAYES_50, DCC_CHECK_NEGATIVE,T_TM2_M_HEADER_IN_MSG,T_TooManySym_01,XMSubLong autolearn=disabled version=3.4.1 X-Spam-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP * 0.7 XMSubLong Long Subject * 0.0 T_TM2_M_HEADER_IN_MSG BODY: No description available. * 0.8 BAYES_50 BODY: Bayes spam probability is 40 to 60% * [score: 0.5000] * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa08 1397; Body=1 Fuz1=1 Fuz2=1] * 0.0 T_TooManySym_01 4+ unique symbols in subject X-Spam-DCC: XMission; sa08 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ;Oleg Nesterov X-Spam-Relay-Country: X-Spam-Timing: total 276 ms - load_scoreonly_sql: 0.04 (0.0%), signal_user_changed: 4.5 (1.6%), b_tie_ro: 3.1 (1.1%), parse: 1.02 (0.4%), extract_message_metadata: 11 (3.8%), get_uri_detail_list: 2.3 (0.8%), tests_pri_-1000: 4.3 (1.6%), tests_pri_-950: 1.10 (0.4%), tests_pri_-900: 0.89 (0.3%), tests_pri_-400: 24 (8.7%), check_bayes: 23 (8.3%), b_tokenize: 7 (2.4%), b_tok_get_all: 8 (3.0%), b_comp_prob: 1.84 (0.7%), b_tok_touch_all: 3.4 (1.2%), b_finish: 0.81 (0.3%), tests_pri_0: 221 (80.3%), check_dkim_signature: 0.42 (0.2%), check_dkim_adsp: 3.0 (1.1%), tests_pri_500: 4.9 (1.8%), rewrite_mail: 0.00 (0.0%) Subject: [PATCH v5 5/6] fork: Have new threads join on-going signal group stops X-Spam-Flag: No X-SA-Exim-Version: 4.2.1 (built Thu, 05 May 2016 13:38:54 -0600) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There are only two signals that are delivered to every member of a signal group: SIGSTOP and SIGKILL. Signal delivery requires every signal appear to be delivered either before or after a clone syscall. SIGKILL terminates the clone so does not need to be considered. Which leaves only SIGSTOP that needs to be considered when creating new threads. Today in the event of a group stop TIF_SIGPENDING will get set and the fork will restart ensuring the fork syscall participates in the group stop. A fork (especially of a process with a lot of memory) is one of the most expensive system so we really only want to restart a fork when necessary. It is easy so check to see if a SIGSTOP is ongoing and have the new thread join it immediate after the clone completes. Making it appear the clone completed happened just before the SIGSTOP. The calculate_sigpending function will see the bits set in jobctl and set TIF_SIGPENDING to ensure the new task takes the slow path to userspace. V2: The call to task_join_group_stop was moved before the new task is added to the thread group list. This should not matter as sighand->siglock is held over both the addition of the threads, the call to task_join_group_stop and do_signal_stop. But the change is trivial and it is one less thing to worry about when reading the code. Signed-off-by: "Eric W. Biederman" --- include/linux/sched/signal.h | 2 ++ kernel/fork.c | 27 +++++++++++++++------------ kernel/signal.c | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h index b55fd293c1e5..ae2b0b81be25 100644 --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -385,6 +385,8 @@ static inline void ptrace_signal_wake_up(struct task_struct *t, bool resume) signal_wake_up_state(t, resume ? __TASK_TRACED : 0); } +void task_join_group_stop(struct task_struct *task); + #ifdef TIF_RESTORE_SIGMASK /* * Legacy restore_sigmask accessors. These are inefficient on diff --git a/kernel/fork.c b/kernel/fork.c index 22d4cdb9a7ca..ab731e15a600 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1934,18 +1934,20 @@ static __latent_entropy struct task_struct *copy_process( goto bad_fork_cancel_cgroup; } - /* - * Process group and session signals need to be delivered to just the - * parent before the fork or both the parent and the child after the - * fork. Restart if a signal comes in before we add the new process to - * it's process group. - * A fatal signal pending means that current will exit, so the new - * thread can't slip out of an OOM kill (or normal SIGKILL). - */ - recalc_sigpending(); - if (signal_pending(current)) { - retval = -ERESTARTNOINTR; - goto bad_fork_cancel_cgroup; + if (!(clone_flags & CLONE_THREAD)) { + /* + * Process group and session signals need to be delivered to just the + * parent before the fork or both the parent and the child after the + * fork. Restart if a signal comes in before we add the new process to + * it's process group. + * A fatal signal pending means that current will exit, so the new + * thread can't slip out of an OOM kill (or normal SIGKILL). + */ + recalc_sigpending(); + if (signal_pending(current)) { + retval = -ERESTARTNOINTR; + goto bad_fork_cancel_cgroup; + } } @@ -1982,6 +1984,7 @@ static __latent_entropy struct task_struct *copy_process( current->signal->nr_threads++; atomic_inc(¤t->signal->live); atomic_inc(¤t->signal->sigcnt); + task_join_group_stop(p); list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group); list_add_tail_rcu(&p->thread_node, diff --git a/kernel/signal.c b/kernel/signal.c index 1e06f1eba363..9f0eafb6d474 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -373,6 +373,20 @@ static bool task_participate_group_stop(struct task_struct *task) return false; } +void task_join_group_stop(struct task_struct *task) +{ + /* Have the new thread join an on-going signal group stop */ + unsigned long jobctl = current->jobctl; + if (jobctl & JOBCTL_STOP_PENDING) { + struct signal_struct *sig = current->signal; + unsigned long signr = jobctl & JOBCTL_STOP_SIGMASK; + unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME; + if (task_set_jobctl_pending(task, signr | gstop)) { + sig->group_stop_count++; + } + } +} + /* * allocate a new signal queue record * - this may be called without locks if and only if t == current, otherwise an -- 2.17.1