core.c 174 KB

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