readahead.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. /*
  47. * Return max readahead size for this inode in number-of-pages.
  48. */
  49. static inline unsigned long get_max_readahead(struct file_ra_state *ra)
  50. {
  51. return ra->ra_pages;
  52. }
  53. static inline unsigned long get_min_readahead(struct file_ra_state *ra)
  54. {
  55. return MIN_RA_PAGES;
  56. }
  57. static inline void reset_ahead_window(struct file_ra_state *ra)
  58. {
  59. /*
  60. * ... but preserve ahead_start + ahead_size value,
  61. * see 'recheck:' label in page_cache_readahead().
  62. * Note: We never use ->ahead_size as rvalue without
  63. * checking ->ahead_start != 0 first.
  64. */
  65. ra->ahead_size += ra->ahead_start;
  66. ra->ahead_start = 0;
  67. }
  68. static inline void ra_off(struct file_ra_state *ra)
  69. {
  70. ra->start = 0;
  71. ra->flags = 0;
  72. ra->size = 0;
  73. reset_ahead_window(ra);
  74. return;
  75. }
  76. /*
  77. * Set the initial window size, round to next power of 2 and square
  78. * for small size, x 4 for medium, and x 2 for large
  79. * for 128k (32 page) max ra
  80. * 1-8 page = 32k initial, > 8 page = 128k initial
  81. */
  82. static unsigned long get_init_ra_size(unsigned long size, unsigned long max)
  83. {
  84. unsigned long newsize = roundup_pow_of_two(size);
  85. if (newsize <= max / 32)
  86. newsize = newsize * 4;
  87. else if (newsize <= max / 4)
  88. newsize = newsize * 2;
  89. else
  90. newsize = max;
  91. return newsize;
  92. }
  93. /*
  94. * Set the new window size, this is called only when I/O is to be submitted,
  95. * not for each call to readahead. If a cache miss occured, reduce next I/O
  96. * size, else increase depending on how close to max we are.
  97. */
  98. static inline unsigned long get_next_ra_size(struct file_ra_state *ra)
  99. {
  100. unsigned long max = get_max_readahead(ra);
  101. unsigned long min = get_min_readahead(ra);
  102. unsigned long cur = ra->size;
  103. unsigned long newsize;
  104. if (ra->flags & RA_FLAG_MISS) {
  105. ra->flags &= ~RA_FLAG_MISS;
  106. newsize = max((cur - 2), min);
  107. } else if (cur < max / 16) {
  108. newsize = 4 * cur;
  109. } else {
  110. newsize = 2 * cur;
  111. }
  112. return min(newsize, max);
  113. }
  114. #define list_to_page(head) (list_entry((head)->prev, struct page, lru))
  115. /**
  116. * read_cache_pages - populate an address space with some pages & start reads against them
  117. * @mapping: the address_space
  118. * @pages: The address of a list_head which contains the target pages. These
  119. * pages have their ->index populated and are otherwise uninitialised.
  120. * @filler: callback routine for filling a single page.
  121. * @data: private data for the callback routine.
  122. *
  123. * Hides the details of the LRU cache etc from the filesystems.
  124. */
  125. int read_cache_pages(struct address_space *mapping, struct list_head *pages,
  126. int (*filler)(void *, struct page *), void *data)
  127. {
  128. struct page *page;
  129. struct pagevec lru_pvec;
  130. int ret = 0;
  131. pagevec_init(&lru_pvec, 0);
  132. while (!list_empty(pages)) {
  133. page = list_to_page(pages);
  134. list_del(&page->lru);
  135. if (add_to_page_cache(page, mapping, page->index, GFP_KERNEL)) {
  136. page_cache_release(page);
  137. continue;
  138. }
  139. ret = filler(data, page);
  140. if (!pagevec_add(&lru_pvec, page))
  141. __pagevec_lru_add(&lru_pvec);
  142. if (ret) {
  143. put_pages_list(pages);
  144. break;
  145. }
  146. task_io_account_read(PAGE_CACHE_SIZE);
  147. }
  148. pagevec_lru_add(&lru_pvec);
  149. return ret;
  150. }
  151. EXPORT_SYMBOL(read_cache_pages);
  152. static int read_pages(struct address_space *mapping, struct file *filp,
  153. struct list_head *pages, unsigned nr_pages)
  154. {
  155. unsigned page_idx;
  156. struct pagevec lru_pvec;
  157. int ret;
  158. if (mapping->a_ops->readpages) {
  159. ret = mapping->a_ops->readpages(filp, mapping, pages, nr_pages);
  160. /* Clean up the remaining pages */
  161. put_pages_list(pages);
  162. goto out;
  163. }
  164. pagevec_init(&lru_pvec, 0);
  165. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  166. struct page *page = list_to_page(pages);
  167. list_del(&page->lru);
  168. if (!add_to_page_cache(page, mapping,
  169. page->index, GFP_KERNEL)) {
  170. mapping->a_ops->readpage(filp, page);
  171. if (!pagevec_add(&lru_pvec, page))
  172. __pagevec_lru_add(&lru_pvec);
  173. } else
  174. page_cache_release(page);
  175. }
  176. pagevec_lru_add(&lru_pvec);
  177. ret = 0;
  178. out:
  179. return ret;
  180. }
  181. /*
  182. * Readahead design.
  183. *
  184. * The fields in struct file_ra_state represent the most-recently-executed
  185. * readahead attempt:
  186. *
  187. * start: Page index at which we started the readahead
  188. * size: Number of pages in that read
  189. * Together, these form the "current window".
  190. * Together, start and size represent the `readahead window'.
  191. * prev_index: The page which the readahead algorithm most-recently inspected.
  192. * It is mainly used to detect sequential file reading.
  193. * If page_cache_readahead sees that it is again being called for
  194. * a page which it just looked at, it can return immediately without
  195. * making any state changes.
  196. * offset: Offset in the prev_index where the last read ended - used for
  197. * detection of sequential file reading.
  198. * ahead_start,
  199. * ahead_size: Together, these form the "ahead window".
  200. * ra_pages: The externally controlled max readahead for this fd.
  201. *
  202. * When readahead is in the off state (size == 0), readahead is disabled.
  203. * In this state, prev_index is used to detect the resumption of sequential I/O.
  204. *
  205. * The readahead code manages two windows - the "current" and the "ahead"
  206. * windows. The intent is that while the application is walking the pages
  207. * in the current window, I/O is underway on the ahead window. When the
  208. * current window is fully traversed, it is replaced by the ahead window
  209. * and the ahead window is invalidated. When this copying happens, the
  210. * new current window's pages are probably still locked. So
  211. * we submit a new batch of I/O immediately, creating a new ahead window.
  212. *
  213. * So:
  214. *
  215. * ----|----------------|----------------|-----
  216. * ^start ^start+size
  217. * ^ahead_start ^ahead_start+ahead_size
  218. *
  219. * ^ When this page is read, we submit I/O for the
  220. * ahead window.
  221. *
  222. * A `readahead hit' occurs when a read request is made against a page which is
  223. * the next sequential page. Ahead window calculations are done only when it
  224. * is time to submit a new IO. The code ramps up the size agressively at first,
  225. * but slow down as it approaches max_readhead.
  226. *
  227. * Any seek/ramdom IO will result in readahead being turned off. It will resume
  228. * at the first sequential access.
  229. *
  230. * There is a special-case: if the first page which the application tries to
  231. * read happens to be the first page of the file, it is assumed that a linear
  232. * read is about to happen and the window is immediately set to the initial size
  233. * based on I/O request size and the max_readahead.
  234. *
  235. * This function is to be called for every read request, rather than when
  236. * it is time to perform readahead. It is called only once for the entire I/O
  237. * regardless of size unless readahead is unable to start enough I/O to satisfy
  238. * the request (I/O request > max_readahead).
  239. */
  240. /*
  241. * do_page_cache_readahead actually reads a chunk of disk. It allocates all
  242. * the pages first, then submits them all for I/O. This avoids the very bad
  243. * behaviour which would occur if page allocations are causing VM writeback.
  244. * We really don't want to intermingle reads and writes like that.
  245. *
  246. * Returns the number of pages requested, or the maximum amount of I/O allowed.
  247. *
  248. * do_page_cache_readahead() returns -1 if it encountered request queue
  249. * congestion.
  250. */
  251. static int
  252. __do_page_cache_readahead(struct address_space *mapping, struct file *filp,
  253. pgoff_t offset, unsigned long nr_to_read,
  254. unsigned long lookahead_size)
  255. {
  256. struct inode *inode = mapping->host;
  257. struct page *page;
  258. unsigned long end_index; /* The last page we want to read */
  259. LIST_HEAD(page_pool);
  260. int page_idx;
  261. int ret = 0;
  262. loff_t isize = i_size_read(inode);
  263. if (isize == 0)
  264. goto out;
  265. end_index = ((isize - 1) >> PAGE_CACHE_SHIFT);
  266. /*
  267. * Preallocate as many pages as we will need.
  268. */
  269. read_lock_irq(&mapping->tree_lock);
  270. for (page_idx = 0; page_idx < nr_to_read; page_idx++) {
  271. pgoff_t page_offset = offset + page_idx;
  272. if (page_offset > end_index)
  273. break;
  274. page = radix_tree_lookup(&mapping->page_tree, page_offset);
  275. if (page)
  276. continue;
  277. read_unlock_irq(&mapping->tree_lock);
  278. page = page_cache_alloc_cold(mapping);
  279. read_lock_irq(&mapping->tree_lock);
  280. if (!page)
  281. break;
  282. page->index = page_offset;
  283. list_add(&page->lru, &page_pool);
  284. if (page_idx == nr_to_read - lookahead_size)
  285. SetPageReadahead(page);
  286. ret++;
  287. }
  288. read_unlock_irq(&mapping->tree_lock);
  289. /*
  290. * Now start the IO. We ignore I/O errors - if the page is not
  291. * uptodate then the caller will launch readpage again, and
  292. * will then handle the error.
  293. */
  294. if (ret)
  295. read_pages(mapping, filp, &page_pool, ret);
  296. BUG_ON(!list_empty(&page_pool));
  297. out:
  298. return ret;
  299. }
  300. /*
  301. * Chunk the readahead into 2 megabyte units, so that we don't pin too much
  302. * memory at once.
  303. */
  304. int force_page_cache_readahead(struct address_space *mapping, struct file *filp,
  305. pgoff_t offset, unsigned long nr_to_read)
  306. {
  307. int ret = 0;
  308. if (unlikely(!mapping->a_ops->readpage && !mapping->a_ops->readpages))
  309. return -EINVAL;
  310. while (nr_to_read) {
  311. int err;
  312. unsigned long this_chunk = (2 * 1024 * 1024) / PAGE_CACHE_SIZE;
  313. if (this_chunk > nr_to_read)
  314. this_chunk = nr_to_read;
  315. err = __do_page_cache_readahead(mapping, filp,
  316. offset, this_chunk, 0);
  317. if (err < 0) {
  318. ret = err;
  319. break;
  320. }
  321. ret += err;
  322. offset += this_chunk;
  323. nr_to_read -= this_chunk;
  324. }
  325. return ret;
  326. }
  327. /*
  328. * Check how effective readahead is being. If the amount of started IO is
  329. * less than expected then the file is partly or fully in pagecache and
  330. * readahead isn't helping.
  331. *
  332. */
  333. static inline int check_ra_success(struct file_ra_state *ra,
  334. unsigned long nr_to_read, unsigned long actual)
  335. {
  336. if (actual == 0) {
  337. ra->cache_hit += nr_to_read;
  338. if (ra->cache_hit >= VM_MAX_CACHE_HIT) {
  339. ra_off(ra);
  340. ra->flags |= RA_FLAG_INCACHE;
  341. return 0;
  342. }
  343. } else {
  344. ra->cache_hit=0;
  345. }
  346. return 1;
  347. }
  348. /*
  349. * This version skips the IO if the queue is read-congested, and will tell the
  350. * block layer to abandon the readahead if request allocation would block.
  351. *
  352. * force_page_cache_readahead() will ignore queue congestion and will block on
  353. * request queues.
  354. */
  355. int do_page_cache_readahead(struct address_space *mapping, struct file *filp,
  356. pgoff_t offset, unsigned long nr_to_read)
  357. {
  358. if (bdi_read_congested(mapping->backing_dev_info))
  359. return -1;
  360. return __do_page_cache_readahead(mapping, filp, offset, nr_to_read, 0);
  361. }
  362. /*
  363. * Read 'nr_to_read' pages starting at page 'offset'. If the flag 'block'
  364. * is set wait till the read completes. Otherwise attempt to read without
  365. * blocking.
  366. * Returns 1 meaning 'success' if read is successful without switching off
  367. * readahead mode. Otherwise return failure.
  368. */
  369. static int
  370. blockable_page_cache_readahead(struct address_space *mapping, struct file *filp,
  371. pgoff_t offset, unsigned long nr_to_read,
  372. struct file_ra_state *ra, int block)
  373. {
  374. int actual;
  375. if (!block && bdi_read_congested(mapping->backing_dev_info))
  376. return 0;
  377. actual = __do_page_cache_readahead(mapping, filp, offset, nr_to_read, 0);
  378. return check_ra_success(ra, nr_to_read, actual);
  379. }
  380. static int make_ahead_window(struct address_space *mapping, struct file *filp,
  381. struct file_ra_state *ra, int force)
  382. {
  383. int block, ret;
  384. ra->ahead_size = get_next_ra_size(ra);
  385. ra->ahead_start = ra->start + ra->size;
  386. block = force || (ra->prev_index >= ra->ahead_start);
  387. ret = blockable_page_cache_readahead(mapping, filp,
  388. ra->ahead_start, ra->ahead_size, ra, block);
  389. if (!ret && !force) {
  390. /* A read failure in blocking mode, implies pages are
  391. * all cached. So we can safely assume we have taken
  392. * care of all the pages requested in this call.
  393. * A read failure in non-blocking mode, implies we are
  394. * reading more pages than requested in this call. So
  395. * we safely assume we have taken care of all the pages
  396. * requested in this call.
  397. *
  398. * Just reset the ahead window in case we failed due to
  399. * congestion. The ahead window will any way be closed
  400. * in case we failed due to excessive page cache hits.
  401. */
  402. reset_ahead_window(ra);
  403. }
  404. return ret;
  405. }
  406. /**
  407. * page_cache_readahead - generic adaptive readahead
  408. * @mapping: address_space which holds the pagecache and I/O vectors
  409. * @ra: file_ra_state which holds the readahead state
  410. * @filp: passed on to ->readpage() and ->readpages()
  411. * @offset: start offset into @mapping, in PAGE_CACHE_SIZE units
  412. * @req_size: hint: total size of the read which the caller is performing in
  413. * PAGE_CACHE_SIZE units
  414. *
  415. * page_cache_readahead() is the main function. It performs the adaptive
  416. * readahead window size management and submits the readahead I/O.
  417. *
  418. * Note that @filp is purely used for passing on to the ->readpage[s]()
  419. * handler: it may refer to a different file from @mapping (so we may not use
  420. * @filp->f_mapping or @filp->f_path.dentry->d_inode here).
  421. * Also, @ra may not be equal to &@filp->f_ra.
  422. *
  423. */
  424. unsigned long
  425. page_cache_readahead(struct address_space *mapping, struct file_ra_state *ra,
  426. struct file *filp, pgoff_t offset, unsigned long req_size)
  427. {
  428. unsigned long max, newsize;
  429. int sequential;
  430. /*
  431. * We avoid doing extra work and bogusly perturbing the readahead
  432. * window expansion logic.
  433. */
  434. if (offset == ra->prev_index && --req_size)
  435. ++offset;
  436. /* Note that prev_index == -1 if it is a first read */
  437. sequential = (offset == ra->prev_index + 1);
  438. ra->prev_index = offset;
  439. ra->prev_offset = 0;
  440. max = get_max_readahead(ra);
  441. newsize = min(req_size, max);
  442. /* No readahead or sub-page sized read or file already in cache */
  443. if (newsize == 0 || (ra->flags & RA_FLAG_INCACHE))
  444. goto out;
  445. ra->prev_index += newsize - 1;
  446. /*
  447. * Special case - first read at start of file. We'll assume it's
  448. * a whole-file read and grow the window fast. Or detect first
  449. * sequential access
  450. */
  451. if (sequential && ra->size == 0) {
  452. ra->size = get_init_ra_size(newsize, max);
  453. ra->start = offset;
  454. if (!blockable_page_cache_readahead(mapping, filp, offset,
  455. ra->size, ra, 1))
  456. goto out;
  457. /*
  458. * If the request size is larger than our max readahead, we
  459. * at least want to be sure that we get 2 IOs in flight and
  460. * we know that we will definitly need the new I/O.
  461. * once we do this, subsequent calls should be able to overlap
  462. * IOs,* thus preventing stalls. so issue the ahead window
  463. * immediately.
  464. */
  465. if (req_size >= max)
  466. make_ahead_window(mapping, filp, ra, 1);
  467. goto out;
  468. }
  469. /*
  470. * Now handle the random case:
  471. * partial page reads and first access were handled above,
  472. * so this must be the next page otherwise it is random
  473. */
  474. if (!sequential) {
  475. ra_off(ra);
  476. blockable_page_cache_readahead(mapping, filp, offset,
  477. newsize, ra, 1);
  478. goto out;
  479. }
  480. /*
  481. * If we get here we are doing sequential IO and this was not the first
  482. * occurence (ie we have an existing window)
  483. */
  484. if (ra->ahead_start == 0) { /* no ahead window yet */
  485. if (!make_ahead_window(mapping, filp, ra, 0))
  486. goto recheck;
  487. }
  488. /*
  489. * Already have an ahead window, check if we crossed into it.
  490. * If so, shift windows and issue a new ahead window.
  491. * Only return the #pages that are in the current window, so that
  492. * we get called back on the first page of the ahead window which
  493. * will allow us to submit more IO.
  494. */
  495. if (ra->prev_index >= ra->ahead_start) {
  496. ra->start = ra->ahead_start;
  497. ra->size = ra->ahead_size;
  498. make_ahead_window(mapping, filp, ra, 0);
  499. recheck:
  500. /* prev_index shouldn't overrun the ahead window */
  501. ra->prev_index = min(ra->prev_index,
  502. ra->ahead_start + ra->ahead_size - 1);
  503. }
  504. out:
  505. return ra->prev_index + 1;
  506. }
  507. EXPORT_SYMBOL_GPL(page_cache_readahead);
  508. /*
  509. * handle_ra_miss() is called when it is known that a page which should have
  510. * been present in the pagecache (we just did some readahead there) was in fact
  511. * not found. This will happen if it was evicted by the VM (readahead
  512. * thrashing)
  513. *
  514. * Turn on the cache miss flag in the RA struct, this will cause the RA code
  515. * to reduce the RA size on the next read.
  516. */
  517. void handle_ra_miss(struct address_space *mapping,
  518. struct file_ra_state *ra, pgoff_t offset)
  519. {
  520. ra->flags |= RA_FLAG_MISS;
  521. ra->flags &= ~RA_FLAG_INCACHE;
  522. ra->cache_hit = 0;
  523. }
  524. /*
  525. * Given a desired number of PAGE_CACHE_SIZE readahead pages, return a
  526. * sensible upper limit.
  527. */
  528. unsigned long max_sane_readahead(unsigned long nr)
  529. {
  530. return min(nr, (node_page_state(numa_node_id(), NR_INACTIVE)
  531. + node_page_state(numa_node_id(), NR_FREE_PAGES)) / 2);
  532. }