core.c 169 KB

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