From: Space Meyer <spm@google.com>
To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: pbonzini@redhat.com, kpsingh@kernel.org, Space Meyer <spm@google.com>
Subject: [PATCH] KVM: Deal with nested sleeps in kvm_vcpu_block()
Date: Wed, 30 Nov 2022 16:19:46 +0000 [thread overview]
Message-ID: <20221130161946.3254953-1-spm@google.com> (raw)
Previously this code assumed nothing would mess with current->state
between the set_current_state() and schedule(). However the call to
kvm_vcpu_check_block() in between might end up requiring locks or other
actions, which would change current->state. A similar pattern was
described in the "The problem with nested sleeping primitives" LWN
article[0].
[0] https://lwn.net/Articles/628628
Signed-off-by: Space Meyer <spm@google.com>
---
virt/kvm/kvm_main.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index fab4d37905785..64e10d73f2a92 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -32,6 +32,7 @@
#include <linux/sched/signal.h>
#include <linux/sched/mm.h>
#include <linux/sched/stat.h>
+#include <linux/wait.h>
#include <linux/cpumask.h>
#include <linux/smp.h>
#include <linux/anon_inodes.h>
@@ -3426,6 +3427,7 @@ static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu)
*/
bool kvm_vcpu_block(struct kvm_vcpu *vcpu)
{
+ DEFINE_WAIT_FUNC(vcpu_block_wait, woken_wake_function);
struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
bool waited = false;
@@ -3437,13 +3439,11 @@ bool kvm_vcpu_block(struct kvm_vcpu *vcpu)
preempt_enable();
for (;;) {
- set_current_state(TASK_INTERRUPTIBLE);
-
if (kvm_vcpu_check_block(vcpu) < 0)
break;
waited = true;
- schedule();
+ wait_woken(&vcpu_block_wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
}
preempt_disable();
--
2.38.1.584.g0f3c55d4c2-goog
next reply other threads:[~2022-11-30 16:20 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-30 16:19 Space Meyer [this message]
2022-11-30 16:48 ` [PATCH] KVM: Deal with nested sleeps in kvm_vcpu_block() Paolo Bonzini
2022-12-02 15:52 ` Space Meyer
2022-12-02 18:26 ` Paolo Bonzini
2022-11-30 16:58 ` Paolo Bonzini
2022-12-02 15:52 ` Space Meyer
2022-12-02 18:27 ` Paolo Bonzini
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=20221130161946.3254953-1-spm@google.com \
--to=spm@google.com \
--cc=kpsingh@kernel.org \
--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