Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1212089 > unrolled thread

Re: [PATCH] mm: mmap: Check all failures before set values

Started byMichal Hocko <mhocko@kernel.org>
First post2015-08-24 13:40 +0200
Last post2015-08-25 00:00 +0200
Articles 3 — 3 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: [PATCH] mm: mmap: Check all failures before set values Michal Hocko <mhocko@kernel.org> - 2015-08-24 13:40 +0200
    Re: [PATCH] mm: mmap: Check all failures before set values Andrew Morton <akpm@linux-foundation.org> - 2015-08-24 23:30 +0200
    Re: [PATCH] mm: mmap: Check all failures before set values Chen Gang <xili_gchen_5257@hotmail.com> - 2015-08-25 00:00 +0200

#1212089 — Re: [PATCH] mm: mmap: Check all failures before set values

FromMichal Hocko <mhocko@kernel.org>
Date2015-08-24 13:40 +0200
SubjectRe: [PATCH] mm: mmap: Check all failures before set values
Message-ID<q0Ygy-6rC-15@gated-at.bofh.it>
On Mon 24-08-15 00:59:39, gang.chen.5i5j@qq.com wrote:
> From: Chen Gang <gang.chen.5i5j@gmail.com>
> 
> When failure occurs and return, vma->vm_pgoff is already set, which is
> not a good idea.

Why? The vma is not inserted anywhere and the failure path is supposed
to simply free the vma.

> Signed-off-by: Chen Gang <gang.chen.5i5j@gmail.com>
> ---
>  mm/mmap.c | 13 +++++++------
>  1 file changed, 7 insertions(+), 6 deletions(-)
> 
> diff --git a/mm/mmap.c b/mm/mmap.c
> index 8e0366e..b5a6f09 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -2878,6 +2878,13 @@ int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
>  	struct vm_area_struct *prev;
>  	struct rb_node **rb_link, *rb_parent;
>  
> +	if (find_vma_links(mm, vma->vm_start, vma->vm_end,
> +			   &prev, &rb_link, &rb_parent))
> +		return -ENOMEM;
> +	if ((vma->vm_flags & VM_ACCOUNT) &&
> +	     security_vm_enough_memory_mm(mm, vma_pages(vma)))
> +		return -ENOMEM;
> +
>  	/*
>  	 * The vm_pgoff of a purely anonymous vma should be irrelevant
>  	 * until its first write fault, when page's anon_vma and index
> @@ -2894,12 +2901,6 @@ int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
>  		BUG_ON(vma->anon_vma);
>  		vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
>  	}
> -	if (find_vma_links(mm, vma->vm_start, vma->vm_end,
> -			   &prev, &rb_link, &rb_parent))
> -		return -ENOMEM;
> -	if ((vma->vm_flags & VM_ACCOUNT) &&
> -	     security_vm_enough_memory_mm(mm, vma_pages(vma)))
> -		return -ENOMEM;
>  
>  	vma_link(mm, vma, prev, rb_link, rb_parent);
>  	return 0;
> -- 
> 1.9.3

-- 
Michal Hocko
SUSE Labs
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [next] | [standalone]


#1212523

FromAndrew Morton <akpm@linux-foundation.org>
Date2015-08-24 23:30 +0200
Message-ID<q17tx-31O-33@gated-at.bofh.it>
In reply to#1212089
On Mon, 24 Aug 2015 13:32:13 +0200 Michal Hocko <mhocko@kernel.org> wrote:

> On Mon 24-08-15 00:59:39, gang.chen.5i5j@qq.com wrote:
> > From: Chen Gang <gang.chen.5i5j@gmail.com>
> > 
> > When failure occurs and return, vma->vm_pgoff is already set, which is
> > not a good idea.
> 
> Why? The vma is not inserted anywhere and the failure path is supposed
> to simply free the vma.

Yes, it's pretty marginal but I suppose the code is a bit better with
the patch than without.  I did this:


From: Chen Gang <gang.chen.5i5j@gmail.com>
Subject: mm/mmap.c:insert_vm_struct(): check for failure before setting values

There's no point in initializing vma->vm_pgoff if the insertion attempt
will be failing anyway.  Run the checks before performing the initialization.

Signed-off-by: Chen Gang <gang.chen.5i5j@gmail.com>
Cc: Michal Hocko <mhocko@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---

 mm/mmap.c |   13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff -puN mm/mmap.c~mm-mmap-check-all-failures-before-set-values mm/mmap.c
--- a/mm/mmap.c~mm-mmap-check-all-failures-before-set-values
+++ a/mm/mmap.c
@@ -2859,6 +2859,13 @@ int insert_vm_struct(struct mm_struct *m
 	struct vm_area_struct *prev;
 	struct rb_node **rb_link, *rb_parent;
 
+	if (find_vma_links(mm, vma->vm_start, vma->vm_end,
+			   &prev, &rb_link, &rb_parent))
+		return -ENOMEM;
+	if ((vma->vm_flags & VM_ACCOUNT) &&
+	     security_vm_enough_memory_mm(mm, vma_pages(vma)))
+		return -ENOMEM;
+
 	/*
 	 * The vm_pgoff of a purely anonymous vma should be irrelevant
 	 * until its first write fault, when page's anon_vma and index
@@ -2875,12 +2882,6 @@ int insert_vm_struct(struct mm_struct *m
 		BUG_ON(vma->anon_vma);
 		vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
 	}
-	if (find_vma_links(mm, vma->vm_start, vma->vm_end,
-			   &prev, &rb_link, &rb_parent))
-		return -ENOMEM;
-	if ((vma->vm_flags & VM_ACCOUNT) &&
-	     security_vm_enough_memory_mm(mm, vma_pages(vma)))
-		return -ENOMEM;
 
 	vma_link(mm, vma, prev, rb_link, rb_parent);
 	return 0;
_


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

[toc] | [prev] | [next] | [standalone]


#1212541

FromChen Gang <xili_gchen_5257@hotmail.com>
Date2015-08-25 00:00 +0200
Message-ID<q17Wy-3Ef-33@gated-at.bofh.it>
In reply to#1212089
T24gOC8yNS8xNSAwNToyNSwgQW5kcmV3IE1vcnRvbiB3cm90ZToKPiBPbiBNb24sIDI0IEF1ZyAy
MDE1IDEzOjMyOjEzICswMjAwIE1pY2hhbCBIb2NrbyA8bWhvY2tvQGtlcm5lbC5vcmc+IHdyb3Rl
Ogo+Cj4+IE9uIE1vbiAyNC0wOC0xNSAwMDo1OTozOSwgZ2FuZy5jaGVuLjVpNWpAcXEuY29tIHdy
b3RlOgo+Pj4gRnJvbTogQ2hlbiBHYW5nIDxnYW5nLmNoZW4uNWk1akBnbWFpbC5jb20+Cj4+Pgo+
Pj4gV2hlbiBmYWlsdXJlIG9jY3VycyBhbmQgcmV0dXJuLCB2bWEtPnZtX3Bnb2ZmIGlzIGFscmVh
ZHkgc2V0LCB3aGljaCBpcwo+Pj4gbm90IGEgZ29vZCBpZGVhLgo+Pgo+PiBXaHk/IFRoZSB2bWEg
aXMgbm90IGluc2VydGVkIGFueXdoZXJlIGFuZCB0aGUgZmFpbHVyZSBwYXRoIGlzIHN1cHBvc2Vk
Cj4+IHRvIHNpbXBseSBmcmVlIHRoZSB2bWEuCj4KPiBZZXMsIGl0J3MgcHJldHR5IG1hcmdpbmFs
IGJ1dCBJIHN1cHBvc2UgdGhlIGNvZGUgaXMgYSBiaXQgYmV0dGVyIHdpdGgKPiB0aGUgcGF0Y2gg
dGhhbiB3aXRob3V0LiBJIGRpZCB0aGlzOgo+CgpPSywgdGhhbmtzLiBUaGUgY29tbWVudHMgcmVh
bGx5IG5lZWQgdG8gYmUgaW1wcm92ZWQsIGp1c3QgbGlrZSBNaWNoYWwKSG9ja28gc2FpZCBiZWZv
cmUuCgoKVGhhbmtzLgotLQpDaGVuIEdhbmcKCk9wZW4sIHNoYXJlLCBhbmQgYXR0aXR1ZGUgbGlr
ZSBhaXIsIHdhdGVyLCBhbmQgbGlmZSB3aGljaCBHb2QgYmxlc3NlZAogCQkgCSAgIAkJICA=
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web