]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
mm/gup: migrate device coherent pages when pinning instead of failing
authorAlistair Popple <apopple@nvidia.com>
Tue, 31 May 2022 20:00:33 +0000 (15:00 -0500)
committerLiam R. Howlett <Liam.Howlett@oracle.com>
Wed, 20 Jul 2022 00:15:01 +0000 (20:15 -0400)
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.

[hch@lst.de: rebased to the split device memory checks, moved migrate_device_page to migrate_device.c]
Link: https://lkml.kernel.org/r/20220531200041.24904-6-alex.sierra@amd.com
Signed-off-by: Alistair Popple <apopple@nvidia.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
Acked-by: Felix Kuehling <Felix.Kuehling@amd.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Jason Gunthorpe <jgg@nvidia.com>
Cc: Jerome Glisse <jglisse@redhat.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Ralph Campbell <rcampbell@nvidia.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/gup.c
mm/internal.h
mm/migrate_device.c

index 67cc836eb1b52c55b7f33408459d571538e3b265..18f6953a74e3f92c9fada845c4280bbfb664c79d 100644 (file)
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -1928,9 +1928,43 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
                        continue;
                prev_folio = folio;
 
-               if (folio_is_pinnable(folio))
+               /*
+                * Device private pages will get faulted in during gup so it
+                * shouldn't be possible to see one here.
+                */
+               if (WARN_ON_ONCE(folio_is_device_private(folio))) {
+                       ret = -EFAULT;
+                       goto unpin_pages;
+               }
+
+               /*
+                * 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
+                * to migrate the page out of device memory.
+                */
+               if (folio_is_device_coherent(folio)) {
+                       WARN_ON_ONCE(PageCompound(&folio->page));
+
+                       /*
+                        * 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(&folio->page);
+                               unpin_user_page(&folio->page);
+                       }
+
+                       pages[i] = migrate_device_page(&folio->page, gup_flags);
+                       if (!pages[i]) {
+                               ret = -EBUSY;
+                               goto unpin_pages;
+                       }
                        continue;
+               }
 
+               if (folio_is_pinnable(folio))
+                       continue;
                /*
                 * Try to move out any movable page before pinning the range.
                 */
@@ -1966,10 +2000,13 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
        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;
+
+               if (gup_flags & FOLL_PIN)
+                       unpin_user_page(pages[i]);
+               else
                        put_page(pages[i]);
        }
 
index eba79d1d3b06a24c9e5b20c8a74ffc32c91cdd98..6e14749ad1e531a7c59c9e67ff47910cf43ced6f 100644 (file)
@@ -851,6 +851,7 @@ int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
                      unsigned long addr, int page_nid, int *flags);
 
 void free_zone_device_page(struct page *page);
+struct page *migrate_device_page(struct page *page, unsigned int gup_flags);
 
 /*
  * mm/gup.c
index cf9668376c5a8f08e89db530c77dd2517e0b11cb..5decd26dd551bedd1b85e3849e8a95f5f1f0553a 100644 (file)
@@ -794,3 +794,56 @@ void migrate_vma_finalize(struct migrate_vma *migrate)
        }
 }
 EXPORT_SYMBOL(migrate_vma_finalize);
+
+/*
+ * Migrate a device coherent page back to normal memory.  The caller should have
+ * a reference on page which will be copied to the new page if migration is
+ * successful or dropped on failure.
+ */
+struct page *migrate_device_page(struct page *page, unsigned int gup_flags)
+{
+       unsigned long src_pfn, dst_pfn = 0;
+       struct migrate_vma args;
+       struct page *dpage;
+
+       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;
+}