Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp4411576ybl; Mon, 26 Aug 2019 10:02:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqw0j4GUBLsdaflAWCra/gncMFSRYG/tJ0DQxsxfEwb5BBal/ikQelOxtdpObkOzLJJdWksy X-Received: by 2002:aa7:85d6:: with SMTP id z22mr20454160pfn.262.1566838968469; Mon, 26 Aug 2019 10:02:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566838968; cv=none; d=google.com; s=arc-20160816; b=Bs3gW7p1bBW3oT5wsoX410RBbdPB9ufIpyYDaoOFcYCSsnjPthXN6A9GeSFOwo008U kMr6x6mj6Qg2o6TxbHOHoR8umftlrr86yJUEvCxB4UrFO1tJ7fX36aoj+2NpDUl4Ov2K KHYx163mpHB/yrwRNgXEcbRXLwDJ5iGcl/n3YQRFEXWRywVM0vZWIaKsvDJ4b9I6EhdR dHtbX9uJyDGM/DBeIWsznaqy4bApYim0f4WQascRc/UzPJgBt9vygJ1zKd6nNbm4VOqR uT21OHMnXOcO7vkiqNfo5MuQ4tM/BT5o6uGQ+6N2+79jY49mvnwsgrGxDzEAV+ZW3S55 6nWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=pbvGU5biJ6C2kxABu2djfupUegRS19Yp2iNoh2QCWVs=; b=RkadXQA3B2iRLLTYjdb3DI9jY1F8+wNPcp8m/lRkLYHcQLKprICC7ZYuObm4v0k8sa bqnBW7PVHzZqWJrzw1TggVdZfn6HLSGo3fFQATYnEj7sIBTfb+IiXXJX8j3zUTm9LIUq N5lvgiJH80vBE5Po4QSy3csAchmGU614it1VrgWtiNGEFDOoQLeZDyXCxEtHVDJ+7qVq jBPqOX92R4g4LYrWhZZYQ7mKP0u0zrEPpOGwRZIhrSVM4l4tyr8K4U3rAf4xT9vtXOH2 yGmzooJHOewddlFGlkbi+O8MFzb0SMri5PZObDsNU0nAN3ayBQjAsQzC+r+d1M2AtXyg KULA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n5si10336256plk.172.2019.08.26.10.02.33; Mon, 26 Aug 2019 10:02:48 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732949AbfHZQkh (ORCPT + 99 others); Mon, 26 Aug 2019 12:40:37 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:60288 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1732900AbfHZQkg (ORCPT ); Mon, 26 Aug 2019 12:40:36 -0400 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: , , CC: , Heyi Guo , Zenghui Yu , Marc Zyngier , James Morse , Julien Thierry , "Suzuki K Poulose" 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 Content-Type: text/plain X-Originating-IP: [10.175.104.237] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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); -- 1.8.3.1