Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp54803ybg; Mon, 8 Jun 2020 16:15:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzTnynNQcM30l2YdMadGLV9ItxYYuIT5mQKmhNt6G8S3i28X0bxeAajZar5sPnNFOo7zZmq X-Received: by 2002:aa7:d5c7:: with SMTP id d7mr24603408eds.11.1591658141021; Mon, 08 Jun 2020 16:15:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591658141; cv=none; d=google.com; s=arc-20160816; b=u9g7WU2kdPo5L7BLDisMTK5ihyqLQ2TVYeswIxFDYsVEeoJ3uHHE1f+dVCO9Clx6Ih eEZI+qHzJdiA7Mln7tlZxjiaD2jWa4YWE+mHCWe6Eh3/42l0fWRSQMSm/iNQWKutS5VJ vnA77pK64MMAjoxRaDIDXUok58yyxtuai0lTsvmxO1e5fRXdg7Bazm1UGFQVwDjPk5RX erhmSQ+qVJPB6K+CyUspJW5y3OnNJ6IHYxWJlIf1JjtAiuUfBA+y9BZhYOJRnIrkAsxg NmG9fZFg92AF1Sd9rWhKMrVrCBB0QUUVF4FtN87HGe9OboetRPT30v9/68DRcCZ2dXVD jKnA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=siu9yJcpOufpkCKT1ZpYE8aBxX0hGgMHDAbhwr9N1qU=; b=wR0VkDXpr/B7hJ2RJVOsrjlVBXEaNbghjg3D8rfTEcR+mNLjfbBRSNIiwhHP7nPmri /jRyChPYpqj5PtoHwXeXjrZCi23DvZ79o7KBNrbsGSD9ohD4P605oZgK5Tphy/PYSG1S rkJhO1pcI/+UXhuxFqyHOTzCQa/nMUcMhVF2ZEOosRcdYcnGIGU6lOIfinhiL3zn6YWg 1ydXTEECwxpRoIXSr7afqFCfDGda8gOjrp6oWbdULytuje5dc3mRo5JZ4aVkRor4TZ4Y WjceebjUM5VhJ4ZAYNz/4MtCsjrp+7zJ+MTxy7vp9ptWnMaLZ5X8nTTdlyTlRMBHQ2rN nxZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gqNNAwg6; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b23si10387203edj.441.2020.06.08.16.15.17; Mon, 08 Jun 2020 16:15:41 -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=gqNNAwg6; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728689AbgFHXKc (ORCPT + 99 others); Mon, 8 Jun 2020 19:10:32 -0400 Received: from mail.kernel.org ([198.145.29.99]:55022 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728382AbgFHXJT (ORCPT ); Mon, 8 Jun 2020 19:09:19 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2146820897; Mon, 8 Jun 2020 23:09:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1591657758; bh=DqbIC+qFwTwJvEUr9A+FWIEILOijxXRKAeWP716pKv4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gqNNAwg67f73jhwcPxFySDvMt7RKb86/3U1G8rsjstP3uwLeazR9yigLkF9OYfh9O 2yhS5pGvpHW4qPzleZFZyTx95RuWvhgZedHPy0TBE6HogIVPvjQ6W4Wj2wR+M8pQOs d9W8Yrs+EDtdwyQ2kS5/L+cm8Ub7Re6riBMSGG5s= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Jann Horn , Peter Zijlstra , Sasha Levin Subject: [PATCH AUTOSEL 5.7 144/274] exit: Move preemption fixup up, move blocking operations down Date: Mon, 8 Jun 2020 19:03:57 -0400 Message-Id: <20200608230607.3361041-144-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200608230607.3361041-1-sashal@kernel.org> References: <20200608230607.3361041-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore 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