Skip to contentSkip to conversation

mm: memcontrol: use per-cpu stocks for socket memory uncharging

PATCH Applied Roman Gushchin 4 participants 13 messages Aug 29, 2017 – Sep 7, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: memcontrol: use per-cpu stocks for socket memory uncharging

We've noticed a quite sensible performance overhead on some hosts with significant network traffic when socket memory accounting is enabled.

Perf top shows that socket memory uncharging path is hot:

  2.13%  [kernel]                [k] page_counter_cancel
  1.14%  [kernel]                [k] __sk_mem_reduce_allocated
  1.14%  [kernel]                [k] _raw_spin_lock
  0.87%  [kernel]                [k] _raw_spin_lock_irqsave
  0.84%  [kernel]                [k] tcp_ack
  0.84%  [kernel]                [k] ixgbe_poll
  0.83%  < workload >
  0.82%  [kernel]                [k] enqueue_entity
  0.68%  [kernel]                [k] __fget
  0.68%  [kernel]                [k] tcp_delack_timer_handler
  0.67%  [kernel]                [k] __schedule
  0.60%  < workload >
  0.59%  [kernel]                [k] __inet6_lookup_established
  0.55%  [kernel]                [k] __switch_to
  0.55%  [kernel]                [k] menu_select
  0.54%  libc-2.20.so            [.] __memcpy_avx_unaligned

To address this issue, the existing per-cpu stock infrastructure can be used.

refill_stock() can be called from mem_cgroup_uncharge_skmem() to move charge to a per-cpu stock instead of calling atomic page_counter_uncharge().

To prevent the uncontrolled growth of per-cpu stocks, refill_stock() will explicitly drain the cached charge, if the cached value exceeds CHARGE_BATCH.

This allows significantly optimize the load:

  1.21%  [kernel]                [k] _raw_spin_lock
  1.01%  [kernel]                [k] ixgbe_poll
  0.92%  [kernel]                [k] _raw_spin_lock_irqsave
  0.90%  [kernel]                [k] enqueue_entity
  0.86%  [kernel]                [k] tcp_ack
  0.85%  < workload >
  0.74%  perf-11120.map          [.] 0x000000000061bf24
  0.73%  [kernel]                [k] __schedule
  0.67%  [kernel]                [k] __fget
  0.63%  [kernel]                [k] __inet6_lookup_established
  0.62%  [kernel]                [k] menu_select
  0.59%  < workload >
  0.59%  [kernel]                [k] __switch_to
  0.57%  libc-2.20.so            [.] __memcpy_avx_unaligned
---
 mm/memcontrol.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
mm/memcontrol.c +4 -2
index b9cf3cf4a3d0..a69d23082abf 100644
@@ -1792,6 +1792,9 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
 	}
 	stock->nr_pages += nr_pages;
 
+	if (stock->nr_pages > CHARGE_BATCH)
+		drain_stock(stock);
+
 	local_irq_restore(flags);
 }
 
@@ -5886,8 +5889,7 @@ void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
 
 	this_cpu_sub(memcg->stat->count[MEMCG_SOCK], nr_pages);
 
-	page_counter_uncharge(&memcg->memory, nr_pages);
-	css_put_many(&memcg->css, nr_pages);
+	refill_stock(memcg, nr_pages);
 }
 
 static int __init cgroup_memory(char *s)
Signature · 1 line
2.13.5
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 49 lines · Roman Gushchin

We've noticed a quite sensible performance overhead on some hosts with significant network traffic when socket memory accounting is enabled.

Perf top shows that socket memory uncharging path is hot:

  2.13%  [kernel]                [k] page_counter_cancel
  1.14%  [kernel]                [k] __sk_mem_reduce_allocated
  1.14%  [kernel]                [k] _raw_spin_lock
  0.87%  [kernel]                [k] _raw_spin_lock_irqsave
  0.84%  [kernel]                [k] tcp_ack
  0.84%  [kernel]                [k] ixgbe_poll
  0.83%  < workload >
  0.82%  [kernel]                [k] enqueue_entity
  0.68%  [kernel]                [k] __fget
  0.68%  [kernel]                [k] tcp_delack_timer_handler
  0.67%  [kernel]                [k] __schedule
  0.60%  < workload >
  0.59%  [kernel]                [k] __inet6_lookup_established
  0.55%  [kernel]                [k] __switch_to
  0.55%  [kernel]                [k] menu_select
  0.54%  libc-2.20.so            [.] __memcpy_avx_unaligned

To address this issue, the existing per-cpu stock infrastructure can be used.

refill_stock() can be called from mem_cgroup_uncharge_skmem() to move charge to a per-cpu stock instead of calling atomic page_counter_uncharge().

To prevent the uncontrolled growth of per-cpu stocks, refill_stock() will explicitly drain the cached charge, if the cached value exceeds CHARGE_BATCH.

This allows significantly optimize the load:

  1.21%  [kernel]                [k] _raw_spin_lock
  1.01%  [kernel]                [k] ixgbe_poll
  0.92%  [kernel]                [k] _raw_spin_lock_irqsave
  0.90%  [kernel]                [k] enqueue_entity
  0.86%  [kernel]                [k] tcp_ack
  0.85%  < workload >
  0.74%  perf-11120.map          [.] 0x000000000061bf24
  0.73%  [kernel]                [k] __schedule
  0.67%  [kernel]                [k] __fget
  0.63%  [kernel]                [k] __inet6_lookup_established
  0.62%  [kernel]                [k] menu_select
  0.59%  < workload >
  0.59%  [kernel]                [k] __switch_to
  0.57%  libc-2.20.so            [.] __memcpy_avx_unaligned
Quoted 8 lines

Neat!

As far as other types of pages go: page cache and anon are already batched pretty well, but I think kmem might benefit from this too. Have you considered using the stock in memcg_kmem_uncharge()?

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

#2 lore raw
Quoted 59 lines · Johannes Weiner
Quoted 49 lines · Roman Gushchin

We've noticed a quite sensible performance overhead on some hosts with significant network traffic when socket memory accounting is enabled.

Perf top shows that socket memory uncharging path is hot:

  2.13%  [kernel]                [k] page_counter_cancel
  1.14%  [kernel]                [k] __sk_mem_reduce_allocated
  1.14%  [kernel]                [k] _raw_spin_lock
  0.87%  [kernel]                [k] _raw_spin_lock_irqsave
  0.84%  [kernel]                [k] tcp_ack
  0.84%  [kernel]                [k] ixgbe_poll
  0.83%  < workload >
  0.82%  [kernel]                [k] enqueue_entity
  0.68%  [kernel]                [k] __fget
  0.68%  [kernel]                [k] tcp_delack_timer_handler
  0.67%  [kernel]                [k] __schedule
  0.60%  < workload >
  0.59%  [kernel]                [k] __inet6_lookup_established
  0.55%  [kernel]                [k] __switch_to
  0.55%  [kernel]                [k] menu_select
  0.54%  libc-2.20.so            [.] __memcpy_avx_unaligned

To address this issue, the existing per-cpu stock infrastructure can be used.

refill_stock() can be called from mem_cgroup_uncharge_skmem() to move charge to a per-cpu stock instead of calling atomic page_counter_uncharge().

To prevent the uncontrolled growth of per-cpu stocks, refill_stock() will explicitly drain the cached charge, if the cached value exceeds CHARGE_BATCH.

This allows significantly optimize the load:

  1.21%  [kernel]                [k] _raw_spin_lock
  1.01%  [kernel]                [k] ixgbe_poll
  0.92%  [kernel]                [k] _raw_spin_lock_irqsave
  0.90%  [kernel]                [k] enqueue_entity
  0.86%  [kernel]                [k] tcp_ack
  0.85%  < workload >
  0.74%  perf-11120.map          [.] 0x000000000061bf24
  0.73%  [kernel]                [k] __schedule
  0.67%  [kernel]                [k] __fget
  0.63%  [kernel]                [k] __inet6_lookup_established
  0.62%  [kernel]                [k] menu_select
  0.59%  < workload >
  0.59%  [kernel]                [k] __switch_to
  0.57%  libc-2.20.so            [.] __memcpy_avx_unaligned
Quoted 8 lines
Quoted 7 lines

Neat!

As far as other types of pages go: page cache and anon are already batched pretty well, but I think kmem might benefit from this too. Have you considered using the stock in memcg_kmem_uncharge()?

Good idea!
I'll try to find an appropriate testcase and check if it really brings any benefits. If so, I'll master a patch.

Thanks!

#3 lore raw
Quoted 8 lines
Quoted 3 lines

As far as other types of pages go: page cache and anon are already batched pretty well, but I think kmem might benefit from this too. Have you considered using the stock in memcg_kmem_uncharge()?

Good idea!
I'll try to find an appropriate testcase and check if it really brings any benefits. If so, I'll master a patch.

Hi Roman, did you get the chance to try this on memcg_kmem_uncharge()?

#4 lore raw
Quoted 10 lines · Shakeel Butt
Quoted 8 lines
Quoted 3 lines

As far as other types of pages go: page cache and anon are already batched pretty well, but I think kmem might benefit from this too. Have you considered using the stock in memcg_kmem_uncharge()?

Good idea!
I'll try to find an appropriate testcase and check if it really brings any benefits. If so, I'll master a patch.

Hi Roman, did you get the chance to try this on memcg_kmem_uncharge()?

Hi Shakeel!

Not yet, I'll try to it asap.
Do you have an example when it's costly?

Thanks!

#5 lore raw
Quoted 10 lines · Roman Gushchin
Quoted 9 lines · Shakeel Butt
Quoted 8 lines
Quoted 3 lines

As far as other types of pages go: page cache and anon are already batched pretty well, but I think kmem might benefit from this too. Have you considered using the stock in memcg_kmem_uncharge()?

Good idea!
I'll try to find an appropriate testcase and check if it really brings any benefits. If so, I'll master a patch.

Quoted 7 lines
Quoted 1 line

Hi Roman, did you get the chance to try this on memcg_kmem_uncharge()?

Hi Shakeel!

Not yet, I'll try to it asap.
Do you have an example when it's costly?

Nah, I was also curious of such examples.

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

#6 lore raw
In reply to Johannes Weiner
Quoted 59 lines · Johannes Weiner
Quoted 49 lines · Roman Gushchin

We've noticed a quite sensible performance overhead on some hosts with significant network traffic when socket memory accounting is enabled.

Perf top shows that socket memory uncharging path is hot:

  2.13%  [kernel]                [k] page_counter_cancel
  1.14%  [kernel]                [k] __sk_mem_reduce_allocated
  1.14%  [kernel]                [k] _raw_spin_lock
  0.87%  [kernel]                [k] _raw_spin_lock_irqsave
  0.84%  [kernel]                [k] tcp_ack
  0.84%  [kernel]                [k] ixgbe_poll
  0.83%  < workload >
  0.82%  [kernel]                [k] enqueue_entity
  0.68%  [kernel]                [k] __fget
  0.68%  [kernel]                [k] tcp_delack_timer_handler
  0.67%  [kernel]                [k] __schedule
  0.60%  < workload >
  0.59%  [kernel]                [k] __inet6_lookup_established
  0.55%  [kernel]                [k] __switch_to
  0.55%  [kernel]                [k] menu_select
  0.54%  libc-2.20.so            [.] __memcpy_avx_unaligned

To address this issue, the existing per-cpu stock infrastructure can be used.

refill_stock() can be called from mem_cgroup_uncharge_skmem() to move charge to a per-cpu stock instead of calling atomic page_counter_uncharge().

To prevent the uncontrolled growth of per-cpu stocks, refill_stock() will explicitly drain the cached charge, if the cached value exceeds CHARGE_BATCH.

This allows significantly optimize the load:

  1.21%  [kernel]                [k] _raw_spin_lock
  1.01%  [kernel]                [k] ixgbe_poll
  0.92%  [kernel]                [k] _raw_spin_lock_irqsave
  0.90%  [kernel]                [k] enqueue_entity
  0.86%  [kernel]                [k] tcp_ack
  0.85%  < workload >
  0.74%  perf-11120.map          [.] 0x000000000061bf24
  0.73%  [kernel]                [k] __schedule
  0.67%  [kernel]                [k] __fget
  0.63%  [kernel]                [k] __inet6_lookup_established
  0.62%  [kernel]                [k] menu_select
  0.59%  < workload >
  0.59%  [kernel]                [k] __switch_to
  0.57%  libc-2.20.so            [.] __memcpy_avx_unaligned
Quoted 8 lines
Quoted 3 lines

Neat!

Hi, Andrew!

Can you, please, pull this patch?

Thank you!

Roman

maintainer
#7 lore raw
In reply to Roman Gushchin

On Tue 29-08-17 11:01:50, Roman Gushchin wrote:
[...]

mm/memcontrol.c static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) · line 1799

Quoted 13 lines
mm/memcontrol.c +3 -0
index b9cf3cf4a3d0..a69d23082abf 100644
@@ -1792,6 +1792,9 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
 	}
 	stock->nr_pages += nr_pages;
 
+	if (stock->nr_pages > CHARGE_BATCH)
+		drain_stock(stock);
+
 	local_irq_restore(flags);
 }

Why do we need this? In other words, why cannot we rely on draining we already do?

mm/memcontrol.c void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) · line 5893

Quoted 9 lines
mm/memcontrol.c +1 -2
@@ -5886,8 +5889,7 @@ void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
 
 	this_cpu_sub(memcg->stat->count[MEMCG_SOCK], nr_pages);
 
-	page_counter_uncharge(&memcg->memory, nr_pages);
-	css_put_many(&memcg->css, nr_pages);
+	refill_stock(memcg, nr_pages);
 }

This makes sense to me.

Signature · 2 lines
Michal Hocko
SUSE Labs
#8 lore raw
Quoted 10 lines · Michal Hocko

On Tue 29-08-17 11:01:50, Roman Gushchin wrote:
[...]

Quoted 8 lines
mm/memcontrol.c +0 -0
index b9cf3cf4a3d0..a69d23082abf 100644
@@ -1792,6 +1792,9 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
 	}
 	stock->nr_pages += nr_pages;
 
Quoted 8 lines

mm/memcontrol.c static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) · line 1799

Quoted 5 lines
mm/memcontrol.c +3 -0
+	if (stock->nr_pages > CHARGE_BATCH)
+		drain_stock(stock);
+
 	local_irq_restore(flags);
 }

Why do we need this? In other words, why cannot we rely on draining we already do?

The existing draining depends on memory pressure, so to keep the accounting (which we expose to a user) reasonable accurate even without memory pressure, we need to limit the size of per-cpu stocks.

Thanks!

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

maintainer
#9 lore raw
Quoted 20 lines · Roman Gushchin
Quoted 10 lines · Michal Hocko

On Tue 29-08-17 11:01:50, Roman Gushchin wrote:
[...]

Quoted 8 lines
mm/memcontrol.c +0 -0
index b9cf3cf4a3d0..a69d23082abf 100644
@@ -1792,6 +1792,9 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
 	}
 	stock->nr_pages += nr_pages;
 
Quoted 8 lines

mm/memcontrol.c static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) · line 1799

Quoted 5 lines
mm/memcontrol.c +3 -0
+	if (stock->nr_pages > CHARGE_BATCH)
+		drain_stock(stock);
+
 	local_irq_restore(flags);
 }

Why do we need this? In other words, why cannot we rely on draining we already do?

Quoted 3 lines

The existing draining depends on memory pressure, so to keep the accounting (which we expose to a user) reasonable accurate even without memory pressure, we need to limit the size of per-cpu stocks.

Why don't we need this for regular page charges? Or maybe we do but that sounds like a seprate and an unrealted fix to me.

Signature · 2 lines
Michal Hocko
SUSE Labs
#10 lore raw
Quoted 21 lines · Michal Hocko
Quoted 20 lines · Roman Gushchin
Quoted 18 lines · Michal Hocko

On Tue 29-08-17 11:01:50, Roman Gushchin wrote:
[...]

mm/memcontrol.c static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) · line 1799

Quoted 13 lines
mm/memcontrol.c +3 -0
index b9cf3cf4a3d0..a69d23082abf 100644
@@ -1792,6 +1792,9 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
 	}
 	stock->nr_pages += nr_pages;
 
+	if (stock->nr_pages > CHARGE_BATCH)
+		drain_stock(stock);
+
 	local_irq_restore(flags);
 }

Why do we need this? In other words, why cannot we rely on draining we already do?

Quoted 6 lines
Quoted 3 lines

The existing draining depends on memory pressure, so to keep the accounting (which we expose to a user) reasonable accurate even without memory pressure, we need to limit the size of per-cpu stocks.

Why don't we need this for regular page charges? Or maybe we do but that sounds like a seprate and an unrealted fix to me.

Because we never refill more than CHARGE_BATCH.

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

maintainer
#11 lore raw
Quoted 22 lines · Roman Gushchin
Quoted 21 lines · Michal Hocko
Quoted 20 lines · Roman Gushchin
Quoted 18 lines · Michal Hocko

On Tue 29-08-17 11:01:50, Roman Gushchin wrote:
[...]

mm/memcontrol.c static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) · line 1799

Quoted 13 lines
mm/memcontrol.c +3 -0
index b9cf3cf4a3d0..a69d23082abf 100644
@@ -1792,6 +1792,9 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
 	}
 	stock->nr_pages += nr_pages;
 
+	if (stock->nr_pages > CHARGE_BATCH)
+		drain_stock(stock);
+
 	local_irq_restore(flags);
 }

Why do we need this? In other words, why cannot we rely on draining we already do?

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

The existing draining depends on memory pressure, so to keep the accounting (which we expose to a user) reasonable accurate even without memory pressure, we need to limit the size of per-cpu stocks.

Why don't we need this for regular page charges? Or maybe we do but that sounds like a seprate and an unrealted fix to me.

Because we never refill more than CHARGE_BATCH.

You are right that a single process will not but try_charge is a preemptible context and so multiple processes might pass consume_stock and then charge a N*CHARGE_BATCH. But I agree that this is quite unlikely so a separate patch is probably not worth it.

Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer Acked-by on v1 1/1
#12 lore raw
In reply to Roman Gushchin
Quoted 49 lines · Roman Gushchin

We've noticed a quite sensible performance overhead on some hosts with significant network traffic when socket memory accounting is enabled.

Perf top shows that socket memory uncharging path is hot:

  2.13%  [kernel]                [k] page_counter_cancel
  1.14%  [kernel]                [k] __sk_mem_reduce_allocated
  1.14%  [kernel]                [k] _raw_spin_lock
  0.87%  [kernel]                [k] _raw_spin_lock_irqsave
  0.84%  [kernel]                [k] tcp_ack
  0.84%  [kernel]                [k] ixgbe_poll
  0.83%  < workload >
  0.82%  [kernel]                [k] enqueue_entity
  0.68%  [kernel]                [k] __fget
  0.68%  [kernel]                [k] tcp_delack_timer_handler
  0.67%  [kernel]                [k] __schedule
  0.60%  < workload >
  0.59%  [kernel]                [k] __inet6_lookup_established
  0.55%  [kernel]                [k] __switch_to
  0.55%  [kernel]                [k] menu_select
  0.54%  libc-2.20.so            [.] __memcpy_avx_unaligned

To address this issue, the existing per-cpu stock infrastructure can be used.

refill_stock() can be called from mem_cgroup_uncharge_skmem() to move charge to a per-cpu stock instead of calling atomic page_counter_uncharge().

To prevent the uncontrolled growth of per-cpu stocks, refill_stock() will explicitly drain the cached charge, if the cached value exceeds CHARGE_BATCH.

This allows significantly optimize the load:

  1.21%  [kernel]                [k] _raw_spin_lock
  1.01%  [kernel]                [k] ixgbe_poll
  0.92%  [kernel]                [k] _raw_spin_lock_irqsave
  0.90%  [kernel]                [k] enqueue_entity
  0.86%  [kernel]                [k] tcp_ack
  0.85%  < workload >
  0.74%  perf-11120.map          [.] 0x000000000061bf24
  0.73%  [kernel]                [k] __schedule
  0.67%  [kernel]                [k] __fget
  0.63%  [kernel]                [k] __inet6_lookup_established
  0.62%  [kernel]                [k] menu_select
  0.59%  < workload >
  0.59%  [kernel]                [k] __switch_to
  0.57%  libc-2.20.so            [.] __memcpy_avx_unaligned
Quoted 8 lines
Quoted 30 lines
---
 mm/memcontrol.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
mm/memcontrol.c +4 -2
index b9cf3cf4a3d0..a69d23082abf 100644
@@ -1792,6 +1792,9 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
 	}
 	stock->nr_pages += nr_pages;
 
+	if (stock->nr_pages > CHARGE_BATCH)
+		drain_stock(stock);
+
 	local_irq_restore(flags);
 }
 
@@ -5886,8 +5889,7 @@ void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
 
 	this_cpu_sub(memcg->stat->count[MEMCG_SOCK], nr_pages);
 
-	page_counter_uncharge(&memcg->memory, nr_pages);
-	css_put_many(&memcg->css, nr_pages);
+	refill_stock(memcg, nr_pages);
 }
 
 static int __init cgroup_memory(char *s)
Signature · 1 line
2.13.5
Signature · 2 lines
Michal Hocko
SUSE Labs