hw_breakpoint.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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/cpu.h>
  42. #include <linux/smp.h>
  43. #include <linux/hw_breakpoint.h>
  44. /*
  45. * Constraints data
  46. */
  47. /* Number of pinned cpu breakpoints in a cpu */
  48. static DEFINE_PER_CPU(unsigned int, nr_cpu_bp_pinned);
  49. /* Number of pinned task breakpoints in a cpu */
  50. static DEFINE_PER_CPU(unsigned int, nr_task_bp_pinned[HBP_NUM]);
  51. /* Number of non-pinned cpu/task breakpoints in a cpu */
  52. static DEFINE_PER_CPU(unsigned int, nr_bp_flexible);
  53. /* Gather the number of total pinned and un-pinned bp in a cpuset */
  54. struct bp_busy_slots {
  55. unsigned int pinned;
  56. unsigned int flexible;
  57. };
  58. /* Serialize accesses to the above constraints */
  59. static DEFINE_MUTEX(nr_bp_mutex);
  60. /*
  61. * Report the maximum number of pinned breakpoints a task
  62. * have in this cpu
  63. */
  64. static unsigned int max_task_bp_pinned(int cpu)
  65. {
  66. int i;
  67. unsigned int *tsk_pinned = per_cpu(nr_task_bp_pinned, cpu);
  68. for (i = HBP_NUM -1; i >= 0; i--) {
  69. if (tsk_pinned[i] > 0)
  70. return i + 1;
  71. }
  72. return 0;
  73. }
  74. static int task_bp_pinned(struct task_struct *tsk)
  75. {
  76. struct perf_event_context *ctx = tsk->perf_event_ctxp;
  77. struct list_head *list;
  78. struct perf_event *bp;
  79. unsigned long flags;
  80. int count = 0;
  81. if (WARN_ONCE(!ctx, "No perf context for this task"))
  82. return 0;
  83. list = &ctx->event_list;
  84. raw_spin_lock_irqsave(&ctx->lock, flags);
  85. /*
  86. * The current breakpoint counter is not included in the list
  87. * at the open() callback time
  88. */
  89. list_for_each_entry(bp, list, event_entry) {
  90. if (bp->attr.type == PERF_TYPE_BREAKPOINT)
  91. count++;
  92. }
  93. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  94. return count;
  95. }
  96. /*
  97. * Report the number of pinned/un-pinned breakpoints we have in
  98. * a given cpu (cpu > -1) or in all of them (cpu = -1).
  99. */
  100. static void
  101. fetch_bp_busy_slots(struct bp_busy_slots *slots, struct perf_event *bp)
  102. {
  103. int cpu = bp->cpu;
  104. struct task_struct *tsk = bp->ctx->task;
  105. if (cpu >= 0) {
  106. slots->pinned = per_cpu(nr_cpu_bp_pinned, cpu);
  107. if (!tsk)
  108. slots->pinned += max_task_bp_pinned(cpu);
  109. else
  110. slots->pinned += task_bp_pinned(tsk);
  111. slots->flexible = per_cpu(nr_bp_flexible, cpu);
  112. return;
  113. }
  114. for_each_online_cpu(cpu) {
  115. unsigned int nr;
  116. nr = per_cpu(nr_cpu_bp_pinned, cpu);
  117. if (!tsk)
  118. nr += max_task_bp_pinned(cpu);
  119. else
  120. nr += task_bp_pinned(tsk);
  121. if (nr > slots->pinned)
  122. slots->pinned = nr;
  123. nr = per_cpu(nr_bp_flexible, cpu);
  124. if (nr > slots->flexible)
  125. slots->flexible = nr;
  126. }
  127. }
  128. /*
  129. * Add a pinned breakpoint for the given task in our constraint table
  130. */
  131. static void toggle_bp_task_slot(struct task_struct *tsk, int cpu, bool enable)
  132. {
  133. unsigned int *tsk_pinned;
  134. int count = 0;
  135. count = task_bp_pinned(tsk);
  136. tsk_pinned = per_cpu(nr_task_bp_pinned, cpu);
  137. if (enable) {
  138. tsk_pinned[count]++;
  139. if (count > 0)
  140. tsk_pinned[count-1]--;
  141. } else {
  142. tsk_pinned[count]--;
  143. if (count > 0)
  144. tsk_pinned[count-1]++;
  145. }
  146. }
  147. /*
  148. * Add/remove the given breakpoint in our constraint table
  149. */
  150. static void toggle_bp_slot(struct perf_event *bp, bool enable)
  151. {
  152. int cpu = bp->cpu;
  153. struct task_struct *tsk = bp->ctx->task;
  154. /* Pinned counter task profiling */
  155. if (tsk) {
  156. if (cpu >= 0) {
  157. toggle_bp_task_slot(tsk, cpu, enable);
  158. return;
  159. }
  160. for_each_online_cpu(cpu)
  161. toggle_bp_task_slot(tsk, cpu, enable);
  162. return;
  163. }
  164. /* Pinned counter cpu profiling */
  165. if (enable)
  166. per_cpu(nr_cpu_bp_pinned, bp->cpu)++;
  167. else
  168. per_cpu(nr_cpu_bp_pinned, bp->cpu)--;
  169. }
  170. /*
  171. * Contraints to check before allowing this new breakpoint counter:
  172. *
  173. * == Non-pinned counter == (Considered as pinned for now)
  174. *
  175. * - If attached to a single cpu, check:
  176. *
  177. * (per_cpu(nr_bp_flexible, cpu) || (per_cpu(nr_cpu_bp_pinned, cpu)
  178. * + max(per_cpu(nr_task_bp_pinned, cpu)))) < HBP_NUM
  179. *
  180. * -> If there are already non-pinned counters in this cpu, it means
  181. * there is already a free slot for them.
  182. * Otherwise, we check that the maximum number of per task
  183. * breakpoints (for this cpu) plus the number of per cpu breakpoint
  184. * (for this cpu) doesn't cover every registers.
  185. *
  186. * - If attached to every cpus, check:
  187. *
  188. * (per_cpu(nr_bp_flexible, *) || (max(per_cpu(nr_cpu_bp_pinned, *))
  189. * + max(per_cpu(nr_task_bp_pinned, *)))) < HBP_NUM
  190. *
  191. * -> This is roughly the same, except we check the number of per cpu
  192. * bp for every cpu and we keep the max one. Same for the per tasks
  193. * breakpoints.
  194. *
  195. *
  196. * == Pinned counter ==
  197. *
  198. * - If attached to a single cpu, check:
  199. *
  200. * ((per_cpu(nr_bp_flexible, cpu) > 1) + per_cpu(nr_cpu_bp_pinned, cpu)
  201. * + max(per_cpu(nr_task_bp_pinned, cpu))) < HBP_NUM
  202. *
  203. * -> Same checks as before. But now the nr_bp_flexible, if any, must keep
  204. * one register at least (or they will never be fed).
  205. *
  206. * - If attached to every cpus, check:
  207. *
  208. * ((per_cpu(nr_bp_flexible, *) > 1) + max(per_cpu(nr_cpu_bp_pinned, *))
  209. * + max(per_cpu(nr_task_bp_pinned, *))) < HBP_NUM
  210. */
  211. static int __reserve_bp_slot(struct perf_event *bp)
  212. {
  213. struct bp_busy_slots slots = {0};
  214. fetch_bp_busy_slots(&slots, bp);
  215. /* Flexible counters need to keep at least one slot */
  216. if (slots.pinned + (!!slots.flexible) == HBP_NUM)
  217. return -ENOSPC;
  218. toggle_bp_slot(bp, true);
  219. return 0;
  220. }
  221. int reserve_bp_slot(struct perf_event *bp)
  222. {
  223. int ret;
  224. mutex_lock(&nr_bp_mutex);
  225. ret = __reserve_bp_slot(bp);
  226. mutex_unlock(&nr_bp_mutex);
  227. return ret;
  228. }
  229. static void __release_bp_slot(struct perf_event *bp)
  230. {
  231. toggle_bp_slot(bp, false);
  232. }
  233. void release_bp_slot(struct perf_event *bp)
  234. {
  235. mutex_lock(&nr_bp_mutex);
  236. __release_bp_slot(bp);
  237. mutex_unlock(&nr_bp_mutex);
  238. }
  239. /*
  240. * Allow the kernel debugger to reserve breakpoint slots without
  241. * taking a lock using the dbg_* variant of for the reserve and
  242. * release breakpoint slots.
  243. */
  244. int dbg_reserve_bp_slot(struct perf_event *bp)
  245. {
  246. if (mutex_is_locked(&nr_bp_mutex))
  247. return -1;
  248. return __reserve_bp_slot(bp);
  249. }
  250. int dbg_release_bp_slot(struct perf_event *bp)
  251. {
  252. if (mutex_is_locked(&nr_bp_mutex))
  253. return -1;
  254. __release_bp_slot(bp);
  255. return 0;
  256. }
  257. int register_perf_hw_breakpoint(struct perf_event *bp)
  258. {
  259. int ret;
  260. ret = reserve_bp_slot(bp);
  261. if (ret)
  262. return ret;
  263. /*
  264. * Ptrace breakpoints can be temporary perf events only
  265. * meant to reserve a slot. In this case, it is created disabled and
  266. * we don't want to check the params right now (as we put a null addr)
  267. * But perf tools create events as disabled and we want to check
  268. * the params for them.
  269. * This is a quick hack that will be removed soon, once we remove
  270. * the tmp breakpoints from ptrace
  271. */
  272. if (!bp->attr.disabled || !bp->overflow_handler)
  273. ret = arch_validate_hwbkpt_settings(bp, bp->ctx->task);
  274. /* if arch_validate_hwbkpt_settings() fails then release bp slot */
  275. if (ret)
  276. release_bp_slot(bp);
  277. return ret;
  278. }
  279. /**
  280. * register_user_hw_breakpoint - register a hardware breakpoint for user space
  281. * @attr: breakpoint attributes
  282. * @triggered: callback to trigger when we hit the breakpoint
  283. * @tsk: pointer to 'task_struct' of the process to which the address belongs
  284. */
  285. struct perf_event *
  286. register_user_hw_breakpoint(struct perf_event_attr *attr,
  287. perf_overflow_handler_t triggered,
  288. struct task_struct *tsk)
  289. {
  290. return perf_event_create_kernel_counter(attr, -1, tsk->pid, triggered);
  291. }
  292. EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
  293. /**
  294. * modify_user_hw_breakpoint - modify a user-space hardware breakpoint
  295. * @bp: the breakpoint structure to modify
  296. * @attr: new breakpoint attributes
  297. * @triggered: callback to trigger when we hit the breakpoint
  298. * @tsk: pointer to 'task_struct' of the process to which the address belongs
  299. */
  300. int modify_user_hw_breakpoint(struct perf_event *bp, struct perf_event_attr *attr)
  301. {
  302. u64 old_addr = bp->attr.bp_addr;
  303. u64 old_len = bp->attr.bp_len;
  304. int old_type = bp->attr.bp_type;
  305. int err = 0;
  306. perf_event_disable(bp);
  307. bp->attr.bp_addr = attr->bp_addr;
  308. bp->attr.bp_type = attr->bp_type;
  309. bp->attr.bp_len = attr->bp_len;
  310. if (attr->disabled)
  311. goto end;
  312. err = arch_validate_hwbkpt_settings(bp, bp->ctx->task);
  313. if (!err)
  314. perf_event_enable(bp);
  315. if (err) {
  316. bp->attr.bp_addr = old_addr;
  317. bp->attr.bp_type = old_type;
  318. bp->attr.bp_len = old_len;
  319. if (!bp->attr.disabled)
  320. perf_event_enable(bp);
  321. return err;
  322. }
  323. end:
  324. bp->attr.disabled = attr->disabled;
  325. return 0;
  326. }
  327. EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint);
  328. /**
  329. * unregister_hw_breakpoint - unregister a user-space hardware breakpoint
  330. * @bp: the breakpoint structure to unregister
  331. */
  332. void unregister_hw_breakpoint(struct perf_event *bp)
  333. {
  334. if (!bp)
  335. return;
  336. perf_event_release_kernel(bp);
  337. }
  338. EXPORT_SYMBOL_GPL(unregister_hw_breakpoint);
  339. /**
  340. * register_wide_hw_breakpoint - register a wide breakpoint in the kernel
  341. * @attr: breakpoint attributes
  342. * @triggered: callback to trigger when we hit the breakpoint
  343. *
  344. * @return a set of per_cpu pointers to perf events
  345. */
  346. struct perf_event * __percpu *
  347. register_wide_hw_breakpoint(struct perf_event_attr *attr,
  348. perf_overflow_handler_t triggered)
  349. {
  350. struct perf_event * __percpu *cpu_events, **pevent, *bp;
  351. long err;
  352. int cpu;
  353. cpu_events = alloc_percpu(typeof(*cpu_events));
  354. if (!cpu_events)
  355. return (void __percpu __force *)ERR_PTR(-ENOMEM);
  356. get_online_cpus();
  357. for_each_online_cpu(cpu) {
  358. pevent = per_cpu_ptr(cpu_events, cpu);
  359. bp = perf_event_create_kernel_counter(attr, cpu, -1, triggered);
  360. *pevent = bp;
  361. if (IS_ERR(bp)) {
  362. err = PTR_ERR(bp);
  363. goto fail;
  364. }
  365. }
  366. put_online_cpus();
  367. return cpu_events;
  368. fail:
  369. for_each_online_cpu(cpu) {
  370. pevent = per_cpu_ptr(cpu_events, cpu);
  371. if (IS_ERR(*pevent))
  372. break;
  373. unregister_hw_breakpoint(*pevent);
  374. }
  375. put_online_cpus();
  376. free_percpu(cpu_events);
  377. return (void __percpu __force *)ERR_PTR(err);
  378. }
  379. EXPORT_SYMBOL_GPL(register_wide_hw_breakpoint);
  380. /**
  381. * unregister_wide_hw_breakpoint - unregister a wide breakpoint in the kernel
  382. * @cpu_events: the per cpu set of events to unregister
  383. */
  384. void unregister_wide_hw_breakpoint(struct perf_event * __percpu *cpu_events)
  385. {
  386. int cpu;
  387. struct perf_event **pevent;
  388. for_each_possible_cpu(cpu) {
  389. pevent = per_cpu_ptr(cpu_events, cpu);
  390. unregister_hw_breakpoint(*pevent);
  391. }
  392. free_percpu(cpu_events);
  393. }
  394. EXPORT_SYMBOL_GPL(unregister_wide_hw_breakpoint);
  395. static struct notifier_block hw_breakpoint_exceptions_nb = {
  396. .notifier_call = hw_breakpoint_exceptions_notify,
  397. /* we need to be notified first */
  398. .priority = 0x7fffffff
  399. };
  400. static int __init init_hw_breakpoint(void)
  401. {
  402. return register_die_notifier(&hw_breakpoint_exceptions_nb);
  403. }
  404. core_initcall(init_hw_breakpoint);
  405. struct pmu perf_ops_bp = {
  406. .enable = arch_install_hw_breakpoint,
  407. .disable = arch_uninstall_hw_breakpoint,
  408. .read = hw_breakpoint_pmu_read,
  409. };