readahead.c 17 KB

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