Skip to content

Commit 25d6a23

Browse files
author
Matthew Wilcox (Oracle)
committed
filemap: Convert filemap_get_read_batch() to use a folio_batch
This change ripples all the way through the filemap_read() call chain and removes a lot of messing about converting folios to pages and back again. Signed-off-by: Matthew Wilcox (Oracle) <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: William Kucharski <[email protected]>
1 parent d996fc7 commit 25d6a23

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

mm/filemap.c

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,16 +2325,16 @@ static void shrink_readahead_size_eio(struct file_ra_state *ra)
23252325
}
23262326

23272327
/*
2328-
* filemap_get_read_batch - Get a batch of pages for read
2328+
* filemap_get_read_batch - Get a batch of folios for read
23292329
*
2330-
* Get a batch of pages which represent a contiguous range of bytes
2331-
* in the file. No tail pages will be returned. If @index is in the
2332-
* middle of a THP, the entire THP will be returned. The last page in
2333-
* the batch may have Readahead set or be not Uptodate so that the
2334-
* caller can take the appropriate action.
2330+
* Get a batch of folios which represent a contiguous range of bytes in
2331+
* the file. No exceptional entries will be returned. If @index is in
2332+
* the middle of a folio, the entire folio will be returned. The last
2333+
* folio in the batch may have the readahead flag set or the uptodate flag
2334+
* clear so that the caller can take the appropriate action.
23352335
*/
23362336
static void filemap_get_read_batch(struct address_space *mapping,
2337-
pgoff_t index, pgoff_t max, struct pagevec *pvec)
2337+
pgoff_t index, pgoff_t max, struct folio_batch *fbatch)
23382338
{
23392339
XA_STATE(xas, &mapping->i_pages, index);
23402340
struct folio *folio;
@@ -2349,9 +2349,9 @@ static void filemap_get_read_batch(struct address_space *mapping,
23492349
goto retry;
23502350

23512351
if (unlikely(folio != xas_reload(&xas)))
2352-
goto put_page;
2352+
goto put_folio;
23532353

2354-
if (!pagevec_add(pvec, &folio->page))
2354+
if (!folio_batch_add(fbatch, folio))
23552355
break;
23562356
if (!folio_test_uptodate(folio))
23572357
break;
@@ -2360,7 +2360,7 @@ static void filemap_get_read_batch(struct address_space *mapping,
23602360
xas.xa_index = folio->index + folio_nr_pages(folio) - 1;
23612361
xas.xa_offset = (xas.xa_index >> xas.xa_shift) & XA_CHUNK_MASK;
23622362
continue;
2363-
put_page:
2363+
put_folio:
23642364
folio_put(folio);
23652365
retry:
23662366
xas_reset(&xas);
@@ -2475,7 +2475,7 @@ static int filemap_update_page(struct kiocb *iocb,
24752475

24762476
static int filemap_create_folio(struct file *file,
24772477
struct address_space *mapping, pgoff_t index,
2478-
struct pagevec *pvec)
2478+
struct folio_batch *fbatch)
24792479
{
24802480
struct folio *folio;
24812481
int error;
@@ -2510,7 +2510,7 @@ static int filemap_create_folio(struct file *file,
25102510
goto error;
25112511

25122512
filemap_invalidate_unlock_shared(mapping);
2513-
pagevec_add(pvec, &folio->page);
2513+
folio_batch_add(fbatch, folio);
25142514
return 0;
25152515
error:
25162516
filemap_invalidate_unlock_shared(mapping);
@@ -2531,7 +2531,7 @@ static int filemap_readahead(struct kiocb *iocb, struct file *file,
25312531
}
25322532

25332533
static int filemap_get_pages(struct kiocb *iocb, struct iov_iter *iter,
2534-
struct pagevec *pvec)
2534+
struct folio_batch *fbatch)
25352535
{
25362536
struct file *filp = iocb->ki_filp;
25372537
struct address_space *mapping = filp->f_mapping;
@@ -2546,32 +2546,33 @@ static int filemap_get_pages(struct kiocb *iocb, struct iov_iter *iter,
25462546
if (fatal_signal_pending(current))
25472547
return -EINTR;
25482548

2549-
filemap_get_read_batch(mapping, index, last_index, pvec);
2550-
if (!pagevec_count(pvec)) {
2549+
filemap_get_read_batch(mapping, index, last_index, fbatch);
2550+
if (!folio_batch_count(fbatch)) {
25512551
if (iocb->ki_flags & IOCB_NOIO)
25522552
return -EAGAIN;
25532553
page_cache_sync_readahead(mapping, ra, filp, index,
25542554
last_index - index);
2555-
filemap_get_read_batch(mapping, index, last_index, pvec);
2555+
filemap_get_read_batch(mapping, index, last_index, fbatch);
25562556
}
2557-
if (!pagevec_count(pvec)) {
2557+
if (!folio_batch_count(fbatch)) {
25582558
if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_WAITQ))
25592559
return -EAGAIN;
25602560
err = filemap_create_folio(filp, mapping,
2561-
iocb->ki_pos >> PAGE_SHIFT, pvec);
2561+
iocb->ki_pos >> PAGE_SHIFT, fbatch);
25622562
if (err == AOP_TRUNCATED_PAGE)
25632563
goto retry;
25642564
return err;
25652565
}
25662566

2567-
folio = page_folio(pvec->pages[pagevec_count(pvec) - 1]);
2567+
folio = fbatch->folios[folio_batch_count(fbatch) - 1];
25682568
if (folio_test_readahead(folio)) {
25692569
err = filemap_readahead(iocb, filp, mapping, folio, last_index);
25702570
if (err)
25712571
goto err;
25722572
}
25732573
if (!folio_test_uptodate(folio)) {
2574-
if ((iocb->ki_flags & IOCB_WAITQ) && pagevec_count(pvec) > 1)
2574+
if ((iocb->ki_flags & IOCB_WAITQ) &&
2575+
folio_batch_count(fbatch) > 1)
25752576
iocb->ki_flags |= IOCB_NOWAIT;
25762577
err = filemap_update_page(iocb, mapping, iter, folio);
25772578
if (err)
@@ -2582,7 +2583,7 @@ static int filemap_get_pages(struct kiocb *iocb, struct iov_iter *iter,
25822583
err:
25832584
if (err < 0)
25842585
folio_put(folio);
2585-
if (likely(--pvec->nr))
2586+
if (likely(--fbatch->nr))
25862587
return 0;
25872588
if (err == AOP_TRUNCATED_PAGE)
25882589
goto retry;
@@ -2609,7 +2610,7 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
26092610
struct file_ra_state *ra = &filp->f_ra;
26102611
struct address_space *mapping = filp->f_mapping;
26112612
struct inode *inode = mapping->host;
2612-
struct pagevec pvec;
2613+
struct folio_batch fbatch;
26132614
int i, error = 0;
26142615
bool writably_mapped;
26152616
loff_t isize, end_offset;
@@ -2620,7 +2621,7 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
26202621
return 0;
26212622

26222623
iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
2623-
pagevec_init(&pvec);
2624+
folio_batch_init(&fbatch);
26242625

26252626
do {
26262627
cond_resched();
@@ -2636,7 +2637,7 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
26362637
if (unlikely(iocb->ki_pos >= i_size_read(inode)))
26372638
break;
26382639

2639-
error = filemap_get_pages(iocb, iter, &pvec);
2640+
error = filemap_get_pages(iocb, iter, &fbatch);
26402641
if (error < 0)
26412642
break;
26422643

@@ -2650,7 +2651,7 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
26502651
*/
26512652
isize = i_size_read(inode);
26522653
if (unlikely(iocb->ki_pos >= isize))
2653-
goto put_pages;
2654+
goto put_folios;
26542655
end_offset = min_t(loff_t, isize, iocb->ki_pos + iter->count);
26552656

26562657
/*
@@ -2665,10 +2666,10 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
26652666
*/
26662667
if (iocb->ki_pos >> PAGE_SHIFT !=
26672668
ra->prev_pos >> PAGE_SHIFT)
2668-
mark_page_accessed(pvec.pages[0]);
2669+
folio_mark_accessed(fbatch.folios[0]);
26692670

2670-
for (i = 0; i < pagevec_count(&pvec); i++) {
2671-
struct folio *folio = page_folio(pvec.pages[i]);
2671+
for (i = 0; i < folio_batch_count(&fbatch); i++) {
2672+
struct folio *folio = fbatch.folios[i];
26722673
size_t fsize = folio_size(folio);
26732674
size_t offset = iocb->ki_pos & (fsize - 1);
26742675
size_t bytes = min_t(loff_t, end_offset - iocb->ki_pos,
@@ -2698,10 +2699,10 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
26982699
break;
26992700
}
27002701
}
2701-
put_pages:
2702-
for (i = 0; i < pagevec_count(&pvec); i++)
2703-
put_page(pvec.pages[i]);
2704-
pagevec_reinit(&pvec);
2702+
put_folios:
2703+
for (i = 0; i < folio_batch_count(&fbatch); i++)
2704+
folio_put(fbatch.folios[i]);
2705+
folio_batch_init(&fbatch);
27052706
} while (iov_iter_count(iter) && iocb->ki_pos < isize && !error);
27062707

27072708
file_accessed(filp);

0 commit comments

Comments
 (0)