xref: /openbmc/linux/lib/smp_processor_id.c (revision f28e4d21)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
239c715b7SIngo Molnar /*
339c715b7SIngo Molnar  * lib/smp_processor_id.c
439c715b7SIngo Molnar  *
539c715b7SIngo Molnar  * DEBUG_PREEMPT variant of smp_processor_id().
639c715b7SIngo Molnar  */
78bc3bcc9SPaul Gortmaker #include <linux/export.h>
8984640ceSMasami Hiramatsu #include <linux/kprobes.h>
94e57b681STim Schmielau #include <linux/sched.h>
1039c715b7SIngo Molnar 
11126f21f0SThomas Gleixner noinstr static
check_preemption_disabled(const char * what1,const char * what2)12984640ceSMasami Hiramatsu unsigned int check_preemption_disabled(const char *what1, const char *what2)
1339c715b7SIngo Molnar {
1439c715b7SIngo Molnar 	int this_cpu = raw_smp_processor_id();
1539c715b7SIngo Molnar 
164a2b4b22SPeter Zijlstra 	if (likely(preempt_count()))
1739c715b7SIngo Molnar 		goto out;
1839c715b7SIngo Molnar 
1939c715b7SIngo Molnar 	if (irqs_disabled())
2039c715b7SIngo Molnar 		goto out;
2139c715b7SIngo Molnar 
22570a752bSYejune Deng 	if (is_percpu_thread())
2339c715b7SIngo Molnar 		goto out;
2439c715b7SIngo Molnar 
2574d862b6SThomas Gleixner #ifdef CONFIG_SMP
26af449901SPeter Zijlstra 	if (current->migration_disabled)
27af449901SPeter Zijlstra 		goto out;
28af449901SPeter Zijlstra #endif
29af449901SPeter Zijlstra 
3039c715b7SIngo Molnar 	/*
3139c715b7SIngo Molnar 	 * It is valid to assume CPU-locality during early bootup:
3239c715b7SIngo Molnar 	 */
331c3c5eabSThomas Gleixner 	if (system_state < SYSTEM_SCHEDULING)
3439c715b7SIngo Molnar 		goto out;
3539c715b7SIngo Molnar 
3639c715b7SIngo Molnar 	/*
3739c715b7SIngo Molnar 	 * Avoid recursion:
3839c715b7SIngo Molnar 	 */
395568b139SSteven Rostedt 	preempt_disable_notrace();
4039c715b7SIngo Molnar 
41126f21f0SThomas Gleixner 	instrumentation_begin();
4239c715b7SIngo Molnar 	if (!printk_ratelimit())
4339c715b7SIngo Molnar 		goto out_enable;
4439c715b7SIngo Molnar 
45188a8140SChristoph Lameter 	printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
46188a8140SChristoph Lameter 		what1, what2, preempt_count() - 1, current->comm, current->pid);
47188a8140SChristoph Lameter 
48d202d47bSSergey Senozhatsky 	printk("caller is %pS\n", __builtin_return_address(0));
4939c715b7SIngo Molnar 	dump_stack();
5039c715b7SIngo Molnar 
5139c715b7SIngo Molnar out_enable:
52*f28e4d21STetsuo Handa 	instrumentation_end();
535568b139SSteven Rostedt 	preempt_enable_no_resched_notrace();
5439c715b7SIngo Molnar out:
5539c715b7SIngo Molnar 	return this_cpu;
5639c715b7SIngo Molnar }
5739c715b7SIngo Molnar 
debug_smp_processor_id(void)58126f21f0SThomas Gleixner noinstr unsigned int debug_smp_processor_id(void)
59188a8140SChristoph Lameter {
60188a8140SChristoph Lameter 	return check_preemption_disabled("smp_processor_id", "");
61188a8140SChristoph Lameter }
6239c715b7SIngo Molnar EXPORT_SYMBOL(debug_smp_processor_id);
6339c715b7SIngo Molnar 
__this_cpu_preempt_check(const char * op)64126f21f0SThomas Gleixner noinstr void __this_cpu_preempt_check(const char *op)
65188a8140SChristoph Lameter {
66188a8140SChristoph Lameter 	check_preemption_disabled("__this_cpu_", op);
67188a8140SChristoph Lameter }
68188a8140SChristoph Lameter EXPORT_SYMBOL(__this_cpu_preempt_check);
69