memcg: optimizatize charge codepath
Subsystems (6)
cover memcg: optimizatize charge codepath
Recently Linux networking stack has moved from a very old per socket pre-charge caching to per-cpu caching to avoid pre-charge fragmentation and unwarranted OOMs. One impact of this change is that for network traffic workloads, memcg charging codepath can become a bottleneck. The kernel test robot has also reported this regression. This patch series tries to improve the memcg charging for such workloads.
This patch series implement three optimizations:
(A) Reduce atomic ops in page counter update path.
(B) Change layout of struct page_counter to eliminate false sharing between usage and high.
(C) Increase the memcg charge batch to 64.
To evaluate the impact of these optimizations, on a 72 CPUs machine, we ran the following workload in root memcg and then compared with scenario where the workload is run in a three level of cgroup hierarchy with top level having min and low setup appropriately.
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K
Results (average throughput of netperf):
1. root memcg 21694.8
2. 6.0-rc1 10482.7 (-51.6%)
3. 6.0-rc1 + (A) 14542.5 (-32.9%)
4. 6.0-rc1 + (B) 12413.7 (-42.7%)
5. 6.0-rc1 + (C) 17063.7 (-21.3%)
6. 6.0-rc1 + (A+B+C) 20120.3 (-7.2%)
With all three optimizations, the memcg overhead of this workload has been reduced from 51.6% to just 7.2%.
Shakeel Butt (3):
mm: page_counter: remove unneeded atomic ops for low/min
mm: page_counter: rearrange struct page_counter fields
memcg: increase MEMCG_CHARGE_BATCH to 64
include/linux/memcontrol.h | 7 ++++--- include/linux/page_counter.h | 34 +++++++++++++++++++++++----------- mm/page_counter.c | 13 ++++++------- 3 files changed, 33 insertions(+), 21 deletions(-)
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
PATCH 1/3 mm: page_counter: remove unneeded atomic ops for low/min
For cgroups using low or min protections, the function propagate_protected_usage() was doing an atomic xchg() operation irrespectively. It only needs to do that operation if the new value of protection is different from older one. This patch does that.
To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K
Results (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 14542.5 Mbps (38.7% improvement)
With the patch, the throughput improved by 38.7%
--- mm/page_counter.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
mm/page_counter.c +6 -7
@@ -17,24 +17,23 @@ static void propagate_protected_usage(struct page_counter *c, unsigned long usage) {unsigned long protected, old_protected; -unsigned long low, min;long delta;if (!c->parent)return ; - min =READ_ONCE (c->min); -if (min ||atomic_long_read (&c->min_usage)) { - protected =min (usage, min); + protected =min (usage,READ_ONCE (c->min)); + old_protected =atomic_long_read (&c->min_usage); +if (protected != old_protected) { old_protected =atomic_long_xchg (&c->min_usage, protected); delta = protected - old_protected;if (delta)atomic_long_add (delta, &c->parent->children_min_usage); } - low =READ_ONCE (c->low); -if (low ||atomic_long_read (&c->low_usage)) { - protected =min (usage, low); + protected =min (usage,READ_ONCE (c->low)); + old_protected =atomic_long_read (&c->low_usage); +if (protected != old_protected) { old_protected =atomic_long_xchg (&c->low_usage, protected); delta = protected - old_protected;if (delta)
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
PATCH 2/3 mm: page_counter: rearrange struct page_counter fields
In reply to Shakeel ButtWith memcg v2 enabled, memcg->memory.usage is a very hot member for the workloads doing memcg charging on multiple CPUs concurrently. Particularly the network intensive workloads. In addition, there is a false cache sharing between memory.usage and memory.high on the charge path. This patch moves the usage into a separate cacheline and move all the read most fields into separate cacheline.
To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K
Results (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 12413.7 Mbps (18.4% improvement)
With the patch, the throughput improved by 18.4%.
One side-effect of this patch is the increase in the size of struct mem_cgroup. However for the performance improvement, this additional size is worth it. In addition there are opportunities to reduce the size of struct mem_cgroup like deprecation of kmem and tcpmem page counters and better packing.
--- include/linux/page_counter.h | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-)
include/linux/page_counter.h +23 -11
@@ -3,15 +3,27 @@#define _LINUX_PAGE_COUNTER_H#include <linux/atomic.h> +#include <linux/cache.h> #include <linux/kernel.h> #include <asm/page.h> +#if defined(CONFIG_SMP ) +struct pc_padding { +char x[0 ]; +}____cacheline_internodealigned_in_smp ; +#define PC_PADDING (name) struct pc_padding name +#else +#define PC_PADDING (name) +#endif +struct page_counter { +/* +* Make sure 'usage' does not share cacheline with any other field. The +* memcg->memory.usage is a hot member of struct mem_cgroup. +*/ +PC_PADDING (_pad1_);atomic_long_t usage; -unsigned long min; -unsigned long low; -unsigned long high; -unsigned long max; +PC_PADDING (_pad2_);/* effective memory.min and memory.min usage tracking */ unsigned long emin;
@@ -23,16 +35,16 @@ struct page_counter {atomic_long_t low_usage;atomic_long_t children_low_usage; -/* legacy */ unsigned long watermark;unsigned long failcnt; -/* -* 'parent' is placed here to be far from 'usage' to reduce -* cache false sharing, as 'usage' is written mostly while -* parent is frequently read for cgroup's hierarchical -* counting nature. -*/ +/* Keep all the read most fields in a separete cacheline. */ +PC_PADDING (_pad3_); + +unsigned long min; +unsigned long low; +unsigned long high; +unsigned long max;struct page_counter *parent; };
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
PATCH 3/3 memcg: increase MEMCG_CHARGE_BATCH to 64
In reply to Shakeel ButtFor several years, MEMCG_CHARGE_BATCH was kept at 32 but with bigger machines and the network intensive workloads requiring througput in Gbps, 32 is too small and makes the memcg charging path a bottleneck. For now, increase it to 64 for easy acceptance to 6.0. We will need to revisit this in future for ever increasing demand of higher performance.
Please note that the memcg charge path drain the per-cpu memcg charge stock, so there should not be any oom behavior change.
To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K
Results (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 17064.7 Mbps (62.7% improvement)
With the patch, the throughput improved by 62.7%.
--- include/linux/memcontrol.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
include/linux/memcontrol.h +4 -3
@@ -354,10 +354,11 @@ struct mem_cgroup { };/* -* size of first charge trial. "32" comes from vmscan.c's magic value. -* TODO: maybe necessary to use big numbers in big irons. +* size of first charge trial. +* TODO: maybe necessary to use big numbers in big irons or dynamic based of the +* workload. */ -#define MEMCG_CHARGE_BATCH 32U +#define MEMCG_CHARGE_BATCH 64Uextern struct mem_cgroup *root_mem_cgroup;
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
Re: [PATCH 1/3] mm: page_counter: remove unneeded atomic ops for low/min
In reply to Shakeel ButtQuoted 16 lines · Shakeel Butt
For cgroups using low or min protections, the function
propagate_protected_usage()was doing an atomicxchg()operation irrespectively. It only needs to do that operation if the new value of protection is different from older one. This patch does that.To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K
Quoted 8 lines
Results (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 14542.5 Mbps (38.7% improvement)With the patch, the throughput improved by 38.7%
Signed-off-by Shakeel Butt <[email protected]>Reported-by kernel test robot <[email protected]>
Nice speed up!
Quoted 42 lines
--- mm/page_counter.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)mm/page_counter.c +6
-8@@ -17,24 +17,23 @@ static void propagate_protected_usage(struct page_counter *c, unsigned long usage) {unsigned long protected, old_protected; -unsigned long low, min;long delta;if (!c->parent)return ; - min =READ_ONCE (c->min); -if (min ||atomic_long_read (&c->min_usage)) { - protected =min (usage, min); + protected =min (usage,READ_ONCE (c->min)); + old_protected =atomic_long_read (&c->min_usage); +if (protected != old_protected) { old_protected =atomic_long_xchg (&c->min_usage, protected); delta = protected - old_protected;if (delta)atomic_long_add (delta, &c->parent->children_min_usage); } - low =READ_ONCE (c->low); -if (low ||atomic_long_read (&c->low_usage)) { - protected =min (usage, low); + protected =min (usage,READ_ONCE (c->low)); + old_protected =atomic_long_read (&c->low_usage); +if (protected != old_protected) { old_protected =atomic_long_xchg (&c->low_usage, protected); delta = protected - old_protected;if (delta) --2.37.1.595.g718a3a8f04-goog
Re: [PATCH 2/3] mm: page_counter: rearrange struct page_counter fields
In reply to Shakeel ButtQuoted 18 lines · Shakeel Butt
With memcg v2 enabled,
memcg->memory.usageis a very hot member for the workloads doing memcg charging on multiple CPUs concurrently. Particularly the network intensive workloads. In addition, there is a false cache sharing between memory.usage and memory.high on the charge path. This patch moves the usage into a separate cacheline and move all the read most fields into separate cacheline.To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K
Quoted 5 lines
Results (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 12413.7 Mbps (18.4% improvement)With the patch, the throughput improved by 18.4%.
Shakeel, for my understanding: is this on top of the gains from the previous patch?
Quoted 75 lines
One side-effect of this patch is the increase in the size of struct
mem_cgroup. However for the performance improvement, this additional size is worth it. In addition there are opportunities to reduce the size of structmem_cgrouplike deprecation of kmem and tcpmem page counters and better packing.Signed-off-by Shakeel Butt <[email protected]>Reported-by kernel test robot <[email protected]>--- include/linux/page_counter.h | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-)include/linux/page_counter.h +23
-12@@ -3,15 +3,27 @@#define _LINUX_PAGE_COUNTER_H#include <linux/atomic.h> +#include <linux/cache.h> #include <linux/kernel.h> #include <asm/page.h> +#if defined(CONFIG_SMP ) +struct pc_padding { +char x[0 ]; +}____cacheline_internodealigned_in_smp ; +#define PC_PADDING (name) struct pc_padding name +#else +#define PC_PADDING (name) +#endif +struct page_counter { +/* +* Make sure 'usage' does not share cacheline with any other field. The +* memcg->memory.usage is a hot member of struct mem_cgroup. +*/ +PC_PADDING (_pad1_);atomic_long_t usage; -unsigned long min; -unsigned long low; -unsigned long high; -unsigned long max; +PC_PADDING (_pad2_);/* effective memory.min and memory.min usage tracking */ unsigned long emin;@@ -23,16 +35,16 @@ struct page_counter {atomic_long_t low_usage;atomic_long_t children_low_usage; -/* legacy */ unsigned long watermark;unsigned long failcnt; -/* -* 'parent' is placed here to be far from 'usage' to reduce -* cache false sharing, as 'usage' is written mostly while -* parent is frequently read for cgroup's hierarchical -* counting nature. -*/ +/* Keep all the read most fields in a separete cacheline. */ +PC_PADDING (_pad3_); + +unsigned long min; +unsigned long low; +unsigned long high; +unsigned long max;struct page_counter *parent; }; --2.37.1.595.g718a3a8f04-goog
Re: [PATCH 3/3] memcg: increase MEMCG_CHARGE_BATCH to 64
In reply to Shakeel ButtQuoted 20 lines · Shakeel Butt
For several years,
MEMCG_CHARGE_BATCHwas kept at 32 but with bigger machines and the network intensive workloads requiring througput in Gbps, 32 is too small and makes the memcg charging path a bottleneck. For now, increase it to 64 for easy acceptance to 6.0. We will need to revisit this in future for ever increasing demand of higher performance.Please note that the memcg charge path drain the per-cpu memcg charge stock, so there should not be any oom behavior change.
To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K
Quoted 8 lines
Results (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 17064.7 Mbps (62.7% improvement)With the patch, the throughput improved by 62.7%.
Signed-off-by Shakeel Butt <[email protected]>Reported-by kernel test robot <[email protected]>
Nice!
Quoted 26 lines
--- include/linux/memcontrol.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)include/linux/memcontrol.h +4
-4@@ -354,10 +354,11 @@ struct mem_cgroup { };/* -* size of first charge trial. "32" comes from vmscan.c's magic value. -* TODO: maybe necessary to use big numbers in big irons. +* size of first charge trial. +* TODO: maybe necessary to use big numbers in big irons or dynamic based of the +* workload. */ -#define MEMCG_CHARGE_BATCH 32U +#define MEMCG_CHARGE_BATCH 64Uextern struct mem_cgroup *root_mem_cgroup; --2.37.1.595.g718a3a8f04-goog
Re: [PATCH 2/3] mm: page_counter: rearrange struct page_counter fields
In reply to Shakeel ButtQuoted 23 lines · Shakeel Butt
With memcg v2 enabled,
memcg->memory.usageis a very hot member for the workloads doing memcg charging on multiple CPUs concurrently. Particularly the network intensive workloads. In addition, there is a false cache sharing between memory.usage and memory.high on the charge path. This patch moves the usage into a separate cacheline and move all the read most fields into separate cacheline.To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10KResults (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 12413.7 Mbps (18.4% improvement)With the patch, the throughput improved by 18.4%.
Quoted 8 lines
One side-effect of this patch is the increase in the size of struct
mem_cgroup. However for the performance improvement, this additional size is worth it. In addition there are opportunities to reduce the size of structmem_cgrouplike deprecation of kmem and tcpmem page counters and better packing.Signed-off-by Shakeel Butt <[email protected]>Reported-by kernel test robot <[email protected]>
Looks good to me, with one nit below.
Quoted 14 lines
--- include/linux/page_counter.h | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-)include/linux/page_counter.h +1
-0@@ -3,15 +3,27 @@#define _LINUX_PAGE_COUNTER_H#include <linux/atomic.h> +#include <linux/cache.h> #include <linux/kernel.h>
include/linux/page_counter.h line 17
Quoted 10 lines
include/linux/page_counter.h +8
-0#include <asm/page.h> +#if defined(CONFIG_SMP ) +struct pc_padding { +char x[0 ]; +}____cacheline_internodealigned_in_smp ; +#define PC_PADDING (name) struct pc_padding name +#else +#define PC_PADDING (name) +#endif
There are 2 similar padding definitions in mmzone.h and memcontrol.h:
struct memcg_padding {
char x[0];
} ____cacheline_internodealigned_in_smp;
#define MEMCG_PADDING(name) struct memcg_padding name
struct zone_padding {
char x[0];
} ____cacheline_internodealigned_in_smp;
#define ZONE_PADDING(name) struct zone_padding name;Maybe we can generalize them, and lift it into include/cache.h? so that more places can reuse it in future.
Thanks,
Feng
Re: [PATCH 3/3] memcg: increase MEMCG_CHARGE_BATCH to 64
In reply to Shakeel ButtQuoted 19 lines · Shakeel Butt
For several years,
MEMCG_CHARGE_BATCHwas kept at 32 but with bigger machines and the network intensive workloads requiring througput in Gbps, 32 is too small and makes the memcg charging path a bottleneck. For now, increase it to 64 for easy acceptance to 6.0. We will need to revisit this in future for ever increasing demand of higher performance.Please note that the memcg charge path drain the per-cpu memcg charge stock, so there should not be any oom behavior change.
To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K
Quoted 8 lines
Results (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 17064.7 Mbps (62.7% improvement)With the patch, the throughput improved by 62.7%.
Signed-off-by Shakeel Butt <[email protected]>Reported-by kernel test robot <[email protected]>
This batch number has long been a pain point :) thanks for the work!
- Feng
Quoted 26 lines
--- include/linux/memcontrol.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)include/linux/memcontrol.h +4
-3@@ -354,10 +354,11 @@ struct mem_cgroup { };/* -* size of first charge trial. "32" comes from vmscan.c's magic value. -* TODO: maybe necessary to use big numbers in big irons. +* size of first charge trial. +* TODO: maybe necessary to use big numbers in big irons or dynamic based of the +* workload. */ -#define MEMCG_CHARGE_BATCH 32U +#define MEMCG_CHARGE_BATCH 64Uextern struct mem_cgroup *root_mem_cgroup;Signature · 1 line
2.37.1.595.g718a3a8f04-goog
Re: [PATCH 1/3] mm: page_counter: remove unneeded atomic ops for low/min
In reply to Shakeel ButtQuoted 15 lines · Shakeel Butt
For cgroups using low or min protections, the function
propagate_protected_usage()was doing an atomicxchg()operation irrespectively. It only needs to do that operation if the new value of protection is different from older one. This patch does that.To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K
Quoted 8 lines
Results (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 14542.5 Mbps (38.7% improvement)With the patch, the throughput improved by 38.7%
Signed-off-by Shakeel Butt <[email protected]>Reported-by kernel test robot <[email protected]>
Thanks!
- Feng
Quoted 42 lines
--- mm/page_counter.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)mm/page_counter.c +6
-7@@ -17,24 +17,23 @@ static void propagate_protected_usage(struct page_counter *c, unsigned long usage) {unsigned long protected, old_protected; -unsigned long low, min;long delta;if (!c->parent)return ; - min =READ_ONCE (c->min); -if (min ||atomic_long_read (&c->min_usage)) { - protected =min (usage, min); + protected =min (usage,READ_ONCE (c->min)); + old_protected =atomic_long_read (&c->min_usage); +if (protected != old_protected) { old_protected =atomic_long_xchg (&c->min_usage, protected); delta = protected - old_protected;if (delta)atomic_long_add (delta, &c->parent->children_min_usage); } - low =READ_ONCE (c->low); -if (low ||atomic_long_read (&c->low_usage)) { - protected =min (usage, low); + protected =min (usage,READ_ONCE (c->low)); + old_protected =atomic_long_read (&c->low_usage); +if (protected != old_protected) { old_protected =atomic_long_xchg (&c->low_usage, protected); delta = protected - old_protected;if (delta)Signature · 1 line
2.37.1.595.g718a3a8f04-goog
Re: [PATCH 2/3] mm: page_counter: rearrange struct page_counter fields
In reply to Soheil Hassas YeganehQuoted 20 lines · Soheil Hassas Yeganeh
Quoted 18 lines · Shakeel Butt
With memcg v2 enabled,
memcg->memory.usageis a very hot member for the workloads doing memcg charging on multiple CPUs concurrently. Particularly the network intensive workloads. In addition, there is a false cache sharing between memory.usage and memory.high on the charge path. This patch moves the usage into a separate cacheline and move all the read most fields into separate cacheline.To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K
Quoted 9 lines
Quoted 5 lines
Results (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 12413.7 Mbps (18.4% improvement)With the patch, the throughput improved by 18.4%.
Shakeel, for my understanding: is this on top of the gains from the previous patch?
No, this is independent of the previous patch. The cover letter has the numbers for all three optimizations applied together.
Re: [PATCH 2/3] mm: page_counter: rearrange struct page_counter fields
In reply to Feng TangQuoted 31 lines · Feng Tang
Quoted 29 lines · Shakeel Butt
With memcg v2 enabled,
memcg->memory.usageis a very hot member for the workloads doing memcg charging on multiple CPUs concurrently. Particularly the network intensive workloads. In addition, there is a false cache sharing between memory.usage and memory.high on the charge path. This patch moves the usage into a separate cacheline and move all the read most fields into separate cacheline.To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10KResults (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 12413.7 Mbps (18.4% improvement)With the patch, the throughput improved by 18.4%.
One side-effect of this patch is the increase in the size of struct
mem_cgroup. However for the performance improvement, this additional size is worth it. In addition there are opportunities to reduce the size of structmem_cgrouplike deprecation of kmem and tcpmem page counters and better packing.
Quoted 6 lines
Quoted 2 lines
Signed-off-by Shakeel Butt <[email protected]>Reported-by kernel test robot <[email protected]>Looks good to me, with one nit below.
Reviewed-by Feng Tang <[email protected]>
Thanks.
Quoted 38 lines
Quoted 15 lines
--- include/linux/page_counter.h | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-)include/linux/page_counter.h +1
-0@@ -3,15 +3,27 @@#define _LINUX_PAGE_COUNTER_H#include <linux/atomic.h> +#include <linux/cache.h> #include <linux/kernel.h> include/linux/page_counter.h line 17
Quoted 10 lines
include/linux/page_counter.h +8
-0#include <asm/page.h> +#if defined(CONFIG_SMP ) +struct pc_padding { +char x[0 ]; +}____cacheline_internodealigned_in_smp ; +#define PC_PADDING (name) struct pc_padding name +#else +#define PC_PADDING (name) +#endif There are 2 similar padding definitions in
mmzone.handmemcontrol.h:struct memcg_padding { char x[0]; } ____cacheline_internodealigned_in_smp; #define MEMCG_PADDING(name) struct memcg_padding name struct zone_padding { char x[0]; } ____cacheline_internodealigned_in_smp; #define ZONE_PADDING(name) struct zone_padding name;
Quoted 3 lines
Maybe we can generalize them, and lift it into
include/cache.h? so that more places can reuse it in future.
This makes sense but let me do that in a separate patch.
Michal Hocko
reviewerRe: [PATCH 1/3] mm: page_counter: remove unneeded atomic ops for low/min
In reply to Shakeel ButtQuoted 4 lines · Shakeel Butt
For cgroups using low or min protections, the function
propagate_protected_usage()was doing an atomicxchg()operation irrespectively. It only needs to do that operation if the new value of protection is different from older one. This patch does that.
This doesn't really explain why.
Quoted 5 lines
To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
I have hard time to really grasp what is the actual setup and why it matters and why the patch makes any difference. Please elaborate some more here.
Quoted 26 lines
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10KResults (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 14542.5 Mbps (38.7% improvement)With the patch, the throughput improved by 38.7%
Signed-off-by Shakeel Butt <[email protected]>Reported-by kernel test robot <[email protected]>--- mm/page_counter.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)mm/page_counter.c +0
-1@@ -17,24 +17,23 @@ static void propagate_protected_usage(struct page_counter *c, unsigned long usage) {unsigned long protected, old_protected; -unsigned long low, min;long delta;
mm/page_counter.c static void propagate_protected_usage(struct page_counter *c, · line 27
Quoted 10 lines
mm/page_counter.c +3
-3if (!c->parent)return ; - min =READ_ONCE (c->min); -if (min ||atomic_long_read (&c->min_usage)) { - protected =min (usage, min); + protected =min (usage,READ_ONCE (c->min)); + old_protected =atomic_long_read (&c->min_usage); +if (protected != old_protected) {
I have to cache that code back into brain. It is really subtle thing and it is not really obvious why this is still correct. I will think about that some more but the changelog could help with that a lot.
Signature · 2 lines
Michal Hocko SUSE Labs
Michal Hocko
reviewerRe: [PATCH 1/3] mm: page_counter: remove unneeded atomic ops for low/min
Quoted 1 line · Michal Hocko
On Mon 22-08-22 00:17:35, Shakeel Butt wrote:
[...]
Quoted 14 lines
Quoted 14 lines
mm/page_counter.c +0
-1@@ -17,24 +17,23 @@ static void propagate_protected_usage(struct page_counter *c, unsigned long usage) {unsigned long protected, old_protected; -unsigned long low, min;long delta;if (!c->parent)return ;
Quoted 10 lines
mm/page_counter.c static void propagate_protected_usage(struct page_counter *c, · line 27
Quoted 6 lines
mm/page_counter.c +3
-3- min =READ_ONCE (c->min); -if (min ||atomic_long_read (&c->min_usage)) { - protected =min (usage, min); + protected =min (usage,READ_ONCE (c->min)); + old_protected =atomic_long_read (&c->min_usage); +if (protected != old_protected) {I have to cache that code back into brain. It is really subtle thing and it is not really obvious why this is still correct. I will think about that some more but the changelog could help with that a lot.
OK, so the this patch will be most useful when the min > 0 && min < usage because then the protection doesn't really change since the last call. In other words when the usage grows above the protection and your workload benefits from this change because that happens a lot as only a part of the workload is protected. Correct?
Unless I have missed anything this shouldn't break the correctness but I still have to think about the proportional distribution of the protection because that adds to the complexity here.
Signature · 2 lines
Michal Hocko SUSE Labs
Michal Hocko
reviewerRe: [PATCH 2/3] mm: page_counter: rearrange struct page_counter fields
In reply to Shakeel ButtQuoted 7 lines · Shakeel Butt
With memcg v2 enabled,
memcg->memory.usageis a very hot member for the workloads doing memcg charging on multiple CPUs concurrently. Particularly the network intensive workloads. In addition, there is a false cache sharing between memory.usage and memory.high on the charge path. This patch moves the usage into a separate cacheline and move all the read most fields into separate cacheline.
Quoted 5 lines
To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
Again the workload description is not particularly useful. I guess the only important aspect is the netserver part below and the number of CPUs because min and low setup doesn't have much to do with this, right? At least that is my reading of the memory.high mentioned above.
Quoted 39 lines
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10KResults (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 12413.7 Mbps (18.4% improvement)With the patch, the throughput improved by 18.4%.
One side-effect of this patch is the increase in the size of struct
mem_cgroup. However for the performance improvement, this additional size is worth it. In addition there are opportunities to reduce the size of structmem_cgrouplike deprecation of kmem and tcpmem page counters and better packing.Signed-off-by Shakeel Butt <[email protected]>Reported-by kernel test robot <[email protected]>--- include/linux/page_counter.h | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-)include/linux/page_counter.h +6
-0@@ -3,15 +3,27 @@#define _LINUX_PAGE_COUNTER_H#include <linux/atomic.h> +#include <linux/cache.h> #include <linux/kernel.h> #include <asm/page.h> +#if defined(CONFIG_SMP ) +struct pc_padding { +char x[0 ]; +}____cacheline_internodealigned_in_smp ; +#define PC_PADDING (name) struct pc_padding name
include/linux/page_counter.h line 24
Quoted 10 lines
include/linux/page_counter.h +9
-0+#else +#define PC_PADDING (name) +#endif +struct page_counter { +/* +* Make sure 'usage' does not share cacheline with any other field. The +* memcg->memory.usage is a hot member of struct mem_cgroup. +*/ +PC_PADDING (_pad1_);
Why don't you simply require alignment for the structure?
Other than that, looks good to me and it makes sense.
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH 3/3] memcg: increase MEMCG_CHARGE_BATCH to 64
In reply to Shakeel ButtQuoted 5 lines · Shakeel Butt
For several years,
MEMCG_CHARGE_BATCHwas kept at 32 but with bigger machines and the network intensive workloads requiring througput in Gbps, 32 is too small and makes the memcg charging path a bottleneck. For now, increase it to 64 for easy acceptance to 6.0. We will need to revisit this in future for ever increasing demand of higher performance.
Yes, the batch size has always been an arbitrary number. I do not think there have ever been any solid grounds for the value we have now except we need something and SWAP_CLUSTER_MAX was a good enough template.
Increasing it to 64 sounds like a reasonable step. It would be great to have it scale based on the number of CPUs and potentially other factors but that would be hard to get right and actually hard to evaluate because it will depend on the specific workload.
Quoted 2 lines
Please note that the memcg charge path drain the per-cpu memcg charge stock, so there should not be any oom behavior change.
It will have an effect on other stuff as well like high limit reclaim backoff and stast flushing.
Quoted 5 lines
To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
a similar feedback to the test case description as with other patches.
Quoted 5 lines
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K
Quoted 8 lines
Results (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 17064.7 Mbps (62.7% improvement)With the patch, the throughput improved by 62.7%.
Signed-off-by Shakeel Butt <[email protected]>Reported-by kernel test robot <[email protected]>
Anyway
Thanks!
Quoted 25 lines
--- include/linux/memcontrol.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)include/linux/memcontrol.h +4
-3@@ -354,10 +354,11 @@ struct mem_cgroup { };/* -* size of first charge trial. "32" comes from vmscan.c's magic value. -* TODO: maybe necessary to use big numbers in big irons. +* size of first charge trial. +* TODO: maybe necessary to use big numbers in big irons or dynamic based of the +* workload. */ -#define MEMCG_CHARGE_BATCH 32U +#define MEMCG_CHARGE_BATCH 64Uextern struct mem_cgroup *root_mem_cgroup;Signature · 1 line
2.37.1.595.g718a3a8f04-goog
Signature · 2 lines
Michal Hocko SUSE Labs
Soheil Hassas Yeganeh
Acked-byRe: [PATCH 2/3] mm: page_counter: rearrange struct page_counter fields
In reply to Shakeel ButtQuoted 26 lines · Shakeel Butt
Quoted 24 lines · Soheil Hassas Yeganeh
Quoted 22 lines · Shakeel Butt
With memcg v2 enabled,
memcg->memory.usageis a very hot member for the workloads doing memcg charging on multiple CPUs concurrently. Particularly the network intensive workloads. In addition, there is a false cache sharing between memory.usage and memory.high on the charge path. This patch moves the usage into a separate cacheline and move all the read most fields into separate cacheline.To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10KResults (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 12413.7 Mbps (18.4% improvement)
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
With the patch, the throughput improved by 18.4%.
Shakeel, for my understanding: is this on top of the gains from the previous patch?
No, this is independent of the previous patch. The cover letter has the numbers for all three optimizations applied together.
Re: [PATCH 1/3] mm: page_counter: remove unneeded atomic ops for low/min
In reply to Michal HockoQuoted 29 lines · Michal Hocko
Quoted 1 line · Michal Hocko
On Mon 22-08-22 00:17:35, Shakeel Butt wrote:
[...]
Quoted 14 lines
Quoted 14 lines
mm/page_counter.c +0
-1@@ -17,24 +17,23 @@ static void propagate_protected_usage(struct page_counter *c, unsigned long usage) {unsigned long protected, old_protected; -unsigned long low, min;long delta;if (!c->parent)return ;Quoted 10 lines
mm/page_counter.c static void propagate_protected_usage(struct page_counter *c, · line 27
Quoted 6 lines
mm/page_counter.c +3
-3- min =READ_ONCE (c->min); -if (min ||atomic_long_read (&c->min_usage)) { - protected =min (usage, min); + protected =min (usage,READ_ONCE (c->min)); + old_protected =atomic_long_read (&c->min_usage); +if (protected != old_protected) {I have to cache that code back into brain. It is really subtle thing and it is not really obvious why this is still correct. I will think about that some more but the changelog could help with that a lot.
Quoted 5 lines
OK, so the this patch will be most useful when the min > 0 && min < usage because then the protection doesn't really change since the last call. In other words when the usage grows above the protection and your workload benefits from this change because that happens a lot as only a part of the workload is protected. Correct?
Yes, that is correct. I hope the experiment setup is clear now.
Quoted 4 lines
Unless I have missed anything this shouldn't break the correctness but I still have to think about the proportional distribution of the protection because that adds to the complexity here.
The patch is not changing any semantics. It is just removing an unnecessary atomic xchg() for a specific scenario (min > 0 && min < usage). I don't think there will be any change related to proportional distribution of the protection.
Re: [PATCH 2/3] mm: page_counter: rearrange struct page_counter fields
In reply to Michal HockoQuoted 15 lines · Michal Hocko
Quoted 7 lines · Shakeel Butt
With memcg v2 enabled,
memcg->memory.usageis a very hot member for the workloads doing memcg charging on multiple CPUs concurrently. Particularly the network intensive workloads. In addition, there is a false cache sharing between memory.usage and memory.high on the charge path. This patch moves the usage into a separate cacheline and move all the read most fields into separate cacheline.Quoted 5 lines
To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
Quoted 5 lines
Again the workload description is not particularly useful. I guess the only important aspect is the netserver part below and the number of CPUs because min and low setup doesn't have much to do with this, right? At least that is my reading of the memory.high mentioned above.
The experiment numbers below are for only this patch independently i.e. the unnecessary min/low atomic xchg() is still happening for both setups. I could run the experiment without setting min and low but I wanted to keep the setup exactly the same for all three optimizations.
This patch and the following perf numbers shows only the impact of removing false sharing in struct page_counter for memcg->memory on the charging code path.
Quoted 43 lines
Quoted 43 lines
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10KResults (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 12413.7 Mbps (18.4% improvement)With the patch, the throughput improved by 18.4%.
One side-effect of this patch is the increase in the size of struct
mem_cgroup. However for the performance improvement, this additional size is worth it. In addition there are opportunities to reduce the size of structmem_cgrouplike deprecation of kmem and tcpmem page counters and better packing.Signed-off-by Shakeel Butt <[email protected]>Reported-by kernel test robot <[email protected]>--- include/linux/page_counter.h | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-)include/linux/page_counter.h +10
-0@@ -3,15 +3,27 @@#define _LINUX_PAGE_COUNTER_H#include <linux/atomic.h> +#include <linux/cache.h> #include <linux/kernel.h> #include <asm/page.h> +#if defined(CONFIG_SMP ) +struct pc_padding { +char x[0 ]; +}____cacheline_internodealigned_in_smp ; +#define PC_PADDING (name) struct pc_padding name +#else +#define PC_PADDING (name) +#endif +
Quoted 8 lines
include/linux/page_counter.h line 24
Quoted 6 lines
include/linux/page_counter.h +5
-0struct page_counter { +/* +* Make sure 'usage' does not share cacheline with any other field. The +* memcg->memory.usage is a hot member of struct mem_cgroup. +*/ +PC_PADDING (_pad1_);Why don't you simply require alignment for the structure?
I don't just want the alignment of the structure. I want different fields of this structure to not share the cache line. More specifically the 'high' and 'usage' fields. With this change the usage will be its own cache line, the read-most fields will be on separate cache line and the fields which sometimes get updated on charge path based on some condition will be a different cache line from the previous two.
Re: [PATCH 3/3] memcg: increase MEMCG_CHARGE_BATCH to 64
In reply to Michal HockoQuoted 1 line · Michal Hocko
[...]
Quoted 8 lines
Quoted 6 lines
To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
a similar feedback to the test case description as with other patches.
What more info should I add to the description? Why did I set up min and low or something else?
Quoted 9 lines
Quoted 9 lines
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10KResults (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 17064.7 Mbps (62.7% improvement)
Quoted 7 lines
Quoted 4 lines
With the patch, the throughput improved by 62.7%.
Signed-off-by Shakeel Butt <[email protected]>Reported-by kernel test robot <[email protected]>Anyway
Acked-by Michal Hocko <[email protected]>
Thanks
Michal Hocko
reviewerRe: [PATCH 2/3] mm: page_counter: rearrange struct page_counter fields
In reply to Shakeel ButtOn Mon 22-08-22 08:06:14, Shakeel Butt wrote:
[...]
Quoted 9 lines
Quoted 8 lines
include/linux/page_counter.h line 24
Quoted 6 lines
struct
page_counter{include/linux/page_counter.h +5
-0+/* +* Make sure 'usage' does not share cacheline with any other field. The +* memcg->memory.usage is a hot member of struct mem_cgroup. +*/ +PC_PADDING (_pad1_);Why don't you simply require alignment for the structure?
Quoted 7 lines
I don't just want the alignment of the structure. I want different fields of this structure to not share the cache line. More specifically the 'high' and 'usage' fields. With this change the usage will be its own cache line, the read-most fields will be on separate cache line and the fields which sometimes get updated on charge path based on some condition will be a different cache line from the previous two.
I do not follow. If you make an explicit requirement for the structure alignement then the first field in the structure will be guarantied to have that alignement and you achieve the rest to be in the other cache line by adding padding behind that.
Signature · 2 lines
Michal Hocko SUSE Labs
Michal Hocko
reviewerRe: [PATCH 1/3] mm: page_counter: remove unneeded atomic ops for low/min
In reply to Shakeel ButtQuoted 30 lines · Shakeel Butt
Quoted 29 lines · Michal Hocko
Quoted 1 line · Michal Hocko
On Mon 22-08-22 00:17:35, Shakeel Butt wrote:
[...]
Quoted 24 lines
Quoted 10 lines
mm/page_counter.c +0
-1@@ -17,24 +17,23 @@ static void propagate_protected_usage(struct page_counter *c, unsigned long usage) {unsigned long protected, old_protected; -unsigned long low, min;long delta;mm/page_counter.c static void propagate_protected_usage(struct page_counter *c, · line 27
Quoted 10 lines
mm/page_counter.c +3
-3if (!c->parent)return ; - min =READ_ONCE (c->min); -if (min ||atomic_long_read (&c->min_usage)) { - protected =min (usage, min); + protected =min (usage,READ_ONCE (c->min)); + old_protected =atomic_long_read (&c->min_usage); +if (protected != old_protected) {I have to cache that code back into brain. It is really subtle thing and it is not really obvious why this is still correct. I will think about that some more but the changelog could help with that a lot.
Quoted 7 lines
Quoted 5 lines
OK, so the this patch will be most useful when the min > 0 && min < usage because then the protection doesn't really change since the last call. In other words when the usage grows above the protection and your workload benefits from this change because that happens a lot as only a part of the workload is protected. Correct?
Yes, that is correct. I hope the experiment setup is clear now.
Maybe it is just me that it took a bit to grasp but maybe we want to save our future selfs from going through that mental process again. So please just be explicit about that in the changelog. It is really the part that workloads excessing the protection will benefit the most that would help to understand this patch.
Quoted 8 lines
Quoted 3 lines
Unless I have missed anything this shouldn't break the correctness but I still have to think about the proportional distribution of the protection because that adds to the complexity here.
The patch is not changing any semantics. It is just removing an unnecessary atomic
xchg()for a specific scenario (min > 0 && min < usage). I don't think there will be any change related to proportional distribution of the protection.
Yes, I suspect you are right. I just remembered previous fixes like 503970e42325 ("mm: memcontrol: fix memory.low proportional distribution") which just made me nervous that this is a tricky area.
I will have another look tomorrow with a fresh brain and send an ack.
Signature · 2 lines
Michal Hocko SUSE Labs
Michal Hocko
reviewerRe: [PATCH 3/3] memcg: increase MEMCG_CHARGE_BATCH to 64
In reply to Shakeel ButtQuoted 10 lines · Shakeel Butt
Quoted 1 line · Michal Hocko
[...]
Quoted 7 lines
Quoted 6 lines
To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
Quoted 4 lines
Quoted 1 line
a similar feedback to the test case description as with other patches.
What more info should I add to the description? Why did I set up min and low or something else?
I do see why you wanted to keep the test consistent over those three patches. I would just drop the reference to the protection configuration because it likely doesn't make much of an impact, does it? It is the multi cpu setup and false sharing that makes the real difference. Or am I wrong in assuming that?
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH 2/3] mm: page_counter: rearrange struct page_counter fields
In reply to Michal HockoQuoted 20 lines · Michal Hocko
On Mon 22-08-22 08:06:14, Shakeel Butt wrote:
[...]Quoted 9 lines
Quoted 8 lines
include/linux/page_counter.h line 24
Quoted 6 lines
struct
page_counter{include/linux/page_counter.h +5
-0+/* +* Make sure 'usage' does not share cacheline with any other field. The +* memcg->memory.usage is a hot member of struct mem_cgroup. +*/ +PC_PADDING (_pad1_);Why don't you simply require alignment for the structure?
Quoted 7 lines
I don't just want the alignment of the structure. I want different fields of this structure to not share the cache line. More specifically the 'high' and 'usage' fields. With this change the usage will be its own cache line, the read-most fields will be on separate cache line and the fields which sometimes get updated on charge path based on some condition will be a different cache line from the previous two.
Quoted 4 lines
I do not follow. If you make an explicit requirement for the structure alignement then the first field in the structure will be guarantied to have that alignement and you achieve the rest to be in the other cache line by adding padding behind that.
Oh, you were talking explicitly about _pad1_, yes, we can remove it and make the struct cache align. I will do it in the next version.
Re: [PATCH 1/3] mm: page_counter: remove unneeded atomic ops for low/min
In reply to Michal HockoQuoted 38 lines · Michal Hocko
Quoted 36 lines · Shakeel Butt
Quoted 34 lines · Michal Hocko
Quoted 1 line · Michal Hocko
On Mon 22-08-22 00:17:35, Shakeel Butt wrote:
[...]
Quoted 14 lines
Quoted 14 lines
mm/page_counter.c +0
-1@@ -17,24 +17,23 @@ static void propagate_protected_usage(struct page_counter *c, unsigned long usage) {unsigned long protected, old_protected; -unsigned long low, min;long delta;if (!c->parent)return ;Quoted 10 lines
mm/page_counter.c static void propagate_protected_usage(struct page_counter *c, · line 27
Quoted 6 lines
mm/page_counter.c +3
-3- min =READ_ONCE (c->min); -if (min ||atomic_long_read (&c->min_usage)) { - protected =min (usage, min); + protected =min (usage,READ_ONCE (c->min)); + old_protected =atomic_long_read (&c->min_usage); +if (protected != old_protected) {I have to cache that code back into brain. It is really subtle thing and it is not really obvious why this is still correct. I will think about that some more but the changelog could help with that a lot.
OK, so the this patch will be most useful when the min > 0 && min < usage because then the protection doesn't really change since the last call. In other words when the usage grows above the protection and your workload benefits from this change because that happens a lot as only a part of the workload is protected. Correct?
Quoted 8 lines
Quoted 1 line
Yes, that is correct. I hope the experiment setup is clear now.
Maybe it is just me that it took a bit to grasp but maybe we want to save our future selfs from going through that mental process again. So please just be explicit about that in the changelog. It is really the part that workloads excessing the protection will benefit the most that would help to understand this patch.
I will add more detail in the commit message in the next version.
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines
Unless I have missed anything this shouldn't break the correctness but I still have to think about the proportional distribution of the protection because that adds to the complexity here.
The patch is not changing any semantics. It is just removing an unnecessary atomic
xchg()for a specific scenario (min > 0 && min < usage). I don't think there will be any change related to proportional distribution of the protection.
Quoted 5 lines
Yes, I suspect you are right. I just remembered previous fixes like
503970e42325("mm: memcontrol: fix memory.low proportional distribution") which just made me nervous that this is a tricky area.I will have another look tomorrow with a fresh brain and send an ack.
I will wait for your ack before sending the next version.
Re: [PATCH 3/3] memcg: increase MEMCG_CHARGE_BATCH to 64
In reply to Michal HockoQuoted 14 lines · Michal Hocko
Quoted 12 lines · Shakeel Butt
Quoted 1 line · Michal Hocko
[...]
Quoted 8 lines
Quoted 6 lines
To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
a similar feedback to the test case description as with other patches.
Quoted 9 lines
Quoted 2 lines
What more info should I add to the description? Why did I set up min and low or something else?
I do see why you wanted to keep the test consistent over those three patches. I would just drop the reference to the protection configuration because it likely doesn't make much of an impact, does it? It is the multi cpu setup and false sharing that makes the real difference. Or am I wrong in assuming that?
No, you are correct. I will cleanup the commit message in the next version.
Re: [PATCH 1/3] mm: page_counter: remove unneeded atomic ops for low/min
In reply to Shakeel ButtQuoted 15 lines · Shakeel Butt
For cgroups using low or min protections, the function
propagate_protected_usage()was doing an atomicxchg()operation irrespectively. It only needs to do that operation if the new value of protection is different from older one. This patch does that.To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K
Quoted 5 lines
Results (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 14542.5 Mbps (38.7% improvement)With the patch, the throughput improved by 38.7%
Nice savings!
Quoted 21 lines
Signed-off-by Shakeel Butt <[email protected]>Reported-by kernel test robot <[email protected]>--- mm/page_counter.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)mm/page_counter.c +0
-1@@ -17,24 +17,23 @@ static void propagate_protected_usage(struct page_counter *c, unsigned long usage) {unsigned long protected, old_protected; -unsigned long low, min;long delta;if (!c->parent)return ;
mm/page_counter.c static void propagate_protected_usage(struct page_counter *c, · line 31
Quoted 10 lines
mm/page_counter.c +3
-3- min =READ_ONCE (c->min); -if (min ||atomic_long_read (&c->min_usage)) { - protected =min (usage, min); + protected =min (usage,READ_ONCE (c->min)); + old_protected =atomic_long_read (&c->min_usage); +if (protected != old_protected) { old_protected =atomic_long_xchg (&c->min_usage, protected); delta = protected - old_protected;if (delta)atomic_long_add (delta, &c->parent->children_min_usage);
What if there is a concurrent update of c->min_usage? Then the patched version can miss an update. I can't imagine a case when it will lead to bad consequences, so probably it's ok. But not super obvious.
I think the way to think of it is that a missed update will be fixed by the next one, so it's ok to run some time with old numbers.
Thanks!
Re: [PATCH 2/3] mm: page_counter: rearrange struct page_counter fields
In reply to Shakeel ButtQuoted 21 lines · Shakeel Butt
Quoted 20 lines · Michal Hocko
On Mon 22-08-22 08:06:14, Shakeel Butt wrote:
[...]Quoted 16 lines
Quoted 8 lines
include/linux/page_counter.h line 24
Quoted 6 lines
struct
page_counter{include/linux/page_counter.h +5
-0+/* +* Make sure 'usage' does not share cacheline with any other field. The +* memcg->memory.usage is a hot member of struct mem_cgroup. +*/ +PC_PADDING (_pad1_);Why don't you simply require alignment for the structure?
I don't just want the alignment of the structure. I want different fields of this structure to not share the cache line. More specifically the 'high' and 'usage' fields. With this change the usage will be its own cache line, the read-most fields will be on separate cache line and the fields which sometimes get updated on charge path based on some condition will be a different cache line from the previous two.
Quoted 7 lines
Quoted 4 lines
I do not follow. If you make an explicit requirement for the structure alignement then the first field in the structure will be guarantied to have that alignement and you achieve the rest to be in the other cache line by adding padding behind that.
Oh, you were talking explicitly about _pad1_, yes, we can remove it and make the struct cache align. I will do it in the next version.
Yes, please, it caught my eyes too.
With this change:
Also, can you, please, include the numbers on the additional memory overhead? I think it still worth it, just think we need to include them for a record.
Thanks!
Re: [PATCH 3/3] memcg: increase MEMCG_CHARGE_BATCH to 64
In reply to Shakeel ButtQuoted 19 lines · Shakeel Butt
For several years,
MEMCG_CHARGE_BATCHwas kept at 32 but with bigger machines and the network intensive workloads requiring througput in Gbps, 32 is too small and makes the memcg charging path a bottleneck. For now, increase it to 64 for easy acceptance to 6.0. We will need to revisit this in future for ever increasing demand of higher performance.Please note that the memcg charge path drain the per-cpu memcg charge stock, so there should not be any oom behavior change.
To evaluate the impact of this optimization, on a 72 CPUs machine, we ran the following workload in a three level of cgroup hierarchy with top level having min and low setup appropriately. More specifically memory.min equal to size of netperf binary and memory.low double of that.
$ netserver -6 # 36 instances of netperf with following params $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K
Quoted 5 lines
Results (average throughput of netperf):
Without (6.0-rc1) 10482.7 Mbps
With patch 17064.7 Mbps (62.7% improvement)With the patch, the throughput improved by 62.7%.
This is pretty significant!
I wonder only if we want to make it configurable (Idk a sysctl or maybe a config option) and close the topic.
Thanks!
Michal Hocko
reviewerRe: [PATCH 3/3] memcg: increase MEMCG_CHARGE_BATCH to 64
On Mon 22-08-22 11:37:30, Roman Gushchin wrote:
[...]
Quoted 2 lines
I wonder only if we want to make it configurable (Idk a sysctl or maybe a config option) and close the topic.
I do not think this is a good idea. We have other examples where we have outsourced internal tunning to the userspace and it has mostly proven impractical and long term more problematic than useful (e.g. lowmem_reserve_ratio, percpu_pagelist_high_fraction, swappiness just to name some that come to my mind). I have seen more often these to be used incorrectly than useful.
In this case, I guess we should consider either moving to per memcg charge batching and see whether the pcp overhead x memcg_count is worth that or some automagic tuning of the batch size depending on how effectively the batch is used. Certainly a lot of room for experimenting.
Signature · 2 lines
Michal Hocko SUSE Labs
Roman Gushchin
maintainerRe: [PATCH 3/3] memcg: increase MEMCG_CHARGE_BATCH to 64
Quoted 11 lines · Michal Hocko
On Mon 22-08-22 11:37:30, Roman Gushchin wrote:
[...]Quoted 2 lines
I wonder only if we want to make it configurable (Idk a sysctl or maybe a config option) and close the topic.
I do not think this is a good idea. We have other examples where we have outsourced internal tunning to the userspace and it has mostly proven impractical and long term more problematic than useful (e.g.
lowmem_reserve_ratio,percpu_pagelist_high_fraction, swappiness just to name some that come to my mind). I have seen more often these to be used incorrectly than useful.
A agree, not a strong opinion here. But I wonder if somebody will complain on Shakeel's change because of the reduced accuracy. I know some users are using memory cgroups to track the size of various workloads (including relatively small) and 32->64 pages per cpu change can be noticeable for them. But we can wait for an actual bug report :)
Quoted 6 lines
In this case, I guess we should consider either moving to per memcg charge batching and see whether the pcp overhead x
memcg_countis worth that or some automagic tuning of the batch size depending on how effectively the batch is used. Certainly a lot of room for experimenting.
I'm not a big believer into the automagic tuning here because it's a fundamental trade-off of accuracy vs performance and various users might make a different choice depending on their needs, not on the cpu count or something else.
Per-memcg batching sounds interesting though. For example, we can likely batch updates on leaf cgroups and have a single atomic update instead of multiple most of the times. Or do you mean something different?
Thanks!
Michal Hocko
reviewerRe: [PATCH 3/3] memcg: increase MEMCG_CHARGE_BATCH to 64
Quoted 13 lines · Roman Gushchin
Quoted 11 lines · Michal Hocko
On Mon 22-08-22 11:37:30, Roman Gushchin wrote:
[...]Quoted 2 lines
I wonder only if we want to make it configurable (Idk a sysctl or maybe a config option) and close the topic.
I do not think this is a good idea. We have other examples where we have outsourced internal tunning to the userspace and it has mostly proven impractical and long term more problematic than useful (e.g.
lowmem_reserve_ratio,percpu_pagelist_high_fraction, swappiness just to name some that come to my mind). I have seen more often these to be used incorrectly than useful.
Quoted 5 lines
A agree, not a strong opinion here. But I wonder if somebody will complain on Shakeel's change because of the reduced accuracy. I know some users are using memory cgroups to track the size of various workloads (including relatively small) and 32->64 pages per cpu change can be noticeable for them. But we can wait for an actual bug report :)
Yes, that would be my approach. I have seen reports like that already but that was mostly because of heavy caching on the SLUB side on older kernels. So there surely are workloads with small limits configured (e.g. 20MB). On the other hand those users were receptive to adapt their limits as they were kinda arbitrary anyway.
Quoted 9 lines
Quoted 5 lines
In this case, I guess we should consider either moving to per memcg charge batching and see whether the pcp overhead x
memcg_countis worth that or some automagic tuning of the batch size depending on how effectively the batch is used. Certainly a lot of room for experimenting.I'm not a big believer into the automagic tuning here because it's a fundamental trade-off of accuracy vs performance and various users might make a different choice depending on their needs, not on the cpu count or something else.
Yes, this not an easy thing to get right. I was mostly thinking some auto scaling based on the limit size or growing the stock if cache hits are common and decrease when stocks get flushed often because multiple memcgs compete over the same pcp stock. But to me it seems like a per memcg approach might lead better results without too many heuristics (albeit more memory hungry).
Quoted 3 lines
Per-memcg batching sounds interesting though. For example, we can likely batch updates on leaf cgroups and have a single atomic update instead of multiple most of the times. Or do you mean something different?
No, that was exactly my thinking as well.
Signature · 2 lines
Michal Hocko SUSE Labs
Michal Hocko
reviewerRe: [PATCH 1/3] mm: page_counter: remove unneeded atomic ops for low/min
In reply to Michal HockoQuoted 2 lines · Michal Hocko
Quoted 1 line · Shakeel Butt
On Mon, Aug 22, 2022 at 3:18 AM Michal Hocko <mhocko-IBi9RG/[email protected]> wrote:
[...]
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines
Unless I have missed anything this shouldn't break the correctness but I still have to think about the proportional distribution of the protection because that adds to the complexity here.
The patch is not changing any semantics. It is just removing an unnecessary atomic
xchg()for a specific scenario (min > 0 && min < usage). I don't think there will be any change related to proportional distribution of the protection.
Quoted 5 lines
Yes, I suspect you are right. I just remembered previous fixes like
503970e42325("mm: memcontrol: fix memory.low proportional distribution") which just made me nervous that this is a tricky area.I will have another look tomorrow with a fresh brain and send an ack.
I cannot spot any problem. But I guess it would be good to have a little comment to explain that races on the min_usage update (mentioned by Roman) are acceptable and savings from atomic update are preferred.
The worst case I can imagine would be something like uncharge 4kB racing with charge 2MB. The first reduces the protection (min_usage) while the other one misses that update and doesn't increase it. But even then the effect shouldn't be really large. At least I have hard time imagine this would throw things off too much.
Signature · 2 lines
Michal Hocko SUSE Labs