public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, dmatlack@google.com
Subject: Re: [PATCH] KVM: x86: avoid memslot check in NX hugepage recovery if it cannot be true
Date: Fri, 18 Nov 2022 00:37:15 +0000	[thread overview]
Message-ID: <Y3bTu4/nUfpX+Enm@google.com> (raw)
In-Reply-To: <20221117173109.3126912-1-pbonzini@redhat.com>

On Thu, Nov 17, 2022, Paolo Bonzini wrote:
> +		if (atomic_read(&kvm->nr_logpage_memslots)) {

Can we use something like nr_dirty_logged_memslots?  logpage doesn't precisely
capture the "dirty log" aspect, e.g. for a (very brief) second I though this was
log(nr_memslot_pages).

> +			slot = gfn_to_memslot(kvm, sp->gfn);
> +			WARN_ON_ONCE(!slot);
> +		}
> +
>  		if (slot && kvm_slot_dirty_track_enabled(slot))
>  			unaccount_nx_huge_page(kvm, sp);
>  		else if (is_tdp_mmu_page(sp))
> diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
> index e6e66c5e56f2..b3c2b975e737 100644
> --- a/include/linux/kvm_host.h
> +++ b/include/linux/kvm_host.h
> @@ -722,6 +722,11 @@ struct kvm {
>  	/* The current active memslot set for each address space */
>  	struct kvm_memslots __rcu *memslots[KVM_ADDRESS_SPACE_NUM];
>  	struct xarray vcpu_array;
> +	/*
> +	 * Protected by slots_lock, but can be read outside if an
> +	 * incorrect answer is acceptable.
> +	 */
> +	atomic_t nr_logpage_memslots;
>  
>  	/* Used to wait for completion of MMU notifiers.  */
>  	spinlock_t mn_invalidate_lock;
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 43bbe4fde078..7670ebd29bcf 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -1627,6 +1627,11 @@ static int kvm_prepare_memory_region(struct kvm *kvm,

This needs to be done in the commit stage, e.g. if kvm_arch_prepare_memory_region()
fails the count will be all kinds of wrong.  Even better, since this seems to be
x86-centric, put it in kvm_mmu_slot_apply_flags() under the

	if ((old_flags ^ new_flags) & KVM_MEM_LOG_DIRTY_PAGES)

to avoid atomic operations if dirty logging isn't being toggled.  That would also
deal with the NULL pointer issues David pointed out.

>  		}
>  	}
>  
> +	atomic_set(&kvm->nr_logpage_memslots,
> +		   atomic_read(&kvm->nr_logpage_memslots)
> +		   + !!(new->flags & KVM_MEM_LOG_DIRTY_PAGES)
> +		   - !!(old->flags & KVM_MEM_LOG_DIRTY_PAGES));

I belive this can be:

	atomic_add(+ !!(new_flags & KVM_MEM_LOG_DIRTY_PAGES)
		   - !!(old_flags & KVM_MEM_LOG_DIRTY_PAGES), ...);

or less weirdly...

	if ((old_flags ^ new_flags) & KVM_MEM_LOG_DIRTY_PAGES) {
		...

		if (new_flags & KVM_MEM_LOG_DIRTY_PAGES)
			atomic_inc(...);
		else
			atomic_dec(...);
	}

      parent reply	other threads:[~2022-11-18  0:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-17 17:31 [PATCH] KVM: x86: avoid memslot check in NX hugepage recovery if it cannot be true Paolo Bonzini
2022-11-17 18:16 ` David Matlack
2022-11-18  0:37 ` Sean Christopherson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y3bTu4/nUfpX+Enm@google.com \
    --to=seanjc@google.com \
    --cc=dmatlack@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox