All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Gavin Shan <gshan@redhat.com>
Cc: shuah@kernel.org, kvm@vger.kernel.org, maz@kernel.org,
	bgardon@google.com, andrew.jones@linux.dev, dmatlack@google.com,
	shan.gavin@gmail.com, catalin.marinas@arm.com,
	kvmarm@lists.linux.dev, pbonzini@redhat.com, zhenyzha@redhat.com,
	will@kernel.org, kvmarm@lists.cs.columbia.edu
Subject: Re: [PATCH v6 1/8] KVM: x86: Introduce KVM_REQ_RING_SOFT_FULL
Date: Fri, 21 Oct 2022 23:48:18 +0000	[thread overview]
Message-ID: <Y1Mvwq5PJ0gxC+47@google.com> (raw)
In-Reply-To: <85d15a4a-bbae-c5e6-f6dc-1d972d07dafb@redhat.com>

On Sat, Oct 22, 2022, Gavin Shan wrote:
> > > When dirty ring becomes full, the VCPU can't handle any operations, which will
> > > bring more dirty pages.
> > 
> > Right, but there's a buffer of 64 entries on top of what the CPU can buffer (VMX's
> > PML can buffer 512 entries).  Hence the "soft full".  If x86 is already on the
> > edge of exhausting that buffer, i.e. can fill 64 entries while handling requests,
> > than we need to increase the buffer provided by the soft limit because sooner or
> > later KVM will be able to fill 65 entries, at which point errors will occur
> > regardless of when the "soft full" request is processed.
> > 
> > In other words, we can take advantage of the fact that the soft-limit buffer needs
> > to be quite conservative.
> > 
> 
> Right, there are extra 64 entries in the ring between soft full and hard full.
> Another 512 entries are reserved when PML is enabled. However, the other requests,
> who produce dirty pages, are producers to the ring. We can't just have the assumption
> that those producers will need less than 64 entries.

But we're already assuming those producers will need less than 65 entries.  My point
is that if one (or even five) extra entries pushes KVM over the limit, then the
buffer provided by the soft limit needs to be jacked up regardless of when the
request is processed.

Hmm, but I suppose it's possible there's a pathological emulator path that can push
double digit entries, and servicing the request right away ensures that requests
have the full 64 entry buffer to play with.

So yeah, I agree, move it below the DEAD check, but keep it above most everything
else.

> > > > Would it make sense to clear the request in kvm_dirty_ring_reset()?  I don't care
> > > > about the overhead of having to re-check the request, the goal would be to help
> > > > document what causes the request to go away.
> > > > 
> > > > E.g. modify kvm_dirty_ring_reset() to take @vcpu and then do:
> > > > 
> > > > 	if (!kvm_dirty_ring_soft_full(ring))
> > > > 		kvm_clear_request(KVM_REQ_RING_SOFT_FULL, vcpu);
> > > > 
> > > 
> > > It's reasonable to clear KVM_REQ_DIRTY_RING_SOFT_FULL when the ring is reseted.
> > > @vcpu can be achieved by container_of(..., ring).
> > 
> > Using container_of() is silly, there's literally one caller that does:
> > 
> > 	kvm_for_each_vcpu(i, vcpu, kvm)
> > 		cleared += kvm_dirty_ring_reset(vcpu->kvm, &vcpu->dirty_ring);
> > 
> 
> May I ask why it's silly by using container_of()?

Because container_of() is inherently dangerous, e.g. if it's used on a pointer that
isn't contained by the expected type, the code will compile cleanly but explode
at runtime.  That's unlikely to happen in this case, e.g. doesn't look like we'll
be adding a ring to "struct kvm", but if someone wanted to add a per-VM ring,
taking the vCPU makes it very obvious that pushing to a ring _requires_ a vCPU,
and enforces that requirement at compile time.

In other words, it's preferable to avoid container_of() unless using it solves a
real problem that doesn't have a better alternative.

In these cases, passing in the vCPU is most definitely a better alternative as
each of the functions in question has a sole caller that has easy access to the
container (vCPU), i.e. it's a trivial change.

> In order to avoid using container_of(), kvm_dirty_ring_push() also need
> @vcpu.

Yep, that one should be changed too.

> So lets change those two functions to something like below. Please
> double-check if they looks good to you?
> 
>   void kvm_dirty_ring_push(struct kvm_vcpu *vcpu, u32 slot, u64 offset);
>   int kvm_dirty_ring_reset(struct kvm_vcpu *vcpu);

Yep, looks good.
_______________________________________________
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

WARNING: multiple messages have this Message-ID (diff)
From: Sean Christopherson <seanjc@google.com>
To: Gavin Shan <gshan@redhat.com>
Cc: kvmarm@lists.linux.dev, kvmarm@lists.cs.columbia.edu,
	kvm@vger.kernel.org, peterx@redhat.com, maz@kernel.org,
	will@kernel.org, catalin.marinas@arm.com, bgardon@google.com,
	shuah@kernel.org, andrew.jones@linux.dev, dmatlack@google.com,
	pbonzini@redhat.com, zhenyzha@redhat.com, james.morse@arm.com,
	suzuki.poulose@arm.com, alexandru.elisei@arm.com,
	oliver.upton@linux.dev, shan.gavin@gmail.com
Subject: Re: [PATCH v6 1/8] KVM: x86: Introduce KVM_REQ_RING_SOFT_FULL
Date: Fri, 21 Oct 2022 23:48:18 +0000	[thread overview]
Message-ID: <Y1Mvwq5PJ0gxC+47@google.com> (raw)
Message-ID: <20221021234818.A7IZLMvluM_Hvz9oAO_7kB7vTYhTZxD2PHOOldPtMMY@z> (raw)
In-Reply-To: <85d15a4a-bbae-c5e6-f6dc-1d972d07dafb@redhat.com>

On Sat, Oct 22, 2022, Gavin Shan wrote:
> > > When dirty ring becomes full, the VCPU can't handle any operations, which will
> > > bring more dirty pages.
> > 
> > Right, but there's a buffer of 64 entries on top of what the CPU can buffer (VMX's
> > PML can buffer 512 entries).  Hence the "soft full".  If x86 is already on the
> > edge of exhausting that buffer, i.e. can fill 64 entries while handling requests,
> > than we need to increase the buffer provided by the soft limit because sooner or
> > later KVM will be able to fill 65 entries, at which point errors will occur
> > regardless of when the "soft full" request is processed.
> > 
> > In other words, we can take advantage of the fact that the soft-limit buffer needs
> > to be quite conservative.
> > 
> 
> Right, there are extra 64 entries in the ring between soft full and hard full.
> Another 512 entries are reserved when PML is enabled. However, the other requests,
> who produce dirty pages, are producers to the ring. We can't just have the assumption
> that those producers will need less than 64 entries.

But we're already assuming those producers will need less than 65 entries.  My point
is that if one (or even five) extra entries pushes KVM over the limit, then the
buffer provided by the soft limit needs to be jacked up regardless of when the
request is processed.

Hmm, but I suppose it's possible there's a pathological emulator path that can push
double digit entries, and servicing the request right away ensures that requests
have the full 64 entry buffer to play with.

So yeah, I agree, move it below the DEAD check, but keep it above most everything
else.

> > > > Would it make sense to clear the request in kvm_dirty_ring_reset()?  I don't care
> > > > about the overhead of having to re-check the request, the goal would be to help
> > > > document what causes the request to go away.
> > > > 
> > > > E.g. modify kvm_dirty_ring_reset() to take @vcpu and then do:
> > > > 
> > > > 	if (!kvm_dirty_ring_soft_full(ring))
> > > > 		kvm_clear_request(KVM_REQ_RING_SOFT_FULL, vcpu);
> > > > 
> > > 
> > > It's reasonable to clear KVM_REQ_DIRTY_RING_SOFT_FULL when the ring is reseted.
> > > @vcpu can be achieved by container_of(..., ring).
> > 
> > Using container_of() is silly, there's literally one caller that does:
> > 
> > 	kvm_for_each_vcpu(i, vcpu, kvm)
> > 		cleared += kvm_dirty_ring_reset(vcpu->kvm, &vcpu->dirty_ring);
> > 
> 
> May I ask why it's silly by using container_of()?

Because container_of() is inherently dangerous, e.g. if it's used on a pointer that
isn't contained by the expected type, the code will compile cleanly but explode
at runtime.  That's unlikely to happen in this case, e.g. doesn't look like we'll
be adding a ring to "struct kvm", but if someone wanted to add a per-VM ring,
taking the vCPU makes it very obvious that pushing to a ring _requires_ a vCPU,
and enforces that requirement at compile time.

In other words, it's preferable to avoid container_of() unless using it solves a
real problem that doesn't have a better alternative.

In these cases, passing in the vCPU is most definitely a better alternative as
each of the functions in question has a sole caller that has easy access to the
container (vCPU), i.e. it's a trivial change.

> In order to avoid using container_of(), kvm_dirty_ring_push() also need
> @vcpu.

Yep, that one should be changed too.

> So lets change those two functions to something like below. Please
> double-check if they looks good to you?
> 
>   void kvm_dirty_ring_push(struct kvm_vcpu *vcpu, u32 slot, u64 offset);
>   int kvm_dirty_ring_reset(struct kvm_vcpu *vcpu);

Yep, looks good.

  reply	other threads:[~2022-10-21 23:48 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-11  6:14 [PATCH v6 0/8] KVM: arm64: Enable ring-based dirty memory tracking Gavin Shan
2022-10-11  6:14 ` Gavin Shan
2022-10-11  6:14 ` [PATCH v6 1/8] KVM: x86: Introduce KVM_REQ_RING_SOFT_FULL Gavin Shan
2022-10-11  6:14   ` Gavin Shan
2022-10-20 22:42   ` Sean Christopherson
2022-10-20 22:42     ` Sean Christopherson
2022-10-21  5:54     ` Gavin Shan
2022-10-21  5:54       ` Gavin Shan
2022-10-21 15:25       ` Sean Christopherson
2022-10-21 15:25         ` Sean Christopherson
2022-10-21 23:03         ` Gavin Shan
2022-10-21 23:03           ` Gavin Shan
2022-10-21 23:48           ` Sean Christopherson [this message]
2022-10-21 23:48             ` Sean Christopherson
2022-10-22  0:16             ` Gavin Shan
2022-10-22  0:16               ` Gavin Shan
2022-10-11  6:14 ` [PATCH v6 2/8] KVM: x86: Move declaration of kvm_cpu_dirty_log_size() to kvm_dirty_ring.h Gavin Shan
2022-10-11  6:14   ` Gavin Shan
2022-10-11  6:14 ` [PATCH v6 3/8] KVM: Add support for using dirty ring in conjunction with bitmap Gavin Shan
2022-10-11  6:14   ` Gavin Shan
2022-10-18 16:07   ` Peter Xu
2022-10-18 16:07     ` Peter Xu
2022-10-18 22:20     ` Gavin Shan
2022-10-18 22:20       ` Gavin Shan
2022-10-20 18:58       ` Oliver Upton
2022-10-20 18:58         ` Oliver Upton
2022-10-20 23:44   ` Sean Christopherson
2022-10-20 23:44     ` Sean Christopherson
2022-10-21  8:06     ` Marc Zyngier
2022-10-21  8:06       ` Marc Zyngier
2022-10-21 16:05       ` Sean Christopherson
2022-10-21 16:05         ` Sean Christopherson
2022-10-22  8:27         ` Gavin Shan
2022-10-22  8:27           ` Gavin Shan
2022-10-22 10:54           ` Marc Zyngier
2022-10-22 10:54             ` Marc Zyngier
2022-10-22 10:33         ` Marc Zyngier
2022-10-22 10:33           ` Marc Zyngier
2022-10-24 23:50           ` Sean Christopherson
2022-10-24 23:50             ` Sean Christopherson
2022-10-25  0:08             ` Sean Christopherson
2022-10-25  0:08               ` Sean Christopherson
2022-10-25  0:24             ` Oliver Upton
2022-10-25  0:24               ` Oliver Upton
2022-10-25  7:31               ` Marc Zyngier
2022-10-25  7:31                 ` Marc Zyngier
2022-10-25 17:47                 ` Sean Christopherson
2022-10-25 17:47                   ` Sean Christopherson
2022-10-27  8:29                   ` Marc Zyngier
2022-10-27  8:29                     ` Marc Zyngier
2022-10-27 17:44                     ` Sean Christopherson
2022-10-27 17:44                       ` Sean Christopherson
2022-10-27 18:30                       ` Marc Zyngier
2022-10-27 18:30                         ` Marc Zyngier
2022-10-27 19:09                         ` Sean Christopherson
2022-10-27 19:09                           ` Sean Christopherson
2022-10-28  6:43                         ` Gavin Shan
2022-10-28  6:43                           ` Gavin Shan
2022-10-28 16:51                           ` Sean Christopherson
2022-10-28 16:51                             ` Sean Christopherson
2022-10-31  3:37                             ` Gavin Shan
2022-10-31  3:37                               ` Gavin Shan
2022-10-31  9:08                             ` Marc Zyngier
2022-10-31  9:08                               ` Marc Zyngier
2022-10-31 22:48                               ` Gavin Shan
2022-10-31 22:48                                 ` Gavin Shan
2022-10-25  7:22             ` Marc Zyngier
2022-10-25  7:22               ` Marc Zyngier
2022-10-21 10:13     ` Gavin Shan
2022-10-21 10:13       ` Gavin Shan
2022-10-21 23:20       ` Sean Christopherson
2022-10-21 23:20         ` Sean Christopherson
2022-10-22  0:33         ` Gavin Shan
2022-10-22  0:33           ` Gavin Shan
2022-10-11  6:14 ` [PATCH v6 4/8] KVM: arm64: Enable ring-based dirty memory tracking Gavin Shan
2022-10-11  6:14   ` Gavin Shan
2022-10-11  6:14 ` [PATCH v6 5/8] KVM: selftests: Enable KVM_CAP_DIRTY_LOG_RING_WITH_BITMAP if possible Gavin Shan
2022-10-11  6:14   ` Gavin Shan
2022-10-11  6:14 ` [PATCH v6 6/8] KVM: selftests: Use host page size to map ring buffer in dirty_log_test Gavin Shan
2022-10-11  6:14   ` Gavin Shan
2022-10-11  6:14 ` [PATCH v6 7/8] KVM: selftests: Clear dirty ring states between two modes " Gavin Shan
2022-10-11  6:14   ` Gavin Shan
2022-10-11  6:14 ` [PATCH v6 8/8] KVM: selftests: Automate choosing dirty ring size " Gavin Shan
2022-10-11  6:14   ` Gavin Shan
2022-10-11  6:23 ` [PATCH v6 0/8] KVM: arm64: Enable ring-based dirty memory tracking Gavin Shan
2022-10-11  6:23   ` Gavin Shan

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=Y1Mvwq5PJ0gxC+47@google.com \
    --to=seanjc@google.com \
    --cc=andrew.jones@linux.dev \
    --cc=bgardon@google.com \
    --cc=catalin.marinas@arm.com \
    --cc=dmatlack@google.com \
    --cc=gshan@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.cs.columbia.edu \
    --cc=kvmarm@lists.linux.dev \
    --cc=maz@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=shan.gavin@gmail.com \
    --cc=shuah@kernel.org \
    --cc=will@kernel.org \
    --cc=zhenyzha@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.