core.c 193 KB

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