1
0

patch-realtime 964 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735
  1. diff --git a/Documentation/hwlat_detector.txt b/Documentation/hwlat_detector.txt
  2. new file mode 100644
  3. index 000000000000..cb61516483d3
  4. --- /dev/null
  5. +++ b/Documentation/hwlat_detector.txt
  6. @@ -0,0 +1,64 @@
  7. +Introduction:
  8. +-------------
  9. +
  10. +The module hwlat_detector is a special purpose kernel module that is used to
  11. +detect large system latencies induced by the behavior of certain underlying
  12. +hardware or firmware, independent of Linux itself. The code was developed
  13. +originally to detect SMIs (System Management Interrupts) on x86 systems,
  14. +however there is nothing x86 specific about this patchset. It was
  15. +originally written for use by the "RT" patch since the Real Time
  16. +kernel is highly latency sensitive.
  17. +
  18. +SMIs are usually not serviced by the Linux kernel, which typically does not
  19. +even know that they are occuring. SMIs are instead are set up by BIOS code
  20. +and are serviced by BIOS code, usually for "critical" events such as
  21. +management of thermal sensors and fans. Sometimes though, SMIs are used for
  22. +other tasks and those tasks can spend an inordinate amount of time in the
  23. +handler (sometimes measured in milliseconds). Obviously this is a problem if
  24. +you are trying to keep event service latencies down in the microsecond range.
  25. +
  26. +The hardware latency detector works by hogging all of the cpus for configurable
  27. +amounts of time (by calling stop_machine()), polling the CPU Time Stamp Counter
  28. +for some period, then looking for gaps in the TSC data. Any gap indicates a
  29. +time when the polling was interrupted and since the machine is stopped and
  30. +interrupts turned off the only thing that could do that would be an SMI.
  31. +
  32. +Note that the SMI detector should *NEVER* be used in a production environment.
  33. +It is intended to be run manually to determine if the hardware platform has a
  34. +problem with long system firmware service routines.
  35. +
  36. +Usage:
  37. +------
  38. +
  39. +Loading the module hwlat_detector passing the parameter "enabled=1" (or by
  40. +setting the "enable" entry in "hwlat_detector" debugfs toggled on) is the only
  41. +step required to start the hwlat_detector. It is possible to redefine the
  42. +threshold in microseconds (us) above which latency spikes will be taken
  43. +into account (parameter "threshold=").
  44. +
  45. +Example:
  46. +
  47. + # modprobe hwlat_detector enabled=1 threshold=100
  48. +
  49. +After the module is loaded, it creates a directory named "hwlat_detector" under
  50. +the debugfs mountpoint, "/debug/hwlat_detector" for this text. It is necessary
  51. +to have debugfs mounted, which might be on /sys/debug on your system.
  52. +
  53. +The /debug/hwlat_detector interface contains the following files:
  54. +
  55. +count - number of latency spikes observed since last reset
  56. +enable - a global enable/disable toggle (0/1), resets count
  57. +max - maximum hardware latency actually observed (usecs)
  58. +sample - a pipe from which to read current raw sample data
  59. + in the format <timestamp> <latency observed usecs>
  60. + (can be opened O_NONBLOCK for a single sample)
  61. +threshold - minimum latency value to be considered (usecs)
  62. +width - time period to sample with CPUs held (usecs)
  63. + must be less than the total window size (enforced)
  64. +window - total period of sampling, width being inside (usecs)
  65. +
  66. +By default we will set width to 500,000 and window to 1,000,000, meaning that
  67. +we will sample every 1,000,000 usecs (1s) for 500,000 usecs (0.5s). If we
  68. +observe any latencies that exceed the threshold (initially 100 usecs),
  69. +then we write to a global sample ring buffer of 8K samples, which is
  70. +consumed by reading from the "sample" (pipe) debugfs file interface.
  71. diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt
  72. index 39280b72f27a..c46295a8d55b 100644
  73. --- a/Documentation/kernel-parameters.txt
  74. +++ b/Documentation/kernel-parameters.txt
  75. @@ -1640,6 +1640,15 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
  76. ip= [IP_PNP]
  77. See Documentation/filesystems/nfs/nfsroot.txt.
  78. + irqaffinity= [SMP] Set the default irq affinity mask
  79. + Format:
  80. + <cpu number>,...,<cpu number>
  81. + or
  82. + <cpu number>-<cpu number>
  83. + (must be a positive range in ascending order)
  84. + or a mixture
  85. + <cpu number>,...,<cpu number>-<cpu number>
  86. +
  87. irqfixup [HW]
  88. When an interrupt is not handled search all handlers
  89. for it. Intended to get systems with badly broken
  90. diff --git a/Documentation/sysrq.txt b/Documentation/sysrq.txt
  91. index 13f5619b2203..f64d075ba647 100644
  92. --- a/Documentation/sysrq.txt
  93. +++ b/Documentation/sysrq.txt
  94. @@ -59,10 +59,17 @@ On PowerPC - Press 'ALT - Print Screen (or F13) - <command key>,
  95. On other - If you know of the key combos for other architectures, please
  96. let me know so I can add them to this section.
  97. -On all - write a character to /proc/sysrq-trigger. e.g.:
  98. -
  99. +On all - write a character to /proc/sysrq-trigger, e.g.:
  100. echo t > /proc/sysrq-trigger
  101. +On all - Enable network SysRq by writing a cookie to icmp_echo_sysrq, e.g.
  102. + echo 0x01020304 >/proc/sys/net/ipv4/icmp_echo_sysrq
  103. + Send an ICMP echo request with this pattern plus the particular
  104. + SysRq command key. Example:
  105. + # ping -c1 -s57 -p0102030468
  106. + will trigger the SysRq-H (help) command.
  107. +
  108. +
  109. * What are the 'command' keys?
  110. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  111. 'b' - Will immediately reboot the system without syncing or unmounting
  112. diff --git a/Documentation/trace/histograms.txt b/Documentation/trace/histograms.txt
  113. new file mode 100644
  114. index 000000000000..6f2aeabf7faa
  115. --- /dev/null
  116. +++ b/Documentation/trace/histograms.txt
  117. @@ -0,0 +1,186 @@
  118. + Using the Linux Kernel Latency Histograms
  119. +
  120. +
  121. +This document gives a short explanation how to enable, configure and use
  122. +latency histograms. Latency histograms are primarily relevant in the
  123. +context of real-time enabled kernels (CONFIG_PREEMPT/CONFIG_PREEMPT_RT)
  124. +and are used in the quality management of the Linux real-time
  125. +capabilities.
  126. +
  127. +
  128. +* Purpose of latency histograms
  129. +
  130. +A latency histogram continuously accumulates the frequencies of latency
  131. +data. There are two types of histograms
  132. +- potential sources of latencies
  133. +- effective latencies
  134. +
  135. +
  136. +* Potential sources of latencies
  137. +
  138. +Potential sources of latencies are code segments where interrupts,
  139. +preemption or both are disabled (aka critical sections). To create
  140. +histograms of potential sources of latency, the kernel stores the time
  141. +stamp at the start of a critical section, determines the time elapsed
  142. +when the end of the section is reached, and increments the frequency
  143. +counter of that latency value - irrespective of whether any concurrently
  144. +running process is affected by latency or not.
  145. +- Configuration items (in the Kernel hacking/Tracers submenu)
  146. + CONFIG_INTERRUPT_OFF_LATENCY
  147. + CONFIG_PREEMPT_OFF_LATENCY
  148. +
  149. +
  150. +* Effective latencies
  151. +
  152. +Effective latencies are actually occuring during wakeup of a process. To
  153. +determine effective latencies, the kernel stores the time stamp when a
  154. +process is scheduled to be woken up, and determines the duration of the
  155. +wakeup time shortly before control is passed over to this process. Note
  156. +that the apparent latency in user space may be somewhat longer, since the
  157. +process may be interrupted after control is passed over to it but before
  158. +the execution in user space takes place. Simply measuring the interval
  159. +between enqueuing and wakeup may also not appropriate in cases when a
  160. +process is scheduled as a result of a timer expiration. The timer may have
  161. +missed its deadline, e.g. due to disabled interrupts, but this latency
  162. +would not be registered. Therefore, the offsets of missed timers are
  163. +recorded in a separate histogram. If both wakeup latency and missed timer
  164. +offsets are configured and enabled, a third histogram may be enabled that
  165. +records the overall latency as a sum of the timer latency, if any, and the
  166. +wakeup latency. This histogram is called "timerandwakeup".
  167. +- Configuration items (in the Kernel hacking/Tracers submenu)
  168. + CONFIG_WAKEUP_LATENCY
  169. + CONFIG_MISSED_TIMER_OFSETS
  170. +
  171. +
  172. +* Usage
  173. +
  174. +The interface to the administration of the latency histograms is located
  175. +in the debugfs file system. To mount it, either enter
  176. +
  177. +mount -t sysfs nodev /sys
  178. +mount -t debugfs nodev /sys/kernel/debug
  179. +
  180. +from shell command line level, or add
  181. +
  182. +nodev /sys sysfs defaults 0 0
  183. +nodev /sys/kernel/debug debugfs defaults 0 0
  184. +
  185. +to the file /etc/fstab. All latency histogram related files are then
  186. +available in the directory /sys/kernel/debug/tracing/latency_hist. A
  187. +particular histogram type is enabled by writing non-zero to the related
  188. +variable in the /sys/kernel/debug/tracing/latency_hist/enable directory.
  189. +Select "preemptirqsoff" for the histograms of potential sources of
  190. +latencies and "wakeup" for histograms of effective latencies etc. The
  191. +histogram data - one per CPU - are available in the files
  192. +
  193. +/sys/kernel/debug/tracing/latency_hist/preemptoff/CPUx
  194. +/sys/kernel/debug/tracing/latency_hist/irqsoff/CPUx
  195. +/sys/kernel/debug/tracing/latency_hist/preemptirqsoff/CPUx
  196. +/sys/kernel/debug/tracing/latency_hist/wakeup/CPUx
  197. +/sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio/CPUx
  198. +/sys/kernel/debug/tracing/latency_hist/missed_timer_offsets/CPUx
  199. +/sys/kernel/debug/tracing/latency_hist/timerandwakeup/CPUx
  200. +
  201. +The histograms are reset by writing non-zero to the file "reset" in a
  202. +particular latency directory. To reset all latency data, use
  203. +
  204. +#!/bin/sh
  205. +
  206. +TRACINGDIR=/sys/kernel/debug/tracing
  207. +HISTDIR=$TRACINGDIR/latency_hist
  208. +
  209. +if test -d $HISTDIR
  210. +then
  211. + cd $HISTDIR
  212. + for i in `find . | grep /reset$`
  213. + do
  214. + echo 1 >$i
  215. + done
  216. +fi
  217. +
  218. +
  219. +* Data format
  220. +
  221. +Latency data are stored with a resolution of one microsecond. The
  222. +maximum latency is 10,240 microseconds. The data are only valid, if the
  223. +overflow register is empty. Every output line contains the latency in
  224. +microseconds in the first row and the number of samples in the second
  225. +row. To display only lines with a positive latency count, use, for
  226. +example,
  227. +
  228. +grep -v " 0$" /sys/kernel/debug/tracing/latency_hist/preemptoff/CPU0
  229. +
  230. +#Minimum latency: 0 microseconds.
  231. +#Average latency: 0 microseconds.
  232. +#Maximum latency: 25 microseconds.
  233. +#Total samples: 3104770694
  234. +#There are 0 samples greater or equal than 10240 microseconds
  235. +#usecs samples
  236. + 0 2984486876
  237. + 1 49843506
  238. + 2 58219047
  239. + 3 5348126
  240. + 4 2187960
  241. + 5 3388262
  242. + 6 959289
  243. + 7 208294
  244. + 8 40420
  245. + 9 4485
  246. + 10 14918
  247. + 11 18340
  248. + 12 25052
  249. + 13 19455
  250. + 14 5602
  251. + 15 969
  252. + 16 47
  253. + 17 18
  254. + 18 14
  255. + 19 1
  256. + 20 3
  257. + 21 2
  258. + 22 5
  259. + 23 2
  260. + 25 1
  261. +
  262. +
  263. +* Wakeup latency of a selected process
  264. +
  265. +To only collect wakeup latency data of a particular process, write the
  266. +PID of the requested process to
  267. +
  268. +/sys/kernel/debug/tracing/latency_hist/wakeup/pid
  269. +
  270. +PIDs are not considered, if this variable is set to 0.
  271. +
  272. +
  273. +* Details of the process with the highest wakeup latency so far
  274. +
  275. +Selected data of the process that suffered from the highest wakeup
  276. +latency that occurred in a particular CPU are available in the file
  277. +
  278. +/sys/kernel/debug/tracing/latency_hist/wakeup/max_latency-CPUx.
  279. +
  280. +In addition, other relevant system data at the time when the
  281. +latency occurred are given.
  282. +
  283. +The format of the data is (all in one line):
  284. +<PID> <Priority> <Latency> (<Timeroffset>) <Command> \
  285. +<- <PID> <Priority> <Command> <Timestamp>
  286. +
  287. +The value of <Timeroffset> is only relevant in the combined timer
  288. +and wakeup latency recording. In the wakeup recording, it is
  289. +always 0, in the missed_timer_offsets recording, it is the same
  290. +as <Latency>.
  291. +
  292. +When retrospectively searching for the origin of a latency and
  293. +tracing was not enabled, it may be helpful to know the name and
  294. +some basic data of the task that (finally) was switching to the
  295. +late real-tlme task. In addition to the victim's data, also the
  296. +data of the possible culprit are therefore displayed after the
  297. +"<-" symbol.
  298. +
  299. +Finally, the timestamp of the time when the latency occurred
  300. +in <seconds>.<microseconds> after the most recent system boot
  301. +is provided.
  302. +
  303. +These data are also reset when the wakeup histogram is reset.
  304. diff --git a/Makefile b/Makefile
  305. index 07070a1e6292..f04a8b6444b9 100644
  306. --- a/Makefile
  307. +++ b/Makefile
  308. @@ -797,6 +797,9 @@ KBUILD_CFLAGS += $(call cc-option,-Werror=strict-prototypes)
  309. # Prohibit date/time macros, which would make the build non-deterministic
  310. KBUILD_CFLAGS += $(call cc-option,-Werror=date-time)
  311. +# enforce correct pointer usage
  312. +KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)
  313. +
  314. # use the deterministic mode of AR if available
  315. KBUILD_ARFLAGS := $(call ar-option,D)
  316. diff --git a/arch/Kconfig b/arch/Kconfig
  317. index 4e949e58b192..3b26d76933fb 100644
  318. --- a/arch/Kconfig
  319. +++ b/arch/Kconfig
  320. @@ -9,6 +9,7 @@ config OPROFILE
  321. tristate "OProfile system profiling"
  322. depends on PROFILING
  323. depends on HAVE_OPROFILE
  324. + depends on !PREEMPT_RT_FULL
  325. select RING_BUFFER
  326. select RING_BUFFER_ALLOW_SWAP
  327. help
  328. @@ -52,6 +53,7 @@ config KPROBES
  329. config JUMP_LABEL
  330. bool "Optimize very unlikely/likely branches"
  331. depends on HAVE_ARCH_JUMP_LABEL
  332. + depends on (!INTERRUPT_OFF_HIST && !PREEMPT_OFF_HIST && !WAKEUP_LATENCY_HIST && !MISSED_TIMER_OFFSETS_HIST)
  333. help
  334. This option enables a transparent branch optimization that
  335. makes certain almost-always-true or almost-always-false branch
  336. diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
  337. index 34e1569a11ee..79c4603e9453 100644
  338. --- a/arch/arm/Kconfig
  339. +++ b/arch/arm/Kconfig
  340. @@ -33,7 +33,7 @@ config ARM
  341. select HARDIRQS_SW_RESEND
  342. select HAVE_ARCH_AUDITSYSCALL if (AEABI && !OABI_COMPAT)
  343. select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6
  344. - select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32
  345. + select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32 && !PREEMPT_RT_BASE
  346. select HAVE_ARCH_KGDB if !CPU_ENDIAN_BE32
  347. select HAVE_ARCH_SECCOMP_FILTER if (AEABI && !OABI_COMPAT)
  348. select HAVE_ARCH_TRACEHOOK
  349. @@ -68,6 +68,7 @@ config ARM
  350. select HAVE_PERF_EVENTS
  351. select HAVE_PERF_REGS
  352. select HAVE_PERF_USER_STACK_DUMP
  353. + select HAVE_PREEMPT_LAZY
  354. select HAVE_RCU_TABLE_FREE if (SMP && ARM_LPAE)
  355. select HAVE_REGS_AND_STACK_ACCESS_API
  356. select HAVE_SYSCALL_TRACEPOINTS
  357. diff --git a/arch/arm/include/asm/switch_to.h b/arch/arm/include/asm/switch_to.h
  358. index 12ebfcc1d539..c962084605bc 100644
  359. --- a/arch/arm/include/asm/switch_to.h
  360. +++ b/arch/arm/include/asm/switch_to.h
  361. @@ -3,6 +3,13 @@
  362. #include <linux/thread_info.h>
  363. +#if defined CONFIG_PREEMPT_RT_FULL && defined CONFIG_HIGHMEM
  364. +void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p);
  365. +#else
  366. +static inline void
  367. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  368. +#endif
  369. +
  370. /*
  371. * For v7 SMP cores running a preemptible kernel we may be pre-empted
  372. * during a TLB maintenance operation, so execute an inner-shareable dsb
  373. @@ -25,6 +32,7 @@ extern struct task_struct *__switch_to(struct task_struct *, struct thread_info
  374. #define switch_to(prev,next,last) \
  375. do { \
  376. __complete_pending_tlbi(); \
  377. + switch_kmaps(prev, next); \
  378. last = __switch_to(prev,task_thread_info(prev), task_thread_info(next)); \
  379. } while (0)
  380. diff --git a/arch/arm/include/asm/thread_info.h b/arch/arm/include/asm/thread_info.h
  381. index 776757d1604a..1f36a4eccc72 100644
  382. --- a/arch/arm/include/asm/thread_info.h
  383. +++ b/arch/arm/include/asm/thread_info.h
  384. @@ -49,6 +49,7 @@ struct cpu_context_save {
  385. struct thread_info {
  386. unsigned long flags; /* low level flags */
  387. int preempt_count; /* 0 => preemptable, <0 => bug */
  388. + int preempt_lazy_count; /* 0 => preemptable, <0 => bug */
  389. mm_segment_t addr_limit; /* address limit */
  390. struct task_struct *task; /* main task structure */
  391. __u32 cpu; /* cpu */
  392. @@ -142,7 +143,8 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
  393. #define TIF_SYSCALL_TRACE 4 /* syscall trace active */
  394. #define TIF_SYSCALL_AUDIT 5 /* syscall auditing active */
  395. #define TIF_SYSCALL_TRACEPOINT 6 /* syscall tracepoint instrumentation */
  396. -#define TIF_SECCOMP 7 /* seccomp syscall filtering active */
  397. +#define TIF_SECCOMP 8 /* seccomp syscall filtering active */
  398. +#define TIF_NEED_RESCHED_LAZY 7
  399. #define TIF_NOHZ 12 /* in adaptive nohz mode */
  400. #define TIF_USING_IWMMXT 17
  401. @@ -152,6 +154,7 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
  402. #define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
  403. #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
  404. #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
  405. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  406. #define _TIF_UPROBE (1 << TIF_UPROBE)
  407. #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
  408. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  409. @@ -167,7 +170,8 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
  410. * Change these and you break ASM code in entry-common.S
  411. */
  412. #define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
  413. - _TIF_NOTIFY_RESUME | _TIF_UPROBE)
  414. + _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  415. + _TIF_NEED_RESCHED_LAZY)
  416. #endif /* __KERNEL__ */
  417. #endif /* __ASM_ARM_THREAD_INFO_H */
  418. diff --git a/arch/arm/kernel/asm-offsets.c b/arch/arm/kernel/asm-offsets.c
  419. index 871b8267d211..4dbe70de7318 100644
  420. --- a/arch/arm/kernel/asm-offsets.c
  421. +++ b/arch/arm/kernel/asm-offsets.c
  422. @@ -65,6 +65,7 @@ int main(void)
  423. BLANK();
  424. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  425. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  426. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  427. DEFINE(TI_ADDR_LIMIT, offsetof(struct thread_info, addr_limit));
  428. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  429. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  430. diff --git a/arch/arm/kernel/entry-armv.S b/arch/arm/kernel/entry-armv.S
  431. index 3ce377f7251f..d044cea59f54 100644
  432. --- a/arch/arm/kernel/entry-armv.S
  433. +++ b/arch/arm/kernel/entry-armv.S
  434. @@ -215,11 +215,18 @@ ENDPROC(__dabt_svc)
  435. #ifdef CONFIG_PREEMPT
  436. get_thread_info tsk
  437. ldr r8, [tsk, #TI_PREEMPT] @ get preempt count
  438. - ldr r0, [tsk, #TI_FLAGS] @ get flags
  439. teq r8, #0 @ if preempt count != 0
  440. + bne 1f @ return from exeption
  441. + ldr r0, [tsk, #TI_FLAGS] @ get flags
  442. + tst r0, #_TIF_NEED_RESCHED @ if NEED_RESCHED is set
  443. + blne svc_preempt @ preempt!
  444. +
  445. + ldr r8, [tsk, #TI_PREEMPT_LAZY] @ get preempt lazy count
  446. + teq r8, #0 @ if preempt lazy count != 0
  447. movne r0, #0 @ force flags to 0
  448. - tst r0, #_TIF_NEED_RESCHED
  449. + tst r0, #_TIF_NEED_RESCHED_LAZY
  450. blne svc_preempt
  451. +1:
  452. #endif
  453. svc_exit r5, irq = 1 @ return from exception
  454. @@ -234,8 +241,14 @@ ENDPROC(__irq_svc)
  455. 1: bl preempt_schedule_irq @ irq en/disable is done inside
  456. ldr r0, [tsk, #TI_FLAGS] @ get new tasks TI_FLAGS
  457. tst r0, #_TIF_NEED_RESCHED
  458. + bne 1b
  459. + tst r0, #_TIF_NEED_RESCHED_LAZY
  460. reteq r8 @ go again
  461. - b 1b
  462. + ldr r0, [tsk, #TI_PREEMPT_LAZY] @ get preempt lazy count
  463. + teq r0, #0 @ if preempt lazy count != 0
  464. + beq 1b
  465. + ret r8 @ go again
  466. +
  467. #endif
  468. __und_fault:
  469. diff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S
  470. index 30a7228eaceb..c3bd6cbfce4b 100644
  471. --- a/arch/arm/kernel/entry-common.S
  472. +++ b/arch/arm/kernel/entry-common.S
  473. @@ -36,7 +36,9 @@
  474. UNWIND(.cantunwind )
  475. disable_irq_notrace @ disable interrupts
  476. ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing
  477. - tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK
  478. + tst r1, #((_TIF_SYSCALL_WORK | _TIF_WORK_MASK) & ~_TIF_SECCOMP)
  479. + bne fast_work_pending
  480. + tst r1, #_TIF_SECCOMP
  481. bne fast_work_pending
  482. /* perform architecture specific actions before user return */
  483. @@ -62,8 +64,11 @@ ENDPROC(ret_fast_syscall)
  484. str r0, [sp, #S_R0 + S_OFF]! @ save returned r0
  485. disable_irq_notrace @ disable interrupts
  486. ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing
  487. - tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK
  488. + tst r1, #((_TIF_SYSCALL_WORK | _TIF_WORK_MASK) & ~_TIF_SECCOMP)
  489. + bne do_slower_path
  490. + tst r1, #_TIF_SECCOMP
  491. beq no_work_pending
  492. +do_slower_path:
  493. UNWIND(.fnend )
  494. ENDPROC(ret_fast_syscall)
  495. diff --git a/arch/arm/kernel/patch.c b/arch/arm/kernel/patch.c
  496. index 69bda1a5707e..1f665acaa6a9 100644
  497. --- a/arch/arm/kernel/patch.c
  498. +++ b/arch/arm/kernel/patch.c
  499. @@ -15,7 +15,7 @@ struct patch {
  500. unsigned int insn;
  501. };
  502. -static DEFINE_SPINLOCK(patch_lock);
  503. +static DEFINE_RAW_SPINLOCK(patch_lock);
  504. static void __kprobes *patch_map(void *addr, int fixmap, unsigned long *flags)
  505. __acquires(&patch_lock)
  506. @@ -32,7 +32,7 @@ static void __kprobes *patch_map(void *addr, int fixmap, unsigned long *flags)
  507. return addr;
  508. if (flags)
  509. - spin_lock_irqsave(&patch_lock, *flags);
  510. + raw_spin_lock_irqsave(&patch_lock, *flags);
  511. else
  512. __acquire(&patch_lock);
  513. @@ -47,7 +47,7 @@ static void __kprobes patch_unmap(int fixmap, unsigned long *flags)
  514. clear_fixmap(fixmap);
  515. if (flags)
  516. - spin_unlock_irqrestore(&patch_lock, *flags);
  517. + raw_spin_unlock_irqrestore(&patch_lock, *flags);
  518. else
  519. __release(&patch_lock);
  520. }
  521. diff --git a/arch/arm/kernel/process.c b/arch/arm/kernel/process.c
  522. index 4adfb46e3ee9..15f1d94b47c5 100644
  523. --- a/arch/arm/kernel/process.c
  524. +++ b/arch/arm/kernel/process.c
  525. @@ -319,6 +319,30 @@ unsigned long arch_randomize_brk(struct mm_struct *mm)
  526. }
  527. #ifdef CONFIG_MMU
  528. +/*
  529. + * CONFIG_SPLIT_PTLOCK_CPUS results in a page->ptl lock. If the lock is not
  530. + * initialized by pgtable_page_ctor() then a coredump of the vector page will
  531. + * fail.
  532. + */
  533. +static int __init vectors_user_mapping_init_page(void)
  534. +{
  535. + struct page *page;
  536. + unsigned long addr = 0xffff0000;
  537. + pgd_t *pgd;
  538. + pud_t *pud;
  539. + pmd_t *pmd;
  540. +
  541. + pgd = pgd_offset_k(addr);
  542. + pud = pud_offset(pgd, addr);
  543. + pmd = pmd_offset(pud, addr);
  544. + page = pmd_page(*(pmd));
  545. +
  546. + pgtable_page_ctor(page);
  547. +
  548. + return 0;
  549. +}
  550. +late_initcall(vectors_user_mapping_init_page);
  551. +
  552. #ifdef CONFIG_KUSER_HELPERS
  553. /*
  554. * The vectors page is always readable from user space for the
  555. diff --git a/arch/arm/kernel/signal.c b/arch/arm/kernel/signal.c
  556. index 7b8f2141427b..96541e00b74a 100644
  557. --- a/arch/arm/kernel/signal.c
  558. +++ b/arch/arm/kernel/signal.c
  559. @@ -572,7 +572,8 @@ do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
  560. */
  561. trace_hardirqs_off();
  562. do {
  563. - if (likely(thread_flags & _TIF_NEED_RESCHED)) {
  564. + if (likely(thread_flags & (_TIF_NEED_RESCHED |
  565. + _TIF_NEED_RESCHED_LAZY))) {
  566. schedule();
  567. } else {
  568. if (unlikely(!user_mode(regs)))
  569. diff --git a/arch/arm/kernel/smp.c b/arch/arm/kernel/smp.c
  570. index b26361355dae..e5754e3b03c4 100644
  571. --- a/arch/arm/kernel/smp.c
  572. +++ b/arch/arm/kernel/smp.c
  573. @@ -230,8 +230,6 @@ int __cpu_disable(void)
  574. flush_cache_louis();
  575. local_flush_tlb_all();
  576. - clear_tasks_mm_cpumask(cpu);
  577. -
  578. return 0;
  579. }
  580. @@ -247,6 +245,9 @@ void __cpu_die(unsigned int cpu)
  581. pr_err("CPU%u: cpu didn't die\n", cpu);
  582. return;
  583. }
  584. +
  585. + clear_tasks_mm_cpumask(cpu);
  586. +
  587. pr_notice("CPU%u: shutdown\n", cpu);
  588. /*
  589. diff --git a/arch/arm/kernel/unwind.c b/arch/arm/kernel/unwind.c
  590. index 0bee233fef9a..314cfb232a63 100644
  591. --- a/arch/arm/kernel/unwind.c
  592. +++ b/arch/arm/kernel/unwind.c
  593. @@ -93,7 +93,7 @@ extern const struct unwind_idx __start_unwind_idx[];
  594. static const struct unwind_idx *__origin_unwind_idx;
  595. extern const struct unwind_idx __stop_unwind_idx[];
  596. -static DEFINE_SPINLOCK(unwind_lock);
  597. +static DEFINE_RAW_SPINLOCK(unwind_lock);
  598. static LIST_HEAD(unwind_tables);
  599. /* Convert a prel31 symbol to an absolute address */
  600. @@ -201,7 +201,7 @@ static const struct unwind_idx *unwind_find_idx(unsigned long addr)
  601. /* module unwind tables */
  602. struct unwind_table *table;
  603. - spin_lock_irqsave(&unwind_lock, flags);
  604. + raw_spin_lock_irqsave(&unwind_lock, flags);
  605. list_for_each_entry(table, &unwind_tables, list) {
  606. if (addr >= table->begin_addr &&
  607. addr < table->end_addr) {
  608. @@ -213,7 +213,7 @@ static const struct unwind_idx *unwind_find_idx(unsigned long addr)
  609. break;
  610. }
  611. }
  612. - spin_unlock_irqrestore(&unwind_lock, flags);
  613. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  614. }
  615. pr_debug("%s: idx = %p\n", __func__, idx);
  616. @@ -529,9 +529,9 @@ struct unwind_table *unwind_table_add(unsigned long start, unsigned long size,
  617. tab->begin_addr = text_addr;
  618. tab->end_addr = text_addr + text_size;
  619. - spin_lock_irqsave(&unwind_lock, flags);
  620. + raw_spin_lock_irqsave(&unwind_lock, flags);
  621. list_add_tail(&tab->list, &unwind_tables);
  622. - spin_unlock_irqrestore(&unwind_lock, flags);
  623. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  624. return tab;
  625. }
  626. @@ -543,9 +543,9 @@ void unwind_table_del(struct unwind_table *tab)
  627. if (!tab)
  628. return;
  629. - spin_lock_irqsave(&unwind_lock, flags);
  630. + raw_spin_lock_irqsave(&unwind_lock, flags);
  631. list_del(&tab->list);
  632. - spin_unlock_irqrestore(&unwind_lock, flags);
  633. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  634. kfree(tab);
  635. }
  636. diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c
  637. index d7bef2144760..36a3e51492f7 100644
  638. --- a/arch/arm/kvm/arm.c
  639. +++ b/arch/arm/kvm/arm.c
  640. @@ -496,18 +496,18 @@ static void kvm_arm_resume_guest(struct kvm *kvm)
  641. struct kvm_vcpu *vcpu;
  642. kvm_for_each_vcpu(i, vcpu, kvm) {
  643. - wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  644. + struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
  645. vcpu->arch.pause = false;
  646. - wake_up_interruptible(wq);
  647. + swake_up(wq);
  648. }
  649. }
  650. static void vcpu_sleep(struct kvm_vcpu *vcpu)
  651. {
  652. - wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  653. + struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
  654. - wait_event_interruptible(*wq, ((!vcpu->arch.power_off) &&
  655. + swait_event_interruptible(*wq, ((!vcpu->arch.power_off) &&
  656. (!vcpu->arch.pause)));
  657. }
  658. @@ -566,7 +566,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  659. * involves poking the GIC, which must be done in a
  660. * non-preemptible context.
  661. */
  662. - preempt_disable();
  663. + migrate_disable();
  664. kvm_timer_flush_hwstate(vcpu);
  665. kvm_vgic_flush_hwstate(vcpu);
  666. @@ -585,7 +585,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  667. local_irq_enable();
  668. kvm_timer_sync_hwstate(vcpu);
  669. kvm_vgic_sync_hwstate(vcpu);
  670. - preempt_enable();
  671. + migrate_enable();
  672. continue;
  673. }
  674. @@ -639,7 +639,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  675. kvm_vgic_sync_hwstate(vcpu);
  676. - preempt_enable();
  677. + migrate_enable();
  678. ret = handle_exit(vcpu, run, ret);
  679. }
  680. diff --git a/arch/arm/kvm/psci.c b/arch/arm/kvm/psci.c
  681. index 443db0c43d7c..a08d7a93aebb 100644
  682. --- a/arch/arm/kvm/psci.c
  683. +++ b/arch/arm/kvm/psci.c
  684. @@ -70,7 +70,7 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
  685. {
  686. struct kvm *kvm = source_vcpu->kvm;
  687. struct kvm_vcpu *vcpu = NULL;
  688. - wait_queue_head_t *wq;
  689. + struct swait_queue_head *wq;
  690. unsigned long cpu_id;
  691. unsigned long context_id;
  692. phys_addr_t target_pc;
  693. @@ -119,7 +119,7 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
  694. smp_mb(); /* Make sure the above is visible */
  695. wq = kvm_arch_vcpu_wq(vcpu);
  696. - wake_up_interruptible(wq);
  697. + swake_up(wq);
  698. return PSCI_RET_SUCCESS;
  699. }
  700. diff --git a/arch/arm/mach-at91/Kconfig b/arch/arm/mach-at91/Kconfig
  701. index 28656c2b54a0..3f501305ca26 100644
  702. --- a/arch/arm/mach-at91/Kconfig
  703. +++ b/arch/arm/mach-at91/Kconfig
  704. @@ -99,6 +99,7 @@ config HAVE_AT91_USB_CLK
  705. config COMMON_CLK_AT91
  706. bool
  707. select COMMON_CLK
  708. + select MFD_SYSCON
  709. config HAVE_AT91_SMD
  710. bool
  711. diff --git a/arch/arm/mach-at91/at91rm9200.c b/arch/arm/mach-at91/at91rm9200.c
  712. index c1a7c6cc00e1..63b4fa25b48a 100644
  713. --- a/arch/arm/mach-at91/at91rm9200.c
  714. +++ b/arch/arm/mach-at91/at91rm9200.c
  715. @@ -12,7 +12,6 @@
  716. #include <linux/of_platform.h>
  717. #include <asm/mach/arch.h>
  718. -#include <asm/system_misc.h>
  719. #include "generic.h"
  720. #include "soc.h"
  721. @@ -33,7 +32,6 @@ static void __init at91rm9200_dt_device_init(void)
  722. of_platform_populate(NULL, of_default_bus_match_table, NULL, soc_dev);
  723. - arm_pm_idle = at91rm9200_idle;
  724. at91rm9200_pm_init();
  725. }
  726. diff --git a/arch/arm/mach-at91/at91sam9.c b/arch/arm/mach-at91/at91sam9.c
  727. index 7eb64f763034..cada2a6412b3 100644
  728. --- a/arch/arm/mach-at91/at91sam9.c
  729. +++ b/arch/arm/mach-at91/at91sam9.c
  730. @@ -62,8 +62,6 @@ static void __init at91sam9_common_init(void)
  731. soc_dev = soc_device_to_device(soc);
  732. of_platform_populate(NULL, of_default_bus_match_table, NULL, soc_dev);
  733. -
  734. - arm_pm_idle = at91sam9_idle;
  735. }
  736. static void __init at91sam9_dt_device_init(void)
  737. diff --git a/arch/arm/mach-at91/generic.h b/arch/arm/mach-at91/generic.h
  738. index b0fa7dc7286d..28ca57a2060f 100644
  739. --- a/arch/arm/mach-at91/generic.h
  740. +++ b/arch/arm/mach-at91/generic.h
  741. @@ -11,27 +11,18 @@
  742. #ifndef _AT91_GENERIC_H
  743. #define _AT91_GENERIC_H
  744. -#include <linux/of.h>
  745. -#include <linux/reboot.h>
  746. -
  747. - /* Map io */
  748. -extern void __init at91_map_io(void);
  749. -extern void __init at91_alt_map_io(void);
  750. -
  751. -/* idle */
  752. -extern void at91rm9200_idle(void);
  753. -extern void at91sam9_idle(void);
  754. -
  755. #ifdef CONFIG_PM
  756. extern void __init at91rm9200_pm_init(void);
  757. extern void __init at91sam9260_pm_init(void);
  758. extern void __init at91sam9g45_pm_init(void);
  759. extern void __init at91sam9x5_pm_init(void);
  760. +extern void __init sama5_pm_init(void);
  761. #else
  762. static inline void __init at91rm9200_pm_init(void) { }
  763. static inline void __init at91sam9260_pm_init(void) { }
  764. static inline void __init at91sam9g45_pm_init(void) { }
  765. static inline void __init at91sam9x5_pm_init(void) { }
  766. +static inline void __init sama5_pm_init(void) { }
  767. #endif
  768. #endif /* _AT91_GENERIC_H */
  769. diff --git a/arch/arm/mach-at91/pm.c b/arch/arm/mach-at91/pm.c
  770. index 84eefbc2b4f9..bff0d062bf68 100644
  771. --- a/arch/arm/mach-at91/pm.c
  772. +++ b/arch/arm/mach-at91/pm.c
  773. @@ -31,10 +31,13 @@
  774. #include <asm/mach/irq.h>
  775. #include <asm/fncpy.h>
  776. #include <asm/cacheflush.h>
  777. +#include <asm/system_misc.h>
  778. #include "generic.h"
  779. #include "pm.h"
  780. +static void __iomem *pmc;
  781. +
  782. /*
  783. * FIXME: this is needed to communicate between the pinctrl driver and
  784. * the PM implementation in the machine. Possibly part of the PM
  785. @@ -87,7 +90,7 @@ static int at91_pm_verify_clocks(void)
  786. unsigned long scsr;
  787. int i;
  788. - scsr = at91_pmc_read(AT91_PMC_SCSR);
  789. + scsr = readl(pmc + AT91_PMC_SCSR);
  790. /* USB must not be using PLLB */
  791. if ((scsr & at91_pm_data.uhp_udp_mask) != 0) {
  792. @@ -101,8 +104,7 @@ static int at91_pm_verify_clocks(void)
  793. if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
  794. continue;
  795. -
  796. - css = at91_pmc_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  797. + css = readl(pmc + AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  798. if (css != AT91_PMC_CSS_SLOW) {
  799. pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  800. return 0;
  801. @@ -145,8 +147,8 @@ static void at91_pm_suspend(suspend_state_t state)
  802. flush_cache_all();
  803. outer_disable();
  804. - at91_suspend_sram_fn(at91_pmc_base, at91_ramc_base[0],
  805. - at91_ramc_base[1], pm_data);
  806. + at91_suspend_sram_fn(pmc, at91_ramc_base[0],
  807. + at91_ramc_base[1], pm_data);
  808. outer_resume();
  809. }
  810. @@ -369,6 +371,21 @@ static __init void at91_dt_ramc(void)
  811. at91_pm_set_standby(standby);
  812. }
  813. +void at91rm9200_idle(void)
  814. +{
  815. + /*
  816. + * Disable the processor clock. The processor will be automatically
  817. + * re-enabled by an interrupt or by a reset.
  818. + */
  819. + writel(AT91_PMC_PCK, pmc + AT91_PMC_SCDR);
  820. +}
  821. +
  822. +void at91sam9_idle(void)
  823. +{
  824. + writel(AT91_PMC_PCK, pmc + AT91_PMC_SCDR);
  825. + cpu_do_idle();
  826. +}
  827. +
  828. static void __init at91_pm_sram_init(void)
  829. {
  830. struct gen_pool *sram_pool;
  831. @@ -415,13 +432,36 @@ static void __init at91_pm_sram_init(void)
  832. &at91_pm_suspend_in_sram, at91_pm_suspend_in_sram_sz);
  833. }
  834. -static void __init at91_pm_init(void)
  835. +static const struct of_device_id atmel_pmc_ids[] __initconst = {
  836. + { .compatible = "atmel,at91rm9200-pmc" },
  837. + { .compatible = "atmel,at91sam9260-pmc" },
  838. + { .compatible = "atmel,at91sam9g45-pmc" },
  839. + { .compatible = "atmel,at91sam9n12-pmc" },
  840. + { .compatible = "atmel,at91sam9x5-pmc" },
  841. + { .compatible = "atmel,sama5d3-pmc" },
  842. + { .compatible = "atmel,sama5d2-pmc" },
  843. + { /* sentinel */ },
  844. +};
  845. +
  846. +static void __init at91_pm_init(void (*pm_idle)(void))
  847. {
  848. - at91_pm_sram_init();
  849. + struct device_node *pmc_np;
  850. if (at91_cpuidle_device.dev.platform_data)
  851. platform_device_register(&at91_cpuidle_device);
  852. + pmc_np = of_find_matching_node(NULL, atmel_pmc_ids);
  853. + pmc = of_iomap(pmc_np, 0);
  854. + if (!pmc) {
  855. + pr_err("AT91: PM not supported, PMC not found\n");
  856. + return;
  857. + }
  858. +
  859. + if (pm_idle)
  860. + arm_pm_idle = pm_idle;
  861. +
  862. + at91_pm_sram_init();
  863. +
  864. if (at91_suspend_sram_fn)
  865. suspend_set_ops(&at91_pm_ops);
  866. else
  867. @@ -440,7 +480,7 @@ void __init at91rm9200_pm_init(void)
  868. at91_pm_data.uhp_udp_mask = AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP;
  869. at91_pm_data.memctrl = AT91_MEMCTRL_MC;
  870. - at91_pm_init();
  871. + at91_pm_init(at91rm9200_idle);
  872. }
  873. void __init at91sam9260_pm_init(void)
  874. @@ -448,7 +488,7 @@ void __init at91sam9260_pm_init(void)
  875. at91_dt_ramc();
  876. at91_pm_data.memctrl = AT91_MEMCTRL_SDRAMC;
  877. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  878. - return at91_pm_init();
  879. + at91_pm_init(at91sam9_idle);
  880. }
  881. void __init at91sam9g45_pm_init(void)
  882. @@ -456,7 +496,7 @@ void __init at91sam9g45_pm_init(void)
  883. at91_dt_ramc();
  884. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP;
  885. at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  886. - return at91_pm_init();
  887. + at91_pm_init(at91sam9_idle);
  888. }
  889. void __init at91sam9x5_pm_init(void)
  890. @@ -464,5 +504,13 @@ void __init at91sam9x5_pm_init(void)
  891. at91_dt_ramc();
  892. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  893. at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  894. - return at91_pm_init();
  895. + at91_pm_init(at91sam9_idle);
  896. +}
  897. +
  898. +void __init sama5_pm_init(void)
  899. +{
  900. + at91_dt_ramc();
  901. + at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  902. + at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  903. + at91_pm_init(NULL);
  904. }
  905. diff --git a/arch/arm/mach-at91/sama5.c b/arch/arm/mach-at91/sama5.c
  906. index d9cf6799aec0..df8fdf1cf66d 100644
  907. --- a/arch/arm/mach-at91/sama5.c
  908. +++ b/arch/arm/mach-at91/sama5.c
  909. @@ -51,7 +51,7 @@ static void __init sama5_dt_device_init(void)
  910. soc_dev = soc_device_to_device(soc);
  911. of_platform_populate(NULL, of_default_bus_match_table, NULL, soc_dev);
  912. - at91sam9x5_pm_init();
  913. + sama5_pm_init();
  914. }
  915. static const char *const sama5_dt_board_compat[] __initconst = {
  916. diff --git a/arch/arm/mach-exynos/platsmp.c b/arch/arm/mach-exynos/platsmp.c
  917. index 98a2c0cbb833..310dce500d3e 100644
  918. --- a/arch/arm/mach-exynos/platsmp.c
  919. +++ b/arch/arm/mach-exynos/platsmp.c
  920. @@ -230,7 +230,7 @@ static void __iomem *scu_base_addr(void)
  921. return (void __iomem *)(S5P_VA_SCU);
  922. }
  923. -static DEFINE_SPINLOCK(boot_lock);
  924. +static DEFINE_RAW_SPINLOCK(boot_lock);
  925. static void exynos_secondary_init(unsigned int cpu)
  926. {
  927. @@ -243,8 +243,8 @@ static void exynos_secondary_init(unsigned int cpu)
  928. /*
  929. * Synchronise with the boot thread.
  930. */
  931. - spin_lock(&boot_lock);
  932. - spin_unlock(&boot_lock);
  933. + raw_spin_lock(&boot_lock);
  934. + raw_spin_unlock(&boot_lock);
  935. }
  936. int exynos_set_boot_addr(u32 core_id, unsigned long boot_addr)
  937. @@ -308,7 +308,7 @@ static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  938. * Set synchronisation state between this boot processor
  939. * and the secondary one
  940. */
  941. - spin_lock(&boot_lock);
  942. + raw_spin_lock(&boot_lock);
  943. /*
  944. * The secondary processor is waiting to be released from
  945. @@ -335,7 +335,7 @@ static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  946. if (timeout == 0) {
  947. printk(KERN_ERR "cpu1 power enable failed");
  948. - spin_unlock(&boot_lock);
  949. + raw_spin_unlock(&boot_lock);
  950. return -ETIMEDOUT;
  951. }
  952. }
  953. @@ -381,7 +381,7 @@ static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  954. * calibrations, then wait for it to finish
  955. */
  956. fail:
  957. - spin_unlock(&boot_lock);
  958. + raw_spin_unlock(&boot_lock);
  959. return pen_release != -1 ? ret : 0;
  960. }
  961. diff --git a/arch/arm/mach-hisi/platmcpm.c b/arch/arm/mach-hisi/platmcpm.c
  962. index b5f8f5ffda79..9753a84df9c4 100644
  963. --- a/arch/arm/mach-hisi/platmcpm.c
  964. +++ b/arch/arm/mach-hisi/platmcpm.c
  965. @@ -61,7 +61,7 @@
  966. static void __iomem *sysctrl, *fabric;
  967. static int hip04_cpu_table[HIP04_MAX_CLUSTERS][HIP04_MAX_CPUS_PER_CLUSTER];
  968. -static DEFINE_SPINLOCK(boot_lock);
  969. +static DEFINE_RAW_SPINLOCK(boot_lock);
  970. static u32 fabric_phys_addr;
  971. /*
  972. * [0]: bootwrapper physical address
  973. @@ -113,7 +113,7 @@ static int hip04_boot_secondary(unsigned int l_cpu, struct task_struct *idle)
  974. if (cluster >= HIP04_MAX_CLUSTERS || cpu >= HIP04_MAX_CPUS_PER_CLUSTER)
  975. return -EINVAL;
  976. - spin_lock_irq(&boot_lock);
  977. + raw_spin_lock_irq(&boot_lock);
  978. if (hip04_cpu_table[cluster][cpu])
  979. goto out;
  980. @@ -147,7 +147,7 @@ static int hip04_boot_secondary(unsigned int l_cpu, struct task_struct *idle)
  981. out:
  982. hip04_cpu_table[cluster][cpu]++;
  983. - spin_unlock_irq(&boot_lock);
  984. + raw_spin_unlock_irq(&boot_lock);
  985. return 0;
  986. }
  987. @@ -162,11 +162,11 @@ static void hip04_cpu_die(unsigned int l_cpu)
  988. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  989. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  990. - spin_lock(&boot_lock);
  991. + raw_spin_lock(&boot_lock);
  992. hip04_cpu_table[cluster][cpu]--;
  993. if (hip04_cpu_table[cluster][cpu] == 1) {
  994. /* A power_up request went ahead of us. */
  995. - spin_unlock(&boot_lock);
  996. + raw_spin_unlock(&boot_lock);
  997. return;
  998. } else if (hip04_cpu_table[cluster][cpu] > 1) {
  999. pr_err("Cluster %d CPU%d boots multiple times\n", cluster, cpu);
  1000. @@ -174,7 +174,7 @@ static void hip04_cpu_die(unsigned int l_cpu)
  1001. }
  1002. last_man = hip04_cluster_is_down(cluster);
  1003. - spin_unlock(&boot_lock);
  1004. + raw_spin_unlock(&boot_lock);
  1005. if (last_man) {
  1006. /* Since it's Cortex A15, disable L2 prefetching. */
  1007. asm volatile(
  1008. @@ -203,7 +203,7 @@ static int hip04_cpu_kill(unsigned int l_cpu)
  1009. cpu >= HIP04_MAX_CPUS_PER_CLUSTER);
  1010. count = TIMEOUT_MSEC / POLL_MSEC;
  1011. - spin_lock_irq(&boot_lock);
  1012. + raw_spin_lock_irq(&boot_lock);
  1013. for (tries = 0; tries < count; tries++) {
  1014. if (hip04_cpu_table[cluster][cpu])
  1015. goto err;
  1016. @@ -211,10 +211,10 @@ static int hip04_cpu_kill(unsigned int l_cpu)
  1017. data = readl_relaxed(sysctrl + SC_CPU_RESET_STATUS(cluster));
  1018. if (data & CORE_WFI_STATUS(cpu))
  1019. break;
  1020. - spin_unlock_irq(&boot_lock);
  1021. + raw_spin_unlock_irq(&boot_lock);
  1022. /* Wait for clean L2 when the whole cluster is down. */
  1023. msleep(POLL_MSEC);
  1024. - spin_lock_irq(&boot_lock);
  1025. + raw_spin_lock_irq(&boot_lock);
  1026. }
  1027. if (tries >= count)
  1028. goto err;
  1029. @@ -231,10 +231,10 @@ static int hip04_cpu_kill(unsigned int l_cpu)
  1030. goto err;
  1031. if (hip04_cluster_is_down(cluster))
  1032. hip04_set_snoop_filter(cluster, 0);
  1033. - spin_unlock_irq(&boot_lock);
  1034. + raw_spin_unlock_irq(&boot_lock);
  1035. return 1;
  1036. err:
  1037. - spin_unlock_irq(&boot_lock);
  1038. + raw_spin_unlock_irq(&boot_lock);
  1039. return 0;
  1040. }
  1041. #endif
  1042. diff --git a/arch/arm/mach-imx/Kconfig b/arch/arm/mach-imx/Kconfig
  1043. index 8ceda2844c4f..08bcf8fb76f2 100644
  1044. --- a/arch/arm/mach-imx/Kconfig
  1045. +++ b/arch/arm/mach-imx/Kconfig
  1046. @@ -524,7 +524,7 @@ config SOC_IMX6Q
  1047. bool "i.MX6 Quad/DualLite support"
  1048. select ARM_ERRATA_764369 if SMP
  1049. select HAVE_ARM_SCU if SMP
  1050. - select HAVE_ARM_TWD if SMP
  1051. + select HAVE_ARM_TWD
  1052. select PCI_DOMAINS if PCI
  1053. select PINCTRL_IMX6Q
  1054. select SOC_IMX6
  1055. diff --git a/arch/arm/mach-omap2/omap-smp.c b/arch/arm/mach-omap2/omap-smp.c
  1056. index 79e1f876d1c9..7e625c17f78e 100644
  1057. --- a/arch/arm/mach-omap2/omap-smp.c
  1058. +++ b/arch/arm/mach-omap2/omap-smp.c
  1059. @@ -43,7 +43,7 @@
  1060. /* SCU base address */
  1061. static void __iomem *scu_base;
  1062. -static DEFINE_SPINLOCK(boot_lock);
  1063. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1064. void __iomem *omap4_get_scu_base(void)
  1065. {
  1066. @@ -74,8 +74,8 @@ static void omap4_secondary_init(unsigned int cpu)
  1067. /*
  1068. * Synchronise with the boot thread.
  1069. */
  1070. - spin_lock(&boot_lock);
  1071. - spin_unlock(&boot_lock);
  1072. + raw_spin_lock(&boot_lock);
  1073. + raw_spin_unlock(&boot_lock);
  1074. }
  1075. static int omap4_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1076. @@ -89,7 +89,7 @@ static int omap4_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1077. * Set synchronisation state between this boot processor
  1078. * and the secondary one
  1079. */
  1080. - spin_lock(&boot_lock);
  1081. + raw_spin_lock(&boot_lock);
  1082. /*
  1083. * Update the AuxCoreBoot0 with boot state for secondary core.
  1084. @@ -166,7 +166,7 @@ static int omap4_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1085. * Now the secondary core is starting up let it run its
  1086. * calibrations, then wait for it to finish
  1087. */
  1088. - spin_unlock(&boot_lock);
  1089. + raw_spin_unlock(&boot_lock);
  1090. return 0;
  1091. }
  1092. diff --git a/arch/arm/mach-prima2/platsmp.c b/arch/arm/mach-prima2/platsmp.c
  1093. index e46c91094dde..dcb3ed0c26da 100644
  1094. --- a/arch/arm/mach-prima2/platsmp.c
  1095. +++ b/arch/arm/mach-prima2/platsmp.c
  1096. @@ -22,7 +22,7 @@
  1097. static void __iomem *clk_base;
  1098. -static DEFINE_SPINLOCK(boot_lock);
  1099. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1100. static void sirfsoc_secondary_init(unsigned int cpu)
  1101. {
  1102. @@ -36,8 +36,8 @@ static void sirfsoc_secondary_init(unsigned int cpu)
  1103. /*
  1104. * Synchronise with the boot thread.
  1105. */
  1106. - spin_lock(&boot_lock);
  1107. - spin_unlock(&boot_lock);
  1108. + raw_spin_lock(&boot_lock);
  1109. + raw_spin_unlock(&boot_lock);
  1110. }
  1111. static const struct of_device_id clk_ids[] = {
  1112. @@ -75,7 +75,7 @@ static int sirfsoc_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1113. /* make sure write buffer is drained */
  1114. mb();
  1115. - spin_lock(&boot_lock);
  1116. + raw_spin_lock(&boot_lock);
  1117. /*
  1118. * The secondary processor is waiting to be released from
  1119. @@ -107,7 +107,7 @@ static int sirfsoc_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1120. * now the secondary core is starting up let it run its
  1121. * calibrations, then wait for it to finish
  1122. */
  1123. - spin_unlock(&boot_lock);
  1124. + raw_spin_unlock(&boot_lock);
  1125. return pen_release != -1 ? -ENOSYS : 0;
  1126. }
  1127. diff --git a/arch/arm/mach-qcom/platsmp.c b/arch/arm/mach-qcom/platsmp.c
  1128. index 9b00123a315d..0a49fe1bc8cf 100644
  1129. --- a/arch/arm/mach-qcom/platsmp.c
  1130. +++ b/arch/arm/mach-qcom/platsmp.c
  1131. @@ -46,7 +46,7 @@
  1132. extern void secondary_startup_arm(void);
  1133. -static DEFINE_SPINLOCK(boot_lock);
  1134. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1135. #ifdef CONFIG_HOTPLUG_CPU
  1136. static void qcom_cpu_die(unsigned int cpu)
  1137. @@ -60,8 +60,8 @@ static void qcom_secondary_init(unsigned int cpu)
  1138. /*
  1139. * Synchronise with the boot thread.
  1140. */
  1141. - spin_lock(&boot_lock);
  1142. - spin_unlock(&boot_lock);
  1143. + raw_spin_lock(&boot_lock);
  1144. + raw_spin_unlock(&boot_lock);
  1145. }
  1146. static int scss_release_secondary(unsigned int cpu)
  1147. @@ -284,7 +284,7 @@ static int qcom_boot_secondary(unsigned int cpu, int (*func)(unsigned int))
  1148. * set synchronisation state between this boot processor
  1149. * and the secondary one
  1150. */
  1151. - spin_lock(&boot_lock);
  1152. + raw_spin_lock(&boot_lock);
  1153. /*
  1154. * Send the secondary CPU a soft interrupt, thereby causing
  1155. @@ -297,7 +297,7 @@ static int qcom_boot_secondary(unsigned int cpu, int (*func)(unsigned int))
  1156. * now the secondary core is starting up let it run its
  1157. * calibrations, then wait for it to finish
  1158. */
  1159. - spin_unlock(&boot_lock);
  1160. + raw_spin_unlock(&boot_lock);
  1161. return ret;
  1162. }
  1163. diff --git a/arch/arm/mach-spear/platsmp.c b/arch/arm/mach-spear/platsmp.c
  1164. index fd4297713d67..b0553b2c2d53 100644
  1165. --- a/arch/arm/mach-spear/platsmp.c
  1166. +++ b/arch/arm/mach-spear/platsmp.c
  1167. @@ -32,7 +32,7 @@ static void write_pen_release(int val)
  1168. sync_cache_w(&pen_release);
  1169. }
  1170. -static DEFINE_SPINLOCK(boot_lock);
  1171. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1172. static void __iomem *scu_base = IOMEM(VA_SCU_BASE);
  1173. @@ -47,8 +47,8 @@ static void spear13xx_secondary_init(unsigned int cpu)
  1174. /*
  1175. * Synchronise with the boot thread.
  1176. */
  1177. - spin_lock(&boot_lock);
  1178. - spin_unlock(&boot_lock);
  1179. + raw_spin_lock(&boot_lock);
  1180. + raw_spin_unlock(&boot_lock);
  1181. }
  1182. static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1183. @@ -59,7 +59,7 @@ static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1184. * set synchronisation state between this boot processor
  1185. * and the secondary one
  1186. */
  1187. - spin_lock(&boot_lock);
  1188. + raw_spin_lock(&boot_lock);
  1189. /*
  1190. * The secondary processor is waiting to be released from
  1191. @@ -84,7 +84,7 @@ static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1192. * now the secondary core is starting up let it run its
  1193. * calibrations, then wait for it to finish
  1194. */
  1195. - spin_unlock(&boot_lock);
  1196. + raw_spin_unlock(&boot_lock);
  1197. return pen_release != -1 ? -ENOSYS : 0;
  1198. }
  1199. diff --git a/arch/arm/mach-sti/platsmp.c b/arch/arm/mach-sti/platsmp.c
  1200. index c4ad6eae67fa..e830b20b212f 100644
  1201. --- a/arch/arm/mach-sti/platsmp.c
  1202. +++ b/arch/arm/mach-sti/platsmp.c
  1203. @@ -35,7 +35,7 @@ static void write_pen_release(int val)
  1204. sync_cache_w(&pen_release);
  1205. }
  1206. -static DEFINE_SPINLOCK(boot_lock);
  1207. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1208. static void sti_secondary_init(unsigned int cpu)
  1209. {
  1210. @@ -48,8 +48,8 @@ static void sti_secondary_init(unsigned int cpu)
  1211. /*
  1212. * Synchronise with the boot thread.
  1213. */
  1214. - spin_lock(&boot_lock);
  1215. - spin_unlock(&boot_lock);
  1216. + raw_spin_lock(&boot_lock);
  1217. + raw_spin_unlock(&boot_lock);
  1218. }
  1219. static int sti_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1220. @@ -60,7 +60,7 @@ static int sti_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1221. * set synchronisation state between this boot processor
  1222. * and the secondary one
  1223. */
  1224. - spin_lock(&boot_lock);
  1225. + raw_spin_lock(&boot_lock);
  1226. /*
  1227. * The secondary processor is waiting to be released from
  1228. @@ -91,7 +91,7 @@ static int sti_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1229. * now the secondary core is starting up let it run its
  1230. * calibrations, then wait for it to finish
  1231. */
  1232. - spin_unlock(&boot_lock);
  1233. + raw_spin_unlock(&boot_lock);
  1234. return pen_release != -1 ? -ENOSYS : 0;
  1235. }
  1236. diff --git a/arch/arm/mm/fault.c b/arch/arm/mm/fault.c
  1237. index 0d20cd594017..a11dc6d8ca02 100644
  1238. --- a/arch/arm/mm/fault.c
  1239. +++ b/arch/arm/mm/fault.c
  1240. @@ -433,6 +433,9 @@ do_translation_fault(unsigned long addr, unsigned int fsr,
  1241. if (addr < TASK_SIZE)
  1242. return do_page_fault(addr, fsr, regs);
  1243. + if (interrupts_enabled(regs))
  1244. + local_irq_enable();
  1245. +
  1246. if (user_mode(regs))
  1247. goto bad_area;
  1248. @@ -500,6 +503,9 @@ do_translation_fault(unsigned long addr, unsigned int fsr,
  1249. static int
  1250. do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  1251. {
  1252. + if (interrupts_enabled(regs))
  1253. + local_irq_enable();
  1254. +
  1255. do_bad_area(addr, fsr, regs);
  1256. return 0;
  1257. }
  1258. diff --git a/arch/arm/mm/highmem.c b/arch/arm/mm/highmem.c
  1259. index d02f8187b1cc..542692dbd40a 100644
  1260. --- a/arch/arm/mm/highmem.c
  1261. +++ b/arch/arm/mm/highmem.c
  1262. @@ -34,6 +34,11 @@ static inline pte_t get_fixmap_pte(unsigned long vaddr)
  1263. return *ptep;
  1264. }
  1265. +static unsigned int fixmap_idx(int type)
  1266. +{
  1267. + return FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  1268. +}
  1269. +
  1270. void *kmap(struct page *page)
  1271. {
  1272. might_sleep();
  1273. @@ -54,12 +59,13 @@ EXPORT_SYMBOL(kunmap);
  1274. void *kmap_atomic(struct page *page)
  1275. {
  1276. + pte_t pte = mk_pte(page, kmap_prot);
  1277. unsigned int idx;
  1278. unsigned long vaddr;
  1279. void *kmap;
  1280. int type;
  1281. - preempt_disable();
  1282. + preempt_disable_nort();
  1283. pagefault_disable();
  1284. if (!PageHighMem(page))
  1285. return page_address(page);
  1286. @@ -79,7 +85,7 @@ void *kmap_atomic(struct page *page)
  1287. type = kmap_atomic_idx_push();
  1288. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  1289. + idx = fixmap_idx(type);
  1290. vaddr = __fix_to_virt(idx);
  1291. #ifdef CONFIG_DEBUG_HIGHMEM
  1292. /*
  1293. @@ -93,7 +99,10 @@ void *kmap_atomic(struct page *page)
  1294. * in place, so the contained TLB flush ensures the TLB is updated
  1295. * with the new mapping.
  1296. */
  1297. - set_fixmap_pte(idx, mk_pte(page, kmap_prot));
  1298. +#ifdef CONFIG_PREEMPT_RT_FULL
  1299. + current->kmap_pte[type] = pte;
  1300. +#endif
  1301. + set_fixmap_pte(idx, pte);
  1302. return (void *)vaddr;
  1303. }
  1304. @@ -106,44 +115,75 @@ void __kunmap_atomic(void *kvaddr)
  1305. if (kvaddr >= (void *)FIXADDR_START) {
  1306. type = kmap_atomic_idx();
  1307. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  1308. + idx = fixmap_idx(type);
  1309. if (cache_is_vivt())
  1310. __cpuc_flush_dcache_area((void *)vaddr, PAGE_SIZE);
  1311. +#ifdef CONFIG_PREEMPT_RT_FULL
  1312. + current->kmap_pte[type] = __pte(0);
  1313. +#endif
  1314. #ifdef CONFIG_DEBUG_HIGHMEM
  1315. BUG_ON(vaddr != __fix_to_virt(idx));
  1316. - set_fixmap_pte(idx, __pte(0));
  1317. #else
  1318. (void) idx; /* to kill a warning */
  1319. #endif
  1320. + set_fixmap_pte(idx, __pte(0));
  1321. kmap_atomic_idx_pop();
  1322. } else if (vaddr >= PKMAP_ADDR(0) && vaddr < PKMAP_ADDR(LAST_PKMAP)) {
  1323. /* this address was obtained through kmap_high_get() */
  1324. kunmap_high(pte_page(pkmap_page_table[PKMAP_NR(vaddr)]));
  1325. }
  1326. pagefault_enable();
  1327. - preempt_enable();
  1328. + preempt_enable_nort();
  1329. }
  1330. EXPORT_SYMBOL(__kunmap_atomic);
  1331. void *kmap_atomic_pfn(unsigned long pfn)
  1332. {
  1333. + pte_t pte = pfn_pte(pfn, kmap_prot);
  1334. unsigned long vaddr;
  1335. int idx, type;
  1336. struct page *page = pfn_to_page(pfn);
  1337. - preempt_disable();
  1338. + preempt_disable_nort();
  1339. pagefault_disable();
  1340. if (!PageHighMem(page))
  1341. return page_address(page);
  1342. type = kmap_atomic_idx_push();
  1343. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  1344. + idx = fixmap_idx(type);
  1345. vaddr = __fix_to_virt(idx);
  1346. #ifdef CONFIG_DEBUG_HIGHMEM
  1347. BUG_ON(!pte_none(get_fixmap_pte(vaddr)));
  1348. #endif
  1349. - set_fixmap_pte(idx, pfn_pte(pfn, kmap_prot));
  1350. +#ifdef CONFIG_PREEMPT_RT_FULL
  1351. + current->kmap_pte[type] = pte;
  1352. +#endif
  1353. + set_fixmap_pte(idx, pte);
  1354. return (void *)vaddr;
  1355. }
  1356. +#if defined CONFIG_PREEMPT_RT_FULL
  1357. +void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  1358. +{
  1359. + int i;
  1360. +
  1361. + /*
  1362. + * Clear @prev's kmap_atomic mappings
  1363. + */
  1364. + for (i = 0; i < prev_p->kmap_idx; i++) {
  1365. + int idx = fixmap_idx(i);
  1366. +
  1367. + set_fixmap_pte(idx, __pte(0));
  1368. + }
  1369. + /*
  1370. + * Restore @next_p's kmap_atomic mappings
  1371. + */
  1372. + for (i = 0; i < next_p->kmap_idx; i++) {
  1373. + int idx = fixmap_idx(i);
  1374. +
  1375. + if (!pte_none(next_p->kmap_pte[i]))
  1376. + set_fixmap_pte(idx, next_p->kmap_pte[i]);
  1377. + }
  1378. +}
  1379. +#endif
  1380. diff --git a/arch/arm/plat-versatile/platsmp.c b/arch/arm/plat-versatile/platsmp.c
  1381. index 53feb90c840c..b4a8d54fc3f3 100644
  1382. --- a/arch/arm/plat-versatile/platsmp.c
  1383. +++ b/arch/arm/plat-versatile/platsmp.c
  1384. @@ -30,7 +30,7 @@ static void write_pen_release(int val)
  1385. sync_cache_w(&pen_release);
  1386. }
  1387. -static DEFINE_SPINLOCK(boot_lock);
  1388. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1389. void versatile_secondary_init(unsigned int cpu)
  1390. {
  1391. @@ -43,8 +43,8 @@ void versatile_secondary_init(unsigned int cpu)
  1392. /*
  1393. * Synchronise with the boot thread.
  1394. */
  1395. - spin_lock(&boot_lock);
  1396. - spin_unlock(&boot_lock);
  1397. + raw_spin_lock(&boot_lock);
  1398. + raw_spin_unlock(&boot_lock);
  1399. }
  1400. int versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1401. @@ -55,7 +55,7 @@ int versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1402. * Set synchronisation state between this boot processor
  1403. * and the secondary one
  1404. */
  1405. - spin_lock(&boot_lock);
  1406. + raw_spin_lock(&boot_lock);
  1407. /*
  1408. * This is really belt and braces; we hold unintended secondary
  1409. @@ -85,7 +85,7 @@ int versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1410. * now the secondary core is starting up let it run its
  1411. * calibrations, then wait for it to finish
  1412. */
  1413. - spin_unlock(&boot_lock);
  1414. + raw_spin_unlock(&boot_lock);
  1415. return pen_release != -1 ? -ENOSYS : 0;
  1416. }
  1417. diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
  1418. index 14cdc6dea493..9196cf82f7be 100644
  1419. --- a/arch/arm64/Kconfig
  1420. +++ b/arch/arm64/Kconfig
  1421. @@ -76,6 +76,7 @@ config ARM64
  1422. select HAVE_PERF_REGS
  1423. select HAVE_PERF_USER_STACK_DUMP
  1424. select HAVE_RCU_TABLE_FREE
  1425. + select HAVE_PREEMPT_LAZY
  1426. select HAVE_SYSCALL_TRACEPOINTS
  1427. select IOMMU_DMA if IOMMU_SUPPORT
  1428. select IRQ_DOMAIN
  1429. @@ -582,7 +583,7 @@ config XEN_DOM0
  1430. config XEN
  1431. bool "Xen guest support on ARM64"
  1432. - depends on ARM64 && OF
  1433. + depends on ARM64 && OF && !PREEMPT_RT_FULL
  1434. select SWIOTLB_XEN
  1435. help
  1436. Say Y if you want to run Linux in a Virtual Machine on Xen on ARM64.
  1437. diff --git a/arch/arm64/include/asm/thread_info.h b/arch/arm64/include/asm/thread_info.h
  1438. index 90c7ff233735..5f4e89fbc290 100644
  1439. --- a/arch/arm64/include/asm/thread_info.h
  1440. +++ b/arch/arm64/include/asm/thread_info.h
  1441. @@ -49,6 +49,7 @@ struct thread_info {
  1442. mm_segment_t addr_limit; /* address limit */
  1443. struct task_struct *task; /* main task structure */
  1444. int preempt_count; /* 0 => preemptable, <0 => bug */
  1445. + int preempt_lazy_count; /* 0 => preemptable, <0 => bug */
  1446. int cpu; /* cpu */
  1447. };
  1448. @@ -103,6 +104,7 @@ static inline struct thread_info *current_thread_info(void)
  1449. #define TIF_NEED_RESCHED 1
  1450. #define TIF_NOTIFY_RESUME 2 /* callback before returning to user */
  1451. #define TIF_FOREIGN_FPSTATE 3 /* CPU's FP state is not current's */
  1452. +#define TIF_NEED_RESCHED_LAZY 4
  1453. #define TIF_NOHZ 7
  1454. #define TIF_SYSCALL_TRACE 8
  1455. #define TIF_SYSCALL_AUDIT 9
  1456. @@ -118,6 +120,7 @@ static inline struct thread_info *current_thread_info(void)
  1457. #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
  1458. #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
  1459. #define _TIF_FOREIGN_FPSTATE (1 << TIF_FOREIGN_FPSTATE)
  1460. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  1461. #define _TIF_NOHZ (1 << TIF_NOHZ)
  1462. #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
  1463. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  1464. @@ -126,7 +129,8 @@ static inline struct thread_info *current_thread_info(void)
  1465. #define _TIF_32BIT (1 << TIF_32BIT)
  1466. #define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
  1467. - _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE)
  1468. + _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE | \
  1469. + _TIF_NEED_RESCHED_LAZY)
  1470. #define _TIF_SYSCALL_WORK (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  1471. _TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | \
  1472. diff --git a/arch/arm64/kernel/asm-offsets.c b/arch/arm64/kernel/asm-offsets.c
  1473. index 087cf9a65359..d74475928399 100644
  1474. --- a/arch/arm64/kernel/asm-offsets.c
  1475. +++ b/arch/arm64/kernel/asm-offsets.c
  1476. @@ -35,6 +35,7 @@ int main(void)
  1477. BLANK();
  1478. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  1479. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  1480. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  1481. DEFINE(TI_ADDR_LIMIT, offsetof(struct thread_info, addr_limit));
  1482. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  1483. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  1484. diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S
  1485. index dccd0c2e9023..a8d4d065fd81 100644
  1486. --- a/arch/arm64/kernel/entry.S
  1487. +++ b/arch/arm64/kernel/entry.S
  1488. @@ -378,11 +378,16 @@ ENDPROC(el1_sync)
  1489. #ifdef CONFIG_PREEMPT
  1490. get_thread_info tsk
  1491. ldr w24, [tsk, #TI_PREEMPT] // get preempt count
  1492. - cbnz w24, 1f // preempt count != 0
  1493. + cbnz w24, 2f // preempt count != 0
  1494. ldr x0, [tsk, #TI_FLAGS] // get flags
  1495. - tbz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  1496. - bl el1_preempt
  1497. + tbnz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  1498. +
  1499. + ldr w24, [tsk, #TI_PREEMPT_LAZY] // get preempt lazy count
  1500. + cbnz w24, 2f // preempt lazy count != 0
  1501. + tbz x0, #TIF_NEED_RESCHED_LAZY, 2f // needs rescheduling?
  1502. 1:
  1503. + bl el1_preempt
  1504. +2:
  1505. #endif
  1506. #ifdef CONFIG_TRACE_IRQFLAGS
  1507. bl trace_hardirqs_on
  1508. @@ -396,6 +401,7 @@ ENDPROC(el1_irq)
  1509. 1: bl preempt_schedule_irq // irq en/disable is done inside
  1510. ldr x0, [tsk, #TI_FLAGS] // get new tasks TI_FLAGS
  1511. tbnz x0, #TIF_NEED_RESCHED, 1b // needs rescheduling?
  1512. + tbnz x0, #TIF_NEED_RESCHED_LAZY, 1b // needs rescheduling?
  1513. ret x24
  1514. #endif
  1515. @@ -640,6 +646,7 @@ ENDPROC(cpu_switch_to)
  1516. */
  1517. work_pending:
  1518. tbnz x1, #TIF_NEED_RESCHED, work_resched
  1519. + tbnz x1, #TIF_NEED_RESCHED_LAZY, work_resched
  1520. /* TIF_SIGPENDING, TIF_NOTIFY_RESUME or TIF_FOREIGN_FPSTATE case */
  1521. ldr x2, [sp, #S_PSTATE]
  1522. mov x0, sp // 'regs'
  1523. diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig
  1524. index 8b0424abc84c..5422d4c0bbdf 100644
  1525. --- a/arch/mips/Kconfig
  1526. +++ b/arch/mips/Kconfig
  1527. @@ -2411,7 +2411,7 @@ config CPU_R4400_WORKAROUNDS
  1528. #
  1529. config HIGHMEM
  1530. bool "High Memory Support"
  1531. - depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA
  1532. + depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA && !PREEMPT_RT_FULL
  1533. config CPU_SUPPORTS_HIGHMEM
  1534. bool
  1535. diff --git a/arch/mips/kvm/mips.c b/arch/mips/kvm/mips.c
  1536. index a017b23ee4aa..8d4d9270140f 100644
  1537. --- a/arch/mips/kvm/mips.c
  1538. +++ b/arch/mips/kvm/mips.c
  1539. @@ -454,8 +454,8 @@ int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1540. dvcpu->arch.wait = 0;
  1541. - if (waitqueue_active(&dvcpu->wq))
  1542. - wake_up_interruptible(&dvcpu->wq);
  1543. + if (swait_active(&dvcpu->wq))
  1544. + swake_up(&dvcpu->wq);
  1545. return 0;
  1546. }
  1547. @@ -1183,8 +1183,8 @@ static void kvm_mips_comparecount_func(unsigned long data)
  1548. kvm_mips_callbacks->queue_timer_int(vcpu);
  1549. vcpu->arch.wait = 0;
  1550. - if (waitqueue_active(&vcpu->wq))
  1551. - wake_up_interruptible(&vcpu->wq);
  1552. + if (swait_active(&vcpu->wq))
  1553. + swake_up(&vcpu->wq);
  1554. }
  1555. /* low level hrtimer wake routine */
  1556. diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
  1557. index dfb1ee8c3e06..cdc3c20ef225 100644
  1558. --- a/arch/powerpc/Kconfig
  1559. +++ b/arch/powerpc/Kconfig
  1560. @@ -60,10 +60,11 @@ config LOCKDEP_SUPPORT
  1561. config RWSEM_GENERIC_SPINLOCK
  1562. bool
  1563. + default y if PREEMPT_RT_FULL
  1564. config RWSEM_XCHGADD_ALGORITHM
  1565. bool
  1566. - default y
  1567. + default y if !PREEMPT_RT_FULL
  1568. config GENERIC_LOCKBREAK
  1569. bool
  1570. @@ -141,6 +142,7 @@ config PPC
  1571. select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
  1572. select GENERIC_STRNCPY_FROM_USER
  1573. select GENERIC_STRNLEN_USER
  1574. + select HAVE_PREEMPT_LAZY
  1575. select HAVE_MOD_ARCH_SPECIFIC
  1576. select MODULES_USE_ELF_RELA
  1577. select CLONE_BACKWARDS
  1578. @@ -319,7 +321,7 @@ menu "Kernel options"
  1579. config HIGHMEM
  1580. bool "High memory support"
  1581. - depends on PPC32
  1582. + depends on PPC32 && !PREEMPT_RT_FULL
  1583. source kernel/Kconfig.hz
  1584. source kernel/Kconfig.preempt
  1585. diff --git a/arch/powerpc/include/asm/kvm_host.h b/arch/powerpc/include/asm/kvm_host.h
  1586. index a92d95aee42d..20376580583f 100644
  1587. --- a/arch/powerpc/include/asm/kvm_host.h
  1588. +++ b/arch/powerpc/include/asm/kvm_host.h
  1589. @@ -286,7 +286,7 @@ struct kvmppc_vcore {
  1590. struct list_head runnable_threads;
  1591. struct list_head preempt_list;
  1592. spinlock_t lock;
  1593. - wait_queue_head_t wq;
  1594. + struct swait_queue_head wq;
  1595. spinlock_t stoltb_lock; /* protects stolen_tb and preempt_tb */
  1596. u64 stolen_tb;
  1597. u64 preempt_tb;
  1598. @@ -627,7 +627,7 @@ struct kvm_vcpu_arch {
  1599. u8 prodded;
  1600. u32 last_inst;
  1601. - wait_queue_head_t *wqp;
  1602. + struct swait_queue_head *wqp;
  1603. struct kvmppc_vcore *vcore;
  1604. int ret;
  1605. int trap;
  1606. diff --git a/arch/powerpc/include/asm/thread_info.h b/arch/powerpc/include/asm/thread_info.h
  1607. index 7efee4a3240b..40e6fa1b85b2 100644
  1608. --- a/arch/powerpc/include/asm/thread_info.h
  1609. +++ b/arch/powerpc/include/asm/thread_info.h
  1610. @@ -42,6 +42,8 @@ struct thread_info {
  1611. int cpu; /* cpu we're on */
  1612. int preempt_count; /* 0 => preemptable,
  1613. <0 => BUG */
  1614. + int preempt_lazy_count; /* 0 => preemptable,
  1615. + <0 => BUG */
  1616. unsigned long local_flags; /* private flags for thread */
  1617. /* low level flags - has atomic operations done on it */
  1618. @@ -82,8 +84,7 @@ static inline struct thread_info *current_thread_info(void)
  1619. #define TIF_SYSCALL_TRACE 0 /* syscall trace active */
  1620. #define TIF_SIGPENDING 1 /* signal pending */
  1621. #define TIF_NEED_RESCHED 2 /* rescheduling necessary */
  1622. -#define TIF_POLLING_NRFLAG 3 /* true if poll_idle() is polling
  1623. - TIF_NEED_RESCHED */
  1624. +#define TIF_NEED_RESCHED_LAZY 3 /* lazy rescheduling necessary */
  1625. #define TIF_32BIT 4 /* 32 bit binary */
  1626. #define TIF_RESTORE_TM 5 /* need to restore TM FP/VEC/VSX */
  1627. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  1628. @@ -101,6 +102,8 @@ static inline struct thread_info *current_thread_info(void)
  1629. #if defined(CONFIG_PPC64)
  1630. #define TIF_ELF2ABI 18 /* function descriptors must die! */
  1631. #endif
  1632. +#define TIF_POLLING_NRFLAG 19 /* true if poll_idle() is polling
  1633. + TIF_NEED_RESCHED */
  1634. /* as above, but as bit values */
  1635. #define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
  1636. @@ -119,14 +122,16 @@ static inline struct thread_info *current_thread_info(void)
  1637. #define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
  1638. #define _TIF_EMULATE_STACK_STORE (1<<TIF_EMULATE_STACK_STORE)
  1639. #define _TIF_NOHZ (1<<TIF_NOHZ)
  1640. +#define _TIF_NEED_RESCHED_LAZY (1<<TIF_NEED_RESCHED_LAZY)
  1641. #define _TIF_SYSCALL_DOTRACE (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  1642. _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT | \
  1643. _TIF_NOHZ)
  1644. #define _TIF_USER_WORK_MASK (_TIF_SIGPENDING | _TIF_NEED_RESCHED | \
  1645. _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  1646. - _TIF_RESTORE_TM)
  1647. + _TIF_RESTORE_TM | _TIF_NEED_RESCHED_LAZY)
  1648. #define _TIF_PERSYSCALL_MASK (_TIF_RESTOREALL|_TIF_NOERROR)
  1649. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  1650. /* Bits in local_flags */
  1651. /* Don't move TLF_NAPPING without adjusting the code in entry_32.S */
  1652. diff --git a/arch/powerpc/kernel/asm-offsets.c b/arch/powerpc/kernel/asm-offsets.c
  1653. index 40da69163d51..bd040815334b 100644
  1654. --- a/arch/powerpc/kernel/asm-offsets.c
  1655. +++ b/arch/powerpc/kernel/asm-offsets.c
  1656. @@ -160,6 +160,7 @@ int main(void)
  1657. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  1658. DEFINE(TI_LOCAL_FLAGS, offsetof(struct thread_info, local_flags));
  1659. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  1660. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  1661. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  1662. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  1663. diff --git a/arch/powerpc/kernel/entry_32.S b/arch/powerpc/kernel/entry_32.S
  1664. index 2405631e91a2..c21b4b42eaa0 100644
  1665. --- a/arch/powerpc/kernel/entry_32.S
  1666. +++ b/arch/powerpc/kernel/entry_32.S
  1667. @@ -818,7 +818,14 @@ user_exc_return: /* r10 contains MSR_KERNEL here */
  1668. cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  1669. bne restore
  1670. andi. r8,r8,_TIF_NEED_RESCHED
  1671. + bne+ 1f
  1672. + lwz r0,TI_PREEMPT_LAZY(r9)
  1673. + cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  1674. + bne restore
  1675. + lwz r0,TI_FLAGS(r9)
  1676. + andi. r0,r0,_TIF_NEED_RESCHED_LAZY
  1677. beq+ restore
  1678. +1:
  1679. lwz r3,_MSR(r1)
  1680. andi. r0,r3,MSR_EE /* interrupts off? */
  1681. beq restore /* don't schedule if so */
  1682. @@ -829,11 +836,11 @@ user_exc_return: /* r10 contains MSR_KERNEL here */
  1683. */
  1684. bl trace_hardirqs_off
  1685. #endif
  1686. -1: bl preempt_schedule_irq
  1687. +2: bl preempt_schedule_irq
  1688. CURRENT_THREAD_INFO(r9, r1)
  1689. lwz r3,TI_FLAGS(r9)
  1690. - andi. r0,r3,_TIF_NEED_RESCHED
  1691. - bne- 1b
  1692. + andi. r0,r3,_TIF_NEED_RESCHED_MASK
  1693. + bne- 2b
  1694. #ifdef CONFIG_TRACE_IRQFLAGS
  1695. /* And now, to properly rebalance the above, we tell lockdep they
  1696. * are being turned back on, which will happen when we return
  1697. @@ -1154,7 +1161,7 @@ END_FTR_SECTION_IFSET(CPU_FTR_NEED_PAIRED_STWCX)
  1698. #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
  1699. do_work: /* r10 contains MSR_KERNEL here */
  1700. - andi. r0,r9,_TIF_NEED_RESCHED
  1701. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  1702. beq do_user_signal
  1703. do_resched: /* r10 contains MSR_KERNEL here */
  1704. @@ -1175,7 +1182,7 @@ do_resched: /* r10 contains MSR_KERNEL here */
  1705. MTMSRD(r10) /* disable interrupts */
  1706. CURRENT_THREAD_INFO(r9, r1)
  1707. lwz r9,TI_FLAGS(r9)
  1708. - andi. r0,r9,_TIF_NEED_RESCHED
  1709. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  1710. bne- do_resched
  1711. andi. r0,r9,_TIF_USER_WORK_MASK
  1712. beq restore_user
  1713. diff --git a/arch/powerpc/kernel/entry_64.S b/arch/powerpc/kernel/entry_64.S
  1714. index f6fd0332c3a2..96235fe0a581 100644
  1715. --- a/arch/powerpc/kernel/entry_64.S
  1716. +++ b/arch/powerpc/kernel/entry_64.S
  1717. @@ -683,7 +683,7 @@ _GLOBAL(ret_from_except_lite)
  1718. #else
  1719. beq restore
  1720. #endif
  1721. -1: andi. r0,r4,_TIF_NEED_RESCHED
  1722. +1: andi. r0,r4,_TIF_NEED_RESCHED_MASK
  1723. beq 2f
  1724. bl restore_interrupts
  1725. SCHEDULE_USER
  1726. @@ -745,10 +745,18 @@ _GLOBAL(ret_from_except_lite)
  1727. #ifdef CONFIG_PREEMPT
  1728. /* Check if we need to preempt */
  1729. + lwz r8,TI_PREEMPT(r9)
  1730. + cmpwi 0,r8,0 /* if non-zero, just restore regs and return */
  1731. + bne restore
  1732. andi. r0,r4,_TIF_NEED_RESCHED
  1733. + bne+ check_count
  1734. +
  1735. + andi. r0,r4,_TIF_NEED_RESCHED_LAZY
  1736. beq+ restore
  1737. + lwz r8,TI_PREEMPT_LAZY(r9)
  1738. +
  1739. /* Check that preempt_count() == 0 and interrupts are enabled */
  1740. - lwz r8,TI_PREEMPT(r9)
  1741. +check_count:
  1742. cmpwi cr1,r8,0
  1743. ld r0,SOFTE(r1)
  1744. cmpdi r0,0
  1745. @@ -765,7 +773,7 @@ _GLOBAL(ret_from_except_lite)
  1746. /* Re-test flags and eventually loop */
  1747. CURRENT_THREAD_INFO(r9, r1)
  1748. ld r4,TI_FLAGS(r9)
  1749. - andi. r0,r4,_TIF_NEED_RESCHED
  1750. + andi. r0,r4,_TIF_NEED_RESCHED_MASK
  1751. bne 1b
  1752. /*
  1753. diff --git a/arch/powerpc/kernel/irq.c b/arch/powerpc/kernel/irq.c
  1754. index 290559df1e8b..070afa6da35d 100644
  1755. --- a/arch/powerpc/kernel/irq.c
  1756. +++ b/arch/powerpc/kernel/irq.c
  1757. @@ -614,6 +614,7 @@ void irq_ctx_init(void)
  1758. }
  1759. }
  1760. +#ifndef CONFIG_PREEMPT_RT_FULL
  1761. void do_softirq_own_stack(void)
  1762. {
  1763. struct thread_info *curtp, *irqtp;
  1764. @@ -631,6 +632,7 @@ void do_softirq_own_stack(void)
  1765. if (irqtp->flags)
  1766. set_bits(irqtp->flags, &curtp->flags);
  1767. }
  1768. +#endif
  1769. irq_hw_number_t virq_to_hw(unsigned int virq)
  1770. {
  1771. diff --git a/arch/powerpc/kernel/misc_32.S b/arch/powerpc/kernel/misc_32.S
  1772. index df4efa304b2c..9cb0c2f6e7ac 100644
  1773. --- a/arch/powerpc/kernel/misc_32.S
  1774. +++ b/arch/powerpc/kernel/misc_32.S
  1775. @@ -40,6 +40,7 @@
  1776. * We store the saved ksp_limit in the unused part
  1777. * of the STACK_FRAME_OVERHEAD
  1778. */
  1779. +#ifndef CONFIG_PREEMPT_RT_FULL
  1780. _GLOBAL(call_do_softirq)
  1781. mflr r0
  1782. stw r0,4(r1)
  1783. @@ -56,6 +57,7 @@ _GLOBAL(call_do_softirq)
  1784. stw r10,THREAD+KSP_LIMIT(r2)
  1785. mtlr r0
  1786. blr
  1787. +#endif
  1788. /*
  1789. * void call_do_irq(struct pt_regs *regs, struct thread_info *irqtp);
  1790. diff --git a/arch/powerpc/kernel/misc_64.S b/arch/powerpc/kernel/misc_64.S
  1791. index db475d41b57a..96b7ef80e05d 100644
  1792. --- a/arch/powerpc/kernel/misc_64.S
  1793. +++ b/arch/powerpc/kernel/misc_64.S
  1794. @@ -30,6 +30,7 @@
  1795. .text
  1796. +#ifndef CONFIG_PREEMPT_RT_FULL
  1797. _GLOBAL(call_do_softirq)
  1798. mflr r0
  1799. std r0,16(r1)
  1800. @@ -40,6 +41,7 @@ _GLOBAL(call_do_softirq)
  1801. ld r0,16(r1)
  1802. mtlr r0
  1803. blr
  1804. +#endif
  1805. _GLOBAL(call_do_irq)
  1806. mflr r0
  1807. diff --git a/arch/powerpc/kvm/Kconfig b/arch/powerpc/kvm/Kconfig
  1808. index c2024ac9d4e8..2303788da7e1 100644
  1809. --- a/arch/powerpc/kvm/Kconfig
  1810. +++ b/arch/powerpc/kvm/Kconfig
  1811. @@ -172,6 +172,7 @@ config KVM_E500MC
  1812. config KVM_MPIC
  1813. bool "KVM in-kernel MPIC emulation"
  1814. depends on KVM && E500
  1815. + depends on !PREEMPT_RT_FULL
  1816. select HAVE_KVM_IRQCHIP
  1817. select HAVE_KVM_IRQFD
  1818. select HAVE_KVM_IRQ_ROUTING
  1819. diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
  1820. index 428563b195c3..4c42c3935025 100644
  1821. --- a/arch/powerpc/kvm/book3s_hv.c
  1822. +++ b/arch/powerpc/kvm/book3s_hv.c
  1823. @@ -114,11 +114,11 @@ static bool kvmppc_ipi_thread(int cpu)
  1824. static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
  1825. {
  1826. int cpu;
  1827. - wait_queue_head_t *wqp;
  1828. + struct swait_queue_head *wqp;
  1829. wqp = kvm_arch_vcpu_wq(vcpu);
  1830. - if (waitqueue_active(wqp)) {
  1831. - wake_up_interruptible(wqp);
  1832. + if (swait_active(wqp)) {
  1833. + swake_up(wqp);
  1834. ++vcpu->stat.halt_wakeup;
  1835. }
  1836. @@ -707,8 +707,8 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
  1837. tvcpu->arch.prodded = 1;
  1838. smp_mb();
  1839. if (vcpu->arch.ceded) {
  1840. - if (waitqueue_active(&vcpu->wq)) {
  1841. - wake_up_interruptible(&vcpu->wq);
  1842. + if (swait_active(&vcpu->wq)) {
  1843. + swake_up(&vcpu->wq);
  1844. vcpu->stat.halt_wakeup++;
  1845. }
  1846. }
  1847. @@ -1453,7 +1453,7 @@ static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
  1848. INIT_LIST_HEAD(&vcore->runnable_threads);
  1849. spin_lock_init(&vcore->lock);
  1850. spin_lock_init(&vcore->stoltb_lock);
  1851. - init_waitqueue_head(&vcore->wq);
  1852. + init_swait_queue_head(&vcore->wq);
  1853. vcore->preempt_tb = TB_NIL;
  1854. vcore->lpcr = kvm->arch.lpcr;
  1855. vcore->first_vcpuid = core * threads_per_subcore;
  1856. @@ -2525,10 +2525,9 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  1857. {
  1858. struct kvm_vcpu *vcpu;
  1859. int do_sleep = 1;
  1860. + DECLARE_SWAITQUEUE(wait);
  1861. - DEFINE_WAIT(wait);
  1862. -
  1863. - prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  1864. + prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  1865. /*
  1866. * Check one last time for pending exceptions and ceded state after
  1867. @@ -2542,7 +2541,7 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  1868. }
  1869. if (!do_sleep) {
  1870. - finish_wait(&vc->wq, &wait);
  1871. + finish_swait(&vc->wq, &wait);
  1872. return;
  1873. }
  1874. @@ -2550,7 +2549,7 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  1875. trace_kvmppc_vcore_blocked(vc, 0);
  1876. spin_unlock(&vc->lock);
  1877. schedule();
  1878. - finish_wait(&vc->wq, &wait);
  1879. + finish_swait(&vc->wq, &wait);
  1880. spin_lock(&vc->lock);
  1881. vc->vcore_state = VCORE_INACTIVE;
  1882. trace_kvmppc_vcore_blocked(vc, 1);
  1883. @@ -2606,7 +2605,7 @@ static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  1884. kvmppc_start_thread(vcpu, vc);
  1885. trace_kvm_guest_enter(vcpu);
  1886. } else if (vc->vcore_state == VCORE_SLEEPING) {
  1887. - wake_up(&vc->wq);
  1888. + swake_up(&vc->wq);
  1889. }
  1890. }
  1891. diff --git a/arch/powerpc/platforms/ps3/device-init.c b/arch/powerpc/platforms/ps3/device-init.c
  1892. index 3f175e8aedb4..c4c02f91904c 100644
  1893. --- a/arch/powerpc/platforms/ps3/device-init.c
  1894. +++ b/arch/powerpc/platforms/ps3/device-init.c
  1895. @@ -752,7 +752,7 @@ static int ps3_notification_read_write(struct ps3_notification_device *dev,
  1896. }
  1897. pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
  1898. - res = wait_event_interruptible(dev->done.wait,
  1899. + res = swait_event_interruptible(dev->done.wait,
  1900. dev->done.done || kthread_should_stop());
  1901. if (kthread_should_stop())
  1902. res = -EINTR;
  1903. diff --git a/arch/s390/include/asm/kvm_host.h b/arch/s390/include/asm/kvm_host.h
  1904. index e9a983f40a24..bbdc539fb3c6 100644
  1905. --- a/arch/s390/include/asm/kvm_host.h
  1906. +++ b/arch/s390/include/asm/kvm_host.h
  1907. @@ -427,7 +427,7 @@ struct kvm_s390_irq_payload {
  1908. struct kvm_s390_local_interrupt {
  1909. spinlock_t lock;
  1910. struct kvm_s390_float_interrupt *float_int;
  1911. - wait_queue_head_t *wq;
  1912. + struct swait_queue_head *wq;
  1913. atomic_t *cpuflags;
  1914. DECLARE_BITMAP(sigp_emerg_pending, KVM_MAX_VCPUS);
  1915. struct kvm_s390_irq_payload irq;
  1916. diff --git a/arch/s390/kvm/interrupt.c b/arch/s390/kvm/interrupt.c
  1917. index 6a75352f453c..cc862c486002 100644
  1918. --- a/arch/s390/kvm/interrupt.c
  1919. +++ b/arch/s390/kvm/interrupt.c
  1920. @@ -868,13 +868,13 @@ int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
  1921. void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
  1922. {
  1923. - if (waitqueue_active(&vcpu->wq)) {
  1924. + if (swait_active(&vcpu->wq)) {
  1925. /*
  1926. * The vcpu gave up the cpu voluntarily, mark it as a good
  1927. * yield-candidate.
  1928. */
  1929. vcpu->preempted = true;
  1930. - wake_up_interruptible(&vcpu->wq);
  1931. + swake_up(&vcpu->wq);
  1932. vcpu->stat.halt_wakeup++;
  1933. }
  1934. }
  1935. diff --git a/arch/sh/kernel/irq.c b/arch/sh/kernel/irq.c
  1936. index 6c0378c0b8b5..abd58b4dff97 100644
  1937. --- a/arch/sh/kernel/irq.c
  1938. +++ b/arch/sh/kernel/irq.c
  1939. @@ -147,6 +147,7 @@ void irq_ctx_exit(int cpu)
  1940. hardirq_ctx[cpu] = NULL;
  1941. }
  1942. +#ifndef CONFIG_PREEMPT_RT_FULL
  1943. void do_softirq_own_stack(void)
  1944. {
  1945. struct thread_info *curctx;
  1946. @@ -174,6 +175,7 @@ void do_softirq_own_stack(void)
  1947. "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
  1948. );
  1949. }
  1950. +#endif
  1951. #else
  1952. static inline void handle_one_irq(unsigned int irq)
  1953. {
  1954. diff --git a/arch/sparc/Kconfig b/arch/sparc/Kconfig
  1955. index 94f4ac21761b..d7c369d061cf 100644
  1956. --- a/arch/sparc/Kconfig
  1957. +++ b/arch/sparc/Kconfig
  1958. @@ -189,12 +189,10 @@ config NR_CPUS
  1959. source kernel/Kconfig.hz
  1960. config RWSEM_GENERIC_SPINLOCK
  1961. - bool
  1962. - default y if SPARC32
  1963. + def_bool PREEMPT_RT_FULL
  1964. config RWSEM_XCHGADD_ALGORITHM
  1965. - bool
  1966. - default y if SPARC64
  1967. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  1968. config GENERIC_HWEIGHT
  1969. bool
  1970. diff --git a/arch/sparc/kernel/irq_64.c b/arch/sparc/kernel/irq_64.c
  1971. index bfbde8c4ffb2..62e7e06013c5 100644
  1972. --- a/arch/sparc/kernel/irq_64.c
  1973. +++ b/arch/sparc/kernel/irq_64.c
  1974. @@ -854,6 +854,7 @@ void __irq_entry handler_irq(int pil, struct pt_regs *regs)
  1975. set_irq_regs(old_regs);
  1976. }
  1977. +#ifndef CONFIG_PREEMPT_RT_FULL
  1978. void do_softirq_own_stack(void)
  1979. {
  1980. void *orig_sp, *sp = softirq_stack[smp_processor_id()];
  1981. @@ -868,6 +869,7 @@ void do_softirq_own_stack(void)
  1982. __asm__ __volatile__("mov %0, %%sp"
  1983. : : "r" (orig_sp));
  1984. }
  1985. +#endif
  1986. #ifdef CONFIG_HOTPLUG_CPU
  1987. void fixup_irqs(void)
  1988. diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
  1989. index 0ef2cdd11616..2eaae0f372fa 100644
  1990. --- a/arch/x86/Kconfig
  1991. +++ b/arch/x86/Kconfig
  1992. @@ -17,6 +17,7 @@ config X86_64
  1993. ### Arch settings
  1994. config X86
  1995. def_bool y
  1996. + select HAVE_PREEMPT_LAZY
  1997. select ACPI_LEGACY_TABLES_LOOKUP if ACPI
  1998. select ACPI_SYSTEM_POWER_STATES_SUPPORT if ACPI
  1999. select ANON_INODES
  2000. @@ -213,8 +214,11 @@ config ARCH_MAY_HAVE_PC_FDC
  2001. def_bool y
  2002. depends on ISA_DMA_API
  2003. +config RWSEM_GENERIC_SPINLOCK
  2004. + def_bool PREEMPT_RT_FULL
  2005. +
  2006. config RWSEM_XCHGADD_ALGORITHM
  2007. - def_bool y
  2008. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  2009. config GENERIC_CALIBRATE_DELAY
  2010. def_bool y
  2011. @@ -849,7 +853,7 @@ config IOMMU_HELPER
  2012. config MAXSMP
  2013. bool "Enable Maximum number of SMP Processors and NUMA Nodes"
  2014. depends on X86_64 && SMP && DEBUG_KERNEL
  2015. - select CPUMASK_OFFSTACK
  2016. + select CPUMASK_OFFSTACK if !PREEMPT_RT_FULL
  2017. ---help---
  2018. Enable maximum number of CPUS and NUMA Nodes for this architecture.
  2019. If unsure, say N.
  2020. diff --git a/arch/x86/crypto/aesni-intel_glue.c b/arch/x86/crypto/aesni-intel_glue.c
  2021. index 3633ad6145c5..c6d5458ee7f9 100644
  2022. --- a/arch/x86/crypto/aesni-intel_glue.c
  2023. +++ b/arch/x86/crypto/aesni-intel_glue.c
  2024. @@ -383,14 +383,14 @@ static int ecb_encrypt(struct blkcipher_desc *desc,
  2025. err = blkcipher_walk_virt(desc, &walk);
  2026. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2027. - kernel_fpu_begin();
  2028. while ((nbytes = walk.nbytes)) {
  2029. + kernel_fpu_begin();
  2030. aesni_ecb_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2031. - nbytes & AES_BLOCK_MASK);
  2032. + nbytes & AES_BLOCK_MASK);
  2033. + kernel_fpu_end();
  2034. nbytes &= AES_BLOCK_SIZE - 1;
  2035. err = blkcipher_walk_done(desc, &walk, nbytes);
  2036. }
  2037. - kernel_fpu_end();
  2038. return err;
  2039. }
  2040. @@ -407,14 +407,14 @@ static int ecb_decrypt(struct blkcipher_desc *desc,
  2041. err = blkcipher_walk_virt(desc, &walk);
  2042. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2043. - kernel_fpu_begin();
  2044. while ((nbytes = walk.nbytes)) {
  2045. + kernel_fpu_begin();
  2046. aesni_ecb_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2047. nbytes & AES_BLOCK_MASK);
  2048. + kernel_fpu_end();
  2049. nbytes &= AES_BLOCK_SIZE - 1;
  2050. err = blkcipher_walk_done(desc, &walk, nbytes);
  2051. }
  2052. - kernel_fpu_end();
  2053. return err;
  2054. }
  2055. @@ -431,14 +431,14 @@ static int cbc_encrypt(struct blkcipher_desc *desc,
  2056. err = blkcipher_walk_virt(desc, &walk);
  2057. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2058. - kernel_fpu_begin();
  2059. while ((nbytes = walk.nbytes)) {
  2060. + kernel_fpu_begin();
  2061. aesni_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2062. nbytes & AES_BLOCK_MASK, walk.iv);
  2063. + kernel_fpu_end();
  2064. nbytes &= AES_BLOCK_SIZE - 1;
  2065. err = blkcipher_walk_done(desc, &walk, nbytes);
  2066. }
  2067. - kernel_fpu_end();
  2068. return err;
  2069. }
  2070. @@ -455,14 +455,14 @@ static int cbc_decrypt(struct blkcipher_desc *desc,
  2071. err = blkcipher_walk_virt(desc, &walk);
  2072. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2073. - kernel_fpu_begin();
  2074. while ((nbytes = walk.nbytes)) {
  2075. + kernel_fpu_begin();
  2076. aesni_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2077. nbytes & AES_BLOCK_MASK, walk.iv);
  2078. + kernel_fpu_end();
  2079. nbytes &= AES_BLOCK_SIZE - 1;
  2080. err = blkcipher_walk_done(desc, &walk, nbytes);
  2081. }
  2082. - kernel_fpu_end();
  2083. return err;
  2084. }
  2085. @@ -514,18 +514,20 @@ static int ctr_crypt(struct blkcipher_desc *desc,
  2086. err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
  2087. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2088. - kernel_fpu_begin();
  2089. while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
  2090. + kernel_fpu_begin();
  2091. aesni_ctr_enc_tfm(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2092. nbytes & AES_BLOCK_MASK, walk.iv);
  2093. + kernel_fpu_end();
  2094. nbytes &= AES_BLOCK_SIZE - 1;
  2095. err = blkcipher_walk_done(desc, &walk, nbytes);
  2096. }
  2097. if (walk.nbytes) {
  2098. + kernel_fpu_begin();
  2099. ctr_crypt_final(ctx, &walk);
  2100. + kernel_fpu_end();
  2101. err = blkcipher_walk_done(desc, &walk, 0);
  2102. }
  2103. - kernel_fpu_end();
  2104. return err;
  2105. }
  2106. diff --git a/arch/x86/crypto/cast5_avx_glue.c b/arch/x86/crypto/cast5_avx_glue.c
  2107. index 8648158f3916..d7699130ee36 100644
  2108. --- a/arch/x86/crypto/cast5_avx_glue.c
  2109. +++ b/arch/x86/crypto/cast5_avx_glue.c
  2110. @@ -59,7 +59,7 @@ static inline void cast5_fpu_end(bool fpu_enabled)
  2111. static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  2112. bool enc)
  2113. {
  2114. - bool fpu_enabled = false;
  2115. + bool fpu_enabled;
  2116. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  2117. const unsigned int bsize = CAST5_BLOCK_SIZE;
  2118. unsigned int nbytes;
  2119. @@ -75,7 +75,7 @@ static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  2120. u8 *wsrc = walk->src.virt.addr;
  2121. u8 *wdst = walk->dst.virt.addr;
  2122. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  2123. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  2124. /* Process multi-block batch */
  2125. if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
  2126. @@ -103,10 +103,9 @@ static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  2127. } while (nbytes >= bsize);
  2128. done:
  2129. + cast5_fpu_end(fpu_enabled);
  2130. err = blkcipher_walk_done(desc, walk, nbytes);
  2131. }
  2132. -
  2133. - cast5_fpu_end(fpu_enabled);
  2134. return err;
  2135. }
  2136. @@ -227,7 +226,7 @@ static unsigned int __cbc_decrypt(struct blkcipher_desc *desc,
  2137. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  2138. struct scatterlist *src, unsigned int nbytes)
  2139. {
  2140. - bool fpu_enabled = false;
  2141. + bool fpu_enabled;
  2142. struct blkcipher_walk walk;
  2143. int err;
  2144. @@ -236,12 +235,11 @@ static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  2145. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2146. while ((nbytes = walk.nbytes)) {
  2147. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  2148. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  2149. nbytes = __cbc_decrypt(desc, &walk);
  2150. + cast5_fpu_end(fpu_enabled);
  2151. err = blkcipher_walk_done(desc, &walk, nbytes);
  2152. }
  2153. -
  2154. - cast5_fpu_end(fpu_enabled);
  2155. return err;
  2156. }
  2157. @@ -311,7 +309,7 @@ static unsigned int __ctr_crypt(struct blkcipher_desc *desc,
  2158. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  2159. struct scatterlist *src, unsigned int nbytes)
  2160. {
  2161. - bool fpu_enabled = false;
  2162. + bool fpu_enabled;
  2163. struct blkcipher_walk walk;
  2164. int err;
  2165. @@ -320,13 +318,12 @@ static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  2166. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2167. while ((nbytes = walk.nbytes) >= CAST5_BLOCK_SIZE) {
  2168. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  2169. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  2170. nbytes = __ctr_crypt(desc, &walk);
  2171. + cast5_fpu_end(fpu_enabled);
  2172. err = blkcipher_walk_done(desc, &walk, nbytes);
  2173. }
  2174. - cast5_fpu_end(fpu_enabled);
  2175. -
  2176. if (walk.nbytes) {
  2177. ctr_crypt_final(desc, &walk);
  2178. err = blkcipher_walk_done(desc, &walk, 0);
  2179. diff --git a/arch/x86/crypto/glue_helper.c b/arch/x86/crypto/glue_helper.c
  2180. index 6a85598931b5..3a506ce7ed93 100644
  2181. --- a/arch/x86/crypto/glue_helper.c
  2182. +++ b/arch/x86/crypto/glue_helper.c
  2183. @@ -39,7 +39,7 @@ static int __glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  2184. void *ctx = crypto_blkcipher_ctx(desc->tfm);
  2185. const unsigned int bsize = 128 / 8;
  2186. unsigned int nbytes, i, func_bytes;
  2187. - bool fpu_enabled = false;
  2188. + bool fpu_enabled;
  2189. int err;
  2190. err = blkcipher_walk_virt(desc, walk);
  2191. @@ -49,7 +49,7 @@ static int __glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  2192. u8 *wdst = walk->dst.virt.addr;
  2193. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2194. - desc, fpu_enabled, nbytes);
  2195. + desc, false, nbytes);
  2196. for (i = 0; i < gctx->num_funcs; i++) {
  2197. func_bytes = bsize * gctx->funcs[i].num_blocks;
  2198. @@ -71,10 +71,10 @@ static int __glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  2199. }
  2200. done:
  2201. + glue_fpu_end(fpu_enabled);
  2202. err = blkcipher_walk_done(desc, walk, nbytes);
  2203. }
  2204. - glue_fpu_end(fpu_enabled);
  2205. return err;
  2206. }
  2207. @@ -194,7 +194,7 @@ int glue_cbc_decrypt_128bit(const struct common_glue_ctx *gctx,
  2208. struct scatterlist *src, unsigned int nbytes)
  2209. {
  2210. const unsigned int bsize = 128 / 8;
  2211. - bool fpu_enabled = false;
  2212. + bool fpu_enabled;
  2213. struct blkcipher_walk walk;
  2214. int err;
  2215. @@ -203,12 +203,12 @@ int glue_cbc_decrypt_128bit(const struct common_glue_ctx *gctx,
  2216. while ((nbytes = walk.nbytes)) {
  2217. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2218. - desc, fpu_enabled, nbytes);
  2219. + desc, false, nbytes);
  2220. nbytes = __glue_cbc_decrypt_128bit(gctx, desc, &walk);
  2221. + glue_fpu_end(fpu_enabled);
  2222. err = blkcipher_walk_done(desc, &walk, nbytes);
  2223. }
  2224. - glue_fpu_end(fpu_enabled);
  2225. return err;
  2226. }
  2227. EXPORT_SYMBOL_GPL(glue_cbc_decrypt_128bit);
  2228. @@ -277,7 +277,7 @@ int glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
  2229. struct scatterlist *src, unsigned int nbytes)
  2230. {
  2231. const unsigned int bsize = 128 / 8;
  2232. - bool fpu_enabled = false;
  2233. + bool fpu_enabled;
  2234. struct blkcipher_walk walk;
  2235. int err;
  2236. @@ -286,13 +286,12 @@ int glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
  2237. while ((nbytes = walk.nbytes) >= bsize) {
  2238. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2239. - desc, fpu_enabled, nbytes);
  2240. + desc, false, nbytes);
  2241. nbytes = __glue_ctr_crypt_128bit(gctx, desc, &walk);
  2242. + glue_fpu_end(fpu_enabled);
  2243. err = blkcipher_walk_done(desc, &walk, nbytes);
  2244. }
  2245. - glue_fpu_end(fpu_enabled);
  2246. -
  2247. if (walk.nbytes) {
  2248. glue_ctr_crypt_final_128bit(
  2249. gctx->funcs[gctx->num_funcs - 1].fn_u.ctr, desc, &walk);
  2250. @@ -347,7 +346,7 @@ int glue_xts_crypt_128bit(const struct common_glue_ctx *gctx,
  2251. void *tweak_ctx, void *crypt_ctx)
  2252. {
  2253. const unsigned int bsize = 128 / 8;
  2254. - bool fpu_enabled = false;
  2255. + bool fpu_enabled;
  2256. struct blkcipher_walk walk;
  2257. int err;
  2258. @@ -360,21 +359,21 @@ int glue_xts_crypt_128bit(const struct common_glue_ctx *gctx,
  2259. /* set minimum length to bsize, for tweak_fn */
  2260. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2261. - desc, fpu_enabled,
  2262. + desc, false,
  2263. nbytes < bsize ? bsize : nbytes);
  2264. -
  2265. /* calculate first value of T */
  2266. tweak_fn(tweak_ctx, walk.iv, walk.iv);
  2267. + glue_fpu_end(fpu_enabled);
  2268. while (nbytes) {
  2269. + fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2270. + desc, false, nbytes);
  2271. nbytes = __glue_xts_crypt_128bit(gctx, crypt_ctx, desc, &walk);
  2272. + glue_fpu_end(fpu_enabled);
  2273. err = blkcipher_walk_done(desc, &walk, nbytes);
  2274. nbytes = walk.nbytes;
  2275. }
  2276. -
  2277. - glue_fpu_end(fpu_enabled);
  2278. -
  2279. return err;
  2280. }
  2281. EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit);
  2282. diff --git a/arch/x86/entry/common.c b/arch/x86/entry/common.c
  2283. index 1a4477cedc49..75a301b6a5b6 100644
  2284. --- a/arch/x86/entry/common.c
  2285. +++ b/arch/x86/entry/common.c
  2286. @@ -220,7 +220,7 @@ long syscall_trace_enter(struct pt_regs *regs)
  2287. #define EXIT_TO_USERMODE_LOOP_FLAGS \
  2288. (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  2289. - _TIF_NEED_RESCHED | _TIF_USER_RETURN_NOTIFY)
  2290. + _TIF_NEED_RESCHED_MASK | _TIF_USER_RETURN_NOTIFY)
  2291. static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
  2292. {
  2293. @@ -236,9 +236,16 @@ static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
  2294. /* We have work to do. */
  2295. local_irq_enable();
  2296. - if (cached_flags & _TIF_NEED_RESCHED)
  2297. + if (cached_flags & _TIF_NEED_RESCHED_MASK)
  2298. schedule();
  2299. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  2300. + if (unlikely(current->forced_info.si_signo)) {
  2301. + struct task_struct *t = current;
  2302. + force_sig_info(t->forced_info.si_signo, &t->forced_info, t);
  2303. + t->forced_info.si_signo = 0;
  2304. + }
  2305. +#endif
  2306. if (cached_flags & _TIF_UPROBE)
  2307. uprobe_notify_resume(regs);
  2308. diff --git a/arch/x86/entry/entry_32.S b/arch/x86/entry/entry_32.S
  2309. index ae678ad128a9..3bcef8bdb911 100644
  2310. --- a/arch/x86/entry/entry_32.S
  2311. +++ b/arch/x86/entry/entry_32.S
  2312. @@ -278,8 +278,24 @@ END(ret_from_exception)
  2313. ENTRY(resume_kernel)
  2314. DISABLE_INTERRUPTS(CLBR_ANY)
  2315. need_resched:
  2316. + # preempt count == 0 + NEED_RS set?
  2317. cmpl $0, PER_CPU_VAR(__preempt_count)
  2318. +#ifndef CONFIG_PREEMPT_LAZY
  2319. jnz restore_all
  2320. +#else
  2321. + jz test_int_off
  2322. +
  2323. + # atleast preempt count == 0 ?
  2324. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  2325. + jne restore_all
  2326. +
  2327. + cmpl $0,TI_preempt_lazy_count(%ebp) # non-zero preempt_lazy_count ?
  2328. + jnz restore_all
  2329. +
  2330. + testl $_TIF_NEED_RESCHED_LAZY, TI_flags(%ebp)
  2331. + jz restore_all
  2332. +test_int_off:
  2333. +#endif
  2334. testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
  2335. jz restore_all
  2336. call preempt_schedule_irq
  2337. diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
  2338. index 952b23b5d4e9..459a68cffcc2 100644
  2339. --- a/arch/x86/entry/entry_64.S
  2340. +++ b/arch/x86/entry/entry_64.S
  2341. @@ -607,7 +607,23 @@ GLOBAL(retint_user)
  2342. bt $9, EFLAGS(%rsp) /* were interrupts off? */
  2343. jnc 1f
  2344. 0: cmpl $0, PER_CPU_VAR(__preempt_count)
  2345. +#ifndef CONFIG_PREEMPT_LAZY
  2346. jnz 1f
  2347. +#else
  2348. + jz do_preempt_schedule_irq
  2349. +
  2350. + # atleast preempt count == 0 ?
  2351. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  2352. + jnz 1f
  2353. +
  2354. + GET_THREAD_INFO(%rcx)
  2355. + cmpl $0, TI_preempt_lazy_count(%rcx)
  2356. + jnz 1f
  2357. +
  2358. + bt $TIF_NEED_RESCHED_LAZY,TI_flags(%rcx)
  2359. + jnc 1f
  2360. +do_preempt_schedule_irq:
  2361. +#endif
  2362. call preempt_schedule_irq
  2363. jmp 0b
  2364. 1:
  2365. @@ -897,6 +913,7 @@ END(native_load_gs_index)
  2366. jmp 2b
  2367. .previous
  2368. +#ifndef CONFIG_PREEMPT_RT_FULL
  2369. /* Call softirq on interrupt stack. Interrupts are off. */
  2370. ENTRY(do_softirq_own_stack)
  2371. pushq %rbp
  2372. @@ -909,6 +926,7 @@ ENTRY(do_softirq_own_stack)
  2373. decl PER_CPU_VAR(irq_count)
  2374. ret
  2375. END(do_softirq_own_stack)
  2376. +#endif
  2377. #ifdef CONFIG_XEN
  2378. idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
  2379. diff --git a/arch/x86/include/asm/preempt.h b/arch/x86/include/asm/preempt.h
  2380. index 01bcde84d3e4..6f432adc55cd 100644
  2381. --- a/arch/x86/include/asm/preempt.h
  2382. +++ b/arch/x86/include/asm/preempt.h
  2383. @@ -79,17 +79,46 @@ static __always_inline void __preempt_count_sub(int val)
  2384. * a decrement which hits zero means we have no preempt_count and should
  2385. * reschedule.
  2386. */
  2387. -static __always_inline bool __preempt_count_dec_and_test(void)
  2388. +static __always_inline bool ____preempt_count_dec_and_test(void)
  2389. {
  2390. GEN_UNARY_RMWcc("decl", __preempt_count, __percpu_arg(0), "e");
  2391. }
  2392. +static __always_inline bool __preempt_count_dec_and_test(void)
  2393. +{
  2394. + if (____preempt_count_dec_and_test())
  2395. + return true;
  2396. +#ifdef CONFIG_PREEMPT_LAZY
  2397. + if (current_thread_info()->preempt_lazy_count)
  2398. + return false;
  2399. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  2400. +#else
  2401. + return false;
  2402. +#endif
  2403. +}
  2404. +
  2405. /*
  2406. * Returns true when we need to resched and can (barring IRQ state).
  2407. */
  2408. static __always_inline bool should_resched(int preempt_offset)
  2409. {
  2410. +#ifdef CONFIG_PREEMPT_LAZY
  2411. + u32 tmp;
  2412. +
  2413. + tmp = raw_cpu_read_4(__preempt_count);
  2414. + if (tmp == preempt_offset)
  2415. + return true;
  2416. +
  2417. + /* preempt count == 0 ? */
  2418. + tmp &= ~PREEMPT_NEED_RESCHED;
  2419. + if (tmp)
  2420. + return false;
  2421. + if (current_thread_info()->preempt_lazy_count)
  2422. + return false;
  2423. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  2424. +#else
  2425. return unlikely(raw_cpu_read_4(__preempt_count) == preempt_offset);
  2426. +#endif
  2427. }
  2428. #ifdef CONFIG_PREEMPT
  2429. diff --git a/arch/x86/include/asm/signal.h b/arch/x86/include/asm/signal.h
  2430. index 2138c9ae19ee..3f5b4ee2e2c1 100644
  2431. --- a/arch/x86/include/asm/signal.h
  2432. +++ b/arch/x86/include/asm/signal.h
  2433. @@ -23,6 +23,19 @@ typedef struct {
  2434. unsigned long sig[_NSIG_WORDS];
  2435. } sigset_t;
  2436. +/*
  2437. + * Because some traps use the IST stack, we must keep preemption
  2438. + * disabled while calling do_trap(), but do_trap() may call
  2439. + * force_sig_info() which will grab the signal spin_locks for the
  2440. + * task, which in PREEMPT_RT_FULL are mutexes. By defining
  2441. + * ARCH_RT_DELAYS_SIGNAL_SEND the force_sig_info() will set
  2442. + * TIF_NOTIFY_RESUME and set up the signal to be sent on exit of the
  2443. + * trap.
  2444. + */
  2445. +#if defined(CONFIG_PREEMPT_RT_FULL)
  2446. +#define ARCH_RT_DELAYS_SIGNAL_SEND
  2447. +#endif
  2448. +
  2449. #ifndef CONFIG_COMPAT
  2450. typedef sigset_t compat_sigset_t;
  2451. #endif
  2452. diff --git a/arch/x86/include/asm/stackprotector.h b/arch/x86/include/asm/stackprotector.h
  2453. index 58505f01962f..02fa39652cd6 100644
  2454. --- a/arch/x86/include/asm/stackprotector.h
  2455. +++ b/arch/x86/include/asm/stackprotector.h
  2456. @@ -59,7 +59,7 @@
  2457. */
  2458. static __always_inline void boot_init_stack_canary(void)
  2459. {
  2460. - u64 canary;
  2461. + u64 uninitialized_var(canary);
  2462. u64 tsc;
  2463. #ifdef CONFIG_X86_64
  2464. @@ -70,8 +70,15 @@ static __always_inline void boot_init_stack_canary(void)
  2465. * of randomness. The TSC only matters for very early init,
  2466. * there it already has some randomness on most systems. Later
  2467. * on during the bootup the random pool has true entropy too.
  2468. + *
  2469. + * For preempt-rt we need to weaken the randomness a bit, as
  2470. + * we can't call into the random generator from atomic context
  2471. + * due to locking constraints. We just leave canary
  2472. + * uninitialized and use the TSC based randomness on top of it.
  2473. */
  2474. +#ifndef CONFIG_PREEMPT_RT_FULL
  2475. get_random_bytes(&canary, sizeof(canary));
  2476. +#endif
  2477. tsc = rdtsc();
  2478. canary += tsc + (tsc << 32UL);
  2479. diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thread_info.h
  2480. index c7b551028740..ddb63bd90e3c 100644
  2481. --- a/arch/x86/include/asm/thread_info.h
  2482. +++ b/arch/x86/include/asm/thread_info.h
  2483. @@ -58,6 +58,8 @@ struct thread_info {
  2484. __u32 status; /* thread synchronous flags */
  2485. __u32 cpu; /* current CPU */
  2486. mm_segment_t addr_limit;
  2487. + int preempt_lazy_count; /* 0 => lazy preemptable
  2488. + <0 => BUG */
  2489. unsigned int sig_on_uaccess_error:1;
  2490. unsigned int uaccess_err:1; /* uaccess failed */
  2491. };
  2492. @@ -95,6 +97,7 @@ struct thread_info {
  2493. #define TIF_SYSCALL_EMU 6 /* syscall emulation active */
  2494. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  2495. #define TIF_SECCOMP 8 /* secure computing */
  2496. +#define TIF_NEED_RESCHED_LAZY 9 /* lazy rescheduling necessary */
  2497. #define TIF_USER_RETURN_NOTIFY 11 /* notify kernel of userspace return */
  2498. #define TIF_UPROBE 12 /* breakpointed or singlestepping */
  2499. #define TIF_NOTSC 16 /* TSC is not accessible in userland */
  2500. @@ -119,6 +122,7 @@ struct thread_info {
  2501. #define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU)
  2502. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  2503. #define _TIF_SECCOMP (1 << TIF_SECCOMP)
  2504. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  2505. #define _TIF_USER_RETURN_NOTIFY (1 << TIF_USER_RETURN_NOTIFY)
  2506. #define _TIF_UPROBE (1 << TIF_UPROBE)
  2507. #define _TIF_NOTSC (1 << TIF_NOTSC)
  2508. @@ -152,6 +156,8 @@ struct thread_info {
  2509. #define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW|_TIF_USER_RETURN_NOTIFY)
  2510. #define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)
  2511. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  2512. +
  2513. #define STACK_WARN (THREAD_SIZE/8)
  2514. /*
  2515. diff --git a/arch/x86/include/asm/uv/uv_bau.h b/arch/x86/include/asm/uv/uv_bau.h
  2516. index fc808b83fccb..ebb40118abf5 100644
  2517. --- a/arch/x86/include/asm/uv/uv_bau.h
  2518. +++ b/arch/x86/include/asm/uv/uv_bau.h
  2519. @@ -615,9 +615,9 @@ struct bau_control {
  2520. cycles_t send_message;
  2521. cycles_t period_end;
  2522. cycles_t period_time;
  2523. - spinlock_t uvhub_lock;
  2524. - spinlock_t queue_lock;
  2525. - spinlock_t disable_lock;
  2526. + raw_spinlock_t uvhub_lock;
  2527. + raw_spinlock_t queue_lock;
  2528. + raw_spinlock_t disable_lock;
  2529. /* tunables */
  2530. int max_concurr;
  2531. int max_concurr_const;
  2532. @@ -776,15 +776,15 @@ static inline int atom_asr(short i, struct atomic_short *v)
  2533. * to be lowered below the current 'v'. atomic_add_unless can only stop
  2534. * on equal.
  2535. */
  2536. -static inline int atomic_inc_unless_ge(spinlock_t *lock, atomic_t *v, int u)
  2537. +static inline int atomic_inc_unless_ge(raw_spinlock_t *lock, atomic_t *v, int u)
  2538. {
  2539. - spin_lock(lock);
  2540. + raw_spin_lock(lock);
  2541. if (atomic_read(v) >= u) {
  2542. - spin_unlock(lock);
  2543. + raw_spin_unlock(lock);
  2544. return 0;
  2545. }
  2546. atomic_inc(v);
  2547. - spin_unlock(lock);
  2548. + raw_spin_unlock(lock);
  2549. return 1;
  2550. }
  2551. diff --git a/arch/x86/include/asm/uv/uv_hub.h b/arch/x86/include/asm/uv/uv_hub.h
  2552. index ea7074784cc4..01ec643ce66e 100644
  2553. --- a/arch/x86/include/asm/uv/uv_hub.h
  2554. +++ b/arch/x86/include/asm/uv/uv_hub.h
  2555. @@ -492,7 +492,7 @@ struct uv_blade_info {
  2556. unsigned short nr_online_cpus;
  2557. unsigned short pnode;
  2558. short memory_nid;
  2559. - spinlock_t nmi_lock; /* obsolete, see uv_hub_nmi */
  2560. + raw_spinlock_t nmi_lock; /* obsolete, see uv_hub_nmi */
  2561. unsigned long nmi_count; /* obsolete, see uv_hub_nmi */
  2562. };
  2563. extern struct uv_blade_info *uv_blade_info;
  2564. diff --git a/arch/x86/kernel/acpi/boot.c b/arch/x86/kernel/acpi/boot.c
  2565. index a1e4a6c3f394..86adbf86f366 100644
  2566. --- a/arch/x86/kernel/acpi/boot.c
  2567. +++ b/arch/x86/kernel/acpi/boot.c
  2568. @@ -87,7 +87,9 @@ static u64 acpi_lapic_addr __initdata = APIC_DEFAULT_PHYS_BASE;
  2569. * ->ioapic_mutex
  2570. * ->ioapic_lock
  2571. */
  2572. +#ifdef CONFIG_X86_IO_APIC
  2573. static DEFINE_MUTEX(acpi_ioapic_lock);
  2574. +#endif
  2575. /* --------------------------------------------------------------------------
  2576. Boot-time Configuration
  2577. diff --git a/arch/x86/kernel/apic/io_apic.c b/arch/x86/kernel/apic/io_apic.c
  2578. index fc91c98bee01..29c4f1b078a1 100644
  2579. --- a/arch/x86/kernel/apic/io_apic.c
  2580. +++ b/arch/x86/kernel/apic/io_apic.c
  2581. @@ -1711,7 +1711,8 @@ static bool io_apic_level_ack_pending(struct mp_chip_data *data)
  2582. static inline bool ioapic_irqd_mask(struct irq_data *data)
  2583. {
  2584. /* If we are moving the irq we need to mask it */
  2585. - if (unlikely(irqd_is_setaffinity_pending(data))) {
  2586. + if (unlikely(irqd_is_setaffinity_pending(data) &&
  2587. + !irqd_irq_inprogress(data))) {
  2588. mask_ioapic_irq(data);
  2589. return true;
  2590. }
  2591. diff --git a/arch/x86/kernel/apic/x2apic_uv_x.c b/arch/x86/kernel/apic/x2apic_uv_x.c
  2592. index 4a139465f1d4..ad2afff02b36 100644
  2593. --- a/arch/x86/kernel/apic/x2apic_uv_x.c
  2594. +++ b/arch/x86/kernel/apic/x2apic_uv_x.c
  2595. @@ -947,7 +947,7 @@ void __init uv_system_init(void)
  2596. uv_blade_info[blade].pnode = pnode;
  2597. uv_blade_info[blade].nr_possible_cpus = 0;
  2598. uv_blade_info[blade].nr_online_cpus = 0;
  2599. - spin_lock_init(&uv_blade_info[blade].nmi_lock);
  2600. + raw_spin_lock_init(&uv_blade_info[blade].nmi_lock);
  2601. min_pnode = min(pnode, min_pnode);
  2602. max_pnode = max(pnode, max_pnode);
  2603. blade++;
  2604. diff --git a/arch/x86/kernel/asm-offsets.c b/arch/x86/kernel/asm-offsets.c
  2605. index 439df975bc7a..b7954ddd6a0a 100644
  2606. --- a/arch/x86/kernel/asm-offsets.c
  2607. +++ b/arch/x86/kernel/asm-offsets.c
  2608. @@ -32,6 +32,7 @@ void common(void) {
  2609. OFFSET(TI_flags, thread_info, flags);
  2610. OFFSET(TI_status, thread_info, status);
  2611. OFFSET(TI_addr_limit, thread_info, addr_limit);
  2612. + OFFSET(TI_preempt_lazy_count, thread_info, preempt_lazy_count);
  2613. BLANK();
  2614. OFFSET(crypto_tfm_ctx_offset, crypto_tfm, __crt_ctx);
  2615. @@ -89,4 +90,5 @@ void common(void) {
  2616. BLANK();
  2617. DEFINE(PTREGS_SIZE, sizeof(struct pt_regs));
  2618. + DEFINE(_PREEMPT_ENABLED, PREEMPT_ENABLED);
  2619. }
  2620. diff --git a/arch/x86/kernel/cpu/mcheck/mce.c b/arch/x86/kernel/cpu/mcheck/mce.c
  2621. index 7e8a736d09db..430a4ec07811 100644
  2622. --- a/arch/x86/kernel/cpu/mcheck/mce.c
  2623. +++ b/arch/x86/kernel/cpu/mcheck/mce.c
  2624. @@ -41,6 +41,8 @@
  2625. #include <linux/debugfs.h>
  2626. #include <linux/irq_work.h>
  2627. #include <linux/export.h>
  2628. +#include <linux/jiffies.h>
  2629. +#include <linux/swork.h>
  2630. #include <asm/processor.h>
  2631. #include <asm/traps.h>
  2632. @@ -1236,7 +1238,7 @@ void mce_log_therm_throt_event(__u64 status)
  2633. static unsigned long check_interval = INITIAL_CHECK_INTERVAL;
  2634. static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
  2635. -static DEFINE_PER_CPU(struct timer_list, mce_timer);
  2636. +static DEFINE_PER_CPU(struct hrtimer, mce_timer);
  2637. static unsigned long mce_adjust_timer_default(unsigned long interval)
  2638. {
  2639. @@ -1245,32 +1247,18 @@ static unsigned long mce_adjust_timer_default(unsigned long interval)
  2640. static unsigned long (*mce_adjust_timer)(unsigned long interval) = mce_adjust_timer_default;
  2641. -static void __restart_timer(struct timer_list *t, unsigned long interval)
  2642. +static enum hrtimer_restart __restart_timer(struct hrtimer *timer, unsigned long interval)
  2643. {
  2644. - unsigned long when = jiffies + interval;
  2645. - unsigned long flags;
  2646. -
  2647. - local_irq_save(flags);
  2648. -
  2649. - if (timer_pending(t)) {
  2650. - if (time_before(when, t->expires))
  2651. - mod_timer_pinned(t, when);
  2652. - } else {
  2653. - t->expires = round_jiffies(when);
  2654. - add_timer_on(t, smp_processor_id());
  2655. - }
  2656. -
  2657. - local_irq_restore(flags);
  2658. + if (!interval)
  2659. + return HRTIMER_NORESTART;
  2660. + hrtimer_forward_now(timer, ns_to_ktime(jiffies_to_nsecs(interval)));
  2661. + return HRTIMER_RESTART;
  2662. }
  2663. -static void mce_timer_fn(unsigned long data)
  2664. +static enum hrtimer_restart mce_timer_fn(struct hrtimer *timer)
  2665. {
  2666. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  2667. - int cpu = smp_processor_id();
  2668. unsigned long iv;
  2669. - WARN_ON(cpu != data);
  2670. -
  2671. iv = __this_cpu_read(mce_next_interval);
  2672. if (mce_available(this_cpu_ptr(&cpu_info))) {
  2673. @@ -1293,7 +1281,7 @@ static void mce_timer_fn(unsigned long data)
  2674. done:
  2675. __this_cpu_write(mce_next_interval, iv);
  2676. - __restart_timer(t, iv);
  2677. + return __restart_timer(timer, iv);
  2678. }
  2679. /*
  2680. @@ -1301,7 +1289,7 @@ static void mce_timer_fn(unsigned long data)
  2681. */
  2682. void mce_timer_kick(unsigned long interval)
  2683. {
  2684. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  2685. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  2686. unsigned long iv = __this_cpu_read(mce_next_interval);
  2687. __restart_timer(t, interval);
  2688. @@ -1316,7 +1304,7 @@ static void mce_timer_delete_all(void)
  2689. int cpu;
  2690. for_each_online_cpu(cpu)
  2691. - del_timer_sync(&per_cpu(mce_timer, cpu));
  2692. + hrtimer_cancel(&per_cpu(mce_timer, cpu));
  2693. }
  2694. static void mce_do_trigger(struct work_struct *work)
  2695. @@ -1326,6 +1314,56 @@ static void mce_do_trigger(struct work_struct *work)
  2696. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  2697. +static void __mce_notify_work(struct swork_event *event)
  2698. +{
  2699. + /* Not more than two messages every minute */
  2700. + static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  2701. +
  2702. + /* wake processes polling /dev/mcelog */
  2703. + wake_up_interruptible(&mce_chrdev_wait);
  2704. +
  2705. + /*
  2706. + * There is no risk of missing notifications because
  2707. + * work_pending is always cleared before the function is
  2708. + * executed.
  2709. + */
  2710. + if (mce_helper[0] && !work_pending(&mce_trigger_work))
  2711. + schedule_work(&mce_trigger_work);
  2712. +
  2713. + if (__ratelimit(&ratelimit))
  2714. + pr_info(HW_ERR "Machine check events logged\n");
  2715. +}
  2716. +
  2717. +#ifdef CONFIG_PREEMPT_RT_FULL
  2718. +static bool notify_work_ready __read_mostly;
  2719. +static struct swork_event notify_work;
  2720. +
  2721. +static int mce_notify_work_init(void)
  2722. +{
  2723. + int err;
  2724. +
  2725. + err = swork_get();
  2726. + if (err)
  2727. + return err;
  2728. +
  2729. + INIT_SWORK(&notify_work, __mce_notify_work);
  2730. + notify_work_ready = true;
  2731. + return 0;
  2732. +}
  2733. +
  2734. +static void mce_notify_work(void)
  2735. +{
  2736. + if (notify_work_ready)
  2737. + swork_queue(&notify_work);
  2738. +}
  2739. +#else
  2740. +static void mce_notify_work(void)
  2741. +{
  2742. + __mce_notify_work(NULL);
  2743. +}
  2744. +static inline int mce_notify_work_init(void) { return 0; }
  2745. +#endif
  2746. +
  2747. /*
  2748. * Notify the user(s) about new machine check events.
  2749. * Can be called from interrupt context, but not from machine check/NMI
  2750. @@ -1333,19 +1371,8 @@ static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  2751. */
  2752. int mce_notify_irq(void)
  2753. {
  2754. - /* Not more than two messages every minute */
  2755. - static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  2756. -
  2757. if (test_and_clear_bit(0, &mce_need_notify)) {
  2758. - /* wake processes polling /dev/mcelog */
  2759. - wake_up_interruptible(&mce_chrdev_wait);
  2760. -
  2761. - if (mce_helper[0])
  2762. - schedule_work(&mce_trigger_work);
  2763. -
  2764. - if (__ratelimit(&ratelimit))
  2765. - pr_info(HW_ERR "Machine check events logged\n");
  2766. -
  2767. + mce_notify_work();
  2768. return 1;
  2769. }
  2770. return 0;
  2771. @@ -1639,7 +1666,7 @@ static void __mcheck_cpu_clear_vendor(struct cpuinfo_x86 *c)
  2772. }
  2773. }
  2774. -static void mce_start_timer(unsigned int cpu, struct timer_list *t)
  2775. +static void mce_start_timer(unsigned int cpu, struct hrtimer *t)
  2776. {
  2777. unsigned long iv = check_interval * HZ;
  2778. @@ -1648,16 +1675,17 @@ static void mce_start_timer(unsigned int cpu, struct timer_list *t)
  2779. per_cpu(mce_next_interval, cpu) = iv;
  2780. - t->expires = round_jiffies(jiffies + iv);
  2781. - add_timer_on(t, cpu);
  2782. + hrtimer_start_range_ns(t, ns_to_ktime(jiffies_to_usecs(iv) * 1000ULL),
  2783. + 0, HRTIMER_MODE_REL_PINNED);
  2784. }
  2785. static void __mcheck_cpu_init_timer(void)
  2786. {
  2787. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  2788. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  2789. unsigned int cpu = smp_processor_id();
  2790. - setup_timer(t, mce_timer_fn, cpu);
  2791. + hrtimer_init(t, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2792. + t->function = mce_timer_fn;
  2793. mce_start_timer(cpu, t);
  2794. }
  2795. @@ -2376,6 +2404,8 @@ static void mce_disable_cpu(void *h)
  2796. if (!mce_available(raw_cpu_ptr(&cpu_info)))
  2797. return;
  2798. + hrtimer_cancel(this_cpu_ptr(&mce_timer));
  2799. +
  2800. if (!(action & CPU_TASKS_FROZEN))
  2801. cmci_clear();
  2802. @@ -2398,6 +2428,7 @@ static void mce_reenable_cpu(void *h)
  2803. if (b->init)
  2804. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  2805. }
  2806. + __mcheck_cpu_init_timer();
  2807. }
  2808. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  2809. @@ -2405,7 +2436,6 @@ static int
  2810. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  2811. {
  2812. unsigned int cpu = (unsigned long)hcpu;
  2813. - struct timer_list *t = &per_cpu(mce_timer, cpu);
  2814. switch (action & ~CPU_TASKS_FROZEN) {
  2815. case CPU_ONLINE:
  2816. @@ -2425,11 +2455,9 @@ mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  2817. break;
  2818. case CPU_DOWN_PREPARE:
  2819. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  2820. - del_timer_sync(t);
  2821. break;
  2822. case CPU_DOWN_FAILED:
  2823. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  2824. - mce_start_timer(cpu, t);
  2825. break;
  2826. }
  2827. @@ -2468,6 +2496,10 @@ static __init int mcheck_init_device(void)
  2828. goto err_out;
  2829. }
  2830. + err = mce_notify_work_init();
  2831. + if (err)
  2832. + goto err_out;
  2833. +
  2834. if (!zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL)) {
  2835. err = -ENOMEM;
  2836. goto err_out;
  2837. diff --git a/arch/x86/kernel/cpu/perf_event_intel_rapl.c b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
  2838. index ed446bdcbf31..d2ac364e2118 100644
  2839. --- a/arch/x86/kernel/cpu/perf_event_intel_rapl.c
  2840. +++ b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
  2841. @@ -117,7 +117,7 @@ static struct perf_pmu_events_attr event_attr_##v = { \
  2842. };
  2843. struct rapl_pmu {
  2844. - spinlock_t lock;
  2845. + raw_spinlock_t lock;
  2846. int n_active; /* number of active events */
  2847. struct list_head active_list;
  2848. struct pmu *pmu; /* pointer to rapl_pmu_class */
  2849. @@ -220,13 +220,13 @@ static enum hrtimer_restart rapl_hrtimer_handle(struct hrtimer *hrtimer)
  2850. if (!pmu->n_active)
  2851. return HRTIMER_NORESTART;
  2852. - spin_lock_irqsave(&pmu->lock, flags);
  2853. + raw_spin_lock_irqsave(&pmu->lock, flags);
  2854. list_for_each_entry(event, &pmu->active_list, active_entry) {
  2855. rapl_event_update(event);
  2856. }
  2857. - spin_unlock_irqrestore(&pmu->lock, flags);
  2858. + raw_spin_unlock_irqrestore(&pmu->lock, flags);
  2859. hrtimer_forward_now(hrtimer, pmu->timer_interval);
  2860. @@ -263,9 +263,9 @@ static void rapl_pmu_event_start(struct perf_event *event, int mode)
  2861. struct rapl_pmu *pmu = __this_cpu_read(rapl_pmu);
  2862. unsigned long flags;
  2863. - spin_lock_irqsave(&pmu->lock, flags);
  2864. + raw_spin_lock_irqsave(&pmu->lock, flags);
  2865. __rapl_pmu_event_start(pmu, event);
  2866. - spin_unlock_irqrestore(&pmu->lock, flags);
  2867. + raw_spin_unlock_irqrestore(&pmu->lock, flags);
  2868. }
  2869. static void rapl_pmu_event_stop(struct perf_event *event, int mode)
  2870. @@ -274,7 +274,7 @@ static void rapl_pmu_event_stop(struct perf_event *event, int mode)
  2871. struct hw_perf_event *hwc = &event->hw;
  2872. unsigned long flags;
  2873. - spin_lock_irqsave(&pmu->lock, flags);
  2874. + raw_spin_lock_irqsave(&pmu->lock, flags);
  2875. /* mark event as deactivated and stopped */
  2876. if (!(hwc->state & PERF_HES_STOPPED)) {
  2877. @@ -299,7 +299,7 @@ static void rapl_pmu_event_stop(struct perf_event *event, int mode)
  2878. hwc->state |= PERF_HES_UPTODATE;
  2879. }
  2880. - spin_unlock_irqrestore(&pmu->lock, flags);
  2881. + raw_spin_unlock_irqrestore(&pmu->lock, flags);
  2882. }
  2883. static int rapl_pmu_event_add(struct perf_event *event, int mode)
  2884. @@ -308,14 +308,14 @@ static int rapl_pmu_event_add(struct perf_event *event, int mode)
  2885. struct hw_perf_event *hwc = &event->hw;
  2886. unsigned long flags;
  2887. - spin_lock_irqsave(&pmu->lock, flags);
  2888. + raw_spin_lock_irqsave(&pmu->lock, flags);
  2889. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  2890. if (mode & PERF_EF_START)
  2891. __rapl_pmu_event_start(pmu, event);
  2892. - spin_unlock_irqrestore(&pmu->lock, flags);
  2893. + raw_spin_unlock_irqrestore(&pmu->lock, flags);
  2894. return 0;
  2895. }
  2896. @@ -603,7 +603,7 @@ static int rapl_cpu_prepare(int cpu)
  2897. pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu));
  2898. if (!pmu)
  2899. return -1;
  2900. - spin_lock_init(&pmu->lock);
  2901. + raw_spin_lock_init(&pmu->lock);
  2902. INIT_LIST_HEAD(&pmu->active_list);
  2903. diff --git a/arch/x86/kernel/dumpstack_32.c b/arch/x86/kernel/dumpstack_32.c
  2904. index 464ffd69b92e..00db1aad1548 100644
  2905. --- a/arch/x86/kernel/dumpstack_32.c
  2906. +++ b/arch/x86/kernel/dumpstack_32.c
  2907. @@ -42,7 +42,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
  2908. unsigned long *stack, unsigned long bp,
  2909. const struct stacktrace_ops *ops, void *data)
  2910. {
  2911. - const unsigned cpu = get_cpu();
  2912. + const unsigned cpu = get_cpu_light();
  2913. int graph = 0;
  2914. u32 *prev_esp;
  2915. @@ -86,7 +86,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
  2916. break;
  2917. touch_nmi_watchdog();
  2918. }
  2919. - put_cpu();
  2920. + put_cpu_light();
  2921. }
  2922. EXPORT_SYMBOL(dump_trace);
  2923. diff --git a/arch/x86/kernel/dumpstack_64.c b/arch/x86/kernel/dumpstack_64.c
  2924. index 5f1c6266eb30..c331e3fef465 100644
  2925. --- a/arch/x86/kernel/dumpstack_64.c
  2926. +++ b/arch/x86/kernel/dumpstack_64.c
  2927. @@ -152,7 +152,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
  2928. unsigned long *stack, unsigned long bp,
  2929. const struct stacktrace_ops *ops, void *data)
  2930. {
  2931. - const unsigned cpu = get_cpu();
  2932. + const unsigned cpu = get_cpu_light();
  2933. struct thread_info *tinfo;
  2934. unsigned long *irq_stack = (unsigned long *)per_cpu(irq_stack_ptr, cpu);
  2935. unsigned long dummy;
  2936. @@ -241,7 +241,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
  2937. * This handles the process stack:
  2938. */
  2939. bp = ops->walk_stack(tinfo, stack, bp, ops, data, NULL, &graph);
  2940. - put_cpu();
  2941. + put_cpu_light();
  2942. }
  2943. EXPORT_SYMBOL(dump_trace);
  2944. @@ -255,7 +255,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  2945. int cpu;
  2946. int i;
  2947. - preempt_disable();
  2948. + migrate_disable();
  2949. cpu = smp_processor_id();
  2950. irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
  2951. @@ -291,7 +291,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  2952. pr_cont(" %016lx", *stack++);
  2953. touch_nmi_watchdog();
  2954. }
  2955. - preempt_enable();
  2956. + migrate_enable();
  2957. pr_cont("\n");
  2958. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  2959. diff --git a/arch/x86/kernel/irq_32.c b/arch/x86/kernel/irq_32.c
  2960. index 38da8f29a9c8..ce71f7098f15 100644
  2961. --- a/arch/x86/kernel/irq_32.c
  2962. +++ b/arch/x86/kernel/irq_32.c
  2963. @@ -128,6 +128,7 @@ void irq_ctx_init(int cpu)
  2964. cpu, per_cpu(hardirq_stack, cpu), per_cpu(softirq_stack, cpu));
  2965. }
  2966. +#ifndef CONFIG_PREEMPT_RT_FULL
  2967. void do_softirq_own_stack(void)
  2968. {
  2969. struct thread_info *curstk;
  2970. @@ -146,6 +147,7 @@ void do_softirq_own_stack(void)
  2971. call_on_stack(__do_softirq, isp);
  2972. }
  2973. +#endif
  2974. bool handle_irq(struct irq_desc *desc, struct pt_regs *regs)
  2975. {
  2976. diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c
  2977. index 32187f8a49b4..1f456f1e06f8 100644
  2978. --- a/arch/x86/kernel/kvm.c
  2979. +++ b/arch/x86/kernel/kvm.c
  2980. @@ -36,6 +36,7 @@
  2981. #include <linux/kprobes.h>
  2982. #include <linux/debugfs.h>
  2983. #include <linux/nmi.h>
  2984. +#include <linux/swait.h>
  2985. #include <asm/timer.h>
  2986. #include <asm/cpu.h>
  2987. #include <asm/traps.h>
  2988. @@ -91,14 +92,14 @@ static void kvm_io_delay(void)
  2989. struct kvm_task_sleep_node {
  2990. struct hlist_node link;
  2991. - wait_queue_head_t wq;
  2992. + struct swait_queue_head wq;
  2993. u32 token;
  2994. int cpu;
  2995. bool halted;
  2996. };
  2997. static struct kvm_task_sleep_head {
  2998. - spinlock_t lock;
  2999. + raw_spinlock_t lock;
  3000. struct hlist_head list;
  3001. } async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
  3002. @@ -122,17 +123,17 @@ void kvm_async_pf_task_wait(u32 token)
  3003. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  3004. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  3005. struct kvm_task_sleep_node n, *e;
  3006. - DEFINE_WAIT(wait);
  3007. + DECLARE_SWAITQUEUE(wait);
  3008. rcu_irq_enter();
  3009. - spin_lock(&b->lock);
  3010. + raw_spin_lock(&b->lock);
  3011. e = _find_apf_task(b, token);
  3012. if (e) {
  3013. /* dummy entry exist -> wake up was delivered ahead of PF */
  3014. hlist_del(&e->link);
  3015. kfree(e);
  3016. - spin_unlock(&b->lock);
  3017. + raw_spin_unlock(&b->lock);
  3018. rcu_irq_exit();
  3019. return;
  3020. @@ -141,13 +142,13 @@ void kvm_async_pf_task_wait(u32 token)
  3021. n.token = token;
  3022. n.cpu = smp_processor_id();
  3023. n.halted = is_idle_task(current) || preempt_count() > 1;
  3024. - init_waitqueue_head(&n.wq);
  3025. + init_swait_queue_head(&n.wq);
  3026. hlist_add_head(&n.link, &b->list);
  3027. - spin_unlock(&b->lock);
  3028. + raw_spin_unlock(&b->lock);
  3029. for (;;) {
  3030. if (!n.halted)
  3031. - prepare_to_wait(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
  3032. + prepare_to_swait(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
  3033. if (hlist_unhashed(&n.link))
  3034. break;
  3035. @@ -168,7 +169,7 @@ void kvm_async_pf_task_wait(u32 token)
  3036. rcu_irq_enter();
  3037. }
  3038. if (!n.halted)
  3039. - finish_wait(&n.wq, &wait);
  3040. + finish_swait(&n.wq, &wait);
  3041. rcu_irq_exit();
  3042. return;
  3043. @@ -180,8 +181,8 @@ static void apf_task_wake_one(struct kvm_task_sleep_node *n)
  3044. hlist_del_init(&n->link);
  3045. if (n->halted)
  3046. smp_send_reschedule(n->cpu);
  3047. - else if (waitqueue_active(&n->wq))
  3048. - wake_up(&n->wq);
  3049. + else if (swait_active(&n->wq))
  3050. + swake_up(&n->wq);
  3051. }
  3052. static void apf_task_wake_all(void)
  3053. @@ -191,14 +192,14 @@ static void apf_task_wake_all(void)
  3054. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
  3055. struct hlist_node *p, *next;
  3056. struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
  3057. - spin_lock(&b->lock);
  3058. + raw_spin_lock(&b->lock);
  3059. hlist_for_each_safe(p, next, &b->list) {
  3060. struct kvm_task_sleep_node *n =
  3061. hlist_entry(p, typeof(*n), link);
  3062. if (n->cpu == smp_processor_id())
  3063. apf_task_wake_one(n);
  3064. }
  3065. - spin_unlock(&b->lock);
  3066. + raw_spin_unlock(&b->lock);
  3067. }
  3068. }
  3069. @@ -214,7 +215,7 @@ void kvm_async_pf_task_wake(u32 token)
  3070. }
  3071. again:
  3072. - spin_lock(&b->lock);
  3073. + raw_spin_lock(&b->lock);
  3074. n = _find_apf_task(b, token);
  3075. if (!n) {
  3076. /*
  3077. @@ -227,17 +228,17 @@ void kvm_async_pf_task_wake(u32 token)
  3078. * Allocation failed! Busy wait while other cpu
  3079. * handles async PF.
  3080. */
  3081. - spin_unlock(&b->lock);
  3082. + raw_spin_unlock(&b->lock);
  3083. cpu_relax();
  3084. goto again;
  3085. }
  3086. n->token = token;
  3087. n->cpu = smp_processor_id();
  3088. - init_waitqueue_head(&n->wq);
  3089. + init_swait_queue_head(&n->wq);
  3090. hlist_add_head(&n->link, &b->list);
  3091. } else
  3092. apf_task_wake_one(n);
  3093. - spin_unlock(&b->lock);
  3094. + raw_spin_unlock(&b->lock);
  3095. return;
  3096. }
  3097. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
  3098. @@ -488,7 +489,7 @@ void __init kvm_guest_init(void)
  3099. paravirt_ops_setup();
  3100. register_reboot_notifier(&kvm_pv_reboot_nb);
  3101. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  3102. - spin_lock_init(&async_pf_sleepers[i].lock);
  3103. + raw_spin_lock_init(&async_pf_sleepers[i].lock);
  3104. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
  3105. x86_init.irqs.trap_init = kvm_apf_trap_init;
  3106. diff --git a/arch/x86/kernel/nmi.c b/arch/x86/kernel/nmi.c
  3107. index 697f90db0e37..424aec4a4c71 100644
  3108. --- a/arch/x86/kernel/nmi.c
  3109. +++ b/arch/x86/kernel/nmi.c
  3110. @@ -231,7 +231,7 @@ pci_serr_error(unsigned char reason, struct pt_regs *regs)
  3111. #endif
  3112. if (panic_on_unrecovered_nmi)
  3113. - panic("NMI: Not continuing");
  3114. + nmi_panic(regs, "NMI: Not continuing");
  3115. pr_emerg("Dazed and confused, but trying to continue\n");
  3116. @@ -255,8 +255,16 @@ io_check_error(unsigned char reason, struct pt_regs *regs)
  3117. reason, smp_processor_id());
  3118. show_regs(regs);
  3119. - if (panic_on_io_nmi)
  3120. - panic("NMI IOCK error: Not continuing");
  3121. + if (panic_on_io_nmi) {
  3122. + nmi_panic(regs, "NMI IOCK error: Not continuing");
  3123. +
  3124. + /*
  3125. + * If we end up here, it means we have received an NMI while
  3126. + * processing panic(). Simply return without delaying and
  3127. + * re-enabling NMIs.
  3128. + */
  3129. + return;
  3130. + }
  3131. /* Re-enable the IOCK line, wait for a few seconds */
  3132. reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_IOCHK;
  3133. @@ -297,7 +305,7 @@ unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
  3134. pr_emerg("Do you have a strange power saving mode enabled?\n");
  3135. if (unknown_nmi_panic || panic_on_unrecovered_nmi)
  3136. - panic("NMI: Not continuing");
  3137. + nmi_panic(regs, "NMI: Not continuing");
  3138. pr_emerg("Dazed and confused, but trying to continue\n");
  3139. }
  3140. diff --git a/arch/x86/kernel/process_32.c b/arch/x86/kernel/process_32.c
  3141. index 9f950917528b..4dd4beae917a 100644
  3142. --- a/arch/x86/kernel/process_32.c
  3143. +++ b/arch/x86/kernel/process_32.c
  3144. @@ -35,6 +35,7 @@
  3145. #include <linux/uaccess.h>
  3146. #include <linux/io.h>
  3147. #include <linux/kdebug.h>
  3148. +#include <linux/highmem.h>
  3149. #include <asm/pgtable.h>
  3150. #include <asm/ldt.h>
  3151. @@ -210,6 +211,35 @@ start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  3152. }
  3153. EXPORT_SYMBOL_GPL(start_thread);
  3154. +#ifdef CONFIG_PREEMPT_RT_FULL
  3155. +static void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  3156. +{
  3157. + int i;
  3158. +
  3159. + /*
  3160. + * Clear @prev's kmap_atomic mappings
  3161. + */
  3162. + for (i = 0; i < prev_p->kmap_idx; i++) {
  3163. + int idx = i + KM_TYPE_NR * smp_processor_id();
  3164. + pte_t *ptep = kmap_pte - idx;
  3165. +
  3166. + kpte_clear_flush(ptep, __fix_to_virt(FIX_KMAP_BEGIN + idx));
  3167. + }
  3168. + /*
  3169. + * Restore @next_p's kmap_atomic mappings
  3170. + */
  3171. + for (i = 0; i < next_p->kmap_idx; i++) {
  3172. + int idx = i + KM_TYPE_NR * smp_processor_id();
  3173. +
  3174. + if (!pte_none(next_p->kmap_pte[i]))
  3175. + set_pte(kmap_pte - idx, next_p->kmap_pte[i]);
  3176. + }
  3177. +}
  3178. +#else
  3179. +static inline void
  3180. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  3181. +#endif
  3182. +
  3183. /*
  3184. * switch_to(x,y) should switch tasks from x to y.
  3185. @@ -286,6 +316,8 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  3186. task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
  3187. __switch_to_xtra(prev_p, next_p, tss);
  3188. + switch_kmaps(prev_p, next_p);
  3189. +
  3190. /*
  3191. * Leave lazy mode, flushing any hypercalls made here.
  3192. * This must be done before restoring TLS segments so
  3193. diff --git a/arch/x86/kernel/reboot.c b/arch/x86/kernel/reboot.c
  3194. index 9a16932c7258..219ffb9ba3a9 100644
  3195. --- a/arch/x86/kernel/reboot.c
  3196. +++ b/arch/x86/kernel/reboot.c
  3197. @@ -730,6 +730,7 @@ static int crashing_cpu;
  3198. static nmi_shootdown_cb shootdown_callback;
  3199. static atomic_t waiting_for_crash_ipi;
  3200. +static int crash_ipi_issued;
  3201. static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
  3202. {
  3203. @@ -792,6 +793,9 @@ void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  3204. smp_send_nmi_allbutself();
  3205. + /* Kick CPUs looping in NMI context. */
  3206. + WRITE_ONCE(crash_ipi_issued, 1);
  3207. +
  3208. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  3209. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  3210. mdelay(1);
  3211. @@ -800,6 +804,22 @@ void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  3212. /* Leave the nmi callback set */
  3213. }
  3214. +
  3215. +/* Override the weak function in kernel/panic.c */
  3216. +void nmi_panic_self_stop(struct pt_regs *regs)
  3217. +{
  3218. + while (1) {
  3219. + /*
  3220. + * Wait for the crash dumping IPI to be issued, and then
  3221. + * call its callback directly.
  3222. + */
  3223. + if (READ_ONCE(crash_ipi_issued))
  3224. + crash_nmi_callback(0, regs); /* Don't return */
  3225. +
  3226. + cpu_relax();
  3227. + }
  3228. +}
  3229. +
  3230. #else /* !CONFIG_SMP */
  3231. void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  3232. {
  3233. diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
  3234. index 1c96f09367ae..ffb6d9859122 100644
  3235. --- a/arch/x86/kvm/lapic.c
  3236. +++ b/arch/x86/kvm/lapic.c
  3237. @@ -1195,7 +1195,7 @@ static void apic_update_lvtt(struct kvm_lapic *apic)
  3238. static void apic_timer_expired(struct kvm_lapic *apic)
  3239. {
  3240. struct kvm_vcpu *vcpu = apic->vcpu;
  3241. - wait_queue_head_t *q = &vcpu->wq;
  3242. + struct swait_queue_head *q = &vcpu->wq;
  3243. struct kvm_timer *ktimer = &apic->lapic_timer;
  3244. if (atomic_read(&apic->lapic_timer.pending))
  3245. @@ -1204,8 +1204,8 @@ static void apic_timer_expired(struct kvm_lapic *apic)
  3246. atomic_inc(&apic->lapic_timer.pending);
  3247. kvm_set_pending_timer(vcpu);
  3248. - if (waitqueue_active(q))
  3249. - wake_up_interruptible(q);
  3250. + if (swait_active(q))
  3251. + swake_up(q);
  3252. if (apic_lvtt_tscdeadline(apic))
  3253. ktimer->expired_tscdeadline = ktimer->tscdeadline;
  3254. @@ -1801,6 +1801,7 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu)
  3255. hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
  3256. HRTIMER_MODE_ABS);
  3257. apic->lapic_timer.timer.function = apic_timer_fn;
  3258. + apic->lapic_timer.timer.irqsafe = 1;
  3259. /*
  3260. * APIC is created enabled. This will prevent kvm_lapic_set_base from
  3261. diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
  3262. index f973cfa8ff4f..b63350721623 100644
  3263. --- a/arch/x86/kvm/x86.c
  3264. +++ b/arch/x86/kvm/x86.c
  3265. @@ -5836,6 +5836,13 @@ int kvm_arch_init(void *opaque)
  3266. goto out;
  3267. }
  3268. +#ifdef CONFIG_PREEMPT_RT_FULL
  3269. + if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  3270. + printk(KERN_ERR "RT requires X86_FEATURE_CONSTANT_TSC\n");
  3271. + return -EOPNOTSUPP;
  3272. + }
  3273. +#endif
  3274. +
  3275. r = kvm_mmu_module_init();
  3276. if (r)
  3277. goto out_free_percpu;
  3278. diff --git a/arch/x86/mm/highmem_32.c b/arch/x86/mm/highmem_32.c
  3279. index a6d739258137..bd24ba1c4a86 100644
  3280. --- a/arch/x86/mm/highmem_32.c
  3281. +++ b/arch/x86/mm/highmem_32.c
  3282. @@ -32,10 +32,11 @@ EXPORT_SYMBOL(kunmap);
  3283. */
  3284. void *kmap_atomic_prot(struct page *page, pgprot_t prot)
  3285. {
  3286. + pte_t pte = mk_pte(page, prot);
  3287. unsigned long vaddr;
  3288. int idx, type;
  3289. - preempt_disable();
  3290. + preempt_disable_nort();
  3291. pagefault_disable();
  3292. if (!PageHighMem(page))
  3293. @@ -45,7 +46,10 @@ void *kmap_atomic_prot(struct page *page, pgprot_t prot)
  3294. idx = type + KM_TYPE_NR*smp_processor_id();
  3295. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  3296. BUG_ON(!pte_none(*(kmap_pte-idx)));
  3297. - set_pte(kmap_pte-idx, mk_pte(page, prot));
  3298. +#ifdef CONFIG_PREEMPT_RT_FULL
  3299. + current->kmap_pte[type] = pte;
  3300. +#endif
  3301. + set_pte(kmap_pte-idx, pte);
  3302. arch_flush_lazy_mmu_mode();
  3303. return (void *)vaddr;
  3304. @@ -88,6 +92,9 @@ void __kunmap_atomic(void *kvaddr)
  3305. * is a bad idea also, in case the page changes cacheability
  3306. * attributes or becomes a protected page in a hypervisor.
  3307. */
  3308. +#ifdef CONFIG_PREEMPT_RT_FULL
  3309. + current->kmap_pte[type] = __pte(0);
  3310. +#endif
  3311. kpte_clear_flush(kmap_pte-idx, vaddr);
  3312. kmap_atomic_idx_pop();
  3313. arch_flush_lazy_mmu_mode();
  3314. @@ -100,7 +107,7 @@ void __kunmap_atomic(void *kvaddr)
  3315. #endif
  3316. pagefault_enable();
  3317. - preempt_enable();
  3318. + preempt_enable_nort();
  3319. }
  3320. EXPORT_SYMBOL(__kunmap_atomic);
  3321. diff --git a/arch/x86/mm/iomap_32.c b/arch/x86/mm/iomap_32.c
  3322. index 9c0ff045fdd4..dd25dd1671b6 100644
  3323. --- a/arch/x86/mm/iomap_32.c
  3324. +++ b/arch/x86/mm/iomap_32.c
  3325. @@ -56,6 +56,7 @@ EXPORT_SYMBOL_GPL(iomap_free);
  3326. void *kmap_atomic_prot_pfn(unsigned long pfn, pgprot_t prot)
  3327. {
  3328. + pte_t pte = pfn_pte(pfn, prot);
  3329. unsigned long vaddr;
  3330. int idx, type;
  3331. @@ -65,7 +66,12 @@ void *kmap_atomic_prot_pfn(unsigned long pfn, pgprot_t prot)
  3332. type = kmap_atomic_idx_push();
  3333. idx = type + KM_TYPE_NR * smp_processor_id();
  3334. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  3335. - set_pte(kmap_pte - idx, pfn_pte(pfn, prot));
  3336. + WARN_ON(!pte_none(*(kmap_pte - idx)));
  3337. +
  3338. +#ifdef CONFIG_PREEMPT_RT_FULL
  3339. + current->kmap_pte[type] = pte;
  3340. +#endif
  3341. + set_pte(kmap_pte - idx, pte);
  3342. arch_flush_lazy_mmu_mode();
  3343. return (void *)vaddr;
  3344. @@ -113,6 +119,9 @@ iounmap_atomic(void __iomem *kvaddr)
  3345. * is a bad idea also, in case the page changes cacheability
  3346. * attributes or becomes a protected page in a hypervisor.
  3347. */
  3348. +#ifdef CONFIG_PREEMPT_RT_FULL
  3349. + current->kmap_pte[type] = __pte(0);
  3350. +#endif
  3351. kpte_clear_flush(kmap_pte-idx, vaddr);
  3352. kmap_atomic_idx_pop();
  3353. }
  3354. diff --git a/arch/x86/mm/pageattr.c b/arch/x86/mm/pageattr.c
  3355. index 79377e2a7bcd..dca36c1f1966 100644
  3356. --- a/arch/x86/mm/pageattr.c
  3357. +++ b/arch/x86/mm/pageattr.c
  3358. @@ -209,7 +209,15 @@ static void cpa_flush_array(unsigned long *start, int numpages, int cache,
  3359. int in_flags, struct page **pages)
  3360. {
  3361. unsigned int i, level;
  3362. +#ifdef CONFIG_PREEMPT
  3363. + /*
  3364. + * Avoid wbinvd() because it causes latencies on all CPUs,
  3365. + * regardless of any CPU isolation that may be in effect.
  3366. + */
  3367. + unsigned long do_wbinvd = 0;
  3368. +#else
  3369. unsigned long do_wbinvd = cache && numpages >= 1024; /* 4M threshold */
  3370. +#endif
  3371. BUG_ON(irqs_disabled());
  3372. diff --git a/arch/x86/platform/uv/tlb_uv.c b/arch/x86/platform/uv/tlb_uv.c
  3373. index 3b6ec42718e4..7871083de089 100644
  3374. --- a/arch/x86/platform/uv/tlb_uv.c
  3375. +++ b/arch/x86/platform/uv/tlb_uv.c
  3376. @@ -714,9 +714,9 @@ static void destination_plugged(struct bau_desc *bau_desc,
  3377. quiesce_local_uvhub(hmaster);
  3378. - spin_lock(&hmaster->queue_lock);
  3379. + raw_spin_lock(&hmaster->queue_lock);
  3380. reset_with_ipi(&bau_desc->distribution, bcp);
  3381. - spin_unlock(&hmaster->queue_lock);
  3382. + raw_spin_unlock(&hmaster->queue_lock);
  3383. end_uvhub_quiesce(hmaster);
  3384. @@ -736,9 +736,9 @@ static void destination_timeout(struct bau_desc *bau_desc,
  3385. quiesce_local_uvhub(hmaster);
  3386. - spin_lock(&hmaster->queue_lock);
  3387. + raw_spin_lock(&hmaster->queue_lock);
  3388. reset_with_ipi(&bau_desc->distribution, bcp);
  3389. - spin_unlock(&hmaster->queue_lock);
  3390. + raw_spin_unlock(&hmaster->queue_lock);
  3391. end_uvhub_quiesce(hmaster);
  3392. @@ -759,7 +759,7 @@ static void disable_for_period(struct bau_control *bcp, struct ptc_stats *stat)
  3393. cycles_t tm1;
  3394. hmaster = bcp->uvhub_master;
  3395. - spin_lock(&hmaster->disable_lock);
  3396. + raw_spin_lock(&hmaster->disable_lock);
  3397. if (!bcp->baudisabled) {
  3398. stat->s_bau_disabled++;
  3399. tm1 = get_cycles();
  3400. @@ -772,7 +772,7 @@ static void disable_for_period(struct bau_control *bcp, struct ptc_stats *stat)
  3401. }
  3402. }
  3403. }
  3404. - spin_unlock(&hmaster->disable_lock);
  3405. + raw_spin_unlock(&hmaster->disable_lock);
  3406. }
  3407. static void count_max_concurr(int stat, struct bau_control *bcp,
  3408. @@ -835,7 +835,7 @@ static void record_send_stats(cycles_t time1, cycles_t time2,
  3409. */
  3410. static void uv1_throttle(struct bau_control *hmaster, struct ptc_stats *stat)
  3411. {
  3412. - spinlock_t *lock = &hmaster->uvhub_lock;
  3413. + raw_spinlock_t *lock = &hmaster->uvhub_lock;
  3414. atomic_t *v;
  3415. v = &hmaster->active_descriptor_count;
  3416. @@ -968,7 +968,7 @@ static int check_enable(struct bau_control *bcp, struct ptc_stats *stat)
  3417. struct bau_control *hmaster;
  3418. hmaster = bcp->uvhub_master;
  3419. - spin_lock(&hmaster->disable_lock);
  3420. + raw_spin_lock(&hmaster->disable_lock);
  3421. if (bcp->baudisabled && (get_cycles() >= bcp->set_bau_on_time)) {
  3422. stat->s_bau_reenabled++;
  3423. for_each_present_cpu(tcpu) {
  3424. @@ -980,10 +980,10 @@ static int check_enable(struct bau_control *bcp, struct ptc_stats *stat)
  3425. tbcp->period_giveups = 0;
  3426. }
  3427. }
  3428. - spin_unlock(&hmaster->disable_lock);
  3429. + raw_spin_unlock(&hmaster->disable_lock);
  3430. return 0;
  3431. }
  3432. - spin_unlock(&hmaster->disable_lock);
  3433. + raw_spin_unlock(&hmaster->disable_lock);
  3434. return -1;
  3435. }
  3436. @@ -1901,9 +1901,9 @@ static void __init init_per_cpu_tunables(void)
  3437. bcp->cong_reps = congested_reps;
  3438. bcp->disabled_period = sec_2_cycles(disabled_period);
  3439. bcp->giveup_limit = giveup_limit;
  3440. - spin_lock_init(&bcp->queue_lock);
  3441. - spin_lock_init(&bcp->uvhub_lock);
  3442. - spin_lock_init(&bcp->disable_lock);
  3443. + raw_spin_lock_init(&bcp->queue_lock);
  3444. + raw_spin_lock_init(&bcp->uvhub_lock);
  3445. + raw_spin_lock_init(&bcp->disable_lock);
  3446. }
  3447. }
  3448. diff --git a/arch/x86/platform/uv/uv_time.c b/arch/x86/platform/uv/uv_time.c
  3449. index 2b158a9fa1d7..5e0b122620cb 100644
  3450. --- a/arch/x86/platform/uv/uv_time.c
  3451. +++ b/arch/x86/platform/uv/uv_time.c
  3452. @@ -57,7 +57,7 @@ static DEFINE_PER_CPU(struct clock_event_device, cpu_ced);
  3453. /* There is one of these allocated per node */
  3454. struct uv_rtc_timer_head {
  3455. - spinlock_t lock;
  3456. + raw_spinlock_t lock;
  3457. /* next cpu waiting for timer, local node relative: */
  3458. int next_cpu;
  3459. /* number of cpus on this node: */
  3460. @@ -177,7 +177,7 @@ static __init int uv_rtc_allocate_timers(void)
  3461. uv_rtc_deallocate_timers();
  3462. return -ENOMEM;
  3463. }
  3464. - spin_lock_init(&head->lock);
  3465. + raw_spin_lock_init(&head->lock);
  3466. head->ncpus = uv_blade_nr_possible_cpus(bid);
  3467. head->next_cpu = -1;
  3468. blade_info[bid] = head;
  3469. @@ -231,7 +231,7 @@ static int uv_rtc_set_timer(int cpu, u64 expires)
  3470. unsigned long flags;
  3471. int next_cpu;
  3472. - spin_lock_irqsave(&head->lock, flags);
  3473. + raw_spin_lock_irqsave(&head->lock, flags);
  3474. next_cpu = head->next_cpu;
  3475. *t = expires;
  3476. @@ -243,12 +243,12 @@ static int uv_rtc_set_timer(int cpu, u64 expires)
  3477. if (uv_setup_intr(cpu, expires)) {
  3478. *t = ULLONG_MAX;
  3479. uv_rtc_find_next_timer(head, pnode);
  3480. - spin_unlock_irqrestore(&head->lock, flags);
  3481. + raw_spin_unlock_irqrestore(&head->lock, flags);
  3482. return -ETIME;
  3483. }
  3484. }
  3485. - spin_unlock_irqrestore(&head->lock, flags);
  3486. + raw_spin_unlock_irqrestore(&head->lock, flags);
  3487. return 0;
  3488. }
  3489. @@ -267,7 +267,7 @@ static int uv_rtc_unset_timer(int cpu, int force)
  3490. unsigned long flags;
  3491. int rc = 0;
  3492. - spin_lock_irqsave(&head->lock, flags);
  3493. + raw_spin_lock_irqsave(&head->lock, flags);
  3494. if ((head->next_cpu == bcpu && uv_read_rtc(NULL) >= *t) || force)
  3495. rc = 1;
  3496. @@ -279,7 +279,7 @@ static int uv_rtc_unset_timer(int cpu, int force)
  3497. uv_rtc_find_next_timer(head, pnode);
  3498. }
  3499. - spin_unlock_irqrestore(&head->lock, flags);
  3500. + raw_spin_unlock_irqrestore(&head->lock, flags);
  3501. return rc;
  3502. }
  3503. @@ -299,13 +299,18 @@ static int uv_rtc_unset_timer(int cpu, int force)
  3504. static cycle_t uv_read_rtc(struct clocksource *cs)
  3505. {
  3506. unsigned long offset;
  3507. + cycle_t cycles;
  3508. + preempt_disable();
  3509. if (uv_get_min_hub_revision_id() == 1)
  3510. offset = 0;
  3511. else
  3512. offset = (uv_blade_processor_id() * L1_CACHE_BYTES) % PAGE_SIZE;
  3513. - return (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  3514. + cycles = (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  3515. + preempt_enable();
  3516. +
  3517. + return cycles;
  3518. }
  3519. /*
  3520. diff --git a/block/blk-core.c b/block/blk-core.c
  3521. index f5f1a55703ae..75a76bcc7ac0 100644
  3522. --- a/block/blk-core.c
  3523. +++ b/block/blk-core.c
  3524. @@ -125,6 +125,9 @@ void blk_rq_init(struct request_queue *q, struct request *rq)
  3525. INIT_LIST_HEAD(&rq->queuelist);
  3526. INIT_LIST_HEAD(&rq->timeout_list);
  3527. +#ifdef CONFIG_PREEMPT_RT_FULL
  3528. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  3529. +#endif
  3530. rq->cpu = -1;
  3531. rq->q = q;
  3532. rq->__sector = (sector_t) -1;
  3533. @@ -233,7 +236,7 @@ EXPORT_SYMBOL(blk_start_queue_async);
  3534. **/
  3535. void blk_start_queue(struct request_queue *q)
  3536. {
  3537. - WARN_ON(!in_interrupt() && !irqs_disabled());
  3538. + WARN_ON_NONRT(!in_interrupt() && !irqs_disabled());
  3539. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  3540. __blk_run_queue(q);
  3541. @@ -659,7 +662,7 @@ int blk_queue_enter(struct request_queue *q, gfp_t gfp)
  3542. if (!gfpflags_allow_blocking(gfp))
  3543. return -EBUSY;
  3544. - ret = wait_event_interruptible(q->mq_freeze_wq,
  3545. + ret = swait_event_interruptible(q->mq_freeze_wq,
  3546. !atomic_read(&q->mq_freeze_depth) ||
  3547. blk_queue_dying(q));
  3548. if (blk_queue_dying(q))
  3549. @@ -679,7 +682,7 @@ static void blk_queue_usage_counter_release(struct percpu_ref *ref)
  3550. struct request_queue *q =
  3551. container_of(ref, struct request_queue, q_usage_counter);
  3552. - wake_up_all(&q->mq_freeze_wq);
  3553. + swake_up_all(&q->mq_freeze_wq);
  3554. }
  3555. struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
  3556. @@ -741,7 +744,7 @@ struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
  3557. q->bypass_depth = 1;
  3558. __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
  3559. - init_waitqueue_head(&q->mq_freeze_wq);
  3560. + init_swait_queue_head(&q->mq_freeze_wq);
  3561. /*
  3562. * Init percpu_ref in atomic mode so that it's faster to shutdown.
  3563. @@ -3222,7 +3225,7 @@ static void queue_unplugged(struct request_queue *q, unsigned int depth,
  3564. blk_run_queue_async(q);
  3565. else
  3566. __blk_run_queue(q);
  3567. - spin_unlock(q->queue_lock);
  3568. + spin_unlock_irq(q->queue_lock);
  3569. }
  3570. static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
  3571. @@ -3270,7 +3273,6 @@ EXPORT_SYMBOL(blk_check_plugged);
  3572. void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  3573. {
  3574. struct request_queue *q;
  3575. - unsigned long flags;
  3576. struct request *rq;
  3577. LIST_HEAD(list);
  3578. unsigned int depth;
  3579. @@ -3290,11 +3292,6 @@ void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  3580. q = NULL;
  3581. depth = 0;
  3582. - /*
  3583. - * Save and disable interrupts here, to avoid doing it for every
  3584. - * queue lock we have to take.
  3585. - */
  3586. - local_irq_save(flags);
  3587. while (!list_empty(&list)) {
  3588. rq = list_entry_rq(list.next);
  3589. list_del_init(&rq->queuelist);
  3590. @@ -3307,7 +3304,7 @@ void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  3591. queue_unplugged(q, depth, from_schedule);
  3592. q = rq->q;
  3593. depth = 0;
  3594. - spin_lock(q->queue_lock);
  3595. + spin_lock_irq(q->queue_lock);
  3596. }
  3597. /*
  3598. @@ -3334,8 +3331,6 @@ void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  3599. */
  3600. if (q)
  3601. queue_unplugged(q, depth, from_schedule);
  3602. -
  3603. - local_irq_restore(flags);
  3604. }
  3605. void blk_finish_plug(struct blk_plug *plug)
  3606. diff --git a/block/blk-ioc.c b/block/blk-ioc.c
  3607. index 381cb50a673c..dc8785233d94 100644
  3608. --- a/block/blk-ioc.c
  3609. +++ b/block/blk-ioc.c
  3610. @@ -7,6 +7,7 @@
  3611. #include <linux/bio.h>
  3612. #include <linux/blkdev.h>
  3613. #include <linux/slab.h>
  3614. +#include <linux/delay.h>
  3615. #include "blk.h"
  3616. @@ -109,7 +110,7 @@ static void ioc_release_fn(struct work_struct *work)
  3617. spin_unlock(q->queue_lock);
  3618. } else {
  3619. spin_unlock_irqrestore(&ioc->lock, flags);
  3620. - cpu_relax();
  3621. + cpu_chill();
  3622. spin_lock_irqsave_nested(&ioc->lock, flags, 1);
  3623. }
  3624. }
  3625. @@ -187,7 +188,7 @@ void put_io_context_active(struct io_context *ioc)
  3626. spin_unlock(icq->q->queue_lock);
  3627. } else {
  3628. spin_unlock_irqrestore(&ioc->lock, flags);
  3629. - cpu_relax();
  3630. + cpu_chill();
  3631. goto retry;
  3632. }
  3633. }
  3634. diff --git a/block/blk-iopoll.c b/block/blk-iopoll.c
  3635. index 0736729d6494..3e21e31d0d7e 100644
  3636. --- a/block/blk-iopoll.c
  3637. +++ b/block/blk-iopoll.c
  3638. @@ -35,6 +35,7 @@ void blk_iopoll_sched(struct blk_iopoll *iop)
  3639. list_add_tail(&iop->list, this_cpu_ptr(&blk_cpu_iopoll));
  3640. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  3641. local_irq_restore(flags);
  3642. + preempt_check_resched_rt();
  3643. }
  3644. EXPORT_SYMBOL(blk_iopoll_sched);
  3645. @@ -132,6 +133,7 @@ static void blk_iopoll_softirq(struct softirq_action *h)
  3646. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  3647. local_irq_enable();
  3648. + preempt_check_resched_rt();
  3649. }
  3650. /**
  3651. @@ -201,6 +203,7 @@ static int blk_iopoll_cpu_notify(struct notifier_block *self,
  3652. this_cpu_ptr(&blk_cpu_iopoll));
  3653. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  3654. local_irq_enable();
  3655. + preempt_check_resched_rt();
  3656. }
  3657. return NOTIFY_OK;
  3658. diff --git a/block/blk-mq-cpu.c b/block/blk-mq-cpu.c
  3659. index bb3ed488f7b5..628c6c13c482 100644
  3660. --- a/block/blk-mq-cpu.c
  3661. +++ b/block/blk-mq-cpu.c
  3662. @@ -16,7 +16,7 @@
  3663. #include "blk-mq.h"
  3664. static LIST_HEAD(blk_mq_cpu_notify_list);
  3665. -static DEFINE_RAW_SPINLOCK(blk_mq_cpu_notify_lock);
  3666. +static DEFINE_SPINLOCK(blk_mq_cpu_notify_lock);
  3667. static int blk_mq_main_cpu_notify(struct notifier_block *self,
  3668. unsigned long action, void *hcpu)
  3669. @@ -25,7 +25,10 @@ static int blk_mq_main_cpu_notify(struct notifier_block *self,
  3670. struct blk_mq_cpu_notifier *notify;
  3671. int ret = NOTIFY_OK;
  3672. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  3673. + if (action != CPU_POST_DEAD)
  3674. + return NOTIFY_OK;
  3675. +
  3676. + spin_lock(&blk_mq_cpu_notify_lock);
  3677. list_for_each_entry(notify, &blk_mq_cpu_notify_list, list) {
  3678. ret = notify->notify(notify->data, action, cpu);
  3679. @@ -33,7 +36,7 @@ static int blk_mq_main_cpu_notify(struct notifier_block *self,
  3680. break;
  3681. }
  3682. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  3683. + spin_unlock(&blk_mq_cpu_notify_lock);
  3684. return ret;
  3685. }
  3686. @@ -41,16 +44,16 @@ void blk_mq_register_cpu_notifier(struct blk_mq_cpu_notifier *notifier)
  3687. {
  3688. BUG_ON(!notifier->notify);
  3689. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  3690. + spin_lock(&blk_mq_cpu_notify_lock);
  3691. list_add_tail(&notifier->list, &blk_mq_cpu_notify_list);
  3692. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  3693. + spin_unlock(&blk_mq_cpu_notify_lock);
  3694. }
  3695. void blk_mq_unregister_cpu_notifier(struct blk_mq_cpu_notifier *notifier)
  3696. {
  3697. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  3698. + spin_lock(&blk_mq_cpu_notify_lock);
  3699. list_del(&notifier->list);
  3700. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  3701. + spin_unlock(&blk_mq_cpu_notify_lock);
  3702. }
  3703. void blk_mq_init_cpu_notifier(struct blk_mq_cpu_notifier *notifier,
  3704. diff --git a/block/blk-mq.c b/block/blk-mq.c
  3705. index 0d1af3e44efb..e4fc80184dd8 100644
  3706. --- a/block/blk-mq.c
  3707. +++ b/block/blk-mq.c
  3708. @@ -92,7 +92,7 @@ EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start);
  3709. static void blk_mq_freeze_queue_wait(struct request_queue *q)
  3710. {
  3711. - wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  3712. + swait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  3713. }
  3714. /*
  3715. @@ -130,7 +130,7 @@ void blk_mq_unfreeze_queue(struct request_queue *q)
  3716. WARN_ON_ONCE(freeze_depth < 0);
  3717. if (!freeze_depth) {
  3718. percpu_ref_reinit(&q->q_usage_counter);
  3719. - wake_up_all(&q->mq_freeze_wq);
  3720. + swake_up_all(&q->mq_freeze_wq);
  3721. }
  3722. }
  3723. EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
  3724. @@ -149,7 +149,7 @@ void blk_mq_wake_waiters(struct request_queue *q)
  3725. * dying, we need to ensure that processes currently waiting on
  3726. * the queue are notified as well.
  3727. */
  3728. - wake_up_all(&q->mq_freeze_wq);
  3729. + swake_up_all(&q->mq_freeze_wq);
  3730. }
  3731. bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
  3732. @@ -196,6 +196,9 @@ static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
  3733. rq->resid_len = 0;
  3734. rq->sense = NULL;
  3735. +#ifdef CONFIG_PREEMPT_RT_FULL
  3736. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  3737. +#endif
  3738. INIT_LIST_HEAD(&rq->timeout_list);
  3739. rq->timeout = 0;
  3740. @@ -325,6 +328,17 @@ void blk_mq_end_request(struct request *rq, int error)
  3741. }
  3742. EXPORT_SYMBOL(blk_mq_end_request);
  3743. +#ifdef CONFIG_PREEMPT_RT_FULL
  3744. +
  3745. +void __blk_mq_complete_request_remote_work(struct work_struct *work)
  3746. +{
  3747. + struct request *rq = container_of(work, struct request, work);
  3748. +
  3749. + rq->q->softirq_done_fn(rq);
  3750. +}
  3751. +
  3752. +#else
  3753. +
  3754. static void __blk_mq_complete_request_remote(void *data)
  3755. {
  3756. struct request *rq = data;
  3757. @@ -332,6 +346,8 @@ static void __blk_mq_complete_request_remote(void *data)
  3758. rq->q->softirq_done_fn(rq);
  3759. }
  3760. +#endif
  3761. +
  3762. static void blk_mq_ipi_complete_request(struct request *rq)
  3763. {
  3764. struct blk_mq_ctx *ctx = rq->mq_ctx;
  3765. @@ -343,19 +359,23 @@ static void blk_mq_ipi_complete_request(struct request *rq)
  3766. return;
  3767. }
  3768. - cpu = get_cpu();
  3769. + cpu = get_cpu_light();
  3770. if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
  3771. shared = cpus_share_cache(cpu, ctx->cpu);
  3772. if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
  3773. +#ifdef CONFIG_PREEMPT_RT_FULL
  3774. + schedule_work_on(ctx->cpu, &rq->work);
  3775. +#else
  3776. rq->csd.func = __blk_mq_complete_request_remote;
  3777. rq->csd.info = rq;
  3778. rq->csd.flags = 0;
  3779. smp_call_function_single_async(ctx->cpu, &rq->csd);
  3780. +#endif
  3781. } else {
  3782. rq->q->softirq_done_fn(rq);
  3783. }
  3784. - put_cpu();
  3785. + put_cpu_light();
  3786. }
  3787. static void __blk_mq_complete_request(struct request *rq)
  3788. @@ -862,14 +882,14 @@ void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  3789. return;
  3790. if (!async) {
  3791. - int cpu = get_cpu();
  3792. + int cpu = get_cpu_light();
  3793. if (cpumask_test_cpu(cpu, hctx->cpumask)) {
  3794. __blk_mq_run_hw_queue(hctx);
  3795. - put_cpu();
  3796. + put_cpu_light();
  3797. return;
  3798. }
  3799. - put_cpu();
  3800. + put_cpu_light();
  3801. }
  3802. kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
  3803. @@ -1616,7 +1636,7 @@ static int blk_mq_hctx_notify(void *data, unsigned long action,
  3804. {
  3805. struct blk_mq_hw_ctx *hctx = data;
  3806. - if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
  3807. + if (action == CPU_POST_DEAD)
  3808. return blk_mq_hctx_cpu_offline(hctx, cpu);
  3809. /*
  3810. diff --git a/block/blk-mq.h b/block/blk-mq.h
  3811. index 713820b47b31..3cb6feb4fe23 100644
  3812. --- a/block/blk-mq.h
  3813. +++ b/block/blk-mq.h
  3814. @@ -74,7 +74,10 @@ struct blk_align_bitmap {
  3815. static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
  3816. unsigned int cpu)
  3817. {
  3818. - return per_cpu_ptr(q->queue_ctx, cpu);
  3819. + struct blk_mq_ctx *ctx;
  3820. +
  3821. + ctx = per_cpu_ptr(q->queue_ctx, cpu);
  3822. + return ctx;
  3823. }
  3824. /*
  3825. @@ -85,12 +88,12 @@ static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
  3826. */
  3827. static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
  3828. {
  3829. - return __blk_mq_get_ctx(q, get_cpu());
  3830. + return __blk_mq_get_ctx(q, get_cpu_light());
  3831. }
  3832. static inline void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
  3833. {
  3834. - put_cpu();
  3835. + put_cpu_light();
  3836. }
  3837. struct blk_mq_alloc_data {
  3838. diff --git a/block/blk-softirq.c b/block/blk-softirq.c
  3839. index 53b1737e978d..81c3c0a62edf 100644
  3840. --- a/block/blk-softirq.c
  3841. +++ b/block/blk-softirq.c
  3842. @@ -51,6 +51,7 @@ static void trigger_softirq(void *data)
  3843. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  3844. local_irq_restore(flags);
  3845. + preempt_check_resched_rt();
  3846. }
  3847. /*
  3848. @@ -93,6 +94,7 @@ static int blk_cpu_notify(struct notifier_block *self, unsigned long action,
  3849. this_cpu_ptr(&blk_cpu_done));
  3850. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  3851. local_irq_enable();
  3852. + preempt_check_resched_rt();
  3853. }
  3854. return NOTIFY_OK;
  3855. @@ -150,6 +152,7 @@ void __blk_complete_request(struct request *req)
  3856. goto do_local;
  3857. local_irq_restore(flags);
  3858. + preempt_check_resched_rt();
  3859. }
  3860. /**
  3861. diff --git a/block/bounce.c b/block/bounce.c
  3862. index 1cb5dd3a5da1..2f1ec8a67cbe 100644
  3863. --- a/block/bounce.c
  3864. +++ b/block/bounce.c
  3865. @@ -55,11 +55,11 @@ static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
  3866. unsigned long flags;
  3867. unsigned char *vto;
  3868. - local_irq_save(flags);
  3869. + local_irq_save_nort(flags);
  3870. vto = kmap_atomic(to->bv_page);
  3871. memcpy(vto + to->bv_offset, vfrom, to->bv_len);
  3872. kunmap_atomic(vto);
  3873. - local_irq_restore(flags);
  3874. + local_irq_restore_nort(flags);
  3875. }
  3876. #else /* CONFIG_HIGHMEM */
  3877. diff --git a/crypto/algapi.c b/crypto/algapi.c
  3878. index eb58b73ca925..ee228adf2dac 100644
  3879. --- a/crypto/algapi.c
  3880. +++ b/crypto/algapi.c
  3881. @@ -732,13 +732,13 @@ EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
  3882. int crypto_register_notifier(struct notifier_block *nb)
  3883. {
  3884. - return blocking_notifier_chain_register(&crypto_chain, nb);
  3885. + return srcu_notifier_chain_register(&crypto_chain, nb);
  3886. }
  3887. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  3888. int crypto_unregister_notifier(struct notifier_block *nb)
  3889. {
  3890. - return blocking_notifier_chain_unregister(&crypto_chain, nb);
  3891. + return srcu_notifier_chain_unregister(&crypto_chain, nb);
  3892. }
  3893. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  3894. diff --git a/crypto/api.c b/crypto/api.c
  3895. index bbc147cb5dec..bc1a848f02ec 100644
  3896. --- a/crypto/api.c
  3897. +++ b/crypto/api.c
  3898. @@ -31,7 +31,7 @@ EXPORT_SYMBOL_GPL(crypto_alg_list);
  3899. DECLARE_RWSEM(crypto_alg_sem);
  3900. EXPORT_SYMBOL_GPL(crypto_alg_sem);
  3901. -BLOCKING_NOTIFIER_HEAD(crypto_chain);
  3902. +SRCU_NOTIFIER_HEAD(crypto_chain);
  3903. EXPORT_SYMBOL_GPL(crypto_chain);
  3904. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg);
  3905. @@ -236,10 +236,10 @@ int crypto_probing_notify(unsigned long val, void *v)
  3906. {
  3907. int ok;
  3908. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  3909. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  3910. if (ok == NOTIFY_DONE) {
  3911. request_module("cryptomgr");
  3912. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  3913. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  3914. }
  3915. return ok;
  3916. diff --git a/crypto/internal.h b/crypto/internal.h
  3917. index 00e42a3ed814..2e85551e235f 100644
  3918. --- a/crypto/internal.h
  3919. +++ b/crypto/internal.h
  3920. @@ -47,7 +47,7 @@ struct crypto_larval {
  3921. extern struct list_head crypto_alg_list;
  3922. extern struct rw_semaphore crypto_alg_sem;
  3923. -extern struct blocking_notifier_head crypto_chain;
  3924. +extern struct srcu_notifier_head crypto_chain;
  3925. #ifdef CONFIG_PROC_FS
  3926. void __init crypto_init_proc(void);
  3927. @@ -143,7 +143,7 @@ static inline int crypto_is_moribund(struct crypto_alg *alg)
  3928. static inline void crypto_notify(unsigned long val, void *v)
  3929. {
  3930. - blocking_notifier_call_chain(&crypto_chain, val, v);
  3931. + srcu_notifier_call_chain(&crypto_chain, val, v);
  3932. }
  3933. #endif /* _CRYPTO_INTERNAL_H */
  3934. diff --git a/drivers/acpi/acpica/acglobal.h b/drivers/acpi/acpica/acglobal.h
  3935. index faa97604d878..941497f31cf0 100644
  3936. --- a/drivers/acpi/acpica/acglobal.h
  3937. +++ b/drivers/acpi/acpica/acglobal.h
  3938. @@ -116,7 +116,7 @@ ACPI_GLOBAL(u8, acpi_gbl_global_lock_pending);
  3939. * interrupt level
  3940. */
  3941. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_gpe_lock); /* For GPE data structs and registers */
  3942. -ACPI_GLOBAL(acpi_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  3943. +ACPI_GLOBAL(acpi_raw_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  3944. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_reference_count_lock);
  3945. /* Mutex for _OSI support */
  3946. diff --git a/drivers/acpi/acpica/hwregs.c b/drivers/acpi/acpica/hwregs.c
  3947. index 3cf77afd142c..dc32e72132f1 100644
  3948. --- a/drivers/acpi/acpica/hwregs.c
  3949. +++ b/drivers/acpi/acpica/hwregs.c
  3950. @@ -269,14 +269,14 @@ acpi_status acpi_hw_clear_acpi_status(void)
  3951. ACPI_BITMASK_ALL_FIXED_STATUS,
  3952. ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status.address)));
  3953. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  3954. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  3955. /* Clear the fixed events in PM1 A/B */
  3956. status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
  3957. ACPI_BITMASK_ALL_FIXED_STATUS);
  3958. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  3959. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  3960. if (ACPI_FAILURE(status)) {
  3961. goto exit;
  3962. diff --git a/drivers/acpi/acpica/hwxface.c b/drivers/acpi/acpica/hwxface.c
  3963. index b2e50d8007fe..ff007084dc48 100644
  3964. --- a/drivers/acpi/acpica/hwxface.c
  3965. +++ b/drivers/acpi/acpica/hwxface.c
  3966. @@ -374,7 +374,7 @@ acpi_status acpi_write_bit_register(u32 register_id, u32 value)
  3967. return_ACPI_STATUS(AE_BAD_PARAMETER);
  3968. }
  3969. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  3970. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  3971. /*
  3972. * At this point, we know that the parent register is one of the
  3973. @@ -435,7 +435,7 @@ acpi_status acpi_write_bit_register(u32 register_id, u32 value)
  3974. unlock_and_exit:
  3975. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  3976. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  3977. return_ACPI_STATUS(status);
  3978. }
  3979. diff --git a/drivers/acpi/acpica/utmutex.c b/drivers/acpi/acpica/utmutex.c
  3980. index ce406e39b669..41a75eb3ae9d 100644
  3981. --- a/drivers/acpi/acpica/utmutex.c
  3982. +++ b/drivers/acpi/acpica/utmutex.c
  3983. @@ -88,7 +88,7 @@ acpi_status acpi_ut_mutex_initialize(void)
  3984. return_ACPI_STATUS (status);
  3985. }
  3986. - status = acpi_os_create_lock (&acpi_gbl_hardware_lock);
  3987. + status = acpi_os_create_raw_lock (&acpi_gbl_hardware_lock);
  3988. if (ACPI_FAILURE (status)) {
  3989. return_ACPI_STATUS (status);
  3990. }
  3991. @@ -156,7 +156,7 @@ void acpi_ut_mutex_terminate(void)
  3992. /* Delete the spinlocks */
  3993. acpi_os_delete_lock(acpi_gbl_gpe_lock);
  3994. - acpi_os_delete_lock(acpi_gbl_hardware_lock);
  3995. + acpi_os_delete_raw_lock(acpi_gbl_hardware_lock);
  3996. acpi_os_delete_lock(acpi_gbl_reference_count_lock);
  3997. /* Delete the reader/writer lock */
  3998. diff --git a/drivers/ata/libata-sff.c b/drivers/ata/libata-sff.c
  3999. index 18de4c457068..dfecf14df732 100644
  4000. --- a/drivers/ata/libata-sff.c
  4001. +++ b/drivers/ata/libata-sff.c
  4002. @@ -678,9 +678,9 @@ unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
  4003. unsigned long flags;
  4004. unsigned int consumed;
  4005. - local_irq_save(flags);
  4006. + local_irq_save_nort(flags);
  4007. consumed = ata_sff_data_xfer32(dev, buf, buflen, rw);
  4008. - local_irq_restore(flags);
  4009. + local_irq_restore_nort(flags);
  4010. return consumed;
  4011. }
  4012. @@ -719,7 +719,7 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
  4013. unsigned long flags;
  4014. /* FIXME: use a bounce buffer */
  4015. - local_irq_save(flags);
  4016. + local_irq_save_nort(flags);
  4017. buf = kmap_atomic(page);
  4018. /* do the actual data transfer */
  4019. @@ -727,7 +727,7 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
  4020. do_write);
  4021. kunmap_atomic(buf);
  4022. - local_irq_restore(flags);
  4023. + local_irq_restore_nort(flags);
  4024. } else {
  4025. buf = page_address(page);
  4026. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  4027. @@ -864,7 +864,7 @@ static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  4028. unsigned long flags;
  4029. /* FIXME: use bounce buffer */
  4030. - local_irq_save(flags);
  4031. + local_irq_save_nort(flags);
  4032. buf = kmap_atomic(page);
  4033. /* do the actual data transfer */
  4034. @@ -872,7 +872,7 @@ static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  4035. count, rw);
  4036. kunmap_atomic(buf);
  4037. - local_irq_restore(flags);
  4038. + local_irq_restore_nort(flags);
  4039. } else {
  4040. buf = page_address(page);
  4041. consumed = ap->ops->sff_data_xfer(dev, buf + offset,
  4042. diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
  4043. index 502406c9e6e1..91d909148688 100644
  4044. --- a/drivers/block/zram/zram_drv.c
  4045. +++ b/drivers/block/zram/zram_drv.c
  4046. @@ -520,6 +520,8 @@ static struct zram_meta *zram_meta_alloc(char *pool_name, u64 disksize)
  4047. goto out_error;
  4048. }
  4049. + zram_meta_init_table_locks(meta, disksize);
  4050. +
  4051. return meta;
  4052. out_error:
  4053. @@ -568,12 +570,12 @@ static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
  4054. unsigned long handle;
  4055. size_t size;
  4056. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4057. + zram_lock_table(&meta->table[index]);
  4058. handle = meta->table[index].handle;
  4059. size = zram_get_obj_size(meta, index);
  4060. if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
  4061. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4062. + zram_unlock_table(&meta->table[index]);
  4063. memset(mem, 0, PAGE_SIZE);
  4064. return 0;
  4065. }
  4066. @@ -584,7 +586,7 @@ static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
  4067. else
  4068. ret = zcomp_decompress(zram->comp, cmem, size, mem);
  4069. zs_unmap_object(meta->mem_pool, handle);
  4070. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4071. + zram_unlock_table(&meta->table[index]);
  4072. /* Should NEVER happen. Return bio error if it does. */
  4073. if (unlikely(ret)) {
  4074. @@ -604,14 +606,14 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
  4075. struct zram_meta *meta = zram->meta;
  4076. page = bvec->bv_page;
  4077. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4078. + zram_lock_table(&meta->table[index]);
  4079. if (unlikely(!meta->table[index].handle) ||
  4080. zram_test_flag(meta, index, ZRAM_ZERO)) {
  4081. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4082. + zram_unlock_table(&meta->table[index]);
  4083. handle_zero_page(bvec);
  4084. return 0;
  4085. }
  4086. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4087. + zram_unlock_table(&meta->table[index]);
  4088. if (is_partial_io(bvec))
  4089. /* Use a temporary buffer to decompress the page */
  4090. @@ -689,10 +691,10 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
  4091. if (user_mem)
  4092. kunmap_atomic(user_mem);
  4093. /* Free memory associated with this sector now. */
  4094. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4095. + zram_lock_table(&meta->table[index]);
  4096. zram_free_page(zram, index);
  4097. zram_set_flag(meta, index, ZRAM_ZERO);
  4098. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4099. + zram_unlock_table(&meta->table[index]);
  4100. atomic64_inc(&zram->stats.zero_pages);
  4101. ret = 0;
  4102. @@ -752,12 +754,12 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
  4103. * Free memory associated with this sector
  4104. * before overwriting unused sectors.
  4105. */
  4106. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4107. + zram_lock_table(&meta->table[index]);
  4108. zram_free_page(zram, index);
  4109. meta->table[index].handle = handle;
  4110. zram_set_obj_size(meta, index, clen);
  4111. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4112. + zram_unlock_table(&meta->table[index]);
  4113. /* Update stats */
  4114. atomic64_add(clen, &zram->stats.compr_data_size);
  4115. @@ -800,9 +802,9 @@ static void zram_bio_discard(struct zram *zram, u32 index,
  4116. }
  4117. while (n >= PAGE_SIZE) {
  4118. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4119. + zram_lock_table(&meta->table[index]);
  4120. zram_free_page(zram, index);
  4121. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4122. + zram_unlock_table(&meta->table[index]);
  4123. atomic64_inc(&zram->stats.notify_free);
  4124. index++;
  4125. n -= PAGE_SIZE;
  4126. @@ -928,9 +930,9 @@ static void zram_slot_free_notify(struct block_device *bdev,
  4127. zram = bdev->bd_disk->private_data;
  4128. meta = zram->meta;
  4129. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4130. + zram_lock_table(&meta->table[index]);
  4131. zram_free_page(zram, index);
  4132. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4133. + zram_unlock_table(&meta->table[index]);
  4134. atomic64_inc(&zram->stats.notify_free);
  4135. }
  4136. diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h
  4137. index 8e92339686d7..9e3e953d680e 100644
  4138. --- a/drivers/block/zram/zram_drv.h
  4139. +++ b/drivers/block/zram/zram_drv.h
  4140. @@ -72,6 +72,9 @@ enum zram_pageflags {
  4141. struct zram_table_entry {
  4142. unsigned long handle;
  4143. unsigned long value;
  4144. +#ifdef CONFIG_PREEMPT_RT_BASE
  4145. + spinlock_t lock;
  4146. +#endif
  4147. };
  4148. struct zram_stats {
  4149. @@ -119,4 +122,42 @@ struct zram {
  4150. */
  4151. bool claim; /* Protected by bdev->bd_mutex */
  4152. };
  4153. +
  4154. +#ifndef CONFIG_PREEMPT_RT_BASE
  4155. +static inline void zram_lock_table(struct zram_table_entry *table)
  4156. +{
  4157. + bit_spin_lock(ZRAM_ACCESS, &table->value);
  4158. +}
  4159. +
  4160. +static inline void zram_unlock_table(struct zram_table_entry *table)
  4161. +{
  4162. + bit_spin_unlock(ZRAM_ACCESS, &table->value);
  4163. +}
  4164. +
  4165. +static inline void zram_meta_init_table_locks(struct zram_meta *meta, u64 disksize) { }
  4166. +#else /* CONFIG_PREEMPT_RT_BASE */
  4167. +static inline void zram_lock_table(struct zram_table_entry *table)
  4168. +{
  4169. + spin_lock(&table->lock);
  4170. + __set_bit(ZRAM_ACCESS, &table->value);
  4171. +}
  4172. +
  4173. +static inline void zram_unlock_table(struct zram_table_entry *table)
  4174. +{
  4175. + __clear_bit(ZRAM_ACCESS, &table->value);
  4176. + spin_unlock(&table->lock);
  4177. +}
  4178. +
  4179. +static inline void zram_meta_init_table_locks(struct zram_meta *meta, u64 disksize)
  4180. +{
  4181. + size_t num_pages = disksize >> PAGE_SHIFT;
  4182. + size_t index;
  4183. +
  4184. + for (index = 0; index < num_pages; index++) {
  4185. + spinlock_t *lock = &meta->table[index].lock;
  4186. + spin_lock_init(lock);
  4187. + }
  4188. +}
  4189. +#endif /* CONFIG_PREEMPT_RT_BASE */
  4190. +
  4191. #endif
  4192. diff --git a/drivers/char/random.c b/drivers/char/random.c
  4193. index 1822472dffab..46c0e27cf27f 100644
  4194. --- a/drivers/char/random.c
  4195. +++ b/drivers/char/random.c
  4196. @@ -260,6 +260,7 @@
  4197. #include <linux/irq.h>
  4198. #include <linux/syscalls.h>
  4199. #include <linux/completion.h>
  4200. +#include <linux/locallock.h>
  4201. #include <asm/processor.h>
  4202. #include <asm/uaccess.h>
  4203. @@ -799,8 +800,6 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
  4204. } sample;
  4205. long delta, delta2, delta3;
  4206. - preempt_disable();
  4207. -
  4208. sample.jiffies = jiffies;
  4209. sample.cycles = random_get_entropy();
  4210. sample.num = num;
  4211. @@ -841,7 +840,6 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
  4212. */
  4213. credit_entropy_bits(r, min_t(int, fls(delta>>1), 11));
  4214. }
  4215. - preempt_enable();
  4216. }
  4217. void add_input_randomness(unsigned int type, unsigned int code,
  4218. @@ -894,28 +892,27 @@ static __u32 get_reg(struct fast_pool *f, struct pt_regs *regs)
  4219. return *(ptr + f->reg_idx++);
  4220. }
  4221. -void add_interrupt_randomness(int irq, int irq_flags)
  4222. +void add_interrupt_randomness(int irq, int irq_flags, __u64 ip)
  4223. {
  4224. struct entropy_store *r;
  4225. struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness);
  4226. - struct pt_regs *regs = get_irq_regs();
  4227. unsigned long now = jiffies;
  4228. cycles_t cycles = random_get_entropy();
  4229. __u32 c_high, j_high;
  4230. - __u64 ip;
  4231. unsigned long seed;
  4232. int credit = 0;
  4233. if (cycles == 0)
  4234. - cycles = get_reg(fast_pool, regs);
  4235. + cycles = get_reg(fast_pool, NULL);
  4236. c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0;
  4237. j_high = (sizeof(now) > 4) ? now >> 32 : 0;
  4238. fast_pool->pool[0] ^= cycles ^ j_high ^ irq;
  4239. fast_pool->pool[1] ^= now ^ c_high;
  4240. - ip = regs ? instruction_pointer(regs) : _RET_IP_;
  4241. + if (!ip)
  4242. + ip = _RET_IP_;
  4243. fast_pool->pool[2] ^= ip;
  4244. fast_pool->pool[3] ^= (sizeof(ip) > 4) ? ip >> 32 :
  4245. - get_reg(fast_pool, regs);
  4246. + get_reg(fast_pool, NULL);
  4247. fast_mix(fast_pool);
  4248. add_interrupt_bench(cycles);
  4249. @@ -1800,6 +1797,7 @@ int random_int_secret_init(void)
  4250. static DEFINE_PER_CPU(__u32 [MD5_DIGEST_WORDS], get_random_int_hash)
  4251. __aligned(sizeof(unsigned long));
  4252. +static DEFINE_LOCAL_IRQ_LOCK(hash_entropy_int_lock);
  4253. /*
  4254. * Get a random word for internal kernel use only. Similar to urandom but
  4255. @@ -1815,12 +1813,12 @@ unsigned int get_random_int(void)
  4256. if (arch_get_random_int(&ret))
  4257. return ret;
  4258. - hash = get_cpu_var(get_random_int_hash);
  4259. + hash = get_locked_var(hash_entropy_int_lock, get_random_int_hash);
  4260. hash[0] += current->pid + jiffies + random_get_entropy();
  4261. md5_transform(hash, random_int_secret);
  4262. ret = hash[0];
  4263. - put_cpu_var(get_random_int_hash);
  4264. + put_locked_var(hash_entropy_int_lock, get_random_int_hash);
  4265. return ret;
  4266. }
  4267. @@ -1837,12 +1835,12 @@ unsigned long get_random_long(void)
  4268. if (arch_get_random_long(&ret))
  4269. return ret;
  4270. - hash = get_cpu_var(get_random_int_hash);
  4271. + hash = get_locked_var(hash_entropy_int_lock, get_random_int_hash);
  4272. hash[0] += current->pid + jiffies + random_get_entropy();
  4273. md5_transform(hash, random_int_secret);
  4274. ret = *(unsigned long *)hash;
  4275. - put_cpu_var(get_random_int_hash);
  4276. + put_locked_var(hash_entropy_int_lock, get_random_int_hash);
  4277. return ret;
  4278. }
  4279. diff --git a/drivers/clk/at91/clk-generated.c b/drivers/clk/at91/clk-generated.c
  4280. index abc80949e1dd..4ad3298eb372 100644
  4281. --- a/drivers/clk/at91/clk-generated.c
  4282. +++ b/drivers/clk/at91/clk-generated.c
  4283. @@ -15,8 +15,8 @@
  4284. #include <linux/clkdev.h>
  4285. #include <linux/clk/at91_pmc.h>
  4286. #include <linux/of.h>
  4287. -#include <linux/of_address.h>
  4288. -#include <linux/io.h>
  4289. +#include <linux/mfd/syscon.h>
  4290. +#include <linux/regmap.h>
  4291. #include "pmc.h"
  4292. @@ -28,8 +28,9 @@
  4293. struct clk_generated {
  4294. struct clk_hw hw;
  4295. - struct at91_pmc *pmc;
  4296. + struct regmap *regmap;
  4297. struct clk_range range;
  4298. + spinlock_t *lock;
  4299. u32 id;
  4300. u32 gckdiv;
  4301. u8 parent_id;
  4302. @@ -41,49 +42,52 @@ struct clk_generated {
  4303. static int clk_generated_enable(struct clk_hw *hw)
  4304. {
  4305. struct clk_generated *gck = to_clk_generated(hw);
  4306. - struct at91_pmc *pmc = gck->pmc;
  4307. - u32 tmp;
  4308. + unsigned long flags;
  4309. pr_debug("GCLK: %s, gckdiv = %d, parent id = %d\n",
  4310. __func__, gck->gckdiv, gck->parent_id);
  4311. - pmc_lock(pmc);
  4312. - pmc_write(pmc, AT91_PMC_PCR, (gck->id & AT91_PMC_PCR_PID_MASK));
  4313. - tmp = pmc_read(pmc, AT91_PMC_PCR) &
  4314. - ~(AT91_PMC_PCR_GCKDIV_MASK | AT91_PMC_PCR_GCKCSS_MASK);
  4315. - pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_GCKCSS(gck->parent_id)
  4316. - | AT91_PMC_PCR_CMD
  4317. - | AT91_PMC_PCR_GCKDIV(gck->gckdiv)
  4318. - | AT91_PMC_PCR_GCKEN);
  4319. - pmc_unlock(pmc);
  4320. + spin_lock_irqsave(gck->lock, flags);
  4321. + regmap_write(gck->regmap, AT91_PMC_PCR,
  4322. + (gck->id & AT91_PMC_PCR_PID_MASK));
  4323. + regmap_update_bits(gck->regmap, AT91_PMC_PCR,
  4324. + AT91_PMC_PCR_GCKDIV_MASK | AT91_PMC_PCR_GCKCSS_MASK |
  4325. + AT91_PMC_PCR_CMD | AT91_PMC_PCR_GCKEN,
  4326. + AT91_PMC_PCR_GCKCSS(gck->parent_id) |
  4327. + AT91_PMC_PCR_CMD |
  4328. + AT91_PMC_PCR_GCKDIV(gck->gckdiv) |
  4329. + AT91_PMC_PCR_GCKEN);
  4330. + spin_unlock_irqrestore(gck->lock, flags);
  4331. return 0;
  4332. }
  4333. static void clk_generated_disable(struct clk_hw *hw)
  4334. {
  4335. struct clk_generated *gck = to_clk_generated(hw);
  4336. - struct at91_pmc *pmc = gck->pmc;
  4337. - u32 tmp;
  4338. -
  4339. - pmc_lock(pmc);
  4340. - pmc_write(pmc, AT91_PMC_PCR, (gck->id & AT91_PMC_PCR_PID_MASK));
  4341. - tmp = pmc_read(pmc, AT91_PMC_PCR) & ~AT91_PMC_PCR_GCKEN;
  4342. - pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_CMD);
  4343. - pmc_unlock(pmc);
  4344. + unsigned long flags;
  4345. +
  4346. + spin_lock_irqsave(gck->lock, flags);
  4347. + regmap_write(gck->regmap, AT91_PMC_PCR,
  4348. + (gck->id & AT91_PMC_PCR_PID_MASK));
  4349. + regmap_update_bits(gck->regmap, AT91_PMC_PCR,
  4350. + AT91_PMC_PCR_CMD | AT91_PMC_PCR_GCKEN,
  4351. + AT91_PMC_PCR_CMD);
  4352. + spin_unlock_irqrestore(gck->lock, flags);
  4353. }
  4354. static int clk_generated_is_enabled(struct clk_hw *hw)
  4355. {
  4356. struct clk_generated *gck = to_clk_generated(hw);
  4357. - struct at91_pmc *pmc = gck->pmc;
  4358. - int ret;
  4359. + unsigned long flags;
  4360. + unsigned int status;
  4361. - pmc_lock(pmc);
  4362. - pmc_write(pmc, AT91_PMC_PCR, (gck->id & AT91_PMC_PCR_PID_MASK));
  4363. - ret = !!(pmc_read(pmc, AT91_PMC_PCR) & AT91_PMC_PCR_GCKEN);
  4364. - pmc_unlock(pmc);
  4365. + spin_lock_irqsave(gck->lock, flags);
  4366. + regmap_write(gck->regmap, AT91_PMC_PCR,
  4367. + (gck->id & AT91_PMC_PCR_PID_MASK));
  4368. + regmap_read(gck->regmap, AT91_PMC_PCR, &status);
  4369. + spin_unlock_irqrestore(gck->lock, flags);
  4370. - return ret;
  4371. + return status & AT91_PMC_PCR_GCKEN ? 1 : 0;
  4372. }
  4373. static unsigned long
  4374. @@ -214,13 +218,14 @@ static const struct clk_ops generated_ops = {
  4375. */
  4376. static void clk_generated_startup(struct clk_generated *gck)
  4377. {
  4378. - struct at91_pmc *pmc = gck->pmc;
  4379. u32 tmp;
  4380. + unsigned long flags;
  4381. - pmc_lock(pmc);
  4382. - pmc_write(pmc, AT91_PMC_PCR, (gck->id & AT91_PMC_PCR_PID_MASK));
  4383. - tmp = pmc_read(pmc, AT91_PMC_PCR);
  4384. - pmc_unlock(pmc);
  4385. + spin_lock_irqsave(gck->lock, flags);
  4386. + regmap_write(gck->regmap, AT91_PMC_PCR,
  4387. + (gck->id & AT91_PMC_PCR_PID_MASK));
  4388. + regmap_read(gck->regmap, AT91_PMC_PCR, &tmp);
  4389. + spin_unlock_irqrestore(gck->lock, flags);
  4390. gck->parent_id = (tmp & AT91_PMC_PCR_GCKCSS_MASK)
  4391. >> AT91_PMC_PCR_GCKCSS_OFFSET;
  4392. @@ -229,8 +234,8 @@ static void clk_generated_startup(struct clk_generated *gck)
  4393. }
  4394. static struct clk * __init
  4395. -at91_clk_register_generated(struct at91_pmc *pmc, const char *name,
  4396. - const char **parent_names, u8 num_parents,
  4397. +at91_clk_register_generated(struct regmap *regmap, spinlock_t *lock, const char
  4398. + *name, const char **parent_names, u8 num_parents,
  4399. u8 id, const struct clk_range *range)
  4400. {
  4401. struct clk_generated *gck;
  4402. @@ -249,7 +254,8 @@ at91_clk_register_generated(struct at91_pmc *pmc, const char *name,
  4403. gck->id = id;
  4404. gck->hw.init = &init;
  4405. - gck->pmc = pmc;
  4406. + gck->regmap = regmap;
  4407. + gck->lock = lock;
  4408. gck->range = *range;
  4409. clk = clk_register(NULL, &gck->hw);
  4410. @@ -261,8 +267,7 @@ at91_clk_register_generated(struct at91_pmc *pmc, const char *name,
  4411. return clk;
  4412. }
  4413. -void __init of_sama5d2_clk_generated_setup(struct device_node *np,
  4414. - struct at91_pmc *pmc)
  4415. +void __init of_sama5d2_clk_generated_setup(struct device_node *np)
  4416. {
  4417. int num;
  4418. u32 id;
  4419. @@ -272,6 +277,7 @@ void __init of_sama5d2_clk_generated_setup(struct device_node *np,
  4420. const char *parent_names[GENERATED_SOURCE_MAX];
  4421. struct device_node *gcknp;
  4422. struct clk_range range = CLK_RANGE(0, 0);
  4423. + struct regmap *regmap;
  4424. num_parents = of_clk_get_parent_count(np);
  4425. if (num_parents <= 0 || num_parents > GENERATED_SOURCE_MAX)
  4426. @@ -283,6 +289,10 @@ void __init of_sama5d2_clk_generated_setup(struct device_node *np,
  4427. if (!num || num > PERIPHERAL_MAX)
  4428. return;
  4429. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4430. + if (IS_ERR(regmap))
  4431. + return;
  4432. +
  4433. for_each_child_of_node(np, gcknp) {
  4434. if (of_property_read_u32(gcknp, "reg", &id))
  4435. continue;
  4436. @@ -296,11 +306,14 @@ void __init of_sama5d2_clk_generated_setup(struct device_node *np,
  4437. of_at91_get_clk_range(gcknp, "atmel,clk-output-range",
  4438. &range);
  4439. - clk = at91_clk_register_generated(pmc, name, parent_names,
  4440. - num_parents, id, &range);
  4441. + clk = at91_clk_register_generated(regmap, &pmc_pcr_lock, name,
  4442. + parent_names, num_parents,
  4443. + id, &range);
  4444. if (IS_ERR(clk))
  4445. continue;
  4446. of_clk_add_provider(gcknp, of_clk_src_simple_get, clk);
  4447. }
  4448. }
  4449. +CLK_OF_DECLARE(of_sama5d2_clk_generated_setup, "atmel,sama5d2-clk-generated",
  4450. + of_sama5d2_clk_generated_setup);
  4451. diff --git a/drivers/clk/at91/clk-h32mx.c b/drivers/clk/at91/clk-h32mx.c
  4452. index a165230e7eda..8e20c8a76db7 100644
  4453. --- a/drivers/clk/at91/clk-h32mx.c
  4454. +++ b/drivers/clk/at91/clk-h32mx.c
  4455. @@ -15,15 +15,9 @@
  4456. #include <linux/clk-provider.h>
  4457. #include <linux/clkdev.h>
  4458. #include <linux/clk/at91_pmc.h>
  4459. -#include <linux/delay.h>
  4460. #include <linux/of.h>
  4461. -#include <linux/of_address.h>
  4462. -#include <linux/of_irq.h>
  4463. -#include <linux/io.h>
  4464. -#include <linux/interrupt.h>
  4465. -#include <linux/irq.h>
  4466. -#include <linux/sched.h>
  4467. -#include <linux/wait.h>
  4468. +#include <linux/regmap.h>
  4469. +#include <linux/mfd/syscon.h>
  4470. #include "pmc.h"
  4471. @@ -31,7 +25,7 @@
  4472. struct clk_sama5d4_h32mx {
  4473. struct clk_hw hw;
  4474. - struct at91_pmc *pmc;
  4475. + struct regmap *regmap;
  4476. };
  4477. #define to_clk_sama5d4_h32mx(hw) container_of(hw, struct clk_sama5d4_h32mx, hw)
  4478. @@ -40,8 +34,10 @@ static unsigned long clk_sama5d4_h32mx_recalc_rate(struct clk_hw *hw,
  4479. unsigned long parent_rate)
  4480. {
  4481. struct clk_sama5d4_h32mx *h32mxclk = to_clk_sama5d4_h32mx(hw);
  4482. + unsigned int mckr;
  4483. - if (pmc_read(h32mxclk->pmc, AT91_PMC_MCKR) & AT91_PMC_H32MXDIV)
  4484. + regmap_read(h32mxclk->regmap, AT91_PMC_MCKR, &mckr);
  4485. + if (mckr & AT91_PMC_H32MXDIV)
  4486. return parent_rate / 2;
  4487. if (parent_rate > H32MX_MAX_FREQ)
  4488. @@ -70,18 +66,16 @@ static int clk_sama5d4_h32mx_set_rate(struct clk_hw *hw, unsigned long rate,
  4489. unsigned long parent_rate)
  4490. {
  4491. struct clk_sama5d4_h32mx *h32mxclk = to_clk_sama5d4_h32mx(hw);
  4492. - struct at91_pmc *pmc = h32mxclk->pmc;
  4493. - u32 tmp;
  4494. + u32 mckr = 0;
  4495. if (parent_rate != rate && (parent_rate / 2) != rate)
  4496. return -EINVAL;
  4497. - pmc_lock(pmc);
  4498. - tmp = pmc_read(pmc, AT91_PMC_MCKR) & ~AT91_PMC_H32MXDIV;
  4499. if ((parent_rate / 2) == rate)
  4500. - tmp |= AT91_PMC_H32MXDIV;
  4501. - pmc_write(pmc, AT91_PMC_MCKR, tmp);
  4502. - pmc_unlock(pmc);
  4503. + mckr = AT91_PMC_H32MXDIV;
  4504. +
  4505. + regmap_update_bits(h32mxclk->regmap, AT91_PMC_MCKR,
  4506. + AT91_PMC_H32MXDIV, mckr);
  4507. return 0;
  4508. }
  4509. @@ -92,14 +86,18 @@ static const struct clk_ops h32mx_ops = {
  4510. .set_rate = clk_sama5d4_h32mx_set_rate,
  4511. };
  4512. -void __init of_sama5d4_clk_h32mx_setup(struct device_node *np,
  4513. - struct at91_pmc *pmc)
  4514. +static void __init of_sama5d4_clk_h32mx_setup(struct device_node *np)
  4515. {
  4516. struct clk_sama5d4_h32mx *h32mxclk;
  4517. struct clk_init_data init;
  4518. const char *parent_name;
  4519. + struct regmap *regmap;
  4520. struct clk *clk;
  4521. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4522. + if (IS_ERR(regmap))
  4523. + return;
  4524. +
  4525. h32mxclk = kzalloc(sizeof(*h32mxclk), GFP_KERNEL);
  4526. if (!h32mxclk)
  4527. return;
  4528. @@ -113,7 +111,7 @@ void __init of_sama5d4_clk_h32mx_setup(struct device_node *np,
  4529. init.flags = CLK_SET_RATE_GATE;
  4530. h32mxclk->hw.init = &init;
  4531. - h32mxclk->pmc = pmc;
  4532. + h32mxclk->regmap = regmap;
  4533. clk = clk_register(NULL, &h32mxclk->hw);
  4534. if (IS_ERR(clk)) {
  4535. @@ -123,3 +121,5 @@ void __init of_sama5d4_clk_h32mx_setup(struct device_node *np,
  4536. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4537. }
  4538. +CLK_OF_DECLARE(of_sama5d4_clk_h32mx_setup, "atmel,sama5d4-clk-h32mx",
  4539. + of_sama5d4_clk_h32mx_setup);
  4540. diff --git a/drivers/clk/at91/clk-main.c b/drivers/clk/at91/clk-main.c
  4541. index fd7247deabdc..4bfc94d6c26e 100644
  4542. --- a/drivers/clk/at91/clk-main.c
  4543. +++ b/drivers/clk/at91/clk-main.c
  4544. @@ -13,13 +13,8 @@
  4545. #include <linux/clk/at91_pmc.h>
  4546. #include <linux/delay.h>
  4547. #include <linux/of.h>
  4548. -#include <linux/of_address.h>
  4549. -#include <linux/of_irq.h>
  4550. -#include <linux/io.h>
  4551. -#include <linux/interrupt.h>
  4552. -#include <linux/irq.h>
  4553. -#include <linux/sched.h>
  4554. -#include <linux/wait.h>
  4555. +#include <linux/mfd/syscon.h>
  4556. +#include <linux/regmap.h>
  4557. #include "pmc.h"
  4558. @@ -34,18 +29,14 @@
  4559. struct clk_main_osc {
  4560. struct clk_hw hw;
  4561. - struct at91_pmc *pmc;
  4562. - unsigned int irq;
  4563. - wait_queue_head_t wait;
  4564. + struct regmap *regmap;
  4565. };
  4566. #define to_clk_main_osc(hw) container_of(hw, struct clk_main_osc, hw)
  4567. struct clk_main_rc_osc {
  4568. struct clk_hw hw;
  4569. - struct at91_pmc *pmc;
  4570. - unsigned int irq;
  4571. - wait_queue_head_t wait;
  4572. + struct regmap *regmap;
  4573. unsigned long frequency;
  4574. unsigned long accuracy;
  4575. };
  4576. @@ -54,51 +45,47 @@ struct clk_main_rc_osc {
  4577. struct clk_rm9200_main {
  4578. struct clk_hw hw;
  4579. - struct at91_pmc *pmc;
  4580. + struct regmap *regmap;
  4581. };
  4582. #define to_clk_rm9200_main(hw) container_of(hw, struct clk_rm9200_main, hw)
  4583. struct clk_sam9x5_main {
  4584. struct clk_hw hw;
  4585. - struct at91_pmc *pmc;
  4586. - unsigned int irq;
  4587. - wait_queue_head_t wait;
  4588. + struct regmap *regmap;
  4589. u8 parent;
  4590. };
  4591. #define to_clk_sam9x5_main(hw) container_of(hw, struct clk_sam9x5_main, hw)
  4592. -static irqreturn_t clk_main_osc_irq_handler(int irq, void *dev_id)
  4593. +static inline bool clk_main_osc_ready(struct regmap *regmap)
  4594. {
  4595. - struct clk_main_osc *osc = dev_id;
  4596. + unsigned int status;
  4597. - wake_up(&osc->wait);
  4598. - disable_irq_nosync(osc->irq);
  4599. + regmap_read(regmap, AT91_PMC_SR, &status);
  4600. - return IRQ_HANDLED;
  4601. + return status & AT91_PMC_MOSCS;
  4602. }
  4603. static int clk_main_osc_prepare(struct clk_hw *hw)
  4604. {
  4605. struct clk_main_osc *osc = to_clk_main_osc(hw);
  4606. - struct at91_pmc *pmc = osc->pmc;
  4607. + struct regmap *regmap = osc->regmap;
  4608. u32 tmp;
  4609. - tmp = pmc_read(pmc, AT91_CKGR_MOR) & ~MOR_KEY_MASK;
  4610. + regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  4611. + tmp &= ~MOR_KEY_MASK;
  4612. +
  4613. if (tmp & AT91_PMC_OSCBYPASS)
  4614. return 0;
  4615. if (!(tmp & AT91_PMC_MOSCEN)) {
  4616. tmp |= AT91_PMC_MOSCEN | AT91_PMC_KEY;
  4617. - pmc_write(pmc, AT91_CKGR_MOR, tmp);
  4618. + regmap_write(regmap, AT91_CKGR_MOR, tmp);
  4619. }
  4620. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS)) {
  4621. - enable_irq(osc->irq);
  4622. - wait_event(osc->wait,
  4623. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS);
  4624. - }
  4625. + while (!clk_main_osc_ready(regmap))
  4626. + cpu_relax();
  4627. return 0;
  4628. }
  4629. @@ -106,9 +93,10 @@ static int clk_main_osc_prepare(struct clk_hw *hw)
  4630. static void clk_main_osc_unprepare(struct clk_hw *hw)
  4631. {
  4632. struct clk_main_osc *osc = to_clk_main_osc(hw);
  4633. - struct at91_pmc *pmc = osc->pmc;
  4634. - u32 tmp = pmc_read(pmc, AT91_CKGR_MOR);
  4635. + struct regmap *regmap = osc->regmap;
  4636. + u32 tmp;
  4637. + regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  4638. if (tmp & AT91_PMC_OSCBYPASS)
  4639. return;
  4640. @@ -116,20 +104,22 @@ static void clk_main_osc_unprepare(struct clk_hw *hw)
  4641. return;
  4642. tmp &= ~(AT91_PMC_KEY | AT91_PMC_MOSCEN);
  4643. - pmc_write(pmc, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
  4644. + regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
  4645. }
  4646. static int clk_main_osc_is_prepared(struct clk_hw *hw)
  4647. {
  4648. struct clk_main_osc *osc = to_clk_main_osc(hw);
  4649. - struct at91_pmc *pmc = osc->pmc;
  4650. - u32 tmp = pmc_read(pmc, AT91_CKGR_MOR);
  4651. + struct regmap *regmap = osc->regmap;
  4652. + u32 tmp, status;
  4653. + regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  4654. if (tmp & AT91_PMC_OSCBYPASS)
  4655. return 1;
  4656. - return !!((pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS) &&
  4657. - (pmc_read(pmc, AT91_CKGR_MOR) & AT91_PMC_MOSCEN));
  4658. + regmap_read(regmap, AT91_PMC_SR, &status);
  4659. +
  4660. + return (status & AT91_PMC_MOSCS) && (tmp & AT91_PMC_MOSCEN);
  4661. }
  4662. static const struct clk_ops main_osc_ops = {
  4663. @@ -139,18 +129,16 @@ static const struct clk_ops main_osc_ops = {
  4664. };
  4665. static struct clk * __init
  4666. -at91_clk_register_main_osc(struct at91_pmc *pmc,
  4667. - unsigned int irq,
  4668. +at91_clk_register_main_osc(struct regmap *regmap,
  4669. const char *name,
  4670. const char *parent_name,
  4671. bool bypass)
  4672. {
  4673. - int ret;
  4674. struct clk_main_osc *osc;
  4675. struct clk *clk = NULL;
  4676. struct clk_init_data init;
  4677. - if (!pmc || !irq || !name || !parent_name)
  4678. + if (!name || !parent_name)
  4679. return ERR_PTR(-EINVAL);
  4680. osc = kzalloc(sizeof(*osc), GFP_KERNEL);
  4681. @@ -164,85 +152,70 @@ at91_clk_register_main_osc(struct at91_pmc *pmc,
  4682. init.flags = CLK_IGNORE_UNUSED;
  4683. osc->hw.init = &init;
  4684. - osc->pmc = pmc;
  4685. - osc->irq = irq;
  4686. -
  4687. - init_waitqueue_head(&osc->wait);
  4688. - irq_set_status_flags(osc->irq, IRQ_NOAUTOEN);
  4689. - ret = request_irq(osc->irq, clk_main_osc_irq_handler,
  4690. - IRQF_TRIGGER_HIGH, name, osc);
  4691. - if (ret) {
  4692. - kfree(osc);
  4693. - return ERR_PTR(ret);
  4694. - }
  4695. + osc->regmap = regmap;
  4696. if (bypass)
  4697. - pmc_write(pmc, AT91_CKGR_MOR,
  4698. - (pmc_read(pmc, AT91_CKGR_MOR) &
  4699. - ~(MOR_KEY_MASK | AT91_PMC_MOSCEN)) |
  4700. - AT91_PMC_OSCBYPASS | AT91_PMC_KEY);
  4701. + regmap_update_bits(regmap,
  4702. + AT91_CKGR_MOR, MOR_KEY_MASK |
  4703. + AT91_PMC_MOSCEN,
  4704. + AT91_PMC_OSCBYPASS | AT91_PMC_KEY);
  4705. clk = clk_register(NULL, &osc->hw);
  4706. - if (IS_ERR(clk)) {
  4707. - free_irq(irq, osc);
  4708. + if (IS_ERR(clk))
  4709. kfree(osc);
  4710. - }
  4711. return clk;
  4712. }
  4713. -void __init of_at91rm9200_clk_main_osc_setup(struct device_node *np,
  4714. - struct at91_pmc *pmc)
  4715. +static void __init of_at91rm9200_clk_main_osc_setup(struct device_node *np)
  4716. {
  4717. struct clk *clk;
  4718. - unsigned int irq;
  4719. const char *name = np->name;
  4720. const char *parent_name;
  4721. + struct regmap *regmap;
  4722. bool bypass;
  4723. of_property_read_string(np, "clock-output-names", &name);
  4724. bypass = of_property_read_bool(np, "atmel,osc-bypass");
  4725. parent_name = of_clk_get_parent_name(np, 0);
  4726. - irq = irq_of_parse_and_map(np, 0);
  4727. - if (!irq)
  4728. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4729. + if (IS_ERR(regmap))
  4730. return;
  4731. - clk = at91_clk_register_main_osc(pmc, irq, name, parent_name, bypass);
  4732. + clk = at91_clk_register_main_osc(regmap, name, parent_name, bypass);
  4733. if (IS_ERR(clk))
  4734. return;
  4735. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4736. }
  4737. +CLK_OF_DECLARE(at91rm9200_clk_main_osc, "atmel,at91rm9200-clk-main-osc",
  4738. + of_at91rm9200_clk_main_osc_setup);
  4739. -static irqreturn_t clk_main_rc_osc_irq_handler(int irq, void *dev_id)
  4740. +static bool clk_main_rc_osc_ready(struct regmap *regmap)
  4741. {
  4742. - struct clk_main_rc_osc *osc = dev_id;
  4743. + unsigned int status;
  4744. - wake_up(&osc->wait);
  4745. - disable_irq_nosync(osc->irq);
  4746. + regmap_read(regmap, AT91_PMC_SR, &status);
  4747. - return IRQ_HANDLED;
  4748. + return status & AT91_PMC_MOSCRCS;
  4749. }
  4750. static int clk_main_rc_osc_prepare(struct clk_hw *hw)
  4751. {
  4752. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  4753. - struct at91_pmc *pmc = osc->pmc;
  4754. - u32 tmp;
  4755. + struct regmap *regmap = osc->regmap;
  4756. + unsigned int mor;
  4757. - tmp = pmc_read(pmc, AT91_CKGR_MOR) & ~MOR_KEY_MASK;
  4758. + regmap_read(regmap, AT91_CKGR_MOR, &mor);
  4759. - if (!(tmp & AT91_PMC_MOSCRCEN)) {
  4760. - tmp |= AT91_PMC_MOSCRCEN | AT91_PMC_KEY;
  4761. - pmc_write(pmc, AT91_CKGR_MOR, tmp);
  4762. - }
  4763. + if (!(mor & AT91_PMC_MOSCRCEN))
  4764. + regmap_update_bits(regmap, AT91_CKGR_MOR,
  4765. + MOR_KEY_MASK | AT91_PMC_MOSCRCEN,
  4766. + AT91_PMC_MOSCRCEN | AT91_PMC_KEY);
  4767. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCRCS)) {
  4768. - enable_irq(osc->irq);
  4769. - wait_event(osc->wait,
  4770. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCRCS);
  4771. - }
  4772. + while (!clk_main_rc_osc_ready(regmap))
  4773. + cpu_relax();
  4774. return 0;
  4775. }
  4776. @@ -250,23 +223,28 @@ static int clk_main_rc_osc_prepare(struct clk_hw *hw)
  4777. static void clk_main_rc_osc_unprepare(struct clk_hw *hw)
  4778. {
  4779. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  4780. - struct at91_pmc *pmc = osc->pmc;
  4781. - u32 tmp = pmc_read(pmc, AT91_CKGR_MOR);
  4782. + struct regmap *regmap = osc->regmap;
  4783. + unsigned int mor;
  4784. +
  4785. + regmap_read(regmap, AT91_CKGR_MOR, &mor);
  4786. - if (!(tmp & AT91_PMC_MOSCRCEN))
  4787. + if (!(mor & AT91_PMC_MOSCRCEN))
  4788. return;
  4789. - tmp &= ~(MOR_KEY_MASK | AT91_PMC_MOSCRCEN);
  4790. - pmc_write(pmc, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
  4791. + regmap_update_bits(regmap, AT91_CKGR_MOR,
  4792. + MOR_KEY_MASK | AT91_PMC_MOSCRCEN, AT91_PMC_KEY);
  4793. }
  4794. static int clk_main_rc_osc_is_prepared(struct clk_hw *hw)
  4795. {
  4796. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  4797. - struct at91_pmc *pmc = osc->pmc;
  4798. + struct regmap *regmap = osc->regmap;
  4799. + unsigned int mor, status;
  4800. - return !!((pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCRCS) &&
  4801. - (pmc_read(pmc, AT91_CKGR_MOR) & AT91_PMC_MOSCRCEN));
  4802. + regmap_read(regmap, AT91_CKGR_MOR, &mor);
  4803. + regmap_read(regmap, AT91_PMC_SR, &status);
  4804. +
  4805. + return (mor & AT91_PMC_MOSCRCEN) && (status & AT91_PMC_MOSCRCS);
  4806. }
  4807. static unsigned long clk_main_rc_osc_recalc_rate(struct clk_hw *hw,
  4808. @@ -294,17 +272,15 @@ static const struct clk_ops main_rc_osc_ops = {
  4809. };
  4810. static struct clk * __init
  4811. -at91_clk_register_main_rc_osc(struct at91_pmc *pmc,
  4812. - unsigned int irq,
  4813. +at91_clk_register_main_rc_osc(struct regmap *regmap,
  4814. const char *name,
  4815. u32 frequency, u32 accuracy)
  4816. {
  4817. - int ret;
  4818. struct clk_main_rc_osc *osc;
  4819. struct clk *clk = NULL;
  4820. struct clk_init_data init;
  4821. - if (!pmc || !irq || !name || !frequency)
  4822. + if (!name || !frequency)
  4823. return ERR_PTR(-EINVAL);
  4824. osc = kzalloc(sizeof(*osc), GFP_KERNEL);
  4825. @@ -318,63 +294,53 @@ at91_clk_register_main_rc_osc(struct at91_pmc *pmc,
  4826. init.flags = CLK_IS_ROOT | CLK_IGNORE_UNUSED;
  4827. osc->hw.init = &init;
  4828. - osc->pmc = pmc;
  4829. - osc->irq = irq;
  4830. + osc->regmap = regmap;
  4831. osc->frequency = frequency;
  4832. osc->accuracy = accuracy;
  4833. - init_waitqueue_head(&osc->wait);
  4834. - irq_set_status_flags(osc->irq, IRQ_NOAUTOEN);
  4835. - ret = request_irq(osc->irq, clk_main_rc_osc_irq_handler,
  4836. - IRQF_TRIGGER_HIGH, name, osc);
  4837. - if (ret)
  4838. - return ERR_PTR(ret);
  4839. -
  4840. clk = clk_register(NULL, &osc->hw);
  4841. - if (IS_ERR(clk)) {
  4842. - free_irq(irq, osc);
  4843. + if (IS_ERR(clk))
  4844. kfree(osc);
  4845. - }
  4846. return clk;
  4847. }
  4848. -void __init of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np,
  4849. - struct at91_pmc *pmc)
  4850. +static void __init of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np)
  4851. {
  4852. struct clk *clk;
  4853. - unsigned int irq;
  4854. u32 frequency = 0;
  4855. u32 accuracy = 0;
  4856. const char *name = np->name;
  4857. + struct regmap *regmap;
  4858. of_property_read_string(np, "clock-output-names", &name);
  4859. of_property_read_u32(np, "clock-frequency", &frequency);
  4860. of_property_read_u32(np, "clock-accuracy", &accuracy);
  4861. - irq = irq_of_parse_and_map(np, 0);
  4862. - if (!irq)
  4863. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4864. + if (IS_ERR(regmap))
  4865. return;
  4866. - clk = at91_clk_register_main_rc_osc(pmc, irq, name, frequency,
  4867. - accuracy);
  4868. + clk = at91_clk_register_main_rc_osc(regmap, name, frequency, accuracy);
  4869. if (IS_ERR(clk))
  4870. return;
  4871. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4872. }
  4873. +CLK_OF_DECLARE(at91sam9x5_clk_main_rc_osc, "atmel,at91sam9x5-clk-main-rc-osc",
  4874. + of_at91sam9x5_clk_main_rc_osc_setup);
  4875. -static int clk_main_probe_frequency(struct at91_pmc *pmc)
  4876. +static int clk_main_probe_frequency(struct regmap *regmap)
  4877. {
  4878. unsigned long prep_time, timeout;
  4879. - u32 tmp;
  4880. + unsigned int mcfr;
  4881. timeout = jiffies + usecs_to_jiffies(MAINFRDY_TIMEOUT);
  4882. do {
  4883. prep_time = jiffies;
  4884. - tmp = pmc_read(pmc, AT91_CKGR_MCFR);
  4885. - if (tmp & AT91_PMC_MAINRDY)
  4886. + regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
  4887. + if (mcfr & AT91_PMC_MAINRDY)
  4888. return 0;
  4889. usleep_range(MAINF_LOOP_MIN_WAIT, MAINF_LOOP_MAX_WAIT);
  4890. } while (time_before(prep_time, timeout));
  4891. @@ -382,34 +348,37 @@ static int clk_main_probe_frequency(struct at91_pmc *pmc)
  4892. return -ETIMEDOUT;
  4893. }
  4894. -static unsigned long clk_main_recalc_rate(struct at91_pmc *pmc,
  4895. +static unsigned long clk_main_recalc_rate(struct regmap *regmap,
  4896. unsigned long parent_rate)
  4897. {
  4898. - u32 tmp;
  4899. + unsigned int mcfr;
  4900. if (parent_rate)
  4901. return parent_rate;
  4902. pr_warn("Main crystal frequency not set, using approximate value\n");
  4903. - tmp = pmc_read(pmc, AT91_CKGR_MCFR);
  4904. - if (!(tmp & AT91_PMC_MAINRDY))
  4905. + regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
  4906. + if (!(mcfr & AT91_PMC_MAINRDY))
  4907. return 0;
  4908. - return ((tmp & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV;
  4909. + return ((mcfr & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV;
  4910. }
  4911. static int clk_rm9200_main_prepare(struct clk_hw *hw)
  4912. {
  4913. struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
  4914. - return clk_main_probe_frequency(clkmain->pmc);
  4915. + return clk_main_probe_frequency(clkmain->regmap);
  4916. }
  4917. static int clk_rm9200_main_is_prepared(struct clk_hw *hw)
  4918. {
  4919. struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
  4920. + unsigned int status;
  4921. +
  4922. + regmap_read(clkmain->regmap, AT91_CKGR_MCFR, &status);
  4923. - return !!(pmc_read(clkmain->pmc, AT91_CKGR_MCFR) & AT91_PMC_MAINRDY);
  4924. + return status & AT91_PMC_MAINRDY ? 1 : 0;
  4925. }
  4926. static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw *hw,
  4927. @@ -417,7 +386,7 @@ static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw *hw,
  4928. {
  4929. struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
  4930. - return clk_main_recalc_rate(clkmain->pmc, parent_rate);
  4931. + return clk_main_recalc_rate(clkmain->regmap, parent_rate);
  4932. }
  4933. static const struct clk_ops rm9200_main_ops = {
  4934. @@ -427,7 +396,7 @@ static const struct clk_ops rm9200_main_ops = {
  4935. };
  4936. static struct clk * __init
  4937. -at91_clk_register_rm9200_main(struct at91_pmc *pmc,
  4938. +at91_clk_register_rm9200_main(struct regmap *regmap,
  4939. const char *name,
  4940. const char *parent_name)
  4941. {
  4942. @@ -435,7 +404,7 @@ at91_clk_register_rm9200_main(struct at91_pmc *pmc,
  4943. struct clk *clk = NULL;
  4944. struct clk_init_data init;
  4945. - if (!pmc || !name)
  4946. + if (!name)
  4947. return ERR_PTR(-EINVAL);
  4948. if (!parent_name)
  4949. @@ -452,7 +421,7 @@ at91_clk_register_rm9200_main(struct at91_pmc *pmc,
  4950. init.flags = 0;
  4951. clkmain->hw.init = &init;
  4952. - clkmain->pmc = pmc;
  4953. + clkmain->regmap = regmap;
  4954. clk = clk_register(NULL, &clkmain->hw);
  4955. if (IS_ERR(clk))
  4956. @@ -461,52 +430,54 @@ at91_clk_register_rm9200_main(struct at91_pmc *pmc,
  4957. return clk;
  4958. }
  4959. -void __init of_at91rm9200_clk_main_setup(struct device_node *np,
  4960. - struct at91_pmc *pmc)
  4961. +static void __init of_at91rm9200_clk_main_setup(struct device_node *np)
  4962. {
  4963. struct clk *clk;
  4964. const char *parent_name;
  4965. const char *name = np->name;
  4966. + struct regmap *regmap;
  4967. parent_name = of_clk_get_parent_name(np, 0);
  4968. of_property_read_string(np, "clock-output-names", &name);
  4969. - clk = at91_clk_register_rm9200_main(pmc, name, parent_name);
  4970. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4971. + if (IS_ERR(regmap))
  4972. + return;
  4973. +
  4974. + clk = at91_clk_register_rm9200_main(regmap, name, parent_name);
  4975. if (IS_ERR(clk))
  4976. return;
  4977. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4978. }
  4979. +CLK_OF_DECLARE(at91rm9200_clk_main, "atmel,at91rm9200-clk-main",
  4980. + of_at91rm9200_clk_main_setup);
  4981. -static irqreturn_t clk_sam9x5_main_irq_handler(int irq, void *dev_id)
  4982. +static inline bool clk_sam9x5_main_ready(struct regmap *regmap)
  4983. {
  4984. - struct clk_sam9x5_main *clkmain = dev_id;
  4985. + unsigned int status;
  4986. - wake_up(&clkmain->wait);
  4987. - disable_irq_nosync(clkmain->irq);
  4988. + regmap_read(regmap, AT91_PMC_SR, &status);
  4989. - return IRQ_HANDLED;
  4990. + return status & AT91_PMC_MOSCSELS ? 1 : 0;
  4991. }
  4992. static int clk_sam9x5_main_prepare(struct clk_hw *hw)
  4993. {
  4994. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  4995. - struct at91_pmc *pmc = clkmain->pmc;
  4996. + struct regmap *regmap = clkmain->regmap;
  4997. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS)) {
  4998. - enable_irq(clkmain->irq);
  4999. - wait_event(clkmain->wait,
  5000. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS);
  5001. - }
  5002. + while (!clk_sam9x5_main_ready(regmap))
  5003. + cpu_relax();
  5004. - return clk_main_probe_frequency(pmc);
  5005. + return clk_main_probe_frequency(regmap);
  5006. }
  5007. static int clk_sam9x5_main_is_prepared(struct clk_hw *hw)
  5008. {
  5009. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  5010. - return !!(pmc_read(clkmain->pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS);
  5011. + return clk_sam9x5_main_ready(clkmain->regmap);
  5012. }
  5013. static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw *hw,
  5014. @@ -514,30 +485,28 @@ static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw *hw,
  5015. {
  5016. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  5017. - return clk_main_recalc_rate(clkmain->pmc, parent_rate);
  5018. + return clk_main_recalc_rate(clkmain->regmap, parent_rate);
  5019. }
  5020. static int clk_sam9x5_main_set_parent(struct clk_hw *hw, u8 index)
  5021. {
  5022. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  5023. - struct at91_pmc *pmc = clkmain->pmc;
  5024. - u32 tmp;
  5025. + struct regmap *regmap = clkmain->regmap;
  5026. + unsigned int tmp;
  5027. if (index > 1)
  5028. return -EINVAL;
  5029. - tmp = pmc_read(pmc, AT91_CKGR_MOR) & ~MOR_KEY_MASK;
  5030. + regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  5031. + tmp &= ~MOR_KEY_MASK;
  5032. if (index && !(tmp & AT91_PMC_MOSCSEL))
  5033. - pmc_write(pmc, AT91_CKGR_MOR, tmp | AT91_PMC_MOSCSEL);
  5034. + regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_MOSCSEL);
  5035. else if (!index && (tmp & AT91_PMC_MOSCSEL))
  5036. - pmc_write(pmc, AT91_CKGR_MOR, tmp & ~AT91_PMC_MOSCSEL);
  5037. + regmap_write(regmap, AT91_CKGR_MOR, tmp & ~AT91_PMC_MOSCSEL);
  5038. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS)) {
  5039. - enable_irq(clkmain->irq);
  5040. - wait_event(clkmain->wait,
  5041. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS);
  5042. - }
  5043. + while (!clk_sam9x5_main_ready(regmap))
  5044. + cpu_relax();
  5045. return 0;
  5046. }
  5047. @@ -545,8 +514,11 @@ static int clk_sam9x5_main_set_parent(struct clk_hw *hw, u8 index)
  5048. static u8 clk_sam9x5_main_get_parent(struct clk_hw *hw)
  5049. {
  5050. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  5051. + unsigned int status;
  5052. +
  5053. + regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
  5054. - return !!(pmc_read(clkmain->pmc, AT91_CKGR_MOR) & AT91_PMC_MOSCEN);
  5055. + return status & AT91_PMC_MOSCEN ? 1 : 0;
  5056. }
  5057. static const struct clk_ops sam9x5_main_ops = {
  5058. @@ -558,18 +530,17 @@ static const struct clk_ops sam9x5_main_ops = {
  5059. };
  5060. static struct clk * __init
  5061. -at91_clk_register_sam9x5_main(struct at91_pmc *pmc,
  5062. - unsigned int irq,
  5063. +at91_clk_register_sam9x5_main(struct regmap *regmap,
  5064. const char *name,
  5065. const char **parent_names,
  5066. int num_parents)
  5067. {
  5068. - int ret;
  5069. struct clk_sam9x5_main *clkmain;
  5070. struct clk *clk = NULL;
  5071. struct clk_init_data init;
  5072. + unsigned int status;
  5073. - if (!pmc || !irq || !name)
  5074. + if (!name)
  5075. return ERR_PTR(-EINVAL);
  5076. if (!parent_names || !num_parents)
  5077. @@ -586,51 +557,42 @@ at91_clk_register_sam9x5_main(struct at91_pmc *pmc,
  5078. init.flags = CLK_SET_PARENT_GATE;
  5079. clkmain->hw.init = &init;
  5080. - clkmain->pmc = pmc;
  5081. - clkmain->irq = irq;
  5082. - clkmain->parent = !!(pmc_read(clkmain->pmc, AT91_CKGR_MOR) &
  5083. - AT91_PMC_MOSCEN);
  5084. - init_waitqueue_head(&clkmain->wait);
  5085. - irq_set_status_flags(clkmain->irq, IRQ_NOAUTOEN);
  5086. - ret = request_irq(clkmain->irq, clk_sam9x5_main_irq_handler,
  5087. - IRQF_TRIGGER_HIGH, name, clkmain);
  5088. - if (ret)
  5089. - return ERR_PTR(ret);
  5090. + clkmain->regmap = regmap;
  5091. + regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
  5092. + clkmain->parent = status & AT91_PMC_MOSCEN ? 1 : 0;
  5093. clk = clk_register(NULL, &clkmain->hw);
  5094. - if (IS_ERR(clk)) {
  5095. - free_irq(clkmain->irq, clkmain);
  5096. + if (IS_ERR(clk))
  5097. kfree(clkmain);
  5098. - }
  5099. return clk;
  5100. }
  5101. -void __init of_at91sam9x5_clk_main_setup(struct device_node *np,
  5102. - struct at91_pmc *pmc)
  5103. +static void __init of_at91sam9x5_clk_main_setup(struct device_node *np)
  5104. {
  5105. struct clk *clk;
  5106. const char *parent_names[2];
  5107. int num_parents;
  5108. - unsigned int irq;
  5109. const char *name = np->name;
  5110. + struct regmap *regmap;
  5111. num_parents = of_clk_get_parent_count(np);
  5112. if (num_parents <= 0 || num_parents > 2)
  5113. return;
  5114. of_clk_parent_fill(np, parent_names, num_parents);
  5115. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5116. + if (IS_ERR(regmap))
  5117. + return;
  5118. of_property_read_string(np, "clock-output-names", &name);
  5119. - irq = irq_of_parse_and_map(np, 0);
  5120. - if (!irq)
  5121. - return;
  5122. -
  5123. - clk = at91_clk_register_sam9x5_main(pmc, irq, name, parent_names,
  5124. + clk = at91_clk_register_sam9x5_main(regmap, name, parent_names,
  5125. num_parents);
  5126. if (IS_ERR(clk))
  5127. return;
  5128. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  5129. }
  5130. +CLK_OF_DECLARE(at91sam9x5_clk_main, "atmel,at91sam9x5-clk-main",
  5131. + of_at91sam9x5_clk_main_setup);
  5132. diff --git a/drivers/clk/at91/clk-master.c b/drivers/clk/at91/clk-master.c
  5133. index 620ea323356b..7d4a1864ea7c 100644
  5134. --- a/drivers/clk/at91/clk-master.c
  5135. +++ b/drivers/clk/at91/clk-master.c
  5136. @@ -12,13 +12,8 @@
  5137. #include <linux/clkdev.h>
  5138. #include <linux/clk/at91_pmc.h>
  5139. #include <linux/of.h>
  5140. -#include <linux/of_address.h>
  5141. -#include <linux/of_irq.h>
  5142. -#include <linux/io.h>
  5143. -#include <linux/wait.h>
  5144. -#include <linux/sched.h>
  5145. -#include <linux/interrupt.h>
  5146. -#include <linux/irq.h>
  5147. +#include <linux/mfd/syscon.h>
  5148. +#include <linux/regmap.h>
  5149. #include "pmc.h"
  5150. @@ -44,32 +39,26 @@ struct clk_master_layout {
  5151. struct clk_master {
  5152. struct clk_hw hw;
  5153. - struct at91_pmc *pmc;
  5154. - unsigned int irq;
  5155. - wait_queue_head_t wait;
  5156. + struct regmap *regmap;
  5157. const struct clk_master_layout *layout;
  5158. const struct clk_master_characteristics *characteristics;
  5159. };
  5160. -static irqreturn_t clk_master_irq_handler(int irq, void *dev_id)
  5161. +static inline bool clk_master_ready(struct regmap *regmap)
  5162. {
  5163. - struct clk_master *master = (struct clk_master *)dev_id;
  5164. + unsigned int status;
  5165. - wake_up(&master->wait);
  5166. - disable_irq_nosync(master->irq);
  5167. + regmap_read(regmap, AT91_PMC_SR, &status);
  5168. - return IRQ_HANDLED;
  5169. + return status & AT91_PMC_MCKRDY ? 1 : 0;
  5170. }
  5171. +
  5172. static int clk_master_prepare(struct clk_hw *hw)
  5173. {
  5174. struct clk_master *master = to_clk_master(hw);
  5175. - struct at91_pmc *pmc = master->pmc;
  5176. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MCKRDY)) {
  5177. - enable_irq(master->irq);
  5178. - wait_event(master->wait,
  5179. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MCKRDY);
  5180. - }
  5181. + while (!clk_master_ready(master->regmap))
  5182. + cpu_relax();
  5183. return 0;
  5184. }
  5185. @@ -78,7 +67,7 @@ static int clk_master_is_prepared(struct clk_hw *hw)
  5186. {
  5187. struct clk_master *master = to_clk_master(hw);
  5188. - return !!(pmc_read(master->pmc, AT91_PMC_SR) & AT91_PMC_MCKRDY);
  5189. + return clk_master_ready(master->regmap);
  5190. }
  5191. static unsigned long clk_master_recalc_rate(struct clk_hw *hw,
  5192. @@ -88,18 +77,16 @@ static unsigned long clk_master_recalc_rate(struct clk_hw *hw,
  5193. u8 div;
  5194. unsigned long rate = parent_rate;
  5195. struct clk_master *master = to_clk_master(hw);
  5196. - struct at91_pmc *pmc = master->pmc;
  5197. const struct clk_master_layout *layout = master->layout;
  5198. const struct clk_master_characteristics *characteristics =
  5199. master->characteristics;
  5200. - u32 tmp;
  5201. + unsigned int mckr;
  5202. - pmc_lock(pmc);
  5203. - tmp = pmc_read(pmc, AT91_PMC_MCKR) & layout->mask;
  5204. - pmc_unlock(pmc);
  5205. + regmap_read(master->regmap, AT91_PMC_MCKR, &mckr);
  5206. + mckr &= layout->mask;
  5207. - pres = (tmp >> layout->pres_shift) & MASTER_PRES_MASK;
  5208. - div = (tmp >> MASTER_DIV_SHIFT) & MASTER_DIV_MASK;
  5209. + pres = (mckr >> layout->pres_shift) & MASTER_PRES_MASK;
  5210. + div = (mckr >> MASTER_DIV_SHIFT) & MASTER_DIV_MASK;
  5211. if (characteristics->have_div3_pres && pres == MASTER_PRES_MAX)
  5212. rate /= 3;
  5213. @@ -119,9 +106,11 @@ static unsigned long clk_master_recalc_rate(struct clk_hw *hw,
  5214. static u8 clk_master_get_parent(struct clk_hw *hw)
  5215. {
  5216. struct clk_master *master = to_clk_master(hw);
  5217. - struct at91_pmc *pmc = master->pmc;
  5218. + unsigned int mckr;
  5219. - return pmc_read(pmc, AT91_PMC_MCKR) & AT91_PMC_CSS;
  5220. + regmap_read(master->regmap, AT91_PMC_MCKR, &mckr);
  5221. +
  5222. + return mckr & AT91_PMC_CSS;
  5223. }
  5224. static const struct clk_ops master_ops = {
  5225. @@ -132,18 +121,17 @@ static const struct clk_ops master_ops = {
  5226. };
  5227. static struct clk * __init
  5228. -at91_clk_register_master(struct at91_pmc *pmc, unsigned int irq,
  5229. +at91_clk_register_master(struct regmap *regmap,
  5230. const char *name, int num_parents,
  5231. const char **parent_names,
  5232. const struct clk_master_layout *layout,
  5233. const struct clk_master_characteristics *characteristics)
  5234. {
  5235. - int ret;
  5236. struct clk_master *master;
  5237. struct clk *clk = NULL;
  5238. struct clk_init_data init;
  5239. - if (!pmc || !irq || !name || !num_parents || !parent_names)
  5240. + if (!name || !num_parents || !parent_names)
  5241. return ERR_PTR(-EINVAL);
  5242. master = kzalloc(sizeof(*master), GFP_KERNEL);
  5243. @@ -159,20 +147,10 @@ at91_clk_register_master(struct at91_pmc *pmc, unsigned int irq,
  5244. master->hw.init = &init;
  5245. master->layout = layout;
  5246. master->characteristics = characteristics;
  5247. - master->pmc = pmc;
  5248. - master->irq = irq;
  5249. - init_waitqueue_head(&master->wait);
  5250. - irq_set_status_flags(master->irq, IRQ_NOAUTOEN);
  5251. - ret = request_irq(master->irq, clk_master_irq_handler,
  5252. - IRQF_TRIGGER_HIGH, "clk-master", master);
  5253. - if (ret) {
  5254. - kfree(master);
  5255. - return ERR_PTR(ret);
  5256. - }
  5257. + master->regmap = regmap;
  5258. clk = clk_register(NULL, &master->hw);
  5259. if (IS_ERR(clk)) {
  5260. - free_irq(master->irq, master);
  5261. kfree(master);
  5262. }
  5263. @@ -217,15 +195,15 @@ of_at91_clk_master_get_characteristics(struct device_node *np)
  5264. }
  5265. static void __init
  5266. -of_at91_clk_master_setup(struct device_node *np, struct at91_pmc *pmc,
  5267. +of_at91_clk_master_setup(struct device_node *np,
  5268. const struct clk_master_layout *layout)
  5269. {
  5270. struct clk *clk;
  5271. int num_parents;
  5272. - unsigned int irq;
  5273. const char *parent_names[MASTER_SOURCE_MAX];
  5274. const char *name = np->name;
  5275. struct clk_master_characteristics *characteristics;
  5276. + struct regmap *regmap;
  5277. num_parents = of_clk_get_parent_count(np);
  5278. if (num_parents <= 0 || num_parents > MASTER_SOURCE_MAX)
  5279. @@ -239,11 +217,11 @@ of_at91_clk_master_setup(struct device_node *np, struct at91_pmc *pmc,
  5280. if (!characteristics)
  5281. return;
  5282. - irq = irq_of_parse_and_map(np, 0);
  5283. - if (!irq)
  5284. - goto out_free_characteristics;
  5285. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5286. + if (IS_ERR(regmap))
  5287. + return;
  5288. - clk = at91_clk_register_master(pmc, irq, name, num_parents,
  5289. + clk = at91_clk_register_master(regmap, name, num_parents,
  5290. parent_names, layout,
  5291. characteristics);
  5292. if (IS_ERR(clk))
  5293. @@ -256,14 +234,16 @@ of_at91_clk_master_setup(struct device_node *np, struct at91_pmc *pmc,
  5294. kfree(characteristics);
  5295. }
  5296. -void __init of_at91rm9200_clk_master_setup(struct device_node *np,
  5297. - struct at91_pmc *pmc)
  5298. +static void __init of_at91rm9200_clk_master_setup(struct device_node *np)
  5299. {
  5300. - of_at91_clk_master_setup(np, pmc, &at91rm9200_master_layout);
  5301. + of_at91_clk_master_setup(np, &at91rm9200_master_layout);
  5302. }
  5303. +CLK_OF_DECLARE(at91rm9200_clk_master, "atmel,at91rm9200-clk-master",
  5304. + of_at91rm9200_clk_master_setup);
  5305. -void __init of_at91sam9x5_clk_master_setup(struct device_node *np,
  5306. - struct at91_pmc *pmc)
  5307. +static void __init of_at91sam9x5_clk_master_setup(struct device_node *np)
  5308. {
  5309. - of_at91_clk_master_setup(np, pmc, &at91sam9x5_master_layout);
  5310. + of_at91_clk_master_setup(np, &at91sam9x5_master_layout);
  5311. }
  5312. +CLK_OF_DECLARE(at91sam9x5_clk_master, "atmel,at91sam9x5-clk-master",
  5313. + of_at91sam9x5_clk_master_setup);
  5314. diff --git a/drivers/clk/at91/clk-peripheral.c b/drivers/clk/at91/clk-peripheral.c
  5315. index 58f3b568e9cb..d69cd2a121b1 100644
  5316. --- a/drivers/clk/at91/clk-peripheral.c
  5317. +++ b/drivers/clk/at91/clk-peripheral.c
  5318. @@ -12,11 +12,13 @@
  5319. #include <linux/clkdev.h>
  5320. #include <linux/clk/at91_pmc.h>
  5321. #include <linux/of.h>
  5322. -#include <linux/of_address.h>
  5323. -#include <linux/io.h>
  5324. +#include <linux/mfd/syscon.h>
  5325. +#include <linux/regmap.h>
  5326. #include "pmc.h"
  5327. +DEFINE_SPINLOCK(pmc_pcr_lock);
  5328. +
  5329. #define PERIPHERAL_MAX 64
  5330. #define PERIPHERAL_AT91RM9200 0
  5331. @@ -33,7 +35,7 @@
  5332. struct clk_peripheral {
  5333. struct clk_hw hw;
  5334. - struct at91_pmc *pmc;
  5335. + struct regmap *regmap;
  5336. u32 id;
  5337. };
  5338. @@ -41,8 +43,9 @@ struct clk_peripheral {
  5339. struct clk_sam9x5_peripheral {
  5340. struct clk_hw hw;
  5341. - struct at91_pmc *pmc;
  5342. + struct regmap *regmap;
  5343. struct clk_range range;
  5344. + spinlock_t *lock;
  5345. u32 id;
  5346. u32 div;
  5347. bool auto_div;
  5348. @@ -54,7 +57,6 @@ struct clk_sam9x5_peripheral {
  5349. static int clk_peripheral_enable(struct clk_hw *hw)
  5350. {
  5351. struct clk_peripheral *periph = to_clk_peripheral(hw);
  5352. - struct at91_pmc *pmc = periph->pmc;
  5353. int offset = AT91_PMC_PCER;
  5354. u32 id = periph->id;
  5355. @@ -62,14 +64,14 @@ static int clk_peripheral_enable(struct clk_hw *hw)
  5356. return 0;
  5357. if (id > PERIPHERAL_ID_MAX)
  5358. offset = AT91_PMC_PCER1;
  5359. - pmc_write(pmc, offset, PERIPHERAL_MASK(id));
  5360. + regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
  5361. +
  5362. return 0;
  5363. }
  5364. static void clk_peripheral_disable(struct clk_hw *hw)
  5365. {
  5366. struct clk_peripheral *periph = to_clk_peripheral(hw);
  5367. - struct at91_pmc *pmc = periph->pmc;
  5368. int offset = AT91_PMC_PCDR;
  5369. u32 id = periph->id;
  5370. @@ -77,21 +79,23 @@ static void clk_peripheral_disable(struct clk_hw *hw)
  5371. return;
  5372. if (id > PERIPHERAL_ID_MAX)
  5373. offset = AT91_PMC_PCDR1;
  5374. - pmc_write(pmc, offset, PERIPHERAL_MASK(id));
  5375. + regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
  5376. }
  5377. static int clk_peripheral_is_enabled(struct clk_hw *hw)
  5378. {
  5379. struct clk_peripheral *periph = to_clk_peripheral(hw);
  5380. - struct at91_pmc *pmc = periph->pmc;
  5381. int offset = AT91_PMC_PCSR;
  5382. + unsigned int status;
  5383. u32 id = periph->id;
  5384. if (id < PERIPHERAL_ID_MIN)
  5385. return 1;
  5386. if (id > PERIPHERAL_ID_MAX)
  5387. offset = AT91_PMC_PCSR1;
  5388. - return !!(pmc_read(pmc, offset) & PERIPHERAL_MASK(id));
  5389. + regmap_read(periph->regmap, offset, &status);
  5390. +
  5391. + return status & PERIPHERAL_MASK(id) ? 1 : 0;
  5392. }
  5393. static const struct clk_ops peripheral_ops = {
  5394. @@ -101,14 +105,14 @@ static const struct clk_ops peripheral_ops = {
  5395. };
  5396. static struct clk * __init
  5397. -at91_clk_register_peripheral(struct at91_pmc *pmc, const char *name,
  5398. +at91_clk_register_peripheral(struct regmap *regmap, const char *name,
  5399. const char *parent_name, u32 id)
  5400. {
  5401. struct clk_peripheral *periph;
  5402. struct clk *clk = NULL;
  5403. struct clk_init_data init;
  5404. - if (!pmc || !name || !parent_name || id > PERIPHERAL_ID_MAX)
  5405. + if (!name || !parent_name || id > PERIPHERAL_ID_MAX)
  5406. return ERR_PTR(-EINVAL);
  5407. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  5408. @@ -123,7 +127,7 @@ at91_clk_register_peripheral(struct at91_pmc *pmc, const char *name,
  5409. periph->id = id;
  5410. periph->hw.init = &init;
  5411. - periph->pmc = pmc;
  5412. + periph->regmap = regmap;
  5413. clk = clk_register(NULL, &periph->hw);
  5414. if (IS_ERR(clk))
  5415. @@ -160,53 +164,58 @@ static void clk_sam9x5_peripheral_autodiv(struct clk_sam9x5_peripheral *periph)
  5416. static int clk_sam9x5_peripheral_enable(struct clk_hw *hw)
  5417. {
  5418. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  5419. - struct at91_pmc *pmc = periph->pmc;
  5420. - u32 tmp;
  5421. + unsigned long flags;
  5422. if (periph->id < PERIPHERAL_ID_MIN)
  5423. return 0;
  5424. - pmc_lock(pmc);
  5425. - pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  5426. - tmp = pmc_read(pmc, AT91_PMC_PCR) & ~AT91_PMC_PCR_DIV_MASK;
  5427. - pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_DIV(periph->div)
  5428. - | AT91_PMC_PCR_CMD
  5429. - | AT91_PMC_PCR_EN);
  5430. - pmc_unlock(pmc);
  5431. + spin_lock_irqsave(periph->lock, flags);
  5432. + regmap_write(periph->regmap, AT91_PMC_PCR,
  5433. + (periph->id & AT91_PMC_PCR_PID_MASK));
  5434. + regmap_update_bits(periph->regmap, AT91_PMC_PCR,
  5435. + AT91_PMC_PCR_DIV_MASK | AT91_PMC_PCR_CMD |
  5436. + AT91_PMC_PCR_EN,
  5437. + AT91_PMC_PCR_DIV(periph->div) |
  5438. + AT91_PMC_PCR_CMD |
  5439. + AT91_PMC_PCR_EN);
  5440. + spin_unlock_irqrestore(periph->lock, flags);
  5441. +
  5442. return 0;
  5443. }
  5444. static void clk_sam9x5_peripheral_disable(struct clk_hw *hw)
  5445. {
  5446. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  5447. - struct at91_pmc *pmc = periph->pmc;
  5448. - u32 tmp;
  5449. + unsigned long flags;
  5450. if (periph->id < PERIPHERAL_ID_MIN)
  5451. return;
  5452. - pmc_lock(pmc);
  5453. - pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  5454. - tmp = pmc_read(pmc, AT91_PMC_PCR) & ~AT91_PMC_PCR_EN;
  5455. - pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_CMD);
  5456. - pmc_unlock(pmc);
  5457. + spin_lock_irqsave(periph->lock, flags);
  5458. + regmap_write(periph->regmap, AT91_PMC_PCR,
  5459. + (periph->id & AT91_PMC_PCR_PID_MASK));
  5460. + regmap_update_bits(periph->regmap, AT91_PMC_PCR,
  5461. + AT91_PMC_PCR_EN | AT91_PMC_PCR_CMD,
  5462. + AT91_PMC_PCR_CMD);
  5463. + spin_unlock_irqrestore(periph->lock, flags);
  5464. }
  5465. static int clk_sam9x5_peripheral_is_enabled(struct clk_hw *hw)
  5466. {
  5467. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  5468. - struct at91_pmc *pmc = periph->pmc;
  5469. - int ret;
  5470. + unsigned long flags;
  5471. + unsigned int status;
  5472. if (periph->id < PERIPHERAL_ID_MIN)
  5473. return 1;
  5474. - pmc_lock(pmc);
  5475. - pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  5476. - ret = !!(pmc_read(pmc, AT91_PMC_PCR) & AT91_PMC_PCR_EN);
  5477. - pmc_unlock(pmc);
  5478. + spin_lock_irqsave(periph->lock, flags);
  5479. + regmap_write(periph->regmap, AT91_PMC_PCR,
  5480. + (periph->id & AT91_PMC_PCR_PID_MASK));
  5481. + regmap_read(periph->regmap, AT91_PMC_PCR, &status);
  5482. + spin_unlock_irqrestore(periph->lock, flags);
  5483. - return ret;
  5484. + return status & AT91_PMC_PCR_EN ? 1 : 0;
  5485. }
  5486. static unsigned long
  5487. @@ -214,19 +223,20 @@ clk_sam9x5_peripheral_recalc_rate(struct clk_hw *hw,
  5488. unsigned long parent_rate)
  5489. {
  5490. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  5491. - struct at91_pmc *pmc = periph->pmc;
  5492. - u32 tmp;
  5493. + unsigned long flags;
  5494. + unsigned int status;
  5495. if (periph->id < PERIPHERAL_ID_MIN)
  5496. return parent_rate;
  5497. - pmc_lock(pmc);
  5498. - pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  5499. - tmp = pmc_read(pmc, AT91_PMC_PCR);
  5500. - pmc_unlock(pmc);
  5501. + spin_lock_irqsave(periph->lock, flags);
  5502. + regmap_write(periph->regmap, AT91_PMC_PCR,
  5503. + (periph->id & AT91_PMC_PCR_PID_MASK));
  5504. + regmap_read(periph->regmap, AT91_PMC_PCR, &status);
  5505. + spin_unlock_irqrestore(periph->lock, flags);
  5506. - if (tmp & AT91_PMC_PCR_EN) {
  5507. - periph->div = PERIPHERAL_RSHIFT(tmp);
  5508. + if (status & AT91_PMC_PCR_EN) {
  5509. + periph->div = PERIPHERAL_RSHIFT(status);
  5510. periph->auto_div = false;
  5511. } else {
  5512. clk_sam9x5_peripheral_autodiv(periph);
  5513. @@ -318,15 +328,15 @@ static const struct clk_ops sam9x5_peripheral_ops = {
  5514. };
  5515. static struct clk * __init
  5516. -at91_clk_register_sam9x5_peripheral(struct at91_pmc *pmc, const char *name,
  5517. - const char *parent_name, u32 id,
  5518. - const struct clk_range *range)
  5519. +at91_clk_register_sam9x5_peripheral(struct regmap *regmap, spinlock_t *lock,
  5520. + const char *name, const char *parent_name,
  5521. + u32 id, const struct clk_range *range)
  5522. {
  5523. struct clk_sam9x5_peripheral *periph;
  5524. struct clk *clk = NULL;
  5525. struct clk_init_data init;
  5526. - if (!pmc || !name || !parent_name)
  5527. + if (!name || !parent_name)
  5528. return ERR_PTR(-EINVAL);
  5529. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  5530. @@ -342,7 +352,8 @@ at91_clk_register_sam9x5_peripheral(struct at91_pmc *pmc, const char *name,
  5531. periph->id = id;
  5532. periph->hw.init = &init;
  5533. periph->div = 0;
  5534. - periph->pmc = pmc;
  5535. + periph->regmap = regmap;
  5536. + periph->lock = lock;
  5537. periph->auto_div = true;
  5538. periph->range = *range;
  5539. @@ -356,7 +367,7 @@ at91_clk_register_sam9x5_peripheral(struct at91_pmc *pmc, const char *name,
  5540. }
  5541. static void __init
  5542. -of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5543. +of_at91_clk_periph_setup(struct device_node *np, u8 type)
  5544. {
  5545. int num;
  5546. u32 id;
  5547. @@ -364,6 +375,7 @@ of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5548. const char *parent_name;
  5549. const char *name;
  5550. struct device_node *periphclknp;
  5551. + struct regmap *regmap;
  5552. parent_name = of_clk_get_parent_name(np, 0);
  5553. if (!parent_name)
  5554. @@ -373,6 +385,10 @@ of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5555. if (!num || num > PERIPHERAL_MAX)
  5556. return;
  5557. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5558. + if (IS_ERR(regmap))
  5559. + return;
  5560. +
  5561. for_each_child_of_node(np, periphclknp) {
  5562. if (of_property_read_u32(periphclknp, "reg", &id))
  5563. continue;
  5564. @@ -384,7 +400,7 @@ of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5565. name = periphclknp->name;
  5566. if (type == PERIPHERAL_AT91RM9200) {
  5567. - clk = at91_clk_register_peripheral(pmc, name,
  5568. + clk = at91_clk_register_peripheral(regmap, name,
  5569. parent_name, id);
  5570. } else {
  5571. struct clk_range range = CLK_RANGE(0, 0);
  5572. @@ -393,7 +409,9 @@ of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5573. "atmel,clk-output-range",
  5574. &range);
  5575. - clk = at91_clk_register_sam9x5_peripheral(pmc, name,
  5576. + clk = at91_clk_register_sam9x5_peripheral(regmap,
  5577. + &pmc_pcr_lock,
  5578. + name,
  5579. parent_name,
  5580. id, &range);
  5581. }
  5582. @@ -405,14 +423,16 @@ of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5583. }
  5584. }
  5585. -void __init of_at91rm9200_clk_periph_setup(struct device_node *np,
  5586. - struct at91_pmc *pmc)
  5587. +static void __init of_at91rm9200_clk_periph_setup(struct device_node *np)
  5588. {
  5589. - of_at91_clk_periph_setup(np, pmc, PERIPHERAL_AT91RM9200);
  5590. + of_at91_clk_periph_setup(np, PERIPHERAL_AT91RM9200);
  5591. }
  5592. +CLK_OF_DECLARE(at91rm9200_clk_periph, "atmel,at91rm9200-clk-peripheral",
  5593. + of_at91rm9200_clk_periph_setup);
  5594. -void __init of_at91sam9x5_clk_periph_setup(struct device_node *np,
  5595. - struct at91_pmc *pmc)
  5596. +static void __init of_at91sam9x5_clk_periph_setup(struct device_node *np)
  5597. {
  5598. - of_at91_clk_periph_setup(np, pmc, PERIPHERAL_AT91SAM9X5);
  5599. + of_at91_clk_periph_setup(np, PERIPHERAL_AT91SAM9X5);
  5600. }
  5601. +CLK_OF_DECLARE(at91sam9x5_clk_periph, "atmel,at91sam9x5-clk-peripheral",
  5602. + of_at91sam9x5_clk_periph_setup);
  5603. diff --git a/drivers/clk/at91/clk-pll.c b/drivers/clk/at91/clk-pll.c
  5604. index 18b60f4895a6..fb2e0b56d4b7 100644
  5605. --- a/drivers/clk/at91/clk-pll.c
  5606. +++ b/drivers/clk/at91/clk-pll.c
  5607. @@ -12,14 +12,8 @@
  5608. #include <linux/clkdev.h>
  5609. #include <linux/clk/at91_pmc.h>
  5610. #include <linux/of.h>
  5611. -#include <linux/of_address.h>
  5612. -#include <linux/of_irq.h>
  5613. -#include <linux/io.h>
  5614. -#include <linux/kernel.h>
  5615. -#include <linux/wait.h>
  5616. -#include <linux/sched.h>
  5617. -#include <linux/interrupt.h>
  5618. -#include <linux/irq.h>
  5619. +#include <linux/mfd/syscon.h>
  5620. +#include <linux/regmap.h>
  5621. #include "pmc.h"
  5622. @@ -58,9 +52,7 @@ struct clk_pll_layout {
  5623. struct clk_pll {
  5624. struct clk_hw hw;
  5625. - struct at91_pmc *pmc;
  5626. - unsigned int irq;
  5627. - wait_queue_head_t wait;
  5628. + struct regmap *regmap;
  5629. u8 id;
  5630. u8 div;
  5631. u8 range;
  5632. @@ -69,20 +61,19 @@ struct clk_pll {
  5633. const struct clk_pll_characteristics *characteristics;
  5634. };
  5635. -static irqreturn_t clk_pll_irq_handler(int irq, void *dev_id)
  5636. +static inline bool clk_pll_ready(struct regmap *regmap, int id)
  5637. {
  5638. - struct clk_pll *pll = (struct clk_pll *)dev_id;
  5639. + unsigned int status;
  5640. - wake_up(&pll->wait);
  5641. - disable_irq_nosync(pll->irq);
  5642. + regmap_read(regmap, AT91_PMC_SR, &status);
  5643. - return IRQ_HANDLED;
  5644. + return status & PLL_STATUS_MASK(id) ? 1 : 0;
  5645. }
  5646. static int clk_pll_prepare(struct clk_hw *hw)
  5647. {
  5648. struct clk_pll *pll = to_clk_pll(hw);
  5649. - struct at91_pmc *pmc = pll->pmc;
  5650. + struct regmap *regmap = pll->regmap;
  5651. const struct clk_pll_layout *layout = pll->layout;
  5652. const struct clk_pll_characteristics *characteristics =
  5653. pll->characteristics;
  5654. @@ -90,39 +81,34 @@ static int clk_pll_prepare(struct clk_hw *hw)
  5655. u32 mask = PLL_STATUS_MASK(id);
  5656. int offset = PLL_REG(id);
  5657. u8 out = 0;
  5658. - u32 pllr, icpr;
  5659. + unsigned int pllr;
  5660. + unsigned int status;
  5661. u8 div;
  5662. u16 mul;
  5663. - pllr = pmc_read(pmc, offset);
  5664. + regmap_read(regmap, offset, &pllr);
  5665. div = PLL_DIV(pllr);
  5666. mul = PLL_MUL(pllr, layout);
  5667. - if ((pmc_read(pmc, AT91_PMC_SR) & mask) &&
  5668. + regmap_read(regmap, AT91_PMC_SR, &status);
  5669. + if ((status & mask) &&
  5670. (div == pll->div && mul == pll->mul))
  5671. return 0;
  5672. if (characteristics->out)
  5673. out = characteristics->out[pll->range];
  5674. - if (characteristics->icpll) {
  5675. - icpr = pmc_read(pmc, AT91_PMC_PLLICPR) & ~PLL_ICPR_MASK(id);
  5676. - icpr |= (characteristics->icpll[pll->range] <<
  5677. - PLL_ICPR_SHIFT(id));
  5678. - pmc_write(pmc, AT91_PMC_PLLICPR, icpr);
  5679. - }
  5680. - pllr &= ~layout->pllr_mask;
  5681. - pllr |= layout->pllr_mask &
  5682. - (pll->div | (PLL_MAX_COUNT << PLL_COUNT_SHIFT) |
  5683. - (out << PLL_OUT_SHIFT) |
  5684. - ((pll->mul & layout->mul_mask) << layout->mul_shift));
  5685. - pmc_write(pmc, offset, pllr);
  5686. -
  5687. - while (!(pmc_read(pmc, AT91_PMC_SR) & mask)) {
  5688. - enable_irq(pll->irq);
  5689. - wait_event(pll->wait,
  5690. - pmc_read(pmc, AT91_PMC_SR) & mask);
  5691. - }
  5692. + if (characteristics->icpll)
  5693. + regmap_update_bits(regmap, AT91_PMC_PLLICPR, PLL_ICPR_MASK(id),
  5694. + characteristics->icpll[pll->range] << PLL_ICPR_SHIFT(id));
  5695. +
  5696. + regmap_update_bits(regmap, offset, layout->pllr_mask,
  5697. + pll->div | (PLL_MAX_COUNT << PLL_COUNT_SHIFT) |
  5698. + (out << PLL_OUT_SHIFT) |
  5699. + ((pll->mul & layout->mul_mask) << layout->mul_shift));
  5700. +
  5701. + while (!clk_pll_ready(regmap, pll->id))
  5702. + cpu_relax();
  5703. return 0;
  5704. }
  5705. @@ -130,32 +116,35 @@ static int clk_pll_prepare(struct clk_hw *hw)
  5706. static int clk_pll_is_prepared(struct clk_hw *hw)
  5707. {
  5708. struct clk_pll *pll = to_clk_pll(hw);
  5709. - struct at91_pmc *pmc = pll->pmc;
  5710. - return !!(pmc_read(pmc, AT91_PMC_SR) &
  5711. - PLL_STATUS_MASK(pll->id));
  5712. + return clk_pll_ready(pll->regmap, pll->id);
  5713. }
  5714. static void clk_pll_unprepare(struct clk_hw *hw)
  5715. {
  5716. struct clk_pll *pll = to_clk_pll(hw);
  5717. - struct at91_pmc *pmc = pll->pmc;
  5718. - const struct clk_pll_layout *layout = pll->layout;
  5719. - int offset = PLL_REG(pll->id);
  5720. - u32 tmp = pmc_read(pmc, offset) & ~(layout->pllr_mask);
  5721. + unsigned int mask = pll->layout->pllr_mask;
  5722. - pmc_write(pmc, offset, tmp);
  5723. + regmap_update_bits(pll->regmap, PLL_REG(pll->id), mask, ~mask);
  5724. }
  5725. static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
  5726. unsigned long parent_rate)
  5727. {
  5728. struct clk_pll *pll = to_clk_pll(hw);
  5729. + unsigned int pllr;
  5730. + u16 mul;
  5731. + u8 div;
  5732. - if (!pll->div || !pll->mul)
  5733. + regmap_read(pll->regmap, PLL_REG(pll->id), &pllr);
  5734. +
  5735. + div = PLL_DIV(pllr);
  5736. + mul = PLL_MUL(pllr, pll->layout);
  5737. +
  5738. + if (!div || !mul)
  5739. return 0;
  5740. - return (parent_rate / pll->div) * (pll->mul + 1);
  5741. + return (parent_rate / div) * (mul + 1);
  5742. }
  5743. static long clk_pll_get_best_div_mul(struct clk_pll *pll, unsigned long rate,
  5744. @@ -308,7 +297,7 @@ static const struct clk_ops pll_ops = {
  5745. };
  5746. static struct clk * __init
  5747. -at91_clk_register_pll(struct at91_pmc *pmc, unsigned int irq, const char *name,
  5748. +at91_clk_register_pll(struct regmap *regmap, const char *name,
  5749. const char *parent_name, u8 id,
  5750. const struct clk_pll_layout *layout,
  5751. const struct clk_pll_characteristics *characteristics)
  5752. @@ -316,9 +305,8 @@ at91_clk_register_pll(struct at91_pmc *pmc, unsigned int irq, const char *name,
  5753. struct clk_pll *pll;
  5754. struct clk *clk = NULL;
  5755. struct clk_init_data init;
  5756. - int ret;
  5757. int offset = PLL_REG(id);
  5758. - u32 tmp;
  5759. + unsigned int pllr;
  5760. if (id > PLL_MAX_ID)
  5761. return ERR_PTR(-EINVAL);
  5762. @@ -337,23 +325,13 @@ at91_clk_register_pll(struct at91_pmc *pmc, unsigned int irq, const char *name,
  5763. pll->hw.init = &init;
  5764. pll->layout = layout;
  5765. pll->characteristics = characteristics;
  5766. - pll->pmc = pmc;
  5767. - pll->irq = irq;
  5768. - tmp = pmc_read(pmc, offset) & layout->pllr_mask;
  5769. - pll->div = PLL_DIV(tmp);
  5770. - pll->mul = PLL_MUL(tmp, layout);
  5771. - init_waitqueue_head(&pll->wait);
  5772. - irq_set_status_flags(pll->irq, IRQ_NOAUTOEN);
  5773. - ret = request_irq(pll->irq, clk_pll_irq_handler, IRQF_TRIGGER_HIGH,
  5774. - id ? "clk-pllb" : "clk-plla", pll);
  5775. - if (ret) {
  5776. - kfree(pll);
  5777. - return ERR_PTR(ret);
  5778. - }
  5779. + pll->regmap = regmap;
  5780. + regmap_read(regmap, offset, &pllr);
  5781. + pll->div = PLL_DIV(pllr);
  5782. + pll->mul = PLL_MUL(pllr, layout);
  5783. clk = clk_register(NULL, &pll->hw);
  5784. if (IS_ERR(clk)) {
  5785. - free_irq(pll->irq, pll);
  5786. kfree(pll);
  5787. }
  5788. @@ -483,12 +461,12 @@ of_at91_clk_pll_get_characteristics(struct device_node *np)
  5789. }
  5790. static void __init
  5791. -of_at91_clk_pll_setup(struct device_node *np, struct at91_pmc *pmc,
  5792. +of_at91_clk_pll_setup(struct device_node *np,
  5793. const struct clk_pll_layout *layout)
  5794. {
  5795. u32 id;
  5796. - unsigned int irq;
  5797. struct clk *clk;
  5798. + struct regmap *regmap;
  5799. const char *parent_name;
  5800. const char *name = np->name;
  5801. struct clk_pll_characteristics *characteristics;
  5802. @@ -500,15 +478,15 @@ of_at91_clk_pll_setup(struct device_node *np, struct at91_pmc *pmc,
  5803. of_property_read_string(np, "clock-output-names", &name);
  5804. - characteristics = of_at91_clk_pll_get_characteristics(np);
  5805. - if (!characteristics)
  5806. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5807. + if (IS_ERR(regmap))
  5808. return;
  5809. - irq = irq_of_parse_and_map(np, 0);
  5810. - if (!irq)
  5811. + characteristics = of_at91_clk_pll_get_characteristics(np);
  5812. + if (!characteristics)
  5813. return;
  5814. - clk = at91_clk_register_pll(pmc, irq, name, parent_name, id, layout,
  5815. + clk = at91_clk_register_pll(regmap, name, parent_name, id, layout,
  5816. characteristics);
  5817. if (IS_ERR(clk))
  5818. goto out_free_characteristics;
  5819. @@ -520,26 +498,30 @@ of_at91_clk_pll_setup(struct device_node *np, struct at91_pmc *pmc,
  5820. kfree(characteristics);
  5821. }
  5822. -void __init of_at91rm9200_clk_pll_setup(struct device_node *np,
  5823. - struct at91_pmc *pmc)
  5824. +static void __init of_at91rm9200_clk_pll_setup(struct device_node *np)
  5825. {
  5826. - of_at91_clk_pll_setup(np, pmc, &at91rm9200_pll_layout);
  5827. + of_at91_clk_pll_setup(np, &at91rm9200_pll_layout);
  5828. }
  5829. +CLK_OF_DECLARE(at91rm9200_clk_pll, "atmel,at91rm9200-clk-pll",
  5830. + of_at91rm9200_clk_pll_setup);
  5831. -void __init of_at91sam9g45_clk_pll_setup(struct device_node *np,
  5832. - struct at91_pmc *pmc)
  5833. +static void __init of_at91sam9g45_clk_pll_setup(struct device_node *np)
  5834. {
  5835. - of_at91_clk_pll_setup(np, pmc, &at91sam9g45_pll_layout);
  5836. + of_at91_clk_pll_setup(np, &at91sam9g45_pll_layout);
  5837. }
  5838. +CLK_OF_DECLARE(at91sam9g45_clk_pll, "atmel,at91sam9g45-clk-pll",
  5839. + of_at91sam9g45_clk_pll_setup);
  5840. -void __init of_at91sam9g20_clk_pllb_setup(struct device_node *np,
  5841. - struct at91_pmc *pmc)
  5842. +static void __init of_at91sam9g20_clk_pllb_setup(struct device_node *np)
  5843. {
  5844. - of_at91_clk_pll_setup(np, pmc, &at91sam9g20_pllb_layout);
  5845. + of_at91_clk_pll_setup(np, &at91sam9g20_pllb_layout);
  5846. }
  5847. +CLK_OF_DECLARE(at91sam9g20_clk_pllb, "atmel,at91sam9g20-clk-pllb",
  5848. + of_at91sam9g20_clk_pllb_setup);
  5849. -void __init of_sama5d3_clk_pll_setup(struct device_node *np,
  5850. - struct at91_pmc *pmc)
  5851. +static void __init of_sama5d3_clk_pll_setup(struct device_node *np)
  5852. {
  5853. - of_at91_clk_pll_setup(np, pmc, &sama5d3_pll_layout);
  5854. + of_at91_clk_pll_setup(np, &sama5d3_pll_layout);
  5855. }
  5856. +CLK_OF_DECLARE(sama5d3_clk_pll, "atmel,sama5d3-clk-pll",
  5857. + of_sama5d3_clk_pll_setup);
  5858. diff --git a/drivers/clk/at91/clk-plldiv.c b/drivers/clk/at91/clk-plldiv.c
  5859. index ea226562bb40..2bed26481027 100644
  5860. --- a/drivers/clk/at91/clk-plldiv.c
  5861. +++ b/drivers/clk/at91/clk-plldiv.c
  5862. @@ -12,8 +12,8 @@
  5863. #include <linux/clkdev.h>
  5864. #include <linux/clk/at91_pmc.h>
  5865. #include <linux/of.h>
  5866. -#include <linux/of_address.h>
  5867. -#include <linux/io.h>
  5868. +#include <linux/mfd/syscon.h>
  5869. +#include <linux/regmap.h>
  5870. #include "pmc.h"
  5871. @@ -21,16 +21,18 @@
  5872. struct clk_plldiv {
  5873. struct clk_hw hw;
  5874. - struct at91_pmc *pmc;
  5875. + struct regmap *regmap;
  5876. };
  5877. static unsigned long clk_plldiv_recalc_rate(struct clk_hw *hw,
  5878. unsigned long parent_rate)
  5879. {
  5880. struct clk_plldiv *plldiv = to_clk_plldiv(hw);
  5881. - struct at91_pmc *pmc = plldiv->pmc;
  5882. + unsigned int mckr;
  5883. - if (pmc_read(pmc, AT91_PMC_MCKR) & AT91_PMC_PLLADIV2)
  5884. + regmap_read(plldiv->regmap, AT91_PMC_MCKR, &mckr);
  5885. +
  5886. + if (mckr & AT91_PMC_PLLADIV2)
  5887. return parent_rate / 2;
  5888. return parent_rate;
  5889. @@ -57,18 +59,12 @@ static int clk_plldiv_set_rate(struct clk_hw *hw, unsigned long rate,
  5890. unsigned long parent_rate)
  5891. {
  5892. struct clk_plldiv *plldiv = to_clk_plldiv(hw);
  5893. - struct at91_pmc *pmc = plldiv->pmc;
  5894. - u32 tmp;
  5895. - if (parent_rate != rate && (parent_rate / 2) != rate)
  5896. + if ((parent_rate != rate) && (parent_rate / 2 != rate))
  5897. return -EINVAL;
  5898. - pmc_lock(pmc);
  5899. - tmp = pmc_read(pmc, AT91_PMC_MCKR) & ~AT91_PMC_PLLADIV2;
  5900. - if ((parent_rate / 2) == rate)
  5901. - tmp |= AT91_PMC_PLLADIV2;
  5902. - pmc_write(pmc, AT91_PMC_MCKR, tmp);
  5903. - pmc_unlock(pmc);
  5904. + regmap_update_bits(plldiv->regmap, AT91_PMC_MCKR, AT91_PMC_PLLADIV2,
  5905. + parent_rate != rate ? AT91_PMC_PLLADIV2 : 0);
  5906. return 0;
  5907. }
  5908. @@ -80,7 +76,7 @@ static const struct clk_ops plldiv_ops = {
  5909. };
  5910. static struct clk * __init
  5911. -at91_clk_register_plldiv(struct at91_pmc *pmc, const char *name,
  5912. +at91_clk_register_plldiv(struct regmap *regmap, const char *name,
  5913. const char *parent_name)
  5914. {
  5915. struct clk_plldiv *plldiv;
  5916. @@ -98,7 +94,7 @@ at91_clk_register_plldiv(struct at91_pmc *pmc, const char *name,
  5917. init.flags = CLK_SET_RATE_GATE;
  5918. plldiv->hw.init = &init;
  5919. - plldiv->pmc = pmc;
  5920. + plldiv->regmap = regmap;
  5921. clk = clk_register(NULL, &plldiv->hw);
  5922. @@ -109,27 +105,27 @@ at91_clk_register_plldiv(struct at91_pmc *pmc, const char *name,
  5923. }
  5924. static void __init
  5925. -of_at91_clk_plldiv_setup(struct device_node *np, struct at91_pmc *pmc)
  5926. +of_at91sam9x5_clk_plldiv_setup(struct device_node *np)
  5927. {
  5928. struct clk *clk;
  5929. const char *parent_name;
  5930. const char *name = np->name;
  5931. + struct regmap *regmap;
  5932. parent_name = of_clk_get_parent_name(np, 0);
  5933. of_property_read_string(np, "clock-output-names", &name);
  5934. - clk = at91_clk_register_plldiv(pmc, name, parent_name);
  5935. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5936. + if (IS_ERR(regmap))
  5937. + return;
  5938. + clk = at91_clk_register_plldiv(regmap, name, parent_name);
  5939. if (IS_ERR(clk))
  5940. return;
  5941. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  5942. return;
  5943. }
  5944. -
  5945. -void __init of_at91sam9x5_clk_plldiv_setup(struct device_node *np,
  5946. - struct at91_pmc *pmc)
  5947. -{
  5948. - of_at91_clk_plldiv_setup(np, pmc);
  5949. -}
  5950. +CLK_OF_DECLARE(at91sam9x5_clk_plldiv, "atmel,at91sam9x5-clk-plldiv",
  5951. + of_at91sam9x5_clk_plldiv_setup);
  5952. diff --git a/drivers/clk/at91/clk-programmable.c b/drivers/clk/at91/clk-programmable.c
  5953. index 14b270b85fec..bc0be629671b 100644
  5954. --- a/drivers/clk/at91/clk-programmable.c
  5955. +++ b/drivers/clk/at91/clk-programmable.c
  5956. @@ -12,10 +12,8 @@
  5957. #include <linux/clkdev.h>
  5958. #include <linux/clk/at91_pmc.h>
  5959. #include <linux/of.h>
  5960. -#include <linux/of_address.h>
  5961. -#include <linux/io.h>
  5962. -#include <linux/wait.h>
  5963. -#include <linux/sched.h>
  5964. +#include <linux/mfd/syscon.h>
  5965. +#include <linux/regmap.h>
  5966. #include "pmc.h"
  5967. @@ -24,6 +22,7 @@
  5968. #define PROG_STATUS_MASK(id) (1 << ((id) + 8))
  5969. #define PROG_PRES_MASK 0x7
  5970. +#define PROG_PRES(layout, pckr) ((pckr >> layout->pres_shift) & PROG_PRES_MASK)
  5971. #define PROG_MAX_RM9200_CSS 3
  5972. struct clk_programmable_layout {
  5973. @@ -34,7 +33,7 @@ struct clk_programmable_layout {
  5974. struct clk_programmable {
  5975. struct clk_hw hw;
  5976. - struct at91_pmc *pmc;
  5977. + struct regmap *regmap;
  5978. u8 id;
  5979. const struct clk_programmable_layout *layout;
  5980. };
  5981. @@ -44,14 +43,12 @@ struct clk_programmable {
  5982. static unsigned long clk_programmable_recalc_rate(struct clk_hw *hw,
  5983. unsigned long parent_rate)
  5984. {
  5985. - u32 pres;
  5986. struct clk_programmable *prog = to_clk_programmable(hw);
  5987. - struct at91_pmc *pmc = prog->pmc;
  5988. - const struct clk_programmable_layout *layout = prog->layout;
  5989. + unsigned int pckr;
  5990. +
  5991. + regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
  5992. - pres = (pmc_read(pmc, AT91_PMC_PCKR(prog->id)) >> layout->pres_shift) &
  5993. - PROG_PRES_MASK;
  5994. - return parent_rate >> pres;
  5995. + return parent_rate >> PROG_PRES(prog->layout, pckr);
  5996. }
  5997. static int clk_programmable_determine_rate(struct clk_hw *hw,
  5998. @@ -101,36 +98,36 @@ static int clk_programmable_set_parent(struct clk_hw *hw, u8 index)
  5999. {
  6000. struct clk_programmable *prog = to_clk_programmable(hw);
  6001. const struct clk_programmable_layout *layout = prog->layout;
  6002. - struct at91_pmc *pmc = prog->pmc;
  6003. - u32 tmp = pmc_read(pmc, AT91_PMC_PCKR(prog->id)) & ~layout->css_mask;
  6004. + unsigned int mask = layout->css_mask;
  6005. + unsigned int pckr = 0;
  6006. if (layout->have_slck_mck)
  6007. - tmp &= AT91_PMC_CSSMCK_MCK;
  6008. + mask |= AT91_PMC_CSSMCK_MCK;
  6009. if (index > layout->css_mask) {
  6010. - if (index > PROG_MAX_RM9200_CSS && layout->have_slck_mck) {
  6011. - tmp |= AT91_PMC_CSSMCK_MCK;
  6012. - return 0;
  6013. - } else {
  6014. + if (index > PROG_MAX_RM9200_CSS && !layout->have_slck_mck)
  6015. return -EINVAL;
  6016. - }
  6017. +
  6018. + pckr |= AT91_PMC_CSSMCK_MCK;
  6019. }
  6020. - pmc_write(pmc, AT91_PMC_PCKR(prog->id), tmp | index);
  6021. + regmap_update_bits(prog->regmap, AT91_PMC_PCKR(prog->id), mask, pckr);
  6022. +
  6023. return 0;
  6024. }
  6025. static u8 clk_programmable_get_parent(struct clk_hw *hw)
  6026. {
  6027. - u32 tmp;
  6028. - u8 ret;
  6029. struct clk_programmable *prog = to_clk_programmable(hw);
  6030. - struct at91_pmc *pmc = prog->pmc;
  6031. const struct clk_programmable_layout *layout = prog->layout;
  6032. + unsigned int pckr;
  6033. + u8 ret;
  6034. +
  6035. + regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
  6036. +
  6037. + ret = pckr & layout->css_mask;
  6038. - tmp = pmc_read(pmc, AT91_PMC_PCKR(prog->id));
  6039. - ret = tmp & layout->css_mask;
  6040. - if (layout->have_slck_mck && (tmp & AT91_PMC_CSSMCK_MCK) && !ret)
  6041. + if (layout->have_slck_mck && (pckr & AT91_PMC_CSSMCK_MCK) && !ret)
  6042. ret = PROG_MAX_RM9200_CSS + 1;
  6043. return ret;
  6044. @@ -140,26 +137,27 @@ static int clk_programmable_set_rate(struct clk_hw *hw, unsigned long rate,
  6045. unsigned long parent_rate)
  6046. {
  6047. struct clk_programmable *prog = to_clk_programmable(hw);
  6048. - struct at91_pmc *pmc = prog->pmc;
  6049. const struct clk_programmable_layout *layout = prog->layout;
  6050. unsigned long div = parent_rate / rate;
  6051. + unsigned int pckr;
  6052. int shift = 0;
  6053. - u32 tmp = pmc_read(pmc, AT91_PMC_PCKR(prog->id)) &
  6054. - ~(PROG_PRES_MASK << layout->pres_shift);
  6055. +
  6056. + regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
  6057. if (!div)
  6058. return -EINVAL;
  6059. shift = fls(div) - 1;
  6060. - if (div != (1<<shift))
  6061. + if (div != (1 << shift))
  6062. return -EINVAL;
  6063. if (shift >= PROG_PRES_MASK)
  6064. return -EINVAL;
  6065. - pmc_write(pmc, AT91_PMC_PCKR(prog->id),
  6066. - tmp | (shift << layout->pres_shift));
  6067. + regmap_update_bits(prog->regmap, AT91_PMC_PCKR(prog->id),
  6068. + PROG_PRES_MASK << layout->pres_shift,
  6069. + shift << layout->pres_shift);
  6070. return 0;
  6071. }
  6072. @@ -173,7 +171,7 @@ static const struct clk_ops programmable_ops = {
  6073. };
  6074. static struct clk * __init
  6075. -at91_clk_register_programmable(struct at91_pmc *pmc,
  6076. +at91_clk_register_programmable(struct regmap *regmap,
  6077. const char *name, const char **parent_names,
  6078. u8 num_parents, u8 id,
  6079. const struct clk_programmable_layout *layout)
  6080. @@ -198,7 +196,7 @@ at91_clk_register_programmable(struct at91_pmc *pmc,
  6081. prog->id = id;
  6082. prog->layout = layout;
  6083. prog->hw.init = &init;
  6084. - prog->pmc = pmc;
  6085. + prog->regmap = regmap;
  6086. clk = clk_register(NULL, &prog->hw);
  6087. if (IS_ERR(clk))
  6088. @@ -226,7 +224,7 @@ static const struct clk_programmable_layout at91sam9x5_programmable_layout = {
  6089. };
  6090. static void __init
  6091. -of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  6092. +of_at91_clk_prog_setup(struct device_node *np,
  6093. const struct clk_programmable_layout *layout)
  6094. {
  6095. int num;
  6096. @@ -236,6 +234,7 @@ of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  6097. const char *parent_names[PROG_SOURCE_MAX];
  6098. const char *name;
  6099. struct device_node *progclknp;
  6100. + struct regmap *regmap;
  6101. num_parents = of_clk_get_parent_count(np);
  6102. if (num_parents <= 0 || num_parents > PROG_SOURCE_MAX)
  6103. @@ -247,6 +246,10 @@ of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  6104. if (!num || num > (PROG_ID_MAX + 1))
  6105. return;
  6106. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6107. + if (IS_ERR(regmap))
  6108. + return;
  6109. +
  6110. for_each_child_of_node(np, progclknp) {
  6111. if (of_property_read_u32(progclknp, "reg", &id))
  6112. continue;
  6113. @@ -254,7 +257,7 @@ of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  6114. if (of_property_read_string(np, "clock-output-names", &name))
  6115. name = progclknp->name;
  6116. - clk = at91_clk_register_programmable(pmc, name,
  6117. + clk = at91_clk_register_programmable(regmap, name,
  6118. parent_names, num_parents,
  6119. id, layout);
  6120. if (IS_ERR(clk))
  6121. @@ -265,20 +268,23 @@ of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  6122. }
  6123. -void __init of_at91rm9200_clk_prog_setup(struct device_node *np,
  6124. - struct at91_pmc *pmc)
  6125. +static void __init of_at91rm9200_clk_prog_setup(struct device_node *np)
  6126. {
  6127. - of_at91_clk_prog_setup(np, pmc, &at91rm9200_programmable_layout);
  6128. + of_at91_clk_prog_setup(np, &at91rm9200_programmable_layout);
  6129. }
  6130. +CLK_OF_DECLARE(at91rm9200_clk_prog, "atmel,at91rm9200-clk-programmable",
  6131. + of_at91rm9200_clk_prog_setup);
  6132. -void __init of_at91sam9g45_clk_prog_setup(struct device_node *np,
  6133. - struct at91_pmc *pmc)
  6134. +static void __init of_at91sam9g45_clk_prog_setup(struct device_node *np)
  6135. {
  6136. - of_at91_clk_prog_setup(np, pmc, &at91sam9g45_programmable_layout);
  6137. + of_at91_clk_prog_setup(np, &at91sam9g45_programmable_layout);
  6138. }
  6139. +CLK_OF_DECLARE(at91sam9g45_clk_prog, "atmel,at91sam9g45-clk-programmable",
  6140. + of_at91sam9g45_clk_prog_setup);
  6141. -void __init of_at91sam9x5_clk_prog_setup(struct device_node *np,
  6142. - struct at91_pmc *pmc)
  6143. +static void __init of_at91sam9x5_clk_prog_setup(struct device_node *np)
  6144. {
  6145. - of_at91_clk_prog_setup(np, pmc, &at91sam9x5_programmable_layout);
  6146. + of_at91_clk_prog_setup(np, &at91sam9x5_programmable_layout);
  6147. }
  6148. +CLK_OF_DECLARE(at91sam9x5_clk_prog, "atmel,at91sam9x5-clk-programmable",
  6149. + of_at91sam9x5_clk_prog_setup);
  6150. diff --git a/drivers/clk/at91/clk-slow.c b/drivers/clk/at91/clk-slow.c
  6151. index d0d5076a9b94..221c09684ba3 100644
  6152. --- a/drivers/clk/at91/clk-slow.c
  6153. +++ b/drivers/clk/at91/clk-slow.c
  6154. @@ -13,17 +13,11 @@
  6155. #include <linux/clk.h>
  6156. #include <linux/clk-provider.h>
  6157. #include <linux/clkdev.h>
  6158. -#include <linux/slab.h>
  6159. #include <linux/clk/at91_pmc.h>
  6160. #include <linux/delay.h>
  6161. #include <linux/of.h>
  6162. -#include <linux/of_address.h>
  6163. -#include <linux/of_irq.h>
  6164. -#include <linux/io.h>
  6165. -#include <linux/interrupt.h>
  6166. -#include <linux/irq.h>
  6167. -#include <linux/sched.h>
  6168. -#include <linux/wait.h>
  6169. +#include <linux/mfd/syscon.h>
  6170. +#include <linux/regmap.h>
  6171. #include "pmc.h"
  6172. #include "sckc.h"
  6173. @@ -59,7 +53,7 @@ struct clk_slow_rc_osc {
  6174. struct clk_sam9260_slow {
  6175. struct clk_hw hw;
  6176. - struct at91_pmc *pmc;
  6177. + struct regmap *regmap;
  6178. };
  6179. #define to_clk_sam9260_slow(hw) container_of(hw, struct clk_sam9260_slow, hw)
  6180. @@ -393,8 +387,11 @@ void __init of_at91sam9x5_clk_slow_setup(struct device_node *np,
  6181. static u8 clk_sam9260_slow_get_parent(struct clk_hw *hw)
  6182. {
  6183. struct clk_sam9260_slow *slowck = to_clk_sam9260_slow(hw);
  6184. + unsigned int status;
  6185. - return !!(pmc_read(slowck->pmc, AT91_PMC_SR) & AT91_PMC_OSCSEL);
  6186. + regmap_read(slowck->regmap, AT91_PMC_SR, &status);
  6187. +
  6188. + return status & AT91_PMC_OSCSEL ? 1 : 0;
  6189. }
  6190. static const struct clk_ops sam9260_slow_ops = {
  6191. @@ -402,7 +399,7 @@ static const struct clk_ops sam9260_slow_ops = {
  6192. };
  6193. static struct clk * __init
  6194. -at91_clk_register_sam9260_slow(struct at91_pmc *pmc,
  6195. +at91_clk_register_sam9260_slow(struct regmap *regmap,
  6196. const char *name,
  6197. const char **parent_names,
  6198. int num_parents)
  6199. @@ -411,7 +408,7 @@ at91_clk_register_sam9260_slow(struct at91_pmc *pmc,
  6200. struct clk *clk = NULL;
  6201. struct clk_init_data init;
  6202. - if (!pmc || !name)
  6203. + if (!name)
  6204. return ERR_PTR(-EINVAL);
  6205. if (!parent_names || !num_parents)
  6206. @@ -428,7 +425,7 @@ at91_clk_register_sam9260_slow(struct at91_pmc *pmc,
  6207. init.flags = 0;
  6208. slowck->hw.init = &init;
  6209. - slowck->pmc = pmc;
  6210. + slowck->regmap = regmap;
  6211. clk = clk_register(NULL, &slowck->hw);
  6212. if (IS_ERR(clk))
  6213. @@ -439,29 +436,34 @@ at91_clk_register_sam9260_slow(struct at91_pmc *pmc,
  6214. return clk;
  6215. }
  6216. -void __init of_at91sam9260_clk_slow_setup(struct device_node *np,
  6217. - struct at91_pmc *pmc)
  6218. +static void __init of_at91sam9260_clk_slow_setup(struct device_node *np)
  6219. {
  6220. struct clk *clk;
  6221. const char *parent_names[2];
  6222. int num_parents;
  6223. const char *name = np->name;
  6224. + struct regmap *regmap;
  6225. num_parents = of_clk_get_parent_count(np);
  6226. if (num_parents != 2)
  6227. return;
  6228. of_clk_parent_fill(np, parent_names, num_parents);
  6229. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6230. + if (IS_ERR(regmap))
  6231. + return;
  6232. of_property_read_string(np, "clock-output-names", &name);
  6233. - clk = at91_clk_register_sam9260_slow(pmc, name, parent_names,
  6234. + clk = at91_clk_register_sam9260_slow(regmap, name, parent_names,
  6235. num_parents);
  6236. if (IS_ERR(clk))
  6237. return;
  6238. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6239. }
  6240. +CLK_OF_DECLARE(at91sam9260_clk_slow, "atmel,at91sam9260-clk-slow",
  6241. + of_at91sam9260_clk_slow_setup);
  6242. /*
  6243. * FIXME: All slow clk users are not properly claiming it (get + prepare +
  6244. diff --git a/drivers/clk/at91/clk-smd.c b/drivers/clk/at91/clk-smd.c
  6245. index a7f8501cfa05..e6948a52005a 100644
  6246. --- a/drivers/clk/at91/clk-smd.c
  6247. +++ b/drivers/clk/at91/clk-smd.c
  6248. @@ -12,8 +12,8 @@
  6249. #include <linux/clkdev.h>
  6250. #include <linux/clk/at91_pmc.h>
  6251. #include <linux/of.h>
  6252. -#include <linux/of_address.h>
  6253. -#include <linux/io.h>
  6254. +#include <linux/mfd/syscon.h>
  6255. +#include <linux/regmap.h>
  6256. #include "pmc.h"
  6257. @@ -24,7 +24,7 @@
  6258. struct at91sam9x5_clk_smd {
  6259. struct clk_hw hw;
  6260. - struct at91_pmc *pmc;
  6261. + struct regmap *regmap;
  6262. };
  6263. #define to_at91sam9x5_clk_smd(hw) \
  6264. @@ -33,13 +33,13 @@ struct at91sam9x5_clk_smd {
  6265. static unsigned long at91sam9x5_clk_smd_recalc_rate(struct clk_hw *hw,
  6266. unsigned long parent_rate)
  6267. {
  6268. - u32 tmp;
  6269. - u8 smddiv;
  6270. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  6271. - struct at91_pmc *pmc = smd->pmc;
  6272. + unsigned int smdr;
  6273. + u8 smddiv;
  6274. +
  6275. + regmap_read(smd->regmap, AT91_PMC_SMD, &smdr);
  6276. + smddiv = (smdr & AT91_PMC_SMD_DIV) >> SMD_DIV_SHIFT;
  6277. - tmp = pmc_read(pmc, AT91_PMC_SMD);
  6278. - smddiv = (tmp & AT91_PMC_SMD_DIV) >> SMD_DIV_SHIFT;
  6279. return parent_rate / (smddiv + 1);
  6280. }
  6281. @@ -67,40 +67,38 @@ static long at91sam9x5_clk_smd_round_rate(struct clk_hw *hw, unsigned long rate,
  6282. static int at91sam9x5_clk_smd_set_parent(struct clk_hw *hw, u8 index)
  6283. {
  6284. - u32 tmp;
  6285. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  6286. - struct at91_pmc *pmc = smd->pmc;
  6287. if (index > 1)
  6288. return -EINVAL;
  6289. - tmp = pmc_read(pmc, AT91_PMC_SMD) & ~AT91_PMC_SMDS;
  6290. - if (index)
  6291. - tmp |= AT91_PMC_SMDS;
  6292. - pmc_write(pmc, AT91_PMC_SMD, tmp);
  6293. +
  6294. + regmap_update_bits(smd->regmap, AT91_PMC_SMD, AT91_PMC_SMDS,
  6295. + index ? AT91_PMC_SMDS : 0);
  6296. +
  6297. return 0;
  6298. }
  6299. static u8 at91sam9x5_clk_smd_get_parent(struct clk_hw *hw)
  6300. {
  6301. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  6302. - struct at91_pmc *pmc = smd->pmc;
  6303. + unsigned int smdr;
  6304. - return pmc_read(pmc, AT91_PMC_SMD) & AT91_PMC_SMDS;
  6305. + regmap_read(smd->regmap, AT91_PMC_SMD, &smdr);
  6306. +
  6307. + return smdr & AT91_PMC_SMDS;
  6308. }
  6309. static int at91sam9x5_clk_smd_set_rate(struct clk_hw *hw, unsigned long rate,
  6310. unsigned long parent_rate)
  6311. {
  6312. - u32 tmp;
  6313. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  6314. - struct at91_pmc *pmc = smd->pmc;
  6315. unsigned long div = parent_rate / rate;
  6316. if (parent_rate % rate || div < 1 || div > (SMD_MAX_DIV + 1))
  6317. return -EINVAL;
  6318. - tmp = pmc_read(pmc, AT91_PMC_SMD) & ~AT91_PMC_SMD_DIV;
  6319. - tmp |= (div - 1) << SMD_DIV_SHIFT;
  6320. - pmc_write(pmc, AT91_PMC_SMD, tmp);
  6321. +
  6322. + regmap_update_bits(smd->regmap, AT91_PMC_SMD, AT91_PMC_SMD_DIV,
  6323. + (div - 1) << SMD_DIV_SHIFT);
  6324. return 0;
  6325. }
  6326. @@ -114,7 +112,7 @@ static const struct clk_ops at91sam9x5_smd_ops = {
  6327. };
  6328. static struct clk * __init
  6329. -at91sam9x5_clk_register_smd(struct at91_pmc *pmc, const char *name,
  6330. +at91sam9x5_clk_register_smd(struct regmap *regmap, const char *name,
  6331. const char **parent_names, u8 num_parents)
  6332. {
  6333. struct at91sam9x5_clk_smd *smd;
  6334. @@ -132,7 +130,7 @@ at91sam9x5_clk_register_smd(struct at91_pmc *pmc, const char *name,
  6335. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
  6336. smd->hw.init = &init;
  6337. - smd->pmc = pmc;
  6338. + smd->regmap = regmap;
  6339. clk = clk_register(NULL, &smd->hw);
  6340. if (IS_ERR(clk))
  6341. @@ -141,13 +139,13 @@ at91sam9x5_clk_register_smd(struct at91_pmc *pmc, const char *name,
  6342. return clk;
  6343. }
  6344. -void __init of_at91sam9x5_clk_smd_setup(struct device_node *np,
  6345. - struct at91_pmc *pmc)
  6346. +static void __init of_at91sam9x5_clk_smd_setup(struct device_node *np)
  6347. {
  6348. struct clk *clk;
  6349. int num_parents;
  6350. const char *parent_names[SMD_SOURCE_MAX];
  6351. const char *name = np->name;
  6352. + struct regmap *regmap;
  6353. num_parents = of_clk_get_parent_count(np);
  6354. if (num_parents <= 0 || num_parents > SMD_SOURCE_MAX)
  6355. @@ -157,10 +155,16 @@ void __init of_at91sam9x5_clk_smd_setup(struct device_node *np,
  6356. of_property_read_string(np, "clock-output-names", &name);
  6357. - clk = at91sam9x5_clk_register_smd(pmc, name, parent_names,
  6358. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6359. + if (IS_ERR(regmap))
  6360. + return;
  6361. +
  6362. + clk = at91sam9x5_clk_register_smd(regmap, name, parent_names,
  6363. num_parents);
  6364. if (IS_ERR(clk))
  6365. return;
  6366. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6367. }
  6368. +CLK_OF_DECLARE(at91sam9x5_clk_smd, "atmel,at91sam9x5-clk-smd",
  6369. + of_at91sam9x5_clk_smd_setup);
  6370. diff --git a/drivers/clk/at91/clk-system.c b/drivers/clk/at91/clk-system.c
  6371. index 3f5314344286..8f35d8172909 100644
  6372. --- a/drivers/clk/at91/clk-system.c
  6373. +++ b/drivers/clk/at91/clk-system.c
  6374. @@ -12,13 +12,8 @@
  6375. #include <linux/clkdev.h>
  6376. #include <linux/clk/at91_pmc.h>
  6377. #include <linux/of.h>
  6378. -#include <linux/of_address.h>
  6379. -#include <linux/io.h>
  6380. -#include <linux/irq.h>
  6381. -#include <linux/of_irq.h>
  6382. -#include <linux/interrupt.h>
  6383. -#include <linux/wait.h>
  6384. -#include <linux/sched.h>
  6385. +#include <linux/mfd/syscon.h>
  6386. +#include <linux/regmap.h>
  6387. #include "pmc.h"
  6388. @@ -29,9 +24,7 @@
  6389. #define to_clk_system(hw) container_of(hw, struct clk_system, hw)
  6390. struct clk_system {
  6391. struct clk_hw hw;
  6392. - struct at91_pmc *pmc;
  6393. - unsigned int irq;
  6394. - wait_queue_head_t wait;
  6395. + struct regmap *regmap;
  6396. u8 id;
  6397. };
  6398. @@ -39,58 +32,54 @@ static inline int is_pck(int id)
  6399. {
  6400. return (id >= 8) && (id <= 15);
  6401. }
  6402. -static irqreturn_t clk_system_irq_handler(int irq, void *dev_id)
  6403. +
  6404. +static inline bool clk_system_ready(struct regmap *regmap, int id)
  6405. {
  6406. - struct clk_system *sys = (struct clk_system *)dev_id;
  6407. + unsigned int status;
  6408. - wake_up(&sys->wait);
  6409. - disable_irq_nosync(sys->irq);
  6410. + regmap_read(regmap, AT91_PMC_SR, &status);
  6411. - return IRQ_HANDLED;
  6412. + return status & (1 << id) ? 1 : 0;
  6413. }
  6414. static int clk_system_prepare(struct clk_hw *hw)
  6415. {
  6416. struct clk_system *sys = to_clk_system(hw);
  6417. - struct at91_pmc *pmc = sys->pmc;
  6418. - u32 mask = 1 << sys->id;
  6419. - pmc_write(pmc, AT91_PMC_SCER, mask);
  6420. + regmap_write(sys->regmap, AT91_PMC_SCER, 1 << sys->id);
  6421. if (!is_pck(sys->id))
  6422. return 0;
  6423. - while (!(pmc_read(pmc, AT91_PMC_SR) & mask)) {
  6424. - if (sys->irq) {
  6425. - enable_irq(sys->irq);
  6426. - wait_event(sys->wait,
  6427. - pmc_read(pmc, AT91_PMC_SR) & mask);
  6428. - } else
  6429. - cpu_relax();
  6430. - }
  6431. + while (!clk_system_ready(sys->regmap, sys->id))
  6432. + cpu_relax();
  6433. +
  6434. return 0;
  6435. }
  6436. static void clk_system_unprepare(struct clk_hw *hw)
  6437. {
  6438. struct clk_system *sys = to_clk_system(hw);
  6439. - struct at91_pmc *pmc = sys->pmc;
  6440. - pmc_write(pmc, AT91_PMC_SCDR, 1 << sys->id);
  6441. + regmap_write(sys->regmap, AT91_PMC_SCDR, 1 << sys->id);
  6442. }
  6443. static int clk_system_is_prepared(struct clk_hw *hw)
  6444. {
  6445. struct clk_system *sys = to_clk_system(hw);
  6446. - struct at91_pmc *pmc = sys->pmc;
  6447. + unsigned int status;
  6448. +
  6449. + regmap_read(sys->regmap, AT91_PMC_SCSR, &status);
  6450. - if (!(pmc_read(pmc, AT91_PMC_SCSR) & (1 << sys->id)))
  6451. + if (!(status & (1 << sys->id)))
  6452. return 0;
  6453. if (!is_pck(sys->id))
  6454. return 1;
  6455. - return !!(pmc_read(pmc, AT91_PMC_SR) & (1 << sys->id));
  6456. + regmap_read(sys->regmap, AT91_PMC_SR, &status);
  6457. +
  6458. + return status & (1 << sys->id) ? 1 : 0;
  6459. }
  6460. static const struct clk_ops system_ops = {
  6461. @@ -100,13 +89,12 @@ static const struct clk_ops system_ops = {
  6462. };
  6463. static struct clk * __init
  6464. -at91_clk_register_system(struct at91_pmc *pmc, const char *name,
  6465. - const char *parent_name, u8 id, int irq)
  6466. +at91_clk_register_system(struct regmap *regmap, const char *name,
  6467. + const char *parent_name, u8 id)
  6468. {
  6469. struct clk_system *sys;
  6470. struct clk *clk = NULL;
  6471. struct clk_init_data init;
  6472. - int ret;
  6473. if (!parent_name || id > SYSTEM_MAX_ID)
  6474. return ERR_PTR(-EINVAL);
  6475. @@ -123,44 +111,33 @@ at91_clk_register_system(struct at91_pmc *pmc, const char *name,
  6476. sys->id = id;
  6477. sys->hw.init = &init;
  6478. - sys->pmc = pmc;
  6479. - sys->irq = irq;
  6480. - if (irq) {
  6481. - init_waitqueue_head(&sys->wait);
  6482. - irq_set_status_flags(sys->irq, IRQ_NOAUTOEN);
  6483. - ret = request_irq(sys->irq, clk_system_irq_handler,
  6484. - IRQF_TRIGGER_HIGH, name, sys);
  6485. - if (ret) {
  6486. - kfree(sys);
  6487. - return ERR_PTR(ret);
  6488. - }
  6489. - }
  6490. + sys->regmap = regmap;
  6491. clk = clk_register(NULL, &sys->hw);
  6492. - if (IS_ERR(clk)) {
  6493. - if (irq)
  6494. - free_irq(sys->irq, sys);
  6495. + if (IS_ERR(clk))
  6496. kfree(sys);
  6497. - }
  6498. return clk;
  6499. }
  6500. -static void __init
  6501. -of_at91_clk_sys_setup(struct device_node *np, struct at91_pmc *pmc)
  6502. +static void __init of_at91rm9200_clk_sys_setup(struct device_node *np)
  6503. {
  6504. int num;
  6505. - int irq = 0;
  6506. u32 id;
  6507. struct clk *clk;
  6508. const char *name;
  6509. struct device_node *sysclknp;
  6510. const char *parent_name;
  6511. + struct regmap *regmap;
  6512. num = of_get_child_count(np);
  6513. if (num > (SYSTEM_MAX_ID + 1))
  6514. return;
  6515. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6516. + if (IS_ERR(regmap))
  6517. + return;
  6518. +
  6519. for_each_child_of_node(np, sysclknp) {
  6520. if (of_property_read_u32(sysclknp, "reg", &id))
  6521. continue;
  6522. @@ -168,21 +145,14 @@ of_at91_clk_sys_setup(struct device_node *np, struct at91_pmc *pmc)
  6523. if (of_property_read_string(np, "clock-output-names", &name))
  6524. name = sysclknp->name;
  6525. - if (is_pck(id))
  6526. - irq = irq_of_parse_and_map(sysclknp, 0);
  6527. -
  6528. parent_name = of_clk_get_parent_name(sysclknp, 0);
  6529. - clk = at91_clk_register_system(pmc, name, parent_name, id, irq);
  6530. + clk = at91_clk_register_system(regmap, name, parent_name, id);
  6531. if (IS_ERR(clk))
  6532. continue;
  6533. of_clk_add_provider(sysclknp, of_clk_src_simple_get, clk);
  6534. }
  6535. }
  6536. -
  6537. -void __init of_at91rm9200_clk_sys_setup(struct device_node *np,
  6538. - struct at91_pmc *pmc)
  6539. -{
  6540. - of_at91_clk_sys_setup(np, pmc);
  6541. -}
  6542. +CLK_OF_DECLARE(at91rm9200_clk_sys, "atmel,at91rm9200-clk-system",
  6543. + of_at91rm9200_clk_sys_setup);
  6544. diff --git a/drivers/clk/at91/clk-usb.c b/drivers/clk/at91/clk-usb.c
  6545. index 8ab8502778a2..650ca45892c0 100644
  6546. --- a/drivers/clk/at91/clk-usb.c
  6547. +++ b/drivers/clk/at91/clk-usb.c
  6548. @@ -12,8 +12,8 @@
  6549. #include <linux/clkdev.h>
  6550. #include <linux/clk/at91_pmc.h>
  6551. #include <linux/of.h>
  6552. -#include <linux/of_address.h>
  6553. -#include <linux/io.h>
  6554. +#include <linux/mfd/syscon.h>
  6555. +#include <linux/regmap.h>
  6556. #include "pmc.h"
  6557. @@ -27,7 +27,7 @@
  6558. struct at91sam9x5_clk_usb {
  6559. struct clk_hw hw;
  6560. - struct at91_pmc *pmc;
  6561. + struct regmap *regmap;
  6562. };
  6563. #define to_at91sam9x5_clk_usb(hw) \
  6564. @@ -35,7 +35,7 @@ struct at91sam9x5_clk_usb {
  6565. struct at91rm9200_clk_usb {
  6566. struct clk_hw hw;
  6567. - struct at91_pmc *pmc;
  6568. + struct regmap *regmap;
  6569. u32 divisors[4];
  6570. };
  6571. @@ -45,13 +45,12 @@ struct at91rm9200_clk_usb {
  6572. static unsigned long at91sam9x5_clk_usb_recalc_rate(struct clk_hw *hw,
  6573. unsigned long parent_rate)
  6574. {
  6575. - u32 tmp;
  6576. - u8 usbdiv;
  6577. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6578. - struct at91_pmc *pmc = usb->pmc;
  6579. + unsigned int usbr;
  6580. + u8 usbdiv;
  6581. - tmp = pmc_read(pmc, AT91_PMC_USB);
  6582. - usbdiv = (tmp & AT91_PMC_OHCIUSBDIV) >> SAM9X5_USB_DIV_SHIFT;
  6583. + regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
  6584. + usbdiv = (usbr & AT91_PMC_OHCIUSBDIV) >> SAM9X5_USB_DIV_SHIFT;
  6585. return DIV_ROUND_CLOSEST(parent_rate, (usbdiv + 1));
  6586. }
  6587. @@ -109,33 +108,31 @@ static int at91sam9x5_clk_usb_determine_rate(struct clk_hw *hw,
  6588. static int at91sam9x5_clk_usb_set_parent(struct clk_hw *hw, u8 index)
  6589. {
  6590. - u32 tmp;
  6591. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6592. - struct at91_pmc *pmc = usb->pmc;
  6593. if (index > 1)
  6594. return -EINVAL;
  6595. - tmp = pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_USBS;
  6596. - if (index)
  6597. - tmp |= AT91_PMC_USBS;
  6598. - pmc_write(pmc, AT91_PMC_USB, tmp);
  6599. +
  6600. + regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS,
  6601. + index ? AT91_PMC_USBS : 0);
  6602. +
  6603. return 0;
  6604. }
  6605. static u8 at91sam9x5_clk_usb_get_parent(struct clk_hw *hw)
  6606. {
  6607. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6608. - struct at91_pmc *pmc = usb->pmc;
  6609. + unsigned int usbr;
  6610. - return pmc_read(pmc, AT91_PMC_USB) & AT91_PMC_USBS;
  6611. + regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
  6612. +
  6613. + return usbr & AT91_PMC_USBS;
  6614. }
  6615. static int at91sam9x5_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  6616. unsigned long parent_rate)
  6617. {
  6618. - u32 tmp;
  6619. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6620. - struct at91_pmc *pmc = usb->pmc;
  6621. unsigned long div;
  6622. if (!rate)
  6623. @@ -145,9 +142,8 @@ static int at91sam9x5_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  6624. if (div > SAM9X5_USB_MAX_DIV + 1 || !div)
  6625. return -EINVAL;
  6626. - tmp = pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_OHCIUSBDIV;
  6627. - tmp |= (div - 1) << SAM9X5_USB_DIV_SHIFT;
  6628. - pmc_write(pmc, AT91_PMC_USB, tmp);
  6629. + regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_OHCIUSBDIV,
  6630. + (div - 1) << SAM9X5_USB_DIV_SHIFT);
  6631. return 0;
  6632. }
  6633. @@ -163,28 +159,28 @@ static const struct clk_ops at91sam9x5_usb_ops = {
  6634. static int at91sam9n12_clk_usb_enable(struct clk_hw *hw)
  6635. {
  6636. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6637. - struct at91_pmc *pmc = usb->pmc;
  6638. - pmc_write(pmc, AT91_PMC_USB,
  6639. - pmc_read(pmc, AT91_PMC_USB) | AT91_PMC_USBS);
  6640. + regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS,
  6641. + AT91_PMC_USBS);
  6642. +
  6643. return 0;
  6644. }
  6645. static void at91sam9n12_clk_usb_disable(struct clk_hw *hw)
  6646. {
  6647. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6648. - struct at91_pmc *pmc = usb->pmc;
  6649. - pmc_write(pmc, AT91_PMC_USB,
  6650. - pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_USBS);
  6651. + regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS, 0);
  6652. }
  6653. static int at91sam9n12_clk_usb_is_enabled(struct clk_hw *hw)
  6654. {
  6655. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6656. - struct at91_pmc *pmc = usb->pmc;
  6657. + unsigned int usbr;
  6658. - return !!(pmc_read(pmc, AT91_PMC_USB) & AT91_PMC_USBS);
  6659. + regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
  6660. +
  6661. + return usbr & AT91_PMC_USBS;
  6662. }
  6663. static const struct clk_ops at91sam9n12_usb_ops = {
  6664. @@ -197,7 +193,7 @@ static const struct clk_ops at91sam9n12_usb_ops = {
  6665. };
  6666. static struct clk * __init
  6667. -at91sam9x5_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6668. +at91sam9x5_clk_register_usb(struct regmap *regmap, const char *name,
  6669. const char **parent_names, u8 num_parents)
  6670. {
  6671. struct at91sam9x5_clk_usb *usb;
  6672. @@ -216,7 +212,7 @@ at91sam9x5_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6673. CLK_SET_RATE_PARENT;
  6674. usb->hw.init = &init;
  6675. - usb->pmc = pmc;
  6676. + usb->regmap = regmap;
  6677. clk = clk_register(NULL, &usb->hw);
  6678. if (IS_ERR(clk))
  6679. @@ -226,7 +222,7 @@ at91sam9x5_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6680. }
  6681. static struct clk * __init
  6682. -at91sam9n12_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6683. +at91sam9n12_clk_register_usb(struct regmap *regmap, const char *name,
  6684. const char *parent_name)
  6685. {
  6686. struct at91sam9x5_clk_usb *usb;
  6687. @@ -244,7 +240,7 @@ at91sam9n12_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6688. init.flags = CLK_SET_RATE_GATE | CLK_SET_RATE_PARENT;
  6689. usb->hw.init = &init;
  6690. - usb->pmc = pmc;
  6691. + usb->regmap = regmap;
  6692. clk = clk_register(NULL, &usb->hw);
  6693. if (IS_ERR(clk))
  6694. @@ -257,12 +253,12 @@ static unsigned long at91rm9200_clk_usb_recalc_rate(struct clk_hw *hw,
  6695. unsigned long parent_rate)
  6696. {
  6697. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  6698. - struct at91_pmc *pmc = usb->pmc;
  6699. - u32 tmp;
  6700. + unsigned int pllbr;
  6701. u8 usbdiv;
  6702. - tmp = pmc_read(pmc, AT91_CKGR_PLLBR);
  6703. - usbdiv = (tmp & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
  6704. + regmap_read(usb->regmap, AT91_CKGR_PLLBR, &pllbr);
  6705. +
  6706. + usbdiv = (pllbr & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
  6707. if (usb->divisors[usbdiv])
  6708. return parent_rate / usb->divisors[usbdiv];
  6709. @@ -310,10 +306,8 @@ static long at91rm9200_clk_usb_round_rate(struct clk_hw *hw, unsigned long rate,
  6710. static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  6711. unsigned long parent_rate)
  6712. {
  6713. - u32 tmp;
  6714. int i;
  6715. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  6716. - struct at91_pmc *pmc = usb->pmc;
  6717. unsigned long div;
  6718. if (!rate)
  6719. @@ -323,10 +317,10 @@ static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  6720. for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
  6721. if (usb->divisors[i] == div) {
  6722. - tmp = pmc_read(pmc, AT91_CKGR_PLLBR) &
  6723. - ~AT91_PMC_USBDIV;
  6724. - tmp |= i << RM9200_USB_DIV_SHIFT;
  6725. - pmc_write(pmc, AT91_CKGR_PLLBR, tmp);
  6726. + regmap_update_bits(usb->regmap, AT91_CKGR_PLLBR,
  6727. + AT91_PMC_USBDIV,
  6728. + i << RM9200_USB_DIV_SHIFT);
  6729. +
  6730. return 0;
  6731. }
  6732. }
  6733. @@ -341,7 +335,7 @@ static const struct clk_ops at91rm9200_usb_ops = {
  6734. };
  6735. static struct clk * __init
  6736. -at91rm9200_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6737. +at91rm9200_clk_register_usb(struct regmap *regmap, const char *name,
  6738. const char *parent_name, const u32 *divisors)
  6739. {
  6740. struct at91rm9200_clk_usb *usb;
  6741. @@ -359,7 +353,7 @@ at91rm9200_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6742. init.flags = CLK_SET_RATE_PARENT;
  6743. usb->hw.init = &init;
  6744. - usb->pmc = pmc;
  6745. + usb->regmap = regmap;
  6746. memcpy(usb->divisors, divisors, sizeof(usb->divisors));
  6747. clk = clk_register(NULL, &usb->hw);
  6748. @@ -369,13 +363,13 @@ at91rm9200_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6749. return clk;
  6750. }
  6751. -void __init of_at91sam9x5_clk_usb_setup(struct device_node *np,
  6752. - struct at91_pmc *pmc)
  6753. +static void __init of_at91sam9x5_clk_usb_setup(struct device_node *np)
  6754. {
  6755. struct clk *clk;
  6756. int num_parents;
  6757. const char *parent_names[USB_SOURCE_MAX];
  6758. const char *name = np->name;
  6759. + struct regmap *regmap;
  6760. num_parents = of_clk_get_parent_count(np);
  6761. if (num_parents <= 0 || num_parents > USB_SOURCE_MAX)
  6762. @@ -385,19 +379,26 @@ void __init of_at91sam9x5_clk_usb_setup(struct device_node *np,
  6763. of_property_read_string(np, "clock-output-names", &name);
  6764. - clk = at91sam9x5_clk_register_usb(pmc, name, parent_names, num_parents);
  6765. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6766. + if (IS_ERR(regmap))
  6767. + return;
  6768. +
  6769. + clk = at91sam9x5_clk_register_usb(regmap, name, parent_names,
  6770. + num_parents);
  6771. if (IS_ERR(clk))
  6772. return;
  6773. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6774. }
  6775. +CLK_OF_DECLARE(at91sam9x5_clk_usb, "atmel,at91sam9x5-clk-usb",
  6776. + of_at91sam9x5_clk_usb_setup);
  6777. -void __init of_at91sam9n12_clk_usb_setup(struct device_node *np,
  6778. - struct at91_pmc *pmc)
  6779. +static void __init of_at91sam9n12_clk_usb_setup(struct device_node *np)
  6780. {
  6781. struct clk *clk;
  6782. const char *parent_name;
  6783. const char *name = np->name;
  6784. + struct regmap *regmap;
  6785. parent_name = of_clk_get_parent_name(np, 0);
  6786. if (!parent_name)
  6787. @@ -405,20 +406,26 @@ void __init of_at91sam9n12_clk_usb_setup(struct device_node *np,
  6788. of_property_read_string(np, "clock-output-names", &name);
  6789. - clk = at91sam9n12_clk_register_usb(pmc, name, parent_name);
  6790. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6791. + if (IS_ERR(regmap))
  6792. + return;
  6793. +
  6794. + clk = at91sam9n12_clk_register_usb(regmap, name, parent_name);
  6795. if (IS_ERR(clk))
  6796. return;
  6797. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6798. }
  6799. +CLK_OF_DECLARE(at91sam9n12_clk_usb, "atmel,at91sam9n12-clk-usb",
  6800. + of_at91sam9n12_clk_usb_setup);
  6801. -void __init of_at91rm9200_clk_usb_setup(struct device_node *np,
  6802. - struct at91_pmc *pmc)
  6803. +static void __init of_at91rm9200_clk_usb_setup(struct device_node *np)
  6804. {
  6805. struct clk *clk;
  6806. const char *parent_name;
  6807. const char *name = np->name;
  6808. u32 divisors[4] = {0, 0, 0, 0};
  6809. + struct regmap *regmap;
  6810. parent_name = of_clk_get_parent_name(np, 0);
  6811. if (!parent_name)
  6812. @@ -430,9 +437,15 @@ void __init of_at91rm9200_clk_usb_setup(struct device_node *np,
  6813. of_property_read_string(np, "clock-output-names", &name);
  6814. - clk = at91rm9200_clk_register_usb(pmc, name, parent_name, divisors);
  6815. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6816. + if (IS_ERR(regmap))
  6817. + return;
  6818. +
  6819. + clk = at91rm9200_clk_register_usb(regmap, name, parent_name, divisors);
  6820. if (IS_ERR(clk))
  6821. return;
  6822. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6823. }
  6824. +CLK_OF_DECLARE(at91rm9200_clk_usb, "atmel,at91rm9200-clk-usb",
  6825. + of_at91rm9200_clk_usb_setup);
  6826. diff --git a/drivers/clk/at91/clk-utmi.c b/drivers/clk/at91/clk-utmi.c
  6827. index ca561e90a60f..61fcf399e58c 100644
  6828. --- a/drivers/clk/at91/clk-utmi.c
  6829. +++ b/drivers/clk/at91/clk-utmi.c
  6830. @@ -11,14 +11,9 @@
  6831. #include <linux/clk-provider.h>
  6832. #include <linux/clkdev.h>
  6833. #include <linux/clk/at91_pmc.h>
  6834. -#include <linux/interrupt.h>
  6835. -#include <linux/irq.h>
  6836. #include <linux/of.h>
  6837. -#include <linux/of_address.h>
  6838. -#include <linux/of_irq.h>
  6839. -#include <linux/io.h>
  6840. -#include <linux/sched.h>
  6841. -#include <linux/wait.h>
  6842. +#include <linux/mfd/syscon.h>
  6843. +#include <linux/regmap.h>
  6844. #include "pmc.h"
  6845. @@ -26,37 +21,30 @@
  6846. struct clk_utmi {
  6847. struct clk_hw hw;
  6848. - struct at91_pmc *pmc;
  6849. - unsigned int irq;
  6850. - wait_queue_head_t wait;
  6851. + struct regmap *regmap;
  6852. };
  6853. #define to_clk_utmi(hw) container_of(hw, struct clk_utmi, hw)
  6854. -static irqreturn_t clk_utmi_irq_handler(int irq, void *dev_id)
  6855. +static inline bool clk_utmi_ready(struct regmap *regmap)
  6856. {
  6857. - struct clk_utmi *utmi = (struct clk_utmi *)dev_id;
  6858. + unsigned int status;
  6859. - wake_up(&utmi->wait);
  6860. - disable_irq_nosync(utmi->irq);
  6861. + regmap_read(regmap, AT91_PMC_SR, &status);
  6862. - return IRQ_HANDLED;
  6863. + return status & AT91_PMC_LOCKU;
  6864. }
  6865. static int clk_utmi_prepare(struct clk_hw *hw)
  6866. {
  6867. struct clk_utmi *utmi = to_clk_utmi(hw);
  6868. - struct at91_pmc *pmc = utmi->pmc;
  6869. - u32 tmp = pmc_read(pmc, AT91_CKGR_UCKR) | AT91_PMC_UPLLEN |
  6870. - AT91_PMC_UPLLCOUNT | AT91_PMC_BIASEN;
  6871. + unsigned int uckr = AT91_PMC_UPLLEN | AT91_PMC_UPLLCOUNT |
  6872. + AT91_PMC_BIASEN;
  6873. - pmc_write(pmc, AT91_CKGR_UCKR, tmp);
  6874. + regmap_update_bits(utmi->regmap, AT91_CKGR_UCKR, uckr, uckr);
  6875. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_LOCKU)) {
  6876. - enable_irq(utmi->irq);
  6877. - wait_event(utmi->wait,
  6878. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_LOCKU);
  6879. - }
  6880. + while (!clk_utmi_ready(utmi->regmap))
  6881. + cpu_relax();
  6882. return 0;
  6883. }
  6884. @@ -64,18 +52,15 @@ static int clk_utmi_prepare(struct clk_hw *hw)
  6885. static int clk_utmi_is_prepared(struct clk_hw *hw)
  6886. {
  6887. struct clk_utmi *utmi = to_clk_utmi(hw);
  6888. - struct at91_pmc *pmc = utmi->pmc;
  6889. - return !!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_LOCKU);
  6890. + return clk_utmi_ready(utmi->regmap);
  6891. }
  6892. static void clk_utmi_unprepare(struct clk_hw *hw)
  6893. {
  6894. struct clk_utmi *utmi = to_clk_utmi(hw);
  6895. - struct at91_pmc *pmc = utmi->pmc;
  6896. - u32 tmp = pmc_read(pmc, AT91_CKGR_UCKR) & ~AT91_PMC_UPLLEN;
  6897. - pmc_write(pmc, AT91_CKGR_UCKR, tmp);
  6898. + regmap_update_bits(utmi->regmap, AT91_CKGR_UCKR, AT91_PMC_UPLLEN, 0);
  6899. }
  6900. static unsigned long clk_utmi_recalc_rate(struct clk_hw *hw,
  6901. @@ -93,10 +78,9 @@ static const struct clk_ops utmi_ops = {
  6902. };
  6903. static struct clk * __init
  6904. -at91_clk_register_utmi(struct at91_pmc *pmc, unsigned int irq,
  6905. +at91_clk_register_utmi(struct regmap *regmap,
  6906. const char *name, const char *parent_name)
  6907. {
  6908. - int ret;
  6909. struct clk_utmi *utmi;
  6910. struct clk *clk = NULL;
  6911. struct clk_init_data init;
  6912. @@ -112,52 +96,36 @@ at91_clk_register_utmi(struct at91_pmc *pmc, unsigned int irq,
  6913. init.flags = CLK_SET_RATE_GATE;
  6914. utmi->hw.init = &init;
  6915. - utmi->pmc = pmc;
  6916. - utmi->irq = irq;
  6917. - init_waitqueue_head(&utmi->wait);
  6918. - irq_set_status_flags(utmi->irq, IRQ_NOAUTOEN);
  6919. - ret = request_irq(utmi->irq, clk_utmi_irq_handler,
  6920. - IRQF_TRIGGER_HIGH, "clk-utmi", utmi);
  6921. - if (ret) {
  6922. - kfree(utmi);
  6923. - return ERR_PTR(ret);
  6924. - }
  6925. + utmi->regmap = regmap;
  6926. clk = clk_register(NULL, &utmi->hw);
  6927. - if (IS_ERR(clk)) {
  6928. - free_irq(utmi->irq, utmi);
  6929. + if (IS_ERR(clk))
  6930. kfree(utmi);
  6931. - }
  6932. return clk;
  6933. }
  6934. -static void __init
  6935. -of_at91_clk_utmi_setup(struct device_node *np, struct at91_pmc *pmc)
  6936. +static void __init of_at91sam9x5_clk_utmi_setup(struct device_node *np)
  6937. {
  6938. - unsigned int irq;
  6939. struct clk *clk;
  6940. const char *parent_name;
  6941. const char *name = np->name;
  6942. + struct regmap *regmap;
  6943. parent_name = of_clk_get_parent_name(np, 0);
  6944. of_property_read_string(np, "clock-output-names", &name);
  6945. - irq = irq_of_parse_and_map(np, 0);
  6946. - if (!irq)
  6947. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6948. + if (IS_ERR(regmap))
  6949. return;
  6950. - clk = at91_clk_register_utmi(pmc, irq, name, parent_name);
  6951. + clk = at91_clk_register_utmi(regmap, name, parent_name);
  6952. if (IS_ERR(clk))
  6953. return;
  6954. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6955. return;
  6956. }
  6957. -
  6958. -void __init of_at91sam9x5_clk_utmi_setup(struct device_node *np,
  6959. - struct at91_pmc *pmc)
  6960. -{
  6961. - of_at91_clk_utmi_setup(np, pmc);
  6962. -}
  6963. +CLK_OF_DECLARE(at91sam9x5_clk_utmi, "atmel,at91sam9x5-clk-utmi",
  6964. + of_at91sam9x5_clk_utmi_setup);
  6965. diff --git a/drivers/clk/at91/pmc.c b/drivers/clk/at91/pmc.c
  6966. index 8476b570779b..526df5ba042d 100644
  6967. --- a/drivers/clk/at91/pmc.c
  6968. +++ b/drivers/clk/at91/pmc.c
  6969. @@ -12,36 +12,13 @@
  6970. #include <linux/clkdev.h>
  6971. #include <linux/clk/at91_pmc.h>
  6972. #include <linux/of.h>
  6973. -#include <linux/of_address.h>
  6974. -#include <linux/io.h>
  6975. -#include <linux/interrupt.h>
  6976. -#include <linux/irq.h>
  6977. -#include <linux/irqchip/chained_irq.h>
  6978. -#include <linux/irqdomain.h>
  6979. -#include <linux/of_irq.h>
  6980. +#include <linux/mfd/syscon.h>
  6981. +#include <linux/regmap.h>
  6982. #include <asm/proc-fns.h>
  6983. #include "pmc.h"
  6984. -void __iomem *at91_pmc_base;
  6985. -EXPORT_SYMBOL_GPL(at91_pmc_base);
  6986. -
  6987. -void at91rm9200_idle(void)
  6988. -{
  6989. - /*
  6990. - * Disable the processor clock. The processor will be automatically
  6991. - * re-enabled by an interrupt or by a reset.
  6992. - */
  6993. - at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
  6994. -}
  6995. -
  6996. -void at91sam9_idle(void)
  6997. -{
  6998. - at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
  6999. - cpu_do_idle();
  7000. -}
  7001. -
  7002. int of_at91_get_clk_range(struct device_node *np, const char *propname,
  7003. struct clk_range *range)
  7004. {
  7005. @@ -64,402 +41,3 @@ int of_at91_get_clk_range(struct device_node *np, const char *propname,
  7006. return 0;
  7007. }
  7008. EXPORT_SYMBOL_GPL(of_at91_get_clk_range);
  7009. -
  7010. -static void pmc_irq_mask(struct irq_data *d)
  7011. -{
  7012. - struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  7013. -
  7014. - pmc_write(pmc, AT91_PMC_IDR, 1 << d->hwirq);
  7015. -}
  7016. -
  7017. -static void pmc_irq_unmask(struct irq_data *d)
  7018. -{
  7019. - struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  7020. -
  7021. - pmc_write(pmc, AT91_PMC_IER, 1 << d->hwirq);
  7022. -}
  7023. -
  7024. -static int pmc_irq_set_type(struct irq_data *d, unsigned type)
  7025. -{
  7026. - if (type != IRQ_TYPE_LEVEL_HIGH) {
  7027. - pr_warn("PMC: type not supported (support only IRQ_TYPE_LEVEL_HIGH type)\n");
  7028. - return -EINVAL;
  7029. - }
  7030. -
  7031. - return 0;
  7032. -}
  7033. -
  7034. -static void pmc_irq_suspend(struct irq_data *d)
  7035. -{
  7036. - struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  7037. -
  7038. - pmc->imr = pmc_read(pmc, AT91_PMC_IMR);
  7039. - pmc_write(pmc, AT91_PMC_IDR, pmc->imr);
  7040. -}
  7041. -
  7042. -static void pmc_irq_resume(struct irq_data *d)
  7043. -{
  7044. - struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  7045. -
  7046. - pmc_write(pmc, AT91_PMC_IER, pmc->imr);
  7047. -}
  7048. -
  7049. -static struct irq_chip pmc_irq = {
  7050. - .name = "PMC",
  7051. - .irq_disable = pmc_irq_mask,
  7052. - .irq_mask = pmc_irq_mask,
  7053. - .irq_unmask = pmc_irq_unmask,
  7054. - .irq_set_type = pmc_irq_set_type,
  7055. - .irq_suspend = pmc_irq_suspend,
  7056. - .irq_resume = pmc_irq_resume,
  7057. -};
  7058. -
  7059. -static struct lock_class_key pmc_lock_class;
  7060. -
  7061. -static int pmc_irq_map(struct irq_domain *h, unsigned int virq,
  7062. - irq_hw_number_t hw)
  7063. -{
  7064. - struct at91_pmc *pmc = h->host_data;
  7065. -
  7066. - irq_set_lockdep_class(virq, &pmc_lock_class);
  7067. -
  7068. - irq_set_chip_and_handler(virq, &pmc_irq,
  7069. - handle_level_irq);
  7070. - irq_set_chip_data(virq, pmc);
  7071. -
  7072. - return 0;
  7073. -}
  7074. -
  7075. -static int pmc_irq_domain_xlate(struct irq_domain *d,
  7076. - struct device_node *ctrlr,
  7077. - const u32 *intspec, unsigned int intsize,
  7078. - irq_hw_number_t *out_hwirq,
  7079. - unsigned int *out_type)
  7080. -{
  7081. - struct at91_pmc *pmc = d->host_data;
  7082. - const struct at91_pmc_caps *caps = pmc->caps;
  7083. -
  7084. - if (WARN_ON(intsize < 1))
  7085. - return -EINVAL;
  7086. -
  7087. - *out_hwirq = intspec[0];
  7088. -
  7089. - if (!(caps->available_irqs & (1 << *out_hwirq)))
  7090. - return -EINVAL;
  7091. -
  7092. - *out_type = IRQ_TYPE_LEVEL_HIGH;
  7093. -
  7094. - return 0;
  7095. -}
  7096. -
  7097. -static const struct irq_domain_ops pmc_irq_ops = {
  7098. - .map = pmc_irq_map,
  7099. - .xlate = pmc_irq_domain_xlate,
  7100. -};
  7101. -
  7102. -static irqreturn_t pmc_irq_handler(int irq, void *data)
  7103. -{
  7104. - struct at91_pmc *pmc = (struct at91_pmc *)data;
  7105. - unsigned long sr;
  7106. - int n;
  7107. -
  7108. - sr = pmc_read(pmc, AT91_PMC_SR) & pmc_read(pmc, AT91_PMC_IMR);
  7109. - if (!sr)
  7110. - return IRQ_NONE;
  7111. -
  7112. - for_each_set_bit(n, &sr, BITS_PER_LONG)
  7113. - generic_handle_irq(irq_find_mapping(pmc->irqdomain, n));
  7114. -
  7115. - return IRQ_HANDLED;
  7116. -}
  7117. -
  7118. -static const struct at91_pmc_caps at91rm9200_caps = {
  7119. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  7120. - AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  7121. - AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
  7122. - AT91_PMC_PCK3RDY,
  7123. -};
  7124. -
  7125. -static const struct at91_pmc_caps at91sam9260_caps = {
  7126. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  7127. - AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  7128. - AT91_PMC_PCK1RDY,
  7129. -};
  7130. -
  7131. -static const struct at91_pmc_caps at91sam9g45_caps = {
  7132. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  7133. - AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  7134. - AT91_PMC_PCK1RDY,
  7135. -};
  7136. -
  7137. -static const struct at91_pmc_caps at91sam9n12_caps = {
  7138. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  7139. - AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  7140. - AT91_PMC_PCK1RDY | AT91_PMC_MOSCSELS |
  7141. - AT91_PMC_MOSCRCS | AT91_PMC_CFDEV,
  7142. -};
  7143. -
  7144. -static const struct at91_pmc_caps at91sam9x5_caps = {
  7145. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  7146. - AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  7147. - AT91_PMC_PCK1RDY | AT91_PMC_MOSCSELS |
  7148. - AT91_PMC_MOSCRCS | AT91_PMC_CFDEV,
  7149. -};
  7150. -
  7151. -static const struct at91_pmc_caps sama5d2_caps = {
  7152. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  7153. - AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  7154. - AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
  7155. - AT91_PMC_MOSCSELS | AT91_PMC_MOSCRCS |
  7156. - AT91_PMC_CFDEV | AT91_PMC_GCKRDY,
  7157. -};
  7158. -
  7159. -static const struct at91_pmc_caps sama5d3_caps = {
  7160. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  7161. - AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  7162. - AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
  7163. - AT91_PMC_MOSCSELS | AT91_PMC_MOSCRCS |
  7164. - AT91_PMC_CFDEV,
  7165. -};
  7166. -
  7167. -static struct at91_pmc *__init at91_pmc_init(struct device_node *np,
  7168. - void __iomem *regbase, int virq,
  7169. - const struct at91_pmc_caps *caps)
  7170. -{
  7171. - struct at91_pmc *pmc;
  7172. -
  7173. - if (!regbase || !virq || !caps)
  7174. - return NULL;
  7175. -
  7176. - at91_pmc_base = regbase;
  7177. -
  7178. - pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
  7179. - if (!pmc)
  7180. - return NULL;
  7181. -
  7182. - spin_lock_init(&pmc->lock);
  7183. - pmc->regbase = regbase;
  7184. - pmc->virq = virq;
  7185. - pmc->caps = caps;
  7186. -
  7187. - pmc->irqdomain = irq_domain_add_linear(np, 32, &pmc_irq_ops, pmc);
  7188. -
  7189. - if (!pmc->irqdomain)
  7190. - goto out_free_pmc;
  7191. -
  7192. - pmc_write(pmc, AT91_PMC_IDR, 0xffffffff);
  7193. - if (request_irq(pmc->virq, pmc_irq_handler,
  7194. - IRQF_SHARED | IRQF_COND_SUSPEND, "pmc", pmc))
  7195. - goto out_remove_irqdomain;
  7196. -
  7197. - return pmc;
  7198. -
  7199. -out_remove_irqdomain:
  7200. - irq_domain_remove(pmc->irqdomain);
  7201. -out_free_pmc:
  7202. - kfree(pmc);
  7203. -
  7204. - return NULL;
  7205. -}
  7206. -
  7207. -static const struct of_device_id pmc_clk_ids[] __initconst = {
  7208. - /* Slow oscillator */
  7209. - {
  7210. - .compatible = "atmel,at91sam9260-clk-slow",
  7211. - .data = of_at91sam9260_clk_slow_setup,
  7212. - },
  7213. - /* Main clock */
  7214. - {
  7215. - .compatible = "atmel,at91rm9200-clk-main-osc",
  7216. - .data = of_at91rm9200_clk_main_osc_setup,
  7217. - },
  7218. - {
  7219. - .compatible = "atmel,at91sam9x5-clk-main-rc-osc",
  7220. - .data = of_at91sam9x5_clk_main_rc_osc_setup,
  7221. - },
  7222. - {
  7223. - .compatible = "atmel,at91rm9200-clk-main",
  7224. - .data = of_at91rm9200_clk_main_setup,
  7225. - },
  7226. - {
  7227. - .compatible = "atmel,at91sam9x5-clk-main",
  7228. - .data = of_at91sam9x5_clk_main_setup,
  7229. - },
  7230. - /* PLL clocks */
  7231. - {
  7232. - .compatible = "atmel,at91rm9200-clk-pll",
  7233. - .data = of_at91rm9200_clk_pll_setup,
  7234. - },
  7235. - {
  7236. - .compatible = "atmel,at91sam9g45-clk-pll",
  7237. - .data = of_at91sam9g45_clk_pll_setup,
  7238. - },
  7239. - {
  7240. - .compatible = "atmel,at91sam9g20-clk-pllb",
  7241. - .data = of_at91sam9g20_clk_pllb_setup,
  7242. - },
  7243. - {
  7244. - .compatible = "atmel,sama5d3-clk-pll",
  7245. - .data = of_sama5d3_clk_pll_setup,
  7246. - },
  7247. - {
  7248. - .compatible = "atmel,at91sam9x5-clk-plldiv",
  7249. - .data = of_at91sam9x5_clk_plldiv_setup,
  7250. - },
  7251. - /* Master clock */
  7252. - {
  7253. - .compatible = "atmel,at91rm9200-clk-master",
  7254. - .data = of_at91rm9200_clk_master_setup,
  7255. - },
  7256. - {
  7257. - .compatible = "atmel,at91sam9x5-clk-master",
  7258. - .data = of_at91sam9x5_clk_master_setup,
  7259. - },
  7260. - /* System clocks */
  7261. - {
  7262. - .compatible = "atmel,at91rm9200-clk-system",
  7263. - .data = of_at91rm9200_clk_sys_setup,
  7264. - },
  7265. - /* Peripheral clocks */
  7266. - {
  7267. - .compatible = "atmel,at91rm9200-clk-peripheral",
  7268. - .data = of_at91rm9200_clk_periph_setup,
  7269. - },
  7270. - {
  7271. - .compatible = "atmel,at91sam9x5-clk-peripheral",
  7272. - .data = of_at91sam9x5_clk_periph_setup,
  7273. - },
  7274. - /* Programmable clocks */
  7275. - {
  7276. - .compatible = "atmel,at91rm9200-clk-programmable",
  7277. - .data = of_at91rm9200_clk_prog_setup,
  7278. - },
  7279. - {
  7280. - .compatible = "atmel,at91sam9g45-clk-programmable",
  7281. - .data = of_at91sam9g45_clk_prog_setup,
  7282. - },
  7283. - {
  7284. - .compatible = "atmel,at91sam9x5-clk-programmable",
  7285. - .data = of_at91sam9x5_clk_prog_setup,
  7286. - },
  7287. - /* UTMI clock */
  7288. -#if defined(CONFIG_HAVE_AT91_UTMI)
  7289. - {
  7290. - .compatible = "atmel,at91sam9x5-clk-utmi",
  7291. - .data = of_at91sam9x5_clk_utmi_setup,
  7292. - },
  7293. -#endif
  7294. - /* USB clock */
  7295. -#if defined(CONFIG_HAVE_AT91_USB_CLK)
  7296. - {
  7297. - .compatible = "atmel,at91rm9200-clk-usb",
  7298. - .data = of_at91rm9200_clk_usb_setup,
  7299. - },
  7300. - {
  7301. - .compatible = "atmel,at91sam9x5-clk-usb",
  7302. - .data = of_at91sam9x5_clk_usb_setup,
  7303. - },
  7304. - {
  7305. - .compatible = "atmel,at91sam9n12-clk-usb",
  7306. - .data = of_at91sam9n12_clk_usb_setup,
  7307. - },
  7308. -#endif
  7309. - /* SMD clock */
  7310. -#if defined(CONFIG_HAVE_AT91_SMD)
  7311. - {
  7312. - .compatible = "atmel,at91sam9x5-clk-smd",
  7313. - .data = of_at91sam9x5_clk_smd_setup,
  7314. - },
  7315. -#endif
  7316. -#if defined(CONFIG_HAVE_AT91_H32MX)
  7317. - {
  7318. - .compatible = "atmel,sama5d4-clk-h32mx",
  7319. - .data = of_sama5d4_clk_h32mx_setup,
  7320. - },
  7321. -#endif
  7322. -#if defined(CONFIG_HAVE_AT91_GENERATED_CLK)
  7323. - {
  7324. - .compatible = "atmel,sama5d2-clk-generated",
  7325. - .data = of_sama5d2_clk_generated_setup,
  7326. - },
  7327. -#endif
  7328. - { /*sentinel*/ }
  7329. -};
  7330. -
  7331. -static void __init of_at91_pmc_setup(struct device_node *np,
  7332. - const struct at91_pmc_caps *caps)
  7333. -{
  7334. - struct at91_pmc *pmc;
  7335. - struct device_node *childnp;
  7336. - void (*clk_setup)(struct device_node *, struct at91_pmc *);
  7337. - const struct of_device_id *clk_id;
  7338. - void __iomem *regbase = of_iomap(np, 0);
  7339. - int virq;
  7340. -
  7341. - if (!regbase)
  7342. - return;
  7343. -
  7344. - virq = irq_of_parse_and_map(np, 0);
  7345. - if (!virq)
  7346. - return;
  7347. -
  7348. - pmc = at91_pmc_init(np, regbase, virq, caps);
  7349. - if (!pmc)
  7350. - return;
  7351. - for_each_child_of_node(np, childnp) {
  7352. - clk_id = of_match_node(pmc_clk_ids, childnp);
  7353. - if (!clk_id)
  7354. - continue;
  7355. - clk_setup = clk_id->data;
  7356. - clk_setup(childnp, pmc);
  7357. - }
  7358. -}
  7359. -
  7360. -static void __init of_at91rm9200_pmc_setup(struct device_node *np)
  7361. -{
  7362. - of_at91_pmc_setup(np, &at91rm9200_caps);
  7363. -}
  7364. -CLK_OF_DECLARE(at91rm9200_clk_pmc, "atmel,at91rm9200-pmc",
  7365. - of_at91rm9200_pmc_setup);
  7366. -
  7367. -static void __init of_at91sam9260_pmc_setup(struct device_node *np)
  7368. -{
  7369. - of_at91_pmc_setup(np, &at91sam9260_caps);
  7370. -}
  7371. -CLK_OF_DECLARE(at91sam9260_clk_pmc, "atmel,at91sam9260-pmc",
  7372. - of_at91sam9260_pmc_setup);
  7373. -
  7374. -static void __init of_at91sam9g45_pmc_setup(struct device_node *np)
  7375. -{
  7376. - of_at91_pmc_setup(np, &at91sam9g45_caps);
  7377. -}
  7378. -CLK_OF_DECLARE(at91sam9g45_clk_pmc, "atmel,at91sam9g45-pmc",
  7379. - of_at91sam9g45_pmc_setup);
  7380. -
  7381. -static void __init of_at91sam9n12_pmc_setup(struct device_node *np)
  7382. -{
  7383. - of_at91_pmc_setup(np, &at91sam9n12_caps);
  7384. -}
  7385. -CLK_OF_DECLARE(at91sam9n12_clk_pmc, "atmel,at91sam9n12-pmc",
  7386. - of_at91sam9n12_pmc_setup);
  7387. -
  7388. -static void __init of_at91sam9x5_pmc_setup(struct device_node *np)
  7389. -{
  7390. - of_at91_pmc_setup(np, &at91sam9x5_caps);
  7391. -}
  7392. -CLK_OF_DECLARE(at91sam9x5_clk_pmc, "atmel,at91sam9x5-pmc",
  7393. - of_at91sam9x5_pmc_setup);
  7394. -
  7395. -static void __init of_sama5d2_pmc_setup(struct device_node *np)
  7396. -{
  7397. - of_at91_pmc_setup(np, &sama5d2_caps);
  7398. -}
  7399. -CLK_OF_DECLARE(sama5d2_clk_pmc, "atmel,sama5d2-pmc",
  7400. - of_sama5d2_pmc_setup);
  7401. -
  7402. -static void __init of_sama5d3_pmc_setup(struct device_node *np)
  7403. -{
  7404. - of_at91_pmc_setup(np, &sama5d3_caps);
  7405. -}
  7406. -CLK_OF_DECLARE(sama5d3_clk_pmc, "atmel,sama5d3-pmc",
  7407. - of_sama5d3_pmc_setup);
  7408. diff --git a/drivers/clk/at91/pmc.h b/drivers/clk/at91/pmc.h
  7409. index f65739272779..5771fff0ee3f 100644
  7410. --- a/drivers/clk/at91/pmc.h
  7411. +++ b/drivers/clk/at91/pmc.h
  7412. @@ -14,8 +14,11 @@
  7413. #include <linux/io.h>
  7414. #include <linux/irqdomain.h>
  7415. +#include <linux/regmap.h>
  7416. #include <linux/spinlock.h>
  7417. +extern spinlock_t pmc_pcr_lock;
  7418. +
  7419. struct clk_range {
  7420. unsigned long min;
  7421. unsigned long max;
  7422. @@ -23,102 +26,7 @@ struct clk_range {
  7423. #define CLK_RANGE(MIN, MAX) {.min = MIN, .max = MAX,}
  7424. -struct at91_pmc_caps {
  7425. - u32 available_irqs;
  7426. -};
  7427. -
  7428. -struct at91_pmc {
  7429. - void __iomem *regbase;
  7430. - int virq;
  7431. - spinlock_t lock;
  7432. - const struct at91_pmc_caps *caps;
  7433. - struct irq_domain *irqdomain;
  7434. - u32 imr;
  7435. -};
  7436. -
  7437. -static inline void pmc_lock(struct at91_pmc *pmc)
  7438. -{
  7439. - spin_lock(&pmc->lock);
  7440. -}
  7441. -
  7442. -static inline void pmc_unlock(struct at91_pmc *pmc)
  7443. -{
  7444. - spin_unlock(&pmc->lock);
  7445. -}
  7446. -
  7447. -static inline u32 pmc_read(struct at91_pmc *pmc, int offset)
  7448. -{
  7449. - return readl(pmc->regbase + offset);
  7450. -}
  7451. -
  7452. -static inline void pmc_write(struct at91_pmc *pmc, int offset, u32 value)
  7453. -{
  7454. - writel(value, pmc->regbase + offset);
  7455. -}
  7456. -
  7457. int of_at91_get_clk_range(struct device_node *np, const char *propname,
  7458. struct clk_range *range);
  7459. -void of_at91sam9260_clk_slow_setup(struct device_node *np,
  7460. - struct at91_pmc *pmc);
  7461. -
  7462. -void of_at91rm9200_clk_main_osc_setup(struct device_node *np,
  7463. - struct at91_pmc *pmc);
  7464. -void of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np,
  7465. - struct at91_pmc *pmc);
  7466. -void of_at91rm9200_clk_main_setup(struct device_node *np,
  7467. - struct at91_pmc *pmc);
  7468. -void of_at91sam9x5_clk_main_setup(struct device_node *np,
  7469. - struct at91_pmc *pmc);
  7470. -
  7471. -void of_at91rm9200_clk_pll_setup(struct device_node *np,
  7472. - struct at91_pmc *pmc);
  7473. -void of_at91sam9g45_clk_pll_setup(struct device_node *np,
  7474. - struct at91_pmc *pmc);
  7475. -void of_at91sam9g20_clk_pllb_setup(struct device_node *np,
  7476. - struct at91_pmc *pmc);
  7477. -void of_sama5d3_clk_pll_setup(struct device_node *np,
  7478. - struct at91_pmc *pmc);
  7479. -void of_at91sam9x5_clk_plldiv_setup(struct device_node *np,
  7480. - struct at91_pmc *pmc);
  7481. -
  7482. -void of_at91rm9200_clk_master_setup(struct device_node *np,
  7483. - struct at91_pmc *pmc);
  7484. -void of_at91sam9x5_clk_master_setup(struct device_node *np,
  7485. - struct at91_pmc *pmc);
  7486. -
  7487. -void of_at91rm9200_clk_sys_setup(struct device_node *np,
  7488. - struct at91_pmc *pmc);
  7489. -
  7490. -void of_at91rm9200_clk_periph_setup(struct device_node *np,
  7491. - struct at91_pmc *pmc);
  7492. -void of_at91sam9x5_clk_periph_setup(struct device_node *np,
  7493. - struct at91_pmc *pmc);
  7494. -
  7495. -void of_at91rm9200_clk_prog_setup(struct device_node *np,
  7496. - struct at91_pmc *pmc);
  7497. -void of_at91sam9g45_clk_prog_setup(struct device_node *np,
  7498. - struct at91_pmc *pmc);
  7499. -void of_at91sam9x5_clk_prog_setup(struct device_node *np,
  7500. - struct at91_pmc *pmc);
  7501. -
  7502. -void of_at91sam9x5_clk_utmi_setup(struct device_node *np,
  7503. - struct at91_pmc *pmc);
  7504. -
  7505. -void of_at91rm9200_clk_usb_setup(struct device_node *np,
  7506. - struct at91_pmc *pmc);
  7507. -void of_at91sam9x5_clk_usb_setup(struct device_node *np,
  7508. - struct at91_pmc *pmc);
  7509. -void of_at91sam9n12_clk_usb_setup(struct device_node *np,
  7510. - struct at91_pmc *pmc);
  7511. -
  7512. -void of_at91sam9x5_clk_smd_setup(struct device_node *np,
  7513. - struct at91_pmc *pmc);
  7514. -
  7515. -void of_sama5d4_clk_h32mx_setup(struct device_node *np,
  7516. - struct at91_pmc *pmc);
  7517. -
  7518. -void of_sama5d2_clk_generated_setup(struct device_node *np,
  7519. - struct at91_pmc *pmc);
  7520. -
  7521. #endif /* __PMC_H_ */
  7522. diff --git a/drivers/clocksource/tcb_clksrc.c b/drivers/clocksource/tcb_clksrc.c
  7523. index 4da2af9694a2..5b6f57f500b8 100644
  7524. --- a/drivers/clocksource/tcb_clksrc.c
  7525. +++ b/drivers/clocksource/tcb_clksrc.c
  7526. @@ -23,8 +23,7 @@
  7527. * this 32 bit free-running counter. the second channel is not used.
  7528. *
  7529. * - The third channel may be used to provide a 16-bit clockevent
  7530. - * source, used in either periodic or oneshot mode. This runs
  7531. - * at 32 KiHZ, and can handle delays of up to two seconds.
  7532. + * source, used in either periodic or oneshot mode.
  7533. *
  7534. * A boot clocksource and clockevent source are also currently needed,
  7535. * unless the relevant platforms (ARM/AT91, AVR32/AT32) are changed so
  7536. @@ -74,6 +73,8 @@ static struct clocksource clksrc = {
  7537. struct tc_clkevt_device {
  7538. struct clock_event_device clkevt;
  7539. struct clk *clk;
  7540. + bool clk_enabled;
  7541. + u32 freq;
  7542. void __iomem *regs;
  7543. };
  7544. @@ -82,15 +83,26 @@ static struct tc_clkevt_device *to_tc_clkevt(struct clock_event_device *clkevt)
  7545. return container_of(clkevt, struct tc_clkevt_device, clkevt);
  7546. }
  7547. -/* For now, we always use the 32K clock ... this optimizes for NO_HZ,
  7548. - * because using one of the divided clocks would usually mean the
  7549. - * tick rate can never be less than several dozen Hz (vs 0.5 Hz).
  7550. - *
  7551. - * A divided clock could be good for high resolution timers, since
  7552. - * 30.5 usec resolution can seem "low".
  7553. - */
  7554. static u32 timer_clock;
  7555. +static void tc_clk_disable(struct clock_event_device *d)
  7556. +{
  7557. + struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  7558. +
  7559. + clk_disable(tcd->clk);
  7560. + tcd->clk_enabled = false;
  7561. +}
  7562. +
  7563. +static void tc_clk_enable(struct clock_event_device *d)
  7564. +{
  7565. + struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  7566. +
  7567. + if (tcd->clk_enabled)
  7568. + return;
  7569. + clk_enable(tcd->clk);
  7570. + tcd->clk_enabled = true;
  7571. +}
  7572. +
  7573. static int tc_shutdown(struct clock_event_device *d)
  7574. {
  7575. struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  7576. @@ -98,8 +110,14 @@ static int tc_shutdown(struct clock_event_device *d)
  7577. __raw_writel(0xff, regs + ATMEL_TC_REG(2, IDR));
  7578. __raw_writel(ATMEL_TC_CLKDIS, regs + ATMEL_TC_REG(2, CCR));
  7579. + return 0;
  7580. +}
  7581. +
  7582. +static int tc_shutdown_clk_off(struct clock_event_device *d)
  7583. +{
  7584. + tc_shutdown(d);
  7585. if (!clockevent_state_detached(d))
  7586. - clk_disable(tcd->clk);
  7587. + tc_clk_disable(d);
  7588. return 0;
  7589. }
  7590. @@ -112,9 +130,9 @@ static int tc_set_oneshot(struct clock_event_device *d)
  7591. if (clockevent_state_oneshot(d) || clockevent_state_periodic(d))
  7592. tc_shutdown(d);
  7593. - clk_enable(tcd->clk);
  7594. + tc_clk_enable(d);
  7595. - /* slow clock, count up to RC, then irq and stop */
  7596. + /* count up to RC, then irq and stop */
  7597. __raw_writel(timer_clock | ATMEL_TC_CPCSTOP | ATMEL_TC_WAVE |
  7598. ATMEL_TC_WAVESEL_UP_AUTO, regs + ATMEL_TC_REG(2, CMR));
  7599. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  7600. @@ -134,12 +152,12 @@ static int tc_set_periodic(struct clock_event_device *d)
  7601. /* By not making the gentime core emulate periodic mode on top
  7602. * of oneshot, we get lower overhead and improved accuracy.
  7603. */
  7604. - clk_enable(tcd->clk);
  7605. + tc_clk_enable(d);
  7606. - /* slow clock, count up to RC, then irq and restart */
  7607. + /* count up to RC, then irq and restart */
  7608. __raw_writel(timer_clock | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  7609. regs + ATMEL_TC_REG(2, CMR));
  7610. - __raw_writel((32768 + HZ / 2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  7611. + __raw_writel((tcd->freq + HZ / 2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  7612. /* Enable clock and interrupts on RC compare */
  7613. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  7614. @@ -166,9 +184,13 @@ static struct tc_clkevt_device clkevt = {
  7615. .features = CLOCK_EVT_FEAT_PERIODIC |
  7616. CLOCK_EVT_FEAT_ONESHOT,
  7617. /* Should be lower than at91rm9200's system timer */
  7618. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  7619. .rating = 125,
  7620. +#else
  7621. + .rating = 200,
  7622. +#endif
  7623. .set_next_event = tc_next_event,
  7624. - .set_state_shutdown = tc_shutdown,
  7625. + .set_state_shutdown = tc_shutdown_clk_off,
  7626. .set_state_periodic = tc_set_periodic,
  7627. .set_state_oneshot = tc_set_oneshot,
  7628. },
  7629. @@ -188,8 +210,9 @@ static irqreturn_t ch2_irq(int irq, void *handle)
  7630. return IRQ_NONE;
  7631. }
  7632. -static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  7633. +static int __init setup_clkevents(struct atmel_tc *tc, int divisor_idx)
  7634. {
  7635. + unsigned divisor = atmel_tc_divisors[divisor_idx];
  7636. int ret;
  7637. struct clk *t2_clk = tc->clk[2];
  7638. int irq = tc->irq[2];
  7639. @@ -210,7 +233,11 @@ static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  7640. clkevt.regs = tc->regs;
  7641. clkevt.clk = t2_clk;
  7642. - timer_clock = clk32k_divisor_idx;
  7643. + timer_clock = divisor_idx;
  7644. + if (!divisor)
  7645. + clkevt.freq = 32768;
  7646. + else
  7647. + clkevt.freq = clk_get_rate(t2_clk) / divisor;
  7648. clkevt.clkevt.cpumask = cpumask_of(0);
  7649. @@ -221,7 +248,7 @@ static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  7650. return ret;
  7651. }
  7652. - clockevents_config_and_register(&clkevt.clkevt, 32768, 1, 0xffff);
  7653. + clockevents_config_and_register(&clkevt.clkevt, clkevt.freq, 1, 0xffff);
  7654. return ret;
  7655. }
  7656. @@ -358,7 +385,11 @@ static int __init tcb_clksrc_init(void)
  7657. goto err_disable_t1;
  7658. /* channel 2: periodic and oneshot timer support */
  7659. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  7660. ret = setup_clkevents(tc, clk32k_divisor_idx);
  7661. +#else
  7662. + ret = setup_clkevents(tc, best_divisor_idx);
  7663. +#endif
  7664. if (ret)
  7665. goto err_unregister_clksrc;
  7666. diff --git a/drivers/clocksource/timer-atmel-pit.c b/drivers/clocksource/timer-atmel-pit.c
  7667. index d911c5dca8f1..7a40f7e88468 100644
  7668. --- a/drivers/clocksource/timer-atmel-pit.c
  7669. +++ b/drivers/clocksource/timer-atmel-pit.c
  7670. @@ -46,6 +46,7 @@ struct pit_data {
  7671. u32 cycle;
  7672. u32 cnt;
  7673. unsigned int irq;
  7674. + bool irq_requested;
  7675. struct clk *mck;
  7676. };
  7677. @@ -96,15 +97,29 @@ static int pit_clkevt_shutdown(struct clock_event_device *dev)
  7678. /* disable irq, leaving the clocksource active */
  7679. pit_write(data->base, AT91_PIT_MR, (data->cycle - 1) | AT91_PIT_PITEN);
  7680. + if (data->irq_requested) {
  7681. + free_irq(data->irq, data);
  7682. + data->irq_requested = false;
  7683. + }
  7684. return 0;
  7685. }
  7686. +static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id);
  7687. /*
  7688. * Clockevent device: interrupts every 1/HZ (== pit_cycles * MCK/16)
  7689. */
  7690. static int pit_clkevt_set_periodic(struct clock_event_device *dev)
  7691. {
  7692. struct pit_data *data = clkevt_to_pit_data(dev);
  7693. + int ret;
  7694. +
  7695. + ret = request_irq(data->irq, at91sam926x_pit_interrupt,
  7696. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7697. + "at91_tick", data);
  7698. + if (ret)
  7699. + panic(pr_fmt("Unable to setup IRQ\n"));
  7700. +
  7701. + data->irq_requested = true;
  7702. /* update clocksource counter */
  7703. data->cnt += data->cycle * PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
  7704. @@ -181,7 +196,6 @@ static void __init at91sam926x_pit_common_init(struct pit_data *data)
  7705. {
  7706. unsigned long pit_rate;
  7707. unsigned bits;
  7708. - int ret;
  7709. /*
  7710. * Use our actual MCK to figure out how many MCK/16 ticks per
  7711. @@ -206,13 +220,6 @@ static void __init at91sam926x_pit_common_init(struct pit_data *data)
  7712. data->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  7713. clocksource_register_hz(&data->clksrc, pit_rate);
  7714. - /* Set up irq handler */
  7715. - ret = request_irq(data->irq, at91sam926x_pit_interrupt,
  7716. - IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7717. - "at91_tick", data);
  7718. - if (ret)
  7719. - panic(pr_fmt("Unable to setup IRQ\n"));
  7720. -
  7721. /* Set up and register clockevents */
  7722. data->clkevt.name = "pit";
  7723. data->clkevt.features = CLOCK_EVT_FEAT_PERIODIC;
  7724. diff --git a/drivers/clocksource/timer-atmel-st.c b/drivers/clocksource/timer-atmel-st.c
  7725. index 29d21d68df5a..103d0fd70cc4 100644
  7726. --- a/drivers/clocksource/timer-atmel-st.c
  7727. +++ b/drivers/clocksource/timer-atmel-st.c
  7728. @@ -115,18 +115,29 @@ static void clkdev32k_disable_and_flush_irq(void)
  7729. last_crtr = read_CRTR();
  7730. }
  7731. +static int atmel_st_irq;
  7732. +
  7733. static int clkevt32k_shutdown(struct clock_event_device *evt)
  7734. {
  7735. clkdev32k_disable_and_flush_irq();
  7736. irqmask = 0;
  7737. regmap_write(regmap_st, AT91_ST_IER, irqmask);
  7738. + free_irq(atmel_st_irq, regmap_st);
  7739. return 0;
  7740. }
  7741. static int clkevt32k_set_oneshot(struct clock_event_device *dev)
  7742. {
  7743. + int ret;
  7744. +
  7745. clkdev32k_disable_and_flush_irq();
  7746. + ret = request_irq(atmel_st_irq, at91rm9200_timer_interrupt,
  7747. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7748. + "at91_tick", regmap_st);
  7749. + if (ret)
  7750. + panic(pr_fmt("Unable to setup IRQ\n"));
  7751. +
  7752. /*
  7753. * ALM for oneshot irqs, set by next_event()
  7754. * before 32 seconds have passed.
  7755. @@ -139,8 +150,16 @@ static int clkevt32k_set_oneshot(struct clock_event_device *dev)
  7756. static int clkevt32k_set_periodic(struct clock_event_device *dev)
  7757. {
  7758. + int ret;
  7759. +
  7760. clkdev32k_disable_and_flush_irq();
  7761. + ret = request_irq(atmel_st_irq, at91rm9200_timer_interrupt,
  7762. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7763. + "at91_tick", regmap_st);
  7764. + if (ret)
  7765. + panic(pr_fmt("Unable to setup IRQ\n"));
  7766. +
  7767. /* PIT for periodic irqs; fixed rate of 1/HZ */
  7768. irqmask = AT91_ST_PITS;
  7769. regmap_write(regmap_st, AT91_ST_PIMR, timer_latch);
  7770. @@ -198,7 +217,7 @@ static void __init atmel_st_timer_init(struct device_node *node)
  7771. {
  7772. struct clk *sclk;
  7773. unsigned int sclk_rate, val;
  7774. - int irq, ret;
  7775. + int ret;
  7776. regmap_st = syscon_node_to_regmap(node);
  7777. if (IS_ERR(regmap_st))
  7778. @@ -210,17 +229,10 @@ static void __init atmel_st_timer_init(struct device_node *node)
  7779. regmap_read(regmap_st, AT91_ST_SR, &val);
  7780. /* Get the interrupts property */
  7781. - irq = irq_of_parse_and_map(node, 0);
  7782. - if (!irq)
  7783. + atmel_st_irq = irq_of_parse_and_map(node, 0);
  7784. + if (!atmel_st_irq)
  7785. panic(pr_fmt("Unable to get IRQ from DT\n"));
  7786. - /* Make IRQs happen for the system timer */
  7787. - ret = request_irq(irq, at91rm9200_timer_interrupt,
  7788. - IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7789. - "at91_tick", regmap_st);
  7790. - if (ret)
  7791. - panic(pr_fmt("Unable to setup IRQ\n"));
  7792. -
  7793. sclk = of_clk_get(node, 0);
  7794. if (IS_ERR(sclk))
  7795. panic(pr_fmt("Unable to get slow clock\n"));
  7796. diff --git a/drivers/cpufreq/Kconfig.x86 b/drivers/cpufreq/Kconfig.x86
  7797. index c59bdcb83217..8f23161d80be 100644
  7798. --- a/drivers/cpufreq/Kconfig.x86
  7799. +++ b/drivers/cpufreq/Kconfig.x86
  7800. @@ -123,7 +123,7 @@ config X86_POWERNOW_K7_ACPI
  7801. config X86_POWERNOW_K8
  7802. tristate "AMD Opteron/Athlon64 PowerNow!"
  7803. - depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ
  7804. + depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ && !PREEMPT_RT_BASE
  7805. help
  7806. This adds the CPUFreq driver for K8/early Opteron/Athlon64 processors.
  7807. Support for K10 and newer processors is now in acpi-cpufreq.
  7808. diff --git a/drivers/cpuidle/coupled.c b/drivers/cpuidle/coupled.c
  7809. index 344058f8501a..d5657d50ac40 100644
  7810. --- a/drivers/cpuidle/coupled.c
  7811. +++ b/drivers/cpuidle/coupled.c
  7812. @@ -119,7 +119,6 @@ struct cpuidle_coupled {
  7813. #define CPUIDLE_COUPLED_NOT_IDLE (-1)
  7814. -static DEFINE_MUTEX(cpuidle_coupled_lock);
  7815. static DEFINE_PER_CPU(struct call_single_data, cpuidle_coupled_poke_cb);
  7816. /*
  7817. diff --git a/drivers/gpu/drm/i915/i915_gem_execbuffer.c b/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  7818. index 6ed7d63a0688..9da7482ad256 100644
  7819. --- a/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  7820. +++ b/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  7821. @@ -1264,7 +1264,9 @@ i915_gem_ringbuffer_submission(struct i915_execbuffer_params *params,
  7822. if (ret)
  7823. return ret;
  7824. +#ifndef CONFIG_PREEMPT_RT_BASE
  7825. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  7826. +#endif
  7827. i915_gem_execbuffer_move_to_active(vmas, params->request);
  7828. i915_gem_execbuffer_retire_commands(params);
  7829. diff --git a/drivers/gpu/drm/i915/i915_gem_shrinker.c b/drivers/gpu/drm/i915/i915_gem_shrinker.c
  7830. index c0a96f1ee18e..deb1e207fa3c 100644
  7831. --- a/drivers/gpu/drm/i915/i915_gem_shrinker.c
  7832. +++ b/drivers/gpu/drm/i915/i915_gem_shrinker.c
  7833. @@ -39,7 +39,7 @@ static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  7834. if (!mutex_is_locked(mutex))
  7835. return false;
  7836. -#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)
  7837. +#if (defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)) && !defined(CONFIG_PREEMPT_RT_BASE)
  7838. return mutex->owner == task;
  7839. #else
  7840. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  7841. diff --git a/drivers/gpu/drm/i915/i915_irq.c b/drivers/gpu/drm/i915/i915_irq.c
  7842. index b7b0a38acd67..148dcb1349d5 100644
  7843. --- a/drivers/gpu/drm/i915/i915_irq.c
  7844. +++ b/drivers/gpu/drm/i915/i915_irq.c
  7845. @@ -812,6 +812,7 @@ static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
  7846. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7847. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  7848. + preempt_disable_rt();
  7849. /* Get optional system timestamp before query. */
  7850. if (stime)
  7851. @@ -863,6 +864,7 @@ static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
  7852. *etime = ktime_get();
  7853. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  7854. + preempt_enable_rt();
  7855. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7856. diff --git a/drivers/gpu/drm/i915/intel_display.c b/drivers/gpu/drm/i915/intel_display.c
  7857. index 4f5d07bb3511..8ecd5c016dba 100644
  7858. --- a/drivers/gpu/drm/i915/intel_display.c
  7859. +++ b/drivers/gpu/drm/i915/intel_display.c
  7860. @@ -11400,7 +11400,7 @@ void intel_check_page_flip(struct drm_device *dev, int pipe)
  7861. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7862. struct intel_unpin_work *work;
  7863. - WARN_ON(!in_interrupt());
  7864. + WARN_ON_NONRT(!in_interrupt());
  7865. if (crtc == NULL)
  7866. return;
  7867. diff --git a/drivers/gpu/drm/i915/intel_sprite.c b/drivers/gpu/drm/i915/intel_sprite.c
  7868. index 2cc6aa072f4c..b79d33f14868 100644
  7869. --- a/drivers/gpu/drm/i915/intel_sprite.c
  7870. +++ b/drivers/gpu/drm/i915/intel_sprite.c
  7871. @@ -38,6 +38,7 @@
  7872. #include "intel_drv.h"
  7873. #include <drm/i915_drm.h>
  7874. #include "i915_drv.h"
  7875. +#include <linux/locallock.h>
  7876. static bool
  7877. format_is_yuv(uint32_t format)
  7878. @@ -64,6 +65,8 @@ static int usecs_to_scanlines(const struct drm_display_mode *adjusted_mode,
  7879. 1000 * adjusted_mode->crtc_htotal);
  7880. }
  7881. +static DEFINE_LOCAL_IRQ_LOCK(pipe_update_lock);
  7882. +
  7883. /**
  7884. * intel_pipe_update_start() - start update of a set of display registers
  7885. * @crtc: the crtc of which the registers are going to be updated
  7886. @@ -96,7 +99,7 @@ void intel_pipe_update_start(struct intel_crtc *crtc)
  7887. min = vblank_start - usecs_to_scanlines(adjusted_mode, 100);
  7888. max = vblank_start - 1;
  7889. - local_irq_disable();
  7890. + local_lock_irq(pipe_update_lock);
  7891. if (min <= 0 || max <= 0)
  7892. return;
  7893. @@ -126,11 +129,11 @@ void intel_pipe_update_start(struct intel_crtc *crtc)
  7894. break;
  7895. }
  7896. - local_irq_enable();
  7897. + local_unlock_irq(pipe_update_lock);
  7898. timeout = schedule_timeout(timeout);
  7899. - local_irq_disable();
  7900. + local_lock_irq(pipe_update_lock);
  7901. }
  7902. finish_wait(wq, &wait);
  7903. @@ -164,7 +167,7 @@ void intel_pipe_update_end(struct intel_crtc *crtc)
  7904. trace_i915_pipe_update_end(crtc, end_vbl_count, scanline_end);
  7905. - local_irq_enable();
  7906. + local_unlock_irq(pipe_update_lock);
  7907. if (crtc->debug.start_vbl_count &&
  7908. crtc->debug.start_vbl_count != end_vbl_count) {
  7909. diff --git a/drivers/gpu/drm/radeon/radeon_display.c b/drivers/gpu/drm/radeon/radeon_display.c
  7910. index 3645b223aa37..642854b2ed2c 100644
  7911. --- a/drivers/gpu/drm/radeon/radeon_display.c
  7912. +++ b/drivers/gpu/drm/radeon/radeon_display.c
  7913. @@ -1862,6 +1862,7 @@ int radeon_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
  7914. struct radeon_device *rdev = dev->dev_private;
  7915. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  7916. + preempt_disable_rt();
  7917. /* Get optional system timestamp before query. */
  7918. if (stime)
  7919. @@ -1954,6 +1955,7 @@ int radeon_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
  7920. *etime = ktime_get();
  7921. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  7922. + preempt_enable_rt();
  7923. /* Decode into vertical and horizontal scanout position. */
  7924. *vpos = position & 0x1fff;
  7925. diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c
  7926. index 802dcb409030..d6d9427860d8 100644
  7927. --- a/drivers/hv/vmbus_drv.c
  7928. +++ b/drivers/hv/vmbus_drv.c
  7929. @@ -820,7 +820,7 @@ static void vmbus_isr(void)
  7930. tasklet_schedule(&msg_dpc);
  7931. }
  7932. - add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
  7933. + add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0, 0);
  7934. }
  7935. diff --git a/drivers/i2c/busses/i2c-omap.c b/drivers/i2c/busses/i2c-omap.c
  7936. index 08d26ba61ed3..46b89dd42b10 100644
  7937. --- a/drivers/i2c/busses/i2c-omap.c
  7938. +++ b/drivers/i2c/busses/i2c-omap.c
  7939. @@ -995,15 +995,12 @@ omap_i2c_isr(int irq, void *dev_id)
  7940. u16 mask;
  7941. u16 stat;
  7942. - spin_lock(&omap->lock);
  7943. - mask = omap_i2c_read_reg(omap, OMAP_I2C_IE_REG);
  7944. stat = omap_i2c_read_reg(omap, OMAP_I2C_STAT_REG);
  7945. + mask = omap_i2c_read_reg(omap, OMAP_I2C_IE_REG);
  7946. if (stat & mask)
  7947. ret = IRQ_WAKE_THREAD;
  7948. - spin_unlock(&omap->lock);
  7949. -
  7950. return ret;
  7951. }
  7952. diff --git a/drivers/ide/alim15x3.c b/drivers/ide/alim15x3.c
  7953. index 36f76e28a0bf..394f142f90c7 100644
  7954. --- a/drivers/ide/alim15x3.c
  7955. +++ b/drivers/ide/alim15x3.c
  7956. @@ -234,7 +234,7 @@ static int init_chipset_ali15x3(struct pci_dev *dev)
  7957. isa_dev = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
  7958. - local_irq_save(flags);
  7959. + local_irq_save_nort(flags);
  7960. if (m5229_revision < 0xC2) {
  7961. /*
  7962. @@ -325,7 +325,7 @@ static int init_chipset_ali15x3(struct pci_dev *dev)
  7963. }
  7964. pci_dev_put(north);
  7965. pci_dev_put(isa_dev);
  7966. - local_irq_restore(flags);
  7967. + local_irq_restore_nort(flags);
  7968. return 0;
  7969. }
  7970. diff --git a/drivers/ide/hpt366.c b/drivers/ide/hpt366.c
  7971. index 696b6c1ec940..0d0a96629b73 100644
  7972. --- a/drivers/ide/hpt366.c
  7973. +++ b/drivers/ide/hpt366.c
  7974. @@ -1241,7 +1241,7 @@ static int init_dma_hpt366(ide_hwif_t *hwif,
  7975. dma_old = inb(base + 2);
  7976. - local_irq_save(flags);
  7977. + local_irq_save_nort(flags);
  7978. dma_new = dma_old;
  7979. pci_read_config_byte(dev, hwif->channel ? 0x4b : 0x43, &masterdma);
  7980. @@ -1252,7 +1252,7 @@ static int init_dma_hpt366(ide_hwif_t *hwif,
  7981. if (dma_new != dma_old)
  7982. outb(dma_new, base + 2);
  7983. - local_irq_restore(flags);
  7984. + local_irq_restore_nort(flags);
  7985. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx\n",
  7986. hwif->name, base, base + 7);
  7987. diff --git a/drivers/ide/ide-io-std.c b/drivers/ide/ide-io-std.c
  7988. index 19763977568c..4169433faab5 100644
  7989. --- a/drivers/ide/ide-io-std.c
  7990. +++ b/drivers/ide/ide-io-std.c
  7991. @@ -175,7 +175,7 @@ void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  7992. unsigned long uninitialized_var(flags);
  7993. if ((io_32bit & 2) && !mmio) {
  7994. - local_irq_save(flags);
  7995. + local_irq_save_nort(flags);
  7996. ata_vlb_sync(io_ports->nsect_addr);
  7997. }
  7998. @@ -186,7 +186,7 @@ void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  7999. insl(data_addr, buf, words);
  8000. if ((io_32bit & 2) && !mmio)
  8001. - local_irq_restore(flags);
  8002. + local_irq_restore_nort(flags);
  8003. if (((len + 1) & 3) < 2)
  8004. return;
  8005. @@ -219,7 +219,7 @@ void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  8006. unsigned long uninitialized_var(flags);
  8007. if ((io_32bit & 2) && !mmio) {
  8008. - local_irq_save(flags);
  8009. + local_irq_save_nort(flags);
  8010. ata_vlb_sync(io_ports->nsect_addr);
  8011. }
  8012. @@ -230,7 +230,7 @@ void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  8013. outsl(data_addr, buf, words);
  8014. if ((io_32bit & 2) && !mmio)
  8015. - local_irq_restore(flags);
  8016. + local_irq_restore_nort(flags);
  8017. if (((len + 1) & 3) < 2)
  8018. return;
  8019. diff --git a/drivers/ide/ide-io.c b/drivers/ide/ide-io.c
  8020. index 669ea1e45795..e12e43e62245 100644
  8021. --- a/drivers/ide/ide-io.c
  8022. +++ b/drivers/ide/ide-io.c
  8023. @@ -659,7 +659,7 @@ void ide_timer_expiry (unsigned long data)
  8024. /* disable_irq_nosync ?? */
  8025. disable_irq(hwif->irq);
  8026. /* local CPU only, as if we were handling an interrupt */
  8027. - local_irq_disable();
  8028. + local_irq_disable_nort();
  8029. if (hwif->polling) {
  8030. startstop = handler(drive);
  8031. } else if (drive_is_ready(drive)) {
  8032. diff --git a/drivers/ide/ide-iops.c b/drivers/ide/ide-iops.c
  8033. index 376f2dc410c5..f014dd1b73dc 100644
  8034. --- a/drivers/ide/ide-iops.c
  8035. +++ b/drivers/ide/ide-iops.c
  8036. @@ -129,12 +129,12 @@ int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad,
  8037. if ((stat & ATA_BUSY) == 0)
  8038. break;
  8039. - local_irq_restore(flags);
  8040. + local_irq_restore_nort(flags);
  8041. *rstat = stat;
  8042. return -EBUSY;
  8043. }
  8044. }
  8045. - local_irq_restore(flags);
  8046. + local_irq_restore_nort(flags);
  8047. }
  8048. /*
  8049. * Allow status to settle, then read it again.
  8050. diff --git a/drivers/ide/ide-probe.c b/drivers/ide/ide-probe.c
  8051. index 0b63facd1d87..4ceba37afc0c 100644
  8052. --- a/drivers/ide/ide-probe.c
  8053. +++ b/drivers/ide/ide-probe.c
  8054. @@ -196,10 +196,10 @@ static void do_identify(ide_drive_t *drive, u8 cmd, u16 *id)
  8055. int bswap = 1;
  8056. /* local CPU only; some systems need this */
  8057. - local_irq_save(flags);
  8058. + local_irq_save_nort(flags);
  8059. /* read 512 bytes of id info */
  8060. hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
  8061. - local_irq_restore(flags);
  8062. + local_irq_restore_nort(flags);
  8063. drive->dev_flags |= IDE_DFLAG_ID_READ;
  8064. #ifdef DEBUG
  8065. diff --git a/drivers/ide/ide-taskfile.c b/drivers/ide/ide-taskfile.c
  8066. index a716693417a3..be0568c722d6 100644
  8067. --- a/drivers/ide/ide-taskfile.c
  8068. +++ b/drivers/ide/ide-taskfile.c
  8069. @@ -250,7 +250,7 @@ void ide_pio_bytes(ide_drive_t *drive, struct ide_cmd *cmd,
  8070. page_is_high = PageHighMem(page);
  8071. if (page_is_high)
  8072. - local_irq_save(flags);
  8073. + local_irq_save_nort(flags);
  8074. buf = kmap_atomic(page) + offset;
  8075. @@ -271,7 +271,7 @@ void ide_pio_bytes(ide_drive_t *drive, struct ide_cmd *cmd,
  8076. kunmap_atomic(buf);
  8077. if (page_is_high)
  8078. - local_irq_restore(flags);
  8079. + local_irq_restore_nort(flags);
  8080. len -= nr_bytes;
  8081. }
  8082. @@ -414,7 +414,7 @@ static ide_startstop_t pre_task_out_intr(ide_drive_t *drive,
  8083. }
  8084. if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
  8085. - local_irq_disable();
  8086. + local_irq_disable_nort();
  8087. ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
  8088. diff --git a/drivers/infiniband/ulp/ipoib/ipoib_multicast.c b/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  8089. index 5580ab0b5781..a123d0439c4c 100644
  8090. --- a/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  8091. +++ b/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  8092. @@ -862,7 +862,7 @@ void ipoib_mcast_restart_task(struct work_struct *work)
  8093. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  8094. - local_irq_save(flags);
  8095. + local_irq_save_nort(flags);
  8096. netif_addr_lock(dev);
  8097. spin_lock(&priv->lock);
  8098. @@ -944,7 +944,7 @@ void ipoib_mcast_restart_task(struct work_struct *work)
  8099. spin_unlock(&priv->lock);
  8100. netif_addr_unlock(dev);
  8101. - local_irq_restore(flags);
  8102. + local_irq_restore_nort(flags);
  8103. /*
  8104. * make sure the in-flight joins have finished before we attempt
  8105. diff --git a/drivers/input/gameport/gameport.c b/drivers/input/gameport/gameport.c
  8106. index 4a2a9e370be7..e970d9afd179 100644
  8107. --- a/drivers/input/gameport/gameport.c
  8108. +++ b/drivers/input/gameport/gameport.c
  8109. @@ -91,13 +91,13 @@ static int gameport_measure_speed(struct gameport *gameport)
  8110. tx = ~0;
  8111. for (i = 0; i < 50; i++) {
  8112. - local_irq_save(flags);
  8113. + local_irq_save_nort(flags);
  8114. t1 = ktime_get_ns();
  8115. for (t = 0; t < 50; t++)
  8116. gameport_read(gameport);
  8117. t2 = ktime_get_ns();
  8118. t3 = ktime_get_ns();
  8119. - local_irq_restore(flags);
  8120. + local_irq_restore_nort(flags);
  8121. udelay(i * 10);
  8122. t = (t2 - t1) - (t3 - t2);
  8123. if (t < tx)
  8124. @@ -124,12 +124,12 @@ static int old_gameport_measure_speed(struct gameport *gameport)
  8125. tx = 1 << 30;
  8126. for(i = 0; i < 50; i++) {
  8127. - local_irq_save(flags);
  8128. + local_irq_save_nort(flags);
  8129. GET_TIME(t1);
  8130. for (t = 0; t < 50; t++) gameport_read(gameport);
  8131. GET_TIME(t2);
  8132. GET_TIME(t3);
  8133. - local_irq_restore(flags);
  8134. + local_irq_restore_nort(flags);
  8135. udelay(i * 10);
  8136. if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
  8137. }
  8138. @@ -148,11 +148,11 @@ static int old_gameport_measure_speed(struct gameport *gameport)
  8139. tx = 1 << 30;
  8140. for(i = 0; i < 50; i++) {
  8141. - local_irq_save(flags);
  8142. + local_irq_save_nort(flags);
  8143. t1 = rdtsc();
  8144. for (t = 0; t < 50; t++) gameport_read(gameport);
  8145. t2 = rdtsc();
  8146. - local_irq_restore(flags);
  8147. + local_irq_restore_nort(flags);
  8148. udelay(i * 10);
  8149. if (t2 - t1 < tx) tx = t2 - t1;
  8150. }
  8151. diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c
  8152. index 52c36394dba5..d777d0197f64 100644
  8153. --- a/drivers/iommu/amd_iommu.c
  8154. +++ b/drivers/iommu/amd_iommu.c
  8155. @@ -2022,10 +2022,10 @@ static int __attach_device(struct iommu_dev_data *dev_data,
  8156. int ret;
  8157. /*
  8158. - * Must be called with IRQs disabled. Warn here to detect early
  8159. - * when its not.
  8160. + * Must be called with IRQs disabled on a non RT kernel. Warn here to
  8161. + * detect early when its not.
  8162. */
  8163. - WARN_ON(!irqs_disabled());
  8164. + WARN_ON_NONRT(!irqs_disabled());
  8165. /* lock domain */
  8166. spin_lock(&domain->lock);
  8167. @@ -2188,10 +2188,10 @@ static void __detach_device(struct iommu_dev_data *dev_data)
  8168. struct protection_domain *domain;
  8169. /*
  8170. - * Must be called with IRQs disabled. Warn here to detect early
  8171. - * when its not.
  8172. + * Must be called with IRQs disabled on a non RT kernel. Warn here to
  8173. + * detect early when its not.
  8174. */
  8175. - WARN_ON(!irqs_disabled());
  8176. + WARN_ON_NONRT(!irqs_disabled());
  8177. if (WARN_ON(!dev_data->domain))
  8178. return;
  8179. diff --git a/drivers/leds/trigger/Kconfig b/drivers/leds/trigger/Kconfig
  8180. index 5bda6a9b56bb..d6286584c807 100644
  8181. --- a/drivers/leds/trigger/Kconfig
  8182. +++ b/drivers/leds/trigger/Kconfig
  8183. @@ -61,7 +61,7 @@ config LEDS_TRIGGER_BACKLIGHT
  8184. config LEDS_TRIGGER_CPU
  8185. bool "LED CPU Trigger"
  8186. - depends on LEDS_TRIGGERS
  8187. + depends on LEDS_TRIGGERS && !PREEMPT_RT_BASE
  8188. help
  8189. This allows LEDs to be controlled by active CPUs. This shows
  8190. the active CPUs across an array of LEDs so you can see which
  8191. diff --git a/drivers/md/bcache/Kconfig b/drivers/md/bcache/Kconfig
  8192. index 4d200883c505..98b64ed5cb81 100644
  8193. --- a/drivers/md/bcache/Kconfig
  8194. +++ b/drivers/md/bcache/Kconfig
  8195. @@ -1,6 +1,7 @@
  8196. config BCACHE
  8197. tristate "Block device as cache"
  8198. + depends on !PREEMPT_RT_FULL
  8199. ---help---
  8200. Allows a block device to be used as cache for other devices; uses
  8201. a btree for indexing and the layout is optimized for SSDs.
  8202. diff --git a/drivers/md/dm.c b/drivers/md/dm.c
  8203. index 9ec6948e3b8b..ecbc23575114 100644
  8204. --- a/drivers/md/dm.c
  8205. +++ b/drivers/md/dm.c
  8206. @@ -2185,7 +2185,7 @@ static void dm_request_fn(struct request_queue *q)
  8207. /* Establish tio->ti before queuing work (map_tio_request) */
  8208. tio->ti = ti;
  8209. queue_kthread_work(&md->kworker, &tio->work);
  8210. - BUG_ON(!irqs_disabled());
  8211. + BUG_ON_NONRT(!irqs_disabled());
  8212. }
  8213. goto out;
  8214. diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c
  8215. index 86ab6d14d782..573b9ac810da 100644
  8216. --- a/drivers/md/raid5.c
  8217. +++ b/drivers/md/raid5.c
  8218. @@ -429,7 +429,7 @@ void raid5_release_stripe(struct stripe_head *sh)
  8219. md_wakeup_thread(conf->mddev->thread);
  8220. return;
  8221. slow_path:
  8222. - local_irq_save(flags);
  8223. + local_irq_save_nort(flags);
  8224. /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
  8225. if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
  8226. INIT_LIST_HEAD(&list);
  8227. @@ -438,7 +438,7 @@ void raid5_release_stripe(struct stripe_head *sh)
  8228. spin_unlock(&conf->device_lock);
  8229. release_inactive_stripe_list(conf, &list, hash);
  8230. }
  8231. - local_irq_restore(flags);
  8232. + local_irq_restore_nort(flags);
  8233. }
  8234. static inline void remove_hash(struct stripe_head *sh)
  8235. @@ -1929,8 +1929,9 @@ static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  8236. struct raid5_percpu *percpu;
  8237. unsigned long cpu;
  8238. - cpu = get_cpu();
  8239. + cpu = get_cpu_light();
  8240. percpu = per_cpu_ptr(conf->percpu, cpu);
  8241. + spin_lock(&percpu->lock);
  8242. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  8243. ops_run_biofill(sh);
  8244. overlap_clear++;
  8245. @@ -1986,7 +1987,8 @@ static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  8246. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  8247. wake_up(&sh->raid_conf->wait_for_overlap);
  8248. }
  8249. - put_cpu();
  8250. + spin_unlock(&percpu->lock);
  8251. + put_cpu_light();
  8252. }
  8253. static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp)
  8254. @@ -6433,6 +6435,7 @@ static int raid5_alloc_percpu(struct r5conf *conf)
  8255. __func__, cpu);
  8256. break;
  8257. }
  8258. + spin_lock_init(&per_cpu_ptr(conf->percpu, cpu)->lock);
  8259. }
  8260. put_online_cpus();
  8261. diff --git a/drivers/md/raid5.h b/drivers/md/raid5.h
  8262. index 517d4b68a1be..efe91887ecd7 100644
  8263. --- a/drivers/md/raid5.h
  8264. +++ b/drivers/md/raid5.h
  8265. @@ -504,6 +504,7 @@ struct r5conf {
  8266. int recovery_disabled;
  8267. /* per cpu variables */
  8268. struct raid5_percpu {
  8269. + spinlock_t lock; /* Protection for -RT */
  8270. struct page *spare_page; /* Used when checking P/Q in raid6 */
  8271. struct flex_array *scribble; /* space for constructing buffer
  8272. * lists and performing address
  8273. diff --git a/drivers/media/platform/vsp1/vsp1_video.c b/drivers/media/platform/vsp1/vsp1_video.c
  8274. index 5ce88e1f5d71..b4f8cd74ecb8 100644
  8275. --- a/drivers/media/platform/vsp1/vsp1_video.c
  8276. +++ b/drivers/media/platform/vsp1/vsp1_video.c
  8277. @@ -520,7 +520,7 @@ static bool vsp1_pipeline_stopped(struct vsp1_pipeline *pipe)
  8278. bool stopped;
  8279. spin_lock_irqsave(&pipe->irqlock, flags);
  8280. - stopped = pipe->state == VSP1_PIPELINE_STOPPED,
  8281. + stopped = pipe->state == VSP1_PIPELINE_STOPPED;
  8282. spin_unlock_irqrestore(&pipe->irqlock, flags);
  8283. return stopped;
  8284. diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
  8285. index 4bf7d50b1bc7..6f7e99ad6e29 100644
  8286. --- a/drivers/misc/Kconfig
  8287. +++ b/drivers/misc/Kconfig
  8288. @@ -54,6 +54,7 @@ config AD525X_DPOT_SPI
  8289. config ATMEL_TCLIB
  8290. bool "Atmel AT32/AT91 Timer/Counter Library"
  8291. depends on (AVR32 || ARCH_AT91)
  8292. + default y if PREEMPT_RT_FULL
  8293. help
  8294. Select this if you want a library to allocate the Timer/Counter
  8295. blocks found on many Atmel processors. This facilitates using
  8296. @@ -69,8 +70,7 @@ config ATMEL_TCB_CLKSRC
  8297. are combined to make a single 32-bit timer.
  8298. When GENERIC_CLOCKEVENTS is defined, the third timer channel
  8299. - may be used as a clock event device supporting oneshot mode
  8300. - (delays of up to two seconds) based on the 32 KiHz clock.
  8301. + may be used as a clock event device supporting oneshot mode.
  8302. config ATMEL_TCB_CLKSRC_BLOCK
  8303. int
  8304. @@ -84,6 +84,15 @@ config ATMEL_TCB_CLKSRC_BLOCK
  8305. TC can be used for other purposes, such as PWM generation and
  8306. interval timing.
  8307. +config ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  8308. + bool "TC Block use 32 KiHz clock"
  8309. + depends on ATMEL_TCB_CLKSRC
  8310. + default y if !PREEMPT_RT_FULL
  8311. + help
  8312. + Select this to use 32 KiHz base clock rate as TC block clock
  8313. + source for clock events.
  8314. +
  8315. +
  8316. config DUMMY_IRQ
  8317. tristate "Dummy IRQ handler"
  8318. default n
  8319. @@ -113,6 +122,35 @@ config IBM_ASM
  8320. for information on the specific driver level and support statement
  8321. for your IBM server.
  8322. +config HWLAT_DETECTOR
  8323. + tristate "Testing module to detect hardware-induced latencies"
  8324. + depends on DEBUG_FS
  8325. + depends on RING_BUFFER
  8326. + default m
  8327. + ---help---
  8328. + A simple hardware latency detector. Use this module to detect
  8329. + large latencies introduced by the behavior of the underlying
  8330. + system firmware external to Linux. We do this using periodic
  8331. + use of stop_machine to grab all available CPUs and measure
  8332. + for unexplainable gaps in the CPU timestamp counter(s). By
  8333. + default, the module is not enabled until the "enable" file
  8334. + within the "hwlat_detector" debugfs directory is toggled.
  8335. +
  8336. + This module is often used to detect SMI (System Management
  8337. + Interrupts) on x86 systems, though is not x86 specific. To
  8338. + this end, we default to using a sample window of 1 second,
  8339. + during which we will sample for 0.5 seconds. If an SMI or
  8340. + similar event occurs during that time, it is recorded
  8341. + into an 8K samples global ring buffer until retreived.
  8342. +
  8343. + WARNING: This software should never be enabled (it can be built
  8344. + but should not be turned on after it is loaded) in a production
  8345. + environment where high latencies are a concern since the
  8346. + sampling mechanism actually introduces latencies for
  8347. + regular tasks while the CPU(s) are being held.
  8348. +
  8349. + If unsure, say N
  8350. +
  8351. config PHANTOM
  8352. tristate "Sensable PHANToM (PCI)"
  8353. depends on PCI
  8354. diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
  8355. index 537d7f3b78da..ec4aecba0656 100644
  8356. --- a/drivers/misc/Makefile
  8357. +++ b/drivers/misc/Makefile
  8358. @@ -39,6 +39,7 @@ obj-$(CONFIG_C2PORT) += c2port/
  8359. obj-$(CONFIG_HMC6352) += hmc6352.o
  8360. obj-y += eeprom/
  8361. obj-y += cb710/
  8362. +obj-$(CONFIG_HWLAT_DETECTOR) += hwlat_detector.o
  8363. obj-$(CONFIG_SPEAR13XX_PCIE_GADGET) += spear13xx_pcie_gadget.o
  8364. obj-$(CONFIG_VMWARE_BALLOON) += vmw_balloon.o
  8365. obj-$(CONFIG_ARM_CHARLCD) += arm-charlcd.o
  8366. diff --git a/drivers/misc/hwlat_detector.c b/drivers/misc/hwlat_detector.c
  8367. new file mode 100644
  8368. index 000000000000..52f5ad5fd9c0
  8369. --- /dev/null
  8370. +++ b/drivers/misc/hwlat_detector.c
  8371. @@ -0,0 +1,1240 @@
  8372. +/*
  8373. + * hwlat_detector.c - A simple Hardware Latency detector.
  8374. + *
  8375. + * Use this module to detect large system latencies induced by the behavior of
  8376. + * certain underlying system hardware or firmware, independent of Linux itself.
  8377. + * The code was developed originally to detect the presence of SMIs on Intel
  8378. + * and AMD systems, although there is no dependency upon x86 herein.
  8379. + *
  8380. + * The classical example usage of this module is in detecting the presence of
  8381. + * SMIs or System Management Interrupts on Intel and AMD systems. An SMI is a
  8382. + * somewhat special form of hardware interrupt spawned from earlier CPU debug
  8383. + * modes in which the (BIOS/EFI/etc.) firmware arranges for the South Bridge
  8384. + * LPC (or other device) to generate a special interrupt under certain
  8385. + * circumstances, for example, upon expiration of a special SMI timer device,
  8386. + * due to certain external thermal readings, on certain I/O address accesses,
  8387. + * and other situations. An SMI hits a special CPU pin, triggers a special
  8388. + * SMI mode (complete with special memory map), and the OS is unaware.
  8389. + *
  8390. + * Although certain hardware-inducing latencies are necessary (for example,
  8391. + * a modern system often requires an SMI handler for correct thermal control
  8392. + * and remote management) they can wreak havoc upon any OS-level performance
  8393. + * guarantees toward low-latency, especially when the OS is not even made
  8394. + * aware of the presence of these interrupts. For this reason, we need a
  8395. + * somewhat brute force mechanism to detect these interrupts. In this case,
  8396. + * we do it by hogging all of the CPU(s) for configurable timer intervals,
  8397. + * sampling the built-in CPU timer, looking for discontiguous readings.
  8398. + *
  8399. + * WARNING: This implementation necessarily introduces latencies. Therefore,
  8400. + * you should NEVER use this module in a production environment
  8401. + * requiring any kind of low-latency performance guarantee(s).
  8402. + *
  8403. + * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com>
  8404. + *
  8405. + * Includes useful feedback from Clark Williams <clark@redhat.com>
  8406. + *
  8407. + * This file is licensed under the terms of the GNU General Public
  8408. + * License version 2. This program is licensed "as is" without any
  8409. + * warranty of any kind, whether express or implied.
  8410. + */
  8411. +
  8412. +#include <linux/module.h>
  8413. +#include <linux/init.h>
  8414. +#include <linux/ring_buffer.h>
  8415. +#include <linux/time.h>
  8416. +#include <linux/hrtimer.h>
  8417. +#include <linux/kthread.h>
  8418. +#include <linux/debugfs.h>
  8419. +#include <linux/seq_file.h>
  8420. +#include <linux/uaccess.h>
  8421. +#include <linux/version.h>
  8422. +#include <linux/delay.h>
  8423. +#include <linux/slab.h>
  8424. +#include <linux/trace_clock.h>
  8425. +
  8426. +#define BUF_SIZE_DEFAULT 262144UL /* 8K*(sizeof(entry)) */
  8427. +#define BUF_FLAGS (RB_FL_OVERWRITE) /* no block on full */
  8428. +#define U64STR_SIZE 22 /* 20 digits max */
  8429. +
  8430. +#define VERSION "1.0.0"
  8431. +#define BANNER "hwlat_detector: "
  8432. +#define DRVNAME "hwlat_detector"
  8433. +#define DEFAULT_SAMPLE_WINDOW 1000000 /* 1s */
  8434. +#define DEFAULT_SAMPLE_WIDTH 500000 /* 0.5s */
  8435. +#define DEFAULT_LAT_THRESHOLD 10 /* 10us */
  8436. +
  8437. +/* Module metadata */
  8438. +
  8439. +MODULE_LICENSE("GPL");
  8440. +MODULE_AUTHOR("Jon Masters <jcm@redhat.com>");
  8441. +MODULE_DESCRIPTION("A simple hardware latency detector");
  8442. +MODULE_VERSION(VERSION);
  8443. +
  8444. +/* Module parameters */
  8445. +
  8446. +static int debug;
  8447. +static int enabled;
  8448. +static int threshold;
  8449. +
  8450. +module_param(debug, int, 0); /* enable debug */
  8451. +module_param(enabled, int, 0); /* enable detector */
  8452. +module_param(threshold, int, 0); /* latency threshold */
  8453. +
  8454. +/* Buffering and sampling */
  8455. +
  8456. +static struct ring_buffer *ring_buffer; /* sample buffer */
  8457. +static DEFINE_MUTEX(ring_buffer_mutex); /* lock changes */
  8458. +static unsigned long buf_size = BUF_SIZE_DEFAULT;
  8459. +static struct task_struct *kthread; /* sampling thread */
  8460. +
  8461. +/* DebugFS filesystem entries */
  8462. +
  8463. +static struct dentry *debug_dir; /* debugfs directory */
  8464. +static struct dentry *debug_max; /* maximum TSC delta */
  8465. +static struct dentry *debug_count; /* total detect count */
  8466. +static struct dentry *debug_sample_width; /* sample width us */
  8467. +static struct dentry *debug_sample_window; /* sample window us */
  8468. +static struct dentry *debug_sample; /* raw samples us */
  8469. +static struct dentry *debug_threshold; /* threshold us */
  8470. +static struct dentry *debug_enable; /* enable/disable */
  8471. +
  8472. +/* Individual samples and global state */
  8473. +
  8474. +struct sample; /* latency sample */
  8475. +struct data; /* Global state */
  8476. +
  8477. +/* Sampling functions */
  8478. +static int __buffer_add_sample(struct sample *sample);
  8479. +static struct sample *buffer_get_sample(struct sample *sample);
  8480. +
  8481. +/* Threading and state */
  8482. +static int kthread_fn(void *unused);
  8483. +static int start_kthread(void);
  8484. +static int stop_kthread(void);
  8485. +static void __reset_stats(void);
  8486. +static int init_stats(void);
  8487. +
  8488. +/* Debugfs interface */
  8489. +static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
  8490. + size_t cnt, loff_t *ppos, const u64 *entry);
  8491. +static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
  8492. + size_t cnt, loff_t *ppos, u64 *entry);
  8493. +static int debug_sample_fopen(struct inode *inode, struct file *filp);
  8494. +static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
  8495. + size_t cnt, loff_t *ppos);
  8496. +static int debug_sample_release(struct inode *inode, struct file *filp);
  8497. +static int debug_enable_fopen(struct inode *inode, struct file *filp);
  8498. +static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
  8499. + size_t cnt, loff_t *ppos);
  8500. +static ssize_t debug_enable_fwrite(struct file *file,
  8501. + const char __user *user_buffer,
  8502. + size_t user_size, loff_t *offset);
  8503. +
  8504. +/* Initialization functions */
  8505. +static int init_debugfs(void);
  8506. +static void free_debugfs(void);
  8507. +static int detector_init(void);
  8508. +static void detector_exit(void);
  8509. +
  8510. +/* Individual latency samples are stored here when detected and packed into
  8511. + * the ring_buffer circular buffer, where they are overwritten when
  8512. + * more than buf_size/sizeof(sample) samples are received. */
  8513. +struct sample {
  8514. + u64 seqnum; /* unique sequence */
  8515. + u64 duration; /* ktime delta */
  8516. + u64 outer_duration; /* ktime delta (outer loop) */
  8517. + struct timespec timestamp; /* wall time */
  8518. + unsigned long lost;
  8519. +};
  8520. +
  8521. +/* keep the global state somewhere. */
  8522. +static struct data {
  8523. +
  8524. + struct mutex lock; /* protect changes */
  8525. +
  8526. + u64 count; /* total since reset */
  8527. + u64 max_sample; /* max hardware latency */
  8528. + u64 threshold; /* sample threshold level */
  8529. +
  8530. + u64 sample_window; /* total sampling window (on+off) */
  8531. + u64 sample_width; /* active sampling portion of window */
  8532. +
  8533. + atomic_t sample_open; /* whether the sample file is open */
  8534. +
  8535. + wait_queue_head_t wq; /* waitqeue for new sample values */
  8536. +
  8537. +} data;
  8538. +
  8539. +/**
  8540. + * __buffer_add_sample - add a new latency sample recording to the ring buffer
  8541. + * @sample: The new latency sample value
  8542. + *
  8543. + * This receives a new latency sample and records it in a global ring buffer.
  8544. + * No additional locking is used in this case.
  8545. + */
  8546. +static int __buffer_add_sample(struct sample *sample)
  8547. +{
  8548. + return ring_buffer_write(ring_buffer,
  8549. + sizeof(struct sample), sample);
  8550. +}
  8551. +
  8552. +/**
  8553. + * buffer_get_sample - remove a hardware latency sample from the ring buffer
  8554. + * @sample: Pre-allocated storage for the sample
  8555. + *
  8556. + * This retrieves a hardware latency sample from the global circular buffer
  8557. + */
  8558. +static struct sample *buffer_get_sample(struct sample *sample)
  8559. +{
  8560. + struct ring_buffer_event *e = NULL;
  8561. + struct sample *s = NULL;
  8562. + unsigned int cpu = 0;
  8563. +
  8564. + if (!sample)
  8565. + return NULL;
  8566. +
  8567. + mutex_lock(&ring_buffer_mutex);
  8568. + for_each_online_cpu(cpu) {
  8569. + e = ring_buffer_consume(ring_buffer, cpu, NULL, &sample->lost);
  8570. + if (e)
  8571. + break;
  8572. + }
  8573. +
  8574. + if (e) {
  8575. + s = ring_buffer_event_data(e);
  8576. + memcpy(sample, s, sizeof(struct sample));
  8577. + } else
  8578. + sample = NULL;
  8579. + mutex_unlock(&ring_buffer_mutex);
  8580. +
  8581. + return sample;
  8582. +}
  8583. +
  8584. +#ifndef CONFIG_TRACING
  8585. +#define time_type ktime_t
  8586. +#define time_get() ktime_get()
  8587. +#define time_to_us(x) ktime_to_us(x)
  8588. +#define time_sub(a, b) ktime_sub(a, b)
  8589. +#define init_time(a, b) (a).tv64 = b
  8590. +#define time_u64(a) ((a).tv64)
  8591. +#else
  8592. +#define time_type u64
  8593. +#define time_get() trace_clock_local()
  8594. +#define time_to_us(x) div_u64(x, 1000)
  8595. +#define time_sub(a, b) ((a) - (b))
  8596. +#define init_time(a, b) (a = b)
  8597. +#define time_u64(a) a
  8598. +#endif
  8599. +/**
  8600. + * get_sample - sample the CPU TSC and look for likely hardware latencies
  8601. + *
  8602. + * Used to repeatedly capture the CPU TSC (or similar), looking for potential
  8603. + * hardware-induced latency. Called with interrupts disabled and with
  8604. + * data.lock held.
  8605. + */
  8606. +static int get_sample(void)
  8607. +{
  8608. + time_type start, t1, t2, last_t2;
  8609. + s64 diff, total = 0;
  8610. + u64 sample = 0;
  8611. + u64 outer_sample = 0;
  8612. + int ret = -1;
  8613. +
  8614. + init_time(last_t2, 0);
  8615. + start = time_get(); /* start timestamp */
  8616. +
  8617. + do {
  8618. +
  8619. + t1 = time_get(); /* we'll look for a discontinuity */
  8620. + t2 = time_get();
  8621. +
  8622. + if (time_u64(last_t2)) {
  8623. + /* Check the delta from outer loop (t2 to next t1) */
  8624. + diff = time_to_us(time_sub(t1, last_t2));
  8625. + /* This shouldn't happen */
  8626. + if (diff < 0) {
  8627. + pr_err(BANNER "time running backwards\n");
  8628. + goto out;
  8629. + }
  8630. + if (diff > outer_sample)
  8631. + outer_sample = diff;
  8632. + }
  8633. + last_t2 = t2;
  8634. +
  8635. + total = time_to_us(time_sub(t2, start)); /* sample width */
  8636. +
  8637. + /* This checks the inner loop (t1 to t2) */
  8638. + diff = time_to_us(time_sub(t2, t1)); /* current diff */
  8639. +
  8640. + /* This shouldn't happen */
  8641. + if (diff < 0) {
  8642. + pr_err(BANNER "time running backwards\n");
  8643. + goto out;
  8644. + }
  8645. +
  8646. + if (diff > sample)
  8647. + sample = diff; /* only want highest value */
  8648. +
  8649. + } while (total <= data.sample_width);
  8650. +
  8651. + ret = 0;
  8652. +
  8653. + /* If we exceed the threshold value, we have found a hardware latency */
  8654. + if (sample > data.threshold || outer_sample > data.threshold) {
  8655. + struct sample s;
  8656. +
  8657. + ret = 1;
  8658. +
  8659. + data.count++;
  8660. + s.seqnum = data.count;
  8661. + s.duration = sample;
  8662. + s.outer_duration = outer_sample;
  8663. + s.timestamp = CURRENT_TIME;
  8664. + __buffer_add_sample(&s);
  8665. +
  8666. + /* Keep a running maximum ever recorded hardware latency */
  8667. + if (sample > data.max_sample)
  8668. + data.max_sample = sample;
  8669. + }
  8670. +
  8671. +out:
  8672. + return ret;
  8673. +}
  8674. +
  8675. +/*
  8676. + * kthread_fn - The CPU time sampling/hardware latency detection kernel thread
  8677. + * @unused: A required part of the kthread API.
  8678. + *
  8679. + * Used to periodically sample the CPU TSC via a call to get_sample. We
  8680. + * disable interrupts, which does (intentionally) introduce latency since we
  8681. + * need to ensure nothing else might be running (and thus pre-empting).
  8682. + * Obviously this should never be used in production environments.
  8683. + *
  8684. + * Currently this runs on which ever CPU it was scheduled on, but most
  8685. + * real-worald hardware latency situations occur across several CPUs,
  8686. + * but we might later generalize this if we find there are any actualy
  8687. + * systems with alternate SMI delivery or other hardware latencies.
  8688. + */
  8689. +static int kthread_fn(void *unused)
  8690. +{
  8691. + int ret;
  8692. + u64 interval;
  8693. +
  8694. + while (!kthread_should_stop()) {
  8695. +
  8696. + mutex_lock(&data.lock);
  8697. +
  8698. + local_irq_disable();
  8699. + ret = get_sample();
  8700. + local_irq_enable();
  8701. +
  8702. + if (ret > 0)
  8703. + wake_up(&data.wq); /* wake up reader(s) */
  8704. +
  8705. + interval = data.sample_window - data.sample_width;
  8706. + do_div(interval, USEC_PER_MSEC); /* modifies interval value */
  8707. +
  8708. + mutex_unlock(&data.lock);
  8709. +
  8710. + if (msleep_interruptible(interval))
  8711. + break;
  8712. + }
  8713. +
  8714. + return 0;
  8715. +}
  8716. +
  8717. +/**
  8718. + * start_kthread - Kick off the hardware latency sampling/detector kthread
  8719. + *
  8720. + * This starts a kernel thread that will sit and sample the CPU timestamp
  8721. + * counter (TSC or similar) and look for potential hardware latencies.
  8722. + */
  8723. +static int start_kthread(void)
  8724. +{
  8725. + kthread = kthread_run(kthread_fn, NULL,
  8726. + DRVNAME);
  8727. + if (IS_ERR(kthread)) {
  8728. + pr_err(BANNER "could not start sampling thread\n");
  8729. + enabled = 0;
  8730. + return -ENOMEM;
  8731. + }
  8732. +
  8733. + return 0;
  8734. +}
  8735. +
  8736. +/**
  8737. + * stop_kthread - Inform the hardware latency samping/detector kthread to stop
  8738. + *
  8739. + * This kicks the running hardware latency sampling/detector kernel thread and
  8740. + * tells it to stop sampling now. Use this on unload and at system shutdown.
  8741. + */
  8742. +static int stop_kthread(void)
  8743. +{
  8744. + int ret;
  8745. +
  8746. + ret = kthread_stop(kthread);
  8747. +
  8748. + return ret;
  8749. +}
  8750. +
  8751. +/**
  8752. + * __reset_stats - Reset statistics for the hardware latency detector
  8753. + *
  8754. + * We use data to store various statistics and global state. We call this
  8755. + * function in order to reset those when "enable" is toggled on or off, and
  8756. + * also at initialization. Should be called with data.lock held.
  8757. + */
  8758. +static void __reset_stats(void)
  8759. +{
  8760. + data.count = 0;
  8761. + data.max_sample = 0;
  8762. + ring_buffer_reset(ring_buffer); /* flush out old sample entries */
  8763. +}
  8764. +
  8765. +/**
  8766. + * init_stats - Setup global state statistics for the hardware latency detector
  8767. + *
  8768. + * We use data to store various statistics and global state. We also use
  8769. + * a global ring buffer (ring_buffer) to keep raw samples of detected hardware
  8770. + * induced system latencies. This function initializes these structures and
  8771. + * allocates the global ring buffer also.
  8772. + */
  8773. +static int init_stats(void)
  8774. +{
  8775. + int ret = -ENOMEM;
  8776. +
  8777. + mutex_init(&data.lock);
  8778. + init_waitqueue_head(&data.wq);
  8779. + atomic_set(&data.sample_open, 0);
  8780. +
  8781. + ring_buffer = ring_buffer_alloc(buf_size, BUF_FLAGS);
  8782. +
  8783. + if (WARN(!ring_buffer, KERN_ERR BANNER
  8784. + "failed to allocate ring buffer!\n"))
  8785. + goto out;
  8786. +
  8787. + __reset_stats();
  8788. + data.threshold = threshold ?: DEFAULT_LAT_THRESHOLD; /* threshold us */
  8789. + data.sample_window = DEFAULT_SAMPLE_WINDOW; /* window us */
  8790. + data.sample_width = DEFAULT_SAMPLE_WIDTH; /* width us */
  8791. +
  8792. + ret = 0;
  8793. +
  8794. +out:
  8795. + return ret;
  8796. +
  8797. +}
  8798. +
  8799. +/*
  8800. + * simple_data_read - Wrapper read function for global state debugfs entries
  8801. + * @filp: The active open file structure for the debugfs "file"
  8802. + * @ubuf: The userspace provided buffer to read value into
  8803. + * @cnt: The maximum number of bytes to read
  8804. + * @ppos: The current "file" position
  8805. + * @entry: The entry to read from
  8806. + *
  8807. + * This function provides a generic read implementation for the global state
  8808. + * "data" structure debugfs filesystem entries. It would be nice to use
  8809. + * simple_attr_read directly, but we need to make sure that the data.lock
  8810. + * is held during the actual read.
  8811. + */
  8812. +static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
  8813. + size_t cnt, loff_t *ppos, const u64 *entry)
  8814. +{
  8815. + char buf[U64STR_SIZE];
  8816. + u64 val = 0;
  8817. + int len = 0;
  8818. +
  8819. + memset(buf, 0, sizeof(buf));
  8820. +
  8821. + if (!entry)
  8822. + return -EFAULT;
  8823. +
  8824. + mutex_lock(&data.lock);
  8825. + val = *entry;
  8826. + mutex_unlock(&data.lock);
  8827. +
  8828. + len = snprintf(buf, sizeof(buf), "%llu\n", (unsigned long long)val);
  8829. +
  8830. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  8831. +
  8832. +}
  8833. +
  8834. +/*
  8835. + * simple_data_write - Wrapper write function for global state debugfs entries
  8836. + * @filp: The active open file structure for the debugfs "file"
  8837. + * @ubuf: The userspace provided buffer to write value from
  8838. + * @cnt: The maximum number of bytes to write
  8839. + * @ppos: The current "file" position
  8840. + * @entry: The entry to write to
  8841. + *
  8842. + * This function provides a generic write implementation for the global state
  8843. + * "data" structure debugfs filesystem entries. It would be nice to use
  8844. + * simple_attr_write directly, but we need to make sure that the data.lock
  8845. + * is held during the actual write.
  8846. + */
  8847. +static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
  8848. + size_t cnt, loff_t *ppos, u64 *entry)
  8849. +{
  8850. + char buf[U64STR_SIZE];
  8851. + int csize = min(cnt, sizeof(buf));
  8852. + u64 val = 0;
  8853. + int err = 0;
  8854. +
  8855. + memset(buf, '\0', sizeof(buf));
  8856. + if (copy_from_user(buf, ubuf, csize))
  8857. + return -EFAULT;
  8858. +
  8859. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  8860. + err = kstrtoull(buf, 10, &val);
  8861. + if (err)
  8862. + return -EINVAL;
  8863. +
  8864. + mutex_lock(&data.lock);
  8865. + *entry = val;
  8866. + mutex_unlock(&data.lock);
  8867. +
  8868. + return csize;
  8869. +}
  8870. +
  8871. +/**
  8872. + * debug_count_fopen - Open function for "count" debugfs entry
  8873. + * @inode: The in-kernel inode representation of the debugfs "file"
  8874. + * @filp: The active open file structure for the debugfs "file"
  8875. + *
  8876. + * This function provides an open implementation for the "count" debugfs
  8877. + * interface to the hardware latency detector.
  8878. + */
  8879. +static int debug_count_fopen(struct inode *inode, struct file *filp)
  8880. +{
  8881. + return 0;
  8882. +}
  8883. +
  8884. +/**
  8885. + * debug_count_fread - Read function for "count" debugfs entry
  8886. + * @filp: The active open file structure for the debugfs "file"
  8887. + * @ubuf: The userspace provided buffer to read value into
  8888. + * @cnt: The maximum number of bytes to read
  8889. + * @ppos: The current "file" position
  8890. + *
  8891. + * This function provides a read implementation for the "count" debugfs
  8892. + * interface to the hardware latency detector. Can be used to read the
  8893. + * number of latency readings exceeding the configured threshold since
  8894. + * the detector was last reset (e.g. by writing a zero into "count").
  8895. + */
  8896. +static ssize_t debug_count_fread(struct file *filp, char __user *ubuf,
  8897. + size_t cnt, loff_t *ppos)
  8898. +{
  8899. + return simple_data_read(filp, ubuf, cnt, ppos, &data.count);
  8900. +}
  8901. +
  8902. +/**
  8903. + * debug_count_fwrite - Write function for "count" debugfs entry
  8904. + * @filp: The active open file structure for the debugfs "file"
  8905. + * @ubuf: The user buffer that contains the value to write
  8906. + * @cnt: The maximum number of bytes to write to "file"
  8907. + * @ppos: The current position in the debugfs "file"
  8908. + *
  8909. + * This function provides a write implementation for the "count" debugfs
  8910. + * interface to the hardware latency detector. Can be used to write a
  8911. + * desired value, especially to zero the total count.
  8912. + */
  8913. +static ssize_t debug_count_fwrite(struct file *filp,
  8914. + const char __user *ubuf,
  8915. + size_t cnt,
  8916. + loff_t *ppos)
  8917. +{
  8918. + return simple_data_write(filp, ubuf, cnt, ppos, &data.count);
  8919. +}
  8920. +
  8921. +/**
  8922. + * debug_enable_fopen - Dummy open function for "enable" debugfs interface
  8923. + * @inode: The in-kernel inode representation of the debugfs "file"
  8924. + * @filp: The active open file structure for the debugfs "file"
  8925. + *
  8926. + * This function provides an open implementation for the "enable" debugfs
  8927. + * interface to the hardware latency detector.
  8928. + */
  8929. +static int debug_enable_fopen(struct inode *inode, struct file *filp)
  8930. +{
  8931. + return 0;
  8932. +}
  8933. +
  8934. +/**
  8935. + * debug_enable_fread - Read function for "enable" debugfs interface
  8936. + * @filp: The active open file structure for the debugfs "file"
  8937. + * @ubuf: The userspace provided buffer to read value into
  8938. + * @cnt: The maximum number of bytes to read
  8939. + * @ppos: The current "file" position
  8940. + *
  8941. + * This function provides a read implementation for the "enable" debugfs
  8942. + * interface to the hardware latency detector. Can be used to determine
  8943. + * whether the detector is currently enabled ("0\n" or "1\n" returned).
  8944. + */
  8945. +static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
  8946. + size_t cnt, loff_t *ppos)
  8947. +{
  8948. + char buf[4];
  8949. +
  8950. + if ((cnt < sizeof(buf)) || (*ppos))
  8951. + return 0;
  8952. +
  8953. + buf[0] = enabled ? '1' : '0';
  8954. + buf[1] = '\n';
  8955. + buf[2] = '\0';
  8956. + if (copy_to_user(ubuf, buf, strlen(buf)))
  8957. + return -EFAULT;
  8958. + return *ppos = strlen(buf);
  8959. +}
  8960. +
  8961. +/**
  8962. + * debug_enable_fwrite - Write function for "enable" debugfs interface
  8963. + * @filp: The active open file structure for the debugfs "file"
  8964. + * @ubuf: The user buffer that contains the value to write
  8965. + * @cnt: The maximum number of bytes to write to "file"
  8966. + * @ppos: The current position in the debugfs "file"
  8967. + *
  8968. + * This function provides a write implementation for the "enable" debugfs
  8969. + * interface to the hardware latency detector. Can be used to enable or
  8970. + * disable the detector, which will have the side-effect of possibly
  8971. + * also resetting the global stats and kicking off the measuring
  8972. + * kthread (on an enable) or the converse (upon a disable).
  8973. + */
  8974. +static ssize_t debug_enable_fwrite(struct file *filp,
  8975. + const char __user *ubuf,
  8976. + size_t cnt,
  8977. + loff_t *ppos)
  8978. +{
  8979. + char buf[4];
  8980. + int csize = min(cnt, sizeof(buf));
  8981. + long val = 0;
  8982. + int err = 0;
  8983. +
  8984. + memset(buf, '\0', sizeof(buf));
  8985. + if (copy_from_user(buf, ubuf, csize))
  8986. + return -EFAULT;
  8987. +
  8988. + buf[sizeof(buf)-1] = '\0'; /* just in case */
  8989. + err = kstrtoul(buf, 10, &val);
  8990. + if (err)
  8991. + return -EINVAL;
  8992. +
  8993. + if (val) {
  8994. + if (enabled)
  8995. + goto unlock;
  8996. + enabled = 1;
  8997. + __reset_stats();
  8998. + if (start_kthread())
  8999. + return -EFAULT;
  9000. + } else {
  9001. + if (!enabled)
  9002. + goto unlock;
  9003. + enabled = 0;
  9004. + err = stop_kthread();
  9005. + if (err) {
  9006. + pr_err(BANNER "cannot stop kthread\n");
  9007. + return -EFAULT;
  9008. + }
  9009. + wake_up(&data.wq); /* reader(s) should return */
  9010. + }
  9011. +unlock:
  9012. + return csize;
  9013. +}
  9014. +
  9015. +/**
  9016. + * debug_max_fopen - Open function for "max" debugfs entry
  9017. + * @inode: The in-kernel inode representation of the debugfs "file"
  9018. + * @filp: The active open file structure for the debugfs "file"
  9019. + *
  9020. + * This function provides an open implementation for the "max" debugfs
  9021. + * interface to the hardware latency detector.
  9022. + */
  9023. +static int debug_max_fopen(struct inode *inode, struct file *filp)
  9024. +{
  9025. + return 0;
  9026. +}
  9027. +
  9028. +/**
  9029. + * debug_max_fread - Read function for "max" debugfs entry
  9030. + * @filp: The active open file structure for the debugfs "file"
  9031. + * @ubuf: The userspace provided buffer to read value into
  9032. + * @cnt: The maximum number of bytes to read
  9033. + * @ppos: The current "file" position
  9034. + *
  9035. + * This function provides a read implementation for the "max" debugfs
  9036. + * interface to the hardware latency detector. Can be used to determine
  9037. + * the maximum latency value observed since it was last reset.
  9038. + */
  9039. +static ssize_t debug_max_fread(struct file *filp, char __user *ubuf,
  9040. + size_t cnt, loff_t *ppos)
  9041. +{
  9042. + return simple_data_read(filp, ubuf, cnt, ppos, &data.max_sample);
  9043. +}
  9044. +
  9045. +/**
  9046. + * debug_max_fwrite - Write function for "max" debugfs entry
  9047. + * @filp: The active open file structure for the debugfs "file"
  9048. + * @ubuf: The user buffer that contains the value to write
  9049. + * @cnt: The maximum number of bytes to write to "file"
  9050. + * @ppos: The current position in the debugfs "file"
  9051. + *
  9052. + * This function provides a write implementation for the "max" debugfs
  9053. + * interface to the hardware latency detector. Can be used to reset the
  9054. + * maximum or set it to some other desired value - if, then, subsequent
  9055. + * measurements exceed this value, the maximum will be updated.
  9056. + */
  9057. +static ssize_t debug_max_fwrite(struct file *filp,
  9058. + const char __user *ubuf,
  9059. + size_t cnt,
  9060. + loff_t *ppos)
  9061. +{
  9062. + return simple_data_write(filp, ubuf, cnt, ppos, &data.max_sample);
  9063. +}
  9064. +
  9065. +
  9066. +/**
  9067. + * debug_sample_fopen - An open function for "sample" debugfs interface
  9068. + * @inode: The in-kernel inode representation of this debugfs "file"
  9069. + * @filp: The active open file structure for the debugfs "file"
  9070. + *
  9071. + * This function handles opening the "sample" file within the hardware
  9072. + * latency detector debugfs directory interface. This file is used to read
  9073. + * raw samples from the global ring_buffer and allows the user to see a
  9074. + * running latency history. Can be opened blocking or non-blocking,
  9075. + * affecting whether it behaves as a buffer read pipe, or does not.
  9076. + * Implements simple locking to prevent multiple simultaneous use.
  9077. + */
  9078. +static int debug_sample_fopen(struct inode *inode, struct file *filp)
  9079. +{
  9080. + if (!atomic_add_unless(&data.sample_open, 1, 1))
  9081. + return -EBUSY;
  9082. + else
  9083. + return 0;
  9084. +}
  9085. +
  9086. +/**
  9087. + * debug_sample_fread - A read function for "sample" debugfs interface
  9088. + * @filp: The active open file structure for the debugfs "file"
  9089. + * @ubuf: The user buffer that will contain the samples read
  9090. + * @cnt: The maximum bytes to read from the debugfs "file"
  9091. + * @ppos: The current position in the debugfs "file"
  9092. + *
  9093. + * This function handles reading from the "sample" file within the hardware
  9094. + * latency detector debugfs directory interface. This file is used to read
  9095. + * raw samples from the global ring_buffer and allows the user to see a
  9096. + * running latency history. By default this will block pending a new
  9097. + * value written into the sample buffer, unless there are already a
  9098. + * number of value(s) waiting in the buffer, or the sample file was
  9099. + * previously opened in a non-blocking mode of operation.
  9100. + */
  9101. +static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
  9102. + size_t cnt, loff_t *ppos)
  9103. +{
  9104. + int len = 0;
  9105. + char buf[64];
  9106. + struct sample *sample = NULL;
  9107. +
  9108. + if (!enabled)
  9109. + return 0;
  9110. +
  9111. + sample = kzalloc(sizeof(struct sample), GFP_KERNEL);
  9112. + if (!sample)
  9113. + return -ENOMEM;
  9114. +
  9115. + while (!buffer_get_sample(sample)) {
  9116. +
  9117. + DEFINE_WAIT(wait);
  9118. +
  9119. + if (filp->f_flags & O_NONBLOCK) {
  9120. + len = -EAGAIN;
  9121. + goto out;
  9122. + }
  9123. +
  9124. + prepare_to_wait(&data.wq, &wait, TASK_INTERRUPTIBLE);
  9125. + schedule();
  9126. + finish_wait(&data.wq, &wait);
  9127. +
  9128. + if (signal_pending(current)) {
  9129. + len = -EINTR;
  9130. + goto out;
  9131. + }
  9132. +
  9133. + if (!enabled) { /* enable was toggled */
  9134. + len = 0;
  9135. + goto out;
  9136. + }
  9137. + }
  9138. +
  9139. + len = snprintf(buf, sizeof(buf), "%010lu.%010lu\t%llu\t%llu\n",
  9140. + sample->timestamp.tv_sec,
  9141. + sample->timestamp.tv_nsec,
  9142. + sample->duration,
  9143. + sample->outer_duration);
  9144. +
  9145. +
  9146. + /* handling partial reads is more trouble than it's worth */
  9147. + if (len > cnt)
  9148. + goto out;
  9149. +
  9150. + if (copy_to_user(ubuf, buf, len))
  9151. + len = -EFAULT;
  9152. +
  9153. +out:
  9154. + kfree(sample);
  9155. + return len;
  9156. +}
  9157. +
  9158. +/**
  9159. + * debug_sample_release - Release function for "sample" debugfs interface
  9160. + * @inode: The in-kernel inode represenation of the debugfs "file"
  9161. + * @filp: The active open file structure for the debugfs "file"
  9162. + *
  9163. + * This function completes the close of the debugfs interface "sample" file.
  9164. + * Frees the sample_open "lock" so that other users may open the interface.
  9165. + */
  9166. +static int debug_sample_release(struct inode *inode, struct file *filp)
  9167. +{
  9168. + atomic_dec(&data.sample_open);
  9169. +
  9170. + return 0;
  9171. +}
  9172. +
  9173. +/**
  9174. + * debug_threshold_fopen - Open function for "threshold" debugfs entry
  9175. + * @inode: The in-kernel inode representation of the debugfs "file"
  9176. + * @filp: The active open file structure for the debugfs "file"
  9177. + *
  9178. + * This function provides an open implementation for the "threshold" debugfs
  9179. + * interface to the hardware latency detector.
  9180. + */
  9181. +static int debug_threshold_fopen(struct inode *inode, struct file *filp)
  9182. +{
  9183. + return 0;
  9184. +}
  9185. +
  9186. +/**
  9187. + * debug_threshold_fread - Read function for "threshold" debugfs entry
  9188. + * @filp: The active open file structure for the debugfs "file"
  9189. + * @ubuf: The userspace provided buffer to read value into
  9190. + * @cnt: The maximum number of bytes to read
  9191. + * @ppos: The current "file" position
  9192. + *
  9193. + * This function provides a read implementation for the "threshold" debugfs
  9194. + * interface to the hardware latency detector. It can be used to determine
  9195. + * the current threshold level at which a latency will be recorded in the
  9196. + * global ring buffer, typically on the order of 10us.
  9197. + */
  9198. +static ssize_t debug_threshold_fread(struct file *filp, char __user *ubuf,
  9199. + size_t cnt, loff_t *ppos)
  9200. +{
  9201. + return simple_data_read(filp, ubuf, cnt, ppos, &data.threshold);
  9202. +}
  9203. +
  9204. +/**
  9205. + * debug_threshold_fwrite - Write function for "threshold" debugfs entry
  9206. + * @filp: The active open file structure for the debugfs "file"
  9207. + * @ubuf: The user buffer that contains the value to write
  9208. + * @cnt: The maximum number of bytes to write to "file"
  9209. + * @ppos: The current position in the debugfs "file"
  9210. + *
  9211. + * This function provides a write implementation for the "threshold" debugfs
  9212. + * interface to the hardware latency detector. It can be used to configure
  9213. + * the threshold level at which any subsequently detected latencies will
  9214. + * be recorded into the global ring buffer.
  9215. + */
  9216. +static ssize_t debug_threshold_fwrite(struct file *filp,
  9217. + const char __user *ubuf,
  9218. + size_t cnt,
  9219. + loff_t *ppos)
  9220. +{
  9221. + int ret;
  9222. +
  9223. + ret = simple_data_write(filp, ubuf, cnt, ppos, &data.threshold);
  9224. +
  9225. + if (enabled)
  9226. + wake_up_process(kthread);
  9227. +
  9228. + return ret;
  9229. +}
  9230. +
  9231. +/**
  9232. + * debug_width_fopen - Open function for "width" debugfs entry
  9233. + * @inode: The in-kernel inode representation of the debugfs "file"
  9234. + * @filp: The active open file structure for the debugfs "file"
  9235. + *
  9236. + * This function provides an open implementation for the "width" debugfs
  9237. + * interface to the hardware latency detector.
  9238. + */
  9239. +static int debug_width_fopen(struct inode *inode, struct file *filp)
  9240. +{
  9241. + return 0;
  9242. +}
  9243. +
  9244. +/**
  9245. + * debug_width_fread - Read function for "width" debugfs entry
  9246. + * @filp: The active open file structure for the debugfs "file"
  9247. + * @ubuf: The userspace provided buffer to read value into
  9248. + * @cnt: The maximum number of bytes to read
  9249. + * @ppos: The current "file" position
  9250. + *
  9251. + * This function provides a read implementation for the "width" debugfs
  9252. + * interface to the hardware latency detector. It can be used to determine
  9253. + * for how many us of the total window us we will actively sample for any
  9254. + * hardware-induced latecy periods. Obviously, it is not possible to
  9255. + * sample constantly and have the system respond to a sample reader, or,
  9256. + * worse, without having the system appear to have gone out to lunch.
  9257. + */
  9258. +static ssize_t debug_width_fread(struct file *filp, char __user *ubuf,
  9259. + size_t cnt, loff_t *ppos)
  9260. +{
  9261. + return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_width);
  9262. +}
  9263. +
  9264. +/**
  9265. + * debug_width_fwrite - Write function for "width" debugfs entry
  9266. + * @filp: The active open file structure for the debugfs "file"
  9267. + * @ubuf: The user buffer that contains the value to write
  9268. + * @cnt: The maximum number of bytes to write to "file"
  9269. + * @ppos: The current position in the debugfs "file"
  9270. + *
  9271. + * This function provides a write implementation for the "width" debugfs
  9272. + * interface to the hardware latency detector. It can be used to configure
  9273. + * for how many us of the total window us we will actively sample for any
  9274. + * hardware-induced latency periods. Obviously, it is not possible to
  9275. + * sample constantly and have the system respond to a sample reader, or,
  9276. + * worse, without having the system appear to have gone out to lunch. It
  9277. + * is enforced that width is less that the total window size.
  9278. + */
  9279. +static ssize_t debug_width_fwrite(struct file *filp,
  9280. + const char __user *ubuf,
  9281. + size_t cnt,
  9282. + loff_t *ppos)
  9283. +{
  9284. + char buf[U64STR_SIZE];
  9285. + int csize = min(cnt, sizeof(buf));
  9286. + u64 val = 0;
  9287. + int err = 0;
  9288. +
  9289. + memset(buf, '\0', sizeof(buf));
  9290. + if (copy_from_user(buf, ubuf, csize))
  9291. + return -EFAULT;
  9292. +
  9293. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  9294. + err = kstrtoull(buf, 10, &val);
  9295. + if (err)
  9296. + return -EINVAL;
  9297. +
  9298. + mutex_lock(&data.lock);
  9299. + if (val < data.sample_window)
  9300. + data.sample_width = val;
  9301. + else {
  9302. + mutex_unlock(&data.lock);
  9303. + return -EINVAL;
  9304. + }
  9305. + mutex_unlock(&data.lock);
  9306. +
  9307. + if (enabled)
  9308. + wake_up_process(kthread);
  9309. +
  9310. + return csize;
  9311. +}
  9312. +
  9313. +/**
  9314. + * debug_window_fopen - Open function for "window" debugfs entry
  9315. + * @inode: The in-kernel inode representation of the debugfs "file"
  9316. + * @filp: The active open file structure for the debugfs "file"
  9317. + *
  9318. + * This function provides an open implementation for the "window" debugfs
  9319. + * interface to the hardware latency detector. The window is the total time
  9320. + * in us that will be considered one sample period. Conceptually, windows
  9321. + * occur back-to-back and contain a sample width period during which
  9322. + * actual sampling occurs.
  9323. + */
  9324. +static int debug_window_fopen(struct inode *inode, struct file *filp)
  9325. +{
  9326. + return 0;
  9327. +}
  9328. +
  9329. +/**
  9330. + * debug_window_fread - Read function for "window" debugfs entry
  9331. + * @filp: The active open file structure for the debugfs "file"
  9332. + * @ubuf: The userspace provided buffer to read value into
  9333. + * @cnt: The maximum number of bytes to read
  9334. + * @ppos: The current "file" position
  9335. + *
  9336. + * This function provides a read implementation for the "window" debugfs
  9337. + * interface to the hardware latency detector. The window is the total time
  9338. + * in us that will be considered one sample period. Conceptually, windows
  9339. + * occur back-to-back and contain a sample width period during which
  9340. + * actual sampling occurs. Can be used to read the total window size.
  9341. + */
  9342. +static ssize_t debug_window_fread(struct file *filp, char __user *ubuf,
  9343. + size_t cnt, loff_t *ppos)
  9344. +{
  9345. + return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_window);
  9346. +}
  9347. +
  9348. +/**
  9349. + * debug_window_fwrite - Write function for "window" debugfs entry
  9350. + * @filp: The active open file structure for the debugfs "file"
  9351. + * @ubuf: The user buffer that contains the value to write
  9352. + * @cnt: The maximum number of bytes to write to "file"
  9353. + * @ppos: The current position in the debugfs "file"
  9354. + *
  9355. + * This function provides a write implementation for the "window" debufds
  9356. + * interface to the hardware latency detetector. The window is the total time
  9357. + * in us that will be considered one sample period. Conceptually, windows
  9358. + * occur back-to-back and contain a sample width period during which
  9359. + * actual sampling occurs. Can be used to write a new total window size. It
  9360. + * is enfoced that any value written must be greater than the sample width
  9361. + * size, or an error results.
  9362. + */
  9363. +static ssize_t debug_window_fwrite(struct file *filp,
  9364. + const char __user *ubuf,
  9365. + size_t cnt,
  9366. + loff_t *ppos)
  9367. +{
  9368. + char buf[U64STR_SIZE];
  9369. + int csize = min(cnt, sizeof(buf));
  9370. + u64 val = 0;
  9371. + int err = 0;
  9372. +
  9373. + memset(buf, '\0', sizeof(buf));
  9374. + if (copy_from_user(buf, ubuf, csize))
  9375. + return -EFAULT;
  9376. +
  9377. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  9378. + err = kstrtoull(buf, 10, &val);
  9379. + if (err)
  9380. + return -EINVAL;
  9381. +
  9382. + mutex_lock(&data.lock);
  9383. + if (data.sample_width < val)
  9384. + data.sample_window = val;
  9385. + else {
  9386. + mutex_unlock(&data.lock);
  9387. + return -EINVAL;
  9388. + }
  9389. + mutex_unlock(&data.lock);
  9390. +
  9391. + return csize;
  9392. +}
  9393. +
  9394. +/*
  9395. + * Function pointers for the "count" debugfs file operations
  9396. + */
  9397. +static const struct file_operations count_fops = {
  9398. + .open = debug_count_fopen,
  9399. + .read = debug_count_fread,
  9400. + .write = debug_count_fwrite,
  9401. + .owner = THIS_MODULE,
  9402. +};
  9403. +
  9404. +/*
  9405. + * Function pointers for the "enable" debugfs file operations
  9406. + */
  9407. +static const struct file_operations enable_fops = {
  9408. + .open = debug_enable_fopen,
  9409. + .read = debug_enable_fread,
  9410. + .write = debug_enable_fwrite,
  9411. + .owner = THIS_MODULE,
  9412. +};
  9413. +
  9414. +/*
  9415. + * Function pointers for the "max" debugfs file operations
  9416. + */
  9417. +static const struct file_operations max_fops = {
  9418. + .open = debug_max_fopen,
  9419. + .read = debug_max_fread,
  9420. + .write = debug_max_fwrite,
  9421. + .owner = THIS_MODULE,
  9422. +};
  9423. +
  9424. +/*
  9425. + * Function pointers for the "sample" debugfs file operations
  9426. + */
  9427. +static const struct file_operations sample_fops = {
  9428. + .open = debug_sample_fopen,
  9429. + .read = debug_sample_fread,
  9430. + .release = debug_sample_release,
  9431. + .owner = THIS_MODULE,
  9432. +};
  9433. +
  9434. +/*
  9435. + * Function pointers for the "threshold" debugfs file operations
  9436. + */
  9437. +static const struct file_operations threshold_fops = {
  9438. + .open = debug_threshold_fopen,
  9439. + .read = debug_threshold_fread,
  9440. + .write = debug_threshold_fwrite,
  9441. + .owner = THIS_MODULE,
  9442. +};
  9443. +
  9444. +/*
  9445. + * Function pointers for the "width" debugfs file operations
  9446. + */
  9447. +static const struct file_operations width_fops = {
  9448. + .open = debug_width_fopen,
  9449. + .read = debug_width_fread,
  9450. + .write = debug_width_fwrite,
  9451. + .owner = THIS_MODULE,
  9452. +};
  9453. +
  9454. +/*
  9455. + * Function pointers for the "window" debugfs file operations
  9456. + */
  9457. +static const struct file_operations window_fops = {
  9458. + .open = debug_window_fopen,
  9459. + .read = debug_window_fread,
  9460. + .write = debug_window_fwrite,
  9461. + .owner = THIS_MODULE,
  9462. +};
  9463. +
  9464. +/**
  9465. + * init_debugfs - A function to initialize the debugfs interface files
  9466. + *
  9467. + * This function creates entries in debugfs for "hwlat_detector", including
  9468. + * files to read values from the detector, current samples, and the
  9469. + * maximum sample that has been captured since the hardware latency
  9470. + * dectector was started.
  9471. + */
  9472. +static int init_debugfs(void)
  9473. +{
  9474. + int ret = -ENOMEM;
  9475. +
  9476. + debug_dir = debugfs_create_dir(DRVNAME, NULL);
  9477. + if (!debug_dir)
  9478. + goto err_debug_dir;
  9479. +
  9480. + debug_sample = debugfs_create_file("sample", 0444,
  9481. + debug_dir, NULL,
  9482. + &sample_fops);
  9483. + if (!debug_sample)
  9484. + goto err_sample;
  9485. +
  9486. + debug_count = debugfs_create_file("count", 0444,
  9487. + debug_dir, NULL,
  9488. + &count_fops);
  9489. + if (!debug_count)
  9490. + goto err_count;
  9491. +
  9492. + debug_max = debugfs_create_file("max", 0444,
  9493. + debug_dir, NULL,
  9494. + &max_fops);
  9495. + if (!debug_max)
  9496. + goto err_max;
  9497. +
  9498. + debug_sample_window = debugfs_create_file("window", 0644,
  9499. + debug_dir, NULL,
  9500. + &window_fops);
  9501. + if (!debug_sample_window)
  9502. + goto err_window;
  9503. +
  9504. + debug_sample_width = debugfs_create_file("width", 0644,
  9505. + debug_dir, NULL,
  9506. + &width_fops);
  9507. + if (!debug_sample_width)
  9508. + goto err_width;
  9509. +
  9510. + debug_threshold = debugfs_create_file("threshold", 0644,
  9511. + debug_dir, NULL,
  9512. + &threshold_fops);
  9513. + if (!debug_threshold)
  9514. + goto err_threshold;
  9515. +
  9516. + debug_enable = debugfs_create_file("enable", 0644,
  9517. + debug_dir, &enabled,
  9518. + &enable_fops);
  9519. + if (!debug_enable)
  9520. + goto err_enable;
  9521. +
  9522. + else {
  9523. + ret = 0;
  9524. + goto out;
  9525. + }
  9526. +
  9527. +err_enable:
  9528. + debugfs_remove(debug_threshold);
  9529. +err_threshold:
  9530. + debugfs_remove(debug_sample_width);
  9531. +err_width:
  9532. + debugfs_remove(debug_sample_window);
  9533. +err_window:
  9534. + debugfs_remove(debug_max);
  9535. +err_max:
  9536. + debugfs_remove(debug_count);
  9537. +err_count:
  9538. + debugfs_remove(debug_sample);
  9539. +err_sample:
  9540. + debugfs_remove(debug_dir);
  9541. +err_debug_dir:
  9542. +out:
  9543. + return ret;
  9544. +}
  9545. +
  9546. +/**
  9547. + * free_debugfs - A function to cleanup the debugfs file interface
  9548. + */
  9549. +static void free_debugfs(void)
  9550. +{
  9551. + /* could also use a debugfs_remove_recursive */
  9552. + debugfs_remove(debug_enable);
  9553. + debugfs_remove(debug_threshold);
  9554. + debugfs_remove(debug_sample_width);
  9555. + debugfs_remove(debug_sample_window);
  9556. + debugfs_remove(debug_max);
  9557. + debugfs_remove(debug_count);
  9558. + debugfs_remove(debug_sample);
  9559. + debugfs_remove(debug_dir);
  9560. +}
  9561. +
  9562. +/**
  9563. + * detector_init - Standard module initialization code
  9564. + */
  9565. +static int detector_init(void)
  9566. +{
  9567. + int ret = -ENOMEM;
  9568. +
  9569. + pr_info(BANNER "version %s\n", VERSION);
  9570. +
  9571. + ret = init_stats();
  9572. + if (ret)
  9573. + goto out;
  9574. +
  9575. + ret = init_debugfs();
  9576. + if (ret)
  9577. + goto err_stats;
  9578. +
  9579. + if (enabled)
  9580. + ret = start_kthread();
  9581. +
  9582. + goto out;
  9583. +
  9584. +err_stats:
  9585. + ring_buffer_free(ring_buffer);
  9586. +out:
  9587. + return ret;
  9588. +
  9589. +}
  9590. +
  9591. +/**
  9592. + * detector_exit - Standard module cleanup code
  9593. + */
  9594. +static void detector_exit(void)
  9595. +{
  9596. + int err;
  9597. +
  9598. + if (enabled) {
  9599. + enabled = 0;
  9600. + err = stop_kthread();
  9601. + if (err)
  9602. + pr_err(BANNER "cannot stop kthread\n");
  9603. + }
  9604. +
  9605. + free_debugfs();
  9606. + ring_buffer_free(ring_buffer); /* free up the ring buffer */
  9607. +
  9608. +}
  9609. +
  9610. +module_init(detector_init);
  9611. +module_exit(detector_exit);
  9612. diff --git a/drivers/mmc/host/mmci.c b/drivers/mmc/host/mmci.c
  9613. index acece3299756..58ea04a03fa9 100644
  9614. --- a/drivers/mmc/host/mmci.c
  9615. +++ b/drivers/mmc/host/mmci.c
  9616. @@ -1155,15 +1155,12 @@ static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
  9617. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  9618. struct variant_data *variant = host->variant;
  9619. void __iomem *base = host->base;
  9620. - unsigned long flags;
  9621. u32 status;
  9622. status = readl(base + MMCISTATUS);
  9623. dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
  9624. - local_irq_save(flags);
  9625. -
  9626. do {
  9627. unsigned int remain, len;
  9628. char *buffer;
  9629. @@ -1203,8 +1200,6 @@ static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
  9630. sg_miter_stop(sg_miter);
  9631. - local_irq_restore(flags);
  9632. -
  9633. /*
  9634. * If we have less than the fifo 'half-full' threshold to transfer,
  9635. * trigger a PIO interrupt as soon as any data is available.
  9636. diff --git a/drivers/net/ethernet/3com/3c59x.c b/drivers/net/ethernet/3com/3c59x.c
  9637. index 2839af00f20c..4348b9c850d3 100644
  9638. --- a/drivers/net/ethernet/3com/3c59x.c
  9639. +++ b/drivers/net/ethernet/3com/3c59x.c
  9640. @@ -842,9 +842,9 @@ static void poll_vortex(struct net_device *dev)
  9641. {
  9642. struct vortex_private *vp = netdev_priv(dev);
  9643. unsigned long flags;
  9644. - local_irq_save(flags);
  9645. + local_irq_save_nort(flags);
  9646. (vp->full_bus_master_rx ? boomerang_interrupt:vortex_interrupt)(dev->irq,dev);
  9647. - local_irq_restore(flags);
  9648. + local_irq_restore_nort(flags);
  9649. }
  9650. #endif
  9651. @@ -1916,12 +1916,12 @@ static void vortex_tx_timeout(struct net_device *dev)
  9652. * Block interrupts because vortex_interrupt does a bare spin_lock()
  9653. */
  9654. unsigned long flags;
  9655. - local_irq_save(flags);
  9656. + local_irq_save_nort(flags);
  9657. if (vp->full_bus_master_tx)
  9658. boomerang_interrupt(dev->irq, dev);
  9659. else
  9660. vortex_interrupt(dev->irq, dev);
  9661. - local_irq_restore(flags);
  9662. + local_irq_restore_nort(flags);
  9663. }
  9664. }
  9665. diff --git a/drivers/net/ethernet/atheros/atl1c/atl1c_main.c b/drivers/net/ethernet/atheros/atl1c/atl1c_main.c
  9666. index 8b5988e210d5..cf9928ccdd7e 100644
  9667. --- a/drivers/net/ethernet/atheros/atl1c/atl1c_main.c
  9668. +++ b/drivers/net/ethernet/atheros/atl1c/atl1c_main.c
  9669. @@ -2221,11 +2221,7 @@ static netdev_tx_t atl1c_xmit_frame(struct sk_buff *skb,
  9670. }
  9671. tpd_req = atl1c_cal_tpd_req(skb);
  9672. - if (!spin_trylock_irqsave(&adapter->tx_lock, flags)) {
  9673. - if (netif_msg_pktdata(adapter))
  9674. - dev_info(&adapter->pdev->dev, "tx locked\n");
  9675. - return NETDEV_TX_LOCKED;
  9676. - }
  9677. + spin_lock_irqsave(&adapter->tx_lock, flags);
  9678. if (atl1c_tpd_avail(adapter, type) < tpd_req) {
  9679. /* no enough descriptor, just stop queue */
  9680. diff --git a/drivers/net/ethernet/atheros/atl1e/atl1e_main.c b/drivers/net/ethernet/atheros/atl1e/atl1e_main.c
  9681. index 59a03a193e83..734f7a7ad2c3 100644
  9682. --- a/drivers/net/ethernet/atheros/atl1e/atl1e_main.c
  9683. +++ b/drivers/net/ethernet/atheros/atl1e/atl1e_main.c
  9684. @@ -1880,8 +1880,7 @@ static netdev_tx_t atl1e_xmit_frame(struct sk_buff *skb,
  9685. return NETDEV_TX_OK;
  9686. }
  9687. tpd_req = atl1e_cal_tdp_req(skb);
  9688. - if (!spin_trylock_irqsave(&adapter->tx_lock, flags))
  9689. - return NETDEV_TX_LOCKED;
  9690. + spin_lock_irqsave(&adapter->tx_lock, flags);
  9691. if (atl1e_tpd_avail(adapter) < tpd_req) {
  9692. /* no enough descriptor, just stop queue */
  9693. diff --git a/drivers/net/ethernet/chelsio/cxgb/sge.c b/drivers/net/ethernet/chelsio/cxgb/sge.c
  9694. index 526ea74e82d9..86f467a2c485 100644
  9695. --- a/drivers/net/ethernet/chelsio/cxgb/sge.c
  9696. +++ b/drivers/net/ethernet/chelsio/cxgb/sge.c
  9697. @@ -1664,8 +1664,7 @@ static int t1_sge_tx(struct sk_buff *skb, struct adapter *adapter,
  9698. struct cmdQ *q = &sge->cmdQ[qid];
  9699. unsigned int credits, pidx, genbit, count, use_sched_skb = 0;
  9700. - if (!spin_trylock(&q->lock))
  9701. - return NETDEV_TX_LOCKED;
  9702. + spin_lock(&q->lock);
  9703. reclaim_completed_tx(sge, q);
  9704. diff --git a/drivers/net/ethernet/neterion/s2io.c b/drivers/net/ethernet/neterion/s2io.c
  9705. index 9ba975853ec6..813cfa698160 100644
  9706. --- a/drivers/net/ethernet/neterion/s2io.c
  9707. +++ b/drivers/net/ethernet/neterion/s2io.c
  9708. @@ -4084,12 +4084,7 @@ static netdev_tx_t s2io_xmit(struct sk_buff *skb, struct net_device *dev)
  9709. [skb->priority & (MAX_TX_FIFOS - 1)];
  9710. fifo = &mac_control->fifos[queue];
  9711. - if (do_spin_lock)
  9712. - spin_lock_irqsave(&fifo->tx_lock, flags);
  9713. - else {
  9714. - if (unlikely(!spin_trylock_irqsave(&fifo->tx_lock, flags)))
  9715. - return NETDEV_TX_LOCKED;
  9716. - }
  9717. + spin_lock_irqsave(&fifo->tx_lock, flags);
  9718. if (sp->config.multiq) {
  9719. if (__netif_subqueue_stopped(dev, fifo->fifo_no)) {
  9720. diff --git a/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c b/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c
  9721. index 3b98b263bad0..ca4add749410 100644
  9722. --- a/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c
  9723. +++ b/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c
  9724. @@ -2137,10 +2137,8 @@ static int pch_gbe_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  9725. struct pch_gbe_tx_ring *tx_ring = adapter->tx_ring;
  9726. unsigned long flags;
  9727. - if (!spin_trylock_irqsave(&tx_ring->tx_lock, flags)) {
  9728. - /* Collision - tell upper layer to requeue */
  9729. - return NETDEV_TX_LOCKED;
  9730. - }
  9731. + spin_lock_irqsave(&tx_ring->tx_lock, flags);
  9732. +
  9733. if (unlikely(!PCH_GBE_DESC_UNUSED(tx_ring))) {
  9734. netif_stop_queue(netdev);
  9735. spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
  9736. diff --git a/drivers/net/ethernet/realtek/8139too.c b/drivers/net/ethernet/realtek/8139too.c
  9737. index ef668d300800..d987d571fdd6 100644
  9738. --- a/drivers/net/ethernet/realtek/8139too.c
  9739. +++ b/drivers/net/ethernet/realtek/8139too.c
  9740. @@ -2229,7 +2229,7 @@ static void rtl8139_poll_controller(struct net_device *dev)
  9741. struct rtl8139_private *tp = netdev_priv(dev);
  9742. const int irq = tp->pci_dev->irq;
  9743. - disable_irq(irq);
  9744. + disable_irq_nosync(irq);
  9745. rtl8139_interrupt(irq, dev);
  9746. enable_irq(irq);
  9747. }
  9748. diff --git a/drivers/net/ethernet/tehuti/tehuti.c b/drivers/net/ethernet/tehuti/tehuti.c
  9749. index 14c9d1baa85c..e1a5305418a8 100644
  9750. --- a/drivers/net/ethernet/tehuti/tehuti.c
  9751. +++ b/drivers/net/ethernet/tehuti/tehuti.c
  9752. @@ -1629,13 +1629,8 @@ static netdev_tx_t bdx_tx_transmit(struct sk_buff *skb,
  9753. unsigned long flags;
  9754. ENTER;
  9755. - local_irq_save(flags);
  9756. - if (!spin_trylock(&priv->tx_lock)) {
  9757. - local_irq_restore(flags);
  9758. - DBG("%s[%s]: TX locked, returning NETDEV_TX_LOCKED\n",
  9759. - BDX_DRV_NAME, ndev->name);
  9760. - return NETDEV_TX_LOCKED;
  9761. - }
  9762. +
  9763. + spin_lock_irqsave(&priv->tx_lock, flags);
  9764. /* build tx descriptor */
  9765. BDX_ASSERT(f->m.wptr >= f->m.memsz); /* started with valid wptr */
  9766. diff --git a/drivers/net/rionet.c b/drivers/net/rionet.c
  9767. index e7034c55e796..2e4ee0f912bf 100644
  9768. --- a/drivers/net/rionet.c
  9769. +++ b/drivers/net/rionet.c
  9770. @@ -174,11 +174,7 @@ static int rionet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  9771. unsigned long flags;
  9772. int add_num = 1;
  9773. - local_irq_save(flags);
  9774. - if (!spin_trylock(&rnet->tx_lock)) {
  9775. - local_irq_restore(flags);
  9776. - return NETDEV_TX_LOCKED;
  9777. - }
  9778. + spin_lock_irqsave(&rnet->tx_lock, flags);
  9779. if (is_multicast_ether_addr(eth->h_dest))
  9780. add_num = nets[rnet->mport->id].nact;
  9781. diff --git a/drivers/net/wireless/orinoco/orinoco_usb.c b/drivers/net/wireless/orinoco/orinoco_usb.c
  9782. index f2cd513d54b2..6c0f4c9638a2 100644
  9783. --- a/drivers/net/wireless/orinoco/orinoco_usb.c
  9784. +++ b/drivers/net/wireless/orinoco/orinoco_usb.c
  9785. @@ -697,7 +697,7 @@ static void ezusb_req_ctx_wait(struct ezusb_priv *upriv,
  9786. while (!ctx->done.done && msecs--)
  9787. udelay(1000);
  9788. } else {
  9789. - wait_event_interruptible(ctx->done.wait,
  9790. + swait_event_interruptible(ctx->done.wait,
  9791. ctx->done.done);
  9792. }
  9793. break;
  9794. diff --git a/drivers/pci/access.c b/drivers/pci/access.c
  9795. index 59ac36fe7c42..7a45a20af78a 100644
  9796. --- a/drivers/pci/access.c
  9797. +++ b/drivers/pci/access.c
  9798. @@ -561,7 +561,7 @@ void pci_cfg_access_unlock(struct pci_dev *dev)
  9799. WARN_ON(!dev->block_cfg_access);
  9800. dev->block_cfg_access = 0;
  9801. - wake_up_all(&pci_cfg_wait);
  9802. + wake_up_all_locked(&pci_cfg_wait);
  9803. raw_spin_unlock_irqrestore(&pci_lock, flags);
  9804. }
  9805. EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
  9806. diff --git a/drivers/pinctrl/qcom/pinctrl-msm.c b/drivers/pinctrl/qcom/pinctrl-msm.c
  9807. index 9736f9be5447..5fe9b173dcb3 100644
  9808. --- a/drivers/pinctrl/qcom/pinctrl-msm.c
  9809. +++ b/drivers/pinctrl/qcom/pinctrl-msm.c
  9810. @@ -60,7 +60,7 @@ struct msm_pinctrl {
  9811. struct notifier_block restart_nb;
  9812. int irq;
  9813. - spinlock_t lock;
  9814. + raw_spinlock_t lock;
  9815. DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
  9816. DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
  9817. @@ -156,14 +156,14 @@ static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
  9818. if (WARN_ON(i == g->nfuncs))
  9819. return -EINVAL;
  9820. - spin_lock_irqsave(&pctrl->lock, flags);
  9821. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9822. val = readl(pctrl->regs + g->ctl_reg);
  9823. val &= ~(0x7 << g->mux_bit);
  9824. val |= i << g->mux_bit;
  9825. writel(val, pctrl->regs + g->ctl_reg);
  9826. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9827. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9828. return 0;
  9829. }
  9830. @@ -326,14 +326,14 @@ static int msm_config_group_set(struct pinctrl_dev *pctldev,
  9831. break;
  9832. case PIN_CONFIG_OUTPUT:
  9833. /* set output value */
  9834. - spin_lock_irqsave(&pctrl->lock, flags);
  9835. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9836. val = readl(pctrl->regs + g->io_reg);
  9837. if (arg)
  9838. val |= BIT(g->out_bit);
  9839. else
  9840. val &= ~BIT(g->out_bit);
  9841. writel(val, pctrl->regs + g->io_reg);
  9842. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9843. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9844. /* enable output */
  9845. arg = 1;
  9846. @@ -354,12 +354,12 @@ static int msm_config_group_set(struct pinctrl_dev *pctldev,
  9847. return -EINVAL;
  9848. }
  9849. - spin_lock_irqsave(&pctrl->lock, flags);
  9850. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9851. val = readl(pctrl->regs + g->ctl_reg);
  9852. val &= ~(mask << bit);
  9853. val |= arg << bit;
  9854. writel(val, pctrl->regs + g->ctl_reg);
  9855. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9856. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9857. }
  9858. return 0;
  9859. @@ -387,13 +387,13 @@ static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  9860. g = &pctrl->soc->groups[offset];
  9861. - spin_lock_irqsave(&pctrl->lock, flags);
  9862. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9863. val = readl(pctrl->regs + g->ctl_reg);
  9864. val &= ~BIT(g->oe_bit);
  9865. writel(val, pctrl->regs + g->ctl_reg);
  9866. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9867. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9868. return 0;
  9869. }
  9870. @@ -407,7 +407,7 @@ static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, in
  9871. g = &pctrl->soc->groups[offset];
  9872. - spin_lock_irqsave(&pctrl->lock, flags);
  9873. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9874. val = readl(pctrl->regs + g->io_reg);
  9875. if (value)
  9876. @@ -420,7 +420,7 @@ static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, in
  9877. val |= BIT(g->oe_bit);
  9878. writel(val, pctrl->regs + g->ctl_reg);
  9879. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9880. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9881. return 0;
  9882. }
  9883. @@ -446,7 +446,7 @@ static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  9884. g = &pctrl->soc->groups[offset];
  9885. - spin_lock_irqsave(&pctrl->lock, flags);
  9886. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9887. val = readl(pctrl->regs + g->io_reg);
  9888. if (value)
  9889. @@ -455,7 +455,7 @@ static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  9890. val &= ~BIT(g->out_bit);
  9891. writel(val, pctrl->regs + g->io_reg);
  9892. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9893. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9894. }
  9895. #ifdef CONFIG_DEBUG_FS
  9896. @@ -574,7 +574,7 @@ static void msm_gpio_irq_mask(struct irq_data *d)
  9897. g = &pctrl->soc->groups[d->hwirq];
  9898. - spin_lock_irqsave(&pctrl->lock, flags);
  9899. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9900. val = readl(pctrl->regs + g->intr_cfg_reg);
  9901. val &= ~BIT(g->intr_enable_bit);
  9902. @@ -582,7 +582,7 @@ static void msm_gpio_irq_mask(struct irq_data *d)
  9903. clear_bit(d->hwirq, pctrl->enabled_irqs);
  9904. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9905. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9906. }
  9907. static void msm_gpio_irq_unmask(struct irq_data *d)
  9908. @@ -595,7 +595,7 @@ static void msm_gpio_irq_unmask(struct irq_data *d)
  9909. g = &pctrl->soc->groups[d->hwirq];
  9910. - spin_lock_irqsave(&pctrl->lock, flags);
  9911. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9912. val = readl(pctrl->regs + g->intr_cfg_reg);
  9913. val |= BIT(g->intr_enable_bit);
  9914. @@ -603,7 +603,7 @@ static void msm_gpio_irq_unmask(struct irq_data *d)
  9915. set_bit(d->hwirq, pctrl->enabled_irqs);
  9916. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9917. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9918. }
  9919. static void msm_gpio_irq_ack(struct irq_data *d)
  9920. @@ -616,7 +616,7 @@ static void msm_gpio_irq_ack(struct irq_data *d)
  9921. g = &pctrl->soc->groups[d->hwirq];
  9922. - spin_lock_irqsave(&pctrl->lock, flags);
  9923. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9924. val = readl(pctrl->regs + g->intr_status_reg);
  9925. if (g->intr_ack_high)
  9926. @@ -628,7 +628,7 @@ static void msm_gpio_irq_ack(struct irq_data *d)
  9927. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  9928. msm_gpio_update_dual_edge_pos(pctrl, g, d);
  9929. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9930. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9931. }
  9932. static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  9933. @@ -641,7 +641,7 @@ static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  9934. g = &pctrl->soc->groups[d->hwirq];
  9935. - spin_lock_irqsave(&pctrl->lock, flags);
  9936. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9937. /*
  9938. * For hw without possibility of detecting both edges
  9939. @@ -715,7 +715,7 @@ static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  9940. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  9941. msm_gpio_update_dual_edge_pos(pctrl, g, d);
  9942. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9943. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9944. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  9945. irq_set_handler_locked(d, handle_level_irq);
  9946. @@ -731,11 +731,11 @@ static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  9947. struct msm_pinctrl *pctrl = to_msm_pinctrl(gc);
  9948. unsigned long flags;
  9949. - spin_lock_irqsave(&pctrl->lock, flags);
  9950. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9951. irq_set_irq_wake(pctrl->irq, on);
  9952. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9953. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9954. return 0;
  9955. }
  9956. @@ -881,7 +881,7 @@ int msm_pinctrl_probe(struct platform_device *pdev,
  9957. pctrl->soc = soc_data;
  9958. pctrl->chip = msm_gpio_template;
  9959. - spin_lock_init(&pctrl->lock);
  9960. + raw_spin_lock_init(&pctrl->lock);
  9961. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  9962. pctrl->regs = devm_ioremap_resource(&pdev->dev, res);
  9963. diff --git a/drivers/scsi/fcoe/fcoe.c b/drivers/scsi/fcoe/fcoe.c
  9964. index f4424063b860..cbbbebd86c6e 100644
  9965. --- a/drivers/scsi/fcoe/fcoe.c
  9966. +++ b/drivers/scsi/fcoe/fcoe.c
  9967. @@ -1286,7 +1286,7 @@ static void fcoe_percpu_thread_destroy(unsigned int cpu)
  9968. struct sk_buff *skb;
  9969. #ifdef CONFIG_SMP
  9970. struct fcoe_percpu_s *p0;
  9971. - unsigned targ_cpu = get_cpu();
  9972. + unsigned targ_cpu = get_cpu_light();
  9973. #endif /* CONFIG_SMP */
  9974. FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
  9975. @@ -1342,7 +1342,7 @@ static void fcoe_percpu_thread_destroy(unsigned int cpu)
  9976. kfree_skb(skb);
  9977. spin_unlock_bh(&p->fcoe_rx_list.lock);
  9978. }
  9979. - put_cpu();
  9980. + put_cpu_light();
  9981. #else
  9982. /*
  9983. * This a non-SMP scenario where the singular Rx thread is
  9984. @@ -1566,11 +1566,11 @@ static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
  9985. static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
  9986. {
  9987. struct fcoe_percpu_s *fps;
  9988. - int rc;
  9989. + int rc, cpu = get_cpu_light();
  9990. - fps = &get_cpu_var(fcoe_percpu);
  9991. + fps = &per_cpu(fcoe_percpu, cpu);
  9992. rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
  9993. - put_cpu_var(fcoe_percpu);
  9994. + put_cpu_light();
  9995. return rc;
  9996. }
  9997. @@ -1766,11 +1766,11 @@ static inline int fcoe_filter_frames(struct fc_lport *lport,
  9998. return 0;
  9999. }
  10000. - stats = per_cpu_ptr(lport->stats, get_cpu());
  10001. + stats = per_cpu_ptr(lport->stats, get_cpu_light());
  10002. stats->InvalidCRCCount++;
  10003. if (stats->InvalidCRCCount < 5)
  10004. printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
  10005. - put_cpu();
  10006. + put_cpu_light();
  10007. return -EINVAL;
  10008. }
  10009. @@ -1814,7 +1814,7 @@ static void fcoe_recv_frame(struct sk_buff *skb)
  10010. */
  10011. hp = (struct fcoe_hdr *) skb_network_header(skb);
  10012. - stats = per_cpu_ptr(lport->stats, get_cpu());
  10013. + stats = per_cpu_ptr(lport->stats, get_cpu_light());
  10014. if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
  10015. if (stats->ErrorFrames < 5)
  10016. printk(KERN_WARNING "fcoe: FCoE version "
  10017. @@ -1846,13 +1846,13 @@ static void fcoe_recv_frame(struct sk_buff *skb)
  10018. goto drop;
  10019. if (!fcoe_filter_frames(lport, fp)) {
  10020. - put_cpu();
  10021. + put_cpu_light();
  10022. fc_exch_recv(lport, fp);
  10023. return;
  10024. }
  10025. drop:
  10026. stats->ErrorFrames++;
  10027. - put_cpu();
  10028. + put_cpu_light();
  10029. kfree_skb(skb);
  10030. }
  10031. diff --git a/drivers/scsi/fcoe/fcoe_ctlr.c b/drivers/scsi/fcoe/fcoe_ctlr.c
  10032. index 34a1b1f333b4..d91131210695 100644
  10033. --- a/drivers/scsi/fcoe/fcoe_ctlr.c
  10034. +++ b/drivers/scsi/fcoe/fcoe_ctlr.c
  10035. @@ -831,7 +831,7 @@ static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
  10036. INIT_LIST_HEAD(&del_list);
  10037. - stats = per_cpu_ptr(fip->lp->stats, get_cpu());
  10038. + stats = per_cpu_ptr(fip->lp->stats, get_cpu_light());
  10039. list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
  10040. deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
  10041. @@ -867,7 +867,7 @@ static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
  10042. sel_time = fcf->time;
  10043. }
  10044. }
  10045. - put_cpu();
  10046. + put_cpu_light();
  10047. list_for_each_entry_safe(fcf, next, &del_list, list) {
  10048. /* Removes fcf from current list */
  10049. diff --git a/drivers/scsi/libfc/fc_exch.c b/drivers/scsi/libfc/fc_exch.c
  10050. index 30f9ef0c0d4f..6c686bc01a82 100644
  10051. --- a/drivers/scsi/libfc/fc_exch.c
  10052. +++ b/drivers/scsi/libfc/fc_exch.c
  10053. @@ -814,10 +814,10 @@ static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
  10054. }
  10055. memset(ep, 0, sizeof(*ep));
  10056. - cpu = get_cpu();
  10057. + cpu = get_cpu_light();
  10058. pool = per_cpu_ptr(mp->pool, cpu);
  10059. spin_lock_bh(&pool->lock);
  10060. - put_cpu();
  10061. + put_cpu_light();
  10062. /* peek cache of free slot */
  10063. if (pool->left != FC_XID_UNKNOWN) {
  10064. diff --git a/drivers/scsi/libsas/sas_ata.c b/drivers/scsi/libsas/sas_ata.c
  10065. index 6f5e2720ffad..ee8a8ed49b89 100644
  10066. --- a/drivers/scsi/libsas/sas_ata.c
  10067. +++ b/drivers/scsi/libsas/sas_ata.c
  10068. @@ -190,7 +190,7 @@ static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
  10069. /* TODO: audit callers to ensure they are ready for qc_issue to
  10070. * unconditionally re-enable interrupts
  10071. */
  10072. - local_irq_save(flags);
  10073. + local_irq_save_nort(flags);
  10074. spin_unlock(ap->lock);
  10075. /* If the device fell off, no sense in issuing commands */
  10076. @@ -255,7 +255,7 @@ static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
  10077. out:
  10078. spin_lock(ap->lock);
  10079. - local_irq_restore(flags);
  10080. + local_irq_restore_nort(flags);
  10081. return ret;
  10082. }
  10083. diff --git a/drivers/scsi/qla2xxx/qla_inline.h b/drivers/scsi/qla2xxx/qla_inline.h
  10084. index fee9eb7c8a60..b42d4adc42dc 100644
  10085. --- a/drivers/scsi/qla2xxx/qla_inline.h
  10086. +++ b/drivers/scsi/qla2xxx/qla_inline.h
  10087. @@ -59,12 +59,12 @@ qla2x00_poll(struct rsp_que *rsp)
  10088. {
  10089. unsigned long flags;
  10090. struct qla_hw_data *ha = rsp->hw;
  10091. - local_irq_save(flags);
  10092. + local_irq_save_nort(flags);
  10093. if (IS_P3P_TYPE(ha))
  10094. qla82xx_poll(0, rsp);
  10095. else
  10096. ha->isp_ops->intr_handler(0, rsp);
  10097. - local_irq_restore(flags);
  10098. + local_irq_restore_nort(flags);
  10099. }
  10100. static inline uint8_t *
  10101. diff --git a/drivers/thermal/x86_pkg_temp_thermal.c b/drivers/thermal/x86_pkg_temp_thermal.c
  10102. index 7fc919f7da4d..e03fa17b8670 100644
  10103. --- a/drivers/thermal/x86_pkg_temp_thermal.c
  10104. +++ b/drivers/thermal/x86_pkg_temp_thermal.c
  10105. @@ -29,6 +29,7 @@
  10106. #include <linux/pm.h>
  10107. #include <linux/thermal.h>
  10108. #include <linux/debugfs.h>
  10109. +#include <linux/swork.h>
  10110. #include <asm/cpu_device_id.h>
  10111. #include <asm/mce.h>
  10112. @@ -352,7 +353,7 @@ static void pkg_temp_thermal_threshold_work_fn(struct work_struct *work)
  10113. }
  10114. }
  10115. -static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  10116. +static void platform_thermal_notify_work(struct swork_event *event)
  10117. {
  10118. unsigned long flags;
  10119. int cpu = smp_processor_id();
  10120. @@ -369,7 +370,7 @@ static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  10121. pkg_work_scheduled[phy_id]) {
  10122. disable_pkg_thres_interrupt();
  10123. spin_unlock_irqrestore(&pkg_work_lock, flags);
  10124. - return -EINVAL;
  10125. + return;
  10126. }
  10127. pkg_work_scheduled[phy_id] = 1;
  10128. spin_unlock_irqrestore(&pkg_work_lock, flags);
  10129. @@ -378,9 +379,48 @@ static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  10130. schedule_delayed_work_on(cpu,
  10131. &per_cpu(pkg_temp_thermal_threshold_work, cpu),
  10132. msecs_to_jiffies(notify_delay_ms));
  10133. +}
  10134. +
  10135. +#ifdef CONFIG_PREEMPT_RT_FULL
  10136. +static struct swork_event notify_work;
  10137. +
  10138. +static int thermal_notify_work_init(void)
  10139. +{
  10140. + int err;
  10141. +
  10142. + err = swork_get();
  10143. + if (err)
  10144. + return err;
  10145. +
  10146. + INIT_SWORK(&notify_work, platform_thermal_notify_work);
  10147. return 0;
  10148. }
  10149. +static void thermal_notify_work_cleanup(void)
  10150. +{
  10151. + swork_put();
  10152. +}
  10153. +
  10154. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  10155. +{
  10156. + swork_queue(&notify_work);
  10157. + return 0;
  10158. +}
  10159. +
  10160. +#else /* !CONFIG_PREEMPT_RT_FULL */
  10161. +
  10162. +static int thermal_notify_work_init(void) { return 0; }
  10163. +
  10164. +static void thermal_notify_work_cleanup(void) { }
  10165. +
  10166. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  10167. +{
  10168. + platform_thermal_notify_work(NULL);
  10169. +
  10170. + return 0;
  10171. +}
  10172. +#endif /* CONFIG_PREEMPT_RT_FULL */
  10173. +
  10174. static int find_siblings_cpu(int cpu)
  10175. {
  10176. int i;
  10177. @@ -584,6 +624,9 @@ static int __init pkg_temp_thermal_init(void)
  10178. if (!x86_match_cpu(pkg_temp_thermal_ids))
  10179. return -ENODEV;
  10180. + if (!thermal_notify_work_init())
  10181. + return -ENODEV;
  10182. +
  10183. spin_lock_init(&pkg_work_lock);
  10184. platform_thermal_package_notify =
  10185. pkg_temp_thermal_platform_thermal_notify;
  10186. @@ -608,7 +651,7 @@ static int __init pkg_temp_thermal_init(void)
  10187. kfree(pkg_work_scheduled);
  10188. platform_thermal_package_notify = NULL;
  10189. platform_thermal_package_rate_control = NULL;
  10190. -
  10191. + thermal_notify_work_cleanup();
  10192. return -ENODEV;
  10193. }
  10194. @@ -633,6 +676,7 @@ static void __exit pkg_temp_thermal_exit(void)
  10195. mutex_unlock(&phy_dev_list_mutex);
  10196. platform_thermal_package_notify = NULL;
  10197. platform_thermal_package_rate_control = NULL;
  10198. + thermal_notify_work_cleanup();
  10199. for_each_online_cpu(i)
  10200. cancel_delayed_work_sync(
  10201. &per_cpu(pkg_temp_thermal_threshold_work, i));
  10202. diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/8250_core.c
  10203. index 39126460c1f5..af7701ca4d48 100644
  10204. --- a/drivers/tty/serial/8250/8250_core.c
  10205. +++ b/drivers/tty/serial/8250/8250_core.c
  10206. @@ -58,7 +58,16 @@ static struct uart_driver serial8250_reg;
  10207. static unsigned int skip_txen_test; /* force skip of txen test at init time */
  10208. -#define PASS_LIMIT 512
  10209. +/*
  10210. + * On -rt we can have a more delays, and legitimately
  10211. + * so - so don't drop work spuriously and spam the
  10212. + * syslog:
  10213. + */
  10214. +#ifdef CONFIG_PREEMPT_RT_FULL
  10215. +# define PASS_LIMIT 1000000
  10216. +#else
  10217. +# define PASS_LIMIT 512
  10218. +#endif
  10219. #include <asm/serial.h>
  10220. /*
  10221. diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c
  10222. index d42d66b72d5a..c08e0724a487 100644
  10223. --- a/drivers/tty/serial/8250/8250_port.c
  10224. +++ b/drivers/tty/serial/8250/8250_port.c
  10225. @@ -35,6 +35,7 @@
  10226. #include <linux/nmi.h>
  10227. #include <linux/mutex.h>
  10228. #include <linux/slab.h>
  10229. +#include <linux/kdb.h>
  10230. #include <linux/uaccess.h>
  10231. #include <linux/pm_runtime.h>
  10232. @@ -2846,9 +2847,9 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,
  10233. serial8250_rpm_get(up);
  10234. - if (port->sysrq)
  10235. + if (port->sysrq || oops_in_progress)
  10236. locked = 0;
  10237. - else if (oops_in_progress)
  10238. + else if (in_kdb_printk())
  10239. locked = spin_trylock_irqsave(&port->lock, flags);
  10240. else
  10241. spin_lock_irqsave(&port->lock, flags);
  10242. diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl011.c
  10243. index 899a77187bde..3ff6363b3751 100644
  10244. --- a/drivers/tty/serial/amba-pl011.c
  10245. +++ b/drivers/tty/serial/amba-pl011.c
  10246. @@ -2067,13 +2067,19 @@ pl011_console_write(struct console *co, const char *s, unsigned int count)
  10247. clk_enable(uap->clk);
  10248. - local_irq_save(flags);
  10249. + /*
  10250. + * local_irq_save(flags);
  10251. + *
  10252. + * This local_irq_save() is nonsense. If we come in via sysrq
  10253. + * handling then interrupts are already disabled. Aside of
  10254. + * that the port.sysrq check is racy on SMP regardless.
  10255. + */
  10256. if (uap->port.sysrq)
  10257. locked = 0;
  10258. else if (oops_in_progress)
  10259. - locked = spin_trylock(&uap->port.lock);
  10260. + locked = spin_trylock_irqsave(&uap->port.lock, flags);
  10261. else
  10262. - spin_lock(&uap->port.lock);
  10263. + spin_lock_irqsave(&uap->port.lock, flags);
  10264. /*
  10265. * First save the CR then disable the interrupts
  10266. @@ -2098,8 +2104,7 @@ pl011_console_write(struct console *co, const char *s, unsigned int count)
  10267. writew(old_cr, uap->port.membase + UART011_CR);
  10268. if (locked)
  10269. - spin_unlock(&uap->port.lock);
  10270. - local_irq_restore(flags);
  10271. + spin_unlock_irqrestore(&uap->port.lock, flags);
  10272. clk_disable(uap->clk);
  10273. }
  10274. diff --git a/drivers/tty/serial/omap-serial.c b/drivers/tty/serial/omap-serial.c
  10275. index 21fc9b3a27cf..2e32729e6a83 100644
  10276. --- a/drivers/tty/serial/omap-serial.c
  10277. +++ b/drivers/tty/serial/omap-serial.c
  10278. @@ -1257,13 +1257,10 @@ serial_omap_console_write(struct console *co, const char *s,
  10279. pm_runtime_get_sync(up->dev);
  10280. - local_irq_save(flags);
  10281. - if (up->port.sysrq)
  10282. - locked = 0;
  10283. - else if (oops_in_progress)
  10284. - locked = spin_trylock(&up->port.lock);
  10285. + if (up->port.sysrq || oops_in_progress)
  10286. + locked = spin_trylock_irqsave(&up->port.lock, flags);
  10287. else
  10288. - spin_lock(&up->port.lock);
  10289. + spin_lock_irqsave(&up->port.lock, flags);
  10290. /*
  10291. * First save the IER then disable the interrupts
  10292. @@ -1292,8 +1289,7 @@ serial_omap_console_write(struct console *co, const char *s,
  10293. pm_runtime_mark_last_busy(up->dev);
  10294. pm_runtime_put_autosuspend(up->dev);
  10295. if (locked)
  10296. - spin_unlock(&up->port.lock);
  10297. - local_irq_restore(flags);
  10298. + spin_unlock_irqrestore(&up->port.lock, flags);
  10299. }
  10300. static int __init
  10301. diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
  10302. index 87a83d925eea..995a37bde992 100644
  10303. --- a/drivers/usb/core/hcd.c
  10304. +++ b/drivers/usb/core/hcd.c
  10305. @@ -1738,9 +1738,9 @@ static void __usb_hcd_giveback_urb(struct urb *urb)
  10306. * and no one may trigger the above deadlock situation when
  10307. * running complete() in tasklet.
  10308. */
  10309. - local_irq_save(flags);
  10310. + local_irq_save_nort(flags);
  10311. urb->complete(urb);
  10312. - local_irq_restore(flags);
  10313. + local_irq_restore_nort(flags);
  10314. usb_anchor_resume_wakeups(anchor);
  10315. atomic_dec(&urb->use_count);
  10316. diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
  10317. index 39bb65265bff..49c167d150ce 100644
  10318. --- a/drivers/usb/gadget/function/f_fs.c
  10319. +++ b/drivers/usb/gadget/function/f_fs.c
  10320. @@ -1404,7 +1404,7 @@ static void ffs_data_put(struct ffs_data *ffs)
  10321. pr_info("%s(): freeing\n", __func__);
  10322. ffs_data_clear(ffs);
  10323. BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
  10324. - waitqueue_active(&ffs->ep0req_completion.wait));
  10325. + swait_active(&ffs->ep0req_completion.wait));
  10326. kfree(ffs->dev_name);
  10327. kfree(ffs);
  10328. }
  10329. diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c
  10330. index 81f3c9cb333c..c996bd43741a 100644
  10331. --- a/drivers/usb/gadget/legacy/inode.c
  10332. +++ b/drivers/usb/gadget/legacy/inode.c
  10333. @@ -346,7 +346,7 @@ ep_io (struct ep_data *epdata, void *buf, unsigned len)
  10334. spin_unlock_irq (&epdata->dev->lock);
  10335. if (likely (value == 0)) {
  10336. - value = wait_event_interruptible (done.wait, done.done);
  10337. + value = swait_event_interruptible (done.wait, done.done);
  10338. if (value != 0) {
  10339. spin_lock_irq (&epdata->dev->lock);
  10340. if (likely (epdata->ep != NULL)) {
  10341. @@ -355,7 +355,7 @@ ep_io (struct ep_data *epdata, void *buf, unsigned len)
  10342. usb_ep_dequeue (epdata->ep, epdata->req);
  10343. spin_unlock_irq (&epdata->dev->lock);
  10344. - wait_event (done.wait, done.done);
  10345. + swait_event (done.wait, done.done);
  10346. if (epdata->status == -ECONNRESET)
  10347. epdata->status = -EINTR;
  10348. } else {
  10349. diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
  10350. index 585cb8734f50..1e469be2c216 100644
  10351. --- a/drivers/usb/gadget/udc/atmel_usba_udc.c
  10352. +++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
  10353. @@ -17,7 +17,9 @@
  10354. #include <linux/device.h>
  10355. #include <linux/dma-mapping.h>
  10356. #include <linux/list.h>
  10357. +#include <linux/mfd/syscon.h>
  10358. #include <linux/platform_device.h>
  10359. +#include <linux/regmap.h>
  10360. #include <linux/usb/ch9.h>
  10361. #include <linux/usb/gadget.h>
  10362. #include <linux/usb/atmel_usba_udc.h>
  10363. @@ -1890,20 +1892,15 @@ static int atmel_usba_stop(struct usb_gadget *gadget)
  10364. #ifdef CONFIG_OF
  10365. static void at91sam9rl_toggle_bias(struct usba_udc *udc, int is_on)
  10366. {
  10367. - unsigned int uckr = at91_pmc_read(AT91_CKGR_UCKR);
  10368. -
  10369. - if (is_on)
  10370. - at91_pmc_write(AT91_CKGR_UCKR, uckr | AT91_PMC_BIASEN);
  10371. - else
  10372. - at91_pmc_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN));
  10373. + regmap_update_bits(udc->pmc, AT91_CKGR_UCKR, AT91_PMC_BIASEN,
  10374. + is_on ? AT91_PMC_BIASEN : 0);
  10375. }
  10376. static void at91sam9g45_pulse_bias(struct usba_udc *udc)
  10377. {
  10378. - unsigned int uckr = at91_pmc_read(AT91_CKGR_UCKR);
  10379. -
  10380. - at91_pmc_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN));
  10381. - at91_pmc_write(AT91_CKGR_UCKR, uckr | AT91_PMC_BIASEN);
  10382. + regmap_update_bits(udc->pmc, AT91_CKGR_UCKR, AT91_PMC_BIASEN, 0);
  10383. + regmap_update_bits(udc->pmc, AT91_CKGR_UCKR, AT91_PMC_BIASEN,
  10384. + AT91_PMC_BIASEN);
  10385. }
  10386. static const struct usba_udc_errata at91sam9rl_errata = {
  10387. @@ -1940,6 +1937,9 @@ static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev,
  10388. return ERR_PTR(-EINVAL);
  10389. udc->errata = match->data;
  10390. + udc->pmc = syscon_regmap_lookup_by_compatible("atmel,at91sam9g45-pmc");
  10391. + if (udc->errata && IS_ERR(udc->pmc))
  10392. + return ERR_CAST(udc->pmc);
  10393. udc->num_ep = 0;
  10394. diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.h b/drivers/usb/gadget/udc/atmel_usba_udc.h
  10395. index ea448a344767..3e1c9d589dfa 100644
  10396. --- a/drivers/usb/gadget/udc/atmel_usba_udc.h
  10397. +++ b/drivers/usb/gadget/udc/atmel_usba_udc.h
  10398. @@ -354,6 +354,8 @@ struct usba_udc {
  10399. struct dentry *debugfs_root;
  10400. struct dentry *debugfs_regs;
  10401. #endif
  10402. +
  10403. + struct regmap *pmc;
  10404. };
  10405. static inline struct usba_ep *to_usba_ep(struct usb_ep *ep)
  10406. diff --git a/fs/aio.c b/fs/aio.c
  10407. index fe4f49212b99..c3194afdc3df 100644
  10408. --- a/fs/aio.c
  10409. +++ b/fs/aio.c
  10410. @@ -40,6 +40,7 @@
  10411. #include <linux/ramfs.h>
  10412. #include <linux/percpu-refcount.h>
  10413. #include <linux/mount.h>
  10414. +#include <linux/swork.h>
  10415. #include <asm/kmap_types.h>
  10416. #include <asm/uaccess.h>
  10417. @@ -115,7 +116,7 @@ struct kioctx {
  10418. struct page **ring_pages;
  10419. long nr_pages;
  10420. - struct work_struct free_work;
  10421. + struct swork_event free_work;
  10422. /*
  10423. * signals when all in-flight requests are done
  10424. @@ -258,6 +259,7 @@ static int __init aio_setup(void)
  10425. .mount = aio_mount,
  10426. .kill_sb = kill_anon_super,
  10427. };
  10428. + BUG_ON(swork_get());
  10429. aio_mnt = kern_mount(&aio_fs);
  10430. if (IS_ERR(aio_mnt))
  10431. panic("Failed to create aio fs mount.");
  10432. @@ -573,9 +575,9 @@ static int kiocb_cancel(struct aio_kiocb *kiocb)
  10433. return cancel(&kiocb->common);
  10434. }
  10435. -static void free_ioctx(struct work_struct *work)
  10436. +static void free_ioctx(struct swork_event *sev)
  10437. {
  10438. - struct kioctx *ctx = container_of(work, struct kioctx, free_work);
  10439. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  10440. pr_debug("freeing %p\n", ctx);
  10441. @@ -594,8 +596,8 @@ static void free_ioctx_reqs(struct percpu_ref *ref)
  10442. if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
  10443. complete(&ctx->rq_wait->comp);
  10444. - INIT_WORK(&ctx->free_work, free_ioctx);
  10445. - schedule_work(&ctx->free_work);
  10446. + INIT_SWORK(&ctx->free_work, free_ioctx);
  10447. + swork_queue(&ctx->free_work);
  10448. }
  10449. /*
  10450. @@ -603,9 +605,9 @@ static void free_ioctx_reqs(struct percpu_ref *ref)
  10451. * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
  10452. * now it's safe to cancel any that need to be.
  10453. */
  10454. -static void free_ioctx_users(struct percpu_ref *ref)
  10455. +static void free_ioctx_users_work(struct swork_event *sev)
  10456. {
  10457. - struct kioctx *ctx = container_of(ref, struct kioctx, users);
  10458. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  10459. struct aio_kiocb *req;
  10460. spin_lock_irq(&ctx->ctx_lock);
  10461. @@ -624,6 +626,14 @@ static void free_ioctx_users(struct percpu_ref *ref)
  10462. percpu_ref_put(&ctx->reqs);
  10463. }
  10464. +static void free_ioctx_users(struct percpu_ref *ref)
  10465. +{
  10466. + struct kioctx *ctx = container_of(ref, struct kioctx, users);
  10467. +
  10468. + INIT_SWORK(&ctx->free_work, free_ioctx_users_work);
  10469. + swork_queue(&ctx->free_work);
  10470. +}
  10471. +
  10472. static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
  10473. {
  10474. unsigned i, new_nr;
  10475. diff --git a/fs/autofs4/autofs_i.h b/fs/autofs4/autofs_i.h
  10476. index 502d3892d8a4..05af8d3e6e88 100644
  10477. --- a/fs/autofs4/autofs_i.h
  10478. +++ b/fs/autofs4/autofs_i.h
  10479. @@ -34,6 +34,7 @@
  10480. #include <linux/sched.h>
  10481. #include <linux/mount.h>
  10482. #include <linux/namei.h>
  10483. +#include <linux/delay.h>
  10484. #include <asm/current.h>
  10485. #include <asm/uaccess.h>
  10486. diff --git a/fs/autofs4/expire.c b/fs/autofs4/expire.c
  10487. index 7a5a598a2d94..d08bcdc30566 100644
  10488. --- a/fs/autofs4/expire.c
  10489. +++ b/fs/autofs4/expire.c
  10490. @@ -150,7 +150,7 @@ static struct dentry *get_next_positive_dentry(struct dentry *prev,
  10491. parent = p->d_parent;
  10492. if (!spin_trylock(&parent->d_lock)) {
  10493. spin_unlock(&p->d_lock);
  10494. - cpu_relax();
  10495. + cpu_chill();
  10496. goto relock;
  10497. }
  10498. spin_unlock(&p->d_lock);
  10499. diff --git a/fs/buffer.c b/fs/buffer.c
  10500. index 6f7d519a093b..96b943e924cc 100644
  10501. --- a/fs/buffer.c
  10502. +++ b/fs/buffer.c
  10503. @@ -305,8 +305,7 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10504. * decide that the page is now completely done.
  10505. */
  10506. first = page_buffers(page);
  10507. - local_irq_save(flags);
  10508. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  10509. + flags = bh_uptodate_lock_irqsave(first);
  10510. clear_buffer_async_read(bh);
  10511. unlock_buffer(bh);
  10512. tmp = bh;
  10513. @@ -319,8 +318,7 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10514. }
  10515. tmp = tmp->b_this_page;
  10516. } while (tmp != bh);
  10517. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10518. - local_irq_restore(flags);
  10519. + bh_uptodate_unlock_irqrestore(first, flags);
  10520. /*
  10521. * If none of the buffers had errors and they are all
  10522. @@ -332,9 +330,7 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10523. return;
  10524. still_busy:
  10525. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10526. - local_irq_restore(flags);
  10527. - return;
  10528. + bh_uptodate_unlock_irqrestore(first, flags);
  10529. }
  10530. /*
  10531. @@ -362,8 +358,7 @@ void end_buffer_async_write(struct buffer_head *bh, int uptodate)
  10532. }
  10533. first = page_buffers(page);
  10534. - local_irq_save(flags);
  10535. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  10536. + flags = bh_uptodate_lock_irqsave(first);
  10537. clear_buffer_async_write(bh);
  10538. unlock_buffer(bh);
  10539. @@ -375,15 +370,12 @@ void end_buffer_async_write(struct buffer_head *bh, int uptodate)
  10540. }
  10541. tmp = tmp->b_this_page;
  10542. }
  10543. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10544. - local_irq_restore(flags);
  10545. + bh_uptodate_unlock_irqrestore(first, flags);
  10546. end_page_writeback(page);
  10547. return;
  10548. still_busy:
  10549. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10550. - local_irq_restore(flags);
  10551. - return;
  10552. + bh_uptodate_unlock_irqrestore(first, flags);
  10553. }
  10554. EXPORT_SYMBOL(end_buffer_async_write);
  10555. @@ -3325,6 +3317,7 @@ struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
  10556. struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
  10557. if (ret) {
  10558. INIT_LIST_HEAD(&ret->b_assoc_buffers);
  10559. + buffer_head_init_locks(ret);
  10560. preempt_disable();
  10561. __this_cpu_inc(bh_accounting.nr);
  10562. recalc_bh_state();
  10563. diff --git a/fs/dcache.c b/fs/dcache.c
  10564. index 3ed642e0a0c2..1437dd042e13 100644
  10565. --- a/fs/dcache.c
  10566. +++ b/fs/dcache.c
  10567. @@ -19,6 +19,7 @@
  10568. #include <linux/mm.h>
  10569. #include <linux/fs.h>
  10570. #include <linux/fsnotify.h>
  10571. +#include <linux/delay.h>
  10572. #include <linux/slab.h>
  10573. #include <linux/init.h>
  10574. #include <linux/hash.h>
  10575. @@ -774,6 +775,8 @@ static inline bool fast_dput(struct dentry *dentry)
  10576. */
  10577. void dput(struct dentry *dentry)
  10578. {
  10579. + struct dentry *parent;
  10580. +
  10581. if (unlikely(!dentry))
  10582. return;
  10583. @@ -810,9 +813,18 @@ void dput(struct dentry *dentry)
  10584. return;
  10585. kill_it:
  10586. - dentry = dentry_kill(dentry);
  10587. - if (dentry) {
  10588. - cond_resched();
  10589. + parent = dentry_kill(dentry);
  10590. + if (parent) {
  10591. + int r;
  10592. +
  10593. + if (parent == dentry) {
  10594. + /* the task with the highest priority won't schedule */
  10595. + r = cond_resched();
  10596. + if (!r)
  10597. + cpu_chill();
  10598. + } else {
  10599. + dentry = parent;
  10600. + }
  10601. goto repeat;
  10602. }
  10603. }
  10604. @@ -2425,7 +2437,7 @@ void d_delete(struct dentry * dentry)
  10605. if (dentry->d_lockref.count == 1) {
  10606. if (!spin_trylock(&inode->i_lock)) {
  10607. spin_unlock(&dentry->d_lock);
  10608. - cpu_relax();
  10609. + cpu_chill();
  10610. goto again;
  10611. }
  10612. dentry->d_flags &= ~DCACHE_CANT_MOUNT;
  10613. diff --git a/fs/eventpoll.c b/fs/eventpoll.c
  10614. index 1b08556776ce..06435352045d 100644
  10615. --- a/fs/eventpoll.c
  10616. +++ b/fs/eventpoll.c
  10617. @@ -505,12 +505,12 @@ static int ep_poll_wakeup_proc(void *priv, void *cookie, int call_nests)
  10618. */
  10619. static void ep_poll_safewake(wait_queue_head_t *wq)
  10620. {
  10621. - int this_cpu = get_cpu();
  10622. + int this_cpu = get_cpu_light();
  10623. ep_call_nested(&poll_safewake_ncalls, EP_MAX_NESTS,
  10624. ep_poll_wakeup_proc, NULL, wq, (void *) (long) this_cpu);
  10625. - put_cpu();
  10626. + put_cpu_light();
  10627. }
  10628. static void ep_remove_wait_queue(struct eppoll_entry *pwq)
  10629. diff --git a/fs/exec.c b/fs/exec.c
  10630. index 9c5ee2a880aa..c12e576bb209 100644
  10631. --- a/fs/exec.c
  10632. +++ b/fs/exec.c
  10633. @@ -887,12 +887,14 @@ static int exec_mmap(struct mm_struct *mm)
  10634. }
  10635. }
  10636. task_lock(tsk);
  10637. + preempt_disable_rt();
  10638. active_mm = tsk->active_mm;
  10639. tsk->mm = mm;
  10640. tsk->active_mm = mm;
  10641. activate_mm(active_mm, mm);
  10642. tsk->mm->vmacache_seqnum = 0;
  10643. vmacache_flush(tsk);
  10644. + preempt_enable_rt();
  10645. task_unlock(tsk);
  10646. if (old_mm) {
  10647. up_read(&old_mm->mmap_sem);
  10648. diff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c
  10649. index 6ca56f5f72b5..9e145fe7cae0 100644
  10650. --- a/fs/ext4/page-io.c
  10651. +++ b/fs/ext4/page-io.c
  10652. @@ -97,8 +97,7 @@ static void ext4_finish_bio(struct bio *bio)
  10653. * We check all buffers in the page under BH_Uptodate_Lock
  10654. * to avoid races with other end io clearing async_write flags
  10655. */
  10656. - local_irq_save(flags);
  10657. - bit_spin_lock(BH_Uptodate_Lock, &head->b_state);
  10658. + flags = bh_uptodate_lock_irqsave(head);
  10659. do {
  10660. if (bh_offset(bh) < bio_start ||
  10661. bh_offset(bh) + bh->b_size > bio_end) {
  10662. @@ -110,8 +109,7 @@ static void ext4_finish_bio(struct bio *bio)
  10663. if (bio->bi_error)
  10664. buffer_io_error(bh);
  10665. } while ((bh = bh->b_this_page) != head);
  10666. - bit_spin_unlock(BH_Uptodate_Lock, &head->b_state);
  10667. - local_irq_restore(flags);
  10668. + bh_uptodate_unlock_irqrestore(head, flags);
  10669. if (!under_io) {
  10670. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  10671. if (ctx)
  10672. diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
  10673. index 2871576fbca4..d1137790ea58 100644
  10674. --- a/fs/f2fs/f2fs.h
  10675. +++ b/fs/f2fs/f2fs.h
  10676. @@ -24,7 +24,6 @@
  10677. #ifdef CONFIG_F2FS_CHECK_FS
  10678. #define f2fs_bug_on(sbi, condition) BUG_ON(condition)
  10679. -#define f2fs_down_write(x, y) down_write_nest_lock(x, y)
  10680. #else
  10681. #define f2fs_bug_on(sbi, condition) \
  10682. do { \
  10683. @@ -33,7 +32,6 @@
  10684. set_sbi_flag(sbi, SBI_NEED_FSCK); \
  10685. } \
  10686. } while (0)
  10687. -#define f2fs_down_write(x, y) down_write(x)
  10688. #endif
  10689. /*
  10690. @@ -959,7 +957,7 @@ static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
  10691. static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
  10692. {
  10693. - f2fs_down_write(&sbi->cp_rwsem, &sbi->cp_mutex);
  10694. + down_write(&sbi->cp_rwsem);
  10695. }
  10696. static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
  10697. diff --git a/fs/namespace.c b/fs/namespace.c
  10698. index ec4078d16eb7..1780601dcbdd 100644
  10699. --- a/fs/namespace.c
  10700. +++ b/fs/namespace.c
  10701. @@ -14,6 +14,7 @@
  10702. #include <linux/mnt_namespace.h>
  10703. #include <linux/user_namespace.h>
  10704. #include <linux/namei.h>
  10705. +#include <linux/delay.h>
  10706. #include <linux/security.h>
  10707. #include <linux/idr.h>
  10708. #include <linux/init.h> /* init_rootfs */
  10709. @@ -357,8 +358,11 @@ int __mnt_want_write(struct vfsmount *m)
  10710. * incremented count after it has set MNT_WRITE_HOLD.
  10711. */
  10712. smp_mb();
  10713. - while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
  10714. - cpu_relax();
  10715. + while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
  10716. + preempt_enable();
  10717. + cpu_chill();
  10718. + preempt_disable();
  10719. + }
  10720. /*
  10721. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  10722. * be set to match its requirements. So we must not load that until
  10723. diff --git a/fs/ntfs/aops.c b/fs/ntfs/aops.c
  10724. index 7521e11db728..f0de4b6b8bf3 100644
  10725. --- a/fs/ntfs/aops.c
  10726. +++ b/fs/ntfs/aops.c
  10727. @@ -107,8 +107,7 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10728. "0x%llx.", (unsigned long long)bh->b_blocknr);
  10729. }
  10730. first = page_buffers(page);
  10731. - local_irq_save(flags);
  10732. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  10733. + flags = bh_uptodate_lock_irqsave(first);
  10734. clear_buffer_async_read(bh);
  10735. unlock_buffer(bh);
  10736. tmp = bh;
  10737. @@ -123,8 +122,7 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10738. }
  10739. tmp = tmp->b_this_page;
  10740. } while (tmp != bh);
  10741. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10742. - local_irq_restore(flags);
  10743. + bh_uptodate_unlock_irqrestore(first, flags);
  10744. /*
  10745. * If none of the buffers had errors then we can set the page uptodate,
  10746. * but we first have to perform the post read mst fixups, if the
  10747. @@ -145,13 +143,13 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10748. recs = PAGE_CACHE_SIZE / rec_size;
  10749. /* Should have been verified before we got here... */
  10750. BUG_ON(!recs);
  10751. - local_irq_save(flags);
  10752. + local_irq_save_nort(flags);
  10753. kaddr = kmap_atomic(page);
  10754. for (i = 0; i < recs; i++)
  10755. post_read_mst_fixup((NTFS_RECORD*)(kaddr +
  10756. i * rec_size), rec_size);
  10757. kunmap_atomic(kaddr);
  10758. - local_irq_restore(flags);
  10759. + local_irq_restore_nort(flags);
  10760. flush_dcache_page(page);
  10761. if (likely(page_uptodate && !PageError(page)))
  10762. SetPageUptodate(page);
  10763. @@ -159,9 +157,7 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10764. unlock_page(page);
  10765. return;
  10766. still_busy:
  10767. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10768. - local_irq_restore(flags);
  10769. - return;
  10770. + bh_uptodate_unlock_irqrestore(first, flags);
  10771. }
  10772. /**
  10773. diff --git a/fs/timerfd.c b/fs/timerfd.c
  10774. index 1327a02ec778..4260febcb029 100644
  10775. --- a/fs/timerfd.c
  10776. +++ b/fs/timerfd.c
  10777. @@ -461,7 +461,10 @@ static int do_timerfd_settime(int ufd, int flags,
  10778. break;
  10779. }
  10780. spin_unlock_irq(&ctx->wqh.lock);
  10781. - cpu_relax();
  10782. + if (isalarm(ctx))
  10783. + hrtimer_wait_for_timer(&ctx->t.alarm.timer);
  10784. + else
  10785. + hrtimer_wait_for_timer(&ctx->t.tmr);
  10786. }
  10787. /*
  10788. diff --git a/include/acpi/platform/aclinux.h b/include/acpi/platform/aclinux.h
  10789. index 323e5daece54..cc5fbd534fd4 100644
  10790. --- a/include/acpi/platform/aclinux.h
  10791. +++ b/include/acpi/platform/aclinux.h
  10792. @@ -127,6 +127,7 @@
  10793. #define acpi_cache_t struct kmem_cache
  10794. #define acpi_spinlock spinlock_t *
  10795. +#define acpi_raw_spinlock raw_spinlock_t *
  10796. #define acpi_cpu_flags unsigned long
  10797. /* Use native linux version of acpi_os_allocate_zeroed */
  10798. @@ -145,6 +146,20 @@
  10799. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_get_thread_id
  10800. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_create_lock
  10801. +#define acpi_os_create_raw_lock(__handle) \
  10802. +({ \
  10803. + raw_spinlock_t *lock = ACPI_ALLOCATE(sizeof(*lock)); \
  10804. + \
  10805. + if (lock) { \
  10806. + *(__handle) = lock; \
  10807. + raw_spin_lock_init(*(__handle)); \
  10808. + } \
  10809. + lock ? AE_OK : AE_NO_MEMORY; \
  10810. + })
  10811. +
  10812. +#define acpi_os_delete_raw_lock(__handle) kfree(__handle)
  10813. +
  10814. +
  10815. /*
  10816. * OSL interfaces used by debugger/disassembler
  10817. */
  10818. diff --git a/include/asm-generic/bug.h b/include/asm-generic/bug.h
  10819. index 630dd2372238..850e4d993a88 100644
  10820. --- a/include/asm-generic/bug.h
  10821. +++ b/include/asm-generic/bug.h
  10822. @@ -206,6 +206,20 @@ extern void warn_slowpath_null(const char *file, const int line);
  10823. # define WARN_ON_SMP(x) ({0;})
  10824. #endif
  10825. +#ifdef CONFIG_PREEMPT_RT_BASE
  10826. +# define BUG_ON_RT(c) BUG_ON(c)
  10827. +# define BUG_ON_NONRT(c) do { } while (0)
  10828. +# define WARN_ON_RT(condition) WARN_ON(condition)
  10829. +# define WARN_ON_NONRT(condition) do { } while (0)
  10830. +# define WARN_ON_ONCE_NONRT(condition) do { } while (0)
  10831. +#else
  10832. +# define BUG_ON_RT(c) do { } while (0)
  10833. +# define BUG_ON_NONRT(c) BUG_ON(c)
  10834. +# define WARN_ON_RT(condition) do { } while (0)
  10835. +# define WARN_ON_NONRT(condition) WARN_ON(condition)
  10836. +# define WARN_ON_ONCE_NONRT(condition) WARN_ON_ONCE(condition)
  10837. +#endif
  10838. +
  10839. #endif /* __ASSEMBLY__ */
  10840. #endif
  10841. diff --git a/include/asm-generic/preempt.h b/include/asm-generic/preempt.h
  10842. index 5d8ffa3e6f8c..c1cde3577551 100644
  10843. --- a/include/asm-generic/preempt.h
  10844. +++ b/include/asm-generic/preempt.h
  10845. @@ -7,10 +7,10 @@
  10846. static __always_inline int preempt_count(void)
  10847. {
  10848. - return current_thread_info()->preempt_count;
  10849. + return READ_ONCE(current_thread_info()->preempt_count);
  10850. }
  10851. -static __always_inline int *preempt_count_ptr(void)
  10852. +static __always_inline volatile int *preempt_count_ptr(void)
  10853. {
  10854. return &current_thread_info()->preempt_count;
  10855. }
  10856. diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h
  10857. index daf17d70aeca..463df8954255 100644
  10858. --- a/include/linux/blk-mq.h
  10859. +++ b/include/linux/blk-mq.h
  10860. @@ -212,6 +212,7 @@ static inline u16 blk_mq_unique_tag_to_tag(u32 unique_tag)
  10861. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_index);
  10862. struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *, unsigned int, int);
  10863. +void __blk_mq_complete_request_remote_work(struct work_struct *work);
  10864. int blk_mq_request_started(struct request *rq);
  10865. void blk_mq_start_request(struct request *rq);
  10866. diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
  10867. index fe14382f9664..a82143ad6702 100644
  10868. --- a/include/linux/blkdev.h
  10869. +++ b/include/linux/blkdev.h
  10870. @@ -89,6 +89,7 @@ struct request {
  10871. struct list_head queuelist;
  10872. union {
  10873. struct call_single_data csd;
  10874. + struct work_struct work;
  10875. unsigned long fifo_time;
  10876. };
  10877. @@ -455,7 +456,7 @@ struct request_queue {
  10878. struct throtl_data *td;
  10879. #endif
  10880. struct rcu_head rcu_head;
  10881. - wait_queue_head_t mq_freeze_wq;
  10882. + struct swait_queue_head mq_freeze_wq;
  10883. struct percpu_ref q_usage_counter;
  10884. struct list_head all_q_node;
  10885. diff --git a/include/linux/bottom_half.h b/include/linux/bottom_half.h
  10886. index 8fdcb783197d..d07dbeec7bc1 100644
  10887. --- a/include/linux/bottom_half.h
  10888. +++ b/include/linux/bottom_half.h
  10889. @@ -3,6 +3,39 @@
  10890. #include <linux/preempt.h>
  10891. +#ifdef CONFIG_PREEMPT_RT_FULL
  10892. +
  10893. +extern void __local_bh_disable(void);
  10894. +extern void _local_bh_enable(void);
  10895. +extern void __local_bh_enable(void);
  10896. +
  10897. +static inline void local_bh_disable(void)
  10898. +{
  10899. + __local_bh_disable();
  10900. +}
  10901. +
  10902. +static inline void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
  10903. +{
  10904. + __local_bh_disable();
  10905. +}
  10906. +
  10907. +static inline void local_bh_enable(void)
  10908. +{
  10909. + __local_bh_enable();
  10910. +}
  10911. +
  10912. +static inline void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
  10913. +{
  10914. + __local_bh_enable();
  10915. +}
  10916. +
  10917. +static inline void local_bh_enable_ip(unsigned long ip)
  10918. +{
  10919. + __local_bh_enable();
  10920. +}
  10921. +
  10922. +#else
  10923. +
  10924. #ifdef CONFIG_TRACE_IRQFLAGS
  10925. extern void __local_bh_disable_ip(unsigned long ip, unsigned int cnt);
  10926. #else
  10927. @@ -30,5 +63,6 @@ static inline void local_bh_enable(void)
  10928. {
  10929. __local_bh_enable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET);
  10930. }
  10931. +#endif
  10932. #endif /* _LINUX_BH_H */
  10933. diff --git a/include/linux/buffer_head.h b/include/linux/buffer_head.h
  10934. index 6fe974dbe741..7242505af75c 100644
  10935. --- a/include/linux/buffer_head.h
  10936. +++ b/include/linux/buffer_head.h
  10937. @@ -75,8 +75,50 @@ struct buffer_head {
  10938. struct address_space *b_assoc_map; /* mapping this buffer is
  10939. associated with */
  10940. atomic_t b_count; /* users using this buffer_head */
  10941. +#ifdef CONFIG_PREEMPT_RT_BASE
  10942. + spinlock_t b_uptodate_lock;
  10943. +#if IS_ENABLED(CONFIG_JBD2)
  10944. + spinlock_t b_state_lock;
  10945. + spinlock_t b_journal_head_lock;
  10946. +#endif
  10947. +#endif
  10948. };
  10949. +static inline unsigned long bh_uptodate_lock_irqsave(struct buffer_head *bh)
  10950. +{
  10951. + unsigned long flags;
  10952. +
  10953. +#ifndef CONFIG_PREEMPT_RT_BASE
  10954. + local_irq_save(flags);
  10955. + bit_spin_lock(BH_Uptodate_Lock, &bh->b_state);
  10956. +#else
  10957. + spin_lock_irqsave(&bh->b_uptodate_lock, flags);
  10958. +#endif
  10959. + return flags;
  10960. +}
  10961. +
  10962. +static inline void
  10963. +bh_uptodate_unlock_irqrestore(struct buffer_head *bh, unsigned long flags)
  10964. +{
  10965. +#ifndef CONFIG_PREEMPT_RT_BASE
  10966. + bit_spin_unlock(BH_Uptodate_Lock, &bh->b_state);
  10967. + local_irq_restore(flags);
  10968. +#else
  10969. + spin_unlock_irqrestore(&bh->b_uptodate_lock, flags);
  10970. +#endif
  10971. +}
  10972. +
  10973. +static inline void buffer_head_init_locks(struct buffer_head *bh)
  10974. +{
  10975. +#ifdef CONFIG_PREEMPT_RT_BASE
  10976. + spin_lock_init(&bh->b_uptodate_lock);
  10977. +#if IS_ENABLED(CONFIG_JBD2)
  10978. + spin_lock_init(&bh->b_state_lock);
  10979. + spin_lock_init(&bh->b_journal_head_lock);
  10980. +#endif
  10981. +#endif
  10982. +}
  10983. +
  10984. /*
  10985. * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
  10986. * and buffer_foo() functions.
  10987. diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h
  10988. index 8da263299754..0cc474291e08 100644
  10989. --- a/include/linux/cgroup-defs.h
  10990. +++ b/include/linux/cgroup-defs.h
  10991. @@ -16,6 +16,7 @@
  10992. #include <linux/percpu-refcount.h>
  10993. #include <linux/percpu-rwsem.h>
  10994. #include <linux/workqueue.h>
  10995. +#include <linux/swork.h>
  10996. #ifdef CONFIG_CGROUPS
  10997. @@ -142,6 +143,7 @@ struct cgroup_subsys_state {
  10998. /* percpu_ref killing and RCU release */
  10999. struct rcu_head rcu_head;
  11000. struct work_struct destroy_work;
  11001. + struct swork_event destroy_swork;
  11002. };
  11003. /*
  11004. diff --git a/include/linux/clk/at91_pmc.h b/include/linux/clk/at91_pmc.h
  11005. index 1e6932222e11..17f413bbbedf 100644
  11006. --- a/include/linux/clk/at91_pmc.h
  11007. +++ b/include/linux/clk/at91_pmc.h
  11008. @@ -16,18 +16,6 @@
  11009. #ifndef AT91_PMC_H
  11010. #define AT91_PMC_H
  11011. -#ifndef __ASSEMBLY__
  11012. -extern void __iomem *at91_pmc_base;
  11013. -
  11014. -#define at91_pmc_read(field) \
  11015. - readl_relaxed(at91_pmc_base + field)
  11016. -
  11017. -#define at91_pmc_write(field, value) \
  11018. - writel_relaxed(value, at91_pmc_base + field)
  11019. -#else
  11020. -.extern at91_pmc_base
  11021. -#endif
  11022. -
  11023. #define AT91_PMC_SCER 0x00 /* System Clock Enable Register */
  11024. #define AT91_PMC_SCDR 0x04 /* System Clock Disable Register */
  11025. diff --git a/include/linux/completion.h b/include/linux/completion.h
  11026. index 5d5aaae3af43..3bca1590e29f 100644
  11027. --- a/include/linux/completion.h
  11028. +++ b/include/linux/completion.h
  11029. @@ -7,8 +7,7 @@
  11030. * Atomic wait-for-completion handler data structures.
  11031. * See kernel/sched/completion.c for details.
  11032. */
  11033. -
  11034. -#include <linux/wait.h>
  11035. +#include <linux/swait.h>
  11036. /*
  11037. * struct completion - structure used to maintain state for a "completion"
  11038. @@ -24,11 +23,11 @@
  11039. */
  11040. struct completion {
  11041. unsigned int done;
  11042. - wait_queue_head_t wait;
  11043. + struct swait_queue_head wait;
  11044. };
  11045. #define COMPLETION_INITIALIZER(work) \
  11046. - { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  11047. + { 0, __SWAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  11048. #define COMPLETION_INITIALIZER_ONSTACK(work) \
  11049. ({ init_completion(&work); work; })
  11050. @@ -73,7 +72,7 @@ struct completion {
  11051. static inline void init_completion(struct completion *x)
  11052. {
  11053. x->done = 0;
  11054. - init_waitqueue_head(&x->wait);
  11055. + init_swait_queue_head(&x->wait);
  11056. }
  11057. /**
  11058. diff --git a/include/linux/cpu.h b/include/linux/cpu.h
  11059. index 7e04bcd9af8e..e7fa0f6935e6 100644
  11060. --- a/include/linux/cpu.h
  11061. +++ b/include/linux/cpu.h
  11062. @@ -231,6 +231,8 @@ extern void get_online_cpus(void);
  11063. extern void put_online_cpus(void);
  11064. extern void cpu_hotplug_disable(void);
  11065. extern void cpu_hotplug_enable(void);
  11066. +extern void pin_current_cpu(void);
  11067. +extern void unpin_current_cpu(void);
  11068. #define hotcpu_notifier(fn, pri) cpu_notifier(fn, pri)
  11069. #define __hotcpu_notifier(fn, pri) __cpu_notifier(fn, pri)
  11070. #define register_hotcpu_notifier(nb) register_cpu_notifier(nb)
  11071. @@ -248,6 +250,8 @@ static inline void cpu_hotplug_done(void) {}
  11072. #define put_online_cpus() do { } while (0)
  11073. #define cpu_hotplug_disable() do { } while (0)
  11074. #define cpu_hotplug_enable() do { } while (0)
  11075. +static inline void pin_current_cpu(void) { }
  11076. +static inline void unpin_current_cpu(void) { }
  11077. #define hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  11078. #define __hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  11079. /* These aren't inline functions due to a GCC bug. */
  11080. diff --git a/include/linux/delay.h b/include/linux/delay.h
  11081. index a6ecb34cf547..37caab306336 100644
  11082. --- a/include/linux/delay.h
  11083. +++ b/include/linux/delay.h
  11084. @@ -52,4 +52,10 @@ static inline void ssleep(unsigned int seconds)
  11085. msleep(seconds * 1000);
  11086. }
  11087. +#ifdef CONFIG_PREEMPT_RT_FULL
  11088. +extern void cpu_chill(void);
  11089. +#else
  11090. +# define cpu_chill() cpu_relax()
  11091. +#endif
  11092. +
  11093. #endif /* defined(_LINUX_DELAY_H) */
  11094. diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h
  11095. index 60048c50404e..f2cd67624f18 100644
  11096. --- a/include/linux/ftrace.h
  11097. +++ b/include/linux/ftrace.h
  11098. @@ -694,6 +694,18 @@ static inline void __ftrace_enabled_restore(int enabled)
  11099. #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
  11100. #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
  11101. +static inline unsigned long get_lock_parent_ip(void)
  11102. +{
  11103. + unsigned long addr = CALLER_ADDR0;
  11104. +
  11105. + if (!in_lock_functions(addr))
  11106. + return addr;
  11107. + addr = CALLER_ADDR1;
  11108. + if (!in_lock_functions(addr))
  11109. + return addr;
  11110. + return CALLER_ADDR2;
  11111. +}
  11112. +
  11113. #ifdef CONFIG_IRQSOFF_TRACER
  11114. extern void time_hardirqs_on(unsigned long a0, unsigned long a1);
  11115. extern void time_hardirqs_off(unsigned long a0, unsigned long a1);
  11116. diff --git a/include/linux/highmem.h b/include/linux/highmem.h
  11117. index bb3f3297062a..a117a33ef72c 100644
  11118. --- a/include/linux/highmem.h
  11119. +++ b/include/linux/highmem.h
  11120. @@ -7,6 +7,7 @@
  11121. #include <linux/mm.h>
  11122. #include <linux/uaccess.h>
  11123. #include <linux/hardirq.h>
  11124. +#include <linux/sched.h>
  11125. #include <asm/cacheflush.h>
  11126. @@ -65,7 +66,7 @@ static inline void kunmap(struct page *page)
  11127. static inline void *kmap_atomic(struct page *page)
  11128. {
  11129. - preempt_disable();
  11130. + preempt_disable_nort();
  11131. pagefault_disable();
  11132. return page_address(page);
  11133. }
  11134. @@ -74,7 +75,7 @@ static inline void *kmap_atomic(struct page *page)
  11135. static inline void __kunmap_atomic(void *addr)
  11136. {
  11137. pagefault_enable();
  11138. - preempt_enable();
  11139. + preempt_enable_nort();
  11140. }
  11141. #define kmap_atomic_pfn(pfn) kmap_atomic(pfn_to_page(pfn))
  11142. @@ -86,32 +87,51 @@ static inline void __kunmap_atomic(void *addr)
  11143. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  11144. +#ifndef CONFIG_PREEMPT_RT_FULL
  11145. DECLARE_PER_CPU(int, __kmap_atomic_idx);
  11146. +#endif
  11147. static inline int kmap_atomic_idx_push(void)
  11148. {
  11149. +#ifndef CONFIG_PREEMPT_RT_FULL
  11150. int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;
  11151. -#ifdef CONFIG_DEBUG_HIGHMEM
  11152. +# ifdef CONFIG_DEBUG_HIGHMEM
  11153. WARN_ON_ONCE(in_irq() && !irqs_disabled());
  11154. BUG_ON(idx >= KM_TYPE_NR);
  11155. -#endif
  11156. +# endif
  11157. return idx;
  11158. +#else
  11159. + current->kmap_idx++;
  11160. + BUG_ON(current->kmap_idx > KM_TYPE_NR);
  11161. + return current->kmap_idx - 1;
  11162. +#endif
  11163. }
  11164. static inline int kmap_atomic_idx(void)
  11165. {
  11166. +#ifndef CONFIG_PREEMPT_RT_FULL
  11167. return __this_cpu_read(__kmap_atomic_idx) - 1;
  11168. +#else
  11169. + return current->kmap_idx - 1;
  11170. +#endif
  11171. }
  11172. static inline void kmap_atomic_idx_pop(void)
  11173. {
  11174. -#ifdef CONFIG_DEBUG_HIGHMEM
  11175. +#ifndef CONFIG_PREEMPT_RT_FULL
  11176. +# ifdef CONFIG_DEBUG_HIGHMEM
  11177. int idx = __this_cpu_dec_return(__kmap_atomic_idx);
  11178. BUG_ON(idx < 0);
  11179. -#else
  11180. +# else
  11181. __this_cpu_dec(__kmap_atomic_idx);
  11182. +# endif
  11183. +#else
  11184. + current->kmap_idx--;
  11185. +# ifdef CONFIG_DEBUG_HIGHMEM
  11186. + BUG_ON(current->kmap_idx < 0);
  11187. +# endif
  11188. #endif
  11189. }
  11190. diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h
  11191. index 2ead22dd74a0..ff317006d3e8 100644
  11192. --- a/include/linux/hrtimer.h
  11193. +++ b/include/linux/hrtimer.h
  11194. @@ -87,6 +87,9 @@ enum hrtimer_restart {
  11195. * @function: timer expiry callback function
  11196. * @base: pointer to the timer base (per cpu and per clock)
  11197. * @state: state information (See bit values above)
  11198. + * @cb_entry: list entry to defer timers from hardirq context
  11199. + * @irqsafe: timer can run in hardirq context
  11200. + * @praecox: timer expiry time if expired at the time of programming
  11201. * @is_rel: Set if the timer was armed relative
  11202. * @start_pid: timer statistics field to store the pid of the task which
  11203. * started the timer
  11204. @@ -103,6 +106,11 @@ struct hrtimer {
  11205. enum hrtimer_restart (*function)(struct hrtimer *);
  11206. struct hrtimer_clock_base *base;
  11207. u8 state;
  11208. + struct list_head cb_entry;
  11209. + int irqsafe;
  11210. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  11211. + ktime_t praecox;
  11212. +#endif
  11213. u8 is_rel;
  11214. #ifdef CONFIG_TIMER_STATS
  11215. int start_pid;
  11216. @@ -123,11 +131,7 @@ struct hrtimer_sleeper {
  11217. struct task_struct *task;
  11218. };
  11219. -#ifdef CONFIG_64BIT
  11220. # define HRTIMER_CLOCK_BASE_ALIGN 64
  11221. -#else
  11222. -# define HRTIMER_CLOCK_BASE_ALIGN 32
  11223. -#endif
  11224. /**
  11225. * struct hrtimer_clock_base - the timer base for a specific clock
  11226. @@ -136,6 +140,7 @@ struct hrtimer_sleeper {
  11227. * timer to a base on another cpu.
  11228. * @clockid: clock id for per_cpu support
  11229. * @active: red black tree root node for the active timers
  11230. + * @expired: list head for deferred timers.
  11231. * @get_time: function to retrieve the current time of the clock
  11232. * @offset: offset of this clock to the monotonic base
  11233. */
  11234. @@ -144,6 +149,7 @@ struct hrtimer_clock_base {
  11235. int index;
  11236. clockid_t clockid;
  11237. struct timerqueue_head active;
  11238. + struct list_head expired;
  11239. ktime_t (*get_time)(void);
  11240. ktime_t offset;
  11241. } __attribute__((__aligned__(HRTIMER_CLOCK_BASE_ALIGN)));
  11242. @@ -187,6 +193,7 @@ struct hrtimer_cpu_base {
  11243. raw_spinlock_t lock;
  11244. seqcount_t seq;
  11245. struct hrtimer *running;
  11246. + struct hrtimer *running_soft;
  11247. unsigned int cpu;
  11248. unsigned int active_bases;
  11249. unsigned int clock_was_set_seq;
  11250. @@ -202,6 +209,9 @@ struct hrtimer_cpu_base {
  11251. unsigned int nr_retries;
  11252. unsigned int nr_hangs;
  11253. unsigned int max_hang_time;
  11254. +#endif
  11255. +#ifdef CONFIG_PREEMPT_RT_BASE
  11256. + wait_queue_head_t wait;
  11257. #endif
  11258. struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
  11259. } ____cacheline_aligned;
  11260. @@ -412,6 +422,13 @@ static inline void hrtimer_restart(struct hrtimer *timer)
  11261. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  11262. }
  11263. +/* Softirq preemption could deadlock timer removal */
  11264. +#ifdef CONFIG_PREEMPT_RT_BASE
  11265. + extern void hrtimer_wait_for_timer(const struct hrtimer *timer);
  11266. +#else
  11267. +# define hrtimer_wait_for_timer(timer) do { cpu_relax(); } while (0)
  11268. +#endif
  11269. +
  11270. /* Query timers: */
  11271. extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
  11272. @@ -436,9 +453,15 @@ static inline int hrtimer_is_queued(struct hrtimer *timer)
  11273. * Helper function to check, whether the timer is running the callback
  11274. * function
  11275. */
  11276. -static inline int hrtimer_callback_running(struct hrtimer *timer)
  11277. +static inline int hrtimer_callback_running(const struct hrtimer *timer)
  11278. {
  11279. - return timer->base->cpu_base->running == timer;
  11280. + if (timer->base->cpu_base->running == timer)
  11281. + return 1;
  11282. +#ifdef CONFIG_PREEMPT_RT_BASE
  11283. + if (timer->base->cpu_base->running_soft == timer)
  11284. + return 1;
  11285. +#endif
  11286. + return 0;
  11287. }
  11288. /* Forward a hrtimer so it expires after now: */
  11289. diff --git a/include/linux/idr.h b/include/linux/idr.h
  11290. index 013fd9bc4cb6..f62be0aec911 100644
  11291. --- a/include/linux/idr.h
  11292. +++ b/include/linux/idr.h
  11293. @@ -95,10 +95,14 @@ bool idr_is_empty(struct idr *idp);
  11294. * Each idr_preload() should be matched with an invocation of this
  11295. * function. See idr_preload() for details.
  11296. */
  11297. +#ifdef CONFIG_PREEMPT_RT_FULL
  11298. +void idr_preload_end(void);
  11299. +#else
  11300. static inline void idr_preload_end(void)
  11301. {
  11302. preempt_enable();
  11303. }
  11304. +#endif
  11305. /**
  11306. * idr_find - return pointer for given id
  11307. diff --git a/include/linux/init_task.h b/include/linux/init_task.h
  11308. index 1c1ff7e4faa4..60fadde71a44 100644
  11309. --- a/include/linux/init_task.h
  11310. +++ b/include/linux/init_task.h
  11311. @@ -148,9 +148,15 @@ extern struct task_group root_task_group;
  11312. # define INIT_PERF_EVENTS(tsk)
  11313. #endif
  11314. +#ifdef CONFIG_PREEMPT_RT_BASE
  11315. +# define INIT_TIMER_LIST .posix_timer_list = NULL,
  11316. +#else
  11317. +# define INIT_TIMER_LIST
  11318. +#endif
  11319. +
  11320. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  11321. # define INIT_VTIME(tsk) \
  11322. - .vtime_seqlock = __SEQLOCK_UNLOCKED(tsk.vtime_seqlock), \
  11323. + .vtime_seqcount = SEQCNT_ZERO(tsk.vtime_seqcount), \
  11324. .vtime_snap = 0, \
  11325. .vtime_snap_whence = VTIME_SYS,
  11326. #else
  11327. @@ -239,6 +245,7 @@ extern struct task_group root_task_group;
  11328. .cpu_timers = INIT_CPU_TIMERS(tsk.cpu_timers), \
  11329. .pi_lock = __RAW_SPIN_LOCK_UNLOCKED(tsk.pi_lock), \
  11330. .timer_slack_ns = 50000, /* 50 usec default slack */ \
  11331. + INIT_TIMER_LIST \
  11332. .pids = { \
  11333. [PIDTYPE_PID] = INIT_PID_LINK(PIDTYPE_PID), \
  11334. [PIDTYPE_PGID] = INIT_PID_LINK(PIDTYPE_PGID), \
  11335. diff --git a/include/linux/interrupt.h b/include/linux/interrupt.h
  11336. index ad16809c8596..655cee096aed 100644
  11337. --- a/include/linux/interrupt.h
  11338. +++ b/include/linux/interrupt.h
  11339. @@ -61,6 +61,7 @@
  11340. * interrupt handler after suspending interrupts. For system
  11341. * wakeup devices users need to implement wakeup detection in
  11342. * their interrupt handlers.
  11343. + * IRQF_NO_SOFTIRQ_CALL - Do not process softirqs in the irq thread context (RT)
  11344. */
  11345. #define IRQF_SHARED 0x00000080
  11346. #define IRQF_PROBE_SHARED 0x00000100
  11347. @@ -74,6 +75,7 @@
  11348. #define IRQF_NO_THREAD 0x00010000
  11349. #define IRQF_EARLY_RESUME 0x00020000
  11350. #define IRQF_COND_SUSPEND 0x00040000
  11351. +#define IRQF_NO_SOFTIRQ_CALL 0x00080000
  11352. #define IRQF_TIMER (__IRQF_TIMER | IRQF_NO_SUSPEND | IRQF_NO_THREAD)
  11353. @@ -186,7 +188,7 @@ extern void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id);
  11354. #ifdef CONFIG_LOCKDEP
  11355. # define local_irq_enable_in_hardirq() do { } while (0)
  11356. #else
  11357. -# define local_irq_enable_in_hardirq() local_irq_enable()
  11358. +# define local_irq_enable_in_hardirq() local_irq_enable_nort()
  11359. #endif
  11360. extern void disable_irq_nosync(unsigned int irq);
  11361. @@ -206,6 +208,7 @@ extern void resume_device_irqs(void);
  11362. * @irq: Interrupt to which notification applies
  11363. * @kref: Reference count, for internal use
  11364. * @work: Work item, for internal use
  11365. + * @list: List item for deferred callbacks
  11366. * @notify: Function to be called on change. This will be
  11367. * called in process context.
  11368. * @release: Function to be called on release. This will be
  11369. @@ -217,6 +220,7 @@ struct irq_affinity_notify {
  11370. unsigned int irq;
  11371. struct kref kref;
  11372. struct work_struct work;
  11373. + struct list_head list;
  11374. void (*notify)(struct irq_affinity_notify *, const cpumask_t *mask);
  11375. void (*release)(struct kref *ref);
  11376. };
  11377. @@ -379,9 +383,13 @@ extern int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
  11378. bool state);
  11379. #ifdef CONFIG_IRQ_FORCED_THREADING
  11380. +# ifndef CONFIG_PREEMPT_RT_BASE
  11381. extern bool force_irqthreads;
  11382. +# else
  11383. +# define force_irqthreads (true)
  11384. +# endif
  11385. #else
  11386. -#define force_irqthreads (0)
  11387. +#define force_irqthreads (false)
  11388. #endif
  11389. #ifndef __ARCH_SET_SOFTIRQ_PENDING
  11390. @@ -438,9 +446,10 @@ struct softirq_action
  11391. void (*action)(struct softirq_action *);
  11392. };
  11393. +#ifndef CONFIG_PREEMPT_RT_FULL
  11394. asmlinkage void do_softirq(void);
  11395. asmlinkage void __do_softirq(void);
  11396. -
  11397. +static inline void thread_do_softirq(void) { do_softirq(); }
  11398. #ifdef __ARCH_HAS_DO_SOFTIRQ
  11399. void do_softirq_own_stack(void);
  11400. #else
  11401. @@ -449,13 +458,25 @@ static inline void do_softirq_own_stack(void)
  11402. __do_softirq();
  11403. }
  11404. #endif
  11405. +#else
  11406. +extern void thread_do_softirq(void);
  11407. +#endif
  11408. extern void open_softirq(int nr, void (*action)(struct softirq_action *));
  11409. extern void softirq_init(void);
  11410. extern void __raise_softirq_irqoff(unsigned int nr);
  11411. +#ifdef CONFIG_PREEMPT_RT_FULL
  11412. +extern void __raise_softirq_irqoff_ksoft(unsigned int nr);
  11413. +#else
  11414. +static inline void __raise_softirq_irqoff_ksoft(unsigned int nr)
  11415. +{
  11416. + __raise_softirq_irqoff(nr);
  11417. +}
  11418. +#endif
  11419. extern void raise_softirq_irqoff(unsigned int nr);
  11420. extern void raise_softirq(unsigned int nr);
  11421. +extern void softirq_check_pending_idle(void);
  11422. DECLARE_PER_CPU(struct task_struct *, ksoftirqd);
  11423. @@ -477,8 +498,9 @@ static inline struct task_struct *this_cpu_ksoftirqd(void)
  11424. to be executed on some cpu at least once after this.
  11425. * If the tasklet is already scheduled, but its execution is still not
  11426. started, it will be executed only once.
  11427. - * If this tasklet is already running on another CPU (or schedule is called
  11428. - from tasklet itself), it is rescheduled for later.
  11429. + * If this tasklet is already running on another CPU, it is rescheduled
  11430. + for later.
  11431. + * Schedule must not be called from the tasklet itself (a lockup occurs)
  11432. * Tasklet is strictly serialized wrt itself, but not
  11433. wrt another tasklets. If client needs some intertask synchronization,
  11434. he makes it with spinlocks.
  11435. @@ -503,27 +525,36 @@ struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(1), func, data }
  11436. enum
  11437. {
  11438. TASKLET_STATE_SCHED, /* Tasklet is scheduled for execution */
  11439. - TASKLET_STATE_RUN /* Tasklet is running (SMP only) */
  11440. + TASKLET_STATE_RUN, /* Tasklet is running (SMP only) */
  11441. + TASKLET_STATE_PENDING /* Tasklet is pending */
  11442. };
  11443. -#ifdef CONFIG_SMP
  11444. +#define TASKLET_STATEF_SCHED (1 << TASKLET_STATE_SCHED)
  11445. +#define TASKLET_STATEF_RUN (1 << TASKLET_STATE_RUN)
  11446. +#define TASKLET_STATEF_PENDING (1 << TASKLET_STATE_PENDING)
  11447. +
  11448. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  11449. static inline int tasklet_trylock(struct tasklet_struct *t)
  11450. {
  11451. return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
  11452. }
  11453. +static inline int tasklet_tryunlock(struct tasklet_struct *t)
  11454. +{
  11455. + return cmpxchg(&t->state, TASKLET_STATEF_RUN, 0) == TASKLET_STATEF_RUN;
  11456. +}
  11457. +
  11458. static inline void tasklet_unlock(struct tasklet_struct *t)
  11459. {
  11460. smp_mb__before_atomic();
  11461. clear_bit(TASKLET_STATE_RUN, &(t)->state);
  11462. }
  11463. -static inline void tasklet_unlock_wait(struct tasklet_struct *t)
  11464. -{
  11465. - while (test_bit(TASKLET_STATE_RUN, &(t)->state)) { barrier(); }
  11466. -}
  11467. +extern void tasklet_unlock_wait(struct tasklet_struct *t);
  11468. +
  11469. #else
  11470. #define tasklet_trylock(t) 1
  11471. +#define tasklet_tryunlock(t) 1
  11472. #define tasklet_unlock_wait(t) do { } while (0)
  11473. #define tasklet_unlock(t) do { } while (0)
  11474. #endif
  11475. @@ -572,12 +603,7 @@ static inline void tasklet_disable(struct tasklet_struct *t)
  11476. smp_mb();
  11477. }
  11478. -static inline void tasklet_enable(struct tasklet_struct *t)
  11479. -{
  11480. - smp_mb__before_atomic();
  11481. - atomic_dec(&t->count);
  11482. -}
  11483. -
  11484. +extern void tasklet_enable(struct tasklet_struct *t);
  11485. extern void tasklet_kill(struct tasklet_struct *t);
  11486. extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
  11487. extern void tasklet_init(struct tasklet_struct *t,
  11488. @@ -608,6 +634,12 @@ void tasklet_hrtimer_cancel(struct tasklet_hrtimer *ttimer)
  11489. tasklet_kill(&ttimer->tasklet);
  11490. }
  11491. +#ifdef CONFIG_PREEMPT_RT_FULL
  11492. +extern void softirq_early_init(void);
  11493. +#else
  11494. +static inline void softirq_early_init(void) { }
  11495. +#endif
  11496. +
  11497. /*
  11498. * Autoprobing for irqs:
  11499. *
  11500. diff --git a/include/linux/irq.h b/include/linux/irq.h
  11501. index f7cade00c525..dac9e11ba037 100644
  11502. --- a/include/linux/irq.h
  11503. +++ b/include/linux/irq.h
  11504. @@ -72,6 +72,7 @@ enum irqchip_irq_state;
  11505. * IRQ_IS_POLLED - Always polled by another interrupt. Exclude
  11506. * it from the spurious interrupt detection
  11507. * mechanism and from core side polling.
  11508. + * IRQ_NO_SOFTIRQ_CALL - No softirq processing in the irq thread context (RT)
  11509. * IRQ_DISABLE_UNLAZY - Disable lazy irq disable
  11510. */
  11511. enum {
  11512. @@ -99,13 +100,14 @@ enum {
  11513. IRQ_PER_CPU_DEVID = (1 << 17),
  11514. IRQ_IS_POLLED = (1 << 18),
  11515. IRQ_DISABLE_UNLAZY = (1 << 19),
  11516. + IRQ_NO_SOFTIRQ_CALL = (1 << 20),
  11517. };
  11518. #define IRQF_MODIFY_MASK \
  11519. (IRQ_TYPE_SENSE_MASK | IRQ_NOPROBE | IRQ_NOREQUEST | \
  11520. IRQ_NOAUTOEN | IRQ_MOVE_PCNTXT | IRQ_LEVEL | IRQ_NO_BALANCING | \
  11521. IRQ_PER_CPU | IRQ_NESTED_THREAD | IRQ_NOTHREAD | IRQ_PER_CPU_DEVID | \
  11522. - IRQ_IS_POLLED | IRQ_DISABLE_UNLAZY)
  11523. + IRQ_IS_POLLED | IRQ_DISABLE_UNLAZY | IRQ_NO_SOFTIRQ_CALL)
  11524. #define IRQ_NO_BALANCING_MASK (IRQ_PER_CPU | IRQ_NO_BALANCING)
  11525. diff --git a/include/linux/irq_work.h b/include/linux/irq_work.h
  11526. index 47b9ebd4a74f..2543aab05daa 100644
  11527. --- a/include/linux/irq_work.h
  11528. +++ b/include/linux/irq_work.h
  11529. @@ -16,6 +16,7 @@
  11530. #define IRQ_WORK_BUSY 2UL
  11531. #define IRQ_WORK_FLAGS 3UL
  11532. #define IRQ_WORK_LAZY 4UL /* Doesn't want IPI, wait for tick */
  11533. +#define IRQ_WORK_HARD_IRQ 8UL /* Run hard IRQ context, even on RT */
  11534. struct irq_work {
  11535. unsigned long flags;
  11536. @@ -51,4 +52,10 @@ static inline bool irq_work_needs_cpu(void) { return false; }
  11537. static inline void irq_work_run(void) { }
  11538. #endif
  11539. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  11540. +void irq_work_tick_soft(void);
  11541. +#else
  11542. +static inline void irq_work_tick_soft(void) { }
  11543. +#endif
  11544. +
  11545. #endif /* _LINUX_IRQ_WORK_H */
  11546. diff --git a/include/linux/irqdesc.h b/include/linux/irqdesc.h
  11547. index a587a33363c7..ad57402a242d 100644
  11548. --- a/include/linux/irqdesc.h
  11549. +++ b/include/linux/irqdesc.h
  11550. @@ -61,6 +61,7 @@ struct irq_desc {
  11551. unsigned int irqs_unhandled;
  11552. atomic_t threads_handled;
  11553. int threads_handled_last;
  11554. + u64 random_ip;
  11555. raw_spinlock_t lock;
  11556. struct cpumask *percpu_enabled;
  11557. #ifdef CONFIG_SMP
  11558. diff --git a/include/linux/irqflags.h b/include/linux/irqflags.h
  11559. index 5dd1272d1ab2..9b77034f7c5e 100644
  11560. --- a/include/linux/irqflags.h
  11561. +++ b/include/linux/irqflags.h
  11562. @@ -25,8 +25,6 @@
  11563. # define trace_softirqs_enabled(p) ((p)->softirqs_enabled)
  11564. # define trace_hardirq_enter() do { current->hardirq_context++; } while (0)
  11565. # define trace_hardirq_exit() do { current->hardirq_context--; } while (0)
  11566. -# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  11567. -# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  11568. # define INIT_TRACE_IRQFLAGS .softirqs_enabled = 1,
  11569. #else
  11570. # define trace_hardirqs_on() do { } while (0)
  11571. @@ -39,9 +37,15 @@
  11572. # define trace_softirqs_enabled(p) 0
  11573. # define trace_hardirq_enter() do { } while (0)
  11574. # define trace_hardirq_exit() do { } while (0)
  11575. +# define INIT_TRACE_IRQFLAGS
  11576. +#endif
  11577. +
  11578. +#if defined(CONFIG_TRACE_IRQFLAGS) && !defined(CONFIG_PREEMPT_RT_FULL)
  11579. +# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  11580. +# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  11581. +#else
  11582. # define lockdep_softirq_enter() do { } while (0)
  11583. # define lockdep_softirq_exit() do { } while (0)
  11584. -# define INIT_TRACE_IRQFLAGS
  11585. #endif
  11586. #if defined(CONFIG_IRQSOFF_TRACER) || \
  11587. @@ -148,4 +152,23 @@
  11588. #define irqs_disabled_flags(flags) raw_irqs_disabled_flags(flags)
  11589. +/*
  11590. + * local_irq* variants depending on RT/!RT
  11591. + */
  11592. +#ifdef CONFIG_PREEMPT_RT_FULL
  11593. +# define local_irq_disable_nort() do { } while (0)
  11594. +# define local_irq_enable_nort() do { } while (0)
  11595. +# define local_irq_save_nort(flags) local_save_flags(flags)
  11596. +# define local_irq_restore_nort(flags) (void)(flags)
  11597. +# define local_irq_disable_rt() local_irq_disable()
  11598. +# define local_irq_enable_rt() local_irq_enable()
  11599. +#else
  11600. +# define local_irq_disable_nort() local_irq_disable()
  11601. +# define local_irq_enable_nort() local_irq_enable()
  11602. +# define local_irq_save_nort(flags) local_irq_save(flags)
  11603. +# define local_irq_restore_nort(flags) local_irq_restore(flags)
  11604. +# define local_irq_disable_rt() do { } while (0)
  11605. +# define local_irq_enable_rt() do { } while (0)
  11606. +#endif
  11607. +
  11608. #endif
  11609. diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
  11610. index 65407f6c9120..eb5aabe4e18c 100644
  11611. --- a/include/linux/jbd2.h
  11612. +++ b/include/linux/jbd2.h
  11613. @@ -352,32 +352,56 @@ static inline struct journal_head *bh2jh(struct buffer_head *bh)
  11614. static inline void jbd_lock_bh_state(struct buffer_head *bh)
  11615. {
  11616. +#ifndef CONFIG_PREEMPT_RT_BASE
  11617. bit_spin_lock(BH_State, &bh->b_state);
  11618. +#else
  11619. + spin_lock(&bh->b_state_lock);
  11620. +#endif
  11621. }
  11622. static inline int jbd_trylock_bh_state(struct buffer_head *bh)
  11623. {
  11624. +#ifndef CONFIG_PREEMPT_RT_BASE
  11625. return bit_spin_trylock(BH_State, &bh->b_state);
  11626. +#else
  11627. + return spin_trylock(&bh->b_state_lock);
  11628. +#endif
  11629. }
  11630. static inline int jbd_is_locked_bh_state(struct buffer_head *bh)
  11631. {
  11632. +#ifndef CONFIG_PREEMPT_RT_BASE
  11633. return bit_spin_is_locked(BH_State, &bh->b_state);
  11634. +#else
  11635. + return spin_is_locked(&bh->b_state_lock);
  11636. +#endif
  11637. }
  11638. static inline void jbd_unlock_bh_state(struct buffer_head *bh)
  11639. {
  11640. +#ifndef CONFIG_PREEMPT_RT_BASE
  11641. bit_spin_unlock(BH_State, &bh->b_state);
  11642. +#else
  11643. + spin_unlock(&bh->b_state_lock);
  11644. +#endif
  11645. }
  11646. static inline void jbd_lock_bh_journal_head(struct buffer_head *bh)
  11647. {
  11648. +#ifndef CONFIG_PREEMPT_RT_BASE
  11649. bit_spin_lock(BH_JournalHead, &bh->b_state);
  11650. +#else
  11651. + spin_lock(&bh->b_journal_head_lock);
  11652. +#endif
  11653. }
  11654. static inline void jbd_unlock_bh_journal_head(struct buffer_head *bh)
  11655. {
  11656. +#ifndef CONFIG_PREEMPT_RT_BASE
  11657. bit_spin_unlock(BH_JournalHead, &bh->b_state);
  11658. +#else
  11659. + spin_unlock(&bh->b_journal_head_lock);
  11660. +#endif
  11661. }
  11662. #define J_ASSERT(assert) BUG_ON(!(assert))
  11663. diff --git a/include/linux/kdb.h b/include/linux/kdb.h
  11664. index a19bcf9e762e..897495386446 100644
  11665. --- a/include/linux/kdb.h
  11666. +++ b/include/linux/kdb.h
  11667. @@ -167,6 +167,7 @@ extern __printf(2, 0) int vkdb_printf(enum kdb_msgsrc src, const char *fmt,
  11668. extern __printf(1, 2) int kdb_printf(const char *, ...);
  11669. typedef __printf(1, 2) int (*kdb_printf_t)(const char *, ...);
  11670. +#define in_kdb_printk() (kdb_trap_printk)
  11671. extern void kdb_init(int level);
  11672. /* Access to kdb specific polling devices */
  11673. @@ -201,6 +202,7 @@ extern int kdb_register_flags(char *, kdb_func_t, char *, char *,
  11674. extern int kdb_unregister(char *);
  11675. #else /* ! CONFIG_KGDB_KDB */
  11676. static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }
  11677. +#define in_kdb_printk() (0)
  11678. static inline void kdb_init(int level) {}
  11679. static inline int kdb_register(char *cmd, kdb_func_t func, char *usage,
  11680. char *help, short minlen) { return 0; }
  11681. diff --git a/include/linux/kernel.h b/include/linux/kernel.h
  11682. index 50220cab738c..d68f639f7330 100644
  11683. --- a/include/linux/kernel.h
  11684. +++ b/include/linux/kernel.h
  11685. @@ -188,6 +188,9 @@ extern int _cond_resched(void);
  11686. */
  11687. # define might_sleep() \
  11688. do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  11689. +
  11690. +# define might_sleep_no_state_check() \
  11691. + do { ___might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  11692. # define sched_annotate_sleep() (current->task_state_change = 0)
  11693. #else
  11694. static inline void ___might_sleep(const char *file, int line,
  11695. @@ -195,6 +198,7 @@ extern int _cond_resched(void);
  11696. static inline void __might_sleep(const char *file, int line,
  11697. int preempt_offset) { }
  11698. # define might_sleep() do { might_resched(); } while (0)
  11699. +# define might_sleep_no_state_check() do { might_resched(); } while (0)
  11700. # define sched_annotate_sleep() do { } while (0)
  11701. #endif
  11702. @@ -255,6 +259,7 @@ extern long (*panic_blink)(int state);
  11703. __printf(1, 2)
  11704. void panic(const char *fmt, ...)
  11705. __noreturn __cold;
  11706. +void nmi_panic(struct pt_regs *regs, const char *msg);
  11707. extern void oops_enter(void);
  11708. extern void oops_exit(void);
  11709. void print_oops_end_marker(void);
  11710. @@ -447,6 +452,14 @@ extern int sysctl_panic_on_stackoverflow;
  11711. extern bool crash_kexec_post_notifiers;
  11712. +/*
  11713. + * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It
  11714. + * holds a CPU number which is executing panic() currently. A value of
  11715. + * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec().
  11716. + */
  11717. +extern atomic_t panic_cpu;
  11718. +#define PANIC_CPU_INVALID -1
  11719. +
  11720. /*
  11721. * Only to be used by arch init code. If the user over-wrote the default
  11722. * CONFIG_PANIC_TIMEOUT, honor it.
  11723. @@ -475,6 +488,7 @@ extern enum system_states {
  11724. SYSTEM_HALT,
  11725. SYSTEM_POWER_OFF,
  11726. SYSTEM_RESTART,
  11727. + SYSTEM_SUSPEND,
  11728. } system_state;
  11729. #define TAINT_PROPRIETARY_MODULE 0
  11730. diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
  11731. index d7ce4e3280db..0b6d392b38e7 100644
  11732. --- a/include/linux/kvm_host.h
  11733. +++ b/include/linux/kvm_host.h
  11734. @@ -25,6 +25,7 @@
  11735. #include <linux/irqflags.h>
  11736. #include <linux/context_tracking.h>
  11737. #include <linux/irqbypass.h>
  11738. +#include <linux/swait.h>
  11739. #include <asm/signal.h>
  11740. #include <linux/kvm.h>
  11741. @@ -243,7 +244,7 @@ struct kvm_vcpu {
  11742. int fpu_active;
  11743. int guest_fpu_loaded, guest_xcr0_loaded;
  11744. unsigned char fpu_counter;
  11745. - wait_queue_head_t wq;
  11746. + struct swait_queue_head wq;
  11747. struct pid *pid;
  11748. int sigset_active;
  11749. sigset_t sigset;
  11750. @@ -794,7 +795,7 @@ static inline bool kvm_arch_has_assigned_device(struct kvm *kvm)
  11751. }
  11752. #endif
  11753. -static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  11754. +static inline struct swait_queue_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  11755. {
  11756. #ifdef __KVM_HAVE_ARCH_WQP
  11757. return vcpu->arch.wqp;
  11758. diff --git a/include/linux/lglock.h b/include/linux/lglock.h
  11759. index c92ebd100d9b..6f035f635d0e 100644
  11760. --- a/include/linux/lglock.h
  11761. +++ b/include/linux/lglock.h
  11762. @@ -34,13 +34,30 @@
  11763. #endif
  11764. struct lglock {
  11765. +#ifdef CONFIG_PREEMPT_RT_FULL
  11766. + struct rt_mutex __percpu *lock;
  11767. +#else
  11768. arch_spinlock_t __percpu *lock;
  11769. +#endif
  11770. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  11771. struct lock_class_key lock_key;
  11772. struct lockdep_map lock_dep_map;
  11773. #endif
  11774. };
  11775. +#ifdef CONFIG_PREEMPT_RT_FULL
  11776. +# define DEFINE_LGLOCK(name) \
  11777. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  11778. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  11779. + struct lglock name = { .lock = &name ## _lock }
  11780. +
  11781. +# define DEFINE_STATIC_LGLOCK(name) \
  11782. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  11783. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  11784. + static struct lglock name = { .lock = &name ## _lock }
  11785. +
  11786. +#else
  11787. +
  11788. #define DEFINE_LGLOCK(name) \
  11789. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  11790. = __ARCH_SPIN_LOCK_UNLOCKED; \
  11791. @@ -50,6 +67,7 @@ struct lglock {
  11792. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  11793. = __ARCH_SPIN_LOCK_UNLOCKED; \
  11794. static struct lglock name = { .lock = &name ## _lock }
  11795. +#endif
  11796. void lg_lock_init(struct lglock *lg, char *name);
  11797. @@ -64,6 +82,12 @@ void lg_double_unlock(struct lglock *lg, int cpu1, int cpu2);
  11798. void lg_global_lock(struct lglock *lg);
  11799. void lg_global_unlock(struct lglock *lg);
  11800. +#ifndef CONFIG_PREEMPT_RT_FULL
  11801. +#define lg_global_trylock_relax(name) lg_global_lock(name)
  11802. +#else
  11803. +void lg_global_trylock_relax(struct lglock *lg);
  11804. +#endif
  11805. +
  11806. #else
  11807. /* When !CONFIG_SMP, map lglock to spinlock */
  11808. #define lglock spinlock
  11809. diff --git a/include/linux/list_bl.h b/include/linux/list_bl.h
  11810. index 8132214e8efd..89ffaa7bd342 100644
  11811. --- a/include/linux/list_bl.h
  11812. +++ b/include/linux/list_bl.h
  11813. @@ -2,6 +2,7 @@
  11814. #define _LINUX_LIST_BL_H
  11815. #include <linux/list.h>
  11816. +#include <linux/spinlock.h>
  11817. #include <linux/bit_spinlock.h>
  11818. /*
  11819. @@ -32,13 +33,24 @@
  11820. struct hlist_bl_head {
  11821. struct hlist_bl_node *first;
  11822. +#ifdef CONFIG_PREEMPT_RT_BASE
  11823. + raw_spinlock_t lock;
  11824. +#endif
  11825. };
  11826. struct hlist_bl_node {
  11827. struct hlist_bl_node *next, **pprev;
  11828. };
  11829. -#define INIT_HLIST_BL_HEAD(ptr) \
  11830. - ((ptr)->first = NULL)
  11831. +
  11832. +#ifdef CONFIG_PREEMPT_RT_BASE
  11833. +#define INIT_HLIST_BL_HEAD(h) \
  11834. +do { \
  11835. + (h)->first = NULL; \
  11836. + raw_spin_lock_init(&(h)->lock); \
  11837. +} while (0)
  11838. +#else
  11839. +#define INIT_HLIST_BL_HEAD(h) (h)->first = NULL
  11840. +#endif
  11841. static inline void INIT_HLIST_BL_NODE(struct hlist_bl_node *h)
  11842. {
  11843. @@ -118,12 +130,26 @@ static inline void hlist_bl_del_init(struct hlist_bl_node *n)
  11844. static inline void hlist_bl_lock(struct hlist_bl_head *b)
  11845. {
  11846. +#ifndef CONFIG_PREEMPT_RT_BASE
  11847. bit_spin_lock(0, (unsigned long *)b);
  11848. +#else
  11849. + raw_spin_lock(&b->lock);
  11850. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  11851. + __set_bit(0, (unsigned long *)b);
  11852. +#endif
  11853. +#endif
  11854. }
  11855. static inline void hlist_bl_unlock(struct hlist_bl_head *b)
  11856. {
  11857. +#ifndef CONFIG_PREEMPT_RT_BASE
  11858. __bit_spin_unlock(0, (unsigned long *)b);
  11859. +#else
  11860. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  11861. + __clear_bit(0, (unsigned long *)b);
  11862. +#endif
  11863. + raw_spin_unlock(&b->lock);
  11864. +#endif
  11865. }
  11866. static inline bool hlist_bl_is_locked(struct hlist_bl_head *b)
  11867. diff --git a/include/linux/locallock.h b/include/linux/locallock.h
  11868. new file mode 100644
  11869. index 000000000000..0baaf28dc4ee
  11870. --- /dev/null
  11871. +++ b/include/linux/locallock.h
  11872. @@ -0,0 +1,285 @@
  11873. +#ifndef _LINUX_LOCALLOCK_H
  11874. +#define _LINUX_LOCALLOCK_H
  11875. +
  11876. +#include <linux/percpu.h>
  11877. +#include <linux/spinlock.h>
  11878. +
  11879. +#ifdef CONFIG_PREEMPT_RT_BASE
  11880. +
  11881. +#ifdef CONFIG_DEBUG_SPINLOCK
  11882. +# define LL_WARN(cond) WARN_ON(cond)
  11883. +#else
  11884. +# define LL_WARN(cond) do { } while (0)
  11885. +#endif
  11886. +
  11887. +/*
  11888. + * per cpu lock based substitute for local_irq_*()
  11889. + */
  11890. +struct local_irq_lock {
  11891. + spinlock_t lock;
  11892. + struct task_struct *owner;
  11893. + int nestcnt;
  11894. + unsigned long flags;
  11895. +};
  11896. +
  11897. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) \
  11898. + DEFINE_PER_CPU(struct local_irq_lock, lvar) = { \
  11899. + .lock = __SPIN_LOCK_UNLOCKED((lvar).lock) }
  11900. +
  11901. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) \
  11902. + DECLARE_PER_CPU(struct local_irq_lock, lvar)
  11903. +
  11904. +#define local_irq_lock_init(lvar) \
  11905. + do { \
  11906. + int __cpu; \
  11907. + for_each_possible_cpu(__cpu) \
  11908. + spin_lock_init(&per_cpu(lvar, __cpu).lock); \
  11909. + } while (0)
  11910. +
  11911. +/*
  11912. + * spin_lock|trylock|unlock_local flavour that does not migrate disable
  11913. + * used for __local_lock|trylock|unlock where get_local_var/put_local_var
  11914. + * already takes care of the migrate_disable/enable
  11915. + * for CONFIG_PREEMPT_BASE map to the normal spin_* calls.
  11916. + */
  11917. +#ifdef CONFIG_PREEMPT_RT_FULL
  11918. +# define spin_lock_local(lock) rt_spin_lock__no_mg(lock)
  11919. +# define spin_trylock_local(lock) rt_spin_trylock__no_mg(lock)
  11920. +# define spin_unlock_local(lock) rt_spin_unlock__no_mg(lock)
  11921. +#else
  11922. +# define spin_lock_local(lock) spin_lock(lock)
  11923. +# define spin_trylock_local(lock) spin_trylock(lock)
  11924. +# define spin_unlock_local(lock) spin_unlock(lock)
  11925. +#endif
  11926. +
  11927. +static inline void __local_lock(struct local_irq_lock *lv)
  11928. +{
  11929. + if (lv->owner != current) {
  11930. + spin_lock_local(&lv->lock);
  11931. + LL_WARN(lv->owner);
  11932. + LL_WARN(lv->nestcnt);
  11933. + lv->owner = current;
  11934. + }
  11935. + lv->nestcnt++;
  11936. +}
  11937. +
  11938. +#define local_lock(lvar) \
  11939. + do { __local_lock(&get_local_var(lvar)); } while (0)
  11940. +
  11941. +#define local_lock_on(lvar, cpu) \
  11942. + do { __local_lock(&per_cpu(lvar, cpu)); } while (0)
  11943. +
  11944. +static inline int __local_trylock(struct local_irq_lock *lv)
  11945. +{
  11946. + if (lv->owner != current && spin_trylock_local(&lv->lock)) {
  11947. + LL_WARN(lv->owner);
  11948. + LL_WARN(lv->nestcnt);
  11949. + lv->owner = current;
  11950. + lv->nestcnt = 1;
  11951. + return 1;
  11952. + } else if (lv->owner == current) {
  11953. + lv->nestcnt++;
  11954. + return 1;
  11955. + }
  11956. + return 0;
  11957. +}
  11958. +
  11959. +#define local_trylock(lvar) \
  11960. + ({ \
  11961. + int __locked; \
  11962. + __locked = __local_trylock(&get_local_var(lvar)); \
  11963. + if (!__locked) \
  11964. + put_local_var(lvar); \
  11965. + __locked; \
  11966. + })
  11967. +
  11968. +static inline void __local_unlock(struct local_irq_lock *lv)
  11969. +{
  11970. + LL_WARN(lv->nestcnt == 0);
  11971. + LL_WARN(lv->owner != current);
  11972. + if (--lv->nestcnt)
  11973. + return;
  11974. +
  11975. + lv->owner = NULL;
  11976. + spin_unlock_local(&lv->lock);
  11977. +}
  11978. +
  11979. +#define local_unlock(lvar) \
  11980. + do { \
  11981. + __local_unlock(this_cpu_ptr(&lvar)); \
  11982. + put_local_var(lvar); \
  11983. + } while (0)
  11984. +
  11985. +#define local_unlock_on(lvar, cpu) \
  11986. + do { __local_unlock(&per_cpu(lvar, cpu)); } while (0)
  11987. +
  11988. +static inline void __local_lock_irq(struct local_irq_lock *lv)
  11989. +{
  11990. + spin_lock_irqsave(&lv->lock, lv->flags);
  11991. + LL_WARN(lv->owner);
  11992. + LL_WARN(lv->nestcnt);
  11993. + lv->owner = current;
  11994. + lv->nestcnt = 1;
  11995. +}
  11996. +
  11997. +#define local_lock_irq(lvar) \
  11998. + do { __local_lock_irq(&get_local_var(lvar)); } while (0)
  11999. +
  12000. +#define local_lock_irq_on(lvar, cpu) \
  12001. + do { __local_lock_irq(&per_cpu(lvar, cpu)); } while (0)
  12002. +
  12003. +static inline void __local_unlock_irq(struct local_irq_lock *lv)
  12004. +{
  12005. + LL_WARN(!lv->nestcnt);
  12006. + LL_WARN(lv->owner != current);
  12007. + lv->owner = NULL;
  12008. + lv->nestcnt = 0;
  12009. + spin_unlock_irq(&lv->lock);
  12010. +}
  12011. +
  12012. +#define local_unlock_irq(lvar) \
  12013. + do { \
  12014. + __local_unlock_irq(this_cpu_ptr(&lvar)); \
  12015. + put_local_var(lvar); \
  12016. + } while (0)
  12017. +
  12018. +#define local_unlock_irq_on(lvar, cpu) \
  12019. + do { \
  12020. + __local_unlock_irq(&per_cpu(lvar, cpu)); \
  12021. + } while (0)
  12022. +
  12023. +static inline int __local_lock_irqsave(struct local_irq_lock *lv)
  12024. +{
  12025. + if (lv->owner != current) {
  12026. + __local_lock_irq(lv);
  12027. + return 0;
  12028. + } else {
  12029. + lv->nestcnt++;
  12030. + return 1;
  12031. + }
  12032. +}
  12033. +
  12034. +#define local_lock_irqsave(lvar, _flags) \
  12035. + do { \
  12036. + if (__local_lock_irqsave(&get_local_var(lvar))) \
  12037. + put_local_var(lvar); \
  12038. + _flags = __this_cpu_read(lvar.flags); \
  12039. + } while (0)
  12040. +
  12041. +#define local_lock_irqsave_on(lvar, _flags, cpu) \
  12042. + do { \
  12043. + __local_lock_irqsave(&per_cpu(lvar, cpu)); \
  12044. + _flags = per_cpu(lvar, cpu).flags; \
  12045. + } while (0)
  12046. +
  12047. +static inline int __local_unlock_irqrestore(struct local_irq_lock *lv,
  12048. + unsigned long flags)
  12049. +{
  12050. + LL_WARN(!lv->nestcnt);
  12051. + LL_WARN(lv->owner != current);
  12052. + if (--lv->nestcnt)
  12053. + return 0;
  12054. +
  12055. + lv->owner = NULL;
  12056. + spin_unlock_irqrestore(&lv->lock, lv->flags);
  12057. + return 1;
  12058. +}
  12059. +
  12060. +#define local_unlock_irqrestore(lvar, flags) \
  12061. + do { \
  12062. + if (__local_unlock_irqrestore(this_cpu_ptr(&lvar), flags)) \
  12063. + put_local_var(lvar); \
  12064. + } while (0)
  12065. +
  12066. +#define local_unlock_irqrestore_on(lvar, flags, cpu) \
  12067. + do { \
  12068. + __local_unlock_irqrestore(&per_cpu(lvar, cpu), flags); \
  12069. + } while (0)
  12070. +
  12071. +#define local_spin_trylock_irq(lvar, lock) \
  12072. + ({ \
  12073. + int __locked; \
  12074. + local_lock_irq(lvar); \
  12075. + __locked = spin_trylock(lock); \
  12076. + if (!__locked) \
  12077. + local_unlock_irq(lvar); \
  12078. + __locked; \
  12079. + })
  12080. +
  12081. +#define local_spin_lock_irq(lvar, lock) \
  12082. + do { \
  12083. + local_lock_irq(lvar); \
  12084. + spin_lock(lock); \
  12085. + } while (0)
  12086. +
  12087. +#define local_spin_unlock_irq(lvar, lock) \
  12088. + do { \
  12089. + spin_unlock(lock); \
  12090. + local_unlock_irq(lvar); \
  12091. + } while (0)
  12092. +
  12093. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  12094. + do { \
  12095. + local_lock_irqsave(lvar, flags); \
  12096. + spin_lock(lock); \
  12097. + } while (0)
  12098. +
  12099. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  12100. + do { \
  12101. + spin_unlock(lock); \
  12102. + local_unlock_irqrestore(lvar, flags); \
  12103. + } while (0)
  12104. +
  12105. +#define get_locked_var(lvar, var) \
  12106. + (*({ \
  12107. + local_lock(lvar); \
  12108. + this_cpu_ptr(&var); \
  12109. + }))
  12110. +
  12111. +#define put_locked_var(lvar, var) local_unlock(lvar);
  12112. +
  12113. +#define local_lock_cpu(lvar) \
  12114. + ({ \
  12115. + local_lock(lvar); \
  12116. + smp_processor_id(); \
  12117. + })
  12118. +
  12119. +#define local_unlock_cpu(lvar) local_unlock(lvar)
  12120. +
  12121. +#else /* PREEMPT_RT_BASE */
  12122. +
  12123. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) __typeof__(const int) lvar
  12124. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) extern __typeof__(const int) lvar
  12125. +
  12126. +static inline void local_irq_lock_init(int lvar) { }
  12127. +
  12128. +#define local_trylock(lvar) \
  12129. + ({ \
  12130. + preempt_disable(); \
  12131. + 1; \
  12132. + })
  12133. +
  12134. +#define local_lock(lvar) preempt_disable()
  12135. +#define local_unlock(lvar) preempt_enable()
  12136. +#define local_lock_irq(lvar) local_irq_disable()
  12137. +#define local_unlock_irq(lvar) local_irq_enable()
  12138. +#define local_lock_irqsave(lvar, flags) local_irq_save(flags)
  12139. +#define local_unlock_irqrestore(lvar, flags) local_irq_restore(flags)
  12140. +
  12141. +#define local_spin_trylock_irq(lvar, lock) spin_trylock_irq(lock)
  12142. +#define local_spin_lock_irq(lvar, lock) spin_lock_irq(lock)
  12143. +#define local_spin_unlock_irq(lvar, lock) spin_unlock_irq(lock)
  12144. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  12145. + spin_lock_irqsave(lock, flags)
  12146. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  12147. + spin_unlock_irqrestore(lock, flags)
  12148. +
  12149. +#define get_locked_var(lvar, var) get_cpu_var(var)
  12150. +#define put_locked_var(lvar, var) put_cpu_var(var)
  12151. +
  12152. +#define local_lock_cpu(lvar) get_cpu()
  12153. +#define local_unlock_cpu(lvar) put_cpu()
  12154. +
  12155. +#endif
  12156. +
  12157. +#endif
  12158. diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
  12159. index 36f4695aa604..2be9cca8be08 100644
  12160. --- a/include/linux/mm_types.h
  12161. +++ b/include/linux/mm_types.h
  12162. @@ -11,6 +11,7 @@
  12163. #include <linux/completion.h>
  12164. #include <linux/cpumask.h>
  12165. #include <linux/uprobes.h>
  12166. +#include <linux/rcupdate.h>
  12167. #include <linux/page-flags-layout.h>
  12168. #include <asm/page.h>
  12169. #include <asm/mmu.h>
  12170. @@ -509,6 +510,9 @@ struct mm_struct {
  12171. bool tlb_flush_batched;
  12172. #endif
  12173. struct uprobes_state uprobes_state;
  12174. +#ifdef CONFIG_PREEMPT_RT_BASE
  12175. + struct rcu_head delayed_drop;
  12176. +#endif
  12177. #ifdef CONFIG_X86_INTEL_MPX
  12178. /* address of the bounds directory */
  12179. void __user *bd_addr;
  12180. diff --git a/include/linux/module.h b/include/linux/module.h
  12181. index b229a9961d02..5fea847cf95c 100644
  12182. --- a/include/linux/module.h
  12183. +++ b/include/linux/module.h
  12184. @@ -500,6 +500,7 @@ static inline int module_is_live(struct module *mod)
  12185. struct module *__module_text_address(unsigned long addr);
  12186. struct module *__module_address(unsigned long addr);
  12187. bool is_module_address(unsigned long addr);
  12188. +bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr);
  12189. bool is_module_percpu_address(unsigned long addr);
  12190. bool is_module_text_address(unsigned long addr);
  12191. @@ -665,6 +666,11 @@ static inline bool is_module_percpu_address(unsigned long addr)
  12192. return false;
  12193. }
  12194. +static inline bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
  12195. +{
  12196. + return false;
  12197. +}
  12198. +
  12199. static inline bool is_module_text_address(unsigned long addr)
  12200. {
  12201. return false;
  12202. diff --git a/include/linux/mutex.h b/include/linux/mutex.h
  12203. index 2cb7531e7d7a..b3fdfc820216 100644
  12204. --- a/include/linux/mutex.h
  12205. +++ b/include/linux/mutex.h
  12206. @@ -19,6 +19,17 @@
  12207. #include <asm/processor.h>
  12208. #include <linux/osq_lock.h>
  12209. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12210. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  12211. + , .dep_map = { .name = #lockname }
  12212. +#else
  12213. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  12214. +#endif
  12215. +
  12216. +#ifdef CONFIG_PREEMPT_RT_FULL
  12217. +# include <linux/mutex_rt.h>
  12218. +#else
  12219. +
  12220. /*
  12221. * Simple, straightforward mutexes with strict semantics:
  12222. *
  12223. @@ -99,13 +110,6 @@ do { \
  12224. static inline void mutex_destroy(struct mutex *lock) {}
  12225. #endif
  12226. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12227. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  12228. - , .dep_map = { .name = #lockname }
  12229. -#else
  12230. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  12231. -#endif
  12232. -
  12233. #define __MUTEX_INITIALIZER(lockname) \
  12234. { .count = ATOMIC_INIT(1) \
  12235. , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  12236. @@ -173,6 +177,8 @@ extern int __must_check mutex_lock_killable(struct mutex *lock);
  12237. extern int mutex_trylock(struct mutex *lock);
  12238. extern void mutex_unlock(struct mutex *lock);
  12239. +#endif /* !PREEMPT_RT_FULL */
  12240. +
  12241. extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
  12242. #endif /* __LINUX_MUTEX_H */
  12243. diff --git a/include/linux/mutex_rt.h b/include/linux/mutex_rt.h
  12244. new file mode 100644
  12245. index 000000000000..e0284edec655
  12246. --- /dev/null
  12247. +++ b/include/linux/mutex_rt.h
  12248. @@ -0,0 +1,89 @@
  12249. +#ifndef __LINUX_MUTEX_RT_H
  12250. +#define __LINUX_MUTEX_RT_H
  12251. +
  12252. +#ifndef __LINUX_MUTEX_H
  12253. +#error "Please include mutex.h"
  12254. +#endif
  12255. +
  12256. +#include <linux/rtmutex.h>
  12257. +
  12258. +/* FIXME: Just for __lockfunc */
  12259. +#include <linux/spinlock.h>
  12260. +
  12261. +struct mutex {
  12262. + struct rt_mutex lock;
  12263. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12264. + struct lockdep_map dep_map;
  12265. +#endif
  12266. +};
  12267. +
  12268. +#define __MUTEX_INITIALIZER(mutexname) \
  12269. + { \
  12270. + .lock = __RT_MUTEX_INITIALIZER(mutexname.lock) \
  12271. + __DEP_MAP_MUTEX_INITIALIZER(mutexname) \
  12272. + }
  12273. +
  12274. +#define DEFINE_MUTEX(mutexname) \
  12275. + struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  12276. +
  12277. +extern void __mutex_do_init(struct mutex *lock, const char *name, struct lock_class_key *key);
  12278. +extern void __lockfunc _mutex_lock(struct mutex *lock);
  12279. +extern int __lockfunc _mutex_lock_interruptible(struct mutex *lock);
  12280. +extern int __lockfunc _mutex_lock_killable(struct mutex *lock);
  12281. +extern void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass);
  12282. +extern void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
  12283. +extern int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass);
  12284. +extern int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass);
  12285. +extern int __lockfunc _mutex_trylock(struct mutex *lock);
  12286. +extern void __lockfunc _mutex_unlock(struct mutex *lock);
  12287. +
  12288. +#define mutex_is_locked(l) rt_mutex_is_locked(&(l)->lock)
  12289. +#define mutex_lock(l) _mutex_lock(l)
  12290. +#define mutex_lock_interruptible(l) _mutex_lock_interruptible(l)
  12291. +#define mutex_lock_killable(l) _mutex_lock_killable(l)
  12292. +#define mutex_trylock(l) _mutex_trylock(l)
  12293. +#define mutex_unlock(l) _mutex_unlock(l)
  12294. +
  12295. +#ifdef CONFIG_DEBUG_MUTEXES
  12296. +#define mutex_destroy(l) rt_mutex_destroy(&(l)->lock)
  12297. +#else
  12298. +static inline void mutex_destroy(struct mutex *lock) {}
  12299. +#endif
  12300. +
  12301. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12302. +# define mutex_lock_nested(l, s) _mutex_lock_nested(l, s)
  12303. +# define mutex_lock_interruptible_nested(l, s) \
  12304. + _mutex_lock_interruptible_nested(l, s)
  12305. +# define mutex_lock_killable_nested(l, s) \
  12306. + _mutex_lock_killable_nested(l, s)
  12307. +
  12308. +# define mutex_lock_nest_lock(lock, nest_lock) \
  12309. +do { \
  12310. + typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  12311. + _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  12312. +} while (0)
  12313. +
  12314. +#else
  12315. +# define mutex_lock_nested(l, s) _mutex_lock(l)
  12316. +# define mutex_lock_interruptible_nested(l, s) \
  12317. + _mutex_lock_interruptible(l)
  12318. +# define mutex_lock_killable_nested(l, s) \
  12319. + _mutex_lock_killable(l)
  12320. +# define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
  12321. +#endif
  12322. +
  12323. +# define mutex_init(mutex) \
  12324. +do { \
  12325. + static struct lock_class_key __key; \
  12326. + \
  12327. + rt_mutex_init(&(mutex)->lock); \
  12328. + __mutex_do_init((mutex), #mutex, &__key); \
  12329. +} while (0)
  12330. +
  12331. +# define __mutex_init(mutex, name, key) \
  12332. +do { \
  12333. + rt_mutex_init(&(mutex)->lock); \
  12334. + __mutex_do_init((mutex), name, key); \
  12335. +} while (0)
  12336. +
  12337. +#endif
  12338. diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
  12339. index fc54049e8286..5a5588a57cad 100644
  12340. --- a/include/linux/netdevice.h
  12341. +++ b/include/linux/netdevice.h
  12342. @@ -390,7 +390,19 @@ typedef enum rx_handler_result rx_handler_result_t;
  12343. typedef rx_handler_result_t rx_handler_func_t(struct sk_buff **pskb);
  12344. void __napi_schedule(struct napi_struct *n);
  12345. +
  12346. +/*
  12347. + * When PREEMPT_RT_FULL is defined, all device interrupt handlers
  12348. + * run as threads, and they can also be preempted (without PREEMPT_RT
  12349. + * interrupt threads can not be preempted). Which means that calling
  12350. + * __napi_schedule_irqoff() from an interrupt handler can be preempted
  12351. + * and can corrupt the napi->poll_list.
  12352. + */
  12353. +#ifdef CONFIG_PREEMPT_RT_FULL
  12354. +#define __napi_schedule_irqoff(n) __napi_schedule(n)
  12355. +#else
  12356. void __napi_schedule_irqoff(struct napi_struct *n);
  12357. +#endif
  12358. static inline bool napi_disable_pending(struct napi_struct *n)
  12359. {
  12360. @@ -2288,11 +2300,20 @@ void netdev_freemem(struct net_device *dev);
  12361. void synchronize_net(void);
  12362. int init_dummy_netdev(struct net_device *dev);
  12363. +#ifdef CONFIG_PREEMPT_RT_FULL
  12364. +static inline int dev_recursion_level(void)
  12365. +{
  12366. + return current->xmit_recursion;
  12367. +}
  12368. +
  12369. +#else
  12370. +
  12371. DECLARE_PER_CPU(int, xmit_recursion);
  12372. static inline int dev_recursion_level(void)
  12373. {
  12374. return this_cpu_read(xmit_recursion);
  12375. }
  12376. +#endif
  12377. struct net_device *dev_get_by_index(struct net *net, int ifindex);
  12378. struct net_device *__dev_get_by_index(struct net *net, int ifindex);
  12379. @@ -2610,6 +2631,7 @@ struct softnet_data {
  12380. unsigned int dropped;
  12381. struct sk_buff_head input_pkt_queue;
  12382. struct napi_struct backlog;
  12383. + struct sk_buff_head tofree_queue;
  12384. };
  12385. diff --git a/include/linux/netfilter/x_tables.h b/include/linux/netfilter/x_tables.h
  12386. index 04078e8a4803..a61c9609e32f 100644
  12387. --- a/include/linux/netfilter/x_tables.h
  12388. +++ b/include/linux/netfilter/x_tables.h
  12389. @@ -4,6 +4,7 @@
  12390. #include <linux/netdevice.h>
  12391. #include <linux/static_key.h>
  12392. +#include <linux/locallock.h>
  12393. #include <uapi/linux/netfilter/x_tables.h>
  12394. /**
  12395. @@ -289,6 +290,8 @@ void xt_free_table_info(struct xt_table_info *info);
  12396. */
  12397. DECLARE_PER_CPU(seqcount_t, xt_recseq);
  12398. +DECLARE_LOCAL_IRQ_LOCK(xt_write_lock);
  12399. +
  12400. /* xt_tee_enabled - true if x_tables needs to handle reentrancy
  12401. *
  12402. * Enabled if current ip(6)tables ruleset has at least one -j TEE rule.
  12403. @@ -309,6 +312,9 @@ static inline unsigned int xt_write_recseq_begin(void)
  12404. {
  12405. unsigned int addend;
  12406. + /* RT protection */
  12407. + local_lock(xt_write_lock);
  12408. +
  12409. /*
  12410. * Low order bit of sequence is set if we already
  12411. * called xt_write_recseq_begin().
  12412. @@ -339,6 +345,7 @@ static inline void xt_write_recseq_end(unsigned int addend)
  12413. /* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
  12414. smp_wmb();
  12415. __this_cpu_add(xt_recseq.sequence, addend);
  12416. + local_unlock(xt_write_lock);
  12417. }
  12418. /*
  12419. diff --git a/include/linux/notifier.h b/include/linux/notifier.h
  12420. index d14a4c362465..2e4414a0c1c4 100644
  12421. --- a/include/linux/notifier.h
  12422. +++ b/include/linux/notifier.h
  12423. @@ -6,7 +6,7 @@
  12424. *
  12425. * Alan Cox <Alan.Cox@linux.org>
  12426. */
  12427. -
  12428. +
  12429. #ifndef _LINUX_NOTIFIER_H
  12430. #define _LINUX_NOTIFIER_H
  12431. #include <linux/errno.h>
  12432. @@ -42,9 +42,7 @@
  12433. * in srcu_notifier_call_chain(): no cache bounces and no memory barriers.
  12434. * As compensation, srcu_notifier_chain_unregister() is rather expensive.
  12435. * SRCU notifier chains should be used when the chain will be called very
  12436. - * often but notifier_blocks will seldom be removed. Also, SRCU notifier
  12437. - * chains are slightly more difficult to use because they require special
  12438. - * runtime initialization.
  12439. + * often but notifier_blocks will seldom be removed.
  12440. */
  12441. typedef int (*notifier_fn_t)(struct notifier_block *nb,
  12442. @@ -88,7 +86,7 @@ struct srcu_notifier_head {
  12443. (name)->head = NULL; \
  12444. } while (0)
  12445. -/* srcu_notifier_heads must be initialized and cleaned up dynamically */
  12446. +/* srcu_notifier_heads must be cleaned up dynamically */
  12447. extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  12448. #define srcu_cleanup_notifier_head(name) \
  12449. cleanup_srcu_struct(&(name)->srcu);
  12450. @@ -101,7 +99,13 @@ extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  12451. .head = NULL }
  12452. #define RAW_NOTIFIER_INIT(name) { \
  12453. .head = NULL }
  12454. -/* srcu_notifier_heads cannot be initialized statically */
  12455. +
  12456. +#define SRCU_NOTIFIER_INIT(name, pcpu) \
  12457. + { \
  12458. + .mutex = __MUTEX_INITIALIZER(name.mutex), \
  12459. + .head = NULL, \
  12460. + .srcu = __SRCU_STRUCT_INIT(name.srcu, pcpu), \
  12461. + }
  12462. #define ATOMIC_NOTIFIER_HEAD(name) \
  12463. struct atomic_notifier_head name = \
  12464. @@ -113,6 +117,18 @@ extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  12465. struct raw_notifier_head name = \
  12466. RAW_NOTIFIER_INIT(name)
  12467. +#define _SRCU_NOTIFIER_HEAD(name, mod) \
  12468. + static DEFINE_PER_CPU(struct srcu_struct_array, \
  12469. + name##_head_srcu_array); \
  12470. + mod struct srcu_notifier_head name = \
  12471. + SRCU_NOTIFIER_INIT(name, name##_head_srcu_array)
  12472. +
  12473. +#define SRCU_NOTIFIER_HEAD(name) \
  12474. + _SRCU_NOTIFIER_HEAD(name, )
  12475. +
  12476. +#define SRCU_NOTIFIER_HEAD_STATIC(name) \
  12477. + _SRCU_NOTIFIER_HEAD(name, static)
  12478. +
  12479. #ifdef __KERNEL__
  12480. extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
  12481. @@ -182,12 +198,12 @@ static inline int notifier_to_errno(int ret)
  12482. /*
  12483. * Declared notifiers so far. I can imagine quite a few more chains
  12484. - * over time (eg laptop power reset chains, reboot chain (to clean
  12485. + * over time (eg laptop power reset chains, reboot chain (to clean
  12486. * device units up), device [un]mount chain, module load/unload chain,
  12487. - * low memory chain, screenblank chain (for plug in modular screenblankers)
  12488. + * low memory chain, screenblank chain (for plug in modular screenblankers)
  12489. * VC switch chains (for loadable kernel svgalib VC switch helpers) etc...
  12490. */
  12491. -
  12492. +
  12493. /* CPU notfiers are defined in include/linux/cpu.h. */
  12494. /* netdevice notifiers are defined in include/linux/netdevice.h */
  12495. diff --git a/include/linux/percpu.h b/include/linux/percpu.h
  12496. index caebf2a758dc..4ecc057b6e27 100644
  12497. --- a/include/linux/percpu.h
  12498. +++ b/include/linux/percpu.h
  12499. @@ -24,6 +24,35 @@
  12500. PERCPU_MODULE_RESERVE)
  12501. #endif
  12502. +#ifdef CONFIG_PREEMPT_RT_FULL
  12503. +
  12504. +#define get_local_var(var) (*({ \
  12505. + migrate_disable(); \
  12506. + this_cpu_ptr(&var); }))
  12507. +
  12508. +#define put_local_var(var) do { \
  12509. + (void)&(var); \
  12510. + migrate_enable(); \
  12511. +} while (0)
  12512. +
  12513. +# define get_local_ptr(var) ({ \
  12514. + migrate_disable(); \
  12515. + this_cpu_ptr(var); })
  12516. +
  12517. +# define put_local_ptr(var) do { \
  12518. + (void)(var); \
  12519. + migrate_enable(); \
  12520. +} while (0)
  12521. +
  12522. +#else
  12523. +
  12524. +#define get_local_var(var) get_cpu_var(var)
  12525. +#define put_local_var(var) put_cpu_var(var)
  12526. +#define get_local_ptr(var) get_cpu_ptr(var)
  12527. +#define put_local_ptr(var) put_cpu_ptr(var)
  12528. +
  12529. +#endif
  12530. +
  12531. /* minimum unit size, also is the maximum supported allocation size */
  12532. #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
  12533. @@ -116,6 +145,7 @@ extern int __init pcpu_page_first_chunk(size_t reserved_size,
  12534. #endif
  12535. extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
  12536. +extern bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr);
  12537. extern bool is_kernel_percpu_address(unsigned long addr);
  12538. #if !defined(CONFIG_SMP) || !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
  12539. diff --git a/include/linux/pid.h b/include/linux/pid.h
  12540. index 97b745ddece5..01a5460a0c85 100644
  12541. --- a/include/linux/pid.h
  12542. +++ b/include/linux/pid.h
  12543. @@ -2,6 +2,7 @@
  12544. #define _LINUX_PID_H
  12545. #include <linux/rcupdate.h>
  12546. +#include <linux/atomic.h>
  12547. enum pid_type
  12548. {
  12549. diff --git a/include/linux/preempt.h b/include/linux/preempt.h
  12550. index 7eeceac52dea..f97c54265904 100644
  12551. --- a/include/linux/preempt.h
  12552. +++ b/include/linux/preempt.h
  12553. @@ -50,7 +50,11 @@
  12554. #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
  12555. #define NMI_OFFSET (1UL << NMI_SHIFT)
  12556. -#define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  12557. +#ifndef CONFIG_PREEMPT_RT_FULL
  12558. +# define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  12559. +#else
  12560. +# define SOFTIRQ_DISABLE_OFFSET (0)
  12561. +#endif
  12562. /* We use the MSB mostly because its available */
  12563. #define PREEMPT_NEED_RESCHED 0x80000000
  12564. @@ -59,9 +63,15 @@
  12565. #include <asm/preempt.h>
  12566. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  12567. -#define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  12568. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
  12569. | NMI_MASK))
  12570. +#ifndef CONFIG_PREEMPT_RT_FULL
  12571. +# define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  12572. +# define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  12573. +#else
  12574. +# define softirq_count() (0UL)
  12575. +extern int in_serving_softirq(void);
  12576. +#endif
  12577. /*
  12578. * Are we doing bottom half or hardware interrupt processing?
  12579. @@ -79,7 +89,6 @@
  12580. #define in_irq() (hardirq_count())
  12581. #define in_softirq() (softirq_count())
  12582. #define in_interrupt() (irq_count())
  12583. -#define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  12584. #define in_nmi() (preempt_count() & NMI_MASK)
  12585. #define in_task() (!(preempt_count() & \
  12586. (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)))
  12587. @@ -96,7 +105,11 @@
  12588. /*
  12589. * The preempt_count offset after spin_lock()
  12590. */
  12591. +#if !defined(CONFIG_PREEMPT_RT_FULL)
  12592. #define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
  12593. +#else
  12594. +#define PREEMPT_LOCK_OFFSET 0
  12595. +#endif
  12596. /*
  12597. * The preempt_count offset needed for things like:
  12598. @@ -145,6 +158,20 @@ extern void preempt_count_sub(int val);
  12599. #define preempt_count_inc() preempt_count_add(1)
  12600. #define preempt_count_dec() preempt_count_sub(1)
  12601. +#ifdef CONFIG_PREEMPT_LAZY
  12602. +#define add_preempt_lazy_count(val) do { preempt_lazy_count() += (val); } while (0)
  12603. +#define sub_preempt_lazy_count(val) do { preempt_lazy_count() -= (val); } while (0)
  12604. +#define inc_preempt_lazy_count() add_preempt_lazy_count(1)
  12605. +#define dec_preempt_lazy_count() sub_preempt_lazy_count(1)
  12606. +#define preempt_lazy_count() (current_thread_info()->preempt_lazy_count)
  12607. +#else
  12608. +#define add_preempt_lazy_count(val) do { } while (0)
  12609. +#define sub_preempt_lazy_count(val) do { } while (0)
  12610. +#define inc_preempt_lazy_count() do { } while (0)
  12611. +#define dec_preempt_lazy_count() do { } while (0)
  12612. +#define preempt_lazy_count() (0)
  12613. +#endif
  12614. +
  12615. #ifdef CONFIG_PREEMPT_COUNT
  12616. #define preempt_disable() \
  12617. @@ -153,13 +180,25 @@ do { \
  12618. barrier(); \
  12619. } while (0)
  12620. +#define preempt_lazy_disable() \
  12621. +do { \
  12622. + inc_preempt_lazy_count(); \
  12623. + barrier(); \
  12624. +} while (0)
  12625. +
  12626. #define sched_preempt_enable_no_resched() \
  12627. do { \
  12628. barrier(); \
  12629. preempt_count_dec(); \
  12630. } while (0)
  12631. -#define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  12632. +#ifdef CONFIG_PREEMPT_RT_BASE
  12633. +# define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  12634. +# define preempt_check_resched_rt() preempt_check_resched()
  12635. +#else
  12636. +# define preempt_enable_no_resched() preempt_enable()
  12637. +# define preempt_check_resched_rt() barrier();
  12638. +#endif
  12639. #define preemptible() (preempt_count() == 0 && !irqs_disabled())
  12640. @@ -184,6 +223,13 @@ do { \
  12641. __preempt_schedule(); \
  12642. } while (0)
  12643. +#define preempt_lazy_enable() \
  12644. +do { \
  12645. + dec_preempt_lazy_count(); \
  12646. + barrier(); \
  12647. + preempt_check_resched(); \
  12648. +} while (0)
  12649. +
  12650. #else /* !CONFIG_PREEMPT */
  12651. #define preempt_enable() \
  12652. do { \
  12653. @@ -229,6 +275,7 @@ do { \
  12654. #define preempt_disable_notrace() barrier()
  12655. #define preempt_enable_no_resched_notrace() barrier()
  12656. #define preempt_enable_notrace() barrier()
  12657. +#define preempt_check_resched_rt() barrier()
  12658. #define preemptible() 0
  12659. #endif /* CONFIG_PREEMPT_COUNT */
  12660. @@ -249,10 +296,31 @@ do { \
  12661. } while (0)
  12662. #define preempt_fold_need_resched() \
  12663. do { \
  12664. - if (tif_need_resched()) \
  12665. + if (tif_need_resched_now()) \
  12666. set_preempt_need_resched(); \
  12667. } while (0)
  12668. +#ifdef CONFIG_PREEMPT_RT_FULL
  12669. +# define preempt_disable_rt() preempt_disable()
  12670. +# define preempt_enable_rt() preempt_enable()
  12671. +# define preempt_disable_nort() barrier()
  12672. +# define preempt_enable_nort() barrier()
  12673. +# ifdef CONFIG_SMP
  12674. + extern void migrate_disable(void);
  12675. + extern void migrate_enable(void);
  12676. +# else /* CONFIG_SMP */
  12677. +# define migrate_disable() barrier()
  12678. +# define migrate_enable() barrier()
  12679. +# endif /* CONFIG_SMP */
  12680. +#else
  12681. +# define preempt_disable_rt() barrier()
  12682. +# define preempt_enable_rt() barrier()
  12683. +# define preempt_disable_nort() preempt_disable()
  12684. +# define preempt_enable_nort() preempt_enable()
  12685. +# define migrate_disable() preempt_disable()
  12686. +# define migrate_enable() preempt_enable()
  12687. +#endif
  12688. +
  12689. #ifdef CONFIG_PREEMPT_NOTIFIERS
  12690. struct preempt_notifier;
  12691. diff --git a/include/linux/printk.h b/include/linux/printk.h
  12692. index 9729565c25ff..9cdca696b718 100644
  12693. --- a/include/linux/printk.h
  12694. +++ b/include/linux/printk.h
  12695. @@ -117,9 +117,11 @@ int no_printk(const char *fmt, ...)
  12696. #ifdef CONFIG_EARLY_PRINTK
  12697. extern asmlinkage __printf(1, 2)
  12698. void early_printk(const char *fmt, ...);
  12699. +extern void printk_kill(void);
  12700. #else
  12701. static inline __printf(1, 2) __cold
  12702. void early_printk(const char *s, ...) { }
  12703. +static inline void printk_kill(void) { }
  12704. #endif
  12705. typedef __printf(1, 0) int (*printk_func_t)(const char *fmt, va_list args);
  12706. diff --git a/include/linux/radix-tree.h b/include/linux/radix-tree.h
  12707. index 5d5174b59802..327dddaf4c8f 100644
  12708. --- a/include/linux/radix-tree.h
  12709. +++ b/include/linux/radix-tree.h
  12710. @@ -279,6 +279,8 @@ unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  12711. unsigned long first_index, unsigned int max_items);
  12712. int radix_tree_preload(gfp_t gfp_mask);
  12713. int radix_tree_maybe_preload(gfp_t gfp_mask);
  12714. +void radix_tree_preload_end(void);
  12715. +
  12716. void radix_tree_init(void);
  12717. void *radix_tree_tag_set(struct radix_tree_root *root,
  12718. unsigned long index, unsigned int tag);
  12719. @@ -301,11 +303,6 @@ unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
  12720. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
  12721. unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item);
  12722. -static inline void radix_tree_preload_end(void)
  12723. -{
  12724. - preempt_enable();
  12725. -}
  12726. -
  12727. /**
  12728. * struct radix_tree_iter - radix tree iterator state
  12729. *
  12730. diff --git a/include/linux/random.h b/include/linux/random.h
  12731. index 9c29122037f9..e7f2f8604918 100644
  12732. --- a/include/linux/random.h
  12733. +++ b/include/linux/random.h
  12734. @@ -20,7 +20,7 @@ struct random_ready_callback {
  12735. extern void add_device_randomness(const void *, unsigned int);
  12736. extern void add_input_randomness(unsigned int type, unsigned int code,
  12737. unsigned int value);
  12738. -extern void add_interrupt_randomness(int irq, int irq_flags);
  12739. +extern void add_interrupt_randomness(int irq, int irq_flags, __u64 ip);
  12740. extern void get_random_bytes(void *buf, int nbytes);
  12741. extern int add_random_ready_callback(struct random_ready_callback *rdy);
  12742. diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h
  12743. index a5aa7ae671f4..24ddffd25492 100644
  12744. --- a/include/linux/rbtree.h
  12745. +++ b/include/linux/rbtree.h
  12746. @@ -31,7 +31,6 @@
  12747. #include <linux/kernel.h>
  12748. #include <linux/stddef.h>
  12749. -#include <linux/rcupdate.h>
  12750. struct rb_node {
  12751. unsigned long __rb_parent_color;
  12752. @@ -86,14 +85,8 @@ static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
  12753. *rb_link = node;
  12754. }
  12755. -static inline void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  12756. - struct rb_node **rb_link)
  12757. -{
  12758. - node->__rb_parent_color = (unsigned long)parent;
  12759. - node->rb_left = node->rb_right = NULL;
  12760. -
  12761. - rcu_assign_pointer(*rb_link, node);
  12762. -}
  12763. +void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  12764. + struct rb_node **rb_link);
  12765. #define rb_entry_safe(ptr, type, member) \
  12766. ({ typeof(ptr) ____ptr = (ptr); \
  12767. diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
  12768. index a0189ba67fde..c2f5f955163d 100644
  12769. --- a/include/linux/rcupdate.h
  12770. +++ b/include/linux/rcupdate.h
  12771. @@ -169,6 +169,9 @@ void call_rcu(struct rcu_head *head,
  12772. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  12773. +#ifdef CONFIG_PREEMPT_RT_FULL
  12774. +#define call_rcu_bh call_rcu
  12775. +#else
  12776. /**
  12777. * call_rcu_bh() - Queue an RCU for invocation after a quicker grace period.
  12778. * @head: structure to be used for queueing the RCU updates.
  12779. @@ -192,6 +195,7 @@ void call_rcu(struct rcu_head *head,
  12780. */
  12781. void call_rcu_bh(struct rcu_head *head,
  12782. rcu_callback_t func);
  12783. +#endif
  12784. /**
  12785. * call_rcu_sched() - Queue an RCU for invocation after sched grace period.
  12786. @@ -292,6 +296,11 @@ void synchronize_rcu(void);
  12787. * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
  12788. */
  12789. #define rcu_preempt_depth() (current->rcu_read_lock_nesting)
  12790. +#ifndef CONFIG_PREEMPT_RT_FULL
  12791. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  12792. +#else
  12793. +static inline int sched_rcu_preempt_depth(void) { return 0; }
  12794. +#endif
  12795. #else /* #ifdef CONFIG_PREEMPT_RCU */
  12796. @@ -317,6 +326,8 @@ static inline int rcu_preempt_depth(void)
  12797. return 0;
  12798. }
  12799. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  12800. +
  12801. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  12802. /* Internal to kernel */
  12803. @@ -489,7 +500,14 @@ extern struct lockdep_map rcu_callback_map;
  12804. int debug_lockdep_rcu_enabled(void);
  12805. int rcu_read_lock_held(void);
  12806. +#ifdef CONFIG_PREEMPT_RT_FULL
  12807. +static inline int rcu_read_lock_bh_held(void)
  12808. +{
  12809. + return rcu_read_lock_held();
  12810. +}
  12811. +#else
  12812. int rcu_read_lock_bh_held(void);
  12813. +#endif
  12814. /**
  12815. * rcu_read_lock_sched_held() - might we be in RCU-sched read-side critical section?
  12816. @@ -937,10 +955,14 @@ static inline void rcu_read_unlock(void)
  12817. static inline void rcu_read_lock_bh(void)
  12818. {
  12819. local_bh_disable();
  12820. +#ifdef CONFIG_PREEMPT_RT_FULL
  12821. + rcu_read_lock();
  12822. +#else
  12823. __acquire(RCU_BH);
  12824. rcu_lock_acquire(&rcu_bh_lock_map);
  12825. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  12826. "rcu_read_lock_bh() used illegally while idle");
  12827. +#endif
  12828. }
  12829. /*
  12830. @@ -950,10 +972,14 @@ static inline void rcu_read_lock_bh(void)
  12831. */
  12832. static inline void rcu_read_unlock_bh(void)
  12833. {
  12834. +#ifdef CONFIG_PREEMPT_RT_FULL
  12835. + rcu_read_unlock();
  12836. +#else
  12837. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  12838. "rcu_read_unlock_bh() used illegally while idle");
  12839. rcu_lock_release(&rcu_bh_lock_map);
  12840. __release(RCU_BH);
  12841. +#endif
  12842. local_bh_enable();
  12843. }
  12844. diff --git a/include/linux/rcutree.h b/include/linux/rcutree.h
  12845. index 60d15a080d7c..436c9e62bfc6 100644
  12846. --- a/include/linux/rcutree.h
  12847. +++ b/include/linux/rcutree.h
  12848. @@ -44,7 +44,11 @@ static inline void rcu_virt_note_context_switch(int cpu)
  12849. rcu_note_context_switch();
  12850. }
  12851. +#ifdef CONFIG_PREEMPT_RT_FULL
  12852. +# define synchronize_rcu_bh synchronize_rcu
  12853. +#else
  12854. void synchronize_rcu_bh(void);
  12855. +#endif
  12856. void synchronize_sched_expedited(void);
  12857. void synchronize_rcu_expedited(void);
  12858. @@ -72,7 +76,11 @@ static inline void synchronize_rcu_bh_expedited(void)
  12859. }
  12860. void rcu_barrier(void);
  12861. +#ifdef CONFIG_PREEMPT_RT_FULL
  12862. +# define rcu_barrier_bh rcu_barrier
  12863. +#else
  12864. void rcu_barrier_bh(void);
  12865. +#endif
  12866. void rcu_barrier_sched(void);
  12867. unsigned long get_state_synchronize_rcu(void);
  12868. void cond_synchronize_rcu(unsigned long oldstate);
  12869. @@ -85,12 +93,10 @@ unsigned long rcu_batches_started(void);
  12870. unsigned long rcu_batches_started_bh(void);
  12871. unsigned long rcu_batches_started_sched(void);
  12872. unsigned long rcu_batches_completed(void);
  12873. -unsigned long rcu_batches_completed_bh(void);
  12874. unsigned long rcu_batches_completed_sched(void);
  12875. void show_rcu_gp_kthreads(void);
  12876. void rcu_force_quiescent_state(void);
  12877. -void rcu_bh_force_quiescent_state(void);
  12878. void rcu_sched_force_quiescent_state(void);
  12879. void rcu_idle_enter(void);
  12880. @@ -105,6 +111,14 @@ extern int rcu_scheduler_active __read_mostly;
  12881. bool rcu_is_watching(void);
  12882. +#ifndef CONFIG_PREEMPT_RT_FULL
  12883. +void rcu_bh_force_quiescent_state(void);
  12884. +unsigned long rcu_batches_completed_bh(void);
  12885. +#else
  12886. +# define rcu_bh_force_quiescent_state rcu_force_quiescent_state
  12887. +# define rcu_batches_completed_bh rcu_batches_completed
  12888. +#endif
  12889. +
  12890. void rcu_all_qs(void);
  12891. #endif /* __LINUX_RCUTREE_H */
  12892. diff --git a/include/linux/rtmutex.h b/include/linux/rtmutex.h
  12893. index 1abba5ce2a2f..30211c627511 100644
  12894. --- a/include/linux/rtmutex.h
  12895. +++ b/include/linux/rtmutex.h
  12896. @@ -13,11 +13,15 @@
  12897. #define __LINUX_RT_MUTEX_H
  12898. #include <linux/linkage.h>
  12899. +#include <linux/spinlock_types_raw.h>
  12900. #include <linux/rbtree.h>
  12901. -#include <linux/spinlock_types.h>
  12902. extern int max_lock_depth; /* for sysctl */
  12903. +#ifdef CONFIG_DEBUG_MUTEXES
  12904. +#include <linux/debug_locks.h>
  12905. +#endif
  12906. +
  12907. /**
  12908. * The rt_mutex structure
  12909. *
  12910. @@ -31,8 +35,8 @@ struct rt_mutex {
  12911. struct rb_root waiters;
  12912. struct rb_node *waiters_leftmost;
  12913. struct task_struct *owner;
  12914. -#ifdef CONFIG_DEBUG_RT_MUTEXES
  12915. int save_state;
  12916. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  12917. const char *name, *file;
  12918. int line;
  12919. void *magic;
  12920. @@ -55,22 +59,33 @@ struct hrtimer_sleeper;
  12921. # define rt_mutex_debug_check_no_locks_held(task) do { } while (0)
  12922. #endif
  12923. +# define rt_mutex_init(mutex) \
  12924. + do { \
  12925. + raw_spin_lock_init(&(mutex)->wait_lock); \
  12926. + __rt_mutex_init(mutex, #mutex); \
  12927. + } while (0)
  12928. +
  12929. #ifdef CONFIG_DEBUG_RT_MUTEXES
  12930. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
  12931. , .name = #mutexname, .file = __FILE__, .line = __LINE__
  12932. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, __func__)
  12933. extern void rt_mutex_debug_task_free(struct task_struct *tsk);
  12934. #else
  12935. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  12936. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL)
  12937. # define rt_mutex_debug_task_free(t) do { } while (0)
  12938. #endif
  12939. -#define __RT_MUTEX_INITIALIZER(mutexname) \
  12940. - { .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  12941. +#define __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  12942. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  12943. , .waiters = RB_ROOT \
  12944. , .owner = NULL \
  12945. - __DEBUG_RT_MUTEX_INITIALIZER(mutexname)}
  12946. + __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  12947. +
  12948. +#define __RT_MUTEX_INITIALIZER(mutexname) \
  12949. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) }
  12950. +
  12951. +#define __RT_MUTEX_INITIALIZER_SAVE_STATE(mutexname) \
  12952. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  12953. + , .save_state = 1 }
  12954. #define DEFINE_RT_MUTEX(mutexname) \
  12955. struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
  12956. @@ -91,6 +106,7 @@ extern void rt_mutex_destroy(struct rt_mutex *lock);
  12957. extern void rt_mutex_lock(struct rt_mutex *lock);
  12958. extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
  12959. +extern int rt_mutex_lock_killable(struct rt_mutex *lock);
  12960. extern int rt_mutex_timed_lock(struct rt_mutex *lock,
  12961. struct hrtimer_sleeper *timeout);
  12962. diff --git a/include/linux/rwlock_rt.h b/include/linux/rwlock_rt.h
  12963. new file mode 100644
  12964. index 000000000000..49ed2d45d3be
  12965. --- /dev/null
  12966. +++ b/include/linux/rwlock_rt.h
  12967. @@ -0,0 +1,99 @@
  12968. +#ifndef __LINUX_RWLOCK_RT_H
  12969. +#define __LINUX_RWLOCK_RT_H
  12970. +
  12971. +#ifndef __LINUX_SPINLOCK_H
  12972. +#error Do not include directly. Use spinlock.h
  12973. +#endif
  12974. +
  12975. +#define rwlock_init(rwl) \
  12976. +do { \
  12977. + static struct lock_class_key __key; \
  12978. + \
  12979. + rt_mutex_init(&(rwl)->lock); \
  12980. + __rt_rwlock_init(rwl, #rwl, &__key); \
  12981. +} while (0)
  12982. +
  12983. +extern void __lockfunc rt_write_lock(rwlock_t *rwlock);
  12984. +extern void __lockfunc rt_read_lock(rwlock_t *rwlock);
  12985. +extern int __lockfunc rt_write_trylock(rwlock_t *rwlock);
  12986. +extern int __lockfunc rt_write_trylock_irqsave(rwlock_t *trylock, unsigned long *flags);
  12987. +extern int __lockfunc rt_read_trylock(rwlock_t *rwlock);
  12988. +extern void __lockfunc rt_write_unlock(rwlock_t *rwlock);
  12989. +extern void __lockfunc rt_read_unlock(rwlock_t *rwlock);
  12990. +extern unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock);
  12991. +extern unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock);
  12992. +extern void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key);
  12993. +
  12994. +#define read_trylock(lock) __cond_lock(lock, rt_read_trylock(lock))
  12995. +#define write_trylock(lock) __cond_lock(lock, rt_write_trylock(lock))
  12996. +
  12997. +#define write_trylock_irqsave(lock, flags) \
  12998. + __cond_lock(lock, rt_write_trylock_irqsave(lock, &flags))
  12999. +
  13000. +#define read_lock_irqsave(lock, flags) \
  13001. + do { \
  13002. + typecheck(unsigned long, flags); \
  13003. + flags = rt_read_lock_irqsave(lock); \
  13004. + } while (0)
  13005. +
  13006. +#define write_lock_irqsave(lock, flags) \
  13007. + do { \
  13008. + typecheck(unsigned long, flags); \
  13009. + flags = rt_write_lock_irqsave(lock); \
  13010. + } while (0)
  13011. +
  13012. +#define read_lock(lock) rt_read_lock(lock)
  13013. +
  13014. +#define read_lock_bh(lock) \
  13015. + do { \
  13016. + local_bh_disable(); \
  13017. + rt_read_lock(lock); \
  13018. + } while (0)
  13019. +
  13020. +#define read_lock_irq(lock) read_lock(lock)
  13021. +
  13022. +#define write_lock(lock) rt_write_lock(lock)
  13023. +
  13024. +#define write_lock_bh(lock) \
  13025. + do { \
  13026. + local_bh_disable(); \
  13027. + rt_write_lock(lock); \
  13028. + } while (0)
  13029. +
  13030. +#define write_lock_irq(lock) write_lock(lock)
  13031. +
  13032. +#define read_unlock(lock) rt_read_unlock(lock)
  13033. +
  13034. +#define read_unlock_bh(lock) \
  13035. + do { \
  13036. + rt_read_unlock(lock); \
  13037. + local_bh_enable(); \
  13038. + } while (0)
  13039. +
  13040. +#define read_unlock_irq(lock) read_unlock(lock)
  13041. +
  13042. +#define write_unlock(lock) rt_write_unlock(lock)
  13043. +
  13044. +#define write_unlock_bh(lock) \
  13045. + do { \
  13046. + rt_write_unlock(lock); \
  13047. + local_bh_enable(); \
  13048. + } while (0)
  13049. +
  13050. +#define write_unlock_irq(lock) write_unlock(lock)
  13051. +
  13052. +#define read_unlock_irqrestore(lock, flags) \
  13053. + do { \
  13054. + typecheck(unsigned long, flags); \
  13055. + (void) flags; \
  13056. + rt_read_unlock(lock); \
  13057. + } while (0)
  13058. +
  13059. +#define write_unlock_irqrestore(lock, flags) \
  13060. + do { \
  13061. + typecheck(unsigned long, flags); \
  13062. + (void) flags; \
  13063. + rt_write_unlock(lock); \
  13064. + } while (0)
  13065. +
  13066. +#endif
  13067. diff --git a/include/linux/rwlock_types.h b/include/linux/rwlock_types.h
  13068. index cc0072e93e36..d0da966ad7a0 100644
  13069. --- a/include/linux/rwlock_types.h
  13070. +++ b/include/linux/rwlock_types.h
  13071. @@ -1,6 +1,10 @@
  13072. #ifndef __LINUX_RWLOCK_TYPES_H
  13073. #define __LINUX_RWLOCK_TYPES_H
  13074. +#if !defined(__LINUX_SPINLOCK_TYPES_H)
  13075. +# error "Do not include directly, include spinlock_types.h"
  13076. +#endif
  13077. +
  13078. /*
  13079. * include/linux/rwlock_types.h - generic rwlock type definitions
  13080. * and initializers
  13081. @@ -43,6 +47,7 @@ typedef struct {
  13082. RW_DEP_MAP_INIT(lockname) }
  13083. #endif
  13084. -#define DEFINE_RWLOCK(x) rwlock_t x = __RW_LOCK_UNLOCKED(x)
  13085. +#define DEFINE_RWLOCK(name) \
  13086. + rwlock_t name __cacheline_aligned_in_smp = __RW_LOCK_UNLOCKED(name)
  13087. #endif /* __LINUX_RWLOCK_TYPES_H */
  13088. diff --git a/include/linux/rwlock_types_rt.h b/include/linux/rwlock_types_rt.h
  13089. new file mode 100644
  13090. index 000000000000..b13832119591
  13091. --- /dev/null
  13092. +++ b/include/linux/rwlock_types_rt.h
  13093. @@ -0,0 +1,33 @@
  13094. +#ifndef __LINUX_RWLOCK_TYPES_RT_H
  13095. +#define __LINUX_RWLOCK_TYPES_RT_H
  13096. +
  13097. +#ifndef __LINUX_SPINLOCK_TYPES_H
  13098. +#error "Do not include directly. Include spinlock_types.h instead"
  13099. +#endif
  13100. +
  13101. +/*
  13102. + * rwlocks - rtmutex which allows single reader recursion
  13103. + */
  13104. +typedef struct {
  13105. + struct rt_mutex lock;
  13106. + int read_depth;
  13107. + unsigned int break_lock;
  13108. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13109. + struct lockdep_map dep_map;
  13110. +#endif
  13111. +} rwlock_t;
  13112. +
  13113. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13114. +# define RW_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  13115. +#else
  13116. +# define RW_DEP_MAP_INIT(lockname)
  13117. +#endif
  13118. +
  13119. +#define __RW_LOCK_UNLOCKED(name) \
  13120. + { .lock = __RT_MUTEX_INITIALIZER_SAVE_STATE(name.lock), \
  13121. + RW_DEP_MAP_INIT(name) }
  13122. +
  13123. +#define DEFINE_RWLOCK(name) \
  13124. + rwlock_t name __cacheline_aligned_in_smp = __RW_LOCK_UNLOCKED(name)
  13125. +
  13126. +#endif
  13127. diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h
  13128. index 8f498cdde280..2b2148431f14 100644
  13129. --- a/include/linux/rwsem.h
  13130. +++ b/include/linux/rwsem.h
  13131. @@ -18,6 +18,10 @@
  13132. #include <linux/osq_lock.h>
  13133. #endif
  13134. +#ifdef CONFIG_PREEMPT_RT_FULL
  13135. +#include <linux/rwsem_rt.h>
  13136. +#else /* PREEMPT_RT_FULL */
  13137. +
  13138. struct rw_semaphore;
  13139. #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
  13140. @@ -177,4 +181,6 @@ extern void up_read_non_owner(struct rw_semaphore *sem);
  13141. # define up_read_non_owner(sem) up_read(sem)
  13142. #endif
  13143. +#endif /* !PREEMPT_RT_FULL */
  13144. +
  13145. #endif /* _LINUX_RWSEM_H */
  13146. diff --git a/include/linux/rwsem_rt.h b/include/linux/rwsem_rt.h
  13147. new file mode 100644
  13148. index 000000000000..f97860b2e2a4
  13149. --- /dev/null
  13150. +++ b/include/linux/rwsem_rt.h
  13151. @@ -0,0 +1,152 @@
  13152. +#ifndef _LINUX_RWSEM_RT_H
  13153. +#define _LINUX_RWSEM_RT_H
  13154. +
  13155. +#ifndef _LINUX_RWSEM_H
  13156. +#error "Include rwsem.h"
  13157. +#endif
  13158. +
  13159. +/*
  13160. + * RW-semaphores are a spinlock plus a reader-depth count.
  13161. + *
  13162. + * Note that the semantics are different from the usual
  13163. + * Linux rw-sems, in PREEMPT_RT mode we do not allow
  13164. + * multiple readers to hold the lock at once, we only allow
  13165. + * a read-lock owner to read-lock recursively. This is
  13166. + * better for latency, makes the implementation inherently
  13167. + * fair and makes it simpler as well.
  13168. + */
  13169. +
  13170. +#include <linux/rtmutex.h>
  13171. +
  13172. +struct rw_semaphore {
  13173. + struct rt_mutex lock;
  13174. + int read_depth;
  13175. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13176. + struct lockdep_map dep_map;
  13177. +#endif
  13178. +};
  13179. +
  13180. +#define __RWSEM_INITIALIZER(name) \
  13181. + { .lock = __RT_MUTEX_INITIALIZER(name.lock), \
  13182. + RW_DEP_MAP_INIT(name) }
  13183. +
  13184. +#define DECLARE_RWSEM(lockname) \
  13185. + struct rw_semaphore lockname = __RWSEM_INITIALIZER(lockname)
  13186. +
  13187. +extern void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  13188. + struct lock_class_key *key);
  13189. +
  13190. +#define __rt_init_rwsem(sem, name, key) \
  13191. + do { \
  13192. + rt_mutex_init(&(sem)->lock); \
  13193. + __rt_rwsem_init((sem), (name), (key));\
  13194. + } while (0)
  13195. +
  13196. +#define __init_rwsem(sem, name, key) __rt_init_rwsem(sem, name, key)
  13197. +
  13198. +# define rt_init_rwsem(sem) \
  13199. +do { \
  13200. + static struct lock_class_key __key; \
  13201. + \
  13202. + __rt_init_rwsem((sem), #sem, &__key); \
  13203. +} while (0)
  13204. +
  13205. +extern void rt_down_write(struct rw_semaphore *rwsem);
  13206. +extern void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass);
  13207. +extern void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass);
  13208. +extern void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  13209. + struct lockdep_map *nest);
  13210. +extern void rt__down_read(struct rw_semaphore *rwsem);
  13211. +extern void rt_down_read(struct rw_semaphore *rwsem);
  13212. +extern int rt_down_write_trylock(struct rw_semaphore *rwsem);
  13213. +extern int rt__down_read_trylock(struct rw_semaphore *rwsem);
  13214. +extern int rt_down_read_trylock(struct rw_semaphore *rwsem);
  13215. +extern void __rt_up_read(struct rw_semaphore *rwsem);
  13216. +extern void rt_up_read(struct rw_semaphore *rwsem);
  13217. +extern void rt_up_write(struct rw_semaphore *rwsem);
  13218. +extern void rt_downgrade_write(struct rw_semaphore *rwsem);
  13219. +
  13220. +#define init_rwsem(sem) rt_init_rwsem(sem)
  13221. +#define rwsem_is_locked(s) rt_mutex_is_locked(&(s)->lock)
  13222. +
  13223. +static inline int rwsem_is_contended(struct rw_semaphore *sem)
  13224. +{
  13225. + /* rt_mutex_has_waiters() */
  13226. + return !RB_EMPTY_ROOT(&sem->lock.waiters);
  13227. +}
  13228. +
  13229. +static inline void __down_read(struct rw_semaphore *sem)
  13230. +{
  13231. + rt__down_read(sem);
  13232. +}
  13233. +
  13234. +static inline void down_read(struct rw_semaphore *sem)
  13235. +{
  13236. + rt_down_read(sem);
  13237. +}
  13238. +
  13239. +static inline int __down_read_trylock(struct rw_semaphore *sem)
  13240. +{
  13241. + return rt__down_read_trylock(sem);
  13242. +}
  13243. +
  13244. +static inline int down_read_trylock(struct rw_semaphore *sem)
  13245. +{
  13246. + return rt_down_read_trylock(sem);
  13247. +}
  13248. +
  13249. +static inline void down_write(struct rw_semaphore *sem)
  13250. +{
  13251. + rt_down_write(sem);
  13252. +}
  13253. +
  13254. +static inline int down_write_trylock(struct rw_semaphore *sem)
  13255. +{
  13256. + return rt_down_write_trylock(sem);
  13257. +}
  13258. +
  13259. +static inline void __up_read(struct rw_semaphore *sem)
  13260. +{
  13261. + __rt_up_read(sem);
  13262. +}
  13263. +
  13264. +static inline void up_read(struct rw_semaphore *sem)
  13265. +{
  13266. + rt_up_read(sem);
  13267. +}
  13268. +
  13269. +static inline void up_write(struct rw_semaphore *sem)
  13270. +{
  13271. + rt_up_write(sem);
  13272. +}
  13273. +
  13274. +static inline void downgrade_write(struct rw_semaphore *sem)
  13275. +{
  13276. + rt_downgrade_write(sem);
  13277. +}
  13278. +
  13279. +static inline void down_read_nested(struct rw_semaphore *sem, int subclass)
  13280. +{
  13281. + return rt_down_read_nested(sem, subclass);
  13282. +}
  13283. +
  13284. +static inline void down_write_nested(struct rw_semaphore *sem, int subclass)
  13285. +{
  13286. + rt_down_write_nested(sem, subclass);
  13287. +}
  13288. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13289. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  13290. + struct rw_semaphore *nest_lock)
  13291. +{
  13292. + rt_down_write_nested_lock(sem, &nest_lock->dep_map);
  13293. +}
  13294. +
  13295. +#else
  13296. +
  13297. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  13298. + struct rw_semaphore *nest_lock)
  13299. +{
  13300. + rt_down_write_nested_lock(sem, NULL);
  13301. +}
  13302. +#endif
  13303. +#endif
  13304. diff --git a/include/linux/sched.h b/include/linux/sched.h
  13305. index e887c8d6f395..f37654adf12a 100644
  13306. --- a/include/linux/sched.h
  13307. +++ b/include/linux/sched.h
  13308. @@ -26,6 +26,7 @@ struct sched_param {
  13309. #include <linux/nodemask.h>
  13310. #include <linux/mm_types.h>
  13311. #include <linux/preempt.h>
  13312. +#include <asm/kmap_types.h>
  13313. #include <asm/page.h>
  13314. #include <asm/ptrace.h>
  13315. @@ -182,8 +183,6 @@ extern void update_cpu_load_nohz(void);
  13316. static inline void update_cpu_load_nohz(void) { }
  13317. #endif
  13318. -extern unsigned long get_parent_ip(unsigned long addr);
  13319. -
  13320. extern void dump_cpu_task(int cpu);
  13321. struct seq_file;
  13322. @@ -235,17 +234,13 @@ extern char ___assert_task_state[1 - 2*!!(
  13323. /* Convenience macros for the sake of wake_up */
  13324. #define TASK_NORMAL (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
  13325. -#define TASK_ALL (TASK_NORMAL | __TASK_STOPPED | __TASK_TRACED)
  13326. /* get_task_state() */
  13327. #define TASK_REPORT (TASK_RUNNING | TASK_INTERRUPTIBLE | \
  13328. TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
  13329. __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
  13330. -#define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
  13331. #define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
  13332. -#define task_is_stopped_or_traced(task) \
  13333. - ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
  13334. #define task_contributes_to_load(task) \
  13335. ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
  13336. (task->flags & PF_FROZEN) == 0 && \
  13337. @@ -311,6 +306,11 @@ extern char ___assert_task_state[1 - 2*!!(
  13338. #endif
  13339. +#define __set_current_state_no_track(state_value) \
  13340. + do { current->state = (state_value); } while (0)
  13341. +#define set_current_state_no_track(state_value) \
  13342. + set_mb(current->state, (state_value))
  13343. +
  13344. /* Task command name length */
  13345. #define TASK_COMM_LEN 16
  13346. @@ -979,9 +979,31 @@ struct wake_q_head {
  13347. #define WAKE_Q(name) \
  13348. struct wake_q_head name = { WAKE_Q_TAIL, &name.first }
  13349. -extern void wake_q_add(struct wake_q_head *head,
  13350. - struct task_struct *task);
  13351. -extern void wake_up_q(struct wake_q_head *head);
  13352. +extern void __wake_q_add(struct wake_q_head *head,
  13353. + struct task_struct *task, bool sleeper);
  13354. +static inline void wake_q_add(struct wake_q_head *head,
  13355. + struct task_struct *task)
  13356. +{
  13357. + __wake_q_add(head, task, false);
  13358. +}
  13359. +
  13360. +static inline void wake_q_add_sleeper(struct wake_q_head *head,
  13361. + struct task_struct *task)
  13362. +{
  13363. + __wake_q_add(head, task, true);
  13364. +}
  13365. +
  13366. +extern void __wake_up_q(struct wake_q_head *head, bool sleeper);
  13367. +
  13368. +static inline void wake_up_q(struct wake_q_head *head)
  13369. +{
  13370. + __wake_up_q(head, false);
  13371. +}
  13372. +
  13373. +static inline void wake_up_q_sleeper(struct wake_q_head *head)
  13374. +{
  13375. + __wake_up_q(head, true);
  13376. +}
  13377. /*
  13378. * sched-domains (multiprocessor balancing) declarations:
  13379. @@ -1389,6 +1411,7 @@ struct tlbflush_unmap_batch {
  13380. struct task_struct {
  13381. volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
  13382. + volatile long saved_state; /* saved state for "spinlock sleepers" */
  13383. void *stack;
  13384. atomic_t usage;
  13385. unsigned int flags; /* per process flags, defined below */
  13386. @@ -1425,6 +1448,13 @@ struct task_struct {
  13387. #endif
  13388. unsigned int policy;
  13389. +#ifdef CONFIG_PREEMPT_RT_FULL
  13390. + int migrate_disable;
  13391. + int migrate_disable_update;
  13392. +# ifdef CONFIG_SCHED_DEBUG
  13393. + int migrate_disable_atomic;
  13394. +# endif
  13395. +#endif
  13396. int nr_cpus_allowed;
  13397. cpumask_t cpus_allowed;
  13398. @@ -1536,11 +1566,14 @@ struct task_struct {
  13399. cputime_t gtime;
  13400. struct prev_cputime prev_cputime;
  13401. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  13402. - seqlock_t vtime_seqlock;
  13403. + seqcount_t vtime_seqcount;
  13404. unsigned long long vtime_snap;
  13405. enum {
  13406. - VTIME_SLEEPING = 0,
  13407. + /* Task is sleeping or running in a CPU with VTIME inactive */
  13408. + VTIME_INACTIVE = 0,
  13409. + /* Task runs in userspace in a CPU with VTIME active */
  13410. VTIME_USER,
  13411. + /* Task runs in kernelspace in a CPU with VTIME active */
  13412. VTIME_SYS,
  13413. } vtime_snap_whence;
  13414. #endif
  13415. @@ -1552,6 +1585,9 @@ struct task_struct {
  13416. struct task_cputime cputime_expires;
  13417. struct list_head cpu_timers[3];
  13418. +#ifdef CONFIG_PREEMPT_RT_BASE
  13419. + struct task_struct *posix_timer_list;
  13420. +#endif
  13421. /* process credentials */
  13422. const struct cred __rcu *ptracer_cred; /* Tracer's credentials at attach */
  13423. @@ -1583,10 +1619,15 @@ struct task_struct {
  13424. /* signal handlers */
  13425. struct signal_struct *signal;
  13426. struct sighand_struct *sighand;
  13427. + struct sigqueue *sigqueue_cache;
  13428. sigset_t blocked, real_blocked;
  13429. sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
  13430. struct sigpending pending;
  13431. +#ifdef CONFIG_PREEMPT_RT_FULL
  13432. + /* TODO: move me into ->restart_block ? */
  13433. + struct siginfo forced_info;
  13434. +#endif
  13435. unsigned long sas_ss_sp;
  13436. size_t sas_ss_size;
  13437. @@ -1611,6 +1652,7 @@ struct task_struct {
  13438. raw_spinlock_t pi_lock;
  13439. struct wake_q_node wake_q;
  13440. + struct wake_q_node wake_q_sleeper;
  13441. #ifdef CONFIG_RT_MUTEXES
  13442. /* PI waiters blocked on a rt_mutex held by this task */
  13443. @@ -1810,6 +1852,12 @@ struct task_struct {
  13444. unsigned long trace;
  13445. /* bitmask and counter of trace recursion */
  13446. unsigned long trace_recursion;
  13447. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  13448. + u64 preempt_timestamp_hist;
  13449. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  13450. + long timer_offset;
  13451. +#endif
  13452. +#endif
  13453. #endif /* CONFIG_TRACING */
  13454. #ifdef CONFIG_MEMCG
  13455. struct mem_cgroup *memcg_in_oom;
  13456. @@ -1826,8 +1874,22 @@ struct task_struct {
  13457. unsigned int sequential_io;
  13458. unsigned int sequential_io_avg;
  13459. #endif
  13460. +#ifdef CONFIG_PREEMPT_RT_BASE
  13461. + struct rcu_head put_rcu;
  13462. + int softirq_nestcnt;
  13463. + unsigned int softirqs_raised;
  13464. +#endif
  13465. +#ifdef CONFIG_PREEMPT_RT_FULL
  13466. +# if defined CONFIG_HIGHMEM || defined CONFIG_X86_32
  13467. + int kmap_idx;
  13468. + pte_t kmap_pte[KM_TYPE_NR];
  13469. +# endif
  13470. +#endif
  13471. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  13472. unsigned long task_state_change;
  13473. +#endif
  13474. +#ifdef CONFIG_PREEMPT_RT_FULL
  13475. + int xmit_recursion;
  13476. #endif
  13477. int pagefault_disabled;
  13478. /* CPU-specific state of this task */
  13479. @@ -1846,9 +1908,6 @@ extern int arch_task_struct_size __read_mostly;
  13480. # define arch_task_struct_size (sizeof(struct task_struct))
  13481. #endif
  13482. -/* Future-safe accessor for struct task_struct's cpus_allowed. */
  13483. -#define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
  13484. -
  13485. #define TNF_MIGRATED 0x01
  13486. #define TNF_NO_GROUP 0x02
  13487. #define TNF_SHARED 0x04
  13488. @@ -2042,6 +2101,15 @@ extern struct pid *cad_pid;
  13489. extern void free_task(struct task_struct *tsk);
  13490. #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
  13491. +#ifdef CONFIG_PREEMPT_RT_BASE
  13492. +extern void __put_task_struct_cb(struct rcu_head *rhp);
  13493. +
  13494. +static inline void put_task_struct(struct task_struct *t)
  13495. +{
  13496. + if (atomic_dec_and_test(&t->usage))
  13497. + call_rcu(&t->put_rcu, __put_task_struct_cb);
  13498. +}
  13499. +#else
  13500. extern void __put_task_struct(struct task_struct *t);
  13501. static inline void put_task_struct(struct task_struct *t)
  13502. @@ -2049,6 +2117,7 @@ static inline void put_task_struct(struct task_struct *t)
  13503. if (atomic_dec_and_test(&t->usage))
  13504. __put_task_struct(t);
  13505. }
  13506. +#endif
  13507. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  13508. extern void task_cputime(struct task_struct *t,
  13509. @@ -2087,6 +2156,7 @@ extern void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut,
  13510. /*
  13511. * Per process flags
  13512. */
  13513. +#define PF_IN_SOFTIRQ 0x00000001 /* Task is serving softirq */
  13514. #define PF_EXITING 0x00000004 /* getting shut down */
  13515. #define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
  13516. #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
  13517. @@ -2251,6 +2321,10 @@ extern void do_set_cpus_allowed(struct task_struct *p,
  13518. extern int set_cpus_allowed_ptr(struct task_struct *p,
  13519. const struct cpumask *new_mask);
  13520. +int migrate_me(void);
  13521. +void tell_sched_cpu_down_begin(int cpu);
  13522. +void tell_sched_cpu_down_done(int cpu);
  13523. +
  13524. #else
  13525. static inline void do_set_cpus_allowed(struct task_struct *p,
  13526. const struct cpumask *new_mask)
  13527. @@ -2263,6 +2337,9 @@ static inline int set_cpus_allowed_ptr(struct task_struct *p,
  13528. return -EINVAL;
  13529. return 0;
  13530. }
  13531. +static inline int migrate_me(void) { return 0; }
  13532. +static inline void tell_sched_cpu_down_begin(int cpu) { }
  13533. +static inline void tell_sched_cpu_down_done(int cpu) { }
  13534. #endif
  13535. #ifdef CONFIG_NO_HZ_COMMON
  13536. @@ -2472,6 +2549,7 @@ extern void xtime_update(unsigned long ticks);
  13537. extern int wake_up_state(struct task_struct *tsk, unsigned int state);
  13538. extern int wake_up_process(struct task_struct *tsk);
  13539. +extern int wake_up_lock_sleeper(struct task_struct * tsk);
  13540. extern void wake_up_new_task(struct task_struct *tsk);
  13541. #ifdef CONFIG_SMP
  13542. extern void kick_process(struct task_struct *tsk);
  13543. @@ -2595,12 +2673,24 @@ extern struct mm_struct * mm_alloc(void);
  13544. /* mmdrop drops the mm and the page tables */
  13545. extern void __mmdrop(struct mm_struct *);
  13546. +
  13547. static inline void mmdrop(struct mm_struct * mm)
  13548. {
  13549. if (unlikely(atomic_dec_and_test(&mm->mm_count)))
  13550. __mmdrop(mm);
  13551. }
  13552. +#ifdef CONFIG_PREEMPT_RT_BASE
  13553. +extern void __mmdrop_delayed(struct rcu_head *rhp);
  13554. +static inline void mmdrop_delayed(struct mm_struct *mm)
  13555. +{
  13556. + if (atomic_dec_and_test(&mm->mm_count))
  13557. + call_rcu(&mm->delayed_drop, __mmdrop_delayed);
  13558. +}
  13559. +#else
  13560. +# define mmdrop_delayed(mm) mmdrop(mm)
  13561. +#endif
  13562. +
  13563. /* mmput gets rid of the mappings and all user-space */
  13564. extern void mmput(struct mm_struct *);
  13565. /* Grab a reference to a task's mm, if it is not already going away */
  13566. @@ -2910,6 +3000,43 @@ static inline int test_tsk_need_resched(struct task_struct *tsk)
  13567. return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
  13568. }
  13569. +#ifdef CONFIG_PREEMPT_LAZY
  13570. +static inline void set_tsk_need_resched_lazy(struct task_struct *tsk)
  13571. +{
  13572. + set_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  13573. +}
  13574. +
  13575. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk)
  13576. +{
  13577. + clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  13578. +}
  13579. +
  13580. +static inline int test_tsk_need_resched_lazy(struct task_struct *tsk)
  13581. +{
  13582. + return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY));
  13583. +}
  13584. +
  13585. +static inline int need_resched_lazy(void)
  13586. +{
  13587. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  13588. +}
  13589. +
  13590. +static inline int need_resched_now(void)
  13591. +{
  13592. + return test_thread_flag(TIF_NEED_RESCHED);
  13593. +}
  13594. +
  13595. +#else
  13596. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk) { }
  13597. +static inline int need_resched_lazy(void) { return 0; }
  13598. +
  13599. +static inline int need_resched_now(void)
  13600. +{
  13601. + return test_thread_flag(TIF_NEED_RESCHED);
  13602. +}
  13603. +
  13604. +#endif
  13605. +
  13606. static inline int restart_syscall(void)
  13607. {
  13608. set_tsk_thread_flag(current, TIF_SIGPENDING);
  13609. @@ -2941,6 +3068,51 @@ static inline int signal_pending_state(long state, struct task_struct *p)
  13610. return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
  13611. }
  13612. +static inline bool __task_is_stopped_or_traced(struct task_struct *task)
  13613. +{
  13614. + if (task->state & (__TASK_STOPPED | __TASK_TRACED))
  13615. + return true;
  13616. +#ifdef CONFIG_PREEMPT_RT_FULL
  13617. + if (task->saved_state & (__TASK_STOPPED | __TASK_TRACED))
  13618. + return true;
  13619. +#endif
  13620. + return false;
  13621. +}
  13622. +
  13623. +static inline bool task_is_stopped_or_traced(struct task_struct *task)
  13624. +{
  13625. + bool traced_stopped;
  13626. +
  13627. +#ifdef CONFIG_PREEMPT_RT_FULL
  13628. + unsigned long flags;
  13629. +
  13630. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  13631. + traced_stopped = __task_is_stopped_or_traced(task);
  13632. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  13633. +#else
  13634. + traced_stopped = __task_is_stopped_or_traced(task);
  13635. +#endif
  13636. + return traced_stopped;
  13637. +}
  13638. +
  13639. +static inline bool task_is_traced(struct task_struct *task)
  13640. +{
  13641. + bool traced = false;
  13642. +
  13643. + if (task->state & __TASK_TRACED)
  13644. + return true;
  13645. +#ifdef CONFIG_PREEMPT_RT_FULL
  13646. + /* in case the task is sleeping on tasklist_lock */
  13647. + raw_spin_lock_irq(&task->pi_lock);
  13648. + if (task->state & __TASK_TRACED)
  13649. + traced = true;
  13650. + else if (task->saved_state & __TASK_TRACED)
  13651. + traced = true;
  13652. + raw_spin_unlock_irq(&task->pi_lock);
  13653. +#endif
  13654. + return traced;
  13655. +}
  13656. +
  13657. /*
  13658. * cond_resched() and cond_resched_lock(): latency reduction via
  13659. * explicit rescheduling in places that are safe. The return
  13660. @@ -2962,12 +3134,16 @@ extern int __cond_resched_lock(spinlock_t *lock);
  13661. __cond_resched_lock(lock); \
  13662. })
  13663. +#ifndef CONFIG_PREEMPT_RT_FULL
  13664. extern int __cond_resched_softirq(void);
  13665. #define cond_resched_softirq() ({ \
  13666. ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
  13667. __cond_resched_softirq(); \
  13668. })
  13669. +#else
  13670. +# define cond_resched_softirq() cond_resched()
  13671. +#endif
  13672. static inline void cond_resched_rcu(void)
  13673. {
  13674. @@ -3129,6 +3305,31 @@ static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
  13675. #endif /* CONFIG_SMP */
  13676. +static inline int __migrate_disabled(struct task_struct *p)
  13677. +{
  13678. +#ifdef CONFIG_PREEMPT_RT_FULL
  13679. + return p->migrate_disable;
  13680. +#else
  13681. + return 0;
  13682. +#endif
  13683. +}
  13684. +
  13685. +/* Future-safe accessor for struct task_struct's cpus_allowed. */
  13686. +static inline const struct cpumask *tsk_cpus_allowed(struct task_struct *p)
  13687. +{
  13688. + if (__migrate_disabled(p))
  13689. + return cpumask_of(task_cpu(p));
  13690. +
  13691. + return &p->cpus_allowed;
  13692. +}
  13693. +
  13694. +static inline int tsk_nr_cpus_allowed(struct task_struct *p)
  13695. +{
  13696. + if (__migrate_disabled(p))
  13697. + return 1;
  13698. + return p->nr_cpus_allowed;
  13699. +}
  13700. +
  13701. extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
  13702. extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
  13703. diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h
  13704. index e0582106ef4f..b14f4d2368aa 100644
  13705. --- a/include/linux/seqlock.h
  13706. +++ b/include/linux/seqlock.h
  13707. @@ -220,20 +220,30 @@ static inline int read_seqcount_retry(const seqcount_t *s, unsigned start)
  13708. return __read_seqcount_retry(s, start);
  13709. }
  13710. -
  13711. -
  13712. -static inline void raw_write_seqcount_begin(seqcount_t *s)
  13713. +static inline void __raw_write_seqcount_begin(seqcount_t *s)
  13714. {
  13715. s->sequence++;
  13716. smp_wmb();
  13717. }
  13718. -static inline void raw_write_seqcount_end(seqcount_t *s)
  13719. +static inline void raw_write_seqcount_begin(seqcount_t *s)
  13720. +{
  13721. + preempt_disable_rt();
  13722. + __raw_write_seqcount_begin(s);
  13723. +}
  13724. +
  13725. +static inline void __raw_write_seqcount_end(seqcount_t *s)
  13726. {
  13727. smp_wmb();
  13728. s->sequence++;
  13729. }
  13730. +static inline void raw_write_seqcount_end(seqcount_t *s)
  13731. +{
  13732. + __raw_write_seqcount_end(s);
  13733. + preempt_enable_rt();
  13734. +}
  13735. +
  13736. /**
  13737. * raw_write_seqcount_barrier - do a seq write barrier
  13738. * @s: pointer to seqcount_t
  13739. @@ -425,10 +435,32 @@ typedef struct {
  13740. /*
  13741. * Read side functions for starting and finalizing a read side section.
  13742. */
  13743. +#ifndef CONFIG_PREEMPT_RT_FULL
  13744. static inline unsigned read_seqbegin(const seqlock_t *sl)
  13745. {
  13746. return read_seqcount_begin(&sl->seqcount);
  13747. }
  13748. +#else
  13749. +/*
  13750. + * Starvation safe read side for RT
  13751. + */
  13752. +static inline unsigned read_seqbegin(seqlock_t *sl)
  13753. +{
  13754. + unsigned ret;
  13755. +
  13756. +repeat:
  13757. + ret = ACCESS_ONCE(sl->seqcount.sequence);
  13758. + if (unlikely(ret & 1)) {
  13759. + /*
  13760. + * Take the lock and let the writer proceed (i.e. evtl
  13761. + * boost it), otherwise we could loop here forever.
  13762. + */
  13763. + spin_unlock_wait(&sl->lock);
  13764. + goto repeat;
  13765. + }
  13766. + return ret;
  13767. +}
  13768. +#endif
  13769. static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  13770. {
  13771. @@ -443,36 +475,36 @@ static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  13772. static inline void write_seqlock(seqlock_t *sl)
  13773. {
  13774. spin_lock(&sl->lock);
  13775. - write_seqcount_begin(&sl->seqcount);
  13776. + __raw_write_seqcount_begin(&sl->seqcount);
  13777. }
  13778. static inline void write_sequnlock(seqlock_t *sl)
  13779. {
  13780. - write_seqcount_end(&sl->seqcount);
  13781. + __raw_write_seqcount_end(&sl->seqcount);
  13782. spin_unlock(&sl->lock);
  13783. }
  13784. static inline void write_seqlock_bh(seqlock_t *sl)
  13785. {
  13786. spin_lock_bh(&sl->lock);
  13787. - write_seqcount_begin(&sl->seqcount);
  13788. + __raw_write_seqcount_begin(&sl->seqcount);
  13789. }
  13790. static inline void write_sequnlock_bh(seqlock_t *sl)
  13791. {
  13792. - write_seqcount_end(&sl->seqcount);
  13793. + __raw_write_seqcount_end(&sl->seqcount);
  13794. spin_unlock_bh(&sl->lock);
  13795. }
  13796. static inline void write_seqlock_irq(seqlock_t *sl)
  13797. {
  13798. spin_lock_irq(&sl->lock);
  13799. - write_seqcount_begin(&sl->seqcount);
  13800. + __raw_write_seqcount_begin(&sl->seqcount);
  13801. }
  13802. static inline void write_sequnlock_irq(seqlock_t *sl)
  13803. {
  13804. - write_seqcount_end(&sl->seqcount);
  13805. + __raw_write_seqcount_end(&sl->seqcount);
  13806. spin_unlock_irq(&sl->lock);
  13807. }
  13808. @@ -481,7 +513,7 @@ static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
  13809. unsigned long flags;
  13810. spin_lock_irqsave(&sl->lock, flags);
  13811. - write_seqcount_begin(&sl->seqcount);
  13812. + __raw_write_seqcount_begin(&sl->seqcount);
  13813. return flags;
  13814. }
  13815. @@ -491,7 +523,7 @@ static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
  13816. static inline void
  13817. write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
  13818. {
  13819. - write_seqcount_end(&sl->seqcount);
  13820. + __raw_write_seqcount_end(&sl->seqcount);
  13821. spin_unlock_irqrestore(&sl->lock, flags);
  13822. }
  13823. diff --git a/include/linux/signal.h b/include/linux/signal.h
  13824. index d80259afb9e5..ddd1e6866a54 100644
  13825. --- a/include/linux/signal.h
  13826. +++ b/include/linux/signal.h
  13827. @@ -233,6 +233,7 @@ static inline void init_sigpending(struct sigpending *sig)
  13828. }
  13829. extern void flush_sigqueue(struct sigpending *queue);
  13830. +extern void flush_task_sigqueue(struct task_struct *tsk);
  13831. /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
  13832. static inline int valid_signal(unsigned long sig)
  13833. diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
  13834. index b5421f6f155a..b05be76fc504 100644
  13835. --- a/include/linux/skbuff.h
  13836. +++ b/include/linux/skbuff.h
  13837. @@ -203,6 +203,7 @@ struct sk_buff_head {
  13838. __u32 qlen;
  13839. spinlock_t lock;
  13840. + raw_spinlock_t raw_lock;
  13841. };
  13842. struct sk_buff;
  13843. @@ -1462,6 +1463,12 @@ static inline void skb_queue_head_init(struct sk_buff_head *list)
  13844. __skb_queue_head_init(list);
  13845. }
  13846. +static inline void skb_queue_head_init_raw(struct sk_buff_head *list)
  13847. +{
  13848. + raw_spin_lock_init(&list->raw_lock);
  13849. + __skb_queue_head_init(list);
  13850. +}
  13851. +
  13852. static inline void skb_queue_head_init_class(struct sk_buff_head *list,
  13853. struct lock_class_key *class)
  13854. {
  13855. diff --git a/include/linux/smp.h b/include/linux/smp.h
  13856. index c4414074bd88..e6ab36aeaaab 100644
  13857. --- a/include/linux/smp.h
  13858. +++ b/include/linux/smp.h
  13859. @@ -185,6 +185,9 @@ static inline void smp_init(void) { }
  13860. #define get_cpu() ({ preempt_disable(); smp_processor_id(); })
  13861. #define put_cpu() preempt_enable()
  13862. +#define get_cpu_light() ({ migrate_disable(); smp_processor_id(); })
  13863. +#define put_cpu_light() migrate_enable()
  13864. +
  13865. /*
  13866. * Callback to arch code if there's nosmp or maxcpus=0 on the
  13867. * boot command line:
  13868. diff --git a/include/linux/spinlock.h b/include/linux/spinlock.h
  13869. index 47dd0cebd204..02928fa5499d 100644
  13870. --- a/include/linux/spinlock.h
  13871. +++ b/include/linux/spinlock.h
  13872. @@ -271,7 +271,11 @@ static inline void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock)
  13873. #define raw_spin_can_lock(lock) (!raw_spin_is_locked(lock))
  13874. /* Include rwlock functions */
  13875. -#include <linux/rwlock.h>
  13876. +#ifdef CONFIG_PREEMPT_RT_FULL
  13877. +# include <linux/rwlock_rt.h>
  13878. +#else
  13879. +# include <linux/rwlock.h>
  13880. +#endif
  13881. /*
  13882. * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
  13883. @@ -282,6 +286,10 @@ static inline void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock)
  13884. # include <linux/spinlock_api_up.h>
  13885. #endif
  13886. +#ifdef CONFIG_PREEMPT_RT_FULL
  13887. +# include <linux/spinlock_rt.h>
  13888. +#else /* PREEMPT_RT_FULL */
  13889. +
  13890. /*
  13891. * Map the spin_lock functions to the raw variants for PREEMPT_RT=n
  13892. */
  13893. @@ -347,6 +355,12 @@ static __always_inline void spin_unlock(spinlock_t *lock)
  13894. raw_spin_unlock(&lock->rlock);
  13895. }
  13896. +static __always_inline int spin_unlock_no_deboost(spinlock_t *lock)
  13897. +{
  13898. + raw_spin_unlock(&lock->rlock);
  13899. + return 0;
  13900. +}
  13901. +
  13902. static __always_inline void spin_unlock_bh(spinlock_t *lock)
  13903. {
  13904. raw_spin_unlock_bh(&lock->rlock);
  13905. @@ -416,4 +430,6 @@ extern int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
  13906. #define atomic_dec_and_lock(atomic, lock) \
  13907. __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
  13908. +#endif /* !PREEMPT_RT_FULL */
  13909. +
  13910. #endif /* __LINUX_SPINLOCK_H */
  13911. diff --git a/include/linux/spinlock_api_smp.h b/include/linux/spinlock_api_smp.h
  13912. index 5344268e6e62..043263f30e81 100644
  13913. --- a/include/linux/spinlock_api_smp.h
  13914. +++ b/include/linux/spinlock_api_smp.h
  13915. @@ -189,6 +189,8 @@ static inline int __raw_spin_trylock_bh(raw_spinlock_t *lock)
  13916. return 0;
  13917. }
  13918. -#include <linux/rwlock_api_smp.h>
  13919. +#ifndef CONFIG_PREEMPT_RT_FULL
  13920. +# include <linux/rwlock_api_smp.h>
  13921. +#endif
  13922. #endif /* __LINUX_SPINLOCK_API_SMP_H */
  13923. diff --git a/include/linux/spinlock_rt.h b/include/linux/spinlock_rt.h
  13924. new file mode 100644
  13925. index 000000000000..7eb87584e843
  13926. --- /dev/null
  13927. +++ b/include/linux/spinlock_rt.h
  13928. @@ -0,0 +1,165 @@
  13929. +#ifndef __LINUX_SPINLOCK_RT_H
  13930. +#define __LINUX_SPINLOCK_RT_H
  13931. +
  13932. +#ifndef __LINUX_SPINLOCK_H
  13933. +#error Do not include directly. Use spinlock.h
  13934. +#endif
  13935. +
  13936. +#include <linux/bug.h>
  13937. +
  13938. +extern void
  13939. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key);
  13940. +
  13941. +#define spin_lock_init(slock) \
  13942. +do { \
  13943. + static struct lock_class_key __key; \
  13944. + \
  13945. + rt_mutex_init(&(slock)->lock); \
  13946. + __rt_spin_lock_init(slock, #slock, &__key); \
  13947. +} while (0)
  13948. +
  13949. +void __lockfunc rt_spin_lock__no_mg(spinlock_t *lock);
  13950. +void __lockfunc rt_spin_unlock__no_mg(spinlock_t *lock);
  13951. +int __lockfunc rt_spin_trylock__no_mg(spinlock_t *lock);
  13952. +
  13953. +extern void __lockfunc rt_spin_lock(spinlock_t *lock);
  13954. +extern unsigned long __lockfunc rt_spin_lock_trace_flags(spinlock_t *lock);
  13955. +extern void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass);
  13956. +extern void __lockfunc rt_spin_unlock(spinlock_t *lock);
  13957. +extern int __lockfunc rt_spin_unlock_no_deboost(spinlock_t *lock);
  13958. +extern void __lockfunc rt_spin_unlock_wait(spinlock_t *lock);
  13959. +extern int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags);
  13960. +extern int __lockfunc rt_spin_trylock_bh(spinlock_t *lock);
  13961. +extern int __lockfunc rt_spin_trylock(spinlock_t *lock);
  13962. +extern int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock);
  13963. +
  13964. +/*
  13965. + * lockdep-less calls, for derived types like rwlock:
  13966. + * (for trylock they can use rt_mutex_trylock() directly.
  13967. + */
  13968. +extern void __lockfunc __rt_spin_lock__no_mg(struct rt_mutex *lock);
  13969. +extern void __lockfunc __rt_spin_lock(struct rt_mutex *lock);
  13970. +extern void __lockfunc __rt_spin_unlock(struct rt_mutex *lock);
  13971. +extern int __lockfunc __rt_spin_trylock(struct rt_mutex *lock);
  13972. +
  13973. +#define spin_lock(lock) rt_spin_lock(lock)
  13974. +
  13975. +#define spin_lock_bh(lock) \
  13976. + do { \
  13977. + local_bh_disable(); \
  13978. + rt_spin_lock(lock); \
  13979. + } while (0)
  13980. +
  13981. +#define spin_lock_irq(lock) spin_lock(lock)
  13982. +
  13983. +#define spin_do_trylock(lock) __cond_lock(lock, rt_spin_trylock(lock))
  13984. +
  13985. +#define spin_trylock(lock) \
  13986. +({ \
  13987. + int __locked; \
  13988. + __locked = spin_do_trylock(lock); \
  13989. + __locked; \
  13990. +})
  13991. +
  13992. +#ifdef CONFIG_LOCKDEP
  13993. +# define spin_lock_nested(lock, subclass) \
  13994. + do { \
  13995. + rt_spin_lock_nested(lock, subclass); \
  13996. + } while (0)
  13997. +
  13998. +#define spin_lock_bh_nested(lock, subclass) \
  13999. + do { \
  14000. + local_bh_disable(); \
  14001. + rt_spin_lock_nested(lock, subclass); \
  14002. + } while (0)
  14003. +
  14004. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  14005. + do { \
  14006. + typecheck(unsigned long, flags); \
  14007. + flags = 0; \
  14008. + rt_spin_lock_nested(lock, subclass); \
  14009. + } while (0)
  14010. +#else
  14011. +# define spin_lock_nested(lock, subclass) spin_lock(lock)
  14012. +# define spin_lock_bh_nested(lock, subclass) spin_lock_bh(lock)
  14013. +
  14014. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  14015. + do { \
  14016. + typecheck(unsigned long, flags); \
  14017. + flags = 0; \
  14018. + spin_lock(lock); \
  14019. + } while (0)
  14020. +#endif
  14021. +
  14022. +#define spin_lock_irqsave(lock, flags) \
  14023. + do { \
  14024. + typecheck(unsigned long, flags); \
  14025. + flags = 0; \
  14026. + spin_lock(lock); \
  14027. + } while (0)
  14028. +
  14029. +static inline unsigned long spin_lock_trace_flags(spinlock_t *lock)
  14030. +{
  14031. + unsigned long flags = 0;
  14032. +#ifdef CONFIG_TRACE_IRQFLAGS
  14033. + flags = rt_spin_lock_trace_flags(lock);
  14034. +#else
  14035. + spin_lock(lock); /* lock_local */
  14036. +#endif
  14037. + return flags;
  14038. +}
  14039. +
  14040. +/* FIXME: we need rt_spin_lock_nest_lock */
  14041. +#define spin_lock_nest_lock(lock, nest_lock) spin_lock_nested(lock, 0)
  14042. +
  14043. +#define spin_unlock(lock) rt_spin_unlock(lock)
  14044. +#define spin_unlock_no_deboost(lock) rt_spin_unlock_no_deboost(lock)
  14045. +
  14046. +#define spin_unlock_bh(lock) \
  14047. + do { \
  14048. + rt_spin_unlock(lock); \
  14049. + local_bh_enable(); \
  14050. + } while (0)
  14051. +
  14052. +#define spin_unlock_irq(lock) spin_unlock(lock)
  14053. +
  14054. +#define spin_unlock_irqrestore(lock, flags) \
  14055. + do { \
  14056. + typecheck(unsigned long, flags); \
  14057. + (void) flags; \
  14058. + spin_unlock(lock); \
  14059. + } while (0)
  14060. +
  14061. +#define spin_trylock_bh(lock) __cond_lock(lock, rt_spin_trylock_bh(lock))
  14062. +#define spin_trylock_irq(lock) spin_trylock(lock)
  14063. +
  14064. +#define spin_trylock_irqsave(lock, flags) \
  14065. + rt_spin_trylock_irqsave(lock, &(flags))
  14066. +
  14067. +#define spin_unlock_wait(lock) rt_spin_unlock_wait(lock)
  14068. +
  14069. +#ifdef CONFIG_GENERIC_LOCKBREAK
  14070. +# define spin_is_contended(lock) ((lock)->break_lock)
  14071. +#else
  14072. +# define spin_is_contended(lock) (((void)(lock), 0))
  14073. +#endif
  14074. +
  14075. +static inline int spin_can_lock(spinlock_t *lock)
  14076. +{
  14077. + return !rt_mutex_is_locked(&lock->lock);
  14078. +}
  14079. +
  14080. +static inline int spin_is_locked(spinlock_t *lock)
  14081. +{
  14082. + return rt_mutex_is_locked(&lock->lock);
  14083. +}
  14084. +
  14085. +static inline void assert_spin_locked(spinlock_t *lock)
  14086. +{
  14087. + BUG_ON(!spin_is_locked(lock));
  14088. +}
  14089. +
  14090. +#define atomic_dec_and_lock(atomic, lock) \
  14091. + atomic_dec_and_spin_lock(atomic, lock)
  14092. +
  14093. +#endif
  14094. diff --git a/include/linux/spinlock_types.h b/include/linux/spinlock_types.h
  14095. index 73548eb13a5d..10bac715ea96 100644
  14096. --- a/include/linux/spinlock_types.h
  14097. +++ b/include/linux/spinlock_types.h
  14098. @@ -9,80 +9,15 @@
  14099. * Released under the General Public License (GPL).
  14100. */
  14101. -#if defined(CONFIG_SMP)
  14102. -# include <asm/spinlock_types.h>
  14103. -#else
  14104. -# include <linux/spinlock_types_up.h>
  14105. -#endif
  14106. -
  14107. -#include <linux/lockdep.h>
  14108. -
  14109. -typedef struct raw_spinlock {
  14110. - arch_spinlock_t raw_lock;
  14111. -#ifdef CONFIG_GENERIC_LOCKBREAK
  14112. - unsigned int break_lock;
  14113. -#endif
  14114. -#ifdef CONFIG_DEBUG_SPINLOCK
  14115. - unsigned int magic, owner_cpu;
  14116. - void *owner;
  14117. -#endif
  14118. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14119. - struct lockdep_map dep_map;
  14120. -#endif
  14121. -} raw_spinlock_t;
  14122. -
  14123. -#define SPINLOCK_MAGIC 0xdead4ead
  14124. -
  14125. -#define SPINLOCK_OWNER_INIT ((void *)-1L)
  14126. -
  14127. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14128. -# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  14129. -#else
  14130. -# define SPIN_DEP_MAP_INIT(lockname)
  14131. -#endif
  14132. +#include <linux/spinlock_types_raw.h>
  14133. -#ifdef CONFIG_DEBUG_SPINLOCK
  14134. -# define SPIN_DEBUG_INIT(lockname) \
  14135. - .magic = SPINLOCK_MAGIC, \
  14136. - .owner_cpu = -1, \
  14137. - .owner = SPINLOCK_OWNER_INIT,
  14138. +#ifndef CONFIG_PREEMPT_RT_FULL
  14139. +# include <linux/spinlock_types_nort.h>
  14140. +# include <linux/rwlock_types.h>
  14141. #else
  14142. -# define SPIN_DEBUG_INIT(lockname)
  14143. +# include <linux/rtmutex.h>
  14144. +# include <linux/spinlock_types_rt.h>
  14145. +# include <linux/rwlock_types_rt.h>
  14146. #endif
  14147. -#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  14148. - { \
  14149. - .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  14150. - SPIN_DEBUG_INIT(lockname) \
  14151. - SPIN_DEP_MAP_INIT(lockname) }
  14152. -
  14153. -#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  14154. - (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  14155. -
  14156. -#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  14157. -
  14158. -typedef struct spinlock {
  14159. - union {
  14160. - struct raw_spinlock rlock;
  14161. -
  14162. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14163. -# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  14164. - struct {
  14165. - u8 __padding[LOCK_PADSIZE];
  14166. - struct lockdep_map dep_map;
  14167. - };
  14168. -#endif
  14169. - };
  14170. -} spinlock_t;
  14171. -
  14172. -#define __SPIN_LOCK_INITIALIZER(lockname) \
  14173. - { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  14174. -
  14175. -#define __SPIN_LOCK_UNLOCKED(lockname) \
  14176. - (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  14177. -
  14178. -#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  14179. -
  14180. -#include <linux/rwlock_types.h>
  14181. -
  14182. #endif /* __LINUX_SPINLOCK_TYPES_H */
  14183. diff --git a/include/linux/spinlock_types_nort.h b/include/linux/spinlock_types_nort.h
  14184. new file mode 100644
  14185. index 000000000000..f1dac1fb1d6a
  14186. --- /dev/null
  14187. +++ b/include/linux/spinlock_types_nort.h
  14188. @@ -0,0 +1,33 @@
  14189. +#ifndef __LINUX_SPINLOCK_TYPES_NORT_H
  14190. +#define __LINUX_SPINLOCK_TYPES_NORT_H
  14191. +
  14192. +#ifndef __LINUX_SPINLOCK_TYPES_H
  14193. +#error "Do not include directly. Include spinlock_types.h instead"
  14194. +#endif
  14195. +
  14196. +/*
  14197. + * The non RT version maps spinlocks to raw_spinlocks
  14198. + */
  14199. +typedef struct spinlock {
  14200. + union {
  14201. + struct raw_spinlock rlock;
  14202. +
  14203. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14204. +# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  14205. + struct {
  14206. + u8 __padding[LOCK_PADSIZE];
  14207. + struct lockdep_map dep_map;
  14208. + };
  14209. +#endif
  14210. + };
  14211. +} spinlock_t;
  14212. +
  14213. +#define __SPIN_LOCK_INITIALIZER(lockname) \
  14214. + { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  14215. +
  14216. +#define __SPIN_LOCK_UNLOCKED(lockname) \
  14217. + (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  14218. +
  14219. +#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  14220. +
  14221. +#endif
  14222. diff --git a/include/linux/spinlock_types_raw.h b/include/linux/spinlock_types_raw.h
  14223. new file mode 100644
  14224. index 000000000000..edffc4d53fc9
  14225. --- /dev/null
  14226. +++ b/include/linux/spinlock_types_raw.h
  14227. @@ -0,0 +1,56 @@
  14228. +#ifndef __LINUX_SPINLOCK_TYPES_RAW_H
  14229. +#define __LINUX_SPINLOCK_TYPES_RAW_H
  14230. +
  14231. +#if defined(CONFIG_SMP)
  14232. +# include <asm/spinlock_types.h>
  14233. +#else
  14234. +# include <linux/spinlock_types_up.h>
  14235. +#endif
  14236. +
  14237. +#include <linux/lockdep.h>
  14238. +
  14239. +typedef struct raw_spinlock {
  14240. + arch_spinlock_t raw_lock;
  14241. +#ifdef CONFIG_GENERIC_LOCKBREAK
  14242. + unsigned int break_lock;
  14243. +#endif
  14244. +#ifdef CONFIG_DEBUG_SPINLOCK
  14245. + unsigned int magic, owner_cpu;
  14246. + void *owner;
  14247. +#endif
  14248. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14249. + struct lockdep_map dep_map;
  14250. +#endif
  14251. +} raw_spinlock_t;
  14252. +
  14253. +#define SPINLOCK_MAGIC 0xdead4ead
  14254. +
  14255. +#define SPINLOCK_OWNER_INIT ((void *)-1L)
  14256. +
  14257. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14258. +# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  14259. +#else
  14260. +# define SPIN_DEP_MAP_INIT(lockname)
  14261. +#endif
  14262. +
  14263. +#ifdef CONFIG_DEBUG_SPINLOCK
  14264. +# define SPIN_DEBUG_INIT(lockname) \
  14265. + .magic = SPINLOCK_MAGIC, \
  14266. + .owner_cpu = -1, \
  14267. + .owner = SPINLOCK_OWNER_INIT,
  14268. +#else
  14269. +# define SPIN_DEBUG_INIT(lockname)
  14270. +#endif
  14271. +
  14272. +#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  14273. + { \
  14274. + .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  14275. + SPIN_DEBUG_INIT(lockname) \
  14276. + SPIN_DEP_MAP_INIT(lockname) }
  14277. +
  14278. +#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  14279. + (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  14280. +
  14281. +#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  14282. +
  14283. +#endif
  14284. diff --git a/include/linux/spinlock_types_rt.h b/include/linux/spinlock_types_rt.h
  14285. new file mode 100644
  14286. index 000000000000..9fd431967abc
  14287. --- /dev/null
  14288. +++ b/include/linux/spinlock_types_rt.h
  14289. @@ -0,0 +1,51 @@
  14290. +#ifndef __LINUX_SPINLOCK_TYPES_RT_H
  14291. +#define __LINUX_SPINLOCK_TYPES_RT_H
  14292. +
  14293. +#ifndef __LINUX_SPINLOCK_TYPES_H
  14294. +#error "Do not include directly. Include spinlock_types.h instead"
  14295. +#endif
  14296. +
  14297. +#include <linux/cache.h>
  14298. +
  14299. +/*
  14300. + * PREEMPT_RT: spinlocks - an RT mutex plus lock-break field:
  14301. + */
  14302. +typedef struct spinlock {
  14303. + struct rt_mutex lock;
  14304. + unsigned int break_lock;
  14305. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14306. + struct lockdep_map dep_map;
  14307. +#endif
  14308. +} spinlock_t;
  14309. +
  14310. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  14311. +# define __RT_SPIN_INITIALIZER(name) \
  14312. + { \
  14313. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  14314. + .save_state = 1, \
  14315. + .file = __FILE__, \
  14316. + .line = __LINE__ , \
  14317. + }
  14318. +#else
  14319. +# define __RT_SPIN_INITIALIZER(name) \
  14320. + { \
  14321. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  14322. + .save_state = 1, \
  14323. + }
  14324. +#endif
  14325. +
  14326. +/*
  14327. +.wait_list = PLIST_HEAD_INIT_RAW((name).lock.wait_list, (name).lock.wait_lock)
  14328. +*/
  14329. +
  14330. +#define __SPIN_LOCK_UNLOCKED(name) \
  14331. + { .lock = __RT_SPIN_INITIALIZER(name.lock), \
  14332. + SPIN_DEP_MAP_INIT(name) }
  14333. +
  14334. +#define __DEFINE_SPINLOCK(name) \
  14335. + spinlock_t name = __SPIN_LOCK_UNLOCKED(name)
  14336. +
  14337. +#define DEFINE_SPINLOCK(name) \
  14338. + spinlock_t name __cacheline_aligned_in_smp = __SPIN_LOCK_UNLOCKED(name)
  14339. +
  14340. +#endif
  14341. diff --git a/include/linux/srcu.h b/include/linux/srcu.h
  14342. index f5f80c5643ac..ec1a8f01563c 100644
  14343. --- a/include/linux/srcu.h
  14344. +++ b/include/linux/srcu.h
  14345. @@ -84,10 +84,10 @@ int init_srcu_struct(struct srcu_struct *sp);
  14346. void process_srcu(struct work_struct *work);
  14347. -#define __SRCU_STRUCT_INIT(name) \
  14348. +#define __SRCU_STRUCT_INIT(name, pcpu_name) \
  14349. { \
  14350. .completed = -300, \
  14351. - .per_cpu_ref = &name##_srcu_array, \
  14352. + .per_cpu_ref = &pcpu_name, \
  14353. .queue_lock = __SPIN_LOCK_UNLOCKED(name.queue_lock), \
  14354. .running = false, \
  14355. .batch_queue = RCU_BATCH_INIT(name.batch_queue), \
  14356. @@ -104,7 +104,7 @@ void process_srcu(struct work_struct *work);
  14357. */
  14358. #define __DEFINE_SRCU(name, is_static) \
  14359. static DEFINE_PER_CPU(struct srcu_struct_array, name##_srcu_array);\
  14360. - is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name)
  14361. + is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name##_srcu_array)
  14362. #define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
  14363. #define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
  14364. diff --git a/include/linux/suspend.h b/include/linux/suspend.h
  14365. index 8b6ec7ef0854..9b77d4cc929f 100644
  14366. --- a/include/linux/suspend.h
  14367. +++ b/include/linux/suspend.h
  14368. @@ -194,6 +194,12 @@ struct platform_freeze_ops {
  14369. void (*end)(void);
  14370. };
  14371. +#if defined(CONFIG_SUSPEND) || defined(CONFIG_HIBERNATION)
  14372. +extern bool pm_in_action;
  14373. +#else
  14374. +# define pm_in_action false
  14375. +#endif
  14376. +
  14377. #ifdef CONFIG_SUSPEND
  14378. /**
  14379. * suspend_set_ops - set platform dependent suspend operations
  14380. diff --git a/include/linux/swait.h b/include/linux/swait.h
  14381. new file mode 100644
  14382. index 000000000000..83f004a72320
  14383. --- /dev/null
  14384. +++ b/include/linux/swait.h
  14385. @@ -0,0 +1,173 @@
  14386. +#ifndef _LINUX_SWAIT_H
  14387. +#define _LINUX_SWAIT_H
  14388. +
  14389. +#include <linux/list.h>
  14390. +#include <linux/stddef.h>
  14391. +#include <linux/spinlock.h>
  14392. +#include <asm/current.h>
  14393. +
  14394. +/*
  14395. + * Simple wait queues
  14396. + *
  14397. + * While these are very similar to the other/complex wait queues (wait.h) the
  14398. + * most important difference is that the simple waitqueue allows for
  14399. + * deterministic behaviour -- IOW it has strictly bounded IRQ and lock hold
  14400. + * times.
  14401. + *
  14402. + * In order to make this so, we had to drop a fair number of features of the
  14403. + * other waitqueue code; notably:
  14404. + *
  14405. + * - mixing INTERRUPTIBLE and UNINTERRUPTIBLE sleeps on the same waitqueue;
  14406. + * all wakeups are TASK_NORMAL in order to avoid O(n) lookups for the right
  14407. + * sleeper state.
  14408. + *
  14409. + * - the exclusive mode; because this requires preserving the list order
  14410. + * and this is hard.
  14411. + *
  14412. + * - custom wake functions; because you cannot give any guarantees about
  14413. + * random code.
  14414. + *
  14415. + * As a side effect of this; the data structures are slimmer.
  14416. + *
  14417. + * One would recommend using this wait queue where possible.
  14418. + */
  14419. +
  14420. +struct task_struct;
  14421. +
  14422. +struct swait_queue_head {
  14423. + raw_spinlock_t lock;
  14424. + struct list_head task_list;
  14425. +};
  14426. +
  14427. +struct swait_queue {
  14428. + struct task_struct *task;
  14429. + struct list_head task_list;
  14430. +};
  14431. +
  14432. +#define __SWAITQUEUE_INITIALIZER(name) { \
  14433. + .task = current, \
  14434. + .task_list = LIST_HEAD_INIT((name).task_list), \
  14435. +}
  14436. +
  14437. +#define DECLARE_SWAITQUEUE(name) \
  14438. + struct swait_queue name = __SWAITQUEUE_INITIALIZER(name)
  14439. +
  14440. +#define __SWAIT_QUEUE_HEAD_INITIALIZER(name) { \
  14441. + .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
  14442. + .task_list = LIST_HEAD_INIT((name).task_list), \
  14443. +}
  14444. +
  14445. +#define DECLARE_SWAIT_QUEUE_HEAD(name) \
  14446. + struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INITIALIZER(name)
  14447. +
  14448. +extern void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
  14449. + struct lock_class_key *key);
  14450. +
  14451. +#define init_swait_queue_head(q) \
  14452. + do { \
  14453. + static struct lock_class_key __key; \
  14454. + __init_swait_queue_head((q), #q, &__key); \
  14455. + } while (0)
  14456. +
  14457. +#ifdef CONFIG_LOCKDEP
  14458. +# define __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
  14459. + ({ init_swait_queue_head(&name); name; })
  14460. +# define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
  14461. + struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name)
  14462. +#else
  14463. +# define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
  14464. + DECLARE_SWAIT_QUEUE_HEAD(name)
  14465. +#endif
  14466. +
  14467. +static inline int swait_active(struct swait_queue_head *q)
  14468. +{
  14469. + return !list_empty(&q->task_list);
  14470. +}
  14471. +
  14472. +extern void swake_up(struct swait_queue_head *q);
  14473. +extern void swake_up_all(struct swait_queue_head *q);
  14474. +extern void swake_up_locked(struct swait_queue_head *q);
  14475. +extern void swake_up_all_locked(struct swait_queue_head *q);
  14476. +
  14477. +extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);
  14478. +extern void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state);
  14479. +extern long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state);
  14480. +
  14481. +extern void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait);
  14482. +extern void finish_swait(struct swait_queue_head *q, struct swait_queue *wait);
  14483. +
  14484. +/* as per ___wait_event() but for swait, therefore "exclusive == 0" */
  14485. +#define ___swait_event(wq, condition, state, ret, cmd) \
  14486. +({ \
  14487. + struct swait_queue __wait; \
  14488. + long __ret = ret; \
  14489. + \
  14490. + INIT_LIST_HEAD(&__wait.task_list); \
  14491. + for (;;) { \
  14492. + long __int = prepare_to_swait_event(&wq, &__wait, state);\
  14493. + \
  14494. + if (condition) \
  14495. + break; \
  14496. + \
  14497. + if (___wait_is_interruptible(state) && __int) { \
  14498. + __ret = __int; \
  14499. + break; \
  14500. + } \
  14501. + \
  14502. + cmd; \
  14503. + } \
  14504. + finish_swait(&wq, &__wait); \
  14505. + __ret; \
  14506. +})
  14507. +
  14508. +#define __swait_event(wq, condition) \
  14509. + (void)___swait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \
  14510. + schedule())
  14511. +
  14512. +#define swait_event(wq, condition) \
  14513. +do { \
  14514. + if (condition) \
  14515. + break; \
  14516. + __swait_event(wq, condition); \
  14517. +} while (0)
  14518. +
  14519. +#define __swait_event_timeout(wq, condition, timeout) \
  14520. + ___swait_event(wq, ___wait_cond_timeout(condition), \
  14521. + TASK_UNINTERRUPTIBLE, timeout, \
  14522. + __ret = schedule_timeout(__ret))
  14523. +
  14524. +#define swait_event_timeout(wq, condition, timeout) \
  14525. +({ \
  14526. + long __ret = timeout; \
  14527. + if (!___wait_cond_timeout(condition)) \
  14528. + __ret = __swait_event_timeout(wq, condition, timeout); \
  14529. + __ret; \
  14530. +})
  14531. +
  14532. +#define __swait_event_interruptible(wq, condition) \
  14533. + ___swait_event(wq, condition, TASK_INTERRUPTIBLE, 0, \
  14534. + schedule())
  14535. +
  14536. +#define swait_event_interruptible(wq, condition) \
  14537. +({ \
  14538. + int __ret = 0; \
  14539. + if (!(condition)) \
  14540. + __ret = __swait_event_interruptible(wq, condition); \
  14541. + __ret; \
  14542. +})
  14543. +
  14544. +#define __swait_event_interruptible_timeout(wq, condition, timeout) \
  14545. + ___swait_event(wq, ___wait_cond_timeout(condition), \
  14546. + TASK_INTERRUPTIBLE, timeout, \
  14547. + __ret = schedule_timeout(__ret))
  14548. +
  14549. +#define swait_event_interruptible_timeout(wq, condition, timeout) \
  14550. +({ \
  14551. + long __ret = timeout; \
  14552. + if (!___wait_cond_timeout(condition)) \
  14553. + __ret = __swait_event_interruptible_timeout(wq, \
  14554. + condition, timeout); \
  14555. + __ret; \
  14556. +})
  14557. +
  14558. +#endif /* _LINUX_SWAIT_H */
  14559. diff --git a/include/linux/swap.h b/include/linux/swap.h
  14560. index d8ca2eaa3a8b..19e038054914 100644
  14561. --- a/include/linux/swap.h
  14562. +++ b/include/linux/swap.h
  14563. @@ -11,6 +11,7 @@
  14564. #include <linux/fs.h>
  14565. #include <linux/atomic.h>
  14566. #include <linux/page-flags.h>
  14567. +#include <linux/locallock.h>
  14568. #include <asm/page.h>
  14569. struct notifier_block;
  14570. @@ -252,7 +253,8 @@ struct swap_info_struct {
  14571. void *workingset_eviction(struct address_space *mapping, struct page *page);
  14572. bool workingset_refault(void *shadow);
  14573. void workingset_activation(struct page *page);
  14574. -extern struct list_lru workingset_shadow_nodes;
  14575. +extern struct list_lru __workingset_shadow_nodes;
  14576. +DECLARE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  14577. static inline unsigned int workingset_node_pages(struct radix_tree_node *node)
  14578. {
  14579. @@ -298,6 +300,7 @@ extern unsigned long nr_free_pagecache_pages(void);
  14580. /* linux/mm/swap.c */
  14581. +DECLARE_LOCAL_IRQ_LOCK(swapvec_lock);
  14582. extern void lru_cache_add(struct page *);
  14583. extern void lru_cache_add_anon(struct page *page);
  14584. extern void lru_cache_add_file(struct page *page);
  14585. diff --git a/include/linux/swork.h b/include/linux/swork.h
  14586. new file mode 100644
  14587. index 000000000000..f175fa9a6016
  14588. --- /dev/null
  14589. +++ b/include/linux/swork.h
  14590. @@ -0,0 +1,24 @@
  14591. +#ifndef _LINUX_SWORK_H
  14592. +#define _LINUX_SWORK_H
  14593. +
  14594. +#include <linux/list.h>
  14595. +
  14596. +struct swork_event {
  14597. + struct list_head item;
  14598. + unsigned long flags;
  14599. + void (*func)(struct swork_event *);
  14600. +};
  14601. +
  14602. +static inline void INIT_SWORK(struct swork_event *event,
  14603. + void (*func)(struct swork_event *))
  14604. +{
  14605. + event->flags = 0;
  14606. + event->func = func;
  14607. +}
  14608. +
  14609. +bool swork_queue(struct swork_event *sev);
  14610. +
  14611. +int swork_get(void);
  14612. +void swork_put(void);
  14613. +
  14614. +#endif /* _LINUX_SWORK_H */
  14615. diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h
  14616. index ff307b548ed3..be9f9dc6a4e1 100644
  14617. --- a/include/linux/thread_info.h
  14618. +++ b/include/linux/thread_info.h
  14619. @@ -102,7 +102,17 @@ static inline int test_ti_thread_flag(struct thread_info *ti, int flag)
  14620. #define test_thread_flag(flag) \
  14621. test_ti_thread_flag(current_thread_info(), flag)
  14622. -#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  14623. +#ifdef CONFIG_PREEMPT_LAZY
  14624. +#define tif_need_resched() (test_thread_flag(TIF_NEED_RESCHED) || \
  14625. + test_thread_flag(TIF_NEED_RESCHED_LAZY))
  14626. +#define tif_need_resched_now() (test_thread_flag(TIF_NEED_RESCHED))
  14627. +#define tif_need_resched_lazy() test_thread_flag(TIF_NEED_RESCHED_LAZY))
  14628. +
  14629. +#else
  14630. +#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  14631. +#define tif_need_resched_now() test_thread_flag(TIF_NEED_RESCHED)
  14632. +#define tif_need_resched_lazy() 0
  14633. +#endif
  14634. #if defined TIF_RESTORE_SIGMASK && !defined HAVE_SET_RESTORE_SIGMASK
  14635. /*
  14636. diff --git a/include/linux/timer.h b/include/linux/timer.h
  14637. index 61aa61dc410c..299d2b78591f 100644
  14638. --- a/include/linux/timer.h
  14639. +++ b/include/linux/timer.h
  14640. @@ -225,7 +225,7 @@ extern void add_timer(struct timer_list *timer);
  14641. extern int try_to_del_timer_sync(struct timer_list *timer);
  14642. -#ifdef CONFIG_SMP
  14643. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  14644. extern int del_timer_sync(struct timer_list *timer);
  14645. #else
  14646. # define del_timer_sync(t) del_timer(t)
  14647. diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h
  14648. index 311176f290b2..62d4bff5f477 100644
  14649. --- a/include/linux/trace_events.h
  14650. +++ b/include/linux/trace_events.h
  14651. @@ -66,6 +66,9 @@ struct trace_entry {
  14652. unsigned char flags;
  14653. unsigned char preempt_count;
  14654. int pid;
  14655. + unsigned short migrate_disable;
  14656. + unsigned short padding;
  14657. + unsigned char preempt_lazy_count;
  14658. };
  14659. #define TRACE_EVENT_TYPE_MAX \
  14660. diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
  14661. index 558129af828a..cf5c472bbc79 100644
  14662. --- a/include/linux/uaccess.h
  14663. +++ b/include/linux/uaccess.h
  14664. @@ -24,6 +24,7 @@ static __always_inline void pagefault_disabled_dec(void)
  14665. */
  14666. static inline void pagefault_disable(void)
  14667. {
  14668. + migrate_disable();
  14669. pagefault_disabled_inc();
  14670. /*
  14671. * make sure to have issued the store before a pagefault
  14672. @@ -40,6 +41,7 @@ static inline void pagefault_enable(void)
  14673. */
  14674. barrier();
  14675. pagefault_disabled_dec();
  14676. + migrate_enable();
  14677. }
  14678. /*
  14679. diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h
  14680. index 4a29c75b146e..0a294e950df8 100644
  14681. --- a/include/linux/uprobes.h
  14682. +++ b/include/linux/uprobes.h
  14683. @@ -27,6 +27,7 @@
  14684. #include <linux/errno.h>
  14685. #include <linux/rbtree.h>
  14686. #include <linux/types.h>
  14687. +#include <linux/wait.h>
  14688. struct vm_area_struct;
  14689. struct mm_struct;
  14690. diff --git a/include/linux/vmstat.h b/include/linux/vmstat.h
  14691. index 3e5d9075960f..7eaa847cd5a5 100644
  14692. --- a/include/linux/vmstat.h
  14693. +++ b/include/linux/vmstat.h
  14694. @@ -33,7 +33,9 @@ DECLARE_PER_CPU(struct vm_event_state, vm_event_states);
  14695. */
  14696. static inline void __count_vm_event(enum vm_event_item item)
  14697. {
  14698. + preempt_disable_rt();
  14699. raw_cpu_inc(vm_event_states.event[item]);
  14700. + preempt_enable_rt();
  14701. }
  14702. static inline void count_vm_event(enum vm_event_item item)
  14703. @@ -43,7 +45,9 @@ static inline void count_vm_event(enum vm_event_item item)
  14704. static inline void __count_vm_events(enum vm_event_item item, long delta)
  14705. {
  14706. + preempt_disable_rt();
  14707. raw_cpu_add(vm_event_states.event[item], delta);
  14708. + preempt_enable_rt();
  14709. }
  14710. static inline void count_vm_events(enum vm_event_item item, long delta)
  14711. diff --git a/include/linux/wait.h b/include/linux/wait.h
  14712. index 513b36f04dfd..981c8a840f96 100644
  14713. --- a/include/linux/wait.h
  14714. +++ b/include/linux/wait.h
  14715. @@ -8,6 +8,7 @@
  14716. #include <linux/spinlock.h>
  14717. #include <asm/current.h>
  14718. #include <uapi/linux/wait.h>
  14719. +#include <linux/atomic.h>
  14720. typedef struct __wait_queue wait_queue_t;
  14721. typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
  14722. diff --git a/include/net/dst.h b/include/net/dst.h
  14723. index e4f450617919..16cd3ef62202 100644
  14724. --- a/include/net/dst.h
  14725. +++ b/include/net/dst.h
  14726. @@ -443,7 +443,7 @@ static inline void dst_confirm(struct dst_entry *dst)
  14727. static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n,
  14728. struct sk_buff *skb)
  14729. {
  14730. - const struct hh_cache *hh;
  14731. + struct hh_cache *hh;
  14732. if (dst->pending_confirm) {
  14733. unsigned long now = jiffies;
  14734. diff --git a/include/net/neighbour.h b/include/net/neighbour.h
  14735. index 8b683841e574..bf656008f6e7 100644
  14736. --- a/include/net/neighbour.h
  14737. +++ b/include/net/neighbour.h
  14738. @@ -446,7 +446,7 @@ static inline int neigh_hh_bridge(struct hh_cache *hh, struct sk_buff *skb)
  14739. }
  14740. #endif
  14741. -static inline int neigh_hh_output(const struct hh_cache *hh, struct sk_buff *skb)
  14742. +static inline int neigh_hh_output(struct hh_cache *hh, struct sk_buff *skb)
  14743. {
  14744. unsigned int seq;
  14745. int hh_len;
  14746. @@ -501,7 +501,7 @@ struct neighbour_cb {
  14747. #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
  14748. -static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
  14749. +static inline void neigh_ha_snapshot(char *dst, struct neighbour *n,
  14750. const struct net_device *dev)
  14751. {
  14752. unsigned int seq;
  14753. diff --git a/include/net/netns/ipv4.h b/include/net/netns/ipv4.h
  14754. index c68926b4899c..dd0751e76065 100644
  14755. --- a/include/net/netns/ipv4.h
  14756. +++ b/include/net/netns/ipv4.h
  14757. @@ -70,6 +70,7 @@ struct netns_ipv4 {
  14758. int sysctl_icmp_echo_ignore_all;
  14759. int sysctl_icmp_echo_ignore_broadcasts;
  14760. + int sysctl_icmp_echo_sysrq;
  14761. int sysctl_icmp_ignore_bogus_error_responses;
  14762. int sysctl_icmp_ratelimit;
  14763. int sysctl_icmp_ratemask;
  14764. diff --git a/include/trace/events/hist.h b/include/trace/events/hist.h
  14765. new file mode 100644
  14766. index 000000000000..f7710de1b1f3
  14767. --- /dev/null
  14768. +++ b/include/trace/events/hist.h
  14769. @@ -0,0 +1,73 @@
  14770. +#undef TRACE_SYSTEM
  14771. +#define TRACE_SYSTEM hist
  14772. +
  14773. +#if !defined(_TRACE_HIST_H) || defined(TRACE_HEADER_MULTI_READ)
  14774. +#define _TRACE_HIST_H
  14775. +
  14776. +#include "latency_hist.h"
  14777. +#include <linux/tracepoint.h>
  14778. +
  14779. +#if !defined(CONFIG_PREEMPT_OFF_HIST) && !defined(CONFIG_INTERRUPT_OFF_HIST)
  14780. +#define trace_preemptirqsoff_hist(a, b)
  14781. +#define trace_preemptirqsoff_hist_rcuidle(a, b)
  14782. +#else
  14783. +TRACE_EVENT(preemptirqsoff_hist,
  14784. +
  14785. + TP_PROTO(int reason, int starthist),
  14786. +
  14787. + TP_ARGS(reason, starthist),
  14788. +
  14789. + TP_STRUCT__entry(
  14790. + __field(int, reason)
  14791. + __field(int, starthist)
  14792. + ),
  14793. +
  14794. + TP_fast_assign(
  14795. + __entry->reason = reason;
  14796. + __entry->starthist = starthist;
  14797. + ),
  14798. +
  14799. + TP_printk("reason=%s starthist=%s", getaction(__entry->reason),
  14800. + __entry->starthist ? "start" : "stop")
  14801. +);
  14802. +#endif
  14803. +
  14804. +#ifndef CONFIG_MISSED_TIMER_OFFSETS_HIST
  14805. +#define trace_hrtimer_interrupt(a, b, c, d)
  14806. +#else
  14807. +TRACE_EVENT(hrtimer_interrupt,
  14808. +
  14809. + TP_PROTO(int cpu, long long offset, struct task_struct *curr,
  14810. + struct task_struct *task),
  14811. +
  14812. + TP_ARGS(cpu, offset, curr, task),
  14813. +
  14814. + TP_STRUCT__entry(
  14815. + __field(int, cpu)
  14816. + __field(long long, offset)
  14817. + __array(char, ccomm, TASK_COMM_LEN)
  14818. + __field(int, cprio)
  14819. + __array(char, tcomm, TASK_COMM_LEN)
  14820. + __field(int, tprio)
  14821. + ),
  14822. +
  14823. + TP_fast_assign(
  14824. + __entry->cpu = cpu;
  14825. + __entry->offset = offset;
  14826. + memcpy(__entry->ccomm, curr->comm, TASK_COMM_LEN);
  14827. + __entry->cprio = curr->prio;
  14828. + memcpy(__entry->tcomm, task != NULL ? task->comm : "<none>",
  14829. + task != NULL ? TASK_COMM_LEN : 7);
  14830. + __entry->tprio = task != NULL ? task->prio : -1;
  14831. + ),
  14832. +
  14833. + TP_printk("cpu=%d offset=%lld curr=%s[%d] thread=%s[%d]",
  14834. + __entry->cpu, __entry->offset, __entry->ccomm,
  14835. + __entry->cprio, __entry->tcomm, __entry->tprio)
  14836. +);
  14837. +#endif
  14838. +
  14839. +#endif /* _TRACE_HIST_H */
  14840. +
  14841. +/* This part must be outside protection */
  14842. +#include <trace/define_trace.h>
  14843. diff --git a/include/trace/events/latency_hist.h b/include/trace/events/latency_hist.h
  14844. new file mode 100644
  14845. index 000000000000..d3f2fbd560b1
  14846. --- /dev/null
  14847. +++ b/include/trace/events/latency_hist.h
  14848. @@ -0,0 +1,29 @@
  14849. +#ifndef _LATENCY_HIST_H
  14850. +#define _LATENCY_HIST_H
  14851. +
  14852. +enum hist_action {
  14853. + IRQS_ON,
  14854. + PREEMPT_ON,
  14855. + TRACE_STOP,
  14856. + IRQS_OFF,
  14857. + PREEMPT_OFF,
  14858. + TRACE_START,
  14859. +};
  14860. +
  14861. +static char *actions[] = {
  14862. + "IRQS_ON",
  14863. + "PREEMPT_ON",
  14864. + "TRACE_STOP",
  14865. + "IRQS_OFF",
  14866. + "PREEMPT_OFF",
  14867. + "TRACE_START",
  14868. +};
  14869. +
  14870. +static inline char *getaction(int action)
  14871. +{
  14872. + if (action >= 0 && action <= sizeof(actions)/sizeof(actions[0]))
  14873. + return actions[action];
  14874. + return "unknown";
  14875. +}
  14876. +
  14877. +#endif /* _LATENCY_HIST_H */
  14878. diff --git a/include/trace/events/writeback.h b/include/trace/events/writeback.h
  14879. index fff846b512e6..73614ce1d204 100644
  14880. --- a/include/trace/events/writeback.h
  14881. +++ b/include/trace/events/writeback.h
  14882. @@ -134,58 +134,28 @@ DEFINE_EVENT(writeback_dirty_inode_template, writeback_dirty_inode,
  14883. #ifdef CREATE_TRACE_POINTS
  14884. #ifdef CONFIG_CGROUP_WRITEBACK
  14885. -static inline size_t __trace_wb_cgroup_size(struct bdi_writeback *wb)
  14886. +static inline unsigned int __trace_wb_assign_cgroup(struct bdi_writeback *wb)
  14887. {
  14888. - return kernfs_path_len(wb->memcg_css->cgroup->kn) + 1;
  14889. + return wb->memcg_css->cgroup->kn->ino;
  14890. }
  14891. -static inline void __trace_wb_assign_cgroup(char *buf, struct bdi_writeback *wb)
  14892. -{
  14893. - struct cgroup *cgrp = wb->memcg_css->cgroup;
  14894. - char *path;
  14895. -
  14896. - path = cgroup_path(cgrp, buf, kernfs_path_len(cgrp->kn) + 1);
  14897. - WARN_ON_ONCE(path != buf);
  14898. -}
  14899. -
  14900. -static inline size_t __trace_wbc_cgroup_size(struct writeback_control *wbc)
  14901. -{
  14902. - if (wbc->wb)
  14903. - return __trace_wb_cgroup_size(wbc->wb);
  14904. - else
  14905. - return 2;
  14906. -}
  14907. -
  14908. -static inline void __trace_wbc_assign_cgroup(char *buf,
  14909. - struct writeback_control *wbc)
  14910. +static inline unsigned int __trace_wbc_assign_cgroup(struct writeback_control *wbc)
  14911. {
  14912. if (wbc->wb)
  14913. - __trace_wb_assign_cgroup(buf, wbc->wb);
  14914. + return __trace_wb_assign_cgroup(wbc->wb);
  14915. else
  14916. - strcpy(buf, "/");
  14917. + return -1U;
  14918. }
  14919. -
  14920. #else /* CONFIG_CGROUP_WRITEBACK */
  14921. -static inline size_t __trace_wb_cgroup_size(struct bdi_writeback *wb)
  14922. -{
  14923. - return 2;
  14924. -}
  14925. -
  14926. -static inline void __trace_wb_assign_cgroup(char *buf, struct bdi_writeback *wb)
  14927. -{
  14928. - strcpy(buf, "/");
  14929. -}
  14930. -
  14931. -static inline size_t __trace_wbc_cgroup_size(struct writeback_control *wbc)
  14932. +static inline unsigned int __trace_wb_assign_cgroup(struct bdi_writeback *wb)
  14933. {
  14934. - return 2;
  14935. + return -1U;
  14936. }
  14937. -static inline void __trace_wbc_assign_cgroup(char *buf,
  14938. - struct writeback_control *wbc)
  14939. +static inline unsigned int __trace_wbc_assign_cgroup(struct writeback_control *wbc)
  14940. {
  14941. - strcpy(buf, "/");
  14942. + return -1U;
  14943. }
  14944. #endif /* CONFIG_CGROUP_WRITEBACK */
  14945. @@ -201,7 +171,7 @@ DECLARE_EVENT_CLASS(writeback_write_inode_template,
  14946. __array(char, name, 32)
  14947. __field(unsigned long, ino)
  14948. __field(int, sync_mode)
  14949. - __dynamic_array(char, cgroup, __trace_wbc_cgroup_size(wbc))
  14950. + __field(unsigned int, cgroup_ino)
  14951. ),
  14952. TP_fast_assign(
  14953. @@ -209,14 +179,14 @@ DECLARE_EVENT_CLASS(writeback_write_inode_template,
  14954. dev_name(inode_to_bdi(inode)->dev), 32);
  14955. __entry->ino = inode->i_ino;
  14956. __entry->sync_mode = wbc->sync_mode;
  14957. - __trace_wbc_assign_cgroup(__get_str(cgroup), wbc);
  14958. + __entry->cgroup_ino = __trace_wbc_assign_cgroup(wbc);
  14959. ),
  14960. - TP_printk("bdi %s: ino=%lu sync_mode=%d cgroup=%s",
  14961. + TP_printk("bdi %s: ino=%lu sync_mode=%d cgroup_ino=%u",
  14962. __entry->name,
  14963. __entry->ino,
  14964. __entry->sync_mode,
  14965. - __get_str(cgroup)
  14966. + __entry->cgroup_ino
  14967. )
  14968. );
  14969. @@ -246,7 +216,7 @@ DECLARE_EVENT_CLASS(writeback_work_class,
  14970. __field(int, range_cyclic)
  14971. __field(int, for_background)
  14972. __field(int, reason)
  14973. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14974. + __field(unsigned int, cgroup_ino)
  14975. ),
  14976. TP_fast_assign(
  14977. strncpy(__entry->name,
  14978. @@ -258,10 +228,10 @@ DECLARE_EVENT_CLASS(writeback_work_class,
  14979. __entry->range_cyclic = work->range_cyclic;
  14980. __entry->for_background = work->for_background;
  14981. __entry->reason = work->reason;
  14982. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14983. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14984. ),
  14985. TP_printk("bdi %s: sb_dev %d:%d nr_pages=%ld sync_mode=%d "
  14986. - "kupdate=%d range_cyclic=%d background=%d reason=%s cgroup=%s",
  14987. + "kupdate=%d range_cyclic=%d background=%d reason=%s cgroup_ino=%u",
  14988. __entry->name,
  14989. MAJOR(__entry->sb_dev), MINOR(__entry->sb_dev),
  14990. __entry->nr_pages,
  14991. @@ -270,7 +240,7 @@ DECLARE_EVENT_CLASS(writeback_work_class,
  14992. __entry->range_cyclic,
  14993. __entry->for_background,
  14994. __print_symbolic(__entry->reason, WB_WORK_REASON),
  14995. - __get_str(cgroup)
  14996. + __entry->cgroup_ino
  14997. )
  14998. );
  14999. #define DEFINE_WRITEBACK_WORK_EVENT(name) \
  15000. @@ -300,15 +270,15 @@ DECLARE_EVENT_CLASS(writeback_class,
  15001. TP_ARGS(wb),
  15002. TP_STRUCT__entry(
  15003. __array(char, name, 32)
  15004. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  15005. + __field(unsigned int, cgroup_ino)
  15006. ),
  15007. TP_fast_assign(
  15008. strncpy(__entry->name, dev_name(wb->bdi->dev), 32);
  15009. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  15010. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  15011. ),
  15012. - TP_printk("bdi %s: cgroup=%s",
  15013. + TP_printk("bdi %s: cgroup_ino=%u",
  15014. __entry->name,
  15015. - __get_str(cgroup)
  15016. + __entry->cgroup_ino
  15017. )
  15018. );
  15019. #define DEFINE_WRITEBACK_EVENT(name) \
  15020. @@ -347,7 +317,7 @@ DECLARE_EVENT_CLASS(wbc_class,
  15021. __field(int, range_cyclic)
  15022. __field(long, range_start)
  15023. __field(long, range_end)
  15024. - __dynamic_array(char, cgroup, __trace_wbc_cgroup_size(wbc))
  15025. + __field(unsigned int, cgroup_ino)
  15026. ),
  15027. TP_fast_assign(
  15028. @@ -361,12 +331,12 @@ DECLARE_EVENT_CLASS(wbc_class,
  15029. __entry->range_cyclic = wbc->range_cyclic;
  15030. __entry->range_start = (long)wbc->range_start;
  15031. __entry->range_end = (long)wbc->range_end;
  15032. - __trace_wbc_assign_cgroup(__get_str(cgroup), wbc);
  15033. + __entry->cgroup_ino = __trace_wbc_assign_cgroup(wbc);
  15034. ),
  15035. TP_printk("bdi %s: towrt=%ld skip=%ld mode=%d kupd=%d "
  15036. "bgrd=%d reclm=%d cyclic=%d "
  15037. - "start=0x%lx end=0x%lx cgroup=%s",
  15038. + "start=0x%lx end=0x%lx cgroup_ino=%u",
  15039. __entry->name,
  15040. __entry->nr_to_write,
  15041. __entry->pages_skipped,
  15042. @@ -377,7 +347,7 @@ DECLARE_EVENT_CLASS(wbc_class,
  15043. __entry->range_cyclic,
  15044. __entry->range_start,
  15045. __entry->range_end,
  15046. - __get_str(cgroup)
  15047. + __entry->cgroup_ino
  15048. )
  15049. )
  15050. @@ -398,7 +368,7 @@ TRACE_EVENT(writeback_queue_io,
  15051. __field(long, age)
  15052. __field(int, moved)
  15053. __field(int, reason)
  15054. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  15055. + __field(unsigned int, cgroup_ino)
  15056. ),
  15057. TP_fast_assign(
  15058. unsigned long *older_than_this = work->older_than_this;
  15059. @@ -408,15 +378,15 @@ TRACE_EVENT(writeback_queue_io,
  15060. (jiffies - *older_than_this) * 1000 / HZ : -1;
  15061. __entry->moved = moved;
  15062. __entry->reason = work->reason;
  15063. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  15064. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  15065. ),
  15066. - TP_printk("bdi %s: older=%lu age=%ld enqueue=%d reason=%s cgroup=%s",
  15067. + TP_printk("bdi %s: older=%lu age=%ld enqueue=%d reason=%s cgroup_ino=%u",
  15068. __entry->name,
  15069. __entry->older, /* older_than_this in jiffies */
  15070. __entry->age, /* older_than_this in relative milliseconds */
  15071. __entry->moved,
  15072. __print_symbolic(__entry->reason, WB_WORK_REASON),
  15073. - __get_str(cgroup)
  15074. + __entry->cgroup_ino
  15075. )
  15076. );
  15077. @@ -484,7 +454,7 @@ TRACE_EVENT(bdi_dirty_ratelimit,
  15078. __field(unsigned long, dirty_ratelimit)
  15079. __field(unsigned long, task_ratelimit)
  15080. __field(unsigned long, balanced_dirty_ratelimit)
  15081. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  15082. + __field(unsigned int, cgroup_ino)
  15083. ),
  15084. TP_fast_assign(
  15085. @@ -496,13 +466,13 @@ TRACE_EVENT(bdi_dirty_ratelimit,
  15086. __entry->task_ratelimit = KBps(task_ratelimit);
  15087. __entry->balanced_dirty_ratelimit =
  15088. KBps(wb->balanced_dirty_ratelimit);
  15089. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  15090. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  15091. ),
  15092. TP_printk("bdi %s: "
  15093. "write_bw=%lu awrite_bw=%lu dirty_rate=%lu "
  15094. "dirty_ratelimit=%lu task_ratelimit=%lu "
  15095. - "balanced_dirty_ratelimit=%lu cgroup=%s",
  15096. + "balanced_dirty_ratelimit=%lu cgroup_ino=%u",
  15097. __entry->bdi,
  15098. __entry->write_bw, /* write bandwidth */
  15099. __entry->avg_write_bw, /* avg write bandwidth */
  15100. @@ -510,7 +480,7 @@ TRACE_EVENT(bdi_dirty_ratelimit,
  15101. __entry->dirty_ratelimit, /* base ratelimit */
  15102. __entry->task_ratelimit, /* ratelimit with position control */
  15103. __entry->balanced_dirty_ratelimit, /* the balanced ratelimit */
  15104. - __get_str(cgroup)
  15105. + __entry->cgroup_ino
  15106. )
  15107. );
  15108. @@ -548,7 +518,7 @@ TRACE_EVENT(balance_dirty_pages,
  15109. __field( long, pause)
  15110. __field(unsigned long, period)
  15111. __field( long, think)
  15112. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  15113. + __field(unsigned int, cgroup_ino)
  15114. ),
  15115. TP_fast_assign(
  15116. @@ -571,7 +541,7 @@ TRACE_EVENT(balance_dirty_pages,
  15117. __entry->period = period * 1000 / HZ;
  15118. __entry->pause = pause * 1000 / HZ;
  15119. __entry->paused = (jiffies - start_time) * 1000 / HZ;
  15120. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  15121. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  15122. ),
  15123. @@ -580,7 +550,7 @@ TRACE_EVENT(balance_dirty_pages,
  15124. "bdi_setpoint=%lu bdi_dirty=%lu "
  15125. "dirty_ratelimit=%lu task_ratelimit=%lu "
  15126. "dirtied=%u dirtied_pause=%u "
  15127. - "paused=%lu pause=%ld period=%lu think=%ld cgroup=%s",
  15128. + "paused=%lu pause=%ld period=%lu think=%ld cgroup_ino=%u",
  15129. __entry->bdi,
  15130. __entry->limit,
  15131. __entry->setpoint,
  15132. @@ -595,7 +565,7 @@ TRACE_EVENT(balance_dirty_pages,
  15133. __entry->pause, /* ms */
  15134. __entry->period, /* ms */
  15135. __entry->think, /* ms */
  15136. - __get_str(cgroup)
  15137. + __entry->cgroup_ino
  15138. )
  15139. );
  15140. @@ -609,8 +579,7 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
  15141. __field(unsigned long, ino)
  15142. __field(unsigned long, state)
  15143. __field(unsigned long, dirtied_when)
  15144. - __dynamic_array(char, cgroup,
  15145. - __trace_wb_cgroup_size(inode_to_wb(inode)))
  15146. + __field(unsigned int, cgroup_ino)
  15147. ),
  15148. TP_fast_assign(
  15149. @@ -619,16 +588,16 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
  15150. __entry->ino = inode->i_ino;
  15151. __entry->state = inode->i_state;
  15152. __entry->dirtied_when = inode->dirtied_when;
  15153. - __trace_wb_assign_cgroup(__get_str(cgroup), inode_to_wb(inode));
  15154. + __entry->cgroup_ino = __trace_wb_assign_cgroup(inode_to_wb(inode));
  15155. ),
  15156. - TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu cgroup=%s",
  15157. + TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu cgroup_ino=%u",
  15158. __entry->name,
  15159. __entry->ino,
  15160. show_inode_state(__entry->state),
  15161. __entry->dirtied_when,
  15162. (jiffies - __entry->dirtied_when) / HZ,
  15163. - __get_str(cgroup)
  15164. + __entry->cgroup_ino
  15165. )
  15166. );
  15167. @@ -684,7 +653,7 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
  15168. __field(unsigned long, writeback_index)
  15169. __field(long, nr_to_write)
  15170. __field(unsigned long, wrote)
  15171. - __dynamic_array(char, cgroup, __trace_wbc_cgroup_size(wbc))
  15172. + __field(unsigned int, cgroup_ino)
  15173. ),
  15174. TP_fast_assign(
  15175. @@ -696,11 +665,11 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
  15176. __entry->writeback_index = inode->i_mapping->writeback_index;
  15177. __entry->nr_to_write = nr_to_write;
  15178. __entry->wrote = nr_to_write - wbc->nr_to_write;
  15179. - __trace_wbc_assign_cgroup(__get_str(cgroup), wbc);
  15180. + __entry->cgroup_ino = __trace_wbc_assign_cgroup(wbc);
  15181. ),
  15182. TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu "
  15183. - "index=%lu to_write=%ld wrote=%lu cgroup=%s",
  15184. + "index=%lu to_write=%ld wrote=%lu cgroup_ino=%u",
  15185. __entry->name,
  15186. __entry->ino,
  15187. show_inode_state(__entry->state),
  15188. @@ -709,7 +678,7 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
  15189. __entry->writeback_index,
  15190. __entry->nr_to_write,
  15191. __entry->wrote,
  15192. - __get_str(cgroup)
  15193. + __entry->cgroup_ino
  15194. )
  15195. );
  15196. diff --git a/init/Kconfig b/init/Kconfig
  15197. index 235c7a2c0d20..a7c81c0911da 100644
  15198. --- a/init/Kconfig
  15199. +++ b/init/Kconfig
  15200. @@ -498,7 +498,7 @@ config TINY_RCU
  15201. config RCU_EXPERT
  15202. bool "Make expert-level adjustments to RCU configuration"
  15203. - default n
  15204. + default y if PREEMPT_RT_FULL
  15205. help
  15206. This option needs to be enabled if you wish to make
  15207. expert-level adjustments to RCU configuration. By default,
  15208. @@ -614,7 +614,7 @@ config RCU_FANOUT_LEAF
  15209. config RCU_FAST_NO_HZ
  15210. bool "Accelerate last non-dyntick-idle CPU's grace periods"
  15211. - depends on NO_HZ_COMMON && SMP && RCU_EXPERT
  15212. + depends on NO_HZ_COMMON && SMP && RCU_EXPERT && !PREEMPT_RT_FULL
  15213. default n
  15214. help
  15215. This option permits CPUs to enter dynticks-idle state even if
  15216. @@ -641,7 +641,7 @@ config TREE_RCU_TRACE
  15217. config RCU_BOOST
  15218. bool "Enable RCU priority boosting"
  15219. depends on RT_MUTEXES && PREEMPT_RCU && RCU_EXPERT
  15220. - default n
  15221. + default y if PREEMPT_RT_FULL
  15222. help
  15223. This option boosts the priority of preempted RCU readers that
  15224. block the current preemptible RCU grace period for too long.
  15225. @@ -1106,6 +1106,7 @@ config CFS_BANDWIDTH
  15226. config RT_GROUP_SCHED
  15227. bool "Group scheduling for SCHED_RR/FIFO"
  15228. depends on CGROUP_SCHED
  15229. + depends on !PREEMPT_RT_FULL
  15230. default n
  15231. help
  15232. This feature lets you explicitly allocate real CPU bandwidth
  15233. @@ -1719,6 +1720,7 @@ choice
  15234. config SLAB
  15235. bool "SLAB"
  15236. + depends on !PREEMPT_RT_FULL
  15237. help
  15238. The regular slab allocator that is established and known to work
  15239. well in all environments. It organizes cache hot objects in
  15240. @@ -1737,6 +1739,7 @@ config SLUB
  15241. config SLOB
  15242. depends on EXPERT
  15243. bool "SLOB (Simple Allocator)"
  15244. + depends on !PREEMPT_RT_FULL
  15245. help
  15246. SLOB replaces the stock allocator with a drastically simpler
  15247. allocator. SLOB is generally more space efficient but
  15248. @@ -1746,7 +1749,7 @@ endchoice
  15249. config SLUB_CPU_PARTIAL
  15250. default y
  15251. - depends on SLUB && SMP
  15252. + depends on SLUB && SMP && !PREEMPT_RT_FULL
  15253. bool "SLUB per cpu partial cache"
  15254. help
  15255. Per cpu partial caches accellerate objects allocation and freeing
  15256. diff --git a/init/Makefile b/init/Makefile
  15257. index 7bc47ee31c36..88cf473554e0 100644
  15258. --- a/init/Makefile
  15259. +++ b/init/Makefile
  15260. @@ -33,4 +33,4 @@ $(obj)/version.o: include/generated/compile.h
  15261. include/generated/compile.h: FORCE
  15262. @$($(quiet)chk_compile.h)
  15263. $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkcompile_h $@ \
  15264. - "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CC) $(KBUILD_CFLAGS)"
  15265. + "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CONFIG_PREEMPT_RT_FULL)" "$(CC) $(KBUILD_CFLAGS)"
  15266. diff --git a/init/main.c b/init/main.c
  15267. index 49926d95442f..995cbb8d09b6 100644
  15268. --- a/init/main.c
  15269. +++ b/init/main.c
  15270. @@ -532,6 +532,7 @@ asmlinkage __visible void __init start_kernel(void)
  15271. setup_command_line(command_line);
  15272. setup_nr_cpu_ids();
  15273. setup_per_cpu_areas();
  15274. + softirq_early_init();
  15275. smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
  15276. build_all_zonelists(NULL, NULL);
  15277. diff --git a/ipc/msg.c b/ipc/msg.c
  15278. index c6521c205cb4..996d89023552 100644
  15279. --- a/ipc/msg.c
  15280. +++ b/ipc/msg.c
  15281. @@ -183,20 +183,14 @@ static void ss_wakeup(struct list_head *h, int kill)
  15282. }
  15283. }
  15284. -static void expunge_all(struct msg_queue *msq, int res)
  15285. +static void expunge_all(struct msg_queue *msq, int res,
  15286. + struct wake_q_head *wake_q)
  15287. {
  15288. struct msg_receiver *msr, *t;
  15289. list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
  15290. - msr->r_msg = NULL; /* initialize expunge ordering */
  15291. - wake_up_process(msr->r_tsk);
  15292. - /*
  15293. - * Ensure that the wakeup is visible before setting r_msg as
  15294. - * the receiving end depends on it: either spinning on a nil,
  15295. - * or dealing with -EAGAIN cases. See lockless receive part 1
  15296. - * and 2 in do_msgrcv().
  15297. - */
  15298. - smp_wmb(); /* barrier (B) */
  15299. +
  15300. + wake_q_add(wake_q, msr->r_tsk);
  15301. msr->r_msg = ERR_PTR(res);
  15302. }
  15303. }
  15304. @@ -213,11 +207,13 @@ static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  15305. {
  15306. struct msg_msg *msg, *t;
  15307. struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
  15308. + WAKE_Q(wake_q);
  15309. - expunge_all(msq, -EIDRM);
  15310. + expunge_all(msq, -EIDRM, &wake_q);
  15311. ss_wakeup(&msq->q_senders, 1);
  15312. msg_rmid(ns, msq);
  15313. ipc_unlock_object(&msq->q_perm);
  15314. + wake_up_q(&wake_q);
  15315. rcu_read_unlock();
  15316. list_for_each_entry_safe(msg, t, &msq->q_messages, m_list) {
  15317. @@ -342,6 +338,7 @@ static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
  15318. struct kern_ipc_perm *ipcp;
  15319. struct msqid64_ds uninitialized_var(msqid64);
  15320. struct msg_queue *msq;
  15321. + WAKE_Q(wake_q);
  15322. int err;
  15323. if (cmd == IPC_SET) {
  15324. @@ -389,7 +386,7 @@ static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
  15325. /* sleeping receivers might be excluded by
  15326. * stricter permissions.
  15327. */
  15328. - expunge_all(msq, -EAGAIN);
  15329. + expunge_all(msq, -EAGAIN, &wake_q);
  15330. /* sleeping senders might be able to send
  15331. * due to a larger queue size.
  15332. */
  15333. @@ -402,6 +399,7 @@ static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
  15334. out_unlock0:
  15335. ipc_unlock_object(&msq->q_perm);
  15336. + wake_up_q(&wake_q);
  15337. out_unlock1:
  15338. rcu_read_unlock();
  15339. out_up:
  15340. @@ -566,7 +564,8 @@ static int testmsg(struct msg_msg *msg, long type, int mode)
  15341. return 0;
  15342. }
  15343. -static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
  15344. +static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg,
  15345. + struct wake_q_head *wake_q)
  15346. {
  15347. struct msg_receiver *msr, *t;
  15348. @@ -577,27 +576,13 @@ static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
  15349. list_del(&msr->r_list);
  15350. if (msr->r_maxsize < msg->m_ts) {
  15351. - /* initialize pipelined send ordering */
  15352. - msr->r_msg = NULL;
  15353. - wake_up_process(msr->r_tsk);
  15354. - /* barrier (B) see barrier comment below */
  15355. - smp_wmb();
  15356. + wake_q_add(wake_q, msr->r_tsk);
  15357. msr->r_msg = ERR_PTR(-E2BIG);
  15358. } else {
  15359. - msr->r_msg = NULL;
  15360. msq->q_lrpid = task_pid_vnr(msr->r_tsk);
  15361. msq->q_rtime = get_seconds();
  15362. - wake_up_process(msr->r_tsk);
  15363. - /*
  15364. - * Ensure that the wakeup is visible before
  15365. - * setting r_msg, as the receiving can otherwise
  15366. - * exit - once r_msg is set, the receiver can
  15367. - * continue. See lockless receive part 1 and 2
  15368. - * in do_msgrcv(). Barrier (B).
  15369. - */
  15370. - smp_wmb();
  15371. + wake_q_add(wake_q, msr->r_tsk);
  15372. msr->r_msg = msg;
  15373. -
  15374. return 1;
  15375. }
  15376. }
  15377. @@ -613,6 +598,7 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
  15378. struct msg_msg *msg;
  15379. int err;
  15380. struct ipc_namespace *ns;
  15381. + WAKE_Q(wake_q);
  15382. ns = current->nsproxy->ipc_ns;
  15383. @@ -698,7 +684,7 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
  15384. msq->q_lspid = task_tgid_vnr(current);
  15385. msq->q_stime = get_seconds();
  15386. - if (!pipelined_send(msq, msg)) {
  15387. + if (!pipelined_send(msq, msg, &wake_q)) {
  15388. /* no one is waiting for this message, enqueue it */
  15389. list_add_tail(&msg->m_list, &msq->q_messages);
  15390. msq->q_cbytes += msgsz;
  15391. @@ -712,6 +698,7 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
  15392. out_unlock0:
  15393. ipc_unlock_object(&msq->q_perm);
  15394. + wake_up_q(&wake_q);
  15395. out_unlock1:
  15396. rcu_read_unlock();
  15397. if (msg != NULL)
  15398. @@ -932,57 +919,25 @@ long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, int msgfl
  15399. rcu_read_lock();
  15400. /* Lockless receive, part 2:
  15401. - * Wait until pipelined_send or expunge_all are outside of
  15402. - * wake_up_process(). There is a race with exit(), see
  15403. - * ipc/mqueue.c for the details. The correct serialization
  15404. - * ensures that a receiver cannot continue without the wakeup
  15405. - * being visibible _before_ setting r_msg:
  15406. + * The work in pipelined_send() and expunge_all():
  15407. + * - Set pointer to message
  15408. + * - Queue the receiver task for later wakeup
  15409. + * - Wake up the process after the lock is dropped.
  15410. *
  15411. - * CPU 0 CPU 1
  15412. - * <loop receiver>
  15413. - * smp_rmb(); (A) <-- pair -. <waker thread>
  15414. - * <load ->r_msg> | msr->r_msg = NULL;
  15415. - * | wake_up_process();
  15416. - * <continue> `------> smp_wmb(); (B)
  15417. - * msr->r_msg = msg;
  15418. - *
  15419. - * Where (A) orders the message value read and where (B) orders
  15420. - * the write to the r_msg -- done in both pipelined_send and
  15421. - * expunge_all.
  15422. + * Should the process wake up before this wakeup (due to a
  15423. + * signal) it will either see the message and continue …
  15424. */
  15425. - for (;;) {
  15426. - /*
  15427. - * Pairs with writer barrier in pipelined_send
  15428. - * or expunge_all.
  15429. - */
  15430. - smp_rmb(); /* barrier (A) */
  15431. - msg = (struct msg_msg *)msr_d.r_msg;
  15432. - if (msg)
  15433. - break;
  15434. - /*
  15435. - * The cpu_relax() call is a compiler barrier
  15436. - * which forces everything in this loop to be
  15437. - * re-loaded.
  15438. - */
  15439. - cpu_relax();
  15440. - }
  15441. -
  15442. - /* Lockless receive, part 3:
  15443. - * If there is a message or an error then accept it without
  15444. - * locking.
  15445. - */
  15446. + msg = (struct msg_msg *)msr_d.r_msg;
  15447. if (msg != ERR_PTR(-EAGAIN))
  15448. goto out_unlock1;
  15449. - /* Lockless receive, part 3:
  15450. - * Acquire the queue spinlock.
  15451. - */
  15452. + /*
  15453. + * … or see -EAGAIN, acquire the lock to check the message
  15454. + * again.
  15455. + */
  15456. ipc_lock_object(&msq->q_perm);
  15457. - /* Lockless receive, part 4:
  15458. - * Repeat test after acquiring the spinlock.
  15459. - */
  15460. msg = (struct msg_msg *)msr_d.r_msg;
  15461. if (msg != ERR_PTR(-EAGAIN))
  15462. goto out_unlock0;
  15463. diff --git a/ipc/sem.c b/ipc/sem.c
  15464. index 9862c3d1c26d..ef34d7376697 100644
  15465. --- a/ipc/sem.c
  15466. +++ b/ipc/sem.c
  15467. @@ -708,6 +708,13 @@ static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
  15468. static void wake_up_sem_queue_prepare(struct list_head *pt,
  15469. struct sem_queue *q, int error)
  15470. {
  15471. +#ifdef CONFIG_PREEMPT_RT_BASE
  15472. + struct task_struct *p = q->sleeper;
  15473. + get_task_struct(p);
  15474. + q->status = error;
  15475. + wake_up_process(p);
  15476. + put_task_struct(p);
  15477. +#else
  15478. if (list_empty(pt)) {
  15479. /*
  15480. * Hold preempt off so that we don't get preempted and have the
  15481. @@ -719,6 +726,7 @@ static void wake_up_sem_queue_prepare(struct list_head *pt,
  15482. q->pid = error;
  15483. list_add_tail(&q->list, pt);
  15484. +#endif
  15485. }
  15486. /**
  15487. @@ -732,6 +740,7 @@ static void wake_up_sem_queue_prepare(struct list_head *pt,
  15488. */
  15489. static void wake_up_sem_queue_do(struct list_head *pt)
  15490. {
  15491. +#ifndef CONFIG_PREEMPT_RT_BASE
  15492. struct sem_queue *q, *t;
  15493. int did_something;
  15494. @@ -744,6 +753,7 @@ static void wake_up_sem_queue_do(struct list_head *pt)
  15495. }
  15496. if (did_something)
  15497. preempt_enable();
  15498. +#endif
  15499. }
  15500. static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
  15501. diff --git a/kernel/Kconfig.locks b/kernel/Kconfig.locks
  15502. index ebdb0043203a..b9e6aa7e5aa6 100644
  15503. --- a/kernel/Kconfig.locks
  15504. +++ b/kernel/Kconfig.locks
  15505. @@ -225,11 +225,11 @@ config ARCH_SUPPORTS_ATOMIC_RMW
  15506. config MUTEX_SPIN_ON_OWNER
  15507. def_bool y
  15508. - depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW
  15509. + depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  15510. config RWSEM_SPIN_ON_OWNER
  15511. def_bool y
  15512. - depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW
  15513. + depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  15514. config LOCK_SPIN_ON_OWNER
  15515. def_bool y
  15516. diff --git a/kernel/Kconfig.preempt b/kernel/Kconfig.preempt
  15517. index 3f9c97419f02..11dbe26a8279 100644
  15518. --- a/kernel/Kconfig.preempt
  15519. +++ b/kernel/Kconfig.preempt
  15520. @@ -1,3 +1,16 @@
  15521. +config PREEMPT
  15522. + bool
  15523. + select PREEMPT_COUNT
  15524. +
  15525. +config PREEMPT_RT_BASE
  15526. + bool
  15527. + select PREEMPT
  15528. +
  15529. +config HAVE_PREEMPT_LAZY
  15530. + bool
  15531. +
  15532. +config PREEMPT_LAZY
  15533. + def_bool y if HAVE_PREEMPT_LAZY && PREEMPT_RT_FULL
  15534. choice
  15535. prompt "Preemption Model"
  15536. @@ -33,9 +46,9 @@ config PREEMPT_VOLUNTARY
  15537. Select this if you are building a kernel for a desktop system.
  15538. -config PREEMPT
  15539. +config PREEMPT__LL
  15540. bool "Preemptible Kernel (Low-Latency Desktop)"
  15541. - select PREEMPT_COUNT
  15542. + select PREEMPT
  15543. select UNINLINE_SPIN_UNLOCK if !ARCH_INLINE_SPIN_UNLOCK
  15544. help
  15545. This option reduces the latency of the kernel by making
  15546. @@ -52,6 +65,22 @@ config PREEMPT
  15547. embedded system with latency requirements in the milliseconds
  15548. range.
  15549. +config PREEMPT_RTB
  15550. + bool "Preemptible Kernel (Basic RT)"
  15551. + select PREEMPT_RT_BASE
  15552. + help
  15553. + This option is basically the same as (Low-Latency Desktop) but
  15554. + enables changes which are preliminary for the full preemptible
  15555. + RT kernel.
  15556. +
  15557. +config PREEMPT_RT_FULL
  15558. + bool "Fully Preemptible Kernel (RT)"
  15559. + depends on IRQ_FORCED_THREADING
  15560. + select PREEMPT_RT_BASE
  15561. + select PREEMPT_RCU
  15562. + help
  15563. + All and everything
  15564. +
  15565. endchoice
  15566. config PREEMPT_COUNT
  15567. diff --git a/kernel/cgroup.c b/kernel/cgroup.c
  15568. index 4cb94b678e9f..8c41ee8a6fee 100644
  15569. --- a/kernel/cgroup.c
  15570. +++ b/kernel/cgroup.c
  15571. @@ -4741,10 +4741,10 @@ static void css_free_rcu_fn(struct rcu_head *rcu_head)
  15572. queue_work(cgroup_destroy_wq, &css->destroy_work);
  15573. }
  15574. -static void css_release_work_fn(struct work_struct *work)
  15575. +static void css_release_work_fn(struct swork_event *sev)
  15576. {
  15577. struct cgroup_subsys_state *css =
  15578. - container_of(work, struct cgroup_subsys_state, destroy_work);
  15579. + container_of(sev, struct cgroup_subsys_state, destroy_swork);
  15580. struct cgroup_subsys *ss = css->ss;
  15581. struct cgroup *cgrp = css->cgroup;
  15582. @@ -4783,8 +4783,8 @@ static void css_release(struct percpu_ref *ref)
  15583. struct cgroup_subsys_state *css =
  15584. container_of(ref, struct cgroup_subsys_state, refcnt);
  15585. - INIT_WORK(&css->destroy_work, css_release_work_fn);
  15586. - queue_work(cgroup_destroy_wq, &css->destroy_work);
  15587. + INIT_SWORK(&css->destroy_swork, css_release_work_fn);
  15588. + swork_queue(&css->destroy_swork);
  15589. }
  15590. static void init_and_link_css(struct cgroup_subsys_state *css,
  15591. @@ -5401,6 +5401,7 @@ static int __init cgroup_wq_init(void)
  15592. */
  15593. cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
  15594. BUG_ON(!cgroup_destroy_wq);
  15595. + BUG_ON(swork_get());
  15596. /*
  15597. * Used to destroy pidlists and separate to serve as flush domain.
  15598. diff --git a/kernel/cpu.c b/kernel/cpu.c
  15599. index 40d20bf5de28..0be18c1684d8 100644
  15600. --- a/kernel/cpu.c
  15601. +++ b/kernel/cpu.c
  15602. @@ -75,8 +75,8 @@ static struct {
  15603. #endif
  15604. } cpu_hotplug = {
  15605. .active_writer = NULL,
  15606. - .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  15607. .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
  15608. + .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  15609. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  15610. .dep_map = {.name = "cpu_hotplug.lock" },
  15611. #endif
  15612. @@ -89,6 +89,289 @@ static struct {
  15613. #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
  15614. #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
  15615. +/**
  15616. + * hotplug_pcp - per cpu hotplug descriptor
  15617. + * @unplug: set when pin_current_cpu() needs to sync tasks
  15618. + * @sync_tsk: the task that waits for tasks to finish pinned sections
  15619. + * @refcount: counter of tasks in pinned sections
  15620. + * @grab_lock: set when the tasks entering pinned sections should wait
  15621. + * @synced: notifier for @sync_tsk to tell cpu_down it's finished
  15622. + * @mutex: the mutex to make tasks wait (used when @grab_lock is true)
  15623. + * @mutex_init: zero if the mutex hasn't been initialized yet.
  15624. + *
  15625. + * Although @unplug and @sync_tsk may point to the same task, the @unplug
  15626. + * is used as a flag and still exists after @sync_tsk has exited and
  15627. + * @sync_tsk set to NULL.
  15628. + */
  15629. +struct hotplug_pcp {
  15630. + struct task_struct *unplug;
  15631. + struct task_struct *sync_tsk;
  15632. + int refcount;
  15633. + int grab_lock;
  15634. + struct completion synced;
  15635. + struct completion unplug_wait;
  15636. +#ifdef CONFIG_PREEMPT_RT_FULL
  15637. + /*
  15638. + * Note, on PREEMPT_RT, the hotplug lock must save the state of
  15639. + * the task, otherwise the mutex will cause the task to fail
  15640. + * to sleep when required. (Because it's called from migrate_disable())
  15641. + *
  15642. + * The spinlock_t on PREEMPT_RT is a mutex that saves the task's
  15643. + * state.
  15644. + */
  15645. + spinlock_t lock;
  15646. +#else
  15647. + struct mutex mutex;
  15648. +#endif
  15649. + int mutex_init;
  15650. +};
  15651. +
  15652. +#ifdef CONFIG_PREEMPT_RT_FULL
  15653. +# define hotplug_lock(hp) rt_spin_lock__no_mg(&(hp)->lock)
  15654. +# define hotplug_unlock(hp) rt_spin_unlock__no_mg(&(hp)->lock)
  15655. +#else
  15656. +# define hotplug_lock(hp) mutex_lock(&(hp)->mutex)
  15657. +# define hotplug_unlock(hp) mutex_unlock(&(hp)->mutex)
  15658. +#endif
  15659. +
  15660. +static DEFINE_PER_CPU(struct hotplug_pcp, hotplug_pcp);
  15661. +
  15662. +/**
  15663. + * pin_current_cpu - Prevent the current cpu from being unplugged
  15664. + *
  15665. + * Lightweight version of get_online_cpus() to prevent cpu from being
  15666. + * unplugged when code runs in a migration disabled region.
  15667. + *
  15668. + * Must be called with preemption disabled (preempt_count = 1)!
  15669. + */
  15670. +void pin_current_cpu(void)
  15671. +{
  15672. + struct hotplug_pcp *hp;
  15673. + int force = 0;
  15674. +
  15675. +retry:
  15676. + hp = this_cpu_ptr(&hotplug_pcp);
  15677. +
  15678. + if (!hp->unplug || hp->refcount || force || preempt_count() > 1 ||
  15679. + hp->unplug == current) {
  15680. + hp->refcount++;
  15681. + return;
  15682. + }
  15683. + if (hp->grab_lock) {
  15684. + preempt_enable();
  15685. + hotplug_lock(hp);
  15686. + hotplug_unlock(hp);
  15687. + } else {
  15688. + preempt_enable();
  15689. + /*
  15690. + * Try to push this task off of this CPU.
  15691. + */
  15692. + if (!migrate_me()) {
  15693. + preempt_disable();
  15694. + hp = this_cpu_ptr(&hotplug_pcp);
  15695. + if (!hp->grab_lock) {
  15696. + /*
  15697. + * Just let it continue it's already pinned
  15698. + * or about to sleep.
  15699. + */
  15700. + force = 1;
  15701. + goto retry;
  15702. + }
  15703. + preempt_enable();
  15704. + }
  15705. + }
  15706. + preempt_disable();
  15707. + goto retry;
  15708. +}
  15709. +
  15710. +/**
  15711. + * unpin_current_cpu - Allow unplug of current cpu
  15712. + *
  15713. + * Must be called with preemption or interrupts disabled!
  15714. + */
  15715. +void unpin_current_cpu(void)
  15716. +{
  15717. + struct hotplug_pcp *hp = this_cpu_ptr(&hotplug_pcp);
  15718. +
  15719. + WARN_ON(hp->refcount <= 0);
  15720. +
  15721. + /* This is safe. sync_unplug_thread is pinned to this cpu */
  15722. + if (!--hp->refcount && hp->unplug && hp->unplug != current)
  15723. + wake_up_process(hp->unplug);
  15724. +}
  15725. +
  15726. +static void wait_for_pinned_cpus(struct hotplug_pcp *hp)
  15727. +{
  15728. + set_current_state(TASK_UNINTERRUPTIBLE);
  15729. + while (hp->refcount) {
  15730. + schedule_preempt_disabled();
  15731. + set_current_state(TASK_UNINTERRUPTIBLE);
  15732. + }
  15733. +}
  15734. +
  15735. +static int sync_unplug_thread(void *data)
  15736. +{
  15737. + struct hotplug_pcp *hp = data;
  15738. +
  15739. + wait_for_completion(&hp->unplug_wait);
  15740. + preempt_disable();
  15741. + hp->unplug = current;
  15742. + wait_for_pinned_cpus(hp);
  15743. +
  15744. + /*
  15745. + * This thread will synchronize the cpu_down() with threads
  15746. + * that have pinned the CPU. When the pinned CPU count reaches
  15747. + * zero, we inform the cpu_down code to continue to the next step.
  15748. + */
  15749. + set_current_state(TASK_UNINTERRUPTIBLE);
  15750. + preempt_enable();
  15751. + complete(&hp->synced);
  15752. +
  15753. + /*
  15754. + * If all succeeds, the next step will need tasks to wait till
  15755. + * the CPU is offline before continuing. To do this, the grab_lock
  15756. + * is set and tasks going into pin_current_cpu() will block on the
  15757. + * mutex. But we still need to wait for those that are already in
  15758. + * pinned CPU sections. If the cpu_down() failed, the kthread_should_stop()
  15759. + * will kick this thread out.
  15760. + */
  15761. + while (!hp->grab_lock && !kthread_should_stop()) {
  15762. + schedule();
  15763. + set_current_state(TASK_UNINTERRUPTIBLE);
  15764. + }
  15765. +
  15766. + /* Make sure grab_lock is seen before we see a stale completion */
  15767. + smp_mb();
  15768. +
  15769. + /*
  15770. + * Now just before cpu_down() enters stop machine, we need to make
  15771. + * sure all tasks that are in pinned CPU sections are out, and new
  15772. + * tasks will now grab the lock, keeping them from entering pinned
  15773. + * CPU sections.
  15774. + */
  15775. + if (!kthread_should_stop()) {
  15776. + preempt_disable();
  15777. + wait_for_pinned_cpus(hp);
  15778. + preempt_enable();
  15779. + complete(&hp->synced);
  15780. + }
  15781. +
  15782. + set_current_state(TASK_UNINTERRUPTIBLE);
  15783. + while (!kthread_should_stop()) {
  15784. + schedule();
  15785. + set_current_state(TASK_UNINTERRUPTIBLE);
  15786. + }
  15787. + set_current_state(TASK_RUNNING);
  15788. +
  15789. + /*
  15790. + * Force this thread off this CPU as it's going down and
  15791. + * we don't want any more work on this CPU.
  15792. + */
  15793. + current->flags &= ~PF_NO_SETAFFINITY;
  15794. + set_cpus_allowed_ptr(current, cpu_present_mask);
  15795. + migrate_me();
  15796. + return 0;
  15797. +}
  15798. +
  15799. +static void __cpu_unplug_sync(struct hotplug_pcp *hp)
  15800. +{
  15801. + wake_up_process(hp->sync_tsk);
  15802. + wait_for_completion(&hp->synced);
  15803. +}
  15804. +
  15805. +static void __cpu_unplug_wait(unsigned int cpu)
  15806. +{
  15807. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15808. +
  15809. + complete(&hp->unplug_wait);
  15810. + wait_for_completion(&hp->synced);
  15811. +}
  15812. +
  15813. +/*
  15814. + * Start the sync_unplug_thread on the target cpu and wait for it to
  15815. + * complete.
  15816. + */
  15817. +static int cpu_unplug_begin(unsigned int cpu)
  15818. +{
  15819. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15820. + int err;
  15821. +
  15822. + /* Protected by cpu_hotplug.lock */
  15823. + if (!hp->mutex_init) {
  15824. +#ifdef CONFIG_PREEMPT_RT_FULL
  15825. + spin_lock_init(&hp->lock);
  15826. +#else
  15827. + mutex_init(&hp->mutex);
  15828. +#endif
  15829. + hp->mutex_init = 1;
  15830. + }
  15831. +
  15832. + /* Inform the scheduler to migrate tasks off this CPU */
  15833. + tell_sched_cpu_down_begin(cpu);
  15834. +
  15835. + init_completion(&hp->synced);
  15836. + init_completion(&hp->unplug_wait);
  15837. +
  15838. + hp->sync_tsk = kthread_create(sync_unplug_thread, hp, "sync_unplug/%d", cpu);
  15839. + if (IS_ERR(hp->sync_tsk)) {
  15840. + err = PTR_ERR(hp->sync_tsk);
  15841. + hp->sync_tsk = NULL;
  15842. + return err;
  15843. + }
  15844. + kthread_bind(hp->sync_tsk, cpu);
  15845. +
  15846. + /*
  15847. + * Wait for tasks to get out of the pinned sections,
  15848. + * it's still OK if new tasks enter. Some CPU notifiers will
  15849. + * wait for tasks that are going to enter these sections and
  15850. + * we must not have them block.
  15851. + */
  15852. + wake_up_process(hp->sync_tsk);
  15853. + return 0;
  15854. +}
  15855. +
  15856. +static void cpu_unplug_sync(unsigned int cpu)
  15857. +{
  15858. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15859. +
  15860. + init_completion(&hp->synced);
  15861. + /* The completion needs to be initialzied before setting grab_lock */
  15862. + smp_wmb();
  15863. +
  15864. + /* Grab the mutex before setting grab_lock */
  15865. + hotplug_lock(hp);
  15866. + hp->grab_lock = 1;
  15867. +
  15868. + /*
  15869. + * The CPU notifiers have been completed.
  15870. + * Wait for tasks to get out of pinned CPU sections and have new
  15871. + * tasks block until the CPU is completely down.
  15872. + */
  15873. + __cpu_unplug_sync(hp);
  15874. +
  15875. + /* All done with the sync thread */
  15876. + kthread_stop(hp->sync_tsk);
  15877. + hp->sync_tsk = NULL;
  15878. +}
  15879. +
  15880. +static void cpu_unplug_done(unsigned int cpu)
  15881. +{
  15882. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15883. +
  15884. + hp->unplug = NULL;
  15885. + /* Let all tasks know cpu unplug is finished before cleaning up */
  15886. + smp_wmb();
  15887. +
  15888. + if (hp->sync_tsk)
  15889. + kthread_stop(hp->sync_tsk);
  15890. +
  15891. + if (hp->grab_lock) {
  15892. + hotplug_unlock(hp);
  15893. + /* protected by cpu_hotplug.lock */
  15894. + hp->grab_lock = 0;
  15895. + }
  15896. + tell_sched_cpu_down_done(cpu);
  15897. +}
  15898. void get_online_cpus(void)
  15899. {
  15900. @@ -338,13 +621,15 @@ static int take_cpu_down(void *_param)
  15901. /* Requires cpu_add_remove_lock to be held */
  15902. static int _cpu_down(unsigned int cpu, int tasks_frozen)
  15903. {
  15904. - int err, nr_calls = 0;
  15905. + int mycpu, err, nr_calls = 0;
  15906. void *hcpu = (void *)(long)cpu;
  15907. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  15908. struct take_cpu_down_param tcd_param = {
  15909. .mod = mod,
  15910. .hcpu = hcpu,
  15911. };
  15912. + cpumask_var_t cpumask;
  15913. + cpumask_var_t cpumask_org;
  15914. if (num_online_cpus() == 1)
  15915. return -EBUSY;
  15916. @@ -352,7 +637,34 @@ static int _cpu_down(unsigned int cpu, int tasks_frozen)
  15917. if (!cpu_online(cpu))
  15918. return -EINVAL;
  15919. + /* Move the downtaker off the unplug cpu */
  15920. + if (!alloc_cpumask_var(&cpumask, GFP_KERNEL))
  15921. + return -ENOMEM;
  15922. + if (!alloc_cpumask_var(&cpumask_org, GFP_KERNEL)) {
  15923. + free_cpumask_var(cpumask);
  15924. + return -ENOMEM;
  15925. + }
  15926. +
  15927. + cpumask_copy(cpumask_org, tsk_cpus_allowed(current));
  15928. + cpumask_andnot(cpumask, cpu_online_mask, cpumask_of(cpu));
  15929. + set_cpus_allowed_ptr(current, cpumask);
  15930. + free_cpumask_var(cpumask);
  15931. + migrate_disable();
  15932. + mycpu = smp_processor_id();
  15933. + if (mycpu == cpu) {
  15934. + printk(KERN_ERR "Yuck! Still on unplug CPU\n!");
  15935. + migrate_enable();
  15936. + err = -EBUSY;
  15937. + goto restore_cpus;
  15938. + }
  15939. + migrate_enable();
  15940. +
  15941. cpu_hotplug_begin();
  15942. + err = cpu_unplug_begin(cpu);
  15943. + if (err) {
  15944. + printk("cpu_unplug_begin(%d) failed\n", cpu);
  15945. + goto out_cancel;
  15946. + }
  15947. err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
  15948. if (err) {
  15949. @@ -378,8 +690,12 @@ static int _cpu_down(unsigned int cpu, int tasks_frozen)
  15950. else
  15951. synchronize_rcu();
  15952. + __cpu_unplug_wait(cpu);
  15953. smpboot_park_threads(cpu);
  15954. + /* Notifiers are done. Don't let any more tasks pin this CPU. */
  15955. + cpu_unplug_sync(cpu);
  15956. +
  15957. /*
  15958. * Prevent irq alloc/free while the dying cpu reorganizes the
  15959. * interrupt affinities.
  15960. @@ -424,9 +740,14 @@ static int _cpu_down(unsigned int cpu, int tasks_frozen)
  15961. check_for_tasks(cpu);
  15962. out_release:
  15963. + cpu_unplug_done(cpu);
  15964. +out_cancel:
  15965. cpu_hotplug_done();
  15966. if (!err)
  15967. cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
  15968. +restore_cpus:
  15969. + set_cpus_allowed_ptr(current, cpumask_org);
  15970. + free_cpumask_var(cpumask_org);
  15971. return err;
  15972. }
  15973. diff --git a/kernel/cpu_pm.c b/kernel/cpu_pm.c
  15974. index 009cc9a17d95..10f4640f991e 100644
  15975. --- a/kernel/cpu_pm.c
  15976. +++ b/kernel/cpu_pm.c
  15977. @@ -22,14 +22,13 @@
  15978. #include <linux/spinlock.h>
  15979. #include <linux/syscore_ops.h>
  15980. -static DEFINE_RWLOCK(cpu_pm_notifier_lock);
  15981. -static RAW_NOTIFIER_HEAD(cpu_pm_notifier_chain);
  15982. +static ATOMIC_NOTIFIER_HEAD(cpu_pm_notifier_chain);
  15983. static int cpu_pm_notify(enum cpu_pm_event event, int nr_to_call, int *nr_calls)
  15984. {
  15985. int ret;
  15986. - ret = __raw_notifier_call_chain(&cpu_pm_notifier_chain, event, NULL,
  15987. + ret = __atomic_notifier_call_chain(&cpu_pm_notifier_chain, event, NULL,
  15988. nr_to_call, nr_calls);
  15989. return notifier_to_errno(ret);
  15990. @@ -47,14 +46,7 @@ static int cpu_pm_notify(enum cpu_pm_event event, int nr_to_call, int *nr_calls)
  15991. */
  15992. int cpu_pm_register_notifier(struct notifier_block *nb)
  15993. {
  15994. - unsigned long flags;
  15995. - int ret;
  15996. -
  15997. - write_lock_irqsave(&cpu_pm_notifier_lock, flags);
  15998. - ret = raw_notifier_chain_register(&cpu_pm_notifier_chain, nb);
  15999. - write_unlock_irqrestore(&cpu_pm_notifier_lock, flags);
  16000. -
  16001. - return ret;
  16002. + return atomic_notifier_chain_register(&cpu_pm_notifier_chain, nb);
  16003. }
  16004. EXPORT_SYMBOL_GPL(cpu_pm_register_notifier);
  16005. @@ -69,14 +61,7 @@ EXPORT_SYMBOL_GPL(cpu_pm_register_notifier);
  16006. */
  16007. int cpu_pm_unregister_notifier(struct notifier_block *nb)
  16008. {
  16009. - unsigned long flags;
  16010. - int ret;
  16011. -
  16012. - write_lock_irqsave(&cpu_pm_notifier_lock, flags);
  16013. - ret = raw_notifier_chain_unregister(&cpu_pm_notifier_chain, nb);
  16014. - write_unlock_irqrestore(&cpu_pm_notifier_lock, flags);
  16015. -
  16016. - return ret;
  16017. + return atomic_notifier_chain_unregister(&cpu_pm_notifier_chain, nb);
  16018. }
  16019. EXPORT_SYMBOL_GPL(cpu_pm_unregister_notifier);
  16020. @@ -100,7 +85,6 @@ int cpu_pm_enter(void)
  16021. int nr_calls;
  16022. int ret = 0;
  16023. - read_lock(&cpu_pm_notifier_lock);
  16024. ret = cpu_pm_notify(CPU_PM_ENTER, -1, &nr_calls);
  16025. if (ret)
  16026. /*
  16027. @@ -108,7 +92,6 @@ int cpu_pm_enter(void)
  16028. * PM entry who are notified earlier to prepare for it.
  16029. */
  16030. cpu_pm_notify(CPU_PM_ENTER_FAILED, nr_calls - 1, NULL);
  16031. - read_unlock(&cpu_pm_notifier_lock);
  16032. return ret;
  16033. }
  16034. @@ -128,13 +111,7 @@ EXPORT_SYMBOL_GPL(cpu_pm_enter);
  16035. */
  16036. int cpu_pm_exit(void)
  16037. {
  16038. - int ret;
  16039. -
  16040. - read_lock(&cpu_pm_notifier_lock);
  16041. - ret = cpu_pm_notify(CPU_PM_EXIT, -1, NULL);
  16042. - read_unlock(&cpu_pm_notifier_lock);
  16043. -
  16044. - return ret;
  16045. + return cpu_pm_notify(CPU_PM_EXIT, -1, NULL);
  16046. }
  16047. EXPORT_SYMBOL_GPL(cpu_pm_exit);
  16048. @@ -159,7 +136,6 @@ int cpu_cluster_pm_enter(void)
  16049. int nr_calls;
  16050. int ret = 0;
  16051. - read_lock(&cpu_pm_notifier_lock);
  16052. ret = cpu_pm_notify(CPU_CLUSTER_PM_ENTER, -1, &nr_calls);
  16053. if (ret)
  16054. /*
  16055. @@ -167,7 +143,6 @@ int cpu_cluster_pm_enter(void)
  16056. * PM entry who are notified earlier to prepare for it.
  16057. */
  16058. cpu_pm_notify(CPU_CLUSTER_PM_ENTER_FAILED, nr_calls - 1, NULL);
  16059. - read_unlock(&cpu_pm_notifier_lock);
  16060. return ret;
  16061. }
  16062. @@ -190,13 +165,7 @@ EXPORT_SYMBOL_GPL(cpu_cluster_pm_enter);
  16063. */
  16064. int cpu_cluster_pm_exit(void)
  16065. {
  16066. - int ret;
  16067. -
  16068. - read_lock(&cpu_pm_notifier_lock);
  16069. - ret = cpu_pm_notify(CPU_CLUSTER_PM_EXIT, -1, NULL);
  16070. - read_unlock(&cpu_pm_notifier_lock);
  16071. -
  16072. - return ret;
  16073. + return cpu_pm_notify(CPU_CLUSTER_PM_EXIT, -1, NULL);
  16074. }
  16075. EXPORT_SYMBOL_GPL(cpu_cluster_pm_exit);
  16076. diff --git a/kernel/cpuset.c b/kernel/cpuset.c
  16077. index dd3ae6ee064d..8dc21a9ac292 100644
  16078. --- a/kernel/cpuset.c
  16079. +++ b/kernel/cpuset.c
  16080. @@ -284,7 +284,7 @@ static struct cpuset top_cpuset = {
  16081. */
  16082. static DEFINE_MUTEX(cpuset_mutex);
  16083. -static DEFINE_SPINLOCK(callback_lock);
  16084. +static DEFINE_RAW_SPINLOCK(callback_lock);
  16085. static struct workqueue_struct *cpuset_migrate_mm_wq;
  16086. @@ -907,9 +907,9 @@ static void update_cpumasks_hier(struct cpuset *cs, struct cpumask *new_cpus)
  16087. continue;
  16088. rcu_read_unlock();
  16089. - spin_lock_irq(&callback_lock);
  16090. + raw_spin_lock_irq(&callback_lock);
  16091. cpumask_copy(cp->effective_cpus, new_cpus);
  16092. - spin_unlock_irq(&callback_lock);
  16093. + raw_spin_unlock_irq(&callback_lock);
  16094. WARN_ON(!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
  16095. !cpumask_equal(cp->cpus_allowed, cp->effective_cpus));
  16096. @@ -974,9 +974,9 @@ static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
  16097. if (retval < 0)
  16098. return retval;
  16099. - spin_lock_irq(&callback_lock);
  16100. + raw_spin_lock_irq(&callback_lock);
  16101. cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
  16102. - spin_unlock_irq(&callback_lock);
  16103. + raw_spin_unlock_irq(&callback_lock);
  16104. /* use trialcs->cpus_allowed as a temp variable */
  16105. update_cpumasks_hier(cs, trialcs->cpus_allowed);
  16106. @@ -1185,9 +1185,9 @@ static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems)
  16107. continue;
  16108. rcu_read_unlock();
  16109. - spin_lock_irq(&callback_lock);
  16110. + raw_spin_lock_irq(&callback_lock);
  16111. cp->effective_mems = *new_mems;
  16112. - spin_unlock_irq(&callback_lock);
  16113. + raw_spin_unlock_irq(&callback_lock);
  16114. WARN_ON(!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
  16115. !nodes_equal(cp->mems_allowed, cp->effective_mems));
  16116. @@ -1255,9 +1255,9 @@ static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
  16117. if (retval < 0)
  16118. goto done;
  16119. - spin_lock_irq(&callback_lock);
  16120. + raw_spin_lock_irq(&callback_lock);
  16121. cs->mems_allowed = trialcs->mems_allowed;
  16122. - spin_unlock_irq(&callback_lock);
  16123. + raw_spin_unlock_irq(&callback_lock);
  16124. /* use trialcs->mems_allowed as a temp variable */
  16125. update_nodemasks_hier(cs, &trialcs->mems_allowed);
  16126. @@ -1348,9 +1348,9 @@ static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
  16127. spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs))
  16128. || (is_spread_page(cs) != is_spread_page(trialcs)));
  16129. - spin_lock_irq(&callback_lock);
  16130. + raw_spin_lock_irq(&callback_lock);
  16131. cs->flags = trialcs->flags;
  16132. - spin_unlock_irq(&callback_lock);
  16133. + raw_spin_unlock_irq(&callback_lock);
  16134. if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed)
  16135. rebuild_sched_domains_locked();
  16136. @@ -1762,7 +1762,7 @@ static int cpuset_common_seq_show(struct seq_file *sf, void *v)
  16137. cpuset_filetype_t type = seq_cft(sf)->private;
  16138. int ret = 0;
  16139. - spin_lock_irq(&callback_lock);
  16140. + raw_spin_lock_irq(&callback_lock);
  16141. switch (type) {
  16142. case FILE_CPULIST:
  16143. @@ -1781,7 +1781,7 @@ static int cpuset_common_seq_show(struct seq_file *sf, void *v)
  16144. ret = -EINVAL;
  16145. }
  16146. - spin_unlock_irq(&callback_lock);
  16147. + raw_spin_unlock_irq(&callback_lock);
  16148. return ret;
  16149. }
  16150. @@ -1996,12 +1996,12 @@ static int cpuset_css_online(struct cgroup_subsys_state *css)
  16151. cpuset_inc();
  16152. - spin_lock_irq(&callback_lock);
  16153. + raw_spin_lock_irq(&callback_lock);
  16154. if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys)) {
  16155. cpumask_copy(cs->effective_cpus, parent->effective_cpus);
  16156. cs->effective_mems = parent->effective_mems;
  16157. }
  16158. - spin_unlock_irq(&callback_lock);
  16159. + raw_spin_unlock_irq(&callback_lock);
  16160. if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags))
  16161. goto out_unlock;
  16162. @@ -2028,12 +2028,12 @@ static int cpuset_css_online(struct cgroup_subsys_state *css)
  16163. }
  16164. rcu_read_unlock();
  16165. - spin_lock_irq(&callback_lock);
  16166. + raw_spin_lock_irq(&callback_lock);
  16167. cs->mems_allowed = parent->mems_allowed;
  16168. cs->effective_mems = parent->mems_allowed;
  16169. cpumask_copy(cs->cpus_allowed, parent->cpus_allowed);
  16170. cpumask_copy(cs->effective_cpus, parent->cpus_allowed);
  16171. - spin_unlock_irq(&callback_lock);
  16172. + raw_spin_unlock_irq(&callback_lock);
  16173. out_unlock:
  16174. mutex_unlock(&cpuset_mutex);
  16175. return 0;
  16176. @@ -2072,7 +2072,7 @@ static void cpuset_css_free(struct cgroup_subsys_state *css)
  16177. static void cpuset_bind(struct cgroup_subsys_state *root_css)
  16178. {
  16179. mutex_lock(&cpuset_mutex);
  16180. - spin_lock_irq(&callback_lock);
  16181. + raw_spin_lock_irq(&callback_lock);
  16182. if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys)) {
  16183. cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask);
  16184. @@ -2083,7 +2083,7 @@ static void cpuset_bind(struct cgroup_subsys_state *root_css)
  16185. top_cpuset.mems_allowed = top_cpuset.effective_mems;
  16186. }
  16187. - spin_unlock_irq(&callback_lock);
  16188. + raw_spin_unlock_irq(&callback_lock);
  16189. mutex_unlock(&cpuset_mutex);
  16190. }
  16191. @@ -2184,12 +2184,12 @@ hotplug_update_tasks_legacy(struct cpuset *cs,
  16192. {
  16193. bool is_empty;
  16194. - spin_lock_irq(&callback_lock);
  16195. + raw_spin_lock_irq(&callback_lock);
  16196. cpumask_copy(cs->cpus_allowed, new_cpus);
  16197. cpumask_copy(cs->effective_cpus, new_cpus);
  16198. cs->mems_allowed = *new_mems;
  16199. cs->effective_mems = *new_mems;
  16200. - spin_unlock_irq(&callback_lock);
  16201. + raw_spin_unlock_irq(&callback_lock);
  16202. /*
  16203. * Don't call update_tasks_cpumask() if the cpuset becomes empty,
  16204. @@ -2226,10 +2226,10 @@ hotplug_update_tasks(struct cpuset *cs,
  16205. if (nodes_empty(*new_mems))
  16206. *new_mems = parent_cs(cs)->effective_mems;
  16207. - spin_lock_irq(&callback_lock);
  16208. + raw_spin_lock_irq(&callback_lock);
  16209. cpumask_copy(cs->effective_cpus, new_cpus);
  16210. cs->effective_mems = *new_mems;
  16211. - spin_unlock_irq(&callback_lock);
  16212. + raw_spin_unlock_irq(&callback_lock);
  16213. if (cpus_updated)
  16214. update_tasks_cpumask(cs);
  16215. @@ -2322,21 +2322,21 @@ static void cpuset_hotplug_workfn(struct work_struct *work)
  16216. /* synchronize cpus_allowed to cpu_active_mask */
  16217. if (cpus_updated) {
  16218. - spin_lock_irq(&callback_lock);
  16219. + raw_spin_lock_irq(&callback_lock);
  16220. if (!on_dfl)
  16221. cpumask_copy(top_cpuset.cpus_allowed, &new_cpus);
  16222. cpumask_copy(top_cpuset.effective_cpus, &new_cpus);
  16223. - spin_unlock_irq(&callback_lock);
  16224. + raw_spin_unlock_irq(&callback_lock);
  16225. /* we don't mess with cpumasks of tasks in top_cpuset */
  16226. }
  16227. /* synchronize mems_allowed to N_MEMORY */
  16228. if (mems_updated) {
  16229. - spin_lock_irq(&callback_lock);
  16230. + raw_spin_lock_irq(&callback_lock);
  16231. if (!on_dfl)
  16232. top_cpuset.mems_allowed = new_mems;
  16233. top_cpuset.effective_mems = new_mems;
  16234. - spin_unlock_irq(&callback_lock);
  16235. + raw_spin_unlock_irq(&callback_lock);
  16236. update_tasks_nodemask(&top_cpuset);
  16237. }
  16238. @@ -2441,11 +2441,11 @@ void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
  16239. {
  16240. unsigned long flags;
  16241. - spin_lock_irqsave(&callback_lock, flags);
  16242. + raw_spin_lock_irqsave(&callback_lock, flags);
  16243. rcu_read_lock();
  16244. guarantee_online_cpus(task_cs(tsk), pmask);
  16245. rcu_read_unlock();
  16246. - spin_unlock_irqrestore(&callback_lock, flags);
  16247. + raw_spin_unlock_irqrestore(&callback_lock, flags);
  16248. }
  16249. void cpuset_cpus_allowed_fallback(struct task_struct *tsk)
  16250. @@ -2493,11 +2493,11 @@ nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
  16251. nodemask_t mask;
  16252. unsigned long flags;
  16253. - spin_lock_irqsave(&callback_lock, flags);
  16254. + raw_spin_lock_irqsave(&callback_lock, flags);
  16255. rcu_read_lock();
  16256. guarantee_online_mems(task_cs(tsk), &mask);
  16257. rcu_read_unlock();
  16258. - spin_unlock_irqrestore(&callback_lock, flags);
  16259. + raw_spin_unlock_irqrestore(&callback_lock, flags);
  16260. return mask;
  16261. }
  16262. @@ -2589,14 +2589,14 @@ int __cpuset_node_allowed(int node, gfp_t gfp_mask)
  16263. return 1;
  16264. /* Not hardwall and node outside mems_allowed: scan up cpusets */
  16265. - spin_lock_irqsave(&callback_lock, flags);
  16266. + raw_spin_lock_irqsave(&callback_lock, flags);
  16267. rcu_read_lock();
  16268. cs = nearest_hardwall_ancestor(task_cs(current));
  16269. allowed = node_isset(node, cs->mems_allowed);
  16270. rcu_read_unlock();
  16271. - spin_unlock_irqrestore(&callback_lock, flags);
  16272. + raw_spin_unlock_irqrestore(&callback_lock, flags);
  16273. return allowed;
  16274. }
  16275. diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
  16276. index 77777d918676..3203e9dee9f8 100644
  16277. --- a/kernel/debug/kdb/kdb_io.c
  16278. +++ b/kernel/debug/kdb/kdb_io.c
  16279. @@ -554,7 +554,6 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
  16280. int linecount;
  16281. int colcount;
  16282. int logging, saved_loglevel = 0;
  16283. - int saved_trap_printk;
  16284. int got_printf_lock = 0;
  16285. int retlen = 0;
  16286. int fnd, len;
  16287. @@ -565,8 +564,6 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
  16288. unsigned long uninitialized_var(flags);
  16289. preempt_disable();
  16290. - saved_trap_printk = kdb_trap_printk;
  16291. - kdb_trap_printk = 0;
  16292. /* Serialize kdb_printf if multiple cpus try to write at once.
  16293. * But if any cpu goes recursive in kdb, just print the output,
  16294. @@ -855,7 +852,6 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
  16295. } else {
  16296. __release(kdb_printf_lock);
  16297. }
  16298. - kdb_trap_printk = saved_trap_printk;
  16299. preempt_enable();
  16300. return retlen;
  16301. }
  16302. @@ -865,9 +861,11 @@ int kdb_printf(const char *fmt, ...)
  16303. va_list ap;
  16304. int r;
  16305. + kdb_trap_printk++;
  16306. va_start(ap, fmt);
  16307. r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
  16308. va_end(ap);
  16309. + kdb_trap_printk--;
  16310. return r;
  16311. }
  16312. diff --git a/kernel/events/core.c b/kernel/events/core.c
  16313. index 8f75386e61a7..489cf0e1ac9c 100644
  16314. --- a/kernel/events/core.c
  16315. +++ b/kernel/events/core.c
  16316. @@ -802,6 +802,7 @@ static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
  16317. raw_spin_lock_init(&cpuctx->hrtimer_lock);
  16318. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
  16319. timer->function = perf_mux_hrtimer_handler;
  16320. + timer->irqsafe = 1;
  16321. }
  16322. static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
  16323. @@ -7241,6 +7242,7 @@ static void perf_swevent_init_hrtimer(struct perf_event *event)
  16324. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  16325. hwc->hrtimer.function = perf_swevent_hrtimer;
  16326. + hwc->hrtimer.irqsafe = 1;
  16327. /*
  16328. * Since hrtimers have a fixed rate, we can do a static freq->period
  16329. diff --git a/kernel/exit.c b/kernel/exit.c
  16330. index ffba5df4abd5..e199407f8831 100644
  16331. --- a/kernel/exit.c
  16332. +++ b/kernel/exit.c
  16333. @@ -144,7 +144,7 @@ static void __exit_signal(struct task_struct *tsk)
  16334. * Do this under ->siglock, we can race with another thread
  16335. * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
  16336. */
  16337. - flush_sigqueue(&tsk->pending);
  16338. + flush_task_sigqueue(tsk);
  16339. tsk->sighand = NULL;
  16340. spin_unlock(&sighand->siglock);
  16341. diff --git a/kernel/fork.c b/kernel/fork.c
  16342. index ac00f14208b7..9859641947bd 100644
  16343. --- a/kernel/fork.c
  16344. +++ b/kernel/fork.c
  16345. @@ -109,7 +109,7 @@ int max_threads; /* tunable limit on nr_threads */
  16346. DEFINE_PER_CPU(unsigned long, process_counts) = 0;
  16347. -__cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
  16348. +DEFINE_RWLOCK(tasklist_lock); /* outer */
  16349. #ifdef CONFIG_PROVE_RCU
  16350. int lockdep_tasklist_lock_is_held(void)
  16351. @@ -246,7 +246,9 @@ static inline void put_signal_struct(struct signal_struct *sig)
  16352. if (atomic_dec_and_test(&sig->sigcnt))
  16353. free_signal_struct(sig);
  16354. }
  16355. -
  16356. +#ifdef CONFIG_PREEMPT_RT_BASE
  16357. +static
  16358. +#endif
  16359. void __put_task_struct(struct task_struct *tsk)
  16360. {
  16361. WARN_ON(!tsk->exit_state);
  16362. @@ -263,7 +265,18 @@ void __put_task_struct(struct task_struct *tsk)
  16363. if (!profile_handoff_task(tsk))
  16364. free_task(tsk);
  16365. }
  16366. +#ifndef CONFIG_PREEMPT_RT_BASE
  16367. EXPORT_SYMBOL_GPL(__put_task_struct);
  16368. +#else
  16369. +void __put_task_struct_cb(struct rcu_head *rhp)
  16370. +{
  16371. + struct task_struct *tsk = container_of(rhp, struct task_struct, put_rcu);
  16372. +
  16373. + __put_task_struct(tsk);
  16374. +
  16375. +}
  16376. +EXPORT_SYMBOL_GPL(__put_task_struct_cb);
  16377. +#endif
  16378. void __init __weak arch_task_cache_init(void) { }
  16379. @@ -388,6 +401,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
  16380. tsk->splice_pipe = NULL;
  16381. tsk->task_frag.page = NULL;
  16382. tsk->wake_q.next = NULL;
  16383. + tsk->wake_q_sleeper.next = NULL;
  16384. account_kernel_stack(ti, 1);
  16385. @@ -699,6 +713,19 @@ void __mmdrop(struct mm_struct *mm)
  16386. }
  16387. EXPORT_SYMBOL_GPL(__mmdrop);
  16388. +#ifdef CONFIG_PREEMPT_RT_BASE
  16389. +/*
  16390. + * RCU callback for delayed mm drop. Not strictly rcu, but we don't
  16391. + * want another facility to make this work.
  16392. + */
  16393. +void __mmdrop_delayed(struct rcu_head *rhp)
  16394. +{
  16395. + struct mm_struct *mm = container_of(rhp, struct mm_struct, delayed_drop);
  16396. +
  16397. + __mmdrop(mm);
  16398. +}
  16399. +#endif
  16400. +
  16401. /*
  16402. * Decrement the use count and release all resources for an mm.
  16403. */
  16404. @@ -1249,6 +1276,9 @@ static void rt_mutex_init_task(struct task_struct *p)
  16405. */
  16406. static void posix_cpu_timers_init(struct task_struct *tsk)
  16407. {
  16408. +#ifdef CONFIG_PREEMPT_RT_BASE
  16409. + tsk->posix_timer_list = NULL;
  16410. +#endif
  16411. tsk->cputime_expires.prof_exp = 0;
  16412. tsk->cputime_expires.virt_exp = 0;
  16413. tsk->cputime_expires.sched_exp = 0;
  16414. @@ -1375,15 +1405,16 @@ static struct task_struct *copy_process(unsigned long clone_flags,
  16415. spin_lock_init(&p->alloc_lock);
  16416. init_sigpending(&p->pending);
  16417. + p->sigqueue_cache = NULL;
  16418. p->utime = p->stime = p->gtime = 0;
  16419. p->utimescaled = p->stimescaled = 0;
  16420. prev_cputime_init(&p->prev_cputime);
  16421. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  16422. - seqlock_init(&p->vtime_seqlock);
  16423. + seqcount_init(&p->vtime_seqcount);
  16424. p->vtime_snap = 0;
  16425. - p->vtime_snap_whence = VTIME_SLEEPING;
  16426. + p->vtime_snap_whence = VTIME_INACTIVE;
  16427. #endif
  16428. #if defined(SPLIT_RSS_COUNTING)
  16429. diff --git a/kernel/futex.c b/kernel/futex.c
  16430. index fc68462801de..b577ac5dc4a0 100644
  16431. --- a/kernel/futex.c
  16432. +++ b/kernel/futex.c
  16433. @@ -815,7 +815,9 @@ void exit_pi_state_list(struct task_struct *curr)
  16434. * task still owns the PI-state:
  16435. */
  16436. if (head->next != next) {
  16437. + raw_spin_unlock_irq(&curr->pi_lock);
  16438. spin_unlock(&hb->lock);
  16439. + raw_spin_lock_irq(&curr->pi_lock);
  16440. continue;
  16441. }
  16442. @@ -1210,6 +1212,7 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  16443. struct futex_pi_state *pi_state = this->pi_state;
  16444. u32 uninitialized_var(curval), newval;
  16445. WAKE_Q(wake_q);
  16446. + WAKE_Q(wake_sleeper_q);
  16447. bool deboost;
  16448. int ret = 0;
  16449. @@ -1223,7 +1226,7 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  16450. if (pi_state->owner != current)
  16451. return -EINVAL;
  16452. - raw_spin_lock(&pi_state->pi_mutex.wait_lock);
  16453. + raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
  16454. new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
  16455. /*
  16456. @@ -1259,24 +1262,25 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  16457. ret = -EINVAL;
  16458. }
  16459. if (ret) {
  16460. - raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  16461. + raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  16462. return ret;
  16463. }
  16464. - raw_spin_lock_irq(&pi_state->owner->pi_lock);
  16465. + raw_spin_lock(&pi_state->owner->pi_lock);
  16466. WARN_ON(list_empty(&pi_state->list));
  16467. list_del_init(&pi_state->list);
  16468. - raw_spin_unlock_irq(&pi_state->owner->pi_lock);
  16469. + raw_spin_unlock(&pi_state->owner->pi_lock);
  16470. - raw_spin_lock_irq(&new_owner->pi_lock);
  16471. + raw_spin_lock(&new_owner->pi_lock);
  16472. WARN_ON(!list_empty(&pi_state->list));
  16473. list_add(&pi_state->list, &new_owner->pi_state_list);
  16474. pi_state->owner = new_owner;
  16475. - raw_spin_unlock_irq(&new_owner->pi_lock);
  16476. + raw_spin_unlock(&new_owner->pi_lock);
  16477. - raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  16478. + raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  16479. - deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
  16480. + deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q,
  16481. + &wake_sleeper_q);
  16482. /*
  16483. * First unlock HB so the waiter does not spin on it once he got woken
  16484. @@ -1284,8 +1288,9 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  16485. * deboost first (and lose our higher priority), then the task might get
  16486. * scheduled away before the wake up can take place.
  16487. */
  16488. - spin_unlock(&hb->lock);
  16489. + deboost |= spin_unlock_no_deboost(&hb->lock);
  16490. wake_up_q(&wake_q);
  16491. + wake_up_q_sleeper(&wake_sleeper_q);
  16492. if (deboost)
  16493. rt_mutex_adjust_prio(current);
  16494. @@ -1822,6 +1827,16 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
  16495. requeue_pi_wake_futex(this, &key2, hb2);
  16496. drop_count++;
  16497. continue;
  16498. + } else if (ret == -EAGAIN) {
  16499. + /*
  16500. + * Waiter was woken by timeout or
  16501. + * signal and has set pi_blocked_on to
  16502. + * PI_WAKEUP_INPROGRESS before we
  16503. + * tried to enqueue it on the rtmutex.
  16504. + */
  16505. + this->pi_state = NULL;
  16506. + free_pi_state(pi_state);
  16507. + continue;
  16508. } else if (ret) {
  16509. /* -EDEADLK */
  16510. this->pi_state = NULL;
  16511. @@ -2143,11 +2158,11 @@ static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
  16512. * we returned due to timeout or signal without taking the
  16513. * rt_mutex. Too late.
  16514. */
  16515. - raw_spin_lock(&q->pi_state->pi_mutex.wait_lock);
  16516. + raw_spin_lock_irq(&q->pi_state->pi_mutex.wait_lock);
  16517. owner = rt_mutex_owner(&q->pi_state->pi_mutex);
  16518. if (!owner)
  16519. owner = rt_mutex_next_owner(&q->pi_state->pi_mutex);
  16520. - raw_spin_unlock(&q->pi_state->pi_mutex.wait_lock);
  16521. + raw_spin_unlock_irq(&q->pi_state->pi_mutex.wait_lock);
  16522. ret = fixup_pi_state_owner(uaddr, q, owner);
  16523. goto out;
  16524. }
  16525. @@ -2694,7 +2709,7 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  16526. {
  16527. struct hrtimer_sleeper timeout, *to = NULL;
  16528. struct rt_mutex_waiter rt_waiter;
  16529. - struct futex_hash_bucket *hb;
  16530. + struct futex_hash_bucket *hb, *hb2;
  16531. union futex_key key2 = FUTEX_KEY_INIT;
  16532. struct futex_q q = futex_q_init;
  16533. int res, ret;
  16534. @@ -2719,10 +2734,7 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  16535. * The waiter is allocated on our stack, manipulated by the requeue
  16536. * code while we sleep on uaddr.
  16537. */
  16538. - debug_rt_mutex_init_waiter(&rt_waiter);
  16539. - RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
  16540. - RB_CLEAR_NODE(&rt_waiter.tree_entry);
  16541. - rt_waiter.task = NULL;
  16542. + rt_mutex_init_waiter(&rt_waiter, false);
  16543. ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
  16544. if (unlikely(ret != 0))
  16545. @@ -2753,20 +2765,55 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  16546. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  16547. futex_wait_queue_me(hb, &q, to);
  16548. - spin_lock(&hb->lock);
  16549. - ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  16550. - spin_unlock(&hb->lock);
  16551. - if (ret)
  16552. - goto out_put_keys;
  16553. + /*
  16554. + * On RT we must avoid races with requeue and trying to block
  16555. + * on two mutexes (hb->lock and uaddr2's rtmutex) by
  16556. + * serializing access to pi_blocked_on with pi_lock.
  16557. + */
  16558. + raw_spin_lock_irq(&current->pi_lock);
  16559. + if (current->pi_blocked_on) {
  16560. + /*
  16561. + * We have been requeued or are in the process of
  16562. + * being requeued.
  16563. + */
  16564. + raw_spin_unlock_irq(&current->pi_lock);
  16565. + } else {
  16566. + /*
  16567. + * Setting pi_blocked_on to PI_WAKEUP_INPROGRESS
  16568. + * prevents a concurrent requeue from moving us to the
  16569. + * uaddr2 rtmutex. After that we can safely acquire
  16570. + * (and possibly block on) hb->lock.
  16571. + */
  16572. + current->pi_blocked_on = PI_WAKEUP_INPROGRESS;
  16573. + raw_spin_unlock_irq(&current->pi_lock);
  16574. +
  16575. + spin_lock(&hb->lock);
  16576. +
  16577. + /*
  16578. + * Clean up pi_blocked_on. We might leak it otherwise
  16579. + * when we succeeded with the hb->lock in the fast
  16580. + * path.
  16581. + */
  16582. + raw_spin_lock_irq(&current->pi_lock);
  16583. + current->pi_blocked_on = NULL;
  16584. + raw_spin_unlock_irq(&current->pi_lock);
  16585. +
  16586. + ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  16587. + spin_unlock(&hb->lock);
  16588. + if (ret)
  16589. + goto out_put_keys;
  16590. + }
  16591. /*
  16592. - * In order for us to be here, we know our q.key == key2, and since
  16593. - * we took the hb->lock above, we also know that futex_requeue() has
  16594. - * completed and we no longer have to concern ourselves with a wakeup
  16595. - * race with the atomic proxy lock acquisition by the requeue code. The
  16596. - * futex_requeue dropped our key1 reference and incremented our key2
  16597. - * reference count.
  16598. + * In order to be here, we have either been requeued, are in
  16599. + * the process of being requeued, or requeue successfully
  16600. + * acquired uaddr2 on our behalf. If pi_blocked_on was
  16601. + * non-null above, we may be racing with a requeue. Do not
  16602. + * rely on q->lock_ptr to be hb2->lock until after blocking on
  16603. + * hb->lock or hb2->lock. The futex_requeue dropped our key1
  16604. + * reference and incremented our key2 reference count.
  16605. */
  16606. + hb2 = hash_futex(&key2);
  16607. /* Check if the requeue code acquired the second futex for us. */
  16608. if (!q.rt_waiter) {
  16609. @@ -2775,7 +2822,8 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  16610. * did a lock-steal - fix up the PI-state in that case.
  16611. */
  16612. if (q.pi_state && (q.pi_state->owner != current)) {
  16613. - spin_lock(q.lock_ptr);
  16614. + spin_lock(&hb2->lock);
  16615. + BUG_ON(&hb2->lock != q.lock_ptr);
  16616. ret = fixup_pi_state_owner(uaddr2, &q, current);
  16617. if (ret && rt_mutex_owner(&q.pi_state->pi_mutex) == current)
  16618. rt_mutex_unlock(&q.pi_state->pi_mutex);
  16619. @@ -2784,7 +2832,7 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  16620. * the requeue_pi() code acquired for us.
  16621. */
  16622. free_pi_state(q.pi_state);
  16623. - spin_unlock(q.lock_ptr);
  16624. + spin_unlock(&hb2->lock);
  16625. }
  16626. } else {
  16627. struct rt_mutex *pi_mutex;
  16628. @@ -2799,7 +2847,8 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  16629. ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter);
  16630. debug_rt_mutex_free_waiter(&rt_waiter);
  16631. - spin_lock(q.lock_ptr);
  16632. + spin_lock(&hb2->lock);
  16633. + BUG_ON(&hb2->lock != q.lock_ptr);
  16634. /*
  16635. * Fixup the pi_state owner and possibly acquire the lock if we
  16636. * haven't already.
  16637. diff --git a/kernel/irq/handle.c b/kernel/irq/handle.c
  16638. index 57bff7857e87..6c65c9252991 100644
  16639. --- a/kernel/irq/handle.c
  16640. +++ b/kernel/irq/handle.c
  16641. @@ -134,6 +134,8 @@ void __irq_wake_thread(struct irq_desc *desc, struct irqaction *action)
  16642. irqreturn_t handle_irq_event_percpu(struct irq_desc *desc)
  16643. {
  16644. + struct pt_regs *regs = get_irq_regs();
  16645. + u64 ip = regs ? instruction_pointer(regs) : 0;
  16646. irqreturn_t retval = IRQ_NONE;
  16647. unsigned int flags = 0, irq = desc->irq_data.irq;
  16648. struct irqaction *action = desc->action;
  16649. @@ -176,7 +178,11 @@ irqreturn_t handle_irq_event_percpu(struct irq_desc *desc)
  16650. action = action->next;
  16651. }
  16652. - add_interrupt_randomness(irq, flags);
  16653. +#ifdef CONFIG_PREEMPT_RT_FULL
  16654. + desc->random_ip = ip;
  16655. +#else
  16656. + add_interrupt_randomness(irq, flags, ip);
  16657. +#endif
  16658. if (!noirqdebug)
  16659. note_interrupt(desc, retval);
  16660. diff --git a/kernel/irq/irqdesc.c b/kernel/irq/irqdesc.c
  16661. index 239e2ae2c947..0b73349a42d5 100644
  16662. --- a/kernel/irq/irqdesc.c
  16663. +++ b/kernel/irq/irqdesc.c
  16664. @@ -24,10 +24,27 @@
  16665. static struct lock_class_key irq_desc_lock_class;
  16666. #if defined(CONFIG_SMP)
  16667. +static int __init irq_affinity_setup(char *str)
  16668. +{
  16669. + zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  16670. + cpulist_parse(str, irq_default_affinity);
  16671. + /*
  16672. + * Set at least the boot cpu. We don't want to end up with
  16673. + * bugreports caused by random comandline masks
  16674. + */
  16675. + cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
  16676. + return 1;
  16677. +}
  16678. +__setup("irqaffinity=", irq_affinity_setup);
  16679. +
  16680. static void __init init_irq_default_affinity(void)
  16681. {
  16682. - alloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  16683. - cpumask_setall(irq_default_affinity);
  16684. +#ifdef CONFIG_CPUMASK_OFFSTACK
  16685. + if (!irq_default_affinity)
  16686. + zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  16687. +#endif
  16688. + if (cpumask_empty(irq_default_affinity))
  16689. + cpumask_setall(irq_default_affinity);
  16690. }
  16691. #else
  16692. static void __init init_irq_default_affinity(void)
  16693. diff --git a/kernel/irq/manage.c b/kernel/irq/manage.c
  16694. index a079ed14f230..2cf55df7ad0e 100644
  16695. --- a/kernel/irq/manage.c
  16696. +++ b/kernel/irq/manage.c
  16697. @@ -22,6 +22,7 @@
  16698. #include "internals.h"
  16699. #ifdef CONFIG_IRQ_FORCED_THREADING
  16700. +# ifndef CONFIG_PREEMPT_RT_BASE
  16701. __read_mostly bool force_irqthreads;
  16702. static int __init setup_forced_irqthreads(char *arg)
  16703. @@ -30,6 +31,7 @@ static int __init setup_forced_irqthreads(char *arg)
  16704. return 0;
  16705. }
  16706. early_param("threadirqs", setup_forced_irqthreads);
  16707. +# endif
  16708. #endif
  16709. static void __synchronize_hardirq(struct irq_desc *desc)
  16710. @@ -181,6 +183,62 @@ static inline void
  16711. irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
  16712. #endif
  16713. +#ifdef CONFIG_PREEMPT_RT_FULL
  16714. +static void _irq_affinity_notify(struct irq_affinity_notify *notify);
  16715. +static struct task_struct *set_affinity_helper;
  16716. +static LIST_HEAD(affinity_list);
  16717. +static DEFINE_RAW_SPINLOCK(affinity_list_lock);
  16718. +
  16719. +static int set_affinity_thread(void *unused)
  16720. +{
  16721. + while (1) {
  16722. + struct irq_affinity_notify *notify;
  16723. + int empty;
  16724. +
  16725. + set_current_state(TASK_INTERRUPTIBLE);
  16726. +
  16727. + raw_spin_lock_irq(&affinity_list_lock);
  16728. + empty = list_empty(&affinity_list);
  16729. + raw_spin_unlock_irq(&affinity_list_lock);
  16730. +
  16731. + if (empty)
  16732. + schedule();
  16733. + if (kthread_should_stop())
  16734. + break;
  16735. + set_current_state(TASK_RUNNING);
  16736. +try_next:
  16737. + notify = NULL;
  16738. +
  16739. + raw_spin_lock_irq(&affinity_list_lock);
  16740. + if (!list_empty(&affinity_list)) {
  16741. + notify = list_first_entry(&affinity_list,
  16742. + struct irq_affinity_notify, list);
  16743. + list_del_init(&notify->list);
  16744. + }
  16745. + raw_spin_unlock_irq(&affinity_list_lock);
  16746. +
  16747. + if (!notify)
  16748. + continue;
  16749. + _irq_affinity_notify(notify);
  16750. + goto try_next;
  16751. + }
  16752. + return 0;
  16753. +}
  16754. +
  16755. +static void init_helper_thread(void)
  16756. +{
  16757. + if (set_affinity_helper)
  16758. + return;
  16759. + set_affinity_helper = kthread_run(set_affinity_thread, NULL,
  16760. + "affinity-cb");
  16761. + WARN_ON(IS_ERR(set_affinity_helper));
  16762. +}
  16763. +#else
  16764. +
  16765. +static inline void init_helper_thread(void) { }
  16766. +
  16767. +#endif
  16768. +
  16769. int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
  16770. bool force)
  16771. {
  16772. @@ -220,7 +278,17 @@ int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
  16773. if (desc->affinity_notify) {
  16774. kref_get(&desc->affinity_notify->kref);
  16775. +
  16776. +#ifdef CONFIG_PREEMPT_RT_FULL
  16777. + raw_spin_lock(&affinity_list_lock);
  16778. + if (list_empty(&desc->affinity_notify->list))
  16779. + list_add_tail(&affinity_list,
  16780. + &desc->affinity_notify->list);
  16781. + raw_spin_unlock(&affinity_list_lock);
  16782. + wake_up_process(set_affinity_helper);
  16783. +#else
  16784. schedule_work(&desc->affinity_notify->work);
  16785. +#endif
  16786. }
  16787. irqd_set(data, IRQD_AFFINITY_SET);
  16788. @@ -258,10 +326,8 @@ int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
  16789. }
  16790. EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
  16791. -static void irq_affinity_notify(struct work_struct *work)
  16792. +static void _irq_affinity_notify(struct irq_affinity_notify *notify)
  16793. {
  16794. - struct irq_affinity_notify *notify =
  16795. - container_of(work, struct irq_affinity_notify, work);
  16796. struct irq_desc *desc = irq_to_desc(notify->irq);
  16797. cpumask_var_t cpumask;
  16798. unsigned long flags;
  16799. @@ -283,6 +349,13 @@ static void irq_affinity_notify(struct work_struct *work)
  16800. kref_put(&notify->kref, notify->release);
  16801. }
  16802. +static void irq_affinity_notify(struct work_struct *work)
  16803. +{
  16804. + struct irq_affinity_notify *notify =
  16805. + container_of(work, struct irq_affinity_notify, work);
  16806. + _irq_affinity_notify(notify);
  16807. +}
  16808. +
  16809. /**
  16810. * irq_set_affinity_notifier - control notification of IRQ affinity changes
  16811. * @irq: Interrupt for which to enable/disable notification
  16812. @@ -312,6 +385,8 @@ irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
  16813. notify->irq = irq;
  16814. kref_init(&notify->kref);
  16815. INIT_WORK(&notify->work, irq_affinity_notify);
  16816. + INIT_LIST_HEAD(&notify->list);
  16817. + init_helper_thread();
  16818. }
  16819. raw_spin_lock_irqsave(&desc->lock, flags);
  16820. @@ -865,7 +940,15 @@ irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
  16821. local_bh_disable();
  16822. ret = action->thread_fn(action->irq, action->dev_id);
  16823. irq_finalize_oneshot(desc, action);
  16824. - local_bh_enable();
  16825. + /*
  16826. + * Interrupts which have real time requirements can be set up
  16827. + * to avoid softirq processing in the thread handler. This is
  16828. + * safe as these interrupts do not raise soft interrupts.
  16829. + */
  16830. + if (irq_settings_no_softirq_call(desc))
  16831. + _local_bh_enable();
  16832. + else
  16833. + local_bh_enable();
  16834. return ret;
  16835. }
  16836. @@ -962,6 +1045,12 @@ static int irq_thread(void *data)
  16837. if (action_ret == IRQ_WAKE_THREAD)
  16838. irq_wake_secondary(desc, action);
  16839. +#ifdef CONFIG_PREEMPT_RT_FULL
  16840. + migrate_disable();
  16841. + add_interrupt_randomness(action->irq, 0,
  16842. + desc->random_ip ^ (unsigned long) action);
  16843. + migrate_enable();
  16844. +#endif
  16845. wake_threads_waitq(desc);
  16846. }
  16847. @@ -1317,6 +1406,9 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
  16848. irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
  16849. }
  16850. + if (new->flags & IRQF_NO_SOFTIRQ_CALL)
  16851. + irq_settings_set_no_softirq_call(desc);
  16852. +
  16853. /* Set default affinity mask once everything is setup */
  16854. setup_affinity(desc, mask);
  16855. @@ -1970,7 +2062,7 @@ EXPORT_SYMBOL_GPL(irq_get_irqchip_state);
  16856. * This call sets the internal irqchip state of an interrupt,
  16857. * depending on the value of @which.
  16858. *
  16859. - * This function should be called with preemption disabled if the
  16860. + * This function should be called with migration disabled if the
  16861. * interrupt controller has per-cpu registers.
  16862. */
  16863. int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
  16864. diff --git a/kernel/irq/settings.h b/kernel/irq/settings.h
  16865. index 320579d89091..2df2d4445b1e 100644
  16866. --- a/kernel/irq/settings.h
  16867. +++ b/kernel/irq/settings.h
  16868. @@ -16,6 +16,7 @@ enum {
  16869. _IRQ_PER_CPU_DEVID = IRQ_PER_CPU_DEVID,
  16870. _IRQ_IS_POLLED = IRQ_IS_POLLED,
  16871. _IRQ_DISABLE_UNLAZY = IRQ_DISABLE_UNLAZY,
  16872. + _IRQ_NO_SOFTIRQ_CALL = IRQ_NO_SOFTIRQ_CALL,
  16873. _IRQF_MODIFY_MASK = IRQF_MODIFY_MASK,
  16874. };
  16875. @@ -30,6 +31,7 @@ enum {
  16876. #define IRQ_PER_CPU_DEVID GOT_YOU_MORON
  16877. #define IRQ_IS_POLLED GOT_YOU_MORON
  16878. #define IRQ_DISABLE_UNLAZY GOT_YOU_MORON
  16879. +#define IRQ_NO_SOFTIRQ_CALL GOT_YOU_MORON
  16880. #undef IRQF_MODIFY_MASK
  16881. #define IRQF_MODIFY_MASK GOT_YOU_MORON
  16882. @@ -40,6 +42,16 @@ irq_settings_clr_and_set(struct irq_desc *desc, u32 clr, u32 set)
  16883. desc->status_use_accessors |= (set & _IRQF_MODIFY_MASK);
  16884. }
  16885. +static inline bool irq_settings_no_softirq_call(struct irq_desc *desc)
  16886. +{
  16887. + return desc->status_use_accessors & _IRQ_NO_SOFTIRQ_CALL;
  16888. +}
  16889. +
  16890. +static inline void irq_settings_set_no_softirq_call(struct irq_desc *desc)
  16891. +{
  16892. + desc->status_use_accessors |= _IRQ_NO_SOFTIRQ_CALL;
  16893. +}
  16894. +
  16895. static inline bool irq_settings_is_per_cpu(struct irq_desc *desc)
  16896. {
  16897. return desc->status_use_accessors & _IRQ_PER_CPU;
  16898. diff --git a/kernel/irq/spurious.c b/kernel/irq/spurious.c
  16899. index 32144175458d..ed26f2554972 100644
  16900. --- a/kernel/irq/spurious.c
  16901. +++ b/kernel/irq/spurious.c
  16902. @@ -444,6 +444,10 @@ MODULE_PARM_DESC(noirqdebug, "Disable irq lockup detection when true");
  16903. static int __init irqfixup_setup(char *str)
  16904. {
  16905. +#ifdef CONFIG_PREEMPT_RT_BASE
  16906. + pr_warn("irqfixup boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  16907. + return 1;
  16908. +#endif
  16909. irqfixup = 1;
  16910. printk(KERN_WARNING "Misrouted IRQ fixup support enabled.\n");
  16911. printk(KERN_WARNING "This may impact system performance.\n");
  16912. @@ -456,6 +460,10 @@ module_param(irqfixup, int, 0644);
  16913. static int __init irqpoll_setup(char *str)
  16914. {
  16915. +#ifdef CONFIG_PREEMPT_RT_BASE
  16916. + pr_warn("irqpoll boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  16917. + return 1;
  16918. +#endif
  16919. irqfixup = 2;
  16920. printk(KERN_WARNING "Misrouted IRQ fixup and polling support "
  16921. "enabled\n");
  16922. diff --git a/kernel/irq_work.c b/kernel/irq_work.c
  16923. index bcf107ce0854..2899ba0d23d1 100644
  16924. --- a/kernel/irq_work.c
  16925. +++ b/kernel/irq_work.c
  16926. @@ -17,6 +17,7 @@
  16927. #include <linux/cpu.h>
  16928. #include <linux/notifier.h>
  16929. #include <linux/smp.h>
  16930. +#include <linux/interrupt.h>
  16931. #include <asm/processor.h>
  16932. @@ -65,6 +66,8 @@ void __weak arch_irq_work_raise(void)
  16933. */
  16934. bool irq_work_queue_on(struct irq_work *work, int cpu)
  16935. {
  16936. + struct llist_head *list;
  16937. +
  16938. /* All work should have been flushed before going offline */
  16939. WARN_ON_ONCE(cpu_is_offline(cpu));
  16940. @@ -75,7 +78,12 @@ bool irq_work_queue_on(struct irq_work *work, int cpu)
  16941. if (!irq_work_claim(work))
  16942. return false;
  16943. - if (llist_add(&work->llnode, &per_cpu(raised_list, cpu)))
  16944. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL) && !(work->flags & IRQ_WORK_HARD_IRQ))
  16945. + list = &per_cpu(lazy_list, cpu);
  16946. + else
  16947. + list = &per_cpu(raised_list, cpu);
  16948. +
  16949. + if (llist_add(&work->llnode, list))
  16950. arch_send_call_function_single_ipi(cpu);
  16951. return true;
  16952. @@ -86,6 +94,9 @@ EXPORT_SYMBOL_GPL(irq_work_queue_on);
  16953. /* Enqueue the irq work @work on the current CPU */
  16954. bool irq_work_queue(struct irq_work *work)
  16955. {
  16956. + struct llist_head *list;
  16957. + bool lazy_work, realtime = IS_ENABLED(CONFIG_PREEMPT_RT_FULL);
  16958. +
  16959. /* Only queue if not already pending */
  16960. if (!irq_work_claim(work))
  16961. return false;
  16962. @@ -93,13 +104,15 @@ bool irq_work_queue(struct irq_work *work)
  16963. /* Queue the entry and raise the IPI if needed. */
  16964. preempt_disable();
  16965. - /* If the work is "lazy", handle it from next tick if any */
  16966. - if (work->flags & IRQ_WORK_LAZY) {
  16967. - if (llist_add(&work->llnode, this_cpu_ptr(&lazy_list)) &&
  16968. - tick_nohz_tick_stopped())
  16969. - arch_irq_work_raise();
  16970. - } else {
  16971. - if (llist_add(&work->llnode, this_cpu_ptr(&raised_list)))
  16972. + lazy_work = work->flags & IRQ_WORK_LAZY;
  16973. +
  16974. + if (lazy_work || (realtime && !(work->flags & IRQ_WORK_HARD_IRQ)))
  16975. + list = this_cpu_ptr(&lazy_list);
  16976. + else
  16977. + list = this_cpu_ptr(&raised_list);
  16978. +
  16979. + if (llist_add(&work->llnode, list)) {
  16980. + if (!lazy_work || tick_nohz_tick_stopped())
  16981. arch_irq_work_raise();
  16982. }
  16983. @@ -116,9 +129,8 @@ bool irq_work_needs_cpu(void)
  16984. raised = this_cpu_ptr(&raised_list);
  16985. lazy = this_cpu_ptr(&lazy_list);
  16986. - if (llist_empty(raised) || arch_irq_work_has_interrupt())
  16987. - if (llist_empty(lazy))
  16988. - return false;
  16989. + if (llist_empty(raised) && llist_empty(lazy))
  16990. + return false;
  16991. /* All work should have been flushed before going offline */
  16992. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  16993. @@ -132,7 +144,7 @@ static void irq_work_run_list(struct llist_head *list)
  16994. struct irq_work *work;
  16995. struct llist_node *llnode;
  16996. - BUG_ON(!irqs_disabled());
  16997. + BUG_ON_NONRT(!irqs_disabled());
  16998. if (llist_empty(list))
  16999. return;
  17000. @@ -169,7 +181,16 @@ static void irq_work_run_list(struct llist_head *list)
  17001. void irq_work_run(void)
  17002. {
  17003. irq_work_run_list(this_cpu_ptr(&raised_list));
  17004. - irq_work_run_list(this_cpu_ptr(&lazy_list));
  17005. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL)) {
  17006. + /*
  17007. + * NOTE: we raise softirq via IPI for safety,
  17008. + * and execute in irq_work_tick() to move the
  17009. + * overhead from hard to soft irq context.
  17010. + */
  17011. + if (!llist_empty(this_cpu_ptr(&lazy_list)))
  17012. + raise_softirq(TIMER_SOFTIRQ);
  17013. + } else
  17014. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  17015. }
  17016. EXPORT_SYMBOL_GPL(irq_work_run);
  17017. @@ -179,8 +200,17 @@ void irq_work_tick(void)
  17018. if (!llist_empty(raised) && !arch_irq_work_has_interrupt())
  17019. irq_work_run_list(raised);
  17020. +
  17021. + if (!IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  17022. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  17023. +}
  17024. +
  17025. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  17026. +void irq_work_tick_soft(void)
  17027. +{
  17028. irq_work_run_list(this_cpu_ptr(&lazy_list));
  17029. }
  17030. +#endif
  17031. /*
  17032. * Synchronize against the irq_work @entry, ensures the entry is not
  17033. diff --git a/kernel/ksysfs.c b/kernel/ksysfs.c
  17034. index e83b26464061..c0e08d1cf33e 100644
  17035. --- a/kernel/ksysfs.c
  17036. +++ b/kernel/ksysfs.c
  17037. @@ -136,6 +136,15 @@ KERNEL_ATTR_RO(vmcoreinfo);
  17038. #endif /* CONFIG_KEXEC_CORE */
  17039. +#if defined(CONFIG_PREEMPT_RT_FULL)
  17040. +static ssize_t realtime_show(struct kobject *kobj,
  17041. + struct kobj_attribute *attr, char *buf)
  17042. +{
  17043. + return sprintf(buf, "%d\n", 1);
  17044. +}
  17045. +KERNEL_ATTR_RO(realtime);
  17046. +#endif
  17047. +
  17048. /* whether file capabilities are enabled */
  17049. static ssize_t fscaps_show(struct kobject *kobj,
  17050. struct kobj_attribute *attr, char *buf)
  17051. @@ -203,6 +212,9 @@ static struct attribute * kernel_attrs[] = {
  17052. &vmcoreinfo_attr.attr,
  17053. #endif
  17054. &rcu_expedited_attr.attr,
  17055. +#ifdef CONFIG_PREEMPT_RT_FULL
  17056. + &realtime_attr.attr,
  17057. +#endif
  17058. NULL
  17059. };
  17060. diff --git a/kernel/locking/Makefile b/kernel/locking/Makefile
  17061. index 8e96f6cc2a4a..447b03082d88 100644
  17062. --- a/kernel/locking/Makefile
  17063. +++ b/kernel/locking/Makefile
  17064. @@ -1,5 +1,5 @@
  17065. -obj-y += mutex.o semaphore.o rwsem.o percpu-rwsem.o
  17066. +obj-y += semaphore.o percpu-rwsem.o
  17067. ifdef CONFIG_FUNCTION_TRACER
  17068. CFLAGS_REMOVE_lockdep.o = $(CC_FLAGS_FTRACE)
  17069. @@ -8,7 +8,11 @@ CFLAGS_REMOVE_mutex-debug.o = $(CC_FLAGS_FTRACE)
  17070. CFLAGS_REMOVE_rtmutex-debug.o = $(CC_FLAGS_FTRACE)
  17071. endif
  17072. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  17073. +obj-y += mutex.o
  17074. obj-$(CONFIG_DEBUG_MUTEXES) += mutex-debug.o
  17075. +obj-y += rwsem.o
  17076. +endif
  17077. obj-$(CONFIG_LOCKDEP) += lockdep.o
  17078. ifeq ($(CONFIG_PROC_FS),y)
  17079. obj-$(CONFIG_LOCKDEP) += lockdep_proc.o
  17080. @@ -22,7 +26,10 @@ obj-$(CONFIG_RT_MUTEXES) += rtmutex.o
  17081. obj-$(CONFIG_DEBUG_RT_MUTEXES) += rtmutex-debug.o
  17082. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o
  17083. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock_debug.o
  17084. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  17085. obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
  17086. obj-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem-xadd.o
  17087. +endif
  17088. +obj-$(CONFIG_PREEMPT_RT_FULL) += rt.o
  17089. obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o
  17090. obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o
  17091. diff --git a/kernel/locking/lglock.c b/kernel/locking/lglock.c
  17092. index 951cfcd10b4a..57e0ea72c28a 100644
  17093. --- a/kernel/locking/lglock.c
  17094. +++ b/kernel/locking/lglock.c
  17095. @@ -4,6 +4,15 @@
  17096. #include <linux/cpu.h>
  17097. #include <linux/string.h>
  17098. +#ifndef CONFIG_PREEMPT_RT_FULL
  17099. +# define lg_lock_ptr arch_spinlock_t
  17100. +# define lg_do_lock(l) arch_spin_lock(l)
  17101. +# define lg_do_unlock(l) arch_spin_unlock(l)
  17102. +#else
  17103. +# define lg_lock_ptr struct rt_mutex
  17104. +# define lg_do_lock(l) __rt_spin_lock__no_mg(l)
  17105. +# define lg_do_unlock(l) __rt_spin_unlock(l)
  17106. +#endif
  17107. /*
  17108. * Note there is no uninit, so lglocks cannot be defined in
  17109. * modules (but it's fine to use them from there)
  17110. @@ -12,51 +21,60 @@
  17111. void lg_lock_init(struct lglock *lg, char *name)
  17112. {
  17113. +#ifdef CONFIG_PREEMPT_RT_FULL
  17114. + int i;
  17115. +
  17116. + for_each_possible_cpu(i) {
  17117. + struct rt_mutex *lock = per_cpu_ptr(lg->lock, i);
  17118. +
  17119. + rt_mutex_init(lock);
  17120. + }
  17121. +#endif
  17122. LOCKDEP_INIT_MAP(&lg->lock_dep_map, name, &lg->lock_key, 0);
  17123. }
  17124. EXPORT_SYMBOL(lg_lock_init);
  17125. void lg_local_lock(struct lglock *lg)
  17126. {
  17127. - arch_spinlock_t *lock;
  17128. + lg_lock_ptr *lock;
  17129. - preempt_disable();
  17130. + migrate_disable();
  17131. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  17132. lock = this_cpu_ptr(lg->lock);
  17133. - arch_spin_lock(lock);
  17134. + lg_do_lock(lock);
  17135. }
  17136. EXPORT_SYMBOL(lg_local_lock);
  17137. void lg_local_unlock(struct lglock *lg)
  17138. {
  17139. - arch_spinlock_t *lock;
  17140. + lg_lock_ptr *lock;
  17141. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  17142. lock = this_cpu_ptr(lg->lock);
  17143. - arch_spin_unlock(lock);
  17144. - preempt_enable();
  17145. + lg_do_unlock(lock);
  17146. + migrate_enable();
  17147. }
  17148. EXPORT_SYMBOL(lg_local_unlock);
  17149. void lg_local_lock_cpu(struct lglock *lg, int cpu)
  17150. {
  17151. - arch_spinlock_t *lock;
  17152. + lg_lock_ptr *lock;
  17153. - preempt_disable();
  17154. + preempt_disable_nort();
  17155. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  17156. lock = per_cpu_ptr(lg->lock, cpu);
  17157. - arch_spin_lock(lock);
  17158. + lg_do_lock(lock);
  17159. }
  17160. EXPORT_SYMBOL(lg_local_lock_cpu);
  17161. void lg_local_unlock_cpu(struct lglock *lg, int cpu)
  17162. {
  17163. - arch_spinlock_t *lock;
  17164. + lg_lock_ptr *lock;
  17165. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  17166. lock = per_cpu_ptr(lg->lock, cpu);
  17167. - arch_spin_unlock(lock);
  17168. - preempt_enable();
  17169. + lg_do_unlock(lock);
  17170. + preempt_enable_nort();
  17171. }
  17172. EXPORT_SYMBOL(lg_local_unlock_cpu);
  17173. @@ -68,30 +86,30 @@ void lg_double_lock(struct lglock *lg, int cpu1, int cpu2)
  17174. if (cpu2 < cpu1)
  17175. swap(cpu1, cpu2);
  17176. - preempt_disable();
  17177. + preempt_disable_nort();
  17178. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  17179. - arch_spin_lock(per_cpu_ptr(lg->lock, cpu1));
  17180. - arch_spin_lock(per_cpu_ptr(lg->lock, cpu2));
  17181. + lg_do_lock(per_cpu_ptr(lg->lock, cpu1));
  17182. + lg_do_lock(per_cpu_ptr(lg->lock, cpu2));
  17183. }
  17184. void lg_double_unlock(struct lglock *lg, int cpu1, int cpu2)
  17185. {
  17186. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  17187. - arch_spin_unlock(per_cpu_ptr(lg->lock, cpu1));
  17188. - arch_spin_unlock(per_cpu_ptr(lg->lock, cpu2));
  17189. - preempt_enable();
  17190. + lg_do_unlock(per_cpu_ptr(lg->lock, cpu1));
  17191. + lg_do_unlock(per_cpu_ptr(lg->lock, cpu2));
  17192. + preempt_enable_nort();
  17193. }
  17194. void lg_global_lock(struct lglock *lg)
  17195. {
  17196. int i;
  17197. - preempt_disable();
  17198. + preempt_disable_nort();
  17199. lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  17200. for_each_possible_cpu(i) {
  17201. - arch_spinlock_t *lock;
  17202. + lg_lock_ptr *lock;
  17203. lock = per_cpu_ptr(lg->lock, i);
  17204. - arch_spin_lock(lock);
  17205. + lg_do_lock(lock);
  17206. }
  17207. }
  17208. EXPORT_SYMBOL(lg_global_lock);
  17209. @@ -102,10 +120,35 @@ void lg_global_unlock(struct lglock *lg)
  17210. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  17211. for_each_possible_cpu(i) {
  17212. - arch_spinlock_t *lock;
  17213. + lg_lock_ptr *lock;
  17214. lock = per_cpu_ptr(lg->lock, i);
  17215. - arch_spin_unlock(lock);
  17216. + lg_do_unlock(lock);
  17217. }
  17218. - preempt_enable();
  17219. + preempt_enable_nort();
  17220. }
  17221. EXPORT_SYMBOL(lg_global_unlock);
  17222. +
  17223. +#ifdef CONFIG_PREEMPT_RT_FULL
  17224. +/*
  17225. + * HACK: If you use this, you get to keep the pieces.
  17226. + * Used in queue_stop_cpus_work() when stop machinery
  17227. + * is called from inactive CPU, so we can't schedule.
  17228. + */
  17229. +# define lg_do_trylock_relax(l) \
  17230. + do { \
  17231. + while (!__rt_spin_trylock(l)) \
  17232. + cpu_relax(); \
  17233. + } while (0)
  17234. +
  17235. +void lg_global_trylock_relax(struct lglock *lg)
  17236. +{
  17237. + int i;
  17238. +
  17239. + lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  17240. + for_each_possible_cpu(i) {
  17241. + lg_lock_ptr *lock;
  17242. + lock = per_cpu_ptr(lg->lock, i);
  17243. + lg_do_trylock_relax(lock);
  17244. + }
  17245. +}
  17246. +#endif
  17247. diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
  17248. index 0e2c4911ba61..aed13ffa9fd3 100644
  17249. --- a/kernel/locking/lockdep.c
  17250. +++ b/kernel/locking/lockdep.c
  17251. @@ -668,6 +668,7 @@ look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
  17252. struct lockdep_subclass_key *key;
  17253. struct list_head *hash_head;
  17254. struct lock_class *class;
  17255. + bool is_static = false;
  17256. #ifdef CONFIG_DEBUG_LOCKDEP
  17257. /*
  17258. @@ -695,10 +696,23 @@ look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
  17259. /*
  17260. * Static locks do not have their class-keys yet - for them the key
  17261. - * is the lock object itself:
  17262. + * is the lock object itself. If the lock is in the per cpu area,
  17263. + * the canonical address of the lock (per cpu offset removed) is
  17264. + * used.
  17265. */
  17266. - if (unlikely(!lock->key))
  17267. - lock->key = (void *)lock;
  17268. + if (unlikely(!lock->key)) {
  17269. + unsigned long can_addr, addr = (unsigned long)lock;
  17270. +
  17271. + if (__is_kernel_percpu_address(addr, &can_addr))
  17272. + lock->key = (void *)can_addr;
  17273. + else if (__is_module_percpu_address(addr, &can_addr))
  17274. + lock->key = (void *)can_addr;
  17275. + else if (static_obj(lock))
  17276. + lock->key = (void *)lock;
  17277. + else
  17278. + return ERR_PTR(-EINVAL);
  17279. + is_static = true;
  17280. + }
  17281. /*
  17282. * NOTE: the class-key must be unique. For dynamic locks, a static
  17283. @@ -730,7 +744,7 @@ look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
  17284. }
  17285. }
  17286. - return NULL;
  17287. + return is_static || static_obj(lock->key) ? NULL : ERR_PTR(-EINVAL);
  17288. }
  17289. /*
  17290. @@ -748,19 +762,18 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
  17291. DEBUG_LOCKS_WARN_ON(!irqs_disabled());
  17292. class = look_up_lock_class(lock, subclass);
  17293. - if (likely(class))
  17294. + if (likely(!IS_ERR_OR_NULL(class)))
  17295. goto out_set_class_cache;
  17296. /*
  17297. * Debug-check: all keys must be persistent!
  17298. - */
  17299. - if (!static_obj(lock->key)) {
  17300. + */
  17301. + if (IS_ERR(class)) {
  17302. debug_locks_off();
  17303. printk("INFO: trying to register non-static key.\n");
  17304. printk("the code is fine but needs lockdep annotation.\n");
  17305. printk("turning off the locking correctness validator.\n");
  17306. dump_stack();
  17307. -
  17308. return NULL;
  17309. }
  17310. @@ -3285,7 +3298,7 @@ static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
  17311. * Clearly if the lock hasn't been acquired _ever_, we're not
  17312. * holding it either, so report failure.
  17313. */
  17314. - if (!class)
  17315. + if (IS_ERR_OR_NULL(class))
  17316. return 0;
  17317. /*
  17318. @@ -3532,6 +3545,7 @@ static void check_flags(unsigned long flags)
  17319. }
  17320. }
  17321. +#ifndef CONFIG_PREEMPT_RT_FULL
  17322. /*
  17323. * We dont accurately track softirq state in e.g.
  17324. * hardirq contexts (such as on 4KSTACKS), so only
  17325. @@ -3546,6 +3560,7 @@ static void check_flags(unsigned long flags)
  17326. DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
  17327. }
  17328. }
  17329. +#endif
  17330. if (!debug_locks)
  17331. print_irqtrace_events(current);
  17332. @@ -3984,7 +3999,7 @@ void lockdep_reset_lock(struct lockdep_map *lock)
  17333. * If the class exists we look it up and zap it:
  17334. */
  17335. class = look_up_lock_class(lock, j);
  17336. - if (class)
  17337. + if (!IS_ERR_OR_NULL(class))
  17338. zap_class(class);
  17339. }
  17340. /*
  17341. diff --git a/kernel/locking/locktorture.c b/kernel/locking/locktorture.c
  17342. index d580b7d6ee6d..4680c502b65b 100644
  17343. --- a/kernel/locking/locktorture.c
  17344. +++ b/kernel/locking/locktorture.c
  17345. @@ -26,7 +26,6 @@
  17346. #include <linux/kthread.h>
  17347. #include <linux/sched/rt.h>
  17348. #include <linux/spinlock.h>
  17349. -#include <linux/rwlock.h>
  17350. #include <linux/mutex.h>
  17351. #include <linux/rwsem.h>
  17352. #include <linux/smp.h>
  17353. diff --git a/kernel/locking/rt.c b/kernel/locking/rt.c
  17354. new file mode 100644
  17355. index 000000000000..d4ab61c1848b
  17356. --- /dev/null
  17357. +++ b/kernel/locking/rt.c
  17358. @@ -0,0 +1,474 @@
  17359. +/*
  17360. + * kernel/rt.c
  17361. + *
  17362. + * Real-Time Preemption Support
  17363. + *
  17364. + * started by Ingo Molnar:
  17365. + *
  17366. + * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  17367. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  17368. + *
  17369. + * historic credit for proving that Linux spinlocks can be implemented via
  17370. + * RT-aware mutexes goes to many people: The Pmutex project (Dirk Grambow
  17371. + * and others) who prototyped it on 2.4 and did lots of comparative
  17372. + * research and analysis; TimeSys, for proving that you can implement a
  17373. + * fully preemptible kernel via the use of IRQ threading and mutexes;
  17374. + * Bill Huey for persuasively arguing on lkml that the mutex model is the
  17375. + * right one; and to MontaVista, who ported pmutexes to 2.6.
  17376. + *
  17377. + * This code is a from-scratch implementation and is not based on pmutexes,
  17378. + * but the idea of converting spinlocks to mutexes is used here too.
  17379. + *
  17380. + * lock debugging, locking tree, deadlock detection:
  17381. + *
  17382. + * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey
  17383. + * Released under the General Public License (GPL).
  17384. + *
  17385. + * Includes portions of the generic R/W semaphore implementation from:
  17386. + *
  17387. + * Copyright (c) 2001 David Howells (dhowells@redhat.com).
  17388. + * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
  17389. + * - Derived also from comments by Linus
  17390. + *
  17391. + * Pending ownership of locks and ownership stealing:
  17392. + *
  17393. + * Copyright (C) 2005, Kihon Technologies Inc., Steven Rostedt
  17394. + *
  17395. + * (also by Steven Rostedt)
  17396. + * - Converted single pi_lock to individual task locks.
  17397. + *
  17398. + * By Esben Nielsen:
  17399. + * Doing priority inheritance with help of the scheduler.
  17400. + *
  17401. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  17402. + * - major rework based on Esben Nielsens initial patch
  17403. + * - replaced thread_info references by task_struct refs
  17404. + * - removed task->pending_owner dependency
  17405. + * - BKL drop/reacquire for semaphore style locks to avoid deadlocks
  17406. + * in the scheduler return path as discussed with Steven Rostedt
  17407. + *
  17408. + * Copyright (C) 2006, Kihon Technologies Inc.
  17409. + * Steven Rostedt <rostedt@goodmis.org>
  17410. + * - debugged and patched Thomas Gleixner's rework.
  17411. + * - added back the cmpxchg to the rework.
  17412. + * - turned atomic require back on for SMP.
  17413. + */
  17414. +
  17415. +#include <linux/spinlock.h>
  17416. +#include <linux/rtmutex.h>
  17417. +#include <linux/sched.h>
  17418. +#include <linux/delay.h>
  17419. +#include <linux/module.h>
  17420. +#include <linux/kallsyms.h>
  17421. +#include <linux/syscalls.h>
  17422. +#include <linux/interrupt.h>
  17423. +#include <linux/plist.h>
  17424. +#include <linux/fs.h>
  17425. +#include <linux/futex.h>
  17426. +#include <linux/hrtimer.h>
  17427. +
  17428. +#include "rtmutex_common.h"
  17429. +
  17430. +/*
  17431. + * struct mutex functions
  17432. + */
  17433. +void __mutex_do_init(struct mutex *mutex, const char *name,
  17434. + struct lock_class_key *key)
  17435. +{
  17436. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  17437. + /*
  17438. + * Make sure we are not reinitializing a held lock:
  17439. + */
  17440. + debug_check_no_locks_freed((void *)mutex, sizeof(*mutex));
  17441. + lockdep_init_map(&mutex->dep_map, name, key, 0);
  17442. +#endif
  17443. + mutex->lock.save_state = 0;
  17444. +}
  17445. +EXPORT_SYMBOL(__mutex_do_init);
  17446. +
  17447. +void __lockfunc _mutex_lock(struct mutex *lock)
  17448. +{
  17449. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  17450. + rt_mutex_lock(&lock->lock);
  17451. +}
  17452. +EXPORT_SYMBOL(_mutex_lock);
  17453. +
  17454. +int __lockfunc _mutex_lock_interruptible(struct mutex *lock)
  17455. +{
  17456. + int ret;
  17457. +
  17458. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  17459. + ret = rt_mutex_lock_interruptible(&lock->lock);
  17460. + if (ret)
  17461. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  17462. + return ret;
  17463. +}
  17464. +EXPORT_SYMBOL(_mutex_lock_interruptible);
  17465. +
  17466. +int __lockfunc _mutex_lock_killable(struct mutex *lock)
  17467. +{
  17468. + int ret;
  17469. +
  17470. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  17471. + ret = rt_mutex_lock_killable(&lock->lock);
  17472. + if (ret)
  17473. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  17474. + return ret;
  17475. +}
  17476. +EXPORT_SYMBOL(_mutex_lock_killable);
  17477. +
  17478. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  17479. +void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass)
  17480. +{
  17481. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  17482. + rt_mutex_lock(&lock->lock);
  17483. +}
  17484. +EXPORT_SYMBOL(_mutex_lock_nested);
  17485. +
  17486. +void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
  17487. +{
  17488. + mutex_acquire_nest(&lock->dep_map, 0, 0, nest, _RET_IP_);
  17489. + rt_mutex_lock(&lock->lock);
  17490. +}
  17491. +EXPORT_SYMBOL(_mutex_lock_nest_lock);
  17492. +
  17493. +int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass)
  17494. +{
  17495. + int ret;
  17496. +
  17497. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  17498. + ret = rt_mutex_lock_interruptible(&lock->lock);
  17499. + if (ret)
  17500. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  17501. + return ret;
  17502. +}
  17503. +EXPORT_SYMBOL(_mutex_lock_interruptible_nested);
  17504. +
  17505. +int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass)
  17506. +{
  17507. + int ret;
  17508. +
  17509. + mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  17510. + ret = rt_mutex_lock_killable(&lock->lock);
  17511. + if (ret)
  17512. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  17513. + return ret;
  17514. +}
  17515. +EXPORT_SYMBOL(_mutex_lock_killable_nested);
  17516. +#endif
  17517. +
  17518. +int __lockfunc _mutex_trylock(struct mutex *lock)
  17519. +{
  17520. + int ret = rt_mutex_trylock(&lock->lock);
  17521. +
  17522. + if (ret)
  17523. + mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  17524. +
  17525. + return ret;
  17526. +}
  17527. +EXPORT_SYMBOL(_mutex_trylock);
  17528. +
  17529. +void __lockfunc _mutex_unlock(struct mutex *lock)
  17530. +{
  17531. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  17532. + rt_mutex_unlock(&lock->lock);
  17533. +}
  17534. +EXPORT_SYMBOL(_mutex_unlock);
  17535. +
  17536. +/*
  17537. + * rwlock_t functions
  17538. + */
  17539. +int __lockfunc rt_write_trylock(rwlock_t *rwlock)
  17540. +{
  17541. + int ret;
  17542. +
  17543. + migrate_disable();
  17544. + ret = rt_mutex_trylock(&rwlock->lock);
  17545. + if (ret)
  17546. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  17547. + else
  17548. + migrate_enable();
  17549. +
  17550. + return ret;
  17551. +}
  17552. +EXPORT_SYMBOL(rt_write_trylock);
  17553. +
  17554. +int __lockfunc rt_write_trylock_irqsave(rwlock_t *rwlock, unsigned long *flags)
  17555. +{
  17556. + int ret;
  17557. +
  17558. + *flags = 0;
  17559. + ret = rt_write_trylock(rwlock);
  17560. + return ret;
  17561. +}
  17562. +EXPORT_SYMBOL(rt_write_trylock_irqsave);
  17563. +
  17564. +int __lockfunc rt_read_trylock(rwlock_t *rwlock)
  17565. +{
  17566. + struct rt_mutex *lock = &rwlock->lock;
  17567. + int ret = 1;
  17568. +
  17569. + /*
  17570. + * recursive read locks succeed when current owns the lock,
  17571. + * but not when read_depth == 0 which means that the lock is
  17572. + * write locked.
  17573. + */
  17574. + if (rt_mutex_owner(lock) != current) {
  17575. + migrate_disable();
  17576. + ret = rt_mutex_trylock(lock);
  17577. + if (ret)
  17578. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  17579. + else
  17580. + migrate_enable();
  17581. +
  17582. + } else if (!rwlock->read_depth) {
  17583. + ret = 0;
  17584. + }
  17585. +
  17586. + if (ret)
  17587. + rwlock->read_depth++;
  17588. +
  17589. + return ret;
  17590. +}
  17591. +EXPORT_SYMBOL(rt_read_trylock);
  17592. +
  17593. +void __lockfunc rt_write_lock(rwlock_t *rwlock)
  17594. +{
  17595. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  17596. + __rt_spin_lock(&rwlock->lock);
  17597. +}
  17598. +EXPORT_SYMBOL(rt_write_lock);
  17599. +
  17600. +void __lockfunc rt_read_lock(rwlock_t *rwlock)
  17601. +{
  17602. + struct rt_mutex *lock = &rwlock->lock;
  17603. +
  17604. +
  17605. + /*
  17606. + * recursive read locks succeed when current owns the lock
  17607. + */
  17608. + if (rt_mutex_owner(lock) != current) {
  17609. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  17610. + __rt_spin_lock(lock);
  17611. + }
  17612. + rwlock->read_depth++;
  17613. +}
  17614. +
  17615. +EXPORT_SYMBOL(rt_read_lock);
  17616. +
  17617. +void __lockfunc rt_write_unlock(rwlock_t *rwlock)
  17618. +{
  17619. + /* NOTE: we always pass in '1' for nested, for simplicity */
  17620. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  17621. + __rt_spin_unlock(&rwlock->lock);
  17622. + migrate_enable();
  17623. +}
  17624. +EXPORT_SYMBOL(rt_write_unlock);
  17625. +
  17626. +void __lockfunc rt_read_unlock(rwlock_t *rwlock)
  17627. +{
  17628. + /* Release the lock only when read_depth is down to 0 */
  17629. + if (--rwlock->read_depth == 0) {
  17630. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  17631. + __rt_spin_unlock(&rwlock->lock);
  17632. + migrate_enable();
  17633. + }
  17634. +}
  17635. +EXPORT_SYMBOL(rt_read_unlock);
  17636. +
  17637. +unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock)
  17638. +{
  17639. + rt_write_lock(rwlock);
  17640. +
  17641. + return 0;
  17642. +}
  17643. +EXPORT_SYMBOL(rt_write_lock_irqsave);
  17644. +
  17645. +unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock)
  17646. +{
  17647. + rt_read_lock(rwlock);
  17648. +
  17649. + return 0;
  17650. +}
  17651. +EXPORT_SYMBOL(rt_read_lock_irqsave);
  17652. +
  17653. +void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key)
  17654. +{
  17655. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  17656. + /*
  17657. + * Make sure we are not reinitializing a held lock:
  17658. + */
  17659. + debug_check_no_locks_freed((void *)rwlock, sizeof(*rwlock));
  17660. + lockdep_init_map(&rwlock->dep_map, name, key, 0);
  17661. +#endif
  17662. + rwlock->lock.save_state = 1;
  17663. + rwlock->read_depth = 0;
  17664. +}
  17665. +EXPORT_SYMBOL(__rt_rwlock_init);
  17666. +
  17667. +/*
  17668. + * rw_semaphores
  17669. + */
  17670. +
  17671. +void rt_up_write(struct rw_semaphore *rwsem)
  17672. +{
  17673. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  17674. + rt_mutex_unlock(&rwsem->lock);
  17675. +}
  17676. +EXPORT_SYMBOL(rt_up_write);
  17677. +
  17678. +void __rt_up_read(struct rw_semaphore *rwsem)
  17679. +{
  17680. + if (--rwsem->read_depth == 0)
  17681. + rt_mutex_unlock(&rwsem->lock);
  17682. +}
  17683. +
  17684. +void rt_up_read(struct rw_semaphore *rwsem)
  17685. +{
  17686. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  17687. + __rt_up_read(rwsem);
  17688. +}
  17689. +EXPORT_SYMBOL(rt_up_read);
  17690. +
  17691. +/*
  17692. + * downgrade a write lock into a read lock
  17693. + * - just wake up any readers at the front of the queue
  17694. + */
  17695. +void rt_downgrade_write(struct rw_semaphore *rwsem)
  17696. +{
  17697. + BUG_ON(rt_mutex_owner(&rwsem->lock) != current);
  17698. + rwsem->read_depth = 1;
  17699. +}
  17700. +EXPORT_SYMBOL(rt_downgrade_write);
  17701. +
  17702. +int rt_down_write_trylock(struct rw_semaphore *rwsem)
  17703. +{
  17704. + int ret = rt_mutex_trylock(&rwsem->lock);
  17705. +
  17706. + if (ret)
  17707. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  17708. + return ret;
  17709. +}
  17710. +EXPORT_SYMBOL(rt_down_write_trylock);
  17711. +
  17712. +void rt_down_write(struct rw_semaphore *rwsem)
  17713. +{
  17714. + rwsem_acquire(&rwsem->dep_map, 0, 0, _RET_IP_);
  17715. + rt_mutex_lock(&rwsem->lock);
  17716. +}
  17717. +EXPORT_SYMBOL(rt_down_write);
  17718. +
  17719. +void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass)
  17720. +{
  17721. + rwsem_acquire(&rwsem->dep_map, subclass, 0, _RET_IP_);
  17722. + rt_mutex_lock(&rwsem->lock);
  17723. +}
  17724. +EXPORT_SYMBOL(rt_down_write_nested);
  17725. +
  17726. +void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  17727. + struct lockdep_map *nest)
  17728. +{
  17729. + rwsem_acquire_nest(&rwsem->dep_map, 0, 0, nest, _RET_IP_);
  17730. + rt_mutex_lock(&rwsem->lock);
  17731. +}
  17732. +EXPORT_SYMBOL(rt_down_write_nested_lock);
  17733. +
  17734. +int rt__down_read_trylock(struct rw_semaphore *rwsem)
  17735. +{
  17736. + struct rt_mutex *lock = &rwsem->lock;
  17737. + int ret = 1;
  17738. +
  17739. + /*
  17740. + * recursive read locks succeed when current owns the rwsem,
  17741. + * but not when read_depth == 0 which means that the rwsem is
  17742. + * write locked.
  17743. + */
  17744. + if (rt_mutex_owner(lock) != current)
  17745. + ret = rt_mutex_trylock(&rwsem->lock);
  17746. + else if (!rwsem->read_depth)
  17747. + ret = 0;
  17748. +
  17749. + if (ret)
  17750. + rwsem->read_depth++;
  17751. + return ret;
  17752. +
  17753. +}
  17754. +
  17755. +int rt_down_read_trylock(struct rw_semaphore *rwsem)
  17756. +{
  17757. + int ret;
  17758. +
  17759. + ret = rt__down_read_trylock(rwsem);
  17760. + if (ret)
  17761. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  17762. +
  17763. + return ret;
  17764. +}
  17765. +EXPORT_SYMBOL(rt_down_read_trylock);
  17766. +
  17767. +void rt__down_read(struct rw_semaphore *rwsem)
  17768. +{
  17769. + struct rt_mutex *lock = &rwsem->lock;
  17770. +
  17771. + if (rt_mutex_owner(lock) != current)
  17772. + rt_mutex_lock(&rwsem->lock);
  17773. + rwsem->read_depth++;
  17774. +}
  17775. +EXPORT_SYMBOL(rt__down_read);
  17776. +
  17777. +static void __rt_down_read(struct rw_semaphore *rwsem, int subclass)
  17778. +{
  17779. + rwsem_acquire_read(&rwsem->dep_map, subclass, 0, _RET_IP_);
  17780. + rt__down_read(rwsem);
  17781. +}
  17782. +
  17783. +void rt_down_read(struct rw_semaphore *rwsem)
  17784. +{
  17785. + __rt_down_read(rwsem, 0);
  17786. +}
  17787. +EXPORT_SYMBOL(rt_down_read);
  17788. +
  17789. +void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass)
  17790. +{
  17791. + __rt_down_read(rwsem, subclass);
  17792. +}
  17793. +EXPORT_SYMBOL(rt_down_read_nested);
  17794. +
  17795. +void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  17796. + struct lock_class_key *key)
  17797. +{
  17798. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  17799. + /*
  17800. + * Make sure we are not reinitializing a held lock:
  17801. + */
  17802. + debug_check_no_locks_freed((void *)rwsem, sizeof(*rwsem));
  17803. + lockdep_init_map(&rwsem->dep_map, name, key, 0);
  17804. +#endif
  17805. + rwsem->read_depth = 0;
  17806. + rwsem->lock.save_state = 0;
  17807. +}
  17808. +EXPORT_SYMBOL(__rt_rwsem_init);
  17809. +
  17810. +/**
  17811. + * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
  17812. + * @cnt: the atomic which we are to dec
  17813. + * @lock: the mutex to return holding if we dec to 0
  17814. + *
  17815. + * return true and hold lock if we dec to 0, return false otherwise
  17816. + */
  17817. +int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
  17818. +{
  17819. + /* dec if we can't possibly hit 0 */
  17820. + if (atomic_add_unless(cnt, -1, 1))
  17821. + return 0;
  17822. + /* we might hit 0, so take the lock */
  17823. + mutex_lock(lock);
  17824. + if (!atomic_dec_and_test(cnt)) {
  17825. + /* when we actually did the dec, we didn't hit 0 */
  17826. + mutex_unlock(lock);
  17827. + return 0;
  17828. + }
  17829. + /* we hit 0, and we hold the lock */
  17830. + return 1;
  17831. +}
  17832. +EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
  17833. diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c
  17834. index b066724d7a5b..bb42267257ad 100644
  17835. --- a/kernel/locking/rtmutex.c
  17836. +++ b/kernel/locking/rtmutex.c
  17837. @@ -7,6 +7,11 @@
  17838. * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  17839. * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
  17840. * Copyright (C) 2006 Esben Nielsen
  17841. + * Adaptive Spinlocks:
  17842. + * Copyright (C) 2008 Novell, Inc., Gregory Haskins, Sven Dietrich,
  17843. + * and Peter Morreale,
  17844. + * Adaptive Spinlocks simplification:
  17845. + * Copyright (C) 2008 Red Hat, Inc., Steven Rostedt <srostedt@redhat.com>
  17846. *
  17847. * See Documentation/locking/rt-mutex-design.txt for details.
  17848. */
  17849. @@ -16,6 +21,8 @@
  17850. #include <linux/sched/rt.h>
  17851. #include <linux/sched/deadline.h>
  17852. #include <linux/timer.h>
  17853. +#include <linux/ww_mutex.h>
  17854. +#include <linux/blkdev.h>
  17855. #include "rtmutex_common.h"
  17856. @@ -133,6 +140,12 @@ static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
  17857. WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
  17858. }
  17859. +static int rt_mutex_real_waiter(struct rt_mutex_waiter *waiter)
  17860. +{
  17861. + return waiter && waiter != PI_WAKEUP_INPROGRESS &&
  17862. + waiter != PI_REQUEUE_INPROGRESS;
  17863. +}
  17864. +
  17865. /*
  17866. * We can speed up the acquire/release, if there's no debugging state to be
  17867. * set up.
  17868. @@ -163,13 +176,14 @@ static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  17869. * 2) Drop lock->wait_lock
  17870. * 3) Try to unlock the lock with cmpxchg
  17871. */
  17872. -static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  17873. +static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
  17874. + unsigned long flags)
  17875. __releases(lock->wait_lock)
  17876. {
  17877. struct task_struct *owner = rt_mutex_owner(lock);
  17878. clear_rt_mutex_waiters(lock);
  17879. - raw_spin_unlock(&lock->wait_lock);
  17880. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17881. /*
  17882. * If a new waiter comes in between the unlock and the cmpxchg
  17883. * we have two situations:
  17884. @@ -211,11 +225,12 @@ static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  17885. /*
  17886. * Simple slow path only version: lock->owner is protected by lock->wait_lock.
  17887. */
  17888. -static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  17889. +static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
  17890. + unsigned long flags)
  17891. __releases(lock->wait_lock)
  17892. {
  17893. lock->owner = NULL;
  17894. - raw_spin_unlock(&lock->wait_lock);
  17895. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17896. return true;
  17897. }
  17898. #endif
  17899. @@ -412,6 +427,14 @@ static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
  17900. return debug_rt_mutex_detect_deadlock(waiter, chwalk);
  17901. }
  17902. +static void rt_mutex_wake_waiter(struct rt_mutex_waiter *waiter)
  17903. +{
  17904. + if (waiter->savestate)
  17905. + wake_up_lock_sleeper(waiter->task);
  17906. + else
  17907. + wake_up_process(waiter->task);
  17908. +}
  17909. +
  17910. /*
  17911. * Max number of times we'll walk the boosting chain:
  17912. */
  17913. @@ -419,7 +442,8 @@ int max_lock_depth = 1024;
  17914. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  17915. {
  17916. - return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  17917. + return rt_mutex_real_waiter(p->pi_blocked_on) ?
  17918. + p->pi_blocked_on->lock : NULL;
  17919. }
  17920. /*
  17921. @@ -497,7 +521,6 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17922. int ret = 0, depth = 0;
  17923. struct rt_mutex *lock;
  17924. bool detect_deadlock;
  17925. - unsigned long flags;
  17926. bool requeue = true;
  17927. detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
  17928. @@ -540,7 +563,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17929. /*
  17930. * [1] Task cannot go away as we did a get_task() before !
  17931. */
  17932. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  17933. + raw_spin_lock_irq(&task->pi_lock);
  17934. /*
  17935. * [2] Get the waiter on which @task is blocked on.
  17936. @@ -556,7 +579,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17937. * reached or the state of the chain has changed while we
  17938. * dropped the locks.
  17939. */
  17940. - if (!waiter)
  17941. + if (!rt_mutex_real_waiter(waiter))
  17942. goto out_unlock_pi;
  17943. /*
  17944. @@ -624,7 +647,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17945. * operations.
  17946. */
  17947. if (!raw_spin_trylock(&lock->wait_lock)) {
  17948. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17949. + raw_spin_unlock_irq(&task->pi_lock);
  17950. cpu_relax();
  17951. goto retry;
  17952. }
  17953. @@ -655,7 +678,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17954. /*
  17955. * No requeue[7] here. Just release @task [8]
  17956. */
  17957. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17958. + raw_spin_unlock(&task->pi_lock);
  17959. put_task_struct(task);
  17960. /*
  17961. @@ -663,14 +686,14 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17962. * If there is no owner of the lock, end of chain.
  17963. */
  17964. if (!rt_mutex_owner(lock)) {
  17965. - raw_spin_unlock(&lock->wait_lock);
  17966. + raw_spin_unlock_irq(&lock->wait_lock);
  17967. return 0;
  17968. }
  17969. /* [10] Grab the next task, i.e. owner of @lock */
  17970. task = rt_mutex_owner(lock);
  17971. get_task_struct(task);
  17972. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  17973. + raw_spin_lock(&task->pi_lock);
  17974. /*
  17975. * No requeue [11] here. We just do deadlock detection.
  17976. @@ -685,8 +708,8 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17977. top_waiter = rt_mutex_top_waiter(lock);
  17978. /* [13] Drop locks */
  17979. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17980. - raw_spin_unlock(&lock->wait_lock);
  17981. + raw_spin_unlock(&task->pi_lock);
  17982. + raw_spin_unlock_irq(&lock->wait_lock);
  17983. /* If owner is not blocked, end of chain. */
  17984. if (!next_lock)
  17985. @@ -707,7 +730,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17986. rt_mutex_enqueue(lock, waiter);
  17987. /* [8] Release the task */
  17988. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17989. + raw_spin_unlock(&task->pi_lock);
  17990. put_task_struct(task);
  17991. /*
  17992. @@ -718,21 +741,24 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17993. * follow here. This is the end of the chain we are walking.
  17994. */
  17995. if (!rt_mutex_owner(lock)) {
  17996. + struct rt_mutex_waiter *lock_top_waiter;
  17997. +
  17998. /*
  17999. * If the requeue [7] above changed the top waiter,
  18000. * then we need to wake the new top waiter up to try
  18001. * to get the lock.
  18002. */
  18003. - if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
  18004. - wake_up_process(rt_mutex_top_waiter(lock)->task);
  18005. - raw_spin_unlock(&lock->wait_lock);
  18006. + lock_top_waiter = rt_mutex_top_waiter(lock);
  18007. + if (prerequeue_top_waiter != lock_top_waiter)
  18008. + rt_mutex_wake_waiter(lock_top_waiter);
  18009. + raw_spin_unlock_irq(&lock->wait_lock);
  18010. return 0;
  18011. }
  18012. /* [10] Grab the next task, i.e. the owner of @lock */
  18013. task = rt_mutex_owner(lock);
  18014. get_task_struct(task);
  18015. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  18016. + raw_spin_lock(&task->pi_lock);
  18017. /* [11] requeue the pi waiters if necessary */
  18018. if (waiter == rt_mutex_top_waiter(lock)) {
  18019. @@ -786,8 +812,8 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  18020. top_waiter = rt_mutex_top_waiter(lock);
  18021. /* [13] Drop the locks */
  18022. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  18023. - raw_spin_unlock(&lock->wait_lock);
  18024. + raw_spin_unlock(&task->pi_lock);
  18025. + raw_spin_unlock_irq(&lock->wait_lock);
  18026. /*
  18027. * Make the actual exit decisions [12], based on the stored
  18028. @@ -810,28 +836,46 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  18029. goto again;
  18030. out_unlock_pi:
  18031. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  18032. + raw_spin_unlock_irq(&task->pi_lock);
  18033. out_put_task:
  18034. put_task_struct(task);
  18035. return ret;
  18036. }
  18037. +
  18038. +#define STEAL_NORMAL 0
  18039. +#define STEAL_LATERAL 1
  18040. +
  18041. +/*
  18042. + * Note that RT tasks are excluded from lateral-steals to prevent the
  18043. + * introduction of an unbounded latency
  18044. + */
  18045. +static inline int lock_is_stealable(struct task_struct *task,
  18046. + struct task_struct *pendowner, int mode)
  18047. +{
  18048. + if (mode == STEAL_NORMAL || rt_task(task)) {
  18049. + if (task->prio >= pendowner->prio)
  18050. + return 0;
  18051. + } else if (task->prio > pendowner->prio)
  18052. + return 0;
  18053. + return 1;
  18054. +}
  18055. +
  18056. /*
  18057. * Try to take an rt-mutex
  18058. *
  18059. - * Must be called with lock->wait_lock held.
  18060. + * Must be called with lock->wait_lock held and interrupts disabled
  18061. *
  18062. * @lock: The lock to be acquired.
  18063. * @task: The task which wants to acquire the lock
  18064. * @waiter: The waiter that is queued to the lock's wait tree if the
  18065. * callsite called task_blocked_on_lock(), otherwise NULL
  18066. */
  18067. -static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  18068. - struct rt_mutex_waiter *waiter)
  18069. +static int __try_to_take_rt_mutex(struct rt_mutex *lock,
  18070. + struct task_struct *task,
  18071. + struct rt_mutex_waiter *waiter, int mode)
  18072. {
  18073. - unsigned long flags;
  18074. -
  18075. /*
  18076. * Before testing whether we can acquire @lock, we set the
  18077. * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
  18078. @@ -867,8 +911,10 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  18079. * If waiter is not the highest priority waiter of
  18080. * @lock, give up.
  18081. */
  18082. - if (waiter != rt_mutex_top_waiter(lock))
  18083. + if (waiter != rt_mutex_top_waiter(lock)) {
  18084. + /* XXX lock_is_stealable() ? */
  18085. return 0;
  18086. + }
  18087. /*
  18088. * We can acquire the lock. Remove the waiter from the
  18089. @@ -886,14 +932,10 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  18090. * not need to be dequeued.
  18091. */
  18092. if (rt_mutex_has_waiters(lock)) {
  18093. - /*
  18094. - * If @task->prio is greater than or equal to
  18095. - * the top waiter priority (kernel view),
  18096. - * @task lost.
  18097. - */
  18098. - if (task->prio >= rt_mutex_top_waiter(lock)->prio)
  18099. - return 0;
  18100. + struct task_struct *pown = rt_mutex_top_waiter(lock)->task;
  18101. + if (task != pown && !lock_is_stealable(task, pown, mode))
  18102. + return 0;
  18103. /*
  18104. * The current top waiter stays enqueued. We
  18105. * don't have to change anything in the lock
  18106. @@ -916,7 +958,7 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  18107. * case, but conditionals are more expensive than a redundant
  18108. * store.
  18109. */
  18110. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  18111. + raw_spin_lock(&task->pi_lock);
  18112. task->pi_blocked_on = NULL;
  18113. /*
  18114. * Finish the lock acquisition. @task is the new owner. If
  18115. @@ -925,7 +967,7 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  18116. */
  18117. if (rt_mutex_has_waiters(lock))
  18118. rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
  18119. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  18120. + raw_spin_unlock(&task->pi_lock);
  18121. takeit:
  18122. /* We got the lock. */
  18123. @@ -942,12 +984,444 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  18124. return 1;
  18125. }
  18126. +#ifdef CONFIG_PREEMPT_RT_FULL
  18127. +/*
  18128. + * preemptible spin_lock functions:
  18129. + */
  18130. +static inline void rt_spin_lock_fastlock(struct rt_mutex *lock,
  18131. + void (*slowfn)(struct rt_mutex *lock,
  18132. + bool mg_off),
  18133. + bool do_mig_dis)
  18134. +{
  18135. + might_sleep_no_state_check();
  18136. +
  18137. + if (do_mig_dis)
  18138. + migrate_disable();
  18139. +
  18140. + if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
  18141. + rt_mutex_deadlock_account_lock(lock, current);
  18142. + else
  18143. + slowfn(lock, do_mig_dis);
  18144. +}
  18145. +
  18146. +static inline int rt_spin_lock_fastunlock(struct rt_mutex *lock,
  18147. + int (*slowfn)(struct rt_mutex *lock))
  18148. +{
  18149. + if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  18150. + rt_mutex_deadlock_account_unlock(current);
  18151. + return 0;
  18152. + }
  18153. + return slowfn(lock);
  18154. +}
  18155. +#ifdef CONFIG_SMP
  18156. +/*
  18157. + * Note that owner is a speculative pointer and dereferencing relies
  18158. + * on rcu_read_lock() and the check against the lock owner.
  18159. + */
  18160. +static int adaptive_wait(struct rt_mutex *lock,
  18161. + struct task_struct *owner)
  18162. +{
  18163. + int res = 0;
  18164. +
  18165. + rcu_read_lock();
  18166. + for (;;) {
  18167. + if (owner != rt_mutex_owner(lock))
  18168. + break;
  18169. + /*
  18170. + * Ensure that owner->on_cpu is dereferenced _after_
  18171. + * checking the above to be valid.
  18172. + */
  18173. + barrier();
  18174. + if (!owner->on_cpu) {
  18175. + res = 1;
  18176. + break;
  18177. + }
  18178. + cpu_relax();
  18179. + }
  18180. + rcu_read_unlock();
  18181. + return res;
  18182. +}
  18183. +#else
  18184. +static int adaptive_wait(struct rt_mutex *lock,
  18185. + struct task_struct *orig_owner)
  18186. +{
  18187. + return 1;
  18188. +}
  18189. +#endif
  18190. +
  18191. +static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  18192. + struct rt_mutex_waiter *waiter,
  18193. + struct task_struct *task,
  18194. + enum rtmutex_chainwalk chwalk);
  18195. +/*
  18196. + * Slow path lock function spin_lock style: this variant is very
  18197. + * careful not to miss any non-lock wakeups.
  18198. + *
  18199. + * We store the current state under p->pi_lock in p->saved_state and
  18200. + * the try_to_wake_up() code handles this accordingly.
  18201. + */
  18202. +static void noinline __sched rt_spin_lock_slowlock(struct rt_mutex *lock,
  18203. + bool mg_off)
  18204. +{
  18205. + struct task_struct *lock_owner, *self = current;
  18206. + struct rt_mutex_waiter waiter, *top_waiter;
  18207. + unsigned long flags;
  18208. + int ret;
  18209. +
  18210. + rt_mutex_init_waiter(&waiter, true);
  18211. +
  18212. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18213. +
  18214. + if (__try_to_take_rt_mutex(lock, self, NULL, STEAL_LATERAL)) {
  18215. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18216. + return;
  18217. + }
  18218. +
  18219. + BUG_ON(rt_mutex_owner(lock) == self);
  18220. +
  18221. + /*
  18222. + * We save whatever state the task is in and we'll restore it
  18223. + * after acquiring the lock taking real wakeups into account
  18224. + * as well. We are serialized via pi_lock against wakeups. See
  18225. + * try_to_wake_up().
  18226. + */
  18227. + raw_spin_lock(&self->pi_lock);
  18228. + self->saved_state = self->state;
  18229. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  18230. + raw_spin_unlock(&self->pi_lock);
  18231. +
  18232. + ret = task_blocks_on_rt_mutex(lock, &waiter, self, RT_MUTEX_MIN_CHAINWALK);
  18233. + BUG_ON(ret);
  18234. +
  18235. + for (;;) {
  18236. + /* Try to acquire the lock again. */
  18237. + if (__try_to_take_rt_mutex(lock, self, &waiter, STEAL_LATERAL))
  18238. + break;
  18239. +
  18240. + top_waiter = rt_mutex_top_waiter(lock);
  18241. + lock_owner = rt_mutex_owner(lock);
  18242. +
  18243. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18244. +
  18245. + debug_rt_mutex_print_deadlock(&waiter);
  18246. +
  18247. + if (top_waiter != &waiter || adaptive_wait(lock, lock_owner)) {
  18248. + if (mg_off)
  18249. + migrate_enable();
  18250. + schedule();
  18251. + if (mg_off)
  18252. + migrate_disable();
  18253. + }
  18254. +
  18255. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18256. +
  18257. + raw_spin_lock(&self->pi_lock);
  18258. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  18259. + raw_spin_unlock(&self->pi_lock);
  18260. + }
  18261. +
  18262. + /*
  18263. + * Restore the task state to current->saved_state. We set it
  18264. + * to the original state above and the try_to_wake_up() code
  18265. + * has possibly updated it when a real (non-rtmutex) wakeup
  18266. + * happened while we were blocked. Clear saved_state so
  18267. + * try_to_wakeup() does not get confused.
  18268. + */
  18269. + raw_spin_lock(&self->pi_lock);
  18270. + __set_current_state_no_track(self->saved_state);
  18271. + self->saved_state = TASK_RUNNING;
  18272. + raw_spin_unlock(&self->pi_lock);
  18273. +
  18274. + /*
  18275. + * try_to_take_rt_mutex() sets the waiter bit
  18276. + * unconditionally. We might have to fix that up:
  18277. + */
  18278. + fixup_rt_mutex_waiters(lock);
  18279. +
  18280. + BUG_ON(rt_mutex_has_waiters(lock) && &waiter == rt_mutex_top_waiter(lock));
  18281. + BUG_ON(!RB_EMPTY_NODE(&waiter.tree_entry));
  18282. +
  18283. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18284. +
  18285. + debug_rt_mutex_free_waiter(&waiter);
  18286. +}
  18287. +
  18288. +static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  18289. + struct wake_q_head *wake_sleeper_q,
  18290. + struct rt_mutex *lock);
  18291. +/*
  18292. + * Slow path to release a rt_mutex spin_lock style
  18293. + */
  18294. +static int noinline __sched rt_spin_lock_slowunlock(struct rt_mutex *lock)
  18295. +{
  18296. + unsigned long flags;
  18297. + WAKE_Q(wake_q);
  18298. + WAKE_Q(wake_sleeper_q);
  18299. +
  18300. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18301. +
  18302. + debug_rt_mutex_unlock(lock);
  18303. +
  18304. + rt_mutex_deadlock_account_unlock(current);
  18305. +
  18306. + if (!rt_mutex_has_waiters(lock)) {
  18307. + lock->owner = NULL;
  18308. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18309. + return 0;
  18310. + }
  18311. +
  18312. + mark_wakeup_next_waiter(&wake_q, &wake_sleeper_q, lock);
  18313. +
  18314. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18315. + wake_up_q(&wake_q);
  18316. + wake_up_q_sleeper(&wake_sleeper_q);
  18317. +
  18318. + /* Undo pi boosting.when necessary */
  18319. + rt_mutex_adjust_prio(current);
  18320. + return 0;
  18321. +}
  18322. +
  18323. +static int noinline __sched rt_spin_lock_slowunlock_no_deboost(struct rt_mutex *lock)
  18324. +{
  18325. + unsigned long flags;
  18326. + WAKE_Q(wake_q);
  18327. + WAKE_Q(wake_sleeper_q);
  18328. +
  18329. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18330. +
  18331. + debug_rt_mutex_unlock(lock);
  18332. +
  18333. + rt_mutex_deadlock_account_unlock(current);
  18334. +
  18335. + if (!rt_mutex_has_waiters(lock)) {
  18336. + lock->owner = NULL;
  18337. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18338. + return 0;
  18339. + }
  18340. +
  18341. + mark_wakeup_next_waiter(&wake_q, &wake_sleeper_q, lock);
  18342. +
  18343. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18344. + wake_up_q(&wake_q);
  18345. + wake_up_q_sleeper(&wake_sleeper_q);
  18346. + return 1;
  18347. +}
  18348. +
  18349. +void __lockfunc rt_spin_lock__no_mg(spinlock_t *lock)
  18350. +{
  18351. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, false);
  18352. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  18353. +}
  18354. +EXPORT_SYMBOL(rt_spin_lock__no_mg);
  18355. +
  18356. +void __lockfunc rt_spin_lock(spinlock_t *lock)
  18357. +{
  18358. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, true);
  18359. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  18360. +}
  18361. +EXPORT_SYMBOL(rt_spin_lock);
  18362. +
  18363. +void __lockfunc __rt_spin_lock(struct rt_mutex *lock)
  18364. +{
  18365. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock, true);
  18366. +}
  18367. +EXPORT_SYMBOL(__rt_spin_lock);
  18368. +
  18369. +void __lockfunc __rt_spin_lock__no_mg(struct rt_mutex *lock)
  18370. +{
  18371. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock, false);
  18372. +}
  18373. +EXPORT_SYMBOL(__rt_spin_lock__no_mg);
  18374. +
  18375. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  18376. +void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass)
  18377. +{
  18378. + spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  18379. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, true);
  18380. +}
  18381. +EXPORT_SYMBOL(rt_spin_lock_nested);
  18382. +#endif
  18383. +
  18384. +void __lockfunc rt_spin_unlock__no_mg(spinlock_t *lock)
  18385. +{
  18386. + /* NOTE: we always pass in '1' for nested, for simplicity */
  18387. + spin_release(&lock->dep_map, 1, _RET_IP_);
  18388. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  18389. +}
  18390. +EXPORT_SYMBOL(rt_spin_unlock__no_mg);
  18391. +
  18392. +void __lockfunc rt_spin_unlock(spinlock_t *lock)
  18393. +{
  18394. + /* NOTE: we always pass in '1' for nested, for simplicity */
  18395. + spin_release(&lock->dep_map, 1, _RET_IP_);
  18396. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  18397. + migrate_enable();
  18398. +}
  18399. +EXPORT_SYMBOL(rt_spin_unlock);
  18400. +
  18401. +int __lockfunc rt_spin_unlock_no_deboost(spinlock_t *lock)
  18402. +{
  18403. + int ret;
  18404. +
  18405. + /* NOTE: we always pass in '1' for nested, for simplicity */
  18406. + spin_release(&lock->dep_map, 1, _RET_IP_);
  18407. + ret = rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock_no_deboost);
  18408. + migrate_enable();
  18409. + return ret;
  18410. +}
  18411. +
  18412. +void __lockfunc __rt_spin_unlock(struct rt_mutex *lock)
  18413. +{
  18414. + rt_spin_lock_fastunlock(lock, rt_spin_lock_slowunlock);
  18415. +}
  18416. +EXPORT_SYMBOL(__rt_spin_unlock);
  18417. +
  18418. +/*
  18419. + * Wait for the lock to get unlocked: instead of polling for an unlock
  18420. + * (like raw spinlocks do), we lock and unlock, to force the kernel to
  18421. + * schedule if there's contention:
  18422. + */
  18423. +void __lockfunc rt_spin_unlock_wait(spinlock_t *lock)
  18424. +{
  18425. + spin_lock(lock);
  18426. + spin_unlock(lock);
  18427. +}
  18428. +EXPORT_SYMBOL(rt_spin_unlock_wait);
  18429. +
  18430. +int __lockfunc __rt_spin_trylock(struct rt_mutex *lock)
  18431. +{
  18432. + return rt_mutex_trylock(lock);
  18433. +}
  18434. +
  18435. +int __lockfunc rt_spin_trylock__no_mg(spinlock_t *lock)
  18436. +{
  18437. + int ret;
  18438. +
  18439. + ret = rt_mutex_trylock(&lock->lock);
  18440. + if (ret)
  18441. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  18442. + return ret;
  18443. +}
  18444. +EXPORT_SYMBOL(rt_spin_trylock__no_mg);
  18445. +
  18446. +int __lockfunc rt_spin_trylock(spinlock_t *lock)
  18447. +{
  18448. + int ret;
  18449. +
  18450. + migrate_disable();
  18451. + ret = rt_mutex_trylock(&lock->lock);
  18452. + if (ret)
  18453. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  18454. + else
  18455. + migrate_enable();
  18456. + return ret;
  18457. +}
  18458. +EXPORT_SYMBOL(rt_spin_trylock);
  18459. +
  18460. +int __lockfunc rt_spin_trylock_bh(spinlock_t *lock)
  18461. +{
  18462. + int ret;
  18463. +
  18464. + local_bh_disable();
  18465. + ret = rt_mutex_trylock(&lock->lock);
  18466. + if (ret) {
  18467. + migrate_disable();
  18468. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  18469. + } else
  18470. + local_bh_enable();
  18471. + return ret;
  18472. +}
  18473. +EXPORT_SYMBOL(rt_spin_trylock_bh);
  18474. +
  18475. +int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags)
  18476. +{
  18477. + int ret;
  18478. +
  18479. + *flags = 0;
  18480. + ret = rt_mutex_trylock(&lock->lock);
  18481. + if (ret) {
  18482. + migrate_disable();
  18483. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  18484. + }
  18485. + return ret;
  18486. +}
  18487. +EXPORT_SYMBOL(rt_spin_trylock_irqsave);
  18488. +
  18489. +int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock)
  18490. +{
  18491. + /* Subtract 1 from counter unless that drops it to 0 (ie. it was 1) */
  18492. + if (atomic_add_unless(atomic, -1, 1))
  18493. + return 0;
  18494. + rt_spin_lock(lock);
  18495. + if (atomic_dec_and_test(atomic))
  18496. + return 1;
  18497. + rt_spin_unlock(lock);
  18498. + return 0;
  18499. +}
  18500. +EXPORT_SYMBOL(atomic_dec_and_spin_lock);
  18501. +
  18502. + void
  18503. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key)
  18504. +{
  18505. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  18506. + /*
  18507. + * Make sure we are not reinitializing a held lock:
  18508. + */
  18509. + debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  18510. + lockdep_init_map(&lock->dep_map, name, key, 0);
  18511. +#endif
  18512. +}
  18513. +EXPORT_SYMBOL(__rt_spin_lock_init);
  18514. +
  18515. +#endif /* PREEMPT_RT_FULL */
  18516. +
  18517. +#ifdef CONFIG_PREEMPT_RT_FULL
  18518. + static inline int __sched
  18519. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  18520. +{
  18521. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  18522. + struct ww_acquire_ctx *hold_ctx = ACCESS_ONCE(ww->ctx);
  18523. +
  18524. + if (!hold_ctx)
  18525. + return 0;
  18526. +
  18527. + if (unlikely(ctx == hold_ctx))
  18528. + return -EALREADY;
  18529. +
  18530. + if (ctx->stamp - hold_ctx->stamp <= LONG_MAX &&
  18531. + (ctx->stamp != hold_ctx->stamp || ctx > hold_ctx)) {
  18532. +#ifdef CONFIG_DEBUG_MUTEXES
  18533. + DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
  18534. + ctx->contending_lock = ww;
  18535. +#endif
  18536. + return -EDEADLK;
  18537. + }
  18538. +
  18539. + return 0;
  18540. +}
  18541. +#else
  18542. + static inline int __sched
  18543. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  18544. +{
  18545. + BUG();
  18546. + return 0;
  18547. +}
  18548. +
  18549. +#endif
  18550. +
  18551. +static inline int
  18552. +try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  18553. + struct rt_mutex_waiter *waiter)
  18554. +{
  18555. + return __try_to_take_rt_mutex(lock, task, waiter, STEAL_NORMAL);
  18556. +}
  18557. +
  18558. /*
  18559. * Task blocks on lock.
  18560. *
  18561. * Prepare waiter and propagate pi chain
  18562. *
  18563. - * This must be called with lock->wait_lock held.
  18564. + * This must be called with lock->wait_lock held and interrupts disabled
  18565. */
  18566. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  18567. struct rt_mutex_waiter *waiter,
  18568. @@ -958,7 +1432,6 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  18569. struct rt_mutex_waiter *top_waiter = waiter;
  18570. struct rt_mutex *next_lock;
  18571. int chain_walk = 0, res;
  18572. - unsigned long flags;
  18573. /*
  18574. * Early deadlock detection. We really don't want the task to
  18575. @@ -972,7 +1445,24 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  18576. if (owner == task)
  18577. return -EDEADLK;
  18578. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  18579. + raw_spin_lock(&task->pi_lock);
  18580. +
  18581. + /*
  18582. + * In the case of futex requeue PI, this will be a proxy
  18583. + * lock. The task will wake unaware that it is enqueueed on
  18584. + * this lock. Avoid blocking on two locks and corrupting
  18585. + * pi_blocked_on via the PI_WAKEUP_INPROGRESS
  18586. + * flag. futex_wait_requeue_pi() sets this when it wakes up
  18587. + * before requeue (due to a signal or timeout). Do not enqueue
  18588. + * the task if PI_WAKEUP_INPROGRESS is set.
  18589. + */
  18590. + if (task != current && task->pi_blocked_on == PI_WAKEUP_INPROGRESS) {
  18591. + raw_spin_unlock(&task->pi_lock);
  18592. + return -EAGAIN;
  18593. + }
  18594. +
  18595. + BUG_ON(rt_mutex_real_waiter(task->pi_blocked_on));
  18596. +
  18597. __rt_mutex_adjust_prio(task);
  18598. waiter->task = task;
  18599. waiter->lock = lock;
  18600. @@ -985,18 +1475,18 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  18601. task->pi_blocked_on = waiter;
  18602. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  18603. + raw_spin_unlock(&task->pi_lock);
  18604. if (!owner)
  18605. return 0;
  18606. - raw_spin_lock_irqsave(&owner->pi_lock, flags);
  18607. + raw_spin_lock(&owner->pi_lock);
  18608. if (waiter == rt_mutex_top_waiter(lock)) {
  18609. rt_mutex_dequeue_pi(owner, top_waiter);
  18610. rt_mutex_enqueue_pi(owner, waiter);
  18611. __rt_mutex_adjust_prio(owner);
  18612. - if (owner->pi_blocked_on)
  18613. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  18614. chain_walk = 1;
  18615. } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  18616. chain_walk = 1;
  18617. @@ -1005,7 +1495,7 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  18618. /* Store the lock on which owner is blocked or NULL */
  18619. next_lock = task_blocked_on_lock(owner);
  18620. - raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  18621. + raw_spin_unlock(&owner->pi_lock);
  18622. /*
  18623. * Even if full deadlock detection is on, if the owner is not
  18624. * blocked itself, we can avoid finding this out in the chain
  18625. @@ -1021,12 +1511,12 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  18626. */
  18627. get_task_struct(owner);
  18628. - raw_spin_unlock(&lock->wait_lock);
  18629. + raw_spin_unlock_irq(&lock->wait_lock);
  18630. res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
  18631. next_lock, waiter, task);
  18632. - raw_spin_lock(&lock->wait_lock);
  18633. + raw_spin_lock_irq(&lock->wait_lock);
  18634. return res;
  18635. }
  18636. @@ -1035,15 +1525,15 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  18637. * Remove the top waiter from the current tasks pi waiter tree and
  18638. * queue it up.
  18639. *
  18640. - * Called with lock->wait_lock held.
  18641. + * Called with lock->wait_lock held and interrupts disabled.
  18642. */
  18643. static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  18644. + struct wake_q_head *wake_sleeper_q,
  18645. struct rt_mutex *lock)
  18646. {
  18647. struct rt_mutex_waiter *waiter;
  18648. - unsigned long flags;
  18649. - raw_spin_lock_irqsave(&current->pi_lock, flags);
  18650. + raw_spin_lock(&current->pi_lock);
  18651. waiter = rt_mutex_top_waiter(lock);
  18652. @@ -1065,15 +1555,18 @@ static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  18653. */
  18654. lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
  18655. - raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  18656. + raw_spin_unlock(&current->pi_lock);
  18657. - wake_q_add(wake_q, waiter->task);
  18658. + if (waiter->savestate)
  18659. + wake_q_add_sleeper(wake_sleeper_q, waiter->task);
  18660. + else
  18661. + wake_q_add(wake_q, waiter->task);
  18662. }
  18663. /*
  18664. * Remove a waiter from a lock and give up
  18665. *
  18666. - * Must be called with lock->wait_lock held and
  18667. + * Must be called with lock->wait_lock held and interrupts disabled. I must
  18668. * have just failed to try_to_take_rt_mutex().
  18669. */
  18670. static void remove_waiter(struct rt_mutex *lock,
  18671. @@ -1081,13 +1574,12 @@ static void remove_waiter(struct rt_mutex *lock,
  18672. {
  18673. bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  18674. struct task_struct *owner = rt_mutex_owner(lock);
  18675. - struct rt_mutex *next_lock;
  18676. - unsigned long flags;
  18677. + struct rt_mutex *next_lock = NULL;
  18678. - raw_spin_lock_irqsave(&current->pi_lock, flags);
  18679. + raw_spin_lock(&current->pi_lock);
  18680. rt_mutex_dequeue(lock, waiter);
  18681. current->pi_blocked_on = NULL;
  18682. - raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  18683. + raw_spin_unlock(&current->pi_lock);
  18684. /*
  18685. * Only update priority if the waiter was the highest priority
  18686. @@ -1096,7 +1588,7 @@ static void remove_waiter(struct rt_mutex *lock,
  18687. if (!owner || !is_top_waiter)
  18688. return;
  18689. - raw_spin_lock_irqsave(&owner->pi_lock, flags);
  18690. + raw_spin_lock(&owner->pi_lock);
  18691. rt_mutex_dequeue_pi(owner, waiter);
  18692. @@ -1106,9 +1598,10 @@ static void remove_waiter(struct rt_mutex *lock,
  18693. __rt_mutex_adjust_prio(owner);
  18694. /* Store the lock on which owner is blocked or NULL */
  18695. - next_lock = task_blocked_on_lock(owner);
  18696. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  18697. + next_lock = task_blocked_on_lock(owner);
  18698. - raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  18699. + raw_spin_unlock(&owner->pi_lock);
  18700. /*
  18701. * Don't walk the chain, if the owner task is not blocked
  18702. @@ -1120,12 +1613,12 @@ static void remove_waiter(struct rt_mutex *lock,
  18703. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  18704. get_task_struct(owner);
  18705. - raw_spin_unlock(&lock->wait_lock);
  18706. + raw_spin_unlock_irq(&lock->wait_lock);
  18707. rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
  18708. next_lock, NULL, current);
  18709. - raw_spin_lock(&lock->wait_lock);
  18710. + raw_spin_lock_irq(&lock->wait_lock);
  18711. }
  18712. /*
  18713. @@ -1142,17 +1635,17 @@ void rt_mutex_adjust_pi(struct task_struct *task)
  18714. raw_spin_lock_irqsave(&task->pi_lock, flags);
  18715. waiter = task->pi_blocked_on;
  18716. - if (!waiter || (waiter->prio == task->prio &&
  18717. + if (!rt_mutex_real_waiter(waiter) || (waiter->prio == task->prio &&
  18718. !dl_prio(task->prio))) {
  18719. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  18720. return;
  18721. }
  18722. next_lock = waiter->lock;
  18723. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  18724. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  18725. get_task_struct(task);
  18726. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  18727. rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
  18728. next_lock, NULL, task);
  18729. }
  18730. @@ -1161,16 +1654,17 @@ void rt_mutex_adjust_pi(struct task_struct *task)
  18731. * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
  18732. * @lock: the rt_mutex to take
  18733. * @state: the state the task should block in (TASK_INTERRUPTIBLE
  18734. - * or TASK_UNINTERRUPTIBLE)
  18735. + * or TASK_UNINTERRUPTIBLE)
  18736. * @timeout: the pre-initialized and started timer, or NULL for none
  18737. * @waiter: the pre-initialized rt_mutex_waiter
  18738. *
  18739. - * lock->wait_lock must be held by the caller.
  18740. + * Must be called with lock->wait_lock held and interrupts disabled
  18741. */
  18742. static int __sched
  18743. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  18744. struct hrtimer_sleeper *timeout,
  18745. - struct rt_mutex_waiter *waiter)
  18746. + struct rt_mutex_waiter *waiter,
  18747. + struct ww_acquire_ctx *ww_ctx)
  18748. {
  18749. int ret = 0;
  18750. @@ -1179,27 +1673,28 @@ __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  18751. if (try_to_take_rt_mutex(lock, current, waiter))
  18752. break;
  18753. - /*
  18754. - * TASK_INTERRUPTIBLE checks for signals and
  18755. - * timeout. Ignored otherwise.
  18756. - */
  18757. - if (unlikely(state == TASK_INTERRUPTIBLE)) {
  18758. - /* Signal pending? */
  18759. - if (signal_pending(current))
  18760. - ret = -EINTR;
  18761. - if (timeout && !timeout->task)
  18762. - ret = -ETIMEDOUT;
  18763. + if (timeout && !timeout->task) {
  18764. + ret = -ETIMEDOUT;
  18765. + break;
  18766. + }
  18767. + if (signal_pending_state(state, current)) {
  18768. + ret = -EINTR;
  18769. + break;
  18770. + }
  18771. +
  18772. + if (ww_ctx && ww_ctx->acquired > 0) {
  18773. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  18774. if (ret)
  18775. break;
  18776. }
  18777. - raw_spin_unlock(&lock->wait_lock);
  18778. + raw_spin_unlock_irq(&lock->wait_lock);
  18779. debug_rt_mutex_print_deadlock(waiter);
  18780. schedule();
  18781. - raw_spin_lock(&lock->wait_lock);
  18782. + raw_spin_lock_irq(&lock->wait_lock);
  18783. set_current_state(state);
  18784. }
  18785. @@ -1227,26 +1722,112 @@ static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
  18786. }
  18787. }
  18788. +static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww,
  18789. + struct ww_acquire_ctx *ww_ctx)
  18790. +{
  18791. +#ifdef CONFIG_DEBUG_MUTEXES
  18792. + /*
  18793. + * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
  18794. + * but released with a normal mutex_unlock in this call.
  18795. + *
  18796. + * This should never happen, always use ww_mutex_unlock.
  18797. + */
  18798. + DEBUG_LOCKS_WARN_ON(ww->ctx);
  18799. +
  18800. + /*
  18801. + * Not quite done after calling ww_acquire_done() ?
  18802. + */
  18803. + DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
  18804. +
  18805. + if (ww_ctx->contending_lock) {
  18806. + /*
  18807. + * After -EDEADLK you tried to
  18808. + * acquire a different ww_mutex? Bad!
  18809. + */
  18810. + DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
  18811. +
  18812. + /*
  18813. + * You called ww_mutex_lock after receiving -EDEADLK,
  18814. + * but 'forgot' to unlock everything else first?
  18815. + */
  18816. + DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
  18817. + ww_ctx->contending_lock = NULL;
  18818. + }
  18819. +
  18820. + /*
  18821. + * Naughty, using a different class will lead to undefined behavior!
  18822. + */
  18823. + DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
  18824. +#endif
  18825. + ww_ctx->acquired++;
  18826. +}
  18827. +
  18828. +#ifdef CONFIG_PREEMPT_RT_FULL
  18829. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  18830. + struct ww_acquire_ctx *ww_ctx)
  18831. +{
  18832. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  18833. + struct rt_mutex_waiter *waiter, *n;
  18834. +
  18835. + /*
  18836. + * This branch gets optimized out for the common case,
  18837. + * and is only important for ww_mutex_lock.
  18838. + */
  18839. + ww_mutex_lock_acquired(ww, ww_ctx);
  18840. + ww->ctx = ww_ctx;
  18841. +
  18842. + /*
  18843. + * Give any possible sleeping processes the chance to wake up,
  18844. + * so they can recheck if they have to back off.
  18845. + */
  18846. + rbtree_postorder_for_each_entry_safe(waiter, n, &lock->waiters,
  18847. + tree_entry) {
  18848. + /* XXX debug rt mutex waiter wakeup */
  18849. +
  18850. + BUG_ON(waiter->lock != lock);
  18851. + rt_mutex_wake_waiter(waiter);
  18852. + }
  18853. +}
  18854. +
  18855. +#else
  18856. +
  18857. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  18858. + struct ww_acquire_ctx *ww_ctx)
  18859. +{
  18860. + BUG();
  18861. +}
  18862. +#endif
  18863. +
  18864. /*
  18865. * Slow path lock function:
  18866. */
  18867. static int __sched
  18868. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  18869. struct hrtimer_sleeper *timeout,
  18870. - enum rtmutex_chainwalk chwalk)
  18871. + enum rtmutex_chainwalk chwalk,
  18872. + struct ww_acquire_ctx *ww_ctx)
  18873. {
  18874. struct rt_mutex_waiter waiter;
  18875. + unsigned long flags;
  18876. int ret = 0;
  18877. - debug_rt_mutex_init_waiter(&waiter);
  18878. - RB_CLEAR_NODE(&waiter.pi_tree_entry);
  18879. - RB_CLEAR_NODE(&waiter.tree_entry);
  18880. + rt_mutex_init_waiter(&waiter, false);
  18881. - raw_spin_lock(&lock->wait_lock);
  18882. + /*
  18883. + * Technically we could use raw_spin_[un]lock_irq() here, but this can
  18884. + * be called in early boot if the cmpxchg() fast path is disabled
  18885. + * (debug, no architecture support). In this case we will acquire the
  18886. + * rtmutex with lock->wait_lock held. But we cannot unconditionally
  18887. + * enable interrupts in that early boot case. So we need to use the
  18888. + * irqsave/restore variants.
  18889. + */
  18890. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18891. /* Try to acquire the lock again: */
  18892. if (try_to_take_rt_mutex(lock, current, NULL)) {
  18893. - raw_spin_unlock(&lock->wait_lock);
  18894. + if (ww_ctx)
  18895. + ww_mutex_account_lock(lock, ww_ctx);
  18896. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18897. return 0;
  18898. }
  18899. @@ -1260,13 +1841,23 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,
  18900. if (likely(!ret))
  18901. /* sleep on the mutex */
  18902. - ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  18903. + ret = __rt_mutex_slowlock(lock, state, timeout, &waiter,
  18904. + ww_ctx);
  18905. + else if (ww_ctx) {
  18906. + /* ww_mutex received EDEADLK, let it become EALREADY */
  18907. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  18908. + BUG_ON(!ret);
  18909. + }
  18910. if (unlikely(ret)) {
  18911. __set_current_state(TASK_RUNNING);
  18912. if (rt_mutex_has_waiters(lock))
  18913. remove_waiter(lock, &waiter);
  18914. - rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  18915. + /* ww_mutex want to report EDEADLK/EALREADY, let them */
  18916. + if (!ww_ctx)
  18917. + rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  18918. + } else if (ww_ctx) {
  18919. + ww_mutex_account_lock(lock, ww_ctx);
  18920. }
  18921. /*
  18922. @@ -1275,7 +1866,7 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,
  18923. */
  18924. fixup_rt_mutex_waiters(lock);
  18925. - raw_spin_unlock(&lock->wait_lock);
  18926. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18927. /* Remove pending timer: */
  18928. if (unlikely(timeout))
  18929. @@ -1291,6 +1882,7 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,
  18930. */
  18931. static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  18932. {
  18933. + unsigned long flags;
  18934. int ret;
  18935. /*
  18936. @@ -1302,10 +1894,10 @@ static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  18937. return 0;
  18938. /*
  18939. - * The mutex has currently no owner. Lock the wait lock and
  18940. - * try to acquire the lock.
  18941. + * The mutex has currently no owner. Lock the wait lock and try to
  18942. + * acquire the lock. We use irqsave here to support early boot calls.
  18943. */
  18944. - raw_spin_lock(&lock->wait_lock);
  18945. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18946. ret = try_to_take_rt_mutex(lock, current, NULL);
  18947. @@ -1315,7 +1907,7 @@ static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  18948. */
  18949. fixup_rt_mutex_waiters(lock);
  18950. - raw_spin_unlock(&lock->wait_lock);
  18951. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18952. return ret;
  18953. }
  18954. @@ -1325,9 +1917,13 @@ static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  18955. * Return whether the current task needs to undo a potential priority boosting.
  18956. */
  18957. static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  18958. - struct wake_q_head *wake_q)
  18959. + struct wake_q_head *wake_q,
  18960. + struct wake_q_head *wake_sleeper_q)
  18961. {
  18962. - raw_spin_lock(&lock->wait_lock);
  18963. + unsigned long flags;
  18964. +
  18965. + /* irqsave required to support early boot calls */
  18966. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18967. debug_rt_mutex_unlock(lock);
  18968. @@ -1366,10 +1962,10 @@ static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  18969. */
  18970. while (!rt_mutex_has_waiters(lock)) {
  18971. /* Drops lock->wait_lock ! */
  18972. - if (unlock_rt_mutex_safe(lock) == true)
  18973. + if (unlock_rt_mutex_safe(lock, flags) == true)
  18974. return false;
  18975. /* Relock the rtmutex and try again */
  18976. - raw_spin_lock(&lock->wait_lock);
  18977. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18978. }
  18979. /*
  18980. @@ -1378,9 +1974,9 @@ static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  18981. *
  18982. * Queue the next waiter for wakeup once we release the wait_lock.
  18983. */
  18984. - mark_wakeup_next_waiter(wake_q, lock);
  18985. + mark_wakeup_next_waiter(wake_q, wake_sleeper_q, lock);
  18986. - raw_spin_unlock(&lock->wait_lock);
  18987. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18988. /* check PI boosting */
  18989. return true;
  18990. @@ -1394,31 +1990,50 @@ static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  18991. */
  18992. static inline int
  18993. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  18994. + struct ww_acquire_ctx *ww_ctx,
  18995. int (*slowfn)(struct rt_mutex *lock, int state,
  18996. struct hrtimer_sleeper *timeout,
  18997. - enum rtmutex_chainwalk chwalk))
  18998. + enum rtmutex_chainwalk chwalk,
  18999. + struct ww_acquire_ctx *ww_ctx))
  19000. {
  19001. if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  19002. rt_mutex_deadlock_account_lock(lock, current);
  19003. return 0;
  19004. - } else
  19005. - return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
  19006. + }
  19007. +
  19008. + /*
  19009. + * If rt_mutex blocks, the function sched_submit_work will not call
  19010. + * blk_schedule_flush_plug (because tsk_is_pi_blocked would be true).
  19011. + * We must call blk_schedule_flush_plug here, if we don't call it,
  19012. + * a deadlock in device mapper may happen.
  19013. + */
  19014. + if (unlikely(blk_needs_flush_plug(current)))
  19015. + blk_schedule_flush_plug(current);
  19016. +
  19017. + return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK,
  19018. + ww_ctx);
  19019. }
  19020. static inline int
  19021. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  19022. struct hrtimer_sleeper *timeout,
  19023. enum rtmutex_chainwalk chwalk,
  19024. + struct ww_acquire_ctx *ww_ctx,
  19025. int (*slowfn)(struct rt_mutex *lock, int state,
  19026. struct hrtimer_sleeper *timeout,
  19027. - enum rtmutex_chainwalk chwalk))
  19028. + enum rtmutex_chainwalk chwalk,
  19029. + struct ww_acquire_ctx *ww_ctx))
  19030. {
  19031. if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
  19032. likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  19033. rt_mutex_deadlock_account_lock(lock, current);
  19034. return 0;
  19035. - } else
  19036. - return slowfn(lock, state, timeout, chwalk);
  19037. + }
  19038. +
  19039. + if (unlikely(blk_needs_flush_plug(current)))
  19040. + blk_schedule_flush_plug(current);
  19041. +
  19042. + return slowfn(lock, state, timeout, chwalk, ww_ctx);
  19043. }
  19044. static inline int
  19045. @@ -1435,17 +2050,20 @@ rt_mutex_fasttrylock(struct rt_mutex *lock,
  19046. static inline void
  19047. rt_mutex_fastunlock(struct rt_mutex *lock,
  19048. bool (*slowfn)(struct rt_mutex *lock,
  19049. - struct wake_q_head *wqh))
  19050. + struct wake_q_head *wqh,
  19051. + struct wake_q_head *wq_sleeper))
  19052. {
  19053. WAKE_Q(wake_q);
  19054. + WAKE_Q(wake_sleeper_q);
  19055. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  19056. rt_mutex_deadlock_account_unlock(current);
  19057. } else {
  19058. - bool deboost = slowfn(lock, &wake_q);
  19059. + bool deboost = slowfn(lock, &wake_q, &wake_sleeper_q);
  19060. wake_up_q(&wake_q);
  19061. + wake_up_q_sleeper(&wake_sleeper_q);
  19062. /* Undo pi boosting if necessary: */
  19063. if (deboost)
  19064. @@ -1462,7 +2080,7 @@ void __sched rt_mutex_lock(struct rt_mutex *lock)
  19065. {
  19066. might_sleep();
  19067. - rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
  19068. + rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, NULL, rt_mutex_slowlock);
  19069. }
  19070. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  19071. @@ -1479,7 +2097,7 @@ int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
  19072. {
  19073. might_sleep();
  19074. - return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
  19075. + return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, NULL, rt_mutex_slowlock);
  19076. }
  19077. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  19078. @@ -1492,10 +2110,29 @@ int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
  19079. might_sleep();
  19080. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  19081. - RT_MUTEX_FULL_CHAINWALK,
  19082. + RT_MUTEX_FULL_CHAINWALK, NULL,
  19083. rt_mutex_slowlock);
  19084. }
  19085. +/**
  19086. + * rt_mutex_lock_killable - lock a rt_mutex killable
  19087. + *
  19088. + * @lock: the rt_mutex to be locked
  19089. + * @detect_deadlock: deadlock detection on/off
  19090. + *
  19091. + * Returns:
  19092. + * 0 on success
  19093. + * -EINTR when interrupted by a signal
  19094. + * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  19095. + */
  19096. +int __sched rt_mutex_lock_killable(struct rt_mutex *lock)
  19097. +{
  19098. + might_sleep();
  19099. +
  19100. + return rt_mutex_fastlock(lock, TASK_KILLABLE, NULL, rt_mutex_slowlock);
  19101. +}
  19102. +EXPORT_SYMBOL_GPL(rt_mutex_lock_killable);
  19103. +
  19104. /**
  19105. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  19106. * the timeout structure is provided
  19107. @@ -1516,6 +2153,7 @@ rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
  19108. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  19109. RT_MUTEX_MIN_CHAINWALK,
  19110. + NULL,
  19111. rt_mutex_slowlock);
  19112. }
  19113. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  19114. @@ -1533,7 +2171,11 @@ EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  19115. */
  19116. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  19117. {
  19118. +#ifdef CONFIG_PREEMPT_RT_FULL
  19119. + if (WARN_ON_ONCE(in_irq() || in_nmi()))
  19120. +#else
  19121. if (WARN_ON(in_irq() || in_nmi() || in_serving_softirq()))
  19122. +#endif
  19123. return 0;
  19124. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  19125. @@ -1559,13 +2201,14 @@ EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  19126. * required or not.
  19127. */
  19128. bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
  19129. - struct wake_q_head *wqh)
  19130. + struct wake_q_head *wqh,
  19131. + struct wake_q_head *wq_sleeper)
  19132. {
  19133. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  19134. rt_mutex_deadlock_account_unlock(current);
  19135. return false;
  19136. }
  19137. - return rt_mutex_slowunlock(lock, wqh);
  19138. + return rt_mutex_slowunlock(lock, wqh, wq_sleeper);
  19139. }
  19140. /**
  19141. @@ -1598,13 +2241,12 @@ EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  19142. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  19143. {
  19144. lock->owner = NULL;
  19145. - raw_spin_lock_init(&lock->wait_lock);
  19146. lock->waiters = RB_ROOT;
  19147. lock->waiters_leftmost = NULL;
  19148. debug_rt_mutex_init(lock, name);
  19149. }
  19150. -EXPORT_SYMBOL_GPL(__rt_mutex_init);
  19151. +EXPORT_SYMBOL(__rt_mutex_init);
  19152. /**
  19153. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  19154. @@ -1619,7 +2261,7 @@ EXPORT_SYMBOL_GPL(__rt_mutex_init);
  19155. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  19156. struct task_struct *proxy_owner)
  19157. {
  19158. - __rt_mutex_init(lock, NULL);
  19159. + rt_mutex_init(lock);
  19160. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  19161. rt_mutex_set_owner(lock, proxy_owner);
  19162. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  19163. @@ -1660,13 +2302,42 @@ int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  19164. {
  19165. int ret;
  19166. - raw_spin_lock(&lock->wait_lock);
  19167. + raw_spin_lock_irq(&lock->wait_lock);
  19168. if (try_to_take_rt_mutex(lock, task, NULL)) {
  19169. - raw_spin_unlock(&lock->wait_lock);
  19170. + raw_spin_unlock_irq(&lock->wait_lock);
  19171. return 1;
  19172. }
  19173. +#ifdef CONFIG_PREEMPT_RT_FULL
  19174. + /*
  19175. + * In PREEMPT_RT there's an added race.
  19176. + * If the task, that we are about to requeue, times out,
  19177. + * it can set the PI_WAKEUP_INPROGRESS. This tells the requeue
  19178. + * to skip this task. But right after the task sets
  19179. + * its pi_blocked_on to PI_WAKEUP_INPROGRESS it can then
  19180. + * block on the spin_lock(&hb->lock), which in RT is an rtmutex.
  19181. + * This will replace the PI_WAKEUP_INPROGRESS with the actual
  19182. + * lock that it blocks on. We *must not* place this task
  19183. + * on this proxy lock in that case.
  19184. + *
  19185. + * To prevent this race, we first take the task's pi_lock
  19186. + * and check if it has updated its pi_blocked_on. If it has,
  19187. + * we assume that it woke up and we return -EAGAIN.
  19188. + * Otherwise, we set the task's pi_blocked_on to
  19189. + * PI_REQUEUE_INPROGRESS, so that if the task is waking up
  19190. + * it will know that we are in the process of requeuing it.
  19191. + */
  19192. + raw_spin_lock(&task->pi_lock);
  19193. + if (task->pi_blocked_on) {
  19194. + raw_spin_unlock(&task->pi_lock);
  19195. + raw_spin_unlock_irq(&lock->wait_lock);
  19196. + return -EAGAIN;
  19197. + }
  19198. + task->pi_blocked_on = PI_REQUEUE_INPROGRESS;
  19199. + raw_spin_unlock(&task->pi_lock);
  19200. +#endif
  19201. +
  19202. /* We enforce deadlock detection for futexes */
  19203. ret = task_blocks_on_rt_mutex(lock, waiter, task,
  19204. RT_MUTEX_FULL_CHAINWALK);
  19205. @@ -1681,10 +2352,10 @@ int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  19206. ret = 0;
  19207. }
  19208. - if (unlikely(ret))
  19209. + if (ret && rt_mutex_has_waiters(lock))
  19210. remove_waiter(lock, waiter);
  19211. - raw_spin_unlock(&lock->wait_lock);
  19212. + raw_spin_unlock_irq(&lock->wait_lock);
  19213. debug_rt_mutex_print_deadlock(waiter);
  19214. @@ -1732,12 +2403,12 @@ int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  19215. {
  19216. int ret;
  19217. - raw_spin_lock(&lock->wait_lock);
  19218. + raw_spin_lock_irq(&lock->wait_lock);
  19219. set_current_state(TASK_INTERRUPTIBLE);
  19220. /* sleep on the mutex */
  19221. - ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  19222. + ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter, NULL);
  19223. if (unlikely(ret))
  19224. remove_waiter(lock, waiter);
  19225. @@ -1748,7 +2419,93 @@ int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  19226. */
  19227. fixup_rt_mutex_waiters(lock);
  19228. - raw_spin_unlock(&lock->wait_lock);
  19229. + raw_spin_unlock_irq(&lock->wait_lock);
  19230. +
  19231. + return ret;
  19232. +}
  19233. +
  19234. +static inline int
  19235. +ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  19236. +{
  19237. +#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  19238. + unsigned tmp;
  19239. +
  19240. + if (ctx->deadlock_inject_countdown-- == 0) {
  19241. + tmp = ctx->deadlock_inject_interval;
  19242. + if (tmp > UINT_MAX/4)
  19243. + tmp = UINT_MAX;
  19244. + else
  19245. + tmp = tmp*2 + tmp + tmp/2;
  19246. +
  19247. + ctx->deadlock_inject_interval = tmp;
  19248. + ctx->deadlock_inject_countdown = tmp;
  19249. + ctx->contending_lock = lock;
  19250. +
  19251. + ww_mutex_unlock(lock);
  19252. +
  19253. + return -EDEADLK;
  19254. + }
  19255. +#endif
  19256. +
  19257. + return 0;
  19258. +}
  19259. +
  19260. +#ifdef CONFIG_PREEMPT_RT_FULL
  19261. +int __sched
  19262. +__ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  19263. +{
  19264. + int ret;
  19265. +
  19266. + might_sleep();
  19267. +
  19268. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  19269. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_INTERRUPTIBLE, NULL, 0, ww_ctx);
  19270. + if (ret)
  19271. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  19272. + else if (!ret && ww_ctx->acquired > 1)
  19273. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  19274. +
  19275. + return ret;
  19276. +}
  19277. +EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible);
  19278. +
  19279. +int __sched
  19280. +__ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  19281. +{
  19282. + int ret;
  19283. +
  19284. + might_sleep();
  19285. +
  19286. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  19287. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_UNINTERRUPTIBLE, NULL, 0, ww_ctx);
  19288. + if (ret)
  19289. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  19290. + else if (!ret && ww_ctx->acquired > 1)
  19291. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  19292. return ret;
  19293. }
  19294. +EXPORT_SYMBOL_GPL(__ww_mutex_lock);
  19295. +
  19296. +void __sched ww_mutex_unlock(struct ww_mutex *lock)
  19297. +{
  19298. + int nest = !!lock->ctx;
  19299. +
  19300. + /*
  19301. + * The unlocking fastpath is the 0->1 transition from 'locked'
  19302. + * into 'unlocked' state:
  19303. + */
  19304. + if (nest) {
  19305. +#ifdef CONFIG_DEBUG_MUTEXES
  19306. + DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
  19307. +#endif
  19308. + if (lock->ctx->acquired > 0)
  19309. + lock->ctx->acquired--;
  19310. + lock->ctx = NULL;
  19311. + }
  19312. +
  19313. + mutex_release(&lock->base.dep_map, nest, _RET_IP_);
  19314. + rt_mutex_unlock(&lock->base.lock);
  19315. +}
  19316. +EXPORT_SYMBOL(ww_mutex_unlock);
  19317. +#endif
  19318. diff --git a/kernel/locking/rtmutex_common.h b/kernel/locking/rtmutex_common.h
  19319. index e317e1cbb3eb..f457c7574920 100644
  19320. --- a/kernel/locking/rtmutex_common.h
  19321. +++ b/kernel/locking/rtmutex_common.h
  19322. @@ -27,6 +27,7 @@ struct rt_mutex_waiter {
  19323. struct rb_node pi_tree_entry;
  19324. struct task_struct *task;
  19325. struct rt_mutex *lock;
  19326. + bool savestate;
  19327. #ifdef CONFIG_DEBUG_RT_MUTEXES
  19328. unsigned long ip;
  19329. struct pid *deadlock_task_pid;
  19330. @@ -98,6 +99,9 @@ enum rtmutex_chainwalk {
  19331. /*
  19332. * PI-futex support (proxy locking functions, etc.):
  19333. */
  19334. +#define PI_WAKEUP_INPROGRESS ((struct rt_mutex_waiter *) 1)
  19335. +#define PI_REQUEUE_INPROGRESS ((struct rt_mutex_waiter *) 2)
  19336. +
  19337. extern struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock);
  19338. extern void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  19339. struct task_struct *proxy_owner);
  19340. @@ -111,7 +115,8 @@ extern int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  19341. struct rt_mutex_waiter *waiter);
  19342. extern int rt_mutex_timed_futex_lock(struct rt_mutex *l, struct hrtimer_sleeper *to);
  19343. extern bool rt_mutex_futex_unlock(struct rt_mutex *lock,
  19344. - struct wake_q_head *wqh);
  19345. + struct wake_q_head *wqh,
  19346. + struct wake_q_head *wq_sleeper);
  19347. extern void rt_mutex_adjust_prio(struct task_struct *task);
  19348. #ifdef CONFIG_DEBUG_RT_MUTEXES
  19349. @@ -120,4 +125,14 @@ extern void rt_mutex_adjust_prio(struct task_struct *task);
  19350. # include "rtmutex.h"
  19351. #endif
  19352. +static inline void
  19353. +rt_mutex_init_waiter(struct rt_mutex_waiter *waiter, bool savestate)
  19354. +{
  19355. + debug_rt_mutex_init_waiter(waiter);
  19356. + waiter->task = NULL;
  19357. + waiter->savestate = savestate;
  19358. + RB_CLEAR_NODE(&waiter->pi_tree_entry);
  19359. + RB_CLEAR_NODE(&waiter->tree_entry);
  19360. +}
  19361. +
  19362. #endif
  19363. diff --git a/kernel/locking/spinlock.c b/kernel/locking/spinlock.c
  19364. index db3ccb1dd614..909779647bd1 100644
  19365. --- a/kernel/locking/spinlock.c
  19366. +++ b/kernel/locking/spinlock.c
  19367. @@ -124,8 +124,11 @@ void __lockfunc __raw_##op##_lock_bh(locktype##_t *lock) \
  19368. * __[spin|read|write]_lock_bh()
  19369. */
  19370. BUILD_LOCK_OPS(spin, raw_spinlock);
  19371. +
  19372. +#ifndef CONFIG_PREEMPT_RT_FULL
  19373. BUILD_LOCK_OPS(read, rwlock);
  19374. BUILD_LOCK_OPS(write, rwlock);
  19375. +#endif
  19376. #endif
  19377. @@ -209,6 +212,8 @@ void __lockfunc _raw_spin_unlock_bh(raw_spinlock_t *lock)
  19378. EXPORT_SYMBOL(_raw_spin_unlock_bh);
  19379. #endif
  19380. +#ifndef CONFIG_PREEMPT_RT_FULL
  19381. +
  19382. #ifndef CONFIG_INLINE_READ_TRYLOCK
  19383. int __lockfunc _raw_read_trylock(rwlock_t *lock)
  19384. {
  19385. @@ -353,6 +358,8 @@ void __lockfunc _raw_write_unlock_bh(rwlock_t *lock)
  19386. EXPORT_SYMBOL(_raw_write_unlock_bh);
  19387. #endif
  19388. +#endif /* !PREEMPT_RT_FULL */
  19389. +
  19390. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  19391. void __lockfunc _raw_spin_lock_nested(raw_spinlock_t *lock, int subclass)
  19392. diff --git a/kernel/locking/spinlock_debug.c b/kernel/locking/spinlock_debug.c
  19393. index 0374a596cffa..94970338d518 100644
  19394. --- a/kernel/locking/spinlock_debug.c
  19395. +++ b/kernel/locking/spinlock_debug.c
  19396. @@ -31,6 +31,7 @@ void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
  19397. EXPORT_SYMBOL(__raw_spin_lock_init);
  19398. +#ifndef CONFIG_PREEMPT_RT_FULL
  19399. void __rwlock_init(rwlock_t *lock, const char *name,
  19400. struct lock_class_key *key)
  19401. {
  19402. @@ -48,6 +49,7 @@ void __rwlock_init(rwlock_t *lock, const char *name,
  19403. }
  19404. EXPORT_SYMBOL(__rwlock_init);
  19405. +#endif
  19406. static void spin_dump(raw_spinlock_t *lock, const char *msg)
  19407. {
  19408. @@ -159,6 +161,7 @@ void do_raw_spin_unlock(raw_spinlock_t *lock)
  19409. arch_spin_unlock(&lock->raw_lock);
  19410. }
  19411. +#ifndef CONFIG_PREEMPT_RT_FULL
  19412. static void rwlock_bug(rwlock_t *lock, const char *msg)
  19413. {
  19414. if (!debug_locks_off())
  19415. @@ -300,3 +303,5 @@ void do_raw_write_unlock(rwlock_t *lock)
  19416. debug_write_unlock(lock);
  19417. arch_write_unlock(&lock->raw_lock);
  19418. }
  19419. +
  19420. +#endif
  19421. diff --git a/kernel/module.c b/kernel/module.c
  19422. index 0a56098d3738..33c1954b07ed 100644
  19423. --- a/kernel/module.c
  19424. +++ b/kernel/module.c
  19425. @@ -682,16 +682,7 @@ static void percpu_modcopy(struct module *mod,
  19426. memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
  19427. }
  19428. -/**
  19429. - * is_module_percpu_address - test whether address is from module static percpu
  19430. - * @addr: address to test
  19431. - *
  19432. - * Test whether @addr belongs to module static percpu area.
  19433. - *
  19434. - * RETURNS:
  19435. - * %true if @addr is from module static percpu area
  19436. - */
  19437. -bool is_module_percpu_address(unsigned long addr)
  19438. +bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
  19439. {
  19440. struct module *mod;
  19441. unsigned int cpu;
  19442. @@ -705,9 +696,11 @@ bool is_module_percpu_address(unsigned long addr)
  19443. continue;
  19444. for_each_possible_cpu(cpu) {
  19445. void *start = per_cpu_ptr(mod->percpu, cpu);
  19446. + void *va = (void *)addr;
  19447. - if ((void *)addr >= start &&
  19448. - (void *)addr < start + mod->percpu_size) {
  19449. + if (va >= start && va < start + mod->percpu_size) {
  19450. + if (can_addr)
  19451. + *can_addr = (unsigned long) (va - start);
  19452. preempt_enable();
  19453. return true;
  19454. }
  19455. @@ -718,6 +711,20 @@ bool is_module_percpu_address(unsigned long addr)
  19456. return false;
  19457. }
  19458. +/**
  19459. + * is_module_percpu_address - test whether address is from module static percpu
  19460. + * @addr: address to test
  19461. + *
  19462. + * Test whether @addr belongs to module static percpu area.
  19463. + *
  19464. + * RETURNS:
  19465. + * %true if @addr is from module static percpu area
  19466. + */
  19467. +bool is_module_percpu_address(unsigned long addr)
  19468. +{
  19469. + return __is_module_percpu_address(addr, NULL);
  19470. +}
  19471. +
  19472. #else /* ... !CONFIG_SMP */
  19473. static inline void __percpu *mod_percpu(struct module *mod)
  19474. @@ -749,6 +756,11 @@ bool is_module_percpu_address(unsigned long addr)
  19475. return false;
  19476. }
  19477. +bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
  19478. +{
  19479. + return false;
  19480. +}
  19481. +
  19482. #endif /* CONFIG_SMP */
  19483. #define MODINFO_ATTR(field) \
  19484. diff --git a/kernel/panic.c b/kernel/panic.c
  19485. index 1d07cf9af849..e182564e6533 100644
  19486. --- a/kernel/panic.c
  19487. +++ b/kernel/panic.c
  19488. @@ -61,6 +61,37 @@ void __weak panic_smp_self_stop(void)
  19489. cpu_relax();
  19490. }
  19491. +/*
  19492. + * Stop ourselves in NMI context if another CPU has already panicked. Arch code
  19493. + * may override this to prepare for crash dumping, e.g. save regs info.
  19494. + */
  19495. +void __weak nmi_panic_self_stop(struct pt_regs *regs)
  19496. +{
  19497. + panic_smp_self_stop();
  19498. +}
  19499. +
  19500. +atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
  19501. +
  19502. +/*
  19503. + * A variant of panic() called from NMI context. We return if we've already
  19504. + * panicked on this CPU. If another CPU already panicked, loop in
  19505. + * nmi_panic_self_stop() which can provide architecture dependent code such
  19506. + * as saving register state for crash dump.
  19507. + */
  19508. +void nmi_panic(struct pt_regs *regs, const char *msg)
  19509. +{
  19510. + int old_cpu, cpu;
  19511. +
  19512. + cpu = raw_smp_processor_id();
  19513. + old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu);
  19514. +
  19515. + if (old_cpu == PANIC_CPU_INVALID)
  19516. + panic("%s", msg);
  19517. + else if (old_cpu != cpu)
  19518. + nmi_panic_self_stop(regs);
  19519. +}
  19520. +EXPORT_SYMBOL(nmi_panic);
  19521. +
  19522. /**
  19523. * panic - halt the system
  19524. * @fmt: The text string to print
  19525. @@ -71,17 +102,17 @@ void __weak panic_smp_self_stop(void)
  19526. */
  19527. void panic(const char *fmt, ...)
  19528. {
  19529. - static DEFINE_SPINLOCK(panic_lock);
  19530. static char buf[1024];
  19531. va_list args;
  19532. long i, i_next = 0;
  19533. int state = 0;
  19534. + int old_cpu, this_cpu;
  19535. /*
  19536. * Disable local interrupts. This will prevent panic_smp_self_stop
  19537. * from deadlocking the first cpu that invokes the panic, since
  19538. * there is nothing to prevent an interrupt handler (that runs
  19539. - * after the panic_lock is acquired) from invoking panic again.
  19540. + * after setting panic_cpu) from invoking panic() again.
  19541. */
  19542. local_irq_disable();
  19543. @@ -94,8 +125,16 @@ void panic(const char *fmt, ...)
  19544. * multiple parallel invocations of panic, all other CPUs either
  19545. * stop themself or will wait until they are stopped by the 1st CPU
  19546. * with smp_send_stop().
  19547. + *
  19548. + * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
  19549. + * comes here, so go ahead.
  19550. + * `old_cpu == this_cpu' means we came from nmi_panic() which sets
  19551. + * panic_cpu to this CPU. In this case, this is also the 1st CPU.
  19552. */
  19553. - if (!spin_trylock(&panic_lock))
  19554. + this_cpu = raw_smp_processor_id();
  19555. + old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
  19556. +
  19557. + if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
  19558. panic_smp_self_stop();
  19559. console_verbose();
  19560. @@ -400,9 +439,11 @@ static u64 oops_id;
  19561. static int init_oops_id(void)
  19562. {
  19563. +#ifndef CONFIG_PREEMPT_RT_FULL
  19564. if (!oops_id)
  19565. get_random_bytes(&oops_id, sizeof(oops_id));
  19566. else
  19567. +#endif
  19568. oops_id++;
  19569. return 0;
  19570. diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
  19571. index 3124cebaec31..c1b981521dd0 100644
  19572. --- a/kernel/power/hibernate.c
  19573. +++ b/kernel/power/hibernate.c
  19574. @@ -285,6 +285,8 @@ static int create_image(int platform_mode)
  19575. local_irq_disable();
  19576. + system_state = SYSTEM_SUSPEND;
  19577. +
  19578. error = syscore_suspend();
  19579. if (error) {
  19580. printk(KERN_ERR "PM: Some system devices failed to power down, "
  19581. @@ -314,6 +316,7 @@ static int create_image(int platform_mode)
  19582. syscore_resume();
  19583. Enable_irqs:
  19584. + system_state = SYSTEM_RUNNING;
  19585. local_irq_enable();
  19586. Enable_cpus:
  19587. @@ -438,6 +441,7 @@ static int resume_target_kernel(bool platform_mode)
  19588. goto Enable_cpus;
  19589. local_irq_disable();
  19590. + system_state = SYSTEM_SUSPEND;
  19591. error = syscore_suspend();
  19592. if (error)
  19593. @@ -471,6 +475,7 @@ static int resume_target_kernel(bool platform_mode)
  19594. syscore_resume();
  19595. Enable_irqs:
  19596. + system_state = SYSTEM_RUNNING;
  19597. local_irq_enable();
  19598. Enable_cpus:
  19599. @@ -556,6 +561,7 @@ int hibernation_platform_enter(void)
  19600. goto Enable_cpus;
  19601. local_irq_disable();
  19602. + system_state = SYSTEM_SUSPEND;
  19603. syscore_suspend();
  19604. if (pm_wakeup_pending()) {
  19605. error = -EAGAIN;
  19606. @@ -568,6 +574,7 @@ int hibernation_platform_enter(void)
  19607. Power_up:
  19608. syscore_resume();
  19609. + system_state = SYSTEM_RUNNING;
  19610. local_irq_enable();
  19611. Enable_cpus:
  19612. @@ -642,6 +649,10 @@ static void power_down(void)
  19613. cpu_relax();
  19614. }
  19615. +#ifndef CONFIG_SUSPEND
  19616. +bool pm_in_action;
  19617. +#endif
  19618. +
  19619. /**
  19620. * hibernate - Carry out system hibernation, including saving the image.
  19621. */
  19622. @@ -654,6 +665,8 @@ int hibernate(void)
  19623. return -EPERM;
  19624. }
  19625. + pm_in_action = true;
  19626. +
  19627. lock_system_sleep();
  19628. /* The snapshot device should not be opened while we're running */
  19629. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  19630. @@ -719,6 +732,7 @@ int hibernate(void)
  19631. atomic_inc(&snapshot_device_available);
  19632. Unlock:
  19633. unlock_system_sleep();
  19634. + pm_in_action = false;
  19635. return error;
  19636. }
  19637. diff --git a/kernel/power/suspend.c b/kernel/power/suspend.c
  19638. index f9fe133c13e2..393bc342c586 100644
  19639. --- a/kernel/power/suspend.c
  19640. +++ b/kernel/power/suspend.c
  19641. @@ -359,6 +359,8 @@ static int suspend_enter(suspend_state_t state, bool *wakeup)
  19642. arch_suspend_disable_irqs();
  19643. BUG_ON(!irqs_disabled());
  19644. + system_state = SYSTEM_SUSPEND;
  19645. +
  19646. error = syscore_suspend();
  19647. if (!error) {
  19648. *wakeup = pm_wakeup_pending();
  19649. @@ -375,6 +377,8 @@ static int suspend_enter(suspend_state_t state, bool *wakeup)
  19650. syscore_resume();
  19651. }
  19652. + system_state = SYSTEM_RUNNING;
  19653. +
  19654. arch_suspend_enable_irqs();
  19655. BUG_ON(irqs_disabled());
  19656. @@ -518,6 +522,8 @@ static int enter_state(suspend_state_t state)
  19657. return error;
  19658. }
  19659. +bool pm_in_action;
  19660. +
  19661. /**
  19662. * pm_suspend - Externally visible function for suspending the system.
  19663. * @state: System sleep state to enter.
  19664. @@ -532,6 +538,8 @@ int pm_suspend(suspend_state_t state)
  19665. if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
  19666. return -EINVAL;
  19667. + pm_in_action = true;
  19668. +
  19669. error = enter_state(state);
  19670. if (error) {
  19671. suspend_stats.fail++;
  19672. @@ -539,6 +547,7 @@ int pm_suspend(suspend_state_t state)
  19673. } else {
  19674. suspend_stats.success++;
  19675. }
  19676. + pm_in_action = false;
  19677. return error;
  19678. }
  19679. EXPORT_SYMBOL(pm_suspend);
  19680. diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
  19681. index 0b5613554769..99deb2617308 100644
  19682. --- a/kernel/printk/printk.c
  19683. +++ b/kernel/printk/printk.c
  19684. @@ -241,6 +241,65 @@ struct printk_log {
  19685. */
  19686. static DEFINE_RAW_SPINLOCK(logbuf_lock);
  19687. +#ifdef CONFIG_EARLY_PRINTK
  19688. +struct console *early_console;
  19689. +
  19690. +static void early_vprintk(const char *fmt, va_list ap)
  19691. +{
  19692. + if (early_console) {
  19693. + char buf[512];
  19694. + int n = vscnprintf(buf, sizeof(buf), fmt, ap);
  19695. +
  19696. + early_console->write(early_console, buf, n);
  19697. + }
  19698. +}
  19699. +
  19700. +asmlinkage void early_printk(const char *fmt, ...)
  19701. +{
  19702. + va_list ap;
  19703. +
  19704. + va_start(ap, fmt);
  19705. + early_vprintk(fmt, ap);
  19706. + va_end(ap);
  19707. +}
  19708. +
  19709. +/*
  19710. + * This is independent of any log levels - a global
  19711. + * kill switch that turns off all of printk.
  19712. + *
  19713. + * Used by the NMI watchdog if early-printk is enabled.
  19714. + */
  19715. +static bool __read_mostly printk_killswitch;
  19716. +
  19717. +static int __init force_early_printk_setup(char *str)
  19718. +{
  19719. + printk_killswitch = true;
  19720. + return 0;
  19721. +}
  19722. +early_param("force_early_printk", force_early_printk_setup);
  19723. +
  19724. +void printk_kill(void)
  19725. +{
  19726. + printk_killswitch = true;
  19727. +}
  19728. +
  19729. +#ifdef CONFIG_PRINTK
  19730. +static int forced_early_printk(const char *fmt, va_list ap)
  19731. +{
  19732. + if (!printk_killswitch)
  19733. + return 0;
  19734. + early_vprintk(fmt, ap);
  19735. + return 1;
  19736. +}
  19737. +#endif
  19738. +
  19739. +#else
  19740. +static inline int forced_early_printk(const char *fmt, va_list ap)
  19741. +{
  19742. + return 0;
  19743. +}
  19744. +#endif
  19745. +
  19746. #ifdef CONFIG_PRINTK
  19747. DECLARE_WAIT_QUEUE_HEAD(log_wait);
  19748. /* the next printk record to read by syslog(READ) or /proc/kmsg */
  19749. @@ -1203,6 +1262,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  19750. {
  19751. char *text;
  19752. int len = 0;
  19753. + int attempts = 0;
  19754. text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
  19755. if (!text)
  19756. @@ -1214,7 +1274,14 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  19757. u64 seq;
  19758. u32 idx;
  19759. enum log_flags prev;
  19760. -
  19761. + int num_msg;
  19762. +try_again:
  19763. + attempts++;
  19764. + if (attempts > 10) {
  19765. + len = -EBUSY;
  19766. + goto out;
  19767. + }
  19768. + num_msg = 0;
  19769. if (clear_seq < log_first_seq) {
  19770. /* messages are gone, move to first available one */
  19771. clear_seq = log_first_seq;
  19772. @@ -1235,6 +1302,14 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  19773. prev = msg->flags;
  19774. idx = log_next(idx);
  19775. seq++;
  19776. + num_msg++;
  19777. + if (num_msg > 5) {
  19778. + num_msg = 0;
  19779. + raw_spin_unlock_irq(&logbuf_lock);
  19780. + raw_spin_lock_irq(&logbuf_lock);
  19781. + if (clear_seq < log_first_seq)
  19782. + goto try_again;
  19783. + }
  19784. }
  19785. /* move first record forward until length fits into the buffer */
  19786. @@ -1248,6 +1323,14 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  19787. prev = msg->flags;
  19788. idx = log_next(idx);
  19789. seq++;
  19790. + num_msg++;
  19791. + if (num_msg > 5) {
  19792. + num_msg = 0;
  19793. + raw_spin_unlock_irq(&logbuf_lock);
  19794. + raw_spin_lock_irq(&logbuf_lock);
  19795. + if (clear_seq < log_first_seq)
  19796. + goto try_again;
  19797. + }
  19798. }
  19799. /* last message fitting into this dump */
  19800. @@ -1288,6 +1371,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  19801. clear_seq = log_next_seq;
  19802. clear_idx = log_next_idx;
  19803. }
  19804. +out:
  19805. raw_spin_unlock_irq(&logbuf_lock);
  19806. kfree(text);
  19807. @@ -1443,6 +1527,12 @@ static void call_console_drivers(int level,
  19808. if (!console_drivers)
  19809. return;
  19810. + if (IS_ENABLED(CONFIG_PREEMPT_RT_BASE)) {
  19811. + if (in_irq() || in_nmi())
  19812. + return;
  19813. + }
  19814. +
  19815. + migrate_disable();
  19816. for_each_console(con) {
  19817. if (exclusive_console && con != exclusive_console)
  19818. continue;
  19819. @@ -1458,6 +1548,7 @@ static void call_console_drivers(int level,
  19820. else
  19821. con->write(con, text, len);
  19822. }
  19823. + migrate_enable();
  19824. }
  19825. /*
  19826. @@ -1518,6 +1609,15 @@ static inline int can_use_console(unsigned int cpu)
  19827. static int console_trylock_for_printk(void)
  19828. {
  19829. unsigned int cpu = smp_processor_id();
  19830. +#ifdef CONFIG_PREEMPT_RT_FULL
  19831. + int lock = !early_boot_irqs_disabled && (preempt_count() == 0) &&
  19832. + !irqs_disabled();
  19833. +#else
  19834. + int lock = 1;
  19835. +#endif
  19836. +
  19837. + if (!lock)
  19838. + return 0;
  19839. if (!console_trylock())
  19840. return 0;
  19841. @@ -1672,6 +1772,13 @@ asmlinkage int vprintk_emit(int facility, int level,
  19842. /* cpu currently holding logbuf_lock in this function */
  19843. static unsigned int logbuf_cpu = UINT_MAX;
  19844. + /*
  19845. + * Fall back to early_printk if a debugging subsystem has
  19846. + * killed printk output
  19847. + */
  19848. + if (unlikely(forced_early_printk(fmt, args)))
  19849. + return 1;
  19850. +
  19851. if (level == LOGLEVEL_SCHED) {
  19852. level = LOGLEVEL_DEFAULT;
  19853. in_sched = true;
  19854. @@ -1813,8 +1920,7 @@ asmlinkage int vprintk_emit(int facility, int level,
  19855. * console_sem which would prevent anyone from printing to
  19856. * console
  19857. */
  19858. - preempt_disable();
  19859. -
  19860. + migrate_disable();
  19861. /*
  19862. * Try to acquire and then immediately release the console
  19863. * semaphore. The release will print out buffers and wake up
  19864. @@ -1822,7 +1928,7 @@ asmlinkage int vprintk_emit(int facility, int level,
  19865. */
  19866. if (console_trylock_for_printk())
  19867. console_unlock();
  19868. - preempt_enable();
  19869. + migrate_enable();
  19870. lockdep_on();
  19871. }
  19872. @@ -1961,26 +2067,6 @@ DEFINE_PER_CPU(printk_func_t, printk_func);
  19873. #endif /* CONFIG_PRINTK */
  19874. -#ifdef CONFIG_EARLY_PRINTK
  19875. -struct console *early_console;
  19876. -
  19877. -asmlinkage __visible void early_printk(const char *fmt, ...)
  19878. -{
  19879. - va_list ap;
  19880. - char buf[512];
  19881. - int n;
  19882. -
  19883. - if (!early_console)
  19884. - return;
  19885. -
  19886. - va_start(ap, fmt);
  19887. - n = vscnprintf(buf, sizeof(buf), fmt, ap);
  19888. - va_end(ap);
  19889. -
  19890. - early_console->write(early_console, buf, n);
  19891. -}
  19892. -#endif
  19893. -
  19894. static int __add_preferred_console(char *name, int idx, char *options,
  19895. char *brl_options)
  19896. {
  19897. @@ -2202,11 +2288,16 @@ static void console_cont_flush(char *text, size_t size)
  19898. goto out;
  19899. len = cont_print_text(text, size);
  19900. +#ifdef CONFIG_PREEMPT_RT_FULL
  19901. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  19902. + call_console_drivers(cont.level, NULL, 0, text, len);
  19903. +#else
  19904. raw_spin_unlock(&logbuf_lock);
  19905. stop_critical_timings();
  19906. call_console_drivers(cont.level, NULL, 0, text, len);
  19907. start_critical_timings();
  19908. local_irq_restore(flags);
  19909. +#endif
  19910. return;
  19911. out:
  19912. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  19913. @@ -2316,13 +2407,17 @@ void console_unlock(void)
  19914. console_idx = log_next(console_idx);
  19915. console_seq++;
  19916. console_prev = msg->flags;
  19917. +#ifdef CONFIG_PREEMPT_RT_FULL
  19918. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  19919. + call_console_drivers(level, ext_text, ext_len, text, len);
  19920. +#else
  19921. raw_spin_unlock(&logbuf_lock);
  19922. stop_critical_timings(); /* don't trace print latency */
  19923. call_console_drivers(level, ext_text, ext_len, text, len);
  19924. start_critical_timings();
  19925. local_irq_restore(flags);
  19926. -
  19927. +#endif
  19928. if (do_cond_resched)
  19929. cond_resched();
  19930. }
  19931. @@ -2374,6 +2469,11 @@ void console_unblank(void)
  19932. {
  19933. struct console *c;
  19934. + if (IS_ENABLED(CONFIG_PREEMPT_RT_BASE)) {
  19935. + if (in_irq() || in_nmi())
  19936. + return;
  19937. + }
  19938. +
  19939. /*
  19940. * console_unblank can no longer be called in interrupt context unless
  19941. * oops_in_progress is set to 1..
  19942. diff --git a/kernel/ptrace.c b/kernel/ptrace.c
  19943. index 5e2cd1030702..d4467d4eb488 100644
  19944. --- a/kernel/ptrace.c
  19945. +++ b/kernel/ptrace.c
  19946. @@ -142,7 +142,14 @@ static bool ptrace_freeze_traced(struct task_struct *task)
  19947. spin_lock_irq(&task->sighand->siglock);
  19948. if (task_is_traced(task) && !__fatal_signal_pending(task)) {
  19949. - task->state = __TASK_TRACED;
  19950. + unsigned long flags;
  19951. +
  19952. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  19953. + if (task->state & __TASK_TRACED)
  19954. + task->state = __TASK_TRACED;
  19955. + else
  19956. + task->saved_state = __TASK_TRACED;
  19957. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  19958. ret = true;
  19959. }
  19960. spin_unlock_irq(&task->sighand->siglock);
  19961. diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c
  19962. index d89328e260df..5bb3364a6284 100644
  19963. --- a/kernel/rcu/rcutorture.c
  19964. +++ b/kernel/rcu/rcutorture.c
  19965. @@ -390,6 +390,7 @@ static struct rcu_torture_ops rcu_ops = {
  19966. .name = "rcu"
  19967. };
  19968. +#ifndef CONFIG_PREEMPT_RT_FULL
  19969. /*
  19970. * Definitions for rcu_bh torture testing.
  19971. */
  19972. @@ -429,6 +430,12 @@ static struct rcu_torture_ops rcu_bh_ops = {
  19973. .name = "rcu_bh"
  19974. };
  19975. +#else
  19976. +static struct rcu_torture_ops rcu_bh_ops = {
  19977. + .ttype = INVALID_RCU_FLAVOR,
  19978. +};
  19979. +#endif
  19980. +
  19981. /*
  19982. * Don't even think about trying any of these in real life!!!
  19983. * The names includes "busted", and they really means it!
  19984. diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c
  19985. index 8a62cbfe1f2f..f60894c6b35c 100644
  19986. --- a/kernel/rcu/tree.c
  19987. +++ b/kernel/rcu/tree.c
  19988. @@ -56,6 +56,11 @@
  19989. #include <linux/random.h>
  19990. #include <linux/trace_events.h>
  19991. #include <linux/suspend.h>
  19992. +#include <linux/delay.h>
  19993. +#include <linux/gfp.h>
  19994. +#include <linux/oom.h>
  19995. +#include <linux/smpboot.h>
  19996. +#include "../time/tick-internal.h"
  19997. #include "tree.h"
  19998. #include "rcu.h"
  19999. @@ -266,6 +271,19 @@ void rcu_sched_qs(void)
  20000. }
  20001. }
  20002. +#ifdef CONFIG_PREEMPT_RT_FULL
  20003. +static void rcu_preempt_qs(void);
  20004. +
  20005. +void rcu_bh_qs(void)
  20006. +{
  20007. + unsigned long flags;
  20008. +
  20009. + /* Callers to this function, rcu_preempt_qs(), must disable irqs. */
  20010. + local_irq_save(flags);
  20011. + rcu_preempt_qs();
  20012. + local_irq_restore(flags);
  20013. +}
  20014. +#else
  20015. void rcu_bh_qs(void)
  20016. {
  20017. if (__this_cpu_read(rcu_bh_data.cpu_no_qs.s)) {
  20018. @@ -275,6 +293,7 @@ void rcu_bh_qs(void)
  20019. __this_cpu_write(rcu_bh_data.cpu_no_qs.b.norm, false);
  20020. }
  20021. }
  20022. +#endif
  20023. static DEFINE_PER_CPU(int, rcu_sched_qs_mask);
  20024. @@ -435,11 +454,13 @@ EXPORT_SYMBOL_GPL(rcu_batches_started_sched);
  20025. /*
  20026. * Return the number of RCU BH batches started thus far for debug & stats.
  20027. */
  20028. +#ifndef CONFIG_PREEMPT_RT_FULL
  20029. unsigned long rcu_batches_started_bh(void)
  20030. {
  20031. return rcu_bh_state.gpnum;
  20032. }
  20033. EXPORT_SYMBOL_GPL(rcu_batches_started_bh);
  20034. +#endif
  20035. /*
  20036. * Return the number of RCU batches completed thus far for debug & stats.
  20037. @@ -459,6 +480,7 @@ unsigned long rcu_batches_completed_sched(void)
  20038. }
  20039. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  20040. +#ifndef CONFIG_PREEMPT_RT_FULL
  20041. /*
  20042. * Return the number of RCU BH batches completed thus far for debug & stats.
  20043. */
  20044. @@ -486,6 +508,13 @@ void rcu_bh_force_quiescent_state(void)
  20045. }
  20046. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  20047. +#else
  20048. +void rcu_force_quiescent_state(void)
  20049. +{
  20050. +}
  20051. +EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  20052. +#endif
  20053. +
  20054. /*
  20055. * Force a quiescent state for RCU-sched.
  20056. */
  20057. @@ -536,9 +565,11 @@ void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
  20058. case RCU_FLAVOR:
  20059. rsp = rcu_state_p;
  20060. break;
  20061. +#ifndef CONFIG_PREEMPT_RT_FULL
  20062. case RCU_BH_FLAVOR:
  20063. rsp = &rcu_bh_state;
  20064. break;
  20065. +#endif
  20066. case RCU_SCHED_FLAVOR:
  20067. rsp = &rcu_sched_state;
  20068. break;
  20069. @@ -1602,7 +1633,6 @@ static int rcu_future_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  20070. int needmore;
  20071. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  20072. - rcu_nocb_gp_cleanup(rsp, rnp);
  20073. rnp->need_future_gp[c & 0x1] = 0;
  20074. needmore = rnp->need_future_gp[(c + 1) & 0x1];
  20075. trace_rcu_future_gp(rnp, rdp, c,
  20076. @@ -1623,7 +1653,7 @@ static void rcu_gp_kthread_wake(struct rcu_state *rsp)
  20077. !READ_ONCE(rsp->gp_flags) ||
  20078. !rsp->gp_kthread)
  20079. return;
  20080. - wake_up(&rsp->gp_wq);
  20081. + swake_up(&rsp->gp_wq);
  20082. }
  20083. /*
  20084. @@ -2003,6 +2033,7 @@ static void rcu_gp_cleanup(struct rcu_state *rsp)
  20085. int nocb = 0;
  20086. struct rcu_data *rdp;
  20087. struct rcu_node *rnp = rcu_get_root(rsp);
  20088. + struct swait_queue_head *sq;
  20089. WRITE_ONCE(rsp->gp_activity, jiffies);
  20090. raw_spin_lock_irq(&rnp->lock);
  20091. @@ -2041,7 +2072,9 @@ static void rcu_gp_cleanup(struct rcu_state *rsp)
  20092. needgp = __note_gp_changes(rsp, rnp, rdp) || needgp;
  20093. /* smp_mb() provided by prior unlock-lock pair. */
  20094. nocb += rcu_future_gp_cleanup(rsp, rnp);
  20095. + sq = rcu_nocb_gp_get(rnp);
  20096. raw_spin_unlock_irq(&rnp->lock);
  20097. + rcu_nocb_gp_cleanup(sq);
  20098. cond_resched_rcu_qs();
  20099. WRITE_ONCE(rsp->gp_activity, jiffies);
  20100. rcu_gp_slow(rsp, gp_cleanup_delay);
  20101. @@ -2088,7 +2121,7 @@ static int __noreturn rcu_gp_kthread(void *arg)
  20102. READ_ONCE(rsp->gpnum),
  20103. TPS("reqwait"));
  20104. rsp->gp_state = RCU_GP_WAIT_GPS;
  20105. - wait_event_interruptible(rsp->gp_wq,
  20106. + swait_event_interruptible(rsp->gp_wq,
  20107. READ_ONCE(rsp->gp_flags) &
  20108. RCU_GP_FLAG_INIT);
  20109. rsp->gp_state = RCU_GP_DONE_GPS;
  20110. @@ -2118,7 +2151,7 @@ static int __noreturn rcu_gp_kthread(void *arg)
  20111. READ_ONCE(rsp->gpnum),
  20112. TPS("fqswait"));
  20113. rsp->gp_state = RCU_GP_WAIT_FQS;
  20114. - ret = wait_event_interruptible_timeout(rsp->gp_wq,
  20115. + ret = swait_event_interruptible_timeout(rsp->gp_wq,
  20116. rcu_gp_fqs_check_wake(rsp, &gf), j);
  20117. rsp->gp_state = RCU_GP_DOING_FQS;
  20118. /* Locking provides needed memory barriers. */
  20119. @@ -2242,7 +2275,7 @@ static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
  20120. WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
  20121. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  20122. raw_spin_unlock_irqrestore(&rcu_get_root(rsp)->lock, flags);
  20123. - rcu_gp_kthread_wake(rsp);
  20124. + swake_up(&rsp->gp_wq); /* Memory barrier implied by swake_up() path. */
  20125. }
  20126. /*
  20127. @@ -2903,7 +2936,7 @@ static void force_quiescent_state(struct rcu_state *rsp)
  20128. }
  20129. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  20130. raw_spin_unlock_irqrestore(&rnp_old->lock, flags);
  20131. - rcu_gp_kthread_wake(rsp);
  20132. + swake_up(&rsp->gp_wq); /* Memory barrier implied by swake_up() path. */
  20133. }
  20134. /*
  20135. @@ -2946,18 +2979,17 @@ __rcu_process_callbacks(struct rcu_state *rsp)
  20136. /*
  20137. * Do RCU core processing for the current CPU.
  20138. */
  20139. -static void rcu_process_callbacks(struct softirq_action *unused)
  20140. +static void rcu_process_callbacks(void)
  20141. {
  20142. struct rcu_state *rsp;
  20143. if (cpu_is_offline(smp_processor_id()))
  20144. return;
  20145. - trace_rcu_utilization(TPS("Start RCU core"));
  20146. for_each_rcu_flavor(rsp)
  20147. __rcu_process_callbacks(rsp);
  20148. - trace_rcu_utilization(TPS("End RCU core"));
  20149. }
  20150. +static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  20151. /*
  20152. * Schedule RCU callback invocation. If the specified type of RCU
  20153. * does not support RCU priority boosting, just do a direct call,
  20154. @@ -2969,18 +3001,105 @@ static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  20155. {
  20156. if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
  20157. return;
  20158. - if (likely(!rsp->boost)) {
  20159. - rcu_do_batch(rsp, rdp);
  20160. - return;
  20161. - }
  20162. - invoke_rcu_callbacks_kthread();
  20163. + rcu_do_batch(rsp, rdp);
  20164. }
  20165. +static void rcu_wake_cond(struct task_struct *t, int status)
  20166. +{
  20167. + /*
  20168. + * If the thread is yielding, only wake it when this
  20169. + * is invoked from idle
  20170. + */
  20171. + if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)))
  20172. + wake_up_process(t);
  20173. +}
  20174. +
  20175. +/*
  20176. + * Wake up this CPU's rcuc kthread to do RCU core processing.
  20177. + */
  20178. static void invoke_rcu_core(void)
  20179. {
  20180. - if (cpu_online(smp_processor_id()))
  20181. - raise_softirq(RCU_SOFTIRQ);
  20182. + unsigned long flags;
  20183. + struct task_struct *t;
  20184. +
  20185. + if (!cpu_online(smp_processor_id()))
  20186. + return;
  20187. + local_irq_save(flags);
  20188. + __this_cpu_write(rcu_cpu_has_work, 1);
  20189. + t = __this_cpu_read(rcu_cpu_kthread_task);
  20190. + if (t != NULL && current != t)
  20191. + rcu_wake_cond(t, __this_cpu_read(rcu_cpu_kthread_status));
  20192. + local_irq_restore(flags);
  20193. +}
  20194. +
  20195. +static void rcu_cpu_kthread_park(unsigned int cpu)
  20196. +{
  20197. + per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  20198. +}
  20199. +
  20200. +static int rcu_cpu_kthread_should_run(unsigned int cpu)
  20201. +{
  20202. + return __this_cpu_read(rcu_cpu_has_work);
  20203. +}
  20204. +
  20205. +/*
  20206. + * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  20207. + * RCU softirq used in flavors and configurations of RCU that do not
  20208. + * support RCU priority boosting.
  20209. + */
  20210. +static void rcu_cpu_kthread(unsigned int cpu)
  20211. +{
  20212. + unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  20213. + char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  20214. + int spincnt;
  20215. +
  20216. + for (spincnt = 0; spincnt < 10; spincnt++) {
  20217. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  20218. + local_bh_disable();
  20219. + *statusp = RCU_KTHREAD_RUNNING;
  20220. + this_cpu_inc(rcu_cpu_kthread_loops);
  20221. + local_irq_disable();
  20222. + work = *workp;
  20223. + *workp = 0;
  20224. + local_irq_enable();
  20225. + if (work)
  20226. + rcu_process_callbacks();
  20227. + local_bh_enable();
  20228. + if (*workp == 0) {
  20229. + trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  20230. + *statusp = RCU_KTHREAD_WAITING;
  20231. + return;
  20232. + }
  20233. + }
  20234. + *statusp = RCU_KTHREAD_YIELDING;
  20235. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  20236. + schedule_timeout_interruptible(2);
  20237. + trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  20238. + *statusp = RCU_KTHREAD_WAITING;
  20239. +}
  20240. +
  20241. +static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  20242. + .store = &rcu_cpu_kthread_task,
  20243. + .thread_should_run = rcu_cpu_kthread_should_run,
  20244. + .thread_fn = rcu_cpu_kthread,
  20245. + .thread_comm = "rcuc/%u",
  20246. + .setup = rcu_cpu_kthread_setup,
  20247. + .park = rcu_cpu_kthread_park,
  20248. +};
  20249. +
  20250. +/*
  20251. + * Spawn per-CPU RCU core processing kthreads.
  20252. + */
  20253. +static int __init rcu_spawn_core_kthreads(void)
  20254. +{
  20255. + int cpu;
  20256. +
  20257. + for_each_possible_cpu(cpu)
  20258. + per_cpu(rcu_cpu_has_work, cpu) = 0;
  20259. + BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  20260. + return 0;
  20261. }
  20262. +early_initcall(rcu_spawn_core_kthreads);
  20263. /*
  20264. * Handle any core-RCU processing required by a call_rcu() invocation.
  20265. @@ -3126,6 +3245,7 @@ void call_rcu_sched(struct rcu_head *head, rcu_callback_t func)
  20266. }
  20267. EXPORT_SYMBOL_GPL(call_rcu_sched);
  20268. +#ifndef CONFIG_PREEMPT_RT_FULL
  20269. /*
  20270. * Queue an RCU callback for invocation after a quicker grace period.
  20271. */
  20272. @@ -3134,6 +3254,7 @@ void call_rcu_bh(struct rcu_head *head, rcu_callback_t func)
  20273. __call_rcu(head, func, &rcu_bh_state, -1, 0);
  20274. }
  20275. EXPORT_SYMBOL_GPL(call_rcu_bh);
  20276. +#endif
  20277. /*
  20278. * Queue an RCU callback for lazy invocation after a grace period.
  20279. @@ -3225,6 +3346,7 @@ void synchronize_sched(void)
  20280. }
  20281. EXPORT_SYMBOL_GPL(synchronize_sched);
  20282. +#ifndef CONFIG_PREEMPT_RT_FULL
  20283. /**
  20284. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  20285. *
  20286. @@ -3251,6 +3373,7 @@ void synchronize_rcu_bh(void)
  20287. wait_rcu_gp(call_rcu_bh);
  20288. }
  20289. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  20290. +#endif
  20291. /**
  20292. * get_state_synchronize_rcu - Snapshot current RCU state
  20293. @@ -3536,7 +3659,7 @@ static void __rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
  20294. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  20295. if (wake) {
  20296. smp_mb(); /* EGP done before wake_up(). */
  20297. - wake_up(&rsp->expedited_wq);
  20298. + swake_up(&rsp->expedited_wq);
  20299. }
  20300. break;
  20301. }
  20302. @@ -3793,7 +3916,7 @@ static void synchronize_sched_expedited_wait(struct rcu_state *rsp)
  20303. jiffies_start = jiffies;
  20304. for (;;) {
  20305. - ret = wait_event_interruptible_timeout(
  20306. + ret = swait_event_timeout(
  20307. rsp->expedited_wq,
  20308. sync_rcu_preempt_exp_done(rnp_root),
  20309. jiffies_stall);
  20310. @@ -3801,7 +3924,7 @@ static void synchronize_sched_expedited_wait(struct rcu_state *rsp)
  20311. return;
  20312. if (ret < 0) {
  20313. /* Hit a signal, disable CPU stall warnings. */
  20314. - wait_event(rsp->expedited_wq,
  20315. + swait_event(rsp->expedited_wq,
  20316. sync_rcu_preempt_exp_done(rnp_root));
  20317. return;
  20318. }
  20319. @@ -4113,6 +4236,7 @@ static void _rcu_barrier(struct rcu_state *rsp)
  20320. mutex_unlock(&rsp->barrier_mutex);
  20321. }
  20322. +#ifndef CONFIG_PREEMPT_RT_FULL
  20323. /**
  20324. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  20325. */
  20326. @@ -4121,6 +4245,7 @@ void rcu_barrier_bh(void)
  20327. _rcu_barrier(&rcu_bh_state);
  20328. }
  20329. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  20330. +#endif
  20331. /**
  20332. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  20333. @@ -4467,8 +4592,8 @@ static void __init rcu_init_one(struct rcu_state *rsp,
  20334. }
  20335. }
  20336. - init_waitqueue_head(&rsp->gp_wq);
  20337. - init_waitqueue_head(&rsp->expedited_wq);
  20338. + init_swait_queue_head(&rsp->gp_wq);
  20339. + init_swait_queue_head(&rsp->expedited_wq);
  20340. rnp = rsp->level[rcu_num_lvls - 1];
  20341. for_each_possible_cpu(i) {
  20342. while (i > rnp->grphi)
  20343. @@ -4588,12 +4713,13 @@ void __init rcu_init(void)
  20344. rcu_bootup_announce();
  20345. rcu_init_geometry();
  20346. +#ifndef CONFIG_PREEMPT_RT_FULL
  20347. rcu_init_one(&rcu_bh_state, &rcu_bh_data);
  20348. +#endif
  20349. rcu_init_one(&rcu_sched_state, &rcu_sched_data);
  20350. if (dump_tree)
  20351. rcu_dump_rcu_node_tree(&rcu_sched_state);
  20352. __rcu_init_preempt();
  20353. - open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  20354. /*
  20355. * We don't need protection against CPU-hotplug here because
  20356. diff --git a/kernel/rcu/tree.h b/kernel/rcu/tree.h
  20357. index 9fb4e238d4dc..c75834d8de24 100644
  20358. --- a/kernel/rcu/tree.h
  20359. +++ b/kernel/rcu/tree.h
  20360. @@ -27,6 +27,7 @@
  20361. #include <linux/threads.h>
  20362. #include <linux/cpumask.h>
  20363. #include <linux/seqlock.h>
  20364. +#include <linux/swait.h>
  20365. #include <linux/stop_machine.h>
  20366. /*
  20367. @@ -241,7 +242,7 @@ struct rcu_node {
  20368. /* Refused to boost: not sure why, though. */
  20369. /* This can happen due to race conditions. */
  20370. #ifdef CONFIG_RCU_NOCB_CPU
  20371. - wait_queue_head_t nocb_gp_wq[2];
  20372. + struct swait_queue_head nocb_gp_wq[2];
  20373. /* Place for rcu_nocb_kthread() to wait GP. */
  20374. #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
  20375. int need_future_gp[2];
  20376. @@ -393,7 +394,7 @@ struct rcu_data {
  20377. atomic_long_t nocb_q_count_lazy; /* invocation (all stages). */
  20378. struct rcu_head *nocb_follower_head; /* CBs ready to invoke. */
  20379. struct rcu_head **nocb_follower_tail;
  20380. - wait_queue_head_t nocb_wq; /* For nocb kthreads to sleep on. */
  20381. + struct swait_queue_head nocb_wq; /* For nocb kthreads to sleep on. */
  20382. struct task_struct *nocb_kthread;
  20383. int nocb_defer_wakeup; /* Defer wakeup of nocb_kthread. */
  20384. @@ -472,7 +473,7 @@ struct rcu_state {
  20385. unsigned long gpnum; /* Current gp number. */
  20386. unsigned long completed; /* # of last completed gp. */
  20387. struct task_struct *gp_kthread; /* Task for grace periods. */
  20388. - wait_queue_head_t gp_wq; /* Where GP task waits. */
  20389. + struct swait_queue_head gp_wq; /* Where GP task waits. */
  20390. short gp_flags; /* Commands for GP task. */
  20391. short gp_state; /* GP kthread sleep state. */
  20392. @@ -504,7 +505,7 @@ struct rcu_state {
  20393. atomic_long_t expedited_workdone3; /* # done by others #3. */
  20394. atomic_long_t expedited_normal; /* # fallbacks to normal. */
  20395. atomic_t expedited_need_qs; /* # CPUs left to check in. */
  20396. - wait_queue_head_t expedited_wq; /* Wait for check-ins. */
  20397. + struct swait_queue_head expedited_wq; /* Wait for check-ins. */
  20398. int ncpus_snap; /* # CPUs seen last time. */
  20399. unsigned long jiffies_force_qs; /* Time at which to invoke */
  20400. @@ -556,18 +557,18 @@ extern struct list_head rcu_struct_flavors;
  20401. */
  20402. extern struct rcu_state rcu_sched_state;
  20403. +#ifndef CONFIG_PREEMPT_RT_FULL
  20404. extern struct rcu_state rcu_bh_state;
  20405. +#endif
  20406. #ifdef CONFIG_PREEMPT_RCU
  20407. extern struct rcu_state rcu_preempt_state;
  20408. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  20409. -#ifdef CONFIG_RCU_BOOST
  20410. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  20411. DECLARE_PER_CPU(int, rcu_cpu_kthread_cpu);
  20412. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  20413. DECLARE_PER_CPU(char, rcu_cpu_has_work);
  20414. -#endif /* #ifdef CONFIG_RCU_BOOST */
  20415. #ifndef RCU_TREE_NONCORE
  20416. @@ -587,10 +588,9 @@ void call_rcu(struct rcu_head *head, rcu_callback_t func);
  20417. static void __init __rcu_init_preempt(void);
  20418. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags);
  20419. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp);
  20420. -static void invoke_rcu_callbacks_kthread(void);
  20421. static bool rcu_is_callbacks_kthread(void);
  20422. +static void rcu_cpu_kthread_setup(unsigned int cpu);
  20423. #ifdef CONFIG_RCU_BOOST
  20424. -static void rcu_preempt_do_callbacks(void);
  20425. static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  20426. struct rcu_node *rnp);
  20427. #endif /* #ifdef CONFIG_RCU_BOOST */
  20428. @@ -607,7 +607,8 @@ static void zero_cpu_stall_ticks(struct rcu_data *rdp);
  20429. static void increment_cpu_stall_ticks(void);
  20430. static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu);
  20431. static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq);
  20432. -static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp);
  20433. +static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp);
  20434. +static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq);
  20435. static void rcu_init_one_nocb(struct rcu_node *rnp);
  20436. static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
  20437. bool lazy, unsigned long flags);
  20438. diff --git a/kernel/rcu/tree_plugin.h b/kernel/rcu/tree_plugin.h
  20439. index 32cbe72bf545..45e3e3e02a5c 100644
  20440. --- a/kernel/rcu/tree_plugin.h
  20441. +++ b/kernel/rcu/tree_plugin.h
  20442. @@ -24,25 +24,10 @@
  20443. * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  20444. */
  20445. -#include <linux/delay.h>
  20446. -#include <linux/gfp.h>
  20447. -#include <linux/oom.h>
  20448. -#include <linux/smpboot.h>
  20449. -#include "../time/tick-internal.h"
  20450. -
  20451. #ifdef CONFIG_RCU_BOOST
  20452. #include "../locking/rtmutex_common.h"
  20453. -/*
  20454. - * Control variables for per-CPU and per-rcu_node kthreads. These
  20455. - * handle all flavors of RCU.
  20456. - */
  20457. -static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  20458. -DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  20459. -DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  20460. -DEFINE_PER_CPU(char, rcu_cpu_has_work);
  20461. -
  20462. #else /* #ifdef CONFIG_RCU_BOOST */
  20463. /*
  20464. @@ -55,6 +40,14 @@ DEFINE_PER_CPU(char, rcu_cpu_has_work);
  20465. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  20466. +/*
  20467. + * Control variables for per-CPU and per-rcu_node kthreads. These
  20468. + * handle all flavors of RCU.
  20469. + */
  20470. +DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  20471. +DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  20472. +DEFINE_PER_CPU(char, rcu_cpu_has_work);
  20473. +
  20474. #ifdef CONFIG_RCU_NOCB_CPU
  20475. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  20476. static bool have_rcu_nocb_mask; /* Was rcu_nocb_mask allocated? */
  20477. @@ -432,7 +425,7 @@ void rcu_read_unlock_special(struct task_struct *t)
  20478. }
  20479. /* Hardware IRQ handlers cannot block, complain if they get here. */
  20480. - if (in_irq() || in_serving_softirq()) {
  20481. + if (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_OFFSET)) {
  20482. lockdep_rcu_suspicious(__FILE__, __LINE__,
  20483. "rcu_read_unlock() from irq or softirq with blocking in critical section!!!\n");
  20484. pr_alert("->rcu_read_unlock_special: %#x (b: %d, enq: %d nq: %d)\n",
  20485. @@ -645,15 +638,6 @@ static void rcu_preempt_check_callbacks(void)
  20486. t->rcu_read_unlock_special.b.need_qs = true;
  20487. }
  20488. -#ifdef CONFIG_RCU_BOOST
  20489. -
  20490. -static void rcu_preempt_do_callbacks(void)
  20491. -{
  20492. - rcu_do_batch(rcu_state_p, this_cpu_ptr(rcu_data_p));
  20493. -}
  20494. -
  20495. -#endif /* #ifdef CONFIG_RCU_BOOST */
  20496. -
  20497. /*
  20498. * Queue a preemptible-RCU callback for invocation after a grace period.
  20499. */
  20500. @@ -930,6 +914,19 @@ void exit_rcu(void)
  20501. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  20502. +/*
  20503. + * If boosting, set rcuc kthreads to realtime priority.
  20504. + */
  20505. +static void rcu_cpu_kthread_setup(unsigned int cpu)
  20506. +{
  20507. +#ifdef CONFIG_RCU_BOOST
  20508. + struct sched_param sp;
  20509. +
  20510. + sp.sched_priority = kthread_prio;
  20511. + sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  20512. +#endif /* #ifdef CONFIG_RCU_BOOST */
  20513. +}
  20514. +
  20515. #ifdef CONFIG_RCU_BOOST
  20516. #include "../locking/rtmutex_common.h"
  20517. @@ -961,16 +958,6 @@ static void rcu_initiate_boost_trace(struct rcu_node *rnp)
  20518. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  20519. -static void rcu_wake_cond(struct task_struct *t, int status)
  20520. -{
  20521. - /*
  20522. - * If the thread is yielding, only wake it when this
  20523. - * is invoked from idle
  20524. - */
  20525. - if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
  20526. - wake_up_process(t);
  20527. -}
  20528. -
  20529. /*
  20530. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  20531. * or ->boost_tasks, advancing the pointer to the next task in the
  20532. @@ -1114,23 +1101,6 @@ static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  20533. }
  20534. }
  20535. -/*
  20536. - * Wake up the per-CPU kthread to invoke RCU callbacks.
  20537. - */
  20538. -static void invoke_rcu_callbacks_kthread(void)
  20539. -{
  20540. - unsigned long flags;
  20541. -
  20542. - local_irq_save(flags);
  20543. - __this_cpu_write(rcu_cpu_has_work, 1);
  20544. - if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
  20545. - current != __this_cpu_read(rcu_cpu_kthread_task)) {
  20546. - rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
  20547. - __this_cpu_read(rcu_cpu_kthread_status));
  20548. - }
  20549. - local_irq_restore(flags);
  20550. -}
  20551. -
  20552. /*
  20553. * Is the current CPU running the RCU-callbacks kthread?
  20554. * Caller must have preemption disabled.
  20555. @@ -1186,67 +1156,6 @@ static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  20556. return 0;
  20557. }
  20558. -static void rcu_kthread_do_work(void)
  20559. -{
  20560. - rcu_do_batch(&rcu_sched_state, this_cpu_ptr(&rcu_sched_data));
  20561. - rcu_do_batch(&rcu_bh_state, this_cpu_ptr(&rcu_bh_data));
  20562. - rcu_preempt_do_callbacks();
  20563. -}
  20564. -
  20565. -static void rcu_cpu_kthread_setup(unsigned int cpu)
  20566. -{
  20567. - struct sched_param sp;
  20568. -
  20569. - sp.sched_priority = kthread_prio;
  20570. - sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  20571. -}
  20572. -
  20573. -static void rcu_cpu_kthread_park(unsigned int cpu)
  20574. -{
  20575. - per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  20576. -}
  20577. -
  20578. -static int rcu_cpu_kthread_should_run(unsigned int cpu)
  20579. -{
  20580. - return __this_cpu_read(rcu_cpu_has_work);
  20581. -}
  20582. -
  20583. -/*
  20584. - * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  20585. - * RCU softirq used in flavors and configurations of RCU that do not
  20586. - * support RCU priority boosting.
  20587. - */
  20588. -static void rcu_cpu_kthread(unsigned int cpu)
  20589. -{
  20590. - unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  20591. - char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  20592. - int spincnt;
  20593. -
  20594. - for (spincnt = 0; spincnt < 10; spincnt++) {
  20595. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  20596. - local_bh_disable();
  20597. - *statusp = RCU_KTHREAD_RUNNING;
  20598. - this_cpu_inc(rcu_cpu_kthread_loops);
  20599. - local_irq_disable();
  20600. - work = *workp;
  20601. - *workp = 0;
  20602. - local_irq_enable();
  20603. - if (work)
  20604. - rcu_kthread_do_work();
  20605. - local_bh_enable();
  20606. - if (*workp == 0) {
  20607. - trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  20608. - *statusp = RCU_KTHREAD_WAITING;
  20609. - return;
  20610. - }
  20611. - }
  20612. - *statusp = RCU_KTHREAD_YIELDING;
  20613. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  20614. - schedule_timeout_interruptible(2);
  20615. - trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  20616. - *statusp = RCU_KTHREAD_WAITING;
  20617. -}
  20618. -
  20619. /*
  20620. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  20621. * served by the rcu_node in question. The CPU hotplug lock is still
  20622. @@ -1276,26 +1185,12 @@ static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  20623. free_cpumask_var(cm);
  20624. }
  20625. -static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  20626. - .store = &rcu_cpu_kthread_task,
  20627. - .thread_should_run = rcu_cpu_kthread_should_run,
  20628. - .thread_fn = rcu_cpu_kthread,
  20629. - .thread_comm = "rcuc/%u",
  20630. - .setup = rcu_cpu_kthread_setup,
  20631. - .park = rcu_cpu_kthread_park,
  20632. -};
  20633. -
  20634. /*
  20635. * Spawn boost kthreads -- called as soon as the scheduler is running.
  20636. */
  20637. static void __init rcu_spawn_boost_kthreads(void)
  20638. {
  20639. struct rcu_node *rnp;
  20640. - int cpu;
  20641. -
  20642. - for_each_possible_cpu(cpu)
  20643. - per_cpu(rcu_cpu_has_work, cpu) = 0;
  20644. - BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  20645. rcu_for_each_leaf_node(rcu_state_p, rnp)
  20646. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  20647. }
  20648. @@ -1318,11 +1213,6 @@ static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  20649. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  20650. }
  20651. -static void invoke_rcu_callbacks_kthread(void)
  20652. -{
  20653. - WARN_ON_ONCE(1);
  20654. -}
  20655. -
  20656. static bool rcu_is_callbacks_kthread(void)
  20657. {
  20658. return false;
  20659. @@ -1346,7 +1236,7 @@ static void rcu_prepare_kthreads(int cpu)
  20660. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  20661. -#if !defined(CONFIG_RCU_FAST_NO_HZ)
  20662. +#if !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL)
  20663. /*
  20664. * Check to see if any future RCU-related work will need to be done
  20665. @@ -1363,7 +1253,9 @@ int rcu_needs_cpu(u64 basemono, u64 *nextevt)
  20666. return IS_ENABLED(CONFIG_RCU_NOCB_CPU_ALL)
  20667. ? 0 : rcu_cpu_has_callbacks(NULL);
  20668. }
  20669. +#endif /* !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL) */
  20670. +#if !defined(CONFIG_RCU_FAST_NO_HZ)
  20671. /*
  20672. * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
  20673. * after it.
  20674. @@ -1459,6 +1351,8 @@ static bool __maybe_unused rcu_try_advance_all_cbs(void)
  20675. return cbs_ready;
  20676. }
  20677. +#ifndef CONFIG_PREEMPT_RT_FULL
  20678. +
  20679. /*
  20680. * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
  20681. * to invoke. If the CPU has callbacks, try to advance them. Tell the
  20682. @@ -1504,6 +1398,7 @@ int rcu_needs_cpu(u64 basemono, u64 *nextevt)
  20683. *nextevt = basemono + dj * TICK_NSEC;
  20684. return 0;
  20685. }
  20686. +#endif /* #ifndef CONFIG_PREEMPT_RT_FULL */
  20687. /*
  20688. * Prepare a CPU for idle from an RCU perspective. The first major task
  20689. @@ -1822,9 +1717,9 @@ early_param("rcu_nocb_poll", parse_rcu_nocb_poll);
  20690. * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
  20691. * grace period.
  20692. */
  20693. -static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  20694. +static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  20695. {
  20696. - wake_up_all(&rnp->nocb_gp_wq[rnp->completed & 0x1]);
  20697. + swake_up_all(sq);
  20698. }
  20699. /*
  20700. @@ -1840,10 +1735,15 @@ static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
  20701. rnp->need_future_gp[(rnp->completed + 1) & 0x1] += nrq;
  20702. }
  20703. +static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  20704. +{
  20705. + return &rnp->nocb_gp_wq[rnp->completed & 0x1];
  20706. +}
  20707. +
  20708. static void rcu_init_one_nocb(struct rcu_node *rnp)
  20709. {
  20710. - init_waitqueue_head(&rnp->nocb_gp_wq[0]);
  20711. - init_waitqueue_head(&rnp->nocb_gp_wq[1]);
  20712. + init_swait_queue_head(&rnp->nocb_gp_wq[0]);
  20713. + init_swait_queue_head(&rnp->nocb_gp_wq[1]);
  20714. }
  20715. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  20716. @@ -1868,7 +1768,7 @@ static void wake_nocb_leader(struct rcu_data *rdp, bool force)
  20717. if (READ_ONCE(rdp_leader->nocb_leader_sleep) || force) {
  20718. /* Prior smp_mb__after_atomic() orders against prior enqueue. */
  20719. WRITE_ONCE(rdp_leader->nocb_leader_sleep, false);
  20720. - wake_up(&rdp_leader->nocb_wq);
  20721. + swake_up(&rdp_leader->nocb_wq);
  20722. }
  20723. }
  20724. @@ -2081,7 +1981,7 @@ static void rcu_nocb_wait_gp(struct rcu_data *rdp)
  20725. */
  20726. trace_rcu_future_gp(rnp, rdp, c, TPS("StartWait"));
  20727. for (;;) {
  20728. - wait_event_interruptible(
  20729. + swait_event_interruptible(
  20730. rnp->nocb_gp_wq[c & 0x1],
  20731. (d = ULONG_CMP_GE(READ_ONCE(rnp->completed), c)));
  20732. if (likely(d))
  20733. @@ -2109,7 +2009,7 @@ static void nocb_leader_wait(struct rcu_data *my_rdp)
  20734. /* Wait for callbacks to appear. */
  20735. if (!rcu_nocb_poll) {
  20736. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, "Sleep");
  20737. - wait_event_interruptible(my_rdp->nocb_wq,
  20738. + swait_event_interruptible(my_rdp->nocb_wq,
  20739. !READ_ONCE(my_rdp->nocb_leader_sleep));
  20740. /* Memory barrier handled by smp_mb() calls below and repoll. */
  20741. } else if (firsttime) {
  20742. @@ -2184,7 +2084,7 @@ static void nocb_leader_wait(struct rcu_data *my_rdp)
  20743. * List was empty, wake up the follower.
  20744. * Memory barriers supplied by atomic_long_add().
  20745. */
  20746. - wake_up(&rdp->nocb_wq);
  20747. + swake_up(&rdp->nocb_wq);
  20748. }
  20749. }
  20750. @@ -2205,7 +2105,7 @@ static void nocb_follower_wait(struct rcu_data *rdp)
  20751. if (!rcu_nocb_poll) {
  20752. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  20753. "FollowerSleep");
  20754. - wait_event_interruptible(rdp->nocb_wq,
  20755. + swait_event_interruptible(rdp->nocb_wq,
  20756. READ_ONCE(rdp->nocb_follower_head));
  20757. } else if (firsttime) {
  20758. /* Don't drown trace log with "Poll"! */
  20759. @@ -2365,7 +2265,7 @@ void __init rcu_init_nohz(void)
  20760. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  20761. {
  20762. rdp->nocb_tail = &rdp->nocb_head;
  20763. - init_waitqueue_head(&rdp->nocb_wq);
  20764. + init_swait_queue_head(&rdp->nocb_wq);
  20765. rdp->nocb_follower_tail = &rdp->nocb_follower_head;
  20766. }
  20767. @@ -2515,7 +2415,7 @@ static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu)
  20768. return false;
  20769. }
  20770. -static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  20771. +static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  20772. {
  20773. }
  20774. @@ -2523,6 +2423,11 @@ static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
  20775. {
  20776. }
  20777. +static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  20778. +{
  20779. + return NULL;
  20780. +}
  20781. +
  20782. static void rcu_init_one_nocb(struct rcu_node *rnp)
  20783. {
  20784. }
  20785. diff --git a/kernel/rcu/update.c b/kernel/rcu/update.c
  20786. index 5f748c5a40f0..9a3904603ff6 100644
  20787. --- a/kernel/rcu/update.c
  20788. +++ b/kernel/rcu/update.c
  20789. @@ -276,6 +276,7 @@ int rcu_read_lock_held(void)
  20790. }
  20791. EXPORT_SYMBOL_GPL(rcu_read_lock_held);
  20792. +#ifndef CONFIG_PREEMPT_RT_FULL
  20793. /**
  20794. * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
  20795. *
  20796. @@ -302,6 +303,7 @@ int rcu_read_lock_bh_held(void)
  20797. return in_softirq() || irqs_disabled();
  20798. }
  20799. EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
  20800. +#endif
  20801. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  20802. diff --git a/kernel/relay.c b/kernel/relay.c
  20803. index 0b4570cfacae..60684be39f22 100644
  20804. --- a/kernel/relay.c
  20805. +++ b/kernel/relay.c
  20806. @@ -336,6 +336,10 @@ static void wakeup_readers(unsigned long data)
  20807. {
  20808. struct rchan_buf *buf = (struct rchan_buf *)data;
  20809. wake_up_interruptible(&buf->read_wait);
  20810. + /*
  20811. + * Stupid polling for now:
  20812. + */
  20813. + mod_timer(&buf->timer, jiffies + 1);
  20814. }
  20815. /**
  20816. @@ -353,6 +357,7 @@ static void __relay_reset(struct rchan_buf *buf, unsigned int init)
  20817. init_waitqueue_head(&buf->read_wait);
  20818. kref_init(&buf->kref);
  20819. setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
  20820. + mod_timer(&buf->timer, jiffies + 1);
  20821. } else
  20822. del_timer_sync(&buf->timer);
  20823. @@ -736,15 +741,6 @@ size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
  20824. else
  20825. buf->early_bytes += buf->chan->subbuf_size -
  20826. buf->padding[old_subbuf];
  20827. - smp_mb();
  20828. - if (waitqueue_active(&buf->read_wait))
  20829. - /*
  20830. - * Calling wake_up_interruptible() from here
  20831. - * will deadlock if we happen to be logging
  20832. - * from the scheduler (trying to re-grab
  20833. - * rq->lock), so defer it.
  20834. - */
  20835. - mod_timer(&buf->timer, jiffies + 1);
  20836. }
  20837. old = buf->data;
  20838. diff --git a/kernel/sched/Makefile b/kernel/sched/Makefile
  20839. index 67687973ce80..01b9994b367a 100644
  20840. --- a/kernel/sched/Makefile
  20841. +++ b/kernel/sched/Makefile
  20842. @@ -13,7 +13,7 @@ endif
  20843. obj-y += core.o loadavg.o clock.o cputime.o
  20844. obj-y += idle_task.o fair.o rt.o deadline.o stop_task.o
  20845. -obj-y += wait.o completion.o idle.o
  20846. +obj-y += wait.o swait.o swork.o completion.o idle.o
  20847. obj-$(CONFIG_SMP) += cpupri.o cpudeadline.o
  20848. obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o
  20849. obj-$(CONFIG_SCHEDSTATS) += stats.o
  20850. diff --git a/kernel/sched/completion.c b/kernel/sched/completion.c
  20851. index 8d0f35debf35..b62cf6400fe0 100644
  20852. --- a/kernel/sched/completion.c
  20853. +++ b/kernel/sched/completion.c
  20854. @@ -30,10 +30,10 @@ void complete(struct completion *x)
  20855. {
  20856. unsigned long flags;
  20857. - spin_lock_irqsave(&x->wait.lock, flags);
  20858. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  20859. x->done++;
  20860. - __wake_up_locked(&x->wait, TASK_NORMAL, 1);
  20861. - spin_unlock_irqrestore(&x->wait.lock, flags);
  20862. + swake_up_locked(&x->wait);
  20863. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  20864. }
  20865. EXPORT_SYMBOL(complete);
  20866. @@ -50,10 +50,10 @@ void complete_all(struct completion *x)
  20867. {
  20868. unsigned long flags;
  20869. - spin_lock_irqsave(&x->wait.lock, flags);
  20870. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  20871. x->done += UINT_MAX/2;
  20872. - __wake_up_locked(&x->wait, TASK_NORMAL, 0);
  20873. - spin_unlock_irqrestore(&x->wait.lock, flags);
  20874. + swake_up_all_locked(&x->wait);
  20875. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  20876. }
  20877. EXPORT_SYMBOL(complete_all);
  20878. @@ -62,20 +62,20 @@ do_wait_for_common(struct completion *x,
  20879. long (*action)(long), long timeout, int state)
  20880. {
  20881. if (!x->done) {
  20882. - DECLARE_WAITQUEUE(wait, current);
  20883. + DECLARE_SWAITQUEUE(wait);
  20884. - __add_wait_queue_tail_exclusive(&x->wait, &wait);
  20885. + __prepare_to_swait(&x->wait, &wait);
  20886. do {
  20887. if (signal_pending_state(state, current)) {
  20888. timeout = -ERESTARTSYS;
  20889. break;
  20890. }
  20891. __set_current_state(state);
  20892. - spin_unlock_irq(&x->wait.lock);
  20893. + raw_spin_unlock_irq(&x->wait.lock);
  20894. timeout = action(timeout);
  20895. - spin_lock_irq(&x->wait.lock);
  20896. + raw_spin_lock_irq(&x->wait.lock);
  20897. } while (!x->done && timeout);
  20898. - __remove_wait_queue(&x->wait, &wait);
  20899. + __finish_swait(&x->wait, &wait);
  20900. if (!x->done)
  20901. return timeout;
  20902. }
  20903. @@ -89,9 +89,9 @@ __wait_for_common(struct completion *x,
  20904. {
  20905. might_sleep();
  20906. - spin_lock_irq(&x->wait.lock);
  20907. + raw_spin_lock_irq(&x->wait.lock);
  20908. timeout = do_wait_for_common(x, action, timeout, state);
  20909. - spin_unlock_irq(&x->wait.lock);
  20910. + raw_spin_unlock_irq(&x->wait.lock);
  20911. return timeout;
  20912. }
  20913. @@ -277,12 +277,12 @@ bool try_wait_for_completion(struct completion *x)
  20914. if (!READ_ONCE(x->done))
  20915. return 0;
  20916. - spin_lock_irqsave(&x->wait.lock, flags);
  20917. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  20918. if (!x->done)
  20919. ret = 0;
  20920. else
  20921. x->done--;
  20922. - spin_unlock_irqrestore(&x->wait.lock, flags);
  20923. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  20924. return ret;
  20925. }
  20926. EXPORT_SYMBOL(try_wait_for_completion);
  20927. @@ -311,7 +311,7 @@ bool completion_done(struct completion *x)
  20928. * after it's acquired the lock.
  20929. */
  20930. smp_rmb();
  20931. - spin_unlock_wait(&x->wait.lock);
  20932. + raw_spin_unlock_wait(&x->wait.lock);
  20933. return true;
  20934. }
  20935. EXPORT_SYMBOL(completion_done);
  20936. diff --git a/kernel/sched/core.c b/kernel/sched/core.c
  20937. index 9d6b3d869592..3e2e4c3de066 100644
  20938. --- a/kernel/sched/core.c
  20939. +++ b/kernel/sched/core.c
  20940. @@ -260,7 +260,11 @@ late_initcall(sched_init_debug);
  20941. * Number of tasks to iterate in a single balance run.
  20942. * Limited because this is done with IRQs disabled.
  20943. */
  20944. +#ifndef CONFIG_PREEMPT_RT_FULL
  20945. const_debug unsigned int sysctl_sched_nr_migrate = 32;
  20946. +#else
  20947. +const_debug unsigned int sysctl_sched_nr_migrate = 8;
  20948. +#endif
  20949. /*
  20950. * period over which we average the RT time consumption, measured
  20951. @@ -438,6 +442,7 @@ static void init_rq_hrtick(struct rq *rq)
  20952. hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  20953. rq->hrtick_timer.function = hrtick;
  20954. + rq->hrtick_timer.irqsafe = 1;
  20955. }
  20956. #else /* CONFIG_SCHED_HRTICK */
  20957. static inline void hrtick_clear(struct rq *rq)
  20958. @@ -518,9 +523,15 @@ static bool set_nr_if_polling(struct task_struct *p)
  20959. #endif
  20960. #endif
  20961. -void wake_q_add(struct wake_q_head *head, struct task_struct *task)
  20962. +void __wake_q_add(struct wake_q_head *head, struct task_struct *task,
  20963. + bool sleeper)
  20964. {
  20965. - struct wake_q_node *node = &task->wake_q;
  20966. + struct wake_q_node *node;
  20967. +
  20968. + if (sleeper)
  20969. + node = &task->wake_q_sleeper;
  20970. + else
  20971. + node = &task->wake_q;
  20972. /*
  20973. * Atomically grab the task, if ->wake_q is !nil already it means
  20974. @@ -542,24 +553,33 @@ void wake_q_add(struct wake_q_head *head, struct task_struct *task)
  20975. head->lastp = &node->next;
  20976. }
  20977. -void wake_up_q(struct wake_q_head *head)
  20978. +void __wake_up_q(struct wake_q_head *head, bool sleeper)
  20979. {
  20980. struct wake_q_node *node = head->first;
  20981. while (node != WAKE_Q_TAIL) {
  20982. struct task_struct *task;
  20983. - task = container_of(node, struct task_struct, wake_q);
  20984. + if (sleeper)
  20985. + task = container_of(node, struct task_struct, wake_q_sleeper);
  20986. + else
  20987. + task = container_of(node, struct task_struct, wake_q);
  20988. BUG_ON(!task);
  20989. /* task can safely be re-inserted now */
  20990. node = node->next;
  20991. - task->wake_q.next = NULL;
  20992. + if (sleeper)
  20993. + task->wake_q_sleeper.next = NULL;
  20994. + else
  20995. + task->wake_q.next = NULL;
  20996. /*
  20997. * wake_up_process() implies a wmb() to pair with the queueing
  20998. * in wake_q_add() so as not to miss wakeups.
  20999. */
  21000. - wake_up_process(task);
  21001. + if (sleeper)
  21002. + wake_up_lock_sleeper(task);
  21003. + else
  21004. + wake_up_process(task);
  21005. put_task_struct(task);
  21006. }
  21007. }
  21008. @@ -595,6 +615,38 @@ void resched_curr(struct rq *rq)
  21009. trace_sched_wake_idle_without_ipi(cpu);
  21010. }
  21011. +#ifdef CONFIG_PREEMPT_LAZY
  21012. +void resched_curr_lazy(struct rq *rq)
  21013. +{
  21014. + struct task_struct *curr = rq->curr;
  21015. + int cpu;
  21016. +
  21017. + if (!sched_feat(PREEMPT_LAZY)) {
  21018. + resched_curr(rq);
  21019. + return;
  21020. + }
  21021. +
  21022. + lockdep_assert_held(&rq->lock);
  21023. +
  21024. + if (test_tsk_need_resched(curr))
  21025. + return;
  21026. +
  21027. + if (test_tsk_need_resched_lazy(curr))
  21028. + return;
  21029. +
  21030. + set_tsk_need_resched_lazy(curr);
  21031. +
  21032. + cpu = cpu_of(rq);
  21033. + if (cpu == smp_processor_id())
  21034. + return;
  21035. +
  21036. + /* NEED_RESCHED_LAZY must be visible before we test polling */
  21037. + smp_mb();
  21038. + if (!tsk_is_polling(curr))
  21039. + smp_send_reschedule(cpu);
  21040. +}
  21041. +#endif
  21042. +
  21043. void resched_cpu(int cpu)
  21044. {
  21045. struct rq *rq = cpu_rq(cpu);
  21046. @@ -617,11 +669,14 @@ void resched_cpu(int cpu)
  21047. */
  21048. int get_nohz_timer_target(void)
  21049. {
  21050. - int i, cpu = smp_processor_id();
  21051. + int i, cpu;
  21052. struct sched_domain *sd;
  21053. + preempt_disable_rt();
  21054. + cpu = smp_processor_id();
  21055. +
  21056. if (!idle_cpu(cpu) && is_housekeeping_cpu(cpu))
  21057. - return cpu;
  21058. + goto preempt_en_rt;
  21059. rcu_read_lock();
  21060. for_each_domain(cpu, sd) {
  21061. @@ -640,6 +695,8 @@ int get_nohz_timer_target(void)
  21062. cpu = housekeeping_any_cpu();
  21063. unlock:
  21064. rcu_read_unlock();
  21065. +preempt_en_rt:
  21066. + preempt_enable_rt();
  21067. return cpu;
  21068. }
  21069. /*
  21070. @@ -1166,7 +1223,8 @@ void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_ma
  21071. p->nr_cpus_allowed = cpumask_weight(new_mask);
  21072. }
  21073. -void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  21074. +static void __do_set_cpus_allowed_tail(struct task_struct *p,
  21075. + const struct cpumask *new_mask)
  21076. {
  21077. struct rq *rq = task_rq(p);
  21078. bool queued, running;
  21079. @@ -1195,6 +1253,98 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  21080. enqueue_task(rq, p, ENQUEUE_RESTORE);
  21081. }
  21082. +void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  21083. +{
  21084. + if (__migrate_disabled(p)) {
  21085. + lockdep_assert_held(&p->pi_lock);
  21086. +
  21087. + cpumask_copy(&p->cpus_allowed, new_mask);
  21088. +#if defined(CONFIG_PREEMPT_RT_FULL) && defined(CONFIG_SMP)
  21089. + p->migrate_disable_update = 1;
  21090. +#endif
  21091. + return;
  21092. + }
  21093. + __do_set_cpus_allowed_tail(p, new_mask);
  21094. +}
  21095. +
  21096. +static DEFINE_PER_CPU(struct cpumask, sched_cpumasks);
  21097. +static DEFINE_MUTEX(sched_down_mutex);
  21098. +static cpumask_t sched_down_cpumask;
  21099. +
  21100. +void tell_sched_cpu_down_begin(int cpu)
  21101. +{
  21102. + mutex_lock(&sched_down_mutex);
  21103. + cpumask_set_cpu(cpu, &sched_down_cpumask);
  21104. + mutex_unlock(&sched_down_mutex);
  21105. +}
  21106. +
  21107. +void tell_sched_cpu_down_done(int cpu)
  21108. +{
  21109. + mutex_lock(&sched_down_mutex);
  21110. + cpumask_clear_cpu(cpu, &sched_down_cpumask);
  21111. + mutex_unlock(&sched_down_mutex);
  21112. +}
  21113. +
  21114. +/**
  21115. + * migrate_me - try to move the current task off this cpu
  21116. + *
  21117. + * Used by the pin_current_cpu() code to try to get tasks
  21118. + * to move off the current CPU as it is going down.
  21119. + * It will only move the task if the task isn't pinned to
  21120. + * the CPU (with migrate_disable, affinity or NO_SETAFFINITY)
  21121. + * and the task has to be in a RUNNING state. Otherwise the
  21122. + * movement of the task will wake it up (change its state
  21123. + * to running) when the task did not expect it.
  21124. + *
  21125. + * Returns 1 if it succeeded in moving the current task
  21126. + * 0 otherwise.
  21127. + */
  21128. +int migrate_me(void)
  21129. +{
  21130. + struct task_struct *p = current;
  21131. + struct migration_arg arg;
  21132. + struct cpumask *cpumask;
  21133. + struct cpumask *mask;
  21134. + unsigned long flags;
  21135. + unsigned int dest_cpu;
  21136. + struct rq *rq;
  21137. +
  21138. + /*
  21139. + * We can not migrate tasks bounded to a CPU or tasks not
  21140. + * running. The movement of the task will wake it up.
  21141. + */
  21142. + if (p->flags & PF_NO_SETAFFINITY || p->state)
  21143. + return 0;
  21144. +
  21145. + mutex_lock(&sched_down_mutex);
  21146. + rq = task_rq_lock(p, &flags);
  21147. +
  21148. + cpumask = this_cpu_ptr(&sched_cpumasks);
  21149. + mask = &p->cpus_allowed;
  21150. +
  21151. + cpumask_andnot(cpumask, mask, &sched_down_cpumask);
  21152. +
  21153. + if (!cpumask_weight(cpumask)) {
  21154. + /* It's only on this CPU? */
  21155. + task_rq_unlock(rq, p, &flags);
  21156. + mutex_unlock(&sched_down_mutex);
  21157. + return 0;
  21158. + }
  21159. +
  21160. + dest_cpu = cpumask_any_and(cpu_active_mask, cpumask);
  21161. +
  21162. + arg.task = p;
  21163. + arg.dest_cpu = dest_cpu;
  21164. +
  21165. + task_rq_unlock(rq, p, &flags);
  21166. +
  21167. + stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
  21168. + tlb_migrate_finish(p->mm);
  21169. + mutex_unlock(&sched_down_mutex);
  21170. +
  21171. + return 1;
  21172. +}
  21173. +
  21174. /*
  21175. * Change a given task's CPU affinity. Migrate the thread to a
  21176. * proper CPU and schedule it away if the CPU it's executing on
  21177. @@ -1234,7 +1384,7 @@ static int __set_cpus_allowed_ptr(struct task_struct *p,
  21178. do_set_cpus_allowed(p, new_mask);
  21179. /* Can the task run on the task's current CPU? If so, we're done */
  21180. - if (cpumask_test_cpu(task_cpu(p), new_mask))
  21181. + if (cpumask_test_cpu(task_cpu(p), new_mask) || __migrate_disabled(p))
  21182. goto out;
  21183. dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
  21184. @@ -1410,6 +1560,18 @@ int migrate_swap(struct task_struct *cur, struct task_struct *p)
  21185. return ret;
  21186. }
  21187. +static bool check_task_state(struct task_struct *p, long match_state)
  21188. +{
  21189. + bool match = false;
  21190. +
  21191. + raw_spin_lock_irq(&p->pi_lock);
  21192. + if (p->state == match_state || p->saved_state == match_state)
  21193. + match = true;
  21194. + raw_spin_unlock_irq(&p->pi_lock);
  21195. +
  21196. + return match;
  21197. +}
  21198. +
  21199. /*
  21200. * wait_task_inactive - wait for a thread to unschedule.
  21201. *
  21202. @@ -1454,7 +1616,7 @@ unsigned long wait_task_inactive(struct task_struct *p, long match_state)
  21203. * is actually now running somewhere else!
  21204. */
  21205. while (task_running(rq, p)) {
  21206. - if (match_state && unlikely(p->state != match_state))
  21207. + if (match_state && !check_task_state(p, match_state))
  21208. return 0;
  21209. cpu_relax();
  21210. }
  21211. @@ -1469,7 +1631,8 @@ unsigned long wait_task_inactive(struct task_struct *p, long match_state)
  21212. running = task_running(rq, p);
  21213. queued = task_on_rq_queued(p);
  21214. ncsw = 0;
  21215. - if (!match_state || p->state == match_state)
  21216. + if (!match_state || p->state == match_state ||
  21217. + p->saved_state == match_state)
  21218. ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
  21219. task_rq_unlock(rq, p, &flags);
  21220. @@ -1626,7 +1789,7 @@ int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)
  21221. {
  21222. lockdep_assert_held(&p->pi_lock);
  21223. - if (p->nr_cpus_allowed > 1)
  21224. + if (tsk_nr_cpus_allowed(p) > 1)
  21225. cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
  21226. /*
  21227. @@ -1706,10 +1869,6 @@ static inline void ttwu_activate(struct rq *rq, struct task_struct *p, int en_fl
  21228. {
  21229. activate_task(rq, p, en_flags);
  21230. p->on_rq = TASK_ON_RQ_QUEUED;
  21231. -
  21232. - /* if a worker is waking up, notify workqueue */
  21233. - if (p->flags & PF_WQ_WORKER)
  21234. - wq_worker_waking_up(p, cpu_of(rq));
  21235. }
  21236. /*
  21237. @@ -1936,8 +2095,27 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
  21238. */
  21239. smp_mb__before_spinlock();
  21240. raw_spin_lock_irqsave(&p->pi_lock, flags);
  21241. - if (!(p->state & state))
  21242. + if (!(p->state & state)) {
  21243. + /*
  21244. + * The task might be running due to a spinlock sleeper
  21245. + * wakeup. Check the saved state and set it to running
  21246. + * if the wakeup condition is true.
  21247. + */
  21248. + if (!(wake_flags & WF_LOCK_SLEEPER)) {
  21249. + if (p->saved_state & state) {
  21250. + p->saved_state = TASK_RUNNING;
  21251. + success = 1;
  21252. + }
  21253. + }
  21254. goto out;
  21255. + }
  21256. +
  21257. + /*
  21258. + * If this is a regular wakeup, then we can unconditionally
  21259. + * clear the saved state of a "lock sleeper".
  21260. + */
  21261. + if (!(wake_flags & WF_LOCK_SLEEPER))
  21262. + p->saved_state = TASK_RUNNING;
  21263. trace_sched_waking(p);
  21264. @@ -2028,52 +2206,6 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
  21265. return success;
  21266. }
  21267. -/**
  21268. - * try_to_wake_up_local - try to wake up a local task with rq lock held
  21269. - * @p: the thread to be awakened
  21270. - *
  21271. - * Put @p on the run-queue if it's not already there. The caller must
  21272. - * ensure that this_rq() is locked, @p is bound to this_rq() and not
  21273. - * the current task.
  21274. - */
  21275. -static void try_to_wake_up_local(struct task_struct *p)
  21276. -{
  21277. - struct rq *rq = task_rq(p);
  21278. -
  21279. - if (WARN_ON_ONCE(rq != this_rq()) ||
  21280. - WARN_ON_ONCE(p == current))
  21281. - return;
  21282. -
  21283. - lockdep_assert_held(&rq->lock);
  21284. -
  21285. - if (!raw_spin_trylock(&p->pi_lock)) {
  21286. - /*
  21287. - * This is OK, because current is on_cpu, which avoids it being
  21288. - * picked for load-balance and preemption/IRQs are still
  21289. - * disabled avoiding further scheduler activity on it and we've
  21290. - * not yet picked a replacement task.
  21291. - */
  21292. - lockdep_unpin_lock(&rq->lock);
  21293. - raw_spin_unlock(&rq->lock);
  21294. - raw_spin_lock(&p->pi_lock);
  21295. - raw_spin_lock(&rq->lock);
  21296. - lockdep_pin_lock(&rq->lock);
  21297. - }
  21298. -
  21299. - if (!(p->state & TASK_NORMAL))
  21300. - goto out;
  21301. -
  21302. - trace_sched_waking(p);
  21303. -
  21304. - if (!task_on_rq_queued(p))
  21305. - ttwu_activate(rq, p, ENQUEUE_WAKEUP);
  21306. -
  21307. - ttwu_do_wakeup(rq, p, 0);
  21308. - ttwu_stat(p, smp_processor_id(), 0);
  21309. -out:
  21310. - raw_spin_unlock(&p->pi_lock);
  21311. -}
  21312. -
  21313. /**
  21314. * wake_up_process - Wake up a specific process
  21315. * @p: The process to be woken up.
  21316. @@ -2092,6 +2224,18 @@ int wake_up_process(struct task_struct *p)
  21317. }
  21318. EXPORT_SYMBOL(wake_up_process);
  21319. +/**
  21320. + * wake_up_lock_sleeper - Wake up a specific process blocked on a "sleeping lock"
  21321. + * @p: The process to be woken up.
  21322. + *
  21323. + * Same as wake_up_process() above, but wake_flags=WF_LOCK_SLEEPER to indicate
  21324. + * the nature of the wakeup.
  21325. + */
  21326. +int wake_up_lock_sleeper(struct task_struct *p)
  21327. +{
  21328. + return try_to_wake_up(p, TASK_UNINTERRUPTIBLE, WF_LOCK_SLEEPER);
  21329. +}
  21330. +
  21331. int wake_up_state(struct task_struct *p, unsigned int state)
  21332. {
  21333. return try_to_wake_up(p, state, 0);
  21334. @@ -2278,6 +2422,9 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p)
  21335. p->on_cpu = 0;
  21336. #endif
  21337. init_task_preempt_count(p);
  21338. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  21339. + task_thread_info(p)->preempt_lazy_count = 0;
  21340. +#endif
  21341. #ifdef CONFIG_SMP
  21342. plist_node_init(&p->pushable_tasks, MAX_PRIO);
  21343. RB_CLEAR_NODE(&p->pushable_dl_tasks);
  21344. @@ -2602,8 +2749,12 @@ static struct rq *finish_task_switch(struct task_struct *prev)
  21345. finish_arch_post_lock_switch();
  21346. fire_sched_in_preempt_notifiers(current);
  21347. + /*
  21348. + * We use mmdrop_delayed() here so we don't have to do the
  21349. + * full __mmdrop() when we are the last user.
  21350. + */
  21351. if (mm)
  21352. - mmdrop(mm);
  21353. + mmdrop_delayed(mm);
  21354. if (unlikely(prev_state == TASK_DEAD)) {
  21355. if (prev->sched_class->task_dead)
  21356. prev->sched_class->task_dead(prev);
  21357. @@ -2934,16 +3085,6 @@ u64 scheduler_tick_max_deferment(void)
  21358. }
  21359. #endif
  21360. -notrace unsigned long get_parent_ip(unsigned long addr)
  21361. -{
  21362. - if (in_lock_functions(addr)) {
  21363. - addr = CALLER_ADDR2;
  21364. - if (in_lock_functions(addr))
  21365. - addr = CALLER_ADDR3;
  21366. - }
  21367. - return addr;
  21368. -}
  21369. -
  21370. #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
  21371. defined(CONFIG_PREEMPT_TRACER))
  21372. @@ -2965,7 +3106,7 @@ void preempt_count_add(int val)
  21373. PREEMPT_MASK - 10);
  21374. #endif
  21375. if (preempt_count() == val) {
  21376. - unsigned long ip = get_parent_ip(CALLER_ADDR1);
  21377. + unsigned long ip = get_lock_parent_ip();
  21378. #ifdef CONFIG_DEBUG_PREEMPT
  21379. current->preempt_disable_ip = ip;
  21380. #endif
  21381. @@ -2992,7 +3133,7 @@ void preempt_count_sub(int val)
  21382. #endif
  21383. if (preempt_count() == val)
  21384. - trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  21385. + trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
  21386. __preempt_count_sub(val);
  21387. }
  21388. EXPORT_SYMBOL(preempt_count_sub);
  21389. @@ -3047,6 +3188,114 @@ static inline void schedule_debug(struct task_struct *prev)
  21390. schedstat_inc(this_rq(), sched_count);
  21391. }
  21392. +#if defined(CONFIG_PREEMPT_RT_FULL) && defined(CONFIG_SMP)
  21393. +
  21394. +void migrate_disable(void)
  21395. +{
  21396. + struct task_struct *p = current;
  21397. +
  21398. + if (in_atomic() || irqs_disabled()) {
  21399. +#ifdef CONFIG_SCHED_DEBUG
  21400. + p->migrate_disable_atomic++;
  21401. +#endif
  21402. + return;
  21403. + }
  21404. +
  21405. +#ifdef CONFIG_SCHED_DEBUG
  21406. + if (unlikely(p->migrate_disable_atomic)) {
  21407. + tracing_off();
  21408. + WARN_ON_ONCE(1);
  21409. + }
  21410. +#endif
  21411. +
  21412. + if (p->migrate_disable) {
  21413. + p->migrate_disable++;
  21414. + return;
  21415. + }
  21416. +
  21417. + preempt_disable();
  21418. + preempt_lazy_disable();
  21419. + pin_current_cpu();
  21420. + p->migrate_disable = 1;
  21421. + preempt_enable();
  21422. +}
  21423. +EXPORT_SYMBOL(migrate_disable);
  21424. +
  21425. +void migrate_enable(void)
  21426. +{
  21427. + struct task_struct *p = current;
  21428. +
  21429. + if (in_atomic() || irqs_disabled()) {
  21430. +#ifdef CONFIG_SCHED_DEBUG
  21431. + p->migrate_disable_atomic--;
  21432. +#endif
  21433. + return;
  21434. + }
  21435. +
  21436. +#ifdef CONFIG_SCHED_DEBUG
  21437. + if (unlikely(p->migrate_disable_atomic)) {
  21438. + tracing_off();
  21439. + WARN_ON_ONCE(1);
  21440. + }
  21441. +#endif
  21442. + WARN_ON_ONCE(p->migrate_disable <= 0);
  21443. +
  21444. + if (p->migrate_disable > 1) {
  21445. + p->migrate_disable--;
  21446. + return;
  21447. + }
  21448. +
  21449. + preempt_disable();
  21450. + /*
  21451. + * Clearing migrate_disable causes tsk_cpus_allowed to
  21452. + * show the tasks original cpu affinity.
  21453. + */
  21454. + p->migrate_disable = 0;
  21455. +
  21456. + if (p->migrate_disable_update) {
  21457. + unsigned long flags;
  21458. + struct rq *rq;
  21459. +
  21460. + rq = task_rq_lock(p, &flags);
  21461. + update_rq_clock(rq);
  21462. +
  21463. + __do_set_cpus_allowed_tail(p, &p->cpus_allowed);
  21464. + task_rq_unlock(rq, p, &flags);
  21465. +
  21466. + p->migrate_disable_update = 0;
  21467. +
  21468. + WARN_ON(smp_processor_id() != task_cpu(p));
  21469. + if (!cpumask_test_cpu(task_cpu(p), &p->cpus_allowed)) {
  21470. + const struct cpumask *cpu_valid_mask = cpu_active_mask;
  21471. + struct migration_arg arg;
  21472. + unsigned int dest_cpu;
  21473. +
  21474. + if (p->flags & PF_KTHREAD) {
  21475. + /*
  21476. + * Kernel threads are allowed on online && !active CPUs
  21477. + */
  21478. + cpu_valid_mask = cpu_online_mask;
  21479. + }
  21480. + dest_cpu = cpumask_any_and(cpu_valid_mask, &p->cpus_allowed);
  21481. + arg.task = p;
  21482. + arg.dest_cpu = dest_cpu;
  21483. +
  21484. + unpin_current_cpu();
  21485. + preempt_lazy_enable();
  21486. + preempt_enable();
  21487. + stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
  21488. + tlb_migrate_finish(p->mm);
  21489. + return;
  21490. + }
  21491. + }
  21492. +
  21493. + unpin_current_cpu();
  21494. + preempt_enable();
  21495. + preempt_lazy_enable();
  21496. +}
  21497. +EXPORT_SYMBOL(migrate_enable);
  21498. +#endif
  21499. +
  21500. /*
  21501. * Pick up the highest-prio task:
  21502. */
  21503. @@ -3171,19 +3420,6 @@ static void __sched notrace __schedule(bool preempt)
  21504. } else {
  21505. deactivate_task(rq, prev, DEQUEUE_SLEEP);
  21506. prev->on_rq = 0;
  21507. -
  21508. - /*
  21509. - * If a worker went to sleep, notify and ask workqueue
  21510. - * whether it wants to wake up a task to maintain
  21511. - * concurrency.
  21512. - */
  21513. - if (prev->flags & PF_WQ_WORKER) {
  21514. - struct task_struct *to_wakeup;
  21515. -
  21516. - to_wakeup = wq_worker_sleeping(prev, cpu);
  21517. - if (to_wakeup)
  21518. - try_to_wake_up_local(to_wakeup);
  21519. - }
  21520. }
  21521. switch_count = &prev->nvcsw;
  21522. }
  21523. @@ -3193,6 +3429,7 @@ static void __sched notrace __schedule(bool preempt)
  21524. next = pick_next_task(rq, prev);
  21525. clear_tsk_need_resched(prev);
  21526. + clear_tsk_need_resched_lazy(prev);
  21527. clear_preempt_need_resched();
  21528. rq->clock_skip_update = 0;
  21529. @@ -3214,8 +3451,19 @@ static void __sched notrace __schedule(bool preempt)
  21530. static inline void sched_submit_work(struct task_struct *tsk)
  21531. {
  21532. - if (!tsk->state || tsk_is_pi_blocked(tsk))
  21533. + if (!tsk->state)
  21534. return;
  21535. + /*
  21536. + * If a worker went to sleep, notify and ask workqueue whether
  21537. + * it wants to wake up a task to maintain concurrency.
  21538. + */
  21539. + if (tsk->flags & PF_WQ_WORKER)
  21540. + wq_worker_sleeping(tsk);
  21541. +
  21542. +
  21543. + if (tsk_is_pi_blocked(tsk))
  21544. + return;
  21545. +
  21546. /*
  21547. * If we are going to sleep and we have plugged IO queued,
  21548. * make sure to submit it to avoid deadlocks.
  21549. @@ -3224,6 +3472,12 @@ static inline void sched_submit_work(struct task_struct *tsk)
  21550. blk_schedule_flush_plug(tsk);
  21551. }
  21552. +static void sched_update_worker(struct task_struct *tsk)
  21553. +{
  21554. + if (tsk->flags & PF_WQ_WORKER)
  21555. + wq_worker_running(tsk);
  21556. +}
  21557. +
  21558. asmlinkage __visible void __sched schedule(void)
  21559. {
  21560. struct task_struct *tsk = current;
  21561. @@ -3234,6 +3488,7 @@ asmlinkage __visible void __sched schedule(void)
  21562. __schedule(false);
  21563. sched_preempt_enable_no_resched();
  21564. } while (need_resched());
  21565. + sched_update_worker(tsk);
  21566. }
  21567. EXPORT_SYMBOL(schedule);
  21568. @@ -3282,6 +3537,30 @@ static void __sched notrace preempt_schedule_common(void)
  21569. } while (need_resched());
  21570. }
  21571. +#ifdef CONFIG_PREEMPT_LAZY
  21572. +/*
  21573. + * If TIF_NEED_RESCHED is then we allow to be scheduled away since this is
  21574. + * set by a RT task. Oterwise we try to avoid beeing scheduled out as long as
  21575. + * preempt_lazy_count counter >0.
  21576. + */
  21577. +static __always_inline int preemptible_lazy(void)
  21578. +{
  21579. + if (test_thread_flag(TIF_NEED_RESCHED))
  21580. + return 1;
  21581. + if (current_thread_info()->preempt_lazy_count)
  21582. + return 0;
  21583. + return 1;
  21584. +}
  21585. +
  21586. +#else
  21587. +
  21588. +static inline int preemptible_lazy(void)
  21589. +{
  21590. + return 1;
  21591. +}
  21592. +
  21593. +#endif
  21594. +
  21595. #ifdef CONFIG_PREEMPT
  21596. /*
  21597. * this is the entry point to schedule() from in-kernel preemption
  21598. @@ -3296,6 +3575,8 @@ asmlinkage __visible void __sched notrace preempt_schedule(void)
  21599. */
  21600. if (likely(!preemptible()))
  21601. return;
  21602. + if (!preemptible_lazy())
  21603. + return;
  21604. preempt_schedule_common();
  21605. }
  21606. @@ -3322,6 +3603,8 @@ asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
  21607. if (likely(!preemptible()))
  21608. return;
  21609. + if (!preemptible_lazy())
  21610. + return;
  21611. do {
  21612. preempt_disable_notrace();
  21613. @@ -3331,7 +3614,16 @@ asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
  21614. * an infinite recursion.
  21615. */
  21616. prev_ctx = exception_enter();
  21617. + /*
  21618. + * The add/subtract must not be traced by the function
  21619. + * tracer. But we still want to account for the
  21620. + * preempt off latency tracer. Since the _notrace versions
  21621. + * of add/subtract skip the accounting for latency tracer
  21622. + * we must force it manually.
  21623. + */
  21624. + start_critical_timings();
  21625. __schedule(true);
  21626. + stop_critical_timings();
  21627. exception_exit(prev_ctx);
  21628. preempt_enable_no_resched_notrace();
  21629. @@ -4675,6 +4967,7 @@ int __cond_resched_lock(spinlock_t *lock)
  21630. }
  21631. EXPORT_SYMBOL(__cond_resched_lock);
  21632. +#ifndef CONFIG_PREEMPT_RT_FULL
  21633. int __sched __cond_resched_softirq(void)
  21634. {
  21635. BUG_ON(!in_softirq());
  21636. @@ -4688,6 +4981,7 @@ int __sched __cond_resched_softirq(void)
  21637. return 0;
  21638. }
  21639. EXPORT_SYMBOL(__cond_resched_softirq);
  21640. +#endif
  21641. /**
  21642. * yield - yield the current processor to other threads.
  21643. @@ -5054,7 +5348,9 @@ void init_idle(struct task_struct *idle, int cpu)
  21644. /* Set the preempt count _outside_ the spinlocks! */
  21645. init_idle_preempt_count(idle, cpu);
  21646. -
  21647. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  21648. + task_thread_info(idle)->preempt_lazy_count = 0;
  21649. +#endif
  21650. /*
  21651. * The idle tasks have their own, simple scheduling class:
  21652. */
  21653. @@ -5195,6 +5491,8 @@ void sched_setnuma(struct task_struct *p, int nid)
  21654. #endif /* CONFIG_NUMA_BALANCING */
  21655. #ifdef CONFIG_HOTPLUG_CPU
  21656. +static DEFINE_PER_CPU(struct mm_struct *, idle_last_mm);
  21657. +
  21658. /*
  21659. * Ensures that the idle task is using init_mm right before its cpu goes
  21660. * offline.
  21661. @@ -5209,7 +5507,11 @@ void idle_task_exit(void)
  21662. switch_mm(mm, &init_mm, current);
  21663. finish_arch_post_lock_switch();
  21664. }
  21665. - mmdrop(mm);
  21666. + /*
  21667. + * Defer the cleanup to an alive cpu. On RT we can neither
  21668. + * call mmdrop() nor mmdrop_delayed() from here.
  21669. + */
  21670. + per_cpu(idle_last_mm, smp_processor_id()) = mm;
  21671. }
  21672. /*
  21673. @@ -5581,6 +5883,10 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
  21674. case CPU_DEAD:
  21675. calc_load_migrate(rq);
  21676. + if (per_cpu(idle_last_mm, cpu)) {
  21677. + mmdrop(per_cpu(idle_last_mm, cpu));
  21678. + per_cpu(idle_last_mm, cpu) = NULL;
  21679. + }
  21680. break;
  21681. #endif
  21682. }
  21683. @@ -5911,6 +6217,7 @@ static int init_rootdomain(struct root_domain *rd)
  21684. rd->rto_cpu = -1;
  21685. raw_spin_lock_init(&rd->rto_lock);
  21686. init_irq_work(&rd->rto_push_work, rto_push_irq_work_func);
  21687. + rd->rto_push_work.flags |= IRQ_WORK_HARD_IRQ;
  21688. #endif
  21689. init_dl_bw(&rd->dl_bw);
  21690. @@ -7585,7 +7892,7 @@ void __init sched_init(void)
  21691. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  21692. static inline int preempt_count_equals(int preempt_offset)
  21693. {
  21694. - int nested = preempt_count() + rcu_preempt_depth();
  21695. + int nested = preempt_count() + sched_rcu_preempt_depth();
  21696. return (nested == preempt_offset);
  21697. }
  21698. diff --git a/kernel/sched/cpudeadline.c b/kernel/sched/cpudeadline.c
  21699. index 5a75b08cfd85..5be58820465c 100644
  21700. --- a/kernel/sched/cpudeadline.c
  21701. +++ b/kernel/sched/cpudeadline.c
  21702. @@ -103,10 +103,10 @@ int cpudl_find(struct cpudl *cp, struct task_struct *p,
  21703. const struct sched_dl_entity *dl_se = &p->dl;
  21704. if (later_mask &&
  21705. - cpumask_and(later_mask, cp->free_cpus, &p->cpus_allowed)) {
  21706. + cpumask_and(later_mask, cp->free_cpus, tsk_cpus_allowed(p))) {
  21707. best_cpu = cpumask_any(later_mask);
  21708. goto out;
  21709. - } else if (cpumask_test_cpu(cpudl_maximum(cp), &p->cpus_allowed) &&
  21710. + } else if (cpumask_test_cpu(cpudl_maximum(cp), tsk_cpus_allowed(p)) &&
  21711. dl_time_before(dl_se->deadline, cp->elements[0].dl)) {
  21712. best_cpu = cpudl_maximum(cp);
  21713. if (later_mask)
  21714. diff --git a/kernel/sched/cpupri.c b/kernel/sched/cpupri.c
  21715. index 981fcd7dc394..11e9705bf937 100644
  21716. --- a/kernel/sched/cpupri.c
  21717. +++ b/kernel/sched/cpupri.c
  21718. @@ -103,11 +103,11 @@ int cpupri_find(struct cpupri *cp, struct task_struct *p,
  21719. if (skip)
  21720. continue;
  21721. - if (cpumask_any_and(&p->cpus_allowed, vec->mask) >= nr_cpu_ids)
  21722. + if (cpumask_any_and(tsk_cpus_allowed(p), vec->mask) >= nr_cpu_ids)
  21723. continue;
  21724. if (lowest_mask) {
  21725. - cpumask_and(lowest_mask, &p->cpus_allowed, vec->mask);
  21726. + cpumask_and(lowest_mask, tsk_cpus_allowed(p), vec->mask);
  21727. /*
  21728. * We have to ensure that we have at least one bit
  21729. diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
  21730. index a1aecbedf5b1..558b98af241d 100644
  21731. --- a/kernel/sched/cputime.c
  21732. +++ b/kernel/sched/cputime.c
  21733. @@ -685,7 +685,7 @@ static cputime_t get_vtime_delta(struct task_struct *tsk)
  21734. {
  21735. unsigned long long delta = vtime_delta(tsk);
  21736. - WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_SLEEPING);
  21737. + WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_INACTIVE);
  21738. tsk->vtime_snap += delta;
  21739. /* CHECKME: always safe to convert nsecs to cputime? */
  21740. @@ -701,37 +701,37 @@ static void __vtime_account_system(struct task_struct *tsk)
  21741. void vtime_account_system(struct task_struct *tsk)
  21742. {
  21743. - write_seqlock(&tsk->vtime_seqlock);
  21744. + write_seqcount_begin(&tsk->vtime_seqcount);
  21745. __vtime_account_system(tsk);
  21746. - write_sequnlock(&tsk->vtime_seqlock);
  21747. + write_seqcount_end(&tsk->vtime_seqcount);
  21748. }
  21749. void vtime_gen_account_irq_exit(struct task_struct *tsk)
  21750. {
  21751. - write_seqlock(&tsk->vtime_seqlock);
  21752. + write_seqcount_begin(&tsk->vtime_seqcount);
  21753. __vtime_account_system(tsk);
  21754. if (context_tracking_in_user())
  21755. tsk->vtime_snap_whence = VTIME_USER;
  21756. - write_sequnlock(&tsk->vtime_seqlock);
  21757. + write_seqcount_end(&tsk->vtime_seqcount);
  21758. }
  21759. void vtime_account_user(struct task_struct *tsk)
  21760. {
  21761. cputime_t delta_cpu;
  21762. - write_seqlock(&tsk->vtime_seqlock);
  21763. + write_seqcount_begin(&tsk->vtime_seqcount);
  21764. delta_cpu = get_vtime_delta(tsk);
  21765. tsk->vtime_snap_whence = VTIME_SYS;
  21766. account_user_time(tsk, delta_cpu, cputime_to_scaled(delta_cpu));
  21767. - write_sequnlock(&tsk->vtime_seqlock);
  21768. + write_seqcount_end(&tsk->vtime_seqcount);
  21769. }
  21770. void vtime_user_enter(struct task_struct *tsk)
  21771. {
  21772. - write_seqlock(&tsk->vtime_seqlock);
  21773. + write_seqcount_begin(&tsk->vtime_seqcount);
  21774. __vtime_account_system(tsk);
  21775. tsk->vtime_snap_whence = VTIME_USER;
  21776. - write_sequnlock(&tsk->vtime_seqlock);
  21777. + write_seqcount_end(&tsk->vtime_seqcount);
  21778. }
  21779. void vtime_guest_enter(struct task_struct *tsk)
  21780. @@ -743,19 +743,19 @@ void vtime_guest_enter(struct task_struct *tsk)
  21781. * synchronization against the reader (task_gtime())
  21782. * that can thus safely catch up with a tickless delta.
  21783. */
  21784. - write_seqlock(&tsk->vtime_seqlock);
  21785. + write_seqcount_begin(&tsk->vtime_seqcount);
  21786. __vtime_account_system(tsk);
  21787. current->flags |= PF_VCPU;
  21788. - write_sequnlock(&tsk->vtime_seqlock);
  21789. + write_seqcount_end(&tsk->vtime_seqcount);
  21790. }
  21791. EXPORT_SYMBOL_GPL(vtime_guest_enter);
  21792. void vtime_guest_exit(struct task_struct *tsk)
  21793. {
  21794. - write_seqlock(&tsk->vtime_seqlock);
  21795. + write_seqcount_begin(&tsk->vtime_seqcount);
  21796. __vtime_account_system(tsk);
  21797. current->flags &= ~PF_VCPU;
  21798. - write_sequnlock(&tsk->vtime_seqlock);
  21799. + write_seqcount_end(&tsk->vtime_seqcount);
  21800. }
  21801. EXPORT_SYMBOL_GPL(vtime_guest_exit);
  21802. @@ -768,24 +768,26 @@ void vtime_account_idle(struct task_struct *tsk)
  21803. void arch_vtime_task_switch(struct task_struct *prev)
  21804. {
  21805. - write_seqlock(&prev->vtime_seqlock);
  21806. - prev->vtime_snap_whence = VTIME_SLEEPING;
  21807. - write_sequnlock(&prev->vtime_seqlock);
  21808. + write_seqcount_begin(&prev->vtime_seqcount);
  21809. + prev->vtime_snap_whence = VTIME_INACTIVE;
  21810. + write_seqcount_end(&prev->vtime_seqcount);
  21811. - write_seqlock(&current->vtime_seqlock);
  21812. + write_seqcount_begin(&current->vtime_seqcount);
  21813. current->vtime_snap_whence = VTIME_SYS;
  21814. current->vtime_snap = sched_clock_cpu(smp_processor_id());
  21815. - write_sequnlock(&current->vtime_seqlock);
  21816. + write_seqcount_end(&current->vtime_seqcount);
  21817. }
  21818. void vtime_init_idle(struct task_struct *t, int cpu)
  21819. {
  21820. unsigned long flags;
  21821. - write_seqlock_irqsave(&t->vtime_seqlock, flags);
  21822. + local_irq_save(flags);
  21823. + write_seqcount_begin(&t->vtime_seqcount);
  21824. t->vtime_snap_whence = VTIME_SYS;
  21825. t->vtime_snap = sched_clock_cpu(cpu);
  21826. - write_sequnlock_irqrestore(&t->vtime_seqlock, flags);
  21827. + write_seqcount_end(&t->vtime_seqcount);
  21828. + local_irq_restore(flags);
  21829. }
  21830. cputime_t task_gtime(struct task_struct *t)
  21831. @@ -797,13 +799,13 @@ cputime_t task_gtime(struct task_struct *t)
  21832. return t->gtime;
  21833. do {
  21834. - seq = read_seqbegin(&t->vtime_seqlock);
  21835. + seq = read_seqcount_begin(&t->vtime_seqcount);
  21836. gtime = t->gtime;
  21837. if (t->flags & PF_VCPU)
  21838. gtime += vtime_delta(t);
  21839. - } while (read_seqretry(&t->vtime_seqlock, seq));
  21840. + } while (read_seqcount_retry(&t->vtime_seqcount, seq));
  21841. return gtime;
  21842. }
  21843. @@ -826,7 +828,7 @@ fetch_task_cputime(struct task_struct *t,
  21844. *udelta = 0;
  21845. *sdelta = 0;
  21846. - seq = read_seqbegin(&t->vtime_seqlock);
  21847. + seq = read_seqcount_begin(&t->vtime_seqcount);
  21848. if (u_dst)
  21849. *u_dst = *u_src;
  21850. @@ -834,7 +836,7 @@ fetch_task_cputime(struct task_struct *t,
  21851. *s_dst = *s_src;
  21852. /* Task is sleeping, nothing to add */
  21853. - if (t->vtime_snap_whence == VTIME_SLEEPING ||
  21854. + if (t->vtime_snap_whence == VTIME_INACTIVE ||
  21855. is_idle_task(t))
  21856. continue;
  21857. @@ -850,7 +852,7 @@ fetch_task_cputime(struct task_struct *t,
  21858. if (t->vtime_snap_whence == VTIME_SYS)
  21859. *sdelta = delta;
  21860. }
  21861. - } while (read_seqretry(&t->vtime_seqlock, seq));
  21862. + } while (read_seqcount_retry(&t->vtime_seqcount, seq));
  21863. }
  21864. diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
  21865. index a996f7356216..645c18fe2075 100644
  21866. --- a/kernel/sched/deadline.c
  21867. +++ b/kernel/sched/deadline.c
  21868. @@ -134,7 +134,7 @@ static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  21869. {
  21870. struct task_struct *p = dl_task_of(dl_se);
  21871. - if (p->nr_cpus_allowed > 1)
  21872. + if (tsk_nr_cpus_allowed(p) > 1)
  21873. dl_rq->dl_nr_migratory++;
  21874. update_dl_migration(dl_rq);
  21875. @@ -144,7 +144,7 @@ static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  21876. {
  21877. struct task_struct *p = dl_task_of(dl_se);
  21878. - if (p->nr_cpus_allowed > 1)
  21879. + if (tsk_nr_cpus_allowed(p) > 1)
  21880. dl_rq->dl_nr_migratory--;
  21881. update_dl_migration(dl_rq);
  21882. @@ -702,6 +702,7 @@ void init_dl_task_timer(struct sched_dl_entity *dl_se)
  21883. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  21884. timer->function = dl_task_timer;
  21885. + timer->irqsafe = 1;
  21886. }
  21887. /*
  21888. @@ -1039,7 +1040,7 @@ static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  21889. enqueue_dl_entity(&p->dl, pi_se, flags);
  21890. - if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  21891. + if (!task_current(rq, p) && tsk_nr_cpus_allowed(p) > 1)
  21892. enqueue_pushable_dl_task(rq, p);
  21893. }
  21894. @@ -1117,9 +1118,9 @@ select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
  21895. * try to make it stay here, it might be important.
  21896. */
  21897. if (unlikely(dl_task(curr)) &&
  21898. - (curr->nr_cpus_allowed < 2 ||
  21899. + (tsk_nr_cpus_allowed(curr) < 2 ||
  21900. !dl_entity_preempt(&p->dl, &curr->dl)) &&
  21901. - (p->nr_cpus_allowed > 1)) {
  21902. + (tsk_nr_cpus_allowed(p) > 1)) {
  21903. int target = find_later_rq(p);
  21904. if (target != -1 &&
  21905. @@ -1140,7 +1141,7 @@ static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
  21906. * Current can't be migrated, useless to reschedule,
  21907. * let's hope p can move out.
  21908. */
  21909. - if (rq->curr->nr_cpus_allowed == 1 ||
  21910. + if (tsk_nr_cpus_allowed(rq->curr) == 1 ||
  21911. cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1)
  21912. return;
  21913. @@ -1148,7 +1149,7 @@ static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
  21914. * p is migratable, so let's not schedule it and
  21915. * see if it is pushed or pulled somewhere else.
  21916. */
  21917. - if (p->nr_cpus_allowed != 1 &&
  21918. + if (tsk_nr_cpus_allowed(p) != 1 &&
  21919. cpudl_find(&rq->rd->cpudl, p, NULL) != -1)
  21920. return;
  21921. @@ -1262,7 +1263,7 @@ static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
  21922. {
  21923. update_curr_dl(rq);
  21924. - if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
  21925. + if (on_dl_rq(&p->dl) && tsk_nr_cpus_allowed(p) > 1)
  21926. enqueue_pushable_dl_task(rq, p);
  21927. }
  21928. @@ -1385,7 +1386,7 @@ static int find_later_rq(struct task_struct *task)
  21929. if (unlikely(!later_mask))
  21930. return -1;
  21931. - if (task->nr_cpus_allowed == 1)
  21932. + if (tsk_nr_cpus_allowed(task) == 1)
  21933. return -1;
  21934. /*
  21935. @@ -1491,7 +1492,7 @@ static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
  21936. if (double_lock_balance(rq, later_rq)) {
  21937. if (unlikely(task_rq(task) != rq ||
  21938. !cpumask_test_cpu(later_rq->cpu,
  21939. - &task->cpus_allowed) ||
  21940. + tsk_cpus_allowed(task)) ||
  21941. task_running(rq, task) ||
  21942. !task_on_rq_queued(task))) {
  21943. double_unlock_balance(rq, later_rq);
  21944. @@ -1530,7 +1531,7 @@ static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
  21945. BUG_ON(rq->cpu != task_cpu(p));
  21946. BUG_ON(task_current(rq, p));
  21947. - BUG_ON(p->nr_cpus_allowed <= 1);
  21948. + BUG_ON(tsk_nr_cpus_allowed(p) <= 1);
  21949. BUG_ON(!task_on_rq_queued(p));
  21950. BUG_ON(!dl_task(p));
  21951. @@ -1569,7 +1570,7 @@ static int push_dl_task(struct rq *rq)
  21952. */
  21953. if (dl_task(rq->curr) &&
  21954. dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
  21955. - rq->curr->nr_cpus_allowed > 1) {
  21956. + tsk_nr_cpus_allowed(rq->curr) > 1) {
  21957. resched_curr(rq);
  21958. return 0;
  21959. }
  21960. @@ -1716,9 +1717,9 @@ static void task_woken_dl(struct rq *rq, struct task_struct *p)
  21961. {
  21962. if (!task_running(rq, p) &&
  21963. !test_tsk_need_resched(rq->curr) &&
  21964. - p->nr_cpus_allowed > 1 &&
  21965. + tsk_nr_cpus_allowed(p) > 1 &&
  21966. dl_task(rq->curr) &&
  21967. - (rq->curr->nr_cpus_allowed < 2 ||
  21968. + (tsk_nr_cpus_allowed(rq->curr) < 2 ||
  21969. !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
  21970. push_dl_tasks(rq);
  21971. }
  21972. @@ -1819,7 +1820,7 @@ static void switched_to_dl(struct rq *rq, struct task_struct *p)
  21973. {
  21974. if (task_on_rq_queued(p) && rq->curr != p) {
  21975. #ifdef CONFIG_SMP
  21976. - if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
  21977. + if (tsk_nr_cpus_allowed(p) > 1 && rq->dl.overloaded)
  21978. queue_push_tasks(rq);
  21979. #endif
  21980. if (dl_task(rq->curr))
  21981. diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c
  21982. index 641511771ae6..a2d69b883623 100644
  21983. --- a/kernel/sched/debug.c
  21984. +++ b/kernel/sched/debug.c
  21985. @@ -251,6 +251,9 @@ void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq)
  21986. P(rt_throttled);
  21987. PN(rt_time);
  21988. PN(rt_runtime);
  21989. +#ifdef CONFIG_SMP
  21990. + P(rt_nr_migratory);
  21991. +#endif
  21992. #undef PN
  21993. #undef P
  21994. @@ -635,6 +638,10 @@ void proc_sched_show_task(struct task_struct *p, struct seq_file *m)
  21995. #endif
  21996. P(policy);
  21997. P(prio);
  21998. +#ifdef CONFIG_PREEMPT_RT_FULL
  21999. + P(migrate_disable);
  22000. +#endif
  22001. + P(nr_cpus_allowed);
  22002. #undef PN
  22003. #undef __PN
  22004. #undef P
  22005. diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
  22006. index 812069b66f47..ddf1424bcc78 100644
  22007. --- a/kernel/sched/fair.c
  22008. +++ b/kernel/sched/fair.c
  22009. @@ -3166,7 +3166,7 @@ check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  22010. ideal_runtime = sched_slice(cfs_rq, curr);
  22011. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  22012. if (delta_exec > ideal_runtime) {
  22013. - resched_curr(rq_of(cfs_rq));
  22014. + resched_curr_lazy(rq_of(cfs_rq));
  22015. /*
  22016. * The current task ran long enough, ensure it doesn't get
  22017. * re-elected due to buddy favours.
  22018. @@ -3190,7 +3190,7 @@ check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  22019. return;
  22020. if (delta > ideal_runtime)
  22021. - resched_curr(rq_of(cfs_rq));
  22022. + resched_curr_lazy(rq_of(cfs_rq));
  22023. }
  22024. static void
  22025. @@ -3330,7 +3330,7 @@ entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
  22026. * validating it and just reschedule.
  22027. */
  22028. if (queued) {
  22029. - resched_curr(rq_of(cfs_rq));
  22030. + resched_curr_lazy(rq_of(cfs_rq));
  22031. return;
  22032. }
  22033. /*
  22034. @@ -3512,7 +3512,7 @@ static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
  22035. * hierarchy can be throttled
  22036. */
  22037. if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
  22038. - resched_curr(rq_of(cfs_rq));
  22039. + resched_curr_lazy(rq_of(cfs_rq));
  22040. }
  22041. static __always_inline
  22042. @@ -4144,7 +4144,7 @@ static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
  22043. if (delta < 0) {
  22044. if (rq->curr == p)
  22045. - resched_curr(rq);
  22046. + resched_curr_lazy(rq);
  22047. return;
  22048. }
  22049. hrtick_start(rq, delta);
  22050. @@ -5232,7 +5232,7 @@ static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_
  22051. return;
  22052. preempt:
  22053. - resched_curr(rq);
  22054. + resched_curr_lazy(rq);
  22055. /*
  22056. * Only set the backward buddy when the current task is still
  22057. * on the rq. This can happen when a wakeup gets interleaved
  22058. @@ -7983,7 +7983,7 @@ static void task_fork_fair(struct task_struct *p)
  22059. * 'current' within the tree based on its new key value.
  22060. */
  22061. swap(curr->vruntime, se->vruntime);
  22062. - resched_curr(rq);
  22063. + resched_curr_lazy(rq);
  22064. }
  22065. se->vruntime -= cfs_rq->min_vruntime;
  22066. @@ -8008,7 +8008,7 @@ prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
  22067. */
  22068. if (rq->curr == p) {
  22069. if (p->prio > oldprio)
  22070. - resched_curr(rq);
  22071. + resched_curr_lazy(rq);
  22072. } else
  22073. check_preempt_curr(rq, p, 0);
  22074. }
  22075. diff --git a/kernel/sched/features.h b/kernel/sched/features.h
  22076. index 69631fa46c2f..6d28fcd08872 100644
  22077. --- a/kernel/sched/features.h
  22078. +++ b/kernel/sched/features.h
  22079. @@ -45,11 +45,19 @@ SCHED_FEAT(LB_BIAS, true)
  22080. */
  22081. SCHED_FEAT(NONTASK_CAPACITY, true)
  22082. +#ifdef CONFIG_PREEMPT_RT_FULL
  22083. +SCHED_FEAT(TTWU_QUEUE, false)
  22084. +# ifdef CONFIG_PREEMPT_LAZY
  22085. +SCHED_FEAT(PREEMPT_LAZY, true)
  22086. +# endif
  22087. +#else
  22088. +
  22089. /*
  22090. * Queue remote wakeups on the target CPU and process them
  22091. * using the scheduler IPI. Reduces rq->lock contention/bounces.
  22092. */
  22093. SCHED_FEAT(TTWU_QUEUE, true)
  22094. +#endif
  22095. #ifdef HAVE_RT_PUSH_IPI
  22096. /*
  22097. diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
  22098. index 95fefb364dab..14f2d740edab 100644
  22099. --- a/kernel/sched/rt.c
  22100. +++ b/kernel/sched/rt.c
  22101. @@ -47,6 +47,7 @@ void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
  22102. hrtimer_init(&rt_b->rt_period_timer,
  22103. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  22104. + rt_b->rt_period_timer.irqsafe = 1;
  22105. rt_b->rt_period_timer.function = sched_rt_period_timer;
  22106. }
  22107. @@ -315,7 +316,7 @@ static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  22108. rt_rq = &rq_of_rt_rq(rt_rq)->rt;
  22109. rt_rq->rt_nr_total++;
  22110. - if (p->nr_cpus_allowed > 1)
  22111. + if (tsk_nr_cpus_allowed(p) > 1)
  22112. rt_rq->rt_nr_migratory++;
  22113. update_rt_migration(rt_rq);
  22114. @@ -332,7 +333,7 @@ static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  22115. rt_rq = &rq_of_rt_rq(rt_rq)->rt;
  22116. rt_rq->rt_nr_total--;
  22117. - if (p->nr_cpus_allowed > 1)
  22118. + if (tsk_nr_cpus_allowed(p) > 1)
  22119. rt_rq->rt_nr_migratory--;
  22120. update_rt_migration(rt_rq);
  22121. @@ -1251,7 +1252,7 @@ enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
  22122. enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD);
  22123. - if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  22124. + if (!task_current(rq, p) && tsk_nr_cpus_allowed(p) > 1)
  22125. enqueue_pushable_task(rq, p);
  22126. }
  22127. @@ -1340,7 +1341,7 @@ select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
  22128. * will have to sort it out.
  22129. */
  22130. if (curr && unlikely(rt_task(curr)) &&
  22131. - (curr->nr_cpus_allowed < 2 ||
  22132. + (tsk_nr_cpus_allowed(curr) < 2 ||
  22133. curr->prio <= p->prio)) {
  22134. int target = find_lowest_rq(p);
  22135. @@ -1364,7 +1365,7 @@ static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
  22136. * Current can't be migrated, useless to reschedule,
  22137. * let's hope p can move out.
  22138. */
  22139. - if (rq->curr->nr_cpus_allowed == 1 ||
  22140. + if (tsk_nr_cpus_allowed(rq->curr) == 1 ||
  22141. !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
  22142. return;
  22143. @@ -1372,7 +1373,7 @@ static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
  22144. * p is migratable, so let's not schedule it and
  22145. * see if it is pushed or pulled somewhere else.
  22146. */
  22147. - if (p->nr_cpus_allowed != 1
  22148. + if (tsk_nr_cpus_allowed(p) != 1
  22149. && cpupri_find(&rq->rd->cpupri, p, NULL))
  22150. return;
  22151. @@ -1506,7 +1507,7 @@ static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
  22152. * The previous task needs to be made eligible for pushing
  22153. * if it is still active
  22154. */
  22155. - if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
  22156. + if (on_rt_rq(&p->rt) && tsk_nr_cpus_allowed(p) > 1)
  22157. enqueue_pushable_task(rq, p);
  22158. }
  22159. @@ -1556,7 +1557,7 @@ static int find_lowest_rq(struct task_struct *task)
  22160. if (unlikely(!lowest_mask))
  22161. return -1;
  22162. - if (task->nr_cpus_allowed == 1)
  22163. + if (tsk_nr_cpus_allowed(task) == 1)
  22164. return -1; /* No other targets possible */
  22165. if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
  22166. @@ -1688,7 +1689,7 @@ static struct task_struct *pick_next_pushable_task(struct rq *rq)
  22167. BUG_ON(rq->cpu != task_cpu(p));
  22168. BUG_ON(task_current(rq, p));
  22169. - BUG_ON(p->nr_cpus_allowed <= 1);
  22170. + BUG_ON(tsk_nr_cpus_allowed(p) <= 1);
  22171. BUG_ON(!task_on_rq_queued(p));
  22172. BUG_ON(!rt_task(p));
  22173. @@ -2060,9 +2061,9 @@ static void task_woken_rt(struct rq *rq, struct task_struct *p)
  22174. {
  22175. if (!task_running(rq, p) &&
  22176. !test_tsk_need_resched(rq->curr) &&
  22177. - p->nr_cpus_allowed > 1 &&
  22178. + tsk_nr_cpus_allowed(p) > 1 &&
  22179. (dl_task(rq->curr) || rt_task(rq->curr)) &&
  22180. - (rq->curr->nr_cpus_allowed < 2 ||
  22181. + (tsk_nr_cpus_allowed(rq->curr) < 2 ||
  22182. rq->curr->prio <= p->prio))
  22183. push_rt_tasks(rq);
  22184. }
  22185. @@ -2135,7 +2136,7 @@ static void switched_to_rt(struct rq *rq, struct task_struct *p)
  22186. */
  22187. if (task_on_rq_queued(p) && rq->curr != p) {
  22188. #ifdef CONFIG_SMP
  22189. - if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
  22190. + if (tsk_nr_cpus_allowed(p) > 1 && rq->rt.overloaded)
  22191. queue_push_tasks(rq);
  22192. #endif /* CONFIG_SMP */
  22193. if (p->prio < rq->curr->prio)
  22194. diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
  22195. index 448a8266ceea..e45a90ce57f7 100644
  22196. --- a/kernel/sched/sched.h
  22197. +++ b/kernel/sched/sched.h
  22198. @@ -1110,6 +1110,7 @@ static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  22199. #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
  22200. #define WF_FORK 0x02 /* child wakeup after fork */
  22201. #define WF_MIGRATED 0x4 /* internal use, task got migrated */
  22202. +#define WF_LOCK_SLEEPER 0x08 /* wakeup spinlock "sleeper" */
  22203. /*
  22204. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  22205. @@ -1309,6 +1310,15 @@ extern void init_sched_fair_class(void);
  22206. extern void resched_curr(struct rq *rq);
  22207. extern void resched_cpu(int cpu);
  22208. +#ifdef CONFIG_PREEMPT_LAZY
  22209. +extern void resched_curr_lazy(struct rq *rq);
  22210. +#else
  22211. +static inline void resched_curr_lazy(struct rq *rq)
  22212. +{
  22213. + resched_curr(rq);
  22214. +}
  22215. +#endif
  22216. +
  22217. extern struct rt_bandwidth def_rt_bandwidth;
  22218. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  22219. diff --git a/kernel/sched/swait.c b/kernel/sched/swait.c
  22220. new file mode 100644
  22221. index 000000000000..205fe36868f9
  22222. --- /dev/null
  22223. +++ b/kernel/sched/swait.c
  22224. @@ -0,0 +1,143 @@
  22225. +#include <linux/sched.h>
  22226. +#include <linux/swait.h>
  22227. +#include <linux/suspend.h>
  22228. +
  22229. +void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
  22230. + struct lock_class_key *key)
  22231. +{
  22232. + raw_spin_lock_init(&q->lock);
  22233. + lockdep_set_class_and_name(&q->lock, key, name);
  22234. + INIT_LIST_HEAD(&q->task_list);
  22235. +}
  22236. +EXPORT_SYMBOL(__init_swait_queue_head);
  22237. +
  22238. +/*
  22239. + * The thing about the wake_up_state() return value; I think we can ignore it.
  22240. + *
  22241. + * If for some reason it would return 0, that means the previously waiting
  22242. + * task is already running, so it will observe condition true (or has already).
  22243. + */
  22244. +void swake_up_locked(struct swait_queue_head *q)
  22245. +{
  22246. + struct swait_queue *curr;
  22247. +
  22248. + if (list_empty(&q->task_list))
  22249. + return;
  22250. +
  22251. + curr = list_first_entry(&q->task_list, typeof(*curr), task_list);
  22252. + wake_up_process(curr->task);
  22253. + list_del_init(&curr->task_list);
  22254. +}
  22255. +EXPORT_SYMBOL(swake_up_locked);
  22256. +
  22257. +void swake_up_all_locked(struct swait_queue_head *q)
  22258. +{
  22259. + struct swait_queue *curr;
  22260. + int wakes = 0;
  22261. +
  22262. + while (!list_empty(&q->task_list)) {
  22263. +
  22264. + curr = list_first_entry(&q->task_list, typeof(*curr),
  22265. + task_list);
  22266. + wake_up_process(curr->task);
  22267. + list_del_init(&curr->task_list);
  22268. + wakes++;
  22269. + }
  22270. + if (pm_in_action)
  22271. + return;
  22272. + WARN(wakes > 2, "complate_all() with %d waiters\n", wakes);
  22273. +}
  22274. +EXPORT_SYMBOL(swake_up_all_locked);
  22275. +
  22276. +void swake_up(struct swait_queue_head *q)
  22277. +{
  22278. + unsigned long flags;
  22279. +
  22280. + if (!swait_active(q))
  22281. + return;
  22282. +
  22283. + raw_spin_lock_irqsave(&q->lock, flags);
  22284. + swake_up_locked(q);
  22285. + raw_spin_unlock_irqrestore(&q->lock, flags);
  22286. +}
  22287. +EXPORT_SYMBOL(swake_up);
  22288. +
  22289. +/*
  22290. + * Does not allow usage from IRQ disabled, since we must be able to
  22291. + * release IRQs to guarantee bounded hold time.
  22292. + */
  22293. +void swake_up_all(struct swait_queue_head *q)
  22294. +{
  22295. + struct swait_queue *curr;
  22296. + LIST_HEAD(tmp);
  22297. +
  22298. + if (!swait_active(q))
  22299. + return;
  22300. +
  22301. + raw_spin_lock_irq(&q->lock);
  22302. + list_splice_init(&q->task_list, &tmp);
  22303. + while (!list_empty(&tmp)) {
  22304. + curr = list_first_entry(&tmp, typeof(*curr), task_list);
  22305. +
  22306. + wake_up_state(curr->task, TASK_NORMAL);
  22307. + list_del_init(&curr->task_list);
  22308. +
  22309. + if (list_empty(&tmp))
  22310. + break;
  22311. +
  22312. + raw_spin_unlock_irq(&q->lock);
  22313. + raw_spin_lock_irq(&q->lock);
  22314. + }
  22315. + raw_spin_unlock_irq(&q->lock);
  22316. +}
  22317. +EXPORT_SYMBOL(swake_up_all);
  22318. +
  22319. +void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait)
  22320. +{
  22321. + wait->task = current;
  22322. + if (list_empty(&wait->task_list))
  22323. + list_add(&wait->task_list, &q->task_list);
  22324. +}
  22325. +
  22326. +void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state)
  22327. +{
  22328. + unsigned long flags;
  22329. +
  22330. + raw_spin_lock_irqsave(&q->lock, flags);
  22331. + __prepare_to_swait(q, wait);
  22332. + set_current_state(state);
  22333. + raw_spin_unlock_irqrestore(&q->lock, flags);
  22334. +}
  22335. +EXPORT_SYMBOL(prepare_to_swait);
  22336. +
  22337. +long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state)
  22338. +{
  22339. + if (signal_pending_state(state, current))
  22340. + return -ERESTARTSYS;
  22341. +
  22342. + prepare_to_swait(q, wait, state);
  22343. +
  22344. + return 0;
  22345. +}
  22346. +EXPORT_SYMBOL(prepare_to_swait_event);
  22347. +
  22348. +void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
  22349. +{
  22350. + __set_current_state(TASK_RUNNING);
  22351. + if (!list_empty(&wait->task_list))
  22352. + list_del_init(&wait->task_list);
  22353. +}
  22354. +
  22355. +void finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
  22356. +{
  22357. + unsigned long flags;
  22358. +
  22359. + __set_current_state(TASK_RUNNING);
  22360. +
  22361. + if (!list_empty_careful(&wait->task_list)) {
  22362. + raw_spin_lock_irqsave(&q->lock, flags);
  22363. + list_del_init(&wait->task_list);
  22364. + raw_spin_unlock_irqrestore(&q->lock, flags);
  22365. + }
  22366. +}
  22367. +EXPORT_SYMBOL(finish_swait);
  22368. diff --git a/kernel/sched/swork.c b/kernel/sched/swork.c
  22369. new file mode 100644
  22370. index 000000000000..1950f40ca725
  22371. --- /dev/null
  22372. +++ b/kernel/sched/swork.c
  22373. @@ -0,0 +1,173 @@
  22374. +/*
  22375. + * Copyright (C) 2014 BMW Car IT GmbH, Daniel Wagner daniel.wagner@bmw-carit.de
  22376. + *
  22377. + * Provides a framework for enqueuing callbacks from irq context
  22378. + * PREEMPT_RT_FULL safe. The callbacks are executed in kthread context.
  22379. + */
  22380. +
  22381. +#include <linux/swait.h>
  22382. +#include <linux/swork.h>
  22383. +#include <linux/kthread.h>
  22384. +#include <linux/slab.h>
  22385. +#include <linux/spinlock.h>
  22386. +#include <linux/export.h>
  22387. +
  22388. +#define SWORK_EVENT_PENDING (1 << 0)
  22389. +
  22390. +static DEFINE_MUTEX(worker_mutex);
  22391. +static struct sworker *glob_worker;
  22392. +
  22393. +struct sworker {
  22394. + struct list_head events;
  22395. + struct swait_queue_head wq;
  22396. +
  22397. + raw_spinlock_t lock;
  22398. +
  22399. + struct task_struct *task;
  22400. + int refs;
  22401. +};
  22402. +
  22403. +static bool swork_readable(struct sworker *worker)
  22404. +{
  22405. + bool r;
  22406. +
  22407. + if (kthread_should_stop())
  22408. + return true;
  22409. +
  22410. + raw_spin_lock_irq(&worker->lock);
  22411. + r = !list_empty(&worker->events);
  22412. + raw_spin_unlock_irq(&worker->lock);
  22413. +
  22414. + return r;
  22415. +}
  22416. +
  22417. +static int swork_kthread(void *arg)
  22418. +{
  22419. + struct sworker *worker = arg;
  22420. +
  22421. + for (;;) {
  22422. + swait_event_interruptible(worker->wq,
  22423. + swork_readable(worker));
  22424. + if (kthread_should_stop())
  22425. + break;
  22426. +
  22427. + raw_spin_lock_irq(&worker->lock);
  22428. + while (!list_empty(&worker->events)) {
  22429. + struct swork_event *sev;
  22430. +
  22431. + sev = list_first_entry(&worker->events,
  22432. + struct swork_event, item);
  22433. + list_del(&sev->item);
  22434. + raw_spin_unlock_irq(&worker->lock);
  22435. +
  22436. + WARN_ON_ONCE(!test_and_clear_bit(SWORK_EVENT_PENDING,
  22437. + &sev->flags));
  22438. + sev->func(sev);
  22439. + raw_spin_lock_irq(&worker->lock);
  22440. + }
  22441. + raw_spin_unlock_irq(&worker->lock);
  22442. + }
  22443. + return 0;
  22444. +}
  22445. +
  22446. +static struct sworker *swork_create(void)
  22447. +{
  22448. + struct sworker *worker;
  22449. +
  22450. + worker = kzalloc(sizeof(*worker), GFP_KERNEL);
  22451. + if (!worker)
  22452. + return ERR_PTR(-ENOMEM);
  22453. +
  22454. + INIT_LIST_HEAD(&worker->events);
  22455. + raw_spin_lock_init(&worker->lock);
  22456. + init_swait_queue_head(&worker->wq);
  22457. +
  22458. + worker->task = kthread_run(swork_kthread, worker, "kswork");
  22459. + if (IS_ERR(worker->task)) {
  22460. + kfree(worker);
  22461. + return ERR_PTR(-ENOMEM);
  22462. + }
  22463. +
  22464. + return worker;
  22465. +}
  22466. +
  22467. +static void swork_destroy(struct sworker *worker)
  22468. +{
  22469. + kthread_stop(worker->task);
  22470. +
  22471. + WARN_ON(!list_empty(&worker->events));
  22472. + kfree(worker);
  22473. +}
  22474. +
  22475. +/**
  22476. + * swork_queue - queue swork
  22477. + *
  22478. + * Returns %false if @work was already on a queue, %true otherwise.
  22479. + *
  22480. + * The work is queued and processed on a random CPU
  22481. + */
  22482. +bool swork_queue(struct swork_event *sev)
  22483. +{
  22484. + unsigned long flags;
  22485. +
  22486. + if (test_and_set_bit(SWORK_EVENT_PENDING, &sev->flags))
  22487. + return false;
  22488. +
  22489. + raw_spin_lock_irqsave(&glob_worker->lock, flags);
  22490. + list_add_tail(&sev->item, &glob_worker->events);
  22491. + raw_spin_unlock_irqrestore(&glob_worker->lock, flags);
  22492. +
  22493. + swake_up(&glob_worker->wq);
  22494. + return true;
  22495. +}
  22496. +EXPORT_SYMBOL_GPL(swork_queue);
  22497. +
  22498. +/**
  22499. + * swork_get - get an instance of the sworker
  22500. + *
  22501. + * Returns an negative error code if the initialization if the worker did not
  22502. + * work, %0 otherwise.
  22503. + *
  22504. + */
  22505. +int swork_get(void)
  22506. +{
  22507. + struct sworker *worker;
  22508. +
  22509. + mutex_lock(&worker_mutex);
  22510. + if (!glob_worker) {
  22511. + worker = swork_create();
  22512. + if (IS_ERR(worker)) {
  22513. + mutex_unlock(&worker_mutex);
  22514. + return -ENOMEM;
  22515. + }
  22516. +
  22517. + glob_worker = worker;
  22518. + }
  22519. +
  22520. + glob_worker->refs++;
  22521. + mutex_unlock(&worker_mutex);
  22522. +
  22523. + return 0;
  22524. +}
  22525. +EXPORT_SYMBOL_GPL(swork_get);
  22526. +
  22527. +/**
  22528. + * swork_put - puts an instance of the sworker
  22529. + *
  22530. + * Will destroy the sworker thread. This function must not be called until all
  22531. + * queued events have been completed.
  22532. + */
  22533. +void swork_put(void)
  22534. +{
  22535. + mutex_lock(&worker_mutex);
  22536. +
  22537. + glob_worker->refs--;
  22538. + if (glob_worker->refs > 0)
  22539. + goto out;
  22540. +
  22541. + swork_destroy(glob_worker);
  22542. + glob_worker = NULL;
  22543. +out:
  22544. + mutex_unlock(&worker_mutex);
  22545. +}
  22546. +EXPORT_SYMBOL_GPL(swork_put);
  22547. diff --git a/kernel/signal.c b/kernel/signal.c
  22548. index 4a548c6a4118..e870a133857b 100644
  22549. --- a/kernel/signal.c
  22550. +++ b/kernel/signal.c
  22551. @@ -14,6 +14,7 @@
  22552. #include <linux/export.h>
  22553. #include <linux/init.h>
  22554. #include <linux/sched.h>
  22555. +#include <linux/sched/rt.h>
  22556. #include <linux/fs.h>
  22557. #include <linux/tty.h>
  22558. #include <linux/binfmts.h>
  22559. @@ -354,13 +355,30 @@ static bool task_participate_group_stop(struct task_struct *task)
  22560. return false;
  22561. }
  22562. +static inline struct sigqueue *get_task_cache(struct task_struct *t)
  22563. +{
  22564. + struct sigqueue *q = t->sigqueue_cache;
  22565. +
  22566. + if (cmpxchg(&t->sigqueue_cache, q, NULL) != q)
  22567. + return NULL;
  22568. + return q;
  22569. +}
  22570. +
  22571. +static inline int put_task_cache(struct task_struct *t, struct sigqueue *q)
  22572. +{
  22573. + if (cmpxchg(&t->sigqueue_cache, NULL, q) == NULL)
  22574. + return 0;
  22575. + return 1;
  22576. +}
  22577. +
  22578. /*
  22579. * allocate a new signal queue record
  22580. * - this may be called without locks if and only if t == current, otherwise an
  22581. * appropriate lock must be held to stop the target task from exiting
  22582. */
  22583. static struct sigqueue *
  22584. -__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
  22585. +__sigqueue_do_alloc(int sig, struct task_struct *t, gfp_t flags,
  22586. + int override_rlimit, int fromslab)
  22587. {
  22588. struct sigqueue *q = NULL;
  22589. struct user_struct *user;
  22590. @@ -377,7 +395,10 @@ __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimi
  22591. if (override_rlimit ||
  22592. atomic_read(&user->sigpending) <=
  22593. task_rlimit(t, RLIMIT_SIGPENDING)) {
  22594. - q = kmem_cache_alloc(sigqueue_cachep, flags);
  22595. + if (!fromslab)
  22596. + q = get_task_cache(t);
  22597. + if (!q)
  22598. + q = kmem_cache_alloc(sigqueue_cachep, flags);
  22599. } else {
  22600. print_dropped_signal(sig);
  22601. }
  22602. @@ -394,6 +415,13 @@ __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimi
  22603. return q;
  22604. }
  22605. +static struct sigqueue *
  22606. +__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags,
  22607. + int override_rlimit)
  22608. +{
  22609. + return __sigqueue_do_alloc(sig, t, flags, override_rlimit, 0);
  22610. +}
  22611. +
  22612. static void __sigqueue_free(struct sigqueue *q)
  22613. {
  22614. if (q->flags & SIGQUEUE_PREALLOC)
  22615. @@ -403,6 +431,21 @@ static void __sigqueue_free(struct sigqueue *q)
  22616. kmem_cache_free(sigqueue_cachep, q);
  22617. }
  22618. +static void sigqueue_free_current(struct sigqueue *q)
  22619. +{
  22620. + struct user_struct *up;
  22621. +
  22622. + if (q->flags & SIGQUEUE_PREALLOC)
  22623. + return;
  22624. +
  22625. + up = q->user;
  22626. + if (rt_prio(current->normal_prio) && !put_task_cache(current, q)) {
  22627. + atomic_dec(&up->sigpending);
  22628. + free_uid(up);
  22629. + } else
  22630. + __sigqueue_free(q);
  22631. +}
  22632. +
  22633. void flush_sigqueue(struct sigpending *queue)
  22634. {
  22635. struct sigqueue *q;
  22636. @@ -415,6 +458,21 @@ void flush_sigqueue(struct sigpending *queue)
  22637. }
  22638. }
  22639. +/*
  22640. + * Called from __exit_signal. Flush tsk->pending and
  22641. + * tsk->sigqueue_cache
  22642. + */
  22643. +void flush_task_sigqueue(struct task_struct *tsk)
  22644. +{
  22645. + struct sigqueue *q;
  22646. +
  22647. + flush_sigqueue(&tsk->pending);
  22648. +
  22649. + q = get_task_cache(tsk);
  22650. + if (q)
  22651. + kmem_cache_free(sigqueue_cachep, q);
  22652. +}
  22653. +
  22654. /*
  22655. * Flush all pending signals for this kthread.
  22656. */
  22657. @@ -534,7 +592,7 @@ static void collect_signal(int sig, struct sigpending *list, siginfo_t *info,
  22658. (info->si_code == SI_TIMER) &&
  22659. (info->si_sys_private);
  22660. - __sigqueue_free(first);
  22661. + sigqueue_free_current(first);
  22662. } else {
  22663. /*
  22664. * Ok, it wasn't in the queue. This must be
  22665. @@ -570,6 +628,8 @@ int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
  22666. bool resched_timer = false;
  22667. int signr;
  22668. + WARN_ON_ONCE(tsk != current);
  22669. +
  22670. /* We only dequeue private signals from ourselves, we don't let
  22671. * signalfd steal them
  22672. */
  22673. @@ -1166,8 +1226,8 @@ int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
  22674. * We don't want to have recursive SIGSEGV's etc, for example,
  22675. * that is why we also clear SIGNAL_UNKILLABLE.
  22676. */
  22677. -int
  22678. -force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  22679. +static int
  22680. +do_force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  22681. {
  22682. unsigned long int flags;
  22683. int ret, blocked, ignored;
  22684. @@ -1192,6 +1252,39 @@ force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  22685. return ret;
  22686. }
  22687. +int force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  22688. +{
  22689. +/*
  22690. + * On some archs, PREEMPT_RT has to delay sending a signal from a trap
  22691. + * since it can not enable preemption, and the signal code's spin_locks
  22692. + * turn into mutexes. Instead, it must set TIF_NOTIFY_RESUME which will
  22693. + * send the signal on exit of the trap.
  22694. + */
  22695. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  22696. + if (in_atomic()) {
  22697. + if (WARN_ON_ONCE(t != current))
  22698. + return 0;
  22699. + if (WARN_ON_ONCE(t->forced_info.si_signo))
  22700. + return 0;
  22701. +
  22702. + if (is_si_special(info)) {
  22703. + WARN_ON_ONCE(info != SEND_SIG_PRIV);
  22704. + t->forced_info.si_signo = sig;
  22705. + t->forced_info.si_errno = 0;
  22706. + t->forced_info.si_code = SI_KERNEL;
  22707. + t->forced_info.si_pid = 0;
  22708. + t->forced_info.si_uid = 0;
  22709. + } else {
  22710. + t->forced_info = *info;
  22711. + }
  22712. +
  22713. + set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  22714. + return 0;
  22715. + }
  22716. +#endif
  22717. + return do_force_sig_info(sig, info, t);
  22718. +}
  22719. +
  22720. /*
  22721. * Nuke all other threads in the group.
  22722. */
  22723. @@ -1226,12 +1319,12 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
  22724. * Disable interrupts early to avoid deadlocks.
  22725. * See rcu_read_unlock() comment header for details.
  22726. */
  22727. - local_irq_save(*flags);
  22728. + local_irq_save_nort(*flags);
  22729. rcu_read_lock();
  22730. sighand = rcu_dereference(tsk->sighand);
  22731. if (unlikely(sighand == NULL)) {
  22732. rcu_read_unlock();
  22733. - local_irq_restore(*flags);
  22734. + local_irq_restore_nort(*flags);
  22735. break;
  22736. }
  22737. /*
  22738. @@ -1252,7 +1345,7 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
  22739. }
  22740. spin_unlock(&sighand->siglock);
  22741. rcu_read_unlock();
  22742. - local_irq_restore(*flags);
  22743. + local_irq_restore_nort(*flags);
  22744. }
  22745. return sighand;
  22746. @@ -1495,7 +1588,8 @@ EXPORT_SYMBOL(kill_pid);
  22747. */
  22748. struct sigqueue *sigqueue_alloc(void)
  22749. {
  22750. - struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
  22751. + /* Preallocated sigqueue objects always from the slabcache ! */
  22752. + struct sigqueue *q = __sigqueue_do_alloc(-1, current, GFP_KERNEL, 0, 1);
  22753. if (q)
  22754. q->flags |= SIGQUEUE_PREALLOC;
  22755. @@ -1856,15 +1950,7 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
  22756. if (gstop_done && ptrace_reparented(current))
  22757. do_notify_parent_cldstop(current, false, why);
  22758. - /*
  22759. - * Don't want to allow preemption here, because
  22760. - * sys_ptrace() needs this task to be inactive.
  22761. - *
  22762. - * XXX: implement read_unlock_no_resched().
  22763. - */
  22764. - preempt_disable();
  22765. read_unlock(&tasklist_lock);
  22766. - preempt_enable_no_resched();
  22767. freezable_schedule();
  22768. } else {
  22769. /*
  22770. diff --git a/kernel/softirq.c b/kernel/softirq.c
  22771. index 479e4436f787..cb9c1d5dee10 100644
  22772. --- a/kernel/softirq.c
  22773. +++ b/kernel/softirq.c
  22774. @@ -21,10 +21,12 @@
  22775. #include <linux/freezer.h>
  22776. #include <linux/kthread.h>
  22777. #include <linux/rcupdate.h>
  22778. +#include <linux/delay.h>
  22779. #include <linux/ftrace.h>
  22780. #include <linux/smp.h>
  22781. #include <linux/smpboot.h>
  22782. #include <linux/tick.h>
  22783. +#include <linux/locallock.h>
  22784. #include <linux/irq.h>
  22785. #define CREATE_TRACE_POINTS
  22786. @@ -56,12 +58,108 @@ EXPORT_SYMBOL(irq_stat);
  22787. static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
  22788. DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
  22789. +#ifdef CONFIG_PREEMPT_RT_FULL
  22790. +#define TIMER_SOFTIRQS ((1 << TIMER_SOFTIRQ) | (1 << HRTIMER_SOFTIRQ))
  22791. +DEFINE_PER_CPU(struct task_struct *, ktimer_softirqd);
  22792. +#endif
  22793. const char * const softirq_to_name[NR_SOFTIRQS] = {
  22794. "HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "BLOCK_IOPOLL",
  22795. "TASKLET", "SCHED", "HRTIMER", "RCU"
  22796. };
  22797. +#ifdef CONFIG_NO_HZ_COMMON
  22798. +# ifdef CONFIG_PREEMPT_RT_FULL
  22799. +
  22800. +struct softirq_runner {
  22801. + struct task_struct *runner[NR_SOFTIRQS];
  22802. +};
  22803. +
  22804. +static DEFINE_PER_CPU(struct softirq_runner, softirq_runners);
  22805. +
  22806. +static inline void softirq_set_runner(unsigned int sirq)
  22807. +{
  22808. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  22809. +
  22810. + sr->runner[sirq] = current;
  22811. +}
  22812. +
  22813. +static inline void softirq_clr_runner(unsigned int sirq)
  22814. +{
  22815. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  22816. +
  22817. + sr->runner[sirq] = NULL;
  22818. +}
  22819. +
  22820. +/*
  22821. + * On preempt-rt a softirq running context might be blocked on a
  22822. + * lock. There might be no other runnable task on this CPU because the
  22823. + * lock owner runs on some other CPU. So we have to go into idle with
  22824. + * the pending bit set. Therefor we need to check this otherwise we
  22825. + * warn about false positives which confuses users and defeats the
  22826. + * whole purpose of this test.
  22827. + *
  22828. + * This code is called with interrupts disabled.
  22829. + */
  22830. +void softirq_check_pending_idle(void)
  22831. +{
  22832. + static int rate_limit;
  22833. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  22834. + u32 warnpending;
  22835. + int i;
  22836. +
  22837. + if (rate_limit >= 10)
  22838. + return;
  22839. +
  22840. + warnpending = local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK;
  22841. + for (i = 0; i < NR_SOFTIRQS; i++) {
  22842. + struct task_struct *tsk = sr->runner[i];
  22843. +
  22844. + /*
  22845. + * The wakeup code in rtmutex.c wakes up the task
  22846. + * _before_ it sets pi_blocked_on to NULL under
  22847. + * tsk->pi_lock. So we need to check for both: state
  22848. + * and pi_blocked_on.
  22849. + */
  22850. + if (tsk) {
  22851. + raw_spin_lock(&tsk->pi_lock);
  22852. + if (tsk->pi_blocked_on || tsk->state == TASK_RUNNING) {
  22853. + /* Clear all bits pending in that task */
  22854. + warnpending &= ~(tsk->softirqs_raised);
  22855. + warnpending &= ~(1 << i);
  22856. + }
  22857. + raw_spin_unlock(&tsk->pi_lock);
  22858. + }
  22859. + }
  22860. +
  22861. + if (warnpending) {
  22862. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  22863. + warnpending);
  22864. + rate_limit++;
  22865. + }
  22866. +}
  22867. +# else
  22868. +/*
  22869. + * On !PREEMPT_RT we just printk rate limited:
  22870. + */
  22871. +void softirq_check_pending_idle(void)
  22872. +{
  22873. + static int rate_limit;
  22874. +
  22875. + if (rate_limit < 10 &&
  22876. + (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  22877. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  22878. + local_softirq_pending());
  22879. + rate_limit++;
  22880. + }
  22881. +}
  22882. +# endif
  22883. +
  22884. +#else /* !CONFIG_NO_HZ_COMMON */
  22885. +static inline void softirq_set_runner(unsigned int sirq) { }
  22886. +static inline void softirq_clr_runner(unsigned int sirq) { }
  22887. +#endif
  22888. +
  22889. /*
  22890. * we cannot loop indefinitely here to avoid userspace starvation,
  22891. * but we also don't want to introduce a worst case 1/HZ latency
  22892. @@ -77,6 +175,79 @@ static void wakeup_softirqd(void)
  22893. wake_up_process(tsk);
  22894. }
  22895. +#ifdef CONFIG_PREEMPT_RT_FULL
  22896. +static void wakeup_timer_softirqd(void)
  22897. +{
  22898. + /* Interrupts are disabled: no need to stop preemption */
  22899. + struct task_struct *tsk = __this_cpu_read(ktimer_softirqd);
  22900. +
  22901. + if (tsk && tsk->state != TASK_RUNNING)
  22902. + wake_up_process(tsk);
  22903. +}
  22904. +#endif
  22905. +
  22906. +static void handle_softirq(unsigned int vec_nr)
  22907. +{
  22908. + struct softirq_action *h = softirq_vec + vec_nr;
  22909. + int prev_count;
  22910. +
  22911. + prev_count = preempt_count();
  22912. +
  22913. + kstat_incr_softirqs_this_cpu(vec_nr);
  22914. +
  22915. + trace_softirq_entry(vec_nr);
  22916. + h->action(h);
  22917. + trace_softirq_exit(vec_nr);
  22918. + if (unlikely(prev_count != preempt_count())) {
  22919. + pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  22920. + vec_nr, softirq_to_name[vec_nr], h->action,
  22921. + prev_count, preempt_count());
  22922. + preempt_count_set(prev_count);
  22923. + }
  22924. +}
  22925. +
  22926. +#ifndef CONFIG_PREEMPT_RT_FULL
  22927. +static inline int ksoftirqd_softirq_pending(void)
  22928. +{
  22929. + return local_softirq_pending();
  22930. +}
  22931. +
  22932. +static void handle_pending_softirqs(u32 pending)
  22933. +{
  22934. + struct softirq_action *h = softirq_vec;
  22935. + int softirq_bit;
  22936. +
  22937. + local_irq_enable();
  22938. +
  22939. + h = softirq_vec;
  22940. +
  22941. + while ((softirq_bit = ffs(pending))) {
  22942. + unsigned int vec_nr;
  22943. +
  22944. + h += softirq_bit - 1;
  22945. + vec_nr = h - softirq_vec;
  22946. + handle_softirq(vec_nr);
  22947. +
  22948. + h++;
  22949. + pending >>= softirq_bit;
  22950. + }
  22951. +
  22952. + rcu_bh_qs();
  22953. + local_irq_disable();
  22954. +}
  22955. +
  22956. +static void run_ksoftirqd(unsigned int cpu)
  22957. +{
  22958. + local_irq_disable();
  22959. + if (ksoftirqd_softirq_pending()) {
  22960. + __do_softirq();
  22961. + local_irq_enable();
  22962. + cond_resched_rcu_qs();
  22963. + return;
  22964. + }
  22965. + local_irq_enable();
  22966. +}
  22967. +
  22968. /*
  22969. * preempt_count and SOFTIRQ_OFFSET usage:
  22970. * - preempt_count is changed by SOFTIRQ_OFFSET on entering or leaving
  22971. @@ -116,9 +287,9 @@ void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
  22972. if (preempt_count() == cnt) {
  22973. #ifdef CONFIG_DEBUG_PREEMPT
  22974. - current->preempt_disable_ip = get_parent_ip(CALLER_ADDR1);
  22975. + current->preempt_disable_ip = get_lock_parent_ip();
  22976. #endif
  22977. - trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  22978. + trace_preempt_off(CALLER_ADDR0, get_lock_parent_ip());
  22979. }
  22980. }
  22981. EXPORT_SYMBOL(__local_bh_disable_ip);
  22982. @@ -232,10 +403,8 @@ asmlinkage __visible void __do_softirq(void)
  22983. unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
  22984. unsigned long old_flags = current->flags;
  22985. int max_restart = MAX_SOFTIRQ_RESTART;
  22986. - struct softirq_action *h;
  22987. bool in_hardirq;
  22988. __u32 pending;
  22989. - int softirq_bit;
  22990. /*
  22991. * Mask out PF_MEMALLOC s current task context is borrowed for the
  22992. @@ -254,36 +423,7 @@ asmlinkage __visible void __do_softirq(void)
  22993. /* Reset the pending bitmask before enabling irqs */
  22994. set_softirq_pending(0);
  22995. - local_irq_enable();
  22996. -
  22997. - h = softirq_vec;
  22998. -
  22999. - while ((softirq_bit = ffs(pending))) {
  23000. - unsigned int vec_nr;
  23001. - int prev_count;
  23002. -
  23003. - h += softirq_bit - 1;
  23004. -
  23005. - vec_nr = h - softirq_vec;
  23006. - prev_count = preempt_count();
  23007. -
  23008. - kstat_incr_softirqs_this_cpu(vec_nr);
  23009. -
  23010. - trace_softirq_entry(vec_nr);
  23011. - h->action(h);
  23012. - trace_softirq_exit(vec_nr);
  23013. - if (unlikely(prev_count != preempt_count())) {
  23014. - pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  23015. - vec_nr, softirq_to_name[vec_nr], h->action,
  23016. - prev_count, preempt_count());
  23017. - preempt_count_set(prev_count);
  23018. - }
  23019. - h++;
  23020. - pending >>= softirq_bit;
  23021. - }
  23022. -
  23023. - rcu_bh_qs();
  23024. - local_irq_disable();
  23025. + handle_pending_softirqs(pending);
  23026. pending = local_softirq_pending();
  23027. if (pending) {
  23028. @@ -319,6 +459,310 @@ asmlinkage __visible void do_softirq(void)
  23029. local_irq_restore(flags);
  23030. }
  23031. +/*
  23032. + * This function must run with irqs disabled!
  23033. + */
  23034. +void raise_softirq_irqoff(unsigned int nr)
  23035. +{
  23036. + __raise_softirq_irqoff(nr);
  23037. +
  23038. + /*
  23039. + * If we're in an interrupt or softirq, we're done
  23040. + * (this also catches softirq-disabled code). We will
  23041. + * actually run the softirq once we return from
  23042. + * the irq or softirq.
  23043. + *
  23044. + * Otherwise we wake up ksoftirqd to make sure we
  23045. + * schedule the softirq soon.
  23046. + */
  23047. + if (!in_interrupt())
  23048. + wakeup_softirqd();
  23049. +}
  23050. +
  23051. +void __raise_softirq_irqoff(unsigned int nr)
  23052. +{
  23053. + trace_softirq_raise(nr);
  23054. + or_softirq_pending(1UL << nr);
  23055. +}
  23056. +
  23057. +static inline void local_bh_disable_nort(void) { local_bh_disable(); }
  23058. +static inline void _local_bh_enable_nort(void) { _local_bh_enable(); }
  23059. +static void ksoftirqd_set_sched_params(unsigned int cpu) { }
  23060. +
  23061. +#else /* !PREEMPT_RT_FULL */
  23062. +
  23063. +/*
  23064. + * On RT we serialize softirq execution with a cpu local lock per softirq
  23065. + */
  23066. +static DEFINE_PER_CPU(struct local_irq_lock [NR_SOFTIRQS], local_softirq_locks);
  23067. +
  23068. +void __init softirq_early_init(void)
  23069. +{
  23070. + int i;
  23071. +
  23072. + for (i = 0; i < NR_SOFTIRQS; i++)
  23073. + local_irq_lock_init(local_softirq_locks[i]);
  23074. +}
  23075. +
  23076. +static void lock_softirq(int which)
  23077. +{
  23078. + local_lock(local_softirq_locks[which]);
  23079. +}
  23080. +
  23081. +static void unlock_softirq(int which)
  23082. +{
  23083. + local_unlock(local_softirq_locks[which]);
  23084. +}
  23085. +
  23086. +static void do_single_softirq(int which)
  23087. +{
  23088. + unsigned long old_flags = current->flags;
  23089. +
  23090. + current->flags &= ~PF_MEMALLOC;
  23091. + vtime_account_irq_enter(current);
  23092. + current->flags |= PF_IN_SOFTIRQ;
  23093. + lockdep_softirq_enter();
  23094. + local_irq_enable();
  23095. + handle_softirq(which);
  23096. + local_irq_disable();
  23097. + lockdep_softirq_exit();
  23098. + current->flags &= ~PF_IN_SOFTIRQ;
  23099. + vtime_account_irq_enter(current);
  23100. + tsk_restore_flags(current, old_flags, PF_MEMALLOC);
  23101. +}
  23102. +
  23103. +/*
  23104. + * Called with interrupts disabled. Process softirqs which were raised
  23105. + * in current context (or on behalf of ksoftirqd).
  23106. + */
  23107. +static void do_current_softirqs(void)
  23108. +{
  23109. + while (current->softirqs_raised) {
  23110. + int i = __ffs(current->softirqs_raised);
  23111. + unsigned int pending, mask = (1U << i);
  23112. +
  23113. + current->softirqs_raised &= ~mask;
  23114. + local_irq_enable();
  23115. +
  23116. + /*
  23117. + * If the lock is contended, we boost the owner to
  23118. + * process the softirq or leave the critical section
  23119. + * now.
  23120. + */
  23121. + lock_softirq(i);
  23122. + local_irq_disable();
  23123. + softirq_set_runner(i);
  23124. + /*
  23125. + * Check with the local_softirq_pending() bits,
  23126. + * whether we need to process this still or if someone
  23127. + * else took care of it.
  23128. + */
  23129. + pending = local_softirq_pending();
  23130. + if (pending & mask) {
  23131. + set_softirq_pending(pending & ~mask);
  23132. + do_single_softirq(i);
  23133. + }
  23134. + softirq_clr_runner(i);
  23135. + WARN_ON(current->softirq_nestcnt != 1);
  23136. + local_irq_enable();
  23137. + unlock_softirq(i);
  23138. + local_irq_disable();
  23139. + }
  23140. +}
  23141. +
  23142. +void __local_bh_disable(void)
  23143. +{
  23144. + if (++current->softirq_nestcnt == 1)
  23145. + migrate_disable();
  23146. +}
  23147. +EXPORT_SYMBOL(__local_bh_disable);
  23148. +
  23149. +void __local_bh_enable(void)
  23150. +{
  23151. + if (WARN_ON(current->softirq_nestcnt == 0))
  23152. + return;
  23153. +
  23154. + local_irq_disable();
  23155. + if (current->softirq_nestcnt == 1 && current->softirqs_raised)
  23156. + do_current_softirqs();
  23157. + local_irq_enable();
  23158. +
  23159. + if (--current->softirq_nestcnt == 0)
  23160. + migrate_enable();
  23161. +}
  23162. +EXPORT_SYMBOL(__local_bh_enable);
  23163. +
  23164. +void _local_bh_enable(void)
  23165. +{
  23166. + if (WARN_ON(current->softirq_nestcnt == 0))
  23167. + return;
  23168. + if (--current->softirq_nestcnt == 0)
  23169. + migrate_enable();
  23170. +}
  23171. +EXPORT_SYMBOL(_local_bh_enable);
  23172. +
  23173. +int in_serving_softirq(void)
  23174. +{
  23175. + return current->flags & PF_IN_SOFTIRQ;
  23176. +}
  23177. +EXPORT_SYMBOL(in_serving_softirq);
  23178. +
  23179. +/* Called with preemption disabled */
  23180. +static void run_ksoftirqd(unsigned int cpu)
  23181. +{
  23182. + local_irq_disable();
  23183. + current->softirq_nestcnt++;
  23184. +
  23185. + do_current_softirqs();
  23186. + current->softirq_nestcnt--;
  23187. + local_irq_enable();
  23188. + cond_resched_rcu_qs();
  23189. +}
  23190. +
  23191. +/*
  23192. + * Called from netif_rx_ni(). Preemption enabled, but migration
  23193. + * disabled. So the cpu can't go away under us.
  23194. + */
  23195. +void thread_do_softirq(void)
  23196. +{
  23197. + if (!in_serving_softirq() && current->softirqs_raised) {
  23198. + current->softirq_nestcnt++;
  23199. + do_current_softirqs();
  23200. + current->softirq_nestcnt--;
  23201. + }
  23202. +}
  23203. +
  23204. +static void do_raise_softirq_irqoff(unsigned int nr)
  23205. +{
  23206. + unsigned int mask;
  23207. +
  23208. + mask = 1UL << nr;
  23209. +
  23210. + trace_softirq_raise(nr);
  23211. + or_softirq_pending(mask);
  23212. +
  23213. + /*
  23214. + * If we are not in a hard interrupt and inside a bh disabled
  23215. + * region, we simply raise the flag on current. local_bh_enable()
  23216. + * will make sure that the softirq is executed. Otherwise we
  23217. + * delegate it to ksoftirqd.
  23218. + */
  23219. + if (!in_irq() && current->softirq_nestcnt)
  23220. + current->softirqs_raised |= mask;
  23221. + else if (!__this_cpu_read(ksoftirqd) || !__this_cpu_read(ktimer_softirqd))
  23222. + return;
  23223. +
  23224. + if (mask & TIMER_SOFTIRQS)
  23225. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  23226. + else
  23227. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  23228. +}
  23229. +
  23230. +static void wakeup_proper_softirq(unsigned int nr)
  23231. +{
  23232. + if ((1UL << nr) & TIMER_SOFTIRQS)
  23233. + wakeup_timer_softirqd();
  23234. + else
  23235. + wakeup_softirqd();
  23236. +}
  23237. +
  23238. +
  23239. +void __raise_softirq_irqoff(unsigned int nr)
  23240. +{
  23241. + do_raise_softirq_irqoff(nr);
  23242. + if (!in_irq() && !current->softirq_nestcnt)
  23243. + wakeup_proper_softirq(nr);
  23244. +}
  23245. +
  23246. +/*
  23247. + * Same as __raise_softirq_irqoff() but will process them in ksoftirqd
  23248. + */
  23249. +void __raise_softirq_irqoff_ksoft(unsigned int nr)
  23250. +{
  23251. + unsigned int mask;
  23252. +
  23253. + if (WARN_ON_ONCE(!__this_cpu_read(ksoftirqd) ||
  23254. + !__this_cpu_read(ktimer_softirqd)))
  23255. + return;
  23256. + mask = 1UL << nr;
  23257. +
  23258. + trace_softirq_raise(nr);
  23259. + or_softirq_pending(mask);
  23260. + if (mask & TIMER_SOFTIRQS)
  23261. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  23262. + else
  23263. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  23264. + wakeup_proper_softirq(nr);
  23265. +}
  23266. +
  23267. +/*
  23268. + * This function must run with irqs disabled!
  23269. + */
  23270. +void raise_softirq_irqoff(unsigned int nr)
  23271. +{
  23272. + do_raise_softirq_irqoff(nr);
  23273. +
  23274. + /*
  23275. + * If we're in an hard interrupt we let irq return code deal
  23276. + * with the wakeup of ksoftirqd.
  23277. + */
  23278. + if (in_irq())
  23279. + return;
  23280. + /*
  23281. + * If we are in thread context but outside of a bh disabled
  23282. + * region, we need to wake ksoftirqd as well.
  23283. + *
  23284. + * CHECKME: Some of the places which do that could be wrapped
  23285. + * into local_bh_disable/enable pairs. Though it's unclear
  23286. + * whether this is worth the effort. To find those places just
  23287. + * raise a WARN() if the condition is met.
  23288. + */
  23289. + if (!current->softirq_nestcnt)
  23290. + wakeup_proper_softirq(nr);
  23291. +}
  23292. +
  23293. +static inline int ksoftirqd_softirq_pending(void)
  23294. +{
  23295. + return current->softirqs_raised;
  23296. +}
  23297. +
  23298. +static inline void local_bh_disable_nort(void) { }
  23299. +static inline void _local_bh_enable_nort(void) { }
  23300. +
  23301. +static inline void ksoftirqd_set_sched_params(unsigned int cpu)
  23302. +{
  23303. + /* Take over all but timer pending softirqs when starting */
  23304. + local_irq_disable();
  23305. + current->softirqs_raised = local_softirq_pending() & ~TIMER_SOFTIRQS;
  23306. + local_irq_enable();
  23307. +}
  23308. +
  23309. +static inline void ktimer_softirqd_set_sched_params(unsigned int cpu)
  23310. +{
  23311. + struct sched_param param = { .sched_priority = 1 };
  23312. +
  23313. + sched_setscheduler(current, SCHED_FIFO, &param);
  23314. +
  23315. + /* Take over timer pending softirqs when starting */
  23316. + local_irq_disable();
  23317. + current->softirqs_raised = local_softirq_pending() & TIMER_SOFTIRQS;
  23318. + local_irq_enable();
  23319. +}
  23320. +
  23321. +static inline void ktimer_softirqd_clr_sched_params(unsigned int cpu,
  23322. + bool online)
  23323. +{
  23324. + struct sched_param param = { .sched_priority = 0 };
  23325. +
  23326. + sched_setscheduler(current, SCHED_NORMAL, &param);
  23327. +}
  23328. +
  23329. +static int ktimer_softirqd_should_run(unsigned int cpu)
  23330. +{
  23331. + return current->softirqs_raised;
  23332. +}
  23333. +
  23334. +#endif /* PREEMPT_RT_FULL */
  23335. /*
  23336. * Enter an interrupt context.
  23337. */
  23338. @@ -330,9 +774,9 @@ void irq_enter(void)
  23339. * Prevent raise_softirq from needlessly waking up ksoftirqd
  23340. * here, as softirq will be serviced on return from interrupt.
  23341. */
  23342. - local_bh_disable();
  23343. + local_bh_disable_nort();
  23344. tick_irq_enter();
  23345. - _local_bh_enable();
  23346. + _local_bh_enable_nort();
  23347. }
  23348. __irq_enter();
  23349. @@ -340,6 +784,7 @@ void irq_enter(void)
  23350. static inline void invoke_softirq(void)
  23351. {
  23352. +#ifndef CONFIG_PREEMPT_RT_FULL
  23353. if (!force_irqthreads) {
  23354. #ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
  23355. /*
  23356. @@ -359,6 +804,18 @@ static inline void invoke_softirq(void)
  23357. } else {
  23358. wakeup_softirqd();
  23359. }
  23360. +#else /* PREEMPT_RT_FULL */
  23361. + unsigned long flags;
  23362. +
  23363. + local_irq_save(flags);
  23364. + if (__this_cpu_read(ksoftirqd) &&
  23365. + __this_cpu_read(ksoftirqd)->softirqs_raised)
  23366. + wakeup_softirqd();
  23367. + if (__this_cpu_read(ktimer_softirqd) &&
  23368. + __this_cpu_read(ktimer_softirqd)->softirqs_raised)
  23369. + wakeup_timer_softirqd();
  23370. + local_irq_restore(flags);
  23371. +#endif
  23372. }
  23373. static inline void tick_irq_exit(void)
  23374. @@ -395,26 +852,6 @@ void irq_exit(void)
  23375. trace_hardirq_exit(); /* must be last! */
  23376. }
  23377. -/*
  23378. - * This function must run with irqs disabled!
  23379. - */
  23380. -inline void raise_softirq_irqoff(unsigned int nr)
  23381. -{
  23382. - __raise_softirq_irqoff(nr);
  23383. -
  23384. - /*
  23385. - * If we're in an interrupt or softirq, we're done
  23386. - * (this also catches softirq-disabled code). We will
  23387. - * actually run the softirq once we return from
  23388. - * the irq or softirq.
  23389. - *
  23390. - * Otherwise we wake up ksoftirqd to make sure we
  23391. - * schedule the softirq soon.
  23392. - */
  23393. - if (!in_interrupt())
  23394. - wakeup_softirqd();
  23395. -}
  23396. -
  23397. void raise_softirq(unsigned int nr)
  23398. {
  23399. unsigned long flags;
  23400. @@ -424,12 +861,6 @@ void raise_softirq(unsigned int nr)
  23401. local_irq_restore(flags);
  23402. }
  23403. -void __raise_softirq_irqoff(unsigned int nr)
  23404. -{
  23405. - trace_softirq_raise(nr);
  23406. - or_softirq_pending(1UL << nr);
  23407. -}
  23408. -
  23409. void open_softirq(int nr, void (*action)(struct softirq_action *))
  23410. {
  23411. softirq_vec[nr].action = action;
  23412. @@ -446,15 +877,45 @@ struct tasklet_head {
  23413. static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
  23414. static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
  23415. +static void inline
  23416. +__tasklet_common_schedule(struct tasklet_struct *t, struct tasklet_head *head, unsigned int nr)
  23417. +{
  23418. + if (tasklet_trylock(t)) {
  23419. +again:
  23420. + /* We may have been preempted before tasklet_trylock
  23421. + * and __tasklet_action may have already run.
  23422. + * So double check the sched bit while the takslet
  23423. + * is locked before adding it to the list.
  23424. + */
  23425. + if (test_bit(TASKLET_STATE_SCHED, &t->state)) {
  23426. + t->next = NULL;
  23427. + *head->tail = t;
  23428. + head->tail = &(t->next);
  23429. + raise_softirq_irqoff(nr);
  23430. + tasklet_unlock(t);
  23431. + } else {
  23432. + /* This is subtle. If we hit the corner case above
  23433. + * It is possible that we get preempted right here,
  23434. + * and another task has successfully called
  23435. + * tasklet_schedule(), then this function, and
  23436. + * failed on the trylock. Thus we must be sure
  23437. + * before releasing the tasklet lock, that the
  23438. + * SCHED_BIT is clear. Otherwise the tasklet
  23439. + * may get its SCHED_BIT set, but not added to the
  23440. + * list
  23441. + */
  23442. + if (!tasklet_tryunlock(t))
  23443. + goto again;
  23444. + }
  23445. + }
  23446. +}
  23447. +
  23448. void __tasklet_schedule(struct tasklet_struct *t)
  23449. {
  23450. unsigned long flags;
  23451. local_irq_save(flags);
  23452. - t->next = NULL;
  23453. - *__this_cpu_read(tasklet_vec.tail) = t;
  23454. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  23455. - raise_softirq_irqoff(TASKLET_SOFTIRQ);
  23456. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
  23457. local_irq_restore(flags);
  23458. }
  23459. EXPORT_SYMBOL(__tasklet_schedule);
  23460. @@ -464,10 +925,7 @@ void __tasklet_hi_schedule(struct tasklet_struct *t)
  23461. unsigned long flags;
  23462. local_irq_save(flags);
  23463. - t->next = NULL;
  23464. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  23465. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  23466. - raise_softirq_irqoff(HI_SOFTIRQ);
  23467. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
  23468. local_irq_restore(flags);
  23469. }
  23470. EXPORT_SYMBOL(__tasklet_hi_schedule);
  23471. @@ -476,82 +934,122 @@ void __tasklet_hi_schedule_first(struct tasklet_struct *t)
  23472. {
  23473. BUG_ON(!irqs_disabled());
  23474. - t->next = __this_cpu_read(tasklet_hi_vec.head);
  23475. - __this_cpu_write(tasklet_hi_vec.head, t);
  23476. - __raise_softirq_irqoff(HI_SOFTIRQ);
  23477. + __tasklet_hi_schedule(t);
  23478. }
  23479. EXPORT_SYMBOL(__tasklet_hi_schedule_first);
  23480. -static void tasklet_action(struct softirq_action *a)
  23481. +void tasklet_enable(struct tasklet_struct *t)
  23482. {
  23483. - struct tasklet_struct *list;
  23484. + if (!atomic_dec_and_test(&t->count))
  23485. + return;
  23486. + if (test_and_clear_bit(TASKLET_STATE_PENDING, &t->state))
  23487. + tasklet_schedule(t);
  23488. +}
  23489. +EXPORT_SYMBOL(tasklet_enable);
  23490. - local_irq_disable();
  23491. - list = __this_cpu_read(tasklet_vec.head);
  23492. - __this_cpu_write(tasklet_vec.head, NULL);
  23493. - __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  23494. - local_irq_enable();
  23495. +static void __tasklet_action(struct softirq_action *a,
  23496. + struct tasklet_struct *list)
  23497. +{
  23498. + int loops = 1000000;
  23499. while (list) {
  23500. struct tasklet_struct *t = list;
  23501. list = list->next;
  23502. - if (tasklet_trylock(t)) {
  23503. - if (!atomic_read(&t->count)) {
  23504. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  23505. - &t->state))
  23506. - BUG();
  23507. - t->func(t->data);
  23508. - tasklet_unlock(t);
  23509. - continue;
  23510. - }
  23511. - tasklet_unlock(t);
  23512. + /*
  23513. + * Should always succeed - after a tasklist got on the
  23514. + * list (after getting the SCHED bit set from 0 to 1),
  23515. + * nothing but the tasklet softirq it got queued to can
  23516. + * lock it:
  23517. + */
  23518. + if (!tasklet_trylock(t)) {
  23519. + WARN_ON(1);
  23520. + continue;
  23521. }
  23522. - local_irq_disable();
  23523. t->next = NULL;
  23524. - *__this_cpu_read(tasklet_vec.tail) = t;
  23525. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  23526. - __raise_softirq_irqoff(TASKLET_SOFTIRQ);
  23527. - local_irq_enable();
  23528. +
  23529. + /*
  23530. + * If we cannot handle the tasklet because it's disabled,
  23531. + * mark it as pending. tasklet_enable() will later
  23532. + * re-schedule the tasklet.
  23533. + */
  23534. + if (unlikely(atomic_read(&t->count))) {
  23535. +out_disabled:
  23536. + /* implicit unlock: */
  23537. + wmb();
  23538. + t->state = TASKLET_STATEF_PENDING;
  23539. + continue;
  23540. + }
  23541. +
  23542. + /*
  23543. + * After this point on the tasklet might be rescheduled
  23544. + * on another CPU, but it can only be added to another
  23545. + * CPU's tasklet list if we unlock the tasklet (which we
  23546. + * dont do yet).
  23547. + */
  23548. + if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  23549. + WARN_ON(1);
  23550. +
  23551. +again:
  23552. + t->func(t->data);
  23553. +
  23554. + /*
  23555. + * Try to unlock the tasklet. We must use cmpxchg, because
  23556. + * another CPU might have scheduled or disabled the tasklet.
  23557. + * We only allow the STATE_RUN -> 0 transition here.
  23558. + */
  23559. + while (!tasklet_tryunlock(t)) {
  23560. + /*
  23561. + * If it got disabled meanwhile, bail out:
  23562. + */
  23563. + if (atomic_read(&t->count))
  23564. + goto out_disabled;
  23565. + /*
  23566. + * If it got scheduled meanwhile, re-execute
  23567. + * the tasklet function:
  23568. + */
  23569. + if (test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  23570. + goto again;
  23571. + if (!--loops) {
  23572. + printk("hm, tasklet state: %08lx\n", t->state);
  23573. + WARN_ON(1);
  23574. + tasklet_unlock(t);
  23575. + break;
  23576. + }
  23577. + }
  23578. }
  23579. }
  23580. +static void tasklet_action(struct softirq_action *a)
  23581. +{
  23582. + struct tasklet_struct *list;
  23583. +
  23584. + local_irq_disable();
  23585. +
  23586. + list = __this_cpu_read(tasklet_vec.head);
  23587. + __this_cpu_write(tasklet_vec.head, NULL);
  23588. + __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  23589. +
  23590. + local_irq_enable();
  23591. +
  23592. + __tasklet_action(a, list);
  23593. +}
  23594. +
  23595. static void tasklet_hi_action(struct softirq_action *a)
  23596. {
  23597. struct tasklet_struct *list;
  23598. local_irq_disable();
  23599. +
  23600. list = __this_cpu_read(tasklet_hi_vec.head);
  23601. __this_cpu_write(tasklet_hi_vec.head, NULL);
  23602. __this_cpu_write(tasklet_hi_vec.tail, this_cpu_ptr(&tasklet_hi_vec.head));
  23603. - local_irq_enable();
  23604. - while (list) {
  23605. - struct tasklet_struct *t = list;
  23606. -
  23607. - list = list->next;
  23608. -
  23609. - if (tasklet_trylock(t)) {
  23610. - if (!atomic_read(&t->count)) {
  23611. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  23612. - &t->state))
  23613. - BUG();
  23614. - t->func(t->data);
  23615. - tasklet_unlock(t);
  23616. - continue;
  23617. - }
  23618. - tasklet_unlock(t);
  23619. - }
  23620. + local_irq_enable();
  23621. - local_irq_disable();
  23622. - t->next = NULL;
  23623. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  23624. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  23625. - __raise_softirq_irqoff(HI_SOFTIRQ);
  23626. - local_irq_enable();
  23627. - }
  23628. + __tasklet_action(a, list);
  23629. }
  23630. void tasklet_init(struct tasklet_struct *t,
  23631. @@ -572,7 +1070,7 @@ void tasklet_kill(struct tasklet_struct *t)
  23632. while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
  23633. do {
  23634. - yield();
  23635. + msleep(1);
  23636. } while (test_bit(TASKLET_STATE_SCHED, &t->state));
  23637. }
  23638. tasklet_unlock_wait(t);
  23639. @@ -646,25 +1144,26 @@ void __init softirq_init(void)
  23640. open_softirq(HI_SOFTIRQ, tasklet_hi_action);
  23641. }
  23642. -static int ksoftirqd_should_run(unsigned int cpu)
  23643. -{
  23644. - return local_softirq_pending();
  23645. -}
  23646. -
  23647. -static void run_ksoftirqd(unsigned int cpu)
  23648. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  23649. +void tasklet_unlock_wait(struct tasklet_struct *t)
  23650. {
  23651. - local_irq_disable();
  23652. - if (local_softirq_pending()) {
  23653. + while (test_bit(TASKLET_STATE_RUN, &(t)->state)) {
  23654. /*
  23655. - * We can safely run softirq on inline stack, as we are not deep
  23656. - * in the task stack here.
  23657. + * Hack for now to avoid this busy-loop:
  23658. */
  23659. - __do_softirq();
  23660. - local_irq_enable();
  23661. - cond_resched_rcu_qs();
  23662. - return;
  23663. +#ifdef CONFIG_PREEMPT_RT_FULL
  23664. + msleep(1);
  23665. +#else
  23666. + barrier();
  23667. +#endif
  23668. }
  23669. - local_irq_enable();
  23670. +}
  23671. +EXPORT_SYMBOL(tasklet_unlock_wait);
  23672. +#endif
  23673. +
  23674. +static int ksoftirqd_should_run(unsigned int cpu)
  23675. +{
  23676. + return ksoftirqd_softirq_pending();
  23677. }
  23678. #ifdef CONFIG_HOTPLUG_CPU
  23679. @@ -746,16 +1245,31 @@ static struct notifier_block cpu_nfb = {
  23680. static struct smp_hotplug_thread softirq_threads = {
  23681. .store = &ksoftirqd,
  23682. + .setup = ksoftirqd_set_sched_params,
  23683. .thread_should_run = ksoftirqd_should_run,
  23684. .thread_fn = run_ksoftirqd,
  23685. .thread_comm = "ksoftirqd/%u",
  23686. };
  23687. +#ifdef CONFIG_PREEMPT_RT_FULL
  23688. +static struct smp_hotplug_thread softirq_timer_threads = {
  23689. + .store = &ktimer_softirqd,
  23690. + .setup = ktimer_softirqd_set_sched_params,
  23691. + .cleanup = ktimer_softirqd_clr_sched_params,
  23692. + .thread_should_run = ktimer_softirqd_should_run,
  23693. + .thread_fn = run_ksoftirqd,
  23694. + .thread_comm = "ktimersoftd/%u",
  23695. +};
  23696. +#endif
  23697. +
  23698. static __init int spawn_ksoftirqd(void)
  23699. {
  23700. register_cpu_notifier(&cpu_nfb);
  23701. BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
  23702. +#ifdef CONFIG_PREEMPT_RT_FULL
  23703. + BUG_ON(smpboot_register_percpu_thread(&softirq_timer_threads));
  23704. +#endif
  23705. return 0;
  23706. }
  23707. diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
  23708. index a3bbaee77c58..f84d3b45cda7 100644
  23709. --- a/kernel/stop_machine.c
  23710. +++ b/kernel/stop_machine.c
  23711. @@ -37,7 +37,7 @@ struct cpu_stop_done {
  23712. struct cpu_stopper {
  23713. struct task_struct *thread;
  23714. - spinlock_t lock;
  23715. + raw_spinlock_t lock;
  23716. bool enabled; /* is this stopper enabled? */
  23717. struct list_head works; /* list of pending works */
  23718. @@ -86,12 +86,12 @@ static void cpu_stop_queue_work(unsigned int cpu, struct cpu_stop_work *work)
  23719. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  23720. unsigned long flags;
  23721. - spin_lock_irqsave(&stopper->lock, flags);
  23722. + raw_spin_lock_irqsave(&stopper->lock, flags);
  23723. if (stopper->enabled)
  23724. __cpu_stop_queue_work(stopper, work);
  23725. else
  23726. cpu_stop_signal_done(work->done, false);
  23727. - spin_unlock_irqrestore(&stopper->lock, flags);
  23728. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  23729. }
  23730. /**
  23731. @@ -224,8 +224,8 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
  23732. int err;
  23733. lg_double_lock(&stop_cpus_lock, cpu1, cpu2);
  23734. - spin_lock_irq(&stopper1->lock);
  23735. - spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  23736. + raw_spin_lock_irq(&stopper1->lock);
  23737. + raw_spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  23738. err = -ENOENT;
  23739. if (!stopper1->enabled || !stopper2->enabled)
  23740. @@ -235,8 +235,8 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
  23741. __cpu_stop_queue_work(stopper1, work1);
  23742. __cpu_stop_queue_work(stopper2, work2);
  23743. unlock:
  23744. - spin_unlock(&stopper2->lock);
  23745. - spin_unlock_irq(&stopper1->lock);
  23746. + raw_spin_unlock(&stopper2->lock);
  23747. + raw_spin_unlock_irq(&stopper1->lock);
  23748. lg_double_unlock(&stop_cpus_lock, cpu1, cpu2);
  23749. return err;
  23750. @@ -258,7 +258,7 @@ int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *
  23751. struct cpu_stop_work work1, work2;
  23752. struct multi_stop_data msdata;
  23753. - preempt_disable();
  23754. + preempt_disable_nort();
  23755. msdata = (struct multi_stop_data){
  23756. .fn = fn,
  23757. .data = arg,
  23758. @@ -278,11 +278,11 @@ int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *
  23759. if (cpu1 > cpu2)
  23760. swap(cpu1, cpu2);
  23761. if (cpu_stop_queue_two_works(cpu1, &work1, cpu2, &work2)) {
  23762. - preempt_enable();
  23763. + preempt_enable_nort();
  23764. return -ENOENT;
  23765. }
  23766. - preempt_enable();
  23767. + preempt_enable_nort();
  23768. wait_for_completion(&done.completion);
  23769. @@ -315,17 +315,20 @@ static DEFINE_MUTEX(stop_cpus_mutex);
  23770. static void queue_stop_cpus_work(const struct cpumask *cpumask,
  23771. cpu_stop_fn_t fn, void *arg,
  23772. - struct cpu_stop_done *done)
  23773. + struct cpu_stop_done *done, bool inactive)
  23774. {
  23775. struct cpu_stop_work *work;
  23776. unsigned int cpu;
  23777. /*
  23778. - * Disable preemption while queueing to avoid getting
  23779. - * preempted by a stopper which might wait for other stoppers
  23780. - * to enter @fn which can lead to deadlock.
  23781. + * Make sure that all work is queued on all cpus before
  23782. + * any of the cpus can execute it.
  23783. */
  23784. - lg_global_lock(&stop_cpus_lock);
  23785. + if (!inactive)
  23786. + lg_global_lock(&stop_cpus_lock);
  23787. + else
  23788. + lg_global_trylock_relax(&stop_cpus_lock);
  23789. +
  23790. for_each_cpu(cpu, cpumask) {
  23791. work = &per_cpu(cpu_stopper.stop_work, cpu);
  23792. work->fn = fn;
  23793. @@ -342,7 +345,7 @@ static int __stop_cpus(const struct cpumask *cpumask,
  23794. struct cpu_stop_done done;
  23795. cpu_stop_init_done(&done, cpumask_weight(cpumask));
  23796. - queue_stop_cpus_work(cpumask, fn, arg, &done);
  23797. + queue_stop_cpus_work(cpumask, fn, arg, &done, false);
  23798. wait_for_completion(&done.completion);
  23799. return done.executed ? done.ret : -ENOENT;
  23800. }
  23801. @@ -422,9 +425,9 @@ static int cpu_stop_should_run(unsigned int cpu)
  23802. unsigned long flags;
  23803. int run;
  23804. - spin_lock_irqsave(&stopper->lock, flags);
  23805. + raw_spin_lock_irqsave(&stopper->lock, flags);
  23806. run = !list_empty(&stopper->works);
  23807. - spin_unlock_irqrestore(&stopper->lock, flags);
  23808. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  23809. return run;
  23810. }
  23811. @@ -436,13 +439,13 @@ static void cpu_stopper_thread(unsigned int cpu)
  23812. repeat:
  23813. work = NULL;
  23814. - spin_lock_irq(&stopper->lock);
  23815. + raw_spin_lock_irq(&stopper->lock);
  23816. if (!list_empty(&stopper->works)) {
  23817. work = list_first_entry(&stopper->works,
  23818. struct cpu_stop_work, list);
  23819. list_del_init(&work->list);
  23820. }
  23821. - spin_unlock_irq(&stopper->lock);
  23822. + raw_spin_unlock_irq(&stopper->lock);
  23823. if (work) {
  23824. cpu_stop_fn_t fn = work->fn;
  23825. @@ -450,6 +453,16 @@ static void cpu_stopper_thread(unsigned int cpu)
  23826. struct cpu_stop_done *done = work->done;
  23827. char ksym_buf[KSYM_NAME_LEN] __maybe_unused;
  23828. + /*
  23829. + * Wait until the stopper finished scheduling on all
  23830. + * cpus
  23831. + */
  23832. + lg_global_lock(&stop_cpus_lock);
  23833. + /*
  23834. + * Let other cpu threads continue as well
  23835. + */
  23836. + lg_global_unlock(&stop_cpus_lock);
  23837. +
  23838. /* cpu stop callbacks are not allowed to sleep */
  23839. preempt_disable();
  23840. @@ -520,10 +533,12 @@ static int __init cpu_stop_init(void)
  23841. for_each_possible_cpu(cpu) {
  23842. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  23843. - spin_lock_init(&stopper->lock);
  23844. + raw_spin_lock_init(&stopper->lock);
  23845. INIT_LIST_HEAD(&stopper->works);
  23846. }
  23847. + lg_lock_init(&stop_cpus_lock, "stop_cpus_lock");
  23848. +
  23849. BUG_ON(smpboot_register_percpu_thread(&cpu_stop_threads));
  23850. stop_machine_unpark(raw_smp_processor_id());
  23851. stop_machine_initialized = true;
  23852. @@ -620,7 +635,7 @@ int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
  23853. set_state(&msdata, MULTI_STOP_PREPARE);
  23854. cpu_stop_init_done(&done, num_active_cpus());
  23855. queue_stop_cpus_work(cpu_active_mask, multi_cpu_stop, &msdata,
  23856. - &done);
  23857. + &done, true);
  23858. ret = multi_cpu_stop(&msdata);
  23859. /* Busy wait for completion. */
  23860. diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
  23861. index 17f7bcff1e02..120fc8932165 100644
  23862. --- a/kernel/time/hrtimer.c
  23863. +++ b/kernel/time/hrtimer.c
  23864. @@ -48,11 +48,13 @@
  23865. #include <linux/sched/rt.h>
  23866. #include <linux/sched/deadline.h>
  23867. #include <linux/timer.h>
  23868. +#include <linux/kthread.h>
  23869. #include <linux/freezer.h>
  23870. #include <asm/uaccess.h>
  23871. #include <trace/events/timer.h>
  23872. +#include <trace/events/hist.h>
  23873. #include "tick-internal.h"
  23874. @@ -717,6 +719,44 @@ static void clock_was_set_work(struct work_struct *work)
  23875. static DECLARE_WORK(hrtimer_work, clock_was_set_work);
  23876. +#ifdef CONFIG_PREEMPT_RT_FULL
  23877. +/*
  23878. + * RT can not call schedule_work from real interrupt context.
  23879. + * Need to make a thread to do the real work.
  23880. + */
  23881. +static struct task_struct *clock_set_delay_thread;
  23882. +static bool do_clock_set_delay;
  23883. +
  23884. +static int run_clock_set_delay(void *ignore)
  23885. +{
  23886. + while (!kthread_should_stop()) {
  23887. + set_current_state(TASK_INTERRUPTIBLE);
  23888. + if (do_clock_set_delay) {
  23889. + do_clock_set_delay = false;
  23890. + schedule_work(&hrtimer_work);
  23891. + }
  23892. + schedule();
  23893. + }
  23894. + __set_current_state(TASK_RUNNING);
  23895. + return 0;
  23896. +}
  23897. +
  23898. +void clock_was_set_delayed(void)
  23899. +{
  23900. + do_clock_set_delay = true;
  23901. + /* Make visible before waking up process */
  23902. + smp_wmb();
  23903. + wake_up_process(clock_set_delay_thread);
  23904. +}
  23905. +
  23906. +static __init int create_clock_set_delay_thread(void)
  23907. +{
  23908. + clock_set_delay_thread = kthread_run(run_clock_set_delay, NULL, "kclksetdelayd");
  23909. + BUG_ON(!clock_set_delay_thread);
  23910. + return 0;
  23911. +}
  23912. +early_initcall(create_clock_set_delay_thread);
  23913. +#else /* PREEMPT_RT_FULL */
  23914. /*
  23915. * Called from timekeeping and resume code to reprogramm the hrtimer
  23916. * interrupt device on all cpus.
  23917. @@ -725,6 +765,7 @@ void clock_was_set_delayed(void)
  23918. {
  23919. schedule_work(&hrtimer_work);
  23920. }
  23921. +#endif
  23922. #else
  23923. @@ -734,11 +775,8 @@ static inline int hrtimer_is_hres_enabled(void) { return 0; }
  23924. static inline void hrtimer_switch_to_hres(void) { }
  23925. static inline void
  23926. hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
  23927. -static inline int hrtimer_reprogram(struct hrtimer *timer,
  23928. - struct hrtimer_clock_base *base)
  23929. -{
  23930. - return 0;
  23931. -}
  23932. +static inline void hrtimer_reprogram(struct hrtimer *timer,
  23933. + struct hrtimer_clock_base *base) { }
  23934. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  23935. static inline void retrigger_next_event(void *arg) { }
  23936. @@ -870,6 +908,32 @@ u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  23937. }
  23938. EXPORT_SYMBOL_GPL(hrtimer_forward);
  23939. +#ifdef CONFIG_PREEMPT_RT_BASE
  23940. +# define wake_up_timer_waiters(b) wake_up(&(b)->wait)
  23941. +
  23942. +/**
  23943. + * hrtimer_wait_for_timer - Wait for a running timer
  23944. + *
  23945. + * @timer: timer to wait for
  23946. + *
  23947. + * The function waits in case the timers callback function is
  23948. + * currently executed on the waitqueue of the timer base. The
  23949. + * waitqueue is woken up after the timer callback function has
  23950. + * finished execution.
  23951. + */
  23952. +void hrtimer_wait_for_timer(const struct hrtimer *timer)
  23953. +{
  23954. + struct hrtimer_clock_base *base = timer->base;
  23955. +
  23956. + if (base && base->cpu_base && !timer->irqsafe)
  23957. + wait_event(base->cpu_base->wait,
  23958. + !(hrtimer_callback_running(timer)));
  23959. +}
  23960. +
  23961. +#else
  23962. +# define wake_up_timer_waiters(b) do { } while (0)
  23963. +#endif
  23964. +
  23965. /*
  23966. * enqueue_hrtimer - internal function to (re)start a timer
  23967. *
  23968. @@ -911,6 +975,11 @@ static void __remove_hrtimer(struct hrtimer *timer,
  23969. if (!(state & HRTIMER_STATE_ENQUEUED))
  23970. return;
  23971. + if (unlikely(!list_empty(&timer->cb_entry))) {
  23972. + list_del_init(&timer->cb_entry);
  23973. + return;
  23974. + }
  23975. +
  23976. if (!timerqueue_del(&base->active, &timer->node))
  23977. cpu_base->active_bases &= ~(1 << base->index);
  23978. @@ -1006,7 +1075,16 @@ void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  23979. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  23980. timer_stats_hrtimer_set_start_info(timer);
  23981. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  23982. + {
  23983. + ktime_t now = new_base->get_time();
  23984. + if (ktime_to_ns(tim) < ktime_to_ns(now))
  23985. + timer->praecox = now;
  23986. + else
  23987. + timer->praecox = ktime_set(0, 0);
  23988. + }
  23989. +#endif
  23990. leftmost = enqueue_hrtimer(timer, new_base);
  23991. if (!leftmost)
  23992. goto unlock;
  23993. @@ -1078,7 +1156,7 @@ int hrtimer_cancel(struct hrtimer *timer)
  23994. if (ret >= 0)
  23995. return ret;
  23996. - cpu_relax();
  23997. + hrtimer_wait_for_timer(timer);
  23998. }
  23999. }
  24000. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  24001. @@ -1142,6 +1220,7 @@ static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  24002. base = hrtimer_clockid_to_base(clock_id);
  24003. timer->base = &cpu_base->clock_base[base];
  24004. + INIT_LIST_HEAD(&timer->cb_entry);
  24005. timerqueue_init(&timer->node);
  24006. #ifdef CONFIG_TIMER_STATS
  24007. @@ -1182,6 +1261,7 @@ bool hrtimer_active(const struct hrtimer *timer)
  24008. seq = raw_read_seqcount_begin(&cpu_base->seq);
  24009. if (timer->state != HRTIMER_STATE_INACTIVE ||
  24010. + cpu_base->running_soft == timer ||
  24011. cpu_base->running == timer)
  24012. return true;
  24013. @@ -1280,10 +1360,112 @@ static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
  24014. cpu_base->running = NULL;
  24015. }
  24016. -static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
  24017. +#ifdef CONFIG_PREEMPT_RT_BASE
  24018. +static void hrtimer_rt_reprogram(int restart, struct hrtimer *timer,
  24019. + struct hrtimer_clock_base *base)
  24020. +{
  24021. + int leftmost;
  24022. +
  24023. + if (restart != HRTIMER_NORESTART &&
  24024. + !(timer->state & HRTIMER_STATE_ENQUEUED)) {
  24025. +
  24026. + leftmost = enqueue_hrtimer(timer, base);
  24027. + if (!leftmost)
  24028. + return;
  24029. +#ifdef CONFIG_HIGH_RES_TIMERS
  24030. + if (!hrtimer_is_hres_active(timer)) {
  24031. + /*
  24032. + * Kick to reschedule the next tick to handle the new timer
  24033. + * on dynticks target.
  24034. + */
  24035. + if (base->cpu_base->nohz_active)
  24036. + wake_up_nohz_cpu(base->cpu_base->cpu);
  24037. + } else {
  24038. +
  24039. + hrtimer_reprogram(timer, base);
  24040. + }
  24041. +#endif
  24042. + }
  24043. +}
  24044. +
  24045. +/*
  24046. + * The changes in mainline which removed the callback modes from
  24047. + * hrtimer are not yet working with -rt. The non wakeup_process()
  24048. + * based callbacks which involve sleeping locks need to be treated
  24049. + * seperately.
  24050. + */
  24051. +static void hrtimer_rt_run_pending(void)
  24052. +{
  24053. + enum hrtimer_restart (*fn)(struct hrtimer *);
  24054. + struct hrtimer_cpu_base *cpu_base;
  24055. + struct hrtimer_clock_base *base;
  24056. + struct hrtimer *timer;
  24057. + int index, restart;
  24058. +
  24059. + local_irq_disable();
  24060. + cpu_base = &per_cpu(hrtimer_bases, smp_processor_id());
  24061. +
  24062. + raw_spin_lock(&cpu_base->lock);
  24063. +
  24064. + for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  24065. + base = &cpu_base->clock_base[index];
  24066. +
  24067. + while (!list_empty(&base->expired)) {
  24068. + timer = list_first_entry(&base->expired,
  24069. + struct hrtimer, cb_entry);
  24070. +
  24071. + /*
  24072. + * Same as the above __run_hrtimer function
  24073. + * just we run with interrupts enabled.
  24074. + */
  24075. + debug_deactivate(timer);
  24076. + cpu_base->running_soft = timer;
  24077. + raw_write_seqcount_barrier(&cpu_base->seq);
  24078. +
  24079. + __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
  24080. + timer_stats_account_hrtimer(timer);
  24081. + fn = timer->function;
  24082. +
  24083. + raw_spin_unlock_irq(&cpu_base->lock);
  24084. + restart = fn(timer);
  24085. + raw_spin_lock_irq(&cpu_base->lock);
  24086. +
  24087. + hrtimer_rt_reprogram(restart, timer, base);
  24088. + raw_write_seqcount_barrier(&cpu_base->seq);
  24089. +
  24090. + WARN_ON_ONCE(cpu_base->running_soft != timer);
  24091. + cpu_base->running_soft = NULL;
  24092. + }
  24093. + }
  24094. +
  24095. + raw_spin_unlock_irq(&cpu_base->lock);
  24096. +
  24097. + wake_up_timer_waiters(cpu_base);
  24098. +}
  24099. +
  24100. +static int hrtimer_rt_defer(struct hrtimer *timer)
  24101. +{
  24102. + if (timer->irqsafe)
  24103. + return 0;
  24104. +
  24105. + __remove_hrtimer(timer, timer->base, timer->state, 0);
  24106. + list_add_tail(&timer->cb_entry, &timer->base->expired);
  24107. + return 1;
  24108. +}
  24109. +
  24110. +#else
  24111. +
  24112. +static inline int hrtimer_rt_defer(struct hrtimer *timer) { return 0; }
  24113. +
  24114. +#endif
  24115. +
  24116. +static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer);
  24117. +
  24118. +static int __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
  24119. {
  24120. struct hrtimer_clock_base *base = cpu_base->clock_base;
  24121. unsigned int active = cpu_base->active_bases;
  24122. + int raise = 0;
  24123. for (; active; base++, active >>= 1) {
  24124. struct timerqueue_node *node;
  24125. @@ -1299,6 +1481,15 @@ static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
  24126. timer = container_of(node, struct hrtimer, node);
  24127. + trace_hrtimer_interrupt(raw_smp_processor_id(),
  24128. + ktime_to_ns(ktime_sub(ktime_to_ns(timer->praecox) ?
  24129. + timer->praecox : hrtimer_get_expires(timer),
  24130. + basenow)),
  24131. + current,
  24132. + timer->function == hrtimer_wakeup ?
  24133. + container_of(timer, struct hrtimer_sleeper,
  24134. + timer)->task : NULL);
  24135. +
  24136. /*
  24137. * The immediate goal for using the softexpires is
  24138. * minimizing wakeups, not running timers at the
  24139. @@ -1314,9 +1505,13 @@ static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
  24140. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer))
  24141. break;
  24142. - __run_hrtimer(cpu_base, base, timer, &basenow);
  24143. + if (!hrtimer_rt_defer(timer))
  24144. + __run_hrtimer(cpu_base, base, timer, &basenow);
  24145. + else
  24146. + raise = 1;
  24147. }
  24148. }
  24149. + return raise;
  24150. }
  24151. #ifdef CONFIG_HIGH_RES_TIMERS
  24152. @@ -1330,6 +1525,7 @@ void hrtimer_interrupt(struct clock_event_device *dev)
  24153. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  24154. ktime_t expires_next, now, entry_time, delta;
  24155. int retries = 0;
  24156. + int raise;
  24157. BUG_ON(!cpu_base->hres_active);
  24158. cpu_base->nr_events++;
  24159. @@ -1348,7 +1544,7 @@ void hrtimer_interrupt(struct clock_event_device *dev)
  24160. */
  24161. cpu_base->expires_next.tv64 = KTIME_MAX;
  24162. - __hrtimer_run_queues(cpu_base, now);
  24163. + raise = __hrtimer_run_queues(cpu_base, now);
  24164. /* Reevaluate the clock bases for the next expiry */
  24165. expires_next = __hrtimer_get_next_event(cpu_base);
  24166. @@ -1359,6 +1555,8 @@ void hrtimer_interrupt(struct clock_event_device *dev)
  24167. cpu_base->expires_next = expires_next;
  24168. cpu_base->in_hrtirq = 0;
  24169. raw_spin_unlock(&cpu_base->lock);
  24170. + if (raise)
  24171. + raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  24172. /* Reprogramming necessary ? */
  24173. if (!tick_program_event(expires_next, 0)) {
  24174. @@ -1438,6 +1636,7 @@ void hrtimer_run_queues(void)
  24175. {
  24176. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  24177. ktime_t now;
  24178. + int raise;
  24179. if (__hrtimer_hres_active(cpu_base))
  24180. return;
  24181. @@ -1456,8 +1655,10 @@ void hrtimer_run_queues(void)
  24182. raw_spin_lock(&cpu_base->lock);
  24183. now = hrtimer_update_base(cpu_base);
  24184. - __hrtimer_run_queues(cpu_base, now);
  24185. + raise = __hrtimer_run_queues(cpu_base, now);
  24186. raw_spin_unlock(&cpu_base->lock);
  24187. + if (raise)
  24188. + raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  24189. }
  24190. /*
  24191. @@ -1479,16 +1680,18 @@ static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  24192. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  24193. {
  24194. sl->timer.function = hrtimer_wakeup;
  24195. + sl->timer.irqsafe = 1;
  24196. sl->task = task;
  24197. }
  24198. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  24199. -static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  24200. +static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode,
  24201. + unsigned long state)
  24202. {
  24203. hrtimer_init_sleeper(t, current);
  24204. do {
  24205. - set_current_state(TASK_INTERRUPTIBLE);
  24206. + set_current_state(state);
  24207. hrtimer_start_expires(&t->timer, mode);
  24208. if (likely(t->task))
  24209. @@ -1530,7 +1733,8 @@ long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  24210. HRTIMER_MODE_ABS);
  24211. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  24212. - if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  24213. + /* cpu_chill() does not care about restart state. */
  24214. + if (do_nanosleep(&t, HRTIMER_MODE_ABS, TASK_INTERRUPTIBLE))
  24215. goto out;
  24216. rmtp = restart->nanosleep.rmtp;
  24217. @@ -1547,8 +1751,10 @@ long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  24218. return ret;
  24219. }
  24220. -long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  24221. - const enum hrtimer_mode mode, const clockid_t clockid)
  24222. +static long
  24223. +__hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  24224. + const enum hrtimer_mode mode, const clockid_t clockid,
  24225. + unsigned long state)
  24226. {
  24227. struct restart_block *restart;
  24228. struct hrtimer_sleeper t;
  24229. @@ -1561,7 +1767,7 @@ long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  24230. hrtimer_init_on_stack(&t.timer, clockid, mode);
  24231. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  24232. - if (do_nanosleep(&t, mode))
  24233. + if (do_nanosleep(&t, mode, state))
  24234. goto out;
  24235. /* Absolute timers do not update the rmtp value and restart: */
  24236. @@ -1588,6 +1794,12 @@ long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  24237. return ret;
  24238. }
  24239. +long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  24240. + const enum hrtimer_mode mode, const clockid_t clockid)
  24241. +{
  24242. + return __hrtimer_nanosleep(rqtp, rmtp, mode, clockid, TASK_INTERRUPTIBLE);
  24243. +}
  24244. +
  24245. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  24246. struct timespec __user *, rmtp)
  24247. {
  24248. @@ -1602,6 +1814,26 @@ SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  24249. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  24250. }
  24251. +#ifdef CONFIG_PREEMPT_RT_FULL
  24252. +/*
  24253. + * Sleep for 1 ms in hope whoever holds what we want will let it go.
  24254. + */
  24255. +void cpu_chill(void)
  24256. +{
  24257. + struct timespec tu = {
  24258. + .tv_nsec = NSEC_PER_MSEC,
  24259. + };
  24260. + unsigned int freeze_flag = current->flags & PF_NOFREEZE;
  24261. +
  24262. + current->flags |= PF_NOFREEZE;
  24263. + __hrtimer_nanosleep(&tu, NULL, HRTIMER_MODE_REL, CLOCK_MONOTONIC,
  24264. + TASK_UNINTERRUPTIBLE);
  24265. + if (!freeze_flag)
  24266. + current->flags &= ~PF_NOFREEZE;
  24267. +}
  24268. +EXPORT_SYMBOL(cpu_chill);
  24269. +#endif
  24270. +
  24271. /*
  24272. * Functions related to boot-time initialization:
  24273. */
  24274. @@ -1613,15 +1845,19 @@ static void init_hrtimers_cpu(int cpu)
  24275. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  24276. cpu_base->clock_base[i].cpu_base = cpu_base;
  24277. timerqueue_init_head(&cpu_base->clock_base[i].active);
  24278. + INIT_LIST_HEAD(&cpu_base->clock_base[i].expired);
  24279. }
  24280. cpu_base->cpu = cpu;
  24281. hrtimer_init_hres(cpu_base);
  24282. +#ifdef CONFIG_PREEMPT_RT_BASE
  24283. + init_waitqueue_head(&cpu_base->wait);
  24284. +#endif
  24285. }
  24286. #ifdef CONFIG_HOTPLUG_CPU
  24287. -static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  24288. +static int migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  24289. struct hrtimer_clock_base *new_base)
  24290. {
  24291. struct hrtimer *timer;
  24292. @@ -1649,12 +1885,21 @@ static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  24293. */
  24294. enqueue_hrtimer(timer, new_base);
  24295. }
  24296. +#ifdef CONFIG_PREEMPT_RT_BASE
  24297. + list_splice_tail(&old_base->expired, &new_base->expired);
  24298. + /*
  24299. + * Tell the caller to raise HRTIMER_SOFTIRQ. We can't safely
  24300. + * acquire ktimersoftd->pi_lock while the base lock is held.
  24301. + */
  24302. + return !list_empty(&new_base->expired);
  24303. +#endif
  24304. + return 0;
  24305. }
  24306. static void migrate_hrtimers(int scpu)
  24307. {
  24308. struct hrtimer_cpu_base *old_base, *new_base;
  24309. - int i;
  24310. + int i, raise = 0;
  24311. BUG_ON(cpu_online(scpu));
  24312. tick_cancel_sched_timer(scpu);
  24313. @@ -1670,13 +1915,16 @@ static void migrate_hrtimers(int scpu)
  24314. raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  24315. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  24316. - migrate_hrtimer_list(&old_base->clock_base[i],
  24317. - &new_base->clock_base[i]);
  24318. + raise |= migrate_hrtimer_list(&old_base->clock_base[i],
  24319. + &new_base->clock_base[i]);
  24320. }
  24321. raw_spin_unlock(&old_base->lock);
  24322. raw_spin_unlock(&new_base->lock);
  24323. + if (raise)
  24324. + raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  24325. +
  24326. /* Check, if we got expired work to do */
  24327. __hrtimer_peek_ahead_timers();
  24328. local_irq_enable();
  24329. @@ -1714,11 +1962,21 @@ static struct notifier_block hrtimers_nb = {
  24330. .notifier_call = hrtimer_cpu_notify,
  24331. };
  24332. +#ifdef CONFIG_PREEMPT_RT_BASE
  24333. +static void run_hrtimer_softirq(struct softirq_action *h)
  24334. +{
  24335. + hrtimer_rt_run_pending();
  24336. +}
  24337. +#endif
  24338. +
  24339. void __init hrtimers_init(void)
  24340. {
  24341. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  24342. (void *)(long)smp_processor_id());
  24343. register_cpu_notifier(&hrtimers_nb);
  24344. +#ifdef CONFIG_PREEMPT_RT_BASE
  24345. + open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  24346. +#endif
  24347. }
  24348. /**
  24349. diff --git a/kernel/time/itimer.c b/kernel/time/itimer.c
  24350. index 1d5c7204ddc9..184de6751180 100644
  24351. --- a/kernel/time/itimer.c
  24352. +++ b/kernel/time/itimer.c
  24353. @@ -213,6 +213,7 @@ int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
  24354. /* We are sharing ->siglock with it_real_fn() */
  24355. if (hrtimer_try_to_cancel(timer) < 0) {
  24356. spin_unlock_irq(&tsk->sighand->siglock);
  24357. + hrtimer_wait_for_timer(&tsk->signal->real_timer);
  24358. goto again;
  24359. }
  24360. expires = timeval_to_ktime(value->it_value);
  24361. diff --git a/kernel/time/jiffies.c b/kernel/time/jiffies.c
  24362. index 347fecf86a3f..2ede47408a3e 100644
  24363. --- a/kernel/time/jiffies.c
  24364. +++ b/kernel/time/jiffies.c
  24365. @@ -74,7 +74,8 @@ static struct clocksource clocksource_jiffies = {
  24366. .max_cycles = 10,
  24367. };
  24368. -__cacheline_aligned_in_smp DEFINE_SEQLOCK(jiffies_lock);
  24369. +__cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock);
  24370. +__cacheline_aligned_in_smp seqcount_t jiffies_seq;
  24371. #if (BITS_PER_LONG < 64)
  24372. u64 get_jiffies_64(void)
  24373. @@ -83,9 +84,9 @@ u64 get_jiffies_64(void)
  24374. u64 ret;
  24375. do {
  24376. - seq = read_seqbegin(&jiffies_lock);
  24377. + seq = read_seqcount_begin(&jiffies_seq);
  24378. ret = jiffies_64;
  24379. - } while (read_seqretry(&jiffies_lock, seq));
  24380. + } while (read_seqcount_retry(&jiffies_seq, seq));
  24381. return ret;
  24382. }
  24383. EXPORT_SYMBOL(get_jiffies_64);
  24384. diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
  24385. index ab861771e37f..0f6868fd2de6 100644
  24386. --- a/kernel/time/ntp.c
  24387. +++ b/kernel/time/ntp.c
  24388. @@ -10,6 +10,7 @@
  24389. #include <linux/workqueue.h>
  24390. #include <linux/hrtimer.h>
  24391. #include <linux/jiffies.h>
  24392. +#include <linux/kthread.h>
  24393. #include <linux/math64.h>
  24394. #include <linux/timex.h>
  24395. #include <linux/time.h>
  24396. @@ -562,10 +563,52 @@ static void sync_cmos_clock(struct work_struct *work)
  24397. &sync_cmos_work, timespec64_to_jiffies(&next));
  24398. }
  24399. +#ifdef CONFIG_PREEMPT_RT_FULL
  24400. +/*
  24401. + * RT can not call schedule_delayed_work from real interrupt context.
  24402. + * Need to make a thread to do the real work.
  24403. + */
  24404. +static struct task_struct *cmos_delay_thread;
  24405. +static bool do_cmos_delay;
  24406. +
  24407. +static int run_cmos_delay(void *ignore)
  24408. +{
  24409. + while (!kthread_should_stop()) {
  24410. + set_current_state(TASK_INTERRUPTIBLE);
  24411. + if (do_cmos_delay) {
  24412. + do_cmos_delay = false;
  24413. + queue_delayed_work(system_power_efficient_wq,
  24414. + &sync_cmos_work, 0);
  24415. + }
  24416. + schedule();
  24417. + }
  24418. + __set_current_state(TASK_RUNNING);
  24419. + return 0;
  24420. +}
  24421. +
  24422. +void ntp_notify_cmos_timer(void)
  24423. +{
  24424. + do_cmos_delay = true;
  24425. + /* Make visible before waking up process */
  24426. + smp_wmb();
  24427. + wake_up_process(cmos_delay_thread);
  24428. +}
  24429. +
  24430. +static __init int create_cmos_delay_thread(void)
  24431. +{
  24432. + cmos_delay_thread = kthread_run(run_cmos_delay, NULL, "kcmosdelayd");
  24433. + BUG_ON(!cmos_delay_thread);
  24434. + return 0;
  24435. +}
  24436. +early_initcall(create_cmos_delay_thread);
  24437. +
  24438. +#else
  24439. +
  24440. void ntp_notify_cmos_timer(void)
  24441. {
  24442. queue_delayed_work(system_power_efficient_wq, &sync_cmos_work, 0);
  24443. }
  24444. +#endif /* CONFIG_PREEMPT_RT_FULL */
  24445. #else
  24446. void ntp_notify_cmos_timer(void) { }
  24447. diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c
  24448. index 80016b329d94..b7342b6e6a5a 100644
  24449. --- a/kernel/time/posix-cpu-timers.c
  24450. +++ b/kernel/time/posix-cpu-timers.c
  24451. @@ -3,6 +3,7 @@
  24452. */
  24453. #include <linux/sched.h>
  24454. +#include <linux/sched/rt.h>
  24455. #include <linux/posix-timers.h>
  24456. #include <linux/errno.h>
  24457. #include <linux/math64.h>
  24458. @@ -650,7 +651,7 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
  24459. /*
  24460. * Disarm any old timer after extracting its expiry time.
  24461. */
  24462. - WARN_ON_ONCE(!irqs_disabled());
  24463. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  24464. ret = 0;
  24465. old_incr = timer->it.cpu.incr;
  24466. @@ -1092,7 +1093,7 @@ void posix_cpu_timer_schedule(struct k_itimer *timer)
  24467. /*
  24468. * Now re-arm for the new expiry time.
  24469. */
  24470. - WARN_ON_ONCE(!irqs_disabled());
  24471. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  24472. arm_timer(timer);
  24473. unlock_task_sighand(p, &flags);
  24474. @@ -1183,13 +1184,13 @@ static inline int fastpath_timer_check(struct task_struct *tsk)
  24475. * already updated our counts. We need to check if any timers fire now.
  24476. * Interrupts are disabled.
  24477. */
  24478. -void run_posix_cpu_timers(struct task_struct *tsk)
  24479. +static void __run_posix_cpu_timers(struct task_struct *tsk)
  24480. {
  24481. LIST_HEAD(firing);
  24482. struct k_itimer *timer, *next;
  24483. unsigned long flags;
  24484. - WARN_ON_ONCE(!irqs_disabled());
  24485. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  24486. /*
  24487. * The fast path checks that there are no expired thread or thread
  24488. @@ -1243,6 +1244,190 @@ void run_posix_cpu_timers(struct task_struct *tsk)
  24489. }
  24490. }
  24491. +#ifdef CONFIG_PREEMPT_RT_BASE
  24492. +#include <linux/kthread.h>
  24493. +#include <linux/cpu.h>
  24494. +DEFINE_PER_CPU(struct task_struct *, posix_timer_task);
  24495. +DEFINE_PER_CPU(struct task_struct *, posix_timer_tasklist);
  24496. +
  24497. +static int posix_cpu_timers_thread(void *data)
  24498. +{
  24499. + int cpu = (long)data;
  24500. +
  24501. + BUG_ON(per_cpu(posix_timer_task,cpu) != current);
  24502. +
  24503. + while (!kthread_should_stop()) {
  24504. + struct task_struct *tsk = NULL;
  24505. + struct task_struct *next = NULL;
  24506. +
  24507. + if (cpu_is_offline(cpu))
  24508. + goto wait_to_die;
  24509. +
  24510. + /* grab task list */
  24511. + raw_local_irq_disable();
  24512. + tsk = per_cpu(posix_timer_tasklist, cpu);
  24513. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  24514. + raw_local_irq_enable();
  24515. +
  24516. + /* its possible the list is empty, just return */
  24517. + if (!tsk) {
  24518. + set_current_state(TASK_INTERRUPTIBLE);
  24519. + schedule();
  24520. + __set_current_state(TASK_RUNNING);
  24521. + continue;
  24522. + }
  24523. +
  24524. + /* Process task list */
  24525. + while (1) {
  24526. + /* save next */
  24527. + next = tsk->posix_timer_list;
  24528. +
  24529. + /* run the task timers, clear its ptr and
  24530. + * unreference it
  24531. + */
  24532. + __run_posix_cpu_timers(tsk);
  24533. + tsk->posix_timer_list = NULL;
  24534. + put_task_struct(tsk);
  24535. +
  24536. + /* check if this is the last on the list */
  24537. + if (next == tsk)
  24538. + break;
  24539. + tsk = next;
  24540. + }
  24541. + }
  24542. + return 0;
  24543. +
  24544. +wait_to_die:
  24545. + /* Wait for kthread_stop */
  24546. + set_current_state(TASK_INTERRUPTIBLE);
  24547. + while (!kthread_should_stop()) {
  24548. + schedule();
  24549. + set_current_state(TASK_INTERRUPTIBLE);
  24550. + }
  24551. + __set_current_state(TASK_RUNNING);
  24552. + return 0;
  24553. +}
  24554. +
  24555. +static inline int __fastpath_timer_check(struct task_struct *tsk)
  24556. +{
  24557. + /* tsk == current, ensure it is safe to use ->signal/sighand */
  24558. + if (unlikely(tsk->exit_state))
  24559. + return 0;
  24560. +
  24561. + if (!task_cputime_zero(&tsk->cputime_expires))
  24562. + return 1;
  24563. +
  24564. + if (!task_cputime_zero(&tsk->signal->cputime_expires))
  24565. + return 1;
  24566. +
  24567. + return 0;
  24568. +}
  24569. +
  24570. +void run_posix_cpu_timers(struct task_struct *tsk)
  24571. +{
  24572. + unsigned long cpu = smp_processor_id();
  24573. + struct task_struct *tasklist;
  24574. +
  24575. + BUG_ON(!irqs_disabled());
  24576. + if(!per_cpu(posix_timer_task, cpu))
  24577. + return;
  24578. + /* get per-cpu references */
  24579. + tasklist = per_cpu(posix_timer_tasklist, cpu);
  24580. +
  24581. + /* check to see if we're already queued */
  24582. + if (!tsk->posix_timer_list && __fastpath_timer_check(tsk)) {
  24583. + get_task_struct(tsk);
  24584. + if (tasklist) {
  24585. + tsk->posix_timer_list = tasklist;
  24586. + } else {
  24587. + /*
  24588. + * The list is terminated by a self-pointing
  24589. + * task_struct
  24590. + */
  24591. + tsk->posix_timer_list = tsk;
  24592. + }
  24593. + per_cpu(posix_timer_tasklist, cpu) = tsk;
  24594. +
  24595. + wake_up_process(per_cpu(posix_timer_task, cpu));
  24596. + }
  24597. +}
  24598. +
  24599. +/*
  24600. + * posix_cpu_thread_call - callback that gets triggered when a CPU is added.
  24601. + * Here we can start up the necessary migration thread for the new CPU.
  24602. + */
  24603. +static int posix_cpu_thread_call(struct notifier_block *nfb,
  24604. + unsigned long action, void *hcpu)
  24605. +{
  24606. + int cpu = (long)hcpu;
  24607. + struct task_struct *p;
  24608. + struct sched_param param;
  24609. +
  24610. + switch (action) {
  24611. + case CPU_UP_PREPARE:
  24612. + p = kthread_create(posix_cpu_timers_thread, hcpu,
  24613. + "posixcputmr/%d",cpu);
  24614. + if (IS_ERR(p))
  24615. + return NOTIFY_BAD;
  24616. + p->flags |= PF_NOFREEZE;
  24617. + kthread_bind(p, cpu);
  24618. + /* Must be high prio to avoid getting starved */
  24619. + param.sched_priority = MAX_RT_PRIO-1;
  24620. + sched_setscheduler(p, SCHED_FIFO, &param);
  24621. + per_cpu(posix_timer_task,cpu) = p;
  24622. + break;
  24623. + case CPU_ONLINE:
  24624. + /* Strictly unneccessary, as first user will wake it. */
  24625. + wake_up_process(per_cpu(posix_timer_task,cpu));
  24626. + break;
  24627. +#ifdef CONFIG_HOTPLUG_CPU
  24628. + case CPU_UP_CANCELED:
  24629. + /* Unbind it from offline cpu so it can run. Fall thru. */
  24630. + kthread_bind(per_cpu(posix_timer_task, cpu),
  24631. + cpumask_any(cpu_online_mask));
  24632. + kthread_stop(per_cpu(posix_timer_task,cpu));
  24633. + per_cpu(posix_timer_task,cpu) = NULL;
  24634. + break;
  24635. + case CPU_DEAD:
  24636. + kthread_stop(per_cpu(posix_timer_task,cpu));
  24637. + per_cpu(posix_timer_task,cpu) = NULL;
  24638. + break;
  24639. +#endif
  24640. + }
  24641. + return NOTIFY_OK;
  24642. +}
  24643. +
  24644. +/* Register at highest priority so that task migration (migrate_all_tasks)
  24645. + * happens before everything else.
  24646. + */
  24647. +static struct notifier_block posix_cpu_thread_notifier = {
  24648. + .notifier_call = posix_cpu_thread_call,
  24649. + .priority = 10
  24650. +};
  24651. +
  24652. +static int __init posix_cpu_thread_init(void)
  24653. +{
  24654. + void *hcpu = (void *)(long)smp_processor_id();
  24655. + /* Start one for boot CPU. */
  24656. + unsigned long cpu;
  24657. +
  24658. + /* init the per-cpu posix_timer_tasklets */
  24659. + for_each_possible_cpu(cpu)
  24660. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  24661. +
  24662. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_UP_PREPARE, hcpu);
  24663. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_ONLINE, hcpu);
  24664. + register_cpu_notifier(&posix_cpu_thread_notifier);
  24665. + return 0;
  24666. +}
  24667. +early_initcall(posix_cpu_thread_init);
  24668. +#else /* CONFIG_PREEMPT_RT_BASE */
  24669. +void run_posix_cpu_timers(struct task_struct *tsk)
  24670. +{
  24671. + __run_posix_cpu_timers(tsk);
  24672. +}
  24673. +#endif /* CONFIG_PREEMPT_RT_BASE */
  24674. +
  24675. /*
  24676. * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
  24677. * The tsk->sighand->siglock must be held by the caller.
  24678. diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
  24679. index f2826c35e918..464a98155a0e 100644
  24680. --- a/kernel/time/posix-timers.c
  24681. +++ b/kernel/time/posix-timers.c
  24682. @@ -506,6 +506,7 @@ static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
  24683. static struct pid *good_sigevent(sigevent_t * event)
  24684. {
  24685. struct task_struct *rtn = current->group_leader;
  24686. + int sig = event->sigev_signo;
  24687. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  24688. (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
  24689. @@ -514,7 +515,8 @@ static struct pid *good_sigevent(sigevent_t * event)
  24690. return NULL;
  24691. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  24692. - ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  24693. + (sig <= 0 || sig > SIGRTMAX || sig_kernel_only(sig) ||
  24694. + sig_kernel_coredump(sig)))
  24695. return NULL;
  24696. return task_pid(rtn);
  24697. @@ -826,6 +828,20 @@ SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
  24698. return overrun;
  24699. }
  24700. +/*
  24701. + * Protected by RCU!
  24702. + */
  24703. +static void timer_wait_for_callback(struct k_clock *kc, struct k_itimer *timr)
  24704. +{
  24705. +#ifdef CONFIG_PREEMPT_RT_FULL
  24706. + if (kc->timer_set == common_timer_set)
  24707. + hrtimer_wait_for_timer(&timr->it.real.timer);
  24708. + else
  24709. + /* FIXME: Whacky hack for posix-cpu-timers */
  24710. + schedule_timeout(1);
  24711. +#endif
  24712. +}
  24713. +
  24714. /* Set a POSIX.1b interval timer. */
  24715. /* timr->it_lock is taken. */
  24716. static int
  24717. @@ -903,6 +919,7 @@ SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
  24718. if (!timr)
  24719. return -EINVAL;
  24720. + rcu_read_lock();
  24721. kc = clockid_to_kclock(timr->it_clock);
  24722. if (WARN_ON_ONCE(!kc || !kc->timer_set))
  24723. error = -EINVAL;
  24724. @@ -911,9 +928,12 @@ SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
  24725. unlock_timer(timr, flag);
  24726. if (error == TIMER_RETRY) {
  24727. + timer_wait_for_callback(kc, timr);
  24728. rtn = NULL; // We already got the old time...
  24729. + rcu_read_unlock();
  24730. goto retry;
  24731. }
  24732. + rcu_read_unlock();
  24733. if (old_setting && !error &&
  24734. copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
  24735. @@ -951,10 +971,15 @@ SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
  24736. if (!timer)
  24737. return -EINVAL;
  24738. + rcu_read_lock();
  24739. if (timer_delete_hook(timer) == TIMER_RETRY) {
  24740. unlock_timer(timer, flags);
  24741. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  24742. + timer);
  24743. + rcu_read_unlock();
  24744. goto retry_delete;
  24745. }
  24746. + rcu_read_unlock();
  24747. spin_lock(&current->sighand->siglock);
  24748. list_del(&timer->list);
  24749. @@ -980,8 +1005,18 @@ static void itimer_delete(struct k_itimer *timer)
  24750. retry_delete:
  24751. spin_lock_irqsave(&timer->it_lock, flags);
  24752. + /* On RT we can race with a deletion */
  24753. + if (!timer->it_signal) {
  24754. + unlock_timer(timer, flags);
  24755. + return;
  24756. + }
  24757. +
  24758. if (timer_delete_hook(timer) == TIMER_RETRY) {
  24759. + rcu_read_lock();
  24760. unlock_timer(timer, flags);
  24761. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  24762. + timer);
  24763. + rcu_read_unlock();
  24764. goto retry_delete;
  24765. }
  24766. list_del(&timer->list);
  24767. diff --git a/kernel/time/tick-broadcast-hrtimer.c b/kernel/time/tick-broadcast-hrtimer.c
  24768. index 53d7184da0be..1b4ac3361c3f 100644
  24769. --- a/kernel/time/tick-broadcast-hrtimer.c
  24770. +++ b/kernel/time/tick-broadcast-hrtimer.c
  24771. @@ -106,5 +106,6 @@ void tick_setup_hrtimer_broadcast(void)
  24772. {
  24773. hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  24774. bctimer.function = bc_handler;
  24775. + bctimer.irqsafe = true;
  24776. clockevents_register_device(&ce_broadcast_hrtimer);
  24777. }
  24778. diff --git a/kernel/time/tick-common.c b/kernel/time/tick-common.c
  24779. index 4fcd99e12aa0..5a47f2e98faf 100644
  24780. --- a/kernel/time/tick-common.c
  24781. +++ b/kernel/time/tick-common.c
  24782. @@ -79,13 +79,15 @@ int tick_is_oneshot_available(void)
  24783. static void tick_periodic(int cpu)
  24784. {
  24785. if (tick_do_timer_cpu == cpu) {
  24786. - write_seqlock(&jiffies_lock);
  24787. + raw_spin_lock(&jiffies_lock);
  24788. + write_seqcount_begin(&jiffies_seq);
  24789. /* Keep track of the next tick event */
  24790. tick_next_period = ktime_add(tick_next_period, tick_period);
  24791. do_timer(1);
  24792. - write_sequnlock(&jiffies_lock);
  24793. + write_seqcount_end(&jiffies_seq);
  24794. + raw_spin_unlock(&jiffies_lock);
  24795. update_wall_time();
  24796. }
  24797. @@ -157,9 +159,9 @@ void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
  24798. ktime_t next;
  24799. do {
  24800. - seq = read_seqbegin(&jiffies_lock);
  24801. + seq = read_seqcount_begin(&jiffies_seq);
  24802. next = tick_next_period;
  24803. - } while (read_seqretry(&jiffies_lock, seq));
  24804. + } while (read_seqcount_retry(&jiffies_seq, seq));
  24805. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  24806. diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
  24807. index e5d228f7224c..45e9456da50f 100644
  24808. --- a/kernel/time/tick-sched.c
  24809. +++ b/kernel/time/tick-sched.c
  24810. @@ -62,7 +62,8 @@ static void tick_do_update_jiffies64(ktime_t now)
  24811. return;
  24812. /* Reevalute with jiffies_lock held */
  24813. - write_seqlock(&jiffies_lock);
  24814. + raw_spin_lock(&jiffies_lock);
  24815. + write_seqcount_begin(&jiffies_seq);
  24816. delta = ktime_sub(now, last_jiffies_update);
  24817. if (delta.tv64 >= tick_period.tv64) {
  24818. @@ -85,10 +86,12 @@ static void tick_do_update_jiffies64(ktime_t now)
  24819. /* Keep the tick_next_period variable up to date */
  24820. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  24821. } else {
  24822. - write_sequnlock(&jiffies_lock);
  24823. + write_seqcount_end(&jiffies_seq);
  24824. + raw_spin_unlock(&jiffies_lock);
  24825. return;
  24826. }
  24827. - write_sequnlock(&jiffies_lock);
  24828. + write_seqcount_end(&jiffies_seq);
  24829. + raw_spin_unlock(&jiffies_lock);
  24830. update_wall_time();
  24831. }
  24832. @@ -99,12 +102,14 @@ static ktime_t tick_init_jiffy_update(void)
  24833. {
  24834. ktime_t period;
  24835. - write_seqlock(&jiffies_lock);
  24836. + raw_spin_lock(&jiffies_lock);
  24837. + write_seqcount_begin(&jiffies_seq);
  24838. /* Did we start the jiffies update yet ? */
  24839. if (last_jiffies_update.tv64 == 0)
  24840. last_jiffies_update = tick_next_period;
  24841. period = last_jiffies_update;
  24842. - write_sequnlock(&jiffies_lock);
  24843. + write_seqcount_end(&jiffies_seq);
  24844. + raw_spin_unlock(&jiffies_lock);
  24845. return period;
  24846. }
  24847. @@ -176,6 +181,11 @@ static bool can_stop_full_tick(void)
  24848. return false;
  24849. }
  24850. + if (!arch_irq_work_has_interrupt()) {
  24851. + trace_tick_stop(0, "missing irq work interrupt\n");
  24852. + return false;
  24853. + }
  24854. +
  24855. /* sched_clock_tick() needs us? */
  24856. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  24857. /*
  24858. @@ -204,6 +214,7 @@ static void nohz_full_kick_work_func(struct irq_work *work)
  24859. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
  24860. .func = nohz_full_kick_work_func,
  24861. + .flags = IRQ_WORK_HARD_IRQ,
  24862. };
  24863. /*
  24864. @@ -583,10 +594,10 @@ static ktime_t tick_nohz_stop_sched_tick(struct tick_sched *ts,
  24865. /* Read jiffies and the time when jiffies were updated last */
  24866. do {
  24867. - seq = read_seqbegin(&jiffies_lock);
  24868. + seq = read_seqcount_begin(&jiffies_seq);
  24869. basemono = last_jiffies_update.tv64;
  24870. basejiff = jiffies;
  24871. - } while (read_seqretry(&jiffies_lock, seq));
  24872. + } while (read_seqcount_retry(&jiffies_seq, seq));
  24873. ts->last_jiffies = basejiff;
  24874. /*
  24875. @@ -768,14 +779,7 @@ static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
  24876. return false;
  24877. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  24878. - static int ratelimit;
  24879. -
  24880. - if (ratelimit < 10 &&
  24881. - (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  24882. - pr_warn("NOHZ: local_softirq_pending %02x\n",
  24883. - (unsigned int) local_softirq_pending());
  24884. - ratelimit++;
  24885. - }
  24886. + softirq_check_pending_idle();
  24887. return false;
  24888. }
  24889. @@ -1115,6 +1119,7 @@ void tick_setup_sched_timer(void)
  24890. * Emulate tick processing via per-CPU hrtimers:
  24891. */
  24892. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  24893. + ts->sched_timer.irqsafe = 1;
  24894. ts->sched_timer.function = tick_sched_timer;
  24895. /* Get the next period (per cpu) */
  24896. diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
  24897. index 6e4866834d26..6c3e5323d06b 100644
  24898. --- a/kernel/time/timekeeping.c
  24899. +++ b/kernel/time/timekeeping.c
  24900. @@ -2091,8 +2091,10 @@ EXPORT_SYMBOL(hardpps);
  24901. */
  24902. void xtime_update(unsigned long ticks)
  24903. {
  24904. - write_seqlock(&jiffies_lock);
  24905. + raw_spin_lock(&jiffies_lock);
  24906. + write_seqcount_begin(&jiffies_seq);
  24907. do_timer(ticks);
  24908. - write_sequnlock(&jiffies_lock);
  24909. + write_seqcount_end(&jiffies_seq);
  24910. + raw_spin_unlock(&jiffies_lock);
  24911. update_wall_time();
  24912. }
  24913. diff --git a/kernel/time/timekeeping.h b/kernel/time/timekeeping.h
  24914. index 704f595ce83f..763a3e5121ff 100644
  24915. --- a/kernel/time/timekeeping.h
  24916. +++ b/kernel/time/timekeeping.h
  24917. @@ -19,7 +19,8 @@ extern void timekeeping_resume(void);
  24918. extern void do_timer(unsigned long ticks);
  24919. extern void update_wall_time(void);
  24920. -extern seqlock_t jiffies_lock;
  24921. +extern raw_spinlock_t jiffies_lock;
  24922. +extern seqcount_t jiffies_seq;
  24923. #define CS_NAME_LEN 32
  24924. diff --git a/kernel/time/timer.c b/kernel/time/timer.c
  24925. index 125407144c01..13652dafe83a 100644
  24926. --- a/kernel/time/timer.c
  24927. +++ b/kernel/time/timer.c
  24928. @@ -80,6 +80,9 @@ struct tvec_root {
  24929. struct tvec_base {
  24930. spinlock_t lock;
  24931. struct timer_list *running_timer;
  24932. +#ifdef CONFIG_PREEMPT_RT_FULL
  24933. + wait_queue_head_t wait_for_running_timer;
  24934. +#endif
  24935. unsigned long timer_jiffies;
  24936. unsigned long next_timer;
  24937. unsigned long active_timers;
  24938. @@ -777,6 +780,39 @@ static struct tvec_base *lock_timer_base(struct timer_list *timer,
  24939. cpu_relax();
  24940. }
  24941. }
  24942. +#ifdef CONFIG_PREEMPT_RT_FULL
  24943. +static inline struct tvec_base *switch_timer_base(struct timer_list *timer,
  24944. + struct tvec_base *old,
  24945. + struct tvec_base *new)
  24946. +{
  24947. + /*
  24948. + * We cannot do the below because we might be preempted and
  24949. + * then the preempter would see NULL and loop forever.
  24950. + */
  24951. + if (spin_trylock(&new->lock)) {
  24952. + WRITE_ONCE(timer->flags,
  24953. + (timer->flags & ~TIMER_BASEMASK) | new->cpu);
  24954. + spin_unlock(&old->lock);
  24955. + return new;
  24956. + }
  24957. + return old;
  24958. +}
  24959. +
  24960. +#else
  24961. +static inline struct tvec_base *switch_timer_base(struct timer_list *timer,
  24962. + struct tvec_base *old,
  24963. + struct tvec_base *new)
  24964. +{
  24965. + /* See the comment in lock_timer_base() */
  24966. + timer->flags |= TIMER_MIGRATING;
  24967. +
  24968. + spin_unlock(&old->lock);
  24969. + spin_lock(&new->lock);
  24970. + WRITE_ONCE(timer->flags,
  24971. + (timer->flags & ~TIMER_BASEMASK) | new->cpu);
  24972. + return new;
  24973. +}
  24974. +#endif
  24975. static inline int
  24976. __mod_timer(struct timer_list *timer, unsigned long expires,
  24977. @@ -807,16 +843,8 @@ __mod_timer(struct timer_list *timer, unsigned long expires,
  24978. * handler yet has not finished. This also guarantees that
  24979. * the timer is serialized wrt itself.
  24980. */
  24981. - if (likely(base->running_timer != timer)) {
  24982. - /* See the comment in lock_timer_base() */
  24983. - timer->flags |= TIMER_MIGRATING;
  24984. -
  24985. - spin_unlock(&base->lock);
  24986. - base = new_base;
  24987. - spin_lock(&base->lock);
  24988. - WRITE_ONCE(timer->flags,
  24989. - (timer->flags & ~TIMER_BASEMASK) | base->cpu);
  24990. - }
  24991. + if (likely(base->running_timer != timer))
  24992. + base = switch_timer_base(timer, base, new_base);
  24993. }
  24994. timer->expires = expires;
  24995. @@ -1006,6 +1034,33 @@ void add_timer_on(struct timer_list *timer, int cpu)
  24996. }
  24997. EXPORT_SYMBOL_GPL(add_timer_on);
  24998. +#ifdef CONFIG_PREEMPT_RT_FULL
  24999. +/*
  25000. + * Wait for a running timer
  25001. + */
  25002. +static void wait_for_running_timer(struct timer_list *timer)
  25003. +{
  25004. + struct tvec_base *base;
  25005. + u32 tf = timer->flags;
  25006. +
  25007. + if (tf & TIMER_MIGRATING)
  25008. + return;
  25009. +
  25010. + base = per_cpu_ptr(&tvec_bases, tf & TIMER_CPUMASK);
  25011. + wait_event(base->wait_for_running_timer,
  25012. + base->running_timer != timer);
  25013. +}
  25014. +
  25015. +# define wakeup_timer_waiters(b) wake_up_all(&(b)->wait_for_running_timer)
  25016. +#else
  25017. +static inline void wait_for_running_timer(struct timer_list *timer)
  25018. +{
  25019. + cpu_relax();
  25020. +}
  25021. +
  25022. +# define wakeup_timer_waiters(b) do { } while (0)
  25023. +#endif
  25024. +
  25025. /**
  25026. * del_timer - deactive a timer.
  25027. * @timer: the timer to be deactivated
  25028. @@ -1063,7 +1118,7 @@ int try_to_del_timer_sync(struct timer_list *timer)
  25029. }
  25030. EXPORT_SYMBOL(try_to_del_timer_sync);
  25031. -#ifdef CONFIG_SMP
  25032. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  25033. /**
  25034. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  25035. * @timer: the timer to be deactivated
  25036. @@ -1123,7 +1178,7 @@ int del_timer_sync(struct timer_list *timer)
  25037. int ret = try_to_del_timer_sync(timer);
  25038. if (ret >= 0)
  25039. return ret;
  25040. - cpu_relax();
  25041. + wait_for_running_timer(timer);
  25042. }
  25043. }
  25044. EXPORT_SYMBOL(del_timer_sync);
  25045. @@ -1248,16 +1303,18 @@ static inline void __run_timers(struct tvec_base *base)
  25046. if (irqsafe) {
  25047. spin_unlock(&base->lock);
  25048. call_timer_fn(timer, fn, data);
  25049. + base->running_timer = NULL;
  25050. spin_lock(&base->lock);
  25051. } else {
  25052. spin_unlock_irq(&base->lock);
  25053. call_timer_fn(timer, fn, data);
  25054. + base->running_timer = NULL;
  25055. spin_lock_irq(&base->lock);
  25056. }
  25057. }
  25058. }
  25059. - base->running_timer = NULL;
  25060. spin_unlock_irq(&base->lock);
  25061. + wakeup_timer_waiters(base);
  25062. }
  25063. #ifdef CONFIG_NO_HZ_COMMON
  25064. @@ -1390,6 +1447,14 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
  25065. if (cpu_is_offline(smp_processor_id()))
  25066. return expires;
  25067. +#ifdef CONFIG_PREEMPT_RT_FULL
  25068. + /*
  25069. + * On PREEMPT_RT we cannot sleep here. As a result we can't take
  25070. + * the base lock to check when the next timer is pending and so
  25071. + * we assume the next jiffy.
  25072. + */
  25073. + return basem + TICK_NSEC;
  25074. +#endif
  25075. spin_lock(&base->lock);
  25076. if (base->active_timers) {
  25077. if (time_before_eq(base->next_timer, base->timer_jiffies))
  25078. @@ -1416,13 +1481,13 @@ void update_process_times(int user_tick)
  25079. /* Note: this timer irq context must be accounted for as well. */
  25080. account_process_tick(p, user_tick);
  25081. + scheduler_tick();
  25082. run_local_timers();
  25083. rcu_check_callbacks(user_tick);
  25084. -#ifdef CONFIG_IRQ_WORK
  25085. +#if defined(CONFIG_IRQ_WORK)
  25086. if (in_irq())
  25087. irq_work_tick();
  25088. #endif
  25089. - scheduler_tick();
  25090. run_posix_cpu_timers(p);
  25091. }
  25092. @@ -1433,6 +1498,8 @@ static void run_timer_softirq(struct softirq_action *h)
  25093. {
  25094. struct tvec_base *base = this_cpu_ptr(&tvec_bases);
  25095. + irq_work_tick_soft();
  25096. +
  25097. if (time_after_eq(jiffies, base->timer_jiffies))
  25098. __run_timers(base);
  25099. }
  25100. @@ -1589,7 +1656,7 @@ static void migrate_timers(int cpu)
  25101. BUG_ON(cpu_online(cpu));
  25102. old_base = per_cpu_ptr(&tvec_bases, cpu);
  25103. - new_base = get_cpu_ptr(&tvec_bases);
  25104. + new_base = get_local_ptr(&tvec_bases);
  25105. /*
  25106. * The caller is globally serialized and nobody else
  25107. * takes two locks at once, deadlock is not possible.
  25108. @@ -1613,7 +1680,7 @@ static void migrate_timers(int cpu)
  25109. spin_unlock(&old_base->lock);
  25110. spin_unlock_irq(&new_base->lock);
  25111. - put_cpu_ptr(&tvec_bases);
  25112. + put_local_ptr(&tvec_bases);
  25113. }
  25114. static int timer_cpu_notify(struct notifier_block *self,
  25115. @@ -1645,6 +1712,9 @@ static void __init init_timer_cpu(int cpu)
  25116. base->cpu = cpu;
  25117. spin_lock_init(&base->lock);
  25118. +#ifdef CONFIG_PREEMPT_RT_FULL
  25119. + init_waitqueue_head(&base->wait_for_running_timer);
  25120. +#endif
  25121. base->timer_jiffies = jiffies;
  25122. base->next_timer = base->timer_jiffies;
  25123. diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig
  25124. index e45db6b0d878..364ccd0eb57b 100644
  25125. --- a/kernel/trace/Kconfig
  25126. +++ b/kernel/trace/Kconfig
  25127. @@ -187,6 +187,24 @@ config IRQSOFF_TRACER
  25128. enabled. This option and the preempt-off timing option can be
  25129. used together or separately.)
  25130. +config INTERRUPT_OFF_HIST
  25131. + bool "Interrupts-off Latency Histogram"
  25132. + depends on IRQSOFF_TRACER
  25133. + help
  25134. + This option generates continuously updated histograms (one per cpu)
  25135. + of the duration of time periods with interrupts disabled. The
  25136. + histograms are disabled by default. To enable them, write a non-zero
  25137. + number to
  25138. +
  25139. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  25140. +
  25141. + If PREEMPT_OFF_HIST is also selected, additional histograms (one
  25142. + per cpu) are generated that accumulate the duration of time periods
  25143. + when both interrupts and preemption are disabled. The histogram data
  25144. + will be located in the debug file system at
  25145. +
  25146. + /sys/kernel/debug/tracing/latency_hist/irqsoff
  25147. +
  25148. config PREEMPT_TRACER
  25149. bool "Preemption-off Latency Tracer"
  25150. default n
  25151. @@ -211,6 +229,24 @@ config PREEMPT_TRACER
  25152. enabled. This option and the irqs-off timing option can be
  25153. used together or separately.)
  25154. +config PREEMPT_OFF_HIST
  25155. + bool "Preemption-off Latency Histogram"
  25156. + depends on PREEMPT_TRACER
  25157. + help
  25158. + This option generates continuously updated histograms (one per cpu)
  25159. + of the duration of time periods with preemption disabled. The
  25160. + histograms are disabled by default. To enable them, write a non-zero
  25161. + number to
  25162. +
  25163. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  25164. +
  25165. + If INTERRUPT_OFF_HIST is also selected, additional histograms (one
  25166. + per cpu) are generated that accumulate the duration of time periods
  25167. + when both interrupts and preemption are disabled. The histogram data
  25168. + will be located in the debug file system at
  25169. +
  25170. + /sys/kernel/debug/tracing/latency_hist/preemptoff
  25171. +
  25172. config SCHED_TRACER
  25173. bool "Scheduling Latency Tracer"
  25174. select GENERIC_TRACER
  25175. @@ -221,6 +257,74 @@ config SCHED_TRACER
  25176. This tracer tracks the latency of the highest priority task
  25177. to be scheduled in, starting from the point it has woken up.
  25178. +config WAKEUP_LATENCY_HIST
  25179. + bool "Scheduling Latency Histogram"
  25180. + depends on SCHED_TRACER
  25181. + help
  25182. + This option generates continuously updated histograms (one per cpu)
  25183. + of the scheduling latency of the highest priority task.
  25184. + The histograms are disabled by default. To enable them, write a
  25185. + non-zero number to
  25186. +
  25187. + /sys/kernel/debug/tracing/latency_hist/enable/wakeup
  25188. +
  25189. + Two different algorithms are used, one to determine the latency of
  25190. + processes that exclusively use the highest priority of the system and
  25191. + another one to determine the latency of processes that share the
  25192. + highest system priority with other processes. The former is used to
  25193. + improve hardware and system software, the latter to optimize the
  25194. + priority design of a given system. The histogram data will be
  25195. + located in the debug file system at
  25196. +
  25197. + /sys/kernel/debug/tracing/latency_hist/wakeup
  25198. +
  25199. + and
  25200. +
  25201. + /sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio
  25202. +
  25203. + If both Scheduling Latency Histogram and Missed Timer Offsets
  25204. + Histogram are selected, additional histogram data will be collected
  25205. + that contain, in addition to the wakeup latency, the timer latency, in
  25206. + case the wakeup was triggered by an expired timer. These histograms
  25207. + are available in the
  25208. +
  25209. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  25210. +
  25211. + directory. They reflect the apparent interrupt and scheduling latency
  25212. + and are best suitable to determine the worst-case latency of a given
  25213. + system. To enable these histograms, write a non-zero number to
  25214. +
  25215. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  25216. +
  25217. +config MISSED_TIMER_OFFSETS_HIST
  25218. + depends on HIGH_RES_TIMERS
  25219. + select GENERIC_TRACER
  25220. + bool "Missed Timer Offsets Histogram"
  25221. + help
  25222. + Generate a histogram of missed timer offsets in microseconds. The
  25223. + histograms are disabled by default. To enable them, write a non-zero
  25224. + number to
  25225. +
  25226. + /sys/kernel/debug/tracing/latency_hist/enable/missed_timer_offsets
  25227. +
  25228. + The histogram data will be located in the debug file system at
  25229. +
  25230. + /sys/kernel/debug/tracing/latency_hist/missed_timer_offsets
  25231. +
  25232. + If both Scheduling Latency Histogram and Missed Timer Offsets
  25233. + Histogram are selected, additional histogram data will be collected
  25234. + that contain, in addition to the wakeup latency, the timer latency, in
  25235. + case the wakeup was triggered by an expired timer. These histograms
  25236. + are available in the
  25237. +
  25238. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  25239. +
  25240. + directory. They reflect the apparent interrupt and scheduling latency
  25241. + and are best suitable to determine the worst-case latency of a given
  25242. + system. To enable these histograms, write a non-zero number to
  25243. +
  25244. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  25245. +
  25246. config ENABLE_DEFAULT_TRACERS
  25247. bool "Trace process context switches and events"
  25248. depends on !GENERIC_TRACER
  25249. diff --git a/kernel/trace/Makefile b/kernel/trace/Makefile
  25250. index 05ea5167e6bb..bc08c67301ae 100644
  25251. --- a/kernel/trace/Makefile
  25252. +++ b/kernel/trace/Makefile
  25253. @@ -40,6 +40,10 @@ obj-$(CONFIG_FUNCTION_TRACER) += trace_functions.o
  25254. obj-$(CONFIG_IRQSOFF_TRACER) += trace_irqsoff.o
  25255. obj-$(CONFIG_PREEMPT_TRACER) += trace_irqsoff.o
  25256. obj-$(CONFIG_SCHED_TRACER) += trace_sched_wakeup.o
  25257. +obj-$(CONFIG_INTERRUPT_OFF_HIST) += latency_hist.o
  25258. +obj-$(CONFIG_PREEMPT_OFF_HIST) += latency_hist.o
  25259. +obj-$(CONFIG_WAKEUP_LATENCY_HIST) += latency_hist.o
  25260. +obj-$(CONFIG_MISSED_TIMER_OFFSETS_HIST) += latency_hist.o
  25261. obj-$(CONFIG_NOP_TRACER) += trace_nop.o
  25262. obj-$(CONFIG_STACK_TRACER) += trace_stack.o
  25263. obj-$(CONFIG_MMIOTRACE) += trace_mmiotrace.o
  25264. diff --git a/kernel/trace/latency_hist.c b/kernel/trace/latency_hist.c
  25265. new file mode 100644
  25266. index 000000000000..7f6ee70dea41
  25267. --- /dev/null
  25268. +++ b/kernel/trace/latency_hist.c
  25269. @@ -0,0 +1,1178 @@
  25270. +/*
  25271. + * kernel/trace/latency_hist.c
  25272. + *
  25273. + * Add support for histograms of preemption-off latency and
  25274. + * interrupt-off latency and wakeup latency, it depends on
  25275. + * Real-Time Preemption Support.
  25276. + *
  25277. + * Copyright (C) 2005 MontaVista Software, Inc.
  25278. + * Yi Yang <yyang@ch.mvista.com>
  25279. + *
  25280. + * Converted to work with the new latency tracer.
  25281. + * Copyright (C) 2008 Red Hat, Inc.
  25282. + * Steven Rostedt <srostedt@redhat.com>
  25283. + *
  25284. + */
  25285. +#include <linux/module.h>
  25286. +#include <linux/debugfs.h>
  25287. +#include <linux/seq_file.h>
  25288. +#include <linux/percpu.h>
  25289. +#include <linux/kallsyms.h>
  25290. +#include <linux/uaccess.h>
  25291. +#include <linux/sched.h>
  25292. +#include <linux/sched/rt.h>
  25293. +#include <linux/slab.h>
  25294. +#include <linux/atomic.h>
  25295. +#include <asm/div64.h>
  25296. +
  25297. +#include "trace.h"
  25298. +#include <trace/events/sched.h>
  25299. +
  25300. +#define NSECS_PER_USECS 1000L
  25301. +
  25302. +#define CREATE_TRACE_POINTS
  25303. +#include <trace/events/hist.h>
  25304. +
  25305. +enum {
  25306. + IRQSOFF_LATENCY = 0,
  25307. + PREEMPTOFF_LATENCY,
  25308. + PREEMPTIRQSOFF_LATENCY,
  25309. + WAKEUP_LATENCY,
  25310. + WAKEUP_LATENCY_SHAREDPRIO,
  25311. + MISSED_TIMER_OFFSETS,
  25312. + TIMERANDWAKEUP_LATENCY,
  25313. + MAX_LATENCY_TYPE,
  25314. +};
  25315. +
  25316. +#define MAX_ENTRY_NUM 10240
  25317. +
  25318. +struct hist_data {
  25319. + atomic_t hist_mode; /* 0 log, 1 don't log */
  25320. + long offset; /* set it to MAX_ENTRY_NUM/2 for a bipolar scale */
  25321. + long min_lat;
  25322. + long max_lat;
  25323. + unsigned long long below_hist_bound_samples;
  25324. + unsigned long long above_hist_bound_samples;
  25325. + long long accumulate_lat;
  25326. + unsigned long long total_samples;
  25327. + unsigned long long hist_array[MAX_ENTRY_NUM];
  25328. +};
  25329. +
  25330. +struct enable_data {
  25331. + int latency_type;
  25332. + int enabled;
  25333. +};
  25334. +
  25335. +static char *latency_hist_dir_root = "latency_hist";
  25336. +
  25337. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  25338. +static DEFINE_PER_CPU(struct hist_data, irqsoff_hist);
  25339. +static char *irqsoff_hist_dir = "irqsoff";
  25340. +static DEFINE_PER_CPU(cycles_t, hist_irqsoff_start);
  25341. +static DEFINE_PER_CPU(int, hist_irqsoff_counting);
  25342. +#endif
  25343. +
  25344. +#ifdef CONFIG_PREEMPT_OFF_HIST
  25345. +static DEFINE_PER_CPU(struct hist_data, preemptoff_hist);
  25346. +static char *preemptoff_hist_dir = "preemptoff";
  25347. +static DEFINE_PER_CPU(cycles_t, hist_preemptoff_start);
  25348. +static DEFINE_PER_CPU(int, hist_preemptoff_counting);
  25349. +#endif
  25350. +
  25351. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  25352. +static DEFINE_PER_CPU(struct hist_data, preemptirqsoff_hist);
  25353. +static char *preemptirqsoff_hist_dir = "preemptirqsoff";
  25354. +static DEFINE_PER_CPU(cycles_t, hist_preemptirqsoff_start);
  25355. +static DEFINE_PER_CPU(int, hist_preemptirqsoff_counting);
  25356. +#endif
  25357. +
  25358. +#if defined(CONFIG_PREEMPT_OFF_HIST) || defined(CONFIG_INTERRUPT_OFF_HIST)
  25359. +static notrace void probe_preemptirqsoff_hist(void *v, int reason, int start);
  25360. +static struct enable_data preemptirqsoff_enabled_data = {
  25361. + .latency_type = PREEMPTIRQSOFF_LATENCY,
  25362. + .enabled = 0,
  25363. +};
  25364. +#endif
  25365. +
  25366. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25367. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25368. +struct maxlatproc_data {
  25369. + char comm[FIELD_SIZEOF(struct task_struct, comm)];
  25370. + char current_comm[FIELD_SIZEOF(struct task_struct, comm)];
  25371. + int pid;
  25372. + int current_pid;
  25373. + int prio;
  25374. + int current_prio;
  25375. + long latency;
  25376. + long timeroffset;
  25377. + cycle_t timestamp;
  25378. +};
  25379. +#endif
  25380. +
  25381. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25382. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist);
  25383. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist_sharedprio);
  25384. +static char *wakeup_latency_hist_dir = "wakeup";
  25385. +static char *wakeup_latency_hist_dir_sharedprio = "sharedprio";
  25386. +static notrace void probe_wakeup_latency_hist_start(void *v,
  25387. + struct task_struct *p);
  25388. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  25389. + bool preempt, struct task_struct *prev, struct task_struct *next);
  25390. +static notrace void probe_sched_migrate_task(void *,
  25391. + struct task_struct *task, int cpu);
  25392. +static struct enable_data wakeup_latency_enabled_data = {
  25393. + .latency_type = WAKEUP_LATENCY,
  25394. + .enabled = 0,
  25395. +};
  25396. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc);
  25397. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc_sharedprio);
  25398. +static DEFINE_PER_CPU(struct task_struct *, wakeup_task);
  25399. +static DEFINE_PER_CPU(int, wakeup_sharedprio);
  25400. +static unsigned long wakeup_pid;
  25401. +#endif
  25402. +
  25403. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25404. +static DEFINE_PER_CPU(struct hist_data, missed_timer_offsets);
  25405. +static char *missed_timer_offsets_dir = "missed_timer_offsets";
  25406. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  25407. + long long offset, struct task_struct *curr, struct task_struct *task);
  25408. +static struct enable_data missed_timer_offsets_enabled_data = {
  25409. + .latency_type = MISSED_TIMER_OFFSETS,
  25410. + .enabled = 0,
  25411. +};
  25412. +static DEFINE_PER_CPU(struct maxlatproc_data, missed_timer_offsets_maxlatproc);
  25413. +static unsigned long missed_timer_offsets_pid;
  25414. +#endif
  25415. +
  25416. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  25417. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25418. +static DEFINE_PER_CPU(struct hist_data, timerandwakeup_latency_hist);
  25419. +static char *timerandwakeup_latency_hist_dir = "timerandwakeup";
  25420. +static struct enable_data timerandwakeup_enabled_data = {
  25421. + .latency_type = TIMERANDWAKEUP_LATENCY,
  25422. + .enabled = 0,
  25423. +};
  25424. +static DEFINE_PER_CPU(struct maxlatproc_data, timerandwakeup_maxlatproc);
  25425. +#endif
  25426. +
  25427. +void notrace latency_hist(int latency_type, int cpu, long latency,
  25428. + long timeroffset, cycle_t stop,
  25429. + struct task_struct *p)
  25430. +{
  25431. + struct hist_data *my_hist;
  25432. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25433. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25434. + struct maxlatproc_data *mp = NULL;
  25435. +#endif
  25436. +
  25437. + if (!cpu_possible(cpu) || latency_type < 0 ||
  25438. + latency_type >= MAX_LATENCY_TYPE)
  25439. + return;
  25440. +
  25441. + switch (latency_type) {
  25442. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  25443. + case IRQSOFF_LATENCY:
  25444. + my_hist = &per_cpu(irqsoff_hist, cpu);
  25445. + break;
  25446. +#endif
  25447. +#ifdef CONFIG_PREEMPT_OFF_HIST
  25448. + case PREEMPTOFF_LATENCY:
  25449. + my_hist = &per_cpu(preemptoff_hist, cpu);
  25450. + break;
  25451. +#endif
  25452. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  25453. + case PREEMPTIRQSOFF_LATENCY:
  25454. + my_hist = &per_cpu(preemptirqsoff_hist, cpu);
  25455. + break;
  25456. +#endif
  25457. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25458. + case WAKEUP_LATENCY:
  25459. + my_hist = &per_cpu(wakeup_latency_hist, cpu);
  25460. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  25461. + break;
  25462. + case WAKEUP_LATENCY_SHAREDPRIO:
  25463. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  25464. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  25465. + break;
  25466. +#endif
  25467. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25468. + case MISSED_TIMER_OFFSETS:
  25469. + my_hist = &per_cpu(missed_timer_offsets, cpu);
  25470. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  25471. + break;
  25472. +#endif
  25473. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  25474. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25475. + case TIMERANDWAKEUP_LATENCY:
  25476. + my_hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  25477. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  25478. + break;
  25479. +#endif
  25480. +
  25481. + default:
  25482. + return;
  25483. + }
  25484. +
  25485. + latency += my_hist->offset;
  25486. +
  25487. + if (atomic_read(&my_hist->hist_mode) == 0)
  25488. + return;
  25489. +
  25490. + if (latency < 0 || latency >= MAX_ENTRY_NUM) {
  25491. + if (latency < 0)
  25492. + my_hist->below_hist_bound_samples++;
  25493. + else
  25494. + my_hist->above_hist_bound_samples++;
  25495. + } else
  25496. + my_hist->hist_array[latency]++;
  25497. +
  25498. + if (unlikely(latency > my_hist->max_lat ||
  25499. + my_hist->min_lat == LONG_MAX)) {
  25500. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25501. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25502. + if (latency_type == WAKEUP_LATENCY ||
  25503. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  25504. + latency_type == MISSED_TIMER_OFFSETS ||
  25505. + latency_type == TIMERANDWAKEUP_LATENCY) {
  25506. + strncpy(mp->comm, p->comm, sizeof(mp->comm));
  25507. + strncpy(mp->current_comm, current->comm,
  25508. + sizeof(mp->current_comm));
  25509. + mp->pid = task_pid_nr(p);
  25510. + mp->current_pid = task_pid_nr(current);
  25511. + mp->prio = p->prio;
  25512. + mp->current_prio = current->prio;
  25513. + mp->latency = latency;
  25514. + mp->timeroffset = timeroffset;
  25515. + mp->timestamp = stop;
  25516. + }
  25517. +#endif
  25518. + my_hist->max_lat = latency;
  25519. + }
  25520. + if (unlikely(latency < my_hist->min_lat))
  25521. + my_hist->min_lat = latency;
  25522. + my_hist->total_samples++;
  25523. + my_hist->accumulate_lat += latency;
  25524. +}
  25525. +
  25526. +static void *l_start(struct seq_file *m, loff_t *pos)
  25527. +{
  25528. + loff_t *index_ptr = NULL;
  25529. + loff_t index = *pos;
  25530. + struct hist_data *my_hist = m->private;
  25531. +
  25532. + if (index == 0) {
  25533. + char minstr[32], avgstr[32], maxstr[32];
  25534. +
  25535. + atomic_dec(&my_hist->hist_mode);
  25536. +
  25537. + if (likely(my_hist->total_samples)) {
  25538. + long avg = (long) div64_s64(my_hist->accumulate_lat,
  25539. + my_hist->total_samples);
  25540. + snprintf(minstr, sizeof(minstr), "%ld",
  25541. + my_hist->min_lat - my_hist->offset);
  25542. + snprintf(avgstr, sizeof(avgstr), "%ld",
  25543. + avg - my_hist->offset);
  25544. + snprintf(maxstr, sizeof(maxstr), "%ld",
  25545. + my_hist->max_lat - my_hist->offset);
  25546. + } else {
  25547. + strcpy(minstr, "<undef>");
  25548. + strcpy(avgstr, minstr);
  25549. + strcpy(maxstr, minstr);
  25550. + }
  25551. +
  25552. + seq_printf(m, "#Minimum latency: %s microseconds\n"
  25553. + "#Average latency: %s microseconds\n"
  25554. + "#Maximum latency: %s microseconds\n"
  25555. + "#Total samples: %llu\n"
  25556. + "#There are %llu samples lower than %ld"
  25557. + " microseconds.\n"
  25558. + "#There are %llu samples greater or equal"
  25559. + " than %ld microseconds.\n"
  25560. + "#usecs\t%16s\n",
  25561. + minstr, avgstr, maxstr,
  25562. + my_hist->total_samples,
  25563. + my_hist->below_hist_bound_samples,
  25564. + -my_hist->offset,
  25565. + my_hist->above_hist_bound_samples,
  25566. + MAX_ENTRY_NUM - my_hist->offset,
  25567. + "samples");
  25568. + }
  25569. + if (index < MAX_ENTRY_NUM) {
  25570. + index_ptr = kmalloc(sizeof(loff_t), GFP_KERNEL);
  25571. + if (index_ptr)
  25572. + *index_ptr = index;
  25573. + }
  25574. +
  25575. + return index_ptr;
  25576. +}
  25577. +
  25578. +static void *l_next(struct seq_file *m, void *p, loff_t *pos)
  25579. +{
  25580. + loff_t *index_ptr = p;
  25581. + struct hist_data *my_hist = m->private;
  25582. +
  25583. + if (++*pos >= MAX_ENTRY_NUM) {
  25584. + atomic_inc(&my_hist->hist_mode);
  25585. + return NULL;
  25586. + }
  25587. + *index_ptr = *pos;
  25588. + return index_ptr;
  25589. +}
  25590. +
  25591. +static void l_stop(struct seq_file *m, void *p)
  25592. +{
  25593. + kfree(p);
  25594. +}
  25595. +
  25596. +static int l_show(struct seq_file *m, void *p)
  25597. +{
  25598. + int index = *(loff_t *) p;
  25599. + struct hist_data *my_hist = m->private;
  25600. +
  25601. + seq_printf(m, "%6ld\t%16llu\n", index - my_hist->offset,
  25602. + my_hist->hist_array[index]);
  25603. + return 0;
  25604. +}
  25605. +
  25606. +static const struct seq_operations latency_hist_seq_op = {
  25607. + .start = l_start,
  25608. + .next = l_next,
  25609. + .stop = l_stop,
  25610. + .show = l_show
  25611. +};
  25612. +
  25613. +static int latency_hist_open(struct inode *inode, struct file *file)
  25614. +{
  25615. + int ret;
  25616. +
  25617. + ret = seq_open(file, &latency_hist_seq_op);
  25618. + if (!ret) {
  25619. + struct seq_file *seq = file->private_data;
  25620. + seq->private = inode->i_private;
  25621. + }
  25622. + return ret;
  25623. +}
  25624. +
  25625. +static const struct file_operations latency_hist_fops = {
  25626. + .open = latency_hist_open,
  25627. + .read = seq_read,
  25628. + .llseek = seq_lseek,
  25629. + .release = seq_release,
  25630. +};
  25631. +
  25632. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25633. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25634. +static void clear_maxlatprocdata(struct maxlatproc_data *mp)
  25635. +{
  25636. + mp->comm[0] = mp->current_comm[0] = '\0';
  25637. + mp->prio = mp->current_prio = mp->pid = mp->current_pid =
  25638. + mp->latency = mp->timeroffset = -1;
  25639. + mp->timestamp = 0;
  25640. +}
  25641. +#endif
  25642. +
  25643. +static void hist_reset(struct hist_data *hist)
  25644. +{
  25645. + atomic_dec(&hist->hist_mode);
  25646. +
  25647. + memset(hist->hist_array, 0, sizeof(hist->hist_array));
  25648. + hist->below_hist_bound_samples = 0ULL;
  25649. + hist->above_hist_bound_samples = 0ULL;
  25650. + hist->min_lat = LONG_MAX;
  25651. + hist->max_lat = LONG_MIN;
  25652. + hist->total_samples = 0ULL;
  25653. + hist->accumulate_lat = 0LL;
  25654. +
  25655. + atomic_inc(&hist->hist_mode);
  25656. +}
  25657. +
  25658. +static ssize_t
  25659. +latency_hist_reset(struct file *file, const char __user *a,
  25660. + size_t size, loff_t *off)
  25661. +{
  25662. + int cpu;
  25663. + struct hist_data *hist = NULL;
  25664. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25665. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25666. + struct maxlatproc_data *mp = NULL;
  25667. +#endif
  25668. + off_t latency_type = (off_t) file->private_data;
  25669. +
  25670. + for_each_online_cpu(cpu) {
  25671. +
  25672. + switch (latency_type) {
  25673. +#ifdef CONFIG_PREEMPT_OFF_HIST
  25674. + case PREEMPTOFF_LATENCY:
  25675. + hist = &per_cpu(preemptoff_hist, cpu);
  25676. + break;
  25677. +#endif
  25678. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  25679. + case IRQSOFF_LATENCY:
  25680. + hist = &per_cpu(irqsoff_hist, cpu);
  25681. + break;
  25682. +#endif
  25683. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  25684. + case PREEMPTIRQSOFF_LATENCY:
  25685. + hist = &per_cpu(preemptirqsoff_hist, cpu);
  25686. + break;
  25687. +#endif
  25688. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25689. + case WAKEUP_LATENCY:
  25690. + hist = &per_cpu(wakeup_latency_hist, cpu);
  25691. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  25692. + break;
  25693. + case WAKEUP_LATENCY_SHAREDPRIO:
  25694. + hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  25695. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  25696. + break;
  25697. +#endif
  25698. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25699. + case MISSED_TIMER_OFFSETS:
  25700. + hist = &per_cpu(missed_timer_offsets, cpu);
  25701. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  25702. + break;
  25703. +#endif
  25704. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  25705. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25706. + case TIMERANDWAKEUP_LATENCY:
  25707. + hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  25708. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  25709. + break;
  25710. +#endif
  25711. + }
  25712. +
  25713. + hist_reset(hist);
  25714. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25715. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25716. + if (latency_type == WAKEUP_LATENCY ||
  25717. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  25718. + latency_type == MISSED_TIMER_OFFSETS ||
  25719. + latency_type == TIMERANDWAKEUP_LATENCY)
  25720. + clear_maxlatprocdata(mp);
  25721. +#endif
  25722. + }
  25723. +
  25724. + return size;
  25725. +}
  25726. +
  25727. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25728. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25729. +static ssize_t
  25730. +show_pid(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  25731. +{
  25732. + char buf[64];
  25733. + int r;
  25734. + unsigned long *this_pid = file->private_data;
  25735. +
  25736. + r = snprintf(buf, sizeof(buf), "%lu\n", *this_pid);
  25737. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  25738. +}
  25739. +
  25740. +static ssize_t do_pid(struct file *file, const char __user *ubuf,
  25741. + size_t cnt, loff_t *ppos)
  25742. +{
  25743. + char buf[64];
  25744. + unsigned long pid;
  25745. + unsigned long *this_pid = file->private_data;
  25746. +
  25747. + if (cnt >= sizeof(buf))
  25748. + return -EINVAL;
  25749. +
  25750. + if (copy_from_user(&buf, ubuf, cnt))
  25751. + return -EFAULT;
  25752. +
  25753. + buf[cnt] = '\0';
  25754. +
  25755. + if (kstrtoul(buf, 10, &pid))
  25756. + return -EINVAL;
  25757. +
  25758. + *this_pid = pid;
  25759. +
  25760. + return cnt;
  25761. +}
  25762. +#endif
  25763. +
  25764. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25765. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25766. +static ssize_t
  25767. +show_maxlatproc(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  25768. +{
  25769. + int r;
  25770. + struct maxlatproc_data *mp = file->private_data;
  25771. + int strmaxlen = (TASK_COMM_LEN * 2) + (8 * 8);
  25772. + unsigned long long t;
  25773. + unsigned long usecs, secs;
  25774. + char *buf;
  25775. +
  25776. + if (mp->pid == -1 || mp->current_pid == -1) {
  25777. + buf = "(none)\n";
  25778. + return simple_read_from_buffer(ubuf, cnt, ppos, buf,
  25779. + strlen(buf));
  25780. + }
  25781. +
  25782. + buf = kmalloc(strmaxlen, GFP_KERNEL);
  25783. + if (buf == NULL)
  25784. + return -ENOMEM;
  25785. +
  25786. + t = ns2usecs(mp->timestamp);
  25787. + usecs = do_div(t, USEC_PER_SEC);
  25788. + secs = (unsigned long) t;
  25789. + r = snprintf(buf, strmaxlen,
  25790. + "%d %d %ld (%ld) %s <- %d %d %s %lu.%06lu\n", mp->pid,
  25791. + MAX_RT_PRIO-1 - mp->prio, mp->latency, mp->timeroffset, mp->comm,
  25792. + mp->current_pid, MAX_RT_PRIO-1 - mp->current_prio, mp->current_comm,
  25793. + secs, usecs);
  25794. + r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  25795. + kfree(buf);
  25796. + return r;
  25797. +}
  25798. +#endif
  25799. +
  25800. +static ssize_t
  25801. +show_enable(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  25802. +{
  25803. + char buf[64];
  25804. + struct enable_data *ed = file->private_data;
  25805. + int r;
  25806. +
  25807. + r = snprintf(buf, sizeof(buf), "%d\n", ed->enabled);
  25808. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  25809. +}
  25810. +
  25811. +static ssize_t
  25812. +do_enable(struct file *file, const char __user *ubuf, size_t cnt, loff_t *ppos)
  25813. +{
  25814. + char buf[64];
  25815. + long enable;
  25816. + struct enable_data *ed = file->private_data;
  25817. +
  25818. + if (cnt >= sizeof(buf))
  25819. + return -EINVAL;
  25820. +
  25821. + if (copy_from_user(&buf, ubuf, cnt))
  25822. + return -EFAULT;
  25823. +
  25824. + buf[cnt] = 0;
  25825. +
  25826. + if (kstrtoul(buf, 10, &enable))
  25827. + return -EINVAL;
  25828. +
  25829. + if ((enable && ed->enabled) || (!enable && !ed->enabled))
  25830. + return cnt;
  25831. +
  25832. + if (enable) {
  25833. + int ret;
  25834. +
  25835. + switch (ed->latency_type) {
  25836. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  25837. + case PREEMPTIRQSOFF_LATENCY:
  25838. + ret = register_trace_preemptirqsoff_hist(
  25839. + probe_preemptirqsoff_hist, NULL);
  25840. + if (ret) {
  25841. + pr_info("wakeup trace: Couldn't assign "
  25842. + "probe_preemptirqsoff_hist "
  25843. + "to trace_preemptirqsoff_hist\n");
  25844. + return ret;
  25845. + }
  25846. + break;
  25847. +#endif
  25848. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25849. + case WAKEUP_LATENCY:
  25850. + ret = register_trace_sched_wakeup(
  25851. + probe_wakeup_latency_hist_start, NULL);
  25852. + if (ret) {
  25853. + pr_info("wakeup trace: Couldn't assign "
  25854. + "probe_wakeup_latency_hist_start "
  25855. + "to trace_sched_wakeup\n");
  25856. + return ret;
  25857. + }
  25858. + ret = register_trace_sched_wakeup_new(
  25859. + probe_wakeup_latency_hist_start, NULL);
  25860. + if (ret) {
  25861. + pr_info("wakeup trace: Couldn't assign "
  25862. + "probe_wakeup_latency_hist_start "
  25863. + "to trace_sched_wakeup_new\n");
  25864. + unregister_trace_sched_wakeup(
  25865. + probe_wakeup_latency_hist_start, NULL);
  25866. + return ret;
  25867. + }
  25868. + ret = register_trace_sched_switch(
  25869. + probe_wakeup_latency_hist_stop, NULL);
  25870. + if (ret) {
  25871. + pr_info("wakeup trace: Couldn't assign "
  25872. + "probe_wakeup_latency_hist_stop "
  25873. + "to trace_sched_switch\n");
  25874. + unregister_trace_sched_wakeup(
  25875. + probe_wakeup_latency_hist_start, NULL);
  25876. + unregister_trace_sched_wakeup_new(
  25877. + probe_wakeup_latency_hist_start, NULL);
  25878. + return ret;
  25879. + }
  25880. + ret = register_trace_sched_migrate_task(
  25881. + probe_sched_migrate_task, NULL);
  25882. + if (ret) {
  25883. + pr_info("wakeup trace: Couldn't assign "
  25884. + "probe_sched_migrate_task "
  25885. + "to trace_sched_migrate_task\n");
  25886. + unregister_trace_sched_wakeup(
  25887. + probe_wakeup_latency_hist_start, NULL);
  25888. + unregister_trace_sched_wakeup_new(
  25889. + probe_wakeup_latency_hist_start, NULL);
  25890. + unregister_trace_sched_switch(
  25891. + probe_wakeup_latency_hist_stop, NULL);
  25892. + return ret;
  25893. + }
  25894. + break;
  25895. +#endif
  25896. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25897. + case MISSED_TIMER_OFFSETS:
  25898. + ret = register_trace_hrtimer_interrupt(
  25899. + probe_hrtimer_interrupt, NULL);
  25900. + if (ret) {
  25901. + pr_info("wakeup trace: Couldn't assign "
  25902. + "probe_hrtimer_interrupt "
  25903. + "to trace_hrtimer_interrupt\n");
  25904. + return ret;
  25905. + }
  25906. + break;
  25907. +#endif
  25908. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  25909. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25910. + case TIMERANDWAKEUP_LATENCY:
  25911. + if (!wakeup_latency_enabled_data.enabled ||
  25912. + !missed_timer_offsets_enabled_data.enabled)
  25913. + return -EINVAL;
  25914. + break;
  25915. +#endif
  25916. + default:
  25917. + break;
  25918. + }
  25919. + } else {
  25920. + switch (ed->latency_type) {
  25921. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  25922. + case PREEMPTIRQSOFF_LATENCY:
  25923. + {
  25924. + int cpu;
  25925. +
  25926. + unregister_trace_preemptirqsoff_hist(
  25927. + probe_preemptirqsoff_hist, NULL);
  25928. + for_each_online_cpu(cpu) {
  25929. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  25930. + per_cpu(hist_irqsoff_counting,
  25931. + cpu) = 0;
  25932. +#endif
  25933. +#ifdef CONFIG_PREEMPT_OFF_HIST
  25934. + per_cpu(hist_preemptoff_counting,
  25935. + cpu) = 0;
  25936. +#endif
  25937. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  25938. + per_cpu(hist_preemptirqsoff_counting,
  25939. + cpu) = 0;
  25940. +#endif
  25941. + }
  25942. + }
  25943. + break;
  25944. +#endif
  25945. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25946. + case WAKEUP_LATENCY:
  25947. + {
  25948. + int cpu;
  25949. +
  25950. + unregister_trace_sched_wakeup(
  25951. + probe_wakeup_latency_hist_start, NULL);
  25952. + unregister_trace_sched_wakeup_new(
  25953. + probe_wakeup_latency_hist_start, NULL);
  25954. + unregister_trace_sched_switch(
  25955. + probe_wakeup_latency_hist_stop, NULL);
  25956. + unregister_trace_sched_migrate_task(
  25957. + probe_sched_migrate_task, NULL);
  25958. +
  25959. + for_each_online_cpu(cpu) {
  25960. + per_cpu(wakeup_task, cpu) = NULL;
  25961. + per_cpu(wakeup_sharedprio, cpu) = 0;
  25962. + }
  25963. + }
  25964. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25965. + timerandwakeup_enabled_data.enabled = 0;
  25966. +#endif
  25967. + break;
  25968. +#endif
  25969. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25970. + case MISSED_TIMER_OFFSETS:
  25971. + unregister_trace_hrtimer_interrupt(
  25972. + probe_hrtimer_interrupt, NULL);
  25973. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25974. + timerandwakeup_enabled_data.enabled = 0;
  25975. +#endif
  25976. + break;
  25977. +#endif
  25978. + default:
  25979. + break;
  25980. + }
  25981. + }
  25982. + ed->enabled = enable;
  25983. + return cnt;
  25984. +}
  25985. +
  25986. +static const struct file_operations latency_hist_reset_fops = {
  25987. + .open = tracing_open_generic,
  25988. + .write = latency_hist_reset,
  25989. +};
  25990. +
  25991. +static const struct file_operations enable_fops = {
  25992. + .open = tracing_open_generic,
  25993. + .read = show_enable,
  25994. + .write = do_enable,
  25995. +};
  25996. +
  25997. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25998. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25999. +static const struct file_operations pid_fops = {
  26000. + .open = tracing_open_generic,
  26001. + .read = show_pid,
  26002. + .write = do_pid,
  26003. +};
  26004. +
  26005. +static const struct file_operations maxlatproc_fops = {
  26006. + .open = tracing_open_generic,
  26007. + .read = show_maxlatproc,
  26008. +};
  26009. +#endif
  26010. +
  26011. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  26012. +static notrace void probe_preemptirqsoff_hist(void *v, int reason,
  26013. + int starthist)
  26014. +{
  26015. + int cpu = raw_smp_processor_id();
  26016. + int time_set = 0;
  26017. +
  26018. + if (starthist) {
  26019. + cycle_t uninitialized_var(start);
  26020. +
  26021. + if (!preempt_count() && !irqs_disabled())
  26022. + return;
  26023. +
  26024. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  26025. + if ((reason == IRQS_OFF || reason == TRACE_START) &&
  26026. + !per_cpu(hist_irqsoff_counting, cpu)) {
  26027. + per_cpu(hist_irqsoff_counting, cpu) = 1;
  26028. + start = ftrace_now(cpu);
  26029. + time_set++;
  26030. + per_cpu(hist_irqsoff_start, cpu) = start;
  26031. + }
  26032. +#endif
  26033. +
  26034. +#ifdef CONFIG_PREEMPT_OFF_HIST
  26035. + if ((reason == PREEMPT_OFF || reason == TRACE_START) &&
  26036. + !per_cpu(hist_preemptoff_counting, cpu)) {
  26037. + per_cpu(hist_preemptoff_counting, cpu) = 1;
  26038. + if (!(time_set++))
  26039. + start = ftrace_now(cpu);
  26040. + per_cpu(hist_preemptoff_start, cpu) = start;
  26041. + }
  26042. +#endif
  26043. +
  26044. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  26045. + if (per_cpu(hist_irqsoff_counting, cpu) &&
  26046. + per_cpu(hist_preemptoff_counting, cpu) &&
  26047. + !per_cpu(hist_preemptirqsoff_counting, cpu)) {
  26048. + per_cpu(hist_preemptirqsoff_counting, cpu) = 1;
  26049. + if (!time_set)
  26050. + start = ftrace_now(cpu);
  26051. + per_cpu(hist_preemptirqsoff_start, cpu) = start;
  26052. + }
  26053. +#endif
  26054. + } else {
  26055. + cycle_t uninitialized_var(stop);
  26056. +
  26057. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  26058. + if ((reason == IRQS_ON || reason == TRACE_STOP) &&
  26059. + per_cpu(hist_irqsoff_counting, cpu)) {
  26060. + cycle_t start = per_cpu(hist_irqsoff_start, cpu);
  26061. +
  26062. + stop = ftrace_now(cpu);
  26063. + time_set++;
  26064. + if (start) {
  26065. + long latency = ((long) (stop - start)) /
  26066. + NSECS_PER_USECS;
  26067. +
  26068. + latency_hist(IRQSOFF_LATENCY, cpu, latency, 0,
  26069. + stop, NULL);
  26070. + }
  26071. + per_cpu(hist_irqsoff_counting, cpu) = 0;
  26072. + }
  26073. +#endif
  26074. +
  26075. +#ifdef CONFIG_PREEMPT_OFF_HIST
  26076. + if ((reason == PREEMPT_ON || reason == TRACE_STOP) &&
  26077. + per_cpu(hist_preemptoff_counting, cpu)) {
  26078. + cycle_t start = per_cpu(hist_preemptoff_start, cpu);
  26079. +
  26080. + if (!(time_set++))
  26081. + stop = ftrace_now(cpu);
  26082. + if (start) {
  26083. + long latency = ((long) (stop - start)) /
  26084. + NSECS_PER_USECS;
  26085. +
  26086. + latency_hist(PREEMPTOFF_LATENCY, cpu, latency,
  26087. + 0, stop, NULL);
  26088. + }
  26089. + per_cpu(hist_preemptoff_counting, cpu) = 0;
  26090. + }
  26091. +#endif
  26092. +
  26093. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  26094. + if ((!per_cpu(hist_irqsoff_counting, cpu) ||
  26095. + !per_cpu(hist_preemptoff_counting, cpu)) &&
  26096. + per_cpu(hist_preemptirqsoff_counting, cpu)) {
  26097. + cycle_t start = per_cpu(hist_preemptirqsoff_start, cpu);
  26098. +
  26099. + if (!time_set)
  26100. + stop = ftrace_now(cpu);
  26101. + if (start) {
  26102. + long latency = ((long) (stop - start)) /
  26103. + NSECS_PER_USECS;
  26104. +
  26105. + latency_hist(PREEMPTIRQSOFF_LATENCY, cpu,
  26106. + latency, 0, stop, NULL);
  26107. + }
  26108. + per_cpu(hist_preemptirqsoff_counting, cpu) = 0;
  26109. + }
  26110. +#endif
  26111. + }
  26112. +}
  26113. +#endif
  26114. +
  26115. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  26116. +static DEFINE_RAW_SPINLOCK(wakeup_lock);
  26117. +static notrace void probe_sched_migrate_task(void *v, struct task_struct *task,
  26118. + int cpu)
  26119. +{
  26120. + int old_cpu = task_cpu(task);
  26121. +
  26122. + if (cpu != old_cpu) {
  26123. + unsigned long flags;
  26124. + struct task_struct *cpu_wakeup_task;
  26125. +
  26126. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  26127. +
  26128. + cpu_wakeup_task = per_cpu(wakeup_task, old_cpu);
  26129. + if (task == cpu_wakeup_task) {
  26130. + put_task_struct(cpu_wakeup_task);
  26131. + per_cpu(wakeup_task, old_cpu) = NULL;
  26132. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = task;
  26133. + get_task_struct(cpu_wakeup_task);
  26134. + }
  26135. +
  26136. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  26137. + }
  26138. +}
  26139. +
  26140. +static notrace void probe_wakeup_latency_hist_start(void *v,
  26141. + struct task_struct *p)
  26142. +{
  26143. + unsigned long flags;
  26144. + struct task_struct *curr = current;
  26145. + int cpu = task_cpu(p);
  26146. + struct task_struct *cpu_wakeup_task;
  26147. +
  26148. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  26149. +
  26150. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  26151. +
  26152. + if (wakeup_pid) {
  26153. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  26154. + p->prio == curr->prio)
  26155. + per_cpu(wakeup_sharedprio, cpu) = 1;
  26156. + if (likely(wakeup_pid != task_pid_nr(p)))
  26157. + goto out;
  26158. + } else {
  26159. + if (likely(!rt_task(p)) ||
  26160. + (cpu_wakeup_task && p->prio > cpu_wakeup_task->prio) ||
  26161. + p->prio > curr->prio)
  26162. + goto out;
  26163. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  26164. + p->prio == curr->prio)
  26165. + per_cpu(wakeup_sharedprio, cpu) = 1;
  26166. + }
  26167. +
  26168. + if (cpu_wakeup_task)
  26169. + put_task_struct(cpu_wakeup_task);
  26170. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = p;
  26171. + get_task_struct(cpu_wakeup_task);
  26172. + cpu_wakeup_task->preempt_timestamp_hist =
  26173. + ftrace_now(raw_smp_processor_id());
  26174. +out:
  26175. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  26176. +}
  26177. +
  26178. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  26179. + bool preempt, struct task_struct *prev, struct task_struct *next)
  26180. +{
  26181. + unsigned long flags;
  26182. + int cpu = task_cpu(next);
  26183. + long latency;
  26184. + cycle_t stop;
  26185. + struct task_struct *cpu_wakeup_task;
  26186. +
  26187. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  26188. +
  26189. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  26190. +
  26191. + if (cpu_wakeup_task == NULL)
  26192. + goto out;
  26193. +
  26194. + /* Already running? */
  26195. + if (unlikely(current == cpu_wakeup_task))
  26196. + goto out_reset;
  26197. +
  26198. + if (next != cpu_wakeup_task) {
  26199. + if (next->prio < cpu_wakeup_task->prio)
  26200. + goto out_reset;
  26201. +
  26202. + if (next->prio == cpu_wakeup_task->prio)
  26203. + per_cpu(wakeup_sharedprio, cpu) = 1;
  26204. +
  26205. + goto out;
  26206. + }
  26207. +
  26208. + if (current->prio == cpu_wakeup_task->prio)
  26209. + per_cpu(wakeup_sharedprio, cpu) = 1;
  26210. +
  26211. + /*
  26212. + * The task we are waiting for is about to be switched to.
  26213. + * Calculate latency and store it in histogram.
  26214. + */
  26215. + stop = ftrace_now(raw_smp_processor_id());
  26216. +
  26217. + latency = ((long) (stop - next->preempt_timestamp_hist)) /
  26218. + NSECS_PER_USECS;
  26219. +
  26220. + if (per_cpu(wakeup_sharedprio, cpu)) {
  26221. + latency_hist(WAKEUP_LATENCY_SHAREDPRIO, cpu, latency, 0, stop,
  26222. + next);
  26223. + per_cpu(wakeup_sharedprio, cpu) = 0;
  26224. + } else {
  26225. + latency_hist(WAKEUP_LATENCY, cpu, latency, 0, stop, next);
  26226. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  26227. + if (timerandwakeup_enabled_data.enabled) {
  26228. + latency_hist(TIMERANDWAKEUP_LATENCY, cpu,
  26229. + next->timer_offset + latency, next->timer_offset,
  26230. + stop, next);
  26231. + }
  26232. +#endif
  26233. + }
  26234. +
  26235. +out_reset:
  26236. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  26237. + next->timer_offset = 0;
  26238. +#endif
  26239. + put_task_struct(cpu_wakeup_task);
  26240. + per_cpu(wakeup_task, cpu) = NULL;
  26241. +out:
  26242. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  26243. +}
  26244. +#endif
  26245. +
  26246. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  26247. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  26248. + long long latency_ns, struct task_struct *curr,
  26249. + struct task_struct *task)
  26250. +{
  26251. + if (latency_ns <= 0 && task != NULL && rt_task(task) &&
  26252. + (task->prio < curr->prio ||
  26253. + (task->prio == curr->prio &&
  26254. + !cpumask_test_cpu(cpu, &task->cpus_allowed)))) {
  26255. + long latency;
  26256. + cycle_t now;
  26257. +
  26258. + if (missed_timer_offsets_pid) {
  26259. + if (likely(missed_timer_offsets_pid !=
  26260. + task_pid_nr(task)))
  26261. + return;
  26262. + }
  26263. +
  26264. + now = ftrace_now(cpu);
  26265. + latency = (long) div_s64(-latency_ns, NSECS_PER_USECS);
  26266. + latency_hist(MISSED_TIMER_OFFSETS, cpu, latency, latency, now,
  26267. + task);
  26268. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  26269. + task->timer_offset = latency;
  26270. +#endif
  26271. + }
  26272. +}
  26273. +#endif
  26274. +
  26275. +static __init int latency_hist_init(void)
  26276. +{
  26277. + struct dentry *latency_hist_root = NULL;
  26278. + struct dentry *dentry;
  26279. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  26280. + struct dentry *dentry_sharedprio;
  26281. +#endif
  26282. + struct dentry *entry;
  26283. + struct dentry *enable_root;
  26284. + int i = 0;
  26285. + struct hist_data *my_hist;
  26286. + char name[64];
  26287. + char *cpufmt = "CPU%d";
  26288. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  26289. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  26290. + char *cpufmt_maxlatproc = "max_latency-CPU%d";
  26291. + struct maxlatproc_data *mp = NULL;
  26292. +#endif
  26293. +
  26294. + dentry = tracing_init_dentry();
  26295. + latency_hist_root = debugfs_create_dir(latency_hist_dir_root, dentry);
  26296. + enable_root = debugfs_create_dir("enable", latency_hist_root);
  26297. +
  26298. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  26299. + dentry = debugfs_create_dir(irqsoff_hist_dir, latency_hist_root);
  26300. + for_each_possible_cpu(i) {
  26301. + sprintf(name, cpufmt, i);
  26302. + entry = debugfs_create_file(name, 0444, dentry,
  26303. + &per_cpu(irqsoff_hist, i), &latency_hist_fops);
  26304. + my_hist = &per_cpu(irqsoff_hist, i);
  26305. + atomic_set(&my_hist->hist_mode, 1);
  26306. + my_hist->min_lat = LONG_MAX;
  26307. + }
  26308. + entry = debugfs_create_file("reset", 0644, dentry,
  26309. + (void *)IRQSOFF_LATENCY, &latency_hist_reset_fops);
  26310. +#endif
  26311. +
  26312. +#ifdef CONFIG_PREEMPT_OFF_HIST
  26313. + dentry = debugfs_create_dir(preemptoff_hist_dir,
  26314. + latency_hist_root);
  26315. + for_each_possible_cpu(i) {
  26316. + sprintf(name, cpufmt, i);
  26317. + entry = debugfs_create_file(name, 0444, dentry,
  26318. + &per_cpu(preemptoff_hist, i), &latency_hist_fops);
  26319. + my_hist = &per_cpu(preemptoff_hist, i);
  26320. + atomic_set(&my_hist->hist_mode, 1);
  26321. + my_hist->min_lat = LONG_MAX;
  26322. + }
  26323. + entry = debugfs_create_file("reset", 0644, dentry,
  26324. + (void *)PREEMPTOFF_LATENCY, &latency_hist_reset_fops);
  26325. +#endif
  26326. +
  26327. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  26328. + dentry = debugfs_create_dir(preemptirqsoff_hist_dir,
  26329. + latency_hist_root);
  26330. + for_each_possible_cpu(i) {
  26331. + sprintf(name, cpufmt, i);
  26332. + entry = debugfs_create_file(name, 0444, dentry,
  26333. + &per_cpu(preemptirqsoff_hist, i), &latency_hist_fops);
  26334. + my_hist = &per_cpu(preemptirqsoff_hist, i);
  26335. + atomic_set(&my_hist->hist_mode, 1);
  26336. + my_hist->min_lat = LONG_MAX;
  26337. + }
  26338. + entry = debugfs_create_file("reset", 0644, dentry,
  26339. + (void *)PREEMPTIRQSOFF_LATENCY, &latency_hist_reset_fops);
  26340. +#endif
  26341. +
  26342. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  26343. + entry = debugfs_create_file("preemptirqsoff", 0644,
  26344. + enable_root, (void *)&preemptirqsoff_enabled_data,
  26345. + &enable_fops);
  26346. +#endif
  26347. +
  26348. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  26349. + dentry = debugfs_create_dir(wakeup_latency_hist_dir,
  26350. + latency_hist_root);
  26351. + dentry_sharedprio = debugfs_create_dir(
  26352. + wakeup_latency_hist_dir_sharedprio, dentry);
  26353. + for_each_possible_cpu(i) {
  26354. + sprintf(name, cpufmt, i);
  26355. +
  26356. + entry = debugfs_create_file(name, 0444, dentry,
  26357. + &per_cpu(wakeup_latency_hist, i),
  26358. + &latency_hist_fops);
  26359. + my_hist = &per_cpu(wakeup_latency_hist, i);
  26360. + atomic_set(&my_hist->hist_mode, 1);
  26361. + my_hist->min_lat = LONG_MAX;
  26362. +
  26363. + entry = debugfs_create_file(name, 0444, dentry_sharedprio,
  26364. + &per_cpu(wakeup_latency_hist_sharedprio, i),
  26365. + &latency_hist_fops);
  26366. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, i);
  26367. + atomic_set(&my_hist->hist_mode, 1);
  26368. + my_hist->min_lat = LONG_MAX;
  26369. +
  26370. + sprintf(name, cpufmt_maxlatproc, i);
  26371. +
  26372. + mp = &per_cpu(wakeup_maxlatproc, i);
  26373. + entry = debugfs_create_file(name, 0444, dentry, mp,
  26374. + &maxlatproc_fops);
  26375. + clear_maxlatprocdata(mp);
  26376. +
  26377. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, i);
  26378. + entry = debugfs_create_file(name, 0444, dentry_sharedprio, mp,
  26379. + &maxlatproc_fops);
  26380. + clear_maxlatprocdata(mp);
  26381. + }
  26382. + entry = debugfs_create_file("pid", 0644, dentry,
  26383. + (void *)&wakeup_pid, &pid_fops);
  26384. + entry = debugfs_create_file("reset", 0644, dentry,
  26385. + (void *)WAKEUP_LATENCY, &latency_hist_reset_fops);
  26386. + entry = debugfs_create_file("reset", 0644, dentry_sharedprio,
  26387. + (void *)WAKEUP_LATENCY_SHAREDPRIO, &latency_hist_reset_fops);
  26388. + entry = debugfs_create_file("wakeup", 0644,
  26389. + enable_root, (void *)&wakeup_latency_enabled_data,
  26390. + &enable_fops);
  26391. +#endif
  26392. +
  26393. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  26394. + dentry = debugfs_create_dir(missed_timer_offsets_dir,
  26395. + latency_hist_root);
  26396. + for_each_possible_cpu(i) {
  26397. + sprintf(name, cpufmt, i);
  26398. + entry = debugfs_create_file(name, 0444, dentry,
  26399. + &per_cpu(missed_timer_offsets, i), &latency_hist_fops);
  26400. + my_hist = &per_cpu(missed_timer_offsets, i);
  26401. + atomic_set(&my_hist->hist_mode, 1);
  26402. + my_hist->min_lat = LONG_MAX;
  26403. +
  26404. + sprintf(name, cpufmt_maxlatproc, i);
  26405. + mp = &per_cpu(missed_timer_offsets_maxlatproc, i);
  26406. + entry = debugfs_create_file(name, 0444, dentry, mp,
  26407. + &maxlatproc_fops);
  26408. + clear_maxlatprocdata(mp);
  26409. + }
  26410. + entry = debugfs_create_file("pid", 0644, dentry,
  26411. + (void *)&missed_timer_offsets_pid, &pid_fops);
  26412. + entry = debugfs_create_file("reset", 0644, dentry,
  26413. + (void *)MISSED_TIMER_OFFSETS, &latency_hist_reset_fops);
  26414. + entry = debugfs_create_file("missed_timer_offsets", 0644,
  26415. + enable_root, (void *)&missed_timer_offsets_enabled_data,
  26416. + &enable_fops);
  26417. +#endif
  26418. +
  26419. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  26420. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  26421. + dentry = debugfs_create_dir(timerandwakeup_latency_hist_dir,
  26422. + latency_hist_root);
  26423. + for_each_possible_cpu(i) {
  26424. + sprintf(name, cpufmt, i);
  26425. + entry = debugfs_create_file(name, 0444, dentry,
  26426. + &per_cpu(timerandwakeup_latency_hist, i),
  26427. + &latency_hist_fops);
  26428. + my_hist = &per_cpu(timerandwakeup_latency_hist, i);
  26429. + atomic_set(&my_hist->hist_mode, 1);
  26430. + my_hist->min_lat = LONG_MAX;
  26431. +
  26432. + sprintf(name, cpufmt_maxlatproc, i);
  26433. + mp = &per_cpu(timerandwakeup_maxlatproc, i);
  26434. + entry = debugfs_create_file(name, 0444, dentry, mp,
  26435. + &maxlatproc_fops);
  26436. + clear_maxlatprocdata(mp);
  26437. + }
  26438. + entry = debugfs_create_file("reset", 0644, dentry,
  26439. + (void *)TIMERANDWAKEUP_LATENCY, &latency_hist_reset_fops);
  26440. + entry = debugfs_create_file("timerandwakeup", 0644,
  26441. + enable_root, (void *)&timerandwakeup_enabled_data,
  26442. + &enable_fops);
  26443. +#endif
  26444. + return 0;
  26445. +}
  26446. +
  26447. +device_initcall(latency_hist_init);
  26448. diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
  26449. index 8aef4e63ac57..cdb7742283e5 100644
  26450. --- a/kernel/trace/trace.c
  26451. +++ b/kernel/trace/trace.c
  26452. @@ -1652,6 +1652,7 @@ tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
  26453. struct task_struct *tsk = current;
  26454. entry->preempt_count = pc & 0xff;
  26455. + entry->preempt_lazy_count = preempt_lazy_count();
  26456. entry->pid = (tsk) ? tsk->pid : 0;
  26457. entry->flags =
  26458. #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
  26459. @@ -1661,8 +1662,11 @@ tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
  26460. #endif
  26461. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  26462. ((pc & SOFTIRQ_OFFSET) ? TRACE_FLAG_SOFTIRQ : 0) |
  26463. - (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
  26464. + (tif_need_resched_now() ? TRACE_FLAG_NEED_RESCHED : 0) |
  26465. + (need_resched_lazy() ? TRACE_FLAG_NEED_RESCHED_LAZY : 0) |
  26466. (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
  26467. +
  26468. + entry->migrate_disable = (tsk) ? __migrate_disabled(tsk) & 0xFF : 0;
  26469. }
  26470. EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
  26471. @@ -2555,14 +2559,17 @@ get_total_entries(struct trace_buffer *buf,
  26472. static void print_lat_help_header(struct seq_file *m)
  26473. {
  26474. - seq_puts(m, "# _------=> CPU# \n"
  26475. - "# / _-----=> irqs-off \n"
  26476. - "# | / _----=> need-resched \n"
  26477. - "# || / _---=> hardirq/softirq \n"
  26478. - "# ||| / _--=> preempt-depth \n"
  26479. - "# |||| / delay \n"
  26480. - "# cmd pid ||||| time | caller \n"
  26481. - "# \\ / ||||| \\ | / \n");
  26482. + seq_puts(m, "# _--------=> CPU# \n"
  26483. + "# / _-------=> irqs-off \n"
  26484. + "# | / _------=> need-resched \n"
  26485. + "# || / _-----=> need-resched_lazy \n"
  26486. + "# ||| / _----=> hardirq/softirq \n"
  26487. + "# |||| / _---=> preempt-depth \n"
  26488. + "# ||||| / _--=> preempt-lazy-depth\n"
  26489. + "# |||||| / _-=> migrate-disable \n"
  26490. + "# ||||||| / delay \n"
  26491. + "# cmd pid |||||||| time | caller \n"
  26492. + "# \\ / |||||||| \\ | / \n");
  26493. }
  26494. static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
  26495. @@ -2588,11 +2595,14 @@ static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file
  26496. print_event_info(buf, m);
  26497. seq_puts(m, "# _-----=> irqs-off\n"
  26498. "# / _----=> need-resched\n"
  26499. - "# | / _---=> hardirq/softirq\n"
  26500. - "# || / _--=> preempt-depth\n"
  26501. - "# ||| / delay\n"
  26502. - "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"
  26503. - "# | | | |||| | |\n");
  26504. + "# |/ _-----=> need-resched_lazy\n"
  26505. + "# || / _---=> hardirq/softirq\n"
  26506. + "# ||| / _--=> preempt-depth\n"
  26507. + "# |||| / _-=> preempt-lazy-depth\n"
  26508. + "# ||||| / _-=> migrate-disable \n"
  26509. + "# |||||| / delay\n"
  26510. + "# TASK-PID CPU# ||||||| TIMESTAMP FUNCTION\n"
  26511. + "# | | | ||||||| | |\n");
  26512. }
  26513. void
  26514. diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
  26515. index 919d9d07686f..3bf86ece683c 100644
  26516. --- a/kernel/trace/trace.h
  26517. +++ b/kernel/trace/trace.h
  26518. @@ -117,6 +117,7 @@ struct kretprobe_trace_entry_head {
  26519. * NEED_RESCHED - reschedule is requested
  26520. * HARDIRQ - inside an interrupt handler
  26521. * SOFTIRQ - inside a softirq handler
  26522. + * NEED_RESCHED_LAZY - lazy reschedule is requested
  26523. */
  26524. enum trace_flag_type {
  26525. TRACE_FLAG_IRQS_OFF = 0x01,
  26526. @@ -125,6 +126,7 @@ enum trace_flag_type {
  26527. TRACE_FLAG_HARDIRQ = 0x08,
  26528. TRACE_FLAG_SOFTIRQ = 0x10,
  26529. TRACE_FLAG_PREEMPT_RESCHED = 0x20,
  26530. + TRACE_FLAG_NEED_RESCHED_LAZY = 0x40,
  26531. };
  26532. #define TRACE_BUF_SIZE 1024
  26533. diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
  26534. index 996f0fd34312..5bd79b347398 100644
  26535. --- a/kernel/trace/trace_events.c
  26536. +++ b/kernel/trace/trace_events.c
  26537. @@ -188,6 +188,8 @@ static int trace_define_common_fields(void)
  26538. __common_field(unsigned char, flags);
  26539. __common_field(unsigned char, preempt_count);
  26540. __common_field(int, pid);
  26541. + __common_field(unsigned short, migrate_disable);
  26542. + __common_field(unsigned short, padding);
  26543. return ret;
  26544. }
  26545. @@ -244,6 +246,14 @@ void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
  26546. local_save_flags(fbuffer->flags);
  26547. fbuffer->pc = preempt_count();
  26548. + /*
  26549. + * If CONFIG_PREEMPT is enabled, then the tracepoint itself disables
  26550. + * preemption (adding one to the preempt_count). Since we are
  26551. + * interested in the preempt_count at the time the tracepoint was
  26552. + * hit, we need to subtract one to offset the increment.
  26553. + */
  26554. + if (IS_ENABLED(CONFIG_PREEMPT))
  26555. + fbuffer->pc--;
  26556. fbuffer->trace_file = trace_file;
  26557. fbuffer->event =
  26558. diff --git a/kernel/trace/trace_irqsoff.c b/kernel/trace/trace_irqsoff.c
  26559. index be3222b7d72e..553e71254ad6 100644
  26560. --- a/kernel/trace/trace_irqsoff.c
  26561. +++ b/kernel/trace/trace_irqsoff.c
  26562. @@ -13,6 +13,7 @@
  26563. #include <linux/uaccess.h>
  26564. #include <linux/module.h>
  26565. #include <linux/ftrace.h>
  26566. +#include <trace/events/hist.h>
  26567. #include "trace.h"
  26568. @@ -424,11 +425,13 @@ void start_critical_timings(void)
  26569. {
  26570. if (preempt_trace() || irq_trace())
  26571. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  26572. + trace_preemptirqsoff_hist_rcuidle(TRACE_START, 1);
  26573. }
  26574. EXPORT_SYMBOL_GPL(start_critical_timings);
  26575. void stop_critical_timings(void)
  26576. {
  26577. + trace_preemptirqsoff_hist_rcuidle(TRACE_STOP, 0);
  26578. if (preempt_trace() || irq_trace())
  26579. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  26580. }
  26581. @@ -438,6 +441,7 @@ EXPORT_SYMBOL_GPL(stop_critical_timings);
  26582. #ifdef CONFIG_PROVE_LOCKING
  26583. void time_hardirqs_on(unsigned long a0, unsigned long a1)
  26584. {
  26585. + trace_preemptirqsoff_hist_rcuidle(IRQS_ON, 0);
  26586. if (!preempt_trace() && irq_trace())
  26587. stop_critical_timing(a0, a1);
  26588. }
  26589. @@ -446,6 +450,7 @@ void time_hardirqs_off(unsigned long a0, unsigned long a1)
  26590. {
  26591. if (!preempt_trace() && irq_trace())
  26592. start_critical_timing(a0, a1);
  26593. + trace_preemptirqsoff_hist_rcuidle(IRQS_OFF, 1);
  26594. }
  26595. #else /* !CONFIG_PROVE_LOCKING */
  26596. @@ -471,6 +476,7 @@ inline void print_irqtrace_events(struct task_struct *curr)
  26597. */
  26598. void trace_hardirqs_on(void)
  26599. {
  26600. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  26601. if (!preempt_trace() && irq_trace())
  26602. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  26603. }
  26604. @@ -480,11 +486,13 @@ void trace_hardirqs_off(void)
  26605. {
  26606. if (!preempt_trace() && irq_trace())
  26607. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  26608. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  26609. }
  26610. EXPORT_SYMBOL(trace_hardirqs_off);
  26611. __visible void trace_hardirqs_on_caller(unsigned long caller_addr)
  26612. {
  26613. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  26614. if (!preempt_trace() && irq_trace())
  26615. stop_critical_timing(CALLER_ADDR0, caller_addr);
  26616. }
  26617. @@ -494,6 +502,7 @@ __visible void trace_hardirqs_off_caller(unsigned long caller_addr)
  26618. {
  26619. if (!preempt_trace() && irq_trace())
  26620. start_critical_timing(CALLER_ADDR0, caller_addr);
  26621. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  26622. }
  26623. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  26624. @@ -503,12 +512,14 @@ EXPORT_SYMBOL(trace_hardirqs_off_caller);
  26625. #ifdef CONFIG_PREEMPT_TRACER
  26626. void trace_preempt_on(unsigned long a0, unsigned long a1)
  26627. {
  26628. + trace_preemptirqsoff_hist(PREEMPT_ON, 0);
  26629. if (preempt_trace() && !irq_trace())
  26630. stop_critical_timing(a0, a1);
  26631. }
  26632. void trace_preempt_off(unsigned long a0, unsigned long a1)
  26633. {
  26634. + trace_preemptirqsoff_hist(PREEMPT_ON, 1);
  26635. if (preempt_trace() && !irq_trace())
  26636. start_critical_timing(a0, a1);
  26637. }
  26638. diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c
  26639. index 282982195e09..9f19d839a756 100644
  26640. --- a/kernel/trace/trace_output.c
  26641. +++ b/kernel/trace/trace_output.c
  26642. @@ -386,6 +386,7 @@ int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  26643. {
  26644. char hardsoft_irq;
  26645. char need_resched;
  26646. + char need_resched_lazy;
  26647. char irqs_off;
  26648. int hardirq;
  26649. int softirq;
  26650. @@ -413,6 +414,8 @@ int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  26651. need_resched = '.';
  26652. break;
  26653. }
  26654. + need_resched_lazy =
  26655. + (entry->flags & TRACE_FLAG_NEED_RESCHED_LAZY) ? 'L' : '.';
  26656. hardsoft_irq =
  26657. (hardirq && softirq) ? 'H' :
  26658. @@ -420,14 +423,25 @@ int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  26659. softirq ? 's' :
  26660. '.';
  26661. - trace_seq_printf(s, "%c%c%c",
  26662. - irqs_off, need_resched, hardsoft_irq);
  26663. + trace_seq_printf(s, "%c%c%c%c",
  26664. + irqs_off, need_resched, need_resched_lazy,
  26665. + hardsoft_irq);
  26666. if (entry->preempt_count)
  26667. trace_seq_printf(s, "%x", entry->preempt_count);
  26668. else
  26669. trace_seq_putc(s, '.');
  26670. + if (entry->preempt_lazy_count)
  26671. + trace_seq_printf(s, "%x", entry->preempt_lazy_count);
  26672. + else
  26673. + trace_seq_putc(s, '.');
  26674. +
  26675. + if (entry->migrate_disable)
  26676. + trace_seq_printf(s, "%x", entry->migrate_disable);
  26677. + else
  26678. + trace_seq_putc(s, '.');
  26679. +
  26680. return !trace_seq_has_overflowed(s);
  26681. }
  26682. diff --git a/kernel/user.c b/kernel/user.c
  26683. index b069ccbfb0b0..1a2e88e98b5e 100644
  26684. --- a/kernel/user.c
  26685. +++ b/kernel/user.c
  26686. @@ -161,11 +161,11 @@ void free_uid(struct user_struct *up)
  26687. if (!up)
  26688. return;
  26689. - local_irq_save(flags);
  26690. + local_irq_save_nort(flags);
  26691. if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  26692. free_user(up, flags);
  26693. else
  26694. - local_irq_restore(flags);
  26695. + local_irq_restore_nort(flags);
  26696. }
  26697. struct user_struct *alloc_uid(kuid_t uid)
  26698. diff --git a/kernel/watchdog.c b/kernel/watchdog.c
  26699. index c1e0b5f429b6..fa2e079cc314 100644
  26700. --- a/kernel/watchdog.c
  26701. +++ b/kernel/watchdog.c
  26702. @@ -299,6 +299,8 @@ static int is_softlockup(unsigned long touch_ts)
  26703. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  26704. +static DEFINE_RAW_SPINLOCK(watchdog_output_lock);
  26705. +
  26706. static struct perf_event_attr wd_hw_attr = {
  26707. .type = PERF_TYPE_HARDWARE,
  26708. .config = PERF_COUNT_HW_CPU_CYCLES,
  26709. @@ -332,6 +334,13 @@ static void watchdog_overflow_callback(struct perf_event *event,
  26710. /* only print hardlockups once */
  26711. if (__this_cpu_read(hard_watchdog_warn) == true)
  26712. return;
  26713. + /*
  26714. + * If early-printk is enabled then make sure we do not
  26715. + * lock up in printk() and kill console logging:
  26716. + */
  26717. + printk_kill();
  26718. +
  26719. + raw_spin_lock(&watchdog_output_lock);
  26720. pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  26721. print_modules();
  26722. @@ -349,8 +358,9 @@ static void watchdog_overflow_callback(struct perf_event *event,
  26723. !test_and_set_bit(0, &hardlockup_allcpu_dumped))
  26724. trigger_allbutself_cpu_backtrace();
  26725. + raw_spin_unlock(&watchdog_output_lock);
  26726. if (hardlockup_panic)
  26727. - panic("Hard LOCKUP");
  26728. + nmi_panic(regs, "Hard LOCKUP");
  26729. __this_cpu_write(hard_watchdog_warn, true);
  26730. return;
  26731. @@ -496,6 +506,7 @@ static void watchdog_enable(unsigned int cpu)
  26732. /* kick off the timer for the hardlockup detector */
  26733. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  26734. hrtimer->function = watchdog_timer_fn;
  26735. + hrtimer->irqsafe = 1;
  26736. /* Enable the perf event */
  26737. watchdog_nmi_enable(cpu);
  26738. diff --git a/kernel/workqueue.c b/kernel/workqueue.c
  26739. index 85555eb4d3cb..79f33789f330 100644
  26740. --- a/kernel/workqueue.c
  26741. +++ b/kernel/workqueue.c
  26742. @@ -48,6 +48,8 @@
  26743. #include <linux/nodemask.h>
  26744. #include <linux/moduleparam.h>
  26745. #include <linux/uaccess.h>
  26746. +#include <linux/locallock.h>
  26747. +#include <linux/delay.h>
  26748. #include "workqueue_internal.h"
  26749. @@ -122,11 +124,16 @@ enum {
  26750. * cpu or grabbing pool->lock is enough for read access. If
  26751. * POOL_DISASSOCIATED is set, it's identical to L.
  26752. *
  26753. + * On RT we need the extra protection via rt_lock_idle_list() for
  26754. + * the list manipulations against read access from
  26755. + * wq_worker_sleeping(). All other places are nicely serialized via
  26756. + * pool->lock.
  26757. + *
  26758. * A: pool->attach_mutex protected.
  26759. *
  26760. * PL: wq_pool_mutex protected.
  26761. *
  26762. - * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
  26763. + * PR: wq_pool_mutex protected for writes. RCU protected for reads.
  26764. *
  26765. * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads.
  26766. *
  26767. @@ -135,7 +142,7 @@ enum {
  26768. *
  26769. * WQ: wq->mutex protected.
  26770. *
  26771. - * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
  26772. + * WR: wq->mutex protected for writes. RCU protected for reads.
  26773. *
  26774. * MD: wq_mayday_lock protected.
  26775. */
  26776. @@ -183,7 +190,7 @@ struct worker_pool {
  26777. atomic_t nr_running ____cacheline_aligned_in_smp;
  26778. /*
  26779. - * Destruction of pool is sched-RCU protected to allow dereferences
  26780. + * Destruction of pool is RCU protected to allow dereferences
  26781. * from get_work_pool().
  26782. */
  26783. struct rcu_head rcu;
  26784. @@ -212,7 +219,7 @@ struct pool_workqueue {
  26785. /*
  26786. * Release of unbound pwq is punted to system_wq. See put_pwq()
  26787. * and pwq_unbound_release_workfn() for details. pool_workqueue
  26788. - * itself is also sched-RCU protected so that the first pwq can be
  26789. + * itself is also RCU protected so that the first pwq can be
  26790. * determined without grabbing wq->mutex.
  26791. */
  26792. struct work_struct unbound_release_work;
  26793. @@ -332,6 +339,8 @@ EXPORT_SYMBOL_GPL(system_power_efficient_wq);
  26794. struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
  26795. EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
  26796. +static DEFINE_LOCAL_IRQ_LOCK(pendingb_lock);
  26797. +
  26798. static int worker_thread(void *__worker);
  26799. static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  26800. @@ -339,20 +348,20 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  26801. #include <trace/events/workqueue.h>
  26802. #define assert_rcu_or_pool_mutex() \
  26803. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  26804. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  26805. !lockdep_is_held(&wq_pool_mutex), \
  26806. - "sched RCU or wq_pool_mutex should be held")
  26807. + "RCU or wq_pool_mutex should be held")
  26808. #define assert_rcu_or_wq_mutex(wq) \
  26809. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  26810. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  26811. !lockdep_is_held(&wq->mutex), \
  26812. - "sched RCU or wq->mutex should be held")
  26813. + "RCU or wq->mutex should be held")
  26814. #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \
  26815. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  26816. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  26817. !lockdep_is_held(&wq->mutex) && \
  26818. !lockdep_is_held(&wq_pool_mutex), \
  26819. - "sched RCU, wq->mutex or wq_pool_mutex should be held")
  26820. + "RCU, wq->mutex or wq_pool_mutex should be held")
  26821. #define for_each_cpu_worker_pool(pool, cpu) \
  26822. for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
  26823. @@ -364,7 +373,7 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  26824. * @pool: iteration cursor
  26825. * @pi: integer used for iteration
  26826. *
  26827. - * This must be called either with wq_pool_mutex held or sched RCU read
  26828. + * This must be called either with wq_pool_mutex held or RCU read
  26829. * locked. If the pool needs to be used beyond the locking in effect, the
  26830. * caller is responsible for guaranteeing that the pool stays online.
  26831. *
  26832. @@ -396,7 +405,7 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  26833. * @pwq: iteration cursor
  26834. * @wq: the target workqueue
  26835. *
  26836. - * This must be called either with wq->mutex held or sched RCU read locked.
  26837. + * This must be called either with wq->mutex held or RCU read locked.
  26838. * If the pwq needs to be used beyond the locking in effect, the caller is
  26839. * responsible for guaranteeing that the pwq stays online.
  26840. *
  26841. @@ -408,6 +417,31 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  26842. if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
  26843. else
  26844. +#ifdef CONFIG_PREEMPT_RT_BASE
  26845. +static inline void rt_lock_idle_list(struct worker_pool *pool)
  26846. +{
  26847. + preempt_disable();
  26848. +}
  26849. +static inline void rt_unlock_idle_list(struct worker_pool *pool)
  26850. +{
  26851. + preempt_enable();
  26852. +}
  26853. +static inline void sched_lock_idle_list(struct worker_pool *pool) { }
  26854. +static inline void sched_unlock_idle_list(struct worker_pool *pool) { }
  26855. +#else
  26856. +static inline void rt_lock_idle_list(struct worker_pool *pool) { }
  26857. +static inline void rt_unlock_idle_list(struct worker_pool *pool) { }
  26858. +static inline void sched_lock_idle_list(struct worker_pool *pool)
  26859. +{
  26860. + spin_lock_irq(&pool->lock);
  26861. +}
  26862. +static inline void sched_unlock_idle_list(struct worker_pool *pool)
  26863. +{
  26864. + spin_unlock_irq(&pool->lock);
  26865. +}
  26866. +#endif
  26867. +
  26868. +
  26869. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  26870. static struct debug_obj_descr work_debug_descr;
  26871. @@ -558,7 +592,7 @@ static int worker_pool_assign_id(struct worker_pool *pool)
  26872. * @wq: the target workqueue
  26873. * @node: the node ID
  26874. *
  26875. - * This must be called with any of wq_pool_mutex, wq->mutex or sched RCU
  26876. + * This must be called with any of wq_pool_mutex, wq->mutex or RCU
  26877. * read locked.
  26878. * If the pwq needs to be used beyond the locking in effect, the caller is
  26879. * responsible for guaranteeing that the pwq stays online.
  26880. @@ -702,8 +736,8 @@ static struct pool_workqueue *get_work_pwq(struct work_struct *work)
  26881. * @work: the work item of interest
  26882. *
  26883. * Pools are created and destroyed under wq_pool_mutex, and allows read
  26884. - * access under sched-RCU read lock. As such, this function should be
  26885. - * called under wq_pool_mutex or with preemption disabled.
  26886. + * access under RCU read lock. As such, this function should be
  26887. + * called under wq_pool_mutex or inside of a rcu_read_lock() region.
  26888. *
  26889. * All fields of the returned pool are accessible as long as the above
  26890. * mentioned locking is in effect. If the returned pool needs to be used
  26891. @@ -840,51 +874,44 @@ static struct worker *first_idle_worker(struct worker_pool *pool)
  26892. */
  26893. static void wake_up_worker(struct worker_pool *pool)
  26894. {
  26895. - struct worker *worker = first_idle_worker(pool);
  26896. + struct worker *worker;
  26897. +
  26898. + rt_lock_idle_list(pool);
  26899. +
  26900. + worker = first_idle_worker(pool);
  26901. if (likely(worker))
  26902. wake_up_process(worker->task);
  26903. +
  26904. + rt_unlock_idle_list(pool);
  26905. }
  26906. /**
  26907. - * wq_worker_waking_up - a worker is waking up
  26908. - * @task: task waking up
  26909. - * @cpu: CPU @task is waking up to
  26910. + * wq_worker_running - a worker is running again
  26911. + * @task: task returning from sleep
  26912. *
  26913. - * This function is called during try_to_wake_up() when a worker is
  26914. - * being awoken.
  26915. - *
  26916. - * CONTEXT:
  26917. - * spin_lock_irq(rq->lock)
  26918. + * This function is called when a worker returns from schedule()
  26919. */
  26920. -void wq_worker_waking_up(struct task_struct *task, int cpu)
  26921. +void wq_worker_running(struct task_struct *task)
  26922. {
  26923. struct worker *worker = kthread_data(task);
  26924. - if (!(worker->flags & WORKER_NOT_RUNNING)) {
  26925. - WARN_ON_ONCE(worker->pool->cpu != cpu);
  26926. + if (!worker->sleeping)
  26927. + return;
  26928. + if (!(worker->flags & WORKER_NOT_RUNNING))
  26929. atomic_inc(&worker->pool->nr_running);
  26930. - }
  26931. + worker->sleeping = 0;
  26932. }
  26933. /**
  26934. * wq_worker_sleeping - a worker is going to sleep
  26935. * @task: task going to sleep
  26936. - * @cpu: CPU in question, must be the current CPU number
  26937. - *
  26938. - * This function is called during schedule() when a busy worker is
  26939. - * going to sleep. Worker on the same cpu can be woken up by
  26940. - * returning pointer to its task.
  26941. - *
  26942. - * CONTEXT:
  26943. - * spin_lock_irq(rq->lock)
  26944. - *
  26945. - * Return:
  26946. - * Worker task on @cpu to wake up, %NULL if none.
  26947. + * This function is called from schedule() when a busy worker is
  26948. + * going to sleep.
  26949. */
  26950. -struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
  26951. +void wq_worker_sleeping(struct task_struct *task)
  26952. {
  26953. - struct worker *worker = kthread_data(task), *to_wakeup = NULL;
  26954. + struct worker *worker = kthread_data(task);
  26955. struct worker_pool *pool;
  26956. /*
  26957. @@ -893,29 +920,26 @@ struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
  26958. * checking NOT_RUNNING.
  26959. */
  26960. if (worker->flags & WORKER_NOT_RUNNING)
  26961. - return NULL;
  26962. + return;
  26963. pool = worker->pool;
  26964. - /* this can only happen on the local cpu */
  26965. - if (WARN_ON_ONCE(cpu != raw_smp_processor_id() || pool->cpu != cpu))
  26966. - return NULL;
  26967. + if (WARN_ON_ONCE(worker->sleeping))
  26968. + return;
  26969. +
  26970. + worker->sleeping = 1;
  26971. /*
  26972. * The counterpart of the following dec_and_test, implied mb,
  26973. * worklist not empty test sequence is in insert_work().
  26974. * Please read comment there.
  26975. - *
  26976. - * NOT_RUNNING is clear. This means that we're bound to and
  26977. - * running on the local cpu w/ rq lock held and preemption
  26978. - * disabled, which in turn means that none else could be
  26979. - * manipulating idle_list, so dereferencing idle_list without pool
  26980. - * lock is safe.
  26981. */
  26982. if (atomic_dec_and_test(&pool->nr_running) &&
  26983. - !list_empty(&pool->worklist))
  26984. - to_wakeup = first_idle_worker(pool);
  26985. - return to_wakeup ? to_wakeup->task : NULL;
  26986. + !list_empty(&pool->worklist)) {
  26987. + sched_lock_idle_list(pool);
  26988. + wake_up_worker(pool);
  26989. + sched_unlock_idle_list(pool);
  26990. + }
  26991. }
  26992. /**
  26993. @@ -1109,12 +1133,14 @@ static void put_pwq_unlocked(struct pool_workqueue *pwq)
  26994. {
  26995. if (pwq) {
  26996. /*
  26997. - * As both pwqs and pools are sched-RCU protected, the
  26998. + * As both pwqs and pools are RCU protected, the
  26999. * following lock operations are safe.
  27000. */
  27001. - spin_lock_irq(&pwq->pool->lock);
  27002. + rcu_read_lock();
  27003. + local_spin_lock_irq(pendingb_lock, &pwq->pool->lock);
  27004. put_pwq(pwq);
  27005. - spin_unlock_irq(&pwq->pool->lock);
  27006. + local_spin_unlock_irq(pendingb_lock, &pwq->pool->lock);
  27007. + rcu_read_unlock();
  27008. }
  27009. }
  27010. @@ -1216,7 +1242,7 @@ static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
  27011. struct worker_pool *pool;
  27012. struct pool_workqueue *pwq;
  27013. - local_irq_save(*flags);
  27014. + local_lock_irqsave(pendingb_lock, *flags);
  27015. /* try to steal the timer if it exists */
  27016. if (is_dwork) {
  27017. @@ -1235,6 +1261,7 @@ static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
  27018. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
  27019. return 0;
  27020. + rcu_read_lock();
  27021. /*
  27022. * The queueing is in progress, or it is already queued. Try to
  27023. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  27024. @@ -1273,14 +1300,16 @@ static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
  27025. set_work_pool_and_keep_pending(work, pool->id);
  27026. spin_unlock(&pool->lock);
  27027. + rcu_read_unlock();
  27028. return 1;
  27029. }
  27030. spin_unlock(&pool->lock);
  27031. fail:
  27032. - local_irq_restore(*flags);
  27033. + rcu_read_unlock();
  27034. + local_unlock_irqrestore(pendingb_lock, *flags);
  27035. if (work_is_canceling(work))
  27036. return -ENOENT;
  27037. - cpu_relax();
  27038. + cpu_chill();
  27039. return -EAGAIN;
  27040. }
  27041. @@ -1349,7 +1378,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
  27042. * queued or lose PENDING. Grabbing PENDING and queueing should
  27043. * happen with IRQ disabled.
  27044. */
  27045. - WARN_ON_ONCE(!irqs_disabled());
  27046. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  27047. debug_work_activate(work);
  27048. @@ -1357,6 +1386,8 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
  27049. if (unlikely(wq->flags & __WQ_DRAINING) &&
  27050. WARN_ON_ONCE(!is_chained_work(wq)))
  27051. return;
  27052. +
  27053. + rcu_read_lock();
  27054. retry:
  27055. if (req_cpu == WORK_CPU_UNBOUND)
  27056. cpu = raw_smp_processor_id();
  27057. @@ -1413,10 +1444,8 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
  27058. /* pwq determined, queue */
  27059. trace_workqueue_queue_work(req_cpu, pwq, work);
  27060. - if (WARN_ON(!list_empty(&work->entry))) {
  27061. - spin_unlock(&pwq->pool->lock);
  27062. - return;
  27063. - }
  27064. + if (WARN_ON(!list_empty(&work->entry)))
  27065. + goto out;
  27066. pwq->nr_in_flight[pwq->work_color]++;
  27067. work_flags = work_color_to_flags(pwq->work_color);
  27068. @@ -1432,7 +1461,9 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
  27069. insert_work(pwq, work, worklist, work_flags);
  27070. +out:
  27071. spin_unlock(&pwq->pool->lock);
  27072. + rcu_read_unlock();
  27073. }
  27074. /**
  27075. @@ -1452,14 +1483,14 @@ bool queue_work_on(int cpu, struct workqueue_struct *wq,
  27076. bool ret = false;
  27077. unsigned long flags;
  27078. - local_irq_save(flags);
  27079. + local_lock_irqsave(pendingb_lock,flags);
  27080. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  27081. __queue_work(cpu, wq, work);
  27082. ret = true;
  27083. }
  27084. - local_irq_restore(flags);
  27085. + local_unlock_irqrestore(pendingb_lock, flags);
  27086. return ret;
  27087. }
  27088. EXPORT_SYMBOL(queue_work_on);
  27089. @@ -1527,14 +1558,14 @@ bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  27090. unsigned long flags;
  27091. /* read the comment in __queue_work() */
  27092. - local_irq_save(flags);
  27093. + local_lock_irqsave(pendingb_lock, flags);
  27094. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  27095. __queue_delayed_work(cpu, wq, dwork, delay);
  27096. ret = true;
  27097. }
  27098. - local_irq_restore(flags);
  27099. + local_unlock_irqrestore(pendingb_lock, flags);
  27100. return ret;
  27101. }
  27102. EXPORT_SYMBOL(queue_delayed_work_on);
  27103. @@ -1569,7 +1600,7 @@ bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
  27104. if (likely(ret >= 0)) {
  27105. __queue_delayed_work(cpu, wq, dwork, delay);
  27106. - local_irq_restore(flags);
  27107. + local_unlock_irqrestore(pendingb_lock, flags);
  27108. }
  27109. /* -ENOENT from try_to_grab_pending() becomes %true */
  27110. @@ -1602,7 +1633,9 @@ static void worker_enter_idle(struct worker *worker)
  27111. worker->last_active = jiffies;
  27112. /* idle_list is LIFO */
  27113. + rt_lock_idle_list(pool);
  27114. list_add(&worker->entry, &pool->idle_list);
  27115. + rt_unlock_idle_list(pool);
  27116. if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
  27117. mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
  27118. @@ -1635,7 +1668,9 @@ static void worker_leave_idle(struct worker *worker)
  27119. return;
  27120. worker_clr_flags(worker, WORKER_IDLE);
  27121. pool->nr_idle--;
  27122. + rt_lock_idle_list(pool);
  27123. list_del_init(&worker->entry);
  27124. + rt_unlock_idle_list(pool);
  27125. }
  27126. static struct worker *alloc_worker(int node)
  27127. @@ -1801,7 +1836,9 @@ static void destroy_worker(struct worker *worker)
  27128. pool->nr_workers--;
  27129. pool->nr_idle--;
  27130. + rt_lock_idle_list(pool);
  27131. list_del_init(&worker->entry);
  27132. + rt_unlock_idle_list(pool);
  27133. worker->flags |= WORKER_DIE;
  27134. wake_up_process(worker->task);
  27135. }
  27136. @@ -2711,14 +2748,14 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
  27137. might_sleep();
  27138. - local_irq_disable();
  27139. + rcu_read_lock();
  27140. pool = get_work_pool(work);
  27141. if (!pool) {
  27142. - local_irq_enable();
  27143. + rcu_read_unlock();
  27144. return false;
  27145. }
  27146. - spin_lock(&pool->lock);
  27147. + spin_lock_irq(&pool->lock);
  27148. /* see the comment in try_to_grab_pending() with the same code */
  27149. pwq = get_work_pwq(work);
  27150. if (pwq) {
  27151. @@ -2745,10 +2782,11 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
  27152. else
  27153. lock_map_acquire_read(&pwq->wq->lockdep_map);
  27154. lock_map_release(&pwq->wq->lockdep_map);
  27155. -
  27156. + rcu_read_unlock();
  27157. return true;
  27158. already_gone:
  27159. spin_unlock_irq(&pool->lock);
  27160. + rcu_read_unlock();
  27161. return false;
  27162. }
  27163. @@ -2835,7 +2873,7 @@ static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
  27164. /* tell other tasks trying to grab @work to back off */
  27165. mark_work_canceling(work);
  27166. - local_irq_restore(flags);
  27167. + local_unlock_irqrestore(pendingb_lock, flags);
  27168. flush_work(work);
  27169. clear_work_data(work);
  27170. @@ -2890,10 +2928,10 @@ EXPORT_SYMBOL_GPL(cancel_work_sync);
  27171. */
  27172. bool flush_delayed_work(struct delayed_work *dwork)
  27173. {
  27174. - local_irq_disable();
  27175. + local_lock_irq(pendingb_lock);
  27176. if (del_timer_sync(&dwork->timer))
  27177. __queue_work(dwork->cpu, dwork->wq, &dwork->work);
  27178. - local_irq_enable();
  27179. + local_unlock_irq(pendingb_lock);
  27180. return flush_work(&dwork->work);
  27181. }
  27182. EXPORT_SYMBOL(flush_delayed_work);
  27183. @@ -2928,7 +2966,7 @@ bool cancel_delayed_work(struct delayed_work *dwork)
  27184. set_work_pool_and_clear_pending(&dwork->work,
  27185. get_work_pool_id(&dwork->work));
  27186. - local_irq_restore(flags);
  27187. + local_unlock_irqrestore(pendingb_lock, flags);
  27188. return ret;
  27189. }
  27190. EXPORT_SYMBOL(cancel_delayed_work);
  27191. @@ -3155,7 +3193,7 @@ static void rcu_free_pool(struct rcu_head *rcu)
  27192. * put_unbound_pool - put a worker_pool
  27193. * @pool: worker_pool to put
  27194. *
  27195. - * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
  27196. + * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU
  27197. * safe manner. get_unbound_pool() calls this function on its failure path
  27198. * and this function should be able to release pools which went through,
  27199. * successfully or not, init_worker_pool().
  27200. @@ -3209,8 +3247,8 @@ static void put_unbound_pool(struct worker_pool *pool)
  27201. del_timer_sync(&pool->idle_timer);
  27202. del_timer_sync(&pool->mayday_timer);
  27203. - /* sched-RCU protected to allow dereferences from get_work_pool() */
  27204. - call_rcu_sched(&pool->rcu, rcu_free_pool);
  27205. + /* RCU protected to allow dereferences from get_work_pool() */
  27206. + call_rcu(&pool->rcu, rcu_free_pool);
  27207. }
  27208. /**
  27209. @@ -3317,14 +3355,14 @@ static void pwq_unbound_release_workfn(struct work_struct *work)
  27210. put_unbound_pool(pool);
  27211. mutex_unlock(&wq_pool_mutex);
  27212. - call_rcu_sched(&pwq->rcu, rcu_free_pwq);
  27213. + call_rcu(&pwq->rcu, rcu_free_pwq);
  27214. /*
  27215. * If we're the last pwq going away, @wq is already dead and no one
  27216. * is gonna access it anymore. Schedule RCU free.
  27217. */
  27218. if (is_last)
  27219. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  27220. + call_rcu(&wq->rcu, rcu_free_wq);
  27221. }
  27222. /**
  27223. @@ -3991,7 +4029,7 @@ void destroy_workqueue(struct workqueue_struct *wq)
  27224. * The base ref is never dropped on per-cpu pwqs. Directly
  27225. * schedule RCU free.
  27226. */
  27227. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  27228. + call_rcu(&wq->rcu, rcu_free_wq);
  27229. } else {
  27230. /*
  27231. * We're the sole accessor of @wq at this point. Directly
  27232. @@ -4085,7 +4123,8 @@ bool workqueue_congested(int cpu, struct workqueue_struct *wq)
  27233. struct pool_workqueue *pwq;
  27234. bool ret;
  27235. - rcu_read_lock_sched();
  27236. + rcu_read_lock();
  27237. + preempt_disable();
  27238. if (cpu == WORK_CPU_UNBOUND)
  27239. cpu = smp_processor_id();
  27240. @@ -4096,7 +4135,8 @@ bool workqueue_congested(int cpu, struct workqueue_struct *wq)
  27241. pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
  27242. ret = !list_empty(&pwq->delayed_works);
  27243. - rcu_read_unlock_sched();
  27244. + preempt_enable();
  27245. + rcu_read_unlock();
  27246. return ret;
  27247. }
  27248. @@ -4122,15 +4162,15 @@ unsigned int work_busy(struct work_struct *work)
  27249. if (work_pending(work))
  27250. ret |= WORK_BUSY_PENDING;
  27251. - local_irq_save(flags);
  27252. + rcu_read_lock();
  27253. pool = get_work_pool(work);
  27254. if (pool) {
  27255. - spin_lock(&pool->lock);
  27256. + spin_lock_irqsave(&pool->lock, flags);
  27257. if (find_worker_executing_work(pool, work))
  27258. ret |= WORK_BUSY_RUNNING;
  27259. - spin_unlock(&pool->lock);
  27260. + spin_unlock_irqrestore(&pool->lock, flags);
  27261. }
  27262. - local_irq_restore(flags);
  27263. + rcu_read_unlock();
  27264. return ret;
  27265. }
  27266. @@ -4319,7 +4359,7 @@ void show_workqueue_state(void)
  27267. unsigned long flags;
  27268. int pi;
  27269. - rcu_read_lock_sched();
  27270. + rcu_read_lock();
  27271. pr_info("Showing busy workqueues and worker pools:\n");
  27272. @@ -4370,7 +4410,7 @@ void show_workqueue_state(void)
  27273. spin_unlock_irqrestore(&pool->lock, flags);
  27274. }
  27275. - rcu_read_unlock_sched();
  27276. + rcu_read_unlock();
  27277. }
  27278. /*
  27279. @@ -4731,16 +4771,16 @@ bool freeze_workqueues_busy(void)
  27280. * nr_active is monotonically decreasing. It's safe
  27281. * to peek without lock.
  27282. */
  27283. - rcu_read_lock_sched();
  27284. + rcu_read_lock();
  27285. for_each_pwq(pwq, wq) {
  27286. WARN_ON_ONCE(pwq->nr_active < 0);
  27287. if (pwq->nr_active) {
  27288. busy = true;
  27289. - rcu_read_unlock_sched();
  27290. + rcu_read_unlock();
  27291. goto out_unlock;
  27292. }
  27293. }
  27294. - rcu_read_unlock_sched();
  27295. + rcu_read_unlock();
  27296. }
  27297. out_unlock:
  27298. mutex_unlock(&wq_pool_mutex);
  27299. @@ -4930,7 +4970,8 @@ static ssize_t wq_pool_ids_show(struct device *dev,
  27300. const char *delim = "";
  27301. int node, written = 0;
  27302. - rcu_read_lock_sched();
  27303. + get_online_cpus();
  27304. + rcu_read_lock();
  27305. for_each_node(node) {
  27306. written += scnprintf(buf + written, PAGE_SIZE - written,
  27307. "%s%d:%d", delim, node,
  27308. @@ -4938,7 +4979,8 @@ static ssize_t wq_pool_ids_show(struct device *dev,
  27309. delim = " ";
  27310. }
  27311. written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
  27312. - rcu_read_unlock_sched();
  27313. + rcu_read_unlock();
  27314. + put_online_cpus();
  27315. return written;
  27316. }
  27317. diff --git a/kernel/workqueue_internal.h b/kernel/workqueue_internal.h
  27318. index 3fa9c146fccb..42d1e3974554 100644
  27319. --- a/kernel/workqueue_internal.h
  27320. +++ b/kernel/workqueue_internal.h
  27321. @@ -44,6 +44,7 @@ struct worker {
  27322. unsigned long last_active; /* L: last active timestamp */
  27323. unsigned int flags; /* X: flags */
  27324. int id; /* I: worker id */
  27325. + int sleeping; /* None */
  27326. /*
  27327. * Opaque string set with work_set_desc(). Printed out with task
  27328. @@ -69,7 +70,7 @@ static inline struct worker *current_wq_worker(void)
  27329. * Scheduler hooks for concurrency managed workqueue. Only to be used from
  27330. * sched/core.c and workqueue.c.
  27331. */
  27332. -void wq_worker_waking_up(struct task_struct *task, int cpu);
  27333. -struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu);
  27334. +void wq_worker_running(struct task_struct *task);
  27335. +void wq_worker_sleeping(struct task_struct *task);
  27336. #endif /* _KERNEL_WORKQUEUE_INTERNAL_H */
  27337. diff --git a/lib/Kconfig b/lib/Kconfig
  27338. index 1a48744253d7..f75de578cca8 100644
  27339. --- a/lib/Kconfig
  27340. +++ b/lib/Kconfig
  27341. @@ -397,6 +397,7 @@ config CHECK_SIGNATURE
  27342. config CPUMASK_OFFSTACK
  27343. bool "Force CPU masks off stack" if DEBUG_PER_CPU_MAPS
  27344. + depends on !PREEMPT_RT_FULL
  27345. help
  27346. Use dynamic allocation for cpumask_var_t, instead of putting
  27347. them on the stack. This is a bit more expensive, but avoids
  27348. diff --git a/lib/debugobjects.c b/lib/debugobjects.c
  27349. index 547f7f923dbc..8fcdbc2fc6d0 100644
  27350. --- a/lib/debugobjects.c
  27351. +++ b/lib/debugobjects.c
  27352. @@ -309,7 +309,10 @@ __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
  27353. struct debug_obj *obj;
  27354. unsigned long flags;
  27355. - fill_pool();
  27356. +#ifdef CONFIG_PREEMPT_RT_FULL
  27357. + if (preempt_count() == 0 && !irqs_disabled())
  27358. +#endif
  27359. + fill_pool();
  27360. db = get_bucket((unsigned long) addr);
  27361. diff --git a/lib/idr.c b/lib/idr.c
  27362. index 6098336df267..9decbe914595 100644
  27363. --- a/lib/idr.c
  27364. +++ b/lib/idr.c
  27365. @@ -30,6 +30,7 @@
  27366. #include <linux/idr.h>
  27367. #include <linux/spinlock.h>
  27368. #include <linux/percpu.h>
  27369. +#include <linux/locallock.h>
  27370. #define MAX_IDR_SHIFT (sizeof(int) * 8 - 1)
  27371. #define MAX_IDR_BIT (1U << MAX_IDR_SHIFT)
  27372. @@ -45,6 +46,37 @@ static DEFINE_PER_CPU(struct idr_layer *, idr_preload_head);
  27373. static DEFINE_PER_CPU(int, idr_preload_cnt);
  27374. static DEFINE_SPINLOCK(simple_ida_lock);
  27375. +#ifdef CONFIG_PREEMPT_RT_FULL
  27376. +static DEFINE_LOCAL_IRQ_LOCK(idr_lock);
  27377. +
  27378. +static inline void idr_preload_lock(void)
  27379. +{
  27380. + local_lock(idr_lock);
  27381. +}
  27382. +
  27383. +static inline void idr_preload_unlock(void)
  27384. +{
  27385. + local_unlock(idr_lock);
  27386. +}
  27387. +
  27388. +void idr_preload_end(void)
  27389. +{
  27390. + idr_preload_unlock();
  27391. +}
  27392. +EXPORT_SYMBOL(idr_preload_end);
  27393. +#else
  27394. +static inline void idr_preload_lock(void)
  27395. +{
  27396. + preempt_disable();
  27397. +}
  27398. +
  27399. +static inline void idr_preload_unlock(void)
  27400. +{
  27401. + preempt_enable();
  27402. +}
  27403. +#endif
  27404. +
  27405. +
  27406. /* the maximum ID which can be allocated given idr->layers */
  27407. static int idr_max(int layers)
  27408. {
  27409. @@ -115,14 +147,14 @@ static struct idr_layer *idr_layer_alloc(gfp_t gfp_mask, struct idr *layer_idr)
  27410. * context. See idr_preload() for details.
  27411. */
  27412. if (!in_interrupt()) {
  27413. - preempt_disable();
  27414. + idr_preload_lock();
  27415. new = __this_cpu_read(idr_preload_head);
  27416. if (new) {
  27417. __this_cpu_write(idr_preload_head, new->ary[0]);
  27418. __this_cpu_dec(idr_preload_cnt);
  27419. new->ary[0] = NULL;
  27420. }
  27421. - preempt_enable();
  27422. + idr_preload_unlock();
  27423. if (new)
  27424. return new;
  27425. }
  27426. @@ -366,7 +398,6 @@ static void idr_fill_slot(struct idr *idr, void *ptr, int id,
  27427. idr_mark_full(pa, id);
  27428. }
  27429. -
  27430. /**
  27431. * idr_preload - preload for idr_alloc()
  27432. * @gfp_mask: allocation mask to use for preloading
  27433. @@ -401,7 +432,7 @@ void idr_preload(gfp_t gfp_mask)
  27434. WARN_ON_ONCE(in_interrupt());
  27435. might_sleep_if(gfpflags_allow_blocking(gfp_mask));
  27436. - preempt_disable();
  27437. + idr_preload_lock();
  27438. /*
  27439. * idr_alloc() is likely to succeed w/o full idr_layer buffer and
  27440. @@ -413,9 +444,9 @@ void idr_preload(gfp_t gfp_mask)
  27441. while (__this_cpu_read(idr_preload_cnt) < MAX_IDR_FREE) {
  27442. struct idr_layer *new;
  27443. - preempt_enable();
  27444. + idr_preload_unlock();
  27445. new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
  27446. - preempt_disable();
  27447. + idr_preload_lock();
  27448. if (!new)
  27449. break;
  27450. diff --git a/lib/locking-selftest.c b/lib/locking-selftest.c
  27451. index 872a15a2a637..b93a6103fa4d 100644
  27452. --- a/lib/locking-selftest.c
  27453. +++ b/lib/locking-selftest.c
  27454. @@ -590,6 +590,8 @@ GENERATE_TESTCASE(init_held_rsem)
  27455. #include "locking-selftest-spin-hardirq.h"
  27456. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
  27457. +#ifndef CONFIG_PREEMPT_RT_FULL
  27458. +
  27459. #include "locking-selftest-rlock-hardirq.h"
  27460. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
  27461. @@ -605,9 +607,12 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
  27462. #include "locking-selftest-wlock-softirq.h"
  27463. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
  27464. +#endif
  27465. +
  27466. #undef E1
  27467. #undef E2
  27468. +#ifndef CONFIG_PREEMPT_RT_FULL
  27469. /*
  27470. * Enabling hardirqs with a softirq-safe lock held:
  27471. */
  27472. @@ -640,6 +645,8 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
  27473. #undef E1
  27474. #undef E2
  27475. +#endif
  27476. +
  27477. /*
  27478. * Enabling irqs with an irq-safe lock held:
  27479. */
  27480. @@ -663,6 +670,8 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
  27481. #include "locking-selftest-spin-hardirq.h"
  27482. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
  27483. +#ifndef CONFIG_PREEMPT_RT_FULL
  27484. +
  27485. #include "locking-selftest-rlock-hardirq.h"
  27486. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
  27487. @@ -678,6 +687,8 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
  27488. #include "locking-selftest-wlock-softirq.h"
  27489. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
  27490. +#endif
  27491. +
  27492. #undef E1
  27493. #undef E2
  27494. @@ -709,6 +720,8 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
  27495. #include "locking-selftest-spin-hardirq.h"
  27496. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
  27497. +#ifndef CONFIG_PREEMPT_RT_FULL
  27498. +
  27499. #include "locking-selftest-rlock-hardirq.h"
  27500. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
  27501. @@ -724,6 +737,8 @@ GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
  27502. #include "locking-selftest-wlock-softirq.h"
  27503. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
  27504. +#endif
  27505. +
  27506. #undef E1
  27507. #undef E2
  27508. #undef E3
  27509. @@ -757,6 +772,8 @@ GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
  27510. #include "locking-selftest-spin-hardirq.h"
  27511. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
  27512. +#ifndef CONFIG_PREEMPT_RT_FULL
  27513. +
  27514. #include "locking-selftest-rlock-hardirq.h"
  27515. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
  27516. @@ -772,10 +789,14 @@ GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
  27517. #include "locking-selftest-wlock-softirq.h"
  27518. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
  27519. +#endif
  27520. +
  27521. #undef E1
  27522. #undef E2
  27523. #undef E3
  27524. +#ifndef CONFIG_PREEMPT_RT_FULL
  27525. +
  27526. /*
  27527. * read-lock / write-lock irq inversion.
  27528. *
  27529. @@ -838,6 +859,10 @@ GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
  27530. #undef E2
  27531. #undef E3
  27532. +#endif
  27533. +
  27534. +#ifndef CONFIG_PREEMPT_RT_FULL
  27535. +
  27536. /*
  27537. * read-lock / write-lock recursion that is actually safe.
  27538. */
  27539. @@ -876,6 +901,8 @@ GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft)
  27540. #undef E2
  27541. #undef E3
  27542. +#endif
  27543. +
  27544. /*
  27545. * read-lock / write-lock recursion that is unsafe.
  27546. */
  27547. @@ -1858,6 +1885,7 @@ void locking_selftest(void)
  27548. printk(" --------------------------------------------------------------------------\n");
  27549. +#ifndef CONFIG_PREEMPT_RT_FULL
  27550. /*
  27551. * irq-context testcases:
  27552. */
  27553. @@ -1870,6 +1898,28 @@ void locking_selftest(void)
  27554. DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
  27555. // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
  27556. +#else
  27557. + /* On -rt, we only do hardirq context test for raw spinlock */
  27558. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 12);
  27559. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 21);
  27560. +
  27561. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 12);
  27562. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 21);
  27563. +
  27564. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 123);
  27565. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 132);
  27566. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 213);
  27567. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 231);
  27568. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 312);
  27569. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 321);
  27570. +
  27571. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 123);
  27572. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 132);
  27573. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 213);
  27574. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 231);
  27575. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 312);
  27576. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 321);
  27577. +#endif
  27578. ww_tests();
  27579. diff --git a/lib/percpu_ida.c b/lib/percpu_ida.c
  27580. index 6d40944960de..822a2c027e72 100644
  27581. --- a/lib/percpu_ida.c
  27582. +++ b/lib/percpu_ida.c
  27583. @@ -26,6 +26,9 @@
  27584. #include <linux/string.h>
  27585. #include <linux/spinlock.h>
  27586. #include <linux/percpu_ida.h>
  27587. +#include <linux/locallock.h>
  27588. +
  27589. +static DEFINE_LOCAL_IRQ_LOCK(irq_off_lock);
  27590. struct percpu_ida_cpu {
  27591. /*
  27592. @@ -148,13 +151,13 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
  27593. unsigned long flags;
  27594. int tag;
  27595. - local_irq_save(flags);
  27596. + local_lock_irqsave(irq_off_lock, flags);
  27597. tags = this_cpu_ptr(pool->tag_cpu);
  27598. /* Fastpath */
  27599. tag = alloc_local_tag(tags);
  27600. if (likely(tag >= 0)) {
  27601. - local_irq_restore(flags);
  27602. + local_unlock_irqrestore(irq_off_lock, flags);
  27603. return tag;
  27604. }
  27605. @@ -173,6 +176,7 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
  27606. if (!tags->nr_free)
  27607. alloc_global_tags(pool, tags);
  27608. +
  27609. if (!tags->nr_free)
  27610. steal_tags(pool, tags);
  27611. @@ -184,7 +188,7 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
  27612. }
  27613. spin_unlock(&pool->lock);
  27614. - local_irq_restore(flags);
  27615. + local_unlock_irqrestore(irq_off_lock, flags);
  27616. if (tag >= 0 || state == TASK_RUNNING)
  27617. break;
  27618. @@ -196,7 +200,7 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
  27619. schedule();
  27620. - local_irq_save(flags);
  27621. + local_lock_irqsave(irq_off_lock, flags);
  27622. tags = this_cpu_ptr(pool->tag_cpu);
  27623. }
  27624. if (state != TASK_RUNNING)
  27625. @@ -221,7 +225,7 @@ void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
  27626. BUG_ON(tag >= pool->nr_tags);
  27627. - local_irq_save(flags);
  27628. + local_lock_irqsave(irq_off_lock, flags);
  27629. tags = this_cpu_ptr(pool->tag_cpu);
  27630. spin_lock(&tags->lock);
  27631. @@ -253,7 +257,7 @@ void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
  27632. spin_unlock(&pool->lock);
  27633. }
  27634. - local_irq_restore(flags);
  27635. + local_unlock_irqrestore(irq_off_lock, flags);
  27636. }
  27637. EXPORT_SYMBOL_GPL(percpu_ida_free);
  27638. @@ -345,7 +349,7 @@ int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
  27639. struct percpu_ida_cpu *remote;
  27640. unsigned cpu, i, err = 0;
  27641. - local_irq_save(flags);
  27642. + local_lock_irqsave(irq_off_lock, flags);
  27643. for_each_possible_cpu(cpu) {
  27644. remote = per_cpu_ptr(pool->tag_cpu, cpu);
  27645. spin_lock(&remote->lock);
  27646. @@ -367,7 +371,7 @@ int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
  27647. }
  27648. spin_unlock(&pool->lock);
  27649. out:
  27650. - local_irq_restore(flags);
  27651. + local_unlock_irqrestore(irq_off_lock, flags);
  27652. return err;
  27653. }
  27654. EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
  27655. diff --git a/lib/radix-tree.c b/lib/radix-tree.c
  27656. index 6b79e9026e24..44bf36a396a9 100644
  27657. --- a/lib/radix-tree.c
  27658. +++ b/lib/radix-tree.c
  27659. @@ -34,7 +34,7 @@
  27660. #include <linux/bitops.h>
  27661. #include <linux/rcupdate.h>
  27662. #include <linux/preempt.h> /* in_interrupt() */
  27663. -
  27664. +#include <linux/locallock.h>
  27665. /*
  27666. * The height_to_maxindex array needs to be one deeper than the maximum
  27667. @@ -69,6 +69,7 @@ struct radix_tree_preload {
  27668. struct radix_tree_node *nodes;
  27669. };
  27670. static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
  27671. +static DEFINE_LOCAL_IRQ_LOCK(radix_tree_preloads_lock);
  27672. static inline void *ptr_to_indirect(void *ptr)
  27673. {
  27674. @@ -196,13 +197,14 @@ radix_tree_node_alloc(struct radix_tree_root *root)
  27675. * succeed in getting a node here (and never reach
  27676. * kmem_cache_alloc)
  27677. */
  27678. - rtp = this_cpu_ptr(&radix_tree_preloads);
  27679. + rtp = &get_locked_var(radix_tree_preloads_lock, radix_tree_preloads);
  27680. if (rtp->nr) {
  27681. ret = rtp->nodes;
  27682. rtp->nodes = ret->private_data;
  27683. ret->private_data = NULL;
  27684. rtp->nr--;
  27685. }
  27686. + put_locked_var(radix_tree_preloads_lock, radix_tree_preloads);
  27687. /*
  27688. * Update the allocation stack trace as this is more useful
  27689. * for debugging.
  27690. @@ -257,14 +259,14 @@ static int __radix_tree_preload(gfp_t gfp_mask)
  27691. struct radix_tree_node *node;
  27692. int ret = -ENOMEM;
  27693. - preempt_disable();
  27694. + local_lock(radix_tree_preloads_lock);
  27695. rtp = this_cpu_ptr(&radix_tree_preloads);
  27696. while (rtp->nr < RADIX_TREE_PRELOAD_SIZE) {
  27697. - preempt_enable();
  27698. + local_unlock(radix_tree_preloads_lock);
  27699. node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
  27700. if (node == NULL)
  27701. goto out;
  27702. - preempt_disable();
  27703. + local_lock(radix_tree_preloads_lock);
  27704. rtp = this_cpu_ptr(&radix_tree_preloads);
  27705. if (rtp->nr < RADIX_TREE_PRELOAD_SIZE) {
  27706. node->private_data = rtp->nodes;
  27707. @@ -306,11 +308,17 @@ int radix_tree_maybe_preload(gfp_t gfp_mask)
  27708. if (gfpflags_allow_blocking(gfp_mask))
  27709. return __radix_tree_preload(gfp_mask);
  27710. /* Preloading doesn't help anything with this gfp mask, skip it */
  27711. - preempt_disable();
  27712. + local_lock(radix_tree_preloads_lock);
  27713. return 0;
  27714. }
  27715. EXPORT_SYMBOL(radix_tree_maybe_preload);
  27716. +void radix_tree_preload_end(void)
  27717. +{
  27718. + local_unlock(radix_tree_preloads_lock);
  27719. +}
  27720. +EXPORT_SYMBOL(radix_tree_preload_end);
  27721. +
  27722. /*
  27723. * Return the maximum key which can be store into a
  27724. * radix tree with height HEIGHT.
  27725. diff --git a/lib/rbtree.c b/lib/rbtree.c
  27726. index 1356454e36de..d15d6c4327f1 100644
  27727. --- a/lib/rbtree.c
  27728. +++ b/lib/rbtree.c
  27729. @@ -23,6 +23,7 @@
  27730. #include <linux/rbtree_augmented.h>
  27731. #include <linux/export.h>
  27732. +#include <linux/rcupdate.h>
  27733. /*
  27734. * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
  27735. @@ -590,3 +591,13 @@ struct rb_node *rb_first_postorder(const struct rb_root *root)
  27736. return rb_left_deepest_node(root->rb_node);
  27737. }
  27738. EXPORT_SYMBOL(rb_first_postorder);
  27739. +
  27740. +void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  27741. + struct rb_node **rb_link)
  27742. +{
  27743. + node->__rb_parent_color = (unsigned long)parent;
  27744. + node->rb_left = node->rb_right = NULL;
  27745. +
  27746. + rcu_assign_pointer(*rb_link, node);
  27747. +}
  27748. +EXPORT_SYMBOL(rb_link_node_rcu);
  27749. diff --git a/lib/scatterlist.c b/lib/scatterlist.c
  27750. index bafa9933fa76..ebe3b7edd086 100644
  27751. --- a/lib/scatterlist.c
  27752. +++ b/lib/scatterlist.c
  27753. @@ -620,7 +620,7 @@ void sg_miter_stop(struct sg_mapping_iter *miter)
  27754. flush_kernel_dcache_page(miter->page);
  27755. if (miter->__flags & SG_MITER_ATOMIC) {
  27756. - WARN_ON_ONCE(preemptible());
  27757. + WARN_ON_ONCE(!pagefault_disabled());
  27758. kunmap_atomic(miter->addr);
  27759. } else
  27760. kunmap(miter->page);
  27761. @@ -664,7 +664,7 @@ size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
  27762. if (!sg_miter_skip(&miter, skip))
  27763. return false;
  27764. - local_irq_save(flags);
  27765. + local_irq_save_nort(flags);
  27766. while (sg_miter_next(&miter) && offset < buflen) {
  27767. unsigned int len;
  27768. @@ -681,7 +681,7 @@ size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
  27769. sg_miter_stop(&miter);
  27770. - local_irq_restore(flags);
  27771. + local_irq_restore_nort(flags);
  27772. return offset;
  27773. }
  27774. EXPORT_SYMBOL(sg_copy_buffer);
  27775. diff --git a/lib/smp_processor_id.c b/lib/smp_processor_id.c
  27776. index 1afec32de6f2..11fa431046a8 100644
  27777. --- a/lib/smp_processor_id.c
  27778. +++ b/lib/smp_processor_id.c
  27779. @@ -39,8 +39,9 @@ notrace static unsigned int check_preemption_disabled(const char *what1,
  27780. if (!printk_ratelimit())
  27781. goto out_enable;
  27782. - printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
  27783. - what1, what2, preempt_count() - 1, current->comm, current->pid);
  27784. + printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x %08x] code: %s/%d\n",
  27785. + what1, what2, preempt_count() - 1, __migrate_disabled(current),
  27786. + current->comm, current->pid);
  27787. print_symbol("caller is %s\n", (long)__builtin_return_address(0));
  27788. dump_stack();
  27789. diff --git a/mm/Kconfig b/mm/Kconfig
  27790. index 97a4e06b15c0..9614351e68b8 100644
  27791. --- a/mm/Kconfig
  27792. +++ b/mm/Kconfig
  27793. @@ -392,7 +392,7 @@ config NOMMU_INITIAL_TRIM_EXCESS
  27794. config TRANSPARENT_HUGEPAGE
  27795. bool "Transparent Hugepage Support"
  27796. - depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE
  27797. + depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE && !PREEMPT_RT_FULL
  27798. select COMPACTION
  27799. help
  27800. Transparent Hugepages allows the kernel to use huge pages and
  27801. diff --git a/mm/backing-dev.c b/mm/backing-dev.c
  27802. index a988d4ef39da..f2c2ee1d5191 100644
  27803. --- a/mm/backing-dev.c
  27804. +++ b/mm/backing-dev.c
  27805. @@ -457,9 +457,9 @@ void wb_congested_put(struct bdi_writeback_congested *congested)
  27806. {
  27807. unsigned long flags;
  27808. - local_irq_save(flags);
  27809. + local_irq_save_nort(flags);
  27810. if (!atomic_dec_and_lock(&congested->refcnt, &cgwb_lock)) {
  27811. - local_irq_restore(flags);
  27812. + local_irq_restore_nort(flags);
  27813. return;
  27814. }
  27815. diff --git a/mm/compaction.c b/mm/compaction.c
  27816. index b6f145ed7ae1..03cac7f6768a 100644
  27817. --- a/mm/compaction.c
  27818. +++ b/mm/compaction.c
  27819. @@ -1450,10 +1450,12 @@ static int compact_zone(struct zone *zone, struct compact_control *cc)
  27820. cc->migrate_pfn & ~((1UL << cc->order) - 1);
  27821. if (cc->last_migrated_pfn < current_block_start) {
  27822. - cpu = get_cpu();
  27823. + cpu = get_cpu_light();
  27824. + local_lock_irq(swapvec_lock);
  27825. lru_add_drain_cpu(cpu);
  27826. + local_unlock_irq(swapvec_lock);
  27827. drain_local_pages(zone);
  27828. - put_cpu();
  27829. + put_cpu_light();
  27830. /* No more flushing until we migrate again */
  27831. cc->last_migrated_pfn = 0;
  27832. }
  27833. diff --git a/mm/filemap.c b/mm/filemap.c
  27834. index 69f75c77c098..b203169ca0b4 100644
  27835. --- a/mm/filemap.c
  27836. +++ b/mm/filemap.c
  27837. @@ -144,9 +144,12 @@ static int page_cache_tree_insert(struct address_space *mapping,
  27838. * node->private_list is protected by
  27839. * mapping->tree_lock.
  27840. */
  27841. - if (!list_empty(&node->private_list))
  27842. - list_lru_del(&workingset_shadow_nodes,
  27843. + if (!list_empty(&node->private_list)) {
  27844. + local_lock(workingset_shadow_lock);
  27845. + list_lru_del(&__workingset_shadow_nodes,
  27846. &node->private_list);
  27847. + local_unlock(workingset_shadow_lock);
  27848. + }
  27849. }
  27850. return 0;
  27851. }
  27852. @@ -218,7 +221,9 @@ static void page_cache_tree_delete(struct address_space *mapping,
  27853. if (!workingset_node_pages(node) &&
  27854. list_empty(&node->private_list)) {
  27855. node->private_data = mapping;
  27856. - list_lru_add(&workingset_shadow_nodes, &node->private_list);
  27857. + local_lock(workingset_shadow_lock);
  27858. + list_lru_add(&__workingset_shadow_nodes, &node->private_list);
  27859. + local_unlock(workingset_shadow_lock);
  27860. }
  27861. }
  27862. diff --git a/mm/highmem.c b/mm/highmem.c
  27863. index 123bcd3ed4f2..16e8cf26d38a 100644
  27864. --- a/mm/highmem.c
  27865. +++ b/mm/highmem.c
  27866. @@ -29,10 +29,11 @@
  27867. #include <linux/kgdb.h>
  27868. #include <asm/tlbflush.h>
  27869. -
  27870. +#ifndef CONFIG_PREEMPT_RT_FULL
  27871. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  27872. DEFINE_PER_CPU(int, __kmap_atomic_idx);
  27873. #endif
  27874. +#endif
  27875. /*
  27876. * Virtual_count is not a pure "count".
  27877. @@ -107,8 +108,9 @@ static inline wait_queue_head_t *get_pkmap_wait_queue_head(unsigned int color)
  27878. unsigned long totalhigh_pages __read_mostly;
  27879. EXPORT_SYMBOL(totalhigh_pages);
  27880. -
  27881. +#ifndef CONFIG_PREEMPT_RT_FULL
  27882. EXPORT_PER_CPU_SYMBOL(__kmap_atomic_idx);
  27883. +#endif
  27884. unsigned int nr_free_highpages (void)
  27885. {
  27886. diff --git a/mm/memcontrol.c b/mm/memcontrol.c
  27887. index e25b93a4267d..1c619267d9da 100644
  27888. --- a/mm/memcontrol.c
  27889. +++ b/mm/memcontrol.c
  27890. @@ -67,6 +67,8 @@
  27891. #include <net/sock.h>
  27892. #include <net/ip.h>
  27893. #include <net/tcp_memcontrol.h>
  27894. +#include <linux/locallock.h>
  27895. +
  27896. #include "slab.h"
  27897. #include <asm/uaccess.h>
  27898. @@ -87,6 +89,7 @@ int do_swap_account __read_mostly;
  27899. #define do_swap_account 0
  27900. #endif
  27901. +static DEFINE_LOCAL_IRQ_LOCK(event_lock);
  27902. static const char * const mem_cgroup_stat_names[] = {
  27903. "cache",
  27904. "rss",
  27905. @@ -1922,14 +1925,17 @@ static void drain_local_stock(struct work_struct *dummy)
  27906. */
  27907. static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
  27908. {
  27909. - struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
  27910. + struct memcg_stock_pcp *stock;
  27911. + int cpu = get_cpu_light();
  27912. +
  27913. + stock = &per_cpu(memcg_stock, cpu);
  27914. if (stock->cached != memcg) { /* reset if necessary */
  27915. drain_stock(stock);
  27916. stock->cached = memcg;
  27917. }
  27918. stock->nr_pages += nr_pages;
  27919. - put_cpu_var(memcg_stock);
  27920. + put_cpu_light();
  27921. }
  27922. /*
  27923. @@ -1945,7 +1951,7 @@ static void drain_all_stock(struct mem_cgroup *root_memcg)
  27924. return;
  27925. /* Notify other cpus that system-wide "drain" is running */
  27926. get_online_cpus();
  27927. - curcpu = get_cpu();
  27928. + curcpu = get_cpu_light();
  27929. for_each_online_cpu(cpu) {
  27930. struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
  27931. struct mem_cgroup *memcg;
  27932. @@ -1962,7 +1968,7 @@ static void drain_all_stock(struct mem_cgroup *root_memcg)
  27933. schedule_work_on(cpu, &stock->work);
  27934. }
  27935. }
  27936. - put_cpu();
  27937. + put_cpu_light();
  27938. put_online_cpus();
  27939. mutex_unlock(&percpu_charge_mutex);
  27940. }
  27941. @@ -4691,12 +4697,12 @@ static int mem_cgroup_move_account(struct page *page,
  27942. ret = 0;
  27943. - local_irq_disable();
  27944. + local_lock_irq(event_lock);
  27945. mem_cgroup_charge_statistics(to, page, nr_pages);
  27946. memcg_check_events(to, page);
  27947. mem_cgroup_charge_statistics(from, page, -nr_pages);
  27948. memcg_check_events(from, page);
  27949. - local_irq_enable();
  27950. + local_unlock_irq(event_lock);
  27951. out_unlock:
  27952. unlock_page(page);
  27953. out:
  27954. @@ -5486,10 +5492,10 @@ void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
  27955. VM_BUG_ON_PAGE(!PageTransHuge(page), page);
  27956. }
  27957. - local_irq_disable();
  27958. + local_lock_irq(event_lock);
  27959. mem_cgroup_charge_statistics(memcg, page, nr_pages);
  27960. memcg_check_events(memcg, page);
  27961. - local_irq_enable();
  27962. + local_unlock_irq(event_lock);
  27963. if (do_swap_account && PageSwapCache(page)) {
  27964. swp_entry_t entry = { .val = page_private(page) };
  27965. @@ -5545,14 +5551,14 @@ static void uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout,
  27966. memcg_oom_recover(memcg);
  27967. }
  27968. - local_irq_save(flags);
  27969. + local_lock_irqsave(event_lock, flags);
  27970. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS], nr_anon);
  27971. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_CACHE], nr_file);
  27972. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], nr_huge);
  27973. __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT], pgpgout);
  27974. __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
  27975. memcg_check_events(memcg, dummy_page);
  27976. - local_irq_restore(flags);
  27977. + local_unlock_irqrestore(event_lock, flags);
  27978. if (!mem_cgroup_is_root(memcg))
  27979. css_put_many(&memcg->css, nr_pages);
  27980. @@ -5762,6 +5768,7 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
  27981. {
  27982. struct mem_cgroup *memcg, *swap_memcg;
  27983. unsigned short oldid;
  27984. + unsigned long flags;
  27985. VM_BUG_ON_PAGE(PageLRU(page), page);
  27986. VM_BUG_ON_PAGE(page_count(page), page);
  27987. @@ -5802,12 +5809,16 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
  27988. * important here to have the interrupts disabled because it is the
  27989. * only synchronisation we have for udpating the per-CPU variables.
  27990. */
  27991. + local_lock_irqsave(event_lock, flags);
  27992. +#ifndef CONFIG_PREEMPT_RT_BASE
  27993. VM_BUG_ON(!irqs_disabled());
  27994. +#endif
  27995. mem_cgroup_charge_statistics(memcg, page, -1);
  27996. memcg_check_events(memcg, page);
  27997. if (!mem_cgroup_is_root(memcg))
  27998. css_put(&memcg->css);
  27999. + local_unlock_irqrestore(event_lock, flags);
  28000. }
  28001. /**
  28002. diff --git a/mm/mmu_context.c b/mm/mmu_context.c
  28003. index 6f4d27c5bb32..5cd25c745a8f 100644
  28004. --- a/mm/mmu_context.c
  28005. +++ b/mm/mmu_context.c
  28006. @@ -23,6 +23,7 @@ void use_mm(struct mm_struct *mm)
  28007. struct task_struct *tsk = current;
  28008. task_lock(tsk);
  28009. + preempt_disable_rt();
  28010. active_mm = tsk->active_mm;
  28011. if (active_mm != mm) {
  28012. atomic_inc(&mm->mm_count);
  28013. @@ -30,6 +31,7 @@ void use_mm(struct mm_struct *mm)
  28014. }
  28015. tsk->mm = mm;
  28016. switch_mm(active_mm, mm, tsk);
  28017. + preempt_enable_rt();
  28018. task_unlock(tsk);
  28019. #ifdef finish_arch_post_lock_switch
  28020. finish_arch_post_lock_switch();
  28021. diff --git a/mm/page_alloc.c b/mm/page_alloc.c
  28022. index 3c70f03d91ec..e1377f157652 100644
  28023. --- a/mm/page_alloc.c
  28024. +++ b/mm/page_alloc.c
  28025. @@ -60,6 +60,7 @@
  28026. #include <linux/page_ext.h>
  28027. #include <linux/hugetlb.h>
  28028. #include <linux/sched/rt.h>
  28029. +#include <linux/locallock.h>
  28030. #include <linux/page_owner.h>
  28031. #include <linux/kthread.h>
  28032. @@ -264,6 +265,18 @@ EXPORT_SYMBOL(nr_node_ids);
  28033. EXPORT_SYMBOL(nr_online_nodes);
  28034. #endif
  28035. +static DEFINE_LOCAL_IRQ_LOCK(pa_lock);
  28036. +
  28037. +#ifdef CONFIG_PREEMPT_RT_BASE
  28038. +# define cpu_lock_irqsave(cpu, flags) \
  28039. + local_lock_irqsave_on(pa_lock, flags, cpu)
  28040. +# define cpu_unlock_irqrestore(cpu, flags) \
  28041. + local_unlock_irqrestore_on(pa_lock, flags, cpu)
  28042. +#else
  28043. +# define cpu_lock_irqsave(cpu, flags) local_irq_save(flags)
  28044. +# define cpu_unlock_irqrestore(cpu, flags) local_irq_restore(flags)
  28045. +#endif
  28046. +
  28047. int page_group_by_mobility_disabled __read_mostly;
  28048. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  28049. @@ -820,7 +833,7 @@ static inline int free_pages_check(struct page *page)
  28050. }
  28051. /*
  28052. - * Frees a number of pages from the PCP lists
  28053. + * Frees a number of pages which have been collected from the pcp lists.
  28054. * Assumes all pages on list are in same zone, and of same order.
  28055. * count is the number of pages to free.
  28056. *
  28057. @@ -831,18 +844,53 @@ static inline int free_pages_check(struct page *page)
  28058. * pinned" detection logic.
  28059. */
  28060. static void free_pcppages_bulk(struct zone *zone, int count,
  28061. - struct per_cpu_pages *pcp)
  28062. + struct list_head *list)
  28063. {
  28064. - int migratetype = 0;
  28065. - int batch_free = 0;
  28066. int to_free = count;
  28067. unsigned long nr_scanned;
  28068. + unsigned long flags;
  28069. +
  28070. + spin_lock_irqsave(&zone->lock, flags);
  28071. - spin_lock(&zone->lock);
  28072. nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
  28073. if (nr_scanned)
  28074. __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
  28075. + while (!list_empty(list)) {
  28076. + struct page *page = list_first_entry(list, struct page, lru);
  28077. + int mt; /* migratetype of the to-be-freed page */
  28078. +
  28079. + /* must delete as __free_one_page list manipulates */
  28080. + list_del(&page->lru);
  28081. +
  28082. + mt = get_pcppage_migratetype(page);
  28083. + /* MIGRATE_ISOLATE page should not go to pcplists */
  28084. + VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
  28085. + /* Pageblock could have been isolated meanwhile */
  28086. + if (unlikely(has_isolate_pageblock(zone)))
  28087. + mt = get_pageblock_migratetype(page);
  28088. +
  28089. + __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  28090. + trace_mm_page_pcpu_drain(page, 0, mt);
  28091. + to_free--;
  28092. + }
  28093. + WARN_ON(to_free != 0);
  28094. + spin_unlock_irqrestore(&zone->lock, flags);
  28095. +}
  28096. +
  28097. +/*
  28098. + * Moves a number of pages from the PCP lists to free list which
  28099. + * is freed outside of the locked region.
  28100. + *
  28101. + * Assumes all pages on list are in same zone, and of same order.
  28102. + * count is the number of pages to free.
  28103. + */
  28104. +static void isolate_pcp_pages(int to_free, struct per_cpu_pages *src,
  28105. + struct list_head *dst)
  28106. +{
  28107. + int migratetype = 0;
  28108. + int batch_free = 0;
  28109. +
  28110. while (to_free) {
  28111. struct page *page;
  28112. struct list_head *list;
  28113. @@ -858,7 +906,7 @@ static void free_pcppages_bulk(struct zone *zone, int count,
  28114. batch_free++;
  28115. if (++migratetype == MIGRATE_PCPTYPES)
  28116. migratetype = 0;
  28117. - list = &pcp->lists[migratetype];
  28118. + list = &src->lists[migratetype];
  28119. } while (list_empty(list));
  28120. /* This is the only non-empty list. Free them all. */
  28121. @@ -866,24 +914,12 @@ static void free_pcppages_bulk(struct zone *zone, int count,
  28122. batch_free = to_free;
  28123. do {
  28124. - int mt; /* migratetype of the to-be-freed page */
  28125. -
  28126. - page = list_entry(list->prev, struct page, lru);
  28127. - /* must delete as __free_one_page list manipulates */
  28128. + page = list_last_entry(list, struct page, lru);
  28129. list_del(&page->lru);
  28130. - mt = get_pcppage_migratetype(page);
  28131. - /* MIGRATE_ISOLATE page should not go to pcplists */
  28132. - VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
  28133. - /* Pageblock could have been isolated meanwhile */
  28134. - if (unlikely(has_isolate_pageblock(zone)))
  28135. - mt = get_pageblock_migratetype(page);
  28136. -
  28137. - __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  28138. - trace_mm_page_pcpu_drain(page, 0, mt);
  28139. + list_add(&page->lru, dst);
  28140. } while (--to_free && --batch_free && !list_empty(list));
  28141. }
  28142. - spin_unlock(&zone->lock);
  28143. }
  28144. static void free_one_page(struct zone *zone,
  28145. @@ -892,7 +928,9 @@ static void free_one_page(struct zone *zone,
  28146. int migratetype)
  28147. {
  28148. unsigned long nr_scanned;
  28149. - spin_lock(&zone->lock);
  28150. + unsigned long flags;
  28151. +
  28152. + spin_lock_irqsave(&zone->lock, flags);
  28153. nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
  28154. if (nr_scanned)
  28155. __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
  28156. @@ -902,7 +940,7 @@ static void free_one_page(struct zone *zone,
  28157. migratetype = get_pfnblock_migratetype(page, pfn);
  28158. }
  28159. __free_one_page(page, pfn, zone, order, migratetype);
  28160. - spin_unlock(&zone->lock);
  28161. + spin_unlock_irqrestore(&zone->lock, flags);
  28162. }
  28163. static int free_tail_pages_check(struct page *head_page, struct page *page)
  28164. @@ -1053,10 +1091,10 @@ static void __free_pages_ok(struct page *page, unsigned int order)
  28165. return;
  28166. migratetype = get_pfnblock_migratetype(page, pfn);
  28167. - local_irq_save(flags);
  28168. + local_lock_irqsave(pa_lock, flags);
  28169. __count_vm_events(PGFREE, 1 << order);
  28170. free_one_page(page_zone(page), page, pfn, order, migratetype);
  28171. - local_irq_restore(flags);
  28172. + local_unlock_irqrestore(pa_lock, flags);
  28173. }
  28174. static void __init __free_pages_boot_core(struct page *page,
  28175. @@ -1925,16 +1963,18 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
  28176. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
  28177. {
  28178. unsigned long flags;
  28179. + LIST_HEAD(dst);
  28180. int to_drain, batch;
  28181. - local_irq_save(flags);
  28182. + local_lock_irqsave(pa_lock, flags);
  28183. batch = READ_ONCE(pcp->batch);
  28184. to_drain = min(pcp->count, batch);
  28185. if (to_drain > 0) {
  28186. - free_pcppages_bulk(zone, to_drain, pcp);
  28187. + isolate_pcp_pages(to_drain, pcp, &dst);
  28188. pcp->count -= to_drain;
  28189. }
  28190. - local_irq_restore(flags);
  28191. + local_unlock_irqrestore(pa_lock, flags);
  28192. + free_pcppages_bulk(zone, to_drain, &dst);
  28193. }
  28194. #endif
  28195. @@ -1950,16 +1990,21 @@ static void drain_pages_zone(unsigned int cpu, struct zone *zone)
  28196. unsigned long flags;
  28197. struct per_cpu_pageset *pset;
  28198. struct per_cpu_pages *pcp;
  28199. + LIST_HEAD(dst);
  28200. + int count;
  28201. - local_irq_save(flags);
  28202. + cpu_lock_irqsave(cpu, flags);
  28203. pset = per_cpu_ptr(zone->pageset, cpu);
  28204. pcp = &pset->pcp;
  28205. - if (pcp->count) {
  28206. - free_pcppages_bulk(zone, pcp->count, pcp);
  28207. + count = pcp->count;
  28208. + if (count) {
  28209. + isolate_pcp_pages(count, pcp, &dst);
  28210. pcp->count = 0;
  28211. }
  28212. - local_irq_restore(flags);
  28213. + cpu_unlock_irqrestore(cpu, flags);
  28214. + if (count)
  28215. + free_pcppages_bulk(zone, count, &dst);
  28216. }
  28217. /*
  28218. @@ -2045,8 +2090,17 @@ void drain_all_pages(struct zone *zone)
  28219. else
  28220. cpumask_clear_cpu(cpu, &cpus_with_pcps);
  28221. }
  28222. +#ifndef CONFIG_PREEMPT_RT_BASE
  28223. on_each_cpu_mask(&cpus_with_pcps, (smp_call_func_t) drain_local_pages,
  28224. zone, 1);
  28225. +#else
  28226. + for_each_cpu(cpu, &cpus_with_pcps) {
  28227. + if (zone)
  28228. + drain_pages_zone(cpu, zone);
  28229. + else
  28230. + drain_pages(cpu);
  28231. + }
  28232. +#endif
  28233. }
  28234. #ifdef CONFIG_HIBERNATION
  28235. @@ -2102,7 +2156,7 @@ void free_hot_cold_page(struct page *page, bool cold)
  28236. migratetype = get_pfnblock_migratetype(page, pfn);
  28237. set_pcppage_migratetype(page, migratetype);
  28238. - local_irq_save(flags);
  28239. + local_lock_irqsave(pa_lock, flags);
  28240. __count_vm_event(PGFREE);
  28241. /*
  28242. @@ -2128,12 +2182,17 @@ void free_hot_cold_page(struct page *page, bool cold)
  28243. pcp->count++;
  28244. if (pcp->count >= pcp->high) {
  28245. unsigned long batch = READ_ONCE(pcp->batch);
  28246. - free_pcppages_bulk(zone, batch, pcp);
  28247. + LIST_HEAD(dst);
  28248. +
  28249. + isolate_pcp_pages(batch, pcp, &dst);
  28250. pcp->count -= batch;
  28251. + local_unlock_irqrestore(pa_lock, flags);
  28252. + free_pcppages_bulk(zone, batch, &dst);
  28253. + return;
  28254. }
  28255. out:
  28256. - local_irq_restore(flags);
  28257. + local_unlock_irqrestore(pa_lock, flags);
  28258. }
  28259. /*
  28260. @@ -2268,7 +2327,7 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
  28261. struct per_cpu_pages *pcp;
  28262. struct list_head *list;
  28263. - local_irq_save(flags);
  28264. + local_lock_irqsave(pa_lock, flags);
  28265. pcp = &this_cpu_ptr(zone->pageset)->pcp;
  28266. list = &pcp->lists[migratetype];
  28267. if (list_empty(list)) {
  28268. @@ -2300,7 +2359,7 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
  28269. */
  28270. WARN_ON_ONCE(order > 1);
  28271. }
  28272. - spin_lock_irqsave(&zone->lock, flags);
  28273. + local_spin_lock_irqsave(pa_lock, &zone->lock, flags);
  28274. page = NULL;
  28275. if (alloc_flags & ALLOC_HARDER) {
  28276. @@ -2310,11 +2369,13 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
  28277. }
  28278. if (!page)
  28279. page = __rmqueue(zone, order, migratetype, gfp_flags);
  28280. - spin_unlock(&zone->lock);
  28281. - if (!page)
  28282. + if (!page) {
  28283. + spin_unlock(&zone->lock);
  28284. goto failed;
  28285. + }
  28286. __mod_zone_freepage_state(zone, -(1 << order),
  28287. get_pcppage_migratetype(page));
  28288. + spin_unlock(&zone->lock);
  28289. }
  28290. __mod_zone_page_state(zone, NR_ALLOC_BATCH, -(1 << order));
  28291. @@ -2324,13 +2385,13 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
  28292. __count_zone_vm_events(PGALLOC, zone, 1 << order);
  28293. zone_statistics(preferred_zone, zone, gfp_flags);
  28294. - local_irq_restore(flags);
  28295. + local_unlock_irqrestore(pa_lock, flags);
  28296. VM_BUG_ON_PAGE(bad_range(zone, page), page);
  28297. return page;
  28298. failed:
  28299. - local_irq_restore(flags);
  28300. + local_unlock_irqrestore(pa_lock, flags);
  28301. return NULL;
  28302. }
  28303. @@ -5999,6 +6060,7 @@ static int page_alloc_cpu_notify(struct notifier_block *self,
  28304. void __init page_alloc_init(void)
  28305. {
  28306. hotcpu_notifier(page_alloc_cpu_notify, 0);
  28307. + local_irq_lock_init(pa_lock);
  28308. }
  28309. /*
  28310. @@ -6893,7 +6955,7 @@ void zone_pcp_reset(struct zone *zone)
  28311. struct per_cpu_pageset *pset;
  28312. /* avoid races with drain_pages() */
  28313. - local_irq_save(flags);
  28314. + local_lock_irqsave(pa_lock, flags);
  28315. if (zone->pageset != &boot_pageset) {
  28316. for_each_online_cpu(cpu) {
  28317. pset = per_cpu_ptr(zone->pageset, cpu);
  28318. @@ -6902,7 +6964,7 @@ void zone_pcp_reset(struct zone *zone)
  28319. free_percpu(zone->pageset);
  28320. zone->pageset = &boot_pageset;
  28321. }
  28322. - local_irq_restore(flags);
  28323. + local_unlock_irqrestore(pa_lock, flags);
  28324. }
  28325. #ifdef CONFIG_MEMORY_HOTREMOVE
  28326. diff --git a/mm/percpu.c b/mm/percpu.c
  28327. index ef6353f0adbd..33ccbac7cdb8 100644
  28328. --- a/mm/percpu.c
  28329. +++ b/mm/percpu.c
  28330. @@ -1285,18 +1285,7 @@ void free_percpu(void __percpu *ptr)
  28331. }
  28332. EXPORT_SYMBOL_GPL(free_percpu);
  28333. -/**
  28334. - * is_kernel_percpu_address - test whether address is from static percpu area
  28335. - * @addr: address to test
  28336. - *
  28337. - * Test whether @addr belongs to in-kernel static percpu area. Module
  28338. - * static percpu areas are not considered. For those, use
  28339. - * is_module_percpu_address().
  28340. - *
  28341. - * RETURNS:
  28342. - * %true if @addr is from in-kernel static percpu area, %false otherwise.
  28343. - */
  28344. -bool is_kernel_percpu_address(unsigned long addr)
  28345. +bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr)
  28346. {
  28347. #ifdef CONFIG_SMP
  28348. const size_t static_size = __per_cpu_end - __per_cpu_start;
  28349. @@ -1305,15 +1294,35 @@ bool is_kernel_percpu_address(unsigned long addr)
  28350. for_each_possible_cpu(cpu) {
  28351. void *start = per_cpu_ptr(base, cpu);
  28352. + void *va = (void *)addr;
  28353. - if ((void *)addr >= start && (void *)addr < start + static_size)
  28354. + if (va >= start && va < start + static_size) {
  28355. + if (can_addr)
  28356. + *can_addr = (unsigned long) (va - start);
  28357. return true;
  28358. - }
  28359. + }
  28360. + }
  28361. #endif
  28362. /* on UP, can't distinguish from other static vars, always false */
  28363. return false;
  28364. }
  28365. +/**
  28366. + * is_kernel_percpu_address - test whether address is from static percpu area
  28367. + * @addr: address to test
  28368. + *
  28369. + * Test whether @addr belongs to in-kernel static percpu area. Module
  28370. + * static percpu areas are not considered. For those, use
  28371. + * is_module_percpu_address().
  28372. + *
  28373. + * RETURNS:
  28374. + * %true if @addr is from in-kernel static percpu area, %false otherwise.
  28375. + */
  28376. +bool is_kernel_percpu_address(unsigned long addr)
  28377. +{
  28378. + return __is_kernel_percpu_address(addr, NULL);
  28379. +}
  28380. +
  28381. /**
  28382. * per_cpu_ptr_to_phys - convert translated percpu address to physical address
  28383. * @addr: the address to be converted to physical address
  28384. diff --git a/mm/slab.h b/mm/slab.h
  28385. index 7b6087197997..afdc57941179 100644
  28386. --- a/mm/slab.h
  28387. +++ b/mm/slab.h
  28388. @@ -324,7 +324,11 @@ static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
  28389. * The slab lists for all objects.
  28390. */
  28391. struct kmem_cache_node {
  28392. +#ifdef CONFIG_SLUB
  28393. + raw_spinlock_t list_lock;
  28394. +#else
  28395. spinlock_t list_lock;
  28396. +#endif
  28397. #ifdef CONFIG_SLAB
  28398. struct list_head slabs_partial; /* partial list first, better asm code */
  28399. diff --git a/mm/slub.c b/mm/slub.c
  28400. index 4cf3a9c768b1..b183c5271607 100644
  28401. --- a/mm/slub.c
  28402. +++ b/mm/slub.c
  28403. @@ -1075,7 +1075,7 @@ static noinline struct kmem_cache_node *free_debug_processing(
  28404. void *object = head;
  28405. int cnt = 0;
  28406. - spin_lock_irqsave(&n->list_lock, *flags);
  28407. + raw_spin_lock_irqsave(&n->list_lock, *flags);
  28408. slab_lock(page);
  28409. if (!check_slab(s, page))
  28410. @@ -1136,7 +1136,7 @@ static noinline struct kmem_cache_node *free_debug_processing(
  28411. fail:
  28412. slab_unlock(page);
  28413. - spin_unlock_irqrestore(&n->list_lock, *flags);
  28414. + raw_spin_unlock_irqrestore(&n->list_lock, *flags);
  28415. slab_fix(s, "Object at 0x%p not freed", object);
  28416. return NULL;
  28417. }
  28418. @@ -1263,6 +1263,12 @@ static inline void dec_slabs_node(struct kmem_cache *s, int node,
  28419. #endif /* CONFIG_SLUB_DEBUG */
  28420. +struct slub_free_list {
  28421. + raw_spinlock_t lock;
  28422. + struct list_head list;
  28423. +};
  28424. +static DEFINE_PER_CPU(struct slub_free_list, slub_free_list);
  28425. +
  28426. /*
  28427. * Hooks for other subsystems that check memory allocations. In a typical
  28428. * production configuration these hooks all should produce no code at all.
  28429. @@ -1399,10 +1405,17 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
  28430. gfp_t alloc_gfp;
  28431. void *start, *p;
  28432. int idx, order;
  28433. + bool enableirqs = false;
  28434. flags &= gfp_allowed_mask;
  28435. if (gfpflags_allow_blocking(flags))
  28436. + enableirqs = true;
  28437. +#ifdef CONFIG_PREEMPT_RT_FULL
  28438. + if (system_state == SYSTEM_RUNNING)
  28439. + enableirqs = true;
  28440. +#endif
  28441. + if (enableirqs)
  28442. local_irq_enable();
  28443. flags |= s->allocflags;
  28444. @@ -1473,7 +1486,7 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
  28445. page->frozen = 1;
  28446. out:
  28447. - if (gfpflags_allow_blocking(flags))
  28448. + if (enableirqs)
  28449. local_irq_disable();
  28450. if (!page)
  28451. return NULL;
  28452. @@ -1529,6 +1542,16 @@ static void __free_slab(struct kmem_cache *s, struct page *page)
  28453. __free_kmem_pages(page, order);
  28454. }
  28455. +static void free_delayed(struct list_head *h)
  28456. +{
  28457. + while(!list_empty(h)) {
  28458. + struct page *page = list_first_entry(h, struct page, lru);
  28459. +
  28460. + list_del(&page->lru);
  28461. + __free_slab(page->slab_cache, page);
  28462. + }
  28463. +}
  28464. +
  28465. #define need_reserve_slab_rcu \
  28466. (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
  28467. @@ -1560,6 +1583,12 @@ static void free_slab(struct kmem_cache *s, struct page *page)
  28468. }
  28469. call_rcu(head, rcu_free_slab);
  28470. + } else if (irqs_disabled()) {
  28471. + struct slub_free_list *f = this_cpu_ptr(&slub_free_list);
  28472. +
  28473. + raw_spin_lock(&f->lock);
  28474. + list_add(&page->lru, &f->list);
  28475. + raw_spin_unlock(&f->lock);
  28476. } else
  28477. __free_slab(s, page);
  28478. }
  28479. @@ -1673,7 +1702,7 @@ static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
  28480. if (!n || !n->nr_partial)
  28481. return NULL;
  28482. - spin_lock(&n->list_lock);
  28483. + raw_spin_lock(&n->list_lock);
  28484. list_for_each_entry_safe(page, page2, &n->partial, lru) {
  28485. void *t;
  28486. @@ -1698,7 +1727,7 @@ static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
  28487. break;
  28488. }
  28489. - spin_unlock(&n->list_lock);
  28490. + raw_spin_unlock(&n->list_lock);
  28491. return object;
  28492. }
  28493. @@ -1944,7 +1973,7 @@ static void deactivate_slab(struct kmem_cache *s, struct page *page,
  28494. * that acquire_slab() will see a slab page that
  28495. * is frozen
  28496. */
  28497. - spin_lock(&n->list_lock);
  28498. + raw_spin_lock(&n->list_lock);
  28499. }
  28500. } else {
  28501. m = M_FULL;
  28502. @@ -1955,7 +1984,7 @@ static void deactivate_slab(struct kmem_cache *s, struct page *page,
  28503. * slabs from diagnostic functions will not see
  28504. * any frozen slabs.
  28505. */
  28506. - spin_lock(&n->list_lock);
  28507. + raw_spin_lock(&n->list_lock);
  28508. }
  28509. }
  28510. @@ -1990,7 +2019,7 @@ static void deactivate_slab(struct kmem_cache *s, struct page *page,
  28511. goto redo;
  28512. if (lock)
  28513. - spin_unlock(&n->list_lock);
  28514. + raw_spin_unlock(&n->list_lock);
  28515. if (m == M_FREE) {
  28516. stat(s, DEACTIVATE_EMPTY);
  28517. @@ -2022,10 +2051,10 @@ static void unfreeze_partials(struct kmem_cache *s,
  28518. n2 = get_node(s, page_to_nid(page));
  28519. if (n != n2) {
  28520. if (n)
  28521. - spin_unlock(&n->list_lock);
  28522. + raw_spin_unlock(&n->list_lock);
  28523. n = n2;
  28524. - spin_lock(&n->list_lock);
  28525. + raw_spin_lock(&n->list_lock);
  28526. }
  28527. do {
  28528. @@ -2054,7 +2083,7 @@ static void unfreeze_partials(struct kmem_cache *s,
  28529. }
  28530. if (n)
  28531. - spin_unlock(&n->list_lock);
  28532. + raw_spin_unlock(&n->list_lock);
  28533. while (discard_page) {
  28534. page = discard_page;
  28535. @@ -2093,14 +2122,21 @@ static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
  28536. pobjects = oldpage->pobjects;
  28537. pages = oldpage->pages;
  28538. if (drain && pobjects > s->cpu_partial) {
  28539. + struct slub_free_list *f;
  28540. unsigned long flags;
  28541. + LIST_HEAD(tofree);
  28542. /*
  28543. * partial array is full. Move the existing
  28544. * set to the per node partial list.
  28545. */
  28546. local_irq_save(flags);
  28547. unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
  28548. + f = this_cpu_ptr(&slub_free_list);
  28549. + raw_spin_lock(&f->lock);
  28550. + list_splice_init(&f->list, &tofree);
  28551. + raw_spin_unlock(&f->lock);
  28552. local_irq_restore(flags);
  28553. + free_delayed(&tofree);
  28554. oldpage = NULL;
  28555. pobjects = 0;
  28556. pages = 0;
  28557. @@ -2172,7 +2208,22 @@ static bool has_cpu_slab(int cpu, void *info)
  28558. static void flush_all(struct kmem_cache *s)
  28559. {
  28560. + LIST_HEAD(tofree);
  28561. + int cpu;
  28562. +
  28563. on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1, GFP_ATOMIC);
  28564. + for_each_online_cpu(cpu) {
  28565. + struct slub_free_list *f;
  28566. +
  28567. + if (!has_cpu_slab(cpu, s))
  28568. + continue;
  28569. +
  28570. + f = &per_cpu(slub_free_list, cpu);
  28571. + raw_spin_lock_irq(&f->lock);
  28572. + list_splice_init(&f->list, &tofree);
  28573. + raw_spin_unlock_irq(&f->lock);
  28574. + free_delayed(&tofree);
  28575. + }
  28576. }
  28577. /*
  28578. @@ -2208,10 +2259,10 @@ static unsigned long count_partial(struct kmem_cache_node *n,
  28579. unsigned long x = 0;
  28580. struct page *page;
  28581. - spin_lock_irqsave(&n->list_lock, flags);
  28582. + raw_spin_lock_irqsave(&n->list_lock, flags);
  28583. list_for_each_entry(page, &n->partial, lru)
  28584. x += get_count(page);
  28585. - spin_unlock_irqrestore(&n->list_lock, flags);
  28586. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  28587. return x;
  28588. }
  28589. #endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
  28590. @@ -2349,8 +2400,10 @@ static inline void *get_freelist(struct kmem_cache *s, struct page *page)
  28591. * already disabled (which is the case for bulk allocation).
  28592. */
  28593. static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  28594. - unsigned long addr, struct kmem_cache_cpu *c)
  28595. + unsigned long addr, struct kmem_cache_cpu *c,
  28596. + struct list_head *to_free)
  28597. {
  28598. + struct slub_free_list *f;
  28599. void *freelist;
  28600. struct page *page;
  28601. @@ -2410,6 +2463,13 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  28602. VM_BUG_ON(!c->page->frozen);
  28603. c->freelist = get_freepointer(s, freelist);
  28604. c->tid = next_tid(c->tid);
  28605. +
  28606. +out:
  28607. + f = this_cpu_ptr(&slub_free_list);
  28608. + raw_spin_lock(&f->lock);
  28609. + list_splice_init(&f->list, to_free);
  28610. + raw_spin_unlock(&f->lock);
  28611. +
  28612. return freelist;
  28613. new_slab:
  28614. @@ -2441,7 +2501,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  28615. deactivate_slab(s, page, get_freepointer(s, freelist));
  28616. c->page = NULL;
  28617. c->freelist = NULL;
  28618. - return freelist;
  28619. + goto out;
  28620. }
  28621. /*
  28622. @@ -2453,6 +2513,7 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  28623. {
  28624. void *p;
  28625. unsigned long flags;
  28626. + LIST_HEAD(tofree);
  28627. local_irq_save(flags);
  28628. #ifdef CONFIG_PREEMPT
  28629. @@ -2464,8 +2525,9 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  28630. c = this_cpu_ptr(s->cpu_slab);
  28631. #endif
  28632. - p = ___slab_alloc(s, gfpflags, node, addr, c);
  28633. + p = ___slab_alloc(s, gfpflags, node, addr, c, &tofree);
  28634. local_irq_restore(flags);
  28635. + free_delayed(&tofree);
  28636. return p;
  28637. }
  28638. @@ -2652,7 +2714,7 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
  28639. do {
  28640. if (unlikely(n)) {
  28641. - spin_unlock_irqrestore(&n->list_lock, flags);
  28642. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  28643. n = NULL;
  28644. }
  28645. prior = page->freelist;
  28646. @@ -2684,7 +2746,7 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
  28647. * Otherwise the list_lock will synchronize with
  28648. * other processors updating the list of slabs.
  28649. */
  28650. - spin_lock_irqsave(&n->list_lock, flags);
  28651. + raw_spin_lock_irqsave(&n->list_lock, flags);
  28652. }
  28653. }
  28654. @@ -2726,7 +2788,7 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
  28655. add_partial(n, page, DEACTIVATE_TO_TAIL);
  28656. stat(s, FREE_ADD_PARTIAL);
  28657. }
  28658. - spin_unlock_irqrestore(&n->list_lock, flags);
  28659. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  28660. return;
  28661. slab_empty:
  28662. @@ -2741,7 +2803,7 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
  28663. remove_full(s, n, page);
  28664. }
  28665. - spin_unlock_irqrestore(&n->list_lock, flags);
  28666. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  28667. stat(s, FREE_SLAB);
  28668. discard_slab(s, page);
  28669. }
  28670. @@ -2913,6 +2975,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
  28671. void **p)
  28672. {
  28673. struct kmem_cache_cpu *c;
  28674. + LIST_HEAD(to_free);
  28675. int i;
  28676. /* memcg and kmem_cache debug support */
  28677. @@ -2936,7 +2999,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
  28678. * of re-populating per CPU c->freelist
  28679. */
  28680. p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE,
  28681. - _RET_IP_, c);
  28682. + _RET_IP_, c, &to_free);
  28683. if (unlikely(!p[i]))
  28684. goto error;
  28685. @@ -2948,6 +3011,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
  28686. }
  28687. c->tid = next_tid(c->tid);
  28688. local_irq_enable();
  28689. + free_delayed(&to_free);
  28690. /* Clear memory outside IRQ disabled fastpath loop */
  28691. if (unlikely(flags & __GFP_ZERO)) {
  28692. @@ -3095,7 +3159,7 @@ static void
  28693. init_kmem_cache_node(struct kmem_cache_node *n)
  28694. {
  28695. n->nr_partial = 0;
  28696. - spin_lock_init(&n->list_lock);
  28697. + raw_spin_lock_init(&n->list_lock);
  28698. INIT_LIST_HEAD(&n->partial);
  28699. #ifdef CONFIG_SLUB_DEBUG
  28700. atomic_long_set(&n->nr_slabs, 0);
  28701. @@ -3677,7 +3741,7 @@ int __kmem_cache_shrink(struct kmem_cache *s, bool deactivate)
  28702. for (i = 0; i < SHRINK_PROMOTE_MAX; i++)
  28703. INIT_LIST_HEAD(promote + i);
  28704. - spin_lock_irqsave(&n->list_lock, flags);
  28705. + raw_spin_lock_irqsave(&n->list_lock, flags);
  28706. /*
  28707. * Build lists of slabs to discard or promote.
  28708. @@ -3708,7 +3772,7 @@ int __kmem_cache_shrink(struct kmem_cache *s, bool deactivate)
  28709. for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--)
  28710. list_splice(promote + i, &n->partial);
  28711. - spin_unlock_irqrestore(&n->list_lock, flags);
  28712. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  28713. /* Release empty slabs */
  28714. list_for_each_entry_safe(page, t, &discard, lru)
  28715. @@ -3884,6 +3948,12 @@ void __init kmem_cache_init(void)
  28716. {
  28717. static __initdata struct kmem_cache boot_kmem_cache,
  28718. boot_kmem_cache_node;
  28719. + int cpu;
  28720. +
  28721. + for_each_possible_cpu(cpu) {
  28722. + raw_spin_lock_init(&per_cpu(slub_free_list, cpu).lock);
  28723. + INIT_LIST_HEAD(&per_cpu(slub_free_list, cpu).list);
  28724. + }
  28725. if (debug_guardpage_minorder())
  28726. slub_max_order = 0;
  28727. @@ -4127,7 +4197,7 @@ static int validate_slab_node(struct kmem_cache *s,
  28728. struct page *page;
  28729. unsigned long flags;
  28730. - spin_lock_irqsave(&n->list_lock, flags);
  28731. + raw_spin_lock_irqsave(&n->list_lock, flags);
  28732. list_for_each_entry(page, &n->partial, lru) {
  28733. validate_slab_slab(s, page, map);
  28734. @@ -4149,7 +4219,7 @@ static int validate_slab_node(struct kmem_cache *s,
  28735. s->name, count, atomic_long_read(&n->nr_slabs));
  28736. out:
  28737. - spin_unlock_irqrestore(&n->list_lock, flags);
  28738. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  28739. return count;
  28740. }
  28741. @@ -4337,12 +4407,12 @@ static int list_locations(struct kmem_cache *s, char *buf,
  28742. if (!atomic_long_read(&n->nr_slabs))
  28743. continue;
  28744. - spin_lock_irqsave(&n->list_lock, flags);
  28745. + raw_spin_lock_irqsave(&n->list_lock, flags);
  28746. list_for_each_entry(page, &n->partial, lru)
  28747. process_slab(&t, s, page, alloc, map);
  28748. list_for_each_entry(page, &n->full, lru)
  28749. process_slab(&t, s, page, alloc, map);
  28750. - spin_unlock_irqrestore(&n->list_lock, flags);
  28751. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  28752. }
  28753. for (i = 0; i < t.count; i++) {
  28754. diff --git a/mm/swap.c b/mm/swap.c
  28755. index 39395fb549c0..ad16649221d7 100644
  28756. --- a/mm/swap.c
  28757. +++ b/mm/swap.c
  28758. @@ -31,6 +31,7 @@
  28759. #include <linux/memcontrol.h>
  28760. #include <linux/gfp.h>
  28761. #include <linux/uio.h>
  28762. +#include <linux/locallock.h>
  28763. #include <linux/hugetlb.h>
  28764. #include <linux/page_idle.h>
  28765. @@ -46,6 +47,9 @@ static DEFINE_PER_CPU(struct pagevec, lru_add_pvec);
  28766. static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
  28767. static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
  28768. +static DEFINE_LOCAL_IRQ_LOCK(rotate_lock);
  28769. +DEFINE_LOCAL_IRQ_LOCK(swapvec_lock);
  28770. +
  28771. /*
  28772. * This path almost never happens for VM activity - pages are normally
  28773. * freed via pagevecs. But it gets used by networking.
  28774. @@ -481,11 +485,11 @@ void rotate_reclaimable_page(struct page *page)
  28775. unsigned long flags;
  28776. page_cache_get(page);
  28777. - local_irq_save(flags);
  28778. + local_lock_irqsave(rotate_lock, flags);
  28779. pvec = this_cpu_ptr(&lru_rotate_pvecs);
  28780. if (!pagevec_add(pvec, page))
  28781. pagevec_move_tail(pvec);
  28782. - local_irq_restore(flags);
  28783. + local_unlock_irqrestore(rotate_lock, flags);
  28784. }
  28785. }
  28786. @@ -536,12 +540,13 @@ static bool need_activate_page_drain(int cpu)
  28787. void activate_page(struct page *page)
  28788. {
  28789. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  28790. - struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
  28791. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  28792. + activate_page_pvecs);
  28793. page_cache_get(page);
  28794. if (!pagevec_add(pvec, page))
  28795. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  28796. - put_cpu_var(activate_page_pvecs);
  28797. + put_locked_var(swapvec_lock, activate_page_pvecs);
  28798. }
  28799. }
  28800. @@ -567,7 +572,7 @@ void activate_page(struct page *page)
  28801. static void __lru_cache_activate_page(struct page *page)
  28802. {
  28803. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  28804. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  28805. int i;
  28806. /*
  28807. @@ -589,7 +594,7 @@ static void __lru_cache_activate_page(struct page *page)
  28808. }
  28809. }
  28810. - put_cpu_var(lru_add_pvec);
  28811. + put_locked_var(swapvec_lock, lru_add_pvec);
  28812. }
  28813. /*
  28814. @@ -630,13 +635,13 @@ EXPORT_SYMBOL(mark_page_accessed);
  28815. static void __lru_cache_add(struct page *page)
  28816. {
  28817. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  28818. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  28819. page_cache_get(page);
  28820. if (!pagevec_space(pvec))
  28821. __pagevec_lru_add(pvec);
  28822. pagevec_add(pvec, page);
  28823. - put_cpu_var(lru_add_pvec);
  28824. + put_locked_var(swapvec_lock, lru_add_pvec);
  28825. }
  28826. /**
  28827. @@ -816,9 +821,15 @@ void lru_add_drain_cpu(int cpu)
  28828. unsigned long flags;
  28829. /* No harm done if a racing interrupt already did this */
  28830. - local_irq_save(flags);
  28831. +#ifdef CONFIG_PREEMPT_RT_BASE
  28832. + local_lock_irqsave_on(rotate_lock, flags, cpu);
  28833. + pagevec_move_tail(pvec);
  28834. + local_unlock_irqrestore_on(rotate_lock, flags, cpu);
  28835. +#else
  28836. + local_lock_irqsave(rotate_lock, flags);
  28837. pagevec_move_tail(pvec);
  28838. - local_irq_restore(flags);
  28839. + local_unlock_irqrestore(rotate_lock, flags);
  28840. +#endif
  28841. }
  28842. pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
  28843. @@ -846,26 +857,47 @@ void deactivate_file_page(struct page *page)
  28844. return;
  28845. if (likely(get_page_unless_zero(page))) {
  28846. - struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
  28847. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  28848. + lru_deactivate_file_pvecs);
  28849. if (!pagevec_add(pvec, page))
  28850. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  28851. - put_cpu_var(lru_deactivate_file_pvecs);
  28852. + put_locked_var(swapvec_lock, lru_deactivate_file_pvecs);
  28853. }
  28854. }
  28855. void lru_add_drain(void)
  28856. {
  28857. - lru_add_drain_cpu(get_cpu());
  28858. - put_cpu();
  28859. + lru_add_drain_cpu(local_lock_cpu(swapvec_lock));
  28860. + local_unlock_cpu(swapvec_lock);
  28861. }
  28862. +
  28863. +#ifdef CONFIG_PREEMPT_RT_BASE
  28864. +static inline void remote_lru_add_drain(int cpu, struct cpumask *has_work)
  28865. +{
  28866. + local_lock_on(swapvec_lock, cpu);
  28867. + lru_add_drain_cpu(cpu);
  28868. + local_unlock_on(swapvec_lock, cpu);
  28869. +}
  28870. +
  28871. +#else
  28872. +
  28873. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  28874. {
  28875. lru_add_drain();
  28876. }
  28877. static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
  28878. +static inline void remote_lru_add_drain(int cpu, struct cpumask *has_work)
  28879. +{
  28880. + struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  28881. +
  28882. + INIT_WORK(work, lru_add_drain_per_cpu);
  28883. + schedule_work_on(cpu, work);
  28884. + cpumask_set_cpu(cpu, has_work);
  28885. +}
  28886. +#endif
  28887. void lru_add_drain_all(void)
  28888. {
  28889. @@ -878,20 +910,17 @@ void lru_add_drain_all(void)
  28890. cpumask_clear(&has_work);
  28891. for_each_online_cpu(cpu) {
  28892. - struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  28893. -
  28894. if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
  28895. pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
  28896. pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
  28897. - need_activate_page_drain(cpu)) {
  28898. - INIT_WORK(work, lru_add_drain_per_cpu);
  28899. - schedule_work_on(cpu, work);
  28900. - cpumask_set_cpu(cpu, &has_work);
  28901. - }
  28902. + need_activate_page_drain(cpu))
  28903. + remote_lru_add_drain(cpu, &has_work);
  28904. }
  28905. +#ifndef CONFIG_PREEMPT_RT_BASE
  28906. for_each_cpu(cpu, &has_work)
  28907. flush_work(&per_cpu(lru_add_drain_work, cpu));
  28908. +#endif
  28909. put_online_cpus();
  28910. mutex_unlock(&lock);
  28911. diff --git a/mm/truncate.c b/mm/truncate.c
  28912. index f4c8270f7b84..ff2d614eb91d 100644
  28913. --- a/mm/truncate.c
  28914. +++ b/mm/truncate.c
  28915. @@ -56,8 +56,11 @@ static void clear_exceptional_entry(struct address_space *mapping,
  28916. * protected by mapping->tree_lock.
  28917. */
  28918. if (!workingset_node_shadows(node) &&
  28919. - !list_empty(&node->private_list))
  28920. - list_lru_del(&workingset_shadow_nodes, &node->private_list);
  28921. + !list_empty(&node->private_list)) {
  28922. + local_lock(workingset_shadow_lock);
  28923. + list_lru_del(&__workingset_shadow_nodes, &node->private_list);
  28924. + local_unlock(workingset_shadow_lock);
  28925. + }
  28926. __radix_tree_delete_node(&mapping->page_tree, node);
  28927. unlock:
  28928. spin_unlock_irq(&mapping->tree_lock);
  28929. diff --git a/mm/vmalloc.c b/mm/vmalloc.c
  28930. index 8e3c9c5a3042..68740314ad54 100644
  28931. --- a/mm/vmalloc.c
  28932. +++ b/mm/vmalloc.c
  28933. @@ -821,7 +821,7 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
  28934. struct vmap_block *vb;
  28935. struct vmap_area *va;
  28936. unsigned long vb_idx;
  28937. - int node, err;
  28938. + int node, err, cpu;
  28939. void *vaddr;
  28940. node = numa_node_id();
  28941. @@ -864,11 +864,12 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
  28942. BUG_ON(err);
  28943. radix_tree_preload_end();
  28944. - vbq = &get_cpu_var(vmap_block_queue);
  28945. + cpu = get_cpu_light();
  28946. + vbq = this_cpu_ptr(&vmap_block_queue);
  28947. spin_lock(&vbq->lock);
  28948. list_add_tail_rcu(&vb->free_list, &vbq->free);
  28949. spin_unlock(&vbq->lock);
  28950. - put_cpu_var(vmap_block_queue);
  28951. + put_cpu_light();
  28952. return vaddr;
  28953. }
  28954. @@ -937,6 +938,7 @@ static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  28955. struct vmap_block *vb;
  28956. void *vaddr = NULL;
  28957. unsigned int order;
  28958. + int cpu;
  28959. BUG_ON(offset_in_page(size));
  28960. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  28961. @@ -951,7 +953,8 @@ static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  28962. order = get_order(size);
  28963. rcu_read_lock();
  28964. - vbq = &get_cpu_var(vmap_block_queue);
  28965. + cpu = get_cpu_light();
  28966. + vbq = this_cpu_ptr(&vmap_block_queue);
  28967. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  28968. unsigned long pages_off;
  28969. @@ -974,7 +977,7 @@ static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  28970. break;
  28971. }
  28972. - put_cpu_var(vmap_block_queue);
  28973. + put_cpu_light();
  28974. rcu_read_unlock();
  28975. /* Allocate new block if nothing was found */
  28976. diff --git a/mm/vmstat.c b/mm/vmstat.c
  28977. index 5712cdaae964..71e04bc3fe66 100644
  28978. --- a/mm/vmstat.c
  28979. +++ b/mm/vmstat.c
  28980. @@ -226,6 +226,7 @@ void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  28981. long x;
  28982. long t;
  28983. + preempt_disable_rt();
  28984. x = delta + __this_cpu_read(*p);
  28985. t = __this_cpu_read(pcp->stat_threshold);
  28986. @@ -235,6 +236,7 @@ void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  28987. x = 0;
  28988. }
  28989. __this_cpu_write(*p, x);
  28990. + preempt_enable_rt();
  28991. }
  28992. EXPORT_SYMBOL(__mod_zone_page_state);
  28993. @@ -267,6 +269,7 @@ void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
  28994. s8 __percpu *p = pcp->vm_stat_diff + item;
  28995. s8 v, t;
  28996. + preempt_disable_rt();
  28997. v = __this_cpu_inc_return(*p);
  28998. t = __this_cpu_read(pcp->stat_threshold);
  28999. if (unlikely(v > t)) {
  29000. @@ -275,6 +278,7 @@ void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
  29001. zone_page_state_add(v + overstep, zone, item);
  29002. __this_cpu_write(*p, -overstep);
  29003. }
  29004. + preempt_enable_rt();
  29005. }
  29006. void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
  29007. @@ -289,6 +293,7 @@ void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  29008. s8 __percpu *p = pcp->vm_stat_diff + item;
  29009. s8 v, t;
  29010. + preempt_disable_rt();
  29011. v = __this_cpu_dec_return(*p);
  29012. t = __this_cpu_read(pcp->stat_threshold);
  29013. if (unlikely(v < - t)) {
  29014. @@ -297,6 +302,7 @@ void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  29015. zone_page_state_add(v - overstep, zone, item);
  29016. __this_cpu_write(*p, overstep);
  29017. }
  29018. + preempt_enable_rt();
  29019. }
  29020. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  29021. diff --git a/mm/workingset.c b/mm/workingset.c
  29022. index df66f426fdcf..6db7b243fa0d 100644
  29023. --- a/mm/workingset.c
  29024. +++ b/mm/workingset.c
  29025. @@ -264,7 +264,8 @@ void workingset_activation(struct page *page)
  29026. * point where they would still be useful.
  29027. */
  29028. -struct list_lru workingset_shadow_nodes;
  29029. +struct list_lru __workingset_shadow_nodes;
  29030. +DEFINE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  29031. static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  29032. struct shrink_control *sc)
  29033. @@ -274,9 +275,9 @@ static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  29034. unsigned long pages;
  29035. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  29036. - local_irq_disable();
  29037. - shadow_nodes = list_lru_shrink_count(&workingset_shadow_nodes, sc);
  29038. - local_irq_enable();
  29039. + local_lock_irq(workingset_shadow_lock);
  29040. + shadow_nodes = list_lru_shrink_count(&__workingset_shadow_nodes, sc);
  29041. + local_unlock_irq(workingset_shadow_lock);
  29042. pages = node_present_pages(sc->nid);
  29043. /*
  29044. @@ -361,9 +362,9 @@ static enum lru_status shadow_lru_isolate(struct list_head *item,
  29045. spin_unlock(&mapping->tree_lock);
  29046. ret = LRU_REMOVED_RETRY;
  29047. out:
  29048. - local_irq_enable();
  29049. + local_unlock_irq(workingset_shadow_lock);
  29050. cond_resched();
  29051. - local_irq_disable();
  29052. + local_lock_irq(workingset_shadow_lock);
  29053. spin_lock(lru_lock);
  29054. return ret;
  29055. }
  29056. @@ -374,10 +375,10 @@ static unsigned long scan_shadow_nodes(struct shrinker *shrinker,
  29057. unsigned long ret;
  29058. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  29059. - local_irq_disable();
  29060. - ret = list_lru_shrink_walk(&workingset_shadow_nodes, sc,
  29061. + local_lock_irq(workingset_shadow_lock);
  29062. + ret = list_lru_shrink_walk(&__workingset_shadow_nodes, sc,
  29063. shadow_lru_isolate, NULL);
  29064. - local_irq_enable();
  29065. + local_unlock_irq(workingset_shadow_lock);
  29066. return ret;
  29067. }
  29068. @@ -398,7 +399,7 @@ static int __init workingset_init(void)
  29069. {
  29070. int ret;
  29071. - ret = list_lru_init_key(&workingset_shadow_nodes, &shadow_nodes_key);
  29072. + ret = list_lru_init_key(&__workingset_shadow_nodes, &shadow_nodes_key);
  29073. if (ret)
  29074. goto err;
  29075. ret = register_shrinker(&workingset_shadow_shrinker);
  29076. @@ -406,7 +407,7 @@ static int __init workingset_init(void)
  29077. goto err_list_lru;
  29078. return 0;
  29079. err_list_lru:
  29080. - list_lru_destroy(&workingset_shadow_nodes);
  29081. + list_lru_destroy(&__workingset_shadow_nodes);
  29082. err:
  29083. return ret;
  29084. }
  29085. diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
  29086. index c1ea19478119..529552c3716d 100644
  29087. --- a/mm/zsmalloc.c
  29088. +++ b/mm/zsmalloc.c
  29089. @@ -64,6 +64,7 @@
  29090. #include <linux/debugfs.h>
  29091. #include <linux/zsmalloc.h>
  29092. #include <linux/zpool.h>
  29093. +#include <linux/locallock.h>
  29094. /*
  29095. * This must be power of 2 and greater than of equal to sizeof(link_free).
  29096. @@ -403,6 +404,7 @@ static unsigned int get_maxobj_per_zspage(int size, int pages_per_zspage)
  29097. /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
  29098. static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
  29099. +static DEFINE_LOCAL_IRQ_LOCK(zs_map_area_lock);
  29100. static int is_first_page(struct page *page)
  29101. {
  29102. @@ -1289,7 +1291,7 @@ void *zs_map_object(struct zs_pool *pool, unsigned long handle,
  29103. class = pool->size_class[class_idx];
  29104. off = obj_idx_to_offset(page, obj_idx, class->size);
  29105. - area = &get_cpu_var(zs_map_area);
  29106. + area = &get_locked_var(zs_map_area_lock, zs_map_area);
  29107. area->vm_mm = mm;
  29108. if (off + class->size <= PAGE_SIZE) {
  29109. /* this object is contained entirely within a page */
  29110. @@ -1342,7 +1344,7 @@ void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
  29111. __zs_unmap_object(area, pages, off, class->size);
  29112. }
  29113. - put_cpu_var(zs_map_area);
  29114. + put_locked_var(zs_map_area_lock, zs_map_area);
  29115. unpin_tag(handle);
  29116. }
  29117. EXPORT_SYMBOL_GPL(zs_unmap_object);
  29118. diff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c
  29119. index c842f40c1173..035a5f6e3de9 100644
  29120. --- a/net/bluetooth/hci_sock.c
  29121. +++ b/net/bluetooth/hci_sock.c
  29122. @@ -213,15 +213,13 @@ void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
  29123. }
  29124. /* Send frame to sockets with specific channel */
  29125. -void hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
  29126. - int flag, struct sock *skip_sk)
  29127. +static void __hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
  29128. + int flag, struct sock *skip_sk)
  29129. {
  29130. struct sock *sk;
  29131. BT_DBG("channel %u len %d", channel, skb->len);
  29132. - read_lock(&hci_sk_list.lock);
  29133. -
  29134. sk_for_each(sk, &hci_sk_list.head) {
  29135. struct sk_buff *nskb;
  29136. @@ -247,6 +245,13 @@ void hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
  29137. kfree_skb(nskb);
  29138. }
  29139. +}
  29140. +
  29141. +void hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
  29142. + int flag, struct sock *skip_sk)
  29143. +{
  29144. + read_lock(&hci_sk_list.lock);
  29145. + __hci_send_to_channel(channel, skb, flag, skip_sk);
  29146. read_unlock(&hci_sk_list.lock);
  29147. }
  29148. @@ -299,8 +304,8 @@ void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb)
  29149. hdr->index = cpu_to_le16(hdev->id);
  29150. hdr->len = cpu_to_le16(skb->len);
  29151. - hci_send_to_channel(HCI_CHANNEL_MONITOR, skb_copy,
  29152. - HCI_SOCK_TRUSTED, NULL);
  29153. + __hci_send_to_channel(HCI_CHANNEL_MONITOR, skb_copy,
  29154. + HCI_SOCK_TRUSTED, NULL);
  29155. kfree_skb(skb_copy);
  29156. }
  29157. diff --git a/net/core/dev.c b/net/core/dev.c
  29158. index 3b67c1e5756f..63614e930907 100644
  29159. --- a/net/core/dev.c
  29160. +++ b/net/core/dev.c
  29161. @@ -186,6 +186,7 @@ static unsigned int napi_gen_id = NR_CPUS;
  29162. static DEFINE_HASHTABLE(napi_hash, 8);
  29163. static seqcount_t devnet_rename_seq;
  29164. +static DEFINE_MUTEX(devnet_rename_mutex);
  29165. static inline void dev_base_seq_inc(struct net *net)
  29166. {
  29167. @@ -207,14 +208,14 @@ static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
  29168. static inline void rps_lock(struct softnet_data *sd)
  29169. {
  29170. #ifdef CONFIG_RPS
  29171. - spin_lock(&sd->input_pkt_queue.lock);
  29172. + raw_spin_lock(&sd->input_pkt_queue.raw_lock);
  29173. #endif
  29174. }
  29175. static inline void rps_unlock(struct softnet_data *sd)
  29176. {
  29177. #ifdef CONFIG_RPS
  29178. - spin_unlock(&sd->input_pkt_queue.lock);
  29179. + raw_spin_unlock(&sd->input_pkt_queue.raw_lock);
  29180. #endif
  29181. }
  29182. @@ -884,7 +885,8 @@ int netdev_get_name(struct net *net, char *name, int ifindex)
  29183. strcpy(name, dev->name);
  29184. rcu_read_unlock();
  29185. if (read_seqcount_retry(&devnet_rename_seq, seq)) {
  29186. - cond_resched();
  29187. + mutex_lock(&devnet_rename_mutex);
  29188. + mutex_unlock(&devnet_rename_mutex);
  29189. goto retry;
  29190. }
  29191. @@ -1153,20 +1155,17 @@ int dev_change_name(struct net_device *dev, const char *newname)
  29192. if (dev->flags & IFF_UP)
  29193. return -EBUSY;
  29194. - write_seqcount_begin(&devnet_rename_seq);
  29195. + mutex_lock(&devnet_rename_mutex);
  29196. + __raw_write_seqcount_begin(&devnet_rename_seq);
  29197. - if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
  29198. - write_seqcount_end(&devnet_rename_seq);
  29199. - return 0;
  29200. - }
  29201. + if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
  29202. + goto outunlock;
  29203. memcpy(oldname, dev->name, IFNAMSIZ);
  29204. err = dev_get_valid_name(net, dev, newname);
  29205. - if (err < 0) {
  29206. - write_seqcount_end(&devnet_rename_seq);
  29207. - return err;
  29208. - }
  29209. + if (err < 0)
  29210. + goto outunlock;
  29211. if (oldname[0] && !strchr(oldname, '%'))
  29212. netdev_info(dev, "renamed from %s\n", oldname);
  29213. @@ -1179,11 +1178,12 @@ int dev_change_name(struct net_device *dev, const char *newname)
  29214. if (ret) {
  29215. memcpy(dev->name, oldname, IFNAMSIZ);
  29216. dev->name_assign_type = old_assign_type;
  29217. - write_seqcount_end(&devnet_rename_seq);
  29218. - return ret;
  29219. + err = ret;
  29220. + goto outunlock;
  29221. }
  29222. - write_seqcount_end(&devnet_rename_seq);
  29223. + __raw_write_seqcount_end(&devnet_rename_seq);
  29224. + mutex_unlock(&devnet_rename_mutex);
  29225. netdev_adjacent_rename_links(dev, oldname);
  29226. @@ -1204,7 +1204,8 @@ int dev_change_name(struct net_device *dev, const char *newname)
  29227. /* err >= 0 after dev_alloc_name() or stores the first errno */
  29228. if (err >= 0) {
  29229. err = ret;
  29230. - write_seqcount_begin(&devnet_rename_seq);
  29231. + mutex_lock(&devnet_rename_mutex);
  29232. + __raw_write_seqcount_begin(&devnet_rename_seq);
  29233. memcpy(dev->name, oldname, IFNAMSIZ);
  29234. memcpy(oldname, newname, IFNAMSIZ);
  29235. dev->name_assign_type = old_assign_type;
  29236. @@ -1217,6 +1218,11 @@ int dev_change_name(struct net_device *dev, const char *newname)
  29237. }
  29238. return err;
  29239. +
  29240. +outunlock:
  29241. + __raw_write_seqcount_end(&devnet_rename_seq);
  29242. + mutex_unlock(&devnet_rename_mutex);
  29243. + return err;
  29244. }
  29245. /**
  29246. @@ -2270,6 +2276,7 @@ static inline void __netif_reschedule(struct Qdisc *q)
  29247. sd->output_queue_tailp = &q->next_sched;
  29248. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  29249. local_irq_restore(flags);
  29250. + preempt_check_resched_rt();
  29251. }
  29252. void __netif_schedule(struct Qdisc *q)
  29253. @@ -2354,6 +2361,7 @@ void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
  29254. __this_cpu_write(softnet_data.completion_queue, skb);
  29255. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  29256. local_irq_restore(flags);
  29257. + preempt_check_resched_rt();
  29258. }
  29259. EXPORT_SYMBOL(__dev_kfree_skb_irq);
  29260. @@ -2918,7 +2926,11 @@ static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
  29261. * This permits __QDISC___STATE_RUNNING owner to get the lock more
  29262. * often and dequeue packets faster.
  29263. */
  29264. +#ifdef CONFIG_PREEMPT_RT_FULL
  29265. + contended = true;
  29266. +#else
  29267. contended = qdisc_is_running(q);
  29268. +#endif
  29269. if (unlikely(contended))
  29270. spin_lock(&q->busylock);
  29271. @@ -2978,9 +2990,44 @@ static void skb_update_prio(struct sk_buff *skb)
  29272. #define skb_update_prio(skb)
  29273. #endif
  29274. +#ifdef CONFIG_PREEMPT_RT_FULL
  29275. +
  29276. +static inline int xmit_rec_read(void)
  29277. +{
  29278. + return current->xmit_recursion;
  29279. +}
  29280. +
  29281. +static inline void xmit_rec_inc(void)
  29282. +{
  29283. + current->xmit_recursion++;
  29284. +}
  29285. +
  29286. +static inline void xmit_rec_dec(void)
  29287. +{
  29288. + current->xmit_recursion--;
  29289. +}
  29290. +
  29291. +#else
  29292. +
  29293. DEFINE_PER_CPU(int, xmit_recursion);
  29294. EXPORT_SYMBOL(xmit_recursion);
  29295. +static inline int xmit_rec_read(void)
  29296. +{
  29297. + return __this_cpu_read(xmit_recursion);
  29298. +}
  29299. +
  29300. +static inline void xmit_rec_inc(void)
  29301. +{
  29302. + __this_cpu_inc(xmit_recursion);
  29303. +}
  29304. +
  29305. +static inline void xmit_rec_dec(void)
  29306. +{
  29307. + __this_cpu_dec(xmit_recursion);
  29308. +}
  29309. +#endif
  29310. +
  29311. #define RECURSION_LIMIT 10
  29312. /**
  29313. @@ -3175,7 +3222,7 @@ static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)
  29314. if (txq->xmit_lock_owner != cpu) {
  29315. - if (__this_cpu_read(xmit_recursion) > RECURSION_LIMIT)
  29316. + if (xmit_rec_read() > RECURSION_LIMIT)
  29317. goto recursion_alert;
  29318. skb = validate_xmit_skb(skb, dev);
  29319. @@ -3185,9 +3232,9 @@ static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)
  29320. HARD_TX_LOCK(dev, txq, cpu);
  29321. if (!netif_xmit_stopped(txq)) {
  29322. - __this_cpu_inc(xmit_recursion);
  29323. + xmit_rec_inc();
  29324. skb = dev_hard_start_xmit(skb, dev, txq, &rc);
  29325. - __this_cpu_dec(xmit_recursion);
  29326. + xmit_rec_dec();
  29327. if (dev_xmit_complete(rc)) {
  29328. HARD_TX_UNLOCK(dev, txq);
  29329. goto out;
  29330. @@ -3561,6 +3608,7 @@ static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
  29331. rps_unlock(sd);
  29332. local_irq_restore(flags);
  29333. + preempt_check_resched_rt();
  29334. atomic_long_inc(&skb->dev->rx_dropped);
  29335. kfree_skb(skb);
  29336. @@ -3579,7 +3627,7 @@ static int netif_rx_internal(struct sk_buff *skb)
  29337. struct rps_dev_flow voidflow, *rflow = &voidflow;
  29338. int cpu;
  29339. - preempt_disable();
  29340. + migrate_disable();
  29341. rcu_read_lock();
  29342. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  29343. @@ -3589,13 +3637,13 @@ static int netif_rx_internal(struct sk_buff *skb)
  29344. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  29345. rcu_read_unlock();
  29346. - preempt_enable();
  29347. + migrate_enable();
  29348. } else
  29349. #endif
  29350. {
  29351. unsigned int qtail;
  29352. - ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
  29353. - put_cpu();
  29354. + ret = enqueue_to_backlog(skb, get_cpu_light(), &qtail);
  29355. + put_cpu_light();
  29356. }
  29357. return ret;
  29358. }
  29359. @@ -3629,16 +3677,44 @@ int netif_rx_ni(struct sk_buff *skb)
  29360. trace_netif_rx_ni_entry(skb);
  29361. - preempt_disable();
  29362. + local_bh_disable();
  29363. err = netif_rx_internal(skb);
  29364. - if (local_softirq_pending())
  29365. - do_softirq();
  29366. - preempt_enable();
  29367. + local_bh_enable();
  29368. return err;
  29369. }
  29370. EXPORT_SYMBOL(netif_rx_ni);
  29371. +#ifdef CONFIG_PREEMPT_RT_FULL
  29372. +/*
  29373. + * RT runs ksoftirqd as a real time thread and the root_lock is a
  29374. + * "sleeping spinlock". If the trylock fails then we can go into an
  29375. + * infinite loop when ksoftirqd preempted the task which actually
  29376. + * holds the lock, because we requeue q and raise NET_TX softirq
  29377. + * causing ksoftirqd to loop forever.
  29378. + *
  29379. + * It's safe to use spin_lock on RT here as softirqs run in thread
  29380. + * context and cannot deadlock against the thread which is holding
  29381. + * root_lock.
  29382. + *
  29383. + * On !RT the trylock might fail, but there we bail out from the
  29384. + * softirq loop after 10 attempts which we can't do on RT. And the
  29385. + * task holding root_lock cannot be preempted, so the only downside of
  29386. + * that trylock is that we need 10 loops to decide that we should have
  29387. + * given up in the first one :)
  29388. + */
  29389. +static inline int take_root_lock(spinlock_t *lock)
  29390. +{
  29391. + spin_lock(lock);
  29392. + return 1;
  29393. +}
  29394. +#else
  29395. +static inline int take_root_lock(spinlock_t *lock)
  29396. +{
  29397. + return spin_trylock(lock);
  29398. +}
  29399. +#endif
  29400. +
  29401. static void net_tx_action(struct softirq_action *h)
  29402. {
  29403. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  29404. @@ -3680,7 +3756,7 @@ static void net_tx_action(struct softirq_action *h)
  29405. head = head->next_sched;
  29406. root_lock = qdisc_lock(q);
  29407. - if (spin_trylock(root_lock)) {
  29408. + if (take_root_lock(root_lock)) {
  29409. smp_mb__before_atomic();
  29410. clear_bit(__QDISC_STATE_SCHED,
  29411. &q->state);
  29412. @@ -4102,7 +4178,7 @@ static void flush_backlog(void *arg)
  29413. skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
  29414. if (skb->dev == dev) {
  29415. __skb_unlink(skb, &sd->input_pkt_queue);
  29416. - kfree_skb(skb);
  29417. + __skb_queue_tail(&sd->tofree_queue, skb);
  29418. input_queue_head_incr(sd);
  29419. }
  29420. }
  29421. @@ -4111,10 +4187,13 @@ static void flush_backlog(void *arg)
  29422. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  29423. if (skb->dev == dev) {
  29424. __skb_unlink(skb, &sd->process_queue);
  29425. - kfree_skb(skb);
  29426. + __skb_queue_tail(&sd->tofree_queue, skb);
  29427. input_queue_head_incr(sd);
  29428. }
  29429. }
  29430. +
  29431. + if (!skb_queue_empty(&sd->tofree_queue))
  29432. + raise_softirq_irqoff(NET_RX_SOFTIRQ);
  29433. }
  29434. static int napi_gro_complete(struct sk_buff *skb)
  29435. @@ -4581,6 +4660,7 @@ static void net_rps_action_and_irq_enable(struct softnet_data *sd)
  29436. sd->rps_ipi_list = NULL;
  29437. local_irq_enable();
  29438. + preempt_check_resched_rt();
  29439. /* Send pending IPI's to kick RPS processing on remote cpus. */
  29440. while (remsd) {
  29441. @@ -4594,6 +4674,7 @@ static void net_rps_action_and_irq_enable(struct softnet_data *sd)
  29442. } else
  29443. #endif
  29444. local_irq_enable();
  29445. + preempt_check_resched_rt();
  29446. }
  29447. static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
  29448. @@ -4675,9 +4756,11 @@ void __napi_schedule(struct napi_struct *n)
  29449. local_irq_save(flags);
  29450. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  29451. local_irq_restore(flags);
  29452. + preempt_check_resched_rt();
  29453. }
  29454. EXPORT_SYMBOL(__napi_schedule);
  29455. +#ifndef CONFIG_PREEMPT_RT_FULL
  29456. /**
  29457. * __napi_schedule_irqoff - schedule for receive
  29458. * @n: entry to schedule
  29459. @@ -4689,6 +4772,7 @@ void __napi_schedule_irqoff(struct napi_struct *n)
  29460. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  29461. }
  29462. EXPORT_SYMBOL(__napi_schedule_irqoff);
  29463. +#endif
  29464. void __napi_complete(struct napi_struct *n)
  29465. {
  29466. @@ -4912,13 +4996,21 @@ static void net_rx_action(struct softirq_action *h)
  29467. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  29468. unsigned long time_limit = jiffies + 2;
  29469. int budget = netdev_budget;
  29470. + struct sk_buff_head tofree_q;
  29471. + struct sk_buff *skb;
  29472. LIST_HEAD(list);
  29473. LIST_HEAD(repoll);
  29474. + __skb_queue_head_init(&tofree_q);
  29475. +
  29476. local_irq_disable();
  29477. + skb_queue_splice_init(&sd->tofree_queue, &tofree_q);
  29478. list_splice_init(&sd->poll_list, &list);
  29479. local_irq_enable();
  29480. + while ((skb = __skb_dequeue(&tofree_q)))
  29481. + kfree_skb(skb);
  29482. +
  29483. for (;;) {
  29484. struct napi_struct *n;
  29485. @@ -4948,7 +5040,7 @@ static void net_rx_action(struct softirq_action *h)
  29486. list_splice_tail(&repoll, &list);
  29487. list_splice(&list, &sd->poll_list);
  29488. if (!list_empty(&sd->poll_list))
  29489. - __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  29490. + __raise_softirq_irqoff_ksoft(NET_RX_SOFTIRQ);
  29491. net_rps_action_and_irq_enable(sd);
  29492. }
  29493. @@ -7287,7 +7379,7 @@ EXPORT_SYMBOL(free_netdev);
  29494. void synchronize_net(void)
  29495. {
  29496. might_sleep();
  29497. - if (rtnl_is_locked())
  29498. + if (rtnl_is_locked() && !IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  29499. synchronize_rcu_expedited();
  29500. else
  29501. synchronize_rcu();
  29502. @@ -7528,16 +7620,20 @@ static int dev_cpu_callback(struct notifier_block *nfb,
  29503. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  29504. local_irq_enable();
  29505. + preempt_check_resched_rt();
  29506. /* Process offline CPU's input_pkt_queue */
  29507. while ((skb = __skb_dequeue(&oldsd->process_queue))) {
  29508. netif_rx_ni(skb);
  29509. input_queue_head_incr(oldsd);
  29510. }
  29511. - while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
  29512. + while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
  29513. netif_rx_ni(skb);
  29514. input_queue_head_incr(oldsd);
  29515. }
  29516. + while ((skb = __skb_dequeue(&oldsd->tofree_queue))) {
  29517. + kfree_skb(skb);
  29518. + }
  29519. return NOTIFY_OK;
  29520. }
  29521. @@ -7839,8 +7935,9 @@ static int __init net_dev_init(void)
  29522. for_each_possible_cpu(i) {
  29523. struct softnet_data *sd = &per_cpu(softnet_data, i);
  29524. - skb_queue_head_init(&sd->input_pkt_queue);
  29525. - skb_queue_head_init(&sd->process_queue);
  29526. + skb_queue_head_init_raw(&sd->input_pkt_queue);
  29527. + skb_queue_head_init_raw(&sd->process_queue);
  29528. + skb_queue_head_init_raw(&sd->tofree_queue);
  29529. INIT_LIST_HEAD(&sd->poll_list);
  29530. sd->output_queue_tailp = &sd->output_queue;
  29531. #ifdef CONFIG_RPS
  29532. diff --git a/net/core/skbuff.c b/net/core/skbuff.c
  29533. index 86b619501350..f5c4897b52a0 100644
  29534. --- a/net/core/skbuff.c
  29535. +++ b/net/core/skbuff.c
  29536. @@ -63,6 +63,7 @@
  29537. #include <linux/errqueue.h>
  29538. #include <linux/prefetch.h>
  29539. #include <linux/if_vlan.h>
  29540. +#include <linux/locallock.h>
  29541. #include <net/protocol.h>
  29542. #include <net/dst.h>
  29543. @@ -351,6 +352,8 @@ EXPORT_SYMBOL(build_skb);
  29544. static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
  29545. static DEFINE_PER_CPU(struct page_frag_cache, napi_alloc_cache);
  29546. +static DEFINE_LOCAL_IRQ_LOCK(netdev_alloc_lock);
  29547. +static DEFINE_LOCAL_IRQ_LOCK(napi_alloc_cache_lock);
  29548. static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  29549. {
  29550. @@ -358,10 +361,10 @@ static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  29551. unsigned long flags;
  29552. void *data;
  29553. - local_irq_save(flags);
  29554. + local_lock_irqsave(netdev_alloc_lock, flags);
  29555. nc = this_cpu_ptr(&netdev_alloc_cache);
  29556. data = __alloc_page_frag(nc, fragsz, gfp_mask);
  29557. - local_irq_restore(flags);
  29558. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  29559. return data;
  29560. }
  29561. @@ -380,9 +383,13 @@ EXPORT_SYMBOL(netdev_alloc_frag);
  29562. static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  29563. {
  29564. - struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  29565. + struct page_frag_cache *nc;
  29566. + void *data;
  29567. - return __alloc_page_frag(nc, fragsz, gfp_mask);
  29568. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  29569. + data = __alloc_page_frag(nc, fragsz, gfp_mask);
  29570. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  29571. + return data;
  29572. }
  29573. void *napi_alloc_frag(unsigned int fragsz)
  29574. @@ -429,13 +436,13 @@ struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
  29575. if (sk_memalloc_socks())
  29576. gfp_mask |= __GFP_MEMALLOC;
  29577. - local_irq_save(flags);
  29578. + local_lock_irqsave(netdev_alloc_lock, flags);
  29579. nc = this_cpu_ptr(&netdev_alloc_cache);
  29580. data = __alloc_page_frag(nc, len, gfp_mask);
  29581. pfmemalloc = nc->pfmemalloc;
  29582. - local_irq_restore(flags);
  29583. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  29584. if (unlikely(!data))
  29585. return NULL;
  29586. @@ -476,9 +483,10 @@ EXPORT_SYMBOL(__netdev_alloc_skb);
  29587. struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
  29588. gfp_t gfp_mask)
  29589. {
  29590. - struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  29591. + struct page_frag_cache *nc;
  29592. struct sk_buff *skb;
  29593. void *data;
  29594. + bool pfmemalloc;
  29595. len += NET_SKB_PAD + NET_IP_ALIGN;
  29596. @@ -496,7 +504,11 @@ struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
  29597. if (sk_memalloc_socks())
  29598. gfp_mask |= __GFP_MEMALLOC;
  29599. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  29600. data = __alloc_page_frag(nc, len, gfp_mask);
  29601. + pfmemalloc = nc->pfmemalloc;
  29602. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  29603. +
  29604. if (unlikely(!data))
  29605. return NULL;
  29606. @@ -507,7 +519,7 @@ struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
  29607. }
  29608. /* use OR instead of assignment to avoid clearing of bits in mask */
  29609. - if (nc->pfmemalloc)
  29610. + if (pfmemalloc)
  29611. skb->pfmemalloc = 1;
  29612. skb->head_frag = 1;
  29613. diff --git a/net/core/sock.c b/net/core/sock.c
  29614. index cd12cb6fe366..982a06dab369 100644
  29615. --- a/net/core/sock.c
  29616. +++ b/net/core/sock.c
  29617. @@ -2449,12 +2449,11 @@ void lock_sock_nested(struct sock *sk, int subclass)
  29618. if (sk->sk_lock.owned)
  29619. __lock_sock(sk);
  29620. sk->sk_lock.owned = 1;
  29621. - spin_unlock(&sk->sk_lock.slock);
  29622. + spin_unlock_bh(&sk->sk_lock.slock);
  29623. /*
  29624. * The sk_lock has mutex_lock() semantics here:
  29625. */
  29626. mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
  29627. - local_bh_enable();
  29628. }
  29629. EXPORT_SYMBOL(lock_sock_nested);
  29630. diff --git a/net/ipv4/icmp.c b/net/ipv4/icmp.c
  29631. index 36e26977c908..2c1ce3e80ee4 100644
  29632. --- a/net/ipv4/icmp.c
  29633. +++ b/net/ipv4/icmp.c
  29634. @@ -69,6 +69,7 @@
  29635. #include <linux/jiffies.h>
  29636. #include <linux/kernel.h>
  29637. #include <linux/fcntl.h>
  29638. +#include <linux/sysrq.h>
  29639. #include <linux/socket.h>
  29640. #include <linux/in.h>
  29641. #include <linux/inet.h>
  29642. @@ -77,6 +78,7 @@
  29643. #include <linux/string.h>
  29644. #include <linux/netfilter_ipv4.h>
  29645. #include <linux/slab.h>
  29646. +#include <linux/locallock.h>
  29647. #include <net/snmp.h>
  29648. #include <net/ip.h>
  29649. #include <net/route.h>
  29650. @@ -204,6 +206,8 @@ static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
  29651. *
  29652. * On SMP we have one ICMP socket per-cpu.
  29653. */
  29654. +static DEFINE_LOCAL_IRQ_LOCK(icmp_sk_lock);
  29655. +
  29656. static struct sock *icmp_sk(struct net *net)
  29657. {
  29658. return *this_cpu_ptr(net->ipv4.icmp_sk);
  29659. @@ -215,12 +219,18 @@ static inline struct sock *icmp_xmit_lock(struct net *net)
  29660. local_bh_disable();
  29661. + if (!local_trylock(icmp_sk_lock)) {
  29662. + local_bh_enable();
  29663. + return NULL;
  29664. + }
  29665. +
  29666. sk = icmp_sk(net);
  29667. if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
  29668. /* This can happen if the output path signals a
  29669. * dst_link_failure() for an outgoing ICMP packet.
  29670. */
  29671. + local_unlock(icmp_sk_lock);
  29672. local_bh_enable();
  29673. return NULL;
  29674. }
  29675. @@ -230,6 +240,7 @@ static inline struct sock *icmp_xmit_lock(struct net *net)
  29676. static inline void icmp_xmit_unlock(struct sock *sk)
  29677. {
  29678. spin_unlock_bh(&sk->sk_lock.slock);
  29679. + local_unlock(icmp_sk_lock);
  29680. }
  29681. int sysctl_icmp_msgs_per_sec __read_mostly = 1000;
  29682. @@ -358,6 +369,7 @@ static void icmp_push_reply(struct icmp_bxm *icmp_param,
  29683. struct sock *sk;
  29684. struct sk_buff *skb;
  29685. + local_lock(icmp_sk_lock);
  29686. sk = icmp_sk(dev_net((*rt)->dst.dev));
  29687. if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
  29688. icmp_param->data_len+icmp_param->head_len,
  29689. @@ -380,6 +392,7 @@ static void icmp_push_reply(struct icmp_bxm *icmp_param,
  29690. skb->ip_summed = CHECKSUM_NONE;
  29691. ip_push_pending_frames(sk, fl4);
  29692. }
  29693. + local_unlock(icmp_sk_lock);
  29694. }
  29695. /*
  29696. @@ -890,6 +903,30 @@ static bool icmp_redirect(struct sk_buff *skb)
  29697. return true;
  29698. }
  29699. +/*
  29700. + * 32bit and 64bit have different timestamp length, so we check for
  29701. + * the cookie at offset 20 and verify it is repeated at offset 50
  29702. + */
  29703. +#define CO_POS0 20
  29704. +#define CO_POS1 50
  29705. +#define CO_SIZE sizeof(int)
  29706. +#define ICMP_SYSRQ_SIZE 57
  29707. +
  29708. +/*
  29709. + * We got a ICMP_SYSRQ_SIZE sized ping request. Check for the cookie
  29710. + * pattern and if it matches send the next byte as a trigger to sysrq.
  29711. + */
  29712. +static void icmp_check_sysrq(struct net *net, struct sk_buff *skb)
  29713. +{
  29714. + int cookie = htonl(net->ipv4.sysctl_icmp_echo_sysrq);
  29715. + char *p = skb->data;
  29716. +
  29717. + if (!memcmp(&cookie, p + CO_POS0, CO_SIZE) &&
  29718. + !memcmp(&cookie, p + CO_POS1, CO_SIZE) &&
  29719. + p[CO_POS0 + CO_SIZE] == p[CO_POS1 + CO_SIZE])
  29720. + handle_sysrq(p[CO_POS0 + CO_SIZE]);
  29721. +}
  29722. +
  29723. /*
  29724. * Handle ICMP_ECHO ("ping") requests.
  29725. *
  29726. @@ -917,6 +954,11 @@ static bool icmp_echo(struct sk_buff *skb)
  29727. icmp_param.data_len = skb->len;
  29728. icmp_param.head_len = sizeof(struct icmphdr);
  29729. icmp_reply(&icmp_param, skb);
  29730. +
  29731. + if (skb->len == ICMP_SYSRQ_SIZE &&
  29732. + net->ipv4.sysctl_icmp_echo_sysrq) {
  29733. + icmp_check_sysrq(net, skb);
  29734. + }
  29735. }
  29736. /* should there be an ICMP stat for ignored echos? */
  29737. return true;
  29738. diff --git a/net/ipv4/sysctl_net_ipv4.c b/net/ipv4/sysctl_net_ipv4.c
  29739. index 70fb352e317f..1bcd436709a4 100644
  29740. --- a/net/ipv4/sysctl_net_ipv4.c
  29741. +++ b/net/ipv4/sysctl_net_ipv4.c
  29742. @@ -817,6 +817,13 @@ static struct ctl_table ipv4_net_table[] = {
  29743. .mode = 0644,
  29744. .proc_handler = proc_dointvec
  29745. },
  29746. + {
  29747. + .procname = "icmp_echo_sysrq",
  29748. + .data = &init_net.ipv4.sysctl_icmp_echo_sysrq,
  29749. + .maxlen = sizeof(int),
  29750. + .mode = 0644,
  29751. + .proc_handler = proc_dointvec
  29752. + },
  29753. {
  29754. .procname = "icmp_ignore_bogus_error_responses",
  29755. .data = &init_net.ipv4.sysctl_icmp_ignore_bogus_error_responses,
  29756. diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
  29757. index 61c93a93f228..4df48ae72abc 100644
  29758. --- a/net/ipv4/tcp_ipv4.c
  29759. +++ b/net/ipv4/tcp_ipv4.c
  29760. @@ -62,6 +62,7 @@
  29761. #include <linux/init.h>
  29762. #include <linux/times.h>
  29763. #include <linux/slab.h>
  29764. +#include <linux/locallock.h>
  29765. #include <net/net_namespace.h>
  29766. #include <net/icmp.h>
  29767. @@ -570,6 +571,7 @@ void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb)
  29768. }
  29769. EXPORT_SYMBOL(tcp_v4_send_check);
  29770. +static DEFINE_LOCAL_IRQ_LOCK(tcp_sk_lock);
  29771. /*
  29772. * This routine will send an RST to the other tcp.
  29773. *
  29774. @@ -691,10 +693,13 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)
  29775. arg.bound_dev_if = sk->sk_bound_dev_if;
  29776. arg.tos = ip_hdr(skb)->tos;
  29777. +
  29778. + local_lock(tcp_sk_lock);
  29779. ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
  29780. skb, &TCP_SKB_CB(skb)->header.h4.opt,
  29781. ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
  29782. &arg, arg.iov[0].iov_len);
  29783. + local_unlock(tcp_sk_lock);
  29784. TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
  29785. TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS);
  29786. @@ -776,10 +781,12 @@ static void tcp_v4_send_ack(struct net *net,
  29787. if (oif)
  29788. arg.bound_dev_if = oif;
  29789. arg.tos = tos;
  29790. + local_lock(tcp_sk_lock);
  29791. ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
  29792. skb, &TCP_SKB_CB(skb)->header.h4.opt,
  29793. ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
  29794. &arg, arg.iov[0].iov_len);
  29795. + local_unlock(tcp_sk_lock);
  29796. TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
  29797. }
  29798. diff --git a/net/mac80211/rx.c b/net/mac80211/rx.c
  29799. index 3bcabc2ba4a6..c3c798388ab8 100644
  29800. --- a/net/mac80211/rx.c
  29801. +++ b/net/mac80211/rx.c
  29802. @@ -3605,7 +3605,7 @@ void ieee80211_rx_napi(struct ieee80211_hw *hw, struct sk_buff *skb,
  29803. struct ieee80211_supported_band *sband;
  29804. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  29805. - WARN_ON_ONCE(softirq_count() == 0);
  29806. + WARN_ON_ONCE_NONRT(softirq_count() == 0);
  29807. if (WARN_ON(status->band >= IEEE80211_NUM_BANDS))
  29808. goto drop;
  29809. diff --git a/net/netfilter/core.c b/net/netfilter/core.c
  29810. index f39276d1c2d7..10880c89d62f 100644
  29811. --- a/net/netfilter/core.c
  29812. +++ b/net/netfilter/core.c
  29813. @@ -22,11 +22,17 @@
  29814. #include <linux/proc_fs.h>
  29815. #include <linux/mutex.h>
  29816. #include <linux/slab.h>
  29817. +#include <linux/locallock.h>
  29818. #include <net/net_namespace.h>
  29819. #include <net/sock.h>
  29820. #include "nf_internals.h"
  29821. +#ifdef CONFIG_PREEMPT_RT_BASE
  29822. +DEFINE_LOCAL_IRQ_LOCK(xt_write_lock);
  29823. +EXPORT_PER_CPU_SYMBOL(xt_write_lock);
  29824. +#endif
  29825. +
  29826. static DEFINE_MUTEX(afinfo_mutex);
  29827. const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
  29828. diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
  29829. index 92ca3e106c2b..ddb4970c3e27 100644
  29830. --- a/net/packet/af_packet.c
  29831. +++ b/net/packet/af_packet.c
  29832. @@ -63,6 +63,7 @@
  29833. #include <linux/if_packet.h>
  29834. #include <linux/wireless.h>
  29835. #include <linux/kernel.h>
  29836. +#include <linux/delay.h>
  29837. #include <linux/kmod.h>
  29838. #include <linux/slab.h>
  29839. #include <linux/vmalloc.h>
  29840. @@ -694,7 +695,7 @@ static void prb_retire_rx_blk_timer_expired(unsigned long data)
  29841. if (BLOCK_NUM_PKTS(pbd)) {
  29842. while (atomic_read(&pkc->blk_fill_in_prog)) {
  29843. /* Waiting for skb_copy_bits to finish... */
  29844. - cpu_relax();
  29845. + cpu_chill();
  29846. }
  29847. }
  29848. @@ -956,7 +957,7 @@ static void prb_retire_current_block(struct tpacket_kbdq_core *pkc,
  29849. if (!(status & TP_STATUS_BLK_TMO)) {
  29850. while (atomic_read(&pkc->blk_fill_in_prog)) {
  29851. /* Waiting for skb_copy_bits to finish... */
  29852. - cpu_relax();
  29853. + cpu_chill();
  29854. }
  29855. }
  29856. prb_close_block(pkc, pbd, po, status);
  29857. diff --git a/net/rds/ib_rdma.c b/net/rds/ib_rdma.c
  29858. index a2340748ec86..19123a97b354 100644
  29859. --- a/net/rds/ib_rdma.c
  29860. +++ b/net/rds/ib_rdma.c
  29861. @@ -34,6 +34,7 @@
  29862. #include <linux/slab.h>
  29863. #include <linux/rculist.h>
  29864. #include <linux/llist.h>
  29865. +#include <linux/delay.h>
  29866. #include "rds.h"
  29867. #include "ib.h"
  29868. @@ -313,7 +314,7 @@ static inline void wait_clean_list_grace(void)
  29869. for_each_online_cpu(cpu) {
  29870. flag = &per_cpu(clean_list_grace, cpu);
  29871. while (test_bit(CLEAN_LIST_BUSY_BIT, flag))
  29872. - cpu_relax();
  29873. + cpu_chill();
  29874. }
  29875. }
  29876. diff --git a/net/sched/sch_generic.c b/net/sched/sch_generic.c
  29877. index aa4725038f94..00b81cab28f3 100644
  29878. --- a/net/sched/sch_generic.c
  29879. +++ b/net/sched/sch_generic.c
  29880. @@ -893,7 +893,7 @@ void dev_deactivate_many(struct list_head *head)
  29881. /* Wait for outstanding qdisc_run calls. */
  29882. list_for_each_entry(dev, head, close_list)
  29883. while (some_qdisc_is_busy(dev))
  29884. - yield();
  29885. + msleep(1);
  29886. }
  29887. void dev_deactivate(struct net_device *dev)
  29888. diff --git a/net/sunrpc/svc_xprt.c b/net/sunrpc/svc_xprt.c
  29889. index a6cbb2104667..5b69bb580617 100644
  29890. --- a/net/sunrpc/svc_xprt.c
  29891. +++ b/net/sunrpc/svc_xprt.c
  29892. @@ -340,7 +340,7 @@ void svc_xprt_do_enqueue(struct svc_xprt *xprt)
  29893. goto out;
  29894. }
  29895. - cpu = get_cpu();
  29896. + cpu = get_cpu_light();
  29897. pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
  29898. atomic_long_inc(&pool->sp_stats.packets);
  29899. @@ -376,7 +376,7 @@ void svc_xprt_do_enqueue(struct svc_xprt *xprt)
  29900. atomic_long_inc(&pool->sp_stats.threads_woken);
  29901. wake_up_process(rqstp->rq_task);
  29902. - put_cpu();
  29903. + put_cpu_light();
  29904. goto out;
  29905. }
  29906. rcu_read_unlock();
  29907. @@ -397,7 +397,7 @@ void svc_xprt_do_enqueue(struct svc_xprt *xprt)
  29908. goto redo_search;
  29909. }
  29910. rqstp = NULL;
  29911. - put_cpu();
  29912. + put_cpu_light();
  29913. out:
  29914. trace_svc_xprt_do_enqueue(xprt, rqstp);
  29915. }
  29916. diff --git a/scripts/mkcompile_h b/scripts/mkcompile_h
  29917. index 6fdc97ef6023..523e0420d7f0 100755
  29918. --- a/scripts/mkcompile_h
  29919. +++ b/scripts/mkcompile_h
  29920. @@ -4,7 +4,8 @@ TARGET=$1
  29921. ARCH=$2
  29922. SMP=$3
  29923. PREEMPT=$4
  29924. -CC=$5
  29925. +RT=$5
  29926. +CC=$6
  29927. vecho() { [ "${quiet}" = "silent_" ] || echo "$@" ; }
  29928. @@ -57,6 +58,7 @@ UTS_VERSION="#$VERSION"
  29929. CONFIG_FLAGS=""
  29930. if [ -n "$SMP" ] ; then CONFIG_FLAGS="SMP"; fi
  29931. if [ -n "$PREEMPT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS PREEMPT"; fi
  29932. +if [ -n "$RT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS RT"; fi
  29933. UTS_VERSION="$UTS_VERSION $CONFIG_FLAGS $TIMESTAMP"
  29934. # Truncate to maximum length
  29935. diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
  29936. index 4ba64fd49759..34e50186885d 100644
  29937. --- a/sound/core/pcm_native.c
  29938. +++ b/sound/core/pcm_native.c
  29939. @@ -135,7 +135,7 @@ EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
  29940. void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
  29941. {
  29942. if (!substream->pcm->nonatomic)
  29943. - local_irq_disable();
  29944. + local_irq_disable_nort();
  29945. snd_pcm_stream_lock(substream);
  29946. }
  29947. EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
  29948. @@ -150,7 +150,7 @@ void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
  29949. {
  29950. snd_pcm_stream_unlock(substream);
  29951. if (!substream->pcm->nonatomic)
  29952. - local_irq_enable();
  29953. + local_irq_enable_nort();
  29954. }
  29955. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
  29956. @@ -158,7 +158,7 @@ unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
  29957. {
  29958. unsigned long flags = 0;
  29959. if (!substream->pcm->nonatomic)
  29960. - local_irq_save(flags);
  29961. + local_irq_save_nort(flags);
  29962. snd_pcm_stream_lock(substream);
  29963. return flags;
  29964. }
  29965. @@ -176,7 +176,7 @@ void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
  29966. {
  29967. snd_pcm_stream_unlock(substream);
  29968. if (!substream->pcm->nonatomic)
  29969. - local_irq_restore(flags);
  29970. + local_irq_restore_nort(flags);
  29971. }
  29972. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
  29973. diff --git a/virt/kvm/async_pf.c b/virt/kvm/async_pf.c
  29974. index 4f70d12e392d..9378d0919ed8 100644
  29975. --- a/virt/kvm/async_pf.c
  29976. +++ b/virt/kvm/async_pf.c
  29977. @@ -98,8 +98,8 @@ static void async_pf_execute(struct work_struct *work)
  29978. * This memory barrier pairs with prepare_to_wait's set_current_state()
  29979. */
  29980. smp_mb();
  29981. - if (waitqueue_active(&vcpu->wq))
  29982. - wake_up_interruptible(&vcpu->wq);
  29983. + if (swait_active(&vcpu->wq))
  29984. + swake_up(&vcpu->wq);
  29985. mmput(mm);
  29986. kvm_put_kvm(vcpu->kvm);
  29987. diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
  29988. index d080f06fd8d9..a8d8150fd398 100644
  29989. --- a/virt/kvm/kvm_main.c
  29990. +++ b/virt/kvm/kvm_main.c
  29991. @@ -228,8 +228,7 @@ int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
  29992. vcpu->kvm = kvm;
  29993. vcpu->vcpu_id = id;
  29994. vcpu->pid = NULL;
  29995. - vcpu->halt_poll_ns = 0;
  29996. - init_waitqueue_head(&vcpu->wq);
  29997. + init_swait_queue_head(&vcpu->wq);
  29998. kvm_async_pf_vcpu_init(vcpu);
  29999. vcpu->pre_pcpu = -1;
  30000. @@ -2008,7 +2007,7 @@ static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu)
  30001. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  30002. {
  30003. ktime_t start, cur;
  30004. - DEFINE_WAIT(wait);
  30005. + DECLARE_SWAITQUEUE(wait);
  30006. bool waited = false;
  30007. u64 block_ns;
  30008. @@ -2033,7 +2032,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  30009. kvm_arch_vcpu_blocking(vcpu);
  30010. for (;;) {
  30011. - prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  30012. + prepare_to_swait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  30013. if (kvm_vcpu_check_block(vcpu) < 0)
  30014. break;
  30015. @@ -2042,7 +2041,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  30016. schedule();
  30017. }
  30018. - finish_wait(&vcpu->wq, &wait);
  30019. + finish_swait(&vcpu->wq, &wait);
  30020. cur = ktime_get();
  30021. kvm_arch_vcpu_unblocking(vcpu);
  30022. @@ -2074,11 +2073,11 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  30023. {
  30024. int me;
  30025. int cpu = vcpu->cpu;
  30026. - wait_queue_head_t *wqp;
  30027. + struct swait_queue_head *wqp;
  30028. wqp = kvm_arch_vcpu_wq(vcpu);
  30029. - if (waitqueue_active(wqp)) {
  30030. - wake_up_interruptible(wqp);
  30031. + if (swait_active(wqp)) {
  30032. + swake_up(wqp);
  30033. ++vcpu->stat.halt_wakeup;
  30034. }
  30035. @@ -2179,7 +2178,7 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
  30036. continue;
  30037. if (vcpu == me)
  30038. continue;
  30039. - if (waitqueue_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  30040. + if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  30041. continue;
  30042. if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
  30043. continue;