^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * lib/smp_processor_id.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * DEBUG_PREEMPT variant of smp_processor_id().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) noinstr static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) unsigned int check_preemption_disabled(const char *what1, const char *what2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) int this_cpu = raw_smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) if (likely(preempt_count()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) if (irqs_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Kernel threads bound to a single CPU can safely use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * smp_processor_id():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (current->nr_cpus_allowed == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * It is valid to assume CPU-locality during early bootup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (system_state < SYSTEM_SCHEDULING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * Avoid recursion:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) preempt_disable_notrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) instrumentation_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (!printk_ratelimit())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) goto out_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) what1, what2, preempt_count() - 1, current->comm, current->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) printk("caller is %pS\n", __builtin_return_address(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) instrumentation_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) out_enable:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) preempt_enable_no_resched_notrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return this_cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) noinstr unsigned int debug_smp_processor_id(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return check_preemption_disabled("smp_processor_id", "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) EXPORT_SYMBOL(debug_smp_processor_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) noinstr void __this_cpu_preempt_check(const char *op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) check_preemption_disabled("__this_cpu_", op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) EXPORT_SYMBOL(__this_cpu_preempt_check);