perf_counter.c 99 KB

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