Skip to contentSkip to conversation

mm, oom: check memcg margin for parallel oom

PATCH Superseded Yafang Shao 3 participants 9 messages Jul 14, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm, oom: check memcg margin for parallel oom

The commit 7775face2079 ("memcg: killed threads should not invoke memcg OOM killer") resolves the problem that different threads in a multi-threaded task doing parallel memcg oom, but it doesn't solve the problem that different tasks doing parallel memcg oom.

It may happens that many different tasks in the same memcg are waiting oom_lock at the same time, if one of them has already made progress and freed enough available memory, the others don't need to trigger the oom killer again. By checking memcg margin after hold oom_lock can help achieve it.

Cc Michal Hocko <[email protected]>
Cc Tetsuo Handa <[email protected]>
Cc David Rientjes <[email protected]>
---
 mm/memcontrol.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
mm/memcontrol.c +17 -2
index 1962232..df141e1 100644
@@ -1560,16 +1560,31 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
 		.gfp_mask = gfp_mask,
 		.order = order,
 	};
-	bool ret;
+	bool ret = true;
 
 	if (mutex_lock_killable(&oom_lock))
 		return true;
+
 	/*
 	 * A few threads which were not waiting at mutex_lock_killable() can
 	 * fail to bail out. Therefore, check again after holding oom_lock.
 	 */
-	ret = should_force_charge() || out_of_memory(&oc);
+	if (should_force_charge())
+		goto out;
+
+	/*
+	 * Different tasks may be doing parallel oom, so after hold the
+	 * oom_lock the task should check the memcg margin again to check
+	 * whether other task has already made progress.
+	 */
+	if (mem_cgroup_margin(memcg) >= (1 << order))
+		goto out;
+
+	ret = out_of_memory(&oc);
+
+out:
 	mutex_unlock(&oom_lock);
+
 	return ret;
 }
 
Signature · 1 line
1.8.3.1
maintainer
#1 lore raw
Quoted 10 lines · Yafang Shao

The commit 7775face2079 ("memcg: killed threads should not invoke memcg OOM killer") resolves the problem that different threads in a multi-threaded task doing parallel memcg oom, but it doesn't solve the problem that different tasks doing parallel memcg oom.

It may happens that many different tasks in the same memcg are waiting oom_lock at the same time, if one of them has already made progress and freed enough available memory, the others don't need to trigger the oom killer again. By checking memcg margin after hold oom_lock can help achieve it.

While the changelog makes sense it I believe it can be improved. I would use something like the following. Feel free to use its parts.

"
Memcg oom killer invocation is synchronized by the global oom_lock and tasks are sleeping on the lock while somebody is selecting the victim or potentially race with the oom_reaper is releasing the victim's memory. This can result in a pointless oom killer invocation because a waiter might be racing with the oom_reaper

	P1		oom_reaper		P2
			oom_reap_task		mutex_lock(oom_lock)
						out_of_memory # no victim because we have one already
			__oom_reap_task_mm	mute_unlock(oom_lock)
mutex_lock(oom_lock)
			set MMF_OOM_SKIP
select_bad_process
# finds a new victim

The page allocator prevents from this race by trying to allocate after the lock can be acquired (in __alloc_pages_may_oom) which acts as a last minute check. Moreover page allocator simply doesn't block on the oom_lock and simply retries the whole reclaim process.

Memcg oom killer should do the last minute check as well. Call mem_cgroup_margin to do that. Trylock on the oom_lock could be done as well but this doesn't seem to be necessary at this stage.
"

Quoted 28 lines
Cc Michal Hocko <[email protected]>
Cc Tetsuo Handa <[email protected]>
Cc David Rientjes <[email protected]>
---
 mm/memcontrol.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
mm/memcontrol.c +2 -2
index 1962232..df141e1 100644
@@ -1560,16 +1560,31 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
 		.gfp_mask = gfp_mask,
 		.order = order,
 	};
-	bool ret;
+	bool ret = true;
 
 	if (mutex_lock_killable(&oom_lock))
 		return true;
+
 	/*
 	 * A few threads which were not waiting at mutex_lock_killable() can
 	 * fail to bail out. Therefore, check again after holding oom_lock.
 	 */
-	ret = should_force_charge() || out_of_memory(&oc);

mm/memcontrol.c static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, · line 1581

Quoted 10 lines
mm/memcontrol.c +10 -0
+	if (should_force_charge())
+		goto out;
+
+	/*
+	 * Different tasks may be doing parallel oom, so after hold the
+	 * oom_lock the task should check the memcg margin again to check
+	 * whether other task has already made progress.
+	 */
+	if (mem_cgroup_margin(memcg) >= (1 << order))
+		goto out;

Is there any reason why you simply haven't done this? (+ your comment which is helpful).

mm/memcontrol.c +8 -2
index 248e6cad0095..2c176825efe3 100644
@@ -1561,15 +1561,21 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
 		.order = order,
 		.chosen_points = LONG_MIN,
 	};
-	bool ret;
+	bool ret = true;
 
-	if (mutex_lock_killable(&oom_lock))
+	if (!mutex_trylock(&oom_lock))
 		return true;
+
+	if (mem_cgroup_margin(memcg) >= (1 << order))
+		goto unlock;
+
 	/*
 	 * A few threads which were not waiting at mutex_lock_killable() can
 	 * fail to bail out. Therefore, check again after holding oom_lock.
 	 */
 	ret = should_force_charge() || out_of_memory(&oc);
+
+unlock:
 	mutex_unlock(&oom_lock);
 	return ret;
 }
Quoted 11 lines
mm/memcontrol.c +5 -0
+
+	ret = out_of_memory(&oc);
+
+out:
 	mutex_unlock(&oom_lock);
+
 	return ret;
 }
 
Signature · 1 line
1.8.3.1
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#2 lore raw

And btw. "memcg, oom: check memcg margin for parallel oom" would be a better fit because this is not really related to the global oom.

Signature · 2 lines
Michal Hocko
SUSE Labs
#3 lore raw
Quoted 3 lines · Michal Hocko

And btw. "memcg, oom: check memcg margin for parallel oom" would be a better fit because this is not really related to the global oom.

Sure, I will do it.

Signature · 2 lines
Thanks
Yafang
#4 lore raw
In reply to Michal Hocko

mm/memcontrol.c static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, · line 1567

Quoted 3 lines · Michal Hocko
mm/memcontrol.c +1 -1
-	if (mutex_lock_killable(&oom_lock))
+	if (!mutex_trylock(&oom_lock))
 		return true;

I don't like this change. The trylock needlessly wastes CPU time which could have been utilized by the OOM killer/reaper for reclaiming memory.

Rather, I want to change

- if (!mutex_trylock(&oom_lock)) {
+ if (mutex_lock_killable(&oom_lock)) {

in __alloc_pages_may_oom() side.

maintainer
#5 lore raw
Quoted 7 lines · Tetsuo Handa

mm/memcontrol.c static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, · line 1567

Quoted 3 lines · Michal Hocko
mm/memcontrol.c +1 -1
-	if (mutex_lock_killable(&oom_lock))
+	if (!mutex_trylock(&oom_lock))
 		return true;

I don't like this change. The trylock needlessly wastes CPU time which could have been utilized by the OOM killer/reaper for reclaiming memory.

Ohh, that was an unintended hunk which stayed there from the previous discussion. With the margin check the lock itself can stay.

Quoted 7 lines

Rather, I want to change

(quoted diff) +1 -1
-	if (!mutex_trylock(&oom_lock)) {
+	if (mutex_lock_killable(&oom_lock)) {

in __alloc_pages_may_oom() side.

Please do not start a tangent discussion again. If you want to make this change then post a patch with a justification.

Signature · 2 lines
Michal Hocko
SUSE Labs
#6 lore raw
In reply to Michal Hocko
Quoted 75 lines · Michal Hocko
Quoted 10 lines · Yafang Shao

The commit 7775face2079 ("memcg: killed threads should not invoke memcg OOM killer") resolves the problem that different threads in a multi-threaded task doing parallel memcg oom, but it doesn't solve the problem that different tasks doing parallel memcg oom.

It may happens that many different tasks in the same memcg are waiting oom_lock at the same time, if one of them has already made progress and freed enough available memory, the others don't need to trigger the oom killer again. By checking memcg margin after hold oom_lock can help achieve it.

While the changelog makes sense it I believe it can be improved. I would use something like the following. Feel free to use its parts.

"
Memcg oom killer invocation is synchronized by the global oom_lock and tasks are sleeping on the lock while somebody is selecting the victim or potentially race with the oom_reaper is releasing the victim's memory. This can result in a pointless oom killer invocation because a waiter might be racing with the oom_reaper

P1 oom_reaper P2
oom_reap_task mutex_lock(oom_lock)
out_of_memory # no victim because we have one already
__oom_reap_task_mm mute_unlock(oom_lock)
mutex_lock(oom_lock)
set MMF_OOM_SKIP
select_bad_process

# finds a new victim

The page allocator prevents from this race by trying to allocate after the lock can be acquired (in __alloc_pages_may_oom) which acts as a last minute check. Moreover page allocator simply doesn't block on the oom_lock and simply retries the whole reclaim process.

Memcg oom killer should do the last minute check as well. Call mem_cgroup_margin to do that. Trylock on the oom_lock could be done as well but this doesn't seem to be necessary at this stage.
"

Quoted 33 lines
Cc Michal Hocko <[email protected]>
Cc Tetsuo Handa <[email protected]>
Cc David Rientjes <[email protected]>
---
 mm/memcontrol.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
mm/memcontrol.c +7 -2
index 1962232..df141e1 100644
@@ -1560,16 +1560,31 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
              .gfp_mask = gfp_mask,
              .order = order,
      };
-     bool ret;
+     bool ret = true;
 
      if (mutex_lock_killable(&oom_lock))
              return true;
+
      /*
       * A few threads which were not waiting at mutex_lock_killable() can
       * fail to bail out. Therefore, check again after holding oom_lock.
       */
-     ret = should_force_charge() || out_of_memory(&oc);
+     if (should_force_charge())
+             goto out;
+
+     /*
+      * Different tasks may be doing parallel oom, so after hold the
Quoted 9 lines

mm/memcontrol.c static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, · line 1581

Quoted 5 lines
mm/memcontrol.c +5 -0
+      * oom_lock the task should check the memcg margin again to check
+      * whether other task has already made progress.
+      */
+     if (mem_cgroup_margin(memcg) >= (1 << order))
+             goto out;

Is there any reason why you simply haven't done this? (+ your comment which is helpful).

No strong reason.
I just think that threads of a multi-thread task are more likely to do parallel OOM, so I checked it first.
I can change it as you suggested below, as it is more simple.

Quoted 43 lines
mm/memcontrol.c +8 -2
index 248e6cad0095..2c176825efe3 100644
@@ -1561,15 +1561,21 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
                .order = order,
                .chosen_points = LONG_MIN,
        };
-       bool ret;
+       bool ret = true;
 
-       if (mutex_lock_killable(&oom_lock))
+       if (!mutex_trylock(&oom_lock))
                return true;
+
+       if (mem_cgroup_margin(memcg) >= (1 << order))
+               goto unlock;
+
        /*
         * A few threads which were not waiting at mutex_lock_killable() can
         * fail to bail out. Therefore, check again after holding oom_lock.
         */
        ret = should_force_charge() || out_of_memory(&oc);
+
+unlock:
        mutex_unlock(&oom_lock);
        return ret;
 }
Quoted 11 lines
mm/memcontrol.c +5 -1
+
+     ret = out_of_memory(&oc);
+
+out:
      mutex_unlock(&oom_lock);
+
      return ret;
 }
 
--

1.8.3.1

--
Michal Hocko
SUSE Labs

Signature · 2 lines
Thanks
Yafang
maintainer
#7 lore raw
Quoted 1 line · Yafang Shao

On Tue, Jul 14, 2020 at 8:37 PM Michal Hocko <[email protected]> wrote:

[...]

Quoted 30 lines
Quoted 20 lines
Quoted 20 lines
mm/memcontrol.c +7 -2
@@ -1560,16 +1560,31 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
              .gfp_mask = gfp_mask,
              .order = order,
      };
-     bool ret;
+     bool ret = true;
 
      if (mutex_lock_killable(&oom_lock))
              return true;
+
      /*
       * A few threads which were not waiting at mutex_lock_killable() can
       * fail to bail out. Therefore, check again after holding oom_lock.
       */
-     ret = should_force_charge() || out_of_memory(&oc);
+     if (should_force_charge())
+             goto out;
+
+     /*
+      * Different tasks may be doing parallel oom, so after hold the
Quoted 9 lines

Review static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, · line 1581

Quoted 5 lines
(quoted diff) +5 -0
+      * oom_lock the task should check the memcg margin again to check
+      * whether other task has already made progress.
+      */
+     if (mem_cgroup_margin(memcg) >= (1 << order))
+             goto out;

Is there any reason why you simply haven't done this? (+ your comment which is helpful).

Quoted 4 lines

No strong reason.
I just think that threads of a multi-thread task are more likely to do parallel OOM, so I checked it first.
I can change it as you suggested below, as it is more simple.

I would rather go with simplicity. This is a super slow path so ordering of checks shouldn't matter much (if at all).

Signature · 2 lines
Michal Hocko
SUSE Labs
#8 lore raw
Quoted 34 lines · Michal Hocko
Quoted 1 line · Yafang Shao

On Tue, Jul 14, 2020 at 8:37 PM Michal Hocko <[email protected]> wrote:

[...]

Quoted 30 lines
Quoted 29 lines
Quoted 15 lines
mm/memcontrol.c +2 -2
@@ -1560,16 +1560,31 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
              .gfp_mask = gfp_mask,
              .order = order,
      };
-     bool ret;
+     bool ret = true;
 
      if (mutex_lock_killable(&oom_lock))
              return true;
+
      /*
       * A few threads which were not waiting at mutex_lock_killable() can
       * fail to bail out. Therefore, check again after holding oom_lock.
       */
-     ret = should_force_charge() || out_of_memory(&oc);

mm/memcontrol.c static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, · line 1581

Quoted 10 lines
mm/memcontrol.c +10 -0
+     if (should_force_charge())
+             goto out;
+
+     /*
+      * Different tasks may be doing parallel oom, so after hold the
+      * oom_lock the task should check the memcg margin again to check
+      * whether other task has already made progress.
+      */
+     if (mem_cgroup_margin(memcg) >= (1 << order))
+             goto out;

Is there any reason why you simply haven't done this? (+ your comment which is helpful).

Quoted 8 lines
Quoted 4 lines

No strong reason.
I just think that threads of a multi-thread task are more likely to do parallel OOM, so I checked it first.
I can change it as you suggested below, as it is more simple.

I would rather go with simplicity. This is a super slow path so ordering of checks shouldn't matter much (if at all).

Sure.

Signature · 2 lines
Thanks
Yafang