From: Matt Helsley This introduces a second, per-task, blocking notifier chain. The per-task chain offers watchers the chance to register with a specific task nstead of all tasks. It also allows the watcher to associate a block of data with the task by wrapping the notifier block using containerof(). Both the global, all-tasks chain and the per-task chain are called from the same function. The two types of chains share the same set of notification values, however registration functions and the registered notifier blocks must be separate. These notifiers are only safe if notifier blocks are registered with the current task while in the context of the current task. This ensures that there are no races between registration, unregistration, and notification. Signed-off-by: Matt Helsley Cc: Jes Sorensen Cc: Chandra S. Seetharaman Cc: Alan Stern DESC task-watchers-add-support-for-per-task-watchers-warning-fix EDESC From: Andrew Morton Needs fixing for real. Cc: Matt Helsley Cc: Jes Sorensen Cc: Chandra S. Seetharaman Cc: Alan Stern Signed-off-by: Andrew Morton --- include/linux/init_task.h | 2 ++ include/linux/notifier.h | 2 ++ include/linux/sched.h | 2 ++ kernel/sys.c | 31 ++++++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 1 deletion(-) diff -puN include/linux/init_task.h~task-watchers-add-support-for-per-task-watchers include/linux/init_task.h --- a/include/linux/init_task.h~task-watchers-add-support-for-per-task-watchers +++ a/include/linux/init_task.h @@ -7,6 +7,7 @@ #include #include #include +#include #define INIT_FDTABLE \ { \ @@ -140,6 +141,7 @@ extern struct group_info init_groups; .pi_lock = SPIN_LOCK_UNLOCKED, \ INIT_TRACE_IRQFLAGS \ INIT_LOCKDEP \ + .notify = RAW_NOTIFIER_INIT(tsk.notify), \ } diff -puN include/linux/notifier.h~task-watchers-add-support-for-per-task-watchers include/linux/notifier.h --- a/include/linux/notifier.h~task-watchers-add-support-for-per-task-watchers +++ a/include/linux/notifier.h @@ -156,6 +156,8 @@ extern int raw_notifier_call_chain(struc extern int register_task_watcher(struct notifier_block *nb); extern int unregister_task_watcher(struct notifier_block *nb); +extern int register_per_task_watcher(struct notifier_block *nb); +extern int unregister_per_task_watcher(struct notifier_block *nb); #define WATCH_FLAGS_MASK ((-1) ^ 0x0FFFFUL) #define get_watch_event(v) ({ ((v) & ~WATCH_FLAGS_MASK); }) #define get_watch_flags(v) ({ ((v) & WATCH_FLAGS_MASK); }) diff -puN include/linux/sched.h~task-watchers-add-support-for-per-task-watchers include/linux/sched.h --- a/include/linux/sched.h~task-watchers-add-support-for-per-task-watchers +++ a/include/linux/sched.h @@ -1026,6 +1026,8 @@ struct task_struct { atomic_t fs_excl; /* holding fs exclusive resources */ struct rcu_head rcu; + struct raw_notifier_head notify; /* generic per-task notifications */ + /* * cache last used pipe for splice */ diff -puN kernel/sys.c~task-watchers-add-support-for-per-task-watchers kernel/sys.c --- a/kernel/sys.c~task-watchers-add-support-for-per-task-watchers +++ a/kernel/sys.c @@ -448,9 +448,38 @@ int unregister_task_watcher(struct notif } EXPORT_SYMBOL_GPL(unregister_task_watcher); +static inline int notify_per_task_watchers(unsigned int val, + struct task_struct *task) +{ + if (get_watch_event(val) != WATCH_TASK_INIT) + return raw_notifier_call_chain(&task->notify, val, task); + RAW_INIT_NOTIFIER_HEAD(&task->notify); + if (task->real_parent) + return raw_notifier_call_chain(&task->real_parent->notify, + val, task); + return 0; +} + +int register_per_task_watcher(struct notifier_block *nb) +{ + return raw_notifier_chain_register(¤t->notify, nb); +} +EXPORT_SYMBOL_GPL(register_per_task_watcher); + +int unregister_per_task_watcher(struct notifier_block *nb) +{ + return raw_notifier_chain_unregister(¤t->notify, nb); +} +EXPORT_SYMBOL_GPL(unregister_per_task_watcher); + int notify_watchers(unsigned long val, void *v) { - return blocking_notifier_call_chain(&task_watchers, val, v); + int retval; + + retval = blocking_notifier_call_chain(&task_watchers, val, v); + if (retval & NOTIFY_STOP_MASK) + return retval; + return notify_per_task_watchers(val, v); } static int set_one_prio(struct task_struct *p, int niceval, int error) _