core.c 160 KB

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