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