hw_breakpoint.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. int reserve_bp_slot(struct perf_event *bp)
  212. {
  213. struct bp_busy_slots slots = {0};
  214. int ret = 0;
  215. mutex_lock(&nr_bp_mutex);
  216. fetch_bp_busy_slots(&slots, bp);
  217. /* Flexible counters need to keep at least one slot */
  218. if (slots.pinned + (!!slots.flexible) == HBP_NUM) {
  219. ret = -ENOSPC;
  220. goto end;
  221. }
  222. toggle_bp_slot(bp, true);
  223. end:
  224. mutex_unlock(&nr_bp_mutex);
  225. return ret;
  226. }
  227. void release_bp_slot(struct perf_event *bp)
  228. {
  229. mutex_lock(&nr_bp_mutex);
  230. toggle_bp_slot(bp, false);
  231. mutex_unlock(&nr_bp_mutex);
  232. }
  233. int register_perf_hw_breakpoint(struct perf_event *bp)
  234. {
  235. int ret;
  236. ret = reserve_bp_slot(bp);
  237. if (ret)
  238. return ret;
  239. /*
  240. * Ptrace breakpoints can be temporary perf events only
  241. * meant to reserve a slot. In this case, it is created disabled and
  242. * we don't want to check the params right now (as we put a null addr)
  243. * But perf tools create events as disabled and we want to check
  244. * the params for them.
  245. * This is a quick hack that will be removed soon, once we remove
  246. * the tmp breakpoints from ptrace
  247. */
  248. if (!bp->attr.disabled || !bp->overflow_handler)
  249. ret = arch_validate_hwbkpt_settings(bp, bp->ctx->task);
  250. return ret;
  251. }
  252. /**
  253. * register_user_hw_breakpoint - register a hardware breakpoint for user space
  254. * @attr: breakpoint attributes
  255. * @triggered: callback to trigger when we hit the breakpoint
  256. * @tsk: pointer to 'task_struct' of the process to which the address belongs
  257. */
  258. struct perf_event *
  259. register_user_hw_breakpoint(struct perf_event_attr *attr,
  260. perf_overflow_handler_t triggered,
  261. struct task_struct *tsk)
  262. {
  263. return perf_event_create_kernel_counter(attr, -1, tsk->pid, triggered);
  264. }
  265. EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
  266. /**
  267. * modify_user_hw_breakpoint - modify a user-space hardware breakpoint
  268. * @bp: the breakpoint structure to modify
  269. * @attr: new breakpoint attributes
  270. * @triggered: callback to trigger when we hit the breakpoint
  271. * @tsk: pointer to 'task_struct' of the process to which the address belongs
  272. */
  273. int modify_user_hw_breakpoint(struct perf_event *bp, struct perf_event_attr *attr)
  274. {
  275. u64 old_addr = bp->attr.bp_addr;
  276. int old_type = bp->attr.bp_type;
  277. int old_len = bp->attr.bp_len;
  278. int err = 0;
  279. perf_event_disable(bp);
  280. bp->attr.bp_addr = attr->bp_addr;
  281. bp->attr.bp_type = attr->bp_type;
  282. bp->attr.bp_len = attr->bp_len;
  283. if (attr->disabled)
  284. goto end;
  285. err = arch_validate_hwbkpt_settings(bp, bp->ctx->task);
  286. if (!err)
  287. perf_event_enable(bp);
  288. if (err) {
  289. bp->attr.bp_addr = old_addr;
  290. bp->attr.bp_type = old_type;
  291. bp->attr.bp_len = old_len;
  292. if (!bp->attr.disabled)
  293. perf_event_enable(bp);
  294. return err;
  295. }
  296. end:
  297. bp->attr.disabled = attr->disabled;
  298. return 0;
  299. }
  300. EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint);
  301. /**
  302. * unregister_hw_breakpoint - unregister a user-space hardware breakpoint
  303. * @bp: the breakpoint structure to unregister
  304. */
  305. void unregister_hw_breakpoint(struct perf_event *bp)
  306. {
  307. if (!bp)
  308. return;
  309. perf_event_release_kernel(bp);
  310. }
  311. EXPORT_SYMBOL_GPL(unregister_hw_breakpoint);
  312. /**
  313. * register_wide_hw_breakpoint - register a wide breakpoint in the kernel
  314. * @attr: breakpoint attributes
  315. * @triggered: callback to trigger when we hit the breakpoint
  316. *
  317. * @return a set of per_cpu pointers to perf events
  318. */
  319. struct perf_event **
  320. register_wide_hw_breakpoint(struct perf_event_attr *attr,
  321. perf_overflow_handler_t triggered)
  322. {
  323. struct perf_event **cpu_events, **pevent, *bp;
  324. long err;
  325. int cpu;
  326. cpu_events = alloc_percpu(typeof(*cpu_events));
  327. if (!cpu_events)
  328. return ERR_PTR(-ENOMEM);
  329. get_online_cpus();
  330. for_each_online_cpu(cpu) {
  331. pevent = per_cpu_ptr(cpu_events, cpu);
  332. bp = perf_event_create_kernel_counter(attr, cpu, -1, triggered);
  333. *pevent = bp;
  334. if (IS_ERR(bp)) {
  335. err = PTR_ERR(bp);
  336. goto fail;
  337. }
  338. }
  339. put_online_cpus();
  340. return cpu_events;
  341. fail:
  342. for_each_online_cpu(cpu) {
  343. pevent = per_cpu_ptr(cpu_events, cpu);
  344. if (IS_ERR(*pevent))
  345. break;
  346. unregister_hw_breakpoint(*pevent);
  347. }
  348. put_online_cpus();
  349. free_percpu(cpu_events);
  350. return ERR_PTR(err);
  351. }
  352. EXPORT_SYMBOL_GPL(register_wide_hw_breakpoint);
  353. /**
  354. * unregister_wide_hw_breakpoint - unregister a wide breakpoint in the kernel
  355. * @cpu_events: the per cpu set of events to unregister
  356. */
  357. void unregister_wide_hw_breakpoint(struct perf_event **cpu_events)
  358. {
  359. int cpu;
  360. struct perf_event **pevent;
  361. for_each_possible_cpu(cpu) {
  362. pevent = per_cpu_ptr(cpu_events, cpu);
  363. unregister_hw_breakpoint(*pevent);
  364. }
  365. free_percpu(cpu_events);
  366. }
  367. EXPORT_SYMBOL_GPL(unregister_wide_hw_breakpoint);
  368. static struct notifier_block hw_breakpoint_exceptions_nb = {
  369. .notifier_call = hw_breakpoint_exceptions_notify,
  370. /* we need to be notified first */
  371. .priority = 0x7fffffff
  372. };
  373. static int __init init_hw_breakpoint(void)
  374. {
  375. return register_die_notifier(&hw_breakpoint_exceptions_nb);
  376. }
  377. core_initcall(init_hw_breakpoint);
  378. struct pmu perf_ops_bp = {
  379. .enable = arch_install_hw_breakpoint,
  380. .disable = arch_uninstall_hw_breakpoint,
  381. .read = hw_breakpoint_pmu_read,
  382. .unthrottle = hw_breakpoint_pmu_unthrottle
  383. };