readahead.c 16 KB

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