core.c 211 KB

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