perf_counter.c 109 KB

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