History

mm/migrate_device.c: add migrate_device_range()

Device drivers can use the migrate_vma family of functions to migrate existing private anonymous mappings to device private pages. These pages are backed by memory on the device with drivers being responsible for copying data to and from device memory. Device private pages are freed via the pgmap->page_free() callback when they are unmapped and their refcount drops to zero. Alternatively they may be freed indirectly via migration back to CPU memory in response to a pgmap->migrate_to_ram() callback called whenever the CPU accesses an address mapped to a device private page. In other words drivers cannot control the lifetime of data allocated on the devices and must wait until these pages are freed from userspace. This causes issues when memory needs to reclaimed on the device, either because the device is going away due to a ->release() callback or because another user needs to use the memory. Drivers could use the existing migrate_vma functions to migrate data off the device. However this would require them to track the mappings of each page which is both complicated and not always possible. Instead drivers need to be able to migrate device pages directly so they can free up device memory. To allow that this patch introduces the migrate_device family of functions which are functionally similar to migrate_vma but which skips the initial lookup based on mapping.

Alistair Popple committed 4 years ago

Commit e7784061 parent 241f6882 files · +89 −7Browse files

Unified · Split

2 files
  1. include/linux/migrate.h +7 −0
  2. mm/migrate_device.c +82 −7
include/linux/migrate.h+7−0
@@ -210,6 +210,13 @@ struct migrate_vma { · 209 unmodified lines
210210 int migrate_vma_setup(struct migrate_vma *args);
211211 void migrate_vma_pages(struct migrate_vma *migrate);
212212 void migrate_vma_finalize(struct migrate_vma *migrate);
213+int migrate_device_range(unsigned long *src_pfns, unsigned long start,
214+ unsigned long npages);
215+void migrate_device_pages(unsigned long *src_pfns, unsigned long *dst_pfns,
216+ unsigned long npages);
217+void migrate_device_finalize(unsigned long *src_pfns,
218+ unsigned long *dst_pfns, unsigned long npages);
219+
213220 #endif /* CONFIG_MIGRATION */
214221
215222 #endif /* _LINUX_MIGRATE_H */
mm/migrate_device.c+82−7
@@ -693,7 +693,7 @@ abort: · 692 unmodified lines
693693 *src &= ~MIGRATE_PFN_MIGRATE;
694694 }
695695
696−static void migrate_device_pages(unsigned long *src_pfns,
696+static void __migrate_device_pages(unsigned long *src_pfns,
697697 unsigned long *dst_pfns, unsigned long npages,
698698 struct migrate_vma *migrate)
699699 {
@@ -715,6 +715,9 @@ static void migrate_device_pages(unsigned long *src_pfns, · 15 unmodified lines
715715 if (!page) {
716716 unsigned long addr;
717717
718+ if (!(src_pfns[i] & MIGRATE_PFN_MIGRATE))
719+ continue;
720+
718721 /*
719722 * The only time there is no vma is when called from
720723 * migrate_device_coherent_page(). However this isn't
@@ -722,8 +725,6 @@ static void migrate_device_pages(unsigned long *src_pfns, · 1 unmodified line
722725 */
723726 VM_BUG_ON(!migrate);
724727 addr = migrate->start + i*PAGE_SIZE;
725− if (!(src_pfns[i] & MIGRATE_PFN_MIGRATE))
726− continue;
727728 if (!notified) {
728729 notified = true;
729730
@@ -778,6 +779,22 @@ static void migrate_device_pages(unsigned long *src_pfns, · 48 unmodified lines
778779 mmu_notifier_invalidate_range_only_end(&range);
779780 }
780781
782+/**
783+ * migrate_device_pages() - migrate meta-data from src page to dst page
784+ * @src_pfns: src_pfns returned from migrate_device_range()
785+ * @dst_pfns: array of pfns allocated by the driver to migrate memory to
786+ * @npages: number of pages in the range
787+ *
788+ * Equivalent to migrate_vma_pages(). This is called to migrate struct page
789+ * meta-data from source struct page to destination.
790+ */
791+void migrate_device_pages(unsigned long *src_pfns, unsigned long *dst_pfns,
792+ unsigned long npages)
793+{
794+ __migrate_device_pages(src_pfns, dst_pfns, npages, NULL);
795+}
796+EXPORT_SYMBOL(migrate_device_pages);
797+
781798 /**
782799 * migrate_vma_pages() - migrate meta-data from src page to dst page
783800 * @migrate: migrate struct containing all migration information
@@ -788,12 +805,22 @@ static void migrate_device_pages(unsigned long *src_pfns, · 4 unmodified lines
788805 */
789806 void migrate_vma_pages(struct migrate_vma *migrate)
790807 {
791− migrate_device_pages(migrate->src, migrate->dst, migrate->npages, migrate);
808+ __migrate_device_pages(migrate->src, migrate->dst, migrate->npages, migrate);
792809 }
793810 EXPORT_SYMBOL(migrate_vma_pages);
794811
795−static void migrate_device_finalize(unsigned long *src_pfns,
796− unsigned long *dst_pfns, unsigned long npages)
812+/*
813+ * migrate_device_finalize() - complete page migration
814+ * @src_pfns: src_pfns returned from migrate_device_range()
815+ * @dst_pfns: array of pfns allocated by the driver to migrate memory to
816+ * @npages: number of pages in the range
817+ *
818+ * Completes migration of the page by removing special migration entries.
819+ * Drivers must ensure copying of page data is complete and visible to the CPU
820+ * before calling this.
821+ */
822+void migrate_device_finalize(unsigned long *src_pfns,
823+ unsigned long *dst_pfns, unsigned long npages)
797824 {
798825 unsigned long i;
799826
@@ -837,6 +864,7 @@ static void migrate_device_finalize(unsigned long *src_pfns, · 37 unmodified lines
837864 }
838865 }
839866 }
867+EXPORT_SYMBOL(migrate_device_finalize);
840868
841869 /**
842870 * migrate_vma_finalize() - restore CPU page table entry
@@ -855,6 +883,53 @@ void migrate_vma_finalize(struct migrate_vma *migrate) · 12 unmodified lines
855883 }
856884 EXPORT_SYMBOL(migrate_vma_finalize);
857885
886+/**
887+ * migrate_device_range() - migrate device private pfns to normal memory.
888+ * @src_pfns: array large enough to hold migrating source device private pfns.
889+ * @start: starting pfn in the range to migrate.
890+ * @npages: number of pages to migrate.
891+ *
892+ * migrate_vma_setup() is similar in concept to migrate_vma_setup() except that
893+ * instead of looking up pages based on virtual address mappings a range of
894+ * device pfns that should be migrated to system memory is used instead.
895+ *
896+ * This is useful when a driver needs to free device memory but doesn't know the
897+ * virtual mappings of every page that may be in device memory. For example this
898+ * is often the case when a driver is being unloaded or unbound from a device.
899+ *
900+ * Like migrate_vma_setup() this function will take a reference and lock any
901+ * migrating pages that aren't free before unmapping them. Drivers may then
902+ * allocate destination pages and start copying data from the device to CPU
903+ * memory before calling migrate_device_pages().
904+ */
905+int migrate_device_range(unsigned long *src_pfns, unsigned long start,
906+ unsigned long npages)
907+{
908+ unsigned long i, pfn;
909+
910+ for (pfn = start, i = 0; i < npages; pfn++, i++) {
911+ struct page *page = pfn_to_page(pfn);
912+
913+ if (!get_page_unless_zero(page)) {
914+ src_pfns[i] = 0;
915+ continue;
916+ }
917+
918+ if (!trylock_page(page)) {
919+ src_pfns[i] = 0;
920+ put_page(page);
921+ continue;
922+ }
923+
924+ src_pfns[i] = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
925+ }
926+
927+ migrate_device_unmap(src_pfns, npages, NULL);
928+
929+ return 0;
930+}
931+EXPORT_SYMBOL(migrate_device_range);
932+
858933 /*
859934 * Migrate a device coherent page back to normal memory. The caller should have
860935 * a reference on page which will be copied to the new page if migration is
@@ -885,7 +960,7 @@ int migrate_device_coherent_page(struct page *page) · 24 unmodified lines
885960 dst_pfn = migrate_pfn(page_to_pfn(dpage));
886961 }
887962
888− migrate_device_pages(&src_pfn, &dst_pfn, 1, NULL);
963+ migrate_device_pages(&src_pfn, &dst_pfn, 1);
889964 if (src_pfn & MIGRATE_PFN_MIGRATE)
890965 copy_highpage(dpage, page);
891966 migrate_device_finalize(&src_pfn, &dst_pfn, 1);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t