unwinder.c 4.2 KB

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