perf_counter.c 37 KB

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