Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp593936ybt; Fri, 19 Jun 2020 08:56:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwxe+boTb90lcz/8V/mXJaAqS4LX6B7DtDhgYNt5on2efJh32/rADPGN7Xwtus9K455kn/r X-Received: by 2002:a17:906:f0c6:: with SMTP id dk6mr4338597ejb.157.1592582213870; Fri, 19 Jun 2020 08:56:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592582213; cv=none; d=google.com; s=arc-20160816; b=bt41Xs0TvmUG5NyEG7i/5teAmn4BW4XTdHwI9IBftrCqRQXgYfzV1U9Syk5hdVD+45 qdORdgduMY48KfTz1gTARRP7Sz1jxl8rNxXDsnDBeXMgLV3CJyD5lXTUUx8RTsqvS6vI OE4tuEMIp3riJjudi4aRb1Mu5s3O8EC27zEcqSZDWXduBut9Z+zwrXLMQNgqS5YSh15X yVtoUp2uti3zs7m1pzcI2BeNl1E/96XSqKtGZPORtQmhbxzaRa/My5nIwmlqPEh36sGm g6LfSKoitqgZIpSTH3gHPk6NeRWapkkqWpTdvJziUiRm/7/NvHm6kk0mT97rKq+78Fx8 HbWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=siu9yJcpOufpkCKT1ZpYE8aBxX0hGgMHDAbhwr9N1qU=; b=FpJUgOG7zptkNBLYUAqhsSCPecLTTOJeL9Xg/Q3x0iU3w0LswXa6wPTUQZVz0ultog WGO4FhjbEYgpBmFbgKpu/K5gZ12ND8WfxUe3SCmy0vRhv6utYWmk1c6uj5nekwudRnj1 zfh6JYqDm4fpnFvfP9VqUpdYiRWQgw/7jRuhOMcmeRO1+sJRwZcgUtx/2yHgLWBlMC+n W+XGaDiOpc+kwkA5Q3pL2Ji3Sk3OyZahVy36ky5S5NuQfE3x/2r5tResXcOdDCt23Si5 l7paBvWOadG5CiAFKwn+vbv3vBq2Bh+UgEzP7///f+6qVBw2I0RHVoF35bA1avv/ov2L NIRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ix0ZVMlc; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id qc28si3942949ejb.292.2020.06.19.08.56.31; Fri, 19 Jun 2020 08:56:53 -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; dkim=pass header.i=@kernel.org header.s=default header.b=ix0ZVMlc; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405411AbgFSPxs (ORCPT + 99 others); Fri, 19 Jun 2020 11:53:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:53670 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2392544AbgFSPWQ (ORCPT ); Fri, 19 Jun 2020 11:22:16 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1EBD221548; Fri, 19 Jun 2020 15:22:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592580135; bh=DqbIC+qFwTwJvEUr9A+FWIEILOijxXRKAeWP716pKv4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ix0ZVMlcZbILK9unuNf+Gk69olYkxYfcP1ox7EEYgr2PvWSurhZwPV3i3p/iOV3Qa VCJxQYHSQ7+c/Bd7G0W0IXpXItAmbjxvQkSZkHbhlHxsmlDZy171QV1WvuClNtBjY/ kmIIhS0+HbIYk6j96y3KI+RMwGjpUvqLSeTRg094= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jann Horn , "Peter Zijlstra (Intel)" , Sasha Levin Subject: [PATCH 5.7 135/376] exit: Move preemption fixup up, move blocking operations down Date: Fri, 19 Jun 2020 16:30:53 +0200 Message-Id: <20200619141716.727303755@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200619141710.350494719@linuxfoundation.org> References: <20200619141710.350494719@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jann Horn [ Upstream commit 586b58cac8b4683eb58a1446fbc399de18974e40 ] With CONFIG_DEBUG_ATOMIC_SLEEP=y and CONFIG_CGROUPS=y, kernel oopses in non-preemptible context look untidy; after the main oops, the kernel prints a "sleeping function called from invalid context" report because exit_signals() -> cgroup_threadgroup_change_begin() -> percpu_down_read() can sleep, and that happens before the preempt_count_set(PREEMPT_ENABLED) fixup. It looks like the same thing applies to profile_task_exit() and kcov_task_exit(). Fix it by moving the preemption fixup up and the calls to profile_task_exit() and kcov_task_exit() down. Fixes: 1dc0fffc48af ("sched/core: Robustify preemption leak checks") Signed-off-by: Jann Horn Signed-off-by: Peter Zijlstra (Intel) Link: https://lkml.kernel.org/r/20200305220657.46800-1-jannh@google.com Signed-off-by: Sasha Levin --- kernel/exit.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/kernel/exit.c b/kernel/exit.c index ce2a75bc0ade..d56fe51bdf07 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -708,8 +708,12 @@ void __noreturn do_exit(long code) struct task_struct *tsk = current; int group_dead; - profile_task_exit(tsk); - kcov_task_exit(tsk); + /* + * We can get here from a kernel oops, sometimes with preemption off. + * Start by checking for critical errors. + * Then fix up important state like USER_DS and preemption. + * Then do everything else. + */ WARN_ON(blk_needs_flush_plug(tsk)); @@ -727,6 +731,16 @@ void __noreturn do_exit(long code) */ set_fs(USER_DS); + if (unlikely(in_atomic())) { + pr_info("note: %s[%d] exited with preempt_count %d\n", + current->comm, task_pid_nr(current), + preempt_count()); + preempt_count_set(PREEMPT_ENABLED); + } + + profile_task_exit(tsk); + kcov_task_exit(tsk); + ptrace_event(PTRACE_EVENT_EXIT, code); validate_creds_for_do_exit(tsk); @@ -744,13 +758,6 @@ void __noreturn do_exit(long code) exit_signals(tsk); /* sets PF_EXITING */ - if (unlikely(in_atomic())) { - pr_info("note: %s[%d] exited with preempt_count %d\n", - current->comm, task_pid_nr(current), - preempt_count()); - preempt_count_set(PREEMPT_ENABLED); - } - /* sync mm's RSS info before statistics gathering */ if (tsk->mm) sync_mm_rss(tsk->mm); -- 2.25.1