core.c 224 KB

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