Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1889146pxb; Thu, 16 Sep 2021 19:09:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzhSTQv9WFtV+AbPQHHUdAakdjyHycKPaGYlrHl0SVzVu8lImshZ2fxu2A0n8Sxwg7q0DBp X-Received: by 2002:a17:906:b00c:: with SMTP id v12mr9453606ejy.222.1631844571148; Thu, 16 Sep 2021 19:09:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631844571; cv=none; d=google.com; s=arc-20160816; b=tXSHV+AOB7xTBYRxJNt7kSFgJSSkT0fVwk3X2vpoZ6CBNyMq3dJ+p4VV4fxLeL4wh6 zW8Rut0TMO8kZ/Dr4FgHbUhAd5t6qUTUNG7RwKIiEElgFHA2dHTa/q77Gwk2iDZ5LuQ8 NsmzhJHxsHiBaKarOgNNqwdEFEYC13rKqm/eRgIG8+rlTwvSunWZWVg3nuobw4UC48AG w3Vwmo4wfsIrjD046Xdd2OWdbaWbEKR6Cce2mNDwvwXYOF6y5Yu4a6DoTIgmx2jK8liO +1GErhcHHg7wM/6mW+3mcGu7mPN58r5xw6fHMiGZY/yx7a9DAF32AagLnT2uG9gxEadW yrNA== 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=JO7LNOLtt8dx+TSVuZJyecL197tret/PmaY/N2AYQeI=; b=U+DmyCG48ZPqhl1BWAThf0CTEOgDzSETWk+oKAgacxQyeWXQfX6Cchr4k1cf6WNsJw 1gLF83VcTnrgRjzcAvyoTWFKsfqBnKRtxDGP1NtttU1nUanXSMTjvJ3M3dNcy9SmkNCl WWOLqGI7Fve+kVCmBPw+8ahuhqfzoXNBB7yAk2PEUQnjp/Iol7cpgx869in6lORfab8d eZfoUKwdDPT1P99rVQ9YLwgH6XR3zGBc/Jr+bX5T6ifz6NNOfKCal7bOp2r66m9FWlIy 1pHx0FcJQ1pk9Mcddeo0SQwrmSKJRDHC/GiEtj1MU7E34IDsN/Nfs9VRfkHzjfd5VCcI 7YJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="OhfT5/vg"; 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 e2si6167851ejs.346.2021.09.16.19.09.05; Thu, 16 Sep 2021 19:09:31 -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=@linuxfoundation.org header.s=korg header.b="OhfT5/vg"; 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 S1345569AbhIPRLL (ORCPT + 99 others); Thu, 16 Sep 2021 13:11:11 -0400 Received: from mail.kernel.org ([198.145.29.99]:34024 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349180AbhIPRDu (ORCPT ); Thu, 16 Sep 2021 13:03:50 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 83A9461B06; Thu, 16 Sep 2021 16:34:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631810074; bh=6G4IsplKoyeetAmOlTy7tdSl++/n/23XqHH2qpFwkFM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OhfT5/vg9j9Wk2vjhnJsiF+r2V+e+wsYbMAnXeCQOrsrvR+utr6JV65VyNDiIQhmP mOjUmJztAVezyLz48CgmwixhQVhupjBJ6OgZUgj7FAovpB11c1q/6sJO338gMGtypF 2fem7LDfnPW5qk5hFOzfKln6qYupyHVP7Ziu2H0o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Petr Mladek , Sergey Senozhatsky , John Ogness , kernel test robot Subject: [PATCH 5.13 348/380] printk/console: Check consistent sequence number when handling race in console_unlock() Date: Thu, 16 Sep 2021 18:01:45 +0200 Message-Id: <20210916155815.886427268@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916155803.966362085@linuxfoundation.org> References: <20210916155803.966362085@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: Petr Mladek commit 11e4b63abbe23872b45f325a7c6c8b7f9ff42cad upstream. The standard printk() tries to flush the message to the console immediately. It tries to take the console lock. If the lock is already taken then the current owner is responsible for flushing even the new message. There is a small race window between checking whether a new message is available and releasing the console lock. It is solved by re-checking the state after releasing the console lock. If the check is positive then console_unlock() tries to take the lock again and process the new message as well. The commit 996e966640ddea7b535c ("printk: remove logbuf_lock") causes that console_seq is not longer read atomically. As a result, the re-check might be done with an inconsistent 64-bit index. Solve it by using the last sequence number that has been checked under the console lock. In the worst case, it will take the lock again only to realized that the new message has already been proceed. But it was possible even before. The variable next_seq is marked as __maybe_unused to call down compiler warning when CONFIG_PRINTK is not defined. Fixes: commit 996e966640ddea7b535c ("printk: remove logbuf_lock") Reported-by: kernel test robot # unused next_seq warning Cc: stable@vger.kernel.org # 5.13 Signed-off-by: Petr Mladek Acked-by: Sergey Senozhatsky Reviewed-by: John Ogness Link: https://lore.kernel.org/r/20210702150657.26760-1-pmladek@suse.com Signed-off-by: Greg Kroah-Hartman --- kernel/printk/printk.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2545,6 +2545,7 @@ void console_unlock(void) bool do_cond_resched, retry; struct printk_info info; struct printk_record r; + u64 __maybe_unused next_seq; if (console_suspended) { up_console_sem(); @@ -2654,8 +2655,10 @@ skip: cond_resched(); } - console_locked = 0; + /* Get consistent value of the next-to-be-used sequence number. */ + next_seq = console_seq; + console_locked = 0; up_console_sem(); /* @@ -2664,7 +2667,7 @@ skip: * there's a new owner and the console_unlock() from them will do the * flush, no worries. */ - retry = prb_read_valid(prb, console_seq, NULL); + retry = prb_read_valid(prb, next_seq, NULL); printk_safe_exit_irqrestore(flags); if (retry && console_trylock())