perf_counter.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046
  1. /*
  2. * Performance counter core code
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  7. * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  8. *
  9. * For licensing details see kernel-base/COPYING
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/mm.h>
  13. #include <linux/cpu.h>
  14. #include <linux/smp.h>
  15. #include <linux/file.h>
  16. #include <linux/poll.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/dcache.h>
  19. #include <linux/percpu.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/vmstat.h>
  22. #include <linux/hardirq.h>
  23. #include <linux/rculist.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/anon_inodes.h>
  27. #include <linux/kernel_stat.h>
  28. #include <linux/perf_counter.h>
  29. #include <asm/irq_regs.h>
  30. /*
  31. * Each CPU has a list of per CPU counters:
  32. */
  33. DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
  34. int perf_max_counters __read_mostly = 1;
  35. static int perf_reserved_percpu __read_mostly;
  36. static int perf_overcommit __read_mostly = 1;
  37. static atomic_t nr_counters __read_mostly;
  38. static atomic_t nr_mmap_tracking __read_mostly;
  39. static atomic_t nr_munmap_tracking __read_mostly;
  40. static atomic_t nr_comm_tracking __read_mostly;
  41. int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
  42. int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
  43. int sysctl_perf_counter_limit __read_mostly = 100000; /* max NMIs per second */
  44. /*
  45. * Lock for (sysadmin-configurable) counter reservations:
  46. */
  47. static DEFINE_SPINLOCK(perf_resource_lock);
  48. /*
  49. * Architecture provided APIs - weak aliases:
  50. */
  51. extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
  52. {
  53. return NULL;
  54. }
  55. void __weak hw_perf_disable(void) { barrier(); }
  56. void __weak hw_perf_enable(void) { barrier(); }
  57. void __weak hw_perf_counter_setup(int cpu) { barrier(); }
  58. int __weak
  59. hw_perf_group_sched_in(struct perf_counter *group_leader,
  60. struct perf_cpu_context *cpuctx,
  61. struct perf_counter_context *ctx, int cpu)
  62. {
  63. return 0;
  64. }
  65. void __weak perf_counter_print_debug(void) { }
  66. static DEFINE_PER_CPU(int, disable_count);
  67. void __perf_disable(void)
  68. {
  69. __get_cpu_var(disable_count)++;
  70. }
  71. bool __perf_enable(void)
  72. {
  73. return !--__get_cpu_var(disable_count);
  74. }
  75. void perf_disable(void)
  76. {
  77. __perf_disable();
  78. hw_perf_disable();
  79. }
  80. void perf_enable(void)
  81. {
  82. if (__perf_enable())
  83. hw_perf_enable();
  84. }
  85. static void get_ctx(struct perf_counter_context *ctx)
  86. {
  87. atomic_inc(&ctx->refcount);
  88. }
  89. static void free_ctx(struct rcu_head *head)
  90. {
  91. struct perf_counter_context *ctx;
  92. ctx = container_of(head, struct perf_counter_context, rcu_head);
  93. kfree(ctx);
  94. }
  95. static void put_ctx(struct perf_counter_context *ctx)
  96. {
  97. if (atomic_dec_and_test(&ctx->refcount)) {
  98. if (ctx->parent_ctx)
  99. put_ctx(ctx->parent_ctx);
  100. if (ctx->task)
  101. put_task_struct(ctx->task);
  102. call_rcu(&ctx->rcu_head, free_ctx);
  103. }
  104. }
  105. /*
  106. * Get the perf_counter_context for a task and lock it.
  107. * This has to cope with with the fact that until it is locked,
  108. * the context could get moved to another task.
  109. */
  110. static struct perf_counter_context *
  111. perf_lock_task_context(struct task_struct *task, unsigned long *flags)
  112. {
  113. struct perf_counter_context *ctx;
  114. rcu_read_lock();
  115. retry:
  116. ctx = rcu_dereference(task->perf_counter_ctxp);
  117. if (ctx) {
  118. /*
  119. * If this context is a clone of another, it might
  120. * get swapped for another underneath us by
  121. * perf_counter_task_sched_out, though the
  122. * rcu_read_lock() protects us from any context
  123. * getting freed. Lock the context and check if it
  124. * got swapped before we could get the lock, and retry
  125. * if so. If we locked the right context, then it
  126. * can't get swapped on us any more.
  127. */
  128. spin_lock_irqsave(&ctx->lock, *flags);
  129. if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
  130. spin_unlock_irqrestore(&ctx->lock, *flags);
  131. goto retry;
  132. }
  133. }
  134. rcu_read_unlock();
  135. return ctx;
  136. }
  137. /*
  138. * Get the context for a task and increment its pin_count so it
  139. * can't get swapped to another task. This also increments its
  140. * reference count so that the context can't get freed.
  141. */
  142. static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
  143. {
  144. struct perf_counter_context *ctx;
  145. unsigned long flags;
  146. ctx = perf_lock_task_context(task, &flags);
  147. if (ctx) {
  148. ++ctx->pin_count;
  149. get_ctx(ctx);
  150. spin_unlock_irqrestore(&ctx->lock, flags);
  151. }
  152. return ctx;
  153. }
  154. static void perf_unpin_context(struct perf_counter_context *ctx)
  155. {
  156. unsigned long flags;
  157. spin_lock_irqsave(&ctx->lock, flags);
  158. --ctx->pin_count;
  159. spin_unlock_irqrestore(&ctx->lock, flags);
  160. put_ctx(ctx);
  161. }
  162. /*
  163. * Add a counter from the lists for its context.
  164. * Must be called with ctx->mutex and ctx->lock held.
  165. */
  166. static void
  167. list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  168. {
  169. struct perf_counter *group_leader = counter->group_leader;
  170. /*
  171. * Depending on whether it is a standalone or sibling counter,
  172. * add it straight to the context's counter list, or to the group
  173. * leader's sibling list:
  174. */
  175. if (group_leader == counter)
  176. list_add_tail(&counter->list_entry, &ctx->counter_list);
  177. else {
  178. list_add_tail(&counter->list_entry, &group_leader->sibling_list);
  179. group_leader->nr_siblings++;
  180. }
  181. list_add_rcu(&counter->event_entry, &ctx->event_list);
  182. ctx->nr_counters++;
  183. }
  184. /*
  185. * Remove a counter from the lists for its context.
  186. * Must be called with ctx->mutex and ctx->lock held.
  187. */
  188. static void
  189. list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  190. {
  191. struct perf_counter *sibling, *tmp;
  192. if (list_empty(&counter->list_entry))
  193. return;
  194. ctx->nr_counters--;
  195. list_del_init(&counter->list_entry);
  196. list_del_rcu(&counter->event_entry);
  197. if (counter->group_leader != counter)
  198. counter->group_leader->nr_siblings--;
  199. /*
  200. * If this was a group counter with sibling counters then
  201. * upgrade the siblings to singleton counters by adding them
  202. * to the context list directly:
  203. */
  204. list_for_each_entry_safe(sibling, tmp,
  205. &counter->sibling_list, list_entry) {
  206. list_move_tail(&sibling->list_entry, &ctx->counter_list);
  207. sibling->group_leader = sibling;
  208. }
  209. }
  210. static void
  211. counter_sched_out(struct perf_counter *counter,
  212. struct perf_cpu_context *cpuctx,
  213. struct perf_counter_context *ctx)
  214. {
  215. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  216. return;
  217. counter->state = PERF_COUNTER_STATE_INACTIVE;
  218. counter->tstamp_stopped = ctx->time;
  219. counter->pmu->disable(counter);
  220. counter->oncpu = -1;
  221. if (!is_software_counter(counter))
  222. cpuctx->active_oncpu--;
  223. ctx->nr_active--;
  224. if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
  225. cpuctx->exclusive = 0;
  226. }
  227. static void
  228. group_sched_out(struct perf_counter *group_counter,
  229. struct perf_cpu_context *cpuctx,
  230. struct perf_counter_context *ctx)
  231. {
  232. struct perf_counter *counter;
  233. if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
  234. return;
  235. counter_sched_out(group_counter, cpuctx, ctx);
  236. /*
  237. * Schedule out siblings (if any):
  238. */
  239. list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
  240. counter_sched_out(counter, cpuctx, ctx);
  241. if (group_counter->hw_event.exclusive)
  242. cpuctx->exclusive = 0;
  243. }
  244. /*
  245. * Cross CPU call to remove a performance counter
  246. *
  247. * We disable the counter on the hardware level first. After that we
  248. * remove it from the context list.
  249. */
  250. static void __perf_counter_remove_from_context(void *info)
  251. {
  252. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  253. struct perf_counter *counter = info;
  254. struct perf_counter_context *ctx = counter->ctx;
  255. /*
  256. * If this is a task context, we need to check whether it is
  257. * the current task context of this cpu. If not it has been
  258. * scheduled out before the smp call arrived.
  259. */
  260. if (ctx->task && cpuctx->task_ctx != ctx)
  261. return;
  262. spin_lock(&ctx->lock);
  263. /*
  264. * Protect the list operation against NMI by disabling the
  265. * counters on a global level.
  266. */
  267. perf_disable();
  268. counter_sched_out(counter, cpuctx, ctx);
  269. list_del_counter(counter, ctx);
  270. if (!ctx->task) {
  271. /*
  272. * Allow more per task counters with respect to the
  273. * reservation:
  274. */
  275. cpuctx->max_pertask =
  276. min(perf_max_counters - ctx->nr_counters,
  277. perf_max_counters - perf_reserved_percpu);
  278. }
  279. perf_enable();
  280. spin_unlock(&ctx->lock);
  281. }
  282. /*
  283. * Remove the counter from a task's (or a CPU's) list of counters.
  284. *
  285. * Must be called with ctx->mutex held.
  286. *
  287. * CPU counters are removed with a smp call. For task counters we only
  288. * call when the task is on a CPU.
  289. *
  290. * If counter->ctx is a cloned context, callers must make sure that
  291. * every task struct that counter->ctx->task could possibly point to
  292. * remains valid. This is OK when called from perf_release since
  293. * that only calls us on the top-level context, which can't be a clone.
  294. * When called from perf_counter_exit_task, it's OK because the
  295. * context has been detached from its task.
  296. */
  297. static void perf_counter_remove_from_context(struct perf_counter *counter)
  298. {
  299. struct perf_counter_context *ctx = counter->ctx;
  300. struct task_struct *task = ctx->task;
  301. if (!task) {
  302. /*
  303. * Per cpu counters are removed via an smp call and
  304. * the removal is always sucessful.
  305. */
  306. smp_call_function_single(counter->cpu,
  307. __perf_counter_remove_from_context,
  308. counter, 1);
  309. return;
  310. }
  311. retry:
  312. task_oncpu_function_call(task, __perf_counter_remove_from_context,
  313. counter);
  314. spin_lock_irq(&ctx->lock);
  315. /*
  316. * If the context is active we need to retry the smp call.
  317. */
  318. if (ctx->nr_active && !list_empty(&counter->list_entry)) {
  319. spin_unlock_irq(&ctx->lock);
  320. goto retry;
  321. }
  322. /*
  323. * The lock prevents that this context is scheduled in so we
  324. * can remove the counter safely, if the call above did not
  325. * succeed.
  326. */
  327. if (!list_empty(&counter->list_entry)) {
  328. list_del_counter(counter, ctx);
  329. }
  330. spin_unlock_irq(&ctx->lock);
  331. }
  332. static inline u64 perf_clock(void)
  333. {
  334. return cpu_clock(smp_processor_id());
  335. }
  336. /*
  337. * Update the record of the current time in a context.
  338. */
  339. static void update_context_time(struct perf_counter_context *ctx)
  340. {
  341. u64 now = perf_clock();
  342. ctx->time += now - ctx->timestamp;
  343. ctx->timestamp = now;
  344. }
  345. /*
  346. * Update the total_time_enabled and total_time_running fields for a counter.
  347. */
  348. static void update_counter_times(struct perf_counter *counter)
  349. {
  350. struct perf_counter_context *ctx = counter->ctx;
  351. u64 run_end;
  352. if (counter->state < PERF_COUNTER_STATE_INACTIVE)
  353. return;
  354. counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
  355. if (counter->state == PERF_COUNTER_STATE_INACTIVE)
  356. run_end = counter->tstamp_stopped;
  357. else
  358. run_end = ctx->time;
  359. counter->total_time_running = run_end - counter->tstamp_running;
  360. }
  361. /*
  362. * Update total_time_enabled and total_time_running for all counters in a group.
  363. */
  364. static void update_group_times(struct perf_counter *leader)
  365. {
  366. struct perf_counter *counter;
  367. update_counter_times(leader);
  368. list_for_each_entry(counter, &leader->sibling_list, list_entry)
  369. update_counter_times(counter);
  370. }
  371. /*
  372. * Cross CPU call to disable a performance counter
  373. */
  374. static void __perf_counter_disable(void *info)
  375. {
  376. struct perf_counter *counter = info;
  377. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  378. struct perf_counter_context *ctx = counter->ctx;
  379. /*
  380. * If this is a per-task counter, need to check whether this
  381. * counter's task is the current task on this cpu.
  382. */
  383. if (ctx->task && cpuctx->task_ctx != ctx)
  384. return;
  385. spin_lock(&ctx->lock);
  386. /*
  387. * If the counter is on, turn it off.
  388. * If it is in error state, leave it in error state.
  389. */
  390. if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
  391. update_context_time(ctx);
  392. update_counter_times(counter);
  393. if (counter == counter->group_leader)
  394. group_sched_out(counter, cpuctx, ctx);
  395. else
  396. counter_sched_out(counter, cpuctx, ctx);
  397. counter->state = PERF_COUNTER_STATE_OFF;
  398. }
  399. spin_unlock(&ctx->lock);
  400. }
  401. /*
  402. * Disable a counter.
  403. *
  404. * If counter->ctx is a cloned context, callers must make sure that
  405. * every task struct that counter->ctx->task could possibly point to
  406. * remains valid. This condition is satisifed when called through
  407. * perf_counter_for_each_child or perf_counter_for_each because they
  408. * hold the top-level counter's child_mutex, so any descendant that
  409. * goes to exit will block in sync_child_counter.
  410. * When called from perf_pending_counter it's OK because counter->ctx
  411. * is the current context on this CPU and preemption is disabled,
  412. * hence we can't get into perf_counter_task_sched_out for this context.
  413. */
  414. static void perf_counter_disable(struct perf_counter *counter)
  415. {
  416. struct perf_counter_context *ctx = counter->ctx;
  417. struct task_struct *task = ctx->task;
  418. if (!task) {
  419. /*
  420. * Disable the counter on the cpu that it's on
  421. */
  422. smp_call_function_single(counter->cpu, __perf_counter_disable,
  423. counter, 1);
  424. return;
  425. }
  426. retry:
  427. task_oncpu_function_call(task, __perf_counter_disable, counter);
  428. spin_lock_irq(&ctx->lock);
  429. /*
  430. * If the counter is still active, we need to retry the cross-call.
  431. */
  432. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  433. spin_unlock_irq(&ctx->lock);
  434. goto retry;
  435. }
  436. /*
  437. * Since we have the lock this context can't be scheduled
  438. * in, so we can change the state safely.
  439. */
  440. if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
  441. update_counter_times(counter);
  442. counter->state = PERF_COUNTER_STATE_OFF;
  443. }
  444. spin_unlock_irq(&ctx->lock);
  445. }
  446. static int
  447. counter_sched_in(struct perf_counter *counter,
  448. struct perf_cpu_context *cpuctx,
  449. struct perf_counter_context *ctx,
  450. int cpu)
  451. {
  452. if (counter->state <= PERF_COUNTER_STATE_OFF)
  453. return 0;
  454. counter->state = PERF_COUNTER_STATE_ACTIVE;
  455. counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
  456. /*
  457. * The new state must be visible before we turn it on in the hardware:
  458. */
  459. smp_wmb();
  460. if (counter->pmu->enable(counter)) {
  461. counter->state = PERF_COUNTER_STATE_INACTIVE;
  462. counter->oncpu = -1;
  463. return -EAGAIN;
  464. }
  465. counter->tstamp_running += ctx->time - counter->tstamp_stopped;
  466. if (!is_software_counter(counter))
  467. cpuctx->active_oncpu++;
  468. ctx->nr_active++;
  469. if (counter->hw_event.exclusive)
  470. cpuctx->exclusive = 1;
  471. return 0;
  472. }
  473. static int
  474. group_sched_in(struct perf_counter *group_counter,
  475. struct perf_cpu_context *cpuctx,
  476. struct perf_counter_context *ctx,
  477. int cpu)
  478. {
  479. struct perf_counter *counter, *partial_group;
  480. int ret;
  481. if (group_counter->state == PERF_COUNTER_STATE_OFF)
  482. return 0;
  483. ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
  484. if (ret)
  485. return ret < 0 ? ret : 0;
  486. if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
  487. return -EAGAIN;
  488. /*
  489. * Schedule in siblings as one group (if any):
  490. */
  491. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  492. if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
  493. partial_group = counter;
  494. goto group_error;
  495. }
  496. }
  497. return 0;
  498. group_error:
  499. /*
  500. * Groups can be scheduled in as one unit only, so undo any
  501. * partial group before returning:
  502. */
  503. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  504. if (counter == partial_group)
  505. break;
  506. counter_sched_out(counter, cpuctx, ctx);
  507. }
  508. counter_sched_out(group_counter, cpuctx, ctx);
  509. return -EAGAIN;
  510. }
  511. /*
  512. * Return 1 for a group consisting entirely of software counters,
  513. * 0 if the group contains any hardware counters.
  514. */
  515. static int is_software_only_group(struct perf_counter *leader)
  516. {
  517. struct perf_counter *counter;
  518. if (!is_software_counter(leader))
  519. return 0;
  520. list_for_each_entry(counter, &leader->sibling_list, list_entry)
  521. if (!is_software_counter(counter))
  522. return 0;
  523. return 1;
  524. }
  525. /*
  526. * Work out whether we can put this counter group on the CPU now.
  527. */
  528. static int group_can_go_on(struct perf_counter *counter,
  529. struct perf_cpu_context *cpuctx,
  530. int can_add_hw)
  531. {
  532. /*
  533. * Groups consisting entirely of software counters can always go on.
  534. */
  535. if (is_software_only_group(counter))
  536. return 1;
  537. /*
  538. * If an exclusive group is already on, no other hardware
  539. * counters can go on.
  540. */
  541. if (cpuctx->exclusive)
  542. return 0;
  543. /*
  544. * If this group is exclusive and there are already
  545. * counters on the CPU, it can't go on.
  546. */
  547. if (counter->hw_event.exclusive && cpuctx->active_oncpu)
  548. return 0;
  549. /*
  550. * Otherwise, try to add it if all previous groups were able
  551. * to go on.
  552. */
  553. return can_add_hw;
  554. }
  555. static void add_counter_to_ctx(struct perf_counter *counter,
  556. struct perf_counter_context *ctx)
  557. {
  558. list_add_counter(counter, ctx);
  559. counter->tstamp_enabled = ctx->time;
  560. counter->tstamp_running = ctx->time;
  561. counter->tstamp_stopped = ctx->time;
  562. }
  563. /*
  564. * Cross CPU call to install and enable a performance counter
  565. *
  566. * Must be called with ctx->mutex held
  567. */
  568. static void __perf_install_in_context(void *info)
  569. {
  570. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  571. struct perf_counter *counter = info;
  572. struct perf_counter_context *ctx = counter->ctx;
  573. struct perf_counter *leader = counter->group_leader;
  574. int cpu = smp_processor_id();
  575. int err;
  576. /*
  577. * If this is a task context, we need to check whether it is
  578. * the current task context of this cpu. If not it has been
  579. * scheduled out before the smp call arrived.
  580. * Or possibly this is the right context but it isn't
  581. * on this cpu because it had no counters.
  582. */
  583. if (ctx->task && cpuctx->task_ctx != ctx) {
  584. if (cpuctx->task_ctx || ctx->task != current)
  585. return;
  586. cpuctx->task_ctx = ctx;
  587. }
  588. spin_lock(&ctx->lock);
  589. ctx->is_active = 1;
  590. update_context_time(ctx);
  591. /*
  592. * Protect the list operation against NMI by disabling the
  593. * counters on a global level. NOP for non NMI based counters.
  594. */
  595. perf_disable();
  596. add_counter_to_ctx(counter, ctx);
  597. /*
  598. * Don't put the counter on if it is disabled or if
  599. * it is in a group and the group isn't on.
  600. */
  601. if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
  602. (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
  603. goto unlock;
  604. /*
  605. * An exclusive counter can't go on if there are already active
  606. * hardware counters, and no hardware counter can go on if there
  607. * is already an exclusive counter on.
  608. */
  609. if (!group_can_go_on(counter, cpuctx, 1))
  610. err = -EEXIST;
  611. else
  612. err = counter_sched_in(counter, cpuctx, ctx, cpu);
  613. if (err) {
  614. /*
  615. * This counter couldn't go on. If it is in a group
  616. * then we have to pull the whole group off.
  617. * If the counter group is pinned then put it in error state.
  618. */
  619. if (leader != counter)
  620. group_sched_out(leader, cpuctx, ctx);
  621. if (leader->hw_event.pinned) {
  622. update_group_times(leader);
  623. leader->state = PERF_COUNTER_STATE_ERROR;
  624. }
  625. }
  626. if (!err && !ctx->task && cpuctx->max_pertask)
  627. cpuctx->max_pertask--;
  628. unlock:
  629. perf_enable();
  630. spin_unlock(&ctx->lock);
  631. }
  632. /*
  633. * Attach a performance counter to a context
  634. *
  635. * First we add the counter to the list with the hardware enable bit
  636. * in counter->hw_config cleared.
  637. *
  638. * If the counter is attached to a task which is on a CPU we use a smp
  639. * call to enable it in the task context. The task might have been
  640. * scheduled away, but we check this in the smp call again.
  641. *
  642. * Must be called with ctx->mutex held.
  643. */
  644. static void
  645. perf_install_in_context(struct perf_counter_context *ctx,
  646. struct perf_counter *counter,
  647. int cpu)
  648. {
  649. struct task_struct *task = ctx->task;
  650. if (!task) {
  651. /*
  652. * Per cpu counters are installed via an smp call and
  653. * the install is always sucessful.
  654. */
  655. smp_call_function_single(cpu, __perf_install_in_context,
  656. counter, 1);
  657. return;
  658. }
  659. retry:
  660. task_oncpu_function_call(task, __perf_install_in_context,
  661. counter);
  662. spin_lock_irq(&ctx->lock);
  663. /*
  664. * we need to retry the smp call.
  665. */
  666. if (ctx->is_active && list_empty(&counter->list_entry)) {
  667. spin_unlock_irq(&ctx->lock);
  668. goto retry;
  669. }
  670. /*
  671. * The lock prevents that this context is scheduled in so we
  672. * can add the counter safely, if it the call above did not
  673. * succeed.
  674. */
  675. if (list_empty(&counter->list_entry))
  676. add_counter_to_ctx(counter, ctx);
  677. spin_unlock_irq(&ctx->lock);
  678. }
  679. /*
  680. * Cross CPU call to enable a performance counter
  681. */
  682. static void __perf_counter_enable(void *info)
  683. {
  684. struct perf_counter *counter = info;
  685. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  686. struct perf_counter_context *ctx = counter->ctx;
  687. struct perf_counter *leader = counter->group_leader;
  688. int err;
  689. /*
  690. * If this is a per-task counter, need to check whether this
  691. * counter's task is the current task on this cpu.
  692. */
  693. if (ctx->task && cpuctx->task_ctx != ctx) {
  694. if (cpuctx->task_ctx || ctx->task != current)
  695. return;
  696. cpuctx->task_ctx = ctx;
  697. }
  698. spin_lock(&ctx->lock);
  699. ctx->is_active = 1;
  700. update_context_time(ctx);
  701. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  702. goto unlock;
  703. counter->state = PERF_COUNTER_STATE_INACTIVE;
  704. counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
  705. /*
  706. * If the counter is in a group and isn't the group leader,
  707. * then don't put it on unless the group is on.
  708. */
  709. if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
  710. goto unlock;
  711. if (!group_can_go_on(counter, cpuctx, 1)) {
  712. err = -EEXIST;
  713. } else {
  714. perf_disable();
  715. if (counter == leader)
  716. err = group_sched_in(counter, cpuctx, ctx,
  717. smp_processor_id());
  718. else
  719. err = counter_sched_in(counter, cpuctx, ctx,
  720. smp_processor_id());
  721. perf_enable();
  722. }
  723. if (err) {
  724. /*
  725. * If this counter can't go on and it's part of a
  726. * group, then the whole group has to come off.
  727. */
  728. if (leader != counter)
  729. group_sched_out(leader, cpuctx, ctx);
  730. if (leader->hw_event.pinned) {
  731. update_group_times(leader);
  732. leader->state = PERF_COUNTER_STATE_ERROR;
  733. }
  734. }
  735. unlock:
  736. spin_unlock(&ctx->lock);
  737. }
  738. /*
  739. * Enable a counter.
  740. *
  741. * If counter->ctx is a cloned context, callers must make sure that
  742. * every task struct that counter->ctx->task could possibly point to
  743. * remains valid. This condition is satisfied when called through
  744. * perf_counter_for_each_child or perf_counter_for_each as described
  745. * for perf_counter_disable.
  746. */
  747. static void perf_counter_enable(struct perf_counter *counter)
  748. {
  749. struct perf_counter_context *ctx = counter->ctx;
  750. struct task_struct *task = ctx->task;
  751. if (!task) {
  752. /*
  753. * Enable the counter on the cpu that it's on
  754. */
  755. smp_call_function_single(counter->cpu, __perf_counter_enable,
  756. counter, 1);
  757. return;
  758. }
  759. spin_lock_irq(&ctx->lock);
  760. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  761. goto out;
  762. /*
  763. * If the counter is in error state, clear that first.
  764. * That way, if we see the counter in error state below, we
  765. * know that it has gone back into error state, as distinct
  766. * from the task having been scheduled away before the
  767. * cross-call arrived.
  768. */
  769. if (counter->state == PERF_COUNTER_STATE_ERROR)
  770. counter->state = PERF_COUNTER_STATE_OFF;
  771. retry:
  772. spin_unlock_irq(&ctx->lock);
  773. task_oncpu_function_call(task, __perf_counter_enable, counter);
  774. spin_lock_irq(&ctx->lock);
  775. /*
  776. * If the context is active and the counter is still off,
  777. * we need to retry the cross-call.
  778. */
  779. if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
  780. goto retry;
  781. /*
  782. * Since we have the lock this context can't be scheduled
  783. * in, so we can change the state safely.
  784. */
  785. if (counter->state == PERF_COUNTER_STATE_OFF) {
  786. counter->state = PERF_COUNTER_STATE_INACTIVE;
  787. counter->tstamp_enabled =
  788. ctx->time - counter->total_time_enabled;
  789. }
  790. out:
  791. spin_unlock_irq(&ctx->lock);
  792. }
  793. static int perf_counter_refresh(struct perf_counter *counter, int refresh)
  794. {
  795. /*
  796. * not supported on inherited counters
  797. */
  798. if (counter->hw_event.inherit)
  799. return -EINVAL;
  800. atomic_add(refresh, &counter->event_limit);
  801. perf_counter_enable(counter);
  802. return 0;
  803. }
  804. void __perf_counter_sched_out(struct perf_counter_context *ctx,
  805. struct perf_cpu_context *cpuctx)
  806. {
  807. struct perf_counter *counter;
  808. spin_lock(&ctx->lock);
  809. ctx->is_active = 0;
  810. if (likely(!ctx->nr_counters))
  811. goto out;
  812. update_context_time(ctx);
  813. perf_disable();
  814. if (ctx->nr_active) {
  815. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  816. if (counter != counter->group_leader)
  817. counter_sched_out(counter, cpuctx, ctx);
  818. else
  819. group_sched_out(counter, cpuctx, ctx);
  820. }
  821. }
  822. perf_enable();
  823. out:
  824. spin_unlock(&ctx->lock);
  825. }
  826. /*
  827. * Test whether two contexts are equivalent, i.e. whether they
  828. * have both been cloned from the same version of the same context
  829. * and they both have the same number of enabled counters.
  830. * If the number of enabled counters is the same, then the set
  831. * of enabled counters should be the same, because these are both
  832. * inherited contexts, therefore we can't access individual counters
  833. * in them directly with an fd; we can only enable/disable all
  834. * counters via prctl, or enable/disable all counters in a family
  835. * via ioctl, which will have the same effect on both contexts.
  836. */
  837. static int context_equiv(struct perf_counter_context *ctx1,
  838. struct perf_counter_context *ctx2)
  839. {
  840. return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
  841. && ctx1->parent_gen == ctx2->parent_gen
  842. && !ctx1->pin_count && !ctx2->pin_count;
  843. }
  844. /*
  845. * Called from scheduler to remove the counters of the current task,
  846. * with interrupts disabled.
  847. *
  848. * We stop each counter and update the counter value in counter->count.
  849. *
  850. * This does not protect us against NMI, but disable()
  851. * sets the disabled bit in the control field of counter _before_
  852. * accessing the counter control register. If a NMI hits, then it will
  853. * not restart the counter.
  854. */
  855. void perf_counter_task_sched_out(struct task_struct *task,
  856. struct task_struct *next, int cpu)
  857. {
  858. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  859. struct perf_counter_context *ctx = task->perf_counter_ctxp;
  860. struct perf_counter_context *next_ctx;
  861. struct perf_counter_context *parent;
  862. struct pt_regs *regs;
  863. int do_switch = 1;
  864. regs = task_pt_regs(task);
  865. perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
  866. if (likely(!ctx || !cpuctx->task_ctx))
  867. return;
  868. update_context_time(ctx);
  869. rcu_read_lock();
  870. parent = rcu_dereference(ctx->parent_ctx);
  871. next_ctx = next->perf_counter_ctxp;
  872. if (parent && next_ctx &&
  873. rcu_dereference(next_ctx->parent_ctx) == parent) {
  874. /*
  875. * Looks like the two contexts are clones, so we might be
  876. * able to optimize the context switch. We lock both
  877. * contexts and check that they are clones under the
  878. * lock (including re-checking that neither has been
  879. * uncloned in the meantime). It doesn't matter which
  880. * order we take the locks because no other cpu could
  881. * be trying to lock both of these tasks.
  882. */
  883. spin_lock(&ctx->lock);
  884. spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
  885. if (context_equiv(ctx, next_ctx)) {
  886. /*
  887. * XXX do we need a memory barrier of sorts
  888. * wrt to rcu_dereference() of perf_counter_ctxp
  889. */
  890. task->perf_counter_ctxp = next_ctx;
  891. next->perf_counter_ctxp = ctx;
  892. ctx->task = next;
  893. next_ctx->task = task;
  894. do_switch = 0;
  895. }
  896. spin_unlock(&next_ctx->lock);
  897. spin_unlock(&ctx->lock);
  898. }
  899. rcu_read_unlock();
  900. if (do_switch) {
  901. __perf_counter_sched_out(ctx, cpuctx);
  902. cpuctx->task_ctx = NULL;
  903. }
  904. }
  905. /*
  906. * Called with IRQs disabled
  907. */
  908. static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
  909. {
  910. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  911. if (!cpuctx->task_ctx)
  912. return;
  913. if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
  914. return;
  915. __perf_counter_sched_out(ctx, cpuctx);
  916. cpuctx->task_ctx = NULL;
  917. }
  918. /*
  919. * Called with IRQs disabled
  920. */
  921. static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
  922. {
  923. __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
  924. }
  925. static void
  926. __perf_counter_sched_in(struct perf_counter_context *ctx,
  927. struct perf_cpu_context *cpuctx, int cpu)
  928. {
  929. struct perf_counter *counter;
  930. int can_add_hw = 1;
  931. spin_lock(&ctx->lock);
  932. ctx->is_active = 1;
  933. if (likely(!ctx->nr_counters))
  934. goto out;
  935. ctx->timestamp = perf_clock();
  936. perf_disable();
  937. /*
  938. * First go through the list and put on any pinned groups
  939. * in order to give them the best chance of going on.
  940. */
  941. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  942. if (counter->state <= PERF_COUNTER_STATE_OFF ||
  943. !counter->hw_event.pinned)
  944. continue;
  945. if (counter->cpu != -1 && counter->cpu != cpu)
  946. continue;
  947. if (counter != counter->group_leader)
  948. counter_sched_in(counter, cpuctx, ctx, cpu);
  949. else {
  950. if (group_can_go_on(counter, cpuctx, 1))
  951. group_sched_in(counter, cpuctx, ctx, cpu);
  952. }
  953. /*
  954. * If this pinned group hasn't been scheduled,
  955. * put it in error state.
  956. */
  957. if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
  958. update_group_times(counter);
  959. counter->state = PERF_COUNTER_STATE_ERROR;
  960. }
  961. }
  962. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  963. /*
  964. * Ignore counters in OFF or ERROR state, and
  965. * ignore pinned counters since we did them already.
  966. */
  967. if (counter->state <= PERF_COUNTER_STATE_OFF ||
  968. counter->hw_event.pinned)
  969. continue;
  970. /*
  971. * Listen to the 'cpu' scheduling filter constraint
  972. * of counters:
  973. */
  974. if (counter->cpu != -1 && counter->cpu != cpu)
  975. continue;
  976. if (counter != counter->group_leader) {
  977. if (counter_sched_in(counter, cpuctx, ctx, cpu))
  978. can_add_hw = 0;
  979. } else {
  980. if (group_can_go_on(counter, cpuctx, can_add_hw)) {
  981. if (group_sched_in(counter, cpuctx, ctx, cpu))
  982. can_add_hw = 0;
  983. }
  984. }
  985. }
  986. perf_enable();
  987. out:
  988. spin_unlock(&ctx->lock);
  989. }
  990. /*
  991. * Called from scheduler to add the counters of the current task
  992. * with interrupts disabled.
  993. *
  994. * We restore the counter value and then enable it.
  995. *
  996. * This does not protect us against NMI, but enable()
  997. * sets the enabled bit in the control field of counter _before_
  998. * accessing the counter control register. If a NMI hits, then it will
  999. * keep the counter running.
  1000. */
  1001. void perf_counter_task_sched_in(struct task_struct *task, int cpu)
  1002. {
  1003. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  1004. struct perf_counter_context *ctx = task->perf_counter_ctxp;
  1005. if (likely(!ctx))
  1006. return;
  1007. if (cpuctx->task_ctx == ctx)
  1008. return;
  1009. __perf_counter_sched_in(ctx, cpuctx, cpu);
  1010. cpuctx->task_ctx = ctx;
  1011. }
  1012. static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
  1013. {
  1014. struct perf_counter_context *ctx = &cpuctx->ctx;
  1015. __perf_counter_sched_in(ctx, cpuctx, cpu);
  1016. }
  1017. #define MAX_INTERRUPTS (~0ULL)
  1018. static void perf_log_throttle(struct perf_counter *counter, int enable);
  1019. static void perf_log_period(struct perf_counter *counter, u64 period);
  1020. static void perf_adjust_freq(struct perf_counter_context *ctx)
  1021. {
  1022. struct perf_counter *counter;
  1023. u64 interrupts, sample_period;
  1024. u64 events, period;
  1025. s64 delta;
  1026. spin_lock(&ctx->lock);
  1027. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  1028. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  1029. continue;
  1030. interrupts = counter->hw.interrupts;
  1031. counter->hw.interrupts = 0;
  1032. if (interrupts == MAX_INTERRUPTS) {
  1033. perf_log_throttle(counter, 1);
  1034. counter->pmu->unthrottle(counter);
  1035. interrupts = 2*sysctl_perf_counter_limit/HZ;
  1036. }
  1037. if (!counter->hw_event.freq || !counter->hw_event.sample_freq)
  1038. continue;
  1039. events = HZ * interrupts * counter->hw.sample_period;
  1040. period = div64_u64(events, counter->hw_event.sample_freq);
  1041. delta = (s64)(1 + period - counter->hw.sample_period);
  1042. delta >>= 1;
  1043. sample_period = counter->hw.sample_period + delta;
  1044. if (!sample_period)
  1045. sample_period = 1;
  1046. perf_log_period(counter, sample_period);
  1047. counter->hw.sample_period = sample_period;
  1048. }
  1049. spin_unlock(&ctx->lock);
  1050. }
  1051. /*
  1052. * Round-robin a context's counters:
  1053. */
  1054. static void rotate_ctx(struct perf_counter_context *ctx)
  1055. {
  1056. struct perf_counter *counter;
  1057. if (!ctx->nr_counters)
  1058. return;
  1059. spin_lock(&ctx->lock);
  1060. /*
  1061. * Rotate the first entry last (works just fine for group counters too):
  1062. */
  1063. perf_disable();
  1064. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  1065. list_move_tail(&counter->list_entry, &ctx->counter_list);
  1066. break;
  1067. }
  1068. perf_enable();
  1069. spin_unlock(&ctx->lock);
  1070. }
  1071. void perf_counter_task_tick(struct task_struct *curr, int cpu)
  1072. {
  1073. struct perf_cpu_context *cpuctx;
  1074. struct perf_counter_context *ctx;
  1075. if (!atomic_read(&nr_counters))
  1076. return;
  1077. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1078. ctx = curr->perf_counter_ctxp;
  1079. perf_adjust_freq(&cpuctx->ctx);
  1080. if (ctx)
  1081. perf_adjust_freq(ctx);
  1082. perf_counter_cpu_sched_out(cpuctx);
  1083. if (ctx)
  1084. __perf_counter_task_sched_out(ctx);
  1085. rotate_ctx(&cpuctx->ctx);
  1086. if (ctx)
  1087. rotate_ctx(ctx);
  1088. perf_counter_cpu_sched_in(cpuctx, cpu);
  1089. if (ctx)
  1090. perf_counter_task_sched_in(curr, cpu);
  1091. }
  1092. /*
  1093. * Cross CPU call to read the hardware counter
  1094. */
  1095. static void __read(void *info)
  1096. {
  1097. struct perf_counter *counter = info;
  1098. struct perf_counter_context *ctx = counter->ctx;
  1099. unsigned long flags;
  1100. local_irq_save(flags);
  1101. if (ctx->is_active)
  1102. update_context_time(ctx);
  1103. counter->pmu->read(counter);
  1104. update_counter_times(counter);
  1105. local_irq_restore(flags);
  1106. }
  1107. static u64 perf_counter_read(struct perf_counter *counter)
  1108. {
  1109. /*
  1110. * If counter is enabled and currently active on a CPU, update the
  1111. * value in the counter structure:
  1112. */
  1113. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  1114. smp_call_function_single(counter->oncpu,
  1115. __read, counter, 1);
  1116. } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
  1117. update_counter_times(counter);
  1118. }
  1119. return atomic64_read(&counter->count);
  1120. }
  1121. /*
  1122. * Initialize the perf_counter context in a task_struct:
  1123. */
  1124. static void
  1125. __perf_counter_init_context(struct perf_counter_context *ctx,
  1126. struct task_struct *task)
  1127. {
  1128. memset(ctx, 0, sizeof(*ctx));
  1129. spin_lock_init(&ctx->lock);
  1130. mutex_init(&ctx->mutex);
  1131. INIT_LIST_HEAD(&ctx->counter_list);
  1132. INIT_LIST_HEAD(&ctx->event_list);
  1133. atomic_set(&ctx->refcount, 1);
  1134. ctx->task = task;
  1135. }
  1136. static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  1137. {
  1138. struct perf_counter_context *parent_ctx;
  1139. struct perf_counter_context *ctx;
  1140. struct perf_cpu_context *cpuctx;
  1141. struct task_struct *task;
  1142. unsigned long flags;
  1143. int err;
  1144. /*
  1145. * If cpu is not a wildcard then this is a percpu counter:
  1146. */
  1147. if (cpu != -1) {
  1148. /* Must be root to operate on a CPU counter: */
  1149. if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
  1150. return ERR_PTR(-EACCES);
  1151. if (cpu < 0 || cpu > num_possible_cpus())
  1152. return ERR_PTR(-EINVAL);
  1153. /*
  1154. * We could be clever and allow to attach a counter to an
  1155. * offline CPU and activate it when the CPU comes up, but
  1156. * that's for later.
  1157. */
  1158. if (!cpu_isset(cpu, cpu_online_map))
  1159. return ERR_PTR(-ENODEV);
  1160. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1161. ctx = &cpuctx->ctx;
  1162. get_ctx(ctx);
  1163. return ctx;
  1164. }
  1165. rcu_read_lock();
  1166. if (!pid)
  1167. task = current;
  1168. else
  1169. task = find_task_by_vpid(pid);
  1170. if (task)
  1171. get_task_struct(task);
  1172. rcu_read_unlock();
  1173. if (!task)
  1174. return ERR_PTR(-ESRCH);
  1175. /*
  1176. * Can't attach counters to a dying task.
  1177. */
  1178. err = -ESRCH;
  1179. if (task->flags & PF_EXITING)
  1180. goto errout;
  1181. /* Reuse ptrace permission checks for now. */
  1182. err = -EACCES;
  1183. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  1184. goto errout;
  1185. retry:
  1186. ctx = perf_lock_task_context(task, &flags);
  1187. if (ctx) {
  1188. parent_ctx = ctx->parent_ctx;
  1189. if (parent_ctx) {
  1190. put_ctx(parent_ctx);
  1191. ctx->parent_ctx = NULL; /* no longer a clone */
  1192. }
  1193. /*
  1194. * Get an extra reference before dropping the lock so that
  1195. * this context won't get freed if the task exits.
  1196. */
  1197. get_ctx(ctx);
  1198. spin_unlock_irqrestore(&ctx->lock, flags);
  1199. }
  1200. if (!ctx) {
  1201. ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
  1202. err = -ENOMEM;
  1203. if (!ctx)
  1204. goto errout;
  1205. __perf_counter_init_context(ctx, task);
  1206. get_ctx(ctx);
  1207. if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
  1208. /*
  1209. * We raced with some other task; use
  1210. * the context they set.
  1211. */
  1212. kfree(ctx);
  1213. goto retry;
  1214. }
  1215. get_task_struct(task);
  1216. }
  1217. put_task_struct(task);
  1218. return ctx;
  1219. errout:
  1220. put_task_struct(task);
  1221. return ERR_PTR(err);
  1222. }
  1223. static void free_counter_rcu(struct rcu_head *head)
  1224. {
  1225. struct perf_counter *counter;
  1226. counter = container_of(head, struct perf_counter, rcu_head);
  1227. if (counter->ns)
  1228. put_pid_ns(counter->ns);
  1229. kfree(counter);
  1230. }
  1231. static void perf_pending_sync(struct perf_counter *counter);
  1232. static void free_counter(struct perf_counter *counter)
  1233. {
  1234. perf_pending_sync(counter);
  1235. atomic_dec(&nr_counters);
  1236. if (counter->hw_event.mmap)
  1237. atomic_dec(&nr_mmap_tracking);
  1238. if (counter->hw_event.munmap)
  1239. atomic_dec(&nr_munmap_tracking);
  1240. if (counter->hw_event.comm)
  1241. atomic_dec(&nr_comm_tracking);
  1242. if (counter->destroy)
  1243. counter->destroy(counter);
  1244. put_ctx(counter->ctx);
  1245. call_rcu(&counter->rcu_head, free_counter_rcu);
  1246. }
  1247. /*
  1248. * Called when the last reference to the file is gone.
  1249. */
  1250. static int perf_release(struct inode *inode, struct file *file)
  1251. {
  1252. struct perf_counter *counter = file->private_data;
  1253. struct perf_counter_context *ctx = counter->ctx;
  1254. file->private_data = NULL;
  1255. WARN_ON_ONCE(ctx->parent_ctx);
  1256. mutex_lock(&ctx->mutex);
  1257. perf_counter_remove_from_context(counter);
  1258. mutex_unlock(&ctx->mutex);
  1259. mutex_lock(&counter->owner->perf_counter_mutex);
  1260. list_del_init(&counter->owner_entry);
  1261. mutex_unlock(&counter->owner->perf_counter_mutex);
  1262. put_task_struct(counter->owner);
  1263. free_counter(counter);
  1264. return 0;
  1265. }
  1266. /*
  1267. * Read the performance counter - simple non blocking version for now
  1268. */
  1269. static ssize_t
  1270. perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
  1271. {
  1272. u64 values[3];
  1273. int n;
  1274. /*
  1275. * Return end-of-file for a read on a counter that is in
  1276. * error state (i.e. because it was pinned but it couldn't be
  1277. * scheduled on to the CPU at some point).
  1278. */
  1279. if (counter->state == PERF_COUNTER_STATE_ERROR)
  1280. return 0;
  1281. WARN_ON_ONCE(counter->ctx->parent_ctx);
  1282. mutex_lock(&counter->child_mutex);
  1283. values[0] = perf_counter_read(counter);
  1284. n = 1;
  1285. if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1286. values[n++] = counter->total_time_enabled +
  1287. atomic64_read(&counter->child_total_time_enabled);
  1288. if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1289. values[n++] = counter->total_time_running +
  1290. atomic64_read(&counter->child_total_time_running);
  1291. if (counter->hw_event.read_format & PERF_FORMAT_ID)
  1292. values[n++] = counter->id;
  1293. mutex_unlock(&counter->child_mutex);
  1294. if (count < n * sizeof(u64))
  1295. return -EINVAL;
  1296. count = n * sizeof(u64);
  1297. if (copy_to_user(buf, values, count))
  1298. return -EFAULT;
  1299. return count;
  1300. }
  1301. static ssize_t
  1302. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  1303. {
  1304. struct perf_counter *counter = file->private_data;
  1305. return perf_read_hw(counter, buf, count);
  1306. }
  1307. static unsigned int perf_poll(struct file *file, poll_table *wait)
  1308. {
  1309. struct perf_counter *counter = file->private_data;
  1310. struct perf_mmap_data *data;
  1311. unsigned int events = POLL_HUP;
  1312. rcu_read_lock();
  1313. data = rcu_dereference(counter->data);
  1314. if (data)
  1315. events = atomic_xchg(&data->poll, 0);
  1316. rcu_read_unlock();
  1317. poll_wait(file, &counter->waitq, wait);
  1318. return events;
  1319. }
  1320. static void perf_counter_reset(struct perf_counter *counter)
  1321. {
  1322. (void)perf_counter_read(counter);
  1323. atomic64_set(&counter->count, 0);
  1324. perf_counter_update_userpage(counter);
  1325. }
  1326. static void perf_counter_for_each_sibling(struct perf_counter *counter,
  1327. void (*func)(struct perf_counter *))
  1328. {
  1329. struct perf_counter_context *ctx = counter->ctx;
  1330. struct perf_counter *sibling;
  1331. WARN_ON_ONCE(ctx->parent_ctx);
  1332. mutex_lock(&ctx->mutex);
  1333. counter = counter->group_leader;
  1334. func(counter);
  1335. list_for_each_entry(sibling, &counter->sibling_list, list_entry)
  1336. func(sibling);
  1337. mutex_unlock(&ctx->mutex);
  1338. }
  1339. /*
  1340. * Holding the top-level counter's child_mutex means that any
  1341. * descendant process that has inherited this counter will block
  1342. * in sync_child_counter if it goes to exit, thus satisfying the
  1343. * task existence requirements of perf_counter_enable/disable.
  1344. */
  1345. static void perf_counter_for_each_child(struct perf_counter *counter,
  1346. void (*func)(struct perf_counter *))
  1347. {
  1348. struct perf_counter *child;
  1349. WARN_ON_ONCE(counter->ctx->parent_ctx);
  1350. mutex_lock(&counter->child_mutex);
  1351. func(counter);
  1352. list_for_each_entry(child, &counter->child_list, child_list)
  1353. func(child);
  1354. mutex_unlock(&counter->child_mutex);
  1355. }
  1356. static void perf_counter_for_each(struct perf_counter *counter,
  1357. void (*func)(struct perf_counter *))
  1358. {
  1359. struct perf_counter *child;
  1360. WARN_ON_ONCE(counter->ctx->parent_ctx);
  1361. mutex_lock(&counter->child_mutex);
  1362. perf_counter_for_each_sibling(counter, func);
  1363. list_for_each_entry(child, &counter->child_list, child_list)
  1364. perf_counter_for_each_sibling(child, func);
  1365. mutex_unlock(&counter->child_mutex);
  1366. }
  1367. static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
  1368. {
  1369. struct perf_counter_context *ctx = counter->ctx;
  1370. unsigned long size;
  1371. int ret = 0;
  1372. u64 value;
  1373. if (!counter->hw_event.sample_period)
  1374. return -EINVAL;
  1375. size = copy_from_user(&value, arg, sizeof(value));
  1376. if (size != sizeof(value))
  1377. return -EFAULT;
  1378. if (!value)
  1379. return -EINVAL;
  1380. spin_lock_irq(&ctx->lock);
  1381. if (counter->hw_event.freq) {
  1382. if (value > sysctl_perf_counter_limit) {
  1383. ret = -EINVAL;
  1384. goto unlock;
  1385. }
  1386. counter->hw_event.sample_freq = value;
  1387. } else {
  1388. counter->hw_event.sample_period = value;
  1389. counter->hw.sample_period = value;
  1390. perf_log_period(counter, value);
  1391. }
  1392. unlock:
  1393. spin_unlock_irq(&ctx->lock);
  1394. return ret;
  1395. }
  1396. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1397. {
  1398. struct perf_counter *counter = file->private_data;
  1399. void (*func)(struct perf_counter *);
  1400. u32 flags = arg;
  1401. switch (cmd) {
  1402. case PERF_COUNTER_IOC_ENABLE:
  1403. func = perf_counter_enable;
  1404. break;
  1405. case PERF_COUNTER_IOC_DISABLE:
  1406. func = perf_counter_disable;
  1407. break;
  1408. case PERF_COUNTER_IOC_RESET:
  1409. func = perf_counter_reset;
  1410. break;
  1411. case PERF_COUNTER_IOC_REFRESH:
  1412. return perf_counter_refresh(counter, arg);
  1413. case PERF_COUNTER_IOC_PERIOD:
  1414. return perf_counter_period(counter, (u64 __user *)arg);
  1415. default:
  1416. return -ENOTTY;
  1417. }
  1418. if (flags & PERF_IOC_FLAG_GROUP)
  1419. perf_counter_for_each(counter, func);
  1420. else
  1421. perf_counter_for_each_child(counter, func);
  1422. return 0;
  1423. }
  1424. int perf_counter_task_enable(void)
  1425. {
  1426. struct perf_counter *counter;
  1427. mutex_lock(&current->perf_counter_mutex);
  1428. list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
  1429. perf_counter_for_each_child(counter, perf_counter_enable);
  1430. mutex_unlock(&current->perf_counter_mutex);
  1431. return 0;
  1432. }
  1433. int perf_counter_task_disable(void)
  1434. {
  1435. struct perf_counter *counter;
  1436. mutex_lock(&current->perf_counter_mutex);
  1437. list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
  1438. perf_counter_for_each_child(counter, perf_counter_disable);
  1439. mutex_unlock(&current->perf_counter_mutex);
  1440. return 0;
  1441. }
  1442. /*
  1443. * Callers need to ensure there can be no nesting of this function, otherwise
  1444. * the seqlock logic goes bad. We can not serialize this because the arch
  1445. * code calls this from NMI context.
  1446. */
  1447. void perf_counter_update_userpage(struct perf_counter *counter)
  1448. {
  1449. struct perf_counter_mmap_page *userpg;
  1450. struct perf_mmap_data *data;
  1451. rcu_read_lock();
  1452. data = rcu_dereference(counter->data);
  1453. if (!data)
  1454. goto unlock;
  1455. userpg = data->user_page;
  1456. /*
  1457. * Disable preemption so as to not let the corresponding user-space
  1458. * spin too long if we get preempted.
  1459. */
  1460. preempt_disable();
  1461. ++userpg->lock;
  1462. barrier();
  1463. userpg->index = counter->hw.idx;
  1464. userpg->offset = atomic64_read(&counter->count);
  1465. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  1466. userpg->offset -= atomic64_read(&counter->hw.prev_count);
  1467. barrier();
  1468. ++userpg->lock;
  1469. preempt_enable();
  1470. unlock:
  1471. rcu_read_unlock();
  1472. }
  1473. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1474. {
  1475. struct perf_counter *counter = vma->vm_file->private_data;
  1476. struct perf_mmap_data *data;
  1477. int ret = VM_FAULT_SIGBUS;
  1478. rcu_read_lock();
  1479. data = rcu_dereference(counter->data);
  1480. if (!data)
  1481. goto unlock;
  1482. if (vmf->pgoff == 0) {
  1483. vmf->page = virt_to_page(data->user_page);
  1484. } else {
  1485. int nr = vmf->pgoff - 1;
  1486. if ((unsigned)nr > data->nr_pages)
  1487. goto unlock;
  1488. vmf->page = virt_to_page(data->data_pages[nr]);
  1489. }
  1490. get_page(vmf->page);
  1491. ret = 0;
  1492. unlock:
  1493. rcu_read_unlock();
  1494. return ret;
  1495. }
  1496. static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
  1497. {
  1498. struct perf_mmap_data *data;
  1499. unsigned long size;
  1500. int i;
  1501. WARN_ON(atomic_read(&counter->mmap_count));
  1502. size = sizeof(struct perf_mmap_data);
  1503. size += nr_pages * sizeof(void *);
  1504. data = kzalloc(size, GFP_KERNEL);
  1505. if (!data)
  1506. goto fail;
  1507. data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
  1508. if (!data->user_page)
  1509. goto fail_user_page;
  1510. for (i = 0; i < nr_pages; i++) {
  1511. data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
  1512. if (!data->data_pages[i])
  1513. goto fail_data_pages;
  1514. }
  1515. data->nr_pages = nr_pages;
  1516. atomic_set(&data->lock, -1);
  1517. rcu_assign_pointer(counter->data, data);
  1518. return 0;
  1519. fail_data_pages:
  1520. for (i--; i >= 0; i--)
  1521. free_page((unsigned long)data->data_pages[i]);
  1522. free_page((unsigned long)data->user_page);
  1523. fail_user_page:
  1524. kfree(data);
  1525. fail:
  1526. return -ENOMEM;
  1527. }
  1528. static void __perf_mmap_data_free(struct rcu_head *rcu_head)
  1529. {
  1530. struct perf_mmap_data *data;
  1531. int i;
  1532. data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
  1533. free_page((unsigned long)data->user_page);
  1534. for (i = 0; i < data->nr_pages; i++)
  1535. free_page((unsigned long)data->data_pages[i]);
  1536. kfree(data);
  1537. }
  1538. static void perf_mmap_data_free(struct perf_counter *counter)
  1539. {
  1540. struct perf_mmap_data *data = counter->data;
  1541. WARN_ON(atomic_read(&counter->mmap_count));
  1542. rcu_assign_pointer(counter->data, NULL);
  1543. call_rcu(&data->rcu_head, __perf_mmap_data_free);
  1544. }
  1545. static void perf_mmap_open(struct vm_area_struct *vma)
  1546. {
  1547. struct perf_counter *counter = vma->vm_file->private_data;
  1548. atomic_inc(&counter->mmap_count);
  1549. }
  1550. static void perf_mmap_close(struct vm_area_struct *vma)
  1551. {
  1552. struct perf_counter *counter = vma->vm_file->private_data;
  1553. WARN_ON_ONCE(counter->ctx->parent_ctx);
  1554. if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
  1555. struct user_struct *user = current_user();
  1556. atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
  1557. vma->vm_mm->locked_vm -= counter->data->nr_locked;
  1558. perf_mmap_data_free(counter);
  1559. mutex_unlock(&counter->mmap_mutex);
  1560. }
  1561. }
  1562. static struct vm_operations_struct perf_mmap_vmops = {
  1563. .open = perf_mmap_open,
  1564. .close = perf_mmap_close,
  1565. .fault = perf_mmap_fault,
  1566. };
  1567. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  1568. {
  1569. struct perf_counter *counter = file->private_data;
  1570. unsigned long user_locked, user_lock_limit;
  1571. struct user_struct *user = current_user();
  1572. unsigned long locked, lock_limit;
  1573. unsigned long vma_size;
  1574. unsigned long nr_pages;
  1575. long user_extra, extra;
  1576. int ret = 0;
  1577. if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
  1578. return -EINVAL;
  1579. vma_size = vma->vm_end - vma->vm_start;
  1580. nr_pages = (vma_size / PAGE_SIZE) - 1;
  1581. /*
  1582. * If we have data pages ensure they're a power-of-two number, so we
  1583. * can do bitmasks instead of modulo.
  1584. */
  1585. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  1586. return -EINVAL;
  1587. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  1588. return -EINVAL;
  1589. if (vma->vm_pgoff != 0)
  1590. return -EINVAL;
  1591. WARN_ON_ONCE(counter->ctx->parent_ctx);
  1592. mutex_lock(&counter->mmap_mutex);
  1593. if (atomic_inc_not_zero(&counter->mmap_count)) {
  1594. if (nr_pages != counter->data->nr_pages)
  1595. ret = -EINVAL;
  1596. goto unlock;
  1597. }
  1598. user_extra = nr_pages + 1;
  1599. user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
  1600. /*
  1601. * Increase the limit linearly with more CPUs:
  1602. */
  1603. user_lock_limit *= num_online_cpus();
  1604. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  1605. extra = 0;
  1606. if (user_locked > user_lock_limit)
  1607. extra = user_locked - user_lock_limit;
  1608. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  1609. lock_limit >>= PAGE_SHIFT;
  1610. locked = vma->vm_mm->locked_vm + extra;
  1611. if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
  1612. ret = -EPERM;
  1613. goto unlock;
  1614. }
  1615. WARN_ON(counter->data);
  1616. ret = perf_mmap_data_alloc(counter, nr_pages);
  1617. if (ret)
  1618. goto unlock;
  1619. atomic_set(&counter->mmap_count, 1);
  1620. atomic_long_add(user_extra, &user->locked_vm);
  1621. vma->vm_mm->locked_vm += extra;
  1622. counter->data->nr_locked = extra;
  1623. unlock:
  1624. mutex_unlock(&counter->mmap_mutex);
  1625. vma->vm_flags &= ~VM_MAYWRITE;
  1626. vma->vm_flags |= VM_RESERVED;
  1627. vma->vm_ops = &perf_mmap_vmops;
  1628. return ret;
  1629. }
  1630. static int perf_fasync(int fd, struct file *filp, int on)
  1631. {
  1632. struct inode *inode = filp->f_path.dentry->d_inode;
  1633. struct perf_counter *counter = filp->private_data;
  1634. int retval;
  1635. mutex_lock(&inode->i_mutex);
  1636. retval = fasync_helper(fd, filp, on, &counter->fasync);
  1637. mutex_unlock(&inode->i_mutex);
  1638. if (retval < 0)
  1639. return retval;
  1640. return 0;
  1641. }
  1642. static const struct file_operations perf_fops = {
  1643. .release = perf_release,
  1644. .read = perf_read,
  1645. .poll = perf_poll,
  1646. .unlocked_ioctl = perf_ioctl,
  1647. .compat_ioctl = perf_ioctl,
  1648. .mmap = perf_mmap,
  1649. .fasync = perf_fasync,
  1650. };
  1651. /*
  1652. * Perf counter wakeup
  1653. *
  1654. * If there's data, ensure we set the poll() state and publish everything
  1655. * to user-space before waking everybody up.
  1656. */
  1657. void perf_counter_wakeup(struct perf_counter *counter)
  1658. {
  1659. wake_up_all(&counter->waitq);
  1660. if (counter->pending_kill) {
  1661. kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
  1662. counter->pending_kill = 0;
  1663. }
  1664. }
  1665. /*
  1666. * Pending wakeups
  1667. *
  1668. * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
  1669. *
  1670. * The NMI bit means we cannot possibly take locks. Therefore, maintain a
  1671. * single linked list and use cmpxchg() to add entries lockless.
  1672. */
  1673. static void perf_pending_counter(struct perf_pending_entry *entry)
  1674. {
  1675. struct perf_counter *counter = container_of(entry,
  1676. struct perf_counter, pending);
  1677. if (counter->pending_disable) {
  1678. counter->pending_disable = 0;
  1679. perf_counter_disable(counter);
  1680. }
  1681. if (counter->pending_wakeup) {
  1682. counter->pending_wakeup = 0;
  1683. perf_counter_wakeup(counter);
  1684. }
  1685. }
  1686. #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
  1687. static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
  1688. PENDING_TAIL,
  1689. };
  1690. static void perf_pending_queue(struct perf_pending_entry *entry,
  1691. void (*func)(struct perf_pending_entry *))
  1692. {
  1693. struct perf_pending_entry **head;
  1694. if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
  1695. return;
  1696. entry->func = func;
  1697. head = &get_cpu_var(perf_pending_head);
  1698. do {
  1699. entry->next = *head;
  1700. } while (cmpxchg(head, entry->next, entry) != entry->next);
  1701. set_perf_counter_pending();
  1702. put_cpu_var(perf_pending_head);
  1703. }
  1704. static int __perf_pending_run(void)
  1705. {
  1706. struct perf_pending_entry *list;
  1707. int nr = 0;
  1708. list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
  1709. while (list != PENDING_TAIL) {
  1710. void (*func)(struct perf_pending_entry *);
  1711. struct perf_pending_entry *entry = list;
  1712. list = list->next;
  1713. func = entry->func;
  1714. entry->next = NULL;
  1715. /*
  1716. * Ensure we observe the unqueue before we issue the wakeup,
  1717. * so that we won't be waiting forever.
  1718. * -- see perf_not_pending().
  1719. */
  1720. smp_wmb();
  1721. func(entry);
  1722. nr++;
  1723. }
  1724. return nr;
  1725. }
  1726. static inline int perf_not_pending(struct perf_counter *counter)
  1727. {
  1728. /*
  1729. * If we flush on whatever cpu we run, there is a chance we don't
  1730. * need to wait.
  1731. */
  1732. get_cpu();
  1733. __perf_pending_run();
  1734. put_cpu();
  1735. /*
  1736. * Ensure we see the proper queue state before going to sleep
  1737. * so that we do not miss the wakeup. -- see perf_pending_handle()
  1738. */
  1739. smp_rmb();
  1740. return counter->pending.next == NULL;
  1741. }
  1742. static void perf_pending_sync(struct perf_counter *counter)
  1743. {
  1744. wait_event(counter->waitq, perf_not_pending(counter));
  1745. }
  1746. void perf_counter_do_pending(void)
  1747. {
  1748. __perf_pending_run();
  1749. }
  1750. /*
  1751. * Callchain support -- arch specific
  1752. */
  1753. __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1754. {
  1755. return NULL;
  1756. }
  1757. /*
  1758. * Output
  1759. */
  1760. struct perf_output_handle {
  1761. struct perf_counter *counter;
  1762. struct perf_mmap_data *data;
  1763. unsigned long head;
  1764. unsigned long offset;
  1765. int nmi;
  1766. int overflow;
  1767. int locked;
  1768. unsigned long flags;
  1769. };
  1770. static void perf_output_wakeup(struct perf_output_handle *handle)
  1771. {
  1772. atomic_set(&handle->data->poll, POLL_IN);
  1773. if (handle->nmi) {
  1774. handle->counter->pending_wakeup = 1;
  1775. perf_pending_queue(&handle->counter->pending,
  1776. perf_pending_counter);
  1777. } else
  1778. perf_counter_wakeup(handle->counter);
  1779. }
  1780. /*
  1781. * Curious locking construct.
  1782. *
  1783. * We need to ensure a later event doesn't publish a head when a former
  1784. * event isn't done writing. However since we need to deal with NMIs we
  1785. * cannot fully serialize things.
  1786. *
  1787. * What we do is serialize between CPUs so we only have to deal with NMI
  1788. * nesting on a single CPU.
  1789. *
  1790. * We only publish the head (and generate a wakeup) when the outer-most
  1791. * event completes.
  1792. */
  1793. static void perf_output_lock(struct perf_output_handle *handle)
  1794. {
  1795. struct perf_mmap_data *data = handle->data;
  1796. int cpu;
  1797. handle->locked = 0;
  1798. local_irq_save(handle->flags);
  1799. cpu = smp_processor_id();
  1800. if (in_nmi() && atomic_read(&data->lock) == cpu)
  1801. return;
  1802. while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
  1803. cpu_relax();
  1804. handle->locked = 1;
  1805. }
  1806. static void perf_output_unlock(struct perf_output_handle *handle)
  1807. {
  1808. struct perf_mmap_data *data = handle->data;
  1809. unsigned long head;
  1810. int cpu;
  1811. data->done_head = data->head;
  1812. if (!handle->locked)
  1813. goto out;
  1814. again:
  1815. /*
  1816. * The xchg implies a full barrier that ensures all writes are done
  1817. * before we publish the new head, matched by a rmb() in userspace when
  1818. * reading this position.
  1819. */
  1820. while ((head = atomic_long_xchg(&data->done_head, 0)))
  1821. data->user_page->data_head = head;
  1822. /*
  1823. * NMI can happen here, which means we can miss a done_head update.
  1824. */
  1825. cpu = atomic_xchg(&data->lock, -1);
  1826. WARN_ON_ONCE(cpu != smp_processor_id());
  1827. /*
  1828. * Therefore we have to validate we did not indeed do so.
  1829. */
  1830. if (unlikely(atomic_long_read(&data->done_head))) {
  1831. /*
  1832. * Since we had it locked, we can lock it again.
  1833. */
  1834. while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
  1835. cpu_relax();
  1836. goto again;
  1837. }
  1838. if (atomic_xchg(&data->wakeup, 0))
  1839. perf_output_wakeup(handle);
  1840. out:
  1841. local_irq_restore(handle->flags);
  1842. }
  1843. static int perf_output_begin(struct perf_output_handle *handle,
  1844. struct perf_counter *counter, unsigned int size,
  1845. int nmi, int overflow)
  1846. {
  1847. struct perf_mmap_data *data;
  1848. unsigned int offset, head;
  1849. /*
  1850. * For inherited counters we send all the output towards the parent.
  1851. */
  1852. if (counter->parent)
  1853. counter = counter->parent;
  1854. rcu_read_lock();
  1855. data = rcu_dereference(counter->data);
  1856. if (!data)
  1857. goto out;
  1858. handle->data = data;
  1859. handle->counter = counter;
  1860. handle->nmi = nmi;
  1861. handle->overflow = overflow;
  1862. if (!data->nr_pages)
  1863. goto fail;
  1864. perf_output_lock(handle);
  1865. do {
  1866. offset = head = atomic_read(&data->head);
  1867. head += size;
  1868. } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
  1869. handle->offset = offset;
  1870. handle->head = head;
  1871. if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
  1872. atomic_set(&data->wakeup, 1);
  1873. return 0;
  1874. fail:
  1875. perf_output_wakeup(handle);
  1876. out:
  1877. rcu_read_unlock();
  1878. return -ENOSPC;
  1879. }
  1880. static void perf_output_copy(struct perf_output_handle *handle,
  1881. void *buf, unsigned int len)
  1882. {
  1883. unsigned int pages_mask;
  1884. unsigned int offset;
  1885. unsigned int size;
  1886. void **pages;
  1887. offset = handle->offset;
  1888. pages_mask = handle->data->nr_pages - 1;
  1889. pages = handle->data->data_pages;
  1890. do {
  1891. unsigned int page_offset;
  1892. int nr;
  1893. nr = (offset >> PAGE_SHIFT) & pages_mask;
  1894. page_offset = offset & (PAGE_SIZE - 1);
  1895. size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
  1896. memcpy(pages[nr] + page_offset, buf, size);
  1897. len -= size;
  1898. buf += size;
  1899. offset += size;
  1900. } while (len);
  1901. handle->offset = offset;
  1902. /*
  1903. * Check we didn't copy past our reservation window, taking the
  1904. * possible unsigned int wrap into account.
  1905. */
  1906. WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
  1907. }
  1908. #define perf_output_put(handle, x) \
  1909. perf_output_copy((handle), &(x), sizeof(x))
  1910. static void perf_output_end(struct perf_output_handle *handle)
  1911. {
  1912. struct perf_counter *counter = handle->counter;
  1913. struct perf_mmap_data *data = handle->data;
  1914. int wakeup_events = counter->hw_event.wakeup_events;
  1915. if (handle->overflow && wakeup_events) {
  1916. int events = atomic_inc_return(&data->events);
  1917. if (events >= wakeup_events) {
  1918. atomic_sub(wakeup_events, &data->events);
  1919. atomic_set(&data->wakeup, 1);
  1920. }
  1921. }
  1922. perf_output_unlock(handle);
  1923. rcu_read_unlock();
  1924. }
  1925. static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
  1926. {
  1927. /*
  1928. * only top level counters have the pid namespace they were created in
  1929. */
  1930. if (counter->parent)
  1931. counter = counter->parent;
  1932. return task_tgid_nr_ns(p, counter->ns);
  1933. }
  1934. static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
  1935. {
  1936. /*
  1937. * only top level counters have the pid namespace they were created in
  1938. */
  1939. if (counter->parent)
  1940. counter = counter->parent;
  1941. return task_pid_nr_ns(p, counter->ns);
  1942. }
  1943. static void perf_counter_output(struct perf_counter *counter,
  1944. int nmi, struct pt_regs *regs, u64 addr)
  1945. {
  1946. int ret;
  1947. u64 sample_type = counter->hw_event.sample_type;
  1948. struct perf_output_handle handle;
  1949. struct perf_event_header header;
  1950. u64 ip;
  1951. struct {
  1952. u32 pid, tid;
  1953. } tid_entry;
  1954. struct {
  1955. u64 id;
  1956. u64 counter;
  1957. } group_entry;
  1958. struct perf_callchain_entry *callchain = NULL;
  1959. int callchain_size = 0;
  1960. u64 time;
  1961. struct {
  1962. u32 cpu, reserved;
  1963. } cpu_entry;
  1964. header.type = 0;
  1965. header.size = sizeof(header);
  1966. header.misc = PERF_EVENT_MISC_OVERFLOW;
  1967. header.misc |= perf_misc_flags(regs);
  1968. if (sample_type & PERF_SAMPLE_IP) {
  1969. ip = perf_instruction_pointer(regs);
  1970. header.type |= PERF_SAMPLE_IP;
  1971. header.size += sizeof(ip);
  1972. }
  1973. if (sample_type & PERF_SAMPLE_TID) {
  1974. /* namespace issues */
  1975. tid_entry.pid = perf_counter_pid(counter, current);
  1976. tid_entry.tid = perf_counter_tid(counter, current);
  1977. header.type |= PERF_SAMPLE_TID;
  1978. header.size += sizeof(tid_entry);
  1979. }
  1980. if (sample_type & PERF_SAMPLE_TIME) {
  1981. /*
  1982. * Maybe do better on x86 and provide cpu_clock_nmi()
  1983. */
  1984. time = sched_clock();
  1985. header.type |= PERF_SAMPLE_TIME;
  1986. header.size += sizeof(u64);
  1987. }
  1988. if (sample_type & PERF_SAMPLE_ADDR) {
  1989. header.type |= PERF_SAMPLE_ADDR;
  1990. header.size += sizeof(u64);
  1991. }
  1992. if (sample_type & PERF_SAMPLE_CONFIG) {
  1993. header.type |= PERF_SAMPLE_CONFIG;
  1994. header.size += sizeof(u64);
  1995. }
  1996. if (sample_type & PERF_SAMPLE_CPU) {
  1997. header.type |= PERF_SAMPLE_CPU;
  1998. header.size += sizeof(cpu_entry);
  1999. cpu_entry.cpu = raw_smp_processor_id();
  2000. }
  2001. if (sample_type & PERF_SAMPLE_GROUP) {
  2002. header.type |= PERF_SAMPLE_GROUP;
  2003. header.size += sizeof(u64) +
  2004. counter->nr_siblings * sizeof(group_entry);
  2005. }
  2006. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  2007. callchain = perf_callchain(regs);
  2008. if (callchain) {
  2009. callchain_size = (1 + callchain->nr) * sizeof(u64);
  2010. header.type |= PERF_SAMPLE_CALLCHAIN;
  2011. header.size += callchain_size;
  2012. }
  2013. }
  2014. ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
  2015. if (ret)
  2016. return;
  2017. perf_output_put(&handle, header);
  2018. if (sample_type & PERF_SAMPLE_IP)
  2019. perf_output_put(&handle, ip);
  2020. if (sample_type & PERF_SAMPLE_TID)
  2021. perf_output_put(&handle, tid_entry);
  2022. if (sample_type & PERF_SAMPLE_TIME)
  2023. perf_output_put(&handle, time);
  2024. if (sample_type & PERF_SAMPLE_ADDR)
  2025. perf_output_put(&handle, addr);
  2026. if (sample_type & PERF_SAMPLE_CONFIG)
  2027. perf_output_put(&handle, counter->hw_event.config);
  2028. if (sample_type & PERF_SAMPLE_CPU)
  2029. perf_output_put(&handle, cpu_entry);
  2030. /*
  2031. * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
  2032. */
  2033. if (sample_type & PERF_SAMPLE_GROUP) {
  2034. struct perf_counter *leader, *sub;
  2035. u64 nr = counter->nr_siblings;
  2036. perf_output_put(&handle, nr);
  2037. leader = counter->group_leader;
  2038. list_for_each_entry(sub, &leader->sibling_list, list_entry) {
  2039. if (sub != counter)
  2040. sub->pmu->read(sub);
  2041. group_entry.id = sub->id;
  2042. group_entry.counter = atomic64_read(&sub->count);
  2043. perf_output_put(&handle, group_entry);
  2044. }
  2045. }
  2046. if (callchain)
  2047. perf_output_copy(&handle, callchain, callchain_size);
  2048. perf_output_end(&handle);
  2049. }
  2050. /*
  2051. * comm tracking
  2052. */
  2053. struct perf_comm_event {
  2054. struct task_struct *task;
  2055. char *comm;
  2056. int comm_size;
  2057. struct {
  2058. struct perf_event_header header;
  2059. u32 pid;
  2060. u32 tid;
  2061. } event;
  2062. };
  2063. static void perf_counter_comm_output(struct perf_counter *counter,
  2064. struct perf_comm_event *comm_event)
  2065. {
  2066. struct perf_output_handle handle;
  2067. int size = comm_event->event.header.size;
  2068. int ret = perf_output_begin(&handle, counter, size, 0, 0);
  2069. if (ret)
  2070. return;
  2071. comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
  2072. comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
  2073. perf_output_put(&handle, comm_event->event);
  2074. perf_output_copy(&handle, comm_event->comm,
  2075. comm_event->comm_size);
  2076. perf_output_end(&handle);
  2077. }
  2078. static int perf_counter_comm_match(struct perf_counter *counter,
  2079. struct perf_comm_event *comm_event)
  2080. {
  2081. if (counter->hw_event.comm &&
  2082. comm_event->event.header.type == PERF_EVENT_COMM)
  2083. return 1;
  2084. return 0;
  2085. }
  2086. static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
  2087. struct perf_comm_event *comm_event)
  2088. {
  2089. struct perf_counter *counter;
  2090. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  2091. return;
  2092. rcu_read_lock();
  2093. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  2094. if (perf_counter_comm_match(counter, comm_event))
  2095. perf_counter_comm_output(counter, comm_event);
  2096. }
  2097. rcu_read_unlock();
  2098. }
  2099. static void perf_counter_comm_event(struct perf_comm_event *comm_event)
  2100. {
  2101. struct perf_cpu_context *cpuctx;
  2102. struct perf_counter_context *ctx;
  2103. unsigned int size;
  2104. char *comm = comm_event->task->comm;
  2105. size = ALIGN(strlen(comm)+1, sizeof(u64));
  2106. comm_event->comm = comm;
  2107. comm_event->comm_size = size;
  2108. comm_event->event.header.size = sizeof(comm_event->event) + size;
  2109. cpuctx = &get_cpu_var(perf_cpu_context);
  2110. perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
  2111. put_cpu_var(perf_cpu_context);
  2112. rcu_read_lock();
  2113. /*
  2114. * doesn't really matter which of the child contexts the
  2115. * events ends up in.
  2116. */
  2117. ctx = rcu_dereference(current->perf_counter_ctxp);
  2118. if (ctx)
  2119. perf_counter_comm_ctx(ctx, comm_event);
  2120. rcu_read_unlock();
  2121. }
  2122. void perf_counter_comm(struct task_struct *task)
  2123. {
  2124. struct perf_comm_event comm_event;
  2125. if (!atomic_read(&nr_comm_tracking))
  2126. return;
  2127. comm_event = (struct perf_comm_event){
  2128. .task = task,
  2129. .event = {
  2130. .header = { .type = PERF_EVENT_COMM, },
  2131. },
  2132. };
  2133. perf_counter_comm_event(&comm_event);
  2134. }
  2135. /*
  2136. * mmap tracking
  2137. */
  2138. struct perf_mmap_event {
  2139. struct file *file;
  2140. char *file_name;
  2141. int file_size;
  2142. struct {
  2143. struct perf_event_header header;
  2144. u32 pid;
  2145. u32 tid;
  2146. u64 start;
  2147. u64 len;
  2148. u64 pgoff;
  2149. } event;
  2150. };
  2151. static void perf_counter_mmap_output(struct perf_counter *counter,
  2152. struct perf_mmap_event *mmap_event)
  2153. {
  2154. struct perf_output_handle handle;
  2155. int size = mmap_event->event.header.size;
  2156. int ret = perf_output_begin(&handle, counter, size, 0, 0);
  2157. if (ret)
  2158. return;
  2159. mmap_event->event.pid = perf_counter_pid(counter, current);
  2160. mmap_event->event.tid = perf_counter_tid(counter, current);
  2161. perf_output_put(&handle, mmap_event->event);
  2162. perf_output_copy(&handle, mmap_event->file_name,
  2163. mmap_event->file_size);
  2164. perf_output_end(&handle);
  2165. }
  2166. static int perf_counter_mmap_match(struct perf_counter *counter,
  2167. struct perf_mmap_event *mmap_event)
  2168. {
  2169. if (counter->hw_event.mmap &&
  2170. mmap_event->event.header.type == PERF_EVENT_MMAP)
  2171. return 1;
  2172. if (counter->hw_event.munmap &&
  2173. mmap_event->event.header.type == PERF_EVENT_MUNMAP)
  2174. return 1;
  2175. return 0;
  2176. }
  2177. static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
  2178. struct perf_mmap_event *mmap_event)
  2179. {
  2180. struct perf_counter *counter;
  2181. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  2182. return;
  2183. rcu_read_lock();
  2184. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  2185. if (perf_counter_mmap_match(counter, mmap_event))
  2186. perf_counter_mmap_output(counter, mmap_event);
  2187. }
  2188. rcu_read_unlock();
  2189. }
  2190. static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
  2191. {
  2192. struct perf_cpu_context *cpuctx;
  2193. struct perf_counter_context *ctx;
  2194. struct file *file = mmap_event->file;
  2195. unsigned int size;
  2196. char tmp[16];
  2197. char *buf = NULL;
  2198. char *name;
  2199. if (file) {
  2200. buf = kzalloc(PATH_MAX, GFP_KERNEL);
  2201. if (!buf) {
  2202. name = strncpy(tmp, "//enomem", sizeof(tmp));
  2203. goto got_name;
  2204. }
  2205. name = d_path(&file->f_path, buf, PATH_MAX);
  2206. if (IS_ERR(name)) {
  2207. name = strncpy(tmp, "//toolong", sizeof(tmp));
  2208. goto got_name;
  2209. }
  2210. } else {
  2211. name = strncpy(tmp, "//anon", sizeof(tmp));
  2212. goto got_name;
  2213. }
  2214. got_name:
  2215. size = ALIGN(strlen(name)+1, sizeof(u64));
  2216. mmap_event->file_name = name;
  2217. mmap_event->file_size = size;
  2218. mmap_event->event.header.size = sizeof(mmap_event->event) + size;
  2219. cpuctx = &get_cpu_var(perf_cpu_context);
  2220. perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
  2221. put_cpu_var(perf_cpu_context);
  2222. rcu_read_lock();
  2223. /*
  2224. * doesn't really matter which of the child contexts the
  2225. * events ends up in.
  2226. */
  2227. ctx = rcu_dereference(current->perf_counter_ctxp);
  2228. if (ctx)
  2229. perf_counter_mmap_ctx(ctx, mmap_event);
  2230. rcu_read_unlock();
  2231. kfree(buf);
  2232. }
  2233. void perf_counter_mmap(unsigned long addr, unsigned long len,
  2234. unsigned long pgoff, struct file *file)
  2235. {
  2236. struct perf_mmap_event mmap_event;
  2237. if (!atomic_read(&nr_mmap_tracking))
  2238. return;
  2239. mmap_event = (struct perf_mmap_event){
  2240. .file = file,
  2241. .event = {
  2242. .header = { .type = PERF_EVENT_MMAP, },
  2243. .start = addr,
  2244. .len = len,
  2245. .pgoff = pgoff,
  2246. },
  2247. };
  2248. perf_counter_mmap_event(&mmap_event);
  2249. }
  2250. void perf_counter_munmap(unsigned long addr, unsigned long len,
  2251. unsigned long pgoff, struct file *file)
  2252. {
  2253. struct perf_mmap_event mmap_event;
  2254. if (!atomic_read(&nr_munmap_tracking))
  2255. return;
  2256. mmap_event = (struct perf_mmap_event){
  2257. .file = file,
  2258. .event = {
  2259. .header = { .type = PERF_EVENT_MUNMAP, },
  2260. .start = addr,
  2261. .len = len,
  2262. .pgoff = pgoff,
  2263. },
  2264. };
  2265. perf_counter_mmap_event(&mmap_event);
  2266. }
  2267. /*
  2268. * Log sample_period changes so that analyzing tools can re-normalize the
  2269. * event flow.
  2270. */
  2271. static void perf_log_period(struct perf_counter *counter, u64 period)
  2272. {
  2273. struct perf_output_handle handle;
  2274. int ret;
  2275. struct {
  2276. struct perf_event_header header;
  2277. u64 time;
  2278. u64 period;
  2279. } freq_event = {
  2280. .header = {
  2281. .type = PERF_EVENT_PERIOD,
  2282. .misc = 0,
  2283. .size = sizeof(freq_event),
  2284. },
  2285. .time = sched_clock(),
  2286. .period = period,
  2287. };
  2288. if (counter->hw.sample_period == period)
  2289. return;
  2290. ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
  2291. if (ret)
  2292. return;
  2293. perf_output_put(&handle, freq_event);
  2294. perf_output_end(&handle);
  2295. }
  2296. /*
  2297. * IRQ throttle logging
  2298. */
  2299. static void perf_log_throttle(struct perf_counter *counter, int enable)
  2300. {
  2301. struct perf_output_handle handle;
  2302. int ret;
  2303. struct {
  2304. struct perf_event_header header;
  2305. u64 time;
  2306. } throttle_event = {
  2307. .header = {
  2308. .type = PERF_EVENT_THROTTLE + 1,
  2309. .misc = 0,
  2310. .size = sizeof(throttle_event),
  2311. },
  2312. .time = sched_clock(),
  2313. };
  2314. ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
  2315. if (ret)
  2316. return;
  2317. perf_output_put(&handle, throttle_event);
  2318. perf_output_end(&handle);
  2319. }
  2320. /*
  2321. * Generic counter overflow handling.
  2322. */
  2323. int perf_counter_overflow(struct perf_counter *counter,
  2324. int nmi, struct pt_regs *regs, u64 addr)
  2325. {
  2326. int events = atomic_read(&counter->event_limit);
  2327. int throttle = counter->pmu->unthrottle != NULL;
  2328. int ret = 0;
  2329. if (!throttle) {
  2330. counter->hw.interrupts++;
  2331. } else if (counter->hw.interrupts != MAX_INTERRUPTS) {
  2332. counter->hw.interrupts++;
  2333. if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
  2334. counter->hw.interrupts = MAX_INTERRUPTS;
  2335. perf_log_throttle(counter, 0);
  2336. ret = 1;
  2337. }
  2338. }
  2339. /*
  2340. * XXX event_limit might not quite work as expected on inherited
  2341. * counters
  2342. */
  2343. counter->pending_kill = POLL_IN;
  2344. if (events && atomic_dec_and_test(&counter->event_limit)) {
  2345. ret = 1;
  2346. counter->pending_kill = POLL_HUP;
  2347. if (nmi) {
  2348. counter->pending_disable = 1;
  2349. perf_pending_queue(&counter->pending,
  2350. perf_pending_counter);
  2351. } else
  2352. perf_counter_disable(counter);
  2353. }
  2354. perf_counter_output(counter, nmi, regs, addr);
  2355. return ret;
  2356. }
  2357. /*
  2358. * Generic software counter infrastructure
  2359. */
  2360. static void perf_swcounter_update(struct perf_counter *counter)
  2361. {
  2362. struct hw_perf_counter *hwc = &counter->hw;
  2363. u64 prev, now;
  2364. s64 delta;
  2365. again:
  2366. prev = atomic64_read(&hwc->prev_count);
  2367. now = atomic64_read(&hwc->count);
  2368. if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
  2369. goto again;
  2370. delta = now - prev;
  2371. atomic64_add(delta, &counter->count);
  2372. atomic64_sub(delta, &hwc->period_left);
  2373. }
  2374. static void perf_swcounter_set_period(struct perf_counter *counter)
  2375. {
  2376. struct hw_perf_counter *hwc = &counter->hw;
  2377. s64 left = atomic64_read(&hwc->period_left);
  2378. s64 period = hwc->sample_period;
  2379. if (unlikely(left <= -period)) {
  2380. left = period;
  2381. atomic64_set(&hwc->period_left, left);
  2382. }
  2383. if (unlikely(left <= 0)) {
  2384. left += period;
  2385. atomic64_add(period, &hwc->period_left);
  2386. }
  2387. atomic64_set(&hwc->prev_count, -left);
  2388. atomic64_set(&hwc->count, -left);
  2389. }
  2390. static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
  2391. {
  2392. enum hrtimer_restart ret = HRTIMER_RESTART;
  2393. struct perf_counter *counter;
  2394. struct pt_regs *regs;
  2395. u64 period;
  2396. counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
  2397. counter->pmu->read(counter);
  2398. regs = get_irq_regs();
  2399. /*
  2400. * In case we exclude kernel IPs or are somehow not in interrupt
  2401. * context, provide the next best thing, the user IP.
  2402. */
  2403. if ((counter->hw_event.exclude_kernel || !regs) &&
  2404. !counter->hw_event.exclude_user)
  2405. regs = task_pt_regs(current);
  2406. if (regs) {
  2407. if (perf_counter_overflow(counter, 0, regs, 0))
  2408. ret = HRTIMER_NORESTART;
  2409. }
  2410. period = max_t(u64, 10000, counter->hw.sample_period);
  2411. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  2412. return ret;
  2413. }
  2414. static void perf_swcounter_overflow(struct perf_counter *counter,
  2415. int nmi, struct pt_regs *regs, u64 addr)
  2416. {
  2417. perf_swcounter_update(counter);
  2418. perf_swcounter_set_period(counter);
  2419. if (perf_counter_overflow(counter, nmi, regs, addr))
  2420. /* soft-disable the counter */
  2421. ;
  2422. }
  2423. static int perf_swcounter_is_counting(struct perf_counter *counter)
  2424. {
  2425. struct perf_counter_context *ctx;
  2426. unsigned long flags;
  2427. int count;
  2428. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  2429. return 1;
  2430. if (counter->state != PERF_COUNTER_STATE_INACTIVE)
  2431. return 0;
  2432. /*
  2433. * If the counter is inactive, it could be just because
  2434. * its task is scheduled out, or because it's in a group
  2435. * which could not go on the PMU. We want to count in
  2436. * the first case but not the second. If the context is
  2437. * currently active then an inactive software counter must
  2438. * be the second case. If it's not currently active then
  2439. * we need to know whether the counter was active when the
  2440. * context was last active, which we can determine by
  2441. * comparing counter->tstamp_stopped with ctx->time.
  2442. *
  2443. * We are within an RCU read-side critical section,
  2444. * which protects the existence of *ctx.
  2445. */
  2446. ctx = counter->ctx;
  2447. spin_lock_irqsave(&ctx->lock, flags);
  2448. count = 1;
  2449. /* Re-check state now we have the lock */
  2450. if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
  2451. counter->ctx->is_active ||
  2452. counter->tstamp_stopped < ctx->time)
  2453. count = 0;
  2454. spin_unlock_irqrestore(&ctx->lock, flags);
  2455. return count;
  2456. }
  2457. static int perf_swcounter_match(struct perf_counter *counter,
  2458. enum perf_event_types type,
  2459. u32 event, struct pt_regs *regs)
  2460. {
  2461. u64 event_config;
  2462. event_config = ((u64) type << PERF_COUNTER_TYPE_SHIFT) | event;
  2463. if (!perf_swcounter_is_counting(counter))
  2464. return 0;
  2465. if (counter->hw_event.config != event_config)
  2466. return 0;
  2467. if (regs) {
  2468. if (counter->hw_event.exclude_user && user_mode(regs))
  2469. return 0;
  2470. if (counter->hw_event.exclude_kernel && !user_mode(regs))
  2471. return 0;
  2472. }
  2473. return 1;
  2474. }
  2475. static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
  2476. int nmi, struct pt_regs *regs, u64 addr)
  2477. {
  2478. int neg = atomic64_add_negative(nr, &counter->hw.count);
  2479. if (counter->hw.sample_period && !neg && regs)
  2480. perf_swcounter_overflow(counter, nmi, regs, addr);
  2481. }
  2482. static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
  2483. enum perf_event_types type, u32 event,
  2484. u64 nr, int nmi, struct pt_regs *regs,
  2485. u64 addr)
  2486. {
  2487. struct perf_counter *counter;
  2488. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  2489. return;
  2490. rcu_read_lock();
  2491. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  2492. if (perf_swcounter_match(counter, type, event, regs))
  2493. perf_swcounter_add(counter, nr, nmi, regs, addr);
  2494. }
  2495. rcu_read_unlock();
  2496. }
  2497. static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
  2498. {
  2499. if (in_nmi())
  2500. return &cpuctx->recursion[3];
  2501. if (in_irq())
  2502. return &cpuctx->recursion[2];
  2503. if (in_softirq())
  2504. return &cpuctx->recursion[1];
  2505. return &cpuctx->recursion[0];
  2506. }
  2507. static void __perf_swcounter_event(enum perf_event_types type, u32 event,
  2508. u64 nr, int nmi, struct pt_regs *regs,
  2509. u64 addr)
  2510. {
  2511. struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
  2512. int *recursion = perf_swcounter_recursion_context(cpuctx);
  2513. struct perf_counter_context *ctx;
  2514. if (*recursion)
  2515. goto out;
  2516. (*recursion)++;
  2517. barrier();
  2518. perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
  2519. nr, nmi, regs, addr);
  2520. rcu_read_lock();
  2521. /*
  2522. * doesn't really matter which of the child contexts the
  2523. * events ends up in.
  2524. */
  2525. ctx = rcu_dereference(current->perf_counter_ctxp);
  2526. if (ctx)
  2527. perf_swcounter_ctx_event(ctx, type, event, nr, nmi, regs, addr);
  2528. rcu_read_unlock();
  2529. barrier();
  2530. (*recursion)--;
  2531. out:
  2532. put_cpu_var(perf_cpu_context);
  2533. }
  2534. void
  2535. perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
  2536. {
  2537. __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
  2538. }
  2539. static void perf_swcounter_read(struct perf_counter *counter)
  2540. {
  2541. perf_swcounter_update(counter);
  2542. }
  2543. static int perf_swcounter_enable(struct perf_counter *counter)
  2544. {
  2545. perf_swcounter_set_period(counter);
  2546. return 0;
  2547. }
  2548. static void perf_swcounter_disable(struct perf_counter *counter)
  2549. {
  2550. perf_swcounter_update(counter);
  2551. }
  2552. static const struct pmu perf_ops_generic = {
  2553. .enable = perf_swcounter_enable,
  2554. .disable = perf_swcounter_disable,
  2555. .read = perf_swcounter_read,
  2556. };
  2557. /*
  2558. * Software counter: cpu wall time clock
  2559. */
  2560. static void cpu_clock_perf_counter_update(struct perf_counter *counter)
  2561. {
  2562. int cpu = raw_smp_processor_id();
  2563. s64 prev;
  2564. u64 now;
  2565. now = cpu_clock(cpu);
  2566. prev = atomic64_read(&counter->hw.prev_count);
  2567. atomic64_set(&counter->hw.prev_count, now);
  2568. atomic64_add(now - prev, &counter->count);
  2569. }
  2570. static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
  2571. {
  2572. struct hw_perf_counter *hwc = &counter->hw;
  2573. int cpu = raw_smp_processor_id();
  2574. atomic64_set(&hwc->prev_count, cpu_clock(cpu));
  2575. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2576. hwc->hrtimer.function = perf_swcounter_hrtimer;
  2577. if (hwc->sample_period) {
  2578. u64 period = max_t(u64, 10000, hwc->sample_period);
  2579. __hrtimer_start_range_ns(&hwc->hrtimer,
  2580. ns_to_ktime(period), 0,
  2581. HRTIMER_MODE_REL, 0);
  2582. }
  2583. return 0;
  2584. }
  2585. static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
  2586. {
  2587. if (counter->hw.sample_period)
  2588. hrtimer_cancel(&counter->hw.hrtimer);
  2589. cpu_clock_perf_counter_update(counter);
  2590. }
  2591. static void cpu_clock_perf_counter_read(struct perf_counter *counter)
  2592. {
  2593. cpu_clock_perf_counter_update(counter);
  2594. }
  2595. static const struct pmu perf_ops_cpu_clock = {
  2596. .enable = cpu_clock_perf_counter_enable,
  2597. .disable = cpu_clock_perf_counter_disable,
  2598. .read = cpu_clock_perf_counter_read,
  2599. };
  2600. /*
  2601. * Software counter: task time clock
  2602. */
  2603. static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
  2604. {
  2605. u64 prev;
  2606. s64 delta;
  2607. prev = atomic64_xchg(&counter->hw.prev_count, now);
  2608. delta = now - prev;
  2609. atomic64_add(delta, &counter->count);
  2610. }
  2611. static int task_clock_perf_counter_enable(struct perf_counter *counter)
  2612. {
  2613. struct hw_perf_counter *hwc = &counter->hw;
  2614. u64 now;
  2615. now = counter->ctx->time;
  2616. atomic64_set(&hwc->prev_count, now);
  2617. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2618. hwc->hrtimer.function = perf_swcounter_hrtimer;
  2619. if (hwc->sample_period) {
  2620. u64 period = max_t(u64, 10000, hwc->sample_period);
  2621. __hrtimer_start_range_ns(&hwc->hrtimer,
  2622. ns_to_ktime(period), 0,
  2623. HRTIMER_MODE_REL, 0);
  2624. }
  2625. return 0;
  2626. }
  2627. static void task_clock_perf_counter_disable(struct perf_counter *counter)
  2628. {
  2629. if (counter->hw.sample_period)
  2630. hrtimer_cancel(&counter->hw.hrtimer);
  2631. task_clock_perf_counter_update(counter, counter->ctx->time);
  2632. }
  2633. static void task_clock_perf_counter_read(struct perf_counter *counter)
  2634. {
  2635. u64 time;
  2636. if (!in_nmi()) {
  2637. update_context_time(counter->ctx);
  2638. time = counter->ctx->time;
  2639. } else {
  2640. u64 now = perf_clock();
  2641. u64 delta = now - counter->ctx->timestamp;
  2642. time = counter->ctx->time + delta;
  2643. }
  2644. task_clock_perf_counter_update(counter, time);
  2645. }
  2646. static const struct pmu perf_ops_task_clock = {
  2647. .enable = task_clock_perf_counter_enable,
  2648. .disable = task_clock_perf_counter_disable,
  2649. .read = task_clock_perf_counter_read,
  2650. };
  2651. /*
  2652. * Software counter: cpu migrations
  2653. */
  2654. void perf_counter_task_migration(struct task_struct *task, int cpu)
  2655. {
  2656. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  2657. struct perf_counter_context *ctx;
  2658. perf_swcounter_ctx_event(&cpuctx->ctx, PERF_TYPE_SOFTWARE,
  2659. PERF_COUNT_CPU_MIGRATIONS,
  2660. 1, 1, NULL, 0);
  2661. ctx = perf_pin_task_context(task);
  2662. if (ctx) {
  2663. perf_swcounter_ctx_event(ctx, PERF_TYPE_SOFTWARE,
  2664. PERF_COUNT_CPU_MIGRATIONS,
  2665. 1, 1, NULL, 0);
  2666. perf_unpin_context(ctx);
  2667. }
  2668. }
  2669. #ifdef CONFIG_EVENT_PROFILE
  2670. void perf_tpcounter_event(int event_id)
  2671. {
  2672. struct pt_regs *regs = get_irq_regs();
  2673. if (!regs)
  2674. regs = task_pt_regs(current);
  2675. __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
  2676. }
  2677. EXPORT_SYMBOL_GPL(perf_tpcounter_event);
  2678. extern int ftrace_profile_enable(int);
  2679. extern void ftrace_profile_disable(int);
  2680. static void tp_perf_counter_destroy(struct perf_counter *counter)
  2681. {
  2682. ftrace_profile_disable(perf_event_id(&counter->hw_event));
  2683. }
  2684. static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
  2685. {
  2686. int event_id = perf_event_id(&counter->hw_event);
  2687. int ret;
  2688. ret = ftrace_profile_enable(event_id);
  2689. if (ret)
  2690. return NULL;
  2691. counter->destroy = tp_perf_counter_destroy;
  2692. counter->hw.sample_period = counter->hw_event.sample_period;
  2693. return &perf_ops_generic;
  2694. }
  2695. #else
  2696. static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
  2697. {
  2698. return NULL;
  2699. }
  2700. #endif
  2701. static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
  2702. {
  2703. const struct pmu *pmu = NULL;
  2704. /*
  2705. * Software counters (currently) can't in general distinguish
  2706. * between user, kernel and hypervisor events.
  2707. * However, context switches and cpu migrations are considered
  2708. * to be kernel events, and page faults are never hypervisor
  2709. * events.
  2710. */
  2711. switch (perf_event_id(&counter->hw_event)) {
  2712. case PERF_COUNT_CPU_CLOCK:
  2713. pmu = &perf_ops_cpu_clock;
  2714. break;
  2715. case PERF_COUNT_TASK_CLOCK:
  2716. /*
  2717. * If the user instantiates this as a per-cpu counter,
  2718. * use the cpu_clock counter instead.
  2719. */
  2720. if (counter->ctx->task)
  2721. pmu = &perf_ops_task_clock;
  2722. else
  2723. pmu = &perf_ops_cpu_clock;
  2724. break;
  2725. case PERF_COUNT_PAGE_FAULTS:
  2726. case PERF_COUNT_PAGE_FAULTS_MIN:
  2727. case PERF_COUNT_PAGE_FAULTS_MAJ:
  2728. case PERF_COUNT_CONTEXT_SWITCHES:
  2729. case PERF_COUNT_CPU_MIGRATIONS:
  2730. pmu = &perf_ops_generic;
  2731. break;
  2732. }
  2733. return pmu;
  2734. }
  2735. /*
  2736. * Allocate and initialize a counter structure
  2737. */
  2738. static struct perf_counter *
  2739. perf_counter_alloc(struct perf_counter_hw_event *hw_event,
  2740. int cpu,
  2741. struct perf_counter_context *ctx,
  2742. struct perf_counter *group_leader,
  2743. gfp_t gfpflags)
  2744. {
  2745. const struct pmu *pmu;
  2746. struct perf_counter *counter;
  2747. struct hw_perf_counter *hwc;
  2748. long err;
  2749. counter = kzalloc(sizeof(*counter), gfpflags);
  2750. if (!counter)
  2751. return ERR_PTR(-ENOMEM);
  2752. /*
  2753. * Single counters are their own group leaders, with an
  2754. * empty sibling list:
  2755. */
  2756. if (!group_leader)
  2757. group_leader = counter;
  2758. mutex_init(&counter->child_mutex);
  2759. INIT_LIST_HEAD(&counter->child_list);
  2760. INIT_LIST_HEAD(&counter->list_entry);
  2761. INIT_LIST_HEAD(&counter->event_entry);
  2762. INIT_LIST_HEAD(&counter->sibling_list);
  2763. init_waitqueue_head(&counter->waitq);
  2764. mutex_init(&counter->mmap_mutex);
  2765. counter->cpu = cpu;
  2766. counter->hw_event = *hw_event;
  2767. counter->group_leader = group_leader;
  2768. counter->pmu = NULL;
  2769. counter->ctx = ctx;
  2770. counter->oncpu = -1;
  2771. counter->state = PERF_COUNTER_STATE_INACTIVE;
  2772. if (hw_event->disabled)
  2773. counter->state = PERF_COUNTER_STATE_OFF;
  2774. pmu = NULL;
  2775. hwc = &counter->hw;
  2776. if (hw_event->freq && hw_event->sample_freq)
  2777. hwc->sample_period = div64_u64(TICK_NSEC, hw_event->sample_freq);
  2778. else
  2779. hwc->sample_period = hw_event->sample_period;
  2780. /*
  2781. * we currently do not support PERF_SAMPLE_GROUP on inherited counters
  2782. */
  2783. if (hw_event->inherit && (hw_event->sample_type & PERF_SAMPLE_GROUP))
  2784. goto done;
  2785. if (perf_event_raw(hw_event)) {
  2786. pmu = hw_perf_counter_init(counter);
  2787. goto done;
  2788. }
  2789. switch (perf_event_type(hw_event)) {
  2790. case PERF_TYPE_HARDWARE:
  2791. pmu = hw_perf_counter_init(counter);
  2792. break;
  2793. case PERF_TYPE_SOFTWARE:
  2794. pmu = sw_perf_counter_init(counter);
  2795. break;
  2796. case PERF_TYPE_TRACEPOINT:
  2797. pmu = tp_perf_counter_init(counter);
  2798. break;
  2799. }
  2800. done:
  2801. err = 0;
  2802. if (!pmu)
  2803. err = -EINVAL;
  2804. else if (IS_ERR(pmu))
  2805. err = PTR_ERR(pmu);
  2806. if (err) {
  2807. kfree(counter);
  2808. return ERR_PTR(err);
  2809. }
  2810. counter->pmu = pmu;
  2811. atomic_inc(&nr_counters);
  2812. if (counter->hw_event.mmap)
  2813. atomic_inc(&nr_mmap_tracking);
  2814. if (counter->hw_event.munmap)
  2815. atomic_inc(&nr_munmap_tracking);
  2816. if (counter->hw_event.comm)
  2817. atomic_inc(&nr_comm_tracking);
  2818. return counter;
  2819. }
  2820. static atomic64_t perf_counter_id;
  2821. /**
  2822. * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
  2823. *
  2824. * @hw_event_uptr: event type attributes for monitoring/sampling
  2825. * @pid: target pid
  2826. * @cpu: target cpu
  2827. * @group_fd: group leader counter fd
  2828. */
  2829. SYSCALL_DEFINE5(perf_counter_open,
  2830. const struct perf_counter_hw_event __user *, hw_event_uptr,
  2831. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  2832. {
  2833. struct perf_counter *counter, *group_leader;
  2834. struct perf_counter_hw_event hw_event;
  2835. struct perf_counter_context *ctx;
  2836. struct file *counter_file = NULL;
  2837. struct file *group_file = NULL;
  2838. int fput_needed = 0;
  2839. int fput_needed2 = 0;
  2840. int ret;
  2841. /* for future expandability... */
  2842. if (flags)
  2843. return -EINVAL;
  2844. if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
  2845. return -EFAULT;
  2846. /*
  2847. * Get the target context (task or percpu):
  2848. */
  2849. ctx = find_get_context(pid, cpu);
  2850. if (IS_ERR(ctx))
  2851. return PTR_ERR(ctx);
  2852. /*
  2853. * Look up the group leader (we will attach this counter to it):
  2854. */
  2855. group_leader = NULL;
  2856. if (group_fd != -1) {
  2857. ret = -EINVAL;
  2858. group_file = fget_light(group_fd, &fput_needed);
  2859. if (!group_file)
  2860. goto err_put_context;
  2861. if (group_file->f_op != &perf_fops)
  2862. goto err_put_context;
  2863. group_leader = group_file->private_data;
  2864. /*
  2865. * Do not allow a recursive hierarchy (this new sibling
  2866. * becoming part of another group-sibling):
  2867. */
  2868. if (group_leader->group_leader != group_leader)
  2869. goto err_put_context;
  2870. /*
  2871. * Do not allow to attach to a group in a different
  2872. * task or CPU context:
  2873. */
  2874. if (group_leader->ctx != ctx)
  2875. goto err_put_context;
  2876. /*
  2877. * Only a group leader can be exclusive or pinned
  2878. */
  2879. if (hw_event.exclusive || hw_event.pinned)
  2880. goto err_put_context;
  2881. }
  2882. counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
  2883. GFP_KERNEL);
  2884. ret = PTR_ERR(counter);
  2885. if (IS_ERR(counter))
  2886. goto err_put_context;
  2887. ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
  2888. if (ret < 0)
  2889. goto err_free_put_context;
  2890. counter_file = fget_light(ret, &fput_needed2);
  2891. if (!counter_file)
  2892. goto err_free_put_context;
  2893. counter->filp = counter_file;
  2894. WARN_ON_ONCE(ctx->parent_ctx);
  2895. mutex_lock(&ctx->mutex);
  2896. perf_install_in_context(ctx, counter, cpu);
  2897. ++ctx->generation;
  2898. mutex_unlock(&ctx->mutex);
  2899. counter->owner = current;
  2900. get_task_struct(current);
  2901. mutex_lock(&current->perf_counter_mutex);
  2902. list_add_tail(&counter->owner_entry, &current->perf_counter_list);
  2903. mutex_unlock(&current->perf_counter_mutex);
  2904. counter->ns = get_pid_ns(current->nsproxy->pid_ns);
  2905. counter->id = atomic64_inc_return(&perf_counter_id);
  2906. fput_light(counter_file, fput_needed2);
  2907. out_fput:
  2908. fput_light(group_file, fput_needed);
  2909. return ret;
  2910. err_free_put_context:
  2911. kfree(counter);
  2912. err_put_context:
  2913. put_ctx(ctx);
  2914. goto out_fput;
  2915. }
  2916. /*
  2917. * inherit a counter from parent task to child task:
  2918. */
  2919. static struct perf_counter *
  2920. inherit_counter(struct perf_counter *parent_counter,
  2921. struct task_struct *parent,
  2922. struct perf_counter_context *parent_ctx,
  2923. struct task_struct *child,
  2924. struct perf_counter *group_leader,
  2925. struct perf_counter_context *child_ctx)
  2926. {
  2927. struct perf_counter *child_counter;
  2928. /*
  2929. * Instead of creating recursive hierarchies of counters,
  2930. * we link inherited counters back to the original parent,
  2931. * which has a filp for sure, which we use as the reference
  2932. * count:
  2933. */
  2934. if (parent_counter->parent)
  2935. parent_counter = parent_counter->parent;
  2936. child_counter = perf_counter_alloc(&parent_counter->hw_event,
  2937. parent_counter->cpu, child_ctx,
  2938. group_leader, GFP_KERNEL);
  2939. if (IS_ERR(child_counter))
  2940. return child_counter;
  2941. get_ctx(child_ctx);
  2942. /*
  2943. * Make the child state follow the state of the parent counter,
  2944. * not its hw_event.disabled bit. We hold the parent's mutex,
  2945. * so we won't race with perf_counter_{en, dis}able_family.
  2946. */
  2947. if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
  2948. child_counter->state = PERF_COUNTER_STATE_INACTIVE;
  2949. else
  2950. child_counter->state = PERF_COUNTER_STATE_OFF;
  2951. /*
  2952. * Link it up in the child's context:
  2953. */
  2954. add_counter_to_ctx(child_counter, child_ctx);
  2955. child_counter->parent = parent_counter;
  2956. /*
  2957. * inherit into child's child as well:
  2958. */
  2959. child_counter->hw_event.inherit = 1;
  2960. /*
  2961. * Get a reference to the parent filp - we will fput it
  2962. * when the child counter exits. This is safe to do because
  2963. * we are in the parent and we know that the filp still
  2964. * exists and has a nonzero count:
  2965. */
  2966. atomic_long_inc(&parent_counter->filp->f_count);
  2967. /*
  2968. * Link this into the parent counter's child list
  2969. */
  2970. WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
  2971. mutex_lock(&parent_counter->child_mutex);
  2972. list_add_tail(&child_counter->child_list, &parent_counter->child_list);
  2973. mutex_unlock(&parent_counter->child_mutex);
  2974. return child_counter;
  2975. }
  2976. static int inherit_group(struct perf_counter *parent_counter,
  2977. struct task_struct *parent,
  2978. struct perf_counter_context *parent_ctx,
  2979. struct task_struct *child,
  2980. struct perf_counter_context *child_ctx)
  2981. {
  2982. struct perf_counter *leader;
  2983. struct perf_counter *sub;
  2984. struct perf_counter *child_ctr;
  2985. leader = inherit_counter(parent_counter, parent, parent_ctx,
  2986. child, NULL, child_ctx);
  2987. if (IS_ERR(leader))
  2988. return PTR_ERR(leader);
  2989. list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
  2990. child_ctr = inherit_counter(sub, parent, parent_ctx,
  2991. child, leader, child_ctx);
  2992. if (IS_ERR(child_ctr))
  2993. return PTR_ERR(child_ctr);
  2994. }
  2995. return 0;
  2996. }
  2997. static void sync_child_counter(struct perf_counter *child_counter,
  2998. struct perf_counter *parent_counter)
  2999. {
  3000. u64 child_val;
  3001. child_val = atomic64_read(&child_counter->count);
  3002. /*
  3003. * Add back the child's count to the parent's count:
  3004. */
  3005. atomic64_add(child_val, &parent_counter->count);
  3006. atomic64_add(child_counter->total_time_enabled,
  3007. &parent_counter->child_total_time_enabled);
  3008. atomic64_add(child_counter->total_time_running,
  3009. &parent_counter->child_total_time_running);
  3010. /*
  3011. * Remove this counter from the parent's list
  3012. */
  3013. WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
  3014. mutex_lock(&parent_counter->child_mutex);
  3015. list_del_init(&child_counter->child_list);
  3016. mutex_unlock(&parent_counter->child_mutex);
  3017. /*
  3018. * Release the parent counter, if this was the last
  3019. * reference to it.
  3020. */
  3021. fput(parent_counter->filp);
  3022. }
  3023. static void
  3024. __perf_counter_exit_task(struct perf_counter *child_counter,
  3025. struct perf_counter_context *child_ctx)
  3026. {
  3027. struct perf_counter *parent_counter;
  3028. update_counter_times(child_counter);
  3029. perf_counter_remove_from_context(child_counter);
  3030. parent_counter = child_counter->parent;
  3031. /*
  3032. * It can happen that parent exits first, and has counters
  3033. * that are still around due to the child reference. These
  3034. * counters need to be zapped - but otherwise linger.
  3035. */
  3036. if (parent_counter) {
  3037. sync_child_counter(child_counter, parent_counter);
  3038. free_counter(child_counter);
  3039. }
  3040. }
  3041. /*
  3042. * When a child task exits, feed back counter values to parent counters.
  3043. */
  3044. void perf_counter_exit_task(struct task_struct *child)
  3045. {
  3046. struct perf_counter *child_counter, *tmp;
  3047. struct perf_counter_context *child_ctx;
  3048. unsigned long flags;
  3049. if (likely(!child->perf_counter_ctxp))
  3050. return;
  3051. local_irq_save(flags);
  3052. /*
  3053. * We can't reschedule here because interrupts are disabled,
  3054. * and either child is current or it is a task that can't be
  3055. * scheduled, so we are now safe from rescheduling changing
  3056. * our context.
  3057. */
  3058. child_ctx = child->perf_counter_ctxp;
  3059. __perf_counter_task_sched_out(child_ctx);
  3060. /*
  3061. * Take the context lock here so that if find_get_context is
  3062. * reading child->perf_counter_ctxp, we wait until it has
  3063. * incremented the context's refcount before we do put_ctx below.
  3064. */
  3065. spin_lock(&child_ctx->lock);
  3066. child->perf_counter_ctxp = NULL;
  3067. if (child_ctx->parent_ctx) {
  3068. /*
  3069. * This context is a clone; unclone it so it can't get
  3070. * swapped to another process while we're removing all
  3071. * the counters from it.
  3072. */
  3073. put_ctx(child_ctx->parent_ctx);
  3074. child_ctx->parent_ctx = NULL;
  3075. }
  3076. spin_unlock(&child_ctx->lock);
  3077. local_irq_restore(flags);
  3078. mutex_lock(&child_ctx->mutex);
  3079. again:
  3080. list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
  3081. list_entry)
  3082. __perf_counter_exit_task(child_counter, child_ctx);
  3083. /*
  3084. * If the last counter was a group counter, it will have appended all
  3085. * its siblings to the list, but we obtained 'tmp' before that which
  3086. * will still point to the list head terminating the iteration.
  3087. */
  3088. if (!list_empty(&child_ctx->counter_list))
  3089. goto again;
  3090. mutex_unlock(&child_ctx->mutex);
  3091. put_ctx(child_ctx);
  3092. }
  3093. /*
  3094. * free an unexposed, unused context as created by inheritance by
  3095. * init_task below, used by fork() in case of fail.
  3096. */
  3097. void perf_counter_free_task(struct task_struct *task)
  3098. {
  3099. struct perf_counter_context *ctx = task->perf_counter_ctxp;
  3100. struct perf_counter *counter, *tmp;
  3101. if (!ctx)
  3102. return;
  3103. mutex_lock(&ctx->mutex);
  3104. again:
  3105. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
  3106. struct perf_counter *parent = counter->parent;
  3107. if (WARN_ON_ONCE(!parent))
  3108. continue;
  3109. mutex_lock(&parent->child_mutex);
  3110. list_del_init(&counter->child_list);
  3111. mutex_unlock(&parent->child_mutex);
  3112. fput(parent->filp);
  3113. list_del_counter(counter, ctx);
  3114. free_counter(counter);
  3115. }
  3116. if (!list_empty(&ctx->counter_list))
  3117. goto again;
  3118. mutex_unlock(&ctx->mutex);
  3119. put_ctx(ctx);
  3120. }
  3121. /*
  3122. * Initialize the perf_counter context in task_struct
  3123. */
  3124. int perf_counter_init_task(struct task_struct *child)
  3125. {
  3126. struct perf_counter_context *child_ctx, *parent_ctx;
  3127. struct perf_counter_context *cloned_ctx;
  3128. struct perf_counter *counter;
  3129. struct task_struct *parent = current;
  3130. int inherited_all = 1;
  3131. int ret = 0;
  3132. child->perf_counter_ctxp = NULL;
  3133. mutex_init(&child->perf_counter_mutex);
  3134. INIT_LIST_HEAD(&child->perf_counter_list);
  3135. if (likely(!parent->perf_counter_ctxp))
  3136. return 0;
  3137. /*
  3138. * This is executed from the parent task context, so inherit
  3139. * counters that have been marked for cloning.
  3140. * First allocate and initialize a context for the child.
  3141. */
  3142. child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
  3143. if (!child_ctx)
  3144. return -ENOMEM;
  3145. __perf_counter_init_context(child_ctx, child);
  3146. child->perf_counter_ctxp = child_ctx;
  3147. get_task_struct(child);
  3148. /*
  3149. * If the parent's context is a clone, pin it so it won't get
  3150. * swapped under us.
  3151. */
  3152. parent_ctx = perf_pin_task_context(parent);
  3153. /*
  3154. * No need to check if parent_ctx != NULL here; since we saw
  3155. * it non-NULL earlier, the only reason for it to become NULL
  3156. * is if we exit, and since we're currently in the middle of
  3157. * a fork we can't be exiting at the same time.
  3158. */
  3159. /*
  3160. * Lock the parent list. No need to lock the child - not PID
  3161. * hashed yet and not running, so nobody can access it.
  3162. */
  3163. mutex_lock(&parent_ctx->mutex);
  3164. /*
  3165. * We dont have to disable NMIs - we are only looking at
  3166. * the list, not manipulating it:
  3167. */
  3168. list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
  3169. if (counter != counter->group_leader)
  3170. continue;
  3171. if (!counter->hw_event.inherit) {
  3172. inherited_all = 0;
  3173. continue;
  3174. }
  3175. ret = inherit_group(counter, parent, parent_ctx,
  3176. child, child_ctx);
  3177. if (ret) {
  3178. inherited_all = 0;
  3179. break;
  3180. }
  3181. }
  3182. if (inherited_all) {
  3183. /*
  3184. * Mark the child context as a clone of the parent
  3185. * context, or of whatever the parent is a clone of.
  3186. * Note that if the parent is a clone, it could get
  3187. * uncloned at any point, but that doesn't matter
  3188. * because the list of counters and the generation
  3189. * count can't have changed since we took the mutex.
  3190. */
  3191. cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
  3192. if (cloned_ctx) {
  3193. child_ctx->parent_ctx = cloned_ctx;
  3194. child_ctx->parent_gen = parent_ctx->parent_gen;
  3195. } else {
  3196. child_ctx->parent_ctx = parent_ctx;
  3197. child_ctx->parent_gen = parent_ctx->generation;
  3198. }
  3199. get_ctx(child_ctx->parent_ctx);
  3200. }
  3201. mutex_unlock(&parent_ctx->mutex);
  3202. perf_unpin_context(parent_ctx);
  3203. return ret;
  3204. }
  3205. static void __cpuinit perf_counter_init_cpu(int cpu)
  3206. {
  3207. struct perf_cpu_context *cpuctx;
  3208. cpuctx = &per_cpu(perf_cpu_context, cpu);
  3209. __perf_counter_init_context(&cpuctx->ctx, NULL);
  3210. spin_lock(&perf_resource_lock);
  3211. cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
  3212. spin_unlock(&perf_resource_lock);
  3213. hw_perf_counter_setup(cpu);
  3214. }
  3215. #ifdef CONFIG_HOTPLUG_CPU
  3216. static void __perf_counter_exit_cpu(void *info)
  3217. {
  3218. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  3219. struct perf_counter_context *ctx = &cpuctx->ctx;
  3220. struct perf_counter *counter, *tmp;
  3221. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
  3222. __perf_counter_remove_from_context(counter);
  3223. }
  3224. static void perf_counter_exit_cpu(int cpu)
  3225. {
  3226. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  3227. struct perf_counter_context *ctx = &cpuctx->ctx;
  3228. mutex_lock(&ctx->mutex);
  3229. smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
  3230. mutex_unlock(&ctx->mutex);
  3231. }
  3232. #else
  3233. static inline void perf_counter_exit_cpu(int cpu) { }
  3234. #endif
  3235. static int __cpuinit
  3236. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  3237. {
  3238. unsigned int cpu = (long)hcpu;
  3239. switch (action) {
  3240. case CPU_UP_PREPARE:
  3241. case CPU_UP_PREPARE_FROZEN:
  3242. perf_counter_init_cpu(cpu);
  3243. break;
  3244. case CPU_DOWN_PREPARE:
  3245. case CPU_DOWN_PREPARE_FROZEN:
  3246. perf_counter_exit_cpu(cpu);
  3247. break;
  3248. default:
  3249. break;
  3250. }
  3251. return NOTIFY_OK;
  3252. }
  3253. /*
  3254. * This has to have a higher priority than migration_notifier in sched.c.
  3255. */
  3256. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  3257. .notifier_call = perf_cpu_notify,
  3258. .priority = 20,
  3259. };
  3260. void __init perf_counter_init(void)
  3261. {
  3262. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  3263. (void *)(long)smp_processor_id());
  3264. register_cpu_notifier(&perf_cpu_nb);
  3265. }
  3266. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  3267. {
  3268. return sprintf(buf, "%d\n", perf_reserved_percpu);
  3269. }
  3270. static ssize_t
  3271. perf_set_reserve_percpu(struct sysdev_class *class,
  3272. const char *buf,
  3273. size_t count)
  3274. {
  3275. struct perf_cpu_context *cpuctx;
  3276. unsigned long val;
  3277. int err, cpu, mpt;
  3278. err = strict_strtoul(buf, 10, &val);
  3279. if (err)
  3280. return err;
  3281. if (val > perf_max_counters)
  3282. return -EINVAL;
  3283. spin_lock(&perf_resource_lock);
  3284. perf_reserved_percpu = val;
  3285. for_each_online_cpu(cpu) {
  3286. cpuctx = &per_cpu(perf_cpu_context, cpu);
  3287. spin_lock_irq(&cpuctx->ctx.lock);
  3288. mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
  3289. perf_max_counters - perf_reserved_percpu);
  3290. cpuctx->max_pertask = mpt;
  3291. spin_unlock_irq(&cpuctx->ctx.lock);
  3292. }
  3293. spin_unlock(&perf_resource_lock);
  3294. return count;
  3295. }
  3296. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  3297. {
  3298. return sprintf(buf, "%d\n", perf_overcommit);
  3299. }
  3300. static ssize_t
  3301. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  3302. {
  3303. unsigned long val;
  3304. int err;
  3305. err = strict_strtoul(buf, 10, &val);
  3306. if (err)
  3307. return err;
  3308. if (val > 1)
  3309. return -EINVAL;
  3310. spin_lock(&perf_resource_lock);
  3311. perf_overcommit = val;
  3312. spin_unlock(&perf_resource_lock);
  3313. return count;
  3314. }
  3315. static SYSDEV_CLASS_ATTR(
  3316. reserve_percpu,
  3317. 0644,
  3318. perf_show_reserve_percpu,
  3319. perf_set_reserve_percpu
  3320. );
  3321. static SYSDEV_CLASS_ATTR(
  3322. overcommit,
  3323. 0644,
  3324. perf_show_overcommit,
  3325. perf_set_overcommit
  3326. );
  3327. static struct attribute *perfclass_attrs[] = {
  3328. &attr_reserve_percpu.attr,
  3329. &attr_overcommit.attr,
  3330. NULL
  3331. };
  3332. static struct attribute_group perfclass_attr_group = {
  3333. .attrs = perfclass_attrs,
  3334. .name = "perf_counters",
  3335. };
  3336. static int __init perf_counter_sysfs_init(void)
  3337. {
  3338. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  3339. &perfclass_attr_group);
  3340. }
  3341. device_initcall(perf_counter_sysfs_init);