linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tejun Heo <tj@kernel.org>
To: axboe@kernel.dk
Cc: linux-kernel@vger.kernel.org, jack@suse.cz, hch@infradead.org,
	hannes@cmpxchg.org, linux-fsdevel@vger.kernel.org,
	vgoyal@redhat.com, lizefan@huawei.com, cgroups@vger.kernel.org,
	linux-mm@kvack.org, mhocko@suse.cz, clm@fb.com,
	fengguang.wu@intel.com, david@fromorbit.com, gthelen@google.com,
	Tejun Heo <tj@kernel.org>
Subject: [PATCH 30/49] writeback: implement WB_has_dirty_io wb_state flag
Date: Mon,  6 Apr 2015 15:58:19 -0400	[thread overview]
Message-ID: <1428350318-8215-31-git-send-email-tj@kernel.org> (raw)
In-Reply-To: <1428350318-8215-1-git-send-email-tj@kernel.org>

Currently, wb_has_dirty_io() determines whether a wb (bdi_writeback)
has any dirty inode by testing all three IO lists on each invocation
without actively keeping track.  For cgroup writeback support, a
single bdi will host multiple wb's each of which will host dirty
inodes separately and we'll need to make bdi_has_dirty_io(), which
currently only represents the root wb, aggregate has_dirty_io from all
member wb's, which requires tracking transitions in has_dirty_io state
on each wb.

This patch introduces inode_wb_list_{move|del}_locked() to consolidate
IO list operations leaving queue_io() the only other function which
directly manipulates IO lists (via move_expired_inodes()).  All three
functions are updated to call wb_io_lists_[de]populated() which keep
track of whether the wb has dirty inodes or not and record it using
the new WB_has_dirty_io flag.  inode_wb_list_moved_locked()'s return
value indicates whether the wb had no dirty inodes before.

mark_inode_dirty() is restructured so that the return value of
inode_wb_list_move_locked() can be used for deciding whether to wake
up the wb.

While at it, change {bdi|wb}_has_dirty_io()'s return values to bool.
These functions were returning 0 and 1 before.  Also, add a comment
explaining the synchronization of wb_state flags.

v2: Updated to accommodate b_dirty_time.

Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Jan Kara <jack@suse.cz>
---
 fs/fs-writeback.c                | 104 ++++++++++++++++++++++++++++++---------
 include/linux/backing-dev-defs.h |   1 +
 include/linux/backing-dev.h      |   8 ++-
 mm/backing-dev.c                 |   2 +-
 4 files changed, 86 insertions(+), 29 deletions(-)

diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
index 1d30249..29b4f8f 100644
--- a/fs/fs-writeback.c
+++ b/fs/fs-writeback.c
@@ -81,6 +81,66 @@ static inline struct inode *wb_inode(struct list_head *head)
 
 EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage);
 
+static bool wb_io_lists_populated(struct bdi_writeback *wb)
+{
+	if (wb_has_dirty_io(wb)) {
+		return false;
+	} else {
+		set_bit(WB_has_dirty_io, &wb->state);
+		return true;
+	}
+}
+
+static void wb_io_lists_depopulated(struct bdi_writeback *wb)
+{
+	if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) &&
+	    list_empty(&wb->b_io) && list_empty(&wb->b_more_io))
+		clear_bit(WB_has_dirty_io, &wb->state);
+}
+
+/**
+ * inode_wb_list_move_locked - move an inode onto a bdi_writeback IO list
+ * @inode: inode to be moved
+ * @wb: target bdi_writeback
+ * @head: one of @wb->b_{dirty|io|more_io}
+ *
+ * Move @inode->i_wb_list to @list of @wb and set %WB_has_dirty_io.
+ * Returns %true if @inode is the first occupant of the !dirty_time IO
+ * lists; otherwise, %false.
+ */
+static bool inode_wb_list_move_locked(struct inode *inode,
+				      struct bdi_writeback *wb,
+				      struct list_head *head)
+{
+	assert_spin_locked(&wb->list_lock);
+
+	list_move(&inode->i_wb_list, head);
+
+	/* dirty_time doesn't count as dirty_io until expiration */
+	if (head != &wb->b_dirty_time)
+		return wb_io_lists_populated(wb);
+
+	wb_io_lists_depopulated(wb);
+	return false;
+}
+
+/**
+ * inode_wb_list_del_locked - remove an inode from its bdi_writeback IO list
+ * @inode: inode to be removed
+ * @wb: bdi_writeback @inode is being removed from
+ *
+ * Remove @inode which may be on one of @wb->b_{dirty|io|more_io} lists and
+ * clear %WB_has_dirty_io if all are empty afterwards.
+ */
+static void inode_wb_list_del_locked(struct inode *inode,
+				     struct bdi_writeback *wb)
+{
+	assert_spin_locked(&wb->list_lock);
+
+	list_del_init(&inode->i_wb_list);
+	wb_io_lists_depopulated(wb);
+}
+
 static void wb_wakeup(struct bdi_writeback *wb)
 {
 	spin_lock_bh(&wb->work_lock);
@@ -205,7 +265,7 @@ void inode_wb_list_del(struct inode *inode)
 	struct bdi_writeback *wb = inode_to_wb(inode);
 
 	spin_lock(&wb->list_lock);
-	list_del_init(&inode->i_wb_list);
+	inode_wb_list_del_locked(inode, wb);
 	spin_unlock(&wb->list_lock);
 }
 
@@ -220,7 +280,6 @@ void inode_wb_list_del(struct inode *inode)
  */
 static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
 {
-	assert_spin_locked(&wb->list_lock);
 	if (!list_empty(&wb->b_dirty)) {
 		struct inode *tail;
 
@@ -228,7 +287,7 @@ static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
 		if (time_before(inode->dirtied_when, tail->dirtied_when))
 			inode->dirtied_when = jiffies;
 	}
-	list_move(&inode->i_wb_list, &wb->b_dirty);
+	inode_wb_list_move_locked(inode, wb, &wb->b_dirty);
 }
 
 /*
@@ -236,8 +295,7 @@ static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
  */
 static void requeue_io(struct inode *inode, struct bdi_writeback *wb)
 {
-	assert_spin_locked(&wb->list_lock);
-	list_move(&inode->i_wb_list, &wb->b_more_io);
+	inode_wb_list_move_locked(inode, wb, &wb->b_more_io);
 }
 
 static void inode_sync_complete(struct inode *inode)
@@ -346,6 +404,8 @@ static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work)
 	moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work);
 	moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io,
 				     EXPIRE_DIRTY_ATIME, work);
+	if (moved)
+		wb_io_lists_populated(wb);
 	trace_writeback_queue_io(wb, work, moved);
 }
 
@@ -470,10 +530,10 @@ static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
 		 */
 		redirty_tail(inode, wb);
 	} else if (inode->i_state & I_DIRTY_TIME) {
-		list_move(&inode->i_wb_list, &wb->b_dirty_time);
+		inode_wb_list_move_locked(inode, wb, &wb->b_dirty_time);
 	} else {
 		/* The inode is clean. Remove from writeback lists. */
-		list_del_init(&inode->i_wb_list);
+		inode_wb_list_del_locked(inode, wb);
 	}
 }
 
@@ -610,7 +670,7 @@ writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
 	 * touch it. See comment above for explanation.
 	 */
 	if (!(inode->i_state & I_DIRTY_ALL))
-		list_del_init(&inode->i_wb_list);
+		inode_wb_list_del_locked(inode, wb);
 	spin_unlock(&wb->list_lock);
 	inode_sync_complete(inode);
 out:
@@ -1264,27 +1324,25 @@ void __mark_inode_dirty(struct inode *inode, int flags)
 
 			spin_unlock(&inode->i_lock);
 			spin_lock(&bdi->wb.list_lock);
-			if (bdi_cap_writeback_dirty(bdi)) {
-				WARN(!test_bit(WB_registered, &bdi->wb.state),
-				     "bdi-%s not registered\n", bdi->name);
 
-				/*
-				 * If this is the first dirty inode for this
-				 * bdi, we have to wake-up the corresponding
-				 * bdi thread to make sure background
-				 * write-back happens later.
-				 */
-				if (!wb_has_dirty_io(&bdi->wb))
-					wakeup_bdi = true;
-			}
+			WARN(bdi_cap_writeback_dirty(bdi) &&
+			     !test_bit(WB_registered, &bdi->wb.state),
+			     "bdi-%s not registered\n", bdi->name);
 
 			inode->dirtied_when = jiffies;
-			list_move(&inode->i_wb_list, dirtytime ?
-				  &bdi->wb.b_dirty_time : &bdi->wb.b_dirty);
+			wakeup_bdi = inode_wb_list_move_locked(inode, &bdi->wb,
+					dirtytime ? &bdi->wb.b_dirty_time :
+						    &bdi->wb.b_dirty);
 			spin_unlock(&bdi->wb.list_lock);
 			trace_writeback_dirty_inode_enqueue(inode);
 
-			if (wakeup_bdi)
+			/*
+			 * If this is the first dirty inode for this bdi,
+			 * we have to wake-up the corresponding bdi thread
+			 * to make sure background write-back happens
+			 * later.
+			 */
+			if (bdi_cap_writeback_dirty(bdi) && wakeup_bdi)
 				wb_wakeup_delayed(&bdi->wb);
 			return;
 		}
diff --git a/include/linux/backing-dev-defs.h b/include/linux/backing-dev-defs.h
index eb38676..7a94b78 100644
--- a/include/linux/backing-dev-defs.h
+++ b/include/linux/backing-dev-defs.h
@@ -21,6 +21,7 @@ struct dentry;
 enum wb_state {
 	WB_registered,		/* bdi_register() was done */
 	WB_writeback_running,	/* Writeback is in progress */
+	WB_has_dirty_io,	/* Dirty inodes on ->b_{dirty|io|more_io} */
 };
 
 enum wb_congested_state {
diff --git a/include/linux/backing-dev.h b/include/linux/backing-dev.h
index 6f08821..3c8403c 100644
--- a/include/linux/backing-dev.h
+++ b/include/linux/backing-dev.h
@@ -29,7 +29,7 @@ void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
 			enum wb_reason reason);
 void bdi_start_background_writeback(struct backing_dev_info *bdi);
 void wb_workfn(struct work_struct *work);
-int bdi_has_dirty_io(struct backing_dev_info *bdi);
+bool bdi_has_dirty_io(struct backing_dev_info *bdi);
 void wb_wakeup_delayed(struct bdi_writeback *wb);
 
 extern spinlock_t bdi_lock;
@@ -37,11 +37,9 @@ extern struct list_head bdi_list;
 
 extern struct workqueue_struct *bdi_wq;
 
-static inline int wb_has_dirty_io(struct bdi_writeback *wb)
+static inline bool wb_has_dirty_io(struct bdi_writeback *wb)
 {
-	return !list_empty(&wb->b_dirty) ||
-	       !list_empty(&wb->b_io) ||
-	       !list_empty(&wb->b_more_io);
+	return test_bit(WB_has_dirty_io, &wb->state);
 }
 
 static inline void __add_wb_stat(struct bdi_writeback *wb,
diff --git a/mm/backing-dev.c b/mm/backing-dev.c
index 5029c4a..161ddf1 100644
--- a/mm/backing-dev.c
+++ b/mm/backing-dev.c
@@ -256,7 +256,7 @@ static int __init default_bdi_init(void)
 }
 subsys_initcall(default_bdi_init);
 
-int bdi_has_dirty_io(struct backing_dev_info *bdi)
+bool bdi_has_dirty_io(struct backing_dev_info *bdi)
 {
 	return wb_has_dirty_io(&bdi->wb);
 }
-- 
2.1.0

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

  parent reply	other threads:[~2015-04-06 19:58 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-06 19:57 [PATCHSET 1/3 v3 block/for-4.1/core] writeback: cgroup writeback support Tejun Heo
2015-04-06 19:57 ` [PATCH 01/49] memcg: add per cgroup dirty page accounting Tejun Heo
2015-04-06 19:57 ` [PATCH 02/49] blkcg: move block/blk-cgroup.h to include/linux/blk-cgroup.h Tejun Heo
2015-04-06 19:57 ` [PATCH 03/49] update !CONFIG_BLK_CGROUP dummies in include/linux/blk-cgroup.h Tejun Heo
2015-04-06 19:57 ` [PATCH 04/49] blkcg: always create the blkcg_gq for the root blkcg Tejun Heo
2015-04-06 19:57 ` [PATCH 05/49] memcg: add mem_cgroup_root_css Tejun Heo
2015-04-06 19:57 ` [PATCH 06/49] blkcg: add blkcg_root_css Tejun Heo
2015-04-06 19:57 ` [PATCH 07/49] cgroup, block: implement task_get_css() and use it in bio_associate_current() Tejun Heo
2015-04-06 19:57 ` [PATCH 08/49] blkcg: implement task_get_blkcg_css() Tejun Heo
2015-04-06 19:57 ` [PATCH 09/49] blkcg: implement bio_associate_blkcg() Tejun Heo
2015-04-06 19:57 ` [PATCH 10/49] memcg: implement mem_cgroup_css_from_page() Tejun Heo
2015-04-06 19:58 ` [PATCH 11/49] writeback: move backing_dev_info->state into bdi_writeback Tejun Heo
2015-04-20 15:32   ` Jan Kara
2015-04-06 19:58 ` [PATCH 12/49] writeback: move backing_dev_info->bdi_stat[] " Tejun Heo
2015-04-20 15:02   ` Jan Kara
     [not found]     ` <20150420150231.GA17020-+0h/O2h83AeN3ZZ/Hiejyg@public.gmane.org>
2015-04-20 17:56       ` Tejun Heo
     [not found]         ` <20150420175626.GB4206-piEFEHQLUPpN0TnZuCh8vA@public.gmane.org>
2015-04-21  8:51           ` Jan Kara
2015-04-21 15:02             ` Tejun Heo
     [not found]               ` <20150421150229.GA9455-piEFEHQLUPpN0TnZuCh8vA@public.gmane.org>
2015-04-21 15:05                 ` Jan Kara
2015-04-06 19:58 ` [PATCH 13/49] writeback: move bandwidth related fields from backing_dev_info " Tejun Heo
2015-04-20 15:09   ` Jan Kara
2015-04-20 18:01     ` Tejun Heo
2015-04-06 19:58 ` [PATCH 14/49] writeback: s/bdi/wb/ in mm/page-writeback.c Tejun Heo
2015-04-20 15:18   ` Jan Kara
2015-04-06 19:58 ` [PATCH 15/49] writeback: move backing_dev_info->wb_lock and ->worklist into bdi_writeback Tejun Heo
     [not found]   ` <1428350318-8215-16-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2015-04-20 15:32     ` Jan Kara
2015-04-20 18:17       ` Tejun Heo
     [not found]         ` <20150420181707.GD4206-piEFEHQLUPpN0TnZuCh8vA@public.gmane.org>
2015-04-21  8:59           ` Jan Kara
2015-04-06 19:58 ` [PATCH 16/49] writeback: reorganize mm/backing-dev.c Tejun Heo
     [not found]   ` <1428350318-8215-17-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2015-04-20 15:34     ` Jan Kara
2015-04-06 19:58 ` [PATCH 17/49] writeback: separate out include/linux/backing-dev-defs.h Tejun Heo
2015-04-20 15:37   ` Jan Kara
2015-04-06 19:58 ` [PATCH 18/49] bdi: make inode_to_bdi() inline Tejun Heo
     [not found]   ` <1428350318-8215-19-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2015-04-20 15:40     ` Jan Kara
2015-04-20 18:21       ` Tejun Heo
2015-04-06 19:58 ` [PATCH 19/49] writeback: add @gfp to wb_init() Tejun Heo
2015-04-20 15:41   ` Jan Kara
2015-04-06 19:58 ` [PATCH 20/49] bdi: separate out congested state into a separate struct Tejun Heo
2015-04-21 14:36   ` Jan Kara
2015-04-06 19:58 ` [PATCH 21/49] writeback: add {CONFIG|BDI_CAP|FS}_CGROUP_WRITEBACK Tejun Heo
2015-04-06 19:58 ` [PATCH 22/49] writeback: make backing_dev_info host cgroup-specific bdi_writebacks Tejun Heo
2015-04-06 19:58 ` [PATCH 23/49] writeback, blkcg: associate each blkcg_gq with the corresponding bdi_writeback_congested Tejun Heo
2015-04-06 19:58 ` [PATCH 24/49] writeback: attribute stats to the matching per-cgroup bdi_writeback Tejun Heo
     [not found] ` <1428350318-8215-1-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2015-04-06 19:58   ` [PATCH 25/49] writeback: let balance_dirty_pages() work on the matching cgroup bdi_writeback Tejun Heo
2015-04-06 19:58 ` [PATCH 26/49] writeback: make congestion functions per bdi_writeback Tejun Heo
2015-04-06 19:58 ` [PATCH 27/49] writeback, blkcg: restructure blk_{set|clear}_queue_congested() Tejun Heo
2015-04-06 19:58 ` [PATCH 28/49] writeback, blkcg: propagate non-root blkcg congestion state Tejun Heo
2015-04-06 19:58 ` [PATCH 29/49] writeback: implement and use inode_congested() Tejun Heo
2015-04-06 19:58 ` Tejun Heo [this message]
2015-04-06 19:58 ` [PATCH 31/49] writeback: implement backing_dev_info->tot_write_bandwidth Tejun Heo
2015-04-06 19:58 ` [PATCH 32/49] writeback: make bdi_has_dirty_io() take multiple bdi_writeback's into account Tejun Heo
2015-04-06 19:58 ` [PATCH 33/49] writeback: don't issue wb_writeback_work if clean Tejun Heo
2015-04-06 19:58 ` [PATCH 34/49] writeback: make bdi->min/max_ratio handling cgroup writeback aware Tejun Heo
2015-04-06 19:58 ` [PATCH 35/49] writeback: implement bdi_for_each_wb() Tejun Heo
2015-04-06 19:58 ` [PATCH 36/49] writeback: remove bdi_start_writeback() Tejun Heo
2015-04-06 19:58 ` [PATCH 37/49] writeback: make laptop_mode_timer_fn() handle multiple bdi_writeback's Tejun Heo
2015-04-06 19:58 ` [PATCH 38/49] writeback: make writeback_in_progress() take bdi_writeback instead of backing_dev_info Tejun Heo
2015-04-06 19:58 ` [PATCH 39/49] writeback: make bdi_start_background_writeback() " Tejun Heo
2015-04-06 19:58 ` [PATCH 40/49] writeback: make wakeup_flusher_threads() handle multiple bdi_writeback's Tejun Heo
2015-04-06 19:58 ` [PATCH 41/49] writeback: add wb_writeback_work->auto_free Tejun Heo
2015-04-06 19:58 ` [PATCH 42/49] writeback: implement bdi_wait_for_completion() Tejun Heo
2015-04-06 19:58 ` [PATCH 43/49] writeback: implement wb_wait_for_single_work() Tejun Heo
2015-04-06 19:58 ` [PATCH 44/49] writeback: restructure try_writeback_inodes_sb[_nr]() Tejun Heo
2015-04-06 19:58 ` [PATCH 45/49] writeback: make writeback initiation functions handle multiple bdi_writeback's Tejun Heo
2015-04-06 19:58 ` [PATCH 46/49] writeback: dirty inodes against their matching cgroup bdi_writeback's Tejun Heo
2015-04-06 19:58 ` [PATCH 47/49] buffer, writeback: make __block_write_full_page() honor cgroup writeback Tejun Heo
2015-04-06 19:58 ` [PATCH 48/49] mpage: make __mpage_writepage() " Tejun Heo
2015-04-06 19:58 ` [PATCH 49/49] ext2: enable cgroup writeback support Tejun Heo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1428350318-8215-31-git-send-email-tj@kernel.org \
    --to=tj@kernel.org \
    --cc=axboe@kernel.dk \
    --cc=cgroups@vger.kernel.org \
    --cc=clm@fb.com \
    --cc=david@fromorbit.com \
    --cc=fengguang.wu@intel.com \
    --cc=gthelen@google.com \
    --cc=hannes@cmpxchg.org \
    --cc=hch@infradead.org \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lizefan@huawei.com \
    --cc=mhocko@suse.cz \
    --cc=vgoyal@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).