Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp708268rwl; Wed, 12 Apr 2023 03:01:39 -0700 (PDT) X-Google-Smtp-Source: AKy350a3+aQL6u1VATwCf6D9X+AL0F4bUov0fLqPwUws3dvPmLW/7eY+VJCYh0q9HSwrbCFUOqqO X-Received: by 2002:a17:906:3290:b0:94a:4107:3975 with SMTP id 16-20020a170906329000b0094a41073975mr1515234ejw.76.1681293699561; Wed, 12 Apr 2023 03:01:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681293699; cv=none; d=google.com; s=arc-20160816; b=A5syWpxeostzbP0Z/zNSE9PQ8Hs0g1LfFICg9incyV+7NYF4DPC2cygtbySZaH71Gw 6extoHxqXz1ZU7a+qDx/qlXlVAxLxXb3KYD0WuXnoRn00uaZ5C7F/TCzqxp/iIEu261R kjNOeLLuRP9puPnJk1TThz9ymK3uvzeCyQ8NZhjl6EIcOoe7bL5uS7HBzUs20JlY9Bmg TlI0IdHh0W/Un6W0Uy2bjzPvKK7JszXeEzkw8dlxZK2+m4NlsWhH5PIqEl41a2SXsW4c WZd/L0tofOCKfubS9sn00vfkKcYtC1UuZ/PlESLdydQYrQuNroaXWQpHiPdto3f3Q5XW IV4Q== 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=B9If0+wN70Oq5rp1C2/Ek0DCBf+9J1O4Wl9LtQ7guzk=; b=ffWuXzy495JpZGKyHzLLVyLnt6WEemSZwETtAuxOvy0uasjR7nKURti59kX6ErjzfC NBU7m+ke8ULpNN/+FGecvdrd4zeo480o0h4LxJByQIAvIlEzAoSwEYyH9L8UK3jdTuGk 4JXHoDa+ScPiJyH8jM2Sh+p3CCj042MoIWMc1abkExpZCk/nL1yXq4v5I+Ug50bHPojK rK/fyUjzuPqYXyS+yco6cA3Js4eBJFq7osiyx6FNutLK1fhHAnYZHfnLvGaZi1QQmWp9 hq+MxBzlfy7ewQTY/iRIpYPjolMqtj629MqQ9MlPWqWqBpT/UAayhHfQPB6qbYXvNrRq iAdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fnYOuj4U; 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 kl11-20020a170907994b00b0092554df06ffsi1749209ejc.286.2023.04.12.03.01.14; Wed, 12 Apr 2023 03:01:39 -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=fnYOuj4U; 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 S230142AbjDLJm5 (ORCPT + 99 others); Wed, 12 Apr 2023 05:42:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229559AbjDLJmz (ORCPT ); Wed, 12 Apr 2023 05:42:55 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B42365B9 for ; Wed, 12 Apr 2023 02:42:52 -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 A9E0161582 for ; Wed, 12 Apr 2023 09:42:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ED620C433D2; Wed, 12 Apr 2023 09:42:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1681292571; bh=68JdKgkUn5uNY3LQiJfscPHIj+ahYETLBDqXatqZd6U=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=fnYOuj4UBwf8T7J0QshcTEPprPi2ntWTZzMIvEKUaLn1GoOyD4ew3d/8fInUpp+eI UUb8s1y45PKp+pVV2i4lNwywLP3r4fZRPTPCqc4bCCZbAk8pevz1ViEIXiUtSxA28F l74nyy/J672+tw0D/Uz9M87AfESSKt5uEiQKhxHztpi6+2aywFbKz2mttrdex9v3Oq A4gcHv+xZ0r/5w+6ir/owzI6N1Y/0ZCzU7PV96yAssQEnMqBLyLmkuvKrDLOklf4vI ZijXYUyQ0n3g0F5ZBt6IDUm0pqsTo2+bnRpI3+u4nVUSgYzd/UnSbrfBrG+xiBXvMd UuAtofRX38D4g== 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 1pmWzz-007mag-NT; Wed, 12 Apr 2023 10:42:43 +0100 Date: Wed, 12 Apr 2023 10:42:43 +0100 Message-ID: <86y1mxl9m4.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: <20230412041510.497-1-jiangkunkun@huawei.com> References: <20230412041510.497-1-jiangkunkun@huawei.com> 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 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 - I'm curious about how you triggered the issue. Could you please elaborate on that> 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? Thanks, M. diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c index 586271b8aa39..cfb8be3e17d6 100644 --- a/drivers/irqchip/irq-gic-v3-its.c +++ b/drivers/irqchip/irq-gic-v3-its.c @@ -271,13 +271,24 @@ static void vpe_to_cpuid_unlock(struct its_vpe *vpe, unsigned long flags) raw_spin_unlock_irqrestore(&vpe->vpe_lock, flags); } +static struct irq_chip its_vpe_irq_chip; + static int irq_to_cpuid_lock(struct irq_data *d, unsigned long *flags) { - struct its_vlpi_map *map = get_vlpi_map(d); + struct its_vpe *vpe = NULL; int cpu; - if (map) { - cpu = vpe_to_cpuid_lock(map->vpe, flags); + if (d->chip == &its_vpe_irq_chip) + vpe = irq_data_get_irq_chip_data(d); + + if (!vpe) { + struct its_vlpi_map *map = get_vlpi_map(d); + if (map) + vpe = map->vpe; + } + + if (vpe) { + cpu = vpe_to_cpuid_lock(vpe, flags); } else { /* Physical LPIs are already locked via the irq_desc lock */ struct its_device *its_dev = irq_data_get_irq_chip_data(d); @@ -291,9 +302,18 @@ static int irq_to_cpuid_lock(struct irq_data *d, unsigned long *flags) static void irq_to_cpuid_unlock(struct irq_data *d, unsigned long flags) { - struct its_vlpi_map *map = get_vlpi_map(d); + struct its_vpe *vpe = NULL; + + if (d->chip == &its_vpe_irq_chip) + vpe = irq_data_get_irq_chip_data(d); + + if (vpe) { + struct its_vlpi_map *map = get_vlpi_map(d); + if (map) + vpe = map->vpe; + } - if (map) + if (vpe) vpe_to_cpuid_unlock(map->vpe, flags); } @@ -1431,14 +1451,29 @@ static void wait_for_syncr(void __iomem *rdbase) cpu_relax(); } -static void direct_lpi_inv(struct irq_data *d) +static void __direct_lpi_inv(struct irq_data *d, u64 val) { - struct its_vlpi_map *map = get_vlpi_map(d); void __iomem *rdbase; unsigned long flags; - u64 val; int cpu; + /* Target the redistributor this LPI is currently routed to */ + cpu = irq_to_cpuid_lock(d, &flags); + raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock); + + rdbase = per_cpu_ptr(gic_rdists->rdist, cpu)->rd_base; + gic_write_lpir(val, rdbase + GICR_INVLPIR); + wait_for_syncr(rdbase); + + raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock); + irq_to_cpuid_unlock(d, flags); +} + +static void direct_lpi_inv(struct irq_data *d) +{ + struct its_vlpi_map *map = get_vlpi_map(d); + u64 val; + if (map) { struct its_device *its_dev = irq_data_get_irq_chip_data(d); @@ -1451,15 +1486,7 @@ static void direct_lpi_inv(struct irq_data *d) val = d->hwirq; } - /* Target the redistributor this LPI is currently routed to */ - cpu = irq_to_cpuid_lock(d, &flags); - raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock); - rdbase = per_cpu_ptr(gic_rdists->rdist, cpu)->rd_base; - gic_write_lpir(val, rdbase + GICR_INVLPIR); - - wait_for_syncr(rdbase); - raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock); - irq_to_cpuid_unlock(d, flags); + __direct_lpi_inv(d, val); } static void lpi_update_config(struct irq_data *d, u8 clr, u8 set) @@ -3941,18 +3968,10 @@ static void its_vpe_send_inv(struct irq_data *d) { struct its_vpe *vpe = irq_data_get_irq_chip_data(d); - if (gic_rdists->has_direct_lpi) { - void __iomem *rdbase; - - /* 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; - 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); - } else { + if (gic_rdists->has_direct_lpi) + __direct_lpi_inv(d, d->parent_data->hwirq); + else its_vpe_send_cmd(vpe, its_send_inv); - } } static void its_vpe_mask_irq(struct irq_data *d) -- Without deviation from the norm, progress is not possible.