Commit 0141450f66c3c12a3aaa869748caa64241885cdf

Authored by Wu Fengguang
Committed by Linus Torvalds
1 parent 42e4960868

readahead: introduce FMODE_RANDOM for POSIX_FADV_RANDOM

This fixes inefficient page-by-page reads on POSIX_FADV_RANDOM.

POSIX_FADV_RANDOM used to set ra_pages=0, which leads to poor performance:
a 16K read will be carried out in 4 _sync_ 1-page reads.

In other places, ra_pages==0 means
- it's ramfs/tmpfs/hugetlbfs/sysfs/configfs
- some IO error happened
where multi-page read IO won't help or should be avoided.

POSIX_FADV_RANDOM actually want a different semantics: to disable the
*heuristic* readahead algorithm, and to use a dumb one which faithfully
submit read IO for whatever application requests.

So introduce a flag FMODE_RANDOM for POSIX_FADV_RANDOM.

Note that the random hint is not likely to help random reads performance
noticeably.  And it may be too permissive on huge request size (its IO
size is not limited by read_ahead_kb).

In Quentin's report (http://lkml.org/lkml/2009/12/24/145), the overall
(NFS read) performance of the application increased by 313%!

Tested-by: Quentin Barnes <qbarnes+nfs@yahoo-inc.com>
Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
Cc: Nick Piggin <npiggin@suse.de>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Steven Whitehouse <swhiteho@redhat.com>
Cc: David Howells <dhowells@redhat.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Trond Myklebust <Trond.Myklebust@netapp.com>
Cc: Chuck Lever <chuck.lever@oracle.com>
Cc: <stable@kernel.org>			[2.6.33.x]
Cc: <qbarnes+nfs@yahoo-inc.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 3 changed files with 18 additions and 1 deletions Side-by-side Diff

... ... @@ -87,6 +87,9 @@
87 87 */
88 88 #define FMODE_NOCMTIME ((__force fmode_t)2048)
89 89  
  90 +/* Expect random access pattern */
  91 +#define FMODE_RANDOM ((__force fmode_t)4096)
  92 +
90 93 /*
91 94 * The below are the various read and write types that we support. Some of
92 95 * them include behavioral modifiers that send information down to the
... ... @@ -77,12 +77,20 @@
77 77 switch (advice) {
78 78 case POSIX_FADV_NORMAL:
79 79 file->f_ra.ra_pages = bdi->ra_pages;
  80 + spin_lock(&file->f_lock);
  81 + file->f_mode &= ~FMODE_RANDOM;
  82 + spin_unlock(&file->f_lock);
80 83 break;
81 84 case POSIX_FADV_RANDOM:
82   - file->f_ra.ra_pages = 0;
  85 + spin_lock(&file->f_lock);
  86 + file->f_mode |= FMODE_RANDOM;
  87 + spin_unlock(&file->f_lock);
83 88 break;
84 89 case POSIX_FADV_SEQUENTIAL:
85 90 file->f_ra.ra_pages = bdi->ra_pages * 2;
  91 + spin_lock(&file->f_lock);
  92 + file->f_mode &= ~FMODE_RANDOM;
  93 + spin_unlock(&file->f_lock);
86 94 break;
87 95 case POSIX_FADV_WILLNEED:
88 96 if (!mapping->a_ops->readpage) {
... ... @@ -501,6 +501,12 @@
501 501 if (!ra->ra_pages)
502 502 return;
503 503  
  504 + /* be dumb */
  505 + if (filp->f_mode & FMODE_RANDOM) {
  506 + force_page_cache_readahead(mapping, filp, offset, req_size);
  507 + return;
  508 + }
  509 +
504 510 /* do read-ahead */
505 511 ondemand_readahead(mapping, ra, filp, false, offset, req_size);
506 512 }