From patchwork Wed Jan 24 09:10:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13528818 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 BCB72C46CD2 for ; Wed, 24 Jan 2024 09:11:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: Mime-Version:Date: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=EA9mU+ff0QMOhM1IxD0zhMescIFrVdIDNN3tBgeL3bc=; b=m7m qStty/zAF7uuplHypNDq1N3O6BTmn7LH1HJl5BAx5C5EetDFiPRtPE4aIopf7ffPh+eIZC+Q9XRd8 nedcvPmjKb3XPYeCAfsz1pBLCkNUz8G29m5+n36vMq7RX+aX9rKftenA3TD7XZpMXRNN0M1Ikx84L 2XRtwyKGPys2ExnUBtgyC48Qfwmy9w71pTP8scqbKY3pYm2TZua7pGDAi36Khql80X+30Ntx5yRXJ qQm5bVIuQ52jR1Po9fmUma6Iyq/6Ijm/Wcp0Ws/AHGQaiuP8GCGQtQFdjB+n6irO9f/EFUc4Qhq90 u9GPYMDOEof/3j7dS0rw+v8dJj0DS6w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rSZHS-0028H0-21; Wed, 24 Jan 2024 09:10:46 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rSZHR-0028GU-28 for linux-arm-kernel@bombadil.infradead.org; Wed, 24 Jan 2024 09:10:45 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Cc:To:From:Subject: Message-ID:Mime-Version:Date:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To:References; bh=azYCHPhWGygUAZtVjzOnMMNYpv2pTJPs25+3mFpcg14=; b=EpztfWVx4Rr+cUug1qhwKSrUq3 TAkVBU2rZkSfJrDeXBqwlePThc8bIgCT6AZszcnQ8XWn0RhUWXoZOLhgQQNUYsVON+un1F7wIgK06 NSAXtHXcJTx2/juxuZLpKVBL7Y5J6u6xvtRZ0KehgAE9BcYk0+YdWGsnAlPmRptUBTVXdUuC1B/PM uB6tlieG1W7gvDI4vZ60MS+X7xD9ufW7oPPKvKP7FdyDfBd41G1ORq7T5ogcJnNwD8S1TxSvxuyg7 cL/qjfkGx4N+javvggJA+PpqnaWFW96IBNN1RouJJppnACJ2fpLDTGJnkkeC4FdU6jrHF5UIS61Ta XcFgQ8dg==; Received: from mail-wm1-x349.google.com ([2a00:1450:4864:20::349]) by desiato.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rSZHN-00000004LMp-1RFZ for linux-arm-kernel@lists.infradead.org; Wed, 24 Jan 2024 09:10:43 +0000 Received: by mail-wm1-x349.google.com with SMTP id 5b1f17b1804b1-40e40126031so51443235e9.0 for ; Wed, 24 Jan 2024 01:10:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706087436; x=1706692236; darn=lists.infradead.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=azYCHPhWGygUAZtVjzOnMMNYpv2pTJPs25+3mFpcg14=; b=PLqO7Hy6pOsx+koRbhJzPrt6kpLmQvCeV8iQktRRt1SP68OyngNQluE0L8VyiLRbOE bigO9T8qgwUdtPWGJxBikMEDNYwwDfDn067heXNyBgL7WDSsEjenP2KzDuMModjxpB1L mX+RZPYtuRA53c2LHgBb5g35TlCQJgTbMM1BF5liTE/PO6n0pE5XW4AFDdRXM4/b4wXl E/jBPhlQEsqSqCMK9RgEiTcOMl2ry7eEMoct4fsISLTEvn9TfVz9zx0PjYFvbFazEuGE fwi2yn6fk3eqFiZ2u7PicZTjuGErCS+2GU76oc6m74RamQXi6Pe8nNRWWIqFvuSiEA45 FHWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706087436; x=1706692236; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=azYCHPhWGygUAZtVjzOnMMNYpv2pTJPs25+3mFpcg14=; b=wA4oB3tRpLFJdu2w6J4w+Ccm5HZM0zgbhHKQVmDqUbBCMM216IFb4HHoL9pRUyWs3j 1s/s1JTvHOY/+XxzQ+EHIwCmc6dMsBM9fmAERLopTjsWp1Na6MFmOW2Uvyub1R+JckdB eaK9aX1+OJ//1JtMjKFp+9X0kt58YzRVLd40h/smTjJTys69oJDIV0ZgzhWj/KbbRGpS roacPwoifJlbst8H5TlCgPCelyB1P/84MYH/mRCw5dgc+dR98zeo9DVxhVW6QV0PZzbH Lmouj1CRRwCN3a6wIxdb9q7MVYmfGOTIMYfIX37azDS4DV7Rv739NShoXLfxepn3Grq3 q38g== X-Gm-Message-State: AOJu0Yy9fevutEYeKSwP6PRVwhFjG8g1UGtcfS8EC+hnjAv1TO7hRHe1 EQg/ZcJrufA1ljUgcsIy5B0STpA/YVYh1vTZL+UC4sxeC19004dKWUFH01t3cBermNK4HeV+01f 5TLCqmluqwjdJelhaDrPJ3wnfyw== X-Google-Smtp-Source: AGHT+IHQu8PSyX8X6hBs0OxdOvH6yTQqd8GMOA0f4B5GP1tHJaIbkkEvv2xekuxmDaroKhS0ZGfjPA1hyZX71dbNPJg= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:600c:518e:b0:40e:c602:e37d with SMTP id fa14-20020a05600c518e00b0040ec602e37dmr8947wmb.0.1706087436115; Wed, 24 Jan 2024 01:10:36 -0800 (PST) Date: Wed, 24 Jan 2024 09:10:28 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240124091027.1477174-2-sebastianene@google.com> Subject: [PATCH v2] KVM: arm64: Fix circular locking dependency From: Sebastian Ene To: Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kernel-team@android.com, Sebastian Ene , stable@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240124_091041_517356_0AA935A3 X-CRM114-Status: GOOD ( 12.25 ) 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 The rule inside kvm enforces that the vcpu->mutex is taken *inside* kvm->lock. The rule is violated by the pkvm_create_hyp_vm() which acquires the kvm->lock while already holding the vcpu->mutex lock from kvm_vcpu_ioctl(). Avoid the circular locking dependency altogether by protecting the hyp vm handle with the config_lock, much like we already do for other forms of VM-scoped data. Signed-off-by: Sebastian Ene Cc: stable@vger.kernel.org Reviewed-by: Oliver Upton --- arch/arm64/kvm/pkvm.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c index 8350fb8fee0b..b7be96a53597 100644 --- a/arch/arm64/kvm/pkvm.c +++ b/arch/arm64/kvm/pkvm.c @@ -101,6 +101,17 @@ void __init kvm_hyp_reserve(void) hyp_mem_base); } +static void __pkvm_destroy_hyp_vm(struct kvm *host_kvm) +{ + if (host_kvm->arch.pkvm.handle) { + WARN_ON(kvm_call_hyp_nvhe(__pkvm_teardown_vm, + host_kvm->arch.pkvm.handle)); + } + + host_kvm->arch.pkvm.handle = 0; + free_hyp_memcache(&host_kvm->arch.pkvm.teardown_mc); +} + /* * Allocates and donates memory for hypervisor VM structs at EL2. * @@ -181,7 +192,7 @@ static int __pkvm_create_hyp_vm(struct kvm *host_kvm) return 0; destroy_vm: - pkvm_destroy_hyp_vm(host_kvm); + __pkvm_destroy_hyp_vm(host_kvm); return ret; free_vm: free_pages_exact(hyp_vm, hyp_vm_sz); @@ -194,23 +205,19 @@ int pkvm_create_hyp_vm(struct kvm *host_kvm) { int ret = 0; - mutex_lock(&host_kvm->lock); + mutex_lock(&host_kvm->arch.config_lock); if (!host_kvm->arch.pkvm.handle) ret = __pkvm_create_hyp_vm(host_kvm); - mutex_unlock(&host_kvm->lock); + mutex_unlock(&host_kvm->arch.config_lock); return ret; } void pkvm_destroy_hyp_vm(struct kvm *host_kvm) { - if (host_kvm->arch.pkvm.handle) { - WARN_ON(kvm_call_hyp_nvhe(__pkvm_teardown_vm, - host_kvm->arch.pkvm.handle)); - } - - host_kvm->arch.pkvm.handle = 0; - free_hyp_memcache(&host_kvm->arch.pkvm.teardown_mc); + mutex_lock(&host_kvm->arch.config_lock); + __pkvm_destroy_hyp_vm(host_kvm); + mutex_unlock(&host_kvm->arch.config_lock); } int pkvm_init_host_vm(struct kvm *host_kvm)