]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
memcg: Fix race condition in memcg_check_events() with this_cpu usage
authorSteven Rostedt <srostedt@redhat.com>
Wed, 28 Sep 2011 00:50:57 +0000 (10:50 +1000)
committerStephen Rothwell <sfr@canb.auug.org.au>
Fri, 30 Sep 2011 04:53:45 +0000 (14:53 +1000)
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 ]

Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
Cc: Greg Thelen <gthelen@google.com>
Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: Balbir Singh <balbir@linux.vnet.ibm.com>
Cc: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Christoph Lameter <cl@linux.com>
Signed-off-by: Andrew Morton <>
mm/memcontrol.c

index 44f4311c07221f36b2a009d7b01acc19c4128c2f..6aff93c98acaf6020eabd37f2e9b49a04135f187 100644 (file)
@@ -687,8 +687,8 @@ static bool __memcg_event_check(struct mem_cgroup *memcg, int target)
 {
        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(struct mem_cgroup *memcg, int target)
 {
        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(struct mem_cgroup *memcg, int target)
                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(struct mem_cgroup *memcg, int target)
  */
 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 mem_cgroup *memcg, struct page *page)
                }
 #endif
        }
+       preempt_enable();
 }
 
 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)