Skip to contentSkip to conversation

oom: fix integer overflow of points in oom_badness

PATCH Superseded Frantisek Hrbata 5 participants 11 messages Oct 31, 2011 – Dec 8, 2011

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

PATCH 1/1 oom: fix integer overflow of points in oom_badness

An integer overflow will happen on 64bit archs if task's sum of rss, swapents and nr_ptes exceeds (2^31)/1000 value. This was introduced by commit

f755a04 oom: use pte pages in OOM score

where the oom score computation was divided into several steps and it's no longer computed as one expression in unsigned long(rss, swapents, nr_pte are unsigned long), where the result value assigned to points(int) is in range(1..1000). So there could be an int overflow while computing

176 points *= 1000;

and points may have negative value. Meaning the oom score for a mem hog task will be one.

196 if (points <= 0)
197 return 1;

For example:
[ 3366] 0 3366 35390480 24303939 5 0 0 oom01
Out of memory: Kill process 3366 (oom01) score 1 or sacrifice child

Here the oom1 process consumes more than 24303939(rss)*4096~=92GB physical memory, but it's oom score is one.

In this situation the mem hog task is skipped and oom killer kills another and most probably innocent task with oom score greater than one.

This patch puts the computation of points back in one expression, so the int overflow will not happen.

My understanding is that we may just change the type of points variable from int to long and keep the current imho clearer(better readable) computation. There should not be an overflow on 32bit and there is a plenty of space for 64bit. If you like this solution better I will post the patch as v2.

---
 mm/oom_kill.c |    6 ++----
 1 files changed, 2 insertions(+), 4 deletions(-)
mm/oom_kill.c +2 -4
index 626303b..d029e9b 100644
@@ -192,11 +192,9 @@ unsigned int oom_badness(struct task_struct *p, struct mem_cgroup *mem,
 	 * The baseline for the badness score is the proportion of RAM that each
 	 * task's rss, pagetable and swap space use.
 	 */
-	points = get_mm_rss(p->mm) + p->mm->nr_ptes;
-	points += get_mm_counter(p->mm, MM_SWAPENTS);
+	points = (int)((get_mm_rss(p->mm) + get_mm_counter(p->mm, MM_SWAPENTS) +
+					p->mm->nr_ptes) * 1000UL / totalpages);
 
-	points *= 1000;
-	points /= totalpages;
 	task_unlock(p);
 
 	/*
Signature · 8 lines
1.7.6.4

--
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 internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw
Quoted 5 lines · Frantisek Hrbata

My understanding is that we may just change the type of points variable from int to long and keep the current imho clearer(better readable) computation. There should not be an overflow on 32bit and there is a plenty of space for 64bit. If you like this solution better I will post the patch as v2.

Up to maintainer, but personally I think the simple s/int/long/ looks better. Everything like get_mm_*/nr_ptes returns long.

Anyway good catch. Imho stable needs the fix too.

Cosmetic nit,

mm/oom_kill.c unsigned int oom_badness(struct task_struct *p, struct mem_cgroup *mem, · line 195

Quoted 3 lines
mm/oom_kill.c +1 -2
-	points = get_mm_rss(p->mm) + p->mm->nr_ptes;
-	points += get_mm_counter(p->mm, MM_SWAPENTS);
+	points = (int)((get_mm_rss(p->mm) + get_mm_counter(p->mm, MM_SWAPENTS) +
                 ^^^^^

Why do we need the explicit typecast? It buys nothing and looks a bit confusing.

And, if you prefer "int", perhaps something like

	- 	points *= 1000;
	-	points /= totalpages;
	+	/* avoid the possible overflow */
	+	points = points * 1000L / totalpages;

looks a bit more readable with the same effect. But I won't insist, this is up to you and David.

Oleg.

--
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 internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw
Quoted 9 lines · Oleg Nesterov
Quoted 5 lines · Frantisek Hrbata

My understanding is that we may just change the type of points variable from int to long and keep the current imho clearer(better readable) computation. There should not be an overflow on 32bit and there is a plenty of space for 64bit. If you like this solution better I will post the patch as v2.

Up to maintainer, but personally I think the simple s/int/long/ looks better. Everything like get_mm_*/nr_ptes returns long.

Agreed. I will post v2 with the int => long change.

Quoted 11 lines

Anyway good catch. Imho stable needs the fix too.

Cosmetic nit,

mm/oom_kill.c unsigned int oom_badness(struct task_struct *p, struct mem_cgroup *mem, · line 195

Quoted 3 lines
mm/oom_kill.c +1 -2
-	points = get_mm_rss(p->mm) + p->mm->nr_ptes;
-	points += get_mm_counter(p->mm, MM_SWAPENTS);
+	points = (int)((get_mm_rss(p->mm) + get_mm_counter(p->mm, MM_SWAPENTS) +
                 ^^^^^

Why do we need the explicit typecast? It buys nothing and looks a bit confusing.

You are right, it's not needed. I just wanted to make the cast more visible, but some kind of comment would be probably better here.

Quoted 10 lines

And, if you prefer "int", perhaps something like

	- 	points *= 1000;
	-	points /= totalpages;
	+	/* avoid the possible overflow */
	+	points = points * 1000L / totalpages;

looks a bit more readable with the same effect. But I won't insist, this is up to you and David.

Sure, this looks much better than the one line expression in the patch I sent. If David or others decide to not go with the int=>long change I think we should use this.

Quoted 3 lines

Oleg.

Many thanks Oleg.

Signature · 8 lines
Frantisek Hrbata

--
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 internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#3 lore raw

PATCH v2 1/1 oom: fix integer overflow of points in oom_badness

In reply to Frantisek Hrbata

An integer overflow will happen on 64bit archs if task's sum of rss, swapents and nr_ptes exceeds (2^31)/1000 value. This was introduced by commit

f755a04 oom: use pte pages in OOM score

where the oom score computation was divided into several steps and it's no longer computed as one expression in unsigned long(rss, swapents, nr_pte are unsigned long), where the result value assigned to points(int) is in range(1..1000). So there could be an int overflow while computing

176 points *= 1000;

and points may have negative value. Meaning the oom score for a mem hog task will be one.

196 if (points <= 0)
197 return 1;

For example:
[ 3366] 0 3366 35390480 24303939 5 0 0 oom01
Out of memory: Kill process 3366 (oom01) score 1 or sacrifice child

Here the oom1 process consumes more than 24303939(rss)*4096~=92GB physical memory, but it's oom score is one.

In this situation the mem hog task is skipped and oom killer kills another and most probably innocent task with oom score greater than one.

The points variable should be of type long instead of int to prevent the int overflow.

---
 mm/oom_kill.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
mm/oom_kill.c +1 -1
index 626303b..e9a1785 100644
@@ -162,7 +162,7 @@ static bool oom_unkillable_task(struct task_struct *p,
 unsigned int oom_badness(struct task_struct *p, struct mem_cgroup *mem,
 		      const nodemask_t *nodemask, unsigned long totalpages)
 {
-	int points;
+	long points;
 
 	if (oom_unkillable_task(p, mem, nodemask))
 		return 0;
Signature · 8 lines
1.7.6.4

--
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 internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Acked-by on v2 1/1
#4 lore raw
Quoted 41 lines · Frantisek Hrbata

An integer overflow will happen on 64bit archs if task's sum of rss, swapents and nr_ptes exceeds (2^31)/1000 value. This was introduced by commit

f755a04 oom: use pte pages in OOM score

where the oom score computation was divided into several steps and it's no longer computed as one expression in unsigned long(rss, swapents, nr_pte are unsigned long), where the result value assigned to points(int) is in range(1..1000). So there could be an int overflow while computing

176 points *= 1000;

and points may have negative value. Meaning the oom score for a mem hog task will be one.

196 if (points <= 0)
197 return 1;

For example:
[ 3366] 0 3366 35390480 24303939 5 0 0 oom01
Out of memory: Kill process 3366 (oom01) score 1 or sacrifice child

Here the oom1 process consumes more than 24303939(rss)*4096~=92GB physical memory, but it's oom score is one.

In this situation the mem hog task is skipped and oom killer kills another and most probably innocent task with oom score greater than one.

The points variable should be of type long instead of int to prevent the int overflow.

---
 mm/oom_kill.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
mm/oom_kill.c +0 -0
index 626303b..e9a1785 100644

mm/oom_kill.c static bool oom_unkillable_task(struct task_struct *p, · line 168

Quoted 8 lines
mm/oom_kill.c +1 -1
@@ -162,7 +162,7 @@ static bool oom_unkillable_task(struct task_struct *p,
 unsigned int oom_badness(struct task_struct *p, struct mem_cgroup *mem,
 		      const nodemask_t *nodemask, unsigned long totalpages)
 {
-	int points;
+	long points;
 
 	if (oom_unkillable_task(p, mem, nodemask))
 		return 0;

Good catch.

Acked-by on v2 1/1
#5 lore raw
In reply to Frantisek Hrbata

mm/oom_kill.c static bool oom_unkillable_task(struct task_struct *p, · line 165

Quoted 6 lines · Frantisek Hrbata

unsigned int oom_badness(struct task_struct *p, struct mem_cgroup *mem,

mm/oom_kill.c +1 -1
 		      const nodemask_t *nodemask, unsigned long totalpages)
 {
-	int points;
+	long points;

Good catch. Imho this is the stable material.

--
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 internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

reviewer Acked-by on v2 1/1
#6 lore raw
In reply to Frantisek Hrbata
Quoted 5 lines · Frantisek Hrbata

An integer overflow will happen on 64bit archs if task's sum of rss, swapents and nr_ptes exceeds (2^31)/1000 value. This was introduced by commit

f755a04 oom: use pte pages in OOM score

This commit was introduced in 2.6.39 but also backported to stable since 2.6.36, so presumably we'd need to mark this for stable as well going back that far.

Quoted 20 lines

where the oom score computation was divided into several steps and it's no longer computed as one expression in unsigned long(rss, swapents, nr_pte are unsigned long), where the result value assigned to points(int) is in range(1..1000). So there could be an int overflow while computing

176 points *= 1000;

and points may have negative value. Meaning the oom score for a mem hog task will be one.

196 if (points <= 0)
197 return 1;

For example:
[ 3366] 0 3366 35390480 24303939 5 0 0 oom01
Out of memory: Kill process 3366 (oom01) score 1 or sacrifice child

Here the oom1 process consumes more than 24303939(rss)*4096~=92GB physical memory, but it's oom score is one.

Quoted 7 lines

In this situation the mem hog task is skipped and oom killer kills another and most probably innocent task with oom score greater than one.

The points variable should be of type long instead of int to prevent the int overflow.

--
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 internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw

PATCH RESEND v2 1/1 oom: fix integer overflow of points in oom_badness

In reply to Frantisek Hrbata

An integer overflow will happen on 64bit archs if task's sum of rss, swapents and nr_ptes exceeds (2^31)/1000 value. This was introduced by commit

f755a04 oom: use pte pages in OOM score

where the oom score computation was divided into several steps and it's no longer computed as one expression in unsigned long(rss, swapents, nr_pte are unsigned long), where the result value assigned to points(int) is in range(1..1000). So there could be an int overflow while computing

176 points *= 1000;

and points may have negative value. Meaning the oom score for a mem hog task will be one.

196 if (points <= 0)
197 return 1;

For example:
[ 3366] 0 3366 35390480 24303939 5 0 0 oom01
Out of memory: Kill process 3366 (oom01) score 1 or sacrifice child

Here the oom1 process consumes more than 24303939(rss)*4096~=92GB physical memory, but it's oom score is one.

In this situation the mem hog task is skipped and oom killer kills another and most probably innocent task with oom score greater than one.

The points variable should be of type long instead of int to prevent the int overflow.

---
 mm/oom_kill.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
mm/oom_kill.c +1 -1
index 626303b..e9a1785 100644
@@ -162,7 +162,7 @@ static bool oom_unkillable_task(struct task_struct *p,
 unsigned int oom_badness(struct task_struct *p, struct mem_cgroup *mem,
 		      const nodemask_t *nodemask, unsigned long totalpages)
 {
-	int points;
+	long points;
 
 	if (oom_unkillable_task(p, mem, nodemask))
 		return 0;
Signature · 14 lines
1.7.6.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

--
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 internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#8 lore raw
Quoted 28 lines · Frantisek Hrbata

An integer overflow will happen on 64bit archs if task's sum of rss, swapents and nr_ptes exceeds (2^31)/1000 value. This was introduced by commit

f755a04 oom: use pte pages in OOM score

where the oom score computation was divided into several steps and it's no longer computed as one expression in unsigned long(rss, swapents, nr_pte are unsigned long), where the result value assigned to points(int) is in range(1..1000). So there could be an int overflow while computing

176 points *= 1000;

and points may have negative value. Meaning the oom score for a mem hog task will be one.

196 if (points <= 0)
197 return 1;

For example:
[ 3366] 0 3366 35390480 24303939 5 0 0 oom01
Out of memory: Kill process 3366 (oom01) score 1 or sacrifice child

Here the oom1 process consumes more than 24303939(rss)*4096~=92GB physical memory, but it's oom score is one.

In this situation the mem hog task is skipped and oom killer kills another and most probably innocent task with oom score greater than one.

Quoted 8 lines

The points variable should be of type long instead of int to prevent the int overflow.

For what it's worth, the stable address has changed to [email protected] so you might want to fix that up in future submissions.

I still catch patches that are tagged with this marking, but you will not end up posting stuff to the list this way :)

thanks,

greg k-h

--
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 internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#9 lore raw
Quoted 38 lines · Greg KH
Quoted 28 lines · Frantisek Hrbata

An integer overflow will happen on 64bit archs if task's sum of rss, swapents and nr_ptes exceeds (2^31)/1000 value. This was introduced by commit

f755a04 oom: use pte pages in OOM score

where the oom score computation was divided into several steps and it's no longer computed as one expression in unsigned long(rss, swapents, nr_pte are unsigned long), where the result value assigned to points(int) is in range(1..1000). So there could be an int overflow while computing

176 points *= 1000;

and points may have negative value. Meaning the oom score for a mem hog task will be one.

196 if (points <= 0)
197 return 1;

For example:
[ 3366] 0 3366 35390480 24303939 5 0 0 oom01
Out of memory: Kill process 3366 (oom01) score 1 or sacrifice child

Here the oom1 process consumes more than 24303939(rss)*4096~=92GB physical memory, but it's oom score is one.

In this situation the mem hog task is skipped and oom killer kills another and most probably innocent task with oom score greater than one.

Quoted 8 lines

The points variable should be of type long instead of int to prevent the int overflow.

Quoted 3 lines

For what it's worth, the stable address has changed to [email protected] so you might want to fix that up in future submissions.

I was not aware of this change.

Quoted 3 lines

I still catch patches that are tagged with this marking, but you will not end up posting stuff to the list this way :)

Duly noted :)

Quoted 2 lines

thanks,

Thanks Greg!

Quoted 2 lines

greg k-h

Signature · 8 lines
Frantisek Hrbata

--
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 internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#10 lore raw
In reply to Frantisek Hrbata
Quoted 28 lines · Frantisek Hrbata

An integer overflow will happen on 64bit archs if task's sum of rss, swapents and nr_ptes exceeds (2^31)/1000 value. This was introduced by commit

f755a04 oom: use pte pages in OOM score

where the oom score computation was divided into several steps and it's no longer computed as one expression in unsigned long(rss, swapents, nr_pte are unsigned long), where the result value assigned to points(int) is in range(1..1000). So there could be an int overflow while computing

176 points *= 1000;

and points may have negative value. Meaning the oom score for a mem hog task will be one.

196 if (points <= 0)
197 return 1;

For example:
[ 3366] 0 3366 35390480 24303939 5 0 0 oom01
Out of memory: Kill process 3366 (oom01) score 1 or sacrifice child

Here the oom1 process consumes more than 24303939(rss)*4096~=92GB physical memory, but it's oom score is one.

In this situation the mem hog task is skipped and oom killer kills another and most probably innocent task with oom score greater than one.

Quoted 8 lines

The points variable should be of type long instead of int to prevent the int overflow.

Andrew, this looks like 3.2-rc5 material.

Quoted 9 lines
---
 mm/oom_kill.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
mm/oom_kill.c +0 -0
index 626303b..e9a1785 100644

mm/oom_kill.c static bool oom_unkillable_task(struct task_struct *p, · line 168

Quoted 8 lines
mm/oom_kill.c +1 -1
@@ -162,7 +162,7 @@ static bool oom_unkillable_task(struct task_struct *p,
 unsigned int oom_badness(struct task_struct *p, struct mem_cgroup *mem,
 		      const nodemask_t *nodemask, unsigned long totalpages)
 {
-	int points;
+	long points;
 
 	if (oom_unkillable_task(p, mem, nodemask))
 		return 0;

--
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 internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>