Commit ebb76ce16daf6908dc030dec1c00827d37129fe5

Authored by KAMEZAWA Hiroyuki
Committed by Linus Torvalds
1 parent b83be6f20a

memcg: fix wrong VM_BUG_ON() in try_charge()'s mm->owner check

At __mem_cgroup_try_charge(), VM_BUG_ON(!mm->owner) is checked.
But as commented in mem_cgroup_from_task(), mm->owner can be NULL
in some racy case. This check of VM_BUG_ON() is bad.

A possible story to hit this is at swapoff()->try_to_unuse(). It passes
mm_struct to mem_cgroup_try_charge_swapin() while mm->owner is NULL. If we
can't get proper mem_cgroup from swap_cgroup information, mm->owner is used
as charge target and we see NULL.

Cc: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp>
Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Reported-by: Hugh Dickins <hughd@google.com>
Reported-by: Thomas Meyer <thomas@m3y3r.de>
Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Reviewed-by: Balbir Singh <balbir@linux.vnet.ibm.com>
Signed-off-by: Hugh Dickins <hughd@google.com>
Cc: stable@kernel.org
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 1 changed file with 9 additions and 10 deletions Side-by-side Diff

... ... @@ -1925,19 +1925,18 @@
1925 1925  
1926 1926 rcu_read_lock();
1927 1927 p = rcu_dereference(mm->owner);
1928   - VM_BUG_ON(!p);
1929 1928 /*
1930   - * because we don't have task_lock(), "p" can exit while
1931   - * we're here. In that case, "mem" can point to root
1932   - * cgroup but never be NULL. (and task_struct itself is freed
1933   - * by RCU, cgroup itself is RCU safe.) Then, we have small
1934   - * risk here to get wrong cgroup. But such kind of mis-account
1935   - * by race always happens because we don't have cgroup_mutex().
1936   - * It's overkill and we allow that small race, here.
  1929 + * Because we don't have task_lock(), "p" can exit.
  1930 + * In that case, "mem" can point to root or p can be NULL with
  1931 + * race with swapoff. Then, we have small risk of mis-accouning.
  1932 + * But such kind of mis-account by race always happens because
  1933 + * we don't have cgroup_mutex(). It's overkill and we allo that
  1934 + * small race, here.
  1935 + * (*) swapoff at el will charge against mm-struct not against
  1936 + * task-struct. So, mm->owner can be NULL.
1937 1937 */
1938 1938 mem = mem_cgroup_from_task(p);
1939   - VM_BUG_ON(!mem);
1940   - if (mem_cgroup_is_root(mem)) {
  1939 + if (!mem || mem_cgroup_is_root(mem)) {
1941 1940 rcu_read_unlock();
1942 1941 goto done;
1943 1942 }