core.c 179 KB

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