From: Matt Helsley This patch simplifies the process events code by factoring many of the common pieces into the task watcher notifier function. This factoring was enabled by switching to task watchers instead of calling process events functions directly. Signed-off-by: Matt Helsley Cc: Guillaume Thouvenin DESC Process events: Fix biarch compatibility EDESC From: Matt Helsley Switch timestamp to two 32-bit scalars instead of two long fields of a timespec struct. This fixes an issue with biarch systems where the kernel was sending two 64-bit fields and a naive 32-bit userspace program was expecting two 32-bit fields. Naive userspace applications that used the timespec directly are broken. This allows more of the naieve apps to work correctly and all apps that are correct to continue to work. I submitted a different solution as an RFC earlier and have since switched to the solution recommended by Evgeniy Polyakov. Compiles with linux-2.6.17-mm6, boots, and tested with and without -m32 on x86-64. Signed-off-by: Matt Helsley Cc: Evgeniy Polyakov Cc: Guillaume Thouvenin Signed-off-by: Andrew Morton --- drivers/connector/cn_proc.c | 126 ++++++++++------------------------ linux/cn_proc.h | 0 2 files changed, 39 insertions(+), 87 deletions(-) diff -puN drivers/connector/cn_proc.c~task-watchers-refactor-process-events drivers/connector/cn_proc.c --- a/drivers/connector/cn_proc.c~task-watchers-refactor-process-events +++ a/drivers/connector/cn_proc.c @@ -47,71 +47,30 @@ static inline void get_seq(__u32 *ts, in put_cpu_var(proc_event_counts); } -static void proc_fork_connector(struct task_struct *task) +static inline void proc_fork_connector(struct task_struct *task, + struct proc_event *ev) { - struct cn_msg *msg; - struct proc_event *ev; - __u8 buffer[CN_PROC_MSG_SIZE]; - struct timespec ts; - - if (atomic_read(&proc_event_num_listeners) < 1) - return; - - msg = (struct cn_msg*)buffer; - ev = (struct proc_event*)msg->data; - get_seq(&msg->seq, &ev->cpu); - ktime_get_ts(&ts); /* get high res monotonic timestamp */ - ev->timestamp_ns = timespec_to_ns(&ts); ev->what = PROC_EVENT_FORK; ev->event_data.fork.parent_pid = task->real_parent->pid; ev->event_data.fork.parent_tgid = task->real_parent->tgid; ev->event_data.fork.child_pid = task->pid; ev->event_data.fork.child_tgid = task->tgid; - - memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); - msg->ack = 0; /* not used */ - msg->len = sizeof(*ev); - /* If cn_netlink_send() failed, the data is not sent */ - cn_netlink_send(msg, CN_IDX_PROC, GFP_ATOMIC); } -static void proc_exec_connector(struct task_struct *task) +static inline void proc_exec_connector(struct task_struct *task, + struct proc_event *ev) { - struct cn_msg *msg; - struct proc_event *ev; - struct timespec ts; - __u8 buffer[CN_PROC_MSG_SIZE]; - - if (atomic_read(&proc_event_num_listeners) < 1) - return; - - msg = (struct cn_msg*)buffer; - ev = (struct proc_event*)msg->data; - get_seq(&msg->seq, &ev->cpu); - ktime_get_ts(&ts); /* get high res monotonic timestamp */ - ev->timestamp_ns = timespec_to_ns(&ts); ev->what = PROC_EVENT_EXEC; ev->event_data.exec.process_pid = task->pid; ev->event_data.exec.process_tgid = task->tgid; - - memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); - msg->ack = 0; /* not used */ - msg->len = sizeof(*ev); - cn_netlink_send(msg, CN_IDX_PROC, GFP_ATOMIC); } -static void proc_id_connector(struct task_struct *task, int which_id) +static inline void proc_id_connector(struct task_struct *task, int which_id, + struct proc_event *ev) { - struct cn_msg *msg; - struct proc_event *ev; - __u8 buffer[CN_PROC_MSG_SIZE]; - struct timespec ts; - - if (atomic_read(&proc_event_num_listeners) < 1) - return; - - msg = (struct cn_msg*)buffer; - ev = (struct proc_event*)msg->data; + ev->what = which_id; + ev->event_data.id.process_pid = task->pid; + ev->event_data.id.process_tgid = task->tgid; ev->what = which_id; ev->event_data.id.process_pid = task->pid; ev->event_data.id.process_tgid = task->tgid; @@ -121,43 +80,18 @@ static void proc_id_connector(struct tas } else if (which_id == PROC_EVENT_GID) { ev->event_data.id.r.rgid = task->gid; ev->event_data.id.e.egid = task->egid; - } else - return; - get_seq(&msg->seq, &ev->cpu); - ktime_get_ts(&ts); /* get high res monotonic timestamp */ - ev->timestamp_ns = timespec_to_ns(&ts); - - memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); - msg->ack = 0; /* not used */ - msg->len = sizeof(*ev); - cn_netlink_send(msg, CN_IDX_PROC, GFP_ATOMIC); + } + WARN_ON((which_id != PROC_EVENT_UID) && (which_id != PROC_EVENT_GID)); } -static void proc_exit_connector(struct task_struct *task) +static inline void proc_exit_connector(struct task_struct *task, + struct proc_event *ev) { - struct cn_msg *msg; - struct proc_event *ev; - __u8 buffer[CN_PROC_MSG_SIZE]; - struct timespec ts; - - if (atomic_read(&proc_event_num_listeners) < 1) - return; - - msg = (struct cn_msg*)buffer; - ev = (struct proc_event*)msg->data; - get_seq(&msg->seq, &ev->cpu); - ktime_get_ts(&ts); /* get high res monotonic timestamp */ - ev->timestamp_ns = timespec_to_ns(&ts); ev->what = PROC_EVENT_EXIT; ev->event_data.exit.process_pid = task->pid; ev->event_data.exit.process_tgid = task->tgid; ev->event_data.exit.exit_code = task->exit_code; ev->event_data.exit.exit_signal = task->exit_signal; - - memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); - msg->ack = 0; /* not used */ - msg->len = sizeof(*ev); - cn_netlink_send(msg, CN_IDX_PROC, GFP_ATOMIC); } /* @@ -228,30 +162,48 @@ static void cn_proc_mcast_ctl(void *data static int cn_proc_watch_task(struct notifier_block *nb, unsigned long val, void *t) { + struct timespec ts; struct task_struct *task = t; + struct cn_msg *msg; + struct proc_event *ev; + __u8 buffer[CN_PROC_MSG_SIZE]; int rc = NOTIFY_OK; + if (atomic_read(&proc_event_num_listeners) < 1) + return rc; + + msg = (struct cn_msg*)buffer; + ev = (struct proc_event*)msg->data; switch (get_watch_event(val)) { case WATCH_TASK_CLONE: - proc_fork_connector(task); + proc_fork_connector(task, ev); break; case WATCH_TASK_EXEC: - proc_exec_connector(task); + proc_exec_connector(task, ev); break; case WATCH_TASK_UID: - proc_id_connector(task, PROC_EVENT_UID); + proc_id_connector(task, PROC_EVENT_UID, ev); break; case WATCH_TASK_GID: - proc_id_connector(task, PROC_EVENT_GID); + proc_id_connector(task, PROC_EVENT_GID, ev); break; case WATCH_TASK_EXIT: - proc_exit_connector(task); + proc_exit_connector(task, ev); break; - default: /* we don't care about WATCH_TASK_INIT|FREE because we - don't keep per-task info */ - rc = NOTIFY_DONE; /* ignore all other notifications */ + default: /* ignore WATCH_TASK_INIT|FREE - we don't keep per-task info */ + rc = NOTIFY_DONE; break; } + if (rc != NOTIFY_OK) + return rc; + get_seq(&msg->seq, &ev->cpu); + ktime_get_ts(&ts); /* get high res monotonic timestamp */ + ev->timestamp_ns = timespec_to_ns(&ts); + memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); + msg->ack = 0; /* not used */ + msg->len = sizeof(*ev); + cn_netlink_send(msg, CN_IDX_PROC, GFP_ATOMIC); + /* If cn_netlink_send() fails, drop data */ return rc; } diff -puN include/linux/cn_proc.h~task-watchers-refactor-process-events include/linux/cn_proc.h _