diff mbox series

[02/10] writeback: add general function domain_dirty_avail to calculate dirty and avail of domain

Message ID 20240429034738.138609-3-shikemeng@huaweicloud.com (mailing list archive)
State New
Headers show
Series Add helper functions to remove repeated code and | expand

Commit Message

Kemeng Shi April 29, 2024, 3:47 a.m. UTC
Add general function domain_dirty_avail to calculate dirty and avail for
either dirty limit or background writeback in either global domain or wb
domain.

Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
---
 mm/page-writeback.c | 50 +++++++++++++++++++++++++++++++++------------
 1 file changed, 37 insertions(+), 13 deletions(-)

Comments

Tejun Heo May 1, 2024, 4:49 p.m. UTC | #1
Hello,

On Mon, Apr 29, 2024 at 11:47:30AM +0800, Kemeng Shi wrote:
> +/*
> + * Dirty background will ignore pages being written as we're trying to
> + * decide whether to put more under writeback.
> + */
> +static void domain_dirty_avail(struct dirty_throttle_control *dtc, bool bg)

I wonder whether it'd be better if the bool arg is flipped to something like
`bool include_writeback` so that it's clear what the difference is between
the two. Also, do global_domain_dirty_avail() and wb_domain_dirty_avail()
have to be separate functions? They seem trivial enough to include into the
body of domain_dirty_avail(). Are they used directly elsewhere?

> +{
> +	struct dirty_throttle_control *gdtc = mdtc_gdtc(dtc);
> +
> +	if (gdtc)

I know this test is used elsewhere but it isn't the most intuitive. Would it
make sense to add dtc_is_global() (or dtc_is_gdtc()) helper instead?

> +		wb_domain_dirty_avail(dtc, bg);
> +	else
> +		global_domain_dirty_avail(dtc, bg);
> +}

Thanks.
Kemeng Shi May 6, 2024, 1:32 a.m. UTC | #2
on 5/2/2024 12:49 AM, Tejun Heo wrote:
> Hello,
> 
> On Mon, Apr 29, 2024 at 11:47:30AM +0800, Kemeng Shi wrote:
>> +/*
>> + * Dirty background will ignore pages being written as we're trying to
>> + * decide whether to put more under writeback.
>> + */
>> +static void domain_dirty_avail(struct dirty_throttle_control *dtc, bool bg)
> 
> I wonder whether it'd be better if the bool arg is flipped to something like
> `bool include_writeback` so that it's clear what the difference is between
Sure, I rename 'bool bg' to 'bool include_writeback'.
> the two. Also, do global_domain_dirty_avail() and wb_domain_dirty_avail()
> have to be separate functions? They seem trivial enough to include into the
> body of domain_dirty_avail(). Are they used directly elsewhere?
I will fold global_domain_dirty_avail() and wb_domain_dirty_avail() and
just use domain_dirty_avail.
> 
>> +{
>> +	struct dirty_throttle_control *gdtc = mdtc_gdtc(dtc);
>> +
>> +	if (gdtc)
> 
> I know this test is used elsewhere but it isn't the most intuitive. Would it
> make sense to add dtc_is_global() (or dtc_is_gdtc()) helper instead?
Will add helper dtc_is_global().

Thanks.
Kemeng
> 
>> +		wb_domain_dirty_avail(dtc, bg);
>> +	else
>> +		global_domain_dirty_avail(dtc, bg);
>> +}
> 
> Thanks.
>
diff mbox series

Patch

diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index e1f73643aca1..e4f181d52035 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -837,6 +837,41 @@  static void mdtc_calc_avail(struct dirty_throttle_control *mdtc,
 	mdtc->avail = filepages + min(headroom, other_clean);
 }
 
+static inline void
+global_domain_dirty_avail(struct dirty_throttle_control *dtc, bool bg)
+{
+	dtc->avail = global_dirtyable_memory();
+	dtc->dirty = global_node_page_state(NR_FILE_DIRTY);
+	if (!bg)
+		dtc->dirty += global_node_page_state(NR_WRITEBACK);
+}
+
+static inline void
+wb_domain_dirty_avail(struct dirty_throttle_control *dtc, bool bg)
+{
+	unsigned long filepages = 0, headroom = 0, writeback = 0;
+
+	mem_cgroup_wb_stats(dtc->wb, &filepages, &headroom, &dtc->dirty,
+			    &writeback);
+	if (!bg)
+		dtc->dirty += writeback;
+	mdtc_calc_avail(dtc, filepages, headroom);
+}
+
+/*
+ * Dirty background will ignore pages being written as we're trying to
+ * decide whether to put more under writeback.
+ */
+static void domain_dirty_avail(struct dirty_throttle_control *dtc, bool bg)
+{
+	struct dirty_throttle_control *gdtc = mdtc_gdtc(dtc);
+
+	if (gdtc)
+		wb_domain_dirty_avail(dtc, bg);
+	else
+		global_domain_dirty_avail(dtc, bg);
+}
+
 /**
  * __wb_calc_thresh - @wb's share of dirty threshold
  * @dtc: dirty_throttle_context of interest
@@ -2119,14 +2154,8 @@  bool wb_over_bg_thresh(struct bdi_writeback *wb)
 	struct dirty_throttle_control * const mdtc = mdtc_valid(&mdtc_stor) ?
 						     &mdtc_stor : NULL;
 
-	/*
-	 * Similar to balance_dirty_pages() but ignores pages being written
-	 * as we're trying to decide whether to put more under writeback.
-	 */
-	gdtc->avail = global_dirtyable_memory();
-	gdtc->dirty = global_node_page_state(NR_FILE_DIRTY);
+	domain_dirty_avail(gdtc, true);
 	domain_dirty_limits(gdtc);
-
 	if (gdtc->dirty > gdtc->bg_thresh)
 		return true;
 
@@ -2135,13 +2164,8 @@  bool wb_over_bg_thresh(struct bdi_writeback *wb)
 		return true;
 
 	if (mdtc) {
-		unsigned long filepages, headroom, writeback;
-
-		mem_cgroup_wb_stats(wb, &filepages, &headroom, &mdtc->dirty,
-				    &writeback);
-		mdtc_calc_avail(mdtc, filepages, headroom);
+		domain_dirty_avail(mdtc, true);
 		domain_dirty_limits(mdtc);	/* ditto, ignore writeback */
-
 		if (mdtc->dirty > mdtc->bg_thresh)
 			return true;