readahead.c 16 KB

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