Skip to contentSkip to conversation

mm/list_lru: Optimize memcg_reparent_list_lru_node()

PATCH Superseded Waiman Long 2 participants 5 messages Mar 9, 2022

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm/list_lru: Optimize memcg_reparent_list_lru_node()

Since commit 2c80cd57c743 ("mm/list_lru.c: fix list_lru_count_node() to be race free"), we are tracking the total number of lru entries in a list_lru_node in its nr_items field. In the case of memcg_reparent_list_lru_node(), there is nothing to be done if nr_items is 0. We don't even need to take the nlru->lock as no new lru entry could be added by a racing list_lru_add() to the draining src_idx memcg at this point.

---
 mm/list_lru.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/list_lru.c +6 -0
index ba76428ceece..c669d87001a6 100644
@@ -394,6 +394,12 @@ static void memcg_reparent_list_lru_node(struct list_lru *lru, int nid,
 	int dst_idx = dst_memcg->kmemcg_id;
 	struct list_lru_one *src, *dst;
 
+	/*
+	 * If there is no lru entry in this nlru, we can skip it immediately.
+	 */
+	if (!READ_ONCE(nlru->nr_items))
+		return;
+
 	/*
 	 * Since list_lru_{add,del} may be called under an IRQ-safe lock,
 	 * we have to use IRQ-safe primitives here to avoid deadlock.
Signature · 1 line
2.27.0
#1 lore raw
Quoted 18 lines · Waiman Long

Since commit 2c80cd57c743 ("mm/list_lru.c: fix list_lru_count_node() to be race free"), we are tracking the total number of lru entries in a list_lru_node in its nr_items field. In the case of memcg_reparent_list_lru_node(), there is nothing to be done if nr_items is 0. We don't even need to take the nlru->lock as no new lru entry could be added by a racing list_lru_add() to the draining src_idx memcg at this point.

---
 mm/list_lru.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/list_lru.c +0 -0
index ba76428ceece..c669d87001a6 100644

mm/list_lru.c static void memcg_reparent_list_lru_node(struct list_lru *lru, int nid, · line 401

Quoted 8 lines
mm/list_lru.c +5 -0
@@ -394,6 +394,12 @@ static void memcg_reparent_list_lru_node(struct list_lru *lru, int nid,
 	int dst_idx = dst_memcg->kmemcg_id;
 	struct list_lru_one *src, *dst;
 
+	/*
+	 * If there is no lru entry in this nlru, we can skip it immediately.
+	 */
+	if (!READ_ONCE(nlru->nr_items))
+		return;

This is a per-node counter, not a per-memcg, right?
If so, do we optimize for the case when all lru items belong to one node and others are empty?

#2 lore raw
Quoted 22 lines · Roman Gushchin
Quoted 21 lines · Waiman Long

Since commit 2c80cd57c743 ("mm/list_lru.c: fix list_lru_count_node() to be race free"), we are tracking the total number of lru entries in a list_lru_node in its nr_items field. In the case of memcg_reparent_list_lru_node(), there is nothing to be done if nr_items is 0. We don't even need to take the nlru->lock as no new lru entry could be added by a racing list_lru_add() to the draining src_idx memcg at this point.

---
  mm/list_lru.c | 6 ++++++
  1 file changed, 6 insertions(+)
mm/list_lru.c +0 -0
index ba76428ceece..c669d87001a6 100644
@@ -394,6 +394,12 @@ static void memcg_reparent_list_lru_node(struct list_lru *lru, int nid,
  	int dst_idx = dst_memcg->kmemcg_id;
  	struct list_lru_one *src, *dst;
 
Quoted 6 lines

mm/list_lru.c static void memcg_reparent_list_lru_node(struct list_lru *lru, int nid, · line 401

Quoted 5 lines
mm/list_lru.c +5 -0
+	/*
+	 * If there is no lru entry in this nlru, we can skip it immediately.
+	 */
+	if (!READ_ONCE(nlru->nr_items))
+		return;

This is a per-node counter, not a per-memcg, right?

Right. list_lru_node is a per-node structure inside list_lru.

Quoted 2 lines

If so, do we optimize for the case when all lru items belong to one node and others are empty?

That is actually the case that I am trying to optimize for.

If a system has many containers. It is also likely each container may mount one or more container specific filesystems. Since a container likely use just a few cpus, it is highly that only the list_lru_node that contains those cpus will be utilized while the rests may be empty.

I got the idea of doing this patch when I was looking at a crash dump related to the list_lru code. That particular crash dump has more than 13k list_lru's and thousands of mount points. I had notice even if nr_items of a list_lru_node is 0, it still tries to transfer lru entries from source idx to dest idx. Without doing an lock/unlock and loading a cacheline from the memcg_lrus, it can save some time. That can be substantial saving if we are talking about thousands of list_lru's.

Cheers,
Longman

reviewer Reviewed-by
#3 lore raw
Quoted 39 lines · Waiman Long
Quoted 14 lines · Roman Gushchin
Quoted 13 lines · Waiman Long

Since commit 2c80cd57c743 ("mm/list_lru.c: fix list_lru_count_node() to be race free"), we are tracking the total number of lru entries in a list_lru_node in its nr_items field. In the case of memcg_reparent_list_lru_node(), there is nothing to be done if nr_items is 0. We don't even need to take the nlru->lock as no new lru entry could be added by a racing list_lru_add() to the draining src_idx memcg at this point.

---
  mm/list_lru.c | 6 ++++++
  1 file changed, 6 insertions(+)
Quoted 13 lines

mm/list_lru.c static void memcg_reparent_list_lru_node(struct list_lru *lru, int nid, · line 400

Quoted 12 lines
mm/list_lru.c +5 -0
index ba76428ceece..c669d87001a6 100644
@@ -394,6 +394,12 @@ static void memcg_reparent_list_lru_node(struct list_lru *lru, int nid,
  	int dst_idx = dst_memcg->kmemcg_id;
  	struct list_lru_one *src, *dst;
+	/*
+	 * If there is no lru entry in this nlru, we can skip it immediately.
+	 */
+	if (!READ_ONCE(nlru->nr_items))
+		return;

This is a per-node counter, not a per-memcg, right?

Right. list_lru_node is a per-node structure inside list_lru.

Quoted 2 lines

If so, do we optimize for the case when all lru items belong to one node and others are empty?

That is actually the case that I am trying to optimize for.

If a system has many containers. It is also likely each container may mount one or more container specific filesystems. Since a container likely use just a few cpus, it is highly that only the list_lru_node that contains those cpus will be utilized while the rests may be empty.

Quoted 7 lines

I got the idea of doing this patch when I was looking at a crash dump related to the list_lru code. That particular crash dump has more than 13k list_lru's and thousands of mount points. I had notice even if nr_items of a list_lru_node is 0, it still tries to transfer lru entries from source idx to dest idx. Without doing an lock/unlock and loading a cacheline from the memcg_lrus, it can save some time. That can be substantial saving if we are talking about thousands of list_lru's.

Cool! Makes total sense to me. Thanks for the explanation! Would you mind to add this text to the commit log?

Thanks!

#4 lore raw
Quoted 46 lines · Roman Gushchin
Quoted 38 lines · Waiman Long
Quoted 14 lines · Roman Gushchin
Quoted 13 lines · Waiman Long

Since commit 2c80cd57c743 ("mm/list_lru.c: fix list_lru_count_node() to be race free"), we are tracking the total number of lru entries in a list_lru_node in its nr_items field. In the case of memcg_reparent_list_lru_node(), there is nothing to be done if nr_items is 0. We don't even need to take the nlru->lock as no new lru entry could be added by a racing list_lru_add() to the draining src_idx memcg at this point.

---
   mm/list_lru.c | 6 ++++++
   1 file changed, 6 insertions(+)
Quoted 13 lines

mm/list_lru.c static void memcg_reparent_list_lru_node(struct list_lru *lru, int nid, · line 400

Quoted 12 lines
mm/list_lru.c +5 -0
index ba76428ceece..c669d87001a6 100644
@@ -394,6 +394,12 @@ static void memcg_reparent_list_lru_node(struct list_lru *lru, int nid,
   	int dst_idx = dst_memcg->kmemcg_id;
   	struct list_lru_one *src, *dst;
+	/*
+	 * If there is no lru entry in this nlru, we can skip it immediately.
+	 */
+	if (!READ_ONCE(nlru->nr_items))
+		return;

This is a per-node counter, not a per-memcg, right?

Right. list_lru_node is a per-node structure inside list_lru.

Quoted 2 lines

If so, do we optimize for the case when all lru items belong to one node and others are empty?

That is actually the case that I am trying to optimize for.

If a system has many containers. It is also likely each container may mount one or more container specific filesystems. Since a container likely use just a few cpus, it is highly that only the list_lru_node that contains those cpus will be utilized while the rests may be empty.

Quoted 7 lines

I got the idea of doing this patch when I was looking at a crash dump related to the list_lru code. That particular crash dump has more than 13k list_lru's and thousands of mount points. I had notice even if nr_items of a list_lru_node is 0, it still tries to transfer lru entries from source idx to dest idx. Without doing an lock/unlock and loading a cacheline from the memcg_lrus, it can save some time. That can be substantial saving if we are talking about thousands of list_lru's.

Quoted 4 lines

Cool! Makes total sense to me. Thanks for the explanation! Would you mind to add this text to the commit log?

Sure. I will add some of it into the commit log.

Cheers,
Longman