Skip to contentSkip to conversation

Memory rlimit fix crash on fork

PATCH In review Balbir Singh 3 participants 12 messages Aug 11, 2008 – Aug 13, 2008

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

cover Memory rlimit fix crash on fork

This patch fixes a crash that occurs when kernbench is set with memrlimit set to 500M on my x86_64 box. The root cause for the failure is

1. We don't set mm->mmap to NULL for the process for which fork() failed
2. mmput() dereferences vma (in unmap_vmas, vma->vm_mm).

This patch fixes the problem by

1. Initializing mm->mmap to NULL prior to failing dup_mmap()
2. unmap_vmas() check if mm->mmap is NULL (vma is NULL)
3. Don't uncharge when do_fork() fails in exit_mmap()

---

 kernel/fork.c |   19 ++++++++++---------
 mm/memory.c   |    6 +++++-
 mm/mmap.c     |    6 +++++-
 3 files changed, 20 insertions(+), 11 deletions(-)
mm/mmap.c renamed from mm/mmap.c~memrlimit-fix-crash-on-fork +5 -1
diff -puN mm/mmap.c~memrlimit-fix-crash-on-fork mm/mmap.c
@@ -2104,6 +2104,7 @@ void exit_mmap(struct mm_struct *mm)
 	struct vm_area_struct *vma;
 	unsigned long nr_accounted = 0;
 	unsigned long end;
+	bool uncharge_as = true;
 
 	/* mm's last user has gone, and its about to be pulled down */
 	arch_exit_mmap(mm);
@@ -2118,6 +2119,8 @@ void exit_mmap(struct mm_struct *mm)
 		}
 	}
 	vma = mm->mmap;
+	if (!vma)
+		uncharge_as = false;
 	lru_add_drain();
 	flush_cache_mm(mm);
 	tlb = tlb_gather_mmu(mm, 1);
@@ -2125,7 +2128,8 @@ void exit_mmap(struct mm_struct *mm)
 	/* Use -1 here to ensure all VMAs in the mm are unmapped */
 	end = unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL);
 	vm_unacct_memory(nr_accounted);
-	memrlimit_cgroup_uncharge_as(mm, mm->total_vm);
+	if (uncharge_as)
+		memrlimit_cgroup_uncharge_as(mm, mm->total_vm);
 	free_pgtables(tlb, vma, FIRST_USER_ADDRESS, 0);
 	tlb_finish_mmu(tlb, 0, end);
 
kernel/fork.c renamed from kernel/fork.c~memrlimit-fix-crash-on-fork +10 -9
diff -puN kernel/fork.c~memrlimit-fix-crash-on-fork kernel/fork.c
@@ -274,15 +274,6 @@ static int dup_mmap(struct mm_struct *mm
 	 */
 	down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
 
-	/*
-	 * Uncharging as a result of failure is done by mmput()
-	 * in dup_mm()
-	 */
-	if (memrlimit_cgroup_charge_as(oldmm, oldmm->total_vm)) {
-		retval = -ENOMEM;
-		goto out;
-	}
-
 	mm->locked_vm = 0;
 	mm->mmap = NULL;
 	mm->mmap_cache = NULL;
@@ -295,6 +286,16 @@ static int dup_mmap(struct mm_struct *mm
 	rb_parent = NULL;
 	pprev = &mm->mmap;
 
+	/*
+	 * Called after mm->mmap is set to NULL, so that the routines
+	 * following this function understand that fork failed (read
+	 * mmput).
+	 */
+	if (memrlimit_cgroup_charge_as(oldmm, oldmm->total_vm)) {
+		retval = -ENOMEM;
+		goto out;
+	}
+
 	for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
 		struct file *file;
 
mm/memory.c renamed from mm/memory.c~memrlimit-fix-crash-on-fork +5 -1
diff -puN mm/memory.c~memrlimit-fix-crash-on-fork mm/memory.c
@@ -901,8 +901,12 @@ unsigned long unmap_vmas(struct mmu_gath
 	unsigned long start = start_addr;
 	spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
 	int fullmm = (*tlbp)->fullmm;
-	struct mm_struct *mm = vma->vm_mm;
+	struct mm_struct *mm;
+
+	if (!vma)
+		return;
 
+	mm = vma->vm_mm;
 	mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
 	for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
 		unsigned long end;

_

Signature · 10 lines
	Warm Regards,
	Balbir Singh
	Linux Technology Center
	IBM, ISTL

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

PATCH 1/2 mm owner fix race between swap and exit

There's a race between mm->owner assignment and try_to_unuse(). The condition occurs when try_to_unuse() runs in parallel with an exiting task.

The race can be visualized below. To quote Hugh
"I don't think your careful alternation of CPU0/1 events at the end matters:
the swapoff CPU simply dereferences mm->owner after that task has gone"

But the alteration does help understand the race better (at-least for me :))

CPU0 CPU1
try_to_unuse
task 1 stars exiting look at mm = task1->mm
.. increment mm_users
task 1 exits
mm->owner needs to be updated, but
no new owner is found
(mm_users > 1, but no other task
has task->mm = task1->mm) mm_update_next_owner() leaves

grace period
user count drops, call mmput(mm)
task 1 freed

					dereferencing mm->owner fails

The fix is to notify the subsystem (via mm_owner_changed callback), if no new owner is found by specifying the new task as NULL.

---

 kernel/cgroup.c |    5 +++--
 kernel/exit.c   |   10 ++++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)
kernel/exit.c renamed from kernel/exit.c~mm-owner-fix-race-with-swap +10 -0
diff -puN kernel/exit.c~mm-owner-fix-race-with-swap kernel/exit.c
@@ -625,6 +625,16 @@ retry:
 	} while_each_thread(g, c);
 
 	read_unlock(&tasklist_lock);
+	/*
+	 * We found no owner and mm_users > 1, this implies that
+	 * we are most likely racing with swap (try_to_unuse())
+	 * Mark owner as NULL, so that subsystems can understand
+	 * the callback and take action
+	 */
+	down_write(&mm->mmap_sem);
+	mm->owner = NULL;
+	cgroup_mm_owner_callbacks(mm->owner, NULL);
+	up_write(&mm->mmap_sem);
 	return;
 
 assign_new_owner:

diff -L kernel/cgroup/.c -puN /dev/null /dev/null

kernel/cgroup.c renamed from kernel/cgroup.c~mm-owner-fix-race-with-swap +3 -2
diff -puN kernel/cgroup.c~mm-owner-fix-race-with-swap kernel/cgroup.c
@@ -2740,14 +2740,15 @@ void cgroup_fork_callbacks(struct task_s
  */
 void cgroup_mm_owner_callbacks(struct task_struct *old, struct task_struct *new)
 {
-	struct cgroup *oldcgrp, *newcgrp;
+	struct cgroup *oldcgrp, *newcgrp = NULL;
 
 	if (need_mm_owner_callback) {
 		int i;
 		for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
 			struct cgroup_subsys *ss = subsys[i];
 			oldcgrp = task_cgroup(old, ss->subsys_id);
-			newcgrp = task_cgroup(new, ss->subsys_id);
+			if (new)
+				newcgrp = task_cgroup(new, ss->subsys_id);
 			if (oldcgrp == newcgrp)
 				continue;
 			if (ss->mm_owner_changed)

_

Signature · 10 lines
	Warm Regards,
	Balbir Singh
	Linux Technology Center
	IBM, ISTL

--
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>
#7 lore raw

PATCH 2/2 Memory rlimit enhance mm_owner_changed callback to deal with exited owner

In reply to Balbir Singh

mm_owner_changed callback can also be called with new task set to NULL. (race between try_to_unuse() and mm->owner exiting). Surprisingly the order of cgroup arguments being passed was incorrect (proves that we did not run into mm_owner_changed callback at all).

---

 mm/memrlimitcgroup.c |   15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
mm/memrlimitcgroup.c renamed from mm/memrlimitcgroup.c~memrlimit-handle-mm-owner-notification-with-task-null +13 -2
diff -puN mm/memrlimitcgroup.c~memrlimit-handle-mm-owner-notification-with-task-null mm/memrlimitcgroup.c
@@ -73,6 +73,12 @@ void memrlimit_cgroup_uncharge_as(struct
 {
 	struct memrlimit_cgroup *memrcg;
 
+	/*
+	 * Uncharge happened as a part of the mm_owner_changed callback
+	 */
+	if (!mm->owner)
+		return;
+
 	memrcg = memrlimit_cgroup_from_task(mm->owner);
 	res_counter_uncharge(&memrcg->as_res, (nr_pages << PAGE_SHIFT));
 }
@@ -235,8 +241,8 @@ out:
  * This callback is called with mmap_sem held
  */
 static void memrlimit_cgroup_mm_owner_changed(struct cgroup_subsys *ss,
-						struct cgroup *cgrp,
 						struct cgroup *old_cgrp,
+						struct cgroup *cgrp,
 						struct task_struct *p)
 {
 	struct memrlimit_cgroup *memrcg, *old_memrcg;
@@ -246,7 +252,12 @@ static void memrlimit_cgroup_mm_owner_ch
 	memrcg = memrlimit_cgroup_from_cgrp(cgrp);
 	old_memrcg = memrlimit_cgroup_from_cgrp(old_cgrp);
 
-	if (res_counter_charge(&memrcg->as_res, (mm->total_vm << PAGE_SHIFT)))
+	/*
+	 * If we don't have a new cgroup, we just uncharge from the old one.
+	 * It means that the task is going away
+	 */
+	if (memrcg &&
+	    res_counter_charge(&memrcg->as_res, (mm->total_vm << PAGE_SHIFT)))
 		goto out;
 	res_counter_uncharge(&old_memrcg->as_res, (mm->total_vm << PAGE_SHIFT));
 out:

_

Signature · 4 lines
	Warm Regards,
	Balbir Singh
	Linux Technology Center
	IBM, ISTL
maintainer
#2 lore raw

Re: [-mm][PATCH 1/2] mm owner fix race between swap and exit

In reply to Balbir Singh
Quoted 20 lines · On Mon, 11 Aug 2008 15:37:33 +0530 Balbir Singh <[email protected]>…

There's a race between mm->owner assignment and try_to_unuse(). The condition occurs when try_to_unuse() runs in parallel with an exiting task.

The race can be visualized below. To quote Hugh
"I don't think your careful alternation of CPU0/1 events at the end matters:
the swapoff CPU simply dereferences mm->owner after that task has gone"

But the alteration does help understand the race better (at-least for me :))

CPU0 CPU1
try_to_unuse
task 1 stars exiting look at mm = task1->mm
.. increment mm_users
task 1 exits
mm->owner needs to be updated, but
no new owner is found
(mm_users > 1, but no other task
has task->mm = task1->mm) mm_update_next_owner() leaves

Quoted 7 lines

grace period
user count drops, call mmput(mm)
task 1 freed

					dereferencing mm->owner fails

The fix is to notify the subsystem (via mm_owner_changed callback), if no new owner is found by specifying the new task as NULL.

This patch applies to mainline, 2.6.27-rc2 and even 2.6.26.

Against which kernel/patch is it actually applicable?

(If the answer was "all of the above" then please don't go embedding mainline bugfixes in the middle of a -mm-only patch series!)

Thanks.

--
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 2/2
#8 lore raw

Re: [-mm][PATCH 2/2] Memory rlimit enhance mm_owner_changed callback to deal with exited owner

In reply to Balbir Singh
Quoted 8 lines · Balbir Singh

mm_owner_changed callback can also be called with new task set to NULL. (race between try_to_unuse() and mm->owner exiting). Surprisingly the order of cgroup arguments being passed was incorrect (proves that we did not run into mm_owner_changed callback at all).

Quoted 47 lines

---

 mm/memrlimitcgroup.c |   15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff -puN mm/memrlimitcgroup.c~memrlimit-handle-mm-owner-notification-with-task-null mm/memrlimitcgroup.c

mm/memrlimitcgroup.c 2008-08-05 11:24:04.000000000 +0530 renamed from mm/memrlimitcgroup.c~memrlimit-handle-mm-owner-notification-with-task-null 2008-08-05 10:56:56.000000000 +0530 +13 -2
@@ -73,6 +73,12 @@ void memrlimit_cgroup_uncharge_as(struct
 {
       struct memrlimit_cgroup *memrcg;
 
+       /*
+        * Uncharge happened as a part of the mm_owner_changed callback
+        */
+       if (!mm->owner)
+               return;
+
       memrcg = memrlimit_cgroup_from_task(mm->owner);
       res_counter_uncharge(&memrcg->as_res, (nr_pages << PAGE_SHIFT));
 }
@@ -235,8 +241,8 @@ out:
 * This callback is called with mmap_sem held
 */
 static void memrlimit_cgroup_mm_owner_changed(struct cgroup_subsys *ss,
-                                               struct cgroup *cgrp,
                                               struct cgroup *old_cgrp,
+                                               struct cgroup *cgrp,
                                               struct task_struct *p)
 {
       struct memrlimit_cgroup *memrcg, *old_memrcg;
@@ -246,7 +252,12 @@ static void memrlimit_cgroup_mm_owner_ch
       memrcg = memrlimit_cgroup_from_cgrp(cgrp);
       old_memrcg = memrlimit_cgroup_from_cgrp(old_cgrp);
 
-       if (res_counter_charge(&memrcg->as_res, (mm->total_vm << PAGE_SHIFT)))
+       /*
+        * If we don't have a new cgroup, we just uncharge from the old one.
+        * It means that the task is going away
+        */
+       if (memrcg &&
+           res_counter_charge(&memrcg->as_res, (mm->total_vm << PAGE_SHIFT)))
               goto out;
       res_counter_uncharge(&old_memrcg->as_res, (mm->total_vm << PAGE_SHIFT));
 out:

_

Quoted 6 lines

--
Warm Regards,
Balbir Singh
Linux Technology Center
IBM, ISTL

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

Re: [-mm][PATCH 1/2] mm owner fix race between swap and exit

In reply to Andrew Morton
Quoted 9 lines · Andrew Morton
Quoted 2 lines

The fix is to notify the subsystem (via mm_owner_changed callback), if no new owner is found by specifying the new task as NULL.

This patch applies to mainline, 2.6.27-rc2 and even 2.6.26.

Against which kernel/patch is it actually applicable?

(If the answer was "all of the above" then please don't go embedding mainline bugfixes in the middle of a -mm-only patch series!)

The main thing this fixes is the memrlimit controller, which is only in -mm. But there's also a dereference of mm->owner in memcontrol.c - and I think that needs to be fixed to handle a possible NULL mm->owner too, since in the case of a swapoff racing with the last user of an mm exiting, I suspect that the swapoff code could try to pull in a page that gets charged to the mm after its owner has been set to NULL.

Paul

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

Re: [-mm][PATCH 1/2] mm owner fix race between swap and exit

In reply to Andrew Morton
Quoted 31 lines · Andrew Morton
Quoted 20 lines · On Mon, 11 Aug 2008 15:37:33 +0530 Balbir Singh <[email protected]>…

There's a race between mm->owner assignment and try_to_unuse(). The condition occurs when try_to_unuse() runs in parallel with an exiting task.

The race can be visualized below. To quote Hugh
"I don't think your careful alternation of CPU0/1 events at the end matters:
the swapoff CPU simply dereferences mm->owner after that task has gone"

But the alteration does help understand the race better (at-least for me :))

CPU0 CPU1
try_to_unuse
task 1 stars exiting look at mm = task1->mm
.. increment mm_users
task 1 exits
mm->owner needs to be updated, but
no new owner is found
(mm_users > 1, but no other task
has task->mm = task1->mm) mm_update_next_owner() leaves

Quoted 7 lines

grace period
user count drops, call mmput(mm)
task 1 freed

					dereferencing mm->owner fails

The fix is to notify the subsystem (via mm_owner_changed callback), if no new owner is found by specifying the new task as NULL.

Quoted 6 lines

This patch applies to mainline, 2.6.27-rc2 and even 2.6.26.

Against which kernel/patch is it actually applicable?

(If the answer was "all of the above" then please don't go embedding mainline bugfixes in the middle of a -mm-only patch series!)

Andrew,

The answer is all, but the bug is not exposed *outside* of the memrlimit controller, thus the push into -mm. I can redo and rework the patches for mainline if required and pull it out of -mm.

Signature · 10 lines
	Warm Regards,
	Balbir Singh
	Linux Technology Center
	IBM, ISTL

--
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
#5 lore raw

Re: [-mm][PATCH 1/2] mm owner fix race between swap and exit

Quoted 4 lines · On Tue, 12 Aug 2008 09:36:36 +0530 Balbir Singh <[email protected]>…
Quoted 4 lines

This patch applies to mainline, 2.6.27-rc2 and even 2.6.26.

Against which kernel/patch is it actually applicable?

Quoted 8 lines
Quoted 2 lines

(If the answer was "all of the above" then please don't go embedding mainline bugfixes in the middle of a -mm-only patch series!)

Andrew,

The answer is all, but the bug is not exposed *outside* of the memrlimit controller, thus the push into -mm. I can redo and rework the patches for mainline if required and pull it out of -mm.

OK, I'll move it into the general MM patchpile for 2.6.28. It will precede any memrlimit merge.

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

#6 lore raw

Re: [-mm][PATCH 1/2] mm owner fix race between swap and exit

Quoted 3 lines · Andrew Morton

OK, I'll move it into the general MM patchpile for 2.6.28. It will precede any memrlimit merge.

Thanks, sounds good.

Signature · 4 lines
	Warm Regards,
	Balbir Singh
	Linux Technology Center
	IBM, ISTL
maintainer
#9 lore raw
In reply to Balbir Singh

mm/memory.c unsigned long unmap_vmas(struct mmu_gath

Quoted 3 lines · On Mon, 11 Aug 2008 15:37:19 +0530 Balbir Singh <[email protected]>…
mm/memory.c renamed from mm/memory.c~memrlimit-fix-crash-on-fork +0 -0
@@ -901,8 +901,12 @@ unsigned long unmap_vmas(struct mmu_gath

^^ returns a long.

mm/memory.c unsigned long unmap_vmas(struct mmu_gath · line 907

Quoted 8 lines
mm/memory.c renamed from mm/memory.c~memrlimit-fix-crash-on-fork +4 -1
 	unsigned long start = start_addr;
 	spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
 	int fullmm = (*tlbp)->fullmm;
-	struct mm_struct *mm = vma->vm_mm;
+	struct mm_struct *mm;
+
+	if (!vma)
+		return;

^^ mm/memory.c:907: warning: 'return' with no value, in function returning non-void

How does this happen?

I'll drop the patch. The above mystery change needs a comment, IMO.

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

#10 lore raw
Quoted 20 lines · Andrew Morton

mm/memory.c unsigned long unmap_vmas(struct mmu_gath

Quoted 3 lines · On Mon, 11 Aug 2008 15:37:19 +0530 Balbir Singh <[email protected]>…
mm/memory.c renamed from mm/memory.c~memrlimit-fix-crash-on-fork +0 -0
@@ -901,8 +901,12 @@ unsigned long unmap_vmas(struct mmu_gath

^^ returns a long.

mm/memory.c unsigned long unmap_vmas(struct mmu_gath · line 907

Quoted 8 lines
mm/memory.c renamed from mm/memory.c~memrlimit-fix-crash-on-fork +4 -1
 	unsigned long start = start_addr;
 	spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
 	int fullmm = (*tlbp)->fullmm;
-	struct mm_struct *mm = vma->vm_mm;
+	struct mm_struct *mm;
+
+	if (!vma)
+		return;

^^ mm/memory.c:907: warning: 'return' with no value, in function returning non-void

Quoted 3 lines

How does this happen?

I'll drop the patch. The above mystery change needs a comment, IMO.

Oops.. I'll send the updated version. I'll comment it as well.

Signature · 7 lines
	Balbir

--
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>
#11 lore raw
Quoted 21 lines · Balbir Singh
Quoted 20 lines · Andrew Morton

mm/memory.c unsigned long unmap_vmas(struct mmu_gath

Quoted 3 lines · On Mon, 11 Aug 2008 15:37:19 +0530 Balbir Singh <[email protected]>…
mm/memory.c renamed from mm/memory.c~memrlimit-fix-crash-on-fork +0 -0
@@ -901,8 +901,12 @@ unsigned long unmap_vmas(struct mmu_gath

^^ returns a long.

mm/memory.c unsigned long unmap_vmas(struct mmu_gath · line 907

Quoted 8 lines
mm/memory.c renamed from mm/memory.c~memrlimit-fix-crash-on-fork +4 -1
 	unsigned long start = start_addr;
 	spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
 	int fullmm = (*tlbp)->fullmm;
-	struct mm_struct *mm = vma->vm_mm;
+	struct mm_struct *mm;
+
+	if (!vma)
+		return;

^^ mm/memory.c:907: warning: 'return' with no value, in function returning non-void

Quoted 6 lines
Quoted 3 lines

How does this happen?

I'll drop the patch. The above mystery change needs a comment, IMO.

Oops.. I'll send the updated version. I'll comment it as well.

Andrew,

I double checked the compiler warnings this time around and tested the patch. I've changed the core logic to avoid calling into unmap_vmas and do an early exit. My understanding is that doing an early exit should be OK, but I would like you get either you or Hugh or folks from linux-mm to comment on it and explicitly mention if it is OK to do so.

Changelog v2
------------

Remove changes from unmap_vmas(), don't call the remaining operations in exit_mmap() if mm->mmap is NULL.

This patch fixes a crash that occurs when kernbench is set with memrlimit set to 500M on my x86_64 box. The root cause for the failure is

1. We don't set mm->mmap to NULL for the process for which fork() failed
2. mmput() dereferences vma (in unmap_vmas, vma->vm_mm).

This patch fixes the problem by

1. Initializing mm->mmap to NULL prior to failing dup_mmap()
2. Check early if mm->mmap is NULL in exit_mmap() and return

---

 kernel/fork.c |   19 ++++++++++---------
 mm/mmap.c     |    9 +++++++++
 2 files changed, 19 insertions(+), 9 deletions(-)
mm/mmap.c renamed from mm/mmap.c~memrlimit-fix-crash-on-fork +9 -0
diff -puN mm/mmap.c~memrlimit-fix-crash-on-fork mm/mmap.c
@@ -2118,6 +2118,15 @@ void exit_mmap(struct mm_struct *mm)
 		}
 	}
 	vma = mm->mmap;
+
+	/*
+	 * In the case that dup_mm() failed, mm->mmap is NULL and
+	 * we never really setup the mm. We don't have much to do,
+	 * we might as well return early
+	 */
+	if (!vma)
+		return;
+
 	lru_add_drain();
 	flush_cache_mm(mm);
 	tlb = tlb_gather_mmu(mm, 1);
kernel/fork.c renamed from kernel/fork.c~memrlimit-fix-crash-on-fork +10 -9
diff -puN kernel/fork.c~memrlimit-fix-crash-on-fork kernel/fork.c
@@ -274,15 +274,6 @@ static int dup_mmap(struct mm_struct *mm
 	 */
 	down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
 
-	/*
-	 * Uncharging as a result of failure is done by mmput()
-	 * in dup_mm()
-	 */
-	if (memrlimit_cgroup_charge_as(oldmm, oldmm->total_vm)) {
-		retval = -ENOMEM;
-		goto out;
-	}
-
 	mm->locked_vm = 0;
 	mm->mmap = NULL;
 	mm->mmap_cache = NULL;
@@ -295,6 +286,16 @@ static int dup_mmap(struct mm_struct *mm
 	rb_parent = NULL;
 	pprev = &mm->mmap;
 
+	/*
+	 * Called after mm->mmap is set to NULL, so that the routines
+	 * following this function understand that fork failed (read
+	 * mmput).
+	 */
+	if (memrlimit_cgroup_charge_as(oldmm, oldmm->total_vm)) {
+		retval = -ENOMEM;
+		goto out;
+	}
+
 	for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
 		struct file *file;
 

diff -puN mm/memory.c~memrlimit-fix-crash-on-fork mm/memory.c _

Signature · 7 lines
	Balbir

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