Add support for SVM atomics in Nouveau
Subsystems (11)
Alistair Popple
reviewercover Add support for SVM atomics in Nouveau
This series adds support to Nouveau for atomic memory operations on OpenCL shared virtual memory (SVM). This is achieved using the atomic PTE bits on the GPU to only permit atomic operations to system memory when a page is not mapped in userspace on the CPU.
This is implemented by adding a mode to migrate_vma_pages() which unmaps and isolates existing pages from the CPU and pins them. The original userspace page table entries are migrated to point to device private pages allocated by the driver. This allows the driver to enable GPU atomic access to the page as it will receive a callback when CPU userspace needs to access it.
In response to this callback the driver revokes the atomic access permission from the GPU and migrates entries to point back to the original page. The original page is unpinned as part of the migration operation which also returns it to the LRU.
Patch 3 contains the bulk of the memory management changes to implement unmap and pin.
Patches 6-9 extend Nouveau to use the new mode to allow system wide atomics for OpenCL SVM to be implemented on Nouveau.
This has been tested using the latest upstream Mesa userspace with a simple OpenCL test program which checks the results of atomic GPU operations on a buffer whilst also writing to the same buffer from the CPU.
Problems yet to be addressed:
Recent changes to pin_user_pages() prevent the creation of pinned pages in ZONE_MOVABLE. This series allows pinned pages to be created in ZONE_MOVABLE as attempts to migrate may fail which would be fatal to userspace.
In this case migration of the pinned page is unnecessary as the page can be unpinned at anytime by having the driver revoke atomic permission as it does for the migrate_to_ram() callback. However a method of calling this when memory needs to be moved has yet to be resolved so any discussion is welcome.
Alistair Popple (9):
mm/migrate.c: Always allow device private pages to migrate mm/migrate.c: Allow pfn flags to be passed to migrate_vma_setup() mm/migrate: Add a unmap and pin migration mode
Documentation: Add unmap and pin to HMM
hmm-tests: Add test for unmap and pin
nouveau/dmem: Only map migrating pages
nouveau/svm: Refactor nouveau_range_fault nouveau/dmem: Add support for multiple page types nouveau/svm: Implement atomic SVM access
Documentation/vm/hmm.rst | 22 +- arch/powerpc/kvm/book3s_hv_uvmem.c | 4 +- drivers/gpu/drm/nouveau/include/nvif/if000c.h | 1 + drivers/gpu/drm/nouveau/nouveau_dmem.c | 190 +++++++++++++++--- drivers/gpu/drm/nouveau/nouveau_dmem.h | 9 + drivers/gpu/drm/nouveau/nouveau_svm.c | 148 +++++++++++--- drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.h | 1 + .../drm/nouveau/nvkm/subdev/mmu/vmmgp100.c | 6 + include/linux/migrate.h | 2 + include/linux/migrate_mode.h | 1 + lib/test_hmm.c | 109 ++++++++-- lib/test_hmm_uapi.h | 1 + mm/migrate.c | 82 +++++--- tools/testing/selftests/vm/hmm-tests.c | 49 +++++ 14 files changed, 524 insertions(+), 101 deletions(-)
Signature · 1 line
2.20.1
Alistair Popple
reviewerPATCH 1/9 mm/migrate.c: Always allow device private pages to migrate
Device private pages are used to represent device memory that is not directly accessible from the CPU. Extra references to a device private page are only used to ensure the struct page itself remains valid whilst waiting for migration entries. Therefore extra references should not prevent device private page migration as this can lead to failures to migrate pages back to the CPU which are fatal to the user process.
--- mm/migrate.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
mm/migrate.c +7 -2
@@ -405,8 +405,13 @@ int migrate_page_move_mapping(struct address_space *mapping,int nr =thp_nr_pages (page);if (!mapping) { -/* Anonymous page without mapping */ -if (page_count (page) != expected_count) +/* +* Anonymous page without mapping. Device private pages should +* never have extra references except during migration, but it +* is safe to ignore these. +*/ +if (!is_device_private_page (page) && +page_count (page) != expected_count)return -EAGAIN ;/* No turning back from here */
Signature · 1 line
2.20.1
Re: [PATCH 1/9] mm/migrate.c: Always allow device private pages to migrate
Quoted 6 lines · Alistair Popple
Device private pages are used to represent device memory that is not directly accessible from the CPU. Extra references to a device private page are only used to ensure the struct page itself remains valid whilst waiting for migration entries. Therefore extra references should not prevent device private page migration as this can lead to failures to migrate pages back to the CPU which are fatal to the user process.
This should identify the extra references in expected_count, just disabling this protection seems unsafe, ZONE_DEVICE is not so special that the refcount means nothing
Is this a side effect of the extra refcounts that Ralph was trying to get rid of? I'd rather see that work finished :)
Jason
Alistair Popple
reviewerRe: [PATCH 1/9] mm/migrate.c: Always allow device private pages to migrate
Quoted 11 lines · AEDT Jason Gunthorpe
Quoted 6 lines · Alistair Popple
Device private pages are used to represent device memory that is not directly accessible from the CPU. Extra references to a device private page are only used to ensure the struct page itself remains valid whilst waiting for migration entries. Therefore extra references should not prevent device private page migration as this can lead to failures to migrate pages back to the CPU which are fatal to the user process.
This should identify the extra references in
expected_count, just disabling this protection seems unsafe,ZONE_DEVICEis not so special that the refcount means nothing
This is similar to what migarte_vma_check_page() does now. The issue is that a migration wait takes a reference on the device private page so you can end up with one thread stuck waiting for migration whilst the other can't migrate due to the extra refcount.
Given device private pages can't undergo GUP and that it's not possible to differentiate the migration wait refcount from any other refcount we assume any possible extra reference must be from migration wait.
Quoted 2 lines
Is this a side effect of the extra refcounts that Ralph was trying to get rid of? I'd rather see that work finished :)
I'd like to see that finished too but I don't think it would help here as this is not a side effect of that.
- Alistair
Quoted 1 line
Jason
Re: [PATCH 1/9] mm/migrate.c: Always allow device private pages to migrate
Quoted 13 lines · Alistair Popple
Quoted 11 lines · AEDT Jason Gunthorpe
Quoted 6 lines · Alistair Popple
Device private pages are used to represent device memory that is not directly accessible from the CPU. Extra references to a device private page are only used to ensure the struct page itself remains valid whilst waiting for migration entries. Therefore extra references should not prevent device private page migration as this can lead to failures to migrate pages back to the CPU which are fatal to the user process.
This should identify the extra references in
expected_count, just disabling this protection seems unsafe,ZONE_DEVICEis not so special that the refcount means nothing
Quoted 8 lines
This is similar to what
migarte_vma_check_page()does now. The issue is that a migration wait takes a reference on the device private page so you can end up with one thread stuck waiting for migration whilst the other can't migrate due to the extra refcount.Given device private pages can't undergo GUP and that it's not possible to differentiate the migration wait refcount from any other refcount we assume any possible extra reference must be from migration wait.
GUP is not the only thing that elevates the refcount, I think this is an unsafe assumption
Why is migration holding an extra refcount anyhow?
Jason
Alistair Popple
reviewerPATCH 2/9 mm/migrate.c: Allow pfn flags to be passed to migrate_vma_setup()
In reply to Alistair PoppleCurrently migrate_vma_setup() zeros both src and dst pfn arrays. This means it is not possible to pass per-pfn flags to migrate_vma_setup(). A future patch introduces per-pfn flags for migrate_vma_setup(), so ensure existing callers will not be affected by having the caller zero both src and dst pfn arrays.
--- arch/powerpc/kvm/book3s_hv_uvmem.c | 4 ++-- lib/test_hmm.c | 6 ++++-- mm/migrate.c | 1 - 3 files changed, 6 insertions(+), 5 deletions(-)
arch/powerpc/kvm/book3s_hv_uvmem.c +2 -2
@@ -506,7 +506,7 @@ static int __kvmppc_svm_page_out(struct vm_area_struct *vma, unsigned long end, unsigned long page_shift, struct kvm *kvm, unsigned long gpa) { - unsigned long src_pfn, dst_pfn = 0; + unsigned long src_pfn = 0, dst_pfn = 0;struct migrate_vma mig;struct page *dpage, *spage;struct kvmppc_uvmem_page_pvt *pvt;
@@ -732,7 +732,7 @@ static int kvmppc_svm_page_in(struct vm_area_struct *vma, unsigned long page_shift, bool pagein) { -unsigned long src_pfn, dst_pfn =0 ; +unsigned long src_pfn =0 , dst_pfn =0 ;struct migrate_vma mig;struct page *spage;unsigned long pfn;
lib/test_hmm.c +4 -2
@@ -696,6 +696,8 @@ static int dmirror_migrate(struct dmirror *dmirror,if (next > vma->vm_end) next = vma->vm_end; +memset (src_pfns,0 ,ARRAY_SIZE (src_pfns)); +memset (dst_pfns,0 ,ARRAY_SIZE (dst_pfns)); args.vma = vma; args.src = src_pfns; args.dst = dst_pfns;
@@ -1025,8 +1027,8 @@ static vm_fault_t dmirror_devmem_fault_alloc_and_copy(struct migrate_vma *args,static vm_fault_t dmirror_devmem_fault (struct vm_fault *vmf) {struct migrate_vma args; -unsigned long src_pfns; -unsigned long dst_pfns; +unsigned long src_pfns =0 ; +unsigned long dst_pfns =0 ;struct page *rpage;struct dmirror *dmirror;vm_fault_t ret;
mm/migrate.c +0 -1
@@ -2874,7 +2874,6 @@ int migrate_vma_setup(struct migrate_vma *args)if (!args->src || !args->dst)return -EINVAL ; -memset (args->src,0 ,sizeof (*args->src) * nr_pages); args->cpages =0 ; args->npages =0 ;
Signature · 1 line
2.20.1
Alistair Popple
reviewerPATCH 3/9 mm/migrate: Add a unmap and pin migration mode
In reply to Alistair PoppleSome drivers need to ensure that a device has access to a particular user page whilst preventing userspace access to that page. For example this is required to allow a driver to implement atomic access to a page when the device hardware does not support atomic access to system memory.
This could be implemented by migrating the data to the device, however this is not always optimal and may fail in some circumstances. In these cases it is advantageous to remap the page for device access without actually migrating the data.
To allow this kind of access introduce an unmap and pin flag called MIGRATE_PFN_PIN/UNPIN for migration pfns. This will cause the original page to be remapped to the provided device private page as normal, but instead of returning or freeing the original CPU page it will pin it and leave it isolated from the LRU.
This ensures the page remains pinned so that a device may access it exclusively. Any userspace CPU accesses will fault and trigger the normal device private migrate_to_ram() callback which must migrate the mapping back to the original page, after which the device will no longer have exclusive access to the page.
As the original page does not get freed it is safe to allow the unmap and pin operation to proceed in cases where there are extra page references present.
--- include/linux/migrate.h | 2 + include/linux/migrate_mode.h | 1 + mm/migrate.c | 74 +++++++++++++++++++++++++----------- 3 files changed, 54 insertions(+), 23 deletions(-)
include/linux/migrate.h +2 -0
@@ -144,6 +144,8 @@ static inline int migrate_misplaced_transhuge_page(struct mm_struct *mm,#define MIGRATE_PFN_MIGRATE (1UL << 1)#define MIGRATE_PFN_LOCKED (1UL << 2)#define MIGRATE_PFN_WRITE (1UL << 3) +#define MIGRATE_PFN_PIN (1UL << 4) +#define MIGRATE_PFN_UNPIN (1UL << 4)#define MIGRATE_PFN_SHIFT 6static inline struct page *migrate_pfn_to_page (unsigned long mpfn)
include/linux/migrate_mode.h +1 -0
@@ -17,6 +17,7 @@ enum migrate_mode {MIGRATE_SYNC_LIGHT ,MIGRATE_SYNC ,MIGRATE_SYNC_NO_COPY , + MIGRATE_REFERENCED, };#endif /* MIGRATE_MODE_H_INCLUDED */
mm/migrate.c +51 -23
160 lines not on this page · raw message
Signature · 1 line
2.20.1
Alistair Popple
reviewerPATCH 4/9 Documentation: Add unmap and pin to HMM
In reply to Alistair PoppleUpdate the HMM documentation to include information on the unmap and pin operation.
--- Documentation/vm/hmm.rst | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-)
Documentation/vm/hmm.rst +19 -3
@@ -346,7 +346,15 @@ between device driver specific code and shared common code: from the LRU (if system memory since device private pages are not on the LRU), unmapped from the process, and a special migration PTE is inserted in place of the original PTE. - migrate_vma_setup() also clears the ``args->dst`` array. + + A device driver may also initialise ``src`` entries with the + ``MIGRATE_PFN_PIN`` flag. This allows the device driver to unmap and pin + the existing system page in place whilst migrating page metadata to a + device private page. This leaves the page isolated from the LRU and gives + the device exclusive access to the page data without the need to migrate + data as any CPU access will trigger a fault. The device driver needs to + keep track of the ``src`` page as it effectively becomes the owner of + the page and needs to pass it in when remapping and unpinning the page. 3. The device driver allocates destination pages and copies source pages to destination pages.
@@ -357,8 +365,8 @@ between device driver specific code and shared common code: array for that page. The driver then allocates either a device private struct page or a - system memory page, locks the page with ``lock_page()``, and fills in the - ``dst`` array entry with:: + system memory page, locks the page with ``lock_page()``, and fills in + the ``dst`` array entry with:: dst[i] = migrate_pfn(page_to_pfn(dpage)) | MIGRATE_PFN_LOCKED;
@@ -373,6 +381,14 @@ between device driver specific code and shared common code: destination or clear the destination device private memory if the pointer is ``NULL`` meaning the source page was not populated in system memory. + Alternatively a driver that is remapping and unpinning a source page + obtained from a ``MIGRATE_PFN_PIN`` operation should lock the original + source page and pass it in along with the ``MIGRATE_PFN_UNPIN`` flag + without any need to copy data:: + + dst[i] = migrate_pfn(page_to_pfn(spage)) | MIGRATE_PFN_LOCKED + | MIGRATE_PFN_UNPIN; + 4. ``migrate_vma_pages()`` This step is where the migration is actually "committed".
Signature · 1 line
2.20.1
Alistair Popple
reviewerPATCH 5/9 hmm-tests: Add test for unmap and pin
In reply to Alistair PoppleAdds a basic test of the HMM unmap and pin operation.
--- lib/test_hmm.c | 107 +++++++++++++++++++++---- lib/test_hmm_uapi.h | 1 + tools/testing/selftests/vm/hmm-tests.c | 49 +++++++++++ 3 files changed, 140 insertions(+), 17 deletions(-)
lib/test_hmm.c +90 -17
@@ -46,6 +46,7 @@ struct dmirror_bounce {unsigned long cpages; }; +#define DPT_XA_TAG_ATOMIC 1UL#define DPT_XA_TAG_WRITE 3UL/*
@@ -83,6 +84,7 @@ struct dmirror_device {struct cdev cdevice;struct hmm_devmem *devmem; +unsigned int devmem_faults;unsigned int devmem_capacity;unsigned int devmem_count;struct dmirror_chunk **devmem_chunks;
@@ -203,8 +205,18 @@ static void dmirror_do_update(struct dmirror *dmirror, unsigned long start,* Therefore, it is OK to just clear the entry. */ xa_for_each_range (&dmirror->pt, pfn, entry, start >>PAGE_SHIFT , - end >>PAGE_SHIFT ) + end >>PAGE_SHIFT ) { +/* +* Typically this would be done in devmap free page, but as +* we're using the XArray to store the reference to the original +* page do it here as it doesn't matter if clean up of the +* pinned page is delayed. +*/ +if (xa_pointer_tag (entry) ==DPT_XA_TAG_ATOMIC ) +unpin_user_page (xa_untag_pointer (entry)); +xa_erase (&dmirror->pt, pfn); + } } static bool dmirror_interval_invalidate(struct mmu_interval_notifier *mni,
@@ -571,7 +583,8 @@ static struct page *dmirror_devmem_alloc_page(struct dmirror_device *mdevice) }static void dmirror_migrate_alloc_and_copy (struct migrate_vma *args, -struct dmirror *dmirror) +struct dmirror *dmirror, +int allow_ref) {struct dmirror_device *mdevice = dmirror->mdevice;const unsigned long *src = args->src;
@@ -598,9 +611,17 @@ static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args,continue ; rpage = dpage->zone_device_data; -if (spage) +if (spage && !(*src &MIGRATE_PFN_PIN ))copy_highpage (rpage, spage);else +/* +* In the MIGRATE_PFN_PIN case we don't really +* need rpage at all because the existing page is +* staying in place and will be mapped. However we need +* somewhere to store dmirror and that place is +* rpage->zone_device_data so we keep it for +* simplicity. +*/ clear_highpage (rpage);/*
@@ -620,7 +641,8 @@ static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args, }static int dmirror_migrate_finalize_and_map (struct migrate_vma *args, -struct dmirror *dmirror) +struct dmirror *dmirror, +int allow_ref) {unsigned long start = args->start;unsigned long end = args->end;
@@ -647,8 +669,14 @@ static int dmirror_migrate_finalize_and_map(struct migrate_vma *args,* Store the page that holds the data so the page table * doesn't have to deal with ZONE_DEVICE private pages. */ - entry = dpage->zone_device_data; -if (*dst &MIGRATE_PFN_WRITE ) +if (*src &MIGRATE_PFN_PIN ) + entry =migrate_pfn_to_page (*src); +else + entry = dpage->zone_device_data; + +if (*src &MIGRATE_PFN_PIN ) + entry =xa_tag_pointer (entry,DPT_XA_TAG_ATOMIC ); +else if (*dst &MIGRATE_PFN_WRITE ) entry =xa_tag_pointer (entry,DPT_XA_TAG_WRITE ); entry =xa_store (&dmirror->pt, pfn, entry,GFP_ATOMIC );if (xa_is_err (entry)) {
@@ -662,7 +690,8 @@ static int dmirror_migrate_finalize_and_map(struct migrate_vma *args, }static int dmirror_migrate (struct dmirror *dmirror, -struct hmm_dmirror_cmd *cmd) +struct hmm_dmirror_cmd *cmd, +int allow_ref) {unsigned long start, end, addr;unsigned long size = cmd->npages <<PAGE_SHIFT ;
@@ -673,7 +702,7 @@ static int dmirror_migrate(struct dmirror *dmirror,struct dmirror_bounce bounce;struct migrate_vma args;unsigned long next; -int ret; +int i, ret; start = cmd->addr; end = start + size;
@@ -696,8 +725,13 @@ static int dmirror_migrate(struct dmirror *dmirror,if (next > vma->vm_end) next = vma->vm_end; -memset (src_pfns,0 ,ARRAY_SIZE (src_pfns)); -memset (dst_pfns,0 ,ARRAY_SIZE (dst_pfns)); +if (allow_ref) +for (i =0 ; i <64 ; ++i) + src_pfns[i] =MIGRATE_PFN_PIN ; +else +memset (src_pfns,0 ,sizeof (src_pfns)); +memset (dst_pfns,0 ,sizeof (dst_pfns)); + args.vma = vma; args.src = src_pfns; args.dst = dst_pfns;
@@ -709,9 +743,9 @@ static int dmirror_migrate(struct dmirror *dmirror,if (ret) goto out; -dmirror_migrate_alloc_and_copy (&args, dmirror); +dmirror_migrate_alloc_and_copy (&args, dmirror, allow_ref);migrate_vma_pages (&args); -dmirror_migrate_finalize_and_map (&args, dmirror); +dmirror_migrate_finalize_and_map (&args, dmirror, allow_ref);migrate_vma_finalize (&args); }mmap_read_unlock (mm);
@@ -739,6 +773,28 @@ static int dmirror_migrate(struct dmirror *dmirror,return ret; } +static int dmirror_migrate_pin (struct dmirror *dmirror, +struct hmm_dmirror_cmd *cmd) +{ +void *tmp; +int nr_pages = cmd->npages; +int ret; + + ret =dmirror_migrate (dmirror, cmd, true); + + tmp =kmalloc (nr_pages <<PAGE_SHIFT ,GFP_KERNEL ); +if (!tmp) +return -ENOMEM ; + +/* Make sure user access faults */ + dmirror->mdevice->devmem_faults =0 ; +if (copy_from_user (tmp,u64_to_user_ptr (cmd->addr), nr_pages <<PAGE_SHIFT )) + ret = -EFAULT ; + cmd->faults = dmirror->mdevice->devmem_faults; + +return ret; +} +static void dmirror_mkentry (struct dmirror *dmirror,struct hmm_range *range,unsigned char *perm,unsigned long entry) {
@@ -948,7 +1004,11 @@ static long dmirror_fops_unlocked_ioctl(struct file *filp,break ;case HMM_DMIRROR_MIGRATE : - ret =dmirror_migrate (dmirror, &cmd); + ret =dmirror_migrate (dmirror, &cmd, false); +break ; + +case HMM_DMIRROR_MIGRATE_PIN : + ret =dmirror_migrate_pin (dmirror, &cmd);break ;case HMM_DMIRROR_SNAPSHOT :
@@ -1004,20 +1064,31 @@ static vm_fault_t dmirror_devmem_fault_alloc_and_copy(struct migrate_vma *args,for (addr = start; addr < end; addr +=PAGE_SIZE , src++, dst++) {struct page *dpage, *spage; +void *entry; spage =migrate_pfn_to_page (*src);if (!spage || !(*src &MIGRATE_PFN_MIGRATE ))continue ; - spage = spage->zone_device_data; - dpage =alloc_page_vma (GFP_HIGHUSER_MOVABLE , args->vma, addr); + entry =xa_load (&dmirror->pt, addr >>PAGE_SHIFT ); +if (entry &&xa_pointer_tag (entry) ==DPT_XA_TAG_ATOMIC ) { + spage =NULL ; + dpage =xa_untag_pointer (entry); + *dst =migrate_pfn (page_to_pfn (dpage)) | +MIGRATE_PFN_LOCKED |MIGRATE_PFN_UNPIN ; + }else { + spage = spage->zone_device_data; + dpage =alloc_page_vma (GFP_HIGHUSER_MOVABLE , args->vma, addr); + *dst =migrate_pfn (page_to_pfn (dpage)) |MIGRATE_PFN_LOCKED ; + } +if (!dpage)continue ;lock_page (dpage);xa_erase (&dmirror->pt, addr >>PAGE_SHIFT ); -copy_highpage (dpage, spage); - *dst =migrate_pfn (page_to_pfn (dpage)) |MIGRATE_PFN_LOCKED ; +if (spage) +copy_highpage (dpage, spage);if (*src &MIGRATE_PFN_WRITE ) *dst |=MIGRATE_PFN_WRITE ; }
@@ -1041,6 +1112,8 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf) rpage = vmf->page->zone_device_data; dmirror = rpage->zone_device_data; + dmirror->mdevice->devmem_faults++; +/* FIXME demonstrate how we can adjust migrate range */ args.vma = vmf->vma; args.start = vmf->address;
lib/test_hmm_uapi.h +1 -0
@@ -33,6 +33,7 @@ struct hmm_dmirror_cmd {#define HMM_DMIRROR_WRITE _IOWR('H', 0x01, struct hmm_dmirror_cmd)#define HMM_DMIRROR_MIGRATE _IOWR('H', 0x02, struct hmm_dmirror_cmd)#define HMM_DMIRROR_SNAPSHOT _IOWR('H', 0x03, struct hmm_dmirror_cmd) +#define HMM_DMIRROR_MIGRATE_PIN _IOWR('H', 0x04, struct hmm_dmirror_cmd)/* * Values returned in hmm_dmirror_cmd.ptr for HMM_DMIRROR_SNAPSHOT.
tools/testing/selftests/vm/hmm-tests.c +49 -0
55 lines not on this page · raw message
Signature · 1 line
2.20.1
Alistair Popple
reviewerPATCH 6/9 nouveau/dmem: Only map migrating pages
In reply to Alistair PoppleOnly pages which were actually migrated should be mapped on the GPU. migrate_vma_pages() clears MIGRATE_PFN_MIGRATE in the src_pfn array, so test this prior to mapping the pages on the GPU. If any pages failed to migrate don't install any mappings - the GPU will demand fault any as required.
--- drivers/gpu/drm/nouveau/nouveau_dmem.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/gpu/drm/nouveau/nouveau_dmem.c +13 -2
@@ -618,8 +618,9 @@ static void nouveau_dmem_migrate_chunk(struct nouveau_drm *drm, dma_addr_t *dma_addrs, u64 *pfns) {struct nouveau_fence *fence; -unsigned long addr = args->start, nr_dma =0 , i; +unsigned long addr = args->start, nr_dma =0 , i, npages; + npages = (args->start - args->end) >>PAGE_SHIFT ;for (i =0 ; addr < args->end; i++) { args->dst[i] =nouveau_dmem_migrate_copy_one (drm, svmm, args->src[i], dma_addrs + nr_dma, pfns + i);
@@ -631,7 +632,17 @@ static void nouveau_dmem_migrate_chunk(struct nouveau_drm *drm,nouveau_fence_new (drm->dmem->migrate.chan, false, &fence);migrate_vma_pages (args);nouveau_dmem_fence_done (&fence); -nouveau_pfns_map (svmm, args->vma->vm_mm, args->start, pfns, i); + +for (i =0 ; i < npages; i++) +if (!(args->src[i] &MIGRATE_PFN_MIGRATE )) +break ; + +/* +* If all pages were migrated successfully map them on the GPU. If any +* failed just let the GPU fault to create the mapping. +*/ +if (i == npages) +nouveau_pfns_map (svmm, args->vma->vm_mm, args->start, pfns, npages);while (nr_dma--) { dma_unmap_page(drm->dev->dev, dma_addrs[nr_dma], PAGE_SIZE,
Signature · 1 line
2.20.1
Alistair Popple
reviewerPATCH 7/9 nouveau/svm: Refactor nouveau_range_fault
In reply to Alistair PoppleCall mmu_interval_notifier_insert() as part of nouveau_range_fault(). This doesn't introduce any functional change but makes it easier for a subsequent patch to alter the behaviour of nouveau_range_fault() to support GPU atomic operations.
--- drivers/gpu/drm/nouveau/nouveau_svm.c | 34 ++++++++++++++++----------- 1 file changed, 20 insertions(+), 14 deletions(-)
drivers/gpu/drm/nouveau/nouveau_svm.c +20 -14
@@ -567,18 +567,27 @@ static int nouveau_range_fault(struct nouveau_svmm *svmm,unsigned long hmm_pfns[1 ];struct hmm_range range = { .notifier = ¬ifier->notifier, - .start = notifier->notifier.interval_tree.start, - .end = notifier->notifier.interval_tree.last +1 , .default_flags = hmm_flags, .hmm_pfns = hmm_pfns, .dev_private_owner = drm->dev, }; -struct mm_struct *mm = notifier->notifier.mm; +struct mm_struct *mm = svmm->notifier.mm;int ret; + ret =mmu_interval_notifier_insert (¬ifier->notifier, mm, + args->p.addr, args->p.size, + &nouveau_svm_mni_ops); +if (ret) +return ret; + + range.start = notifier->notifier.interval_tree.start; + range.end = notifier->notifier.interval_tree.last +1 ; +while (true) { -if (time_after (jiffies, timeout)) -return -EBUSY ; +if (time_after (jiffies, timeout)) { + ret = -EBUSY ; + goto out; + } range.notifier_seq =mmu_interval_read_begin (range.notifier);mmap_read_lock (mm);
@@ -587,7 +596,7 @@ static int nouveau_range_fault(struct nouveau_svmm *svmm,if (ret) {if (ret == -EBUSY )continue ; -return ret; + goto out; }mutex_lock (&svmm->mutex);
@@ -606,6 +615,9 @@ static int nouveau_range_fault(struct nouveau_svmm *svmm, svmm->vmm->vmm.object.client->super = false;mutex_unlock (&svmm->mutex); +out: +mmu_interval_notifier_remove (¬ifier->notifier); +return ret; }
@@ -727,14 +739,8 @@ nouveau_svm_fault(struct nvif_notify *notify) } notifier.svmm = svmm; - ret =mmu_interval_notifier_insert (¬ifier.notifier, mm, - args.i.p.addr, args.i.p.size, - &nouveau_svm_mni_ops); -if (!ret) { - ret =nouveau_range_fault (svmm, svm->drm, &args.i, -sizeof (args), hmm_flags, ¬ifier); -mmu_interval_notifier_remove (¬ifier.notifier); - } + ret =nouveau_range_fault (svmm, svm->drm, &args.i, +sizeof (args), hmm_flags, ¬ifier);mmput (mm); limit = args.i.p.addr + args.i.p.size;
Signature · 1 line
2.20.1
Alistair Popple
reviewerPATCH 8/9 nouveau/dmem: Add support for multiple page types
In reply to Alistair PoppleDevice private pages are used to track a per-page migrate_to_ram() callback which is called when the CPU attempts to access a GPU page from the CPU. Currently the same callback is used for all GPU pages tracked by Nouveau. However a future patch requires support for calling a different callback when accessing some GPU pages.
This patch extends the existing Nouveau device private page allocator to make it easier to allocate device private pages with different callbacks but should not introduce any functional changes.
--- drivers/gpu/drm/nouveau/nouveau_dmem.c | 27 ++++++++++++++------------ drivers/gpu/drm/nouveau/nouveau_dmem.h | 5 +++++ 2 files changed, 20 insertions(+), 12 deletions(-)
drivers/gpu/drm/nouveau/nouveau_dmem.c +15 -12
@@ -67,6 +67,7 @@ struct nouveau_dmem_chunk {struct nouveau_bo *bo;struct nouveau_drm *drm;unsigned long callocated; +enum nouveau_dmem_type type;struct dev_pagemap pagemap; };
@@ -81,7 +82,7 @@ struct nouveau_dmem {struct nouveau_dmem_migrate migrate;struct list_head chunks;struct mutex mutex; -struct page *free_pages; +struct page *free_pages[NOUVEAU_DMEM_NTYPES ];spinlock_t lock; };
@@ -112,8 +113,8 @@ static void nouveau_dmem_page_free(struct page *page)struct nouveau_dmem *dmem = chunk->drm->dmem;spin_lock (&dmem->lock); - page->zone_device_data = dmem->free_pages; - dmem->free_pages = page; + page->zone_device_data = dmem->free_pages[chunk->type]; + dmem->free_pages[chunk->type] = page;WARN_ON (!chunk->callocated); chunk->callocated--;
@@ -224,7 +225,8 @@ static const struct dev_pagemap_ops nouveau_dmem_pagemap_ops = { };static int -nouveau_dmem_chunk_alloc (struct nouveau_drm *drm,struct page **ppage) +nouveau_dmem_chunk_alloc (struct nouveau_drm *drm,struct page **ppage, +enum nouveau_dmem_type type) {struct nouveau_dmem_chunk *chunk;struct resource *res;
@@ -248,6 +250,7 @@ nouveau_dmem_chunk_alloc(struct nouveau_drm *drm, struct page **ppage) } chunk->drm = drm; + chunk->type = type; chunk->pagemap.type =MEMORY_DEVICE_PRIVATE ; chunk->pagemap.range.start = res->start; chunk->pagemap.range.end = res->end;
@@ -279,8 +282,8 @@ nouveau_dmem_chunk_alloc(struct nouveau_drm *drm, struct page **ppage) page =pfn_to_page (pfn_first);spin_lock (&drm->dmem->lock);for (i =0 ; i <DMEM_CHUNK_NPAGES -1 ; ++i, ++page) { - page->zone_device_data = drm->dmem->free_pages; - drm->dmem->free_pages = page; + page->zone_device_data = drm->dmem->free_pages[type]; + drm->dmem->free_pages[type] = page; } *ppage = page; chunk->callocated++;
@@ -304,22 +307,22 @@ nouveau_dmem_chunk_alloc(struct nouveau_drm *drm, struct page **ppage) }static struct page * -nouveau_dmem_page_alloc_locked (struct nouveau_drm *drm) +nouveau_dmem_page_alloc_locked (struct nouveau_drm *drm,enum nouveau_dmem_type type) {struct nouveau_dmem_chunk *chunk;struct page *page =NULL ;int ret;spin_lock (&drm->dmem->lock); -if (drm->dmem->free_pages) { - page = drm->dmem->free_pages; - drm->dmem->free_pages = page->zone_device_data; +if (drm->dmem->free_pages[type]) { + page = drm->dmem->free_pages[type]; + drm->dmem->free_pages[type] = page->zone_device_data; chunk =nouveau_page_to_chunk (page); chunk->callocated++;spin_unlock (&drm->dmem->lock); }else {spin_unlock (&drm->dmem->lock); - ret =nouveau_dmem_chunk_alloc (drm, &page); + ret =nouveau_dmem_chunk_alloc (drm, &page, type);if (ret)return NULL ; }
@@ -577,7 +580,7 @@ static unsigned long nouveau_dmem_migrate_copy_one(struct nouveau_drm *drm,if (!(src &MIGRATE_PFN_MIGRATE )) goto out; - dpage =nouveau_dmem_page_alloc_locked (drm); + dpage =nouveau_dmem_page_alloc_locked (drm,NOUVEAU_DMEM );if (!dpage) goto out;
drivers/gpu/drm/nouveau/nouveau_dmem.h +5 -0
@@ -28,6 +28,11 @@ struct nouveau_drm;struct nouveau_svmm ;struct hmm_range ; +enum nouveau_dmem_type { +NOUVEAU_DMEM , +NOUVEAU_DMEM_NTYPES ,/* Number of types, must be last */ +}; +#if IS_ENABLED (CONFIG_DRM_NOUVEAU_SVM )void nouveau_dmem_init (struct nouveau_drm *);void nouveau_dmem_fini (struct nouveau_drm *);
Signature · 1 line
2.20.1
Alistair Popple
reviewerPATCH 9/9 nouveau/svm: Implement atomic SVM access
In reply to Alistair PoppleSome NVIDIA GPUs do not support direct atomic access to system memory via PCIe. Instead this must be emulated by granting the GPU exclusive access to the memory. This is achieved by migrating the userspace mappings to device private pages whilst leaving the actual page in place.
The driver then grants the GPU permission to update the page undergoing atomic access via the GPU page tables. When CPU access to the page is required a CPU fault is raised which calls into the device driver via migrate_to_ram() to remap the page into the CPU page tables and revoke GPU access.
--- drivers/gpu/drm/nouveau/include/nvif/if000c.h | 1 + drivers/gpu/drm/nouveau/nouveau_dmem.c | 148 ++++++++++++++++-- drivers/gpu/drm/nouveau/nouveau_dmem.h | 4 + drivers/gpu/drm/nouveau/nouveau_svm.c | 116 ++++++++++++-- drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.h | 1 + .../drm/nouveau/nvkm/subdev/mmu/vmmgp100.c | 6 + 6 files changed, 249 insertions(+), 27 deletions(-)
drivers/gpu/drm/nouveau/include/nvif/if000c.h +1 -0
@@ -77,6 +77,7 @@ struct nvif_vmm_pfnmap_v0 {#define NVIF_VMM_PFNMAP_V0_APER 0x00000000000000f0ULL#define NVIF_VMM_PFNMAP_V0_HOST 0x0000000000000000ULL#define NVIF_VMM_PFNMAP_V0_VRAM 0x0000000000000010ULL +#define NVIF_VMM_PFNMAP_V0_A 0x0000000000000004ULL#define NVIF_VMM_PFNMAP_V0_W 0x0000000000000002ULL#define NVIF_VMM_PFNMAP_V0_V 0x0000000000000001ULL#define NVIF_VMM_PFNMAP_V0_NONE 0x0000000000000000ULL
drivers/gpu/drm/nouveau/nouveau_dmem.c +134 -14
205 lines not on this page · raw message
drivers/gpu/drm/nouveau/nouveau_dmem.h +4 -0
@@ -30,6 +30,7 @@ struct hmm_range;enum nouveau_dmem_type {NOUVEAU_DMEM , +NOUVEAU_ATOMIC ,NOUVEAU_DMEM_NTYPES ,/* Number of types, must be last */ };
@@ -45,6 +46,9 @@ int nouveau_dmem_migrate_vma(struct nouveau_drm *drm,unsigned long start, unsigned long end);unsigned long nouveau_dmem_page_addr (struct page *page); +struct page *nouveau_dmem_page_alloc_locked (struct nouveau_drm *drm, +enum nouveau_dmem_type type); +void nouveau_dmem_set_atomic (struct page *dpage,struct page *atomic_page);#else /* IS_ENABLED(CONFIG_DRM_NOUVEAU_SVM) */ static inline void nouveau_dmem_init (struct nouveau_drm *drm) {}
drivers/gpu/drm/nouveau/nouveau_svm.c +103 -13
173 lines not on this page · raw message
drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.h +1 -0
@@ -178,6 +178,7 @@ void nvkm_vmm_unmap_region(struct nvkm_vmm *, struct nvkm_vma *);#define NVKM_VMM_PFN_APER 0x00000000000000f0ULL#define NVKM_VMM_PFN_HOST 0x0000000000000000ULL#define NVKM_VMM_PFN_VRAM 0x0000000000000010ULL +#define NVKM_VMM_PFN_A 0x0000000000000004ULL#define NVKM_VMM_PFN_W 0x0000000000000002ULL#define NVKM_VMM_PFN_V 0x0000000000000001ULL#define NVKM_VMM_PFN_NONE 0x0000000000000000ULL
drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmmgp100.c +6 -0
@@ -88,6 +88,9 @@ gp100_vmm_pgt_pfn(struct nvkm_vmm *vmm, struct nvkm_mmu_pt *pt,if (!(*map->pfn &NVKM_VMM_PFN_W )) data |=BIT_ULL (6 );/* RO. */ +if (!(*map->pfn &NVKM_VMM_PFN_A )) + data |=BIT_ULL (7 );/* Atomic disable. */ +if (!(*map->pfn &NVKM_VMM_PFN_VRAM )) { addr = *map->pfn >>NVKM_VMM_PFN_ADDR_SHIFT ; addr = dma_map_page(dev, pfn_to_page(addr), 0,
@@ -322,6 +325,9 @@ gp100_vmm_pd0_pfn(struct nvkm_vmm *vmm, struct nvkm_mmu_pt *pt,if (!(*map->pfn &NVKM_VMM_PFN_W )) data |=BIT_ULL (6 );/* RO. */ +if (!(*map->pfn &NVKM_VMM_PFN_A )) + data |=BIT_ULL (7 );/* Atomic disable. */ +if (!(*map->pfn &NVKM_VMM_PFN_VRAM )) { addr = *map->pfn >>NVKM_VMM_PFN_ADDR_SHIFT ; addr = dma_map_page(dev, pfn_to_page(addr), 0,
Signature · 1 line
2.20.1
Quoted 33 lines · Alistair Popple
This series adds support to Nouveau for atomic memory operations on OpenCL shared virtual memory (SVM). This is achieved using the atomic PTE bits on the GPU to only permit atomic operations to system memory when a page is not mapped in userspace on the CPU.
This is implemented by adding a mode to
migrate_vma_pages()which unmaps and isolates existing pages from the CPU and pins them. The original userspace page table entries are migrated to point to device private pages allocated by the driver. This allows the driver to enable GPU atomic access to the page as it will receive a callback when CPU userspace needs to access it.In response to this callback the driver revokes the atomic access permission from the GPU and migrates entries to point back to the original page. The original page is unpinned as part of the migration operation which also returns it to the LRU.
Patch 3 contains the bulk of the memory management changes to implement unmap and pin.
Patches 6-9 extend Nouveau to use the new mode to allow system wide atomics for OpenCL SVM to be implemented on Nouveau.
This has been tested using the latest upstream Mesa userspace with a simple OpenCL test program which checks the results of atomic GPU operations on a buffer whilst also writing to the same buffer from the CPU.
Problems yet to be addressed:
Recent changes to
pin_user_pages()prevent the creation of pinned pages inZONE_MOVABLE. This series allows pinned pages to be created inZONE_MOVABLEas attempts to migrate may fail which would be fatal to userspace.
Quoted 5 lines
In this case migration of the pinned page is unnecessary as the page can be unpinned at anytime by having the driver revoke atomic permission as it does for the
migrate_to_ram()callback. However a method of calling this when memory needs to be moved has yet to be resolved so any discussion is welcome.
Why do we need to pin for gpu atomics? You still have the callback for cpu faults, so you
can move the page as needed, and hence a long-term pin sounds like the wrong approach.
That would avoid all the hacking around long term pin constraints, because for real unmoveable long term pinned memory we really want to have all these checks. So I think we might be missing some other callbacks to be able to move these pages, instead of abusing longterm pins for lack of better tools.
Cheers, Daniel
Quoted 28 lines
Alistair Popple (9):
mm/migrate.c: Always allow device private pages to migratemm/migrate.c: Allow pfn flags to be passed tomigrate_vma_setup()mm/migrate: Add a unmap and pin migration mode
Documentation: Add unmap and pin to HMM
hmm-tests: Add test for unmap and pin
nouveau/dmem: Only map migrating pages
nouveau/svm: Refactornouveau_range_faultnouveau/dmem: Add support for multiple page types nouveau/svm: Implement atomic SVM accessDocumentation/vm/hmm.rst | 22 +- arch/powerpc/kvm/book3s_hv_uvmem.c | 4 +- drivers/gpu/drm/nouveau/include/nvif/if000c.h | 1 + drivers/gpu/drm/nouveau/nouveau_dmem.c | 190 +++++++++++++++--- drivers/gpu/drm/nouveau/nouveau_dmem.h | 9 + drivers/gpu/drm/nouveau/nouveau_svm.c | 148 +++++++++++--- drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.h | 1 + .../drm/nouveau/nvkm/subdev/mmu/vmmgp100.c | 6 + include/linux/migrate.h | 2 + include/linux/migrate_mode.h | 1 + lib/test_hmm.c | 109 ++++++++-- lib/test_hmm_uapi.h | 1 + mm/migrate.c | 82 +++++--- tools/testing/selftests/vm/hmm-tests.c | 49 +++++ 14 files changed, 524 insertions(+), 101 deletions(-)
Quoted 3 lines
--
2.20.1
--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
Alistair Popple
reviewerQuoted 3 lines · AEDT Daniel Vetter
Quoted 3 lines
Recent changes to
pin_user_pages()prevent the creation of pinned pages inZONE_MOVABLE. This series allows pinned pages to be created in
ZONE_MOVABLE
Quoted 3 lines
Quoted 3 lines
as attempts to migrate may fail which would be fatal to userspace.
In this case migration of the pinned page is unnecessary as the page can
be
Quoted 9 lines
Quoted 4 lines
unpinned at anytime by having the driver revoke atomic permission as it does for the
migrate_to_ram()callback. However a method of calling this when memory needs to be moved has yet to be resolved so any discussion is welcome.Why do we need to pin for gpu atomics? You still have the callback for cpu faults, so you
can move the page as needed, and hence a long-term pin sounds like the wrong approach.
Technically a real long term unmoveable pin isn't required, because as you say the page can be moved as needed at any time. However I needed some way of stopping the CPU page from being freed once the userspace mappings for it had been removed. Obviously I could have just used get_page() but from the perspective of page migration the result is much the same as a pin - a page which can't be moved because of the extra refcount.
The normal solution of registering an MMU notifier to unpin the page when it needs to be moved also doesn't work as the CPU page tables now point to the device-private page and hence the migration code won't call any invalidate notifiers for the CPU page.
Quoted 5 lines
That would avoid all the hacking around long term pin constraints, because for real unmoveable long term pinned memory we really want to have all these checks. So I think we might be missing some other callbacks to be able to move these pages, instead of abusing longterm pins for lack of better tools.
Yes, I would like to avoid the long term pin constraints as well if possible I just haven't found a solution yet. Are you suggesting it might be possible to add a callback in the page migration logic to specially deal with moving these pages?
Thanks, Alistair
Quoted 40 lines
Cheers, Daniel
Quoted 28 lines
Alistair Popple (9):
mm/migrate.c: Always allow device private pages to migratemm/migrate.c: Allow pfn flags to be passed tomigrate_vma_setup()mm/migrate: Add a unmap and pin migration mode
Documentation: Add unmap and pin to HMM
hmm-tests: Add test for unmap and pin
nouveau/dmem: Only map migrating pages
nouveau/svm: Refactornouveau_range_faultnouveau/dmem: Add support for multiple page types nouveau/svm: Implement atomic SVM accessDocumentation/vm/hmm.rst | 22 +- arch/powerpc/kvm/book3s_hv_uvmem.c | 4 +- drivers/gpu/drm/nouveau/include/nvif/if000c.h | 1 + drivers/gpu/drm/nouveau/nouveau_dmem.c | 190 +++++++++++++++--- drivers/gpu/drm/nouveau/nouveau_dmem.h | 9 + drivers/gpu/drm/nouveau/nouveau_svm.c | 148 +++++++++++--- drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.h | 1 + .../drm/nouveau/nvkm/subdev/mmu/vmmgp100.c | 6 + include/linux/migrate.h | 2 + include/linux/migrate_mode.h | 1 + lib/test_hmm.c | 109 ++++++++-- lib/test_hmm_uapi.h | 1 + mm/migrate.c | 82 +++++--- tools/testing/selftests/vm/hmm-tests.c | 49 +++++ 14 files changed, 524 insertions(+), 101 deletions(-)Quoted 3 lines
--
2.20.1--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
Quoted 19 lines · Alistair Popple
Quoted 3 lines · AEDT Daniel Vetter
Quoted 3 lines
Recent changes to
pin_user_pages()prevent the creation of pinned pages inZONE_MOVABLE. This series allows pinned pages to be created in
ZONE_MOVABLEQuoted 3 lines
Quoted 3 lines
as attempts to migrate may fail which would be fatal to userspace.
In this case migration of the pinned page is unnecessary as the page can
be
Quoted 9 lines
Quoted 4 lines
unpinned at anytime by having the driver revoke atomic permission as it does for the
migrate_to_ram()callback. However a method of calling this when memory needs to be moved has yet to be resolved so any discussion is welcome.Why do we need to pin for gpu atomics? You still have the callback for cpu faults, so you
can move the page as needed, and hence a long-term pin sounds like the wrong approach.
Quoted 4 lines
Technically a real long term unmoveable pin isn't required, because as you say the page can be moved as needed at any time. However I needed some way of stopping the CPU page from being freed once the userspace mappings for it had been removed.
The issue is you took the page out of the PTE it belongs to, which makes it orphaned and unlocatable by the rest of the mm?
Ideally this would leave the PTE in place so everything continues to work, just disable CPU access to it.
Maybe some kind of special swap entry?
I also don't much like the use of ZONE_DEVICE here, that should only be used for actual device memory, not as a temporary proxy for CPU pages.. Having two struct pages refer to the same physical memory is pretty ugly.
Quoted 4 lines
The normal solution of registering an MMU notifier to unpin the page when it needs to be moved also doesn't work as the CPU page tables now point to the device-private page and hence the migration code won't call any invalidate notifiers for the CPU page.
The fact the page is lost from the MM seems to be the main issue here.
Quoted 4 lines
Yes, I would like to avoid the long term pin constraints as well if possible I just haven't found a solution yet. Are you suggesting it might be possible to add a callback in the page migration logic to specially deal with moving these pages?
How would migration even find the page?
Jason
Quoted 26 lines · Jason Gunthorpe
Quoted 19 lines · Alistair Popple
Quoted 3 lines · AEDT Daniel Vetter
Quoted 3 lines
Recent changes to
pin_user_pages()prevent the creation of pinned pages inZONE_MOVABLE. This series allows pinned pages to be created in
ZONE_MOVABLEQuoted 3 lines
Quoted 3 lines
as attempts to migrate may fail which would be fatal to userspace.
In this case migration of the pinned page is unnecessary as the page can
be
Quoted 9 lines
Quoted 4 lines
unpinned at anytime by having the driver revoke atomic permission as it does for the
migrate_to_ram()callback. However a method of calling this when memory needs to be moved has yet to be resolved so any discussion is welcome.Why do we need to pin for gpu atomics? You still have the callback for cpu faults, so you
can move the page as needed, and hence a long-term pin sounds like the wrong approach.Quoted 4 lines
Technically a real long term unmoveable pin isn't required, because as you say the page can be moved as needed at any time. However I needed some way of stopping the CPU page from being freed once the userspace mappings for it had been removed.
Quoted 7 lines
The issue is you took the page out of the PTE it belongs to, which makes it orphaned and unlocatable by the rest of the mm?
Ideally this would leave the PTE in place so everything continues to work, just disable CPU access to it.
Maybe some kind of special swap entry?
I probably should have read the patches more in detail, I was assuming the ZONE_DEVICE is only for vram. At least I thought the requirement for gpu atomics was that the page is in vram, but maybe I'm mixing up how this works on nvidia with how it works in other places. Iirc we had a long discussion about this at lpc19 that ended with the conclusion that we must be able to migrate, and sometimes migration is blocked. But the details ellude me now.
Either way ZONE_DEVICE for not vram/device memory sounds wrong. Is that really going on here?
-Daniel
Quoted 21 lines
I also don't much like the use of
ZONE_DEVICEhere, that should only be used for actual device memory, not as a temporary proxy for CPU pages.. Having two struct pages refer to the same physical memory is pretty ugly.Quoted 4 lines
The normal solution of registering an MMU notifier to unpin the page when it needs to be moved also doesn't work as the CPU page tables now point to the device-private page and hence the migration code won't call any invalidate notifiers for the CPU page.
The fact the page is lost from the MM seems to be the main issue here.
Quoted 4 lines
Yes, I would like to avoid the long term pin constraints as well if possible I just haven't found a solution yet. Are you suggesting it might be possible to add a callback in the page migration logic to specially deal with moving these pages?
How would migration even find the page?
Jason
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch
Quoted 2 lines · Daniel Vetter
Either way
ZONE_DEVICEfor not vram/device memory sounds wrong. Is that really going on here?
My read was this was doing non-coherent atomics on CPU memory.
Atomics on GPU memory is just called migration to GPU memory, it doesn't need to be special for atomics. In that case it can free the CPU struct page completely as the data now lives in the ZONE_DEVICE page so no need for a pin, no problem with movable
Jason
Quoted 43 lines · Jason Gunthorpe
Quoted 19 lines · Alistair Popple
Quoted 3 lines · AEDT Daniel Vetter
Quoted 3 lines
Recent changes to
pin_user_pages()prevent the creation of pinned pages inZONE_MOVABLE. This series allows pinned pages to be created in
ZONE_MOVABLEQuoted 3 lines
Quoted 3 lines
as attempts to migrate may fail which would be fatal to userspace.
In this case migration of the pinned page is unnecessary as the page can
be
Quoted 9 lines
Quoted 4 lines
unpinned at anytime by having the driver revoke atomic permission as it does for the
migrate_to_ram()callback. However a method of calling this when memory needs to be moved has yet to be resolved so any discussion is welcome.Why do we need to pin for gpu atomics? You still have the callback for cpu faults, so you
can move the page as needed, and hence a long-term pin sounds like the wrong approach.Quoted 4 lines
Technically a real long term unmoveable pin isn't required, because as you say the page can be moved as needed at any time. However I needed some way of stopping the CPU page from being freed once the userspace mappings for it had been removed.
The issue is you took the page out of the PTE it belongs to, which makes it orphaned and unlocatable by the rest of the mm?
Ideally this would leave the PTE in place so everything continues to work, just disable CPU access to it.
Maybe some kind of special swap entry?
I also don't much like the use of
ZONE_DEVICEhere, that should only be used for actual device memory, not as a temporary proxy for CPU pages.. Having two struct pages refer to the same physical memory is pretty ugly.Quoted 4 lines
The normal solution of registering an MMU notifier to unpin the page when it needs to be moved also doesn't work as the CPU page tables now point to the device-private page and hence the migration code won't call any invalidate notifiers for the CPU page.
Quoted 8 lines
The fact the page is lost from the MM seems to be the main issue here.
Quoted 4 lines
Yes, I would like to avoid the long term pin constraints as well if possible I just haven't found a solution yet. Are you suggesting it might be possible to add a callback in the page migration logic to specially deal with moving these pages?
How would migration even find the page?
Migration can scan memory from physical address (isolate_migratepages_range()) So the CPU mapping is not the only path to get to a page.
Cheers,
Jérôme
Quoted 9 lines · Jerome Glisse
Quoted 6 lines
Quoted 4 lines
Yes, I would like to avoid the long term pin constraints as well if possible I just haven't found a solution yet. Are you suggesting it might be possible to add a callback in the page migration logic to specially deal with moving these pages?
How would migration even find the page?
Migration can scan memory from physical address (
isolate_migratepages_range()) So the CPU mapping is not the only path to get to a page.
I mean find out that the page is now owned by the GPU driver to tell it that it needs to migrate that reference. Normally that would go through the VMA to the mmu notifier, but with the page removed from the normal VMA it can't get to a mmu notifier or the GPU driver.
Jason
Quoted 20 lines · Alistair Popple
Quoted 3 lines · AEDT Daniel Vetter
Quoted 3 lines
Recent changes to
pin_user_pages()prevent the creation of pinned pages inZONE_MOVABLE. This series allows pinned pages to be created in
ZONE_MOVABLEQuoted 3 lines
Quoted 3 lines
as attempts to migrate may fail which would be fatal to userspace.
In this case migration of the pinned page is unnecessary as the page can
be
Quoted 9 lines
Quoted 4 lines
unpinned at anytime by having the driver revoke atomic permission as it does for the
migrate_to_ram()callback. However a method of calling this when memory needs to be moved has yet to be resolved so any discussion is welcome.Why do we need to pin for gpu atomics? You still have the callback for cpu faults, so you
can move the page as needed, and hence a long-term pin sounds like the wrong approach.
Quoted 6 lines
Technically a real long term unmoveable pin isn't required, because as you say the page can be moved as needed at any time. However I needed some way of stopping the CPU page from being freed once the userspace mappings for it had been removed. Obviously I could have just used
get_page()but from the perspective of page migration the result is much the same as a pin - a page which can't be moved because of the extra refcount.
long term pin vs short term page reference aren't fully fleshed out. But the rule more or less is:
- short term page reference: _must_ get released in finite time for migration and other things, either because you have a callback, or because it's just for direct I/O, which will complete. This means short term pins will delay migration, but not foul it complete
- long term pin: the page cannot be moved, all migration must fail. Also this will have an impact on COW behaviour for fork (but not sure where those patches are, John Hubbard will know).
So I think for your use case here you want a) short term page reference to make sure it doesn't disappear plus b) callback to make sure migrate isn't blocked.
Breaking ZONE_MOVEABLE with either allowing long term pins or failing migrations because you don't release your short term page reference isn't good.
Quoted 4 lines
The normal solution of registering an MMU notifier to unpin the page when it needs to be moved also doesn't work as the CPU page tables now point to the device-private page and hence the migration code won't call any invalidate notifiers for the CPU page.
Yeah you need some other callback for migration on the page directly. it's a bit awkward since there is one already for struct address_space, but that's own by the address_space/page cache, not HMM. So I think we need something else, maybe something for each ZONE_DEVICE?
Quoted 10 lines
Quoted 5 lines
That would avoid all the hacking around long term pin constraints, because for real unmoveable long term pinned memory we really want to have all these checks. So I think we might be missing some other callbacks to be able to move these pages, instead of abusing longterm pins for lack of better tools.
Yes, I would like to avoid the long term pin constraints as well if possible I just haven't found a solution yet. Are you suggesting it might be possible to add a callback in the page migration logic to specially deal with moving these pages?
s/possible/need to type some code to address it/ I think.
But also I'm not much of an expert on this, I've only just started learning how this all fits together coming from the gpu side. There's a _lot_ of finesse involved.
Cheers, Daniel
Quoted 47 lines
Thanks, Alistair
Quoted 40 lines
Cheers, Daniel
Quoted 28 lines
Alistair Popple (9):
mm/migrate.c: Always allow device private pages to migratemm/migrate.c: Allow pfn flags to be passed tomigrate_vma_setup()mm/migrate: Add a unmap and pin migration mode
Documentation: Add unmap and pin to HMM
hmm-tests: Add test for unmap and pin
nouveau/dmem: Only map migrating pages
nouveau/svm: Refactornouveau_range_faultnouveau/dmem: Add support for multiple page types nouveau/svm: Implement atomic SVM accessDocumentation/vm/hmm.rst | 22 +- arch/powerpc/kvm/book3s_hv_uvmem.c | 4 +- drivers/gpu/drm/nouveau/include/nvif/if000c.h | 1 + drivers/gpu/drm/nouveau/nouveau_dmem.c | 190 +++++++++++++++--- drivers/gpu/drm/nouveau/nouveau_dmem.h | 9 + drivers/gpu/drm/nouveau/nouveau_svm.c | 148 +++++++++++--- drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.h | 1 + .../drm/nouveau/nvkm/subdev/mmu/vmmgp100.c | 6 + include/linux/migrate.h | 2 + include/linux/migrate_mode.h | 1 + lib/test_hmm.c | 109 ++++++++-- lib/test_hmm_uapi.h | 1 + mm/migrate.c | 82 +++++--- tools/testing/selftests/vm/hmm-tests.c | 49 +++++ 14 files changed, 524 insertions(+), 101 deletions(-)Quoted 3 lines
--
2.20.1--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch
Quoted 28 lines · Daniel Vetter
Quoted 20 lines · Alistair Popple
Quoted 3 lines · AEDT Daniel Vetter
Quoted 3 lines
Recent changes to
pin_user_pages()prevent the creation of pinned pages inZONE_MOVABLE. This series allows pinned pages to be created in
ZONE_MOVABLEQuoted 3 lines
Quoted 3 lines
as attempts to migrate may fail which would be fatal to userspace.
In this case migration of the pinned page is unnecessary as the page can
be
Quoted 9 lines
Quoted 4 lines
unpinned at anytime by having the driver revoke atomic permission as it does for the
migrate_to_ram()callback. However a method of calling this when memory needs to be moved has yet to be resolved so any discussion is welcome.Why do we need to pin for gpu atomics? You still have the callback for cpu faults, so you
can move the page as needed, and hence a long-term pin sounds like the wrong approach.Quoted 6 lines
Technically a real long term unmoveable pin isn't required, because as you say the page can be moved as needed at any time. However I needed some way of stopping the CPU page from being freed once the userspace mappings for it had been removed. Obviously I could have just used
get_page()but from the perspective of page migration the result is much the same as a pin - a page which can't be moved because of the extra refcount.
Quoted 6 lines
long term pin vs short term page reference aren't fully fleshed out. But the rule more or less is:
- short term page reference: _must_ get released in finite time for migration and other things, either because you have a callback, or because it's just for direct I/O, which will complete. This means short term pins will delay migration, but not foul it complete
GPU atomic operations to sysmem are hard to categorize, because because application programmers could easily write programs that do a long series of atomic operations. Such a program would be a little weird, but it's hard to rule out.
Quoted 4 lines
- long term pin: the page cannot be moved, all migration must fail. Also this will have an impact on COW behaviour for fork (but not sure where those patches are, John Hubbard will know).
That would be Jason's commit 57efa1fe59576 ("mm/gup: prevent gup_fast from racing with COW during fork"), which is in linux-next 20201216.
Quoted 14 lines
So I think for your use case here you want a) short term page reference to make sure it doesn't disappear plus b) callback to make sure migrate isn't blocked.
Breaking
ZONE_MOVEABLEwith either allowing long term pins or failing migrations because you don't release your short term page reference isn't good.Quoted 4 lines
The normal solution of registering an MMU notifier to unpin the page when it needs to be moved also doesn't work as the CPU page tables now point to the device-private page and hence the migration code won't call any invalidate notifiers for the CPU page.
Quoted 6 lines
Yeah you need some other callback for migration on the page directly. it's a bit awkward since there is one already for struct
address_space, but that's own by the address_space/page cache, not HMM. So I think we need something else, maybe something for eachZONE_DEVICE?
This direction sounds at least...possible. Using MMU notifiers instead of pins is definitely appealing. I'm not quite clear on the callback idea above, but overall it seems like taking advantage of the ZONE_DEVICE tracking of pages (without having to put anything additional in each struct page), could work.
Additional notes or ideas here are definitely welcome.
thanks,
Signature · 2 lines
John Hubbard NVIDIA
Quoted 37 lines · John Hubbard
Quoted 28 lines · Daniel Vetter
Quoted 20 lines · Alistair Popple
Quoted 3 lines · AEDT Daniel Vetter
Quoted 3 lines
Recent changes to
pin_user_pages()prevent the creation of pinned pages inZONE_MOVABLE. This series allows pinned pages to be created in
ZONE_MOVABLEQuoted 3 lines
Quoted 3 lines
as attempts to migrate may fail which would be fatal to userspace.
In this case migration of the pinned page is unnecessary as the page can
be
Quoted 9 lines
Quoted 4 lines
unpinned at anytime by having the driver revoke atomic permission as it does for the
migrate_to_ram()callback. However a method of calling this when memory needs to be moved has yet to be resolved so any discussion is welcome.Why do we need to pin for gpu atomics? You still have the callback for cpu faults, so you
can move the page as needed, and hence a long-term pin sounds like the wrong approach.Quoted 6 lines
Technically a real long term unmoveable pin isn't required, because as you say the page can be moved as needed at any time. However I needed some way of stopping the CPU page from being freed once the userspace mappings for it had been removed. Obviously I could have just used
get_page()but from the perspective of page migration the result is much the same as a pin - a page which can't be moved because of the extra refcount.Quoted 6 lines
long term pin vs short term page reference aren't fully fleshed out. But the rule more or less is:
- short term page reference: _must_ get released in finite time for migration and other things, either because you have a callback, or because it's just for direct I/O, which will complete. This means short term pins will delay migration, but not foul it complete
Quoted 3 lines
GPU atomic operations to sysmem are hard to categorize, because because application programmers could easily write programs that do a long series of atomic operations. Such a program would be a little weird, but it's hard to rule out.
Yeah, but we can forcefully break this whenever we feel like by revoking the page, moving it, and then reinstating the gpu pte again and let it continue.
If that's no possible then what we need here instead is an mlock() type of thing I think.
Quoted 7 lines
Quoted 3 lines
- long term pin: the page cannot be moved, all migration must fail. Also this will have an impact on COW behaviour for fork (but not sure where those patches are, John Hubbard will know).
That would be Jason's commit
57efa1fe59576("mm/gup: preventgup_fastfrom racing with COW during fork"), which is in linux-next 20201216.
Nice, thanks for the pointer.
Quoted 23 lines
Quoted 16 lines
So I think for your use case here you want a) short term page reference to make sure it doesn't disappear plus b) callback to make sure migrate isn't blocked.
Breaking
ZONE_MOVEABLEwith either allowing long term pins or failing migrations because you don't release your short term page reference isn't good.Quoted 4 lines
The normal solution of registering an MMU notifier to unpin the page when it needs to be moved also doesn't work as the CPU page tables now point to the device-private page and hence the migration code won't call any invalidate notifiers for the CPU page.
Quoted 6 lines
Yeah you need some other callback for migration on the page directly. it's a bit awkward since there is one already for struct
address_space, but that's own by the address_space/page cache, not HMM. So I think we need something else, maybe something for eachZONE_DEVICE?
Quoted 6 lines
This direction sounds at least...possible. Using MMU notifiers instead of pins is definitely appealing. I'm not quite clear on the callback idea above, but overall it seems like taking advantage of the
ZONE_DEVICEtracking of pages (without having to put anything additional in each struct page), could work.Additional notes or ideas here are definitely welcome.
Well I don't have ideas for the details tbh, just from the little I learned about how this all fits together pretending to be a pin while pretending to not be a pin just gets us back to the mess we're trying to solve with gup vs pup cleanup. And given that the pin_user_pages rollout hasn't even completed yet it's maybe way to early to already toss it out again.
I think overall we should try really hard to not mix up things between memory that's pinned and memory that can be moved. Because retroactively trying to fix things up just because it was easier to get a feature going that way is very, very hard. And I think we've demonstrated countless times that "ah we just fix this with pinning, it doesn't happen often, no one will notice" always comes back to bite us later on :-)
Like just looking ahead, 1GB pages are a thing, we will have to support that, migrate_page is the only way to get there, and statistically just a 1/256th chance of encountering a pinned page guarantees we'll never assemble a giant page.
Cheers, Daniel
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch
On 2/10/21 4:59 AM, Daniel Vetter wrote:
...
Quoted 7 lines
Quoted 3 lines
GPU atomic operations to sysmem are hard to categorize, because because application programmers could easily write programs that do a long series of atomic operations. Such a program would be a little weird, but it's hard to rule out.
Yeah, but we can forcefully break this whenever we feel like by revoking the page, moving it, and then reinstating the gpu pte again and let it continue.
Oh yes, that's true.
Quoted 3 lines
If that's no possible then what we need here instead is an
mlock()type of thing I think.
No need for that, then.
thanks,
Signature · 2 lines
John Hubbard NVIDIA
Quoted 4 lines · John Hubbard
This direction sounds at least...possible. Using MMU notifiers instead of pins is definitely appealing. I'm not quite clear on the callback idea above, but overall it seems like taking advantage of the
ZONE_DEVICEtracking of pages (without having to put anything additional in each struct page), could work.
It isn't the ZONE_DEVICE page that needs to be tracked.
Really what you want to do here is leave the CPU page in the VMA and the page tables where it started and deny CPU access to the page. Then all the proper machinery will continue to work.
IMHO "migration" is the wrong idea if the data isn't actually moving.
Jason
Quoted 5 lines · Jason Gunthorpe
Really what you want to do here is leave the CPU page in the VMA and the page tables where it started and deny CPU access to the page. Then all the proper machinery will continue to work.
IMHO "migration" is the wrong idea if the data isn't actually moving.
Agreed.
Alistair Popple
reviewerQuoted 8 lines · AEDT Christoph Hellwig
Quoted 5 lines · Jason Gunthorpe
Really what you want to do here is leave the CPU page in the VMA and the page tables where it started and deny CPU access to the page. Then all the proper machinery will continue to work.
IMHO "migration" is the wrong idea if the data isn't actually moving.
Agreed.
I chose "migration" because device private pages seemed like a good way of reusing existing code to do what was required (a callback on CPU access).
However I have been reworking this to use mmu notifiers as the callback and it seems to simplify some things nicely so think I also agree. It removes the requirement for the pin as well which is nice, I'll post it as a v2 soon.
- Alistair