1 From d883544515aae54842c21730b880172e7894fde9 Mon Sep 17 00:00:00 2001
2 From: Yang Shi <yang.shi@linux.alibaba.com>
3 Date: Tue, 13 Aug 2019 15:37:15 -0700
4 Subject: mm: mempolicy: make the behavior consistent when MPOL_MF_MOVE* and MPOL_MF_STRICT were specified
6 From: Yang Shi <yang.shi@linux.alibaba.com>
8 commit d883544515aae54842c21730b880172e7894fde9 upstream.
10 When both MPOL_MF_MOVE* and MPOL_MF_STRICT was specified, mbind() should
11 try best to migrate misplaced pages, if some of the pages could not be
12 migrated, then return -EIO.
14 There are three different sub-cases:
15 1. vma is not migratable
16 2. vma is migratable, but there are unmovable pages
17 3. vma is migratable, pages are movable, but migrate_pages() fails
19 If #1 happens, kernel would just abort immediately, then return -EIO,
20 after a7f40cfe3b7a ("mm: mempolicy: make mbind() return -EIO when
21 MPOL_MF_STRICT is specified").
23 If #3 happens, kernel would set policy and migrate pages with
24 best-effort, but won't rollback the migrated pages and reset the policy
27 Before that commit, they behaves in the same way. It'd better to keep
28 their behavior consistent. But, rolling back the migrated pages and
29 resetting the policy back sounds not feasible, so just make #1 behave as
32 Userspace will know that not everything was successfully migrated (via
33 -EIO), and can take whatever steps it deems necessary - attempt
34 rollback, determine which exact page(s) are violating the policy, etc.
36 Make queue_pages_range() return 1 to indicate there are unmovable pages
37 or vma is not migratable.
39 The #2 is not handled correctly in the current kernel, the following
42 [yang.shi@linux.alibaba.com: fix review comments from Vlastimil]
43 Link: http://lkml.kernel.org/r/1563556862-54056-2-git-send-email-yang.shi@linux.alibaba.com
44 Link: http://lkml.kernel.org/r/1561162809-59140-2-git-send-email-yang.shi@linux.alibaba.com
45 Signed-off-by: Yang Shi <yang.shi@linux.alibaba.com>
46 Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
47 Cc: Michal Hocko <mhocko@suse.com>
48 Cc: Mel Gorman <mgorman@techsingularity.net>
49 Cc: <stable@vger.kernel.org>
50 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
51 Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
52 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
55 mm/mempolicy.c | 68 ++++++++++++++++++++++++++++++++++++++++-----------------
56 1 file changed, 48 insertions(+), 20 deletions(-)
60 @@ -429,11 +429,14 @@ static inline bool queue_pages_required(
64 - * queue_pages_pmd() has three possible return values:
65 - * 1 - pages are placed on the right node or queued successfully.
66 - * 0 - THP was split.
67 - * -EIO - is migration entry or MPOL_MF_STRICT was specified and an existing
68 - * page was already on a node that does not follow the policy.
69 + * queue_pages_pmd() has four possible return values:
70 + * 0 - pages are placed on the right node or queued successfully.
71 + * 1 - there is unmovable page, and MPOL_MF_MOVE* & MPOL_MF_STRICT were
73 + * 2 - THP was split.
74 + * -EIO - is migration entry or only MPOL_MF_STRICT was specified and an
75 + * existing page was already on a node that does not follow the
78 static int queue_pages_pmd(pmd_t *pmd, spinlock_t *ptl, unsigned long addr,
79 unsigned long end, struct mm_walk *walk)
80 @@ -451,19 +454,17 @@ static int queue_pages_pmd(pmd_t *pmd, s
81 if (is_huge_zero_page(page)) {
83 __split_huge_pmd(walk->vma, pmd, addr, false, NULL);
87 - if (!queue_pages_required(page, qp)) {
89 + if (!queue_pages_required(page, qp))
95 /* go to thp migration */
96 if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) {
97 if (!vma_migratable(walk->vma)) {
103 @@ -479,6 +480,13 @@ out:
105 * Scan through pages checking if pages follow certain conditions,
106 * and move them to the pagelist if they do.
108 + * queue_pages_pte_range() has three possible return values:
109 + * 0 - pages are placed on the right node or queued successfully.
110 + * 1 - there is unmovable page, and MPOL_MF_MOVE* & MPOL_MF_STRICT were
112 + * -EIO - only MPOL_MF_STRICT was specified and an existing page was already
113 + * on a node that does not follow the policy.
115 static int queue_pages_pte_range(pmd_t *pmd, unsigned long addr,
116 unsigned long end, struct mm_walk *walk)
117 @@ -488,17 +496,17 @@ static int queue_pages_pte_range(pmd_t *
118 struct queue_pages *qp = walk->private;
119 unsigned long flags = qp->flags;
121 + bool has_unmovable = false;
125 ptl = pmd_trans_huge_lock(pmd, vma);
127 ret = queue_pages_pmd(pmd, ptl, addr, end, walk);
134 + /* THP was split, fall through to pte walk */
136 if (pmd_trans_unstable(pmd))
138 @@ -519,14 +527,21 @@ static int queue_pages_pte_range(pmd_t *
139 if (!queue_pages_required(page, qp))
141 if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) {
142 - if (!vma_migratable(vma))
143 + /* MPOL_MF_STRICT must be specified if we get here */
144 + if (!vma_migratable(vma)) {
145 + has_unmovable = true;
148 migrate_page_add(page, qp->pagelist, flags);
152 pte_unmap_unlock(pte - 1, ptl);
158 return addr != end ? -EIO : 0;
161 @@ -639,7 +654,13 @@ static int queue_pages_test_walk(unsigne
163 * If pages found in a given range are on a set of nodes (determined by
164 * @nodes and @flags,) it's isolated and queued to the pagelist which is
165 - * passed via @private.)
166 + * passed via @private.
168 + * queue_pages_range() has three possible return values:
169 + * 1 - there is unmovable page, but MPOL_MF_MOVE* & MPOL_MF_STRICT were
171 + * 0 - queue pages successfully or no misplaced page.
172 + * -EIO - there is misplaced page and only MPOL_MF_STRICT was specified.
175 queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end,
176 @@ -1182,6 +1203,7 @@ static long do_mbind(unsigned long start
177 struct mempolicy *new;
183 if (flags & ~(unsigned long)MPOL_MF_VALID)
184 @@ -1243,10 +1265,15 @@ static long do_mbind(unsigned long start
188 - err = queue_pages_range(mm, start, end, nmask,
189 + ret = queue_pages_range(mm, start, end, nmask,
190 flags | MPOL_MF_INVERT, &pagelist);
192 - err = mbind_range(mm, start, end, new);
199 + err = mbind_range(mm, start, end, new);
203 @@ -1259,13 +1286,14 @@ static long do_mbind(unsigned long start
204 putback_movable_pages(&pagelist);
207 - if (nr_failed && (flags & MPOL_MF_STRICT))
208 + if ((ret > 0) || (nr_failed && (flags & MPOL_MF_STRICT)))
211 putback_movable_pages(&pagelist);
214 up_write(&mm->mmap_sem);