Skip to contentSkip to conversation

memcg: avoid overflow in memcg_hierarchical_free_pages()

PATCH In review Greg Thelen 5 participants 6 messages Nov 9, 2010 – Nov 16, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 memcg: avoid overflow in memcg_hierarchical_free_pages()

Use page counts rather than byte counts to avoid overflowing unsigned long local variables.

---
 mm/memcontrol.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
mm/memcontrol.c +5 -5
index 6c7115d..b287afd 100644
@@ -1345,17 +1345,17 @@ memcg_hierarchical_free_pages(struct mem_cgroup *mem)
 {
 	unsigned long free, min_free;
 
-	min_free = global_page_state(NR_FREE_PAGES) << PAGE_SHIFT;
+	min_free = global_page_state(NR_FREE_PAGES);
 
 	while (mem) {
-		free = res_counter_read_u64(&mem->res, RES_LIMIT) -
-			res_counter_read_u64(&mem->res, RES_USAGE);
+		free = (res_counter_read_u64(&mem->res, RES_LIMIT) -
+			res_counter_read_u64(&mem->res, RES_USAGE)) >>
+			PAGE_SHIFT;
 		min_free = min(min_free, free);
 		mem = parent_mem_cgroup(mem);
 	}
 
-	/* Translate free memory in pages */
-	return min_free >> PAGE_SHIFT;
+	return min_free;
 }
 
 /*
Signature · 1 line
1.7.3.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Greg Thelen

Use page counts rather than byte counts to avoid overflowing unsigned long local variables.

Signature · 9 lines
Kind regards,
Minchan Kim

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Greg Thelen
Quoted 4 lines · Greg Thelen

Use page counts rather than byte counts to avoid overflowing unsigned long local variables.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw
In reply to Greg Thelen
Quoted 13 lines · On Mon, 8 Nov 2010 17:15:20 -0800 Greg Thelen <[email protected]> wrote:

Use page counts rather than byte counts to avoid overflowing unsigned long local variables.

---
 mm/memcontrol.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
mm/memcontrol.c +0 -0
index 6c7115d..b287afd 100644

mm/memcontrol.c memcg_hierarchical_free_pages(struct mem_cgroup *mem) · line 1347

Quoted 3 lines
mm/memcontrol.c +0 -0
@@ -1345,17 +1345,17 @@ memcg_hierarchical_free_pages(struct mem_cgroup *mem)
 {
 	unsigned long free, min_free;
 

hmm, the default value of RES_LIMIT is LLONG_MAX, so I think we must declare "free" as unsinged long long to avoid overflow.

Thanks,
Daisuke Nishimura.

Quoted 22 lines
mm/memcontrol.c +5 -5
-	min_free = global_page_state(NR_FREE_PAGES) << PAGE_SHIFT;
+	min_free = global_page_state(NR_FREE_PAGES);
 
 	while (mem) {
-		free = res_counter_read_u64(&mem->res, RES_LIMIT) -
-			res_counter_read_u64(&mem->res, RES_USAGE);
+		free = (res_counter_read_u64(&mem->res, RES_LIMIT) -
+			res_counter_read_u64(&mem->res, RES_USAGE)) >>
+			PAGE_SHIFT;
 		min_free = min(min_free, free);
 		mem = parent_mem_cgroup(mem);
 	}
 
-	/* Translate free memory in pages */
-	return min_free >> PAGE_SHIFT;
+	return min_free;
 }
 
 /*
Signature · 1 line
1.7.3.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw
Quoted 11 lines · Daisuke Nishimura
Quoted 8 lines · On Mon,  8 Nov 2010 17:15:20 -0800 Greg Thelen <[email protected]> wrote:

Use page counts rather than byte counts to avoid overflowing unsigned long local variables.

---
 mm/memcontrol.c |   10 +++++-----

 1 files changed, 5 insertions(+), 5 deletions(-)
Quoted 10 lines

mm/memcontrol.c memcg_hierarchical_free_pages(struct mem_cgroup *mem) · line 1347

Quoted 8 lines
mm/memcontrol.c +0 -0
index 6c7115d..b287afd 100644
@@ -1345,17 +1345,17 @@ memcg_hierarchical_free_pages(struct mem_cgroup *mem)
  {
       unsigned long free, min_free;
 

hmm, the default value of RES_LIMIT is LLONG_MAX, so I think we must declare "free" as unsinged long long to avoid overflow.

Agreed. I am testing a fix for that issue now. I do not want complicate this patch with the RES_LIMIT issue you mention. The fix will be in a separate patch.

Quoted 26 lines

Thanks,
Daisuke Nishimura.

Quoted 22 lines
mm/memcontrol.c +5 -6
-     min_free = global_page_state(NR_FREE_PAGES) << PAGE_SHIFT;
+     min_free = global_page_state(NR_FREE_PAGES);
 
       while (mem) {
-             free = res_counter_read_u64(&mem->res, RES_LIMIT) -
-                     res_counter_read_u64(&mem->res, RES_USAGE);
+             free = (res_counter_read_u64(&mem->res, RES_LIMIT) -
+                     res_counter_read_u64(&mem->res, RES_USAGE)) >>
+                     PAGE_SHIFT;
               min_free = min(min_free, free);
               mem = parent_mem_cgroup(mem);
       }
 
-     /* Translate free memory in pages */
-     return min_free >> PAGE_SHIFT;
+     return min_free;
  }
 
  /*
--

1.7.3.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Acked-by on v1 1/1
#5 lore raw
In reply to Greg Thelen
Quoted 4 lines · On Mon, 8 Nov 2010 17:15:20 -0800 Greg Thelen <[email protected]> wrote:

Use page counts rather than byte counts to avoid overflowing unsigned long local variables.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>