b24413180f560 (Greg Kroah-Hartman 2017-11-01 15:07:57 +0100 1) // SPDX-License-Identifier: GPL-2.0
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 2) /*
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 3) * lib/smp_processor_id.c
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 4) *
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 5) * DEBUG_PREEMPT variant of smp_processor_id().
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 6) */
8bc3bcc93a2b4 (Paul Gortmaker 2011-11-16 21:29:17 -0500 7) #include <linux/export.h>
984640ce427fa (Masami Hiramatsu 2019-02-13 01:14:09 +0900 8) #include <linux/kprobes.h>
4e57b68178809 (Tim Schmielau 2005-10-30 15:03:48 -0800 9) #include <linux/sched.h>
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 10)
126f21f0e8d46 (Thomas Gleixner 2020-03-10 23:47:39 +0100 11) noinstr static
984640ce427fa (Masami Hiramatsu 2019-02-13 01:14:09 +0900 12) unsigned int check_preemption_disabled(const char *what1, const char *what2)
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 13) {
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 14) int this_cpu = raw_smp_processor_id();
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 15)
4a2b4b222743b (Peter Zijlstra 2013-08-14 14:55:24 +0200 16) if (likely(preempt_count()))
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 17) goto out;
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 18)
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 19) if (irqs_disabled())
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 20) goto out;
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 21)
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 22) /*
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 23) * Kernel threads bound to a single CPU can safely use
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 24) * smp_processor_id():
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 25) */
e950cca3f3c40 (Waiman Long 2019-10-03 16:36:08 -0400 26) if (current->nr_cpus_allowed == 1)
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 27) goto out;
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 28)
74d862b682f51 (Thomas Gleixner 2020-11-18 20:48:42 +0100 29) #ifdef CONFIG_SMP
af449901b84c9 (Peter Zijlstra 2020-09-17 10:38:30 +0200 30) if (current->migration_disabled)
af449901b84c9 (Peter Zijlstra 2020-09-17 10:38:30 +0200 31) goto out;
af449901b84c9 (Peter Zijlstra 2020-09-17 10:38:30 +0200 32) #endif
af449901b84c9 (Peter Zijlstra 2020-09-17 10:38:30 +0200 33)
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 34) /*
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 35) * It is valid to assume CPU-locality during early bootup:
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 36) */
1c3c5eab17159 (Thomas Gleixner 2017-05-16 20:42:48 +0200 37) if (system_state < SYSTEM_SCHEDULING)
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 38) goto out;
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 39)
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 40) /*
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 41) * Avoid recursion:
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 42) */
5568b139f4d19 (Steven Rostedt 2008-05-12 21:20:44 +0200 43) preempt_disable_notrace();
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 44)
126f21f0e8d46 (Thomas Gleixner 2020-03-10 23:47:39 +0100 45) instrumentation_begin();
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 46) if (!printk_ratelimit())
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 47) goto out_enable;
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 48)
188a81409ff7d (Christoph Lameter 2014-04-07 15:39:44 -0700 49) printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
188a81409ff7d (Christoph Lameter 2014-04-07 15:39:44 -0700 50) what1, what2, preempt_count() - 1, current->comm, current->pid);
188a81409ff7d (Christoph Lameter 2014-04-07 15:39:44 -0700 51)
d202d47b5e62c (Sergey Senozhatsky 2017-12-11 21:50:24 +0900 52) printk("caller is %pS\n", __builtin_return_address(0));
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 53) dump_stack();
126f21f0e8d46 (Thomas Gleixner 2020-03-10 23:47:39 +0100 54) instrumentation_end();
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 55)
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 56) out_enable:
5568b139f4d19 (Steven Rostedt 2008-05-12 21:20:44 +0200 57) preempt_enable_no_resched_notrace();
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 58) out:
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 59) return this_cpu;
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 60) }
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 61)
126f21f0e8d46 (Thomas Gleixner 2020-03-10 23:47:39 +0100 62) noinstr unsigned int debug_smp_processor_id(void)
188a81409ff7d (Christoph Lameter 2014-04-07 15:39:44 -0700 63) {
188a81409ff7d (Christoph Lameter 2014-04-07 15:39:44 -0700 64) return check_preemption_disabled("smp_processor_id", "");
188a81409ff7d (Christoph Lameter 2014-04-07 15:39:44 -0700 65) }
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 66) EXPORT_SYMBOL(debug_smp_processor_id);
39c715b71740c (Ingo Molnar 2005-06-21 17:14:34 -0700 67)
126f21f0e8d46 (Thomas Gleixner 2020-03-10 23:47:39 +0100 68) noinstr void __this_cpu_preempt_check(const char *op)
188a81409ff7d (Christoph Lameter 2014-04-07 15:39:44 -0700 69) {
188a81409ff7d (Christoph Lameter 2014-04-07 15:39:44 -0700 70) check_preemption_disabled("__this_cpu_", op);
188a81409ff7d (Christoph Lameter 2014-04-07 15:39:44 -0700 71) }
188a81409ff7d (Christoph Lameter 2014-04-07 15:39:44 -0700 72) EXPORT_SYMBOL(__this_cpu_preempt_check);