FROMLIST: extcon: Add the support for the capability of each property
[firefly-linux-kernel-4.4.55.git] / mm / page_io.c
index a8a3ef45fed753b68ac1cc4a94c9260979a37879..b995a5ba5e8f13f49c049e99eface364148d5660 100644 (file)
@@ -20,7 +20,8 @@
 #include <linux/buffer_head.h>
 #include <linux/writeback.h>
 #include <linux/frontswap.h>
-#include <linux/aio.h>
+#include <linux/blkdev.h>
+#include <linux/uio.h>
 #include <asm/pgtable.h>
 
 static struct bio *get_swap_bio(gfp_t gfp_flags,
@@ -30,24 +31,21 @@ static struct bio *get_swap_bio(gfp_t gfp_flags,
 
        bio = bio_alloc(gfp_flags, 1);
        if (bio) {
-               bio->bi_sector = map_swap_page(page, &bio->bi_bdev);
-               bio->bi_sector <<= PAGE_SHIFT - 9;
-               bio->bi_io_vec[0].bv_page = page;
-               bio->bi_io_vec[0].bv_len = PAGE_SIZE;
-               bio->bi_io_vec[0].bv_offset = 0;
-               bio->bi_vcnt = 1;
-               bio->bi_size = PAGE_SIZE;
+               bio->bi_iter.bi_sector = map_swap_page(page, &bio->bi_bdev);
+               bio->bi_iter.bi_sector <<= PAGE_SHIFT - 9;
                bio->bi_end_io = end_io;
+
+               bio_add_page(bio, page, PAGE_SIZE, 0);
+               BUG_ON(bio->bi_iter.bi_size != PAGE_SIZE);
        }
        return bio;
 }
 
-void end_swap_bio_write(struct bio *bio, int err)
+void end_swap_bio_write(struct bio *bio)
 {
-       const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
        struct page *page = bio->bi_io_vec[0].bv_page;
 
-       if (!uptodate) {
+       if (bio->bi_error) {
                SetPageError(page);
                /*
                 * We failed to write the page out to swap-space.
@@ -61,28 +59,72 @@ void end_swap_bio_write(struct bio *bio, int err)
                printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n",
                                imajor(bio->bi_bdev->bd_inode),
                                iminor(bio->bi_bdev->bd_inode),
-                               (unsigned long long)bio->bi_sector);
+                               (unsigned long long)bio->bi_iter.bi_sector);
                ClearPageReclaim(page);
        }
        end_page_writeback(page);
        bio_put(bio);
 }
 
-void end_swap_bio_read(struct bio *bio, int err)
+static void end_swap_bio_read(struct bio *bio)
 {
-       const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
        struct page *page = bio->bi_io_vec[0].bv_page;
 
-       if (!uptodate) {
+       if (bio->bi_error) {
                SetPageError(page);
                ClearPageUptodate(page);
                printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n",
                                imajor(bio->bi_bdev->bd_inode),
                                iminor(bio->bi_bdev->bd_inode),
-                               (unsigned long long)bio->bi_sector);
-       } else {
-               SetPageUptodate(page);
+                               (unsigned long long)bio->bi_iter.bi_sector);
+               goto out;
        }
+
+       SetPageUptodate(page);
+
+       /*
+        * There is no guarantee that the page is in swap cache - the software
+        * suspend code (at least) uses end_swap_bio_read() against a non-
+        * swapcache page.  So we must check PG_swapcache before proceeding with
+        * this optimization.
+        */
+       if (likely(PageSwapCache(page))) {
+               struct swap_info_struct *sis;
+
+               sis = page_swap_info(page);
+               if (sis->flags & SWP_BLKDEV) {
+                       /*
+                        * The swap subsystem performs lazy swap slot freeing,
+                        * expecting that the page will be swapped out again.
+                        * So we can avoid an unnecessary write if the page
+                        * isn't redirtied.
+                        * This is good for real swap storage because we can
+                        * reduce unnecessary I/O and enhance wear-leveling
+                        * if an SSD is used as the as swap device.
+                        * But if in-memory swap device (eg zram) is used,
+                        * this causes a duplicated copy between uncompressed
+                        * data in VM-owned memory and compressed data in
+                        * zram-owned memory.  So let's free zram-owned memory
+                        * and make the VM-owned decompressed page *dirty*,
+                        * so the page should be swapped out somewhere again if
+                        * we again wish to reclaim it.
+                        */
+                       struct gendisk *disk = sis->bdev->bd_disk;
+                       if (disk->fops->swap_slot_free_notify) {
+                               swp_entry_t entry;
+                               unsigned long offset;
+
+                               entry.val = page_private(page);
+                               offset = swp_offset(entry);
+
+                               SetPageDirty(page);
+                               disk->fops->swap_slot_free_notify(sis->bdev,
+                                               offset);
+                       }
+               }
+       }
+
+out:
        unlock_page(page);
        bio_put(bio);
 }
@@ -202,33 +244,36 @@ out:
        return ret;
 }
 
+static sector_t swap_page_sector(struct page *page)
+{
+       return (sector_t)__page_file_index(page) << (PAGE_CACHE_SHIFT - 9);
+}
+
 int __swap_writepage(struct page *page, struct writeback_control *wbc,
-       void (*end_write_func)(struct bio *, int))
+               bio_end_io_t end_write_func)
 {
        struct bio *bio;
-       int ret = 0, rw = WRITE;
+       int ret, rw = WRITE;
        struct swap_info_struct *sis = page_swap_info(page);
 
        if (sis->flags & SWP_FILE) {
                struct kiocb kiocb;
                struct file *swap_file = sis->swap_file;
                struct address_space *mapping = swap_file->f_mapping;
-               struct iovec iov = {
-                       .iov_base = kmap(page),
-                       .iov_len  = PAGE_SIZE,
+               struct bio_vec bv = {
+                       .bv_page = page,
+                       .bv_len  = PAGE_SIZE,
+                       .bv_offset = 0
                };
+               struct iov_iter from;
 
+               iov_iter_bvec(&from, ITER_BVEC | WRITE, &bv, 1, PAGE_SIZE);
                init_sync_kiocb(&kiocb, swap_file);
                kiocb.ki_pos = page_file_offset(page);
-               kiocb.ki_left = PAGE_SIZE;
-               kiocb.ki_nbytes = PAGE_SIZE;
 
                set_page_writeback(page);
                unlock_page(page);
-               ret = mapping->a_ops->direct_IO(KERNEL_WRITE,
-                                               &kiocb, &iov,
-                                               kiocb.ki_pos, 1);
-               kunmap(page);
+               ret = mapping->a_ops->direct_IO(&kiocb, &from, kiocb.ki_pos);
                if (ret == PAGE_SIZE) {
                        count_vm_event(PSWPOUT);
                        ret = 0;
@@ -252,6 +297,13 @@ int __swap_writepage(struct page *page, struct writeback_control *wbc,
                return ret;
        }
 
+       ret = bdev_write_page(sis->bdev, swap_page_sector(page), page, wbc);
+       if (!ret) {
+               count_vm_event(PSWPOUT);
+               return 0;
+       }
+
+       ret = 0;
        bio = get_swap_bio(GFP_NOIO, page, end_write_func);
        if (bio == NULL) {
                set_page_dirty(page);
@@ -275,8 +327,8 @@ int swap_readpage(struct page *page)
        int ret = 0;
        struct swap_info_struct *sis = page_swap_info(page);
 
-       VM_BUG_ON(!PageLocked(page));
-       VM_BUG_ON(PageUptodate(page));
+       VM_BUG_ON_PAGE(!PageLocked(page), page);
+       VM_BUG_ON_PAGE(PageUptodate(page), page);
        if (frontswap_load(page) == 0) {
                SetPageUptodate(page);
                unlock_page(page);
@@ -293,6 +345,13 @@ int swap_readpage(struct page *page)
                return ret;
        }
 
+       ret = bdev_read_page(sis->bdev, swap_page_sector(page), page);
+       if (!ret) {
+               count_vm_event(PSWPIN);
+               return 0;
+       }
+
+       ret = 0;
        bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
        if (bio == NULL) {
                unlock_page(page);