core.c 162 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124
  1. /*
  2. * Performance events core code:
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2008-2011 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/idr.h>
  16. #include <linux/file.h>
  17. #include <linux/poll.h>
  18. #include <linux/slab.h>
  19. #include <linux/hash.h>
  20. #include <linux/sysfs.h>
  21. #include <linux/dcache.h>
  22. #include <linux/percpu.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/reboot.h>
  25. #include <linux/vmstat.h>
  26. #include <linux/device.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/hardirq.h>
  29. #include <linux/rculist.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/suspend.h>
  32. #include <linux/syscalls.h>
  33. #include <linux/anon_inodes.h>
  34. #include <linux/kernel_stat.h>
  35. #include <linux/perf_event.h>
  36. #include <linux/ftrace_event.h>
  37. #include <linux/hw_breakpoint.h>
  38. #include "internal.h"
  39. #include <asm/irq_regs.h>
  40. struct remote_function_call {
  41. struct task_struct *p;
  42. int (*func)(void *info);
  43. void *info;
  44. int ret;
  45. };
  46. static void remote_function(void *data)
  47. {
  48. struct remote_function_call *tfc = data;
  49. struct task_struct *p = tfc->p;
  50. if (p) {
  51. tfc->ret = -EAGAIN;
  52. if (task_cpu(p) != smp_processor_id() || !task_curr(p))
  53. return;
  54. }
  55. tfc->ret = tfc->func(tfc->info);
  56. }
  57. /**
  58. * task_function_call - call a function on the cpu on which a task runs
  59. * @p: the task to evaluate
  60. * @func: the function to be called
  61. * @info: the function call argument
  62. *
  63. * Calls the function @func when the task is currently running. This might
  64. * be on the current CPU, which just calls the function directly
  65. *
  66. * returns: @func return value, or
  67. * -ESRCH - when the process isn't running
  68. * -EAGAIN - when the process moved away
  69. */
  70. static int
  71. task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
  72. {
  73. struct remote_function_call data = {
  74. .p = p,
  75. .func = func,
  76. .info = info,
  77. .ret = -ESRCH, /* No such (running) process */
  78. };
  79. if (task_curr(p))
  80. smp_call_function_single(task_cpu(p), remote_function, &data, 1);
  81. return data.ret;
  82. }
  83. /**
  84. * cpu_function_call - call a function on the cpu
  85. * @func: the function to be called
  86. * @info: the function call argument
  87. *
  88. * Calls the function @func on the remote cpu.
  89. *
  90. * returns: @func return value or -ENXIO when the cpu is offline
  91. */
  92. static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
  93. {
  94. struct remote_function_call data = {
  95. .p = NULL,
  96. .func = func,
  97. .info = info,
  98. .ret = -ENXIO, /* No such CPU */
  99. };
  100. smp_call_function_single(cpu, remote_function, &data, 1);
  101. return data.ret;
  102. }
  103. #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
  104. PERF_FLAG_FD_OUTPUT |\
  105. PERF_FLAG_PID_CGROUP)
  106. enum event_type_t {
  107. EVENT_FLEXIBLE = 0x1,
  108. EVENT_PINNED = 0x2,
  109. EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
  110. };
  111. /*
  112. * perf_sched_events : >0 events exist
  113. * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
  114. */
  115. struct jump_label_key perf_sched_events __read_mostly;
  116. static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
  117. static atomic_t nr_mmap_events __read_mostly;
  118. static atomic_t nr_comm_events __read_mostly;
  119. static atomic_t nr_task_events __read_mostly;
  120. static LIST_HEAD(pmus);
  121. static DEFINE_MUTEX(pmus_lock);
  122. static struct srcu_struct pmus_srcu;
  123. /*
  124. * perf event paranoia level:
  125. * -1 - not paranoid at all
  126. * 0 - disallow raw tracepoint access for unpriv
  127. * 1 - disallow cpu events for unpriv
  128. * 2 - disallow kernel profiling for unpriv
  129. */
  130. int sysctl_perf_event_paranoid __read_mostly = 1;
  131. /* Minimum for 512 kiB + 1 user control page */
  132. int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
  133. /*
  134. * max perf event sample rate
  135. */
  136. #define DEFAULT_MAX_SAMPLE_RATE 100000
  137. int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
  138. static int max_samples_per_tick __read_mostly =
  139. DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
  140. int perf_proc_update_handler(struct ctl_table *table, int write,
  141. void __user *buffer, size_t *lenp,
  142. loff_t *ppos)
  143. {
  144. int ret = proc_dointvec(table, write, buffer, lenp, ppos);
  145. if (ret || !write)
  146. return ret;
  147. max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
  148. return 0;
  149. }
  150. static atomic64_t perf_event_id;
  151. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  152. enum event_type_t event_type);
  153. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  154. enum event_type_t event_type,
  155. struct task_struct *task);
  156. static void update_context_time(struct perf_event_context *ctx);
  157. static u64 perf_event_time(struct perf_event *event);
  158. void __weak perf_event_print_debug(void) { }
  159. extern __weak const char *perf_pmu_name(void)
  160. {
  161. return "pmu";
  162. }
  163. static inline u64 perf_clock(void)
  164. {
  165. return local_clock();
  166. }
  167. static inline struct perf_cpu_context *
  168. __get_cpu_context(struct perf_event_context *ctx)
  169. {
  170. return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
  171. }
  172. static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
  173. struct perf_event_context *ctx)
  174. {
  175. raw_spin_lock(&cpuctx->ctx.lock);
  176. if (ctx)
  177. raw_spin_lock(&ctx->lock);
  178. }
  179. static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
  180. struct perf_event_context *ctx)
  181. {
  182. if (ctx)
  183. raw_spin_unlock(&ctx->lock);
  184. raw_spin_unlock(&cpuctx->ctx.lock);
  185. }
  186. #ifdef CONFIG_CGROUP_PERF
  187. /*
  188. * Must ensure cgroup is pinned (css_get) before calling
  189. * this function. In other words, we cannot call this function
  190. * if there is no cgroup event for the current CPU context.
  191. */
  192. static inline struct perf_cgroup *
  193. perf_cgroup_from_task(struct task_struct *task)
  194. {
  195. return container_of(task_subsys_state(task, perf_subsys_id),
  196. struct perf_cgroup, css);
  197. }
  198. static inline bool
  199. perf_cgroup_match(struct perf_event *event)
  200. {
  201. struct perf_event_context *ctx = event->ctx;
  202. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  203. return !event->cgrp || event->cgrp == cpuctx->cgrp;
  204. }
  205. static inline void perf_get_cgroup(struct perf_event *event)
  206. {
  207. css_get(&event->cgrp->css);
  208. }
  209. static inline void perf_put_cgroup(struct perf_event *event)
  210. {
  211. css_put(&event->cgrp->css);
  212. }
  213. static inline void perf_detach_cgroup(struct perf_event *event)
  214. {
  215. perf_put_cgroup(event);
  216. event->cgrp = NULL;
  217. }
  218. static inline int is_cgroup_event(struct perf_event *event)
  219. {
  220. return event->cgrp != NULL;
  221. }
  222. static inline u64 perf_cgroup_event_time(struct perf_event *event)
  223. {
  224. struct perf_cgroup_info *t;
  225. t = per_cpu_ptr(event->cgrp->info, event->cpu);
  226. return t->time;
  227. }
  228. static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
  229. {
  230. struct perf_cgroup_info *info;
  231. u64 now;
  232. now = perf_clock();
  233. info = this_cpu_ptr(cgrp->info);
  234. info->time += now - info->timestamp;
  235. info->timestamp = now;
  236. }
  237. static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
  238. {
  239. struct perf_cgroup *cgrp_out = cpuctx->cgrp;
  240. if (cgrp_out)
  241. __update_cgrp_time(cgrp_out);
  242. }
  243. static inline void update_cgrp_time_from_event(struct perf_event *event)
  244. {
  245. struct perf_cgroup *cgrp;
  246. /*
  247. * ensure we access cgroup data only when needed and
  248. * when we know the cgroup is pinned (css_get)
  249. */
  250. if (!is_cgroup_event(event))
  251. return;
  252. cgrp = perf_cgroup_from_task(current);
  253. /*
  254. * Do not update time when cgroup is not active
  255. */
  256. if (cgrp == event->cgrp)
  257. __update_cgrp_time(event->cgrp);
  258. }
  259. static inline void
  260. perf_cgroup_set_timestamp(struct task_struct *task,
  261. struct perf_event_context *ctx)
  262. {
  263. struct perf_cgroup *cgrp;
  264. struct perf_cgroup_info *info;
  265. /*
  266. * ctx->lock held by caller
  267. * ensure we do not access cgroup data
  268. * unless we have the cgroup pinned (css_get)
  269. */
  270. if (!task || !ctx->nr_cgroups)
  271. return;
  272. cgrp = perf_cgroup_from_task(task);
  273. info = this_cpu_ptr(cgrp->info);
  274. info->timestamp = ctx->timestamp;
  275. }
  276. #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
  277. #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
  278. /*
  279. * reschedule events based on the cgroup constraint of task.
  280. *
  281. * mode SWOUT : schedule out everything
  282. * mode SWIN : schedule in based on cgroup for next
  283. */
  284. void perf_cgroup_switch(struct task_struct *task, int mode)
  285. {
  286. struct perf_cpu_context *cpuctx;
  287. struct pmu *pmu;
  288. unsigned long flags;
  289. /*
  290. * disable interrupts to avoid geting nr_cgroup
  291. * changes via __perf_event_disable(). Also
  292. * avoids preemption.
  293. */
  294. local_irq_save(flags);
  295. /*
  296. * we reschedule only in the presence of cgroup
  297. * constrained events.
  298. */
  299. rcu_read_lock();
  300. list_for_each_entry_rcu(pmu, &pmus, entry) {
  301. cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  302. /*
  303. * perf_cgroup_events says at least one
  304. * context on this CPU has cgroup events.
  305. *
  306. * ctx->nr_cgroups reports the number of cgroup
  307. * events for a context.
  308. */
  309. if (cpuctx->ctx.nr_cgroups > 0) {
  310. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  311. perf_pmu_disable(cpuctx->ctx.pmu);
  312. if (mode & PERF_CGROUP_SWOUT) {
  313. cpu_ctx_sched_out(cpuctx, EVENT_ALL);
  314. /*
  315. * must not be done before ctxswout due
  316. * to event_filter_match() in event_sched_out()
  317. */
  318. cpuctx->cgrp = NULL;
  319. }
  320. if (mode & PERF_CGROUP_SWIN) {
  321. WARN_ON_ONCE(cpuctx->cgrp);
  322. /* set cgrp before ctxsw in to
  323. * allow event_filter_match() to not
  324. * have to pass task around
  325. */
  326. cpuctx->cgrp = perf_cgroup_from_task(task);
  327. cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
  328. }
  329. perf_pmu_enable(cpuctx->ctx.pmu);
  330. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  331. }
  332. }
  333. rcu_read_unlock();
  334. local_irq_restore(flags);
  335. }
  336. static inline void perf_cgroup_sched_out(struct task_struct *task)
  337. {
  338. perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
  339. }
  340. static inline void perf_cgroup_sched_in(struct task_struct *task)
  341. {
  342. perf_cgroup_switch(task, PERF_CGROUP_SWIN);
  343. }
  344. static inline int perf_cgroup_connect(int fd, struct perf_event *event,
  345. struct perf_event_attr *attr,
  346. struct perf_event *group_leader)
  347. {
  348. struct perf_cgroup *cgrp;
  349. struct cgroup_subsys_state *css;
  350. struct file *file;
  351. int ret = 0, fput_needed;
  352. file = fget_light(fd, &fput_needed);
  353. if (!file)
  354. return -EBADF;
  355. css = cgroup_css_from_dir(file, perf_subsys_id);
  356. if (IS_ERR(css)) {
  357. ret = PTR_ERR(css);
  358. goto out;
  359. }
  360. cgrp = container_of(css, struct perf_cgroup, css);
  361. event->cgrp = cgrp;
  362. /* must be done before we fput() the file */
  363. perf_get_cgroup(event);
  364. /*
  365. * all events in a group must monitor
  366. * the same cgroup because a task belongs
  367. * to only one perf cgroup at a time
  368. */
  369. if (group_leader && group_leader->cgrp != cgrp) {
  370. perf_detach_cgroup(event);
  371. ret = -EINVAL;
  372. }
  373. out:
  374. fput_light(file, fput_needed);
  375. return ret;
  376. }
  377. static inline void
  378. perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
  379. {
  380. struct perf_cgroup_info *t;
  381. t = per_cpu_ptr(event->cgrp->info, event->cpu);
  382. event->shadow_ctx_time = now - t->timestamp;
  383. }
  384. static inline void
  385. perf_cgroup_defer_enabled(struct perf_event *event)
  386. {
  387. /*
  388. * when the current task's perf cgroup does not match
  389. * the event's, we need to remember to call the
  390. * perf_mark_enable() function the first time a task with
  391. * a matching perf cgroup is scheduled in.
  392. */
  393. if (is_cgroup_event(event) && !perf_cgroup_match(event))
  394. event->cgrp_defer_enabled = 1;
  395. }
  396. static inline void
  397. perf_cgroup_mark_enabled(struct perf_event *event,
  398. struct perf_event_context *ctx)
  399. {
  400. struct perf_event *sub;
  401. u64 tstamp = perf_event_time(event);
  402. if (!event->cgrp_defer_enabled)
  403. return;
  404. event->cgrp_defer_enabled = 0;
  405. event->tstamp_enabled = tstamp - event->total_time_enabled;
  406. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  407. if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
  408. sub->tstamp_enabled = tstamp - sub->total_time_enabled;
  409. sub->cgrp_defer_enabled = 0;
  410. }
  411. }
  412. }
  413. #else /* !CONFIG_CGROUP_PERF */
  414. static inline bool
  415. perf_cgroup_match(struct perf_event *event)
  416. {
  417. return true;
  418. }
  419. static inline void perf_detach_cgroup(struct perf_event *event)
  420. {}
  421. static inline int is_cgroup_event(struct perf_event *event)
  422. {
  423. return 0;
  424. }
  425. static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
  426. {
  427. return 0;
  428. }
  429. static inline void update_cgrp_time_from_event(struct perf_event *event)
  430. {
  431. }
  432. static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
  433. {
  434. }
  435. static inline void perf_cgroup_sched_out(struct task_struct *task)
  436. {
  437. }
  438. static inline void perf_cgroup_sched_in(struct task_struct *task)
  439. {
  440. }
  441. static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
  442. struct perf_event_attr *attr,
  443. struct perf_event *group_leader)
  444. {
  445. return -EINVAL;
  446. }
  447. static inline void
  448. perf_cgroup_set_timestamp(struct task_struct *task,
  449. struct perf_event_context *ctx)
  450. {
  451. }
  452. void
  453. perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
  454. {
  455. }
  456. static inline void
  457. perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
  458. {
  459. }
  460. static inline u64 perf_cgroup_event_time(struct perf_event *event)
  461. {
  462. return 0;
  463. }
  464. static inline void
  465. perf_cgroup_defer_enabled(struct perf_event *event)
  466. {
  467. }
  468. static inline void
  469. perf_cgroup_mark_enabled(struct perf_event *event,
  470. struct perf_event_context *ctx)
  471. {
  472. }
  473. #endif
  474. void perf_pmu_disable(struct pmu *pmu)
  475. {
  476. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  477. if (!(*count)++)
  478. pmu->pmu_disable(pmu);
  479. }
  480. void perf_pmu_enable(struct pmu *pmu)
  481. {
  482. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  483. if (!--(*count))
  484. pmu->pmu_enable(pmu);
  485. }
  486. static DEFINE_PER_CPU(struct list_head, rotation_list);
  487. /*
  488. * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
  489. * because they're strictly cpu affine and rotate_start is called with IRQs
  490. * disabled, while rotate_context is called from IRQ context.
  491. */
  492. static void perf_pmu_rotate_start(struct pmu *pmu)
  493. {
  494. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  495. struct list_head *head = &__get_cpu_var(rotation_list);
  496. WARN_ON(!irqs_disabled());
  497. if (list_empty(&cpuctx->rotation_list))
  498. list_add(&cpuctx->rotation_list, head);
  499. }
  500. static void get_ctx(struct perf_event_context *ctx)
  501. {
  502. WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
  503. }
  504. static void put_ctx(struct perf_event_context *ctx)
  505. {
  506. if (atomic_dec_and_test(&ctx->refcount)) {
  507. if (ctx->parent_ctx)
  508. put_ctx(ctx->parent_ctx);
  509. if (ctx->task)
  510. put_task_struct(ctx->task);
  511. kfree_rcu(ctx, rcu_head);
  512. }
  513. }
  514. static void unclone_ctx(struct perf_event_context *ctx)
  515. {
  516. if (ctx->parent_ctx) {
  517. put_ctx(ctx->parent_ctx);
  518. ctx->parent_ctx = NULL;
  519. }
  520. }
  521. static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
  522. {
  523. /*
  524. * only top level events have the pid namespace they were created in
  525. */
  526. if (event->parent)
  527. event = event->parent;
  528. return task_tgid_nr_ns(p, event->ns);
  529. }
  530. static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
  531. {
  532. /*
  533. * only top level events have the pid namespace they were created in
  534. */
  535. if (event->parent)
  536. event = event->parent;
  537. return task_pid_nr_ns(p, event->ns);
  538. }
  539. /*
  540. * If we inherit events we want to return the parent event id
  541. * to userspace.
  542. */
  543. static u64 primary_event_id(struct perf_event *event)
  544. {
  545. u64 id = event->id;
  546. if (event->parent)
  547. id = event->parent->id;
  548. return id;
  549. }
  550. /*
  551. * Get the perf_event_context for a task and lock it.
  552. * This has to cope with with the fact that until it is locked,
  553. * the context could get moved to another task.
  554. */
  555. static struct perf_event_context *
  556. perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
  557. {
  558. struct perf_event_context *ctx;
  559. rcu_read_lock();
  560. retry:
  561. ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
  562. if (ctx) {
  563. /*
  564. * If this context is a clone of another, it might
  565. * get swapped for another underneath us by
  566. * perf_event_task_sched_out, though the
  567. * rcu_read_lock() protects us from any context
  568. * getting freed. Lock the context and check if it
  569. * got swapped before we could get the lock, and retry
  570. * if so. If we locked the right context, then it
  571. * can't get swapped on us any more.
  572. */
  573. raw_spin_lock_irqsave(&ctx->lock, *flags);
  574. if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
  575. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  576. goto retry;
  577. }
  578. if (!atomic_inc_not_zero(&ctx->refcount)) {
  579. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  580. ctx = NULL;
  581. }
  582. }
  583. rcu_read_unlock();
  584. return ctx;
  585. }
  586. /*
  587. * Get the context for a task and increment its pin_count so it
  588. * can't get swapped to another task. This also increments its
  589. * reference count so that the context can't get freed.
  590. */
  591. static struct perf_event_context *
  592. perf_pin_task_context(struct task_struct *task, int ctxn)
  593. {
  594. struct perf_event_context *ctx;
  595. unsigned long flags;
  596. ctx = perf_lock_task_context(task, ctxn, &flags);
  597. if (ctx) {
  598. ++ctx->pin_count;
  599. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  600. }
  601. return ctx;
  602. }
  603. static void perf_unpin_context(struct perf_event_context *ctx)
  604. {
  605. unsigned long flags;
  606. raw_spin_lock_irqsave(&ctx->lock, flags);
  607. --ctx->pin_count;
  608. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  609. }
  610. /*
  611. * Update the record of the current time in a context.
  612. */
  613. static void update_context_time(struct perf_event_context *ctx)
  614. {
  615. u64 now = perf_clock();
  616. ctx->time += now - ctx->timestamp;
  617. ctx->timestamp = now;
  618. }
  619. static u64 perf_event_time(struct perf_event *event)
  620. {
  621. struct perf_event_context *ctx = event->ctx;
  622. if (is_cgroup_event(event))
  623. return perf_cgroup_event_time(event);
  624. return ctx ? ctx->time : 0;
  625. }
  626. /*
  627. * Update the total_time_enabled and total_time_running fields for a event.
  628. * The caller of this function needs to hold the ctx->lock.
  629. */
  630. static void update_event_times(struct perf_event *event)
  631. {
  632. struct perf_event_context *ctx = event->ctx;
  633. u64 run_end;
  634. if (event->state < PERF_EVENT_STATE_INACTIVE ||
  635. event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
  636. return;
  637. /*
  638. * in cgroup mode, time_enabled represents
  639. * the time the event was enabled AND active
  640. * tasks were in the monitored cgroup. This is
  641. * independent of the activity of the context as
  642. * there may be a mix of cgroup and non-cgroup events.
  643. *
  644. * That is why we treat cgroup events differently
  645. * here.
  646. */
  647. if (is_cgroup_event(event))
  648. run_end = perf_event_time(event);
  649. else if (ctx->is_active)
  650. run_end = ctx->time;
  651. else
  652. run_end = event->tstamp_stopped;
  653. event->total_time_enabled = run_end - event->tstamp_enabled;
  654. if (event->state == PERF_EVENT_STATE_INACTIVE)
  655. run_end = event->tstamp_stopped;
  656. else
  657. run_end = perf_event_time(event);
  658. event->total_time_running = run_end - event->tstamp_running;
  659. }
  660. /*
  661. * Update total_time_enabled and total_time_running for all events in a group.
  662. */
  663. static void update_group_times(struct perf_event *leader)
  664. {
  665. struct perf_event *event;
  666. update_event_times(leader);
  667. list_for_each_entry(event, &leader->sibling_list, group_entry)
  668. update_event_times(event);
  669. }
  670. static struct list_head *
  671. ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
  672. {
  673. if (event->attr.pinned)
  674. return &ctx->pinned_groups;
  675. else
  676. return &ctx->flexible_groups;
  677. }
  678. /*
  679. * Add a event from the lists for its context.
  680. * Must be called with ctx->mutex and ctx->lock held.
  681. */
  682. static void
  683. list_add_event(struct perf_event *event, struct perf_event_context *ctx)
  684. {
  685. WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
  686. event->attach_state |= PERF_ATTACH_CONTEXT;
  687. /*
  688. * If we're a stand alone event or group leader, we go to the context
  689. * list, group events are kept attached to the group so that
  690. * perf_group_detach can, at all times, locate all siblings.
  691. */
  692. if (event->group_leader == event) {
  693. struct list_head *list;
  694. if (is_software_event(event))
  695. event->group_flags |= PERF_GROUP_SOFTWARE;
  696. list = ctx_group_list(event, ctx);
  697. list_add_tail(&event->group_entry, list);
  698. }
  699. if (is_cgroup_event(event))
  700. ctx->nr_cgroups++;
  701. list_add_rcu(&event->event_entry, &ctx->event_list);
  702. if (!ctx->nr_events)
  703. perf_pmu_rotate_start(ctx->pmu);
  704. ctx->nr_events++;
  705. if (event->attr.inherit_stat)
  706. ctx->nr_stat++;
  707. }
  708. /*
  709. * Called at perf_event creation and when events are attached/detached from a
  710. * group.
  711. */
  712. static void perf_event__read_size(struct perf_event *event)
  713. {
  714. int entry = sizeof(u64); /* value */
  715. int size = 0;
  716. int nr = 1;
  717. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  718. size += sizeof(u64);
  719. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  720. size += sizeof(u64);
  721. if (event->attr.read_format & PERF_FORMAT_ID)
  722. entry += sizeof(u64);
  723. if (event->attr.read_format & PERF_FORMAT_GROUP) {
  724. nr += event->group_leader->nr_siblings;
  725. size += sizeof(u64);
  726. }
  727. size += entry * nr;
  728. event->read_size = size;
  729. }
  730. static void perf_event__header_size(struct perf_event *event)
  731. {
  732. struct perf_sample_data *data;
  733. u64 sample_type = event->attr.sample_type;
  734. u16 size = 0;
  735. perf_event__read_size(event);
  736. if (sample_type & PERF_SAMPLE_IP)
  737. size += sizeof(data->ip);
  738. if (sample_type & PERF_SAMPLE_ADDR)
  739. size += sizeof(data->addr);
  740. if (sample_type & PERF_SAMPLE_PERIOD)
  741. size += sizeof(data->period);
  742. if (sample_type & PERF_SAMPLE_READ)
  743. size += event->read_size;
  744. event->header_size = size;
  745. }
  746. static void perf_event__id_header_size(struct perf_event *event)
  747. {
  748. struct perf_sample_data *data;
  749. u64 sample_type = event->attr.sample_type;
  750. u16 size = 0;
  751. if (sample_type & PERF_SAMPLE_TID)
  752. size += sizeof(data->tid_entry);
  753. if (sample_type & PERF_SAMPLE_TIME)
  754. size += sizeof(data->time);
  755. if (sample_type & PERF_SAMPLE_ID)
  756. size += sizeof(data->id);
  757. if (sample_type & PERF_SAMPLE_STREAM_ID)
  758. size += sizeof(data->stream_id);
  759. if (sample_type & PERF_SAMPLE_CPU)
  760. size += sizeof(data->cpu_entry);
  761. event->id_header_size = size;
  762. }
  763. static void perf_group_attach(struct perf_event *event)
  764. {
  765. struct perf_event *group_leader = event->group_leader, *pos;
  766. /*
  767. * We can have double attach due to group movement in perf_event_open.
  768. */
  769. if (event->attach_state & PERF_ATTACH_GROUP)
  770. return;
  771. event->attach_state |= PERF_ATTACH_GROUP;
  772. if (group_leader == event)
  773. return;
  774. if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
  775. !is_software_event(event))
  776. group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
  777. list_add_tail(&event->group_entry, &group_leader->sibling_list);
  778. group_leader->nr_siblings++;
  779. perf_event__header_size(group_leader);
  780. list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
  781. perf_event__header_size(pos);
  782. }
  783. /*
  784. * Remove a event from the lists for its context.
  785. * Must be called with ctx->mutex and ctx->lock held.
  786. */
  787. static void
  788. list_del_event(struct perf_event *event, struct perf_event_context *ctx)
  789. {
  790. struct perf_cpu_context *cpuctx;
  791. /*
  792. * We can have double detach due to exit/hot-unplug + close.
  793. */
  794. if (!(event->attach_state & PERF_ATTACH_CONTEXT))
  795. return;
  796. event->attach_state &= ~PERF_ATTACH_CONTEXT;
  797. if (is_cgroup_event(event)) {
  798. ctx->nr_cgroups--;
  799. cpuctx = __get_cpu_context(ctx);
  800. /*
  801. * if there are no more cgroup events
  802. * then cler cgrp to avoid stale pointer
  803. * in update_cgrp_time_from_cpuctx()
  804. */
  805. if (!ctx->nr_cgroups)
  806. cpuctx->cgrp = NULL;
  807. }
  808. ctx->nr_events--;
  809. if (event->attr.inherit_stat)
  810. ctx->nr_stat--;
  811. list_del_rcu(&event->event_entry);
  812. if (event->group_leader == event)
  813. list_del_init(&event->group_entry);
  814. update_group_times(event);
  815. /*
  816. * If event was in error state, then keep it
  817. * that way, otherwise bogus counts will be
  818. * returned on read(). The only way to get out
  819. * of error state is by explicit re-enabling
  820. * of the event
  821. */
  822. if (event->state > PERF_EVENT_STATE_OFF)
  823. event->state = PERF_EVENT_STATE_OFF;
  824. }
  825. static void perf_group_detach(struct perf_event *event)
  826. {
  827. struct perf_event *sibling, *tmp;
  828. struct list_head *list = NULL;
  829. /*
  830. * We can have double detach due to exit/hot-unplug + close.
  831. */
  832. if (!(event->attach_state & PERF_ATTACH_GROUP))
  833. return;
  834. event->attach_state &= ~PERF_ATTACH_GROUP;
  835. /*
  836. * If this is a sibling, remove it from its group.
  837. */
  838. if (event->group_leader != event) {
  839. list_del_init(&event->group_entry);
  840. event->group_leader->nr_siblings--;
  841. goto out;
  842. }
  843. if (!list_empty(&event->group_entry))
  844. list = &event->group_entry;
  845. /*
  846. * If this was a group event with sibling events then
  847. * upgrade the siblings to singleton events by adding them
  848. * to whatever list we are on.
  849. */
  850. list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
  851. if (list)
  852. list_move_tail(&sibling->group_entry, list);
  853. sibling->group_leader = sibling;
  854. /* Inherit group flags from the previous leader */
  855. sibling->group_flags = event->group_flags;
  856. }
  857. out:
  858. perf_event__header_size(event->group_leader);
  859. list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
  860. perf_event__header_size(tmp);
  861. }
  862. static inline int
  863. event_filter_match(struct perf_event *event)
  864. {
  865. return (event->cpu == -1 || event->cpu == smp_processor_id())
  866. && perf_cgroup_match(event);
  867. }
  868. static void
  869. event_sched_out(struct perf_event *event,
  870. struct perf_cpu_context *cpuctx,
  871. struct perf_event_context *ctx)
  872. {
  873. u64 tstamp = perf_event_time(event);
  874. u64 delta;
  875. /*
  876. * An event which could not be activated because of
  877. * filter mismatch still needs to have its timings
  878. * maintained, otherwise bogus information is return
  879. * via read() for time_enabled, time_running:
  880. */
  881. if (event->state == PERF_EVENT_STATE_INACTIVE
  882. && !event_filter_match(event)) {
  883. delta = tstamp - event->tstamp_stopped;
  884. event->tstamp_running += delta;
  885. event->tstamp_stopped = tstamp;
  886. }
  887. if (event->state != PERF_EVENT_STATE_ACTIVE)
  888. return;
  889. event->state = PERF_EVENT_STATE_INACTIVE;
  890. if (event->pending_disable) {
  891. event->pending_disable = 0;
  892. event->state = PERF_EVENT_STATE_OFF;
  893. }
  894. event->tstamp_stopped = tstamp;
  895. event->pmu->del(event, 0);
  896. event->oncpu = -1;
  897. if (!is_software_event(event))
  898. cpuctx->active_oncpu--;
  899. ctx->nr_active--;
  900. if (event->attr.exclusive || !cpuctx->active_oncpu)
  901. cpuctx->exclusive = 0;
  902. }
  903. static void
  904. group_sched_out(struct perf_event *group_event,
  905. struct perf_cpu_context *cpuctx,
  906. struct perf_event_context *ctx)
  907. {
  908. struct perf_event *event;
  909. int state = group_event->state;
  910. event_sched_out(group_event, cpuctx, ctx);
  911. /*
  912. * Schedule out siblings (if any):
  913. */
  914. list_for_each_entry(event, &group_event->sibling_list, group_entry)
  915. event_sched_out(event, cpuctx, ctx);
  916. if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
  917. cpuctx->exclusive = 0;
  918. }
  919. /*
  920. * Cross CPU call to remove a performance event
  921. *
  922. * We disable the event on the hardware level first. After that we
  923. * remove it from the context list.
  924. */
  925. static int __perf_remove_from_context(void *info)
  926. {
  927. struct perf_event *event = info;
  928. struct perf_event_context *ctx = event->ctx;
  929. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  930. raw_spin_lock(&ctx->lock);
  931. event_sched_out(event, cpuctx, ctx);
  932. list_del_event(event, ctx);
  933. if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
  934. ctx->is_active = 0;
  935. cpuctx->task_ctx = NULL;
  936. }
  937. raw_spin_unlock(&ctx->lock);
  938. return 0;
  939. }
  940. /*
  941. * Remove the event from a task's (or a CPU's) list of events.
  942. *
  943. * CPU events are removed with a smp call. For task events we only
  944. * call when the task is on a CPU.
  945. *
  946. * If event->ctx is a cloned context, callers must make sure that
  947. * every task struct that event->ctx->task could possibly point to
  948. * remains valid. This is OK when called from perf_release since
  949. * that only calls us on the top-level context, which can't be a clone.
  950. * When called from perf_event_exit_task, it's OK because the
  951. * context has been detached from its task.
  952. */
  953. static void perf_remove_from_context(struct perf_event *event)
  954. {
  955. struct perf_event_context *ctx = event->ctx;
  956. struct task_struct *task = ctx->task;
  957. lockdep_assert_held(&ctx->mutex);
  958. if (!task) {
  959. /*
  960. * Per cpu events are removed via an smp call and
  961. * the removal is always successful.
  962. */
  963. cpu_function_call(event->cpu, __perf_remove_from_context, event);
  964. return;
  965. }
  966. retry:
  967. if (!task_function_call(task, __perf_remove_from_context, event))
  968. return;
  969. raw_spin_lock_irq(&ctx->lock);
  970. /*
  971. * If we failed to find a running task, but find the context active now
  972. * that we've acquired the ctx->lock, retry.
  973. */
  974. if (ctx->is_active) {
  975. raw_spin_unlock_irq(&ctx->lock);
  976. goto retry;
  977. }
  978. /*
  979. * Since the task isn't running, its safe to remove the event, us
  980. * holding the ctx->lock ensures the task won't get scheduled in.
  981. */
  982. list_del_event(event, ctx);
  983. raw_spin_unlock_irq(&ctx->lock);
  984. }
  985. /*
  986. * Cross CPU call to disable a performance event
  987. */
  988. static int __perf_event_disable(void *info)
  989. {
  990. struct perf_event *event = info;
  991. struct perf_event_context *ctx = event->ctx;
  992. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  993. /*
  994. * If this is a per-task event, need to check whether this
  995. * event's task is the current task on this cpu.
  996. *
  997. * Can trigger due to concurrent perf_event_context_sched_out()
  998. * flipping contexts around.
  999. */
  1000. if (ctx->task && cpuctx->task_ctx != ctx)
  1001. return -EINVAL;
  1002. raw_spin_lock(&ctx->lock);
  1003. /*
  1004. * If the event is on, turn it off.
  1005. * If it is in error state, leave it in error state.
  1006. */
  1007. if (event->state >= PERF_EVENT_STATE_INACTIVE) {
  1008. update_context_time(ctx);
  1009. update_cgrp_time_from_event(event);
  1010. update_group_times(event);
  1011. if (event == event->group_leader)
  1012. group_sched_out(event, cpuctx, ctx);
  1013. else
  1014. event_sched_out(event, cpuctx, ctx);
  1015. event->state = PERF_EVENT_STATE_OFF;
  1016. }
  1017. raw_spin_unlock(&ctx->lock);
  1018. return 0;
  1019. }
  1020. /*
  1021. * Disable a event.
  1022. *
  1023. * If event->ctx is a cloned context, callers must make sure that
  1024. * every task struct that event->ctx->task could possibly point to
  1025. * remains valid. This condition is satisifed when called through
  1026. * perf_event_for_each_child or perf_event_for_each because they
  1027. * hold the top-level event's child_mutex, so any descendant that
  1028. * goes to exit will block in sync_child_event.
  1029. * When called from perf_pending_event it's OK because event->ctx
  1030. * is the current context on this CPU and preemption is disabled,
  1031. * hence we can't get into perf_event_task_sched_out for this context.
  1032. */
  1033. void perf_event_disable(struct perf_event *event)
  1034. {
  1035. struct perf_event_context *ctx = event->ctx;
  1036. struct task_struct *task = ctx->task;
  1037. if (!task) {
  1038. /*
  1039. * Disable the event on the cpu that it's on
  1040. */
  1041. cpu_function_call(event->cpu, __perf_event_disable, event);
  1042. return;
  1043. }
  1044. retry:
  1045. if (!task_function_call(task, __perf_event_disable, event))
  1046. return;
  1047. raw_spin_lock_irq(&ctx->lock);
  1048. /*
  1049. * If the event is still active, we need to retry the cross-call.
  1050. */
  1051. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  1052. raw_spin_unlock_irq(&ctx->lock);
  1053. /*
  1054. * Reload the task pointer, it might have been changed by
  1055. * a concurrent perf_event_context_sched_out().
  1056. */
  1057. task = ctx->task;
  1058. goto retry;
  1059. }
  1060. /*
  1061. * Since we have the lock this context can't be scheduled
  1062. * in, so we can change the state safely.
  1063. */
  1064. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1065. update_group_times(event);
  1066. event->state = PERF_EVENT_STATE_OFF;
  1067. }
  1068. raw_spin_unlock_irq(&ctx->lock);
  1069. }
  1070. static void perf_set_shadow_time(struct perf_event *event,
  1071. struct perf_event_context *ctx,
  1072. u64 tstamp)
  1073. {
  1074. /*
  1075. * use the correct time source for the time snapshot
  1076. *
  1077. * We could get by without this by leveraging the
  1078. * fact that to get to this function, the caller
  1079. * has most likely already called update_context_time()
  1080. * and update_cgrp_time_xx() and thus both timestamp
  1081. * are identical (or very close). Given that tstamp is,
  1082. * already adjusted for cgroup, we could say that:
  1083. * tstamp - ctx->timestamp
  1084. * is equivalent to
  1085. * tstamp - cgrp->timestamp.
  1086. *
  1087. * Then, in perf_output_read(), the calculation would
  1088. * work with no changes because:
  1089. * - event is guaranteed scheduled in
  1090. * - no scheduled out in between
  1091. * - thus the timestamp would be the same
  1092. *
  1093. * But this is a bit hairy.
  1094. *
  1095. * So instead, we have an explicit cgroup call to remain
  1096. * within the time time source all along. We believe it
  1097. * is cleaner and simpler to understand.
  1098. */
  1099. if (is_cgroup_event(event))
  1100. perf_cgroup_set_shadow_time(event, tstamp);
  1101. else
  1102. event->shadow_ctx_time = tstamp - ctx->timestamp;
  1103. }
  1104. #define MAX_INTERRUPTS (~0ULL)
  1105. static void perf_log_throttle(struct perf_event *event, int enable);
  1106. static int
  1107. event_sched_in(struct perf_event *event,
  1108. struct perf_cpu_context *cpuctx,
  1109. struct perf_event_context *ctx)
  1110. {
  1111. u64 tstamp = perf_event_time(event);
  1112. if (event->state <= PERF_EVENT_STATE_OFF)
  1113. return 0;
  1114. event->state = PERF_EVENT_STATE_ACTIVE;
  1115. event->oncpu = smp_processor_id();
  1116. /*
  1117. * Unthrottle events, since we scheduled we might have missed several
  1118. * ticks already, also for a heavily scheduling task there is little
  1119. * guarantee it'll get a tick in a timely manner.
  1120. */
  1121. if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
  1122. perf_log_throttle(event, 1);
  1123. event->hw.interrupts = 0;
  1124. }
  1125. /*
  1126. * The new state must be visible before we turn it on in the hardware:
  1127. */
  1128. smp_wmb();
  1129. if (event->pmu->add(event, PERF_EF_START)) {
  1130. event->state = PERF_EVENT_STATE_INACTIVE;
  1131. event->oncpu = -1;
  1132. return -EAGAIN;
  1133. }
  1134. event->tstamp_running += tstamp - event->tstamp_stopped;
  1135. perf_set_shadow_time(event, ctx, tstamp);
  1136. if (!is_software_event(event))
  1137. cpuctx->active_oncpu++;
  1138. ctx->nr_active++;
  1139. if (event->attr.exclusive)
  1140. cpuctx->exclusive = 1;
  1141. return 0;
  1142. }
  1143. static int
  1144. group_sched_in(struct perf_event *group_event,
  1145. struct perf_cpu_context *cpuctx,
  1146. struct perf_event_context *ctx)
  1147. {
  1148. struct perf_event *event, *partial_group = NULL;
  1149. struct pmu *pmu = group_event->pmu;
  1150. u64 now = ctx->time;
  1151. bool simulate = false;
  1152. if (group_event->state == PERF_EVENT_STATE_OFF)
  1153. return 0;
  1154. pmu->start_txn(pmu);
  1155. if (event_sched_in(group_event, cpuctx, ctx)) {
  1156. pmu->cancel_txn(pmu);
  1157. return -EAGAIN;
  1158. }
  1159. /*
  1160. * Schedule in siblings as one group (if any):
  1161. */
  1162. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  1163. if (event_sched_in(event, cpuctx, ctx)) {
  1164. partial_group = event;
  1165. goto group_error;
  1166. }
  1167. }
  1168. if (!pmu->commit_txn(pmu))
  1169. return 0;
  1170. group_error:
  1171. /*
  1172. * Groups can be scheduled in as one unit only, so undo any
  1173. * partial group before returning:
  1174. * The events up to the failed event are scheduled out normally,
  1175. * tstamp_stopped will be updated.
  1176. *
  1177. * The failed events and the remaining siblings need to have
  1178. * their timings updated as if they had gone thru event_sched_in()
  1179. * and event_sched_out(). This is required to get consistent timings
  1180. * across the group. This also takes care of the case where the group
  1181. * could never be scheduled by ensuring tstamp_stopped is set to mark
  1182. * the time the event was actually stopped, such that time delta
  1183. * calculation in update_event_times() is correct.
  1184. */
  1185. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  1186. if (event == partial_group)
  1187. simulate = true;
  1188. if (simulate) {
  1189. event->tstamp_running += now - event->tstamp_stopped;
  1190. event->tstamp_stopped = now;
  1191. } else {
  1192. event_sched_out(event, cpuctx, ctx);
  1193. }
  1194. }
  1195. event_sched_out(group_event, cpuctx, ctx);
  1196. pmu->cancel_txn(pmu);
  1197. return -EAGAIN;
  1198. }
  1199. /*
  1200. * Work out whether we can put this event group on the CPU now.
  1201. */
  1202. static int group_can_go_on(struct perf_event *event,
  1203. struct perf_cpu_context *cpuctx,
  1204. int can_add_hw)
  1205. {
  1206. /*
  1207. * Groups consisting entirely of software events can always go on.
  1208. */
  1209. if (event->group_flags & PERF_GROUP_SOFTWARE)
  1210. return 1;
  1211. /*
  1212. * If an exclusive group is already on, no other hardware
  1213. * events can go on.
  1214. */
  1215. if (cpuctx->exclusive)
  1216. return 0;
  1217. /*
  1218. * If this group is exclusive and there are already
  1219. * events on the CPU, it can't go on.
  1220. */
  1221. if (event->attr.exclusive && cpuctx->active_oncpu)
  1222. return 0;
  1223. /*
  1224. * Otherwise, try to add it if all previous groups were able
  1225. * to go on.
  1226. */
  1227. return can_add_hw;
  1228. }
  1229. static void add_event_to_ctx(struct perf_event *event,
  1230. struct perf_event_context *ctx)
  1231. {
  1232. u64 tstamp = perf_event_time(event);
  1233. list_add_event(event, ctx);
  1234. perf_group_attach(event);
  1235. event->tstamp_enabled = tstamp;
  1236. event->tstamp_running = tstamp;
  1237. event->tstamp_stopped = tstamp;
  1238. }
  1239. static void task_ctx_sched_out(struct perf_event_context *ctx);
  1240. static void
  1241. ctx_sched_in(struct perf_event_context *ctx,
  1242. struct perf_cpu_context *cpuctx,
  1243. enum event_type_t event_type,
  1244. struct task_struct *task);
  1245. static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
  1246. struct perf_event_context *ctx,
  1247. struct task_struct *task)
  1248. {
  1249. cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
  1250. if (ctx)
  1251. ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
  1252. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
  1253. if (ctx)
  1254. ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
  1255. }
  1256. /*
  1257. * Cross CPU call to install and enable a performance event
  1258. *
  1259. * Must be called with ctx->mutex held
  1260. */
  1261. static int __perf_install_in_context(void *info)
  1262. {
  1263. struct perf_event *event = info;
  1264. struct perf_event_context *ctx = event->ctx;
  1265. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1266. struct perf_event_context *task_ctx = cpuctx->task_ctx;
  1267. struct task_struct *task = current;
  1268. perf_ctx_lock(cpuctx, task_ctx);
  1269. perf_pmu_disable(cpuctx->ctx.pmu);
  1270. /*
  1271. * If there was an active task_ctx schedule it out.
  1272. */
  1273. if (task_ctx)
  1274. task_ctx_sched_out(task_ctx);
  1275. /*
  1276. * If the context we're installing events in is not the
  1277. * active task_ctx, flip them.
  1278. */
  1279. if (ctx->task && task_ctx != ctx) {
  1280. if (task_ctx)
  1281. raw_spin_unlock(&task_ctx->lock);
  1282. raw_spin_lock(&ctx->lock);
  1283. task_ctx = ctx;
  1284. }
  1285. if (task_ctx) {
  1286. cpuctx->task_ctx = task_ctx;
  1287. task = task_ctx->task;
  1288. }
  1289. cpu_ctx_sched_out(cpuctx, EVENT_ALL);
  1290. update_context_time(ctx);
  1291. /*
  1292. * update cgrp time only if current cgrp
  1293. * matches event->cgrp. Must be done before
  1294. * calling add_event_to_ctx()
  1295. */
  1296. update_cgrp_time_from_event(event);
  1297. add_event_to_ctx(event, ctx);
  1298. /*
  1299. * Schedule everything back in
  1300. */
  1301. perf_event_sched_in(cpuctx, task_ctx, task);
  1302. perf_pmu_enable(cpuctx->ctx.pmu);
  1303. perf_ctx_unlock(cpuctx, task_ctx);
  1304. return 0;
  1305. }
  1306. /*
  1307. * Attach a performance event to a context
  1308. *
  1309. * First we add the event to the list with the hardware enable bit
  1310. * in event->hw_config cleared.
  1311. *
  1312. * If the event is attached to a task which is on a CPU we use a smp
  1313. * call to enable it in the task context. The task might have been
  1314. * scheduled away, but we check this in the smp call again.
  1315. */
  1316. static void
  1317. perf_install_in_context(struct perf_event_context *ctx,
  1318. struct perf_event *event,
  1319. int cpu)
  1320. {
  1321. struct task_struct *task = ctx->task;
  1322. lockdep_assert_held(&ctx->mutex);
  1323. event->ctx = ctx;
  1324. if (!task) {
  1325. /*
  1326. * Per cpu events are installed via an smp call and
  1327. * the install is always successful.
  1328. */
  1329. cpu_function_call(cpu, __perf_install_in_context, event);
  1330. return;
  1331. }
  1332. retry:
  1333. if (!task_function_call(task, __perf_install_in_context, event))
  1334. return;
  1335. raw_spin_lock_irq(&ctx->lock);
  1336. /*
  1337. * If we failed to find a running task, but find the context active now
  1338. * that we've acquired the ctx->lock, retry.
  1339. */
  1340. if (ctx->is_active) {
  1341. raw_spin_unlock_irq(&ctx->lock);
  1342. goto retry;
  1343. }
  1344. /*
  1345. * Since the task isn't running, its safe to add the event, us holding
  1346. * the ctx->lock ensures the task won't get scheduled in.
  1347. */
  1348. add_event_to_ctx(event, ctx);
  1349. raw_spin_unlock_irq(&ctx->lock);
  1350. }
  1351. /*
  1352. * Put a event into inactive state and update time fields.
  1353. * Enabling the leader of a group effectively enables all
  1354. * the group members that aren't explicitly disabled, so we
  1355. * have to update their ->tstamp_enabled also.
  1356. * Note: this works for group members as well as group leaders
  1357. * since the non-leader members' sibling_lists will be empty.
  1358. */
  1359. static void __perf_event_mark_enabled(struct perf_event *event,
  1360. struct perf_event_context *ctx)
  1361. {
  1362. struct perf_event *sub;
  1363. u64 tstamp = perf_event_time(event);
  1364. event->state = PERF_EVENT_STATE_INACTIVE;
  1365. event->tstamp_enabled = tstamp - event->total_time_enabled;
  1366. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  1367. if (sub->state >= PERF_EVENT_STATE_INACTIVE)
  1368. sub->tstamp_enabled = tstamp - sub->total_time_enabled;
  1369. }
  1370. }
  1371. /*
  1372. * Cross CPU call to enable a performance event
  1373. */
  1374. static int __perf_event_enable(void *info)
  1375. {
  1376. struct perf_event *event = info;
  1377. struct perf_event_context *ctx = event->ctx;
  1378. struct perf_event *leader = event->group_leader;
  1379. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1380. int err;
  1381. if (WARN_ON_ONCE(!ctx->is_active))
  1382. return -EINVAL;
  1383. raw_spin_lock(&ctx->lock);
  1384. update_context_time(ctx);
  1385. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  1386. goto unlock;
  1387. /*
  1388. * set current task's cgroup time reference point
  1389. */
  1390. perf_cgroup_set_timestamp(current, ctx);
  1391. __perf_event_mark_enabled(event, ctx);
  1392. if (!event_filter_match(event)) {
  1393. if (is_cgroup_event(event))
  1394. perf_cgroup_defer_enabled(event);
  1395. goto unlock;
  1396. }
  1397. /*
  1398. * If the event is in a group and isn't the group leader,
  1399. * then don't put it on unless the group is on.
  1400. */
  1401. if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
  1402. goto unlock;
  1403. if (!group_can_go_on(event, cpuctx, 1)) {
  1404. err = -EEXIST;
  1405. } else {
  1406. if (event == leader)
  1407. err = group_sched_in(event, cpuctx, ctx);
  1408. else
  1409. err = event_sched_in(event, cpuctx, ctx);
  1410. }
  1411. if (err) {
  1412. /*
  1413. * If this event can't go on and it's part of a
  1414. * group, then the whole group has to come off.
  1415. */
  1416. if (leader != event)
  1417. group_sched_out(leader, cpuctx, ctx);
  1418. if (leader->attr.pinned) {
  1419. update_group_times(leader);
  1420. leader->state = PERF_EVENT_STATE_ERROR;
  1421. }
  1422. }
  1423. unlock:
  1424. raw_spin_unlock(&ctx->lock);
  1425. return 0;
  1426. }
  1427. /*
  1428. * Enable a event.
  1429. *
  1430. * If event->ctx is a cloned context, callers must make sure that
  1431. * every task struct that event->ctx->task could possibly point to
  1432. * remains valid. This condition is satisfied when called through
  1433. * perf_event_for_each_child or perf_event_for_each as described
  1434. * for perf_event_disable.
  1435. */
  1436. void perf_event_enable(struct perf_event *event)
  1437. {
  1438. struct perf_event_context *ctx = event->ctx;
  1439. struct task_struct *task = ctx->task;
  1440. if (!task) {
  1441. /*
  1442. * Enable the event on the cpu that it's on
  1443. */
  1444. cpu_function_call(event->cpu, __perf_event_enable, event);
  1445. return;
  1446. }
  1447. raw_spin_lock_irq(&ctx->lock);
  1448. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  1449. goto out;
  1450. /*
  1451. * If the event is in error state, clear that first.
  1452. * That way, if we see the event in error state below, we
  1453. * know that it has gone back into error state, as distinct
  1454. * from the task having been scheduled away before the
  1455. * cross-call arrived.
  1456. */
  1457. if (event->state == PERF_EVENT_STATE_ERROR)
  1458. event->state = PERF_EVENT_STATE_OFF;
  1459. retry:
  1460. if (!ctx->is_active) {
  1461. __perf_event_mark_enabled(event, ctx);
  1462. goto out;
  1463. }
  1464. raw_spin_unlock_irq(&ctx->lock);
  1465. if (!task_function_call(task, __perf_event_enable, event))
  1466. return;
  1467. raw_spin_lock_irq(&ctx->lock);
  1468. /*
  1469. * If the context is active and the event is still off,
  1470. * we need to retry the cross-call.
  1471. */
  1472. if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
  1473. /*
  1474. * task could have been flipped by a concurrent
  1475. * perf_event_context_sched_out()
  1476. */
  1477. task = ctx->task;
  1478. goto retry;
  1479. }
  1480. out:
  1481. raw_spin_unlock_irq(&ctx->lock);
  1482. }
  1483. int perf_event_refresh(struct perf_event *event, int refresh)
  1484. {
  1485. /*
  1486. * not supported on inherited events
  1487. */
  1488. if (event->attr.inherit || !is_sampling_event(event))
  1489. return -EINVAL;
  1490. atomic_add(refresh, &event->event_limit);
  1491. perf_event_enable(event);
  1492. return 0;
  1493. }
  1494. EXPORT_SYMBOL_GPL(perf_event_refresh);
  1495. static void ctx_sched_out(struct perf_event_context *ctx,
  1496. struct perf_cpu_context *cpuctx,
  1497. enum event_type_t event_type)
  1498. {
  1499. struct perf_event *event;
  1500. int is_active = ctx->is_active;
  1501. ctx->is_active &= ~event_type;
  1502. if (likely(!ctx->nr_events))
  1503. return;
  1504. update_context_time(ctx);
  1505. update_cgrp_time_from_cpuctx(cpuctx);
  1506. if (!ctx->nr_active)
  1507. return;
  1508. perf_pmu_disable(ctx->pmu);
  1509. if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
  1510. list_for_each_entry(event, &ctx->pinned_groups, group_entry)
  1511. group_sched_out(event, cpuctx, ctx);
  1512. }
  1513. if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
  1514. list_for_each_entry(event, &ctx->flexible_groups, group_entry)
  1515. group_sched_out(event, cpuctx, ctx);
  1516. }
  1517. perf_pmu_enable(ctx->pmu);
  1518. }
  1519. /*
  1520. * Test whether two contexts are equivalent, i.e. whether they
  1521. * have both been cloned from the same version of the same context
  1522. * and they both have the same number of enabled events.
  1523. * If the number of enabled events is the same, then the set
  1524. * of enabled events should be the same, because these are both
  1525. * inherited contexts, therefore we can't access individual events
  1526. * in them directly with an fd; we can only enable/disable all
  1527. * events via prctl, or enable/disable all events in a family
  1528. * via ioctl, which will have the same effect on both contexts.
  1529. */
  1530. static int context_equiv(struct perf_event_context *ctx1,
  1531. struct perf_event_context *ctx2)
  1532. {
  1533. return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
  1534. && ctx1->parent_gen == ctx2->parent_gen
  1535. && !ctx1->pin_count && !ctx2->pin_count;
  1536. }
  1537. static void __perf_event_sync_stat(struct perf_event *event,
  1538. struct perf_event *next_event)
  1539. {
  1540. u64 value;
  1541. if (!event->attr.inherit_stat)
  1542. return;
  1543. /*
  1544. * Update the event value, we cannot use perf_event_read()
  1545. * because we're in the middle of a context switch and have IRQs
  1546. * disabled, which upsets smp_call_function_single(), however
  1547. * we know the event must be on the current CPU, therefore we
  1548. * don't need to use it.
  1549. */
  1550. switch (event->state) {
  1551. case PERF_EVENT_STATE_ACTIVE:
  1552. event->pmu->read(event);
  1553. /* fall-through */
  1554. case PERF_EVENT_STATE_INACTIVE:
  1555. update_event_times(event);
  1556. break;
  1557. default:
  1558. break;
  1559. }
  1560. /*
  1561. * In order to keep per-task stats reliable we need to flip the event
  1562. * values when we flip the contexts.
  1563. */
  1564. value = local64_read(&next_event->count);
  1565. value = local64_xchg(&event->count, value);
  1566. local64_set(&next_event->count, value);
  1567. swap(event->total_time_enabled, next_event->total_time_enabled);
  1568. swap(event->total_time_running, next_event->total_time_running);
  1569. /*
  1570. * Since we swizzled the values, update the user visible data too.
  1571. */
  1572. perf_event_update_userpage(event);
  1573. perf_event_update_userpage(next_event);
  1574. }
  1575. #define list_next_entry(pos, member) \
  1576. list_entry(pos->member.next, typeof(*pos), member)
  1577. static void perf_event_sync_stat(struct perf_event_context *ctx,
  1578. struct perf_event_context *next_ctx)
  1579. {
  1580. struct perf_event *event, *next_event;
  1581. if (!ctx->nr_stat)
  1582. return;
  1583. update_context_time(ctx);
  1584. event = list_first_entry(&ctx->event_list,
  1585. struct perf_event, event_entry);
  1586. next_event = list_first_entry(&next_ctx->event_list,
  1587. struct perf_event, event_entry);
  1588. while (&event->event_entry != &ctx->event_list &&
  1589. &next_event->event_entry != &next_ctx->event_list) {
  1590. __perf_event_sync_stat(event, next_event);
  1591. event = list_next_entry(event, event_entry);
  1592. next_event = list_next_entry(next_event, event_entry);
  1593. }
  1594. }
  1595. static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
  1596. struct task_struct *next)
  1597. {
  1598. struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
  1599. struct perf_event_context *next_ctx;
  1600. struct perf_event_context *parent;
  1601. struct perf_cpu_context *cpuctx;
  1602. int do_switch = 1;
  1603. if (likely(!ctx))
  1604. return;
  1605. cpuctx = __get_cpu_context(ctx);
  1606. if (!cpuctx->task_ctx)
  1607. return;
  1608. rcu_read_lock();
  1609. parent = rcu_dereference(ctx->parent_ctx);
  1610. next_ctx = next->perf_event_ctxp[ctxn];
  1611. if (parent && next_ctx &&
  1612. rcu_dereference(next_ctx->parent_ctx) == parent) {
  1613. /*
  1614. * Looks like the two contexts are clones, so we might be
  1615. * able to optimize the context switch. We lock both
  1616. * contexts and check that they are clones under the
  1617. * lock (including re-checking that neither has been
  1618. * uncloned in the meantime). It doesn't matter which
  1619. * order we take the locks because no other cpu could
  1620. * be trying to lock both of these tasks.
  1621. */
  1622. raw_spin_lock(&ctx->lock);
  1623. raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
  1624. if (context_equiv(ctx, next_ctx)) {
  1625. /*
  1626. * XXX do we need a memory barrier of sorts
  1627. * wrt to rcu_dereference() of perf_event_ctxp
  1628. */
  1629. task->perf_event_ctxp[ctxn] = next_ctx;
  1630. next->perf_event_ctxp[ctxn] = ctx;
  1631. ctx->task = next;
  1632. next_ctx->task = task;
  1633. do_switch = 0;
  1634. perf_event_sync_stat(ctx, next_ctx);
  1635. }
  1636. raw_spin_unlock(&next_ctx->lock);
  1637. raw_spin_unlock(&ctx->lock);
  1638. }
  1639. rcu_read_unlock();
  1640. if (do_switch) {
  1641. raw_spin_lock(&ctx->lock);
  1642. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  1643. cpuctx->task_ctx = NULL;
  1644. raw_spin_unlock(&ctx->lock);
  1645. }
  1646. }
  1647. #define for_each_task_context_nr(ctxn) \
  1648. for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
  1649. /*
  1650. * Called from scheduler to remove the events of the current task,
  1651. * with interrupts disabled.
  1652. *
  1653. * We stop each event and update the event value in event->count.
  1654. *
  1655. * This does not protect us against NMI, but disable()
  1656. * sets the disabled bit in the control field of event _before_
  1657. * accessing the event control register. If a NMI hits, then it will
  1658. * not restart the event.
  1659. */
  1660. void __perf_event_task_sched_out(struct task_struct *task,
  1661. struct task_struct *next)
  1662. {
  1663. int ctxn;
  1664. for_each_task_context_nr(ctxn)
  1665. perf_event_context_sched_out(task, ctxn, next);
  1666. /*
  1667. * if cgroup events exist on this CPU, then we need
  1668. * to check if we have to switch out PMU state.
  1669. * cgroup event are system-wide mode only
  1670. */
  1671. if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
  1672. perf_cgroup_sched_out(task);
  1673. }
  1674. static void task_ctx_sched_out(struct perf_event_context *ctx)
  1675. {
  1676. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1677. if (!cpuctx->task_ctx)
  1678. return;
  1679. if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
  1680. return;
  1681. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  1682. cpuctx->task_ctx = NULL;
  1683. }
  1684. /*
  1685. * Called with IRQs disabled
  1686. */
  1687. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  1688. enum event_type_t event_type)
  1689. {
  1690. ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
  1691. }
  1692. static void
  1693. ctx_pinned_sched_in(struct perf_event_context *ctx,
  1694. struct perf_cpu_context *cpuctx)
  1695. {
  1696. struct perf_event *event;
  1697. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  1698. if (event->state <= PERF_EVENT_STATE_OFF)
  1699. continue;
  1700. if (!event_filter_match(event))
  1701. continue;
  1702. /* may need to reset tstamp_enabled */
  1703. if (is_cgroup_event(event))
  1704. perf_cgroup_mark_enabled(event, ctx);
  1705. if (group_can_go_on(event, cpuctx, 1))
  1706. group_sched_in(event, cpuctx, ctx);
  1707. /*
  1708. * If this pinned group hasn't been scheduled,
  1709. * put it in error state.
  1710. */
  1711. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1712. update_group_times(event);
  1713. event->state = PERF_EVENT_STATE_ERROR;
  1714. }
  1715. }
  1716. }
  1717. static void
  1718. ctx_flexible_sched_in(struct perf_event_context *ctx,
  1719. struct perf_cpu_context *cpuctx)
  1720. {
  1721. struct perf_event *event;
  1722. int can_add_hw = 1;
  1723. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  1724. /* Ignore events in OFF or ERROR state */
  1725. if (event->state <= PERF_EVENT_STATE_OFF)
  1726. continue;
  1727. /*
  1728. * Listen to the 'cpu' scheduling filter constraint
  1729. * of events:
  1730. */
  1731. if (!event_filter_match(event))
  1732. continue;
  1733. /* may need to reset tstamp_enabled */
  1734. if (is_cgroup_event(event))
  1735. perf_cgroup_mark_enabled(event, ctx);
  1736. if (group_can_go_on(event, cpuctx, can_add_hw)) {
  1737. if (group_sched_in(event, cpuctx, ctx))
  1738. can_add_hw = 0;
  1739. }
  1740. }
  1741. }
  1742. static void
  1743. ctx_sched_in(struct perf_event_context *ctx,
  1744. struct perf_cpu_context *cpuctx,
  1745. enum event_type_t event_type,
  1746. struct task_struct *task)
  1747. {
  1748. u64 now;
  1749. int is_active = ctx->is_active;
  1750. ctx->is_active |= event_type;
  1751. if (likely(!ctx->nr_events))
  1752. return;
  1753. now = perf_clock();
  1754. ctx->timestamp = now;
  1755. perf_cgroup_set_timestamp(task, ctx);
  1756. /*
  1757. * First go through the list and put on any pinned groups
  1758. * in order to give them the best chance of going on.
  1759. */
  1760. if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
  1761. ctx_pinned_sched_in(ctx, cpuctx);
  1762. /* Then walk through the lower prio flexible groups */
  1763. if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
  1764. ctx_flexible_sched_in(ctx, cpuctx);
  1765. }
  1766. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  1767. enum event_type_t event_type,
  1768. struct task_struct *task)
  1769. {
  1770. struct perf_event_context *ctx = &cpuctx->ctx;
  1771. ctx_sched_in(ctx, cpuctx, event_type, task);
  1772. }
  1773. static void perf_event_context_sched_in(struct perf_event_context *ctx,
  1774. struct task_struct *task)
  1775. {
  1776. struct perf_cpu_context *cpuctx;
  1777. cpuctx = __get_cpu_context(ctx);
  1778. if (cpuctx->task_ctx == ctx)
  1779. return;
  1780. perf_ctx_lock(cpuctx, ctx);
  1781. perf_pmu_disable(ctx->pmu);
  1782. /*
  1783. * We want to keep the following priority order:
  1784. * cpu pinned (that don't need to move), task pinned,
  1785. * cpu flexible, task flexible.
  1786. */
  1787. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  1788. perf_event_sched_in(cpuctx, ctx, task);
  1789. cpuctx->task_ctx = ctx;
  1790. perf_pmu_enable(ctx->pmu);
  1791. perf_ctx_unlock(cpuctx, ctx);
  1792. /*
  1793. * Since these rotations are per-cpu, we need to ensure the
  1794. * cpu-context we got scheduled on is actually rotating.
  1795. */
  1796. perf_pmu_rotate_start(ctx->pmu);
  1797. }
  1798. /*
  1799. * Called from scheduler to add the events of the current task
  1800. * with interrupts disabled.
  1801. *
  1802. * We restore the event value and then enable it.
  1803. *
  1804. * This does not protect us against NMI, but enable()
  1805. * sets the enabled bit in the control field of event _before_
  1806. * accessing the event control register. If a NMI hits, then it will
  1807. * keep the event running.
  1808. */
  1809. void __perf_event_task_sched_in(struct task_struct *task)
  1810. {
  1811. struct perf_event_context *ctx;
  1812. int ctxn;
  1813. for_each_task_context_nr(ctxn) {
  1814. ctx = task->perf_event_ctxp[ctxn];
  1815. if (likely(!ctx))
  1816. continue;
  1817. perf_event_context_sched_in(ctx, task);
  1818. }
  1819. /*
  1820. * if cgroup events exist on this CPU, then we need
  1821. * to check if we have to switch in PMU state.
  1822. * cgroup event are system-wide mode only
  1823. */
  1824. if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
  1825. perf_cgroup_sched_in(task);
  1826. }
  1827. static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
  1828. {
  1829. u64 frequency = event->attr.sample_freq;
  1830. u64 sec = NSEC_PER_SEC;
  1831. u64 divisor, dividend;
  1832. int count_fls, nsec_fls, frequency_fls, sec_fls;
  1833. count_fls = fls64(count);
  1834. nsec_fls = fls64(nsec);
  1835. frequency_fls = fls64(frequency);
  1836. sec_fls = 30;
  1837. /*
  1838. * We got @count in @nsec, with a target of sample_freq HZ
  1839. * the target period becomes:
  1840. *
  1841. * @count * 10^9
  1842. * period = -------------------
  1843. * @nsec * sample_freq
  1844. *
  1845. */
  1846. /*
  1847. * Reduce accuracy by one bit such that @a and @b converge
  1848. * to a similar magnitude.
  1849. */
  1850. #define REDUCE_FLS(a, b) \
  1851. do { \
  1852. if (a##_fls > b##_fls) { \
  1853. a >>= 1; \
  1854. a##_fls--; \
  1855. } else { \
  1856. b >>= 1; \
  1857. b##_fls--; \
  1858. } \
  1859. } while (0)
  1860. /*
  1861. * Reduce accuracy until either term fits in a u64, then proceed with
  1862. * the other, so that finally we can do a u64/u64 division.
  1863. */
  1864. while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
  1865. REDUCE_FLS(nsec, frequency);
  1866. REDUCE_FLS(sec, count);
  1867. }
  1868. if (count_fls + sec_fls > 64) {
  1869. divisor = nsec * frequency;
  1870. while (count_fls + sec_fls > 64) {
  1871. REDUCE_FLS(count, sec);
  1872. divisor >>= 1;
  1873. }
  1874. dividend = count * sec;
  1875. } else {
  1876. dividend = count * sec;
  1877. while (nsec_fls + frequency_fls > 64) {
  1878. REDUCE_FLS(nsec, frequency);
  1879. dividend >>= 1;
  1880. }
  1881. divisor = nsec * frequency;
  1882. }
  1883. if (!divisor)
  1884. return dividend;
  1885. return div64_u64(dividend, divisor);
  1886. }
  1887. static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
  1888. {
  1889. struct hw_perf_event *hwc = &event->hw;
  1890. s64 period, sample_period;
  1891. s64 delta;
  1892. period = perf_calculate_period(event, nsec, count);
  1893. delta = (s64)(period - hwc->sample_period);
  1894. delta = (delta + 7) / 8; /* low pass filter */
  1895. sample_period = hwc->sample_period + delta;
  1896. if (!sample_period)
  1897. sample_period = 1;
  1898. hwc->sample_period = sample_period;
  1899. if (local64_read(&hwc->period_left) > 8*sample_period) {
  1900. event->pmu->stop(event, PERF_EF_UPDATE);
  1901. local64_set(&hwc->period_left, 0);
  1902. event->pmu->start(event, PERF_EF_RELOAD);
  1903. }
  1904. }
  1905. static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
  1906. {
  1907. struct perf_event *event;
  1908. struct hw_perf_event *hwc;
  1909. u64 interrupts, now;
  1910. s64 delta;
  1911. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  1912. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1913. continue;
  1914. if (!event_filter_match(event))
  1915. continue;
  1916. hwc = &event->hw;
  1917. interrupts = hwc->interrupts;
  1918. hwc->interrupts = 0;
  1919. /*
  1920. * unthrottle events on the tick
  1921. */
  1922. if (interrupts == MAX_INTERRUPTS) {
  1923. perf_log_throttle(event, 1);
  1924. event->pmu->start(event, 0);
  1925. }
  1926. if (!event->attr.freq || !event->attr.sample_freq)
  1927. continue;
  1928. event->pmu->read(event);
  1929. now = local64_read(&event->count);
  1930. delta = now - hwc->freq_count_stamp;
  1931. hwc->freq_count_stamp = now;
  1932. if (delta > 0)
  1933. perf_adjust_period(event, period, delta);
  1934. }
  1935. }
  1936. /*
  1937. * Round-robin a context's events:
  1938. */
  1939. static void rotate_ctx(struct perf_event_context *ctx)
  1940. {
  1941. /*
  1942. * Rotate the first entry last of non-pinned groups. Rotation might be
  1943. * disabled by the inheritance code.
  1944. */
  1945. if (!ctx->rotate_disable)
  1946. list_rotate_left(&ctx->flexible_groups);
  1947. }
  1948. /*
  1949. * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
  1950. * because they're strictly cpu affine and rotate_start is called with IRQs
  1951. * disabled, while rotate_context is called from IRQ context.
  1952. */
  1953. static void perf_rotate_context(struct perf_cpu_context *cpuctx)
  1954. {
  1955. u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
  1956. struct perf_event_context *ctx = NULL;
  1957. int rotate = 0, remove = 1;
  1958. if (cpuctx->ctx.nr_events) {
  1959. remove = 0;
  1960. if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
  1961. rotate = 1;
  1962. }
  1963. ctx = cpuctx->task_ctx;
  1964. if (ctx && ctx->nr_events) {
  1965. remove = 0;
  1966. if (ctx->nr_events != ctx->nr_active)
  1967. rotate = 1;
  1968. }
  1969. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  1970. perf_pmu_disable(cpuctx->ctx.pmu);
  1971. perf_ctx_adjust_freq(&cpuctx->ctx, interval);
  1972. if (ctx)
  1973. perf_ctx_adjust_freq(ctx, interval);
  1974. if (!rotate)
  1975. goto done;
  1976. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  1977. if (ctx)
  1978. ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
  1979. rotate_ctx(&cpuctx->ctx);
  1980. if (ctx)
  1981. rotate_ctx(ctx);
  1982. perf_event_sched_in(cpuctx, ctx, current);
  1983. done:
  1984. if (remove)
  1985. list_del_init(&cpuctx->rotation_list);
  1986. perf_pmu_enable(cpuctx->ctx.pmu);
  1987. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  1988. }
  1989. void perf_event_task_tick(void)
  1990. {
  1991. struct list_head *head = &__get_cpu_var(rotation_list);
  1992. struct perf_cpu_context *cpuctx, *tmp;
  1993. WARN_ON(!irqs_disabled());
  1994. list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
  1995. if (cpuctx->jiffies_interval == 1 ||
  1996. !(jiffies % cpuctx->jiffies_interval))
  1997. perf_rotate_context(cpuctx);
  1998. }
  1999. }
  2000. static int event_enable_on_exec(struct perf_event *event,
  2001. struct perf_event_context *ctx)
  2002. {
  2003. if (!event->attr.enable_on_exec)
  2004. return 0;
  2005. event->attr.enable_on_exec = 0;
  2006. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  2007. return 0;
  2008. __perf_event_mark_enabled(event, ctx);
  2009. return 1;
  2010. }
  2011. /*
  2012. * Enable all of a task's events that have been marked enable-on-exec.
  2013. * This expects task == current.
  2014. */
  2015. static void perf_event_enable_on_exec(struct perf_event_context *ctx)
  2016. {
  2017. struct perf_event *event;
  2018. unsigned long flags;
  2019. int enabled = 0;
  2020. int ret;
  2021. local_irq_save(flags);
  2022. if (!ctx || !ctx->nr_events)
  2023. goto out;
  2024. /*
  2025. * We must ctxsw out cgroup events to avoid conflict
  2026. * when invoking perf_task_event_sched_in() later on
  2027. * in this function. Otherwise we end up trying to
  2028. * ctxswin cgroup events which are already scheduled
  2029. * in.
  2030. */
  2031. perf_cgroup_sched_out(current);
  2032. raw_spin_lock(&ctx->lock);
  2033. task_ctx_sched_out(ctx);
  2034. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  2035. ret = event_enable_on_exec(event, ctx);
  2036. if (ret)
  2037. enabled = 1;
  2038. }
  2039. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  2040. ret = event_enable_on_exec(event, ctx);
  2041. if (ret)
  2042. enabled = 1;
  2043. }
  2044. /*
  2045. * Unclone this context if we enabled any event.
  2046. */
  2047. if (enabled)
  2048. unclone_ctx(ctx);
  2049. raw_spin_unlock(&ctx->lock);
  2050. /*
  2051. * Also calls ctxswin for cgroup events, if any:
  2052. */
  2053. perf_event_context_sched_in(ctx, ctx->task);
  2054. out:
  2055. local_irq_restore(flags);
  2056. }
  2057. /*
  2058. * Cross CPU call to read the hardware event
  2059. */
  2060. static void __perf_event_read(void *info)
  2061. {
  2062. struct perf_event *event = info;
  2063. struct perf_event_context *ctx = event->ctx;
  2064. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  2065. /*
  2066. * If this is a task context, we need to check whether it is
  2067. * the current task context of this cpu. If not it has been
  2068. * scheduled out before the smp call arrived. In that case
  2069. * event->count would have been updated to a recent sample
  2070. * when the event was scheduled out.
  2071. */
  2072. if (ctx->task && cpuctx->task_ctx != ctx)
  2073. return;
  2074. raw_spin_lock(&ctx->lock);
  2075. if (ctx->is_active) {
  2076. update_context_time(ctx);
  2077. update_cgrp_time_from_event(event);
  2078. }
  2079. update_event_times(event);
  2080. if (event->state == PERF_EVENT_STATE_ACTIVE)
  2081. event->pmu->read(event);
  2082. raw_spin_unlock(&ctx->lock);
  2083. }
  2084. static inline u64 perf_event_count(struct perf_event *event)
  2085. {
  2086. return local64_read(&event->count) + atomic64_read(&event->child_count);
  2087. }
  2088. static u64 perf_event_read(struct perf_event *event)
  2089. {
  2090. /*
  2091. * If event is enabled and currently active on a CPU, update the
  2092. * value in the event structure:
  2093. */
  2094. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  2095. smp_call_function_single(event->oncpu,
  2096. __perf_event_read, event, 1);
  2097. } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
  2098. struct perf_event_context *ctx = event->ctx;
  2099. unsigned long flags;
  2100. raw_spin_lock_irqsave(&ctx->lock, flags);
  2101. /*
  2102. * may read while context is not active
  2103. * (e.g., thread is blocked), in that case
  2104. * we cannot update context time
  2105. */
  2106. if (ctx->is_active) {
  2107. update_context_time(ctx);
  2108. update_cgrp_time_from_event(event);
  2109. }
  2110. update_event_times(event);
  2111. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  2112. }
  2113. return perf_event_count(event);
  2114. }
  2115. /*
  2116. * Callchain support
  2117. */
  2118. struct callchain_cpus_entries {
  2119. struct rcu_head rcu_head;
  2120. struct perf_callchain_entry *cpu_entries[0];
  2121. };
  2122. static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
  2123. static atomic_t nr_callchain_events;
  2124. static DEFINE_MUTEX(callchain_mutex);
  2125. struct callchain_cpus_entries *callchain_cpus_entries;
  2126. __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
  2127. struct pt_regs *regs)
  2128. {
  2129. }
  2130. __weak void perf_callchain_user(struct perf_callchain_entry *entry,
  2131. struct pt_regs *regs)
  2132. {
  2133. }
  2134. static void release_callchain_buffers_rcu(struct rcu_head *head)
  2135. {
  2136. struct callchain_cpus_entries *entries;
  2137. int cpu;
  2138. entries = container_of(head, struct callchain_cpus_entries, rcu_head);
  2139. for_each_possible_cpu(cpu)
  2140. kfree(entries->cpu_entries[cpu]);
  2141. kfree(entries);
  2142. }
  2143. static void release_callchain_buffers(void)
  2144. {
  2145. struct callchain_cpus_entries *entries;
  2146. entries = callchain_cpus_entries;
  2147. rcu_assign_pointer(callchain_cpus_entries, NULL);
  2148. call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
  2149. }
  2150. static int alloc_callchain_buffers(void)
  2151. {
  2152. int cpu;
  2153. int size;
  2154. struct callchain_cpus_entries *entries;
  2155. /*
  2156. * We can't use the percpu allocation API for data that can be
  2157. * accessed from NMI. Use a temporary manual per cpu allocation
  2158. * until that gets sorted out.
  2159. */
  2160. size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
  2161. entries = kzalloc(size, GFP_KERNEL);
  2162. if (!entries)
  2163. return -ENOMEM;
  2164. size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
  2165. for_each_possible_cpu(cpu) {
  2166. entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
  2167. cpu_to_node(cpu));
  2168. if (!entries->cpu_entries[cpu])
  2169. goto fail;
  2170. }
  2171. rcu_assign_pointer(callchain_cpus_entries, entries);
  2172. return 0;
  2173. fail:
  2174. for_each_possible_cpu(cpu)
  2175. kfree(entries->cpu_entries[cpu]);
  2176. kfree(entries);
  2177. return -ENOMEM;
  2178. }
  2179. static int get_callchain_buffers(void)
  2180. {
  2181. int err = 0;
  2182. int count;
  2183. mutex_lock(&callchain_mutex);
  2184. count = atomic_inc_return(&nr_callchain_events);
  2185. if (WARN_ON_ONCE(count < 1)) {
  2186. err = -EINVAL;
  2187. goto exit;
  2188. }
  2189. if (count > 1) {
  2190. /* If the allocation failed, give up */
  2191. if (!callchain_cpus_entries)
  2192. err = -ENOMEM;
  2193. goto exit;
  2194. }
  2195. err = alloc_callchain_buffers();
  2196. if (err)
  2197. release_callchain_buffers();
  2198. exit:
  2199. mutex_unlock(&callchain_mutex);
  2200. return err;
  2201. }
  2202. static void put_callchain_buffers(void)
  2203. {
  2204. if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
  2205. release_callchain_buffers();
  2206. mutex_unlock(&callchain_mutex);
  2207. }
  2208. }
  2209. static int get_recursion_context(int *recursion)
  2210. {
  2211. int rctx;
  2212. if (in_nmi())
  2213. rctx = 3;
  2214. else if (in_irq())
  2215. rctx = 2;
  2216. else if (in_softirq())
  2217. rctx = 1;
  2218. else
  2219. rctx = 0;
  2220. if (recursion[rctx])
  2221. return -1;
  2222. recursion[rctx]++;
  2223. barrier();
  2224. return rctx;
  2225. }
  2226. static inline void put_recursion_context(int *recursion, int rctx)
  2227. {
  2228. barrier();
  2229. recursion[rctx]--;
  2230. }
  2231. static struct perf_callchain_entry *get_callchain_entry(int *rctx)
  2232. {
  2233. int cpu;
  2234. struct callchain_cpus_entries *entries;
  2235. *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
  2236. if (*rctx == -1)
  2237. return NULL;
  2238. entries = rcu_dereference(callchain_cpus_entries);
  2239. if (!entries)
  2240. return NULL;
  2241. cpu = smp_processor_id();
  2242. return &entries->cpu_entries[cpu][*rctx];
  2243. }
  2244. static void
  2245. put_callchain_entry(int rctx)
  2246. {
  2247. put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
  2248. }
  2249. static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  2250. {
  2251. int rctx;
  2252. struct perf_callchain_entry *entry;
  2253. entry = get_callchain_entry(&rctx);
  2254. if (rctx == -1)
  2255. return NULL;
  2256. if (!entry)
  2257. goto exit_put;
  2258. entry->nr = 0;
  2259. if (!user_mode(regs)) {
  2260. perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
  2261. perf_callchain_kernel(entry, regs);
  2262. if (current->mm)
  2263. regs = task_pt_regs(current);
  2264. else
  2265. regs = NULL;
  2266. }
  2267. if (regs) {
  2268. perf_callchain_store(entry, PERF_CONTEXT_USER);
  2269. perf_callchain_user(entry, regs);
  2270. }
  2271. exit_put:
  2272. put_callchain_entry(rctx);
  2273. return entry;
  2274. }
  2275. /*
  2276. * Initialize the perf_event context in a task_struct:
  2277. */
  2278. static void __perf_event_init_context(struct perf_event_context *ctx)
  2279. {
  2280. raw_spin_lock_init(&ctx->lock);
  2281. mutex_init(&ctx->mutex);
  2282. INIT_LIST_HEAD(&ctx->pinned_groups);
  2283. INIT_LIST_HEAD(&ctx->flexible_groups);
  2284. INIT_LIST_HEAD(&ctx->event_list);
  2285. atomic_set(&ctx->refcount, 1);
  2286. }
  2287. static struct perf_event_context *
  2288. alloc_perf_context(struct pmu *pmu, struct task_struct *task)
  2289. {
  2290. struct perf_event_context *ctx;
  2291. ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  2292. if (!ctx)
  2293. return NULL;
  2294. __perf_event_init_context(ctx);
  2295. if (task) {
  2296. ctx->task = task;
  2297. get_task_struct(task);
  2298. }
  2299. ctx->pmu = pmu;
  2300. return ctx;
  2301. }
  2302. static struct task_struct *
  2303. find_lively_task_by_vpid(pid_t vpid)
  2304. {
  2305. struct task_struct *task;
  2306. int err;
  2307. rcu_read_lock();
  2308. if (!vpid)
  2309. task = current;
  2310. else
  2311. task = find_task_by_vpid(vpid);
  2312. if (task)
  2313. get_task_struct(task);
  2314. rcu_read_unlock();
  2315. if (!task)
  2316. return ERR_PTR(-ESRCH);
  2317. /* Reuse ptrace permission checks for now. */
  2318. err = -EACCES;
  2319. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  2320. goto errout;
  2321. return task;
  2322. errout:
  2323. put_task_struct(task);
  2324. return ERR_PTR(err);
  2325. }
  2326. /*
  2327. * Returns a matching context with refcount and pincount.
  2328. */
  2329. static struct perf_event_context *
  2330. find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
  2331. {
  2332. struct perf_event_context *ctx;
  2333. struct perf_cpu_context *cpuctx;
  2334. unsigned long flags;
  2335. int ctxn, err;
  2336. if (!task) {
  2337. /* Must be root to operate on a CPU event: */
  2338. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  2339. return ERR_PTR(-EACCES);
  2340. /*
  2341. * We could be clever and allow to attach a event to an
  2342. * offline CPU and activate it when the CPU comes up, but
  2343. * that's for later.
  2344. */
  2345. if (!cpu_online(cpu))
  2346. return ERR_PTR(-ENODEV);
  2347. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  2348. ctx = &cpuctx->ctx;
  2349. get_ctx(ctx);
  2350. ++ctx->pin_count;
  2351. return ctx;
  2352. }
  2353. err = -EINVAL;
  2354. ctxn = pmu->task_ctx_nr;
  2355. if (ctxn < 0)
  2356. goto errout;
  2357. retry:
  2358. ctx = perf_lock_task_context(task, ctxn, &flags);
  2359. if (ctx) {
  2360. unclone_ctx(ctx);
  2361. ++ctx->pin_count;
  2362. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  2363. } else {
  2364. ctx = alloc_perf_context(pmu, task);
  2365. err = -ENOMEM;
  2366. if (!ctx)
  2367. goto errout;
  2368. err = 0;
  2369. mutex_lock(&task->perf_event_mutex);
  2370. /*
  2371. * If it has already passed perf_event_exit_task().
  2372. * we must see PF_EXITING, it takes this mutex too.
  2373. */
  2374. if (task->flags & PF_EXITING)
  2375. err = -ESRCH;
  2376. else if (task->perf_event_ctxp[ctxn])
  2377. err = -EAGAIN;
  2378. else {
  2379. get_ctx(ctx);
  2380. ++ctx->pin_count;
  2381. rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
  2382. }
  2383. mutex_unlock(&task->perf_event_mutex);
  2384. if (unlikely(err)) {
  2385. put_ctx(ctx);
  2386. if (err == -EAGAIN)
  2387. goto retry;
  2388. goto errout;
  2389. }
  2390. }
  2391. return ctx;
  2392. errout:
  2393. return ERR_PTR(err);
  2394. }
  2395. static void perf_event_free_filter(struct perf_event *event);
  2396. static void free_event_rcu(struct rcu_head *head)
  2397. {
  2398. struct perf_event *event;
  2399. event = container_of(head, struct perf_event, rcu_head);
  2400. if (event->ns)
  2401. put_pid_ns(event->ns);
  2402. perf_event_free_filter(event);
  2403. kfree(event);
  2404. }
  2405. static void ring_buffer_put(struct ring_buffer *rb);
  2406. static void free_event(struct perf_event *event)
  2407. {
  2408. irq_work_sync(&event->pending);
  2409. if (!event->parent) {
  2410. if (event->attach_state & PERF_ATTACH_TASK)
  2411. jump_label_dec(&perf_sched_events);
  2412. if (event->attr.mmap || event->attr.mmap_data)
  2413. atomic_dec(&nr_mmap_events);
  2414. if (event->attr.comm)
  2415. atomic_dec(&nr_comm_events);
  2416. if (event->attr.task)
  2417. atomic_dec(&nr_task_events);
  2418. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
  2419. put_callchain_buffers();
  2420. if (is_cgroup_event(event)) {
  2421. atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
  2422. jump_label_dec(&perf_sched_events);
  2423. }
  2424. }
  2425. if (event->rb) {
  2426. ring_buffer_put(event->rb);
  2427. event->rb = NULL;
  2428. }
  2429. if (is_cgroup_event(event))
  2430. perf_detach_cgroup(event);
  2431. if (event->destroy)
  2432. event->destroy(event);
  2433. if (event->ctx)
  2434. put_ctx(event->ctx);
  2435. call_rcu(&event->rcu_head, free_event_rcu);
  2436. }
  2437. int perf_event_release_kernel(struct perf_event *event)
  2438. {
  2439. struct perf_event_context *ctx = event->ctx;
  2440. WARN_ON_ONCE(ctx->parent_ctx);
  2441. /*
  2442. * There are two ways this annotation is useful:
  2443. *
  2444. * 1) there is a lock recursion from perf_event_exit_task
  2445. * see the comment there.
  2446. *
  2447. * 2) there is a lock-inversion with mmap_sem through
  2448. * perf_event_read_group(), which takes faults while
  2449. * holding ctx->mutex, however this is called after
  2450. * the last filedesc died, so there is no possibility
  2451. * to trigger the AB-BA case.
  2452. */
  2453. mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
  2454. raw_spin_lock_irq(&ctx->lock);
  2455. perf_group_detach(event);
  2456. raw_spin_unlock_irq(&ctx->lock);
  2457. perf_remove_from_context(event);
  2458. mutex_unlock(&ctx->mutex);
  2459. free_event(event);
  2460. return 0;
  2461. }
  2462. EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  2463. /*
  2464. * Called when the last reference to the file is gone.
  2465. */
  2466. static int perf_release(struct inode *inode, struct file *file)
  2467. {
  2468. struct perf_event *event = file->private_data;
  2469. struct task_struct *owner;
  2470. file->private_data = NULL;
  2471. rcu_read_lock();
  2472. owner = ACCESS_ONCE(event->owner);
  2473. /*
  2474. * Matches the smp_wmb() in perf_event_exit_task(). If we observe
  2475. * !owner it means the list deletion is complete and we can indeed
  2476. * free this event, otherwise we need to serialize on
  2477. * owner->perf_event_mutex.
  2478. */
  2479. smp_read_barrier_depends();
  2480. if (owner) {
  2481. /*
  2482. * Since delayed_put_task_struct() also drops the last
  2483. * task reference we can safely take a new reference
  2484. * while holding the rcu_read_lock().
  2485. */
  2486. get_task_struct(owner);
  2487. }
  2488. rcu_read_unlock();
  2489. if (owner) {
  2490. mutex_lock(&owner->perf_event_mutex);
  2491. /*
  2492. * We have to re-check the event->owner field, if it is cleared
  2493. * we raced with perf_event_exit_task(), acquiring the mutex
  2494. * ensured they're done, and we can proceed with freeing the
  2495. * event.
  2496. */
  2497. if (event->owner)
  2498. list_del_init(&event->owner_entry);
  2499. mutex_unlock(&owner->perf_event_mutex);
  2500. put_task_struct(owner);
  2501. }
  2502. return perf_event_release_kernel(event);
  2503. }
  2504. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  2505. {
  2506. struct perf_event *child;
  2507. u64 total = 0;
  2508. *enabled = 0;
  2509. *running = 0;
  2510. mutex_lock(&event->child_mutex);
  2511. total += perf_event_read(event);
  2512. *enabled += event->total_time_enabled +
  2513. atomic64_read(&event->child_total_time_enabled);
  2514. *running += event->total_time_running +
  2515. atomic64_read(&event->child_total_time_running);
  2516. list_for_each_entry(child, &event->child_list, child_list) {
  2517. total += perf_event_read(child);
  2518. *enabled += child->total_time_enabled;
  2519. *running += child->total_time_running;
  2520. }
  2521. mutex_unlock(&event->child_mutex);
  2522. return total;
  2523. }
  2524. EXPORT_SYMBOL_GPL(perf_event_read_value);
  2525. static int perf_event_read_group(struct perf_event *event,
  2526. u64 read_format, char __user *buf)
  2527. {
  2528. struct perf_event *leader = event->group_leader, *sub;
  2529. int n = 0, size = 0, ret = -EFAULT;
  2530. struct perf_event_context *ctx = leader->ctx;
  2531. u64 values[5];
  2532. u64 count, enabled, running;
  2533. mutex_lock(&ctx->mutex);
  2534. count = perf_event_read_value(leader, &enabled, &running);
  2535. values[n++] = 1 + leader->nr_siblings;
  2536. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2537. values[n++] = enabled;
  2538. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2539. values[n++] = running;
  2540. values[n++] = count;
  2541. if (read_format & PERF_FORMAT_ID)
  2542. values[n++] = primary_event_id(leader);
  2543. size = n * sizeof(u64);
  2544. if (copy_to_user(buf, values, size))
  2545. goto unlock;
  2546. ret = size;
  2547. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2548. n = 0;
  2549. values[n++] = perf_event_read_value(sub, &enabled, &running);
  2550. if (read_format & PERF_FORMAT_ID)
  2551. values[n++] = primary_event_id(sub);
  2552. size = n * sizeof(u64);
  2553. if (copy_to_user(buf + ret, values, size)) {
  2554. ret = -EFAULT;
  2555. goto unlock;
  2556. }
  2557. ret += size;
  2558. }
  2559. unlock:
  2560. mutex_unlock(&ctx->mutex);
  2561. return ret;
  2562. }
  2563. static int perf_event_read_one(struct perf_event *event,
  2564. u64 read_format, char __user *buf)
  2565. {
  2566. u64 enabled, running;
  2567. u64 values[4];
  2568. int n = 0;
  2569. values[n++] = perf_event_read_value(event, &enabled, &running);
  2570. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2571. values[n++] = enabled;
  2572. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2573. values[n++] = running;
  2574. if (read_format & PERF_FORMAT_ID)
  2575. values[n++] = primary_event_id(event);
  2576. if (copy_to_user(buf, values, n * sizeof(u64)))
  2577. return -EFAULT;
  2578. return n * sizeof(u64);
  2579. }
  2580. /*
  2581. * Read the performance event - simple non blocking version for now
  2582. */
  2583. static ssize_t
  2584. perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
  2585. {
  2586. u64 read_format = event->attr.read_format;
  2587. int ret;
  2588. /*
  2589. * Return end-of-file for a read on a event that is in
  2590. * error state (i.e. because it was pinned but it couldn't be
  2591. * scheduled on to the CPU at some point).
  2592. */
  2593. if (event->state == PERF_EVENT_STATE_ERROR)
  2594. return 0;
  2595. if (count < event->read_size)
  2596. return -ENOSPC;
  2597. WARN_ON_ONCE(event->ctx->parent_ctx);
  2598. if (read_format & PERF_FORMAT_GROUP)
  2599. ret = perf_event_read_group(event, read_format, buf);
  2600. else
  2601. ret = perf_event_read_one(event, read_format, buf);
  2602. return ret;
  2603. }
  2604. static ssize_t
  2605. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  2606. {
  2607. struct perf_event *event = file->private_data;
  2608. return perf_read_hw(event, buf, count);
  2609. }
  2610. static unsigned int perf_poll(struct file *file, poll_table *wait)
  2611. {
  2612. struct perf_event *event = file->private_data;
  2613. struct ring_buffer *rb;
  2614. unsigned int events = POLL_HUP;
  2615. rcu_read_lock();
  2616. rb = rcu_dereference(event->rb);
  2617. if (rb)
  2618. events = atomic_xchg(&rb->poll, 0);
  2619. rcu_read_unlock();
  2620. poll_wait(file, &event->waitq, wait);
  2621. return events;
  2622. }
  2623. static void perf_event_reset(struct perf_event *event)
  2624. {
  2625. (void)perf_event_read(event);
  2626. local64_set(&event->count, 0);
  2627. perf_event_update_userpage(event);
  2628. }
  2629. /*
  2630. * Holding the top-level event's child_mutex means that any
  2631. * descendant process that has inherited this event will block
  2632. * in sync_child_event if it goes to exit, thus satisfying the
  2633. * task existence requirements of perf_event_enable/disable.
  2634. */
  2635. static void perf_event_for_each_child(struct perf_event *event,
  2636. void (*func)(struct perf_event *))
  2637. {
  2638. struct perf_event *child;
  2639. WARN_ON_ONCE(event->ctx->parent_ctx);
  2640. mutex_lock(&event->child_mutex);
  2641. func(event);
  2642. list_for_each_entry(child, &event->child_list, child_list)
  2643. func(child);
  2644. mutex_unlock(&event->child_mutex);
  2645. }
  2646. static void perf_event_for_each(struct perf_event *event,
  2647. void (*func)(struct perf_event *))
  2648. {
  2649. struct perf_event_context *ctx = event->ctx;
  2650. struct perf_event *sibling;
  2651. WARN_ON_ONCE(ctx->parent_ctx);
  2652. mutex_lock(&ctx->mutex);
  2653. event = event->group_leader;
  2654. perf_event_for_each_child(event, func);
  2655. func(event);
  2656. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  2657. perf_event_for_each_child(event, func);
  2658. mutex_unlock(&ctx->mutex);
  2659. }
  2660. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  2661. {
  2662. struct perf_event_context *ctx = event->ctx;
  2663. int ret = 0;
  2664. u64 value;
  2665. if (!is_sampling_event(event))
  2666. return -EINVAL;
  2667. if (copy_from_user(&value, arg, sizeof(value)))
  2668. return -EFAULT;
  2669. if (!value)
  2670. return -EINVAL;
  2671. raw_spin_lock_irq(&ctx->lock);
  2672. if (event->attr.freq) {
  2673. if (value > sysctl_perf_event_sample_rate) {
  2674. ret = -EINVAL;
  2675. goto unlock;
  2676. }
  2677. event->attr.sample_freq = value;
  2678. } else {
  2679. event->attr.sample_period = value;
  2680. event->hw.sample_period = value;
  2681. }
  2682. unlock:
  2683. raw_spin_unlock_irq(&ctx->lock);
  2684. return ret;
  2685. }
  2686. static const struct file_operations perf_fops;
  2687. static struct perf_event *perf_fget_light(int fd, int *fput_needed)
  2688. {
  2689. struct file *file;
  2690. file = fget_light(fd, fput_needed);
  2691. if (!file)
  2692. return ERR_PTR(-EBADF);
  2693. if (file->f_op != &perf_fops) {
  2694. fput_light(file, *fput_needed);
  2695. *fput_needed = 0;
  2696. return ERR_PTR(-EBADF);
  2697. }
  2698. return file->private_data;
  2699. }
  2700. static int perf_event_set_output(struct perf_event *event,
  2701. struct perf_event *output_event);
  2702. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  2703. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2704. {
  2705. struct perf_event *event = file->private_data;
  2706. void (*func)(struct perf_event *);
  2707. u32 flags = arg;
  2708. switch (cmd) {
  2709. case PERF_EVENT_IOC_ENABLE:
  2710. func = perf_event_enable;
  2711. break;
  2712. case PERF_EVENT_IOC_DISABLE:
  2713. func = perf_event_disable;
  2714. break;
  2715. case PERF_EVENT_IOC_RESET:
  2716. func = perf_event_reset;
  2717. break;
  2718. case PERF_EVENT_IOC_REFRESH:
  2719. return perf_event_refresh(event, arg);
  2720. case PERF_EVENT_IOC_PERIOD:
  2721. return perf_event_period(event, (u64 __user *)arg);
  2722. case PERF_EVENT_IOC_SET_OUTPUT:
  2723. {
  2724. struct perf_event *output_event = NULL;
  2725. int fput_needed = 0;
  2726. int ret;
  2727. if (arg != -1) {
  2728. output_event = perf_fget_light(arg, &fput_needed);
  2729. if (IS_ERR(output_event))
  2730. return PTR_ERR(output_event);
  2731. }
  2732. ret = perf_event_set_output(event, output_event);
  2733. if (output_event)
  2734. fput_light(output_event->filp, fput_needed);
  2735. return ret;
  2736. }
  2737. case PERF_EVENT_IOC_SET_FILTER:
  2738. return perf_event_set_filter(event, (void __user *)arg);
  2739. default:
  2740. return -ENOTTY;
  2741. }
  2742. if (flags & PERF_IOC_FLAG_GROUP)
  2743. perf_event_for_each(event, func);
  2744. else
  2745. perf_event_for_each_child(event, func);
  2746. return 0;
  2747. }
  2748. int perf_event_task_enable(void)
  2749. {
  2750. struct perf_event *event;
  2751. mutex_lock(&current->perf_event_mutex);
  2752. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  2753. perf_event_for_each_child(event, perf_event_enable);
  2754. mutex_unlock(&current->perf_event_mutex);
  2755. return 0;
  2756. }
  2757. int perf_event_task_disable(void)
  2758. {
  2759. struct perf_event *event;
  2760. mutex_lock(&current->perf_event_mutex);
  2761. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  2762. perf_event_for_each_child(event, perf_event_disable);
  2763. mutex_unlock(&current->perf_event_mutex);
  2764. return 0;
  2765. }
  2766. #ifndef PERF_EVENT_INDEX_OFFSET
  2767. # define PERF_EVENT_INDEX_OFFSET 0
  2768. #endif
  2769. static int perf_event_index(struct perf_event *event)
  2770. {
  2771. if (event->hw.state & PERF_HES_STOPPED)
  2772. return 0;
  2773. if (event->state != PERF_EVENT_STATE_ACTIVE)
  2774. return 0;
  2775. return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
  2776. }
  2777. static void calc_timer_values(struct perf_event *event,
  2778. u64 *running,
  2779. u64 *enabled)
  2780. {
  2781. u64 now, ctx_time;
  2782. now = perf_clock();
  2783. ctx_time = event->shadow_ctx_time + now;
  2784. *enabled = ctx_time - event->tstamp_enabled;
  2785. *running = ctx_time - event->tstamp_running;
  2786. }
  2787. /*
  2788. * Callers need to ensure there can be no nesting of this function, otherwise
  2789. * the seqlock logic goes bad. We can not serialize this because the arch
  2790. * code calls this from NMI context.
  2791. */
  2792. void perf_event_update_userpage(struct perf_event *event)
  2793. {
  2794. struct perf_event_mmap_page *userpg;
  2795. struct ring_buffer *rb;
  2796. u64 enabled, running;
  2797. rcu_read_lock();
  2798. /*
  2799. * compute total_time_enabled, total_time_running
  2800. * based on snapshot values taken when the event
  2801. * was last scheduled in.
  2802. *
  2803. * we cannot simply called update_context_time()
  2804. * because of locking issue as we can be called in
  2805. * NMI context
  2806. */
  2807. calc_timer_values(event, &enabled, &running);
  2808. rb = rcu_dereference(event->rb);
  2809. if (!rb)
  2810. goto unlock;
  2811. userpg = rb->user_page;
  2812. /*
  2813. * Disable preemption so as to not let the corresponding user-space
  2814. * spin too long if we get preempted.
  2815. */
  2816. preempt_disable();
  2817. ++userpg->lock;
  2818. barrier();
  2819. userpg->index = perf_event_index(event);
  2820. userpg->offset = perf_event_count(event);
  2821. if (event->state == PERF_EVENT_STATE_ACTIVE)
  2822. userpg->offset -= local64_read(&event->hw.prev_count);
  2823. userpg->time_enabled = enabled +
  2824. atomic64_read(&event->child_total_time_enabled);
  2825. userpg->time_running = running +
  2826. atomic64_read(&event->child_total_time_running);
  2827. barrier();
  2828. ++userpg->lock;
  2829. preempt_enable();
  2830. unlock:
  2831. rcu_read_unlock();
  2832. }
  2833. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  2834. {
  2835. struct perf_event *event = vma->vm_file->private_data;
  2836. struct ring_buffer *rb;
  2837. int ret = VM_FAULT_SIGBUS;
  2838. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  2839. if (vmf->pgoff == 0)
  2840. ret = 0;
  2841. return ret;
  2842. }
  2843. rcu_read_lock();
  2844. rb = rcu_dereference(event->rb);
  2845. if (!rb)
  2846. goto unlock;
  2847. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  2848. goto unlock;
  2849. vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
  2850. if (!vmf->page)
  2851. goto unlock;
  2852. get_page(vmf->page);
  2853. vmf->page->mapping = vma->vm_file->f_mapping;
  2854. vmf->page->index = vmf->pgoff;
  2855. ret = 0;
  2856. unlock:
  2857. rcu_read_unlock();
  2858. return ret;
  2859. }
  2860. static void rb_free_rcu(struct rcu_head *rcu_head)
  2861. {
  2862. struct ring_buffer *rb;
  2863. rb = container_of(rcu_head, struct ring_buffer, rcu_head);
  2864. rb_free(rb);
  2865. }
  2866. static struct ring_buffer *ring_buffer_get(struct perf_event *event)
  2867. {
  2868. struct ring_buffer *rb;
  2869. rcu_read_lock();
  2870. rb = rcu_dereference(event->rb);
  2871. if (rb) {
  2872. if (!atomic_inc_not_zero(&rb->refcount))
  2873. rb = NULL;
  2874. }
  2875. rcu_read_unlock();
  2876. return rb;
  2877. }
  2878. static void ring_buffer_put(struct ring_buffer *rb)
  2879. {
  2880. if (!atomic_dec_and_test(&rb->refcount))
  2881. return;
  2882. call_rcu(&rb->rcu_head, rb_free_rcu);
  2883. }
  2884. static void perf_mmap_open(struct vm_area_struct *vma)
  2885. {
  2886. struct perf_event *event = vma->vm_file->private_data;
  2887. atomic_inc(&event->mmap_count);
  2888. }
  2889. static void perf_mmap_close(struct vm_area_struct *vma)
  2890. {
  2891. struct perf_event *event = vma->vm_file->private_data;
  2892. if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
  2893. unsigned long size = perf_data_size(event->rb);
  2894. struct user_struct *user = event->mmap_user;
  2895. struct ring_buffer *rb = event->rb;
  2896. atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
  2897. vma->vm_mm->locked_vm -= event->mmap_locked;
  2898. rcu_assign_pointer(event->rb, NULL);
  2899. mutex_unlock(&event->mmap_mutex);
  2900. ring_buffer_put(rb);
  2901. free_uid(user);
  2902. }
  2903. }
  2904. static const struct vm_operations_struct perf_mmap_vmops = {
  2905. .open = perf_mmap_open,
  2906. .close = perf_mmap_close,
  2907. .fault = perf_mmap_fault,
  2908. .page_mkwrite = perf_mmap_fault,
  2909. };
  2910. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  2911. {
  2912. struct perf_event *event = file->private_data;
  2913. unsigned long user_locked, user_lock_limit;
  2914. struct user_struct *user = current_user();
  2915. unsigned long locked, lock_limit;
  2916. struct ring_buffer *rb;
  2917. unsigned long vma_size;
  2918. unsigned long nr_pages;
  2919. long user_extra, extra;
  2920. int ret = 0, flags = 0;
  2921. /*
  2922. * Don't allow mmap() of inherited per-task counters. This would
  2923. * create a performance issue due to all children writing to the
  2924. * same rb.
  2925. */
  2926. if (event->cpu == -1 && event->attr.inherit)
  2927. return -EINVAL;
  2928. if (!(vma->vm_flags & VM_SHARED))
  2929. return -EINVAL;
  2930. vma_size = vma->vm_end - vma->vm_start;
  2931. nr_pages = (vma_size / PAGE_SIZE) - 1;
  2932. /*
  2933. * If we have rb pages ensure they're a power-of-two number, so we
  2934. * can do bitmasks instead of modulo.
  2935. */
  2936. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  2937. return -EINVAL;
  2938. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  2939. return -EINVAL;
  2940. if (vma->vm_pgoff != 0)
  2941. return -EINVAL;
  2942. WARN_ON_ONCE(event->ctx->parent_ctx);
  2943. mutex_lock(&event->mmap_mutex);
  2944. if (event->rb) {
  2945. if (event->rb->nr_pages == nr_pages)
  2946. atomic_inc(&event->rb->refcount);
  2947. else
  2948. ret = -EINVAL;
  2949. goto unlock;
  2950. }
  2951. user_extra = nr_pages + 1;
  2952. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  2953. /*
  2954. * Increase the limit linearly with more CPUs:
  2955. */
  2956. user_lock_limit *= num_online_cpus();
  2957. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  2958. extra = 0;
  2959. if (user_locked > user_lock_limit)
  2960. extra = user_locked - user_lock_limit;
  2961. lock_limit = rlimit(RLIMIT_MEMLOCK);
  2962. lock_limit >>= PAGE_SHIFT;
  2963. locked = vma->vm_mm->locked_vm + extra;
  2964. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  2965. !capable(CAP_IPC_LOCK)) {
  2966. ret = -EPERM;
  2967. goto unlock;
  2968. }
  2969. WARN_ON(event->rb);
  2970. if (vma->vm_flags & VM_WRITE)
  2971. flags |= RING_BUFFER_WRITABLE;
  2972. rb = rb_alloc(nr_pages,
  2973. event->attr.watermark ? event->attr.wakeup_watermark : 0,
  2974. event->cpu, flags);
  2975. if (!rb) {
  2976. ret = -ENOMEM;
  2977. goto unlock;
  2978. }
  2979. rcu_assign_pointer(event->rb, rb);
  2980. atomic_long_add(user_extra, &user->locked_vm);
  2981. event->mmap_locked = extra;
  2982. event->mmap_user = get_current_user();
  2983. vma->vm_mm->locked_vm += event->mmap_locked;
  2984. unlock:
  2985. if (!ret)
  2986. atomic_inc(&event->mmap_count);
  2987. mutex_unlock(&event->mmap_mutex);
  2988. vma->vm_flags |= VM_RESERVED;
  2989. vma->vm_ops = &perf_mmap_vmops;
  2990. return ret;
  2991. }
  2992. static int perf_fasync(int fd, struct file *filp, int on)
  2993. {
  2994. struct inode *inode = filp->f_path.dentry->d_inode;
  2995. struct perf_event *event = filp->private_data;
  2996. int retval;
  2997. mutex_lock(&inode->i_mutex);
  2998. retval = fasync_helper(fd, filp, on, &event->fasync);
  2999. mutex_unlock(&inode->i_mutex);
  3000. if (retval < 0)
  3001. return retval;
  3002. return 0;
  3003. }
  3004. static const struct file_operations perf_fops = {
  3005. .llseek = no_llseek,
  3006. .release = perf_release,
  3007. .read = perf_read,
  3008. .poll = perf_poll,
  3009. .unlocked_ioctl = perf_ioctl,
  3010. .compat_ioctl = perf_ioctl,
  3011. .mmap = perf_mmap,
  3012. .fasync = perf_fasync,
  3013. };
  3014. /*
  3015. * Perf event wakeup
  3016. *
  3017. * If there's data, ensure we set the poll() state and publish everything
  3018. * to user-space before waking everybody up.
  3019. */
  3020. void perf_event_wakeup(struct perf_event *event)
  3021. {
  3022. wake_up_all(&event->waitq);
  3023. if (event->pending_kill) {
  3024. kill_fasync(&event->fasync, SIGIO, event->pending_kill);
  3025. event->pending_kill = 0;
  3026. }
  3027. }
  3028. static void perf_pending_event(struct irq_work *entry)
  3029. {
  3030. struct perf_event *event = container_of(entry,
  3031. struct perf_event, pending);
  3032. if (event->pending_disable) {
  3033. event->pending_disable = 0;
  3034. __perf_event_disable(event);
  3035. }
  3036. if (event->pending_wakeup) {
  3037. event->pending_wakeup = 0;
  3038. perf_event_wakeup(event);
  3039. }
  3040. }
  3041. /*
  3042. * We assume there is only KVM supporting the callbacks.
  3043. * Later on, we might change it to a list if there is
  3044. * another virtualization implementation supporting the callbacks.
  3045. */
  3046. struct perf_guest_info_callbacks *perf_guest_cbs;
  3047. int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  3048. {
  3049. perf_guest_cbs = cbs;
  3050. return 0;
  3051. }
  3052. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  3053. int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  3054. {
  3055. perf_guest_cbs = NULL;
  3056. return 0;
  3057. }
  3058. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  3059. static void __perf_event_header__init_id(struct perf_event_header *header,
  3060. struct perf_sample_data *data,
  3061. struct perf_event *event)
  3062. {
  3063. u64 sample_type = event->attr.sample_type;
  3064. data->type = sample_type;
  3065. header->size += event->id_header_size;
  3066. if (sample_type & PERF_SAMPLE_TID) {
  3067. /* namespace issues */
  3068. data->tid_entry.pid = perf_event_pid(event, current);
  3069. data->tid_entry.tid = perf_event_tid(event, current);
  3070. }
  3071. if (sample_type & PERF_SAMPLE_TIME)
  3072. data->time = perf_clock();
  3073. if (sample_type & PERF_SAMPLE_ID)
  3074. data->id = primary_event_id(event);
  3075. if (sample_type & PERF_SAMPLE_STREAM_ID)
  3076. data->stream_id = event->id;
  3077. if (sample_type & PERF_SAMPLE_CPU) {
  3078. data->cpu_entry.cpu = raw_smp_processor_id();
  3079. data->cpu_entry.reserved = 0;
  3080. }
  3081. }
  3082. void perf_event_header__init_id(struct perf_event_header *header,
  3083. struct perf_sample_data *data,
  3084. struct perf_event *event)
  3085. {
  3086. if (event->attr.sample_id_all)
  3087. __perf_event_header__init_id(header, data, event);
  3088. }
  3089. static void __perf_event__output_id_sample(struct perf_output_handle *handle,
  3090. struct perf_sample_data *data)
  3091. {
  3092. u64 sample_type = data->type;
  3093. if (sample_type & PERF_SAMPLE_TID)
  3094. perf_output_put(handle, data->tid_entry);
  3095. if (sample_type & PERF_SAMPLE_TIME)
  3096. perf_output_put(handle, data->time);
  3097. if (sample_type & PERF_SAMPLE_ID)
  3098. perf_output_put(handle, data->id);
  3099. if (sample_type & PERF_SAMPLE_STREAM_ID)
  3100. perf_output_put(handle, data->stream_id);
  3101. if (sample_type & PERF_SAMPLE_CPU)
  3102. perf_output_put(handle, data->cpu_entry);
  3103. }
  3104. void perf_event__output_id_sample(struct perf_event *event,
  3105. struct perf_output_handle *handle,
  3106. struct perf_sample_data *sample)
  3107. {
  3108. if (event->attr.sample_id_all)
  3109. __perf_event__output_id_sample(handle, sample);
  3110. }
  3111. static void perf_output_read_one(struct perf_output_handle *handle,
  3112. struct perf_event *event,
  3113. u64 enabled, u64 running)
  3114. {
  3115. u64 read_format = event->attr.read_format;
  3116. u64 values[4];
  3117. int n = 0;
  3118. values[n++] = perf_event_count(event);
  3119. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  3120. values[n++] = enabled +
  3121. atomic64_read(&event->child_total_time_enabled);
  3122. }
  3123. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  3124. values[n++] = running +
  3125. atomic64_read(&event->child_total_time_running);
  3126. }
  3127. if (read_format & PERF_FORMAT_ID)
  3128. values[n++] = primary_event_id(event);
  3129. __output_copy(handle, values, n * sizeof(u64));
  3130. }
  3131. /*
  3132. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  3133. */
  3134. static void perf_output_read_group(struct perf_output_handle *handle,
  3135. struct perf_event *event,
  3136. u64 enabled, u64 running)
  3137. {
  3138. struct perf_event *leader = event->group_leader, *sub;
  3139. u64 read_format = event->attr.read_format;
  3140. u64 values[5];
  3141. int n = 0;
  3142. values[n++] = 1 + leader->nr_siblings;
  3143. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  3144. values[n++] = enabled;
  3145. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  3146. values[n++] = running;
  3147. if (leader != event)
  3148. leader->pmu->read(leader);
  3149. values[n++] = perf_event_count(leader);
  3150. if (read_format & PERF_FORMAT_ID)
  3151. values[n++] = primary_event_id(leader);
  3152. __output_copy(handle, values, n * sizeof(u64));
  3153. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  3154. n = 0;
  3155. if (sub != event)
  3156. sub->pmu->read(sub);
  3157. values[n++] = perf_event_count(sub);
  3158. if (read_format & PERF_FORMAT_ID)
  3159. values[n++] = primary_event_id(sub);
  3160. __output_copy(handle, values, n * sizeof(u64));
  3161. }
  3162. }
  3163. #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
  3164. PERF_FORMAT_TOTAL_TIME_RUNNING)
  3165. static void perf_output_read(struct perf_output_handle *handle,
  3166. struct perf_event *event)
  3167. {
  3168. u64 enabled = 0, running = 0;
  3169. u64 read_format = event->attr.read_format;
  3170. /*
  3171. * compute total_time_enabled, total_time_running
  3172. * based on snapshot values taken when the event
  3173. * was last scheduled in.
  3174. *
  3175. * we cannot simply called update_context_time()
  3176. * because of locking issue as we are called in
  3177. * NMI context
  3178. */
  3179. if (read_format & PERF_FORMAT_TOTAL_TIMES)
  3180. calc_timer_values(event, &enabled, &running);
  3181. if (event->attr.read_format & PERF_FORMAT_GROUP)
  3182. perf_output_read_group(handle, event, enabled, running);
  3183. else
  3184. perf_output_read_one(handle, event, enabled, running);
  3185. }
  3186. void perf_output_sample(struct perf_output_handle *handle,
  3187. struct perf_event_header *header,
  3188. struct perf_sample_data *data,
  3189. struct perf_event *event)
  3190. {
  3191. u64 sample_type = data->type;
  3192. perf_output_put(handle, *header);
  3193. if (sample_type & PERF_SAMPLE_IP)
  3194. perf_output_put(handle, data->ip);
  3195. if (sample_type & PERF_SAMPLE_TID)
  3196. perf_output_put(handle, data->tid_entry);
  3197. if (sample_type & PERF_SAMPLE_TIME)
  3198. perf_output_put(handle, data->time);
  3199. if (sample_type & PERF_SAMPLE_ADDR)
  3200. perf_output_put(handle, data->addr);
  3201. if (sample_type & PERF_SAMPLE_ID)
  3202. perf_output_put(handle, data->id);
  3203. if (sample_type & PERF_SAMPLE_STREAM_ID)
  3204. perf_output_put(handle, data->stream_id);
  3205. if (sample_type & PERF_SAMPLE_CPU)
  3206. perf_output_put(handle, data->cpu_entry);
  3207. if (sample_type & PERF_SAMPLE_PERIOD)
  3208. perf_output_put(handle, data->period);
  3209. if (sample_type & PERF_SAMPLE_READ)
  3210. perf_output_read(handle, event);
  3211. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  3212. if (data->callchain) {
  3213. int size = 1;
  3214. if (data->callchain)
  3215. size += data->callchain->nr;
  3216. size *= sizeof(u64);
  3217. __output_copy(handle, data->callchain, size);
  3218. } else {
  3219. u64 nr = 0;
  3220. perf_output_put(handle, nr);
  3221. }
  3222. }
  3223. if (sample_type & PERF_SAMPLE_RAW) {
  3224. if (data->raw) {
  3225. perf_output_put(handle, data->raw->size);
  3226. __output_copy(handle, data->raw->data,
  3227. data->raw->size);
  3228. } else {
  3229. struct {
  3230. u32 size;
  3231. u32 data;
  3232. } raw = {
  3233. .size = sizeof(u32),
  3234. .data = 0,
  3235. };
  3236. perf_output_put(handle, raw);
  3237. }
  3238. }
  3239. if (!event->attr.watermark) {
  3240. int wakeup_events = event->attr.wakeup_events;
  3241. if (wakeup_events) {
  3242. struct ring_buffer *rb = handle->rb;
  3243. int events = local_inc_return(&rb->events);
  3244. if (events >= wakeup_events) {
  3245. local_sub(wakeup_events, &rb->events);
  3246. local_inc(&rb->wakeup);
  3247. }
  3248. }
  3249. }
  3250. }
  3251. void perf_prepare_sample(struct perf_event_header *header,
  3252. struct perf_sample_data *data,
  3253. struct perf_event *event,
  3254. struct pt_regs *regs)
  3255. {
  3256. u64 sample_type = event->attr.sample_type;
  3257. header->type = PERF_RECORD_SAMPLE;
  3258. header->size = sizeof(*header) + event->header_size;
  3259. header->misc = 0;
  3260. header->misc |= perf_misc_flags(regs);
  3261. __perf_event_header__init_id(header, data, event);
  3262. if (sample_type & PERF_SAMPLE_IP)
  3263. data->ip = perf_instruction_pointer(regs);
  3264. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  3265. int size = 1;
  3266. data->callchain = perf_callchain(regs);
  3267. if (data->callchain)
  3268. size += data->callchain->nr;
  3269. header->size += size * sizeof(u64);
  3270. }
  3271. if (sample_type & PERF_SAMPLE_RAW) {
  3272. int size = sizeof(u32);
  3273. if (data->raw)
  3274. size += data->raw->size;
  3275. else
  3276. size += sizeof(u32);
  3277. WARN_ON_ONCE(size & (sizeof(u64)-1));
  3278. header->size += size;
  3279. }
  3280. }
  3281. static void perf_event_output(struct perf_event *event,
  3282. struct perf_sample_data *data,
  3283. struct pt_regs *regs)
  3284. {
  3285. struct perf_output_handle handle;
  3286. struct perf_event_header header;
  3287. /* protect the callchain buffers */
  3288. rcu_read_lock();
  3289. perf_prepare_sample(&header, data, event, regs);
  3290. if (perf_output_begin(&handle, event, header.size))
  3291. goto exit;
  3292. perf_output_sample(&handle, &header, data, event);
  3293. perf_output_end(&handle);
  3294. exit:
  3295. rcu_read_unlock();
  3296. }
  3297. /*
  3298. * read event_id
  3299. */
  3300. struct perf_read_event {
  3301. struct perf_event_header header;
  3302. u32 pid;
  3303. u32 tid;
  3304. };
  3305. static void
  3306. perf_event_read_event(struct perf_event *event,
  3307. struct task_struct *task)
  3308. {
  3309. struct perf_output_handle handle;
  3310. struct perf_sample_data sample;
  3311. struct perf_read_event read_event = {
  3312. .header = {
  3313. .type = PERF_RECORD_READ,
  3314. .misc = 0,
  3315. .size = sizeof(read_event) + event->read_size,
  3316. },
  3317. .pid = perf_event_pid(event, task),
  3318. .tid = perf_event_tid(event, task),
  3319. };
  3320. int ret;
  3321. perf_event_header__init_id(&read_event.header, &sample, event);
  3322. ret = perf_output_begin(&handle, event, read_event.header.size);
  3323. if (ret)
  3324. return;
  3325. perf_output_put(&handle, read_event);
  3326. perf_output_read(&handle, event);
  3327. perf_event__output_id_sample(event, &handle, &sample);
  3328. perf_output_end(&handle);
  3329. }
  3330. /*
  3331. * task tracking -- fork/exit
  3332. *
  3333. * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
  3334. */
  3335. struct perf_task_event {
  3336. struct task_struct *task;
  3337. struct perf_event_context *task_ctx;
  3338. struct {
  3339. struct perf_event_header header;
  3340. u32 pid;
  3341. u32 ppid;
  3342. u32 tid;
  3343. u32 ptid;
  3344. u64 time;
  3345. } event_id;
  3346. };
  3347. static void perf_event_task_output(struct perf_event *event,
  3348. struct perf_task_event *task_event)
  3349. {
  3350. struct perf_output_handle handle;
  3351. struct perf_sample_data sample;
  3352. struct task_struct *task = task_event->task;
  3353. int ret, size = task_event->event_id.header.size;
  3354. perf_event_header__init_id(&task_event->event_id.header, &sample, event);
  3355. ret = perf_output_begin(&handle, event,
  3356. task_event->event_id.header.size);
  3357. if (ret)
  3358. goto out;
  3359. task_event->event_id.pid = perf_event_pid(event, task);
  3360. task_event->event_id.ppid = perf_event_pid(event, current);
  3361. task_event->event_id.tid = perf_event_tid(event, task);
  3362. task_event->event_id.ptid = perf_event_tid(event, current);
  3363. perf_output_put(&handle, task_event->event_id);
  3364. perf_event__output_id_sample(event, &handle, &sample);
  3365. perf_output_end(&handle);
  3366. out:
  3367. task_event->event_id.header.size = size;
  3368. }
  3369. static int perf_event_task_match(struct perf_event *event)
  3370. {
  3371. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3372. return 0;
  3373. if (!event_filter_match(event))
  3374. return 0;
  3375. if (event->attr.comm || event->attr.mmap ||
  3376. event->attr.mmap_data || event->attr.task)
  3377. return 1;
  3378. return 0;
  3379. }
  3380. static void perf_event_task_ctx(struct perf_event_context *ctx,
  3381. struct perf_task_event *task_event)
  3382. {
  3383. struct perf_event *event;
  3384. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3385. if (perf_event_task_match(event))
  3386. perf_event_task_output(event, task_event);
  3387. }
  3388. }
  3389. static void perf_event_task_event(struct perf_task_event *task_event)
  3390. {
  3391. struct perf_cpu_context *cpuctx;
  3392. struct perf_event_context *ctx;
  3393. struct pmu *pmu;
  3394. int ctxn;
  3395. rcu_read_lock();
  3396. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3397. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3398. if (cpuctx->active_pmu != pmu)
  3399. goto next;
  3400. perf_event_task_ctx(&cpuctx->ctx, task_event);
  3401. ctx = task_event->task_ctx;
  3402. if (!ctx) {
  3403. ctxn = pmu->task_ctx_nr;
  3404. if (ctxn < 0)
  3405. goto next;
  3406. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3407. }
  3408. if (ctx)
  3409. perf_event_task_ctx(ctx, task_event);
  3410. next:
  3411. put_cpu_ptr(pmu->pmu_cpu_context);
  3412. }
  3413. rcu_read_unlock();
  3414. }
  3415. static void perf_event_task(struct task_struct *task,
  3416. struct perf_event_context *task_ctx,
  3417. int new)
  3418. {
  3419. struct perf_task_event task_event;
  3420. if (!atomic_read(&nr_comm_events) &&
  3421. !atomic_read(&nr_mmap_events) &&
  3422. !atomic_read(&nr_task_events))
  3423. return;
  3424. task_event = (struct perf_task_event){
  3425. .task = task,
  3426. .task_ctx = task_ctx,
  3427. .event_id = {
  3428. .header = {
  3429. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  3430. .misc = 0,
  3431. .size = sizeof(task_event.event_id),
  3432. },
  3433. /* .pid */
  3434. /* .ppid */
  3435. /* .tid */
  3436. /* .ptid */
  3437. .time = perf_clock(),
  3438. },
  3439. };
  3440. perf_event_task_event(&task_event);
  3441. }
  3442. void perf_event_fork(struct task_struct *task)
  3443. {
  3444. perf_event_task(task, NULL, 1);
  3445. }
  3446. /*
  3447. * comm tracking
  3448. */
  3449. struct perf_comm_event {
  3450. struct task_struct *task;
  3451. char *comm;
  3452. int comm_size;
  3453. struct {
  3454. struct perf_event_header header;
  3455. u32 pid;
  3456. u32 tid;
  3457. } event_id;
  3458. };
  3459. static void perf_event_comm_output(struct perf_event *event,
  3460. struct perf_comm_event *comm_event)
  3461. {
  3462. struct perf_output_handle handle;
  3463. struct perf_sample_data sample;
  3464. int size = comm_event->event_id.header.size;
  3465. int ret;
  3466. perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
  3467. ret = perf_output_begin(&handle, event,
  3468. comm_event->event_id.header.size);
  3469. if (ret)
  3470. goto out;
  3471. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  3472. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  3473. perf_output_put(&handle, comm_event->event_id);
  3474. __output_copy(&handle, comm_event->comm,
  3475. comm_event->comm_size);
  3476. perf_event__output_id_sample(event, &handle, &sample);
  3477. perf_output_end(&handle);
  3478. out:
  3479. comm_event->event_id.header.size = size;
  3480. }
  3481. static int perf_event_comm_match(struct perf_event *event)
  3482. {
  3483. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3484. return 0;
  3485. if (!event_filter_match(event))
  3486. return 0;
  3487. if (event->attr.comm)
  3488. return 1;
  3489. return 0;
  3490. }
  3491. static void perf_event_comm_ctx(struct perf_event_context *ctx,
  3492. struct perf_comm_event *comm_event)
  3493. {
  3494. struct perf_event *event;
  3495. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3496. if (perf_event_comm_match(event))
  3497. perf_event_comm_output(event, comm_event);
  3498. }
  3499. }
  3500. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  3501. {
  3502. struct perf_cpu_context *cpuctx;
  3503. struct perf_event_context *ctx;
  3504. char comm[TASK_COMM_LEN];
  3505. unsigned int size;
  3506. struct pmu *pmu;
  3507. int ctxn;
  3508. memset(comm, 0, sizeof(comm));
  3509. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  3510. size = ALIGN(strlen(comm)+1, sizeof(u64));
  3511. comm_event->comm = comm;
  3512. comm_event->comm_size = size;
  3513. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  3514. rcu_read_lock();
  3515. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3516. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3517. if (cpuctx->active_pmu != pmu)
  3518. goto next;
  3519. perf_event_comm_ctx(&cpuctx->ctx, comm_event);
  3520. ctxn = pmu->task_ctx_nr;
  3521. if (ctxn < 0)
  3522. goto next;
  3523. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3524. if (ctx)
  3525. perf_event_comm_ctx(ctx, comm_event);
  3526. next:
  3527. put_cpu_ptr(pmu->pmu_cpu_context);
  3528. }
  3529. rcu_read_unlock();
  3530. }
  3531. void perf_event_comm(struct task_struct *task)
  3532. {
  3533. struct perf_comm_event comm_event;
  3534. struct perf_event_context *ctx;
  3535. int ctxn;
  3536. for_each_task_context_nr(ctxn) {
  3537. ctx = task->perf_event_ctxp[ctxn];
  3538. if (!ctx)
  3539. continue;
  3540. perf_event_enable_on_exec(ctx);
  3541. }
  3542. if (!atomic_read(&nr_comm_events))
  3543. return;
  3544. comm_event = (struct perf_comm_event){
  3545. .task = task,
  3546. /* .comm */
  3547. /* .comm_size */
  3548. .event_id = {
  3549. .header = {
  3550. .type = PERF_RECORD_COMM,
  3551. .misc = 0,
  3552. /* .size */
  3553. },
  3554. /* .pid */
  3555. /* .tid */
  3556. },
  3557. };
  3558. perf_event_comm_event(&comm_event);
  3559. }
  3560. /*
  3561. * mmap tracking
  3562. */
  3563. struct perf_mmap_event {
  3564. struct vm_area_struct *vma;
  3565. const char *file_name;
  3566. int file_size;
  3567. struct {
  3568. struct perf_event_header header;
  3569. u32 pid;
  3570. u32 tid;
  3571. u64 start;
  3572. u64 len;
  3573. u64 pgoff;
  3574. } event_id;
  3575. };
  3576. static void perf_event_mmap_output(struct perf_event *event,
  3577. struct perf_mmap_event *mmap_event)
  3578. {
  3579. struct perf_output_handle handle;
  3580. struct perf_sample_data sample;
  3581. int size = mmap_event->event_id.header.size;
  3582. int ret;
  3583. perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
  3584. ret = perf_output_begin(&handle, event,
  3585. mmap_event->event_id.header.size);
  3586. if (ret)
  3587. goto out;
  3588. mmap_event->event_id.pid = perf_event_pid(event, current);
  3589. mmap_event->event_id.tid = perf_event_tid(event, current);
  3590. perf_output_put(&handle, mmap_event->event_id);
  3591. __output_copy(&handle, mmap_event->file_name,
  3592. mmap_event->file_size);
  3593. perf_event__output_id_sample(event, &handle, &sample);
  3594. perf_output_end(&handle);
  3595. out:
  3596. mmap_event->event_id.header.size = size;
  3597. }
  3598. static int perf_event_mmap_match(struct perf_event *event,
  3599. struct perf_mmap_event *mmap_event,
  3600. int executable)
  3601. {
  3602. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3603. return 0;
  3604. if (!event_filter_match(event))
  3605. return 0;
  3606. if ((!executable && event->attr.mmap_data) ||
  3607. (executable && event->attr.mmap))
  3608. return 1;
  3609. return 0;
  3610. }
  3611. static void perf_event_mmap_ctx(struct perf_event_context *ctx,
  3612. struct perf_mmap_event *mmap_event,
  3613. int executable)
  3614. {
  3615. struct perf_event *event;
  3616. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3617. if (perf_event_mmap_match(event, mmap_event, executable))
  3618. perf_event_mmap_output(event, mmap_event);
  3619. }
  3620. }
  3621. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  3622. {
  3623. struct perf_cpu_context *cpuctx;
  3624. struct perf_event_context *ctx;
  3625. struct vm_area_struct *vma = mmap_event->vma;
  3626. struct file *file = vma->vm_file;
  3627. unsigned int size;
  3628. char tmp[16];
  3629. char *buf = NULL;
  3630. const char *name;
  3631. struct pmu *pmu;
  3632. int ctxn;
  3633. memset(tmp, 0, sizeof(tmp));
  3634. if (file) {
  3635. /*
  3636. * d_path works from the end of the rb backwards, so we
  3637. * need to add enough zero bytes after the string to handle
  3638. * the 64bit alignment we do later.
  3639. */
  3640. buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
  3641. if (!buf) {
  3642. name = strncpy(tmp, "//enomem", sizeof(tmp));
  3643. goto got_name;
  3644. }
  3645. name = d_path(&file->f_path, buf, PATH_MAX);
  3646. if (IS_ERR(name)) {
  3647. name = strncpy(tmp, "//toolong", sizeof(tmp));
  3648. goto got_name;
  3649. }
  3650. } else {
  3651. if (arch_vma_name(mmap_event->vma)) {
  3652. name = strncpy(tmp, arch_vma_name(mmap_event->vma),
  3653. sizeof(tmp));
  3654. goto got_name;
  3655. }
  3656. if (!vma->vm_mm) {
  3657. name = strncpy(tmp, "[vdso]", sizeof(tmp));
  3658. goto got_name;
  3659. } else if (vma->vm_start <= vma->vm_mm->start_brk &&
  3660. vma->vm_end >= vma->vm_mm->brk) {
  3661. name = strncpy(tmp, "[heap]", sizeof(tmp));
  3662. goto got_name;
  3663. } else if (vma->vm_start <= vma->vm_mm->start_stack &&
  3664. vma->vm_end >= vma->vm_mm->start_stack) {
  3665. name = strncpy(tmp, "[stack]", sizeof(tmp));
  3666. goto got_name;
  3667. }
  3668. name = strncpy(tmp, "//anon", sizeof(tmp));
  3669. goto got_name;
  3670. }
  3671. got_name:
  3672. size = ALIGN(strlen(name)+1, sizeof(u64));
  3673. mmap_event->file_name = name;
  3674. mmap_event->file_size = size;
  3675. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  3676. rcu_read_lock();
  3677. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3678. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3679. if (cpuctx->active_pmu != pmu)
  3680. goto next;
  3681. perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
  3682. vma->vm_flags & VM_EXEC);
  3683. ctxn = pmu->task_ctx_nr;
  3684. if (ctxn < 0)
  3685. goto next;
  3686. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3687. if (ctx) {
  3688. perf_event_mmap_ctx(ctx, mmap_event,
  3689. vma->vm_flags & VM_EXEC);
  3690. }
  3691. next:
  3692. put_cpu_ptr(pmu->pmu_cpu_context);
  3693. }
  3694. rcu_read_unlock();
  3695. kfree(buf);
  3696. }
  3697. void perf_event_mmap(struct vm_area_struct *vma)
  3698. {
  3699. struct perf_mmap_event mmap_event;
  3700. if (!atomic_read(&nr_mmap_events))
  3701. return;
  3702. mmap_event = (struct perf_mmap_event){
  3703. .vma = vma,
  3704. /* .file_name */
  3705. /* .file_size */
  3706. .event_id = {
  3707. .header = {
  3708. .type = PERF_RECORD_MMAP,
  3709. .misc = PERF_RECORD_MISC_USER,
  3710. /* .size */
  3711. },
  3712. /* .pid */
  3713. /* .tid */
  3714. .start = vma->vm_start,
  3715. .len = vma->vm_end - vma->vm_start,
  3716. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  3717. },
  3718. };
  3719. perf_event_mmap_event(&mmap_event);
  3720. }
  3721. /*
  3722. * IRQ throttle logging
  3723. */
  3724. static void perf_log_throttle(struct perf_event *event, int enable)
  3725. {
  3726. struct perf_output_handle handle;
  3727. struct perf_sample_data sample;
  3728. int ret;
  3729. struct {
  3730. struct perf_event_header header;
  3731. u64 time;
  3732. u64 id;
  3733. u64 stream_id;
  3734. } throttle_event = {
  3735. .header = {
  3736. .type = PERF_RECORD_THROTTLE,
  3737. .misc = 0,
  3738. .size = sizeof(throttle_event),
  3739. },
  3740. .time = perf_clock(),
  3741. .id = primary_event_id(event),
  3742. .stream_id = event->id,
  3743. };
  3744. if (enable)
  3745. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  3746. perf_event_header__init_id(&throttle_event.header, &sample, event);
  3747. ret = perf_output_begin(&handle, event,
  3748. throttle_event.header.size);
  3749. if (ret)
  3750. return;
  3751. perf_output_put(&handle, throttle_event);
  3752. perf_event__output_id_sample(event, &handle, &sample);
  3753. perf_output_end(&handle);
  3754. }
  3755. /*
  3756. * Generic event overflow handling, sampling.
  3757. */
  3758. static int __perf_event_overflow(struct perf_event *event,
  3759. int throttle, struct perf_sample_data *data,
  3760. struct pt_regs *regs)
  3761. {
  3762. int events = atomic_read(&event->event_limit);
  3763. struct hw_perf_event *hwc = &event->hw;
  3764. int ret = 0;
  3765. /*
  3766. * Non-sampling counters might still use the PMI to fold short
  3767. * hardware counters, ignore those.
  3768. */
  3769. if (unlikely(!is_sampling_event(event)))
  3770. return 0;
  3771. if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
  3772. if (throttle) {
  3773. hwc->interrupts = MAX_INTERRUPTS;
  3774. perf_log_throttle(event, 0);
  3775. ret = 1;
  3776. }
  3777. } else
  3778. hwc->interrupts++;
  3779. if (event->attr.freq) {
  3780. u64 now = perf_clock();
  3781. s64 delta = now - hwc->freq_time_stamp;
  3782. hwc->freq_time_stamp = now;
  3783. if (delta > 0 && delta < 2*TICK_NSEC)
  3784. perf_adjust_period(event, delta, hwc->last_period);
  3785. }
  3786. /*
  3787. * XXX event_limit might not quite work as expected on inherited
  3788. * events
  3789. */
  3790. event->pending_kill = POLL_IN;
  3791. if (events && atomic_dec_and_test(&event->event_limit)) {
  3792. ret = 1;
  3793. event->pending_kill = POLL_HUP;
  3794. event->pending_disable = 1;
  3795. irq_work_queue(&event->pending);
  3796. }
  3797. if (event->overflow_handler)
  3798. event->overflow_handler(event, data, regs);
  3799. else
  3800. perf_event_output(event, data, regs);
  3801. if (event->fasync && event->pending_kill) {
  3802. event->pending_wakeup = 1;
  3803. irq_work_queue(&event->pending);
  3804. }
  3805. return ret;
  3806. }
  3807. int perf_event_overflow(struct perf_event *event,
  3808. struct perf_sample_data *data,
  3809. struct pt_regs *regs)
  3810. {
  3811. return __perf_event_overflow(event, 1, data, regs);
  3812. }
  3813. /*
  3814. * Generic software event infrastructure
  3815. */
  3816. struct swevent_htable {
  3817. struct swevent_hlist *swevent_hlist;
  3818. struct mutex hlist_mutex;
  3819. int hlist_refcount;
  3820. /* Recursion avoidance in each contexts */
  3821. int recursion[PERF_NR_CONTEXTS];
  3822. };
  3823. static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
  3824. /*
  3825. * We directly increment event->count and keep a second value in
  3826. * event->hw.period_left to count intervals. This period event
  3827. * is kept in the range [-sample_period, 0] so that we can use the
  3828. * sign as trigger.
  3829. */
  3830. static u64 perf_swevent_set_period(struct perf_event *event)
  3831. {
  3832. struct hw_perf_event *hwc = &event->hw;
  3833. u64 period = hwc->last_period;
  3834. u64 nr, offset;
  3835. s64 old, val;
  3836. hwc->last_period = hwc->sample_period;
  3837. again:
  3838. old = val = local64_read(&hwc->period_left);
  3839. if (val < 0)
  3840. return 0;
  3841. nr = div64_u64(period + val, period);
  3842. offset = nr * period;
  3843. val -= offset;
  3844. if (local64_cmpxchg(&hwc->period_left, old, val) != old)
  3845. goto again;
  3846. return nr;
  3847. }
  3848. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  3849. struct perf_sample_data *data,
  3850. struct pt_regs *regs)
  3851. {
  3852. struct hw_perf_event *hwc = &event->hw;
  3853. int throttle = 0;
  3854. data->period = event->hw.last_period;
  3855. if (!overflow)
  3856. overflow = perf_swevent_set_period(event);
  3857. if (hwc->interrupts == MAX_INTERRUPTS)
  3858. return;
  3859. for (; overflow; overflow--) {
  3860. if (__perf_event_overflow(event, throttle,
  3861. data, regs)) {
  3862. /*
  3863. * We inhibit the overflow from happening when
  3864. * hwc->interrupts == MAX_INTERRUPTS.
  3865. */
  3866. break;
  3867. }
  3868. throttle = 1;
  3869. }
  3870. }
  3871. static void perf_swevent_event(struct perf_event *event, u64 nr,
  3872. struct perf_sample_data *data,
  3873. struct pt_regs *regs)
  3874. {
  3875. struct hw_perf_event *hwc = &event->hw;
  3876. local64_add(nr, &event->count);
  3877. if (!regs)
  3878. return;
  3879. if (!is_sampling_event(event))
  3880. return;
  3881. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  3882. return perf_swevent_overflow(event, 1, data, regs);
  3883. if (local64_add_negative(nr, &hwc->period_left))
  3884. return;
  3885. perf_swevent_overflow(event, 0, data, regs);
  3886. }
  3887. static int perf_exclude_event(struct perf_event *event,
  3888. struct pt_regs *regs)
  3889. {
  3890. if (event->hw.state & PERF_HES_STOPPED)
  3891. return 1;
  3892. if (regs) {
  3893. if (event->attr.exclude_user && user_mode(regs))
  3894. return 1;
  3895. if (event->attr.exclude_kernel && !user_mode(regs))
  3896. return 1;
  3897. }
  3898. return 0;
  3899. }
  3900. static int perf_swevent_match(struct perf_event *event,
  3901. enum perf_type_id type,
  3902. u32 event_id,
  3903. struct perf_sample_data *data,
  3904. struct pt_regs *regs)
  3905. {
  3906. if (event->attr.type != type)
  3907. return 0;
  3908. if (event->attr.config != event_id)
  3909. return 0;
  3910. if (perf_exclude_event(event, regs))
  3911. return 0;
  3912. return 1;
  3913. }
  3914. static inline u64 swevent_hash(u64 type, u32 event_id)
  3915. {
  3916. u64 val = event_id | (type << 32);
  3917. return hash_64(val, SWEVENT_HLIST_BITS);
  3918. }
  3919. static inline struct hlist_head *
  3920. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  3921. {
  3922. u64 hash = swevent_hash(type, event_id);
  3923. return &hlist->heads[hash];
  3924. }
  3925. /* For the read side: events when they trigger */
  3926. static inline struct hlist_head *
  3927. find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
  3928. {
  3929. struct swevent_hlist *hlist;
  3930. hlist = rcu_dereference(swhash->swevent_hlist);
  3931. if (!hlist)
  3932. return NULL;
  3933. return __find_swevent_head(hlist, type, event_id);
  3934. }
  3935. /* For the event head insertion and removal in the hlist */
  3936. static inline struct hlist_head *
  3937. find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
  3938. {
  3939. struct swevent_hlist *hlist;
  3940. u32 event_id = event->attr.config;
  3941. u64 type = event->attr.type;
  3942. /*
  3943. * Event scheduling is always serialized against hlist allocation
  3944. * and release. Which makes the protected version suitable here.
  3945. * The context lock guarantees that.
  3946. */
  3947. hlist = rcu_dereference_protected(swhash->swevent_hlist,
  3948. lockdep_is_held(&event->ctx->lock));
  3949. if (!hlist)
  3950. return NULL;
  3951. return __find_swevent_head(hlist, type, event_id);
  3952. }
  3953. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  3954. u64 nr,
  3955. struct perf_sample_data *data,
  3956. struct pt_regs *regs)
  3957. {
  3958. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3959. struct perf_event *event;
  3960. struct hlist_node *node;
  3961. struct hlist_head *head;
  3962. rcu_read_lock();
  3963. head = find_swevent_head_rcu(swhash, type, event_id);
  3964. if (!head)
  3965. goto end;
  3966. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  3967. if (perf_swevent_match(event, type, event_id, data, regs))
  3968. perf_swevent_event(event, nr, data, regs);
  3969. }
  3970. end:
  3971. rcu_read_unlock();
  3972. }
  3973. int perf_swevent_get_recursion_context(void)
  3974. {
  3975. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3976. return get_recursion_context(swhash->recursion);
  3977. }
  3978. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  3979. inline void perf_swevent_put_recursion_context(int rctx)
  3980. {
  3981. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3982. put_recursion_context(swhash->recursion, rctx);
  3983. }
  3984. void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  3985. {
  3986. struct perf_sample_data data;
  3987. int rctx;
  3988. preempt_disable_notrace();
  3989. rctx = perf_swevent_get_recursion_context();
  3990. if (rctx < 0)
  3991. return;
  3992. perf_sample_data_init(&data, addr);
  3993. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
  3994. perf_swevent_put_recursion_context(rctx);
  3995. preempt_enable_notrace();
  3996. }
  3997. static void perf_swevent_read(struct perf_event *event)
  3998. {
  3999. }
  4000. static int perf_swevent_add(struct perf_event *event, int flags)
  4001. {
  4002. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  4003. struct hw_perf_event *hwc = &event->hw;
  4004. struct hlist_head *head;
  4005. if (is_sampling_event(event)) {
  4006. hwc->last_period = hwc->sample_period;
  4007. perf_swevent_set_period(event);
  4008. }
  4009. hwc->state = !(flags & PERF_EF_START);
  4010. head = find_swevent_head(swhash, event);
  4011. if (WARN_ON_ONCE(!head))
  4012. return -EINVAL;
  4013. hlist_add_head_rcu(&event->hlist_entry, head);
  4014. return 0;
  4015. }
  4016. static void perf_swevent_del(struct perf_event *event, int flags)
  4017. {
  4018. hlist_del_rcu(&event->hlist_entry);
  4019. }
  4020. static void perf_swevent_start(struct perf_event *event, int flags)
  4021. {
  4022. event->hw.state = 0;
  4023. }
  4024. static void perf_swevent_stop(struct perf_event *event, int flags)
  4025. {
  4026. event->hw.state = PERF_HES_STOPPED;
  4027. }
  4028. /* Deref the hlist from the update side */
  4029. static inline struct swevent_hlist *
  4030. swevent_hlist_deref(struct swevent_htable *swhash)
  4031. {
  4032. return rcu_dereference_protected(swhash->swevent_hlist,
  4033. lockdep_is_held(&swhash->hlist_mutex));
  4034. }
  4035. static void swevent_hlist_release(struct swevent_htable *swhash)
  4036. {
  4037. struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
  4038. if (!hlist)
  4039. return;
  4040. rcu_assign_pointer(swhash->swevent_hlist, NULL);
  4041. kfree_rcu(hlist, rcu_head);
  4042. }
  4043. static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
  4044. {
  4045. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  4046. mutex_lock(&swhash->hlist_mutex);
  4047. if (!--swhash->hlist_refcount)
  4048. swevent_hlist_release(swhash);
  4049. mutex_unlock(&swhash->hlist_mutex);
  4050. }
  4051. static void swevent_hlist_put(struct perf_event *event)
  4052. {
  4053. int cpu;
  4054. if (event->cpu != -1) {
  4055. swevent_hlist_put_cpu(event, event->cpu);
  4056. return;
  4057. }
  4058. for_each_possible_cpu(cpu)
  4059. swevent_hlist_put_cpu(event, cpu);
  4060. }
  4061. static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
  4062. {
  4063. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  4064. int err = 0;
  4065. mutex_lock(&swhash->hlist_mutex);
  4066. if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
  4067. struct swevent_hlist *hlist;
  4068. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  4069. if (!hlist) {
  4070. err = -ENOMEM;
  4071. goto exit;
  4072. }
  4073. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  4074. }
  4075. swhash->hlist_refcount++;
  4076. exit:
  4077. mutex_unlock(&swhash->hlist_mutex);
  4078. return err;
  4079. }
  4080. static int swevent_hlist_get(struct perf_event *event)
  4081. {
  4082. int err;
  4083. int cpu, failed_cpu;
  4084. if (event->cpu != -1)
  4085. return swevent_hlist_get_cpu(event, event->cpu);
  4086. get_online_cpus();
  4087. for_each_possible_cpu(cpu) {
  4088. err = swevent_hlist_get_cpu(event, cpu);
  4089. if (err) {
  4090. failed_cpu = cpu;
  4091. goto fail;
  4092. }
  4093. }
  4094. put_online_cpus();
  4095. return 0;
  4096. fail:
  4097. for_each_possible_cpu(cpu) {
  4098. if (cpu == failed_cpu)
  4099. break;
  4100. swevent_hlist_put_cpu(event, cpu);
  4101. }
  4102. put_online_cpus();
  4103. return err;
  4104. }
  4105. struct jump_label_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
  4106. static void sw_perf_event_destroy(struct perf_event *event)
  4107. {
  4108. u64 event_id = event->attr.config;
  4109. WARN_ON(event->parent);
  4110. jump_label_dec(&perf_swevent_enabled[event_id]);
  4111. swevent_hlist_put(event);
  4112. }
  4113. static int perf_swevent_init(struct perf_event *event)
  4114. {
  4115. int event_id = event->attr.config;
  4116. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4117. return -ENOENT;
  4118. switch (event_id) {
  4119. case PERF_COUNT_SW_CPU_CLOCK:
  4120. case PERF_COUNT_SW_TASK_CLOCK:
  4121. return -ENOENT;
  4122. default:
  4123. break;
  4124. }
  4125. if (event_id >= PERF_COUNT_SW_MAX)
  4126. return -ENOENT;
  4127. if (!event->parent) {
  4128. int err;
  4129. err = swevent_hlist_get(event);
  4130. if (err)
  4131. return err;
  4132. jump_label_inc(&perf_swevent_enabled[event_id]);
  4133. event->destroy = sw_perf_event_destroy;
  4134. }
  4135. return 0;
  4136. }
  4137. static struct pmu perf_swevent = {
  4138. .task_ctx_nr = perf_sw_context,
  4139. .event_init = perf_swevent_init,
  4140. .add = perf_swevent_add,
  4141. .del = perf_swevent_del,
  4142. .start = perf_swevent_start,
  4143. .stop = perf_swevent_stop,
  4144. .read = perf_swevent_read,
  4145. };
  4146. #ifdef CONFIG_EVENT_TRACING
  4147. static int perf_tp_filter_match(struct perf_event *event,
  4148. struct perf_sample_data *data)
  4149. {
  4150. void *record = data->raw->data;
  4151. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  4152. return 1;
  4153. return 0;
  4154. }
  4155. static int perf_tp_event_match(struct perf_event *event,
  4156. struct perf_sample_data *data,
  4157. struct pt_regs *regs)
  4158. {
  4159. if (event->hw.state & PERF_HES_STOPPED)
  4160. return 0;
  4161. /*
  4162. * All tracepoints are from kernel-space.
  4163. */
  4164. if (event->attr.exclude_kernel)
  4165. return 0;
  4166. if (!perf_tp_filter_match(event, data))
  4167. return 0;
  4168. return 1;
  4169. }
  4170. void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
  4171. struct pt_regs *regs, struct hlist_head *head, int rctx)
  4172. {
  4173. struct perf_sample_data data;
  4174. struct perf_event *event;
  4175. struct hlist_node *node;
  4176. struct perf_raw_record raw = {
  4177. .size = entry_size,
  4178. .data = record,
  4179. };
  4180. perf_sample_data_init(&data, addr);
  4181. data.raw = &raw;
  4182. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  4183. if (perf_tp_event_match(event, &data, regs))
  4184. perf_swevent_event(event, count, &data, regs);
  4185. }
  4186. perf_swevent_put_recursion_context(rctx);
  4187. }
  4188. EXPORT_SYMBOL_GPL(perf_tp_event);
  4189. static void tp_perf_event_destroy(struct perf_event *event)
  4190. {
  4191. perf_trace_destroy(event);
  4192. }
  4193. static int perf_tp_event_init(struct perf_event *event)
  4194. {
  4195. int err;
  4196. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  4197. return -ENOENT;
  4198. err = perf_trace_init(event);
  4199. if (err)
  4200. return err;
  4201. event->destroy = tp_perf_event_destroy;
  4202. return 0;
  4203. }
  4204. static struct pmu perf_tracepoint = {
  4205. .task_ctx_nr = perf_sw_context,
  4206. .event_init = perf_tp_event_init,
  4207. .add = perf_trace_add,
  4208. .del = perf_trace_del,
  4209. .start = perf_swevent_start,
  4210. .stop = perf_swevent_stop,
  4211. .read = perf_swevent_read,
  4212. };
  4213. static inline void perf_tp_register(void)
  4214. {
  4215. perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
  4216. }
  4217. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  4218. {
  4219. char *filter_str;
  4220. int ret;
  4221. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  4222. return -EINVAL;
  4223. filter_str = strndup_user(arg, PAGE_SIZE);
  4224. if (IS_ERR(filter_str))
  4225. return PTR_ERR(filter_str);
  4226. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  4227. kfree(filter_str);
  4228. return ret;
  4229. }
  4230. static void perf_event_free_filter(struct perf_event *event)
  4231. {
  4232. ftrace_profile_free_filter(event);
  4233. }
  4234. #else
  4235. static inline void perf_tp_register(void)
  4236. {
  4237. }
  4238. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  4239. {
  4240. return -ENOENT;
  4241. }
  4242. static void perf_event_free_filter(struct perf_event *event)
  4243. {
  4244. }
  4245. #endif /* CONFIG_EVENT_TRACING */
  4246. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  4247. void perf_bp_event(struct perf_event *bp, void *data)
  4248. {
  4249. struct perf_sample_data sample;
  4250. struct pt_regs *regs = data;
  4251. perf_sample_data_init(&sample, bp->attr.bp_addr);
  4252. if (!bp->hw.state && !perf_exclude_event(bp, regs))
  4253. perf_swevent_event(bp, 1, &sample, regs);
  4254. }
  4255. #endif
  4256. /*
  4257. * hrtimer based swevent callback
  4258. */
  4259. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  4260. {
  4261. enum hrtimer_restart ret = HRTIMER_RESTART;
  4262. struct perf_sample_data data;
  4263. struct pt_regs *regs;
  4264. struct perf_event *event;
  4265. u64 period;
  4266. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  4267. if (event->state != PERF_EVENT_STATE_ACTIVE)
  4268. return HRTIMER_NORESTART;
  4269. event->pmu->read(event);
  4270. perf_sample_data_init(&data, 0);
  4271. data.period = event->hw.last_period;
  4272. regs = get_irq_regs();
  4273. if (regs && !perf_exclude_event(event, regs)) {
  4274. if (!(event->attr.exclude_idle && current->pid == 0))
  4275. if (perf_event_overflow(event, &data, regs))
  4276. ret = HRTIMER_NORESTART;
  4277. }
  4278. period = max_t(u64, 10000, event->hw.sample_period);
  4279. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  4280. return ret;
  4281. }
  4282. static void perf_swevent_start_hrtimer(struct perf_event *event)
  4283. {
  4284. struct hw_perf_event *hwc = &event->hw;
  4285. s64 period;
  4286. if (!is_sampling_event(event))
  4287. return;
  4288. period = local64_read(&hwc->period_left);
  4289. if (period) {
  4290. if (period < 0)
  4291. period = 10000;
  4292. local64_set(&hwc->period_left, 0);
  4293. } else {
  4294. period = max_t(u64, 10000, hwc->sample_period);
  4295. }
  4296. __hrtimer_start_range_ns(&hwc->hrtimer,
  4297. ns_to_ktime(period), 0,
  4298. HRTIMER_MODE_REL_PINNED, 0);
  4299. }
  4300. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  4301. {
  4302. struct hw_perf_event *hwc = &event->hw;
  4303. if (is_sampling_event(event)) {
  4304. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  4305. local64_set(&hwc->period_left, ktime_to_ns(remaining));
  4306. hrtimer_cancel(&hwc->hrtimer);
  4307. }
  4308. }
  4309. static void perf_swevent_init_hrtimer(struct perf_event *event)
  4310. {
  4311. struct hw_perf_event *hwc = &event->hw;
  4312. if (!is_sampling_event(event))
  4313. return;
  4314. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  4315. hwc->hrtimer.function = perf_swevent_hrtimer;
  4316. /*
  4317. * Since hrtimers have a fixed rate, we can do a static freq->period
  4318. * mapping and avoid the whole period adjust feedback stuff.
  4319. */
  4320. if (event->attr.freq) {
  4321. long freq = event->attr.sample_freq;
  4322. event->attr.sample_period = NSEC_PER_SEC / freq;
  4323. hwc->sample_period = event->attr.sample_period;
  4324. local64_set(&hwc->period_left, hwc->sample_period);
  4325. event->attr.freq = 0;
  4326. }
  4327. }
  4328. /*
  4329. * Software event: cpu wall time clock
  4330. */
  4331. static void cpu_clock_event_update(struct perf_event *event)
  4332. {
  4333. s64 prev;
  4334. u64 now;
  4335. now = local_clock();
  4336. prev = local64_xchg(&event->hw.prev_count, now);
  4337. local64_add(now - prev, &event->count);
  4338. }
  4339. static void cpu_clock_event_start(struct perf_event *event, int flags)
  4340. {
  4341. local64_set(&event->hw.prev_count, local_clock());
  4342. perf_swevent_start_hrtimer(event);
  4343. }
  4344. static void cpu_clock_event_stop(struct perf_event *event, int flags)
  4345. {
  4346. perf_swevent_cancel_hrtimer(event);
  4347. cpu_clock_event_update(event);
  4348. }
  4349. static int cpu_clock_event_add(struct perf_event *event, int flags)
  4350. {
  4351. if (flags & PERF_EF_START)
  4352. cpu_clock_event_start(event, flags);
  4353. return 0;
  4354. }
  4355. static void cpu_clock_event_del(struct perf_event *event, int flags)
  4356. {
  4357. cpu_clock_event_stop(event, flags);
  4358. }
  4359. static void cpu_clock_event_read(struct perf_event *event)
  4360. {
  4361. cpu_clock_event_update(event);
  4362. }
  4363. static int cpu_clock_event_init(struct perf_event *event)
  4364. {
  4365. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4366. return -ENOENT;
  4367. if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
  4368. return -ENOENT;
  4369. perf_swevent_init_hrtimer(event);
  4370. return 0;
  4371. }
  4372. static struct pmu perf_cpu_clock = {
  4373. .task_ctx_nr = perf_sw_context,
  4374. .event_init = cpu_clock_event_init,
  4375. .add = cpu_clock_event_add,
  4376. .del = cpu_clock_event_del,
  4377. .start = cpu_clock_event_start,
  4378. .stop = cpu_clock_event_stop,
  4379. .read = cpu_clock_event_read,
  4380. };
  4381. /*
  4382. * Software event: task time clock
  4383. */
  4384. static void task_clock_event_update(struct perf_event *event, u64 now)
  4385. {
  4386. u64 prev;
  4387. s64 delta;
  4388. prev = local64_xchg(&event->hw.prev_count, now);
  4389. delta = now - prev;
  4390. local64_add(delta, &event->count);
  4391. }
  4392. static void task_clock_event_start(struct perf_event *event, int flags)
  4393. {
  4394. local64_set(&event->hw.prev_count, event->ctx->time);
  4395. perf_swevent_start_hrtimer(event);
  4396. }
  4397. static void task_clock_event_stop(struct perf_event *event, int flags)
  4398. {
  4399. perf_swevent_cancel_hrtimer(event);
  4400. task_clock_event_update(event, event->ctx->time);
  4401. }
  4402. static int task_clock_event_add(struct perf_event *event, int flags)
  4403. {
  4404. if (flags & PERF_EF_START)
  4405. task_clock_event_start(event, flags);
  4406. return 0;
  4407. }
  4408. static void task_clock_event_del(struct perf_event *event, int flags)
  4409. {
  4410. task_clock_event_stop(event, PERF_EF_UPDATE);
  4411. }
  4412. static void task_clock_event_read(struct perf_event *event)
  4413. {
  4414. u64 now = perf_clock();
  4415. u64 delta = now - event->ctx->timestamp;
  4416. u64 time = event->ctx->time + delta;
  4417. task_clock_event_update(event, time);
  4418. }
  4419. static int task_clock_event_init(struct perf_event *event)
  4420. {
  4421. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4422. return -ENOENT;
  4423. if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
  4424. return -ENOENT;
  4425. perf_swevent_init_hrtimer(event);
  4426. return 0;
  4427. }
  4428. static struct pmu perf_task_clock = {
  4429. .task_ctx_nr = perf_sw_context,
  4430. .event_init = task_clock_event_init,
  4431. .add = task_clock_event_add,
  4432. .del = task_clock_event_del,
  4433. .start = task_clock_event_start,
  4434. .stop = task_clock_event_stop,
  4435. .read = task_clock_event_read,
  4436. };
  4437. static void perf_pmu_nop_void(struct pmu *pmu)
  4438. {
  4439. }
  4440. static int perf_pmu_nop_int(struct pmu *pmu)
  4441. {
  4442. return 0;
  4443. }
  4444. static void perf_pmu_start_txn(struct pmu *pmu)
  4445. {
  4446. perf_pmu_disable(pmu);
  4447. }
  4448. static int perf_pmu_commit_txn(struct pmu *pmu)
  4449. {
  4450. perf_pmu_enable(pmu);
  4451. return 0;
  4452. }
  4453. static void perf_pmu_cancel_txn(struct pmu *pmu)
  4454. {
  4455. perf_pmu_enable(pmu);
  4456. }
  4457. /*
  4458. * Ensures all contexts with the same task_ctx_nr have the same
  4459. * pmu_cpu_context too.
  4460. */
  4461. static void *find_pmu_context(int ctxn)
  4462. {
  4463. struct pmu *pmu;
  4464. if (ctxn < 0)
  4465. return NULL;
  4466. list_for_each_entry(pmu, &pmus, entry) {
  4467. if (pmu->task_ctx_nr == ctxn)
  4468. return pmu->pmu_cpu_context;
  4469. }
  4470. return NULL;
  4471. }
  4472. static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
  4473. {
  4474. int cpu;
  4475. for_each_possible_cpu(cpu) {
  4476. struct perf_cpu_context *cpuctx;
  4477. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  4478. if (cpuctx->active_pmu == old_pmu)
  4479. cpuctx->active_pmu = pmu;
  4480. }
  4481. }
  4482. static void free_pmu_context(struct pmu *pmu)
  4483. {
  4484. struct pmu *i;
  4485. mutex_lock(&pmus_lock);
  4486. /*
  4487. * Like a real lame refcount.
  4488. */
  4489. list_for_each_entry(i, &pmus, entry) {
  4490. if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
  4491. update_pmu_context(i, pmu);
  4492. goto out;
  4493. }
  4494. }
  4495. free_percpu(pmu->pmu_cpu_context);
  4496. out:
  4497. mutex_unlock(&pmus_lock);
  4498. }
  4499. static struct idr pmu_idr;
  4500. static ssize_t
  4501. type_show(struct device *dev, struct device_attribute *attr, char *page)
  4502. {
  4503. struct pmu *pmu = dev_get_drvdata(dev);
  4504. return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
  4505. }
  4506. static struct device_attribute pmu_dev_attrs[] = {
  4507. __ATTR_RO(type),
  4508. __ATTR_NULL,
  4509. };
  4510. static int pmu_bus_running;
  4511. static struct bus_type pmu_bus = {
  4512. .name = "event_source",
  4513. .dev_attrs = pmu_dev_attrs,
  4514. };
  4515. static void pmu_dev_release(struct device *dev)
  4516. {
  4517. kfree(dev);
  4518. }
  4519. static int pmu_dev_alloc(struct pmu *pmu)
  4520. {
  4521. int ret = -ENOMEM;
  4522. pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  4523. if (!pmu->dev)
  4524. goto out;
  4525. device_initialize(pmu->dev);
  4526. ret = dev_set_name(pmu->dev, "%s", pmu->name);
  4527. if (ret)
  4528. goto free_dev;
  4529. dev_set_drvdata(pmu->dev, pmu);
  4530. pmu->dev->bus = &pmu_bus;
  4531. pmu->dev->release = pmu_dev_release;
  4532. ret = device_add(pmu->dev);
  4533. if (ret)
  4534. goto free_dev;
  4535. out:
  4536. return ret;
  4537. free_dev:
  4538. put_device(pmu->dev);
  4539. goto out;
  4540. }
  4541. static struct lock_class_key cpuctx_mutex;
  4542. static struct lock_class_key cpuctx_lock;
  4543. int perf_pmu_register(struct pmu *pmu, char *name, int type)
  4544. {
  4545. int cpu, ret;
  4546. mutex_lock(&pmus_lock);
  4547. ret = -ENOMEM;
  4548. pmu->pmu_disable_count = alloc_percpu(int);
  4549. if (!pmu->pmu_disable_count)
  4550. goto unlock;
  4551. pmu->type = -1;
  4552. if (!name)
  4553. goto skip_type;
  4554. pmu->name = name;
  4555. if (type < 0) {
  4556. int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
  4557. if (!err)
  4558. goto free_pdc;
  4559. err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
  4560. if (err) {
  4561. ret = err;
  4562. goto free_pdc;
  4563. }
  4564. }
  4565. pmu->type = type;
  4566. if (pmu_bus_running) {
  4567. ret = pmu_dev_alloc(pmu);
  4568. if (ret)
  4569. goto free_idr;
  4570. }
  4571. skip_type:
  4572. pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
  4573. if (pmu->pmu_cpu_context)
  4574. goto got_cpu_context;
  4575. pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
  4576. if (!pmu->pmu_cpu_context)
  4577. goto free_dev;
  4578. for_each_possible_cpu(cpu) {
  4579. struct perf_cpu_context *cpuctx;
  4580. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  4581. __perf_event_init_context(&cpuctx->ctx);
  4582. lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
  4583. lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
  4584. cpuctx->ctx.type = cpu_context;
  4585. cpuctx->ctx.pmu = pmu;
  4586. cpuctx->jiffies_interval = 1;
  4587. INIT_LIST_HEAD(&cpuctx->rotation_list);
  4588. cpuctx->active_pmu = pmu;
  4589. }
  4590. got_cpu_context:
  4591. if (!pmu->start_txn) {
  4592. if (pmu->pmu_enable) {
  4593. /*
  4594. * If we have pmu_enable/pmu_disable calls, install
  4595. * transaction stubs that use that to try and batch
  4596. * hardware accesses.
  4597. */
  4598. pmu->start_txn = perf_pmu_start_txn;
  4599. pmu->commit_txn = perf_pmu_commit_txn;
  4600. pmu->cancel_txn = perf_pmu_cancel_txn;
  4601. } else {
  4602. pmu->start_txn = perf_pmu_nop_void;
  4603. pmu->commit_txn = perf_pmu_nop_int;
  4604. pmu->cancel_txn = perf_pmu_nop_void;
  4605. }
  4606. }
  4607. if (!pmu->pmu_enable) {
  4608. pmu->pmu_enable = perf_pmu_nop_void;
  4609. pmu->pmu_disable = perf_pmu_nop_void;
  4610. }
  4611. list_add_rcu(&pmu->entry, &pmus);
  4612. ret = 0;
  4613. unlock:
  4614. mutex_unlock(&pmus_lock);
  4615. return ret;
  4616. free_dev:
  4617. device_del(pmu->dev);
  4618. put_device(pmu->dev);
  4619. free_idr:
  4620. if (pmu->type >= PERF_TYPE_MAX)
  4621. idr_remove(&pmu_idr, pmu->type);
  4622. free_pdc:
  4623. free_percpu(pmu->pmu_disable_count);
  4624. goto unlock;
  4625. }
  4626. void perf_pmu_unregister(struct pmu *pmu)
  4627. {
  4628. mutex_lock(&pmus_lock);
  4629. list_del_rcu(&pmu->entry);
  4630. mutex_unlock(&pmus_lock);
  4631. /*
  4632. * We dereference the pmu list under both SRCU and regular RCU, so
  4633. * synchronize against both of those.
  4634. */
  4635. synchronize_srcu(&pmus_srcu);
  4636. synchronize_rcu();
  4637. free_percpu(pmu->pmu_disable_count);
  4638. if (pmu->type >= PERF_TYPE_MAX)
  4639. idr_remove(&pmu_idr, pmu->type);
  4640. device_del(pmu->dev);
  4641. put_device(pmu->dev);
  4642. free_pmu_context(pmu);
  4643. }
  4644. struct pmu *perf_init_event(struct perf_event *event)
  4645. {
  4646. struct pmu *pmu = NULL;
  4647. int idx;
  4648. int ret;
  4649. idx = srcu_read_lock(&pmus_srcu);
  4650. rcu_read_lock();
  4651. pmu = idr_find(&pmu_idr, event->attr.type);
  4652. rcu_read_unlock();
  4653. if (pmu) {
  4654. event->pmu = pmu;
  4655. ret = pmu->event_init(event);
  4656. if (ret)
  4657. pmu = ERR_PTR(ret);
  4658. goto unlock;
  4659. }
  4660. list_for_each_entry_rcu(pmu, &pmus, entry) {
  4661. event->pmu = pmu;
  4662. ret = pmu->event_init(event);
  4663. if (!ret)
  4664. goto unlock;
  4665. if (ret != -ENOENT) {
  4666. pmu = ERR_PTR(ret);
  4667. goto unlock;
  4668. }
  4669. }
  4670. pmu = ERR_PTR(-ENOENT);
  4671. unlock:
  4672. srcu_read_unlock(&pmus_srcu, idx);
  4673. return pmu;
  4674. }
  4675. /*
  4676. * Allocate and initialize a event structure
  4677. */
  4678. static struct perf_event *
  4679. perf_event_alloc(struct perf_event_attr *attr, int cpu,
  4680. struct task_struct *task,
  4681. struct perf_event *group_leader,
  4682. struct perf_event *parent_event,
  4683. perf_overflow_handler_t overflow_handler,
  4684. void *context)
  4685. {
  4686. struct pmu *pmu;
  4687. struct perf_event *event;
  4688. struct hw_perf_event *hwc;
  4689. long err;
  4690. if ((unsigned)cpu >= nr_cpu_ids) {
  4691. if (!task || cpu != -1)
  4692. return ERR_PTR(-EINVAL);
  4693. }
  4694. event = kzalloc(sizeof(*event), GFP_KERNEL);
  4695. if (!event)
  4696. return ERR_PTR(-ENOMEM);
  4697. /*
  4698. * Single events are their own group leaders, with an
  4699. * empty sibling list:
  4700. */
  4701. if (!group_leader)
  4702. group_leader = event;
  4703. mutex_init(&event->child_mutex);
  4704. INIT_LIST_HEAD(&event->child_list);
  4705. INIT_LIST_HEAD(&event->group_entry);
  4706. INIT_LIST_HEAD(&event->event_entry);
  4707. INIT_LIST_HEAD(&event->sibling_list);
  4708. init_waitqueue_head(&event->waitq);
  4709. init_irq_work(&event->pending, perf_pending_event);
  4710. mutex_init(&event->mmap_mutex);
  4711. event->cpu = cpu;
  4712. event->attr = *attr;
  4713. event->group_leader = group_leader;
  4714. event->pmu = NULL;
  4715. event->oncpu = -1;
  4716. event->parent = parent_event;
  4717. event->ns = get_pid_ns(current->nsproxy->pid_ns);
  4718. event->id = atomic64_inc_return(&perf_event_id);
  4719. event->state = PERF_EVENT_STATE_INACTIVE;
  4720. if (task) {
  4721. event->attach_state = PERF_ATTACH_TASK;
  4722. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  4723. /*
  4724. * hw_breakpoint is a bit difficult here..
  4725. */
  4726. if (attr->type == PERF_TYPE_BREAKPOINT)
  4727. event->hw.bp_target = task;
  4728. #endif
  4729. }
  4730. if (!overflow_handler && parent_event) {
  4731. overflow_handler = parent_event->overflow_handler;
  4732. context = parent_event->overflow_handler_context;
  4733. }
  4734. event->overflow_handler = overflow_handler;
  4735. event->overflow_handler_context = context;
  4736. if (attr->disabled)
  4737. event->state = PERF_EVENT_STATE_OFF;
  4738. pmu = NULL;
  4739. hwc = &event->hw;
  4740. hwc->sample_period = attr->sample_period;
  4741. if (attr->freq && attr->sample_freq)
  4742. hwc->sample_period = 1;
  4743. hwc->last_period = hwc->sample_period;
  4744. local64_set(&hwc->period_left, hwc->sample_period);
  4745. /*
  4746. * we currently do not support PERF_FORMAT_GROUP on inherited events
  4747. */
  4748. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  4749. goto done;
  4750. pmu = perf_init_event(event);
  4751. done:
  4752. err = 0;
  4753. if (!pmu)
  4754. err = -EINVAL;
  4755. else if (IS_ERR(pmu))
  4756. err = PTR_ERR(pmu);
  4757. if (err) {
  4758. if (event->ns)
  4759. put_pid_ns(event->ns);
  4760. kfree(event);
  4761. return ERR_PTR(err);
  4762. }
  4763. if (!event->parent) {
  4764. if (event->attach_state & PERF_ATTACH_TASK)
  4765. jump_label_inc(&perf_sched_events);
  4766. if (event->attr.mmap || event->attr.mmap_data)
  4767. atomic_inc(&nr_mmap_events);
  4768. if (event->attr.comm)
  4769. atomic_inc(&nr_comm_events);
  4770. if (event->attr.task)
  4771. atomic_inc(&nr_task_events);
  4772. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  4773. err = get_callchain_buffers();
  4774. if (err) {
  4775. free_event(event);
  4776. return ERR_PTR(err);
  4777. }
  4778. }
  4779. }
  4780. return event;
  4781. }
  4782. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  4783. struct perf_event_attr *attr)
  4784. {
  4785. u32 size;
  4786. int ret;
  4787. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  4788. return -EFAULT;
  4789. /*
  4790. * zero the full structure, so that a short copy will be nice.
  4791. */
  4792. memset(attr, 0, sizeof(*attr));
  4793. ret = get_user(size, &uattr->size);
  4794. if (ret)
  4795. return ret;
  4796. if (size > PAGE_SIZE) /* silly large */
  4797. goto err_size;
  4798. if (!size) /* abi compat */
  4799. size = PERF_ATTR_SIZE_VER0;
  4800. if (size < PERF_ATTR_SIZE_VER0)
  4801. goto err_size;
  4802. /*
  4803. * If we're handed a bigger struct than we know of,
  4804. * ensure all the unknown bits are 0 - i.e. new
  4805. * user-space does not rely on any kernel feature
  4806. * extensions we dont know about yet.
  4807. */
  4808. if (size > sizeof(*attr)) {
  4809. unsigned char __user *addr;
  4810. unsigned char __user *end;
  4811. unsigned char val;
  4812. addr = (void __user *)uattr + sizeof(*attr);
  4813. end = (void __user *)uattr + size;
  4814. for (; addr < end; addr++) {
  4815. ret = get_user(val, addr);
  4816. if (ret)
  4817. return ret;
  4818. if (val)
  4819. goto err_size;
  4820. }
  4821. size = sizeof(*attr);
  4822. }
  4823. ret = copy_from_user(attr, uattr, size);
  4824. if (ret)
  4825. return -EFAULT;
  4826. if (attr->__reserved_1)
  4827. return -EINVAL;
  4828. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  4829. return -EINVAL;
  4830. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  4831. return -EINVAL;
  4832. out:
  4833. return ret;
  4834. err_size:
  4835. put_user(sizeof(*attr), &uattr->size);
  4836. ret = -E2BIG;
  4837. goto out;
  4838. }
  4839. static int
  4840. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  4841. {
  4842. struct ring_buffer *rb = NULL, *old_rb = NULL;
  4843. int ret = -EINVAL;
  4844. if (!output_event)
  4845. goto set;
  4846. /* don't allow circular references */
  4847. if (event == output_event)
  4848. goto out;
  4849. /*
  4850. * Don't allow cross-cpu buffers
  4851. */
  4852. if (output_event->cpu != event->cpu)
  4853. goto out;
  4854. /*
  4855. * If its not a per-cpu rb, it must be the same task.
  4856. */
  4857. if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  4858. goto out;
  4859. set:
  4860. mutex_lock(&event->mmap_mutex);
  4861. /* Can't redirect output if we've got an active mmap() */
  4862. if (atomic_read(&event->mmap_count))
  4863. goto unlock;
  4864. if (output_event) {
  4865. /* get the rb we want to redirect to */
  4866. rb = ring_buffer_get(output_event);
  4867. if (!rb)
  4868. goto unlock;
  4869. }
  4870. old_rb = event->rb;
  4871. rcu_assign_pointer(event->rb, rb);
  4872. ret = 0;
  4873. unlock:
  4874. mutex_unlock(&event->mmap_mutex);
  4875. if (old_rb)
  4876. ring_buffer_put(old_rb);
  4877. out:
  4878. return ret;
  4879. }
  4880. /**
  4881. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  4882. *
  4883. * @attr_uptr: event_id type attributes for monitoring/sampling
  4884. * @pid: target pid
  4885. * @cpu: target cpu
  4886. * @group_fd: group leader event fd
  4887. */
  4888. SYSCALL_DEFINE5(perf_event_open,
  4889. struct perf_event_attr __user *, attr_uptr,
  4890. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  4891. {
  4892. struct perf_event *group_leader = NULL, *output_event = NULL;
  4893. struct perf_event *event, *sibling;
  4894. struct perf_event_attr attr;
  4895. struct perf_event_context *ctx;
  4896. struct file *event_file = NULL;
  4897. struct file *group_file = NULL;
  4898. struct task_struct *task = NULL;
  4899. struct pmu *pmu;
  4900. int event_fd;
  4901. int move_group = 0;
  4902. int fput_needed = 0;
  4903. int err;
  4904. /* for future expandability... */
  4905. if (flags & ~PERF_FLAG_ALL)
  4906. return -EINVAL;
  4907. err = perf_copy_attr(attr_uptr, &attr);
  4908. if (err)
  4909. return err;
  4910. if (!attr.exclude_kernel) {
  4911. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  4912. return -EACCES;
  4913. }
  4914. if (attr.freq) {
  4915. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  4916. return -EINVAL;
  4917. }
  4918. /*
  4919. * In cgroup mode, the pid argument is used to pass the fd
  4920. * opened to the cgroup directory in cgroupfs. The cpu argument
  4921. * designates the cpu on which to monitor threads from that
  4922. * cgroup.
  4923. */
  4924. if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
  4925. return -EINVAL;
  4926. event_fd = get_unused_fd_flags(O_RDWR);
  4927. if (event_fd < 0)
  4928. return event_fd;
  4929. if (group_fd != -1) {
  4930. group_leader = perf_fget_light(group_fd, &fput_needed);
  4931. if (IS_ERR(group_leader)) {
  4932. err = PTR_ERR(group_leader);
  4933. goto err_fd;
  4934. }
  4935. group_file = group_leader->filp;
  4936. if (flags & PERF_FLAG_FD_OUTPUT)
  4937. output_event = group_leader;
  4938. if (flags & PERF_FLAG_FD_NO_GROUP)
  4939. group_leader = NULL;
  4940. }
  4941. if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
  4942. task = find_lively_task_by_vpid(pid);
  4943. if (IS_ERR(task)) {
  4944. err = PTR_ERR(task);
  4945. goto err_group_fd;
  4946. }
  4947. }
  4948. event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
  4949. NULL, NULL);
  4950. if (IS_ERR(event)) {
  4951. err = PTR_ERR(event);
  4952. goto err_task;
  4953. }
  4954. if (flags & PERF_FLAG_PID_CGROUP) {
  4955. err = perf_cgroup_connect(pid, event, &attr, group_leader);
  4956. if (err)
  4957. goto err_alloc;
  4958. /*
  4959. * one more event:
  4960. * - that has cgroup constraint on event->cpu
  4961. * - that may need work on context switch
  4962. */
  4963. atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
  4964. jump_label_inc(&perf_sched_events);
  4965. }
  4966. /*
  4967. * Special case software events and allow them to be part of
  4968. * any hardware group.
  4969. */
  4970. pmu = event->pmu;
  4971. if (group_leader &&
  4972. (is_software_event(event) != is_software_event(group_leader))) {
  4973. if (is_software_event(event)) {
  4974. /*
  4975. * If event and group_leader are not both a software
  4976. * event, and event is, then group leader is not.
  4977. *
  4978. * Allow the addition of software events to !software
  4979. * groups, this is safe because software events never
  4980. * fail to schedule.
  4981. */
  4982. pmu = group_leader->pmu;
  4983. } else if (is_software_event(group_leader) &&
  4984. (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
  4985. /*
  4986. * In case the group is a pure software group, and we
  4987. * try to add a hardware event, move the whole group to
  4988. * the hardware context.
  4989. */
  4990. move_group = 1;
  4991. }
  4992. }
  4993. /*
  4994. * Get the target context (task or percpu):
  4995. */
  4996. ctx = find_get_context(pmu, task, cpu);
  4997. if (IS_ERR(ctx)) {
  4998. err = PTR_ERR(ctx);
  4999. goto err_alloc;
  5000. }
  5001. if (task) {
  5002. put_task_struct(task);
  5003. task = NULL;
  5004. }
  5005. /*
  5006. * Look up the group leader (we will attach this event to it):
  5007. */
  5008. if (group_leader) {
  5009. err = -EINVAL;
  5010. /*
  5011. * Do not allow a recursive hierarchy (this new sibling
  5012. * becoming part of another group-sibling):
  5013. */
  5014. if (group_leader->group_leader != group_leader)
  5015. goto err_context;
  5016. /*
  5017. * Do not allow to attach to a group in a different
  5018. * task or CPU context:
  5019. */
  5020. if (move_group) {
  5021. if (group_leader->ctx->type != ctx->type)
  5022. goto err_context;
  5023. } else {
  5024. if (group_leader->ctx != ctx)
  5025. goto err_context;
  5026. }
  5027. /*
  5028. * Only a group leader can be exclusive or pinned
  5029. */
  5030. if (attr.exclusive || attr.pinned)
  5031. goto err_context;
  5032. }
  5033. if (output_event) {
  5034. err = perf_event_set_output(event, output_event);
  5035. if (err)
  5036. goto err_context;
  5037. }
  5038. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
  5039. if (IS_ERR(event_file)) {
  5040. err = PTR_ERR(event_file);
  5041. goto err_context;
  5042. }
  5043. if (move_group) {
  5044. struct perf_event_context *gctx = group_leader->ctx;
  5045. mutex_lock(&gctx->mutex);
  5046. perf_remove_from_context(group_leader);
  5047. list_for_each_entry(sibling, &group_leader->sibling_list,
  5048. group_entry) {
  5049. perf_remove_from_context(sibling);
  5050. put_ctx(gctx);
  5051. }
  5052. mutex_unlock(&gctx->mutex);
  5053. put_ctx(gctx);
  5054. }
  5055. event->filp = event_file;
  5056. WARN_ON_ONCE(ctx->parent_ctx);
  5057. mutex_lock(&ctx->mutex);
  5058. if (move_group) {
  5059. perf_install_in_context(ctx, group_leader, cpu);
  5060. get_ctx(ctx);
  5061. list_for_each_entry(sibling, &group_leader->sibling_list,
  5062. group_entry) {
  5063. perf_install_in_context(ctx, sibling, cpu);
  5064. get_ctx(ctx);
  5065. }
  5066. }
  5067. perf_install_in_context(ctx, event, cpu);
  5068. ++ctx->generation;
  5069. perf_unpin_context(ctx);
  5070. mutex_unlock(&ctx->mutex);
  5071. event->owner = current;
  5072. mutex_lock(&current->perf_event_mutex);
  5073. list_add_tail(&event->owner_entry, &current->perf_event_list);
  5074. mutex_unlock(&current->perf_event_mutex);
  5075. /*
  5076. * Precalculate sample_data sizes
  5077. */
  5078. perf_event__header_size(event);
  5079. perf_event__id_header_size(event);
  5080. /*
  5081. * Drop the reference on the group_event after placing the
  5082. * new event on the sibling_list. This ensures destruction
  5083. * of the group leader will find the pointer to itself in
  5084. * perf_group_detach().
  5085. */
  5086. fput_light(group_file, fput_needed);
  5087. fd_install(event_fd, event_file);
  5088. return event_fd;
  5089. err_context:
  5090. perf_unpin_context(ctx);
  5091. put_ctx(ctx);
  5092. err_alloc:
  5093. free_event(event);
  5094. err_task:
  5095. if (task)
  5096. put_task_struct(task);
  5097. err_group_fd:
  5098. fput_light(group_file, fput_needed);
  5099. err_fd:
  5100. put_unused_fd(event_fd);
  5101. return err;
  5102. }
  5103. /**
  5104. * perf_event_create_kernel_counter
  5105. *
  5106. * @attr: attributes of the counter to create
  5107. * @cpu: cpu in which the counter is bound
  5108. * @task: task to profile (NULL for percpu)
  5109. */
  5110. struct perf_event *
  5111. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  5112. struct task_struct *task,
  5113. perf_overflow_handler_t overflow_handler,
  5114. void *context)
  5115. {
  5116. struct perf_event_context *ctx;
  5117. struct perf_event *event;
  5118. int err;
  5119. /*
  5120. * Get the target context (task or percpu):
  5121. */
  5122. event = perf_event_alloc(attr, cpu, task, NULL, NULL,
  5123. overflow_handler, context);
  5124. if (IS_ERR(event)) {
  5125. err = PTR_ERR(event);
  5126. goto err;
  5127. }
  5128. ctx = find_get_context(event->pmu, task, cpu);
  5129. if (IS_ERR(ctx)) {
  5130. err = PTR_ERR(ctx);
  5131. goto err_free;
  5132. }
  5133. event->filp = NULL;
  5134. WARN_ON_ONCE(ctx->parent_ctx);
  5135. mutex_lock(&ctx->mutex);
  5136. perf_install_in_context(ctx, event, cpu);
  5137. ++ctx->generation;
  5138. perf_unpin_context(ctx);
  5139. mutex_unlock(&ctx->mutex);
  5140. return event;
  5141. err_free:
  5142. free_event(event);
  5143. err:
  5144. return ERR_PTR(err);
  5145. }
  5146. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  5147. static void sync_child_event(struct perf_event *child_event,
  5148. struct task_struct *child)
  5149. {
  5150. struct perf_event *parent_event = child_event->parent;
  5151. u64 child_val;
  5152. if (child_event->attr.inherit_stat)
  5153. perf_event_read_event(child_event, child);
  5154. child_val = perf_event_count(child_event);
  5155. /*
  5156. * Add back the child's count to the parent's count:
  5157. */
  5158. atomic64_add(child_val, &parent_event->child_count);
  5159. atomic64_add(child_event->total_time_enabled,
  5160. &parent_event->child_total_time_enabled);
  5161. atomic64_add(child_event->total_time_running,
  5162. &parent_event->child_total_time_running);
  5163. /*
  5164. * Remove this event from the parent's list
  5165. */
  5166. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  5167. mutex_lock(&parent_event->child_mutex);
  5168. list_del_init(&child_event->child_list);
  5169. mutex_unlock(&parent_event->child_mutex);
  5170. /*
  5171. * Release the parent event, if this was the last
  5172. * reference to it.
  5173. */
  5174. fput(parent_event->filp);
  5175. }
  5176. static void
  5177. __perf_event_exit_task(struct perf_event *child_event,
  5178. struct perf_event_context *child_ctx,
  5179. struct task_struct *child)
  5180. {
  5181. if (child_event->parent) {
  5182. raw_spin_lock_irq(&child_ctx->lock);
  5183. perf_group_detach(child_event);
  5184. raw_spin_unlock_irq(&child_ctx->lock);
  5185. }
  5186. perf_remove_from_context(child_event);
  5187. /*
  5188. * It can happen that the parent exits first, and has events
  5189. * that are still around due to the child reference. These
  5190. * events need to be zapped.
  5191. */
  5192. if (child_event->parent) {
  5193. sync_child_event(child_event, child);
  5194. free_event(child_event);
  5195. }
  5196. }
  5197. static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
  5198. {
  5199. struct perf_event *child_event, *tmp;
  5200. struct perf_event_context *child_ctx;
  5201. unsigned long flags;
  5202. if (likely(!child->perf_event_ctxp[ctxn])) {
  5203. perf_event_task(child, NULL, 0);
  5204. return;
  5205. }
  5206. local_irq_save(flags);
  5207. /*
  5208. * We can't reschedule here because interrupts are disabled,
  5209. * and either child is current or it is a task that can't be
  5210. * scheduled, so we are now safe from rescheduling changing
  5211. * our context.
  5212. */
  5213. child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
  5214. /*
  5215. * Take the context lock here so that if find_get_context is
  5216. * reading child->perf_event_ctxp, we wait until it has
  5217. * incremented the context's refcount before we do put_ctx below.
  5218. */
  5219. raw_spin_lock(&child_ctx->lock);
  5220. task_ctx_sched_out(child_ctx);
  5221. child->perf_event_ctxp[ctxn] = NULL;
  5222. /*
  5223. * If this context is a clone; unclone it so it can't get
  5224. * swapped to another process while we're removing all
  5225. * the events from it.
  5226. */
  5227. unclone_ctx(child_ctx);
  5228. update_context_time(child_ctx);
  5229. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  5230. /*
  5231. * Report the task dead after unscheduling the events so that we
  5232. * won't get any samples after PERF_RECORD_EXIT. We can however still
  5233. * get a few PERF_RECORD_READ events.
  5234. */
  5235. perf_event_task(child, child_ctx, 0);
  5236. /*
  5237. * We can recurse on the same lock type through:
  5238. *
  5239. * __perf_event_exit_task()
  5240. * sync_child_event()
  5241. * fput(parent_event->filp)
  5242. * perf_release()
  5243. * mutex_lock(&ctx->mutex)
  5244. *
  5245. * But since its the parent context it won't be the same instance.
  5246. */
  5247. mutex_lock(&child_ctx->mutex);
  5248. again:
  5249. list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
  5250. group_entry)
  5251. __perf_event_exit_task(child_event, child_ctx, child);
  5252. list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
  5253. group_entry)
  5254. __perf_event_exit_task(child_event, child_ctx, child);
  5255. /*
  5256. * If the last event was a group event, it will have appended all
  5257. * its siblings to the list, but we obtained 'tmp' before that which
  5258. * will still point to the list head terminating the iteration.
  5259. */
  5260. if (!list_empty(&child_ctx->pinned_groups) ||
  5261. !list_empty(&child_ctx->flexible_groups))
  5262. goto again;
  5263. mutex_unlock(&child_ctx->mutex);
  5264. put_ctx(child_ctx);
  5265. }
  5266. /*
  5267. * When a child task exits, feed back event values to parent events.
  5268. */
  5269. void perf_event_exit_task(struct task_struct *child)
  5270. {
  5271. struct perf_event *event, *tmp;
  5272. int ctxn;
  5273. mutex_lock(&child->perf_event_mutex);
  5274. list_for_each_entry_safe(event, tmp, &child->perf_event_list,
  5275. owner_entry) {
  5276. list_del_init(&event->owner_entry);
  5277. /*
  5278. * Ensure the list deletion is visible before we clear
  5279. * the owner, closes a race against perf_release() where
  5280. * we need to serialize on the owner->perf_event_mutex.
  5281. */
  5282. smp_wmb();
  5283. event->owner = NULL;
  5284. }
  5285. mutex_unlock(&child->perf_event_mutex);
  5286. for_each_task_context_nr(ctxn)
  5287. perf_event_exit_task_context(child, ctxn);
  5288. }
  5289. static void perf_free_event(struct perf_event *event,
  5290. struct perf_event_context *ctx)
  5291. {
  5292. struct perf_event *parent = event->parent;
  5293. if (WARN_ON_ONCE(!parent))
  5294. return;
  5295. mutex_lock(&parent->child_mutex);
  5296. list_del_init(&event->child_list);
  5297. mutex_unlock(&parent->child_mutex);
  5298. fput(parent->filp);
  5299. perf_group_detach(event);
  5300. list_del_event(event, ctx);
  5301. free_event(event);
  5302. }
  5303. /*
  5304. * free an unexposed, unused context as created by inheritance by
  5305. * perf_event_init_task below, used by fork() in case of fail.
  5306. */
  5307. void perf_event_free_task(struct task_struct *task)
  5308. {
  5309. struct perf_event_context *ctx;
  5310. struct perf_event *event, *tmp;
  5311. int ctxn;
  5312. for_each_task_context_nr(ctxn) {
  5313. ctx = task->perf_event_ctxp[ctxn];
  5314. if (!ctx)
  5315. continue;
  5316. mutex_lock(&ctx->mutex);
  5317. again:
  5318. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
  5319. group_entry)
  5320. perf_free_event(event, ctx);
  5321. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  5322. group_entry)
  5323. perf_free_event(event, ctx);
  5324. if (!list_empty(&ctx->pinned_groups) ||
  5325. !list_empty(&ctx->flexible_groups))
  5326. goto again;
  5327. mutex_unlock(&ctx->mutex);
  5328. put_ctx(ctx);
  5329. }
  5330. }
  5331. void perf_event_delayed_put(struct task_struct *task)
  5332. {
  5333. int ctxn;
  5334. for_each_task_context_nr(ctxn)
  5335. WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
  5336. }
  5337. /*
  5338. * inherit a event from parent task to child task:
  5339. */
  5340. static struct perf_event *
  5341. inherit_event(struct perf_event *parent_event,
  5342. struct task_struct *parent,
  5343. struct perf_event_context *parent_ctx,
  5344. struct task_struct *child,
  5345. struct perf_event *group_leader,
  5346. struct perf_event_context *child_ctx)
  5347. {
  5348. struct perf_event *child_event;
  5349. unsigned long flags;
  5350. /*
  5351. * Instead of creating recursive hierarchies of events,
  5352. * we link inherited events back to the original parent,
  5353. * which has a filp for sure, which we use as the reference
  5354. * count:
  5355. */
  5356. if (parent_event->parent)
  5357. parent_event = parent_event->parent;
  5358. child_event = perf_event_alloc(&parent_event->attr,
  5359. parent_event->cpu,
  5360. child,
  5361. group_leader, parent_event,
  5362. NULL, NULL);
  5363. if (IS_ERR(child_event))
  5364. return child_event;
  5365. get_ctx(child_ctx);
  5366. /*
  5367. * Make the child state follow the state of the parent event,
  5368. * not its attr.disabled bit. We hold the parent's mutex,
  5369. * so we won't race with perf_event_{en, dis}able_family.
  5370. */
  5371. if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
  5372. child_event->state = PERF_EVENT_STATE_INACTIVE;
  5373. else
  5374. child_event->state = PERF_EVENT_STATE_OFF;
  5375. if (parent_event->attr.freq) {
  5376. u64 sample_period = parent_event->hw.sample_period;
  5377. struct hw_perf_event *hwc = &child_event->hw;
  5378. hwc->sample_period = sample_period;
  5379. hwc->last_period = sample_period;
  5380. local64_set(&hwc->period_left, sample_period);
  5381. }
  5382. child_event->ctx = child_ctx;
  5383. child_event->overflow_handler = parent_event->overflow_handler;
  5384. child_event->overflow_handler_context
  5385. = parent_event->overflow_handler_context;
  5386. /*
  5387. * Precalculate sample_data sizes
  5388. */
  5389. perf_event__header_size(child_event);
  5390. perf_event__id_header_size(child_event);
  5391. /*
  5392. * Link it up in the child's context:
  5393. */
  5394. raw_spin_lock_irqsave(&child_ctx->lock, flags);
  5395. add_event_to_ctx(child_event, child_ctx);
  5396. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  5397. /*
  5398. * Get a reference to the parent filp - we will fput it
  5399. * when the child event exits. This is safe to do because
  5400. * we are in the parent and we know that the filp still
  5401. * exists and has a nonzero count:
  5402. */
  5403. atomic_long_inc(&parent_event->filp->f_count);
  5404. /*
  5405. * Link this into the parent event's child list
  5406. */
  5407. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  5408. mutex_lock(&parent_event->child_mutex);
  5409. list_add_tail(&child_event->child_list, &parent_event->child_list);
  5410. mutex_unlock(&parent_event->child_mutex);
  5411. return child_event;
  5412. }
  5413. static int inherit_group(struct perf_event *parent_event,
  5414. struct task_struct *parent,
  5415. struct perf_event_context *parent_ctx,
  5416. struct task_struct *child,
  5417. struct perf_event_context *child_ctx)
  5418. {
  5419. struct perf_event *leader;
  5420. struct perf_event *sub;
  5421. struct perf_event *child_ctr;
  5422. leader = inherit_event(parent_event, parent, parent_ctx,
  5423. child, NULL, child_ctx);
  5424. if (IS_ERR(leader))
  5425. return PTR_ERR(leader);
  5426. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  5427. child_ctr = inherit_event(sub, parent, parent_ctx,
  5428. child, leader, child_ctx);
  5429. if (IS_ERR(child_ctr))
  5430. return PTR_ERR(child_ctr);
  5431. }
  5432. return 0;
  5433. }
  5434. static int
  5435. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  5436. struct perf_event_context *parent_ctx,
  5437. struct task_struct *child, int ctxn,
  5438. int *inherited_all)
  5439. {
  5440. int ret;
  5441. struct perf_event_context *child_ctx;
  5442. if (!event->attr.inherit) {
  5443. *inherited_all = 0;
  5444. return 0;
  5445. }
  5446. child_ctx = child->perf_event_ctxp[ctxn];
  5447. if (!child_ctx) {
  5448. /*
  5449. * This is executed from the parent task context, so
  5450. * inherit events that have been marked for cloning.
  5451. * First allocate and initialize a context for the
  5452. * child.
  5453. */
  5454. child_ctx = alloc_perf_context(event->pmu, child);
  5455. if (!child_ctx)
  5456. return -ENOMEM;
  5457. child->perf_event_ctxp[ctxn] = child_ctx;
  5458. }
  5459. ret = inherit_group(event, parent, parent_ctx,
  5460. child, child_ctx);
  5461. if (ret)
  5462. *inherited_all = 0;
  5463. return ret;
  5464. }
  5465. /*
  5466. * Initialize the perf_event context in task_struct
  5467. */
  5468. int perf_event_init_context(struct task_struct *child, int ctxn)
  5469. {
  5470. struct perf_event_context *child_ctx, *parent_ctx;
  5471. struct perf_event_context *cloned_ctx;
  5472. struct perf_event *event;
  5473. struct task_struct *parent = current;
  5474. int inherited_all = 1;
  5475. unsigned long flags;
  5476. int ret = 0;
  5477. if (likely(!parent->perf_event_ctxp[ctxn]))
  5478. return 0;
  5479. /*
  5480. * If the parent's context is a clone, pin it so it won't get
  5481. * swapped under us.
  5482. */
  5483. parent_ctx = perf_pin_task_context(parent, ctxn);
  5484. /*
  5485. * No need to check if parent_ctx != NULL here; since we saw
  5486. * it non-NULL earlier, the only reason for it to become NULL
  5487. * is if we exit, and since we're currently in the middle of
  5488. * a fork we can't be exiting at the same time.
  5489. */
  5490. /*
  5491. * Lock the parent list. No need to lock the child - not PID
  5492. * hashed yet and not running, so nobody can access it.
  5493. */
  5494. mutex_lock(&parent_ctx->mutex);
  5495. /*
  5496. * We dont have to disable NMIs - we are only looking at
  5497. * the list, not manipulating it:
  5498. */
  5499. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  5500. ret = inherit_task_group(event, parent, parent_ctx,
  5501. child, ctxn, &inherited_all);
  5502. if (ret)
  5503. break;
  5504. }
  5505. /*
  5506. * We can't hold ctx->lock when iterating the ->flexible_group list due
  5507. * to allocations, but we need to prevent rotation because
  5508. * rotate_ctx() will change the list from interrupt context.
  5509. */
  5510. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  5511. parent_ctx->rotate_disable = 1;
  5512. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  5513. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  5514. ret = inherit_task_group(event, parent, parent_ctx,
  5515. child, ctxn, &inherited_all);
  5516. if (ret)
  5517. break;
  5518. }
  5519. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  5520. parent_ctx->rotate_disable = 0;
  5521. child_ctx = child->perf_event_ctxp[ctxn];
  5522. if (child_ctx && inherited_all) {
  5523. /*
  5524. * Mark the child context as a clone of the parent
  5525. * context, or of whatever the parent is a clone of.
  5526. *
  5527. * Note that if the parent is a clone, the holding of
  5528. * parent_ctx->lock avoids it from being uncloned.
  5529. */
  5530. cloned_ctx = parent_ctx->parent_ctx;
  5531. if (cloned_ctx) {
  5532. child_ctx->parent_ctx = cloned_ctx;
  5533. child_ctx->parent_gen = parent_ctx->parent_gen;
  5534. } else {
  5535. child_ctx->parent_ctx = parent_ctx;
  5536. child_ctx->parent_gen = parent_ctx->generation;
  5537. }
  5538. get_ctx(child_ctx->parent_ctx);
  5539. }
  5540. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  5541. mutex_unlock(&parent_ctx->mutex);
  5542. perf_unpin_context(parent_ctx);
  5543. put_ctx(parent_ctx);
  5544. return ret;
  5545. }
  5546. /*
  5547. * Initialize the perf_event context in task_struct
  5548. */
  5549. int perf_event_init_task(struct task_struct *child)
  5550. {
  5551. int ctxn, ret;
  5552. memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
  5553. mutex_init(&child->perf_event_mutex);
  5554. INIT_LIST_HEAD(&child->perf_event_list);
  5555. for_each_task_context_nr(ctxn) {
  5556. ret = perf_event_init_context(child, ctxn);
  5557. if (ret)
  5558. return ret;
  5559. }
  5560. return 0;
  5561. }
  5562. static void __init perf_event_init_all_cpus(void)
  5563. {
  5564. struct swevent_htable *swhash;
  5565. int cpu;
  5566. for_each_possible_cpu(cpu) {
  5567. swhash = &per_cpu(swevent_htable, cpu);
  5568. mutex_init(&swhash->hlist_mutex);
  5569. INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
  5570. }
  5571. }
  5572. static void __cpuinit perf_event_init_cpu(int cpu)
  5573. {
  5574. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5575. mutex_lock(&swhash->hlist_mutex);
  5576. if (swhash->hlist_refcount > 0 && !swhash->swevent_hlist) {
  5577. struct swevent_hlist *hlist;
  5578. hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
  5579. WARN_ON(!hlist);
  5580. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  5581. }
  5582. mutex_unlock(&swhash->hlist_mutex);
  5583. }
  5584. #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
  5585. static void perf_pmu_rotate_stop(struct pmu *pmu)
  5586. {
  5587. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  5588. WARN_ON(!irqs_disabled());
  5589. list_del_init(&cpuctx->rotation_list);
  5590. }
  5591. static void __perf_event_exit_context(void *__info)
  5592. {
  5593. struct perf_event_context *ctx = __info;
  5594. struct perf_event *event, *tmp;
  5595. perf_pmu_rotate_stop(ctx->pmu);
  5596. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
  5597. __perf_remove_from_context(event);
  5598. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
  5599. __perf_remove_from_context(event);
  5600. }
  5601. static void perf_event_exit_cpu_context(int cpu)
  5602. {
  5603. struct perf_event_context *ctx;
  5604. struct pmu *pmu;
  5605. int idx;
  5606. idx = srcu_read_lock(&pmus_srcu);
  5607. list_for_each_entry_rcu(pmu, &pmus, entry) {
  5608. ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
  5609. mutex_lock(&ctx->mutex);
  5610. smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
  5611. mutex_unlock(&ctx->mutex);
  5612. }
  5613. srcu_read_unlock(&pmus_srcu, idx);
  5614. }
  5615. static void perf_event_exit_cpu(int cpu)
  5616. {
  5617. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5618. mutex_lock(&swhash->hlist_mutex);
  5619. swevent_hlist_release(swhash);
  5620. mutex_unlock(&swhash->hlist_mutex);
  5621. perf_event_exit_cpu_context(cpu);
  5622. }
  5623. #else
  5624. static inline void perf_event_exit_cpu(int cpu) { }
  5625. #endif
  5626. static int
  5627. perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
  5628. {
  5629. int cpu;
  5630. for_each_online_cpu(cpu)
  5631. perf_event_exit_cpu(cpu);
  5632. return NOTIFY_OK;
  5633. }
  5634. /*
  5635. * Run the perf reboot notifier at the very last possible moment so that
  5636. * the generic watchdog code runs as long as possible.
  5637. */
  5638. static struct notifier_block perf_reboot_notifier = {
  5639. .notifier_call = perf_reboot,
  5640. .priority = INT_MIN,
  5641. };
  5642. static int __cpuinit
  5643. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  5644. {
  5645. unsigned int cpu = (long)hcpu;
  5646. /*
  5647. * Ignore suspend/resume action, the perf_pm_notifier will
  5648. * take care of that.
  5649. */
  5650. if (action & CPU_TASKS_FROZEN)
  5651. return NOTIFY_OK;
  5652. switch (action) {
  5653. case CPU_UP_PREPARE:
  5654. case CPU_DOWN_FAILED:
  5655. perf_event_init_cpu(cpu);
  5656. break;
  5657. case CPU_UP_CANCELED:
  5658. case CPU_DOWN_PREPARE:
  5659. perf_event_exit_cpu(cpu);
  5660. break;
  5661. default:
  5662. break;
  5663. }
  5664. return NOTIFY_OK;
  5665. }
  5666. static void perf_pm_resume_cpu(void *unused)
  5667. {
  5668. struct perf_cpu_context *cpuctx;
  5669. struct perf_event_context *ctx;
  5670. struct pmu *pmu;
  5671. int idx;
  5672. idx = srcu_read_lock(&pmus_srcu);
  5673. list_for_each_entry_rcu(pmu, &pmus, entry) {
  5674. cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  5675. ctx = cpuctx->task_ctx;
  5676. perf_ctx_lock(cpuctx, ctx);
  5677. perf_pmu_disable(cpuctx->ctx.pmu);
  5678. cpu_ctx_sched_out(cpuctx, EVENT_ALL);
  5679. if (ctx)
  5680. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  5681. perf_pmu_enable(cpuctx->ctx.pmu);
  5682. perf_ctx_unlock(cpuctx, ctx);
  5683. }
  5684. srcu_read_unlock(&pmus_srcu, idx);
  5685. }
  5686. static void perf_pm_suspend_cpu(void *unused)
  5687. {
  5688. struct perf_cpu_context *cpuctx;
  5689. struct perf_event_context *ctx;
  5690. struct pmu *pmu;
  5691. int idx;
  5692. idx = srcu_read_lock(&pmus_srcu);
  5693. list_for_each_entry_rcu(pmu, &pmus, entry) {
  5694. cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  5695. ctx = cpuctx->task_ctx;
  5696. perf_ctx_lock(cpuctx, ctx);
  5697. perf_pmu_disable(cpuctx->ctx.pmu);
  5698. perf_event_sched_in(cpuctx, ctx, current);
  5699. perf_pmu_enable(cpuctx->ctx.pmu);
  5700. perf_ctx_unlock(cpuctx, ctx);
  5701. }
  5702. srcu_read_unlock(&pmus_srcu, idx);
  5703. }
  5704. static int perf_resume(void)
  5705. {
  5706. get_online_cpus();
  5707. smp_call_function(perf_pm_resume_cpu, NULL, 1);
  5708. put_online_cpus();
  5709. return NOTIFY_OK;
  5710. }
  5711. static int perf_suspend(void)
  5712. {
  5713. get_online_cpus();
  5714. smp_call_function(perf_pm_suspend_cpu, NULL, 1);
  5715. put_online_cpus();
  5716. return NOTIFY_OK;
  5717. }
  5718. static int perf_pm(struct notifier_block *self, unsigned long action, void *ptr)
  5719. {
  5720. switch (action) {
  5721. case PM_POST_HIBERNATION:
  5722. case PM_POST_SUSPEND:
  5723. return perf_resume();
  5724. case PM_HIBERNATION_PREPARE:
  5725. case PM_SUSPEND_PREPARE:
  5726. return perf_suspend();
  5727. default:
  5728. return NOTIFY_DONE;
  5729. }
  5730. }
  5731. static struct notifier_block perf_pm_notifier = {
  5732. .notifier_call = perf_pm,
  5733. };
  5734. void __init perf_event_init(void)
  5735. {
  5736. int ret;
  5737. idr_init(&pmu_idr);
  5738. perf_event_init_all_cpus();
  5739. init_srcu_struct(&pmus_srcu);
  5740. perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
  5741. perf_pmu_register(&perf_cpu_clock, NULL, -1);
  5742. perf_pmu_register(&perf_task_clock, NULL, -1);
  5743. perf_tp_register();
  5744. perf_cpu_notifier(perf_cpu_notify);
  5745. register_reboot_notifier(&perf_reboot_notifier);
  5746. register_pm_notifier(&perf_pm_notifier);
  5747. ret = init_hw_breakpoint();
  5748. WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
  5749. }
  5750. static int __init perf_event_sysfs_init(void)
  5751. {
  5752. struct pmu *pmu;
  5753. int ret;
  5754. mutex_lock(&pmus_lock);
  5755. ret = bus_register(&pmu_bus);
  5756. if (ret)
  5757. goto unlock;
  5758. list_for_each_entry(pmu, &pmus, entry) {
  5759. if (!pmu->name || pmu->type < 0)
  5760. continue;
  5761. ret = pmu_dev_alloc(pmu);
  5762. WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
  5763. }
  5764. pmu_bus_running = 1;
  5765. ret = 0;
  5766. unlock:
  5767. mutex_unlock(&pmus_lock);
  5768. return ret;
  5769. }
  5770. device_initcall(perf_event_sysfs_init);
  5771. #ifdef CONFIG_CGROUP_PERF
  5772. static struct cgroup_subsys_state *perf_cgroup_create(
  5773. struct cgroup_subsys *ss, struct cgroup *cont)
  5774. {
  5775. struct perf_cgroup *jc;
  5776. jc = kzalloc(sizeof(*jc), GFP_KERNEL);
  5777. if (!jc)
  5778. return ERR_PTR(-ENOMEM);
  5779. jc->info = alloc_percpu(struct perf_cgroup_info);
  5780. if (!jc->info) {
  5781. kfree(jc);
  5782. return ERR_PTR(-ENOMEM);
  5783. }
  5784. return &jc->css;
  5785. }
  5786. static void perf_cgroup_destroy(struct cgroup_subsys *ss,
  5787. struct cgroup *cont)
  5788. {
  5789. struct perf_cgroup *jc;
  5790. jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
  5791. struct perf_cgroup, css);
  5792. free_percpu(jc->info);
  5793. kfree(jc);
  5794. }
  5795. static int __perf_cgroup_move(void *info)
  5796. {
  5797. struct task_struct *task = info;
  5798. perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
  5799. return 0;
  5800. }
  5801. static void
  5802. perf_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *task)
  5803. {
  5804. task_function_call(task, __perf_cgroup_move, task);
  5805. }
  5806. static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
  5807. struct cgroup *old_cgrp, struct task_struct *task)
  5808. {
  5809. /*
  5810. * cgroup_exit() is called in the copy_process() failure path.
  5811. * Ignore this case since the task hasn't ran yet, this avoids
  5812. * trying to poke a half freed task state from generic code.
  5813. */
  5814. if (!(task->flags & PF_EXITING))
  5815. return;
  5816. perf_cgroup_attach_task(cgrp, task);
  5817. }
  5818. struct cgroup_subsys perf_subsys = {
  5819. .name = "perf_event",
  5820. .subsys_id = perf_subsys_id,
  5821. .create = perf_cgroup_create,
  5822. .destroy = perf_cgroup_destroy,
  5823. .exit = perf_cgroup_exit,
  5824. .attach_task = perf_cgroup_attach_task,
  5825. };
  5826. #endif /* CONFIG_CGROUP_PERF */