core.c 190 KB

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