perf_counter.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266
  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 <linux/dcache.h>
  28. #include <asm/irq_regs.h>
  29. /*
  30. * Each CPU has a list of per CPU counters:
  31. */
  32. DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
  33. int perf_max_counters __read_mostly = 1;
  34. static int perf_reserved_percpu __read_mostly;
  35. static int perf_overcommit __read_mostly = 1;
  36. /*
  37. * Mutex for (sysadmin-configurable) counter reservations:
  38. */
  39. static DEFINE_MUTEX(perf_resource_mutex);
  40. /*
  41. * Architecture provided APIs - weak aliases:
  42. */
  43. extern __weak const struct hw_perf_counter_ops *
  44. hw_perf_counter_init(struct perf_counter *counter)
  45. {
  46. return NULL;
  47. }
  48. u64 __weak hw_perf_save_disable(void) { return 0; }
  49. void __weak hw_perf_restore(u64 ctrl) { barrier(); }
  50. void __weak hw_perf_counter_setup(int cpu) { barrier(); }
  51. int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
  52. struct perf_cpu_context *cpuctx,
  53. struct perf_counter_context *ctx, int cpu)
  54. {
  55. return 0;
  56. }
  57. void __weak perf_counter_print_debug(void) { }
  58. static void
  59. list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  60. {
  61. struct perf_counter *group_leader = counter->group_leader;
  62. /*
  63. * Depending on whether it is a standalone or sibling counter,
  64. * add it straight to the context's counter list, or to the group
  65. * leader's sibling list:
  66. */
  67. if (counter->group_leader == counter)
  68. list_add_tail(&counter->list_entry, &ctx->counter_list);
  69. else {
  70. list_add_tail(&counter->list_entry, &group_leader->sibling_list);
  71. group_leader->nr_siblings++;
  72. }
  73. list_add_rcu(&counter->event_entry, &ctx->event_list);
  74. }
  75. static void
  76. list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  77. {
  78. struct perf_counter *sibling, *tmp;
  79. list_del_init(&counter->list_entry);
  80. list_del_rcu(&counter->event_entry);
  81. if (counter->group_leader != counter)
  82. counter->group_leader->nr_siblings--;
  83. /*
  84. * If this was a group counter with sibling counters then
  85. * upgrade the siblings to singleton counters by adding them
  86. * to the context list directly:
  87. */
  88. list_for_each_entry_safe(sibling, tmp,
  89. &counter->sibling_list, list_entry) {
  90. list_move_tail(&sibling->list_entry, &ctx->counter_list);
  91. sibling->group_leader = sibling;
  92. }
  93. }
  94. static void
  95. counter_sched_out(struct perf_counter *counter,
  96. struct perf_cpu_context *cpuctx,
  97. struct perf_counter_context *ctx)
  98. {
  99. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  100. return;
  101. counter->state = PERF_COUNTER_STATE_INACTIVE;
  102. counter->tstamp_stopped = ctx->time;
  103. counter->hw_ops->disable(counter);
  104. counter->oncpu = -1;
  105. if (!is_software_counter(counter))
  106. cpuctx->active_oncpu--;
  107. ctx->nr_active--;
  108. if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
  109. cpuctx->exclusive = 0;
  110. }
  111. static void
  112. group_sched_out(struct perf_counter *group_counter,
  113. struct perf_cpu_context *cpuctx,
  114. struct perf_counter_context *ctx)
  115. {
  116. struct perf_counter *counter;
  117. if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
  118. return;
  119. counter_sched_out(group_counter, cpuctx, ctx);
  120. /*
  121. * Schedule out siblings (if any):
  122. */
  123. list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
  124. counter_sched_out(counter, cpuctx, ctx);
  125. if (group_counter->hw_event.exclusive)
  126. cpuctx->exclusive = 0;
  127. }
  128. /*
  129. * Cross CPU call to remove a performance counter
  130. *
  131. * We disable the counter on the hardware level first. After that we
  132. * remove it from the context list.
  133. */
  134. static void __perf_counter_remove_from_context(void *info)
  135. {
  136. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  137. struct perf_counter *counter = info;
  138. struct perf_counter_context *ctx = counter->ctx;
  139. unsigned long flags;
  140. u64 perf_flags;
  141. /*
  142. * If this is a task context, we need to check whether it is
  143. * the current task context of this cpu. If not it has been
  144. * scheduled out before the smp call arrived.
  145. */
  146. if (ctx->task && cpuctx->task_ctx != ctx)
  147. return;
  148. spin_lock_irqsave(&ctx->lock, flags);
  149. counter_sched_out(counter, cpuctx, ctx);
  150. counter->task = NULL;
  151. ctx->nr_counters--;
  152. /*
  153. * Protect the list operation against NMI by disabling the
  154. * counters on a global level. NOP for non NMI based counters.
  155. */
  156. perf_flags = hw_perf_save_disable();
  157. list_del_counter(counter, ctx);
  158. hw_perf_restore(perf_flags);
  159. if (!ctx->task) {
  160. /*
  161. * Allow more per task counters with respect to the
  162. * reservation:
  163. */
  164. cpuctx->max_pertask =
  165. min(perf_max_counters - ctx->nr_counters,
  166. perf_max_counters - perf_reserved_percpu);
  167. }
  168. spin_unlock_irqrestore(&ctx->lock, 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. static inline u64 perf_clock(void)
  216. {
  217. return cpu_clock(smp_processor_id());
  218. }
  219. /*
  220. * Update the record of the current time in a context.
  221. */
  222. static void update_context_time(struct perf_counter_context *ctx)
  223. {
  224. u64 now = perf_clock();
  225. ctx->time += now - ctx->timestamp;
  226. ctx->timestamp = now;
  227. }
  228. /*
  229. * Update the total_time_enabled and total_time_running fields for a counter.
  230. */
  231. static void update_counter_times(struct perf_counter *counter)
  232. {
  233. struct perf_counter_context *ctx = counter->ctx;
  234. u64 run_end;
  235. if (counter->state < PERF_COUNTER_STATE_INACTIVE)
  236. return;
  237. counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
  238. if (counter->state == PERF_COUNTER_STATE_INACTIVE)
  239. run_end = counter->tstamp_stopped;
  240. else
  241. run_end = ctx->time;
  242. counter->total_time_running = run_end - counter->tstamp_running;
  243. }
  244. /*
  245. * Update total_time_enabled and total_time_running for all counters in a group.
  246. */
  247. static void update_group_times(struct perf_counter *leader)
  248. {
  249. struct perf_counter *counter;
  250. update_counter_times(leader);
  251. list_for_each_entry(counter, &leader->sibling_list, list_entry)
  252. update_counter_times(counter);
  253. }
  254. /*
  255. * Cross CPU call to disable a performance counter
  256. */
  257. static void __perf_counter_disable(void *info)
  258. {
  259. struct perf_counter *counter = info;
  260. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  261. struct perf_counter_context *ctx = counter->ctx;
  262. unsigned long flags;
  263. /*
  264. * If this is a per-task counter, need to check whether this
  265. * counter's task is the current task on this cpu.
  266. */
  267. if (ctx->task && cpuctx->task_ctx != ctx)
  268. return;
  269. spin_lock_irqsave(&ctx->lock, flags);
  270. /*
  271. * If the counter is on, turn it off.
  272. * If it is in error state, leave it in error state.
  273. */
  274. if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
  275. update_context_time(ctx);
  276. update_counter_times(counter);
  277. if (counter == counter->group_leader)
  278. group_sched_out(counter, cpuctx, ctx);
  279. else
  280. counter_sched_out(counter, cpuctx, ctx);
  281. counter->state = PERF_COUNTER_STATE_OFF;
  282. }
  283. spin_unlock_irqrestore(&ctx->lock, flags);
  284. }
  285. /*
  286. * Disable a counter.
  287. */
  288. static void perf_counter_disable(struct perf_counter *counter)
  289. {
  290. struct perf_counter_context *ctx = counter->ctx;
  291. struct task_struct *task = ctx->task;
  292. if (!task) {
  293. /*
  294. * Disable the counter on the cpu that it's on
  295. */
  296. smp_call_function_single(counter->cpu, __perf_counter_disable,
  297. counter, 1);
  298. return;
  299. }
  300. retry:
  301. task_oncpu_function_call(task, __perf_counter_disable, counter);
  302. spin_lock_irq(&ctx->lock);
  303. /*
  304. * If the counter is still active, we need to retry the cross-call.
  305. */
  306. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  307. spin_unlock_irq(&ctx->lock);
  308. goto retry;
  309. }
  310. /*
  311. * Since we have the lock this context can't be scheduled
  312. * in, so we can change the state safely.
  313. */
  314. if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
  315. update_counter_times(counter);
  316. counter->state = PERF_COUNTER_STATE_OFF;
  317. }
  318. spin_unlock_irq(&ctx->lock);
  319. }
  320. /*
  321. * Disable a counter and all its children.
  322. */
  323. static void perf_counter_disable_family(struct perf_counter *counter)
  324. {
  325. struct perf_counter *child;
  326. perf_counter_disable(counter);
  327. /*
  328. * Lock the mutex to protect the list of children
  329. */
  330. mutex_lock(&counter->mutex);
  331. list_for_each_entry(child, &counter->child_list, child_list)
  332. perf_counter_disable(child);
  333. mutex_unlock(&counter->mutex);
  334. }
  335. static int
  336. counter_sched_in(struct perf_counter *counter,
  337. struct perf_cpu_context *cpuctx,
  338. struct perf_counter_context *ctx,
  339. int cpu)
  340. {
  341. if (counter->state <= PERF_COUNTER_STATE_OFF)
  342. return 0;
  343. counter->state = PERF_COUNTER_STATE_ACTIVE;
  344. counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
  345. /*
  346. * The new state must be visible before we turn it on in the hardware:
  347. */
  348. smp_wmb();
  349. if (counter->hw_ops->enable(counter)) {
  350. counter->state = PERF_COUNTER_STATE_INACTIVE;
  351. counter->oncpu = -1;
  352. return -EAGAIN;
  353. }
  354. counter->tstamp_running += ctx->time - counter->tstamp_stopped;
  355. if (!is_software_counter(counter))
  356. cpuctx->active_oncpu++;
  357. ctx->nr_active++;
  358. if (counter->hw_event.exclusive)
  359. cpuctx->exclusive = 1;
  360. return 0;
  361. }
  362. /*
  363. * Return 1 for a group consisting entirely of software counters,
  364. * 0 if the group contains any hardware counters.
  365. */
  366. static int is_software_only_group(struct perf_counter *leader)
  367. {
  368. struct perf_counter *counter;
  369. if (!is_software_counter(leader))
  370. return 0;
  371. list_for_each_entry(counter, &leader->sibling_list, list_entry)
  372. if (!is_software_counter(counter))
  373. return 0;
  374. return 1;
  375. }
  376. /*
  377. * Work out whether we can put this counter group on the CPU now.
  378. */
  379. static int group_can_go_on(struct perf_counter *counter,
  380. struct perf_cpu_context *cpuctx,
  381. int can_add_hw)
  382. {
  383. /*
  384. * Groups consisting entirely of software counters can always go on.
  385. */
  386. if (is_software_only_group(counter))
  387. return 1;
  388. /*
  389. * If an exclusive group is already on, no other hardware
  390. * counters can go on.
  391. */
  392. if (cpuctx->exclusive)
  393. return 0;
  394. /*
  395. * If this group is exclusive and there are already
  396. * counters on the CPU, it can't go on.
  397. */
  398. if (counter->hw_event.exclusive && cpuctx->active_oncpu)
  399. return 0;
  400. /*
  401. * Otherwise, try to add it if all previous groups were able
  402. * to go on.
  403. */
  404. return can_add_hw;
  405. }
  406. static void add_counter_to_ctx(struct perf_counter *counter,
  407. struct perf_counter_context *ctx)
  408. {
  409. list_add_counter(counter, ctx);
  410. ctx->nr_counters++;
  411. counter->prev_state = PERF_COUNTER_STATE_OFF;
  412. counter->tstamp_enabled = ctx->time;
  413. counter->tstamp_running = ctx->time;
  414. counter->tstamp_stopped = ctx->time;
  415. }
  416. /*
  417. * Cross CPU call to install and enable a performance counter
  418. */
  419. static void __perf_install_in_context(void *info)
  420. {
  421. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  422. struct perf_counter *counter = info;
  423. struct perf_counter_context *ctx = counter->ctx;
  424. struct perf_counter *leader = counter->group_leader;
  425. int cpu = smp_processor_id();
  426. unsigned long flags;
  427. u64 perf_flags;
  428. int err;
  429. /*
  430. * If this is a task context, we need to check whether it is
  431. * the current task context of this cpu. If not it has been
  432. * scheduled out before the smp call arrived.
  433. */
  434. if (ctx->task && cpuctx->task_ctx != ctx)
  435. return;
  436. spin_lock_irqsave(&ctx->lock, flags);
  437. update_context_time(ctx);
  438. /*
  439. * Protect the list operation against NMI by disabling the
  440. * counters on a global level. NOP for non NMI based counters.
  441. */
  442. perf_flags = hw_perf_save_disable();
  443. add_counter_to_ctx(counter, ctx);
  444. /*
  445. * Don't put the counter on if it is disabled or if
  446. * it is in a group and the group isn't on.
  447. */
  448. if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
  449. (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
  450. goto unlock;
  451. /*
  452. * An exclusive counter can't go on if there are already active
  453. * hardware counters, and no hardware counter can go on if there
  454. * is already an exclusive counter on.
  455. */
  456. if (!group_can_go_on(counter, cpuctx, 1))
  457. err = -EEXIST;
  458. else
  459. err = counter_sched_in(counter, cpuctx, ctx, cpu);
  460. if (err) {
  461. /*
  462. * This counter couldn't go on. If it is in a group
  463. * then we have to pull the whole group off.
  464. * If the counter group is pinned then put it in error state.
  465. */
  466. if (leader != counter)
  467. group_sched_out(leader, cpuctx, ctx);
  468. if (leader->hw_event.pinned) {
  469. update_group_times(leader);
  470. leader->state = PERF_COUNTER_STATE_ERROR;
  471. }
  472. }
  473. if (!err && !ctx->task && cpuctx->max_pertask)
  474. cpuctx->max_pertask--;
  475. unlock:
  476. hw_perf_restore(perf_flags);
  477. spin_unlock_irqrestore(&ctx->lock, flags);
  478. }
  479. /*
  480. * Attach a performance counter to a context
  481. *
  482. * First we add the counter to the list with the hardware enable bit
  483. * in counter->hw_config cleared.
  484. *
  485. * If the counter is attached to a task which is on a CPU we use a smp
  486. * call to enable it in the task context. The task might have been
  487. * scheduled away, but we check this in the smp call again.
  488. *
  489. * Must be called with ctx->mutex held.
  490. */
  491. static void
  492. perf_install_in_context(struct perf_counter_context *ctx,
  493. struct perf_counter *counter,
  494. int cpu)
  495. {
  496. struct task_struct *task = ctx->task;
  497. if (!task) {
  498. /*
  499. * Per cpu counters are installed via an smp call and
  500. * the install is always sucessful.
  501. */
  502. smp_call_function_single(cpu, __perf_install_in_context,
  503. counter, 1);
  504. return;
  505. }
  506. counter->task = task;
  507. retry:
  508. task_oncpu_function_call(task, __perf_install_in_context,
  509. counter);
  510. spin_lock_irq(&ctx->lock);
  511. /*
  512. * we need to retry the smp call.
  513. */
  514. if (ctx->is_active && list_empty(&counter->list_entry)) {
  515. spin_unlock_irq(&ctx->lock);
  516. goto retry;
  517. }
  518. /*
  519. * The lock prevents that this context is scheduled in so we
  520. * can add the counter safely, if it the call above did not
  521. * succeed.
  522. */
  523. if (list_empty(&counter->list_entry))
  524. add_counter_to_ctx(counter, ctx);
  525. spin_unlock_irq(&ctx->lock);
  526. }
  527. /*
  528. * Cross CPU call to enable a performance counter
  529. */
  530. static void __perf_counter_enable(void *info)
  531. {
  532. struct perf_counter *counter = info;
  533. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  534. struct perf_counter_context *ctx = counter->ctx;
  535. struct perf_counter *leader = counter->group_leader;
  536. unsigned long flags;
  537. int err;
  538. /*
  539. * If this is a per-task counter, need to check whether this
  540. * counter's task is the current task on this cpu.
  541. */
  542. if (ctx->task && cpuctx->task_ctx != ctx)
  543. return;
  544. spin_lock_irqsave(&ctx->lock, flags);
  545. update_context_time(ctx);
  546. counter->prev_state = counter->state;
  547. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  548. goto unlock;
  549. counter->state = PERF_COUNTER_STATE_INACTIVE;
  550. counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
  551. /*
  552. * If the counter is in a group and isn't the group leader,
  553. * then don't put it on unless the group is on.
  554. */
  555. if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
  556. goto unlock;
  557. if (!group_can_go_on(counter, cpuctx, 1))
  558. err = -EEXIST;
  559. else
  560. err = counter_sched_in(counter, cpuctx, ctx,
  561. smp_processor_id());
  562. if (err) {
  563. /*
  564. * If this counter can't go on and it's part of a
  565. * group, then the whole group has to come off.
  566. */
  567. if (leader != counter)
  568. group_sched_out(leader, cpuctx, ctx);
  569. if (leader->hw_event.pinned) {
  570. update_group_times(leader);
  571. leader->state = PERF_COUNTER_STATE_ERROR;
  572. }
  573. }
  574. unlock:
  575. spin_unlock_irqrestore(&ctx->lock, flags);
  576. }
  577. /*
  578. * Enable a counter.
  579. */
  580. static void perf_counter_enable(struct perf_counter *counter)
  581. {
  582. struct perf_counter_context *ctx = counter->ctx;
  583. struct task_struct *task = ctx->task;
  584. if (!task) {
  585. /*
  586. * Enable the counter on the cpu that it's on
  587. */
  588. smp_call_function_single(counter->cpu, __perf_counter_enable,
  589. counter, 1);
  590. return;
  591. }
  592. spin_lock_irq(&ctx->lock);
  593. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  594. goto out;
  595. /*
  596. * If the counter is in error state, clear that first.
  597. * That way, if we see the counter in error state below, we
  598. * know that it has gone back into error state, as distinct
  599. * from the task having been scheduled away before the
  600. * cross-call arrived.
  601. */
  602. if (counter->state == PERF_COUNTER_STATE_ERROR)
  603. counter->state = PERF_COUNTER_STATE_OFF;
  604. retry:
  605. spin_unlock_irq(&ctx->lock);
  606. task_oncpu_function_call(task, __perf_counter_enable, counter);
  607. spin_lock_irq(&ctx->lock);
  608. /*
  609. * If the context is active and the counter is still off,
  610. * we need to retry the cross-call.
  611. */
  612. if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
  613. goto retry;
  614. /*
  615. * Since we have the lock this context can't be scheduled
  616. * in, so we can change the state safely.
  617. */
  618. if (counter->state == PERF_COUNTER_STATE_OFF) {
  619. counter->state = PERF_COUNTER_STATE_INACTIVE;
  620. counter->tstamp_enabled =
  621. ctx->time - counter->total_time_enabled;
  622. }
  623. out:
  624. spin_unlock_irq(&ctx->lock);
  625. }
  626. static void perf_counter_refresh(struct perf_counter *counter, int refresh)
  627. {
  628. atomic_add(refresh, &counter->event_limit);
  629. perf_counter_enable(counter);
  630. }
  631. /*
  632. * Enable a counter and all its children.
  633. */
  634. static void perf_counter_enable_family(struct perf_counter *counter)
  635. {
  636. struct perf_counter *child;
  637. perf_counter_enable(counter);
  638. /*
  639. * Lock the mutex to protect the list of children
  640. */
  641. mutex_lock(&counter->mutex);
  642. list_for_each_entry(child, &counter->child_list, child_list)
  643. perf_counter_enable(child);
  644. mutex_unlock(&counter->mutex);
  645. }
  646. void __perf_counter_sched_out(struct perf_counter_context *ctx,
  647. struct perf_cpu_context *cpuctx)
  648. {
  649. struct perf_counter *counter;
  650. u64 flags;
  651. spin_lock(&ctx->lock);
  652. ctx->is_active = 0;
  653. if (likely(!ctx->nr_counters))
  654. goto out;
  655. update_context_time(ctx);
  656. flags = hw_perf_save_disable();
  657. if (ctx->nr_active) {
  658. list_for_each_entry(counter, &ctx->counter_list, list_entry)
  659. group_sched_out(counter, cpuctx, ctx);
  660. }
  661. hw_perf_restore(flags);
  662. out:
  663. spin_unlock(&ctx->lock);
  664. }
  665. /*
  666. * Called from scheduler to remove the counters of the current task,
  667. * with interrupts disabled.
  668. *
  669. * We stop each counter and update the counter value in counter->count.
  670. *
  671. * This does not protect us against NMI, but disable()
  672. * sets the disabled bit in the control field of counter _before_
  673. * accessing the counter control register. If a NMI hits, then it will
  674. * not restart the counter.
  675. */
  676. void perf_counter_task_sched_out(struct task_struct *task, int cpu)
  677. {
  678. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  679. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  680. struct pt_regs *regs;
  681. if (likely(!cpuctx->task_ctx))
  682. return;
  683. update_context_time(ctx);
  684. regs = task_pt_regs(task);
  685. perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
  686. __perf_counter_sched_out(ctx, cpuctx);
  687. cpuctx->task_ctx = NULL;
  688. }
  689. static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
  690. {
  691. __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
  692. }
  693. static int
  694. group_sched_in(struct perf_counter *group_counter,
  695. struct perf_cpu_context *cpuctx,
  696. struct perf_counter_context *ctx,
  697. int cpu)
  698. {
  699. struct perf_counter *counter, *partial_group;
  700. int ret;
  701. if (group_counter->state == PERF_COUNTER_STATE_OFF)
  702. return 0;
  703. ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
  704. if (ret)
  705. return ret < 0 ? ret : 0;
  706. group_counter->prev_state = group_counter->state;
  707. if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
  708. return -EAGAIN;
  709. /*
  710. * Schedule in siblings as one group (if any):
  711. */
  712. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  713. counter->prev_state = counter->state;
  714. if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
  715. partial_group = counter;
  716. goto group_error;
  717. }
  718. }
  719. return 0;
  720. group_error:
  721. /*
  722. * Groups can be scheduled in as one unit only, so undo any
  723. * partial group before returning:
  724. */
  725. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  726. if (counter == partial_group)
  727. break;
  728. counter_sched_out(counter, cpuctx, ctx);
  729. }
  730. counter_sched_out(group_counter, cpuctx, ctx);
  731. return -EAGAIN;
  732. }
  733. static void
  734. __perf_counter_sched_in(struct perf_counter_context *ctx,
  735. struct perf_cpu_context *cpuctx, int cpu)
  736. {
  737. struct perf_counter *counter;
  738. u64 flags;
  739. int can_add_hw = 1;
  740. spin_lock(&ctx->lock);
  741. ctx->is_active = 1;
  742. if (likely(!ctx->nr_counters))
  743. goto out;
  744. ctx->timestamp = perf_clock();
  745. flags = hw_perf_save_disable();
  746. /*
  747. * First go through the list and put on any pinned groups
  748. * in order to give them the best chance of going on.
  749. */
  750. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  751. if (counter->state <= PERF_COUNTER_STATE_OFF ||
  752. !counter->hw_event.pinned)
  753. continue;
  754. if (counter->cpu != -1 && counter->cpu != cpu)
  755. continue;
  756. if (group_can_go_on(counter, cpuctx, 1))
  757. group_sched_in(counter, cpuctx, ctx, cpu);
  758. /*
  759. * If this pinned group hasn't been scheduled,
  760. * put it in error state.
  761. */
  762. if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
  763. update_group_times(counter);
  764. counter->state = PERF_COUNTER_STATE_ERROR;
  765. }
  766. }
  767. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  768. /*
  769. * Ignore counters in OFF or ERROR state, and
  770. * ignore pinned counters since we did them already.
  771. */
  772. if (counter->state <= PERF_COUNTER_STATE_OFF ||
  773. counter->hw_event.pinned)
  774. continue;
  775. /*
  776. * Listen to the 'cpu' scheduling filter constraint
  777. * of counters:
  778. */
  779. if (counter->cpu != -1 && counter->cpu != cpu)
  780. continue;
  781. if (group_can_go_on(counter, cpuctx, can_add_hw)) {
  782. if (group_sched_in(counter, cpuctx, ctx, cpu))
  783. can_add_hw = 0;
  784. }
  785. }
  786. hw_perf_restore(flags);
  787. out:
  788. spin_unlock(&ctx->lock);
  789. }
  790. /*
  791. * Called from scheduler to add the counters of the current task
  792. * with interrupts disabled.
  793. *
  794. * We restore the counter value and then enable it.
  795. *
  796. * This does not protect us against NMI, but enable()
  797. * sets the enabled bit in the control field of counter _before_
  798. * accessing the counter control register. If a NMI hits, then it will
  799. * keep the counter running.
  800. */
  801. void perf_counter_task_sched_in(struct task_struct *task, int cpu)
  802. {
  803. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  804. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  805. __perf_counter_sched_in(ctx, cpuctx, cpu);
  806. cpuctx->task_ctx = ctx;
  807. }
  808. static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
  809. {
  810. struct perf_counter_context *ctx = &cpuctx->ctx;
  811. __perf_counter_sched_in(ctx, cpuctx, cpu);
  812. }
  813. int perf_counter_task_disable(void)
  814. {
  815. struct task_struct *curr = current;
  816. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  817. struct perf_counter *counter;
  818. unsigned long flags;
  819. u64 perf_flags;
  820. int cpu;
  821. if (likely(!ctx->nr_counters))
  822. return 0;
  823. local_irq_save(flags);
  824. cpu = smp_processor_id();
  825. perf_counter_task_sched_out(curr, cpu);
  826. spin_lock(&ctx->lock);
  827. /*
  828. * Disable all the counters:
  829. */
  830. perf_flags = hw_perf_save_disable();
  831. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  832. if (counter->state != PERF_COUNTER_STATE_ERROR) {
  833. update_group_times(counter);
  834. counter->state = PERF_COUNTER_STATE_OFF;
  835. }
  836. }
  837. hw_perf_restore(perf_flags);
  838. spin_unlock_irqrestore(&ctx->lock, flags);
  839. return 0;
  840. }
  841. int perf_counter_task_enable(void)
  842. {
  843. struct task_struct *curr = current;
  844. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  845. struct perf_counter *counter;
  846. unsigned long flags;
  847. u64 perf_flags;
  848. int cpu;
  849. if (likely(!ctx->nr_counters))
  850. return 0;
  851. local_irq_save(flags);
  852. cpu = smp_processor_id();
  853. perf_counter_task_sched_out(curr, cpu);
  854. spin_lock(&ctx->lock);
  855. /*
  856. * Disable all the counters:
  857. */
  858. perf_flags = hw_perf_save_disable();
  859. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  860. if (counter->state > PERF_COUNTER_STATE_OFF)
  861. continue;
  862. counter->state = PERF_COUNTER_STATE_INACTIVE;
  863. counter->tstamp_enabled =
  864. ctx->time - counter->total_time_enabled;
  865. counter->hw_event.disabled = 0;
  866. }
  867. hw_perf_restore(perf_flags);
  868. spin_unlock(&ctx->lock);
  869. perf_counter_task_sched_in(curr, cpu);
  870. local_irq_restore(flags);
  871. return 0;
  872. }
  873. /*
  874. * Round-robin a context's counters:
  875. */
  876. static void rotate_ctx(struct perf_counter_context *ctx)
  877. {
  878. struct perf_counter *counter;
  879. u64 perf_flags;
  880. if (!ctx->nr_counters)
  881. return;
  882. spin_lock(&ctx->lock);
  883. /*
  884. * Rotate the first entry last (works just fine for group counters too):
  885. */
  886. perf_flags = hw_perf_save_disable();
  887. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  888. list_move_tail(&counter->list_entry, &ctx->counter_list);
  889. break;
  890. }
  891. hw_perf_restore(perf_flags);
  892. spin_unlock(&ctx->lock);
  893. }
  894. void perf_counter_task_tick(struct task_struct *curr, int cpu)
  895. {
  896. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  897. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  898. const int rotate_percpu = 0;
  899. if (rotate_percpu)
  900. perf_counter_cpu_sched_out(cpuctx);
  901. perf_counter_task_sched_out(curr, cpu);
  902. if (rotate_percpu)
  903. rotate_ctx(&cpuctx->ctx);
  904. rotate_ctx(ctx);
  905. if (rotate_percpu)
  906. perf_counter_cpu_sched_in(cpuctx, cpu);
  907. perf_counter_task_sched_in(curr, cpu);
  908. }
  909. /*
  910. * Cross CPU call to read the hardware counter
  911. */
  912. static void __read(void *info)
  913. {
  914. struct perf_counter *counter = info;
  915. struct perf_counter_context *ctx = counter->ctx;
  916. unsigned long flags;
  917. local_irq_save(flags);
  918. if (ctx->is_active)
  919. update_context_time(ctx);
  920. counter->hw_ops->read(counter);
  921. update_counter_times(counter);
  922. local_irq_restore(flags);
  923. }
  924. static u64 perf_counter_read(struct perf_counter *counter)
  925. {
  926. /*
  927. * If counter is enabled and currently active on a CPU, update the
  928. * value in the counter structure:
  929. */
  930. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  931. smp_call_function_single(counter->oncpu,
  932. __read, counter, 1);
  933. } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
  934. update_counter_times(counter);
  935. }
  936. return atomic64_read(&counter->count);
  937. }
  938. static void put_context(struct perf_counter_context *ctx)
  939. {
  940. if (ctx->task)
  941. put_task_struct(ctx->task);
  942. }
  943. static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  944. {
  945. struct perf_cpu_context *cpuctx;
  946. struct perf_counter_context *ctx;
  947. struct task_struct *task;
  948. /*
  949. * If cpu is not a wildcard then this is a percpu counter:
  950. */
  951. if (cpu != -1) {
  952. /* Must be root to operate on a CPU counter: */
  953. if (!capable(CAP_SYS_ADMIN))
  954. return ERR_PTR(-EACCES);
  955. if (cpu < 0 || cpu > num_possible_cpus())
  956. return ERR_PTR(-EINVAL);
  957. /*
  958. * We could be clever and allow to attach a counter to an
  959. * offline CPU and activate it when the CPU comes up, but
  960. * that's for later.
  961. */
  962. if (!cpu_isset(cpu, cpu_online_map))
  963. return ERR_PTR(-ENODEV);
  964. cpuctx = &per_cpu(perf_cpu_context, cpu);
  965. ctx = &cpuctx->ctx;
  966. return ctx;
  967. }
  968. rcu_read_lock();
  969. if (!pid)
  970. task = current;
  971. else
  972. task = find_task_by_vpid(pid);
  973. if (task)
  974. get_task_struct(task);
  975. rcu_read_unlock();
  976. if (!task)
  977. return ERR_PTR(-ESRCH);
  978. ctx = &task->perf_counter_ctx;
  979. ctx->task = task;
  980. /* Reuse ptrace permission checks for now. */
  981. if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
  982. put_context(ctx);
  983. return ERR_PTR(-EACCES);
  984. }
  985. return ctx;
  986. }
  987. static void free_counter_rcu(struct rcu_head *head)
  988. {
  989. struct perf_counter *counter;
  990. counter = container_of(head, struct perf_counter, rcu_head);
  991. kfree(counter);
  992. }
  993. static void perf_pending_sync(struct perf_counter *counter);
  994. static void free_counter(struct perf_counter *counter)
  995. {
  996. perf_pending_sync(counter);
  997. if (counter->destroy)
  998. counter->destroy(counter);
  999. call_rcu(&counter->rcu_head, free_counter_rcu);
  1000. }
  1001. /*
  1002. * Called when the last reference to the file is gone.
  1003. */
  1004. static int perf_release(struct inode *inode, struct file *file)
  1005. {
  1006. struct perf_counter *counter = file->private_data;
  1007. struct perf_counter_context *ctx = counter->ctx;
  1008. file->private_data = NULL;
  1009. mutex_lock(&ctx->mutex);
  1010. mutex_lock(&counter->mutex);
  1011. perf_counter_remove_from_context(counter);
  1012. mutex_unlock(&counter->mutex);
  1013. mutex_unlock(&ctx->mutex);
  1014. free_counter(counter);
  1015. put_context(ctx);
  1016. return 0;
  1017. }
  1018. /*
  1019. * Read the performance counter - simple non blocking version for now
  1020. */
  1021. static ssize_t
  1022. perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
  1023. {
  1024. u64 values[3];
  1025. int n;
  1026. /*
  1027. * Return end-of-file for a read on a counter that is in
  1028. * error state (i.e. because it was pinned but it couldn't be
  1029. * scheduled on to the CPU at some point).
  1030. */
  1031. if (counter->state == PERF_COUNTER_STATE_ERROR)
  1032. return 0;
  1033. mutex_lock(&counter->mutex);
  1034. values[0] = perf_counter_read(counter);
  1035. n = 1;
  1036. if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1037. values[n++] = counter->total_time_enabled +
  1038. atomic64_read(&counter->child_total_time_enabled);
  1039. if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1040. values[n++] = counter->total_time_running +
  1041. atomic64_read(&counter->child_total_time_running);
  1042. mutex_unlock(&counter->mutex);
  1043. if (count < n * sizeof(u64))
  1044. return -EINVAL;
  1045. count = n * sizeof(u64);
  1046. if (copy_to_user(buf, values, count))
  1047. return -EFAULT;
  1048. return count;
  1049. }
  1050. static ssize_t
  1051. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  1052. {
  1053. struct perf_counter *counter = file->private_data;
  1054. return perf_read_hw(counter, buf, count);
  1055. }
  1056. static unsigned int perf_poll(struct file *file, poll_table *wait)
  1057. {
  1058. struct perf_counter *counter = file->private_data;
  1059. struct perf_mmap_data *data;
  1060. unsigned int events;
  1061. rcu_read_lock();
  1062. data = rcu_dereference(counter->data);
  1063. if (data)
  1064. events = atomic_xchg(&data->wakeup, 0);
  1065. else
  1066. events = POLL_HUP;
  1067. rcu_read_unlock();
  1068. poll_wait(file, &counter->waitq, wait);
  1069. return events;
  1070. }
  1071. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1072. {
  1073. struct perf_counter *counter = file->private_data;
  1074. int err = 0;
  1075. switch (cmd) {
  1076. case PERF_COUNTER_IOC_ENABLE:
  1077. perf_counter_enable_family(counter);
  1078. break;
  1079. case PERF_COUNTER_IOC_DISABLE:
  1080. perf_counter_disable_family(counter);
  1081. break;
  1082. case PERF_COUNTER_IOC_REFRESH:
  1083. perf_counter_refresh(counter, arg);
  1084. break;
  1085. default:
  1086. err = -ENOTTY;
  1087. }
  1088. return err;
  1089. }
  1090. /*
  1091. * Callers need to ensure there can be no nesting of this function, otherwise
  1092. * the seqlock logic goes bad. We can not serialize this because the arch
  1093. * code calls this from NMI context.
  1094. */
  1095. void perf_counter_update_userpage(struct perf_counter *counter)
  1096. {
  1097. struct perf_mmap_data *data;
  1098. struct perf_counter_mmap_page *userpg;
  1099. rcu_read_lock();
  1100. data = rcu_dereference(counter->data);
  1101. if (!data)
  1102. goto unlock;
  1103. userpg = data->user_page;
  1104. /*
  1105. * Disable preemption so as to not let the corresponding user-space
  1106. * spin too long if we get preempted.
  1107. */
  1108. preempt_disable();
  1109. ++userpg->lock;
  1110. barrier();
  1111. userpg->index = counter->hw.idx;
  1112. userpg->offset = atomic64_read(&counter->count);
  1113. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  1114. userpg->offset -= atomic64_read(&counter->hw.prev_count);
  1115. barrier();
  1116. ++userpg->lock;
  1117. preempt_enable();
  1118. unlock:
  1119. rcu_read_unlock();
  1120. }
  1121. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1122. {
  1123. struct perf_counter *counter = vma->vm_file->private_data;
  1124. struct perf_mmap_data *data;
  1125. int ret = VM_FAULT_SIGBUS;
  1126. rcu_read_lock();
  1127. data = rcu_dereference(counter->data);
  1128. if (!data)
  1129. goto unlock;
  1130. if (vmf->pgoff == 0) {
  1131. vmf->page = virt_to_page(data->user_page);
  1132. } else {
  1133. int nr = vmf->pgoff - 1;
  1134. if ((unsigned)nr > data->nr_pages)
  1135. goto unlock;
  1136. vmf->page = virt_to_page(data->data_pages[nr]);
  1137. }
  1138. get_page(vmf->page);
  1139. ret = 0;
  1140. unlock:
  1141. rcu_read_unlock();
  1142. return ret;
  1143. }
  1144. static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
  1145. {
  1146. struct perf_mmap_data *data;
  1147. unsigned long size;
  1148. int i;
  1149. WARN_ON(atomic_read(&counter->mmap_count));
  1150. size = sizeof(struct perf_mmap_data);
  1151. size += nr_pages * sizeof(void *);
  1152. data = kzalloc(size, GFP_KERNEL);
  1153. if (!data)
  1154. goto fail;
  1155. data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
  1156. if (!data->user_page)
  1157. goto fail_user_page;
  1158. for (i = 0; i < nr_pages; i++) {
  1159. data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
  1160. if (!data->data_pages[i])
  1161. goto fail_data_pages;
  1162. }
  1163. data->nr_pages = nr_pages;
  1164. rcu_assign_pointer(counter->data, data);
  1165. return 0;
  1166. fail_data_pages:
  1167. for (i--; i >= 0; i--)
  1168. free_page((unsigned long)data->data_pages[i]);
  1169. free_page((unsigned long)data->user_page);
  1170. fail_user_page:
  1171. kfree(data);
  1172. fail:
  1173. return -ENOMEM;
  1174. }
  1175. static void __perf_mmap_data_free(struct rcu_head *rcu_head)
  1176. {
  1177. struct perf_mmap_data *data = container_of(rcu_head,
  1178. struct perf_mmap_data, rcu_head);
  1179. int i;
  1180. free_page((unsigned long)data->user_page);
  1181. for (i = 0; i < data->nr_pages; i++)
  1182. free_page((unsigned long)data->data_pages[i]);
  1183. kfree(data);
  1184. }
  1185. static void perf_mmap_data_free(struct perf_counter *counter)
  1186. {
  1187. struct perf_mmap_data *data = counter->data;
  1188. WARN_ON(atomic_read(&counter->mmap_count));
  1189. rcu_assign_pointer(counter->data, NULL);
  1190. call_rcu(&data->rcu_head, __perf_mmap_data_free);
  1191. }
  1192. static void perf_mmap_open(struct vm_area_struct *vma)
  1193. {
  1194. struct perf_counter *counter = vma->vm_file->private_data;
  1195. atomic_inc(&counter->mmap_count);
  1196. }
  1197. static void perf_mmap_close(struct vm_area_struct *vma)
  1198. {
  1199. struct perf_counter *counter = vma->vm_file->private_data;
  1200. if (atomic_dec_and_mutex_lock(&counter->mmap_count,
  1201. &counter->mmap_mutex)) {
  1202. vma->vm_mm->locked_vm -= counter->data->nr_pages + 1;
  1203. perf_mmap_data_free(counter);
  1204. mutex_unlock(&counter->mmap_mutex);
  1205. }
  1206. }
  1207. static struct vm_operations_struct perf_mmap_vmops = {
  1208. .open = perf_mmap_open,
  1209. .close = perf_mmap_close,
  1210. .fault = perf_mmap_fault,
  1211. };
  1212. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  1213. {
  1214. struct perf_counter *counter = file->private_data;
  1215. unsigned long vma_size;
  1216. unsigned long nr_pages;
  1217. unsigned long locked, lock_limit;
  1218. int ret = 0;
  1219. if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
  1220. return -EINVAL;
  1221. vma_size = vma->vm_end - vma->vm_start;
  1222. nr_pages = (vma_size / PAGE_SIZE) - 1;
  1223. /*
  1224. * If we have data pages ensure they're a power-of-two number, so we
  1225. * can do bitmasks instead of modulo.
  1226. */
  1227. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  1228. return -EINVAL;
  1229. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  1230. return -EINVAL;
  1231. if (vma->vm_pgoff != 0)
  1232. return -EINVAL;
  1233. mutex_lock(&counter->mmap_mutex);
  1234. if (atomic_inc_not_zero(&counter->mmap_count)) {
  1235. if (nr_pages != counter->data->nr_pages)
  1236. ret = -EINVAL;
  1237. goto unlock;
  1238. }
  1239. locked = vma->vm_mm->locked_vm;
  1240. locked += nr_pages + 1;
  1241. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  1242. lock_limit >>= PAGE_SHIFT;
  1243. if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
  1244. ret = -EPERM;
  1245. goto unlock;
  1246. }
  1247. WARN_ON(counter->data);
  1248. ret = perf_mmap_data_alloc(counter, nr_pages);
  1249. if (ret)
  1250. goto unlock;
  1251. atomic_set(&counter->mmap_count, 1);
  1252. vma->vm_mm->locked_vm += nr_pages + 1;
  1253. unlock:
  1254. mutex_unlock(&counter->mmap_mutex);
  1255. vma->vm_flags &= ~VM_MAYWRITE;
  1256. vma->vm_flags |= VM_RESERVED;
  1257. vma->vm_ops = &perf_mmap_vmops;
  1258. return ret;
  1259. }
  1260. static int perf_fasync(int fd, struct file *filp, int on)
  1261. {
  1262. struct perf_counter *counter = filp->private_data;
  1263. struct inode *inode = filp->f_path.dentry->d_inode;
  1264. int retval;
  1265. mutex_lock(&inode->i_mutex);
  1266. retval = fasync_helper(fd, filp, on, &counter->fasync);
  1267. mutex_unlock(&inode->i_mutex);
  1268. if (retval < 0)
  1269. return retval;
  1270. return 0;
  1271. }
  1272. static const struct file_operations perf_fops = {
  1273. .release = perf_release,
  1274. .read = perf_read,
  1275. .poll = perf_poll,
  1276. .unlocked_ioctl = perf_ioctl,
  1277. .compat_ioctl = perf_ioctl,
  1278. .mmap = perf_mmap,
  1279. .fasync = perf_fasync,
  1280. };
  1281. /*
  1282. * Perf counter wakeup
  1283. *
  1284. * If there's data, ensure we set the poll() state and publish everything
  1285. * to user-space before waking everybody up.
  1286. */
  1287. void perf_counter_wakeup(struct perf_counter *counter)
  1288. {
  1289. struct perf_mmap_data *data;
  1290. rcu_read_lock();
  1291. data = rcu_dereference(counter->data);
  1292. if (data) {
  1293. atomic_set(&data->wakeup, POLL_IN);
  1294. /*
  1295. * Ensure all data writes are issued before updating the
  1296. * user-space data head information. The matching rmb()
  1297. * will be in userspace after reading this value.
  1298. */
  1299. smp_wmb();
  1300. data->user_page->data_head = atomic_read(&data->head);
  1301. }
  1302. rcu_read_unlock();
  1303. wake_up_all(&counter->waitq);
  1304. if (counter->pending_kill) {
  1305. kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
  1306. counter->pending_kill = 0;
  1307. }
  1308. }
  1309. /*
  1310. * Pending wakeups
  1311. *
  1312. * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
  1313. *
  1314. * The NMI bit means we cannot possibly take locks. Therefore, maintain a
  1315. * single linked list and use cmpxchg() to add entries lockless.
  1316. */
  1317. static void perf_pending_counter(struct perf_pending_entry *entry)
  1318. {
  1319. struct perf_counter *counter = container_of(entry,
  1320. struct perf_counter, pending);
  1321. if (counter->pending_disable) {
  1322. counter->pending_disable = 0;
  1323. perf_counter_disable(counter);
  1324. }
  1325. if (counter->pending_wakeup) {
  1326. counter->pending_wakeup = 0;
  1327. perf_counter_wakeup(counter);
  1328. }
  1329. }
  1330. #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
  1331. static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
  1332. PENDING_TAIL,
  1333. };
  1334. static void perf_pending_queue(struct perf_pending_entry *entry,
  1335. void (*func)(struct perf_pending_entry *))
  1336. {
  1337. struct perf_pending_entry **head;
  1338. if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
  1339. return;
  1340. entry->func = func;
  1341. head = &get_cpu_var(perf_pending_head);
  1342. do {
  1343. entry->next = *head;
  1344. } while (cmpxchg(head, entry->next, entry) != entry->next);
  1345. set_perf_counter_pending();
  1346. put_cpu_var(perf_pending_head);
  1347. }
  1348. static int __perf_pending_run(void)
  1349. {
  1350. struct perf_pending_entry *list;
  1351. int nr = 0;
  1352. list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
  1353. while (list != PENDING_TAIL) {
  1354. void (*func)(struct perf_pending_entry *);
  1355. struct perf_pending_entry *entry = list;
  1356. list = list->next;
  1357. func = entry->func;
  1358. entry->next = NULL;
  1359. /*
  1360. * Ensure we observe the unqueue before we issue the wakeup,
  1361. * so that we won't be waiting forever.
  1362. * -- see perf_not_pending().
  1363. */
  1364. smp_wmb();
  1365. func(entry);
  1366. nr++;
  1367. }
  1368. return nr;
  1369. }
  1370. static inline int perf_not_pending(struct perf_counter *counter)
  1371. {
  1372. /*
  1373. * If we flush on whatever cpu we run, there is a chance we don't
  1374. * need to wait.
  1375. */
  1376. get_cpu();
  1377. __perf_pending_run();
  1378. put_cpu();
  1379. /*
  1380. * Ensure we see the proper queue state before going to sleep
  1381. * so that we do not miss the wakeup. -- see perf_pending_handle()
  1382. */
  1383. smp_rmb();
  1384. return counter->pending.next == NULL;
  1385. }
  1386. static void perf_pending_sync(struct perf_counter *counter)
  1387. {
  1388. wait_event(counter->waitq, perf_not_pending(counter));
  1389. }
  1390. void perf_counter_do_pending(void)
  1391. {
  1392. __perf_pending_run();
  1393. }
  1394. /*
  1395. * Callchain support -- arch specific
  1396. */
  1397. __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1398. {
  1399. return NULL;
  1400. }
  1401. /*
  1402. * Output
  1403. */
  1404. struct perf_output_handle {
  1405. struct perf_counter *counter;
  1406. struct perf_mmap_data *data;
  1407. unsigned int offset;
  1408. unsigned int head;
  1409. int wakeup;
  1410. int nmi;
  1411. int overflow;
  1412. };
  1413. static inline void __perf_output_wakeup(struct perf_output_handle *handle)
  1414. {
  1415. if (handle->nmi) {
  1416. handle->counter->pending_wakeup = 1;
  1417. perf_pending_queue(&handle->counter->pending,
  1418. perf_pending_counter);
  1419. } else
  1420. perf_counter_wakeup(handle->counter);
  1421. }
  1422. static int perf_output_begin(struct perf_output_handle *handle,
  1423. struct perf_counter *counter, unsigned int size,
  1424. int nmi, int overflow)
  1425. {
  1426. struct perf_mmap_data *data;
  1427. unsigned int offset, head;
  1428. rcu_read_lock();
  1429. data = rcu_dereference(counter->data);
  1430. if (!data)
  1431. goto out;
  1432. handle->counter = counter;
  1433. handle->nmi = nmi;
  1434. handle->overflow = overflow;
  1435. if (!data->nr_pages)
  1436. goto fail;
  1437. do {
  1438. offset = head = atomic_read(&data->head);
  1439. head += size;
  1440. } while (atomic_cmpxchg(&data->head, offset, head) != offset);
  1441. handle->data = data;
  1442. handle->offset = offset;
  1443. handle->head = head;
  1444. handle->wakeup = (offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT);
  1445. return 0;
  1446. fail:
  1447. __perf_output_wakeup(handle);
  1448. out:
  1449. rcu_read_unlock();
  1450. return -ENOSPC;
  1451. }
  1452. static void perf_output_copy(struct perf_output_handle *handle,
  1453. void *buf, unsigned int len)
  1454. {
  1455. unsigned int pages_mask;
  1456. unsigned int offset;
  1457. unsigned int size;
  1458. void **pages;
  1459. offset = handle->offset;
  1460. pages_mask = handle->data->nr_pages - 1;
  1461. pages = handle->data->data_pages;
  1462. do {
  1463. unsigned int page_offset;
  1464. int nr;
  1465. nr = (offset >> PAGE_SHIFT) & pages_mask;
  1466. page_offset = offset & (PAGE_SIZE - 1);
  1467. size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
  1468. memcpy(pages[nr] + page_offset, buf, size);
  1469. len -= size;
  1470. buf += size;
  1471. offset += size;
  1472. } while (len);
  1473. handle->offset = offset;
  1474. WARN_ON_ONCE(handle->offset > handle->head);
  1475. }
  1476. #define perf_output_put(handle, x) \
  1477. perf_output_copy((handle), &(x), sizeof(x))
  1478. static void perf_output_end(struct perf_output_handle *handle)
  1479. {
  1480. int wakeup_events = handle->counter->hw_event.wakeup_events;
  1481. if (handle->overflow && wakeup_events) {
  1482. int events = atomic_inc_return(&handle->data->events);
  1483. if (events >= wakeup_events) {
  1484. atomic_sub(wakeup_events, &handle->data->events);
  1485. __perf_output_wakeup(handle);
  1486. }
  1487. } else if (handle->wakeup)
  1488. __perf_output_wakeup(handle);
  1489. rcu_read_unlock();
  1490. }
  1491. static void perf_counter_output(struct perf_counter *counter,
  1492. int nmi, struct pt_regs *regs, u64 addr)
  1493. {
  1494. int ret;
  1495. u64 record_type = counter->hw_event.record_type;
  1496. struct perf_output_handle handle;
  1497. struct perf_event_header header;
  1498. u64 ip;
  1499. struct {
  1500. u32 pid, tid;
  1501. } tid_entry;
  1502. struct {
  1503. u64 event;
  1504. u64 counter;
  1505. } group_entry;
  1506. struct perf_callchain_entry *callchain = NULL;
  1507. int callchain_size = 0;
  1508. u64 time;
  1509. header.type = 0;
  1510. header.size = sizeof(header);
  1511. header.misc = PERF_EVENT_MISC_OVERFLOW;
  1512. header.misc |= user_mode(regs) ?
  1513. PERF_EVENT_MISC_USER : PERF_EVENT_MISC_KERNEL;
  1514. if (record_type & PERF_RECORD_IP) {
  1515. ip = instruction_pointer(regs);
  1516. header.type |= PERF_RECORD_IP;
  1517. header.size += sizeof(ip);
  1518. }
  1519. if (record_type & PERF_RECORD_TID) {
  1520. /* namespace issues */
  1521. tid_entry.pid = current->group_leader->pid;
  1522. tid_entry.tid = current->pid;
  1523. header.type |= PERF_RECORD_TID;
  1524. header.size += sizeof(tid_entry);
  1525. }
  1526. if (record_type & PERF_RECORD_TIME) {
  1527. /*
  1528. * Maybe do better on x86 and provide cpu_clock_nmi()
  1529. */
  1530. time = sched_clock();
  1531. header.type |= PERF_RECORD_TIME;
  1532. header.size += sizeof(u64);
  1533. }
  1534. if (record_type & PERF_RECORD_ADDR) {
  1535. header.type |= PERF_RECORD_ADDR;
  1536. header.size += sizeof(u64);
  1537. }
  1538. if (record_type & PERF_RECORD_GROUP) {
  1539. header.type |= PERF_RECORD_GROUP;
  1540. header.size += sizeof(u64) +
  1541. counter->nr_siblings * sizeof(group_entry);
  1542. }
  1543. if (record_type & PERF_RECORD_CALLCHAIN) {
  1544. callchain = perf_callchain(regs);
  1545. if (callchain) {
  1546. callchain_size = (1 + callchain->nr) * sizeof(u64);
  1547. header.type |= PERF_RECORD_CALLCHAIN;
  1548. header.size += callchain_size;
  1549. }
  1550. }
  1551. ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
  1552. if (ret)
  1553. return;
  1554. perf_output_put(&handle, header);
  1555. if (record_type & PERF_RECORD_IP)
  1556. perf_output_put(&handle, ip);
  1557. if (record_type & PERF_RECORD_TID)
  1558. perf_output_put(&handle, tid_entry);
  1559. if (record_type & PERF_RECORD_TIME)
  1560. perf_output_put(&handle, time);
  1561. if (record_type & PERF_RECORD_ADDR)
  1562. perf_output_put(&handle, addr);
  1563. if (record_type & PERF_RECORD_GROUP) {
  1564. struct perf_counter *leader, *sub;
  1565. u64 nr = counter->nr_siblings;
  1566. perf_output_put(&handle, nr);
  1567. leader = counter->group_leader;
  1568. list_for_each_entry(sub, &leader->sibling_list, list_entry) {
  1569. if (sub != counter)
  1570. sub->hw_ops->read(sub);
  1571. group_entry.event = sub->hw_event.config;
  1572. group_entry.counter = atomic64_read(&sub->count);
  1573. perf_output_put(&handle, group_entry);
  1574. }
  1575. }
  1576. if (callchain)
  1577. perf_output_copy(&handle, callchain, callchain_size);
  1578. perf_output_end(&handle);
  1579. }
  1580. /*
  1581. * comm tracking
  1582. */
  1583. struct perf_comm_event {
  1584. struct task_struct *task;
  1585. char *comm;
  1586. int comm_size;
  1587. struct {
  1588. struct perf_event_header header;
  1589. u32 pid;
  1590. u32 tid;
  1591. } event;
  1592. };
  1593. static void perf_counter_comm_output(struct perf_counter *counter,
  1594. struct perf_comm_event *comm_event)
  1595. {
  1596. struct perf_output_handle handle;
  1597. int size = comm_event->event.header.size;
  1598. int ret = perf_output_begin(&handle, counter, size, 0, 0);
  1599. if (ret)
  1600. return;
  1601. perf_output_put(&handle, comm_event->event);
  1602. perf_output_copy(&handle, comm_event->comm,
  1603. comm_event->comm_size);
  1604. perf_output_end(&handle);
  1605. }
  1606. static int perf_counter_comm_match(struct perf_counter *counter,
  1607. struct perf_comm_event *comm_event)
  1608. {
  1609. if (counter->hw_event.comm &&
  1610. comm_event->event.header.type == PERF_EVENT_COMM)
  1611. return 1;
  1612. return 0;
  1613. }
  1614. static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
  1615. struct perf_comm_event *comm_event)
  1616. {
  1617. struct perf_counter *counter;
  1618. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  1619. return;
  1620. rcu_read_lock();
  1621. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  1622. if (perf_counter_comm_match(counter, comm_event))
  1623. perf_counter_comm_output(counter, comm_event);
  1624. }
  1625. rcu_read_unlock();
  1626. }
  1627. static void perf_counter_comm_event(struct perf_comm_event *comm_event)
  1628. {
  1629. struct perf_cpu_context *cpuctx;
  1630. unsigned int size;
  1631. char *comm = comm_event->task->comm;
  1632. size = ALIGN(strlen(comm)+1, sizeof(u64));
  1633. comm_event->comm = comm;
  1634. comm_event->comm_size = size;
  1635. comm_event->event.header.size = sizeof(comm_event->event) + size;
  1636. cpuctx = &get_cpu_var(perf_cpu_context);
  1637. perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
  1638. put_cpu_var(perf_cpu_context);
  1639. perf_counter_comm_ctx(&current->perf_counter_ctx, comm_event);
  1640. }
  1641. void perf_counter_comm(struct task_struct *task)
  1642. {
  1643. struct perf_comm_event comm_event = {
  1644. .task = task,
  1645. .event = {
  1646. .header = { .type = PERF_EVENT_COMM, },
  1647. .pid = task->group_leader->pid,
  1648. .tid = task->pid,
  1649. },
  1650. };
  1651. perf_counter_comm_event(&comm_event);
  1652. }
  1653. /*
  1654. * mmap tracking
  1655. */
  1656. struct perf_mmap_event {
  1657. struct file *file;
  1658. char *file_name;
  1659. int file_size;
  1660. struct {
  1661. struct perf_event_header header;
  1662. u32 pid;
  1663. u32 tid;
  1664. u64 start;
  1665. u64 len;
  1666. u64 pgoff;
  1667. } event;
  1668. };
  1669. static void perf_counter_mmap_output(struct perf_counter *counter,
  1670. struct perf_mmap_event *mmap_event)
  1671. {
  1672. struct perf_output_handle handle;
  1673. int size = mmap_event->event.header.size;
  1674. int ret = perf_output_begin(&handle, counter, size, 0, 0);
  1675. if (ret)
  1676. return;
  1677. perf_output_put(&handle, mmap_event->event);
  1678. perf_output_copy(&handle, mmap_event->file_name,
  1679. mmap_event->file_size);
  1680. perf_output_end(&handle);
  1681. }
  1682. static int perf_counter_mmap_match(struct perf_counter *counter,
  1683. struct perf_mmap_event *mmap_event)
  1684. {
  1685. if (counter->hw_event.mmap &&
  1686. mmap_event->event.header.type == PERF_EVENT_MMAP)
  1687. return 1;
  1688. if (counter->hw_event.munmap &&
  1689. mmap_event->event.header.type == PERF_EVENT_MUNMAP)
  1690. return 1;
  1691. return 0;
  1692. }
  1693. static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
  1694. struct perf_mmap_event *mmap_event)
  1695. {
  1696. struct perf_counter *counter;
  1697. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  1698. return;
  1699. rcu_read_lock();
  1700. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  1701. if (perf_counter_mmap_match(counter, mmap_event))
  1702. perf_counter_mmap_output(counter, mmap_event);
  1703. }
  1704. rcu_read_unlock();
  1705. }
  1706. static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
  1707. {
  1708. struct perf_cpu_context *cpuctx;
  1709. struct file *file = mmap_event->file;
  1710. unsigned int size;
  1711. char tmp[16];
  1712. char *buf = NULL;
  1713. char *name;
  1714. if (file) {
  1715. buf = kzalloc(PATH_MAX, GFP_KERNEL);
  1716. if (!buf) {
  1717. name = strncpy(tmp, "//enomem", sizeof(tmp));
  1718. goto got_name;
  1719. }
  1720. name = dentry_path(file->f_dentry, buf, PATH_MAX);
  1721. if (IS_ERR(name)) {
  1722. name = strncpy(tmp, "//toolong", sizeof(tmp));
  1723. goto got_name;
  1724. }
  1725. } else {
  1726. name = strncpy(tmp, "//anon", sizeof(tmp));
  1727. goto got_name;
  1728. }
  1729. got_name:
  1730. size = ALIGN(strlen(name)+1, sizeof(u64));
  1731. mmap_event->file_name = name;
  1732. mmap_event->file_size = size;
  1733. mmap_event->event.header.size = sizeof(mmap_event->event) + size;
  1734. cpuctx = &get_cpu_var(perf_cpu_context);
  1735. perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
  1736. put_cpu_var(perf_cpu_context);
  1737. perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
  1738. kfree(buf);
  1739. }
  1740. void perf_counter_mmap(unsigned long addr, unsigned long len,
  1741. unsigned long pgoff, struct file *file)
  1742. {
  1743. struct perf_mmap_event mmap_event = {
  1744. .file = file,
  1745. .event = {
  1746. .header = { .type = PERF_EVENT_MMAP, },
  1747. .pid = current->group_leader->pid,
  1748. .tid = current->pid,
  1749. .start = addr,
  1750. .len = len,
  1751. .pgoff = pgoff,
  1752. },
  1753. };
  1754. perf_counter_mmap_event(&mmap_event);
  1755. }
  1756. void perf_counter_munmap(unsigned long addr, unsigned long len,
  1757. unsigned long pgoff, struct file *file)
  1758. {
  1759. struct perf_mmap_event mmap_event = {
  1760. .file = file,
  1761. .event = {
  1762. .header = { .type = PERF_EVENT_MUNMAP, },
  1763. .pid = current->group_leader->pid,
  1764. .tid = current->pid,
  1765. .start = addr,
  1766. .len = len,
  1767. .pgoff = pgoff,
  1768. },
  1769. };
  1770. perf_counter_mmap_event(&mmap_event);
  1771. }
  1772. /*
  1773. * Generic counter overflow handling.
  1774. */
  1775. int perf_counter_overflow(struct perf_counter *counter,
  1776. int nmi, struct pt_regs *regs, u64 addr)
  1777. {
  1778. int events = atomic_read(&counter->event_limit);
  1779. int ret = 0;
  1780. counter->pending_kill = POLL_IN;
  1781. if (events && atomic_dec_and_test(&counter->event_limit)) {
  1782. ret = 1;
  1783. counter->pending_kill = POLL_HUP;
  1784. if (nmi) {
  1785. counter->pending_disable = 1;
  1786. perf_pending_queue(&counter->pending,
  1787. perf_pending_counter);
  1788. } else
  1789. perf_counter_disable(counter);
  1790. }
  1791. perf_counter_output(counter, nmi, regs, addr);
  1792. return ret;
  1793. }
  1794. /*
  1795. * Generic software counter infrastructure
  1796. */
  1797. static void perf_swcounter_update(struct perf_counter *counter)
  1798. {
  1799. struct hw_perf_counter *hwc = &counter->hw;
  1800. u64 prev, now;
  1801. s64 delta;
  1802. again:
  1803. prev = atomic64_read(&hwc->prev_count);
  1804. now = atomic64_read(&hwc->count);
  1805. if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
  1806. goto again;
  1807. delta = now - prev;
  1808. atomic64_add(delta, &counter->count);
  1809. atomic64_sub(delta, &hwc->period_left);
  1810. }
  1811. static void perf_swcounter_set_period(struct perf_counter *counter)
  1812. {
  1813. struct hw_perf_counter *hwc = &counter->hw;
  1814. s64 left = atomic64_read(&hwc->period_left);
  1815. s64 period = hwc->irq_period;
  1816. if (unlikely(left <= -period)) {
  1817. left = period;
  1818. atomic64_set(&hwc->period_left, left);
  1819. }
  1820. if (unlikely(left <= 0)) {
  1821. left += period;
  1822. atomic64_add(period, &hwc->period_left);
  1823. }
  1824. atomic64_set(&hwc->prev_count, -left);
  1825. atomic64_set(&hwc->count, -left);
  1826. }
  1827. static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
  1828. {
  1829. enum hrtimer_restart ret = HRTIMER_RESTART;
  1830. struct perf_counter *counter;
  1831. struct pt_regs *regs;
  1832. counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
  1833. counter->hw_ops->read(counter);
  1834. regs = get_irq_regs();
  1835. /*
  1836. * In case we exclude kernel IPs or are somehow not in interrupt
  1837. * context, provide the next best thing, the user IP.
  1838. */
  1839. if ((counter->hw_event.exclude_kernel || !regs) &&
  1840. !counter->hw_event.exclude_user)
  1841. regs = task_pt_regs(current);
  1842. if (regs) {
  1843. if (perf_counter_overflow(counter, 0, regs, 0))
  1844. ret = HRTIMER_NORESTART;
  1845. }
  1846. hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
  1847. return ret;
  1848. }
  1849. static void perf_swcounter_overflow(struct perf_counter *counter,
  1850. int nmi, struct pt_regs *regs, u64 addr)
  1851. {
  1852. perf_swcounter_update(counter);
  1853. perf_swcounter_set_period(counter);
  1854. if (perf_counter_overflow(counter, nmi, regs, addr))
  1855. /* soft-disable the counter */
  1856. ;
  1857. }
  1858. static int perf_swcounter_match(struct perf_counter *counter,
  1859. enum perf_event_types type,
  1860. u32 event, struct pt_regs *regs)
  1861. {
  1862. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  1863. return 0;
  1864. if (perf_event_raw(&counter->hw_event))
  1865. return 0;
  1866. if (perf_event_type(&counter->hw_event) != type)
  1867. return 0;
  1868. if (perf_event_id(&counter->hw_event) != event)
  1869. return 0;
  1870. if (counter->hw_event.exclude_user && user_mode(regs))
  1871. return 0;
  1872. if (counter->hw_event.exclude_kernel && !user_mode(regs))
  1873. return 0;
  1874. return 1;
  1875. }
  1876. static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
  1877. int nmi, struct pt_regs *regs, u64 addr)
  1878. {
  1879. int neg = atomic64_add_negative(nr, &counter->hw.count);
  1880. if (counter->hw.irq_period && !neg)
  1881. perf_swcounter_overflow(counter, nmi, regs, addr);
  1882. }
  1883. static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
  1884. enum perf_event_types type, u32 event,
  1885. u64 nr, int nmi, struct pt_regs *regs,
  1886. u64 addr)
  1887. {
  1888. struct perf_counter *counter;
  1889. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  1890. return;
  1891. rcu_read_lock();
  1892. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  1893. if (perf_swcounter_match(counter, type, event, regs))
  1894. perf_swcounter_add(counter, nr, nmi, regs, addr);
  1895. }
  1896. rcu_read_unlock();
  1897. }
  1898. static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
  1899. {
  1900. if (in_nmi())
  1901. return &cpuctx->recursion[3];
  1902. if (in_irq())
  1903. return &cpuctx->recursion[2];
  1904. if (in_softirq())
  1905. return &cpuctx->recursion[1];
  1906. return &cpuctx->recursion[0];
  1907. }
  1908. static void __perf_swcounter_event(enum perf_event_types type, u32 event,
  1909. u64 nr, int nmi, struct pt_regs *regs,
  1910. u64 addr)
  1911. {
  1912. struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
  1913. int *recursion = perf_swcounter_recursion_context(cpuctx);
  1914. if (*recursion)
  1915. goto out;
  1916. (*recursion)++;
  1917. barrier();
  1918. perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
  1919. nr, nmi, regs, addr);
  1920. if (cpuctx->task_ctx) {
  1921. perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
  1922. nr, nmi, regs, addr);
  1923. }
  1924. barrier();
  1925. (*recursion)--;
  1926. out:
  1927. put_cpu_var(perf_cpu_context);
  1928. }
  1929. void
  1930. perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
  1931. {
  1932. __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
  1933. }
  1934. static void perf_swcounter_read(struct perf_counter *counter)
  1935. {
  1936. perf_swcounter_update(counter);
  1937. }
  1938. static int perf_swcounter_enable(struct perf_counter *counter)
  1939. {
  1940. perf_swcounter_set_period(counter);
  1941. return 0;
  1942. }
  1943. static void perf_swcounter_disable(struct perf_counter *counter)
  1944. {
  1945. perf_swcounter_update(counter);
  1946. }
  1947. static const struct hw_perf_counter_ops perf_ops_generic = {
  1948. .enable = perf_swcounter_enable,
  1949. .disable = perf_swcounter_disable,
  1950. .read = perf_swcounter_read,
  1951. };
  1952. /*
  1953. * Software counter: cpu wall time clock
  1954. */
  1955. static void cpu_clock_perf_counter_update(struct perf_counter *counter)
  1956. {
  1957. int cpu = raw_smp_processor_id();
  1958. s64 prev;
  1959. u64 now;
  1960. now = cpu_clock(cpu);
  1961. prev = atomic64_read(&counter->hw.prev_count);
  1962. atomic64_set(&counter->hw.prev_count, now);
  1963. atomic64_add(now - prev, &counter->count);
  1964. }
  1965. static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
  1966. {
  1967. struct hw_perf_counter *hwc = &counter->hw;
  1968. int cpu = raw_smp_processor_id();
  1969. atomic64_set(&hwc->prev_count, cpu_clock(cpu));
  1970. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1971. hwc->hrtimer.function = perf_swcounter_hrtimer;
  1972. if (hwc->irq_period) {
  1973. __hrtimer_start_range_ns(&hwc->hrtimer,
  1974. ns_to_ktime(hwc->irq_period), 0,
  1975. HRTIMER_MODE_REL, 0);
  1976. }
  1977. return 0;
  1978. }
  1979. static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
  1980. {
  1981. hrtimer_cancel(&counter->hw.hrtimer);
  1982. cpu_clock_perf_counter_update(counter);
  1983. }
  1984. static void cpu_clock_perf_counter_read(struct perf_counter *counter)
  1985. {
  1986. cpu_clock_perf_counter_update(counter);
  1987. }
  1988. static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
  1989. .enable = cpu_clock_perf_counter_enable,
  1990. .disable = cpu_clock_perf_counter_disable,
  1991. .read = cpu_clock_perf_counter_read,
  1992. };
  1993. /*
  1994. * Software counter: task time clock
  1995. */
  1996. static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
  1997. {
  1998. u64 prev;
  1999. s64 delta;
  2000. prev = atomic64_xchg(&counter->hw.prev_count, now);
  2001. delta = now - prev;
  2002. atomic64_add(delta, &counter->count);
  2003. }
  2004. static int task_clock_perf_counter_enable(struct perf_counter *counter)
  2005. {
  2006. struct hw_perf_counter *hwc = &counter->hw;
  2007. u64 now;
  2008. now = counter->ctx->time;
  2009. atomic64_set(&hwc->prev_count, now);
  2010. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2011. hwc->hrtimer.function = perf_swcounter_hrtimer;
  2012. if (hwc->irq_period) {
  2013. __hrtimer_start_range_ns(&hwc->hrtimer,
  2014. ns_to_ktime(hwc->irq_period), 0,
  2015. HRTIMER_MODE_REL, 0);
  2016. }
  2017. return 0;
  2018. }
  2019. static void task_clock_perf_counter_disable(struct perf_counter *counter)
  2020. {
  2021. hrtimer_cancel(&counter->hw.hrtimer);
  2022. task_clock_perf_counter_update(counter, counter->ctx->time);
  2023. }
  2024. static void task_clock_perf_counter_read(struct perf_counter *counter)
  2025. {
  2026. u64 time;
  2027. if (!in_nmi()) {
  2028. update_context_time(counter->ctx);
  2029. time = counter->ctx->time;
  2030. } else {
  2031. u64 now = perf_clock();
  2032. u64 delta = now - counter->ctx->timestamp;
  2033. time = counter->ctx->time + delta;
  2034. }
  2035. task_clock_perf_counter_update(counter, time);
  2036. }
  2037. static const struct hw_perf_counter_ops perf_ops_task_clock = {
  2038. .enable = task_clock_perf_counter_enable,
  2039. .disable = task_clock_perf_counter_disable,
  2040. .read = task_clock_perf_counter_read,
  2041. };
  2042. /*
  2043. * Software counter: cpu migrations
  2044. */
  2045. static inline u64 get_cpu_migrations(struct perf_counter *counter)
  2046. {
  2047. struct task_struct *curr = counter->ctx->task;
  2048. if (curr)
  2049. return curr->se.nr_migrations;
  2050. return cpu_nr_migrations(smp_processor_id());
  2051. }
  2052. static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
  2053. {
  2054. u64 prev, now;
  2055. s64 delta;
  2056. prev = atomic64_read(&counter->hw.prev_count);
  2057. now = get_cpu_migrations(counter);
  2058. atomic64_set(&counter->hw.prev_count, now);
  2059. delta = now - prev;
  2060. atomic64_add(delta, &counter->count);
  2061. }
  2062. static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
  2063. {
  2064. cpu_migrations_perf_counter_update(counter);
  2065. }
  2066. static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
  2067. {
  2068. if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
  2069. atomic64_set(&counter->hw.prev_count,
  2070. get_cpu_migrations(counter));
  2071. return 0;
  2072. }
  2073. static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
  2074. {
  2075. cpu_migrations_perf_counter_update(counter);
  2076. }
  2077. static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
  2078. .enable = cpu_migrations_perf_counter_enable,
  2079. .disable = cpu_migrations_perf_counter_disable,
  2080. .read = cpu_migrations_perf_counter_read,
  2081. };
  2082. #ifdef CONFIG_EVENT_PROFILE
  2083. void perf_tpcounter_event(int event_id)
  2084. {
  2085. struct pt_regs *regs = get_irq_regs();
  2086. if (!regs)
  2087. regs = task_pt_regs(current);
  2088. __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
  2089. }
  2090. extern int ftrace_profile_enable(int);
  2091. extern void ftrace_profile_disable(int);
  2092. static void tp_perf_counter_destroy(struct perf_counter *counter)
  2093. {
  2094. ftrace_profile_disable(perf_event_id(&counter->hw_event));
  2095. }
  2096. static const struct hw_perf_counter_ops *
  2097. tp_perf_counter_init(struct perf_counter *counter)
  2098. {
  2099. int event_id = perf_event_id(&counter->hw_event);
  2100. int ret;
  2101. ret = ftrace_profile_enable(event_id);
  2102. if (ret)
  2103. return NULL;
  2104. counter->destroy = tp_perf_counter_destroy;
  2105. counter->hw.irq_period = counter->hw_event.irq_period;
  2106. return &perf_ops_generic;
  2107. }
  2108. #else
  2109. static const struct hw_perf_counter_ops *
  2110. tp_perf_counter_init(struct perf_counter *counter)
  2111. {
  2112. return NULL;
  2113. }
  2114. #endif
  2115. static const struct hw_perf_counter_ops *
  2116. sw_perf_counter_init(struct perf_counter *counter)
  2117. {
  2118. struct perf_counter_hw_event *hw_event = &counter->hw_event;
  2119. const struct hw_perf_counter_ops *hw_ops = NULL;
  2120. struct hw_perf_counter *hwc = &counter->hw;
  2121. /*
  2122. * Software counters (currently) can't in general distinguish
  2123. * between user, kernel and hypervisor events.
  2124. * However, context switches and cpu migrations are considered
  2125. * to be kernel events, and page faults are never hypervisor
  2126. * events.
  2127. */
  2128. switch (perf_event_id(&counter->hw_event)) {
  2129. case PERF_COUNT_CPU_CLOCK:
  2130. hw_ops = &perf_ops_cpu_clock;
  2131. if (hw_event->irq_period && hw_event->irq_period < 10000)
  2132. hw_event->irq_period = 10000;
  2133. break;
  2134. case PERF_COUNT_TASK_CLOCK:
  2135. /*
  2136. * If the user instantiates this as a per-cpu counter,
  2137. * use the cpu_clock counter instead.
  2138. */
  2139. if (counter->ctx->task)
  2140. hw_ops = &perf_ops_task_clock;
  2141. else
  2142. hw_ops = &perf_ops_cpu_clock;
  2143. if (hw_event->irq_period && hw_event->irq_period < 10000)
  2144. hw_event->irq_period = 10000;
  2145. break;
  2146. case PERF_COUNT_PAGE_FAULTS:
  2147. case PERF_COUNT_PAGE_FAULTS_MIN:
  2148. case PERF_COUNT_PAGE_FAULTS_MAJ:
  2149. case PERF_COUNT_CONTEXT_SWITCHES:
  2150. hw_ops = &perf_ops_generic;
  2151. break;
  2152. case PERF_COUNT_CPU_MIGRATIONS:
  2153. if (!counter->hw_event.exclude_kernel)
  2154. hw_ops = &perf_ops_cpu_migrations;
  2155. break;
  2156. }
  2157. if (hw_ops)
  2158. hwc->irq_period = hw_event->irq_period;
  2159. return hw_ops;
  2160. }
  2161. /*
  2162. * Allocate and initialize a counter structure
  2163. */
  2164. static struct perf_counter *
  2165. perf_counter_alloc(struct perf_counter_hw_event *hw_event,
  2166. int cpu,
  2167. struct perf_counter_context *ctx,
  2168. struct perf_counter *group_leader,
  2169. gfp_t gfpflags)
  2170. {
  2171. const struct hw_perf_counter_ops *hw_ops;
  2172. struct perf_counter *counter;
  2173. long err;
  2174. counter = kzalloc(sizeof(*counter), gfpflags);
  2175. if (!counter)
  2176. return ERR_PTR(-ENOMEM);
  2177. /*
  2178. * Single counters are their own group leaders, with an
  2179. * empty sibling list:
  2180. */
  2181. if (!group_leader)
  2182. group_leader = counter;
  2183. mutex_init(&counter->mutex);
  2184. INIT_LIST_HEAD(&counter->list_entry);
  2185. INIT_LIST_HEAD(&counter->event_entry);
  2186. INIT_LIST_HEAD(&counter->sibling_list);
  2187. init_waitqueue_head(&counter->waitq);
  2188. mutex_init(&counter->mmap_mutex);
  2189. INIT_LIST_HEAD(&counter->child_list);
  2190. counter->cpu = cpu;
  2191. counter->hw_event = *hw_event;
  2192. counter->group_leader = group_leader;
  2193. counter->hw_ops = NULL;
  2194. counter->ctx = ctx;
  2195. counter->state = PERF_COUNTER_STATE_INACTIVE;
  2196. if (hw_event->disabled)
  2197. counter->state = PERF_COUNTER_STATE_OFF;
  2198. hw_ops = NULL;
  2199. if (perf_event_raw(hw_event)) {
  2200. hw_ops = hw_perf_counter_init(counter);
  2201. goto done;
  2202. }
  2203. switch (perf_event_type(hw_event)) {
  2204. case PERF_TYPE_HARDWARE:
  2205. hw_ops = hw_perf_counter_init(counter);
  2206. break;
  2207. case PERF_TYPE_SOFTWARE:
  2208. hw_ops = sw_perf_counter_init(counter);
  2209. break;
  2210. case PERF_TYPE_TRACEPOINT:
  2211. hw_ops = tp_perf_counter_init(counter);
  2212. break;
  2213. }
  2214. done:
  2215. err = 0;
  2216. if (!hw_ops)
  2217. err = -EINVAL;
  2218. else if (IS_ERR(hw_ops))
  2219. err = PTR_ERR(hw_ops);
  2220. if (err) {
  2221. kfree(counter);
  2222. return ERR_PTR(err);
  2223. }
  2224. counter->hw_ops = hw_ops;
  2225. return counter;
  2226. }
  2227. /**
  2228. * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
  2229. *
  2230. * @hw_event_uptr: event type attributes for monitoring/sampling
  2231. * @pid: target pid
  2232. * @cpu: target cpu
  2233. * @group_fd: group leader counter fd
  2234. */
  2235. SYSCALL_DEFINE5(perf_counter_open,
  2236. const struct perf_counter_hw_event __user *, hw_event_uptr,
  2237. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  2238. {
  2239. struct perf_counter *counter, *group_leader;
  2240. struct perf_counter_hw_event hw_event;
  2241. struct perf_counter_context *ctx;
  2242. struct file *counter_file = NULL;
  2243. struct file *group_file = NULL;
  2244. int fput_needed = 0;
  2245. int fput_needed2 = 0;
  2246. int ret;
  2247. /* for future expandability... */
  2248. if (flags)
  2249. return -EINVAL;
  2250. if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
  2251. return -EFAULT;
  2252. /*
  2253. * Get the target context (task or percpu):
  2254. */
  2255. ctx = find_get_context(pid, cpu);
  2256. if (IS_ERR(ctx))
  2257. return PTR_ERR(ctx);
  2258. /*
  2259. * Look up the group leader (we will attach this counter to it):
  2260. */
  2261. group_leader = NULL;
  2262. if (group_fd != -1) {
  2263. ret = -EINVAL;
  2264. group_file = fget_light(group_fd, &fput_needed);
  2265. if (!group_file)
  2266. goto err_put_context;
  2267. if (group_file->f_op != &perf_fops)
  2268. goto err_put_context;
  2269. group_leader = group_file->private_data;
  2270. /*
  2271. * Do not allow a recursive hierarchy (this new sibling
  2272. * becoming part of another group-sibling):
  2273. */
  2274. if (group_leader->group_leader != group_leader)
  2275. goto err_put_context;
  2276. /*
  2277. * Do not allow to attach to a group in a different
  2278. * task or CPU context:
  2279. */
  2280. if (group_leader->ctx != ctx)
  2281. goto err_put_context;
  2282. /*
  2283. * Only a group leader can be exclusive or pinned
  2284. */
  2285. if (hw_event.exclusive || hw_event.pinned)
  2286. goto err_put_context;
  2287. }
  2288. counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
  2289. GFP_KERNEL);
  2290. ret = PTR_ERR(counter);
  2291. if (IS_ERR(counter))
  2292. goto err_put_context;
  2293. ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
  2294. if (ret < 0)
  2295. goto err_free_put_context;
  2296. counter_file = fget_light(ret, &fput_needed2);
  2297. if (!counter_file)
  2298. goto err_free_put_context;
  2299. counter->filp = counter_file;
  2300. mutex_lock(&ctx->mutex);
  2301. perf_install_in_context(ctx, counter, cpu);
  2302. mutex_unlock(&ctx->mutex);
  2303. fput_light(counter_file, fput_needed2);
  2304. out_fput:
  2305. fput_light(group_file, fput_needed);
  2306. return ret;
  2307. err_free_put_context:
  2308. kfree(counter);
  2309. err_put_context:
  2310. put_context(ctx);
  2311. goto out_fput;
  2312. }
  2313. /*
  2314. * Initialize the perf_counter context in a task_struct:
  2315. */
  2316. static void
  2317. __perf_counter_init_context(struct perf_counter_context *ctx,
  2318. struct task_struct *task)
  2319. {
  2320. memset(ctx, 0, sizeof(*ctx));
  2321. spin_lock_init(&ctx->lock);
  2322. mutex_init(&ctx->mutex);
  2323. INIT_LIST_HEAD(&ctx->counter_list);
  2324. INIT_LIST_HEAD(&ctx->event_list);
  2325. ctx->task = task;
  2326. }
  2327. /*
  2328. * inherit a counter from parent task to child task:
  2329. */
  2330. static struct perf_counter *
  2331. inherit_counter(struct perf_counter *parent_counter,
  2332. struct task_struct *parent,
  2333. struct perf_counter_context *parent_ctx,
  2334. struct task_struct *child,
  2335. struct perf_counter *group_leader,
  2336. struct perf_counter_context *child_ctx)
  2337. {
  2338. struct perf_counter *child_counter;
  2339. /*
  2340. * Instead of creating recursive hierarchies of counters,
  2341. * we link inherited counters back to the original parent,
  2342. * which has a filp for sure, which we use as the reference
  2343. * count:
  2344. */
  2345. if (parent_counter->parent)
  2346. parent_counter = parent_counter->parent;
  2347. child_counter = perf_counter_alloc(&parent_counter->hw_event,
  2348. parent_counter->cpu, child_ctx,
  2349. group_leader, GFP_KERNEL);
  2350. if (IS_ERR(child_counter))
  2351. return child_counter;
  2352. /*
  2353. * Link it up in the child's context:
  2354. */
  2355. child_counter->task = child;
  2356. add_counter_to_ctx(child_counter, child_ctx);
  2357. child_counter->parent = parent_counter;
  2358. /*
  2359. * inherit into child's child as well:
  2360. */
  2361. child_counter->hw_event.inherit = 1;
  2362. /*
  2363. * Get a reference to the parent filp - we will fput it
  2364. * when the child counter exits. This is safe to do because
  2365. * we are in the parent and we know that the filp still
  2366. * exists and has a nonzero count:
  2367. */
  2368. atomic_long_inc(&parent_counter->filp->f_count);
  2369. /*
  2370. * Link this into the parent counter's child list
  2371. */
  2372. mutex_lock(&parent_counter->mutex);
  2373. list_add_tail(&child_counter->child_list, &parent_counter->child_list);
  2374. /*
  2375. * Make the child state follow the state of the parent counter,
  2376. * not its hw_event.disabled bit. We hold the parent's mutex,
  2377. * so we won't race with perf_counter_{en,dis}able_family.
  2378. */
  2379. if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
  2380. child_counter->state = PERF_COUNTER_STATE_INACTIVE;
  2381. else
  2382. child_counter->state = PERF_COUNTER_STATE_OFF;
  2383. mutex_unlock(&parent_counter->mutex);
  2384. return child_counter;
  2385. }
  2386. static int inherit_group(struct perf_counter *parent_counter,
  2387. struct task_struct *parent,
  2388. struct perf_counter_context *parent_ctx,
  2389. struct task_struct *child,
  2390. struct perf_counter_context *child_ctx)
  2391. {
  2392. struct perf_counter *leader;
  2393. struct perf_counter *sub;
  2394. struct perf_counter *child_ctr;
  2395. leader = inherit_counter(parent_counter, parent, parent_ctx,
  2396. child, NULL, child_ctx);
  2397. if (IS_ERR(leader))
  2398. return PTR_ERR(leader);
  2399. list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
  2400. child_ctr = inherit_counter(sub, parent, parent_ctx,
  2401. child, leader, child_ctx);
  2402. if (IS_ERR(child_ctr))
  2403. return PTR_ERR(child_ctr);
  2404. }
  2405. return 0;
  2406. }
  2407. static void sync_child_counter(struct perf_counter *child_counter,
  2408. struct perf_counter *parent_counter)
  2409. {
  2410. u64 parent_val, child_val;
  2411. parent_val = atomic64_read(&parent_counter->count);
  2412. child_val = atomic64_read(&child_counter->count);
  2413. /*
  2414. * Add back the child's count to the parent's count:
  2415. */
  2416. atomic64_add(child_val, &parent_counter->count);
  2417. atomic64_add(child_counter->total_time_enabled,
  2418. &parent_counter->child_total_time_enabled);
  2419. atomic64_add(child_counter->total_time_running,
  2420. &parent_counter->child_total_time_running);
  2421. /*
  2422. * Remove this counter from the parent's list
  2423. */
  2424. mutex_lock(&parent_counter->mutex);
  2425. list_del_init(&child_counter->child_list);
  2426. mutex_unlock(&parent_counter->mutex);
  2427. /*
  2428. * Release the parent counter, if this was the last
  2429. * reference to it.
  2430. */
  2431. fput(parent_counter->filp);
  2432. }
  2433. static void
  2434. __perf_counter_exit_task(struct task_struct *child,
  2435. struct perf_counter *child_counter,
  2436. struct perf_counter_context *child_ctx)
  2437. {
  2438. struct perf_counter *parent_counter;
  2439. struct perf_counter *sub, *tmp;
  2440. /*
  2441. * If we do not self-reap then we have to wait for the
  2442. * child task to unschedule (it will happen for sure),
  2443. * so that its counter is at its final count. (This
  2444. * condition triggers rarely - child tasks usually get
  2445. * off their CPU before the parent has a chance to
  2446. * get this far into the reaping action)
  2447. */
  2448. if (child != current) {
  2449. wait_task_inactive(child, 0);
  2450. list_del_init(&child_counter->list_entry);
  2451. update_counter_times(child_counter);
  2452. } else {
  2453. struct perf_cpu_context *cpuctx;
  2454. unsigned long flags;
  2455. u64 perf_flags;
  2456. /*
  2457. * Disable and unlink this counter.
  2458. *
  2459. * Be careful about zapping the list - IRQ/NMI context
  2460. * could still be processing it:
  2461. */
  2462. local_irq_save(flags);
  2463. perf_flags = hw_perf_save_disable();
  2464. cpuctx = &__get_cpu_var(perf_cpu_context);
  2465. group_sched_out(child_counter, cpuctx, child_ctx);
  2466. update_counter_times(child_counter);
  2467. list_del_init(&child_counter->list_entry);
  2468. child_ctx->nr_counters--;
  2469. hw_perf_restore(perf_flags);
  2470. local_irq_restore(flags);
  2471. }
  2472. parent_counter = child_counter->parent;
  2473. /*
  2474. * It can happen that parent exits first, and has counters
  2475. * that are still around due to the child reference. These
  2476. * counters need to be zapped - but otherwise linger.
  2477. */
  2478. if (parent_counter) {
  2479. sync_child_counter(child_counter, parent_counter);
  2480. list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
  2481. list_entry) {
  2482. if (sub->parent) {
  2483. sync_child_counter(sub, sub->parent);
  2484. free_counter(sub);
  2485. }
  2486. }
  2487. free_counter(child_counter);
  2488. }
  2489. }
  2490. /*
  2491. * When a child task exits, feed back counter values to parent counters.
  2492. *
  2493. * Note: we may be running in child context, but the PID is not hashed
  2494. * anymore so new counters will not be added.
  2495. */
  2496. void perf_counter_exit_task(struct task_struct *child)
  2497. {
  2498. struct perf_counter *child_counter, *tmp;
  2499. struct perf_counter_context *child_ctx;
  2500. child_ctx = &child->perf_counter_ctx;
  2501. if (likely(!child_ctx->nr_counters))
  2502. return;
  2503. list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
  2504. list_entry)
  2505. __perf_counter_exit_task(child, child_counter, child_ctx);
  2506. }
  2507. /*
  2508. * Initialize the perf_counter context in task_struct
  2509. */
  2510. void perf_counter_init_task(struct task_struct *child)
  2511. {
  2512. struct perf_counter_context *child_ctx, *parent_ctx;
  2513. struct perf_counter *counter;
  2514. struct task_struct *parent = current;
  2515. child_ctx = &child->perf_counter_ctx;
  2516. parent_ctx = &parent->perf_counter_ctx;
  2517. __perf_counter_init_context(child_ctx, child);
  2518. /*
  2519. * This is executed from the parent task context, so inherit
  2520. * counters that have been marked for cloning:
  2521. */
  2522. if (likely(!parent_ctx->nr_counters))
  2523. return;
  2524. /*
  2525. * Lock the parent list. No need to lock the child - not PID
  2526. * hashed yet and not running, so nobody can access it.
  2527. */
  2528. mutex_lock(&parent_ctx->mutex);
  2529. /*
  2530. * We dont have to disable NMIs - we are only looking at
  2531. * the list, not manipulating it:
  2532. */
  2533. list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
  2534. if (!counter->hw_event.inherit)
  2535. continue;
  2536. if (inherit_group(counter, parent,
  2537. parent_ctx, child, child_ctx))
  2538. break;
  2539. }
  2540. mutex_unlock(&parent_ctx->mutex);
  2541. }
  2542. static void __cpuinit perf_counter_init_cpu(int cpu)
  2543. {
  2544. struct perf_cpu_context *cpuctx;
  2545. cpuctx = &per_cpu(perf_cpu_context, cpu);
  2546. __perf_counter_init_context(&cpuctx->ctx, NULL);
  2547. mutex_lock(&perf_resource_mutex);
  2548. cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
  2549. mutex_unlock(&perf_resource_mutex);
  2550. hw_perf_counter_setup(cpu);
  2551. }
  2552. #ifdef CONFIG_HOTPLUG_CPU
  2553. static void __perf_counter_exit_cpu(void *info)
  2554. {
  2555. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  2556. struct perf_counter_context *ctx = &cpuctx->ctx;
  2557. struct perf_counter *counter, *tmp;
  2558. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
  2559. __perf_counter_remove_from_context(counter);
  2560. }
  2561. static void perf_counter_exit_cpu(int cpu)
  2562. {
  2563. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  2564. struct perf_counter_context *ctx = &cpuctx->ctx;
  2565. mutex_lock(&ctx->mutex);
  2566. smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
  2567. mutex_unlock(&ctx->mutex);
  2568. }
  2569. #else
  2570. static inline void perf_counter_exit_cpu(int cpu) { }
  2571. #endif
  2572. static int __cpuinit
  2573. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  2574. {
  2575. unsigned int cpu = (long)hcpu;
  2576. switch (action) {
  2577. case CPU_UP_PREPARE:
  2578. case CPU_UP_PREPARE_FROZEN:
  2579. perf_counter_init_cpu(cpu);
  2580. break;
  2581. case CPU_DOWN_PREPARE:
  2582. case CPU_DOWN_PREPARE_FROZEN:
  2583. perf_counter_exit_cpu(cpu);
  2584. break;
  2585. default:
  2586. break;
  2587. }
  2588. return NOTIFY_OK;
  2589. }
  2590. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  2591. .notifier_call = perf_cpu_notify,
  2592. };
  2593. static int __init perf_counter_init(void)
  2594. {
  2595. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  2596. (void *)(long)smp_processor_id());
  2597. register_cpu_notifier(&perf_cpu_nb);
  2598. return 0;
  2599. }
  2600. early_initcall(perf_counter_init);
  2601. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  2602. {
  2603. return sprintf(buf, "%d\n", perf_reserved_percpu);
  2604. }
  2605. static ssize_t
  2606. perf_set_reserve_percpu(struct sysdev_class *class,
  2607. const char *buf,
  2608. size_t count)
  2609. {
  2610. struct perf_cpu_context *cpuctx;
  2611. unsigned long val;
  2612. int err, cpu, mpt;
  2613. err = strict_strtoul(buf, 10, &val);
  2614. if (err)
  2615. return err;
  2616. if (val > perf_max_counters)
  2617. return -EINVAL;
  2618. mutex_lock(&perf_resource_mutex);
  2619. perf_reserved_percpu = val;
  2620. for_each_online_cpu(cpu) {
  2621. cpuctx = &per_cpu(perf_cpu_context, cpu);
  2622. spin_lock_irq(&cpuctx->ctx.lock);
  2623. mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
  2624. perf_max_counters - perf_reserved_percpu);
  2625. cpuctx->max_pertask = mpt;
  2626. spin_unlock_irq(&cpuctx->ctx.lock);
  2627. }
  2628. mutex_unlock(&perf_resource_mutex);
  2629. return count;
  2630. }
  2631. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  2632. {
  2633. return sprintf(buf, "%d\n", perf_overcommit);
  2634. }
  2635. static ssize_t
  2636. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  2637. {
  2638. unsigned long val;
  2639. int err;
  2640. err = strict_strtoul(buf, 10, &val);
  2641. if (err)
  2642. return err;
  2643. if (val > 1)
  2644. return -EINVAL;
  2645. mutex_lock(&perf_resource_mutex);
  2646. perf_overcommit = val;
  2647. mutex_unlock(&perf_resource_mutex);
  2648. return count;
  2649. }
  2650. static SYSDEV_CLASS_ATTR(
  2651. reserve_percpu,
  2652. 0644,
  2653. perf_show_reserve_percpu,
  2654. perf_set_reserve_percpu
  2655. );
  2656. static SYSDEV_CLASS_ATTR(
  2657. overcommit,
  2658. 0644,
  2659. perf_show_overcommit,
  2660. perf_set_overcommit
  2661. );
  2662. static struct attribute *perfclass_attrs[] = {
  2663. &attr_reserve_percpu.attr,
  2664. &attr_overcommit.attr,
  2665. NULL
  2666. };
  2667. static struct attribute_group perfclass_attr_group = {
  2668. .attrs = perfclass_attrs,
  2669. .name = "perf_counters",
  2670. };
  2671. static int __init perf_counter_sysfs_init(void)
  2672. {
  2673. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  2674. &perfclass_attr_group);
  2675. }
  2676. device_initcall(perf_counter_sysfs_init);