Received: by 10.223.185.116 with SMTP id b49csp1108453wrg; Sat, 3 Mar 2018 15:49:06 -0800 (PST) X-Google-Smtp-Source: AG47ELuwnu1xD4aomnErELlZVJpKq7k1PZ6l7IYYEpWDURXP4QSD3PJ/yx5hIzIJMGlc0eXE1HsW X-Received: by 2002:a17:902:7045:: with SMTP id h5-v6mr9064425plt.217.1520120945978; Sat, 03 Mar 2018 15:49:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520120945; cv=none; d=google.com; s=arc-20160816; b=w8l1m8CBWxMFHVdWTSgvv06lNN1hsVRjCwJluk7mWsrcM7e0hMRFTPw9JZVDxqrgic 3XDi+iHxKDHvZ1T94iM4yX6n9pYPowa7ZXBXxHPutcJNAC45LRfeLdGUdhhU3oejLx/v lqGqwxnwdw/ONDfFbB+3jDcV62chaWniY1eCs+Rfp+IbVmgd6dNNCetQu1KG8dwdysXW wpjH7MasZtNhUPM+wcLv2QX2QYrSxgBjho0cpDCbS0IbBWc32ZJMikPEf7qiyS9ZR+NZ UOwA9oKUc3/8L6xXi7Z1x2ejczZcT+3cMq8e/yRG2qSnvVCgczULT0FARrPK4ZVOSmqr +S6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=RvI0LqMGmtM7PSTNY9XdCoIw+Gw3UrSHg2UrYOP1D7c=; b=qW6sxVrt415euWLz+41jotiLDDtKILFjAPU/0Ly3J5CVm7xitD9umhvXZXWpWUhoQB tsAa1ZmGqS+C3JMOSi4jRBQ3KYVTyZQ854AKzArRx/vxBhT6qyl620YrUZgD9UH8U+3b 3vBmp4AgwprIWJidxA2GMphCQ45Qb85h9PUFJ2epsJKHTDZzoG84cmxgD3bc4n0empmV 1Q/B0apoigLUTL0y6X743hzOf7bgpkJN3+Eu2ObGCl7Wv8To3NMTkPSt6mmMa4wZR35u HIw2Dft13dPLi70Q9/Kt0zl8bWK/LDgEuZdzulyZRmR5ZFKrWBW2HB57ovgOr7rI7BEm UZtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=OXDCeAQ/; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 2-v6si6971460plf.84.2018.03.03.15.48.52; Sat, 03 Mar 2018 15:49:05 -0800 (PST) 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; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=OXDCeAQ/; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752465AbeCCXrK (ORCPT + 99 others); Sat, 3 Mar 2018 18:47:10 -0500 Received: from mail-bn3nam01on0104.outbound.protection.outlook.com ([104.47.33.104]:36491 "EHLO NAM01-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S934135AbeCCWeG (ORCPT ); Sat, 3 Mar 2018 17:34:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=RvI0LqMGmtM7PSTNY9XdCoIw+Gw3UrSHg2UrYOP1D7c=; b=OXDCeAQ/fw/noFx+p6+bcnQRv3VMvz0TO/xLTcNou3IT2r+kzazsI23iDvWA4NnAF5GOiF87zGEP1zlLRjR20F/n1Vv/z6x1MoLzCFs25NnIaf7SJpQ/L7YzY9QSEchJ1uvgCkf4G6uUubOwX8GuWwNZ4nkPYeVVF2rEfPfFboI= Received: from MW2PR2101MB1034.namprd21.prod.outlook.com (52.132.149.10) by MW2PR2101MB1065.namprd21.prod.outlook.com (52.132.149.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.567.2; Sat, 3 Mar 2018 22:33:58 +0000 Received: from MW2PR2101MB1034.namprd21.prod.outlook.com ([fe80::1d56:338f:e2b:cec0]) by MW2PR2101MB1034.namprd21.prod.outlook.com ([fe80::1d56:338f:e2b:cec0%3]) with mapi id 15.20.0567.006; Sat, 3 Mar 2018 22:33:58 +0000 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Petr Mladek , Steven Rostedt , Peter Zijlstra , Andrew Morton , Greg Kroah-Hartman , Jiri Slaby , "linux-fbdev@vger.kernel.org" , Sasha Levin Subject: [PATCH AUTOSEL for 4.9 105/219] printk: Correctly handle preemption in console_unlock() Thread-Topic: [PATCH AUTOSEL for 4.9 105/219] printk: Correctly handle preemption in console_unlock() Thread-Index: AQHTsz8EqAqQ68089kK2JQv6+DuQSg== Date: Sat, 3 Mar 2018 22:28:56 +0000 Message-ID: <20180303222716.26640-105-alexander.levin@microsoft.com> References: <20180303222716.26640-1-alexander.levin@microsoft.com> In-Reply-To: <20180303222716.26640-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;MW2PR2101MB1065;7:nqYQX2vRYDNTXC9//IWD6Qv9JELsVwamM20gcWCT3EbfkeP/UOisrgrtqSvxawHPO7/Hyi0na8Y8pGxBDHRY0s6MI1gngphyjTuSnd6Hng1gkYcuJI4RDA3OLfy0BG7p6EPoBUZlNJOhFzZzZ0tjnICcNeSB04JRzNTykD6oliF+uB6kL3vyNS+50lwXO7m12c8tPG8Nnlq1utDWNscB3g9v+EOI628p7uUjEB1LkPc+AjZ6+smnH/ggcqMwwE5C x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: b0e4ad06-eb37-41a4-144a-08d58156db1d x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(4534165)(4627221)(201703031133081)(201702281549075)(48565401081)(5600026)(4604075)(3008032)(2017052603307)(7193020);SRVR:MW2PR2101MB1065; x-ms-traffictypediagnostic: MW2PR2101MB1065: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(274141268393343)(89211679590171)(209352067349851)(9452136761055)(85827821059158)(42068640409301)(17755550239193); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(61425038)(6040501)(2401047)(5005006)(8121501046)(3002001)(10201501046)(93006095)(93001095)(3231220)(944501244)(52105095)(6055026)(61426038)(61427038)(6041288)(20161123558120)(20161123564045)(20161123562045)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011);SRVR:MW2PR2101MB1065;BCL:0;PCL:0;RULEID:;SRVR:MW2PR2101MB1065; x-forefront-prvs: 0600F93FE1 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(366004)(346002)(376002)(396003)(39380400002)(39860400002)(189003)(199004)(6506007)(22452003)(54906003)(25786009)(186003)(2950100002)(102836004)(5250100002)(2501003)(110136005)(97736004)(316002)(2900100001)(36756003)(76176011)(6436002)(59450400001)(6486002)(26005)(4326008)(99286004)(68736007)(6512007)(6116002)(10090500001)(6306002)(106356001)(105586002)(478600001)(66066001)(107886003)(3660700001)(966005)(305945005)(86612001)(14454004)(81166006)(7736002)(86362001)(1076002)(72206003)(2906002)(10290500003)(8676002)(8936002)(81156014)(3846002)(5660300001)(3280700002)(53936002)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:MW2PR2101MB1065;H:MW2PR2101MB1034.namprd21.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: 1UzRtVyRMBwz51YUjOLk3lIeuOiSpfxCxXu0clXnPb09TqIy2c3SkgF/mNEuQu9Ix+KbSTsu0UYLku0UhC8iI4k9zNEIckkxb43y//dAeYu/cwozVZuuKI3QRn75j83sgWReUxchBnMeND5aKxjkT0EHjreGness2RheL7ywjBQ= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: b0e4ad06-eb37-41a4-144a-08d58156db1d X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Mar 2018 22:28:56.9007 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW2PR2101MB1065 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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_tryloc= k() callers") Link: http://lkml.kernel.org/r/1490372045-22288-1-git-send-email-pmladek@su= se.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 --- kernel/printk/printk.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 9c5b231684d0..ab6855a4218b 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2342,7 +2342,7 @@ void console_unlock(void) } =20 /* - * 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 =3D console_may_schedule; +again: console_may_schedule =3D 0; =20 -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 --=20 2.14.1