readahead.c 15 KB

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