perf_counter.c 58 KB

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