readahead.c 14 KB

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