[Devel,rh7] mm/memcg, ub: fix overflow in mem_cgroup_apply_beancounter()

Submitted by Andrey Ryabinin on June 1, 2017, 9:46 a.m.

Details

Message ID 20170601094608.27851-1-aryabinin@virtuozzo.com
State New
Series "mm/memcg, ub: fix overflow in mem_cgroup_apply_beancounter()"
Headers show

Commit Message

Andrey Ryabinin June 1, 2017, 9:46 a.m.
If UBC has unlimited UB_PHYSPAGES/UB_SWAPPAGES mem_cgroup_apply_beancounter()
will set memcg limits to negative values due to overflow. This will
lead to bogus -ENOMEM.

Add proper check for overflow and also make sure that we don't mem and memsw
bigger than PAGE_COUNTER_MAX as this is maximal possible value for page counters.

Reported-by: Vladimir Meshkov <vmeshkov@cloudlinux.com>
Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com>
---
 mm/memcontrol.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

Patch hide | download patch | download mbox

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index fff4cb021b6c..66d51b7b07c5 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -4732,10 +4732,14 @@  int mem_cgroup_apply_beancounter(struct mem_cgroup *memcg,
 
 	mem = ub->ub_parms[UB_PHYSPAGES].limit;
 	memsw = ub->ub_parms[UB_SWAPPAGES].limit;
-	if (memsw < PAGE_COUNTER_MAX - mem)
-		memsw += mem;
-	else
+
+	if (mem > PAGE_COUNTER_MAX)
+		mem = PAGE_COUNTER_MAX;
+
+	if (memsw + mem < mem || memsw + mem > PAGE_COUNTER_MAX)
 		memsw = PAGE_COUNTER_MAX;
+	else
+		memsw += mem;
 
 	oomguar = ub->ub_parms[UB_OOMGUARPAGES].barrier;