blob: 046ac6297c7811ad66e35cb271dabb5ceb015c4d [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
Yejune Deng570a7522021-05-10 16:10:24 +010022 if (is_percpu_thread())
Ingo Molnar39c715b2005-06-21 17:14:34 -070023 goto out;
24
Thomas Gleixner74d862b62020-11-18 20:48:42 +010025#ifdef CONFIG_SMP
Peter Zijlstraaf449902020-09-17 10:38:30 +020026 if (current->migration_disabled)
27 goto out;
28#endif
29
Ingo Molnar39c715b2005-06-21 17:14:34 -070030 /*
31 * It is valid to assume CPU-locality during early bootup:
32 */
Thomas Gleixner1c3c5ea2017-05-16 20:42:48 +020033 if (system_state < SYSTEM_SCHEDULING)
Ingo Molnar39c715b2005-06-21 17:14:34 -070034 goto out;
35
36 /*
37 * Avoid recursion:
38 */
Steven Rostedt5568b132008-05-12 21:20:44 +020039 preempt_disable_notrace();
Ingo Molnar39c715b2005-06-21 17:14:34 -070040
Thomas Gleixner126f21f2020-03-10 23:47:39 +010041 instrumentation_begin();
Ingo Molnar39c715b2005-06-21 17:14:34 -070042 if (!printk_ratelimit())
43 goto out_enable;
44
Christoph Lameter188a8142014-04-07 15:39:44 -070045 printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
46 what1, what2, preempt_count() - 1, current->comm, current->pid);
47
Sergey Senozhatskyd202d472017-12-11 21:50:24 +090048 printk("caller is %pS\n", __builtin_return_address(0));
Ingo Molnar39c715b2005-06-21 17:14:34 -070049 dump_stack();
Thomas Gleixner126f21f2020-03-10 23:47:39 +010050 instrumentation_end();
Ingo Molnar39c715b2005-06-21 17:14:34 -070051
52out_enable:
Steven Rostedt5568b132008-05-12 21:20:44 +020053 preempt_enable_no_resched_notrace();
Ingo Molnar39c715b2005-06-21 17:14:34 -070054out:
55 return this_cpu;
56}
57
Thomas Gleixner126f21f2020-03-10 23:47:39 +010058noinstr unsigned int debug_smp_processor_id(void)
Christoph Lameter188a8142014-04-07 15:39:44 -070059{
60 return check_preemption_disabled("smp_processor_id", "");
61}
Ingo Molnar39c715b2005-06-21 17:14:34 -070062EXPORT_SYMBOL(debug_smp_processor_id);
63
Thomas Gleixner126f21f2020-03-10 23:47:39 +010064noinstr void __this_cpu_preempt_check(const char *op)
Christoph Lameter188a8142014-04-07 15:39:44 -070065{
66 check_preemption_disabled("__this_cpu_", op);
67}
68EXPORT_SYMBOL(__this_cpu_preempt_check);