Skip to content

Commit 8026e49

Browse files
axboeakpm00
authored andcommitted
mm/filemap: add read support for RWF_DONTCACHE
Add RWF_DONTCACHE as a read operation flag, which means that any data read wil be removed from the page cache upon completion. Uses the page cache to synchronize, and simply prunes folios that were instantiated when the operation completes. While it would be possible to use private pages for this, using the page cache as synchronization is handy for a variety of reasons: 1) No special truncate magic is needed 2) Async buffered reads need some place to serialize, using the page cache is a lot easier than writing extra code for this 3) The pruning cost is pretty reasonable and the code to support this is much simpler as a result. You can think of uncached buffered IO as being the much more attractive cousin of O_DIRECT - it has none of the restrictions of O_DIRECT. Yes, it will copy the data, but unlike regular buffered IO, it doesn't run into the unpredictability of the page cache in terms of reclaim. As an example, on a test box with 32 drives, reading them with buffered IO looks as follows: Reading bs 65536, uncached 0 1s: 145945MB/sec 2s: 158067MB/sec 3s: 157007MB/sec 4s: 148622MB/sec 5s: 118824MB/sec 6s: 70494MB/sec 7s: 41754MB/sec 8s: 90811MB/sec 9s: 92204MB/sec 10s: 95178MB/sec 11s: 95488MB/sec 12s: 95552MB/sec 13s: 96275MB/sec where it's quite easy to see where the page cache filled up, and performance went from good to erratic, and finally settles at a much lower rate. Looking at top while this is ongoing, we see: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 7535 root 20 0 267004 0 0 S 3199 0.0 8:40.65 uncached 3326 root 20 0 0 0 0 R 100.0 0.0 0:16.40 kswapd4 3327 root 20 0 0 0 0 R 100.0 0.0 0:17.22 kswapd5 3328 root 20 0 0 0 0 R 100.0 0.0 0:13.29 kswapd6 3332 root 20 0 0 0 0 R 100.0 0.0 0:11.11 kswapd10 3339 root 20 0 0 0 0 R 100.0 0.0 0:16.25 kswapd17 3348 root 20 0 0 0 0 R 100.0 0.0 0:16.40 kswapd26 3343 root 20 0 0 0 0 R 100.0 0.0 0:16.30 kswapd21 3344 root 20 0 0 0 0 R 100.0 0.0 0:11.92 kswapd22 3349 root 20 0 0 0 0 R 100.0 0.0 0:16.28 kswapd27 3352 root 20 0 0 0 0 R 99.7 0.0 0:11.89 kswapd30 3353 root 20 0 0 0 0 R 96.7 0.0 0:16.04 kswapd31 3329 root 20 0 0 0 0 R 96.4 0.0 0:11.41 kswapd7 3345 root 20 0 0 0 0 R 96.4 0.0 0:13.40 kswapd23 3330 root 20 0 0 0 0 S 91.1 0.0 0:08.28 kswapd8 3350 root 20 0 0 0 0 S 86.8 0.0 0:11.13 kswapd28 3325 root 20 0 0 0 0 S 76.3 0.0 0:07.43 kswapd3 3341 root 20 0 0 0 0 S 74.7 0.0 0:08.85 kswapd19 3334 root 20 0 0 0 0 S 71.7 0.0 0:10.04 kswapd12 3351 root 20 0 0 0 0 R 60.5 0.0 0:09.59 kswapd29 3323 root 20 0 0 0 0 R 57.6 0.0 0:11.50 kswapd1 [...] which is just showing a partial list of the 32 kswapd threads that are running mostly full tilt, burning ~28 full CPU cores. If the same test case is run with RWF_DONTCACHE set for the buffered read, the output looks as follows: Reading bs 65536, uncached 0 1s: 153144MB/sec 2s: 156760MB/sec 3s: 158110MB/sec 4s: 158009MB/sec 5s: 158043MB/sec 6s: 157638MB/sec 7s: 157999MB/sec 8s: 158024MB/sec 9s: 157764MB/sec 10s: 157477MB/sec 11s: 157417MB/sec 12s: 157455MB/sec 13s: 157233MB/sec 14s: 156692MB/sec which is just chugging along at ~155GB/sec of read performance. Looking at top, we see: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 7961 root 20 0 267004 0 0 S 3180 0.0 5:37.95 uncached 8024 axboe 20 0 14292 4096 0 R 1.0 0.0 0:00.13 top where just the test app is using CPU, no reclaim is taking place outside of the main thread. Not only is performance 65% better, it's also using half the CPU to do it. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]> Cc: Brian Foster <[email protected]> Cc: Chris Mason <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Kirill A. Shutemov <[email protected]> Cc: Matthew Wilcox (Oracle) <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent b9f958d commit 8026e49

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

mm/filemap.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,6 +2445,8 @@ static int filemap_create_folio(struct kiocb *iocb, struct folio_batch *fbatch)
24452445
folio = filemap_alloc_folio(mapping_gfp_mask(mapping), min_order);
24462446
if (!folio)
24472447
return -ENOMEM;
2448+
if (iocb->ki_flags & IOCB_DONTCACHE)
2449+
__folio_set_dropbehind(folio);
24482450

24492451
/*
24502452
* Protect against truncate / hole punch. Grabbing invalidate_lock
@@ -2490,6 +2492,8 @@ static int filemap_readahead(struct kiocb *iocb, struct file *file,
24902492

24912493
if (iocb->ki_flags & IOCB_NOIO)
24922494
return -EAGAIN;
2495+
if (iocb->ki_flags & IOCB_DONTCACHE)
2496+
ractl.dropbehind = 1;
24932497
page_cache_async_ra(&ractl, folio, last_index - folio->index);
24942498
return 0;
24952499
}
@@ -2519,6 +2523,8 @@ static int filemap_get_pages(struct kiocb *iocb, size_t count,
25192523
return -EAGAIN;
25202524
if (iocb->ki_flags & IOCB_NOWAIT)
25212525
flags = memalloc_noio_save();
2526+
if (iocb->ki_flags & IOCB_DONTCACHE)
2527+
ractl.dropbehind = 1;
25222528
page_cache_sync_ra(&ractl, last_index - index);
25232529
if (iocb->ki_flags & IOCB_NOWAIT)
25242530
memalloc_noio_restore(flags);
@@ -2566,6 +2572,20 @@ static inline bool pos_same_folio(loff_t pos1, loff_t pos2, struct folio *folio)
25662572
return (pos1 >> shift == pos2 >> shift);
25672573
}
25682574

2575+
static void filemap_end_dropbehind_read(struct address_space *mapping,
2576+
struct folio *folio)
2577+
{
2578+
if (!folio_test_dropbehind(folio))
2579+
return;
2580+
if (folio_test_writeback(folio) || folio_test_dirty(folio))
2581+
return;
2582+
if (folio_trylock(folio)) {
2583+
if (folio_test_clear_dropbehind(folio))
2584+
folio_unmap_invalidate(mapping, folio, 0);
2585+
folio_unlock(folio);
2586+
}
2587+
}
2588+
25692589
/**
25702590
* filemap_read - Read data from the page cache.
25712591
* @iocb: The iocb to read.
@@ -2679,8 +2699,12 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
26792699
}
26802700
}
26812701
put_folios:
2682-
for (i = 0; i < folio_batch_count(&fbatch); i++)
2683-
folio_put(fbatch.folios[i]);
2702+
for (i = 0; i < folio_batch_count(&fbatch); i++) {
2703+
struct folio *folio = fbatch.folios[i];
2704+
2705+
filemap_end_dropbehind_read(mapping, folio);
2706+
folio_put(folio);
2707+
}
26842708
folio_batch_init(&fbatch);
26852709
} while (iov_iter_count(iter) && iocb->ki_pos < isize && !error);
26862710

mm/swap.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,8 @@ static bool lru_gen_clear_refs(struct folio *folio)
448448
*/
449449
void folio_mark_accessed(struct folio *folio)
450450
{
451+
if (folio_test_dropbehind(folio))
452+
return;
451453
if (lru_gen_enabled()) {
452454
lru_gen_inc_refs(folio);
453455
return;

0 commit comments

Comments
 (0)