Skip to contentSkip to conversation

mm: mempolicy: fix the wrong return value and potential pages leak of mbind

PATCH Applied Yang Shi 3 participants 6 messages Oct 30, 2019 – Oct 31, 2019

Subsystems (3)
lore
Share & tools
lore

Yang Shi

author
#0 lore raw

PATCH 1/1 mm: mempolicy: fix the wrong return value and potential pages leak of mbind

The commit d883544515aa ("mm: mempolicy: make the behavior consistent when MPOL_MF_MOVE* and MPOL_MF_STRICT were specified") fixed the return value of mbind() for a couple of corner cases. But, it altered the errno for some other cases, for example, mbind() should return -EFAULT when part or all of the memory range specified by nodemask and maxnode points outside your accessible address space, or there was an unmapped hole in the specified memory range specified by addr and len.

Fixed this by preserving the errno returned by queue_pages_range(). And, the pagelist may be not empty even though queue_pages_range() returns error, put the pages back to LRU since mbind_range() is not called to really apply the policy so those pages should not be migrated, this is also the old behavior before the problematic commit.

Cc Vlastimil Babka <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Mel Gorman <[email protected]>
Cc <[email protected]> v4.19 and v5.2+
---
 mm/mempolicy.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
mm/mempolicy.c +9 -5
index 4ae967b..e08c941 100644
@@ -672,7 +672,9 @@ static int queue_pages_test_walk(unsigned long start, unsigned long end,
  * 1 - there is unmovable page, but MPOL_MF_MOVE* & MPOL_MF_STRICT were
  *     specified.
  * 0 - queue pages successfully or no misplaced page.
- * -EIO - there is misplaced page and only MPOL_MF_STRICT was specified.
+ * errno - i.e. misplaced pages with MPOL_MF_STRICT specified (-EIO) or
+ *         memory range specified by nodemask and maxnode points outside
+ *         your accessible address space (-EFAULT)
  */
 static int
 queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end,
@@ -1286,7 +1288,7 @@ static long do_mbind(unsigned long start, unsigned long len,
 			  flags | MPOL_MF_INVERT, &pagelist);
 
 	if (ret < 0) {
-		err = -EIO;
+		err = ret;
 		goto up_out;
 	}
 
@@ -1305,10 +1307,12 @@ static long do_mbind(unsigned long start, unsigned long len,
 
 		if ((ret > 0) || (nr_failed && (flags & MPOL_MF_STRICT)))
 			err = -EIO;
-	} else
-		putback_movable_pages(&pagelist);
-
+	} else {
 up_out:
+		if (!list_empty(&pagelist))
+			putback_movable_pages(&pagelist);
+	}
+
 	up_write(&mm->mmap_sem);
 mpol_out:
 	mpol_put(new);
Signature · 1 line
1.8.3.1
author
#1 lore raw
Quoted 8 lines · Yang Shi

The commit d883544515aa ("mm: mempolicy: make the behavior consistent when MPOL_MF_MOVE* and MPOL_MF_STRICT were specified") fixed the return value of mbind() for a couple of corner cases. But, it altered the errno for some other cases, for example, mbind() should return -EFAULT when part or all of the memory range specified by nodemask and maxnode points outside your accessible address space, or there was an unmapped hole in the specified memory range specified by addr and len.

Quoted 5 lines

Fixed this by preserving the errno returned by queue_pages_range(). And, the pagelist may be not empty even though queue_pages_range() returns error, put the pages back to LRU since mbind_range() is not called to really apply the policy so those pages should not be migrated, this is also the old behavior before the problematic commit.

Forgot fixes tag.

Fixes d883544515aa ("mm: mempolicy: make the behavior consistent when MPOL_MF_MOVE* and MPOL_MF_STRICT were specified")
Quoted 50 lines
Cc Vlastimil Babka <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Mel Gorman <[email protected]>
Cc <[email protected]> v4.19 and v5.2+
---
  mm/mempolicy.c | 14 +++++++++-----
  1 file changed, 9 insertions(+), 5 deletions(-)
mm/mempolicy.c +9 -5
index 4ae967b..e08c941 100644
@@ -672,7 +672,9 @@ static int queue_pages_test_walk(unsigned long start, unsigned long end,
   * 1 - there is unmovable page, but MPOL_MF_MOVE* & MPOL_MF_STRICT were
   *     specified.
   * 0 - queue pages successfully or no misplaced page.
- * -EIO - there is misplaced page and only MPOL_MF_STRICT was specified.
+ * errno - i.e. misplaced pages with MPOL_MF_STRICT specified (-EIO) or
+ *         memory range specified by nodemask and maxnode points outside
+ *         your accessible address space (-EFAULT)
   */
  static int
  queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end,
@@ -1286,7 +1288,7 @@ static long do_mbind(unsigned long start, unsigned long len,
  			  flags | MPOL_MF_INVERT, &pagelist);
 
  	if (ret < 0) {
-		err = -EIO;
+		err = ret;
  		goto up_out;
  	}
 
@@ -1305,10 +1307,12 @@ static long do_mbind(unsigned long start, unsigned long len,
 
  		if ((ret > 0) || (nr_failed && (flags & MPOL_MF_STRICT)))
  			err = -EIO;
-	} else
-		putback_movable_pages(&pagelist);
-
+	} else {
  up_out:
+		if (!list_empty(&pagelist))
+			putback_movable_pages(&pagelist);
+	}
+
  	up_write(&mm->mmap_sem);
  mpol_out:
  	mpol_put(new);
Reviewed-by
#2 lore raw
Quoted 16 lines · On 2019-10-31 at 02:14 Yang Shi wrote:
Quoted 8 lines · Yang Shi

The commit d883544515aa ("mm: mempolicy: make the behavior consistent when MPOL_MF_MOVE* and MPOL_MF_STRICT were specified") fixed the return value of mbind() for a couple of corner cases.  But, it altered the errno for some other cases, for example, mbind() should return -EFAULT when part or all of the memory range specified by nodemask and maxnode points  outside your accessible address space, or there was an unmapped hole in the specified memory range specified by addr and len.

Quoted 5 lines

Fixed this by preserving the errno returned by queue_pages_range(). And, the pagelist may be not empty even though queue_pages_range() returns error, put the pages back to LRU since mbind_range() is not called to really apply the policy so those pages should not be migrated, this is also the old behavior before the problematic commit.

Quoted 5 lines

Forgot fixes tag.

Fixes d883544515aa ("mm: mempolicy: make the behavior consistent when

MPOL_MF_MOVE* and MPOL_MF_STRICT were specified")

Looks good to me.

Quoted 51 lines
Quoted 50 lines
Cc Vlastimil Babka <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Mel Gorman <[email protected]>
Cc <[email protected]> v4.19 and v5.2+

---
   mm/mempolicy.c | 14 +++++++++-----

   1 file changed, 9 insertions(+), 5 deletions(-)
mm/mempolicy.c +3 -1
index 4ae967b..e08c941 100644
@@ -672,7 +672,9 @@ static int queue_pages_test_walk(unsigned long start, unsigned long end,
     * 1 - there is unmovable page, but MPOL_MF_MOVE* & MPOL_MF_STRICT were
     *     specified.
     * 0 - queue pages successfully or no misplaced page.
- * -EIO - there is misplaced page and only MPOL_MF_STRICT was specified.
+ * errno - i.e. misplaced pages with MPOL_MF_STRICT specified (-EIO) or
+ *         memory range specified by nodemask and maxnode points outside
+ *         your accessible address space (-EFAULT)
     */

   static int
   queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end,

mm/mempolicy.c +0 -0
@@ -1286,7 +1288,7 @@ static long do_mbind(unsigned long start, unsigned long len,
      flags | MPOL_MF_INVERT, &pagelist);


   if (ret < 0) {

mm/mempolicy.c +1 -1
-	err = -EIO;
+	err = ret;

   goto up_out;
   }

mm/mempolicy.c +0 -0
@@ -1305,10 +1307,12 @@ static long do_mbind(unsigned long start, unsigned long len,


   if ((ret > 0) || (nr_failed && (flags & MPOL_MF_STRICT))) err = -EIO;

mm/mempolicy.c +1 -3
-	} else
-	putback_movable_pages(&pagelist);
-
+	} else {

   up_out:

mm/mempolicy.c +4 -0
+	if (!list_empty(&pagelist))
+	putback_movable_pages(&pagelist);
+	}
+

   up_write(&mm->mmap_sem);
   mpol_out:
   mpol_put(new);

maintainer
#3 lore raw
In reply to Yang Shi
Quoted 14 lines · On Wed, 30 Oct 2019 11:14:58 -0700 Yang Shi <[email protected]> wrote:
Quoted 8 lines · Yang Shi

The commit d883544515aa ("mm: mempolicy: make the behavior consistent when MPOL_MF_MOVE* and MPOL_MF_STRICT were specified") fixed the return value of mbind() for a couple of corner cases. But, it altered the errno for some other cases, for example, mbind() should return -EFAULT when part or all of the memory range specified by nodemask and maxnode points outside your accessible address space, or there was an unmapped hole in the specified memory range specified by addr and len.

Quoted 5 lines

Fixed this by preserving the errno returned by queue_pages_range(). And, the pagelist may be not empty even though queue_pages_range() returns error, put the pages back to LRU since mbind_range() is not called to really apply the policy so those pages should not be migrated, this is also the old behavior before the problematic commit.

Quoted 4 lines

Forgot fixes tag.

Fixes d883544515aa ("mm: mempolicy: make the behavior consistent when

MPOL_MF_MOVE* and MPOL_MF_STRICT were specified")

What's the relationship between this patch and
http://lkml.kernel.org/r/[email protected]?

#4 lore raw
Quoted 16 lines · On 2019-10-31 at 12:31 Andrew Morton wrote:
Quoted 14 lines · On Wed, 30 Oct 2019 11:14:58 -0700 Yang Shi <[email protected]> wrote:
Quoted 13 lines · Yang Shi

The commit d883544515aa ("mm: mempolicy: make the behavior consistent when MPOL_MF_MOVE* and MPOL_MF_STRICT were specified") fixed the return value of mbind() for a couple of corner cases.  But, it altered the errno for some other cases, for example, mbind() should return -EFAULT when part or all of the memory range specified by nodemask and maxnode points  outside your accessible address space, or there was an unmapped hole in the specified memory range specified by addr and len.

Fixed this by preserving the errno returned by queue_pages_range(). And, the pagelist may be not empty even though queue_pages_range() returns error, put the pages back to LRU since mbind_range() is not called to really apply the policy so those pages should not be migrated, this is also the old behavior before the problematic commit.

Quoted 8 lines
Quoted 4 lines

Forgot fixes tag.

Fixes d883544515aa ("mm: mempolicy: make the behavior consistent when

MPOL_MF_MOVE* and MPOL_MF_STRICT were specified")

What's the relationship between this patch and
http://lkml.kernel.org/r/[email protected]?

They are for different issues. I found that -EFAULT is hidden from user space by d883544515aa when I was fixing the unmapped hole issue which is described in your quoted link.

Now, it is fixed for by current commit in this mail thread. I will explain the other one in its thead.

- Xinhai

author
#5 lore raw
In reply to Andrew Morton
Quoted 16 lines · Andrew Morton
Quoted 14 lines · On Wed, 30 Oct 2019 11:14:58 -0700 Yang Shi <[email protected]> wrote:
Quoted 13 lines · Yang Shi

The commit d883544515aa ("mm: mempolicy: make the behavior consistent when MPOL_MF_MOVE* and MPOL_MF_STRICT were specified") fixed the return value of mbind() for a couple of corner cases. But, it altered the errno for some other cases, for example, mbind() should return -EFAULT when part or all of the memory range specified by nodemask and maxnode points outside your accessible address space, or there was an unmapped hole in the specified memory range specified by addr and len.

Fixed this by preserving the errno returned by queue_pages_range(). And, the pagelist may be not empty even though queue_pages_range() returns error, put the pages back to LRU since mbind_range() is not called to really apply the policy so those pages should not be migrated, this is also the old behavior before the problematic commit.

Quoted 6 lines
Quoted 4 lines

Forgot fixes tag.

Fixes d883544515aa ("mm: mempolicy: make the behavior consistent when

MPOL_MF_MOVE* and MPOL_MF_STRICT were specified")

What's the relationship between this patch and
http://lkml.kernel.org/r/[email protected]?

They are irrelevant. The commit d883544515aa ("mm: mempolicy: make the behavior consistent
when MPOL_MF_MOVE* and MPOL_MF_STRICT were specified") override the -EFAULT return value of queue_pages_range() by -EIO mistakenly and missed putting non-empty pagelist back, this patch is aimed to fix the two issues.

I think Li Xinhai found the return value override problem during debugging his patch.