readahead.c 13 KB

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