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 <asm/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. static atomic_t unwinder_running = ATOMIC_INIT(0);
  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. /*
  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 (atomic_inc_return(&unwinder_running) != 1) {
  131. spin_lock_irqsave(&unwinder_lock, flags);
  132. if (!list_is_singular(&unwinder_list)) {
  133. list_del(&curr_unwinder->list);
  134. curr_unwinder = select_unwinder();
  135. }
  136. spin_unlock_irqrestore(&unwinder_lock, flags);
  137. atomic_dec(&unwinder_running);
  138. }
  139. curr_unwinder->dump(task, regs, sp, ops, data);
  140. atomic_dec(&unwinder_running);
  141. }
  142. EXPORT_SYMBOL_GPL(unwind_stack);