Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp2057059rwl; Thu, 13 Apr 2023 00:40:48 -0700 (PDT) X-Google-Smtp-Source: AKy350a0XoN5vuJ9mUf6jBRsEnf7m5mVcZ4vAETlomaHXq1eAphfdNG60nHKs8tDCRVXZxMoROLL X-Received: by 2002:a05:6402:517b:b0:4c6:10a7:c422 with SMTP id d27-20020a056402517b00b004c610a7c422mr1731636ede.18.1681371648173; Thu, 13 Apr 2023 00:40:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681371648; cv=none; d=google.com; s=arc-20160816; b=SLydqJH5NpEcYY29LSpfQu9v1pvVMT1WGKyxJ9OfwcaQMDKep2sgyG0HPn5TFryk1g SCGQY4+3vHNKMZ73SHd3e408RS4RX2/M0gkqbVSqonO9L2J9Z2x24LfZRuFOGtqir19s o93ASNaVBeg7sj2+X8Nst9UdxhKV7yM0rgL5pea76WLN0Azb0oqJa8KJY48nwrtOJhN6 KDA9tGje7MfKoB3y5ljTCDwbHUUVNsfvtMjSpObXMDQ4Tww7vSjsy4iVDo7ZkPREH03S 4puT+WmHYb8OTwn6GaRo1m3ftyoPRwtNUuxCTm8nObf09FOZm82Bl7FrZaVJr97hxSE9 TRvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature; bh=lS2pXKngqYG/7O14VcZD6oSI2JZkqoihp7S6bpiBIM0=; b=p22BhfAJQIFH2FgChfZ/HnGXmcP61nDJFddy/6ZOB+ElAn9ydqRucYgF/vIAqvLlGM zXpHbmBxg1+AXKZOizKLAKBofG+Z+FRBCOKNor3xQgGsiGA9UqNpdwkt7NuAxsRWE77T YcTUYrvuWIwkCaxOZIfBXHKU06bdyCDZz63HrwvFPr7PwbJcbo1JIhjhDyrGgnLFjLGQ /weRU/zLHcDkIbtovVy8KbpRTAjEcUmm2R0I9jUF+2C48mZcOU8xNOQ08kDa93cPR/vD Qr/zx4vqEk5z8VqJU09U2feZdL6TJes7FeD09vH50/iSLt6TSB1imcKdHv75sbwKsyJp Jl5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Lc/yIjOv"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n2-20020a056402514200b00504b18d24f8si1139046edd.362.2023.04.13.00.40.22; Thu, 13 Apr 2023 00:40:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Lc/yIjOv"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229830AbjDMHf2 (ORCPT + 99 others); Thu, 13 Apr 2023 03:35:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229597AbjDMHfX (ORCPT ); Thu, 13 Apr 2023 03:35:23 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B9C693CF for ; Thu, 13 Apr 2023 00:35:08 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0C494614D0 for ; Thu, 13 Apr 2023 07:35:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 71BE9C433D2; Thu, 13 Apr 2023 07:35:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1681371307; bh=wdR1ZCPGB+immJ4jeOOX3FET2VbmRaEVHHP0Z9AB6fM=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Lc/yIjOvBWgQJExW1iM/oEskSUVwlJ9MbLB3HOIPi7A5fIo2ZPz/WuCRo+12vEZXF HRrSJLB95SqKzt0OvXPKyCdwyMWMGfF+M5k/SND4UQ9GY5TCWtdgNp7l2wWAHCuY/1 37rS0EubKdgEgTsHwAs4fx1HINWSxDzp2D16nnS7XGdYtRV+WZDAUJpanjknsNYUAm 7+IJJi1nE8MzRUicAG6x/qXKLvFjxf05MxUO35ADNAH0SdURqzD7TE6ce7dNjffKmF 6mn7Te0n2EUzbT2Rk5qQnJIoOXst0PGU1mYvwOTdPMvNEyd4yIA7ERECv7qvGm4uAc DF3tiJ/smAbug== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pmrU1-0082KM-5t; Thu, 13 Apr 2023 08:35:05 +0100 Date: Thu, 13 Apr 2023 08:35:04 +0100 Message-ID: <86mt3ckzfb.wl-maz@kernel.org> From: Marc Zyngier To: Kunkun Jiang Cc: Thomas Gleixner , Zenghui Yu , "open list:IRQCHIP DRIVERS" , , , Subject: Re: [PATCH] irqchipi/gic-v4: Ensure accessing the correct RD when and writing INVLPIR In-Reply-To: References: <20230412041510.497-1-jiangkunkun@huawei.com> <86y1mxl9m4.wl-maz@kernel.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/28.2 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: jiangkunkun@huawei.com, tglx@linutronix.de, yuzenghui@huawei.com, linux-kernel@vger.kernel.org, wanghaibin.wang@huawei.com, chenxiang66@hisilicon.com, tangnianyao@huawei.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 13 Apr 2023 04:57:17 +0100, Kunkun Jiang wrote: > > Hi Marc, > > On 2023/4/12 17:42, Marc Zyngier wrote: > > On Wed, 12 Apr 2023 05:15:10 +0100, > > Kunkun Jiang wrote: > >> commit f3a059219bc7 ("irqchip/gic-v4.1: Ensure mutual exclusion between > >> vPE affinity change and RD access") tried to address the race > >> between the RD accesses and the vPE affinity change, but somehow > >> forgot to take GICR_INVLPIR into account. Let's take the vpe_lock > >> before evaluating vpe->col_idx to fix it. > >> > >> Fixes: f3a059219bc7 ("irqchip/gic-v4.1: Ensure mutual exclusion between vPE affinity change and RD access") > >> Signed-off-by: Kunkun Jiang > >> Signed-off-by: Xiang Chen > >> Signed-off-by: Nianyao Tang > > Yup, nice catch. A few remarks though: > > > > - the subject looks odd: there is a spurious 'and' there, and it > > doesn't say this is all about VPE doorbell invalidation (the code > > that deals with direct LPI is otherwise fine) > > > > - the SoB chain is also odd. You should be last in the chain, and all > > the others have Co-developed-by tags in addition to the SoB, unless > > you wanted another tag > Thanks for your guidance. > > > > - I'm curious about how you triggered the issue. Could you please > > elaborate on that> > > I will detail it here: > 1. a multi-core VM with a pass-through device, enable GICv4 > 2. insmod the device driver in guest > then,the following two call trace information is displayed occasionally: > > > [ 1055.683978] BUG: spinlock already unlocked on CPU#1, [...] > After analysis, I found that when insmod the device driver in guest, > its_map_vm will be executed on the host and map all vPEs to the > first possible CPU(pCPU0). When dealing with WFI, its_vpe_send_inv > will access RD, obtain and release the 'rd_lock' through 'vpe->col_idx'. [...] > So something like this happens: > After obtaining the 'rd_lock' through 'vpe->col_idx', its_map_vm > modifies 'vpe->col_idx'. > This is also the cause of the preceding call trace. Right. I figured this out, but the key information is that you get it at boot time due to moving the VPEs away from CPU0 > There's a little question I haven't figured out here. Why map all vPEs > to the first possible CPU in its_map_vm? In addition, vpe_lock is not > used here. Will concurrency problems occur in the future? Where would you like to map them? Any physical CPU would do the trick. We could take the current CPU, but the fact is that we don't always know where the VPEs are running. As for holding vpe_lock, I don't think we need to. This only happens on the first VLPI being forwarded, so there should be no real GIC context for anything. But I must admit that I haven't looked at this code in a long time, and have no way to test it anymore. > > > > > Finally, I think we can fix it in a better way, see below: > > > >> --- > >> drivers/irqchip/irq-gic-v3-its.c | 10 +++++++--- > >> 1 file changed, 7 insertions(+), 3 deletions(-) > >> > >> diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c > >> index 586271b8aa39..041f06922587 100644 > >> --- a/drivers/irqchip/irq-gic-v3-its.c > >> +++ b/drivers/irqchip/irq-gic-v3-its.c > >> @@ -3943,13 +3943,17 @@ static void its_vpe_send_inv(struct irq_data *d) > >> if (gic_rdists->has_direct_lpi) { > >> void __iomem *rdbase; > >> + unsigned long flags; > >> + int cpu; > >> /* Target the redistributor this VPE is currently > >> known on */ > >> - raw_spin_lock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock); > >> - rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base; > >> + cpu = vpe_to_cpuid_lock(vpe, &flags); > >> + raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock); > >> + rdbase = per_cpu_ptr(gic_rdists->rdist, cpu)->rd_base; > >> gic_write_lpir(d->parent_data->hwirq, rdbase + GICR_INVLPIR); > >> wait_for_syncr(rdbase); > >> - raw_spin_unlock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock); > >> + raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock); > >> + vpe_to_cpuid_unlock(vpe, flags); > >> } else { > >> its_vpe_send_cmd(vpe, its_send_inv); > >> } > > The main reason this bug crept in is that we have a some pretty silly > > code duplication going on. > > > > Wouldn't it be nice if irq_to_cpuid() could work out whether it is > > dealing with a LPI or a VLPI like it does today, but also directly > > with a VPE? We could then use the same code as derect_lpi_inv(). I > > came up with this the hack below, which is totally untested as I don't > > have access to GICv4.1 HW. > > > > Could you give it a spin? > > Nice, I will test it as soon as possible. Cool, please let me know when you get a chance. Thanks, M. -- Without deviation from the norm, progress is not possible.