core.c 216 KB

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