Skip to contentSkip to conversation

memcg: Fix race condition in memcg_check_events() with this_cpu usage

PATCH In review Steven Rostedt 6 participants 8 messages Sep 24, 2011 – Sep 27, 2011

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

PATCH 1/1 memcg: Fix race condition in memcg_check_events() with this_cpu usage

From: Steven Rostedt <[email protected]>

The code in memcg_check_events() calls this_cpu_read() on different variables without disabling preemption, and can cause the calculations to be done from two different CPU variables.

Disable preemption throughout the check to keep apples and oranges from becoming a mixed drink.

[ Added this_cpu to __this_cpu conversion by Johannes ]

Cc Greg Thelen <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Balbir Singh <[email protected]>
Cc Daisuke Nishimura <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Christoph Lameter <[email protected]>
---
 mm/memcontrol.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)
mm/memcontrol.c +6 -4
index 3508777..f823fc8 100644
@@ -683,8 +683,8 @@ static bool __memcg_event_check(struct mem_cgroup *mem, int target)
 {
 	unsigned long val, next;
 
-	val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]);
-	next = this_cpu_read(mem->stat->targets[target]);
+	val = __this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]);
+	next = __this_cpu_read(mem->stat->targets[target]);
 	/* from time_after() in jiffies.h */
 	return ((long)next - (long)val < 0);
 }
@@ -693,7 +693,7 @@ static void __mem_cgroup_target_update(struct mem_cgroup *mem, int target)
 {
 	unsigned long val, next;
 
-	val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]);
+	val = __this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]);
 
 	switch (target) {
 	case MEM_CGROUP_TARGET_THRESH:
@@ -709,7 +709,7 @@ static void __mem_cgroup_target_update(struct mem_cgroup *mem, int target)
 		return;
 	}
 
-	this_cpu_write(mem->stat->targets[target], next);
+	__this_cpu_write(mem->stat->targets[target], next);
 }
 
 /*
@@ -718,6 +718,7 @@ static void __mem_cgroup_target_update(struct mem_cgroup *mem, int target)
  */
 static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
 {
+	preempt_disable();
 	/* threshold event is triggered in finer grain than soft limit */
 	if (unlikely(__memcg_event_check(mem, MEM_CGROUP_TARGET_THRESH))) {
 		mem_cgroup_threshold(mem);
@@ -737,6 +738,7 @@ static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
 		}
 #endif
 	}
+	preempt_enable();
 }
 
 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Signature · 1 line
1.7.3.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Steven Rostedt

From: Steven Rostedt <[email protected]>

The code in memcg_check_events() calls this_cpu_read() on different variables without disabling preemption, and can cause the calculations to be done from two different CPU variables.

Disable preemption throughout the check to keep apples and oranges from becoming a mixed drink.

[ Added this_cpu to __this_cpu conversion by Johannes ]

Quoted 11 lines
Cc Greg Thelen <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Balbir Singh <[email protected]>
Cc Daisuke Nishimura <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Christoph Lameter <[email protected]>

Looks good to me. Thanks.

#2 lore raw
cc updated Balbir Singh email address
Quoted 7 lines · Greg Thelen
Quoted 6 lines · Steven Rostedt

From: Steven Rostedt <[email protected]>

The code in memcg_check_events() calls this_cpu_read() on different variables without disabling preemption, and can cause the calculations to be done from two different CPU variables.

Quoted 8 lines
Quoted 8 lines

Disable preemption throughout the check to keep apples and oranges from becoming a mixed drink.

[ Added this_cpu to __this_cpu conversion by Johannes ]

Cc Greg Thelen <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Balbir Singh <[email protected]>

Correction:
Cc: Balbir Singh <[email protected]>

Quoted 13 lines
Quoted 8 lines

Looks good to me.  Thanks.

#3 lore raw
Quoted 13 lines · Greg Thelen
cc updated Balbir Singh email address
Quoted 10 lines · Greg Thelen
Quoted 9 lines · Steven Rostedt

From: Steven Rostedt <[email protected]>

The code in memcg_check_events() calls this_cpu_read() on different variables without disabling preemption, and can cause the calculations to be done from two different CPU variables.

Disable preemption throughout the check to keep apples and oranges from becoming a mixed drink.

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

[ Added this_cpu to __this_cpu conversion by Johannes ]

Cc Greg Thelen <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Balbir Singh <[email protected]>

Correction:
Cc: Balbir Singh <[email protected]>

Thanks, I was starting to hate that "return mail to sender" thing.

-- Steve

Quoted 14 lines
Quoted 14 lines
Quoted 9 lines

Looks good to me. Thanks.

Acked-by
#4 lore raw
Quoted 16 lines · Steven Rostedt
Quoted 15 lines · Greg Thelen
cc updated Balbir Singh email address
Quoted 12 lines · Greg Thelen
Quoted 11 lines · Steven Rostedt

From: Steven Rostedt <[email protected]>

The code in memcg_check_events() calls this_cpu_read() on different variables without disabling preemption, and can cause the calculations to be done from two different CPU variables.

Disable preemption throughout the check to keep apples and oranges from becoming a mixed drink.

[ Added this_cpu to __this_cpu conversion by Johannes ]

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Cc Greg Thelen <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Balbir Singh <[email protected]>

Correction:
Cc: Balbir Singh <[email protected]>

Thanks, I was starting to hate that "return mail to sender" thing.

Thanks for the correction. The change looks good to me

Balbir

Balbir Singh

Acked-by on v1 1/1
#6 lore raw
In reply to Steven Rostedt
Quoted 11 lines · On Fri, 23 Sep 2011 20:54:42 -0400 Steven Rostedt <[email protected]> wrote:

From: Steven Rostedt <[email protected]>

The code in memcg_check_events() calls this_cpu_read() on different variables without disabling preemption, and can cause the calculations to be done from two different CPU variables.

Disable preemption throughout the check to keep apples and oranges from becoming a mixed drink.

[ Added this_cpu to __this_cpu conversion by Johannes ]

Quoted 11 lines
Cc Greg Thelen <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Balbir Singh <[email protected]>
Cc Daisuke Nishimura <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Christoph Lameter <[email protected]>

Andrew, could you pick this up ?

#7 lore raw
Quoted 26 lines · On Mon, 26 Sep 2011 09:43:22 +0900 KAMEZAWA Hiroyuki…
Quoted 11 lines · On Fri, 23 Sep 2011 20:54:42 -0400 Steven Rostedt <[email protected]> wrote:

From: Steven Rostedt <[email protected]>

The code in memcg_check_events() calls this_cpu_read() on different variables without disabling preemption, and can cause the calculations to be done from two different CPU variables.

Disable preemption throughout the check to keep apples and oranges from becoming a mixed drink.

[ Added this_cpu to __this_cpu conversion by Johannes ]

Quoted 11 lines
Cc Greg Thelen <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Balbir Singh <[email protected]>
Cc Daisuke Nishimura <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Christoph Lameter <[email protected]>
Quoted 3 lines

Andrew, could you pick this up ?

The patch needed rework due to other changes we have pending in there.

From: Steven Rostedt <[email protected]>
Subject: memcg: Fix race condition in memcg_check_events() with this_cpu usage

Various code in memcontrol.c () calls this_cpu_read() on the calculations to be done from two different percpu variables, or does an open-coded read-modify-write on a single percpu variable.

Disable preemption throughout these operations so that the writes go to the correct palces.

[ Added this_cpu to __this_cpu conversion by Johannes ]

Cc Greg Thelen <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Balbir Singh <[email protected]>
Cc Daisuke Nishimura <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Christoph Lameter <[email protected]>

---

 mm/memcontrol.c |   10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
mm/memcontrol.c renamed from mm/memcontrol.c~memcg-fix-race-condition-in-memcg_check_events-with-this_cpu-usage +6 -4
diff -puN mm/memcontrol.c~memcg-fix-race-condition-in-memcg_check_events-with-this_cpu-usage mm/memcontrol.c
@@ -687,8 +687,8 @@ static bool __memcg_event_check(struct m
 {
 	unsigned long val, next;
 
-	val = this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]);
-	next = this_cpu_read(memcg->stat->targets[target]);
+	val = __this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]);
+	next = __this_cpu_read(memcg->stat->targets[target]);
 	/* from time_after() in jiffies.h */
 	return ((long)next - (long)val < 0);
 }
@@ -697,7 +697,7 @@ static void __mem_cgroup_target_update(s
 {
 	unsigned long val, next;
 
-	val = this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]);
+	val = __this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]);
 
 	switch (target) {
 	case MEM_CGROUP_TARGET_THRESH:
@@ -713,7 +713,7 @@ static void __mem_cgroup_target_update(s
 		return;
 	}
 
-	this_cpu_write(memcg->stat->targets[target], next);
+	__this_cpu_write(memcg->stat->targets[target], next);
 }
 
 /*
@@ -722,6 +722,7 @@ static void __mem_cgroup_target_update(s
  */
 static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
 {
+	preempt_disable();
 	/* threshold event is triggered in finer grain than soft limit */
 	if (unlikely(__memcg_event_check(memcg, MEM_CGROUP_TARGET_THRESH))) {
 		mem_cgroup_threshold(memcg);
@@ -741,6 +742,7 @@ static void memcg_check_events(struct me
 		}
 #endif
 	}
+	preempt_enable();
 }
 
 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)

_