Received: by 10.213.65.68 with SMTP id h4csp1721552imn; Mon, 19 Mar 2018 11:25:26 -0700 (PDT) X-Google-Smtp-Source: AG47ELv2G9ga2RQesTA5P2ChnTc7NGL0Ibr2NXIXnHIqKTpO8A+7ZJcZvssy2a0Ibk7G+wisq9rG X-Received: by 10.101.100.144 with SMTP id e16mr10065372pgv.315.1521483926660; Mon, 19 Mar 2018 11:25:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521483926; cv=none; d=google.com; s=arc-20160816; b=bRb7Sj9tba/tOhUSvzqiWgpncEcfgJ4AmTLTrD7sx8oLPp6Hcv0jvWY8hB+j4HAApP LNAYekxAgo6gPDIqGkCxYRnN73AD28RLXwEJJXV8AmzHULC9FjuuTqgrak5KNOAVwQGR SH69GMc/NglrgQGIHHl39AqD6iHkor5TGuKKi+7BxowbXl6vMVZkH+Q6brKyQ5KBYbqU b5AN79i3f95GUYp+vlz+L3RKhpqfb6T1CXuVpKvWCTmEQ3IGFF6NV/oA/qsp6NCb/gde 7Gw9t6VCw+h5chalcksHzdNMtGnI3tSJfhwCJv72fTDRz2mVklGu8RjkOQOE1U6VN/vr Xtig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=rpuQkzWiu2UWBPvCv8Qi1uESQ3+RzBMwMp/s2bGVthM=; b=cP4cZg3OXG02rn9dBidYS3AddEJvcz5GtVYoF58DhZWGpyVksU8bIaebUIj7Yu4kzN xXj4LcsL5KDNbPpE2VVtfU1RAtI7k6+Ubf4Y5MXoLRZ1BsaswCXlhOoLYC7SRMtOIOCV h8aYx/G+S6AxP+BOGlkYTLhwO2dVPITyM7kaFjFjgL6CORdY+idZVfNU61S99oQnY4FG PaudNRyR9QLdgRZJoTclkiriz2D4Puy8KhLzujWZjo/nKww3oerJFQIQevIcZHHD9DmG 3at/i7t1TPEj6uwsm43NwuFUNDEgSv21aPcPKlirgzLt797eDxZ/r3DoPRhFOcuuijan uD3A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y1-v6si453325pli.586.2018.03.19.11.25.12; Mon, 19 Mar 2018 11:25:26 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1031478AbeCSSXr (ORCPT + 99 others); Mon, 19 Mar 2018 14:23:47 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:49560 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1031459AbeCSSXl (ORCPT ); Mon, 19 Mar 2018 14:23:41 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 8D08FD77; Mon, 19 Mar 2018 18:23:40 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tetsuo Handa , Steven Rostedt , Peter Zijlstra , Andrew Morton , Jiri Slaby , linux-fbdev@vger.kernel.org, Sergey Senozhatsky , Petr Mladek , Sasha Levin Subject: [PATCH 4.9 102/241] printk: Correctly handle preemption in console_unlock() Date: Mon, 19 Mar 2018 19:06:07 +0100 Message-Id: <20180319180755.427526044@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180319180751.172155436@linuxfoundation.org> References: <20180319180751.172155436@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: Petr Mladek [ Upstream commit 257ab443118bffc7fdcef38f49cf59be68a3e362 ] Some console drivers code calls console_conditional_schedule() that looks at @console_may_schedule. The value must be cleared when the drivers are called from console_unlock() with interrupts disabled. But rescheduling is fine when the same code is called, for example, from tty operations where the console semaphore is taken via console_lock(). This is why @console_may_schedule is cleared before calling console drivers. The original value is stored to decide if we could sleep between lines. Now, @console_may_schedule is not cleared when we call console_trylock() and jump back to the "again" goto label. This has become a problem, since the commit 6b97a20d3a7909daa066 ("printk: set may_schedule for some of console_trylock() callers"). @console_may_schedule might get enabled now. There is also the opposite problem. console_lock() can be called only from preemptive context. It can always enable scheduling in the console code. But console_trylock() is not able to detect it when CONFIG_PREEMPT_COUNT is disabled. Therefore we should use the original @console_may_schedule value after re-acquiring the console semaphore in console_unlock(). This patch solves both problems by moving the "again" goto label. Alternative solution was to clear and restore the value around call_console_drivers(). Then console_conditional_schedule() could be used also inside console_unlock(). But there was a potential race with console_flush_on_panic() as reported by Sergey Senozhatsky. That function should be called only where there is only one CPU and with interrupts disabled. But better be on the safe side because stopping CPUs might fail. Fixes: 6b97a20d3a7909 ("printk: set may_schedule for some of console_trylock() callers") Link: http://lkml.kernel.org/r/1490372045-22288-1-git-send-email-pmladek@suse.com Suggested-by: Tetsuo Handa Cc: Steven Rostedt Cc: Peter Zijlstra Cc: Andrew Morton Cc: Greg Kroah-Hartman Cc: Jiri Slaby Cc: linux-fbdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org Reviewed-by: Sergey Senozhatsky Signed-off-by: Petr Mladek Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- kernel/printk/printk.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2342,7 +2342,7 @@ void console_unlock(void) } /* - * Console drivers are called under logbuf_lock, so + * Console drivers are called with interrupts disabled, so * @console_may_schedule should be cleared before; however, we may * end up dumping a lot of lines, for example, if called from * console registration path, and should invoke cond_resched() @@ -2350,11 +2350,15 @@ void console_unlock(void) * scheduling stall on a slow console leading to RCU stall and * softlockup warnings which exacerbate the issue with more * messages practically incapacitating the system. + * + * console_trylock() is not able to detect the preemptive + * context reliably. Therefore the value must be stored before + * and cleared after the the "again" goto label. */ do_cond_resched = console_may_schedule; +again: console_may_schedule = 0; -again: /* * We released the console_sem lock, so we need to recheck if * cpu is online and (if not) is there at least one CON_ANYTIME