readahead.c 13 KB

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