page_io.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. cond_resched();
  151. first_block = bmap(inode, probe_block);
  152. if (first_block == 0)
  153. goto bad_bmap;
  154. /*
  155. * It must be PAGE_SIZE aligned on-disk
  156. */
  157. if (first_block & (blocks_per_page - 1)) {
  158. probe_block++;
  159. goto reprobe;
  160. }
  161. for (block_in_page = 1; block_in_page < blocks_per_page;
  162. block_in_page++) {
  163. sector_t block;
  164. block = bmap(inode, probe_block + block_in_page);
  165. if (block == 0)
  166. goto bad_bmap;
  167. if (block != first_block + block_in_page) {
  168. /* Discontiguity */
  169. probe_block++;
  170. goto reprobe;
  171. }
  172. }
  173. first_block >>= (PAGE_SHIFT - blkbits);
  174. if (page_no) { /* exclude the header page */
  175. if (first_block < lowest_block)
  176. lowest_block = first_block;
  177. if (first_block > highest_block)
  178. highest_block = first_block;
  179. }
  180. /*
  181. * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
  182. */
  183. ret = add_swap_extent(sis, page_no, 1, first_block);
  184. if (ret < 0)
  185. goto out;
  186. nr_extents += ret;
  187. page_no++;
  188. probe_block += blocks_per_page;
  189. reprobe:
  190. continue;
  191. }
  192. ret = nr_extents;
  193. *span = 1 + highest_block - lowest_block;
  194. if (page_no == 0)
  195. page_no = 1; /* force Empty message */
  196. sis->max = page_no;
  197. sis->pages = page_no - 1;
  198. sis->highest_bit = page_no - 1;
  199. out:
  200. return ret;
  201. bad_bmap:
  202. pr_err("swapon: swapfile has holes\n");
  203. ret = -EINVAL;
  204. goto out;
  205. }
  206. /*
  207. * We may have stale swap cache pages in memory: notice
  208. * them here and get rid of the unnecessary final write.
  209. */
  210. int swap_writepage(struct page *page, struct writeback_control *wbc)
  211. {
  212. int ret = 0;
  213. if (try_to_free_swap(page)) {
  214. unlock_page(page);
  215. goto out;
  216. }
  217. if (frontswap_store(page) == 0) {
  218. set_page_writeback(page);
  219. unlock_page(page);
  220. end_page_writeback(page);
  221. goto out;
  222. }
  223. ret = __swap_writepage(page, wbc, end_swap_bio_write);
  224. out:
  225. return ret;
  226. }
  227. static sector_t swap_page_sector(struct page *page)
  228. {
  229. return (sector_t)__page_file_index(page) << (PAGE_SHIFT - 9);
  230. }
  231. int __swap_writepage(struct page *page, struct writeback_control *wbc,
  232. bio_end_io_t end_write_func)
  233. {
  234. struct bio *bio;
  235. int ret;
  236. struct swap_info_struct *sis = page_swap_info(page);
  237. VM_BUG_ON_PAGE(!PageSwapCache(page), page);
  238. if (sis->flags & SWP_FILE) {
  239. struct kiocb kiocb;
  240. struct file *swap_file = sis->swap_file;
  241. struct address_space *mapping = swap_file->f_mapping;
  242. struct bio_vec bv = {
  243. .bv_page = page,
  244. .bv_len = PAGE_SIZE,
  245. .bv_offset = 0
  246. };
  247. struct iov_iter from;
  248. iov_iter_bvec(&from, ITER_BVEC | WRITE, &bv, 1, PAGE_SIZE);
  249. init_sync_kiocb(&kiocb, swap_file);
  250. kiocb.ki_pos = page_file_offset(page);
  251. set_page_writeback(page);
  252. unlock_page(page);
  253. ret = mapping->a_ops->direct_IO(&kiocb, &from);
  254. if (ret == PAGE_SIZE) {
  255. count_vm_event(PSWPOUT);
  256. ret = 0;
  257. } else {
  258. /*
  259. * In the case of swap-over-nfs, this can be a
  260. * temporary failure if the system has limited
  261. * memory for allocating transmit buffers.
  262. * Mark the page dirty and avoid
  263. * rotate_reclaimable_page but rate-limit the
  264. * messages but do not flag PageError like
  265. * the normal direct-to-bio case as it could
  266. * be temporary.
  267. */
  268. set_page_dirty(page);
  269. ClearPageReclaim(page);
  270. pr_err_ratelimited("Write error on dio swapfile (%llu)\n",
  271. page_file_offset(page));
  272. }
  273. end_page_writeback(page);
  274. return ret;
  275. }
  276. ret = bdev_write_page(sis->bdev, swap_page_sector(page), page, wbc);
  277. if (!ret) {
  278. count_vm_event(PSWPOUT);
  279. return 0;
  280. }
  281. ret = 0;
  282. bio = get_swap_bio(GFP_NOIO, page, end_write_func);
  283. if (bio == NULL) {
  284. set_page_dirty(page);
  285. unlock_page(page);
  286. ret = -ENOMEM;
  287. goto out;
  288. }
  289. if (wbc->sync_mode == WB_SYNC_ALL)
  290. bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC);
  291. else
  292. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  293. count_vm_event(PSWPOUT);
  294. set_page_writeback(page);
  295. unlock_page(page);
  296. submit_bio(bio);
  297. out:
  298. return ret;
  299. }
  300. int swap_readpage(struct page *page)
  301. {
  302. struct bio *bio;
  303. int ret = 0;
  304. struct swap_info_struct *sis = page_swap_info(page);
  305. VM_BUG_ON_PAGE(!PageSwapCache(page), page);
  306. VM_BUG_ON_PAGE(!PageLocked(page), page);
  307. VM_BUG_ON_PAGE(PageUptodate(page), page);
  308. if (frontswap_load(page) == 0) {
  309. SetPageUptodate(page);
  310. unlock_page(page);
  311. goto out;
  312. }
  313. if (sis->flags & SWP_FILE) {
  314. struct file *swap_file = sis->swap_file;
  315. struct address_space *mapping = swap_file->f_mapping;
  316. ret = mapping->a_ops->readpage(swap_file, page);
  317. if (!ret)
  318. count_vm_event(PSWPIN);
  319. return ret;
  320. }
  321. ret = bdev_read_page(sis->bdev, swap_page_sector(page), page);
  322. if (!ret) {
  323. if (trylock_page(page)) {
  324. swap_slot_free_notify(page);
  325. unlock_page(page);
  326. }
  327. count_vm_event(PSWPIN);
  328. return 0;
  329. }
  330. ret = 0;
  331. bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
  332. if (bio == NULL) {
  333. unlock_page(page);
  334. ret = -ENOMEM;
  335. goto out;
  336. }
  337. bio_set_op_attrs(bio, REQ_OP_READ, 0);
  338. count_vm_event(PSWPIN);
  339. submit_bio(bio);
  340. out:
  341. return ret;
  342. }
  343. int swap_set_page_dirty(struct page *page)
  344. {
  345. struct swap_info_struct *sis = page_swap_info(page);
  346. if (sis->flags & SWP_FILE) {
  347. struct address_space *mapping = sis->swap_file->f_mapping;
  348. VM_BUG_ON_PAGE(!PageSwapCache(page), page);
  349. return mapping->a_ops->set_page_dirty(page);
  350. } else {
  351. return __set_page_dirty_no_writeback(page);
  352. }
  353. }