readahead.c 13 KB

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