core.c 220 KB

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