From patchwork Mon Aug 26 16:39:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heyi Guo X-Patchwork-Id: 11115069 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D844D1800 for ; Mon, 26 Aug 2019 16:40:57 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id B6601206BA for ; Mon, 26 Aug 2019 16:40:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="GalUg9C4" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B6601206BA Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject: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=+FXSdfq87UzSNeGCiwGpqi6t9cC6FAaOJ2tZlpachJg=; b=GalUg9C4ELocuR Y1BhUQXpekFm/LhewbUyXgjdB25ljl5CexVC2WeFQ7R7YL1lffZj3nSD+KeYYJBcoEiTOKJ9hrwqo dTh0Ylw8aDosuzfrNRbPHzi0sC/6+bgd37e+bQQVLJz0eENfHCyaATQt6x+NdTnCC8oHBOWP6ocgy Hs3g0MJ2xKWiZsTulEN3Pf2ODfefMPJaw0UKJ1dteD0Ms5IX0pQpRyoAW0coJKqGOuHThkEDk8nVZ TVtm0KZZOafp7OF9Ztev8ZUWmxpOVNXy8MabPYwMCUQzcfpnnYKdQh+Vaf/+cPgFDB9IKEEZmVL+7 4cukhPbGKR3wDbkc0pYg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92 #3 (Red Hat Linux)) id 1i2I31-0002lW-BC; Mon, 26 Aug 2019 16:40:51 +0000 Received: from szxga07-in.huawei.com ([45.249.212.35] helo=huawei.com) by bombadil.infradead.org with esmtps (Exim 4.92 #3 (Red Hat Linux)) id 1i2I2w-0002N1-IQ for linux-arm-kernel@lists.infradead.org; Mon, 26 Aug 2019 16:40:48 +0000 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 52465C224D703FD301BC; Tue, 27 Aug 2019 00:40:29 +0800 (CST) Received: from linux-lLwqrf.huawei.com (10.175.104.237) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.439.0; Tue, 27 Aug 2019 00:40:20 +0800 From: Heyi Guo To: , , Subject: [PATCH] kvm/arm/vgic: fix potential deadlock when ap_list is long Date: Tue, 27 Aug 2019 00:39:12 +0800 Message-ID: <1566837552-127854-1-git-send-email-guoheyi@huawei.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 X-Originating-IP: [10.175.104.237] X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190826_094046_848050_8FE4BB73 X-CRM114-Status: GOOD ( 10.25 ) X-Spam-Score: -2.3 (--) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-2.3 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [45.249.212.35 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Suzuki K Poulose , Marc Zyngier , Zenghui Yu , James Morse , Heyi Guo , wanghaibin.wang@huawei.com, Julien Thierry Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org If ap_list is longer than 256, merge_final() in sort_list() will call comparison function with the same element just as below: do { /* * If the merge is highly unbalanced (e.g. the input is * already sorted), this loop may run many iterations. * Continue callbacks to the client even though no * element comparison is needed, so the client's cmp() * routine can invoke cond_resched() periodically. */ if (unlikely(!++count)) cmp(priv, b, b); This will definitely cause deadlock in vgic_irq_cmp() and the call trace is: [ 2667.130283] Call trace: [ 2667.130284] queued_spin_lock_slowpath+0x64/0x2a8 [ 2667.130284] vgic_irq_cmp+0xfc/0x130 [ 2667.130284] list_sort.part.0+0x1c0/0x268 [ 2667.130285] list_sort+0x18/0x28 [ 2667.130285] vgic_flush_lr_state+0x158/0x518 [ 2667.130285] kvm_vgic_flush_hwstate+0x70/0x108 [ 2667.130286] kvm_arch_vcpu_ioctl_run+0x114/0xa50 [ 2667.130286] kvm_vcpu_ioctl+0x490/0x8c8 [ 2667.130286] do_vfs_ioctl+0xc4/0x8c0 [ 2667.130287] ksys_ioctl+0x8c/0xa0 [ 2667.130287] __arm64_sys_ioctl+0x28/0x38 [ 2667.130287] el0_svc_common+0x78/0x130 [ 2667.130288] el0_svc_handler+0x38/0x78 [ 2667.130288] el0_svc+0x8/0xc So return 0 immediately when a==b. Signed-off-by: Zenghui Yu Signed-off-by: Heyi Guo Cc: Marc Zyngier Cc: James Morse Cc: Julien Thierry Cc: Suzuki K Poulose --- virt/kvm/arm/vgic/vgic.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/virt/kvm/arm/vgic/vgic.c b/virt/kvm/arm/vgic/vgic.c index 13d4b38..64ed0dc 100644 --- a/virt/kvm/arm/vgic/vgic.c +++ b/virt/kvm/arm/vgic/vgic.c @@ -254,6 +254,13 @@ static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b) bool penda, pendb; int ret; + /* + * list_sort may call this function with the same element when the list + * is farely long. + */ + if (unlikely(a == b)) + return 0; + raw_spin_lock(&irqa->irq_lock); raw_spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING);