Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp611757pxb; Wed, 3 Mar 2021 10:43:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJwwwoyOmJek+UAvpJEm/fowC59WZfDGbW5w4kENka65ATFLOWs1xz/kAM4+9xL5t4jJxoqh X-Received: by 2002:a17:907:75c7:: with SMTP id jl7mr199917ejc.191.1614796980156; Wed, 03 Mar 2021 10:43:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614796980; cv=none; d=google.com; s=arc-20160816; b=SMXoBbpWxF8JwmnwW4DH52/6FTqrVP+wm0v0OqAM6n8maJGwWea3wLw2QGqj3NveYB GSIRAKjek0Fd4i685rELDLqWNjOBiKl+0ZvG1LiOQn/k6NKHk2yukos1vcKbudQ5YkZj kAie0Ezd7Dt4kC2wlzaK1u7S6ErBVFbSg7rVtZKjs7SjmEyqb9l1PFvjarCd+pgrbeI0 TGMGjmlJJKPNQJmAbhCV7rlrYWD+nI6U/lPaetiWf0QifRdYpkueEycA5y8bEhpzRLN/ j8WLArZim516X/Qf+yfmysTBcsSXUrCURyNJrqUTXQxLsro+M0My37zX76MyD608cMC2 qomA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Z2OeR0kk8i5HU4TQvHyfBdaSuwIMxUoYaASJBts7ZBE=; b=ytpMFXzwJeqihL4jwaDaBzURtWiqL6Cb7MX9KxVxZGMkE6zl7pkOiLuNyCNuJMAS6Z nS5woo0CtLiQOzj71IVMGO/Zb1T1KP9y4+V/8f636UhqS+Y4A+O2gTcZtGD3y38JxU2P 6l5foZYmIx8hgrFxUvVmPkyGu7f8DneK40AXaMc82swaN0nS+JN9+Z2JA+QcWDIXz5F5 jDEBnISLpEsgCY774HsqtdWU6C87rzPRrNhRbOaLlGKVB98tOGtGyCfMpEkoAtektTLX SfTxmqJedEg+DJQuIA49g8c1mk/iUISw1zAo7qannLlv+k3quo21g36+SWxdb/98nPQt Qr+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=sMfK4OXV; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d9si15785349edt.564.2021.03.03.10.42.28; Wed, 03 Mar 2021 10:43:00 -0800 (PST) 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=@linuxfoundation.org header.s=korg header.b=sMfK4OXV; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378349AbhCBBFg (ORCPT + 99 others); Mon, 1 Mar 2021 20:05:36 -0500 Received: from mail.kernel.org ([198.145.29.99]:34124 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235033AbhCAS7J (ORCPT ); Mon, 1 Mar 2021 13:59:09 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 546A964FA5; Mon, 1 Mar 2021 17:31:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614619891; bh=clNgsSlW6BodqZSmbhyAxuKEX+FLcS59BXvNc8EeZLU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sMfK4OXVuT1nLCOUqQ8veVM3iOyD/gf0RkXJs8tnkwlaFA6hS2fQI66p+lkb3hBk4 TWB5VBzgsZRU9GWPxRX2HED3OPYFIamK6FtUz6XsvzSid1Imv5yY/4SronOJnrt6Q+ eLvCztsgLrTjxtaaoqqHY96LnTINFTbPO/OslCOE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Muchun Song , Petr Mladek , Sergey Senozhatsky Subject: [PATCH 5.10 617/663] printk: fix deadlock when kernel panic Date: Mon, 1 Mar 2021 17:14:25 +0100 Message-Id: <20210301161212.371954534@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210301161141.760350206@linuxfoundation.org> References: <20210301161141.760350206@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Muchun Song commit 8a8109f303e25a27f92c1d8edd67d7cbbc60a4eb upstream. printk_safe_flush_on_panic() caused the following deadlock on our server: CPU0: CPU1: panic rcu_dump_cpu_stacks kdump_nmi_shootdown_cpus nmi_trigger_cpumask_backtrace register_nmi_handler(crash_nmi_callback) printk_safe_flush __printk_safe_flush raw_spin_lock_irqsave(&read_lock) // send NMI to other processors apic_send_IPI_allbutself(NMI_VECTOR) // NMI interrupt, dead loop crash_nmi_callback printk_safe_flush_on_panic printk_safe_flush __printk_safe_flush // deadlock raw_spin_lock_irqsave(&read_lock) DEADLOCK: read_lock is taken on CPU1 and will never get released. It happens when panic() stops a CPU by NMI while it has been in the middle of printk_safe_flush(). Handle the lock the same way as logbuf_lock. The printk_safe buffers are flushed only when both locks can be safely taken. It can avoid the deadlock _in this particular case_ at expense of losing contents of printk_safe buffers. Note: It would actually be safe to re-init the locks when all CPUs were stopped by NMI. But it would require passing this information from arch-specific code. It is not worth the complexity. Especially because logbuf_lock and printk_safe buffers have been obsoleted by the lockless ring buffer. Fixes: cf9b1106c81c ("printk/nmi: flush NMI messages on the system panic") Signed-off-by: Muchun Song Reviewed-by: Petr Mladek Cc: Acked-by: Sergey Senozhatsky Signed-off-by: Petr Mladek Link: https://lore.kernel.org/r/20210210034823.64867-1-songmuchun@bytedance.com Signed-off-by: Greg Kroah-Hartman --- kernel/printk/printk_safe.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) --- a/kernel/printk/printk_safe.c +++ b/kernel/printk/printk_safe.c @@ -45,6 +45,8 @@ struct printk_safe_seq_buf { static DEFINE_PER_CPU(struct printk_safe_seq_buf, safe_print_seq); static DEFINE_PER_CPU(int, printk_context); +static DEFINE_RAW_SPINLOCK(safe_read_lock); + #ifdef CONFIG_PRINTK_NMI static DEFINE_PER_CPU(struct printk_safe_seq_buf, nmi_print_seq); #endif @@ -180,8 +182,6 @@ static void report_message_lost(struct p */ static void __printk_safe_flush(struct irq_work *work) { - static raw_spinlock_t read_lock = - __RAW_SPIN_LOCK_INITIALIZER(read_lock); struct printk_safe_seq_buf *s = container_of(work, struct printk_safe_seq_buf, work); unsigned long flags; @@ -195,7 +195,7 @@ static void __printk_safe_flush(struct i * different CPUs. This is especially important when printing * a backtrace. */ - raw_spin_lock_irqsave(&read_lock, flags); + raw_spin_lock_irqsave(&safe_read_lock, flags); i = 0; more: @@ -232,7 +232,7 @@ more: out: report_message_lost(s); - raw_spin_unlock_irqrestore(&read_lock, flags); + raw_spin_unlock_irqrestore(&safe_read_lock, flags); } /** @@ -278,6 +278,14 @@ void printk_safe_flush_on_panic(void) raw_spin_lock_init(&logbuf_lock); } + if (raw_spin_is_locked(&safe_read_lock)) { + if (num_online_cpus() > 1) + return; + + debug_locks_off(); + raw_spin_lock_init(&safe_read_lock); + } + printk_safe_flush(); }