core.c 187 KB

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