perf_counter.c 37 KB

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