From 8c3b1bca4398e3c7c1112524f34c04fcdae567ae Mon Sep 17 00:00:00 2001 From: Geert Uytterhoeven Date: Sat, 24 May 2003 21:34:02 -0700 Subject: [PATCH] HAVE_ARCH_GET_SIGNAL_TO_DELIVER warning Kill warning about unused static functions if HAVE_ARCH_GET_SIGNAL_TO_DELIVER is defined. --- kernel/signal.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'kernel') diff --git a/kernel/signal.c b/kernel/signal.c index d15a55ec9e5e..3560d7da7032 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1346,6 +1346,9 @@ do_notify_parent_cldstop(struct task_struct *tsk, struct task_struct *parent) spin_unlock_irqrestore(&sighand->siglock, flags); } + +#ifndef HAVE_ARCH_GET_SIGNAL_TO_DELIVER + static void finish_stop(int stop_count) { @@ -1460,9 +1463,6 @@ do_signal_stop(int signr) finish_stop(stop_count); } - -#ifndef HAVE_ARCH_GET_SIGNAL_TO_DELIVER - /* * Do appropriate magic when group_stop_count > 0. * We return nonzero if we stopped, after releasing the siglock. -- cgit v1.2.3 From 7149345c76a810e3fb8cc9b58706027c310497b3 Mon Sep 17 00:00:00 2001 From: Ingo Molnar Date: Sat, 24 May 2003 21:50:32 -0700 Subject: [PATCH] support "requeueing" futexes This addresses a futex related SMP scalability problem of glibc. A number of regressions have been reported to the NTPL mailing list when going to many CPUs, for applications that use condition variables and the pthread_cond_broadcast() API call. Using this functionality, testcode shows a slowdown from 0.12 seconds runtime to over 237 seconds (!) runtime, on 4-CPU systems. pthread condition variables use two futex-backed mutex-alike locks: an internal one for the glibc CV state itself, and a user-supplied mutex which the API guarantees to take in certain codepaths. (Unfortunately the user-supplied mutex cannot be used to protect the CV state, so we've got to deal with two locks.) The cause of the slowdown is a 'swarm effect': if lots of threads are blocked on a condition variable, and pthread_cond_broadcast() is done, then glibc first does a FUTEX_WAKE on the cv-internal mutex, then down a mutex_down() on the user-supplied mutex. Ie. a swarm of threads is created which all race to serialize on the user-supplied mutex. The more threads are used, the more likely it becomes that the scheduler will balance them over to other CPUs - where they just schedule, try to lock the mutex, and go to sleep. This 'swarm effect' is purely technical, a side-effect of glibc's use of futexes, and the imperfect coupling of the two locks. the solution to this problem is to not wake up the swarm of threads, but 'requeue' them from the CV-internal mutex to the user-supplied mutex. The attached patch adds the FUTEX_REQUEUE feature FUTEX_REQUEUE requeues N threads from futex address A to futex address B. This way glibc can wake up a single thread (which will take the user-mutex), and can requeue the rest, with a single system-call. Ulrich Drepper has implemented FUTEX_REQUEUE support in glibc, and a number of people have tested it over the past couple of weeks. Here are the measurements done by Saurabh Desai: System: 4xPIII 700MHz ./cond-perf -r 100 -n 200: 1p 2p 4p Default NPTL: 0.120s 0.211s 237.407s requeue NPTL: 0.124s 0.156s 0.040s ./cond-perf -r 1000 -n 100: Default NPTL: 0.276s 0.412s 0.530s requeue NPTL: 0.349s 0.503s 0.550s ./pp -v -n 128 -i 1000 -S 32768: Default NPTL: 128 games in 1.111s 1.270s 16.894s requeue NPTL: 128 games in 1.111s 1.959s 2.426s ./pp -v -n 1024 -i 10 -S 32768: Default NPTL: 1024 games in 0.181s 0.394s incompleted 2m+ requeue NPTL: 1024 games in 0.166s 0.254s 0.341s the speedup with increasing number of threads is quite significant, in the 128 threads, case it's more than 8 times. In the cond-perf test, on 4 CPUs it's almost infinitely faster than the 'swarm of threads' catastrophy triggered by the old code. --- include/linux/futex.h | 8 ++- kernel/compat.c | 9 ++- kernel/fork.c | 2 +- kernel/futex.c | 156 ++++++++++++++++++++++++++++++++++++++------------ 4 files changed, 134 insertions(+), 41 deletions(-) (limited to 'kernel') diff --git a/include/linux/futex.h b/include/linux/futex.h index b91878c07352..c76dd1ee3076 100644 --- a/include/linux/futex.h +++ b/include/linux/futex.h @@ -2,10 +2,16 @@ #define _LINUX_FUTEX_H /* Second argument to futex syscall */ + + #define FUTEX_WAIT (0) #define FUTEX_WAKE (1) #define FUTEX_FD (2) +#define FUTEX_REQUEUE (3) + + +asmlinkage long sys_futex(u32 __user *uaddr, int op, int val, + struct timespec __user *utime, u32 __user *uaddr2); -extern asmlinkage long sys_futex(u32 __user *uaddr, int op, int val, struct timespec __user *utime); #endif diff --git a/kernel/compat.c b/kernel/compat.c index 0dcab5fc6acd..87c3005e8f58 100644 --- a/kernel/compat.c +++ b/kernel/compat.c @@ -211,20 +211,25 @@ asmlinkage long compat_sys_sigprocmask(int how, compat_old_sigset_t *set, return ret; } -extern long do_futex(unsigned long, int, int, unsigned long); +extern long do_futex(unsigned long uaddr, int op, int val, + unsigned long timeout, unsigned long uaddr2, int val2); asmlinkage long compat_sys_futex(u32 *uaddr, int op, int val, struct compat_timespec *utime) { struct timespec t; unsigned long timeout = MAX_SCHEDULE_TIMEOUT; + int val2 = 0; if ((op == FUTEX_WAIT) && utime) { if (get_compat_timespec(&t, utime)) return -EFAULT; timeout = timespec_to_jiffies(&t) + 1; } - return do_futex((unsigned long)uaddr, op, val, timeout); + if (op == FUTEX_REQUEUE) + val2 = (int) utime; + + return do_futex((unsigned long)uaddr, op, val, timeout, uaddr2, val2); } asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit *rlim); diff --git a/kernel/fork.c b/kernel/fork.c index a509e6da132f..23c6d34f800f 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -457,7 +457,7 @@ void mm_release(struct task_struct *tsk, struct mm_struct *mm) * not set up a proper pointer then tough luck. */ put_user(0, tidptr); - sys_futex(tidptr, FUTEX_WAKE, 1, NULL); + sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL); } } diff --git a/kernel/futex.c b/kernel/futex.c index ce5c894e459e..df2dcbf557d0 100644 --- a/kernel/futex.c +++ b/kernel/futex.c @@ -2,6 +2,9 @@ * Fast Userspace Mutexes (which I call "Futexes!"). * (C) Rusty Russell, IBM 2002 * + * Generalized futexes, futex requeueing, misc fixes by Ingo Molnar + * (C) Copyright 2003 Red Hat Inc, All Rights Reserved + * * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly * enough at me, Linus for the original (flawed) idea, Matthew * Kirkwood for proof-of-concept implementation. @@ -9,9 +12,6 @@ * "The futexes are also cursed." * "But they come in a choice of three flavours!" * - * Generalized futexes for every mapping type, Ingo Molnar, 2002 - * - * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or @@ -93,19 +93,18 @@ static inline struct list_head *hash_futex(struct page *page, int offset) FUTEX_HASHBITS)]; } -/* Waiter either waiting in FUTEX_WAIT or poll(), or expecting signal */ -static inline void tell_waiter(struct futex_q *q) -{ - wake_up_all(&q->waiters); - if (q->filp) - send_sigio(&q->filp->f_owner, q->fd, POLL_IN); -} - /* * Get kernel address of the user page and pin it. * * Must be called with (and returns with) all futex-MM locks held. */ +static inline struct page *__pin_page_atomic (struct page *page) +{ + if (!PageReserved(page)) + get_page(page); + return page; +} + static struct page *__pin_page(unsigned long addr) { struct mm_struct *mm = current->mm; @@ -116,11 +115,8 @@ static struct page *__pin_page(unsigned long addr) * Do a quick atomic lookup first - this is the fastpath. */ page = follow_page(mm, addr, 0); - if (likely(page != NULL)) { - if (!PageReserved(page)) - get_page(page); - return page; - } + if (likely(page != NULL)) + return __pin_page_atomic(page); /* * No luck - need to fault in the page: @@ -150,16 +146,11 @@ repeat_lookup: return page; } -static inline void unpin_page(struct page *page) -{ - put_page(page); -} - /* * Wake up all waiters hashed on the physical page that is mapped * to this virtual address: */ -static int futex_wake(unsigned long uaddr, int offset, int num) +static inline int futex_wake(unsigned long uaddr, int offset, int num) { struct list_head *i, *next, *head; struct page *page; @@ -181,7 +172,9 @@ static int futex_wake(unsigned long uaddr, int offset, int num) if (this->page == page && this->offset == offset) { list_del_init(i); __detach_vcache(&this->vcache); - tell_waiter(this); + wake_up_all(&this->waiters); + if (this->filp) + send_sigio(&this->filp->f_owner, this->fd, POLL_IN); ret++; if (ret >= num) break; @@ -189,7 +182,7 @@ static int futex_wake(unsigned long uaddr, int offset, int num) } unlock_futex_mm(); - unpin_page(page); + put_page(page); return ret; } @@ -208,7 +201,9 @@ static void futex_vcache_callback(vcache_t *vcache, struct page *new_page) spin_lock(&futex_lock); if (!list_empty(&q->list)) { + put_page(q->page); q->page = new_page; + __pin_page_atomic(new_page); list_del(&q->list); list_add_tail(&q->list, head); } @@ -216,6 +211,65 @@ static void futex_vcache_callback(vcache_t *vcache, struct page *new_page) spin_unlock(&futex_lock); } +/* + * Requeue all waiters hashed on one physical page to another + * physical page. + */ +static inline int futex_requeue(unsigned long uaddr1, int offset1, + unsigned long uaddr2, int offset2, int nr_wake, int nr_requeue) +{ + struct list_head *i, *next, *head1, *head2; + struct page *page1 = NULL, *page2 = NULL; + int ret = 0; + + lock_futex_mm(); + + page1 = __pin_page(uaddr1 - offset1); + if (!page1) + goto out; + page2 = __pin_page(uaddr2 - offset2); + if (!page2) + goto out; + + head1 = hash_futex(page1, offset1); + head2 = hash_futex(page2, offset2); + + list_for_each_safe(i, next, head1) { + struct futex_q *this = list_entry(i, struct futex_q, list); + + if (this->page == page1 && this->offset == offset1) { + list_del_init(i); + __detach_vcache(&this->vcache); + if (++ret <= nr_wake) { + wake_up_all(&this->waiters); + if (this->filp) + send_sigio(&this->filp->f_owner, + this->fd, POLL_IN); + } else { + put_page(this->page); + __pin_page_atomic (page2); + list_add_tail(i, head2); + __attach_vcache(&this->vcache, uaddr2, + current->mm, futex_vcache_callback); + this->offset = offset2; + this->page = page2; + if (ret - nr_wake >= nr_requeue) + break; + } + } + } + +out: + unlock_futex_mm(); + + if (page1) + put_page(page1); + if (page2) + put_page(page2); + + return ret; +} + static inline void __queue_me(struct futex_q *q, struct page *page, unsigned long uaddr, int offset, int fd, struct file *filp) @@ -252,7 +306,7 @@ static inline int unqueue_me(struct futex_q *q) return ret; } -static int futex_wait(unsigned long uaddr, +static inline int futex_wait(unsigned long uaddr, int offset, int val, unsigned long time) @@ -273,14 +327,17 @@ static int futex_wait(unsigned long uaddr, } __queue_me(&q, page, uaddr, offset, -1, NULL); - unlock_futex_mm(); - - /* Page is pinned, but may no longer be in this address space. */ + /* + * Page is pinned, but may no longer be in this address space. + * It cannot schedule, so we access it with the spinlock held. + */ if (get_user(curval, (int *)uaddr) != 0) { + unlock_futex_mm(); ret = -EFAULT; goto out; } if (curval != val) { + unlock_futex_mm(); ret = -EWOULDBLOCK; goto out; } @@ -288,13 +345,15 @@ static int futex_wait(unsigned long uaddr, * The get_user() above might fault and schedule so we * cannot just set TASK_INTERRUPTIBLE state when queueing * ourselves into the futex hash. This code thus has to - * rely on the FUTEX_WAKE code doing a wakeup after removing + * rely on the futex_wake() code doing a wakeup after removing * the waiter from the list. */ add_wait_queue(&q.waiters, &wait); set_current_state(TASK_INTERRUPTIBLE); - if (!list_empty(&q.list)) + if (!list_empty(&q.list)) { + unlock_futex_mm(); time = schedule_timeout(time); + } set_current_state(TASK_RUNNING); /* * NOTE: we don't remove ourselves from the waitqueue because @@ -310,7 +369,7 @@ out: /* Were we woken up anyway? */ if (!unqueue_me(&q)) ret = 0; - unpin_page(page); + put_page(q.page); return ret; } @@ -320,7 +379,7 @@ static int futex_close(struct inode *inode, struct file *filp) struct futex_q *q = filp->private_data; unqueue_me(q); - unpin_page(q->page); + put_page(q->page); kfree(filp->private_data); return 0; } @@ -416,11 +475,12 @@ static int futex_fd(unsigned long uaddr, int offset, int signal) page = NULL; out: if (page) - unpin_page(page); + put_page(page); return ret; } -long do_futex(unsigned long uaddr, int op, int val, unsigned long timeout) +long do_futex(unsigned long uaddr, int op, int val, unsigned long timeout, + unsigned long uaddr2, int val2) { unsigned long pos_in_page; int ret; @@ -442,23 +502,45 @@ long do_futex(unsigned long uaddr, int op, int val, unsigned long timeout) /* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */ ret = futex_fd(uaddr, pos_in_page, val); break; + case FUTEX_REQUEUE: + { + unsigned long pos_in_page2 = uaddr2 % PAGE_SIZE; + + /* Must be "naturally" aligned */ + if (pos_in_page2 % sizeof(u32)) + return -EINVAL; + + ret = futex_requeue(uaddr, pos_in_page, uaddr2, pos_in_page2, + val, val2); + break; + } default: - ret = -EINVAL; + ret = -ENOSYS; } return ret; } -asmlinkage long sys_futex(u32 __user *uaddr, int op, int val, struct timespec __user *utime) + +asmlinkage long sys_futex(u32 __user *uaddr, int op, int val, + struct timespec __user *utime, u32 __user *uaddr2) { struct timespec t; unsigned long timeout = MAX_SCHEDULE_TIMEOUT; + int val2 = 0; if ((op == FUTEX_WAIT) && utime) { if (copy_from_user(&t, utime, sizeof(t)) != 0) return -EFAULT; timeout = timespec_to_jiffies(&t) + 1; } - return do_futex((unsigned long)uaddr, op, val, timeout); + /* + * requeue parameter in 'utime' if op == FUTEX_REQUEUE. + */ + if (op == FUTEX_REQUEUE) + val2 = (int) utime; + + return do_futex((unsigned long)uaddr, op, val, timeout, + (unsigned long)uaddr2, val2); } static struct super_block * -- cgit v1.2.3 From 055e188d5ce5d5edd5d916a0cd549ebb195388a3 Mon Sep 17 00:00:00 2001 From: Andrew Morton Date: Sun, 25 May 2003 01:10:37 -0700 Subject: [PATCH] Fix dcache_lock/tasklist_lock ranking bug __unhash_process acquires the dcache_lock while holding the tasklist_lock for writing. This can deadlock. Additionally, fs/proc/base.c incorrectly assumed that p->pid would be set to 0 during release_task. The patch fixes that by adding a new spinlock to the task structure and fixing all references to (!p->pid). The alternative to the new spinlock would be to hold dcache_lock around __unhash_process. - fs/proc/base.c assumed that p->pid is reset to 0 during exit. This is not the case anymore. I now look at the count of the pid structure for PIDTYPE_PID. - de_thread now tested - as broken as it was before: open handles to /proc/ are either stale or invalid after an exec of a nptl process, if the exec was call from a secondary thread. - a few lock_kernels removed - that part of /proc doesn't need it. - additional instances of 'if(current->pid)' replaced with pid_alive. --- fs/exec.c | 37 +++-------- fs/proc/base.c | 154 ++++++++++++++++++++++++++++++++-------------- fs/proc/root.c | 2 +- include/linux/init_task.h | 1 + include/linux/proc_fs.h | 2 + include/linux/sched.h | 2 + kernel/exit.c | 40 ++++-------- 7 files changed, 135 insertions(+), 103 deletions(-) (limited to 'kernel') diff --git a/fs/exec.c b/fs/exec.c index e214e30ca525..9bbd3b4e76dc 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -529,30 +529,6 @@ static int exec_mmap(struct mm_struct *mm) return 0; } -static struct dentry *clean_proc_dentry(struct task_struct *p) -{ - struct dentry *proc_dentry = p->proc_dentry; - - if (proc_dentry) { - spin_lock(&dcache_lock); - if (!d_unhashed(proc_dentry)) { - dget_locked(proc_dentry); - __d_drop(proc_dentry); - } else - proc_dentry = NULL; - spin_unlock(&dcache_lock); - } - return proc_dentry; -} - -static inline void put_proc_dentry(struct dentry *dentry) -{ - if (dentry) { - shrink_dcache_parent(dentry); - dput(dentry); - } -} - /* * This function makes sure the current process has its own signal table, * so that flush_signal_handlers can later reset the handlers without @@ -660,9 +636,11 @@ static inline int de_thread(struct task_struct *tsk) while (leader->state != TASK_ZOMBIE) yield(); + spin_lock(&leader->proc_lock); + spin_lock(¤t->proc_lock); + proc_dentry1 = proc_pid_unhash(current); + proc_dentry2 = proc_pid_unhash(leader); write_lock_irq(&tasklist_lock); - proc_dentry1 = clean_proc_dentry(current); - proc_dentry2 = clean_proc_dentry(leader); if (leader->tgid != current->tgid) BUG(); @@ -702,9 +680,10 @@ static inline int de_thread(struct task_struct *tsk) state = leader->state; write_unlock_irq(&tasklist_lock); - - put_proc_dentry(proc_dentry1); - put_proc_dentry(proc_dentry2); + spin_unlock(&leader->proc_lock); + spin_unlock(¤t->proc_lock); + proc_pid_flush(proc_dentry1); + proc_pid_flush(proc_dentry2); if (state != TASK_ZOMBIE) BUG(); diff --git a/fs/proc/base.c b/fs/proc/base.c index cad41397fd3f..a657b562beb6 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -624,6 +624,12 @@ static struct inode_operations proc_pid_link_inode_operations = { .follow_link = proc_pid_follow_link }; +static int pid_alive(struct task_struct *p) +{ + BUG_ON(p->pids[PIDTYPE_PID].pidptr != &p->pids[PIDTYPE_PID].pid); + return atomic_read(&p->pids[PIDTYPE_PID].pid.count); +} + #define NUMBUF 10 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir) @@ -635,6 +641,9 @@ static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir) char buf[NUMBUF]; struct files_struct * files; + retval = -ENOENT; + if (!pid_alive(p)) + goto out; retval = 0; pid = p->pid; @@ -696,48 +705,46 @@ static int proc_base_readdir(struct file * filp, int pid; struct inode *inode = filp->f_dentry->d_inode; struct pid_entry *p; - int ret = 0; + int ret; - lock_kernel(); + ret = -ENOENT; + if (!pid_alive(proc_task(inode))) + goto out; + ret = 0; pid = proc_task(inode)->pid; - if (!pid) { - ret = -ENOENT; - goto out; - } i = filp->f_pos; switch (i) { - case 0: - if (filldir(dirent, ".", 1, i, inode->i_ino, DT_DIR) < 0) - goto out; - i++; - filp->f_pos++; - /* fall through */ - case 1: - if (filldir(dirent, "..", 2, i, PROC_ROOT_INO, DT_DIR) < 0) + case 0: + if (filldir(dirent, ".", 1, i, inode->i_ino, DT_DIR) < 0) + goto out; + i++; + filp->f_pos++; + /* fall through */ + case 1: + if (filldir(dirent, "..", 2, i, PROC_ROOT_INO, DT_DIR) < 0) + goto out; + i++; + filp->f_pos++; + /* fall through */ + default: + i -= 2; + if (i >= ARRAY_SIZE(base_stuff)) { + ret = 1; + goto out; + } + p = base_stuff + i; + while (p->name) { + if (filldir(dirent, p->name, p->len, filp->f_pos, + fake_ino(pid, p->type), p->mode >> 12) < 0) goto out; - i++; filp->f_pos++; - /* fall through */ - default: - i -= 2; - if (i>=sizeof(base_stuff)/sizeof(base_stuff[0])) { - ret = 1; - goto out; - } - p = base_stuff + i; - while (p->name) { - if (filldir(dirent, p->name, p->len, filp->f_pos, - fake_ino(pid, p->type), p->mode >> 12) < 0) - goto out; - filp->f_pos++; - p++; - } + p++; + } } ret = 1; out: - unlock_kernel(); return ret; } @@ -774,7 +781,7 @@ static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_st inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; inode->i_ino = fake_ino(task->pid, ino); - if (!task->pid) + if (!pid_alive(task)) goto out_unlock; /* @@ -808,7 +815,7 @@ out_unlock: */ static int pid_revalidate(struct dentry * dentry, int flags) { - if (proc_task(dentry->d_inode)->pid) + if (pid_alive(proc_task(dentry->d_inode))) return 1; d_drop(dentry); return 0; @@ -842,18 +849,23 @@ static int pid_fd_revalidate(struct dentry * dentry, int flags) static void pid_base_iput(struct dentry *dentry, struct inode *inode) { struct task_struct *task = proc_task(inode); - write_lock_irq(&tasklist_lock); + spin_lock(&task->proc_lock); if (task->proc_dentry == dentry) task->proc_dentry = NULL; - write_unlock_irq(&tasklist_lock); + spin_unlock(&task->proc_lock); iput(inode); } static int pid_delete_dentry(struct dentry * dentry) { - return proc_task(dentry->d_inode)->pid == 0; + /* Is the task we represent dead? + * If so, then don't put the dentry on the lru list, + * kill it immediately. + */ + return !pid_alive(proc_task(dentry->d_inode)); } + static struct dentry_operations pid_fd_dentry_operations = { .d_revalidate = pid_fd_revalidate, @@ -909,6 +921,8 @@ static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry) if (fd == ~0U) goto out; + if (!pid_alive(task)) + goto out; inode = proc_pid_make_inode(dir->i_sb, task, PROC_PID_FD_DIR+fd); if (!inode) @@ -937,8 +951,6 @@ static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry) ei->op.proc_get_link = proc_fd_link; dentry->d_op = &pid_fd_dentry_operations; d_add(dentry, inode); - if (!proc_task(dentry->d_inode)->pid) - d_drop(dentry); return NULL; out_unlock2: @@ -975,6 +987,9 @@ static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) error = -ENOENT; inode = NULL; + if (!pid_alive(task)) + goto out; + for (p = base_stuff; p->name; p++) { if (p->len != dentry->d_name.len) continue; @@ -1056,8 +1071,6 @@ static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) } dentry->d_op = &pid_dentry_operations; d_add(dentry, inode); - if (!proc_task(dentry->d_inode)->pid) - d_drop(dentry); return NULL; out: @@ -1095,6 +1108,55 @@ static struct inode_operations proc_self_inode_operations = { .follow_link = proc_self_follow_link, }; +/** + * proc_pid_unhash - Unhash /proc/ entry from the dcache. + * @p: task that should be flushed. + * + * Drops the /proc/ dcache entry from the hash chains. + * + * Dropping /proc/ entries and detach_pid must be synchroneous, + * otherwise e.g. /proc//exe might point to the wrong executable, + * if the pid value is immediately reused. This is enforced by + * - caller must acquire spin_lock(p->proc_lock) + * - must be called before detach_pid() + * - proc_pid_lookup acquires proc_lock, and checks that + * the target is not dead by looking at the attach count + * of PIDTYPE_PID. + */ + +struct dentry *proc_pid_unhash(struct task_struct *p) +{ + struct dentry *proc_dentry; + + proc_dentry = p->proc_dentry; + if (proc_dentry != NULL) { + + spin_lock(&dcache_lock); + if (!d_unhashed(proc_dentry)) { + dget_locked(proc_dentry); + __d_drop(proc_dentry); + } else + proc_dentry = NULL; + spin_unlock(&dcache_lock); + } + return proc_dentry; +} + +/** + * proc_pid_flush - recover memory used by stale /proc//x entries + * @proc_entry: directoy to prune. + * + * Shrink the /proc directory that was used by the just killed thread. + */ + +void proc_pid_flush(struct dentry *proc_dentry) +{ + if(proc_dentry != NULL) { + shrink_dcache_parent(proc_dentry); + dput(proc_dentry); + } +} + /* SMP-safe */ struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry) { @@ -1143,12 +1205,12 @@ struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry) inode->i_flags|=S_IMMUTABLE; dentry->d_op = &pid_base_dentry_operations; + + spin_lock(&task->proc_lock); + task->proc_dentry = dentry; d_add(dentry, inode); - read_lock(&tasklist_lock); - proc_task(dentry->d_inode)->proc_dentry = dentry; - read_unlock(&tasklist_lock); - if (!proc_task(dentry->d_inode)->pid) - d_drop(dentry); + spin_unlock(&task->proc_lock); + return NULL; out: return ERR_PTR(-ENOENT); @@ -1171,7 +1233,7 @@ static int get_pid_list(int index, unsigned int *pids) read_lock(&tasklist_lock); for_each_process(p) { int pid = p->pid; - if (!pid) + if (!pid_alive(p)) continue; if (--index >= 0) continue; diff --git a/fs/proc/root.c b/fs/proc/root.c index c4d71953bc6a..d49f353378df 100644 --- a/fs/proc/root.c +++ b/fs/proc/root.c @@ -110,9 +110,9 @@ static int proc_root_readdir(struct file * filp, } filp->f_pos = FIRST_PROCESS_ENTRY; } + unlock_kernel(); ret = proc_pid_readdir(filp, dirent, filldir); - unlock_kernel(); return ret; } diff --git a/include/linux/init_task.h b/include/linux/init_task.h index 96a5f926300e..6372a411be8f 100644 --- a/include/linux/init_task.h +++ b/include/linux/init_task.h @@ -101,6 +101,7 @@ .blocked = {{0}}, \ .posix_timers = LIST_HEAD_INIT(tsk.posix_timers), \ .alloc_lock = SPIN_LOCK_UNLOCKED, \ + .proc_lock = SPIN_LOCK_UNLOCKED, \ .switch_lock = SPIN_LOCK_UNLOCKED, \ .journal_info = NULL, \ } diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h index 0dd15d1cb2e2..4607df82fd93 100644 --- a/include/linux/proc_fs.h +++ b/include/linux/proc_fs.h @@ -87,6 +87,8 @@ extern void proc_root_init(void); extern void proc_misc_init(void); struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry); +struct dentry *proc_pid_unhash(struct task_struct *p); +void proc_pid_flush(struct dentry *proc_dentry); int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir); extern struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode, diff --git a/include/linux/sched.h b/include/linux/sched.h index 46981bca766f..75e6e1b5b363 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -429,6 +429,8 @@ struct task_struct { u32 self_exec_id; /* Protection of (de-)allocation: mm, files, fs, tty */ spinlock_t alloc_lock; +/* Protection of proc_dentry: nesting proc_lock, dcache_lock, write_lock_irq(&tasklist_lock); */ + spinlock_t proc_lock; /* context-switch lock */ spinlock_t switch_lock; diff --git a/kernel/exit.c b/kernel/exit.c index c4130eb03ca1..c5b8ec241a83 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -31,10 +32,8 @@ extern struct task_struct *child_reaper; int getrusage(struct task_struct *, int, struct rusage *); -static struct dentry * __unhash_process(struct task_struct *p) +static void __unhash_process(struct task_struct *p) { - struct dentry *proc_dentry; - nr_threads--; detach_pid(p, PIDTYPE_PID); detach_pid(p, PIDTYPE_TGID); @@ -46,34 +45,25 @@ static struct dentry * __unhash_process(struct task_struct *p) } REMOVE_LINKS(p); - proc_dentry = p->proc_dentry; - if (unlikely(proc_dentry != NULL)) { - spin_lock(&dcache_lock); - if (!d_unhashed(proc_dentry)) { - dget_locked(proc_dentry); - __d_drop(proc_dentry); - } else - proc_dentry = NULL; - spin_unlock(&dcache_lock); - } - return proc_dentry; } void release_task(struct task_struct * p) { - struct dentry *proc_dentry; task_t *leader; + struct dentry *proc_dentry; BUG_ON(p->state < TASK_ZOMBIE); atomic_dec(&p->user->processes); + spin_lock(&p->proc_lock); + proc_dentry = proc_pid_unhash(p); write_lock_irq(&tasklist_lock); if (unlikely(p->ptrace)) __ptrace_unlink(p); BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children)); __exit_signal(p); __exit_sighand(p); - proc_dentry = __unhash_process(p); + __unhash_process(p); /* * If we are the last non-leader member of the thread @@ -92,11 +82,8 @@ void release_task(struct task_struct * p) p->parent->cnswap += p->nswap + p->cnswap; sched_exit(p); write_unlock_irq(&tasklist_lock); - - if (unlikely(proc_dentry != NULL)) { - shrink_dcache_parent(proc_dentry); - dput(proc_dentry); - } + spin_unlock(&p->proc_lock); + proc_pid_flush(proc_dentry); release_thread(p); put_task_struct(p); } @@ -107,14 +94,13 @@ void unhash_process(struct task_struct *p) { struct dentry *proc_dentry; + spin_lock(&p->proc_lock); + proc_dentry = proc_pid_unhash(p); write_lock_irq(&tasklist_lock); - proc_dentry = __unhash_process(p); + __unhash_process(p); write_unlock_irq(&tasklist_lock); - - if (unlikely(proc_dentry != NULL)) { - shrink_dcache_parent(proc_dentry); - dput(proc_dentry); - } + spin_unlock(&p->proc_lock); + proc_pid_flush(proc_dentry); } /* -- cgit v1.2.3 From e8c0de6e726047aff99ae1519b07d449476d1a7a Mon Sep 17 00:00:00 2001 From: Andrew Morton Date: Sun, 25 May 2003 01:12:27 -0700 Subject: [PATCH] CONFIG_FUTEX From: Christopher Hoover Not everyone needs futex support, so it should be optional. This is needed for small platforms. --- include/linux/futex.h | 3 +++ init/Kconfig | 20 ++++++++++++++++++-- kernel/Makefile | 3 ++- kernel/compat.c | 6 +++--- kernel/sys.c | 2 ++ 5 files changed, 28 insertions(+), 6 deletions(-) (limited to 'kernel') diff --git a/include/linux/futex.h b/include/linux/futex.h index c76dd1ee3076..ef87c1b0d637 100644 --- a/include/linux/futex.h +++ b/include/linux/futex.h @@ -14,4 +14,7 @@ asmlinkage long sys_futex(u32 __user *uaddr, int op, int val, struct timespec __user *utime, u32 __user *uaddr2); +long do_futex(unsigned long uaddr, int op, int val, + unsigned long timeout, unsigned long uaddr2, int val2); + #endif diff --git a/init/Kconfig b/init/Kconfig index 6b636f039048..da63acb275c0 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -108,7 +108,24 @@ config LOG_BUF_SHIFT 13 => 8 KB 12 => 4 KB -endmenu + +menuconfig EMBEDDED + bool "Remove kernel features (for embedded systems)" + help + This option allows certain base kernel features to be removed from + the build. This is for specialized environments which can tolerate + a "non-standard" kernel. Only use this if you really know what you + are doing. + +config FUTEX + bool "Enable futex support" if EMBEDDED + default y + help + Disabling this option will cause the kernel to be built without + support for "fast userspace mutexes". The resulting kernel may not + run glibc-based applications correctly. + +endmenu # General setup menu "Loadable module support" @@ -181,4 +198,3 @@ config KMOD in . endmenu - diff --git a/kernel/Makefile b/kernel/Makefile index 2929aae0c2fe..1e652214037c 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -5,9 +5,10 @@ obj-y = sched.o fork.o exec_domain.o panic.o printk.o profile.o \ exit.o itimer.o time.o softirq.o resource.o \ sysctl.o capability.o ptrace.o timer.o user.o \ - signal.o sys.o kmod.o workqueue.o futex.o pid.o \ + signal.o sys.o kmod.o workqueue.o pid.o \ rcupdate.o intermodule.o extable.o params.o posix-timers.o +obj-$(CONFIG_FUTEX) += futex.o obj-$(CONFIG_GENERIC_ISA_DMA) += dma.o obj-$(CONFIG_SMP) += cpu.o obj-$(CONFIG_UID16) += uid16.o diff --git a/kernel/compat.c b/kernel/compat.c index 87c3005e8f58..e0998f98b72b 100644 --- a/kernel/compat.c +++ b/kernel/compat.c @@ -18,6 +18,7 @@ #include #include /* for MAX_SCHEDULE_TIMEOUT */ #include /* for FUTEX_WAIT */ +#include #include @@ -211,9 +212,7 @@ asmlinkage long compat_sys_sigprocmask(int how, compat_old_sigset_t *set, return ret; } -extern long do_futex(unsigned long uaddr, int op, int val, - unsigned long timeout, unsigned long uaddr2, int val2); - +#ifdef CONFIG_FUTEX asmlinkage long compat_sys_futex(u32 *uaddr, int op, int val, struct compat_timespec *utime) { @@ -231,6 +230,7 @@ asmlinkage long compat_sys_futex(u32 *uaddr, int op, int val, return do_futex((unsigned long)uaddr, op, val, timeout, uaddr2, val2); } +#endif asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit *rlim); diff --git a/kernel/sys.c b/kernel/sys.c index 8e7de84e1df5..a4d19c51b00e 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -226,6 +226,8 @@ cond_syscall(sys_shutdown) cond_syscall(sys_sendmsg) cond_syscall(sys_recvmsg) cond_syscall(sys_socketcall) +cond_syscall(sys_futex) +cond_syscall(compat_sys_futex) static int set_one_prio(struct task_struct *p, int niceval, int error) { -- cgit v1.2.3 From fb39f360f4dc671e1f7843c64fa4624aa945841d Mon Sep 17 00:00:00 2001 From: Andrew Morton Date: Sun, 25 May 2003 01:12:37 -0700 Subject: [PATCH] CONFIG_EPOLL From: Christopher Hoover Here's a patch to drop some more text/data/bss out of 2.5. This time the ``victim'' is eventpollfs (epoll). --- fs/Makefile | 4 +++- include/linux/eventpoll.h | 9 +++++++++ init/Kconfig | 7 +++++++ kernel/sys.c | 3 +++ 4 files changed, 22 insertions(+), 1 deletion(-) (limited to 'kernel') diff --git a/fs/Makefile b/fs/Makefile index 0462ed4d42db..7bf93d805a00 100644 --- a/fs/Makefile +++ b/fs/Makefile @@ -10,7 +10,9 @@ obj-y := open.o read_write.o file_table.o buffer.o \ namei.o fcntl.o ioctl.o readdir.o select.o fifo.o locks.o \ dcache.o inode.o attr.o bad_inode.o file.o dnotify.o \ filesystems.o namespace.o seq_file.o xattr.o libfs.o \ - fs-writeback.o mpage.o direct-io.o aio.o eventpoll.o + fs-writeback.o mpage.o direct-io.o aio.o + +obj-$(CONFIG_EPOLL) += eventpoll.o obj-$(CONFIG_COMPAT) += compat.o diff --git a/include/linux/eventpoll.h b/include/linux/eventpoll.h index 632c5d6efe6f..8288857d2ade 100644 --- a/include/linux/eventpoll.h +++ b/include/linux/eventpoll.h @@ -40,12 +40,21 @@ asmlinkage long sys_epoll_ctl(int epfd, int op, int fd, struct epoll_event *even asmlinkage long sys_epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout); +#ifdef CONFIG_EPOLL + /* Used to initialize the epoll bits inside the "struct file" */ void eventpoll_init_file(struct file *file); /* Used in fs/file_table.c:__fput() to unlink files from the eventpoll interface */ void eventpoll_release(struct file *file); +#else + +static inline void eventpoll_init_file(struct file *file) {} +static inline void eventpoll_release(struct file *file) {} + +#endif + #endif /* #ifdef __KERNEL__ */ #endif /* #ifndef _LINUX_EVENTPOLL_H */ diff --git a/init/Kconfig b/init/Kconfig index da63acb275c0..d3a9874335aa 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -125,6 +125,13 @@ config FUTEX support for "fast userspace mutexes". The resulting kernel may not run glibc-based applications correctly. +config EPOLL + bool "Enable eventpoll support" if EMBEDDED + default y + help + Disabling this option will cause the kernel to be built without + support for epoll family of system calls. + endmenu # General setup diff --git a/kernel/sys.c b/kernel/sys.c index a4d19c51b00e..5c2c439ae6bc 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -228,6 +228,9 @@ cond_syscall(sys_recvmsg) cond_syscall(sys_socketcall) cond_syscall(sys_futex) cond_syscall(compat_sys_futex) +cond_syscall(sys_epoll_create) +cond_syscall(sys_epoll_ctl) +cond_syscall(sys_epoll_wait) static int set_one_prio(struct task_struct *p, int niceval, int error) { -- cgit v1.2.3 From 73accc3dda343b96b37a0e2b1fd6367b57a20b64 Mon Sep 17 00:00:00 2001 From: Andrew Morton Date: Sun, 25 May 2003 01:13:17 -0700 Subject: [PATCH] add notify_count for de_thread From: Manfred Spraul de_thread is called by exec to kill all threads in the thread group except the threads required for exec. The waiting is implemented by waiting for a wakeup from __exit_signal: If the reference count is less or equal to 2, then the waiter is woken up. If exec is called by a non-leader thread, then two threads are required for exec. But if a thread group leader calls exec, then only one thread is required for exec. Thus the hardcoded "2" leads to a superfluous wakeup. The patch fixes that by adding a "notify_count" field to the signal structure. --- fs/exec.c | 1 + include/linux/sched.h | 6 ++++++ kernel/signal.c | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) (limited to 'kernel') diff --git a/fs/exec.c b/fs/exec.c index 9bbd3b4e76dc..d84c9a2c4b8f 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -609,6 +609,7 @@ static inline int de_thread(struct task_struct *tsk) count = 1; while (atomic_read(&oldsig->count) > count) { oldsig->group_exit_task = current; + oldsig->notify_count = count; __set_current_state(TASK_UNINTERRUPTIBLE); spin_unlock_irq(lock); schedule(); diff --git a/include/linux/sched.h b/include/linux/sched.h index 75e6e1b5b363..003195629026 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -245,7 +245,13 @@ struct signal_struct { /* thread group exit support */ int group_exit; int group_exit_code; + /* overloaded: + * - notify group_exit_task when ->count is equal to notify_count + * - everyone except group_exit_task is stopped during signal delivery + * of fatal signals, group_exit_task processes the signal. + */ struct task_struct *group_exit_task; + int notify_count; /* thread group stop support, overloads group_exit_code too */ int group_stop_count; diff --git a/kernel/signal.c b/kernel/signal.c index 3560d7da7032..d1f02ea4b7b1 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -336,7 +336,7 @@ void __exit_signal(struct task_struct *tsk) * If there is any task waiting for the group exit * then notify it: */ - if (sig->group_exit_task && atomic_read(&sig->count) <= 2) { + if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count) { wake_up_process(sig->group_exit_task); sig->group_exit_task = NULL; } -- cgit v1.2.3