page_io.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. pr_alert("Write-error on swap-device (%u:%u:%llu)\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 swap_slot_free_notify(struct page *page)
  63. {
  64. struct swap_info_struct *sis;
  65. struct gendisk *disk;
  66. /*
  67. * There is no guarantee that the page is in swap cache - the software
  68. * suspend code (at least) uses end_swap_bio_read() against a non-
  69. * swapcache page. So we must check PG_swapcache before proceeding with
  70. * this optimization.
  71. */
  72. if (unlikely(!PageSwapCache(page)))
  73. return;
  74. sis = page_swap_info(page);
  75. if (!(sis->flags & SWP_BLKDEV))
  76. return;
  77. /*
  78. * The swap subsystem performs lazy swap slot freeing,
  79. * expecting that the page will be swapped out again.
  80. * So we can avoid an unnecessary write if the page
  81. * isn't redirtied.
  82. * This is good for real swap storage because we can
  83. * reduce unnecessary I/O and enhance wear-leveling
  84. * if an SSD is used as the as swap device.
  85. * But if in-memory swap device (eg zram) is used,
  86. * this causes a duplicated copy between uncompressed
  87. * data in VM-owned memory and compressed data in
  88. * zram-owned memory. So let's free zram-owned memory
  89. * and make the VM-owned decompressed page *dirty*,
  90. * so the page should be swapped out somewhere again if
  91. * we again wish to reclaim it.
  92. */
  93. disk = sis->bdev->bd_disk;
  94. if (disk->fops->swap_slot_free_notify) {
  95. swp_entry_t entry;
  96. unsigned long offset;
  97. entry.val = page_private(page);
  98. offset = swp_offset(entry);
  99. SetPageDirty(page);
  100. disk->fops->swap_slot_free_notify(sis->bdev,
  101. offset);
  102. }
  103. }
  104. static void end_swap_bio_read(struct bio *bio)
  105. {
  106. struct page *page = bio->bi_io_vec[0].bv_page;
  107. if (bio->bi_error) {
  108. SetPageError(page);
  109. ClearPageUptodate(page);
  110. pr_alert("Read-error on swap-device (%u:%u:%llu)\n",
  111. imajor(bio->bi_bdev->bd_inode),
  112. iminor(bio->bi_bdev->bd_inode),
  113. (unsigned long long)bio->bi_iter.bi_sector);
  114. goto out;
  115. }
  116. SetPageUptodate(page);
  117. swap_slot_free_notify(page);
  118. out:
  119. unlock_page(page);
  120. bio_put(bio);
  121. }
  122. int generic_swapfile_activate(struct swap_info_struct *sis,
  123. struct file *swap_file,
  124. sector_t *span)
  125. {
  126. struct address_space *mapping = swap_file->f_mapping;
  127. struct inode *inode = mapping->host;
  128. unsigned blocks_per_page;
  129. unsigned long page_no;
  130. unsigned blkbits;
  131. sector_t probe_block;
  132. sector_t last_block;
  133. sector_t lowest_block = -1;
  134. sector_t highest_block = 0;
  135. int nr_extents = 0;
  136. int ret;
  137. blkbits = inode->i_blkbits;
  138. blocks_per_page = PAGE_SIZE >> blkbits;
  139. /*
  140. * Map all the blocks into the extent list. This code doesn't try
  141. * to be very smart.
  142. */
  143. probe_block = 0;
  144. page_no = 0;
  145. last_block = i_size_read(inode) >> blkbits;
  146. while ((probe_block + blocks_per_page) <= last_block &&
  147. page_no < sis->max) {
  148. unsigned block_in_page;
  149. sector_t first_block;
  150. first_block = bmap(inode, probe_block);
  151. if (first_block == 0)
  152. goto bad_bmap;
  153. /*
  154. * It must be PAGE_SIZE aligned on-disk
  155. */
  156. if (first_block & (blocks_per_page - 1)) {
  157. probe_block++;
  158. goto reprobe;
  159. }
  160. for (block_in_page = 1; block_in_page < blocks_per_page;
  161. block_in_page++) {
  162. sector_t block;
  163. block = bmap(inode, probe_block + block_in_page);
  164. if (block == 0)
  165. goto bad_bmap;
  166. if (block != first_block + block_in_page) {
  167. /* Discontiguity */
  168. probe_block++;
  169. goto reprobe;
  170. }
  171. }
  172. first_block >>= (PAGE_SHIFT - blkbits);
  173. if (page_no) { /* exclude the header page */
  174. if (first_block < lowest_block)
  175. lowest_block = first_block;
  176. if (first_block > highest_block)
  177. highest_block = first_block;
  178. }
  179. /*
  180. * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
  181. */
  182. ret = add_swap_extent(sis, page_no, 1, first_block);
  183. if (ret < 0)
  184. goto out;
  185. nr_extents += ret;
  186. page_no++;
  187. probe_block += blocks_per_page;
  188. reprobe:
  189. continue;
  190. }
  191. ret = nr_extents;
  192. *span = 1 + highest_block - lowest_block;
  193. if (page_no == 0)
  194. page_no = 1; /* force Empty message */
  195. sis->max = page_no;
  196. sis->pages = page_no - 1;
  197. sis->highest_bit = page_no - 1;
  198. out:
  199. return ret;
  200. bad_bmap:
  201. pr_err("swapon: swapfile has holes\n");
  202. ret = -EINVAL;
  203. goto out;
  204. }
  205. /*
  206. * We may have stale swap cache pages in memory: notice
  207. * them here and get rid of the unnecessary final write.
  208. */
  209. int swap_writepage(struct page *page, struct writeback_control *wbc)
  210. {
  211. int ret = 0;
  212. if (try_to_free_swap(page)) {
  213. unlock_page(page);
  214. goto out;
  215. }
  216. if (frontswap_store(page) == 0) {
  217. set_page_writeback(page);
  218. unlock_page(page);
  219. end_page_writeback(page);
  220. goto out;
  221. }
  222. ret = __swap_writepage(page, wbc, end_swap_bio_write);
  223. out:
  224. return ret;
  225. }
  226. static sector_t swap_page_sector(struct page *page)
  227. {
  228. return (sector_t)__page_file_index(page) << (PAGE_SHIFT - 9);
  229. }
  230. int __swap_writepage(struct page *page, struct writeback_control *wbc,
  231. bio_end_io_t end_write_func)
  232. {
  233. struct bio *bio;
  234. int ret, rw = WRITE;
  235. struct swap_info_struct *sis = page_swap_info(page);
  236. if (sis->flags & SWP_FILE) {
  237. struct kiocb kiocb;
  238. struct file *swap_file = sis->swap_file;
  239. struct address_space *mapping = swap_file->f_mapping;
  240. struct bio_vec bv = {
  241. .bv_page = page,
  242. .bv_len = PAGE_SIZE,
  243. .bv_offset = 0
  244. };
  245. struct iov_iter from;
  246. iov_iter_bvec(&from, ITER_BVEC | WRITE, &bv, 1, PAGE_SIZE);
  247. init_sync_kiocb(&kiocb, swap_file);
  248. kiocb.ki_pos = page_file_offset(page);
  249. set_page_writeback(page);
  250. unlock_page(page);
  251. ret = mapping->a_ops->direct_IO(&kiocb, &from);
  252. if (ret == PAGE_SIZE) {
  253. count_vm_event(PSWPOUT);
  254. ret = 0;
  255. } else {
  256. /*
  257. * In the case of swap-over-nfs, this can be a
  258. * temporary failure if the system has limited
  259. * memory for allocating transmit buffers.
  260. * Mark the page dirty and avoid
  261. * rotate_reclaimable_page but rate-limit the
  262. * messages but do not flag PageError like
  263. * the normal direct-to-bio case as it could
  264. * be temporary.
  265. */
  266. set_page_dirty(page);
  267. ClearPageReclaim(page);
  268. pr_err_ratelimited("Write error on dio swapfile (%llu)\n",
  269. page_file_offset(page));
  270. }
  271. end_page_writeback(page);
  272. return ret;
  273. }
  274. ret = bdev_write_page(sis->bdev, swap_page_sector(page), page, wbc);
  275. if (!ret) {
  276. count_vm_event(PSWPOUT);
  277. return 0;
  278. }
  279. ret = 0;
  280. bio = get_swap_bio(GFP_NOIO, page, end_write_func);
  281. if (bio == NULL) {
  282. set_page_dirty(page);
  283. unlock_page(page);
  284. ret = -ENOMEM;
  285. goto out;
  286. }
  287. if (wbc->sync_mode == WB_SYNC_ALL)
  288. rw |= REQ_SYNC;
  289. count_vm_event(PSWPOUT);
  290. set_page_writeback(page);
  291. unlock_page(page);
  292. submit_bio(rw, bio);
  293. out:
  294. return ret;
  295. }
  296. int swap_readpage(struct page *page)
  297. {
  298. struct bio *bio;
  299. int ret = 0;
  300. struct swap_info_struct *sis = page_swap_info(page);
  301. VM_BUG_ON_PAGE(!PageLocked(page), page);
  302. VM_BUG_ON_PAGE(PageUptodate(page), page);
  303. if (frontswap_load(page) == 0) {
  304. SetPageUptodate(page);
  305. unlock_page(page);
  306. goto out;
  307. }
  308. if (sis->flags & SWP_FILE) {
  309. struct file *swap_file = sis->swap_file;
  310. struct address_space *mapping = swap_file->f_mapping;
  311. ret = mapping->a_ops->readpage(swap_file, page);
  312. if (!ret)
  313. count_vm_event(PSWPIN);
  314. return ret;
  315. }
  316. ret = bdev_read_page(sis->bdev, swap_page_sector(page), page);
  317. if (!ret) {
  318. if (trylock_page(page)) {
  319. swap_slot_free_notify(page);
  320. unlock_page(page);
  321. }
  322. count_vm_event(PSWPIN);
  323. return 0;
  324. }
  325. ret = 0;
  326. bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
  327. if (bio == NULL) {
  328. unlock_page(page);
  329. ret = -ENOMEM;
  330. goto out;
  331. }
  332. count_vm_event(PSWPIN);
  333. submit_bio(READ, bio);
  334. out:
  335. return ret;
  336. }
  337. int swap_set_page_dirty(struct page *page)
  338. {
  339. struct swap_info_struct *sis = page_swap_info(page);
  340. if (sis->flags & SWP_FILE) {
  341. struct address_space *mapping = sis->swap_file->f_mapping;
  342. return mapping->a_ops->set_page_dirty(page);
  343. } else {
  344. return __set_page_dirty_no_writeback(page);
  345. }
  346. }