core.c 195 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372
  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. struct perf_event *event = file->private_data;
  2786. /*
  2787. * Event can be in state OFF because of a constraint check.
  2788. * Change to ACTIVE so that it gets cleaned up correctly.
  2789. */
  2790. if ((event->state == PERF_EVENT_STATE_OFF) &&
  2791. event->attr.constraint_duplicate)
  2792. event->state = PERF_EVENT_STATE_ACTIVE;
  2793. put_event(file->private_data);
  2794. return 0;
  2795. }
  2796. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  2797. {
  2798. struct perf_event *child;
  2799. u64 total = 0;
  2800. *enabled = 0;
  2801. *running = 0;
  2802. mutex_lock(&event->child_mutex);
  2803. total += perf_event_read(event);
  2804. *enabled += event->total_time_enabled +
  2805. atomic64_read(&event->child_total_time_enabled);
  2806. *running += event->total_time_running +
  2807. atomic64_read(&event->child_total_time_running);
  2808. list_for_each_entry(child, &event->child_list, child_list) {
  2809. total += perf_event_read(child);
  2810. *enabled += child->total_time_enabled;
  2811. *running += child->total_time_running;
  2812. }
  2813. mutex_unlock(&event->child_mutex);
  2814. return total;
  2815. }
  2816. EXPORT_SYMBOL_GPL(perf_event_read_value);
  2817. static int perf_event_read_group(struct perf_event *event,
  2818. u64 read_format, char __user *buf)
  2819. {
  2820. struct perf_event *leader = event->group_leader, *sub;
  2821. int n = 0, size = 0, ret;
  2822. u64 count, enabled, running;
  2823. u64 values[5];
  2824. lockdep_assert_held(&ctx->mutex);
  2825. count = perf_event_read_value(leader, &enabled, &running);
  2826. values[n++] = 1 + leader->nr_siblings;
  2827. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2828. values[n++] = enabled;
  2829. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2830. values[n++] = running;
  2831. values[n++] = count;
  2832. if (read_format & PERF_FORMAT_ID)
  2833. values[n++] = primary_event_id(leader);
  2834. size = n * sizeof(u64);
  2835. if (copy_to_user(buf, values, size))
  2836. return -EFAULT;
  2837. ret = size;
  2838. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2839. n = 0;
  2840. values[n++] = perf_event_read_value(sub, &enabled, &running);
  2841. if (read_format & PERF_FORMAT_ID)
  2842. values[n++] = primary_event_id(sub);
  2843. size = n * sizeof(u64);
  2844. if (copy_to_user(buf + ret, values, size)) {
  2845. return -EFAULT;
  2846. }
  2847. ret += size;
  2848. }
  2849. return ret;
  2850. }
  2851. static int perf_event_read_one(struct perf_event *event,
  2852. u64 read_format, char __user *buf)
  2853. {
  2854. u64 enabled, running;
  2855. u64 values[4];
  2856. int n = 0;
  2857. values[n++] = perf_event_read_value(event, &enabled, &running);
  2858. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2859. values[n++] = enabled;
  2860. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2861. values[n++] = running;
  2862. if (read_format & PERF_FORMAT_ID)
  2863. values[n++] = primary_event_id(event);
  2864. if (copy_to_user(buf, values, n * sizeof(u64)))
  2865. return -EFAULT;
  2866. return n * sizeof(u64);
  2867. }
  2868. /*
  2869. * Read the performance event - simple non blocking version for now
  2870. */
  2871. static ssize_t
  2872. perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
  2873. {
  2874. u64 read_format = event->attr.read_format;
  2875. int ret;
  2876. /*
  2877. * Return end-of-file for a read on a event that is in
  2878. * error state (i.e. because it was pinned but it couldn't be
  2879. * scheduled on to the CPU at some point).
  2880. */
  2881. if (event->state == PERF_EVENT_STATE_ERROR)
  2882. return 0;
  2883. if (count < event->read_size)
  2884. return -ENOSPC;
  2885. WARN_ON_ONCE(event->ctx->parent_ctx);
  2886. if (read_format & PERF_FORMAT_GROUP)
  2887. ret = perf_event_read_group(event, read_format, buf);
  2888. else
  2889. ret = perf_event_read_one(event, read_format, buf);
  2890. return ret;
  2891. }
  2892. static ssize_t
  2893. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  2894. {
  2895. struct perf_event *event = file->private_data;
  2896. struct perf_event_context *ctx;
  2897. int ret;
  2898. ctx = perf_event_ctx_lock(event);
  2899. ret = perf_read_hw(event, buf, count);
  2900. perf_event_ctx_unlock(event, ctx);
  2901. return ret;
  2902. }
  2903. static unsigned int perf_poll(struct file *file, poll_table *wait)
  2904. {
  2905. struct perf_event *event = file->private_data;
  2906. struct ring_buffer *rb;
  2907. unsigned int events = POLL_HUP;
  2908. /*
  2909. * Pin the event->rb by taking event->mmap_mutex; otherwise
  2910. * perf_event_set_output() can swizzle our rb and make us miss wakeups.
  2911. */
  2912. mutex_lock(&event->mmap_mutex);
  2913. rb = event->rb;
  2914. if (rb)
  2915. events = atomic_xchg(&rb->poll, 0);
  2916. mutex_unlock(&event->mmap_mutex);
  2917. poll_wait(file, &event->waitq, wait);
  2918. return events;
  2919. }
  2920. static void _perf_event_reset(struct perf_event *event)
  2921. {
  2922. (void)perf_event_read(event);
  2923. local64_set(&event->count, 0);
  2924. perf_event_update_userpage(event);
  2925. }
  2926. /*
  2927. * Holding the top-level event's child_mutex means that any
  2928. * descendant process that has inherited this event will block
  2929. * in sync_child_event if it goes to exit, thus satisfying the
  2930. * task existence requirements of perf_event_enable/disable.
  2931. */
  2932. static void perf_event_for_each_child(struct perf_event *event,
  2933. void (*func)(struct perf_event *))
  2934. {
  2935. struct perf_event *child;
  2936. WARN_ON_ONCE(event->ctx->parent_ctx);
  2937. mutex_lock(&event->child_mutex);
  2938. func(event);
  2939. list_for_each_entry(child, &event->child_list, child_list)
  2940. func(child);
  2941. mutex_unlock(&event->child_mutex);
  2942. }
  2943. static void perf_event_for_each(struct perf_event *event,
  2944. void (*func)(struct perf_event *))
  2945. {
  2946. struct perf_event *sibling;
  2947. lockdep_assert_held(&ctx->mutex);
  2948. event = event->group_leader;
  2949. perf_event_for_each_child(event, func);
  2950. func(event);
  2951. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  2952. perf_event_for_each_child(sibling, func);
  2953. }
  2954. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  2955. {
  2956. struct perf_event_context *ctx = event->ctx;
  2957. int ret = 0;
  2958. u64 value;
  2959. if (!is_sampling_event(event))
  2960. return -EINVAL;
  2961. if (copy_from_user(&value, arg, sizeof(value)))
  2962. return -EFAULT;
  2963. if (!value)
  2964. return -EINVAL;
  2965. raw_spin_lock_irq(&ctx->lock);
  2966. if (event->attr.freq) {
  2967. if (value > sysctl_perf_event_sample_rate) {
  2968. ret = -EINVAL;
  2969. goto unlock;
  2970. }
  2971. event->attr.sample_freq = value;
  2972. } else {
  2973. event->attr.sample_period = value;
  2974. event->hw.sample_period = value;
  2975. }
  2976. unlock:
  2977. raw_spin_unlock_irq(&ctx->lock);
  2978. return ret;
  2979. }
  2980. static const struct file_operations perf_fops;
  2981. static struct file *perf_fget_light(int fd, int *fput_needed)
  2982. {
  2983. struct file *file;
  2984. file = fget_light(fd, fput_needed);
  2985. if (!file)
  2986. return ERR_PTR(-EBADF);
  2987. if (file->f_op != &perf_fops) {
  2988. fput_light(file, *fput_needed);
  2989. *fput_needed = 0;
  2990. return ERR_PTR(-EBADF);
  2991. }
  2992. return file;
  2993. }
  2994. static int perf_event_set_output(struct perf_event *event,
  2995. struct perf_event *output_event);
  2996. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  2997. static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
  2998. {
  2999. void (*func)(struct perf_event *);
  3000. u32 flags = arg;
  3001. switch (cmd) {
  3002. case PERF_EVENT_IOC_ENABLE:
  3003. func = _perf_event_enable;
  3004. break;
  3005. case PERF_EVENT_IOC_DISABLE:
  3006. func = _perf_event_disable;
  3007. break;
  3008. case PERF_EVENT_IOC_RESET:
  3009. func = _perf_event_reset;
  3010. break;
  3011. case PERF_EVENT_IOC_REFRESH:
  3012. return _perf_event_refresh(event, arg);
  3013. case PERF_EVENT_IOC_PERIOD:
  3014. return perf_event_period(event, (u64 __user *)arg);
  3015. case PERF_EVENT_IOC_SET_OUTPUT:
  3016. {
  3017. struct file *output_file = NULL;
  3018. struct perf_event *output_event = NULL;
  3019. int fput_needed = 0;
  3020. int ret;
  3021. if (arg != -1) {
  3022. output_file = perf_fget_light(arg, &fput_needed);
  3023. if (IS_ERR(output_file))
  3024. return PTR_ERR(output_file);
  3025. output_event = output_file->private_data;
  3026. }
  3027. ret = perf_event_set_output(event, output_event);
  3028. if (output_event)
  3029. fput_light(output_file, fput_needed);
  3030. return ret;
  3031. }
  3032. case PERF_EVENT_IOC_SET_FILTER:
  3033. return perf_event_set_filter(event, (void __user *)arg);
  3034. default:
  3035. return -ENOTTY;
  3036. }
  3037. if (flags & PERF_IOC_FLAG_GROUP)
  3038. perf_event_for_each(event, func);
  3039. else
  3040. perf_event_for_each_child(event, func);
  3041. return 0;
  3042. }
  3043. #ifdef CONFIG_COMPAT
  3044. static long perf_compat_ioctl(struct file *file, unsigned int cmd,
  3045. unsigned long arg)
  3046. {
  3047. switch (_IOC_NR(cmd)) {
  3048. case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
  3049. /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
  3050. if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
  3051. cmd &= ~IOCSIZE_MASK;
  3052. cmd |= sizeof(void *) << IOCSIZE_SHIFT;
  3053. }
  3054. break;
  3055. }
  3056. return perf_ioctl(file, cmd, arg);
  3057. }
  3058. #else
  3059. # define perf_compat_ioctl NULL
  3060. #endif
  3061. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  3062. {
  3063. struct perf_event *event = file->private_data;
  3064. struct perf_event_context *ctx;
  3065. long ret;
  3066. ctx = perf_event_ctx_lock(event);
  3067. ret = _perf_ioctl(event, cmd, arg);
  3068. perf_event_ctx_unlock(event, ctx);
  3069. return ret;
  3070. }
  3071. int perf_event_task_enable(void)
  3072. {
  3073. struct perf_event_context *ctx;
  3074. struct perf_event *event;
  3075. mutex_lock(&current->perf_event_mutex);
  3076. list_for_each_entry(event, &current->perf_event_list, owner_entry) {
  3077. ctx = perf_event_ctx_lock(event);
  3078. perf_event_for_each_child(event, _perf_event_enable);
  3079. perf_event_ctx_unlock(event, ctx);
  3080. }
  3081. mutex_unlock(&current->perf_event_mutex);
  3082. return 0;
  3083. }
  3084. int perf_event_task_disable(void)
  3085. {
  3086. struct perf_event_context *ctx;
  3087. struct perf_event *event;
  3088. mutex_lock(&current->perf_event_mutex);
  3089. list_for_each_entry(event, &current->perf_event_list, owner_entry) {
  3090. ctx = perf_event_ctx_lock(event);
  3091. perf_event_for_each_child(event, _perf_event_disable);
  3092. perf_event_ctx_unlock(event, ctx);
  3093. }
  3094. mutex_unlock(&current->perf_event_mutex);
  3095. return 0;
  3096. }
  3097. static int perf_event_index(struct perf_event *event)
  3098. {
  3099. if (event->hw.state & PERF_HES_STOPPED)
  3100. return 0;
  3101. if (event->state != PERF_EVENT_STATE_ACTIVE)
  3102. return 0;
  3103. return event->pmu->event_idx(event);
  3104. }
  3105. static void calc_timer_values(struct perf_event *event,
  3106. u64 *now,
  3107. u64 *enabled,
  3108. u64 *running)
  3109. {
  3110. u64 ctx_time;
  3111. *now = perf_clock();
  3112. ctx_time = event->shadow_ctx_time + *now;
  3113. *enabled = ctx_time - event->tstamp_enabled;
  3114. *running = ctx_time - event->tstamp_running;
  3115. }
  3116. static void perf_event_init_userpage(struct perf_event *event)
  3117. {
  3118. struct perf_event_mmap_page *userpg;
  3119. struct ring_buffer *rb;
  3120. rcu_read_lock();
  3121. rb = rcu_dereference(event->rb);
  3122. if (!rb)
  3123. goto unlock;
  3124. userpg = rb->user_page;
  3125. /* Allow new userspace to detect that bit 0 is deprecated */
  3126. userpg->cap_bit0_is_deprecated = 1;
  3127. userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
  3128. userpg->data_offset = PAGE_SIZE;
  3129. userpg->data_size = perf_data_size(rb);
  3130. unlock:
  3131. rcu_read_unlock();
  3132. }
  3133. void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
  3134. {
  3135. }
  3136. /*
  3137. * Callers need to ensure there can be no nesting of this function, otherwise
  3138. * the seqlock logic goes bad. We can not serialize this because the arch
  3139. * code calls this from NMI context.
  3140. */
  3141. void perf_event_update_userpage(struct perf_event *event)
  3142. {
  3143. struct perf_event_mmap_page *userpg;
  3144. struct ring_buffer *rb;
  3145. u64 enabled, running, now;
  3146. rcu_read_lock();
  3147. /*
  3148. * compute total_time_enabled, total_time_running
  3149. * based on snapshot values taken when the event
  3150. * was last scheduled in.
  3151. *
  3152. * we cannot simply called update_context_time()
  3153. * because of locking issue as we can be called in
  3154. * NMI context
  3155. */
  3156. calc_timer_values(event, &now, &enabled, &running);
  3157. rb = rcu_dereference(event->rb);
  3158. if (!rb)
  3159. goto unlock;
  3160. userpg = rb->user_page;
  3161. /*
  3162. * Disable preemption so as to not let the corresponding user-space
  3163. * spin too long if we get preempted.
  3164. */
  3165. preempt_disable();
  3166. ++userpg->lock;
  3167. barrier();
  3168. userpg->index = perf_event_index(event);
  3169. userpg->offset = perf_event_count(event);
  3170. if (userpg->index)
  3171. userpg->offset -= local64_read(&event->hw.prev_count);
  3172. userpg->time_enabled = enabled +
  3173. atomic64_read(&event->child_total_time_enabled);
  3174. userpg->time_running = running +
  3175. atomic64_read(&event->child_total_time_running);
  3176. arch_perf_update_userpage(userpg, now);
  3177. barrier();
  3178. ++userpg->lock;
  3179. preempt_enable();
  3180. unlock:
  3181. rcu_read_unlock();
  3182. }
  3183. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  3184. {
  3185. struct perf_event *event = vma->vm_file->private_data;
  3186. struct ring_buffer *rb;
  3187. int ret = VM_FAULT_SIGBUS;
  3188. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  3189. if (vmf->pgoff == 0)
  3190. ret = 0;
  3191. return ret;
  3192. }
  3193. rcu_read_lock();
  3194. rb = rcu_dereference(event->rb);
  3195. if (!rb)
  3196. goto unlock;
  3197. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  3198. goto unlock;
  3199. vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
  3200. if (!vmf->page)
  3201. goto unlock;
  3202. get_page(vmf->page);
  3203. vmf->page->mapping = vma->vm_file->f_mapping;
  3204. vmf->page->index = vmf->pgoff;
  3205. ret = 0;
  3206. unlock:
  3207. rcu_read_unlock();
  3208. return ret;
  3209. }
  3210. static void ring_buffer_attach(struct perf_event *event,
  3211. struct ring_buffer *rb)
  3212. {
  3213. struct ring_buffer *old_rb = NULL;
  3214. unsigned long flags;
  3215. if (event->rb) {
  3216. /*
  3217. * Should be impossible, we set this when removing
  3218. * event->rb_entry and wait/clear when adding event->rb_entry.
  3219. */
  3220. WARN_ON_ONCE(event->rcu_pending);
  3221. old_rb = event->rb;
  3222. spin_lock_irqsave(&old_rb->event_lock, flags);
  3223. list_del_rcu(&event->rb_entry);
  3224. spin_unlock_irqrestore(&old_rb->event_lock, flags);
  3225. event->rcu_batches = get_state_synchronize_rcu();
  3226. event->rcu_pending = 1;
  3227. }
  3228. if (rb) {
  3229. if (event->rcu_pending) {
  3230. cond_synchronize_rcu(event->rcu_batches);
  3231. event->rcu_pending = 0;
  3232. }
  3233. spin_lock_irqsave(&rb->event_lock, flags);
  3234. list_add_rcu(&event->rb_entry, &rb->event_list);
  3235. spin_unlock_irqrestore(&rb->event_lock, flags);
  3236. }
  3237. rcu_assign_pointer(event->rb, rb);
  3238. if (old_rb) {
  3239. ring_buffer_put(old_rb);
  3240. /*
  3241. * Since we detached before setting the new rb, so that we
  3242. * could attach the new rb, we could have missed a wakeup.
  3243. * Provide it now.
  3244. */
  3245. wake_up_all(&event->waitq);
  3246. }
  3247. }
  3248. static void ring_buffer_wakeup(struct perf_event *event)
  3249. {
  3250. struct ring_buffer *rb;
  3251. rcu_read_lock();
  3252. rb = rcu_dereference(event->rb);
  3253. if (rb) {
  3254. list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
  3255. wake_up_all(&event->waitq);
  3256. }
  3257. rcu_read_unlock();
  3258. }
  3259. static void rb_free_rcu(struct rcu_head *rcu_head)
  3260. {
  3261. struct ring_buffer *rb;
  3262. rb = container_of(rcu_head, struct ring_buffer, rcu_head);
  3263. rb_free(rb);
  3264. }
  3265. static struct ring_buffer *ring_buffer_get(struct perf_event *event)
  3266. {
  3267. struct ring_buffer *rb;
  3268. rcu_read_lock();
  3269. rb = rcu_dereference(event->rb);
  3270. if (rb) {
  3271. if (!atomic_inc_not_zero(&rb->refcount))
  3272. rb = NULL;
  3273. }
  3274. rcu_read_unlock();
  3275. return rb;
  3276. }
  3277. static void ring_buffer_put(struct ring_buffer *rb)
  3278. {
  3279. if (!atomic_dec_and_test(&rb->refcount))
  3280. return;
  3281. WARN_ON_ONCE(!list_empty(&rb->event_list));
  3282. call_rcu(&rb->rcu_head, rb_free_rcu);
  3283. }
  3284. static void perf_mmap_open(struct vm_area_struct *vma)
  3285. {
  3286. struct perf_event *event = vma->vm_file->private_data;
  3287. atomic_inc(&event->mmap_count);
  3288. atomic_inc(&event->rb->mmap_count);
  3289. if (event->pmu->event_mapped)
  3290. event->pmu->event_mapped(event);
  3291. if (vma->vm_pgoff)
  3292. atomic_inc(&event->rb->aux_mmap_count);
  3293. }
  3294. static void perf_pmu_output_stop(struct perf_event *event);
  3295. /*
  3296. * A buffer can be mmap()ed multiple times; either directly through the same
  3297. * event, or through other events by use of perf_event_set_output().
  3298. *
  3299. * In order to undo the VM accounting done by perf_mmap() we need to destroy
  3300. * the buffer here, where we still have a VM context. This means we need
  3301. * to detach all events redirecting to us.
  3302. */
  3303. static void perf_mmap_close(struct vm_area_struct *vma)
  3304. {
  3305. struct perf_event *event = vma->vm_file->private_data;
  3306. struct ring_buffer *rb = ring_buffer_get(event);
  3307. struct user_struct *mmap_user = rb->mmap_user;
  3308. int mmap_locked = rb->mmap_locked;
  3309. unsigned long size = perf_data_size(rb);
  3310. if (event->pmu->event_unmapped)
  3311. event->pmu->event_unmapped(event);
  3312. /*
  3313. * rb->aux_mmap_count will always drop before rb->mmap_count and
  3314. * event->mmap_count, so it is ok to use event->mmap_mutex to
  3315. * serialize with perf_mmap here.
  3316. */
  3317. if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
  3318. atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
  3319. /*
  3320. * Stop all AUX events that are writing to this buffer,
  3321. * so that we can free its AUX pages and corresponding PMU
  3322. * data. Note that after rb::aux_mmap_count dropped to zero,
  3323. * they won't start any more (see perf_aux_output_begin()).
  3324. */
  3325. perf_pmu_output_stop(event);
  3326. /* now it's safe to free the pages */
  3327. atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
  3328. vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
  3329. /* this has to be the last one */
  3330. rb_free_aux(rb);
  3331. WARN_ON_ONCE(atomic_read(&rb->aux_refcount));
  3332. mutex_unlock(&event->mmap_mutex);
  3333. }
  3334. atomic_dec(&rb->mmap_count);
  3335. if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
  3336. goto out_put;
  3337. ring_buffer_attach(event, NULL);
  3338. mutex_unlock(&event->mmap_mutex);
  3339. /* If there's still other mmap()s of this buffer, we're done. */
  3340. if (atomic_read(&rb->mmap_count))
  3341. goto out_put;
  3342. /*
  3343. * No other mmap()s, detach from all other events that might redirect
  3344. * into the now unreachable buffer. Somewhat complicated by the
  3345. * fact that rb::event_lock otherwise nests inside mmap_mutex.
  3346. */
  3347. again:
  3348. rcu_read_lock();
  3349. list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
  3350. if (!atomic_long_inc_not_zero(&event->refcount)) {
  3351. /*
  3352. * This event is en-route to free_event() which will
  3353. * detach it and remove it from the list.
  3354. */
  3355. continue;
  3356. }
  3357. rcu_read_unlock();
  3358. mutex_lock(&event->mmap_mutex);
  3359. /*
  3360. * Check we didn't race with perf_event_set_output() which can
  3361. * swizzle the rb from under us while we were waiting to
  3362. * acquire mmap_mutex.
  3363. *
  3364. * If we find a different rb; ignore this event, a next
  3365. * iteration will no longer find it on the list. We have to
  3366. * still restart the iteration to make sure we're not now
  3367. * iterating the wrong list.
  3368. */
  3369. if (event->rb == rb)
  3370. ring_buffer_attach(event, NULL);
  3371. mutex_unlock(&event->mmap_mutex);
  3372. put_event(event);
  3373. /*
  3374. * Restart the iteration; either we're on the wrong list or
  3375. * destroyed its integrity by doing a deletion.
  3376. */
  3377. goto again;
  3378. }
  3379. rcu_read_unlock();
  3380. /*
  3381. * It could be there's still a few 0-ref events on the list; they'll
  3382. * get cleaned up by free_event() -- they'll also still have their
  3383. * ref on the rb and will free it whenever they are done with it.
  3384. *
  3385. * Aside from that, this buffer is 'fully' detached and unmapped,
  3386. * undo the VM accounting.
  3387. */
  3388. atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
  3389. vma->vm_mm->pinned_vm -= mmap_locked;
  3390. free_uid(mmap_user);
  3391. out_put:
  3392. ring_buffer_put(rb); /* could be last */
  3393. }
  3394. static const struct vm_operations_struct perf_mmap_vmops = {
  3395. .open = perf_mmap_open,
  3396. .close = perf_mmap_close, /* non mergable */
  3397. .fault = perf_mmap_fault,
  3398. .page_mkwrite = perf_mmap_fault,
  3399. };
  3400. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  3401. {
  3402. struct perf_event *event = file->private_data;
  3403. unsigned long user_locked, user_lock_limit;
  3404. struct user_struct *user = current_user();
  3405. unsigned long locked, lock_limit;
  3406. struct ring_buffer *rb = NULL;
  3407. unsigned long vma_size;
  3408. unsigned long nr_pages;
  3409. long user_extra = 0, extra = 0;
  3410. int ret = 0, flags = 0;
  3411. /*
  3412. * Don't allow mmap() of inherited per-task counters. This would
  3413. * create a performance issue due to all children writing to the
  3414. * same rb.
  3415. */
  3416. if (event->cpu == -1 && event->attr.inherit)
  3417. return -EINVAL;
  3418. if (!(vma->vm_flags & VM_SHARED))
  3419. return -EINVAL;
  3420. vma_size = vma->vm_end - vma->vm_start;
  3421. if (vma->vm_pgoff == 0) {
  3422. nr_pages = (vma_size / PAGE_SIZE) - 1;
  3423. } else {
  3424. /*
  3425. * AUX area mapping: if rb->aux_nr_pages != 0, it's already
  3426. * mapped, all subsequent mappings should have the same size
  3427. * and offset. Must be above the normal perf buffer.
  3428. */
  3429. u64 aux_offset, aux_size;
  3430. if (!event->rb)
  3431. return -EINVAL;
  3432. nr_pages = vma_size / PAGE_SIZE;
  3433. mutex_lock(&event->mmap_mutex);
  3434. ret = -EINVAL;
  3435. rb = event->rb;
  3436. if (!rb)
  3437. goto aux_unlock;
  3438. aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
  3439. aux_size = ACCESS_ONCE(rb->user_page->aux_size);
  3440. if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
  3441. goto aux_unlock;
  3442. if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
  3443. goto aux_unlock;
  3444. /* already mapped with a different offset */
  3445. if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
  3446. goto aux_unlock;
  3447. if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
  3448. goto aux_unlock;
  3449. /* already mapped with a different size */
  3450. if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
  3451. goto aux_unlock;
  3452. if (!is_power_of_2(nr_pages))
  3453. goto aux_unlock;
  3454. if (!atomic_inc_not_zero(&rb->mmap_count))
  3455. goto aux_unlock;
  3456. if (rb_has_aux(rb)) {
  3457. atomic_inc(&rb->aux_mmap_count);
  3458. ret = 0;
  3459. goto unlock;
  3460. }
  3461. atomic_set(&rb->aux_mmap_count, 1);
  3462. user_extra = nr_pages;
  3463. goto accounting;
  3464. }
  3465. /*
  3466. * If we have rb pages ensure they're a power-of-two number, so we
  3467. * can do bitmasks instead of modulo.
  3468. */
  3469. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  3470. return -EINVAL;
  3471. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  3472. return -EINVAL;
  3473. if (vma->vm_pgoff != 0)
  3474. return -EINVAL;
  3475. WARN_ON_ONCE(event->ctx->parent_ctx);
  3476. again:
  3477. mutex_lock(&event->mmap_mutex);
  3478. if (event->rb) {
  3479. if (event->rb->nr_pages != nr_pages) {
  3480. ret = -EINVAL;
  3481. goto unlock;
  3482. }
  3483. if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
  3484. /*
  3485. * Raced against perf_mmap_close() through
  3486. * perf_event_set_output(). Try again, hope for better
  3487. * luck.
  3488. */
  3489. mutex_unlock(&event->mmap_mutex);
  3490. goto again;
  3491. }
  3492. goto unlock;
  3493. }
  3494. user_extra = nr_pages + 1;
  3495. accounting:
  3496. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  3497. /*
  3498. * Increase the limit linearly with more CPUs:
  3499. */
  3500. user_lock_limit *= num_online_cpus();
  3501. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  3502. if (user_locked > user_lock_limit)
  3503. extra = user_locked - user_lock_limit;
  3504. lock_limit = rlimit(RLIMIT_MEMLOCK);
  3505. lock_limit >>= PAGE_SHIFT;
  3506. locked = vma->vm_mm->pinned_vm + extra;
  3507. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  3508. !capable(CAP_IPC_LOCK)) {
  3509. ret = -EPERM;
  3510. goto unlock;
  3511. }
  3512. WARN_ON(!rb && event->rb);
  3513. if (vma->vm_flags & VM_WRITE)
  3514. flags |= RING_BUFFER_WRITABLE;
  3515. if (!rb) {
  3516. rb = rb_alloc(nr_pages,
  3517. event->attr.watermark ? event->attr.wakeup_watermark : 0,
  3518. event->cpu, flags);
  3519. if (!rb) {
  3520. ret = -ENOMEM;
  3521. goto unlock;
  3522. }
  3523. atomic_set(&rb->mmap_count, 1);
  3524. rb->mmap_user = get_current_user();
  3525. rb->mmap_locked = extra;
  3526. ring_buffer_attach(event, rb);
  3527. perf_event_init_userpage(event);
  3528. perf_event_update_userpage(event);
  3529. } else {
  3530. ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
  3531. event->attr.aux_watermark, flags);
  3532. if (!ret)
  3533. rb->aux_mmap_locked = extra;
  3534. }
  3535. unlock:
  3536. if (!ret) {
  3537. atomic_long_add(user_extra, &user->locked_vm);
  3538. vma->vm_mm->pinned_vm += extra;
  3539. atomic_inc(&event->mmap_count);
  3540. } else if (rb) {
  3541. atomic_dec(&rb->mmap_count);
  3542. }
  3543. aux_unlock:
  3544. mutex_unlock(&event->mmap_mutex);
  3545. /*
  3546. * Since pinned accounting is per vm we cannot allow fork() to copy our
  3547. * vma.
  3548. */
  3549. vma->vm_flags |= VM_DONTCOPY | VM_RESERVED;
  3550. vma->vm_ops = &perf_mmap_vmops;
  3551. if (event->pmu->event_mapped)
  3552. event->pmu->event_mapped(event);
  3553. return ret;
  3554. }
  3555. static int perf_fasync(int fd, struct file *filp, int on)
  3556. {
  3557. struct inode *inode = filp->f_path.dentry->d_inode;
  3558. struct perf_event *event = filp->private_data;
  3559. int retval;
  3560. mutex_lock(&inode->i_mutex);
  3561. retval = fasync_helper(fd, filp, on, &event->fasync);
  3562. mutex_unlock(&inode->i_mutex);
  3563. if (retval < 0)
  3564. return retval;
  3565. return 0;
  3566. }
  3567. static const struct file_operations perf_fops = {
  3568. .llseek = no_llseek,
  3569. .release = perf_release,
  3570. .read = perf_read,
  3571. .poll = perf_poll,
  3572. .unlocked_ioctl = perf_ioctl,
  3573. .compat_ioctl = perf_compat_ioctl,
  3574. .mmap = perf_mmap,
  3575. .fasync = perf_fasync,
  3576. };
  3577. /*
  3578. * Perf event wakeup
  3579. *
  3580. * If there's data, ensure we set the poll() state and publish everything
  3581. * to user-space before waking everybody up.
  3582. */
  3583. static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
  3584. {
  3585. /* only the parent has fasync state */
  3586. if (event->parent)
  3587. event = event->parent;
  3588. return &event->fasync;
  3589. }
  3590. void perf_event_wakeup(struct perf_event *event)
  3591. {
  3592. ring_buffer_wakeup(event);
  3593. if (event->pending_kill) {
  3594. kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
  3595. event->pending_kill = 0;
  3596. }
  3597. }
  3598. static void perf_pending_event(struct irq_work *entry)
  3599. {
  3600. struct perf_event *event = container_of(entry,
  3601. struct perf_event, pending);
  3602. int rctx;
  3603. rctx = perf_swevent_get_recursion_context();
  3604. /*
  3605. * If we 'fail' here, that's OK, it means recursion is already disabled
  3606. * and we won't recurse 'further'.
  3607. */
  3608. if (event->pending_disable) {
  3609. event->pending_disable = 0;
  3610. __perf_event_disable(event);
  3611. }
  3612. if (event->pending_wakeup) {
  3613. event->pending_wakeup = 0;
  3614. perf_event_wakeup(event);
  3615. }
  3616. if (rctx >= 0)
  3617. perf_swevent_put_recursion_context(rctx);
  3618. }
  3619. /*
  3620. * We assume there is only KVM supporting the callbacks.
  3621. * Later on, we might change it to a list if there is
  3622. * another virtualization implementation supporting the callbacks.
  3623. */
  3624. struct perf_guest_info_callbacks *perf_guest_cbs;
  3625. int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  3626. {
  3627. perf_guest_cbs = cbs;
  3628. return 0;
  3629. }
  3630. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  3631. int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  3632. {
  3633. perf_guest_cbs = NULL;
  3634. return 0;
  3635. }
  3636. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  3637. static void
  3638. perf_output_sample_regs(struct perf_output_handle *handle,
  3639. struct pt_regs *regs, u64 mask)
  3640. {
  3641. int bit;
  3642. for_each_set_bit(bit, (const unsigned long *) &mask,
  3643. sizeof(mask) * BITS_PER_BYTE) {
  3644. u64 val;
  3645. val = perf_reg_value(regs, bit);
  3646. perf_output_put(handle, val);
  3647. }
  3648. }
  3649. static void perf_sample_regs_user(struct perf_regs *regs_user,
  3650. struct pt_regs *regs)
  3651. {
  3652. if (!user_mode(regs)) {
  3653. if (current->mm)
  3654. regs = task_pt_regs(current);
  3655. else
  3656. regs = NULL;
  3657. }
  3658. if (regs) {
  3659. regs_user->regs = regs;
  3660. regs_user->abi = perf_reg_abi(current);
  3661. }
  3662. }
  3663. static void perf_sample_regs_intr(struct perf_regs *regs_intr,
  3664. struct pt_regs *regs)
  3665. {
  3666. regs_intr->regs = regs;
  3667. regs_intr->abi = perf_reg_abi(current);
  3668. }
  3669. /*
  3670. * Get remaining task size from user stack pointer.
  3671. *
  3672. * It'd be better to take stack vma map and limit this more
  3673. * precisly, but there's no way to get it safely under interrupt,
  3674. * so using TASK_SIZE as limit.
  3675. */
  3676. static u64 perf_ustack_task_size(struct pt_regs *regs)
  3677. {
  3678. unsigned long addr = perf_user_stack_pointer(regs);
  3679. if (!addr || addr >= TASK_SIZE)
  3680. return 0;
  3681. return TASK_SIZE - addr;
  3682. }
  3683. static u16
  3684. perf_sample_ustack_size(u16 stack_size, u16 header_size,
  3685. struct pt_regs *regs)
  3686. {
  3687. u64 task_size;
  3688. /* No regs, no stack pointer, no dump. */
  3689. if (!regs)
  3690. return 0;
  3691. /*
  3692. * Check if we fit in with the requested stack size into the:
  3693. * - TASK_SIZE
  3694. * If we don't, we limit the size to the TASK_SIZE.
  3695. *
  3696. * - remaining sample size
  3697. * If we don't, we customize the stack size to
  3698. * fit in to the remaining sample size.
  3699. */
  3700. task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
  3701. stack_size = min(stack_size, (u16) task_size);
  3702. /* Current header size plus static size and dynamic size. */
  3703. header_size += 2 * sizeof(u64);
  3704. /* Do we fit in with the current stack dump size? */
  3705. if ((u16) (header_size + stack_size) < header_size) {
  3706. /*
  3707. * If we overflow the maximum size for the sample,
  3708. * we customize the stack dump size to fit in.
  3709. */
  3710. stack_size = USHRT_MAX - header_size - sizeof(u64);
  3711. stack_size = round_up(stack_size, sizeof(u64));
  3712. }
  3713. return stack_size;
  3714. }
  3715. static void
  3716. perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
  3717. struct pt_regs *regs)
  3718. {
  3719. /* Case of a kernel thread, nothing to dump */
  3720. if (!regs) {
  3721. u64 size = 0;
  3722. perf_output_put(handle, size);
  3723. } else {
  3724. unsigned long sp;
  3725. unsigned int rem;
  3726. u64 dyn_size;
  3727. /*
  3728. * We dump:
  3729. * static size
  3730. * - the size requested by user or the best one we can fit
  3731. * in to the sample max size
  3732. * data
  3733. * - user stack dump data
  3734. * dynamic size
  3735. * - the actual dumped size
  3736. */
  3737. /* Static size. */
  3738. perf_output_put(handle, dump_size);
  3739. /* Data. */
  3740. sp = perf_user_stack_pointer(regs);
  3741. rem = __output_copy_user(handle, (void *) sp, dump_size);
  3742. dyn_size = dump_size - rem;
  3743. perf_output_skip(handle, rem);
  3744. /* Dynamic size. */
  3745. perf_output_put(handle, dyn_size);
  3746. }
  3747. }
  3748. static void __perf_event_header__init_id(struct perf_event_header *header,
  3749. struct perf_sample_data *data,
  3750. struct perf_event *event)
  3751. {
  3752. u64 sample_type = event->attr.sample_type;
  3753. data->type = sample_type;
  3754. header->size += event->id_header_size;
  3755. if (sample_type & PERF_SAMPLE_TID) {
  3756. /* namespace issues */
  3757. data->tid_entry.pid = perf_event_pid(event, current);
  3758. data->tid_entry.tid = perf_event_tid(event, current);
  3759. }
  3760. if (sample_type & PERF_SAMPLE_TIME)
  3761. data->time = perf_event_clock(event);
  3762. if (sample_type & PERF_SAMPLE_ID)
  3763. data->id = primary_event_id(event);
  3764. if (sample_type & PERF_SAMPLE_STREAM_ID)
  3765. data->stream_id = event->id;
  3766. if (sample_type & PERF_SAMPLE_CPU) {
  3767. data->cpu_entry.cpu = raw_smp_processor_id();
  3768. data->cpu_entry.reserved = 0;
  3769. }
  3770. }
  3771. void perf_event_header__init_id(struct perf_event_header *header,
  3772. struct perf_sample_data *data,
  3773. struct perf_event *event)
  3774. {
  3775. if (event->attr.sample_id_all)
  3776. __perf_event_header__init_id(header, data, event);
  3777. }
  3778. static void __perf_event__output_id_sample(struct perf_output_handle *handle,
  3779. struct perf_sample_data *data)
  3780. {
  3781. u64 sample_type = data->type;
  3782. if (sample_type & PERF_SAMPLE_TID)
  3783. perf_output_put(handle, data->tid_entry);
  3784. if (sample_type & PERF_SAMPLE_TIME)
  3785. perf_output_put(handle, data->time);
  3786. if (sample_type & PERF_SAMPLE_ID)
  3787. perf_output_put(handle, data->id);
  3788. if (sample_type & PERF_SAMPLE_STREAM_ID)
  3789. perf_output_put(handle, data->stream_id);
  3790. if (sample_type & PERF_SAMPLE_CPU)
  3791. perf_output_put(handle, data->cpu_entry);
  3792. }
  3793. void perf_event__output_id_sample(struct perf_event *event,
  3794. struct perf_output_handle *handle,
  3795. struct perf_sample_data *sample)
  3796. {
  3797. if (event->attr.sample_id_all)
  3798. __perf_event__output_id_sample(handle, sample);
  3799. }
  3800. static void perf_output_read_one(struct perf_output_handle *handle,
  3801. struct perf_event *event,
  3802. u64 enabled, u64 running)
  3803. {
  3804. u64 read_format = event->attr.read_format;
  3805. u64 values[4];
  3806. int n = 0;
  3807. values[n++] = perf_event_count(event);
  3808. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  3809. values[n++] = enabled +
  3810. atomic64_read(&event->child_total_time_enabled);
  3811. }
  3812. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  3813. values[n++] = running +
  3814. atomic64_read(&event->child_total_time_running);
  3815. }
  3816. if (read_format & PERF_FORMAT_ID)
  3817. values[n++] = primary_event_id(event);
  3818. __output_copy(handle, values, n * sizeof(u64));
  3819. }
  3820. /*
  3821. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  3822. */
  3823. static void perf_output_read_group(struct perf_output_handle *handle,
  3824. struct perf_event *event,
  3825. u64 enabled, u64 running)
  3826. {
  3827. struct perf_event *leader = event->group_leader, *sub;
  3828. u64 read_format = event->attr.read_format;
  3829. u64 values[5];
  3830. int n = 0;
  3831. values[n++] = 1 + leader->nr_siblings;
  3832. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  3833. values[n++] = enabled;
  3834. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  3835. values[n++] = running;
  3836. if (leader != event)
  3837. leader->pmu->read(leader);
  3838. values[n++] = perf_event_count(leader);
  3839. if (read_format & PERF_FORMAT_ID)
  3840. values[n++] = primary_event_id(leader);
  3841. __output_copy(handle, values, n * sizeof(u64));
  3842. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  3843. n = 0;
  3844. if (sub != event)
  3845. sub->pmu->read(sub);
  3846. values[n++] = perf_event_count(sub);
  3847. if (read_format & PERF_FORMAT_ID)
  3848. values[n++] = primary_event_id(sub);
  3849. __output_copy(handle, values, n * sizeof(u64));
  3850. }
  3851. }
  3852. #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
  3853. PERF_FORMAT_TOTAL_TIME_RUNNING)
  3854. static void perf_output_read(struct perf_output_handle *handle,
  3855. struct perf_event *event)
  3856. {
  3857. u64 enabled = 0, running = 0, now;
  3858. u64 read_format = event->attr.read_format;
  3859. /*
  3860. * compute total_time_enabled, total_time_running
  3861. * based on snapshot values taken when the event
  3862. * was last scheduled in.
  3863. *
  3864. * we cannot simply called update_context_time()
  3865. * because of locking issue as we are called in
  3866. * NMI context
  3867. */
  3868. if (read_format & PERF_FORMAT_TOTAL_TIMES)
  3869. calc_timer_values(event, &now, &enabled, &running);
  3870. if (event->attr.read_format & PERF_FORMAT_GROUP)
  3871. perf_output_read_group(handle, event, enabled, running);
  3872. else
  3873. perf_output_read_one(handle, event, enabled, running);
  3874. }
  3875. void perf_output_sample(struct perf_output_handle *handle,
  3876. struct perf_event_header *header,
  3877. struct perf_sample_data *data,
  3878. struct perf_event *event)
  3879. {
  3880. u64 sample_type = data->type;
  3881. perf_output_put(handle, *header);
  3882. if (sample_type & PERF_SAMPLE_IP)
  3883. perf_output_put(handle, data->ip);
  3884. if (sample_type & PERF_SAMPLE_TID)
  3885. perf_output_put(handle, data->tid_entry);
  3886. if (sample_type & PERF_SAMPLE_TIME)
  3887. perf_output_put(handle, data->time);
  3888. if (sample_type & PERF_SAMPLE_ADDR)
  3889. perf_output_put(handle, data->addr);
  3890. if (sample_type & PERF_SAMPLE_ID)
  3891. perf_output_put(handle, data->id);
  3892. if (sample_type & PERF_SAMPLE_STREAM_ID)
  3893. perf_output_put(handle, data->stream_id);
  3894. if (sample_type & PERF_SAMPLE_CPU)
  3895. perf_output_put(handle, data->cpu_entry);
  3896. if (sample_type & PERF_SAMPLE_PERIOD)
  3897. perf_output_put(handle, data->period);
  3898. if (sample_type & PERF_SAMPLE_READ)
  3899. perf_output_read(handle, event);
  3900. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  3901. if (data->callchain) {
  3902. int size = 1;
  3903. if (data->callchain)
  3904. size += data->callchain->nr;
  3905. size *= sizeof(u64);
  3906. __output_copy(handle, data->callchain, size);
  3907. } else {
  3908. u64 nr = 0;
  3909. perf_output_put(handle, nr);
  3910. }
  3911. }
  3912. if (sample_type & PERF_SAMPLE_RAW) {
  3913. if (data->raw) {
  3914. perf_output_put(handle, data->raw->size);
  3915. __output_copy(handle, data->raw->data,
  3916. data->raw->size);
  3917. } else {
  3918. struct {
  3919. u32 size;
  3920. u32 data;
  3921. } raw = {
  3922. .size = sizeof(u32),
  3923. .data = 0,
  3924. };
  3925. perf_output_put(handle, raw);
  3926. }
  3927. }
  3928. if (sample_type & PERF_SAMPLE_REGS_INTR) {
  3929. u64 abi = data->regs_intr.abi;
  3930. /*
  3931. * If there are no regs to dump, notice it through
  3932. * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
  3933. */
  3934. perf_output_put(handle, abi);
  3935. if (abi) {
  3936. u64 mask = event->attr.sample_regs_intr;
  3937. perf_output_sample_regs(handle,
  3938. data->regs_intr.regs,
  3939. mask);
  3940. }
  3941. }
  3942. if (!event->attr.watermark) {
  3943. int wakeup_events = event->attr.wakeup_events;
  3944. if (wakeup_events) {
  3945. struct ring_buffer *rb = handle->rb;
  3946. int events = local_inc_return(&rb->events);
  3947. if (events >= wakeup_events) {
  3948. local_sub(wakeup_events, &rb->events);
  3949. local_inc(&rb->wakeup);
  3950. }
  3951. }
  3952. }
  3953. if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
  3954. if (data->br_stack) {
  3955. size_t size;
  3956. size = data->br_stack->nr
  3957. * sizeof(struct perf_branch_entry);
  3958. perf_output_put(handle, data->br_stack->nr);
  3959. perf_output_copy(handle, data->br_stack->entries, size);
  3960. } else {
  3961. /*
  3962. * we always store at least the value of nr
  3963. */
  3964. u64 nr = 0;
  3965. perf_output_put(handle, nr);
  3966. }
  3967. }
  3968. if (sample_type & PERF_SAMPLE_REGS_USER) {
  3969. u64 abi = data->regs_user.abi;
  3970. /*
  3971. * If there are no regs to dump, notice it through
  3972. * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
  3973. */
  3974. perf_output_put(handle, abi);
  3975. if (abi) {
  3976. u64 mask = event->attr.sample_regs_user;
  3977. perf_output_sample_regs(handle,
  3978. data->regs_user.regs,
  3979. mask);
  3980. }
  3981. }
  3982. if (sample_type & PERF_SAMPLE_STACK_USER)
  3983. perf_output_sample_ustack(handle,
  3984. data->stack_user_size,
  3985. data->regs_user.regs);
  3986. }
  3987. void perf_prepare_sample(struct perf_event_header *header,
  3988. struct perf_sample_data *data,
  3989. struct perf_event *event,
  3990. struct pt_regs *regs)
  3991. {
  3992. u64 sample_type = event->attr.sample_type;
  3993. header->type = PERF_RECORD_SAMPLE;
  3994. header->size = sizeof(*header) + event->header_size;
  3995. header->misc = 0;
  3996. header->misc |= perf_misc_flags(regs);
  3997. __perf_event_header__init_id(header, data, event);
  3998. if (sample_type & PERF_SAMPLE_IP)
  3999. data->ip = perf_instruction_pointer(regs);
  4000. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  4001. int size = 1;
  4002. data->callchain = perf_callchain(event, regs);
  4003. if (data->callchain)
  4004. size += data->callchain->nr;
  4005. header->size += size * sizeof(u64);
  4006. }
  4007. if (sample_type & PERF_SAMPLE_RAW) {
  4008. int size = sizeof(u32);
  4009. if (data->raw)
  4010. size += data->raw->size;
  4011. else
  4012. size += sizeof(u32);
  4013. WARN_ON_ONCE(size & (sizeof(u64)-1));
  4014. header->size += size;
  4015. }
  4016. if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
  4017. int size = sizeof(u64); /* nr */
  4018. if (data->br_stack) {
  4019. size += data->br_stack->nr
  4020. * sizeof(struct perf_branch_entry);
  4021. }
  4022. header->size += size;
  4023. }
  4024. if (sample_type & PERF_SAMPLE_REGS_USER) {
  4025. /* regs dump ABI info */
  4026. int size = sizeof(u64);
  4027. perf_sample_regs_user(&data->regs_user, regs);
  4028. if (data->regs_user.regs) {
  4029. u64 mask = event->attr.sample_regs_user;
  4030. size += hweight64(mask) * sizeof(u64);
  4031. }
  4032. header->size += size;
  4033. }
  4034. if (sample_type & PERF_SAMPLE_STACK_USER) {
  4035. /*
  4036. * Either we need PERF_SAMPLE_STACK_USER bit to be allways
  4037. * processed as the last one or have additional check added
  4038. * in case new sample type is added, because we could eat
  4039. * up the rest of the sample size.
  4040. */
  4041. struct perf_regs *uregs = &data->regs_user;
  4042. u16 stack_size = event->attr.sample_stack_user;
  4043. u16 size = sizeof(u64);
  4044. if (!uregs->abi)
  4045. perf_sample_regs_user(uregs, regs);
  4046. stack_size = perf_sample_ustack_size(stack_size, header->size,
  4047. uregs->regs);
  4048. /*
  4049. * If there is something to dump, add space for the dump
  4050. * itself and for the field that tells the dynamic size,
  4051. * which is how many have been actually dumped.
  4052. */
  4053. if (stack_size)
  4054. size += sizeof(u64) + stack_size;
  4055. data->stack_user_size = stack_size;
  4056. header->size += size;
  4057. }
  4058. if (sample_type & PERF_SAMPLE_REGS_INTR) {
  4059. /* regs dump ABI info */
  4060. int size = sizeof(u64);
  4061. perf_sample_regs_intr(&data->regs_intr, regs);
  4062. if (data->regs_intr.regs) {
  4063. u64 mask = event->attr.sample_regs_intr;
  4064. size += hweight64(mask) * sizeof(u64);
  4065. }
  4066. header->size += size;
  4067. }
  4068. }
  4069. static void perf_event_output(struct perf_event *event,
  4070. struct perf_sample_data *data,
  4071. struct pt_regs *regs)
  4072. {
  4073. struct perf_output_handle handle;
  4074. struct perf_event_header header;
  4075. /* protect the callchain buffers */
  4076. rcu_read_lock();
  4077. perf_prepare_sample(&header, data, event, regs);
  4078. if (perf_output_begin(&handle, event, header.size))
  4079. goto exit;
  4080. perf_output_sample(&handle, &header, data, event);
  4081. perf_output_end(&handle);
  4082. exit:
  4083. rcu_read_unlock();
  4084. }
  4085. /*
  4086. * read event_id
  4087. */
  4088. struct perf_read_event {
  4089. struct perf_event_header header;
  4090. u32 pid;
  4091. u32 tid;
  4092. };
  4093. static void
  4094. perf_event_read_event(struct perf_event *event,
  4095. struct task_struct *task)
  4096. {
  4097. struct perf_output_handle handle;
  4098. struct perf_sample_data sample;
  4099. struct perf_read_event read_event = {
  4100. .header = {
  4101. .type = PERF_RECORD_READ,
  4102. .misc = 0,
  4103. .size = sizeof(read_event) + event->read_size,
  4104. },
  4105. .pid = perf_event_pid(event, task),
  4106. .tid = perf_event_tid(event, task),
  4107. };
  4108. int ret;
  4109. perf_event_header__init_id(&read_event.header, &sample, event);
  4110. ret = perf_output_begin(&handle, event, read_event.header.size);
  4111. if (ret)
  4112. return;
  4113. perf_output_put(&handle, read_event);
  4114. perf_output_read(&handle, event);
  4115. perf_event__output_id_sample(event, &handle, &sample);
  4116. perf_output_end(&handle);
  4117. }
  4118. struct remote_output {
  4119. struct ring_buffer *rb;
  4120. int err;
  4121. };
  4122. static void __perf_event_output_stop(struct perf_event *event, void *data)
  4123. {
  4124. struct perf_event *parent = event->parent;
  4125. struct remote_output *ro = data;
  4126. struct ring_buffer *rb = ro->rb;
  4127. if (!has_aux(event))
  4128. return;
  4129. if (!parent)
  4130. parent = event;
  4131. /*
  4132. * In case of inheritance, it will be the parent that links to the
  4133. * ring-buffer, but it will be the child that's actually using it:
  4134. */
  4135. if (rcu_dereference(parent->rb) == rb)
  4136. ro->err = __perf_event_stop(event);
  4137. }
  4138. static int __perf_pmu_output_stop(void *info)
  4139. {
  4140. struct perf_event *event = info;
  4141. struct pmu *pmu = event->pmu;
  4142. struct perf_cpu_context *cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  4143. struct remote_output ro = {
  4144. .rb = event->rb,
  4145. };
  4146. rcu_read_lock();
  4147. if (cpuctx->task_ctx)
  4148. perf_event_aux_ctx(cpuctx->task_ctx, __perf_event_output_stop,
  4149. &ro);
  4150. rcu_read_unlock();
  4151. return ro.err;
  4152. }
  4153. static void perf_pmu_output_stop(struct perf_event *event)
  4154. {
  4155. struct perf_event *iter;
  4156. int err, cpu;
  4157. restart:
  4158. rcu_read_lock();
  4159. list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
  4160. /*
  4161. * For per-CPU events, we need to make sure that neither they
  4162. * nor their children are running; for cpu==-1 events it's
  4163. * sufficient to stop the event itself if it's active, since
  4164. * it can't have children.
  4165. */
  4166. cpu = iter->cpu;
  4167. if (cpu == -1)
  4168. cpu = READ_ONCE(iter->oncpu);
  4169. if (cpu == -1)
  4170. continue;
  4171. err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
  4172. if (err == -EAGAIN) {
  4173. rcu_read_unlock();
  4174. goto restart;
  4175. }
  4176. }
  4177. rcu_read_unlock();
  4178. }
  4179. static void
  4180. perf_event_aux_ctx(struct perf_event_context *ctx,
  4181. perf_event_aux_output_cb output,
  4182. void *data)
  4183. {
  4184. struct perf_event *event;
  4185. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  4186. if (event->state < PERF_EVENT_STATE_INACTIVE)
  4187. continue;
  4188. if (!event_filter_match(event))
  4189. continue;
  4190. output(event, data);
  4191. }
  4192. }
  4193. static void
  4194. perf_event_aux(perf_event_aux_output_cb output, void *data,
  4195. struct perf_event_context *task_ctx)
  4196. {
  4197. struct perf_cpu_context *cpuctx;
  4198. struct perf_event_context *ctx;
  4199. struct pmu *pmu;
  4200. int ctxn;
  4201. rcu_read_lock();
  4202. list_for_each_entry_rcu(pmu, &pmus, entry) {
  4203. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  4204. #if 0
  4205. if (cpuctx->unique_pmu != pmu)
  4206. goto next;
  4207. #endif
  4208. perf_event_aux_ctx(&cpuctx->ctx, output, data);
  4209. if (task_ctx)
  4210. goto next;
  4211. ctxn = pmu->task_ctx_nr;
  4212. if (ctxn < 0)
  4213. goto next;
  4214. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  4215. if (ctx)
  4216. perf_event_aux_ctx(ctx, output, data);
  4217. next:
  4218. put_cpu_ptr(pmu->pmu_cpu_context);
  4219. }
  4220. if (task_ctx) {
  4221. preempt_disable();
  4222. perf_event_aux_ctx(task_ctx, output, data);
  4223. preempt_enable();
  4224. }
  4225. rcu_read_unlock();
  4226. }
  4227. /*
  4228. * task tracking -- fork/exit
  4229. *
  4230. * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
  4231. */
  4232. struct perf_task_event {
  4233. struct task_struct *task;
  4234. struct perf_event_context *task_ctx;
  4235. struct {
  4236. struct perf_event_header header;
  4237. u32 pid;
  4238. u32 ppid;
  4239. u32 tid;
  4240. u32 ptid;
  4241. u64 time;
  4242. } event_id;
  4243. };
  4244. static int perf_event_task_match(struct perf_event *event)
  4245. {
  4246. return event->attr.comm || event->attr.mmap ||
  4247. event->attr.mmap_data || event->attr.task;
  4248. }
  4249. static void perf_event_task_output(struct perf_event *event,
  4250. void *data)
  4251. {
  4252. struct perf_task_event *task_event = data;
  4253. struct perf_output_handle handle;
  4254. struct perf_sample_data sample;
  4255. struct task_struct *task = task_event->task;
  4256. int ret, size = task_event->event_id.header.size;
  4257. if (!perf_event_task_match(event))
  4258. return;
  4259. perf_event_header__init_id(&task_event->event_id.header, &sample, event);
  4260. ret = perf_output_begin(&handle, event,
  4261. task_event->event_id.header.size);
  4262. if (ret)
  4263. goto out;
  4264. task_event->event_id.pid = perf_event_pid(event, task);
  4265. task_event->event_id.ppid = perf_event_pid(event, current);
  4266. task_event->event_id.tid = perf_event_tid(event, task);
  4267. task_event->event_id.ptid = perf_event_tid(event, current);
  4268. task_event->event_id.time = perf_event_clock(event);
  4269. perf_output_put(&handle, task_event->event_id);
  4270. perf_event__output_id_sample(event, &handle, &sample);
  4271. perf_output_end(&handle);
  4272. out:
  4273. task_event->event_id.header.size = size;
  4274. }
  4275. static void perf_event_task(struct task_struct *task,
  4276. struct perf_event_context *task_ctx,
  4277. int new)
  4278. {
  4279. struct perf_task_event task_event;
  4280. if (!atomic_read(&nr_comm_events) &&
  4281. !atomic_read(&nr_mmap_events) &&
  4282. !atomic_read(&nr_task_events))
  4283. return;
  4284. task_event = (struct perf_task_event){
  4285. .task = task,
  4286. .task_ctx = task_ctx,
  4287. .event_id = {
  4288. .header = {
  4289. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  4290. .misc = 0,
  4291. .size = sizeof(task_event.event_id),
  4292. },
  4293. /* .pid */
  4294. /* .ppid */
  4295. /* .tid */
  4296. /* .ptid */
  4297. /* .time */
  4298. },
  4299. };
  4300. perf_event_aux(perf_event_task_output,
  4301. &task_event,
  4302. task_ctx);
  4303. }
  4304. void perf_event_fork(struct task_struct *task)
  4305. {
  4306. perf_event_task(task, NULL, 1);
  4307. }
  4308. /*
  4309. * comm tracking
  4310. */
  4311. struct perf_comm_event {
  4312. struct task_struct *task;
  4313. char *comm;
  4314. int comm_size;
  4315. struct {
  4316. struct perf_event_header header;
  4317. u32 pid;
  4318. u32 tid;
  4319. } event_id;
  4320. };
  4321. static void perf_event_comm_output(struct perf_event *event,
  4322. void *data)
  4323. {
  4324. struct perf_comm_event *comm_event = data;
  4325. struct perf_output_handle handle;
  4326. struct perf_sample_data sample;
  4327. int size = comm_event->event_id.header.size;
  4328. int ret;
  4329. if (!perf_event_comm_match(event))
  4330. return;
  4331. perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
  4332. ret = perf_output_begin(&handle, event,
  4333. comm_event->event_id.header.size);
  4334. if (ret)
  4335. goto out;
  4336. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  4337. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  4338. perf_output_put(&handle, comm_event->event_id);
  4339. __output_copy(&handle, comm_event->comm,
  4340. comm_event->comm_size);
  4341. perf_event__output_id_sample(event, &handle, &sample);
  4342. perf_output_end(&handle);
  4343. out:
  4344. comm_event->event_id.header.size = size;
  4345. }
  4346. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  4347. {
  4348. char comm[TASK_COMM_LEN];
  4349. unsigned int size;
  4350. memset(comm, 0, sizeof(comm));
  4351. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  4352. size = ALIGN(strlen(comm)+1, sizeof(u64));
  4353. comm_event->comm = comm;
  4354. comm_event->comm_size = size;
  4355. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  4356. perf_event_aux(perf_event_comm_output,
  4357. comm_event,
  4358. NULL);
  4359. }
  4360. static int perf_event_comm_match(struct perf_event *event)
  4361. {
  4362. return event->attr.comm;
  4363. }
  4364. void perf_event_comm(struct task_struct *task, bool exec)
  4365. {
  4366. struct perf_comm_event comm_event;
  4367. if (!atomic_read(&nr_comm_events))
  4368. return;
  4369. comm_event = (struct perf_comm_event){
  4370. .task = task,
  4371. /* .comm */
  4372. /* .comm_size */
  4373. .event_id = {
  4374. .header = {
  4375. .type = PERF_RECORD_COMM,
  4376. .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
  4377. /* .size */
  4378. },
  4379. /* .pid */
  4380. /* .tid */
  4381. },
  4382. };
  4383. perf_event_comm_event(&comm_event);
  4384. }
  4385. /*
  4386. * mmap tracking
  4387. */
  4388. struct perf_mmap_event {
  4389. struct vm_area_struct *vma;
  4390. const char *file_name;
  4391. int file_size;
  4392. struct {
  4393. struct perf_event_header header;
  4394. u32 pid;
  4395. u32 tid;
  4396. u64 start;
  4397. u64 len;
  4398. u64 pgoff;
  4399. } event_id;
  4400. };
  4401. static int perf_event_mmap_match(struct perf_event *event,
  4402. void *data)
  4403. {
  4404. struct perf_mmap_event *mmap_event = data;
  4405. struct vm_area_struct *vma = mmap_event->vma;
  4406. int executable = vma->vm_flags & VM_EXEC;
  4407. return (!executable && event->attr.mmap_data) ||
  4408. (executable && event->attr.mmap);
  4409. }
  4410. static void perf_event_mmap_output(struct perf_event *event,
  4411. void *data)
  4412. {
  4413. struct perf_mmap_event *mmap_event = data;
  4414. struct perf_output_handle handle;
  4415. struct perf_sample_data sample;
  4416. int size = mmap_event->event_id.header.size;
  4417. int ret;
  4418. if (!perf_event_mmap_match(event, data))
  4419. return;
  4420. perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
  4421. ret = perf_output_begin(&handle, event,
  4422. mmap_event->event_id.header.size);
  4423. if (ret)
  4424. goto out;
  4425. mmap_event->event_id.pid = perf_event_pid(event, current);
  4426. mmap_event->event_id.tid = perf_event_tid(event, current);
  4427. perf_output_put(&handle, mmap_event->event_id);
  4428. __output_copy(&handle, mmap_event->file_name,
  4429. mmap_event->file_size);
  4430. perf_event__output_id_sample(event, &handle, &sample);
  4431. perf_output_end(&handle);
  4432. out:
  4433. mmap_event->event_id.header.size = size;
  4434. }
  4435. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  4436. {
  4437. struct vm_area_struct *vma = mmap_event->vma;
  4438. struct file *file = vma->vm_file;
  4439. unsigned int size;
  4440. char tmp[16];
  4441. char *buf = NULL;
  4442. const char *name;
  4443. memset(tmp, 0, sizeof(tmp));
  4444. if (file) {
  4445. /*
  4446. * d_path works from the end of the rb backwards, so we
  4447. * need to add enough zero bytes after the string to handle
  4448. * the 64bit alignment we do later.
  4449. */
  4450. buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
  4451. if (!buf) {
  4452. name = strncpy(tmp, "//enomem", sizeof(tmp));
  4453. goto got_name;
  4454. }
  4455. name = d_path(&file->f_path, buf, PATH_MAX);
  4456. if (IS_ERR(name)) {
  4457. name = strncpy(tmp, "//toolong", sizeof(tmp));
  4458. goto got_name;
  4459. }
  4460. } else {
  4461. if (arch_vma_name(mmap_event->vma)) {
  4462. name = strncpy(tmp, arch_vma_name(mmap_event->vma),
  4463. sizeof(tmp));
  4464. goto got_name;
  4465. }
  4466. if (!vma->vm_mm) {
  4467. name = strncpy(tmp, "[vdso]", sizeof(tmp));
  4468. goto got_name;
  4469. } else if (vma->vm_start <= vma->vm_mm->start_brk &&
  4470. vma->vm_end >= vma->vm_mm->brk) {
  4471. name = strncpy(tmp, "[heap]", sizeof(tmp));
  4472. goto got_name;
  4473. } else if (vma->vm_start <= vma->vm_mm->start_stack &&
  4474. vma->vm_end >= vma->vm_mm->start_stack) {
  4475. name = strncpy(tmp, "[stack]", sizeof(tmp));
  4476. goto got_name;
  4477. }
  4478. name = strncpy(tmp, "//anon", sizeof(tmp));
  4479. goto got_name;
  4480. }
  4481. got_name:
  4482. size = ALIGN(strlen(name)+1, sizeof(u64));
  4483. mmap_event->file_name = name;
  4484. mmap_event->file_size = size;
  4485. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  4486. perf_event_aux(perf_event_mmap_output,
  4487. mmap_event,
  4488. NULL);
  4489. kfree(buf);
  4490. }
  4491. void perf_event_mmap(struct vm_area_struct *vma)
  4492. {
  4493. struct perf_mmap_event mmap_event;
  4494. if (!atomic_read(&nr_mmap_events))
  4495. return;
  4496. mmap_event = (struct perf_mmap_event){
  4497. .vma = vma,
  4498. /* .file_name */
  4499. /* .file_size */
  4500. .event_id = {
  4501. .header = {
  4502. .type = PERF_RECORD_MMAP,
  4503. .misc = PERF_RECORD_MISC_USER,
  4504. /* .size */
  4505. },
  4506. /* .pid */
  4507. /* .tid */
  4508. .start = vma->vm_start,
  4509. .len = vma->vm_end - vma->vm_start,
  4510. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  4511. },
  4512. };
  4513. perf_event_mmap_event(&mmap_event);
  4514. }
  4515. /*
  4516. * IRQ throttle logging
  4517. */
  4518. static void perf_log_throttle(struct perf_event *event, int enable)
  4519. {
  4520. struct perf_output_handle handle;
  4521. struct perf_sample_data sample;
  4522. int ret;
  4523. struct {
  4524. struct perf_event_header header;
  4525. u64 time;
  4526. u64 id;
  4527. u64 stream_id;
  4528. } throttle_event = {
  4529. .header = {
  4530. .type = PERF_RECORD_THROTTLE,
  4531. .misc = 0,
  4532. .size = sizeof(throttle_event),
  4533. },
  4534. .time = perf_event_clock(event),
  4535. .id = primary_event_id(event),
  4536. .stream_id = event->id,
  4537. };
  4538. if (enable)
  4539. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  4540. perf_event_header__init_id(&throttle_event.header, &sample, event);
  4541. ret = perf_output_begin(&handle, event,
  4542. throttle_event.header.size);
  4543. if (ret)
  4544. return;
  4545. perf_output_put(&handle, throttle_event);
  4546. perf_event__output_id_sample(event, &handle, &sample);
  4547. perf_output_end(&handle);
  4548. }
  4549. /*
  4550. * Generic event overflow handling, sampling.
  4551. */
  4552. static int __perf_event_overflow(struct perf_event *event,
  4553. int throttle, struct perf_sample_data *data,
  4554. struct pt_regs *regs)
  4555. {
  4556. int events = atomic_read(&event->event_limit);
  4557. struct hw_perf_event *hwc = &event->hw;
  4558. u64 seq;
  4559. int ret = 0;
  4560. /*
  4561. * Non-sampling counters might still use the PMI to fold short
  4562. * hardware counters, ignore those.
  4563. */
  4564. if (unlikely(!is_sampling_event(event)))
  4565. return 0;
  4566. seq = __this_cpu_read(perf_throttled_seq);
  4567. if (seq != hwc->interrupts_seq) {
  4568. hwc->interrupts_seq = seq;
  4569. hwc->interrupts = 1;
  4570. } else {
  4571. hwc->interrupts++;
  4572. if (unlikely(throttle
  4573. && hwc->interrupts >= max_samples_per_tick)) {
  4574. __this_cpu_inc(perf_throttled_count);
  4575. hwc->interrupts = MAX_INTERRUPTS;
  4576. perf_log_throttle(event, 0);
  4577. ret = 1;
  4578. }
  4579. }
  4580. if (event->attr.freq) {
  4581. u64 now = perf_clock();
  4582. s64 delta = now - hwc->freq_time_stamp;
  4583. hwc->freq_time_stamp = now;
  4584. if (delta > 0 && delta < 2*TICK_NSEC)
  4585. perf_adjust_period(event, delta, hwc->last_period, true);
  4586. }
  4587. /*
  4588. * XXX event_limit might not quite work as expected on inherited
  4589. * events
  4590. */
  4591. event->pending_kill = POLL_IN;
  4592. if (events && atomic_dec_and_test(&event->event_limit)) {
  4593. ret = 1;
  4594. event->pending_kill = POLL_HUP;
  4595. event->pending_disable = 1;
  4596. irq_work_queue(&event->pending);
  4597. }
  4598. if (event->overflow_handler)
  4599. event->overflow_handler(event, data, regs);
  4600. else
  4601. perf_event_output(event, data, regs);
  4602. if (*perf_event_fasync(event) && event->pending_kill) {
  4603. event->pending_wakeup = 1;
  4604. irq_work_queue(&event->pending);
  4605. }
  4606. return ret;
  4607. }
  4608. int perf_event_overflow(struct perf_event *event,
  4609. struct perf_sample_data *data,
  4610. struct pt_regs *regs)
  4611. {
  4612. return __perf_event_overflow(event, 1, data, regs);
  4613. }
  4614. /*
  4615. * Generic software event infrastructure
  4616. */
  4617. struct swevent_htable {
  4618. struct swevent_hlist *swevent_hlist;
  4619. struct mutex hlist_mutex;
  4620. int hlist_refcount;
  4621. /* Recursion avoidance in each contexts */
  4622. int recursion[PERF_NR_CONTEXTS];
  4623. };
  4624. static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
  4625. /*
  4626. * We directly increment event->count and keep a second value in
  4627. * event->hw.period_left to count intervals. This period event
  4628. * is kept in the range [-sample_period, 0] so that we can use the
  4629. * sign as trigger.
  4630. */
  4631. static u64 perf_swevent_set_period(struct perf_event *event)
  4632. {
  4633. struct hw_perf_event *hwc = &event->hw;
  4634. u64 period = hwc->last_period;
  4635. u64 nr, offset;
  4636. s64 old, val;
  4637. hwc->last_period = hwc->sample_period;
  4638. again:
  4639. old = val = local64_read(&hwc->period_left);
  4640. if (val < 0)
  4641. return 0;
  4642. nr = div64_u64(period + val, period);
  4643. offset = nr * period;
  4644. val -= offset;
  4645. if (local64_cmpxchg(&hwc->period_left, old, val) != old)
  4646. goto again;
  4647. return nr;
  4648. }
  4649. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  4650. struct perf_sample_data *data,
  4651. struct pt_regs *regs)
  4652. {
  4653. struct hw_perf_event *hwc = &event->hw;
  4654. int throttle = 0;
  4655. if (!overflow)
  4656. overflow = perf_swevent_set_period(event);
  4657. if (hwc->interrupts == MAX_INTERRUPTS)
  4658. return;
  4659. for (; overflow; overflow--) {
  4660. if (__perf_event_overflow(event, throttle,
  4661. data, regs)) {
  4662. /*
  4663. * We inhibit the overflow from happening when
  4664. * hwc->interrupts == MAX_INTERRUPTS.
  4665. */
  4666. break;
  4667. }
  4668. throttle = 1;
  4669. }
  4670. }
  4671. static void perf_swevent_event(struct perf_event *event, u64 nr,
  4672. struct perf_sample_data *data,
  4673. struct pt_regs *regs)
  4674. {
  4675. struct hw_perf_event *hwc = &event->hw;
  4676. local64_add(nr, &event->count);
  4677. if (!regs)
  4678. return;
  4679. if (!is_sampling_event(event))
  4680. return;
  4681. if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
  4682. data->period = nr;
  4683. return perf_swevent_overflow(event, 1, data, regs);
  4684. } else
  4685. data->period = event->hw.last_period;
  4686. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  4687. return perf_swevent_overflow(event, 1, data, regs);
  4688. if (local64_add_negative(nr, &hwc->period_left))
  4689. return;
  4690. perf_swevent_overflow(event, 0, data, regs);
  4691. }
  4692. static int perf_exclude_event(struct perf_event *event,
  4693. struct pt_regs *regs)
  4694. {
  4695. if (event->hw.state & PERF_HES_STOPPED)
  4696. return 1;
  4697. if (regs) {
  4698. if (event->attr.exclude_user && user_mode(regs))
  4699. return 1;
  4700. if (event->attr.exclude_kernel && !user_mode(regs))
  4701. return 1;
  4702. }
  4703. return 0;
  4704. }
  4705. static int perf_swevent_match(struct perf_event *event,
  4706. enum perf_type_id type,
  4707. u32 event_id,
  4708. struct perf_sample_data *data,
  4709. struct pt_regs *regs)
  4710. {
  4711. if (event->attr.type != type)
  4712. return 0;
  4713. if (event->attr.config != event_id)
  4714. return 0;
  4715. if (perf_exclude_event(event, regs))
  4716. return 0;
  4717. return 1;
  4718. }
  4719. static inline u64 swevent_hash(u64 type, u32 event_id)
  4720. {
  4721. u64 val = event_id | (type << 32);
  4722. return hash_64(val, SWEVENT_HLIST_BITS);
  4723. }
  4724. static inline struct hlist_head *
  4725. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  4726. {
  4727. u64 hash = swevent_hash(type, event_id);
  4728. return &hlist->heads[hash];
  4729. }
  4730. /* For the read side: events when they trigger */
  4731. static inline struct hlist_head *
  4732. find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
  4733. {
  4734. struct swevent_hlist *hlist;
  4735. hlist = rcu_dereference(swhash->swevent_hlist);
  4736. if (!hlist)
  4737. return NULL;
  4738. return __find_swevent_head(hlist, type, event_id);
  4739. }
  4740. /* For the event head insertion and removal in the hlist */
  4741. static inline struct hlist_head *
  4742. find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
  4743. {
  4744. struct swevent_hlist *hlist;
  4745. u32 event_id = event->attr.config;
  4746. u64 type = event->attr.type;
  4747. /*
  4748. * Event scheduling is always serialized against hlist allocation
  4749. * and release. Which makes the protected version suitable here.
  4750. * The context lock guarantees that.
  4751. */
  4752. hlist = rcu_dereference_protected(swhash->swevent_hlist,
  4753. lockdep_is_held(&event->ctx->lock));
  4754. if (!hlist)
  4755. return NULL;
  4756. return __find_swevent_head(hlist, type, event_id);
  4757. }
  4758. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  4759. u64 nr,
  4760. struct perf_sample_data *data,
  4761. struct pt_regs *regs)
  4762. {
  4763. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  4764. struct perf_event *event;
  4765. struct hlist_node *node;
  4766. struct hlist_head *head;
  4767. rcu_read_lock();
  4768. head = find_swevent_head_rcu(swhash, type, event_id);
  4769. if (!head)
  4770. goto end;
  4771. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  4772. if (perf_swevent_match(event, type, event_id, data, regs))
  4773. perf_swevent_event(event, nr, data, regs);
  4774. }
  4775. end:
  4776. rcu_read_unlock();
  4777. }
  4778. int perf_swevent_get_recursion_context(void)
  4779. {
  4780. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  4781. return get_recursion_context(swhash->recursion);
  4782. }
  4783. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  4784. inline void perf_swevent_put_recursion_context(int rctx)
  4785. {
  4786. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  4787. put_recursion_context(swhash->recursion, rctx);
  4788. }
  4789. void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  4790. {
  4791. struct perf_sample_data data;
  4792. int rctx;
  4793. preempt_disable_notrace();
  4794. rctx = perf_swevent_get_recursion_context();
  4795. if (rctx < 0)
  4796. return;
  4797. perf_sample_data_init(&data, addr, 0);
  4798. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
  4799. perf_swevent_put_recursion_context(rctx);
  4800. preempt_enable_notrace();
  4801. }
  4802. static void perf_swevent_read(struct perf_event *event)
  4803. {
  4804. }
  4805. static int perf_swevent_add(struct perf_event *event, int flags)
  4806. {
  4807. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  4808. struct hw_perf_event *hwc = &event->hw;
  4809. struct hlist_head *head;
  4810. if (is_sampling_event(event)) {
  4811. hwc->last_period = hwc->sample_period;
  4812. perf_swevent_set_period(event);
  4813. }
  4814. hwc->state = !(flags & PERF_EF_START);
  4815. head = find_swevent_head(swhash, event);
  4816. if (WARN_ON_ONCE(!head))
  4817. return -EINVAL;
  4818. hlist_add_head_rcu(&event->hlist_entry, head);
  4819. return 0;
  4820. }
  4821. static void perf_swevent_del(struct perf_event *event, int flags)
  4822. {
  4823. hlist_del_rcu(&event->hlist_entry);
  4824. }
  4825. static void perf_swevent_start(struct perf_event *event, int flags)
  4826. {
  4827. event->hw.state = 0;
  4828. }
  4829. static void perf_swevent_stop(struct perf_event *event, int flags)
  4830. {
  4831. event->hw.state = PERF_HES_STOPPED;
  4832. }
  4833. /* Deref the hlist from the update side */
  4834. static inline struct swevent_hlist *
  4835. swevent_hlist_deref(struct swevent_htable *swhash)
  4836. {
  4837. return rcu_dereference_protected(swhash->swevent_hlist,
  4838. lockdep_is_held(&swhash->hlist_mutex));
  4839. }
  4840. static void swevent_hlist_release(struct swevent_htable *swhash)
  4841. {
  4842. struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
  4843. if (!hlist)
  4844. return;
  4845. rcu_assign_pointer(swhash->swevent_hlist, NULL);
  4846. kfree_rcu(hlist, rcu_head);
  4847. }
  4848. static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
  4849. {
  4850. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  4851. mutex_lock(&swhash->hlist_mutex);
  4852. if (!--swhash->hlist_refcount)
  4853. swevent_hlist_release(swhash);
  4854. mutex_unlock(&swhash->hlist_mutex);
  4855. }
  4856. static void swevent_hlist_put(struct perf_event *event)
  4857. {
  4858. int cpu;
  4859. if (event->cpu != -1) {
  4860. swevent_hlist_put_cpu(event, event->cpu);
  4861. return;
  4862. }
  4863. for_each_possible_cpu(cpu)
  4864. swevent_hlist_put_cpu(event, cpu);
  4865. }
  4866. static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
  4867. {
  4868. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  4869. int err = 0;
  4870. mutex_lock(&swhash->hlist_mutex);
  4871. if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
  4872. struct swevent_hlist *hlist;
  4873. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  4874. if (!hlist) {
  4875. err = -ENOMEM;
  4876. goto exit;
  4877. }
  4878. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  4879. }
  4880. swhash->hlist_refcount++;
  4881. exit:
  4882. mutex_unlock(&swhash->hlist_mutex);
  4883. return err;
  4884. }
  4885. static int swevent_hlist_get(struct perf_event *event)
  4886. {
  4887. int err;
  4888. int cpu, failed_cpu;
  4889. if (event->cpu != -1)
  4890. return swevent_hlist_get_cpu(event, event->cpu);
  4891. get_online_cpus();
  4892. for_each_possible_cpu(cpu) {
  4893. err = swevent_hlist_get_cpu(event, cpu);
  4894. if (err) {
  4895. failed_cpu = cpu;
  4896. goto fail;
  4897. }
  4898. }
  4899. put_online_cpus();
  4900. return 0;
  4901. fail:
  4902. for_each_possible_cpu(cpu) {
  4903. if (cpu == failed_cpu)
  4904. break;
  4905. swevent_hlist_put_cpu(event, cpu);
  4906. }
  4907. put_online_cpus();
  4908. return err;
  4909. }
  4910. struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
  4911. static void sw_perf_event_destroy(struct perf_event *event)
  4912. {
  4913. u64 event_id = event->attr.config;
  4914. WARN_ON(event->parent);
  4915. static_key_slow_dec(&perf_swevent_enabled[event_id]);
  4916. swevent_hlist_put(event);
  4917. }
  4918. static int perf_swevent_init(struct perf_event *event)
  4919. {
  4920. u64 event_id = event->attr.config;
  4921. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4922. return -ENOENT;
  4923. /*
  4924. * no branch sampling for software events
  4925. */
  4926. if (has_branch_stack(event))
  4927. return -EOPNOTSUPP;
  4928. switch (event_id) {
  4929. case PERF_COUNT_SW_CPU_CLOCK:
  4930. case PERF_COUNT_SW_TASK_CLOCK:
  4931. return -ENOENT;
  4932. default:
  4933. break;
  4934. }
  4935. if (event_id >= PERF_COUNT_SW_MAX)
  4936. return -ENOENT;
  4937. if (!event->parent) {
  4938. int err;
  4939. err = swevent_hlist_get(event);
  4940. if (err)
  4941. return err;
  4942. static_key_slow_inc(&perf_swevent_enabled[event_id]);
  4943. event->destroy = sw_perf_event_destroy;
  4944. }
  4945. return 0;
  4946. }
  4947. static int perf_swevent_event_idx(struct perf_event *event)
  4948. {
  4949. return 0;
  4950. }
  4951. static struct pmu perf_swevent = {
  4952. .task_ctx_nr = perf_sw_context,
  4953. .capabilities = PERF_PMU_CAP_NO_NMI,
  4954. .event_init = perf_swevent_init,
  4955. .add = perf_swevent_add,
  4956. .del = perf_swevent_del,
  4957. .start = perf_swevent_start,
  4958. .stop = perf_swevent_stop,
  4959. .read = perf_swevent_read,
  4960. .event_idx = perf_swevent_event_idx,
  4961. .events_across_hotplug = 1,
  4962. };
  4963. #ifdef CONFIG_EVENT_TRACING
  4964. static int perf_tp_filter_match(struct perf_event *event,
  4965. struct perf_sample_data *data)
  4966. {
  4967. void *record = data->raw->data;
  4968. /* only top level events have filters set */
  4969. if (event->parent)
  4970. event = event->parent;
  4971. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  4972. return 1;
  4973. return 0;
  4974. }
  4975. static int perf_tp_event_match(struct perf_event *event,
  4976. struct perf_sample_data *data,
  4977. struct pt_regs *regs)
  4978. {
  4979. if (event->hw.state & PERF_HES_STOPPED)
  4980. return 0;
  4981. /*
  4982. * All tracepoints are from kernel-space.
  4983. */
  4984. if (event->attr.exclude_kernel)
  4985. return 0;
  4986. if (!perf_tp_filter_match(event, data))
  4987. return 0;
  4988. return 1;
  4989. }
  4990. void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
  4991. struct pt_regs *regs, struct hlist_head *head, int rctx,
  4992. struct task_struct *task)
  4993. {
  4994. struct perf_sample_data data;
  4995. struct perf_event *event;
  4996. struct hlist_node *node;
  4997. struct perf_raw_record raw = {
  4998. .size = entry_size,
  4999. .data = record,
  5000. };
  5001. perf_sample_data_init(&data, addr, 0);
  5002. data.raw = &raw;
  5003. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  5004. if (perf_tp_event_match(event, &data, regs))
  5005. perf_swevent_event(event, count, &data, regs);
  5006. }
  5007. /*
  5008. * If we got specified a target task, also iterate its context and
  5009. * deliver this event there too.
  5010. */
  5011. if (task && task != current) {
  5012. struct perf_event_context *ctx;
  5013. struct trace_entry *entry = record;
  5014. rcu_read_lock();
  5015. ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
  5016. if (!ctx)
  5017. goto unlock;
  5018. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  5019. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  5020. continue;
  5021. if (event->attr.config != entry->type)
  5022. continue;
  5023. if (perf_tp_event_match(event, &data, regs))
  5024. perf_swevent_event(event, count, &data, regs);
  5025. }
  5026. unlock:
  5027. rcu_read_unlock();
  5028. }
  5029. perf_swevent_put_recursion_context(rctx);
  5030. }
  5031. EXPORT_SYMBOL_GPL(perf_tp_event);
  5032. static void tp_perf_event_destroy(struct perf_event *event)
  5033. {
  5034. perf_trace_destroy(event);
  5035. }
  5036. static int perf_tp_event_init(struct perf_event *event)
  5037. {
  5038. int err;
  5039. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  5040. return -ENOENT;
  5041. /*
  5042. * no branch sampling for tracepoint events
  5043. */
  5044. if (has_branch_stack(event))
  5045. return -EOPNOTSUPP;
  5046. err = perf_trace_init(event);
  5047. if (err)
  5048. return err;
  5049. event->destroy = tp_perf_event_destroy;
  5050. return 0;
  5051. }
  5052. static struct pmu perf_tracepoint = {
  5053. .task_ctx_nr = perf_sw_context,
  5054. .event_init = perf_tp_event_init,
  5055. .add = perf_trace_add,
  5056. .del = perf_trace_del,
  5057. .start = perf_swevent_start,
  5058. .stop = perf_swevent_stop,
  5059. .read = perf_swevent_read,
  5060. .event_idx = perf_swevent_event_idx,
  5061. .events_across_hotplug = 1,
  5062. };
  5063. static inline void perf_tp_register(void)
  5064. {
  5065. perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
  5066. }
  5067. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  5068. {
  5069. char *filter_str;
  5070. int ret;
  5071. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  5072. return -EINVAL;
  5073. filter_str = strndup_user(arg, PAGE_SIZE);
  5074. if (IS_ERR(filter_str))
  5075. return PTR_ERR(filter_str);
  5076. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  5077. kfree(filter_str);
  5078. return ret;
  5079. }
  5080. static void perf_event_free_filter(struct perf_event *event)
  5081. {
  5082. ftrace_profile_free_filter(event);
  5083. }
  5084. #else
  5085. static inline void perf_tp_register(void)
  5086. {
  5087. }
  5088. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  5089. {
  5090. return -ENOENT;
  5091. }
  5092. static void perf_event_free_filter(struct perf_event *event)
  5093. {
  5094. }
  5095. #endif /* CONFIG_EVENT_TRACING */
  5096. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  5097. void perf_bp_event(struct perf_event *bp, void *data)
  5098. {
  5099. struct perf_sample_data sample;
  5100. struct pt_regs *regs = data;
  5101. perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
  5102. if (!bp->hw.state && !perf_exclude_event(bp, regs))
  5103. perf_swevent_event(bp, 1, &sample, regs);
  5104. }
  5105. #endif
  5106. /*
  5107. * hrtimer based swevent callback
  5108. */
  5109. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  5110. {
  5111. enum hrtimer_restart ret = HRTIMER_RESTART;
  5112. struct perf_sample_data data;
  5113. struct pt_regs *regs;
  5114. struct perf_event *event;
  5115. u64 period;
  5116. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  5117. if (event->state != PERF_EVENT_STATE_ACTIVE)
  5118. return HRTIMER_NORESTART;
  5119. event->pmu->read(event);
  5120. perf_sample_data_init(&data, 0, event->hw.last_period);
  5121. regs = get_irq_regs();
  5122. if (regs && !perf_exclude_event(event, regs)) {
  5123. if (!(event->attr.exclude_idle && is_idle_task(current)))
  5124. if (perf_event_overflow(event, &data, regs))
  5125. ret = HRTIMER_NORESTART;
  5126. }
  5127. period = max_t(u64, 10000, event->hw.sample_period);
  5128. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  5129. return ret;
  5130. }
  5131. static void perf_swevent_start_hrtimer(struct perf_event *event)
  5132. {
  5133. struct hw_perf_event *hwc = &event->hw;
  5134. s64 period;
  5135. if (!is_sampling_event(event))
  5136. return;
  5137. period = local64_read(&hwc->period_left);
  5138. if (period) {
  5139. if (period < 0)
  5140. period = 10000;
  5141. local64_set(&hwc->period_left, 0);
  5142. } else {
  5143. period = max_t(u64, 10000, hwc->sample_period);
  5144. }
  5145. __hrtimer_start_range_ns(&hwc->hrtimer,
  5146. ns_to_ktime(period), 0,
  5147. HRTIMER_MODE_REL_PINNED, 0);
  5148. }
  5149. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  5150. {
  5151. struct hw_perf_event *hwc = &event->hw;
  5152. if (is_sampling_event(event)) {
  5153. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  5154. local64_set(&hwc->period_left, ktime_to_ns(remaining));
  5155. hrtimer_cancel(&hwc->hrtimer);
  5156. }
  5157. }
  5158. static void perf_swevent_init_hrtimer(struct perf_event *event)
  5159. {
  5160. struct hw_perf_event *hwc = &event->hw;
  5161. if (!is_sampling_event(event))
  5162. return;
  5163. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  5164. hwc->hrtimer.function = perf_swevent_hrtimer;
  5165. /*
  5166. * Since hrtimers have a fixed rate, we can do a static freq->period
  5167. * mapping and avoid the whole period adjust feedback stuff.
  5168. */
  5169. if (event->attr.freq) {
  5170. long freq = event->attr.sample_freq;
  5171. event->attr.sample_period = NSEC_PER_SEC / freq;
  5172. hwc->sample_period = event->attr.sample_period;
  5173. local64_set(&hwc->period_left, hwc->sample_period);
  5174. event->attr.freq = 0;
  5175. }
  5176. }
  5177. /*
  5178. * Software event: cpu wall time clock
  5179. */
  5180. static void cpu_clock_event_update(struct perf_event *event)
  5181. {
  5182. s64 prev;
  5183. u64 now;
  5184. now = local_clock();
  5185. prev = local64_xchg(&event->hw.prev_count, now);
  5186. local64_add(now - prev, &event->count);
  5187. }
  5188. static void cpu_clock_event_start(struct perf_event *event, int flags)
  5189. {
  5190. local64_set(&event->hw.prev_count, local_clock());
  5191. perf_swevent_start_hrtimer(event);
  5192. }
  5193. static void cpu_clock_event_stop(struct perf_event *event, int flags)
  5194. {
  5195. perf_swevent_cancel_hrtimer(event);
  5196. cpu_clock_event_update(event);
  5197. }
  5198. static int cpu_clock_event_add(struct perf_event *event, int flags)
  5199. {
  5200. if (flags & PERF_EF_START)
  5201. cpu_clock_event_start(event, flags);
  5202. return 0;
  5203. }
  5204. static void cpu_clock_event_del(struct perf_event *event, int flags)
  5205. {
  5206. cpu_clock_event_stop(event, flags);
  5207. }
  5208. static void cpu_clock_event_read(struct perf_event *event)
  5209. {
  5210. cpu_clock_event_update(event);
  5211. }
  5212. static int cpu_clock_event_init(struct perf_event *event)
  5213. {
  5214. if (event->attr.type != PERF_TYPE_SOFTWARE)
  5215. return -ENOENT;
  5216. if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
  5217. return -ENOENT;
  5218. /*
  5219. * no branch sampling for software events
  5220. */
  5221. if (has_branch_stack(event))
  5222. return -EOPNOTSUPP;
  5223. perf_swevent_init_hrtimer(event);
  5224. return 0;
  5225. }
  5226. static struct pmu perf_cpu_clock = {
  5227. .task_ctx_nr = perf_sw_context,
  5228. .capabilities = PERF_PMU_CAP_NO_NMI,
  5229. .event_init = cpu_clock_event_init,
  5230. .add = cpu_clock_event_add,
  5231. .del = cpu_clock_event_del,
  5232. .start = cpu_clock_event_start,
  5233. .stop = cpu_clock_event_stop,
  5234. .read = cpu_clock_event_read,
  5235. .event_idx = perf_swevent_event_idx,
  5236. .events_across_hotplug = 1,
  5237. };
  5238. /*
  5239. * Software event: task time clock
  5240. */
  5241. static void task_clock_event_update(struct perf_event *event, u64 now)
  5242. {
  5243. u64 prev;
  5244. s64 delta;
  5245. prev = local64_xchg(&event->hw.prev_count, now);
  5246. delta = now - prev;
  5247. local64_add(delta, &event->count);
  5248. }
  5249. static void task_clock_event_start(struct perf_event *event, int flags)
  5250. {
  5251. local64_set(&event->hw.prev_count, event->ctx->time);
  5252. perf_swevent_start_hrtimer(event);
  5253. }
  5254. static void task_clock_event_stop(struct perf_event *event, int flags)
  5255. {
  5256. perf_swevent_cancel_hrtimer(event);
  5257. task_clock_event_update(event, event->ctx->time);
  5258. }
  5259. static int task_clock_event_add(struct perf_event *event, int flags)
  5260. {
  5261. if (flags & PERF_EF_START)
  5262. task_clock_event_start(event, flags);
  5263. return 0;
  5264. }
  5265. static void task_clock_event_del(struct perf_event *event, int flags)
  5266. {
  5267. task_clock_event_stop(event, PERF_EF_UPDATE);
  5268. }
  5269. static void task_clock_event_read(struct perf_event *event)
  5270. {
  5271. u64 now = perf_clock();
  5272. u64 delta = now - event->ctx->timestamp;
  5273. u64 time = event->ctx->time + delta;
  5274. task_clock_event_update(event, time);
  5275. }
  5276. static int task_clock_event_init(struct perf_event *event)
  5277. {
  5278. if (event->attr.type != PERF_TYPE_SOFTWARE)
  5279. return -ENOENT;
  5280. if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
  5281. return -ENOENT;
  5282. /*
  5283. * no branch sampling for software events
  5284. */
  5285. if (has_branch_stack(event))
  5286. return -EOPNOTSUPP;
  5287. perf_swevent_init_hrtimer(event);
  5288. return 0;
  5289. }
  5290. static struct pmu perf_task_clock = {
  5291. .task_ctx_nr = perf_sw_context,
  5292. .capabilities = PERF_PMU_CAP_NO_NMI,
  5293. .event_init = task_clock_event_init,
  5294. .add = task_clock_event_add,
  5295. .del = task_clock_event_del,
  5296. .start = task_clock_event_start,
  5297. .stop = task_clock_event_stop,
  5298. .read = task_clock_event_read,
  5299. .event_idx = perf_swevent_event_idx,
  5300. .events_across_hotplug = 1,
  5301. };
  5302. static void perf_pmu_nop_void(struct pmu *pmu)
  5303. {
  5304. }
  5305. static int perf_pmu_nop_int(struct pmu *pmu)
  5306. {
  5307. return 0;
  5308. }
  5309. static void perf_pmu_start_txn(struct pmu *pmu)
  5310. {
  5311. perf_pmu_disable(pmu);
  5312. }
  5313. static int perf_pmu_commit_txn(struct pmu *pmu)
  5314. {
  5315. perf_pmu_enable(pmu);
  5316. return 0;
  5317. }
  5318. static void perf_pmu_cancel_txn(struct pmu *pmu)
  5319. {
  5320. perf_pmu_enable(pmu);
  5321. }
  5322. static int perf_event_idx_default(struct perf_event *event)
  5323. {
  5324. return event->hw.idx + 1;
  5325. }
  5326. /*
  5327. * Ensures all contexts with the same task_ctx_nr have the same
  5328. * pmu_cpu_context too.
  5329. */
  5330. static void *find_pmu_context(int ctxn)
  5331. {
  5332. struct pmu *pmu;
  5333. if (ctxn < 0)
  5334. return NULL;
  5335. list_for_each_entry(pmu, &pmus, entry) {
  5336. if (pmu->task_ctx_nr == ctxn)
  5337. return pmu->pmu_cpu_context;
  5338. }
  5339. return NULL;
  5340. }
  5341. static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
  5342. {
  5343. int cpu;
  5344. for_each_possible_cpu(cpu) {
  5345. struct perf_cpu_context *cpuctx;
  5346. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  5347. if (cpuctx->unique_pmu == old_pmu)
  5348. cpuctx->unique_pmu = pmu;
  5349. }
  5350. }
  5351. static void free_pmu_context(struct pmu *pmu)
  5352. {
  5353. struct pmu *i;
  5354. mutex_lock(&pmus_lock);
  5355. /*
  5356. * Like a real lame refcount.
  5357. */
  5358. list_for_each_entry(i, &pmus, entry) {
  5359. if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
  5360. update_pmu_context(i, pmu);
  5361. goto out;
  5362. }
  5363. }
  5364. free_percpu(pmu->pmu_cpu_context);
  5365. out:
  5366. mutex_unlock(&pmus_lock);
  5367. }
  5368. static struct idr pmu_idr;
  5369. static ssize_t
  5370. type_show(struct device *dev, struct device_attribute *attr, char *page)
  5371. {
  5372. struct pmu *pmu = dev_get_drvdata(dev);
  5373. return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
  5374. }
  5375. static struct device_attribute pmu_dev_attrs[] = {
  5376. __ATTR_RO(type),
  5377. __ATTR_NULL,
  5378. };
  5379. static int pmu_bus_running;
  5380. static struct bus_type pmu_bus = {
  5381. .name = "event_source",
  5382. .dev_attrs = pmu_dev_attrs,
  5383. };
  5384. static void pmu_dev_release(struct device *dev)
  5385. {
  5386. kfree(dev);
  5387. }
  5388. static int pmu_dev_alloc(struct pmu *pmu)
  5389. {
  5390. int ret = -ENOMEM;
  5391. pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  5392. if (!pmu->dev)
  5393. goto out;
  5394. pmu->dev->groups = pmu->attr_groups;
  5395. device_initialize(pmu->dev);
  5396. ret = dev_set_name(pmu->dev, "%s", pmu->name);
  5397. if (ret)
  5398. goto free_dev;
  5399. dev_set_drvdata(pmu->dev, pmu);
  5400. pmu->dev->bus = &pmu_bus;
  5401. pmu->dev->release = pmu_dev_release;
  5402. ret = device_add(pmu->dev);
  5403. if (ret)
  5404. goto free_dev;
  5405. out:
  5406. return ret;
  5407. free_dev:
  5408. put_device(pmu->dev);
  5409. goto out;
  5410. }
  5411. static struct lock_class_key cpuctx_mutex;
  5412. static struct lock_class_key cpuctx_lock;
  5413. int perf_pmu_register(struct pmu *pmu, char *name, int type)
  5414. {
  5415. int cpu, ret;
  5416. mutex_lock(&pmus_lock);
  5417. ret = -ENOMEM;
  5418. pmu->pmu_disable_count = alloc_percpu(int);
  5419. if (!pmu->pmu_disable_count)
  5420. goto unlock;
  5421. pmu->type = -1;
  5422. if (!name)
  5423. goto skip_type;
  5424. pmu->name = name;
  5425. if (type < 0) {
  5426. int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
  5427. if (!err)
  5428. goto free_pdc;
  5429. err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
  5430. if (err) {
  5431. ret = err;
  5432. goto free_pdc;
  5433. }
  5434. }
  5435. pmu->type = type;
  5436. if (pmu_bus_running) {
  5437. ret = pmu_dev_alloc(pmu);
  5438. if (ret)
  5439. goto free_idr;
  5440. }
  5441. skip_type:
  5442. pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
  5443. if (pmu->pmu_cpu_context)
  5444. goto got_cpu_context;
  5445. ret = -ENOMEM;
  5446. pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
  5447. if (!pmu->pmu_cpu_context)
  5448. goto free_dev;
  5449. for_each_possible_cpu(cpu) {
  5450. struct perf_cpu_context *cpuctx;
  5451. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  5452. __perf_event_init_context(&cpuctx->ctx);
  5453. lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
  5454. lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
  5455. cpuctx->ctx.pmu = pmu;
  5456. cpuctx->jiffies_interval = 1;
  5457. INIT_LIST_HEAD(&cpuctx->rotation_list);
  5458. cpuctx->unique_pmu = pmu;
  5459. }
  5460. got_cpu_context:
  5461. if (!pmu->start_txn) {
  5462. if (pmu->pmu_enable) {
  5463. /*
  5464. * If we have pmu_enable/pmu_disable calls, install
  5465. * transaction stubs that use that to try and batch
  5466. * hardware accesses.
  5467. */
  5468. pmu->start_txn = perf_pmu_start_txn;
  5469. pmu->commit_txn = perf_pmu_commit_txn;
  5470. pmu->cancel_txn = perf_pmu_cancel_txn;
  5471. } else {
  5472. pmu->start_txn = perf_pmu_nop_void;
  5473. pmu->commit_txn = perf_pmu_nop_int;
  5474. pmu->cancel_txn = perf_pmu_nop_void;
  5475. }
  5476. }
  5477. if (!pmu->pmu_enable) {
  5478. pmu->pmu_enable = perf_pmu_nop_void;
  5479. pmu->pmu_disable = perf_pmu_nop_void;
  5480. }
  5481. if (!pmu->event_idx)
  5482. pmu->event_idx = perf_event_idx_default;
  5483. list_add_rcu(&pmu->entry, &pmus);
  5484. ret = 0;
  5485. unlock:
  5486. mutex_unlock(&pmus_lock);
  5487. return ret;
  5488. free_dev:
  5489. device_del(pmu->dev);
  5490. put_device(pmu->dev);
  5491. free_idr:
  5492. if (pmu->type >= PERF_TYPE_MAX)
  5493. idr_remove(&pmu_idr, pmu->type);
  5494. free_pdc:
  5495. free_percpu(pmu->pmu_disable_count);
  5496. goto unlock;
  5497. }
  5498. void perf_pmu_unregister(struct pmu *pmu)
  5499. {
  5500. mutex_lock(&pmus_lock);
  5501. list_del_rcu(&pmu->entry);
  5502. mutex_unlock(&pmus_lock);
  5503. /*
  5504. * We dereference the pmu list under both SRCU and regular RCU, so
  5505. * synchronize against both of those.
  5506. */
  5507. synchronize_srcu(&pmus_srcu);
  5508. synchronize_rcu();
  5509. free_percpu(pmu->pmu_disable_count);
  5510. if (pmu->type >= PERF_TYPE_MAX)
  5511. idr_remove(&pmu_idr, pmu->type);
  5512. device_del(pmu->dev);
  5513. put_device(pmu->dev);
  5514. free_pmu_context(pmu);
  5515. }
  5516. struct pmu *perf_init_event(struct perf_event *event)
  5517. {
  5518. struct pmu *pmu = NULL;
  5519. int idx;
  5520. int ret;
  5521. idx = srcu_read_lock(&pmus_srcu);
  5522. rcu_read_lock();
  5523. pmu = idr_find(&pmu_idr, event->attr.type);
  5524. rcu_read_unlock();
  5525. if (pmu) {
  5526. event->pmu = pmu;
  5527. ret = pmu->event_init(event);
  5528. if (ret)
  5529. pmu = ERR_PTR(ret);
  5530. goto unlock;
  5531. }
  5532. list_for_each_entry_rcu(pmu, &pmus, entry) {
  5533. event->pmu = pmu;
  5534. ret = pmu->event_init(event);
  5535. if (!ret)
  5536. goto unlock;
  5537. if (ret != -ENOENT) {
  5538. pmu = ERR_PTR(ret);
  5539. goto unlock;
  5540. }
  5541. }
  5542. pmu = ERR_PTR(-ENOENT);
  5543. unlock:
  5544. srcu_read_unlock(&pmus_srcu, idx);
  5545. return pmu;
  5546. }
  5547. /*
  5548. * Allocate and initialize a event structure
  5549. */
  5550. static struct perf_event *
  5551. perf_event_alloc(struct perf_event_attr *attr, int cpu,
  5552. struct task_struct *task,
  5553. struct perf_event *group_leader,
  5554. struct perf_event *parent_event,
  5555. perf_overflow_handler_t overflow_handler,
  5556. void *context)
  5557. {
  5558. struct pmu *pmu;
  5559. struct perf_event *event;
  5560. struct hw_perf_event *hwc;
  5561. long err;
  5562. if ((unsigned)cpu >= nr_cpu_ids) {
  5563. if (!task || cpu != -1)
  5564. return ERR_PTR(-EINVAL);
  5565. }
  5566. event = kzalloc(sizeof(*event), GFP_KERNEL);
  5567. if (!event)
  5568. return ERR_PTR(-ENOMEM);
  5569. /*
  5570. * Single events are their own group leaders, with an
  5571. * empty sibling list:
  5572. */
  5573. if (!group_leader)
  5574. group_leader = event;
  5575. mutex_init(&event->group_leader_mutex);
  5576. mutex_init(&event->child_mutex);
  5577. INIT_LIST_HEAD(&event->child_list);
  5578. INIT_LIST_HEAD(&event->group_entry);
  5579. INIT_LIST_HEAD(&event->event_entry);
  5580. INIT_LIST_HEAD(&event->sibling_list);
  5581. INIT_LIST_HEAD(&event->rb_entry);
  5582. init_waitqueue_head(&event->waitq);
  5583. init_irq_work(&event->pending, perf_pending_event);
  5584. mutex_init(&event->mmap_mutex);
  5585. atomic_long_set(&event->refcount, 1);
  5586. event->cpu = cpu;
  5587. event->attr = *attr;
  5588. event->group_leader = group_leader;
  5589. event->pmu = NULL;
  5590. event->oncpu = -1;
  5591. event->parent = parent_event;
  5592. event->ns = get_pid_ns(current->nsproxy->pid_ns);
  5593. event->id = atomic64_inc_return(&perf_event_id);
  5594. event->state = PERF_EVENT_STATE_INACTIVE;
  5595. if (task) {
  5596. event->attach_state = PERF_ATTACH_TASK;
  5597. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  5598. /*
  5599. * hw_breakpoint is a bit difficult here..
  5600. */
  5601. if (attr->type == PERF_TYPE_BREAKPOINT)
  5602. event->hw.bp_target = task;
  5603. #endif
  5604. }
  5605. event->clock = &local_clock;
  5606. if (parent_event)
  5607. event->clock = parent_event->clock;
  5608. if (!overflow_handler && parent_event) {
  5609. overflow_handler = parent_event->overflow_handler;
  5610. context = parent_event->overflow_handler_context;
  5611. }
  5612. event->overflow_handler = overflow_handler;
  5613. event->overflow_handler_context = context;
  5614. perf_event__state_init(event);
  5615. pmu = NULL;
  5616. hwc = &event->hw;
  5617. hwc->sample_period = attr->sample_period;
  5618. if (attr->freq && attr->sample_freq)
  5619. hwc->sample_period = 1;
  5620. hwc->last_period = hwc->sample_period;
  5621. local64_set(&hwc->period_left, hwc->sample_period);
  5622. /*
  5623. * we currently do not support PERF_FORMAT_GROUP on inherited events
  5624. */
  5625. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  5626. goto done;
  5627. pmu = perf_init_event(event);
  5628. done:
  5629. err = 0;
  5630. if (!pmu)
  5631. err = -EINVAL;
  5632. else if (IS_ERR(pmu))
  5633. err = PTR_ERR(pmu);
  5634. if (err) {
  5635. if (event->ns)
  5636. put_pid_ns(event->ns);
  5637. kfree(event);
  5638. return ERR_PTR(err);
  5639. }
  5640. if (!event->parent) {
  5641. if (event->attach_state & PERF_ATTACH_TASK)
  5642. static_key_slow_inc(&perf_sched_events.key);
  5643. if (event->attr.mmap || event->attr.mmap_data)
  5644. atomic_inc(&nr_mmap_events);
  5645. if (event->attr.comm)
  5646. atomic_inc(&nr_comm_events);
  5647. if (event->attr.task)
  5648. atomic_inc(&nr_task_events);
  5649. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  5650. err = get_callchain_buffers();
  5651. if (err) {
  5652. free_event(event);
  5653. return ERR_PTR(err);
  5654. }
  5655. }
  5656. if (has_branch_stack(event)) {
  5657. static_key_slow_inc(&perf_sched_events.key);
  5658. if (!(event->attach_state & PERF_ATTACH_TASK))
  5659. atomic_inc(&per_cpu(perf_branch_stack_events,
  5660. event->cpu));
  5661. }
  5662. }
  5663. return event;
  5664. }
  5665. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  5666. struct perf_event_attr *attr)
  5667. {
  5668. u32 size;
  5669. int ret;
  5670. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  5671. return -EFAULT;
  5672. /*
  5673. * zero the full structure, so that a short copy will be nice.
  5674. */
  5675. memset(attr, 0, sizeof(*attr));
  5676. ret = get_user(size, &uattr->size);
  5677. if (ret)
  5678. return ret;
  5679. if (size > PAGE_SIZE) /* silly large */
  5680. goto err_size;
  5681. if (!size) /* abi compat */
  5682. size = PERF_ATTR_SIZE_VER0;
  5683. if (size < PERF_ATTR_SIZE_VER0)
  5684. goto err_size;
  5685. /*
  5686. * If we're handed a bigger struct than we know of,
  5687. * ensure all the unknown bits are 0 - i.e. new
  5688. * user-space does not rely on any kernel feature
  5689. * extensions we dont know about yet.
  5690. */
  5691. if (size > sizeof(*attr)) {
  5692. unsigned char __user *addr;
  5693. unsigned char __user *end;
  5694. unsigned char val;
  5695. addr = (void __user *)uattr + sizeof(*attr);
  5696. end = (void __user *)uattr + size;
  5697. for (; addr < end; addr++) {
  5698. ret = get_user(val, addr);
  5699. if (ret)
  5700. return ret;
  5701. if (val)
  5702. goto err_size;
  5703. }
  5704. size = sizeof(*attr);
  5705. }
  5706. ret = copy_from_user(attr, uattr, size);
  5707. if (ret)
  5708. return -EFAULT;
  5709. if (attr->__reserved_1)
  5710. return -EINVAL;
  5711. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  5712. return -EINVAL;
  5713. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  5714. return -EINVAL;
  5715. if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
  5716. u64 mask = attr->branch_sample_type;
  5717. /* only using defined bits */
  5718. if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
  5719. return -EINVAL;
  5720. /* at least one branch bit must be set */
  5721. if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
  5722. return -EINVAL;
  5723. /* kernel level capture: check permissions */
  5724. if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
  5725. && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  5726. return -EACCES;
  5727. /* propagate priv level, when not set for branch */
  5728. if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
  5729. /* exclude_kernel checked on syscall entry */
  5730. if (!attr->exclude_kernel)
  5731. mask |= PERF_SAMPLE_BRANCH_KERNEL;
  5732. if (!attr->exclude_user)
  5733. mask |= PERF_SAMPLE_BRANCH_USER;
  5734. if (!attr->exclude_hv)
  5735. mask |= PERF_SAMPLE_BRANCH_HV;
  5736. /*
  5737. * adjust user setting (for HW filter setup)
  5738. */
  5739. attr->branch_sample_type = mask;
  5740. }
  5741. }
  5742. if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
  5743. ret = perf_reg_validate(attr->sample_regs_user);
  5744. if (ret)
  5745. return ret;
  5746. }
  5747. if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
  5748. if (!arch_perf_have_user_stack_dump())
  5749. return -ENOSYS;
  5750. /*
  5751. * We have __u32 type for the size, but so far
  5752. * we can only use __u16 as maximum due to the
  5753. * __u16 sample size limit.
  5754. */
  5755. if (attr->sample_stack_user >= USHRT_MAX)
  5756. ret = -EINVAL;
  5757. else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
  5758. ret = -EINVAL;
  5759. }
  5760. if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
  5761. ret = perf_reg_validate(attr->sample_regs_intr);
  5762. out:
  5763. return ret;
  5764. err_size:
  5765. put_user(sizeof(*attr), &uattr->size);
  5766. ret = -E2BIG;
  5767. goto out;
  5768. }
  5769. static int
  5770. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  5771. {
  5772. struct ring_buffer *rb = NULL;
  5773. int ret = -EINVAL;
  5774. if (!output_event)
  5775. goto set;
  5776. /* don't allow circular references */
  5777. if (event == output_event)
  5778. goto out;
  5779. /*
  5780. * Don't allow cross-cpu buffers
  5781. */
  5782. if (output_event->cpu != event->cpu)
  5783. goto out;
  5784. /*
  5785. * If its not a per-cpu rb, it must be the same task.
  5786. */
  5787. if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  5788. goto out;
  5789. /*
  5790. * If both events generate aux data, they must be on the same PMU
  5791. */
  5792. if (has_aux(event) && has_aux(output_event) &&
  5793. event->pmu != output_event->pmu)
  5794. goto out;
  5795. /*
  5796. * Mixing clocks in the same buffer is trouble you don't need.
  5797. */
  5798. if (output_event->clock != event->clock)
  5799. goto out;
  5800. set:
  5801. mutex_lock(&event->mmap_mutex);
  5802. /* Can't redirect output if we've got an active mmap() */
  5803. if (atomic_read(&event->mmap_count))
  5804. goto unlock;
  5805. if (output_event) {
  5806. /* get the rb we want to redirect to */
  5807. rb = ring_buffer_get(output_event);
  5808. if (!rb)
  5809. goto unlock;
  5810. }
  5811. ring_buffer_attach(event, rb);
  5812. ret = 0;
  5813. unlock:
  5814. mutex_unlock(&event->mmap_mutex);
  5815. out:
  5816. return ret;
  5817. }
  5818. static void mutex_lock_double(struct mutex *a, struct mutex *b)
  5819. {
  5820. if (b < a)
  5821. swap(a, b);
  5822. mutex_lock(a);
  5823. mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
  5824. }
  5825. /*
  5826. * Variation on perf_event_ctx_lock_nested(), except we take two context
  5827. * mutexes.
  5828. */
  5829. static struct perf_event_context *
  5830. __perf_event_ctx_lock_double(struct perf_event *group_leader,
  5831. struct perf_event_context *ctx)
  5832. {
  5833. struct perf_event_context *gctx;
  5834. again:
  5835. rcu_read_lock();
  5836. gctx = ACCESS_ONCE(group_leader->ctx);
  5837. if (!atomic_inc_not_zero(&gctx->refcount)) {
  5838. rcu_read_unlock();
  5839. goto again;
  5840. }
  5841. rcu_read_unlock();
  5842. mutex_lock_double(&gctx->mutex, &ctx->mutex);
  5843. if (group_leader->ctx != gctx) {
  5844. mutex_unlock(&ctx->mutex);
  5845. mutex_unlock(&gctx->mutex);
  5846. put_ctx(gctx);
  5847. goto again;
  5848. }
  5849. return gctx;
  5850. }
  5851. #if 0
  5852. static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
  5853. {
  5854. bool nmi_safe = false;
  5855. switch (clk_id) {
  5856. case CLOCK_MONOTONIC:
  5857. event->clock = &ktime_get_mono_fast_ns;
  5858. nmi_safe = true;
  5859. break;
  5860. case CLOCK_MONOTONIC_RAW:
  5861. event->clock = &ktime_get_raw_fast_ns;
  5862. nmi_safe = true;
  5863. break;
  5864. case CLOCK_REALTIME:
  5865. event->clock = &ktime_get_real_ns;
  5866. break;
  5867. case CLOCK_BOOTTIME:
  5868. event->clock = &ktime_get_boot_ns;
  5869. break;
  5870. case CLOCK_TAI:
  5871. event->clock = &ktime_get_tai_ns;
  5872. break;
  5873. default:
  5874. return -EINVAL;
  5875. }
  5876. if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
  5877. return -EINVAL;
  5878. return 0;
  5879. }
  5880. #endif
  5881. /**
  5882. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  5883. *
  5884. * @attr_uptr: event_id type attributes for monitoring/sampling
  5885. * @pid: target pid
  5886. * @cpu: target cpu
  5887. * @group_fd: group leader event fd
  5888. */
  5889. SYSCALL_DEFINE5(perf_event_open,
  5890. struct perf_event_attr __user *, attr_uptr,
  5891. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  5892. {
  5893. struct perf_event *group_leader = NULL, *output_event = NULL;
  5894. struct perf_event *event, *sibling;
  5895. struct perf_event_attr attr;
  5896. struct perf_event_context *ctx, *uninitialized_var(gctx);
  5897. struct file *event_file = NULL;
  5898. struct file *group_file = NULL;
  5899. struct task_struct *task = NULL;
  5900. struct pmu *pmu;
  5901. int event_fd;
  5902. int move_group = 0;
  5903. int fput_needed = 0;
  5904. int err;
  5905. int f_flags = O_RDWR;
  5906. /* for future expandability... */
  5907. if (flags & ~PERF_FLAG_ALL)
  5908. return -EINVAL;
  5909. if (perf_paranoid_any() && !capable(CAP_SYS_ADMIN))
  5910. return -EACCES;
  5911. err = perf_copy_attr(attr_uptr, &attr);
  5912. if (err)
  5913. return err;
  5914. if (attr.constraint_duplicate || attr.__reserved_1)
  5915. return -EINVAL;
  5916. if (!attr.exclude_kernel) {
  5917. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  5918. return -EACCES;
  5919. }
  5920. if (attr.freq) {
  5921. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  5922. return -EINVAL;
  5923. } else {
  5924. if (attr.sample_period & (1ULL << 63))
  5925. return -EINVAL;
  5926. }
  5927. /*
  5928. * In cgroup mode, the pid argument is used to pass the fd
  5929. * opened to the cgroup directory in cgroupfs. The cpu argument
  5930. * designates the cpu on which to monitor threads from that
  5931. * cgroup.
  5932. */
  5933. if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
  5934. return -EINVAL;
  5935. if (flags & PERF_FLAG_FD_CLOEXEC)
  5936. f_flags |= O_CLOEXEC;
  5937. event_fd = get_unused_fd_flags(f_flags);
  5938. if (event_fd < 0)
  5939. return event_fd;
  5940. if (group_fd != -1) {
  5941. group_file = perf_fget_light(group_fd, &fput_needed);
  5942. if (IS_ERR(group_file)) {
  5943. err = PTR_ERR(group_file);
  5944. goto err_fd;
  5945. }
  5946. group_leader = group_file->private_data;
  5947. if (flags & PERF_FLAG_FD_OUTPUT)
  5948. output_event = group_leader;
  5949. if (flags & PERF_FLAG_FD_NO_GROUP)
  5950. group_leader = NULL;
  5951. }
  5952. /*
  5953. * Take the group_leader's group_leader_mutex before observing
  5954. * anything in the group leader that leads to changes in ctx,
  5955. * many of which may be changing on another thread.
  5956. * In particular, we want to take this lock before deciding
  5957. * whether we need to move_group.
  5958. */
  5959. if (group_leader)
  5960. mutex_lock(&group_leader->group_leader_mutex);
  5961. if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
  5962. task = find_lively_task_by_vpid(pid);
  5963. if (IS_ERR(task)) {
  5964. err = PTR_ERR(task);
  5965. goto err_group_fd;
  5966. }
  5967. }
  5968. event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
  5969. NULL, NULL);
  5970. if (IS_ERR(event)) {
  5971. err = PTR_ERR(event);
  5972. goto err_task;
  5973. }
  5974. if (flags & PERF_FLAG_PID_CGROUP) {
  5975. err = perf_cgroup_connect(pid, event, &attr, group_leader);
  5976. if (err)
  5977. goto err_alloc;
  5978. /*
  5979. * one more event:
  5980. * - that has cgroup constraint on event->cpu
  5981. * - that may need work on context switch
  5982. */
  5983. atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
  5984. static_key_slow_inc(&perf_sched_events.key);
  5985. }
  5986. if (is_sampling_event(event)) {
  5987. if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
  5988. err = -ENOTSUPP;
  5989. goto err_alloc;
  5990. }
  5991. }
  5992. /*
  5993. * Special case software events and allow them to be part of
  5994. * any hardware group.
  5995. */
  5996. pmu = event->pmu;
  5997. #if 0
  5998. if (attr.use_clockid) {
  5999. err = perf_event_set_clock(event, attr.clockid);
  6000. if (err)
  6001. goto err_alloc;
  6002. }
  6003. #endif
  6004. if (group_leader &&
  6005. (is_software_event(event) != is_software_event(group_leader))) {
  6006. if (is_software_event(event)) {
  6007. /*
  6008. * If event and group_leader are not both a software
  6009. * event, and event is, then group leader is not.
  6010. *
  6011. * Allow the addition of software events to !software
  6012. * groups, this is safe because software events never
  6013. * fail to schedule.
  6014. */
  6015. pmu = group_leader->pmu;
  6016. } else if (is_software_event(group_leader) &&
  6017. (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
  6018. /*
  6019. * In case the group is a pure software group, and we
  6020. * try to add a hardware event, move the whole group to
  6021. * the hardware context.
  6022. */
  6023. move_group = 1;
  6024. }
  6025. }
  6026. /*
  6027. * Get the target context (task or percpu):
  6028. */
  6029. ctx = find_get_context(pmu, task, event->cpu);
  6030. if (IS_ERR(ctx)) {
  6031. err = PTR_ERR(ctx);
  6032. goto err_alloc;
  6033. }
  6034. if (task) {
  6035. put_task_struct(task);
  6036. task = NULL;
  6037. }
  6038. /*
  6039. * Look up the group leader (we will attach this event to it):
  6040. */
  6041. if (group_leader) {
  6042. err = -EINVAL;
  6043. /*
  6044. * Do not allow a recursive hierarchy (this new sibling
  6045. * becoming part of another group-sibling):
  6046. */
  6047. if (group_leader->group_leader != group_leader)
  6048. goto err_context;
  6049. /* All events in a group should have the same clock */
  6050. if (group_leader->clock != event->clock)
  6051. goto err_context;
  6052. /*
  6053. * Make sure we're both events for the same CPU;
  6054. * grouping events for different CPUs is broken; since
  6055. * you can never concurrently schedule them anyhow.
  6056. */
  6057. if (group_leader->cpu != event->cpu)
  6058. goto err_context;
  6059. /*
  6060. * Make sure we're both on the same task, or both
  6061. * per-CPU events.
  6062. */
  6063. if (group_leader->ctx->task != ctx->task)
  6064. goto err_context;
  6065. /*
  6066. * Do not allow to attach to a group in a different task
  6067. * or CPU context. If we're moving SW events, we'll fix
  6068. * this up later, so allow that.
  6069. */
  6070. if (!move_group && group_leader->ctx != ctx)
  6071. goto err_context;
  6072. /*
  6073. * Only a group leader can be exclusive or pinned
  6074. */
  6075. if (attr.exclusive || attr.pinned)
  6076. goto err_context;
  6077. }
  6078. if (output_event) {
  6079. err = perf_event_set_output(event, output_event);
  6080. if (err)
  6081. goto err_context;
  6082. }
  6083. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
  6084. f_flags);
  6085. if (IS_ERR(event_file)) {
  6086. err = PTR_ERR(event_file);
  6087. goto err_context;
  6088. }
  6089. if (move_group) {
  6090. gctx = __perf_event_ctx_lock_double(group_leader, ctx);
  6091. /*
  6092. * Check if we raced against another sys_perf_event_open() call
  6093. * moving the software group underneath us.
  6094. */
  6095. if (!(group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
  6096. /*
  6097. * If someone moved the group out from under us, check
  6098. * if this new event wound up on the same ctx, if so
  6099. * its the regular !move_group case, otherwise fail.
  6100. */
  6101. if (gctx != ctx) {
  6102. err = -EINVAL;
  6103. goto err_locked;
  6104. } else {
  6105. perf_event_ctx_unlock(group_leader, gctx);
  6106. move_group = 0;
  6107. }
  6108. }
  6109. /*
  6110. * See perf_event_ctx_lock() for comments on the details
  6111. * of swizzling perf_event::ctx.
  6112. */
  6113. perf_remove_from_context(group_leader, false);
  6114. /*
  6115. * Removing from the context ends up with disabled
  6116. * event. What we want here is event in the initial
  6117. * startup state, ready to be add into new context.
  6118. */
  6119. perf_event__state_init(group_leader);
  6120. list_for_each_entry(sibling, &group_leader->sibling_list,
  6121. group_entry) {
  6122. perf_remove_from_context(sibling, false);
  6123. perf_event__state_init(sibling);
  6124. put_ctx(gctx);
  6125. }
  6126. mutex_lock(&ctx->mutex);
  6127. } else {
  6128. mutex_lock(&ctx->mutex);
  6129. }
  6130. WARN_ON_ONCE(ctx->parent_ctx);
  6131. if (move_group) {
  6132. synchronize_rcu();
  6133. perf_install_in_context(ctx, group_leader, event->cpu);
  6134. get_ctx(ctx);
  6135. list_for_each_entry(sibling, &group_leader->sibling_list,
  6136. group_entry) {
  6137. perf_install_in_context(ctx, sibling, event->cpu);
  6138. get_ctx(ctx);
  6139. }
  6140. }
  6141. perf_install_in_context(ctx, event, event->cpu);
  6142. perf_unpin_context(ctx);
  6143. if (move_group) {
  6144. perf_event_ctx_unlock(group_leader, gctx);
  6145. put_ctx(gctx);
  6146. }
  6147. mutex_unlock(&ctx->mutex);
  6148. if (group_leader)
  6149. mutex_unlock(&group_leader->group_leader_mutex);
  6150. event->owner = current;
  6151. mutex_lock(&current->perf_event_mutex);
  6152. list_add_tail(&event->owner_entry, &current->perf_event_list);
  6153. mutex_unlock(&current->perf_event_mutex);
  6154. /*
  6155. * Precalculate sample_data sizes
  6156. */
  6157. perf_event__header_size(event);
  6158. perf_event__id_header_size(event);
  6159. /*
  6160. * Drop the reference on the group_event after placing the
  6161. * new event on the sibling_list. This ensures destruction
  6162. * of the group leader will find the pointer to itself in
  6163. * perf_group_detach().
  6164. */
  6165. fput_light(group_file, fput_needed);
  6166. fd_install(event_fd, event_file);
  6167. return event_fd;
  6168. err_locked:
  6169. if (move_group)
  6170. perf_event_ctx_unlock(group_leader, gctx);
  6171. mutex_unlock(&ctx->mutex);
  6172. fput(event_file);
  6173. err_context:
  6174. perf_unpin_context(ctx);
  6175. put_ctx(ctx);
  6176. err_alloc:
  6177. /*
  6178. * If event_file is set, the fput() above will have called ->release()
  6179. * and that will take care of freeing the event.
  6180. */
  6181. if (!event_file)
  6182. free_event(event);
  6183. err_task:
  6184. if (task)
  6185. put_task_struct(task);
  6186. err_group_fd:
  6187. if (group_leader)
  6188. mutex_unlock(&group_leader->group_leader_mutex);
  6189. fput_light(group_file, fput_needed);
  6190. err_fd:
  6191. put_unused_fd(event_fd);
  6192. return err;
  6193. }
  6194. /**
  6195. * perf_event_create_kernel_counter
  6196. *
  6197. * @attr: attributes of the counter to create
  6198. * @cpu: cpu in which the counter is bound
  6199. * @task: task to profile (NULL for percpu)
  6200. */
  6201. struct perf_event *
  6202. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  6203. struct task_struct *task,
  6204. perf_overflow_handler_t overflow_handler,
  6205. void *context)
  6206. {
  6207. struct perf_event_context *ctx;
  6208. struct perf_event *event;
  6209. int err;
  6210. /*
  6211. * Get the target context (task or percpu):
  6212. */
  6213. event = perf_event_alloc(attr, cpu, task, NULL, NULL,
  6214. overflow_handler, context);
  6215. if (IS_ERR(event)) {
  6216. err = PTR_ERR(event);
  6217. goto err;
  6218. }
  6219. ctx = find_get_context(event->pmu, task, cpu);
  6220. if (IS_ERR(ctx)) {
  6221. err = PTR_ERR(ctx);
  6222. goto err_free;
  6223. }
  6224. WARN_ON_ONCE(ctx->parent_ctx);
  6225. mutex_lock(&ctx->mutex);
  6226. perf_install_in_context(ctx, event, cpu);
  6227. perf_unpin_context(ctx);
  6228. mutex_unlock(&ctx->mutex);
  6229. return event;
  6230. err_free:
  6231. free_event(event);
  6232. err:
  6233. return ERR_PTR(err);
  6234. }
  6235. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  6236. void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
  6237. {
  6238. struct perf_event_context *src_ctx;
  6239. struct perf_event_context *dst_ctx;
  6240. struct perf_event *event, *tmp;
  6241. LIST_HEAD(events);
  6242. src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
  6243. dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
  6244. mutex_lock(&src_ctx->mutex);
  6245. list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
  6246. event_entry) {
  6247. perf_remove_from_context(event, true);
  6248. put_ctx(src_ctx);
  6249. list_add(&event->event_entry, &events);
  6250. }
  6251. mutex_unlock(&src_ctx->mutex);
  6252. synchronize_rcu();
  6253. mutex_lock(&dst_ctx->mutex);
  6254. list_for_each_entry_safe(event, tmp, &events, event_entry) {
  6255. list_del(&event->event_entry);
  6256. if (event->state >= PERF_EVENT_STATE_OFF)
  6257. event->state = PERF_EVENT_STATE_INACTIVE;
  6258. perf_install_in_context(dst_ctx, event, dst_cpu);
  6259. get_ctx(dst_ctx);
  6260. }
  6261. mutex_unlock(&dst_ctx->mutex);
  6262. }
  6263. EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
  6264. static void sync_child_event(struct perf_event *child_event,
  6265. struct task_struct *child)
  6266. {
  6267. struct perf_event *parent_event = child_event->parent;
  6268. u64 child_val;
  6269. if (child_event->attr.inherit_stat)
  6270. perf_event_read_event(child_event, child);
  6271. child_val = perf_event_count(child_event);
  6272. /*
  6273. * Add back the child's count to the parent's count:
  6274. */
  6275. atomic64_add(child_val, &parent_event->child_count);
  6276. atomic64_add(child_event->total_time_enabled,
  6277. &parent_event->child_total_time_enabled);
  6278. atomic64_add(child_event->total_time_running,
  6279. &parent_event->child_total_time_running);
  6280. /*
  6281. * Remove this event from the parent's list
  6282. */
  6283. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  6284. mutex_lock(&parent_event->child_mutex);
  6285. list_del_init(&child_event->child_list);
  6286. mutex_unlock(&parent_event->child_mutex);
  6287. /*
  6288. * Release the parent event, if this was the last
  6289. * reference to it.
  6290. */
  6291. put_event(parent_event);
  6292. }
  6293. static void
  6294. __perf_event_exit_task(struct perf_event *child_event,
  6295. struct perf_event_context *child_ctx,
  6296. struct task_struct *child)
  6297. {
  6298. perf_remove_from_context(child_event, !!child_event->parent);
  6299. /*
  6300. * It can happen that the parent exits first, and has events
  6301. * that are still around due to the child reference. These
  6302. * events need to be zapped.
  6303. */
  6304. if (child_event->parent) {
  6305. sync_child_event(child_event, child);
  6306. free_event(child_event);
  6307. }
  6308. }
  6309. static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
  6310. {
  6311. struct perf_event *child_event, *tmp;
  6312. struct perf_event_context *child_ctx, *clone_ctx = NULL;
  6313. unsigned long flags;
  6314. if (likely(!child->perf_event_ctxp[ctxn])) {
  6315. perf_event_task(child, NULL, 0);
  6316. return;
  6317. }
  6318. local_irq_save(flags);
  6319. /*
  6320. * We can't reschedule here because interrupts are disabled,
  6321. * and either child is current or it is a task that can't be
  6322. * scheduled, so we are now safe from rescheduling changing
  6323. * our context.
  6324. */
  6325. child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
  6326. /*
  6327. * Take the context lock here so that if find_get_context is
  6328. * reading child->perf_event_ctxp, we wait until it has
  6329. * incremented the context's refcount before we do put_ctx below.
  6330. */
  6331. raw_spin_lock(&child_ctx->lock);
  6332. task_ctx_sched_out(child_ctx);
  6333. child->perf_event_ctxp[ctxn] = NULL;
  6334. /*
  6335. * If this context is a clone; unclone it so it can't get
  6336. * swapped to another process while we're removing all
  6337. * the events from it.
  6338. */
  6339. clone_ctx = unclone_ctx(child_ctx);
  6340. update_context_time(child_ctx);
  6341. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  6342. if (clone_ctx)
  6343. put_ctx(clone_ctx);
  6344. /*
  6345. * Report the task dead after unscheduling the events so that we
  6346. * won't get any samples after PERF_RECORD_EXIT. We can however still
  6347. * get a few PERF_RECORD_READ events.
  6348. */
  6349. perf_event_task(child, child_ctx, 0);
  6350. /*
  6351. * We can recurse on the same lock type through:
  6352. *
  6353. * __perf_event_exit_task()
  6354. * sync_child_event()
  6355. * put_event()
  6356. * mutex_lock(&ctx->mutex)
  6357. *
  6358. * But since its the parent context it won't be the same instance.
  6359. */
  6360. mutex_lock(&child_ctx->mutex);
  6361. again:
  6362. list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
  6363. group_entry)
  6364. __perf_event_exit_task(child_event, child_ctx, child);
  6365. list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
  6366. group_entry)
  6367. __perf_event_exit_task(child_event, child_ctx, child);
  6368. /*
  6369. * If the last event was a group event, it will have appended all
  6370. * its siblings to the list, but we obtained 'tmp' before that which
  6371. * will still point to the list head terminating the iteration.
  6372. */
  6373. if (!list_empty(&child_ctx->pinned_groups) ||
  6374. !list_empty(&child_ctx->flexible_groups))
  6375. goto again;
  6376. mutex_unlock(&child_ctx->mutex);
  6377. put_ctx(child_ctx);
  6378. }
  6379. /*
  6380. * When a child task exits, feed back event values to parent events.
  6381. */
  6382. void perf_event_exit_task(struct task_struct *child)
  6383. {
  6384. struct perf_event *event, *tmp;
  6385. int ctxn;
  6386. mutex_lock(&child->perf_event_mutex);
  6387. list_for_each_entry_safe(event, tmp, &child->perf_event_list,
  6388. owner_entry) {
  6389. list_del_init(&event->owner_entry);
  6390. /*
  6391. * Ensure the list deletion is visible before we clear
  6392. * the owner, closes a race against perf_release() where
  6393. * we need to serialize on the owner->perf_event_mutex.
  6394. */
  6395. smp_wmb();
  6396. event->owner = NULL;
  6397. }
  6398. mutex_unlock(&child->perf_event_mutex);
  6399. for_each_task_context_nr(ctxn)
  6400. perf_event_exit_task_context(child, ctxn);
  6401. }
  6402. static void perf_free_event(struct perf_event *event,
  6403. struct perf_event_context *ctx)
  6404. {
  6405. struct perf_event *parent = event->parent;
  6406. if (WARN_ON_ONCE(!parent))
  6407. return;
  6408. mutex_lock(&parent->child_mutex);
  6409. list_del_init(&event->child_list);
  6410. mutex_unlock(&parent->child_mutex);
  6411. put_event(parent);
  6412. perf_group_detach(event);
  6413. list_del_event(event, ctx);
  6414. free_event(event);
  6415. }
  6416. /*
  6417. * free an unexposed, unused context as created by inheritance by
  6418. * perf_event_init_task below, used by fork() in case of fail.
  6419. */
  6420. void perf_event_free_task(struct task_struct *task)
  6421. {
  6422. struct perf_event_context *ctx;
  6423. struct perf_event *event, *tmp;
  6424. int ctxn;
  6425. for_each_task_context_nr(ctxn) {
  6426. ctx = task->perf_event_ctxp[ctxn];
  6427. if (!ctx)
  6428. continue;
  6429. mutex_lock(&ctx->mutex);
  6430. again:
  6431. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
  6432. group_entry)
  6433. perf_free_event(event, ctx);
  6434. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  6435. group_entry)
  6436. perf_free_event(event, ctx);
  6437. if (!list_empty(&ctx->pinned_groups) ||
  6438. !list_empty(&ctx->flexible_groups))
  6439. goto again;
  6440. mutex_unlock(&ctx->mutex);
  6441. put_ctx(ctx);
  6442. }
  6443. }
  6444. void perf_event_delayed_put(struct task_struct *task)
  6445. {
  6446. int ctxn;
  6447. for_each_task_context_nr(ctxn)
  6448. WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
  6449. }
  6450. /*
  6451. * inherit a event from parent task to child task:
  6452. */
  6453. static struct perf_event *
  6454. inherit_event(struct perf_event *parent_event,
  6455. struct task_struct *parent,
  6456. struct perf_event_context *parent_ctx,
  6457. struct task_struct *child,
  6458. struct perf_event *group_leader,
  6459. struct perf_event_context *child_ctx)
  6460. {
  6461. struct perf_event *child_event;
  6462. unsigned long flags;
  6463. /*
  6464. * Instead of creating recursive hierarchies of events,
  6465. * we link inherited events back to the original parent,
  6466. * which has a filp for sure, which we use as the reference
  6467. * count:
  6468. */
  6469. if (parent_event->parent)
  6470. parent_event = parent_event->parent;
  6471. child_event = perf_event_alloc(&parent_event->attr,
  6472. parent_event->cpu,
  6473. child,
  6474. group_leader, parent_event,
  6475. NULL, NULL);
  6476. if (IS_ERR(child_event))
  6477. return child_event;
  6478. if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
  6479. free_event(child_event);
  6480. return NULL;
  6481. }
  6482. get_ctx(child_ctx);
  6483. /*
  6484. * Make the child state follow the state of the parent event,
  6485. * not its attr.disabled bit. We hold the parent's mutex,
  6486. * so we won't race with perf_event_{en, dis}able_family.
  6487. */
  6488. if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
  6489. child_event->state = PERF_EVENT_STATE_INACTIVE;
  6490. else
  6491. child_event->state = PERF_EVENT_STATE_OFF;
  6492. if (parent_event->attr.freq) {
  6493. u64 sample_period = parent_event->hw.sample_period;
  6494. struct hw_perf_event *hwc = &child_event->hw;
  6495. hwc->sample_period = sample_period;
  6496. hwc->last_period = sample_period;
  6497. local64_set(&hwc->period_left, sample_period);
  6498. }
  6499. child_event->ctx = child_ctx;
  6500. child_event->overflow_handler = parent_event->overflow_handler;
  6501. child_event->overflow_handler_context
  6502. = parent_event->overflow_handler_context;
  6503. /*
  6504. * Precalculate sample_data sizes
  6505. */
  6506. perf_event__header_size(child_event);
  6507. perf_event__id_header_size(child_event);
  6508. /*
  6509. * Link it up in the child's context:
  6510. */
  6511. raw_spin_lock_irqsave(&child_ctx->lock, flags);
  6512. add_event_to_ctx(child_event, child_ctx);
  6513. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  6514. /*
  6515. * Link this into the parent event's child list
  6516. */
  6517. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  6518. mutex_lock(&parent_event->child_mutex);
  6519. list_add_tail(&child_event->child_list, &parent_event->child_list);
  6520. mutex_unlock(&parent_event->child_mutex);
  6521. return child_event;
  6522. }
  6523. static int inherit_group(struct perf_event *parent_event,
  6524. struct task_struct *parent,
  6525. struct perf_event_context *parent_ctx,
  6526. struct task_struct *child,
  6527. struct perf_event_context *child_ctx)
  6528. {
  6529. struct perf_event *leader;
  6530. struct perf_event *sub;
  6531. struct perf_event *child_ctr;
  6532. leader = inherit_event(parent_event, parent, parent_ctx,
  6533. child, NULL, child_ctx);
  6534. if (IS_ERR(leader))
  6535. return PTR_ERR(leader);
  6536. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  6537. child_ctr = inherit_event(sub, parent, parent_ctx,
  6538. child, leader, child_ctx);
  6539. if (IS_ERR(child_ctr))
  6540. return PTR_ERR(child_ctr);
  6541. }
  6542. return 0;
  6543. }
  6544. static int
  6545. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  6546. struct perf_event_context *parent_ctx,
  6547. struct task_struct *child, int ctxn,
  6548. int *inherited_all)
  6549. {
  6550. int ret;
  6551. struct perf_event_context *child_ctx;
  6552. if (!event->attr.inherit) {
  6553. *inherited_all = 0;
  6554. return 0;
  6555. }
  6556. child_ctx = child->perf_event_ctxp[ctxn];
  6557. if (!child_ctx) {
  6558. /*
  6559. * This is executed from the parent task context, so
  6560. * inherit events that have been marked for cloning.
  6561. * First allocate and initialize a context for the
  6562. * child.
  6563. */
  6564. child_ctx = alloc_perf_context(parent_ctx->pmu, child);
  6565. if (!child_ctx)
  6566. return -ENOMEM;
  6567. child->perf_event_ctxp[ctxn] = child_ctx;
  6568. }
  6569. ret = inherit_group(event, parent, parent_ctx,
  6570. child, child_ctx);
  6571. if (ret)
  6572. *inherited_all = 0;
  6573. return ret;
  6574. }
  6575. /*
  6576. * Initialize the perf_event context in task_struct
  6577. */
  6578. int perf_event_init_context(struct task_struct *child, int ctxn)
  6579. {
  6580. struct perf_event_context *child_ctx, *parent_ctx;
  6581. struct perf_event_context *cloned_ctx;
  6582. struct perf_event *event;
  6583. struct task_struct *parent = current;
  6584. int inherited_all = 1;
  6585. unsigned long flags;
  6586. int ret = 0;
  6587. if (likely(!parent->perf_event_ctxp[ctxn]))
  6588. return 0;
  6589. /*
  6590. * If the parent's context is a clone, pin it so it won't get
  6591. * swapped under us.
  6592. */
  6593. parent_ctx = perf_pin_task_context(parent, ctxn);
  6594. /*
  6595. * No need to check if parent_ctx != NULL here; since we saw
  6596. * it non-NULL earlier, the only reason for it to become NULL
  6597. * is if we exit, and since we're currently in the middle of
  6598. * a fork we can't be exiting at the same time.
  6599. */
  6600. /*
  6601. * Lock the parent list. No need to lock the child - not PID
  6602. * hashed yet and not running, so nobody can access it.
  6603. */
  6604. mutex_lock(&parent_ctx->mutex);
  6605. /*
  6606. * We dont have to disable NMIs - we are only looking at
  6607. * the list, not manipulating it:
  6608. */
  6609. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  6610. ret = inherit_task_group(event, parent, parent_ctx,
  6611. child, ctxn, &inherited_all);
  6612. if (ret)
  6613. break;
  6614. }
  6615. /*
  6616. * We can't hold ctx->lock when iterating the ->flexible_group list due
  6617. * to allocations, but we need to prevent rotation because
  6618. * rotate_ctx() will change the list from interrupt context.
  6619. */
  6620. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  6621. parent_ctx->rotate_disable = 1;
  6622. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  6623. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  6624. ret = inherit_task_group(event, parent, parent_ctx,
  6625. child, ctxn, &inherited_all);
  6626. if (ret)
  6627. break;
  6628. }
  6629. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  6630. parent_ctx->rotate_disable = 0;
  6631. child_ctx = child->perf_event_ctxp[ctxn];
  6632. if (child_ctx && inherited_all) {
  6633. /*
  6634. * Mark the child context as a clone of the parent
  6635. * context, or of whatever the parent is a clone of.
  6636. *
  6637. * Note that if the parent is a clone, the holding of
  6638. * parent_ctx->lock avoids it from being uncloned.
  6639. */
  6640. cloned_ctx = parent_ctx->parent_ctx;
  6641. if (cloned_ctx) {
  6642. child_ctx->parent_ctx = cloned_ctx;
  6643. child_ctx->parent_gen = parent_ctx->parent_gen;
  6644. } else {
  6645. child_ctx->parent_ctx = parent_ctx;
  6646. child_ctx->parent_gen = parent_ctx->generation;
  6647. }
  6648. get_ctx(child_ctx->parent_ctx);
  6649. }
  6650. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  6651. mutex_unlock(&parent_ctx->mutex);
  6652. perf_unpin_context(parent_ctx);
  6653. put_ctx(parent_ctx);
  6654. return ret;
  6655. }
  6656. /*
  6657. * Initialize the perf_event context in task_struct
  6658. */
  6659. int perf_event_init_task(struct task_struct *child)
  6660. {
  6661. int ctxn, ret;
  6662. memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
  6663. mutex_init(&child->perf_event_mutex);
  6664. INIT_LIST_HEAD(&child->perf_event_list);
  6665. for_each_task_context_nr(ctxn) {
  6666. ret = perf_event_init_context(child, ctxn);
  6667. if (ret) {
  6668. perf_event_free_task(child);
  6669. return ret;
  6670. }
  6671. }
  6672. return 0;
  6673. }
  6674. static void __init perf_event_init_all_cpus(void)
  6675. {
  6676. struct swevent_htable *swhash;
  6677. int cpu;
  6678. for_each_possible_cpu(cpu) {
  6679. swhash = &per_cpu(swevent_htable, cpu);
  6680. mutex_init(&swhash->hlist_mutex);
  6681. INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
  6682. }
  6683. }
  6684. static void __cpuinit perf_event_init_cpu(int cpu)
  6685. {
  6686. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  6687. mutex_lock(&swhash->hlist_mutex);
  6688. if (swhash->hlist_refcount > 0) {
  6689. struct swevent_hlist *hlist;
  6690. hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
  6691. WARN_ON(!hlist);
  6692. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  6693. }
  6694. mutex_unlock(&swhash->hlist_mutex);
  6695. }
  6696. #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
  6697. static void perf_pmu_rotate_stop(struct pmu *pmu)
  6698. {
  6699. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  6700. WARN_ON(!irqs_disabled());
  6701. list_del_init(&cpuctx->rotation_list);
  6702. }
  6703. static void __perf_event_exit_context(void *__info)
  6704. {
  6705. struct remove_event re = { .detach_group = false };
  6706. struct perf_event_context *ctx = __info;
  6707. perf_pmu_rotate_stop(ctx->pmu);
  6708. rcu_read_lock();
  6709. list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
  6710. __perf_remove_from_context(&re);
  6711. rcu_read_unlock();
  6712. }
  6713. static void perf_event_exit_cpu_context(int cpu)
  6714. {
  6715. struct perf_event_context *ctx;
  6716. struct pmu *pmu;
  6717. int idx;
  6718. idx = srcu_read_lock(&pmus_srcu);
  6719. list_for_each_entry_rcu(pmu, &pmus, entry) {
  6720. /*
  6721. * If keeping events across hotplugging is supported, do not
  6722. * remove the event list, but keep it alive across CPU hotplug.
  6723. * The context is exited via an fd close path when userspace
  6724. * is done and the target CPU is online.
  6725. */
  6726. if (!pmu->events_across_hotplug) {
  6727. ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
  6728. mutex_lock(&ctx->mutex);
  6729. smp_call_function_single(cpu, __perf_event_exit_context,
  6730. ctx, 1);
  6731. mutex_unlock(&ctx->mutex);
  6732. }
  6733. }
  6734. srcu_read_unlock(&pmus_srcu, idx);
  6735. }
  6736. static void perf_event_exit_cpu(int cpu)
  6737. {
  6738. perf_event_exit_cpu_context(cpu);
  6739. }
  6740. #else
  6741. static inline void perf_event_exit_cpu(int cpu) { }
  6742. #endif
  6743. static int
  6744. perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
  6745. {
  6746. int cpu;
  6747. for_each_online_cpu(cpu)
  6748. perf_event_exit_cpu(cpu);
  6749. return NOTIFY_OK;
  6750. }
  6751. /*
  6752. * Run the perf reboot notifier at the very last possible moment so that
  6753. * the generic watchdog code runs as long as possible.
  6754. */
  6755. static struct notifier_block perf_reboot_notifier = {
  6756. .notifier_call = perf_reboot,
  6757. .priority = INT_MIN,
  6758. };
  6759. static int __cpuinit
  6760. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  6761. {
  6762. unsigned int cpu = (long)hcpu;
  6763. switch (action & ~CPU_TASKS_FROZEN) {
  6764. case CPU_UP_PREPARE:
  6765. case CPU_DOWN_FAILED:
  6766. perf_event_init_cpu(cpu);
  6767. break;
  6768. case CPU_UP_CANCELED:
  6769. case CPU_DOWN_PREPARE:
  6770. perf_event_exit_cpu(cpu);
  6771. break;
  6772. default:
  6773. break;
  6774. }
  6775. return NOTIFY_OK;
  6776. }
  6777. void __init perf_event_init(void)
  6778. {
  6779. int ret;
  6780. idr_init(&pmu_idr);
  6781. perf_event_init_all_cpus();
  6782. init_srcu_struct(&pmus_srcu);
  6783. perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
  6784. perf_pmu_register(&perf_cpu_clock, NULL, -1);
  6785. perf_pmu_register(&perf_task_clock, NULL, -1);
  6786. perf_tp_register();
  6787. perf_cpu_notifier(perf_cpu_notify);
  6788. register_reboot_notifier(&perf_reboot_notifier);
  6789. ret = init_hw_breakpoint();
  6790. WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
  6791. /* do not patch jump label more than once per second */
  6792. jump_label_rate_limit(&perf_sched_events, HZ);
  6793. /*
  6794. * Build time assertion that we keep the data_head at the intended
  6795. * location. IOW, validation we got the __reserved[] size right.
  6796. */
  6797. BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
  6798. != 1024);
  6799. }
  6800. static int __init perf_event_sysfs_init(void)
  6801. {
  6802. struct pmu *pmu;
  6803. int ret;
  6804. mutex_lock(&pmus_lock);
  6805. ret = bus_register(&pmu_bus);
  6806. if (ret)
  6807. goto unlock;
  6808. list_for_each_entry(pmu, &pmus, entry) {
  6809. if (!pmu->name || pmu->type < 0)
  6810. continue;
  6811. ret = pmu_dev_alloc(pmu);
  6812. WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
  6813. }
  6814. pmu_bus_running = 1;
  6815. ret = 0;
  6816. unlock:
  6817. mutex_unlock(&pmus_lock);
  6818. return ret;
  6819. }
  6820. device_initcall(perf_event_sysfs_init);
  6821. #ifdef CONFIG_CGROUP_PERF
  6822. static struct cgroup_subsys_state *perf_cgroup_create(struct cgroup *cont)
  6823. {
  6824. struct perf_cgroup *jc;
  6825. jc = kzalloc(sizeof(*jc), GFP_KERNEL);
  6826. if (!jc)
  6827. return ERR_PTR(-ENOMEM);
  6828. jc->info = alloc_percpu(struct perf_cgroup_info);
  6829. if (!jc->info) {
  6830. kfree(jc);
  6831. return ERR_PTR(-ENOMEM);
  6832. }
  6833. return &jc->css;
  6834. }
  6835. static void perf_cgroup_destroy(struct cgroup *cont)
  6836. {
  6837. struct perf_cgroup *jc;
  6838. jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
  6839. struct perf_cgroup, css);
  6840. free_percpu(jc->info);
  6841. kfree(jc);
  6842. }
  6843. static int __perf_cgroup_move(void *info)
  6844. {
  6845. struct task_struct *task = info;
  6846. perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
  6847. return 0;
  6848. }
  6849. static void perf_cgroup_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
  6850. {
  6851. struct task_struct *task;
  6852. cgroup_taskset_for_each(task, cgrp, tset)
  6853. task_function_call(task, __perf_cgroup_move, task);
  6854. }
  6855. static void perf_cgroup_exit(struct cgroup *cgrp, struct cgroup *old_cgrp,
  6856. struct task_struct *task)
  6857. {
  6858. /*
  6859. * cgroup_exit() is called in the copy_process() failure path.
  6860. * Ignore this case since the task hasn't ran yet, this avoids
  6861. * trying to poke a half freed task state from generic code.
  6862. */
  6863. if (!(task->flags & PF_EXITING))
  6864. return;
  6865. task_function_call(task, __perf_cgroup_move, task);
  6866. }
  6867. struct cgroup_subsys perf_subsys = {
  6868. .name = "perf_event",
  6869. .subsys_id = perf_subsys_id,
  6870. .create = perf_cgroup_create,
  6871. .destroy = perf_cgroup_destroy,
  6872. .exit = perf_cgroup_exit,
  6873. .attach = perf_cgroup_attach,
  6874. };
  6875. #endif /* CONFIG_CGROUP_PERF */