On Mon, Nov 30, 2026 at 11:15:44AM -0800, Niranjana Vishwanathapura wrote:
> On Tue, Nov 24, 2026 at 12:57:14PM -0800, Matthew Brost wrote:
> > On Fri, Nov 13, 2026 at 05:47:59PM -0800, Niranjana Vishwanathapura wrote:
> > > On Fri, Jul 10, 2026 at 07:56:15PM -0700, Matthew Brost wrote:
> > > > A future patch performs the destination DMA mapping of a defrag move in
> > > > two stages when the BO is IOVA mapped: a temporary contiguous mapping of
> > > > just tht changed pages used for the GPU copy, followed by a post-copy
> > > > finalize stet that tears that mapping down and links the new backing
> > > > into the (transferred) IOVA reservation. The finalize step must run only
> > > > after the GPU copy it depends on has completed, so it is dispatched as
> > > > an xe_dep_job,
> > >
> > > Assisted-by: LLM
> > >
> > > NIT...minor types 'tht', 'stet' and trailing comma.
> > >
> > > >
> > > > Add a per-device xe_dep_scheduler dedicated to these finalize jobs,
> > > > created in xe_bo_defrag_init() (after the migrate contexts are up) and
> > > > torn down in xe_bo_defrag_fini(). No users yet; the job itself is added
> > > > in a later patch.
> > > >
> > > > Cc: Carlos Santa <[email protected]>
> > > > Cc: Ryan Neph <[email protected]>
> > > > Cc: Christian Koenig <[email protected]>
> > > > Cc: Huang Rui <[email protected]>
> > > > Cc: Matthew Auld <[email protected]>
> > > > Cc: Maarten Lankhorst <[email protected]>
> > > > Cc: Maxime Ripard <[email protected]>
> > > > Cc: Thomas Zimmermann <[email protected]>
> > > > Cc: David Airlie <[email protected]>
> > > > Cc: Simona Vetter <[email protected]>
> > > > Cc: [email protected]
> > > > Cc: [email protected]
> > > > Cc: Thomas Hellström <[email protected]>
> > > > Assisted-by: GitHub_Copilot:claude-opus-4.8
> > > > Signed-off-by: Matthew Brost <[email protected]>
> > > > ---
> > > > drivers/gpu/drm/xe/xe_bo.c | 15 +++++++++++++++
> > > > drivers/gpu/drm/xe/xe_device_types.h | 10 ++++++++++
> > > > 2 files changed, 25 insertions(+)
> > > >
> > > > diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
> > > > index d54e69aefeba..c5022c16411f 100644
> > > > --- a/drivers/gpu/drm/xe/xe_bo.c
> > > > +++ b/drivers/gpu/drm/xe/xe_bo.c
> > > > @@ -25,6 +25,7 @@
> > > > #include <trace/events/gpu_mem.h>
> > > >
> > > > #include "xe_device.h"
> > > > +#include "xe_dep_scheduler.h"
> > > > #include "xe_dma_buf.h"
> > > > #include "xe_drm_client.h"
> > > > #include "xe_ggtt.h"
> > > > @@ -1306,6 +1307,10 @@ static void xe_bo_defrag_fini(void *arg)
> > > > struct xe_device *xe = arg;
> > > >
> > > > disable_delayed_work_sync(&xe->mem.defrag.worker);
> > > > + if (xe->mem.defrag.iova_sched) {
> > > > + xe_dep_scheduler_fini(xe->mem.defrag.iova_sched);
> > >
> > > LLM suggests adding drm_sched_entity_flush() after calling
> >
> > Honestly I'd rather than not call drm_sched_entity_flush as that a
> > pretty scary function.
> >
> > We might be able to explode here if iova jobs are in flight but I'd
> > really have to think one through. Even if were possible it really just
> > an accidemic issue and no one in practice hotplugs a driver - if we find
> > accidemic issues we can fixup later. My fix probably would be use ref
> > counting from jobs -> xe->mem.defrag.iova_sched to make everything safe
> > (i.e., do what we do for exec queues).
> >
>
> Ok
>
> > > disable_delayed_work_sync() but before calling xe_dep_scheduler_fini().
> > >
> > > > + xe->mem.defrag.iova_sched = NULL;
> > > > + }
> > > > }
> > > >
> > > > /**
> > > > @@ -1342,6 +1347,16 @@ void xe_bo_defrag_init_early(struct xe_device
> > > > *xe)
> > > > */
> > > > int xe_bo_defrag_init(struct xe_device *xe)
> > > > {
> > > > +#define XE_BO_MAX_IOVA_DEFRAG_JOBS 16 /* Picking a reasonable
> > > > value */
> > > > + struct xe_dep_scheduler *iova_sched;
> > > > +
> > > > + iova_sched = xe_dep_scheduler_create(xe, NULL, "xe_iova_defrag",
> > > > +
> > > > XE_BO_MAX_IOVA_DEFRAG_JOBS);
> > >
> > > Should we be creating this only if IOMMU is enabled (IOVA supported)
> > > and defragmentation is enabled?
> >
> > This very small piece of memory and I don't think it worth do any
> > special casing here.
> >
>
> ok, but the check seems simple 'if (use_dma_iommu(xe->drm.dev))'.
>
Yea ok, will add this as I use use_dma_iommu in different place in the
code so this is consistent.
Matt
> > >
> > > > + if (IS_ERR(iova_sched))
> > > > + return PTR_ERR(iova_sched);
> > >
> > > returning early here skips registering xe_bo_defrag_fini. Though it is
> > > unlikely that defrag worker getting scheduled by this time, it is
> > > probably still good to register xe_bo_defrag_fini to ensure worker
> > > is stopped.
> >
> > So call:
> > disable_delayed_work_sync(&xe->mem.defrag.worker);
> >
> > Probably a good idea.
> >
>
> ok.
>
> Niranjana
>
> > Matt
> >
> > >
> > > Niranjana
> > >
> > > > + xe->mem.defrag.iova_sched = iova_sched;
> > > > +#undef XE_BO_MAX_IOVA_DEFRAG_JOBS
> > > > +
> > > > return devm_add_action_or_reset(xe->drm.dev, xe_bo_defrag_fini,
> > > > xe);
> > > > }
> > > >
> > > > diff --git a/drivers/gpu/drm/xe/xe_device_types.h
> > > > b/drivers/gpu/drm/xe/xe_device_types.h
> > > > index 6e68add4d652..b55d549d5e30 100644
> > > > --- a/drivers/gpu/drm/xe/xe_device_types.h
> > > > +++ b/drivers/gpu/drm/xe/xe_device_types.h
> > > > @@ -38,6 +38,7 @@
> > > >
> > > > struct drm_pagemap_shrinker;
> > > > struct intel_display;
> > > > +struct xe_dep_scheduler;
> > > > struct intel_dg_nvm_dev;
> > > > struct xe_ggtt;
> > > > struct xe_i2c;
> > > > @@ -325,6 +326,15 @@ struct xe_device {
> > > > * @mem.defrag.worker, in milliseconds.
> > > > */
> > > > unsigned int interval_ms;
> > > > + /**
> > > > + * @mem.defrag.iova_sched: Dependency scheduler
> > > > used to
> > > > + * run the post-copy IOVA finalize job for
> > > > defrag moves
> > > > + * of IOVA-mapped BOs. The job tears down the
> > > > temporary
> > > > + * copy-step IOVA mapping and links the new
> > > > backing into
> > > > + * the (transferred) reservation once the GPU
> > > > copy that
> > > > + * its dependencies gate has completed.
> > > > + */
> > > > + struct xe_dep_scheduler *iova_sched;
> > > > } defrag;
> > > > } mem;
> > > >
> > > > --
> > > > 2.34.1
> > > >