core.c 194 KB

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