hw_breakpoint.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2007 Alan Stern
  4. * Copyright (C) IBM Corporation, 2009
  5. * Copyright (C) 2009, Frederic Weisbecker <fweisbec@gmail.com>
  6. *
  7. * Thanks to Ingo Molnar for his many suggestions.
  8. *
  9. * Authors: Alan Stern <stern@rowland.harvard.edu>
  10. * K.Prasad <prasad@linux.vnet.ibm.com>
  11. * Frederic Weisbecker <fweisbec@gmail.com>
  12. */
  13. /*
  14. * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
  15. * using the CPU's debug registers.
  16. * This file contains the arch-independent routines.
  17. */
  18. #include <linux/hw_breakpoint.h>
  19. #include <linux/atomic.h>
  20. #include <linux/bug.h>
  21. #include <linux/cpu.h>
  22. #include <linux/export.h>
  23. #include <linux/init.h>
  24. #include <linux/irqflags.h>
  25. #include <linux/kdebug.h>
  26. #include <linux/kernel.h>
  27. #include <linux/mutex.h>
  28. #include <linux/notifier.h>
  29. #include <linux/percpu-rwsem.h>
  30. #include <linux/percpu.h>
  31. #include <linux/rhashtable.h>
  32. #include <linux/sched.h>
  33. #include <linux/slab.h>
  34. /*
  35. * Datastructure to track the total uses of N slots across tasks or CPUs;
  36. * bp_slots_histogram::count[N] is the number of assigned N+1 breakpoint slots.
  37. */
  38. struct bp_slots_histogram {
  39. #ifdef hw_breakpoint_slots
  40. atomic_t count[hw_breakpoint_slots(0)];
  41. #else
  42. atomic_t *count;
  43. #endif
  44. };
  45. /*
  46. * Per-CPU constraints data.
  47. */
  48. struct bp_cpuinfo {
  49. /* Number of pinned CPU breakpoints in a CPU. */
  50. unsigned int cpu_pinned;
  51. /* Histogram of pinned task breakpoints in a CPU. */
  52. struct bp_slots_histogram tsk_pinned;
  53. };
  54. static DEFINE_PER_CPU(struct bp_cpuinfo, bp_cpuinfo[TYPE_MAX]);
  55. static struct bp_cpuinfo *get_bp_info(int cpu, enum bp_type_idx type)
  56. {
  57. return per_cpu_ptr(bp_cpuinfo + type, cpu);
  58. }
  59. /* Number of pinned CPU breakpoints globally. */
  60. static struct bp_slots_histogram cpu_pinned[TYPE_MAX];
  61. /* Number of pinned CPU-independent task breakpoints. */
  62. static struct bp_slots_histogram tsk_pinned_all[TYPE_MAX];
  63. /* Keep track of the breakpoints attached to tasks */
  64. static struct rhltable task_bps_ht;
  65. static const struct rhashtable_params task_bps_ht_params = {
  66. .head_offset = offsetof(struct hw_perf_event, bp_list),
  67. .key_offset = offsetof(struct hw_perf_event, target),
  68. .key_len = sizeof_field(struct hw_perf_event, target),
  69. .automatic_shrinking = true,
  70. };
  71. static bool constraints_initialized __ro_after_init;
  72. /*
  73. * Synchronizes accesses to the per-CPU constraints; the locking rules are:
  74. *
  75. * 1. Atomic updates to bp_cpuinfo::tsk_pinned only require a held read-lock
  76. * (due to bp_slots_histogram::count being atomic, no update are lost).
  77. *
  78. * 2. Holding a write-lock is required for computations that require a
  79. * stable snapshot of all bp_cpuinfo::tsk_pinned.
  80. *
  81. * 3. In all other cases, non-atomic accesses require the appropriately held
  82. * lock (read-lock for read-only accesses; write-lock for reads/writes).
  83. */
  84. DEFINE_STATIC_PERCPU_RWSEM(bp_cpuinfo_sem);
  85. /*
  86. * Return mutex to serialize accesses to per-task lists in task_bps_ht. Since
  87. * rhltable synchronizes concurrent insertions/deletions, independent tasks may
  88. * insert/delete concurrently; therefore, a mutex per task is sufficient.
  89. *
  90. * Uses task_struct::perf_event_mutex, to avoid extending task_struct with a
  91. * hw_breakpoint-only mutex, which may be infrequently used. The caveat here is
  92. * that hw_breakpoint may contend with per-task perf event list management. The
  93. * assumption is that perf usecases involving hw_breakpoints are very unlikely
  94. * to result in unnecessary contention.
  95. */
  96. static inline struct mutex *get_task_bps_mutex(struct perf_event *bp)
  97. {
  98. struct task_struct *tsk = bp->hw.target;
  99. return tsk ? &tsk->perf_event_mutex : NULL;
  100. }
  101. static struct mutex *bp_constraints_lock(struct perf_event *bp)
  102. {
  103. struct mutex *tsk_mtx = get_task_bps_mutex(bp);
  104. if (tsk_mtx) {
  105. /*
  106. * Fully analogous to the perf_try_init_event() nesting
  107. * argument in the comment near perf_event_ctx_lock_nested();
  108. * this child->perf_event_mutex cannot ever deadlock against
  109. * the parent->perf_event_mutex usage from
  110. * perf_event_task_{en,dis}able().
  111. *
  112. * Specifically, inherited events will never occur on
  113. * ->perf_event_list.
  114. */
  115. mutex_lock_nested(tsk_mtx, SINGLE_DEPTH_NESTING);
  116. percpu_down_read(&bp_cpuinfo_sem);
  117. } else {
  118. percpu_down_write(&bp_cpuinfo_sem);
  119. }
  120. return tsk_mtx;
  121. }
  122. static void bp_constraints_unlock(struct mutex *tsk_mtx)
  123. {
  124. if (tsk_mtx) {
  125. percpu_up_read(&bp_cpuinfo_sem);
  126. mutex_unlock(tsk_mtx);
  127. } else {
  128. percpu_up_write(&bp_cpuinfo_sem);
  129. }
  130. }
  131. static bool bp_constraints_is_locked(struct perf_event *bp)
  132. {
  133. struct mutex *tsk_mtx = get_task_bps_mutex(bp);
  134. return percpu_is_write_locked(&bp_cpuinfo_sem) ||
  135. (tsk_mtx ? mutex_is_locked(tsk_mtx) :
  136. percpu_is_read_locked(&bp_cpuinfo_sem));
  137. }
  138. static inline void assert_bp_constraints_lock_held(struct perf_event *bp)
  139. {
  140. struct mutex *tsk_mtx = get_task_bps_mutex(bp);
  141. if (tsk_mtx)
  142. lockdep_assert_held(tsk_mtx);
  143. lockdep_assert_held(&bp_cpuinfo_sem);
  144. }
  145. #ifdef hw_breakpoint_slots
  146. /*
  147. * Number of breakpoint slots is constant, and the same for all types.
  148. */
  149. static_assert(hw_breakpoint_slots(TYPE_INST) == hw_breakpoint_slots(TYPE_DATA));
  150. static inline int hw_breakpoint_slots_cached(int type) { return hw_breakpoint_slots(type); }
  151. static inline int init_breakpoint_slots(void) { return 0; }
  152. #else
  153. /*
  154. * Dynamic number of breakpoint slots.
  155. */
  156. static int __nr_bp_slots[TYPE_MAX] __ro_after_init;
  157. static inline int hw_breakpoint_slots_cached(int type)
  158. {
  159. return __nr_bp_slots[type];
  160. }
  161. static __init bool
  162. bp_slots_histogram_alloc(struct bp_slots_histogram *hist, enum bp_type_idx type)
  163. {
  164. hist->count = kcalloc(hw_breakpoint_slots_cached(type), sizeof(*hist->count), GFP_KERNEL);
  165. return hist->count;
  166. }
  167. static __init void bp_slots_histogram_free(struct bp_slots_histogram *hist)
  168. {
  169. kfree(hist->count);
  170. }
  171. static __init int init_breakpoint_slots(void)
  172. {
  173. int i, cpu, err_cpu;
  174. for (i = 0; i < TYPE_MAX; i++)
  175. __nr_bp_slots[i] = hw_breakpoint_slots(i);
  176. for_each_possible_cpu(cpu) {
  177. for (i = 0; i < TYPE_MAX; i++) {
  178. struct bp_cpuinfo *info = get_bp_info(cpu, i);
  179. if (!bp_slots_histogram_alloc(&info->tsk_pinned, i))
  180. goto err;
  181. }
  182. }
  183. for (i = 0; i < TYPE_MAX; i++) {
  184. if (!bp_slots_histogram_alloc(&cpu_pinned[i], i))
  185. goto err;
  186. if (!bp_slots_histogram_alloc(&tsk_pinned_all[i], i))
  187. goto err;
  188. }
  189. return 0;
  190. err:
  191. for_each_possible_cpu(err_cpu) {
  192. for (i = 0; i < TYPE_MAX; i++)
  193. bp_slots_histogram_free(&get_bp_info(err_cpu, i)->tsk_pinned);
  194. if (err_cpu == cpu)
  195. break;
  196. }
  197. for (i = 0; i < TYPE_MAX; i++) {
  198. bp_slots_histogram_free(&cpu_pinned[i]);
  199. bp_slots_histogram_free(&tsk_pinned_all[i]);
  200. }
  201. return -ENOMEM;
  202. }
  203. #endif
  204. static inline void
  205. bp_slots_histogram_add(struct bp_slots_histogram *hist, int old, int val)
  206. {
  207. const int old_idx = old - 1;
  208. const int new_idx = old_idx + val;
  209. if (old_idx >= 0)
  210. WARN_ON(atomic_dec_return_relaxed(&hist->count[old_idx]) < 0);
  211. if (new_idx >= 0)
  212. WARN_ON(atomic_inc_return_relaxed(&hist->count[new_idx]) < 0);
  213. }
  214. static int
  215. bp_slots_histogram_max(struct bp_slots_histogram *hist, enum bp_type_idx type)
  216. {
  217. for (int i = hw_breakpoint_slots_cached(type) - 1; i >= 0; i--) {
  218. const int count = atomic_read(&hist->count[i]);
  219. /* Catch unexpected writers; we want a stable snapshot. */
  220. ASSERT_EXCLUSIVE_WRITER(hist->count[i]);
  221. if (count > 0)
  222. return i + 1;
  223. WARN(count < 0, "inconsistent breakpoint slots histogram");
  224. }
  225. return 0;
  226. }
  227. static int
  228. bp_slots_histogram_max_merge(struct bp_slots_histogram *hist1, struct bp_slots_histogram *hist2,
  229. enum bp_type_idx type)
  230. {
  231. for (int i = hw_breakpoint_slots_cached(type) - 1; i >= 0; i--) {
  232. const int count1 = atomic_read(&hist1->count[i]);
  233. const int count2 = atomic_read(&hist2->count[i]);
  234. /* Catch unexpected writers; we want a stable snapshot. */
  235. ASSERT_EXCLUSIVE_WRITER(hist1->count[i]);
  236. ASSERT_EXCLUSIVE_WRITER(hist2->count[i]);
  237. if (count1 + count2 > 0)
  238. return i + 1;
  239. WARN(count1 < 0, "inconsistent breakpoint slots histogram");
  240. WARN(count2 < 0, "inconsistent breakpoint slots histogram");
  241. }
  242. return 0;
  243. }
  244. #ifndef hw_breakpoint_weight
  245. static inline int hw_breakpoint_weight(struct perf_event *bp)
  246. {
  247. return 1;
  248. }
  249. #endif
  250. static inline enum bp_type_idx find_slot_idx(u64 bp_type)
  251. {
  252. if (bp_type & HW_BREAKPOINT_RW)
  253. return TYPE_DATA;
  254. return TYPE_INST;
  255. }
  256. /*
  257. * Return the maximum number of pinned breakpoints a task has in this CPU.
  258. */
  259. static unsigned int max_task_bp_pinned(int cpu, enum bp_type_idx type)
  260. {
  261. struct bp_slots_histogram *tsk_pinned = &get_bp_info(cpu, type)->tsk_pinned;
  262. /*
  263. * At this point we want to have acquired the bp_cpuinfo_sem as a
  264. * writer to ensure that there are no concurrent writers in
  265. * toggle_bp_task_slot() to tsk_pinned, and we get a stable snapshot.
  266. */
  267. lockdep_assert_held_write(&bp_cpuinfo_sem);
  268. return bp_slots_histogram_max_merge(tsk_pinned, &tsk_pinned_all[type], type);
  269. }
  270. /*
  271. * Count the number of breakpoints of the same type and same task.
  272. * The given event must be not on the list.
  273. *
  274. * If @cpu is -1, but the result of task_bp_pinned() is not CPU-independent,
  275. * returns a negative value.
  276. */
  277. static int task_bp_pinned(int cpu, struct perf_event *bp, enum bp_type_idx type)
  278. {
  279. struct rhlist_head *head, *pos;
  280. struct perf_event *iter;
  281. int count = 0;
  282. /*
  283. * We need a stable snapshot of the per-task breakpoint list.
  284. */
  285. assert_bp_constraints_lock_held(bp);
  286. rcu_read_lock();
  287. head = rhltable_lookup(&task_bps_ht, &bp->hw.target, task_bps_ht_params);
  288. if (!head)
  289. goto out;
  290. rhl_for_each_entry_rcu(iter, pos, head, hw.bp_list) {
  291. if (find_slot_idx(iter->attr.bp_type) != type)
  292. continue;
  293. if (iter->cpu >= 0) {
  294. if (cpu == -1) {
  295. count = -1;
  296. goto out;
  297. } else if (cpu != iter->cpu)
  298. continue;
  299. }
  300. count += hw_breakpoint_weight(iter);
  301. }
  302. out:
  303. rcu_read_unlock();
  304. return count;
  305. }
  306. static const struct cpumask *cpumask_of_bp(struct perf_event *bp)
  307. {
  308. if (bp->cpu >= 0)
  309. return cpumask_of(bp->cpu);
  310. return cpu_possible_mask;
  311. }
  312. /*
  313. * Returns the max pinned breakpoint slots in a given
  314. * CPU (cpu > -1) or across all of them (cpu = -1).
  315. */
  316. static int
  317. max_bp_pinned_slots(struct perf_event *bp, enum bp_type_idx type)
  318. {
  319. const struct cpumask *cpumask = cpumask_of_bp(bp);
  320. int pinned_slots = 0;
  321. int cpu;
  322. if (bp->hw.target && bp->cpu < 0) {
  323. int max_pinned = task_bp_pinned(-1, bp, type);
  324. if (max_pinned >= 0) {
  325. /*
  326. * Fast path: task_bp_pinned() is CPU-independent and
  327. * returns the same value for any CPU.
  328. */
  329. max_pinned += bp_slots_histogram_max(&cpu_pinned[type], type);
  330. return max_pinned;
  331. }
  332. }
  333. for_each_cpu(cpu, cpumask) {
  334. struct bp_cpuinfo *info = get_bp_info(cpu, type);
  335. int nr;
  336. nr = info->cpu_pinned;
  337. if (!bp->hw.target)
  338. nr += max_task_bp_pinned(cpu, type);
  339. else
  340. nr += task_bp_pinned(cpu, bp, type);
  341. pinned_slots = max(nr, pinned_slots);
  342. }
  343. return pinned_slots;
  344. }
  345. /*
  346. * Add/remove the given breakpoint in our constraint table
  347. */
  348. static int
  349. toggle_bp_slot(struct perf_event *bp, bool enable, enum bp_type_idx type, int weight)
  350. {
  351. int cpu, next_tsk_pinned;
  352. if (!enable)
  353. weight = -weight;
  354. if (!bp->hw.target) {
  355. /*
  356. * Update the pinned CPU slots, in per-CPU bp_cpuinfo and in the
  357. * global histogram.
  358. */
  359. struct bp_cpuinfo *info = get_bp_info(bp->cpu, type);
  360. lockdep_assert_held_write(&bp_cpuinfo_sem);
  361. bp_slots_histogram_add(&cpu_pinned[type], info->cpu_pinned, weight);
  362. info->cpu_pinned += weight;
  363. return 0;
  364. }
  365. /*
  366. * If bp->hw.target, tsk_pinned is only modified, but not used
  367. * otherwise. We can permit concurrent updates as long as there are no
  368. * other uses: having acquired bp_cpuinfo_sem as a reader allows
  369. * concurrent updates here. Uses of tsk_pinned will require acquiring
  370. * bp_cpuinfo_sem as a writer to stabilize tsk_pinned's value.
  371. */
  372. lockdep_assert_held_read(&bp_cpuinfo_sem);
  373. /*
  374. * Update the pinned task slots, in per-CPU bp_cpuinfo and in the global
  375. * histogram. We need to take care of 4 cases:
  376. *
  377. * 1. This breakpoint targets all CPUs (cpu < 0), and there may only
  378. * exist other task breakpoints targeting all CPUs. In this case we
  379. * can simply update the global slots histogram.
  380. *
  381. * 2. This breakpoint targets a specific CPU (cpu >= 0), but there may
  382. * only exist other task breakpoints targeting all CPUs.
  383. *
  384. * a. On enable: remove the existing breakpoints from the global
  385. * slots histogram and use the per-CPU histogram.
  386. *
  387. * b. On disable: re-insert the existing breakpoints into the global
  388. * slots histogram and remove from per-CPU histogram.
  389. *
  390. * 3. Some other existing task breakpoints target specific CPUs. Only
  391. * update the per-CPU slots histogram.
  392. */
  393. if (!enable) {
  394. /*
  395. * Remove before updating histograms so we can determine if this
  396. * was the last task breakpoint for a specific CPU.
  397. */
  398. int ret = rhltable_remove(&task_bps_ht, &bp->hw.bp_list, task_bps_ht_params);
  399. if (ret)
  400. return ret;
  401. }
  402. /*
  403. * Note: If !enable, next_tsk_pinned will not count the to-be-removed breakpoint.
  404. */
  405. next_tsk_pinned = task_bp_pinned(-1, bp, type);
  406. if (next_tsk_pinned >= 0) {
  407. if (bp->cpu < 0) { /* Case 1: fast path */
  408. if (!enable)
  409. next_tsk_pinned += hw_breakpoint_weight(bp);
  410. bp_slots_histogram_add(&tsk_pinned_all[type], next_tsk_pinned, weight);
  411. } else if (enable) { /* Case 2.a: slow path */
  412. /* Add existing to per-CPU histograms. */
  413. for_each_possible_cpu(cpu) {
  414. bp_slots_histogram_add(&get_bp_info(cpu, type)->tsk_pinned,
  415. 0, next_tsk_pinned);
  416. }
  417. /* Add this first CPU-pinned task breakpoint. */
  418. bp_slots_histogram_add(&get_bp_info(bp->cpu, type)->tsk_pinned,
  419. next_tsk_pinned, weight);
  420. /* Rebalance global task pinned histogram. */
  421. bp_slots_histogram_add(&tsk_pinned_all[type], next_tsk_pinned,
  422. -next_tsk_pinned);
  423. } else { /* Case 2.b: slow path */
  424. /* Remove this last CPU-pinned task breakpoint. */
  425. bp_slots_histogram_add(&get_bp_info(bp->cpu, type)->tsk_pinned,
  426. next_tsk_pinned + hw_breakpoint_weight(bp), weight);
  427. /* Remove all from per-CPU histograms. */
  428. for_each_possible_cpu(cpu) {
  429. bp_slots_histogram_add(&get_bp_info(cpu, type)->tsk_pinned,
  430. next_tsk_pinned, -next_tsk_pinned);
  431. }
  432. /* Rebalance global task pinned histogram. */
  433. bp_slots_histogram_add(&tsk_pinned_all[type], 0, next_tsk_pinned);
  434. }
  435. } else { /* Case 3: slow path */
  436. const struct cpumask *cpumask = cpumask_of_bp(bp);
  437. for_each_cpu(cpu, cpumask) {
  438. next_tsk_pinned = task_bp_pinned(cpu, bp, type);
  439. if (!enable)
  440. next_tsk_pinned += hw_breakpoint_weight(bp);
  441. bp_slots_histogram_add(&get_bp_info(cpu, type)->tsk_pinned,
  442. next_tsk_pinned, weight);
  443. }
  444. }
  445. /*
  446. * Readers want a stable snapshot of the per-task breakpoint list.
  447. */
  448. assert_bp_constraints_lock_held(bp);
  449. if (enable)
  450. return rhltable_insert(&task_bps_ht, &bp->hw.bp_list, task_bps_ht_params);
  451. return 0;
  452. }
  453. /*
  454. * Constraints to check before allowing this new breakpoint counter.
  455. *
  456. * Note: Flexible breakpoints are currently unimplemented, but outlined in the
  457. * below algorithm for completeness. The implementation treats flexible as
  458. * pinned due to no guarantee that we currently always schedule flexible events
  459. * before a pinned event in a same CPU.
  460. *
  461. * == Non-pinned counter == (Considered as pinned for now)
  462. *
  463. * - If attached to a single cpu, check:
  464. *
  465. * (per_cpu(info->flexible, cpu) || (per_cpu(info->cpu_pinned, cpu)
  466. * + max(per_cpu(info->tsk_pinned, cpu)))) < HBP_NUM
  467. *
  468. * -> If there are already non-pinned counters in this cpu, it means
  469. * there is already a free slot for them.
  470. * Otherwise, we check that the maximum number of per task
  471. * breakpoints (for this cpu) plus the number of per cpu breakpoint
  472. * (for this cpu) doesn't cover every registers.
  473. *
  474. * - If attached to every cpus, check:
  475. *
  476. * (per_cpu(info->flexible, *) || (max(per_cpu(info->cpu_pinned, *))
  477. * + max(per_cpu(info->tsk_pinned, *)))) < HBP_NUM
  478. *
  479. * -> This is roughly the same, except we check the number of per cpu
  480. * bp for every cpu and we keep the max one. Same for the per tasks
  481. * breakpoints.
  482. *
  483. *
  484. * == Pinned counter ==
  485. *
  486. * - If attached to a single cpu, check:
  487. *
  488. * ((per_cpu(info->flexible, cpu) > 1) + per_cpu(info->cpu_pinned, cpu)
  489. * + max(per_cpu(info->tsk_pinned, cpu))) < HBP_NUM
  490. *
  491. * -> Same checks as before. But now the info->flexible, if any, must keep
  492. * one register at least (or they will never be fed).
  493. *
  494. * - If attached to every cpus, check:
  495. *
  496. * ((per_cpu(info->flexible, *) > 1) + max(per_cpu(info->cpu_pinned, *))
  497. * + max(per_cpu(info->tsk_pinned, *))) < HBP_NUM
  498. */
  499. static int __reserve_bp_slot(struct perf_event *bp, u64 bp_type)
  500. {
  501. enum bp_type_idx type;
  502. int max_pinned_slots;
  503. int weight;
  504. /* We couldn't initialize breakpoint constraints on boot */
  505. if (!constraints_initialized)
  506. return -ENOMEM;
  507. /* Basic checks */
  508. if (bp_type == HW_BREAKPOINT_EMPTY ||
  509. bp_type == HW_BREAKPOINT_INVALID)
  510. return -EINVAL;
  511. type = find_slot_idx(bp_type);
  512. weight = hw_breakpoint_weight(bp);
  513. /* Check if this new breakpoint can be satisfied across all CPUs. */
  514. max_pinned_slots = max_bp_pinned_slots(bp, type) + weight;
  515. if (max_pinned_slots > hw_breakpoint_slots_cached(type))
  516. return -ENOSPC;
  517. return toggle_bp_slot(bp, true, type, weight);
  518. }
  519. int reserve_bp_slot(struct perf_event *bp)
  520. {
  521. struct mutex *mtx = bp_constraints_lock(bp);
  522. int ret = __reserve_bp_slot(bp, bp->attr.bp_type);
  523. bp_constraints_unlock(mtx);
  524. return ret;
  525. }
  526. static void __release_bp_slot(struct perf_event *bp, u64 bp_type)
  527. {
  528. enum bp_type_idx type;
  529. int weight;
  530. type = find_slot_idx(bp_type);
  531. weight = hw_breakpoint_weight(bp);
  532. WARN_ON(toggle_bp_slot(bp, false, type, weight));
  533. }
  534. void release_bp_slot(struct perf_event *bp)
  535. {
  536. struct mutex *mtx = bp_constraints_lock(bp);
  537. __release_bp_slot(bp, bp->attr.bp_type);
  538. bp_constraints_unlock(mtx);
  539. }
  540. static int __modify_bp_slot(struct perf_event *bp, u64 old_type, u64 new_type)
  541. {
  542. int err;
  543. __release_bp_slot(bp, old_type);
  544. err = __reserve_bp_slot(bp, new_type);
  545. if (err) {
  546. /*
  547. * Reserve the old_type slot back in case
  548. * there's no space for the new type.
  549. *
  550. * This must succeed, because we just released
  551. * the old_type slot in the __release_bp_slot
  552. * call above. If not, something is broken.
  553. */
  554. WARN_ON(__reserve_bp_slot(bp, old_type));
  555. }
  556. return err;
  557. }
  558. static int modify_bp_slot(struct perf_event *bp, u64 old_type, u64 new_type)
  559. {
  560. struct mutex *mtx = bp_constraints_lock(bp);
  561. int ret = __modify_bp_slot(bp, old_type, new_type);
  562. bp_constraints_unlock(mtx);
  563. return ret;
  564. }
  565. /*
  566. * Allow the kernel debugger to reserve breakpoint slots without
  567. * taking a lock using the dbg_* variant of for the reserve and
  568. * release breakpoint slots.
  569. */
  570. int dbg_reserve_bp_slot(struct perf_event *bp)
  571. {
  572. int ret;
  573. if (bp_constraints_is_locked(bp))
  574. return -1;
  575. /* Locks aren't held; disable lockdep assert checking. */
  576. lockdep_off();
  577. ret = __reserve_bp_slot(bp, bp->attr.bp_type);
  578. lockdep_on();
  579. return ret;
  580. }
  581. int dbg_release_bp_slot(struct perf_event *bp)
  582. {
  583. if (bp_constraints_is_locked(bp))
  584. return -1;
  585. /* Locks aren't held; disable lockdep assert checking. */
  586. lockdep_off();
  587. __release_bp_slot(bp, bp->attr.bp_type);
  588. lockdep_on();
  589. return 0;
  590. }
  591. static int hw_breakpoint_parse(struct perf_event *bp,
  592. const struct perf_event_attr *attr,
  593. struct arch_hw_breakpoint *hw)
  594. {
  595. int err;
  596. err = hw_breakpoint_arch_parse(bp, attr, hw);
  597. if (err)
  598. return err;
  599. if (arch_check_bp_in_kernelspace(hw)) {
  600. if (attr->exclude_kernel)
  601. return -EINVAL;
  602. /*
  603. * Don't let unprivileged users set a breakpoint in the trap
  604. * path to avoid trap recursion attacks.
  605. */
  606. if (!capable(CAP_SYS_ADMIN))
  607. return -EPERM;
  608. }
  609. return 0;
  610. }
  611. int register_perf_hw_breakpoint(struct perf_event *bp)
  612. {
  613. struct arch_hw_breakpoint hw = { };
  614. int err;
  615. err = reserve_bp_slot(bp);
  616. if (err)
  617. return err;
  618. err = hw_breakpoint_parse(bp, &bp->attr, &hw);
  619. if (err) {
  620. release_bp_slot(bp);
  621. return err;
  622. }
  623. bp->hw.info = hw;
  624. return 0;
  625. }
  626. /**
  627. * register_user_hw_breakpoint - register a hardware breakpoint for user space
  628. * @attr: breakpoint attributes
  629. * @triggered: callback to trigger when we hit the breakpoint
  630. * @context: context data could be used in the triggered callback
  631. * @tsk: pointer to 'task_struct' of the process to which the address belongs
  632. */
  633. struct perf_event *
  634. register_user_hw_breakpoint(struct perf_event_attr *attr,
  635. perf_overflow_handler_t triggered,
  636. void *context,
  637. struct task_struct *tsk)
  638. {
  639. return perf_event_create_kernel_counter(attr, -1, tsk, triggered,
  640. context);
  641. }
  642. EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
  643. static void hw_breakpoint_copy_attr(struct perf_event_attr *to,
  644. struct perf_event_attr *from)
  645. {
  646. to->bp_addr = from->bp_addr;
  647. to->bp_type = from->bp_type;
  648. to->bp_len = from->bp_len;
  649. to->disabled = from->disabled;
  650. }
  651. int
  652. modify_user_hw_breakpoint_check(struct perf_event *bp, struct perf_event_attr *attr,
  653. bool check)
  654. {
  655. struct arch_hw_breakpoint hw = { };
  656. int err;
  657. err = hw_breakpoint_parse(bp, attr, &hw);
  658. if (err)
  659. return err;
  660. if (check) {
  661. struct perf_event_attr old_attr;
  662. old_attr = bp->attr;
  663. hw_breakpoint_copy_attr(&old_attr, attr);
  664. if (memcmp(&old_attr, attr, sizeof(*attr)))
  665. return -EINVAL;
  666. }
  667. if (bp->attr.bp_type != attr->bp_type) {
  668. err = modify_bp_slot(bp, bp->attr.bp_type, attr->bp_type);
  669. if (err)
  670. return err;
  671. }
  672. hw_breakpoint_copy_attr(&bp->attr, attr);
  673. bp->hw.info = hw;
  674. return 0;
  675. }
  676. /**
  677. * modify_user_hw_breakpoint - modify a user-space hardware breakpoint
  678. * @bp: the breakpoint structure to modify
  679. * @attr: new breakpoint attributes
  680. */
  681. int modify_user_hw_breakpoint(struct perf_event *bp, struct perf_event_attr *attr)
  682. {
  683. int err;
  684. /*
  685. * modify_user_hw_breakpoint can be invoked with IRQs disabled and hence it
  686. * will not be possible to raise IPIs that invoke __perf_event_disable.
  687. * So call the function directly after making sure we are targeting the
  688. * current task.
  689. */
  690. if (irqs_disabled() && bp->ctx && bp->ctx->task == current)
  691. perf_event_disable_local(bp);
  692. else
  693. perf_event_disable(bp);
  694. err = modify_user_hw_breakpoint_check(bp, attr, false);
  695. if (!bp->attr.disabled)
  696. perf_event_enable(bp);
  697. return err;
  698. }
  699. EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint);
  700. /**
  701. * unregister_hw_breakpoint - unregister a user-space hardware breakpoint
  702. * @bp: the breakpoint structure to unregister
  703. */
  704. void unregister_hw_breakpoint(struct perf_event *bp)
  705. {
  706. if (!bp)
  707. return;
  708. perf_event_release_kernel(bp);
  709. }
  710. EXPORT_SYMBOL_GPL(unregister_hw_breakpoint);
  711. /**
  712. * register_wide_hw_breakpoint - register a wide breakpoint in the kernel
  713. * @attr: breakpoint attributes
  714. * @triggered: callback to trigger when we hit the breakpoint
  715. * @context: context data could be used in the triggered callback
  716. *
  717. * @return a set of per_cpu pointers to perf events
  718. */
  719. struct perf_event * __percpu *
  720. register_wide_hw_breakpoint(struct perf_event_attr *attr,
  721. perf_overflow_handler_t triggered,
  722. void *context)
  723. {
  724. struct perf_event * __percpu *cpu_events, *bp;
  725. long err = 0;
  726. int cpu;
  727. cpu_events = alloc_percpu(typeof(*cpu_events));
  728. if (!cpu_events)
  729. return (void __percpu __force *)ERR_PTR(-ENOMEM);
  730. cpus_read_lock();
  731. for_each_online_cpu(cpu) {
  732. bp = perf_event_create_kernel_counter(attr, cpu, NULL,
  733. triggered, context);
  734. if (IS_ERR(bp)) {
  735. err = PTR_ERR(bp);
  736. break;
  737. }
  738. per_cpu(*cpu_events, cpu) = bp;
  739. }
  740. cpus_read_unlock();
  741. if (likely(!err))
  742. return cpu_events;
  743. unregister_wide_hw_breakpoint(cpu_events);
  744. return (void __percpu __force *)ERR_PTR(err);
  745. }
  746. EXPORT_SYMBOL_GPL(register_wide_hw_breakpoint);
  747. /**
  748. * unregister_wide_hw_breakpoint - unregister a wide breakpoint in the kernel
  749. * @cpu_events: the per cpu set of events to unregister
  750. */
  751. void unregister_wide_hw_breakpoint(struct perf_event * __percpu *cpu_events)
  752. {
  753. int cpu;
  754. for_each_possible_cpu(cpu)
  755. unregister_hw_breakpoint(per_cpu(*cpu_events, cpu));
  756. free_percpu(cpu_events);
  757. }
  758. EXPORT_SYMBOL_GPL(unregister_wide_hw_breakpoint);
  759. /**
  760. * hw_breakpoint_is_used - check if breakpoints are currently used
  761. *
  762. * Returns: true if breakpoints are used, false otherwise.
  763. */
  764. bool hw_breakpoint_is_used(void)
  765. {
  766. int cpu;
  767. if (!constraints_initialized)
  768. return false;
  769. for_each_possible_cpu(cpu) {
  770. for (int type = 0; type < TYPE_MAX; ++type) {
  771. struct bp_cpuinfo *info = get_bp_info(cpu, type);
  772. if (info->cpu_pinned)
  773. return true;
  774. for (int slot = 0; slot < hw_breakpoint_slots_cached(type); ++slot) {
  775. if (atomic_read(&info->tsk_pinned.count[slot]))
  776. return true;
  777. }
  778. }
  779. }
  780. for (int type = 0; type < TYPE_MAX; ++type) {
  781. for (int slot = 0; slot < hw_breakpoint_slots_cached(type); ++slot) {
  782. /*
  783. * Warn, because if there are CPU pinned counters,
  784. * should never get here; bp_cpuinfo::cpu_pinned should
  785. * be consistent with the global cpu_pinned histogram.
  786. */
  787. if (WARN_ON(atomic_read(&cpu_pinned[type].count[slot])))
  788. return true;
  789. if (atomic_read(&tsk_pinned_all[type].count[slot]))
  790. return true;
  791. }
  792. }
  793. return false;
  794. }
  795. static struct notifier_block hw_breakpoint_exceptions_nb = {
  796. .notifier_call = hw_breakpoint_exceptions_notify,
  797. /* we need to be notified first */
  798. .priority = 0x7fffffff
  799. };
  800. static void bp_perf_event_destroy(struct perf_event *event)
  801. {
  802. release_bp_slot(event);
  803. }
  804. static int hw_breakpoint_event_init(struct perf_event *bp)
  805. {
  806. int err;
  807. if (bp->attr.type != PERF_TYPE_BREAKPOINT)
  808. return -ENOENT;
  809. /*
  810. * no branch sampling for breakpoint events
  811. */
  812. if (has_branch_stack(bp))
  813. return -EOPNOTSUPP;
  814. err = register_perf_hw_breakpoint(bp);
  815. if (err)
  816. return err;
  817. bp->destroy = bp_perf_event_destroy;
  818. return 0;
  819. }
  820. static int hw_breakpoint_add(struct perf_event *bp, int flags)
  821. {
  822. if (!(flags & PERF_EF_START))
  823. bp->hw.state = PERF_HES_STOPPED;
  824. if (is_sampling_event(bp)) {
  825. bp->hw.last_period = bp->hw.sample_period;
  826. perf_swevent_set_period(bp);
  827. }
  828. return arch_install_hw_breakpoint(bp);
  829. }
  830. static void hw_breakpoint_del(struct perf_event *bp, int flags)
  831. {
  832. arch_uninstall_hw_breakpoint(bp);
  833. }
  834. static void hw_breakpoint_start(struct perf_event *bp, int flags)
  835. {
  836. bp->hw.state = 0;
  837. }
  838. static void hw_breakpoint_stop(struct perf_event *bp, int flags)
  839. {
  840. bp->hw.state = PERF_HES_STOPPED;
  841. }
  842. static struct pmu perf_breakpoint = {
  843. .task_ctx_nr = perf_sw_context, /* could eventually get its own */
  844. .event_init = hw_breakpoint_event_init,
  845. .add = hw_breakpoint_add,
  846. .del = hw_breakpoint_del,
  847. .start = hw_breakpoint_start,
  848. .stop = hw_breakpoint_stop,
  849. .read = hw_breakpoint_pmu_read,
  850. };
  851. int __init init_hw_breakpoint(void)
  852. {
  853. int ret;
  854. ret = rhltable_init(&task_bps_ht, &task_bps_ht_params);
  855. if (ret)
  856. return ret;
  857. ret = init_breakpoint_slots();
  858. if (ret)
  859. return ret;
  860. constraints_initialized = true;
  861. perf_pmu_register(&perf_breakpoint, "breakpoint", PERF_TYPE_BREAKPOINT);
  862. return register_die_notifier(&hw_breakpoint_exceptions_nb);
  863. }