mm/hmm: fixes for device private page migration
Subsystems (4)
cover mm/hmm: fixes for device private page migration
Testing the latest linux git tree turned up a few bugs with page migration to and from ZONE_DEVICE private and anonymous pages. Hopefully it clarifies how ZONE_DEVICE private struct page uses the same mapping and index fields from the source anonymous page mapping.
Patch #3 was sent earlier and this is v2 with an updated change log.
http://lkml.kernel.org/r/[email protected]
Ralph Campbell (3):
mm: document zone device struct page reserved fields mm/hmm: fix ZONE_DEVICE anon page mapping reuse mm/hmm: Fix bad subpage pointer in try_to_unmap_one
include/linux/mm_types.h | 9 ++++++++- kernel/memremap.c | 4 ++++ mm/rmap.c | 1 + 3 files changed, 13 insertions(+), 1 deletion(-)
Signature · 1 line
2.20.1
PATCH 1/3 mm: document zone device struct page reserved fields
Struct page for ZONE_DEVICE private pages uses the reserved fields when anonymous pages are migrated to device private memory. This is so the page->mapping and page->index fields are preserved and the page can be migrated back to system memory.
Document this in comments so it is more clear.
--- include/linux/mm_types.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
include/linux/mm_types.h +8 -1
@@ -159,7 +159,14 @@ struct page {/** @pgmap: Points to the hosting device page map. */ struct dev_pagemap *pgmap;void *zone_device_data; -unsigned long _zd_pad_1;/* uses mapping */ +/* +* The following fields are used to hold the source +* page anonymous mapping information while it is +* migrated to device memory. See migrate_page(). +*/ +unsigned long _zd_pad_1;/* aliases mapping */ +unsigned long _zd_pad_2;/* aliases index */ +unsigned long _zd_pad_3;/* aliases private */ };/** @rcu_head: You can use this to free a page by RCU. */
Signature · 1 line
2.20.1
PATCH 2/3 mm/hmm: fix ZONE_DEVICE anon page mapping reuse
In reply to Ralph CampbellWhen a ZONE_DEVICE private page is freed, the page->mapping field can be set. If this page is reused as an anonymous page, the previous value can prevent the page from being inserted into the CPU's anon rmap table. For example, when migrating a pte_none() page to device memory:
migrate_vma(ops, vma, start, end, src, dst, private)
migrate_vma_collect()
src[] = MIGRATE_PFN_MIGRATE
migrate_vma_prepare()
/* no page to lock or isolate so OK */
migrate_vma_unmap()
/* no page to unmap so OK */
ops->alloc_and_copy()
/* driver allocates ZONE_DEVICE page for dst[] */
migrate_vma_pages()
migrate_vma_insert_page()
page_add_new_anon_rmap()
__page_set_anon_rmap()
/* This check sees the page's stale mapping field */
if (PageAnon(page))
return
/* page->mapping is not updated */The result is that the migration appears to succeed but a subsequent CPU fault will be unable to migrate the page back to system memory or worse.
Clear the page->mapping field when freeing the ZONE_DEVICE page so stale pointer data doesn't affect future page use.
--- kernel/memremap.c | 4 ++++ 1 file changed, 4 insertions(+)
kernel/memremap.c +4 -0
@@ -408,6 +408,10 @@ void __put_devmap_managed_page(struct page *page)mem_cgroup_uncharge (page); +/* Clear anonymous page mapping to prevent stale pointers */ +if (is_device_private_page (page)) + page->mapping =NULL ; + page->pgmap->ops->page_free (page); }else if (!count)__put_page (page);
Signature · 1 line
2.20.1
PATCH 3/3 mm/hmm: Fix bad subpage pointer in try_to_unmap_one
In reply to Ralph CampbellWhen migrating an anonymous private page to a ZONE_DEVICE private page, the source page->mapping and page->index fields are copied to the destination ZONE_DEVICE struct page and the page_mapcount() is increased. This is so rmap_walk() can be used to unmap and migrate the page back to system memory. However, try_to_unmap_one() computes the subpage pointer from a swap pte which computes an invalid page pointer and a kernel panic results such as:
BUG: unable to handle page fault for address: ffffea1fffffffc8
Currently, only single pages can be migrated to device private memory so no subpage computation is needed and it can be set to "page".
--- mm/rmap.c | 1 + 1 file changed, 1 insertion(+)
mm/rmap.c +1 -0
@@ -1476,6 +1476,7 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,* No need to invalidate here it will synchronize on * against the special swap migration pte. */ + subpage = page; goto discard; }
Signature · 1 line
2.20.1