page_io.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * linux/mm/page_io.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. *
  6. * Swap reorganised 29.12.95,
  7. * Asynchronous swapping added 30.12.95. Stephen Tweedie
  8. * Removed race in async swapping. 14.4.1996. Bruno Haible
  9. * Add swap of shared pages through the page cache. 20.2.1998. Stephen Tweedie
  10. * Always use brw_page, life becomes simpler. 12 May 1998 Eric Biederman
  11. */
  12. #include <linux/mm.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/gfp.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/swap.h>
  17. #include <linux/bio.h>
  18. #include <linux/swapops.h>
  19. #include <linux/buffer_head.h>
  20. #include <linux/writeback.h>
  21. #include <linux/frontswap.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/uio.h>
  24. #include <asm/pgtable.h>
  25. static struct bio *get_swap_bio(gfp_t gfp_flags,
  26. struct page *page, bio_end_io_t end_io)
  27. {
  28. struct bio *bio;
  29. bio = bio_alloc(gfp_flags, 1);
  30. if (bio) {
  31. bio->bi_iter.bi_sector = map_swap_page(page, &bio->bi_bdev);
  32. bio->bi_iter.bi_sector <<= PAGE_SHIFT - 9;
  33. bio->bi_end_io = end_io;
  34. bio_add_page(bio, page, PAGE_SIZE, 0);
  35. BUG_ON(bio->bi_iter.bi_size != PAGE_SIZE);
  36. }
  37. return bio;
  38. }
  39. void end_swap_bio_write(struct bio *bio)
  40. {
  41. struct page *page = bio->bi_io_vec[0].bv_page;
  42. if (bio->bi_error) {
  43. SetPageError(page);
  44. /*
  45. * We failed to write the page out to swap-space.
  46. * Re-dirty the page in order to avoid it being reclaimed.
  47. * Also print a dire warning that things will go BAD (tm)
  48. * very quickly.
  49. *
  50. * Also clear PG_reclaim to avoid rotate_reclaimable_page()
  51. */
  52. set_page_dirty(page);
  53. printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n",
  54. imajor(bio->bi_bdev->bd_inode),
  55. iminor(bio->bi_bdev->bd_inode),
  56. (unsigned long long)bio->bi_iter.bi_sector);
  57. ClearPageReclaim(page);
  58. }
  59. end_page_writeback(page);
  60. bio_put(bio);
  61. }
  62. static void end_swap_bio_read(struct bio *bio)
  63. {
  64. struct page *page = bio->bi_io_vec[0].bv_page;
  65. if (bio->bi_error) {
  66. SetPageError(page);
  67. ClearPageUptodate(page);
  68. printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n",
  69. imajor(bio->bi_bdev->bd_inode),
  70. iminor(bio->bi_bdev->bd_inode),
  71. (unsigned long long)bio->bi_iter.bi_sector);
  72. goto out;
  73. }
  74. SetPageUptodate(page);
  75. /*
  76. * There is no guarantee that the page is in swap cache - the software
  77. * suspend code (at least) uses end_swap_bio_read() against a non-
  78. * swapcache page. So we must check PG_swapcache before proceeding with
  79. * this optimization.
  80. */
  81. if (likely(PageSwapCache(page))) {
  82. struct swap_info_struct *sis;
  83. sis = page_swap_info(page);
  84. if (sis->flags & SWP_BLKDEV) {
  85. /*
  86. * The swap subsystem performs lazy swap slot freeing,
  87. * expecting that the page will be swapped out again.
  88. * So we can avoid an unnecessary write if the page
  89. * isn't redirtied.
  90. * This is good for real swap storage because we can
  91. * reduce unnecessary I/O and enhance wear-leveling
  92. * if an SSD is used as the as swap device.
  93. * But if in-memory swap device (eg zram) is used,
  94. * this causes a duplicated copy between uncompressed
  95. * data in VM-owned memory and compressed data in
  96. * zram-owned memory. So let's free zram-owned memory
  97. * and make the VM-owned decompressed page *dirty*,
  98. * so the page should be swapped out somewhere again if
  99. * we again wish to reclaim it.
  100. */
  101. struct gendisk *disk = sis->bdev->bd_disk;
  102. if (disk->fops->swap_slot_free_notify) {
  103. swp_entry_t entry;
  104. unsigned long offset;
  105. entry.val = page_private(page);
  106. offset = swp_offset(entry);
  107. SetPageDirty(page);
  108. disk->fops->swap_slot_free_notify(sis->bdev,
  109. offset);
  110. }
  111. }
  112. }
  113. out:
  114. unlock_page(page);
  115. bio_put(bio);
  116. }
  117. int generic_swapfile_activate(struct swap_info_struct *sis,
  118. struct file *swap_file,
  119. sector_t *span)
  120. {
  121. struct address_space *mapping = swap_file->f_mapping;
  122. struct inode *inode = mapping->host;
  123. unsigned blocks_per_page;
  124. unsigned long page_no;
  125. unsigned blkbits;
  126. sector_t probe_block;
  127. sector_t last_block;
  128. sector_t lowest_block = -1;
  129. sector_t highest_block = 0;
  130. int nr_extents = 0;
  131. int ret;
  132. blkbits = inode->i_blkbits;
  133. blocks_per_page = PAGE_SIZE >> blkbits;
  134. /*
  135. * Map all the blocks into the extent list. This code doesn't try
  136. * to be very smart.
  137. */
  138. probe_block = 0;
  139. page_no = 0;
  140. last_block = i_size_read(inode) >> blkbits;
  141. while ((probe_block + blocks_per_page) <= last_block &&
  142. page_no < sis->max) {
  143. unsigned block_in_page;
  144. sector_t first_block;
  145. first_block = bmap(inode, probe_block);
  146. if (first_block == 0)
  147. goto bad_bmap;
  148. /*
  149. * It must be PAGE_SIZE aligned on-disk
  150. */
  151. if (first_block & (blocks_per_page - 1)) {
  152. probe_block++;
  153. goto reprobe;
  154. }
  155. for (block_in_page = 1; block_in_page < blocks_per_page;
  156. block_in_page++) {
  157. sector_t block;
  158. block = bmap(inode, probe_block + block_in_page);
  159. if (block == 0)
  160. goto bad_bmap;
  161. if (block != first_block + block_in_page) {
  162. /* Discontiguity */
  163. probe_block++;
  164. goto reprobe;
  165. }
  166. }
  167. first_block >>= (PAGE_SHIFT - blkbits);
  168. if (page_no) { /* exclude the header page */
  169. if (first_block < lowest_block)
  170. lowest_block = first_block;
  171. if (first_block > highest_block)
  172. highest_block = first_block;
  173. }
  174. /*
  175. * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
  176. */
  177. ret = add_swap_extent(sis, page_no, 1, first_block);
  178. if (ret < 0)
  179. goto out;
  180. nr_extents += ret;
  181. page_no++;
  182. probe_block += blocks_per_page;
  183. reprobe:
  184. continue;
  185. }
  186. ret = nr_extents;
  187. *span = 1 + highest_block - lowest_block;
  188. if (page_no == 0)
  189. page_no = 1; /* force Empty message */
  190. sis->max = page_no;
  191. sis->pages = page_no - 1;
  192. sis->highest_bit = page_no - 1;
  193. out:
  194. return ret;
  195. bad_bmap:
  196. printk(KERN_ERR "swapon: swapfile has holes\n");
  197. ret = -EINVAL;
  198. goto out;
  199. }
  200. /*
  201. * We may have stale swap cache pages in memory: notice
  202. * them here and get rid of the unnecessary final write.
  203. */
  204. int swap_writepage(struct page *page, struct writeback_control *wbc)
  205. {
  206. int ret = 0;
  207. if (try_to_free_swap(page)) {
  208. unlock_page(page);
  209. goto out;
  210. }
  211. if (frontswap_store(page) == 0) {
  212. set_page_writeback(page);
  213. unlock_page(page);
  214. end_page_writeback(page);
  215. goto out;
  216. }
  217. ret = __swap_writepage(page, wbc, end_swap_bio_write);
  218. out:
  219. return ret;
  220. }
  221. static sector_t swap_page_sector(struct page *page)
  222. {
  223. return (sector_t)__page_file_index(page) << (PAGE_CACHE_SHIFT - 9);
  224. }
  225. int __swap_writepage(struct page *page, struct writeback_control *wbc,
  226. bio_end_io_t end_write_func)
  227. {
  228. struct bio *bio;
  229. int ret, rw = WRITE;
  230. struct swap_info_struct *sis = page_swap_info(page);
  231. if (sis->flags & SWP_FILE) {
  232. struct kiocb kiocb;
  233. struct file *swap_file = sis->swap_file;
  234. struct address_space *mapping = swap_file->f_mapping;
  235. struct bio_vec bv = {
  236. .bv_page = page,
  237. .bv_len = PAGE_SIZE,
  238. .bv_offset = 0
  239. };
  240. struct iov_iter from;
  241. iov_iter_bvec(&from, ITER_BVEC | WRITE, &bv, 1, PAGE_SIZE);
  242. init_sync_kiocb(&kiocb, swap_file);
  243. kiocb.ki_pos = page_file_offset(page);
  244. set_page_writeback(page);
  245. unlock_page(page);
  246. ret = mapping->a_ops->direct_IO(&kiocb, &from, kiocb.ki_pos);
  247. if (ret == PAGE_SIZE) {
  248. count_vm_event(PSWPOUT);
  249. ret = 0;
  250. } else {
  251. /*
  252. * In the case of swap-over-nfs, this can be a
  253. * temporary failure if the system has limited
  254. * memory for allocating transmit buffers.
  255. * Mark the page dirty and avoid
  256. * rotate_reclaimable_page but rate-limit the
  257. * messages but do not flag PageError like
  258. * the normal direct-to-bio case as it could
  259. * be temporary.
  260. */
  261. set_page_dirty(page);
  262. ClearPageReclaim(page);
  263. pr_err_ratelimited("Write error on dio swapfile (%Lu)\n",
  264. page_file_offset(page));
  265. }
  266. end_page_writeback(page);
  267. return ret;
  268. }
  269. ret = bdev_write_page(sis->bdev, swap_page_sector(page), page, wbc);
  270. if (!ret) {
  271. count_vm_event(PSWPOUT);
  272. return 0;
  273. }
  274. ret = 0;
  275. bio = get_swap_bio(GFP_NOIO, page, end_write_func);
  276. if (bio == NULL) {
  277. set_page_dirty(page);
  278. unlock_page(page);
  279. ret = -ENOMEM;
  280. goto out;
  281. }
  282. if (wbc->sync_mode == WB_SYNC_ALL)
  283. rw |= REQ_SYNC;
  284. count_vm_event(PSWPOUT);
  285. set_page_writeback(page);
  286. unlock_page(page);
  287. submit_bio(rw, bio);
  288. out:
  289. return ret;
  290. }
  291. int swap_readpage(struct page *page)
  292. {
  293. struct bio *bio;
  294. int ret = 0;
  295. struct swap_info_struct *sis = page_swap_info(page);
  296. VM_BUG_ON_PAGE(!PageLocked(page), page);
  297. VM_BUG_ON_PAGE(PageUptodate(page), page);
  298. if (frontswap_load(page) == 0) {
  299. SetPageUptodate(page);
  300. unlock_page(page);
  301. goto out;
  302. }
  303. if (sis->flags & SWP_FILE) {
  304. struct file *swap_file = sis->swap_file;
  305. struct address_space *mapping = swap_file->f_mapping;
  306. ret = mapping->a_ops->readpage(swap_file, page);
  307. if (!ret)
  308. count_vm_event(PSWPIN);
  309. return ret;
  310. }
  311. ret = bdev_read_page(sis->bdev, swap_page_sector(page), page);
  312. if (!ret) {
  313. count_vm_event(PSWPIN);
  314. return 0;
  315. }
  316. ret = 0;
  317. bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
  318. if (bio == NULL) {
  319. unlock_page(page);
  320. ret = -ENOMEM;
  321. goto out;
  322. }
  323. count_vm_event(PSWPIN);
  324. submit_bio(READ, bio);
  325. out:
  326. return ret;
  327. }
  328. int swap_set_page_dirty(struct page *page)
  329. {
  330. struct swap_info_struct *sis = page_swap_info(page);
  331. if (sis->flags & SWP_FILE) {
  332. struct address_space *mapping = sis->swap_file->f_mapping;
  333. return mapping->a_ops->set_page_dirty(page);
  334. } else {
  335. return __set_page_dirty_no_writeback(page);
  336. }
  337. }