drm/printer: Allow NULL data in devcoredump printer

We want to determine the size of the devcoredump before writing it out.
To that end, we will run the devcoredump printer with NULL data to get
the size, alloc data based on the generated offset, then run the
devcorecump again with a valid data pointer to print.  This necessitates
not writing data to the data pointer on the initial pass, when it is
NULL.

v5:
 - Better commit message (Jonathan)
 - Add kerenl doc with examples (Jani)

Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Acked-by: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Signed-off-by: Matthew Brost <matthew.brost@intel.com>
Reviewed-by: Jonathan Cavitt <jonathan.cavitt@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20240801154118.2547543-3-matthew.brost@intel.com
This commit is contained in:
Matthew Brost 2024-08-01 08:41:17 -07:00
parent c3bc97d2f1
commit 53369581dc
2 changed files with 61 additions and 6 deletions

View file

@ -100,8 +100,9 @@ void __drm_puts_coredump(struct drm_printer *p, const char *str)
copy = iterator->remain;
/* Copy out the bit of the string that we need */
memcpy(iterator->data,
str + (iterator->start - iterator->offset), copy);
if (iterator->data)
memcpy(iterator->data,
str + (iterator->start - iterator->offset), copy);
iterator->offset = iterator->start + copy;
iterator->remain -= copy;
@ -110,7 +111,8 @@ void __drm_puts_coredump(struct drm_printer *p, const char *str)
len = min_t(ssize_t, strlen(str), iterator->remain);
memcpy(iterator->data + pos, str, len);
if (iterator->data)
memcpy(iterator->data + pos, str, len);
iterator->offset += len;
iterator->remain -= len;
@ -140,8 +142,9 @@ void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf)
if ((iterator->offset >= iterator->start) && (len < iterator->remain)) {
ssize_t pos = iterator->offset - iterator->start;
snprintf(((char *) iterator->data) + pos,
iterator->remain, "%pV", vaf);
if (iterator->data)
snprintf(((char *) iterator->data) + pos,
iterator->remain, "%pV", vaf);
iterator->offset += len;
iterator->remain -= len;

View file

@ -221,7 +221,8 @@ drm_vprintf(struct drm_printer *p, const char *fmt, va_list *va)
/**
* struct drm_print_iterator - local struct used with drm_printer_coredump
* @data: Pointer to the devcoredump output buffer
* @data: Pointer to the devcoredump output buffer, can be NULL if using
* drm_printer_coredump to determine size of devcoredump
* @start: The offset within the buffer to start writing
* @remain: The number of bytes to write for this iteration
*/
@ -266,6 +267,57 @@ struct drm_print_iterator {
* coredump_read, ...)
* }
*
* The above example has a time complexity of O(N^2), where N is the size of the
* devcoredump. This is acceptable for small devcoredumps but scales poorly for
* larger ones.
*
* Another use case for drm_coredump_printer is to capture the devcoredump into
* a saved buffer before the dev_coredump() callback. This involves two passes:
* one to determine the size of the devcoredump and another to print it to a
* buffer. Then, in dev_coredump(), copy from the saved buffer into the
* devcoredump read buffer.
*
* For example::
*
* char *devcoredump_saved_buffer;
*
* ssize_t __coredump_print(char *buffer, ssize_t count, ...)
* {
* struct drm_print_iterator iter;
* struct drm_printer p;
*
* iter.data = buffer;
* iter.start = 0;
* iter.remain = count;
*
* p = drm_coredump_printer(&iter);
*
* drm_printf(p, "foo=%d\n", foo);
* ...
* return count - iter.remain;
* }
*
* void coredump_print(...)
* {
* ssize_t count;
*
* count = __coredump_print(NULL, INT_MAX, ...);
* devcoredump_saved_buffer = kvmalloc(count, GFP_KERNEL);
* __coredump_print(devcoredump_saved_buffer, count, ...);
* }
*
* void coredump_read(char *buffer, loff_t offset, size_t count,
* void *data, size_t datalen)
* {
* ...
* memcpy(buffer, devcoredump_saved_buffer + offset, count);
* ...
* }
*
* The above example has a time complexity of O(N*2), where N is the size of the
* devcoredump. This scales better than the previous example for larger
* devcoredumps.
*
* RETURNS:
* The &drm_printer object
*/