From patchwork Mon Jul 1 07:23:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tangnianyao X-Patchwork-Id: 13717613 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BD8B4C2BD09 for ; Mon, 1 Jul 2024 07:24:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type: Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject:CC:To:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=uCls7WicbrqMYKp5QxrX6uW2m3h3XwPk/n5kwRL6wkM=; b=MHwnXeZE5koRoXsjoavRdGYYSp DwrjpJud4tMP4LZCWEh2ssa0ES00rB2hyxUptDmWZu+RYZ3GG+c7fcKkboIUapX9kQgNsq5iKhzs2 jibspKxxrMnu1ds4LP0SSQ08T5nVxX4xRrj+mTKfg2f3FR2UPLJf6fWJeG9zDWR0XlLZww4zCmjOV bdOz4SFRBpUOJAlPIy++gTi+3Tbs7hjsm40Zi3V4h+UhYfgLcE7BAfP4Vdnw3+2XIXo8yrSfqmRb9 uGAuYUVbs1Y1bT3z1KVcVpTtMeu80B1IwIrRIuYMazAHVO/Uvn9y2NlEOySqdEISoW2Rkb2ruQVAv aNjsyGyg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sOBON-000000021CA-1yWj; Mon, 01 Jul 2024 07:24:03 +0000 Received: from szxga02-in.huawei.com ([45.249.212.188]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sOBOA-0000000215Y-41Pf for linux-arm-kernel@lists.infradead.org; Mon, 01 Jul 2024 07:23:53 +0000 Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4WCHZp0q03znYj9; Mon, 1 Jul 2024 15:23:30 +0800 (CST) Received: from kwepemd500014.china.huawei.com (unknown [7.221.188.63]) by mail.maildlp.com (Postfix) with ESMTPS id B162F180086; Mon, 1 Jul 2024 15:23:43 +0800 (CST) Received: from 228-1616.huawei.com (10.67.246.68) by kwepemd500014.china.huawei.com (7.221.188.63) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.34; Mon, 1 Jul 2024 15:23:43 +0800 From: Nianyao Tang To: , , , CC: , , Subject: [PATCH] irqchip/gic-v4: Fix vcpus racing for vpe->col_idx in vmapp and vmovp Date: Mon, 1 Jul 2024 07:23:05 +0000 Message-ID: <20240701072305.4129823-1-tangnianyao@huawei.com> X-Mailer: git-send-email 2.30.0 MIME-Version: 1.0 X-Originating-IP: [10.67.246.68] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemd500014.china.huawei.com (7.221.188.63) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240701_002351_696929_2860FC52 X-CRM114-Status: GOOD ( 14.49 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org its_map_vm may modify vpe->col_idx without holding vpe->vpe_lock. It would result in a vpe resident on one RD after vmovp to a different RD. Or, a vpe maybe vmovp to a RD same as it is current mapped in vpe table. On a 2-ITS, GICv4 enabled system, 32 vcpus deployed on cpu of collection 0 and 1. Two pci devices route VLPIs, using each of the ITS. VPE ready to reside on RD1 may have such unexpected case because another vcpu on other cpu is doing vmapp and modify his vpe->col_idx. Unexpected Case 1: RD 0 1 vcpu_load lock vpe_lock vpe->col_idx = 1 its_map_vm lock vmovp_lock waiting vmovp_lock vpe->col_idx = 0 (cpu0 is first online cpu) vmapp vpe on col0 unlock vmovp_lock lock vmovp_lock vmovp vpe to col0 unlock vmovp_lock vpe resident here fail to receive VLPI! Unexpected Case 2: RD 0 1 its_map_vm vcpu_load lock vmovp_lock lock vpe_lock vpe->col_idx = 0 vpe->col_idx = 1 vmapp vpe on col1 waiting vmovp_lock unlock vmovp_lock lock vmovp_lock vmovp vpe to col1 (target RD == source RD!) unlock vmovp_lock Signed-off-by: Nianyao Tang Signed-off-by: Marc Zyngier --- drivers/irqchip/irq-gic-v3-its.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c index f99c0a86320b..adda9824e0e7 100644 --- a/drivers/irqchip/irq-gic-v3-its.c +++ b/drivers/irqchip/irq-gic-v3-its.c @@ -1794,11 +1794,15 @@ static bool gic_requires_eager_mapping(void) static void its_map_vm(struct its_node *its, struct its_vm *vm) { unsigned long flags; + bool vm_mapped_on_any_its = false; + int i; if (gic_requires_eager_mapping()) return; - raw_spin_lock_irqsave(&vmovp_lock, flags); + for (i = 0; i < GICv4_ITS_LIST_MAX; i++) + if (vm->vlpi_count[i] > 0) + vm_mapped_on_any_its = true; /* * If the VM wasn't mapped yet, iterate over the vpes and get @@ -1813,15 +1817,19 @@ static void its_map_vm(struct its_node *its, struct its_vm *vm) struct its_vpe *vpe = vm->vpes[i]; struct irq_data *d = irq_get_irq_data(vpe->irq); - /* Map the VPE to the first possible CPU */ - vpe->col_idx = cpumask_first(cpu_online_mask); + raw_spin_lock_irqsave(&vpe->vpe_lock, flags); + + if (!vm_mapped_on_any_its) { + /* Map the VPE to the first possible CPU */ + vpe->col_idx = cpumask_first(cpu_online_mask); + } its_send_vmapp(its, vpe, true); its_send_vinvall(its, vpe); irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx)); + + raw_spin_unlock_irqrestore(&vpe->vpe_lock, flags); } } - - raw_spin_unlock_irqrestore(&vmovp_lock, flags); } static void its_unmap_vm(struct its_node *its, struct its_vm *vm)