|
30 | 30 | #include <drm/drmP.h> |
31 | 31 | #include <drm/drm_print.h> |
32 | 32 |
|
33 | | -void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf) |
| 33 | +void __drm_puts_coredump(struct drm_printer *p, const char *str) |
34 | 34 | { |
35 | 35 | struct drm_print_iterator *iterator = p->arg; |
36 | 36 | ssize_t len; |
37 | 37 |
|
38 | 38 | if (!iterator->remain) |
39 | 39 | return; |
40 | 40 |
|
41 | | - /* Figure out how big the string will be */ |
42 | | - len = snprintf(NULL, 0, "%pV", vaf); |
43 | | - |
44 | 41 | if (iterator->offset < iterator->start) { |
45 | | - char *buf; |
46 | 42 | ssize_t copy; |
47 | 43 |
|
| 44 | + len = strlen(str); |
| 45 | + |
48 | 46 | if (iterator->offset + len <= iterator->start) { |
49 | 47 | iterator->offset += len; |
50 | 48 | return; |
51 | 49 | } |
52 | 50 |
|
53 | | - /* Print the string into a temporary buffer */ |
54 | | - buf = kmalloc(len + 1, |
55 | | - GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); |
56 | | - if (!buf) |
57 | | - return; |
58 | | - |
59 | | - snprintf(buf, len + 1, "%pV", vaf); |
60 | | - |
61 | 51 | copy = len - (iterator->start - iterator->offset); |
62 | 52 |
|
63 | 53 | if (copy > iterator->remain) |
64 | 54 | copy = iterator->remain; |
65 | 55 |
|
66 | 56 | /* Copy out the bit of the string that we need */ |
67 | 57 | memcpy(iterator->data, |
68 | | - buf + (iterator->start - iterator->offset), copy); |
| 58 | + str + (iterator->start - iterator->offset), copy); |
69 | 59 |
|
70 | 60 | iterator->offset = iterator->start + copy; |
71 | 61 | iterator->remain -= copy; |
72 | | - |
73 | | - kfree(buf); |
74 | 62 | } else { |
75 | | - char *buf; |
76 | 63 | ssize_t pos = iterator->offset - iterator->start; |
77 | 64 |
|
78 | | - if (len < iterator->remain) { |
79 | | - snprintf(((char *) iterator->data) + pos, |
80 | | - iterator->remain, "%pV", vaf); |
| 65 | + len = min_t(ssize_t, strlen(str), iterator->remain); |
81 | 66 |
|
82 | | - iterator->offset += len; |
83 | | - iterator->remain -= len; |
| 67 | + memcpy(iterator->data + pos, str, len); |
84 | 68 |
|
85 | | - return; |
86 | | - } |
| 69 | + iterator->offset += len; |
| 70 | + iterator->remain -= len; |
| 71 | + } |
| 72 | +} |
| 73 | +EXPORT_SYMBOL(__drm_puts_coredump); |
87 | 74 |
|
88 | | - /* Print the string into a temporary buffer */ |
89 | | - buf = kmalloc(len + 1, |
90 | | - GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); |
91 | | - if (!buf) |
92 | | - return; |
| 75 | +void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf) |
| 76 | +{ |
| 77 | + struct drm_print_iterator *iterator = p->arg; |
| 78 | + size_t len; |
| 79 | + char *buf; |
| 80 | + |
| 81 | + if (!iterator->remain) |
| 82 | + return; |
| 83 | + |
| 84 | + /* Figure out how big the string will be */ |
| 85 | + len = snprintf(NULL, 0, "%pV", vaf); |
| 86 | + |
| 87 | + /* This is the easiest path, we've already advanced beyond the offset */ |
| 88 | + if (iterator->offset + len <= iterator->start) { |
| 89 | + iterator->offset += len; |
| 90 | + return; |
| 91 | + } |
93 | 92 |
|
94 | | - snprintf(buf, len + 1, "%pV", vaf); |
| 93 | + /* Then check if we can directly copy into the target buffer */ |
| 94 | + if ((iterator->offset >= iterator->start) && (len < iterator->remain)) { |
| 95 | + ssize_t pos = iterator->offset - iterator->start; |
95 | 96 |
|
96 | | - /* Copy out the remaining bits */ |
97 | | - memcpy(iterator->data + pos, buf, iterator->remain); |
| 97 | + snprintf(((char *) iterator->data) + pos, |
| 98 | + iterator->remain, "%pV", vaf); |
98 | 99 |
|
99 | | - iterator->offset += iterator->remain; |
100 | | - iterator->remain = 0; |
| 100 | + iterator->offset += len; |
| 101 | + iterator->remain -= len; |
101 | 102 |
|
102 | | - kfree(buf); |
| 103 | + return; |
103 | 104 | } |
| 105 | + |
| 106 | + /* |
| 107 | + * Finally, hit the slow path and make a temporary string to copy over |
| 108 | + * using _drm_puts_coredump |
| 109 | + */ |
| 110 | + buf = kmalloc(len + 1, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); |
| 111 | + if (!buf) |
| 112 | + return; |
| 113 | + |
| 114 | + snprintf(buf, len + 1, "%pV", vaf); |
| 115 | + __drm_puts_coredump(p, (const char *) buf); |
| 116 | + |
| 117 | + kfree(buf); |
104 | 118 | } |
105 | 119 | EXPORT_SYMBOL(__drm_printfn_coredump); |
106 | 120 |
|
|
0 commit comments