unwinder.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2009 Matt Fleming
  4. *
  5. * Based, in part, on kernel/time/clocksource.c.
  6. *
  7. * This file provides arbitration code for stack unwinders.
  8. *
  9. * Multiple stack unwinders can be available on a system, usually with
  10. * the most accurate unwinder being the currently active one.
  11. */
  12. #include <linux/errno.h>
  13. #include <linux/list.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/module.h>
  16. #include <asm/unwinder.h>
  17. #include <linux/atomic.h>
  18. /*
  19. * This is the most basic stack unwinder an architecture can
  20. * provide. For architectures without reliable frame pointers, e.g.
  21. * RISC CPUs, it can be implemented by looking through the stack for
  22. * addresses that lie within the kernel text section.
  23. *
  24. * Other CPUs, e.g. x86, can use their frame pointer register to
  25. * construct more accurate stack traces.
  26. */
  27. static struct list_head unwinder_list;
  28. static struct unwinder stack_reader = {
  29. .name = "stack-reader",
  30. .dump = stack_reader_dump,
  31. .rating = 50,
  32. .list = {
  33. .next = &unwinder_list,
  34. .prev = &unwinder_list,
  35. },
  36. };
  37. /*
  38. * "curr_unwinder" points to the stack unwinder currently in use. This
  39. * is the unwinder with the highest rating.
  40. *
  41. * "unwinder_list" is a linked-list of all available unwinders, sorted
  42. * by rating.
  43. *
  44. * All modifications of "curr_unwinder" and "unwinder_list" must be
  45. * performed whilst holding "unwinder_lock".
  46. */
  47. static struct unwinder *curr_unwinder = &stack_reader;
  48. static struct list_head unwinder_list = {
  49. .next = &stack_reader.list,
  50. .prev = &stack_reader.list,
  51. };
  52. static DEFINE_SPINLOCK(unwinder_lock);
  53. /**
  54. * select_unwinder - Select the best registered stack unwinder.
  55. *
  56. * Private function. Must hold unwinder_lock when called.
  57. *
  58. * Select the stack unwinder with the best rating. This is useful for
  59. * setting up curr_unwinder.
  60. */
  61. static struct unwinder *select_unwinder(void)
  62. {
  63. struct unwinder *best;
  64. if (list_empty(&unwinder_list))
  65. return NULL;
  66. best = list_entry(unwinder_list.next, struct unwinder, list);
  67. if (best == curr_unwinder)
  68. return NULL;
  69. return best;
  70. }
  71. /*
  72. * Enqueue the stack unwinder sorted by rating.
  73. */
  74. static int unwinder_enqueue(struct unwinder *ops)
  75. {
  76. struct list_head *tmp, *entry = &unwinder_list;
  77. list_for_each(tmp, &unwinder_list) {
  78. struct unwinder *o;
  79. o = list_entry(tmp, struct unwinder, list);
  80. if (o == ops)
  81. return -EBUSY;
  82. /* Keep track of the place, where to insert */
  83. if (o->rating >= ops->rating)
  84. entry = tmp;
  85. }
  86. list_add(&ops->list, entry);
  87. return 0;
  88. }
  89. /**
  90. * unwinder_register - Used to install new stack unwinder
  91. * @u: unwinder to be registered
  92. *
  93. * Install the new stack unwinder on the unwinder list, which is sorted
  94. * by rating.
  95. *
  96. * Returns -EBUSY if registration fails, zero otherwise.
  97. */
  98. int unwinder_register(struct unwinder *u)
  99. {
  100. unsigned long flags;
  101. int ret;
  102. spin_lock_irqsave(&unwinder_lock, flags);
  103. ret = unwinder_enqueue(u);
  104. if (!ret)
  105. curr_unwinder = select_unwinder();
  106. spin_unlock_irqrestore(&unwinder_lock, flags);
  107. return ret;
  108. }
  109. int unwinder_faulted = 0;
  110. /*
  111. * Unwind the call stack and pass information to the stacktrace_ops
  112. * functions. Also handle the case where we need to switch to a new
  113. * stack dumper because the current one faulted unexpectedly.
  114. */
  115. void unwind_stack(struct task_struct *task, struct pt_regs *regs,
  116. unsigned long *sp, const struct stacktrace_ops *ops,
  117. void *data)
  118. {
  119. unsigned long flags;
  120. /*
  121. * The problem with unwinders with high ratings is that they are
  122. * inherently more complicated than the simple ones with lower
  123. * ratings. We are therefore more likely to fault in the
  124. * complicated ones, e.g. hitting BUG()s. If we fault in the
  125. * code for the current stack unwinder we try to downgrade to
  126. * one with a lower rating.
  127. *
  128. * Hopefully this will give us a semi-reliable stacktrace so we
  129. * can diagnose why curr_unwinder->dump() faulted.
  130. */
  131. if (unwinder_faulted) {
  132. spin_lock_irqsave(&unwinder_lock, flags);
  133. /* Make sure no one beat us to changing the unwinder */
  134. if (unwinder_faulted && !list_is_singular(&unwinder_list)) {
  135. list_del(&curr_unwinder->list);
  136. curr_unwinder = select_unwinder();
  137. unwinder_faulted = 0;
  138. }
  139. spin_unlock_irqrestore(&unwinder_lock, flags);
  140. }
  141. curr_unwinder->dump(task, regs, sp, ops, data);
  142. }
  143. EXPORT_SYMBOL_GPL(unwind_stack);