Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] drm/nouveau/dmem: unserialized shared channel access with TTM
  moves
- [High] drm/nouveau/dmem: unpinned DMA-mapped source in
  nouveau_dmem_migrate_abort()
- [Medium] drm/nouveau/dmem: self-invalidation on PTE-mapped large folio
  split
- [Low] drm/nouveau/svm: missing kernel-parameters.txt entry for
  svm_migrate_on_fault
--

commit 07015472583ba355493e1f5be29cf446dab4bad1
Author: Mika Penttilä <[email protected]>

drm/nouveau/svm: add support for migrate on fault

This commit adds migrate-on-fault support for Nouveau SVM using
HMM_PFN_REQ_MIGRATE and nouveau_dmem_migrate_fault(), gated by the
svm_migrate_on_fault module parameter.

> diff --git a/drivers/gpu/drm/nouveau/nouveau_dmem.c 
> b/drivers/gpu/drm/nouveau/nouveau_dmem.c
> index 36c44d3a3c8fe..ecb8397951c1d 100644
> --- a/drivers/gpu/drm/nouveau/nouveau_dmem.c
> +++ b/drivers/gpu/drm/nouveau/nouveau_dmem.c
> @@ -79,7 +80,7 @@ struct nouveau_dmem_migrate {
>       struct nouveau_channel *chan;
>       /*
>        * Serializes the dmem users of chan (CPU faults in migrate_to_ram(),
> -      * bulk migration, eviction): the copy and clear
> +      * migrate on fault, bulk migration, eviction): the copy and clear
>        * commands they write to its push buffer and the fences they emit.
>        * Held only around those writes, never across memory allocation
>        * (fences are allocated before taking it).

[Severity: High]
Can nouveau_dmem_migrate_fault() race with TTM buffer moves on the shared
copy channel?

nouveau_dmem_migrate_init() sets drm->dmem->migrate.chan to drm->ttm.chan.
While nouveau_dmem_migrate_fault() serializes its pushbuffer writes and
nouveau_dmem_fence_new() emissions with drm->dmem->migrate.lock,
nouveau_bo_move_m2mf() accesses drm->ttm.chan under cli->mutex without
taking migrate.lock:

kworker (nouveau_svm_fault)       TTM move (nouveau_bo_move_m2mf)
nouveau_dmem_migrate_fault()      mutex_lock(&cli->mutex)
  nouveau_dmem_copy()             drm->ttm.move(chan, ...)
    mutex_lock(&migrate.lock)       nve0_bo_move_copy()
    nvc0b5_migrate_copy()             PUSH_MTHD(push, NVA0B5, ...)
      PUSH_MTHD(push, NVA0B5, ...)  nouveau_fence_new(&fence, chan)
    mutex_unlock(&migrate.lock)       nouveau_fence_emit()
  nouveau_dmem_fence_new()              ++fctx->sequence
    mutex_lock(&migrate.lock)
    nouveau_fence_emit()
      ++fctx->sequence  <-- concurrent pushbuffer and sequence corruption

Concurrent GPU fault migrations and TTM moves can corrupt push->cur,
interleave stateful NVA0B5 copy engine methods on the same subchannel, and
race on the unlocked ++fctx->sequence increment in nouveau_fence_emit().

[ ... ]

> @@ -984,6 +1001,262 @@ static void nouveau_dmem_migrate_chunk(struct 
> nouveau_drm *drm,
>       migrate_vma_finalize(args);
>  }
>  
> +/*
> + * Encode an hmm_range_fault() pfn for @start into the nouveau PFNMAP format,
> + * shared by nouveau_hmm_convert_pfn() and the in-place map in
> + * nouveau_dmem_migrate_fault().
> + */

[ ... ]

> +             /*Without a fence (fret set) the copy can't be confirmed. */
> +             if (migrated && !fret)
> +                     fret = nouveau_dmem_fence_poll(&fence);
> +
> +             /* Commit the migration only once the copy is confirmed. */
> +             if (fret)
> +                     nouveau_dmem_migrate_abort(drm, &migrate);
> +             migrate_vma_pages(&migrate);

[ ... ]

> +             /* After an aborted copy the engine may still use the mappings 
> */
> +             while (!fret && nr_dma--) {
> +                     dma_unmap_page(drm->dev->dev, dma_info[nr_dma].dma_addr,
> +                                    dma_info[nr_dma].size, 
> DMA_BIDIRECTIONAL);
> +             }
> +             migrate_vma_finalize(&migrate);

[Severity: High]
When fret is non-zero, does aborting the migration leave the DMA-mapped
source system RAM folio unpinned while the copy engine may still be
reading from it?

Unlike nouveau_dmem_migrate_to_ram() (where args.dst[0] is the DMA-mapped
system RAM page), nouveau_dmem_migrate_copy_one() DMA-maps the source
system RAM page spage (migrate.src[i]) into dma_info->dma_addr.  On abort,
nouveau_dmem_migrate_abort() unlocks and zeroes migrate.dst[i] without
pinning migrate.src[i], the loop above skips dma_unmap_page(), and
migrate_vma_finalize() drops the migration reference on migrate.src[i] and
restores its PTE.  If the process then unmaps the page or exits (or
reclaim evicts it), the system RAM page can be freed back to the page
allocator while its DMA mapping is still active.

In addition, leaking the unmapped VRAM destination folio in migrate.dst[i]
leaves chunk->callocated and pgmap->ref elevated without any PTE pointing
to it, so nouveau_dmem_evict_chunk() cannot migrate it out during
nouveau_dmem_fini(), triggering WARN_ON(chunk->callocated) and hanging in
memunmap_pages().

> diff --git a/drivers/gpu/drm/nouveau/nouveau_svm.c 
> b/drivers/gpu/drm/nouveau/nouveau_svm.c
> index c519c28164fa0..a673952070324 100644
> --- a/drivers/gpu/drm/nouveau/nouveau_svm.c
> +++ b/drivers/gpu/drm/nouveau/nouveau_svm.c
> @@ -78,6 +79,17 @@ struct nouveau_svm {
>  #define SVM_DBG(s,f,a...) NV_DEBUG((s)->drm, "svm: "f"\n", ##a)
>  #define SVM_ERR(s,f,a...) NV_WARN((s)->drm, "svm: "f"\n", ##a)
>  
> +/*
> + * When set, migrate the faulting range to VRAM on GPU fault (default off).
> + * Like the other nouveau parameters, it is documented by MODULE_PARM_DESC
> + * only, and it matters only with the experimental SVM support.
> + */
> +static bool nouveau_svm_migrate_on_fault;
> +module_param_named(svm_migrate_on_fault, nouveau_svm_migrate_on_fault, bool,
> +                0644);
> +MODULE_PARM_DESC(svm_migrate_on_fault,
> +              "migrate system memory to VRAM on GPU fault (default off)");

[Severity: Low]
This adds the nouveau.svm_migrate_on_fault module parameter without an
entry in Documentation/admin-guide/kernel-parameters.txt (or
Documentation/gpu/nouveau.rst); noting this for maintainer awareness since
it is gated by the experimental CONFIG_DRM_NOUVEAU_SVM option.

[ ... ]

> @@ -527,6 +547,17 @@ static bool nouveau_svm_range_invalidate(struct 
> mmu_interval_notifier *mni,
>           range->owner == sn->svmm->vmm->cli->drm->dev)
>               return true;
>  
> +     /*
> +      * Ignore the invalidation of this fault's own migrate-on-fault walk,
> +      * which runs in the walking task; otherwise its MMU_NOTIFY_MIGRATE
> +      * bumps the seq and read_retry() spins to timeout. Other migrations
> +      * on the device (CPU faults, bind) must still invalidate.
> +      */
> +     if (range->event == MMU_NOTIFY_MIGRATE &&
> +         range->owner == sn->svmm->vmm->cli->drm->dev &&
> +         READ_ONCE(sn->migrating) == current)
> +             return true;

[Severity: Medium]
Does a migrate-on-fault walk that splits a PTE-mapped large folio
self-invalidate its own mmu_interval_notifier?

When a GPU fault lands on a PTE-mapped large anonymous folio (such as an
mTHP or an already PMD-split THP), hmm_range_fault() splits the folio via
hmm_vma_handle_migrate_prepare() -> hmm_migrate_split_folio() ->
split_folio(), which emits an MMU_NOTIFY_CLEAR invalidation.  Because
nouveau_svm_range_invalidate() only ignores MMU_NOTIFY_MIGRATE for
sn->migrating == current, MMU_NOTIFY_CLEAR updates mni->invalidate_seq.
Unlike the PMD split path (which returns -EBUSY so
hmm_range_fault_locked() checks mmu_interval_check_retry() before
collecting), hmm_vma_handle_migrate_prepare() returns HMM_MIGRATE_REWALK
and hmm_vma_walk_pmd() re-walks internally and returns 0 with a stale
range.notifier_seq.  nouveau_dmem_migrate_fault() then completes the VRAM
copy, fault replay, fence wait, and migrate_vma_pages(), only for
mmu_interval_read_retry() to fail with -EBUSY, forcing
nouveau_svmm_invalidate() and an unnecessary retry loop iteration.

-- 
Sashiko AI review · 
https://sashiko.dev/#/patchset/[email protected]?part=14

Reply via email to