perf_counter.c 57 KB

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