hw_breakpoint.c 11 KB

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