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

stop_machine: Add function and caller debug info

Crashes in stop-machine are hard to connect to the calling code, add a
little something to help with that.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Valentin Schneider <valentin.schneider@arm.com>
Reviewed-by: Daniel Bristot de Oliveira <bristot@redhat.com>
Link: https://lkml.kernel.org/r/20201023102346.116513635@infradead.org

+32 -3
+5
include/linux/stop_machine.h
··· 24 24 struct cpu_stop_work { 25 25 struct list_head list; /* cpu_stopper->works */ 26 26 cpu_stop_fn_t fn; 27 + unsigned long caller; 27 28 void *arg; 28 29 struct cpu_stop_done *done; 29 30 }; ··· 36 35 void stop_machine_park(int cpu); 37 36 void stop_machine_unpark(int cpu); 38 37 void stop_machine_yield(const struct cpumask *cpumask); 38 + 39 + extern void print_stop_info(const char *log_lvl, struct task_struct *task); 39 40 40 41 #else /* CONFIG_SMP */ 41 42 ··· 82 79 83 80 return false; 84 81 } 82 + 83 + static inline void print_stop_info(const char *log_lvl, struct task_struct *task) { } 85 84 86 85 #endif /* CONFIG_SMP */ 87 86
+1
kernel/sched/core.c
··· 6447 6447 (unsigned long)task_thread_info(p)->flags); 6448 6448 6449 6449 print_worker_info(KERN_INFO, p); 6450 + print_stop_info(KERN_INFO, p); 6450 6451 show_stack(p, NULL, KERN_INFO); 6451 6452 put_task_stack(p); 6452 6453 }
+24 -3
kernel/stop_machine.c
··· 42 42 struct list_head works; /* list of pending works */ 43 43 44 44 struct cpu_stop_work stop_work; /* for stop_cpus */ 45 + unsigned long caller; 46 + cpu_stop_fn_t fn; 45 47 }; 46 48 47 49 static DEFINE_PER_CPU(struct cpu_stopper, cpu_stopper); 48 50 static bool stop_machine_initialized = false; 51 + 52 + void print_stop_info(const char *log_lvl, struct task_struct *task) 53 + { 54 + /* 55 + * If @task is a stopper task, it cannot migrate and task_cpu() is 56 + * stable. 57 + */ 58 + struct cpu_stopper *stopper = per_cpu_ptr(&cpu_stopper, task_cpu(task)); 59 + 60 + if (task != stopper->thread) 61 + return; 62 + 63 + printk("%sStopper: %pS <- %pS\n", log_lvl, stopper->fn, (void *)stopper->caller); 64 + } 49 65 50 66 /* static data for stop_cpus */ 51 67 static DEFINE_MUTEX(stop_cpus_mutex); ··· 139 123 int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg) 140 124 { 141 125 struct cpu_stop_done done; 142 - struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done }; 126 + struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done, .caller = _RET_IP_ }; 143 127 144 128 cpu_stop_init_done(&done, 1); 145 129 if (!cpu_stop_queue_work(cpu, &work)) ··· 347 331 work1 = work2 = (struct cpu_stop_work){ 348 332 .fn = multi_cpu_stop, 349 333 .arg = &msdata, 350 - .done = &done 334 + .done = &done, 335 + .caller = _RET_IP_, 351 336 }; 352 337 353 338 cpu_stop_init_done(&done, 2); ··· 384 367 bool stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg, 385 368 struct cpu_stop_work *work_buf) 386 369 { 387 - *work_buf = (struct cpu_stop_work){ .fn = fn, .arg = arg, }; 370 + *work_buf = (struct cpu_stop_work){ .fn = fn, .arg = arg, .caller = _RET_IP_, }; 388 371 return cpu_stop_queue_work(cpu, work_buf); 389 372 } 390 373 ··· 504 487 int ret; 505 488 506 489 /* cpu stop callbacks must not sleep, make in_atomic() == T */ 490 + stopper->caller = work->caller; 491 + stopper->fn = fn; 507 492 preempt_count_inc(); 508 493 ret = fn(arg); 509 494 if (done) { ··· 514 495 cpu_stop_signal_done(done); 515 496 } 516 497 preempt_count_dec(); 498 + stopper->fn = NULL; 499 + stopper->caller = 0; 517 500 WARN_ONCE(preempt_count(), 518 501 "cpu_stop: %ps(%p) leaked preempt count\n", fn, arg); 519 502 goto repeat;
+2
lib/dump_stack.c
··· 12 12 #include <linux/atomic.h> 13 13 #include <linux/kexec.h> 14 14 #include <linux/utsname.h> 15 + #include <linux/stop_machine.h> 15 16 16 17 static char dump_stack_arch_desc_str[128]; 17 18 ··· 58 57 log_lvl, dump_stack_arch_desc_str); 59 58 60 59 print_worker_info(log_lvl, current); 60 + print_stop_info(log_lvl, current); 61 61 } 62 62 63 63 /**