readahead.c 13 KB

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