Skip to contentSkip to conversation

page-allocator: Ensure that processes that have been OOM killed exit the page allocator (resend)

PATCH In review Mel Gorman 4 participants 10 messages Jul 15, 2009 – Jul 17, 2009

Subsystems (2)
lore
Share & tools
lore

Mel Gorman

author
#0 lore raw

PATCH 1/1 page-allocator: Ensure that processes that have been OOM killed exit the page allocator (resend)

Processes that have been OOM killed set the thread flag TIF_MEMDIE. A process such as this is expected to exit the page allocator but potentially, it loops forever. This patch checks TIF_MEMDIE when deciding whether to loop again in the page allocator. If set, and __GFP_NOFAIL is not specified then the loop will exit on the assumption it's no longer important for the process to make forward progress. Note that a process that has just been OOM-killed will still loop at least one more time retrying the allocation before the thread flag is checked.

---

 mm/page_alloc.c |    8 ++++++++
 1 file changed, 8 insertions(+)
mm/page_alloc.c +8 -0
index f8902e7..5c98d02 100644
@@ -1547,6 +1547,14 @@ should_alloc_retry(gfp_t gfp_mask, unsigned int order,
 	if (gfp_mask & __GFP_NORETRY)
 		return 0;
 
+	/* Do not loop if OOM-killed unless __GFP_NOFAIL is specified */
+	if (test_thread_flag(TIF_MEMDIE)) {
+		if (gfp_mask & __GFP_NOFAIL)
+			WARN(1, "Potential infinite loop with __GFP_NOFAIL");
+		else
+			return 0;
+	}
+
 	/*
 	 * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
 	 * means __GFP_NOFAIL, but that may not be true in other

--
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/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#1 lore raw
Quoted 10 lines · Mel Gorman
mm/page_alloc.c +1 -0
index f8902e7..5c98d02 100644
@@ -1547,6 +1547,14 @@ should_alloc_retry(gfp_t gfp_mask, unsigned int order,
 	if (gfp_mask & __GFP_NORETRY)
 		return 0;
 
+	/* Do not loop if OOM-killed unless __GFP_NOFAIL is specified */

mm/page_alloc.c should_alloc_retry(gfp_t gfp_mask, unsigned int order, · line 1560

Quoted 10 lines
mm/page_alloc.c +7 -0
+	if (test_thread_flag(TIF_MEMDIE)) {
+		if (gfp_mask & __GFP_NOFAIL)
+			WARN(1, "Potential infinite loop with __GFP_NOFAIL");
+		else
+			return 0;
+	}
+
 	/*
 	 * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
 	 * means __GFP_NOFAIL, but that may not be true in other

This only works for GFP_ATOMIC since the next iteration of the page allocator will (probably) fail reclaim and simply invoke the oom killer again, which will notice current has TIF_MEMDIE set and choose to do nothing, at which time the allocator simply loops again.

#2 lore raw
Quoted 5 lines · David Rientjes

This only works for GFP_ATOMIC since the next iteration of the page allocator will (probably) fail reclaim and simply invoke the oom killer again, which will notice current has TIF_MEMDIE set and choose to do nothing, at which time the allocator simply loops again.

In other words, I'd propose this as an alternative patch.

mm/page_alloc.c +4 -0
@@ -1789,6 +1789,10 @@ rebalance:
 	if (p->flags & PF_MEMALLOC)
 		goto nopage;
 
+	/* Avoid allocations with no watermarks from looping endlessly */
+	if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
+		goto nopage;
+
 	/* Try direct reclaim and then allocating */
 	page = __alloc_pages_direct_reclaim(gfp_mask, order,
 					zonelist, high_zoneidx,
author
#3 lore raw
In reply to David Rientjes
Quoted 10 lines · David Rientjes
Quoted 8 lines · Mel Gorman
mm/page_alloc.c +0 -0
index f8902e7..5c98d02 100644
@@ -1547,6 +1547,14 @@ should_alloc_retry(gfp_t gfp_mask, unsigned int order,
 	if (gfp_mask & __GFP_NORETRY)
 		return 0;
 
Quoted 16 lines

mm/page_alloc.c should_alloc_retry(gfp_t gfp_mask, unsigned int order, · line 1560

Quoted 12 lines
mm/page_alloc.c +8 -0
+	/* Do not loop if OOM-killed unless __GFP_NOFAIL is specified */
+	if (test_thread_flag(TIF_MEMDIE)) {
+		if (gfp_mask & __GFP_NOFAIL)
+			WARN(1, "Potential infinite loop with __GFP_NOFAIL");
+		else
+			return 0;
+	}
+
 	/*
 	 * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
 	 * means __GFP_NOFAIL, but that may not be true in other

This only works for GFP_ATOMIC since the next iteration of the page allocator will (probably) fail reclaim and simply invoke the oom killer again,

GFP_ATOMIC should not be calling the OOM killer. It has already exited. Immeditely after an OOM kill, I would expect the allocation to succeed. However, in the event that the task selected for OOM killing is the current one and no other task exits, it could loop.

Quoted 3 lines

which will notice current has TIF_MEMDIE set and choose to do nothing, at which time the allocator simply loops again.

So, we should unconditionally check if we should loop again whether we have OOM killed or not which the following should do.

==== CUT HERE ====
page-allocator: Check after an OOM kill if the allocator should loop

Currently, the allocator loops unconditionally after an OOM kill on the assumption that the allocation will succeed. However, if the task selected for OOM-kill is the current task, it could in theory loop forever and always entering the OOM killer. This patch checks as normal after an OOM kill if the allocator should loop again.

Signature · 2 lines
 mm/page_alloc.c |    2 --
 1 file changed, 2 deletions(-)
mm/page_alloc.c +0 -2
index 4b8552e..b381a6b 100644
@@ -1830,8 +1830,6 @@ rebalance:
 			if (order > PAGE_ALLOC_COSTLY_ORDER &&
 						!(gfp_mask & __GFP_NOFAIL))
 				goto nopage;
-
-			goto restart;
 		}
 	}
 

--
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/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw
Quoted 6 lines · Mel Gorman
mm/page_alloc.c +0 -0
index 4b8552e..b381a6b 100644

mm/page_alloc.c rebalance: · line 1835

Quoted 8 lines
mm/page_alloc.c +0 -2
@@ -1830,8 +1830,6 @@ rebalance:
 			if (order > PAGE_ALLOC_COSTLY_ORDER &&
 						!(gfp_mask & __GFP_NOFAIL))
 				goto nopage;
-
-			goto restart;
 		}
 	}
 

This isn't right (and not only because it'll add a compiler warning because `restart' is now unused).

This would immediately fail any allocation that triggered the oom killer and ended up being selected that isn't __GFP_NOFAIL, even if it would have succeeded without even killing any task simply because it allocates without watermarks.

It will also, coupled with your earlier patch, inappropriately warn about an infinite loop with __GFP_NOFAIL even though it hasn't even attempted to loop once since that decision is now handled by should_alloc_retry().

The liklihood of such an infinite loop, considering only one thread per system (or cpuset) can be TIF_MEMDIE at a time, is very low. I've never seen memory reserves completely depleted such that the next high-priority allocation wouldn't succeed so that current could handle its pending SIGKILL.

You get the same behavior with my patch, but are allowed to try the high priority allocation again for the attempt that triggered the oom killer (and not only subsequent ones).
---

mm/page_alloc.c +4 -0
@@ -1789,6 +1789,10 @@ rebalance:
 	if (p->flags & PF_MEMALLOC)
 		goto nopage;
 
+	/* Avoid allocations with no watermarks from looping endlessly */
+	if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
+		goto nopage;
+
 	/* Try direct reclaim and then allocating */
 	page = __alloc_pages_direct_reclaim(gfp_mask, order,
 					zonelist, high_zoneidx,

--
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/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author
#5 lore raw
Quoted 35 lines · David Rientjes
Quoted 6 lines · Mel Gorman
mm/page_alloc.c +0 -0
index 4b8552e..b381a6b 100644

mm/page_alloc.c rebalance: · line 1835

Quoted 8 lines
mm/page_alloc.c +0 -2
@@ -1830,8 +1830,6 @@ rebalance:
 			if (order > PAGE_ALLOC_COSTLY_ORDER &&
 						!(gfp_mask & __GFP_NOFAIL))
 				goto nopage;
-
-			goto restart;
 		}
 	}
 

This isn't right (and not only because it'll add a compiler warning because `restart' is now unused).

This would immediately fail any allocation that triggered the oom killer and ended up being selected that isn't __GFP_NOFAIL, even if it would have succeeded without even killing any task simply because it allocates without watermarks.

It will also, coupled with your earlier patch, inappropriately warn about an infinite loop with __GFP_NOFAIL even though it hasn't even attempted to loop once since that decision is now handled by should_alloc_retry().

The liklihood of such an infinite loop, considering only one thread per system (or cpuset) can be TIF_MEMDIE at a time, is very low. I've never seen memory reserves completely depleted such that the next high-priority allocation wouldn't succeed so that current could handle its pending SIGKILL.

Quoted 3 lines

You get the same behavior with my patch, but are allowed to try the high priority allocation again for the attempt that triggered the oom killer (and not only subsequent ones).

Ok, lets go with this patch then. Thanks

Quoted 16 lines

---

mm/page_alloc.c +4 -0
@@ -1789,6 +1789,10 @@ rebalance:
 	if (p->flags & PF_MEMALLOC)
 		goto nopage;
 
+	/* Avoid allocations with no watermarks from looping endlessly */
+	if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
+		goto nopage;
+
 	/* Try direct reclaim and then allocating */
 	page = __alloc_pages_direct_reclaim(gfp_mask, order,
 					zonelist, high_zoneidx,
Signature · 9 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab

--
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/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Acked-by Mel Gorman
#6 lore raw
Quoted 2 lines · Mel Gorman

Ok, lets go with this patch then. Thanks

Ok, thanks, I'll add that as your acked-by and I'll write a formal patch description for it.

mm: avoid endless looping for oom killed tasks

If a task is oom killed and still cannot find memory when trying with no watermarks, it's better to fail the allocation attempt than to loop endlessly. Direct reclaim has already failed and the oom killer will be a no-op since current has yet to die, so there is no other alternative for allocations that are not __GFP_NOFAIL.

---

mm/page_alloc.c +4 -0
@@ -1789,6 +1789,10 @@ rebalance:
 	if (p->flags & PF_MEMALLOC)
 		goto nopage;
 
+	/* Avoid allocations with no watermarks from looping endlessly */
+	if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
+		goto nopage;
+
 	/* Try direct reclaim and then allocating */
 	page = __alloc_pages_direct_reclaim(gfp_mask, order,
 					zonelist, high_zoneidx,

--
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/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Acked-by
#7 lore raw
Quoted 11 lines · David Rientjes
Quoted 2 lines · Mel Gorman

Ok, lets go with this patch then. Thanks

Ok, thanks, I'll add that as your acked-by and I'll write a formal patch description for it.

mm: avoid endless looping for oom killed tasks

Quoted 8 lines

If a task is oom killed and still cannot find memory when trying with no watermarks, it's better to fail the allocation attempt than to loop endlessly. Direct reclaim has already failed and the oom killer will be a no-op since current has yet to die, so there is no other alternative for allocations that are not __GFP_NOFAIL.

This works much better for me than earlier variants (I'm needing to worry about OOM when KSM has a lot of pages to break COW on; but a large mlock is a good test) - thanks.

Quoted 5 lines

---

mm/page_alloc.c rebalance: · line 1798

Quoted 10 lines
mm/page_alloc.c +4 -0
@@ -1789,6 +1789,10 @@ rebalance:
 	if (p->flags & PF_MEMALLOC)
 		goto nopage;
 
+	/* Avoid allocations with no watermarks from looping endlessly */
+	if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
+		goto nopage;
+
 	/* Try direct reclaim and then allocating */
 	page = __alloc_pages_direct_reclaim(gfp_mask, order,
 					zonelist, high_zoneidx,

--
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/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#8 lore raw
In reply to Mel Gorman
Quoted 23 lines · On Wed, 15 Jul 2009 11:49:45 +0100 Mel Gorman <[email protected]> wrote:

Processes that have been OOM killed set the thread flag TIF_MEMDIE. A process such as this is expected to exit the page allocator but potentially, it loops forever. This patch checks TIF_MEMDIE when deciding whether to loop again in the page allocator. If set, and __GFP_NOFAIL is not specified then the loop will exit on the assumption it's no longer important for the process to make forward progress. Note that a process that has just been OOM-killed will still loop at least one more time retrying the allocation before the thread flag is checked.

---

 mm/page_alloc.c |    8 ++++++++
 1 file changed, 8 insertions(+)
mm/page_alloc.c +1 -0
index f8902e7..5c98d02 100644
@@ -1547,6 +1547,14 @@ should_alloc_retry(gfp_t gfp_mask, unsigned int order,
 	if (gfp_mask & __GFP_NORETRY)
 		return 0;
 
+	/* Do not loop if OOM-killed unless __GFP_NOFAIL is specified */

mm/page_alloc.c should_alloc_retry(gfp_t gfp_mask, unsigned int order, · line 1560

Quoted 10 lines
mm/page_alloc.c +7 -0
+	if (test_thread_flag(TIF_MEMDIE)) {
+		if (gfp_mask & __GFP_NOFAIL)
+			WARN(1, "Potential infinite loop with __GFP_NOFAIL");
+		else
+			return 0;
+	}
+
 	/*
 	 * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
 	 * means __GFP_NOFAIL, but that may not be true in other

This fixes a post-2.6.30 regression, yes?

I dug out the commit ID a while back but lost it. Ho hum.

--
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/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author
#9 lore raw
Quoted 39 lines · Andrew Morton
Quoted 23 lines · On Wed, 15 Jul 2009 11:49:45 +0100 Mel Gorman <[email protected]> wrote:

Processes that have been OOM killed set the thread flag TIF_MEMDIE. A process such as this is expected to exit the page allocator but potentially, it loops forever. This patch checks TIF_MEMDIE when deciding whether to loop again in the page allocator. If set, and __GFP_NOFAIL is not specified then the loop will exit on the assumption it's no longer important for the process to make forward progress. Note that a process that has just been OOM-killed will still loop at least one more time retrying the allocation before the thread flag is checked.

---

 mm/page_alloc.c |    8 ++++++++
 1 file changed, 8 insertions(+)
mm/page_alloc.c +1 -0
index f8902e7..5c98d02 100644
@@ -1547,6 +1547,14 @@ should_alloc_retry(gfp_t gfp_mask, unsigned int order,
 	if (gfp_mask & __GFP_NORETRY)
 		return 0;
 
+	/* Do not loop if OOM-killed unless __GFP_NOFAIL is specified */

mm/page_alloc.c should_alloc_retry(gfp_t gfp_mask, unsigned int order, · line 1560

Quoted 10 lines
mm/page_alloc.c +7 -0
+	if (test_thread_flag(TIF_MEMDIE)) {
+		if (gfp_mask & __GFP_NOFAIL)
+			WARN(1, "Potential infinite loop with __GFP_NOFAIL");
+		else
+			return 0;
+	}
+
 	/*
 	 * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
 	 * means __GFP_NOFAIL, but that may not be true in other

This fixes a post-2.6.30 regression, yes?

Quoted 2 lines

I dug out the commit ID a while back but lost it. Ho hum.

You made a note at the time "Offending commit 341ce06 handled the PF_MEMALLOC case but forgot about the TIF_MEMDIE case."

Signature · 9 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab

--
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/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>