readahead.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 = 0;
  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. mapping->a_ops->readpage(filp, page);
  155. if (!pagevec_add(&lru_pvec, page))
  156. __pagevec_lru_add(&lru_pvec);
  157. } else {
  158. page_cache_release(page);
  159. }
  160. }
  161. pagevec_lru_add(&lru_pvec);
  162. out:
  163. return ret;
  164. }
  165. /*
  166. * Readahead design.
  167. *
  168. * The fields in struct file_ra_state represent the most-recently-executed
  169. * readahead attempt:
  170. *
  171. * start: Page index at which we started the readahead
  172. * size: Number of pages in that read
  173. * Together, these form the "current window".
  174. * Together, start and size represent the `readahead window'.
  175. * prev_page: The page which the readahead algorithm most-recently inspected.
  176. * It is mainly used to detect sequential file reading.
  177. * If page_cache_readahead sees that it is again being called for
  178. * a page which it just looked at, it can return immediately without
  179. * making any state changes.
  180. * ahead_start,
  181. * ahead_size: Together, these form the "ahead window".
  182. * ra_pages: The externally controlled max readahead for this fd.
  183. *
  184. * When readahead is in the off state (size == 0), readahead is disabled.
  185. * In this state, prev_page is used to detect the resumption of sequential I/O.
  186. *
  187. * The readahead code manages two windows - the "current" and the "ahead"
  188. * windows. The intent is that while the application is walking the pages
  189. * in the current window, I/O is underway on the ahead window. When the
  190. * current window is fully traversed, it is replaced by the ahead window
  191. * and the ahead window is invalidated. When this copying happens, the
  192. * new current window's pages are probably still locked. So
  193. * we submit a new batch of I/O immediately, creating a new ahead window.
  194. *
  195. * So:
  196. *
  197. * ----|----------------|----------------|-----
  198. * ^start ^start+size
  199. * ^ahead_start ^ahead_start+ahead_size
  200. *
  201. * ^ When this page is read, we submit I/O for the
  202. * ahead window.
  203. *
  204. * A `readahead hit' occurs when a read request is made against a page which is
  205. * the next sequential page. Ahead window calculations are done only when it
  206. * is time to submit a new IO. The code ramps up the size agressively at first,
  207. * but slow down as it approaches max_readhead.
  208. *
  209. * Any seek/ramdom IO will result in readahead being turned off. It will resume
  210. * at the first sequential access.
  211. *
  212. * There is a special-case: if the first page which the application tries to
  213. * read happens to be the first page of the file, it is assumed that a linear
  214. * read is about to happen and the window is immediately set to the initial size
  215. * based on I/O request size and the max_readahead.
  216. *
  217. * This function is to be called for every read request, rather than when
  218. * it is time to perform readahead. It is called only once for the entire I/O
  219. * regardless of size unless readahead is unable to start enough I/O to satisfy
  220. * the request (I/O request > max_readahead).
  221. */
  222. /*
  223. * do_page_cache_readahead actually reads a chunk of disk. It allocates all
  224. * the pages first, then submits them all for I/O. This avoids the very bad
  225. * behaviour which would occur if page allocations are causing VM writeback.
  226. * We really don't want to intermingle reads and writes like that.
  227. *
  228. * Returns the number of pages requested, or the maximum amount of I/O allowed.
  229. *
  230. * do_page_cache_readahead() returns -1 if it encountered request queue
  231. * congestion.
  232. */
  233. static int
  234. __do_page_cache_readahead(struct address_space *mapping, struct file *filp,
  235. unsigned long offset, unsigned long nr_to_read)
  236. {
  237. struct inode *inode = mapping->host;
  238. struct page *page;
  239. unsigned long end_index; /* The last page we want to read */
  240. LIST_HEAD(page_pool);
  241. int page_idx;
  242. int ret = 0;
  243. loff_t isize = i_size_read(inode);
  244. if (isize == 0)
  245. goto out;
  246. end_index = ((isize - 1) >> PAGE_CACHE_SHIFT);
  247. /*
  248. * Preallocate as many pages as we will need.
  249. */
  250. read_lock_irq(&mapping->tree_lock);
  251. for (page_idx = 0; page_idx < nr_to_read; page_idx++) {
  252. unsigned long page_offset = offset + page_idx;
  253. if (page_offset > end_index)
  254. break;
  255. page = radix_tree_lookup(&mapping->page_tree, page_offset);
  256. if (page)
  257. continue;
  258. read_unlock_irq(&mapping->tree_lock);
  259. page = page_cache_alloc_cold(mapping);
  260. read_lock_irq(&mapping->tree_lock);
  261. if (!page)
  262. break;
  263. page->index = page_offset;
  264. list_add(&page->lru, &page_pool);
  265. ret++;
  266. }
  267. read_unlock_irq(&mapping->tree_lock);
  268. /*
  269. * Now start the IO. We ignore I/O errors - if the page is not
  270. * uptodate then the caller will launch readpage again, and
  271. * will then handle the error.
  272. */
  273. if (ret)
  274. read_pages(mapping, filp, &page_pool, ret);
  275. BUG_ON(!list_empty(&page_pool));
  276. out:
  277. return ret;
  278. }
  279. /*
  280. * Chunk the readahead into 2 megabyte units, so that we don't pin too much
  281. * memory at once.
  282. */
  283. int force_page_cache_readahead(struct address_space *mapping, struct file *filp,
  284. unsigned long offset, unsigned long nr_to_read)
  285. {
  286. int ret = 0;
  287. if (unlikely(!mapping->a_ops->readpage && !mapping->a_ops->readpages))
  288. return -EINVAL;
  289. while (nr_to_read) {
  290. int err;
  291. unsigned long this_chunk = (2 * 1024 * 1024) / PAGE_CACHE_SIZE;
  292. if (this_chunk > nr_to_read)
  293. this_chunk = nr_to_read;
  294. err = __do_page_cache_readahead(mapping, filp,
  295. offset, this_chunk);
  296. if (err < 0) {
  297. ret = err;
  298. break;
  299. }
  300. ret += err;
  301. offset += this_chunk;
  302. nr_to_read -= this_chunk;
  303. }
  304. return ret;
  305. }
  306. /*
  307. * Check how effective readahead is being. If the amount of started IO is
  308. * less than expected then the file is partly or fully in pagecache and
  309. * readahead isn't helping.
  310. *
  311. */
  312. static inline int check_ra_success(struct file_ra_state *ra,
  313. unsigned long nr_to_read, unsigned long actual)
  314. {
  315. if (actual == 0) {
  316. ra->cache_hit += nr_to_read;
  317. if (ra->cache_hit >= VM_MAX_CACHE_HIT) {
  318. ra_off(ra);
  319. ra->flags |= RA_FLAG_INCACHE;
  320. return 0;
  321. }
  322. } else {
  323. ra->cache_hit=0;
  324. }
  325. return 1;
  326. }
  327. /*
  328. * This version skips the IO if the queue is read-congested, and will tell the
  329. * block layer to abandon the readahead if request allocation would block.
  330. *
  331. * force_page_cache_readahead() will ignore queue congestion and will block on
  332. * request queues.
  333. */
  334. int do_page_cache_readahead(struct address_space *mapping, struct file *filp,
  335. unsigned long offset, unsigned long nr_to_read)
  336. {
  337. if (bdi_read_congested(mapping->backing_dev_info))
  338. return -1;
  339. return __do_page_cache_readahead(mapping, filp, offset, nr_to_read);
  340. }
  341. /*
  342. * Read 'nr_to_read' pages starting at page 'offset'. If the flag 'block'
  343. * is set wait till the read completes. Otherwise attempt to read without
  344. * blocking.
  345. * Returns 1 meaning 'success' if read is succesfull without switching off
  346. * readhaead mode. Otherwise return failure.
  347. */
  348. static int
  349. blockable_page_cache_readahead(struct address_space *mapping, struct file *filp,
  350. unsigned long offset, unsigned long nr_to_read,
  351. struct file_ra_state *ra, int block)
  352. {
  353. int actual;
  354. if (!block && bdi_read_congested(mapping->backing_dev_info))
  355. return 0;
  356. actual = __do_page_cache_readahead(mapping, filp, offset, nr_to_read);
  357. return check_ra_success(ra, nr_to_read, actual);
  358. }
  359. static int make_ahead_window(struct address_space *mapping, struct file *filp,
  360. struct file_ra_state *ra, int force)
  361. {
  362. int block, ret;
  363. ra->ahead_size = get_next_ra_size(ra);
  364. ra->ahead_start = ra->start + ra->size;
  365. block = force || (ra->prev_page >= ra->ahead_start);
  366. ret = blockable_page_cache_readahead(mapping, filp,
  367. ra->ahead_start, ra->ahead_size, ra, block);
  368. if (!ret && !force) {
  369. /* A read failure in blocking mode, implies pages are
  370. * all cached. So we can safely assume we have taken
  371. * care of all the pages requested in this call.
  372. * A read failure in non-blocking mode, implies we are
  373. * reading more pages than requested in this call. So
  374. * we safely assume we have taken care of all the pages
  375. * requested in this call.
  376. *
  377. * Just reset the ahead window in case we failed due to
  378. * congestion. The ahead window will any way be closed
  379. * in case we failed due to excessive page cache hits.
  380. */
  381. ra->ahead_start = 0;
  382. ra->ahead_size = 0;
  383. }
  384. return ret;
  385. }
  386. /*
  387. * page_cache_readahead is the main function. If performs the adaptive
  388. * readahead window size management and submits the readahead I/O.
  389. */
  390. unsigned long
  391. page_cache_readahead(struct address_space *mapping, struct file_ra_state *ra,
  392. struct file *filp, unsigned long offset,
  393. unsigned long req_size)
  394. {
  395. unsigned long max, newsize;
  396. int sequential;
  397. /*
  398. * We avoid doing extra work and bogusly perturbing the readahead
  399. * window expansion logic.
  400. */
  401. if (offset == ra->prev_page && --req_size)
  402. ++offset;
  403. /* Note that prev_page == -1 if it is a first read */
  404. sequential = (offset == ra->prev_page + 1);
  405. ra->prev_page = offset;
  406. max = get_max_readahead(ra);
  407. newsize = min(req_size, max);
  408. /* No readahead or sub-page sized read or file already in cache */
  409. if (newsize == 0 || (ra->flags & RA_FLAG_INCACHE))
  410. goto out;
  411. ra->prev_page += newsize - 1;
  412. /*
  413. * Special case - first read at start of file. We'll assume it's
  414. * a whole-file read and grow the window fast. Or detect first
  415. * sequential access
  416. */
  417. if (sequential && ra->size == 0) {
  418. ra->size = get_init_ra_size(newsize, max);
  419. ra->start = offset;
  420. if (!blockable_page_cache_readahead(mapping, filp, offset,
  421. ra->size, ra, 1))
  422. goto out;
  423. /*
  424. * If the request size is larger than our max readahead, we
  425. * at least want to be sure that we get 2 IOs in flight and
  426. * we know that we will definitly need the new I/O.
  427. * once we do this, subsequent calls should be able to overlap
  428. * IOs,* thus preventing stalls. so issue the ahead window
  429. * immediately.
  430. */
  431. if (req_size >= max)
  432. make_ahead_window(mapping, filp, ra, 1);
  433. goto out;
  434. }
  435. /*
  436. * Now handle the random case:
  437. * partial page reads and first access were handled above,
  438. * so this must be the next page otherwise it is random
  439. */
  440. if (!sequential) {
  441. ra_off(ra);
  442. blockable_page_cache_readahead(mapping, filp, offset,
  443. newsize, ra, 1);
  444. goto out;
  445. }
  446. /*
  447. * If we get here we are doing sequential IO and this was not the first
  448. * occurence (ie we have an existing window)
  449. */
  450. if (ra->ahead_start == 0) { /* no ahead window yet */
  451. if (!make_ahead_window(mapping, filp, ra, 0))
  452. goto out;
  453. }
  454. /*
  455. * Already have an ahead window, check if we crossed into it.
  456. * If so, shift windows and issue a new ahead window.
  457. * Only return the #pages that are in the current window, so that
  458. * we get called back on the first page of the ahead window which
  459. * will allow us to submit more IO.
  460. */
  461. if (ra->prev_page >= ra->ahead_start) {
  462. ra->start = ra->ahead_start;
  463. ra->size = ra->ahead_size;
  464. make_ahead_window(mapping, filp, ra, 0);
  465. }
  466. out:
  467. return ra->prev_page + 1;
  468. }
  469. /*
  470. * handle_ra_miss() is called when it is known that a page which should have
  471. * been present in the pagecache (we just did some readahead there) was in fact
  472. * not found. This will happen if it was evicted by the VM (readahead
  473. * thrashing)
  474. *
  475. * Turn on the cache miss flag in the RA struct, this will cause the RA code
  476. * to reduce the RA size on the next read.
  477. */
  478. void handle_ra_miss(struct address_space *mapping,
  479. struct file_ra_state *ra, pgoff_t offset)
  480. {
  481. ra->flags |= RA_FLAG_MISS;
  482. ra->flags &= ~RA_FLAG_INCACHE;
  483. }
  484. /*
  485. * Given a desired number of PAGE_CACHE_SIZE readahead pages, return a
  486. * sensible upper limit.
  487. */
  488. unsigned long max_sane_readahead(unsigned long nr)
  489. {
  490. unsigned long active;
  491. unsigned long inactive;
  492. unsigned long free;
  493. __get_zone_counts(&active, &inactive, &free, NODE_DATA(numa_node_id()));
  494. return min(nr, (inactive + free) / 2);
  495. }