All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2, 0/4] mm, memcg: cgroup v1 and v2 tunable load/store tearing fixes
@ 2023-03-06 15:41 ` Yue Zhao
  0 siblings, 0 replies; 23+ messages in thread
From: Yue Zhao @ 2023-03-06 15:41 UTC (permalink / raw)
  To: akpm
  Cc: roman.gushchin, hannes, mhocko, shakeelb, muchun.song, willy,
	linux-mm, cgroups, linux-kernel, tangyeechou, Yue Zhao

This patch series helps to prevent load/store tearing in
several cgroup knobs.

As kindly pointed out by Michal Hocko and Roman Gushchin
, the changelog has been rephrased.

Besides, more knobs were checked, according to kind suggestions
from Shakeel Butt and Muchun Song.

v1:
- Add [WRITE|READ]_ONCE for memcg->oom_group
v2:
- Rephrase changelog
- Add [WRITE|READ]_ONCE for memcg->oom_kill_disable,
 memcg->swappiness, vm_swappiness and memcg->soft_limit

Yue Zhao (4):
  mm, memcg: Prevent memory.oom.group load/store tearing
  mm, memcg: Prevent memory.swappiness load/store tearing
  mm, memcg: Prevent memory.oom_control load/store tearing
  mm, memcg: Prevent memory.soft_limit_in_bytes load/store tearing

 include/linux/swap.h |  8 ++++----
 mm/memcontrol.c      | 18 +++++++++---------
 2 files changed, 13 insertions(+), 13 deletions(-)

-- 
2.17.1


^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH v2, 0/4] mm, memcg: cgroup v1 and v2 tunable load/store tearing fixes
@ 2023-03-06 15:41 ` Yue Zhao
  0 siblings, 0 replies; 23+ messages in thread
From: Yue Zhao @ 2023-03-06 15:41 UTC (permalink / raw)
  To: akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b
  Cc: roman.gushchin-fxUVXftIFDnyG1zEObXtfA,
	hannes-druUgvl0LCNAfugRpC6u6w, mhocko-DgEjT+Ai2ygdnm+yROfE0A,
	shakeelb-hpIqsD4AKlfQT0dZR+AlfA,
	muchun.song-fxUVXftIFDnyG1zEObXtfA, willy-wEGCiKHe2LqWVfeAwA7xHQ,
	linux-mm-Bw31MaZKKs3YtjvyW6yDsg, cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	tangyeechou-Re5JQEeQqe8AvxtiuMwx3w, Yue Zhao

This patch series helps to prevent load/store tearing in
several cgroup knobs.

As kindly pointed out by Michal Hocko and Roman Gushchin
, the changelog has been rephrased.

Besides, more knobs were checked, according to kind suggestions
from Shakeel Butt and Muchun Song.

v1:
- Add [WRITE|READ]_ONCE for memcg->oom_group
v2:
- Rephrase changelog
- Add [WRITE|READ]_ONCE for memcg->oom_kill_disable,
 memcg->swappiness, vm_swappiness and memcg->soft_limit

Yue Zhao (4):
  mm, memcg: Prevent memory.oom.group load/store tearing
  mm, memcg: Prevent memory.swappiness load/store tearing
  mm, memcg: Prevent memory.oom_control load/store tearing
  mm, memcg: Prevent memory.soft_limit_in_bytes load/store tearing

 include/linux/swap.h |  8 ++++----
 mm/memcontrol.c      | 18 +++++++++---------
 2 files changed, 13 insertions(+), 13 deletions(-)

-- 
2.17.1


^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH v2, 1/4] mm, memcg: Prevent memory.oom.group load/store tearing
@ 2023-03-06 15:41   ` Yue Zhao
  0 siblings, 0 replies; 23+ messages in thread
From: Yue Zhao @ 2023-03-06 15:41 UTC (permalink / raw)
  To: akpm
  Cc: roman.gushchin, hannes, mhocko, shakeelb, muchun.song, willy,
	linux-mm, cgroups, linux-kernel, tangyeechou, Yue Zhao

The knob for cgroup v2 memory controller: memory.oom.group
is not protected by any locking so it can be modified while it is used.
This is not an actual problem because races are unlikely (the knob is
usually configured long before any workloads hits actual memcg oom)
but it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
doing anything funky.

The access of memcg->oom_group is lockless, so it can be
concurrently set at the same time as we are trying to read it.

Signed-off-by: Yue Zhao <findns94@gmail.com>
---
 mm/memcontrol.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 5abffe6f8389..06821e5f7604 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2067,7 +2067,7 @@ struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
 	 * highest-level memory cgroup with oom.group set.
 	 */
 	for (; memcg; memcg = parent_mem_cgroup(memcg)) {
-		if (memcg->oom_group)
+		if (READ_ONCE(memcg->oom_group))
 			oom_group = memcg;
 
 		if (memcg == oom_domain)
@@ -6623,7 +6623,7 @@ static int memory_oom_group_show(struct seq_file *m, void *v)
 {
 	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
 
-	seq_printf(m, "%d\n", memcg->oom_group);
+	seq_printf(m, "%d\n", READ_ONCE(memcg->oom_group));
 
 	return 0;
 }
@@ -6645,7 +6645,7 @@ static ssize_t memory_oom_group_write(struct kernfs_open_file *of,
 	if (oom_group != 0 && oom_group != 1)
 		return -EINVAL;
 
-	memcg->oom_group = oom_group;
+	WRITE_ONCE(memcg->oom_group, oom_group);
 
 	return nbytes;
 }
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH v2, 1/4] mm, memcg: Prevent memory.oom.group load/store tearing
@ 2023-03-06 15:41   ` Yue Zhao
  0 siblings, 0 replies; 23+ messages in thread
From: Yue Zhao @ 2023-03-06 15:41 UTC (permalink / raw)
  To: akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b
  Cc: roman.gushchin-fxUVXftIFDnyG1zEObXtfA,
	hannes-druUgvl0LCNAfugRpC6u6w, mhocko-DgEjT+Ai2ygdnm+yROfE0A,
	shakeelb-hpIqsD4AKlfQT0dZR+AlfA,
	muchun.song-fxUVXftIFDnyG1zEObXtfA, willy-wEGCiKHe2LqWVfeAwA7xHQ,
	linux-mm-Bw31MaZKKs3YtjvyW6yDsg, cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	tangyeechou-Re5JQEeQqe8AvxtiuMwx3w, Yue Zhao

The knob for cgroup v2 memory controller: memory.oom.group
is not protected by any locking so it can be modified while it is used.
This is not an actual problem because races are unlikely (the knob is
usually configured long before any workloads hits actual memcg oom)
but it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
doing anything funky.

The access of memcg->oom_group is lockless, so it can be
concurrently set at the same time as we are trying to read it.

Signed-off-by: Yue Zhao <findns94-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
---
 mm/memcontrol.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 5abffe6f8389..06821e5f7604 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2067,7 +2067,7 @@ struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
 	 * highest-level memory cgroup with oom.group set.
 	 */
 	for (; memcg; memcg = parent_mem_cgroup(memcg)) {
-		if (memcg->oom_group)
+		if (READ_ONCE(memcg->oom_group))
 			oom_group = memcg;
 
 		if (memcg == oom_domain)
@@ -6623,7 +6623,7 @@ static int memory_oom_group_show(struct seq_file *m, void *v)
 {
 	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
 
-	seq_printf(m, "%d\n", memcg->oom_group);
+	seq_printf(m, "%d\n", READ_ONCE(memcg->oom_group));
 
 	return 0;
 }
@@ -6645,7 +6645,7 @@ static ssize_t memory_oom_group_write(struct kernfs_open_file *of,
 	if (oom_group != 0 && oom_group != 1)
 		return -EINVAL;
 
-	memcg->oom_group = oom_group;
+	WRITE_ONCE(memcg->oom_group, oom_group);
 
 	return nbytes;
 }
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH v2, 2/4] mm, memcg: Prevent memory.swappiness load/store tearing
  2023-03-06 15:41 ` Yue Zhao
  (?)
  (?)
@ 2023-03-06 15:41 ` Yue Zhao
  2023-03-06 17:50     ` Michal Hocko
  -1 siblings, 1 reply; 23+ messages in thread
From: Yue Zhao @ 2023-03-06 15:41 UTC (permalink / raw)
  To: akpm
  Cc: roman.gushchin, hannes, mhocko, shakeelb, muchun.song, willy,
	linux-mm, cgroups, linux-kernel, tangyeechou, Yue Zhao

The knob for cgroup v1 memory controller: memory.swappiness
is not protected by any locking so it can be modified while it is used.
This is not an actual problem because races are unlikely.
But it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
doing anything funky.

The access of memcg->swappiness and vm_swappiness is lockless,
so both of them can be concurrently set at the same time
as we are trying to read them. 

Signed-off-by: Yue Zhao <findns94@gmail.com>
---
 include/linux/swap.h | 8 ++++----
 mm/memcontrol.c      | 4 ++--
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/include/linux/swap.h b/include/linux/swap.h
index 209a425739a9..3f3fe43d1766 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -620,18 +620,18 @@ static inline int mem_cgroup_swappiness(struct mem_cgroup *memcg)
 {
 	/* Cgroup2 doesn't have per-cgroup swappiness */
 	if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
-		return vm_swappiness;
+		return READ_ONCE(vm_swappiness);
 
 	/* root ? */
 	if (mem_cgroup_disabled() || mem_cgroup_is_root(memcg))
-		return vm_swappiness;
+		return READ_ONCE(vm_swappiness);
 
-	return memcg->swappiness;
+	return READ_ONCE(memcg->swappiness);
 }
 #else
 static inline int mem_cgroup_swappiness(struct mem_cgroup *mem)
 {
-	return vm_swappiness;
+	return READ_ONCE(vm_swappiness);
 }
 #endif
 
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 06821e5f7604..dca895c66a9b 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -4179,9 +4179,9 @@ static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
 		return -EINVAL;
 
 	if (!mem_cgroup_is_root(memcg))
-		memcg->swappiness = val;
+		WRITE_ONCE(memcg->swappiness, val);
 	else
-		vm_swappiness = val;
+		WRITE_ONCE(vm_swappiness, val);
 
 	return 0;
 }
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH v2, 3/4] mm, memcg: Prevent memory.oom_control load/store tearing
@ 2023-03-06 15:41   ` Yue Zhao
  0 siblings, 0 replies; 23+ messages in thread
From: Yue Zhao @ 2023-03-06 15:41 UTC (permalink / raw)
  To: akpm
  Cc: roman.gushchin, hannes, mhocko, shakeelb, muchun.song, willy,
	linux-mm, cgroups, linux-kernel, tangyeechou, Yue Zhao

The knob for cgroup v1 memory controller: memory.oom_control
is not protected by any locking so it can be modified while it is used.
This is not an actual problem because races are unlikely.
But it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
doing anything funky.

The access of memcg->oom_kill_disable is lockless,
so it can be concurrently set at the same time as we are
trying to read it.

Signed-off-by: Yue Zhao <findns94@gmail.com>
---
 mm/memcontrol.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index dca895c66a9b..26605b2f51b1 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -4515,7 +4515,7 @@ static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
 {
 	struct mem_cgroup *memcg = mem_cgroup_from_seq(sf);
 
-	seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable);
+	seq_printf(sf, "oom_kill_disable %d\n", READ_ONCE(memcg->oom_kill_disable));
 	seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom);
 	seq_printf(sf, "oom_kill %lu\n",
 		   atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL]));
@@ -4531,7 +4531,7 @@ static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
 	if (mem_cgroup_is_root(memcg) || !((val == 0) || (val == 1)))
 		return -EINVAL;
 
-	memcg->oom_kill_disable = val;
+	WRITE_ONCE(memcg->oom_kill_disable, val);
 	if (!val)
 		memcg_oom_recover(memcg);
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH v2, 3/4] mm, memcg: Prevent memory.oom_control load/store tearing
@ 2023-03-06 15:41   ` Yue Zhao
  0 siblings, 0 replies; 23+ messages in thread
From: Yue Zhao @ 2023-03-06 15:41 UTC (permalink / raw)
  To: akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b
  Cc: roman.gushchin-fxUVXftIFDnyG1zEObXtfA,
	hannes-druUgvl0LCNAfugRpC6u6w, mhocko-DgEjT+Ai2ygdnm+yROfE0A,
	shakeelb-hpIqsD4AKlfQT0dZR+AlfA,
	muchun.song-fxUVXftIFDnyG1zEObXtfA, willy-wEGCiKHe2LqWVfeAwA7xHQ,
	linux-mm-Bw31MaZKKs3YtjvyW6yDsg, cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	tangyeechou-Re5JQEeQqe8AvxtiuMwx3w, Yue Zhao

The knob for cgroup v1 memory controller: memory.oom_control
is not protected by any locking so it can be modified while it is used.
This is not an actual problem because races are unlikely.
But it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
doing anything funky.

The access of memcg->oom_kill_disable is lockless,
so it can be concurrently set at the same time as we are
trying to read it.

Signed-off-by: Yue Zhao <findns94-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
---
 mm/memcontrol.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index dca895c66a9b..26605b2f51b1 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -4515,7 +4515,7 @@ static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
 {
 	struct mem_cgroup *memcg = mem_cgroup_from_seq(sf);
 
-	seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable);
+	seq_printf(sf, "oom_kill_disable %d\n", READ_ONCE(memcg->oom_kill_disable));
 	seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom);
 	seq_printf(sf, "oom_kill %lu\n",
 		   atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL]));
@@ -4531,7 +4531,7 @@ static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
 	if (mem_cgroup_is_root(memcg) || !((val == 0) || (val == 1)))
 		return -EINVAL;
 
-	memcg->oom_kill_disable = val;
+	WRITE_ONCE(memcg->oom_kill_disable, val);
 	if (!val)
 		memcg_oom_recover(memcg);
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH v2, 4/4] mm, memcg: Prevent memory.soft_limit_in_bytes load/store tearing
@ 2023-03-06 15:41   ` Yue Zhao
  0 siblings, 0 replies; 23+ messages in thread
From: Yue Zhao @ 2023-03-06 15:41 UTC (permalink / raw)
  To: akpm
  Cc: roman.gushchin, hannes, mhocko, shakeelb, muchun.song, willy,
	linux-mm, cgroups, linux-kernel, tangyeechou, Yue Zhao

The knob for cgroup v1 memory controller: memory.soft_limit_in_bytes
is not protected by any locking so it can be modified while it is used.
This is not an actual problem because races are unlikely.
But it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
doing anything funky.

The access of memcg->soft_limit is lockless,
so it can be concurrently set at the same time as we are
trying to read it.

Signed-off-by: Yue Zhao <findns94@gmail.com>
---
 mm/memcontrol.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 26605b2f51b1..20566f59bbcb 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -3728,7 +3728,7 @@ static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
 	case RES_FAILCNT:
 		return counter->failcnt;
 	case RES_SOFT_LIMIT:
-		return (u64)memcg->soft_limit * PAGE_SIZE;
+		return (u64)READ_ONCE(memcg->soft_limit) * PAGE_SIZE;
 	default:
 		BUG();
 	}
@@ -3870,7 +3870,7 @@ static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
 		if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
 			ret = -EOPNOTSUPP;
 		} else {
-			memcg->soft_limit = nr_pages;
+			WRITE_ONCE(memcg->soft_limit, nr_pages);
 			ret = 0;
 		}
 		break;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH v2, 4/4] mm, memcg: Prevent memory.soft_limit_in_bytes load/store tearing
@ 2023-03-06 15:41   ` Yue Zhao
  0 siblings, 0 replies; 23+ messages in thread
From: Yue Zhao @ 2023-03-06 15:41 UTC (permalink / raw)
  To: akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b
  Cc: roman.gushchin-fxUVXftIFDnyG1zEObXtfA,
	hannes-druUgvl0LCNAfugRpC6u6w, mhocko-DgEjT+Ai2ygdnm+yROfE0A,
	shakeelb-hpIqsD4AKlfQT0dZR+AlfA,
	muchun.song-fxUVXftIFDnyG1zEObXtfA, willy-wEGCiKHe2LqWVfeAwA7xHQ,
	linux-mm-Bw31MaZKKs3YtjvyW6yDsg, cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	tangyeechou-Re5JQEeQqe8AvxtiuMwx3w, Yue Zhao

The knob for cgroup v1 memory controller: memory.soft_limit_in_bytes
is not protected by any locking so it can be modified while it is used.
This is not an actual problem because races are unlikely.
But it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
doing anything funky.

The access of memcg->soft_limit is lockless,
so it can be concurrently set at the same time as we are
trying to read it.

Signed-off-by: Yue Zhao <findns94-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
---
 mm/memcontrol.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 26605b2f51b1..20566f59bbcb 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -3728,7 +3728,7 @@ static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
 	case RES_FAILCNT:
 		return counter->failcnt;
 	case RES_SOFT_LIMIT:
-		return (u64)memcg->soft_limit * PAGE_SIZE;
+		return (u64)READ_ONCE(memcg->soft_limit) * PAGE_SIZE;
 	default:
 		BUG();
 	}
@@ -3870,7 +3870,7 @@ static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
 		if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
 			ret = -EOPNOTSUPP;
 		} else {
-			memcg->soft_limit = nr_pages;
+			WRITE_ONCE(memcg->soft_limit, nr_pages);
 			ret = 0;
 		}
 		break;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* Re: [PATCH v2, 0/4] mm, memcg: cgroup v1 and v2 tunable load/store tearing fixes
@ 2023-03-06 16:25   ` Shakeel Butt
  0 siblings, 0 replies; 23+ messages in thread
From: Shakeel Butt @ 2023-03-06 16:25 UTC (permalink / raw)
  To: Yue Zhao
  Cc: akpm, roman.gushchin, hannes, mhocko, muchun.song, willy,
	linux-mm, cgroups, linux-kernel, tangyeechou

On Mon, Mar 6, 2023 at 7:42 AM Yue Zhao <findns94@gmail.com> wrote:
>
> This patch series helps to prevent load/store tearing in
> several cgroup knobs.
>
> As kindly pointed out by Michal Hocko and Roman Gushchin
> , the changelog has been rephrased.
>
> Besides, more knobs were checked, according to kind suggestions
> from Shakeel Butt and Muchun Song.
>
> v1:
> - Add [WRITE|READ]_ONCE for memcg->oom_group
> v2:
> - Rephrase changelog
> - Add [WRITE|READ]_ONCE for memcg->oom_kill_disable,
>  memcg->swappiness, vm_swappiness and memcg->soft_limit
>

Thanks Yue and for the whole series:

Acked-by: Shakeel Butt <shakeelb@google.com>

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH v2, 0/4] mm, memcg: cgroup v1 and v2 tunable load/store tearing fixes
@ 2023-03-06 16:25   ` Shakeel Butt
  0 siblings, 0 replies; 23+ messages in thread
From: Shakeel Butt @ 2023-03-06 16:25 UTC (permalink / raw)
  To: Yue Zhao
  Cc: akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b,
	roman.gushchin-fxUVXftIFDnyG1zEObXtfA,
	hannes-druUgvl0LCNAfugRpC6u6w, mhocko-DgEjT+Ai2ygdnm+yROfE0A,
	muchun.song-fxUVXftIFDnyG1zEObXtfA, willy-wEGCiKHe2LqWVfeAwA7xHQ,
	linux-mm-Bw31MaZKKs3YtjvyW6yDsg, cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	tangyeechou-Re5JQEeQqe8AvxtiuMwx3w

On Mon, Mar 6, 2023 at 7:42 AM Yue Zhao <findns94-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> This patch series helps to prevent load/store tearing in
> several cgroup knobs.
>
> As kindly pointed out by Michal Hocko and Roman Gushchin
> , the changelog has been rephrased.
>
> Besides, more knobs were checked, according to kind suggestions
> from Shakeel Butt and Muchun Song.
>
> v1:
> - Add [WRITE|READ]_ONCE for memcg->oom_group
> v2:
> - Rephrase changelog
> - Add [WRITE|READ]_ONCE for memcg->oom_kill_disable,
>  memcg->swappiness, vm_swappiness and memcg->soft_limit
>

Thanks Yue and for the whole series:

Acked-by: Shakeel Butt <shakeelb-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH v2, 1/4] mm, memcg: Prevent memory.oom.group load/store tearing
@ 2023-03-06 17:47     ` Michal Hocko
  0 siblings, 0 replies; 23+ messages in thread
From: Michal Hocko @ 2023-03-06 17:47 UTC (permalink / raw)
  To: Yue Zhao
  Cc: akpm, roman.gushchin, hannes, shakeelb, muchun.song, willy,
	linux-mm, cgroups, linux-kernel, tangyeechou

On Mon 06-03-23 23:41:35, Yue Zhao wrote:
> The knob for cgroup v2 memory controller: memory.oom.group
> is not protected by any locking so it can be modified while it is used.
> This is not an actual problem because races are unlikely (the knob is
> usually configured long before any workloads hits actual memcg oom)
> but it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
> doing anything funky.
> 
> The access of memcg->oom_group is lockless, so it can be
> concurrently set at the same time as we are trying to read it.
> 
> Signed-off-by: Yue Zhao <findns94@gmail.com>

Acked-by: Michal Hocko <mhocko@suse.com>
Thanks!

> ---
>  mm/memcontrol.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 5abffe6f8389..06821e5f7604 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -2067,7 +2067,7 @@ struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
>  	 * highest-level memory cgroup with oom.group set.
>  	 */
>  	for (; memcg; memcg = parent_mem_cgroup(memcg)) {
> -		if (memcg->oom_group)
> +		if (READ_ONCE(memcg->oom_group))
>  			oom_group = memcg;
>  
>  		if (memcg == oom_domain)
> @@ -6623,7 +6623,7 @@ static int memory_oom_group_show(struct seq_file *m, void *v)
>  {
>  	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
>  
> -	seq_printf(m, "%d\n", memcg->oom_group);
> +	seq_printf(m, "%d\n", READ_ONCE(memcg->oom_group));
>  
>  	return 0;
>  }
> @@ -6645,7 +6645,7 @@ static ssize_t memory_oom_group_write(struct kernfs_open_file *of,
>  	if (oom_group != 0 && oom_group != 1)
>  		return -EINVAL;
>  
> -	memcg->oom_group = oom_group;
> +	WRITE_ONCE(memcg->oom_group, oom_group);
>  
>  	return nbytes;
>  }
> -- 
> 2.17.1

-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH v2, 1/4] mm, memcg: Prevent memory.oom.group load/store tearing
@ 2023-03-06 17:47     ` Michal Hocko
  0 siblings, 0 replies; 23+ messages in thread
From: Michal Hocko @ 2023-03-06 17:47 UTC (permalink / raw)
  To: Yue Zhao
  Cc: akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b,
	roman.gushchin-fxUVXftIFDnyG1zEObXtfA,
	hannes-druUgvl0LCNAfugRpC6u6w, shakeelb-hpIqsD4AKlfQT0dZR+AlfA,
	muchun.song-fxUVXftIFDnyG1zEObXtfA, willy-wEGCiKHe2LqWVfeAwA7xHQ,
	linux-mm-Bw31MaZKKs3YtjvyW6yDsg, cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	tangyeechou-Re5JQEeQqe8AvxtiuMwx3w

On Mon 06-03-23 23:41:35, Yue Zhao wrote:
> The knob for cgroup v2 memory controller: memory.oom.group
> is not protected by any locking so it can be modified while it is used.
> This is not an actual problem because races are unlikely (the knob is
> usually configured long before any workloads hits actual memcg oom)
> but it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
> doing anything funky.
> 
> The access of memcg->oom_group is lockless, so it can be
> concurrently set at the same time as we are trying to read it.
> 
> Signed-off-by: Yue Zhao <findns94-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>

Acked-by: Michal Hocko <mhocko-IBi9RG/b67k@public.gmane.org>
Thanks!

> ---
>  mm/memcontrol.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 5abffe6f8389..06821e5f7604 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -2067,7 +2067,7 @@ struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
>  	 * highest-level memory cgroup with oom.group set.
>  	 */
>  	for (; memcg; memcg = parent_mem_cgroup(memcg)) {
> -		if (memcg->oom_group)
> +		if (READ_ONCE(memcg->oom_group))
>  			oom_group = memcg;
>  
>  		if (memcg == oom_domain)
> @@ -6623,7 +6623,7 @@ static int memory_oom_group_show(struct seq_file *m, void *v)
>  {
>  	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
>  
> -	seq_printf(m, "%d\n", memcg->oom_group);
> +	seq_printf(m, "%d\n", READ_ONCE(memcg->oom_group));
>  
>  	return 0;
>  }
> @@ -6645,7 +6645,7 @@ static ssize_t memory_oom_group_write(struct kernfs_open_file *of,
>  	if (oom_group != 0 && oom_group != 1)
>  		return -EINVAL;
>  
> -	memcg->oom_group = oom_group;
> +	WRITE_ONCE(memcg->oom_group, oom_group);
>  
>  	return nbytes;
>  }
> -- 
> 2.17.1

-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH v2, 2/4] mm, memcg: Prevent memory.swappiness load/store tearing
@ 2023-03-06 17:50     ` Michal Hocko
  0 siblings, 0 replies; 23+ messages in thread
From: Michal Hocko @ 2023-03-06 17:50 UTC (permalink / raw)
  To: Yue Zhao
  Cc: akpm, roman.gushchin, hannes, shakeelb, muchun.song, willy,
	linux-mm, cgroups, linux-kernel, tangyeechou

On Mon 06-03-23 23:41:36, Yue Zhao wrote:
> The knob for cgroup v1 memory controller: memory.swappiness
> is not protected by any locking so it can be modified while it is used.
> This is not an actual problem because races are unlikely.
> But it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
> doing anything funky.
> 
> The access of memcg->swappiness and vm_swappiness is lockless,
> so both of them can be concurrently set at the same time
> as we are trying to read them. 
> 
> Signed-off-by: Yue Zhao <findns94@gmail.com>

Acked-by: Michal Hocko <mhocko@suse.com>
Thanks!

> ---
>  include/linux/swap.h | 8 ++++----
>  mm/memcontrol.c      | 4 ++--
>  2 files changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/include/linux/swap.h b/include/linux/swap.h
> index 209a425739a9..3f3fe43d1766 100644
> --- a/include/linux/swap.h
> +++ b/include/linux/swap.h
> @@ -620,18 +620,18 @@ static inline int mem_cgroup_swappiness(struct mem_cgroup *memcg)
>  {
>  	/* Cgroup2 doesn't have per-cgroup swappiness */
>  	if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
> -		return vm_swappiness;
> +		return READ_ONCE(vm_swappiness);
>  
>  	/* root ? */
>  	if (mem_cgroup_disabled() || mem_cgroup_is_root(memcg))
> -		return vm_swappiness;
> +		return READ_ONCE(vm_swappiness);
>  
> -	return memcg->swappiness;
> +	return READ_ONCE(memcg->swappiness);
>  }
>  #else
>  static inline int mem_cgroup_swappiness(struct mem_cgroup *mem)
>  {
> -	return vm_swappiness;
> +	return READ_ONCE(vm_swappiness);
>  }
>  #endif
>  
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 06821e5f7604..dca895c66a9b 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -4179,9 +4179,9 @@ static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
>  		return -EINVAL;
>  
>  	if (!mem_cgroup_is_root(memcg))
> -		memcg->swappiness = val;
> +		WRITE_ONCE(memcg->swappiness, val);
>  	else
> -		vm_swappiness = val;
> +		WRITE_ONCE(vm_swappiness, val);
>  
>  	return 0;
>  }
> -- 
> 2.17.1

-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH v2, 2/4] mm, memcg: Prevent memory.swappiness load/store tearing
@ 2023-03-06 17:50     ` Michal Hocko
  0 siblings, 0 replies; 23+ messages in thread
From: Michal Hocko @ 2023-03-06 17:50 UTC (permalink / raw)
  To: Yue Zhao
  Cc: akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b,
	roman.gushchin-fxUVXftIFDnyG1zEObXtfA,
	hannes-druUgvl0LCNAfugRpC6u6w, shakeelb-hpIqsD4AKlfQT0dZR+AlfA,
	muchun.song-fxUVXftIFDnyG1zEObXtfA, willy-wEGCiKHe2LqWVfeAwA7xHQ,
	linux-mm-Bw31MaZKKs3YtjvyW6yDsg, cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	tangyeechou-Re5JQEeQqe8AvxtiuMwx3w

On Mon 06-03-23 23:41:36, Yue Zhao wrote:
> The knob for cgroup v1 memory controller: memory.swappiness
> is not protected by any locking so it can be modified while it is used.
> This is not an actual problem because races are unlikely.
> But it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
> doing anything funky.
> 
> The access of memcg->swappiness and vm_swappiness is lockless,
> so both of them can be concurrently set at the same time
> as we are trying to read them. 
> 
> Signed-off-by: Yue Zhao <findns94-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>

Acked-by: Michal Hocko <mhocko-IBi9RG/b67k@public.gmane.org>
Thanks!

> ---
>  include/linux/swap.h | 8 ++++----
>  mm/memcontrol.c      | 4 ++--
>  2 files changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/include/linux/swap.h b/include/linux/swap.h
> index 209a425739a9..3f3fe43d1766 100644
> --- a/include/linux/swap.h
> +++ b/include/linux/swap.h
> @@ -620,18 +620,18 @@ static inline int mem_cgroup_swappiness(struct mem_cgroup *memcg)
>  {
>  	/* Cgroup2 doesn't have per-cgroup swappiness */
>  	if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
> -		return vm_swappiness;
> +		return READ_ONCE(vm_swappiness);
>  
>  	/* root ? */
>  	if (mem_cgroup_disabled() || mem_cgroup_is_root(memcg))
> -		return vm_swappiness;
> +		return READ_ONCE(vm_swappiness);
>  
> -	return memcg->swappiness;
> +	return READ_ONCE(memcg->swappiness);
>  }
>  #else
>  static inline int mem_cgroup_swappiness(struct mem_cgroup *mem)
>  {
> -	return vm_swappiness;
> +	return READ_ONCE(vm_swappiness);
>  }
>  #endif
>  
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 06821e5f7604..dca895c66a9b 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -4179,9 +4179,9 @@ static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
>  		return -EINVAL;
>  
>  	if (!mem_cgroup_is_root(memcg))
> -		memcg->swappiness = val;
> +		WRITE_ONCE(memcg->swappiness, val);
>  	else
> -		vm_swappiness = val;
> +		WRITE_ONCE(vm_swappiness, val);
>  
>  	return 0;
>  }
> -- 
> 2.17.1

-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH v2, 0/4] mm, memcg: cgroup v1 and v2 tunable load/store tearing fixes
@ 2023-03-06 17:51   ` Roman Gushchin
  0 siblings, 0 replies; 23+ messages in thread
From: Roman Gushchin @ 2023-03-06 17:51 UTC (permalink / raw)
  To: Yue Zhao
  Cc: akpm, hannes, mhocko, shakeelb, muchun.song, willy, linux-mm,
	cgroups, linux-kernel, tangyeechou

On Mon, Mar 06, 2023 at 11:41:34PM +0800, Yue Zhao wrote:
> This patch series helps to prevent load/store tearing in
> several cgroup knobs.
> 
> As kindly pointed out by Michal Hocko and Roman Gushchin
> , the changelog has been rephrased.
> 
> Besides, more knobs were checked, according to kind suggestions
> from Shakeel Butt and Muchun Song.
> 
> v1:
> - Add [WRITE|READ]_ONCE for memcg->oom_group
> v2:
> - Rephrase changelog
> - Add [WRITE|READ]_ONCE for memcg->oom_kill_disable,
>  memcg->swappiness, vm_swappiness and memcg->soft_limit
> 
> Yue Zhao (4):
>   mm, memcg: Prevent memory.oom.group load/store tearing
>   mm, memcg: Prevent memory.swappiness load/store tearing
>   mm, memcg: Prevent memory.oom_control load/store tearing
>   mm, memcg: Prevent memory.soft_limit_in_bytes load/store tearing

Acked-by: Roman Gushchin <roman.gushchin@linux.dev>
for the series.

Thank you!

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH v2, 0/4] mm, memcg: cgroup v1 and v2 tunable load/store tearing fixes
@ 2023-03-06 17:51   ` Roman Gushchin
  0 siblings, 0 replies; 23+ messages in thread
From: Roman Gushchin @ 2023-03-06 17:51 UTC (permalink / raw)
  To: Yue Zhao
  Cc: akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b,
	hannes-druUgvl0LCNAfugRpC6u6w, mhocko-DgEjT+Ai2ygdnm+yROfE0A,
	shakeelb-hpIqsD4AKlfQT0dZR+AlfA,
	muchun.song-fxUVXftIFDnyG1zEObXtfA, willy-wEGCiKHe2LqWVfeAwA7xHQ,
	linux-mm-Bw31MaZKKs3YtjvyW6yDsg, cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	tangyeechou-Re5JQEeQqe8AvxtiuMwx3w

On Mon, Mar 06, 2023 at 11:41:34PM +0800, Yue Zhao wrote:
> This patch series helps to prevent load/store tearing in
> several cgroup knobs.
> 
> As kindly pointed out by Michal Hocko and Roman Gushchin
> , the changelog has been rephrased.
> 
> Besides, more knobs were checked, according to kind suggestions
> from Shakeel Butt and Muchun Song.
> 
> v1:
> - Add [WRITE|READ]_ONCE for memcg->oom_group
> v2:
> - Rephrase changelog
> - Add [WRITE|READ]_ONCE for memcg->oom_kill_disable,
>  memcg->swappiness, vm_swappiness and memcg->soft_limit
> 
> Yue Zhao (4):
>   mm, memcg: Prevent memory.oom.group load/store tearing
>   mm, memcg: Prevent memory.swappiness load/store tearing
>   mm, memcg: Prevent memory.oom_control load/store tearing
>   mm, memcg: Prevent memory.soft_limit_in_bytes load/store tearing

Acked-by: Roman Gushchin <roman.gushchin-fxUVXftIFDnyG1zEObXtfA@public.gmane.org>
for the series.

Thank you!

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH v2, 3/4] mm, memcg: Prevent memory.oom_control load/store tearing
@ 2023-03-06 17:53     ` Michal Hocko
  0 siblings, 0 replies; 23+ messages in thread
From: Michal Hocko @ 2023-03-06 17:53 UTC (permalink / raw)
  To: Yue Zhao
  Cc: akpm, roman.gushchin, hannes, shakeelb, muchun.song, willy,
	linux-mm, cgroups, linux-kernel, tangyeechou

On Mon 06-03-23 23:41:37, Yue Zhao wrote:
> The knob for cgroup v1 memory controller: memory.oom_control
> is not protected by any locking so it can be modified while it is used.
> This is not an actual problem because races are unlikely.
> But it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
> doing anything funky.
> 
> The access of memcg->oom_kill_disable is lockless,
> so it can be concurrently set at the same time as we are
> trying to read it.
> 
> Signed-off-by: Yue Zhao <findns94@gmail.com>
> ---
>  mm/memcontrol.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index dca895c66a9b..26605b2f51b1 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -4515,7 +4515,7 @@ static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
>  {
>  	struct mem_cgroup *memcg = mem_cgroup_from_seq(sf);
>  
> -	seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable);
> +	seq_printf(sf, "oom_kill_disable %d\n", READ_ONCE(memcg->oom_kill_disable));
>  	seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom);
>  	seq_printf(sf, "oom_kill %lu\n",
>  		   atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL]));
> @@ -4531,7 +4531,7 @@ static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
>  	if (mem_cgroup_is_root(memcg) || !((val == 0) || (val == 1)))
>  		return -EINVAL;
>  
> -	memcg->oom_kill_disable = val;
> +	WRITE_ONCE(memcg->oom_kill_disable, val);
>  	if (!val)
>  		memcg_oom_recover(memcg);

Any specific reasons you haven't covered other accesses
(mem_cgroup_css_alloc, mem_cgroup_oom, mem_cgroup_oom_synchronize)?
>  
> -- 
> 2.17.1

-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH v2, 3/4] mm, memcg: Prevent memory.oom_control load/store tearing
@ 2023-03-06 17:53     ` Michal Hocko
  0 siblings, 0 replies; 23+ messages in thread
From: Michal Hocko @ 2023-03-06 17:53 UTC (permalink / raw)
  To: Yue Zhao
  Cc: akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b,
	roman.gushchin-fxUVXftIFDnyG1zEObXtfA,
	hannes-druUgvl0LCNAfugRpC6u6w, shakeelb-hpIqsD4AKlfQT0dZR+AlfA,
	muchun.song-fxUVXftIFDnyG1zEObXtfA, willy-wEGCiKHe2LqWVfeAwA7xHQ,
	linux-mm-Bw31MaZKKs3YtjvyW6yDsg, cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	tangyeechou-Re5JQEeQqe8AvxtiuMwx3w

On Mon 06-03-23 23:41:37, Yue Zhao wrote:
> The knob for cgroup v1 memory controller: memory.oom_control
> is not protected by any locking so it can be modified while it is used.
> This is not an actual problem because races are unlikely.
> But it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
> doing anything funky.
> 
> The access of memcg->oom_kill_disable is lockless,
> so it can be concurrently set at the same time as we are
> trying to read it.
> 
> Signed-off-by: Yue Zhao <findns94-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> ---
>  mm/memcontrol.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index dca895c66a9b..26605b2f51b1 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -4515,7 +4515,7 @@ static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
>  {
>  	struct mem_cgroup *memcg = mem_cgroup_from_seq(sf);
>  
> -	seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable);
> +	seq_printf(sf, "oom_kill_disable %d\n", READ_ONCE(memcg->oom_kill_disable));
>  	seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom);
>  	seq_printf(sf, "oom_kill %lu\n",
>  		   atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL]));
> @@ -4531,7 +4531,7 @@ static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
>  	if (mem_cgroup_is_root(memcg) || !((val == 0) || (val == 1)))
>  		return -EINVAL;
>  
> -	memcg->oom_kill_disable = val;
> +	WRITE_ONCE(memcg->oom_kill_disable, val);
>  	if (!val)
>  		memcg_oom_recover(memcg);

Any specific reasons you haven't covered other accesses
(mem_cgroup_css_alloc, mem_cgroup_oom, mem_cgroup_oom_synchronize)?
>  
> -- 
> 2.17.1

-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH v2, 4/4] mm, memcg: Prevent memory.soft_limit_in_bytes load/store tearing
@ 2023-03-06 17:55     ` Michal Hocko
  0 siblings, 0 replies; 23+ messages in thread
From: Michal Hocko @ 2023-03-06 17:55 UTC (permalink / raw)
  To: Yue Zhao
  Cc: akpm, roman.gushchin, hannes, shakeelb, muchun.song, willy,
	linux-mm, cgroups, linux-kernel, tangyeechou

On Mon 06-03-23 23:41:38, Yue Zhao wrote:
> The knob for cgroup v1 memory controller: memory.soft_limit_in_bytes
> is not protected by any locking so it can be modified while it is used.
> This is not an actual problem because races are unlikely.
> But it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
> doing anything funky.
> 
> The access of memcg->soft_limit is lockless,
> so it can be concurrently set at the same time as we are
> trying to read it.

Similar here. mem_cgroup_css_reset and mem_cgroup_css_alloc are not
covered.
> 
> Signed-off-by: Yue Zhao <findns94@gmail.com>
> ---
>  mm/memcontrol.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 26605b2f51b1..20566f59bbcb 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -3728,7 +3728,7 @@ static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
>  	case RES_FAILCNT:
>  		return counter->failcnt;
>  	case RES_SOFT_LIMIT:
> -		return (u64)memcg->soft_limit * PAGE_SIZE;
> +		return (u64)READ_ONCE(memcg->soft_limit) * PAGE_SIZE;
>  	default:
>  		BUG();
>  	}
> @@ -3870,7 +3870,7 @@ static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
>  		if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
>  			ret = -EOPNOTSUPP;
>  		} else {
> -			memcg->soft_limit = nr_pages;
> +			WRITE_ONCE(memcg->soft_limit, nr_pages);
>  			ret = 0;
>  		}
>  		break;
> -- 
> 2.17.1

-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH v2, 4/4] mm, memcg: Prevent memory.soft_limit_in_bytes load/store tearing
@ 2023-03-06 17:55     ` Michal Hocko
  0 siblings, 0 replies; 23+ messages in thread
From: Michal Hocko @ 2023-03-06 17:55 UTC (permalink / raw)
  To: Yue Zhao
  Cc: akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b,
	roman.gushchin-fxUVXftIFDnyG1zEObXtfA,
	hannes-druUgvl0LCNAfugRpC6u6w, shakeelb-hpIqsD4AKlfQT0dZR+AlfA,
	muchun.song-fxUVXftIFDnyG1zEObXtfA, willy-wEGCiKHe2LqWVfeAwA7xHQ,
	linux-mm-Bw31MaZKKs3YtjvyW6yDsg, cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	tangyeechou-Re5JQEeQqe8AvxtiuMwx3w

On Mon 06-03-23 23:41:38, Yue Zhao wrote:
> The knob for cgroup v1 memory controller: memory.soft_limit_in_bytes
> is not protected by any locking so it can be modified while it is used.
> This is not an actual problem because races are unlikely.
> But it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
> doing anything funky.
> 
> The access of memcg->soft_limit is lockless,
> so it can be concurrently set at the same time as we are
> trying to read it.

Similar here. mem_cgroup_css_reset and mem_cgroup_css_alloc are not
covered.
> 
> Signed-off-by: Yue Zhao <findns94-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> ---
>  mm/memcontrol.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 26605b2f51b1..20566f59bbcb 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -3728,7 +3728,7 @@ static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
>  	case RES_FAILCNT:
>  		return counter->failcnt;
>  	case RES_SOFT_LIMIT:
> -		return (u64)memcg->soft_limit * PAGE_SIZE;
> +		return (u64)READ_ONCE(memcg->soft_limit) * PAGE_SIZE;
>  	default:
>  		BUG();
>  	}
> @@ -3870,7 +3870,7 @@ static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
>  		if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
>  			ret = -EOPNOTSUPP;
>  		} else {
> -			memcg->soft_limit = nr_pages;
> +			WRITE_ONCE(memcg->soft_limit, nr_pages);
>  			ret = 0;
>  		}
>  		break;
> -- 
> 2.17.1

-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH v2, 3/4] mm, memcg: Prevent memory.oom_control load/store tearing
@ 2023-03-08 14:51       ` Martin Zhao
  0 siblings, 0 replies; 23+ messages in thread
From: Martin Zhao @ 2023-03-08 14:51 UTC (permalink / raw)
  To: Michal Hocko
  Cc: akpm, roman.gushchin, hannes, shakeelb, muchun.song, willy,
	linux-mm, cgroups, linux-kernel, tangyeechou

On Tue, Mar 7, 2023 at 1:53 AM Michal Hocko <mhocko@suse.com> wrote:
>
> On Mon 06-03-23 23:41:37, Yue Zhao wrote:
> > The knob for cgroup v1 memory controller: memory.oom_control
> > is not protected by any locking so it can be modified while it is used.
> > This is not an actual problem because races are unlikely.
> > But it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
> > doing anything funky.
> >
> > The access of memcg->oom_kill_disable is lockless,
> > so it can be concurrently set at the same time as we are
> > trying to read it.
> >
> > Signed-off-by: Yue Zhao <findns94@gmail.com>
> > ---
> >  mm/memcontrol.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> > index dca895c66a9b..26605b2f51b1 100644
> > --- a/mm/memcontrol.c
> > +++ b/mm/memcontrol.c
> > @@ -4515,7 +4515,7 @@ static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
> >  {
> >       struct mem_cgroup *memcg = mem_cgroup_from_seq(sf);
> >
> > -     seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable);
> > +     seq_printf(sf, "oom_kill_disable %d\n", READ_ONCE(memcg->oom_kill_disable));
> >       seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom);
> >       seq_printf(sf, "oom_kill %lu\n",
> >                  atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL]));
> > @@ -4531,7 +4531,7 @@ static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
> >       if (mem_cgroup_is_root(memcg) || !((val == 0) || (val == 1)))
> >               return -EINVAL;
> >
> > -     memcg->oom_kill_disable = val;
> > +     WRITE_ONCE(memcg->oom_kill_disable, val);
> >       if (!val)
> >               memcg_oom_recover(memcg);
>
> Any specific reasons you haven't covered other accesses
> (mem_cgroup_css_alloc, mem_cgroup_oom, mem_cgroup_oom_synchronize)?

Thanks for point this out, you are right, we should add
[READ|WRITE]_ONCE for all used places.
Let me create PATCH v3 later.
Also for the memcg->soft_limit, I will update as well.

> >
> > --
> > 2.17.1
>
> --
> Michal Hocko
> SUSE Labs

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH v2, 3/4] mm, memcg: Prevent memory.oom_control load/store tearing
@ 2023-03-08 14:51       ` Martin Zhao
  0 siblings, 0 replies; 23+ messages in thread
From: Martin Zhao @ 2023-03-08 14:51 UTC (permalink / raw)
  To: Michal Hocko
  Cc: akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b,
	roman.gushchin-fxUVXftIFDnyG1zEObXtfA,
	hannes-druUgvl0LCNAfugRpC6u6w, shakeelb-hpIqsD4AKlfQT0dZR+AlfA,
	muchun.song-fxUVXftIFDnyG1zEObXtfA, willy-wEGCiKHe2LqWVfeAwA7xHQ,
	linux-mm-Bw31MaZKKs3YtjvyW6yDsg, cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	tangyeechou-Re5JQEeQqe8AvxtiuMwx3w

On Tue, Mar 7, 2023 at 1:53 AM Michal Hocko <mhocko-IBi9RG/b67k@public.gmane.org> wrote:
>
> On Mon 06-03-23 23:41:37, Yue Zhao wrote:
> > The knob for cgroup v1 memory controller: memory.oom_control
> > is not protected by any locking so it can be modified while it is used.
> > This is not an actual problem because races are unlikely.
> > But it is better to use READ_ONCE/WRITE_ONCE to prevent compiler from
> > doing anything funky.
> >
> > The access of memcg->oom_kill_disable is lockless,
> > so it can be concurrently set at the same time as we are
> > trying to read it.
> >
> > Signed-off-by: Yue Zhao <findns94-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> > ---
> >  mm/memcontrol.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> > index dca895c66a9b..26605b2f51b1 100644
> > --- a/mm/memcontrol.c
> > +++ b/mm/memcontrol.c
> > @@ -4515,7 +4515,7 @@ static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
> >  {
> >       struct mem_cgroup *memcg = mem_cgroup_from_seq(sf);
> >
> > -     seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable);
> > +     seq_printf(sf, "oom_kill_disable %d\n", READ_ONCE(memcg->oom_kill_disable));
> >       seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom);
> >       seq_printf(sf, "oom_kill %lu\n",
> >                  atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL]));
> > @@ -4531,7 +4531,7 @@ static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
> >       if (mem_cgroup_is_root(memcg) || !((val == 0) || (val == 1)))
> >               return -EINVAL;
> >
> > -     memcg->oom_kill_disable = val;
> > +     WRITE_ONCE(memcg->oom_kill_disable, val);
> >       if (!val)
> >               memcg_oom_recover(memcg);
>
> Any specific reasons you haven't covered other accesses
> (mem_cgroup_css_alloc, mem_cgroup_oom, mem_cgroup_oom_synchronize)?

Thanks for point this out, you are right, we should add
[READ|WRITE]_ONCE for all used places.
Let me create PATCH v3 later.
Also for the memcg->soft_limit, I will update as well.

> >
> > --
> > 2.17.1
>
> --
> Michal Hocko
> SUSE Labs

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2023-03-08 14:52 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-06 15:41 [PATCH v2, 0/4] mm, memcg: cgroup v1 and v2 tunable load/store tearing fixes Yue Zhao
2023-03-06 15:41 ` Yue Zhao
2023-03-06 15:41 ` [PATCH v2, 1/4] mm, memcg: Prevent memory.oom.group load/store tearing Yue Zhao
2023-03-06 15:41   ` Yue Zhao
2023-03-06 17:47   ` Michal Hocko
2023-03-06 17:47     ` Michal Hocko
2023-03-06 15:41 ` [PATCH v2, 2/4] mm, memcg: Prevent memory.swappiness " Yue Zhao
2023-03-06 17:50   ` Michal Hocko
2023-03-06 17:50     ` Michal Hocko
2023-03-06 15:41 ` [PATCH v2, 3/4] mm, memcg: Prevent memory.oom_control " Yue Zhao
2023-03-06 15:41   ` Yue Zhao
2023-03-06 17:53   ` Michal Hocko
2023-03-06 17:53     ` Michal Hocko
2023-03-08 14:51     ` Martin Zhao
2023-03-08 14:51       ` Martin Zhao
2023-03-06 15:41 ` [PATCH v2, 4/4] mm, memcg: Prevent memory.soft_limit_in_bytes " Yue Zhao
2023-03-06 15:41   ` Yue Zhao
2023-03-06 17:55   ` Michal Hocko
2023-03-06 17:55     ` Michal Hocko
2023-03-06 16:25 ` [PATCH v2, 0/4] mm, memcg: cgroup v1 and v2 tunable load/store tearing fixes Shakeel Butt
2023-03-06 16:25   ` Shakeel Butt
2023-03-06 17:51 ` Roman Gushchin
2023-03-06 17:51   ` Roman Gushchin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.