Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp993540rdb; Wed, 24 Jan 2024 01:10:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IEsrn4hiJUHOoXypofeczTTAy/0xUexYY3Q8WPaT0jC3SGGUnjdaxM23zBqyD06mhWjvUb2 X-Received: by 2002:a25:eb10:0:b0:dbd:b105:3a49 with SMTP id d16-20020a25eb10000000b00dbdb1053a49mr336003ybs.91.1706087449864; Wed, 24 Jan 2024 01:10:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706087449; cv=pass; d=google.com; s=arc-20160816; b=d3OfRXfQ23qOBmoDYwd+oxp5FAjkP68BQb9Ql6tMwEllYhGPazikZzogeOxOpEFuGj vEXaXOmIEW0QkTTbrJQfUV6sc5/wqRoIzWX/p8YdBuG12vsNXwDnjsaRIw/1SO4mJdyT k5iYKfEBkhG6MZ8RgKjS5QEkWTl5q0PU664fnqT93xHoTHfOPdkP4/i2s952Po4fvV2z QJy6y+cb6nqI5a2ZLWFALUBd/qH7hKkTQWKhSZoU2+XXa8KdKH1wbbwU6lDN1MXRaU7T fAO2g9d+DCPRFl3RWQC9DcV0kORCafYkmRNPN5MjLzqfa9ZFwnZmtKHgUYqn41aaOi5N rIKA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:dkim-signature; bh=azYCHPhWGygUAZtVjzOnMMNYpv2pTJPs25+3mFpcg14=; fh=NCV4VdvBgdk8cxIKaqonQv/VA5zcHTvIGRXkRofCIXw=; b=Mifgm98bd2HQ3lzb7kZQHTdQoNWirl+1Vd1nOsyAkjh239XBWKLGtdvAgKsE61BHx4 XUewbcPsvx3iyQZR/TeUs3+kLGVTTSlw3ItSBolEJ+U2X9V5v1WvgasqD/Ra4yk0Ho5q QVeJnk8SJ5NKfnh3adLWem4vQmxHD2/7dGSaAEoRpQz5B6dInPYN5jP/KgXrmP785DN8 J9it/VMUgCfEvAU/AVJAiuLDt1bKwkiwz6m/funh2IhC+JBeJcJbhV5fdyfcKuGXulWC s5LoqKxQRmxLzDEGhfxQRbFERDvwl/gQAf+MmY8J6hkZiNR9JduwshBlsv5rjj9vuGM0 rIww== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=uedZXKC2; arc=pass (i=1 spf=pass spfdomain=flex--sebastianene.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-36681-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36681-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id v10-20020a0c8e0a000000b00686a0c905e1si3206191qvb.463.2024.01.24.01.10.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 01:10:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-36681-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=uedZXKC2; arc=pass (i=1 spf=pass spfdomain=flex--sebastianene.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-36681-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36681-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 372E01C21DA9 for ; Wed, 24 Jan 2024 09:10:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6A72E17BCC; Wed, 24 Jan 2024 09:10:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uedZXKC2" Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A78617BA5 for ; Wed, 24 Jan 2024 09:10:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706087439; cv=none; b=fQI5PDLIn4CE1193pCKbbAbVGizldot6NoCt67SrhrxwI/MecxeRSZrWk2u4l3gGmHJJTBY+hpL8lY8vTTCYedeKNofAown9yj64ZS/bRXhzHOa+WKj22squaPFK76fZ852QhWKirSyuomEuGDvYT4KUw3oDEIQu0EjtusXC1a4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706087439; c=relaxed/simple; bh=VbIlhMmrk27yNKtV56v+KJ/TtRK0LOiHjDYn+JraBwI=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=DryksBd2EA9uGuXP3HwOkcv0Onne7Hqd0i5v3hfBLMBu79/9KmjdfJN0sCxSMX9nIPvYuFC61AgxttmzOqU6FQNtMgijZF+JinVWH84gKx59Bzar06dmy5Gbn6q7NkjlZdm5idAcPDfVPWfYcT7EzjUXH4hor3OjD3exjwAvI8s= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=uedZXKC2; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-33927b1fac7so2253815f8f.1 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=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=azYCHPhWGygUAZtVjzOnMMNYpv2pTJPs25+3mFpcg14=; b=uedZXKC2+RzmTKjt+Jbqhi1yd/+qzSqxlrk+L9NnuuyiGSGW8iM3rjwskmnOUlrXDE 57ccLc1nj4jeDq2zCSqS5h3diAWLu1nyFfj+3Aq5rP7Jq3DrsY+i5slNUgwZKr3XZ29S 9b5P0EtcCnNoA80RciwHAL9Kb7+eD1cvGiIq6TO9srUkVIaboubXc9a5gsSZT+OahsP5 uL0fl7HaZG0uD1JWPug+/DFtfjyjhihnFQSimV0NvdJsAwRWTK3S5dlacnqlYga9r9Gu 92d0uJOlmW9AOw1qiMeG7ZVKNG4VhHgL+kTcOygipqhMnMCTKjc+zTcueVNtu6Wx+PFT QyxQ== 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=jrj2a0x2IZT6dhoNl+S8lBq5ryyq7xCqCcVsZgIc2UlBrDYO2GQoMSWzbcMg1JgHdf LV6kfSE6vYBC482W/zTn8GooGe7nmtQ6jrTy5+TzxHtaSs4BWCOhZvXGfCfO4LdBf5eq agtwTIPA0pWx+muSTLrOdiufvTL30647/vcB1xsITB2JWuYjAisKWD9FSfSv56t9weP7 pCOsiynwn2Q7F+OHcd9AEVguYTosl1pBtK1pYgXeNByPNy8V3BxZK8ApPQvXnB0ddJHB bhlh3CReyD6ZH0eLwrdcZpAJhM6XlPayJkiPIU+Gd+Srl6xcT1BVNLS8h4CLIynFY2ug z0SQ== X-Gm-Message-State: AOJu0Yxs3Dbgl9S4vbh0kYTGAcMjOZLlrwlbw1qWTdUZi9TkhDwnpjAc PvWsQySkz++mUnsrQ+LbR2plAHdfwtVcrRhO+El47q8QWCrNYreMfTQAmbdm8t8e5V0msXCB0bR H97mo5llwZ5pbeoXxHWKpsoX5tg== 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 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 Content-Type: text/plain; charset="UTF-8" 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) -- 2.43.0.429.g432eaa2c6b-goog