From: Paolo Bonzini <pbonzini@redhat.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Pontus Fuchs <pontus.fuchs@gmail.com>,
Linus Torvalds <torvalds@linux-foundation.org>,
mingo@redhat.com,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
gleb@kernel.org
Subject: Re: [PATCH] sched,kvm: Fix KVM preempt_notifier usage
Date: Fri, 3 Jul 2015 17:38:42 +0200 [thread overview]
Message-ID: <5596AC82.6080706@redhat.com> (raw)
In-Reply-To: <5596A997.5060705@redhat.com>
On 03/07/2015 17:26, Paolo Bonzini wrote:
>
>
> On 03/07/2015 17:16, Peter Zijlstra wrote:
>> On Fri, Jul 03, 2015 at 03:17:13PM +0200, Peter Zijlstra wrote:
>>> But, could we rework the code so that you register the preempt notifier
>>> when creating the vcpu thread and leave it installed forevermore?
>>
>> OK, it looks like there is no fixed relation between a thread and a vcpu
>> :/
>>
>> Would something like the below (on top of all the others) work for you?
>
> This looks fine, but one would do the same thing without the previous
> patch, i.e. directly on top of Linus's master---right? The patch in tip
> is a red herring, and the hunks below are reverting large parts of it.
So basically this. Can you reply with SoB and maybe Acked-by?
------------- 8< ---------------
From: Peter Zijlstra <peterz@infradead.org>
Subject: [PATCH] sched, preempt_notifier: separate notifier registration from static_key inc/dec
Commit 1cde2930e154 ("sched/preempt: Add static_key() to preempt_notifiers")
had two problems. First, the preempt-notifier API needs to sleep with the
addition of the static_key, we do however need to hold off preemption
while modifying the preempt notifier list, otherwise a preemption could
observe an inconsistent list state. KVM correctly registers and
unregisters preempt notifiers with preemption disabled, so the sleep
caused dmesg splats.
Second, KVM registers and unregisters preemption notifiers very often
(in vcpu_load/vcpu_put). With a single uniprocessor guest the static key
would move between 0 and 1 continuously, hitting the slow path on every
userspace exit.
To fix this, wrap the static_key inc/dec in a new API, and call it from
KVM.
Fixes: 1cde2930e154 ("sched/preempt: Add static_key() to preempt_notifiers")
Reported-by: Pontus Fuchs <pontus.fuchs@gmail.com>
Reported-by: Takashi Iwai <tiwai@suse.de>
Not-signed-off-by: Peter Zijlstra <peterz@infradead.org>
Not-acked-by: Peter Zijlstra <peterz@infradead.org>
Not-signed-off-by: Paolo Bonzini <pbonzini@redhat.com
diff --git a/include/linux/preempt.h b/include/linux/preempt.h
index 0f1534acaf60..84991f185173 100644
--- a/include/linux/preempt.h
+++ b/include/linux/preempt.h
@@ -293,6 +293,8 @@ struct preempt_notifier {
struct preempt_ops *ops;
};
+void preempt_notifier_inc(void);
+void preempt_notifier_dec(void);
void preempt_notifier_register(struct preempt_notifier *notifier);
void preempt_notifier_unregister(struct preempt_notifier *notifier);
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index b803e1b8ab0c..552710ab19e0 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2320,13 +2320,27 @@ void wake_up_new_task(struct task_struct *p)
static struct static_key preempt_notifier_key = STATIC_KEY_INIT_FALSE;
+void preempt_notifier_inc(void)
+{
+ static_key_slow_inc(&preempt_notifier_key);
+}
+EXPORT_SYMBOL_GPL(preempt_notifier_inc);
+
+void preempt_notifier_dec(void)
+{
+ static_key_slow_dec(&preempt_notifier_key);
+}
+EXPORT_SYMBOL_GPL(preempt_notifier_dec);
+
/**
* preempt_notifier_register - tell me when current is being preempted & rescheduled
* @notifier: notifier struct to register
*/
void preempt_notifier_register(struct preempt_notifier *notifier)
{
- static_key_slow_inc(&preempt_notifier_key);
+ if (!static_key_false(&preempt_notifier_key))
+ WARN(1, "registering preempt_notifier while notifiers disabled\n");
+
hlist_add_head(¬ifier->link, ¤t->preempt_notifiers);
}
EXPORT_SYMBOL_GPL(preempt_notifier_register);
@@ -2340,7 +2354,6 @@ EXPORT_SYMBOL_GPL(preempt_notifier_register);
void preempt_notifier_unregister(struct preempt_notifier *notifier)
{
hlist_del(¬ifier->link);
- static_key_slow_dec(&preempt_notifier_key);
}
EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 848af90b8091..8b8a44453670 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -553,6 +553,8 @@ static struct kvm *kvm_create_vm(unsigned long type)
list_add(&kvm->vm_list, &vm_list);
spin_unlock(&kvm_lock);
+ preempt_notifier_inc();
+
return kvm;
out_err:
@@ -620,6 +622,7 @@ static void kvm_destroy_vm(struct kvm *kvm)
cleanup_srcu_struct(&kvm->irq_srcu);
cleanup_srcu_struct(&kvm->srcu);
kvm_arch_free_vm(kvm);
+ preempt_notifier_dec();
hardware_disable_all();
mmdrop(mm);
}
next prev parent reply other threads:[~2015-07-03 15:38 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-06-25 12:00 Regression: sched/preempt: Add static_key() to preempt_notifiers breaks my KVM Pontus Fuchs
2015-06-25 12:09 ` Peter Zijlstra
2015-06-25 12:15 ` Pontus Fuchs
2015-06-25 12:55 ` [PATCH] sched,kvm: Fix KVM preempt_notifier usage Peter Zijlstra
2015-06-30 11:10 ` [tip:sched/urgent] sched/preempt, kvm: " tip-bot for Peter Zijlstra
2015-07-03 11:23 ` Paolo Bonzini
2015-07-03 11:12 ` [PATCH] sched,kvm: " Paolo Bonzini
2015-07-03 12:19 ` Peter Zijlstra
2015-07-03 12:31 ` Paolo Bonzini
2015-07-03 13:17 ` Peter Zijlstra
2015-07-03 15:16 ` Peter Zijlstra
2015-07-03 15:26 ` Paolo Bonzini
2015-07-03 15:38 ` Paolo Bonzini [this message]
2015-07-03 15:42 ` Peter Zijlstra
2015-07-03 15:46 ` Paolo Bonzini
2015-07-03 15:57 ` Takashi Iwai
2015-06-30 13:47 ` Regression: sched/preempt: Add static_key() to preempt_notifiers breaks my KVM Josh Boyer
2015-07-01 6:55 ` Ingo Molnar
2015-07-03 13:15 ` Takashi Iwai
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=5596AC82.6080706@redhat.com \
--to=pbonzini@redhat.com \
--cc=gleb@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=pontus.fuchs@gmail.com \
--cc=torvalds@linux-foundation.org \
/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