perf_counter.c 58 KB

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