core.c 179 KB

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