perf_counter.c 40 KB

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