blob: bd95716532889016c7d03a687e06c7ee162646e3 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ingo Molnar39c715b2005-06-21 17:14:34 -07002/*
3 * lib/smp_processor_id.c
4 *
5 * DEBUG_PREEMPT variant of smp_processor_id().
6 */
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05007#include <linux/export.h>
Masami Hiramatsu984640c2019-02-13 01:14:09 +09008#include <linux/kprobes.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -08009#include <linux/sched.h>
Ingo Molnar39c715b2005-06-21 17:14:34 -070010
Masami Hiramatsu984640c2019-02-13 01:14:09 +090011notrace static nokprobe_inline
12unsigned int check_preemption_disabled(const char *what1, const char *what2)
Ingo Molnar39c715b2005-06-21 17:14:34 -070013{
Ingo Molnar39c715b2005-06-21 17:14:34 -070014 int this_cpu = raw_smp_processor_id();
Ingo Molnar39c715b2005-06-21 17:14:34 -070015
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +020016 if (likely(preempt_count()))
Ingo Molnar39c715b2005-06-21 17:14:34 -070017 goto out;
18
19 if (irqs_disabled())
20 goto out;
21
22 /*
23 * Kernel threads bound to a single CPU can safely use
24 * smp_processor_id():
25 */
Waiman Longe950cca2019-10-03 16:36:08 -040026 if (current->nr_cpus_allowed == 1)
Ingo Molnar39c715b2005-06-21 17:14:34 -070027 goto out;
28
29 /*
30 * It is valid to assume CPU-locality during early bootup:
31 */
Thomas Gleixner1c3c5ea2017-05-16 20:42:48 +020032 if (system_state < SYSTEM_SCHEDULING)
Ingo Molnar39c715b2005-06-21 17:14:34 -070033 goto out;
34
35 /*
36 * Avoid recursion:
37 */
Steven Rostedt5568b132008-05-12 21:20:44 +020038 preempt_disable_notrace();
Ingo Molnar39c715b2005-06-21 17:14:34 -070039
40 if (!printk_ratelimit())
41 goto out_enable;
42
Christoph Lameter188a8142014-04-07 15:39:44 -070043 printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
44 what1, what2, preempt_count() - 1, current->comm, current->pid);
45
Sergey Senozhatskyd202d472017-12-11 21:50:24 +090046 printk("caller is %pS\n", __builtin_return_address(0));
Ingo Molnar39c715b2005-06-21 17:14:34 -070047 dump_stack();
48
49out_enable:
Steven Rostedt5568b132008-05-12 21:20:44 +020050 preempt_enable_no_resched_notrace();
Ingo Molnar39c715b2005-06-21 17:14:34 -070051out:
52 return this_cpu;
53}
54
Christoph Lameter188a8142014-04-07 15:39:44 -070055notrace unsigned int debug_smp_processor_id(void)
56{
57 return check_preemption_disabled("smp_processor_id", "");
58}
Ingo Molnar39c715b2005-06-21 17:14:34 -070059EXPORT_SYMBOL(debug_smp_processor_id);
Masami Hiramatsu984640c2019-02-13 01:14:09 +090060NOKPROBE_SYMBOL(debug_smp_processor_id);
Ingo Molnar39c715b2005-06-21 17:14:34 -070061
Christoph Lameter188a8142014-04-07 15:39:44 -070062notrace void __this_cpu_preempt_check(const char *op)
63{
64 check_preemption_disabled("__this_cpu_", op);
65}
66EXPORT_SYMBOL(__this_cpu_preempt_check);
Masami Hiramatsu984640c2019-02-13 01:14:09 +090067NOKPROBE_SYMBOL(__this_cpu_preempt_check);