Skip to content

Commit 95a46c6

Browse files
htejunaxboe
authored andcommitted
writeback: make bdi_has_dirty_io() take multiple bdi_writeback's into account
bdi_has_dirty_io() used to only reflect whether the root wb (bdi_writeback) has dirty inodes. For cgroup writeback support, it needs to take all active wb's into account. If any wb on the bdi has dirty inodes, bdi_has_dirty_io() should return true. To achieve that, as inode_wb_list_{move|del}_locked() now keep track of the dirty state transition of each wb, the number of dirty wbs can be counted in the bdi; however, bdi is already aggregating wb->avg_write_bandwidth which can easily be guaranteed to be > 0 when there are any dirty inodes by ensuring wb->avg_write_bandwidth can't dip below 1. bdi_has_dirty_io() can simply test whether bdi->tot_write_bandwidth is zero or not. While this bumps the value of wb->avg_write_bandwidth to one when it used to be zero, this shouldn't cause any meaningful behavior difference. bdi_has_dirty_io() is made an inline function which tests whether ->tot_write_bandwidth is non-zero. Also, WARN_ON_ONCE()'s on its value are added to inode_wb_list_{move|del}_locked(). Signed-off-by: Tejun Heo <[email protected]> Cc: Jens Axboe <[email protected]> Cc: Jan Kara <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 766a9d6 commit 95a46c6

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

fs/fs-writeback.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ static bool wb_io_lists_populated(struct bdi_writeback *wb)
9999
return false;
100100
} else {
101101
set_bit(WB_has_dirty_io, &wb->state);
102+
WARN_ON_ONCE(!wb->avg_write_bandwidth);
102103
atomic_long_add(wb->avg_write_bandwidth,
103104
&wb->bdi->tot_write_bandwidth);
104105
return true;
@@ -110,8 +111,8 @@ static void wb_io_lists_depopulated(struct bdi_writeback *wb)
110111
if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) &&
111112
list_empty(&wb->b_io) && list_empty(&wb->b_more_io)) {
112113
clear_bit(WB_has_dirty_io, &wb->state);
113-
atomic_long_sub(wb->avg_write_bandwidth,
114-
&wb->bdi->tot_write_bandwidth);
114+
WARN_ON_ONCE(atomic_long_sub_return(wb->avg_write_bandwidth,
115+
&wb->bdi->tot_write_bandwidth) < 0);
115116
}
116117
}
117118

include/linux/backing-dev-defs.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ struct bdi_writeback {
9898
unsigned long dirtied_stamp;
9999
unsigned long written_stamp; /* pages written at bw_time_stamp */
100100
unsigned long write_bandwidth; /* the estimated write bandwidth */
101-
unsigned long avg_write_bandwidth; /* further smoothed write bw */
101+
unsigned long avg_write_bandwidth; /* further smoothed write bw, > 0 */
102102

103103
/*
104104
* The base dirty throttle rate, re-calculated on every 200ms.
@@ -142,7 +142,11 @@ struct backing_dev_info {
142142
unsigned int min_ratio;
143143
unsigned int max_ratio, max_prop_frac;
144144

145-
atomic_long_t tot_write_bandwidth; /* sum of active avg_write_bw */
145+
/*
146+
* Sum of avg_write_bw of wbs with dirty inodes. > 0 if there are
147+
* any dirty wbs, which is depended upon by bdi_has_dirty().
148+
*/
149+
atomic_long_t tot_write_bandwidth;
146150

147151
struct bdi_writeback wb; /* the root writeback info for this bdi */
148152
struct bdi_writeback_congested wb_congested; /* its congested state */

include/linux/backing-dev.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
2929
enum wb_reason reason);
3030
void bdi_start_background_writeback(struct backing_dev_info *bdi);
3131
void wb_workfn(struct work_struct *work);
32-
bool bdi_has_dirty_io(struct backing_dev_info *bdi);
3332
void wb_wakeup_delayed(struct bdi_writeback *wb);
3433

3534
extern spinlock_t bdi_lock;
@@ -42,6 +41,15 @@ static inline bool wb_has_dirty_io(struct bdi_writeback *wb)
4241
return test_bit(WB_has_dirty_io, &wb->state);
4342
}
4443

44+
static inline bool bdi_has_dirty_io(struct backing_dev_info *bdi)
45+
{
46+
/*
47+
* @bdi->tot_write_bandwidth is guaranteed to be > 0 if there are
48+
* any dirty wbs. See wb_update_write_bandwidth().
49+
*/
50+
return atomic_long_read(&bdi->tot_write_bandwidth);
51+
}
52+
4553
static inline void __add_wb_stat(struct bdi_writeback *wb,
4654
enum wb_stat_item item, s64 amount)
4755
{

mm/backing-dev.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,6 @@ static int __init default_bdi_init(void)
256256
}
257257
subsys_initcall(default_bdi_init);
258258

259-
bool bdi_has_dirty_io(struct backing_dev_info *bdi)
260-
{
261-
return wb_has_dirty_io(&bdi->wb);
262-
}
263-
264259
/*
265260
* This function is used when the first inode for this wb is marked dirty. It
266261
* wakes-up the corresponding bdi thread which should then take care of the

mm/page-writeback.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -881,9 +881,13 @@ static void wb_update_write_bandwidth(struct bdi_writeback *wb,
881881
avg += (old - avg) >> 3;
882882

883883
out:
884-
if (wb_has_dirty_io(wb))
885-
atomic_long_add(avg - wb->avg_write_bandwidth,
886-
&wb->bdi->tot_write_bandwidth);
884+
/* keep avg > 0 to guarantee that tot > 0 if there are dirty wbs */
885+
avg = max(avg, 1LU);
886+
if (wb_has_dirty_io(wb)) {
887+
long delta = avg - wb->avg_write_bandwidth;
888+
WARN_ON_ONCE(atomic_long_add_return(delta,
889+
&wb->bdi->tot_write_bandwidth) <= 0);
890+
}
887891
wb->write_bandwidth = bw;
888892
wb->avg_write_bandwidth = avg;
889893
}

0 commit comments

Comments
 (0)