readahead.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  2. * mm/readahead.c - address_space-level file readahead.
  3. *
  4. * Copyright (C) 2002, Linus Torvalds
  5. *
  6. * 09Apr2002 Andrew Morton
  7. * Initial version.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/dax.h>
  11. #include <linux/gfp.h>
  12. #include <linux/export.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/task_io_accounting_ops.h>
  16. #include <linux/pagevec.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/file.h>
  20. #include <linux/mm_inline.h>
  21. #include "internal.h"
  22. /*
  23. * Initialise a struct file's readahead state. Assumes that the caller has
  24. * memset *ra to zero.
  25. */
  26. void
  27. file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping)
  28. {
  29. ra->ra_pages = inode_to_bdi(mapping->host)->ra_pages;
  30. ra->prev_pos = -1;
  31. }
  32. EXPORT_SYMBOL_GPL(file_ra_state_init);
  33. /*
  34. * see if a page needs releasing upon read_cache_pages() failure
  35. * - the caller of read_cache_pages() may have set PG_private or PG_fscache
  36. * before calling, such as the NFS fs marking pages that are cached locally
  37. * on disk, thus we need to give the fs a chance to clean up in the event of
  38. * an error
  39. */
  40. static void read_cache_pages_invalidate_page(struct address_space *mapping,
  41. struct page *page)
  42. {
  43. if (page_has_private(page)) {
  44. if (!trylock_page(page))
  45. BUG();
  46. page->mapping = mapping;
  47. do_invalidatepage(page, 0, PAGE_SIZE);
  48. page->mapping = NULL;
  49. unlock_page(page);
  50. }
  51. put_page(page);
  52. }
  53. /*
  54. * release a list of pages, invalidating them first if need be
  55. */
  56. static void read_cache_pages_invalidate_pages(struct address_space *mapping,
  57. struct list_head *pages)
  58. {
  59. struct page *victim;
  60. while (!list_empty(pages)) {
  61. victim = lru_to_page(pages);
  62. list_del(&victim->lru);
  63. read_cache_pages_invalidate_page(mapping, victim);
  64. }
  65. }
  66. /**
  67. * read_cache_pages - populate an address space with some pages & start reads against them
  68. * @mapping: the address_space
  69. * @pages: The address of a list_head which contains the target pages. These
  70. * pages have their ->index populated and are otherwise uninitialised.
  71. * @filler: callback routine for filling a single page.
  72. * @data: private data for the callback routine.
  73. *
  74. * Hides the details of the LRU cache etc from the filesystems.
  75. */
  76. int read_cache_pages(struct address_space *mapping, struct list_head *pages,
  77. int (*filler)(void *, struct page *), void *data)
  78. {
  79. struct page *page;
  80. int ret = 0;
  81. while (!list_empty(pages)) {
  82. page = lru_to_page(pages);
  83. list_del(&page->lru);
  84. if (add_to_page_cache_lru(page, mapping, page->index,
  85. readahead_gfp_mask(mapping))) {
  86. read_cache_pages_invalidate_page(mapping, page);
  87. continue;
  88. }
  89. put_page(page);
  90. ret = filler(data, page);
  91. if (unlikely(ret)) {
  92. read_cache_pages_invalidate_pages(mapping, pages);
  93. break;
  94. }
  95. task_io_account_read(PAGE_SIZE);
  96. }
  97. return ret;
  98. }
  99. EXPORT_SYMBOL(read_cache_pages);
  100. static int read_pages(struct address_space *mapping, struct file *filp,
  101. struct list_head *pages, unsigned int nr_pages, gfp_t gfp)
  102. {
  103. struct blk_plug plug;
  104. unsigned page_idx;
  105. int ret;
  106. blk_start_plug(&plug);
  107. if (mapping->a_ops->readpages) {
  108. ret = mapping->a_ops->readpages(filp, mapping, pages, nr_pages);
  109. /* Clean up the remaining pages */
  110. put_pages_list(pages);
  111. goto out;
  112. }
  113. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  114. struct page *page = lru_to_page(pages);
  115. list_del(&page->lru);
  116. if (!add_to_page_cache_lru(page, mapping, page->index, gfp))
  117. mapping->a_ops->readpage(filp, page);
  118. put_page(page);
  119. }
  120. ret = 0;
  121. out:
  122. blk_finish_plug(&plug);
  123. return ret;
  124. }
  125. /*
  126. * __do_page_cache_readahead() actually reads a chunk of disk. It allocates all
  127. * the pages first, then submits them all for I/O. This avoids the very bad
  128. * behaviour which would occur if page allocations are causing VM writeback.
  129. * We really don't want to intermingle reads and writes like that.
  130. *
  131. * Returns the number of pages requested, or the maximum amount of I/O allowed.
  132. */
  133. int __do_page_cache_readahead(struct address_space *mapping, struct file *filp,
  134. pgoff_t offset, unsigned long nr_to_read,
  135. unsigned long lookahead_size)
  136. {
  137. struct inode *inode = mapping->host;
  138. struct page *page;
  139. unsigned long end_index; /* The last page we want to read */
  140. LIST_HEAD(page_pool);
  141. int page_idx;
  142. int ret = 0;
  143. loff_t isize = i_size_read(inode);
  144. gfp_t gfp_mask = readahead_gfp_mask(mapping);
  145. if (isize == 0)
  146. goto out;
  147. end_index = ((isize - 1) >> PAGE_SHIFT);
  148. /*
  149. * Preallocate as many pages as we will need.
  150. */
  151. for (page_idx = 0; page_idx < nr_to_read; page_idx++) {
  152. pgoff_t page_offset = offset + page_idx;
  153. if (page_offset > end_index)
  154. break;
  155. rcu_read_lock();
  156. page = radix_tree_lookup(&mapping->page_tree, page_offset);
  157. rcu_read_unlock();
  158. if (page && !radix_tree_exceptional_entry(page))
  159. continue;
  160. page = __page_cache_alloc(gfp_mask);
  161. if (!page)
  162. break;
  163. page->index = page_offset;
  164. list_add(&page->lru, &page_pool);
  165. if (page_idx == nr_to_read - lookahead_size)
  166. SetPageReadahead(page);
  167. ret++;
  168. }
  169. /*
  170. * Now start the IO. We ignore I/O errors - if the page is not
  171. * uptodate then the caller will launch readpage again, and
  172. * will then handle the error.
  173. */
  174. if (ret)
  175. read_pages(mapping, filp, &page_pool, ret, gfp_mask);
  176. BUG_ON(!list_empty(&page_pool));
  177. out:
  178. return ret;
  179. }
  180. /*
  181. * Chunk the readahead into 2 megabyte units, so that we don't pin too much
  182. * memory at once.
  183. */
  184. int force_page_cache_readahead(struct address_space *mapping, struct file *filp,
  185. pgoff_t offset, unsigned long nr_to_read)
  186. {
  187. struct backing_dev_info *bdi = inode_to_bdi(mapping->host);
  188. struct file_ra_state *ra = &filp->f_ra;
  189. unsigned long max_pages;
  190. if (unlikely(!mapping->a_ops->readpage && !mapping->a_ops->readpages))
  191. return -EINVAL;
  192. /*
  193. * If the request exceeds the readahead window, allow the read to
  194. * be up to the optimal hardware IO size
  195. */
  196. max_pages = max_t(unsigned long, bdi->io_pages, ra->ra_pages);
  197. nr_to_read = min(nr_to_read, max_pages);
  198. while (nr_to_read) {
  199. int err;
  200. unsigned long this_chunk = (2 * 1024 * 1024) / PAGE_SIZE;
  201. if (this_chunk > nr_to_read)
  202. this_chunk = nr_to_read;
  203. err = __do_page_cache_readahead(mapping, filp,
  204. offset, this_chunk, 0);
  205. if (err < 0)
  206. return err;
  207. offset += this_chunk;
  208. nr_to_read -= this_chunk;
  209. }
  210. return 0;
  211. }
  212. /*
  213. * Set the initial window size, round to next power of 2 and square
  214. * for small size, x 4 for medium, and x 2 for large
  215. * for 128k (32 page) max ra
  216. * 1-8 page = 32k initial, > 8 page = 128k initial
  217. */
  218. static unsigned long get_init_ra_size(unsigned long size, unsigned long max)
  219. {
  220. unsigned long newsize = roundup_pow_of_two(size);
  221. if (newsize <= max / 32)
  222. newsize = newsize * 4;
  223. else if (newsize <= max / 4)
  224. newsize = newsize * 2;
  225. else
  226. newsize = max;
  227. return newsize;
  228. }
  229. /*
  230. * Get the previous window size, ramp it up, and
  231. * return it as the new window size.
  232. */
  233. static unsigned long get_next_ra_size(struct file_ra_state *ra,
  234. unsigned long max)
  235. {
  236. unsigned long cur = ra->size;
  237. unsigned long newsize;
  238. if (cur < max / 16)
  239. newsize = 4 * cur;
  240. else
  241. newsize = 2 * cur;
  242. return min(newsize, max);
  243. }
  244. /*
  245. * On-demand readahead design.
  246. *
  247. * The fields in struct file_ra_state represent the most-recently-executed
  248. * readahead attempt:
  249. *
  250. * |<----- async_size ---------|
  251. * |------------------- size -------------------->|
  252. * |==================#===========================|
  253. * ^start ^page marked with PG_readahead
  254. *
  255. * To overlap application thinking time and disk I/O time, we do
  256. * `readahead pipelining': Do not wait until the application consumed all
  257. * readahead pages and stalled on the missing page at readahead_index;
  258. * Instead, submit an asynchronous readahead I/O as soon as there are
  259. * only async_size pages left in the readahead window. Normally async_size
  260. * will be equal to size, for maximum pipelining.
  261. *
  262. * In interleaved sequential reads, concurrent streams on the same fd can
  263. * be invalidating each other's readahead state. So we flag the new readahead
  264. * page at (start+size-async_size) with PG_readahead, and use it as readahead
  265. * indicator. The flag won't be set on already cached pages, to avoid the
  266. * readahead-for-nothing fuss, saving pointless page cache lookups.
  267. *
  268. * prev_pos tracks the last visited byte in the _previous_ read request.
  269. * It should be maintained by the caller, and will be used for detecting
  270. * small random reads. Note that the readahead algorithm checks loosely
  271. * for sequential patterns. Hence interleaved reads might be served as
  272. * sequential ones.
  273. *
  274. * There is a special-case: if the first page which the application tries to
  275. * read happens to be the first page of the file, it is assumed that a linear
  276. * read is about to happen and the window is immediately set to the initial size
  277. * based on I/O request size and the max_readahead.
  278. *
  279. * The code ramps up the readahead size aggressively at first, but slow down as
  280. * it approaches max_readhead.
  281. */
  282. /*
  283. * Count contiguously cached pages from @offset-1 to @offset-@max,
  284. * this count is a conservative estimation of
  285. * - length of the sequential read sequence, or
  286. * - thrashing threshold in memory tight systems
  287. */
  288. static pgoff_t count_history_pages(struct address_space *mapping,
  289. pgoff_t offset, unsigned long max)
  290. {
  291. pgoff_t head;
  292. rcu_read_lock();
  293. head = page_cache_prev_hole(mapping, offset - 1, max);
  294. rcu_read_unlock();
  295. return offset - 1 - head;
  296. }
  297. /*
  298. * page cache context based read-ahead
  299. */
  300. static int try_context_readahead(struct address_space *mapping,
  301. struct file_ra_state *ra,
  302. pgoff_t offset,
  303. unsigned long req_size,
  304. unsigned long max)
  305. {
  306. pgoff_t size;
  307. size = count_history_pages(mapping, offset, max);
  308. /*
  309. * not enough history pages:
  310. * it could be a random read
  311. */
  312. if (size <= req_size)
  313. return 0;
  314. /*
  315. * starts from beginning of file:
  316. * it is a strong indication of long-run stream (or whole-file-read)
  317. */
  318. if (size >= offset)
  319. size *= 2;
  320. ra->start = offset;
  321. ra->size = min(size + req_size, max);
  322. ra->async_size = 1;
  323. return 1;
  324. }
  325. /*
  326. * A minimal readahead algorithm for trivial sequential/random reads.
  327. */
  328. static unsigned long
  329. ondemand_readahead(struct address_space *mapping,
  330. struct file_ra_state *ra, struct file *filp,
  331. bool hit_readahead_marker, pgoff_t offset,
  332. unsigned long req_size)
  333. {
  334. struct backing_dev_info *bdi = inode_to_bdi(mapping->host);
  335. unsigned long max_pages = ra->ra_pages;
  336. pgoff_t prev_offset;
  337. /*
  338. * If the request exceeds the readahead window, allow the read to
  339. * be up to the optimal hardware IO size
  340. */
  341. if (req_size > max_pages && bdi->io_pages > max_pages)
  342. max_pages = min(req_size, bdi->io_pages);
  343. /*
  344. * start of file
  345. */
  346. if (!offset)
  347. goto initial_readahead;
  348. /*
  349. * It's the expected callback offset, assume sequential access.
  350. * Ramp up sizes, and push forward the readahead window.
  351. */
  352. if ((offset == (ra->start + ra->size - ra->async_size) ||
  353. offset == (ra->start + ra->size))) {
  354. ra->start += ra->size;
  355. ra->size = get_next_ra_size(ra, max_pages);
  356. ra->async_size = ra->size;
  357. goto readit;
  358. }
  359. /*
  360. * Hit a marked page without valid readahead state.
  361. * E.g. interleaved reads.
  362. * Query the pagecache for async_size, which normally equals to
  363. * readahead size. Ramp it up and use it as the new readahead size.
  364. */
  365. if (hit_readahead_marker) {
  366. pgoff_t start;
  367. rcu_read_lock();
  368. start = page_cache_next_hole(mapping, offset + 1, max_pages);
  369. rcu_read_unlock();
  370. if (!start || start - offset > max_pages)
  371. return 0;
  372. ra->start = start;
  373. ra->size = start - offset; /* old async_size */
  374. ra->size += req_size;
  375. ra->size = get_next_ra_size(ra, max_pages);
  376. ra->async_size = ra->size;
  377. goto readit;
  378. }
  379. /*
  380. * oversize read
  381. */
  382. if (req_size > max_pages)
  383. goto initial_readahead;
  384. /*
  385. * sequential cache miss
  386. * trivial case: (offset - prev_offset) == 1
  387. * unaligned reads: (offset - prev_offset) == 0
  388. */
  389. prev_offset = (unsigned long long)ra->prev_pos >> PAGE_SHIFT;
  390. if (offset - prev_offset <= 1UL)
  391. goto initial_readahead;
  392. /*
  393. * Query the page cache and look for the traces(cached history pages)
  394. * that a sequential stream would leave behind.
  395. */
  396. if (try_context_readahead(mapping, ra, offset, req_size, max_pages))
  397. goto readit;
  398. /*
  399. * standalone, small random read
  400. * Read as is, and do not pollute the readahead state.
  401. */
  402. return __do_page_cache_readahead(mapping, filp, offset, req_size, 0);
  403. initial_readahead:
  404. ra->start = offset;
  405. ra->size = get_init_ra_size(req_size, max_pages);
  406. ra->async_size = ra->size > req_size ? ra->size - req_size : ra->size;
  407. readit:
  408. /*
  409. * Will this read hit the readahead marker made by itself?
  410. * If so, trigger the readahead marker hit now, and merge
  411. * the resulted next readahead window into the current one.
  412. */
  413. if (offset == ra->start && ra->size == ra->async_size) {
  414. ra->async_size = get_next_ra_size(ra, max_pages);
  415. ra->size += ra->async_size;
  416. }
  417. return ra_submit(ra, mapping, filp);
  418. }
  419. /**
  420. * page_cache_sync_readahead - generic file readahead
  421. * @mapping: address_space which holds the pagecache and I/O vectors
  422. * @ra: file_ra_state which holds the readahead state
  423. * @filp: passed on to ->readpage() and ->readpages()
  424. * @offset: start offset into @mapping, in pagecache page-sized units
  425. * @req_size: hint: total size of the read which the caller is performing in
  426. * pagecache pages
  427. *
  428. * page_cache_sync_readahead() should be called when a cache miss happened:
  429. * it will submit the read. The readahead logic may decide to piggyback more
  430. * pages onto the read request if access patterns suggest it will improve
  431. * performance.
  432. */
  433. void page_cache_sync_readahead(struct address_space *mapping,
  434. struct file_ra_state *ra, struct file *filp,
  435. pgoff_t offset, unsigned long req_size)
  436. {
  437. /* no read-ahead */
  438. if (!ra->ra_pages)
  439. return;
  440. /* be dumb */
  441. if (filp && (filp->f_mode & FMODE_RANDOM)) {
  442. force_page_cache_readahead(mapping, filp, offset, req_size);
  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. * page_cache_async_readahead - file readahead for marked pages
  451. * @mapping: address_space which holds the pagecache and I/O vectors
  452. * @ra: file_ra_state which holds the readahead state
  453. * @filp: passed on to ->readpage() and ->readpages()
  454. * @page: the page at @offset which has the PG_readahead flag set
  455. * @offset: start offset into @mapping, in pagecache page-sized units
  456. * @req_size: hint: total size of the read which the caller is performing in
  457. * pagecache pages
  458. *
  459. * page_cache_async_readahead() should be called when a page is used which
  460. * has the PG_readahead flag; this is a marker to suggest that the application
  461. * has used up enough of the readahead window that we should start pulling in
  462. * more pages.
  463. */
  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. * Same bit is used for PG_readahead and PG_reclaim.
  475. */
  476. if (PageWriteback(page))
  477. return;
  478. ClearPageReadahead(page);
  479. /*
  480. * Defer asynchronous read-ahead on IO congestion.
  481. */
  482. if (inode_read_congested(mapping->host))
  483. return;
  484. /* do read-ahead */
  485. ondemand_readahead(mapping, ra, filp, true, offset, req_size);
  486. }
  487. EXPORT_SYMBOL_GPL(page_cache_async_readahead);
  488. static ssize_t
  489. do_readahead(struct address_space *mapping, struct file *filp,
  490. pgoff_t index, unsigned long nr)
  491. {
  492. if (!mapping || !mapping->a_ops)
  493. return -EINVAL;
  494. /*
  495. * Readahead doesn't make sense for DAX inodes, but we don't want it
  496. * to report a failure either. Instead, we just return success and
  497. * don't do any work.
  498. */
  499. if (dax_mapping(mapping))
  500. return 0;
  501. return force_page_cache_readahead(mapping, filp, index, nr);
  502. }
  503. SYSCALL_DEFINE3(readahead, int, fd, loff_t, offset, size_t, count)
  504. {
  505. ssize_t ret;
  506. struct fd f;
  507. ret = -EBADF;
  508. f = fdget(fd);
  509. if (f.file) {
  510. if (f.file->f_mode & FMODE_READ) {
  511. struct address_space *mapping = f.file->f_mapping;
  512. pgoff_t start = offset >> PAGE_SHIFT;
  513. pgoff_t end = (offset + count - 1) >> PAGE_SHIFT;
  514. unsigned long len = end - start + 1;
  515. ret = do_readahead(mapping, f.file, start, len);
  516. }
  517. fdput(f);
  518. }
  519. return ret;
  520. }