perf_counter.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  1. /*
  2. * Performance counter core code
  3. *
  4. * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
  6. *
  7. * For licencing details see kernel-base/COPYING
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/cpu.h>
  11. #include <linux/smp.h>
  12. #include <linux/file.h>
  13. #include <linux/poll.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/percpu.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/anon_inodes.h>
  20. #include <linux/kernel_stat.h>
  21. #include <linux/perf_counter.h>
  22. /*
  23. * Each CPU has a list of per CPU counters:
  24. */
  25. DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
  26. int perf_max_counters __read_mostly = 1;
  27. static int perf_reserved_percpu __read_mostly;
  28. static int perf_overcommit __read_mostly = 1;
  29. /*
  30. * Mutex for (sysadmin-configurable) counter reservations:
  31. */
  32. static DEFINE_MUTEX(perf_resource_mutex);
  33. /*
  34. * Architecture provided APIs - weak aliases:
  35. */
  36. extern __weak const struct hw_perf_counter_ops *
  37. hw_perf_counter_init(struct perf_counter *counter)
  38. {
  39. return NULL;
  40. }
  41. u64 __weak hw_perf_save_disable(void) { return 0; }
  42. void __weak hw_perf_restore(u64 ctrl) { barrier(); }
  43. void __weak hw_perf_counter_setup(void) { barrier(); }
  44. static void
  45. list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  46. {
  47. struct perf_counter *group_leader = counter->group_leader;
  48. /*
  49. * Depending on whether it is a standalone or sibling counter,
  50. * add it straight to the context's counter list, or to the group
  51. * leader's sibling list:
  52. */
  53. if (counter->group_leader == counter)
  54. list_add_tail(&counter->list_entry, &ctx->counter_list);
  55. else
  56. list_add_tail(&counter->list_entry, &group_leader->sibling_list);
  57. }
  58. static void
  59. list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  60. {
  61. struct perf_counter *sibling, *tmp;
  62. list_del_init(&counter->list_entry);
  63. /*
  64. * If this was a group counter with sibling counters then
  65. * upgrade the siblings to singleton counters by adding them
  66. * to the context list directly:
  67. */
  68. list_for_each_entry_safe(sibling, tmp,
  69. &counter->sibling_list, list_entry) {
  70. list_del_init(&sibling->list_entry);
  71. list_add_tail(&sibling->list_entry, &ctx->counter_list);
  72. sibling->group_leader = sibling;
  73. }
  74. }
  75. /*
  76. * Cross CPU call to remove a performance counter
  77. *
  78. * We disable the counter on the hardware level first. After that we
  79. * remove it from the context list.
  80. */
  81. static void __perf_counter_remove_from_context(void *info)
  82. {
  83. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  84. struct perf_counter *counter = info;
  85. struct perf_counter_context *ctx = counter->ctx;
  86. unsigned long flags;
  87. u64 perf_flags;
  88. /*
  89. * If this is a task context, we need to check whether it is
  90. * the current task context of this cpu. If not it has been
  91. * scheduled out before the smp call arrived.
  92. */
  93. if (ctx->task && cpuctx->task_ctx != ctx)
  94. return;
  95. curr_rq_lock_irq_save(&flags);
  96. spin_lock(&ctx->lock);
  97. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  98. counter->state = PERF_COUNTER_STATE_INACTIVE;
  99. counter->hw_ops->disable(counter);
  100. ctx->nr_active--;
  101. cpuctx->active_oncpu--;
  102. counter->task = NULL;
  103. counter->oncpu = -1;
  104. }
  105. ctx->nr_counters--;
  106. /*
  107. * Protect the list operation against NMI by disabling the
  108. * counters on a global level. NOP for non NMI based counters.
  109. */
  110. perf_flags = hw_perf_save_disable();
  111. list_del_counter(counter, ctx);
  112. hw_perf_restore(perf_flags);
  113. if (!ctx->task) {
  114. /*
  115. * Allow more per task counters with respect to the
  116. * reservation:
  117. */
  118. cpuctx->max_pertask =
  119. min(perf_max_counters - ctx->nr_counters,
  120. perf_max_counters - perf_reserved_percpu);
  121. }
  122. spin_unlock(&ctx->lock);
  123. curr_rq_unlock_irq_restore(&flags);
  124. }
  125. /*
  126. * Remove the counter from a task's (or a CPU's) list of counters.
  127. *
  128. * Must be called with counter->mutex held.
  129. *
  130. * CPU counters are removed with a smp call. For task counters we only
  131. * call when the task is on a CPU.
  132. */
  133. static void perf_counter_remove_from_context(struct perf_counter *counter)
  134. {
  135. struct perf_counter_context *ctx = counter->ctx;
  136. struct task_struct *task = ctx->task;
  137. if (!task) {
  138. /*
  139. * Per cpu counters are removed via an smp call and
  140. * the removal is always sucessful.
  141. */
  142. smp_call_function_single(counter->cpu,
  143. __perf_counter_remove_from_context,
  144. counter, 1);
  145. return;
  146. }
  147. retry:
  148. task_oncpu_function_call(task, __perf_counter_remove_from_context,
  149. counter);
  150. spin_lock_irq(&ctx->lock);
  151. /*
  152. * If the context is active we need to retry the smp call.
  153. */
  154. if (ctx->nr_active && !list_empty(&counter->list_entry)) {
  155. spin_unlock_irq(&ctx->lock);
  156. goto retry;
  157. }
  158. /*
  159. * The lock prevents that this context is scheduled in so we
  160. * can remove the counter safely, if the call above did not
  161. * succeed.
  162. */
  163. if (!list_empty(&counter->list_entry)) {
  164. ctx->nr_counters--;
  165. list_del_counter(counter, ctx);
  166. counter->task = NULL;
  167. }
  168. spin_unlock_irq(&ctx->lock);
  169. }
  170. static int
  171. counter_sched_in(struct perf_counter *counter,
  172. struct perf_cpu_context *cpuctx,
  173. struct perf_counter_context *ctx,
  174. int cpu)
  175. {
  176. if (counter->state == PERF_COUNTER_STATE_OFF)
  177. return 0;
  178. counter->state = PERF_COUNTER_STATE_ACTIVE;
  179. counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
  180. /*
  181. * The new state must be visible before we turn it on in the hardware:
  182. */
  183. smp_wmb();
  184. if (counter->hw_ops->enable(counter)) {
  185. counter->state = PERF_COUNTER_STATE_INACTIVE;
  186. counter->oncpu = -1;
  187. return -EAGAIN;
  188. }
  189. cpuctx->active_oncpu++;
  190. ctx->nr_active++;
  191. return 0;
  192. }
  193. /*
  194. * Cross CPU call to install and enable a performance counter
  195. */
  196. static void __perf_install_in_context(void *info)
  197. {
  198. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  199. struct perf_counter *counter = info;
  200. struct perf_counter_context *ctx = counter->ctx;
  201. int cpu = smp_processor_id();
  202. unsigned long flags;
  203. u64 perf_flags;
  204. /*
  205. * If this is a task context, we need to check whether it is
  206. * the current task context of this cpu. If not it has been
  207. * scheduled out before the smp call arrived.
  208. */
  209. if (ctx->task && cpuctx->task_ctx != ctx)
  210. return;
  211. curr_rq_lock_irq_save(&flags);
  212. spin_lock(&ctx->lock);
  213. /*
  214. * Protect the list operation against NMI by disabling the
  215. * counters on a global level. NOP for non NMI based counters.
  216. */
  217. perf_flags = hw_perf_save_disable();
  218. list_add_counter(counter, ctx);
  219. ctx->nr_counters++;
  220. counter_sched_in(counter, cpuctx, ctx, cpu);
  221. if (!ctx->task && cpuctx->max_pertask)
  222. cpuctx->max_pertask--;
  223. hw_perf_restore(perf_flags);
  224. spin_unlock(&ctx->lock);
  225. curr_rq_unlock_irq_restore(&flags);
  226. }
  227. /*
  228. * Attach a performance counter to a context
  229. *
  230. * First we add the counter to the list with the hardware enable bit
  231. * in counter->hw_config cleared.
  232. *
  233. * If the counter is attached to a task which is on a CPU we use a smp
  234. * call to enable it in the task context. The task might have been
  235. * scheduled away, but we check this in the smp call again.
  236. */
  237. static void
  238. perf_install_in_context(struct perf_counter_context *ctx,
  239. struct perf_counter *counter,
  240. int cpu)
  241. {
  242. struct task_struct *task = ctx->task;
  243. counter->ctx = ctx;
  244. if (!task) {
  245. /*
  246. * Per cpu counters are installed via an smp call and
  247. * the install is always sucessful.
  248. */
  249. smp_call_function_single(cpu, __perf_install_in_context,
  250. counter, 1);
  251. return;
  252. }
  253. counter->task = task;
  254. retry:
  255. task_oncpu_function_call(task, __perf_install_in_context,
  256. counter);
  257. spin_lock_irq(&ctx->lock);
  258. /*
  259. * we need to retry the smp call.
  260. */
  261. if (ctx->nr_active && list_empty(&counter->list_entry)) {
  262. spin_unlock_irq(&ctx->lock);
  263. goto retry;
  264. }
  265. /*
  266. * The lock prevents that this context is scheduled in so we
  267. * can add the counter safely, if it the call above did not
  268. * succeed.
  269. */
  270. if (list_empty(&counter->list_entry)) {
  271. list_add_counter(counter, ctx);
  272. ctx->nr_counters++;
  273. }
  274. spin_unlock_irq(&ctx->lock);
  275. }
  276. static void
  277. counter_sched_out(struct perf_counter *counter,
  278. struct perf_cpu_context *cpuctx,
  279. struct perf_counter_context *ctx)
  280. {
  281. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  282. return;
  283. counter->state = PERF_COUNTER_STATE_INACTIVE;
  284. counter->hw_ops->disable(counter);
  285. counter->oncpu = -1;
  286. cpuctx->active_oncpu--;
  287. ctx->nr_active--;
  288. }
  289. static void
  290. group_sched_out(struct perf_counter *group_counter,
  291. struct perf_cpu_context *cpuctx,
  292. struct perf_counter_context *ctx)
  293. {
  294. struct perf_counter *counter;
  295. counter_sched_out(group_counter, cpuctx, ctx);
  296. /*
  297. * Schedule out siblings (if any):
  298. */
  299. list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
  300. counter_sched_out(counter, cpuctx, ctx);
  301. }
  302. void __perf_counter_sched_out(struct perf_counter_context *ctx,
  303. struct perf_cpu_context *cpuctx)
  304. {
  305. struct perf_counter *counter;
  306. if (likely(!ctx->nr_counters))
  307. return;
  308. spin_lock(&ctx->lock);
  309. if (ctx->nr_active) {
  310. list_for_each_entry(counter, &ctx->counter_list, list_entry)
  311. group_sched_out(counter, cpuctx, ctx);
  312. }
  313. spin_unlock(&ctx->lock);
  314. }
  315. /*
  316. * Called from scheduler to remove the counters of the current task,
  317. * with interrupts disabled.
  318. *
  319. * We stop each counter and update the counter value in counter->count.
  320. *
  321. * This does not protect us against NMI, but disable()
  322. * sets the disabled bit in the control field of counter _before_
  323. * accessing the counter control register. If a NMI hits, then it will
  324. * not restart the counter.
  325. */
  326. void perf_counter_task_sched_out(struct task_struct *task, int cpu)
  327. {
  328. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  329. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  330. if (likely(!cpuctx->task_ctx))
  331. return;
  332. __perf_counter_sched_out(ctx, cpuctx);
  333. cpuctx->task_ctx = NULL;
  334. }
  335. static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
  336. {
  337. __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
  338. }
  339. static int
  340. group_sched_in(struct perf_counter *group_counter,
  341. struct perf_cpu_context *cpuctx,
  342. struct perf_counter_context *ctx,
  343. int cpu)
  344. {
  345. struct perf_counter *counter, *partial_group;
  346. int ret = 0;
  347. if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
  348. return -EAGAIN;
  349. /*
  350. * Schedule in siblings as one group (if any):
  351. */
  352. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  353. if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
  354. partial_group = counter;
  355. goto group_error;
  356. }
  357. ret = -EAGAIN;
  358. }
  359. return ret;
  360. group_error:
  361. /*
  362. * Groups can be scheduled in as one unit only, so undo any
  363. * partial group before returning:
  364. */
  365. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  366. if (counter == partial_group)
  367. break;
  368. counter_sched_out(counter, cpuctx, ctx);
  369. }
  370. counter_sched_out(group_counter, cpuctx, ctx);
  371. return -EAGAIN;
  372. }
  373. static void
  374. __perf_counter_sched_in(struct perf_counter_context *ctx,
  375. struct perf_cpu_context *cpuctx, int cpu)
  376. {
  377. struct perf_counter *counter;
  378. if (likely(!ctx->nr_counters))
  379. return;
  380. spin_lock(&ctx->lock);
  381. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  382. /*
  383. * Listen to the 'cpu' scheduling filter constraint
  384. * of counters:
  385. */
  386. if (counter->cpu != -1 && counter->cpu != cpu)
  387. continue;
  388. /*
  389. * If we scheduled in a group atomically and
  390. * exclusively, break out:
  391. */
  392. if (group_sched_in(counter, cpuctx, ctx, cpu))
  393. break;
  394. }
  395. spin_unlock(&ctx->lock);
  396. }
  397. /*
  398. * Called from scheduler to add the counters of the current task
  399. * with interrupts disabled.
  400. *
  401. * We restore the counter value and then enable it.
  402. *
  403. * This does not protect us against NMI, but enable()
  404. * sets the enabled bit in the control field of counter _before_
  405. * accessing the counter control register. If a NMI hits, then it will
  406. * keep the counter running.
  407. */
  408. void perf_counter_task_sched_in(struct task_struct *task, int cpu)
  409. {
  410. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  411. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  412. __perf_counter_sched_in(ctx, cpuctx, cpu);
  413. cpuctx->task_ctx = ctx;
  414. }
  415. static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
  416. {
  417. struct perf_counter_context *ctx = &cpuctx->ctx;
  418. __perf_counter_sched_in(ctx, cpuctx, cpu);
  419. }
  420. int perf_counter_task_disable(void)
  421. {
  422. struct task_struct *curr = current;
  423. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  424. struct perf_counter *counter;
  425. unsigned long flags;
  426. u64 perf_flags;
  427. int cpu;
  428. if (likely(!ctx->nr_counters))
  429. return 0;
  430. curr_rq_lock_irq_save(&flags);
  431. cpu = smp_processor_id();
  432. /* force the update of the task clock: */
  433. __task_delta_exec(curr, 1);
  434. perf_counter_task_sched_out(curr, cpu);
  435. spin_lock(&ctx->lock);
  436. /*
  437. * Disable all the counters:
  438. */
  439. perf_flags = hw_perf_save_disable();
  440. list_for_each_entry(counter, &ctx->counter_list, list_entry)
  441. counter->state = PERF_COUNTER_STATE_OFF;
  442. hw_perf_restore(perf_flags);
  443. spin_unlock(&ctx->lock);
  444. curr_rq_unlock_irq_restore(&flags);
  445. return 0;
  446. }
  447. int perf_counter_task_enable(void)
  448. {
  449. struct task_struct *curr = current;
  450. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  451. struct perf_counter *counter;
  452. unsigned long flags;
  453. u64 perf_flags;
  454. int cpu;
  455. if (likely(!ctx->nr_counters))
  456. return 0;
  457. curr_rq_lock_irq_save(&flags);
  458. cpu = smp_processor_id();
  459. /* force the update of the task clock: */
  460. __task_delta_exec(curr, 1);
  461. perf_counter_task_sched_out(curr, cpu);
  462. spin_lock(&ctx->lock);
  463. /*
  464. * Disable all the counters:
  465. */
  466. perf_flags = hw_perf_save_disable();
  467. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  468. if (counter->state != PERF_COUNTER_STATE_OFF)
  469. continue;
  470. counter->state = PERF_COUNTER_STATE_INACTIVE;
  471. counter->hw_event.disabled = 0;
  472. }
  473. hw_perf_restore(perf_flags);
  474. spin_unlock(&ctx->lock);
  475. perf_counter_task_sched_in(curr, cpu);
  476. curr_rq_unlock_irq_restore(&flags);
  477. return 0;
  478. }
  479. /*
  480. * Round-robin a context's counters:
  481. */
  482. static void rotate_ctx(struct perf_counter_context *ctx)
  483. {
  484. struct perf_counter *counter;
  485. u64 perf_flags;
  486. if (!ctx->nr_counters)
  487. return;
  488. spin_lock(&ctx->lock);
  489. /*
  490. * Rotate the first entry last (works just fine for group counters too):
  491. */
  492. perf_flags = hw_perf_save_disable();
  493. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  494. list_del(&counter->list_entry);
  495. list_add_tail(&counter->list_entry, &ctx->counter_list);
  496. break;
  497. }
  498. hw_perf_restore(perf_flags);
  499. spin_unlock(&ctx->lock);
  500. }
  501. void perf_counter_task_tick(struct task_struct *curr, int cpu)
  502. {
  503. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  504. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  505. const int rotate_percpu = 0;
  506. if (rotate_percpu)
  507. perf_counter_cpu_sched_out(cpuctx);
  508. perf_counter_task_sched_out(curr, cpu);
  509. if (rotate_percpu)
  510. rotate_ctx(&cpuctx->ctx);
  511. rotate_ctx(ctx);
  512. if (rotate_percpu)
  513. perf_counter_cpu_sched_in(cpuctx, cpu);
  514. perf_counter_task_sched_in(curr, cpu);
  515. }
  516. /*
  517. * Cross CPU call to read the hardware counter
  518. */
  519. static void __read(void *info)
  520. {
  521. struct perf_counter *counter = info;
  522. unsigned long flags;
  523. curr_rq_lock_irq_save(&flags);
  524. counter->hw_ops->read(counter);
  525. curr_rq_unlock_irq_restore(&flags);
  526. }
  527. static u64 perf_counter_read(struct perf_counter *counter)
  528. {
  529. /*
  530. * If counter is enabled and currently active on a CPU, update the
  531. * value in the counter structure:
  532. */
  533. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  534. smp_call_function_single(counter->oncpu,
  535. __read, counter, 1);
  536. }
  537. return atomic64_read(&counter->count);
  538. }
  539. /*
  540. * Cross CPU call to switch performance data pointers
  541. */
  542. static void __perf_switch_irq_data(void *info)
  543. {
  544. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  545. struct perf_counter *counter = info;
  546. struct perf_counter_context *ctx = counter->ctx;
  547. struct perf_data *oldirqdata = counter->irqdata;
  548. /*
  549. * If this is a task context, we need to check whether it is
  550. * the current task context of this cpu. If not it has been
  551. * scheduled out before the smp call arrived.
  552. */
  553. if (ctx->task) {
  554. if (cpuctx->task_ctx != ctx)
  555. return;
  556. spin_lock(&ctx->lock);
  557. }
  558. /* Change the pointer NMI safe */
  559. atomic_long_set((atomic_long_t *)&counter->irqdata,
  560. (unsigned long) counter->usrdata);
  561. counter->usrdata = oldirqdata;
  562. if (ctx->task)
  563. spin_unlock(&ctx->lock);
  564. }
  565. static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
  566. {
  567. struct perf_counter_context *ctx = counter->ctx;
  568. struct perf_data *oldirqdata = counter->irqdata;
  569. struct task_struct *task = ctx->task;
  570. if (!task) {
  571. smp_call_function_single(counter->cpu,
  572. __perf_switch_irq_data,
  573. counter, 1);
  574. return counter->usrdata;
  575. }
  576. retry:
  577. spin_lock_irq(&ctx->lock);
  578. if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
  579. counter->irqdata = counter->usrdata;
  580. counter->usrdata = oldirqdata;
  581. spin_unlock_irq(&ctx->lock);
  582. return oldirqdata;
  583. }
  584. spin_unlock_irq(&ctx->lock);
  585. task_oncpu_function_call(task, __perf_switch_irq_data, counter);
  586. /* Might have failed, because task was scheduled out */
  587. if (counter->irqdata == oldirqdata)
  588. goto retry;
  589. return counter->usrdata;
  590. }
  591. static void put_context(struct perf_counter_context *ctx)
  592. {
  593. if (ctx->task)
  594. put_task_struct(ctx->task);
  595. }
  596. static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  597. {
  598. struct perf_cpu_context *cpuctx;
  599. struct perf_counter_context *ctx;
  600. struct task_struct *task;
  601. /*
  602. * If cpu is not a wildcard then this is a percpu counter:
  603. */
  604. if (cpu != -1) {
  605. /* Must be root to operate on a CPU counter: */
  606. if (!capable(CAP_SYS_ADMIN))
  607. return ERR_PTR(-EACCES);
  608. if (cpu < 0 || cpu > num_possible_cpus())
  609. return ERR_PTR(-EINVAL);
  610. /*
  611. * We could be clever and allow to attach a counter to an
  612. * offline CPU and activate it when the CPU comes up, but
  613. * that's for later.
  614. */
  615. if (!cpu_isset(cpu, cpu_online_map))
  616. return ERR_PTR(-ENODEV);
  617. cpuctx = &per_cpu(perf_cpu_context, cpu);
  618. ctx = &cpuctx->ctx;
  619. return ctx;
  620. }
  621. rcu_read_lock();
  622. if (!pid)
  623. task = current;
  624. else
  625. task = find_task_by_vpid(pid);
  626. if (task)
  627. get_task_struct(task);
  628. rcu_read_unlock();
  629. if (!task)
  630. return ERR_PTR(-ESRCH);
  631. ctx = &task->perf_counter_ctx;
  632. ctx->task = task;
  633. /* Reuse ptrace permission checks for now. */
  634. if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
  635. put_context(ctx);
  636. return ERR_PTR(-EACCES);
  637. }
  638. return ctx;
  639. }
  640. /*
  641. * Called when the last reference to the file is gone.
  642. */
  643. static int perf_release(struct inode *inode, struct file *file)
  644. {
  645. struct perf_counter *counter = file->private_data;
  646. struct perf_counter_context *ctx = counter->ctx;
  647. file->private_data = NULL;
  648. mutex_lock(&counter->mutex);
  649. perf_counter_remove_from_context(counter);
  650. put_context(ctx);
  651. mutex_unlock(&counter->mutex);
  652. kfree(counter);
  653. return 0;
  654. }
  655. /*
  656. * Read the performance counter - simple non blocking version for now
  657. */
  658. static ssize_t
  659. perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
  660. {
  661. u64 cntval;
  662. if (count != sizeof(cntval))
  663. return -EINVAL;
  664. mutex_lock(&counter->mutex);
  665. cntval = perf_counter_read(counter);
  666. mutex_unlock(&counter->mutex);
  667. return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
  668. }
  669. static ssize_t
  670. perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
  671. {
  672. if (!usrdata->len)
  673. return 0;
  674. count = min(count, (size_t)usrdata->len);
  675. if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
  676. return -EFAULT;
  677. /* Adjust the counters */
  678. usrdata->len -= count;
  679. if (!usrdata->len)
  680. usrdata->rd_idx = 0;
  681. else
  682. usrdata->rd_idx += count;
  683. return count;
  684. }
  685. static ssize_t
  686. perf_read_irq_data(struct perf_counter *counter,
  687. char __user *buf,
  688. size_t count,
  689. int nonblocking)
  690. {
  691. struct perf_data *irqdata, *usrdata;
  692. DECLARE_WAITQUEUE(wait, current);
  693. ssize_t res;
  694. irqdata = counter->irqdata;
  695. usrdata = counter->usrdata;
  696. if (usrdata->len + irqdata->len >= count)
  697. goto read_pending;
  698. if (nonblocking)
  699. return -EAGAIN;
  700. spin_lock_irq(&counter->waitq.lock);
  701. __add_wait_queue(&counter->waitq, &wait);
  702. for (;;) {
  703. set_current_state(TASK_INTERRUPTIBLE);
  704. if (usrdata->len + irqdata->len >= count)
  705. break;
  706. if (signal_pending(current))
  707. break;
  708. spin_unlock_irq(&counter->waitq.lock);
  709. schedule();
  710. spin_lock_irq(&counter->waitq.lock);
  711. }
  712. __remove_wait_queue(&counter->waitq, &wait);
  713. __set_current_state(TASK_RUNNING);
  714. spin_unlock_irq(&counter->waitq.lock);
  715. if (usrdata->len + irqdata->len < count)
  716. return -ERESTARTSYS;
  717. read_pending:
  718. mutex_lock(&counter->mutex);
  719. /* Drain pending data first: */
  720. res = perf_copy_usrdata(usrdata, buf, count);
  721. if (res < 0 || res == count)
  722. goto out;
  723. /* Switch irq buffer: */
  724. usrdata = perf_switch_irq_data(counter);
  725. if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) {
  726. if (!res)
  727. res = -EFAULT;
  728. } else {
  729. res = count;
  730. }
  731. out:
  732. mutex_unlock(&counter->mutex);
  733. return res;
  734. }
  735. static ssize_t
  736. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  737. {
  738. struct perf_counter *counter = file->private_data;
  739. switch (counter->hw_event.record_type) {
  740. case PERF_RECORD_SIMPLE:
  741. return perf_read_hw(counter, buf, count);
  742. case PERF_RECORD_IRQ:
  743. case PERF_RECORD_GROUP:
  744. return perf_read_irq_data(counter, buf, count,
  745. file->f_flags & O_NONBLOCK);
  746. }
  747. return -EINVAL;
  748. }
  749. static unsigned int perf_poll(struct file *file, poll_table *wait)
  750. {
  751. struct perf_counter *counter = file->private_data;
  752. unsigned int events = 0;
  753. unsigned long flags;
  754. poll_wait(file, &counter->waitq, wait);
  755. spin_lock_irqsave(&counter->waitq.lock, flags);
  756. if (counter->usrdata->len || counter->irqdata->len)
  757. events |= POLLIN;
  758. spin_unlock_irqrestore(&counter->waitq.lock, flags);
  759. return events;
  760. }
  761. static const struct file_operations perf_fops = {
  762. .release = perf_release,
  763. .read = perf_read,
  764. .poll = perf_poll,
  765. };
  766. static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
  767. {
  768. return 0;
  769. }
  770. static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
  771. {
  772. }
  773. static void cpu_clock_perf_counter_read(struct perf_counter *counter)
  774. {
  775. int cpu = raw_smp_processor_id();
  776. atomic64_set(&counter->count, cpu_clock(cpu));
  777. }
  778. static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
  779. .enable = cpu_clock_perf_counter_enable,
  780. .disable = cpu_clock_perf_counter_disable,
  781. .read = cpu_clock_perf_counter_read,
  782. };
  783. /*
  784. * Called from within the scheduler:
  785. */
  786. static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
  787. {
  788. struct task_struct *curr = counter->task;
  789. u64 delta;
  790. delta = __task_delta_exec(curr, update);
  791. return curr->se.sum_exec_runtime + delta;
  792. }
  793. static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
  794. {
  795. u64 prev;
  796. s64 delta;
  797. prev = atomic64_read(&counter->hw.prev_count);
  798. atomic64_set(&counter->hw.prev_count, now);
  799. delta = now - prev;
  800. atomic64_add(delta, &counter->count);
  801. }
  802. static void task_clock_perf_counter_read(struct perf_counter *counter)
  803. {
  804. u64 now = task_clock_perf_counter_val(counter, 1);
  805. task_clock_perf_counter_update(counter, now);
  806. }
  807. static int task_clock_perf_counter_enable(struct perf_counter *counter)
  808. {
  809. u64 now = task_clock_perf_counter_val(counter, 0);
  810. atomic64_set(&counter->hw.prev_count, now);
  811. return 0;
  812. }
  813. static void task_clock_perf_counter_disable(struct perf_counter *counter)
  814. {
  815. u64 now = task_clock_perf_counter_val(counter, 0);
  816. task_clock_perf_counter_update(counter, now);
  817. }
  818. static const struct hw_perf_counter_ops perf_ops_task_clock = {
  819. .enable = task_clock_perf_counter_enable,
  820. .disable = task_clock_perf_counter_disable,
  821. .read = task_clock_perf_counter_read,
  822. };
  823. static u64 get_page_faults(void)
  824. {
  825. struct task_struct *curr = current;
  826. return curr->maj_flt + curr->min_flt;
  827. }
  828. static void page_faults_perf_counter_update(struct perf_counter *counter)
  829. {
  830. u64 prev, now;
  831. s64 delta;
  832. prev = atomic64_read(&counter->hw.prev_count);
  833. now = get_page_faults();
  834. atomic64_set(&counter->hw.prev_count, now);
  835. delta = now - prev;
  836. atomic64_add(delta, &counter->count);
  837. }
  838. static void page_faults_perf_counter_read(struct perf_counter *counter)
  839. {
  840. page_faults_perf_counter_update(counter);
  841. }
  842. static int page_faults_perf_counter_enable(struct perf_counter *counter)
  843. {
  844. /*
  845. * page-faults is a per-task value already,
  846. * so we dont have to clear it on switch-in.
  847. */
  848. return 0;
  849. }
  850. static void page_faults_perf_counter_disable(struct perf_counter *counter)
  851. {
  852. page_faults_perf_counter_update(counter);
  853. }
  854. static const struct hw_perf_counter_ops perf_ops_page_faults = {
  855. .enable = page_faults_perf_counter_enable,
  856. .disable = page_faults_perf_counter_disable,
  857. .read = page_faults_perf_counter_read,
  858. };
  859. static u64 get_context_switches(void)
  860. {
  861. struct task_struct *curr = current;
  862. return curr->nvcsw + curr->nivcsw;
  863. }
  864. static void context_switches_perf_counter_update(struct perf_counter *counter)
  865. {
  866. u64 prev, now;
  867. s64 delta;
  868. prev = atomic64_read(&counter->hw.prev_count);
  869. now = get_context_switches();
  870. atomic64_set(&counter->hw.prev_count, now);
  871. delta = now - prev;
  872. atomic64_add(delta, &counter->count);
  873. }
  874. static void context_switches_perf_counter_read(struct perf_counter *counter)
  875. {
  876. context_switches_perf_counter_update(counter);
  877. }
  878. static int context_switches_perf_counter_enable(struct perf_counter *counter)
  879. {
  880. /*
  881. * ->nvcsw + curr->nivcsw is a per-task value already,
  882. * so we dont have to clear it on switch-in.
  883. */
  884. return 0;
  885. }
  886. static void context_switches_perf_counter_disable(struct perf_counter *counter)
  887. {
  888. context_switches_perf_counter_update(counter);
  889. }
  890. static const struct hw_perf_counter_ops perf_ops_context_switches = {
  891. .enable = context_switches_perf_counter_enable,
  892. .disable = context_switches_perf_counter_disable,
  893. .read = context_switches_perf_counter_read,
  894. };
  895. static inline u64 get_cpu_migrations(void)
  896. {
  897. return current->se.nr_migrations;
  898. }
  899. static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
  900. {
  901. u64 prev, now;
  902. s64 delta;
  903. prev = atomic64_read(&counter->hw.prev_count);
  904. now = get_cpu_migrations();
  905. atomic64_set(&counter->hw.prev_count, now);
  906. delta = now - prev;
  907. atomic64_add(delta, &counter->count);
  908. }
  909. static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
  910. {
  911. cpu_migrations_perf_counter_update(counter);
  912. }
  913. static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
  914. {
  915. /*
  916. * se.nr_migrations is a per-task value already,
  917. * so we dont have to clear it on switch-in.
  918. */
  919. return 0;
  920. }
  921. static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
  922. {
  923. cpu_migrations_perf_counter_update(counter);
  924. }
  925. static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
  926. .enable = cpu_migrations_perf_counter_enable,
  927. .disable = cpu_migrations_perf_counter_disable,
  928. .read = cpu_migrations_perf_counter_read,
  929. };
  930. static const struct hw_perf_counter_ops *
  931. sw_perf_counter_init(struct perf_counter *counter)
  932. {
  933. const struct hw_perf_counter_ops *hw_ops = NULL;
  934. switch (counter->hw_event.type) {
  935. case PERF_COUNT_CPU_CLOCK:
  936. hw_ops = &perf_ops_cpu_clock;
  937. break;
  938. case PERF_COUNT_TASK_CLOCK:
  939. hw_ops = &perf_ops_task_clock;
  940. break;
  941. case PERF_COUNT_PAGE_FAULTS:
  942. hw_ops = &perf_ops_page_faults;
  943. break;
  944. case PERF_COUNT_CONTEXT_SWITCHES:
  945. hw_ops = &perf_ops_context_switches;
  946. break;
  947. case PERF_COUNT_CPU_MIGRATIONS:
  948. hw_ops = &perf_ops_cpu_migrations;
  949. break;
  950. default:
  951. break;
  952. }
  953. return hw_ops;
  954. }
  955. /*
  956. * Allocate and initialize a counter structure
  957. */
  958. static struct perf_counter *
  959. perf_counter_alloc(struct perf_counter_hw_event *hw_event,
  960. int cpu,
  961. struct perf_counter *group_leader,
  962. gfp_t gfpflags)
  963. {
  964. const struct hw_perf_counter_ops *hw_ops;
  965. struct perf_counter *counter;
  966. counter = kzalloc(sizeof(*counter), gfpflags);
  967. if (!counter)
  968. return NULL;
  969. /*
  970. * Single counters are their own group leaders, with an
  971. * empty sibling list:
  972. */
  973. if (!group_leader)
  974. group_leader = counter;
  975. mutex_init(&counter->mutex);
  976. INIT_LIST_HEAD(&counter->list_entry);
  977. INIT_LIST_HEAD(&counter->sibling_list);
  978. init_waitqueue_head(&counter->waitq);
  979. counter->irqdata = &counter->data[0];
  980. counter->usrdata = &counter->data[1];
  981. counter->cpu = cpu;
  982. counter->hw_event = *hw_event;
  983. counter->wakeup_pending = 0;
  984. counter->group_leader = group_leader;
  985. counter->hw_ops = NULL;
  986. counter->state = PERF_COUNTER_STATE_INACTIVE;
  987. if (hw_event->disabled)
  988. counter->state = PERF_COUNTER_STATE_OFF;
  989. hw_ops = NULL;
  990. if (!hw_event->raw && hw_event->type < 0)
  991. hw_ops = sw_perf_counter_init(counter);
  992. if (!hw_ops)
  993. hw_ops = hw_perf_counter_init(counter);
  994. if (!hw_ops) {
  995. kfree(counter);
  996. return NULL;
  997. }
  998. counter->hw_ops = hw_ops;
  999. return counter;
  1000. }
  1001. /**
  1002. * sys_perf_task_open - open a performance counter, associate it to a task/cpu
  1003. *
  1004. * @hw_event_uptr: event type attributes for monitoring/sampling
  1005. * @pid: target pid
  1006. * @cpu: target cpu
  1007. * @group_fd: group leader counter fd
  1008. */
  1009. asmlinkage int
  1010. sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user,
  1011. pid_t pid, int cpu, int group_fd)
  1012. {
  1013. struct perf_counter *counter, *group_leader;
  1014. struct perf_counter_hw_event hw_event;
  1015. struct perf_counter_context *ctx;
  1016. struct file *counter_file = NULL;
  1017. struct file *group_file = NULL;
  1018. int fput_needed = 0;
  1019. int fput_needed2 = 0;
  1020. int ret;
  1021. if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
  1022. return -EFAULT;
  1023. /*
  1024. * Get the target context (task or percpu):
  1025. */
  1026. ctx = find_get_context(pid, cpu);
  1027. if (IS_ERR(ctx))
  1028. return PTR_ERR(ctx);
  1029. /*
  1030. * Look up the group leader (we will attach this counter to it):
  1031. */
  1032. group_leader = NULL;
  1033. if (group_fd != -1) {
  1034. ret = -EINVAL;
  1035. group_file = fget_light(group_fd, &fput_needed);
  1036. if (!group_file)
  1037. goto err_put_context;
  1038. if (group_file->f_op != &perf_fops)
  1039. goto err_put_context;
  1040. group_leader = group_file->private_data;
  1041. /*
  1042. * Do not allow a recursive hierarchy (this new sibling
  1043. * becoming part of another group-sibling):
  1044. */
  1045. if (group_leader->group_leader != group_leader)
  1046. goto err_put_context;
  1047. /*
  1048. * Do not allow to attach to a group in a different
  1049. * task or CPU context:
  1050. */
  1051. if (group_leader->ctx != ctx)
  1052. goto err_put_context;
  1053. }
  1054. ret = -EINVAL;
  1055. counter = perf_counter_alloc(&hw_event, cpu, group_leader, GFP_KERNEL);
  1056. if (!counter)
  1057. goto err_put_context;
  1058. ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
  1059. if (ret < 0)
  1060. goto err_free_put_context;
  1061. counter_file = fget_light(ret, &fput_needed2);
  1062. if (!counter_file)
  1063. goto err_free_put_context;
  1064. counter->filp = counter_file;
  1065. perf_install_in_context(ctx, counter, cpu);
  1066. fput_light(counter_file, fput_needed2);
  1067. out_fput:
  1068. fput_light(group_file, fput_needed);
  1069. return ret;
  1070. err_free_put_context:
  1071. kfree(counter);
  1072. err_put_context:
  1073. put_context(ctx);
  1074. goto out_fput;
  1075. }
  1076. /*
  1077. * Initialize the perf_counter context in a task_struct:
  1078. */
  1079. static void
  1080. __perf_counter_init_context(struct perf_counter_context *ctx,
  1081. struct task_struct *task)
  1082. {
  1083. memset(ctx, 0, sizeof(*ctx));
  1084. spin_lock_init(&ctx->lock);
  1085. INIT_LIST_HEAD(&ctx->counter_list);
  1086. ctx->task = task;
  1087. }
  1088. /*
  1089. * inherit a counter from parent task to child task:
  1090. */
  1091. static int
  1092. inherit_counter(struct perf_counter *parent_counter,
  1093. struct task_struct *parent,
  1094. struct perf_counter_context *parent_ctx,
  1095. struct task_struct *child,
  1096. struct perf_counter_context *child_ctx)
  1097. {
  1098. struct perf_counter *child_counter;
  1099. child_counter = perf_counter_alloc(&parent_counter->hw_event,
  1100. parent_counter->cpu, NULL,
  1101. GFP_ATOMIC);
  1102. if (!child_counter)
  1103. return -ENOMEM;
  1104. /*
  1105. * Link it up in the child's context:
  1106. */
  1107. child_counter->ctx = child_ctx;
  1108. child_counter->task = child;
  1109. list_add_counter(child_counter, child_ctx);
  1110. child_ctx->nr_counters++;
  1111. child_counter->parent = parent_counter;
  1112. /*
  1113. * inherit into child's child as well:
  1114. */
  1115. child_counter->hw_event.inherit = 1;
  1116. /*
  1117. * Get a reference to the parent filp - we will fput it
  1118. * when the child counter exits. This is safe to do because
  1119. * we are in the parent and we know that the filp still
  1120. * exists and has a nonzero count:
  1121. */
  1122. atomic_long_inc(&parent_counter->filp->f_count);
  1123. return 0;
  1124. }
  1125. static void
  1126. __perf_counter_exit_task(struct task_struct *child,
  1127. struct perf_counter *child_counter,
  1128. struct perf_counter_context *child_ctx)
  1129. {
  1130. struct perf_counter *parent_counter;
  1131. u64 parent_val, child_val;
  1132. /*
  1133. * If we do not self-reap then we have to wait for the
  1134. * child task to unschedule (it will happen for sure),
  1135. * so that its counter is at its final count. (This
  1136. * condition triggers rarely - child tasks usually get
  1137. * off their CPU before the parent has a chance to
  1138. * get this far into the reaping action)
  1139. */
  1140. if (child != current) {
  1141. wait_task_inactive(child, 0);
  1142. list_del_init(&child_counter->list_entry);
  1143. } else {
  1144. struct perf_cpu_context *cpuctx;
  1145. unsigned long flags;
  1146. u64 perf_flags;
  1147. /*
  1148. * Disable and unlink this counter.
  1149. *
  1150. * Be careful about zapping the list - IRQ/NMI context
  1151. * could still be processing it:
  1152. */
  1153. curr_rq_lock_irq_save(&flags);
  1154. perf_flags = hw_perf_save_disable();
  1155. cpuctx = &__get_cpu_var(perf_cpu_context);
  1156. if (child_counter->state == PERF_COUNTER_STATE_ACTIVE) {
  1157. child_counter->state = PERF_COUNTER_STATE_INACTIVE;
  1158. child_counter->hw_ops->disable(child_counter);
  1159. cpuctx->active_oncpu--;
  1160. child_ctx->nr_active--;
  1161. child_counter->oncpu = -1;
  1162. }
  1163. list_del_init(&child_counter->list_entry);
  1164. child_ctx->nr_counters--;
  1165. hw_perf_restore(perf_flags);
  1166. curr_rq_unlock_irq_restore(&flags);
  1167. }
  1168. parent_counter = child_counter->parent;
  1169. /*
  1170. * It can happen that parent exits first, and has counters
  1171. * that are still around due to the child reference. These
  1172. * counters need to be zapped - but otherwise linger.
  1173. */
  1174. if (!parent_counter)
  1175. return;
  1176. parent_val = atomic64_read(&parent_counter->count);
  1177. child_val = atomic64_read(&child_counter->count);
  1178. /*
  1179. * Add back the child's count to the parent's count:
  1180. */
  1181. atomic64_add(child_val, &parent_counter->count);
  1182. fput(parent_counter->filp);
  1183. kfree(child_counter);
  1184. }
  1185. /*
  1186. * When a child task exist, feed back counter values to parent counters.
  1187. *
  1188. * Note: we are running in child context, but the PID is not hashed
  1189. * anymore so new counters will not be added.
  1190. */
  1191. void perf_counter_exit_task(struct task_struct *child)
  1192. {
  1193. struct perf_counter *child_counter, *tmp;
  1194. struct perf_counter_context *child_ctx;
  1195. child_ctx = &child->perf_counter_ctx;
  1196. if (likely(!child_ctx->nr_counters))
  1197. return;
  1198. list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
  1199. list_entry)
  1200. __perf_counter_exit_task(child, child_counter, child_ctx);
  1201. }
  1202. /*
  1203. * Initialize the perf_counter context in task_struct
  1204. */
  1205. void perf_counter_init_task(struct task_struct *child)
  1206. {
  1207. struct perf_counter_context *child_ctx, *parent_ctx;
  1208. struct perf_counter *counter, *parent_counter;
  1209. struct task_struct *parent = current;
  1210. unsigned long flags;
  1211. child_ctx = &child->perf_counter_ctx;
  1212. parent_ctx = &parent->perf_counter_ctx;
  1213. __perf_counter_init_context(child_ctx, child);
  1214. /*
  1215. * This is executed from the parent task context, so inherit
  1216. * counters that have been marked for cloning:
  1217. */
  1218. if (likely(!parent_ctx->nr_counters))
  1219. return;
  1220. /*
  1221. * Lock the parent list. No need to lock the child - not PID
  1222. * hashed yet and not running, so nobody can access it.
  1223. */
  1224. spin_lock_irqsave(&parent_ctx->lock, flags);
  1225. /*
  1226. * We dont have to disable NMIs - we are only looking at
  1227. * the list, not manipulating it:
  1228. */
  1229. list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
  1230. if (!counter->hw_event.inherit || counter->group_leader != counter)
  1231. continue;
  1232. /*
  1233. * Instead of creating recursive hierarchies of counters,
  1234. * we link inheritd counters back to the original parent,
  1235. * which has a filp for sure, which we use as the reference
  1236. * count:
  1237. */
  1238. parent_counter = counter;
  1239. if (counter->parent)
  1240. parent_counter = counter->parent;
  1241. if (inherit_counter(parent_counter, parent,
  1242. parent_ctx, child, child_ctx))
  1243. break;
  1244. }
  1245. spin_unlock_irqrestore(&parent_ctx->lock, flags);
  1246. }
  1247. static void __cpuinit perf_counter_init_cpu(int cpu)
  1248. {
  1249. struct perf_cpu_context *cpuctx;
  1250. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1251. __perf_counter_init_context(&cpuctx->ctx, NULL);
  1252. mutex_lock(&perf_resource_mutex);
  1253. cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
  1254. mutex_unlock(&perf_resource_mutex);
  1255. hw_perf_counter_setup();
  1256. }
  1257. #ifdef CONFIG_HOTPLUG_CPU
  1258. static void __perf_counter_exit_cpu(void *info)
  1259. {
  1260. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1261. struct perf_counter_context *ctx = &cpuctx->ctx;
  1262. struct perf_counter *counter, *tmp;
  1263. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
  1264. __perf_counter_remove_from_context(counter);
  1265. }
  1266. static void perf_counter_exit_cpu(int cpu)
  1267. {
  1268. smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
  1269. }
  1270. #else
  1271. static inline void perf_counter_exit_cpu(int cpu) { }
  1272. #endif
  1273. static int __cpuinit
  1274. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  1275. {
  1276. unsigned int cpu = (long)hcpu;
  1277. switch (action) {
  1278. case CPU_UP_PREPARE:
  1279. case CPU_UP_PREPARE_FROZEN:
  1280. perf_counter_init_cpu(cpu);
  1281. break;
  1282. case CPU_DOWN_PREPARE:
  1283. case CPU_DOWN_PREPARE_FROZEN:
  1284. perf_counter_exit_cpu(cpu);
  1285. break;
  1286. default:
  1287. break;
  1288. }
  1289. return NOTIFY_OK;
  1290. }
  1291. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  1292. .notifier_call = perf_cpu_notify,
  1293. };
  1294. static int __init perf_counter_init(void)
  1295. {
  1296. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  1297. (void *)(long)smp_processor_id());
  1298. register_cpu_notifier(&perf_cpu_nb);
  1299. return 0;
  1300. }
  1301. early_initcall(perf_counter_init);
  1302. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  1303. {
  1304. return sprintf(buf, "%d\n", perf_reserved_percpu);
  1305. }
  1306. static ssize_t
  1307. perf_set_reserve_percpu(struct sysdev_class *class,
  1308. const char *buf,
  1309. size_t count)
  1310. {
  1311. struct perf_cpu_context *cpuctx;
  1312. unsigned long val;
  1313. int err, cpu, mpt;
  1314. err = strict_strtoul(buf, 10, &val);
  1315. if (err)
  1316. return err;
  1317. if (val > perf_max_counters)
  1318. return -EINVAL;
  1319. mutex_lock(&perf_resource_mutex);
  1320. perf_reserved_percpu = val;
  1321. for_each_online_cpu(cpu) {
  1322. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1323. spin_lock_irq(&cpuctx->ctx.lock);
  1324. mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
  1325. perf_max_counters - perf_reserved_percpu);
  1326. cpuctx->max_pertask = mpt;
  1327. spin_unlock_irq(&cpuctx->ctx.lock);
  1328. }
  1329. mutex_unlock(&perf_resource_mutex);
  1330. return count;
  1331. }
  1332. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  1333. {
  1334. return sprintf(buf, "%d\n", perf_overcommit);
  1335. }
  1336. static ssize_t
  1337. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  1338. {
  1339. unsigned long val;
  1340. int err;
  1341. err = strict_strtoul(buf, 10, &val);
  1342. if (err)
  1343. return err;
  1344. if (val > 1)
  1345. return -EINVAL;
  1346. mutex_lock(&perf_resource_mutex);
  1347. perf_overcommit = val;
  1348. mutex_unlock(&perf_resource_mutex);
  1349. return count;
  1350. }
  1351. static SYSDEV_CLASS_ATTR(
  1352. reserve_percpu,
  1353. 0644,
  1354. perf_show_reserve_percpu,
  1355. perf_set_reserve_percpu
  1356. );
  1357. static SYSDEV_CLASS_ATTR(
  1358. overcommit,
  1359. 0644,
  1360. perf_show_overcommit,
  1361. perf_set_overcommit
  1362. );
  1363. static struct attribute *perfclass_attrs[] = {
  1364. &attr_reserve_percpu.attr,
  1365. &attr_overcommit.attr,
  1366. NULL
  1367. };
  1368. static struct attribute_group perfclass_attr_group = {
  1369. .attrs = perfclass_attrs,
  1370. .name = "perf_counters",
  1371. };
  1372. static int __init perf_counter_sysfs_init(void)
  1373. {
  1374. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  1375. &perfclass_attr_group);
  1376. }
  1377. device_initcall(perf_counter_sysfs_init);