core.c 173 KB

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