Skip to content

Commit 5dc634b

Browse files
Jordan Crouserobclark
authored andcommitted
drm: Add puts callback for the coredump printer
Add a puts function for the coredump printer to bypass printf() for constant strings for a speed boost. Reorganize the coredump printf callback to share as much code as possible. v2: Try to reuse code between print and puts as suggested by Chris Wilson Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org> Signed-off-by: Rob Clark <robdclark@gmail.com>
1 parent 4538d73 commit 5dc634b

2 files changed

Lines changed: 51 additions & 35 deletions

File tree

‎drivers/gpu/drm/drm_print.c‎

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,77 +30,91 @@
3030
#include <drm/drmP.h>
3131
#include <drm/drm_print.h>
3232

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)
3434
{
3535
struct drm_print_iterator *iterator = p->arg;
3636
ssize_t len;
3737

3838
if (!iterator->remain)
3939
return;
4040

41-
/* Figure out how big the string will be */
42-
len = snprintf(NULL, 0, "%pV", vaf);
43-
4441
if (iterator->offset < iterator->start) {
45-
char *buf;
4642
ssize_t copy;
4743

44+
len = strlen(str);
45+
4846
if (iterator->offset + len <= iterator->start) {
4947
iterator->offset += len;
5048
return;
5149
}
5250

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-
6151
copy = len - (iterator->start - iterator->offset);
6252

6353
if (copy > iterator->remain)
6454
copy = iterator->remain;
6555

6656
/* Copy out the bit of the string that we need */
6757
memcpy(iterator->data,
68-
buf + (iterator->start - iterator->offset), copy);
58+
str + (iterator->start - iterator->offset), copy);
6959

7060
iterator->offset = iterator->start + copy;
7161
iterator->remain -= copy;
72-
73-
kfree(buf);
7462
} else {
75-
char *buf;
7663
ssize_t pos = iterator->offset - iterator->start;
7764

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);
8166

82-
iterator->offset += len;
83-
iterator->remain -= len;
67+
memcpy(iterator->data + pos, str, len);
8468

85-
return;
86-
}
69+
iterator->offset += len;
70+
iterator->remain -= len;
71+
}
72+
}
73+
EXPORT_SYMBOL(__drm_puts_coredump);
8774

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+
}
9392

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;
9596

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);
9899

99-
iterator->offset += iterator->remain;
100-
iterator->remain = 0;
100+
iterator->offset += len;
101+
iterator->remain -= len;
101102

102-
kfree(buf);
103+
return;
103104
}
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);
104118
}
105119
EXPORT_SYMBOL(__drm_printfn_coredump);
106120

‎include/drm/drm_print.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct drm_printer {
7575
};
7676

7777
void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf);
78+
void __drm_puts_coredump(struct drm_printer *p, const char *str);
7879
void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf);
7980
void __drm_puts_seq_file(struct drm_printer *p, const char *str);
8081
void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
@@ -163,6 +164,7 @@ drm_coredump_printer(struct drm_print_iterator *iter)
163164
{
164165
struct drm_printer p = {
165166
.printfn = __drm_printfn_coredump,
167+
.puts = __drm_puts_coredump,
166168
.arg = iter,
167169
};
168170

0 commit comments

Comments
 (0)