xref: /linux/drivers/gpu/drm/xe/xe_devcoredump.c (revision 5b0764a53235d3c77e1ed674e836e1b3fe1a4b1f)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #include "xe_devcoredump.h"
7 #include "xe_devcoredump_types.h"
8 
9 #include <linux/ascii85.h>
10 #include <linux/devcoredump.h>
11 #include <linux/utsname.h>
12 
13 #include <drm/drm_managed.h>
14 
15 #include "xe_device.h"
16 #include "xe_exec_queue.h"
17 #include "xe_force_wake.h"
18 #include "xe_gt_printk.h"
19 #include "xe_gt_types.h"
20 #include "xe_guc_capture.h"
21 #include "xe_guc_ct.h"
22 #include "xe_guc_log.h"
23 #include "xe_guc_submit.h"
24 #include "xe_hw_engine.h"
25 #include "xe_pm.h"
26 #include "xe_sched_job.h"
27 #include "xe_vm.h"
28 
29 /**
30  * DOC: Xe device coredump
31  *
32  * Xe uses dev_coredump infrastructure for exposing the crash errors in a
33  * standardized way. Once a crash occurs, devcoredump exposes a temporary
34  * node under ``/sys/class/devcoredump/devcd<m>/``. The same node is also
35  * accessible in ``/sys/class/drm/card<n>/device/devcoredump/``. The
36  * ``failing_device`` symlink points to the device that crashed and created the
37  * coredump.
38  *
39  * The following characteristics are observed by xe when creating a device
40  * coredump:
41  *
42  * **Snapshot at hang**:
43  *   The 'data' file contains a snapshot of the HW and driver states at the time
44  *   the hang happened. Due to the driver recovering from resets/crashes, it may
45  *   not correspond to the state of the system when the file is read by
46  *   userspace.
47  *
48  * **Coredump release**:
49  *   After a coredump is generated, it stays in kernel memory until released by
50  *   userspace by writing anything to it, or after an internal timer expires. The
51  *   exact timeout may vary and should not be relied upon. Example to release
52  *   a coredump:
53  *
54  *   .. code-block:: shell
55  *
56  *	$ > /sys/class/drm/card0/device/devcoredump/data
57  *
58  * **First failure only**:
59  *   In general, the first hang is the most critical one since the following
60  *   hangs can be a consequence of the initial hang. For this reason a snapshot
61  *   is taken only for the first failure. Until the devcoredump is released by
62  *   userspace or kernel, all subsequent hangs do not override the snapshot nor
63  *   create new ones. Devcoredump has a delayed work queue that will eventually
64  *   delete the file node and free all the dump information.
65  */
66 
67 #ifdef CONFIG_DEV_COREDUMP
68 
69 /* 1 hour timeout */
70 #define XE_COREDUMP_TIMEOUT_JIFFIES (60 * 60 * HZ)
71 
72 static struct xe_device *coredump_to_xe(const struct xe_devcoredump *coredump)
73 {
74 	return container_of(coredump, struct xe_device, devcoredump);
75 }
76 
77 static ssize_t __xe_devcoredump_read(char *buffer, ssize_t count,
78 				     ssize_t start,
79 				     struct xe_devcoredump *coredump)
80 {
81 	struct xe_device *xe;
82 	struct xe_devcoredump_snapshot *ss;
83 	struct drm_printer p;
84 	struct drm_print_iterator iter;
85 	struct timespec64 ts;
86 	int i;
87 
88 	xe = coredump_to_xe(coredump);
89 	ss = &coredump->snapshot;
90 
91 	iter.data = buffer;
92 	iter.start = start;
93 	iter.remain = count;
94 
95 	p = drm_coredump_printer(&iter);
96 
97 	drm_puts(&p, "**** Xe Device Coredump ****\n");
98 	drm_printf(&p, "Reason: %s\n", ss->reason);
99 	drm_printf(&p, "kernel: %s\n", init_utsname()->release);
100 	drm_puts(&p, "module: " KBUILD_MODNAME "\n");
101 
102 	ts = ktime_to_timespec64(ss->snapshot_time);
103 	drm_printf(&p, "Snapshot time: %ptSp\n", &ts);
104 	ts = ktime_to_timespec64(ss->boot_time);
105 	drm_printf(&p, "Uptime: %ptSp\n", &ts);
106 	drm_printf(&p, "Process: %s [%d]\n", ss->process_name, ss->pid);
107 	xe_device_snapshot_print(xe, &p);
108 
109 	drm_printf(&p, "\n**** GT #%d ****\n", ss->gt->info.id);
110 	drm_printf(&p, "\tTile: %d\n", ss->gt->tile->id);
111 
112 	drm_puts(&p, "\n**** GuC Log ****\n");
113 	xe_guc_log_snapshot_print(ss->guc.log, &p);
114 	drm_puts(&p, "\n**** GuC CT ****\n");
115 	xe_guc_ct_snapshot_print(ss->guc.ct, &p);
116 
117 	drm_puts(&p, "\n**** Contexts ****\n");
118 	xe_guc_exec_queue_snapshot_print(ss->ge, &p);
119 
120 	drm_puts(&p, "\n**** Job ****\n");
121 	xe_sched_job_snapshot_print(ss->job, &p);
122 
123 	drm_puts(&p, "\n**** HW Engines ****\n");
124 	for (i = 0; i < XE_NUM_HW_ENGINES; i++)
125 		if (ss->hwe[i]) {
126 			xe_engine_snapshot_print(ss->hwe[i], &p);
127 			drm_puts(&p, "\n");
128 		}
129 
130 	drm_puts(&p, "\n**** VM state ****\n");
131 	xe_vm_snapshot_print(ss->vm, &p);
132 
133 	return count - iter.remain;
134 }
135 
136 static void xe_devcoredump_snapshot_free(struct xe_devcoredump_snapshot *ss)
137 {
138 	int i;
139 
140 	kfree(ss->reason);
141 	ss->reason = NULL;
142 
143 	xe_guc_log_snapshot_free(ss->guc.log);
144 	ss->guc.log = NULL;
145 
146 	xe_guc_ct_snapshot_free(ss->guc.ct);
147 	ss->guc.ct = NULL;
148 
149 	if (!IS_ERR_OR_NULL(ss->gt))
150 		xe_guc_capture_put_matched_nodes(&ss->gt->uc.guc);
151 	ss->matched_node = NULL;
152 
153 	xe_guc_exec_queue_snapshot_free(ss->ge);
154 	ss->ge = NULL;
155 
156 	xe_sched_job_snapshot_free(ss->job);
157 	ss->job = NULL;
158 
159 	for (i = 0; i < XE_NUM_HW_ENGINES; i++)
160 		if (ss->hwe[i]) {
161 			xe_hw_engine_snapshot_free(ss->hwe[i]);
162 			ss->hwe[i] = NULL;
163 		}
164 
165 	xe_vm_snapshot_free(ss->vm);
166 	ss->vm = NULL;
167 }
168 
169 #define XE_DEVCOREDUMP_CHUNK_MAX	(SZ_512M + SZ_1G)
170 
171 /**
172  * xe_devcoredump_read() - Read data from the Xe device coredump snapshot
173  * @buffer: Destination buffer to copy the coredump data into
174  * @offset: Offset in the coredump data to start reading from
175  * @count: Number of bytes to read
176  * @data: Pointer to the xe_devcoredump structure
177  * @datalen: Length of the data (unused)
178  *
179  * Reads a chunk of the coredump snapshot data into the provided buffer.
180  * If the devcoredump is smaller than 1.5 GB (XE_DEVCOREDUMP_CHUNK_MAX),
181  * it is read directly from a pre-written buffer. For larger devcoredumps,
182  * the pre-written buffer must be periodically repopulated from the snapshot
183  * state due to kmalloc size limitations.
184  *
185  * Return: Number of bytes copied on success, or a negative error code on failure.
186  */
187 static ssize_t xe_devcoredump_read(char *buffer, loff_t offset,
188 				   size_t count, void *data, size_t datalen)
189 {
190 	struct xe_devcoredump *coredump = data;
191 	struct xe_devcoredump_snapshot *ss;
192 	ssize_t byte_copied = 0;
193 	u32 chunk_offset;
194 	ssize_t new_chunk_position;
195 	bool pm_needed = false;
196 	int ret = 0;
197 
198 	if (!coredump)
199 		return -ENODEV;
200 
201 	ss = &coredump->snapshot;
202 
203 	/* Ensure delayed work is captured before continuing */
204 	flush_work(&ss->work);
205 
206 	pm_needed = ss->read.size > XE_DEVCOREDUMP_CHUNK_MAX;
207 	if (pm_needed)
208 		xe_pm_runtime_get(gt_to_xe(ss->gt));
209 
210 	mutex_lock(&coredump->lock);
211 
212 	if (!ss->read.buffer) {
213 		ret = -ENODEV;
214 		goto unlock;
215 	}
216 
217 	if (offset >= ss->read.size)
218 		goto unlock;
219 
220 	new_chunk_position = div_u64_rem(offset,
221 					 XE_DEVCOREDUMP_CHUNK_MAX,
222 					 &chunk_offset);
223 
224 	if (offset >= ss->read.chunk_position + XE_DEVCOREDUMP_CHUNK_MAX ||
225 	    offset < ss->read.chunk_position) {
226 		ss->read.chunk_position = new_chunk_position *
227 			XE_DEVCOREDUMP_CHUNK_MAX;
228 
229 		__xe_devcoredump_read(ss->read.buffer,
230 				      XE_DEVCOREDUMP_CHUNK_MAX,
231 				      ss->read.chunk_position, coredump);
232 	}
233 
234 	byte_copied = count < ss->read.size - offset ? count :
235 		ss->read.size - offset;
236 	memcpy(buffer, ss->read.buffer + chunk_offset, byte_copied);
237 
238 unlock:
239 	mutex_unlock(&coredump->lock);
240 
241 	if (pm_needed)
242 		xe_pm_runtime_put(gt_to_xe(ss->gt));
243 
244 	return byte_copied ? byte_copied : ret;
245 }
246 
247 static void xe_devcoredump_free(void *data)
248 {
249 	struct xe_devcoredump *coredump = data;
250 
251 	/* Our device is gone. Nothing to do... */
252 	if (!data || !coredump_to_xe(coredump))
253 		return;
254 
255 	if (coredump->captured)
256 		cancel_work_sync(&coredump->snapshot.work);
257 
258 	mutex_lock(&coredump->lock);
259 
260 	xe_devcoredump_snapshot_free(&coredump->snapshot);
261 	kvfree(coredump->snapshot.read.buffer);
262 
263 	/* To prevent stale data on next snapshot, clear everything */
264 	memset(&coredump->snapshot, 0, sizeof(coredump->snapshot));
265 	coredump->captured = false;
266 	drm_info(&coredump_to_xe(coredump)->drm,
267 		 "Xe device coredump has been deleted.\n");
268 
269 	mutex_unlock(&coredump->lock);
270 }
271 
272 static void xe_devcoredump_deferred_snap_work(struct work_struct *work)
273 {
274 	struct xe_devcoredump_snapshot *ss = container_of(work, typeof(*ss), work);
275 	struct xe_devcoredump *coredump = container_of(ss, typeof(*coredump), snapshot);
276 	struct xe_device *xe = coredump_to_xe(coredump);
277 
278 	/*
279 	 * NB: Despite passing a GFP_ flags parameter here, more allocations are done
280 	 * internally using GFP_KERNEL explicitly. Hence this call must be in the worker
281 	 * thread and not in the initial capture call.
282 	 */
283 	dev_coredumpm_timeout(gt_to_xe(ss->gt)->drm.dev, THIS_MODULE, coredump, 0, GFP_KERNEL,
284 			      xe_devcoredump_read, xe_devcoredump_free,
285 			      XE_COREDUMP_TIMEOUT_JIFFIES);
286 
287 	guard(xe_pm_runtime)(xe);
288 
289 	/* keep going if fw fails as we still want to save the memory and SW data */
290 	xe_with_force_wake(fw_ref, gt_to_fw(ss->gt), XE_FORCEWAKE_ALL) {
291 		if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FORCEWAKE_ALL))
292 			xe_gt_info(ss->gt, "failed to get forcewake for coredump capture\n");
293 		xe_vm_snapshot_capture_delayed(ss->vm);
294 		xe_guc_exec_queue_snapshot_capture_delayed(ss->ge);
295 	}
296 
297 	ss->read.chunk_position = 0;
298 
299 	/* Calculate devcoredump size */
300 	ss->read.size = __xe_devcoredump_read(NULL, LONG_MAX, 0, coredump);
301 
302 	if (ss->read.size > XE_DEVCOREDUMP_CHUNK_MAX) {
303 		ss->read.buffer = kvmalloc(XE_DEVCOREDUMP_CHUNK_MAX,
304 					   GFP_USER);
305 		if (!ss->read.buffer)
306 			return;
307 
308 		__xe_devcoredump_read(ss->read.buffer,
309 				      XE_DEVCOREDUMP_CHUNK_MAX,
310 				      0, coredump);
311 	} else {
312 		ss->read.buffer = kvmalloc(ss->read.size, GFP_USER);
313 		if (!ss->read.buffer)
314 			return;
315 
316 		__xe_devcoredump_read(ss->read.buffer, ss->read.size, 0,
317 				      coredump);
318 		xe_devcoredump_snapshot_free(ss);
319 	}
320 }
321 
322 static void devcoredump_snapshot(struct xe_devcoredump *coredump,
323 				 struct xe_gt *gt,
324 				 struct xe_exec_queue *q,
325 				 struct xe_sched_job *job)
326 {
327 	struct xe_devcoredump_snapshot *ss = &coredump->snapshot;
328 	struct xe_guc *guc = &gt->uc.guc;
329 	const char *process_name = "no process";
330 	bool cookie;
331 
332 	ss->snapshot_time = ktime_get_real();
333 	ss->boot_time = ktime_get_boottime();
334 
335 	if (q && q->vm && q->vm->xef) {
336 		process_name = q->vm->xef->process_name;
337 		ss->pid = q->vm->xef->pid;
338 	}
339 
340 	strscpy(ss->process_name, process_name);
341 
342 	ss->gt = gt;
343 	INIT_WORK(&ss->work, xe_devcoredump_deferred_snap_work);
344 
345 	/* keep going if fw fails as we still want to save the memory and SW data */
346 	CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FORCEWAKE_ALL);
347 
348 	cookie = dma_fence_begin_signalling();
349 
350 	ss->guc.log = xe_guc_log_snapshot_capture(&guc->log, true);
351 	ss->guc.ct = xe_guc_ct_snapshot_capture(&guc->ct);
352 
353 	if (q) {
354 		ss->ge = xe_guc_exec_queue_snapshot_capture(q);
355 		if (job)
356 			ss->job = xe_sched_job_snapshot_capture(job);
357 		ss->vm = xe_vm_snapshot_capture(q->vm);
358 
359 		xe_engine_snapshot_capture_for_queue(q);
360 	}
361 
362 	queue_work(system_dfl_wq, &ss->work);
363 
364 	dma_fence_end_signalling(cookie);
365 }
366 
367 /**
368  * __xe_devcoredump - Take the required snapshots and initialize coredump device.
369  * @gt: The GT where the issue was detected.
370  * @q: The faulty xe_exec_queue, where the issue was detected, may be NULL for
371  *     hangs that are not tied to an exec queue (e.g. TLB invalidation
372  *     timeouts); in that case only the GT-level state (GuC log and CT state)
373  *     is captured.
374  * @job: The faulty xe_sched_job, where the issue was detected, may be NULL.
375  * @fmt: Printf format + args to describe the reason for the core dump
376  *
377  * This function should be called at the crash time within the serialized
378  * gt_reset. It is skipped if we still have the core dump device available
379  * with the information of the 'first' snapshot.
380  */
381 __printf(4, 5)
382 void __xe_devcoredump(struct xe_gt *gt, struct xe_exec_queue *q,
383 		      struct xe_sched_job *job, const char *fmt, ...)
384 {
385 	struct xe_device *xe = gt_to_xe(gt);
386 	struct xe_devcoredump *coredump = &xe->devcoredump;
387 	va_list varg;
388 
389 	mutex_lock(&coredump->lock);
390 
391 	if (coredump->captured) {
392 		drm_dbg(&xe->drm, "Multiple hangs are occurring, but only the first snapshot was taken\n");
393 		mutex_unlock(&coredump->lock);
394 		return;
395 	}
396 
397 	coredump->captured = true;
398 
399 	va_start(varg, fmt);
400 	coredump->snapshot.reason = kvasprintf(GFP_ATOMIC, fmt, varg);
401 	va_end(varg);
402 
403 	devcoredump_snapshot(coredump, gt, q, job);
404 
405 	drm_info(&xe->drm, "Xe device coredump has been created\n");
406 	drm_info(&xe->drm, "Check your /sys/class/drm/card%d/device/devcoredump/data\n",
407 		 xe->drm.primary->index);
408 
409 	mutex_unlock(&coredump->lock);
410 }
411 
412 static void xe_driver_devcoredump_fini(void *arg)
413 {
414 	struct drm_device *drm = arg;
415 
416 	dev_coredump_put(drm->dev);
417 }
418 
419 int xe_devcoredump_init(struct xe_device *xe)
420 {
421 	int err;
422 
423 	err = drmm_mutex_init(&xe->drm, &xe->devcoredump.lock);
424 	if (err)
425 		return err;
426 
427 	if (IS_ENABLED(CONFIG_LOCKDEP)) {
428 		fs_reclaim_acquire(GFP_KERNEL);
429 		might_lock(&xe->devcoredump.lock);
430 		fs_reclaim_release(GFP_KERNEL);
431 	}
432 
433 	return devm_add_action_or_reset(xe->drm.dev, xe_driver_devcoredump_fini, &xe->drm);
434 }
435 
436 #endif
437 
438 /**
439  * xe_print_blob_ascii85 - print a BLOB to some useful location in ASCII85
440  *
441  * The output is split into multiple calls to drm_puts() because some print
442  * targets, e.g. dmesg, cannot handle arbitrarily long lines. These targets may
443  * add newlines, as is the case with dmesg: each drm_puts() call creates a
444  * separate line.
445  *
446  * There is also a scheduler yield call to prevent the 'task has been stuck for
447  * 120s' kernel hang check feature from firing when printing to a slow target
448  * such as dmesg over a serial port.
449  *
450  * @p: the printer object to output to
451  * @prefix: optional prefix to add to output string
452  * @suffix: optional suffix to add at the end. 0 disables it and is
453  *          not added to the output, which is useful when using multiple calls
454  *          to dump data to @p
455  * @blob: the Binary Large OBject to dump out
456  * @offset: offset in bytes to skip from the front of the BLOB, must be a multiple of sizeof(u32)
457  * @size: the size in bytes of the BLOB, must be a multiple of sizeof(u32)
458  */
459 void xe_print_blob_ascii85(struct drm_printer *p, const char *prefix, char suffix,
460 			   const void *blob, size_t offset, size_t size)
461 {
462 	const u32 *blob32 = (const u32 *)blob;
463 	char buff[ASCII85_BUFSZ], *line_buff;
464 	size_t line_pos = 0;
465 
466 #define DMESG_MAX_LINE_LEN	800
467 	/* Always leave space for the suffix char and the \0 */
468 #define MIN_SPACE		(ASCII85_BUFSZ + 2)	/* 85 + "<suffix>\0" */
469 
470 	if (size & 3)
471 		drm_printf(p, "Size not word aligned: %zu", size);
472 	if (offset & 3)
473 		drm_printf(p, "Offset not word aligned: %zu", offset);
474 
475 	line_buff = kzalloc(DMESG_MAX_LINE_LEN, GFP_ATOMIC);
476 	if (!line_buff) {
477 		drm_printf(p, "Failed to allocate line buffer\n");
478 		return;
479 	}
480 
481 	blob32 += offset / sizeof(*blob32);
482 	size /= sizeof(*blob32);
483 
484 	if (prefix) {
485 		strscpy(line_buff, prefix, DMESG_MAX_LINE_LEN - MIN_SPACE - 2);
486 		line_pos = strlen(line_buff);
487 
488 		line_buff[line_pos++] = ':';
489 		line_buff[line_pos++] = ' ';
490 	}
491 
492 	while (size--) {
493 		u32 val = *(blob32++);
494 
495 		strscpy(line_buff + line_pos, ascii85_encode(val, buff),
496 			DMESG_MAX_LINE_LEN - line_pos);
497 		line_pos += strlen(line_buff + line_pos);
498 
499 		if ((line_pos + MIN_SPACE) >= DMESG_MAX_LINE_LEN) {
500 			line_buff[line_pos++] = 0;
501 
502 			drm_puts(p, line_buff);
503 
504 			line_pos = 0;
505 
506 			/* Prevent 'stuck thread' time out errors */
507 			cond_resched();
508 		}
509 	}
510 
511 	if (suffix)
512 		line_buff[line_pos++] = suffix;
513 
514 	if (line_pos) {
515 		line_buff[line_pos++] = 0;
516 		drm_puts(p, line_buff);
517 	}
518 
519 	kfree(line_buff);
520 
521 #undef MIN_SPACE
522 #undef DMESG_MAX_LINE_LEN
523 }
524