Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp998883ybt; Fri, 19 Jun 2020 21:06:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzFKtPa3zCGuE39rD/jpXOJr7i/AqjzLV7TEp7TjKtBiCamue1PTmno1hsjQBUqQQ5qiXTU X-Received: by 2002:a17:907:2162:: with SMTP id rl2mr6073736ejb.365.1592625974000; Fri, 19 Jun 2020 21:06:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592625973; cv=none; d=google.com; s=arc-20160816; b=X8k2ak9yP9ho+XzSgL/hjIoxOv/Gz9enpHgt/s9ELxcDzSogOV1dUhGWoSCMbRc8ec lezTkPF98RIKNiLOjb0IQxT1GPXZ71dUnLs7dykAnN+zpJxaxBBWTPXueArZeY+LyD+6 et9Rtwlusq6/W3PGdbcJXm3bqBm2Qptp9+VYZFMVTkQMaxvcWjOPxAhjnxn4S9ODiUfg eJjn58OnxmtsO3FMUtUmfNvU6+ystzz2r1C34aY0B4PxGcgDnvC9+fP7KurVFmV4TR5e p/fAUbwANppqvQEfQ+AeErN9Rb+p21lfovcEgDXPBeK85oLfn6TxP+PFbegpn/fdnPv+ 0ycw== 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=1j8+X1/mBrQK+sGlkloihmftKtyuqIBRL8qjhR3E+74=; b=XY77bwnd4nevuh/Y5SW5+ymIenCsh7u12bH7gk3Sl8xXCtkWoUc1UzmY3FZZspYSfn 4BhtXZUBeL+OTKegTaT883+vB75VHkPKXXrnBzYjg68RZcFwaNqXUkENBlsQwdxeCkDg nI4SnGgX4aMOWqof/wvwzc+5Ych3kZtO78HQHRDP59mlQKjjN2ZuaG1nDzWRaboMKhLe nV8Nm9P/UmKqWGDQdcaQohYuig6rWZ3X3Q5SRSebuBaQcaTj5jgspcTUiy4BKeBp7zGG o0xFRPX/rZeN4XvQxJtrPayAj+okrWCshEks3eyezNdSN9IpvJiR7B55b8TA2F7Dfm+Y MiNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=unGurp6r; 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 r19si431071edw.168.2020.06.19.21.05.52; Fri, 19 Jun 2020 21:06:13 -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=unGurp6r; 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 S2389029AbgFSQpG (ORCPT + 99 others); Fri, 19 Jun 2020 12:45:06 -0400 Received: from mail.kernel.org ([198.145.29.99]:33300 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387762AbgFSOme (ORCPT ); Fri, 19 Jun 2020 10:42:34 -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 743E021582; Fri, 19 Jun 2020 14:42:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592577754; bh=84hFvqnIv9RWd0djDh3w5bOVOFisJWS+WGdyjLQ/ADg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=unGurp6r1kRIHKOViGARdvEiZu/5l5NNlp9D9gk6Yt6tIoekyQBYWsyBUccL/Elra xNO46SIeogixEbmVfLKSnCQ3GYguiN6w7HE7cYFWyO3QTBTMjPwTXGFkp22P2d2G1I ViNDSRFvBM5pnS2sUDvRBlIN0a3B5zZOmwMikZfA= 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 4.9 075/128] exit: Move preemption fixup up, move blocking operations down Date: Fri, 19 Jun 2020 16:32:49 +0200 Message-Id: <20200619141624.152006079@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200619141620.148019466@linuxfoundation.org> References: <20200619141620.148019466@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 d9394fcd0e2c..27f4168eaeb1 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -739,8 +739,12 @@ void __noreturn do_exit(long code) int group_dead; TASKS_RCU(int tasks_rcu_i); - 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)); @@ -758,6 +762,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); @@ -794,13 +808,6 @@ void __noreturn do_exit(long code) */ raw_spin_unlock_wait(&tsk->pi_lock); - 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