Skip to contentSkip to conversation

mem_cgroup: optimize the atomic count of wb_completion

PATCH In review brookxu 2 participants 6 messages Sep 24, 2021

Subsystems (4)
lore
Share & tools
lore

brookxu

author
#0 lore raw

PATCH 1/2 mem_cgroup: optimize the atomic count of wb_completion

From: Chunguang Xu <[email protected]>

In order to track inflight foreign writeback, we init wb_completion.cnt to 1. For normal writeback, this cause wb_wait_for_completion() to perform meaningless atomic operations. Since foreign writebacks rarely occur in most scenarios, we can init wb_completion.cnt to 0 and set frn.done.cnt to 1. In this way we can avoid unnecessary atomic operations.

---
 fs/fs-writeback.c                | 1 -
 include/linux/backing-dev-defs.h | 2 +-
 mm/memcontrol.c                  | 7 ++++---
 3 files changed, 5 insertions(+), 5 deletions(-)
fs/fs-writeback.c +0 -1
index 81ec192..1ef10f2 100644
@@ -186,7 +186,6 @@ static void wb_queue_work(struct bdi_writeback *wb,
  */
 void wb_wait_for_completion(struct wb_completion *done)
 {
-	atomic_dec(&done->cnt);		/* put down the initial count */
 	wait_event(*done->waitq, !atomic_read(&done->cnt));
 }
 
include/linux/backing-dev-defs.h +1 -1
index 3320700..38bd571 100644
@@ -71,7 +71,7 @@ struct wb_completion {
 };
 
 #define __WB_COMPLETION_INIT(_waitq)	\
-	(struct wb_completion){ .cnt = ATOMIC_INIT(1), .waitq = (_waitq) }
+	(struct wb_completion){ .cnt = ATOMIC_INIT(0), .waitq = (_waitq) }
 
 /*
  * If one wants to wait for one or more wb_writeback_works, each work's
mm/memcontrol.c +4 -3
index b762215..3e1384a6 100644
@@ -5168,9 +5168,10 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
 #endif
 #ifdef CONFIG_CGROUP_WRITEBACK
 	INIT_LIST_HEAD(&memcg->cgwb_list);
-	for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++)
-		memcg->cgwb_frn[i].done =
-			__WB_COMPLETION_INIT(&memcg_cgwb_frn_waitq);
+	for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) {
+		atomic_set(&memcg->cgwb_frn[i].done.cnt, 1);
+		memcg->cgwb_frn[i].done.waitq = &memcg_cgwb_frn_waitq;
+	}
 #endif
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 	spin_lock_init(&memcg->deferred_split_queue.split_queue_lock);
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 2/2 mem_cgroup: introduce foreign_writeback_in_process() function

From: Chunguang Xu <[email protected]>

Directly use atomic_read(&frn->done.cnt) == 1 to check whether frn is issued, which makes the code a bit obscure. Maybe we should replace it with a more understandable function.

---
 mm/memcontrol.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
mm/memcontrol.c +8 -2
index 3e1384a6..464745b 100644
@@ -4542,6 +4542,12 @@ void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
  * As being wrong occasionally doesn't matter, updates and accesses to the
  * records are lockless and racy.
  */
+
+static inline bool foreign_writeback_in_process(struct memcg_cgwb_frn *frn)
+{
+	return atomic_read(&frn->done.cnt) != 1;
+}
+
 void mem_cgroup_track_foreign_dirty_slowpath(struct page *page,
 					     struct bdi_writeback *wb)
 {
@@ -4565,7 +4571,7 @@ void mem_cgroup_track_foreign_dirty_slowpath(struct page *page,
 		    frn->memcg_id == wb->memcg_css->id)
 			break;
 		if (time_before64(frn->at, oldest_at) &&
-		    atomic_read(&frn->done.cnt) == 1) {
+		    !foreign_writeback_in_process(frn)) {
 			oldest = i;
 			oldest_at = frn->at;
 		}
@@ -4612,7 +4618,7 @@ void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
 		 * already one in flight.
 		 */
 		if (time_after64(frn->at, now - intv) &&
-		    atomic_read(&frn->done.cnt) == 1) {
+		    !foreign_writeback_in_process(frn)) {
 			frn->at = 0;
 			trace_flush_foreign(wb, frn->bdi_id, frn->memcg_id);
 			cgroup_writeback_by_id(frn->bdi_id, frn->memcg_id,
Signature · 1 line
1.8.3.1
#2 lore raw
In reply to brookxu
Quoted 9 lines · brookxu

From: Chunguang Xu <[email protected]>

In order to track inflight foreign writeback, we init wb_completion.cnt to 1. For normal writeback, this cause wb_wait_for_completion() to perform meaningless atomic operations. Since foreign writebacks rarely occur in most scenarios, we can init wb_completion.cnt to 0 and set frn.done.cnt to 1. In this way we can avoid unnecessary atomic operations.

Does this lead to any measurable differences?

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#3 lore raw

Thanks for your time.

Michal Hocko wrote on 2021/9/24 17:34:

Quoted 11 lines · brookxu
Quoted 9 lines

From: Chunguang Xu <[email protected]>

In order to track inflight foreign writeback, we init wb_completion.cnt to 1. For normal writeback, this cause wb_wait_for_completion() to perform meaningless atomic operations. Since foreign writebacks rarely occur in most scenarios, we can init wb_completion.cnt to 0 and set frn.done.cnt to 1. In this way we can avoid unnecessary atomic operations.

Quoted 1 line

Does this lead to any measurable differences?

I created multiple cgroups that performed IO on multiple disks, then flushed the cache with sync command, and no measurable differences have been observed so far.

Quoted 1 line
#4 lore raw
Quoted 14 lines · brookxu

Thanks for your time.

Michal Hocko wrote on 2021/9/24 17:34:

Quoted 11 lines · brookxu
Quoted 9 lines

From: Chunguang Xu <[email protected]>

In order to track inflight foreign writeback, we init wb_completion.cnt to 1. For normal writeback, this cause wb_wait_for_completion() to perform meaningless atomic operations. Since foreign writebacks rarely occur in most scenarios, we can init wb_completion.cnt to 0 and set frn.done.cnt to 1. In this way we can avoid unnecessary atomic operations.

Quoted 5 lines
Quoted 1 line

Does this lead to any measurable differences?

I created multiple cgroups that performed IO on multiple disks, then flushed the cache with sync command, and no measurable differences have been observed so far.

OK, so why do we want to optimize this code?

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#5 lore raw

Thanks for your time.

Quoted 15 lines · Michal Hocko
Quoted 14 lines · brookxu

Thanks for your time.

Michal Hocko wrote on 2021/9/24 17:34:

Quoted 11 lines · brookxu
Quoted 9 lines

From: Chunguang Xu <[email protected]>

In order to track inflight foreign writeback, we init wb_completion.cnt to 1. For normal writeback, this cause wb_wait_for_completion() to perform meaningless atomic operations. Since foreign writebacks rarely occur in most scenarios, we can init wb_completion.cnt to 0 and set frn.done.cnt to 1. In this way we can avoid unnecessary atomic operations.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Does this lead to any measurable differences?

I created multiple cgroups that performed IO on multiple disks, then flushed the cache with sync command, and no measurable differences have been observed so far.

OK, so why do we want to optimize this code?

Just a optimization point discovered during the diagnosis, no behavior change.

Quoted 1 line