Commit af248a0c authored by Damien Ramonda's avatar Damien Ramonda Committed by Linus Torvalds

readahead: fix sequential read cache miss detection

The kernel's readahead algorithm sometimes interprets random read
accesses as sequential and triggers unnecessary data prefecthing from
storage device (impacting random read average latency).

In order to identify sequential cache read misses, the readahead
algorithm intends to check whether offset - previous offset == 1
(trivial sequential reads) or offset - previous offset == 0 (sequential
reads not aligned on page boundary):

  if (offset - (ra->prev_pos >> PAGE_CACHE_SHIFT) <= 1UL)

The current offset is stored in the "offset" variable of type "pgoff_t"
(unsigned long), while previous offset is stored in "ra->prev_pos" of
type "loff_t" (long long).  Therefore, operands of the if statement are
implicitly converted to type long long.  Consequently, when previous
offset > current offset (which happens on random pattern), the if
condition is true and access is wrongly interpeted as sequential.  An
unnecessary data prefetching is triggered, impacting the average random
read latency.

Storing the previous offset value in a "pgoff_t" variable (unsigned
long) fixes the sequential read detection logic.
Signed-off-by: default avatarDamien Ramonda <damien.ramonda@intel.com>
Reviewed-by: default avatarFengguang Wu <fengguang.wu@intel.com>
Acked-by: default avatarPierre Tardy <pierre.tardy@intel.com>
Acked-by: default avatarDavid Cohen <david.a.cohen@linux.intel.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent c78e9363
...@@ -401,6 +401,7 @@ ondemand_readahead(struct address_space *mapping, ...@@ -401,6 +401,7 @@ ondemand_readahead(struct address_space *mapping,
unsigned long req_size) unsigned long req_size)
{ {
unsigned long max = max_sane_readahead(ra->ra_pages); unsigned long max = max_sane_readahead(ra->ra_pages);
pgoff_t prev_offset;
/* /*
* start of file * start of file
...@@ -452,8 +453,11 @@ ondemand_readahead(struct address_space *mapping, ...@@ -452,8 +453,11 @@ ondemand_readahead(struct address_space *mapping,
/* /*
* sequential cache miss * sequential cache miss
* trivial case: (offset - prev_offset) == 1
* unaligned reads: (offset - prev_offset) == 0
*/ */
if (offset - (ra->prev_pos >> PAGE_CACHE_SHIFT) <= 1UL) prev_offset = (unsigned long long)ra->prev_pos >> PAGE_CACHE_SHIFT;
if (offset - prev_offset <= 1UL)
goto initial_readahead; goto initial_readahead;
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment