Skip to contentSkip to conversation

mm: consolidate GFP_NOFAIL checks in the allocator slowpath

PATCH Superseded Michal Hocko 4 participants 6 messages Dec 14, 2016 – Dec 16, 2016

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

PATCH 1/1 mm: consolidate GFP_NOFAIL checks in the allocator slowpath

From: Michal Hocko <[email protected]>

Tetsuo Handa has pointed out that 0a0337e0d1d1 ("mm, oom: rework oom detection") has subtly changed semantic for costly high order requests with __GFP_NOFAIL and withtout __GFP_REPEAT and those can fail right now. My code inspection didn't reveal any such users in the tree but it is true that this might lead to unexpected allocation failures and subsequent OOPs.

__alloc_pages_slowpath wrt. GFP_NOFAIL is hard to follow currently. There are few special cases but we are lacking a catch all place to be sure we will not miss any case where the non failing allocation might fail. This patch reorganizes the code a bit and puts all those special cases under nopage label which is the generic go-to-fail path. Non failing allocations are retried or those that cannot retry like non-sleeping allocation go to the failure point directly. This should make the code flow much easier to follow and make it less error prone for future changes.

While we are there we have to move the stall check up to catch potentially looping non-failing allocations.

---
Hi Andrew,
this has been posted previously as a 2 patch series [1]. This is the first patch. The second one has generated a lot of discussion and Tetsuo has naked it based because he is worried about a potential lockups. I have argued [2] that there are other aspects to consider but then later realized that there is a different risk in place which hasn't been considered before. There are some users who are performing a lot of __GFP_NOFAIL|GFP_NOFS requests and we certainly do not want to give them full access to memory reserves without invoking the OOM killer [3].

For that reason I have dropped the second patch for now and think about this some more. The first patch still makes some sense and I find it as a useful cleanup so I would ask you to merge it before I find a better solution for the other issue. There was no opposition this this patch so I guess it should be good to go.

[1] http://lkml.kernel.org/r/[email protected]
[2] http://lkml.kernel.org/r/[email protected]
[3] http://lkml.kernel.org/r/[email protected]

 mm/page_alloc.c | 68 ++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 41 insertions(+), 27 deletions(-)
mm/page_alloc.c +41 -27
index 3f2c9e535f7f..79b327d9c9a6 100644
@@ -3640,32 +3640,23 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
 		goto got_pg;
 
 	/* Caller is not willing to reclaim, we can't balance anything */
-	if (!can_direct_reclaim) {
-		/*
-		 * All existing users of the __GFP_NOFAIL are blockable, so warn
-		 * of any new users that actually allow this type of allocation
-		 * to fail.
-		 */
-		WARN_ON_ONCE(gfp_mask & __GFP_NOFAIL);
+	if (!can_direct_reclaim)
 		goto nopage;
+
+	/* Make sure we know about allocations which stall for too long */
+	if (time_after(jiffies, alloc_start + stall_timeout)) {
+		warn_alloc(gfp_mask,
+			"page alloction stalls for %ums, order:%u",
+			jiffies_to_msecs(jiffies-alloc_start), order);
+		stall_timeout += 10 * HZ;
 	}
 
 	/* Avoid recursion of direct reclaim */
-	if (current->flags & PF_MEMALLOC) {
-		/*
-		 * __GFP_NOFAIL request from this context is rather bizarre
-		 * because we cannot reclaim anything and only can loop waiting
-		 * for somebody to do a work for us.
-		 */
-		if (WARN_ON_ONCE(gfp_mask & __GFP_NOFAIL)) {
-			cond_resched();
-			goto retry;
-		}
+	if (current->flags & PF_MEMALLOC)
 		goto nopage;
-	}
 
 	/* Avoid allocations with no watermarks from looping endlessly */
-	if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
+	if (test_thread_flag(TIF_MEMDIE))
 		goto nopage;
 
 
@@ -3692,14 +3683,6 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
 	if (order > PAGE_ALLOC_COSTLY_ORDER && !(gfp_mask & __GFP_REPEAT))
 		goto nopage;
 
-	/* Make sure we know about allocations which stall for too long */
-	if (time_after(jiffies, alloc_start + stall_timeout)) {
-		warn_alloc(gfp_mask,
-			"page allocation stalls for %ums, order:%u",
-			jiffies_to_msecs(jiffies-alloc_start), order);
-		stall_timeout += 10 * HZ;
-	}
-
 	if (should_reclaim_retry(gfp_mask, order, ac, alloc_flags,
 				 did_some_progress > 0, &no_progress_loops))
 		goto retry;
@@ -3728,6 +3711,37 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
 	}
 
 nopage:
+	/*
+	 * Make sure that __GFP_NOFAIL request doesn't leak out and make sure
+	 * we always retry
+	 */
+	if (gfp_mask & __GFP_NOFAIL) {
+		/*
+		 * All existing users of the __GFP_NOFAIL are blockable, so warn
+		 * of any new users that actually require GFP_NOWAIT
+		 */
+		if (WARN_ON_ONCE(!can_direct_reclaim))
+			goto fail;
+
+		/*
+		 * PF_MEMALLOC request from this context is rather bizarre
+		 * because we cannot reclaim anything and only can loop waiting
+		 * for somebody to do a work for us
+		 */
+		WARN_ON_ONCE(current->flags & PF_MEMALLOC);
+
+		/*
+		 * non failing costly orders are a hard requirement which we
+		 * are not prepared for much so let's warn about these users
+		 * so that we can identify them and convert them to something
+		 * else.
+		 */
+		WARN_ON_ONCE(order > PAGE_ALLOC_COSTLY_ORDER);
+
+		cond_resched();
+		goto retry;
+	}
+fail:
 	warn_alloc(gfp_mask,
 			"page allocation failure: order:%u", order);
 got_pg:
Signature · 7 lines
2.10.2

--
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>
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 19 lines · Michal Hocko

From: Michal Hocko <[email protected]>

Tetsuo Handa has pointed out that 0a0337e0d1d1 ("mm, oom: rework oom detection") has subtly changed semantic for costly high order requests with __GFP_NOFAIL and withtout __GFP_REPEAT and those can fail right now. My code inspection didn't reveal any such users in the tree but it is true that this might lead to unexpected allocation failures and subsequent OOPs.

__alloc_pages_slowpath wrt. GFP_NOFAIL is hard to follow currently. There are few special cases but we are lacking a catch all place to be sure we will not miss any case where the non failing allocation might fail. This patch reorganizes the code a bit and puts all those special cases under nopage label which is the generic go-to-fail path. Non failing allocations are retried or those that cannot retry like non-sleeping allocation go to the failure point directly. This should make the code flow much easier to follow and make it less error prone for future changes.

Quoted 5 lines

While we are there we have to move the stall check up to catch potentially looping non-failing allocations.

It's not the nicest thing that we have to duplicate all the conditions to warn on, but it's preferable over unreliable GFP_NOFAIL handling. Consolidating the handling of this flag makes a lot of sense to me.

--
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 on v1 1/1
#2 lore raw
In reply to Michal Hocko
Quoted 83 lines · Michal Hocko

From: Michal Hocko <[email protected]>

Tetsuo Handa has pointed out that 0a0337e0d1d1 ("mm, oom: rework oom detection") has subtly changed semantic for costly high order requests with __GFP_NOFAIL and withtout __GFP_REPEAT and those can fail right now. My code inspection didn't reveal any such users in the tree but it is true that this might lead to unexpected allocation failures and subsequent OOPs.

__alloc_pages_slowpath wrt. GFP_NOFAIL is hard to follow currently. There are few special cases but we are lacking a catch all place to be sure we will not miss any case where the non failing allocation might fail. This patch reorganizes the code a bit and puts all those special cases under nopage label which is the generic go-to-fail path. Non failing allocations are retried or those that cannot retry like non-sleeping allocation go to the failure point directly. This should make the code flow much easier to follow and make it less error prone for future changes.

While we are there we have to move the stall check up to catch potentially looping non-failing allocations.

---
Hi Andrew,
this has been posted previously as a 2 patch series [1]. This is the first patch. The second one has generated a lot of discussion and Tetsuo has naked it based because he is worried about a potential lockups. I have argued [2] that there are other aspects to consider but then later realized that there is a different risk in place which hasn't been considered before. There are some users who are performing a lot of __GFP_NOFAIL|GFP_NOFS requests and we certainly do not want to give them full access to memory reserves without invoking the OOM killer [3].

For that reason I have dropped the second patch for now and think about this some more. The first patch still makes some sense and I find it as a useful cleanup so I would ask you to merge it before I find a better solution for the other issue. There was no opposition this this patch so I guess it should be good to go.

[1] http://lkml.kernel.org/r/[email protected]
[2] http://lkml.kernel.org/r/[email protected]
[3] http://lkml.kernel.org/r/[email protected]

 mm/page_alloc.c | 68 ++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 41 insertions(+), 27 deletions(-)
mm/page_alloc.c +8 -16
index 3f2c9e535f7f..79b327d9c9a6 100644
@@ -3640,32 +3640,23 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
 		goto got_pg;
 
 	/* Caller is not willing to reclaim, we can't balance anything */
-	if (!can_direct_reclaim) {
-		/*
-		 * All existing users of the __GFP_NOFAIL are blockable, so warn
-		 * of any new users that actually allow this type of allocation
-		 * to fail.
-		 */
-		WARN_ON_ONCE(gfp_mask & __GFP_NOFAIL);
+	if (!can_direct_reclaim)
 		goto nopage;
+
+	/* Make sure we know about allocations which stall for too long */
+	if (time_after(jiffies, alloc_start + stall_timeout)) {
+		warn_alloc(gfp_mask,
+			"page alloction stalls for %ums, order:%u",
+			jiffies_to_msecs(jiffies-alloc_start), order);
+		stall_timeout += 10 * HZ;
 	}
 
 	/* Avoid recursion of direct reclaim */
-	if (current->flags & PF_MEMALLOC) {
-		/*
-		 * __GFP_NOFAIL request from this context is rather bizarre
-		 * because we cannot reclaim anything and only can loop waiting
-		 * for somebody to do a work for us.
-		 */
-		if (WARN_ON_ONCE(gfp_mask & __GFP_NOFAIL)) {
-			cond_resched();
-			goto retry;

mm/page_alloc.c __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, · line 3661

Quoted 10 lines
mm/page_alloc.c +2 -3
-		}
+	if (current->flags & PF_MEMALLOC)
 		goto nopage;
-	}
 
 	/* Avoid allocations with no watermarks from looping endlessly */
-	if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
+	if (test_thread_flag(TIF_MEMDIE))
 		goto nopage;
 

Nit: currently we allow TIF_MEMDIE & __GFP_NOFAIL request to try direct reclaim. Are you intentionally reclaiming that chance?

Other than that, feel free to add

Quoted 55 lines
mm/page_alloc.c +31 -9
@@ -3692,14 +3683,6 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
 	if (order > PAGE_ALLOC_COSTLY_ORDER && !(gfp_mask & __GFP_REPEAT))
 		goto nopage;
 
-	/* Make sure we know about allocations which stall for too long */
-	if (time_after(jiffies, alloc_start + stall_timeout)) {
-		warn_alloc(gfp_mask,
-			"page allocation stalls for %ums, order:%u",
-			jiffies_to_msecs(jiffies-alloc_start), order);
-		stall_timeout += 10 * HZ;
-	}
-
 	if (should_reclaim_retry(gfp_mask, order, ac, alloc_flags,
 				 did_some_progress > 0, &no_progress_loops))
 		goto retry;
@@ -3728,6 +3711,37 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
 	}
 
 nopage:
+	/*
+	 * Make sure that __GFP_NOFAIL request doesn't leak out and make sure
+	 * we always retry
+	 */
+	if (gfp_mask & __GFP_NOFAIL) {
+		/*
+		 * All existing users of the __GFP_NOFAIL are blockable, so warn
+		 * of any new users that actually require GFP_NOWAIT
+		 */
+		if (WARN_ON_ONCE(!can_direct_reclaim))
+			goto fail;
+
+		/*
+		 * PF_MEMALLOC request from this context is rather bizarre
+		 * because we cannot reclaim anything and only can loop waiting
+		 * for somebody to do a work for us
+		 */
+		WARN_ON_ONCE(current->flags & PF_MEMALLOC);
+
+		/*
+		 * non failing costly orders are a hard requirement which we
+		 * are not prepared for much so let's warn about these users
+		 * so that we can identify them and convert them to something
+		 * else.
+		 */
+		WARN_ON_ONCE(order > PAGE_ALLOC_COSTLY_ORDER);
+
+		cond_resched();
+		goto retry;
+	}
+fail:
 	warn_alloc(gfp_mask,
 			"page allocation failure: order:%u", order);
 got_pg:
--
Quoted 1 line

2.10.2

--
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>

#3 lore raw

PATCH v2 1/1 mm: consolidate GFP_NOFAIL checks in the allocator slowpath

Quoted 1 line · Hillf Danton

On Wednesday, December 14, 2016 11:07 PM Michal Hocko wrote:

[...]

Quoted 7 lines

Review

Quoted 5 lines
(quoted diff) +1 -1
 	/* Avoid allocations with no watermarks from looping endlessly */
-	if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
+	if (test_thread_flag(TIF_MEMDIE))
 		goto nopage;

Nit: currently we allow TIF_MEMDIE & __GFP_NOFAIL request to try direct reclaim. Are you intentionally reclaiming that chance?

That is definitely not a nit! Thanks for catching that. We definitely shouldn't bypass the direct reclaim because that would mean we rely on somebody else makes progress for us.

Updated patch below:
---

#4 lore raw
Quoted 39 lines · Michal Hocko
Quoted 1 line · Hillf Danton

On Wednesday, December 14, 2016 11:07 PM Michal Hocko wrote:

[...]

Quoted 7 lines

Review

Quoted 5 lines
(quoted diff) +1 -1
 	/* Avoid allocations with no watermarks from looping endlessly */
-	if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
+	if (test_thread_flag(TIF_MEMDIE))
 		goto nopage;

Nit: currently we allow TIF_MEMDIE & __GFP_NOFAIL request to try direct reclaim. Are you intentionally reclaiming that chance?

That is definitely not a nit! Thanks for catching that. We definitely shouldn't bypass the direct reclaim because that would mean we rely on somebody else makes progress for us.

Updated patch below:
---
From cebd2d933f245a59504fdce31312b67186311e50 Mon Sep 17 00:00:00 2001
From: Michal Hocko <[email protected]>
Date: Tue, 22 Nov 2016 07:52:58 +0100
Subject: [PATCH] mm: consolidate GFP_NOFAIL checks in the allocator slowpath

Tetsuo Handa has pointed out that 0a0337e0d1d1 ("mm, oom: rework oom detection") has subtly changed semantic for costly high order requests with __GFP_NOFAIL and withtout __GFP_REPEAT and those can fail right now. My code inspection didn't reveal any such users in the tree but it is true that this might lead to unexpected allocation failures and subsequent OOPs.

__alloc_pages_slowpath wrt. GFP_NOFAIL is hard to follow currently. There are few special cases but we are lacking a catch all place to be sure we will not miss any case where the non failing allocation might fail. This patch reorganizes the code a bit and puts all those special cases under nopage label which is the generic go-to-fail path. Non failing allocations are retried or those that cannot retry like non-sleeping allocation go to the failure point directly. This should make the code flow much easier to follow and make it less error prone for future changes.

Quoted 2 lines

While we are there we have to move the stall check up to catch potentially looping non-failing allocations.

Currently we allow TIF_MEMDIE && __GFP_NOFAIL threads to call __alloc_pages_may_oom() after !__alloc_pages_direct_reclaim() && !__alloc_pages_direct_compact() && !should_reclaim_retry() && !should_compact_retry().

But this patch changes TIF_MEMDIE && __GFP_NOFAIL threads not to call __alloc_pages_may_oom(). If this is intentional, please describe it (i.e. this patch adds a location which currently does not cause OOM livelock) in change log. (I don't trust your assumption that __GFP_FS allocations are running in parallel and they will call out_of_memory() on behalf of TIF_MEMDIE && __GFP_NOFAIL threads. Surprising things (e.g. all __GFP_FS allocations get stuck due to kswapd v.s. shrink_inactive_list() trap) can happen if TIF_MEMDIE && __GFP_NOFAIL has to loop forever. The OOM reaper allows selecting next OOM victim by setting MMF_OOM_REAPED does not help if we hit surprising traps.

Quoted 12 lines

Changes since v1
- do not skip direct reclaim for TIF_MEMDIE && GFP_NOFAIL as per Hillf

---
 mm/page_alloc.c | 75 +++++++++++++++++++++++++++++++++------------------------
 1 file changed, 44 insertions(+), 31 deletions(-)
#5 lore raw
Quoted 48 lines · Tetsuo Handa
Quoted 39 lines · Michal Hocko
Quoted 1 line · Hillf Danton

On Wednesday, December 14, 2016 11:07 PM Michal Hocko wrote:

[...]

Quoted 7 lines

Review

Quoted 5 lines
(quoted diff) +1 -1
 	/* Avoid allocations with no watermarks from looping endlessly */
-	if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
+	if (test_thread_flag(TIF_MEMDIE))
 		goto nopage;

Nit: currently we allow TIF_MEMDIE & __GFP_NOFAIL request to try direct reclaim. Are you intentionally reclaiming that chance?

That is definitely not a nit! Thanks for catching that. We definitely shouldn't bypass the direct reclaim because that would mean we rely on somebody else makes progress for us.

Updated patch below:
---
From cebd2d933f245a59504fdce31312b67186311e50 Mon Sep 17 00:00:00 2001
From: Michal Hocko <[email protected]>
Date: Tue, 22 Nov 2016 07:52:58 +0100
Subject: [PATCH] mm: consolidate GFP_NOFAIL checks in the allocator slowpath

Tetsuo Handa has pointed out that 0a0337e0d1d1 ("mm, oom: rework oom detection") has subtly changed semantic for costly high order requests with __GFP_NOFAIL and withtout __GFP_REPEAT and those can fail right now. My code inspection didn't reveal any such users in the tree but it is true that this might lead to unexpected allocation failures and subsequent OOPs.

__alloc_pages_slowpath wrt. GFP_NOFAIL is hard to follow currently. There are few special cases but we are lacking a catch all place to be sure we will not miss any case where the non failing allocation might fail. This patch reorganizes the code a bit and puts all those special cases under nopage label which is the generic go-to-fail path. Non failing allocations are retried or those that cannot retry like non-sleeping allocation go to the failure point directly. This should make the code flow much easier to follow and make it less error prone for future changes.

Quoted 2 lines

While we are there we have to move the stall check up to catch potentially looping non-failing allocations.

Currently we allow TIF_MEMDIE && __GFP_NOFAIL threads to call __alloc_pages_may_oom() after !__alloc_pages_direct_reclaim() && !__alloc_pages_direct_compact() && !should_reclaim_retry() && !should_compact_retry().

Quoted 4 lines

But this patch changes TIF_MEMDIE && __GFP_NOFAIL threads not to call __alloc_pages_may_oom(). If this is intentional, please describe it (i.e. this patch adds a location which currently does not cause OOM livelock) in change log.

No, it's not intentional. And you have a point, we shouldn't bypass __alloc_pages_may_oom. Does the following on top look any better?
---

mm/page_alloc.c +4 -4
index 3f44a5115b4c..095e2fa286de 100644
@@ -3667,10 +3667,6 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
 	if (page)
 		goto got_pg;
 
-	/* Avoid allocations with no watermarks from looping endlessly */
-	if (test_thread_flag(TIF_MEMDIE))
-		goto nopage;
-
 	/* Do not loop if specifically requested */
 	if (gfp_mask & __GFP_NORETRY)
 		goto nopage;
@@ -3703,6 +3699,10 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
 	if (page)
 		goto got_pg;
 
+	/* Avoid allocations with no watermarks from looping endlessly */
+	if (test_thread_flag(TIF_MEMDIE))
+		goto nopage;
+
 	/* Retry as long as the OOM killer is making progress */
 	if (did_some_progress) {
 		no_progress_loops = 0;
Signature · 8 lines
Michal Hocko
SUSE Labs

--
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>