ext4: mark buffer new if it is unwritten to avoid stale data exposure
[platform/kernel/linux-starfive.git] / fs / fs-writeback.c
index 443f833..be2d329 100644 (file)
@@ -611,6 +611,24 @@ out_free:
        kfree(isw);
 }
 
+static bool isw_prepare_wbs_switch(struct inode_switch_wbs_context *isw,
+                                  struct list_head *list, int *nr)
+{
+       struct inode *inode;
+
+       list_for_each_entry(inode, list, i_io_list) {
+               if (!inode_prepare_wbs_switch(inode, isw->new_wb))
+                       continue;
+
+               isw->inodes[*nr] = inode;
+               (*nr)++;
+
+               if (*nr >= WB_MAX_INODES_PER_ISW - 1)
+                       return true;
+       }
+       return false;
+}
+
 /**
  * cleanup_offline_cgwb - detach associated inodes
  * @wb: target wb
@@ -623,7 +641,6 @@ bool cleanup_offline_cgwb(struct bdi_writeback *wb)
 {
        struct cgroup_subsys_state *memcg_css;
        struct inode_switch_wbs_context *isw;
-       struct inode *inode;
        int nr;
        bool restart = false;
 
@@ -645,17 +662,17 @@ bool cleanup_offline_cgwb(struct bdi_writeback *wb)
 
        nr = 0;
        spin_lock(&wb->list_lock);
-       list_for_each_entry(inode, &wb->b_attached, i_io_list) {
-               if (!inode_prepare_wbs_switch(inode, isw->new_wb))
-                       continue;
-
-               isw->inodes[nr++] = inode;
-
-               if (nr >= WB_MAX_INODES_PER_ISW - 1) {
-                       restart = true;
-                       break;
-               }
-       }
+       /*
+        * In addition to the inodes that have completed writeback, also switch
+        * cgwbs for those inodes only with dirty timestamps. Otherwise, those
+        * inodes won't be written back for a long time when lazytime is
+        * enabled, and thus pinning the dying cgwbs. It won't break the
+        * bandwidth restrictions, as writeback of inode metadata is not
+        * accounted for.
+        */
+       restart = isw_prepare_wbs_switch(isw, &wb->b_attached, &nr);
+       if (!restart)
+               restart = isw_prepare_wbs_switch(isw, &wb->b_dirty_time, &nr);
        spin_unlock(&wb->list_lock);
 
        /* no attached inodes? bail out */
@@ -827,7 +844,7 @@ void wbc_detach_inode(struct writeback_control *wbc)
                 * is okay.  The main goal is avoiding keeping an inode on
                 * the wrong wb for an extended period of time.
                 */
-               if (hweight32(history) > WB_FRN_HIST_THR_SLOTS)
+               if (hweight16(history) > WB_FRN_HIST_THR_SLOTS)
                        inode_switch_wbs(inode, max_id);
        }
 
@@ -974,6 +991,16 @@ restart:
                        continue;
                }
 
+               /*
+                * If wb_tryget fails, the wb has been shutdown, skip it.
+                *
+                * Pin @wb so that it stays on @bdi->wb_list.  This allows
+                * continuing iteration from @wb after dropping and
+                * regrabbing rcu read lock.
+                */
+               if (!wb_tryget(wb))
+                       continue;
+
                /* alloc failed, execute synchronously using on-stack fallback */
                work = &fallback_work;
                *work = *base_work;
@@ -982,13 +1009,6 @@ restart:
                work->done = &fallback_work_done;
 
                wb_queue_work(wb, work);
-
-               /*
-                * Pin @wb so that it stays on @bdi->wb_list.  This allows
-                * continuing iteration from @wb after dropping and
-                * regrabbing rcu read lock.
-                */
-               wb_get(wb);
                last_wb = wb;
 
                rcu_read_unlock();
@@ -1519,10 +1539,15 @@ static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
 
        if (wbc->pages_skipped) {
                /*
-                * writeback is not making progress due to locked
-                * buffers. Skip this inode for now.
+                * Writeback is not making progress due to locked buffers.
+                * Skip this inode for now. Although having skipped pages
+                * is odd for clean inodes, it can happen for some
+                * filesystems so handle that gracefully.
                 */
-               redirty_tail_locked(inode, wb);
+               if (inode->i_state & I_DIRTY_ALL)
+                       redirty_tail_locked(inode, wb);
+               else
+                       inode_cgwb_move_to_attached(inode, wb);
                return;
        }
 
@@ -1712,18 +1737,26 @@ static int writeback_single_inode(struct inode *inode,
        wb = inode_to_wb_and_lock_list(inode);
        spin_lock(&inode->i_lock);
        /*
-        * If the inode is now fully clean, then it can be safely removed from
-        * its writeback list (if any).  Otherwise the flusher threads are
-        * responsible for the writeback lists.
+        * If the inode is freeing, its i_io_list shoudn't be updated
+        * as it can be finally deleted at this moment.
         */
-       if (!(inode->i_state & I_DIRTY_ALL))
-               inode_cgwb_move_to_attached(inode, wb);
-       else if (!(inode->i_state & I_SYNC_QUEUED)) {
-               if ((inode->i_state & I_DIRTY))
-                       redirty_tail_locked(inode, wb);
-               else if (inode->i_state & I_DIRTY_TIME) {
-                       inode->dirtied_when = jiffies;
-                       inode_io_list_move_locked(inode, wb, &wb->b_dirty_time);
+       if (!(inode->i_state & I_FREEING)) {
+               /*
+                * If the inode is now fully clean, then it can be safely
+                * removed from its writeback list (if any). Otherwise the
+                * flusher threads are responsible for the writeback lists.
+                */
+               if (!(inode->i_state & I_DIRTY_ALL))
+                       inode_cgwb_move_to_attached(inode, wb);
+               else if (!(inode->i_state & I_SYNC_QUEUED)) {
+                       if ((inode->i_state & I_DIRTY))
+                               redirty_tail_locked(inode, wb);
+                       else if (inode->i_state & I_DIRTY_TIME) {
+                               inode->dirtied_when = jiffies;
+                               inode_io_list_move_locked(inode,
+                                                         wb,
+                                                         &wb->b_dirty_time);
+                       }
                }
        }