blob: 1c1dbd3003257a618c7420d7286cf78668cc4051 [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
Thomas Gleixner126f21f2020-03-10 23:47:39 +010011noinstr static
Masami Hiramatsu984640c2019-02-13 01:14:09 +090012unsigned 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
Thomas Gleixner74d862b62020-11-18 20:48:42 +010029#ifdef CONFIG_SMP
Peter Zijlstraaf449902020-09-17 10:38:30 +020030 if (current->migration_disabled)
31 goto out;
32#endif
33
Ingo Molnar39c715b2005-06-21 17:14:34 -070034 /*
35 * It is valid to assume CPU-locality during early bootup:
36 */
Thomas Gleixner1c3c5ea2017-05-16 20:42:48 +020037 if (system_state < SYSTEM_SCHEDULING)
Ingo Molnar39c715b2005-06-21 17:14:34 -070038 goto out;
39
40 /*
41 * Avoid recursion:
42 */
Steven Rostedt5568b132008-05-12 21:20:44 +020043 preempt_disable_notrace();
Ingo Molnar39c715b2005-06-21 17:14:34 -070044
Thomas Gleixner126f21f2020-03-10 23:47:39 +010045 instrumentation_begin();
Ingo Molnar39c715b2005-06-21 17:14:34 -070046 if (!printk_ratelimit())
47 goto out_enable;
48
Christoph Lameter188a8142014-04-07 15:39:44 -070049 printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
50 what1, what2, preempt_count() - 1, current->comm, current->pid);
51
Sergey Senozhatskyd202d472017-12-11 21:50:24 +090052 printk("caller is %pS\n", __builtin_return_address(0));
Ingo Molnar39c715b2005-06-21 17:14:34 -070053 dump_stack();
Thomas Gleixner126f21f2020-03-10 23:47:39 +010054 instrumentation_end();
Ingo Molnar39c715b2005-06-21 17:14:34 -070055
56out_enable:
Steven Rostedt5568b132008-05-12 21:20:44 +020057 preempt_enable_no_resched_notrace();
Ingo Molnar39c715b2005-06-21 17:14:34 -070058out:
59 return this_cpu;
60}
61
Thomas Gleixner126f21f2020-03-10 23:47:39 +010062noinstr unsigned int debug_smp_processor_id(void)
Christoph Lameter188a8142014-04-07 15:39:44 -070063{
64 return check_preemption_disabled("smp_processor_id", "");
65}
Ingo Molnar39c715b2005-06-21 17:14:34 -070066EXPORT_SYMBOL(debug_smp_processor_id);
67
Thomas Gleixner126f21f2020-03-10 23:47:39 +010068noinstr void __this_cpu_preempt_check(const char *op)
Christoph Lameter188a8142014-04-07 15:39:44 -070069{
70 check_preemption_disabled("__this_cpu_", op);
71}
72EXPORT_SYMBOL(__this_cpu_preempt_check);