hw_breakpoint.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. *
  16. * Copyright (C) 2007 Alan Stern
  17. * Copyright (C) IBM Corporation, 2009
  18. * Copyright (C) 2009, Frederic Weisbecker <fweisbec@gmail.com>
  19. *
  20. * Thanks to Ingo Molnar for his many suggestions.
  21. *
  22. * Authors: Alan Stern <stern@rowland.harvard.edu>
  23. * K.Prasad <prasad@linux.vnet.ibm.com>
  24. * Frederic Weisbecker <fweisbec@gmail.com>
  25. */
  26. /*
  27. * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
  28. * using the CPU's debug registers.
  29. * This file contains the arch-independent routines.
  30. */
  31. #include <linux/irqflags.h>
  32. #include <linux/kallsyms.h>
  33. #include <linux/notifier.h>
  34. #include <linux/kprobes.h>
  35. #include <linux/kdebug.h>
  36. #include <linux/kernel.h>
  37. #include <linux/module.h>
  38. #include <linux/percpu.h>
  39. #include <linux/sched.h>
  40. #include <linux/init.h>
  41. #include <linux/smp.h>
  42. #include <linux/hw_breakpoint.h>
  43. /*
  44. * Constraints data
  45. */
  46. /* Number of pinned cpu breakpoints in a cpu */
  47. static DEFINE_PER_CPU(unsigned int, nr_cpu_bp_pinned);
  48. /* Number of pinned task breakpoints in a cpu */
  49. static DEFINE_PER_CPU(unsigned int, task_bp_pinned[HBP_NUM]);
  50. /* Number of non-pinned cpu/task breakpoints in a cpu */
  51. static DEFINE_PER_CPU(unsigned int, nr_bp_flexible);
  52. /* Gather the number of total pinned and un-pinned bp in a cpuset */
  53. struct bp_busy_slots {
  54. unsigned int pinned;
  55. unsigned int flexible;
  56. };
  57. /* Serialize accesses to the above constraints */
  58. static DEFINE_MUTEX(nr_bp_mutex);
  59. /*
  60. * Report the maximum number of pinned breakpoints a task
  61. * have in this cpu
  62. */
  63. static unsigned int max_task_bp_pinned(int cpu)
  64. {
  65. int i;
  66. unsigned int *tsk_pinned = per_cpu(task_bp_pinned, cpu);
  67. for (i = HBP_NUM -1; i >= 0; i--) {
  68. if (tsk_pinned[i] > 0)
  69. return i + 1;
  70. }
  71. return 0;
  72. }
  73. /*
  74. * Report the number of pinned/un-pinned breakpoints we have in
  75. * a given cpu (cpu > -1) or in all of them (cpu = -1).
  76. */
  77. static void fetch_bp_busy_slots(struct bp_busy_slots *slots, int cpu)
  78. {
  79. if (cpu >= 0) {
  80. slots->pinned = per_cpu(nr_cpu_bp_pinned, cpu);
  81. slots->pinned += max_task_bp_pinned(cpu);
  82. slots->flexible = per_cpu(nr_bp_flexible, cpu);
  83. return;
  84. }
  85. for_each_online_cpu(cpu) {
  86. unsigned int nr;
  87. nr = per_cpu(nr_cpu_bp_pinned, cpu);
  88. nr += max_task_bp_pinned(cpu);
  89. if (nr > slots->pinned)
  90. slots->pinned = nr;
  91. nr = per_cpu(nr_bp_flexible, cpu);
  92. if (nr > slots->flexible)
  93. slots->flexible = nr;
  94. }
  95. }
  96. /*
  97. * Add a pinned breakpoint for the given task in our constraint table
  98. */
  99. static void toggle_bp_task_slot(struct task_struct *tsk, int cpu, bool enable)
  100. {
  101. int count = 0;
  102. struct perf_event *bp;
  103. struct perf_event_context *ctx = tsk->perf_event_ctxp;
  104. unsigned int *tsk_pinned;
  105. struct list_head *list;
  106. unsigned long flags;
  107. if (WARN_ONCE(!ctx, "No perf context for this task"))
  108. return;
  109. list = &ctx->event_list;
  110. spin_lock_irqsave(&ctx->lock, flags);
  111. /*
  112. * The current breakpoint counter is not included in the list
  113. * at the open() callback time
  114. */
  115. list_for_each_entry(bp, list, event_entry) {
  116. if (bp->attr.type == PERF_TYPE_BREAKPOINT)
  117. count++;
  118. }
  119. spin_unlock_irqrestore(&ctx->lock, flags);
  120. if (WARN_ONCE(count < 0, "No breakpoint counter found in the counter list"))
  121. return;
  122. tsk_pinned = per_cpu(task_bp_pinned, cpu);
  123. if (enable) {
  124. tsk_pinned[count]++;
  125. if (count > 0)
  126. tsk_pinned[count-1]--;
  127. } else {
  128. tsk_pinned[count]--;
  129. if (count > 0)
  130. tsk_pinned[count-1]++;
  131. }
  132. }
  133. /*
  134. * Add/remove the given breakpoint in our constraint table
  135. */
  136. static void toggle_bp_slot(struct perf_event *bp, bool enable)
  137. {
  138. int cpu = bp->cpu;
  139. struct task_struct *tsk = bp->ctx->task;
  140. /* Pinned counter task profiling */
  141. if (tsk) {
  142. if (cpu >= 0) {
  143. toggle_bp_task_slot(tsk, cpu, enable);
  144. return;
  145. }
  146. for_each_online_cpu(cpu)
  147. toggle_bp_task_slot(tsk, cpu, enable);
  148. return;
  149. }
  150. /* Pinned counter cpu profiling */
  151. if (enable)
  152. per_cpu(nr_cpu_bp_pinned, bp->cpu)++;
  153. else
  154. per_cpu(nr_cpu_bp_pinned, bp->cpu)--;
  155. }
  156. /*
  157. * Contraints to check before allowing this new breakpoint counter:
  158. *
  159. * == Non-pinned counter == (Considered as pinned for now)
  160. *
  161. * - If attached to a single cpu, check:
  162. *
  163. * (per_cpu(nr_bp_flexible, cpu) || (per_cpu(nr_cpu_bp_pinned, cpu)
  164. * + max(per_cpu(task_bp_pinned, cpu)))) < HBP_NUM
  165. *
  166. * -> If there are already non-pinned counters in this cpu, it means
  167. * there is already a free slot for them.
  168. * Otherwise, we check that the maximum number of per task
  169. * breakpoints (for this cpu) plus the number of per cpu breakpoint
  170. * (for this cpu) doesn't cover every registers.
  171. *
  172. * - If attached to every cpus, check:
  173. *
  174. * (per_cpu(nr_bp_flexible, *) || (max(per_cpu(nr_cpu_bp_pinned, *))
  175. * + max(per_cpu(task_bp_pinned, *)))) < HBP_NUM
  176. *
  177. * -> This is roughly the same, except we check the number of per cpu
  178. * bp for every cpu and we keep the max one. Same for the per tasks
  179. * breakpoints.
  180. *
  181. *
  182. * == Pinned counter ==
  183. *
  184. * - If attached to a single cpu, check:
  185. *
  186. * ((per_cpu(nr_bp_flexible, cpu) > 1) + per_cpu(nr_cpu_bp_pinned, cpu)
  187. * + max(per_cpu(task_bp_pinned, cpu))) < HBP_NUM
  188. *
  189. * -> Same checks as before. But now the nr_bp_flexible, if any, must keep
  190. * one register at least (or they will never be fed).
  191. *
  192. * - If attached to every cpus, check:
  193. *
  194. * ((per_cpu(nr_bp_flexible, *) > 1) + max(per_cpu(nr_cpu_bp_pinned, *))
  195. * + max(per_cpu(task_bp_pinned, *))) < HBP_NUM
  196. */
  197. int reserve_bp_slot(struct perf_event *bp)
  198. {
  199. struct bp_busy_slots slots = {0};
  200. int ret = 0;
  201. mutex_lock(&nr_bp_mutex);
  202. fetch_bp_busy_slots(&slots, bp->cpu);
  203. /* Flexible counters need to keep at least one slot */
  204. if (slots.pinned + (!!slots.flexible) == HBP_NUM) {
  205. ret = -ENOSPC;
  206. goto end;
  207. }
  208. toggle_bp_slot(bp, true);
  209. end:
  210. mutex_unlock(&nr_bp_mutex);
  211. return ret;
  212. }
  213. void release_bp_slot(struct perf_event *bp)
  214. {
  215. mutex_lock(&nr_bp_mutex);
  216. toggle_bp_slot(bp, false);
  217. mutex_unlock(&nr_bp_mutex);
  218. }
  219. int __register_perf_hw_breakpoint(struct perf_event *bp)
  220. {
  221. int ret;
  222. ret = reserve_bp_slot(bp);
  223. if (ret)
  224. return ret;
  225. /*
  226. * Ptrace breakpoints can be temporary perf events only
  227. * meant to reserve a slot. In this case, it is created disabled and
  228. * we don't want to check the params right now (as we put a null addr)
  229. * But perf tools create events as disabled and we want to check
  230. * the params for them.
  231. * This is a quick hack that will be removed soon, once we remove
  232. * the tmp breakpoints from ptrace
  233. */
  234. if (!bp->attr.disabled || bp->callback == perf_bp_event)
  235. ret = arch_validate_hwbkpt_settings(bp, bp->ctx->task);
  236. return ret;
  237. }
  238. int register_perf_hw_breakpoint(struct perf_event *bp)
  239. {
  240. bp->callback = perf_bp_event;
  241. return __register_perf_hw_breakpoint(bp);
  242. }
  243. /**
  244. * register_user_hw_breakpoint - register a hardware breakpoint for user space
  245. * @attr: breakpoint attributes
  246. * @triggered: callback to trigger when we hit the breakpoint
  247. * @tsk: pointer to 'task_struct' of the process to which the address belongs
  248. */
  249. struct perf_event *
  250. register_user_hw_breakpoint(struct perf_event_attr *attr,
  251. perf_callback_t triggered,
  252. struct task_struct *tsk)
  253. {
  254. return perf_event_create_kernel_counter(attr, -1, tsk->pid, triggered);
  255. }
  256. EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
  257. /**
  258. * modify_user_hw_breakpoint - modify a user-space hardware breakpoint
  259. * @bp: the breakpoint structure to modify
  260. * @attr: new breakpoint attributes
  261. * @triggered: callback to trigger when we hit the breakpoint
  262. * @tsk: pointer to 'task_struct' of the process to which the address belongs
  263. */
  264. struct perf_event *
  265. modify_user_hw_breakpoint(struct perf_event *bp, struct perf_event_attr *attr,
  266. perf_callback_t triggered,
  267. struct task_struct *tsk)
  268. {
  269. /*
  270. * FIXME: do it without unregistering
  271. * - We don't want to lose our slot
  272. * - If the new bp is incorrect, don't lose the older one
  273. */
  274. unregister_hw_breakpoint(bp);
  275. return perf_event_create_kernel_counter(attr, -1, tsk->pid, triggered);
  276. }
  277. EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint);
  278. /**
  279. * unregister_hw_breakpoint - unregister a user-space hardware breakpoint
  280. * @bp: the breakpoint structure to unregister
  281. */
  282. void unregister_hw_breakpoint(struct perf_event *bp)
  283. {
  284. if (!bp)
  285. return;
  286. perf_event_release_kernel(bp);
  287. }
  288. EXPORT_SYMBOL_GPL(unregister_hw_breakpoint);
  289. /**
  290. * register_wide_hw_breakpoint - register a wide breakpoint in the kernel
  291. * @attr: breakpoint attributes
  292. * @triggered: callback to trigger when we hit the breakpoint
  293. *
  294. * @return a set of per_cpu pointers to perf events
  295. */
  296. struct perf_event **
  297. register_wide_hw_breakpoint(struct perf_event_attr *attr,
  298. perf_callback_t triggered)
  299. {
  300. struct perf_event **cpu_events, **pevent, *bp;
  301. long err;
  302. int cpu;
  303. cpu_events = alloc_percpu(typeof(*cpu_events));
  304. if (!cpu_events)
  305. return ERR_PTR(-ENOMEM);
  306. for_each_possible_cpu(cpu) {
  307. pevent = per_cpu_ptr(cpu_events, cpu);
  308. bp = perf_event_create_kernel_counter(attr, cpu, -1, triggered);
  309. *pevent = bp;
  310. if (IS_ERR(bp)) {
  311. err = PTR_ERR(bp);
  312. goto fail;
  313. }
  314. }
  315. return cpu_events;
  316. fail:
  317. for_each_possible_cpu(cpu) {
  318. pevent = per_cpu_ptr(cpu_events, cpu);
  319. if (IS_ERR(*pevent))
  320. break;
  321. unregister_hw_breakpoint(*pevent);
  322. }
  323. free_percpu(cpu_events);
  324. /* return the error if any */
  325. return ERR_PTR(err);
  326. }
  327. EXPORT_SYMBOL_GPL(register_wide_hw_breakpoint);
  328. /**
  329. * unregister_wide_hw_breakpoint - unregister a wide breakpoint in the kernel
  330. * @cpu_events: the per cpu set of events to unregister
  331. */
  332. void unregister_wide_hw_breakpoint(struct perf_event **cpu_events)
  333. {
  334. int cpu;
  335. struct perf_event **pevent;
  336. for_each_possible_cpu(cpu) {
  337. pevent = per_cpu_ptr(cpu_events, cpu);
  338. unregister_hw_breakpoint(*pevent);
  339. }
  340. free_percpu(cpu_events);
  341. }
  342. EXPORT_SYMBOL_GPL(unregister_wide_hw_breakpoint);
  343. static struct notifier_block hw_breakpoint_exceptions_nb = {
  344. .notifier_call = hw_breakpoint_exceptions_notify,
  345. /* we need to be notified first */
  346. .priority = 0x7fffffff
  347. };
  348. static int __init init_hw_breakpoint(void)
  349. {
  350. return register_die_notifier(&hw_breakpoint_exceptions_nb);
  351. }
  352. core_initcall(init_hw_breakpoint);
  353. struct pmu perf_ops_bp = {
  354. .enable = arch_install_hw_breakpoint,
  355. .disable = arch_uninstall_hw_breakpoint,
  356. .read = hw_breakpoint_pmu_read,
  357. .unthrottle = hw_breakpoint_pmu_unthrottle
  358. };