perf_counter.c 62 KB

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