Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp473627pxv; Thu, 24 Jun 2021 12:05:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzjHODo6lqwmu7x3BhCsQUbgeO+d8np8ITt+gBw1UTzs7p+DDEtT4XfAR8dz9plHsINoz5B X-Received: by 2002:a17:907:9711:: with SMTP id jg17mr6870536ejc.106.1624561549850; Thu, 24 Jun 2021 12:05:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624561549; cv=none; d=google.com; s=arc-20160816; b=pcYlYcwHqO/4emCHiFNA6HC1HZF9WiPDUM4x00WJqfQL1z0cZb9AN2pjGUjzloTvn8 6aacP32aPNquDtRo3+n9IBp2/m+PZ5U5RdUi7e/G6d8YbrH9vPMCh2aCR8AzpVpj7cXb Xk+YjPDd0/P80xMj8pa4VW2FlTFZxTt1N2UILl6jbjigf8gRmPcFFSb5KeXwmKO+prlA cKBvO9u8IeGeP2kA7NxN05s3PFsXAoPyl9iVFwcYAsOCsq6e9VpABVneXlpVCI3b1wYo na8zQ+HJxnN0VQDdqB1HI6rZVMIN1zSVAQE5fpLZ+2eMQk0dqasomQWJkSTmhocJPKur UG4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:subject:mime-version:user-agent:message-id :in-reply-to:date:references:cc:to:from; bh=k/WwKlcNuyw7pooXh4xLXox0pTEouPY6rO9dIACuP1s=; b=cSX9FtxWPcUavqVrgBs4Wa5NcgSvpORXcDgCYqgdKyooRak+kmPDz7mIfh1+xebvyv YXAzafbxo+5Bjnb3qoctEA28+MWFwdh3OiWC8oAaSyFoKSYwc384VmE0o5t+gxLnNGvm iHIl6vJwxDrx2VPNY3QKNng6P+w4yJfQScCzVKk5jburkLYKTTt3pj+InD7+DLeh98lo 5UbudfM0OiOgXj1yp5ZsoDk719wUKjq+8vRvZnMfU+ZtuET2Pp/P57W7jysTTPJGmg1K gA/lGYcxjeizBVLDZbbUVdgjfkqLSmNLqGTAspiW5MT1ethBbSmdYWmrZLqCmHzcJUq1 F8YA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s24si3849320ejs.346.2021.06.24.12.05.25; Thu, 24 Jun 2021 12:05:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232598AbhFXTFV (ORCPT + 99 others); Thu, 24 Jun 2021 15:05:21 -0400 Received: from out03.mta.xmission.com ([166.70.13.233]:59868 "EHLO out03.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232537AbhFXTFU (ORCPT ); Thu, 24 Jun 2021 15:05:20 -0400 Received: from in02.mta.xmission.com ([166.70.13.52]:33542) by out03.mta.xmission.com with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1lwUct-008eU2-2h; Thu, 24 Jun 2021 13:02:59 -0600 Received: from ip68-227-160-95.om.om.cox.net ([68.227.160.95]:47212 helo=email.xmission.com) by in02.mta.xmission.com with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1lwUcr-003HZQ-M6; Thu, 24 Jun 2021 13:02:58 -0600 From: ebiederm@xmission.com (Eric W. Biederman) To: Linus Torvalds Cc: Al Viro , Michael Schmitz , linux-arch , Jens Axboe , Oleg Nesterov , Linux Kernel Mailing List , Richard Henderson , Ivan Kokshaysky , Matt Turner , alpha , Geert Uytterhoeven , linux-m68k , Arnd Bergmann , Ley Foon Tan , Tejun Heo , Kees Cook References: <87sg1lwhvm.fsf@disp2133> <6e47eff8-d0a4-8390-1222-e975bfbf3a65@gmail.com> <924ec53c-2fd9-2e1c-bbb1-3fda49809be4@gmail.com> <87eed4v2dc.fsf@disp2133> <5929e116-fa61-b211-342a-c706dcb834ca@gmail.com> <87fsxjorgs.fsf@disp2133> <87a6njf0ia.fsf@disp2133> <87tulpbp19.fsf@disp2133> <87zgvgabw1.fsf@disp2133> <875yy3850g.fsf_-_@disp2133> Date: Thu, 24 Jun 2021 14:02:50 -0500 In-Reply-To: <875yy3850g.fsf_-_@disp2133> (Eric W. Biederman's message of "Thu, 24 Jun 2021 13:57:35 -0500") Message-ID: <87y2az5bmt.fsf_-_@disp2133> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-XM-SPF: eid=1lwUcr-003HZQ-M6;;;mid=<87y2az5bmt.fsf_-_@disp2133>;;;hst=in02.mta.xmission.com;;;ip=68.227.160.95;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX1/lXuwnEpH9d/WqY5Bt5fTe321yxl0TTdU= X-SA-Exim-Connect-IP: 68.227.160.95 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on sa01.xmission.com X-Spam-Level: ** X-Spam-Status: No, score=2.0 required=8.0 tests=ALL_TRUSTED,BAYES_50, DCC_CHECK_NEGATIVE,T_TooManySym_01,XMNoVowels,XMSubLong autolearn=disabled version=3.4.2 X-Spam-Virus: No X-Spam-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP * 0.8 BAYES_50 BODY: Bayes spam probability is 40 to 60% * [score: 0.5000] * 0.7 XMSubLong Long Subject * 1.5 XMNoVowels Alpha-numberic number with no vowels * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa01 1397; Body=1 Fuz1=1 Fuz2=1] * 0.0 T_TooManySym_01 4+ unique symbols in subject X-Spam-DCC: XMission; sa01 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: **;Linus Torvalds X-Spam-Relay-Country: X-Spam-Timing: total 815 ms - load_scoreonly_sql: 0.02 (0.0%), signal_user_changed: 3.7 (0.5%), b_tie_ro: 2.6 (0.3%), parse: 0.83 (0.1%), extract_message_metadata: 11 (1.3%), get_uri_detail_list: 2.5 (0.3%), tests_pri_-1000: 12 (1.4%), tests_pri_-950: 1.08 (0.1%), tests_pri_-900: 0.87 (0.1%), tests_pri_-90: 378 (46.3%), check_bayes: 376 (46.2%), b_tokenize: 10 (1.2%), b_tok_get_all: 239 (29.3%), b_comp_prob: 1.93 (0.2%), b_tok_touch_all: 122 (15.0%), b_finish: 0.79 (0.1%), tests_pri_0: 399 (48.9%), check_dkim_signature: 0.46 (0.1%), check_dkim_adsp: 1.95 (0.2%), poll_dns_idle: 0.63 (0.1%), tests_pri_10: 1.83 (0.2%), tests_pri_500: 6 (0.7%), rewrite_mail: 0.00 (0.0%) Subject: [PATCH 7/9] signal: Make individual tasks exiting a first class concept. X-SA-Exim-Version: 4.2.1 (built Sat, 08 Feb 2020 21:53:50 +0000) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Implement start_task_exit_locked and rewrite the de_thread logic in exec using it. Calling start_task_exit_locked is equivalent to asyncrhonously calling exit(2) aka pthread_exit on a task. Signed-off-by: "Eric W. Biederman" --- fs/exec.c | 10 +++++++++- include/linux/sched/jobctl.h | 2 ++ include/linux/sched/signal.h | 1 + kernel/signal.c | 37 ++++++++++++++++-------------------- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 18594f11c31f..b6f50213f0a0 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1040,6 +1040,7 @@ static int de_thread(struct task_struct *tsk) struct signal_struct *sig = tsk->signal; struct sighand_struct *oldsighand = tsk->sighand; spinlock_t *lock = &oldsighand->siglock; + struct task_struct *t; if (thread_group_empty(tsk)) goto no_thread_group; @@ -1058,7 +1059,14 @@ static int de_thread(struct task_struct *tsk) } sig->group_exit_task = tsk; - sig->notify_count = zap_other_threads(tsk); + sig->group_stop_count = 0; + sig->notify_count = 0; + __for_each_thread(sig, t) { + if (t == tsk) + continue; + sig->notify_count++; + start_task_exit_locked(t, SIGKILL); + } if (!thread_group_leader(tsk)) sig->notify_count--; diff --git a/include/linux/sched/jobctl.h b/include/linux/sched/jobctl.h index fa067de9f1a9..e94833b0c819 100644 --- a/include/linux/sched/jobctl.h +++ b/include/linux/sched/jobctl.h @@ -19,6 +19,7 @@ struct task_struct; #define JOBCTL_TRAPPING_BIT 21 /* switching to TRACED */ #define JOBCTL_LISTENING_BIT 22 /* ptracer is listening for events */ #define JOBCTL_TRAP_FREEZE_BIT 23 /* trap for cgroup freezer */ +#define JOBCTL_TASK_EXITING_BIT 31 /* the task is exiting */ #define JOBCTL_STOP_DEQUEUED (1UL << JOBCTL_STOP_DEQUEUED_BIT) #define JOBCTL_STOP_PENDING (1UL << JOBCTL_STOP_PENDING_BIT) @@ -28,6 +29,7 @@ struct task_struct; #define JOBCTL_TRAPPING (1UL << JOBCTL_TRAPPING_BIT) #define JOBCTL_LISTENING (1UL << JOBCTL_LISTENING_BIT) #define JOBCTL_TRAP_FREEZE (1UL << JOBCTL_TRAP_FREEZE_BIT) +#define JOBCTL_TASK_EXITING (1UL << JOBCTL_TASK_EXITING_BIT) #define JOBCTL_TRAP_MASK (JOBCTL_TRAP_STOP | JOBCTL_TRAP_NOTIFY) #define JOBCTL_PENDING_MASK (JOBCTL_STOP_PENDING | JOBCTL_TRAP_MASK) diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h index c007e55cb119..a958381ba4a9 100644 --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -429,6 +429,7 @@ static inline void ptrace_signal_wake_up(struct task_struct *t, bool resume) } void start_group_exit(int exit_code); +void start_task_exit_locked(struct task_struct *task, int exit_code); void task_join_group_stop(struct task_struct *task); diff --git a/kernel/signal.c b/kernel/signal.c index 95a076af600a..afbc001220dd 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -264,6 +264,12 @@ static inline void print_dropped_signal(int sig) current->comm, current->pid, sig); } +static void task_set_jobctl_exiting(struct task_struct *task, int exit_code) +{ + WARN_ON_ONCE(task->jobctl & ~JOBCTL_STOP_SIGMASK); + task->jobctl = JOBCTL_TASK_EXITING | (exit_code & JOBCTL_STOP_SIGMASK); +} + /** * task_set_jobctl_pending - set jobctl pending bits * @task: target task @@ -1407,28 +1413,15 @@ int force_sig_info(struct kernel_siginfo *info) return force_sig_info_to_task(info, current, false); } -/* - * Nuke all other threads in the group. - */ -int zap_other_threads(struct task_struct *p) +void start_task_exit_locked(struct task_struct *task, int exit_code) { - struct task_struct *t = p; - int count = 0; - - p->signal->group_stop_count = 0; - - while_each_thread(p, t) { - task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK); - count++; - - /* Don't bother with already dead threads */ - if (t->exit_state) - continue; - sigaddset(&t->pending.signal, SIGKILL); - signal_wake_up(t, 1); + task_clear_jobctl_pending(task, JOBCTL_PENDING_MASK); + /* Only bother with threads that might be alive */ + if (!task->exit_state) { + task_set_jobctl_exiting(task, exit_code); + sigaddset(&task->pending.signal, SIGKILL); + signal_wake_up(task, 1); } - - return count; } struct sighand_struct *__lock_task_sighand(struct task_struct *tsk, @@ -2714,7 +2707,7 @@ bool get_signal(struct ksignal *ksig) } /* Has this task already been marked for death? */ - if (signal_group_exit(signal)) { + if (signal_group_exit(signal) || (current->jobctl & JOBCTL_TASK_EXITING)) { ksig->info.si_signo = signr = SIGKILL; sigdelset(¤t->pending.signal, SIGKILL); trace_signal_deliver(SIGKILL, SEND_SIG_NOINFO, @@ -2874,6 +2867,8 @@ bool get_signal(struct ksignal *ksig) if (signal_group_exit(signal)) { /* Another thread got here before we took the lock. */ exit_code = signal->group_exit_code; + } else if (current->jobctl & JOBCTL_TASK_EXITING) { + exit_code = current->jobctl & JOBCTL_STOP_SIGMASK; } else { start_group_exit_locked(signal, exit_code); } -- 2.20.1