Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[firefly-linux-kernel-4.4.55.git] / fs / fs-writeback.c
index f43c479feee9b54efe001da166efb29d9de2d16e..04cf3b91e5016a1f7e3ceef734c7d85574ec296d 100644 (file)
@@ -35,6 +35,7 @@
 struct wb_writeback_work {
        long nr_pages;
        struct super_block *sb;
+       unsigned long *older_than_this;
        enum writeback_sync_modes sync_mode;
        unsigned int tagged_writepages:1;
        unsigned int for_kupdate:1;
@@ -248,15 +249,16 @@ static bool inode_dirtied_after(struct inode *inode, unsigned long t)
 /*
  * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
  */
-static void move_expired_inodes(struct list_head *delaying_queue,
+static int move_expired_inodes(struct list_head *delaying_queue,
                               struct list_head *dispatch_queue,
-                               unsigned long *older_than_this)
+                              unsigned long *older_than_this)
 {
        LIST_HEAD(tmp);
        struct list_head *pos, *node;
        struct super_block *sb = NULL;
        struct inode *inode;
        int do_sb_sort = 0;
+       int moved = 0;
 
        while (!list_empty(delaying_queue)) {
                inode = wb_inode(delaying_queue->prev);
@@ -267,12 +269,13 @@ static void move_expired_inodes(struct list_head *delaying_queue,
                        do_sb_sort = 1;
                sb = inode->i_sb;
                list_move(&inode->i_wb_list, &tmp);
+               moved++;
        }
 
        /* just one sb in list, splice to dispatch_queue and we're done */
        if (!do_sb_sort) {
                list_splice(&tmp, dispatch_queue);
-               return;
+               goto out;
        }
 
        /* Move inodes from one superblock together */
@@ -284,6 +287,8 @@ static void move_expired_inodes(struct list_head *delaying_queue,
                                list_move(&inode->i_wb_list, dispatch_queue);
                }
        }
+out:
+       return moved;
 }
 
 /*
@@ -299,9 +304,11 @@ static void move_expired_inodes(struct list_head *delaying_queue,
  */
 static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this)
 {
+       int moved;
        assert_spin_locked(&wb->list_lock);
        list_splice_init(&wb->b_more_io, &wb->b_io);
-       move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this);
+       moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this);
+       trace_writeback_queue_io(wb, older_than_this, moved);
 }
 
 static int write_inode(struct inode *inode, struct writeback_control *wbc)
@@ -346,6 +353,7 @@ writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
                       struct writeback_control *wbc)
 {
        struct address_space *mapping = inode->i_mapping;
+       long nr_to_write = wbc->nr_to_write;
        unsigned dirty;
        int ret;
 
@@ -368,6 +376,8 @@ writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
                 */
                if (wbc->sync_mode != WB_SYNC_ALL) {
                        requeue_io(inode, wb);
+                       trace_writeback_single_inode_requeue(inode, wbc,
+                                                            nr_to_write);
                        return 0;
                }
 
@@ -463,37 +473,42 @@ writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
                         * No need to add it back to the LRU.
                         */
                        list_del_init(&inode->i_wb_list);
-                       wbc->inodes_written++;
                }
        }
        inode_sync_complete(inode);
+       trace_writeback_single_inode(inode, wbc, nr_to_write);
        return ret;
 }
 
-/*
- * For background writeback the caller does not have the sb pinned
- * before calling writeback. So make sure that we do pin it, so it doesn't
- * go away while we are writing inodes from it.
- */
-static bool pin_sb_for_writeback(struct super_block *sb)
+static long writeback_chunk_size(struct backing_dev_info *bdi,
+                                struct wb_writeback_work *work)
 {
-       spin_lock(&sb_lock);
-       if (list_empty(&sb->s_instances)) {
-               spin_unlock(&sb_lock);
-               return false;
-       }
-
-       sb->s_count++;
-       spin_unlock(&sb_lock);
+       long pages;
 
-       if (down_read_trylock(&sb->s_umount)) {
-               if (sb->s_root)
-                       return true;
-               up_read(&sb->s_umount);
+       /*
+        * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
+        * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
+        * here avoids calling into writeback_inodes_wb() more than once.
+        *
+        * The intended call sequence for WB_SYNC_ALL writeback is:
+        *
+        *      wb_writeback()
+        *          writeback_sb_inodes()       <== called only once
+        *              write_cache_pages()     <== called once for each inode
+        *                   (quickly) tag currently dirty pages
+        *                   (maybe slowly) sync all tagged pages
+        */
+       if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages)
+               pages = LONG_MAX;
+       else {
+               pages = min(bdi->avg_write_bandwidth / 2,
+                           global_dirty_limit / DIRTY_SCOPE);
+               pages = min(pages, work->nr_pages);
+               pages = round_down(pages + MIN_WRITEBACK_PAGES,
+                                  MIN_WRITEBACK_PAGES);
        }
 
-       put_super(sb);
-       return false;
+       return pages;
 }
 
 /*
@@ -503,18 +518,30 @@ static bool pin_sb_for_writeback(struct super_block *sb)
  * inodes. Otherwise write only ones which go sequentially
  * in reverse order.
  *
- * Return 1, if the caller writeback routine should be
- * interrupted. Otherwise return 0.
+ * Return the number of pages and/or inodes written.
  */
-static int writeback_sb_inodes(struct super_block *sb, struct bdi_writeback *wb,
-               struct writeback_control *wbc, bool only_this_sb)
+static long writeback_sb_inodes(struct super_block *sb,
+                               struct bdi_writeback *wb,
+                               struct wb_writeback_work *work)
 {
+       struct writeback_control wbc = {
+               .sync_mode              = work->sync_mode,
+               .tagged_writepages      = work->tagged_writepages,
+               .for_kupdate            = work->for_kupdate,
+               .for_background         = work->for_background,
+               .range_cyclic           = work->range_cyclic,
+               .range_start            = 0,
+               .range_end              = LLONG_MAX,
+       };
+       unsigned long start_time = jiffies;
+       long write_chunk;
+       long wrote = 0;  /* count both pages and inodes */
+
        while (!list_empty(&wb->b_io)) {
-               long pages_skipped;
                struct inode *inode = wb_inode(wb->b_io.prev);
 
                if (inode->i_sb != sb) {
-                       if (only_this_sb) {
+                       if (work->sb) {
                                /*
                                 * We only want to write back data for this
                                 * superblock, move all inodes not belonging
@@ -529,7 +556,7 @@ static int writeback_sb_inodes(struct super_block *sb, struct bdi_writeback *wb,
                         * Bounce back to the caller to unpin this and
                         * pin the next superblock.
                         */
-                       return 0;
+                       break;
                }
 
                /*
@@ -540,15 +567,21 @@ static int writeback_sb_inodes(struct super_block *sb, struct bdi_writeback *wb,
                spin_lock(&inode->i_lock);
                if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
                        spin_unlock(&inode->i_lock);
-                       requeue_io(inode, wb);
+                       redirty_tail(inode, wb);
                        continue;
                }
-
                __iget(inode);
+               write_chunk = writeback_chunk_size(wb->bdi, work);
+               wbc.nr_to_write = write_chunk;
+               wbc.pages_skipped = 0;
+
+               writeback_single_inode(inode, wb, &wbc);
 
-               pages_skipped = wbc->pages_skipped;
-               writeback_single_inode(inode, wb, wbc);
-               if (wbc->pages_skipped != pages_skipped) {
+               work->nr_pages -= write_chunk - wbc.nr_to_write;
+               wrote += write_chunk - wbc.nr_to_write;
+               if (!(inode->i_state & I_DIRTY))
+                       wrote++;
+               if (wbc.pages_skipped) {
                        /*
                         * writeback is not making progress due to locked
                         * buffers.  Skip this inode for now.
@@ -560,53 +593,70 @@ static int writeback_sb_inodes(struct super_block *sb, struct bdi_writeback *wb,
                iput(inode);
                cond_resched();
                spin_lock(&wb->list_lock);
-               if (wbc->nr_to_write <= 0)
-                       return 1;
+               /*
+                * bail out to wb_writeback() often enough to check
+                * background threshold and other termination conditions.
+                */
+               if (wrote) {
+                       if (time_is_before_jiffies(start_time + HZ / 10UL))
+                               break;
+                       if (work->nr_pages <= 0)
+                               break;
+               }
        }
-       /* b_io is empty */
-       return 1;
+       return wrote;
 }
 
-static void __writeback_inodes_wb(struct bdi_writeback *wb,
-                                 struct writeback_control *wbc)
+static long __writeback_inodes_wb(struct bdi_writeback *wb,
+                                 struct wb_writeback_work *work)
 {
-       int ret = 0;
+       unsigned long start_time = jiffies;
+       long wrote = 0;
 
        while (!list_empty(&wb->b_io)) {
                struct inode *inode = wb_inode(wb->b_io.prev);
                struct super_block *sb = inode->i_sb;
 
-               if (!pin_sb_for_writeback(sb)) {
-                       requeue_io(inode, wb);
+               if (!grab_super_passive(sb)) {
+                       /*
+                        * grab_super_passive() may fail consistently due to
+                        * s_umount being grabbed by someone else. Don't use
+                        * requeue_io() to avoid busy retrying the inode/sb.
+                        */
+                       redirty_tail(inode, wb);
                        continue;
                }
-               ret = writeback_sb_inodes(sb, wb, wbc, false);
+               wrote += writeback_sb_inodes(sb, wb, work);
                drop_super(sb);
 
-               if (ret)
-                       break;
+               /* refer to the same tests at the end of writeback_sb_inodes */
+               if (wrote) {
+                       if (time_is_before_jiffies(start_time + HZ / 10UL))
+                               break;
+                       if (work->nr_pages <= 0)
+                               break;
+               }
        }
        /* Leave any unwritten inodes on b_io */
+       return wrote;
 }
 
-void writeback_inodes_wb(struct bdi_writeback *wb,
-               struct writeback_control *wbc)
+long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages)
 {
+       struct wb_writeback_work work = {
+               .nr_pages       = nr_pages,
+               .sync_mode      = WB_SYNC_NONE,
+               .range_cyclic   = 1,
+       };
+
        spin_lock(&wb->list_lock);
        if (list_empty(&wb->b_io))
-               queue_io(wb, wbc->older_than_this);
-       __writeback_inodes_wb(wb, wbc);
+               queue_io(wb, NULL);
+       __writeback_inodes_wb(wb, &work);
        spin_unlock(&wb->list_lock);
-}
 
-/*
- * The maximum number of pages to writeout in a single bdi flush/kupdate
- * operation.  We do this so we don't hold I_SYNC against an inode for
- * enormous amounts of time, which would block a userspace task which has
- * been forced to throttle against that inode.  Also, the code reevaluates
- * the dirty each time it has written this many pages.
- */
-#define MAX_WRITEBACK_PAGES     1024
+       return nr_pages - work.nr_pages;
+}
 
 static inline bool over_bground_thresh(void)
 {
@@ -618,6 +668,16 @@ static inline bool over_bground_thresh(void)
                global_page_state(NR_UNSTABLE_NFS) > background_thresh);
 }
 
+/*
+ * Called under wb->list_lock. If there are multiple wb per bdi,
+ * only the flusher working on the first wb should do it.
+ */
+static void wb_update_bandwidth(struct bdi_writeback *wb,
+                               unsigned long start_time)
+{
+       __bdi_update_bandwidth(wb->bdi, 0, 0, 0, 0, start_time);
+}
+
 /*
  * Explicit flushing or periodic writeback of "old" data.
  *
@@ -636,42 +696,14 @@ static inline bool over_bground_thresh(void)
 static long wb_writeback(struct bdi_writeback *wb,
                         struct wb_writeback_work *work)
 {
-       struct writeback_control wbc = {
-               .sync_mode              = work->sync_mode,
-               .tagged_writepages      = work->tagged_writepages,
-               .older_than_this        = NULL,
-               .for_kupdate            = work->for_kupdate,
-               .for_background         = work->for_background,
-               .range_cyclic           = work->range_cyclic,
-       };
+       unsigned long wb_start = jiffies;
+       long nr_pages = work->nr_pages;
        unsigned long oldest_jif;
-       long wrote = 0;
-       long write_chunk = MAX_WRITEBACK_PAGES;
        struct inode *inode;
-
-       if (!wbc.range_cyclic) {
-               wbc.range_start = 0;
-               wbc.range_end = LLONG_MAX;
-       }
-
-       /*
-        * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
-        * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
-        * here avoids calling into writeback_inodes_wb() more than once.
-        *
-        * The intended call sequence for WB_SYNC_ALL writeback is:
-        *
-        *      wb_writeback()
-        *          writeback_sb_inodes()       <== called only once
-        *              write_cache_pages()     <== called once for each inode
-        *                   (quickly) tag currently dirty pages
-        *                   (maybe slowly) sync all tagged pages
-        */
-       if (wbc.sync_mode == WB_SYNC_ALL || wbc.tagged_writepages)
-               write_chunk = LONG_MAX;
+       long progress;
 
        oldest_jif = jiffies;
-       wbc.older_than_this = &oldest_jif;
+       work->older_than_this = &oldest_jif;
 
        spin_lock(&wb->list_lock);
        for (;;) {
@@ -701,24 +733,19 @@ static long wb_writeback(struct bdi_writeback *wb,
                if (work->for_kupdate) {
                        oldest_jif = jiffies -
                                msecs_to_jiffies(dirty_expire_interval * 10);
-                       wbc.older_than_this = &oldest_jif;
+                       work->older_than_this = &oldest_jif;
                }
 
-               wbc.nr_to_write = write_chunk;
-               wbc.pages_skipped = 0;
-               wbc.inodes_written = 0;
-
-               trace_wbc_writeback_start(&wbc, wb->bdi);
+               trace_writeback_start(wb->bdi, work);
                if (list_empty(&wb->b_io))
-                       queue_io(wb, wbc.older_than_this);
+                       queue_io(wb, work->older_than_this);
                if (work->sb)
-                       writeback_sb_inodes(work->sb, wb, &wbc, true);
+                       progress = writeback_sb_inodes(work->sb, wb, work);
                else
-                       __writeback_inodes_wb(wb, &wbc);
-               trace_wbc_writeback_written(&wbc, wb->bdi);
+                       progress = __writeback_inodes_wb(wb, work);
+               trace_writeback_written(wb->bdi, work);
 
-               work->nr_pages -= write_chunk - wbc.nr_to_write;
-               wrote += write_chunk - wbc.nr_to_write;
+               wb_update_bandwidth(wb, wb_start);
 
                /*
                 * Did we write something? Try for more
@@ -728,9 +755,7 @@ static long wb_writeback(struct bdi_writeback *wb,
                 * mean the overall work is done. So we keep looping as long
                 * as made some progress on cleaning pages or inodes.
                 */
-               if (wbc.nr_to_write < write_chunk)
-                       continue;
-               if (wbc.inodes_written)
+               if (progress)
                        continue;
                /*
                 * No more inodes for IO, bail
@@ -743,8 +768,8 @@ static long wb_writeback(struct bdi_writeback *wb,
                 * we'll just busyloop.
                 */
                if (!list_empty(&wb->b_more_io))  {
+                       trace_writeback_wait(wb->bdi, work);
                        inode = wb_inode(wb->b_more_io.prev);
-                       trace_wbc_writeback_wait(&wbc, wb->bdi);
                        spin_lock(&inode->i_lock);
                        inode_wait_for_writeback(inode, wb);
                        spin_unlock(&inode->i_lock);
@@ -752,7 +777,7 @@ static long wb_writeback(struct bdi_writeback *wb,
        }
        spin_unlock(&wb->list_lock);
 
-       return wrote;
+       return nr_pages - work->nr_pages;
 }
 
 /*