Received: by 2002:a05:7412:2a91:b0:fc:a2b0:25d7 with SMTP id u17csp56480rdh; Tue, 13 Feb 2024 09:13:23 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUWCJAP1KwMr2Lh1oXD4DwadzfjJUHAKoG62HRpSBSD4Ci7e6zMy2WPE9NBBeZwZig1rBQZSIIGq8nYc1kFYd4omAd2ZhHQ3zCjuv+bIA== X-Google-Smtp-Source: AGHT+IG/HKpCOjpjSC7+5ETx7QgVVBp2IaEx7eWIF2l2u5vyGXc6NNpAmDb29v2wpnDuuVxlCCvh X-Received: by 2002:a17:906:1b19:b0:a3c:8ae5:36fb with SMTP id o25-20020a1709061b1900b00a3c8ae536fbmr5151475ejg.54.1707844403037; Tue, 13 Feb 2024 09:13:23 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707844403; cv=pass; d=google.com; s=arc-20160816; b=cg0j8ZMrSLBV8W5HPGiQ6+ZtmBgWtVO/nhpGL9XT5pNcw+2EhZS1tNZYHwAE2SkUGl mw4v+V7woh8UeqZtpRxb49AX8coJcgV9LsuFMCuJwn7SdsxZL0cCUY8QZbqbzVBosIKF EK16qYEncvM6d0OdQFE+KHn4oN6oeLGkqpTGGdMfyPC7tfoGNmdQIuzNzGZSMTefTx1E d5c06CSMFlnwJIlJ+O0xHCriEKD7Y59zH6zIeasaXl4lYTrJwsmtRRA0vv0KQDvWOZd5 BQzhVjL5mgvzbKilNHYBvO5xaRtY38iO6hrR6xTKGwGh/bFf+byILIFmNnubkHGGpSmH y5+w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=7NseTxscz5dqu1awIczEokA/c80peECIvMUkxkuD+nY=; fh=pyoMOENMD2A3Q4qA5/L3GmpY7F5xYiO8WwQhlbbKXJ4=; b=sFQuV36zu6RucCtQilB4PIR/zOaoV1qOy+sJNGEeQ3WquT0ds2Dri5vFtcNeAkPMNP M/djcWBmTqgv4W0d0Yhqu4mR3goS+fz3aUYWIG5OnWTEJ6UnLylj6QApx7XwW0HdNkOW c0ix4Sxg51S3ewykR9S4N66QsfJ649Jtz30dyJTrOljjF0gXijGp3vzEfLbI8reivAVS 79fLB+pAAZzO8rLN+8HdCUOYReQ2tUnslY6+EkISrh+HOTRDO0Aj9lR8PX/6Hn2EW7zP tYxnMsPKCL+fi+WGY7L9kZGFLPkP8/IciC0gOpwOtkV1/0AdrzjTKioylGWL634a+vuZ O4KQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=dl55hXqo; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-63968-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63968-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCU+PkT9t3G8KiR1vs3QKJSd612omex7n066AL7MZzVwTBWN6SdbU5fm4osBiQWbevljHL1w7v/+y/TZhC94yX4vLTjx3WZr1kUl9pcHKw== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id x17-20020a1709064bd100b00a3cedc4cf4asi1117456ejv.908.2024.02.13.09.13.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 09:13:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-63968-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=dl55hXqo; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-63968-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63968-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 98C521F2690F for ; Tue, 13 Feb 2024 17:13:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3D3E25FDC4; Tue, 13 Feb 2024 17:13:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="dl55hXqo" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 5EA9B5FB95 for ; Tue, 13 Feb 2024 17:13:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707844389; cv=none; b=TX8ejkab4u+Qp7SHQxdDkbznF+zU7mV5lraR1yjSuJikb03ep+5qcS4OPia4inaxW5pEdHJBsorQijLjoop6l39TCHprdxeHEfVlzT3iiuf2U6O1Ew6RG+CBEXGbcTyRiKcGgY+pitGHY05AaN752pt/bDestkMB6c2HLb2yLOk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707844389; c=relaxed/simple; bh=h4cB5JHd8AcNVaeM9c7rZQN4CcuQCnphSNrUX1ZKPI8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lT7INdKYIWwBaztqkZyKk8XoYEeKdTM7ZJvpQgIlIOwxWp+2oRRupAuOHagDVtp6O3DILXhfTnQ1CLwtSUOSMacXeTWjcPn4k8OkrY2BLrQT+8orfSClDPSlIZcaWHU7n9YV9XUrHhnzrsKqAgOc6EGh0WRfh/5DU3yr/9h9mUM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=dl55hXqo; arc=none smtp.client-ip=209.85.128.202 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--seanjc.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-607838c0800so10337607b3.1 for ; Tue, 13 Feb 2024 09:13:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707844386; x=1708449186; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7NseTxscz5dqu1awIczEokA/c80peECIvMUkxkuD+nY=; b=dl55hXqo6n9BeI5DdLkGoh0o2zzUJ+AOixuQUCAO5H0VzjKgZhBKLxqOB033/uQTwM bR0hO7LLY/A/8x7o02JqRDEa22Q0rgowyAO38uUY5VG7/bPYqrGT9koJ9TXzTbWAEBnW XHbHMyNdD5kn3K5QK2IakqU8k/Mzjs7K2LOPVqbH048Cf//yun56jycHKW9DI1almal8 VYddFfQNcMNpD6qA3NOQz2KfMb8QdItTzeWTpqWLnES15dUwYG35MFx5zzD2NlpfVrvi 9Pxs/hBTa3uJiYp38GzJrOqzR+RTLCXAbZeJJZq3Ov/NuIaF8/daJ1BJ0wD/GvlBXzia 7Amw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707844386; x=1708449186; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7NseTxscz5dqu1awIczEokA/c80peECIvMUkxkuD+nY=; b=It+tfJTUj7Jb9On102hyHypo8DPT5xZzC7Q36R9xTKQ2gzEwgTvTjPQsoxEXLY+pYo jHqC3IdGeGYT/4kiVJFV57627+wREZDN8cn4zhWbW1ehjlISc9FYxPtfvGLq1EQXb92/ ncznLTc4vcC6+P2VVQ0lhL4GVz0202BlLwmy0uQ0e3RvTSl7W4ZyHzRgisuHoi8vGoQz i6b7XLuwHAki8W+PlV8DTVrDted8tS3fht79yGxRACw2F55iecXyPkqCOcQRp8UmZa+Y 1OF4XuukKwV4s+USpoGcBWWg7uqOtEoyY/DoSiRZ3dn1oIQrXCCBSgXBPDU2lYeQ6DSb ZhYQ== X-Forwarded-Encrypted: i=1; AJvYcCUoMoGmEalG36NPIz4sZ2x02C4xSgvWPAKjVF36ATyRQcAdk0Kzsw2wFi2ZrWBgrbaY+FD5wrzZAJ0719UdGTmRADzQ0FyE5H/fFAaH X-Gm-Message-State: AOJu0YyYtF6tviuDNsnXyQgfXqkOU6V6gNrFOzdO1xRmbfnOSkr6/7wb 7K5ych9QUTYbS/NxDBDl8OpQRcCPEx0HjCGbe1RlD8L4B1SAWcg4qj639uxwkmbvwjyGvW/1HlC W8w== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1890:b0:dbd:ee44:8908 with SMTP id cj16-20020a056902189000b00dbdee448908mr20517ybb.0.1707844386397; Tue, 13 Feb 2024 09:13:06 -0800 (PST) Date: Tue, 13 Feb 2024 09:13:04 -0800 In-Reply-To: <20240206153405.489531-1-avagin@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240206153405.489531-1-avagin@google.com> Message-ID: Subject: Re: [PATCH v2] kvm/x86: allocate the write-tracking metadata on-demand From: Sean Christopherson To: Andrei Vagin Cc: Paolo Bonzini , linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, Zhenyu Wang Content-Type: text/plain; charset="us-ascii" On Tue, Feb 06, 2024, Andrei Vagin wrote: > The write-track is used externally only by the gpu/drm/i915 driver. > Currently, it is always enabled, if a kernel has been compiled with this > driver. > > Enabling the write-track mechanism adds a two-byte overhead per page across > all memory slots. It isn't significant for regular VMs. However in gVisor, > where the entire process virtual address space is mapped into the VM, even > with a 39-bit address space, the overhead amounts to 256MB. > > This change rework the write-tracking mechanism to enable it on-demand > in kvm_page_track_register_notifier. Don't use "this change", "this patch", or any other variant of "this blah" that you come up with. :-) Simply phrase the changelog as a command. > Here is Sean's comment about the locking scheme: > > The only potential hiccup would be if taking slots_arch_lock would > deadlock, but it should be impossible for slots_arch_lock to be taken in > any other path that involves VFIO and/or KVMGT *and* can be coincident. > Except for kvm_arch_destroy_vm() (which deletes KVM's internal > memslots), slots_arch_lock is taken only through KVM ioctls(), and the > caller of kvm_page_track_register_notifier() *must* hold a reference to > the VM. > > Cc: Paolo Bonzini > Cc: Sean Christopherson > Cc: Zhenyu Wang > Suggested-by: Sean Christopherson > Signed-off-by: Andrei Vagin > --- > v1: https://lore.kernel.org/lkml/ZcErs9rPqT09nNge@google.com/T/ > v2: allocate the write-tracking metadata on-demand > > arch/x86/include/asm/kvm_host.h | 2 + > arch/x86/kvm/mmu/mmu.c | 24 +++++------ > arch/x86/kvm/mmu/page_track.c | 74 ++++++++++++++++++++++++++++----- > arch/x86/kvm/mmu/page_track.h | 3 +- > 4 files changed, 78 insertions(+), 25 deletions(-) > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h > index d271ba20a0b2..c35641add93c 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -1503,6 +1503,8 @@ struct kvm_arch { > */ > #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1) > struct kvm_mmu_memory_cache split_desc_cache; > + > + bool page_write_tracking_enabled; Rather than a generic page_write_tracking_enabled, I think it makes sense to explicitly track if there are *external* write tracking users. One could argue it makes the total tracking *too* fine grained, but I think it would be helpful for readers to when KVM itself is using write tracking (shadow paging) versus when KVM has write tracking enabled, but has not allocated rmaps (external write tracking user). That way, kernels with CONFIG_KVM_EXTERNAL_WRITE_TRACKING=n don't need to check the bool (though they'll still check kvm_shadow_root_allocated()). And as a bonus, the diff is quite a bit smaller. > diff --git a/arch/x86/kvm/mmu/page_track.c b/arch/x86/kvm/mmu/page_track.c > index c87da11f3a04..a4790b0a6f50 100644 > --- a/arch/x86/kvm/mmu/page_track.c > +++ b/arch/x86/kvm/mmu/page_track.c > @@ -20,10 +20,14 @@ > #include "mmu_internal.h" > #include "page_track.h" > > -bool kvm_page_track_write_tracking_enabled(struct kvm *kvm) > +static bool kvm_page_track_write_tracking_enabled(struct kvm *kvm) > { > - return IS_ENABLED(CONFIG_KVM_EXTERNAL_WRITE_TRACKING) || > - !tdp_enabled || kvm_shadow_root_allocated(kvm); > + /* > + * Read page_write_tracking_enabled before related pointers. Pairs with > + * smp_store_release in kvm_page_track_write_tracking_enable. > + */ > + return smp_load_acquire(&kvm->arch.page_write_tracking_enabled) | Needs to be a logical ||, not a bitwise |. > @@ -161,12 +204,21 @@ int kvm_page_track_register_notifier(struct kvm *kvm, > struct kvm_page_track_notifier_node *n) > { > struct kvm_page_track_notifier_head *head; > + int r; > > if (!kvm || kvm->mm != current->mm) > return -ESRCH; > > kvm_get_kvm(kvm); > > + mutex_lock(&kvm->slots_arch_lock); This can and should check if write tracking is enabled without taking the mutex. I *highly* doubt it will matter in practice, especially since KVM-GT is the only user of the external tracking, and attaching a vGPU is a one-time thing. But it's a cheap an easy optimization that also makes the code look more like the shadow_root_allocated, i.e. makes it easier to grok that the two flows are doing very similar things. > + r = kvm_page_track_write_tracking_enable(kvm); I'd prefer to call this helper kvm_enable_external_write_tracking(). As is, I had hard time seein which flows were calling enable() versus enabled(). > + mutex_unlock(&kvm->slots_arch_lock); > + if (r) { > + kvm_put_kvm(kvm); Allocate write tracking before kvm_get_kvm(), then there's no need to have an error handling path. All in all, this? Compile tested only. --- arch/x86/include/asm/kvm_host.h | 9 +++++ arch/x86/kvm/mmu/page_track.c | 68 ++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index ad5319a503f0..af857a899f85 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1467,6 +1467,15 @@ struct kvm_arch { */ bool shadow_root_allocated; +#ifdef CONFIG_KVM_EXTERNAL_WRITE_TRACKING + /* + * If set, the VM has (or had) an external write tracking user, and + * thus all write tracking metadata has been allocated, even if KVM + * itself isn't using write tracking. + */ + bool external_write_tracking_enabled; +#endif + #if IS_ENABLED(CONFIG_HYPERV) hpa_t hv_root_tdp; spinlock_t hv_root_tdp_lock; diff --git a/arch/x86/kvm/mmu/page_track.c b/arch/x86/kvm/mmu/page_track.c index c87da11f3a04..6fb61b33675f 100644 --- a/arch/x86/kvm/mmu/page_track.c +++ b/arch/x86/kvm/mmu/page_track.c @@ -20,10 +20,23 @@ #include "mmu_internal.h" #include "page_track.h" +static bool kvm_external_write_tracking_enabled(struct kvm *kvm) +{ +#ifdef CONFIG_KVM_EXTERNAL_WRITE_TRACKING + /* + * Read external_write_tracking_enabled before related pointers. Pairs + * with the smp_store_release in kvm_page_track_write_tracking_enable(). + */ + return smp_load_acquire(&kvm->arch.external_write_tracking_enabled); +#else + return false; +#endif +} + bool kvm_page_track_write_tracking_enabled(struct kvm *kvm) { - return IS_ENABLED(CONFIG_KVM_EXTERNAL_WRITE_TRACKING) || - !tdp_enabled || kvm_shadow_root_allocated(kvm); + return kvm_external_write_tracking_enabled(kvm) || + kvm_shadow_root_allocated(kvm) || !tdp_enabled; } void kvm_page_track_free_memslot(struct kvm_memory_slot *slot) @@ -153,6 +166,50 @@ int kvm_page_track_init(struct kvm *kvm) return init_srcu_struct(&head->track_srcu); } +static int kvm_enable_external_write_tracking(struct kvm *kvm) +{ + struct kvm_memslots *slots; + struct kvm_memory_slot *slot; + int r = 0, i, bkt; + + mutex_lock(&kvm->slots_arch_lock); + + /* + * Check for *any* write tracking user (not just external users) under + * lock. This avoids unnecessary work, e.g. if KVM itself is using + * write tracking, or if two external users raced when registering. + */ + if (kvm_page_track_write_tracking_enabled(kvm)) + goto out_success; + + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { + slots = __kvm_memslots(kvm, i); + kvm_for_each_memslot(slot, bkt, slots) { + /* + * Intentionally do NOT free allocations on failure to + * avoid having to track which allocations were made + * now versus when the memslot was created. The + * metadata is guaranteed to be freed when the slot is + * freed, and will be kept/used if userspace retries + * the failed ioctl() instead of killing the VM. + */ + r = kvm_page_track_write_tracking_alloc(slot); + if (r) + goto out_unlock; + } + } + + /* + * Ensure that external_write_tracking_enabled becomes true strictly + * after all the related pointers are set. + */ +out_success: + smp_store_release(&kvm->arch.external_write_tracking_enabled, true); +out_unlock: + mutex_unlock(&kvm->slots_arch_lock); + return r; +} + /* * register the notifier so that event interception for the tracked guest * pages can be received. @@ -161,10 +218,17 @@ int kvm_page_track_register_notifier(struct kvm *kvm, struct kvm_page_track_notifier_node *n) { struct kvm_page_track_notifier_head *head; + int r; if (!kvm || kvm->mm != current->mm) return -ESRCH; + if (!kvm_external_write_tracking_enabled(kvm)) { + r = kvm_enable_external_write_tracking(kvm); + if (r) + return r; + } + kvm_get_kvm(kvm); head = &kvm->arch.track_notifier_head; base-commit: 7455665a3521aa7b56245c0a2810f748adc5fdd4 --