readahead.c 17 KB

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