core.c 186 KB

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