Skip to content

Commit cf914a7

Browse files
rustyrussellLinus Torvalds
authored andcommitted
readahead: split ondemand readahead interface into two functions
Split ondemand readahead interface into two functions. I think this makes it a little clearer for non-readahead experts (like Rusty). Internally they both call ondemand_readahead(), but the page argument is changed to an obvious boolean flag. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Signed-off-by: Fengguang Wu <wfg@mail.ustc.edu.cn> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent fe3cba1 commit cf914a7

6 files changed

Lines changed: 85 additions & 56 deletions

File tree

‎fs/ext3/dir.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ static int ext3_readdir(struct file * filp,
139139
pgoff_t index = map_bh.b_blocknr >>
140140
(PAGE_CACHE_SHIFT - inode->i_blkbits);
141141
if (!ra_has_index(&filp->f_ra, index))
142-
page_cache_readahead_ondemand(
142+
page_cache_sync_readahead(
143143
sb->s_bdev->bd_inode->i_mapping,
144144
&filp->f_ra, filp,
145-
NULL, index, 1);
145+
index, 1);
146146
filp->f_ra.prev_index = index;
147147
bh = ext3_bread(NULL, inode, blk, 0, &err);
148148
}

‎fs/ext4/dir.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ static int ext4_readdir(struct file * filp,
138138
pgoff_t index = map_bh.b_blocknr >>
139139
(PAGE_CACHE_SHIFT - inode->i_blkbits);
140140
if (!ra_has_index(&filp->f_ra, index))
141-
page_cache_readahead_ondemand(
141+
page_cache_sync_readahead(
142142
sb->s_bdev->bd_inode->i_mapping,
143143
&filp->f_ra, filp,
144-
NULL, index, 1);
144+
index, 1);
145145
filp->f_ra.prev_index = index;
146146
bh = ext4_bread(NULL, inode, blk, 0, &err);
147147
}

‎fs/splice.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ __generic_file_splice_read(struct file *in, loff_t *ppos,
295295
* readahead/allocate the rest and fill in the holes.
296296
*/
297297
if (spd.nr_pages < nr_pages)
298-
page_cache_readahead_ondemand(mapping, &in->f_ra, in,
299-
NULL, index, req_pages - spd.nr_pages);
298+
page_cache_sync_readahead(mapping, &in->f_ra, in,
299+
index, req_pages - spd.nr_pages);
300300

301301
error = 0;
302302
while (spd.nr_pages < nr_pages) {
@@ -352,7 +352,7 @@ __generic_file_splice_read(struct file *in, loff_t *ppos,
352352
page = pages[page_nr];
353353

354354
if (PageReadahead(page))
355-
page_cache_readahead_ondemand(mapping, &in->f_ra, in,
355+
page_cache_async_readahead(mapping, &in->f_ra, in,
356356
page, index, req_pages - page_nr);
357357

358358
/*

‎include/linux/mm.h‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,12 +1138,20 @@ int do_page_cache_readahead(struct address_space *mapping, struct file *filp,
11381138
pgoff_t offset, unsigned long nr_to_read);
11391139
int force_page_cache_readahead(struct address_space *mapping, struct file *filp,
11401140
pgoff_t offset, unsigned long nr_to_read);
1141-
unsigned long page_cache_readahead_ondemand(struct address_space *mapping,
1142-
struct file_ra_state *ra,
1143-
struct file *filp,
1144-
struct page *page,
1145-
pgoff_t offset,
1146-
unsigned long size);
1141+
1142+
void page_cache_sync_readahead(struct address_space *mapping,
1143+
struct file_ra_state *ra,
1144+
struct file *filp,
1145+
pgoff_t offset,
1146+
unsigned long size);
1147+
1148+
void page_cache_async_readahead(struct address_space *mapping,
1149+
struct file_ra_state *ra,
1150+
struct file *filp,
1151+
struct page *pg,
1152+
pgoff_t offset,
1153+
unsigned long size);
1154+
11471155
unsigned long max_sane_readahead(unsigned long nr);
11481156

11491157
/* Do stack extension */

‎mm/filemap.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -894,15 +894,15 @@ void do_generic_mapping_read(struct address_space *mapping,
894894
find_page:
895895
page = find_get_page(mapping, index);
896896
if (!page) {
897-
page_cache_readahead_ondemand(mapping,
898-
&ra, filp, page,
897+
page_cache_sync_readahead(mapping,
898+
&ra, filp,
899899
index, last_index - index);
900900
page = find_get_page(mapping, index);
901901
if (unlikely(page == NULL))
902902
goto no_cached_page;
903903
}
904904
if (PageReadahead(page)) {
905-
page_cache_readahead_ondemand(mapping,
905+
page_cache_async_readahead(mapping,
906906
&ra, filp, page,
907907
index, last_index - index);
908908
}
@@ -1348,14 +1348,14 @@ int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
13481348
*/
13491349
if (VM_SequentialReadHint(vma)) {
13501350
if (!page) {
1351-
page_cache_readahead_ondemand(mapping, ra, file, page,
1351+
page_cache_sync_readahead(mapping, ra, file,
13521352
vmf->pgoff, 1);
13531353
page = find_lock_page(mapping, vmf->pgoff);
13541354
if (!page)
13551355
goto no_cached_page;
13561356
}
13571357
if (PageReadahead(page)) {
1358-
page_cache_readahead_ondemand(mapping, ra, file, page,
1358+
page_cache_async_readahead(mapping, ra, file, page,
13591359
vmf->pgoff, 1);
13601360
}
13611361
}

‎mm/readahead.c‎

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ static unsigned long get_next_ra_size(struct file_ra_state *ra,
359359
static unsigned long
360360
ondemand_readahead(struct address_space *mapping,
361361
struct file_ra_state *ra, struct file *filp,
362-
struct page *page, pgoff_t offset,
362+
bool hit_readahead_marker, pgoff_t offset,
363363
unsigned long req_size)
364364
{
365365
unsigned long max; /* max readahead pages */
@@ -387,7 +387,7 @@ ondemand_readahead(struct address_space *mapping,
387387
* Standalone, small read.
388388
* Read as is, and do not pollute the readahead state.
389389
*/
390-
if (!page && !sequential) {
390+
if (!hit_readahead_marker && !sequential) {
391391
return __do_page_cache_readahead(mapping, filp,
392392
offset, req_size, 0);
393393
}
@@ -408,7 +408,7 @@ ondemand_readahead(struct address_space *mapping,
408408
* E.g. interleaved reads.
409409
* Not knowing its readahead pos/size, bet on the minimal possible one.
410410
*/
411-
if (page) {
411+
if (hit_readahead_marker) {
412412
ra_index++;
413413
ra_size = min(4 * ra_size, max);
414414
}
@@ -421,50 +421,71 @@ ondemand_readahead(struct address_space *mapping,
421421
}
422422

423423
/**
424-
* page_cache_readahead_ondemand - generic file readahead
424+
* page_cache_sync_readahead - generic file readahead
425425
* @mapping: address_space which holds the pagecache and I/O vectors
426426
* @ra: file_ra_state which holds the readahead state
427427
* @filp: passed on to ->readpage() and ->readpages()
428-
* @page: the page at @offset, or NULL if non-present
429-
* @offset: start offset into @mapping, in PAGE_CACHE_SIZE units
428+
* @offset: start offset into @mapping, in pagecache page-sized units
430429
* @req_size: hint: total size of the read which the caller is performing in
431-
* PAGE_CACHE_SIZE units
430+
* pagecache pages
432431
*
433-
* page_cache_readahead_ondemand() is the entry point of readahead logic.
434-
* This function should be called when it is time to perform readahead:
435-
* 1) @page == NULL
436-
* A cache miss happened, time for synchronous readahead.
437-
* 2) @page != NULL && PageReadahead(@page)
438-
* A look-ahead hit occured, time for asynchronous readahead.
432+
* page_cache_sync_readahead() should be called when a cache miss happened:
433+
* it will submit the read. The readahead logic may decide to piggyback more
434+
* pages onto the read request if access patterns suggest it will improve
435+
* performance.
439436
*/
440-
unsigned long
441-
page_cache_readahead_ondemand(struct address_space *mapping,
442-
struct file_ra_state *ra, struct file *filp,
443-
struct page *page, pgoff_t offset,
444-
unsigned long req_size)
437+
void page_cache_sync_readahead(struct address_space *mapping,
438+
struct file_ra_state *ra, struct file *filp,
439+
pgoff_t offset, unsigned long req_size)
445440
{
446441
/* no read-ahead */
447442
if (!ra->ra_pages)
448-
return 0;
449-
450-
if (page) {
451-
/*
452-
* It can be PG_reclaim.
453-
*/
454-
if (PageWriteback(page))
455-
return 0;
456-
457-
ClearPageReadahead(page);
458-
459-
/*
460-
* Defer asynchronous read-ahead on IO congestion.
461-
*/
462-
if (bdi_read_congested(mapping->backing_dev_info))
463-
return 0;
464-
}
443+
return;
444+
445+
/* do read-ahead */
446+
ondemand_readahead(mapping, ra, filp, false, offset, req_size);
447+
}
448+
EXPORT_SYMBOL_GPL(page_cache_sync_readahead);
449+
450+
/**
451+
* page_cache_async_readahead - file readahead for marked pages
452+
* @mapping: address_space which holds the pagecache and I/O vectors
453+
* @ra: file_ra_state which holds the readahead state
454+
* @filp: passed on to ->readpage() and ->readpages()
455+
* @page: the page at @offset which has the PG_readahead flag set
456+
* @offset: start offset into @mapping, in pagecache page-sized units
457+
* @req_size: hint: total size of the read which the caller is performing in
458+
* pagecache pages
459+
*
460+
* page_cache_async_ondemand() should be called when a page is used which
461+
* has the PG_readahead flag: this is a marker to suggest that the application
462+
* has used up enough of the readahead window that we should start pulling in
463+
* more pages. */
464+
void
465+
page_cache_async_readahead(struct address_space *mapping,
466+
struct file_ra_state *ra, struct file *filp,
467+
struct page *page, pgoff_t offset,
468+
unsigned long req_size)
469+
{
470+
/* no read-ahead */
471+
if (!ra->ra_pages)
472+
return;
473+
474+
/*
475+
* Same bit is used for PG_readahead and PG_reclaim.
476+
*/
477+
if (PageWriteback(page))
478+
return;
479+
480+
ClearPageReadahead(page);
481+
482+
/*
483+
* Defer asynchronous read-ahead on IO congestion.
484+
*/
485+
if (bdi_read_congested(mapping->backing_dev_info))
486+
return;
465487

466488
/* do read-ahead */
467-
return ondemand_readahead(mapping, ra, filp, page,
468-
offset, req_size);
489+
ondemand_readahead(mapping, ra, filp, true, offset, req_size);
469490
}
470-
EXPORT_SYMBOL_GPL(page_cache_readahead_ondemand);
491+
EXPORT_SYMBOL_GPL(page_cache_async_readahead);

0 commit comments

Comments
 (0)