readahead.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * mm/readahead.c - address_space-level file readahead.
  3. *
  4. * Copyright (C) 2002, Linus Torvalds
  5. *
  6. * 09Apr2002 akpm@zip.com.au
  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)
  205. + node_page_state(numa_node_id(), NR_FREE_PAGES)) / 2);
  206. }
  207. static int __init readahead_init(void)
  208. {
  209. return bdi_init(&default_backing_dev_info);
  210. }
  211. subsys_initcall(readahead_init);
  212. /*
  213. * Submit IO for the read-ahead request in file_ra_state.
  214. */
  215. static unsigned long ra_submit(struct file_ra_state *ra,
  216. struct address_space *mapping, struct file *filp)
  217. {
  218. int actual;
  219. actual = __do_page_cache_readahead(mapping, filp,
  220. ra->start, ra->size, ra->async_size);
  221. return actual;
  222. }
  223. /*
  224. * Set the initial window size, round to next power of 2 and square
  225. * for small size, x 4 for medium, and x 2 for large
  226. * for 128k (32 page) max ra
  227. * 1-8 page = 32k initial, > 8 page = 128k initial
  228. */
  229. static unsigned long get_init_ra_size(unsigned long size, unsigned long max)
  230. {
  231. unsigned long newsize = roundup_pow_of_two(size);
  232. if (newsize <= max / 32)
  233. newsize = newsize * 4;
  234. else if (newsize <= max / 4)
  235. newsize = newsize * 2;
  236. else
  237. newsize = max;
  238. return newsize;
  239. }
  240. /*
  241. * Get the previous window size, ramp it up, and
  242. * return it as the new window size.
  243. */
  244. static unsigned long get_next_ra_size(struct file_ra_state *ra,
  245. unsigned long max)
  246. {
  247. unsigned long cur = ra->size;
  248. unsigned long newsize;
  249. if (cur < max / 16)
  250. newsize = 4 * cur;
  251. else
  252. newsize = 2 * cur;
  253. return min(newsize, max);
  254. }
  255. /*
  256. * On-demand readahead design.
  257. *
  258. * The fields in struct file_ra_state represent the most-recently-executed
  259. * readahead attempt:
  260. *
  261. * |<----- async_size ---------|
  262. * |------------------- size -------------------->|
  263. * |==================#===========================|
  264. * ^start ^page marked with PG_readahead
  265. *
  266. * To overlap application thinking time and disk I/O time, we do
  267. * `readahead pipelining': Do not wait until the application consumed all
  268. * readahead pages and stalled on the missing page at readahead_index;
  269. * Instead, submit an asynchronous readahead I/O as soon as there are
  270. * only async_size pages left in the readahead window. Normally async_size
  271. * will be equal to size, for maximum pipelining.
  272. *
  273. * In interleaved sequential reads, concurrent streams on the same fd can
  274. * be invalidating each other's readahead state. So we flag the new readahead
  275. * page at (start+size-async_size) with PG_readahead, and use it as readahead
  276. * indicator. The flag won't be set on already cached pages, to avoid the
  277. * readahead-for-nothing fuss, saving pointless page cache lookups.
  278. *
  279. * prev_pos tracks the last visited byte in the _previous_ read request.
  280. * It should be maintained by the caller, and will be used for detecting
  281. * small random reads. Note that the readahead algorithm checks loosely
  282. * for sequential patterns. Hence interleaved reads might be served as
  283. * sequential ones.
  284. *
  285. * There is a special-case: if the first page which the application tries to
  286. * read happens to be the first page of the file, it is assumed that a linear
  287. * read is about to happen and the window is immediately set to the initial size
  288. * based on I/O request size and the max_readahead.
  289. *
  290. * The code ramps up the readahead size aggressively at first, but slow down as
  291. * it approaches max_readhead.
  292. */
  293. /*
  294. * A minimal readahead algorithm for trivial sequential/random reads.
  295. */
  296. static unsigned long
  297. ondemand_readahead(struct address_space *mapping,
  298. struct file_ra_state *ra, struct file *filp,
  299. bool hit_readahead_marker, pgoff_t offset,
  300. unsigned long req_size)
  301. {
  302. int max = ra->ra_pages; /* max readahead pages */
  303. pgoff_t prev_offset;
  304. int sequential;
  305. /*
  306. * It's the expected callback offset, assume sequential access.
  307. * Ramp up sizes, and push forward the readahead window.
  308. */
  309. if (offset && (offset == (ra->start + ra->size - ra->async_size) ||
  310. offset == (ra->start + ra->size))) {
  311. ra->start += ra->size;
  312. ra->size = get_next_ra_size(ra, max);
  313. ra->async_size = ra->size;
  314. goto readit;
  315. }
  316. prev_offset = ra->prev_pos >> PAGE_CACHE_SHIFT;
  317. sequential = offset - prev_offset <= 1UL || req_size > max;
  318. /*
  319. * Standalone, small read.
  320. * Read as is, and do not pollute the readahead state.
  321. */
  322. if (!hit_readahead_marker && !sequential) {
  323. return __do_page_cache_readahead(mapping, filp,
  324. offset, req_size, 0);
  325. }
  326. /*
  327. * Hit a marked page without valid readahead state.
  328. * E.g. interleaved reads.
  329. * Query the pagecache for async_size, which normally equals to
  330. * readahead size. Ramp it up and use it as the new readahead size.
  331. */
  332. if (hit_readahead_marker) {
  333. pgoff_t start;
  334. read_lock_irq(&mapping->tree_lock);
  335. start = radix_tree_next_hole(&mapping->page_tree, offset, max+1);
  336. read_unlock_irq(&mapping->tree_lock);
  337. if (!start || start - offset > max)
  338. return 0;
  339. ra->start = start;
  340. ra->size = start - offset; /* old async_size */
  341. ra->size = get_next_ra_size(ra, max);
  342. ra->async_size = ra->size;
  343. goto readit;
  344. }
  345. /*
  346. * It may be one of
  347. * - first read on start of file
  348. * - sequential cache miss
  349. * - oversize random read
  350. * Start readahead for it.
  351. */
  352. ra->start = offset;
  353. ra->size = get_init_ra_size(req_size, max);
  354. ra->async_size = ra->size > req_size ? ra->size - req_size : ra->size;
  355. readit:
  356. return ra_submit(ra, mapping, filp);
  357. }
  358. /**
  359. * page_cache_sync_readahead - generic file readahead
  360. * @mapping: address_space which holds the pagecache and I/O vectors
  361. * @ra: file_ra_state which holds the readahead state
  362. * @filp: passed on to ->readpage() and ->readpages()
  363. * @offset: start offset into @mapping, in pagecache page-sized units
  364. * @req_size: hint: total size of the read which the caller is performing in
  365. * pagecache pages
  366. *
  367. * page_cache_sync_readahead() should be called when a cache miss happened:
  368. * it will submit the read. The readahead logic may decide to piggyback more
  369. * pages onto the read request if access patterns suggest it will improve
  370. * performance.
  371. */
  372. void page_cache_sync_readahead(struct address_space *mapping,
  373. struct file_ra_state *ra, struct file *filp,
  374. pgoff_t offset, unsigned long req_size)
  375. {
  376. /* no read-ahead */
  377. if (!ra->ra_pages)
  378. return;
  379. /* do read-ahead */
  380. ondemand_readahead(mapping, ra, filp, false, offset, req_size);
  381. }
  382. EXPORT_SYMBOL_GPL(page_cache_sync_readahead);
  383. /**
  384. * page_cache_async_readahead - file readahead for marked pages
  385. * @mapping: address_space which holds the pagecache and I/O vectors
  386. * @ra: file_ra_state which holds the readahead state
  387. * @filp: passed on to ->readpage() and ->readpages()
  388. * @page: the page at @offset which has the PG_readahead flag set
  389. * @offset: start offset into @mapping, in pagecache page-sized units
  390. * @req_size: hint: total size of the read which the caller is performing in
  391. * pagecache pages
  392. *
  393. * page_cache_async_ondemand() should be called when a page is used which
  394. * has the PG_readahead flag; this is a marker to suggest that the application
  395. * has used up enough of the readahead window that we should start pulling in
  396. * more pages.
  397. */
  398. void
  399. page_cache_async_readahead(struct address_space *mapping,
  400. struct file_ra_state *ra, struct file *filp,
  401. struct page *page, pgoff_t offset,
  402. unsigned long req_size)
  403. {
  404. /* no read-ahead */
  405. if (!ra->ra_pages)
  406. return;
  407. /*
  408. * Same bit is used for PG_readahead and PG_reclaim.
  409. */
  410. if (PageWriteback(page))
  411. return;
  412. ClearPageReadahead(page);
  413. /*
  414. * Defer asynchronous read-ahead on IO congestion.
  415. */
  416. if (bdi_read_congested(mapping->backing_dev_info))
  417. return;
  418. /* do read-ahead */
  419. ondemand_readahead(mapping, ra, filp, true, offset, req_size);
  420. }
  421. EXPORT_SYMBOL_GPL(page_cache_async_readahead);