memcg: avoid overflow caused by PAGE_ALIGN
authorSha Zhengju <handai.szj@taobao.com>
Thu, 12 Sep 2013 22:13:48 +0000 (15:13 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 12 Sep 2013 22:38:02 +0000 (15:38 -0700)
Since PAGE_ALIGN is aligning up(the next page boundary), so after
PAGE_ALIGN, the value might be overflow, such as write the MAX value to
*.limit_in_bytes.

  $ cat /cgroup/memory/memory.limit_in_bytes
  18446744073709551615

  # echo 18446744073709551615 > /cgroup/memory/memory.limit_in_bytes
  bash: echo: write error: Invalid argument

Some user programs might depend on such behaviours(like libcg, we read
the value in snapshot, then use the value to reset cgroup later), and
that will cause confusion.  So we need to fix it.

Signed-off-by: Sha Zhengju <handai.szj@taobao.com>
Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
Acked-by: Michal Hocko <mhocko@suse.cz>
Cc: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp>
Cc: Jeff Liu <jeff.liu@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
kernel/res_counter.c

index 3f0417f97e7665f514b73280e4bbff2e9608f96a..085d3ae478fe5ac7c89da55b6215a49bd8cd9c4d 100644 (file)
@@ -195,6 +195,10 @@ int res_counter_memparse_write_strategy(const char *buf,
        if (*end != '\0')
                return -EINVAL;
 
-       *res = PAGE_ALIGN(*res);
+       if (PAGE_ALIGN(*res) >= *res)
+               *res = PAGE_ALIGN(*res);
+       else
+               *res = RES_COUNTER_MAX;
+
        return 0;
 }