# \[PATCH 2/3\] cgroup: make sure memcg margin is 0 when over limit

- From: Glauber Costa \<hidden\>
- Date: 2012-01-20 15:03:20 
- List: netdev
- Page: https://ratatoskr.run/netdev/2012/01/3811640
- Thread: https://ratatoskr.run/netdev/2012/01/3811636/t.md

```text
For the memcg sock code, we'll need to register allocations
that are temporarily over limit. Let's make sure that margin
is 0 in this case.

I am keeping this as a separate patch, so that if any weirdness
interaction appears in the future, we can now exactly what caused
it.

Suggested by Johannes Weiner 

Signed-off-by: Glauber Costa <redacted>
CC: KAMEZAWA Hiroyuki <redacted>
CC: Johannes Weiner <redacted>
CC: Michal Hocko <redacted>
CC: Tejun Heo <redacted>
CC: Li Zefan <redacted>
---
 include/linux/res_counter.h |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)

diff --git a/include/linux/res_counter.h b/include/linux/res_counter.h
index c9d625c..d06d014 100644
--- a/include/linux/res_counter.h
+++ b/include/linux/res_counter.h
@@ -142,7 +142,10 @@ static inline unsigned long long res_counter_margin(struct res_counter *cnt)
 	unsigned long flags;
 
 	spin_lock_irqsave(&cnt->lock, flags);
-	margin = cnt->limit - cnt->usage;
+	if (cnt->limit > cnt->usage)
+		margin = cnt->limit - cnt->usage;
+	else
+		margin = 0;
 	spin_unlock_irqrestore(&cnt->lock, flags);
 	return margin;
 }
-- 
1.7.7.4
```
