readahead.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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/fs.h>
  11. #include <linux/gfp.h>
  12. #include <linux/mm.h>
  13. #include <linux/export.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/backing-dev.h>
  16. #include <linux/task_io_accounting_ops.h>
  17. #include <linux/pagevec.h>
  18. #include <linux/pagemap.h>
  19. /*
  20. * Initialise a struct file's readahead state. Assumes that the caller has
  21. * memset *ra to zero.
  22. */
  23. void
  24. file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping)
  25. {
  26. ra->ra_pages = mapping->backing_dev_info->ra_pages;
  27. ra->prev_pos = -1;
  28. }
  29. EXPORT_SYMBOL_GPL(file_ra_state_init);
  30. #define list_to_page(head) (list_entry((head)->prev, struct page, lru))
  31. /*
  32. * see if a page needs releasing upon read_cache_pages() failure
  33. * - the caller of read_cache_pages() may have set PG_private or PG_fscache
  34. * before calling, such as the NFS fs marking pages that are cached locally
  35. * on disk, thus we need to give the fs a chance to clean up in the event of
  36. * an error
  37. */
  38. static void read_cache_pages_invalidate_page(struct address_space *mapping,
  39. struct page *page)
  40. {
  41. if (page_has_private(page)) {
  42. if (!trylock_page(page))
  43. BUG();
  44. page->mapping = mapping;
  45. do_invalidatepage(page, 0);
  46. page->mapping = NULL;
  47. unlock_page(page);
  48. }
  49. page_cache_release(page);
  50. }
  51. /*
  52. * release a list of pages, invalidating them first if need be
  53. */
  54. static void read_cache_pages_invalidate_pages(struct address_space *mapping,
  55. struct list_head *pages)
  56. {
  57. struct page *victim;
  58. while (!list_empty(pages)) {
  59. victim = list_to_page(pages);
  60. list_del(&victim->lru);
  61. read_cache_pages_invalidate_page(mapping, victim);
  62. }
  63. }
  64. /**
  65. * read_cache_pages - populate an address space with some pages & start reads against them
  66. * @mapping: the address_space
  67. * @pages: The address of a list_head which contains the target pages. These
  68. * pages have their ->index populated and are otherwise uninitialised.
  69. * @filler: callback routine for filling a single page.
  70. * @data: private data for the callback routine.
  71. *
  72. * Hides the details of the LRU cache etc from the filesystems.
  73. */
  74. int read_cache_pages(struct address_space *mapping, struct list_head *pages,
  75. int (*filler)(void *, struct page *), void *data)
  76. {
  77. struct page *page;
  78. int ret = 0;
  79. while (!list_empty(pages)) {
  80. page = list_to_page(pages);
  81. list_del(&page->lru);
  82. if (add_to_page_cache_lru(page, mapping,
  83. page->index, GFP_KERNEL)) {
  84. read_cache_pages_invalidate_page(mapping, page);
  85. continue;
  86. }
  87. page_cache_release(page);
  88. ret = filler(data, page);
  89. if (unlikely(ret)) {
  90. read_cache_pages_invalidate_pages(mapping, pages);
  91. break;
  92. }
  93. task_io_account_read(PAGE_CACHE_SIZE);
  94. }
  95. return ret;
  96. }
  97. EXPORT_SYMBOL(read_cache_pages);
  98. static int read_pages(struct address_space *mapping, struct file *filp,
  99. struct list_head *pages, unsigned nr_pages)
  100. {
  101. struct blk_plug plug;
  102. unsigned page_idx;
  103. int ret;
  104. blk_start_plug(&plug);
  105. if (mapping->a_ops->readpages) {
  106. ret = mapping->a_ops->readpages(filp, mapping, pages, nr_pages);
  107. /* Clean up the remaining pages */
  108. put_pages_list(pages);
  109. goto out;
  110. }
  111. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  112. struct page *page = list_to_page(pages);
  113. list_del(&page->lru);
  114. if (!add_to_page_cache_lru(page, mapping,
  115. page->index, GFP_KERNEL)) {
  116. mapping->a_ops->readpage(filp, page);
  117. }
  118. page_cache_release(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. static int
  134. __do_page_cache_readahead(struct address_space *mapping, struct file *filp,
  135. 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. int ret = 0;
  144. loff_t isize = i_size_read(inode);
  145. #ifdef CONFIG_SCFS_LOWER_PAGECACHE_INVALIDATION
  146. //struct scfs_sb_info *sbi;
  147. #endif
  148. if (isize == 0)
  149. goto out;
  150. end_index = ((isize - 1) >> PAGE_CACHE_SHIFT);
  151. /*
  152. * Preallocate as many pages as we will need.
  153. */
  154. for (page_idx = 0; page_idx < nr_to_read; page_idx++) {
  155. pgoff_t page_offset = offset + page_idx;
  156. if (page_offset > end_index)
  157. break;
  158. rcu_read_lock();
  159. page = radix_tree_lookup(&mapping->page_tree, page_offset);
  160. rcu_read_unlock();
  161. if (page)
  162. continue;
  163. page = page_cache_alloc_readahead(mapping);
  164. if (!page)
  165. break;
  166. #ifdef CONFIG_SCFS_LOWER_PAGECACHE_INVALIDATION
  167. /*
  168. if (filp->f_flags & O_SCFSLOWER) {
  169. sbi = ;
  170. sbi->scfs_lowerpage_alloc_count++;
  171. }
  172. */
  173. #endif
  174. page->index = page_offset;
  175. page->flags |= (1L << PG_readahead);
  176. list_add(&page->lru, &page_pool);
  177. if (page_idx == nr_to_read - lookahead_size)
  178. SetPageReadahead(page);
  179. ret++;
  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 (ret)
  187. read_pages(mapping, filp, &page_pool, ret);
  188. BUG_ON(!list_empty(&page_pool));
  189. out:
  190. return ret;
  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. int ret = 0;
  200. if (unlikely(!mapping->a_ops->readpage && !mapping->a_ops->readpages))
  201. return -EINVAL;
  202. nr_to_read = max_sane_readahead(nr_to_read);
  203. while (nr_to_read) {
  204. int err;
  205. unsigned long this_chunk = (2 * 1024 * 1024) / PAGE_CACHE_SIZE;
  206. if (this_chunk > nr_to_read)
  207. this_chunk = nr_to_read;
  208. err = __do_page_cache_readahead(mapping, filp,
  209. offset, this_chunk, 0);
  210. if (err < 0) {
  211. ret = err;
  212. break;
  213. }
  214. ret += err;
  215. offset += this_chunk;
  216. nr_to_read -= this_chunk;
  217. }
  218. return ret;
  219. }
  220. /*
  221. * Given a desired number of PAGE_CACHE_SIZE readahead pages, return a
  222. * sensible upper limit.
  223. */
  224. unsigned long max_sane_readahead(unsigned long nr)
  225. {
  226. return min(nr, (node_page_state(numa_node_id(), NR_INACTIVE_FILE)
  227. + node_page_state(numa_node_id(), NR_FREE_PAGES)) / 2);
  228. }
  229. /*
  230. * Submit IO for the read-ahead request in file_ra_state.
  231. */
  232. unsigned long ra_submit(struct file_ra_state *ra,
  233. struct address_space *mapping, struct file *filp)
  234. {
  235. int actual;
  236. actual = __do_page_cache_readahead(mapping, filp,
  237. ra->start, ra->size, ra->async_size);
  238. return actual;
  239. }
  240. /*
  241. * Set the initial window size, round to next power of 2 and square
  242. * Small size is not dependant on max value - only a one-page read is regarded
  243. * as small.
  244. * for small size, x 4 for medium, and x 2 for large
  245. * for 128k (32 page) max ra
  246. * 1-8 page = 32k initial, > 8 page = 128k initial
  247. */
  248. static unsigned long get_init_ra_size(unsigned long size, unsigned long max)
  249. {
  250. unsigned long newsize = roundup_pow_of_two(size);
  251. if (newsize <= 1)
  252. newsize = newsize * 4;
  253. else if (newsize <= max / 4)
  254. newsize = newsize * 2;
  255. else
  256. newsize = max;
  257. return newsize;
  258. }
  259. /*
  260. * Get the previous window size, ramp it up, and
  261. * return it as the new window size.
  262. */
  263. static unsigned long get_next_ra_size(struct file_ra_state *ra,
  264. unsigned long max)
  265. {
  266. unsigned long cur = ra->size;
  267. unsigned long newsize;
  268. if (cur < max / 16)
  269. newsize = 4 * cur;
  270. else
  271. newsize = 2 * cur;
  272. return min(newsize, max);
  273. }
  274. /*
  275. * On-demand readahead design.
  276. *
  277. * The fields in struct file_ra_state represent the most-recently-executed
  278. * readahead attempt:
  279. *
  280. * |<----- async_size ---------|
  281. * |------------------- size -------------------->|
  282. * |==================#===========================|
  283. * ^start ^page marked with PG_readahead
  284. *
  285. * To overlap application thinking time and disk I/O time, we do
  286. * `readahead pipelining': Do not wait until the application consumed all
  287. * readahead pages and stalled on the missing page at readahead_index;
  288. * Instead, submit an asynchronous readahead I/O as soon as there are
  289. * only async_size pages left in the readahead window. Normally async_size
  290. * will be equal to size, for maximum pipelining.
  291. *
  292. * In interleaved sequential reads, concurrent streams on the same fd can
  293. * be invalidating each other's readahead state. So we flag the new readahead
  294. * page at (start+size-async_size) with PG_readahead, and use it as readahead
  295. * indicator. The flag won't be set on already cached pages, to avoid the
  296. * readahead-for-nothing fuss, saving pointless page cache lookups.
  297. *
  298. * prev_pos tracks the last visited byte in the _previous_ read request.
  299. * It should be maintained by the caller, and will be used for detecting
  300. * small random reads. Note that the readahead algorithm checks loosely
  301. * for sequential patterns. Hence interleaved reads might be served as
  302. * sequential ones.
  303. *
  304. * There is a special-case: if the first page which the application tries to
  305. * read happens to be the first page of the file, it is assumed that a linear
  306. * read is about to happen and the window is immediately set to the initial size
  307. * based on I/O request size and the max_readahead.
  308. *
  309. * The code ramps up the readahead size aggressively at first, but slow down as
  310. * it approaches max_readhead.
  311. */
  312. /*
  313. * Count contiguously cached pages from @offset-1 to @offset-@max,
  314. * this count is a conservative estimation of
  315. * - length of the sequential read sequence, or
  316. * - thrashing threshold in memory tight systems
  317. */
  318. static pgoff_t count_history_pages(struct address_space *mapping,
  319. struct file_ra_state *ra,
  320. pgoff_t offset, unsigned long max)
  321. {
  322. pgoff_t head;
  323. rcu_read_lock();
  324. head = page_cache_prev_hole(mapping, offset - 1, max);
  325. rcu_read_unlock();
  326. return offset - 1 - head;
  327. }
  328. /*
  329. * page cache context based read-ahead
  330. */
  331. static int try_context_readahead(struct address_space *mapping,
  332. struct file_ra_state *ra,
  333. pgoff_t offset,
  334. unsigned long req_size,
  335. unsigned long max)
  336. {
  337. pgoff_t size;
  338. size = count_history_pages(mapping, ra, offset, max);
  339. /*
  340. * not enough history pages:
  341. * it could be a random read
  342. */
  343. if (size <= req_size)
  344. return 0;
  345. /*
  346. * starts from beginning of file:
  347. * it is a strong indication of long-run stream (or whole-file-read)
  348. */
  349. if (size >= offset)
  350. size *= 2;
  351. ra->start = offset;
  352. ra->size = min(size + req_size, max);
  353. ra->async_size = 1;
  354. return 1;
  355. }
  356. /*
  357. * A minimal readahead algorithm for trivial sequential/random reads.
  358. */
  359. static unsigned long
  360. ondemand_readahead(struct address_space *mapping,
  361. struct file_ra_state *ra, struct file *filp,
  362. bool hit_readahead_marker, pgoff_t offset,
  363. unsigned long req_size)
  364. {
  365. unsigned long max = max_sane_readahead(ra->ra_pages);
  366. /*
  367. * start of file
  368. */
  369. if (!offset)
  370. goto initial_readahead;
  371. /*
  372. * It's the expected callback offset, assume sequential access.
  373. * Ramp up sizes, and push forward the readahead window.
  374. */
  375. if ((offset == (ra->start + ra->size - ra->async_size) ||
  376. offset == (ra->start + ra->size))) {
  377. ra->start += ra->size;
  378. ra->size = get_next_ra_size(ra, max);
  379. ra->async_size = ra->size;
  380. goto readit;
  381. }
  382. /*
  383. * Hit a marked page without valid readahead state.
  384. * E.g. interleaved reads.
  385. * Query the pagecache for async_size, which normally equals to
  386. * readahead size. Ramp it up and use it as the new readahead size.
  387. */
  388. if (hit_readahead_marker) {
  389. pgoff_t start;
  390. rcu_read_lock();
  391. start = page_cache_next_hole(mapping, offset + 1, max);
  392. rcu_read_unlock();
  393. if (!start || start - offset > max)
  394. return 0;
  395. ra->start = start;
  396. ra->size = start - offset; /* old async_size */
  397. ra->size += req_size;
  398. ra->size = get_next_ra_size(ra, max);
  399. ra->async_size = ra->size;
  400. goto readit;
  401. }
  402. /*
  403. * oversize read
  404. */
  405. if (req_size > max)
  406. goto initial_readahead;
  407. /*
  408. * sequential cache miss
  409. */
  410. if (offset - (ra->prev_pos >> PAGE_CACHE_SHIFT) <= 1UL)
  411. goto initial_readahead;
  412. /*
  413. * Query the page cache and look for the traces(cached history pages)
  414. * that a sequential stream would leave behind.
  415. */
  416. if (try_context_readahead(mapping, ra, offset, req_size, max))
  417. goto readit;
  418. /*
  419. * standalone, small random read
  420. * Read as is, and do not pollute the readahead state.
  421. */
  422. return __do_page_cache_readahead(mapping, filp, offset, req_size, 0);
  423. initial_readahead:
  424. ra->start = offset;
  425. ra->size = get_init_ra_size(req_size, max);
  426. ra->async_size = ra->size > req_size ? ra->size - req_size : ra->size;
  427. readit:
  428. /*
  429. * Will this read hit the readahead marker made by itself?
  430. * If so, trigger the readahead marker hit now, and merge
  431. * the resulted next readahead window into the current one.
  432. */
  433. if (offset == ra->start && ra->size == ra->async_size) {
  434. ra->async_size = get_next_ra_size(ra, max);
  435. ra->size += ra->async_size;
  436. }
  437. return ra_submit(ra, mapping, filp);
  438. }
  439. /**
  440. * page_cache_sync_readahead - generic file readahead
  441. * @mapping: address_space which holds the pagecache and I/O vectors
  442. * @ra: file_ra_state which holds the readahead state
  443. * @filp: passed on to ->readpage() and ->readpages()
  444. * @offset: start offset into @mapping, in pagecache page-sized units
  445. * @req_size: hint: total size of the read which the caller is performing in
  446. * pagecache pages
  447. *
  448. * page_cache_sync_readahead() should be called when a cache miss happened:
  449. * it will submit the read. The readahead logic may decide to piggyback more
  450. * pages onto the read request if access patterns suggest it will improve
  451. * performance.
  452. */
  453. void page_cache_sync_readahead(struct address_space *mapping,
  454. struct file_ra_state *ra, struct file *filp,
  455. pgoff_t offset, unsigned long req_size)
  456. {
  457. /* no read-ahead */
  458. if (!ra->ra_pages)
  459. return;
  460. /* be dumb */
  461. if (filp && (filp->f_mode & FMODE_RANDOM)) {
  462. force_page_cache_readahead(mapping, filp, offset, req_size);
  463. return;
  464. }
  465. /* do read-ahead */
  466. ondemand_readahead(mapping, ra, filp, false, offset, req_size);
  467. }
  468. EXPORT_SYMBOL_GPL(page_cache_sync_readahead);
  469. /**
  470. * page_cache_async_readahead - file readahead for marked pages
  471. * @mapping: address_space which holds the pagecache and I/O vectors
  472. * @ra: file_ra_state which holds the readahead state
  473. * @filp: passed on to ->readpage() and ->readpages()
  474. * @page: the page at @offset which has the PG_readahead flag set
  475. * @offset: start offset into @mapping, in pagecache page-sized units
  476. * @req_size: hint: total size of the read which the caller is performing in
  477. * pagecache pages
  478. *
  479. * page_cache_async_readahead() should be called when a page is used which
  480. * has the PG_readahead flag; this is a marker to suggest that the application
  481. * has used up enough of the readahead window that we should start pulling in
  482. * more pages.
  483. */
  484. void
  485. page_cache_async_readahead(struct address_space *mapping,
  486. struct file_ra_state *ra, struct file *filp,
  487. struct page *page, pgoff_t offset,
  488. unsigned long req_size)
  489. {
  490. /* no read-ahead */
  491. if (!ra->ra_pages)
  492. return;
  493. /*
  494. * Same bit is used for PG_readahead and PG_reclaim.
  495. */
  496. if (PageWriteback(page))
  497. return;
  498. ClearPageReadahead(page);
  499. /*
  500. * Defer asynchronous read-ahead on IO congestion.
  501. */
  502. if (bdi_read_congested(mapping->backing_dev_info))
  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);