aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/trace/trace_seq.c
diff options
context:
space:
mode:
authorDmitry Torokhov <dmitry.torokhov@gmail.com>2015-02-10 14:35:36 -0500
committerDmitry Torokhov <dmitry.torokhov@gmail.com>2015-02-10 14:35:36 -0500
commit4ba24fef3eb3b142197135223b90ced2f319cd53 (patch)
treea20c125b27740ec7b4c761b11d801108e1b316b2 /kernel/trace/trace_seq.c
parent47c1ffb2b6b630894e9a16442611c056ab21c057 (diff)
parent98a4a59ee31a12105a2b84f5b8b515ac2cb208ef (diff)
Merge branch 'next' into for-linus
Prepare first round of input updates for 3.20.
Diffstat (limited to 'kernel/trace/trace_seq.c')
-rw-r--r--kernel/trace/trace_seq.c253
1 files changed, 101 insertions, 152 deletions
diff --git a/kernel/trace/trace_seq.c b/kernel/trace/trace_seq.c
index 1f24ed99dca2..f8b45d8792f9 100644
--- a/kernel/trace/trace_seq.c
+++ b/kernel/trace/trace_seq.c
@@ -27,10 +27,19 @@
27#include <linux/trace_seq.h> 27#include <linux/trace_seq.h>
28 28
29/* How much buffer is left on the trace_seq? */ 29/* How much buffer is left on the trace_seq? */
30#define TRACE_SEQ_BUF_LEFT(s) ((PAGE_SIZE - 1) - (s)->len) 30#define TRACE_SEQ_BUF_LEFT(s) seq_buf_buffer_left(&(s)->seq)
31 31
32/* How much buffer is written? */ 32/* How much buffer is written? */
33#define TRACE_SEQ_BUF_USED(s) min((s)->len, (unsigned int)(PAGE_SIZE - 1)) 33#define TRACE_SEQ_BUF_USED(s) seq_buf_used(&(s)->seq)
34
35/*
36 * trace_seq should work with being initialized with 0s.
37 */
38static inline void __trace_seq_init(struct trace_seq *s)
39{
40 if (unlikely(!s->seq.size))
41 trace_seq_init(s);
42}
34 43
35/** 44/**
36 * trace_print_seq - move the contents of trace_seq into a seq_file 45 * trace_print_seq - move the contents of trace_seq into a seq_file
@@ -43,10 +52,11 @@
43 */ 52 */
44int trace_print_seq(struct seq_file *m, struct trace_seq *s) 53int trace_print_seq(struct seq_file *m, struct trace_seq *s)
45{ 54{
46 unsigned int len = TRACE_SEQ_BUF_USED(s);
47 int ret; 55 int ret;
48 56
49 ret = seq_write(m, s->buffer, len); 57 __trace_seq_init(s);
58
59 ret = seq_buf_print_seq(m, &s->seq);
50 60
51 /* 61 /*
52 * Only reset this buffer if we successfully wrote to the 62 * Only reset this buffer if we successfully wrote to the
@@ -69,34 +79,26 @@ int trace_print_seq(struct seq_file *m, struct trace_seq *s)
69 * trace_seq_printf() is used to store strings into a special 79 * trace_seq_printf() is used to store strings into a special
70 * buffer (@s). Then the output may be either used by 80 * buffer (@s). Then the output may be either used by
71 * the sequencer or pulled into another buffer. 81 * the sequencer or pulled into another buffer.
72 *
73 * Returns 1 if we successfully written all the contents to
74 * the buffer.
75 * Returns 0 if we the length to write is bigger than the
76 * reserved buffer space. In this case, nothing gets written.
77 */ 82 */
78int trace_seq_printf(struct trace_seq *s, const char *fmt, ...) 83void trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
79{ 84{
80 unsigned int len = TRACE_SEQ_BUF_LEFT(s); 85 unsigned int save_len = s->seq.len;
81 va_list ap; 86 va_list ap;
82 int ret;
83 87
84 if (s->full || !len) 88 if (s->full)
85 return 0; 89 return;
90
91 __trace_seq_init(s);
86 92
87 va_start(ap, fmt); 93 va_start(ap, fmt);
88 ret = vsnprintf(s->buffer + s->len, len, fmt, ap); 94 seq_buf_vprintf(&s->seq, fmt, ap);
89 va_end(ap); 95 va_end(ap);
90 96
91 /* If we can't write it all, don't bother writing anything */ 97 /* If we can't write it all, don't bother writing anything */
92 if (ret >= len) { 98 if (unlikely(seq_buf_has_overflowed(&s->seq))) {
99 s->seq.len = save_len;
93 s->full = 1; 100 s->full = 1;
94 return 0;
95 } 101 }
96
97 s->len += ret;
98
99 return 1;
100} 102}
101EXPORT_SYMBOL_GPL(trace_seq_printf); 103EXPORT_SYMBOL_GPL(trace_seq_printf);
102 104
@@ -107,25 +109,23 @@ EXPORT_SYMBOL_GPL(trace_seq_printf);
107 * @nmaskbits: The number of bits that are valid in @maskp 109 * @nmaskbits: The number of bits that are valid in @maskp
108 * 110 *
109 * Writes a ASCII representation of a bitmask string into @s. 111 * Writes a ASCII representation of a bitmask string into @s.
110 *
111 * Returns 1 if we successfully written all the contents to
112 * the buffer.
113 * Returns 0 if we the length to write is bigger than the
114 * reserved buffer space. In this case, nothing gets written.
115 */ 112 */
116int trace_seq_bitmask(struct trace_seq *s, const unsigned long *maskp, 113void trace_seq_bitmask(struct trace_seq *s, const unsigned long *maskp,
117 int nmaskbits) 114 int nmaskbits)
118{ 115{
119 unsigned int len = TRACE_SEQ_BUF_LEFT(s); 116 unsigned int save_len = s->seq.len;
120 int ret;
121 117
122 if (s->full || !len) 118 if (s->full)
123 return 0; 119 return;
124 120
125 ret = bitmap_scnprintf(s->buffer, len, maskp, nmaskbits); 121 __trace_seq_init(s);
126 s->len += ret;
127 122
128 return 1; 123 seq_buf_bitmask(&s->seq, maskp, nmaskbits);
124
125 if (unlikely(seq_buf_has_overflowed(&s->seq))) {
126 s->seq.len = save_len;
127 s->full = 1;
128 }
129} 129}
130EXPORT_SYMBOL_GPL(trace_seq_bitmask); 130EXPORT_SYMBOL_GPL(trace_seq_bitmask);
131 131
@@ -139,28 +139,23 @@ EXPORT_SYMBOL_GPL(trace_seq_bitmask);
139 * trace_seq_printf is used to store strings into a special 139 * trace_seq_printf is used to store strings into a special
140 * buffer (@s). Then the output may be either used by 140 * buffer (@s). Then the output may be either used by
141 * the sequencer or pulled into another buffer. 141 * the sequencer or pulled into another buffer.
142 *
143 * Returns how much it wrote to the buffer.
144 */ 142 */
145int trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args) 143void trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
146{ 144{
147 unsigned int len = TRACE_SEQ_BUF_LEFT(s); 145 unsigned int save_len = s->seq.len;
148 int ret;
149 146
150 if (s->full || !len) 147 if (s->full)
151 return 0; 148 return;
152 149
153 ret = vsnprintf(s->buffer + s->len, len, fmt, args); 150 __trace_seq_init(s);
151
152 seq_buf_vprintf(&s->seq, fmt, args);
154 153
155 /* If we can't write it all, don't bother writing anything */ 154 /* If we can't write it all, don't bother writing anything */
156 if (ret >= len) { 155 if (unlikely(seq_buf_has_overflowed(&s->seq))) {
156 s->seq.len = save_len;
157 s->full = 1; 157 s->full = 1;
158 return 0;
159 } 158 }
160
161 s->len += ret;
162
163 return len;
164} 159}
165EXPORT_SYMBOL_GPL(trace_seq_vprintf); 160EXPORT_SYMBOL_GPL(trace_seq_vprintf);
166 161
@@ -178,28 +173,24 @@ EXPORT_SYMBOL_GPL(trace_seq_vprintf);
178 * 173 *
179 * This function will take the format and the binary array and finish 174 * This function will take the format and the binary array and finish
180 * the conversion into the ASCII string within the buffer. 175 * the conversion into the ASCII string within the buffer.
181 *
182 * Returns how much it wrote to the buffer.
183 */ 176 */
184int trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary) 177void trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary)
185{ 178{
186 unsigned int len = TRACE_SEQ_BUF_LEFT(s); 179 unsigned int save_len = s->seq.len;
187 int ret;
188 180
189 if (s->full || !len) 181 if (s->full)
190 return 0; 182 return;
183
184 __trace_seq_init(s);
191 185
192 ret = bstr_printf(s->buffer + s->len, len, fmt, binary); 186 seq_buf_bprintf(&s->seq, fmt, binary);
193 187
194 /* If we can't write it all, don't bother writing anything */ 188 /* If we can't write it all, don't bother writing anything */
195 if (ret >= len) { 189 if (unlikely(seq_buf_has_overflowed(&s->seq))) {
190 s->seq.len = save_len;
196 s->full = 1; 191 s->full = 1;
197 return 0; 192 return;
198 } 193 }
199
200 s->len += ret;
201
202 return len;
203} 194}
204EXPORT_SYMBOL_GPL(trace_seq_bprintf); 195EXPORT_SYMBOL_GPL(trace_seq_bprintf);
205 196
@@ -212,25 +203,22 @@ EXPORT_SYMBOL_GPL(trace_seq_bprintf);
212 * copy to user routines. This function records a simple string 203 * copy to user routines. This function records a simple string
213 * into a special buffer (@s) for later retrieval by a sequencer 204 * into a special buffer (@s) for later retrieval by a sequencer
214 * or other mechanism. 205 * or other mechanism.
215 *
216 * Returns how much it wrote to the buffer.
217 */ 206 */
218int trace_seq_puts(struct trace_seq *s, const char *str) 207void trace_seq_puts(struct trace_seq *s, const char *str)
219{ 208{
220 unsigned int len = strlen(str); 209 unsigned int len = strlen(str);
221 210
222 if (s->full) 211 if (s->full)
223 return 0; 212 return;
213
214 __trace_seq_init(s);
224 215
225 if (len > TRACE_SEQ_BUF_LEFT(s)) { 216 if (len > TRACE_SEQ_BUF_LEFT(s)) {
226 s->full = 1; 217 s->full = 1;
227 return 0; 218 return;
228 } 219 }
229 220
230 memcpy(s->buffer + s->len, str, len); 221 seq_buf_putmem(&s->seq, str, len);
231 s->len += len;
232
233 return len;
234} 222}
235EXPORT_SYMBOL_GPL(trace_seq_puts); 223EXPORT_SYMBOL_GPL(trace_seq_puts);
236 224
@@ -243,22 +231,20 @@ EXPORT_SYMBOL_GPL(trace_seq_puts);
243 * copy to user routines. This function records a simple charater 231 * copy to user routines. This function records a simple charater
244 * into a special buffer (@s) for later retrieval by a sequencer 232 * into a special buffer (@s) for later retrieval by a sequencer
245 * or other mechanism. 233 * or other mechanism.
246 *
247 * Returns how much it wrote to the buffer.
248 */ 234 */
249int trace_seq_putc(struct trace_seq *s, unsigned char c) 235void trace_seq_putc(struct trace_seq *s, unsigned char c)
250{ 236{
251 if (s->full) 237 if (s->full)
252 return 0; 238 return;
239
240 __trace_seq_init(s);
253 241
254 if (TRACE_SEQ_BUF_LEFT(s) < 1) { 242 if (TRACE_SEQ_BUF_LEFT(s) < 1) {
255 s->full = 1; 243 s->full = 1;
256 return 0; 244 return;
257 } 245 }
258 246
259 s->buffer[s->len++] = c; 247 seq_buf_putc(&s->seq, c);
260
261 return 1;
262} 248}
263EXPORT_SYMBOL_GPL(trace_seq_putc); 249EXPORT_SYMBOL_GPL(trace_seq_putc);
264 250
@@ -271,29 +257,23 @@ EXPORT_SYMBOL_GPL(trace_seq_putc);
271 * There may be cases where raw memory needs to be written into the 257 * There may be cases where raw memory needs to be written into the
272 * buffer and a strcpy() would not work. Using this function allows 258 * buffer and a strcpy() would not work. Using this function allows
273 * for such cases. 259 * for such cases.
274 *
275 * Returns how much it wrote to the buffer.
276 */ 260 */
277int trace_seq_putmem(struct trace_seq *s, const void *mem, unsigned int len) 261void trace_seq_putmem(struct trace_seq *s, const void *mem, unsigned int len)
278{ 262{
279 if (s->full) 263 if (s->full)
280 return 0; 264 return;
265
266 __trace_seq_init(s);
281 267
282 if (len > TRACE_SEQ_BUF_LEFT(s)) { 268 if (len > TRACE_SEQ_BUF_LEFT(s)) {
283 s->full = 1; 269 s->full = 1;
284 return 0; 270 return;
285 } 271 }
286 272
287 memcpy(s->buffer + s->len, mem, len); 273 seq_buf_putmem(&s->seq, mem, len);
288 s->len += len;
289
290 return len;
291} 274}
292EXPORT_SYMBOL_GPL(trace_seq_putmem); 275EXPORT_SYMBOL_GPL(trace_seq_putmem);
293 276
294#define MAX_MEMHEX_BYTES 8U
295#define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
296
297/** 277/**
298 * trace_seq_putmem_hex - write raw memory into the buffer in ASCII hex 278 * trace_seq_putmem_hex - write raw memory into the buffer in ASCII hex
299 * @s: trace sequence descriptor 279 * @s: trace sequence descriptor
@@ -303,41 +283,31 @@ EXPORT_SYMBOL_GPL(trace_seq_putmem);
303 * This is similar to trace_seq_putmem() except instead of just copying the 283 * This is similar to trace_seq_putmem() except instead of just copying the
304 * raw memory into the buffer it writes its ASCII representation of it 284 * raw memory into the buffer it writes its ASCII representation of it
305 * in hex characters. 285 * in hex characters.
306 *
307 * Returns how much it wrote to the buffer.
308 */ 286 */
309int trace_seq_putmem_hex(struct trace_seq *s, const void *mem, 287void trace_seq_putmem_hex(struct trace_seq *s, const void *mem,
310 unsigned int len) 288 unsigned int len)
311{ 289{
312 unsigned char hex[HEX_CHARS]; 290 unsigned int save_len = s->seq.len;
313 const unsigned char *data = mem;
314 unsigned int start_len;
315 int i, j;
316 int cnt = 0;
317 291
318 if (s->full) 292 if (s->full)
319 return 0; 293 return;
320 294
321 while (len) { 295 __trace_seq_init(s);
322 start_len = min(len, HEX_CHARS - 1); 296
323#ifdef __BIG_ENDIAN 297 /* Each byte is represented by two chars */
324 for (i = 0, j = 0; i < start_len; i++) { 298 if (len * 2 > TRACE_SEQ_BUF_LEFT(s)) {
325#else 299 s->full = 1;
326 for (i = start_len-1, j = 0; i >= 0; i--) { 300 return;
327#endif 301 }
328 hex[j++] = hex_asc_hi(data[i]); 302
329 hex[j++] = hex_asc_lo(data[i]); 303 /* The added spaces can still cause an overflow */
330 } 304 seq_buf_putmem_hex(&s->seq, mem, len);
331 if (WARN_ON_ONCE(j == 0 || j/2 > len)) 305
332 break; 306 if (unlikely(seq_buf_has_overflowed(&s->seq))) {
333 307 s->seq.len = save_len;
334 /* j increments twice per loop */ 308 s->full = 1;
335 len -= j / 2; 309 return;
336 hex[j++] = ' ';
337
338 cnt += trace_seq_putmem(s, hex, j);
339 } 310 }
340 return cnt;
341} 311}
342EXPORT_SYMBOL_GPL(trace_seq_putmem_hex); 312EXPORT_SYMBOL_GPL(trace_seq_putmem_hex);
343 313
@@ -355,30 +325,27 @@ EXPORT_SYMBOL_GPL(trace_seq_putmem_hex);
355 */ 325 */
356int trace_seq_path(struct trace_seq *s, const struct path *path) 326int trace_seq_path(struct trace_seq *s, const struct path *path)
357{ 327{
358 unsigned char *p; 328 unsigned int save_len = s->seq.len;
359 329
360 if (s->full) 330 if (s->full)
361 return 0; 331 return 0;
362 332
333 __trace_seq_init(s);
334
363 if (TRACE_SEQ_BUF_LEFT(s) < 1) { 335 if (TRACE_SEQ_BUF_LEFT(s) < 1) {
364 s->full = 1; 336 s->full = 1;
365 return 0; 337 return 0;
366 } 338 }
367 339
368 p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len); 340 seq_buf_path(&s->seq, path, "\n");
369 if (!IS_ERR(p)) { 341
370 p = mangle_path(s->buffer + s->len, p, "\n"); 342 if (unlikely(seq_buf_has_overflowed(&s->seq))) {
371 if (p) { 343 s->seq.len = save_len;
372 s->len = p - s->buffer; 344 s->full = 1;
373 return 1; 345 return 0;
374 }
375 } else {
376 s->buffer[s->len++] = '?';
377 return 1;
378 } 346 }
379 347
380 s->full = 1; 348 return 1;
381 return 0;
382} 349}
383EXPORT_SYMBOL_GPL(trace_seq_path); 350EXPORT_SYMBOL_GPL(trace_seq_path);
384 351
@@ -404,25 +371,7 @@ EXPORT_SYMBOL_GPL(trace_seq_path);
404 */ 371 */
405int trace_seq_to_user(struct trace_seq *s, char __user *ubuf, int cnt) 372int trace_seq_to_user(struct trace_seq *s, char __user *ubuf, int cnt)
406{ 373{
407 int len; 374 __trace_seq_init(s);
408 int ret; 375 return seq_buf_to_user(&s->seq, ubuf, cnt);
409
410 if (!cnt)
411 return 0;
412
413 if (s->len <= s->readpos)
414 return -EBUSY;
415
416 len = s->len - s->readpos;
417 if (cnt > len)
418 cnt = len;
419 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
420 if (ret == cnt)
421 return -EFAULT;
422
423 cnt -= ret;
424
425 s->readpos += cnt;
426 return cnt;
427} 376}
428EXPORT_SYMBOL_GPL(trace_seq_to_user); 377EXPORT_SYMBOL_GPL(trace_seq_to_user);