[PATCH 2/5] KVM: Bail from the dirty ring reset flow if a signal is pending

Sean Christopherson posted 5 patches 1 year ago
There is a newer version of this series
[PATCH 2/5] KVM: Bail from the dirty ring reset flow if a signal is pending
Posted by Sean Christopherson 1 year ago
Abort a dirty ring reset if the current task has a pending signal, as the
hard limit of INT_MAX entries doesn't ensure KVM will respond to a signal
in a timely fashion.

Fixes: fb04a1eddb1a ("KVM: X86: Implement ring-based dirty memory tracking")
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 virt/kvm/dirty_ring.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c
index 2faf894dec5a..a81ad17d5eef 100644
--- a/virt/kvm/dirty_ring.c
+++ b/virt/kvm/dirty_ring.c
@@ -117,6 +117,9 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
 	cur_slot = cur_offset = mask = 0;
 
 	while (likely((*nr_entries_reset) < INT_MAX)) {
+		if (signal_pending(current))
+			return -EINTR;
+
 		entry = &ring->dirty_gfns[ring->reset_index & (ring->size - 1)];
 
 		if (!kvm_dirty_gfn_harvested(entry))
-- 
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH 2/5] KVM: Bail from the dirty ring reset flow if a signal is pending
Posted by Yan Zhao 1 year ago
On Fri, Jan 10, 2025 at 05:04:06PM -0800, Sean Christopherson wrote:
> Abort a dirty ring reset if the current task has a pending signal, as the
> hard limit of INT_MAX entries doesn't ensure KVM will respond to a signal
> in a timely fashion.
> 
> Fixes: fb04a1eddb1a ("KVM: X86: Implement ring-based dirty memory tracking")
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
>  virt/kvm/dirty_ring.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c
> index 2faf894dec5a..a81ad17d5eef 100644
> --- a/virt/kvm/dirty_ring.c
> +++ b/virt/kvm/dirty_ring.c
> @@ -117,6 +117,9 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
>  	cur_slot = cur_offset = mask = 0;
>  
>  	while (likely((*nr_entries_reset) < INT_MAX)) {
> +		if (signal_pending(current))
> +			return -EINTR;
Will it break the userspace when a signal is pending? e.g. QEMU might report an
error like
"kvm_dirty_ring_reap_locked: Assertion `ret == total' failed".

>  		entry = &ring->dirty_gfns[ring->reset_index & (ring->size - 1)];
>  
>  		if (!kvm_dirty_gfn_harvested(entry))
> -- 
> 2.47.1.613.gc27f4b7a9f-goog
>
Re: [PATCH 2/5] KVM: Bail from the dirty ring reset flow if a signal is pending
Posted by Sean Christopherson 1 year ago
On Mon, Jan 13, 2025, Yan Zhao wrote:
> On Fri, Jan 10, 2025 at 05:04:06PM -0800, Sean Christopherson wrote:
> > Abort a dirty ring reset if the current task has a pending signal, as the
> > hard limit of INT_MAX entries doesn't ensure KVM will respond to a signal
> > in a timely fashion.
> > 
> > Fixes: fb04a1eddb1a ("KVM: X86: Implement ring-based dirty memory tracking")
> > Signed-off-by: Sean Christopherson <seanjc@google.com>
> > ---
> >  virt/kvm/dirty_ring.c | 3 +++
> >  1 file changed, 3 insertions(+)
> > 
> > diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c
> > index 2faf894dec5a..a81ad17d5eef 100644
> > --- a/virt/kvm/dirty_ring.c
> > +++ b/virt/kvm/dirty_ring.c
> > @@ -117,6 +117,9 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
> >  	cur_slot = cur_offset = mask = 0;
> >  
> >  	while (likely((*nr_entries_reset) < INT_MAX)) {
> > +		if (signal_pending(current))
> > +			return -EINTR;
> Will it break the userspace when a signal is pending? e.g. QEMU might report an
> error like
> "kvm_dirty_ring_reap_locked: Assertion `ret == total' failed".

Ugh.  In theory, yes.  In practice, I hope not?  If it's a potential problem for
QEMU, the only idea have is to only react to fatal signals by default, and then
let userspace opt-in to reacting to non-fatal signals.

> 
> >  		entry = &ring->dirty_gfns[ring->reset_index & (ring->size - 1)];
> >  
> >  		if (!kvm_dirty_gfn_harvested(entry))
> > -- 
> > 2.47.1.613.gc27f4b7a9f-goog
> >
Re: [PATCH 2/5] KVM: Bail from the dirty ring reset flow if a signal is pending
Posted by Yan Zhao 1 year ago
On Mon, Jan 13, 2025 at 07:48:46AM -0800, Sean Christopherson wrote:
> On Mon, Jan 13, 2025, Yan Zhao wrote:
> > On Fri, Jan 10, 2025 at 05:04:06PM -0800, Sean Christopherson wrote:
> > > Abort a dirty ring reset if the current task has a pending signal, as the
> > > hard limit of INT_MAX entries doesn't ensure KVM will respond to a signal
> > > in a timely fashion.
> > > 
> > > Fixes: fb04a1eddb1a ("KVM: X86: Implement ring-based dirty memory tracking")
> > > Signed-off-by: Sean Christopherson <seanjc@google.com>
> > > ---
> > >  virt/kvm/dirty_ring.c | 3 +++
> > >  1 file changed, 3 insertions(+)
> > > 
> > > diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c
> > > index 2faf894dec5a..a81ad17d5eef 100644
> > > --- a/virt/kvm/dirty_ring.c
> > > +++ b/virt/kvm/dirty_ring.c
> > > @@ -117,6 +117,9 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
> > >  	cur_slot = cur_offset = mask = 0;
> > >  
> > >  	while (likely((*nr_entries_reset) < INT_MAX)) {
> > > +		if (signal_pending(current))
> > > +			return -EINTR;
> > Will it break the userspace when a signal is pending? e.g. QEMU might report an
> > error like
> > "kvm_dirty_ring_reap_locked: Assertion `ret == total' failed".
> 
> Ugh.  In theory, yes.  In practice, I hope not?  If it's a potential problem for
> QEMU, the only idea have is to only react to fatal signals by default, and then
> let userspace opt-in to reacting to non-fatal signals.
So, what about just fatal_signal_pending() as in other ioctls in kernel?

if (fatal_signal_pending(current))
	return -EINTR;

> 
> > 
> > >  		entry = &ring->dirty_gfns[ring->reset_index & (ring->size - 1)];
> > >  
> > >  		if (!kvm_dirty_gfn_harvested(entry))
> > > -- 
> > > 2.47.1.613.gc27f4b7a9f-goog
> > > 
>
Re: [PATCH 2/5] KVM: Bail from the dirty ring reset flow if a signal is pending
Posted by Sean Christopherson 1 year ago
On Tue, Jan 14, 2025, Yan Zhao wrote:
> On Mon, Jan 13, 2025 at 07:48:46AM -0800, Sean Christopherson wrote:
> > On Mon, Jan 13, 2025, Yan Zhao wrote:
> > > On Fri, Jan 10, 2025 at 05:04:06PM -0800, Sean Christopherson wrote:
> > > > Abort a dirty ring reset if the current task has a pending signal, as the
> > > > hard limit of INT_MAX entries doesn't ensure KVM will respond to a signal
> > > > in a timely fashion.
> > > > 
> > > > Fixes: fb04a1eddb1a ("KVM: X86: Implement ring-based dirty memory tracking")
> > > > Signed-off-by: Sean Christopherson <seanjc@google.com>
> > > > ---
> > > >  virt/kvm/dirty_ring.c | 3 +++
> > > >  1 file changed, 3 insertions(+)
> > > > 
> > > > diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c
> > > > index 2faf894dec5a..a81ad17d5eef 100644
> > > > --- a/virt/kvm/dirty_ring.c
> > > > +++ b/virt/kvm/dirty_ring.c
> > > > @@ -117,6 +117,9 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
> > > >  	cur_slot = cur_offset = mask = 0;
> > > >  
> > > >  	while (likely((*nr_entries_reset) < INT_MAX)) {
> > > > +		if (signal_pending(current))
> > > > +			return -EINTR;
> > > Will it break the userspace when a signal is pending? e.g. QEMU might report an
> > > error like
> > > "kvm_dirty_ring_reap_locked: Assertion `ret == total' failed".
> > 
> > Ugh.  In theory, yes.  In practice, I hope not?  If it's a potential problem for
> > QEMU, the only idea have is to only react to fatal signals by default, and then
> > let userspace opt-in to reacting to non-fatal signals.
> So, what about just fatal_signal_pending() as in other ioctls in kernel?

Ya, though I would leave the decision up to Peter or Paolo (or someone else that
knows what QEMU wants/prefers/tolerates).

> if (fatal_signal_pending(current))
> 	return -EINTR;