From: Stephen Hemminger <shemminger@osdl.org>
To: vamsi@in.ibm.com, John Levon <levon@movementarian.org>
Cc: Linus Torvalds <torvalds@transmeta.com>,
Alan Cox <alan@lxorguk.ukuu.org.uk>,
Kernel List <linux-kernel@vger.kernel.org>
Subject: [PATCH] (4/5) notifier callback mechanism - read copy update V2
Date: 19 Dec 2002 15:07:27 -0800 [thread overview]
Message-ID: <1040339247.1079.6.camel@dell_ss3.pdx.osdl.net> (raw)
In-Reply-To: <20021219181929.A5265@in.ibm.com>
Here is third try at using RCU for notifier callbacks. The difference is
that this version has a separate version for use by kernel profile that does
it's own locking and can sleep.
diff -Nru a/include/linux/notifier.h b/include/linux/notifier.h
--- a/include/linux/notifier.h Thu Dec 19 14:51:07 2002
+++ b/include/linux/notifier.h Thu Dec 19 14:51:07 2002
@@ -25,6 +25,7 @@
extern int notifier_chain_register(struct list_head *, struct notifier_block *);
extern int notifier_chain_unregister(struct list_head *, struct notifier_block *);
extern int notifier_call_chain(struct list_head *, unsigned long, void *);
+extern int notifier_call_chain_safe(struct list_head *, unsigned long, void *);
extern int register_panic_notifier(struct notifier_block *);
extern int unregister_panic_notifier(struct notifier_block *);
diff -Nru a/kernel/profile.c b/kernel/profile.c
--- a/kernel/profile.c Thu Dec 19 14:51:07 2002
+++ b/kernel/profile.c Thu Dec 19 14:51:07 2002
@@ -55,21 +55,21 @@
void profile_exit_task(struct task_struct * task)
{
down_read(&profile_rwsem);
- notifier_call_chain(&exit_task_notifier, 0, task);
+ notifier_call_chain_safe(&exit_task_notifier, 0, task);
up_read(&profile_rwsem);
}
void profile_exit_mmap(struct mm_struct * mm)
{
down_read(&profile_rwsem);
- notifier_call_chain(&exit_mmap_notifier, 0, mm);
+ notifier_call_chain_safe(&exit_mmap_notifier, 0, mm);
up_read(&profile_rwsem);
}
void profile_exec_unmap(struct mm_struct * mm)
{
down_read(&profile_rwsem);
- notifier_call_chain(&exec_unmap_notifier, 0, mm);
+ notifier_call_chain_safe(&exec_unmap_notifier, 0, mm);
up_read(&profile_rwsem);
}
diff -Nru a/kernel/sys.c b/kernel/sys.c
--- a/kernel/sys.c Thu Dec 19 14:51:07 2002
+++ b/kernel/sys.c Thu Dec 19 14:51:07 2002
@@ -22,6 +22,7 @@
#include <linux/security.h>
#include <linux/dcookies.h>
#include <linux/suspend.h>
+#include <linux/rcupdate.h>
#include <asm/uaccess.h>
#include <asm/io.h>
@@ -78,7 +79,7 @@
*/
static LIST_HEAD(reboot_notifier_list);
-static rwlock_t notifier_lock = RW_LOCK_UNLOCKED;
+static spinlock_t notifier_lock = SPIN_LOCK_UNLOCKED;
/**
* notifier_chain_register - Add notifier to a notifier chain
@@ -95,7 +96,8 @@
struct list_head *p;
INIT_LIST_HEAD(&n->link);
- write_lock(¬ifier_lock);
+
+ spin_lock(¬ifier_lock);
list_for_each(p, list) {
struct notifier_block *e
= list_entry(p, struct notifier_block, link);
@@ -104,7 +106,7 @@
}
list_add(&n->link, p);
- write_unlock(¬ifier_lock);
+ spin_unlock(¬ifier_lock);
return 0;
}
@@ -122,15 +124,18 @@
{
struct list_head *cur;
- write_lock(¬ifier_lock);
+ spin_lock(¬ifier_lock);
list_for_each(cur, list) {
if (n == list_entry(cur, struct notifier_block, link)) {
- list_del(cur);
- write_unlock(¬ifier_lock);
+ list_del_rcu(cur);
+ spin_unlock(¬ifier_lock);
+
+ synchronize_kernel();
+
return 0;
}
}
- write_unlock(¬ifier_lock);
+ spin_unlock(¬ifier_lock);
return -ENOENT;
}
@@ -148,18 +153,62 @@
* the notifier function which halted execution.
* Otherwise, the return value is the return value
* of the last notifier function called.
+ *
+ * This might be called from NMI or other context where it
+ * is impossible to sleep or spin. The restriction is that the
+ * handler must not sleep since rcu_read_lock disables preempt.
*/
-
int notifier_call_chain(struct list_head *list, unsigned long val, void *v)
{
- struct list_head *p;
+ struct list_head *p, *nxtp;
int ret = NOTIFY_DONE;
- list_for_each(p, list) {
+ rcu_read_lock();
+ list_for_each_safe_rcu(p, nxtp, list) {
+ struct notifier_block *nb =
+ list_entry(p, struct notifier_block, link);
+
+ ret = nb->notifier_call(nb,val,v);
+
+ if (ret & NOTIFY_STOP_MASK)
+ goto end_loop;
+ }
+
+ end_loop:
+ rcu_read_unlock();
+ return ret;
+}
+
+/**
+ * notifier_call_chain_safe - Call functions in a notifier chain
+ * @n: Pointer to root pointer of notifier chain
+ * @val: Value passed unmodified to notifier function
+ * @v: Pointer passed unmodified to notifier function
+ *
+ * Calls each function in a notifier chain in turn.
+ *
+ * If the return value of the notifier can be and'd
+ * with %NOTIFY_STOP_MASK, then notifier_call_chain
+ * will return immediately, with the return value of
+ * the notifier function which halted execution.
+ * Otherwise, the return value is the return value
+ * of the last notifier function called.
+ *
+ * This differs from notifier_call_chain because it assumes
+ * that the caller has done its own mutual exclusion and
+ * does not want to use read-copy-update.
+ */
+int notifier_call_chain_safe(struct list_head *list, unsigned long val, void *v)
+{
+ struct list_head *p, *nxtp;
+ int ret = NOTIFY_DONE;
+
+ list_for_each_safe(p, nxtp, list) {
struct notifier_block *nb =
list_entry(p, struct notifier_block, link);
ret = nb->notifier_call(nb,val,v);
+
if (ret & NOTIFY_STOP_MASK)
goto end_loop;
}
next prev parent reply other threads:[~2002-12-19 22:59 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-12-18 22:14 [PATCH] (4/5) improved notifier callback mechanism - read copy update Stephen Hemminger
2002-12-19 12:49 ` Vamsi Krishna S .
2002-12-19 17:37 ` Stephen Hemminger
2002-12-19 18:08 ` John Levon
2002-12-19 23:07 ` Stephen Hemminger [this message]
2002-12-19 23:09 ` [PATCH] (5/5) improved notifier callback mechanism -- remove old locking V2 Stephen Hemminger
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=1040339247.1079.6.camel@dell_ss3.pdx.osdl.net \
--to=shemminger@osdl.org \
--cc=alan@lxorguk.ukuu.org.uk \
--cc=levon@movementarian.org \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@transmeta.com \
--cc=vamsi@in.ibm.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
all inboxes | Powered by JetHome®