readahead.c 14 KB

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