perf_counter.c 103 KB

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