Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp1773549pxy; Thu, 29 Apr 2021 14:23:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyYRmNyDdTmpCJw9rjKsBOQfnNvwlBY6k41uAdCff0AfAZHJ2wqxgQ+GyqnHesubP5awIPA X-Received: by 2002:aa7:d7c7:: with SMTP id e7mr1954705eds.132.1619731408631; Thu, 29 Apr 2021 14:23:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619731408; cv=none; d=google.com; s=arc-20160816; b=iJTDTGWpDOGzdBqLsEqYKIO4CaUfqad1vM9m3Dahcac13L+3Yg+JrHVpps87QWoybX HYu58E1mk9XwqclK2JrCrw3tAUEM7J1pYShj1o+gYGRYTuaJRULs9XXMRxyQcCPDQ3co 6T51ItHnWvTPMTadNVatc3Z8B5CDh38F5zg8CcqjAWBQNCXZ6yWEdDKLdOed7h3G0ECf w3pQ0i1OOzd9K27uA88BCPWWb0/LSmFDnt57nhMMOZAiWJQ2HA9nJQN+Fo4Rck2U3FnU SCjNNeYJReXzbEPRHWwa0Frj1Z2M5Mxhhy4TI5acUaeahq7n/MFTGE6n28n4v3aylByx pWjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=IXgrcu6fpr/f6DU3zcjv8Lr+SP1VIhyvQbBITAXnt70=; b=lP67SHrcK4LlRcn8NAY0n+v/L0nKcT7K4xMK58dYqfoANRw2+bb4us9DmvLFzJZSc1 aLWJ5xasV0j+GlXbOK5CAb1VPBWrBHc0bwRqQOSqh2I6QQreCvQmMLq4taLCIQFYPoIi 8pUR6X12rpukjbJzXQOLbCAyiBOMvHKZBmD+jMbHdWt8RLXkZAUgkQU4olTAJWVREpfn NarEPDdPW7/VGQ4ZYBXeSlkFckZ7VrxqvXyYpL+zLcA75VPaTcdzlo/+pDwiUlNjfI6w oJWDjFnTKSGe1jGnbqYQGVlhPhD463e9jyCUKgBV/quNdL2GPaPTX5/RvnM/t7WjoFGo lLbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=uFdeOtrn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y25si1280915ejb.256.2021.04.29.14.23.05; Thu, 29 Apr 2021 14:23:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=uFdeOtrn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237461AbhD2VT7 (ORCPT + 99 others); Thu, 29 Apr 2021 17:19:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237509AbhD2VTv (ORCPT ); Thu, 29 Apr 2021 17:19:51 -0400 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A727FC06138F for ; Thu, 29 Apr 2021 14:19:04 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id h15-20020a37de0f0000b029029a8ada2e18so28793646qkj.11 for ; Thu, 29 Apr 2021 14:19:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=IXgrcu6fpr/f6DU3zcjv8Lr+SP1VIhyvQbBITAXnt70=; b=uFdeOtrnJ/E5UdVBYv6JOGZ37SBAXdfJGQVxQMpubPDNCJ8+IzNmupd/lPzTTkOXi8 WfSjXnIwkdi941dxgyJ+Bx4AYe2zw0hbz/JkxajApbjOajeMVhqQVFx6pkort1WPafaT ichw0tOOVFdiLfh2EcLejztdrgeg5Xv3klQUCF12znnq1LhDPsmhdfq0nRsuKP0B1jj0 AmCyjde7POqS41yFvfcWL+Mu53UW+OBN+hiJA2P/dk6/1UeeJiyzWQHbUEoNfHkP8X6u LbWKs18J4vJ/D9K77ysXQXRdRJ2KXJxgeTDT3VIHiaJEFwjPnWmBLQnSv9MH3on/GfVu 4DDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=IXgrcu6fpr/f6DU3zcjv8Lr+SP1VIhyvQbBITAXnt70=; b=tChMggTEfDa504MseqQk+9vtjWaomyxMD5SbxNAcjGWeXj4FmAnWzQYE/v2yWHod3V ZheZrANvFyxckEPW0MgA+BzYzgL3ywWjiA+3vyypXzK5s3xCinITrvuc3nxWQrR/oC80 hLYmmzSN7p9TpJO84GaOoL6W6eeI0qMRd0os1aP8r+Heeijb7yCF9F52u/x2JqwokIyW PFkDoQGfuYikuAGNbMvVQbtHwTUZULnLSwY+tZ/XqRctQGw4tLd+3Dj2VmAdGeyfx1Lr 4hzpzW/l/TkFn/Fjy34zuoKM1dsY/3lr6UCBZ1NDvNgLX6TPcwVXsQR+4TP1keLF53vo zmSg== X-Gm-Message-State: AOAM531jZgF67oAUp0VOCqqQB8KysfSaLKQGeA3BGiztP8nPCBPWfv1j 1GuKloAyVN6hd2HqvhN7evzBZTdcDY6mXV3HrQNO/2T4g7hX2FsCUtg9MDGfT1rPm9lwHJld7Q0 NsFZDeYImRp6pYuXvBbdo6IYfRudR0CRW6DY/2jlIXgKCL5M8btJ8xKXzQg6M2mIk8bsJvtGh X-Received: from bgardon.sea.corp.google.com ([2620:15c:100:202:1a18:9719:a02:56fb]) (user=bgardon job=sendgmr) by 2002:a05:6214:1bca:: with SMTP id m10mr1868439qvc.56.1619731143719; Thu, 29 Apr 2021 14:19:03 -0700 (PDT) Date: Thu, 29 Apr 2021 14:18:32 -0700 In-Reply-To: <20210429211833.3361994-1-bgardon@google.com> Message-Id: <20210429211833.3361994-7-bgardon@google.com> Mime-Version: 1.0 References: <20210429211833.3361994-1-bgardon@google.com> X-Mailer: git-send-email 2.31.1.527.g47e6f16901-goog Subject: [PATCH v2 6/7] KVM: mmu: Add slots_arch_lock for memslot arch fields From: Ben Gardon To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Peter Xu , Sean Christopherson , Peter Shier , Junaid Shahid , Jim Mattson , Yulei Zhang , Wanpeng Li , Vitaly Kuznetsov , Xiao Guangrong , Ben Gardon Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a new lock to protect the arch-specific fields of memslots if they need to be modified in a kvm->srcu read critical section. A future commit will use this lock to lazily allocate memslot rmaps for x86. Signed-off-by: Ben Gardon --- include/linux/kvm_host.h | 9 +++++++++ virt/kvm/kvm_main.c | 31 ++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 8895b95b6a22..2d5e797fbb08 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -472,6 +472,15 @@ struct kvm { #endif /* KVM_HAVE_MMU_RWLOCK */ struct mutex slots_lock; + + /* + * Protects the arch-specific fields of struct kvm_memory_slots in + * use by the VM. To be used under the slots_lock (above) or in a + * kvm->srcu read cirtical section where acquiring the slots_lock + * would lead to deadlock with the synchronize_srcu in + * install_new_memslots. + */ + struct mutex slots_arch_lock; struct mm_struct *mm; /* userspace tied to this vm */ struct kvm_memslots __rcu *memslots[KVM_ADDRESS_SPACE_NUM]; struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index c8010f55e368..97b03fa2d0c8 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -908,6 +908,7 @@ static struct kvm *kvm_create_vm(unsigned long type) mutex_init(&kvm->lock); mutex_init(&kvm->irq_lock); mutex_init(&kvm->slots_lock); + mutex_init(&kvm->slots_arch_lock); INIT_LIST_HEAD(&kvm->devices); BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX); @@ -1280,6 +1281,10 @@ static struct kvm_memslots *install_new_memslots(struct kvm *kvm, slots->generation = gen | KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS; rcu_assign_pointer(kvm->memslots[as_id], slots); + + /* Acquired in kvm_set_memslot. */ + mutex_unlock(&kvm->slots_arch_lock); + synchronize_srcu_expedited(&kvm->srcu); /* @@ -1351,6 +1356,9 @@ static int kvm_set_memslot(struct kvm *kvm, struct kvm_memslots *slots; int r; + /* Released in install_new_memslots. */ + mutex_lock(&kvm->slots_arch_lock); + slots = kvm_dup_memslots(__kvm_memslots(kvm, as_id), change); if (!slots) return -ENOMEM; @@ -1364,10 +1372,9 @@ static int kvm_set_memslot(struct kvm *kvm, slot->flags |= KVM_MEMSLOT_INVALID; /* - * We can re-use the old memslots, the only difference from the - * newly installed memslots is the invalid flag, which will get - * dropped by update_memslots anyway. We'll also revert to the - * old memslots if preparing the new memory region fails. + * We can re-use the memory from the old memslots. + * It will be overwritten with a copy of the new memslots + * after reacquiring the slots_arch_lock below. */ slots = install_new_memslots(kvm, as_id, slots); @@ -1379,6 +1386,17 @@ static int kvm_set_memslot(struct kvm *kvm, * - kvm_is_visible_gfn (mmu_check_root) */ kvm_arch_flush_shadow_memslot(kvm, slot); + + /* Released in install_new_memslots. */ + mutex_lock(&kvm->slots_arch_lock); + + /* + * The arch-specific fields of the memslots could have changed + * between releasing the slots_arch_lock in + * install_new_memslots and here, so get a fresh copy of the + * slots. + */ + kvm_copy_memslots(__kvm_memslots(kvm, as_id), slots); } r = kvm_arch_prepare_memory_region(kvm, new, mem, change); @@ -1394,8 +1412,11 @@ static int kvm_set_memslot(struct kvm *kvm, return 0; out_slots: - if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) + if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) { + slot = id_to_memslot(slots, old->id); + slot->flags &= ~KVM_MEMSLOT_INVALID; slots = install_new_memslots(kvm, as_id, slots); + } kvfree(slots); return r; } -- 2.31.1.527.g47e6f16901-goog