unwinder.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <asm/unwinder.h>
  15. #include <asm/atomic.h>
  16. /*
  17. * This is the most basic stack unwinder an architecture can
  18. * provide. For architectures without reliable frame pointers, e.g.
  19. * RISC CPUs, it can be implemented by looking through the stack for
  20. * addresses that lie within the kernel text section.
  21. *
  22. * Other CPUs, e.g. x86, can use their frame pointer register to
  23. * construct more accurate stack traces.
  24. */
  25. static struct list_head unwinder_list;
  26. static struct unwinder stack_reader = {
  27. .name = "stack-reader",
  28. .dump = stack_reader_dump,
  29. .rating = 50,
  30. .list = {
  31. .next = &unwinder_list,
  32. .prev = &unwinder_list,
  33. },
  34. };
  35. /*
  36. * "curr_unwinder" points to the stack unwinder currently in use. This
  37. * is the unwinder with the highest rating.
  38. *
  39. * "unwinder_list" is a linked-list of all available unwinders, sorted
  40. * by rating.
  41. *
  42. * All modifications of "curr_unwinder" and "unwinder_list" must be
  43. * performed whilst holding "unwinder_lock".
  44. */
  45. static struct unwinder *curr_unwinder = &stack_reader;
  46. static struct list_head unwinder_list = {
  47. .next = &stack_reader.list,
  48. .prev = &stack_reader.list,
  49. };
  50. static DEFINE_SPINLOCK(unwinder_lock);
  51. static atomic_t unwinder_running = ATOMIC_INIT(0);
  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. /*
  109. * Unwind the call stack and pass information to the stacktrace_ops
  110. * functions. Also handle the case where we need to switch to a new
  111. * stack dumper because the current one faulted unexpectedly.
  112. */
  113. void unwind_stack(struct task_struct *task, struct pt_regs *regs,
  114. unsigned long *sp, const struct stacktrace_ops *ops,
  115. void *data)
  116. {
  117. unsigned long flags;
  118. /*
  119. * The problem with unwinders with high ratings is that they are
  120. * inherently more complicated than the simple ones with lower
  121. * ratings. We are therefore more likely to fault in the
  122. * complicated ones, e.g. hitting BUG()s. If we fault in the
  123. * code for the current stack unwinder we try to downgrade to
  124. * one with a lower rating.
  125. *
  126. * Hopefully this will give us a semi-reliable stacktrace so we
  127. * can diagnose why curr_unwinder->dump() faulted.
  128. */
  129. if (atomic_inc_return(&unwinder_running) != 1) {
  130. spin_lock_irqsave(&unwinder_lock, flags);
  131. if (!list_is_singular(&unwinder_list)) {
  132. list_del(&curr_unwinder->list);
  133. curr_unwinder = select_unwinder();
  134. }
  135. spin_unlock_irqrestore(&unwinder_lock, flags);
  136. atomic_dec(&unwinder_running);
  137. }
  138. curr_unwinder->dump(task, regs, sp, ops, data);
  139. atomic_dec(&unwinder_running);
  140. }