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