core.c 218 KB

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