Received: by 2002:a17:90b:8d0:0:0:0:0 with SMTP id ds16csp4879820pjb; Mon, 27 Jul 2020 07:28:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwRhajz0DVJBwThqpIcrb6cOooaR2p+HY9RKXN18UFRtF4mcSgUfo6H+ssw6Xp3Xb2VRoDX X-Received: by 2002:a05:6402:176e:: with SMTP id da14mr21914423edb.262.1595860086088; Mon, 27 Jul 2020 07:28:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595860086; cv=none; d=google.com; s=arc-20160816; b=P0NowFaDd1ZpefvqLSoiMY+cxk+EuaiskiAOykFVtokx44ZjHcnrwzogiHUwpiLuSA ymkTq4FOb7hqCo0+/WkkMDfNuPrmTcvW9Sv1wHbGtCsAWhXRNFPQPqR8/2h4aOGaDh1k D6aaJITUvumNCUbIPr9OBn7QZBvyPibcI81qk9Htvml4Ffkv6HqSDD7l6QHhZKQgy1ks SyUeJzAXs09YaxHsjqOeMvi9CjZUcjTp0S8IJ1b9rDYEaZTAiLCNRZqyaajDAfWePKbE 8L2FLXkiJcH+KjkL5Mmzajs0qTzisnNc92lsWi+fewmMJCysxwEC2DnysaOorsHyzO1F ttCA== 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=382Mwz2u+z83hrA3kr66CLEF+iO1ZY2CmmM0h0yDrdM=; b=usz7kw9EhwVEXzAHJ7M79ZE4HXtTDUgVaTuK2E4QLWxulcFrxOsKO9tdHbg+KPrtTr mBMCxTXuI3xzwHHZBF5/dPGKvqVwu1hV3oOJ2EWIon13bQZB1qXuZApJydAscUGNZKwX yFLnJOj1Qn0mB4sXCS6UqiFk4UjaU/TSsMjQlELQChXCJwQKt/54QYO/sJETn+SmHX9t nK3X78IEe+9nCtyZ8eC4IndYC/3R6gIjQtlPAlXpnSOWXTURcfZs5o2f0wC3UFehmwO1 q/en7YURc45YIBK99YAt0bs5UDNld6CRDupq4oDFy9CXIT/zcv7rRRS6DWqC6Y13s4Zw Wnbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=oWJjs17r; 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 l9si6132497ejq.427.2020.07.27.07.27.43; Mon, 27 Jul 2020 07:28:06 -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=oWJjs17r; 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 S1731673AbgG0OY2 (ORCPT + 99 others); Mon, 27 Jul 2020 10:24:28 -0400 Received: from mail.kernel.org ([198.145.29.99]:53992 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732202AbgG0OY1 (ORCPT ); Mon, 27 Jul 2020 10:24:27 -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 D50A22083E; Mon, 27 Jul 2020 14:24:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1595859866; bh=dVQ0hwlWWpDvG5Xm+0dZcSPdLlPaxjOtR+QNtoShlzE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oWJjs17rYgn+KyBYTFj4MGS/ClJeoMS4ZCg9f/LumRexPwz8TlmJZ1WJDYk28BATa Ggn0lMdBKoUxp8flnZwZ5gEov89RGx8gmvIxez9yeQrtmtctXhceBgbxdnFOS3syMF SIQs4WU/aARn6vu/sy/kIf0Yzgt7bn8TvXBTn8x0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Arnd Bergmann , Paul Walmsley , Guo Ren , Michael Ellerman , Palmer Dabbelt , Emil Renner Berthing , Palmer Dabbelt , Will Deacon , Sasha Levin Subject: [PATCH 5.7 135/179] asm-generic/mmiowb: Allow mmiowb_set_pending() when preemptible() Date: Mon, 27 Jul 2020 16:05:10 +0200 Message-Id: <20200727134939.225631465@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200727134932.659499757@linuxfoundation.org> References: <20200727134932.659499757@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: Will Deacon [ Upstream commit bd024e82e4cd95c7f1a475a55f99871936c2b2db ] Although mmiowb() is concerned only with serialising MMIO writes occuring in contexts where a spinlock is held, the call to mmiowb_set_pending() from the MMIO write accessors can occur in preemptible contexts, such as during driver probe() functions where ordering between CPUs is not usually a concern, assuming that the task migration path provides the necessary ordering guarantees. Unfortunately, the default implementation of mmiowb_set_pending() is not preempt-safe, as it makes use of a a per-cpu variable to track its internal state. This has been reported to generate the following splat on riscv: | BUG: using smp_processor_id() in preemptible [00000000] code: swapper/0/1 | caller is regmap_mmio_write32le+0x1c/0x46 | CPU: 3 PID: 1 Comm: swapper/0 Not tainted 5.8.0-rc3-hfu+ #1 | Call Trace: | walk_stackframe+0x0/0x7a | dump_stack+0x6e/0x88 | regmap_mmio_write32le+0x18/0x46 | check_preemption_disabled+0xa4/0xaa | regmap_mmio_write32le+0x18/0x46 | regmap_mmio_write+0x26/0x44 | regmap_write+0x28/0x48 | sifive_gpio_probe+0xc0/0x1da Although it's possible to fix the driver in this case, other splats have been seen from other drivers, including the infamous 8250 UART, and so it's better to address this problem in the mmiowb core itself. Fix mmiowb_set_pending() by using the raw_cpu_ptr() to get at the mmiowb state and then only updating the 'mmiowb_pending' field if we are not preemptible (i.e. we have a non-zero nesting count). Cc: Arnd Bergmann Cc: Paul Walmsley Cc: Guo Ren Cc: Michael Ellerman Reported-by: Palmer Dabbelt Reported-by: Emil Renner Berthing Tested-by: Emil Renner Berthing Reviewed-by: Palmer Dabbelt Acked-by: Palmer Dabbelt Link: https://lore.kernel.org/r/20200716112816.7356-1-will@kernel.org Signed-off-by: Will Deacon Signed-off-by: Sasha Levin --- include/asm-generic/mmiowb.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/asm-generic/mmiowb.h b/include/asm-generic/mmiowb.h index 9439ff037b2d1..5698fca3bf560 100644 --- a/include/asm-generic/mmiowb.h +++ b/include/asm-generic/mmiowb.h @@ -27,7 +27,7 @@ #include DECLARE_PER_CPU(struct mmiowb_state, __mmiowb_state); -#define __mmiowb_state() this_cpu_ptr(&__mmiowb_state) +#define __mmiowb_state() raw_cpu_ptr(&__mmiowb_state) #else #define __mmiowb_state() arch_mmiowb_state() #endif /* arch_mmiowb_state */ @@ -35,7 +35,9 @@ DECLARE_PER_CPU(struct mmiowb_state, __mmiowb_state); static inline void mmiowb_set_pending(void) { struct mmiowb_state *ms = __mmiowb_state(); - ms->mmiowb_pending = ms->nesting_count; + + if (likely(ms->nesting_count)) + ms->mmiowb_pending = ms->nesting_count; } static inline void mmiowb_spin_lock(void) -- 2.25.1