[PATCH 4/4] drm/xe: Implement VM snapshot support for BO's and userptr, v2.

Souza, Jose jose.souza at intel.com
Thu Jan 25 18:39:01 UTC 2024


On Wed, 2024-01-24 at 17:52 +0100, Maarten Lankhorst wrote:
> Since we cannot immediately capture the BO's and userptr, perform it in
> 2 stages. The immediate stage takes a reference to each BO and userptr,
> while a delayed worker captures the contents and then frees the
> reference.
> 
> This is required because in signaling context, no locks can be taken, no
> memory can be allocated, and no waits on userspace can be performed.
> 
> With the delayed worker, all of this can be performed very easily,
> without having to resort to hacks.
> 
> Changes since v1:
> - Fix crash on NULL captured vm.
> - Use ascii85_encode to capture BO contents and save some space. (Jose
>   Souza)
> - Add length to coredump output for each captured area.

[  222.474873] xe 0000:00:02.0: [drm:intel_pps_vdd_off_sync_unlocked [xe]] [ENCODER:307:DDI A/PHY A] PPS 0 PP_STATUS: 0x80000008 PP_CONTROL:
0x00000067
[  266.462371] xe 0000:00:02.0: [drm] Timedout job: seqno=4294967169, guc_id=2, flags=0x8
[  266.462912] ------------[ cut here ]------------
[  266.462912] irq event stamp: 188994
[  266.462923] hardirqs last  enabled at (188993): [<ffffffff81d2e2ca>] _raw_read_unlock_irqrestore+0x4a/0x70
[  266.462948] DEBUG_LOCKS_WARN_ON(lock->magic != lock)
[  266.462959] hardirqs last disabled at (188994): [<ffffffff81d2db1a>] _raw_spin_lock_irqsave+0x4a/0x50
[  266.462988] WARNING: CPU: 2 PID: 918 at kernel/locking/mutex.c:587 __mutex_lock+0x50d/0xb80
[  266.463001] softirqs last  enabled at (188988): [<ffffffff81b7a5b4>] tcp_sendmsg+0x14/0x40
[  266.463031] Modules linked in:
[  266.463055] softirqs last disabled at (188986): [<ffffffff81ac4607>] lock_sock_nested+0x47/0x70
[  266.463083]  xe drm_ttm_helper gpu_sched drm_suballoc_helper drm_gpuvm drm_exec i2c_algo_bit drm_buddy video drm_display_helper ttm
snd_hda_codec_hdmi snd_ctl_led ledtrig_audio snd_hda_codec_realtek snd_hda_codec_generic mei_hdcp mei_pxp wmi_bmof snd_hda_intel x86_pkg_temp_thermal
snd_intel_dspcfg snd_hda_codec snd_hwdep coretemp snd_hda_core e1000e crct10dif_pclmul crc32_pclmul ghash_clmulni_intel kvm_intel snd_pcm ptp mei_me
i2c_i801 pps_core i2c_smbus mei intel_pmc_core intel_vsec pmt_telemetry wmi pmt_class fuse [last unloaded: ttm]
[  266.463298] CPU: 2 PID: 918 Comm: kworker/u16:5 Not tainted 6.8.0-rc1-zeh-xe+ #1212
[  266.463315] Hardware name: Dell Inc. Latitude 5420/01M3M4, BIOS 1.27.0 03/17/2023
[  266.463328] Workqueue: gt-ordered-wq drm_sched_job_timedout [gpu_sched]
[  266.463347] RIP: 0010:__mutex_lock+0x50d/0xb80
[  266.463360] Code: ff 85 c0 0f 84 7d fb ff ff 8b 15 d2 ea ba 00 85 d2 0f 85 6f fb ff ff 48 c7 c6 b6 41 3a 82 48 c7 c7 5f d1 39 82 e8 a3 1b 40 ff
<0f> 0b e9 55 fb ff ff 31 c9 31 d2 4c 89 e7 e8 b0 80 47 ff 84 c0 0f
[  266.463405] RSP: 0018:ffffc900022bbc30 EFLAGS: 00010282
[  266.463415] RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000000
[  266.463428] RDX: 0000000000000002 RSI: 0000000000000027 RDI: 00000000ffffffff
[  266.463440] RBP: ffffc900022bbcc0 R08: 0000000000000000 R09: 0000000000000000
[  266.463452] R10: ffffffff811bdedf R11: 0000000000000001 R12: ffff88813fd88ca8
[  266.463464] R13: 0000000000000000 R14: ffff88813fd88838 R15: ffff88814438fa00
[  266.463476] FS:  0000000000000000(0000) GS:ffff888287900000(0000) knlGS:0000000000000000
[  266.463489] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  266.463499] CR2: 00007f84dad9dbc8 CR3: 000000000564a005 CR4: 0000000000770ef0
[  266.463511] PKRU: 55555554
[  266.463519] Call Trace:
[  266.463526]  <TASK>
[  266.463532]  ? __mutex_lock+0x50d/0xb80
[  266.463542]  ? __warn+0x7c/0x170
[  266.463552]  ? __mutex_lock+0x50d/0xb80
[  266.463572]  ? report_bug+0x189/0x1c0
[  266.463588]  ? handle_bug+0x36/0x70
[  266.463604]  ? exc_invalid_op+0x13/0x60
[  266.463620]  ? asm_exc_invalid_op+0x16/0x20
[  266.463639]  ? console_flush_all+0x1ef/0x540
[  266.463658]  ? __mutex_lock+0x50d/0xb80
[  266.463674]  ? __slab_alloc.isra.0+0x4d/0x90
[  266.463696]  ? __slab_alloc.isra.0+0x5a/0x90
[  266.463713]  ? xe_vm_snapshot_capture+0x35/0x1f0 [xe]
[  266.463827]  ? rcu_is_watching+0xd/0x40
[  266.463846]  ? __kmalloc+0x2bd/0x400
[  266.463864]  ? xe_vm_snapshot_capture+0x35/0x1f0 [xe]
[  266.463970]  xe_vm_snapshot_capture+0x35/0x1f0 [xe]
[  266.464071]  ? xe_sched_job_snapshot_capture+0x64/0x80 [xe]
[  266.464143]  xe_devcoredump+0x1b9/0x2e0 [xe]
[  266.464194]  guc_exec_queue_timedout_job+0x1ab/0x730 [xe]
[  266.464250]  ? find_held_lock+0x2b/0x80
[  266.464262]  drm_sched_job_timedout+0x77/0xe0 [gpu_sched]
[  266.464279]  ? process_one_work+0x18d/0x4d0
[  266.464290]  process_one_work+0x1f4/0x4d0
[  266.464301]  worker_thread+0x1d8/0x3c0
[  266.464311]  ? rescuer_thread+0x390/0x390
[  266.464321]  kthread+0xfb/0x130
[  266.464329]  ? kthread_complete_and_exit+0x20/0x20
[  266.464341]  ret_from_fork+0x28/0x40
[  266.464352]  ? kthread_complete_and_exit+0x20/0x20
[  266.464362]  ret_from_fork_asm+0x11/0x20
[  266.464375]  </TASK>
[  266.464382] irq event stamp: 1412193
[  266.464389] hardirqs last  enabled at (1412193): [<ffffffff81d2e2ca>] _raw_read_unlock_irqrestore+0x4a/0x70
[  266.464408] hardirqs last disabled at (1412192): [<ffffffff81d2df50>] _raw_read_lock_irqsave+0x80/0x90
[  266.464428] softirqs last  enabled at (1412170): [<ffffffff81131c32>] irq_exit_rcu+0x82/0xe0
[  266.465466] softirqs last disabled at (1412029): [<ffffffff81131c32>] irq_exit_rcu+0x82/0xe0
[  266.466585] ---[ end trace 0000000000000000 ]---
[  266.467585] xe 0000:00:02.0: [drm] Xe device coredump has been created
[  266.468540] xe 0000:00:02.0: [drm] Check your /sys/class/drm/card0/device/devcoredump/data
[  266.470590] xe 0000:00:02.0: [drm] Engine reset: guc_id=2
[  266.472076] xe 0000:00:02.0: [drm] Timedout job: seqno=4294967169, guc_id=3, flags=0x8
[  266.474044] xe 0000:00:02.0: [drm:guc_exec_queue_timedout_job [xe]] Multiple hangs


> 
> Signed-off-by: Maarten Lankhorst <maarten.lankhorst at linux.intel.com>
> ---
>  drivers/gpu/drm/xe/xe_devcoredump.c       |  32 ++++-
>  drivers/gpu/drm/xe/xe_devcoredump_types.h |   8 ++
>  drivers/gpu/drm/xe/xe_vm.c                | 167 ++++++++++++++++++++++
>  drivers/gpu/drm/xe/xe_vm.h                |   5 +
>  4 files changed, 210 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/gpu/drm/xe/xe_devcoredump.c b/drivers/gpu/drm/xe/xe_devcoredump.c
> index 68abc0b195be..4a3bbcc4f7a0 100644
> --- a/drivers/gpu/drm/xe/xe_devcoredump.c
> +++ b/drivers/gpu/drm/xe/xe_devcoredump.c
> @@ -16,6 +16,7 @@
>  #include "xe_guc_ct.h"
>  #include "xe_guc_submit.h"
>  #include "xe_hw_engine.h"
> +#include "xe_vm.h"
>  
>  /**
>   * DOC: Xe device coredump
> @@ -58,11 +59,20 @@ static struct xe_guc *exec_queue_to_guc(struct xe_exec_queue *q)
>  	return &q->gt->uc.guc;
>  }
>  
> +static void xe_devcoredump_deferred_snap_work(struct work_struct *work)
> +{
> +	struct xe_devcoredump_snapshot *ss = container_of(work, typeof(*ss), work);
> +
> +	xe_force_wake_get(gt_to_fw(ss->gt), XE_FORCEWAKE_ALL);
> +	xe_vm_snapshot_capture_delayed(ss->vm);
> +	xe_force_wake_put(gt_to_fw(ss->gt), XE_FORCEWAKE_ALL);
> +}
> +
>  static ssize_t xe_devcoredump_read(char *buffer, loff_t offset,
>  				   size_t count, void *data, size_t datalen)
>  {
>  	struct xe_devcoredump *coredump = data;
> -	struct xe_devcoredump_snapshot *ss;
> +	struct xe_devcoredump_snapshot *ss = &coredump->snapshot;
>  	struct drm_printer p;
>  	struct drm_print_iterator iter;
>  	struct timespec64 ts;
> @@ -72,12 +82,14 @@ static ssize_t xe_devcoredump_read(char *buffer, loff_t offset,
>  	if (!data || !coredump_to_xe(coredump))
>  		return -ENODEV;
>  
> +	/* Ensure delayed work is captured before continuing */
> +	flush_work(&ss->work);
> +
>  	iter.data = buffer;
>  	iter.offset = 0;
>  	iter.start = offset;
>  	iter.remain = count;
>  
> -	ss = &coredump->snapshot;
>  	p = drm_coredump_printer(&iter);
>  
>  	drm_printf(&p, "**** Xe Device Coredump ****\n");
> @@ -98,6 +110,10 @@ static ssize_t xe_devcoredump_read(char *buffer, loff_t offset,
>  		if (coredump->snapshot.hwe[i])
>  			xe_hw_engine_snapshot_print(coredump->snapshot.hwe[i],
>  						    &p);
> +	if (coredump->snapshot.vm) {
> +		drm_printf(&p, "\n**** VM state ****\n");
> +		xe_vm_snapshot_print(coredump->snapshot.vm, &p);
> +	}
>  
>  	return count - iter.remain;
>  }
> @@ -111,11 +127,15 @@ static void xe_devcoredump_free(void *data)
>  	if (!data || !coredump_to_xe(coredump))
>  		return;
>  
> +	cancel_work_sync(&coredump->snapshot.work);
> +
>  	xe_guc_ct_snapshot_free(coredump->snapshot.ct);
>  	xe_guc_exec_queue_snapshot_free(coredump->snapshot.ge);
>  	for (i = 0; i < XE_NUM_HW_ENGINES; i++)
>  		if (coredump->snapshot.hwe[i])
>  			xe_hw_engine_snapshot_free(coredump->snapshot.hwe[i]);
> +	xe_vm_snapshot_free(coredump->snapshot.vm);
> +	memset(&coredump->snapshot, 0, sizeof(coredump->snapshot));
>  
>  	coredump->captured = false;
>  	drm_info(&coredump_to_xe(coredump)->drm,
> @@ -137,6 +157,9 @@ static void devcoredump_snapshot(struct xe_devcoredump *coredump,
>  	ss->snapshot_time = ktime_get_real();
>  	ss->boot_time = ktime_get_boottime();
>  
> +	ss->gt = q->gt;
> +	INIT_WORK(&ss->work, xe_devcoredump_deferred_snap_work);
> +
>  	cookie = dma_fence_begin_signalling();
>  	for (i = 0; q->width > 1 && i < XE_HW_ENGINE_MAX_INSTANCE;) {
>  		if (adj_logical_mask & BIT(i)) {
> @@ -151,6 +174,7 @@ static void devcoredump_snapshot(struct xe_devcoredump *coredump,
>  
>  	coredump->snapshot.ct = xe_guc_ct_snapshot_capture(&guc->ct, true);
>  	coredump->snapshot.ge = xe_guc_exec_queue_snapshot_capture(q);
> +	coredump->snapshot.vm = xe_vm_snapshot_capture(q->vm);
>  
>  	for_each_hw_engine(hwe, q->gt, id) {
>  		if (hwe->class != q->hwe->class ||
> @@ -161,6 +185,9 @@ static void devcoredump_snapshot(struct xe_devcoredump *coredump,
>  		coredump->snapshot.hwe[id] = xe_hw_engine_snapshot_capture(hwe);
>  	}
>  
> +	if (ss->vm)
> +		queue_work(system_unbound_wq, &ss->work);
> +
>  	xe_force_wake_put(gt_to_fw(q->gt), XE_FORCEWAKE_ALL);
>  	dma_fence_end_signalling(cookie);
>  }
> @@ -194,3 +221,4 @@ void xe_devcoredump(struct xe_exec_queue *q)
>  		      xe_devcoredump_read, xe_devcoredump_free);
>  }
>  #endif
> +
> diff --git a/drivers/gpu/drm/xe/xe_devcoredump_types.h b/drivers/gpu/drm/xe/xe_devcoredump_types.h
> index 7fdad9c3d3dd..17ae3e3597b0 100644
> --- a/drivers/gpu/drm/xe/xe_devcoredump_types.h
> +++ b/drivers/gpu/drm/xe/xe_devcoredump_types.h
> @@ -12,6 +12,7 @@
>  #include "xe_hw_engine_types.h"
>  
>  struct xe_device;
> +struct xe_gt;
>  
>  /**
>   * struct xe_devcoredump_snapshot - Crash snapshot
> @@ -26,6 +27,11 @@ struct xe_devcoredump_snapshot {
>  	/** @boot_time:  Relative boot time so the uptime can be calculated. */
>  	ktime_t boot_time;
>  
> +	/** @gt: Affected GT, used by forcewake for delayed capture */
> +	struct xe_gt *gt;
> +	/** @work: Workqueue for deffered capture outside of signaling context */
> +	struct work_struct work;
> +
>  	/* GuC snapshots */
>  	/** @ct: GuC CT snapshot */
>  	struct xe_guc_ct_snapshot *ct;
> @@ -33,6 +39,8 @@ struct xe_devcoredump_snapshot {
>  	struct xe_guc_submit_exec_queue_snapshot *ge;
>  	/** @hwe: HW Engine snapshot array */
>  	struct xe_hw_engine_snapshot *hwe[XE_NUM_HW_ENGINES];
> +	/** @vm: Snapshot of VM state */
> +	struct xe_vm_snapshot *vm;
>  };
>  
>  /**
> diff --git a/drivers/gpu/drm/xe/xe_vm.c b/drivers/gpu/drm/xe/xe_vm.c
> index e9672df71081..e27cf2da3f4d 100644
> --- a/drivers/gpu/drm/xe/xe_vm.c
> +++ b/drivers/gpu/drm/xe/xe_vm.c
> @@ -13,6 +13,7 @@
>  #include <drm/ttm/ttm_execbuf_util.h>
>  #include <drm/ttm/ttm_tt.h>
>  #include <drm/xe_drm.h>
> +#include <linux/ascii85.h>
>  #include <linux/delay.h>
>  #include <linux/kthread.h>
>  #include <linux/mm.h>
> @@ -3237,3 +3238,169 @@ int xe_analyze_vm(struct drm_printer *p, struct xe_vm *vm, int gt_id)
>  
>  	return 0;
>  }
> +
> +struct xe_vm_snapshot {
> +	unsigned long num_snaps;
> +	struct {
> +		uint64_t ofs, bo_ofs;
> +		unsigned long len;
> +		struct xe_bo *bo;
> +		void *data;
> +		struct mm_struct *mm;
> +	} snap[];
> +};
> +
> +struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm)
> +{
> +	unsigned long num_snaps = 0, i;
> +	struct xe_vm_snapshot *snap = NULL;
> +	struct drm_gpuva *gpuva;
> +
> +	if (!vm)
> +		return NULL;
> +
> +	mutex_lock(&vm->snap_mutex);
> +	drm_gpuvm_for_each_va(gpuva, &vm->gpuvm) {
> +		if (gpuva->flags & XE_VMA_DUMPABLE)
> +			num_snaps++;
> +	}
> +
> +	if (num_snaps)
> +		snap = kvzalloc(offsetof(struct xe_vm_snapshot, snap[num_snaps]), GFP_NOWAIT);
> +	if (!snap)
> +		goto out_unlock;
> +
> +	snap->num_snaps = num_snaps;
> +	i = 0;
> +	drm_gpuvm_for_each_va(gpuva, &vm->gpuvm) {
> +		struct xe_vma *vma = gpuva_to_vma(gpuva);
> +		struct xe_bo *bo = vma->gpuva.gem.obj ?
> +			gem_to_xe_bo(vma->gpuva.gem.obj) : NULL;
> +
> +		if (!(gpuva->flags & XE_VMA_DUMPABLE))
> +			continue;
> +
> +		snap->snap[i].ofs = xe_vma_start(vma);
> +		snap->snap[i].len = xe_vma_size(vma);
> +		if (bo) {
> +			snap->snap[i].bo = xe_bo_get(bo);
> +			snap->snap[i].bo_ofs = xe_vma_bo_offset(vma);
> +		} else if (xe_vma_is_userptr(vma)) {
> +			if (mmget_not_zero(vma->userptr.notifier.mm))
> +				snap->snap[i].mm = vma->userptr.notifier.mm;
> +			else
> +				snap->snap[i].data = ERR_PTR(-EFAULT);
> +			snap->snap[i].bo_ofs = xe_vma_userptr(vma);
> +		} else {
> +			snap->snap[i].data = ERR_PTR(-ENOENT);
> +		}
> +		i++;
> +	}
> +
> +out_unlock:
> +	mutex_unlock(&vm->snap_mutex);
> +	return snap;
> +}
> +
> +void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap)
> +{
> +	if (!snap)
> +		return;
> +
> +	for (int i = 0; i < snap->num_snaps; i++) {
> +		struct xe_bo *bo = snap->snap[i].bo;
> +		struct iosys_map src;
> +		int err;
> +
> +		if (IS_ERR(snap->snap[i].data))
> +			continue;
> +
> +		snap->snap[i].data = kvmalloc(snap->snap[i].len, GFP_USER);
> +		if (!snap->snap[i].data) {
> +			snap->snap[i].data = ERR_PTR(-ENOMEM);
> +			goto cleanup_bo;
> +		}
> +
> +		if (bo) {
> +			dma_resv_lock(bo->ttm.base.resv, NULL);
> +			err = ttm_bo_vmap(&bo->ttm, &src);
> +			if (!err) {
> +				xe_map_memcpy_from(xe_bo_device(bo),
> +						   snap->snap[i].data,
> +						   &src, snap->snap[i].bo_ofs,
> +						   snap->snap[i].len);
> +				ttm_bo_vunmap(&bo->ttm, &src);
> +			}
> +			dma_resv_unlock(bo->ttm.base.resv);
> +		} else {
> +			void __user *userptr = (void __user *)(size_t)snap->snap[i].bo_ofs;
> +			kthread_use_mm(snap->snap[i].mm);
> +
> +			if (!copy_from_user(snap->snap[i].data, userptr, snap->snap[i].len))
> +				err = 0;
> +			else
> +				err = -EFAULT;
> +			kthread_unuse_mm(snap->snap[i].mm);
> +			mmput(snap->snap[i].mm);
> +			snap->snap[i].mm = NULL;
> +		}
> +
> +		if (err) {
> +			kvfree(snap->snap[i].data);
> +			snap->snap[i].data = ERR_PTR(err);
> +		}
> +
> +cleanup_bo:
> +		xe_bo_put(bo);
> +		snap->snap[i].bo = NULL;
> +	}
> +}
> +
> +void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p)
> +{
> +	unsigned long i, j;
> +
> +	for (i = 0; i < snap->num_snaps; i++) {
> +		if (IS_ERR(snap->snap[i].data))
> +			goto uncaptured;
> +
> +		drm_printf(p, "[%llx].length: 0x%lx\n", snap->snap[i].ofs, snap->snap[i].len);
> +
> +		for (j = 0; j < snap->snap[i].len; j += 64) {
> +			uint32_t *val = snap->snap[i].data + j;
> +			char dumped[16][ASCII85_BUFSZ];
> +			const char *x[16];
> +			uint32_t k;
> +
> +			for (k = 0; k < 16; k++)
> +				x[k] = ascii85_encode(val[k], dumped[k]);
> +
> +			drm_printf(p, "[%llx].data: { %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s }\n",
> +				   snap->snap[i].ofs + j, x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7],
> +				   x[8], x[9], x[10], x[11], x[12], x[13], x[14], x[15]);
> +		}
> +		continue;
> +
> +uncaptured:
> +		drm_printf(p, "Unable to capture range [%llx-%llx]: %li\n",
> +			   snap->snap[i].ofs, snap->snap[i].ofs + snap->snap[i].len - 1,
> +			   PTR_ERR(snap->snap[i].data));
> +	}
> +}
> +
> +void xe_vm_snapshot_free(struct xe_vm_snapshot *snap)
> +{
> +	unsigned long i;
> +
> +	if (!snap)
> +		return;
> +
> +	for (i = 0; i < snap->num_snaps; i++) {
> +		if (!IS_ERR(snap->snap[i].data))
> +			kvfree(snap->snap[i].data);
> +		xe_bo_put(snap->snap[i].bo);
> +		if (snap->snap[i].mm)
> +			mmput(snap->snap[i].mm);
> +	}
> +	kvfree(snap);
> +}
> diff --git a/drivers/gpu/drm/xe/xe_vm.h b/drivers/gpu/drm/xe/xe_vm.h
> index e9c907cbcd89..8b061f1f753a 100644
> --- a/drivers/gpu/drm/xe/xe_vm.h
> +++ b/drivers/gpu/drm/xe/xe_vm.h
> @@ -259,3 +259,8 @@ static inline void vm_dbg(const struct drm_device *dev,
>  { /* noop */ }
>  #endif
>  #endif
> +
> +struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm);
> +void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap);
> +void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p);
> +void xe_vm_snapshot_free(struct xe_vm_snapshot *snap);



More information about the Intel-xe mailing list