Device coherent pages represent memory on a coherently attached device such as a GPU which is usually under the control of a driver. These pages should not be pinned as the driver needs to be able to move pages as required. Currently this is enforced by failing any attempt to pin a device coherent page.
A similar problem exists for ZONE_MOVABLE pages. In that case though the pages are migrated instead of causing failure. There is no reason the kernel can't migrate device coherent pages so this series implements migration for device coherent pages so the same strategy of migrate and pin can be used.
This series depends on the series "Add MEMORY_DEVICE_COHERENT for coherent device memory mapping"[1] which is in linux-next-20220204 and should apply cleanly to that.
[1] - https://lore.kernel.org/linux-mm/20220128200825.8623-1-alex.sierra@amd.com/
Changes for v2:
- Rebased on to linux-next-20220204
Alex Sierra (1): tools: add hmm gup test for long term pinned device pages
Alistair Popple (2): migrate.c: Remove vma check in migrate_vma_setup() mm/gup.c: Migrate device coherent pages when pinning instead of failing
mm/gup.c | 105 +++++++++++++++++++++++--- mm/migrate.c | 34 ++++---- tools/testing/selftests/vm/Makefile | 2 +- tools/testing/selftests/vm/hmm-tests.c | 81 ++++++++++++++++++++- 4 files changed, 194 insertions(+), 28 deletions(-)
base-commit: ef6b35306dd8f15a7e5e5a2532e665917a43c5d9
migrate_vma_setup() checks that a valid vma is passed so that the page tables can be walked to find the pfns associated with a given address range. However in some cases the pfns are already known, such as when migrating device coherent pages during pin_user_pages() meaning a valid vma isn't required.
Signed-off-by: Alistair Popple apopple@nvidia.com Acked-by: Felix Kuehling Felix.Kuehling@amd.com ---
Changes for v2:
- Added Felix's Acked-by
mm/migrate.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
diff --git a/mm/migrate.c b/mm/migrate.c index a9aed12..0d6570d 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -2602,24 +2602,24 @@ int migrate_vma_setup(struct migrate_vma *args)
args->start &= PAGE_MASK; args->end &= PAGE_MASK; - if (!args->vma || is_vm_hugetlb_page(args->vma) || - (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma)) - return -EINVAL; - if (nr_pages <= 0) - return -EINVAL; - if (args->start < args->vma->vm_start || - args->start >= args->vma->vm_end) - return -EINVAL; - if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end) - return -EINVAL; if (!args->src || !args->dst) return -EINVAL; - - memset(args->src, 0, sizeof(*args->src) * nr_pages); - args->cpages = 0; - args->npages = 0; - - migrate_vma_collect(args); + if (args->vma) { + if (is_vm_hugetlb_page(args->vma) || + (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma)) + return -EINVAL; + if (args->start < args->vma->vm_start || + args->start >= args->vma->vm_end) + return -EINVAL; + if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end) + return -EINVAL; + + memset(args->src, 0, sizeof(*args->src) * nr_pages); + args->cpages = 0; + args->npages = 0; + + migrate_vma_collect(args); + }
if (args->cpages) migrate_vma_unmap(args); @@ -2804,7 +2804,7 @@ void migrate_vma_pages(struct migrate_vma *migrate) continue; }
- if (!page) { + if (!page && migrate->vma) { if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE)) continue; if (!notified) {
On 2/6/22 20:26, Alistair Popple wrote:
migrate_vma_setup() checks that a valid vma is passed so that the page tables can be walked to find the pfns associated with a given address range. However in some cases the pfns are already known, such as when migrating device coherent pages during pin_user_pages() meaning a valid vma isn't required.
Signed-off-by: Alistair Popple apopple@nvidia.com Acked-by: Felix Kuehling Felix.Kuehling@amd.com
Changes for v2:
- Added Felix's Acked-by
mm/migrate.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
Hi Alistair,
Another late-breaking review question, below. :)
diff --git a/mm/migrate.c b/mm/migrate.c index a9aed12..0d6570d 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -2602,24 +2602,24 @@ int migrate_vma_setup(struct migrate_vma *args)
args->start &= PAGE_MASK; args->end &= PAGE_MASK;
- if (!args->vma || is_vm_hugetlb_page(args->vma) ||
(args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma))
return -EINVAL;
- if (nr_pages <= 0)
return -EINVAL;
Was the above check left out intentionally? If so, then it needs a commit description note. And maybe even a separate patch, because it changes the behavior.
If you do want to change the behavior:
* The kerneldoc comment above this function supports such a change: it requires returning 0 for the case of zero pages requested. So your change would bring the comments into alignment with the code.
* I don't think memset deals properly with a zero length input arg, so it's probably better to return 0, before that point.
thanks,
Currently any attempts to pin a device coherent page will fail. This is because device coherent pages need to be managed by a device driver, and pinning them would prevent a driver from migrating them off the device.
However this is no reason to fail pinning of these pages. These are coherent and accessible from the CPU so can be migrated just like pinning ZONE_MOVABLE pages. So instead of failing all attempts to pin them first try migrating them out of ZONE_DEVICE.
Signed-off-by: Alistair Popple apopple@nvidia.com Acked-by: Felix Kuehling Felix.Kuehling@amd.com ---
Changes for v2:
- Added Felix's Acked-by - Fixed missing check for dpage == NULL
mm/gup.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 95 insertions(+), 10 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c index 56d9577..5e826db 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1861,6 +1861,60 @@ struct page *get_dump_page(unsigned long addr)
#ifdef CONFIG_MIGRATION /* + * Migrates a device coherent page back to normal memory. Caller should have a + * reference on page which will be copied to the new page if migration is + * successful or dropped on failure. + */ +static struct page *migrate_device_page(struct page *page, + unsigned int gup_flags) +{ + struct page *dpage; + struct migrate_vma args; + unsigned long src_pfn, dst_pfn = 0; + + lock_page(page); + src_pfn = migrate_pfn(page_to_pfn(page)) | MIGRATE_PFN_MIGRATE; + args.src = &src_pfn; + args.dst = &dst_pfn; + args.cpages = 1; + args.npages = 1; + args.vma = NULL; + migrate_vma_setup(&args); + if (!(src_pfn & MIGRATE_PFN_MIGRATE)) + return NULL; + + dpage = alloc_pages(GFP_USER | __GFP_NOWARN, 0); + + /* + * get/pin the new page now so we don't have to retry gup after + * migrating. We already have a reference so this should never fail. + */ + if (dpage && WARN_ON_ONCE(!try_grab_page(dpage, gup_flags))) { + __free_pages(dpage, 0); + dpage = NULL; + } + + if (dpage) { + lock_page(dpage); + dst_pfn = migrate_pfn(page_to_pfn(dpage)); + } + + migrate_vma_pages(&args); + if (src_pfn & MIGRATE_PFN_MIGRATE) + copy_highpage(dpage, page); + migrate_vma_finalize(&args); + if (dpage && !(src_pfn & MIGRATE_PFN_MIGRATE)) { + if (gup_flags & FOLL_PIN) + unpin_user_page(dpage); + else + put_page(dpage); + dpage = NULL; + } + + return dpage; +} + +/* * Check whether all pages are pinnable, if so return number of pages. If some * pages are not pinnable, migrate them, and unpin all pages. Return zero if * pages were migrated, or if some pages were not successfully isolated. @@ -1888,15 +1942,40 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, continue; prev_head = head; /* - * If we get a movable page, since we are going to be pinning - * these entries, try to move them out if possible. + * Device coherent pages are managed by a driver and should not + * be pinned indefinitely as it prevents the driver moving the + * page. So when trying to pin with FOLL_LONGTERM instead try + * migrating page out of device memory. */ if (is_dev_private_or_coherent_page(head)) { + /* + * device private pages will get faulted in during gup + * so it shouldn't be possible to see one here. + */ WARN_ON_ONCE(is_device_private_page(head)); - ret = -EFAULT; - goto unpin_pages; + WARN_ON_ONCE(PageCompound(head)); + + /* + * migration will fail if the page is pinned, so convert + * the pin on the source page to a normal reference. + */ + if (gup_flags & FOLL_PIN) { + get_page(head); + unpin_user_page(head); + } + + pages[i] = migrate_device_page(head, gup_flags); + if (!pages[i]) { + ret = -EBUSY; + break; + } + continue; }
+ /* + * If we get a movable page, since we are going to be pinning + * these entries, try to move them out if possible. + */ if (!is_pinnable_page(head)) { if (PageHuge(head)) { if (!isolate_huge_page(head, &movable_page_list)) @@ -1924,16 +2003,22 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, * If list is empty, and no isolation errors, means that all pages are * in the correct zone. */ - if (list_empty(&movable_page_list) && !isolation_error_count) + if (!ret && list_empty(&movable_page_list) && !isolation_error_count) return nr_pages;
-unpin_pages: - if (gup_flags & FOLL_PIN) { - unpin_user_pages(pages, nr_pages); - } else { - for (i = 0; i < nr_pages; i++) + for (i = 0; i < nr_pages; i++) + if (!pages[i]) + continue; + else if (gup_flags & FOLL_PIN) + unpin_user_page(pages[i]); + else put_page(pages[i]); + + if (ret && !list_empty(&movable_page_list)) { + putback_movable_pages(&movable_page_list); + return ret; } + if (!list_empty(&movable_page_list)) { ret = migrate_pages(&movable_page_list, alloc_migration_target, NULL, (unsigned long)&mtc, MIGRATE_SYNC,
On 07.02.22 05:26, Alistair Popple wrote:
Currently any attempts to pin a device coherent page will fail. This is because device coherent pages need to be managed by a device driver, and pinning them would prevent a driver from migrating them off the device.
However this is no reason to fail pinning of these pages. These are coherent and accessible from the CPU so can be migrated just like pinning ZONE_MOVABLE pages. So instead of failing all attempts to pin them first try migrating them out of ZONE_DEVICE.
Signed-off-by: Alistair Popple apopple@nvidia.com Acked-by: Felix Kuehling Felix.Kuehling@amd.com
Changes for v2:
- Added Felix's Acked-by
- Fixed missing check for dpage == NULL
mm/gup.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 95 insertions(+), 10 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c index 56d9577..5e826db 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1861,6 +1861,60 @@ struct page *get_dump_page(unsigned long addr)
#ifdef CONFIG_MIGRATION /*
- Migrates a device coherent page back to normal memory. Caller should have a
- reference on page which will be copied to the new page if migration is
- successful or dropped on failure.
- */
+static struct page *migrate_device_page(struct page *page,
unsigned int gup_flags)
+{
- struct page *dpage;
- struct migrate_vma args;
- unsigned long src_pfn, dst_pfn = 0;
- lock_page(page);
- src_pfn = migrate_pfn(page_to_pfn(page)) | MIGRATE_PFN_MIGRATE;
- args.src = &src_pfn;
- args.dst = &dst_pfn;
- args.cpages = 1;
- args.npages = 1;
- args.vma = NULL;
- migrate_vma_setup(&args);
- if (!(src_pfn & MIGRATE_PFN_MIGRATE))
return NULL;
- dpage = alloc_pages(GFP_USER | __GFP_NOWARN, 0);
- /*
* get/pin the new page now so we don't have to retry gup after
* migrating. We already have a reference so this should never fail.
*/
- if (dpage && WARN_ON_ONCE(!try_grab_page(dpage, gup_flags))) {
__free_pages(dpage, 0);
dpage = NULL;
- }
- if (dpage) {
lock_page(dpage);
dst_pfn = migrate_pfn(page_to_pfn(dpage));
- }
- migrate_vma_pages(&args);
- if (src_pfn & MIGRATE_PFN_MIGRATE)
copy_highpage(dpage, page);
- migrate_vma_finalize(&args);
- if (dpage && !(src_pfn & MIGRATE_PFN_MIGRATE)) {
if (gup_flags & FOLL_PIN)
unpin_user_page(dpage);
else
put_page(dpage);
dpage = NULL;
- }
- return dpage;
+}
+/*
- Check whether all pages are pinnable, if so return number of pages. If some
- pages are not pinnable, migrate them, and unpin all pages. Return zero if
- pages were migrated, or if some pages were not successfully isolated.
@@ -1888,15 +1942,40 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, continue; prev_head = head; /*
* If we get a movable page, since we are going to be pinning
* these entries, try to move them out if possible.
* Device coherent pages are managed by a driver and should not
* be pinned indefinitely as it prevents the driver moving the
* page. So when trying to pin with FOLL_LONGTERM instead try
*/ if (is_dev_private_or_coherent_page(head)) {* migrating page out of device memory.
/*
* device private pages will get faulted in during gup
* so it shouldn't be possible to see one here.
*/ WARN_ON_ONCE(is_device_private_page(head));
ret = -EFAULT;
goto unpin_pages;
WARN_ON_ONCE(PageCompound(head));
/*
* migration will fail if the page is pinned, so convert
* the pin on the source page to a normal reference.
*/
if (gup_flags & FOLL_PIN) {
get_page(head);
unpin_user_page(head);
}
pages[i] = migrate_device_page(head, gup_flags);
For ordinary migrate_pages(), we'll unpin all pages and return 0 so the caller will retry pinning by walking the page tables again.
Why can't we apply the same mechanism here? This "let's avoid another walk" looks unnecessary complicated to me, but I might be wrong.
On Thursday, 10 February 2022 9:53:38 PM AEDT David Hildenbrand wrote:
On 07.02.22 05:26, Alistair Popple wrote:
Currently any attempts to pin a device coherent page will fail. This is because device coherent pages need to be managed by a device driver, and pinning them would prevent a driver from migrating them off the device.
However this is no reason to fail pinning of these pages. These are coherent and accessible from the CPU so can be migrated just like pinning ZONE_MOVABLE pages. So instead of failing all attempts to pin them first try migrating them out of ZONE_DEVICE.
Signed-off-by: Alistair Popple apopple@nvidia.com Acked-by: Felix Kuehling Felix.Kuehling@amd.com
Changes for v2:
- Added Felix's Acked-by
- Fixed missing check for dpage == NULL
mm/gup.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 95 insertions(+), 10 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c index 56d9577..5e826db 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1861,6 +1861,60 @@ struct page *get_dump_page(unsigned long addr)
#ifdef CONFIG_MIGRATION /*
- Migrates a device coherent page back to normal memory. Caller should have a
- reference on page which will be copied to the new page if migration is
- successful or dropped on failure.
- */
+static struct page *migrate_device_page(struct page *page,
unsigned int gup_flags)
+{
- struct page *dpage;
- struct migrate_vma args;
- unsigned long src_pfn, dst_pfn = 0;
- lock_page(page);
- src_pfn = migrate_pfn(page_to_pfn(page)) | MIGRATE_PFN_MIGRATE;
- args.src = &src_pfn;
- args.dst = &dst_pfn;
- args.cpages = 1;
- args.npages = 1;
- args.vma = NULL;
- migrate_vma_setup(&args);
- if (!(src_pfn & MIGRATE_PFN_MIGRATE))
return NULL;
- dpage = alloc_pages(GFP_USER | __GFP_NOWARN, 0);
- /*
* get/pin the new page now so we don't have to retry gup after
* migrating. We already have a reference so this should never fail.
*/
- if (dpage && WARN_ON_ONCE(!try_grab_page(dpage, gup_flags))) {
__free_pages(dpage, 0);
dpage = NULL;
- }
- if (dpage) {
lock_page(dpage);
dst_pfn = migrate_pfn(page_to_pfn(dpage));
- }
- migrate_vma_pages(&args);
- if (src_pfn & MIGRATE_PFN_MIGRATE)
copy_highpage(dpage, page);
- migrate_vma_finalize(&args);
- if (dpage && !(src_pfn & MIGRATE_PFN_MIGRATE)) {
if (gup_flags & FOLL_PIN)
unpin_user_page(dpage);
else
put_page(dpage);
dpage = NULL;
- }
- return dpage;
+}
+/*
- Check whether all pages are pinnable, if so return number of pages. If some
- pages are not pinnable, migrate them, and unpin all pages. Return zero if
- pages were migrated, or if some pages were not successfully isolated.
@@ -1888,15 +1942,40 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, continue; prev_head = head; /*
* If we get a movable page, since we are going to be pinning
* these entries, try to move them out if possible.
* Device coherent pages are managed by a driver and should not
* be pinned indefinitely as it prevents the driver moving the
* page. So when trying to pin with FOLL_LONGTERM instead try
*/ if (is_dev_private_or_coherent_page(head)) {* migrating page out of device memory.
/*
* device private pages will get faulted in during gup
* so it shouldn't be possible to see one here.
*/ WARN_ON_ONCE(is_device_private_page(head));
ret = -EFAULT;
goto unpin_pages;
WARN_ON_ONCE(PageCompound(head));
/*
* migration will fail if the page is pinned, so convert
* the pin on the source page to a normal reference.
*/
if (gup_flags & FOLL_PIN) {
get_page(head);
unpin_user_page(head);
}
pages[i] = migrate_device_page(head, gup_flags);
For ordinary migrate_pages(), we'll unpin all pages and return 0 so the caller will retry pinning by walking the page tables again.
Why can't we apply the same mechanism here? This "let's avoid another walk" looks unnecessary complicated to me, but I might be wrong.
There's no reason we couldn't. I figured we have the page in the right spot anyway so it was easy to do, and looking at this rebased on top of Christoph's ZONE_DEVICE refcount simplification I'm not sure it would be any simpler anyway.
It would remove the call to try_grab_page(), but we'd still have to return an error on migration failures whilst also ensuring we putback any non-device pages that may have been isolated. I might have overlooked something though, so certainly happy for suggestions.
On 10.02.22 12:39, Alistair Popple wrote:
On Thursday, 10 February 2022 9:53:38 PM AEDT David Hildenbrand wrote:
On 07.02.22 05:26, Alistair Popple wrote:
Currently any attempts to pin a device coherent page will fail. This is because device coherent pages need to be managed by a device driver, and pinning them would prevent a driver from migrating them off the device.
However this is no reason to fail pinning of these pages. These are coherent and accessible from the CPU so can be migrated just like pinning ZONE_MOVABLE pages. So instead of failing all attempts to pin them first try migrating them out of ZONE_DEVICE.
Signed-off-by: Alistair Popple apopple@nvidia.com Acked-by: Felix Kuehling Felix.Kuehling@amd.com
Changes for v2:
- Added Felix's Acked-by
- Fixed missing check for dpage == NULL
mm/gup.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 95 insertions(+), 10 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c index 56d9577..5e826db 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1861,6 +1861,60 @@ struct page *get_dump_page(unsigned long addr)
#ifdef CONFIG_MIGRATION /*
- Migrates a device coherent page back to normal memory. Caller should have a
- reference on page which will be copied to the new page if migration is
- successful or dropped on failure.
- */
+static struct page *migrate_device_page(struct page *page,
unsigned int gup_flags)
+{
- struct page *dpage;
- struct migrate_vma args;
- unsigned long src_pfn, dst_pfn = 0;
- lock_page(page);
- src_pfn = migrate_pfn(page_to_pfn(page)) | MIGRATE_PFN_MIGRATE;
- args.src = &src_pfn;
- args.dst = &dst_pfn;
- args.cpages = 1;
- args.npages = 1;
- args.vma = NULL;
- migrate_vma_setup(&args);
- if (!(src_pfn & MIGRATE_PFN_MIGRATE))
return NULL;
- dpage = alloc_pages(GFP_USER | __GFP_NOWARN, 0);
- /*
* get/pin the new page now so we don't have to retry gup after
* migrating. We already have a reference so this should never fail.
*/
- if (dpage && WARN_ON_ONCE(!try_grab_page(dpage, gup_flags))) {
__free_pages(dpage, 0);
dpage = NULL;
- }
- if (dpage) {
lock_page(dpage);
dst_pfn = migrate_pfn(page_to_pfn(dpage));
- }
- migrate_vma_pages(&args);
- if (src_pfn & MIGRATE_PFN_MIGRATE)
copy_highpage(dpage, page);
- migrate_vma_finalize(&args);
- if (dpage && !(src_pfn & MIGRATE_PFN_MIGRATE)) {
if (gup_flags & FOLL_PIN)
unpin_user_page(dpage);
else
put_page(dpage);
dpage = NULL;
- }
- return dpage;
+}
+/*
- Check whether all pages are pinnable, if so return number of pages. If some
- pages are not pinnable, migrate them, and unpin all pages. Return zero if
- pages were migrated, or if some pages were not successfully isolated.
@@ -1888,15 +1942,40 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, continue; prev_head = head; /*
* If we get a movable page, since we are going to be pinning
* these entries, try to move them out if possible.
* Device coherent pages are managed by a driver and should not
* be pinned indefinitely as it prevents the driver moving the
* page. So when trying to pin with FOLL_LONGTERM instead try
*/ if (is_dev_private_or_coherent_page(head)) {* migrating page out of device memory.
/*
* device private pages will get faulted in during gup
* so it shouldn't be possible to see one here.
*/ WARN_ON_ONCE(is_device_private_page(head));
ret = -EFAULT;
goto unpin_pages;
WARN_ON_ONCE(PageCompound(head));
/*
* migration will fail if the page is pinned, so convert
* the pin on the source page to a normal reference.
*/
if (gup_flags & FOLL_PIN) {
get_page(head);
unpin_user_page(head);
}
pages[i] = migrate_device_page(head, gup_flags);
For ordinary migrate_pages(), we'll unpin all pages and return 0 so the caller will retry pinning by walking the page tables again.
Why can't we apply the same mechanism here? This "let's avoid another walk" looks unnecessary complicated to me, but I might be wrong.
There's no reason we couldn't. I figured we have the page in the right spot anyway so it was easy to do, and looking at this rebased on top of Christoph's ZONE_DEVICE refcount simplification I'm not sure it would be any simpler anyway.
It would remove the call to try_grab_page(), but we'd still have to return an error on migration failures whilst also ensuring we putback any non-device pages that may have been isolated. I might have overlooked something though, so certainly happy for suggestions.
Staring at the code, I was wondering if we could either
* build a second list of device coherent pages to migrate and call a migrate_device_pages() bulk function * simply use movable_page_list() and teach migrate_pages() how to handle them.
I'd really appreciate as little special casing as possible for the ever growing list of new DEVICE types all over the place. E.g., just staring at fork even before the new device coherent made my head spin.
On Thursday, 10 February 2022 10:47:35 PM AEDT David Hildenbrand wrote:
On 10.02.22 12:39, Alistair Popple wrote:
On Thursday, 10 February 2022 9:53:38 PM AEDT David Hildenbrand wrote:
On 07.02.22 05:26, Alistair Popple wrote:
Currently any attempts to pin a device coherent page will fail. This is because device coherent pages need to be managed by a device driver, and pinning them would prevent a driver from migrating them off the device.
However this is no reason to fail pinning of these pages. These are coherent and accessible from the CPU so can be migrated just like pinning ZONE_MOVABLE pages. So instead of failing all attempts to pin them first try migrating them out of ZONE_DEVICE.
Signed-off-by: Alistair Popple apopple@nvidia.com Acked-by: Felix Kuehling Felix.Kuehling@amd.com
Changes for v2:
- Added Felix's Acked-by
- Fixed missing check for dpage == NULL
mm/gup.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 95 insertions(+), 10 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c index 56d9577..5e826db 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1861,6 +1861,60 @@ struct page *get_dump_page(unsigned long addr)
#ifdef CONFIG_MIGRATION /*
- Migrates a device coherent page back to normal memory. Caller should have a
- reference on page which will be copied to the new page if migration is
- successful or dropped on failure.
- */
+static struct page *migrate_device_page(struct page *page,
unsigned int gup_flags)
+{
- struct page *dpage;
- struct migrate_vma args;
- unsigned long src_pfn, dst_pfn = 0;
- lock_page(page);
- src_pfn = migrate_pfn(page_to_pfn(page)) | MIGRATE_PFN_MIGRATE;
- args.src = &src_pfn;
- args.dst = &dst_pfn;
- args.cpages = 1;
- args.npages = 1;
- args.vma = NULL;
- migrate_vma_setup(&args);
- if (!(src_pfn & MIGRATE_PFN_MIGRATE))
return NULL;
- dpage = alloc_pages(GFP_USER | __GFP_NOWARN, 0);
- /*
* get/pin the new page now so we don't have to retry gup after
* migrating. We already have a reference so this should never fail.
*/
- if (dpage && WARN_ON_ONCE(!try_grab_page(dpage, gup_flags))) {
__free_pages(dpage, 0);
dpage = NULL;
- }
- if (dpage) {
lock_page(dpage);
dst_pfn = migrate_pfn(page_to_pfn(dpage));
- }
- migrate_vma_pages(&args);
- if (src_pfn & MIGRATE_PFN_MIGRATE)
copy_highpage(dpage, page);
- migrate_vma_finalize(&args);
- if (dpage && !(src_pfn & MIGRATE_PFN_MIGRATE)) {
if (gup_flags & FOLL_PIN)
unpin_user_page(dpage);
else
put_page(dpage);
dpage = NULL;
- }
- return dpage;
+}
+/*
- Check whether all pages are pinnable, if so return number of pages. If some
- pages are not pinnable, migrate them, and unpin all pages. Return zero if
- pages were migrated, or if some pages were not successfully isolated.
@@ -1888,15 +1942,40 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, continue; prev_head = head; /*
* If we get a movable page, since we are going to be pinning
* these entries, try to move them out if possible.
* Device coherent pages are managed by a driver and should not
* be pinned indefinitely as it prevents the driver moving the
* page. So when trying to pin with FOLL_LONGTERM instead try
*/ if (is_dev_private_or_coherent_page(head)) {* migrating page out of device memory.
/*
* device private pages will get faulted in during gup
* so it shouldn't be possible to see one here.
*/ WARN_ON_ONCE(is_device_private_page(head));
ret = -EFAULT;
goto unpin_pages;
WARN_ON_ONCE(PageCompound(head));
/*
* migration will fail if the page is pinned, so convert
* the pin on the source page to a normal reference.
*/
if (gup_flags & FOLL_PIN) {
get_page(head);
unpin_user_page(head);
}
pages[i] = migrate_device_page(head, gup_flags);
For ordinary migrate_pages(), we'll unpin all pages and return 0 so the caller will retry pinning by walking the page tables again.
Why can't we apply the same mechanism here? This "let's avoid another walk" looks unnecessary complicated to me, but I might be wrong.
There's no reason we couldn't. I figured we have the page in the right spot anyway so it was easy to do, and looking at this rebased on top of Christoph's ZONE_DEVICE refcount simplification I'm not sure it would be any simpler anyway.
It would remove the call to try_grab_page(), but we'd still have to return an error on migration failures whilst also ensuring we putback any non-device pages that may have been isolated. I might have overlooked something though, so certainly happy for suggestions.
Staring at the code, I was wondering if we could either
- build a second list of device coherent pages to migrate and call a migrate_device_pages() bulk function
- simply use movable_page_list() and teach migrate_pages() how to handle them.
I did consider that approach. The problem is zone device pages are not LRU pages. In particular page->lru is not available to add the page to a list, and as an external API and internally migrate_pages() relies heavily on moving pages between lists.
I'd really appreciate as little special casing as possible for the ever growing list of new DEVICE types all over the place. E.g., just staring at fork even before the new device coherent made my head spin.
That's fair. We could pull the checks for device pages out into a self contained function (eg. check_and_migrate_device_pages()) called before check_and_migrate_movable_pages(). The down side of that is we'd always have an extra loop over all the pages just to scan for device pages, but perhaps that's not a concern?
On 11.02.22 00:41, Alistair Popple wrote:
On Thursday, 10 February 2022 10:47:35 PM AEDT David Hildenbrand wrote:
On 10.02.22 12:39, Alistair Popple wrote:
On Thursday, 10 February 2022 9:53:38 PM AEDT David Hildenbrand wrote:
On 07.02.22 05:26, Alistair Popple wrote:
Currently any attempts to pin a device coherent page will fail. This is because device coherent pages need to be managed by a device driver, and pinning them would prevent a driver from migrating them off the device.
However this is no reason to fail pinning of these pages. These are coherent and accessible from the CPU so can be migrated just like pinning ZONE_MOVABLE pages. So instead of failing all attempts to pin them first try migrating them out of ZONE_DEVICE.
Signed-off-by: Alistair Popple apopple@nvidia.com Acked-by: Felix Kuehling Felix.Kuehling@amd.com
Changes for v2:
- Added Felix's Acked-by
- Fixed missing check for dpage == NULL
mm/gup.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 95 insertions(+), 10 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c index 56d9577..5e826db 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1861,6 +1861,60 @@ struct page *get_dump_page(unsigned long addr)
#ifdef CONFIG_MIGRATION /*
- Migrates a device coherent page back to normal memory. Caller should have a
- reference on page which will be copied to the new page if migration is
- successful or dropped on failure.
- */
+static struct page *migrate_device_page(struct page *page,
unsigned int gup_flags)
+{
- struct page *dpage;
- struct migrate_vma args;
- unsigned long src_pfn, dst_pfn = 0;
- lock_page(page);
- src_pfn = migrate_pfn(page_to_pfn(page)) | MIGRATE_PFN_MIGRATE;
- args.src = &src_pfn;
- args.dst = &dst_pfn;
- args.cpages = 1;
- args.npages = 1;
- args.vma = NULL;
- migrate_vma_setup(&args);
- if (!(src_pfn & MIGRATE_PFN_MIGRATE))
return NULL;
- dpage = alloc_pages(GFP_USER | __GFP_NOWARN, 0);
- /*
* get/pin the new page now so we don't have to retry gup after
* migrating. We already have a reference so this should never fail.
*/
- if (dpage && WARN_ON_ONCE(!try_grab_page(dpage, gup_flags))) {
__free_pages(dpage, 0);
dpage = NULL;
- }
- if (dpage) {
lock_page(dpage);
dst_pfn = migrate_pfn(page_to_pfn(dpage));
- }
- migrate_vma_pages(&args);
- if (src_pfn & MIGRATE_PFN_MIGRATE)
copy_highpage(dpage, page);
- migrate_vma_finalize(&args);
- if (dpage && !(src_pfn & MIGRATE_PFN_MIGRATE)) {
if (gup_flags & FOLL_PIN)
unpin_user_page(dpage);
else
put_page(dpage);
dpage = NULL;
- }
- return dpage;
+}
+/*
- Check whether all pages are pinnable, if so return number of pages. If some
- pages are not pinnable, migrate them, and unpin all pages. Return zero if
- pages were migrated, or if some pages were not successfully isolated.
@@ -1888,15 +1942,40 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, continue; prev_head = head; /*
* If we get a movable page, since we are going to be pinning
* these entries, try to move them out if possible.
* Device coherent pages are managed by a driver and should not
* be pinned indefinitely as it prevents the driver moving the
* page. So when trying to pin with FOLL_LONGTERM instead try
*/ if (is_dev_private_or_coherent_page(head)) {* migrating page out of device memory.
/*
* device private pages will get faulted in during gup
* so it shouldn't be possible to see one here.
*/ WARN_ON_ONCE(is_device_private_page(head));
ret = -EFAULT;
goto unpin_pages;
WARN_ON_ONCE(PageCompound(head));
/*
* migration will fail if the page is pinned, so convert
* the pin on the source page to a normal reference.
*/
if (gup_flags & FOLL_PIN) {
get_page(head);
unpin_user_page(head);
}
pages[i] = migrate_device_page(head, gup_flags);
For ordinary migrate_pages(), we'll unpin all pages and return 0 so the caller will retry pinning by walking the page tables again.
Why can't we apply the same mechanism here? This "let's avoid another walk" looks unnecessary complicated to me, but I might be wrong.
There's no reason we couldn't. I figured we have the page in the right spot anyway so it was easy to do, and looking at this rebased on top of Christoph's ZONE_DEVICE refcount simplification I'm not sure it would be any simpler anyway.
It would remove the call to try_grab_page(), but we'd still have to return an error on migration failures whilst also ensuring we putback any non-device pages that may have been isolated. I might have overlooked something though, so certainly happy for suggestions.
Staring at the code, I was wondering if we could either
- build a second list of device coherent pages to migrate and call a migrate_device_pages() bulk function
- simply use movable_page_list() and teach migrate_pages() how to handle them.
(sorry for the late reply)
I did consider that approach. The problem is zone device pages are not LRU pages. In particular page->lru is not available to add the page to a list, and as an external API and internally migrate_pages() relies heavily on moving pages between lists.
I see, so I assume there is no way we could add them to a list? We could use a temporary array we'd try allocating once we stumble over over such a device page that needs migration.
The you'd teach is_pinnable_page() to reject is_dev_private_or_coherent_page() and special case is_dev_private_or_coherent_page() under the "if (!is_pinnable_page(head))" check.
You'd grab an additional reference and add them to the temp array. The you'd just proceed as normal towards the end of the function (reverting the pin/ref from the input array) and would try to migrate all device pages in the temp array just before migrate_pages() -- migrate_device_pages(), properly handling/indicating if either migration path fails.
Instead of putback_movable_pages() on the list you'd had unref_device_pages() and supply the array.
Just a thought to limit the impact and eventually make it a bit nicer to read, avoiding modifications of the input array.
I'd really appreciate as little special casing as possible for the ever growing list of new DEVICE types all over the place. E.g., just staring at fork even before the new device coherent made my head spin.
That's fair. We could pull the checks for device pages out into a self contained function (eg. check_and_migrate_device_pages()) called before check_and_migrate_movable_pages(). The down side of that is we'd always have an extra loop over all the pages just to scan for device pages, but perhaps that's not a concern?
I mean, they are movable ... just not "ordinarily" movable, so it smells like this logic belongs into check_and_migrate_movable_pages() :)
On 2/6/22 20:26, Alistair Popple wrote:
Currently any attempts to pin a device coherent page will fail. This is because device coherent pages need to be managed by a device driver, and pinning them would prevent a driver from migrating them off the device.
However this is no reason to fail pinning of these pages. These are coherent and accessible from the CPU so can be migrated just like pinning ZONE_MOVABLE pages. So instead of failing all attempts to pin them first try migrating them out of ZONE_DEVICE.
Hi Alistair and all,
Here's a possible issue (below) that I really should have spotted the first time around, sorry for this late-breaking review. And maybe it's actually just my misunderstanding, anyway.
Signed-off-by: Alistair Popple apopple@nvidia.com Acked-by: Felix Kuehling Felix.Kuehling@amd.com
Changes for v2:
- Added Felix's Acked-by
- Fixed missing check for dpage == NULL
mm/gup.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 95 insertions(+), 10 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c index 56d9577..5e826db 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1861,6 +1861,60 @@ struct page *get_dump_page(unsigned long addr)
#ifdef CONFIG_MIGRATION /*
- Migrates a device coherent page back to normal memory. Caller should have a
- reference on page which will be copied to the new page if migration is
- successful or dropped on failure.
- */
+static struct page *migrate_device_page(struct page *page,
unsigned int gup_flags)
+{
- struct page *dpage;
- struct migrate_vma args;
- unsigned long src_pfn, dst_pfn = 0;
- lock_page(page);
- src_pfn = migrate_pfn(page_to_pfn(page)) | MIGRATE_PFN_MIGRATE;
- args.src = &src_pfn;
- args.dst = &dst_pfn;
- args.cpages = 1;
- args.npages = 1;
- args.vma = NULL;
- migrate_vma_setup(&args);
- if (!(src_pfn & MIGRATE_PFN_MIGRATE))
return NULL;
- dpage = alloc_pages(GFP_USER | __GFP_NOWARN, 0);
- /*
* get/pin the new page now so we don't have to retry gup after
* migrating. We already have a reference so this should never fail.
*/
- if (dpage && WARN_ON_ONCE(!try_grab_page(dpage, gup_flags))) {
__free_pages(dpage, 0);
dpage = NULL;
- }
- if (dpage) {
lock_page(dpage);
dst_pfn = migrate_pfn(page_to_pfn(dpage));
- }
- migrate_vma_pages(&args);
- if (src_pfn & MIGRATE_PFN_MIGRATE)
copy_highpage(dpage, page);
- migrate_vma_finalize(&args);
- if (dpage && !(src_pfn & MIGRATE_PFN_MIGRATE)) {
if (gup_flags & FOLL_PIN)
unpin_user_page(dpage);
else
put_page(dpage);
dpage = NULL;
- }
- return dpage;
+}
+/*
- Check whether all pages are pinnable, if so return number of pages. If some
- pages are not pinnable, migrate them, and unpin all pages. Return zero if
- pages were migrated, or if some pages were not successfully isolated.
@@ -1888,15 +1942,40 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, continue; prev_head = head; /*
* If we get a movable page, since we are going to be pinning
* these entries, try to move them out if possible.
* Device coherent pages are managed by a driver and should not
* be pinned indefinitely as it prevents the driver moving the
* page. So when trying to pin with FOLL_LONGTERM instead try
*/ if (is_dev_private_or_coherent_page(head)) {* migrating page out of device memory.
/*
* device private pages will get faulted in during gup
* so it shouldn't be possible to see one here.
*/ WARN_ON_ONCE(is_device_private_page(head));
ret = -EFAULT;
goto unpin_pages;
WARN_ON_ONCE(PageCompound(head));
/*
* migration will fail if the page is pinned, so convert
* the pin on the source page to a normal reference.
*/
if (gup_flags & FOLL_PIN) {
get_page(head);
unpin_user_page(head);
OK...but now gup_flags can no longer be used as a guide for how to release these pages, right? In other words, up until this point, FOLL_PIN meant "call unpin_user_page() in order to release". However, now this page must be released via put_page().
See below...
}
pages[i] = migrate_device_page(head, gup_flags);
if (!pages[i]) {
ret = -EBUSY;
break;
}
continue;
}
/*
* If we get a movable page, since we are going to be pinning
* these entries, try to move them out if possible.
*/
if (!is_pinnable_page(head)) { if (PageHuge(head)) { if (!isolate_huge_page(head, &movable_page_list))
@@ -1924,16 +2003,22 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, * If list is empty, and no isolation errors, means that all pages are * in the correct zone. */
- if (list_empty(&movable_page_list) && !isolation_error_count)
- if (!ret && list_empty(&movable_page_list) && !isolation_error_count) return nr_pages;
-unpin_pages:
- if (gup_flags & FOLL_PIN) {
unpin_user_pages(pages, nr_pages);
- } else {
for (i = 0; i < nr_pages; i++)
- for (i = 0; i < nr_pages; i++)
if (!pages[i])
continue;
else if (gup_flags & FOLL_PIN)
unpin_user_page(pages[i]);
...and here, for example, we are still trusting gup_flags to decide how to release the pages.
This reminds me: out of the many things to monitor, the FOLL_PIN counts in /proc/vmstat are especially helpful, whenever making changes to code that deals with this:
nr_foll_pin_acquired nr_foll_pin_released
...and those should normally be equal to each other when "at rest".
thanks,
On Saturday, 12 February 2022 1:10:29 PM AEDT John Hubbard wrote:
On 2/6/22 20:26, Alistair Popple wrote:
Currently any attempts to pin a device coherent page will fail. This is because device coherent pages need to be managed by a device driver, and pinning them would prevent a driver from migrating them off the device.
However this is no reason to fail pinning of these pages. These are coherent and accessible from the CPU so can be migrated just like pinning ZONE_MOVABLE pages. So instead of failing all attempts to pin them first try migrating them out of ZONE_DEVICE.
Hi Alistair and all,
Here's a possible issue (below) that I really should have spotted the first time around, sorry for this late-breaking review. And maybe it's actually just my misunderstanding, anyway.
I think it might be a misunderstanding, see below.
Signed-off-by: Alistair Popple apopple@nvidia.com Acked-by: Felix Kuehling Felix.Kuehling@amd.com
Changes for v2:
- Added Felix's Acked-by
- Fixed missing check for dpage == NULL
mm/gup.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 95 insertions(+), 10 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c index 56d9577..5e826db 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1861,6 +1861,60 @@ struct page *get_dump_page(unsigned long addr)
#ifdef CONFIG_MIGRATION /*
- Migrates a device coherent page back to normal memory. Caller should have a
- reference on page which will be copied to the new page if migration is
- successful or dropped on failure.
- */
+static struct page *migrate_device_page(struct page *page,
unsigned int gup_flags)
+{
- struct page *dpage;
- struct migrate_vma args;
- unsigned long src_pfn, dst_pfn = 0;
- lock_page(page);
- src_pfn = migrate_pfn(page_to_pfn(page)) | MIGRATE_PFN_MIGRATE;
- args.src = &src_pfn;
- args.dst = &dst_pfn;
- args.cpages = 1;
- args.npages = 1;
- args.vma = NULL;
- migrate_vma_setup(&args);
- if (!(src_pfn & MIGRATE_PFN_MIGRATE))
return NULL;
- dpage = alloc_pages(GFP_USER | __GFP_NOWARN, 0);
- /*
* get/pin the new page now so we don't have to retry gup after
* migrating. We already have a reference so this should never fail.
*/
- if (dpage && WARN_ON_ONCE(!try_grab_page(dpage, gup_flags))) {
__free_pages(dpage, 0);
dpage = NULL;
- }
- if (dpage) {
lock_page(dpage);
dst_pfn = migrate_pfn(page_to_pfn(dpage));
- }
- migrate_vma_pages(&args);
- if (src_pfn & MIGRATE_PFN_MIGRATE)
copy_highpage(dpage, page);
- migrate_vma_finalize(&args);
- if (dpage && !(src_pfn & MIGRATE_PFN_MIGRATE)) {
if (gup_flags & FOLL_PIN)
unpin_user_page(dpage);
else
put_page(dpage);
dpage = NULL;
- }
- return dpage;
+}
+/*
- Check whether all pages are pinnable, if so return number of pages. If some
- pages are not pinnable, migrate them, and unpin all pages. Return zero if
- pages were migrated, or if some pages were not successfully isolated.
@@ -1888,15 +1942,40 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, continue; prev_head = head; /*
* If we get a movable page, since we are going to be pinning
* these entries, try to move them out if possible.
* Device coherent pages are managed by a driver and should not
* be pinned indefinitely as it prevents the driver moving the
* page. So when trying to pin with FOLL_LONGTERM instead try
*/ if (is_dev_private_or_coherent_page(head)) {* migrating page out of device memory.
/*
* device private pages will get faulted in during gup
* so it shouldn't be possible to see one here.
*/ WARN_ON_ONCE(is_device_private_page(head));
ret = -EFAULT;
goto unpin_pages;
WARN_ON_ONCE(PageCompound(head));
/*
* migration will fail if the page is pinned, so convert
* the pin on the source page to a normal reference.
*/
if (gup_flags & FOLL_PIN) {
get_page(head);
unpin_user_page(head);
OK...but now gup_flags can no longer be used as a guide for how to release these pages, right? In other words, up until this point, FOLL_PIN meant "call unpin_user_page() in order to release". However, now this page must be released via put_page().
This is the source page (head). We are unpinning it because we can't migrate a pinned page, however we still need a reference on it for migrate_vma hence the get_page followed by unpin. In the non-FOLL_PIN case we already have a reference from gup.
See below...
}
pages[i] = migrate_device_page(head, gup_flags);
migrate_device_page() will return a new page that has been correctly pinned with gup_flags by try_grab_page(). Therefore this page can still be released with unpin_user_page() or put_page() as appropriate for the given gup_flags.
The reference we had on the source page (head) always gets dropped in migrate_vma_finalize().
if (!pages[i]) {
ret = -EBUSY;
break;
}
continue;
}
/*
* If we get a movable page, since we are going to be pinning
* these entries, try to move them out if possible.
*/
if (!is_pinnable_page(head)) { if (PageHuge(head)) { if (!isolate_huge_page(head, &movable_page_list))
@@ -1924,16 +2003,22 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, * If list is empty, and no isolation errors, means that all pages are * in the correct zone. */
- if (list_empty(&movable_page_list) && !isolation_error_count)
- if (!ret && list_empty(&movable_page_list) && !isolation_error_count) return nr_pages;
-unpin_pages:
- if (gup_flags & FOLL_PIN) {
unpin_user_pages(pages, nr_pages);
- } else {
for (i = 0; i < nr_pages; i++)
- for (i = 0; i < nr_pages; i++)
if (!pages[i])
continue;
else if (gup_flags & FOLL_PIN)
unpin_user_page(pages[i]);
...and here, for example, we are still trusting gup_flags to decide how to release the pages.
Which unless I've missed something is still the correct thing to do.
This reminds me: out of the many things to monitor, the FOLL_PIN counts in /proc/vmstat are especially helpful, whenever making changes to code that deals with this:
nr_foll_pin_acquired nr_foll_pin_released
...and those should normally be equal to each other when "at rest".
thanks,
else put_page(pages[i]);
- if (ret && !list_empty(&movable_page_list)) {
putback_movable_pages(&movable_page_list);
}return ret;
- if (!list_empty(&movable_page_list)) { ret = migrate_pages(&movable_page_list, alloc_migration_target, NULL, (unsigned long)&mtc, MIGRATE_SYNC,
On 2/11/22 18:51, Alistair Popple wrote: ...
@@ -1888,15 +1942,40 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, continue; prev_head = head; /*
* If we get a movable page, since we are going to be pinning
* these entries, try to move them out if possible.
* Device coherent pages are managed by a driver and should not
* be pinned indefinitely as it prevents the driver moving the
* page. So when trying to pin with FOLL_LONGTERM instead try
* migrating page out of device memory. */ if (is_dev_private_or_coherent_page(head)) {
/*
* device private pages will get faulted in during gup
* so it shouldn't be possible to see one here.
*/ WARN_ON_ONCE(is_device_private_page(head));
ret = -EFAULT;
goto unpin_pages;
WARN_ON_ONCE(PageCompound(head));
/*
* migration will fail if the page is pinned, so convert
* the pin on the source page to a normal reference.
*/
if (gup_flags & FOLL_PIN) {
get_page(head);
unpin_user_page(head);
OK...but now gup_flags can no longer be used as a guide for how to release these pages, right? In other words, up until this point, FOLL_PIN meant "call unpin_user_page() in order to release". However, now this page must be released via put_page().
This is the source page (head). We are unpinning it because we can't migrate a pinned page, however we still need a reference on it for migrate_vma hence the get_page followed by unpin. In the non-FOLL_PIN case we already have a reference from gup.
See below...
}
pages[i] = migrate_device_page(head, gup_flags);
migrate_device_page() will return a new page that has been correctly pinned with gup_flags by try_grab_page(). Therefore this page can still be released with unpin_user_page() or put_page() as appropriate for the given gup_flags.
The reference we had on the source page (head) always gets dropped in migrate_vma_finalize().
OK. Good.
The above would be good to have in a comment, right around here, imho. Because we have this marvelous mix of references for migration (get_page()) and other, and it's a bit hard to see that it's all correct without a hint or two.
...
Which unless I've missed something is still the correct thing to do.
This reminds me: out of the many things to monitor, the FOLL_PIN counts in /proc/vmstat are especially helpful, whenever making changes to code that deals with this:
nr_foll_pin_acquired nr_foll_pin_released
...and those should normally be equal to each other when "at rest".
I hope this is/was run, just to be sure?
thanks,
John Hubbard jhubbard@nvidia.com writes:
On 2/11/22 18:51, Alistair Popple wrote:
[…]
See below…
}
pages[i] = migrate_device_page(head, gup_flags);
migrate_device_page() will return a new page that has been correctly pinned with gup_flags by try_grab_page(). Therefore this page can still be released with unpin_user_page() or put_page() as appropriate for the given gup_flags. The reference we had on the source page (head) always gets dropped in migrate_vma_finalize().
OK. Good.
The above would be good to have in a comment, right around here, imho. Because we have this marvelous mix of references for migration (get_page()) and other, and it’s a bit hard to see that it’s all correct without a hint or two.
Ok, will do.
…
Which unless I’ve missed something is still the correct thing to do.
This reminds me: out of the many things to monitor, the FOLL_PIN counts in /proc/vmstat are especially helpful, whenever making changes to code that deals with this:
nr_foll_pin_acquired nr_foll_pin_released
…and those should normally be equal to each other when “at rest”.
I hope this is/was run, just to be sure?
Thanks for the suggestion, these remain equal to each other after running hmm-tests which confirms everything is working as expected.
thanks,
From: Alex Sierra alex.sierra@amd.com
The intention is to test device coherent type pages that have been called through get user pages with PIN_LONGTERM flag set. These pages should get migrated back to normal system memory.
Signed-off-by: Alex Sierra alex.sierra@amd.com Signed-off-by: Alistair Popple apopple@nvidia.com Reviewed-by: Felix Kuehling Felix.Kuehling@amd.com ---
Changes for v2: - Added Felix's Reviewed-by (thanks!)
tools/testing/selftests/vm/Makefile | 2 +- tools/testing/selftests/vm/hmm-tests.c | 81 +++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile index 96714d2..32032c7 100644 --- a/tools/testing/selftests/vm/Makefile +++ b/tools/testing/selftests/vm/Makefile @@ -143,7 +143,7 @@ $(OUTPUT)/mlock-random-test $(OUTPUT)/memfd_secret: LDLIBS += -lcap
$(OUTPUT)/gup_test: ../../../../mm/gup_test.h
-$(OUTPUT)/hmm-tests: local_config.h +$(OUTPUT)/hmm-tests: local_config.h ../../../../mm/gup_test.h
# HMM_EXTRA_LIBS may get set in local_config.mk, or it may be left empty. $(OUTPUT)/hmm-tests: LDLIBS += $(HMM_EXTRA_LIBS) diff --git a/tools/testing/selftests/vm/hmm-tests.c b/tools/testing/selftests/vm/hmm-tests.c index 84ec8c4..11b83a8 100644 --- a/tools/testing/selftests/vm/hmm-tests.c +++ b/tools/testing/selftests/vm/hmm-tests.c @@ -36,6 +36,7 @@ * in the usual include/uapi/... directory. */ #include "../../../../lib/test_hmm_uapi.h" +#include "../../../../mm/gup_test.h"
struct hmm_buffer { void *ptr; @@ -60,6 +61,8 @@ enum { #define NTIMES 10
#define ALIGN(x, a) (((x) + (a - 1)) & (~((a) - 1))) +/* Just the flags we need, copied from mm.h: */ +#define FOLL_WRITE 0x01 /* check pte is writable */
FIXTURE(hmm) { @@ -1766,4 +1769,82 @@ TEST_F(hmm, exclusive_cow) hmm_buffer_free(buffer); }
+/* + * Test get user device pages through gup_test. Setting PIN_LONGTERM flag. + * This should trigger a migration back to system memory for both, private + * and coherent type pages. + * This test makes use of gup_test module. Make sure GUP_TEST_CONFIG is added + * to your configuration before you run it. + */ +TEST_F(hmm, hmm_gup_test) +{ + struct hmm_buffer *buffer; + struct gup_test gup; + int gup_fd; + unsigned long npages; + unsigned long size; + unsigned long i; + int *ptr; + int ret; + unsigned char *m; + + gup_fd = open("/sys/kernel/debug/gup_test", O_RDWR); + if (gup_fd == -1) + SKIP(return, "Skipping test, could not find gup_test driver"); + + npages = 4; + ASSERT_NE(npages, 0); + size = npages << self->page_shift; + + buffer = malloc(sizeof(*buffer)); + ASSERT_NE(buffer, NULL); + + buffer->fd = -1; + buffer->size = size; + buffer->mirror = malloc(size); + ASSERT_NE(buffer->mirror, NULL); + + buffer->ptr = mmap(NULL, size, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, + buffer->fd, 0); + ASSERT_NE(buffer->ptr, MAP_FAILED); + + /* Initialize buffer in system memory. */ + for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i) + ptr[i] = i; + + /* Migrate memory to device. */ + ret = hmm_migrate_sys_to_dev(self->fd, buffer, npages); + ASSERT_EQ(ret, 0); + ASSERT_EQ(buffer->cpages, npages); + /* Check what the device read. */ + for (i = 0, ptr = buffer->mirror; i < size / sizeof(*ptr); ++i) + ASSERT_EQ(ptr[i], i); + + gup.nr_pages_per_call = npages; + gup.addr = (unsigned long)buffer->ptr; + gup.gup_flags = FOLL_WRITE; + gup.size = size; + /* + * Calling gup_test ioctl. It will try to PIN_LONGTERM these device pages + * causing a migration back to system memory for both, private and coherent + * type pages. + */ + if (ioctl(gup_fd, PIN_LONGTERM_BENCHMARK, &gup)) { + perror("ioctl on PIN_LONGTERM_BENCHMARK\n"); + goto out_test; + } + + /* Take snapshot to make sure pages have been migrated to sys memory */ + ret = hmm_dmirror_cmd(self->fd, HMM_DMIRROR_SNAPSHOT, buffer, npages); + ASSERT_EQ(ret, 0); + ASSERT_EQ(buffer->cpages, npages); + m = buffer->mirror; + for (i = 0; i < npages; i++) + ASSERT_EQ(m[i], HMM_DMIRROR_PROT_WRITE); +out_test: + close(gup_fd); + hmm_buffer_free(buffer); +} TEST_HARNESS_MAIN
dri-devel@lists.freedesktop.org