hw_breakpoint.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 a breakpoint bound to a task and a given cpu.
  245. * If cpu is -1, the breakpoint is active for the task in every cpu
  246. * If the task is -1, the breakpoint is active for every tasks in the given
  247. * cpu.
  248. */
  249. static struct perf_event *
  250. register_user_hw_breakpoint_cpu(unsigned long addr,
  251. int len,
  252. int type,
  253. perf_callback_t triggered,
  254. pid_t pid,
  255. int cpu,
  256. bool active)
  257. {
  258. struct perf_event_attr *attr;
  259. struct perf_event *bp;
  260. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  261. if (!attr)
  262. return ERR_PTR(-ENOMEM);
  263. attr->type = PERF_TYPE_BREAKPOINT;
  264. attr->size = sizeof(*attr);
  265. attr->bp_addr = addr;
  266. attr->bp_len = len;
  267. attr->bp_type = type;
  268. /*
  269. * Such breakpoints are used by debuggers to trigger signals when
  270. * we hit the excepted memory op. We can't miss such events, they
  271. * must be pinned.
  272. */
  273. attr->pinned = 1;
  274. if (!active)
  275. attr->disabled = 1;
  276. bp = perf_event_create_kernel_counter(attr, cpu, pid, triggered);
  277. kfree(attr);
  278. return bp;
  279. }
  280. /**
  281. * register_user_hw_breakpoint - register a hardware breakpoint for user space
  282. * @addr: is the memory address that triggers the breakpoint
  283. * @len: the length of the access to the memory (1 byte, 2 bytes etc...)
  284. * @type: the type of the access to the memory (read/write/exec)
  285. * @triggered: callback to trigger when we hit the breakpoint
  286. * @tsk: pointer to 'task_struct' of the process to which the address belongs
  287. * @active: should we activate it while registering it
  288. *
  289. */
  290. struct perf_event *
  291. register_user_hw_breakpoint(unsigned long addr,
  292. int len,
  293. int type,
  294. perf_callback_t triggered,
  295. struct task_struct *tsk,
  296. bool active)
  297. {
  298. return register_user_hw_breakpoint_cpu(addr, len, type, triggered,
  299. tsk->pid, -1, active);
  300. }
  301. EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
  302. /**
  303. * modify_user_hw_breakpoint - modify a user-space hardware breakpoint
  304. * @bp: the breakpoint structure to modify
  305. * @addr: is the memory address that triggers the breakpoint
  306. * @len: the length of the access to the memory (1 byte, 2 bytes etc...)
  307. * @type: the type of the access to the memory (read/write/exec)
  308. * @triggered: callback to trigger when we hit the breakpoint
  309. * @tsk: pointer to 'task_struct' of the process to which the address belongs
  310. * @active: should we activate it while registering it
  311. */
  312. struct perf_event *
  313. modify_user_hw_breakpoint(struct perf_event *bp,
  314. unsigned long addr,
  315. int len,
  316. int type,
  317. perf_callback_t triggered,
  318. struct task_struct *tsk,
  319. bool active)
  320. {
  321. /*
  322. * FIXME: do it without unregistering
  323. * - We don't want to lose our slot
  324. * - If the new bp is incorrect, don't lose the older one
  325. */
  326. unregister_hw_breakpoint(bp);
  327. return register_user_hw_breakpoint(addr, len, type, triggered,
  328. tsk, active);
  329. }
  330. EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint);
  331. /**
  332. * unregister_hw_breakpoint - unregister a user-space hardware breakpoint
  333. * @bp: the breakpoint structure to unregister
  334. */
  335. void unregister_hw_breakpoint(struct perf_event *bp)
  336. {
  337. if (!bp)
  338. return;
  339. perf_event_release_kernel(bp);
  340. }
  341. EXPORT_SYMBOL_GPL(unregister_hw_breakpoint);
  342. static struct perf_event *
  343. register_kernel_hw_breakpoint_cpu(unsigned long addr,
  344. int len,
  345. int type,
  346. perf_callback_t triggered,
  347. int cpu,
  348. bool active)
  349. {
  350. return register_user_hw_breakpoint_cpu(addr, len, type, triggered,
  351. -1, cpu, active);
  352. }
  353. /**
  354. * register_wide_hw_breakpoint - register a wide breakpoint in the kernel
  355. * @addr: is the memory address that triggers the breakpoint
  356. * @len: the length of the access to the memory (1 byte, 2 bytes etc...)
  357. * @type: the type of the access to the memory (read/write/exec)
  358. * @triggered: callback to trigger when we hit the breakpoint
  359. * @active: should we activate it while registering it
  360. *
  361. * @return a set of per_cpu pointers to perf events
  362. */
  363. struct perf_event **
  364. register_wide_hw_breakpoint(unsigned long addr,
  365. int len,
  366. int type,
  367. perf_callback_t triggered,
  368. bool active)
  369. {
  370. struct perf_event **cpu_events, **pevent, *bp;
  371. long err;
  372. int cpu;
  373. cpu_events = alloc_percpu(typeof(*cpu_events));
  374. if (!cpu_events)
  375. return ERR_PTR(-ENOMEM);
  376. for_each_possible_cpu(cpu) {
  377. pevent = per_cpu_ptr(cpu_events, cpu);
  378. bp = register_kernel_hw_breakpoint_cpu(addr, len, type,
  379. triggered, cpu, active);
  380. *pevent = bp;
  381. if (IS_ERR(bp)) {
  382. err = PTR_ERR(bp);
  383. goto fail;
  384. }
  385. }
  386. return cpu_events;
  387. fail:
  388. for_each_possible_cpu(cpu) {
  389. pevent = per_cpu_ptr(cpu_events, cpu);
  390. if (IS_ERR(*pevent))
  391. break;
  392. unregister_hw_breakpoint(*pevent);
  393. }
  394. free_percpu(cpu_events);
  395. /* return the error if any */
  396. return ERR_PTR(err);
  397. }
  398. EXPORT_SYMBOL_GPL(register_wide_hw_breakpoint);
  399. /**
  400. * unregister_wide_hw_breakpoint - unregister a wide breakpoint in the kernel
  401. * @cpu_events: the per cpu set of events to unregister
  402. */
  403. void unregister_wide_hw_breakpoint(struct perf_event **cpu_events)
  404. {
  405. int cpu;
  406. struct perf_event **pevent;
  407. for_each_possible_cpu(cpu) {
  408. pevent = per_cpu_ptr(cpu_events, cpu);
  409. unregister_hw_breakpoint(*pevent);
  410. }
  411. free_percpu(cpu_events);
  412. }
  413. EXPORT_SYMBOL_GPL(unregister_wide_hw_breakpoint);
  414. static struct notifier_block hw_breakpoint_exceptions_nb = {
  415. .notifier_call = hw_breakpoint_exceptions_notify,
  416. /* we need to be notified first */
  417. .priority = 0x7fffffff
  418. };
  419. static int __init init_hw_breakpoint(void)
  420. {
  421. return register_die_notifier(&hw_breakpoint_exceptions_nb);
  422. }
  423. core_initcall(init_hw_breakpoint);
  424. struct pmu perf_ops_bp = {
  425. .enable = arch_install_hw_breakpoint,
  426. .disable = arch_uninstall_hw_breakpoint,
  427. .read = hw_breakpoint_pmu_read,
  428. .unthrottle = hw_breakpoint_pmu_unthrottle
  429. };