lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:	Fri, 13 Jan 2012 08:36:40 +0800
From:	Huang Ying <ying.huang@...el.com>
To:	linux-kernel@...r.kernel.org
Cc:	Ingo Molnar <mingo@...e.hu>, Tony Luck <tony.luck@...el.com>,
	Borislav Petkov <bp@...64.org>,
	Chen Gong <gong.chen@...ux.intel.com>,
	Hidetoshi Seto <seto.hidetoshi@...fujitsu.com>,
	ying.huang@...el.com, Avi Kivity <avi@...hat.com>,
	Peter Zijlstra <peterz@...radead.org>
Subject: [RFC 1/2] urn, make user return notifier lockless

This makes it possible to use user return notifier in hardware error
handler, which usually has NMI like semantics such as machine check
exception handler.

The implementation is based on that of irq_work.

Because one mandatory initializer interface is added, to make patchset
bisectable, the changes to the only current user: kvm, is included in
this patch too.

Signed-off-by: Huang Ying <ying.huang@...el.com>
Cc: Avi Kivity <avi@...hat.com>
Cc: Peter Zijlstra <peterz@...radead.org>
---
 arch/x86/kvm/x86.c                   |    7 ++--
 include/linux/user-return-notifier.h |   15 +++++++--
 kernel/user-return-notifier.c        |   53 ++++++++++++++++++++++++++++-------
 3 files changed, 59 insertions(+), 16 deletions(-)

--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -178,7 +178,6 @@ static void kvm_on_user_return(struct us
 		}
 	}
 	locals->registered = false;
-	user_return_notifier_unregister(urn);
 }
 
 static void shared_msr_update(unsigned slot, u32 msr)
@@ -225,7 +224,7 @@ void kvm_set_shared_msr(unsigned slot, u
 	smsr->values[slot].curr = value;
 	wrmsrl(shared_msrs_global.msrs[slot], value);
 	if (!smsr->registered) {
-		smsr->urn.on_user_return = kvm_on_user_return;
+		init_user_return_notifier(&smsr->urn, kvm_on_user_return);
 		user_return_notifier_register(&smsr->urn);
 		smsr->registered = true;
 	}
@@ -236,8 +235,10 @@ static void drop_user_return_notifiers(v
 {
 	struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
 
-	if (smsr->registered)
+	if (smsr->registered) {
 		kvm_on_user_return(&smsr->urn);
+		user_return_notifier_unregister(&smsr->urn);
+	}
 }
 
 u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
--- a/include/linux/user-return-notifier.h
+++ b/include/linux/user-return-notifier.h
@@ -3,16 +3,25 @@
 
 #ifdef CONFIG_USER_RETURN_NOTIFIER
 
-#include <linux/list.h>
+#include <linux/llist.h>
 #include <linux/sched.h>
 
 struct user_return_notifier {
+	unsigned long flags;
 	void (*on_user_return)(struct user_return_notifier *urn);
-	struct hlist_node link;
+	struct llist_node link;
 };
 
 
-void user_return_notifier_register(struct user_return_notifier *urn);
+static inline
+void init_user_return_notifier(struct user_return_notifier *urn,
+			       void (*func)(struct user_return_notifier *))
+{
+	urn->flags = 0;
+	urn->on_user_return = func;
+}
+
+bool user_return_notifier_register(struct user_return_notifier *urn);
 void user_return_notifier_unregister(struct user_return_notifier *urn);
 
 static inline void propagate_user_return_notify(struct task_struct *prev,
--- a/kernel/user-return-notifier.c
+++ b/kernel/user-return-notifier.c
@@ -3,18 +3,26 @@
 #include <linux/percpu.h>
 #include <linux/sched.h>
 #include <linux/export.h>
+#include <linux/llist.h>
 
-static DEFINE_PER_CPU(struct hlist_head, return_notifier_list);
+/* The urn entry is claimed to be enqueued */
+#define URN_CLAIMED_BIT		0
+
+static DEFINE_PER_CPU(struct llist_head, return_notifier_list);
 
 /*
  * Request a notification when the current cpu returns to userspace.  Must be
  * called in atomic context.  The notifier will also be called in atomic
- * context.
+ * context.  Return true on success, failure when the urn entry was already
+ * enqueued by someone else.
  */
-void user_return_notifier_register(struct user_return_notifier *urn)
+bool user_return_notifier_register(struct user_return_notifier *urn)
 {
+	if (test_and_set_bit(URN_CLAIMED_BIT, &urn->flags))
+		return false;
+	llist_add(&urn->link, &__get_cpu_var(return_notifier_list));
 	set_tsk_thread_flag(current, TIF_USER_RETURN_NOTIFY);
-	hlist_add_head(&urn->link, &__get_cpu_var(return_notifier_list));
+	return true;
 }
 EXPORT_SYMBOL_GPL(user_return_notifier_register);
 
@@ -24,9 +32,27 @@ EXPORT_SYMBOL_GPL(user_return_notifier_r
  */
 void user_return_notifier_unregister(struct user_return_notifier *urn)
 {
-	hlist_del(&urn->link);
-	if (hlist_empty(&__get_cpu_var(return_notifier_list)))
-		clear_tsk_thread_flag(current, TIF_USER_RETURN_NOTIFY);
+	struct llist_head *head;
+	struct llist_node *node;
+	bool found;
+
+	head = &__get_cpu_var(return_notifier_list);
+	clear_tsk_thread_flag(current, TIF_USER_RETURN_NOTIFY);
+	node = llist_del_all(head);
+	while (node) {
+		if (&urn->link == node) {
+			found = true;
+			continue;
+		}
+		llist_add(node, head);
+		node = node->next;
+	}
+	/* The urn entry may be fired already */
+	if (!found)
+		return;
+	if (!llist_empty(head))
+		set_tsk_thread_flag(current, TIF_USER_RETURN_NOTIFY);
+	clear_bit(URN_CLAIMED_BIT, &urn->flags);
 }
 EXPORT_SYMBOL_GPL(user_return_notifier_unregister);
 
@@ -34,11 +60,18 @@ EXPORT_SYMBOL_GPL(user_return_notifier_u
 void fire_user_return_notifiers(void)
 {
 	struct user_return_notifier *urn;
-	struct hlist_node *tmp1, *tmp2;
-	struct hlist_head *head;
+	struct llist_node *node, *next;
+	struct llist_head *head;
 
 	head = &get_cpu_var(return_notifier_list);
-	hlist_for_each_entry_safe(urn, tmp1, tmp2, head, link)
+	clear_tsk_thread_flag(current, TIF_USER_RETURN_NOTIFY);
+	node = llist_del_all(head);
+	while (node) {
+		next = node->next;
+		urn = llist_entry(node, struct user_return_notifier, link);
+		clear_bit(URN_CLAIMED_BIT, &urn->flags);
 		urn->on_user_return(urn);
+		node = next;
+	}
 	put_cpu_var(return_notifier_list);
 }
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ