smp_processor_id.c 1.4 KB

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