linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: kosaki.motohiro@gmail.com
To: linux-kernel@vger.kernel.org
Cc: linux-mm@kvack.org, Andrew Morton <akpm@google.com>,
	Dave Jones <davej@redhat.com>, Mel Gorman <mgorman@suse.de>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Christoph Lameter <cl@linux.com>,
	stable@vger.kernel.org, hughd@google.com,
	KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Miao Xie <miaox@cn.fujitsu.com>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>
Subject: [PATCH 5/6] mempolicy: fix a memory corruption by refcount imbalance in alloc_pages_vma()
Date: Wed, 30 May 2012 05:02:08 -0400	[thread overview]
Message-ID: <1338368529-21784-6-git-send-email-kosaki.motohiro@gmail.com> (raw)
In-Reply-To: <1338368529-21784-1-git-send-email-kosaki.motohiro@gmail.com>

From: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>

commit cc9a6c8776 (cpuset: mm: reduce large amounts of memory barrier related
damage v3) introduced a memory corruption.

shmem_alloc_page() passes pseudo vma and it has one significant unique
combination, vma->vm_ops=NULL and (vma->policy->flags & MPOL_F_SHARED).

Now, get_vma_policy() does NOT increase a policy ref when vma->vm_ops=NULL
and mpol_cond_put() DOES decrease a policy ref when a policy has MPOL_F_SHARED.
Therefore, when cpuset race is happen and alloc_pages_vma() fall in
'goto retry_cpuset' path, a policy refcount will be decreased too much and
therefore it will make memory corruption.

This patch fixes it.

Cc: Dave Jones <davej@redhat.com>,
Cc: Mel Gorman <mgorman@suse.de>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Lameter <cl@linux.com>,
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: <stable@vger.kernel.org>
Cc: Miao Xie <miaox@cn.fujitsu.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
---
 mm/mempolicy.c |   13 ++++++++++++-
 mm/shmem.c     |    9 +++++----
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 7fb7d51..0da0969 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -1544,18 +1544,29 @@ struct mempolicy *get_vma_policy(struct task_struct *task,
 		struct vm_area_struct *vma, unsigned long addr)
 {
 	struct mempolicy *pol = task->mempolicy;
+	int got_ref;
 
 	if (vma) {
 		if (vma->vm_ops && vma->vm_ops->get_policy) {
 			struct mempolicy *vpol = vma->vm_ops->get_policy(vma,
 									addr);
-			if (vpol)
+			if (vpol) {
 				pol = vpol;
+				got_ref = 1;
+			}
 		} else if (vma->vm_policy)
 			pol = vma->vm_policy;
 	}
 	if (!pol)
 		pol = &default_policy;
+
+	/*
+	 * shmem_alloc_page() passes MPOL_F_SHARED policy with vma->vm_ops=NULL.
+	 * Thus, we need to take additional ref for avoiding refcount imbalance.
+	 */
+	if (!got_ref && mpol_needs_cond_ref(pol))
+		mpol_get(pol);
+
 	return pol;
 }
 
diff --git a/mm/shmem.c b/mm/shmem.c
index d576b84..eb5f1eb 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -919,6 +919,7 @@ static struct page *shmem_alloc_page(gfp_t gfp,
 			struct shmem_inode_info *info, pgoff_t index)
 {
 	struct vm_area_struct pvma;
+	struct page *page;
 
 	/* Create a pseudo vma that just contains the policy */
 	pvma.vm_start = 0;
@@ -926,10 +927,10 @@ static struct page *shmem_alloc_page(gfp_t gfp,
 	pvma.vm_ops = NULL;
 	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
 
-	/*
-	 * alloc_page_vma() will drop the shared policy reference
-	 */
-	return alloc_page_vma(gfp, &pvma, 0);
+	page = alloc_page_vma(gfp, &pvma, 0);
+
+	mpol_put(pvma.vm_policy);
+	return page;
 }
 #else /* !CONFIG_NUMA */
 #ifdef CONFIG_TMPFS
-- 
1.7.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

  parent reply	other threads:[~2012-05-30  9:03 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-30  9:02 [PATCH 0/6] mempolicy memory corruption fixlet kosaki.motohiro
2012-05-30  9:02 ` [PATCH 1/6] Revert "mm: mempolicy: Let vma_merge and vma_split handle vma->vm_policy linkages" kosaki.motohiro
2012-05-30 19:17   ` Christoph Lameter
2012-05-30 19:39     ` KOSAKI Motohiro
2012-05-30 19:44       ` Christoph Lameter
2012-05-31  6:49     ` KOSAKI Motohiro
2012-05-30  9:02 ` [PATCH 2/6] mempolicy: Kill all mempolicy sharing kosaki.motohiro
2012-05-30 19:41   ` Christoph Lameter
2012-05-30 19:46     ` KOSAKI Motohiro
2012-05-30 19:48     ` Andi Kleen
2012-05-30 20:31   ` Andi Kleen
2012-05-30 20:58     ` KOSAKI Motohiro
2012-05-30  9:02 ` [PATCH 3/6] mempolicy: fix a race in shared_policy_replace() kosaki.motohiro
2012-05-30  9:02 ` [PATCH 4/6] mempolicy: fix refcount leak in mpol_set_shared_policy() kosaki.motohiro
2012-05-30  9:02 ` kosaki.motohiro [this message]
2012-05-30 20:37   ` [PATCH 5/6] mempolicy: fix a memory corruption by refcount imbalance in alloc_pages_vma() Andi Kleen
2012-05-30  9:02 ` [PATCH 6/6] MAINTAINERS: Added MEMPOLICY entry kosaki.motohiro
2012-05-30 19:27   ` Christoph Lameter
2012-05-30 18:26 ` [PATCH 0/6] mempolicy memory corruption fixlet Linus Torvalds
2012-05-30 18:34   ` Christoph Lameter
2012-05-30 18:46     ` Andi Kleen
2012-05-30 18:50       ` Christoph Lameter
2012-05-30 19:32         ` Andi Kleen
2012-05-30 19:42           ` Christoph Lameter
2012-05-30 19:52             ` Andi Kleen
2012-05-30 20:00               ` KOSAKI Motohiro
2012-05-30 21:22                 ` Ben Hutchings
2012-05-30 21:25                   ` KOSAKI Motohiro
2012-05-30 19:54             ` KOSAKI Motohiro
2012-05-30 20:10               ` Andi Kleen
2012-05-30 20:16                 ` KOSAKI Motohiro
2012-06-01  0:45                   ` david
2012-06-01 13:51                     ` Christoph Lameter
2012-06-01 19:31                       ` david
2012-06-01 19:37                         ` KOSAKI Motohiro
2012-06-05 19:02                           ` Linus Torvalds
2012-06-05 19:17                             ` Andrew Morton
2012-06-06 19:10                               ` KOSAKI Motohiro
2012-05-30 19:03     ` Linus Torvalds
2012-05-30 20:10   ` KOSAKI Motohiro
2012-06-11  9:17 [PATCH 0/6][resend] " kosaki.motohiro
2012-06-11  9:17 ` [PATCH 5/6] mempolicy: fix a memory corruption by refcount imbalance in alloc_pages_vma() kosaki.motohiro
2012-06-11 13:33   ` Ben Hutchings
2012-06-11 15:24     ` KOSAKI Motohiro
2012-06-12 14:20   ` Mel Gorman
2012-06-12 16:31     ` KOSAKI Motohiro

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1338368529-21784-6-git-send-email-kosaki.motohiro@gmail.com \
    --to=kosaki.motohiro@gmail.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=akpm@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=cl@linux.com \
    --cc=davej@redhat.com \
    --cc=hughd@google.com \
    --cc=kosaki.motohiro@jp.fujitsu.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mgorman@suse.de \
    --cc=miaox@cn.fujitsu.com \
    --cc=stable@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox