Back to home page

LXR

 
 

    


0001 /*
0002  *  linux/mm/page_io.c
0003  *
0004  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
0005  *
0006  *  Swap reorganised 29.12.95, 
0007  *  Asynchronous swapping added 30.12.95. Stephen Tweedie
0008  *  Removed race in async swapping. 14.4.1996. Bruno Haible
0009  *  Add swap of shared pages through the page cache. 20.2.1998. Stephen Tweedie
0010  *  Always use brw_page, life becomes simpler. 12 May 1998 Eric Biederman
0011  */
0012 
0013 #include <linux/mm.h>
0014 #include <linux/kernel_stat.h>
0015 #include <linux/gfp.h>
0016 #include <linux/pagemap.h>
0017 #include <linux/swap.h>
0018 #include <linux/bio.h>
0019 #include <linux/swapops.h>
0020 #include <linux/buffer_head.h>
0021 #include <linux/writeback.h>
0022 #include <linux/frontswap.h>
0023 #include <linux/blkdev.h>
0024 #include <linux/uio.h>
0025 #include <asm/pgtable.h>
0026 
0027 static struct bio *get_swap_bio(gfp_t gfp_flags,
0028                 struct page *page, bio_end_io_t end_io)
0029 {
0030     struct bio *bio;
0031 
0032     bio = bio_alloc(gfp_flags, 1);
0033     if (bio) {
0034         bio->bi_iter.bi_sector = map_swap_page(page, &bio->bi_bdev);
0035         bio->bi_iter.bi_sector <<= PAGE_SHIFT - 9;
0036         bio->bi_end_io = end_io;
0037 
0038         bio_add_page(bio, page, PAGE_SIZE, 0);
0039         BUG_ON(bio->bi_iter.bi_size != PAGE_SIZE);
0040     }
0041     return bio;
0042 }
0043 
0044 void end_swap_bio_write(struct bio *bio)
0045 {
0046     struct page *page = bio->bi_io_vec[0].bv_page;
0047 
0048     if (bio->bi_error) {
0049         SetPageError(page);
0050         /*
0051          * We failed to write the page out to swap-space.
0052          * Re-dirty the page in order to avoid it being reclaimed.
0053          * Also print a dire warning that things will go BAD (tm)
0054          * very quickly.
0055          *
0056          * Also clear PG_reclaim to avoid rotate_reclaimable_page()
0057          */
0058         set_page_dirty(page);
0059         pr_alert("Write-error on swap-device (%u:%u:%llu)\n",
0060              imajor(bio->bi_bdev->bd_inode),
0061              iminor(bio->bi_bdev->bd_inode),
0062              (unsigned long long)bio->bi_iter.bi_sector);
0063         ClearPageReclaim(page);
0064     }
0065     end_page_writeback(page);
0066     bio_put(bio);
0067 }
0068 
0069 static void swap_slot_free_notify(struct page *page)
0070 {
0071     struct swap_info_struct *sis;
0072     struct gendisk *disk;
0073 
0074     /*
0075      * There is no guarantee that the page is in swap cache - the software
0076      * suspend code (at least) uses end_swap_bio_read() against a non-
0077      * swapcache page.  So we must check PG_swapcache before proceeding with
0078      * this optimization.
0079      */
0080     if (unlikely(!PageSwapCache(page)))
0081         return;
0082 
0083     sis = page_swap_info(page);
0084     if (!(sis->flags & SWP_BLKDEV))
0085         return;
0086 
0087     /*
0088      * The swap subsystem performs lazy swap slot freeing,
0089      * expecting that the page will be swapped out again.
0090      * So we can avoid an unnecessary write if the page
0091      * isn't redirtied.
0092      * This is good for real swap storage because we can
0093      * reduce unnecessary I/O and enhance wear-leveling
0094      * if an SSD is used as the as swap device.
0095      * But if in-memory swap device (eg zram) is used,
0096      * this causes a duplicated copy between uncompressed
0097      * data in VM-owned memory and compressed data in
0098      * zram-owned memory.  So let's free zram-owned memory
0099      * and make the VM-owned decompressed page *dirty*,
0100      * so the page should be swapped out somewhere again if
0101      * we again wish to reclaim it.
0102      */
0103     disk = sis->bdev->bd_disk;
0104     if (disk->fops->swap_slot_free_notify) {
0105         swp_entry_t entry;
0106         unsigned long offset;
0107 
0108         entry.val = page_private(page);
0109         offset = swp_offset(entry);
0110 
0111         SetPageDirty(page);
0112         disk->fops->swap_slot_free_notify(sis->bdev,
0113                 offset);
0114     }
0115 }
0116 
0117 static void end_swap_bio_read(struct bio *bio)
0118 {
0119     struct page *page = bio->bi_io_vec[0].bv_page;
0120 
0121     if (bio->bi_error) {
0122         SetPageError(page);
0123         ClearPageUptodate(page);
0124         pr_alert("Read-error on swap-device (%u:%u:%llu)\n",
0125              imajor(bio->bi_bdev->bd_inode),
0126              iminor(bio->bi_bdev->bd_inode),
0127              (unsigned long long)bio->bi_iter.bi_sector);
0128         goto out;
0129     }
0130 
0131     SetPageUptodate(page);
0132     swap_slot_free_notify(page);
0133 out:
0134     unlock_page(page);
0135     bio_put(bio);
0136 }
0137 
0138 int generic_swapfile_activate(struct swap_info_struct *sis,
0139                 struct file *swap_file,
0140                 sector_t *span)
0141 {
0142     struct address_space *mapping = swap_file->f_mapping;
0143     struct inode *inode = mapping->host;
0144     unsigned blocks_per_page;
0145     unsigned long page_no;
0146     unsigned blkbits;
0147     sector_t probe_block;
0148     sector_t last_block;
0149     sector_t lowest_block = -1;
0150     sector_t highest_block = 0;
0151     int nr_extents = 0;
0152     int ret;
0153 
0154     blkbits = inode->i_blkbits;
0155     blocks_per_page = PAGE_SIZE >> blkbits;
0156 
0157     /*
0158      * Map all the blocks into the extent list.  This code doesn't try
0159      * to be very smart.
0160      */
0161     probe_block = 0;
0162     page_no = 0;
0163     last_block = i_size_read(inode) >> blkbits;
0164     while ((probe_block + blocks_per_page) <= last_block &&
0165             page_no < sis->max) {
0166         unsigned block_in_page;
0167         sector_t first_block;
0168 
0169         cond_resched();
0170 
0171         first_block = bmap(inode, probe_block);
0172         if (first_block == 0)
0173             goto bad_bmap;
0174 
0175         /*
0176          * It must be PAGE_SIZE aligned on-disk
0177          */
0178         if (first_block & (blocks_per_page - 1)) {
0179             probe_block++;
0180             goto reprobe;
0181         }
0182 
0183         for (block_in_page = 1; block_in_page < blocks_per_page;
0184                     block_in_page++) {
0185             sector_t block;
0186 
0187             block = bmap(inode, probe_block + block_in_page);
0188             if (block == 0)
0189                 goto bad_bmap;
0190             if (block != first_block + block_in_page) {
0191                 /* Discontiguity */
0192                 probe_block++;
0193                 goto reprobe;
0194             }
0195         }
0196 
0197         first_block >>= (PAGE_SHIFT - blkbits);
0198         if (page_no) {  /* exclude the header page */
0199             if (first_block < lowest_block)
0200                 lowest_block = first_block;
0201             if (first_block > highest_block)
0202                 highest_block = first_block;
0203         }
0204 
0205         /*
0206          * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
0207          */
0208         ret = add_swap_extent(sis, page_no, 1, first_block);
0209         if (ret < 0)
0210             goto out;
0211         nr_extents += ret;
0212         page_no++;
0213         probe_block += blocks_per_page;
0214 reprobe:
0215         continue;
0216     }
0217     ret = nr_extents;
0218     *span = 1 + highest_block - lowest_block;
0219     if (page_no == 0)
0220         page_no = 1;    /* force Empty message */
0221     sis->max = page_no;
0222     sis->pages = page_no - 1;
0223     sis->highest_bit = page_no - 1;
0224 out:
0225     return ret;
0226 bad_bmap:
0227     pr_err("swapon: swapfile has holes\n");
0228     ret = -EINVAL;
0229     goto out;
0230 }
0231 
0232 /*
0233  * We may have stale swap cache pages in memory: notice
0234  * them here and get rid of the unnecessary final write.
0235  */
0236 int swap_writepage(struct page *page, struct writeback_control *wbc)
0237 {
0238     int ret = 0;
0239 
0240     if (try_to_free_swap(page)) {
0241         unlock_page(page);
0242         goto out;
0243     }
0244     if (frontswap_store(page) == 0) {
0245         set_page_writeback(page);
0246         unlock_page(page);
0247         end_page_writeback(page);
0248         goto out;
0249     }
0250     ret = __swap_writepage(page, wbc, end_swap_bio_write);
0251 out:
0252     return ret;
0253 }
0254 
0255 static sector_t swap_page_sector(struct page *page)
0256 {
0257     return (sector_t)__page_file_index(page) << (PAGE_SHIFT - 9);
0258 }
0259 
0260 int __swap_writepage(struct page *page, struct writeback_control *wbc,
0261         bio_end_io_t end_write_func)
0262 {
0263     struct bio *bio;
0264     int ret;
0265     struct swap_info_struct *sis = page_swap_info(page);
0266 
0267     VM_BUG_ON_PAGE(!PageSwapCache(page), page);
0268     if (sis->flags & SWP_FILE) {
0269         struct kiocb kiocb;
0270         struct file *swap_file = sis->swap_file;
0271         struct address_space *mapping = swap_file->f_mapping;
0272         struct bio_vec bv = {
0273             .bv_page = page,
0274             .bv_len  = PAGE_SIZE,
0275             .bv_offset = 0
0276         };
0277         struct iov_iter from;
0278 
0279         iov_iter_bvec(&from, ITER_BVEC | WRITE, &bv, 1, PAGE_SIZE);
0280         init_sync_kiocb(&kiocb, swap_file);
0281         kiocb.ki_pos = page_file_offset(page);
0282 
0283         set_page_writeback(page);
0284         unlock_page(page);
0285         ret = mapping->a_ops->direct_IO(&kiocb, &from);
0286         if (ret == PAGE_SIZE) {
0287             count_vm_event(PSWPOUT);
0288             ret = 0;
0289         } else {
0290             /*
0291              * In the case of swap-over-nfs, this can be a
0292              * temporary failure if the system has limited
0293              * memory for allocating transmit buffers.
0294              * Mark the page dirty and avoid
0295              * rotate_reclaimable_page but rate-limit the
0296              * messages but do not flag PageError like
0297              * the normal direct-to-bio case as it could
0298              * be temporary.
0299              */
0300             set_page_dirty(page);
0301             ClearPageReclaim(page);
0302             pr_err_ratelimited("Write error on dio swapfile (%llu)\n",
0303                        page_file_offset(page));
0304         }
0305         end_page_writeback(page);
0306         return ret;
0307     }
0308 
0309     ret = bdev_write_page(sis->bdev, swap_page_sector(page), page, wbc);
0310     if (!ret) {
0311         count_vm_event(PSWPOUT);
0312         return 0;
0313     }
0314 
0315     ret = 0;
0316     bio = get_swap_bio(GFP_NOIO, page, end_write_func);
0317     if (bio == NULL) {
0318         set_page_dirty(page);
0319         unlock_page(page);
0320         ret = -ENOMEM;
0321         goto out;
0322     }
0323     bio->bi_opf = REQ_OP_WRITE | wbc_to_write_flags(wbc);
0324     count_vm_event(PSWPOUT);
0325     set_page_writeback(page);
0326     unlock_page(page);
0327     submit_bio(bio);
0328 out:
0329     return ret;
0330 }
0331 
0332 int swap_readpage(struct page *page)
0333 {
0334     struct bio *bio;
0335     int ret = 0;
0336     struct swap_info_struct *sis = page_swap_info(page);
0337 
0338     VM_BUG_ON_PAGE(!PageSwapCache(page), page);
0339     VM_BUG_ON_PAGE(!PageLocked(page), page);
0340     VM_BUG_ON_PAGE(PageUptodate(page), page);
0341     if (frontswap_load(page) == 0) {
0342         SetPageUptodate(page);
0343         unlock_page(page);
0344         goto out;
0345     }
0346 
0347     if (sis->flags & SWP_FILE) {
0348         struct file *swap_file = sis->swap_file;
0349         struct address_space *mapping = swap_file->f_mapping;
0350 
0351         ret = mapping->a_ops->readpage(swap_file, page);
0352         if (!ret)
0353             count_vm_event(PSWPIN);
0354         return ret;
0355     }
0356 
0357     ret = bdev_read_page(sis->bdev, swap_page_sector(page), page);
0358     if (!ret) {
0359         if (trylock_page(page)) {
0360             swap_slot_free_notify(page);
0361             unlock_page(page);
0362         }
0363 
0364         count_vm_event(PSWPIN);
0365         return 0;
0366     }
0367 
0368     ret = 0;
0369     bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
0370     if (bio == NULL) {
0371         unlock_page(page);
0372         ret = -ENOMEM;
0373         goto out;
0374     }
0375     bio_set_op_attrs(bio, REQ_OP_READ, 0);
0376     count_vm_event(PSWPIN);
0377     submit_bio(bio);
0378 out:
0379     return ret;
0380 }
0381 
0382 int swap_set_page_dirty(struct page *page)
0383 {
0384     struct swap_info_struct *sis = page_swap_info(page);
0385 
0386     if (sis->flags & SWP_FILE) {
0387         struct address_space *mapping = sis->swap_file->f_mapping;
0388 
0389         VM_BUG_ON_PAGE(!PageSwapCache(page), page);
0390         return mapping->a_ops->set_page_dirty(page);
0391     } else {
0392         return __set_page_dirty_no_writeback(page);
0393     }
0394 }