hw_breakpoint.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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/slab.h>
  42. #include <linux/list.h>
  43. #include <linux/cpu.h>
  44. #include <linux/smp.h>
  45. #include <linux/hw_breakpoint.h>
  46. /*
  47. * Constraints data
  48. */
  49. /* Number of pinned cpu breakpoints in a cpu */
  50. static DEFINE_PER_CPU(unsigned int, nr_cpu_bp_pinned[TYPE_MAX]);
  51. /* Number of pinned task breakpoints in a cpu */
  52. static DEFINE_PER_CPU(unsigned int *, nr_task_bp_pinned[TYPE_MAX]);
  53. /* Number of non-pinned cpu/task breakpoints in a cpu */
  54. static DEFINE_PER_CPU(unsigned int, nr_bp_flexible[TYPE_MAX]);
  55. static int nr_slots[TYPE_MAX];
  56. /* Keep track of the breakpoints attached to tasks */
  57. static LIST_HEAD(bp_task_head);
  58. static int constraints_initialized;
  59. /* Gather the number of total pinned and un-pinned bp in a cpuset */
  60. struct bp_busy_slots {
  61. unsigned int pinned;
  62. unsigned int flexible;
  63. };
  64. /* Serialize accesses to the above constraints */
  65. static DEFINE_MUTEX(nr_bp_mutex);
  66. __weak int hw_breakpoint_weight(struct perf_event *bp)
  67. {
  68. return 1;
  69. }
  70. static inline enum bp_type_idx find_slot_idx(struct perf_event *bp)
  71. {
  72. if (bp->attr.bp_type & HW_BREAKPOINT_RW)
  73. return TYPE_DATA;
  74. return TYPE_INST;
  75. }
  76. /*
  77. * Report the maximum number of pinned breakpoints a task
  78. * have in this cpu
  79. */
  80. static unsigned int max_task_bp_pinned(int cpu, enum bp_type_idx type)
  81. {
  82. int i;
  83. unsigned int *tsk_pinned = per_cpu(nr_task_bp_pinned[type], cpu);
  84. for (i = nr_slots[type] - 1; i >= 0; i--) {
  85. if (tsk_pinned[i] > 0)
  86. return i + 1;
  87. }
  88. return 0;
  89. }
  90. /*
  91. * Count the number of breakpoints of the same type and same task.
  92. * The given event must be not on the list.
  93. */
  94. static int task_bp_pinned(struct perf_event *bp, enum bp_type_idx type)
  95. {
  96. struct task_struct *tsk = bp->hw.bp_target;
  97. struct perf_event *iter;
  98. int count = 0;
  99. list_for_each_entry(iter, &bp_task_head, hw.bp_list) {
  100. if (iter->hw.bp_target == tsk && find_slot_idx(iter) == type)
  101. count += hw_breakpoint_weight(iter);
  102. }
  103. return count;
  104. }
  105. /*
  106. * Report the number of pinned/un-pinned breakpoints we have in
  107. * a given cpu (cpu > -1) or in all of them (cpu = -1).
  108. */
  109. static void
  110. fetch_bp_busy_slots(struct bp_busy_slots *slots, struct perf_event *bp,
  111. enum bp_type_idx type)
  112. {
  113. int cpu = bp->cpu;
  114. struct task_struct *tsk = bp->hw.bp_target;
  115. if (cpu >= 0) {
  116. slots->pinned = per_cpu(nr_cpu_bp_pinned[type], cpu);
  117. if (!tsk)
  118. slots->pinned += max_task_bp_pinned(cpu, type);
  119. else
  120. slots->pinned += task_bp_pinned(bp, type);
  121. slots->flexible = per_cpu(nr_bp_flexible[type], cpu);
  122. return;
  123. }
  124. for_each_online_cpu(cpu) {
  125. unsigned int nr;
  126. nr = per_cpu(nr_cpu_bp_pinned[type], cpu);
  127. if (!tsk)
  128. nr += max_task_bp_pinned(cpu, type);
  129. else
  130. nr += task_bp_pinned(bp, type);
  131. if (nr > slots->pinned)
  132. slots->pinned = nr;
  133. nr = per_cpu(nr_bp_flexible[type], cpu);
  134. if (nr > slots->flexible)
  135. slots->flexible = nr;
  136. }
  137. }
  138. /*
  139. * For now, continue to consider flexible as pinned, until we can
  140. * ensure no flexible event can ever be scheduled before a pinned event
  141. * in a same cpu.
  142. */
  143. static void
  144. fetch_this_slot(struct bp_busy_slots *slots, int weight)
  145. {
  146. slots->pinned += weight;
  147. }
  148. /*
  149. * Add a pinned breakpoint for the given task in our constraint table
  150. */
  151. static void toggle_bp_task_slot(struct perf_event *bp, int cpu, bool enable,
  152. enum bp_type_idx type, int weight)
  153. {
  154. unsigned int *tsk_pinned;
  155. int old_count = 0;
  156. int old_idx = 0;
  157. int idx = 0;
  158. old_count = task_bp_pinned(bp, type);
  159. old_idx = old_count - 1;
  160. idx = old_idx + weight;
  161. /* tsk_pinned[n] is the number of tasks having n breakpoints */
  162. tsk_pinned = per_cpu(nr_task_bp_pinned[type], cpu);
  163. if (enable) {
  164. tsk_pinned[idx]++;
  165. if (old_count > 0)
  166. tsk_pinned[old_idx]--;
  167. } else {
  168. tsk_pinned[idx]--;
  169. if (old_count > 0)
  170. tsk_pinned[old_idx]++;
  171. }
  172. }
  173. /*
  174. * Add/remove the given breakpoint in our constraint table
  175. */
  176. static void
  177. toggle_bp_slot(struct perf_event *bp, bool enable, enum bp_type_idx type,
  178. int weight)
  179. {
  180. int cpu = bp->cpu;
  181. struct task_struct *tsk = bp->hw.bp_target;
  182. /* Pinned counter cpu profiling */
  183. if (!tsk) {
  184. if (enable)
  185. per_cpu(nr_cpu_bp_pinned[type], bp->cpu) += weight;
  186. else
  187. per_cpu(nr_cpu_bp_pinned[type], bp->cpu) -= weight;
  188. return;
  189. }
  190. /* Pinned counter task profiling */
  191. if (!enable)
  192. list_del(&bp->hw.bp_list);
  193. if (cpu >= 0) {
  194. toggle_bp_task_slot(bp, cpu, enable, type, weight);
  195. } else {
  196. for_each_online_cpu(cpu)
  197. toggle_bp_task_slot(bp, cpu, enable, type, weight);
  198. }
  199. if (enable)
  200. list_add_tail(&bp->hw.bp_list, &bp_task_head);
  201. }
  202. /*
  203. * Function to perform processor-specific cleanup during unregistration
  204. */
  205. __weak void arch_unregister_hw_breakpoint(struct perf_event *bp)
  206. {
  207. /*
  208. * A weak stub function here for those archs that don't define
  209. * it inside arch/.../kernel/hw_breakpoint.c
  210. */
  211. }
  212. /*
  213. * Contraints to check before allowing this new breakpoint counter:
  214. *
  215. * == Non-pinned counter == (Considered as pinned for now)
  216. *
  217. * - If attached to a single cpu, check:
  218. *
  219. * (per_cpu(nr_bp_flexible, cpu) || (per_cpu(nr_cpu_bp_pinned, cpu)
  220. * + max(per_cpu(nr_task_bp_pinned, cpu)))) < HBP_NUM
  221. *
  222. * -> If there are already non-pinned counters in this cpu, it means
  223. * there is already a free slot for them.
  224. * Otherwise, we check that the maximum number of per task
  225. * breakpoints (for this cpu) plus the number of per cpu breakpoint
  226. * (for this cpu) doesn't cover every registers.
  227. *
  228. * - If attached to every cpus, check:
  229. *
  230. * (per_cpu(nr_bp_flexible, *) || (max(per_cpu(nr_cpu_bp_pinned, *))
  231. * + max(per_cpu(nr_task_bp_pinned, *)))) < HBP_NUM
  232. *
  233. * -> This is roughly the same, except we check the number of per cpu
  234. * bp for every cpu and we keep the max one. Same for the per tasks
  235. * breakpoints.
  236. *
  237. *
  238. * == Pinned counter ==
  239. *
  240. * - If attached to a single cpu, check:
  241. *
  242. * ((per_cpu(nr_bp_flexible, cpu) > 1) + per_cpu(nr_cpu_bp_pinned, cpu)
  243. * + max(per_cpu(nr_task_bp_pinned, cpu))) < HBP_NUM
  244. *
  245. * -> Same checks as before. But now the nr_bp_flexible, if any, must keep
  246. * one register at least (or they will never be fed).
  247. *
  248. * - If attached to every cpus, check:
  249. *
  250. * ((per_cpu(nr_bp_flexible, *) > 1) + max(per_cpu(nr_cpu_bp_pinned, *))
  251. * + max(per_cpu(nr_task_bp_pinned, *))) < HBP_NUM
  252. */
  253. static int __reserve_bp_slot(struct perf_event *bp)
  254. {
  255. struct bp_busy_slots slots = {0};
  256. enum bp_type_idx type;
  257. int weight;
  258. /* We couldn't initialize breakpoint constraints on boot */
  259. if (!constraints_initialized)
  260. return -ENOMEM;
  261. /* Basic checks */
  262. if (bp->attr.bp_type == HW_BREAKPOINT_EMPTY ||
  263. bp->attr.bp_type == HW_BREAKPOINT_INVALID)
  264. return -EINVAL;
  265. type = find_slot_idx(bp);
  266. weight = hw_breakpoint_weight(bp);
  267. fetch_bp_busy_slots(&slots, bp, type);
  268. /*
  269. * Simulate the addition of this breakpoint to the constraints
  270. * and see the result.
  271. */
  272. fetch_this_slot(&slots, weight);
  273. /* Flexible counters need to keep at least one slot */
  274. if (slots.pinned + (!!slots.flexible) > nr_slots[type])
  275. return -ENOSPC;
  276. toggle_bp_slot(bp, true, type, weight);
  277. return 0;
  278. }
  279. int reserve_bp_slot(struct perf_event *bp)
  280. {
  281. int ret;
  282. mutex_lock(&nr_bp_mutex);
  283. ret = __reserve_bp_slot(bp);
  284. mutex_unlock(&nr_bp_mutex);
  285. return ret;
  286. }
  287. static void __release_bp_slot(struct perf_event *bp)
  288. {
  289. enum bp_type_idx type;
  290. int weight;
  291. type = find_slot_idx(bp);
  292. weight = hw_breakpoint_weight(bp);
  293. toggle_bp_slot(bp, false, type, weight);
  294. }
  295. void release_bp_slot(struct perf_event *bp)
  296. {
  297. mutex_lock(&nr_bp_mutex);
  298. arch_unregister_hw_breakpoint(bp);
  299. __release_bp_slot(bp);
  300. mutex_unlock(&nr_bp_mutex);
  301. }
  302. /*
  303. * Allow the kernel debugger to reserve breakpoint slots without
  304. * taking a lock using the dbg_* variant of for the reserve and
  305. * release breakpoint slots.
  306. */
  307. int dbg_reserve_bp_slot(struct perf_event *bp)
  308. {
  309. if (mutex_is_locked(&nr_bp_mutex))
  310. return -1;
  311. return __reserve_bp_slot(bp);
  312. }
  313. int dbg_release_bp_slot(struct perf_event *bp)
  314. {
  315. if (mutex_is_locked(&nr_bp_mutex))
  316. return -1;
  317. __release_bp_slot(bp);
  318. return 0;
  319. }
  320. static int validate_hw_breakpoint(struct perf_event *bp)
  321. {
  322. int ret;
  323. ret = arch_validate_hwbkpt_settings(bp);
  324. if (ret)
  325. return ret;
  326. if (arch_check_bp_in_kernelspace(bp)) {
  327. if (bp->attr.exclude_kernel)
  328. return -EINVAL;
  329. /*
  330. * Don't let unprivileged users set a breakpoint in the trap
  331. * path to avoid trap recursion attacks.
  332. */
  333. if (!capable(CAP_SYS_ADMIN))
  334. return -EPERM;
  335. }
  336. return 0;
  337. }
  338. int register_perf_hw_breakpoint(struct perf_event *bp)
  339. {
  340. int ret;
  341. ret = reserve_bp_slot(bp);
  342. if (ret)
  343. return ret;
  344. ret = validate_hw_breakpoint(bp);
  345. /* if arch_validate_hwbkpt_settings() fails then release bp slot */
  346. if (ret)
  347. release_bp_slot(bp);
  348. return ret;
  349. }
  350. /**
  351. * register_user_hw_breakpoint - register a hardware breakpoint for user space
  352. * @attr: breakpoint attributes
  353. * @triggered: callback to trigger when we hit the breakpoint
  354. * @tsk: pointer to 'task_struct' of the process to which the address belongs
  355. */
  356. struct perf_event *
  357. register_user_hw_breakpoint(struct perf_event_attr *attr,
  358. perf_overflow_handler_t triggered,
  359. struct task_struct *tsk)
  360. {
  361. return perf_event_create_kernel_counter(attr, -1, tsk, triggered);
  362. }
  363. EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
  364. /**
  365. * modify_user_hw_breakpoint - modify a user-space hardware breakpoint
  366. * @bp: the breakpoint structure to modify
  367. * @attr: new breakpoint attributes
  368. * @triggered: callback to trigger when we hit the breakpoint
  369. * @tsk: pointer to 'task_struct' of the process to which the address belongs
  370. */
  371. int modify_user_hw_breakpoint(struct perf_event *bp, struct perf_event_attr *attr)
  372. {
  373. u64 old_addr = bp->attr.bp_addr;
  374. u64 old_len = bp->attr.bp_len;
  375. int old_type = bp->attr.bp_type;
  376. int err = 0;
  377. perf_event_disable(bp);
  378. bp->attr.bp_addr = attr->bp_addr;
  379. bp->attr.bp_type = attr->bp_type;
  380. bp->attr.bp_len = attr->bp_len;
  381. if (attr->disabled)
  382. goto end;
  383. err = validate_hw_breakpoint(bp);
  384. if (!err)
  385. perf_event_enable(bp);
  386. if (err) {
  387. bp->attr.bp_addr = old_addr;
  388. bp->attr.bp_type = old_type;
  389. bp->attr.bp_len = old_len;
  390. if (!bp->attr.disabled)
  391. perf_event_enable(bp);
  392. return err;
  393. }
  394. end:
  395. bp->attr.disabled = attr->disabled;
  396. return 0;
  397. }
  398. EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint);
  399. /**
  400. * unregister_hw_breakpoint - unregister a user-space hardware breakpoint
  401. * @bp: the breakpoint structure to unregister
  402. */
  403. void unregister_hw_breakpoint(struct perf_event *bp)
  404. {
  405. if (!bp)
  406. return;
  407. perf_event_release_kernel(bp);
  408. }
  409. EXPORT_SYMBOL_GPL(unregister_hw_breakpoint);
  410. /**
  411. * register_wide_hw_breakpoint - register a wide breakpoint in the kernel
  412. * @attr: breakpoint attributes
  413. * @triggered: callback to trigger when we hit the breakpoint
  414. *
  415. * @return a set of per_cpu pointers to perf events
  416. */
  417. struct perf_event * __percpu *
  418. register_wide_hw_breakpoint(struct perf_event_attr *attr,
  419. perf_overflow_handler_t triggered)
  420. {
  421. struct perf_event * __percpu *cpu_events, **pevent, *bp;
  422. long err;
  423. int cpu;
  424. cpu_events = alloc_percpu(typeof(*cpu_events));
  425. if (!cpu_events)
  426. return (void __percpu __force *)ERR_PTR(-ENOMEM);
  427. get_online_cpus();
  428. for_each_online_cpu(cpu) {
  429. pevent = per_cpu_ptr(cpu_events, cpu);
  430. bp = perf_event_create_kernel_counter(attr, cpu, NULL, triggered);
  431. *pevent = bp;
  432. if (IS_ERR(bp)) {
  433. err = PTR_ERR(bp);
  434. goto fail;
  435. }
  436. }
  437. put_online_cpus();
  438. return cpu_events;
  439. fail:
  440. for_each_online_cpu(cpu) {
  441. pevent = per_cpu_ptr(cpu_events, cpu);
  442. if (IS_ERR(*pevent))
  443. break;
  444. unregister_hw_breakpoint(*pevent);
  445. }
  446. put_online_cpus();
  447. free_percpu(cpu_events);
  448. return (void __percpu __force *)ERR_PTR(err);
  449. }
  450. EXPORT_SYMBOL_GPL(register_wide_hw_breakpoint);
  451. /**
  452. * unregister_wide_hw_breakpoint - unregister a wide breakpoint in the kernel
  453. * @cpu_events: the per cpu set of events to unregister
  454. */
  455. void unregister_wide_hw_breakpoint(struct perf_event * __percpu *cpu_events)
  456. {
  457. int cpu;
  458. struct perf_event **pevent;
  459. for_each_possible_cpu(cpu) {
  460. pevent = per_cpu_ptr(cpu_events, cpu);
  461. unregister_hw_breakpoint(*pevent);
  462. }
  463. free_percpu(cpu_events);
  464. }
  465. EXPORT_SYMBOL_GPL(unregister_wide_hw_breakpoint);
  466. static struct notifier_block hw_breakpoint_exceptions_nb = {
  467. .notifier_call = hw_breakpoint_exceptions_notify,
  468. /* we need to be notified first */
  469. .priority = 0x7fffffff
  470. };
  471. static void bp_perf_event_destroy(struct perf_event *event)
  472. {
  473. release_bp_slot(event);
  474. }
  475. static int hw_breakpoint_event_init(struct perf_event *bp)
  476. {
  477. int err;
  478. if (bp->attr.type != PERF_TYPE_BREAKPOINT)
  479. return -ENOENT;
  480. err = register_perf_hw_breakpoint(bp);
  481. if (err)
  482. return err;
  483. bp->destroy = bp_perf_event_destroy;
  484. return 0;
  485. }
  486. static int hw_breakpoint_add(struct perf_event *bp, int flags)
  487. {
  488. if (!(flags & PERF_EF_START))
  489. bp->hw.state = PERF_HES_STOPPED;
  490. return arch_install_hw_breakpoint(bp);
  491. }
  492. static void hw_breakpoint_del(struct perf_event *bp, int flags)
  493. {
  494. arch_uninstall_hw_breakpoint(bp);
  495. }
  496. static void hw_breakpoint_start(struct perf_event *bp, int flags)
  497. {
  498. bp->hw.state = 0;
  499. }
  500. static void hw_breakpoint_stop(struct perf_event *bp, int flags)
  501. {
  502. bp->hw.state = PERF_HES_STOPPED;
  503. }
  504. static struct pmu perf_breakpoint = {
  505. .task_ctx_nr = perf_sw_context, /* could eventually get its own */
  506. .event_init = hw_breakpoint_event_init,
  507. .add = hw_breakpoint_add,
  508. .del = hw_breakpoint_del,
  509. .start = hw_breakpoint_start,
  510. .stop = hw_breakpoint_stop,
  511. .read = hw_breakpoint_pmu_read,
  512. };
  513. int __init init_hw_breakpoint(void)
  514. {
  515. unsigned int **task_bp_pinned;
  516. int cpu, err_cpu;
  517. int i;
  518. for (i = 0; i < TYPE_MAX; i++)
  519. nr_slots[i] = hw_breakpoint_slots(i);
  520. for_each_possible_cpu(cpu) {
  521. for (i = 0; i < TYPE_MAX; i++) {
  522. task_bp_pinned = &per_cpu(nr_task_bp_pinned[i], cpu);
  523. *task_bp_pinned = kzalloc(sizeof(int) * nr_slots[i],
  524. GFP_KERNEL);
  525. if (!*task_bp_pinned)
  526. goto err_alloc;
  527. }
  528. }
  529. constraints_initialized = 1;
  530. perf_pmu_register(&perf_breakpoint, "breakpoint", PERF_TYPE_BREAKPOINT);
  531. return register_die_notifier(&hw_breakpoint_exceptions_nb);
  532. err_alloc:
  533. for_each_possible_cpu(err_cpu) {
  534. if (err_cpu == cpu)
  535. break;
  536. for (i = 0; i < TYPE_MAX; i++)
  537. kfree(per_cpu(nr_task_bp_pinned[i], cpu));
  538. }
  539. return -ENOMEM;
  540. }