Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

tracing: Move trace_printk functions out of trace.c and into trace_printk.c

The file trace.c has become a catchall for most things tracing. Start
making it smaller by breaking out various aspects into their own files.

Move the functions associated to the trace_printk operations out of trace.c and
into trace_printk.c.

Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Link: https://patch.msgid.link/20260208032450.828744197@kernel.org
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

+432 -431
-431
kernel/trace/trace.c
··· 539 539 /* List of trace_arrays interested in the top level trace_marker */ 540 540 static LIST_HEAD(marker_copies); 541 541 542 - static __always_inline bool printk_binsafe(struct trace_array *tr) 543 - { 544 - /* 545 - * The binary format of traceprintk can cause a crash if used 546 - * by a buffer from another boot. Force the use of the 547 - * non binary version of trace_printk if the trace_printk 548 - * buffer is a boot mapped ring buffer. 549 - */ 550 - return !(tr->flags & TRACE_ARRAY_FL_BOOT); 551 - } 552 - 553 542 static void update_printk_trace(struct trace_array *tr) 554 543 { 555 544 if (printk_trace == tr) ··· 1047 1058 tracer_tracing_on(&global_trace); 1048 1059 } 1049 1060 EXPORT_SYMBOL_GPL(tracing_on); 1050 - 1051 - int __trace_array_puts(struct trace_array *tr, unsigned long ip, 1052 - const char *str, int size) 1053 - { 1054 - struct ring_buffer_event *event; 1055 - struct trace_buffer *buffer; 1056 - struct print_entry *entry; 1057 - unsigned int trace_ctx; 1058 - int alloc; 1059 - 1060 - if (!(tr->trace_flags & TRACE_ITER(PRINTK))) 1061 - return 0; 1062 - 1063 - if (unlikely(tracing_selftest_running && 1064 - (tr->flags & TRACE_ARRAY_FL_GLOBAL))) 1065 - return 0; 1066 - 1067 - if (unlikely(tracing_disabled)) 1068 - return 0; 1069 - 1070 - alloc = sizeof(*entry) + size + 2; /* possible \n added */ 1071 - 1072 - trace_ctx = tracing_gen_ctx(); 1073 - buffer = tr->array_buffer.buffer; 1074 - guard(ring_buffer_nest)(buffer); 1075 - event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc, 1076 - trace_ctx); 1077 - if (!event) 1078 - return 0; 1079 - 1080 - entry = ring_buffer_event_data(event); 1081 - entry->ip = ip; 1082 - 1083 - memcpy(&entry->buf, str, size); 1084 - 1085 - /* Add a newline if necessary */ 1086 - if (entry->buf[size - 1] != '\n') { 1087 - entry->buf[size] = '\n'; 1088 - entry->buf[size + 1] = '\0'; 1089 - } else 1090 - entry->buf[size] = '\0'; 1091 - 1092 - __buffer_unlock_commit(buffer, event); 1093 - ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL); 1094 - return size; 1095 - } 1096 - EXPORT_SYMBOL_GPL(__trace_array_puts); 1097 - 1098 - /** 1099 - * __trace_puts - write a constant string into the trace buffer. 1100 - * @ip: The address of the caller 1101 - * @str: The constant string to write 1102 - * @size: The size of the string. 1103 - */ 1104 - int __trace_puts(unsigned long ip, const char *str, int size) 1105 - { 1106 - return __trace_array_puts(printk_trace, ip, str, size); 1107 - } 1108 - EXPORT_SYMBOL_GPL(__trace_puts); 1109 - 1110 - /** 1111 - * __trace_bputs - write the pointer to a constant string into trace buffer 1112 - * @ip: The address of the caller 1113 - * @str: The constant string to write to the buffer to 1114 - */ 1115 - int __trace_bputs(unsigned long ip, const char *str) 1116 - { 1117 - struct trace_array *tr = READ_ONCE(printk_trace); 1118 - struct ring_buffer_event *event; 1119 - struct trace_buffer *buffer; 1120 - struct bputs_entry *entry; 1121 - unsigned int trace_ctx; 1122 - int size = sizeof(struct bputs_entry); 1123 - 1124 - if (!printk_binsafe(tr)) 1125 - return __trace_puts(ip, str, strlen(str)); 1126 - 1127 - if (!(tr->trace_flags & TRACE_ITER(PRINTK))) 1128 - return 0; 1129 - 1130 - if (unlikely(tracing_selftest_running || tracing_disabled)) 1131 - return 0; 1132 - 1133 - trace_ctx = tracing_gen_ctx(); 1134 - buffer = tr->array_buffer.buffer; 1135 - 1136 - guard(ring_buffer_nest)(buffer); 1137 - event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size, 1138 - trace_ctx); 1139 - if (!event) 1140 - return 0; 1141 - 1142 - entry = ring_buffer_event_data(event); 1143 - entry->ip = ip; 1144 - entry->str = str; 1145 - 1146 - __buffer_unlock_commit(buffer, event); 1147 - ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL); 1148 - 1149 - return 1; 1150 - } 1151 - EXPORT_SYMBOL_GPL(__trace_bputs); 1152 1061 1153 1062 #ifdef CONFIG_TRACER_SNAPSHOT 1154 1063 static void tracing_snapshot_instance_cond(struct trace_array *tr, ··· 3045 3158 3046 3159 __buffer_unlock_commit(buffer, event); 3047 3160 } 3048 - 3049 - /* created for use with alloc_percpu */ 3050 - struct trace_buffer_struct { 3051 - int nesting; 3052 - char buffer[4][TRACE_BUF_SIZE]; 3053 - }; 3054 - 3055 - static struct trace_buffer_struct __percpu *trace_percpu_buffer; 3056 - 3057 - /* 3058 - * This allows for lockless recording. If we're nested too deeply, then 3059 - * this returns NULL. 3060 - */ 3061 - static char *get_trace_buf(void) 3062 - { 3063 - struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer); 3064 - 3065 - if (!trace_percpu_buffer || buffer->nesting >= 4) 3066 - return NULL; 3067 - 3068 - buffer->nesting++; 3069 - 3070 - /* Interrupts must see nesting incremented before we use the buffer */ 3071 - barrier(); 3072 - return &buffer->buffer[buffer->nesting - 1][0]; 3073 - } 3074 - 3075 - static void put_trace_buf(void) 3076 - { 3077 - /* Don't let the decrement of nesting leak before this */ 3078 - barrier(); 3079 - this_cpu_dec(trace_percpu_buffer->nesting); 3080 - } 3081 - 3082 - static int alloc_percpu_trace_buffer(void) 3083 - { 3084 - struct trace_buffer_struct __percpu *buffers; 3085 - 3086 - if (trace_percpu_buffer) 3087 - return 0; 3088 - 3089 - buffers = alloc_percpu(struct trace_buffer_struct); 3090 - if (MEM_FAIL(!buffers, "Could not allocate percpu trace_printk buffer")) 3091 - return -ENOMEM; 3092 - 3093 - trace_percpu_buffer = buffers; 3094 - return 0; 3095 - } 3096 - 3097 - static int buffers_allocated; 3098 - 3099 - void trace_printk_init_buffers(void) 3100 - { 3101 - if (buffers_allocated) 3102 - return; 3103 - 3104 - if (alloc_percpu_trace_buffer()) 3105 - return; 3106 - 3107 - /* trace_printk() is for debug use only. Don't use it in production. */ 3108 - 3109 - pr_warn("\n"); 3110 - pr_warn("**********************************************************\n"); 3111 - pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); 3112 - pr_warn("** **\n"); 3113 - pr_warn("** trace_printk() being used. Allocating extra memory. **\n"); 3114 - pr_warn("** **\n"); 3115 - pr_warn("** This means that this is a DEBUG kernel and it is **\n"); 3116 - pr_warn("** unsafe for production use. **\n"); 3117 - pr_warn("** **\n"); 3118 - pr_warn("** If you see this message and you are not debugging **\n"); 3119 - pr_warn("** the kernel, report this immediately to your vendor! **\n"); 3120 - pr_warn("** **\n"); 3121 - pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); 3122 - pr_warn("**********************************************************\n"); 3123 - 3124 - /* Expand the buffers to set size */ 3125 - if (tracing_update_buffers(NULL) < 0) 3126 - pr_err("Failed to expand tracing buffers for trace_printk() calls\n"); 3127 - else 3128 - buffers_allocated = 1; 3129 - 3130 - /* 3131 - * trace_printk_init_buffers() can be called by modules. 3132 - * If that happens, then we need to start cmdline recording 3133 - * directly here. 3134 - */ 3135 - if (system_state == SYSTEM_RUNNING) 3136 - tracing_start_cmdline_record(); 3137 - } 3138 - EXPORT_SYMBOL_GPL(trace_printk_init_buffers); 3139 - 3140 - void trace_printk_start_comm(void) 3141 - { 3142 - /* Start tracing comms if trace printk is set */ 3143 - if (!buffers_allocated) 3144 - return; 3145 - tracing_start_cmdline_record(); 3146 - } 3147 - 3148 - static void trace_printk_start_stop_comm(int enabled) 3149 - { 3150 - if (!buffers_allocated) 3151 - return; 3152 - 3153 - if (enabled) 3154 - tracing_start_cmdline_record(); 3155 - else 3156 - tracing_stop_cmdline_record(); 3157 - } 3158 - 3159 - /** 3160 - * trace_vbprintk - write binary msg to tracing buffer 3161 - * @ip: The address of the caller 3162 - * @fmt: The string format to write to the buffer 3163 - * @args: Arguments for @fmt 3164 - */ 3165 - int trace_vbprintk(unsigned long ip, const char *fmt, va_list args) 3166 - { 3167 - struct ring_buffer_event *event; 3168 - struct trace_buffer *buffer; 3169 - struct trace_array *tr = READ_ONCE(printk_trace); 3170 - struct bprint_entry *entry; 3171 - unsigned int trace_ctx; 3172 - char *tbuffer; 3173 - int len = 0, size; 3174 - 3175 - if (!printk_binsafe(tr)) 3176 - return trace_vprintk(ip, fmt, args); 3177 - 3178 - if (unlikely(tracing_selftest_running || tracing_disabled)) 3179 - return 0; 3180 - 3181 - /* Don't pollute graph traces with trace_vprintk internals */ 3182 - pause_graph_tracing(); 3183 - 3184 - trace_ctx = tracing_gen_ctx(); 3185 - guard(preempt_notrace)(); 3186 - 3187 - tbuffer = get_trace_buf(); 3188 - if (!tbuffer) { 3189 - len = 0; 3190 - goto out_nobuffer; 3191 - } 3192 - 3193 - len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args); 3194 - 3195 - if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0) 3196 - goto out_put; 3197 - 3198 - size = sizeof(*entry) + sizeof(u32) * len; 3199 - buffer = tr->array_buffer.buffer; 3200 - scoped_guard(ring_buffer_nest, buffer) { 3201 - event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size, 3202 - trace_ctx); 3203 - if (!event) 3204 - goto out_put; 3205 - entry = ring_buffer_event_data(event); 3206 - entry->ip = ip; 3207 - entry->fmt = fmt; 3208 - 3209 - memcpy(entry->buf, tbuffer, sizeof(u32) * len); 3210 - __buffer_unlock_commit(buffer, event); 3211 - ftrace_trace_stack(tr, buffer, trace_ctx, 6, NULL); 3212 - } 3213 - out_put: 3214 - put_trace_buf(); 3215 - 3216 - out_nobuffer: 3217 - unpause_graph_tracing(); 3218 - 3219 - return len; 3220 - } 3221 - EXPORT_SYMBOL_GPL(trace_vbprintk); 3222 - 3223 - static __printf(3, 0) 3224 - int __trace_array_vprintk(struct trace_buffer *buffer, 3225 - unsigned long ip, const char *fmt, va_list args) 3226 - { 3227 - struct ring_buffer_event *event; 3228 - int len = 0, size; 3229 - struct print_entry *entry; 3230 - unsigned int trace_ctx; 3231 - char *tbuffer; 3232 - 3233 - if (unlikely(tracing_disabled)) 3234 - return 0; 3235 - 3236 - /* Don't pollute graph traces with trace_vprintk internals */ 3237 - pause_graph_tracing(); 3238 - 3239 - trace_ctx = tracing_gen_ctx(); 3240 - guard(preempt_notrace)(); 3241 - 3242 - 3243 - tbuffer = get_trace_buf(); 3244 - if (!tbuffer) { 3245 - len = 0; 3246 - goto out_nobuffer; 3247 - } 3248 - 3249 - len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args); 3250 - 3251 - size = sizeof(*entry) + len + 1; 3252 - scoped_guard(ring_buffer_nest, buffer) { 3253 - event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, 3254 - trace_ctx); 3255 - if (!event) 3256 - goto out; 3257 - entry = ring_buffer_event_data(event); 3258 - entry->ip = ip; 3259 - 3260 - memcpy(&entry->buf, tbuffer, len + 1); 3261 - __buffer_unlock_commit(buffer, event); 3262 - ftrace_trace_stack(printk_trace, buffer, trace_ctx, 6, NULL); 3263 - } 3264 - out: 3265 - put_trace_buf(); 3266 - 3267 - out_nobuffer: 3268 - unpause_graph_tracing(); 3269 - 3270 - return len; 3271 - } 3272 - 3273 - int trace_array_vprintk(struct trace_array *tr, 3274 - unsigned long ip, const char *fmt, va_list args) 3275 - { 3276 - if (tracing_selftest_running && (tr->flags & TRACE_ARRAY_FL_GLOBAL)) 3277 - return 0; 3278 - 3279 - return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args); 3280 - } 3281 - 3282 - /** 3283 - * trace_array_printk - Print a message to a specific instance 3284 - * @tr: The instance trace_array descriptor 3285 - * @ip: The instruction pointer that this is called from. 3286 - * @fmt: The format to print (printf format) 3287 - * 3288 - * If a subsystem sets up its own instance, they have the right to 3289 - * printk strings into their tracing instance buffer using this 3290 - * function. Note, this function will not write into the top level 3291 - * buffer (use trace_printk() for that), as writing into the top level 3292 - * buffer should only have events that can be individually disabled. 3293 - * trace_printk() is only used for debugging a kernel, and should not 3294 - * be ever incorporated in normal use. 3295 - * 3296 - * trace_array_printk() can be used, as it will not add noise to the 3297 - * top level tracing buffer. 3298 - * 3299 - * Note, trace_array_init_printk() must be called on @tr before this 3300 - * can be used. 3301 - */ 3302 - int trace_array_printk(struct trace_array *tr, 3303 - unsigned long ip, const char *fmt, ...) 3304 - { 3305 - int ret; 3306 - va_list ap; 3307 - 3308 - if (!tr) 3309 - return -ENOENT; 3310 - 3311 - /* This is only allowed for created instances */ 3312 - if (tr->flags & TRACE_ARRAY_FL_GLOBAL) 3313 - return 0; 3314 - 3315 - if (!(tr->trace_flags & TRACE_ITER(PRINTK))) 3316 - return 0; 3317 - 3318 - va_start(ap, fmt); 3319 - ret = trace_array_vprintk(tr, ip, fmt, ap); 3320 - va_end(ap); 3321 - return ret; 3322 - } 3323 - EXPORT_SYMBOL_GPL(trace_array_printk); 3324 - 3325 - /** 3326 - * trace_array_init_printk - Initialize buffers for trace_array_printk() 3327 - * @tr: The trace array to initialize the buffers for 3328 - * 3329 - * As trace_array_printk() only writes into instances, they are OK to 3330 - * have in the kernel (unlike trace_printk()). This needs to be called 3331 - * before trace_array_printk() can be used on a trace_array. 3332 - */ 3333 - int trace_array_init_printk(struct trace_array *tr) 3334 - { 3335 - if (!tr) 3336 - return -ENOENT; 3337 - 3338 - /* This is only allowed for created instances */ 3339 - if (tr->flags & TRACE_ARRAY_FL_GLOBAL) 3340 - return -EINVAL; 3341 - 3342 - return alloc_percpu_trace_buffer(); 3343 - } 3344 - EXPORT_SYMBOL_GPL(trace_array_init_printk); 3345 - 3346 - int trace_array_printk_buf(struct trace_buffer *buffer, 3347 - unsigned long ip, const char *fmt, ...) 3348 - { 3349 - int ret; 3350 - va_list ap; 3351 - 3352 - if (!(printk_trace->trace_flags & TRACE_ITER(PRINTK))) 3353 - return 0; 3354 - 3355 - va_start(ap, fmt); 3356 - ret = __trace_array_vprintk(buffer, ip, fmt, ap); 3357 - va_end(ap); 3358 - return ret; 3359 - } 3360 - 3361 - int trace_vprintk(unsigned long ip, const char *fmt, va_list args) 3362 - { 3363 - return trace_array_vprintk(printk_trace, ip, fmt, args); 3364 - } 3365 - EXPORT_SYMBOL_GPL(trace_vprintk); 3366 3161 3367 3162 static void trace_iterator_increment(struct trace_iterator *iter) 3368 3163 {
+1
kernel/trace/trace.h
··· 2131 2131 2132 2132 void trace_printk_control(bool enabled); 2133 2133 void trace_printk_start_comm(void); 2134 + void trace_printk_start_stop_comm(int enabled); 2134 2135 int trace_keep_overwrite(struct tracer *tracer, u64 mask, int set); 2135 2136 int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled); 2136 2137
+431
kernel/trace/trace_printk.c
··· 376 376 .release = seq_release, 377 377 }; 378 378 379 + static __always_inline bool printk_binsafe(struct trace_array *tr) 380 + { 381 + /* 382 + * The binary format of traceprintk can cause a crash if used 383 + * by a buffer from another boot. Force the use of the 384 + * non binary version of trace_printk if the trace_printk 385 + * buffer is a boot mapped ring buffer. 386 + */ 387 + return !(tr->flags & TRACE_ARRAY_FL_BOOT); 388 + } 389 + 390 + int __trace_array_puts(struct trace_array *tr, unsigned long ip, 391 + const char *str, int size) 392 + { 393 + struct ring_buffer_event *event; 394 + struct trace_buffer *buffer; 395 + struct print_entry *entry; 396 + unsigned int trace_ctx; 397 + int alloc; 398 + 399 + if (!(tr->trace_flags & TRACE_ITER(PRINTK))) 400 + return 0; 401 + 402 + if (unlikely(tracing_selftest_running && 403 + (tr->flags & TRACE_ARRAY_FL_GLOBAL))) 404 + return 0; 405 + 406 + if (unlikely(tracing_disabled)) 407 + return 0; 408 + 409 + alloc = sizeof(*entry) + size + 2; /* possible \n added */ 410 + 411 + trace_ctx = tracing_gen_ctx(); 412 + buffer = tr->array_buffer.buffer; 413 + guard(ring_buffer_nest)(buffer); 414 + event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc, 415 + trace_ctx); 416 + if (!event) 417 + return 0; 418 + 419 + entry = ring_buffer_event_data(event); 420 + entry->ip = ip; 421 + 422 + memcpy(&entry->buf, str, size); 423 + 424 + /* Add a newline if necessary */ 425 + if (entry->buf[size - 1] != '\n') { 426 + entry->buf[size] = '\n'; 427 + entry->buf[size + 1] = '\0'; 428 + } else 429 + entry->buf[size] = '\0'; 430 + 431 + __buffer_unlock_commit(buffer, event); 432 + ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL); 433 + return size; 434 + } 435 + EXPORT_SYMBOL_GPL(__trace_array_puts); 436 + 437 + /** 438 + * __trace_puts - write a constant string into the trace buffer. 439 + * @ip: The address of the caller 440 + * @str: The constant string to write 441 + * @size: The size of the string. 442 + */ 443 + int __trace_puts(unsigned long ip, const char *str, int size) 444 + { 445 + return __trace_array_puts(printk_trace, ip, str, size); 446 + } 447 + EXPORT_SYMBOL_GPL(__trace_puts); 448 + 449 + /** 450 + * __trace_bputs - write the pointer to a constant string into trace buffer 451 + * @ip: The address of the caller 452 + * @str: The constant string to write to the buffer to 453 + */ 454 + int __trace_bputs(unsigned long ip, const char *str) 455 + { 456 + struct trace_array *tr = READ_ONCE(printk_trace); 457 + struct ring_buffer_event *event; 458 + struct trace_buffer *buffer; 459 + struct bputs_entry *entry; 460 + unsigned int trace_ctx; 461 + int size = sizeof(struct bputs_entry); 462 + 463 + if (!printk_binsafe(tr)) 464 + return __trace_puts(ip, str, strlen(str)); 465 + 466 + if (!(tr->trace_flags & TRACE_ITER(PRINTK))) 467 + return 0; 468 + 469 + if (unlikely(tracing_selftest_running || tracing_disabled)) 470 + return 0; 471 + 472 + trace_ctx = tracing_gen_ctx(); 473 + buffer = tr->array_buffer.buffer; 474 + 475 + guard(ring_buffer_nest)(buffer); 476 + event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size, 477 + trace_ctx); 478 + if (!event) 479 + return 0; 480 + 481 + entry = ring_buffer_event_data(event); 482 + entry->ip = ip; 483 + entry->str = str; 484 + 485 + __buffer_unlock_commit(buffer, event); 486 + ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL); 487 + 488 + return 1; 489 + } 490 + EXPORT_SYMBOL_GPL(__trace_bputs); 491 + 492 + /* created for use with alloc_percpu */ 493 + struct trace_buffer_struct { 494 + int nesting; 495 + char buffer[4][TRACE_BUF_SIZE]; 496 + }; 497 + 498 + static struct trace_buffer_struct __percpu *trace_percpu_buffer; 499 + 500 + /* 501 + * This allows for lockless recording. If we're nested too deeply, then 502 + * this returns NULL. 503 + */ 504 + static char *get_trace_buf(void) 505 + { 506 + struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer); 507 + 508 + if (!trace_percpu_buffer || buffer->nesting >= 4) 509 + return NULL; 510 + 511 + buffer->nesting++; 512 + 513 + /* Interrupts must see nesting incremented before we use the buffer */ 514 + barrier(); 515 + return &buffer->buffer[buffer->nesting - 1][0]; 516 + } 517 + 518 + static void put_trace_buf(void) 519 + { 520 + /* Don't let the decrement of nesting leak before this */ 521 + barrier(); 522 + this_cpu_dec(trace_percpu_buffer->nesting); 523 + } 524 + 525 + static int alloc_percpu_trace_buffer(void) 526 + { 527 + struct trace_buffer_struct __percpu *buffers; 528 + 529 + if (trace_percpu_buffer) 530 + return 0; 531 + 532 + buffers = alloc_percpu(struct trace_buffer_struct); 533 + if (MEM_FAIL(!buffers, "Could not allocate percpu trace_printk buffer")) 534 + return -ENOMEM; 535 + 536 + trace_percpu_buffer = buffers; 537 + return 0; 538 + } 539 + 540 + static int buffers_allocated; 541 + 542 + void trace_printk_init_buffers(void) 543 + { 544 + if (buffers_allocated) 545 + return; 546 + 547 + if (alloc_percpu_trace_buffer()) 548 + return; 549 + 550 + /* trace_printk() is for debug use only. Don't use it in production. */ 551 + 552 + pr_warn("\n"); 553 + pr_warn("**********************************************************\n"); 554 + pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); 555 + pr_warn("** **\n"); 556 + pr_warn("** trace_printk() being used. Allocating extra memory. **\n"); 557 + pr_warn("** **\n"); 558 + pr_warn("** This means that this is a DEBUG kernel and it is **\n"); 559 + pr_warn("** unsafe for production use. **\n"); 560 + pr_warn("** **\n"); 561 + pr_warn("** If you see this message and you are not debugging **\n"); 562 + pr_warn("** the kernel, report this immediately to your vendor! **\n"); 563 + pr_warn("** **\n"); 564 + pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); 565 + pr_warn("**********************************************************\n"); 566 + 567 + /* Expand the buffers to set size */ 568 + if (tracing_update_buffers(NULL) < 0) 569 + pr_err("Failed to expand tracing buffers for trace_printk() calls\n"); 570 + else 571 + buffers_allocated = 1; 572 + 573 + /* 574 + * trace_printk_init_buffers() can be called by modules. 575 + * If that happens, then we need to start cmdline recording 576 + * directly here. 577 + */ 578 + if (system_state == SYSTEM_RUNNING) 579 + tracing_start_cmdline_record(); 580 + } 581 + EXPORT_SYMBOL_GPL(trace_printk_init_buffers); 582 + 583 + void trace_printk_start_comm(void) 584 + { 585 + /* Start tracing comms if trace printk is set */ 586 + if (!buffers_allocated) 587 + return; 588 + tracing_start_cmdline_record(); 589 + } 590 + 591 + void trace_printk_start_stop_comm(int enabled) 592 + { 593 + if (!buffers_allocated) 594 + return; 595 + 596 + if (enabled) 597 + tracing_start_cmdline_record(); 598 + else 599 + tracing_stop_cmdline_record(); 600 + } 601 + 602 + /** 603 + * trace_vbprintk - write binary msg to tracing buffer 604 + * @ip: The address of the caller 605 + * @fmt: The string format to write to the buffer 606 + * @args: Arguments for @fmt 607 + */ 608 + int trace_vbprintk(unsigned long ip, const char *fmt, va_list args) 609 + { 610 + struct ring_buffer_event *event; 611 + struct trace_buffer *buffer; 612 + struct trace_array *tr = READ_ONCE(printk_trace); 613 + struct bprint_entry *entry; 614 + unsigned int trace_ctx; 615 + char *tbuffer; 616 + int len = 0, size; 617 + 618 + if (!printk_binsafe(tr)) 619 + return trace_vprintk(ip, fmt, args); 620 + 621 + if (unlikely(tracing_selftest_running || tracing_disabled)) 622 + return 0; 623 + 624 + /* Don't pollute graph traces with trace_vprintk internals */ 625 + pause_graph_tracing(); 626 + 627 + trace_ctx = tracing_gen_ctx(); 628 + guard(preempt_notrace)(); 629 + 630 + tbuffer = get_trace_buf(); 631 + if (!tbuffer) { 632 + len = 0; 633 + goto out_nobuffer; 634 + } 635 + 636 + len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args); 637 + 638 + if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0) 639 + goto out_put; 640 + 641 + size = sizeof(*entry) + sizeof(u32) * len; 642 + buffer = tr->array_buffer.buffer; 643 + scoped_guard(ring_buffer_nest, buffer) { 644 + event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size, 645 + trace_ctx); 646 + if (!event) 647 + goto out_put; 648 + entry = ring_buffer_event_data(event); 649 + entry->ip = ip; 650 + entry->fmt = fmt; 651 + 652 + memcpy(entry->buf, tbuffer, sizeof(u32) * len); 653 + __buffer_unlock_commit(buffer, event); 654 + ftrace_trace_stack(tr, buffer, trace_ctx, 6, NULL); 655 + } 656 + out_put: 657 + put_trace_buf(); 658 + 659 + out_nobuffer: 660 + unpause_graph_tracing(); 661 + 662 + return len; 663 + } 664 + EXPORT_SYMBOL_GPL(trace_vbprintk); 665 + 666 + static __printf(3, 0) 667 + int __trace_array_vprintk(struct trace_buffer *buffer, 668 + unsigned long ip, const char *fmt, va_list args) 669 + { 670 + struct ring_buffer_event *event; 671 + int len = 0, size; 672 + struct print_entry *entry; 673 + unsigned int trace_ctx; 674 + char *tbuffer; 675 + 676 + if (unlikely(tracing_disabled)) 677 + return 0; 678 + 679 + /* Don't pollute graph traces with trace_vprintk internals */ 680 + pause_graph_tracing(); 681 + 682 + trace_ctx = tracing_gen_ctx(); 683 + guard(preempt_notrace)(); 684 + 685 + 686 + tbuffer = get_trace_buf(); 687 + if (!tbuffer) { 688 + len = 0; 689 + goto out_nobuffer; 690 + } 691 + 692 + len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args); 693 + 694 + size = sizeof(*entry) + len + 1; 695 + scoped_guard(ring_buffer_nest, buffer) { 696 + event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, 697 + trace_ctx); 698 + if (!event) 699 + goto out; 700 + entry = ring_buffer_event_data(event); 701 + entry->ip = ip; 702 + 703 + memcpy(&entry->buf, tbuffer, len + 1); 704 + __buffer_unlock_commit(buffer, event); 705 + ftrace_trace_stack(printk_trace, buffer, trace_ctx, 6, NULL); 706 + } 707 + out: 708 + put_trace_buf(); 709 + 710 + out_nobuffer: 711 + unpause_graph_tracing(); 712 + 713 + return len; 714 + } 715 + 716 + int trace_array_vprintk(struct trace_array *tr, 717 + unsigned long ip, const char *fmt, va_list args) 718 + { 719 + if (tracing_selftest_running && (tr->flags & TRACE_ARRAY_FL_GLOBAL)) 720 + return 0; 721 + 722 + return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args); 723 + } 724 + 725 + /** 726 + * trace_array_printk - Print a message to a specific instance 727 + * @tr: The instance trace_array descriptor 728 + * @ip: The instruction pointer that this is called from. 729 + * @fmt: The format to print (printf format) 730 + * 731 + * If a subsystem sets up its own instance, they have the right to 732 + * printk strings into their tracing instance buffer using this 733 + * function. Note, this function will not write into the top level 734 + * buffer (use trace_printk() for that), as writing into the top level 735 + * buffer should only have events that can be individually disabled. 736 + * trace_printk() is only used for debugging a kernel, and should not 737 + * be ever incorporated in normal use. 738 + * 739 + * trace_array_printk() can be used, as it will not add noise to the 740 + * top level tracing buffer. 741 + * 742 + * Note, trace_array_init_printk() must be called on @tr before this 743 + * can be used. 744 + */ 745 + int trace_array_printk(struct trace_array *tr, 746 + unsigned long ip, const char *fmt, ...) 747 + { 748 + int ret; 749 + va_list ap; 750 + 751 + if (!tr) 752 + return -ENOENT; 753 + 754 + /* This is only allowed for created instances */ 755 + if (tr->flags & TRACE_ARRAY_FL_GLOBAL) 756 + return 0; 757 + 758 + if (!(tr->trace_flags & TRACE_ITER(PRINTK))) 759 + return 0; 760 + 761 + va_start(ap, fmt); 762 + ret = trace_array_vprintk(tr, ip, fmt, ap); 763 + va_end(ap); 764 + return ret; 765 + } 766 + EXPORT_SYMBOL_GPL(trace_array_printk); 767 + 768 + /** 769 + * trace_array_init_printk - Initialize buffers for trace_array_printk() 770 + * @tr: The trace array to initialize the buffers for 771 + * 772 + * As trace_array_printk() only writes into instances, they are OK to 773 + * have in the kernel (unlike trace_printk()). This needs to be called 774 + * before trace_array_printk() can be used on a trace_array. 775 + */ 776 + int trace_array_init_printk(struct trace_array *tr) 777 + { 778 + if (!tr) 779 + return -ENOENT; 780 + 781 + /* This is only allowed for created instances */ 782 + if (tr->flags & TRACE_ARRAY_FL_GLOBAL) 783 + return -EINVAL; 784 + 785 + return alloc_percpu_trace_buffer(); 786 + } 787 + EXPORT_SYMBOL_GPL(trace_array_init_printk); 788 + 789 + int trace_array_printk_buf(struct trace_buffer *buffer, 790 + unsigned long ip, const char *fmt, ...) 791 + { 792 + int ret; 793 + va_list ap; 794 + 795 + if (!(printk_trace->trace_flags & TRACE_ITER(PRINTK))) 796 + return 0; 797 + 798 + va_start(ap, fmt); 799 + ret = __trace_array_vprintk(buffer, ip, fmt, ap); 800 + va_end(ap); 801 + return ret; 802 + } 803 + 804 + int trace_vprintk(unsigned long ip, const char *fmt, va_list args) 805 + { 806 + return trace_array_vprintk(printk_trace, ip, fmt, args); 807 + } 808 + EXPORT_SYMBOL_GPL(trace_vprintk); 809 + 379 810 static __init int init_trace_printk_function_export(void) 380 811 { 381 812 int ret;