kernel-ark/fs/jffs2
David Woodhouse 199bc9ff5c jffs2: Fix memory corruption in jffs2_read_inode_range()
In 2.6.23 kernel, commit a32ea1e1f9
("Fix read/truncate race") fixed a race in the generic code, and as a
side effect, now do_generic_file_read() can ask us to readpage() past
the i_size. This seems to be correctly handled by the block routines
(e.g. block_read_full_page() fills the page with zeroes in case if
somebody is trying to read past the last inode's block).

JFFS2 doesn't handle this; it assumes that it won't be asked to read
pages which don't exist -- and thus that there will be at least _one_
valid 'frag' on the page it's being asked to read. It will fill any
holes with the following memset:

  memset(buf, 0, min(end, frag->ofs + frag->size) - offset);

When the 'closest smaller match' returned by jffs2_lookup_node_frag() is
actually on a previous page and ends before 'offset', that results in:

  memset(buf, 0, <huge unsigned negative>);

Hopefully, in most cases the corruption is fatal, and quickly causing
random oopses, like this:

  root@10.0.0.4:~/ltp-fs-20090531# ./testcases/kernel/fs/ftest/ftest01
  Unable to handle kernel paging request for data at address 0x00000008
  Faulting instruction address: 0xc01cd980
  Oops: Kernel access of bad area, sig: 11 [#1]
  [...]
  NIP [c01cd980] rb_insert_color+0x38/0x184
  LR [c0043978] enqueue_hrtimer+0x88/0xc4
  Call Trace:
  [c6c63b60] [c004f9a8] tick_sched_timer+0xa0/0xe4 (unreliable)
  [c6c63b80] [c0043978] enqueue_hrtimer+0x88/0xc4
  [c6c63b90] [c0043a48] __run_hrtimer+0x94/0xbc
  [c6c63bb0] [c0044628] hrtimer_interrupt+0x140/0x2b8
  [c6c63c10] [c000f8e8] timer_interrupt+0x13c/0x254
  [c6c63c30] [c001352c] ret_from_except+0x0/0x14
  --- Exception: 901 at memset+0x38/0x5c
      LR = jffs2_read_inode_range+0x144/0x17c
  [c6c63cf0] [00000000] (null) (unreliable)

This patch fixes the issue, plus fixes all LTP tests on NAND/UBI with
JFFS2 filesystem that were failing since 2.6.23 (seems like the bug
above also broke the truncation).

Reported-By: Anton Vorontsov <avorontsov@ru.mvista.com>
Tested-By: Anton Vorontsov <avorontsov@ru.mvista.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
Cc: stable@kernel.org
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2009-11-30 13:52:40 -08:00
..
acl.c jffs2/jfs/xfs: switch over to 'check_acl' rather than 'permission()' 2009-09-08 11:09:04 -07:00
acl.h jffs2/jfs/xfs: switch over to 'check_acl' rather than 'permission()' 2009-09-08 11:09:04 -07:00
background.c jffs2: move jffs2_gcd_mtd threads to the new kthread API 2009-09-04 09:53:35 +01:00
build.c [JFFS2] Track parent inode for directories (for NFS export) 2008-05-01 18:47:17 +01:00
compr_lzo.c [JFFS2] fix race condition in jffs2_lzo_compress() 2008-11-05 23:22:02 +01:00
compr_rtime.c [JFFS2] Whitespace cleanups. 2007-07-10 10:01:22 +01:00
compr_rubin.c [JFFS2] Clean up fs/jffs2/compr_rubin.c 2008-12-10 15:34:49 +00:00
compr_zlib.c [JFFS2] Whitespace cleanups. 2007-07-10 10:01:22 +01:00
compr.c [JFFS2] Correct parameter names of jffs2_compress() in comments 2008-10-17 15:56:44 +01:00
compr.h [JFFS2] Add declaration of jffs2_lzo_{init,exit} to compr.h 2007-07-11 15:33:14 +01:00
debug.c [JFFS2] Add erase_checking_list to hold blocks being marked. 2008-04-23 14:15:24 +01:00
debug.h fs: replace remaining __FUNCTION__ occurrences 2008-04-30 08:29:54 -07:00
dir.c jffs2/jfs/xfs: switch over to 'check_acl' rather than 'permission()' 2009-09-08 11:09:04 -07:00
erase.c Remove multiple KERN_ prefixes from printk formats 2009-07-08 10:30:03 -07:00
file.c jffs2/jfs/xfs: switch over to 'check_acl' rather than 'permission()' 2009-09-08 11:09:04 -07:00
fs.c Push BKL down into ->remount_fs() 2009-06-11 21:36:11 -04:00
gc.c [JFFS2] Track parent inode for directories (for NFS export) 2008-05-01 18:47:17 +01:00
ioctl.c [JFFS2] Use .unlocked_ioctl 2008-07-11 18:33:42 +01:00
jffs2_fs_i.h switch jffs2 to inode->i_acl 2009-06-24 08:17:05 -04:00
jffs2_fs_sb.h [JFFS2] Add erase_checking_list to hold blocks being marked. 2008-04-23 14:15:24 +01:00
Kconfig [JFFS2] Move JFFS2 config options out of fs/Kconfig 2008-09-01 11:29:52 +01:00
LICENCE [JFFS2] Tidy up licensing/copyright boilerplate. 2007-04-25 14:16:47 +01:00
Makefile [JFFS2] Add LZO compression support. 2007-07-11 15:03:53 +01:00
malloc.c jffs2: Use SLAB_HWCACHE_ALIGN for jffs2_raw_{dirent,inode} slabs 2009-09-19 16:14:01 -07:00
nodelist.c [JFFS2] Improve getdents vs. f_pos handling on NOR flash. 2007-11-01 16:25:56 -04:00
nodelist.h [JFFS2] remove junk prototypes 2009-01-09 21:05:21 +00:00
nodemgmt.c [JFFS2] Fix build failure with !CONFIG_JFFS2_FS_WRITEBUFFER 2008-10-21 13:37:43 +01:00
os-linux.h switch jffs2 to inode->i_acl 2009-06-24 08:17:05 -04:00
read.c jffs2: Fix memory corruption in jffs2_read_inode_range() 2009-11-30 13:52:40 -08:00
readinode.c switch jffs2 to inode->i_acl 2009-06-24 08:17:05 -04:00
README.Locking [JFFS2] semaphore->mutex conversion 2008-04-22 15:13:40 +01:00
scan.c jffs2: fix another potential leak on error path in scan.c 2009-06-23 01:34:19 +01:00
security.c [JFFS2] Whitespace cleanups. 2007-07-10 10:01:22 +01:00
summary.c [JFFS2] Fix allocation of summary buffer 2008-08-01 10:07:51 +01:00
summary.h [JFFS2] Fix allocation of summary buffer 2008-08-01 10:07:51 +01:00
super.c const: mark remaining export_operations const 2009-09-22 07:17:24 -07:00
symlink.c jffs2/jfs/xfs: switch over to 'check_acl' rather than 'permission()' 2009-09-08 11:09:04 -07:00
TODO [JFFS2] Tidy up licensing/copyright boilerplate. 2007-04-25 14:16:47 +01:00
wbuf.c JFFS2: add missing verify buffer allocation/deallocation 2009-09-03 15:01:34 +01:00
write.c [JFFS2] Track parent inode for directories (for NFS export) 2008-05-01 18:47:17 +01:00
writev.c [JFFS2] Tidy up licensing/copyright boilerplate. 2007-04-25 14:16:47 +01:00
xattr_trusted.c [JFFS2] Tidy up licensing/copyright boilerplate. 2007-04-25 14:16:47 +01:00
xattr_user.c [JFFS2] Whitespace cleanups. 2007-07-10 10:01:22 +01:00
xattr.c Merge git://git.infradead.org/mtd-2.6 2008-05-01 11:15:28 -07:00
xattr.h [JFFS2] Whitespace cleanups. 2007-07-10 10:01:22 +01:00

	JFFS2 LOCKING DOCUMENTATION
	---------------------------

At least theoretically, JFFS2 does not require the Big Kernel Lock
(BKL), which was always helpfully obtained for it by Linux 2.4 VFS
code. It has its own locking, as described below.

This document attempts to describe the existing locking rules for
JFFS2. It is not expected to remain perfectly up to date, but ought to
be fairly close.


	alloc_sem
	---------

The alloc_sem is a per-filesystem mutex, used primarily to ensure
contiguous allocation of space on the medium. It is automatically
obtained during space allocations (jffs2_reserve_space()) and freed
upon write completion (jffs2_complete_reservation()). Note that
the garbage collector will obtain this right at the beginning of
jffs2_garbage_collect_pass() and release it at the end, thereby
preventing any other write activity on the file system during a
garbage collect pass.

When writing new nodes, the alloc_sem must be held until the new nodes
have been properly linked into the data structures for the inode to
which they belong. This is for the benefit of NAND flash - adding new
nodes to an inode may obsolete old ones, and by holding the alloc_sem
until this happens we ensure that any data in the write-buffer at the
time this happens are part of the new node, not just something that
was written afterwards. Hence, we can ensure the newly-obsoleted nodes
don't actually get erased until the write-buffer has been flushed to
the medium.

With the introduction of NAND flash support and the write-buffer, 
the alloc_sem is also used to protect the wbuf-related members of the
jffs2_sb_info structure. Atomically reading the wbuf_len member to see
if the wbuf is currently holding any data is permitted, though.

Ordering constraints: See f->sem.


	File Mutex f->sem
	---------------------

This is the JFFS2-internal equivalent of the inode mutex i->i_sem.
It protects the contents of the jffs2_inode_info private inode data,
including the linked list of node fragments (but see the notes below on
erase_completion_lock), etc.

The reason that the i_sem itself isn't used for this purpose is to
avoid deadlocks with garbage collection -- the VFS will lock the i_sem
before calling a function which may need to allocate space. The
allocation may trigger garbage-collection, which may need to move a
node belonging to the inode which was locked in the first place by the
VFS. If the garbage collection code were to attempt to lock the i_sem
of the inode from which it's garbage-collecting a physical node, this
lead to deadlock, unless we played games with unlocking the i_sem
before calling the space allocation functions.

Instead of playing such games, we just have an extra internal
mutex, which is obtained by the garbage collection code and also
by the normal file system code _after_ allocation of space.

Ordering constraints: 

	1. Never attempt to allocate space or lock alloc_sem with 
	   any f->sem held.
	2. Never attempt to lock two file mutexes in one thread.
	   No ordering rules have been made for doing so.


	erase_completion_lock spinlock
	------------------------------

This is used to serialise access to the eraseblock lists, to the
per-eraseblock lists of physical jffs2_raw_node_ref structures, and
(NB) the per-inode list of physical nodes. The latter is a special
case - see below.

As the MTD API no longer permits erase-completion callback functions
to be called from bottom-half (timer) context (on the basis that nobody
ever actually implemented such a thing), it's now sufficient to use
a simple spin_lock() rather than spin_lock_bh().

Note that the per-inode list of physical nodes (f->nodes) is a special
case. Any changes to _valid_ nodes (i.e. ->flash_offset & 1 == 0) in
the list are protected by the file mutex f->sem. But the erase code
may remove _obsolete_ nodes from the list while holding only the
erase_completion_lock. So you can walk the list only while holding the
erase_completion_lock, and can drop the lock temporarily mid-walk as
long as the pointer you're holding is to a _valid_ node, not an
obsolete one.

The erase_completion_lock is also used to protect the c->gc_task
pointer when the garbage collection thread exits. The code to kill the
GC thread locks it, sends the signal, then unlocks it - while the GC
thread itself locks it, zeroes c->gc_task, then unlocks on the exit path.


	inocache_lock spinlock
	----------------------

This spinlock protects the hashed list (c->inocache_list) of the
in-core jffs2_inode_cache objects (each inode in JFFS2 has the
correspondent jffs2_inode_cache object). So, the inocache_lock
has to be locked while walking the c->inocache_list hash buckets.

This spinlock also covers allocation of new inode numbers, which is
currently just '++->highest_ino++', but might one day get more complicated
if we need to deal with wrapping after 4 milliard inode numbers are used.

Note, the f->sem guarantees that the correspondent jffs2_inode_cache
will not be removed. So, it is allowed to access it without locking
the inocache_lock spinlock. 

Ordering constraints: 

	If both erase_completion_lock and inocache_lock are needed, the
	c->erase_completion has to be acquired first.


	erase_free_sem
	--------------

This mutex is only used by the erase code which frees obsolete node
references and the jffs2_garbage_collect_deletion_dirent() function.
The latter function on NAND flash must read _obsolete_ nodes to
determine whether the 'deletion dirent' under consideration can be
discarded or whether it is still required to show that an inode has
been unlinked. Because reading from the flash may sleep, the
erase_completion_lock cannot be held, so an alternative, more
heavyweight lock was required to prevent the erase code from freeing
the jffs2_raw_node_ref structures in question while the garbage
collection code is looking at them.

Suggestions for alternative solutions to this problem would be welcomed.


	wbuf_sem
	--------

This read/write semaphore protects against concurrent access to the
write-behind buffer ('wbuf') used for flash chips where we must write
in blocks. It protects both the contents of the wbuf and the metadata
which indicates which flash region (if any) is currently covered by 
the buffer.

Ordering constraints:
	Lock wbuf_sem last, after the alloc_sem or and f->sem.


	c->xattr_sem
	------------

This read/write semaphore protects against concurrent access to the
xattr related objects which include stuff in superblock and ic->xref.
In read-only path, write-semaphore is too much exclusion. It's enough
by read-semaphore. But you must hold write-semaphore when updating,
creating or deleting any xattr related object.

Once xattr_sem released, there would be no assurance for the existence
of those objects. Thus, a series of processes is often required to retry,
when updating such a object is necessary under holding read semaphore.
For example, do_jffs2_getxattr() holds read-semaphore to scan xref and
xdatum at first. But it retries this process with holding write-semaphore
after release read-semaphore, if it's necessary to load name/value pair
from medium.

Ordering constraints:
	Lock xattr_sem last, after the alloc_sem.