core.c 170 KB

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