perf_counter.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438
  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. #include <linux/mm.h>
  23. #include <linux/vmstat.h>
  24. #include <linux/rculist.h>
  25. #include <asm/irq_regs.h>
  26. /*
  27. * Each CPU has a list of per CPU counters:
  28. */
  29. DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
  30. int perf_max_counters __read_mostly = 1;
  31. static int perf_reserved_percpu __read_mostly;
  32. static int perf_overcommit __read_mostly = 1;
  33. /*
  34. * Mutex for (sysadmin-configurable) counter reservations:
  35. */
  36. static DEFINE_MUTEX(perf_resource_mutex);
  37. /*
  38. * Architecture provided APIs - weak aliases:
  39. */
  40. extern __weak const struct hw_perf_counter_ops *
  41. hw_perf_counter_init(struct perf_counter *counter)
  42. {
  43. return NULL;
  44. }
  45. u64 __weak hw_perf_save_disable(void) { return 0; }
  46. void __weak hw_perf_restore(u64 ctrl) { barrier(); }
  47. void __weak hw_perf_counter_setup(int cpu) { barrier(); }
  48. int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
  49. struct perf_cpu_context *cpuctx,
  50. struct perf_counter_context *ctx, int cpu)
  51. {
  52. return 0;
  53. }
  54. void __weak perf_counter_print_debug(void) { }
  55. static void
  56. list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  57. {
  58. struct perf_counter *group_leader = counter->group_leader;
  59. /*
  60. * Depending on whether it is a standalone or sibling counter,
  61. * add it straight to the context's counter list, or to the group
  62. * leader's sibling list:
  63. */
  64. if (counter->group_leader == counter)
  65. list_add_tail(&counter->list_entry, &ctx->counter_list);
  66. else
  67. list_add_tail(&counter->list_entry, &group_leader->sibling_list);
  68. list_add_rcu(&counter->event_entry, &ctx->event_list);
  69. }
  70. static void
  71. list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  72. {
  73. struct perf_counter *sibling, *tmp;
  74. list_del_init(&counter->list_entry);
  75. list_del_rcu(&counter->event_entry);
  76. /*
  77. * If this was a group counter with sibling counters then
  78. * upgrade the siblings to singleton counters by adding them
  79. * to the context list directly:
  80. */
  81. list_for_each_entry_safe(sibling, tmp,
  82. &counter->sibling_list, list_entry) {
  83. list_move_tail(&sibling->list_entry, &ctx->counter_list);
  84. sibling->group_leader = sibling;
  85. }
  86. }
  87. static void
  88. counter_sched_out(struct perf_counter *counter,
  89. struct perf_cpu_context *cpuctx,
  90. struct perf_counter_context *ctx)
  91. {
  92. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  93. return;
  94. counter->state = PERF_COUNTER_STATE_INACTIVE;
  95. counter->hw_ops->disable(counter);
  96. counter->oncpu = -1;
  97. if (!is_software_counter(counter))
  98. cpuctx->active_oncpu--;
  99. ctx->nr_active--;
  100. if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
  101. cpuctx->exclusive = 0;
  102. }
  103. static void
  104. group_sched_out(struct perf_counter *group_counter,
  105. struct perf_cpu_context *cpuctx,
  106. struct perf_counter_context *ctx)
  107. {
  108. struct perf_counter *counter;
  109. if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
  110. return;
  111. counter_sched_out(group_counter, cpuctx, ctx);
  112. /*
  113. * Schedule out siblings (if any):
  114. */
  115. list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
  116. counter_sched_out(counter, cpuctx, ctx);
  117. if (group_counter->hw_event.exclusive)
  118. cpuctx->exclusive = 0;
  119. }
  120. /*
  121. * Cross CPU call to remove a performance counter
  122. *
  123. * We disable the counter on the hardware level first. After that we
  124. * remove it from the context list.
  125. */
  126. static void __perf_counter_remove_from_context(void *info)
  127. {
  128. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  129. struct perf_counter *counter = info;
  130. struct perf_counter_context *ctx = counter->ctx;
  131. unsigned long flags;
  132. u64 perf_flags;
  133. /*
  134. * If this is a task context, we need to check whether it is
  135. * the current task context of this cpu. If not it has been
  136. * scheduled out before the smp call arrived.
  137. */
  138. if (ctx->task && cpuctx->task_ctx != ctx)
  139. return;
  140. curr_rq_lock_irq_save(&flags);
  141. spin_lock(&ctx->lock);
  142. counter_sched_out(counter, cpuctx, ctx);
  143. counter->task = NULL;
  144. ctx->nr_counters--;
  145. /*
  146. * Protect the list operation against NMI by disabling the
  147. * counters on a global level. NOP for non NMI based counters.
  148. */
  149. perf_flags = hw_perf_save_disable();
  150. list_del_counter(counter, ctx);
  151. hw_perf_restore(perf_flags);
  152. if (!ctx->task) {
  153. /*
  154. * Allow more per task counters with respect to the
  155. * reservation:
  156. */
  157. cpuctx->max_pertask =
  158. min(perf_max_counters - ctx->nr_counters,
  159. perf_max_counters - perf_reserved_percpu);
  160. }
  161. spin_unlock(&ctx->lock);
  162. curr_rq_unlock_irq_restore(&flags);
  163. }
  164. /*
  165. * Remove the counter from a task's (or a CPU's) list of counters.
  166. *
  167. * Must be called with counter->mutex and ctx->mutex held.
  168. *
  169. * CPU counters are removed with a smp call. For task counters we only
  170. * call when the task is on a CPU.
  171. */
  172. static void perf_counter_remove_from_context(struct perf_counter *counter)
  173. {
  174. struct perf_counter_context *ctx = counter->ctx;
  175. struct task_struct *task = ctx->task;
  176. if (!task) {
  177. /*
  178. * Per cpu counters are removed via an smp call and
  179. * the removal is always sucessful.
  180. */
  181. smp_call_function_single(counter->cpu,
  182. __perf_counter_remove_from_context,
  183. counter, 1);
  184. return;
  185. }
  186. retry:
  187. task_oncpu_function_call(task, __perf_counter_remove_from_context,
  188. counter);
  189. spin_lock_irq(&ctx->lock);
  190. /*
  191. * If the context is active we need to retry the smp call.
  192. */
  193. if (ctx->nr_active && !list_empty(&counter->list_entry)) {
  194. spin_unlock_irq(&ctx->lock);
  195. goto retry;
  196. }
  197. /*
  198. * The lock prevents that this context is scheduled in so we
  199. * can remove the counter safely, if the call above did not
  200. * succeed.
  201. */
  202. if (!list_empty(&counter->list_entry)) {
  203. ctx->nr_counters--;
  204. list_del_counter(counter, ctx);
  205. counter->task = NULL;
  206. }
  207. spin_unlock_irq(&ctx->lock);
  208. }
  209. /*
  210. * Cross CPU call to disable a performance counter
  211. */
  212. static void __perf_counter_disable(void *info)
  213. {
  214. struct perf_counter *counter = info;
  215. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  216. struct perf_counter_context *ctx = counter->ctx;
  217. unsigned long flags;
  218. /*
  219. * If this is a per-task counter, need to check whether this
  220. * counter's task is the current task on this cpu.
  221. */
  222. if (ctx->task && cpuctx->task_ctx != ctx)
  223. return;
  224. curr_rq_lock_irq_save(&flags);
  225. spin_lock(&ctx->lock);
  226. /*
  227. * If the counter is on, turn it off.
  228. * If it is in error state, leave it in error state.
  229. */
  230. if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
  231. if (counter == counter->group_leader)
  232. group_sched_out(counter, cpuctx, ctx);
  233. else
  234. counter_sched_out(counter, cpuctx, ctx);
  235. counter->state = PERF_COUNTER_STATE_OFF;
  236. }
  237. spin_unlock(&ctx->lock);
  238. curr_rq_unlock_irq_restore(&flags);
  239. }
  240. /*
  241. * Disable a counter.
  242. */
  243. static void perf_counter_disable(struct perf_counter *counter)
  244. {
  245. struct perf_counter_context *ctx = counter->ctx;
  246. struct task_struct *task = ctx->task;
  247. if (!task) {
  248. /*
  249. * Disable the counter on the cpu that it's on
  250. */
  251. smp_call_function_single(counter->cpu, __perf_counter_disable,
  252. counter, 1);
  253. return;
  254. }
  255. retry:
  256. task_oncpu_function_call(task, __perf_counter_disable, counter);
  257. spin_lock_irq(&ctx->lock);
  258. /*
  259. * If the counter is still active, we need to retry the cross-call.
  260. */
  261. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  262. spin_unlock_irq(&ctx->lock);
  263. goto retry;
  264. }
  265. /*
  266. * Since we have the lock this context can't be scheduled
  267. * in, so we can change the state safely.
  268. */
  269. if (counter->state == PERF_COUNTER_STATE_INACTIVE)
  270. counter->state = PERF_COUNTER_STATE_OFF;
  271. spin_unlock_irq(&ctx->lock);
  272. }
  273. /*
  274. * Disable a counter and all its children.
  275. */
  276. static void perf_counter_disable_family(struct perf_counter *counter)
  277. {
  278. struct perf_counter *child;
  279. perf_counter_disable(counter);
  280. /*
  281. * Lock the mutex to protect the list of children
  282. */
  283. mutex_lock(&counter->mutex);
  284. list_for_each_entry(child, &counter->child_list, child_list)
  285. perf_counter_disable(child);
  286. mutex_unlock(&counter->mutex);
  287. }
  288. static int
  289. counter_sched_in(struct perf_counter *counter,
  290. struct perf_cpu_context *cpuctx,
  291. struct perf_counter_context *ctx,
  292. int cpu)
  293. {
  294. if (counter->state <= PERF_COUNTER_STATE_OFF)
  295. return 0;
  296. counter->state = PERF_COUNTER_STATE_ACTIVE;
  297. counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
  298. /*
  299. * The new state must be visible before we turn it on in the hardware:
  300. */
  301. smp_wmb();
  302. if (counter->hw_ops->enable(counter)) {
  303. counter->state = PERF_COUNTER_STATE_INACTIVE;
  304. counter->oncpu = -1;
  305. return -EAGAIN;
  306. }
  307. if (!is_software_counter(counter))
  308. cpuctx->active_oncpu++;
  309. ctx->nr_active++;
  310. if (counter->hw_event.exclusive)
  311. cpuctx->exclusive = 1;
  312. return 0;
  313. }
  314. /*
  315. * Return 1 for a group consisting entirely of software counters,
  316. * 0 if the group contains any hardware counters.
  317. */
  318. static int is_software_only_group(struct perf_counter *leader)
  319. {
  320. struct perf_counter *counter;
  321. if (!is_software_counter(leader))
  322. return 0;
  323. list_for_each_entry(counter, &leader->sibling_list, list_entry)
  324. if (!is_software_counter(counter))
  325. return 0;
  326. return 1;
  327. }
  328. /*
  329. * Work out whether we can put this counter group on the CPU now.
  330. */
  331. static int group_can_go_on(struct perf_counter *counter,
  332. struct perf_cpu_context *cpuctx,
  333. int can_add_hw)
  334. {
  335. /*
  336. * Groups consisting entirely of software counters can always go on.
  337. */
  338. if (is_software_only_group(counter))
  339. return 1;
  340. /*
  341. * If an exclusive group is already on, no other hardware
  342. * counters can go on.
  343. */
  344. if (cpuctx->exclusive)
  345. return 0;
  346. /*
  347. * If this group is exclusive and there are already
  348. * counters on the CPU, it can't go on.
  349. */
  350. if (counter->hw_event.exclusive && cpuctx->active_oncpu)
  351. return 0;
  352. /*
  353. * Otherwise, try to add it if all previous groups were able
  354. * to go on.
  355. */
  356. return can_add_hw;
  357. }
  358. /*
  359. * Cross CPU call to install and enable a performance counter
  360. */
  361. static void __perf_install_in_context(void *info)
  362. {
  363. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  364. struct perf_counter *counter = info;
  365. struct perf_counter_context *ctx = counter->ctx;
  366. struct perf_counter *leader = counter->group_leader;
  367. int cpu = smp_processor_id();
  368. unsigned long flags;
  369. u64 perf_flags;
  370. int err;
  371. /*
  372. * If this is a task context, we need to check whether it is
  373. * the current task context of this cpu. If not it has been
  374. * scheduled out before the smp call arrived.
  375. */
  376. if (ctx->task && cpuctx->task_ctx != ctx)
  377. return;
  378. curr_rq_lock_irq_save(&flags);
  379. spin_lock(&ctx->lock);
  380. /*
  381. * Protect the list operation against NMI by disabling the
  382. * counters on a global level. NOP for non NMI based counters.
  383. */
  384. perf_flags = hw_perf_save_disable();
  385. list_add_counter(counter, ctx);
  386. ctx->nr_counters++;
  387. counter->prev_state = PERF_COUNTER_STATE_OFF;
  388. /*
  389. * Don't put the counter on if it is disabled or if
  390. * it is in a group and the group isn't on.
  391. */
  392. if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
  393. (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
  394. goto unlock;
  395. /*
  396. * An exclusive counter can't go on if there are already active
  397. * hardware counters, and no hardware counter can go on if there
  398. * is already an exclusive counter on.
  399. */
  400. if (!group_can_go_on(counter, cpuctx, 1))
  401. err = -EEXIST;
  402. else
  403. err = counter_sched_in(counter, cpuctx, ctx, cpu);
  404. if (err) {
  405. /*
  406. * This counter couldn't go on. If it is in a group
  407. * then we have to pull the whole group off.
  408. * If the counter group is pinned then put it in error state.
  409. */
  410. if (leader != counter)
  411. group_sched_out(leader, cpuctx, ctx);
  412. if (leader->hw_event.pinned)
  413. leader->state = PERF_COUNTER_STATE_ERROR;
  414. }
  415. if (!err && !ctx->task && cpuctx->max_pertask)
  416. cpuctx->max_pertask--;
  417. unlock:
  418. hw_perf_restore(perf_flags);
  419. spin_unlock(&ctx->lock);
  420. curr_rq_unlock_irq_restore(&flags);
  421. }
  422. /*
  423. * Attach a performance counter to a context
  424. *
  425. * First we add the counter to the list with the hardware enable bit
  426. * in counter->hw_config cleared.
  427. *
  428. * If the counter is attached to a task which is on a CPU we use a smp
  429. * call to enable it in the task context. The task might have been
  430. * scheduled away, but we check this in the smp call again.
  431. *
  432. * Must be called with ctx->mutex held.
  433. */
  434. static void
  435. perf_install_in_context(struct perf_counter_context *ctx,
  436. struct perf_counter *counter,
  437. int cpu)
  438. {
  439. struct task_struct *task = ctx->task;
  440. if (!task) {
  441. /*
  442. * Per cpu counters are installed via an smp call and
  443. * the install is always sucessful.
  444. */
  445. smp_call_function_single(cpu, __perf_install_in_context,
  446. counter, 1);
  447. return;
  448. }
  449. counter->task = task;
  450. retry:
  451. task_oncpu_function_call(task, __perf_install_in_context,
  452. counter);
  453. spin_lock_irq(&ctx->lock);
  454. /*
  455. * we need to retry the smp call.
  456. */
  457. if (ctx->is_active && list_empty(&counter->list_entry)) {
  458. spin_unlock_irq(&ctx->lock);
  459. goto retry;
  460. }
  461. /*
  462. * The lock prevents that this context is scheduled in so we
  463. * can add the counter safely, if it the call above did not
  464. * succeed.
  465. */
  466. if (list_empty(&counter->list_entry)) {
  467. list_add_counter(counter, ctx);
  468. ctx->nr_counters++;
  469. }
  470. spin_unlock_irq(&ctx->lock);
  471. }
  472. /*
  473. * Cross CPU call to enable a performance counter
  474. */
  475. static void __perf_counter_enable(void *info)
  476. {
  477. struct perf_counter *counter = info;
  478. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  479. struct perf_counter_context *ctx = counter->ctx;
  480. struct perf_counter *leader = counter->group_leader;
  481. unsigned long flags;
  482. int err;
  483. /*
  484. * If this is a per-task counter, need to check whether this
  485. * counter's task is the current task on this cpu.
  486. */
  487. if (ctx->task && cpuctx->task_ctx != ctx)
  488. return;
  489. curr_rq_lock_irq_save(&flags);
  490. spin_lock(&ctx->lock);
  491. counter->prev_state = counter->state;
  492. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  493. goto unlock;
  494. counter->state = PERF_COUNTER_STATE_INACTIVE;
  495. /*
  496. * If the counter is in a group and isn't the group leader,
  497. * then don't put it on unless the group is on.
  498. */
  499. if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
  500. goto unlock;
  501. if (!group_can_go_on(counter, cpuctx, 1))
  502. err = -EEXIST;
  503. else
  504. err = counter_sched_in(counter, cpuctx, ctx,
  505. smp_processor_id());
  506. if (err) {
  507. /*
  508. * If this counter can't go on and it's part of a
  509. * group, then the whole group has to come off.
  510. */
  511. if (leader != counter)
  512. group_sched_out(leader, cpuctx, ctx);
  513. if (leader->hw_event.pinned)
  514. leader->state = PERF_COUNTER_STATE_ERROR;
  515. }
  516. unlock:
  517. spin_unlock(&ctx->lock);
  518. curr_rq_unlock_irq_restore(&flags);
  519. }
  520. /*
  521. * Enable a counter.
  522. */
  523. static void perf_counter_enable(struct perf_counter *counter)
  524. {
  525. struct perf_counter_context *ctx = counter->ctx;
  526. struct task_struct *task = ctx->task;
  527. if (!task) {
  528. /*
  529. * Enable the counter on the cpu that it's on
  530. */
  531. smp_call_function_single(counter->cpu, __perf_counter_enable,
  532. counter, 1);
  533. return;
  534. }
  535. spin_lock_irq(&ctx->lock);
  536. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  537. goto out;
  538. /*
  539. * If the counter is in error state, clear that first.
  540. * That way, if we see the counter in error state below, we
  541. * know that it has gone back into error state, as distinct
  542. * from the task having been scheduled away before the
  543. * cross-call arrived.
  544. */
  545. if (counter->state == PERF_COUNTER_STATE_ERROR)
  546. counter->state = PERF_COUNTER_STATE_OFF;
  547. retry:
  548. spin_unlock_irq(&ctx->lock);
  549. task_oncpu_function_call(task, __perf_counter_enable, counter);
  550. spin_lock_irq(&ctx->lock);
  551. /*
  552. * If the context is active and the counter is still off,
  553. * we need to retry the cross-call.
  554. */
  555. if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
  556. goto retry;
  557. /*
  558. * Since we have the lock this context can't be scheduled
  559. * in, so we can change the state safely.
  560. */
  561. if (counter->state == PERF_COUNTER_STATE_OFF)
  562. counter->state = PERF_COUNTER_STATE_INACTIVE;
  563. out:
  564. spin_unlock_irq(&ctx->lock);
  565. }
  566. /*
  567. * Enable a counter and all its children.
  568. */
  569. static void perf_counter_enable_family(struct perf_counter *counter)
  570. {
  571. struct perf_counter *child;
  572. perf_counter_enable(counter);
  573. /*
  574. * Lock the mutex to protect the list of children
  575. */
  576. mutex_lock(&counter->mutex);
  577. list_for_each_entry(child, &counter->child_list, child_list)
  578. perf_counter_enable(child);
  579. mutex_unlock(&counter->mutex);
  580. }
  581. void __perf_counter_sched_out(struct perf_counter_context *ctx,
  582. struct perf_cpu_context *cpuctx)
  583. {
  584. struct perf_counter *counter;
  585. u64 flags;
  586. spin_lock(&ctx->lock);
  587. ctx->is_active = 0;
  588. if (likely(!ctx->nr_counters))
  589. goto out;
  590. flags = hw_perf_save_disable();
  591. if (ctx->nr_active) {
  592. list_for_each_entry(counter, &ctx->counter_list, list_entry)
  593. group_sched_out(counter, cpuctx, ctx);
  594. }
  595. hw_perf_restore(flags);
  596. out:
  597. spin_unlock(&ctx->lock);
  598. }
  599. /*
  600. * Called from scheduler to remove the counters of the current task,
  601. * with interrupts disabled.
  602. *
  603. * We stop each counter and update the counter value in counter->count.
  604. *
  605. * This does not protect us against NMI, but disable()
  606. * sets the disabled bit in the control field of counter _before_
  607. * accessing the counter control register. If a NMI hits, then it will
  608. * not restart the counter.
  609. */
  610. void perf_counter_task_sched_out(struct task_struct *task, int cpu)
  611. {
  612. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  613. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  614. struct pt_regs *regs;
  615. if (likely(!cpuctx->task_ctx))
  616. return;
  617. regs = task_pt_regs(task);
  618. perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs);
  619. __perf_counter_sched_out(ctx, cpuctx);
  620. cpuctx->task_ctx = NULL;
  621. }
  622. static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
  623. {
  624. __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
  625. }
  626. static int
  627. group_sched_in(struct perf_counter *group_counter,
  628. struct perf_cpu_context *cpuctx,
  629. struct perf_counter_context *ctx,
  630. int cpu)
  631. {
  632. struct perf_counter *counter, *partial_group;
  633. int ret;
  634. if (group_counter->state == PERF_COUNTER_STATE_OFF)
  635. return 0;
  636. ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
  637. if (ret)
  638. return ret < 0 ? ret : 0;
  639. group_counter->prev_state = group_counter->state;
  640. if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
  641. return -EAGAIN;
  642. /*
  643. * Schedule in siblings as one group (if any):
  644. */
  645. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  646. counter->prev_state = counter->state;
  647. if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
  648. partial_group = counter;
  649. goto group_error;
  650. }
  651. }
  652. return 0;
  653. group_error:
  654. /*
  655. * Groups can be scheduled in as one unit only, so undo any
  656. * partial group before returning:
  657. */
  658. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  659. if (counter == partial_group)
  660. break;
  661. counter_sched_out(counter, cpuctx, ctx);
  662. }
  663. counter_sched_out(group_counter, cpuctx, ctx);
  664. return -EAGAIN;
  665. }
  666. static void
  667. __perf_counter_sched_in(struct perf_counter_context *ctx,
  668. struct perf_cpu_context *cpuctx, int cpu)
  669. {
  670. struct perf_counter *counter;
  671. u64 flags;
  672. int can_add_hw = 1;
  673. spin_lock(&ctx->lock);
  674. ctx->is_active = 1;
  675. if (likely(!ctx->nr_counters))
  676. goto out;
  677. flags = hw_perf_save_disable();
  678. /*
  679. * First go through the list and put on any pinned groups
  680. * in order to give them the best chance of going on.
  681. */
  682. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  683. if (counter->state <= PERF_COUNTER_STATE_OFF ||
  684. !counter->hw_event.pinned)
  685. continue;
  686. if (counter->cpu != -1 && counter->cpu != cpu)
  687. continue;
  688. if (group_can_go_on(counter, cpuctx, 1))
  689. group_sched_in(counter, cpuctx, ctx, cpu);
  690. /*
  691. * If this pinned group hasn't been scheduled,
  692. * put it in error state.
  693. */
  694. if (counter->state == PERF_COUNTER_STATE_INACTIVE)
  695. counter->state = PERF_COUNTER_STATE_ERROR;
  696. }
  697. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  698. /*
  699. * Ignore counters in OFF or ERROR state, and
  700. * ignore pinned counters since we did them already.
  701. */
  702. if (counter->state <= PERF_COUNTER_STATE_OFF ||
  703. counter->hw_event.pinned)
  704. continue;
  705. /*
  706. * Listen to the 'cpu' scheduling filter constraint
  707. * of counters:
  708. */
  709. if (counter->cpu != -1 && counter->cpu != cpu)
  710. continue;
  711. if (group_can_go_on(counter, cpuctx, can_add_hw)) {
  712. if (group_sched_in(counter, cpuctx, ctx, cpu))
  713. can_add_hw = 0;
  714. }
  715. }
  716. hw_perf_restore(flags);
  717. out:
  718. spin_unlock(&ctx->lock);
  719. }
  720. /*
  721. * Called from scheduler to add the counters of the current task
  722. * with interrupts disabled.
  723. *
  724. * We restore the counter value and then enable it.
  725. *
  726. * This does not protect us against NMI, but enable()
  727. * sets the enabled bit in the control field of counter _before_
  728. * accessing the counter control register. If a NMI hits, then it will
  729. * keep the counter running.
  730. */
  731. void perf_counter_task_sched_in(struct task_struct *task, int cpu)
  732. {
  733. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  734. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  735. __perf_counter_sched_in(ctx, cpuctx, cpu);
  736. cpuctx->task_ctx = ctx;
  737. }
  738. static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
  739. {
  740. struct perf_counter_context *ctx = &cpuctx->ctx;
  741. __perf_counter_sched_in(ctx, cpuctx, cpu);
  742. }
  743. int perf_counter_task_disable(void)
  744. {
  745. struct task_struct *curr = current;
  746. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  747. struct perf_counter *counter;
  748. unsigned long flags;
  749. u64 perf_flags;
  750. int cpu;
  751. if (likely(!ctx->nr_counters))
  752. return 0;
  753. curr_rq_lock_irq_save(&flags);
  754. cpu = smp_processor_id();
  755. /* force the update of the task clock: */
  756. __task_delta_exec(curr, 1);
  757. perf_counter_task_sched_out(curr, cpu);
  758. spin_lock(&ctx->lock);
  759. /*
  760. * Disable all the counters:
  761. */
  762. perf_flags = hw_perf_save_disable();
  763. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  764. if (counter->state != PERF_COUNTER_STATE_ERROR)
  765. counter->state = PERF_COUNTER_STATE_OFF;
  766. }
  767. hw_perf_restore(perf_flags);
  768. spin_unlock(&ctx->lock);
  769. curr_rq_unlock_irq_restore(&flags);
  770. return 0;
  771. }
  772. int perf_counter_task_enable(void)
  773. {
  774. struct task_struct *curr = current;
  775. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  776. struct perf_counter *counter;
  777. unsigned long flags;
  778. u64 perf_flags;
  779. int cpu;
  780. if (likely(!ctx->nr_counters))
  781. return 0;
  782. curr_rq_lock_irq_save(&flags);
  783. cpu = smp_processor_id();
  784. /* force the update of the task clock: */
  785. __task_delta_exec(curr, 1);
  786. perf_counter_task_sched_out(curr, cpu);
  787. spin_lock(&ctx->lock);
  788. /*
  789. * Disable all the counters:
  790. */
  791. perf_flags = hw_perf_save_disable();
  792. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  793. if (counter->state > PERF_COUNTER_STATE_OFF)
  794. continue;
  795. counter->state = PERF_COUNTER_STATE_INACTIVE;
  796. counter->hw_event.disabled = 0;
  797. }
  798. hw_perf_restore(perf_flags);
  799. spin_unlock(&ctx->lock);
  800. perf_counter_task_sched_in(curr, cpu);
  801. curr_rq_unlock_irq_restore(&flags);
  802. return 0;
  803. }
  804. /*
  805. * Round-robin a context's counters:
  806. */
  807. static void rotate_ctx(struct perf_counter_context *ctx)
  808. {
  809. struct perf_counter *counter;
  810. u64 perf_flags;
  811. if (!ctx->nr_counters)
  812. return;
  813. spin_lock(&ctx->lock);
  814. /*
  815. * Rotate the first entry last (works just fine for group counters too):
  816. */
  817. perf_flags = hw_perf_save_disable();
  818. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  819. list_move_tail(&counter->list_entry, &ctx->counter_list);
  820. break;
  821. }
  822. hw_perf_restore(perf_flags);
  823. spin_unlock(&ctx->lock);
  824. }
  825. void perf_counter_task_tick(struct task_struct *curr, int cpu)
  826. {
  827. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  828. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  829. const int rotate_percpu = 0;
  830. if (rotate_percpu)
  831. perf_counter_cpu_sched_out(cpuctx);
  832. perf_counter_task_sched_out(curr, cpu);
  833. if (rotate_percpu)
  834. rotate_ctx(&cpuctx->ctx);
  835. rotate_ctx(ctx);
  836. if (rotate_percpu)
  837. perf_counter_cpu_sched_in(cpuctx, cpu);
  838. perf_counter_task_sched_in(curr, cpu);
  839. }
  840. /*
  841. * Cross CPU call to read the hardware counter
  842. */
  843. static void __read(void *info)
  844. {
  845. struct perf_counter *counter = info;
  846. unsigned long flags;
  847. curr_rq_lock_irq_save(&flags);
  848. counter->hw_ops->read(counter);
  849. curr_rq_unlock_irq_restore(&flags);
  850. }
  851. static u64 perf_counter_read(struct perf_counter *counter)
  852. {
  853. /*
  854. * If counter is enabled and currently active on a CPU, update the
  855. * value in the counter structure:
  856. */
  857. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  858. smp_call_function_single(counter->oncpu,
  859. __read, counter, 1);
  860. }
  861. return atomic64_read(&counter->count);
  862. }
  863. /*
  864. * Cross CPU call to switch performance data pointers
  865. */
  866. static void __perf_switch_irq_data(void *info)
  867. {
  868. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  869. struct perf_counter *counter = info;
  870. struct perf_counter_context *ctx = counter->ctx;
  871. struct perf_data *oldirqdata = counter->irqdata;
  872. /*
  873. * If this is a task context, we need to check whether it is
  874. * the current task context of this cpu. If not it has been
  875. * scheduled out before the smp call arrived.
  876. */
  877. if (ctx->task) {
  878. if (cpuctx->task_ctx != ctx)
  879. return;
  880. spin_lock(&ctx->lock);
  881. }
  882. /* Change the pointer NMI safe */
  883. atomic_long_set((atomic_long_t *)&counter->irqdata,
  884. (unsigned long) counter->usrdata);
  885. counter->usrdata = oldirqdata;
  886. if (ctx->task)
  887. spin_unlock(&ctx->lock);
  888. }
  889. static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
  890. {
  891. struct perf_counter_context *ctx = counter->ctx;
  892. struct perf_data *oldirqdata = counter->irqdata;
  893. struct task_struct *task = ctx->task;
  894. if (!task) {
  895. smp_call_function_single(counter->cpu,
  896. __perf_switch_irq_data,
  897. counter, 1);
  898. return counter->usrdata;
  899. }
  900. retry:
  901. spin_lock_irq(&ctx->lock);
  902. if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
  903. counter->irqdata = counter->usrdata;
  904. counter->usrdata = oldirqdata;
  905. spin_unlock_irq(&ctx->lock);
  906. return oldirqdata;
  907. }
  908. spin_unlock_irq(&ctx->lock);
  909. task_oncpu_function_call(task, __perf_switch_irq_data, counter);
  910. /* Might have failed, because task was scheduled out */
  911. if (counter->irqdata == oldirqdata)
  912. goto retry;
  913. return counter->usrdata;
  914. }
  915. static void put_context(struct perf_counter_context *ctx)
  916. {
  917. if (ctx->task)
  918. put_task_struct(ctx->task);
  919. }
  920. static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  921. {
  922. struct perf_cpu_context *cpuctx;
  923. struct perf_counter_context *ctx;
  924. struct task_struct *task;
  925. /*
  926. * If cpu is not a wildcard then this is a percpu counter:
  927. */
  928. if (cpu != -1) {
  929. /* Must be root to operate on a CPU counter: */
  930. if (!capable(CAP_SYS_ADMIN))
  931. return ERR_PTR(-EACCES);
  932. if (cpu < 0 || cpu > num_possible_cpus())
  933. return ERR_PTR(-EINVAL);
  934. /*
  935. * We could be clever and allow to attach a counter to an
  936. * offline CPU and activate it when the CPU comes up, but
  937. * that's for later.
  938. */
  939. if (!cpu_isset(cpu, cpu_online_map))
  940. return ERR_PTR(-ENODEV);
  941. cpuctx = &per_cpu(perf_cpu_context, cpu);
  942. ctx = &cpuctx->ctx;
  943. return ctx;
  944. }
  945. rcu_read_lock();
  946. if (!pid)
  947. task = current;
  948. else
  949. task = find_task_by_vpid(pid);
  950. if (task)
  951. get_task_struct(task);
  952. rcu_read_unlock();
  953. if (!task)
  954. return ERR_PTR(-ESRCH);
  955. ctx = &task->perf_counter_ctx;
  956. ctx->task = task;
  957. /* Reuse ptrace permission checks for now. */
  958. if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
  959. put_context(ctx);
  960. return ERR_PTR(-EACCES);
  961. }
  962. return ctx;
  963. }
  964. static void free_counter_rcu(struct rcu_head *head)
  965. {
  966. struct perf_counter *counter;
  967. counter = container_of(head, struct perf_counter, rcu_head);
  968. kfree(counter);
  969. }
  970. static void free_counter(struct perf_counter *counter)
  971. {
  972. if (counter->destroy)
  973. counter->destroy(counter);
  974. call_rcu(&counter->rcu_head, free_counter_rcu);
  975. }
  976. /*
  977. * Called when the last reference to the file is gone.
  978. */
  979. static int perf_release(struct inode *inode, struct file *file)
  980. {
  981. struct perf_counter *counter = file->private_data;
  982. struct perf_counter_context *ctx = counter->ctx;
  983. file->private_data = NULL;
  984. mutex_lock(&ctx->mutex);
  985. mutex_lock(&counter->mutex);
  986. perf_counter_remove_from_context(counter);
  987. mutex_unlock(&counter->mutex);
  988. mutex_unlock(&ctx->mutex);
  989. free_counter(counter);
  990. put_context(ctx);
  991. return 0;
  992. }
  993. /*
  994. * Read the performance counter - simple non blocking version for now
  995. */
  996. static ssize_t
  997. perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
  998. {
  999. u64 cntval;
  1000. if (count != sizeof(cntval))
  1001. return -EINVAL;
  1002. /*
  1003. * Return end-of-file for a read on a counter that is in
  1004. * error state (i.e. because it was pinned but it couldn't be
  1005. * scheduled on to the CPU at some point).
  1006. */
  1007. if (counter->state == PERF_COUNTER_STATE_ERROR)
  1008. return 0;
  1009. mutex_lock(&counter->mutex);
  1010. cntval = perf_counter_read(counter);
  1011. mutex_unlock(&counter->mutex);
  1012. return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
  1013. }
  1014. static ssize_t
  1015. perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
  1016. {
  1017. if (!usrdata->len)
  1018. return 0;
  1019. count = min(count, (size_t)usrdata->len);
  1020. if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
  1021. return -EFAULT;
  1022. /* Adjust the counters */
  1023. usrdata->len -= count;
  1024. if (!usrdata->len)
  1025. usrdata->rd_idx = 0;
  1026. else
  1027. usrdata->rd_idx += count;
  1028. return count;
  1029. }
  1030. static ssize_t
  1031. perf_read_irq_data(struct perf_counter *counter,
  1032. char __user *buf,
  1033. size_t count,
  1034. int nonblocking)
  1035. {
  1036. struct perf_data *irqdata, *usrdata;
  1037. DECLARE_WAITQUEUE(wait, current);
  1038. ssize_t res, res2;
  1039. irqdata = counter->irqdata;
  1040. usrdata = counter->usrdata;
  1041. if (usrdata->len + irqdata->len >= count)
  1042. goto read_pending;
  1043. if (nonblocking)
  1044. return -EAGAIN;
  1045. spin_lock_irq(&counter->waitq.lock);
  1046. __add_wait_queue(&counter->waitq, &wait);
  1047. for (;;) {
  1048. set_current_state(TASK_INTERRUPTIBLE);
  1049. if (usrdata->len + irqdata->len >= count)
  1050. break;
  1051. if (signal_pending(current))
  1052. break;
  1053. if (counter->state == PERF_COUNTER_STATE_ERROR)
  1054. break;
  1055. spin_unlock_irq(&counter->waitq.lock);
  1056. schedule();
  1057. spin_lock_irq(&counter->waitq.lock);
  1058. }
  1059. __remove_wait_queue(&counter->waitq, &wait);
  1060. __set_current_state(TASK_RUNNING);
  1061. spin_unlock_irq(&counter->waitq.lock);
  1062. if (usrdata->len + irqdata->len < count &&
  1063. counter->state != PERF_COUNTER_STATE_ERROR)
  1064. return -ERESTARTSYS;
  1065. read_pending:
  1066. mutex_lock(&counter->mutex);
  1067. /* Drain pending data first: */
  1068. res = perf_copy_usrdata(usrdata, buf, count);
  1069. if (res < 0 || res == count)
  1070. goto out;
  1071. /* Switch irq buffer: */
  1072. usrdata = perf_switch_irq_data(counter);
  1073. res2 = perf_copy_usrdata(usrdata, buf + res, count - res);
  1074. if (res2 < 0) {
  1075. if (!res)
  1076. res = -EFAULT;
  1077. } else {
  1078. res += res2;
  1079. }
  1080. out:
  1081. mutex_unlock(&counter->mutex);
  1082. return res;
  1083. }
  1084. static ssize_t
  1085. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  1086. {
  1087. struct perf_counter *counter = file->private_data;
  1088. switch (counter->hw_event.record_type) {
  1089. case PERF_RECORD_SIMPLE:
  1090. return perf_read_hw(counter, buf, count);
  1091. case PERF_RECORD_IRQ:
  1092. case PERF_RECORD_GROUP:
  1093. return perf_read_irq_data(counter, buf, count,
  1094. file->f_flags & O_NONBLOCK);
  1095. }
  1096. return -EINVAL;
  1097. }
  1098. static unsigned int perf_poll(struct file *file, poll_table *wait)
  1099. {
  1100. struct perf_counter *counter = file->private_data;
  1101. unsigned int events = 0;
  1102. unsigned long flags;
  1103. poll_wait(file, &counter->waitq, wait);
  1104. spin_lock_irqsave(&counter->waitq.lock, flags);
  1105. if (counter->usrdata->len || counter->irqdata->len)
  1106. events |= POLLIN;
  1107. spin_unlock_irqrestore(&counter->waitq.lock, flags);
  1108. return events;
  1109. }
  1110. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1111. {
  1112. struct perf_counter *counter = file->private_data;
  1113. int err = 0;
  1114. switch (cmd) {
  1115. case PERF_COUNTER_IOC_ENABLE:
  1116. perf_counter_enable_family(counter);
  1117. break;
  1118. case PERF_COUNTER_IOC_DISABLE:
  1119. perf_counter_disable_family(counter);
  1120. break;
  1121. default:
  1122. err = -ENOTTY;
  1123. }
  1124. return err;
  1125. }
  1126. static const struct file_operations perf_fops = {
  1127. .release = perf_release,
  1128. .read = perf_read,
  1129. .poll = perf_poll,
  1130. .unlocked_ioctl = perf_ioctl,
  1131. .compat_ioctl = perf_ioctl,
  1132. };
  1133. /*
  1134. * Output
  1135. */
  1136. static void perf_counter_store_irq(struct perf_counter *counter, u64 data)
  1137. {
  1138. struct perf_data *irqdata = counter->irqdata;
  1139. if (irqdata->len > PERF_DATA_BUFLEN - sizeof(u64)) {
  1140. irqdata->overrun++;
  1141. } else {
  1142. u64 *p = (u64 *) &irqdata->data[irqdata->len];
  1143. *p = data;
  1144. irqdata->len += sizeof(u64);
  1145. }
  1146. }
  1147. static void perf_counter_handle_group(struct perf_counter *counter)
  1148. {
  1149. struct perf_counter *leader, *sub;
  1150. leader = counter->group_leader;
  1151. list_for_each_entry(sub, &leader->sibling_list, list_entry) {
  1152. if (sub != counter)
  1153. sub->hw_ops->read(sub);
  1154. perf_counter_store_irq(counter, sub->hw_event.event_config);
  1155. perf_counter_store_irq(counter, atomic64_read(&sub->count));
  1156. }
  1157. }
  1158. void perf_counter_output(struct perf_counter *counter,
  1159. int nmi, struct pt_regs *regs)
  1160. {
  1161. switch (counter->hw_event.record_type) {
  1162. case PERF_RECORD_SIMPLE:
  1163. return;
  1164. case PERF_RECORD_IRQ:
  1165. perf_counter_store_irq(counter, instruction_pointer(regs));
  1166. break;
  1167. case PERF_RECORD_GROUP:
  1168. perf_counter_handle_group(counter);
  1169. break;
  1170. }
  1171. if (nmi) {
  1172. counter->wakeup_pending = 1;
  1173. set_perf_counter_pending();
  1174. } else
  1175. wake_up(&counter->waitq);
  1176. }
  1177. /*
  1178. * Generic software counter infrastructure
  1179. */
  1180. static void perf_swcounter_update(struct perf_counter *counter)
  1181. {
  1182. struct hw_perf_counter *hwc = &counter->hw;
  1183. u64 prev, now;
  1184. s64 delta;
  1185. again:
  1186. prev = atomic64_read(&hwc->prev_count);
  1187. now = atomic64_read(&hwc->count);
  1188. if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
  1189. goto again;
  1190. delta = now - prev;
  1191. atomic64_add(delta, &counter->count);
  1192. atomic64_sub(delta, &hwc->period_left);
  1193. }
  1194. static void perf_swcounter_set_period(struct perf_counter *counter)
  1195. {
  1196. struct hw_perf_counter *hwc = &counter->hw;
  1197. s64 left = atomic64_read(&hwc->period_left);
  1198. s64 period = hwc->irq_period;
  1199. if (unlikely(left <= -period)) {
  1200. left = period;
  1201. atomic64_set(&hwc->period_left, left);
  1202. }
  1203. if (unlikely(left <= 0)) {
  1204. left += period;
  1205. atomic64_add(period, &hwc->period_left);
  1206. }
  1207. atomic64_set(&hwc->prev_count, -left);
  1208. atomic64_set(&hwc->count, -left);
  1209. }
  1210. static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
  1211. {
  1212. struct perf_counter *counter;
  1213. struct pt_regs *regs;
  1214. counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
  1215. counter->hw_ops->read(counter);
  1216. regs = get_irq_regs();
  1217. /*
  1218. * In case we exclude kernel IPs or are somehow not in interrupt
  1219. * context, provide the next best thing, the user IP.
  1220. */
  1221. if ((counter->hw_event.exclude_kernel || !regs) &&
  1222. !counter->hw_event.exclude_user)
  1223. regs = task_pt_regs(current);
  1224. if (regs)
  1225. perf_counter_output(counter, 0, regs);
  1226. hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
  1227. return HRTIMER_RESTART;
  1228. }
  1229. static void perf_swcounter_overflow(struct perf_counter *counter,
  1230. int nmi, struct pt_regs *regs)
  1231. {
  1232. perf_swcounter_update(counter);
  1233. perf_swcounter_set_period(counter);
  1234. perf_counter_output(counter, nmi, regs);
  1235. }
  1236. static int perf_swcounter_match(struct perf_counter *counter,
  1237. enum perf_event_types type,
  1238. u32 event, struct pt_regs *regs)
  1239. {
  1240. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  1241. return 0;
  1242. if (counter->hw_event.raw_type)
  1243. return 0;
  1244. if (counter->hw_event.type != type)
  1245. return 0;
  1246. if (counter->hw_event.event_id != event)
  1247. return 0;
  1248. if (counter->hw_event.exclude_user && user_mode(regs))
  1249. return 0;
  1250. if (counter->hw_event.exclude_kernel && !user_mode(regs))
  1251. return 0;
  1252. return 1;
  1253. }
  1254. static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
  1255. int nmi, struct pt_regs *regs)
  1256. {
  1257. int neg = atomic64_add_negative(nr, &counter->hw.count);
  1258. if (counter->hw.irq_period && !neg)
  1259. perf_swcounter_overflow(counter, nmi, regs);
  1260. }
  1261. static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
  1262. enum perf_event_types type, u32 event,
  1263. u64 nr, int nmi, struct pt_regs *regs)
  1264. {
  1265. struct perf_counter *counter;
  1266. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  1267. return;
  1268. rcu_read_lock();
  1269. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  1270. if (perf_swcounter_match(counter, type, event, regs))
  1271. perf_swcounter_add(counter, nr, nmi, regs);
  1272. }
  1273. rcu_read_unlock();
  1274. }
  1275. static void __perf_swcounter_event(enum perf_event_types type, u32 event,
  1276. u64 nr, int nmi, struct pt_regs *regs)
  1277. {
  1278. struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
  1279. perf_swcounter_ctx_event(&cpuctx->ctx, type, event, nr, nmi, regs);
  1280. if (cpuctx->task_ctx) {
  1281. perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
  1282. nr, nmi, regs);
  1283. }
  1284. put_cpu_var(perf_cpu_context);
  1285. }
  1286. void perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs)
  1287. {
  1288. __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs);
  1289. }
  1290. static void perf_swcounter_read(struct perf_counter *counter)
  1291. {
  1292. perf_swcounter_update(counter);
  1293. }
  1294. static int perf_swcounter_enable(struct perf_counter *counter)
  1295. {
  1296. perf_swcounter_set_period(counter);
  1297. return 0;
  1298. }
  1299. static void perf_swcounter_disable(struct perf_counter *counter)
  1300. {
  1301. perf_swcounter_update(counter);
  1302. }
  1303. static const struct hw_perf_counter_ops perf_ops_generic = {
  1304. .enable = perf_swcounter_enable,
  1305. .disable = perf_swcounter_disable,
  1306. .read = perf_swcounter_read,
  1307. };
  1308. /*
  1309. * Software counter: cpu wall time clock
  1310. */
  1311. static void cpu_clock_perf_counter_update(struct perf_counter *counter)
  1312. {
  1313. int cpu = raw_smp_processor_id();
  1314. s64 prev;
  1315. u64 now;
  1316. now = cpu_clock(cpu);
  1317. prev = atomic64_read(&counter->hw.prev_count);
  1318. atomic64_set(&counter->hw.prev_count, now);
  1319. atomic64_add(now - prev, &counter->count);
  1320. }
  1321. static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
  1322. {
  1323. struct hw_perf_counter *hwc = &counter->hw;
  1324. int cpu = raw_smp_processor_id();
  1325. atomic64_set(&hwc->prev_count, cpu_clock(cpu));
  1326. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1327. hwc->hrtimer.function = perf_swcounter_hrtimer;
  1328. if (hwc->irq_period) {
  1329. __hrtimer_start_range_ns(&hwc->hrtimer,
  1330. ns_to_ktime(hwc->irq_period), 0,
  1331. HRTIMER_MODE_REL, 0);
  1332. }
  1333. return 0;
  1334. }
  1335. static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
  1336. {
  1337. hrtimer_cancel(&counter->hw.hrtimer);
  1338. cpu_clock_perf_counter_update(counter);
  1339. }
  1340. static void cpu_clock_perf_counter_read(struct perf_counter *counter)
  1341. {
  1342. cpu_clock_perf_counter_update(counter);
  1343. }
  1344. static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
  1345. .enable = cpu_clock_perf_counter_enable,
  1346. .disable = cpu_clock_perf_counter_disable,
  1347. .read = cpu_clock_perf_counter_read,
  1348. };
  1349. /*
  1350. * Software counter: task time clock
  1351. */
  1352. /*
  1353. * Called from within the scheduler:
  1354. */
  1355. static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
  1356. {
  1357. struct task_struct *curr = counter->task;
  1358. u64 delta;
  1359. delta = __task_delta_exec(curr, update);
  1360. return curr->se.sum_exec_runtime + delta;
  1361. }
  1362. static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
  1363. {
  1364. u64 prev;
  1365. s64 delta;
  1366. prev = atomic64_read(&counter->hw.prev_count);
  1367. atomic64_set(&counter->hw.prev_count, now);
  1368. delta = now - prev;
  1369. atomic64_add(delta, &counter->count);
  1370. }
  1371. static int task_clock_perf_counter_enable(struct perf_counter *counter)
  1372. {
  1373. struct hw_perf_counter *hwc = &counter->hw;
  1374. atomic64_set(&hwc->prev_count, task_clock_perf_counter_val(counter, 0));
  1375. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1376. hwc->hrtimer.function = perf_swcounter_hrtimer;
  1377. if (hwc->irq_period) {
  1378. __hrtimer_start_range_ns(&hwc->hrtimer,
  1379. ns_to_ktime(hwc->irq_period), 0,
  1380. HRTIMER_MODE_REL, 0);
  1381. }
  1382. return 0;
  1383. }
  1384. static void task_clock_perf_counter_disable(struct perf_counter *counter)
  1385. {
  1386. hrtimer_cancel(&counter->hw.hrtimer);
  1387. task_clock_perf_counter_update(counter,
  1388. task_clock_perf_counter_val(counter, 0));
  1389. }
  1390. static void task_clock_perf_counter_read(struct perf_counter *counter)
  1391. {
  1392. task_clock_perf_counter_update(counter,
  1393. task_clock_perf_counter_val(counter, 1));
  1394. }
  1395. static const struct hw_perf_counter_ops perf_ops_task_clock = {
  1396. .enable = task_clock_perf_counter_enable,
  1397. .disable = task_clock_perf_counter_disable,
  1398. .read = task_clock_perf_counter_read,
  1399. };
  1400. /*
  1401. * Software counter: cpu migrations
  1402. */
  1403. static inline u64 get_cpu_migrations(struct perf_counter *counter)
  1404. {
  1405. struct task_struct *curr = counter->ctx->task;
  1406. if (curr)
  1407. return curr->se.nr_migrations;
  1408. return cpu_nr_migrations(smp_processor_id());
  1409. }
  1410. static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
  1411. {
  1412. u64 prev, now;
  1413. s64 delta;
  1414. prev = atomic64_read(&counter->hw.prev_count);
  1415. now = get_cpu_migrations(counter);
  1416. atomic64_set(&counter->hw.prev_count, now);
  1417. delta = now - prev;
  1418. atomic64_add(delta, &counter->count);
  1419. }
  1420. static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
  1421. {
  1422. cpu_migrations_perf_counter_update(counter);
  1423. }
  1424. static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
  1425. {
  1426. if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
  1427. atomic64_set(&counter->hw.prev_count,
  1428. get_cpu_migrations(counter));
  1429. return 0;
  1430. }
  1431. static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
  1432. {
  1433. cpu_migrations_perf_counter_update(counter);
  1434. }
  1435. static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
  1436. .enable = cpu_migrations_perf_counter_enable,
  1437. .disable = cpu_migrations_perf_counter_disable,
  1438. .read = cpu_migrations_perf_counter_read,
  1439. };
  1440. #ifdef CONFIG_EVENT_PROFILE
  1441. void perf_tpcounter_event(int event_id)
  1442. {
  1443. struct pt_regs *regs = get_irq_regs();
  1444. if (!regs)
  1445. regs = task_pt_regs(current);
  1446. __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs);
  1447. }
  1448. extern int ftrace_profile_enable(int);
  1449. extern void ftrace_profile_disable(int);
  1450. static void tp_perf_counter_destroy(struct perf_counter *counter)
  1451. {
  1452. ftrace_profile_disable(counter->hw_event.event_id);
  1453. }
  1454. static const struct hw_perf_counter_ops *
  1455. tp_perf_counter_init(struct perf_counter *counter)
  1456. {
  1457. int event_id = counter->hw_event.event_id;
  1458. int ret;
  1459. ret = ftrace_profile_enable(event_id);
  1460. if (ret)
  1461. return NULL;
  1462. counter->destroy = tp_perf_counter_destroy;
  1463. counter->hw.irq_period = counter->hw_event.irq_period;
  1464. return &perf_ops_generic;
  1465. }
  1466. #else
  1467. static const struct hw_perf_counter_ops *
  1468. tp_perf_counter_init(struct perf_counter *counter)
  1469. {
  1470. return NULL;
  1471. }
  1472. #endif
  1473. static const struct hw_perf_counter_ops *
  1474. sw_perf_counter_init(struct perf_counter *counter)
  1475. {
  1476. struct perf_counter_hw_event *hw_event = &counter->hw_event;
  1477. const struct hw_perf_counter_ops *hw_ops = NULL;
  1478. struct hw_perf_counter *hwc = &counter->hw;
  1479. /*
  1480. * Software counters (currently) can't in general distinguish
  1481. * between user, kernel and hypervisor events.
  1482. * However, context switches and cpu migrations are considered
  1483. * to be kernel events, and page faults are never hypervisor
  1484. * events.
  1485. */
  1486. switch (counter->hw_event.event_id) {
  1487. case PERF_COUNT_CPU_CLOCK:
  1488. hw_ops = &perf_ops_cpu_clock;
  1489. if (hw_event->irq_period && hw_event->irq_period < 10000)
  1490. hw_event->irq_period = 10000;
  1491. break;
  1492. case PERF_COUNT_TASK_CLOCK:
  1493. /*
  1494. * If the user instantiates this as a per-cpu counter,
  1495. * use the cpu_clock counter instead.
  1496. */
  1497. if (counter->ctx->task)
  1498. hw_ops = &perf_ops_task_clock;
  1499. else
  1500. hw_ops = &perf_ops_cpu_clock;
  1501. if (hw_event->irq_period && hw_event->irq_period < 10000)
  1502. hw_event->irq_period = 10000;
  1503. break;
  1504. case PERF_COUNT_PAGE_FAULTS:
  1505. case PERF_COUNT_PAGE_FAULTS_MIN:
  1506. case PERF_COUNT_PAGE_FAULTS_MAJ:
  1507. case PERF_COUNT_CONTEXT_SWITCHES:
  1508. hw_ops = &perf_ops_generic;
  1509. break;
  1510. case PERF_COUNT_CPU_MIGRATIONS:
  1511. if (!counter->hw_event.exclude_kernel)
  1512. hw_ops = &perf_ops_cpu_migrations;
  1513. break;
  1514. }
  1515. if (hw_ops)
  1516. hwc->irq_period = hw_event->irq_period;
  1517. return hw_ops;
  1518. }
  1519. /*
  1520. * Allocate and initialize a counter structure
  1521. */
  1522. static struct perf_counter *
  1523. perf_counter_alloc(struct perf_counter_hw_event *hw_event,
  1524. int cpu,
  1525. struct perf_counter_context *ctx,
  1526. struct perf_counter *group_leader,
  1527. gfp_t gfpflags)
  1528. {
  1529. const struct hw_perf_counter_ops *hw_ops;
  1530. struct perf_counter *counter;
  1531. counter = kzalloc(sizeof(*counter), gfpflags);
  1532. if (!counter)
  1533. return NULL;
  1534. /*
  1535. * Single counters are their own group leaders, with an
  1536. * empty sibling list:
  1537. */
  1538. if (!group_leader)
  1539. group_leader = counter;
  1540. mutex_init(&counter->mutex);
  1541. INIT_LIST_HEAD(&counter->list_entry);
  1542. INIT_LIST_HEAD(&counter->event_entry);
  1543. INIT_LIST_HEAD(&counter->sibling_list);
  1544. init_waitqueue_head(&counter->waitq);
  1545. INIT_LIST_HEAD(&counter->child_list);
  1546. counter->irqdata = &counter->data[0];
  1547. counter->usrdata = &counter->data[1];
  1548. counter->cpu = cpu;
  1549. counter->hw_event = *hw_event;
  1550. counter->wakeup_pending = 0;
  1551. counter->group_leader = group_leader;
  1552. counter->hw_ops = NULL;
  1553. counter->ctx = ctx;
  1554. counter->state = PERF_COUNTER_STATE_INACTIVE;
  1555. if (hw_event->disabled)
  1556. counter->state = PERF_COUNTER_STATE_OFF;
  1557. hw_ops = NULL;
  1558. if (hw_event->raw_type)
  1559. hw_ops = hw_perf_counter_init(counter);
  1560. else switch (hw_event->type) {
  1561. case PERF_TYPE_HARDWARE:
  1562. hw_ops = hw_perf_counter_init(counter);
  1563. break;
  1564. case PERF_TYPE_SOFTWARE:
  1565. hw_ops = sw_perf_counter_init(counter);
  1566. break;
  1567. case PERF_TYPE_TRACEPOINT:
  1568. hw_ops = tp_perf_counter_init(counter);
  1569. break;
  1570. }
  1571. if (!hw_ops) {
  1572. kfree(counter);
  1573. return NULL;
  1574. }
  1575. counter->hw_ops = hw_ops;
  1576. return counter;
  1577. }
  1578. /**
  1579. * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
  1580. *
  1581. * @hw_event_uptr: event type attributes for monitoring/sampling
  1582. * @pid: target pid
  1583. * @cpu: target cpu
  1584. * @group_fd: group leader counter fd
  1585. */
  1586. SYSCALL_DEFINE5(perf_counter_open,
  1587. const struct perf_counter_hw_event __user *, hw_event_uptr,
  1588. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  1589. {
  1590. struct perf_counter *counter, *group_leader;
  1591. struct perf_counter_hw_event hw_event;
  1592. struct perf_counter_context *ctx;
  1593. struct file *counter_file = NULL;
  1594. struct file *group_file = NULL;
  1595. int fput_needed = 0;
  1596. int fput_needed2 = 0;
  1597. int ret;
  1598. /* for future expandability... */
  1599. if (flags)
  1600. return -EINVAL;
  1601. if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
  1602. return -EFAULT;
  1603. /*
  1604. * Get the target context (task or percpu):
  1605. */
  1606. ctx = find_get_context(pid, cpu);
  1607. if (IS_ERR(ctx))
  1608. return PTR_ERR(ctx);
  1609. /*
  1610. * Look up the group leader (we will attach this counter to it):
  1611. */
  1612. group_leader = NULL;
  1613. if (group_fd != -1) {
  1614. ret = -EINVAL;
  1615. group_file = fget_light(group_fd, &fput_needed);
  1616. if (!group_file)
  1617. goto err_put_context;
  1618. if (group_file->f_op != &perf_fops)
  1619. goto err_put_context;
  1620. group_leader = group_file->private_data;
  1621. /*
  1622. * Do not allow a recursive hierarchy (this new sibling
  1623. * becoming part of another group-sibling):
  1624. */
  1625. if (group_leader->group_leader != group_leader)
  1626. goto err_put_context;
  1627. /*
  1628. * Do not allow to attach to a group in a different
  1629. * task or CPU context:
  1630. */
  1631. if (group_leader->ctx != ctx)
  1632. goto err_put_context;
  1633. /*
  1634. * Only a group leader can be exclusive or pinned
  1635. */
  1636. if (hw_event.exclusive || hw_event.pinned)
  1637. goto err_put_context;
  1638. }
  1639. ret = -EINVAL;
  1640. counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
  1641. GFP_KERNEL);
  1642. if (!counter)
  1643. goto err_put_context;
  1644. ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
  1645. if (ret < 0)
  1646. goto err_free_put_context;
  1647. counter_file = fget_light(ret, &fput_needed2);
  1648. if (!counter_file)
  1649. goto err_free_put_context;
  1650. counter->filp = counter_file;
  1651. mutex_lock(&ctx->mutex);
  1652. perf_install_in_context(ctx, counter, cpu);
  1653. mutex_unlock(&ctx->mutex);
  1654. fput_light(counter_file, fput_needed2);
  1655. out_fput:
  1656. fput_light(group_file, fput_needed);
  1657. return ret;
  1658. err_free_put_context:
  1659. kfree(counter);
  1660. err_put_context:
  1661. put_context(ctx);
  1662. goto out_fput;
  1663. }
  1664. /*
  1665. * Initialize the perf_counter context in a task_struct:
  1666. */
  1667. static void
  1668. __perf_counter_init_context(struct perf_counter_context *ctx,
  1669. struct task_struct *task)
  1670. {
  1671. memset(ctx, 0, sizeof(*ctx));
  1672. spin_lock_init(&ctx->lock);
  1673. mutex_init(&ctx->mutex);
  1674. INIT_LIST_HEAD(&ctx->counter_list);
  1675. INIT_LIST_HEAD(&ctx->event_list);
  1676. ctx->task = task;
  1677. }
  1678. /*
  1679. * inherit a counter from parent task to child task:
  1680. */
  1681. static struct perf_counter *
  1682. inherit_counter(struct perf_counter *parent_counter,
  1683. struct task_struct *parent,
  1684. struct perf_counter_context *parent_ctx,
  1685. struct task_struct *child,
  1686. struct perf_counter *group_leader,
  1687. struct perf_counter_context *child_ctx)
  1688. {
  1689. struct perf_counter *child_counter;
  1690. /*
  1691. * Instead of creating recursive hierarchies of counters,
  1692. * we link inherited counters back to the original parent,
  1693. * which has a filp for sure, which we use as the reference
  1694. * count:
  1695. */
  1696. if (parent_counter->parent)
  1697. parent_counter = parent_counter->parent;
  1698. child_counter = perf_counter_alloc(&parent_counter->hw_event,
  1699. parent_counter->cpu, child_ctx,
  1700. group_leader, GFP_KERNEL);
  1701. if (!child_counter)
  1702. return NULL;
  1703. /*
  1704. * Link it up in the child's context:
  1705. */
  1706. child_counter->task = child;
  1707. list_add_counter(child_counter, child_ctx);
  1708. child_ctx->nr_counters++;
  1709. child_counter->parent = parent_counter;
  1710. /*
  1711. * inherit into child's child as well:
  1712. */
  1713. child_counter->hw_event.inherit = 1;
  1714. /*
  1715. * Get a reference to the parent filp - we will fput it
  1716. * when the child counter exits. This is safe to do because
  1717. * we are in the parent and we know that the filp still
  1718. * exists and has a nonzero count:
  1719. */
  1720. atomic_long_inc(&parent_counter->filp->f_count);
  1721. /*
  1722. * Link this into the parent counter's child list
  1723. */
  1724. mutex_lock(&parent_counter->mutex);
  1725. list_add_tail(&child_counter->child_list, &parent_counter->child_list);
  1726. /*
  1727. * Make the child state follow the state of the parent counter,
  1728. * not its hw_event.disabled bit. We hold the parent's mutex,
  1729. * so we won't race with perf_counter_{en,dis}able_family.
  1730. */
  1731. if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
  1732. child_counter->state = PERF_COUNTER_STATE_INACTIVE;
  1733. else
  1734. child_counter->state = PERF_COUNTER_STATE_OFF;
  1735. mutex_unlock(&parent_counter->mutex);
  1736. return child_counter;
  1737. }
  1738. static int inherit_group(struct perf_counter *parent_counter,
  1739. struct task_struct *parent,
  1740. struct perf_counter_context *parent_ctx,
  1741. struct task_struct *child,
  1742. struct perf_counter_context *child_ctx)
  1743. {
  1744. struct perf_counter *leader;
  1745. struct perf_counter *sub;
  1746. leader = inherit_counter(parent_counter, parent, parent_ctx,
  1747. child, NULL, child_ctx);
  1748. if (!leader)
  1749. return -ENOMEM;
  1750. list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
  1751. if (!inherit_counter(sub, parent, parent_ctx,
  1752. child, leader, child_ctx))
  1753. return -ENOMEM;
  1754. }
  1755. return 0;
  1756. }
  1757. static void sync_child_counter(struct perf_counter *child_counter,
  1758. struct perf_counter *parent_counter)
  1759. {
  1760. u64 parent_val, child_val;
  1761. parent_val = atomic64_read(&parent_counter->count);
  1762. child_val = atomic64_read(&child_counter->count);
  1763. /*
  1764. * Add back the child's count to the parent's count:
  1765. */
  1766. atomic64_add(child_val, &parent_counter->count);
  1767. /*
  1768. * Remove this counter from the parent's list
  1769. */
  1770. mutex_lock(&parent_counter->mutex);
  1771. list_del_init(&child_counter->child_list);
  1772. mutex_unlock(&parent_counter->mutex);
  1773. /*
  1774. * Release the parent counter, if this was the last
  1775. * reference to it.
  1776. */
  1777. fput(parent_counter->filp);
  1778. }
  1779. static void
  1780. __perf_counter_exit_task(struct task_struct *child,
  1781. struct perf_counter *child_counter,
  1782. struct perf_counter_context *child_ctx)
  1783. {
  1784. struct perf_counter *parent_counter;
  1785. struct perf_counter *sub, *tmp;
  1786. /*
  1787. * If we do not self-reap then we have to wait for the
  1788. * child task to unschedule (it will happen for sure),
  1789. * so that its counter is at its final count. (This
  1790. * condition triggers rarely - child tasks usually get
  1791. * off their CPU before the parent has a chance to
  1792. * get this far into the reaping action)
  1793. */
  1794. if (child != current) {
  1795. wait_task_inactive(child, 0);
  1796. list_del_init(&child_counter->list_entry);
  1797. } else {
  1798. struct perf_cpu_context *cpuctx;
  1799. unsigned long flags;
  1800. u64 perf_flags;
  1801. /*
  1802. * Disable and unlink this counter.
  1803. *
  1804. * Be careful about zapping the list - IRQ/NMI context
  1805. * could still be processing it:
  1806. */
  1807. curr_rq_lock_irq_save(&flags);
  1808. perf_flags = hw_perf_save_disable();
  1809. cpuctx = &__get_cpu_var(perf_cpu_context);
  1810. group_sched_out(child_counter, cpuctx, child_ctx);
  1811. list_del_init(&child_counter->list_entry);
  1812. child_ctx->nr_counters--;
  1813. hw_perf_restore(perf_flags);
  1814. curr_rq_unlock_irq_restore(&flags);
  1815. }
  1816. parent_counter = child_counter->parent;
  1817. /*
  1818. * It can happen that parent exits first, and has counters
  1819. * that are still around due to the child reference. These
  1820. * counters need to be zapped - but otherwise linger.
  1821. */
  1822. if (parent_counter) {
  1823. sync_child_counter(child_counter, parent_counter);
  1824. list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
  1825. list_entry) {
  1826. if (sub->parent) {
  1827. sync_child_counter(sub, sub->parent);
  1828. free_counter(sub);
  1829. }
  1830. }
  1831. free_counter(child_counter);
  1832. }
  1833. }
  1834. /*
  1835. * When a child task exits, feed back counter values to parent counters.
  1836. *
  1837. * Note: we may be running in child context, but the PID is not hashed
  1838. * anymore so new counters will not be added.
  1839. */
  1840. void perf_counter_exit_task(struct task_struct *child)
  1841. {
  1842. struct perf_counter *child_counter, *tmp;
  1843. struct perf_counter_context *child_ctx;
  1844. child_ctx = &child->perf_counter_ctx;
  1845. if (likely(!child_ctx->nr_counters))
  1846. return;
  1847. list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
  1848. list_entry)
  1849. __perf_counter_exit_task(child, child_counter, child_ctx);
  1850. }
  1851. /*
  1852. * Initialize the perf_counter context in task_struct
  1853. */
  1854. void perf_counter_init_task(struct task_struct *child)
  1855. {
  1856. struct perf_counter_context *child_ctx, *parent_ctx;
  1857. struct perf_counter *counter;
  1858. struct task_struct *parent = current;
  1859. child_ctx = &child->perf_counter_ctx;
  1860. parent_ctx = &parent->perf_counter_ctx;
  1861. __perf_counter_init_context(child_ctx, child);
  1862. /*
  1863. * This is executed from the parent task context, so inherit
  1864. * counters that have been marked for cloning:
  1865. */
  1866. if (likely(!parent_ctx->nr_counters))
  1867. return;
  1868. /*
  1869. * Lock the parent list. No need to lock the child - not PID
  1870. * hashed yet and not running, so nobody can access it.
  1871. */
  1872. mutex_lock(&parent_ctx->mutex);
  1873. /*
  1874. * We dont have to disable NMIs - we are only looking at
  1875. * the list, not manipulating it:
  1876. */
  1877. list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
  1878. if (!counter->hw_event.inherit)
  1879. continue;
  1880. if (inherit_group(counter, parent,
  1881. parent_ctx, child, child_ctx))
  1882. break;
  1883. }
  1884. mutex_unlock(&parent_ctx->mutex);
  1885. }
  1886. static void __cpuinit perf_counter_init_cpu(int cpu)
  1887. {
  1888. struct perf_cpu_context *cpuctx;
  1889. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1890. __perf_counter_init_context(&cpuctx->ctx, NULL);
  1891. mutex_lock(&perf_resource_mutex);
  1892. cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
  1893. mutex_unlock(&perf_resource_mutex);
  1894. hw_perf_counter_setup(cpu);
  1895. }
  1896. #ifdef CONFIG_HOTPLUG_CPU
  1897. static void __perf_counter_exit_cpu(void *info)
  1898. {
  1899. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1900. struct perf_counter_context *ctx = &cpuctx->ctx;
  1901. struct perf_counter *counter, *tmp;
  1902. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
  1903. __perf_counter_remove_from_context(counter);
  1904. }
  1905. static void perf_counter_exit_cpu(int cpu)
  1906. {
  1907. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  1908. struct perf_counter_context *ctx = &cpuctx->ctx;
  1909. mutex_lock(&ctx->mutex);
  1910. smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
  1911. mutex_unlock(&ctx->mutex);
  1912. }
  1913. #else
  1914. static inline void perf_counter_exit_cpu(int cpu) { }
  1915. #endif
  1916. static int __cpuinit
  1917. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  1918. {
  1919. unsigned int cpu = (long)hcpu;
  1920. switch (action) {
  1921. case CPU_UP_PREPARE:
  1922. case CPU_UP_PREPARE_FROZEN:
  1923. perf_counter_init_cpu(cpu);
  1924. break;
  1925. case CPU_DOWN_PREPARE:
  1926. case CPU_DOWN_PREPARE_FROZEN:
  1927. perf_counter_exit_cpu(cpu);
  1928. break;
  1929. default:
  1930. break;
  1931. }
  1932. return NOTIFY_OK;
  1933. }
  1934. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  1935. .notifier_call = perf_cpu_notify,
  1936. };
  1937. static int __init perf_counter_init(void)
  1938. {
  1939. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  1940. (void *)(long)smp_processor_id());
  1941. register_cpu_notifier(&perf_cpu_nb);
  1942. return 0;
  1943. }
  1944. early_initcall(perf_counter_init);
  1945. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  1946. {
  1947. return sprintf(buf, "%d\n", perf_reserved_percpu);
  1948. }
  1949. static ssize_t
  1950. perf_set_reserve_percpu(struct sysdev_class *class,
  1951. const char *buf,
  1952. size_t count)
  1953. {
  1954. struct perf_cpu_context *cpuctx;
  1955. unsigned long val;
  1956. int err, cpu, mpt;
  1957. err = strict_strtoul(buf, 10, &val);
  1958. if (err)
  1959. return err;
  1960. if (val > perf_max_counters)
  1961. return -EINVAL;
  1962. mutex_lock(&perf_resource_mutex);
  1963. perf_reserved_percpu = val;
  1964. for_each_online_cpu(cpu) {
  1965. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1966. spin_lock_irq(&cpuctx->ctx.lock);
  1967. mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
  1968. perf_max_counters - perf_reserved_percpu);
  1969. cpuctx->max_pertask = mpt;
  1970. spin_unlock_irq(&cpuctx->ctx.lock);
  1971. }
  1972. mutex_unlock(&perf_resource_mutex);
  1973. return count;
  1974. }
  1975. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  1976. {
  1977. return sprintf(buf, "%d\n", perf_overcommit);
  1978. }
  1979. static ssize_t
  1980. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  1981. {
  1982. unsigned long val;
  1983. int err;
  1984. err = strict_strtoul(buf, 10, &val);
  1985. if (err)
  1986. return err;
  1987. if (val > 1)
  1988. return -EINVAL;
  1989. mutex_lock(&perf_resource_mutex);
  1990. perf_overcommit = val;
  1991. mutex_unlock(&perf_resource_mutex);
  1992. return count;
  1993. }
  1994. static SYSDEV_CLASS_ATTR(
  1995. reserve_percpu,
  1996. 0644,
  1997. perf_show_reserve_percpu,
  1998. perf_set_reserve_percpu
  1999. );
  2000. static SYSDEV_CLASS_ATTR(
  2001. overcommit,
  2002. 0644,
  2003. perf_show_overcommit,
  2004. perf_set_overcommit
  2005. );
  2006. static struct attribute *perfclass_attrs[] = {
  2007. &attr_reserve_percpu.attr,
  2008. &attr_overcommit.attr,
  2009. NULL
  2010. };
  2011. static struct attribute_group perfclass_attr_group = {
  2012. .attrs = perfclass_attrs,
  2013. .name = "perf_counters",
  2014. };
  2015. static int __init perf_counter_sysfs_init(void)
  2016. {
  2017. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  2018. &perfclass_attr_group);
  2019. }
  2020. device_initcall(perf_counter_sysfs_init);