core.c 189 KB

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