Skip to content

Commit 8315ada

Browse files
Jérôme Glissetorvalds
authored andcommitted
mm/migrate: allow migrate_vma() to alloc new page on empty entry
This allows callers of migrate_vma() to allocate new page for empty CPU page table entry (pte_none or back by zero page). This is only for anonymous memory and it won't allow new page to be instanced if the userfaultfd is armed. This is useful to device driver that want to migrate a range of virtual address and would rather allocate new memory than having to fault later on. Link: http://lkml.kernel.org/r/20170817000548.32038-18-jglisse@redhat.com Signed-off-by: Jérôme Glisse <jglisse@redhat.com> Cc: Aneesh Kumar <aneesh.kumar@linux.vnet.ibm.com> Cc: Balbir Singh <bsingharora@gmail.com> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Dan Williams <dan.j.williams@intel.com> Cc: David Nellans <dnellans@nvidia.com> Cc: Evgeny Baskakov <ebaskakov@nvidia.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: John Hubbard <jhubbard@nvidia.com> Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Mark Hairgrove <mhairgrove@nvidia.com> Cc: Michal Hocko <mhocko@kernel.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Ross Zwisler <ross.zwisler@linux.intel.com> Cc: Sherry Cheung <SCheung@nvidia.com> Cc: Subhash Gutti <sgutti@nvidia.com> Cc: Vladimir Davydov <vdavydov.dev@gmail.com> Cc: Bob Liu <liubo95@huawei.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a5430dd commit 8315ada

2 files changed

Lines changed: 205 additions & 9 deletions

File tree

‎include/linux/migrate.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,15 @@ static inline unsigned long migrate_pfn(unsigned long pfn)
218218
* driver should avoid setting MIGRATE_PFN_ERROR unless it is really in an
219219
* unrecoverable state.
220220
*
221+
* For empty entry inside CPU page table (pte_none() or pmd_none() is true) we
222+
* do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus
223+
* allowing device driver to allocate device memory for those unback virtual
224+
* address. For this the device driver simply have to allocate device memory
225+
* and properly set the destination entry like for regular migration. Note that
226+
* this can still fails and thus inside the device driver must check if the
227+
* migration was successful for those entry inside the finalize_and_map()
228+
* callback just like for regular migration.
229+
*
221230
* THE alloc_and_copy() CALLBACK MUST NOT CHANGE ANY OF THE SRC ARRAY ENTRIES
222231
* OR BAD THINGS WILL HAPPEN !
223232
*

‎mm/migrate.c‎

Lines changed: 196 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <linux/hugetlb_cgroup.h>
3838
#include <linux/gfp.h>
3939
#include <linux/memremap.h>
40+
#include <linux/userfaultfd_k.h>
4041
#include <linux/balloon_compaction.h>
4142
#include <linux/mmu_notifier.h>
4243
#include <linux/page_idle.h>
@@ -2140,6 +2141,22 @@ static int migrate_vma_collect_hole(unsigned long start,
21402141
struct migrate_vma *migrate = walk->private;
21412142
unsigned long addr;
21422143

2144+
for (addr = start & PAGE_MASK; addr < end; addr += PAGE_SIZE) {
2145+
migrate->src[migrate->npages++] = MIGRATE_PFN_MIGRATE;
2146+
migrate->dst[migrate->npages] = 0;
2147+
migrate->cpages++;
2148+
}
2149+
2150+
return 0;
2151+
}
2152+
2153+
static int migrate_vma_collect_skip(unsigned long start,
2154+
unsigned long end,
2155+
struct mm_walk *walk)
2156+
{
2157+
struct migrate_vma *migrate = walk->private;
2158+
unsigned long addr;
2159+
21432160
for (addr = start & PAGE_MASK; addr < end; addr += PAGE_SIZE) {
21442161
migrate->dst[migrate->npages] = 0;
21452162
migrate->src[migrate->npages++] = 0;
@@ -2178,27 +2195,30 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
21782195
spin_unlock(ptl);
21792196
split_huge_pmd(vma, pmdp, addr);
21802197
if (pmd_trans_unstable(pmdp))
2181-
return migrate_vma_collect_hole(start, end,
2198+
return migrate_vma_collect_skip(start, end,
21822199
walk);
21832200
} else {
21842201
int ret;
21852202

21862203
get_page(page);
21872204
spin_unlock(ptl);
21882205
if (unlikely(!trylock_page(page)))
2189-
return migrate_vma_collect_hole(start, end,
2206+
return migrate_vma_collect_skip(start, end,
21902207
walk);
21912208
ret = split_huge_page(page);
21922209
unlock_page(page);
21932210
put_page(page);
2194-
if (ret || pmd_none(*pmdp))
2211+
if (ret)
2212+
return migrate_vma_collect_skip(start, end,
2213+
walk);
2214+
if (pmd_none(*pmdp))
21952215
return migrate_vma_collect_hole(start, end,
21962216
walk);
21972217
}
21982218
}
21992219

22002220
if (unlikely(pmd_bad(*pmdp)))
2201-
return migrate_vma_collect_hole(start, end, walk);
2221+
return migrate_vma_collect_skip(start, end, walk);
22022222

22032223
ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
22042224
arch_enter_lazy_mmu_mode();
@@ -2213,7 +2233,9 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
22132233
pfn = pte_pfn(pte);
22142234

22152235
if (pte_none(pte)) {
2216-
mpfn = pfn = 0;
2236+
mpfn = MIGRATE_PFN_MIGRATE;
2237+
migrate->cpages++;
2238+
pfn = 0;
22172239
goto next;
22182240
}
22192241

@@ -2235,6 +2257,12 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
22352257
if (is_write_device_private_entry(entry))
22362258
mpfn |= MIGRATE_PFN_WRITE;
22372259
} else {
2260+
if (is_zero_pfn(pfn)) {
2261+
mpfn = MIGRATE_PFN_MIGRATE;
2262+
migrate->cpages++;
2263+
pfn = 0;
2264+
goto next;
2265+
}
22382266
page = vm_normal_page(migrate->vma, addr, pte);
22392267
mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
22402268
mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
@@ -2554,6 +2582,135 @@ static void migrate_vma_unmap(struct migrate_vma *migrate)
25542582
}
25552583
}
25562584

2585+
static void migrate_vma_insert_page(struct migrate_vma *migrate,
2586+
unsigned long addr,
2587+
struct page *page,
2588+
unsigned long *src,
2589+
unsigned long *dst)
2590+
{
2591+
struct vm_area_struct *vma = migrate->vma;
2592+
struct mm_struct *mm = vma->vm_mm;
2593+
struct mem_cgroup *memcg;
2594+
bool flush = false;
2595+
spinlock_t *ptl;
2596+
pte_t entry;
2597+
pgd_t *pgdp;
2598+
p4d_t *p4dp;
2599+
pud_t *pudp;
2600+
pmd_t *pmdp;
2601+
pte_t *ptep;
2602+
2603+
/* Only allow populating anonymous memory */
2604+
if (!vma_is_anonymous(vma))
2605+
goto abort;
2606+
2607+
pgdp = pgd_offset(mm, addr);
2608+
p4dp = p4d_alloc(mm, pgdp, addr);
2609+
if (!p4dp)
2610+
goto abort;
2611+
pudp = pud_alloc(mm, p4dp, addr);
2612+
if (!pudp)
2613+
goto abort;
2614+
pmdp = pmd_alloc(mm, pudp, addr);
2615+
if (!pmdp)
2616+
goto abort;
2617+
2618+
if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp))
2619+
goto abort;
2620+
2621+
/*
2622+
* Use pte_alloc() instead of pte_alloc_map(). We can't run
2623+
* pte_offset_map() on pmds where a huge pmd might be created
2624+
* from a different thread.
2625+
*
2626+
* pte_alloc_map() is safe to use under down_write(mmap_sem) or when
2627+
* parallel threads are excluded by other means.
2628+
*
2629+
* Here we only have down_read(mmap_sem).
2630+
*/
2631+
if (pte_alloc(mm, pmdp, addr))
2632+
goto abort;
2633+
2634+
/* See the comment in pte_alloc_one_map() */
2635+
if (unlikely(pmd_trans_unstable(pmdp)))
2636+
goto abort;
2637+
2638+
if (unlikely(anon_vma_prepare(vma)))
2639+
goto abort;
2640+
if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, &memcg, false))
2641+
goto abort;
2642+
2643+
/*
2644+
* The memory barrier inside __SetPageUptodate makes sure that
2645+
* preceding stores to the page contents become visible before
2646+
* the set_pte_at() write.
2647+
*/
2648+
__SetPageUptodate(page);
2649+
2650+
if (is_zone_device_page(page) && is_device_private_page(page)) {
2651+
swp_entry_t swp_entry;
2652+
2653+
swp_entry = make_device_private_entry(page, vma->vm_flags & VM_WRITE);
2654+
entry = swp_entry_to_pte(swp_entry);
2655+
} else {
2656+
entry = mk_pte(page, vma->vm_page_prot);
2657+
if (vma->vm_flags & VM_WRITE)
2658+
entry = pte_mkwrite(pte_mkdirty(entry));
2659+
}
2660+
2661+
ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
2662+
2663+
if (pte_present(*ptep)) {
2664+
unsigned long pfn = pte_pfn(*ptep);
2665+
2666+
if (!is_zero_pfn(pfn)) {
2667+
pte_unmap_unlock(ptep, ptl);
2668+
mem_cgroup_cancel_charge(page, memcg, false);
2669+
goto abort;
2670+
}
2671+
flush = true;
2672+
} else if (!pte_none(*ptep)) {
2673+
pte_unmap_unlock(ptep, ptl);
2674+
mem_cgroup_cancel_charge(page, memcg, false);
2675+
goto abort;
2676+
}
2677+
2678+
/*
2679+
* Check for usefaultfd but do not deliver the fault. Instead,
2680+
* just back off.
2681+
*/
2682+
if (userfaultfd_missing(vma)) {
2683+
pte_unmap_unlock(ptep, ptl);
2684+
mem_cgroup_cancel_charge(page, memcg, false);
2685+
goto abort;
2686+
}
2687+
2688+
inc_mm_counter(mm, MM_ANONPAGES);
2689+
page_add_new_anon_rmap(page, vma, addr, false);
2690+
mem_cgroup_commit_charge(page, memcg, false, false);
2691+
if (!is_zone_device_page(page))
2692+
lru_cache_add_active_or_unevictable(page, vma);
2693+
get_page(page);
2694+
2695+
if (flush) {
2696+
flush_cache_page(vma, addr, pte_pfn(*ptep));
2697+
ptep_clear_flush_notify(vma, addr, ptep);
2698+
set_pte_at_notify(mm, addr, ptep, entry);
2699+
update_mmu_cache(vma, addr, ptep);
2700+
} else {
2701+
/* No need to invalidate - it was non-present before */
2702+
set_pte_at(mm, addr, ptep, entry);
2703+
update_mmu_cache(vma, addr, ptep);
2704+
}
2705+
2706+
pte_unmap_unlock(ptep, ptl);
2707+
*src = MIGRATE_PFN_MIGRATE;
2708+
return;
2709+
2710+
abort:
2711+
*src &= ~MIGRATE_PFN_MIGRATE;
2712+
}
2713+
25572714
/*
25582715
* migrate_vma_pages() - migrate meta-data from src page to dst page
25592716
* @migrate: migrate struct containing all migration information
@@ -2566,18 +2723,38 @@ static void migrate_vma_pages(struct migrate_vma *migrate)
25662723
{
25672724
const unsigned long npages = migrate->npages;
25682725
const unsigned long start = migrate->start;
2569-
unsigned long addr, i;
2726+
struct vm_area_struct *vma = migrate->vma;
2727+
struct mm_struct *mm = vma->vm_mm;
2728+
unsigned long addr, i, mmu_start;
2729+
bool notified = false;
25702730

25712731
for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) {
25722732
struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
25732733
struct page *page = migrate_pfn_to_page(migrate->src[i]);
25742734
struct address_space *mapping;
25752735
int r;
25762736

2577-
if (!page || !newpage)
2737+
if (!newpage) {
2738+
migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
25782739
continue;
2579-
if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE))
2740+
}
2741+
2742+
if (!page) {
2743+
if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE)) {
2744+
continue;
2745+
}
2746+
if (!notified) {
2747+
mmu_start = addr;
2748+
notified = true;
2749+
mmu_notifier_invalidate_range_start(mm,
2750+
mmu_start,
2751+
migrate->end);
2752+
}
2753+
migrate_vma_insert_page(migrate, addr, newpage,
2754+
&migrate->src[i],
2755+
&migrate->dst[i]);
25802756
continue;
2757+
}
25812758

25822759
mapping = page_mapping(page);
25832760

@@ -2605,6 +2782,10 @@ static void migrate_vma_pages(struct migrate_vma *migrate)
26052782
if (r != MIGRATEPAGE_SUCCESS)
26062783
migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
26072784
}
2785+
2786+
if (notified)
2787+
mmu_notifier_invalidate_range_end(mm, mmu_start,
2788+
migrate->end);
26082789
}
26092790

26102791
/*
@@ -2627,8 +2808,14 @@ static void migrate_vma_finalize(struct migrate_vma *migrate)
26272808
struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
26282809
struct page *page = migrate_pfn_to_page(migrate->src[i]);
26292810

2630-
if (!page)
2811+
if (!page) {
2812+
if (newpage) {
2813+
unlock_page(newpage);
2814+
put_page(newpage);
2815+
}
26312816
continue;
2817+
}
2818+
26322819
if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) {
26332820
if (newpage) {
26342821
unlock_page(newpage);

0 commit comments

Comments
 (0)