perf_counter.c 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133
  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_freq(struct perf_counter_context *ctx)
  1021. {
  1022. struct perf_counter *counter;
  1023. u64 interrupts, sample_period;
  1024. u64 events, period;
  1025. s64 delta;
  1026. spin_lock(&ctx->lock);
  1027. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  1028. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  1029. continue;
  1030. interrupts = counter->hw.interrupts;
  1031. counter->hw.interrupts = 0;
  1032. if (interrupts == MAX_INTERRUPTS) {
  1033. perf_log_throttle(counter, 1);
  1034. counter->pmu->unthrottle(counter);
  1035. interrupts = 2*sysctl_perf_counter_limit/HZ;
  1036. }
  1037. if (!counter->attr.freq || !counter->attr.sample_freq)
  1038. continue;
  1039. events = HZ * interrupts * counter->hw.sample_period;
  1040. period = div64_u64(events, counter->attr.sample_freq);
  1041. delta = (s64)(1 + period - counter->hw.sample_period);
  1042. delta >>= 1;
  1043. sample_period = counter->hw.sample_period + delta;
  1044. if (!sample_period)
  1045. sample_period = 1;
  1046. perf_log_period(counter, sample_period);
  1047. counter->hw.sample_period = sample_period;
  1048. }
  1049. spin_unlock(&ctx->lock);
  1050. }
  1051. /*
  1052. * Round-robin a context's counters:
  1053. */
  1054. static void rotate_ctx(struct perf_counter_context *ctx)
  1055. {
  1056. struct perf_counter *counter;
  1057. if (!ctx->nr_counters)
  1058. return;
  1059. spin_lock(&ctx->lock);
  1060. /*
  1061. * Rotate the first entry last (works just fine for group counters too):
  1062. */
  1063. perf_disable();
  1064. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  1065. list_move_tail(&counter->list_entry, &ctx->counter_list);
  1066. break;
  1067. }
  1068. perf_enable();
  1069. spin_unlock(&ctx->lock);
  1070. }
  1071. void perf_counter_task_tick(struct task_struct *curr, int cpu)
  1072. {
  1073. struct perf_cpu_context *cpuctx;
  1074. struct perf_counter_context *ctx;
  1075. if (!atomic_read(&nr_counters))
  1076. return;
  1077. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1078. ctx = curr->perf_counter_ctxp;
  1079. perf_adjust_freq(&cpuctx->ctx);
  1080. if (ctx)
  1081. perf_adjust_freq(ctx);
  1082. perf_counter_cpu_sched_out(cpuctx);
  1083. if (ctx)
  1084. __perf_counter_task_sched_out(ctx);
  1085. rotate_ctx(&cpuctx->ctx);
  1086. if (ctx)
  1087. rotate_ctx(ctx);
  1088. perf_counter_cpu_sched_in(cpuctx, cpu);
  1089. if (ctx)
  1090. perf_counter_task_sched_in(curr, cpu);
  1091. }
  1092. /*
  1093. * Cross CPU call to read the hardware counter
  1094. */
  1095. static void __read(void *info)
  1096. {
  1097. struct perf_counter *counter = info;
  1098. struct perf_counter_context *ctx = counter->ctx;
  1099. unsigned long flags;
  1100. local_irq_save(flags);
  1101. if (ctx->is_active)
  1102. update_context_time(ctx);
  1103. counter->pmu->read(counter);
  1104. update_counter_times(counter);
  1105. local_irq_restore(flags);
  1106. }
  1107. static u64 perf_counter_read(struct perf_counter *counter)
  1108. {
  1109. /*
  1110. * If counter is enabled and currently active on a CPU, update the
  1111. * value in the counter structure:
  1112. */
  1113. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  1114. smp_call_function_single(counter->oncpu,
  1115. __read, counter, 1);
  1116. } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
  1117. update_counter_times(counter);
  1118. }
  1119. return atomic64_read(&counter->count);
  1120. }
  1121. /*
  1122. * Initialize the perf_counter context in a task_struct:
  1123. */
  1124. static void
  1125. __perf_counter_init_context(struct perf_counter_context *ctx,
  1126. struct task_struct *task)
  1127. {
  1128. memset(ctx, 0, sizeof(*ctx));
  1129. spin_lock_init(&ctx->lock);
  1130. mutex_init(&ctx->mutex);
  1131. INIT_LIST_HEAD(&ctx->counter_list);
  1132. INIT_LIST_HEAD(&ctx->event_list);
  1133. atomic_set(&ctx->refcount, 1);
  1134. ctx->task = task;
  1135. }
  1136. static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  1137. {
  1138. struct perf_counter_context *parent_ctx;
  1139. struct perf_counter_context *ctx;
  1140. struct perf_cpu_context *cpuctx;
  1141. struct task_struct *task;
  1142. unsigned long flags;
  1143. int err;
  1144. /*
  1145. * If cpu is not a wildcard then this is a percpu counter:
  1146. */
  1147. if (cpu != -1) {
  1148. /* Must be root to operate on a CPU counter: */
  1149. if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
  1150. return ERR_PTR(-EACCES);
  1151. if (cpu < 0 || cpu > num_possible_cpus())
  1152. return ERR_PTR(-EINVAL);
  1153. /*
  1154. * We could be clever and allow to attach a counter to an
  1155. * offline CPU and activate it when the CPU comes up, but
  1156. * that's for later.
  1157. */
  1158. if (!cpu_isset(cpu, cpu_online_map))
  1159. return ERR_PTR(-ENODEV);
  1160. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1161. ctx = &cpuctx->ctx;
  1162. get_ctx(ctx);
  1163. return ctx;
  1164. }
  1165. rcu_read_lock();
  1166. if (!pid)
  1167. task = current;
  1168. else
  1169. task = find_task_by_vpid(pid);
  1170. if (task)
  1171. get_task_struct(task);
  1172. rcu_read_unlock();
  1173. if (!task)
  1174. return ERR_PTR(-ESRCH);
  1175. /*
  1176. * Can't attach counters to a dying task.
  1177. */
  1178. err = -ESRCH;
  1179. if (task->flags & PF_EXITING)
  1180. goto errout;
  1181. /* Reuse ptrace permission checks for now. */
  1182. err = -EACCES;
  1183. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  1184. goto errout;
  1185. retry:
  1186. ctx = perf_lock_task_context(task, &flags);
  1187. if (ctx) {
  1188. parent_ctx = ctx->parent_ctx;
  1189. if (parent_ctx) {
  1190. put_ctx(parent_ctx);
  1191. ctx->parent_ctx = NULL; /* no longer a clone */
  1192. }
  1193. /*
  1194. * Get an extra reference before dropping the lock so that
  1195. * this context won't get freed if the task exits.
  1196. */
  1197. get_ctx(ctx);
  1198. spin_unlock_irqrestore(&ctx->lock, flags);
  1199. }
  1200. if (!ctx) {
  1201. ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
  1202. err = -ENOMEM;
  1203. if (!ctx)
  1204. goto errout;
  1205. __perf_counter_init_context(ctx, task);
  1206. get_ctx(ctx);
  1207. if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
  1208. /*
  1209. * We raced with some other task; use
  1210. * the context they set.
  1211. */
  1212. kfree(ctx);
  1213. goto retry;
  1214. }
  1215. get_task_struct(task);
  1216. }
  1217. put_task_struct(task);
  1218. return ctx;
  1219. errout:
  1220. put_task_struct(task);
  1221. return ERR_PTR(err);
  1222. }
  1223. static void free_counter_rcu(struct rcu_head *head)
  1224. {
  1225. struct perf_counter *counter;
  1226. counter = container_of(head, struct perf_counter, rcu_head);
  1227. if (counter->ns)
  1228. put_pid_ns(counter->ns);
  1229. kfree(counter);
  1230. }
  1231. static void perf_pending_sync(struct perf_counter *counter);
  1232. static void free_counter(struct perf_counter *counter)
  1233. {
  1234. perf_pending_sync(counter);
  1235. atomic_dec(&nr_counters);
  1236. if (counter->attr.mmap)
  1237. atomic_dec(&nr_mmap_counters);
  1238. if (counter->attr.comm)
  1239. atomic_dec(&nr_comm_counters);
  1240. if (counter->destroy)
  1241. counter->destroy(counter);
  1242. put_ctx(counter->ctx);
  1243. call_rcu(&counter->rcu_head, free_counter_rcu);
  1244. }
  1245. /*
  1246. * Called when the last reference to the file is gone.
  1247. */
  1248. static int perf_release(struct inode *inode, struct file *file)
  1249. {
  1250. struct perf_counter *counter = file->private_data;
  1251. struct perf_counter_context *ctx = counter->ctx;
  1252. file->private_data = NULL;
  1253. WARN_ON_ONCE(ctx->parent_ctx);
  1254. mutex_lock(&ctx->mutex);
  1255. perf_counter_remove_from_context(counter);
  1256. mutex_unlock(&ctx->mutex);
  1257. mutex_lock(&counter->owner->perf_counter_mutex);
  1258. list_del_init(&counter->owner_entry);
  1259. mutex_unlock(&counter->owner->perf_counter_mutex);
  1260. put_task_struct(counter->owner);
  1261. free_counter(counter);
  1262. return 0;
  1263. }
  1264. /*
  1265. * Read the performance counter - simple non blocking version for now
  1266. */
  1267. static ssize_t
  1268. perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
  1269. {
  1270. u64 values[3];
  1271. int n;
  1272. /*
  1273. * Return end-of-file for a read on a counter that is in
  1274. * error state (i.e. because it was pinned but it couldn't be
  1275. * scheduled on to the CPU at some point).
  1276. */
  1277. if (counter->state == PERF_COUNTER_STATE_ERROR)
  1278. return 0;
  1279. WARN_ON_ONCE(counter->ctx->parent_ctx);
  1280. mutex_lock(&counter->child_mutex);
  1281. values[0] = perf_counter_read(counter);
  1282. n = 1;
  1283. if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1284. values[n++] = counter->total_time_enabled +
  1285. atomic64_read(&counter->child_total_time_enabled);
  1286. if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1287. values[n++] = counter->total_time_running +
  1288. atomic64_read(&counter->child_total_time_running);
  1289. if (counter->attr.read_format & PERF_FORMAT_ID)
  1290. values[n++] = counter->id;
  1291. mutex_unlock(&counter->child_mutex);
  1292. if (count < n * sizeof(u64))
  1293. return -EINVAL;
  1294. count = n * sizeof(u64);
  1295. if (copy_to_user(buf, values, count))
  1296. return -EFAULT;
  1297. return count;
  1298. }
  1299. static ssize_t
  1300. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  1301. {
  1302. struct perf_counter *counter = file->private_data;
  1303. return perf_read_hw(counter, buf, count);
  1304. }
  1305. static unsigned int perf_poll(struct file *file, poll_table *wait)
  1306. {
  1307. struct perf_counter *counter = file->private_data;
  1308. struct perf_mmap_data *data;
  1309. unsigned int events = POLL_HUP;
  1310. rcu_read_lock();
  1311. data = rcu_dereference(counter->data);
  1312. if (data)
  1313. events = atomic_xchg(&data->poll, 0);
  1314. rcu_read_unlock();
  1315. poll_wait(file, &counter->waitq, wait);
  1316. return events;
  1317. }
  1318. static void perf_counter_reset(struct perf_counter *counter)
  1319. {
  1320. (void)perf_counter_read(counter);
  1321. atomic64_set(&counter->count, 0);
  1322. perf_counter_update_userpage(counter);
  1323. }
  1324. static void perf_counter_for_each_sibling(struct perf_counter *counter,
  1325. void (*func)(struct perf_counter *))
  1326. {
  1327. struct perf_counter_context *ctx = counter->ctx;
  1328. struct perf_counter *sibling;
  1329. WARN_ON_ONCE(ctx->parent_ctx);
  1330. mutex_lock(&ctx->mutex);
  1331. counter = counter->group_leader;
  1332. func(counter);
  1333. list_for_each_entry(sibling, &counter->sibling_list, list_entry)
  1334. func(sibling);
  1335. mutex_unlock(&ctx->mutex);
  1336. }
  1337. /*
  1338. * Holding the top-level counter's child_mutex means that any
  1339. * descendant process that has inherited this counter will block
  1340. * in sync_child_counter if it goes to exit, thus satisfying the
  1341. * task existence requirements of perf_counter_enable/disable.
  1342. */
  1343. static void perf_counter_for_each_child(struct perf_counter *counter,
  1344. void (*func)(struct perf_counter *))
  1345. {
  1346. struct perf_counter *child;
  1347. WARN_ON_ONCE(counter->ctx->parent_ctx);
  1348. mutex_lock(&counter->child_mutex);
  1349. func(counter);
  1350. list_for_each_entry(child, &counter->child_list, child_list)
  1351. func(child);
  1352. mutex_unlock(&counter->child_mutex);
  1353. }
  1354. static void perf_counter_for_each(struct perf_counter *counter,
  1355. void (*func)(struct perf_counter *))
  1356. {
  1357. struct perf_counter *child;
  1358. WARN_ON_ONCE(counter->ctx->parent_ctx);
  1359. mutex_lock(&counter->child_mutex);
  1360. perf_counter_for_each_sibling(counter, func);
  1361. list_for_each_entry(child, &counter->child_list, child_list)
  1362. perf_counter_for_each_sibling(child, func);
  1363. mutex_unlock(&counter->child_mutex);
  1364. }
  1365. static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
  1366. {
  1367. struct perf_counter_context *ctx = counter->ctx;
  1368. unsigned long size;
  1369. int ret = 0;
  1370. u64 value;
  1371. if (!counter->attr.sample_period)
  1372. return -EINVAL;
  1373. size = copy_from_user(&value, arg, sizeof(value));
  1374. if (size != sizeof(value))
  1375. return -EFAULT;
  1376. if (!value)
  1377. return -EINVAL;
  1378. spin_lock_irq(&ctx->lock);
  1379. if (counter->attr.freq) {
  1380. if (value > sysctl_perf_counter_limit) {
  1381. ret = -EINVAL;
  1382. goto unlock;
  1383. }
  1384. counter->attr.sample_freq = value;
  1385. } else {
  1386. counter->attr.sample_period = value;
  1387. counter->hw.sample_period = value;
  1388. perf_log_period(counter, value);
  1389. }
  1390. unlock:
  1391. spin_unlock_irq(&ctx->lock);
  1392. return ret;
  1393. }
  1394. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1395. {
  1396. struct perf_counter *counter = file->private_data;
  1397. void (*func)(struct perf_counter *);
  1398. u32 flags = arg;
  1399. switch (cmd) {
  1400. case PERF_COUNTER_IOC_ENABLE:
  1401. func = perf_counter_enable;
  1402. break;
  1403. case PERF_COUNTER_IOC_DISABLE:
  1404. func = perf_counter_disable;
  1405. break;
  1406. case PERF_COUNTER_IOC_RESET:
  1407. func = perf_counter_reset;
  1408. break;
  1409. case PERF_COUNTER_IOC_REFRESH:
  1410. return perf_counter_refresh(counter, arg);
  1411. case PERF_COUNTER_IOC_PERIOD:
  1412. return perf_counter_period(counter, (u64 __user *)arg);
  1413. default:
  1414. return -ENOTTY;
  1415. }
  1416. if (flags & PERF_IOC_FLAG_GROUP)
  1417. perf_counter_for_each(counter, func);
  1418. else
  1419. perf_counter_for_each_child(counter, func);
  1420. return 0;
  1421. }
  1422. int perf_counter_task_enable(void)
  1423. {
  1424. struct perf_counter *counter;
  1425. mutex_lock(&current->perf_counter_mutex);
  1426. list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
  1427. perf_counter_for_each_child(counter, perf_counter_enable);
  1428. mutex_unlock(&current->perf_counter_mutex);
  1429. return 0;
  1430. }
  1431. int perf_counter_task_disable(void)
  1432. {
  1433. struct perf_counter *counter;
  1434. mutex_lock(&current->perf_counter_mutex);
  1435. list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
  1436. perf_counter_for_each_child(counter, perf_counter_disable);
  1437. mutex_unlock(&current->perf_counter_mutex);
  1438. return 0;
  1439. }
  1440. /*
  1441. * Callers need to ensure there can be no nesting of this function, otherwise
  1442. * the seqlock logic goes bad. We can not serialize this because the arch
  1443. * code calls this from NMI context.
  1444. */
  1445. void perf_counter_update_userpage(struct perf_counter *counter)
  1446. {
  1447. struct perf_counter_mmap_page *userpg;
  1448. struct perf_mmap_data *data;
  1449. rcu_read_lock();
  1450. data = rcu_dereference(counter->data);
  1451. if (!data)
  1452. goto unlock;
  1453. userpg = data->user_page;
  1454. /*
  1455. * Disable preemption so as to not let the corresponding user-space
  1456. * spin too long if we get preempted.
  1457. */
  1458. preempt_disable();
  1459. ++userpg->lock;
  1460. barrier();
  1461. userpg->index = counter->hw.idx;
  1462. userpg->offset = atomic64_read(&counter->count);
  1463. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  1464. userpg->offset -= atomic64_read(&counter->hw.prev_count);
  1465. barrier();
  1466. ++userpg->lock;
  1467. preempt_enable();
  1468. unlock:
  1469. rcu_read_unlock();
  1470. }
  1471. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1472. {
  1473. struct perf_counter *counter = vma->vm_file->private_data;
  1474. struct perf_mmap_data *data;
  1475. int ret = VM_FAULT_SIGBUS;
  1476. rcu_read_lock();
  1477. data = rcu_dereference(counter->data);
  1478. if (!data)
  1479. goto unlock;
  1480. if (vmf->pgoff == 0) {
  1481. vmf->page = virt_to_page(data->user_page);
  1482. } else {
  1483. int nr = vmf->pgoff - 1;
  1484. if ((unsigned)nr > data->nr_pages)
  1485. goto unlock;
  1486. vmf->page = virt_to_page(data->data_pages[nr]);
  1487. }
  1488. get_page(vmf->page);
  1489. ret = 0;
  1490. unlock:
  1491. rcu_read_unlock();
  1492. return ret;
  1493. }
  1494. static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
  1495. {
  1496. struct perf_mmap_data *data;
  1497. unsigned long size;
  1498. int i;
  1499. WARN_ON(atomic_read(&counter->mmap_count));
  1500. size = sizeof(struct perf_mmap_data);
  1501. size += nr_pages * sizeof(void *);
  1502. data = kzalloc(size, GFP_KERNEL);
  1503. if (!data)
  1504. goto fail;
  1505. data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
  1506. if (!data->user_page)
  1507. goto fail_user_page;
  1508. for (i = 0; i < nr_pages; i++) {
  1509. data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
  1510. if (!data->data_pages[i])
  1511. goto fail_data_pages;
  1512. }
  1513. data->nr_pages = nr_pages;
  1514. atomic_set(&data->lock, -1);
  1515. rcu_assign_pointer(counter->data, data);
  1516. return 0;
  1517. fail_data_pages:
  1518. for (i--; i >= 0; i--)
  1519. free_page((unsigned long)data->data_pages[i]);
  1520. free_page((unsigned long)data->user_page);
  1521. fail_user_page:
  1522. kfree(data);
  1523. fail:
  1524. return -ENOMEM;
  1525. }
  1526. static void __perf_mmap_data_free(struct rcu_head *rcu_head)
  1527. {
  1528. struct perf_mmap_data *data;
  1529. int i;
  1530. data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
  1531. free_page((unsigned long)data->user_page);
  1532. for (i = 0; i < data->nr_pages; i++)
  1533. free_page((unsigned long)data->data_pages[i]);
  1534. kfree(data);
  1535. }
  1536. static void perf_mmap_data_free(struct perf_counter *counter)
  1537. {
  1538. struct perf_mmap_data *data = counter->data;
  1539. WARN_ON(atomic_read(&counter->mmap_count));
  1540. rcu_assign_pointer(counter->data, NULL);
  1541. call_rcu(&data->rcu_head, __perf_mmap_data_free);
  1542. }
  1543. static void perf_mmap_open(struct vm_area_struct *vma)
  1544. {
  1545. struct perf_counter *counter = vma->vm_file->private_data;
  1546. atomic_inc(&counter->mmap_count);
  1547. }
  1548. static void perf_mmap_close(struct vm_area_struct *vma)
  1549. {
  1550. struct perf_counter *counter = vma->vm_file->private_data;
  1551. WARN_ON_ONCE(counter->ctx->parent_ctx);
  1552. if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
  1553. struct user_struct *user = current_user();
  1554. atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
  1555. vma->vm_mm->locked_vm -= counter->data->nr_locked;
  1556. perf_mmap_data_free(counter);
  1557. mutex_unlock(&counter->mmap_mutex);
  1558. }
  1559. }
  1560. static struct vm_operations_struct perf_mmap_vmops = {
  1561. .open = perf_mmap_open,
  1562. .close = perf_mmap_close,
  1563. .fault = perf_mmap_fault,
  1564. };
  1565. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  1566. {
  1567. struct perf_counter *counter = file->private_data;
  1568. unsigned long user_locked, user_lock_limit;
  1569. struct user_struct *user = current_user();
  1570. unsigned long locked, lock_limit;
  1571. unsigned long vma_size;
  1572. unsigned long nr_pages;
  1573. long user_extra, extra;
  1574. int ret = 0;
  1575. if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
  1576. return -EINVAL;
  1577. vma_size = vma->vm_end - vma->vm_start;
  1578. nr_pages = (vma_size / PAGE_SIZE) - 1;
  1579. /*
  1580. * If we have data pages ensure they're a power-of-two number, so we
  1581. * can do bitmasks instead of modulo.
  1582. */
  1583. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  1584. return -EINVAL;
  1585. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  1586. return -EINVAL;
  1587. if (vma->vm_pgoff != 0)
  1588. return -EINVAL;
  1589. WARN_ON_ONCE(counter->ctx->parent_ctx);
  1590. mutex_lock(&counter->mmap_mutex);
  1591. if (atomic_inc_not_zero(&counter->mmap_count)) {
  1592. if (nr_pages != counter->data->nr_pages)
  1593. ret = -EINVAL;
  1594. goto unlock;
  1595. }
  1596. user_extra = nr_pages + 1;
  1597. user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
  1598. /*
  1599. * Increase the limit linearly with more CPUs:
  1600. */
  1601. user_lock_limit *= num_online_cpus();
  1602. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  1603. extra = 0;
  1604. if (user_locked > user_lock_limit)
  1605. extra = user_locked - user_lock_limit;
  1606. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  1607. lock_limit >>= PAGE_SHIFT;
  1608. locked = vma->vm_mm->locked_vm + extra;
  1609. if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
  1610. ret = -EPERM;
  1611. goto unlock;
  1612. }
  1613. WARN_ON(counter->data);
  1614. ret = perf_mmap_data_alloc(counter, nr_pages);
  1615. if (ret)
  1616. goto unlock;
  1617. atomic_set(&counter->mmap_count, 1);
  1618. atomic_long_add(user_extra, &user->locked_vm);
  1619. vma->vm_mm->locked_vm += extra;
  1620. counter->data->nr_locked = extra;
  1621. unlock:
  1622. mutex_unlock(&counter->mmap_mutex);
  1623. vma->vm_flags &= ~VM_MAYWRITE;
  1624. vma->vm_flags |= VM_RESERVED;
  1625. vma->vm_ops = &perf_mmap_vmops;
  1626. return ret;
  1627. }
  1628. static int perf_fasync(int fd, struct file *filp, int on)
  1629. {
  1630. struct inode *inode = filp->f_path.dentry->d_inode;
  1631. struct perf_counter *counter = filp->private_data;
  1632. int retval;
  1633. mutex_lock(&inode->i_mutex);
  1634. retval = fasync_helper(fd, filp, on, &counter->fasync);
  1635. mutex_unlock(&inode->i_mutex);
  1636. if (retval < 0)
  1637. return retval;
  1638. return 0;
  1639. }
  1640. static const struct file_operations perf_fops = {
  1641. .release = perf_release,
  1642. .read = perf_read,
  1643. .poll = perf_poll,
  1644. .unlocked_ioctl = perf_ioctl,
  1645. .compat_ioctl = perf_ioctl,
  1646. .mmap = perf_mmap,
  1647. .fasync = perf_fasync,
  1648. };
  1649. /*
  1650. * Perf counter wakeup
  1651. *
  1652. * If there's data, ensure we set the poll() state and publish everything
  1653. * to user-space before waking everybody up.
  1654. */
  1655. void perf_counter_wakeup(struct perf_counter *counter)
  1656. {
  1657. wake_up_all(&counter->waitq);
  1658. if (counter->pending_kill) {
  1659. kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
  1660. counter->pending_kill = 0;
  1661. }
  1662. }
  1663. /*
  1664. * Pending wakeups
  1665. *
  1666. * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
  1667. *
  1668. * The NMI bit means we cannot possibly take locks. Therefore, maintain a
  1669. * single linked list and use cmpxchg() to add entries lockless.
  1670. */
  1671. static void perf_pending_counter(struct perf_pending_entry *entry)
  1672. {
  1673. struct perf_counter *counter = container_of(entry,
  1674. struct perf_counter, pending);
  1675. if (counter->pending_disable) {
  1676. counter->pending_disable = 0;
  1677. perf_counter_disable(counter);
  1678. }
  1679. if (counter->pending_wakeup) {
  1680. counter->pending_wakeup = 0;
  1681. perf_counter_wakeup(counter);
  1682. }
  1683. }
  1684. #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
  1685. static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
  1686. PENDING_TAIL,
  1687. };
  1688. static void perf_pending_queue(struct perf_pending_entry *entry,
  1689. void (*func)(struct perf_pending_entry *))
  1690. {
  1691. struct perf_pending_entry **head;
  1692. if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
  1693. return;
  1694. entry->func = func;
  1695. head = &get_cpu_var(perf_pending_head);
  1696. do {
  1697. entry->next = *head;
  1698. } while (cmpxchg(head, entry->next, entry) != entry->next);
  1699. set_perf_counter_pending();
  1700. put_cpu_var(perf_pending_head);
  1701. }
  1702. static int __perf_pending_run(void)
  1703. {
  1704. struct perf_pending_entry *list;
  1705. int nr = 0;
  1706. list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
  1707. while (list != PENDING_TAIL) {
  1708. void (*func)(struct perf_pending_entry *);
  1709. struct perf_pending_entry *entry = list;
  1710. list = list->next;
  1711. func = entry->func;
  1712. entry->next = NULL;
  1713. /*
  1714. * Ensure we observe the unqueue before we issue the wakeup,
  1715. * so that we won't be waiting forever.
  1716. * -- see perf_not_pending().
  1717. */
  1718. smp_wmb();
  1719. func(entry);
  1720. nr++;
  1721. }
  1722. return nr;
  1723. }
  1724. static inline int perf_not_pending(struct perf_counter *counter)
  1725. {
  1726. /*
  1727. * If we flush on whatever cpu we run, there is a chance we don't
  1728. * need to wait.
  1729. */
  1730. get_cpu();
  1731. __perf_pending_run();
  1732. put_cpu();
  1733. /*
  1734. * Ensure we see the proper queue state before going to sleep
  1735. * so that we do not miss the wakeup. -- see perf_pending_handle()
  1736. */
  1737. smp_rmb();
  1738. return counter->pending.next == NULL;
  1739. }
  1740. static void perf_pending_sync(struct perf_counter *counter)
  1741. {
  1742. wait_event(counter->waitq, perf_not_pending(counter));
  1743. }
  1744. void perf_counter_do_pending(void)
  1745. {
  1746. __perf_pending_run();
  1747. }
  1748. /*
  1749. * Callchain support -- arch specific
  1750. */
  1751. __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1752. {
  1753. return NULL;
  1754. }
  1755. /*
  1756. * Output
  1757. */
  1758. struct perf_output_handle {
  1759. struct perf_counter *counter;
  1760. struct perf_mmap_data *data;
  1761. unsigned long head;
  1762. unsigned long offset;
  1763. int nmi;
  1764. int overflow;
  1765. int locked;
  1766. unsigned long flags;
  1767. };
  1768. static void perf_output_wakeup(struct perf_output_handle *handle)
  1769. {
  1770. atomic_set(&handle->data->poll, POLL_IN);
  1771. if (handle->nmi) {
  1772. handle->counter->pending_wakeup = 1;
  1773. perf_pending_queue(&handle->counter->pending,
  1774. perf_pending_counter);
  1775. } else
  1776. perf_counter_wakeup(handle->counter);
  1777. }
  1778. /*
  1779. * Curious locking construct.
  1780. *
  1781. * We need to ensure a later event doesn't publish a head when a former
  1782. * event isn't done writing. However since we need to deal with NMIs we
  1783. * cannot fully serialize things.
  1784. *
  1785. * What we do is serialize between CPUs so we only have to deal with NMI
  1786. * nesting on a single CPU.
  1787. *
  1788. * We only publish the head (and generate a wakeup) when the outer-most
  1789. * event completes.
  1790. */
  1791. static void perf_output_lock(struct perf_output_handle *handle)
  1792. {
  1793. struct perf_mmap_data *data = handle->data;
  1794. int cpu;
  1795. handle->locked = 0;
  1796. local_irq_save(handle->flags);
  1797. cpu = smp_processor_id();
  1798. if (in_nmi() && atomic_read(&data->lock) == cpu)
  1799. return;
  1800. while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
  1801. cpu_relax();
  1802. handle->locked = 1;
  1803. }
  1804. static void perf_output_unlock(struct perf_output_handle *handle)
  1805. {
  1806. struct perf_mmap_data *data = handle->data;
  1807. unsigned long head;
  1808. int cpu;
  1809. data->done_head = data->head;
  1810. if (!handle->locked)
  1811. goto out;
  1812. again:
  1813. /*
  1814. * The xchg implies a full barrier that ensures all writes are done
  1815. * before we publish the new head, matched by a rmb() in userspace when
  1816. * reading this position.
  1817. */
  1818. while ((head = atomic_long_xchg(&data->done_head, 0)))
  1819. data->user_page->data_head = head;
  1820. /*
  1821. * NMI can happen here, which means we can miss a done_head update.
  1822. */
  1823. cpu = atomic_xchg(&data->lock, -1);
  1824. WARN_ON_ONCE(cpu != smp_processor_id());
  1825. /*
  1826. * Therefore we have to validate we did not indeed do so.
  1827. */
  1828. if (unlikely(atomic_long_read(&data->done_head))) {
  1829. /*
  1830. * Since we had it locked, we can lock it again.
  1831. */
  1832. while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
  1833. cpu_relax();
  1834. goto again;
  1835. }
  1836. if (atomic_xchg(&data->wakeup, 0))
  1837. perf_output_wakeup(handle);
  1838. out:
  1839. local_irq_restore(handle->flags);
  1840. }
  1841. static int perf_output_begin(struct perf_output_handle *handle,
  1842. struct perf_counter *counter, unsigned int size,
  1843. int nmi, int overflow)
  1844. {
  1845. struct perf_mmap_data *data;
  1846. unsigned int offset, head;
  1847. /*
  1848. * For inherited counters we send all the output towards the parent.
  1849. */
  1850. if (counter->parent)
  1851. counter = counter->parent;
  1852. rcu_read_lock();
  1853. data = rcu_dereference(counter->data);
  1854. if (!data)
  1855. goto out;
  1856. handle->data = data;
  1857. handle->counter = counter;
  1858. handle->nmi = nmi;
  1859. handle->overflow = overflow;
  1860. if (!data->nr_pages)
  1861. goto fail;
  1862. perf_output_lock(handle);
  1863. do {
  1864. offset = head = atomic_long_read(&data->head);
  1865. head += size;
  1866. } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
  1867. handle->offset = offset;
  1868. handle->head = head;
  1869. if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
  1870. atomic_set(&data->wakeup, 1);
  1871. return 0;
  1872. fail:
  1873. perf_output_wakeup(handle);
  1874. out:
  1875. rcu_read_unlock();
  1876. return -ENOSPC;
  1877. }
  1878. static void perf_output_copy(struct perf_output_handle *handle,
  1879. const void *buf, unsigned int len)
  1880. {
  1881. unsigned int pages_mask;
  1882. unsigned int offset;
  1883. unsigned int size;
  1884. void **pages;
  1885. offset = handle->offset;
  1886. pages_mask = handle->data->nr_pages - 1;
  1887. pages = handle->data->data_pages;
  1888. do {
  1889. unsigned int page_offset;
  1890. int nr;
  1891. nr = (offset >> PAGE_SHIFT) & pages_mask;
  1892. page_offset = offset & (PAGE_SIZE - 1);
  1893. size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
  1894. memcpy(pages[nr] + page_offset, buf, size);
  1895. len -= size;
  1896. buf += size;
  1897. offset += size;
  1898. } while (len);
  1899. handle->offset = offset;
  1900. /*
  1901. * Check we didn't copy past our reservation window, taking the
  1902. * possible unsigned int wrap into account.
  1903. */
  1904. WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
  1905. }
  1906. #define perf_output_put(handle, x) \
  1907. perf_output_copy((handle), &(x), sizeof(x))
  1908. static void perf_output_end(struct perf_output_handle *handle)
  1909. {
  1910. struct perf_counter *counter = handle->counter;
  1911. struct perf_mmap_data *data = handle->data;
  1912. int wakeup_events = counter->attr.wakeup_events;
  1913. if (handle->overflow && wakeup_events) {
  1914. int events = atomic_inc_return(&data->events);
  1915. if (events >= wakeup_events) {
  1916. atomic_sub(wakeup_events, &data->events);
  1917. atomic_set(&data->wakeup, 1);
  1918. }
  1919. }
  1920. perf_output_unlock(handle);
  1921. rcu_read_unlock();
  1922. }
  1923. static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
  1924. {
  1925. /*
  1926. * only top level counters have the pid namespace they were created in
  1927. */
  1928. if (counter->parent)
  1929. counter = counter->parent;
  1930. return task_tgid_nr_ns(p, counter->ns);
  1931. }
  1932. static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
  1933. {
  1934. /*
  1935. * only top level counters have the pid namespace they were created in
  1936. */
  1937. if (counter->parent)
  1938. counter = counter->parent;
  1939. return task_pid_nr_ns(p, counter->ns);
  1940. }
  1941. static void perf_counter_output(struct perf_counter *counter,
  1942. int nmi, struct pt_regs *regs, u64 addr)
  1943. {
  1944. int ret;
  1945. u64 sample_type = counter->attr.sample_type;
  1946. struct perf_output_handle handle;
  1947. struct perf_event_header header;
  1948. u64 ip;
  1949. struct {
  1950. u32 pid, tid;
  1951. } tid_entry;
  1952. struct {
  1953. u64 id;
  1954. u64 counter;
  1955. } group_entry;
  1956. struct perf_callchain_entry *callchain = NULL;
  1957. int callchain_size = 0;
  1958. u64 time;
  1959. struct {
  1960. u32 cpu, reserved;
  1961. } cpu_entry;
  1962. header.type = 0;
  1963. header.size = sizeof(header);
  1964. header.misc = PERF_EVENT_MISC_OVERFLOW;
  1965. header.misc |= perf_misc_flags(regs);
  1966. if (sample_type & PERF_SAMPLE_IP) {
  1967. ip = perf_instruction_pointer(regs);
  1968. header.type |= PERF_SAMPLE_IP;
  1969. header.size += sizeof(ip);
  1970. }
  1971. if (sample_type & PERF_SAMPLE_TID) {
  1972. /* namespace issues */
  1973. tid_entry.pid = perf_counter_pid(counter, current);
  1974. tid_entry.tid = perf_counter_tid(counter, current);
  1975. header.type |= PERF_SAMPLE_TID;
  1976. header.size += sizeof(tid_entry);
  1977. }
  1978. if (sample_type & PERF_SAMPLE_TIME) {
  1979. /*
  1980. * Maybe do better on x86 and provide cpu_clock_nmi()
  1981. */
  1982. time = sched_clock();
  1983. header.type |= PERF_SAMPLE_TIME;
  1984. header.size += sizeof(u64);
  1985. }
  1986. if (sample_type & PERF_SAMPLE_ADDR) {
  1987. header.type |= PERF_SAMPLE_ADDR;
  1988. header.size += sizeof(u64);
  1989. }
  1990. if (sample_type & PERF_SAMPLE_ID) {
  1991. header.type |= PERF_SAMPLE_ID;
  1992. header.size += sizeof(u64);
  1993. }
  1994. if (sample_type & PERF_SAMPLE_CPU) {
  1995. header.type |= PERF_SAMPLE_CPU;
  1996. header.size += sizeof(cpu_entry);
  1997. cpu_entry.cpu = raw_smp_processor_id();
  1998. }
  1999. if (sample_type & PERF_SAMPLE_GROUP) {
  2000. header.type |= PERF_SAMPLE_GROUP;
  2001. header.size += sizeof(u64) +
  2002. counter->nr_siblings * sizeof(group_entry);
  2003. }
  2004. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  2005. callchain = perf_callchain(regs);
  2006. if (callchain) {
  2007. callchain_size = (1 + callchain->nr) * sizeof(u64);
  2008. header.type |= PERF_SAMPLE_CALLCHAIN;
  2009. header.size += callchain_size;
  2010. }
  2011. }
  2012. ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
  2013. if (ret)
  2014. return;
  2015. perf_output_put(&handle, header);
  2016. if (sample_type & PERF_SAMPLE_IP)
  2017. perf_output_put(&handle, ip);
  2018. if (sample_type & PERF_SAMPLE_TID)
  2019. perf_output_put(&handle, tid_entry);
  2020. if (sample_type & PERF_SAMPLE_TIME)
  2021. perf_output_put(&handle, time);
  2022. if (sample_type & PERF_SAMPLE_ADDR)
  2023. perf_output_put(&handle, addr);
  2024. if (sample_type & PERF_SAMPLE_ID)
  2025. perf_output_put(&handle, counter->id);
  2026. if (sample_type & PERF_SAMPLE_CPU)
  2027. perf_output_put(&handle, cpu_entry);
  2028. /*
  2029. * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
  2030. */
  2031. if (sample_type & PERF_SAMPLE_GROUP) {
  2032. struct perf_counter *leader, *sub;
  2033. u64 nr = counter->nr_siblings;
  2034. perf_output_put(&handle, nr);
  2035. leader = counter->group_leader;
  2036. list_for_each_entry(sub, &leader->sibling_list, list_entry) {
  2037. if (sub != counter)
  2038. sub->pmu->read(sub);
  2039. group_entry.id = sub->id;
  2040. group_entry.counter = atomic64_read(&sub->count);
  2041. perf_output_put(&handle, group_entry);
  2042. }
  2043. }
  2044. if (callchain)
  2045. perf_output_copy(&handle, callchain, callchain_size);
  2046. perf_output_end(&handle);
  2047. }
  2048. /*
  2049. * fork tracking
  2050. */
  2051. struct perf_fork_event {
  2052. struct task_struct *task;
  2053. struct {
  2054. struct perf_event_header header;
  2055. u32 pid;
  2056. u32 ppid;
  2057. } event;
  2058. };
  2059. static void perf_counter_fork_output(struct perf_counter *counter,
  2060. struct perf_fork_event *fork_event)
  2061. {
  2062. struct perf_output_handle handle;
  2063. int size = fork_event->event.header.size;
  2064. struct task_struct *task = fork_event->task;
  2065. int ret = perf_output_begin(&handle, counter, size, 0, 0);
  2066. if (ret)
  2067. return;
  2068. fork_event->event.pid = perf_counter_pid(counter, task);
  2069. fork_event->event.ppid = perf_counter_pid(counter, task->real_parent);
  2070. perf_output_put(&handle, fork_event->event);
  2071. perf_output_end(&handle);
  2072. }
  2073. static int perf_counter_fork_match(struct perf_counter *counter)
  2074. {
  2075. if (counter->attr.comm || counter->attr.mmap)
  2076. return 1;
  2077. return 0;
  2078. }
  2079. static void perf_counter_fork_ctx(struct perf_counter_context *ctx,
  2080. struct perf_fork_event *fork_event)
  2081. {
  2082. struct perf_counter *counter;
  2083. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  2084. return;
  2085. rcu_read_lock();
  2086. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  2087. if (perf_counter_fork_match(counter))
  2088. perf_counter_fork_output(counter, fork_event);
  2089. }
  2090. rcu_read_unlock();
  2091. }
  2092. static void perf_counter_fork_event(struct perf_fork_event *fork_event)
  2093. {
  2094. struct perf_cpu_context *cpuctx;
  2095. struct perf_counter_context *ctx;
  2096. cpuctx = &get_cpu_var(perf_cpu_context);
  2097. perf_counter_fork_ctx(&cpuctx->ctx, fork_event);
  2098. put_cpu_var(perf_cpu_context);
  2099. rcu_read_lock();
  2100. /*
  2101. * doesn't really matter which of the child contexts the
  2102. * events ends up in.
  2103. */
  2104. ctx = rcu_dereference(current->perf_counter_ctxp);
  2105. if (ctx)
  2106. perf_counter_fork_ctx(ctx, fork_event);
  2107. rcu_read_unlock();
  2108. }
  2109. void perf_counter_fork(struct task_struct *task)
  2110. {
  2111. struct perf_fork_event fork_event;
  2112. if (!atomic_read(&nr_comm_counters) &&
  2113. !atomic_read(&nr_mmap_counters))
  2114. return;
  2115. fork_event = (struct perf_fork_event){
  2116. .task = task,
  2117. .event = {
  2118. .header = {
  2119. .type = PERF_EVENT_FORK,
  2120. .size = sizeof(fork_event.event),
  2121. },
  2122. },
  2123. };
  2124. perf_counter_fork_event(&fork_event);
  2125. }
  2126. /*
  2127. * comm tracking
  2128. */
  2129. struct perf_comm_event {
  2130. struct task_struct *task;
  2131. char *comm;
  2132. int comm_size;
  2133. struct {
  2134. struct perf_event_header header;
  2135. u32 pid;
  2136. u32 tid;
  2137. } event;
  2138. };
  2139. static void perf_counter_comm_output(struct perf_counter *counter,
  2140. struct perf_comm_event *comm_event)
  2141. {
  2142. struct perf_output_handle handle;
  2143. int size = comm_event->event.header.size;
  2144. int ret = perf_output_begin(&handle, counter, size, 0, 0);
  2145. if (ret)
  2146. return;
  2147. comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
  2148. comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
  2149. perf_output_put(&handle, comm_event->event);
  2150. perf_output_copy(&handle, comm_event->comm,
  2151. comm_event->comm_size);
  2152. perf_output_end(&handle);
  2153. }
  2154. static int perf_counter_comm_match(struct perf_counter *counter)
  2155. {
  2156. if (counter->attr.comm)
  2157. return 1;
  2158. return 0;
  2159. }
  2160. static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
  2161. struct perf_comm_event *comm_event)
  2162. {
  2163. struct perf_counter *counter;
  2164. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  2165. return;
  2166. rcu_read_lock();
  2167. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  2168. if (perf_counter_comm_match(counter))
  2169. perf_counter_comm_output(counter, comm_event);
  2170. }
  2171. rcu_read_unlock();
  2172. }
  2173. static void perf_counter_comm_event(struct perf_comm_event *comm_event)
  2174. {
  2175. struct perf_cpu_context *cpuctx;
  2176. struct perf_counter_context *ctx;
  2177. unsigned int size;
  2178. char *comm = comm_event->task->comm;
  2179. size = ALIGN(strlen(comm)+1, sizeof(u64));
  2180. comm_event->comm = comm;
  2181. comm_event->comm_size = size;
  2182. comm_event->event.header.size = sizeof(comm_event->event) + size;
  2183. cpuctx = &get_cpu_var(perf_cpu_context);
  2184. perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
  2185. put_cpu_var(perf_cpu_context);
  2186. rcu_read_lock();
  2187. /*
  2188. * doesn't really matter which of the child contexts the
  2189. * events ends up in.
  2190. */
  2191. ctx = rcu_dereference(current->perf_counter_ctxp);
  2192. if (ctx)
  2193. perf_counter_comm_ctx(ctx, comm_event);
  2194. rcu_read_unlock();
  2195. }
  2196. void perf_counter_comm(struct task_struct *task)
  2197. {
  2198. struct perf_comm_event comm_event;
  2199. if (!atomic_read(&nr_comm_counters))
  2200. return;
  2201. comm_event = (struct perf_comm_event){
  2202. .task = task,
  2203. .event = {
  2204. .header = { .type = PERF_EVENT_COMM, },
  2205. },
  2206. };
  2207. perf_counter_comm_event(&comm_event);
  2208. }
  2209. /*
  2210. * mmap tracking
  2211. */
  2212. struct perf_mmap_event {
  2213. struct vm_area_struct *vma;
  2214. const char *file_name;
  2215. int file_size;
  2216. struct {
  2217. struct perf_event_header header;
  2218. u32 pid;
  2219. u32 tid;
  2220. u64 start;
  2221. u64 len;
  2222. u64 pgoff;
  2223. } event;
  2224. };
  2225. static void perf_counter_mmap_output(struct perf_counter *counter,
  2226. struct perf_mmap_event *mmap_event)
  2227. {
  2228. struct perf_output_handle handle;
  2229. int size = mmap_event->event.header.size;
  2230. int ret = perf_output_begin(&handle, counter, size, 0, 0);
  2231. if (ret)
  2232. return;
  2233. mmap_event->event.pid = perf_counter_pid(counter, current);
  2234. mmap_event->event.tid = perf_counter_tid(counter, current);
  2235. perf_output_put(&handle, mmap_event->event);
  2236. perf_output_copy(&handle, mmap_event->file_name,
  2237. mmap_event->file_size);
  2238. perf_output_end(&handle);
  2239. }
  2240. static int perf_counter_mmap_match(struct perf_counter *counter,
  2241. struct perf_mmap_event *mmap_event)
  2242. {
  2243. if (counter->attr.mmap)
  2244. return 1;
  2245. return 0;
  2246. }
  2247. static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
  2248. struct perf_mmap_event *mmap_event)
  2249. {
  2250. struct perf_counter *counter;
  2251. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  2252. return;
  2253. rcu_read_lock();
  2254. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  2255. if (perf_counter_mmap_match(counter, mmap_event))
  2256. perf_counter_mmap_output(counter, mmap_event);
  2257. }
  2258. rcu_read_unlock();
  2259. }
  2260. static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
  2261. {
  2262. struct perf_cpu_context *cpuctx;
  2263. struct perf_counter_context *ctx;
  2264. struct vm_area_struct *vma = mmap_event->vma;
  2265. struct file *file = vma->vm_file;
  2266. unsigned int size;
  2267. char tmp[16];
  2268. char *buf = NULL;
  2269. const char *name;
  2270. if (file) {
  2271. buf = kzalloc(PATH_MAX, GFP_KERNEL);
  2272. if (!buf) {
  2273. name = strncpy(tmp, "//enomem", sizeof(tmp));
  2274. goto got_name;
  2275. }
  2276. name = d_path(&file->f_path, buf, PATH_MAX);
  2277. if (IS_ERR(name)) {
  2278. name = strncpy(tmp, "//toolong", sizeof(tmp));
  2279. goto got_name;
  2280. }
  2281. } else {
  2282. name = arch_vma_name(mmap_event->vma);
  2283. if (name)
  2284. goto got_name;
  2285. if (!vma->vm_mm) {
  2286. name = strncpy(tmp, "[vdso]", sizeof(tmp));
  2287. goto got_name;
  2288. }
  2289. name = strncpy(tmp, "//anon", sizeof(tmp));
  2290. goto got_name;
  2291. }
  2292. got_name:
  2293. size = ALIGN(strlen(name)+1, sizeof(u64));
  2294. mmap_event->file_name = name;
  2295. mmap_event->file_size = size;
  2296. mmap_event->event.header.size = sizeof(mmap_event->event) + size;
  2297. cpuctx = &get_cpu_var(perf_cpu_context);
  2298. perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
  2299. put_cpu_var(perf_cpu_context);
  2300. rcu_read_lock();
  2301. /*
  2302. * doesn't really matter which of the child contexts the
  2303. * events ends up in.
  2304. */
  2305. ctx = rcu_dereference(current->perf_counter_ctxp);
  2306. if (ctx)
  2307. perf_counter_mmap_ctx(ctx, mmap_event);
  2308. rcu_read_unlock();
  2309. kfree(buf);
  2310. }
  2311. void __perf_counter_mmap(struct vm_area_struct *vma)
  2312. {
  2313. struct perf_mmap_event mmap_event;
  2314. if (!atomic_read(&nr_mmap_counters))
  2315. return;
  2316. mmap_event = (struct perf_mmap_event){
  2317. .vma = vma,
  2318. .event = {
  2319. .header = { .type = PERF_EVENT_MMAP, },
  2320. .start = vma->vm_start,
  2321. .len = vma->vm_end - vma->vm_start,
  2322. .pgoff = vma->vm_pgoff,
  2323. },
  2324. };
  2325. perf_counter_mmap_event(&mmap_event);
  2326. }
  2327. /*
  2328. * Log sample_period changes so that analyzing tools can re-normalize the
  2329. * event flow.
  2330. */
  2331. static void perf_log_period(struct perf_counter *counter, u64 period)
  2332. {
  2333. struct perf_output_handle handle;
  2334. int ret;
  2335. struct {
  2336. struct perf_event_header header;
  2337. u64 time;
  2338. u64 period;
  2339. } freq_event = {
  2340. .header = {
  2341. .type = PERF_EVENT_PERIOD,
  2342. .misc = 0,
  2343. .size = sizeof(freq_event),
  2344. },
  2345. .time = sched_clock(),
  2346. .period = period,
  2347. };
  2348. if (counter->hw.sample_period == period)
  2349. return;
  2350. ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
  2351. if (ret)
  2352. return;
  2353. perf_output_put(&handle, freq_event);
  2354. perf_output_end(&handle);
  2355. }
  2356. /*
  2357. * IRQ throttle logging
  2358. */
  2359. static void perf_log_throttle(struct perf_counter *counter, int enable)
  2360. {
  2361. struct perf_output_handle handle;
  2362. int ret;
  2363. struct {
  2364. struct perf_event_header header;
  2365. u64 time;
  2366. } throttle_event = {
  2367. .header = {
  2368. .type = PERF_EVENT_THROTTLE + 1,
  2369. .misc = 0,
  2370. .size = sizeof(throttle_event),
  2371. },
  2372. .time = sched_clock(),
  2373. };
  2374. ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
  2375. if (ret)
  2376. return;
  2377. perf_output_put(&handle, throttle_event);
  2378. perf_output_end(&handle);
  2379. }
  2380. /*
  2381. * Generic counter overflow handling.
  2382. */
  2383. int perf_counter_overflow(struct perf_counter *counter,
  2384. int nmi, struct pt_regs *regs, u64 addr)
  2385. {
  2386. int events = atomic_read(&counter->event_limit);
  2387. int throttle = counter->pmu->unthrottle != NULL;
  2388. int ret = 0;
  2389. if (!throttle) {
  2390. counter->hw.interrupts++;
  2391. } else {
  2392. if (counter->hw.interrupts != MAX_INTERRUPTS) {
  2393. counter->hw.interrupts++;
  2394. if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
  2395. counter->hw.interrupts = MAX_INTERRUPTS;
  2396. perf_log_throttle(counter, 0);
  2397. ret = 1;
  2398. }
  2399. } else {
  2400. /*
  2401. * Keep re-disabling counters even though on the previous
  2402. * pass we disabled it - just in case we raced with a
  2403. * sched-in and the counter got enabled again:
  2404. */
  2405. ret = 1;
  2406. }
  2407. }
  2408. /*
  2409. * XXX event_limit might not quite work as expected on inherited
  2410. * counters
  2411. */
  2412. counter->pending_kill = POLL_IN;
  2413. if (events && atomic_dec_and_test(&counter->event_limit)) {
  2414. ret = 1;
  2415. counter->pending_kill = POLL_HUP;
  2416. if (nmi) {
  2417. counter->pending_disable = 1;
  2418. perf_pending_queue(&counter->pending,
  2419. perf_pending_counter);
  2420. } else
  2421. perf_counter_disable(counter);
  2422. }
  2423. perf_counter_output(counter, nmi, regs, addr);
  2424. return ret;
  2425. }
  2426. /*
  2427. * Generic software counter infrastructure
  2428. */
  2429. static void perf_swcounter_update(struct perf_counter *counter)
  2430. {
  2431. struct hw_perf_counter *hwc = &counter->hw;
  2432. u64 prev, now;
  2433. s64 delta;
  2434. again:
  2435. prev = atomic64_read(&hwc->prev_count);
  2436. now = atomic64_read(&hwc->count);
  2437. if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
  2438. goto again;
  2439. delta = now - prev;
  2440. atomic64_add(delta, &counter->count);
  2441. atomic64_sub(delta, &hwc->period_left);
  2442. }
  2443. static void perf_swcounter_set_period(struct perf_counter *counter)
  2444. {
  2445. struct hw_perf_counter *hwc = &counter->hw;
  2446. s64 left = atomic64_read(&hwc->period_left);
  2447. s64 period = hwc->sample_period;
  2448. if (unlikely(left <= -period)) {
  2449. left = period;
  2450. atomic64_set(&hwc->period_left, left);
  2451. }
  2452. if (unlikely(left <= 0)) {
  2453. left += period;
  2454. atomic64_add(period, &hwc->period_left);
  2455. }
  2456. atomic64_set(&hwc->prev_count, -left);
  2457. atomic64_set(&hwc->count, -left);
  2458. }
  2459. static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
  2460. {
  2461. enum hrtimer_restart ret = HRTIMER_RESTART;
  2462. struct perf_counter *counter;
  2463. struct pt_regs *regs;
  2464. u64 period;
  2465. counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
  2466. counter->pmu->read(counter);
  2467. regs = get_irq_regs();
  2468. /*
  2469. * In case we exclude kernel IPs or are somehow not in interrupt
  2470. * context, provide the next best thing, the user IP.
  2471. */
  2472. if ((counter->attr.exclude_kernel || !regs) &&
  2473. !counter->attr.exclude_user)
  2474. regs = task_pt_regs(current);
  2475. if (regs) {
  2476. if (perf_counter_overflow(counter, 0, regs, 0))
  2477. ret = HRTIMER_NORESTART;
  2478. }
  2479. period = max_t(u64, 10000, counter->hw.sample_period);
  2480. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  2481. return ret;
  2482. }
  2483. static void perf_swcounter_overflow(struct perf_counter *counter,
  2484. int nmi, struct pt_regs *regs, u64 addr)
  2485. {
  2486. perf_swcounter_update(counter);
  2487. perf_swcounter_set_period(counter);
  2488. if (perf_counter_overflow(counter, nmi, regs, addr))
  2489. /* soft-disable the counter */
  2490. ;
  2491. }
  2492. static int perf_swcounter_is_counting(struct perf_counter *counter)
  2493. {
  2494. struct perf_counter_context *ctx;
  2495. unsigned long flags;
  2496. int count;
  2497. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  2498. return 1;
  2499. if (counter->state != PERF_COUNTER_STATE_INACTIVE)
  2500. return 0;
  2501. /*
  2502. * If the counter is inactive, it could be just because
  2503. * its task is scheduled out, or because it's in a group
  2504. * which could not go on the PMU. We want to count in
  2505. * the first case but not the second. If the context is
  2506. * currently active then an inactive software counter must
  2507. * be the second case. If it's not currently active then
  2508. * we need to know whether the counter was active when the
  2509. * context was last active, which we can determine by
  2510. * comparing counter->tstamp_stopped with ctx->time.
  2511. *
  2512. * We are within an RCU read-side critical section,
  2513. * which protects the existence of *ctx.
  2514. */
  2515. ctx = counter->ctx;
  2516. spin_lock_irqsave(&ctx->lock, flags);
  2517. count = 1;
  2518. /* Re-check state now we have the lock */
  2519. if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
  2520. counter->ctx->is_active ||
  2521. counter->tstamp_stopped < ctx->time)
  2522. count = 0;
  2523. spin_unlock_irqrestore(&ctx->lock, flags);
  2524. return count;
  2525. }
  2526. static int perf_swcounter_match(struct perf_counter *counter,
  2527. enum perf_event_types type,
  2528. u32 event, struct pt_regs *regs)
  2529. {
  2530. u64 event_config;
  2531. event_config = ((u64) type << PERF_COUNTER_TYPE_SHIFT) | event;
  2532. if (!perf_swcounter_is_counting(counter))
  2533. return 0;
  2534. if (counter->attr.config != event_config)
  2535. return 0;
  2536. if (regs) {
  2537. if (counter->attr.exclude_user && user_mode(regs))
  2538. return 0;
  2539. if (counter->attr.exclude_kernel && !user_mode(regs))
  2540. return 0;
  2541. }
  2542. return 1;
  2543. }
  2544. static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
  2545. int nmi, struct pt_regs *regs, u64 addr)
  2546. {
  2547. int neg = atomic64_add_negative(nr, &counter->hw.count);
  2548. if (counter->hw.sample_period && !neg && regs)
  2549. perf_swcounter_overflow(counter, nmi, regs, addr);
  2550. }
  2551. static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
  2552. enum perf_event_types type, u32 event,
  2553. u64 nr, int nmi, struct pt_regs *regs,
  2554. u64 addr)
  2555. {
  2556. struct perf_counter *counter;
  2557. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  2558. return;
  2559. rcu_read_lock();
  2560. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  2561. if (perf_swcounter_match(counter, type, event, regs))
  2562. perf_swcounter_add(counter, nr, nmi, regs, addr);
  2563. }
  2564. rcu_read_unlock();
  2565. }
  2566. static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
  2567. {
  2568. if (in_nmi())
  2569. return &cpuctx->recursion[3];
  2570. if (in_irq())
  2571. return &cpuctx->recursion[2];
  2572. if (in_softirq())
  2573. return &cpuctx->recursion[1];
  2574. return &cpuctx->recursion[0];
  2575. }
  2576. static void __perf_swcounter_event(enum perf_event_types type, u32 event,
  2577. u64 nr, int nmi, struct pt_regs *regs,
  2578. u64 addr)
  2579. {
  2580. struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
  2581. int *recursion = perf_swcounter_recursion_context(cpuctx);
  2582. struct perf_counter_context *ctx;
  2583. if (*recursion)
  2584. goto out;
  2585. (*recursion)++;
  2586. barrier();
  2587. perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
  2588. nr, nmi, regs, addr);
  2589. rcu_read_lock();
  2590. /*
  2591. * doesn't really matter which of the child contexts the
  2592. * events ends up in.
  2593. */
  2594. ctx = rcu_dereference(current->perf_counter_ctxp);
  2595. if (ctx)
  2596. perf_swcounter_ctx_event(ctx, type, event, nr, nmi, regs, addr);
  2597. rcu_read_unlock();
  2598. barrier();
  2599. (*recursion)--;
  2600. out:
  2601. put_cpu_var(perf_cpu_context);
  2602. }
  2603. void
  2604. perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
  2605. {
  2606. __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
  2607. }
  2608. static void perf_swcounter_read(struct perf_counter *counter)
  2609. {
  2610. perf_swcounter_update(counter);
  2611. }
  2612. static int perf_swcounter_enable(struct perf_counter *counter)
  2613. {
  2614. perf_swcounter_set_period(counter);
  2615. return 0;
  2616. }
  2617. static void perf_swcounter_disable(struct perf_counter *counter)
  2618. {
  2619. perf_swcounter_update(counter);
  2620. }
  2621. static const struct pmu perf_ops_generic = {
  2622. .enable = perf_swcounter_enable,
  2623. .disable = perf_swcounter_disable,
  2624. .read = perf_swcounter_read,
  2625. };
  2626. /*
  2627. * Software counter: cpu wall time clock
  2628. */
  2629. static void cpu_clock_perf_counter_update(struct perf_counter *counter)
  2630. {
  2631. int cpu = raw_smp_processor_id();
  2632. s64 prev;
  2633. u64 now;
  2634. now = cpu_clock(cpu);
  2635. prev = atomic64_read(&counter->hw.prev_count);
  2636. atomic64_set(&counter->hw.prev_count, now);
  2637. atomic64_add(now - prev, &counter->count);
  2638. }
  2639. static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
  2640. {
  2641. struct hw_perf_counter *hwc = &counter->hw;
  2642. int cpu = raw_smp_processor_id();
  2643. atomic64_set(&hwc->prev_count, cpu_clock(cpu));
  2644. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2645. hwc->hrtimer.function = perf_swcounter_hrtimer;
  2646. if (hwc->sample_period) {
  2647. u64 period = max_t(u64, 10000, hwc->sample_period);
  2648. __hrtimer_start_range_ns(&hwc->hrtimer,
  2649. ns_to_ktime(period), 0,
  2650. HRTIMER_MODE_REL, 0);
  2651. }
  2652. return 0;
  2653. }
  2654. static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
  2655. {
  2656. if (counter->hw.sample_period)
  2657. hrtimer_cancel(&counter->hw.hrtimer);
  2658. cpu_clock_perf_counter_update(counter);
  2659. }
  2660. static void cpu_clock_perf_counter_read(struct perf_counter *counter)
  2661. {
  2662. cpu_clock_perf_counter_update(counter);
  2663. }
  2664. static const struct pmu perf_ops_cpu_clock = {
  2665. .enable = cpu_clock_perf_counter_enable,
  2666. .disable = cpu_clock_perf_counter_disable,
  2667. .read = cpu_clock_perf_counter_read,
  2668. };
  2669. /*
  2670. * Software counter: task time clock
  2671. */
  2672. static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
  2673. {
  2674. u64 prev;
  2675. s64 delta;
  2676. prev = atomic64_xchg(&counter->hw.prev_count, now);
  2677. delta = now - prev;
  2678. atomic64_add(delta, &counter->count);
  2679. }
  2680. static int task_clock_perf_counter_enable(struct perf_counter *counter)
  2681. {
  2682. struct hw_perf_counter *hwc = &counter->hw;
  2683. u64 now;
  2684. now = counter->ctx->time;
  2685. atomic64_set(&hwc->prev_count, now);
  2686. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2687. hwc->hrtimer.function = perf_swcounter_hrtimer;
  2688. if (hwc->sample_period) {
  2689. u64 period = max_t(u64, 10000, hwc->sample_period);
  2690. __hrtimer_start_range_ns(&hwc->hrtimer,
  2691. ns_to_ktime(period), 0,
  2692. HRTIMER_MODE_REL, 0);
  2693. }
  2694. return 0;
  2695. }
  2696. static void task_clock_perf_counter_disable(struct perf_counter *counter)
  2697. {
  2698. if (counter->hw.sample_period)
  2699. hrtimer_cancel(&counter->hw.hrtimer);
  2700. task_clock_perf_counter_update(counter, counter->ctx->time);
  2701. }
  2702. static void task_clock_perf_counter_read(struct perf_counter *counter)
  2703. {
  2704. u64 time;
  2705. if (!in_nmi()) {
  2706. update_context_time(counter->ctx);
  2707. time = counter->ctx->time;
  2708. } else {
  2709. u64 now = perf_clock();
  2710. u64 delta = now - counter->ctx->timestamp;
  2711. time = counter->ctx->time + delta;
  2712. }
  2713. task_clock_perf_counter_update(counter, time);
  2714. }
  2715. static const struct pmu perf_ops_task_clock = {
  2716. .enable = task_clock_perf_counter_enable,
  2717. .disable = task_clock_perf_counter_disable,
  2718. .read = task_clock_perf_counter_read,
  2719. };
  2720. /*
  2721. * Software counter: cpu migrations
  2722. */
  2723. void perf_counter_task_migration(struct task_struct *task, int cpu)
  2724. {
  2725. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  2726. struct perf_counter_context *ctx;
  2727. perf_swcounter_ctx_event(&cpuctx->ctx, PERF_TYPE_SOFTWARE,
  2728. PERF_COUNT_CPU_MIGRATIONS,
  2729. 1, 1, NULL, 0);
  2730. ctx = perf_pin_task_context(task);
  2731. if (ctx) {
  2732. perf_swcounter_ctx_event(ctx, PERF_TYPE_SOFTWARE,
  2733. PERF_COUNT_CPU_MIGRATIONS,
  2734. 1, 1, NULL, 0);
  2735. perf_unpin_context(ctx);
  2736. }
  2737. }
  2738. #ifdef CONFIG_EVENT_PROFILE
  2739. void perf_tpcounter_event(int event_id)
  2740. {
  2741. struct pt_regs *regs = get_irq_regs();
  2742. if (!regs)
  2743. regs = task_pt_regs(current);
  2744. __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
  2745. }
  2746. EXPORT_SYMBOL_GPL(perf_tpcounter_event);
  2747. extern int ftrace_profile_enable(int);
  2748. extern void ftrace_profile_disable(int);
  2749. static void tp_perf_counter_destroy(struct perf_counter *counter)
  2750. {
  2751. ftrace_profile_disable(perf_event_id(&counter->attr));
  2752. }
  2753. static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
  2754. {
  2755. int event_id = perf_event_id(&counter->attr);
  2756. int ret;
  2757. ret = ftrace_profile_enable(event_id);
  2758. if (ret)
  2759. return NULL;
  2760. counter->destroy = tp_perf_counter_destroy;
  2761. counter->hw.sample_period = counter->attr.sample_period;
  2762. return &perf_ops_generic;
  2763. }
  2764. #else
  2765. static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
  2766. {
  2767. return NULL;
  2768. }
  2769. #endif
  2770. static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
  2771. {
  2772. const struct pmu *pmu = NULL;
  2773. /*
  2774. * Software counters (currently) can't in general distinguish
  2775. * between user, kernel and hypervisor events.
  2776. * However, context switches and cpu migrations are considered
  2777. * to be kernel events, and page faults are never hypervisor
  2778. * events.
  2779. */
  2780. switch (perf_event_id(&counter->attr)) {
  2781. case PERF_COUNT_CPU_CLOCK:
  2782. pmu = &perf_ops_cpu_clock;
  2783. break;
  2784. case PERF_COUNT_TASK_CLOCK:
  2785. /*
  2786. * If the user instantiates this as a per-cpu counter,
  2787. * use the cpu_clock counter instead.
  2788. */
  2789. if (counter->ctx->task)
  2790. pmu = &perf_ops_task_clock;
  2791. else
  2792. pmu = &perf_ops_cpu_clock;
  2793. break;
  2794. case PERF_COUNT_PAGE_FAULTS:
  2795. case PERF_COUNT_PAGE_FAULTS_MIN:
  2796. case PERF_COUNT_PAGE_FAULTS_MAJ:
  2797. case PERF_COUNT_CONTEXT_SWITCHES:
  2798. case PERF_COUNT_CPU_MIGRATIONS:
  2799. pmu = &perf_ops_generic;
  2800. break;
  2801. }
  2802. return pmu;
  2803. }
  2804. /*
  2805. * Allocate and initialize a counter structure
  2806. */
  2807. static struct perf_counter *
  2808. perf_counter_alloc(struct perf_counter_attr *attr,
  2809. int cpu,
  2810. struct perf_counter_context *ctx,
  2811. struct perf_counter *group_leader,
  2812. gfp_t gfpflags)
  2813. {
  2814. const struct pmu *pmu;
  2815. struct perf_counter *counter;
  2816. struct hw_perf_counter *hwc;
  2817. long err;
  2818. counter = kzalloc(sizeof(*counter), gfpflags);
  2819. if (!counter)
  2820. return ERR_PTR(-ENOMEM);
  2821. /*
  2822. * Single counters are their own group leaders, with an
  2823. * empty sibling list:
  2824. */
  2825. if (!group_leader)
  2826. group_leader = counter;
  2827. mutex_init(&counter->child_mutex);
  2828. INIT_LIST_HEAD(&counter->child_list);
  2829. INIT_LIST_HEAD(&counter->list_entry);
  2830. INIT_LIST_HEAD(&counter->event_entry);
  2831. INIT_LIST_HEAD(&counter->sibling_list);
  2832. init_waitqueue_head(&counter->waitq);
  2833. mutex_init(&counter->mmap_mutex);
  2834. counter->cpu = cpu;
  2835. counter->attr = *attr;
  2836. counter->group_leader = group_leader;
  2837. counter->pmu = NULL;
  2838. counter->ctx = ctx;
  2839. counter->oncpu = -1;
  2840. counter->ns = get_pid_ns(current->nsproxy->pid_ns);
  2841. counter->id = atomic64_inc_return(&perf_counter_id);
  2842. counter->state = PERF_COUNTER_STATE_INACTIVE;
  2843. if (attr->disabled)
  2844. counter->state = PERF_COUNTER_STATE_OFF;
  2845. pmu = NULL;
  2846. hwc = &counter->hw;
  2847. if (attr->freq && attr->sample_freq)
  2848. hwc->sample_period = div64_u64(TICK_NSEC, attr->sample_freq);
  2849. else
  2850. hwc->sample_period = attr->sample_period;
  2851. /*
  2852. * we currently do not support PERF_SAMPLE_GROUP on inherited counters
  2853. */
  2854. if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP))
  2855. goto done;
  2856. if (perf_event_raw(attr)) {
  2857. pmu = hw_perf_counter_init(counter);
  2858. goto done;
  2859. }
  2860. switch (perf_event_type(attr)) {
  2861. case PERF_TYPE_HARDWARE:
  2862. pmu = hw_perf_counter_init(counter);
  2863. break;
  2864. case PERF_TYPE_SOFTWARE:
  2865. pmu = sw_perf_counter_init(counter);
  2866. break;
  2867. case PERF_TYPE_TRACEPOINT:
  2868. pmu = tp_perf_counter_init(counter);
  2869. break;
  2870. }
  2871. done:
  2872. err = 0;
  2873. if (!pmu)
  2874. err = -EINVAL;
  2875. else if (IS_ERR(pmu))
  2876. err = PTR_ERR(pmu);
  2877. if (err) {
  2878. if (counter->ns)
  2879. put_pid_ns(counter->ns);
  2880. kfree(counter);
  2881. return ERR_PTR(err);
  2882. }
  2883. counter->pmu = pmu;
  2884. atomic_inc(&nr_counters);
  2885. if (counter->attr.mmap)
  2886. atomic_inc(&nr_mmap_counters);
  2887. if (counter->attr.comm)
  2888. atomic_inc(&nr_comm_counters);
  2889. return counter;
  2890. }
  2891. /**
  2892. * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
  2893. *
  2894. * @attr_uptr: event type attributes for monitoring/sampling
  2895. * @pid: target pid
  2896. * @cpu: target cpu
  2897. * @group_fd: group leader counter fd
  2898. */
  2899. SYSCALL_DEFINE5(perf_counter_open,
  2900. const struct perf_counter_attr __user *, attr_uptr,
  2901. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  2902. {
  2903. struct perf_counter *counter, *group_leader;
  2904. struct perf_counter_attr attr;
  2905. struct perf_counter_context *ctx;
  2906. struct file *counter_file = NULL;
  2907. struct file *group_file = NULL;
  2908. int fput_needed = 0;
  2909. int fput_needed2 = 0;
  2910. int ret;
  2911. /* for future expandability... */
  2912. if (flags)
  2913. return -EINVAL;
  2914. if (copy_from_user(&attr, attr_uptr, sizeof(attr)) != 0)
  2915. return -EFAULT;
  2916. /*
  2917. * Get the target context (task or percpu):
  2918. */
  2919. ctx = find_get_context(pid, cpu);
  2920. if (IS_ERR(ctx))
  2921. return PTR_ERR(ctx);
  2922. /*
  2923. * Look up the group leader (we will attach this counter to it):
  2924. */
  2925. group_leader = NULL;
  2926. if (group_fd != -1) {
  2927. ret = -EINVAL;
  2928. group_file = fget_light(group_fd, &fput_needed);
  2929. if (!group_file)
  2930. goto err_put_context;
  2931. if (group_file->f_op != &perf_fops)
  2932. goto err_put_context;
  2933. group_leader = group_file->private_data;
  2934. /*
  2935. * Do not allow a recursive hierarchy (this new sibling
  2936. * becoming part of another group-sibling):
  2937. */
  2938. if (group_leader->group_leader != group_leader)
  2939. goto err_put_context;
  2940. /*
  2941. * Do not allow to attach to a group in a different
  2942. * task or CPU context:
  2943. */
  2944. if (group_leader->ctx != ctx)
  2945. goto err_put_context;
  2946. /*
  2947. * Only a group leader can be exclusive or pinned
  2948. */
  2949. if (attr.exclusive || attr.pinned)
  2950. goto err_put_context;
  2951. }
  2952. counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
  2953. GFP_KERNEL);
  2954. ret = PTR_ERR(counter);
  2955. if (IS_ERR(counter))
  2956. goto err_put_context;
  2957. ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
  2958. if (ret < 0)
  2959. goto err_free_put_context;
  2960. counter_file = fget_light(ret, &fput_needed2);
  2961. if (!counter_file)
  2962. goto err_free_put_context;
  2963. counter->filp = counter_file;
  2964. WARN_ON_ONCE(ctx->parent_ctx);
  2965. mutex_lock(&ctx->mutex);
  2966. perf_install_in_context(ctx, counter, cpu);
  2967. ++ctx->generation;
  2968. mutex_unlock(&ctx->mutex);
  2969. counter->owner = current;
  2970. get_task_struct(current);
  2971. mutex_lock(&current->perf_counter_mutex);
  2972. list_add_tail(&counter->owner_entry, &current->perf_counter_list);
  2973. mutex_unlock(&current->perf_counter_mutex);
  2974. fput_light(counter_file, fput_needed2);
  2975. out_fput:
  2976. fput_light(group_file, fput_needed);
  2977. return ret;
  2978. err_free_put_context:
  2979. kfree(counter);
  2980. err_put_context:
  2981. put_ctx(ctx);
  2982. goto out_fput;
  2983. }
  2984. /*
  2985. * inherit a counter from parent task to child task:
  2986. */
  2987. static struct perf_counter *
  2988. inherit_counter(struct perf_counter *parent_counter,
  2989. struct task_struct *parent,
  2990. struct perf_counter_context *parent_ctx,
  2991. struct task_struct *child,
  2992. struct perf_counter *group_leader,
  2993. struct perf_counter_context *child_ctx)
  2994. {
  2995. struct perf_counter *child_counter;
  2996. /*
  2997. * Instead of creating recursive hierarchies of counters,
  2998. * we link inherited counters back to the original parent,
  2999. * which has a filp for sure, which we use as the reference
  3000. * count:
  3001. */
  3002. if (parent_counter->parent)
  3003. parent_counter = parent_counter->parent;
  3004. child_counter = perf_counter_alloc(&parent_counter->attr,
  3005. parent_counter->cpu, child_ctx,
  3006. group_leader, GFP_KERNEL);
  3007. if (IS_ERR(child_counter))
  3008. return child_counter;
  3009. get_ctx(child_ctx);
  3010. /*
  3011. * Make the child state follow the state of the parent counter,
  3012. * not its attr.disabled bit. We hold the parent's mutex,
  3013. * so we won't race with perf_counter_{en, dis}able_family.
  3014. */
  3015. if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
  3016. child_counter->state = PERF_COUNTER_STATE_INACTIVE;
  3017. else
  3018. child_counter->state = PERF_COUNTER_STATE_OFF;
  3019. /*
  3020. * Link it up in the child's context:
  3021. */
  3022. add_counter_to_ctx(child_counter, child_ctx);
  3023. child_counter->parent = parent_counter;
  3024. /*
  3025. * inherit into child's child as well:
  3026. */
  3027. child_counter->attr.inherit = 1;
  3028. /*
  3029. * Get a reference to the parent filp - we will fput it
  3030. * when the child counter exits. This is safe to do because
  3031. * we are in the parent and we know that the filp still
  3032. * exists and has a nonzero count:
  3033. */
  3034. atomic_long_inc(&parent_counter->filp->f_count);
  3035. /*
  3036. * Link this into the parent counter's child list
  3037. */
  3038. WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
  3039. mutex_lock(&parent_counter->child_mutex);
  3040. list_add_tail(&child_counter->child_list, &parent_counter->child_list);
  3041. mutex_unlock(&parent_counter->child_mutex);
  3042. return child_counter;
  3043. }
  3044. static int inherit_group(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_context *child_ctx)
  3049. {
  3050. struct perf_counter *leader;
  3051. struct perf_counter *sub;
  3052. struct perf_counter *child_ctr;
  3053. leader = inherit_counter(parent_counter, parent, parent_ctx,
  3054. child, NULL, child_ctx);
  3055. if (IS_ERR(leader))
  3056. return PTR_ERR(leader);
  3057. list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
  3058. child_ctr = inherit_counter(sub, parent, parent_ctx,
  3059. child, leader, child_ctx);
  3060. if (IS_ERR(child_ctr))
  3061. return PTR_ERR(child_ctr);
  3062. }
  3063. return 0;
  3064. }
  3065. static void sync_child_counter(struct perf_counter *child_counter,
  3066. struct perf_counter *parent_counter)
  3067. {
  3068. u64 child_val;
  3069. child_val = atomic64_read(&child_counter->count);
  3070. /*
  3071. * Add back the child's count to the parent's count:
  3072. */
  3073. atomic64_add(child_val, &parent_counter->count);
  3074. atomic64_add(child_counter->total_time_enabled,
  3075. &parent_counter->child_total_time_enabled);
  3076. atomic64_add(child_counter->total_time_running,
  3077. &parent_counter->child_total_time_running);
  3078. /*
  3079. * Remove this counter from the parent's list
  3080. */
  3081. WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
  3082. mutex_lock(&parent_counter->child_mutex);
  3083. list_del_init(&child_counter->child_list);
  3084. mutex_unlock(&parent_counter->child_mutex);
  3085. /*
  3086. * Release the parent counter, if this was the last
  3087. * reference to it.
  3088. */
  3089. fput(parent_counter->filp);
  3090. }
  3091. static void
  3092. __perf_counter_exit_task(struct perf_counter *child_counter,
  3093. struct perf_counter_context *child_ctx)
  3094. {
  3095. struct perf_counter *parent_counter;
  3096. update_counter_times(child_counter);
  3097. perf_counter_remove_from_context(child_counter);
  3098. parent_counter = child_counter->parent;
  3099. /*
  3100. * It can happen that parent exits first, and has counters
  3101. * that are still around due to the child reference. These
  3102. * counters need to be zapped - but otherwise linger.
  3103. */
  3104. if (parent_counter) {
  3105. sync_child_counter(child_counter, parent_counter);
  3106. free_counter(child_counter);
  3107. }
  3108. }
  3109. /*
  3110. * When a child task exits, feed back counter values to parent counters.
  3111. */
  3112. void perf_counter_exit_task(struct task_struct *child)
  3113. {
  3114. struct perf_counter *child_counter, *tmp;
  3115. struct perf_counter_context *child_ctx;
  3116. unsigned long flags;
  3117. if (likely(!child->perf_counter_ctxp))
  3118. return;
  3119. local_irq_save(flags);
  3120. /*
  3121. * We can't reschedule here because interrupts are disabled,
  3122. * and either child is current or it is a task that can't be
  3123. * scheduled, so we are now safe from rescheduling changing
  3124. * our context.
  3125. */
  3126. child_ctx = child->perf_counter_ctxp;
  3127. __perf_counter_task_sched_out(child_ctx);
  3128. /*
  3129. * Take the context lock here so that if find_get_context is
  3130. * reading child->perf_counter_ctxp, we wait until it has
  3131. * incremented the context's refcount before we do put_ctx below.
  3132. */
  3133. spin_lock(&child_ctx->lock);
  3134. child->perf_counter_ctxp = NULL;
  3135. if (child_ctx->parent_ctx) {
  3136. /*
  3137. * This context is a clone; unclone it so it can't get
  3138. * swapped to another process while we're removing all
  3139. * the counters from it.
  3140. */
  3141. put_ctx(child_ctx->parent_ctx);
  3142. child_ctx->parent_ctx = NULL;
  3143. }
  3144. spin_unlock(&child_ctx->lock);
  3145. local_irq_restore(flags);
  3146. mutex_lock(&child_ctx->mutex);
  3147. again:
  3148. list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
  3149. list_entry)
  3150. __perf_counter_exit_task(child_counter, child_ctx);
  3151. /*
  3152. * If the last counter was a group counter, it will have appended all
  3153. * its siblings to the list, but we obtained 'tmp' before that which
  3154. * will still point to the list head terminating the iteration.
  3155. */
  3156. if (!list_empty(&child_ctx->counter_list))
  3157. goto again;
  3158. mutex_unlock(&child_ctx->mutex);
  3159. put_ctx(child_ctx);
  3160. }
  3161. /*
  3162. * free an unexposed, unused context as created by inheritance by
  3163. * init_task below, used by fork() in case of fail.
  3164. */
  3165. void perf_counter_free_task(struct task_struct *task)
  3166. {
  3167. struct perf_counter_context *ctx = task->perf_counter_ctxp;
  3168. struct perf_counter *counter, *tmp;
  3169. if (!ctx)
  3170. return;
  3171. mutex_lock(&ctx->mutex);
  3172. again:
  3173. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
  3174. struct perf_counter *parent = counter->parent;
  3175. if (WARN_ON_ONCE(!parent))
  3176. continue;
  3177. mutex_lock(&parent->child_mutex);
  3178. list_del_init(&counter->child_list);
  3179. mutex_unlock(&parent->child_mutex);
  3180. fput(parent->filp);
  3181. list_del_counter(counter, ctx);
  3182. free_counter(counter);
  3183. }
  3184. if (!list_empty(&ctx->counter_list))
  3185. goto again;
  3186. mutex_unlock(&ctx->mutex);
  3187. put_ctx(ctx);
  3188. }
  3189. /*
  3190. * Initialize the perf_counter context in task_struct
  3191. */
  3192. int perf_counter_init_task(struct task_struct *child)
  3193. {
  3194. struct perf_counter_context *child_ctx, *parent_ctx;
  3195. struct perf_counter_context *cloned_ctx;
  3196. struct perf_counter *counter;
  3197. struct task_struct *parent = current;
  3198. int inherited_all = 1;
  3199. int ret = 0;
  3200. child->perf_counter_ctxp = NULL;
  3201. mutex_init(&child->perf_counter_mutex);
  3202. INIT_LIST_HEAD(&child->perf_counter_list);
  3203. if (likely(!parent->perf_counter_ctxp))
  3204. return 0;
  3205. /*
  3206. * This is executed from the parent task context, so inherit
  3207. * counters that have been marked for cloning.
  3208. * First allocate and initialize a context for the child.
  3209. */
  3210. child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
  3211. if (!child_ctx)
  3212. return -ENOMEM;
  3213. __perf_counter_init_context(child_ctx, child);
  3214. child->perf_counter_ctxp = child_ctx;
  3215. get_task_struct(child);
  3216. /*
  3217. * If the parent's context is a clone, pin it so it won't get
  3218. * swapped under us.
  3219. */
  3220. parent_ctx = perf_pin_task_context(parent);
  3221. /*
  3222. * No need to check if parent_ctx != NULL here; since we saw
  3223. * it non-NULL earlier, the only reason for it to become NULL
  3224. * is if we exit, and since we're currently in the middle of
  3225. * a fork we can't be exiting at the same time.
  3226. */
  3227. /*
  3228. * Lock the parent list. No need to lock the child - not PID
  3229. * hashed yet and not running, so nobody can access it.
  3230. */
  3231. mutex_lock(&parent_ctx->mutex);
  3232. /*
  3233. * We dont have to disable NMIs - we are only looking at
  3234. * the list, not manipulating it:
  3235. */
  3236. list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
  3237. if (counter != counter->group_leader)
  3238. continue;
  3239. if (!counter->attr.inherit) {
  3240. inherited_all = 0;
  3241. continue;
  3242. }
  3243. ret = inherit_group(counter, parent, parent_ctx,
  3244. child, child_ctx);
  3245. if (ret) {
  3246. inherited_all = 0;
  3247. break;
  3248. }
  3249. }
  3250. if (inherited_all) {
  3251. /*
  3252. * Mark the child context as a clone of the parent
  3253. * context, or of whatever the parent is a clone of.
  3254. * Note that if the parent is a clone, it could get
  3255. * uncloned at any point, but that doesn't matter
  3256. * because the list of counters and the generation
  3257. * count can't have changed since we took the mutex.
  3258. */
  3259. cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
  3260. if (cloned_ctx) {
  3261. child_ctx->parent_ctx = cloned_ctx;
  3262. child_ctx->parent_gen = parent_ctx->parent_gen;
  3263. } else {
  3264. child_ctx->parent_ctx = parent_ctx;
  3265. child_ctx->parent_gen = parent_ctx->generation;
  3266. }
  3267. get_ctx(child_ctx->parent_ctx);
  3268. }
  3269. mutex_unlock(&parent_ctx->mutex);
  3270. perf_unpin_context(parent_ctx);
  3271. return ret;
  3272. }
  3273. static void __cpuinit perf_counter_init_cpu(int cpu)
  3274. {
  3275. struct perf_cpu_context *cpuctx;
  3276. cpuctx = &per_cpu(perf_cpu_context, cpu);
  3277. __perf_counter_init_context(&cpuctx->ctx, NULL);
  3278. spin_lock(&perf_resource_lock);
  3279. cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
  3280. spin_unlock(&perf_resource_lock);
  3281. hw_perf_counter_setup(cpu);
  3282. }
  3283. #ifdef CONFIG_HOTPLUG_CPU
  3284. static void __perf_counter_exit_cpu(void *info)
  3285. {
  3286. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  3287. struct perf_counter_context *ctx = &cpuctx->ctx;
  3288. struct perf_counter *counter, *tmp;
  3289. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
  3290. __perf_counter_remove_from_context(counter);
  3291. }
  3292. static void perf_counter_exit_cpu(int cpu)
  3293. {
  3294. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  3295. struct perf_counter_context *ctx = &cpuctx->ctx;
  3296. mutex_lock(&ctx->mutex);
  3297. smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
  3298. mutex_unlock(&ctx->mutex);
  3299. }
  3300. #else
  3301. static inline void perf_counter_exit_cpu(int cpu) { }
  3302. #endif
  3303. static int __cpuinit
  3304. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  3305. {
  3306. unsigned int cpu = (long)hcpu;
  3307. switch (action) {
  3308. case CPU_UP_PREPARE:
  3309. case CPU_UP_PREPARE_FROZEN:
  3310. perf_counter_init_cpu(cpu);
  3311. break;
  3312. case CPU_DOWN_PREPARE:
  3313. case CPU_DOWN_PREPARE_FROZEN:
  3314. perf_counter_exit_cpu(cpu);
  3315. break;
  3316. default:
  3317. break;
  3318. }
  3319. return NOTIFY_OK;
  3320. }
  3321. /*
  3322. * This has to have a higher priority than migration_notifier in sched.c.
  3323. */
  3324. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  3325. .notifier_call = perf_cpu_notify,
  3326. .priority = 20,
  3327. };
  3328. void __init perf_counter_init(void)
  3329. {
  3330. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  3331. (void *)(long)smp_processor_id());
  3332. register_cpu_notifier(&perf_cpu_nb);
  3333. }
  3334. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  3335. {
  3336. return sprintf(buf, "%d\n", perf_reserved_percpu);
  3337. }
  3338. static ssize_t
  3339. perf_set_reserve_percpu(struct sysdev_class *class,
  3340. const char *buf,
  3341. size_t count)
  3342. {
  3343. struct perf_cpu_context *cpuctx;
  3344. unsigned long val;
  3345. int err, cpu, mpt;
  3346. err = strict_strtoul(buf, 10, &val);
  3347. if (err)
  3348. return err;
  3349. if (val > perf_max_counters)
  3350. return -EINVAL;
  3351. spin_lock(&perf_resource_lock);
  3352. perf_reserved_percpu = val;
  3353. for_each_online_cpu(cpu) {
  3354. cpuctx = &per_cpu(perf_cpu_context, cpu);
  3355. spin_lock_irq(&cpuctx->ctx.lock);
  3356. mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
  3357. perf_max_counters - perf_reserved_percpu);
  3358. cpuctx->max_pertask = mpt;
  3359. spin_unlock_irq(&cpuctx->ctx.lock);
  3360. }
  3361. spin_unlock(&perf_resource_lock);
  3362. return count;
  3363. }
  3364. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  3365. {
  3366. return sprintf(buf, "%d\n", perf_overcommit);
  3367. }
  3368. static ssize_t
  3369. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  3370. {
  3371. unsigned long val;
  3372. int err;
  3373. err = strict_strtoul(buf, 10, &val);
  3374. if (err)
  3375. return err;
  3376. if (val > 1)
  3377. return -EINVAL;
  3378. spin_lock(&perf_resource_lock);
  3379. perf_overcommit = val;
  3380. spin_unlock(&perf_resource_lock);
  3381. return count;
  3382. }
  3383. static SYSDEV_CLASS_ATTR(
  3384. reserve_percpu,
  3385. 0644,
  3386. perf_show_reserve_percpu,
  3387. perf_set_reserve_percpu
  3388. );
  3389. static SYSDEV_CLASS_ATTR(
  3390. overcommit,
  3391. 0644,
  3392. perf_show_overcommit,
  3393. perf_set_overcommit
  3394. );
  3395. static struct attribute *perfclass_attrs[] = {
  3396. &attr_reserve_percpu.attr,
  3397. &attr_overcommit.attr,
  3398. NULL
  3399. };
  3400. static struct attribute_group perfclass_attr_group = {
  3401. .attrs = perfclass_attrs,
  3402. .name = "perf_counters",
  3403. };
  3404. static int __init perf_counter_sysfs_init(void)
  3405. {
  3406. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  3407. &perfclass_attr_group);
  3408. }
  3409. device_initcall(perf_counter_sysfs_init);