android_kernel_xiaomi_sm7250/include
Eric W. Biederman 0804ef4b0d [PATCH] proc: readdir race fix (take 3)
The problem: An opendir, readdir, closedir sequence can fail to report
process ids that are continually in use throughout the sequence of system
calls.  For this race to trigger the process that proc_pid_readdir stops at
must exit before readdir is called again.

This can cause ps to fail to report processes, and it is in violation of
posix guarantees and normal application expectations with respect to
readdir.

Currently there is no way to work around this problem in user space short
of providing a gargantuan buffer to user space so the directory read all
happens in on system call.

This patch implements the normal directory semantics for proc, that
guarantee that a directory entry that is neither created nor destroyed
while reading the directory entry will be returned.  For directory that are
either created or destroyed during the readdir you may or may not see them.
 Furthermore you may seek to a directory offset you have previously seen.

These are the guarantee that ext[23] provides and that posix requires, and
more importantly that user space expects.  Plus it is a simple semantic to
implement reliable service.  It is just a matter of calling readdir a
second time if you are wondering if something new has show up.

These better semantics are implemented by scanning through the pids in
numerical order and by making the file offset a pid plus a fixed offset.

The pid scan happens on the pid bitmap, which when you look at it is
remarkably efficient for a brute force algorithm.  Given that a typical
cache line is 64 bytes and thus covers space for 64*8 == 200 pids.  There
are only 40 cache lines for the entire 32K pid space.  A typical system
will have 100 pids or more so this is actually fewer cache lines we have to
look at to scan a linked list, and the worst case of having to scan the
entire pid bitmap is pretty reasonable.

If we need something more efficient we can go to a more efficient data
structure for indexing the pids, but for now what we have should be
sufficient.

In addition this takes no additional locks and is actually less code than
what we are doing now.

Also another very subtle bug in this area has been fixed.  It is possible
to catch a task in the middle of de_thread where a thread is assuming the
thread of it's thread group leader.  This patch carefully handles that case
so if we hit it we don't fail to return the pid, that is undergoing the
de_thread dance.

Thanks to KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> for
providing the first fix, pointing this out and working on it.

[oleg@tv-sign.ru: fix it]
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Acked-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Signed-off-by: Oleg Nesterov <oleg@tv-sign.ru>
Cc: Jean Delvare <jdelvare@suse.de>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-10-02 07:57:12 -07:00
..
acpi
asm-alpha [PATCH] Directed yield: cpu_relax variants for spinlocks and rw-locks 2006-10-01 00:39:21 -07:00
asm-arm [PATCH] Directed yield: cpu_relax variants for spinlocks and rw-locks 2006-10-01 00:39:21 -07:00
asm-arm26 [PATCH] consistently use MAX_ERRNO in __syscall_return 2006-09-27 08:26:18 -07:00
asm-avr32 [PATCH] AVR32 MTD: Static Memory Controller driver 2006-09-26 08:48:54 -07:00
asm-cris [PATCH] Directed yield: cpu_relax variants for spinlocks and rw-locks 2006-10-01 00:39:21 -07:00
asm-frv [PATCH] paravirt: remove set pte atomic 2006-10-01 00:39:34 -07:00
asm-generic [PATCH] paravirt: remove set pte atomic 2006-10-01 00:39:34 -07:00
asm-h8300 [PATCH] remove SYSRQ_KEY and related defines from ppc/sh/h8300 2006-10-01 00:39:22 -07:00
asm-i386 [PATCH] paravirt: update pte hook 2006-10-01 00:39:34 -07:00
asm-ia64 [PATCH] Directed yield: cpu_relax variants for spinlocks and rw-locks 2006-10-01 00:39:21 -07:00
asm-m32r [PATCH] paravirt: remove set pte atomic 2006-10-01 00:39:34 -07:00
asm-m68k [PATCH] consistently use MAX_ERRNO in __syscall_return 2006-09-27 08:26:18 -07:00
asm-m68knommu [PATCH] consistently use MAX_ERRNO in __syscall_return 2006-09-27 08:26:18 -07:00
asm-mips [PATCH] Directed yield: cpu_relax variants for spinlocks and rw-locks 2006-10-01 00:39:21 -07:00
asm-parisc [PATCH] Directed yield: cpu_relax variants for spinlocks and rw-locks 2006-10-01 00:39:21 -07:00
asm-powerpc [PATCH] update legacy io handling for pmac 2006-10-01 00:39:23 -07:00
asm-ppc [PATCH] Directed yield: cpu_relax variants for spinlocks and rw-locks 2006-10-01 00:39:21 -07:00
asm-s390 [PATCH] Directed yield: direct yield of spinlocks for s390. 2006-10-01 00:39:22 -07:00
asm-sh [PATCH] remove SYSRQ_KEY and related defines from ppc/sh/h8300 2006-10-01 00:39:22 -07:00
asm-sh64 [PATCH] ntp: cleanup defines and comments 2006-10-01 00:39:27 -07:00
asm-sparc [PATCH] Directed yield: cpu_relax variants for spinlocks and rw-locks 2006-10-01 00:39:21 -07:00
asm-sparc64 [PATCH] Directed yield: cpu_relax variants for spinlocks and rw-locks 2006-10-01 00:39:21 -07:00
asm-um [PATCH] uml: remove pte_mkexec 2006-09-29 09:18:04 -07:00
asm-v850 [PATCH] consistently use MAX_ERRNO in __syscall_return 2006-09-27 08:26:18 -07:00
asm-x86_64 [PATCH] kill wall_jiffies 2006-10-01 00:39:27 -07:00
asm-xtensa [PATCH] ntp: cleanup defines and comments 2006-10-01 00:39:27 -07:00
crypto
keys
linux [PATCH] proc: readdir race fix (take 3) 2006-10-02 07:57:12 -07:00
math-emu
media Merge master.kernel.org:/pub/scm/linux/kernel/git/mchehab/v4l-dvb 2006-09-30 09:39:15 -07:00
mtd
net [PATCH] Add genetlink utilities for payload length calculation 2006-10-01 00:39:29 -07:00
pcmcia
rdma IB/sa: fix ib_sa_selector names 2006-09-22 15:22:55 -07:00
rxrpc
scsi [PATCH] scsi: device_reprobe() can fail 2006-09-30 19:33:43 -07:00
sound [PATCH] maximum latency tracking: ALSA support 2006-10-01 00:39:19 -07:00
video
Kbuild