readahead.c 13 KB

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