1
0

realtime.patch 793 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435
  1. diff -Nur linux-4.1.13.orig/arch/alpha/mm/fault.c linux-4.1.13/arch/alpha/mm/fault.c
  2. --- linux-4.1.13.orig/arch/alpha/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  3. +++ linux-4.1.13/arch/alpha/mm/fault.c 2015-11-29 09:23:09.477622951 +0100
  4. @@ -23,8 +23,7 @@
  5. #include <linux/smp.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/module.h>
  8. -
  9. -#include <asm/uaccess.h>
  10. +#include <linux/uaccess.h>
  11. extern void die_if_kernel(char *,struct pt_regs *,long, unsigned long *);
  12. @@ -107,7 +106,7 @@
  13. /* If we're in an interrupt context, or have no user context,
  14. we must not take the fault. */
  15. - if (!mm || in_atomic())
  16. + if (!mm || faulthandler_disabled())
  17. goto no_context;
  18. #ifdef CONFIG_ALPHA_LARGE_VMALLOC
  19. diff -Nur linux-4.1.13.orig/arch/arc/include/asm/futex.h linux-4.1.13/arch/arc/include/asm/futex.h
  20. --- linux-4.1.13.orig/arch/arc/include/asm/futex.h 2015-11-09 23:34:10.000000000 +0100
  21. +++ linux-4.1.13/arch/arc/include/asm/futex.h 2015-11-29 09:23:09.477622951 +0100
  22. @@ -53,7 +53,7 @@
  23. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(int)))
  24. return -EFAULT;
  25. - pagefault_disable(); /* implies preempt_disable() */
  26. + pagefault_disable();
  27. switch (op) {
  28. case FUTEX_OP_SET:
  29. @@ -75,7 +75,7 @@
  30. ret = -ENOSYS;
  31. }
  32. - pagefault_enable(); /* subsumes preempt_enable() */
  33. + pagefault_enable();
  34. if (!ret) {
  35. switch (cmp) {
  36. @@ -104,7 +104,7 @@
  37. return ret;
  38. }
  39. -/* Compare-xchg with preemption disabled.
  40. +/* Compare-xchg with pagefaults disabled.
  41. * Notes:
  42. * -Best-Effort: Exchg happens only if compare succeeds.
  43. * If compare fails, returns; leaving retry/looping to upper layers
  44. @@ -121,7 +121,7 @@
  45. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(int)))
  46. return -EFAULT;
  47. - pagefault_disable(); /* implies preempt_disable() */
  48. + pagefault_disable();
  49. /* TBD : can use llock/scond */
  50. __asm__ __volatile__(
  51. @@ -142,7 +142,7 @@
  52. : "r"(oldval), "r"(newval), "r"(uaddr), "ir"(-EFAULT)
  53. : "cc", "memory");
  54. - pagefault_enable(); /* subsumes preempt_enable() */
  55. + pagefault_enable();
  56. *uval = val;
  57. return val;
  58. diff -Nur linux-4.1.13.orig/arch/arc/mm/fault.c linux-4.1.13/arch/arc/mm/fault.c
  59. --- linux-4.1.13.orig/arch/arc/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  60. +++ linux-4.1.13/arch/arc/mm/fault.c 2015-11-29 09:23:09.477622951 +0100
  61. @@ -86,7 +86,7 @@
  62. * If we're in an interrupt or have no user
  63. * context, we must not take the fault..
  64. */
  65. - if (in_atomic() || !mm)
  66. + if (faulthandler_disabled() || !mm)
  67. goto no_context;
  68. if (user_mode(regs))
  69. diff -Nur linux-4.1.13.orig/arch/arm/include/asm/cmpxchg.h linux-4.1.13/arch/arm/include/asm/cmpxchg.h
  70. --- linux-4.1.13.orig/arch/arm/include/asm/cmpxchg.h 2015-11-09 23:34:10.000000000 +0100
  71. +++ linux-4.1.13/arch/arm/include/asm/cmpxchg.h 2015-11-29 09:23:09.477622951 +0100
  72. @@ -129,6 +129,8 @@
  73. #else /* min ARCH >= ARMv6 */
  74. +#define __HAVE_ARCH_CMPXCHG 1
  75. +
  76. extern void __bad_cmpxchg(volatile void *ptr, int size);
  77. /*
  78. diff -Nur linux-4.1.13.orig/arch/arm/include/asm/futex.h linux-4.1.13/arch/arm/include/asm/futex.h
  79. --- linux-4.1.13.orig/arch/arm/include/asm/futex.h 2015-11-09 23:34:10.000000000 +0100
  80. +++ linux-4.1.13/arch/arm/include/asm/futex.h 2015-11-29 09:23:09.477622951 +0100
  81. @@ -93,6 +93,7 @@
  82. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  83. return -EFAULT;
  84. + preempt_disable();
  85. __asm__ __volatile__("@futex_atomic_cmpxchg_inatomic\n"
  86. "1: " TUSER(ldr) " %1, [%4]\n"
  87. " teq %1, %2\n"
  88. @@ -104,6 +105,8 @@
  89. : "cc", "memory");
  90. *uval = val;
  91. + preempt_enable();
  92. +
  93. return ret;
  94. }
  95. @@ -124,7 +127,10 @@
  96. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  97. return -EFAULT;
  98. - pagefault_disable(); /* implies preempt_disable() */
  99. +#ifndef CONFIG_SMP
  100. + preempt_disable();
  101. +#endif
  102. + pagefault_disable();
  103. switch (op) {
  104. case FUTEX_OP_SET:
  105. @@ -146,7 +152,10 @@
  106. ret = -ENOSYS;
  107. }
  108. - pagefault_enable(); /* subsumes preempt_enable() */
  109. + pagefault_enable();
  110. +#ifndef CONFIG_SMP
  111. + preempt_enable();
  112. +#endif
  113. if (!ret) {
  114. switch (cmp) {
  115. diff -Nur linux-4.1.13.orig/arch/arm/include/asm/switch_to.h linux-4.1.13/arch/arm/include/asm/switch_to.h
  116. --- linux-4.1.13.orig/arch/arm/include/asm/switch_to.h 2015-11-09 23:34:10.000000000 +0100
  117. +++ linux-4.1.13/arch/arm/include/asm/switch_to.h 2015-11-29 09:23:09.477622951 +0100
  118. @@ -3,6 +3,13 @@
  119. #include <linux/thread_info.h>
  120. +#if defined CONFIG_PREEMPT_RT_FULL && defined CONFIG_HIGHMEM
  121. +void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p);
  122. +#else
  123. +static inline void
  124. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  125. +#endif
  126. +
  127. /*
  128. * For v7 SMP cores running a preemptible kernel we may be pre-empted
  129. * during a TLB maintenance operation, so execute an inner-shareable dsb
  130. @@ -22,6 +29,7 @@
  131. #define switch_to(prev,next,last) \
  132. do { \
  133. + switch_kmaps(prev, next); \
  134. last = __switch_to(prev,task_thread_info(prev), task_thread_info(next)); \
  135. } while (0)
  136. diff -Nur linux-4.1.13.orig/arch/arm/include/asm/thread_info.h linux-4.1.13/arch/arm/include/asm/thread_info.h
  137. --- linux-4.1.13.orig/arch/arm/include/asm/thread_info.h 2015-11-09 23:34:10.000000000 +0100
  138. +++ linux-4.1.13/arch/arm/include/asm/thread_info.h 2015-11-29 09:23:09.477622951 +0100
  139. @@ -50,6 +50,7 @@
  140. struct thread_info {
  141. unsigned long flags; /* low level flags */
  142. int preempt_count; /* 0 => preemptable, <0 => bug */
  143. + int preempt_lazy_count; /* 0 => preemptable, <0 => bug */
  144. mm_segment_t addr_limit; /* address limit */
  145. struct task_struct *task; /* main task structure */
  146. __u32 cpu; /* cpu */
  147. @@ -147,6 +148,7 @@
  148. #define TIF_SIGPENDING 0
  149. #define TIF_NEED_RESCHED 1
  150. #define TIF_NOTIFY_RESUME 2 /* callback before returning to user */
  151. +#define TIF_NEED_RESCHED_LAZY 3
  152. #define TIF_UPROBE 7
  153. #define TIF_SYSCALL_TRACE 8
  154. #define TIF_SYSCALL_AUDIT 9
  155. @@ -160,6 +162,7 @@
  156. #define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
  157. #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
  158. #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
  159. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  160. #define _TIF_UPROBE (1 << TIF_UPROBE)
  161. #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
  162. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  163. diff -Nur linux-4.1.13.orig/arch/arm/Kconfig linux-4.1.13/arch/arm/Kconfig
  164. --- linux-4.1.13.orig/arch/arm/Kconfig 2015-11-09 23:34:10.000000000 +0100
  165. +++ linux-4.1.13/arch/arm/Kconfig 2015-11-29 09:23:09.477622951 +0100
  166. @@ -31,7 +31,7 @@
  167. select HARDIRQS_SW_RESEND
  168. select HAVE_ARCH_AUDITSYSCALL if (AEABI && !OABI_COMPAT)
  169. select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6
  170. - select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL
  171. + select HAVE_ARCH_JUMP_LABEL if (!XIP_KERNEL && !PREEMPT_RT_BASE)
  172. select HAVE_ARCH_KGDB
  173. select HAVE_ARCH_SECCOMP_FILTER if (AEABI && !OABI_COMPAT)
  174. select HAVE_ARCH_TRACEHOOK
  175. @@ -66,6 +66,7 @@
  176. select HAVE_PERF_EVENTS
  177. select HAVE_PERF_REGS
  178. select HAVE_PERF_USER_STACK_DUMP
  179. + select HAVE_PREEMPT_LAZY
  180. select HAVE_RCU_TABLE_FREE if (SMP && ARM_LPAE)
  181. select HAVE_REGS_AND_STACK_ACCESS_API
  182. select HAVE_SYSCALL_TRACEPOINTS
  183. diff -Nur linux-4.1.13.orig/arch/arm/kernel/asm-offsets.c linux-4.1.13/arch/arm/kernel/asm-offsets.c
  184. --- linux-4.1.13.orig/arch/arm/kernel/asm-offsets.c 2015-11-09 23:34:10.000000000 +0100
  185. +++ linux-4.1.13/arch/arm/kernel/asm-offsets.c 2015-11-29 09:23:09.477622951 +0100
  186. @@ -65,6 +65,7 @@
  187. BLANK();
  188. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  189. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  190. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  191. DEFINE(TI_ADDR_LIMIT, offsetof(struct thread_info, addr_limit));
  192. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  193. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  194. diff -Nur linux-4.1.13.orig/arch/arm/kernel/entry-armv.S linux-4.1.13/arch/arm/kernel/entry-armv.S
  195. --- linux-4.1.13.orig/arch/arm/kernel/entry-armv.S 2015-11-09 23:34:10.000000000 +0100
  196. +++ linux-4.1.13/arch/arm/kernel/entry-armv.S 2015-11-29 09:23:09.477622951 +0100
  197. @@ -208,11 +208,18 @@
  198. #ifdef CONFIG_PREEMPT
  199. get_thread_info tsk
  200. ldr r8, [tsk, #TI_PREEMPT] @ get preempt count
  201. - ldr r0, [tsk, #TI_FLAGS] @ get flags
  202. teq r8, #0 @ if preempt count != 0
  203. + bne 1f @ return from exeption
  204. + ldr r0, [tsk, #TI_FLAGS] @ get flags
  205. + tst r0, #_TIF_NEED_RESCHED @ if NEED_RESCHED is set
  206. + blne svc_preempt @ preempt!
  207. +
  208. + ldr r8, [tsk, #TI_PREEMPT_LAZY] @ get preempt lazy count
  209. + teq r8, #0 @ if preempt lazy count != 0
  210. movne r0, #0 @ force flags to 0
  211. - tst r0, #_TIF_NEED_RESCHED
  212. + tst r0, #_TIF_NEED_RESCHED_LAZY
  213. blne svc_preempt
  214. +1:
  215. #endif
  216. svc_exit r5, irq = 1 @ return from exception
  217. @@ -227,6 +234,8 @@
  218. 1: bl preempt_schedule_irq @ irq en/disable is done inside
  219. ldr r0, [tsk, #TI_FLAGS] @ get new tasks TI_FLAGS
  220. tst r0, #_TIF_NEED_RESCHED
  221. + bne 1b
  222. + tst r0, #_TIF_NEED_RESCHED_LAZY
  223. reteq r8 @ go again
  224. b 1b
  225. #endif
  226. diff -Nur linux-4.1.13.orig/arch/arm/kernel/process.c linux-4.1.13/arch/arm/kernel/process.c
  227. --- linux-4.1.13.orig/arch/arm/kernel/process.c 2015-11-09 23:34:10.000000000 +0100
  228. +++ linux-4.1.13/arch/arm/kernel/process.c 2015-11-29 09:23:09.477622951 +0100
  229. @@ -290,6 +290,30 @@
  230. }
  231. #ifdef CONFIG_MMU
  232. +/*
  233. + * CONFIG_SPLIT_PTLOCK_CPUS results in a page->ptl lock. If the lock is not
  234. + * initialized by pgtable_page_ctor() then a coredump of the vector page will
  235. + * fail.
  236. + */
  237. +static int __init vectors_user_mapping_init_page(void)
  238. +{
  239. + struct page *page;
  240. + unsigned long addr = 0xffff0000;
  241. + pgd_t *pgd;
  242. + pud_t *pud;
  243. + pmd_t *pmd;
  244. +
  245. + pgd = pgd_offset_k(addr);
  246. + pud = pud_offset(pgd, addr);
  247. + pmd = pmd_offset(pud, addr);
  248. + page = pmd_page(*(pmd));
  249. +
  250. + pgtable_page_ctor(page);
  251. +
  252. + return 0;
  253. +}
  254. +late_initcall(vectors_user_mapping_init_page);
  255. +
  256. #ifdef CONFIG_KUSER_HELPERS
  257. /*
  258. * The vectors page is always readable from user space for the
  259. diff -Nur linux-4.1.13.orig/arch/arm/kernel/signal.c linux-4.1.13/arch/arm/kernel/signal.c
  260. --- linux-4.1.13.orig/arch/arm/kernel/signal.c 2015-11-09 23:34:10.000000000 +0100
  261. +++ linux-4.1.13/arch/arm/kernel/signal.c 2015-11-29 09:23:09.477622951 +0100
  262. @@ -568,7 +568,8 @@
  263. do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
  264. {
  265. do {
  266. - if (likely(thread_flags & _TIF_NEED_RESCHED)) {
  267. + if (likely(thread_flags & (_TIF_NEED_RESCHED |
  268. + _TIF_NEED_RESCHED_LAZY))) {
  269. schedule();
  270. } else {
  271. if (unlikely(!user_mode(regs)))
  272. diff -Nur linux-4.1.13.orig/arch/arm/kernel/smp.c linux-4.1.13/arch/arm/kernel/smp.c
  273. --- linux-4.1.13.orig/arch/arm/kernel/smp.c 2015-11-09 23:34:10.000000000 +0100
  274. +++ linux-4.1.13/arch/arm/kernel/smp.c 2015-11-29 09:23:09.481622687 +0100
  275. @@ -213,8 +213,6 @@
  276. flush_cache_louis();
  277. local_flush_tlb_all();
  278. - clear_tasks_mm_cpumask(cpu);
  279. -
  280. return 0;
  281. }
  282. @@ -230,6 +228,9 @@
  283. pr_err("CPU%u: cpu didn't die\n", cpu);
  284. return;
  285. }
  286. +
  287. + clear_tasks_mm_cpumask(cpu);
  288. +
  289. pr_notice("CPU%u: shutdown\n", cpu);
  290. /*
  291. diff -Nur linux-4.1.13.orig/arch/arm/kernel/unwind.c linux-4.1.13/arch/arm/kernel/unwind.c
  292. --- linux-4.1.13.orig/arch/arm/kernel/unwind.c 2015-11-09 23:34:10.000000000 +0100
  293. +++ linux-4.1.13/arch/arm/kernel/unwind.c 2015-11-29 09:23:09.481622687 +0100
  294. @@ -93,7 +93,7 @@
  295. static const struct unwind_idx *__origin_unwind_idx;
  296. extern const struct unwind_idx __stop_unwind_idx[];
  297. -static DEFINE_SPINLOCK(unwind_lock);
  298. +static DEFINE_RAW_SPINLOCK(unwind_lock);
  299. static LIST_HEAD(unwind_tables);
  300. /* Convert a prel31 symbol to an absolute address */
  301. @@ -201,7 +201,7 @@
  302. /* module unwind tables */
  303. struct unwind_table *table;
  304. - spin_lock_irqsave(&unwind_lock, flags);
  305. + raw_spin_lock_irqsave(&unwind_lock, flags);
  306. list_for_each_entry(table, &unwind_tables, list) {
  307. if (addr >= table->begin_addr &&
  308. addr < table->end_addr) {
  309. @@ -213,7 +213,7 @@
  310. break;
  311. }
  312. }
  313. - spin_unlock_irqrestore(&unwind_lock, flags);
  314. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  315. }
  316. pr_debug("%s: idx = %p\n", __func__, idx);
  317. @@ -529,9 +529,9 @@
  318. tab->begin_addr = text_addr;
  319. tab->end_addr = text_addr + text_size;
  320. - spin_lock_irqsave(&unwind_lock, flags);
  321. + raw_spin_lock_irqsave(&unwind_lock, flags);
  322. list_add_tail(&tab->list, &unwind_tables);
  323. - spin_unlock_irqrestore(&unwind_lock, flags);
  324. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  325. return tab;
  326. }
  327. @@ -543,9 +543,9 @@
  328. if (!tab)
  329. return;
  330. - spin_lock_irqsave(&unwind_lock, flags);
  331. + raw_spin_lock_irqsave(&unwind_lock, flags);
  332. list_del(&tab->list);
  333. - spin_unlock_irqrestore(&unwind_lock, flags);
  334. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  335. kfree(tab);
  336. }
  337. diff -Nur linux-4.1.13.orig/arch/arm/kvm/arm.c linux-4.1.13/arch/arm/kvm/arm.c
  338. --- linux-4.1.13.orig/arch/arm/kvm/arm.c 2015-11-09 23:34:10.000000000 +0100
  339. +++ linux-4.1.13/arch/arm/kvm/arm.c 2015-11-29 09:23:09.481622687 +0100
  340. @@ -474,9 +474,9 @@
  341. static void vcpu_pause(struct kvm_vcpu *vcpu)
  342. {
  343. - wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  344. + struct swait_head *wq = kvm_arch_vcpu_wq(vcpu);
  345. - wait_event_interruptible(*wq, !vcpu->arch.pause);
  346. + swait_event_interruptible(*wq, !vcpu->arch.pause);
  347. }
  348. static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
  349. diff -Nur linux-4.1.13.orig/arch/arm/kvm/psci.c linux-4.1.13/arch/arm/kvm/psci.c
  350. --- linux-4.1.13.orig/arch/arm/kvm/psci.c 2015-11-09 23:34:10.000000000 +0100
  351. +++ linux-4.1.13/arch/arm/kvm/psci.c 2015-11-29 09:23:09.501621354 +0100
  352. @@ -68,7 +68,7 @@
  353. {
  354. struct kvm *kvm = source_vcpu->kvm;
  355. struct kvm_vcpu *vcpu = NULL;
  356. - wait_queue_head_t *wq;
  357. + struct swait_head *wq;
  358. unsigned long cpu_id;
  359. unsigned long context_id;
  360. phys_addr_t target_pc;
  361. @@ -117,7 +117,7 @@
  362. smp_mb(); /* Make sure the above is visible */
  363. wq = kvm_arch_vcpu_wq(vcpu);
  364. - wake_up_interruptible(wq);
  365. + swait_wake_interruptible(wq);
  366. return PSCI_RET_SUCCESS;
  367. }
  368. diff -Nur linux-4.1.13.orig/arch/arm/mach-exynos/platsmp.c linux-4.1.13/arch/arm/mach-exynos/platsmp.c
  369. --- linux-4.1.13.orig/arch/arm/mach-exynos/platsmp.c 2015-11-09 23:34:10.000000000 +0100
  370. +++ linux-4.1.13/arch/arm/mach-exynos/platsmp.c 2015-11-29 09:23:09.501621354 +0100
  371. @@ -231,7 +231,7 @@
  372. return (void __iomem *)(S5P_VA_SCU);
  373. }
  374. -static DEFINE_SPINLOCK(boot_lock);
  375. +static DEFINE_RAW_SPINLOCK(boot_lock);
  376. static void exynos_secondary_init(unsigned int cpu)
  377. {
  378. @@ -244,8 +244,8 @@
  379. /*
  380. * Synchronise with the boot thread.
  381. */
  382. - spin_lock(&boot_lock);
  383. - spin_unlock(&boot_lock);
  384. + raw_spin_lock(&boot_lock);
  385. + raw_spin_unlock(&boot_lock);
  386. }
  387. static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  388. @@ -259,7 +259,7 @@
  389. * Set synchronisation state between this boot processor
  390. * and the secondary one
  391. */
  392. - spin_lock(&boot_lock);
  393. + raw_spin_lock(&boot_lock);
  394. /*
  395. * The secondary processor is waiting to be released from
  396. @@ -286,7 +286,7 @@
  397. if (timeout == 0) {
  398. printk(KERN_ERR "cpu1 power enable failed");
  399. - spin_unlock(&boot_lock);
  400. + raw_spin_unlock(&boot_lock);
  401. return -ETIMEDOUT;
  402. }
  403. }
  404. @@ -342,7 +342,7 @@
  405. * calibrations, then wait for it to finish
  406. */
  407. fail:
  408. - spin_unlock(&boot_lock);
  409. + raw_spin_unlock(&boot_lock);
  410. return pen_release != -1 ? ret : 0;
  411. }
  412. diff -Nur linux-4.1.13.orig/arch/arm/mach-hisi/platmcpm.c linux-4.1.13/arch/arm/mach-hisi/platmcpm.c
  413. --- linux-4.1.13.orig/arch/arm/mach-hisi/platmcpm.c 2015-11-09 23:34:10.000000000 +0100
  414. +++ linux-4.1.13/arch/arm/mach-hisi/platmcpm.c 2015-11-29 09:23:09.501621354 +0100
  415. @@ -57,7 +57,7 @@
  416. static void __iomem *sysctrl, *fabric;
  417. static int hip04_cpu_table[HIP04_MAX_CLUSTERS][HIP04_MAX_CPUS_PER_CLUSTER];
  418. -static DEFINE_SPINLOCK(boot_lock);
  419. +static DEFINE_RAW_SPINLOCK(boot_lock);
  420. static u32 fabric_phys_addr;
  421. /*
  422. * [0]: bootwrapper physical address
  423. @@ -104,7 +104,7 @@
  424. if (cluster >= HIP04_MAX_CLUSTERS || cpu >= HIP04_MAX_CPUS_PER_CLUSTER)
  425. return -EINVAL;
  426. - spin_lock_irq(&boot_lock);
  427. + raw_spin_lock_irq(&boot_lock);
  428. if (hip04_cpu_table[cluster][cpu])
  429. goto out;
  430. @@ -133,7 +133,7 @@
  431. udelay(20);
  432. out:
  433. hip04_cpu_table[cluster][cpu]++;
  434. - spin_unlock_irq(&boot_lock);
  435. + raw_spin_unlock_irq(&boot_lock);
  436. return 0;
  437. }
  438. @@ -149,7 +149,7 @@
  439. __mcpm_cpu_going_down(cpu, cluster);
  440. - spin_lock(&boot_lock);
  441. + raw_spin_lock(&boot_lock);
  442. BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
  443. hip04_cpu_table[cluster][cpu]--;
  444. if (hip04_cpu_table[cluster][cpu] == 1) {
  445. @@ -162,7 +162,7 @@
  446. last_man = hip04_cluster_is_down(cluster);
  447. if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
  448. - spin_unlock(&boot_lock);
  449. + raw_spin_unlock(&boot_lock);
  450. /* Since it's Cortex A15, disable L2 prefetching. */
  451. asm volatile(
  452. "mcr p15, 1, %0, c15, c0, 3 \n\t"
  453. @@ -173,7 +173,7 @@
  454. hip04_set_snoop_filter(cluster, 0);
  455. __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
  456. } else {
  457. - spin_unlock(&boot_lock);
  458. + raw_spin_unlock(&boot_lock);
  459. v7_exit_coherency_flush(louis);
  460. }
  461. @@ -192,7 +192,7 @@
  462. cpu >= HIP04_MAX_CPUS_PER_CLUSTER);
  463. count = TIMEOUT_MSEC / POLL_MSEC;
  464. - spin_lock_irq(&boot_lock);
  465. + raw_spin_lock_irq(&boot_lock);
  466. for (tries = 0; tries < count; tries++) {
  467. if (hip04_cpu_table[cluster][cpu]) {
  468. ret = -EBUSY;
  469. @@ -202,10 +202,10 @@
  470. data = readl_relaxed(sysctrl + SC_CPU_RESET_STATUS(cluster));
  471. if (data & CORE_WFI_STATUS(cpu))
  472. break;
  473. - spin_unlock_irq(&boot_lock);
  474. + raw_spin_unlock_irq(&boot_lock);
  475. /* Wait for clean L2 when the whole cluster is down. */
  476. msleep(POLL_MSEC);
  477. - spin_lock_irq(&boot_lock);
  478. + raw_spin_lock_irq(&boot_lock);
  479. }
  480. if (tries >= count)
  481. goto err;
  482. @@ -220,10 +220,10 @@
  483. }
  484. if (tries >= count)
  485. goto err;
  486. - spin_unlock_irq(&boot_lock);
  487. + raw_spin_unlock_irq(&boot_lock);
  488. return 0;
  489. err:
  490. - spin_unlock_irq(&boot_lock);
  491. + raw_spin_unlock_irq(&boot_lock);
  492. return ret;
  493. }
  494. @@ -235,10 +235,10 @@
  495. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  496. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  497. - spin_lock(&boot_lock);
  498. + raw_spin_lock(&boot_lock);
  499. if (!hip04_cpu_table[cluster][cpu])
  500. hip04_cpu_table[cluster][cpu] = 1;
  501. - spin_unlock(&boot_lock);
  502. + raw_spin_unlock(&boot_lock);
  503. }
  504. static void __naked hip04_mcpm_power_up_setup(unsigned int affinity_level)
  505. diff -Nur linux-4.1.13.orig/arch/arm/mach-omap2/omap-smp.c linux-4.1.13/arch/arm/mach-omap2/omap-smp.c
  506. --- linux-4.1.13.orig/arch/arm/mach-omap2/omap-smp.c 2015-11-09 23:34:10.000000000 +0100
  507. +++ linux-4.1.13/arch/arm/mach-omap2/omap-smp.c 2015-11-29 09:23:09.501621354 +0100
  508. @@ -43,7 +43,7 @@
  509. /* SCU base address */
  510. static void __iomem *scu_base;
  511. -static DEFINE_SPINLOCK(boot_lock);
  512. +static DEFINE_RAW_SPINLOCK(boot_lock);
  513. void __iomem *omap4_get_scu_base(void)
  514. {
  515. @@ -74,8 +74,8 @@
  516. /*
  517. * Synchronise with the boot thread.
  518. */
  519. - spin_lock(&boot_lock);
  520. - spin_unlock(&boot_lock);
  521. + raw_spin_lock(&boot_lock);
  522. + raw_spin_unlock(&boot_lock);
  523. }
  524. static int omap4_boot_secondary(unsigned int cpu, struct task_struct *idle)
  525. @@ -89,7 +89,7 @@
  526. * Set synchronisation state between this boot processor
  527. * and the secondary one
  528. */
  529. - spin_lock(&boot_lock);
  530. + raw_spin_lock(&boot_lock);
  531. /*
  532. * Update the AuxCoreBoot0 with boot state for secondary core.
  533. @@ -166,7 +166,7 @@
  534. * Now the secondary core is starting up let it run its
  535. * calibrations, then wait for it to finish
  536. */
  537. - spin_unlock(&boot_lock);
  538. + raw_spin_unlock(&boot_lock);
  539. return 0;
  540. }
  541. diff -Nur linux-4.1.13.orig/arch/arm/mach-prima2/platsmp.c linux-4.1.13/arch/arm/mach-prima2/platsmp.c
  542. --- linux-4.1.13.orig/arch/arm/mach-prima2/platsmp.c 2015-11-09 23:34:10.000000000 +0100
  543. +++ linux-4.1.13/arch/arm/mach-prima2/platsmp.c 2015-11-29 09:23:09.501621354 +0100
  544. @@ -22,7 +22,7 @@
  545. static void __iomem *clk_base;
  546. -static DEFINE_SPINLOCK(boot_lock);
  547. +static DEFINE_RAW_SPINLOCK(boot_lock);
  548. static void sirfsoc_secondary_init(unsigned int cpu)
  549. {
  550. @@ -36,8 +36,8 @@
  551. /*
  552. * Synchronise with the boot thread.
  553. */
  554. - spin_lock(&boot_lock);
  555. - spin_unlock(&boot_lock);
  556. + raw_spin_lock(&boot_lock);
  557. + raw_spin_unlock(&boot_lock);
  558. }
  559. static const struct of_device_id clk_ids[] = {
  560. @@ -75,7 +75,7 @@
  561. /* make sure write buffer is drained */
  562. mb();
  563. - spin_lock(&boot_lock);
  564. + raw_spin_lock(&boot_lock);
  565. /*
  566. * The secondary processor is waiting to be released from
  567. @@ -107,7 +107,7 @@
  568. * now the secondary core is starting up let it run its
  569. * calibrations, then wait for it to finish
  570. */
  571. - spin_unlock(&boot_lock);
  572. + raw_spin_unlock(&boot_lock);
  573. return pen_release != -1 ? -ENOSYS : 0;
  574. }
  575. diff -Nur linux-4.1.13.orig/arch/arm/mach-qcom/platsmp.c linux-4.1.13/arch/arm/mach-qcom/platsmp.c
  576. --- linux-4.1.13.orig/arch/arm/mach-qcom/platsmp.c 2015-11-09 23:34:10.000000000 +0100
  577. +++ linux-4.1.13/arch/arm/mach-qcom/platsmp.c 2015-11-29 09:23:09.501621354 +0100
  578. @@ -46,7 +46,7 @@
  579. extern void secondary_startup_arm(void);
  580. -static DEFINE_SPINLOCK(boot_lock);
  581. +static DEFINE_RAW_SPINLOCK(boot_lock);
  582. #ifdef CONFIG_HOTPLUG_CPU
  583. static void __ref qcom_cpu_die(unsigned int cpu)
  584. @@ -60,8 +60,8 @@
  585. /*
  586. * Synchronise with the boot thread.
  587. */
  588. - spin_lock(&boot_lock);
  589. - spin_unlock(&boot_lock);
  590. + raw_spin_lock(&boot_lock);
  591. + raw_spin_unlock(&boot_lock);
  592. }
  593. static int scss_release_secondary(unsigned int cpu)
  594. @@ -284,7 +284,7 @@
  595. * set synchronisation state between this boot processor
  596. * and the secondary one
  597. */
  598. - spin_lock(&boot_lock);
  599. + raw_spin_lock(&boot_lock);
  600. /*
  601. * Send the secondary CPU a soft interrupt, thereby causing
  602. @@ -297,7 +297,7 @@
  603. * now the secondary core is starting up let it run its
  604. * calibrations, then wait for it to finish
  605. */
  606. - spin_unlock(&boot_lock);
  607. + raw_spin_unlock(&boot_lock);
  608. return ret;
  609. }
  610. diff -Nur linux-4.1.13.orig/arch/arm/mach-spear/platsmp.c linux-4.1.13/arch/arm/mach-spear/platsmp.c
  611. --- linux-4.1.13.orig/arch/arm/mach-spear/platsmp.c 2015-11-09 23:34:10.000000000 +0100
  612. +++ linux-4.1.13/arch/arm/mach-spear/platsmp.c 2015-11-29 09:23:09.501621354 +0100
  613. @@ -32,7 +32,7 @@
  614. sync_cache_w(&pen_release);
  615. }
  616. -static DEFINE_SPINLOCK(boot_lock);
  617. +static DEFINE_RAW_SPINLOCK(boot_lock);
  618. static void __iomem *scu_base = IOMEM(VA_SCU_BASE);
  619. @@ -47,8 +47,8 @@
  620. /*
  621. * Synchronise with the boot thread.
  622. */
  623. - spin_lock(&boot_lock);
  624. - spin_unlock(&boot_lock);
  625. + raw_spin_lock(&boot_lock);
  626. + raw_spin_unlock(&boot_lock);
  627. }
  628. static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
  629. @@ -59,7 +59,7 @@
  630. * set synchronisation state between this boot processor
  631. * and the secondary one
  632. */
  633. - spin_lock(&boot_lock);
  634. + raw_spin_lock(&boot_lock);
  635. /*
  636. * The secondary processor is waiting to be released from
  637. @@ -84,7 +84,7 @@
  638. * now the secondary core is starting up let it run its
  639. * calibrations, then wait for it to finish
  640. */
  641. - spin_unlock(&boot_lock);
  642. + raw_spin_unlock(&boot_lock);
  643. return pen_release != -1 ? -ENOSYS : 0;
  644. }
  645. diff -Nur linux-4.1.13.orig/arch/arm/mach-sti/platsmp.c linux-4.1.13/arch/arm/mach-sti/platsmp.c
  646. --- linux-4.1.13.orig/arch/arm/mach-sti/platsmp.c 2015-11-09 23:34:10.000000000 +0100
  647. +++ linux-4.1.13/arch/arm/mach-sti/platsmp.c 2015-11-29 09:23:09.501621354 +0100
  648. @@ -34,7 +34,7 @@
  649. sync_cache_w(&pen_release);
  650. }
  651. -static DEFINE_SPINLOCK(boot_lock);
  652. +static DEFINE_RAW_SPINLOCK(boot_lock);
  653. static void sti_secondary_init(unsigned int cpu)
  654. {
  655. @@ -49,8 +49,8 @@
  656. /*
  657. * Synchronise with the boot thread.
  658. */
  659. - spin_lock(&boot_lock);
  660. - spin_unlock(&boot_lock);
  661. + raw_spin_lock(&boot_lock);
  662. + raw_spin_unlock(&boot_lock);
  663. }
  664. static int sti_boot_secondary(unsigned int cpu, struct task_struct *idle)
  665. @@ -61,7 +61,7 @@
  666. * set synchronisation state between this boot processor
  667. * and the secondary one
  668. */
  669. - spin_lock(&boot_lock);
  670. + raw_spin_lock(&boot_lock);
  671. /*
  672. * The secondary processor is waiting to be released from
  673. @@ -92,7 +92,7 @@
  674. * now the secondary core is starting up let it run its
  675. * calibrations, then wait for it to finish
  676. */
  677. - spin_unlock(&boot_lock);
  678. + raw_spin_unlock(&boot_lock);
  679. return pen_release != -1 ? -ENOSYS : 0;
  680. }
  681. diff -Nur linux-4.1.13.orig/arch/arm/mach-ux500/platsmp.c linux-4.1.13/arch/arm/mach-ux500/platsmp.c
  682. --- linux-4.1.13.orig/arch/arm/mach-ux500/platsmp.c 2015-11-09 23:34:10.000000000 +0100
  683. +++ linux-4.1.13/arch/arm/mach-ux500/platsmp.c 2015-11-29 09:23:09.501621354 +0100
  684. @@ -51,7 +51,7 @@
  685. return NULL;
  686. }
  687. -static DEFINE_SPINLOCK(boot_lock);
  688. +static DEFINE_RAW_SPINLOCK(boot_lock);
  689. static void ux500_secondary_init(unsigned int cpu)
  690. {
  691. @@ -64,8 +64,8 @@
  692. /*
  693. * Synchronise with the boot thread.
  694. */
  695. - spin_lock(&boot_lock);
  696. - spin_unlock(&boot_lock);
  697. + raw_spin_lock(&boot_lock);
  698. + raw_spin_unlock(&boot_lock);
  699. }
  700. static int ux500_boot_secondary(unsigned int cpu, struct task_struct *idle)
  701. @@ -76,7 +76,7 @@
  702. * set synchronisation state between this boot processor
  703. * and the secondary one
  704. */
  705. - spin_lock(&boot_lock);
  706. + raw_spin_lock(&boot_lock);
  707. /*
  708. * The secondary processor is waiting to be released from
  709. @@ -97,7 +97,7 @@
  710. * now the secondary core is starting up let it run its
  711. * calibrations, then wait for it to finish
  712. */
  713. - spin_unlock(&boot_lock);
  714. + raw_spin_unlock(&boot_lock);
  715. return pen_release != -1 ? -ENOSYS : 0;
  716. }
  717. diff -Nur linux-4.1.13.orig/arch/arm/mm/fault.c linux-4.1.13/arch/arm/mm/fault.c
  718. --- linux-4.1.13.orig/arch/arm/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  719. +++ linux-4.1.13/arch/arm/mm/fault.c 2015-11-29 09:23:09.501621354 +0100
  720. @@ -276,7 +276,7 @@
  721. * If we're in an interrupt or have no user
  722. * context, we must not take the fault..
  723. */
  724. - if (in_atomic() || !mm)
  725. + if (faulthandler_disabled() || !mm)
  726. goto no_context;
  727. if (user_mode(regs))
  728. @@ -430,6 +430,9 @@
  729. if (addr < TASK_SIZE)
  730. return do_page_fault(addr, fsr, regs);
  731. + if (interrupts_enabled(regs))
  732. + local_irq_enable();
  733. +
  734. if (user_mode(regs))
  735. goto bad_area;
  736. @@ -497,6 +500,9 @@
  737. static int
  738. do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  739. {
  740. + if (interrupts_enabled(regs))
  741. + local_irq_enable();
  742. +
  743. do_bad_area(addr, fsr, regs);
  744. return 0;
  745. }
  746. diff -Nur linux-4.1.13.orig/arch/arm/mm/highmem.c linux-4.1.13/arch/arm/mm/highmem.c
  747. --- linux-4.1.13.orig/arch/arm/mm/highmem.c 2015-11-09 23:34:10.000000000 +0100
  748. +++ linux-4.1.13/arch/arm/mm/highmem.c 2015-11-29 09:23:09.505621090 +0100
  749. @@ -54,11 +54,13 @@
  750. void *kmap_atomic(struct page *page)
  751. {
  752. + pte_t pte = mk_pte(page, kmap_prot);
  753. unsigned int idx;
  754. unsigned long vaddr;
  755. void *kmap;
  756. int type;
  757. + preempt_disable_nort();
  758. pagefault_disable();
  759. if (!PageHighMem(page))
  760. return page_address(page);
  761. @@ -92,7 +94,10 @@
  762. * in place, so the contained TLB flush ensures the TLB is updated
  763. * with the new mapping.
  764. */
  765. - set_fixmap_pte(idx, mk_pte(page, kmap_prot));
  766. +#ifdef CONFIG_PREEMPT_RT_FULL
  767. + current->kmap_pte[type] = pte;
  768. +#endif
  769. + set_fixmap_pte(idx, pte);
  770. return (void *)vaddr;
  771. }
  772. @@ -109,27 +114,33 @@
  773. if (cache_is_vivt())
  774. __cpuc_flush_dcache_area((void *)vaddr, PAGE_SIZE);
  775. +#ifdef CONFIG_PREEMPT_RT_FULL
  776. + current->kmap_pte[type] = __pte(0);
  777. +#endif
  778. #ifdef CONFIG_DEBUG_HIGHMEM
  779. BUG_ON(vaddr != __fix_to_virt(idx));
  780. - set_fixmap_pte(idx, __pte(0));
  781. #else
  782. (void) idx; /* to kill a warning */
  783. #endif
  784. + set_fixmap_pte(idx, __pte(0));
  785. kmap_atomic_idx_pop();
  786. } else if (vaddr >= PKMAP_ADDR(0) && vaddr < PKMAP_ADDR(LAST_PKMAP)) {
  787. /* this address was obtained through kmap_high_get() */
  788. kunmap_high(pte_page(pkmap_page_table[PKMAP_NR(vaddr)]));
  789. }
  790. pagefault_enable();
  791. + preempt_enable_nort();
  792. }
  793. EXPORT_SYMBOL(__kunmap_atomic);
  794. void *kmap_atomic_pfn(unsigned long pfn)
  795. {
  796. + pte_t pte = pfn_pte(pfn, kmap_prot);
  797. unsigned long vaddr;
  798. int idx, type;
  799. struct page *page = pfn_to_page(pfn);
  800. + preempt_disable_nort();
  801. pagefault_disable();
  802. if (!PageHighMem(page))
  803. return page_address(page);
  804. @@ -140,7 +151,10 @@
  805. #ifdef CONFIG_DEBUG_HIGHMEM
  806. BUG_ON(!pte_none(get_fixmap_pte(vaddr)));
  807. #endif
  808. - set_fixmap_pte(idx, pfn_pte(pfn, kmap_prot));
  809. +#ifdef CONFIG_PREEMPT_RT_FULL
  810. + current->kmap_pte[type] = pte;
  811. +#endif
  812. + set_fixmap_pte(idx, pte);
  813. return (void *)vaddr;
  814. }
  815. @@ -154,3 +168,28 @@
  816. return pte_page(get_fixmap_pte(vaddr));
  817. }
  818. +
  819. +#if defined CONFIG_PREEMPT_RT_FULL
  820. +void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  821. +{
  822. + int i;
  823. +
  824. + /*
  825. + * Clear @prev's kmap_atomic mappings
  826. + */
  827. + for (i = 0; i < prev_p->kmap_idx; i++) {
  828. + int idx = i + KM_TYPE_NR * smp_processor_id();
  829. +
  830. + set_fixmap_pte(idx, __pte(0));
  831. + }
  832. + /*
  833. + * Restore @next_p's kmap_atomic mappings
  834. + */
  835. + for (i = 0; i < next_p->kmap_idx; i++) {
  836. + int idx = i + KM_TYPE_NR * smp_processor_id();
  837. +
  838. + if (!pte_none(next_p->kmap_pte[i]))
  839. + set_fixmap_pte(idx, next_p->kmap_pte[i]);
  840. + }
  841. +}
  842. +#endif
  843. diff -Nur linux-4.1.13.orig/arch/arm/plat-versatile/platsmp.c linux-4.1.13/arch/arm/plat-versatile/platsmp.c
  844. --- linux-4.1.13.orig/arch/arm/plat-versatile/platsmp.c 2015-11-09 23:34:10.000000000 +0100
  845. +++ linux-4.1.13/arch/arm/plat-versatile/platsmp.c 2015-11-29 09:23:09.505621090 +0100
  846. @@ -30,7 +30,7 @@
  847. sync_cache_w(&pen_release);
  848. }
  849. -static DEFINE_SPINLOCK(boot_lock);
  850. +static DEFINE_RAW_SPINLOCK(boot_lock);
  851. void versatile_secondary_init(unsigned int cpu)
  852. {
  853. @@ -43,8 +43,8 @@
  854. /*
  855. * Synchronise with the boot thread.
  856. */
  857. - spin_lock(&boot_lock);
  858. - spin_unlock(&boot_lock);
  859. + raw_spin_lock(&boot_lock);
  860. + raw_spin_unlock(&boot_lock);
  861. }
  862. int versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
  863. @@ -55,7 +55,7 @@
  864. * Set synchronisation state between this boot processor
  865. * and the secondary one
  866. */
  867. - spin_lock(&boot_lock);
  868. + raw_spin_lock(&boot_lock);
  869. /*
  870. * This is really belt and braces; we hold unintended secondary
  871. @@ -85,7 +85,7 @@
  872. * now the secondary core is starting up let it run its
  873. * calibrations, then wait for it to finish
  874. */
  875. - spin_unlock(&boot_lock);
  876. + raw_spin_unlock(&boot_lock);
  877. return pen_release != -1 ? -ENOSYS : 0;
  878. }
  879. diff -Nur linux-4.1.13.orig/arch/arm64/include/asm/futex.h linux-4.1.13/arch/arm64/include/asm/futex.h
  880. --- linux-4.1.13.orig/arch/arm64/include/asm/futex.h 2015-11-09 23:34:10.000000000 +0100
  881. +++ linux-4.1.13/arch/arm64/include/asm/futex.h 2015-11-29 09:23:09.505621090 +0100
  882. @@ -58,7 +58,7 @@
  883. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  884. return -EFAULT;
  885. - pagefault_disable(); /* implies preempt_disable() */
  886. + pagefault_disable();
  887. switch (op) {
  888. case FUTEX_OP_SET:
  889. @@ -85,7 +85,7 @@
  890. ret = -ENOSYS;
  891. }
  892. - pagefault_enable(); /* subsumes preempt_enable() */
  893. + pagefault_enable();
  894. if (!ret) {
  895. switch (cmp) {
  896. diff -Nur linux-4.1.13.orig/arch/arm64/include/asm/thread_info.h linux-4.1.13/arch/arm64/include/asm/thread_info.h
  897. --- linux-4.1.13.orig/arch/arm64/include/asm/thread_info.h 2015-11-09 23:34:10.000000000 +0100
  898. +++ linux-4.1.13/arch/arm64/include/asm/thread_info.h 2015-11-29 09:23:09.505621090 +0100
  899. @@ -47,6 +47,7 @@
  900. mm_segment_t addr_limit; /* address limit */
  901. struct task_struct *task; /* main task structure */
  902. int preempt_count; /* 0 => preemptable, <0 => bug */
  903. + int preempt_lazy_count; /* 0 => preemptable, <0 => bug */
  904. int cpu; /* cpu */
  905. };
  906. @@ -101,6 +102,7 @@
  907. #define TIF_NEED_RESCHED 1
  908. #define TIF_NOTIFY_RESUME 2 /* callback before returning to user */
  909. #define TIF_FOREIGN_FPSTATE 3 /* CPU's FP state is not current's */
  910. +#define TIF_NEED_RESCHED_LAZY 4
  911. #define TIF_NOHZ 7
  912. #define TIF_SYSCALL_TRACE 8
  913. #define TIF_SYSCALL_AUDIT 9
  914. @@ -117,6 +119,7 @@
  915. #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
  916. #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
  917. #define _TIF_FOREIGN_FPSTATE (1 << TIF_FOREIGN_FPSTATE)
  918. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  919. #define _TIF_NOHZ (1 << TIF_NOHZ)
  920. #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
  921. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  922. diff -Nur linux-4.1.13.orig/arch/arm64/Kconfig linux-4.1.13/arch/arm64/Kconfig
  923. --- linux-4.1.13.orig/arch/arm64/Kconfig 2015-11-09 23:34:10.000000000 +0100
  924. +++ linux-4.1.13/arch/arm64/Kconfig 2015-11-29 09:23:09.505621090 +0100
  925. @@ -69,8 +69,10 @@
  926. select HAVE_PERF_REGS
  927. select HAVE_PERF_USER_STACK_DUMP
  928. select HAVE_RCU_TABLE_FREE
  929. + select HAVE_PREEMPT_LAZY
  930. select HAVE_SYSCALL_TRACEPOINTS
  931. select IRQ_DOMAIN
  932. + select IRQ_FORCED_THREADING
  933. select MODULES_USE_ELF_RELA
  934. select NO_BOOTMEM
  935. select OF
  936. @@ -599,7 +601,7 @@
  937. config XEN
  938. bool "Xen guest support on ARM64"
  939. - depends on ARM64 && OF
  940. + depends on ARM64 && OF && !PREEMPT_RT_FULL
  941. select SWIOTLB_XEN
  942. help
  943. Say Y if you want to run Linux in a Virtual Machine on Xen on ARM64.
  944. diff -Nur linux-4.1.13.orig/arch/arm64/kernel/asm-offsets.c linux-4.1.13/arch/arm64/kernel/asm-offsets.c
  945. --- linux-4.1.13.orig/arch/arm64/kernel/asm-offsets.c 2015-11-09 23:34:10.000000000 +0100
  946. +++ linux-4.1.13/arch/arm64/kernel/asm-offsets.c 2015-11-29 09:23:09.505621090 +0100
  947. @@ -35,6 +35,7 @@
  948. BLANK();
  949. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  950. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  951. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  952. DEFINE(TI_ADDR_LIMIT, offsetof(struct thread_info, addr_limit));
  953. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  954. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  955. diff -Nur linux-4.1.13.orig/arch/arm64/kernel/debug-monitors.c linux-4.1.13/arch/arm64/kernel/debug-monitors.c
  956. --- linux-4.1.13.orig/arch/arm64/kernel/debug-monitors.c 2015-11-09 23:34:10.000000000 +0100
  957. +++ linux-4.1.13/arch/arm64/kernel/debug-monitors.c 2015-11-29 09:23:09.505621090 +0100
  958. @@ -271,20 +271,21 @@
  959. * Use reader/writer locks instead of plain spinlock.
  960. */
  961. static LIST_HEAD(break_hook);
  962. -static DEFINE_RWLOCK(break_hook_lock);
  963. +static DEFINE_SPINLOCK(break_hook_lock);
  964. void register_break_hook(struct break_hook *hook)
  965. {
  966. - write_lock(&break_hook_lock);
  967. - list_add(&hook->node, &break_hook);
  968. - write_unlock(&break_hook_lock);
  969. + spin_lock(&break_hook_lock);
  970. + list_add_rcu(&hook->node, &break_hook);
  971. + spin_unlock(&break_hook_lock);
  972. }
  973. void unregister_break_hook(struct break_hook *hook)
  974. {
  975. - write_lock(&break_hook_lock);
  976. - list_del(&hook->node);
  977. - write_unlock(&break_hook_lock);
  978. + spin_lock(&break_hook_lock);
  979. + list_del_rcu(&hook->node);
  980. + spin_unlock(&break_hook_lock);
  981. + synchronize_rcu();
  982. }
  983. static int call_break_hook(struct pt_regs *regs, unsigned int esr)
  984. @@ -292,11 +293,11 @@
  985. struct break_hook *hook;
  986. int (*fn)(struct pt_regs *regs, unsigned int esr) = NULL;
  987. - read_lock(&break_hook_lock);
  988. - list_for_each_entry(hook, &break_hook, node)
  989. + rcu_read_lock();
  990. + list_for_each_entry_rcu(hook, &break_hook, node)
  991. if ((esr & hook->esr_mask) == hook->esr_val)
  992. fn = hook->fn;
  993. - read_unlock(&break_hook_lock);
  994. + rcu_read_unlock();
  995. return fn ? fn(regs, esr) : DBG_HOOK_ERROR;
  996. }
  997. diff -Nur linux-4.1.13.orig/arch/arm64/kernel/entry.S linux-4.1.13/arch/arm64/kernel/entry.S
  998. --- linux-4.1.13.orig/arch/arm64/kernel/entry.S 2015-11-09 23:34:10.000000000 +0100
  999. +++ linux-4.1.13/arch/arm64/kernel/entry.S 2015-11-29 09:23:09.505621090 +0100
  1000. @@ -367,11 +367,16 @@
  1001. #ifdef CONFIG_PREEMPT
  1002. get_thread_info tsk
  1003. ldr w24, [tsk, #TI_PREEMPT] // get preempt count
  1004. - cbnz w24, 1f // preempt count != 0
  1005. + cbnz w24, 2f // preempt count != 0
  1006. ldr x0, [tsk, #TI_FLAGS] // get flags
  1007. - tbz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  1008. - bl el1_preempt
  1009. + tbnz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  1010. +
  1011. + ldr w24, [tsk, #TI_PREEMPT_LAZY] // get preempt lazy count
  1012. + cbnz w24, 2f // preempt lazy count != 0
  1013. + tbz x0, #TIF_NEED_RESCHED_LAZY, 2f // needs rescheduling?
  1014. 1:
  1015. + bl el1_preempt
  1016. +2:
  1017. #endif
  1018. #ifdef CONFIG_TRACE_IRQFLAGS
  1019. bl trace_hardirqs_on
  1020. @@ -385,6 +390,7 @@
  1021. 1: bl preempt_schedule_irq // irq en/disable is done inside
  1022. ldr x0, [tsk, #TI_FLAGS] // get new tasks TI_FLAGS
  1023. tbnz x0, #TIF_NEED_RESCHED, 1b // needs rescheduling?
  1024. + tbnz x0, #TIF_NEED_RESCHED_LAZY, 1b // needs rescheduling?
  1025. ret x24
  1026. #endif
  1027. @@ -622,6 +628,7 @@
  1028. str x0, [sp, #S_X0] // returned x0
  1029. work_pending:
  1030. tbnz x1, #TIF_NEED_RESCHED, work_resched
  1031. + tbnz x1, #TIF_NEED_RESCHED_LAZY, work_resched
  1032. /* TIF_SIGPENDING, TIF_NOTIFY_RESUME or TIF_FOREIGN_FPSTATE case */
  1033. ldr x2, [sp, #S_PSTATE]
  1034. mov x0, sp // 'regs'
  1035. diff -Nur linux-4.1.13.orig/arch/arm64/kernel/insn.c linux-4.1.13/arch/arm64/kernel/insn.c
  1036. --- linux-4.1.13.orig/arch/arm64/kernel/insn.c 2015-11-09 23:34:10.000000000 +0100
  1037. +++ linux-4.1.13/arch/arm64/kernel/insn.c 2015-11-29 09:23:09.505621090 +0100
  1038. @@ -77,7 +77,7 @@
  1039. }
  1040. }
  1041. -static DEFINE_SPINLOCK(patch_lock);
  1042. +static DEFINE_RAW_SPINLOCK(patch_lock);
  1043. static void __kprobes *patch_map(void *addr, int fixmap)
  1044. {
  1045. @@ -124,13 +124,13 @@
  1046. unsigned long flags = 0;
  1047. int ret;
  1048. - spin_lock_irqsave(&patch_lock, flags);
  1049. + raw_spin_lock_irqsave(&patch_lock, flags);
  1050. waddr = patch_map(addr, FIX_TEXT_POKE0);
  1051. ret = probe_kernel_write(waddr, &insn, AARCH64_INSN_SIZE);
  1052. patch_unmap(FIX_TEXT_POKE0);
  1053. - spin_unlock_irqrestore(&patch_lock, flags);
  1054. + raw_spin_unlock_irqrestore(&patch_lock, flags);
  1055. return ret;
  1056. }
  1057. diff -Nur linux-4.1.13.orig/arch/arm64/kernel/perf_event.c linux-4.1.13/arch/arm64/kernel/perf_event.c
  1058. --- linux-4.1.13.orig/arch/arm64/kernel/perf_event.c 2015-11-09 23:34:10.000000000 +0100
  1059. +++ linux-4.1.13/arch/arm64/kernel/perf_event.c 2015-11-29 09:23:09.505621090 +0100
  1060. @@ -488,7 +488,7 @@
  1061. }
  1062. err = request_irq(irq, armpmu->handle_irq,
  1063. - IRQF_NOBALANCING,
  1064. + IRQF_NOBALANCING | IRQF_NO_THREAD,
  1065. "arm-pmu", armpmu);
  1066. if (err) {
  1067. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  1068. diff -Nur linux-4.1.13.orig/arch/arm64/mm/fault.c linux-4.1.13/arch/arm64/mm/fault.c
  1069. --- linux-4.1.13.orig/arch/arm64/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  1070. +++ linux-4.1.13/arch/arm64/mm/fault.c 2015-11-29 09:23:09.505621090 +0100
  1071. @@ -211,7 +211,7 @@
  1072. * If we're in an interrupt or have no user context, we must not take
  1073. * the fault.
  1074. */
  1075. - if (in_atomic() || !mm)
  1076. + if (faulthandler_disabled() || !mm)
  1077. goto no_context;
  1078. if (user_mode(regs))
  1079. diff -Nur linux-4.1.13.orig/arch/avr32/include/asm/uaccess.h linux-4.1.13/arch/avr32/include/asm/uaccess.h
  1080. --- linux-4.1.13.orig/arch/avr32/include/asm/uaccess.h 2015-11-09 23:34:10.000000000 +0100
  1081. +++ linux-4.1.13/arch/avr32/include/asm/uaccess.h 2015-11-29 09:23:09.505621090 +0100
  1082. @@ -97,7 +97,8 @@
  1083. * @x: Value to copy to user space.
  1084. * @ptr: Destination address, in user space.
  1085. *
  1086. - * Context: User context only. This function may sleep.
  1087. + * Context: User context only. This function may sleep if pagefaults are
  1088. + * enabled.
  1089. *
  1090. * This macro copies a single simple value from kernel space to user
  1091. * space. It supports simple types like char and int, but not larger
  1092. @@ -116,7 +117,8 @@
  1093. * @x: Variable to store result.
  1094. * @ptr: Source address, in user space.
  1095. *
  1096. - * Context: User context only. This function may sleep.
  1097. + * Context: User context only. This function may sleep if pagefaults are
  1098. + * enabled.
  1099. *
  1100. * This macro copies a single simple variable from user space to kernel
  1101. * space. It supports simple types like char and int, but not larger
  1102. @@ -136,7 +138,8 @@
  1103. * @x: Value to copy to user space.
  1104. * @ptr: Destination address, in user space.
  1105. *
  1106. - * Context: User context only. This function may sleep.
  1107. + * Context: User context only. This function may sleep if pagefaults are
  1108. + * enabled.
  1109. *
  1110. * This macro copies a single simple value from kernel space to user
  1111. * space. It supports simple types like char and int, but not larger
  1112. @@ -158,7 +161,8 @@
  1113. * @x: Variable to store result.
  1114. * @ptr: Source address, in user space.
  1115. *
  1116. - * Context: User context only. This function may sleep.
  1117. + * Context: User context only. This function may sleep if pagefaults are
  1118. + * enabled.
  1119. *
  1120. * This macro copies a single simple variable from user space to kernel
  1121. * space. It supports simple types like char and int, but not larger
  1122. diff -Nur linux-4.1.13.orig/arch/avr32/mm/fault.c linux-4.1.13/arch/avr32/mm/fault.c
  1123. --- linux-4.1.13.orig/arch/avr32/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  1124. +++ linux-4.1.13/arch/avr32/mm/fault.c 2015-11-29 09:23:09.505621090 +0100
  1125. @@ -14,11 +14,11 @@
  1126. #include <linux/pagemap.h>
  1127. #include <linux/kdebug.h>
  1128. #include <linux/kprobes.h>
  1129. +#include <linux/uaccess.h>
  1130. #include <asm/mmu_context.h>
  1131. #include <asm/sysreg.h>
  1132. #include <asm/tlb.h>
  1133. -#include <asm/uaccess.h>
  1134. #ifdef CONFIG_KPROBES
  1135. static inline int notify_page_fault(struct pt_regs *regs, int trap)
  1136. @@ -81,7 +81,7 @@
  1137. * If we're in an interrupt or have no user context, we must
  1138. * not take the fault...
  1139. */
  1140. - if (in_atomic() || !mm || regs->sr & SYSREG_BIT(GM))
  1141. + if (faulthandler_disabled() || !mm || regs->sr & SYSREG_BIT(GM))
  1142. goto no_context;
  1143. local_irq_enable();
  1144. diff -Nur linux-4.1.13.orig/arch/cris/mm/fault.c linux-4.1.13/arch/cris/mm/fault.c
  1145. --- linux-4.1.13.orig/arch/cris/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  1146. +++ linux-4.1.13/arch/cris/mm/fault.c 2015-11-29 09:23:09.505621090 +0100
  1147. @@ -8,7 +8,7 @@
  1148. #include <linux/interrupt.h>
  1149. #include <linux/module.h>
  1150. #include <linux/wait.h>
  1151. -#include <asm/uaccess.h>
  1152. +#include <linux/uaccess.h>
  1153. #include <arch/system.h>
  1154. extern int find_fixup_code(struct pt_regs *);
  1155. @@ -109,11 +109,11 @@
  1156. info.si_code = SEGV_MAPERR;
  1157. /*
  1158. - * If we're in an interrupt or "atomic" operation or have no
  1159. + * If we're in an interrupt, have pagefaults disabled or have no
  1160. * user context, we must not take the fault.
  1161. */
  1162. - if (in_atomic() || !mm)
  1163. + if (faulthandler_disabled() || !mm)
  1164. goto no_context;
  1165. if (user_mode(regs))
  1166. diff -Nur linux-4.1.13.orig/arch/frv/mm/fault.c linux-4.1.13/arch/frv/mm/fault.c
  1167. --- linux-4.1.13.orig/arch/frv/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  1168. +++ linux-4.1.13/arch/frv/mm/fault.c 2015-11-29 09:23:09.505621090 +0100
  1169. @@ -19,9 +19,9 @@
  1170. #include <linux/kernel.h>
  1171. #include <linux/ptrace.h>
  1172. #include <linux/hardirq.h>
  1173. +#include <linux/uaccess.h>
  1174. #include <asm/pgtable.h>
  1175. -#include <asm/uaccess.h>
  1176. #include <asm/gdb-stub.h>
  1177. /*****************************************************************************/
  1178. @@ -78,7 +78,7 @@
  1179. * If we're in an interrupt or have no user
  1180. * context, we must not take the fault..
  1181. */
  1182. - if (in_atomic() || !mm)
  1183. + if (faulthandler_disabled() || !mm)
  1184. goto no_context;
  1185. if (user_mode(__frame))
  1186. diff -Nur linux-4.1.13.orig/arch/frv/mm/highmem.c linux-4.1.13/arch/frv/mm/highmem.c
  1187. --- linux-4.1.13.orig/arch/frv/mm/highmem.c 2015-11-09 23:34:10.000000000 +0100
  1188. +++ linux-4.1.13/arch/frv/mm/highmem.c 2015-11-29 09:23:09.505621090 +0100
  1189. @@ -42,6 +42,7 @@
  1190. unsigned long paddr;
  1191. int type;
  1192. + preempt_disable();
  1193. pagefault_disable();
  1194. type = kmap_atomic_idx_push();
  1195. paddr = page_to_phys(page);
  1196. @@ -85,5 +86,6 @@
  1197. }
  1198. kmap_atomic_idx_pop();
  1199. pagefault_enable();
  1200. + preempt_enable();
  1201. }
  1202. EXPORT_SYMBOL(__kunmap_atomic);
  1203. diff -Nur linux-4.1.13.orig/arch/hexagon/include/asm/uaccess.h linux-4.1.13/arch/hexagon/include/asm/uaccess.h
  1204. --- linux-4.1.13.orig/arch/hexagon/include/asm/uaccess.h 2015-11-09 23:34:10.000000000 +0100
  1205. +++ linux-4.1.13/arch/hexagon/include/asm/uaccess.h 2015-11-29 09:23:09.505621090 +0100
  1206. @@ -36,7 +36,8 @@
  1207. * @addr: User space pointer to start of block to check
  1208. * @size: Size of block to check
  1209. *
  1210. - * Context: User context only. This function may sleep.
  1211. + * Context: User context only. This function may sleep if pagefaults are
  1212. + * enabled.
  1213. *
  1214. * Checks if a pointer to a block of memory in user space is valid.
  1215. *
  1216. diff -Nur linux-4.1.13.orig/arch/ia64/mm/fault.c linux-4.1.13/arch/ia64/mm/fault.c
  1217. --- linux-4.1.13.orig/arch/ia64/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  1218. +++ linux-4.1.13/arch/ia64/mm/fault.c 2015-11-29 09:23:09.505621090 +0100
  1219. @@ -11,10 +11,10 @@
  1220. #include <linux/kprobes.h>
  1221. #include <linux/kdebug.h>
  1222. #include <linux/prefetch.h>
  1223. +#include <linux/uaccess.h>
  1224. #include <asm/pgtable.h>
  1225. #include <asm/processor.h>
  1226. -#include <asm/uaccess.h>
  1227. extern int die(char *, struct pt_regs *, long);
  1228. @@ -96,7 +96,7 @@
  1229. /*
  1230. * If we're in an interrupt or have no user context, we must not take the fault..
  1231. */
  1232. - if (in_atomic() || !mm)
  1233. + if (faulthandler_disabled() || !mm)
  1234. goto no_context;
  1235. #ifdef CONFIG_VIRTUAL_MEM_MAP
  1236. diff -Nur linux-4.1.13.orig/arch/Kconfig linux-4.1.13/arch/Kconfig
  1237. --- linux-4.1.13.orig/arch/Kconfig 2015-11-09 23:34:10.000000000 +0100
  1238. +++ linux-4.1.13/arch/Kconfig 2015-11-29 09:23:09.477622951 +0100
  1239. @@ -6,6 +6,7 @@
  1240. tristate "OProfile system profiling"
  1241. depends on PROFILING
  1242. depends on HAVE_OPROFILE
  1243. + depends on !PREEMPT_RT_FULL
  1244. select RING_BUFFER
  1245. select RING_BUFFER_ALLOW_SWAP
  1246. help
  1247. diff -Nur linux-4.1.13.orig/arch/m32r/include/asm/uaccess.h linux-4.1.13/arch/m32r/include/asm/uaccess.h
  1248. --- linux-4.1.13.orig/arch/m32r/include/asm/uaccess.h 2015-11-09 23:34:10.000000000 +0100
  1249. +++ linux-4.1.13/arch/m32r/include/asm/uaccess.h 2015-11-29 09:23:09.509620826 +0100
  1250. @@ -91,7 +91,8 @@
  1251. * @addr: User space pointer to start of block to check
  1252. * @size: Size of block to check
  1253. *
  1254. - * Context: User context only. This function may sleep.
  1255. + * Context: User context only. This function may sleep if pagefaults are
  1256. + * enabled.
  1257. *
  1258. * Checks if a pointer to a block of memory in user space is valid.
  1259. *
  1260. @@ -155,7 +156,8 @@
  1261. * @x: Variable to store result.
  1262. * @ptr: Source address, in user space.
  1263. *
  1264. - * Context: User context only. This function may sleep.
  1265. + * Context: User context only. This function may sleep if pagefaults are
  1266. + * enabled.
  1267. *
  1268. * This macro copies a single simple variable from user space to kernel
  1269. * space. It supports simple types like char and int, but not larger
  1270. @@ -175,7 +177,8 @@
  1271. * @x: Value to copy to user space.
  1272. * @ptr: Destination address, in user space.
  1273. *
  1274. - * Context: User context only. This function may sleep.
  1275. + * Context: User context only. This function may sleep if pagefaults are
  1276. + * enabled.
  1277. *
  1278. * This macro copies a single simple value from kernel space to user
  1279. * space. It supports simple types like char and int, but not larger
  1280. @@ -194,7 +197,8 @@
  1281. * @x: Variable to store result.
  1282. * @ptr: Source address, in user space.
  1283. *
  1284. - * Context: User context only. This function may sleep.
  1285. + * Context: User context only. This function may sleep if pagefaults are
  1286. + * enabled.
  1287. *
  1288. * This macro copies a single simple variable from user space to kernel
  1289. * space. It supports simple types like char and int, but not larger
  1290. @@ -274,7 +278,8 @@
  1291. * @x: Value to copy to user space.
  1292. * @ptr: Destination address, in user space.
  1293. *
  1294. - * Context: User context only. This function may sleep.
  1295. + * Context: User context only. This function may sleep if pagefaults are
  1296. + * enabled.
  1297. *
  1298. * This macro copies a single simple value from kernel space to user
  1299. * space. It supports simple types like char and int, but not larger
  1300. @@ -568,7 +573,8 @@
  1301. * @from: Source address, in kernel space.
  1302. * @n: Number of bytes to copy.
  1303. *
  1304. - * Context: User context only. This function may sleep.
  1305. + * Context: User context only. This function may sleep if pagefaults are
  1306. + * enabled.
  1307. *
  1308. * Copy data from kernel space to user space. Caller must check
  1309. * the specified block with access_ok() before calling this function.
  1310. @@ -588,7 +594,8 @@
  1311. * @from: Source address, in kernel space.
  1312. * @n: Number of bytes to copy.
  1313. *
  1314. - * Context: User context only. This function may sleep.
  1315. + * Context: User context only. This function may sleep if pagefaults are
  1316. + * enabled.
  1317. *
  1318. * Copy data from kernel space to user space.
  1319. *
  1320. @@ -606,7 +613,8 @@
  1321. * @from: Source address, in user space.
  1322. * @n: Number of bytes to copy.
  1323. *
  1324. - * Context: User context only. This function may sleep.
  1325. + * Context: User context only. This function may sleep if pagefaults are
  1326. + * enabled.
  1327. *
  1328. * Copy data from user space to kernel space. Caller must check
  1329. * the specified block with access_ok() before calling this function.
  1330. @@ -626,7 +634,8 @@
  1331. * @from: Source address, in user space.
  1332. * @n: Number of bytes to copy.
  1333. *
  1334. - * Context: User context only. This function may sleep.
  1335. + * Context: User context only. This function may sleep if pagefaults are
  1336. + * enabled.
  1337. *
  1338. * Copy data from user space to kernel space.
  1339. *
  1340. @@ -677,7 +686,8 @@
  1341. * strlen_user: - Get the size of a string in user space.
  1342. * @str: The string to measure.
  1343. *
  1344. - * Context: User context only. This function may sleep.
  1345. + * Context: User context only. This function may sleep if pagefaults are
  1346. + * enabled.
  1347. *
  1348. * Get the size of a NUL-terminated string in user space.
  1349. *
  1350. diff -Nur linux-4.1.13.orig/arch/m32r/mm/fault.c linux-4.1.13/arch/m32r/mm/fault.c
  1351. --- linux-4.1.13.orig/arch/m32r/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  1352. +++ linux-4.1.13/arch/m32r/mm/fault.c 2015-11-29 09:23:09.509620826 +0100
  1353. @@ -24,9 +24,9 @@
  1354. #include <linux/vt_kern.h> /* For unblank_screen() */
  1355. #include <linux/highmem.h>
  1356. #include <linux/module.h>
  1357. +#include <linux/uaccess.h>
  1358. #include <asm/m32r.h>
  1359. -#include <asm/uaccess.h>
  1360. #include <asm/hardirq.h>
  1361. #include <asm/mmu_context.h>
  1362. #include <asm/tlbflush.h>
  1363. @@ -111,10 +111,10 @@
  1364. mm = tsk->mm;
  1365. /*
  1366. - * If we're in an interrupt or have no user context or are running in an
  1367. - * atomic region then we must not take the fault..
  1368. + * If we're in an interrupt or have no user context or have pagefaults
  1369. + * disabled then we must not take the fault.
  1370. */
  1371. - if (in_atomic() || !mm)
  1372. + if (faulthandler_disabled() || !mm)
  1373. goto bad_area_nosemaphore;
  1374. if (error_code & ACE_USERMODE)
  1375. diff -Nur linux-4.1.13.orig/arch/m68k/mm/fault.c linux-4.1.13/arch/m68k/mm/fault.c
  1376. --- linux-4.1.13.orig/arch/m68k/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  1377. +++ linux-4.1.13/arch/m68k/mm/fault.c 2015-11-29 09:23:09.509620826 +0100
  1378. @@ -10,10 +10,10 @@
  1379. #include <linux/ptrace.h>
  1380. #include <linux/interrupt.h>
  1381. #include <linux/module.h>
  1382. +#include <linux/uaccess.h>
  1383. #include <asm/setup.h>
  1384. #include <asm/traps.h>
  1385. -#include <asm/uaccess.h>
  1386. #include <asm/pgalloc.h>
  1387. extern void die_if_kernel(char *, struct pt_regs *, long);
  1388. @@ -81,7 +81,7 @@
  1389. * If we're in an interrupt or have no user
  1390. * context, we must not take the fault..
  1391. */
  1392. - if (in_atomic() || !mm)
  1393. + if (faulthandler_disabled() || !mm)
  1394. goto no_context;
  1395. if (user_mode(regs))
  1396. diff -Nur linux-4.1.13.orig/arch/metag/mm/fault.c linux-4.1.13/arch/metag/mm/fault.c
  1397. --- linux-4.1.13.orig/arch/metag/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  1398. +++ linux-4.1.13/arch/metag/mm/fault.c 2015-11-29 09:23:09.509620826 +0100
  1399. @@ -105,7 +105,7 @@
  1400. mm = tsk->mm;
  1401. - if (in_atomic() || !mm)
  1402. + if (faulthandler_disabled() || !mm)
  1403. goto no_context;
  1404. if (user_mode(regs))
  1405. diff -Nur linux-4.1.13.orig/arch/metag/mm/highmem.c linux-4.1.13/arch/metag/mm/highmem.c
  1406. --- linux-4.1.13.orig/arch/metag/mm/highmem.c 2015-11-09 23:34:10.000000000 +0100
  1407. +++ linux-4.1.13/arch/metag/mm/highmem.c 2015-11-29 09:23:09.509620826 +0100
  1408. @@ -43,7 +43,7 @@
  1409. unsigned long vaddr;
  1410. int type;
  1411. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  1412. + preempt_disable();
  1413. pagefault_disable();
  1414. if (!PageHighMem(page))
  1415. return page_address(page);
  1416. @@ -82,6 +82,7 @@
  1417. }
  1418. pagefault_enable();
  1419. + preempt_enable();
  1420. }
  1421. EXPORT_SYMBOL(__kunmap_atomic);
  1422. @@ -95,6 +96,7 @@
  1423. unsigned long vaddr;
  1424. int type;
  1425. + preempt_disable();
  1426. pagefault_disable();
  1427. type = kmap_atomic_idx_push();
  1428. diff -Nur linux-4.1.13.orig/arch/microblaze/include/asm/uaccess.h linux-4.1.13/arch/microblaze/include/asm/uaccess.h
  1429. --- linux-4.1.13.orig/arch/microblaze/include/asm/uaccess.h 2015-11-09 23:34:10.000000000 +0100
  1430. +++ linux-4.1.13/arch/microblaze/include/asm/uaccess.h 2015-11-29 09:23:09.509620826 +0100
  1431. @@ -178,7 +178,8 @@
  1432. * @x: Variable to store result.
  1433. * @ptr: Source address, in user space.
  1434. *
  1435. - * Context: User context only. This function may sleep.
  1436. + * Context: User context only. This function may sleep if pagefaults are
  1437. + * enabled.
  1438. *
  1439. * This macro copies a single simple variable from user space to kernel
  1440. * space. It supports simple types like char and int, but not larger
  1441. @@ -290,7 +291,8 @@
  1442. * @x: Value to copy to user space.
  1443. * @ptr: Destination address, in user space.
  1444. *
  1445. - * Context: User context only. This function may sleep.
  1446. + * Context: User context only. This function may sleep if pagefaults are
  1447. + * enabled.
  1448. *
  1449. * This macro copies a single simple value from kernel space to user
  1450. * space. It supports simple types like char and int, but not larger
  1451. diff -Nur linux-4.1.13.orig/arch/microblaze/mm/fault.c linux-4.1.13/arch/microblaze/mm/fault.c
  1452. --- linux-4.1.13.orig/arch/microblaze/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  1453. +++ linux-4.1.13/arch/microblaze/mm/fault.c 2015-11-29 09:23:09.509620826 +0100
  1454. @@ -107,14 +107,14 @@
  1455. if ((error_code & 0x13) == 0x13 || (error_code & 0x11) == 0x11)
  1456. is_write = 0;
  1457. - if (unlikely(in_atomic() || !mm)) {
  1458. + if (unlikely(faulthandler_disabled() || !mm)) {
  1459. if (kernel_mode(regs))
  1460. goto bad_area_nosemaphore;
  1461. - /* in_atomic() in user mode is really bad,
  1462. + /* faulthandler_disabled() in user mode is really bad,
  1463. as is current->mm == NULL. */
  1464. - pr_emerg("Page fault in user mode with in_atomic(), mm = %p\n",
  1465. - mm);
  1466. + pr_emerg("Page fault in user mode with faulthandler_disabled(), mm = %p\n",
  1467. + mm);
  1468. pr_emerg("r15 = %lx MSR = %lx\n",
  1469. regs->r15, regs->msr);
  1470. die("Weird page fault", regs, SIGSEGV);
  1471. diff -Nur linux-4.1.13.orig/arch/microblaze/mm/highmem.c linux-4.1.13/arch/microblaze/mm/highmem.c
  1472. --- linux-4.1.13.orig/arch/microblaze/mm/highmem.c 2015-11-09 23:34:10.000000000 +0100
  1473. +++ linux-4.1.13/arch/microblaze/mm/highmem.c 2015-11-29 09:23:09.509620826 +0100
  1474. @@ -37,7 +37,7 @@
  1475. unsigned long vaddr;
  1476. int idx, type;
  1477. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  1478. + preempt_disable();
  1479. pagefault_disable();
  1480. if (!PageHighMem(page))
  1481. return page_address(page);
  1482. @@ -63,6 +63,7 @@
  1483. if (vaddr < __fix_to_virt(FIX_KMAP_END)) {
  1484. pagefault_enable();
  1485. + preempt_enable();
  1486. return;
  1487. }
  1488. @@ -84,5 +85,6 @@
  1489. #endif
  1490. kmap_atomic_idx_pop();
  1491. pagefault_enable();
  1492. + preempt_enable();
  1493. }
  1494. EXPORT_SYMBOL(__kunmap_atomic);
  1495. diff -Nur linux-4.1.13.orig/arch/mips/include/asm/uaccess.h linux-4.1.13/arch/mips/include/asm/uaccess.h
  1496. --- linux-4.1.13.orig/arch/mips/include/asm/uaccess.h 2015-11-09 23:34:10.000000000 +0100
  1497. +++ linux-4.1.13/arch/mips/include/asm/uaccess.h 2015-11-29 09:23:09.513620562 +0100
  1498. @@ -103,7 +103,8 @@
  1499. * @addr: User space pointer to start of block to check
  1500. * @size: Size of block to check
  1501. *
  1502. - * Context: User context only. This function may sleep.
  1503. + * Context: User context only. This function may sleep if pagefaults are
  1504. + * enabled.
  1505. *
  1506. * Checks if a pointer to a block of memory in user space is valid.
  1507. *
  1508. @@ -138,7 +139,8 @@
  1509. * @x: Value to copy to user space.
  1510. * @ptr: Destination address, in user space.
  1511. *
  1512. - * Context: User context only. This function may sleep.
  1513. + * Context: User context only. This function may sleep if pagefaults are
  1514. + * enabled.
  1515. *
  1516. * This macro copies a single simple value from kernel space to user
  1517. * space. It supports simple types like char and int, but not larger
  1518. @@ -157,7 +159,8 @@
  1519. * @x: Variable to store result.
  1520. * @ptr: Source address, in user space.
  1521. *
  1522. - * Context: User context only. This function may sleep.
  1523. + * Context: User context only. This function may sleep if pagefaults are
  1524. + * enabled.
  1525. *
  1526. * This macro copies a single simple variable from user space to kernel
  1527. * space. It supports simple types like char and int, but not larger
  1528. @@ -177,7 +180,8 @@
  1529. * @x: Value to copy to user space.
  1530. * @ptr: Destination address, in user space.
  1531. *
  1532. - * Context: User context only. This function may sleep.
  1533. + * Context: User context only. This function may sleep if pagefaults are
  1534. + * enabled.
  1535. *
  1536. * This macro copies a single simple value from kernel space to user
  1537. * space. It supports simple types like char and int, but not larger
  1538. @@ -199,7 +203,8 @@
  1539. * @x: Variable to store result.
  1540. * @ptr: Source address, in user space.
  1541. *
  1542. - * Context: User context only. This function may sleep.
  1543. + * Context: User context only. This function may sleep if pagefaults are
  1544. + * enabled.
  1545. *
  1546. * This macro copies a single simple variable from user space to kernel
  1547. * space. It supports simple types like char and int, but not larger
  1548. @@ -498,7 +503,8 @@
  1549. * @x: Value to copy to user space.
  1550. * @ptr: Destination address, in user space.
  1551. *
  1552. - * Context: User context only. This function may sleep.
  1553. + * Context: User context only. This function may sleep if pagefaults are
  1554. + * enabled.
  1555. *
  1556. * This macro copies a single simple value from kernel space to user
  1557. * space. It supports simple types like char and int, but not larger
  1558. @@ -517,7 +523,8 @@
  1559. * @x: Variable to store result.
  1560. * @ptr: Source address, in user space.
  1561. *
  1562. - * Context: User context only. This function may sleep.
  1563. + * Context: User context only. This function may sleep if pagefaults are
  1564. + * enabled.
  1565. *
  1566. * This macro copies a single simple variable from user space to kernel
  1567. * space. It supports simple types like char and int, but not larger
  1568. @@ -537,7 +544,8 @@
  1569. * @x: Value to copy to user space.
  1570. * @ptr: Destination address, in user space.
  1571. *
  1572. - * Context: User context only. This function may sleep.
  1573. + * Context: User context only. This function may sleep if pagefaults are
  1574. + * enabled.
  1575. *
  1576. * This macro copies a single simple value from kernel space to user
  1577. * space. It supports simple types like char and int, but not larger
  1578. @@ -559,7 +567,8 @@
  1579. * @x: Variable to store result.
  1580. * @ptr: Source address, in user space.
  1581. *
  1582. - * Context: User context only. This function may sleep.
  1583. + * Context: User context only. This function may sleep if pagefaults are
  1584. + * enabled.
  1585. *
  1586. * This macro copies a single simple variable from user space to kernel
  1587. * space. It supports simple types like char and int, but not larger
  1588. @@ -815,7 +824,8 @@
  1589. * @from: Source address, in kernel space.
  1590. * @n: Number of bytes to copy.
  1591. *
  1592. - * Context: User context only. This function may sleep.
  1593. + * Context: User context only. This function may sleep if pagefaults are
  1594. + * enabled.
  1595. *
  1596. * Copy data from kernel space to user space. Caller must check
  1597. * the specified block with access_ok() before calling this function.
  1598. @@ -888,7 +898,8 @@
  1599. * @from: Source address, in kernel space.
  1600. * @n: Number of bytes to copy.
  1601. *
  1602. - * Context: User context only. This function may sleep.
  1603. + * Context: User context only. This function may sleep if pagefaults are
  1604. + * enabled.
  1605. *
  1606. * Copy data from kernel space to user space.
  1607. *
  1608. @@ -1075,7 +1086,8 @@
  1609. * @from: Source address, in user space.
  1610. * @n: Number of bytes to copy.
  1611. *
  1612. - * Context: User context only. This function may sleep.
  1613. + * Context: User context only. This function may sleep if pagefaults are
  1614. + * enabled.
  1615. *
  1616. * Copy data from user space to kernel space. Caller must check
  1617. * the specified block with access_ok() before calling this function.
  1618. @@ -1107,7 +1119,8 @@
  1619. * @from: Source address, in user space.
  1620. * @n: Number of bytes to copy.
  1621. *
  1622. - * Context: User context only. This function may sleep.
  1623. + * Context: User context only. This function may sleep if pagefaults are
  1624. + * enabled.
  1625. *
  1626. * Copy data from user space to kernel space.
  1627. *
  1628. @@ -1329,7 +1342,8 @@
  1629. * strlen_user: - Get the size of a string in user space.
  1630. * @str: The string to measure.
  1631. *
  1632. - * Context: User context only. This function may sleep.
  1633. + * Context: User context only. This function may sleep if pagefaults are
  1634. + * enabled.
  1635. *
  1636. * Get the size of a NUL-terminated string in user space.
  1637. *
  1638. @@ -1398,7 +1412,8 @@
  1639. * strnlen_user: - Get the size of a string in user space.
  1640. * @str: The string to measure.
  1641. *
  1642. - * Context: User context only. This function may sleep.
  1643. + * Context: User context only. This function may sleep if pagefaults are
  1644. + * enabled.
  1645. *
  1646. * Get the size of a NUL-terminated string in user space.
  1647. *
  1648. diff -Nur linux-4.1.13.orig/arch/mips/Kconfig linux-4.1.13/arch/mips/Kconfig
  1649. --- linux-4.1.13.orig/arch/mips/Kconfig 2015-11-09 23:34:10.000000000 +0100
  1650. +++ linux-4.1.13/arch/mips/Kconfig 2015-11-29 09:23:09.513620562 +0100
  1651. @@ -2366,7 +2366,7 @@
  1652. #
  1653. config HIGHMEM
  1654. bool "High Memory Support"
  1655. - depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA
  1656. + depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA && !PREEMPT_RT_FULL
  1657. config CPU_SUPPORTS_HIGHMEM
  1658. bool
  1659. diff -Nur linux-4.1.13.orig/arch/mips/kernel/signal-common.h linux-4.1.13/arch/mips/kernel/signal-common.h
  1660. --- linux-4.1.13.orig/arch/mips/kernel/signal-common.h 2015-11-09 23:34:10.000000000 +0100
  1661. +++ linux-4.1.13/arch/mips/kernel/signal-common.h 2015-11-29 09:23:09.513620562 +0100
  1662. @@ -28,12 +28,7 @@
  1663. extern int fpcsr_pending(unsigned int __user *fpcsr);
  1664. /* Make sure we will not lose FPU ownership */
  1665. -#ifdef CONFIG_PREEMPT
  1666. -#define lock_fpu_owner() preempt_disable()
  1667. -#define unlock_fpu_owner() preempt_enable()
  1668. -#else
  1669. -#define lock_fpu_owner() pagefault_disable()
  1670. -#define unlock_fpu_owner() pagefault_enable()
  1671. -#endif
  1672. +#define lock_fpu_owner() ({ preempt_disable(); pagefault_disable(); })
  1673. +#define unlock_fpu_owner() ({ pagefault_enable(); preempt_enable(); })
  1674. #endif /* __SIGNAL_COMMON_H */
  1675. diff -Nur linux-4.1.13.orig/arch/mips/mm/fault.c linux-4.1.13/arch/mips/mm/fault.c
  1676. --- linux-4.1.13.orig/arch/mips/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  1677. +++ linux-4.1.13/arch/mips/mm/fault.c 2015-11-29 09:23:09.513620562 +0100
  1678. @@ -21,10 +21,10 @@
  1679. #include <linux/module.h>
  1680. #include <linux/kprobes.h>
  1681. #include <linux/perf_event.h>
  1682. +#include <linux/uaccess.h>
  1683. #include <asm/branch.h>
  1684. #include <asm/mmu_context.h>
  1685. -#include <asm/uaccess.h>
  1686. #include <asm/ptrace.h>
  1687. #include <asm/highmem.h> /* For VMALLOC_END */
  1688. #include <linux/kdebug.h>
  1689. @@ -94,7 +94,7 @@
  1690. * If we're in an interrupt or have no user
  1691. * context, we must not take the fault..
  1692. */
  1693. - if (in_atomic() || !mm)
  1694. + if (faulthandler_disabled() || !mm)
  1695. goto bad_area_nosemaphore;
  1696. if (user_mode(regs))
  1697. diff -Nur linux-4.1.13.orig/arch/mips/mm/highmem.c linux-4.1.13/arch/mips/mm/highmem.c
  1698. --- linux-4.1.13.orig/arch/mips/mm/highmem.c 2015-11-09 23:34:10.000000000 +0100
  1699. +++ linux-4.1.13/arch/mips/mm/highmem.c 2015-11-29 09:23:09.513620562 +0100
  1700. @@ -47,7 +47,7 @@
  1701. unsigned long vaddr;
  1702. int idx, type;
  1703. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  1704. + preempt_disable();
  1705. pagefault_disable();
  1706. if (!PageHighMem(page))
  1707. return page_address(page);
  1708. @@ -72,6 +72,7 @@
  1709. if (vaddr < FIXADDR_START) { // FIXME
  1710. pagefault_enable();
  1711. + preempt_enable();
  1712. return;
  1713. }
  1714. @@ -92,6 +93,7 @@
  1715. #endif
  1716. kmap_atomic_idx_pop();
  1717. pagefault_enable();
  1718. + preempt_enable();
  1719. }
  1720. EXPORT_SYMBOL(__kunmap_atomic);
  1721. @@ -104,6 +106,7 @@
  1722. unsigned long vaddr;
  1723. int idx, type;
  1724. + preempt_disable();
  1725. pagefault_disable();
  1726. type = kmap_atomic_idx_push();
  1727. diff -Nur linux-4.1.13.orig/arch/mips/mm/init.c linux-4.1.13/arch/mips/mm/init.c
  1728. --- linux-4.1.13.orig/arch/mips/mm/init.c 2015-11-09 23:34:10.000000000 +0100
  1729. +++ linux-4.1.13/arch/mips/mm/init.c 2015-11-29 09:23:09.513620562 +0100
  1730. @@ -90,6 +90,7 @@
  1731. BUG_ON(Page_dcache_dirty(page));
  1732. + preempt_disable();
  1733. pagefault_disable();
  1734. idx = (addr >> PAGE_SHIFT) & (FIX_N_COLOURS - 1);
  1735. idx += in_interrupt() ? FIX_N_COLOURS : 0;
  1736. @@ -152,6 +153,7 @@
  1737. write_c0_entryhi(old_ctx);
  1738. local_irq_restore(flags);
  1739. pagefault_enable();
  1740. + preempt_enable();
  1741. }
  1742. void copy_user_highpage(struct page *to, struct page *from,
  1743. diff -Nur linux-4.1.13.orig/arch/mn10300/include/asm/highmem.h linux-4.1.13/arch/mn10300/include/asm/highmem.h
  1744. --- linux-4.1.13.orig/arch/mn10300/include/asm/highmem.h 2015-11-09 23:34:10.000000000 +0100
  1745. +++ linux-4.1.13/arch/mn10300/include/asm/highmem.h 2015-11-29 09:23:09.513620562 +0100
  1746. @@ -75,6 +75,7 @@
  1747. unsigned long vaddr;
  1748. int idx, type;
  1749. + preempt_disable();
  1750. pagefault_disable();
  1751. if (page < highmem_start_page)
  1752. return page_address(page);
  1753. @@ -98,6 +99,7 @@
  1754. if (vaddr < FIXADDR_START) { /* FIXME */
  1755. pagefault_enable();
  1756. + preempt_enable();
  1757. return;
  1758. }
  1759. @@ -122,6 +124,7 @@
  1760. kmap_atomic_idx_pop();
  1761. pagefault_enable();
  1762. + preempt_enable();
  1763. }
  1764. #endif /* __KERNEL__ */
  1765. diff -Nur linux-4.1.13.orig/arch/mn10300/mm/fault.c linux-4.1.13/arch/mn10300/mm/fault.c
  1766. --- linux-4.1.13.orig/arch/mn10300/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  1767. +++ linux-4.1.13/arch/mn10300/mm/fault.c 2015-11-29 09:23:09.513620562 +0100
  1768. @@ -23,8 +23,8 @@
  1769. #include <linux/interrupt.h>
  1770. #include <linux/init.h>
  1771. #include <linux/vt_kern.h> /* For unblank_screen() */
  1772. +#include <linux/uaccess.h>
  1773. -#include <asm/uaccess.h>
  1774. #include <asm/pgalloc.h>
  1775. #include <asm/hardirq.h>
  1776. #include <asm/cpu-regs.h>
  1777. @@ -168,7 +168,7 @@
  1778. * If we're in an interrupt or have no user
  1779. * context, we must not take the fault..
  1780. */
  1781. - if (in_atomic() || !mm)
  1782. + if (faulthandler_disabled() || !mm)
  1783. goto no_context;
  1784. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR)
  1785. diff -Nur linux-4.1.13.orig/arch/nios2/mm/fault.c linux-4.1.13/arch/nios2/mm/fault.c
  1786. --- linux-4.1.13.orig/arch/nios2/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  1787. +++ linux-4.1.13/arch/nios2/mm/fault.c 2015-11-29 09:23:09.513620562 +0100
  1788. @@ -77,7 +77,7 @@
  1789. * If we're in an interrupt or have no user
  1790. * context, we must not take the fault..
  1791. */
  1792. - if (in_atomic() || !mm)
  1793. + if (faulthandler_disabled() || !mm)
  1794. goto bad_area_nosemaphore;
  1795. if (user_mode(regs))
  1796. diff -Nur linux-4.1.13.orig/arch/parisc/include/asm/cacheflush.h linux-4.1.13/arch/parisc/include/asm/cacheflush.h
  1797. --- linux-4.1.13.orig/arch/parisc/include/asm/cacheflush.h 2015-11-09 23:34:10.000000000 +0100
  1798. +++ linux-4.1.13/arch/parisc/include/asm/cacheflush.h 2015-11-29 09:23:09.513620562 +0100
  1799. @@ -142,6 +142,7 @@
  1800. static inline void *kmap_atomic(struct page *page)
  1801. {
  1802. + preempt_disable();
  1803. pagefault_disable();
  1804. return page_address(page);
  1805. }
  1806. @@ -150,6 +151,7 @@
  1807. {
  1808. flush_kernel_dcache_page_addr(addr);
  1809. pagefault_enable();
  1810. + preempt_enable();
  1811. }
  1812. #define kmap_atomic_prot(page, prot) kmap_atomic(page)
  1813. diff -Nur linux-4.1.13.orig/arch/parisc/kernel/traps.c linux-4.1.13/arch/parisc/kernel/traps.c
  1814. --- linux-4.1.13.orig/arch/parisc/kernel/traps.c 2015-11-09 23:34:10.000000000 +0100
  1815. +++ linux-4.1.13/arch/parisc/kernel/traps.c 2015-11-29 09:23:09.513620562 +0100
  1816. @@ -26,9 +26,9 @@
  1817. #include <linux/console.h>
  1818. #include <linux/bug.h>
  1819. #include <linux/ratelimit.h>
  1820. +#include <linux/uaccess.h>
  1821. #include <asm/assembly.h>
  1822. -#include <asm/uaccess.h>
  1823. #include <asm/io.h>
  1824. #include <asm/irq.h>
  1825. #include <asm/traps.h>
  1826. @@ -796,7 +796,7 @@
  1827. * unless pagefault_disable() was called before.
  1828. */
  1829. - if (fault_space == 0 && !in_atomic())
  1830. + if (fault_space == 0 && !faulthandler_disabled())
  1831. {
  1832. pdc_chassis_send_status(PDC_CHASSIS_DIRECT_PANIC);
  1833. parisc_terminate("Kernel Fault", regs, code, fault_address);
  1834. diff -Nur linux-4.1.13.orig/arch/parisc/mm/fault.c linux-4.1.13/arch/parisc/mm/fault.c
  1835. --- linux-4.1.13.orig/arch/parisc/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  1836. +++ linux-4.1.13/arch/parisc/mm/fault.c 2015-11-29 09:23:09.513620562 +0100
  1837. @@ -15,8 +15,8 @@
  1838. #include <linux/sched.h>
  1839. #include <linux/interrupt.h>
  1840. #include <linux/module.h>
  1841. +#include <linux/uaccess.h>
  1842. -#include <asm/uaccess.h>
  1843. #include <asm/traps.h>
  1844. /* Various important other fields */
  1845. @@ -207,7 +207,7 @@
  1846. int fault;
  1847. unsigned int flags;
  1848. - if (in_atomic())
  1849. + if (pagefault_disabled())
  1850. goto no_context;
  1851. tsk = current;
  1852. diff -Nur linux-4.1.13.orig/arch/powerpc/include/asm/kvm_host.h linux-4.1.13/arch/powerpc/include/asm/kvm_host.h
  1853. --- linux-4.1.13.orig/arch/powerpc/include/asm/kvm_host.h 2015-11-09 23:34:10.000000000 +0100
  1854. +++ linux-4.1.13/arch/powerpc/include/asm/kvm_host.h 2015-11-29 09:23:09.517620297 +0100
  1855. @@ -280,7 +280,7 @@
  1856. u8 in_guest;
  1857. struct list_head runnable_threads;
  1858. spinlock_t lock;
  1859. - wait_queue_head_t wq;
  1860. + struct swait_head wq;
  1861. spinlock_t stoltb_lock; /* protects stolen_tb and preempt_tb */
  1862. u64 stolen_tb;
  1863. u64 preempt_tb;
  1864. @@ -613,7 +613,7 @@
  1865. u8 prodded;
  1866. u32 last_inst;
  1867. - wait_queue_head_t *wqp;
  1868. + struct swait_head *wqp;
  1869. struct kvmppc_vcore *vcore;
  1870. int ret;
  1871. int trap;
  1872. diff -Nur linux-4.1.13.orig/arch/powerpc/include/asm/thread_info.h linux-4.1.13/arch/powerpc/include/asm/thread_info.h
  1873. --- linux-4.1.13.orig/arch/powerpc/include/asm/thread_info.h 2015-11-09 23:34:10.000000000 +0100
  1874. +++ linux-4.1.13/arch/powerpc/include/asm/thread_info.h 2015-11-29 09:23:09.517620297 +0100
  1875. @@ -42,6 +42,8 @@
  1876. int cpu; /* cpu we're on */
  1877. int preempt_count; /* 0 => preemptable,
  1878. <0 => BUG */
  1879. + int preempt_lazy_count; /* 0 => preemptable,
  1880. + <0 => BUG */
  1881. unsigned long local_flags; /* private flags for thread */
  1882. /* low level flags - has atomic operations done on it */
  1883. @@ -82,8 +84,7 @@
  1884. #define TIF_SYSCALL_TRACE 0 /* syscall trace active */
  1885. #define TIF_SIGPENDING 1 /* signal pending */
  1886. #define TIF_NEED_RESCHED 2 /* rescheduling necessary */
  1887. -#define TIF_POLLING_NRFLAG 3 /* true if poll_idle() is polling
  1888. - TIF_NEED_RESCHED */
  1889. +#define TIF_NEED_RESCHED_LAZY 3 /* lazy rescheduling necessary */
  1890. #define TIF_32BIT 4 /* 32 bit binary */
  1891. #define TIF_RESTORE_TM 5 /* need to restore TM FP/VEC/VSX */
  1892. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  1893. @@ -101,6 +102,8 @@
  1894. #if defined(CONFIG_PPC64)
  1895. #define TIF_ELF2ABI 18 /* function descriptors must die! */
  1896. #endif
  1897. +#define TIF_POLLING_NRFLAG 19 /* true if poll_idle() is polling
  1898. + TIF_NEED_RESCHED */
  1899. /* as above, but as bit values */
  1900. #define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
  1901. @@ -119,14 +122,16 @@
  1902. #define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
  1903. #define _TIF_EMULATE_STACK_STORE (1<<TIF_EMULATE_STACK_STORE)
  1904. #define _TIF_NOHZ (1<<TIF_NOHZ)
  1905. +#define _TIF_NEED_RESCHED_LAZY (1<<TIF_NEED_RESCHED_LAZY)
  1906. #define _TIF_SYSCALL_DOTRACE (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  1907. _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT | \
  1908. _TIF_NOHZ)
  1909. #define _TIF_USER_WORK_MASK (_TIF_SIGPENDING | _TIF_NEED_RESCHED | \
  1910. _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  1911. - _TIF_RESTORE_TM)
  1912. + _TIF_RESTORE_TM | _TIF_NEED_RESCHED_LAZY)
  1913. #define _TIF_PERSYSCALL_MASK (_TIF_RESTOREALL|_TIF_NOERROR)
  1914. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  1915. /* Bits in local_flags */
  1916. /* Don't move TLF_NAPPING without adjusting the code in entry_32.S */
  1917. diff -Nur linux-4.1.13.orig/arch/powerpc/Kconfig linux-4.1.13/arch/powerpc/Kconfig
  1918. --- linux-4.1.13.orig/arch/powerpc/Kconfig 2015-11-09 23:34:10.000000000 +0100
  1919. +++ linux-4.1.13/arch/powerpc/Kconfig 2015-11-29 09:23:09.513620562 +0100
  1920. @@ -60,10 +60,11 @@
  1921. config RWSEM_GENERIC_SPINLOCK
  1922. bool
  1923. + default y if PREEMPT_RT_FULL
  1924. config RWSEM_XCHGADD_ALGORITHM
  1925. bool
  1926. - default y
  1927. + default y if !PREEMPT_RT_FULL
  1928. config GENERIC_LOCKBREAK
  1929. bool
  1930. @@ -138,6 +139,7 @@
  1931. select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
  1932. select GENERIC_STRNCPY_FROM_USER
  1933. select GENERIC_STRNLEN_USER
  1934. + select HAVE_PREEMPT_LAZY
  1935. select HAVE_MOD_ARCH_SPECIFIC
  1936. select MODULES_USE_ELF_RELA
  1937. select CLONE_BACKWARDS
  1938. @@ -312,7 +314,7 @@
  1939. config HIGHMEM
  1940. bool "High memory support"
  1941. - depends on PPC32
  1942. + depends on PPC32 && !PREEMPT_RT_FULL
  1943. source kernel/Kconfig.hz
  1944. source kernel/Kconfig.preempt
  1945. diff -Nur linux-4.1.13.orig/arch/powerpc/kernel/asm-offsets.c linux-4.1.13/arch/powerpc/kernel/asm-offsets.c
  1946. --- linux-4.1.13.orig/arch/powerpc/kernel/asm-offsets.c 2015-11-09 23:34:10.000000000 +0100
  1947. +++ linux-4.1.13/arch/powerpc/kernel/asm-offsets.c 2015-11-29 09:23:09.517620297 +0100
  1948. @@ -160,6 +160,7 @@
  1949. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  1950. DEFINE(TI_LOCAL_FLAGS, offsetof(struct thread_info, local_flags));
  1951. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  1952. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  1953. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  1954. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  1955. diff -Nur linux-4.1.13.orig/arch/powerpc/kernel/entry_32.S linux-4.1.13/arch/powerpc/kernel/entry_32.S
  1956. --- linux-4.1.13.orig/arch/powerpc/kernel/entry_32.S 2015-11-09 23:34:10.000000000 +0100
  1957. +++ linux-4.1.13/arch/powerpc/kernel/entry_32.S 2015-11-29 09:23:09.517620297 +0100
  1958. @@ -813,7 +813,14 @@
  1959. cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  1960. bne restore
  1961. andi. r8,r8,_TIF_NEED_RESCHED
  1962. + bne+ 1f
  1963. + lwz r0,TI_PREEMPT_LAZY(r9)
  1964. + cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  1965. + bne restore
  1966. + lwz r0,TI_FLAGS(r9)
  1967. + andi. r0,r0,_TIF_NEED_RESCHED_LAZY
  1968. beq+ restore
  1969. +1:
  1970. lwz r3,_MSR(r1)
  1971. andi. r0,r3,MSR_EE /* interrupts off? */
  1972. beq restore /* don't schedule if so */
  1973. @@ -824,11 +831,11 @@
  1974. */
  1975. bl trace_hardirqs_off
  1976. #endif
  1977. -1: bl preempt_schedule_irq
  1978. +2: bl preempt_schedule_irq
  1979. CURRENT_THREAD_INFO(r9, r1)
  1980. lwz r3,TI_FLAGS(r9)
  1981. - andi. r0,r3,_TIF_NEED_RESCHED
  1982. - bne- 1b
  1983. + andi. r0,r3,_TIF_NEED_RESCHED_MASK
  1984. + bne- 2b
  1985. #ifdef CONFIG_TRACE_IRQFLAGS
  1986. /* And now, to properly rebalance the above, we tell lockdep they
  1987. * are being turned back on, which will happen when we return
  1988. @@ -1149,7 +1156,7 @@
  1989. #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
  1990. do_work: /* r10 contains MSR_KERNEL here */
  1991. - andi. r0,r9,_TIF_NEED_RESCHED
  1992. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  1993. beq do_user_signal
  1994. do_resched: /* r10 contains MSR_KERNEL here */
  1995. @@ -1170,7 +1177,7 @@
  1996. MTMSRD(r10) /* disable interrupts */
  1997. CURRENT_THREAD_INFO(r9, r1)
  1998. lwz r9,TI_FLAGS(r9)
  1999. - andi. r0,r9,_TIF_NEED_RESCHED
  2000. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  2001. bne- do_resched
  2002. andi. r0,r9,_TIF_USER_WORK_MASK
  2003. beq restore_user
  2004. diff -Nur linux-4.1.13.orig/arch/powerpc/kernel/entry_64.S linux-4.1.13/arch/powerpc/kernel/entry_64.S
  2005. --- linux-4.1.13.orig/arch/powerpc/kernel/entry_64.S 2015-11-09 23:34:10.000000000 +0100
  2006. +++ linux-4.1.13/arch/powerpc/kernel/entry_64.S 2015-11-29 09:23:09.517620297 +0100
  2007. @@ -636,7 +636,7 @@
  2008. #else
  2009. beq restore
  2010. #endif
  2011. -1: andi. r0,r4,_TIF_NEED_RESCHED
  2012. +1: andi. r0,r4,_TIF_NEED_RESCHED_MASK
  2013. beq 2f
  2014. bl restore_interrupts
  2015. SCHEDULE_USER
  2016. @@ -698,10 +698,18 @@
  2017. #ifdef CONFIG_PREEMPT
  2018. /* Check if we need to preempt */
  2019. + lwz r8,TI_PREEMPT(r9)
  2020. + cmpwi 0,r8,0 /* if non-zero, just restore regs and return */
  2021. + bne restore
  2022. andi. r0,r4,_TIF_NEED_RESCHED
  2023. + bne+ check_count
  2024. +
  2025. + andi. r0,r4,_TIF_NEED_RESCHED_LAZY
  2026. beq+ restore
  2027. + lwz r8,TI_PREEMPT_LAZY(r9)
  2028. +
  2029. /* Check that preempt_count() == 0 and interrupts are enabled */
  2030. - lwz r8,TI_PREEMPT(r9)
  2031. +check_count:
  2032. cmpwi cr1,r8,0
  2033. ld r0,SOFTE(r1)
  2034. cmpdi r0,0
  2035. @@ -718,7 +726,7 @@
  2036. /* Re-test flags and eventually loop */
  2037. CURRENT_THREAD_INFO(r9, r1)
  2038. ld r4,TI_FLAGS(r9)
  2039. - andi. r0,r4,_TIF_NEED_RESCHED
  2040. + andi. r0,r4,_TIF_NEED_RESCHED_MASK
  2041. bne 1b
  2042. /*
  2043. diff -Nur linux-4.1.13.orig/arch/powerpc/kernel/irq.c linux-4.1.13/arch/powerpc/kernel/irq.c
  2044. --- linux-4.1.13.orig/arch/powerpc/kernel/irq.c 2015-11-09 23:34:10.000000000 +0100
  2045. +++ linux-4.1.13/arch/powerpc/kernel/irq.c 2015-11-29 09:23:09.517620297 +0100
  2046. @@ -614,6 +614,7 @@
  2047. }
  2048. }
  2049. +#ifndef CONFIG_PREEMPT_RT_FULL
  2050. void do_softirq_own_stack(void)
  2051. {
  2052. struct thread_info *curtp, *irqtp;
  2053. @@ -631,6 +632,7 @@
  2054. if (irqtp->flags)
  2055. set_bits(irqtp->flags, &curtp->flags);
  2056. }
  2057. +#endif
  2058. irq_hw_number_t virq_to_hw(unsigned int virq)
  2059. {
  2060. diff -Nur linux-4.1.13.orig/arch/powerpc/kernel/misc_32.S linux-4.1.13/arch/powerpc/kernel/misc_32.S
  2061. --- linux-4.1.13.orig/arch/powerpc/kernel/misc_32.S 2015-11-09 23:34:10.000000000 +0100
  2062. +++ linux-4.1.13/arch/powerpc/kernel/misc_32.S 2015-11-29 09:23:09.517620297 +0100
  2063. @@ -40,6 +40,7 @@
  2064. * We store the saved ksp_limit in the unused part
  2065. * of the STACK_FRAME_OVERHEAD
  2066. */
  2067. +#ifndef CONFIG_PREEMPT_RT_FULL
  2068. _GLOBAL(call_do_softirq)
  2069. mflr r0
  2070. stw r0,4(r1)
  2071. @@ -56,6 +57,7 @@
  2072. stw r10,THREAD+KSP_LIMIT(r2)
  2073. mtlr r0
  2074. blr
  2075. +#endif
  2076. /*
  2077. * void call_do_irq(struct pt_regs *regs, struct thread_info *irqtp);
  2078. diff -Nur linux-4.1.13.orig/arch/powerpc/kernel/misc_64.S linux-4.1.13/arch/powerpc/kernel/misc_64.S
  2079. --- linux-4.1.13.orig/arch/powerpc/kernel/misc_64.S 2015-11-09 23:34:10.000000000 +0100
  2080. +++ linux-4.1.13/arch/powerpc/kernel/misc_64.S 2015-11-29 09:23:09.517620297 +0100
  2081. @@ -29,6 +29,7 @@
  2082. .text
  2083. +#ifndef CONFIG_PREEMPT_RT_FULL
  2084. _GLOBAL(call_do_softirq)
  2085. mflr r0
  2086. std r0,16(r1)
  2087. @@ -39,6 +40,7 @@
  2088. ld r0,16(r1)
  2089. mtlr r0
  2090. blr
  2091. +#endif
  2092. _GLOBAL(call_do_irq)
  2093. mflr r0
  2094. diff -Nur linux-4.1.13.orig/arch/powerpc/kvm/book3s_hv.c linux-4.1.13/arch/powerpc/kvm/book3s_hv.c
  2095. --- linux-4.1.13.orig/arch/powerpc/kvm/book3s_hv.c 2015-11-09 23:34:10.000000000 +0100
  2096. +++ linux-4.1.13/arch/powerpc/kvm/book3s_hv.c 2015-11-29 09:23:09.517620297 +0100
  2097. @@ -115,11 +115,11 @@
  2098. static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
  2099. {
  2100. int cpu = vcpu->cpu;
  2101. - wait_queue_head_t *wqp;
  2102. + struct swait_head *wqp;
  2103. wqp = kvm_arch_vcpu_wq(vcpu);
  2104. - if (waitqueue_active(wqp)) {
  2105. - wake_up_interruptible(wqp);
  2106. + if (swaitqueue_active(wqp)) {
  2107. + swait_wake_interruptible(wqp);
  2108. ++vcpu->stat.halt_wakeup;
  2109. }
  2110. @@ -686,8 +686,8 @@
  2111. tvcpu->arch.prodded = 1;
  2112. smp_mb();
  2113. if (vcpu->arch.ceded) {
  2114. - if (waitqueue_active(&vcpu->wq)) {
  2115. - wake_up_interruptible(&vcpu->wq);
  2116. + if (swaitqueue_active(&vcpu->wq)) {
  2117. + swait_wake_interruptible(&vcpu->wq);
  2118. vcpu->stat.halt_wakeup++;
  2119. }
  2120. }
  2121. @@ -1426,7 +1426,7 @@
  2122. INIT_LIST_HEAD(&vcore->runnable_threads);
  2123. spin_lock_init(&vcore->lock);
  2124. spin_lock_init(&vcore->stoltb_lock);
  2125. - init_waitqueue_head(&vcore->wq);
  2126. + init_swait_head(&vcore->wq);
  2127. vcore->preempt_tb = TB_NIL;
  2128. vcore->lpcr = kvm->arch.lpcr;
  2129. vcore->first_vcpuid = core * threads_per_subcore;
  2130. @@ -2073,10 +2073,9 @@
  2131. {
  2132. struct kvm_vcpu *vcpu;
  2133. int do_sleep = 1;
  2134. + DEFINE_SWAITER(wait);
  2135. - DEFINE_WAIT(wait);
  2136. -
  2137. - prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  2138. + swait_prepare(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  2139. /*
  2140. * Check one last time for pending exceptions and ceded state after
  2141. @@ -2090,7 +2089,7 @@
  2142. }
  2143. if (!do_sleep) {
  2144. - finish_wait(&vc->wq, &wait);
  2145. + swait_finish(&vc->wq, &wait);
  2146. return;
  2147. }
  2148. @@ -2098,7 +2097,7 @@
  2149. trace_kvmppc_vcore_blocked(vc, 0);
  2150. spin_unlock(&vc->lock);
  2151. schedule();
  2152. - finish_wait(&vc->wq, &wait);
  2153. + swait_finish(&vc->wq, &wait);
  2154. spin_lock(&vc->lock);
  2155. vc->vcore_state = VCORE_INACTIVE;
  2156. trace_kvmppc_vcore_blocked(vc, 1);
  2157. @@ -2142,7 +2141,7 @@
  2158. kvmppc_start_thread(vcpu);
  2159. trace_kvm_guest_enter(vcpu);
  2160. } else if (vc->vcore_state == VCORE_SLEEPING) {
  2161. - wake_up(&vc->wq);
  2162. + swait_wake(&vc->wq);
  2163. }
  2164. }
  2165. diff -Nur linux-4.1.13.orig/arch/powerpc/kvm/Kconfig linux-4.1.13/arch/powerpc/kvm/Kconfig
  2166. --- linux-4.1.13.orig/arch/powerpc/kvm/Kconfig 2015-11-09 23:34:10.000000000 +0100
  2167. +++ linux-4.1.13/arch/powerpc/kvm/Kconfig 2015-11-29 09:23:09.517620297 +0100
  2168. @@ -172,6 +172,7 @@
  2169. config KVM_MPIC
  2170. bool "KVM in-kernel MPIC emulation"
  2171. depends on KVM && E500
  2172. + depends on !PREEMPT_RT_FULL
  2173. select HAVE_KVM_IRQCHIP
  2174. select HAVE_KVM_IRQFD
  2175. select HAVE_KVM_IRQ_ROUTING
  2176. diff -Nur linux-4.1.13.orig/arch/powerpc/mm/fault.c linux-4.1.13/arch/powerpc/mm/fault.c
  2177. --- linux-4.1.13.orig/arch/powerpc/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  2178. +++ linux-4.1.13/arch/powerpc/mm/fault.c 2015-11-29 09:23:09.517620297 +0100
  2179. @@ -33,13 +33,13 @@
  2180. #include <linux/ratelimit.h>
  2181. #include <linux/context_tracking.h>
  2182. #include <linux/hugetlb.h>
  2183. +#include <linux/uaccess.h>
  2184. #include <asm/firmware.h>
  2185. #include <asm/page.h>
  2186. #include <asm/pgtable.h>
  2187. #include <asm/mmu.h>
  2188. #include <asm/mmu_context.h>
  2189. -#include <asm/uaccess.h>
  2190. #include <asm/tlbflush.h>
  2191. #include <asm/siginfo.h>
  2192. #include <asm/debug.h>
  2193. @@ -272,15 +272,16 @@
  2194. if (!arch_irq_disabled_regs(regs))
  2195. local_irq_enable();
  2196. - if (in_atomic() || mm == NULL) {
  2197. + if (faulthandler_disabled() || mm == NULL) {
  2198. if (!user_mode(regs)) {
  2199. rc = SIGSEGV;
  2200. goto bail;
  2201. }
  2202. - /* in_atomic() in user mode is really bad,
  2203. + /* faulthandler_disabled() in user mode is really bad,
  2204. as is current->mm == NULL. */
  2205. printk(KERN_EMERG "Page fault in user mode with "
  2206. - "in_atomic() = %d mm = %p\n", in_atomic(), mm);
  2207. + "faulthandler_disabled() = %d mm = %p\n",
  2208. + faulthandler_disabled(), mm);
  2209. printk(KERN_EMERG "NIP = %lx MSR = %lx\n",
  2210. regs->nip, regs->msr);
  2211. die("Weird page fault", regs, SIGSEGV);
  2212. diff -Nur linux-4.1.13.orig/arch/powerpc/mm/highmem.c linux-4.1.13/arch/powerpc/mm/highmem.c
  2213. --- linux-4.1.13.orig/arch/powerpc/mm/highmem.c 2015-11-09 23:34:10.000000000 +0100
  2214. +++ linux-4.1.13/arch/powerpc/mm/highmem.c 2015-11-29 09:23:09.517620297 +0100
  2215. @@ -34,7 +34,7 @@
  2216. unsigned long vaddr;
  2217. int idx, type;
  2218. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  2219. + preempt_disable();
  2220. pagefault_disable();
  2221. if (!PageHighMem(page))
  2222. return page_address(page);
  2223. @@ -59,6 +59,7 @@
  2224. if (vaddr < __fix_to_virt(FIX_KMAP_END)) {
  2225. pagefault_enable();
  2226. + preempt_enable();
  2227. return;
  2228. }
  2229. @@ -82,5 +83,6 @@
  2230. kmap_atomic_idx_pop();
  2231. pagefault_enable();
  2232. + preempt_enable();
  2233. }
  2234. EXPORT_SYMBOL(__kunmap_atomic);
  2235. diff -Nur linux-4.1.13.orig/arch/powerpc/platforms/ps3/device-init.c linux-4.1.13/arch/powerpc/platforms/ps3/device-init.c
  2236. --- linux-4.1.13.orig/arch/powerpc/platforms/ps3/device-init.c 2015-11-09 23:34:10.000000000 +0100
  2237. +++ linux-4.1.13/arch/powerpc/platforms/ps3/device-init.c 2015-11-29 09:23:09.521620031 +0100
  2238. @@ -752,7 +752,7 @@
  2239. }
  2240. pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
  2241. - res = wait_event_interruptible(dev->done.wait,
  2242. + res = swait_event_interruptible(dev->done.wait,
  2243. dev->done.done || kthread_should_stop());
  2244. if (kthread_should_stop())
  2245. res = -EINTR;
  2246. diff -Nur linux-4.1.13.orig/arch/s390/include/asm/kvm_host.h linux-4.1.13/arch/s390/include/asm/kvm_host.h
  2247. --- linux-4.1.13.orig/arch/s390/include/asm/kvm_host.h 2015-11-09 23:34:10.000000000 +0100
  2248. +++ linux-4.1.13/arch/s390/include/asm/kvm_host.h 2015-11-29 09:23:09.521620031 +0100
  2249. @@ -419,7 +419,7 @@
  2250. struct kvm_s390_local_interrupt {
  2251. spinlock_t lock;
  2252. struct kvm_s390_float_interrupt *float_int;
  2253. - wait_queue_head_t *wq;
  2254. + struct swait_head *wq;
  2255. atomic_t *cpuflags;
  2256. DECLARE_BITMAP(sigp_emerg_pending, KVM_MAX_VCPUS);
  2257. struct kvm_s390_irq_payload irq;
  2258. diff -Nur linux-4.1.13.orig/arch/s390/include/asm/uaccess.h linux-4.1.13/arch/s390/include/asm/uaccess.h
  2259. --- linux-4.1.13.orig/arch/s390/include/asm/uaccess.h 2015-11-09 23:34:10.000000000 +0100
  2260. +++ linux-4.1.13/arch/s390/include/asm/uaccess.h 2015-11-29 09:23:09.521620031 +0100
  2261. @@ -98,7 +98,8 @@
  2262. * @from: Source address, in user space.
  2263. * @n: Number of bytes to copy.
  2264. *
  2265. - * Context: User context only. This function may sleep.
  2266. + * Context: User context only. This function may sleep if pagefaults are
  2267. + * enabled.
  2268. *
  2269. * Copy data from user space to kernel space. Caller must check
  2270. * the specified block with access_ok() before calling this function.
  2271. @@ -118,7 +119,8 @@
  2272. * @from: Source address, in kernel space.
  2273. * @n: Number of bytes to copy.
  2274. *
  2275. - * Context: User context only. This function may sleep.
  2276. + * Context: User context only. This function may sleep if pagefaults are
  2277. + * enabled.
  2278. *
  2279. * Copy data from kernel space to user space. Caller must check
  2280. * the specified block with access_ok() before calling this function.
  2281. @@ -264,7 +266,8 @@
  2282. * @from: Source address, in kernel space.
  2283. * @n: Number of bytes to copy.
  2284. *
  2285. - * Context: User context only. This function may sleep.
  2286. + * Context: User context only. This function may sleep if pagefaults are
  2287. + * enabled.
  2288. *
  2289. * Copy data from kernel space to user space.
  2290. *
  2291. @@ -290,7 +293,8 @@
  2292. * @from: Source address, in user space.
  2293. * @n: Number of bytes to copy.
  2294. *
  2295. - * Context: User context only. This function may sleep.
  2296. + * Context: User context only. This function may sleep if pagefaults are
  2297. + * enabled.
  2298. *
  2299. * Copy data from user space to kernel space.
  2300. *
  2301. @@ -348,7 +352,8 @@
  2302. * strlen_user: - Get the size of a string in user space.
  2303. * @str: The string to measure.
  2304. *
  2305. - * Context: User context only. This function may sleep.
  2306. + * Context: User context only. This function may sleep if pagefaults are
  2307. + * enabled.
  2308. *
  2309. * Get the size of a NUL-terminated string in user space.
  2310. *
  2311. diff -Nur linux-4.1.13.orig/arch/s390/kvm/interrupt.c linux-4.1.13/arch/s390/kvm/interrupt.c
  2312. --- linux-4.1.13.orig/arch/s390/kvm/interrupt.c 2015-11-09 23:34:10.000000000 +0100
  2313. +++ linux-4.1.13/arch/s390/kvm/interrupt.c 2015-11-29 09:23:09.521620031 +0100
  2314. @@ -875,13 +875,13 @@
  2315. void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
  2316. {
  2317. - if (waitqueue_active(&vcpu->wq)) {
  2318. + if (swaitqueue_active(&vcpu->wq)) {
  2319. /*
  2320. * The vcpu gave up the cpu voluntarily, mark it as a good
  2321. * yield-candidate.
  2322. */
  2323. vcpu->preempted = true;
  2324. - wake_up_interruptible(&vcpu->wq);
  2325. + swait_wake_interruptible(&vcpu->wq);
  2326. vcpu->stat.halt_wakeup++;
  2327. }
  2328. }
  2329. @@ -987,7 +987,7 @@
  2330. spin_lock(&li->lock);
  2331. irq.u.pgm.code = code;
  2332. __inject_prog(vcpu, &irq);
  2333. - BUG_ON(waitqueue_active(li->wq));
  2334. + BUG_ON(swaitqueue_active(li->wq));
  2335. spin_unlock(&li->lock);
  2336. return 0;
  2337. }
  2338. @@ -1006,7 +1006,7 @@
  2339. spin_lock(&li->lock);
  2340. irq.u.pgm = *pgm_info;
  2341. rc = __inject_prog(vcpu, &irq);
  2342. - BUG_ON(waitqueue_active(li->wq));
  2343. + BUG_ON(swaitqueue_active(li->wq));
  2344. spin_unlock(&li->lock);
  2345. return rc;
  2346. }
  2347. diff -Nur linux-4.1.13.orig/arch/s390/mm/fault.c linux-4.1.13/arch/s390/mm/fault.c
  2348. --- linux-4.1.13.orig/arch/s390/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  2349. +++ linux-4.1.13/arch/s390/mm/fault.c 2015-11-29 09:23:09.521620031 +0100
  2350. @@ -399,7 +399,7 @@
  2351. * user context.
  2352. */
  2353. fault = VM_FAULT_BADCONTEXT;
  2354. - if (unlikely(!user_space_fault(regs) || in_atomic() || !mm))
  2355. + if (unlikely(!user_space_fault(regs) || faulthandler_disabled() || !mm))
  2356. goto out;
  2357. address = trans_exc_code & __FAIL_ADDR_MASK;
  2358. diff -Nur linux-4.1.13.orig/arch/score/include/asm/uaccess.h linux-4.1.13/arch/score/include/asm/uaccess.h
  2359. --- linux-4.1.13.orig/arch/score/include/asm/uaccess.h 2015-11-09 23:34:10.000000000 +0100
  2360. +++ linux-4.1.13/arch/score/include/asm/uaccess.h 2015-11-29 09:23:09.521620031 +0100
  2361. @@ -36,7 +36,8 @@
  2362. * @addr: User space pointer to start of block to check
  2363. * @size: Size of block to check
  2364. *
  2365. - * Context: User context only. This function may sleep.
  2366. + * Context: User context only. This function may sleep if pagefaults are
  2367. + * enabled.
  2368. *
  2369. * Checks if a pointer to a block of memory in user space is valid.
  2370. *
  2371. @@ -61,7 +62,8 @@
  2372. * @x: Value to copy to user space.
  2373. * @ptr: Destination address, in user space.
  2374. *
  2375. - * Context: User context only. This function may sleep.
  2376. + * Context: User context only. This function may sleep if pagefaults are
  2377. + * enabled.
  2378. *
  2379. * This macro copies a single simple value from kernel space to user
  2380. * space. It supports simple types like char and int, but not larger
  2381. @@ -79,7 +81,8 @@
  2382. * @x: Variable to store result.
  2383. * @ptr: Source address, in user space.
  2384. *
  2385. - * Context: User context only. This function may sleep.
  2386. + * Context: User context only. This function may sleep if pagefaults are
  2387. + * enabled.
  2388. *
  2389. * This macro copies a single simple variable from user space to kernel
  2390. * space. It supports simple types like char and int, but not larger
  2391. @@ -98,7 +101,8 @@
  2392. * @x: Value to copy to user space.
  2393. * @ptr: Destination address, in user space.
  2394. *
  2395. - * Context: User context only. This function may sleep.
  2396. + * Context: User context only. This function may sleep if pagefaults are
  2397. + * enabled.
  2398. *
  2399. * This macro copies a single simple value from kernel space to user
  2400. * space. It supports simple types like char and int, but not larger
  2401. @@ -119,7 +123,8 @@
  2402. * @x: Variable to store result.
  2403. * @ptr: Source address, in user space.
  2404. *
  2405. - * Context: User context only. This function may sleep.
  2406. + * Context: User context only. This function may sleep if pagefaults are
  2407. + * enabled.
  2408. *
  2409. * This macro copies a single simple variable from user space to kernel
  2410. * space. It supports simple types like char and int, but not larger
  2411. diff -Nur linux-4.1.13.orig/arch/score/mm/fault.c linux-4.1.13/arch/score/mm/fault.c
  2412. --- linux-4.1.13.orig/arch/score/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  2413. +++ linux-4.1.13/arch/score/mm/fault.c 2015-11-29 09:23:09.521620031 +0100
  2414. @@ -34,6 +34,7 @@
  2415. #include <linux/string.h>
  2416. #include <linux/types.h>
  2417. #include <linux/ptrace.h>
  2418. +#include <linux/uaccess.h>
  2419. /*
  2420. * This routine handles page faults. It determines the address,
  2421. @@ -73,7 +74,7 @@
  2422. * If we're in an interrupt or have no user
  2423. * context, we must not take the fault..
  2424. */
  2425. - if (in_atomic() || !mm)
  2426. + if (pagefault_disabled() || !mm)
  2427. goto bad_area_nosemaphore;
  2428. if (user_mode(regs))
  2429. diff -Nur linux-4.1.13.orig/arch/sh/kernel/irq.c linux-4.1.13/arch/sh/kernel/irq.c
  2430. --- linux-4.1.13.orig/arch/sh/kernel/irq.c 2015-11-09 23:34:10.000000000 +0100
  2431. +++ linux-4.1.13/arch/sh/kernel/irq.c 2015-11-29 09:23:09.521620031 +0100
  2432. @@ -147,6 +147,7 @@
  2433. hardirq_ctx[cpu] = NULL;
  2434. }
  2435. +#ifndef CONFIG_PREEMPT_RT_FULL
  2436. void do_softirq_own_stack(void)
  2437. {
  2438. struct thread_info *curctx;
  2439. @@ -174,6 +175,7 @@
  2440. "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
  2441. );
  2442. }
  2443. +#endif
  2444. #else
  2445. static inline void handle_one_irq(unsigned int irq)
  2446. {
  2447. diff -Nur linux-4.1.13.orig/arch/sh/mm/fault.c linux-4.1.13/arch/sh/mm/fault.c
  2448. --- linux-4.1.13.orig/arch/sh/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  2449. +++ linux-4.1.13/arch/sh/mm/fault.c 2015-11-29 09:23:09.521620031 +0100
  2450. @@ -17,6 +17,7 @@
  2451. #include <linux/kprobes.h>
  2452. #include <linux/perf_event.h>
  2453. #include <linux/kdebug.h>
  2454. +#include <linux/uaccess.h>
  2455. #include <asm/io_trapped.h>
  2456. #include <asm/mmu_context.h>
  2457. #include <asm/tlbflush.h>
  2458. @@ -438,9 +439,9 @@
  2459. /*
  2460. * If we're in an interrupt, have no user context or are running
  2461. - * in an atomic region then we must not take the fault:
  2462. + * with pagefaults disabled then we must not take the fault:
  2463. */
  2464. - if (unlikely(in_atomic() || !mm)) {
  2465. + if (unlikely(faulthandler_disabled() || !mm)) {
  2466. bad_area_nosemaphore(regs, error_code, address);
  2467. return;
  2468. }
  2469. diff -Nur linux-4.1.13.orig/arch/sparc/Kconfig linux-4.1.13/arch/sparc/Kconfig
  2470. --- linux-4.1.13.orig/arch/sparc/Kconfig 2015-11-09 23:34:10.000000000 +0100
  2471. +++ linux-4.1.13/arch/sparc/Kconfig 2015-11-29 09:23:09.521620031 +0100
  2472. @@ -189,12 +189,10 @@
  2473. source kernel/Kconfig.hz
  2474. config RWSEM_GENERIC_SPINLOCK
  2475. - bool
  2476. - default y if SPARC32
  2477. + def_bool PREEMPT_RT_FULL
  2478. config RWSEM_XCHGADD_ALGORITHM
  2479. - bool
  2480. - default y if SPARC64
  2481. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  2482. config GENERIC_HWEIGHT
  2483. bool
  2484. diff -Nur linux-4.1.13.orig/arch/sparc/kernel/irq_64.c linux-4.1.13/arch/sparc/kernel/irq_64.c
  2485. --- linux-4.1.13.orig/arch/sparc/kernel/irq_64.c 2015-11-09 23:34:10.000000000 +0100
  2486. +++ linux-4.1.13/arch/sparc/kernel/irq_64.c 2015-11-29 09:23:09.521620031 +0100
  2487. @@ -849,6 +849,7 @@
  2488. set_irq_regs(old_regs);
  2489. }
  2490. +#ifndef CONFIG_PREEMPT_RT_FULL
  2491. void do_softirq_own_stack(void)
  2492. {
  2493. void *orig_sp, *sp = softirq_stack[smp_processor_id()];
  2494. @@ -863,6 +864,7 @@
  2495. __asm__ __volatile__("mov %0, %%sp"
  2496. : : "r" (orig_sp));
  2497. }
  2498. +#endif
  2499. #ifdef CONFIG_HOTPLUG_CPU
  2500. void fixup_irqs(void)
  2501. diff -Nur linux-4.1.13.orig/arch/sparc/mm/fault_32.c linux-4.1.13/arch/sparc/mm/fault_32.c
  2502. --- linux-4.1.13.orig/arch/sparc/mm/fault_32.c 2015-11-09 23:34:10.000000000 +0100
  2503. +++ linux-4.1.13/arch/sparc/mm/fault_32.c 2015-11-29 09:23:09.521620031 +0100
  2504. @@ -21,6 +21,7 @@
  2505. #include <linux/perf_event.h>
  2506. #include <linux/interrupt.h>
  2507. #include <linux/kdebug.h>
  2508. +#include <linux/uaccess.h>
  2509. #include <asm/page.h>
  2510. #include <asm/pgtable.h>
  2511. @@ -29,7 +30,6 @@
  2512. #include <asm/setup.h>
  2513. #include <asm/smp.h>
  2514. #include <asm/traps.h>
  2515. -#include <asm/uaccess.h>
  2516. #include "mm_32.h"
  2517. @@ -196,7 +196,7 @@
  2518. * If we're in an interrupt or have no user
  2519. * context, we must not take the fault..
  2520. */
  2521. - if (in_atomic() || !mm)
  2522. + if (pagefault_disabled() || !mm)
  2523. goto no_context;
  2524. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  2525. diff -Nur linux-4.1.13.orig/arch/sparc/mm/fault_64.c linux-4.1.13/arch/sparc/mm/fault_64.c
  2526. --- linux-4.1.13.orig/arch/sparc/mm/fault_64.c 2015-11-09 23:34:10.000000000 +0100
  2527. +++ linux-4.1.13/arch/sparc/mm/fault_64.c 2015-11-29 09:23:09.521620031 +0100
  2528. @@ -22,12 +22,12 @@
  2529. #include <linux/kdebug.h>
  2530. #include <linux/percpu.h>
  2531. #include <linux/context_tracking.h>
  2532. +#include <linux/uaccess.h>
  2533. #include <asm/page.h>
  2534. #include <asm/pgtable.h>
  2535. #include <asm/openprom.h>
  2536. #include <asm/oplib.h>
  2537. -#include <asm/uaccess.h>
  2538. #include <asm/asi.h>
  2539. #include <asm/lsu.h>
  2540. #include <asm/sections.h>
  2541. @@ -330,7 +330,7 @@
  2542. * If we're in an interrupt or have no user
  2543. * context, we must not take the fault..
  2544. */
  2545. - if (in_atomic() || !mm)
  2546. + if (faulthandler_disabled() || !mm)
  2547. goto intr_or_no_mm;
  2548. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  2549. diff -Nur linux-4.1.13.orig/arch/sparc/mm/highmem.c linux-4.1.13/arch/sparc/mm/highmem.c
  2550. --- linux-4.1.13.orig/arch/sparc/mm/highmem.c 2015-11-09 23:34:10.000000000 +0100
  2551. +++ linux-4.1.13/arch/sparc/mm/highmem.c 2015-11-29 09:23:09.521620031 +0100
  2552. @@ -53,7 +53,7 @@
  2553. unsigned long vaddr;
  2554. long idx, type;
  2555. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  2556. + preempt_disable();
  2557. pagefault_disable();
  2558. if (!PageHighMem(page))
  2559. return page_address(page);
  2560. @@ -91,6 +91,7 @@
  2561. if (vaddr < FIXADDR_START) { // FIXME
  2562. pagefault_enable();
  2563. + preempt_enable();
  2564. return;
  2565. }
  2566. @@ -126,5 +127,6 @@
  2567. kmap_atomic_idx_pop();
  2568. pagefault_enable();
  2569. + preempt_enable();
  2570. }
  2571. EXPORT_SYMBOL(__kunmap_atomic);
  2572. diff -Nur linux-4.1.13.orig/arch/sparc/mm/init_64.c linux-4.1.13/arch/sparc/mm/init_64.c
  2573. --- linux-4.1.13.orig/arch/sparc/mm/init_64.c 2015-11-09 23:34:10.000000000 +0100
  2574. +++ linux-4.1.13/arch/sparc/mm/init_64.c 2015-11-29 09:23:09.521620031 +0100
  2575. @@ -2738,7 +2738,7 @@
  2576. struct mm_struct *mm = current->mm;
  2577. struct tsb_config *tp;
  2578. - if (in_atomic() || !mm) {
  2579. + if (faulthandler_disabled() || !mm) {
  2580. const struct exception_table_entry *entry;
  2581. entry = search_exception_tables(regs->tpc);
  2582. diff -Nur linux-4.1.13.orig/arch/tile/include/asm/uaccess.h linux-4.1.13/arch/tile/include/asm/uaccess.h
  2583. --- linux-4.1.13.orig/arch/tile/include/asm/uaccess.h 2015-11-09 23:34:10.000000000 +0100
  2584. +++ linux-4.1.13/arch/tile/include/asm/uaccess.h 2015-11-29 09:23:09.521620031 +0100
  2585. @@ -78,7 +78,8 @@
  2586. * @addr: User space pointer to start of block to check
  2587. * @size: Size of block to check
  2588. *
  2589. - * Context: User context only. This function may sleep.
  2590. + * Context: User context only. This function may sleep if pagefaults are
  2591. + * enabled.
  2592. *
  2593. * Checks if a pointer to a block of memory in user space is valid.
  2594. *
  2595. @@ -192,7 +193,8 @@
  2596. * @x: Variable to store result.
  2597. * @ptr: Source address, in user space.
  2598. *
  2599. - * Context: User context only. This function may sleep.
  2600. + * Context: User context only. This function may sleep if pagefaults are
  2601. + * enabled.
  2602. *
  2603. * This macro copies a single simple variable from user space to kernel
  2604. * space. It supports simple types like char and int, but not larger
  2605. @@ -274,7 +276,8 @@
  2606. * @x: Value to copy to user space.
  2607. * @ptr: Destination address, in user space.
  2608. *
  2609. - * Context: User context only. This function may sleep.
  2610. + * Context: User context only. This function may sleep if pagefaults are
  2611. + * enabled.
  2612. *
  2613. * This macro copies a single simple value from kernel space to user
  2614. * space. It supports simple types like char and int, but not larger
  2615. @@ -330,7 +333,8 @@
  2616. * @from: Source address, in kernel space.
  2617. * @n: Number of bytes to copy.
  2618. *
  2619. - * Context: User context only. This function may sleep.
  2620. + * Context: User context only. This function may sleep if pagefaults are
  2621. + * enabled.
  2622. *
  2623. * Copy data from kernel space to user space. Caller must check
  2624. * the specified block with access_ok() before calling this function.
  2625. @@ -366,7 +370,8 @@
  2626. * @from: Source address, in user space.
  2627. * @n: Number of bytes to copy.
  2628. *
  2629. - * Context: User context only. This function may sleep.
  2630. + * Context: User context only. This function may sleep if pagefaults are
  2631. + * enabled.
  2632. *
  2633. * Copy data from user space to kernel space. Caller must check
  2634. * the specified block with access_ok() before calling this function.
  2635. @@ -437,7 +442,8 @@
  2636. * @from: Source address, in user space.
  2637. * @n: Number of bytes to copy.
  2638. *
  2639. - * Context: User context only. This function may sleep.
  2640. + * Context: User context only. This function may sleep if pagefaults are
  2641. + * enabled.
  2642. *
  2643. * Copy data from user space to user space. Caller must check
  2644. * the specified blocks with access_ok() before calling this function.
  2645. diff -Nur linux-4.1.13.orig/arch/tile/mm/fault.c linux-4.1.13/arch/tile/mm/fault.c
  2646. --- linux-4.1.13.orig/arch/tile/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  2647. +++ linux-4.1.13/arch/tile/mm/fault.c 2015-11-29 09:23:09.521620031 +0100
  2648. @@ -354,9 +354,9 @@
  2649. /*
  2650. * If we're in an interrupt, have no user context or are running in an
  2651. - * atomic region then we must not take the fault.
  2652. + * region with pagefaults disabled then we must not take the fault.
  2653. */
  2654. - if (in_atomic() || !mm) {
  2655. + if (pagefault_disabled() || !mm) {
  2656. vma = NULL; /* happy compiler */
  2657. goto bad_area_nosemaphore;
  2658. }
  2659. diff -Nur linux-4.1.13.orig/arch/tile/mm/highmem.c linux-4.1.13/arch/tile/mm/highmem.c
  2660. --- linux-4.1.13.orig/arch/tile/mm/highmem.c 2015-11-09 23:34:10.000000000 +0100
  2661. +++ linux-4.1.13/arch/tile/mm/highmem.c 2015-11-29 09:23:09.521620031 +0100
  2662. @@ -201,7 +201,7 @@
  2663. int idx, type;
  2664. pte_t *pte;
  2665. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  2666. + preempt_disable();
  2667. pagefault_disable();
  2668. /* Avoid icache flushes by disallowing atomic executable mappings. */
  2669. @@ -259,6 +259,7 @@
  2670. }
  2671. pagefault_enable();
  2672. + preempt_enable();
  2673. }
  2674. EXPORT_SYMBOL(__kunmap_atomic);
  2675. diff -Nur linux-4.1.13.orig/arch/um/kernel/trap.c linux-4.1.13/arch/um/kernel/trap.c
  2676. --- linux-4.1.13.orig/arch/um/kernel/trap.c 2015-11-09 23:34:10.000000000 +0100
  2677. +++ linux-4.1.13/arch/um/kernel/trap.c 2015-11-29 09:23:09.521620031 +0100
  2678. @@ -35,10 +35,10 @@
  2679. *code_out = SEGV_MAPERR;
  2680. /*
  2681. - * If the fault was during atomic operation, don't take the fault, just
  2682. + * If the fault was with pagefaults disabled, don't take the fault, just
  2683. * fail.
  2684. */
  2685. - if (in_atomic())
  2686. + if (faulthandler_disabled())
  2687. goto out_nosemaphore;
  2688. if (is_user)
  2689. diff -Nur linux-4.1.13.orig/arch/unicore32/mm/fault.c linux-4.1.13/arch/unicore32/mm/fault.c
  2690. --- linux-4.1.13.orig/arch/unicore32/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  2691. +++ linux-4.1.13/arch/unicore32/mm/fault.c 2015-11-29 09:23:09.525619763 +0100
  2692. @@ -218,7 +218,7 @@
  2693. * If we're in an interrupt or have no user
  2694. * context, we must not take the fault..
  2695. */
  2696. - if (in_atomic() || !mm)
  2697. + if (faulthandler_disabled() || !mm)
  2698. goto no_context;
  2699. if (user_mode(regs))
  2700. diff -Nur linux-4.1.13.orig/arch/x86/crypto/aesni-intel_glue.c linux-4.1.13/arch/x86/crypto/aesni-intel_glue.c
  2701. --- linux-4.1.13.orig/arch/x86/crypto/aesni-intel_glue.c 2015-11-09 23:34:10.000000000 +0100
  2702. +++ linux-4.1.13/arch/x86/crypto/aesni-intel_glue.c 2015-11-29 09:23:09.525619763 +0100
  2703. @@ -382,14 +382,14 @@
  2704. err = blkcipher_walk_virt(desc, &walk);
  2705. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2706. - kernel_fpu_begin();
  2707. while ((nbytes = walk.nbytes)) {
  2708. + kernel_fpu_begin();
  2709. aesni_ecb_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2710. - nbytes & AES_BLOCK_MASK);
  2711. + nbytes & AES_BLOCK_MASK);
  2712. + kernel_fpu_end();
  2713. nbytes &= AES_BLOCK_SIZE - 1;
  2714. err = blkcipher_walk_done(desc, &walk, nbytes);
  2715. }
  2716. - kernel_fpu_end();
  2717. return err;
  2718. }
  2719. @@ -406,14 +406,14 @@
  2720. err = blkcipher_walk_virt(desc, &walk);
  2721. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2722. - kernel_fpu_begin();
  2723. while ((nbytes = walk.nbytes)) {
  2724. + kernel_fpu_begin();
  2725. aesni_ecb_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2726. nbytes & AES_BLOCK_MASK);
  2727. + kernel_fpu_end();
  2728. nbytes &= AES_BLOCK_SIZE - 1;
  2729. err = blkcipher_walk_done(desc, &walk, nbytes);
  2730. }
  2731. - kernel_fpu_end();
  2732. return err;
  2733. }
  2734. @@ -430,14 +430,14 @@
  2735. err = blkcipher_walk_virt(desc, &walk);
  2736. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2737. - kernel_fpu_begin();
  2738. while ((nbytes = walk.nbytes)) {
  2739. + kernel_fpu_begin();
  2740. aesni_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2741. nbytes & AES_BLOCK_MASK, walk.iv);
  2742. + kernel_fpu_end();
  2743. nbytes &= AES_BLOCK_SIZE - 1;
  2744. err = blkcipher_walk_done(desc, &walk, nbytes);
  2745. }
  2746. - kernel_fpu_end();
  2747. return err;
  2748. }
  2749. @@ -454,14 +454,14 @@
  2750. err = blkcipher_walk_virt(desc, &walk);
  2751. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2752. - kernel_fpu_begin();
  2753. while ((nbytes = walk.nbytes)) {
  2754. + kernel_fpu_begin();
  2755. aesni_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2756. nbytes & AES_BLOCK_MASK, walk.iv);
  2757. + kernel_fpu_end();
  2758. nbytes &= AES_BLOCK_SIZE - 1;
  2759. err = blkcipher_walk_done(desc, &walk, nbytes);
  2760. }
  2761. - kernel_fpu_end();
  2762. return err;
  2763. }
  2764. @@ -513,18 +513,20 @@
  2765. err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
  2766. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2767. - kernel_fpu_begin();
  2768. while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
  2769. + kernel_fpu_begin();
  2770. aesni_ctr_enc_tfm(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2771. nbytes & AES_BLOCK_MASK, walk.iv);
  2772. + kernel_fpu_end();
  2773. nbytes &= AES_BLOCK_SIZE - 1;
  2774. err = blkcipher_walk_done(desc, &walk, nbytes);
  2775. }
  2776. if (walk.nbytes) {
  2777. + kernel_fpu_begin();
  2778. ctr_crypt_final(ctx, &walk);
  2779. + kernel_fpu_end();
  2780. err = blkcipher_walk_done(desc, &walk, 0);
  2781. }
  2782. - kernel_fpu_end();
  2783. return err;
  2784. }
  2785. diff -Nur linux-4.1.13.orig/arch/x86/crypto/cast5_avx_glue.c linux-4.1.13/arch/x86/crypto/cast5_avx_glue.c
  2786. --- linux-4.1.13.orig/arch/x86/crypto/cast5_avx_glue.c 2015-11-09 23:34:10.000000000 +0100
  2787. +++ linux-4.1.13/arch/x86/crypto/cast5_avx_glue.c 2015-11-29 09:23:09.525619763 +0100
  2788. @@ -60,7 +60,7 @@
  2789. static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  2790. bool enc)
  2791. {
  2792. - bool fpu_enabled = false;
  2793. + bool fpu_enabled;
  2794. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  2795. const unsigned int bsize = CAST5_BLOCK_SIZE;
  2796. unsigned int nbytes;
  2797. @@ -76,7 +76,7 @@
  2798. u8 *wsrc = walk->src.virt.addr;
  2799. u8 *wdst = walk->dst.virt.addr;
  2800. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  2801. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  2802. /* Process multi-block batch */
  2803. if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
  2804. @@ -104,10 +104,9 @@
  2805. } while (nbytes >= bsize);
  2806. done:
  2807. + cast5_fpu_end(fpu_enabled);
  2808. err = blkcipher_walk_done(desc, walk, nbytes);
  2809. }
  2810. -
  2811. - cast5_fpu_end(fpu_enabled);
  2812. return err;
  2813. }
  2814. @@ -228,7 +227,7 @@
  2815. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  2816. struct scatterlist *src, unsigned int nbytes)
  2817. {
  2818. - bool fpu_enabled = false;
  2819. + bool fpu_enabled;
  2820. struct blkcipher_walk walk;
  2821. int err;
  2822. @@ -237,12 +236,11 @@
  2823. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2824. while ((nbytes = walk.nbytes)) {
  2825. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  2826. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  2827. nbytes = __cbc_decrypt(desc, &walk);
  2828. + cast5_fpu_end(fpu_enabled);
  2829. err = blkcipher_walk_done(desc, &walk, nbytes);
  2830. }
  2831. -
  2832. - cast5_fpu_end(fpu_enabled);
  2833. return err;
  2834. }
  2835. @@ -312,7 +310,7 @@
  2836. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  2837. struct scatterlist *src, unsigned int nbytes)
  2838. {
  2839. - bool fpu_enabled = false;
  2840. + bool fpu_enabled;
  2841. struct blkcipher_walk walk;
  2842. int err;
  2843. @@ -321,13 +319,12 @@
  2844. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2845. while ((nbytes = walk.nbytes) >= CAST5_BLOCK_SIZE) {
  2846. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  2847. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  2848. nbytes = __ctr_crypt(desc, &walk);
  2849. + cast5_fpu_end(fpu_enabled);
  2850. err = blkcipher_walk_done(desc, &walk, nbytes);
  2851. }
  2852. - cast5_fpu_end(fpu_enabled);
  2853. -
  2854. if (walk.nbytes) {
  2855. ctr_crypt_final(desc, &walk);
  2856. err = blkcipher_walk_done(desc, &walk, 0);
  2857. diff -Nur linux-4.1.13.orig/arch/x86/crypto/glue_helper.c linux-4.1.13/arch/x86/crypto/glue_helper.c
  2858. --- linux-4.1.13.orig/arch/x86/crypto/glue_helper.c 2015-11-09 23:34:10.000000000 +0100
  2859. +++ linux-4.1.13/arch/x86/crypto/glue_helper.c 2015-11-29 09:23:09.525619763 +0100
  2860. @@ -39,7 +39,7 @@
  2861. void *ctx = crypto_blkcipher_ctx(desc->tfm);
  2862. const unsigned int bsize = 128 / 8;
  2863. unsigned int nbytes, i, func_bytes;
  2864. - bool fpu_enabled = false;
  2865. + bool fpu_enabled;
  2866. int err;
  2867. err = blkcipher_walk_virt(desc, walk);
  2868. @@ -49,7 +49,7 @@
  2869. u8 *wdst = walk->dst.virt.addr;
  2870. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2871. - desc, fpu_enabled, nbytes);
  2872. + desc, false, nbytes);
  2873. for (i = 0; i < gctx->num_funcs; i++) {
  2874. func_bytes = bsize * gctx->funcs[i].num_blocks;
  2875. @@ -71,10 +71,10 @@
  2876. }
  2877. done:
  2878. + glue_fpu_end(fpu_enabled);
  2879. err = blkcipher_walk_done(desc, walk, nbytes);
  2880. }
  2881. - glue_fpu_end(fpu_enabled);
  2882. return err;
  2883. }
  2884. @@ -194,7 +194,7 @@
  2885. struct scatterlist *src, unsigned int nbytes)
  2886. {
  2887. const unsigned int bsize = 128 / 8;
  2888. - bool fpu_enabled = false;
  2889. + bool fpu_enabled;
  2890. struct blkcipher_walk walk;
  2891. int err;
  2892. @@ -203,12 +203,12 @@
  2893. while ((nbytes = walk.nbytes)) {
  2894. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2895. - desc, fpu_enabled, nbytes);
  2896. + desc, false, nbytes);
  2897. nbytes = __glue_cbc_decrypt_128bit(gctx, desc, &walk);
  2898. + glue_fpu_end(fpu_enabled);
  2899. err = blkcipher_walk_done(desc, &walk, nbytes);
  2900. }
  2901. - glue_fpu_end(fpu_enabled);
  2902. return err;
  2903. }
  2904. EXPORT_SYMBOL_GPL(glue_cbc_decrypt_128bit);
  2905. @@ -277,7 +277,7 @@
  2906. struct scatterlist *src, unsigned int nbytes)
  2907. {
  2908. const unsigned int bsize = 128 / 8;
  2909. - bool fpu_enabled = false;
  2910. + bool fpu_enabled;
  2911. struct blkcipher_walk walk;
  2912. int err;
  2913. @@ -286,13 +286,12 @@
  2914. while ((nbytes = walk.nbytes) >= bsize) {
  2915. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2916. - desc, fpu_enabled, nbytes);
  2917. + desc, false, nbytes);
  2918. nbytes = __glue_ctr_crypt_128bit(gctx, desc, &walk);
  2919. + glue_fpu_end(fpu_enabled);
  2920. err = blkcipher_walk_done(desc, &walk, nbytes);
  2921. }
  2922. - glue_fpu_end(fpu_enabled);
  2923. -
  2924. if (walk.nbytes) {
  2925. glue_ctr_crypt_final_128bit(
  2926. gctx->funcs[gctx->num_funcs - 1].fn_u.ctr, desc, &walk);
  2927. @@ -347,7 +346,7 @@
  2928. void *tweak_ctx, void *crypt_ctx)
  2929. {
  2930. const unsigned int bsize = 128 / 8;
  2931. - bool fpu_enabled = false;
  2932. + bool fpu_enabled;
  2933. struct blkcipher_walk walk;
  2934. int err;
  2935. @@ -360,21 +359,21 @@
  2936. /* set minimum length to bsize, for tweak_fn */
  2937. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2938. - desc, fpu_enabled,
  2939. + desc, false,
  2940. nbytes < bsize ? bsize : nbytes);
  2941. -
  2942. /* calculate first value of T */
  2943. tweak_fn(tweak_ctx, walk.iv, walk.iv);
  2944. + glue_fpu_end(fpu_enabled);
  2945. while (nbytes) {
  2946. + fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2947. + desc, false, nbytes);
  2948. nbytes = __glue_xts_crypt_128bit(gctx, crypt_ctx, desc, &walk);
  2949. + glue_fpu_end(fpu_enabled);
  2950. err = blkcipher_walk_done(desc, &walk, nbytes);
  2951. nbytes = walk.nbytes;
  2952. }
  2953. -
  2954. - glue_fpu_end(fpu_enabled);
  2955. -
  2956. return err;
  2957. }
  2958. EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit);
  2959. diff -Nur linux-4.1.13.orig/arch/x86/include/asm/preempt.h linux-4.1.13/arch/x86/include/asm/preempt.h
  2960. --- linux-4.1.13.orig/arch/x86/include/asm/preempt.h 2015-11-09 23:34:10.000000000 +0100
  2961. +++ linux-4.1.13/arch/x86/include/asm/preempt.h 2015-11-29 09:23:09.525619763 +0100
  2962. @@ -82,17 +82,33 @@
  2963. * a decrement which hits zero means we have no preempt_count and should
  2964. * reschedule.
  2965. */
  2966. -static __always_inline bool __preempt_count_dec_and_test(void)
  2967. +static __always_inline bool ____preempt_count_dec_and_test(void)
  2968. {
  2969. GEN_UNARY_RMWcc("decl", __preempt_count, __percpu_arg(0), "e");
  2970. }
  2971. +static __always_inline bool __preempt_count_dec_and_test(void)
  2972. +{
  2973. + if (____preempt_count_dec_and_test())
  2974. + return true;
  2975. +#ifdef CONFIG_PREEMPT_LAZY
  2976. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  2977. +#else
  2978. + return false;
  2979. +#endif
  2980. +}
  2981. +
  2982. /*
  2983. * Returns true when we need to resched and can (barring IRQ state).
  2984. */
  2985. static __always_inline bool should_resched(int preempt_offset)
  2986. {
  2987. +#ifdef CONFIG_PREEMPT_LAZY
  2988. + return unlikely(raw_cpu_read_4(__preempt_count) == preempt_offset ||
  2989. + test_thread_flag(TIF_NEED_RESCHED_LAZY));
  2990. +#else
  2991. return unlikely(raw_cpu_read_4(__preempt_count) == preempt_offset);
  2992. +#endif
  2993. }
  2994. #ifdef CONFIG_PREEMPT
  2995. diff -Nur linux-4.1.13.orig/arch/x86/include/asm/signal.h linux-4.1.13/arch/x86/include/asm/signal.h
  2996. --- linux-4.1.13.orig/arch/x86/include/asm/signal.h 2015-11-09 23:34:10.000000000 +0100
  2997. +++ linux-4.1.13/arch/x86/include/asm/signal.h 2015-11-29 09:23:09.525619763 +0100
  2998. @@ -23,6 +23,19 @@
  2999. unsigned long sig[_NSIG_WORDS];
  3000. } sigset_t;
  3001. +/*
  3002. + * Because some traps use the IST stack, we must keep preemption
  3003. + * disabled while calling do_trap(), but do_trap() may call
  3004. + * force_sig_info() which will grab the signal spin_locks for the
  3005. + * task, which in PREEMPT_RT_FULL are mutexes. By defining
  3006. + * ARCH_RT_DELAYS_SIGNAL_SEND the force_sig_info() will set
  3007. + * TIF_NOTIFY_RESUME and set up the signal to be sent on exit of the
  3008. + * trap.
  3009. + */
  3010. +#if defined(CONFIG_PREEMPT_RT_FULL) && defined(CONFIG_X86_64)
  3011. +#define ARCH_RT_DELAYS_SIGNAL_SEND
  3012. +#endif
  3013. +
  3014. #ifndef CONFIG_COMPAT
  3015. typedef sigset_t compat_sigset_t;
  3016. #endif
  3017. diff -Nur linux-4.1.13.orig/arch/x86/include/asm/stackprotector.h linux-4.1.13/arch/x86/include/asm/stackprotector.h
  3018. --- linux-4.1.13.orig/arch/x86/include/asm/stackprotector.h 2015-11-09 23:34:10.000000000 +0100
  3019. +++ linux-4.1.13/arch/x86/include/asm/stackprotector.h 2015-11-29 09:23:09.525619763 +0100
  3020. @@ -57,7 +57,7 @@
  3021. */
  3022. static __always_inline void boot_init_stack_canary(void)
  3023. {
  3024. - u64 canary;
  3025. + u64 uninitialized_var(canary);
  3026. u64 tsc;
  3027. #ifdef CONFIG_X86_64
  3028. @@ -68,8 +68,16 @@
  3029. * of randomness. The TSC only matters for very early init,
  3030. * there it already has some randomness on most systems. Later
  3031. * on during the bootup the random pool has true entropy too.
  3032. + *
  3033. + * For preempt-rt we need to weaken the randomness a bit, as
  3034. + * we can't call into the random generator from atomic context
  3035. + * due to locking constraints. We just leave canary
  3036. + * uninitialized and use the TSC based randomness on top of
  3037. + * it.
  3038. */
  3039. +#ifndef CONFIG_PREEMPT_RT_FULL
  3040. get_random_bytes(&canary, sizeof(canary));
  3041. +#endif
  3042. tsc = __native_read_tsc();
  3043. canary += tsc + (tsc << 32UL);
  3044. diff -Nur linux-4.1.13.orig/arch/x86/include/asm/thread_info.h linux-4.1.13/arch/x86/include/asm/thread_info.h
  3045. --- linux-4.1.13.orig/arch/x86/include/asm/thread_info.h 2015-11-09 23:34:10.000000000 +0100
  3046. +++ linux-4.1.13/arch/x86/include/asm/thread_info.h 2015-11-29 09:23:09.525619763 +0100
  3047. @@ -55,6 +55,8 @@
  3048. __u32 status; /* thread synchronous flags */
  3049. __u32 cpu; /* current CPU */
  3050. int saved_preempt_count;
  3051. + int preempt_lazy_count; /* 0 => lazy preemptable
  3052. + <0 => BUG */
  3053. mm_segment_t addr_limit;
  3054. void __user *sysenter_return;
  3055. unsigned int sig_on_uaccess_error:1;
  3056. @@ -95,6 +97,7 @@
  3057. #define TIF_SYSCALL_EMU 6 /* syscall emulation active */
  3058. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  3059. #define TIF_SECCOMP 8 /* secure computing */
  3060. +#define TIF_NEED_RESCHED_LAZY 9 /* lazy rescheduling necessary */
  3061. #define TIF_USER_RETURN_NOTIFY 11 /* notify kernel of userspace return */
  3062. #define TIF_UPROBE 12 /* breakpointed or singlestepping */
  3063. #define TIF_NOTSC 16 /* TSC is not accessible in userland */
  3064. @@ -119,6 +122,7 @@
  3065. #define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU)
  3066. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  3067. #define _TIF_SECCOMP (1 << TIF_SECCOMP)
  3068. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  3069. #define _TIF_USER_RETURN_NOTIFY (1 << TIF_USER_RETURN_NOTIFY)
  3070. #define _TIF_UPROBE (1 << TIF_UPROBE)
  3071. #define _TIF_NOTSC (1 << TIF_NOTSC)
  3072. @@ -168,6 +172,8 @@
  3073. #define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW|_TIF_USER_RETURN_NOTIFY)
  3074. #define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)
  3075. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  3076. +
  3077. #define STACK_WARN (THREAD_SIZE/8)
  3078. /*
  3079. diff -Nur linux-4.1.13.orig/arch/x86/include/asm/uaccess_32.h linux-4.1.13/arch/x86/include/asm/uaccess_32.h
  3080. --- linux-4.1.13.orig/arch/x86/include/asm/uaccess_32.h 2015-11-09 23:34:10.000000000 +0100
  3081. +++ linux-4.1.13/arch/x86/include/asm/uaccess_32.h 2015-11-29 09:23:09.525619763 +0100
  3082. @@ -70,7 +70,8 @@
  3083. * @from: Source address, in kernel space.
  3084. * @n: Number of bytes to copy.
  3085. *
  3086. - * Context: User context only. This function may sleep.
  3087. + * Context: User context only. This function may sleep if pagefaults are
  3088. + * enabled.
  3089. *
  3090. * Copy data from kernel space to user space. Caller must check
  3091. * the specified block with access_ok() before calling this function.
  3092. @@ -117,7 +118,8 @@
  3093. * @from: Source address, in user space.
  3094. * @n: Number of bytes to copy.
  3095. *
  3096. - * Context: User context only. This function may sleep.
  3097. + * Context: User context only. This function may sleep if pagefaults are
  3098. + * enabled.
  3099. *
  3100. * Copy data from user space to kernel space. Caller must check
  3101. * the specified block with access_ok() before calling this function.
  3102. diff -Nur linux-4.1.13.orig/arch/x86/include/asm/uaccess.h linux-4.1.13/arch/x86/include/asm/uaccess.h
  3103. --- linux-4.1.13.orig/arch/x86/include/asm/uaccess.h 2015-11-09 23:34:10.000000000 +0100
  3104. +++ linux-4.1.13/arch/x86/include/asm/uaccess.h 2015-11-29 09:23:09.525619763 +0100
  3105. @@ -74,7 +74,8 @@
  3106. * @addr: User space pointer to start of block to check
  3107. * @size: Size of block to check
  3108. *
  3109. - * Context: User context only. This function may sleep.
  3110. + * Context: User context only. This function may sleep if pagefaults are
  3111. + * enabled.
  3112. *
  3113. * Checks if a pointer to a block of memory in user space is valid.
  3114. *
  3115. @@ -145,7 +146,8 @@
  3116. * @x: Variable to store result.
  3117. * @ptr: Source address, in user space.
  3118. *
  3119. - * Context: User context only. This function may sleep.
  3120. + * Context: User context only. This function may sleep if pagefaults are
  3121. + * enabled.
  3122. *
  3123. * This macro copies a single simple variable from user space to kernel
  3124. * space. It supports simple types like char and int, but not larger
  3125. @@ -240,7 +242,8 @@
  3126. * @x: Value to copy to user space.
  3127. * @ptr: Destination address, in user space.
  3128. *
  3129. - * Context: User context only. This function may sleep.
  3130. + * Context: User context only. This function may sleep if pagefaults are
  3131. + * enabled.
  3132. *
  3133. * This macro copies a single simple value from kernel space to user
  3134. * space. It supports simple types like char and int, but not larger
  3135. @@ -455,7 +458,8 @@
  3136. * @x: Variable to store result.
  3137. * @ptr: Source address, in user space.
  3138. *
  3139. - * Context: User context only. This function may sleep.
  3140. + * Context: User context only. This function may sleep if pagefaults are
  3141. + * enabled.
  3142. *
  3143. * This macro copies a single simple variable from user space to kernel
  3144. * space. It supports simple types like char and int, but not larger
  3145. @@ -479,7 +483,8 @@
  3146. * @x: Value to copy to user space.
  3147. * @ptr: Destination address, in user space.
  3148. *
  3149. - * Context: User context only. This function may sleep.
  3150. + * Context: User context only. This function may sleep if pagefaults are
  3151. + * enabled.
  3152. *
  3153. * This macro copies a single simple value from kernel space to user
  3154. * space. It supports simple types like char and int, but not larger
  3155. diff -Nur linux-4.1.13.orig/arch/x86/include/asm/uv/uv_bau.h linux-4.1.13/arch/x86/include/asm/uv/uv_bau.h
  3156. --- linux-4.1.13.orig/arch/x86/include/asm/uv/uv_bau.h 2015-11-09 23:34:10.000000000 +0100
  3157. +++ linux-4.1.13/arch/x86/include/asm/uv/uv_bau.h 2015-11-29 09:23:09.525619763 +0100
  3158. @@ -615,9 +615,9 @@
  3159. cycles_t send_message;
  3160. cycles_t period_end;
  3161. cycles_t period_time;
  3162. - spinlock_t uvhub_lock;
  3163. - spinlock_t queue_lock;
  3164. - spinlock_t disable_lock;
  3165. + raw_spinlock_t uvhub_lock;
  3166. + raw_spinlock_t queue_lock;
  3167. + raw_spinlock_t disable_lock;
  3168. /* tunables */
  3169. int max_concurr;
  3170. int max_concurr_const;
  3171. @@ -776,15 +776,15 @@
  3172. * to be lowered below the current 'v'. atomic_add_unless can only stop
  3173. * on equal.
  3174. */
  3175. -static inline int atomic_inc_unless_ge(spinlock_t *lock, atomic_t *v, int u)
  3176. +static inline int atomic_inc_unless_ge(raw_spinlock_t *lock, atomic_t *v, int u)
  3177. {
  3178. - spin_lock(lock);
  3179. + raw_spin_lock(lock);
  3180. if (atomic_read(v) >= u) {
  3181. - spin_unlock(lock);
  3182. + raw_spin_unlock(lock);
  3183. return 0;
  3184. }
  3185. atomic_inc(v);
  3186. - spin_unlock(lock);
  3187. + raw_spin_unlock(lock);
  3188. return 1;
  3189. }
  3190. diff -Nur linux-4.1.13.orig/arch/x86/include/asm/uv/uv_hub.h linux-4.1.13/arch/x86/include/asm/uv/uv_hub.h
  3191. --- linux-4.1.13.orig/arch/x86/include/asm/uv/uv_hub.h 2015-11-09 23:34:10.000000000 +0100
  3192. +++ linux-4.1.13/arch/x86/include/asm/uv/uv_hub.h 2015-11-29 09:23:09.525619763 +0100
  3193. @@ -492,7 +492,7 @@
  3194. unsigned short nr_online_cpus;
  3195. unsigned short pnode;
  3196. short memory_nid;
  3197. - spinlock_t nmi_lock; /* obsolete, see uv_hub_nmi */
  3198. + raw_spinlock_t nmi_lock; /* obsolete, see uv_hub_nmi */
  3199. unsigned long nmi_count; /* obsolete, see uv_hub_nmi */
  3200. };
  3201. extern struct uv_blade_info *uv_blade_info;
  3202. diff -Nur linux-4.1.13.orig/arch/x86/Kconfig linux-4.1.13/arch/x86/Kconfig
  3203. --- linux-4.1.13.orig/arch/x86/Kconfig 2015-11-09 23:34:10.000000000 +0100
  3204. +++ linux-4.1.13/arch/x86/Kconfig 2015-11-29 09:23:09.525619763 +0100
  3205. @@ -22,6 +22,7 @@
  3206. ### Arch settings
  3207. config X86
  3208. def_bool y
  3209. + select HAVE_PREEMPT_LAZY
  3210. select ACPI_SYSTEM_POWER_STATES_SUPPORT if ACPI
  3211. select ARCH_MIGHT_HAVE_ACPI_PDC if ACPI
  3212. select ARCH_HAS_DEBUG_STRICT_USER_COPY_CHECKS
  3213. @@ -203,8 +204,11 @@
  3214. def_bool y
  3215. depends on ISA_DMA_API
  3216. +config RWSEM_GENERIC_SPINLOCK
  3217. + def_bool PREEMPT_RT_FULL
  3218. +
  3219. config RWSEM_XCHGADD_ALGORITHM
  3220. - def_bool y
  3221. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  3222. config GENERIC_CALIBRATE_DELAY
  3223. def_bool y
  3224. @@ -838,7 +842,7 @@
  3225. config MAXSMP
  3226. bool "Enable Maximum number of SMP Processors and NUMA Nodes"
  3227. depends on X86_64 && SMP && DEBUG_KERNEL
  3228. - select CPUMASK_OFFSTACK
  3229. + select CPUMASK_OFFSTACK if !PREEMPT_RT_FULL
  3230. ---help---
  3231. Enable maximum number of CPUS and NUMA Nodes for this architecture.
  3232. If unsure, say N.
  3233. diff -Nur linux-4.1.13.orig/arch/x86/kernel/apic/io_apic.c linux-4.1.13/arch/x86/kernel/apic/io_apic.c
  3234. --- linux-4.1.13.orig/arch/x86/kernel/apic/io_apic.c 2015-11-09 23:34:10.000000000 +0100
  3235. +++ linux-4.1.13/arch/x86/kernel/apic/io_apic.c 2015-11-29 09:23:09.525619763 +0100
  3236. @@ -1891,7 +1891,8 @@
  3237. static inline bool ioapic_irqd_mask(struct irq_data *data, struct irq_cfg *cfg)
  3238. {
  3239. /* If we are moving the irq we need to mask it */
  3240. - if (unlikely(irqd_is_setaffinity_pending(data))) {
  3241. + if (unlikely(irqd_is_setaffinity_pending(data) &&
  3242. + !irqd_irq_inprogress(data))) {
  3243. mask_ioapic(cfg);
  3244. return true;
  3245. }
  3246. diff -Nur linux-4.1.13.orig/arch/x86/kernel/apic/x2apic_uv_x.c linux-4.1.13/arch/x86/kernel/apic/x2apic_uv_x.c
  3247. --- linux-4.1.13.orig/arch/x86/kernel/apic/x2apic_uv_x.c 2015-11-09 23:34:10.000000000 +0100
  3248. +++ linux-4.1.13/arch/x86/kernel/apic/x2apic_uv_x.c 2015-11-29 09:23:09.525619763 +0100
  3249. @@ -949,7 +949,7 @@
  3250. uv_blade_info[blade].pnode = pnode;
  3251. uv_blade_info[blade].nr_possible_cpus = 0;
  3252. uv_blade_info[blade].nr_online_cpus = 0;
  3253. - spin_lock_init(&uv_blade_info[blade].nmi_lock);
  3254. + raw_spin_lock_init(&uv_blade_info[blade].nmi_lock);
  3255. min_pnode = min(pnode, min_pnode);
  3256. max_pnode = max(pnode, max_pnode);
  3257. blade++;
  3258. diff -Nur linux-4.1.13.orig/arch/x86/kernel/asm-offsets.c linux-4.1.13/arch/x86/kernel/asm-offsets.c
  3259. --- linux-4.1.13.orig/arch/x86/kernel/asm-offsets.c 2015-11-09 23:34:10.000000000 +0100
  3260. +++ linux-4.1.13/arch/x86/kernel/asm-offsets.c 2015-11-29 09:23:09.525619763 +0100
  3261. @@ -32,6 +32,7 @@
  3262. OFFSET(TI_flags, thread_info, flags);
  3263. OFFSET(TI_status, thread_info, status);
  3264. OFFSET(TI_addr_limit, thread_info, addr_limit);
  3265. + OFFSET(TI_preempt_lazy_count, thread_info, preempt_lazy_count);
  3266. BLANK();
  3267. OFFSET(crypto_tfm_ctx_offset, crypto_tfm, __crt_ctx);
  3268. @@ -71,4 +72,5 @@
  3269. BLANK();
  3270. DEFINE(PTREGS_SIZE, sizeof(struct pt_regs));
  3271. + DEFINE(_PREEMPT_ENABLED, PREEMPT_ENABLED);
  3272. }
  3273. diff -Nur linux-4.1.13.orig/arch/x86/kernel/cpu/mcheck/mce.c linux-4.1.13/arch/x86/kernel/cpu/mcheck/mce.c
  3274. --- linux-4.1.13.orig/arch/x86/kernel/cpu/mcheck/mce.c 2015-11-09 23:34:10.000000000 +0100
  3275. +++ linux-4.1.13/arch/x86/kernel/cpu/mcheck/mce.c 2015-11-29 09:23:09.529619496 +0100
  3276. @@ -41,6 +41,8 @@
  3277. #include <linux/debugfs.h>
  3278. #include <linux/irq_work.h>
  3279. #include <linux/export.h>
  3280. +#include <linux/jiffies.h>
  3281. +#include <linux/work-simple.h>
  3282. #include <asm/processor.h>
  3283. #include <asm/traps.h>
  3284. @@ -1267,7 +1269,7 @@
  3285. static unsigned long check_interval = INITIAL_CHECK_INTERVAL;
  3286. static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
  3287. -static DEFINE_PER_CPU(struct timer_list, mce_timer);
  3288. +static DEFINE_PER_CPU(struct hrtimer, mce_timer);
  3289. static unsigned long mce_adjust_timer_default(unsigned long interval)
  3290. {
  3291. @@ -1276,32 +1278,18 @@
  3292. static unsigned long (*mce_adjust_timer)(unsigned long interval) = mce_adjust_timer_default;
  3293. -static void __restart_timer(struct timer_list *t, unsigned long interval)
  3294. +static enum hrtimer_restart __restart_timer(struct hrtimer *timer, unsigned long interval)
  3295. {
  3296. - unsigned long when = jiffies + interval;
  3297. - unsigned long flags;
  3298. -
  3299. - local_irq_save(flags);
  3300. -
  3301. - if (timer_pending(t)) {
  3302. - if (time_before(when, t->expires))
  3303. - mod_timer_pinned(t, when);
  3304. - } else {
  3305. - t->expires = round_jiffies(when);
  3306. - add_timer_on(t, smp_processor_id());
  3307. - }
  3308. -
  3309. - local_irq_restore(flags);
  3310. + if (!interval)
  3311. + return HRTIMER_NORESTART;
  3312. + hrtimer_forward_now(timer, ns_to_ktime(jiffies_to_nsecs(interval)));
  3313. + return HRTIMER_RESTART;
  3314. }
  3315. -static void mce_timer_fn(unsigned long data)
  3316. +static enum hrtimer_restart mce_timer_fn(struct hrtimer *timer)
  3317. {
  3318. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  3319. - int cpu = smp_processor_id();
  3320. unsigned long iv;
  3321. - WARN_ON(cpu != data);
  3322. -
  3323. iv = __this_cpu_read(mce_next_interval);
  3324. if (mce_available(this_cpu_ptr(&cpu_info))) {
  3325. @@ -1324,7 +1312,7 @@
  3326. done:
  3327. __this_cpu_write(mce_next_interval, iv);
  3328. - __restart_timer(t, iv);
  3329. + return __restart_timer(timer, iv);
  3330. }
  3331. /*
  3332. @@ -1332,7 +1320,7 @@
  3333. */
  3334. void mce_timer_kick(unsigned long interval)
  3335. {
  3336. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  3337. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  3338. unsigned long iv = __this_cpu_read(mce_next_interval);
  3339. __restart_timer(t, interval);
  3340. @@ -1347,7 +1335,7 @@
  3341. int cpu;
  3342. for_each_online_cpu(cpu)
  3343. - del_timer_sync(&per_cpu(mce_timer, cpu));
  3344. + hrtimer_cancel(&per_cpu(mce_timer, cpu));
  3345. }
  3346. static void mce_do_trigger(struct work_struct *work)
  3347. @@ -1357,6 +1345,56 @@
  3348. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  3349. +static void __mce_notify_work(struct swork_event *event)
  3350. +{
  3351. + /* Not more than two messages every minute */
  3352. + static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  3353. +
  3354. + /* wake processes polling /dev/mcelog */
  3355. + wake_up_interruptible(&mce_chrdev_wait);
  3356. +
  3357. + /*
  3358. + * There is no risk of missing notifications because
  3359. + * work_pending is always cleared before the function is
  3360. + * executed.
  3361. + */
  3362. + if (mce_helper[0] && !work_pending(&mce_trigger_work))
  3363. + schedule_work(&mce_trigger_work);
  3364. +
  3365. + if (__ratelimit(&ratelimit))
  3366. + pr_info(HW_ERR "Machine check events logged\n");
  3367. +}
  3368. +
  3369. +#ifdef CONFIG_PREEMPT_RT_FULL
  3370. +static bool notify_work_ready __read_mostly;
  3371. +static struct swork_event notify_work;
  3372. +
  3373. +static int mce_notify_work_init(void)
  3374. +{
  3375. + int err;
  3376. +
  3377. + err = swork_get();
  3378. + if (err)
  3379. + return err;
  3380. +
  3381. + INIT_SWORK(&notify_work, __mce_notify_work);
  3382. + notify_work_ready = true;
  3383. + return 0;
  3384. +}
  3385. +
  3386. +static void mce_notify_work(void)
  3387. +{
  3388. + if (notify_work_ready)
  3389. + swork_queue(&notify_work);
  3390. +}
  3391. +#else
  3392. +static void mce_notify_work(void)
  3393. +{
  3394. + __mce_notify_work(NULL);
  3395. +}
  3396. +static inline int mce_notify_work_init(void) { return 0; }
  3397. +#endif
  3398. +
  3399. /*
  3400. * Notify the user(s) about new machine check events.
  3401. * Can be called from interrupt context, but not from machine check/NMI
  3402. @@ -1364,19 +1402,8 @@
  3403. */
  3404. int mce_notify_irq(void)
  3405. {
  3406. - /* Not more than two messages every minute */
  3407. - static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  3408. -
  3409. if (test_and_clear_bit(0, &mce_need_notify)) {
  3410. - /* wake processes polling /dev/mcelog */
  3411. - wake_up_interruptible(&mce_chrdev_wait);
  3412. -
  3413. - if (mce_helper[0])
  3414. - schedule_work(&mce_trigger_work);
  3415. -
  3416. - if (__ratelimit(&ratelimit))
  3417. - pr_info(HW_ERR "Machine check events logged\n");
  3418. -
  3419. + mce_notify_work();
  3420. return 1;
  3421. }
  3422. return 0;
  3423. @@ -1649,7 +1676,7 @@
  3424. }
  3425. }
  3426. -static void mce_start_timer(unsigned int cpu, struct timer_list *t)
  3427. +static void mce_start_timer(unsigned int cpu, struct hrtimer *t)
  3428. {
  3429. unsigned long iv = check_interval * HZ;
  3430. @@ -1658,16 +1685,17 @@
  3431. per_cpu(mce_next_interval, cpu) = iv;
  3432. - t->expires = round_jiffies(jiffies + iv);
  3433. - add_timer_on(t, cpu);
  3434. + hrtimer_start_range_ns(t, ns_to_ktime(jiffies_to_usecs(iv) * 1000ULL),
  3435. + 0, HRTIMER_MODE_REL_PINNED);
  3436. }
  3437. static void __mcheck_cpu_init_timer(void)
  3438. {
  3439. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  3440. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  3441. unsigned int cpu = smp_processor_id();
  3442. - setup_timer(t, mce_timer_fn, cpu);
  3443. + hrtimer_init(t, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  3444. + t->function = mce_timer_fn;
  3445. mce_start_timer(cpu, t);
  3446. }
  3447. @@ -2345,6 +2373,8 @@
  3448. if (!mce_available(raw_cpu_ptr(&cpu_info)))
  3449. return;
  3450. + hrtimer_cancel(this_cpu_ptr(&mce_timer));
  3451. +
  3452. if (!(action & CPU_TASKS_FROZEN))
  3453. cmci_clear();
  3454. for (i = 0; i < mca_cfg.banks; i++) {
  3455. @@ -2371,6 +2401,7 @@
  3456. if (b->init)
  3457. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  3458. }
  3459. + __mcheck_cpu_init_timer();
  3460. }
  3461. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  3462. @@ -2378,7 +2409,6 @@
  3463. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  3464. {
  3465. unsigned int cpu = (unsigned long)hcpu;
  3466. - struct timer_list *t = &per_cpu(mce_timer, cpu);
  3467. switch (action & ~CPU_TASKS_FROZEN) {
  3468. case CPU_ONLINE:
  3469. @@ -2398,11 +2428,9 @@
  3470. break;
  3471. case CPU_DOWN_PREPARE:
  3472. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  3473. - del_timer_sync(t);
  3474. break;
  3475. case CPU_DOWN_FAILED:
  3476. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  3477. - mce_start_timer(cpu, t);
  3478. break;
  3479. }
  3480. @@ -2441,6 +2469,10 @@
  3481. goto err_out;
  3482. }
  3483. + err = mce_notify_work_init();
  3484. + if (err)
  3485. + goto err_out;
  3486. +
  3487. if (!zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL)) {
  3488. err = -ENOMEM;
  3489. goto err_out;
  3490. diff -Nur linux-4.1.13.orig/arch/x86/kernel/dumpstack_32.c linux-4.1.13/arch/x86/kernel/dumpstack_32.c
  3491. --- linux-4.1.13.orig/arch/x86/kernel/dumpstack_32.c 2015-11-09 23:34:10.000000000 +0100
  3492. +++ linux-4.1.13/arch/x86/kernel/dumpstack_32.c 2015-11-29 09:23:09.529619496 +0100
  3493. @@ -42,7 +42,7 @@
  3494. unsigned long *stack, unsigned long bp,
  3495. const struct stacktrace_ops *ops, void *data)
  3496. {
  3497. - const unsigned cpu = get_cpu();
  3498. + const unsigned cpu = get_cpu_light();
  3499. int graph = 0;
  3500. u32 *prev_esp;
  3501. @@ -86,7 +86,7 @@
  3502. break;
  3503. touch_nmi_watchdog();
  3504. }
  3505. - put_cpu();
  3506. + put_cpu_light();
  3507. }
  3508. EXPORT_SYMBOL(dump_trace);
  3509. diff -Nur linux-4.1.13.orig/arch/x86/kernel/dumpstack_64.c linux-4.1.13/arch/x86/kernel/dumpstack_64.c
  3510. --- linux-4.1.13.orig/arch/x86/kernel/dumpstack_64.c 2015-11-09 23:34:10.000000000 +0100
  3511. +++ linux-4.1.13/arch/x86/kernel/dumpstack_64.c 2015-11-29 09:23:09.529619496 +0100
  3512. @@ -152,7 +152,7 @@
  3513. unsigned long *stack, unsigned long bp,
  3514. const struct stacktrace_ops *ops, void *data)
  3515. {
  3516. - const unsigned cpu = get_cpu();
  3517. + const unsigned cpu = get_cpu_light();
  3518. struct thread_info *tinfo;
  3519. unsigned long *irq_stack = (unsigned long *)per_cpu(irq_stack_ptr, cpu);
  3520. unsigned long dummy;
  3521. @@ -241,7 +241,7 @@
  3522. * This handles the process stack:
  3523. */
  3524. bp = ops->walk_stack(tinfo, stack, bp, ops, data, NULL, &graph);
  3525. - put_cpu();
  3526. + put_cpu_light();
  3527. }
  3528. EXPORT_SYMBOL(dump_trace);
  3529. @@ -255,7 +255,7 @@
  3530. int cpu;
  3531. int i;
  3532. - preempt_disable();
  3533. + migrate_disable();
  3534. cpu = smp_processor_id();
  3535. irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
  3536. @@ -291,7 +291,7 @@
  3537. pr_cont(" %016lx", *stack++);
  3538. touch_nmi_watchdog();
  3539. }
  3540. - preempt_enable();
  3541. + migrate_enable();
  3542. pr_cont("\n");
  3543. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  3544. diff -Nur linux-4.1.13.orig/arch/x86/kernel/entry_32.S linux-4.1.13/arch/x86/kernel/entry_32.S
  3545. --- linux-4.1.13.orig/arch/x86/kernel/entry_32.S 2015-11-09 23:34:10.000000000 +0100
  3546. +++ linux-4.1.13/arch/x86/kernel/entry_32.S 2015-11-29 09:23:09.529619496 +0100
  3547. @@ -359,8 +359,24 @@
  3548. ENTRY(resume_kernel)
  3549. DISABLE_INTERRUPTS(CLBR_ANY)
  3550. need_resched:
  3551. + # preempt count == 0 + NEED_RS set?
  3552. cmpl $0,PER_CPU_VAR(__preempt_count)
  3553. +#ifndef CONFIG_PREEMPT_LAZY
  3554. jnz restore_all
  3555. +#else
  3556. + jz test_int_off
  3557. +
  3558. + # atleast preempt count == 0 ?
  3559. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  3560. + jne restore_all
  3561. +
  3562. + cmpl $0,TI_preempt_lazy_count(%ebp) # non-zero preempt_lazy_count ?
  3563. + jnz restore_all
  3564. +
  3565. + testl $_TIF_NEED_RESCHED_LAZY, TI_flags(%ebp)
  3566. + jz restore_all
  3567. +test_int_off:
  3568. +#endif
  3569. testl $X86_EFLAGS_IF,PT_EFLAGS(%esp) # interrupts off (exception path) ?
  3570. jz restore_all
  3571. call preempt_schedule_irq
  3572. @@ -594,7 +610,7 @@
  3573. ALIGN
  3574. RING0_PTREGS_FRAME # can't unwind into user space anyway
  3575. work_pending:
  3576. - testb $_TIF_NEED_RESCHED, %cl
  3577. + testl $_TIF_NEED_RESCHED_MASK, %ecx
  3578. jz work_notifysig
  3579. work_resched:
  3580. call schedule
  3581. @@ -607,7 +623,7 @@
  3582. andl $_TIF_WORK_MASK, %ecx # is there any work to be done other
  3583. # than syscall tracing?
  3584. jz restore_all
  3585. - testb $_TIF_NEED_RESCHED, %cl
  3586. + testl $_TIF_NEED_RESCHED_MASK, %ecx
  3587. jnz work_resched
  3588. work_notifysig: # deal with pending signals and
  3589. diff -Nur linux-4.1.13.orig/arch/x86/kernel/entry_64.S linux-4.1.13/arch/x86/kernel/entry_64.S
  3590. --- linux-4.1.13.orig/arch/x86/kernel/entry_64.S 2015-11-09 23:34:10.000000000 +0100
  3591. +++ linux-4.1.13/arch/x86/kernel/entry_64.S 2015-11-29 09:23:09.529619496 +0100
  3592. @@ -370,8 +370,8 @@
  3593. /* First do a reschedule test. */
  3594. /* edx: work, edi: workmask */
  3595. int_careful:
  3596. - bt $TIF_NEED_RESCHED,%edx
  3597. - jnc int_very_careful
  3598. + testl $_TIF_NEED_RESCHED_MASK,%edx
  3599. + jz int_very_careful
  3600. TRACE_IRQS_ON
  3601. ENABLE_INTERRUPTS(CLBR_NONE)
  3602. pushq_cfi %rdi
  3603. @@ -776,7 +776,23 @@
  3604. bt $9,EFLAGS(%rsp) /* interrupts were off? */
  3605. jnc 1f
  3606. 0: cmpl $0,PER_CPU_VAR(__preempt_count)
  3607. +#ifndef CONFIG_PREEMPT_LAZY
  3608. jnz 1f
  3609. +#else
  3610. + jz do_preempt_schedule_irq
  3611. +
  3612. + # atleast preempt count == 0 ?
  3613. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  3614. + jnz 1f
  3615. +
  3616. + GET_THREAD_INFO(%rcx)
  3617. + cmpl $0, TI_preempt_lazy_count(%rcx)
  3618. + jnz 1f
  3619. +
  3620. + bt $TIF_NEED_RESCHED_LAZY,TI_flags(%rcx)
  3621. + jnc 1f
  3622. +do_preempt_schedule_irq:
  3623. +#endif
  3624. call preempt_schedule_irq
  3625. jmp 0b
  3626. 1:
  3627. @@ -844,8 +860,8 @@
  3628. /* edi: workmask, edx: work */
  3629. retint_careful:
  3630. CFI_RESTORE_STATE
  3631. - bt $TIF_NEED_RESCHED,%edx
  3632. - jnc retint_signal
  3633. + testl $_TIF_NEED_RESCHED_MASK,%edx
  3634. + jz retint_signal
  3635. TRACE_IRQS_ON
  3636. ENABLE_INTERRUPTS(CLBR_NONE)
  3637. pushq_cfi %rdi
  3638. @@ -1118,6 +1134,7 @@
  3639. jmp 2b
  3640. .previous
  3641. +#ifndef CONFIG_PREEMPT_RT_FULL
  3642. /* Call softirq on interrupt stack. Interrupts are off. */
  3643. ENTRY(do_softirq_own_stack)
  3644. CFI_STARTPROC
  3645. @@ -1137,6 +1154,7 @@
  3646. ret
  3647. CFI_ENDPROC
  3648. END(do_softirq_own_stack)
  3649. +#endif
  3650. #ifdef CONFIG_XEN
  3651. idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
  3652. diff -Nur linux-4.1.13.orig/arch/x86/kernel/irq_32.c linux-4.1.13/arch/x86/kernel/irq_32.c
  3653. --- linux-4.1.13.orig/arch/x86/kernel/irq_32.c 2015-11-09 23:34:10.000000000 +0100
  3654. +++ linux-4.1.13/arch/x86/kernel/irq_32.c 2015-11-29 09:23:09.529619496 +0100
  3655. @@ -135,6 +135,7 @@
  3656. cpu, per_cpu(hardirq_stack, cpu), per_cpu(softirq_stack, cpu));
  3657. }
  3658. +#ifndef CONFIG_PREEMPT_RT_FULL
  3659. void do_softirq_own_stack(void)
  3660. {
  3661. struct thread_info *curstk;
  3662. @@ -153,6 +154,7 @@
  3663. call_on_stack(__do_softirq, isp);
  3664. }
  3665. +#endif
  3666. bool handle_irq(unsigned irq, struct pt_regs *regs)
  3667. {
  3668. diff -Nur linux-4.1.13.orig/arch/x86/kernel/process_32.c linux-4.1.13/arch/x86/kernel/process_32.c
  3669. --- linux-4.1.13.orig/arch/x86/kernel/process_32.c 2015-11-09 23:34:10.000000000 +0100
  3670. +++ linux-4.1.13/arch/x86/kernel/process_32.c 2015-11-29 09:23:09.529619496 +0100
  3671. @@ -35,6 +35,7 @@
  3672. #include <linux/uaccess.h>
  3673. #include <linux/io.h>
  3674. #include <linux/kdebug.h>
  3675. +#include <linux/highmem.h>
  3676. #include <asm/pgtable.h>
  3677. #include <asm/ldt.h>
  3678. @@ -210,6 +211,35 @@
  3679. }
  3680. EXPORT_SYMBOL_GPL(start_thread);
  3681. +#ifdef CONFIG_PREEMPT_RT_FULL
  3682. +static void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  3683. +{
  3684. + int i;
  3685. +
  3686. + /*
  3687. + * Clear @prev's kmap_atomic mappings
  3688. + */
  3689. + for (i = 0; i < prev_p->kmap_idx; i++) {
  3690. + int idx = i + KM_TYPE_NR * smp_processor_id();
  3691. + pte_t *ptep = kmap_pte - idx;
  3692. +
  3693. + kpte_clear_flush(ptep, __fix_to_virt(FIX_KMAP_BEGIN + idx));
  3694. + }
  3695. + /*
  3696. + * Restore @next_p's kmap_atomic mappings
  3697. + */
  3698. + for (i = 0; i < next_p->kmap_idx; i++) {
  3699. + int idx = i + KM_TYPE_NR * smp_processor_id();
  3700. +
  3701. + if (!pte_none(next_p->kmap_pte[i]))
  3702. + set_pte(kmap_pte - idx, next_p->kmap_pte[i]);
  3703. + }
  3704. +}
  3705. +#else
  3706. +static inline void
  3707. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  3708. +#endif
  3709. +
  3710. /*
  3711. * switch_to(x,y) should switch tasks from x to y.
  3712. @@ -292,6 +322,8 @@
  3713. task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
  3714. __switch_to_xtra(prev_p, next_p, tss);
  3715. + switch_kmaps(prev_p, next_p);
  3716. +
  3717. /*
  3718. * Leave lazy mode, flushing any hypercalls made here.
  3719. * This must be done before restoring TLS segments so
  3720. diff -Nur linux-4.1.13.orig/arch/x86/kernel/signal.c linux-4.1.13/arch/x86/kernel/signal.c
  3721. --- linux-4.1.13.orig/arch/x86/kernel/signal.c 2015-11-09 23:34:10.000000000 +0100
  3722. +++ linux-4.1.13/arch/x86/kernel/signal.c 2015-11-29 09:23:09.529619496 +0100
  3723. @@ -723,6 +723,14 @@
  3724. {
  3725. user_exit();
  3726. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  3727. + if (unlikely(current->forced_info.si_signo)) {
  3728. + struct task_struct *t = current;
  3729. + force_sig_info(t->forced_info.si_signo, &t->forced_info, t);
  3730. + t->forced_info.si_signo = 0;
  3731. + }
  3732. +#endif
  3733. +
  3734. if (thread_info_flags & _TIF_UPROBE)
  3735. uprobe_notify_resume(regs);
  3736. diff -Nur linux-4.1.13.orig/arch/x86/kernel/traps.c linux-4.1.13/arch/x86/kernel/traps.c
  3737. --- linux-4.1.13.orig/arch/x86/kernel/traps.c 2015-11-09 23:34:10.000000000 +0100
  3738. +++ linux-4.1.13/arch/x86/kernel/traps.c 2015-11-29 09:23:09.529619496 +0100
  3739. @@ -88,9 +88,21 @@
  3740. local_irq_enable();
  3741. }
  3742. -static inline void preempt_conditional_sti(struct pt_regs *regs)
  3743. +static inline void conditional_sti_ist(struct pt_regs *regs)
  3744. {
  3745. +#ifdef CONFIG_X86_64
  3746. + /*
  3747. + * X86_64 uses a per CPU stack on the IST for certain traps
  3748. + * like int3. The task can not be preempted when using one
  3749. + * of these stacks, thus preemption must be disabled, otherwise
  3750. + * the stack can be corrupted if the task is scheduled out,
  3751. + * and another task comes in and uses this stack.
  3752. + *
  3753. + * On x86_32 the task keeps its own stack and it is OK if the
  3754. + * task schedules out.
  3755. + */
  3756. preempt_count_inc();
  3757. +#endif
  3758. if (regs->flags & X86_EFLAGS_IF)
  3759. local_irq_enable();
  3760. }
  3761. @@ -101,11 +113,13 @@
  3762. local_irq_disable();
  3763. }
  3764. -static inline void preempt_conditional_cli(struct pt_regs *regs)
  3765. +static inline void conditional_cli_ist(struct pt_regs *regs)
  3766. {
  3767. if (regs->flags & X86_EFLAGS_IF)
  3768. local_irq_disable();
  3769. +#ifdef CONFIG_X86_64
  3770. preempt_count_dec();
  3771. +#endif
  3772. }
  3773. enum ctx_state ist_enter(struct pt_regs *regs)
  3774. @@ -536,9 +550,9 @@
  3775. * as we may switch to the interrupt stack.
  3776. */
  3777. debug_stack_usage_inc();
  3778. - preempt_conditional_sti(regs);
  3779. + conditional_sti_ist(regs);
  3780. do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, error_code, NULL);
  3781. - preempt_conditional_cli(regs);
  3782. + conditional_cli_ist(regs);
  3783. debug_stack_usage_dec();
  3784. exit:
  3785. ist_exit(regs, prev_state);
  3786. @@ -668,12 +682,12 @@
  3787. debug_stack_usage_inc();
  3788. /* It's safe to allow irq's after DR6 has been saved */
  3789. - preempt_conditional_sti(regs);
  3790. + conditional_sti_ist(regs);
  3791. if (v8086_mode(regs)) {
  3792. handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
  3793. X86_TRAP_DB);
  3794. - preempt_conditional_cli(regs);
  3795. + conditional_cli_ist(regs);
  3796. debug_stack_usage_dec();
  3797. goto exit;
  3798. }
  3799. @@ -693,7 +707,7 @@
  3800. si_code = get_si_code(tsk->thread.debugreg6);
  3801. if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
  3802. send_sigtrap(tsk, regs, error_code, si_code);
  3803. - preempt_conditional_cli(regs);
  3804. + conditional_cli_ist(regs);
  3805. debug_stack_usage_dec();
  3806. exit:
  3807. diff -Nur linux-4.1.13.orig/arch/x86/kvm/lapic.c linux-4.1.13/arch/x86/kvm/lapic.c
  3808. --- linux-4.1.13.orig/arch/x86/kvm/lapic.c 2015-11-09 23:34:10.000000000 +0100
  3809. +++ linux-4.1.13/arch/x86/kvm/lapic.c 2015-11-29 09:23:09.529619496 +0100
  3810. @@ -1104,7 +1104,7 @@
  3811. static void apic_timer_expired(struct kvm_lapic *apic)
  3812. {
  3813. struct kvm_vcpu *vcpu = apic->vcpu;
  3814. - wait_queue_head_t *q = &vcpu->wq;
  3815. + struct swait_head *q = &vcpu->wq;
  3816. struct kvm_timer *ktimer = &apic->lapic_timer;
  3817. if (atomic_read(&apic->lapic_timer.pending))
  3818. @@ -1113,8 +1113,8 @@
  3819. atomic_inc(&apic->lapic_timer.pending);
  3820. kvm_set_pending_timer(vcpu);
  3821. - if (waitqueue_active(q))
  3822. - wake_up_interruptible(q);
  3823. + if (swaitqueue_active(q))
  3824. + swait_wake_interruptible(q);
  3825. if (apic_lvtt_tscdeadline(apic))
  3826. ktimer->expired_tscdeadline = ktimer->tscdeadline;
  3827. @@ -1167,8 +1167,36 @@
  3828. __delay(tsc_deadline - guest_tsc);
  3829. }
  3830. +static enum hrtimer_restart apic_timer_fn(struct hrtimer *data);
  3831. +
  3832. +static void __apic_timer_expired(struct hrtimer *data)
  3833. +{
  3834. + int ret, i = 0;
  3835. + enum hrtimer_restart r;
  3836. + struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
  3837. +
  3838. + r = apic_timer_fn(data);
  3839. +
  3840. + if (r == HRTIMER_RESTART) {
  3841. + do {
  3842. + ret = hrtimer_start_expires(data, HRTIMER_MODE_ABS);
  3843. + if (ret == -ETIME)
  3844. + hrtimer_add_expires_ns(&ktimer->timer,
  3845. + ktimer->period);
  3846. + i++;
  3847. + } while (ret == -ETIME && i < 10);
  3848. +
  3849. + if (ret == -ETIME) {
  3850. + printk_once(KERN_ERR "%s: failed to reprogram timer\n",
  3851. + __func__);
  3852. + WARN_ON_ONCE(1);
  3853. + }
  3854. + }
  3855. +}
  3856. +
  3857. static void start_apic_timer(struct kvm_lapic *apic)
  3858. {
  3859. + int ret;
  3860. ktime_t now;
  3861. atomic_set(&apic->lapic_timer.pending, 0);
  3862. @@ -1199,9 +1227,11 @@
  3863. }
  3864. }
  3865. - hrtimer_start(&apic->lapic_timer.timer,
  3866. + ret = hrtimer_start(&apic->lapic_timer.timer,
  3867. ktime_add_ns(now, apic->lapic_timer.period),
  3868. HRTIMER_MODE_ABS);
  3869. + if (ret == -ETIME)
  3870. + __apic_timer_expired(&apic->lapic_timer.timer);
  3871. apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
  3872. PRIx64 ", "
  3873. @@ -1233,8 +1263,10 @@
  3874. do_div(ns, this_tsc_khz);
  3875. expire = ktime_add_ns(now, ns);
  3876. expire = ktime_sub_ns(expire, lapic_timer_advance_ns);
  3877. - hrtimer_start(&apic->lapic_timer.timer,
  3878. + ret = hrtimer_start(&apic->lapic_timer.timer,
  3879. expire, HRTIMER_MODE_ABS);
  3880. + if (ret == -ETIME)
  3881. + __apic_timer_expired(&apic->lapic_timer.timer);
  3882. } else
  3883. apic_timer_expired(apic);
  3884. @@ -1707,6 +1739,7 @@
  3885. hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
  3886. HRTIMER_MODE_ABS);
  3887. apic->lapic_timer.timer.function = apic_timer_fn;
  3888. + apic->lapic_timer.timer.irqsafe = 1;
  3889. /*
  3890. * APIC is created enabled. This will prevent kvm_lapic_set_base from
  3891. @@ -1834,7 +1867,8 @@
  3892. timer = &vcpu->arch.apic->lapic_timer.timer;
  3893. if (hrtimer_cancel(timer))
  3894. - hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  3895. + if (hrtimer_start_expires(timer, HRTIMER_MODE_ABS) == -ETIME)
  3896. + __apic_timer_expired(timer);
  3897. }
  3898. /*
  3899. diff -Nur linux-4.1.13.orig/arch/x86/kvm/x86.c linux-4.1.13/arch/x86/kvm/x86.c
  3900. --- linux-4.1.13.orig/arch/x86/kvm/x86.c 2015-11-09 23:34:10.000000000 +0100
  3901. +++ linux-4.1.13/arch/x86/kvm/x86.c 2015-11-29 09:23:09.529619496 +0100
  3902. @@ -5813,6 +5813,13 @@
  3903. goto out;
  3904. }
  3905. +#ifdef CONFIG_PREEMPT_RT_FULL
  3906. + if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  3907. + printk(KERN_ERR "RT requires X86_FEATURE_CONSTANT_TSC\n");
  3908. + return -EOPNOTSUPP;
  3909. + }
  3910. +#endif
  3911. +
  3912. r = kvm_mmu_module_init();
  3913. if (r)
  3914. goto out_free_percpu;
  3915. diff -Nur linux-4.1.13.orig/arch/x86/lib/usercopy_32.c linux-4.1.13/arch/x86/lib/usercopy_32.c
  3916. --- linux-4.1.13.orig/arch/x86/lib/usercopy_32.c 2015-11-09 23:34:10.000000000 +0100
  3917. +++ linux-4.1.13/arch/x86/lib/usercopy_32.c 2015-11-29 09:23:09.533619230 +0100
  3918. @@ -647,7 +647,8 @@
  3919. * @from: Source address, in kernel space.
  3920. * @n: Number of bytes to copy.
  3921. *
  3922. - * Context: User context only. This function may sleep.
  3923. + * Context: User context only. This function may sleep if pagefaults are
  3924. + * enabled.
  3925. *
  3926. * Copy data from kernel space to user space.
  3927. *
  3928. @@ -668,7 +669,8 @@
  3929. * @from: Source address, in user space.
  3930. * @n: Number of bytes to copy.
  3931. *
  3932. - * Context: User context only. This function may sleep.
  3933. + * Context: User context only. This function may sleep if pagefaults are
  3934. + * enabled.
  3935. *
  3936. * Copy data from user space to kernel space.
  3937. *
  3938. diff -Nur linux-4.1.13.orig/arch/x86/mm/fault.c linux-4.1.13/arch/x86/mm/fault.c
  3939. --- linux-4.1.13.orig/arch/x86/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  3940. +++ linux-4.1.13/arch/x86/mm/fault.c 2015-11-29 09:23:09.533619230 +0100
  3941. @@ -13,6 +13,7 @@
  3942. #include <linux/hugetlb.h> /* hstate_index_to_shift */
  3943. #include <linux/prefetch.h> /* prefetchw */
  3944. #include <linux/context_tracking.h> /* exception_enter(), ... */
  3945. +#include <linux/uaccess.h> /* faulthandler_disabled() */
  3946. #include <asm/traps.h> /* dotraplinkage, ... */
  3947. #include <asm/pgalloc.h> /* pgd_*(), ... */
  3948. @@ -1126,9 +1127,9 @@
  3949. /*
  3950. * If we're in an interrupt, have no user context or are running
  3951. - * in an atomic region then we must not take the fault:
  3952. + * in a region with pagefaults disabled then we must not take the fault
  3953. */
  3954. - if (unlikely(in_atomic() || !mm)) {
  3955. + if (unlikely(faulthandler_disabled() || !mm)) {
  3956. bad_area_nosemaphore(regs, error_code, address);
  3957. return;
  3958. }
  3959. diff -Nur linux-4.1.13.orig/arch/x86/mm/highmem_32.c linux-4.1.13/arch/x86/mm/highmem_32.c
  3960. --- linux-4.1.13.orig/arch/x86/mm/highmem_32.c 2015-11-09 23:34:10.000000000 +0100
  3961. +++ linux-4.1.13/arch/x86/mm/highmem_32.c 2015-11-29 09:23:09.533619230 +0100
  3962. @@ -32,10 +32,11 @@
  3963. */
  3964. void *kmap_atomic_prot(struct page *page, pgprot_t prot)
  3965. {
  3966. + pte_t pte = mk_pte(page, prot);
  3967. unsigned long vaddr;
  3968. int idx, type;
  3969. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  3970. + preempt_disable_nort();
  3971. pagefault_disable();
  3972. if (!PageHighMem(page))
  3973. @@ -45,7 +46,10 @@
  3974. idx = type + KM_TYPE_NR*smp_processor_id();
  3975. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  3976. BUG_ON(!pte_none(*(kmap_pte-idx)));
  3977. - set_pte(kmap_pte-idx, mk_pte(page, prot));
  3978. +#ifdef CONFIG_PREEMPT_RT_FULL
  3979. + current->kmap_pte[type] = pte;
  3980. +#endif
  3981. + set_pte(kmap_pte-idx, pte);
  3982. arch_flush_lazy_mmu_mode();
  3983. return (void *)vaddr;
  3984. @@ -88,6 +92,9 @@
  3985. * is a bad idea also, in case the page changes cacheability
  3986. * attributes or becomes a protected page in a hypervisor.
  3987. */
  3988. +#ifdef CONFIG_PREEMPT_RT_FULL
  3989. + current->kmap_pte[type] = __pte(0);
  3990. +#endif
  3991. kpte_clear_flush(kmap_pte-idx, vaddr);
  3992. kmap_atomic_idx_pop();
  3993. arch_flush_lazy_mmu_mode();
  3994. @@ -100,6 +107,7 @@
  3995. #endif
  3996. pagefault_enable();
  3997. + preempt_enable_nort();
  3998. }
  3999. EXPORT_SYMBOL(__kunmap_atomic);
  4000. diff -Nur linux-4.1.13.orig/arch/x86/mm/iomap_32.c linux-4.1.13/arch/x86/mm/iomap_32.c
  4001. --- linux-4.1.13.orig/arch/x86/mm/iomap_32.c 2015-11-09 23:34:10.000000000 +0100
  4002. +++ linux-4.1.13/arch/x86/mm/iomap_32.c 2015-11-29 09:23:09.533619230 +0100
  4003. @@ -56,15 +56,22 @@
  4004. void *kmap_atomic_prot_pfn(unsigned long pfn, pgprot_t prot)
  4005. {
  4006. + pte_t pte = pfn_pte(pfn, prot);
  4007. unsigned long vaddr;
  4008. int idx, type;
  4009. + preempt_disable();
  4010. pagefault_disable();
  4011. type = kmap_atomic_idx_push();
  4012. idx = type + KM_TYPE_NR * smp_processor_id();
  4013. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  4014. - set_pte(kmap_pte - idx, pfn_pte(pfn, prot));
  4015. + WARN_ON(!pte_none(*(kmap_pte - idx)));
  4016. +
  4017. +#ifdef CONFIG_PREEMPT_RT_FULL
  4018. + current->kmap_pte[type] = pte;
  4019. +#endif
  4020. + set_pte(kmap_pte - idx, pte);
  4021. arch_flush_lazy_mmu_mode();
  4022. return (void *)vaddr;
  4023. @@ -112,10 +119,14 @@
  4024. * is a bad idea also, in case the page changes cacheability
  4025. * attributes or becomes a protected page in a hypervisor.
  4026. */
  4027. +#ifdef CONFIG_PREEMPT_RT_FULL
  4028. + current->kmap_pte[type] = __pte(0);
  4029. +#endif
  4030. kpte_clear_flush(kmap_pte-idx, vaddr);
  4031. kmap_atomic_idx_pop();
  4032. }
  4033. pagefault_enable();
  4034. + preempt_enable();
  4035. }
  4036. EXPORT_SYMBOL_GPL(iounmap_atomic);
  4037. diff -Nur linux-4.1.13.orig/arch/x86/platform/uv/tlb_uv.c linux-4.1.13/arch/x86/platform/uv/tlb_uv.c
  4038. --- linux-4.1.13.orig/arch/x86/platform/uv/tlb_uv.c 2015-11-09 23:34:10.000000000 +0100
  4039. +++ linux-4.1.13/arch/x86/platform/uv/tlb_uv.c 2015-11-29 09:23:09.533619230 +0100
  4040. @@ -714,9 +714,9 @@
  4041. quiesce_local_uvhub(hmaster);
  4042. - spin_lock(&hmaster->queue_lock);
  4043. + raw_spin_lock(&hmaster->queue_lock);
  4044. reset_with_ipi(&bau_desc->distribution, bcp);
  4045. - spin_unlock(&hmaster->queue_lock);
  4046. + raw_spin_unlock(&hmaster->queue_lock);
  4047. end_uvhub_quiesce(hmaster);
  4048. @@ -736,9 +736,9 @@
  4049. quiesce_local_uvhub(hmaster);
  4050. - spin_lock(&hmaster->queue_lock);
  4051. + raw_spin_lock(&hmaster->queue_lock);
  4052. reset_with_ipi(&bau_desc->distribution, bcp);
  4053. - spin_unlock(&hmaster->queue_lock);
  4054. + raw_spin_unlock(&hmaster->queue_lock);
  4055. end_uvhub_quiesce(hmaster);
  4056. @@ -759,7 +759,7 @@
  4057. cycles_t tm1;
  4058. hmaster = bcp->uvhub_master;
  4059. - spin_lock(&hmaster->disable_lock);
  4060. + raw_spin_lock(&hmaster->disable_lock);
  4061. if (!bcp->baudisabled) {
  4062. stat->s_bau_disabled++;
  4063. tm1 = get_cycles();
  4064. @@ -772,7 +772,7 @@
  4065. }
  4066. }
  4067. }
  4068. - spin_unlock(&hmaster->disable_lock);
  4069. + raw_spin_unlock(&hmaster->disable_lock);
  4070. }
  4071. static void count_max_concurr(int stat, struct bau_control *bcp,
  4072. @@ -835,7 +835,7 @@
  4073. */
  4074. static void uv1_throttle(struct bau_control *hmaster, struct ptc_stats *stat)
  4075. {
  4076. - spinlock_t *lock = &hmaster->uvhub_lock;
  4077. + raw_spinlock_t *lock = &hmaster->uvhub_lock;
  4078. atomic_t *v;
  4079. v = &hmaster->active_descriptor_count;
  4080. @@ -968,7 +968,7 @@
  4081. struct bau_control *hmaster;
  4082. hmaster = bcp->uvhub_master;
  4083. - spin_lock(&hmaster->disable_lock);
  4084. + raw_spin_lock(&hmaster->disable_lock);
  4085. if (bcp->baudisabled && (get_cycles() >= bcp->set_bau_on_time)) {
  4086. stat->s_bau_reenabled++;
  4087. for_each_present_cpu(tcpu) {
  4088. @@ -980,10 +980,10 @@
  4089. tbcp->period_giveups = 0;
  4090. }
  4091. }
  4092. - spin_unlock(&hmaster->disable_lock);
  4093. + raw_spin_unlock(&hmaster->disable_lock);
  4094. return 0;
  4095. }
  4096. - spin_unlock(&hmaster->disable_lock);
  4097. + raw_spin_unlock(&hmaster->disable_lock);
  4098. return -1;
  4099. }
  4100. @@ -1901,9 +1901,9 @@
  4101. bcp->cong_reps = congested_reps;
  4102. bcp->disabled_period = sec_2_cycles(disabled_period);
  4103. bcp->giveup_limit = giveup_limit;
  4104. - spin_lock_init(&bcp->queue_lock);
  4105. - spin_lock_init(&bcp->uvhub_lock);
  4106. - spin_lock_init(&bcp->disable_lock);
  4107. + raw_spin_lock_init(&bcp->queue_lock);
  4108. + raw_spin_lock_init(&bcp->uvhub_lock);
  4109. + raw_spin_lock_init(&bcp->disable_lock);
  4110. }
  4111. }
  4112. diff -Nur linux-4.1.13.orig/arch/x86/platform/uv/uv_time.c linux-4.1.13/arch/x86/platform/uv/uv_time.c
  4113. --- linux-4.1.13.orig/arch/x86/platform/uv/uv_time.c 2015-11-09 23:34:10.000000000 +0100
  4114. +++ linux-4.1.13/arch/x86/platform/uv/uv_time.c 2015-11-29 09:23:09.533619230 +0100
  4115. @@ -58,7 +58,7 @@
  4116. /* There is one of these allocated per node */
  4117. struct uv_rtc_timer_head {
  4118. - spinlock_t lock;
  4119. + raw_spinlock_t lock;
  4120. /* next cpu waiting for timer, local node relative: */
  4121. int next_cpu;
  4122. /* number of cpus on this node: */
  4123. @@ -178,7 +178,7 @@
  4124. uv_rtc_deallocate_timers();
  4125. return -ENOMEM;
  4126. }
  4127. - spin_lock_init(&head->lock);
  4128. + raw_spin_lock_init(&head->lock);
  4129. head->ncpus = uv_blade_nr_possible_cpus(bid);
  4130. head->next_cpu = -1;
  4131. blade_info[bid] = head;
  4132. @@ -232,7 +232,7 @@
  4133. unsigned long flags;
  4134. int next_cpu;
  4135. - spin_lock_irqsave(&head->lock, flags);
  4136. + raw_spin_lock_irqsave(&head->lock, flags);
  4137. next_cpu = head->next_cpu;
  4138. *t = expires;
  4139. @@ -244,12 +244,12 @@
  4140. if (uv_setup_intr(cpu, expires)) {
  4141. *t = ULLONG_MAX;
  4142. uv_rtc_find_next_timer(head, pnode);
  4143. - spin_unlock_irqrestore(&head->lock, flags);
  4144. + raw_spin_unlock_irqrestore(&head->lock, flags);
  4145. return -ETIME;
  4146. }
  4147. }
  4148. - spin_unlock_irqrestore(&head->lock, flags);
  4149. + raw_spin_unlock_irqrestore(&head->lock, flags);
  4150. return 0;
  4151. }
  4152. @@ -268,7 +268,7 @@
  4153. unsigned long flags;
  4154. int rc = 0;
  4155. - spin_lock_irqsave(&head->lock, flags);
  4156. + raw_spin_lock_irqsave(&head->lock, flags);
  4157. if ((head->next_cpu == bcpu && uv_read_rtc(NULL) >= *t) || force)
  4158. rc = 1;
  4159. @@ -280,7 +280,7 @@
  4160. uv_rtc_find_next_timer(head, pnode);
  4161. }
  4162. - spin_unlock_irqrestore(&head->lock, flags);
  4163. + raw_spin_unlock_irqrestore(&head->lock, flags);
  4164. return rc;
  4165. }
  4166. @@ -300,13 +300,18 @@
  4167. static cycle_t uv_read_rtc(struct clocksource *cs)
  4168. {
  4169. unsigned long offset;
  4170. + cycle_t cycles;
  4171. + preempt_disable();
  4172. if (uv_get_min_hub_revision_id() == 1)
  4173. offset = 0;
  4174. else
  4175. offset = (uv_blade_processor_id() * L1_CACHE_BYTES) % PAGE_SIZE;
  4176. - return (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  4177. + cycles = (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  4178. + preempt_enable();
  4179. +
  4180. + return cycles;
  4181. }
  4182. /*
  4183. diff -Nur linux-4.1.13.orig/arch/xtensa/mm/fault.c linux-4.1.13/arch/xtensa/mm/fault.c
  4184. --- linux-4.1.13.orig/arch/xtensa/mm/fault.c 2015-11-09 23:34:10.000000000 +0100
  4185. +++ linux-4.1.13/arch/xtensa/mm/fault.c 2015-11-29 09:23:09.533619230 +0100
  4186. @@ -15,10 +15,10 @@
  4187. #include <linux/mm.h>
  4188. #include <linux/module.h>
  4189. #include <linux/hardirq.h>
  4190. +#include <linux/uaccess.h>
  4191. #include <asm/mmu_context.h>
  4192. #include <asm/cacheflush.h>
  4193. #include <asm/hardirq.h>
  4194. -#include <asm/uaccess.h>
  4195. #include <asm/pgalloc.h>
  4196. DEFINE_PER_CPU(unsigned long, asid_cache) = ASID_USER_FIRST;
  4197. @@ -57,7 +57,7 @@
  4198. /* If we're in an interrupt or have no user
  4199. * context, we must not take the fault..
  4200. */
  4201. - if (in_atomic() || !mm) {
  4202. + if (faulthandler_disabled() || !mm) {
  4203. bad_page_fault(regs, address, SIGSEGV);
  4204. return;
  4205. }
  4206. diff -Nur linux-4.1.13.orig/arch/xtensa/mm/highmem.c linux-4.1.13/arch/xtensa/mm/highmem.c
  4207. --- linux-4.1.13.orig/arch/xtensa/mm/highmem.c 2015-11-09 23:34:10.000000000 +0100
  4208. +++ linux-4.1.13/arch/xtensa/mm/highmem.c 2015-11-29 09:23:09.533619230 +0100
  4209. @@ -42,6 +42,7 @@
  4210. enum fixed_addresses idx;
  4211. unsigned long vaddr;
  4212. + preempt_disable();
  4213. pagefault_disable();
  4214. if (!PageHighMem(page))
  4215. return page_address(page);
  4216. @@ -79,6 +80,7 @@
  4217. }
  4218. pagefault_enable();
  4219. + preempt_enable();
  4220. }
  4221. EXPORT_SYMBOL(__kunmap_atomic);
  4222. diff -Nur linux-4.1.13.orig/block/blk-core.c linux-4.1.13/block/blk-core.c
  4223. --- linux-4.1.13.orig/block/blk-core.c 2015-11-09 23:34:10.000000000 +0100
  4224. +++ linux-4.1.13/block/blk-core.c 2015-11-29 09:23:09.533619230 +0100
  4225. @@ -100,6 +100,9 @@
  4226. INIT_LIST_HEAD(&rq->queuelist);
  4227. INIT_LIST_HEAD(&rq->timeout_list);
  4228. +#ifdef CONFIG_PREEMPT_RT_FULL
  4229. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  4230. +#endif
  4231. rq->cpu = -1;
  4232. rq->q = q;
  4233. rq->__sector = (sector_t) -1;
  4234. @@ -194,7 +197,7 @@
  4235. **/
  4236. void blk_start_queue(struct request_queue *q)
  4237. {
  4238. - WARN_ON(!irqs_disabled());
  4239. + WARN_ON_NONRT(!irqs_disabled());
  4240. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  4241. __blk_run_queue(q);
  4242. @@ -661,7 +664,7 @@
  4243. q->bypass_depth = 1;
  4244. __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
  4245. - init_waitqueue_head(&q->mq_freeze_wq);
  4246. + init_swait_head(&q->mq_freeze_wq);
  4247. if (blkcg_init_queue(q))
  4248. goto fail_bdi;
  4249. @@ -3077,7 +3080,7 @@
  4250. blk_run_queue_async(q);
  4251. else
  4252. __blk_run_queue(q);
  4253. - spin_unlock(q->queue_lock);
  4254. + spin_unlock_irq(q->queue_lock);
  4255. }
  4256. static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
  4257. @@ -3125,7 +3128,6 @@
  4258. void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  4259. {
  4260. struct request_queue *q;
  4261. - unsigned long flags;
  4262. struct request *rq;
  4263. LIST_HEAD(list);
  4264. unsigned int depth;
  4265. @@ -3145,11 +3147,6 @@
  4266. q = NULL;
  4267. depth = 0;
  4268. - /*
  4269. - * Save and disable interrupts here, to avoid doing it for every
  4270. - * queue lock we have to take.
  4271. - */
  4272. - local_irq_save(flags);
  4273. while (!list_empty(&list)) {
  4274. rq = list_entry_rq(list.next);
  4275. list_del_init(&rq->queuelist);
  4276. @@ -3162,7 +3159,7 @@
  4277. queue_unplugged(q, depth, from_schedule);
  4278. q = rq->q;
  4279. depth = 0;
  4280. - spin_lock(q->queue_lock);
  4281. + spin_lock_irq(q->queue_lock);
  4282. }
  4283. /*
  4284. @@ -3189,8 +3186,6 @@
  4285. */
  4286. if (q)
  4287. queue_unplugged(q, depth, from_schedule);
  4288. -
  4289. - local_irq_restore(flags);
  4290. }
  4291. void blk_finish_plug(struct blk_plug *plug)
  4292. diff -Nur linux-4.1.13.orig/block/blk-ioc.c linux-4.1.13/block/blk-ioc.c
  4293. --- linux-4.1.13.orig/block/blk-ioc.c 2015-11-09 23:34:10.000000000 +0100
  4294. +++ linux-4.1.13/block/blk-ioc.c 2015-11-29 09:23:09.533619230 +0100
  4295. @@ -7,6 +7,7 @@
  4296. #include <linux/bio.h>
  4297. #include <linux/blkdev.h>
  4298. #include <linux/slab.h>
  4299. +#include <linux/delay.h>
  4300. #include "blk.h"
  4301. @@ -109,7 +110,7 @@
  4302. spin_unlock(q->queue_lock);
  4303. } else {
  4304. spin_unlock_irqrestore(&ioc->lock, flags);
  4305. - cpu_relax();
  4306. + cpu_chill();
  4307. spin_lock_irqsave_nested(&ioc->lock, flags, 1);
  4308. }
  4309. }
  4310. @@ -187,7 +188,7 @@
  4311. spin_unlock(icq->q->queue_lock);
  4312. } else {
  4313. spin_unlock_irqrestore(&ioc->lock, flags);
  4314. - cpu_relax();
  4315. + cpu_chill();
  4316. goto retry;
  4317. }
  4318. }
  4319. diff -Nur linux-4.1.13.orig/block/blk-iopoll.c linux-4.1.13/block/blk-iopoll.c
  4320. --- linux-4.1.13.orig/block/blk-iopoll.c 2015-11-09 23:34:10.000000000 +0100
  4321. +++ linux-4.1.13/block/blk-iopoll.c 2015-11-29 09:23:09.533619230 +0100
  4322. @@ -35,6 +35,7 @@
  4323. list_add_tail(&iop->list, this_cpu_ptr(&blk_cpu_iopoll));
  4324. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  4325. local_irq_restore(flags);
  4326. + preempt_check_resched_rt();
  4327. }
  4328. EXPORT_SYMBOL(blk_iopoll_sched);
  4329. @@ -132,6 +133,7 @@
  4330. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  4331. local_irq_enable();
  4332. + preempt_check_resched_rt();
  4333. }
  4334. /**
  4335. @@ -201,6 +203,7 @@
  4336. this_cpu_ptr(&blk_cpu_iopoll));
  4337. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  4338. local_irq_enable();
  4339. + preempt_check_resched_rt();
  4340. }
  4341. return NOTIFY_OK;
  4342. diff -Nur linux-4.1.13.orig/block/blk-mq.c linux-4.1.13/block/blk-mq.c
  4343. --- linux-4.1.13.orig/block/blk-mq.c 2015-11-09 23:34:10.000000000 +0100
  4344. +++ linux-4.1.13/block/blk-mq.c 2015-11-29 09:23:09.533619230 +0100
  4345. @@ -88,7 +88,7 @@
  4346. if (!(gfp & __GFP_WAIT))
  4347. return -EBUSY;
  4348. - ret = wait_event_interruptible(q->mq_freeze_wq,
  4349. + ret = swait_event_interruptible(q->mq_freeze_wq,
  4350. !q->mq_freeze_depth || blk_queue_dying(q));
  4351. if (blk_queue_dying(q))
  4352. return -ENODEV;
  4353. @@ -107,7 +107,7 @@
  4354. struct request_queue *q =
  4355. container_of(ref, struct request_queue, mq_usage_counter);
  4356. - wake_up_all(&q->mq_freeze_wq);
  4357. + swait_wake_all(&q->mq_freeze_wq);
  4358. }
  4359. void blk_mq_freeze_queue_start(struct request_queue *q)
  4360. @@ -127,7 +127,7 @@
  4361. static void blk_mq_freeze_queue_wait(struct request_queue *q)
  4362. {
  4363. - wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->mq_usage_counter));
  4364. + swait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->mq_usage_counter));
  4365. }
  4366. /*
  4367. @@ -151,7 +151,7 @@
  4368. spin_unlock_irq(q->queue_lock);
  4369. if (wake) {
  4370. percpu_ref_reinit(&q->mq_usage_counter);
  4371. - wake_up_all(&q->mq_freeze_wq);
  4372. + swait_wake_all(&q->mq_freeze_wq);
  4373. }
  4374. }
  4375. EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
  4376. @@ -170,7 +170,7 @@
  4377. * dying, we need to ensure that processes currently waiting on
  4378. * the queue are notified as well.
  4379. */
  4380. - wake_up_all(&q->mq_freeze_wq);
  4381. + swait_wake_all(&q->mq_freeze_wq);
  4382. }
  4383. bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
  4384. @@ -217,6 +217,9 @@
  4385. rq->resid_len = 0;
  4386. rq->sense = NULL;
  4387. +#ifdef CONFIG_PREEMPT_RT_FULL
  4388. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  4389. +#endif
  4390. INIT_LIST_HEAD(&rq->timeout_list);
  4391. rq->timeout = 0;
  4392. @@ -346,6 +349,17 @@
  4393. }
  4394. EXPORT_SYMBOL(blk_mq_end_request);
  4395. +#ifdef CONFIG_PREEMPT_RT_FULL
  4396. +
  4397. +void __blk_mq_complete_request_remote_work(struct work_struct *work)
  4398. +{
  4399. + struct request *rq = container_of(work, struct request, work);
  4400. +
  4401. + rq->q->softirq_done_fn(rq);
  4402. +}
  4403. +
  4404. +#else
  4405. +
  4406. static void __blk_mq_complete_request_remote(void *data)
  4407. {
  4408. struct request *rq = data;
  4409. @@ -353,6 +367,8 @@
  4410. rq->q->softirq_done_fn(rq);
  4411. }
  4412. +#endif
  4413. +
  4414. static void blk_mq_ipi_complete_request(struct request *rq)
  4415. {
  4416. struct blk_mq_ctx *ctx = rq->mq_ctx;
  4417. @@ -364,19 +380,23 @@
  4418. return;
  4419. }
  4420. - cpu = get_cpu();
  4421. + cpu = get_cpu_light();
  4422. if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
  4423. shared = cpus_share_cache(cpu, ctx->cpu);
  4424. if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
  4425. +#ifdef CONFIG_PREEMPT_RT_FULL
  4426. + schedule_work_on(ctx->cpu, &rq->work);
  4427. +#else
  4428. rq->csd.func = __blk_mq_complete_request_remote;
  4429. rq->csd.info = rq;
  4430. rq->csd.flags = 0;
  4431. smp_call_function_single_async(ctx->cpu, &rq->csd);
  4432. +#endif
  4433. } else {
  4434. rq->q->softirq_done_fn(rq);
  4435. }
  4436. - put_cpu();
  4437. + put_cpu_light();
  4438. }
  4439. void __blk_mq_complete_request(struct request *rq)
  4440. @@ -905,14 +925,14 @@
  4441. return;
  4442. if (!async) {
  4443. - int cpu = get_cpu();
  4444. + int cpu = get_cpu_light();
  4445. if (cpumask_test_cpu(cpu, hctx->cpumask)) {
  4446. __blk_mq_run_hw_queue(hctx);
  4447. - put_cpu();
  4448. + put_cpu_light();
  4449. return;
  4450. }
  4451. - put_cpu();
  4452. + put_cpu_light();
  4453. }
  4454. kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
  4455. @@ -1589,7 +1609,7 @@
  4456. {
  4457. struct blk_mq_hw_ctx *hctx = data;
  4458. - if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
  4459. + if (action == CPU_POST_DEAD)
  4460. return blk_mq_hctx_cpu_offline(hctx, cpu);
  4461. /*
  4462. diff -Nur linux-4.1.13.orig/block/blk-mq-cpu.c linux-4.1.13/block/blk-mq-cpu.c
  4463. --- linux-4.1.13.orig/block/blk-mq-cpu.c 2015-11-09 23:34:10.000000000 +0100
  4464. +++ linux-4.1.13/block/blk-mq-cpu.c 2015-11-29 09:23:09.533619230 +0100
  4465. @@ -16,7 +16,7 @@
  4466. #include "blk-mq.h"
  4467. static LIST_HEAD(blk_mq_cpu_notify_list);
  4468. -static DEFINE_RAW_SPINLOCK(blk_mq_cpu_notify_lock);
  4469. +static DEFINE_SPINLOCK(blk_mq_cpu_notify_lock);
  4470. static int blk_mq_main_cpu_notify(struct notifier_block *self,
  4471. unsigned long action, void *hcpu)
  4472. @@ -25,7 +25,10 @@
  4473. struct blk_mq_cpu_notifier *notify;
  4474. int ret = NOTIFY_OK;
  4475. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  4476. + if (action != CPU_POST_DEAD)
  4477. + return NOTIFY_OK;
  4478. +
  4479. + spin_lock(&blk_mq_cpu_notify_lock);
  4480. list_for_each_entry(notify, &blk_mq_cpu_notify_list, list) {
  4481. ret = notify->notify(notify->data, action, cpu);
  4482. @@ -33,7 +36,7 @@
  4483. break;
  4484. }
  4485. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  4486. + spin_unlock(&blk_mq_cpu_notify_lock);
  4487. return ret;
  4488. }
  4489. @@ -41,16 +44,16 @@
  4490. {
  4491. BUG_ON(!notifier->notify);
  4492. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  4493. + spin_lock(&blk_mq_cpu_notify_lock);
  4494. list_add_tail(&notifier->list, &blk_mq_cpu_notify_list);
  4495. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  4496. + spin_unlock(&blk_mq_cpu_notify_lock);
  4497. }
  4498. void blk_mq_unregister_cpu_notifier(struct blk_mq_cpu_notifier *notifier)
  4499. {
  4500. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  4501. + spin_lock(&blk_mq_cpu_notify_lock);
  4502. list_del(&notifier->list);
  4503. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  4504. + spin_unlock(&blk_mq_cpu_notify_lock);
  4505. }
  4506. void blk_mq_init_cpu_notifier(struct blk_mq_cpu_notifier *notifier,
  4507. diff -Nur linux-4.1.13.orig/block/blk-mq.h linux-4.1.13/block/blk-mq.h
  4508. --- linux-4.1.13.orig/block/blk-mq.h 2015-11-09 23:34:10.000000000 +0100
  4509. +++ linux-4.1.13/block/blk-mq.h 2015-11-29 09:23:09.533619230 +0100
  4510. @@ -76,7 +76,10 @@
  4511. static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
  4512. unsigned int cpu)
  4513. {
  4514. - return per_cpu_ptr(q->queue_ctx, cpu);
  4515. + struct blk_mq_ctx *ctx;
  4516. +
  4517. + ctx = per_cpu_ptr(q->queue_ctx, cpu);
  4518. + return ctx;
  4519. }
  4520. /*
  4521. @@ -87,12 +90,12 @@
  4522. */
  4523. static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
  4524. {
  4525. - return __blk_mq_get_ctx(q, get_cpu());
  4526. + return __blk_mq_get_ctx(q, get_cpu_light());
  4527. }
  4528. static inline void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
  4529. {
  4530. - put_cpu();
  4531. + put_cpu_light();
  4532. }
  4533. struct blk_mq_alloc_data {
  4534. diff -Nur linux-4.1.13.orig/block/blk-softirq.c linux-4.1.13/block/blk-softirq.c
  4535. --- linux-4.1.13.orig/block/blk-softirq.c 2015-11-09 23:34:10.000000000 +0100
  4536. +++ linux-4.1.13/block/blk-softirq.c 2015-11-29 09:23:09.533619230 +0100
  4537. @@ -51,6 +51,7 @@
  4538. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  4539. local_irq_restore(flags);
  4540. + preempt_check_resched_rt();
  4541. }
  4542. /*
  4543. @@ -93,6 +94,7 @@
  4544. this_cpu_ptr(&blk_cpu_done));
  4545. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  4546. local_irq_enable();
  4547. + preempt_check_resched_rt();
  4548. }
  4549. return NOTIFY_OK;
  4550. @@ -150,6 +152,7 @@
  4551. goto do_local;
  4552. local_irq_restore(flags);
  4553. + preempt_check_resched_rt();
  4554. }
  4555. /**
  4556. diff -Nur linux-4.1.13.orig/block/bounce.c linux-4.1.13/block/bounce.c
  4557. --- linux-4.1.13.orig/block/bounce.c 2015-11-09 23:34:10.000000000 +0100
  4558. +++ linux-4.1.13/block/bounce.c 2015-11-29 09:23:09.537618965 +0100
  4559. @@ -54,11 +54,11 @@
  4560. unsigned long flags;
  4561. unsigned char *vto;
  4562. - local_irq_save(flags);
  4563. + local_irq_save_nort(flags);
  4564. vto = kmap_atomic(to->bv_page);
  4565. memcpy(vto + to->bv_offset, vfrom, to->bv_len);
  4566. kunmap_atomic(vto);
  4567. - local_irq_restore(flags);
  4568. + local_irq_restore_nort(flags);
  4569. }
  4570. #else /* CONFIG_HIGHMEM */
  4571. diff -Nur linux-4.1.13.orig/crypto/algapi.c linux-4.1.13/crypto/algapi.c
  4572. --- linux-4.1.13.orig/crypto/algapi.c 2015-11-09 23:34:10.000000000 +0100
  4573. +++ linux-4.1.13/crypto/algapi.c 2015-11-29 09:23:09.537618965 +0100
  4574. @@ -695,13 +695,13 @@
  4575. int crypto_register_notifier(struct notifier_block *nb)
  4576. {
  4577. - return blocking_notifier_chain_register(&crypto_chain, nb);
  4578. + return srcu_notifier_chain_register(&crypto_chain, nb);
  4579. }
  4580. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  4581. int crypto_unregister_notifier(struct notifier_block *nb)
  4582. {
  4583. - return blocking_notifier_chain_unregister(&crypto_chain, nb);
  4584. + return srcu_notifier_chain_unregister(&crypto_chain, nb);
  4585. }
  4586. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  4587. diff -Nur linux-4.1.13.orig/crypto/api.c linux-4.1.13/crypto/api.c
  4588. --- linux-4.1.13.orig/crypto/api.c 2015-11-09 23:34:10.000000000 +0100
  4589. +++ linux-4.1.13/crypto/api.c 2015-11-29 09:23:09.537618965 +0100
  4590. @@ -31,7 +31,7 @@
  4591. DECLARE_RWSEM(crypto_alg_sem);
  4592. EXPORT_SYMBOL_GPL(crypto_alg_sem);
  4593. -BLOCKING_NOTIFIER_HEAD(crypto_chain);
  4594. +SRCU_NOTIFIER_HEAD(crypto_chain);
  4595. EXPORT_SYMBOL_GPL(crypto_chain);
  4596. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg);
  4597. @@ -236,10 +236,10 @@
  4598. {
  4599. int ok;
  4600. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  4601. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  4602. if (ok == NOTIFY_DONE) {
  4603. request_module("cryptomgr");
  4604. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  4605. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  4606. }
  4607. return ok;
  4608. diff -Nur linux-4.1.13.orig/crypto/internal.h linux-4.1.13/crypto/internal.h
  4609. --- linux-4.1.13.orig/crypto/internal.h 2015-11-09 23:34:10.000000000 +0100
  4610. +++ linux-4.1.13/crypto/internal.h 2015-11-29 09:23:09.537618965 +0100
  4611. @@ -48,7 +48,7 @@
  4612. extern struct list_head crypto_alg_list;
  4613. extern struct rw_semaphore crypto_alg_sem;
  4614. -extern struct blocking_notifier_head crypto_chain;
  4615. +extern struct srcu_notifier_head crypto_chain;
  4616. #ifdef CONFIG_PROC_FS
  4617. void __init crypto_init_proc(void);
  4618. @@ -142,7 +142,7 @@
  4619. static inline void crypto_notify(unsigned long val, void *v)
  4620. {
  4621. - blocking_notifier_call_chain(&crypto_chain, val, v);
  4622. + srcu_notifier_call_chain(&crypto_chain, val, v);
  4623. }
  4624. #endif /* _CRYPTO_INTERNAL_H */
  4625. diff -Nur linux-4.1.13.orig/Documentation/hwlat_detector.txt linux-4.1.13/Documentation/hwlat_detector.txt
  4626. --- linux-4.1.13.orig/Documentation/hwlat_detector.txt 1970-01-01 01:00:00.000000000 +0100
  4627. +++ linux-4.1.13/Documentation/hwlat_detector.txt 2015-11-29 09:23:09.477622951 +0100
  4628. @@ -0,0 +1,64 @@
  4629. +Introduction:
  4630. +-------------
  4631. +
  4632. +The module hwlat_detector is a special purpose kernel module that is used to
  4633. +detect large system latencies induced by the behavior of certain underlying
  4634. +hardware or firmware, independent of Linux itself. The code was developed
  4635. +originally to detect SMIs (System Management Interrupts) on x86 systems,
  4636. +however there is nothing x86 specific about this patchset. It was
  4637. +originally written for use by the "RT" patch since the Real Time
  4638. +kernel is highly latency sensitive.
  4639. +
  4640. +SMIs are usually not serviced by the Linux kernel, which typically does not
  4641. +even know that they are occuring. SMIs are instead are set up by BIOS code
  4642. +and are serviced by BIOS code, usually for "critical" events such as
  4643. +management of thermal sensors and fans. Sometimes though, SMIs are used for
  4644. +other tasks and those tasks can spend an inordinate amount of time in the
  4645. +handler (sometimes measured in milliseconds). Obviously this is a problem if
  4646. +you are trying to keep event service latencies down in the microsecond range.
  4647. +
  4648. +The hardware latency detector works by hogging all of the cpus for configurable
  4649. +amounts of time (by calling stop_machine()), polling the CPU Time Stamp Counter
  4650. +for some period, then looking for gaps in the TSC data. Any gap indicates a
  4651. +time when the polling was interrupted and since the machine is stopped and
  4652. +interrupts turned off the only thing that could do that would be an SMI.
  4653. +
  4654. +Note that the SMI detector should *NEVER* be used in a production environment.
  4655. +It is intended to be run manually to determine if the hardware platform has a
  4656. +problem with long system firmware service routines.
  4657. +
  4658. +Usage:
  4659. +------
  4660. +
  4661. +Loading the module hwlat_detector passing the parameter "enabled=1" (or by
  4662. +setting the "enable" entry in "hwlat_detector" debugfs toggled on) is the only
  4663. +step required to start the hwlat_detector. It is possible to redefine the
  4664. +threshold in microseconds (us) above which latency spikes will be taken
  4665. +into account (parameter "threshold=").
  4666. +
  4667. +Example:
  4668. +
  4669. + # modprobe hwlat_detector enabled=1 threshold=100
  4670. +
  4671. +After the module is loaded, it creates a directory named "hwlat_detector" under
  4672. +the debugfs mountpoint, "/debug/hwlat_detector" for this text. It is necessary
  4673. +to have debugfs mounted, which might be on /sys/debug on your system.
  4674. +
  4675. +The /debug/hwlat_detector interface contains the following files:
  4676. +
  4677. +count - number of latency spikes observed since last reset
  4678. +enable - a global enable/disable toggle (0/1), resets count
  4679. +max - maximum hardware latency actually observed (usecs)
  4680. +sample - a pipe from which to read current raw sample data
  4681. + in the format <timestamp> <latency observed usecs>
  4682. + (can be opened O_NONBLOCK for a single sample)
  4683. +threshold - minimum latency value to be considered (usecs)
  4684. +width - time period to sample with CPUs held (usecs)
  4685. + must be less than the total window size (enforced)
  4686. +window - total period of sampling, width being inside (usecs)
  4687. +
  4688. +By default we will set width to 500,000 and window to 1,000,000, meaning that
  4689. +we will sample every 1,000,000 usecs (1s) for 500,000 usecs (0.5s). If we
  4690. +observe any latencies that exceed the threshold (initially 100 usecs),
  4691. +then we write to a global sample ring buffer of 8K samples, which is
  4692. +consumed by reading from the "sample" (pipe) debugfs file interface.
  4693. diff -Nur linux-4.1.13.orig/Documentation/sysrq.txt linux-4.1.13/Documentation/sysrq.txt
  4694. --- linux-4.1.13.orig/Documentation/sysrq.txt 2015-11-09 23:34:10.000000000 +0100
  4695. +++ linux-4.1.13/Documentation/sysrq.txt 2015-11-29 09:23:09.477622951 +0100
  4696. @@ -59,10 +59,17 @@
  4697. On other - If you know of the key combos for other architectures, please
  4698. let me know so I can add them to this section.
  4699. -On all - write a character to /proc/sysrq-trigger. e.g.:
  4700. -
  4701. +On all - write a character to /proc/sysrq-trigger, e.g.:
  4702. echo t > /proc/sysrq-trigger
  4703. +On all - Enable network SysRq by writing a cookie to icmp_echo_sysrq, e.g.
  4704. + echo 0x01020304 >/proc/sys/net/ipv4/icmp_echo_sysrq
  4705. + Send an ICMP echo request with this pattern plus the particular
  4706. + SysRq command key. Example:
  4707. + # ping -c1 -s57 -p0102030468
  4708. + will trigger the SysRq-H (help) command.
  4709. +
  4710. +
  4711. * What are the 'command' keys?
  4712. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4713. 'b' - Will immediately reboot the system without syncing or unmounting
  4714. diff -Nur linux-4.1.13.orig/Documentation/trace/histograms.txt linux-4.1.13/Documentation/trace/histograms.txt
  4715. --- linux-4.1.13.orig/Documentation/trace/histograms.txt 1970-01-01 01:00:00.000000000 +0100
  4716. +++ linux-4.1.13/Documentation/trace/histograms.txt 2015-11-29 09:23:09.477622951 +0100
  4717. @@ -0,0 +1,186 @@
  4718. + Using the Linux Kernel Latency Histograms
  4719. +
  4720. +
  4721. +This document gives a short explanation how to enable, configure and use
  4722. +latency histograms. Latency histograms are primarily relevant in the
  4723. +context of real-time enabled kernels (CONFIG_PREEMPT/CONFIG_PREEMPT_RT)
  4724. +and are used in the quality management of the Linux real-time
  4725. +capabilities.
  4726. +
  4727. +
  4728. +* Purpose of latency histograms
  4729. +
  4730. +A latency histogram continuously accumulates the frequencies of latency
  4731. +data. There are two types of histograms
  4732. +- potential sources of latencies
  4733. +- effective latencies
  4734. +
  4735. +
  4736. +* Potential sources of latencies
  4737. +
  4738. +Potential sources of latencies are code segments where interrupts,
  4739. +preemption or both are disabled (aka critical sections). To create
  4740. +histograms of potential sources of latency, the kernel stores the time
  4741. +stamp at the start of a critical section, determines the time elapsed
  4742. +when the end of the section is reached, and increments the frequency
  4743. +counter of that latency value - irrespective of whether any concurrently
  4744. +running process is affected by latency or not.
  4745. +- Configuration items (in the Kernel hacking/Tracers submenu)
  4746. + CONFIG_INTERRUPT_OFF_LATENCY
  4747. + CONFIG_PREEMPT_OFF_LATENCY
  4748. +
  4749. +
  4750. +* Effective latencies
  4751. +
  4752. +Effective latencies are actually occuring during wakeup of a process. To
  4753. +determine effective latencies, the kernel stores the time stamp when a
  4754. +process is scheduled to be woken up, and determines the duration of the
  4755. +wakeup time shortly before control is passed over to this process. Note
  4756. +that the apparent latency in user space may be somewhat longer, since the
  4757. +process may be interrupted after control is passed over to it but before
  4758. +the execution in user space takes place. Simply measuring the interval
  4759. +between enqueuing and wakeup may also not appropriate in cases when a
  4760. +process is scheduled as a result of a timer expiration. The timer may have
  4761. +missed its deadline, e.g. due to disabled interrupts, but this latency
  4762. +would not be registered. Therefore, the offsets of missed timers are
  4763. +recorded in a separate histogram. If both wakeup latency and missed timer
  4764. +offsets are configured and enabled, a third histogram may be enabled that
  4765. +records the overall latency as a sum of the timer latency, if any, and the
  4766. +wakeup latency. This histogram is called "timerandwakeup".
  4767. +- Configuration items (in the Kernel hacking/Tracers submenu)
  4768. + CONFIG_WAKEUP_LATENCY
  4769. + CONFIG_MISSED_TIMER_OFSETS
  4770. +
  4771. +
  4772. +* Usage
  4773. +
  4774. +The interface to the administration of the latency histograms is located
  4775. +in the debugfs file system. To mount it, either enter
  4776. +
  4777. +mount -t sysfs nodev /sys
  4778. +mount -t debugfs nodev /sys/kernel/debug
  4779. +
  4780. +from shell command line level, or add
  4781. +
  4782. +nodev /sys sysfs defaults 0 0
  4783. +nodev /sys/kernel/debug debugfs defaults 0 0
  4784. +
  4785. +to the file /etc/fstab. All latency histogram related files are then
  4786. +available in the directory /sys/kernel/debug/tracing/latency_hist. A
  4787. +particular histogram type is enabled by writing non-zero to the related
  4788. +variable in the /sys/kernel/debug/tracing/latency_hist/enable directory.
  4789. +Select "preemptirqsoff" for the histograms of potential sources of
  4790. +latencies and "wakeup" for histograms of effective latencies etc. The
  4791. +histogram data - one per CPU - are available in the files
  4792. +
  4793. +/sys/kernel/debug/tracing/latency_hist/preemptoff/CPUx
  4794. +/sys/kernel/debug/tracing/latency_hist/irqsoff/CPUx
  4795. +/sys/kernel/debug/tracing/latency_hist/preemptirqsoff/CPUx
  4796. +/sys/kernel/debug/tracing/latency_hist/wakeup/CPUx
  4797. +/sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio/CPUx
  4798. +/sys/kernel/debug/tracing/latency_hist/missed_timer_offsets/CPUx
  4799. +/sys/kernel/debug/tracing/latency_hist/timerandwakeup/CPUx
  4800. +
  4801. +The histograms are reset by writing non-zero to the file "reset" in a
  4802. +particular latency directory. To reset all latency data, use
  4803. +
  4804. +#!/bin/sh
  4805. +
  4806. +TRACINGDIR=/sys/kernel/debug/tracing
  4807. +HISTDIR=$TRACINGDIR/latency_hist
  4808. +
  4809. +if test -d $HISTDIR
  4810. +then
  4811. + cd $HISTDIR
  4812. + for i in `find . | grep /reset$`
  4813. + do
  4814. + echo 1 >$i
  4815. + done
  4816. +fi
  4817. +
  4818. +
  4819. +* Data format
  4820. +
  4821. +Latency data are stored with a resolution of one microsecond. The
  4822. +maximum latency is 10,240 microseconds. The data are only valid, if the
  4823. +overflow register is empty. Every output line contains the latency in
  4824. +microseconds in the first row and the number of samples in the second
  4825. +row. To display only lines with a positive latency count, use, for
  4826. +example,
  4827. +
  4828. +grep -v " 0$" /sys/kernel/debug/tracing/latency_hist/preemptoff/CPU0
  4829. +
  4830. +#Minimum latency: 0 microseconds.
  4831. +#Average latency: 0 microseconds.
  4832. +#Maximum latency: 25 microseconds.
  4833. +#Total samples: 3104770694
  4834. +#There are 0 samples greater or equal than 10240 microseconds
  4835. +#usecs samples
  4836. + 0 2984486876
  4837. + 1 49843506
  4838. + 2 58219047
  4839. + 3 5348126
  4840. + 4 2187960
  4841. + 5 3388262
  4842. + 6 959289
  4843. + 7 208294
  4844. + 8 40420
  4845. + 9 4485
  4846. + 10 14918
  4847. + 11 18340
  4848. + 12 25052
  4849. + 13 19455
  4850. + 14 5602
  4851. + 15 969
  4852. + 16 47
  4853. + 17 18
  4854. + 18 14
  4855. + 19 1
  4856. + 20 3
  4857. + 21 2
  4858. + 22 5
  4859. + 23 2
  4860. + 25 1
  4861. +
  4862. +
  4863. +* Wakeup latency of a selected process
  4864. +
  4865. +To only collect wakeup latency data of a particular process, write the
  4866. +PID of the requested process to
  4867. +
  4868. +/sys/kernel/debug/tracing/latency_hist/wakeup/pid
  4869. +
  4870. +PIDs are not considered, if this variable is set to 0.
  4871. +
  4872. +
  4873. +* Details of the process with the highest wakeup latency so far
  4874. +
  4875. +Selected data of the process that suffered from the highest wakeup
  4876. +latency that occurred in a particular CPU are available in the file
  4877. +
  4878. +/sys/kernel/debug/tracing/latency_hist/wakeup/max_latency-CPUx.
  4879. +
  4880. +In addition, other relevant system data at the time when the
  4881. +latency occurred are given.
  4882. +
  4883. +The format of the data is (all in one line):
  4884. +<PID> <Priority> <Latency> (<Timeroffset>) <Command> \
  4885. +<- <PID> <Priority> <Command> <Timestamp>
  4886. +
  4887. +The value of <Timeroffset> is only relevant in the combined timer
  4888. +and wakeup latency recording. In the wakeup recording, it is
  4889. +always 0, in the missed_timer_offsets recording, it is the same
  4890. +as <Latency>.
  4891. +
  4892. +When retrospectively searching for the origin of a latency and
  4893. +tracing was not enabled, it may be helpful to know the name and
  4894. +some basic data of the task that (finally) was switching to the
  4895. +late real-tlme task. In addition to the victim's data, also the
  4896. +data of the possible culprit are therefore displayed after the
  4897. +"<-" symbol.
  4898. +
  4899. +Finally, the timestamp of the time when the latency occurred
  4900. +in <seconds>.<microseconds> after the most recent system boot
  4901. +is provided.
  4902. +
  4903. +These data are also reset when the wakeup histogram is reset.
  4904. diff -Nur linux-4.1.13.orig/drivers/acpi/acpica/acglobal.h linux-4.1.13/drivers/acpi/acpica/acglobal.h
  4905. --- linux-4.1.13.orig/drivers/acpi/acpica/acglobal.h 2015-11-09 23:34:10.000000000 +0100
  4906. +++ linux-4.1.13/drivers/acpi/acpica/acglobal.h 2015-11-29 09:23:09.537618965 +0100
  4907. @@ -112,7 +112,7 @@
  4908. * interrupt level
  4909. */
  4910. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_gpe_lock); /* For GPE data structs and registers */
  4911. -ACPI_GLOBAL(acpi_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  4912. +ACPI_GLOBAL(acpi_raw_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  4913. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_reference_count_lock);
  4914. /* Mutex for _OSI support */
  4915. diff -Nur linux-4.1.13.orig/drivers/acpi/acpica/hwregs.c linux-4.1.13/drivers/acpi/acpica/hwregs.c
  4916. --- linux-4.1.13.orig/drivers/acpi/acpica/hwregs.c 2015-11-09 23:34:10.000000000 +0100
  4917. +++ linux-4.1.13/drivers/acpi/acpica/hwregs.c 2015-11-29 09:23:09.537618965 +0100
  4918. @@ -269,14 +269,14 @@
  4919. ACPI_BITMASK_ALL_FIXED_STATUS,
  4920. ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status.address)));
  4921. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  4922. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  4923. /* Clear the fixed events in PM1 A/B */
  4924. status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
  4925. ACPI_BITMASK_ALL_FIXED_STATUS);
  4926. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  4927. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  4928. if (ACPI_FAILURE(status)) {
  4929. goto exit;
  4930. diff -Nur linux-4.1.13.orig/drivers/acpi/acpica/hwxface.c linux-4.1.13/drivers/acpi/acpica/hwxface.c
  4931. --- linux-4.1.13.orig/drivers/acpi/acpica/hwxface.c 2015-11-09 23:34:10.000000000 +0100
  4932. +++ linux-4.1.13/drivers/acpi/acpica/hwxface.c 2015-11-29 09:23:09.537618965 +0100
  4933. @@ -374,7 +374,7 @@
  4934. return_ACPI_STATUS(AE_BAD_PARAMETER);
  4935. }
  4936. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  4937. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  4938. /*
  4939. * At this point, we know that the parent register is one of the
  4940. @@ -435,7 +435,7 @@
  4941. unlock_and_exit:
  4942. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  4943. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  4944. return_ACPI_STATUS(status);
  4945. }
  4946. diff -Nur linux-4.1.13.orig/drivers/acpi/acpica/utmutex.c linux-4.1.13/drivers/acpi/acpica/utmutex.c
  4947. --- linux-4.1.13.orig/drivers/acpi/acpica/utmutex.c 2015-11-09 23:34:10.000000000 +0100
  4948. +++ linux-4.1.13/drivers/acpi/acpica/utmutex.c 2015-11-29 09:23:09.537618965 +0100
  4949. @@ -88,7 +88,7 @@
  4950. return_ACPI_STATUS (status);
  4951. }
  4952. - status = acpi_os_create_lock (&acpi_gbl_hardware_lock);
  4953. + status = acpi_os_create_raw_lock (&acpi_gbl_hardware_lock);
  4954. if (ACPI_FAILURE (status)) {
  4955. return_ACPI_STATUS (status);
  4956. }
  4957. @@ -141,7 +141,7 @@
  4958. /* Delete the spinlocks */
  4959. acpi_os_delete_lock(acpi_gbl_gpe_lock);
  4960. - acpi_os_delete_lock(acpi_gbl_hardware_lock);
  4961. + acpi_os_delete_raw_lock(acpi_gbl_hardware_lock);
  4962. acpi_os_delete_lock(acpi_gbl_reference_count_lock);
  4963. /* Delete the reader/writer lock */
  4964. diff -Nur linux-4.1.13.orig/drivers/ata/libata-sff.c linux-4.1.13/drivers/ata/libata-sff.c
  4965. --- linux-4.1.13.orig/drivers/ata/libata-sff.c 2015-11-09 23:34:10.000000000 +0100
  4966. +++ linux-4.1.13/drivers/ata/libata-sff.c 2015-11-29 09:23:09.537618965 +0100
  4967. @@ -678,9 +678,9 @@
  4968. unsigned long flags;
  4969. unsigned int consumed;
  4970. - local_irq_save(flags);
  4971. + local_irq_save_nort(flags);
  4972. consumed = ata_sff_data_xfer32(dev, buf, buflen, rw);
  4973. - local_irq_restore(flags);
  4974. + local_irq_restore_nort(flags);
  4975. return consumed;
  4976. }
  4977. @@ -719,7 +719,7 @@
  4978. unsigned long flags;
  4979. /* FIXME: use a bounce buffer */
  4980. - local_irq_save(flags);
  4981. + local_irq_save_nort(flags);
  4982. buf = kmap_atomic(page);
  4983. /* do the actual data transfer */
  4984. @@ -727,7 +727,7 @@
  4985. do_write);
  4986. kunmap_atomic(buf);
  4987. - local_irq_restore(flags);
  4988. + local_irq_restore_nort(flags);
  4989. } else {
  4990. buf = page_address(page);
  4991. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  4992. @@ -864,7 +864,7 @@
  4993. unsigned long flags;
  4994. /* FIXME: use bounce buffer */
  4995. - local_irq_save(flags);
  4996. + local_irq_save_nort(flags);
  4997. buf = kmap_atomic(page);
  4998. /* do the actual data transfer */
  4999. @@ -872,7 +872,7 @@
  5000. count, rw);
  5001. kunmap_atomic(buf);
  5002. - local_irq_restore(flags);
  5003. + local_irq_restore_nort(flags);
  5004. } else {
  5005. buf = page_address(page);
  5006. consumed = ap->ops->sff_data_xfer(dev, buf + offset,
  5007. diff -Nur linux-4.1.13.orig/drivers/char/random.c linux-4.1.13/drivers/char/random.c
  5008. --- linux-4.1.13.orig/drivers/char/random.c 2015-11-09 23:34:10.000000000 +0100
  5009. +++ linux-4.1.13/drivers/char/random.c 2015-11-29 09:23:09.537618965 +0100
  5010. @@ -776,8 +776,6 @@
  5011. } sample;
  5012. long delta, delta2, delta3;
  5013. - preempt_disable();
  5014. -
  5015. sample.jiffies = jiffies;
  5016. sample.cycles = random_get_entropy();
  5017. sample.num = num;
  5018. @@ -818,7 +816,6 @@
  5019. */
  5020. credit_entropy_bits(r, min_t(int, fls(delta>>1), 11));
  5021. }
  5022. - preempt_enable();
  5023. }
  5024. void add_input_randomness(unsigned int type, unsigned int code,
  5025. @@ -871,28 +868,27 @@
  5026. return *(ptr + f->reg_idx++);
  5027. }
  5028. -void add_interrupt_randomness(int irq, int irq_flags)
  5029. +void add_interrupt_randomness(int irq, int irq_flags, __u64 ip)
  5030. {
  5031. struct entropy_store *r;
  5032. struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness);
  5033. - struct pt_regs *regs = get_irq_regs();
  5034. unsigned long now = jiffies;
  5035. cycles_t cycles = random_get_entropy();
  5036. __u32 c_high, j_high;
  5037. - __u64 ip;
  5038. unsigned long seed;
  5039. int credit = 0;
  5040. if (cycles == 0)
  5041. - cycles = get_reg(fast_pool, regs);
  5042. + cycles = get_reg(fast_pool, NULL);
  5043. c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0;
  5044. j_high = (sizeof(now) > 4) ? now >> 32 : 0;
  5045. fast_pool->pool[0] ^= cycles ^ j_high ^ irq;
  5046. fast_pool->pool[1] ^= now ^ c_high;
  5047. - ip = regs ? instruction_pointer(regs) : _RET_IP_;
  5048. + if (!ip)
  5049. + ip = _RET_IP_;
  5050. fast_pool->pool[2] ^= ip;
  5051. fast_pool->pool[3] ^= (sizeof(ip) > 4) ? ip >> 32 :
  5052. - get_reg(fast_pool, regs);
  5053. + get_reg(fast_pool, NULL);
  5054. fast_mix(fast_pool);
  5055. add_interrupt_bench(cycles);
  5056. diff -Nur linux-4.1.13.orig/drivers/clocksource/tcb_clksrc.c linux-4.1.13/drivers/clocksource/tcb_clksrc.c
  5057. --- linux-4.1.13.orig/drivers/clocksource/tcb_clksrc.c 2015-11-09 23:34:10.000000000 +0100
  5058. +++ linux-4.1.13/drivers/clocksource/tcb_clksrc.c 2015-11-29 09:23:09.537618965 +0100
  5059. @@ -23,8 +23,7 @@
  5060. * this 32 bit free-running counter. the second channel is not used.
  5061. *
  5062. * - The third channel may be used to provide a 16-bit clockevent
  5063. - * source, used in either periodic or oneshot mode. This runs
  5064. - * at 32 KiHZ, and can handle delays of up to two seconds.
  5065. + * source, used in either periodic or oneshot mode.
  5066. *
  5067. * A boot clocksource and clockevent source are also currently needed,
  5068. * unless the relevant platforms (ARM/AT91, AVR32/AT32) are changed so
  5069. @@ -74,6 +73,7 @@
  5070. struct tc_clkevt_device {
  5071. struct clock_event_device clkevt;
  5072. struct clk *clk;
  5073. + u32 freq;
  5074. void __iomem *regs;
  5075. };
  5076. @@ -82,13 +82,6 @@
  5077. return container_of(clkevt, struct tc_clkevt_device, clkevt);
  5078. }
  5079. -/* For now, we always use the 32K clock ... this optimizes for NO_HZ,
  5080. - * because using one of the divided clocks would usually mean the
  5081. - * tick rate can never be less than several dozen Hz (vs 0.5 Hz).
  5082. - *
  5083. - * A divided clock could be good for high resolution timers, since
  5084. - * 30.5 usec resolution can seem "low".
  5085. - */
  5086. static u32 timer_clock;
  5087. static void tc_mode(enum clock_event_mode m, struct clock_event_device *d)
  5088. @@ -111,11 +104,12 @@
  5089. case CLOCK_EVT_MODE_PERIODIC:
  5090. clk_enable(tcd->clk);
  5091. - /* slow clock, count up to RC, then irq and restart */
  5092. + /* count up to RC, then irq and restart */
  5093. __raw_writel(timer_clock
  5094. | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  5095. regs + ATMEL_TC_REG(2, CMR));
  5096. - __raw_writel((32768 + HZ/2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  5097. + __raw_writel((tcd->freq + HZ / 2) / HZ,
  5098. + tcaddr + ATMEL_TC_REG(2, RC));
  5099. /* Enable clock and interrupts on RC compare */
  5100. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  5101. @@ -128,7 +122,7 @@
  5102. case CLOCK_EVT_MODE_ONESHOT:
  5103. clk_enable(tcd->clk);
  5104. - /* slow clock, count up to RC, then irq and stop */
  5105. + /* count up to RC, then irq and stop */
  5106. __raw_writel(timer_clock | ATMEL_TC_CPCSTOP
  5107. | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  5108. regs + ATMEL_TC_REG(2, CMR));
  5109. @@ -157,8 +151,12 @@
  5110. .name = "tc_clkevt",
  5111. .features = CLOCK_EVT_FEAT_PERIODIC
  5112. | CLOCK_EVT_FEAT_ONESHOT,
  5113. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  5114. /* Should be lower than at91rm9200's system timer */
  5115. .rating = 125,
  5116. +#else
  5117. + .rating = 200,
  5118. +#endif
  5119. .set_next_event = tc_next_event,
  5120. .set_mode = tc_mode,
  5121. },
  5122. @@ -178,8 +176,9 @@
  5123. return IRQ_NONE;
  5124. }
  5125. -static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  5126. +static int __init setup_clkevents(struct atmel_tc *tc, int divisor_idx)
  5127. {
  5128. + unsigned divisor = atmel_tc_divisors[divisor_idx];
  5129. int ret;
  5130. struct clk *t2_clk = tc->clk[2];
  5131. int irq = tc->irq[2];
  5132. @@ -193,7 +192,11 @@
  5133. clkevt.regs = tc->regs;
  5134. clkevt.clk = t2_clk;
  5135. - timer_clock = clk32k_divisor_idx;
  5136. + timer_clock = divisor_idx;
  5137. + if (!divisor)
  5138. + clkevt.freq = 32768;
  5139. + else
  5140. + clkevt.freq = clk_get_rate(t2_clk) / divisor;
  5141. clkevt.clkevt.cpumask = cpumask_of(0);
  5142. @@ -203,7 +206,7 @@
  5143. return ret;
  5144. }
  5145. - clockevents_config_and_register(&clkevt.clkevt, 32768, 1, 0xffff);
  5146. + clockevents_config_and_register(&clkevt.clkevt, clkevt.freq, 1, 0xffff);
  5147. return ret;
  5148. }
  5149. @@ -340,7 +343,11 @@
  5150. goto err_disable_t1;
  5151. /* channel 2: periodic and oneshot timer support */
  5152. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  5153. ret = setup_clkevents(tc, clk32k_divisor_idx);
  5154. +#else
  5155. + ret = setup_clkevents(tc, best_divisor_idx);
  5156. +#endif
  5157. if (ret)
  5158. goto err_unregister_clksrc;
  5159. diff -Nur linux-4.1.13.orig/drivers/clocksource/timer-atmel-pit.c linux-4.1.13/drivers/clocksource/timer-atmel-pit.c
  5160. --- linux-4.1.13.orig/drivers/clocksource/timer-atmel-pit.c 2015-11-09 23:34:10.000000000 +0100
  5161. +++ linux-4.1.13/drivers/clocksource/timer-atmel-pit.c 2015-11-29 09:23:09.537618965 +0100
  5162. @@ -90,6 +90,7 @@
  5163. return elapsed;
  5164. }
  5165. +static struct irqaction at91sam926x_pit_irq;
  5166. /*
  5167. * Clockevent device: interrupts every 1/HZ (== pit_cycles * MCK/16)
  5168. */
  5169. @@ -100,6 +101,8 @@
  5170. switch (mode) {
  5171. case CLOCK_EVT_MODE_PERIODIC:
  5172. + /* Set up irq handler */
  5173. + setup_irq(at91sam926x_pit_irq.irq, &at91sam926x_pit_irq);
  5174. /* update clocksource counter */
  5175. data->cnt += data->cycle * PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
  5176. pit_write(data->base, AT91_PIT_MR,
  5177. @@ -113,6 +116,7 @@
  5178. /* disable irq, leaving the clocksource active */
  5179. pit_write(data->base, AT91_PIT_MR,
  5180. (data->cycle - 1) | AT91_PIT_PITEN);
  5181. + remove_irq(at91sam926x_pit_irq.irq, &at91sam926x_pit_irq);
  5182. break;
  5183. case CLOCK_EVT_MODE_RESUME:
  5184. break;
  5185. diff -Nur linux-4.1.13.orig/drivers/clocksource/timer-atmel-st.c linux-4.1.13/drivers/clocksource/timer-atmel-st.c
  5186. --- linux-4.1.13.orig/drivers/clocksource/timer-atmel-st.c 2015-11-09 23:34:10.000000000 +0100
  5187. +++ linux-4.1.13/drivers/clocksource/timer-atmel-st.c 2015-11-29 09:23:09.537618965 +0100
  5188. @@ -131,6 +131,7 @@
  5189. break;
  5190. case CLOCK_EVT_MODE_SHUTDOWN:
  5191. case CLOCK_EVT_MODE_UNUSED:
  5192. + remove_irq(NR_IRQS_LEGACY + AT91_ID_SYS, &at91rm9200_timer_irq);
  5193. case CLOCK_EVT_MODE_RESUME:
  5194. irqmask = 0;
  5195. break;
  5196. diff -Nur linux-4.1.13.orig/drivers/cpufreq/cpufreq.c linux-4.1.13/drivers/cpufreq/cpufreq.c
  5197. --- linux-4.1.13.orig/drivers/cpufreq/cpufreq.c 2015-11-09 23:34:10.000000000 +0100
  5198. +++ linux-4.1.13/drivers/cpufreq/cpufreq.c 2015-11-29 09:23:09.545618436 +0100
  5199. @@ -64,12 +64,6 @@
  5200. return cpufreq_driver->target_index || cpufreq_driver->target;
  5201. }
  5202. -/*
  5203. - * rwsem to guarantee that cpufreq driver module doesn't unload during critical
  5204. - * sections
  5205. - */
  5206. -static DECLARE_RWSEM(cpufreq_rwsem);
  5207. -
  5208. /* internal prototypes */
  5209. static int __cpufreq_governor(struct cpufreq_policy *policy,
  5210. unsigned int event);
  5211. @@ -215,9 +209,6 @@
  5212. if (cpu >= nr_cpu_ids)
  5213. return NULL;
  5214. - if (!down_read_trylock(&cpufreq_rwsem))
  5215. - return NULL;
  5216. -
  5217. /* get the cpufreq driver */
  5218. read_lock_irqsave(&cpufreq_driver_lock, flags);
  5219. @@ -230,9 +221,6 @@
  5220. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  5221. - if (!policy)
  5222. - up_read(&cpufreq_rwsem);
  5223. -
  5224. return policy;
  5225. }
  5226. EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
  5227. @@ -240,7 +228,6 @@
  5228. void cpufreq_cpu_put(struct cpufreq_policy *policy)
  5229. {
  5230. kobject_put(&policy->kobj);
  5231. - up_read(&cpufreq_rwsem);
  5232. }
  5233. EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
  5234. @@ -765,9 +752,6 @@
  5235. struct freq_attr *fattr = to_attr(attr);
  5236. ssize_t ret;
  5237. - if (!down_read_trylock(&cpufreq_rwsem))
  5238. - return -EINVAL;
  5239. -
  5240. down_read(&policy->rwsem);
  5241. if (fattr->show)
  5242. @@ -776,7 +760,6 @@
  5243. ret = -EIO;
  5244. up_read(&policy->rwsem);
  5245. - up_read(&cpufreq_rwsem);
  5246. return ret;
  5247. }
  5248. @@ -793,9 +776,6 @@
  5249. if (!cpu_online(policy->cpu))
  5250. goto unlock;
  5251. - if (!down_read_trylock(&cpufreq_rwsem))
  5252. - goto unlock;
  5253. -
  5254. down_write(&policy->rwsem);
  5255. if (fattr->store)
  5256. @@ -804,8 +784,6 @@
  5257. ret = -EIO;
  5258. up_write(&policy->rwsem);
  5259. -
  5260. - up_read(&cpufreq_rwsem);
  5261. unlock:
  5262. put_online_cpus();
  5263. @@ -1117,16 +1095,12 @@
  5264. if (unlikely(policy))
  5265. return 0;
  5266. - if (!down_read_trylock(&cpufreq_rwsem))
  5267. - return 0;
  5268. -
  5269. /* Check if this cpu was hot-unplugged earlier and has siblings */
  5270. read_lock_irqsave(&cpufreq_driver_lock, flags);
  5271. for_each_policy(policy) {
  5272. if (cpumask_test_cpu(cpu, policy->related_cpus)) {
  5273. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  5274. ret = cpufreq_add_policy_cpu(policy, cpu, dev);
  5275. - up_read(&cpufreq_rwsem);
  5276. return ret;
  5277. }
  5278. }
  5279. @@ -1269,8 +1243,6 @@
  5280. kobject_uevent(&policy->kobj, KOBJ_ADD);
  5281. - up_read(&cpufreq_rwsem);
  5282. -
  5283. /* Callback for handling stuff after policy is ready */
  5284. if (cpufreq_driver->ready)
  5285. cpufreq_driver->ready(policy);
  5286. @@ -1304,8 +1276,6 @@
  5287. cpufreq_policy_free(policy);
  5288. nomem_out:
  5289. - up_read(&cpufreq_rwsem);
  5290. -
  5291. return ret;
  5292. }
  5293. @@ -2499,19 +2469,20 @@
  5294. pr_debug("unregistering driver %s\n", driver->name);
  5295. + /* Protect against concurrent cpu hotplug */
  5296. + get_online_cpus();
  5297. subsys_interface_unregister(&cpufreq_interface);
  5298. if (cpufreq_boost_supported())
  5299. cpufreq_sysfs_remove_file(&boost.attr);
  5300. unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
  5301. - down_write(&cpufreq_rwsem);
  5302. write_lock_irqsave(&cpufreq_driver_lock, flags);
  5303. cpufreq_driver = NULL;
  5304. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  5305. - up_write(&cpufreq_rwsem);
  5306. + put_online_cpus();
  5307. return 0;
  5308. }
  5309. diff -Nur linux-4.1.13.orig/drivers/cpufreq/Kconfig.x86 linux-4.1.13/drivers/cpufreq/Kconfig.x86
  5310. --- linux-4.1.13.orig/drivers/cpufreq/Kconfig.x86 2015-11-09 23:34:10.000000000 +0100
  5311. +++ linux-4.1.13/drivers/cpufreq/Kconfig.x86 2015-11-29 09:23:09.545618436 +0100
  5312. @@ -123,7 +123,7 @@
  5313. config X86_POWERNOW_K8
  5314. tristate "AMD Opteron/Athlon64 PowerNow!"
  5315. - depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ
  5316. + depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ && !PREEMPT_RT_BASE
  5317. help
  5318. This adds the CPUFreq driver for K8/early Opteron/Athlon64 processors.
  5319. Support for K10 and newer processors is now in acpi-cpufreq.
  5320. diff -Nur linux-4.1.13.orig/drivers/gpio/gpio-omap.c linux-4.1.13/drivers/gpio/gpio-omap.c
  5321. --- linux-4.1.13.orig/drivers/gpio/gpio-omap.c 2015-11-09 23:34:10.000000000 +0100
  5322. +++ linux-4.1.13/drivers/gpio/gpio-omap.c 2015-11-29 09:23:09.545618436 +0100
  5323. @@ -57,7 +57,7 @@
  5324. u32 saved_datain;
  5325. u32 level_mask;
  5326. u32 toggle_mask;
  5327. - spinlock_t lock;
  5328. + raw_spinlock_t lock;
  5329. struct gpio_chip chip;
  5330. struct clk *dbck;
  5331. u32 mod_usage;
  5332. @@ -498,14 +498,14 @@
  5333. (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
  5334. return -EINVAL;
  5335. - spin_lock_irqsave(&bank->lock, flags);
  5336. + raw_spin_lock_irqsave(&bank->lock, flags);
  5337. retval = omap_set_gpio_triggering(bank, offset, type);
  5338. omap_gpio_init_irq(bank, offset);
  5339. if (!omap_gpio_is_input(bank, offset)) {
  5340. - spin_unlock_irqrestore(&bank->lock, flags);
  5341. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5342. return -EINVAL;
  5343. }
  5344. - spin_unlock_irqrestore(&bank->lock, flags);
  5345. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5346. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  5347. __irq_set_handler_locked(d->irq, handle_level_irq);
  5348. @@ -626,14 +626,14 @@
  5349. return -EINVAL;
  5350. }
  5351. - spin_lock_irqsave(&bank->lock, flags);
  5352. + raw_spin_lock_irqsave(&bank->lock, flags);
  5353. if (enable)
  5354. bank->context.wake_en |= gpio_bit;
  5355. else
  5356. bank->context.wake_en &= ~gpio_bit;
  5357. writel_relaxed(bank->context.wake_en, bank->base + bank->regs->wkup_en);
  5358. - spin_unlock_irqrestore(&bank->lock, flags);
  5359. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5360. return 0;
  5361. }
  5362. @@ -668,7 +668,7 @@
  5363. if (!BANK_USED(bank))
  5364. pm_runtime_get_sync(bank->dev);
  5365. - spin_lock_irqsave(&bank->lock, flags);
  5366. + raw_spin_lock_irqsave(&bank->lock, flags);
  5367. /* Set trigger to none. You need to enable the desired trigger with
  5368. * request_irq() or set_irq_type(). Only do this if the IRQ line has
  5369. * not already been requested.
  5370. @@ -678,7 +678,7 @@
  5371. omap_enable_gpio_module(bank, offset);
  5372. }
  5373. bank->mod_usage |= BIT(offset);
  5374. - spin_unlock_irqrestore(&bank->lock, flags);
  5375. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5376. return 0;
  5377. }
  5378. @@ -688,11 +688,11 @@
  5379. struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
  5380. unsigned long flags;
  5381. - spin_lock_irqsave(&bank->lock, flags);
  5382. + raw_spin_lock_irqsave(&bank->lock, flags);
  5383. bank->mod_usage &= ~(BIT(offset));
  5384. omap_disable_gpio_module(bank, offset);
  5385. omap_reset_gpio(bank, offset);
  5386. - spin_unlock_irqrestore(&bank->lock, flags);
  5387. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5388. /*
  5389. * If this is the last gpio to be freed in the bank,
  5390. @@ -794,9 +794,9 @@
  5391. if (!BANK_USED(bank))
  5392. pm_runtime_get_sync(bank->dev);
  5393. - spin_lock_irqsave(&bank->lock, flags);
  5394. + raw_spin_lock_irqsave(&bank->lock, flags);
  5395. omap_gpio_init_irq(bank, offset);
  5396. - spin_unlock_irqrestore(&bank->lock, flags);
  5397. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5398. omap_gpio_unmask_irq(d);
  5399. return 0;
  5400. @@ -808,11 +808,11 @@
  5401. unsigned long flags;
  5402. unsigned offset = d->hwirq;
  5403. - spin_lock_irqsave(&bank->lock, flags);
  5404. + raw_spin_lock_irqsave(&bank->lock, flags);
  5405. bank->irq_usage &= ~(BIT(offset));
  5406. omap_disable_gpio_module(bank, offset);
  5407. omap_reset_gpio(bank, offset);
  5408. - spin_unlock_irqrestore(&bank->lock, flags);
  5409. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5410. /*
  5411. * If this is the last IRQ to be freed in the bank,
  5412. @@ -836,10 +836,10 @@
  5413. unsigned offset = d->hwirq;
  5414. unsigned long flags;
  5415. - spin_lock_irqsave(&bank->lock, flags);
  5416. + raw_spin_lock_irqsave(&bank->lock, flags);
  5417. omap_set_gpio_irqenable(bank, offset, 0);
  5418. omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
  5419. - spin_unlock_irqrestore(&bank->lock, flags);
  5420. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5421. }
  5422. static void omap_gpio_unmask_irq(struct irq_data *d)
  5423. @@ -849,7 +849,7 @@
  5424. u32 trigger = irqd_get_trigger_type(d);
  5425. unsigned long flags;
  5426. - spin_lock_irqsave(&bank->lock, flags);
  5427. + raw_spin_lock_irqsave(&bank->lock, flags);
  5428. if (trigger)
  5429. omap_set_gpio_triggering(bank, offset, trigger);
  5430. @@ -861,7 +861,7 @@
  5431. }
  5432. omap_set_gpio_irqenable(bank, offset, 1);
  5433. - spin_unlock_irqrestore(&bank->lock, flags);
  5434. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5435. }
  5436. /*---------------------------------------------------------------------*/
  5437. @@ -874,9 +874,9 @@
  5438. OMAP_MPUIO_GPIO_MASKIT / bank->stride;
  5439. unsigned long flags;
  5440. - spin_lock_irqsave(&bank->lock, flags);
  5441. + raw_spin_lock_irqsave(&bank->lock, flags);
  5442. writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
  5443. - spin_unlock_irqrestore(&bank->lock, flags);
  5444. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5445. return 0;
  5446. }
  5447. @@ -889,9 +889,9 @@
  5448. OMAP_MPUIO_GPIO_MASKIT / bank->stride;
  5449. unsigned long flags;
  5450. - spin_lock_irqsave(&bank->lock, flags);
  5451. + raw_spin_lock_irqsave(&bank->lock, flags);
  5452. writel_relaxed(bank->context.wake_en, mask_reg);
  5453. - spin_unlock_irqrestore(&bank->lock, flags);
  5454. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5455. return 0;
  5456. }
  5457. @@ -937,9 +937,9 @@
  5458. bank = container_of(chip, struct gpio_bank, chip);
  5459. reg = bank->base + bank->regs->direction;
  5460. - spin_lock_irqsave(&bank->lock, flags);
  5461. + raw_spin_lock_irqsave(&bank->lock, flags);
  5462. dir = !!(readl_relaxed(reg) & BIT(offset));
  5463. - spin_unlock_irqrestore(&bank->lock, flags);
  5464. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5465. return dir;
  5466. }
  5467. @@ -949,9 +949,9 @@
  5468. unsigned long flags;
  5469. bank = container_of(chip, struct gpio_bank, chip);
  5470. - spin_lock_irqsave(&bank->lock, flags);
  5471. + raw_spin_lock_irqsave(&bank->lock, flags);
  5472. omap_set_gpio_direction(bank, offset, 1);
  5473. - spin_unlock_irqrestore(&bank->lock, flags);
  5474. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5475. return 0;
  5476. }
  5477. @@ -973,10 +973,10 @@
  5478. unsigned long flags;
  5479. bank = container_of(chip, struct gpio_bank, chip);
  5480. - spin_lock_irqsave(&bank->lock, flags);
  5481. + raw_spin_lock_irqsave(&bank->lock, flags);
  5482. bank->set_dataout(bank, offset, value);
  5483. omap_set_gpio_direction(bank, offset, 0);
  5484. - spin_unlock_irqrestore(&bank->lock, flags);
  5485. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5486. return 0;
  5487. }
  5488. @@ -988,9 +988,9 @@
  5489. bank = container_of(chip, struct gpio_bank, chip);
  5490. - spin_lock_irqsave(&bank->lock, flags);
  5491. + raw_spin_lock_irqsave(&bank->lock, flags);
  5492. omap2_set_gpio_debounce(bank, offset, debounce);
  5493. - spin_unlock_irqrestore(&bank->lock, flags);
  5494. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5495. return 0;
  5496. }
  5497. @@ -1001,9 +1001,9 @@
  5498. unsigned long flags;
  5499. bank = container_of(chip, struct gpio_bank, chip);
  5500. - spin_lock_irqsave(&bank->lock, flags);
  5501. + raw_spin_lock_irqsave(&bank->lock, flags);
  5502. bank->set_dataout(bank, offset, value);
  5503. - spin_unlock_irqrestore(&bank->lock, flags);
  5504. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5505. }
  5506. /*---------------------------------------------------------------------*/
  5507. @@ -1199,7 +1199,7 @@
  5508. else
  5509. bank->set_dataout = omap_set_gpio_dataout_mask;
  5510. - spin_lock_init(&bank->lock);
  5511. + raw_spin_lock_init(&bank->lock);
  5512. /* Static mapping, never released */
  5513. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  5514. @@ -1246,7 +1246,7 @@
  5515. unsigned long flags;
  5516. u32 wake_low, wake_hi;
  5517. - spin_lock_irqsave(&bank->lock, flags);
  5518. + raw_spin_lock_irqsave(&bank->lock, flags);
  5519. /*
  5520. * Only edges can generate a wakeup event to the PRCM.
  5521. @@ -1299,7 +1299,7 @@
  5522. bank->get_context_loss_count(bank->dev);
  5523. omap_gpio_dbck_disable(bank);
  5524. - spin_unlock_irqrestore(&bank->lock, flags);
  5525. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5526. return 0;
  5527. }
  5528. @@ -1314,7 +1314,7 @@
  5529. unsigned long flags;
  5530. int c;
  5531. - spin_lock_irqsave(&bank->lock, flags);
  5532. + raw_spin_lock_irqsave(&bank->lock, flags);
  5533. /*
  5534. * On the first resume during the probe, the context has not
  5535. @@ -1350,14 +1350,14 @@
  5536. if (c != bank->context_loss_count) {
  5537. omap_gpio_restore_context(bank);
  5538. } else {
  5539. - spin_unlock_irqrestore(&bank->lock, flags);
  5540. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5541. return 0;
  5542. }
  5543. }
  5544. }
  5545. if (!bank->workaround_enabled) {
  5546. - spin_unlock_irqrestore(&bank->lock, flags);
  5547. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5548. return 0;
  5549. }
  5550. @@ -1412,7 +1412,7 @@
  5551. }
  5552. bank->workaround_enabled = false;
  5553. - spin_unlock_irqrestore(&bank->lock, flags);
  5554. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5555. return 0;
  5556. }
  5557. diff -Nur linux-4.1.13.orig/drivers/gpu/drm/i915/i915_gem_execbuffer.c linux-4.1.13/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  5558. --- linux-4.1.13.orig/drivers/gpu/drm/i915/i915_gem_execbuffer.c 2015-11-09 23:34:10.000000000 +0100
  5559. +++ linux-4.1.13/drivers/gpu/drm/i915/i915_gem_execbuffer.c 2015-11-29 09:23:09.545618436 +0100
  5560. @@ -32,6 +32,7 @@
  5561. #include "i915_trace.h"
  5562. #include "intel_drv.h"
  5563. #include <linux/dma_remapping.h>
  5564. +#include <linux/uaccess.h>
  5565. #define __EXEC_OBJECT_HAS_PIN (1<<31)
  5566. #define __EXEC_OBJECT_HAS_FENCE (1<<30)
  5567. @@ -465,7 +466,7 @@
  5568. }
  5569. /* We can't wait for rendering with pagefaults disabled */
  5570. - if (obj->active && in_atomic())
  5571. + if (obj->active && pagefault_disabled())
  5572. return -EFAULT;
  5573. if (use_cpu_reloc(obj))
  5574. @@ -1338,7 +1339,9 @@
  5575. return ret;
  5576. }
  5577. +#ifndef CONFIG_PREEMPT_RT_BASE
  5578. trace_i915_gem_ring_dispatch(intel_ring_get_request(ring), dispatch_flags);
  5579. +#endif
  5580. i915_gem_execbuffer_move_to_active(vmas, ring);
  5581. i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
  5582. diff -Nur linux-4.1.13.orig/drivers/gpu/drm/i915/i915_gem_shrinker.c linux-4.1.13/drivers/gpu/drm/i915/i915_gem_shrinker.c
  5583. --- linux-4.1.13.orig/drivers/gpu/drm/i915/i915_gem_shrinker.c 2015-11-09 23:34:10.000000000 +0100
  5584. +++ linux-4.1.13/drivers/gpu/drm/i915/i915_gem_shrinker.c 2015-11-29 09:23:09.545618436 +0100
  5585. @@ -39,7 +39,7 @@
  5586. if (!mutex_is_locked(mutex))
  5587. return false;
  5588. -#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
  5589. +#if (defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)) && !defined(CONFIG_PREEMPT_RT_BASE)
  5590. return mutex->owner == task;
  5591. #else
  5592. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  5593. diff -Nur linux-4.1.13.orig/drivers/gpu/drm/i915/intel_display.c linux-4.1.13/drivers/gpu/drm/i915/intel_display.c
  5594. --- linux-4.1.13.orig/drivers/gpu/drm/i915/intel_display.c 2015-11-09 23:34:10.000000000 +0100
  5595. +++ linux-4.1.13/drivers/gpu/drm/i915/intel_display.c 2015-11-29 09:23:09.549618170 +0100
  5596. @@ -10088,7 +10088,7 @@
  5597. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  5598. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5599. - WARN_ON(!in_interrupt());
  5600. + WARN_ON_NONRT(!in_interrupt());
  5601. if (crtc == NULL)
  5602. return;
  5603. diff -Nur linux-4.1.13.orig/drivers/i2c/busses/i2c-omap.c linux-4.1.13/drivers/i2c/busses/i2c-omap.c
  5604. --- linux-4.1.13.orig/drivers/i2c/busses/i2c-omap.c 2015-11-09 23:34:10.000000000 +0100
  5605. +++ linux-4.1.13/drivers/i2c/busses/i2c-omap.c 2015-11-29 09:23:09.549618170 +0100
  5606. @@ -996,15 +996,12 @@
  5607. u16 mask;
  5608. u16 stat;
  5609. - spin_lock(&dev->lock);
  5610. - mask = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
  5611. stat = omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
  5612. + mask = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
  5613. if (stat & mask)
  5614. ret = IRQ_WAKE_THREAD;
  5615. - spin_unlock(&dev->lock);
  5616. -
  5617. return ret;
  5618. }
  5619. diff -Nur linux-4.1.13.orig/drivers/ide/alim15x3.c linux-4.1.13/drivers/ide/alim15x3.c
  5620. --- linux-4.1.13.orig/drivers/ide/alim15x3.c 2015-11-09 23:34:10.000000000 +0100
  5621. +++ linux-4.1.13/drivers/ide/alim15x3.c 2015-11-29 09:23:09.549618170 +0100
  5622. @@ -234,7 +234,7 @@
  5623. isa_dev = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
  5624. - local_irq_save(flags);
  5625. + local_irq_save_nort(flags);
  5626. if (m5229_revision < 0xC2) {
  5627. /*
  5628. @@ -325,7 +325,7 @@
  5629. }
  5630. pci_dev_put(north);
  5631. pci_dev_put(isa_dev);
  5632. - local_irq_restore(flags);
  5633. + local_irq_restore_nort(flags);
  5634. return 0;
  5635. }
  5636. diff -Nur linux-4.1.13.orig/drivers/ide/hpt366.c linux-4.1.13/drivers/ide/hpt366.c
  5637. --- linux-4.1.13.orig/drivers/ide/hpt366.c 2015-11-09 23:34:10.000000000 +0100
  5638. +++ linux-4.1.13/drivers/ide/hpt366.c 2015-11-29 09:23:09.557617634 +0100
  5639. @@ -1241,7 +1241,7 @@
  5640. dma_old = inb(base + 2);
  5641. - local_irq_save(flags);
  5642. + local_irq_save_nort(flags);
  5643. dma_new = dma_old;
  5644. pci_read_config_byte(dev, hwif->channel ? 0x4b : 0x43, &masterdma);
  5645. @@ -1252,7 +1252,7 @@
  5646. if (dma_new != dma_old)
  5647. outb(dma_new, base + 2);
  5648. - local_irq_restore(flags);
  5649. + local_irq_restore_nort(flags);
  5650. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx\n",
  5651. hwif->name, base, base + 7);
  5652. diff -Nur linux-4.1.13.orig/drivers/ide/ide-io.c linux-4.1.13/drivers/ide/ide-io.c
  5653. --- linux-4.1.13.orig/drivers/ide/ide-io.c 2015-11-09 23:34:10.000000000 +0100
  5654. +++ linux-4.1.13/drivers/ide/ide-io.c 2015-11-29 09:23:09.557617634 +0100
  5655. @@ -659,7 +659,7 @@
  5656. /* disable_irq_nosync ?? */
  5657. disable_irq(hwif->irq);
  5658. /* local CPU only, as if we were handling an interrupt */
  5659. - local_irq_disable();
  5660. + local_irq_disable_nort();
  5661. if (hwif->polling) {
  5662. startstop = handler(drive);
  5663. } else if (drive_is_ready(drive)) {
  5664. diff -Nur linux-4.1.13.orig/drivers/ide/ide-iops.c linux-4.1.13/drivers/ide/ide-iops.c
  5665. --- linux-4.1.13.orig/drivers/ide/ide-iops.c 2015-11-09 23:34:10.000000000 +0100
  5666. +++ linux-4.1.13/drivers/ide/ide-iops.c 2015-11-29 09:23:09.557617634 +0100
  5667. @@ -129,12 +129,12 @@
  5668. if ((stat & ATA_BUSY) == 0)
  5669. break;
  5670. - local_irq_restore(flags);
  5671. + local_irq_restore_nort(flags);
  5672. *rstat = stat;
  5673. return -EBUSY;
  5674. }
  5675. }
  5676. - local_irq_restore(flags);
  5677. + local_irq_restore_nort(flags);
  5678. }
  5679. /*
  5680. * Allow status to settle, then read it again.
  5681. diff -Nur linux-4.1.13.orig/drivers/ide/ide-io-std.c linux-4.1.13/drivers/ide/ide-io-std.c
  5682. --- linux-4.1.13.orig/drivers/ide/ide-io-std.c 2015-11-09 23:34:10.000000000 +0100
  5683. +++ linux-4.1.13/drivers/ide/ide-io-std.c 2015-11-29 09:23:09.557617634 +0100
  5684. @@ -175,7 +175,7 @@
  5685. unsigned long uninitialized_var(flags);
  5686. if ((io_32bit & 2) && !mmio) {
  5687. - local_irq_save(flags);
  5688. + local_irq_save_nort(flags);
  5689. ata_vlb_sync(io_ports->nsect_addr);
  5690. }
  5691. @@ -186,7 +186,7 @@
  5692. insl(data_addr, buf, words);
  5693. if ((io_32bit & 2) && !mmio)
  5694. - local_irq_restore(flags);
  5695. + local_irq_restore_nort(flags);
  5696. if (((len + 1) & 3) < 2)
  5697. return;
  5698. @@ -219,7 +219,7 @@
  5699. unsigned long uninitialized_var(flags);
  5700. if ((io_32bit & 2) && !mmio) {
  5701. - local_irq_save(flags);
  5702. + local_irq_save_nort(flags);
  5703. ata_vlb_sync(io_ports->nsect_addr);
  5704. }
  5705. @@ -230,7 +230,7 @@
  5706. outsl(data_addr, buf, words);
  5707. if ((io_32bit & 2) && !mmio)
  5708. - local_irq_restore(flags);
  5709. + local_irq_restore_nort(flags);
  5710. if (((len + 1) & 3) < 2)
  5711. return;
  5712. diff -Nur linux-4.1.13.orig/drivers/ide/ide-probe.c linux-4.1.13/drivers/ide/ide-probe.c
  5713. --- linux-4.1.13.orig/drivers/ide/ide-probe.c 2015-11-09 23:34:10.000000000 +0100
  5714. +++ linux-4.1.13/drivers/ide/ide-probe.c 2015-11-29 09:23:09.557617634 +0100
  5715. @@ -196,10 +196,10 @@
  5716. int bswap = 1;
  5717. /* local CPU only; some systems need this */
  5718. - local_irq_save(flags);
  5719. + local_irq_save_nort(flags);
  5720. /* read 512 bytes of id info */
  5721. hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
  5722. - local_irq_restore(flags);
  5723. + local_irq_restore_nort(flags);
  5724. drive->dev_flags |= IDE_DFLAG_ID_READ;
  5725. #ifdef DEBUG
  5726. diff -Nur linux-4.1.13.orig/drivers/ide/ide-taskfile.c linux-4.1.13/drivers/ide/ide-taskfile.c
  5727. --- linux-4.1.13.orig/drivers/ide/ide-taskfile.c 2015-11-09 23:34:10.000000000 +0100
  5728. +++ linux-4.1.13/drivers/ide/ide-taskfile.c 2015-11-29 09:23:09.557617634 +0100
  5729. @@ -250,7 +250,7 @@
  5730. page_is_high = PageHighMem(page);
  5731. if (page_is_high)
  5732. - local_irq_save(flags);
  5733. + local_irq_save_nort(flags);
  5734. buf = kmap_atomic(page) + offset;
  5735. @@ -271,7 +271,7 @@
  5736. kunmap_atomic(buf);
  5737. if (page_is_high)
  5738. - local_irq_restore(flags);
  5739. + local_irq_restore_nort(flags);
  5740. len -= nr_bytes;
  5741. }
  5742. @@ -414,7 +414,7 @@
  5743. }
  5744. if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
  5745. - local_irq_disable();
  5746. + local_irq_disable_nort();
  5747. ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
  5748. diff -Nur linux-4.1.13.orig/drivers/infiniband/ulp/ipoib/ipoib_multicast.c linux-4.1.13/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  5749. --- linux-4.1.13.orig/drivers/infiniband/ulp/ipoib/ipoib_multicast.c 2015-11-09 23:34:10.000000000 +0100
  5750. +++ linux-4.1.13/drivers/infiniband/ulp/ipoib/ipoib_multicast.c 2015-11-29 09:23:09.557617634 +0100
  5751. @@ -821,7 +821,7 @@
  5752. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  5753. - local_irq_save(flags);
  5754. + local_irq_save_nort(flags);
  5755. netif_addr_lock(dev);
  5756. spin_lock(&priv->lock);
  5757. @@ -903,7 +903,7 @@
  5758. spin_unlock(&priv->lock);
  5759. netif_addr_unlock(dev);
  5760. - local_irq_restore(flags);
  5761. + local_irq_restore_nort(flags);
  5762. /*
  5763. * make sure the in-flight joins have finished before we attempt
  5764. diff -Nur linux-4.1.13.orig/drivers/input/gameport/gameport.c linux-4.1.13/drivers/input/gameport/gameport.c
  5765. --- linux-4.1.13.orig/drivers/input/gameport/gameport.c 2015-11-09 23:34:10.000000000 +0100
  5766. +++ linux-4.1.13/drivers/input/gameport/gameport.c 2015-11-29 09:23:09.557617634 +0100
  5767. @@ -124,12 +124,12 @@
  5768. tx = 1 << 30;
  5769. for(i = 0; i < 50; i++) {
  5770. - local_irq_save(flags);
  5771. + local_irq_save_nort(flags);
  5772. GET_TIME(t1);
  5773. for (t = 0; t < 50; t++) gameport_read(gameport);
  5774. GET_TIME(t2);
  5775. GET_TIME(t3);
  5776. - local_irq_restore(flags);
  5777. + local_irq_restore_nort(flags);
  5778. udelay(i * 10);
  5779. if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
  5780. }
  5781. @@ -148,11 +148,11 @@
  5782. tx = 1 << 30;
  5783. for(i = 0; i < 50; i++) {
  5784. - local_irq_save(flags);
  5785. + local_irq_save_nort(flags);
  5786. rdtscl(t1);
  5787. for (t = 0; t < 50; t++) gameport_read(gameport);
  5788. rdtscl(t2);
  5789. - local_irq_restore(flags);
  5790. + local_irq_restore_nort(flags);
  5791. udelay(i * 10);
  5792. if (t2 - t1 < tx) tx = t2 - t1;
  5793. }
  5794. diff -Nur linux-4.1.13.orig/drivers/leds/trigger/Kconfig linux-4.1.13/drivers/leds/trigger/Kconfig
  5795. --- linux-4.1.13.orig/drivers/leds/trigger/Kconfig 2015-11-09 23:34:10.000000000 +0100
  5796. +++ linux-4.1.13/drivers/leds/trigger/Kconfig 2015-11-29 09:23:09.557617634 +0100
  5797. @@ -61,7 +61,7 @@
  5798. config LEDS_TRIGGER_CPU
  5799. bool "LED CPU Trigger"
  5800. - depends on LEDS_TRIGGERS
  5801. + depends on LEDS_TRIGGERS && !PREEMPT_RT_BASE
  5802. help
  5803. This allows LEDs to be controlled by active CPUs. This shows
  5804. the active CPUs across an array of LEDs so you can see which
  5805. diff -Nur linux-4.1.13.orig/drivers/md/bcache/Kconfig linux-4.1.13/drivers/md/bcache/Kconfig
  5806. --- linux-4.1.13.orig/drivers/md/bcache/Kconfig 2015-11-09 23:34:10.000000000 +0100
  5807. +++ linux-4.1.13/drivers/md/bcache/Kconfig 2015-11-29 09:23:09.557617634 +0100
  5808. @@ -1,6 +1,7 @@
  5809. config BCACHE
  5810. tristate "Block device as cache"
  5811. + depends on !PREEMPT_RT_FULL
  5812. ---help---
  5813. Allows a block device to be used as cache for other devices; uses
  5814. a btree for indexing and the layout is optimized for SSDs.
  5815. diff -Nur linux-4.1.13.orig/drivers/md/dm.c linux-4.1.13/drivers/md/dm.c
  5816. --- linux-4.1.13.orig/drivers/md/dm.c 2015-11-09 23:34:10.000000000 +0100
  5817. +++ linux-4.1.13/drivers/md/dm.c 2015-11-29 09:23:09.557617634 +0100
  5818. @@ -2132,7 +2132,7 @@
  5819. /* Establish tio->ti before queuing work (map_tio_request) */
  5820. tio->ti = ti;
  5821. queue_kthread_work(&md->kworker, &tio->work);
  5822. - BUG_ON(!irqs_disabled());
  5823. + BUG_ON_NONRT(!irqs_disabled());
  5824. }
  5825. goto out;
  5826. diff -Nur linux-4.1.13.orig/drivers/md/raid5.c linux-4.1.13/drivers/md/raid5.c
  5827. --- linux-4.1.13.orig/drivers/md/raid5.c 2015-11-09 23:34:10.000000000 +0100
  5828. +++ linux-4.1.13/drivers/md/raid5.c 2015-11-29 09:23:09.561617368 +0100
  5829. @@ -1918,8 +1918,9 @@
  5830. struct raid5_percpu *percpu;
  5831. unsigned long cpu;
  5832. - cpu = get_cpu();
  5833. + cpu = get_cpu_light();
  5834. percpu = per_cpu_ptr(conf->percpu, cpu);
  5835. + spin_lock(&percpu->lock);
  5836. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  5837. ops_run_biofill(sh);
  5838. overlap_clear++;
  5839. @@ -1975,7 +1976,8 @@
  5840. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  5841. wake_up(&sh->raid_conf->wait_for_overlap);
  5842. }
  5843. - put_cpu();
  5844. + spin_unlock(&percpu->lock);
  5845. + put_cpu_light();
  5846. }
  5847. static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp)
  5848. @@ -6363,6 +6365,7 @@
  5849. __func__, cpu);
  5850. break;
  5851. }
  5852. + spin_lock_init(&per_cpu_ptr(conf->percpu, cpu)->lock);
  5853. }
  5854. put_online_cpus();
  5855. diff -Nur linux-4.1.13.orig/drivers/md/raid5.h linux-4.1.13/drivers/md/raid5.h
  5856. --- linux-4.1.13.orig/drivers/md/raid5.h 2015-11-09 23:34:10.000000000 +0100
  5857. +++ linux-4.1.13/drivers/md/raid5.h 2015-11-29 09:23:09.561617368 +0100
  5858. @@ -495,6 +495,7 @@
  5859. int recovery_disabled;
  5860. /* per cpu variables */
  5861. struct raid5_percpu {
  5862. + spinlock_t lock; /* Protection for -RT */
  5863. struct page *spare_page; /* Used when checking P/Q in raid6 */
  5864. struct flex_array *scribble; /* space for constructing buffer
  5865. * lists and performing address
  5866. diff -Nur linux-4.1.13.orig/drivers/misc/hwlat_detector.c linux-4.1.13/drivers/misc/hwlat_detector.c
  5867. --- linux-4.1.13.orig/drivers/misc/hwlat_detector.c 1970-01-01 01:00:00.000000000 +0100
  5868. +++ linux-4.1.13/drivers/misc/hwlat_detector.c 2015-11-29 09:23:09.561617368 +0100
  5869. @@ -0,0 +1,1240 @@
  5870. +/*
  5871. + * hwlat_detector.c - A simple Hardware Latency detector.
  5872. + *
  5873. + * Use this module to detect large system latencies induced by the behavior of
  5874. + * certain underlying system hardware or firmware, independent of Linux itself.
  5875. + * The code was developed originally to detect the presence of SMIs on Intel
  5876. + * and AMD systems, although there is no dependency upon x86 herein.
  5877. + *
  5878. + * The classical example usage of this module is in detecting the presence of
  5879. + * SMIs or System Management Interrupts on Intel and AMD systems. An SMI is a
  5880. + * somewhat special form of hardware interrupt spawned from earlier CPU debug
  5881. + * modes in which the (BIOS/EFI/etc.) firmware arranges for the South Bridge
  5882. + * LPC (or other device) to generate a special interrupt under certain
  5883. + * circumstances, for example, upon expiration of a special SMI timer device,
  5884. + * due to certain external thermal readings, on certain I/O address accesses,
  5885. + * and other situations. An SMI hits a special CPU pin, triggers a special
  5886. + * SMI mode (complete with special memory map), and the OS is unaware.
  5887. + *
  5888. + * Although certain hardware-inducing latencies are necessary (for example,
  5889. + * a modern system often requires an SMI handler for correct thermal control
  5890. + * and remote management) they can wreak havoc upon any OS-level performance
  5891. + * guarantees toward low-latency, especially when the OS is not even made
  5892. + * aware of the presence of these interrupts. For this reason, we need a
  5893. + * somewhat brute force mechanism to detect these interrupts. In this case,
  5894. + * we do it by hogging all of the CPU(s) for configurable timer intervals,
  5895. + * sampling the built-in CPU timer, looking for discontiguous readings.
  5896. + *
  5897. + * WARNING: This implementation necessarily introduces latencies. Therefore,
  5898. + * you should NEVER use this module in a production environment
  5899. + * requiring any kind of low-latency performance guarantee(s).
  5900. + *
  5901. + * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com>
  5902. + *
  5903. + * Includes useful feedback from Clark Williams <clark@redhat.com>
  5904. + *
  5905. + * This file is licensed under the terms of the GNU General Public
  5906. + * License version 2. This program is licensed "as is" without any
  5907. + * warranty of any kind, whether express or implied.
  5908. + */
  5909. +
  5910. +#include <linux/module.h>
  5911. +#include <linux/init.h>
  5912. +#include <linux/ring_buffer.h>
  5913. +#include <linux/time.h>
  5914. +#include <linux/hrtimer.h>
  5915. +#include <linux/kthread.h>
  5916. +#include <linux/debugfs.h>
  5917. +#include <linux/seq_file.h>
  5918. +#include <linux/uaccess.h>
  5919. +#include <linux/version.h>
  5920. +#include <linux/delay.h>
  5921. +#include <linux/slab.h>
  5922. +#include <linux/trace_clock.h>
  5923. +
  5924. +#define BUF_SIZE_DEFAULT 262144UL /* 8K*(sizeof(entry)) */
  5925. +#define BUF_FLAGS (RB_FL_OVERWRITE) /* no block on full */
  5926. +#define U64STR_SIZE 22 /* 20 digits max */
  5927. +
  5928. +#define VERSION "1.0.0"
  5929. +#define BANNER "hwlat_detector: "
  5930. +#define DRVNAME "hwlat_detector"
  5931. +#define DEFAULT_SAMPLE_WINDOW 1000000 /* 1s */
  5932. +#define DEFAULT_SAMPLE_WIDTH 500000 /* 0.5s */
  5933. +#define DEFAULT_LAT_THRESHOLD 10 /* 10us */
  5934. +
  5935. +/* Module metadata */
  5936. +
  5937. +MODULE_LICENSE("GPL");
  5938. +MODULE_AUTHOR("Jon Masters <jcm@redhat.com>");
  5939. +MODULE_DESCRIPTION("A simple hardware latency detector");
  5940. +MODULE_VERSION(VERSION);
  5941. +
  5942. +/* Module parameters */
  5943. +
  5944. +static int debug;
  5945. +static int enabled;
  5946. +static int threshold;
  5947. +
  5948. +module_param(debug, int, 0); /* enable debug */
  5949. +module_param(enabled, int, 0); /* enable detector */
  5950. +module_param(threshold, int, 0); /* latency threshold */
  5951. +
  5952. +/* Buffering and sampling */
  5953. +
  5954. +static struct ring_buffer *ring_buffer; /* sample buffer */
  5955. +static DEFINE_MUTEX(ring_buffer_mutex); /* lock changes */
  5956. +static unsigned long buf_size = BUF_SIZE_DEFAULT;
  5957. +static struct task_struct *kthread; /* sampling thread */
  5958. +
  5959. +/* DebugFS filesystem entries */
  5960. +
  5961. +static struct dentry *debug_dir; /* debugfs directory */
  5962. +static struct dentry *debug_max; /* maximum TSC delta */
  5963. +static struct dentry *debug_count; /* total detect count */
  5964. +static struct dentry *debug_sample_width; /* sample width us */
  5965. +static struct dentry *debug_sample_window; /* sample window us */
  5966. +static struct dentry *debug_sample; /* raw samples us */
  5967. +static struct dentry *debug_threshold; /* threshold us */
  5968. +static struct dentry *debug_enable; /* enable/disable */
  5969. +
  5970. +/* Individual samples and global state */
  5971. +
  5972. +struct sample; /* latency sample */
  5973. +struct data; /* Global state */
  5974. +
  5975. +/* Sampling functions */
  5976. +static int __buffer_add_sample(struct sample *sample);
  5977. +static struct sample *buffer_get_sample(struct sample *sample);
  5978. +
  5979. +/* Threading and state */
  5980. +static int kthread_fn(void *unused);
  5981. +static int start_kthread(void);
  5982. +static int stop_kthread(void);
  5983. +static void __reset_stats(void);
  5984. +static int init_stats(void);
  5985. +
  5986. +/* Debugfs interface */
  5987. +static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
  5988. + size_t cnt, loff_t *ppos, const u64 *entry);
  5989. +static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
  5990. + size_t cnt, loff_t *ppos, u64 *entry);
  5991. +static int debug_sample_fopen(struct inode *inode, struct file *filp);
  5992. +static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
  5993. + size_t cnt, loff_t *ppos);
  5994. +static int debug_sample_release(struct inode *inode, struct file *filp);
  5995. +static int debug_enable_fopen(struct inode *inode, struct file *filp);
  5996. +static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
  5997. + size_t cnt, loff_t *ppos);
  5998. +static ssize_t debug_enable_fwrite(struct file *file,
  5999. + const char __user *user_buffer,
  6000. + size_t user_size, loff_t *offset);
  6001. +
  6002. +/* Initialization functions */
  6003. +static int init_debugfs(void);
  6004. +static void free_debugfs(void);
  6005. +static int detector_init(void);
  6006. +static void detector_exit(void);
  6007. +
  6008. +/* Individual latency samples are stored here when detected and packed into
  6009. + * the ring_buffer circular buffer, where they are overwritten when
  6010. + * more than buf_size/sizeof(sample) samples are received. */
  6011. +struct sample {
  6012. + u64 seqnum; /* unique sequence */
  6013. + u64 duration; /* ktime delta */
  6014. + u64 outer_duration; /* ktime delta (outer loop) */
  6015. + struct timespec timestamp; /* wall time */
  6016. + unsigned long lost;
  6017. +};
  6018. +
  6019. +/* keep the global state somewhere. */
  6020. +static struct data {
  6021. +
  6022. + struct mutex lock; /* protect changes */
  6023. +
  6024. + u64 count; /* total since reset */
  6025. + u64 max_sample; /* max hardware latency */
  6026. + u64 threshold; /* sample threshold level */
  6027. +
  6028. + u64 sample_window; /* total sampling window (on+off) */
  6029. + u64 sample_width; /* active sampling portion of window */
  6030. +
  6031. + atomic_t sample_open; /* whether the sample file is open */
  6032. +
  6033. + wait_queue_head_t wq; /* waitqeue for new sample values */
  6034. +
  6035. +} data;
  6036. +
  6037. +/**
  6038. + * __buffer_add_sample - add a new latency sample recording to the ring buffer
  6039. + * @sample: The new latency sample value
  6040. + *
  6041. + * This receives a new latency sample and records it in a global ring buffer.
  6042. + * No additional locking is used in this case.
  6043. + */
  6044. +static int __buffer_add_sample(struct sample *sample)
  6045. +{
  6046. + return ring_buffer_write(ring_buffer,
  6047. + sizeof(struct sample), sample);
  6048. +}
  6049. +
  6050. +/**
  6051. + * buffer_get_sample - remove a hardware latency sample from the ring buffer
  6052. + * @sample: Pre-allocated storage for the sample
  6053. + *
  6054. + * This retrieves a hardware latency sample from the global circular buffer
  6055. + */
  6056. +static struct sample *buffer_get_sample(struct sample *sample)
  6057. +{
  6058. + struct ring_buffer_event *e = NULL;
  6059. + struct sample *s = NULL;
  6060. + unsigned int cpu = 0;
  6061. +
  6062. + if (!sample)
  6063. + return NULL;
  6064. +
  6065. + mutex_lock(&ring_buffer_mutex);
  6066. + for_each_online_cpu(cpu) {
  6067. + e = ring_buffer_consume(ring_buffer, cpu, NULL, &sample->lost);
  6068. + if (e)
  6069. + break;
  6070. + }
  6071. +
  6072. + if (e) {
  6073. + s = ring_buffer_event_data(e);
  6074. + memcpy(sample, s, sizeof(struct sample));
  6075. + } else
  6076. + sample = NULL;
  6077. + mutex_unlock(&ring_buffer_mutex);
  6078. +
  6079. + return sample;
  6080. +}
  6081. +
  6082. +#ifndef CONFIG_TRACING
  6083. +#define time_type ktime_t
  6084. +#define time_get() ktime_get()
  6085. +#define time_to_us(x) ktime_to_us(x)
  6086. +#define time_sub(a, b) ktime_sub(a, b)
  6087. +#define init_time(a, b) (a).tv64 = b
  6088. +#define time_u64(a) ((a).tv64)
  6089. +#else
  6090. +#define time_type u64
  6091. +#define time_get() trace_clock_local()
  6092. +#define time_to_us(x) div_u64(x, 1000)
  6093. +#define time_sub(a, b) ((a) - (b))
  6094. +#define init_time(a, b) (a = b)
  6095. +#define time_u64(a) a
  6096. +#endif
  6097. +/**
  6098. + * get_sample - sample the CPU TSC and look for likely hardware latencies
  6099. + *
  6100. + * Used to repeatedly capture the CPU TSC (or similar), looking for potential
  6101. + * hardware-induced latency. Called with interrupts disabled and with
  6102. + * data.lock held.
  6103. + */
  6104. +static int get_sample(void)
  6105. +{
  6106. + time_type start, t1, t2, last_t2;
  6107. + s64 diff, total = 0;
  6108. + u64 sample = 0;
  6109. + u64 outer_sample = 0;
  6110. + int ret = -1;
  6111. +
  6112. + init_time(last_t2, 0);
  6113. + start = time_get(); /* start timestamp */
  6114. +
  6115. + do {
  6116. +
  6117. + t1 = time_get(); /* we'll look for a discontinuity */
  6118. + t2 = time_get();
  6119. +
  6120. + if (time_u64(last_t2)) {
  6121. + /* Check the delta from outer loop (t2 to next t1) */
  6122. + diff = time_to_us(time_sub(t1, last_t2));
  6123. + /* This shouldn't happen */
  6124. + if (diff < 0) {
  6125. + pr_err(BANNER "time running backwards\n");
  6126. + goto out;
  6127. + }
  6128. + if (diff > outer_sample)
  6129. + outer_sample = diff;
  6130. + }
  6131. + last_t2 = t2;
  6132. +
  6133. + total = time_to_us(time_sub(t2, start)); /* sample width */
  6134. +
  6135. + /* This checks the inner loop (t1 to t2) */
  6136. + diff = time_to_us(time_sub(t2, t1)); /* current diff */
  6137. +
  6138. + /* This shouldn't happen */
  6139. + if (diff < 0) {
  6140. + pr_err(BANNER "time running backwards\n");
  6141. + goto out;
  6142. + }
  6143. +
  6144. + if (diff > sample)
  6145. + sample = diff; /* only want highest value */
  6146. +
  6147. + } while (total <= data.sample_width);
  6148. +
  6149. + ret = 0;
  6150. +
  6151. + /* If we exceed the threshold value, we have found a hardware latency */
  6152. + if (sample > data.threshold || outer_sample > data.threshold) {
  6153. + struct sample s;
  6154. +
  6155. + ret = 1;
  6156. +
  6157. + data.count++;
  6158. + s.seqnum = data.count;
  6159. + s.duration = sample;
  6160. + s.outer_duration = outer_sample;
  6161. + s.timestamp = CURRENT_TIME;
  6162. + __buffer_add_sample(&s);
  6163. +
  6164. + /* Keep a running maximum ever recorded hardware latency */
  6165. + if (sample > data.max_sample)
  6166. + data.max_sample = sample;
  6167. + }
  6168. +
  6169. +out:
  6170. + return ret;
  6171. +}
  6172. +
  6173. +/*
  6174. + * kthread_fn - The CPU time sampling/hardware latency detection kernel thread
  6175. + * @unused: A required part of the kthread API.
  6176. + *
  6177. + * Used to periodically sample the CPU TSC via a call to get_sample. We
  6178. + * disable interrupts, which does (intentionally) introduce latency since we
  6179. + * need to ensure nothing else might be running (and thus pre-empting).
  6180. + * Obviously this should never be used in production environments.
  6181. + *
  6182. + * Currently this runs on which ever CPU it was scheduled on, but most
  6183. + * real-worald hardware latency situations occur across several CPUs,
  6184. + * but we might later generalize this if we find there are any actualy
  6185. + * systems with alternate SMI delivery or other hardware latencies.
  6186. + */
  6187. +static int kthread_fn(void *unused)
  6188. +{
  6189. + int ret;
  6190. + u64 interval;
  6191. +
  6192. + while (!kthread_should_stop()) {
  6193. +
  6194. + mutex_lock(&data.lock);
  6195. +
  6196. + local_irq_disable();
  6197. + ret = get_sample();
  6198. + local_irq_enable();
  6199. +
  6200. + if (ret > 0)
  6201. + wake_up(&data.wq); /* wake up reader(s) */
  6202. +
  6203. + interval = data.sample_window - data.sample_width;
  6204. + do_div(interval, USEC_PER_MSEC); /* modifies interval value */
  6205. +
  6206. + mutex_unlock(&data.lock);
  6207. +
  6208. + if (msleep_interruptible(interval))
  6209. + break;
  6210. + }
  6211. +
  6212. + return 0;
  6213. +}
  6214. +
  6215. +/**
  6216. + * start_kthread - Kick off the hardware latency sampling/detector kthread
  6217. + *
  6218. + * This starts a kernel thread that will sit and sample the CPU timestamp
  6219. + * counter (TSC or similar) and look for potential hardware latencies.
  6220. + */
  6221. +static int start_kthread(void)
  6222. +{
  6223. + kthread = kthread_run(kthread_fn, NULL,
  6224. + DRVNAME);
  6225. + if (IS_ERR(kthread)) {
  6226. + pr_err(BANNER "could not start sampling thread\n");
  6227. + enabled = 0;
  6228. + return -ENOMEM;
  6229. + }
  6230. +
  6231. + return 0;
  6232. +}
  6233. +
  6234. +/**
  6235. + * stop_kthread - Inform the hardware latency samping/detector kthread to stop
  6236. + *
  6237. + * This kicks the running hardware latency sampling/detector kernel thread and
  6238. + * tells it to stop sampling now. Use this on unload and at system shutdown.
  6239. + */
  6240. +static int stop_kthread(void)
  6241. +{
  6242. + int ret;
  6243. +
  6244. + ret = kthread_stop(kthread);
  6245. +
  6246. + return ret;
  6247. +}
  6248. +
  6249. +/**
  6250. + * __reset_stats - Reset statistics for the hardware latency detector
  6251. + *
  6252. + * We use data to store various statistics and global state. We call this
  6253. + * function in order to reset those when "enable" is toggled on or off, and
  6254. + * also at initialization. Should be called with data.lock held.
  6255. + */
  6256. +static void __reset_stats(void)
  6257. +{
  6258. + data.count = 0;
  6259. + data.max_sample = 0;
  6260. + ring_buffer_reset(ring_buffer); /* flush out old sample entries */
  6261. +}
  6262. +
  6263. +/**
  6264. + * init_stats - Setup global state statistics for the hardware latency detector
  6265. + *
  6266. + * We use data to store various statistics and global state. We also use
  6267. + * a global ring buffer (ring_buffer) to keep raw samples of detected hardware
  6268. + * induced system latencies. This function initializes these structures and
  6269. + * allocates the global ring buffer also.
  6270. + */
  6271. +static int init_stats(void)
  6272. +{
  6273. + int ret = -ENOMEM;
  6274. +
  6275. + mutex_init(&data.lock);
  6276. + init_waitqueue_head(&data.wq);
  6277. + atomic_set(&data.sample_open, 0);
  6278. +
  6279. + ring_buffer = ring_buffer_alloc(buf_size, BUF_FLAGS);
  6280. +
  6281. + if (WARN(!ring_buffer, KERN_ERR BANNER
  6282. + "failed to allocate ring buffer!\n"))
  6283. + goto out;
  6284. +
  6285. + __reset_stats();
  6286. + data.threshold = threshold ?: DEFAULT_LAT_THRESHOLD; /* threshold us */
  6287. + data.sample_window = DEFAULT_SAMPLE_WINDOW; /* window us */
  6288. + data.sample_width = DEFAULT_SAMPLE_WIDTH; /* width us */
  6289. +
  6290. + ret = 0;
  6291. +
  6292. +out:
  6293. + return ret;
  6294. +
  6295. +}
  6296. +
  6297. +/*
  6298. + * simple_data_read - Wrapper read function for global state debugfs entries
  6299. + * @filp: The active open file structure for the debugfs "file"
  6300. + * @ubuf: The userspace provided buffer to read value into
  6301. + * @cnt: The maximum number of bytes to read
  6302. + * @ppos: The current "file" position
  6303. + * @entry: The entry to read from
  6304. + *
  6305. + * This function provides a generic read implementation for the global state
  6306. + * "data" structure debugfs filesystem entries. It would be nice to use
  6307. + * simple_attr_read directly, but we need to make sure that the data.lock
  6308. + * is held during the actual read.
  6309. + */
  6310. +static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
  6311. + size_t cnt, loff_t *ppos, const u64 *entry)
  6312. +{
  6313. + char buf[U64STR_SIZE];
  6314. + u64 val = 0;
  6315. + int len = 0;
  6316. +
  6317. + memset(buf, 0, sizeof(buf));
  6318. +
  6319. + if (!entry)
  6320. + return -EFAULT;
  6321. +
  6322. + mutex_lock(&data.lock);
  6323. + val = *entry;
  6324. + mutex_unlock(&data.lock);
  6325. +
  6326. + len = snprintf(buf, sizeof(buf), "%llu\n", (unsigned long long)val);
  6327. +
  6328. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  6329. +
  6330. +}
  6331. +
  6332. +/*
  6333. + * simple_data_write - Wrapper write function for global state debugfs entries
  6334. + * @filp: The active open file structure for the debugfs "file"
  6335. + * @ubuf: The userspace provided buffer to write value from
  6336. + * @cnt: The maximum number of bytes to write
  6337. + * @ppos: The current "file" position
  6338. + * @entry: The entry to write to
  6339. + *
  6340. + * This function provides a generic write implementation for the global state
  6341. + * "data" structure debugfs filesystem entries. It would be nice to use
  6342. + * simple_attr_write directly, but we need to make sure that the data.lock
  6343. + * is held during the actual write.
  6344. + */
  6345. +static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
  6346. + size_t cnt, loff_t *ppos, u64 *entry)
  6347. +{
  6348. + char buf[U64STR_SIZE];
  6349. + int csize = min(cnt, sizeof(buf));
  6350. + u64 val = 0;
  6351. + int err = 0;
  6352. +
  6353. + memset(buf, '\0', sizeof(buf));
  6354. + if (copy_from_user(buf, ubuf, csize))
  6355. + return -EFAULT;
  6356. +
  6357. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  6358. + err = kstrtoull(buf, 10, &val);
  6359. + if (err)
  6360. + return -EINVAL;
  6361. +
  6362. + mutex_lock(&data.lock);
  6363. + *entry = val;
  6364. + mutex_unlock(&data.lock);
  6365. +
  6366. + return csize;
  6367. +}
  6368. +
  6369. +/**
  6370. + * debug_count_fopen - Open function for "count" debugfs entry
  6371. + * @inode: The in-kernel inode representation of the debugfs "file"
  6372. + * @filp: The active open file structure for the debugfs "file"
  6373. + *
  6374. + * This function provides an open implementation for the "count" debugfs
  6375. + * interface to the hardware latency detector.
  6376. + */
  6377. +static int debug_count_fopen(struct inode *inode, struct file *filp)
  6378. +{
  6379. + return 0;
  6380. +}
  6381. +
  6382. +/**
  6383. + * debug_count_fread - Read function for "count" debugfs entry
  6384. + * @filp: The active open file structure for the debugfs "file"
  6385. + * @ubuf: The userspace provided buffer to read value into
  6386. + * @cnt: The maximum number of bytes to read
  6387. + * @ppos: The current "file" position
  6388. + *
  6389. + * This function provides a read implementation for the "count" debugfs
  6390. + * interface to the hardware latency detector. Can be used to read the
  6391. + * number of latency readings exceeding the configured threshold since
  6392. + * the detector was last reset (e.g. by writing a zero into "count").
  6393. + */
  6394. +static ssize_t debug_count_fread(struct file *filp, char __user *ubuf,
  6395. + size_t cnt, loff_t *ppos)
  6396. +{
  6397. + return simple_data_read(filp, ubuf, cnt, ppos, &data.count);
  6398. +}
  6399. +
  6400. +/**
  6401. + * debug_count_fwrite - Write function for "count" debugfs entry
  6402. + * @filp: The active open file structure for the debugfs "file"
  6403. + * @ubuf: The user buffer that contains the value to write
  6404. + * @cnt: The maximum number of bytes to write to "file"
  6405. + * @ppos: The current position in the debugfs "file"
  6406. + *
  6407. + * This function provides a write implementation for the "count" debugfs
  6408. + * interface to the hardware latency detector. Can be used to write a
  6409. + * desired value, especially to zero the total count.
  6410. + */
  6411. +static ssize_t debug_count_fwrite(struct file *filp,
  6412. + const char __user *ubuf,
  6413. + size_t cnt,
  6414. + loff_t *ppos)
  6415. +{
  6416. + return simple_data_write(filp, ubuf, cnt, ppos, &data.count);
  6417. +}
  6418. +
  6419. +/**
  6420. + * debug_enable_fopen - Dummy open function for "enable" debugfs interface
  6421. + * @inode: The in-kernel inode representation of the debugfs "file"
  6422. + * @filp: The active open file structure for the debugfs "file"
  6423. + *
  6424. + * This function provides an open implementation for the "enable" debugfs
  6425. + * interface to the hardware latency detector.
  6426. + */
  6427. +static int debug_enable_fopen(struct inode *inode, struct file *filp)
  6428. +{
  6429. + return 0;
  6430. +}
  6431. +
  6432. +/**
  6433. + * debug_enable_fread - Read function for "enable" debugfs interface
  6434. + * @filp: The active open file structure for the debugfs "file"
  6435. + * @ubuf: The userspace provided buffer to read value into
  6436. + * @cnt: The maximum number of bytes to read
  6437. + * @ppos: The current "file" position
  6438. + *
  6439. + * This function provides a read implementation for the "enable" debugfs
  6440. + * interface to the hardware latency detector. Can be used to determine
  6441. + * whether the detector is currently enabled ("0\n" or "1\n" returned).
  6442. + */
  6443. +static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
  6444. + size_t cnt, loff_t *ppos)
  6445. +{
  6446. + char buf[4];
  6447. +
  6448. + if ((cnt < sizeof(buf)) || (*ppos))
  6449. + return 0;
  6450. +
  6451. + buf[0] = enabled ? '1' : '0';
  6452. + buf[1] = '\n';
  6453. + buf[2] = '\0';
  6454. + if (copy_to_user(ubuf, buf, strlen(buf)))
  6455. + return -EFAULT;
  6456. + return *ppos = strlen(buf);
  6457. +}
  6458. +
  6459. +/**
  6460. + * debug_enable_fwrite - Write function for "enable" debugfs interface
  6461. + * @filp: The active open file structure for the debugfs "file"
  6462. + * @ubuf: The user buffer that contains the value to write
  6463. + * @cnt: The maximum number of bytes to write to "file"
  6464. + * @ppos: The current position in the debugfs "file"
  6465. + *
  6466. + * This function provides a write implementation for the "enable" debugfs
  6467. + * interface to the hardware latency detector. Can be used to enable or
  6468. + * disable the detector, which will have the side-effect of possibly
  6469. + * also resetting the global stats and kicking off the measuring
  6470. + * kthread (on an enable) or the converse (upon a disable).
  6471. + */
  6472. +static ssize_t debug_enable_fwrite(struct file *filp,
  6473. + const char __user *ubuf,
  6474. + size_t cnt,
  6475. + loff_t *ppos)
  6476. +{
  6477. + char buf[4];
  6478. + int csize = min(cnt, sizeof(buf));
  6479. + long val = 0;
  6480. + int err = 0;
  6481. +
  6482. + memset(buf, '\0', sizeof(buf));
  6483. + if (copy_from_user(buf, ubuf, csize))
  6484. + return -EFAULT;
  6485. +
  6486. + buf[sizeof(buf)-1] = '\0'; /* just in case */
  6487. + err = kstrtoul(buf, 10, &val);
  6488. + if (0 != err)
  6489. + return -EINVAL;
  6490. +
  6491. + if (val) {
  6492. + if (enabled)
  6493. + goto unlock;
  6494. + enabled = 1;
  6495. + __reset_stats();
  6496. + if (start_kthread())
  6497. + return -EFAULT;
  6498. + } else {
  6499. + if (!enabled)
  6500. + goto unlock;
  6501. + enabled = 0;
  6502. + err = stop_kthread();
  6503. + if (err) {
  6504. + pr_err(BANNER "cannot stop kthread\n");
  6505. + return -EFAULT;
  6506. + }
  6507. + wake_up(&data.wq); /* reader(s) should return */
  6508. + }
  6509. +unlock:
  6510. + return csize;
  6511. +}
  6512. +
  6513. +/**
  6514. + * debug_max_fopen - Open function for "max" debugfs entry
  6515. + * @inode: The in-kernel inode representation of the debugfs "file"
  6516. + * @filp: The active open file structure for the debugfs "file"
  6517. + *
  6518. + * This function provides an open implementation for the "max" debugfs
  6519. + * interface to the hardware latency detector.
  6520. + */
  6521. +static int debug_max_fopen(struct inode *inode, struct file *filp)
  6522. +{
  6523. + return 0;
  6524. +}
  6525. +
  6526. +/**
  6527. + * debug_max_fread - Read function for "max" debugfs entry
  6528. + * @filp: The active open file structure for the debugfs "file"
  6529. + * @ubuf: The userspace provided buffer to read value into
  6530. + * @cnt: The maximum number of bytes to read
  6531. + * @ppos: The current "file" position
  6532. + *
  6533. + * This function provides a read implementation for the "max" debugfs
  6534. + * interface to the hardware latency detector. Can be used to determine
  6535. + * the maximum latency value observed since it was last reset.
  6536. + */
  6537. +static ssize_t debug_max_fread(struct file *filp, char __user *ubuf,
  6538. + size_t cnt, loff_t *ppos)
  6539. +{
  6540. + return simple_data_read(filp, ubuf, cnt, ppos, &data.max_sample);
  6541. +}
  6542. +
  6543. +/**
  6544. + * debug_max_fwrite - Write function for "max" debugfs entry
  6545. + * @filp: The active open file structure for the debugfs "file"
  6546. + * @ubuf: The user buffer that contains the value to write
  6547. + * @cnt: The maximum number of bytes to write to "file"
  6548. + * @ppos: The current position in the debugfs "file"
  6549. + *
  6550. + * This function provides a write implementation for the "max" debugfs
  6551. + * interface to the hardware latency detector. Can be used to reset the
  6552. + * maximum or set it to some other desired value - if, then, subsequent
  6553. + * measurements exceed this value, the maximum will be updated.
  6554. + */
  6555. +static ssize_t debug_max_fwrite(struct file *filp,
  6556. + const char __user *ubuf,
  6557. + size_t cnt,
  6558. + loff_t *ppos)
  6559. +{
  6560. + return simple_data_write(filp, ubuf, cnt, ppos, &data.max_sample);
  6561. +}
  6562. +
  6563. +
  6564. +/**
  6565. + * debug_sample_fopen - An open function for "sample" debugfs interface
  6566. + * @inode: The in-kernel inode representation of this debugfs "file"
  6567. + * @filp: The active open file structure for the debugfs "file"
  6568. + *
  6569. + * This function handles opening the "sample" file within the hardware
  6570. + * latency detector debugfs directory interface. This file is used to read
  6571. + * raw samples from the global ring_buffer and allows the user to see a
  6572. + * running latency history. Can be opened blocking or non-blocking,
  6573. + * affecting whether it behaves as a buffer read pipe, or does not.
  6574. + * Implements simple locking to prevent multiple simultaneous use.
  6575. + */
  6576. +static int debug_sample_fopen(struct inode *inode, struct file *filp)
  6577. +{
  6578. + if (!atomic_add_unless(&data.sample_open, 1, 1))
  6579. + return -EBUSY;
  6580. + else
  6581. + return 0;
  6582. +}
  6583. +
  6584. +/**
  6585. + * debug_sample_fread - A read function for "sample" debugfs interface
  6586. + * @filp: The active open file structure for the debugfs "file"
  6587. + * @ubuf: The user buffer that will contain the samples read
  6588. + * @cnt: The maximum bytes to read from the debugfs "file"
  6589. + * @ppos: The current position in the debugfs "file"
  6590. + *
  6591. + * This function handles reading from the "sample" file within the hardware
  6592. + * latency detector debugfs directory interface. This file is used to read
  6593. + * raw samples from the global ring_buffer and allows the user to see a
  6594. + * running latency history. By default this will block pending a new
  6595. + * value written into the sample buffer, unless there are already a
  6596. + * number of value(s) waiting in the buffer, or the sample file was
  6597. + * previously opened in a non-blocking mode of operation.
  6598. + */
  6599. +static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
  6600. + size_t cnt, loff_t *ppos)
  6601. +{
  6602. + int len = 0;
  6603. + char buf[64];
  6604. + struct sample *sample = NULL;
  6605. +
  6606. + if (!enabled)
  6607. + return 0;
  6608. +
  6609. + sample = kzalloc(sizeof(struct sample), GFP_KERNEL);
  6610. + if (!sample)
  6611. + return -ENOMEM;
  6612. +
  6613. + while (!buffer_get_sample(sample)) {
  6614. +
  6615. + DEFINE_WAIT(wait);
  6616. +
  6617. + if (filp->f_flags & O_NONBLOCK) {
  6618. + len = -EAGAIN;
  6619. + goto out;
  6620. + }
  6621. +
  6622. + prepare_to_wait(&data.wq, &wait, TASK_INTERRUPTIBLE);
  6623. + schedule();
  6624. + finish_wait(&data.wq, &wait);
  6625. +
  6626. + if (signal_pending(current)) {
  6627. + len = -EINTR;
  6628. + goto out;
  6629. + }
  6630. +
  6631. + if (!enabled) { /* enable was toggled */
  6632. + len = 0;
  6633. + goto out;
  6634. + }
  6635. + }
  6636. +
  6637. + len = snprintf(buf, sizeof(buf), "%010lu.%010lu\t%llu\t%llu\n",
  6638. + sample->timestamp.tv_sec,
  6639. + sample->timestamp.tv_nsec,
  6640. + sample->duration,
  6641. + sample->outer_duration);
  6642. +
  6643. +
  6644. + /* handling partial reads is more trouble than it's worth */
  6645. + if (len > cnt)
  6646. + goto out;
  6647. +
  6648. + if (copy_to_user(ubuf, buf, len))
  6649. + len = -EFAULT;
  6650. +
  6651. +out:
  6652. + kfree(sample);
  6653. + return len;
  6654. +}
  6655. +
  6656. +/**
  6657. + * debug_sample_release - Release function for "sample" debugfs interface
  6658. + * @inode: The in-kernel inode represenation of the debugfs "file"
  6659. + * @filp: The active open file structure for the debugfs "file"
  6660. + *
  6661. + * This function completes the close of the debugfs interface "sample" file.
  6662. + * Frees the sample_open "lock" so that other users may open the interface.
  6663. + */
  6664. +static int debug_sample_release(struct inode *inode, struct file *filp)
  6665. +{
  6666. + atomic_dec(&data.sample_open);
  6667. +
  6668. + return 0;
  6669. +}
  6670. +
  6671. +/**
  6672. + * debug_threshold_fopen - Open function for "threshold" debugfs entry
  6673. + * @inode: The in-kernel inode representation of the debugfs "file"
  6674. + * @filp: The active open file structure for the debugfs "file"
  6675. + *
  6676. + * This function provides an open implementation for the "threshold" debugfs
  6677. + * interface to the hardware latency detector.
  6678. + */
  6679. +static int debug_threshold_fopen(struct inode *inode, struct file *filp)
  6680. +{
  6681. + return 0;
  6682. +}
  6683. +
  6684. +/**
  6685. + * debug_threshold_fread - Read function for "threshold" debugfs entry
  6686. + * @filp: The active open file structure for the debugfs "file"
  6687. + * @ubuf: The userspace provided buffer to read value into
  6688. + * @cnt: The maximum number of bytes to read
  6689. + * @ppos: The current "file" position
  6690. + *
  6691. + * This function provides a read implementation for the "threshold" debugfs
  6692. + * interface to the hardware latency detector. It can be used to determine
  6693. + * the current threshold level at which a latency will be recorded in the
  6694. + * global ring buffer, typically on the order of 10us.
  6695. + */
  6696. +static ssize_t debug_threshold_fread(struct file *filp, char __user *ubuf,
  6697. + size_t cnt, loff_t *ppos)
  6698. +{
  6699. + return simple_data_read(filp, ubuf, cnt, ppos, &data.threshold);
  6700. +}
  6701. +
  6702. +/**
  6703. + * debug_threshold_fwrite - Write function for "threshold" debugfs entry
  6704. + * @filp: The active open file structure for the debugfs "file"
  6705. + * @ubuf: The user buffer that contains the value to write
  6706. + * @cnt: The maximum number of bytes to write to "file"
  6707. + * @ppos: The current position in the debugfs "file"
  6708. + *
  6709. + * This function provides a write implementation for the "threshold" debugfs
  6710. + * interface to the hardware latency detector. It can be used to configure
  6711. + * the threshold level at which any subsequently detected latencies will
  6712. + * be recorded into the global ring buffer.
  6713. + */
  6714. +static ssize_t debug_threshold_fwrite(struct file *filp,
  6715. + const char __user *ubuf,
  6716. + size_t cnt,
  6717. + loff_t *ppos)
  6718. +{
  6719. + int ret;
  6720. +
  6721. + ret = simple_data_write(filp, ubuf, cnt, ppos, &data.threshold);
  6722. +
  6723. + if (enabled)
  6724. + wake_up_process(kthread);
  6725. +
  6726. + return ret;
  6727. +}
  6728. +
  6729. +/**
  6730. + * debug_width_fopen - Open function for "width" debugfs entry
  6731. + * @inode: The in-kernel inode representation of the debugfs "file"
  6732. + * @filp: The active open file structure for the debugfs "file"
  6733. + *
  6734. + * This function provides an open implementation for the "width" debugfs
  6735. + * interface to the hardware latency detector.
  6736. + */
  6737. +static int debug_width_fopen(struct inode *inode, struct file *filp)
  6738. +{
  6739. + return 0;
  6740. +}
  6741. +
  6742. +/**
  6743. + * debug_width_fread - Read function for "width" debugfs entry
  6744. + * @filp: The active open file structure for the debugfs "file"
  6745. + * @ubuf: The userspace provided buffer to read value into
  6746. + * @cnt: The maximum number of bytes to read
  6747. + * @ppos: The current "file" position
  6748. + *
  6749. + * This function provides a read implementation for the "width" debugfs
  6750. + * interface to the hardware latency detector. It can be used to determine
  6751. + * for how many us of the total window us we will actively sample for any
  6752. + * hardware-induced latecy periods. Obviously, it is not possible to
  6753. + * sample constantly and have the system respond to a sample reader, or,
  6754. + * worse, without having the system appear to have gone out to lunch.
  6755. + */
  6756. +static ssize_t debug_width_fread(struct file *filp, char __user *ubuf,
  6757. + size_t cnt, loff_t *ppos)
  6758. +{
  6759. + return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_width);
  6760. +}
  6761. +
  6762. +/**
  6763. + * debug_width_fwrite - Write function for "width" debugfs entry
  6764. + * @filp: The active open file structure for the debugfs "file"
  6765. + * @ubuf: The user buffer that contains the value to write
  6766. + * @cnt: The maximum number of bytes to write to "file"
  6767. + * @ppos: The current position in the debugfs "file"
  6768. + *
  6769. + * This function provides a write implementation for the "width" debugfs
  6770. + * interface to the hardware latency detector. It can be used to configure
  6771. + * for how many us of the total window us we will actively sample for any
  6772. + * hardware-induced latency periods. Obviously, it is not possible to
  6773. + * sample constantly and have the system respond to a sample reader, or,
  6774. + * worse, without having the system appear to have gone out to lunch. It
  6775. + * is enforced that width is less that the total window size.
  6776. + */
  6777. +static ssize_t debug_width_fwrite(struct file *filp,
  6778. + const char __user *ubuf,
  6779. + size_t cnt,
  6780. + loff_t *ppos)
  6781. +{
  6782. + char buf[U64STR_SIZE];
  6783. + int csize = min(cnt, sizeof(buf));
  6784. + u64 val = 0;
  6785. + int err = 0;
  6786. +
  6787. + memset(buf, '\0', sizeof(buf));
  6788. + if (copy_from_user(buf, ubuf, csize))
  6789. + return -EFAULT;
  6790. +
  6791. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  6792. + err = kstrtoull(buf, 10, &val);
  6793. + if (0 != err)
  6794. + return -EINVAL;
  6795. +
  6796. + mutex_lock(&data.lock);
  6797. + if (val < data.sample_window)
  6798. + data.sample_width = val;
  6799. + else {
  6800. + mutex_unlock(&data.lock);
  6801. + return -EINVAL;
  6802. + }
  6803. + mutex_unlock(&data.lock);
  6804. +
  6805. + if (enabled)
  6806. + wake_up_process(kthread);
  6807. +
  6808. + return csize;
  6809. +}
  6810. +
  6811. +/**
  6812. + * debug_window_fopen - Open function for "window" debugfs entry
  6813. + * @inode: The in-kernel inode representation of the debugfs "file"
  6814. + * @filp: The active open file structure for the debugfs "file"
  6815. + *
  6816. + * This function provides an open implementation for the "window" debugfs
  6817. + * interface to the hardware latency detector. The window is the total time
  6818. + * in us that will be considered one sample period. Conceptually, windows
  6819. + * occur back-to-back and contain a sample width period during which
  6820. + * actual sampling occurs.
  6821. + */
  6822. +static int debug_window_fopen(struct inode *inode, struct file *filp)
  6823. +{
  6824. + return 0;
  6825. +}
  6826. +
  6827. +/**
  6828. + * debug_window_fread - Read function for "window" debugfs entry
  6829. + * @filp: The active open file structure for the debugfs "file"
  6830. + * @ubuf: The userspace provided buffer to read value into
  6831. + * @cnt: The maximum number of bytes to read
  6832. + * @ppos: The current "file" position
  6833. + *
  6834. + * This function provides a read implementation for the "window" debugfs
  6835. + * interface to the hardware latency detector. The window is the total time
  6836. + * in us that will be considered one sample period. Conceptually, windows
  6837. + * occur back-to-back and contain a sample width period during which
  6838. + * actual sampling occurs. Can be used to read the total window size.
  6839. + */
  6840. +static ssize_t debug_window_fread(struct file *filp, char __user *ubuf,
  6841. + size_t cnt, loff_t *ppos)
  6842. +{
  6843. + return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_window);
  6844. +}
  6845. +
  6846. +/**
  6847. + * debug_window_fwrite - Write function for "window" debugfs entry
  6848. + * @filp: The active open file structure for the debugfs "file"
  6849. + * @ubuf: The user buffer that contains the value to write
  6850. + * @cnt: The maximum number of bytes to write to "file"
  6851. + * @ppos: The current position in the debugfs "file"
  6852. + *
  6853. + * This function provides a write implementation for the "window" debufds
  6854. + * interface to the hardware latency detetector. The window is the total time
  6855. + * in us that will be considered one sample period. Conceptually, windows
  6856. + * occur back-to-back and contain a sample width period during which
  6857. + * actual sampling occurs. Can be used to write a new total window size. It
  6858. + * is enfoced that any value written must be greater than the sample width
  6859. + * size, or an error results.
  6860. + */
  6861. +static ssize_t debug_window_fwrite(struct file *filp,
  6862. + const char __user *ubuf,
  6863. + size_t cnt,
  6864. + loff_t *ppos)
  6865. +{
  6866. + char buf[U64STR_SIZE];
  6867. + int csize = min(cnt, sizeof(buf));
  6868. + u64 val = 0;
  6869. + int err = 0;
  6870. +
  6871. + memset(buf, '\0', sizeof(buf));
  6872. + if (copy_from_user(buf, ubuf, csize))
  6873. + return -EFAULT;
  6874. +
  6875. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  6876. + err = kstrtoull(buf, 10, &val);
  6877. + if (0 != err)
  6878. + return -EINVAL;
  6879. +
  6880. + mutex_lock(&data.lock);
  6881. + if (data.sample_width < val)
  6882. + data.sample_window = val;
  6883. + else {
  6884. + mutex_unlock(&data.lock);
  6885. + return -EINVAL;
  6886. + }
  6887. + mutex_unlock(&data.lock);
  6888. +
  6889. + return csize;
  6890. +}
  6891. +
  6892. +/*
  6893. + * Function pointers for the "count" debugfs file operations
  6894. + */
  6895. +static const struct file_operations count_fops = {
  6896. + .open = debug_count_fopen,
  6897. + .read = debug_count_fread,
  6898. + .write = debug_count_fwrite,
  6899. + .owner = THIS_MODULE,
  6900. +};
  6901. +
  6902. +/*
  6903. + * Function pointers for the "enable" debugfs file operations
  6904. + */
  6905. +static const struct file_operations enable_fops = {
  6906. + .open = debug_enable_fopen,
  6907. + .read = debug_enable_fread,
  6908. + .write = debug_enable_fwrite,
  6909. + .owner = THIS_MODULE,
  6910. +};
  6911. +
  6912. +/*
  6913. + * Function pointers for the "max" debugfs file operations
  6914. + */
  6915. +static const struct file_operations max_fops = {
  6916. + .open = debug_max_fopen,
  6917. + .read = debug_max_fread,
  6918. + .write = debug_max_fwrite,
  6919. + .owner = THIS_MODULE,
  6920. +};
  6921. +
  6922. +/*
  6923. + * Function pointers for the "sample" debugfs file operations
  6924. + */
  6925. +static const struct file_operations sample_fops = {
  6926. + .open = debug_sample_fopen,
  6927. + .read = debug_sample_fread,
  6928. + .release = debug_sample_release,
  6929. + .owner = THIS_MODULE,
  6930. +};
  6931. +
  6932. +/*
  6933. + * Function pointers for the "threshold" debugfs file operations
  6934. + */
  6935. +static const struct file_operations threshold_fops = {
  6936. + .open = debug_threshold_fopen,
  6937. + .read = debug_threshold_fread,
  6938. + .write = debug_threshold_fwrite,
  6939. + .owner = THIS_MODULE,
  6940. +};
  6941. +
  6942. +/*
  6943. + * Function pointers for the "width" debugfs file operations
  6944. + */
  6945. +static const struct file_operations width_fops = {
  6946. + .open = debug_width_fopen,
  6947. + .read = debug_width_fread,
  6948. + .write = debug_width_fwrite,
  6949. + .owner = THIS_MODULE,
  6950. +};
  6951. +
  6952. +/*
  6953. + * Function pointers for the "window" debugfs file operations
  6954. + */
  6955. +static const struct file_operations window_fops = {
  6956. + .open = debug_window_fopen,
  6957. + .read = debug_window_fread,
  6958. + .write = debug_window_fwrite,
  6959. + .owner = THIS_MODULE,
  6960. +};
  6961. +
  6962. +/**
  6963. + * init_debugfs - A function to initialize the debugfs interface files
  6964. + *
  6965. + * This function creates entries in debugfs for "hwlat_detector", including
  6966. + * files to read values from the detector, current samples, and the
  6967. + * maximum sample that has been captured since the hardware latency
  6968. + * dectector was started.
  6969. + */
  6970. +static int init_debugfs(void)
  6971. +{
  6972. + int ret = -ENOMEM;
  6973. +
  6974. + debug_dir = debugfs_create_dir(DRVNAME, NULL);
  6975. + if (!debug_dir)
  6976. + goto err_debug_dir;
  6977. +
  6978. + debug_sample = debugfs_create_file("sample", 0444,
  6979. + debug_dir, NULL,
  6980. + &sample_fops);
  6981. + if (!debug_sample)
  6982. + goto err_sample;
  6983. +
  6984. + debug_count = debugfs_create_file("count", 0444,
  6985. + debug_dir, NULL,
  6986. + &count_fops);
  6987. + if (!debug_count)
  6988. + goto err_count;
  6989. +
  6990. + debug_max = debugfs_create_file("max", 0444,
  6991. + debug_dir, NULL,
  6992. + &max_fops);
  6993. + if (!debug_max)
  6994. + goto err_max;
  6995. +
  6996. + debug_sample_window = debugfs_create_file("window", 0644,
  6997. + debug_dir, NULL,
  6998. + &window_fops);
  6999. + if (!debug_sample_window)
  7000. + goto err_window;
  7001. +
  7002. + debug_sample_width = debugfs_create_file("width", 0644,
  7003. + debug_dir, NULL,
  7004. + &width_fops);
  7005. + if (!debug_sample_width)
  7006. + goto err_width;
  7007. +
  7008. + debug_threshold = debugfs_create_file("threshold", 0644,
  7009. + debug_dir, NULL,
  7010. + &threshold_fops);
  7011. + if (!debug_threshold)
  7012. + goto err_threshold;
  7013. +
  7014. + debug_enable = debugfs_create_file("enable", 0644,
  7015. + debug_dir, &enabled,
  7016. + &enable_fops);
  7017. + if (!debug_enable)
  7018. + goto err_enable;
  7019. +
  7020. + else {
  7021. + ret = 0;
  7022. + goto out;
  7023. + }
  7024. +
  7025. +err_enable:
  7026. + debugfs_remove(debug_threshold);
  7027. +err_threshold:
  7028. + debugfs_remove(debug_sample_width);
  7029. +err_width:
  7030. + debugfs_remove(debug_sample_window);
  7031. +err_window:
  7032. + debugfs_remove(debug_max);
  7033. +err_max:
  7034. + debugfs_remove(debug_count);
  7035. +err_count:
  7036. + debugfs_remove(debug_sample);
  7037. +err_sample:
  7038. + debugfs_remove(debug_dir);
  7039. +err_debug_dir:
  7040. +out:
  7041. + return ret;
  7042. +}
  7043. +
  7044. +/**
  7045. + * free_debugfs - A function to cleanup the debugfs file interface
  7046. + */
  7047. +static void free_debugfs(void)
  7048. +{
  7049. + /* could also use a debugfs_remove_recursive */
  7050. + debugfs_remove(debug_enable);
  7051. + debugfs_remove(debug_threshold);
  7052. + debugfs_remove(debug_sample_width);
  7053. + debugfs_remove(debug_sample_window);
  7054. + debugfs_remove(debug_max);
  7055. + debugfs_remove(debug_count);
  7056. + debugfs_remove(debug_sample);
  7057. + debugfs_remove(debug_dir);
  7058. +}
  7059. +
  7060. +/**
  7061. + * detector_init - Standard module initialization code
  7062. + */
  7063. +static int detector_init(void)
  7064. +{
  7065. + int ret = -ENOMEM;
  7066. +
  7067. + pr_info(BANNER "version %s\n", VERSION);
  7068. +
  7069. + ret = init_stats();
  7070. + if (0 != ret)
  7071. + goto out;
  7072. +
  7073. + ret = init_debugfs();
  7074. + if (0 != ret)
  7075. + goto err_stats;
  7076. +
  7077. + if (enabled)
  7078. + ret = start_kthread();
  7079. +
  7080. + goto out;
  7081. +
  7082. +err_stats:
  7083. + ring_buffer_free(ring_buffer);
  7084. +out:
  7085. + return ret;
  7086. +
  7087. +}
  7088. +
  7089. +/**
  7090. + * detector_exit - Standard module cleanup code
  7091. + */
  7092. +static void detector_exit(void)
  7093. +{
  7094. + int err;
  7095. +
  7096. + if (enabled) {
  7097. + enabled = 0;
  7098. + err = stop_kthread();
  7099. + if (err)
  7100. + pr_err(BANNER "cannot stop kthread\n");
  7101. + }
  7102. +
  7103. + free_debugfs();
  7104. + ring_buffer_free(ring_buffer); /* free up the ring buffer */
  7105. +
  7106. +}
  7107. +
  7108. +module_init(detector_init);
  7109. +module_exit(detector_exit);
  7110. diff -Nur linux-4.1.13.orig/drivers/misc/Kconfig linux-4.1.13/drivers/misc/Kconfig
  7111. --- linux-4.1.13.orig/drivers/misc/Kconfig 2015-11-09 23:34:10.000000000 +0100
  7112. +++ linux-4.1.13/drivers/misc/Kconfig 2015-11-29 09:23:09.561617368 +0100
  7113. @@ -54,6 +54,7 @@
  7114. config ATMEL_TCLIB
  7115. bool "Atmel AT32/AT91 Timer/Counter Library"
  7116. depends on (AVR32 || ARCH_AT91)
  7117. + default y if PREEMPT_RT_FULL
  7118. help
  7119. Select this if you want a library to allocate the Timer/Counter
  7120. blocks found on many Atmel processors. This facilitates using
  7121. @@ -69,8 +70,7 @@
  7122. are combined to make a single 32-bit timer.
  7123. When GENERIC_CLOCKEVENTS is defined, the third timer channel
  7124. - may be used as a clock event device supporting oneshot mode
  7125. - (delays of up to two seconds) based on the 32 KiHz clock.
  7126. + may be used as a clock event device supporting oneshot mode.
  7127. config ATMEL_TCB_CLKSRC_BLOCK
  7128. int
  7129. @@ -84,6 +84,15 @@
  7130. TC can be used for other purposes, such as PWM generation and
  7131. interval timing.
  7132. +config ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  7133. + bool "TC Block use 32 KiHz clock"
  7134. + depends on ATMEL_TCB_CLKSRC
  7135. + default y if !PREEMPT_RT_FULL
  7136. + help
  7137. + Select this to use 32 KiHz base clock rate as TC block clock
  7138. + source for clock events.
  7139. +
  7140. +
  7141. config DUMMY_IRQ
  7142. tristate "Dummy IRQ handler"
  7143. default n
  7144. @@ -113,6 +122,35 @@
  7145. for information on the specific driver level and support statement
  7146. for your IBM server.
  7147. +config HWLAT_DETECTOR
  7148. + tristate "Testing module to detect hardware-induced latencies"
  7149. + depends on DEBUG_FS
  7150. + depends on RING_BUFFER
  7151. + default m
  7152. + ---help---
  7153. + A simple hardware latency detector. Use this module to detect
  7154. + large latencies introduced by the behavior of the underlying
  7155. + system firmware external to Linux. We do this using periodic
  7156. + use of stop_machine to grab all available CPUs and measure
  7157. + for unexplainable gaps in the CPU timestamp counter(s). By
  7158. + default, the module is not enabled until the "enable" file
  7159. + within the "hwlat_detector" debugfs directory is toggled.
  7160. +
  7161. + This module is often used to detect SMI (System Management
  7162. + Interrupts) on x86 systems, though is not x86 specific. To
  7163. + this end, we default to using a sample window of 1 second,
  7164. + during which we will sample for 0.5 seconds. If an SMI or
  7165. + similar event occurs during that time, it is recorded
  7166. + into an 8K samples global ring buffer until retreived.
  7167. +
  7168. + WARNING: This software should never be enabled (it can be built
  7169. + but should not be turned on after it is loaded) in a production
  7170. + environment where high latencies are a concern since the
  7171. + sampling mechanism actually introduces latencies for
  7172. + regular tasks while the CPU(s) are being held.
  7173. +
  7174. + If unsure, say N
  7175. +
  7176. config PHANTOM
  7177. tristate "Sensable PHANToM (PCI)"
  7178. depends on PCI
  7179. diff -Nur linux-4.1.13.orig/drivers/misc/Makefile linux-4.1.13/drivers/misc/Makefile
  7180. --- linux-4.1.13.orig/drivers/misc/Makefile 2015-11-09 23:34:10.000000000 +0100
  7181. +++ linux-4.1.13/drivers/misc/Makefile 2015-11-29 09:23:09.561617368 +0100
  7182. @@ -38,6 +38,7 @@
  7183. obj-$(CONFIG_HMC6352) += hmc6352.o
  7184. obj-y += eeprom/
  7185. obj-y += cb710/
  7186. +obj-$(CONFIG_HWLAT_DETECTOR) += hwlat_detector.o
  7187. obj-$(CONFIG_SPEAR13XX_PCIE_GADGET) += spear13xx_pcie_gadget.o
  7188. obj-$(CONFIG_VMWARE_BALLOON) += vmw_balloon.o
  7189. obj-$(CONFIG_ARM_CHARLCD) += arm-charlcd.o
  7190. diff -Nur linux-4.1.13.orig/drivers/mmc/host/mmci.c linux-4.1.13/drivers/mmc/host/mmci.c
  7191. --- linux-4.1.13.orig/drivers/mmc/host/mmci.c 2015-11-09 23:34:10.000000000 +0100
  7192. +++ linux-4.1.13/drivers/mmc/host/mmci.c 2015-11-29 09:23:09.561617368 +0100
  7193. @@ -1155,15 +1155,12 @@
  7194. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  7195. struct variant_data *variant = host->variant;
  7196. void __iomem *base = host->base;
  7197. - unsigned long flags;
  7198. u32 status;
  7199. status = readl(base + MMCISTATUS);
  7200. dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
  7201. - local_irq_save(flags);
  7202. -
  7203. do {
  7204. unsigned int remain, len;
  7205. char *buffer;
  7206. @@ -1203,8 +1200,6 @@
  7207. sg_miter_stop(sg_miter);
  7208. - local_irq_restore(flags);
  7209. -
  7210. /*
  7211. * If we have less than the fifo 'half-full' threshold to transfer,
  7212. * trigger a PIO interrupt as soon as any data is available.
  7213. diff -Nur linux-4.1.13.orig/drivers/net/ethernet/3com/3c59x.c linux-4.1.13/drivers/net/ethernet/3com/3c59x.c
  7214. --- linux-4.1.13.orig/drivers/net/ethernet/3com/3c59x.c 2015-11-09 23:34:10.000000000 +0100
  7215. +++ linux-4.1.13/drivers/net/ethernet/3com/3c59x.c 2015-11-29 09:23:09.561617368 +0100
  7216. @@ -842,9 +842,9 @@
  7217. {
  7218. struct vortex_private *vp = netdev_priv(dev);
  7219. unsigned long flags;
  7220. - local_irq_save(flags);
  7221. + local_irq_save_nort(flags);
  7222. (vp->full_bus_master_rx ? boomerang_interrupt:vortex_interrupt)(dev->irq,dev);
  7223. - local_irq_restore(flags);
  7224. + local_irq_restore_nort(flags);
  7225. }
  7226. #endif
  7227. @@ -1916,12 +1916,12 @@
  7228. * Block interrupts because vortex_interrupt does a bare spin_lock()
  7229. */
  7230. unsigned long flags;
  7231. - local_irq_save(flags);
  7232. + local_irq_save_nort(flags);
  7233. if (vp->full_bus_master_tx)
  7234. boomerang_interrupt(dev->irq, dev);
  7235. else
  7236. vortex_interrupt(dev->irq, dev);
  7237. - local_irq_restore(flags);
  7238. + local_irq_restore_nort(flags);
  7239. }
  7240. }
  7241. diff -Nur linux-4.1.13.orig/drivers/net/ethernet/atheros/atl1c/atl1c_main.c linux-4.1.13/drivers/net/ethernet/atheros/atl1c/atl1c_main.c
  7242. --- linux-4.1.13.orig/drivers/net/ethernet/atheros/atl1c/atl1c_main.c 2015-11-09 23:34:10.000000000 +0100
  7243. +++ linux-4.1.13/drivers/net/ethernet/atheros/atl1c/atl1c_main.c 2015-11-29 09:23:09.561617368 +0100
  7244. @@ -2213,11 +2213,7 @@
  7245. }
  7246. tpd_req = atl1c_cal_tpd_req(skb);
  7247. - if (!spin_trylock_irqsave(&adapter->tx_lock, flags)) {
  7248. - if (netif_msg_pktdata(adapter))
  7249. - dev_info(&adapter->pdev->dev, "tx locked\n");
  7250. - return NETDEV_TX_LOCKED;
  7251. - }
  7252. + spin_lock_irqsave(&adapter->tx_lock, flags);
  7253. if (atl1c_tpd_avail(adapter, type) < tpd_req) {
  7254. /* no enough descriptor, just stop queue */
  7255. diff -Nur linux-4.1.13.orig/drivers/net/ethernet/atheros/atl1e/atl1e_main.c linux-4.1.13/drivers/net/ethernet/atheros/atl1e/atl1e_main.c
  7256. --- linux-4.1.13.orig/drivers/net/ethernet/atheros/atl1e/atl1e_main.c 2015-11-09 23:34:10.000000000 +0100
  7257. +++ linux-4.1.13/drivers/net/ethernet/atheros/atl1e/atl1e_main.c 2015-11-29 09:23:09.561617368 +0100
  7258. @@ -1880,8 +1880,7 @@
  7259. return NETDEV_TX_OK;
  7260. }
  7261. tpd_req = atl1e_cal_tdp_req(skb);
  7262. - if (!spin_trylock_irqsave(&adapter->tx_lock, flags))
  7263. - return NETDEV_TX_LOCKED;
  7264. + spin_lock_irqsave(&adapter->tx_lock, flags);
  7265. if (atl1e_tpd_avail(adapter) < tpd_req) {
  7266. /* no enough descriptor, just stop queue */
  7267. diff -Nur linux-4.1.13.orig/drivers/net/ethernet/chelsio/cxgb/sge.c linux-4.1.13/drivers/net/ethernet/chelsio/cxgb/sge.c
  7268. --- linux-4.1.13.orig/drivers/net/ethernet/chelsio/cxgb/sge.c 2015-11-09 23:34:10.000000000 +0100
  7269. +++ linux-4.1.13/drivers/net/ethernet/chelsio/cxgb/sge.c 2015-11-29 09:23:09.569616840 +0100
  7270. @@ -1664,8 +1664,7 @@
  7271. struct cmdQ *q = &sge->cmdQ[qid];
  7272. unsigned int credits, pidx, genbit, count, use_sched_skb = 0;
  7273. - if (!spin_trylock(&q->lock))
  7274. - return NETDEV_TX_LOCKED;
  7275. + spin_lock(&q->lock);
  7276. reclaim_completed_tx(sge, q);
  7277. diff -Nur linux-4.1.13.orig/drivers/net/ethernet/freescale/gianfar.c linux-4.1.13/drivers/net/ethernet/freescale/gianfar.c
  7278. --- linux-4.1.13.orig/drivers/net/ethernet/freescale/gianfar.c 2015-11-09 23:34:10.000000000 +0100
  7279. +++ linux-4.1.13/drivers/net/ethernet/freescale/gianfar.c 2015-11-29 09:23:09.569616840 +0100
  7280. @@ -1540,7 +1540,7 @@
  7281. if (netif_running(ndev)) {
  7282. - local_irq_save(flags);
  7283. + local_irq_save_nort(flags);
  7284. lock_tx_qs(priv);
  7285. gfar_halt_nodisable(priv);
  7286. @@ -1556,7 +1556,7 @@
  7287. gfar_write(&regs->maccfg1, tempval);
  7288. unlock_tx_qs(priv);
  7289. - local_irq_restore(flags);
  7290. + local_irq_restore_nort(flags);
  7291. disable_napi(priv);
  7292. @@ -1598,7 +1598,7 @@
  7293. /* Disable Magic Packet mode, in case something
  7294. * else woke us up.
  7295. */
  7296. - local_irq_save(flags);
  7297. + local_irq_save_nort(flags);
  7298. lock_tx_qs(priv);
  7299. tempval = gfar_read(&regs->maccfg2);
  7300. @@ -1608,7 +1608,7 @@
  7301. gfar_start(priv);
  7302. unlock_tx_qs(priv);
  7303. - local_irq_restore(flags);
  7304. + local_irq_restore_nort(flags);
  7305. netif_device_attach(ndev);
  7306. @@ -3418,14 +3418,14 @@
  7307. dev->stats.tx_dropped++;
  7308. atomic64_inc(&priv->extra_stats.tx_underrun);
  7309. - local_irq_save(flags);
  7310. + local_irq_save_nort(flags);
  7311. lock_tx_qs(priv);
  7312. /* Reactivate the Tx Queues */
  7313. gfar_write(&regs->tstat, gfargrp->tstat);
  7314. unlock_tx_qs(priv);
  7315. - local_irq_restore(flags);
  7316. + local_irq_restore_nort(flags);
  7317. }
  7318. netif_dbg(priv, tx_err, dev, "Transmit Error\n");
  7319. }
  7320. diff -Nur linux-4.1.13.orig/drivers/net/ethernet/neterion/s2io.c linux-4.1.13/drivers/net/ethernet/neterion/s2io.c
  7321. --- linux-4.1.13.orig/drivers/net/ethernet/neterion/s2io.c 2015-11-09 23:34:10.000000000 +0100
  7322. +++ linux-4.1.13/drivers/net/ethernet/neterion/s2io.c 2015-11-29 09:23:09.569616840 +0100
  7323. @@ -4084,12 +4084,7 @@
  7324. [skb->priority & (MAX_TX_FIFOS - 1)];
  7325. fifo = &mac_control->fifos[queue];
  7326. - if (do_spin_lock)
  7327. - spin_lock_irqsave(&fifo->tx_lock, flags);
  7328. - else {
  7329. - if (unlikely(!spin_trylock_irqsave(&fifo->tx_lock, flags)))
  7330. - return NETDEV_TX_LOCKED;
  7331. - }
  7332. + spin_lock_irqsave(&fifo->tx_lock, flags);
  7333. if (sp->config.multiq) {
  7334. if (__netif_subqueue_stopped(dev, fifo->fifo_no)) {
  7335. diff -Nur linux-4.1.13.orig/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c linux-4.1.13/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c
  7336. --- linux-4.1.13.orig/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c 2015-11-09 23:34:10.000000000 +0100
  7337. +++ linux-4.1.13/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c 2015-11-29 09:23:09.569616840 +0100
  7338. @@ -2137,10 +2137,8 @@
  7339. struct pch_gbe_tx_ring *tx_ring = adapter->tx_ring;
  7340. unsigned long flags;
  7341. - if (!spin_trylock_irqsave(&tx_ring->tx_lock, flags)) {
  7342. - /* Collision - tell upper layer to requeue */
  7343. - return NETDEV_TX_LOCKED;
  7344. - }
  7345. + spin_lock_irqsave(&tx_ring->tx_lock, flags);
  7346. +
  7347. if (unlikely(!PCH_GBE_DESC_UNUSED(tx_ring))) {
  7348. netif_stop_queue(netdev);
  7349. spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
  7350. diff -Nur linux-4.1.13.orig/drivers/net/ethernet/realtek/8139too.c linux-4.1.13/drivers/net/ethernet/realtek/8139too.c
  7351. --- linux-4.1.13.orig/drivers/net/ethernet/realtek/8139too.c 2015-11-09 23:34:10.000000000 +0100
  7352. +++ linux-4.1.13/drivers/net/ethernet/realtek/8139too.c 2015-11-29 09:23:09.569616840 +0100
  7353. @@ -2229,7 +2229,7 @@
  7354. struct rtl8139_private *tp = netdev_priv(dev);
  7355. const int irq = tp->pci_dev->irq;
  7356. - disable_irq(irq);
  7357. + disable_irq_nosync(irq);
  7358. rtl8139_interrupt(irq, dev);
  7359. enable_irq(irq);
  7360. }
  7361. diff -Nur linux-4.1.13.orig/drivers/net/ethernet/tehuti/tehuti.c linux-4.1.13/drivers/net/ethernet/tehuti/tehuti.c
  7362. --- linux-4.1.13.orig/drivers/net/ethernet/tehuti/tehuti.c 2015-11-09 23:34:10.000000000 +0100
  7363. +++ linux-4.1.13/drivers/net/ethernet/tehuti/tehuti.c 2015-11-29 09:23:09.569616840 +0100
  7364. @@ -1629,13 +1629,8 @@
  7365. unsigned long flags;
  7366. ENTER;
  7367. - local_irq_save(flags);
  7368. - if (!spin_trylock(&priv->tx_lock)) {
  7369. - local_irq_restore(flags);
  7370. - DBG("%s[%s]: TX locked, returning NETDEV_TX_LOCKED\n",
  7371. - BDX_DRV_NAME, ndev->name);
  7372. - return NETDEV_TX_LOCKED;
  7373. - }
  7374. +
  7375. + spin_lock_irqsave(&priv->tx_lock, flags);
  7376. /* build tx descriptor */
  7377. BDX_ASSERT(f->m.wptr >= f->m.memsz); /* started with valid wptr */
  7378. diff -Nur linux-4.1.13.orig/drivers/net/rionet.c linux-4.1.13/drivers/net/rionet.c
  7379. --- linux-4.1.13.orig/drivers/net/rionet.c 2015-11-09 23:34:10.000000000 +0100
  7380. +++ linux-4.1.13/drivers/net/rionet.c 2015-11-29 09:23:09.581616045 +0100
  7381. @@ -174,11 +174,7 @@
  7382. unsigned long flags;
  7383. int add_num = 1;
  7384. - local_irq_save(flags);
  7385. - if (!spin_trylock(&rnet->tx_lock)) {
  7386. - local_irq_restore(flags);
  7387. - return NETDEV_TX_LOCKED;
  7388. - }
  7389. + spin_lock_irqsave(&rnet->tx_lock, flags);
  7390. if (is_multicast_ether_addr(eth->h_dest))
  7391. add_num = nets[rnet->mport->id].nact;
  7392. diff -Nur linux-4.1.13.orig/drivers/net/wireless/orinoco/orinoco_usb.c linux-4.1.13/drivers/net/wireless/orinoco/orinoco_usb.c
  7393. --- linux-4.1.13.orig/drivers/net/wireless/orinoco/orinoco_usb.c 2015-11-09 23:34:10.000000000 +0100
  7394. +++ linux-4.1.13/drivers/net/wireless/orinoco/orinoco_usb.c 2015-11-29 09:23:09.581616045 +0100
  7395. @@ -697,7 +697,7 @@
  7396. while (!ctx->done.done && msecs--)
  7397. udelay(1000);
  7398. } else {
  7399. - wait_event_interruptible(ctx->done.wait,
  7400. + swait_event_interruptible(ctx->done.wait,
  7401. ctx->done.done);
  7402. }
  7403. break;
  7404. diff -Nur linux-4.1.13.orig/drivers/pci/access.c linux-4.1.13/drivers/pci/access.c
  7405. --- linux-4.1.13.orig/drivers/pci/access.c 2015-11-09 23:34:10.000000000 +0100
  7406. +++ linux-4.1.13/drivers/pci/access.c 2015-11-29 09:23:09.581616045 +0100
  7407. @@ -561,7 +561,7 @@
  7408. WARN_ON(!dev->block_cfg_access);
  7409. dev->block_cfg_access = 0;
  7410. - wake_up_all(&pci_cfg_wait);
  7411. + wake_up_all_locked(&pci_cfg_wait);
  7412. raw_spin_unlock_irqrestore(&pci_lock, flags);
  7413. }
  7414. EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
  7415. diff -Nur linux-4.1.13.orig/drivers/scsi/fcoe/fcoe.c linux-4.1.13/drivers/scsi/fcoe/fcoe.c
  7416. --- linux-4.1.13.orig/drivers/scsi/fcoe/fcoe.c 2015-11-09 23:34:10.000000000 +0100
  7417. +++ linux-4.1.13/drivers/scsi/fcoe/fcoe.c 2015-11-29 09:23:09.581616045 +0100
  7418. @@ -1287,7 +1287,7 @@
  7419. struct sk_buff *skb;
  7420. #ifdef CONFIG_SMP
  7421. struct fcoe_percpu_s *p0;
  7422. - unsigned targ_cpu = get_cpu();
  7423. + unsigned targ_cpu = get_cpu_light();
  7424. #endif /* CONFIG_SMP */
  7425. FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
  7426. @@ -1343,7 +1343,7 @@
  7427. kfree_skb(skb);
  7428. spin_unlock_bh(&p->fcoe_rx_list.lock);
  7429. }
  7430. - put_cpu();
  7431. + put_cpu_light();
  7432. #else
  7433. /*
  7434. * This a non-SMP scenario where the singular Rx thread is
  7435. @@ -1567,11 +1567,11 @@
  7436. static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
  7437. {
  7438. struct fcoe_percpu_s *fps;
  7439. - int rc;
  7440. + int rc, cpu = get_cpu_light();
  7441. - fps = &get_cpu_var(fcoe_percpu);
  7442. + fps = &per_cpu(fcoe_percpu, cpu);
  7443. rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
  7444. - put_cpu_var(fcoe_percpu);
  7445. + put_cpu_light();
  7446. return rc;
  7447. }
  7448. @@ -1767,11 +1767,11 @@
  7449. return 0;
  7450. }
  7451. - stats = per_cpu_ptr(lport->stats, get_cpu());
  7452. + stats = per_cpu_ptr(lport->stats, get_cpu_light());
  7453. stats->InvalidCRCCount++;
  7454. if (stats->InvalidCRCCount < 5)
  7455. printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
  7456. - put_cpu();
  7457. + put_cpu_light();
  7458. return -EINVAL;
  7459. }
  7460. @@ -1847,13 +1847,13 @@
  7461. goto drop;
  7462. if (!fcoe_filter_frames(lport, fp)) {
  7463. - put_cpu();
  7464. + put_cpu_light();
  7465. fc_exch_recv(lport, fp);
  7466. return;
  7467. }
  7468. drop:
  7469. stats->ErrorFrames++;
  7470. - put_cpu();
  7471. + put_cpu_light();
  7472. kfree_skb(skb);
  7473. }
  7474. diff -Nur linux-4.1.13.orig/drivers/scsi/fcoe/fcoe_ctlr.c linux-4.1.13/drivers/scsi/fcoe/fcoe_ctlr.c
  7475. --- linux-4.1.13.orig/drivers/scsi/fcoe/fcoe_ctlr.c 2015-11-09 23:34:10.000000000 +0100
  7476. +++ linux-4.1.13/drivers/scsi/fcoe/fcoe_ctlr.c 2015-11-29 09:23:09.581616045 +0100
  7477. @@ -831,7 +831,7 @@
  7478. INIT_LIST_HEAD(&del_list);
  7479. - stats = per_cpu_ptr(fip->lp->stats, get_cpu());
  7480. + stats = per_cpu_ptr(fip->lp->stats, get_cpu_light());
  7481. list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
  7482. deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
  7483. @@ -867,7 +867,7 @@
  7484. sel_time = fcf->time;
  7485. }
  7486. }
  7487. - put_cpu();
  7488. + put_cpu_light();
  7489. list_for_each_entry_safe(fcf, next, &del_list, list) {
  7490. /* Removes fcf from current list */
  7491. diff -Nur linux-4.1.13.orig/drivers/scsi/libfc/fc_exch.c linux-4.1.13/drivers/scsi/libfc/fc_exch.c
  7492. --- linux-4.1.13.orig/drivers/scsi/libfc/fc_exch.c 2015-11-09 23:34:10.000000000 +0100
  7493. +++ linux-4.1.13/drivers/scsi/libfc/fc_exch.c 2015-11-29 09:23:09.581616045 +0100
  7494. @@ -814,10 +814,10 @@
  7495. }
  7496. memset(ep, 0, sizeof(*ep));
  7497. - cpu = get_cpu();
  7498. + cpu = get_cpu_light();
  7499. pool = per_cpu_ptr(mp->pool, cpu);
  7500. spin_lock_bh(&pool->lock);
  7501. - put_cpu();
  7502. + put_cpu_light();
  7503. /* peek cache of free slot */
  7504. if (pool->left != FC_XID_UNKNOWN) {
  7505. diff -Nur linux-4.1.13.orig/drivers/scsi/libsas/sas_ata.c linux-4.1.13/drivers/scsi/libsas/sas_ata.c
  7506. --- linux-4.1.13.orig/drivers/scsi/libsas/sas_ata.c 2015-11-09 23:34:10.000000000 +0100
  7507. +++ linux-4.1.13/drivers/scsi/libsas/sas_ata.c 2015-11-29 09:23:09.581616045 +0100
  7508. @@ -190,7 +190,7 @@
  7509. /* TODO: audit callers to ensure they are ready for qc_issue to
  7510. * unconditionally re-enable interrupts
  7511. */
  7512. - local_irq_save(flags);
  7513. + local_irq_save_nort(flags);
  7514. spin_unlock(ap->lock);
  7515. /* If the device fell off, no sense in issuing commands */
  7516. @@ -255,7 +255,7 @@
  7517. out:
  7518. spin_lock(ap->lock);
  7519. - local_irq_restore(flags);
  7520. + local_irq_restore_nort(flags);
  7521. return ret;
  7522. }
  7523. diff -Nur linux-4.1.13.orig/drivers/scsi/qla2xxx/qla_inline.h linux-4.1.13/drivers/scsi/qla2xxx/qla_inline.h
  7524. --- linux-4.1.13.orig/drivers/scsi/qla2xxx/qla_inline.h 2015-11-09 23:34:10.000000000 +0100
  7525. +++ linux-4.1.13/drivers/scsi/qla2xxx/qla_inline.h 2015-11-29 09:23:09.581616045 +0100
  7526. @@ -59,12 +59,12 @@
  7527. {
  7528. unsigned long flags;
  7529. struct qla_hw_data *ha = rsp->hw;
  7530. - local_irq_save(flags);
  7531. + local_irq_save_nort(flags);
  7532. if (IS_P3P_TYPE(ha))
  7533. qla82xx_poll(0, rsp);
  7534. else
  7535. ha->isp_ops->intr_handler(0, rsp);
  7536. - local_irq_restore(flags);
  7537. + local_irq_restore_nort(flags);
  7538. }
  7539. static inline uint8_t *
  7540. diff -Nur linux-4.1.13.orig/drivers/thermal/x86_pkg_temp_thermal.c linux-4.1.13/drivers/thermal/x86_pkg_temp_thermal.c
  7541. --- linux-4.1.13.orig/drivers/thermal/x86_pkg_temp_thermal.c 2015-11-09 23:34:10.000000000 +0100
  7542. +++ linux-4.1.13/drivers/thermal/x86_pkg_temp_thermal.c 2015-11-29 09:23:09.581616045 +0100
  7543. @@ -29,6 +29,7 @@
  7544. #include <linux/pm.h>
  7545. #include <linux/thermal.h>
  7546. #include <linux/debugfs.h>
  7547. +#include <linux/work-simple.h>
  7548. #include <asm/cpu_device_id.h>
  7549. #include <asm/mce.h>
  7550. @@ -352,7 +353,7 @@
  7551. }
  7552. }
  7553. -static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  7554. +static void platform_thermal_notify_work(struct swork_event *event)
  7555. {
  7556. unsigned long flags;
  7557. int cpu = smp_processor_id();
  7558. @@ -369,7 +370,7 @@
  7559. pkg_work_scheduled[phy_id]) {
  7560. disable_pkg_thres_interrupt();
  7561. spin_unlock_irqrestore(&pkg_work_lock, flags);
  7562. - return -EINVAL;
  7563. + return;
  7564. }
  7565. pkg_work_scheduled[phy_id] = 1;
  7566. spin_unlock_irqrestore(&pkg_work_lock, flags);
  7567. @@ -378,9 +379,48 @@
  7568. schedule_delayed_work_on(cpu,
  7569. &per_cpu(pkg_temp_thermal_threshold_work, cpu),
  7570. msecs_to_jiffies(notify_delay_ms));
  7571. +}
  7572. +
  7573. +#ifdef CONFIG_PREEMPT_RT_FULL
  7574. +static struct swork_event notify_work;
  7575. +
  7576. +static int thermal_notify_work_init(void)
  7577. +{
  7578. + int err;
  7579. +
  7580. + err = swork_get();
  7581. + if (err)
  7582. + return err;
  7583. +
  7584. + INIT_SWORK(&notify_work, platform_thermal_notify_work);
  7585. return 0;
  7586. }
  7587. +static void thermal_notify_work_cleanup(void)
  7588. +{
  7589. + swork_put();
  7590. +}
  7591. +
  7592. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  7593. +{
  7594. + swork_queue(&notify_work);
  7595. + return 0;
  7596. +}
  7597. +
  7598. +#else /* !CONFIG_PREEMPT_RT_FULL */
  7599. +
  7600. +static int thermal_notify_work_init(void) { return 0; }
  7601. +
  7602. +static void thermal_notify_work_cleanup(void) { }
  7603. +
  7604. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  7605. +{
  7606. + platform_thermal_notify_work(NULL);
  7607. +
  7608. + return 0;
  7609. +}
  7610. +#endif /* CONFIG_PREEMPT_RT_FULL */
  7611. +
  7612. static int find_siblings_cpu(int cpu)
  7613. {
  7614. int i;
  7615. @@ -584,6 +624,9 @@
  7616. if (!x86_match_cpu(pkg_temp_thermal_ids))
  7617. return -ENODEV;
  7618. + if (!thermal_notify_work_init())
  7619. + return -ENODEV;
  7620. +
  7621. spin_lock_init(&pkg_work_lock);
  7622. platform_thermal_package_notify =
  7623. pkg_temp_thermal_platform_thermal_notify;
  7624. @@ -608,7 +651,7 @@
  7625. kfree(pkg_work_scheduled);
  7626. platform_thermal_package_notify = NULL;
  7627. platform_thermal_package_rate_control = NULL;
  7628. -
  7629. + thermal_notify_work_cleanup();
  7630. return -ENODEV;
  7631. }
  7632. @@ -633,6 +676,7 @@
  7633. mutex_unlock(&phy_dev_list_mutex);
  7634. platform_thermal_package_notify = NULL;
  7635. platform_thermal_package_rate_control = NULL;
  7636. + thermal_notify_work_cleanup();
  7637. for_each_online_cpu(i)
  7638. cancel_delayed_work_sync(
  7639. &per_cpu(pkg_temp_thermal_threshold_work, i));
  7640. diff -Nur linux-4.1.13.orig/drivers/tty/serial/8250/8250_core.c linux-4.1.13/drivers/tty/serial/8250/8250_core.c
  7641. --- linux-4.1.13.orig/drivers/tty/serial/8250/8250_core.c 2015-11-09 23:34:10.000000000 +0100
  7642. +++ linux-4.1.13/drivers/tty/serial/8250/8250_core.c 2015-11-29 09:23:09.581616045 +0100
  7643. @@ -36,6 +36,7 @@
  7644. #include <linux/nmi.h>
  7645. #include <linux/mutex.h>
  7646. #include <linux/slab.h>
  7647. +#include <linux/kdb.h>
  7648. #include <linux/uaccess.h>
  7649. #include <linux/pm_runtime.h>
  7650. #ifdef CONFIG_SPARC
  7651. @@ -80,7 +81,16 @@
  7652. #define DEBUG_INTR(fmt...) do { } while (0)
  7653. #endif
  7654. -#define PASS_LIMIT 512
  7655. +/*
  7656. + * On -rt we can have a more delays, and legitimately
  7657. + * so - so don't drop work spuriously and spam the
  7658. + * syslog:
  7659. + */
  7660. +#ifdef CONFIG_PREEMPT_RT_FULL
  7661. +# define PASS_LIMIT 1000000
  7662. +#else
  7663. +# define PASS_LIMIT 512
  7664. +#endif
  7665. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  7666. @@ -3372,7 +3382,7 @@
  7667. if (port->sysrq)
  7668. locked = 0;
  7669. - else if (oops_in_progress)
  7670. + else if (oops_in_progress || in_kdb_printk())
  7671. locked = spin_trylock_irqsave(&port->lock, flags);
  7672. else
  7673. spin_lock_irqsave(&port->lock, flags);
  7674. diff -Nur linux-4.1.13.orig/drivers/tty/serial/amba-pl011.c linux-4.1.13/drivers/tty/serial/amba-pl011.c
  7675. --- linux-4.1.13.orig/drivers/tty/serial/amba-pl011.c 2015-11-09 23:34:10.000000000 +0100
  7676. +++ linux-4.1.13/drivers/tty/serial/amba-pl011.c 2015-11-29 09:23:09.585615778 +0100
  7677. @@ -2000,13 +2000,19 @@
  7678. clk_enable(uap->clk);
  7679. - local_irq_save(flags);
  7680. + /*
  7681. + * local_irq_save(flags);
  7682. + *
  7683. + * This local_irq_save() is nonsense. If we come in via sysrq
  7684. + * handling then interrupts are already disabled. Aside of
  7685. + * that the port.sysrq check is racy on SMP regardless.
  7686. + */
  7687. if (uap->port.sysrq)
  7688. locked = 0;
  7689. else if (oops_in_progress)
  7690. - locked = spin_trylock(&uap->port.lock);
  7691. + locked = spin_trylock_irqsave(&uap->port.lock, flags);
  7692. else
  7693. - spin_lock(&uap->port.lock);
  7694. + spin_lock_irqsave(&uap->port.lock, flags);
  7695. /*
  7696. * First save the CR then disable the interrupts
  7697. @@ -2028,8 +2034,7 @@
  7698. writew(old_cr, uap->port.membase + UART011_CR);
  7699. if (locked)
  7700. - spin_unlock(&uap->port.lock);
  7701. - local_irq_restore(flags);
  7702. + spin_unlock_irqrestore(&uap->port.lock, flags);
  7703. clk_disable(uap->clk);
  7704. }
  7705. diff -Nur linux-4.1.13.orig/drivers/tty/serial/omap-serial.c linux-4.1.13/drivers/tty/serial/omap-serial.c
  7706. --- linux-4.1.13.orig/drivers/tty/serial/omap-serial.c 2015-11-09 23:34:10.000000000 +0100
  7707. +++ linux-4.1.13/drivers/tty/serial/omap-serial.c 2015-11-29 09:23:09.585615778 +0100
  7708. @@ -1282,13 +1282,10 @@
  7709. pm_runtime_get_sync(up->dev);
  7710. - local_irq_save(flags);
  7711. - if (up->port.sysrq)
  7712. - locked = 0;
  7713. - else if (oops_in_progress)
  7714. - locked = spin_trylock(&up->port.lock);
  7715. + if (up->port.sysrq || oops_in_progress)
  7716. + locked = spin_trylock_irqsave(&up->port.lock, flags);
  7717. else
  7718. - spin_lock(&up->port.lock);
  7719. + spin_lock_irqsave(&up->port.lock, flags);
  7720. /*
  7721. * First save the IER then disable the interrupts
  7722. @@ -1317,8 +1314,7 @@
  7723. pm_runtime_mark_last_busy(up->dev);
  7724. pm_runtime_put_autosuspend(up->dev);
  7725. if (locked)
  7726. - spin_unlock(&up->port.lock);
  7727. - local_irq_restore(flags);
  7728. + spin_unlock_irqrestore(&up->port.lock, flags);
  7729. }
  7730. static int __init
  7731. diff -Nur linux-4.1.13.orig/drivers/usb/core/hcd.c linux-4.1.13/drivers/usb/core/hcd.c
  7732. --- linux-4.1.13.orig/drivers/usb/core/hcd.c 2015-11-09 23:34:10.000000000 +0100
  7733. +++ linux-4.1.13/drivers/usb/core/hcd.c 2015-11-29 09:23:09.585615778 +0100
  7734. @@ -1684,9 +1684,9 @@
  7735. * and no one may trigger the above deadlock situation when
  7736. * running complete() in tasklet.
  7737. */
  7738. - local_irq_save(flags);
  7739. + local_irq_save_nort(flags);
  7740. urb->complete(urb);
  7741. - local_irq_restore(flags);
  7742. + local_irq_restore_nort(flags);
  7743. usb_anchor_resume_wakeups(anchor);
  7744. atomic_dec(&urb->use_count);
  7745. diff -Nur linux-4.1.13.orig/drivers/usb/gadget/function/f_fs.c linux-4.1.13/drivers/usb/gadget/function/f_fs.c
  7746. --- linux-4.1.13.orig/drivers/usb/gadget/function/f_fs.c 2015-11-09 23:34:10.000000000 +0100
  7747. +++ linux-4.1.13/drivers/usb/gadget/function/f_fs.c 2015-11-29 09:23:09.585615778 +0100
  7748. @@ -1405,7 +1405,7 @@
  7749. pr_info("%s(): freeing\n", __func__);
  7750. ffs_data_clear(ffs);
  7751. BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
  7752. - waitqueue_active(&ffs->ep0req_completion.wait));
  7753. + swaitqueue_active(&ffs->ep0req_completion.wait));
  7754. kfree(ffs->dev_name);
  7755. kfree(ffs);
  7756. }
  7757. diff -Nur linux-4.1.13.orig/drivers/usb/gadget/legacy/inode.c linux-4.1.13/drivers/usb/gadget/legacy/inode.c
  7758. --- linux-4.1.13.orig/drivers/usb/gadget/legacy/inode.c 2015-11-09 23:34:10.000000000 +0100
  7759. +++ linux-4.1.13/drivers/usb/gadget/legacy/inode.c 2015-11-29 09:23:09.585615778 +0100
  7760. @@ -345,7 +345,7 @@
  7761. spin_unlock_irq (&epdata->dev->lock);
  7762. if (likely (value == 0)) {
  7763. - value = wait_event_interruptible (done.wait, done.done);
  7764. + value = swait_event_interruptible (done.wait, done.done);
  7765. if (value != 0) {
  7766. spin_lock_irq (&epdata->dev->lock);
  7767. if (likely (epdata->ep != NULL)) {
  7768. @@ -354,7 +354,7 @@
  7769. usb_ep_dequeue (epdata->ep, epdata->req);
  7770. spin_unlock_irq (&epdata->dev->lock);
  7771. - wait_event (done.wait, done.done);
  7772. + swait_event (done.wait, done.done);
  7773. if (epdata->status == -ECONNRESET)
  7774. epdata->status = -EINTR;
  7775. } else {
  7776. diff -Nur linux-4.1.13.orig/fs/aio.c linux-4.1.13/fs/aio.c
  7777. --- linux-4.1.13.orig/fs/aio.c 2015-11-09 23:34:10.000000000 +0100
  7778. +++ linux-4.1.13/fs/aio.c 2015-11-29 09:23:09.585615778 +0100
  7779. @@ -40,6 +40,7 @@
  7780. #include <linux/ramfs.h>
  7781. #include <linux/percpu-refcount.h>
  7782. #include <linux/mount.h>
  7783. +#include <linux/work-simple.h>
  7784. #include <asm/kmap_types.h>
  7785. #include <asm/uaccess.h>
  7786. @@ -115,7 +116,7 @@
  7787. struct page **ring_pages;
  7788. long nr_pages;
  7789. - struct work_struct free_work;
  7790. + struct swork_event free_work;
  7791. /*
  7792. * signals when all in-flight requests are done
  7793. @@ -253,6 +254,7 @@
  7794. .mount = aio_mount,
  7795. .kill_sb = kill_anon_super,
  7796. };
  7797. + BUG_ON(swork_get());
  7798. aio_mnt = kern_mount(&aio_fs);
  7799. if (IS_ERR(aio_mnt))
  7800. panic("Failed to create aio fs mount.");
  7801. @@ -559,9 +561,9 @@
  7802. return cancel(&kiocb->common);
  7803. }
  7804. -static void free_ioctx(struct work_struct *work)
  7805. +static void free_ioctx(struct swork_event *sev)
  7806. {
  7807. - struct kioctx *ctx = container_of(work, struct kioctx, free_work);
  7808. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  7809. pr_debug("freeing %p\n", ctx);
  7810. @@ -580,8 +582,8 @@
  7811. if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
  7812. complete(&ctx->rq_wait->comp);
  7813. - INIT_WORK(&ctx->free_work, free_ioctx);
  7814. - schedule_work(&ctx->free_work);
  7815. + INIT_SWORK(&ctx->free_work, free_ioctx);
  7816. + swork_queue(&ctx->free_work);
  7817. }
  7818. /*
  7819. @@ -589,9 +591,9 @@
  7820. * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
  7821. * now it's safe to cancel any that need to be.
  7822. */
  7823. -static void free_ioctx_users(struct percpu_ref *ref)
  7824. +static void free_ioctx_users_work(struct swork_event *sev)
  7825. {
  7826. - struct kioctx *ctx = container_of(ref, struct kioctx, users);
  7827. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  7828. struct aio_kiocb *req;
  7829. spin_lock_irq(&ctx->ctx_lock);
  7830. @@ -610,6 +612,14 @@
  7831. percpu_ref_put(&ctx->reqs);
  7832. }
  7833. +static void free_ioctx_users(struct percpu_ref *ref)
  7834. +{
  7835. + struct kioctx *ctx = container_of(ref, struct kioctx, users);
  7836. +
  7837. + INIT_SWORK(&ctx->free_work, free_ioctx_users_work);
  7838. + swork_queue(&ctx->free_work);
  7839. +}
  7840. +
  7841. static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
  7842. {
  7843. unsigned i, new_nr;
  7844. diff -Nur linux-4.1.13.orig/fs/autofs4/autofs_i.h linux-4.1.13/fs/autofs4/autofs_i.h
  7845. --- linux-4.1.13.orig/fs/autofs4/autofs_i.h 2015-11-09 23:34:10.000000000 +0100
  7846. +++ linux-4.1.13/fs/autofs4/autofs_i.h 2015-11-29 09:23:09.585615778 +0100
  7847. @@ -34,6 +34,7 @@
  7848. #include <linux/sched.h>
  7849. #include <linux/mount.h>
  7850. #include <linux/namei.h>
  7851. +#include <linux/delay.h>
  7852. #include <asm/current.h>
  7853. #include <asm/uaccess.h>
  7854. diff -Nur linux-4.1.13.orig/fs/autofs4/expire.c linux-4.1.13/fs/autofs4/expire.c
  7855. --- linux-4.1.13.orig/fs/autofs4/expire.c 2015-11-09 23:34:10.000000000 +0100
  7856. +++ linux-4.1.13/fs/autofs4/expire.c 2015-11-29 09:23:09.585615778 +0100
  7857. @@ -150,7 +150,7 @@
  7858. parent = p->d_parent;
  7859. if (!spin_trylock(&parent->d_lock)) {
  7860. spin_unlock(&p->d_lock);
  7861. - cpu_relax();
  7862. + cpu_chill();
  7863. goto relock;
  7864. }
  7865. spin_unlock(&p->d_lock);
  7866. diff -Nur linux-4.1.13.orig/fs/buffer.c linux-4.1.13/fs/buffer.c
  7867. --- linux-4.1.13.orig/fs/buffer.c 2015-11-09 23:34:10.000000000 +0100
  7868. +++ linux-4.1.13/fs/buffer.c 2015-11-29 09:23:09.585615778 +0100
  7869. @@ -301,8 +301,7 @@
  7870. * decide that the page is now completely done.
  7871. */
  7872. first = page_buffers(page);
  7873. - local_irq_save(flags);
  7874. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  7875. + flags = bh_uptodate_lock_irqsave(first);
  7876. clear_buffer_async_read(bh);
  7877. unlock_buffer(bh);
  7878. tmp = bh;
  7879. @@ -315,8 +314,7 @@
  7880. }
  7881. tmp = tmp->b_this_page;
  7882. } while (tmp != bh);
  7883. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  7884. - local_irq_restore(flags);
  7885. + bh_uptodate_unlock_irqrestore(first, flags);
  7886. /*
  7887. * If none of the buffers had errors and they are all
  7888. @@ -328,9 +326,7 @@
  7889. return;
  7890. still_busy:
  7891. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  7892. - local_irq_restore(flags);
  7893. - return;
  7894. + bh_uptodate_unlock_irqrestore(first, flags);
  7895. }
  7896. /*
  7897. @@ -358,8 +354,7 @@
  7898. }
  7899. first = page_buffers(page);
  7900. - local_irq_save(flags);
  7901. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  7902. + flags = bh_uptodate_lock_irqsave(first);
  7903. clear_buffer_async_write(bh);
  7904. unlock_buffer(bh);
  7905. @@ -371,15 +366,12 @@
  7906. }
  7907. tmp = tmp->b_this_page;
  7908. }
  7909. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  7910. - local_irq_restore(flags);
  7911. + bh_uptodate_unlock_irqrestore(first, flags);
  7912. end_page_writeback(page);
  7913. return;
  7914. still_busy:
  7915. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  7916. - local_irq_restore(flags);
  7917. - return;
  7918. + bh_uptodate_unlock_irqrestore(first, flags);
  7919. }
  7920. EXPORT_SYMBOL(end_buffer_async_write);
  7921. @@ -3325,6 +3317,7 @@
  7922. struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
  7923. if (ret) {
  7924. INIT_LIST_HEAD(&ret->b_assoc_buffers);
  7925. + buffer_head_init_locks(ret);
  7926. preempt_disable();
  7927. __this_cpu_inc(bh_accounting.nr);
  7928. recalc_bh_state();
  7929. diff -Nur linux-4.1.13.orig/fs/dcache.c linux-4.1.13/fs/dcache.c
  7930. --- linux-4.1.13.orig/fs/dcache.c 2015-11-09 23:34:10.000000000 +0100
  7931. +++ linux-4.1.13/fs/dcache.c 2015-11-29 09:23:09.585615778 +0100
  7932. @@ -19,6 +19,7 @@
  7933. #include <linux/mm.h>
  7934. #include <linux/fs.h>
  7935. #include <linux/fsnotify.h>
  7936. +#include <linux/delay.h>
  7937. #include <linux/slab.h>
  7938. #include <linux/init.h>
  7939. #include <linux/hash.h>
  7940. @@ -589,7 +590,7 @@
  7941. failed:
  7942. spin_unlock(&dentry->d_lock);
  7943. - cpu_relax();
  7944. + cpu_chill();
  7945. return dentry; /* try again with same dentry */
  7946. }
  7947. @@ -2398,7 +2399,7 @@
  7948. if (dentry->d_lockref.count == 1) {
  7949. if (!spin_trylock(&inode->i_lock)) {
  7950. spin_unlock(&dentry->d_lock);
  7951. - cpu_relax();
  7952. + cpu_chill();
  7953. goto again;
  7954. }
  7955. dentry->d_flags &= ~DCACHE_CANT_MOUNT;
  7956. diff -Nur linux-4.1.13.orig/fs/eventpoll.c linux-4.1.13/fs/eventpoll.c
  7957. --- linux-4.1.13.orig/fs/eventpoll.c 2015-11-09 23:34:10.000000000 +0100
  7958. +++ linux-4.1.13/fs/eventpoll.c 2015-11-29 09:23:09.585615778 +0100
  7959. @@ -505,12 +505,12 @@
  7960. */
  7961. static void ep_poll_safewake(wait_queue_head_t *wq)
  7962. {
  7963. - int this_cpu = get_cpu();
  7964. + int this_cpu = get_cpu_light();
  7965. ep_call_nested(&poll_safewake_ncalls, EP_MAX_NESTS,
  7966. ep_poll_wakeup_proc, NULL, wq, (void *) (long) this_cpu);
  7967. - put_cpu();
  7968. + put_cpu_light();
  7969. }
  7970. static void ep_remove_wait_queue(struct eppoll_entry *pwq)
  7971. diff -Nur linux-4.1.13.orig/fs/exec.c linux-4.1.13/fs/exec.c
  7972. --- linux-4.1.13.orig/fs/exec.c 2015-11-09 23:34:10.000000000 +0100
  7973. +++ linux-4.1.13/fs/exec.c 2015-11-29 09:23:09.593615242 +0100
  7974. @@ -859,12 +859,14 @@
  7975. }
  7976. }
  7977. task_lock(tsk);
  7978. + preempt_disable_rt();
  7979. active_mm = tsk->active_mm;
  7980. tsk->mm = mm;
  7981. tsk->active_mm = mm;
  7982. activate_mm(active_mm, mm);
  7983. tsk->mm->vmacache_seqnum = 0;
  7984. vmacache_flush(tsk);
  7985. + preempt_enable_rt();
  7986. task_unlock(tsk);
  7987. if (old_mm) {
  7988. up_read(&old_mm->mmap_sem);
  7989. diff -Nur linux-4.1.13.orig/fs/jbd/checkpoint.c linux-4.1.13/fs/jbd/checkpoint.c
  7990. --- linux-4.1.13.orig/fs/jbd/checkpoint.c 2015-11-09 23:34:10.000000000 +0100
  7991. +++ linux-4.1.13/fs/jbd/checkpoint.c 2015-11-29 09:23:09.593615242 +0100
  7992. @@ -129,6 +129,8 @@
  7993. if (journal->j_flags & JFS_ABORT)
  7994. return;
  7995. spin_unlock(&journal->j_state_lock);
  7996. + if (current->plug)
  7997. + io_schedule();
  7998. mutex_lock(&journal->j_checkpoint_mutex);
  7999. /*
  8000. diff -Nur linux-4.1.13.orig/fs/jbd2/checkpoint.c linux-4.1.13/fs/jbd2/checkpoint.c
  8001. --- linux-4.1.13.orig/fs/jbd2/checkpoint.c 2015-11-09 23:34:10.000000000 +0100
  8002. +++ linux-4.1.13/fs/jbd2/checkpoint.c 2015-11-29 09:23:09.593615242 +0100
  8003. @@ -116,6 +116,8 @@
  8004. nblocks = jbd2_space_needed(journal);
  8005. while (jbd2_log_space_left(journal) < nblocks) {
  8006. write_unlock(&journal->j_state_lock);
  8007. + if (current->plug)
  8008. + io_schedule();
  8009. mutex_lock(&journal->j_checkpoint_mutex);
  8010. /*
  8011. diff -Nur linux-4.1.13.orig/fs/namespace.c linux-4.1.13/fs/namespace.c
  8012. --- linux-4.1.13.orig/fs/namespace.c 2015-11-09 23:34:10.000000000 +0100
  8013. +++ linux-4.1.13/fs/namespace.c 2015-11-29 09:23:09.593615242 +0100
  8014. @@ -14,6 +14,7 @@
  8015. #include <linux/mnt_namespace.h>
  8016. #include <linux/user_namespace.h>
  8017. #include <linux/namei.h>
  8018. +#include <linux/delay.h>
  8019. #include <linux/security.h>
  8020. #include <linux/idr.h>
  8021. #include <linux/init.h> /* init_rootfs */
  8022. @@ -353,8 +354,11 @@
  8023. * incremented count after it has set MNT_WRITE_HOLD.
  8024. */
  8025. smp_mb();
  8026. - while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
  8027. - cpu_relax();
  8028. + while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
  8029. + preempt_enable();
  8030. + cpu_chill();
  8031. + preempt_disable();
  8032. + }
  8033. /*
  8034. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  8035. * be set to match its requirements. So we must not load that until
  8036. diff -Nur linux-4.1.13.orig/fs/ntfs/aops.c linux-4.1.13/fs/ntfs/aops.c
  8037. --- linux-4.1.13.orig/fs/ntfs/aops.c 2015-11-09 23:34:10.000000000 +0100
  8038. +++ linux-4.1.13/fs/ntfs/aops.c 2015-11-29 09:23:09.593615242 +0100
  8039. @@ -107,8 +107,7 @@
  8040. "0x%llx.", (unsigned long long)bh->b_blocknr);
  8041. }
  8042. first = page_buffers(page);
  8043. - local_irq_save(flags);
  8044. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  8045. + flags = bh_uptodate_lock_irqsave(first);
  8046. clear_buffer_async_read(bh);
  8047. unlock_buffer(bh);
  8048. tmp = bh;
  8049. @@ -123,8 +122,7 @@
  8050. }
  8051. tmp = tmp->b_this_page;
  8052. } while (tmp != bh);
  8053. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  8054. - local_irq_restore(flags);
  8055. + bh_uptodate_unlock_irqrestore(first, flags);
  8056. /*
  8057. * If none of the buffers had errors then we can set the page uptodate,
  8058. * but we first have to perform the post read mst fixups, if the
  8059. @@ -145,13 +143,13 @@
  8060. recs = PAGE_CACHE_SIZE / rec_size;
  8061. /* Should have been verified before we got here... */
  8062. BUG_ON(!recs);
  8063. - local_irq_save(flags);
  8064. + local_irq_save_nort(flags);
  8065. kaddr = kmap_atomic(page);
  8066. for (i = 0; i < recs; i++)
  8067. post_read_mst_fixup((NTFS_RECORD*)(kaddr +
  8068. i * rec_size), rec_size);
  8069. kunmap_atomic(kaddr);
  8070. - local_irq_restore(flags);
  8071. + local_irq_restore_nort(flags);
  8072. flush_dcache_page(page);
  8073. if (likely(page_uptodate && !PageError(page)))
  8074. SetPageUptodate(page);
  8075. @@ -159,9 +157,7 @@
  8076. unlock_page(page);
  8077. return;
  8078. still_busy:
  8079. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  8080. - local_irq_restore(flags);
  8081. - return;
  8082. + bh_uptodate_unlock_irqrestore(first, flags);
  8083. }
  8084. /**
  8085. diff -Nur linux-4.1.13.orig/fs/timerfd.c linux-4.1.13/fs/timerfd.c
  8086. --- linux-4.1.13.orig/fs/timerfd.c 2015-11-09 23:34:10.000000000 +0100
  8087. +++ linux-4.1.13/fs/timerfd.c 2015-11-29 09:23:09.593615242 +0100
  8088. @@ -450,7 +450,10 @@
  8089. break;
  8090. }
  8091. spin_unlock_irq(&ctx->wqh.lock);
  8092. - cpu_relax();
  8093. + if (isalarm(ctx))
  8094. + hrtimer_wait_for_timer(&ctx->t.alarm.timer);
  8095. + else
  8096. + hrtimer_wait_for_timer(&ctx->t.tmr);
  8097. }
  8098. /*
  8099. diff -Nur linux-4.1.13.orig/fs/xfs/xfs_inode.c linux-4.1.13/fs/xfs/xfs_inode.c
  8100. --- linux-4.1.13.orig/fs/xfs/xfs_inode.c 2015-11-09 23:34:10.000000000 +0100
  8101. +++ linux-4.1.13/fs/xfs/xfs_inode.c 2015-11-29 09:23:09.593615242 +0100
  8102. @@ -164,7 +164,7 @@
  8103. (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
  8104. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  8105. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  8106. - ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  8107. + ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
  8108. if (lock_flags & XFS_IOLOCK_EXCL)
  8109. mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  8110. @@ -212,7 +212,7 @@
  8111. (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
  8112. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  8113. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  8114. - ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  8115. + ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
  8116. if (lock_flags & XFS_IOLOCK_EXCL) {
  8117. if (!mrtryupdate(&ip->i_iolock))
  8118. @@ -281,7 +281,7 @@
  8119. (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
  8120. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  8121. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  8122. - ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  8123. + ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
  8124. ASSERT(lock_flags != 0);
  8125. if (lock_flags & XFS_IOLOCK_EXCL)
  8126. @@ -364,30 +364,38 @@
  8127. /*
  8128. * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
  8129. - * value. This shouldn't be called for page fault locking, but we also need to
  8130. - * ensure we don't overrun the number of lockdep subclasses for the iolock or
  8131. - * mmaplock as that is limited to 12 by the mmap lock lockdep annotations.
  8132. + * value. This can be called for any type of inode lock combination, including
  8133. + * parent locking. Care must be taken to ensure we don't overrun the subclass
  8134. + * storage fields in the class mask we build.
  8135. */
  8136. static inline int
  8137. xfs_lock_inumorder(int lock_mode, int subclass)
  8138. {
  8139. + int class = 0;
  8140. +
  8141. + ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
  8142. + XFS_ILOCK_RTSUM)));
  8143. +
  8144. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
  8145. - ASSERT(subclass + XFS_LOCK_INUMORDER <
  8146. - (1 << (XFS_MMAPLOCK_SHIFT - XFS_IOLOCK_SHIFT)));
  8147. - lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
  8148. + ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
  8149. + ASSERT(subclass + XFS_IOLOCK_PARENT_VAL <
  8150. + MAX_LOCKDEP_SUBCLASSES);
  8151. + class += subclass << XFS_IOLOCK_SHIFT;
  8152. + if (lock_mode & XFS_IOLOCK_PARENT)
  8153. + class += XFS_IOLOCK_PARENT_VAL << XFS_IOLOCK_SHIFT;
  8154. }
  8155. if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
  8156. - ASSERT(subclass + XFS_LOCK_INUMORDER <
  8157. - (1 << (XFS_ILOCK_SHIFT - XFS_MMAPLOCK_SHIFT)));
  8158. - lock_mode |= (subclass + XFS_LOCK_INUMORDER) <<
  8159. - XFS_MMAPLOCK_SHIFT;
  8160. + ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
  8161. + class += subclass << XFS_MMAPLOCK_SHIFT;
  8162. }
  8163. - if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
  8164. - lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
  8165. + if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
  8166. + ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
  8167. + class += subclass << XFS_ILOCK_SHIFT;
  8168. + }
  8169. - return lock_mode;
  8170. + return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
  8171. }
  8172. /*
  8173. @@ -399,6 +407,11 @@
  8174. * transaction (such as truncate). This can result in deadlock since the long
  8175. * running trans might need to wait for the inode we just locked in order to
  8176. * push the tail and free space in the log.
  8177. + *
  8178. + * xfs_lock_inodes() can only be used to lock one type of lock at a time -
  8179. + * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
  8180. + * lock more than one at a time, lockdep will report false positives saying we
  8181. + * have violated locking orders.
  8182. */
  8183. void
  8184. xfs_lock_inodes(
  8185. @@ -409,8 +422,29 @@
  8186. int attempts = 0, i, j, try_lock;
  8187. xfs_log_item_t *lp;
  8188. - /* currently supports between 2 and 5 inodes */
  8189. + /*
  8190. + * Currently supports between 2 and 5 inodes with exclusive locking. We
  8191. + * support an arbitrary depth of locking here, but absolute limits on
  8192. + * inodes depend on the the type of locking and the limits placed by
  8193. + * lockdep annotations in xfs_lock_inumorder. These are all checked by
  8194. + * the asserts.
  8195. + */
  8196. ASSERT(ips && inodes >= 2 && inodes <= 5);
  8197. + ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
  8198. + XFS_ILOCK_EXCL));
  8199. + ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
  8200. + XFS_ILOCK_SHARED)));
  8201. + ASSERT(!(lock_mode & XFS_IOLOCK_EXCL) ||
  8202. + inodes <= XFS_IOLOCK_MAX_SUBCLASS + 1);
  8203. + ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
  8204. + inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
  8205. + ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
  8206. + inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
  8207. +
  8208. + if (lock_mode & XFS_IOLOCK_EXCL) {
  8209. + ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
  8210. + } else if (lock_mode & XFS_MMAPLOCK_EXCL)
  8211. + ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
  8212. try_lock = 0;
  8213. i = 0;
  8214. diff -Nur linux-4.1.13.orig/fs/xfs/xfs_inode.h linux-4.1.13/fs/xfs/xfs_inode.h
  8215. --- linux-4.1.13.orig/fs/xfs/xfs_inode.h 2015-11-09 23:34:10.000000000 +0100
  8216. +++ linux-4.1.13/fs/xfs/xfs_inode.h 2015-11-29 09:23:09.593615242 +0100
  8217. @@ -284,9 +284,9 @@
  8218. * Flags for lockdep annotations.
  8219. *
  8220. * XFS_LOCK_PARENT - for directory operations that require locking a
  8221. - * parent directory inode and a child entry inode. The parent gets locked
  8222. - * with this flag so it gets a lockdep subclass of 1 and the child entry
  8223. - * lock will have a lockdep subclass of 0.
  8224. + * parent directory inode and a child entry inode. IOLOCK requires nesting,
  8225. + * MMAPLOCK does not support this class, ILOCK requires a single subclass
  8226. + * to differentiate parent from child.
  8227. *
  8228. * XFS_LOCK_RTBITMAP/XFS_LOCK_RTSUM - the realtime device bitmap and summary
  8229. * inodes do not participate in the normal lock order, and thus have their
  8230. @@ -295,30 +295,63 @@
  8231. * XFS_LOCK_INUMORDER - for locking several inodes at the some time
  8232. * with xfs_lock_inodes(). This flag is used as the starting subclass
  8233. * and each subsequent lock acquired will increment the subclass by one.
  8234. - * So the first lock acquired will have a lockdep subclass of 4, the
  8235. - * second lock will have a lockdep subclass of 5, and so on. It is
  8236. - * the responsibility of the class builder to shift this to the correct
  8237. - * portion of the lock_mode lockdep mask.
  8238. + * However, MAX_LOCKDEP_SUBCLASSES == 8, which means we are greatly
  8239. + * limited to the subclasses we can represent via nesting. We need at least
  8240. + * 5 inodes nest depth for the ILOCK through rename, and we also have to support
  8241. + * XFS_ILOCK_PARENT, which gives 6 subclasses. Then we have XFS_ILOCK_RTBITMAP
  8242. + * and XFS_ILOCK_RTSUM, which are another 2 unique subclasses, so that's all
  8243. + * 8 subclasses supported by lockdep.
  8244. + *
  8245. + * This also means we have to number the sub-classes in the lowest bits of
  8246. + * the mask we keep, and we have to ensure we never exceed 3 bits of lockdep
  8247. + * mask and we can't use bit-masking to build the subclasses. What a mess.
  8248. + *
  8249. + * Bit layout:
  8250. + *
  8251. + * Bit Lock Region
  8252. + * 16-19 XFS_IOLOCK_SHIFT dependencies
  8253. + * 20-23 XFS_MMAPLOCK_SHIFT dependencies
  8254. + * 24-31 XFS_ILOCK_SHIFT dependencies
  8255. + *
  8256. + * IOLOCK values
  8257. + *
  8258. + * 0-3 subclass value
  8259. + * 4-7 PARENT subclass values
  8260. + *
  8261. + * MMAPLOCK values
  8262. + *
  8263. + * 0-3 subclass value
  8264. + * 4-7 unused
  8265. + *
  8266. + * ILOCK values
  8267. + * 0-4 subclass values
  8268. + * 5 PARENT subclass (not nestable)
  8269. + * 6 RTBITMAP subclass (not nestable)
  8270. + * 7 RTSUM subclass (not nestable)
  8271. + *
  8272. */
  8273. -#define XFS_LOCK_PARENT 1
  8274. -#define XFS_LOCK_RTBITMAP 2
  8275. -#define XFS_LOCK_RTSUM 3
  8276. -#define XFS_LOCK_INUMORDER 4
  8277. -
  8278. -#define XFS_IOLOCK_SHIFT 16
  8279. -#define XFS_IOLOCK_PARENT (XFS_LOCK_PARENT << XFS_IOLOCK_SHIFT)
  8280. +#define XFS_IOLOCK_SHIFT 16
  8281. +#define XFS_IOLOCK_PARENT_VAL 4
  8282. +#define XFS_IOLOCK_MAX_SUBCLASS (XFS_IOLOCK_PARENT_VAL - 1)
  8283. +#define XFS_IOLOCK_DEP_MASK 0x000f0000
  8284. +#define XFS_IOLOCK_PARENT (XFS_IOLOCK_PARENT_VAL << XFS_IOLOCK_SHIFT)
  8285. -#define XFS_MMAPLOCK_SHIFT 20
  8286. +#define XFS_MMAPLOCK_SHIFT 20
  8287. +#define XFS_MMAPLOCK_NUMORDER 0
  8288. +#define XFS_MMAPLOCK_MAX_SUBCLASS 3
  8289. +#define XFS_MMAPLOCK_DEP_MASK 0x00f00000
  8290. -#define XFS_ILOCK_SHIFT 24
  8291. -#define XFS_ILOCK_PARENT (XFS_LOCK_PARENT << XFS_ILOCK_SHIFT)
  8292. -#define XFS_ILOCK_RTBITMAP (XFS_LOCK_RTBITMAP << XFS_ILOCK_SHIFT)
  8293. -#define XFS_ILOCK_RTSUM (XFS_LOCK_RTSUM << XFS_ILOCK_SHIFT)
  8294. +#define XFS_ILOCK_SHIFT 24
  8295. +#define XFS_ILOCK_PARENT_VAL 5
  8296. +#define XFS_ILOCK_MAX_SUBCLASS (XFS_ILOCK_PARENT_VAL - 1)
  8297. +#define XFS_ILOCK_RTBITMAP_VAL 6
  8298. +#define XFS_ILOCK_RTSUM_VAL 7
  8299. +#define XFS_ILOCK_DEP_MASK 0xff000000
  8300. +#define XFS_ILOCK_PARENT (XFS_ILOCK_PARENT_VAL << XFS_ILOCK_SHIFT)
  8301. +#define XFS_ILOCK_RTBITMAP (XFS_ILOCK_RTBITMAP_VAL << XFS_ILOCK_SHIFT)
  8302. +#define XFS_ILOCK_RTSUM (XFS_ILOCK_RTSUM_VAL << XFS_ILOCK_SHIFT)
  8303. -#define XFS_IOLOCK_DEP_MASK 0x000f0000
  8304. -#define XFS_MMAPLOCK_DEP_MASK 0x00f00000
  8305. -#define XFS_ILOCK_DEP_MASK 0xff000000
  8306. -#define XFS_LOCK_DEP_MASK (XFS_IOLOCK_DEP_MASK | \
  8307. +#define XFS_LOCK_SUBCLASS_MASK (XFS_IOLOCK_DEP_MASK | \
  8308. XFS_MMAPLOCK_DEP_MASK | \
  8309. XFS_ILOCK_DEP_MASK)
  8310. diff -Nur linux-4.1.13.orig/include/acpi/platform/aclinux.h linux-4.1.13/include/acpi/platform/aclinux.h
  8311. --- linux-4.1.13.orig/include/acpi/platform/aclinux.h 2015-11-09 23:34:10.000000000 +0100
  8312. +++ linux-4.1.13/include/acpi/platform/aclinux.h 2015-11-29 09:23:09.593615242 +0100
  8313. @@ -123,6 +123,7 @@
  8314. #define acpi_cache_t struct kmem_cache
  8315. #define acpi_spinlock spinlock_t *
  8316. +#define acpi_raw_spinlock raw_spinlock_t *
  8317. #define acpi_cpu_flags unsigned long
  8318. /* Use native linux version of acpi_os_allocate_zeroed */
  8319. @@ -141,6 +142,20 @@
  8320. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_get_thread_id
  8321. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_create_lock
  8322. +#define acpi_os_create_raw_lock(__handle) \
  8323. +({ \
  8324. + raw_spinlock_t *lock = ACPI_ALLOCATE(sizeof(*lock)); \
  8325. + \
  8326. + if (lock) { \
  8327. + *(__handle) = lock; \
  8328. + raw_spin_lock_init(*(__handle)); \
  8329. + } \
  8330. + lock ? AE_OK : AE_NO_MEMORY; \
  8331. + })
  8332. +
  8333. +#define acpi_os_delete_raw_lock(__handle) kfree(__handle)
  8334. +
  8335. +
  8336. /*
  8337. * OSL interfaces used by debugger/disassembler
  8338. */
  8339. diff -Nur linux-4.1.13.orig/include/asm-generic/bug.h linux-4.1.13/include/asm-generic/bug.h
  8340. --- linux-4.1.13.orig/include/asm-generic/bug.h 2015-11-09 23:34:10.000000000 +0100
  8341. +++ linux-4.1.13/include/asm-generic/bug.h 2015-11-29 09:23:09.593615242 +0100
  8342. @@ -206,6 +206,20 @@
  8343. # define WARN_ON_SMP(x) ({0;})
  8344. #endif
  8345. +#ifdef CONFIG_PREEMPT_RT_BASE
  8346. +# define BUG_ON_RT(c) BUG_ON(c)
  8347. +# define BUG_ON_NONRT(c) do { } while (0)
  8348. +# define WARN_ON_RT(condition) WARN_ON(condition)
  8349. +# define WARN_ON_NONRT(condition) do { } while (0)
  8350. +# define WARN_ON_ONCE_NONRT(condition) do { } while (0)
  8351. +#else
  8352. +# define BUG_ON_RT(c) do { } while (0)
  8353. +# define BUG_ON_NONRT(c) BUG_ON(c)
  8354. +# define WARN_ON_RT(condition) do { } while (0)
  8355. +# define WARN_ON_NONRT(condition) WARN_ON(condition)
  8356. +# define WARN_ON_ONCE_NONRT(condition) WARN_ON_ONCE(condition)
  8357. +#endif
  8358. +
  8359. #endif /* __ASSEMBLY__ */
  8360. #endif
  8361. diff -Nur linux-4.1.13.orig/include/asm-generic/futex.h linux-4.1.13/include/asm-generic/futex.h
  8362. --- linux-4.1.13.orig/include/asm-generic/futex.h 2015-11-09 23:34:10.000000000 +0100
  8363. +++ linux-4.1.13/include/asm-generic/futex.h 2015-11-29 09:23:09.593615242 +0100
  8364. @@ -8,8 +8,7 @@
  8365. #ifndef CONFIG_SMP
  8366. /*
  8367. * The following implementation only for uniprocessor machines.
  8368. - * For UP, it's relies on the fact that pagefault_disable() also disables
  8369. - * preemption to ensure mutual exclusion.
  8370. + * It relies on preempt_disable() ensuring mutual exclusion.
  8371. *
  8372. */
  8373. @@ -38,6 +37,7 @@
  8374. if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
  8375. oparg = 1 << oparg;
  8376. + preempt_disable();
  8377. pagefault_disable();
  8378. ret = -EFAULT;
  8379. @@ -72,6 +72,7 @@
  8380. out_pagefault_enable:
  8381. pagefault_enable();
  8382. + preempt_enable();
  8383. if (ret == 0) {
  8384. switch (cmp) {
  8385. @@ -106,6 +107,7 @@
  8386. {
  8387. u32 val;
  8388. + preempt_disable();
  8389. if (unlikely(get_user(val, uaddr) != 0))
  8390. return -EFAULT;
  8391. @@ -113,6 +115,7 @@
  8392. return -EFAULT;
  8393. *uval = val;
  8394. + preempt_enable();
  8395. return 0;
  8396. }
  8397. diff -Nur linux-4.1.13.orig/include/linux/blkdev.h linux-4.1.13/include/linux/blkdev.h
  8398. --- linux-4.1.13.orig/include/linux/blkdev.h 2015-11-09 23:34:10.000000000 +0100
  8399. +++ linux-4.1.13/include/linux/blkdev.h 2015-11-29 09:23:09.593615242 +0100
  8400. @@ -101,6 +101,7 @@
  8401. struct list_head queuelist;
  8402. union {
  8403. struct call_single_data csd;
  8404. + struct work_struct work;
  8405. unsigned long fifo_time;
  8406. };
  8407. @@ -482,7 +483,7 @@
  8408. struct throtl_data *td;
  8409. #endif
  8410. struct rcu_head rcu_head;
  8411. - wait_queue_head_t mq_freeze_wq;
  8412. + struct swait_head mq_freeze_wq;
  8413. struct percpu_ref mq_usage_counter;
  8414. struct list_head all_q_node;
  8415. diff -Nur linux-4.1.13.orig/include/linux/blk-mq.h linux-4.1.13/include/linux/blk-mq.h
  8416. --- linux-4.1.13.orig/include/linux/blk-mq.h 2015-11-09 23:34:10.000000000 +0100
  8417. +++ linux-4.1.13/include/linux/blk-mq.h 2015-11-29 09:23:09.593615242 +0100
  8418. @@ -202,6 +202,7 @@
  8419. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_index);
  8420. struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *, unsigned int, int);
  8421. +void __blk_mq_complete_request_remote_work(struct work_struct *work);
  8422. int blk_mq_request_started(struct request *rq);
  8423. void blk_mq_start_request(struct request *rq);
  8424. diff -Nur linux-4.1.13.orig/include/linux/bottom_half.h linux-4.1.13/include/linux/bottom_half.h
  8425. --- linux-4.1.13.orig/include/linux/bottom_half.h 2015-11-09 23:34:10.000000000 +0100
  8426. +++ linux-4.1.13/include/linux/bottom_half.h 2015-11-29 09:23:09.593615242 +0100
  8427. @@ -4,6 +4,39 @@
  8428. #include <linux/preempt.h>
  8429. #include <linux/preempt_mask.h>
  8430. +#ifdef CONFIG_PREEMPT_RT_FULL
  8431. +
  8432. +extern void __local_bh_disable(void);
  8433. +extern void _local_bh_enable(void);
  8434. +extern void __local_bh_enable(void);
  8435. +
  8436. +static inline void local_bh_disable(void)
  8437. +{
  8438. + __local_bh_disable();
  8439. +}
  8440. +
  8441. +static inline void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
  8442. +{
  8443. + __local_bh_disable();
  8444. +}
  8445. +
  8446. +static inline void local_bh_enable(void)
  8447. +{
  8448. + __local_bh_enable();
  8449. +}
  8450. +
  8451. +static inline void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
  8452. +{
  8453. + __local_bh_enable();
  8454. +}
  8455. +
  8456. +static inline void local_bh_enable_ip(unsigned long ip)
  8457. +{
  8458. + __local_bh_enable();
  8459. +}
  8460. +
  8461. +#else
  8462. +
  8463. #ifdef CONFIG_TRACE_IRQFLAGS
  8464. extern void __local_bh_disable_ip(unsigned long ip, unsigned int cnt);
  8465. #else
  8466. @@ -31,5 +64,6 @@
  8467. {
  8468. __local_bh_enable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET);
  8469. }
  8470. +#endif
  8471. #endif /* _LINUX_BH_H */
  8472. diff -Nur linux-4.1.13.orig/include/linux/buffer_head.h linux-4.1.13/include/linux/buffer_head.h
  8473. --- linux-4.1.13.orig/include/linux/buffer_head.h 2015-11-09 23:34:10.000000000 +0100
  8474. +++ linux-4.1.13/include/linux/buffer_head.h 2015-11-29 09:23:09.601614714 +0100
  8475. @@ -75,8 +75,52 @@
  8476. struct address_space *b_assoc_map; /* mapping this buffer is
  8477. associated with */
  8478. atomic_t b_count; /* users using this buffer_head */
  8479. +#ifdef CONFIG_PREEMPT_RT_BASE
  8480. + spinlock_t b_uptodate_lock;
  8481. +#if defined(CONFIG_JBD) || defined(CONFIG_JBD_MODULE) || \
  8482. + defined(CONFIG_JBD2) || defined(CONFIG_JBD2_MODULE)
  8483. + spinlock_t b_state_lock;
  8484. + spinlock_t b_journal_head_lock;
  8485. +#endif
  8486. +#endif
  8487. };
  8488. +static inline unsigned long bh_uptodate_lock_irqsave(struct buffer_head *bh)
  8489. +{
  8490. + unsigned long flags;
  8491. +
  8492. +#ifndef CONFIG_PREEMPT_RT_BASE
  8493. + local_irq_save(flags);
  8494. + bit_spin_lock(BH_Uptodate_Lock, &bh->b_state);
  8495. +#else
  8496. + spin_lock_irqsave(&bh->b_uptodate_lock, flags);
  8497. +#endif
  8498. + return flags;
  8499. +}
  8500. +
  8501. +static inline void
  8502. +bh_uptodate_unlock_irqrestore(struct buffer_head *bh, unsigned long flags)
  8503. +{
  8504. +#ifndef CONFIG_PREEMPT_RT_BASE
  8505. + bit_spin_unlock(BH_Uptodate_Lock, &bh->b_state);
  8506. + local_irq_restore(flags);
  8507. +#else
  8508. + spin_unlock_irqrestore(&bh->b_uptodate_lock, flags);
  8509. +#endif
  8510. +}
  8511. +
  8512. +static inline void buffer_head_init_locks(struct buffer_head *bh)
  8513. +{
  8514. +#ifdef CONFIG_PREEMPT_RT_BASE
  8515. + spin_lock_init(&bh->b_uptodate_lock);
  8516. +#if defined(CONFIG_JBD) || defined(CONFIG_JBD_MODULE) || \
  8517. + defined(CONFIG_JBD2) || defined(CONFIG_JBD2_MODULE)
  8518. + spin_lock_init(&bh->b_state_lock);
  8519. + spin_lock_init(&bh->b_journal_head_lock);
  8520. +#endif
  8521. +#endif
  8522. +}
  8523. +
  8524. /*
  8525. * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
  8526. * and buffer_foo() functions.
  8527. diff -Nur linux-4.1.13.orig/include/linux/cgroup.h linux-4.1.13/include/linux/cgroup.h
  8528. --- linux-4.1.13.orig/include/linux/cgroup.h 2015-11-09 23:34:10.000000000 +0100
  8529. +++ linux-4.1.13/include/linux/cgroup.h 2015-11-29 09:23:09.601614714 +0100
  8530. @@ -22,6 +22,7 @@
  8531. #include <linux/seq_file.h>
  8532. #include <linux/kernfs.h>
  8533. #include <linux/wait.h>
  8534. +#include <linux/work-simple.h>
  8535. #ifdef CONFIG_CGROUPS
  8536. @@ -91,6 +92,7 @@
  8537. /* percpu_ref killing and RCU release */
  8538. struct rcu_head rcu_head;
  8539. struct work_struct destroy_work;
  8540. + struct swork_event destroy_swork;
  8541. };
  8542. /* bits in struct cgroup_subsys_state flags field */
  8543. diff -Nur linux-4.1.13.orig/include/linux/completion.h linux-4.1.13/include/linux/completion.h
  8544. --- linux-4.1.13.orig/include/linux/completion.h 2015-11-09 23:34:10.000000000 +0100
  8545. +++ linux-4.1.13/include/linux/completion.h 2015-11-29 09:23:09.601614714 +0100
  8546. @@ -7,8 +7,7 @@
  8547. * Atomic wait-for-completion handler data structures.
  8548. * See kernel/sched/completion.c for details.
  8549. */
  8550. -
  8551. -#include <linux/wait.h>
  8552. +#include <linux/wait-simple.h>
  8553. /*
  8554. * struct completion - structure used to maintain state for a "completion"
  8555. @@ -24,11 +23,11 @@
  8556. */
  8557. struct completion {
  8558. unsigned int done;
  8559. - wait_queue_head_t wait;
  8560. + struct swait_head wait;
  8561. };
  8562. #define COMPLETION_INITIALIZER(work) \
  8563. - { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  8564. + { 0, SWAIT_HEAD_INITIALIZER((work).wait) }
  8565. #define COMPLETION_INITIALIZER_ONSTACK(work) \
  8566. ({ init_completion(&work); work; })
  8567. @@ -73,7 +72,7 @@
  8568. static inline void init_completion(struct completion *x)
  8569. {
  8570. x->done = 0;
  8571. - init_waitqueue_head(&x->wait);
  8572. + init_swait_head(&x->wait);
  8573. }
  8574. /**
  8575. diff -Nur linux-4.1.13.orig/include/linux/cpu.h linux-4.1.13/include/linux/cpu.h
  8576. --- linux-4.1.13.orig/include/linux/cpu.h 2015-11-09 23:34:10.000000000 +0100
  8577. +++ linux-4.1.13/include/linux/cpu.h 2015-11-29 09:23:09.601614714 +0100
  8578. @@ -231,6 +231,8 @@
  8579. extern void put_online_cpus(void);
  8580. extern void cpu_hotplug_disable(void);
  8581. extern void cpu_hotplug_enable(void);
  8582. +extern void pin_current_cpu(void);
  8583. +extern void unpin_current_cpu(void);
  8584. #define hotcpu_notifier(fn, pri) cpu_notifier(fn, pri)
  8585. #define __hotcpu_notifier(fn, pri) __cpu_notifier(fn, pri)
  8586. #define register_hotcpu_notifier(nb) register_cpu_notifier(nb)
  8587. @@ -249,6 +251,8 @@
  8588. #define put_online_cpus() do { } while (0)
  8589. #define cpu_hotplug_disable() do { } while (0)
  8590. #define cpu_hotplug_enable() do { } while (0)
  8591. +static inline void pin_current_cpu(void) { }
  8592. +static inline void unpin_current_cpu(void) { }
  8593. #define hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  8594. #define __hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  8595. /* These aren't inline functions due to a GCC bug. */
  8596. diff -Nur linux-4.1.13.orig/include/linux/delay.h linux-4.1.13/include/linux/delay.h
  8597. --- linux-4.1.13.orig/include/linux/delay.h 2015-11-09 23:34:10.000000000 +0100
  8598. +++ linux-4.1.13/include/linux/delay.h 2015-11-29 09:23:09.601614714 +0100
  8599. @@ -52,4 +52,10 @@
  8600. msleep(seconds * 1000);
  8601. }
  8602. +#ifdef CONFIG_PREEMPT_RT_FULL
  8603. +extern void cpu_chill(void);
  8604. +#else
  8605. +# define cpu_chill() cpu_relax()
  8606. +#endif
  8607. +
  8608. #endif /* defined(_LINUX_DELAY_H) */
  8609. diff -Nur linux-4.1.13.orig/include/linux/ftrace_event.h linux-4.1.13/include/linux/ftrace_event.h
  8610. --- linux-4.1.13.orig/include/linux/ftrace_event.h 2015-11-09 23:34:10.000000000 +0100
  8611. +++ linux-4.1.13/include/linux/ftrace_event.h 2015-11-29 09:23:09.601614714 +0100
  8612. @@ -66,6 +66,9 @@
  8613. unsigned char flags;
  8614. unsigned char preempt_count;
  8615. int pid;
  8616. + unsigned short migrate_disable;
  8617. + unsigned short padding;
  8618. + unsigned char preempt_lazy_count;
  8619. };
  8620. #define FTRACE_MAX_EVENT \
  8621. diff -Nur linux-4.1.13.orig/include/linux/highmem.h linux-4.1.13/include/linux/highmem.h
  8622. --- linux-4.1.13.orig/include/linux/highmem.h 2015-11-09 23:34:10.000000000 +0100
  8623. +++ linux-4.1.13/include/linux/highmem.h 2015-11-29 09:23:09.601614714 +0100
  8624. @@ -7,6 +7,7 @@
  8625. #include <linux/mm.h>
  8626. #include <linux/uaccess.h>
  8627. #include <linux/hardirq.h>
  8628. +#include <linux/sched.h>
  8629. #include <asm/cacheflush.h>
  8630. @@ -65,6 +66,7 @@
  8631. static inline void *kmap_atomic(struct page *page)
  8632. {
  8633. + preempt_disable_nort();
  8634. pagefault_disable();
  8635. return page_address(page);
  8636. }
  8637. @@ -73,6 +75,7 @@
  8638. static inline void __kunmap_atomic(void *addr)
  8639. {
  8640. pagefault_enable();
  8641. + preempt_enable_nort();
  8642. }
  8643. #define kmap_atomic_pfn(pfn) kmap_atomic(pfn_to_page(pfn))
  8644. @@ -85,32 +88,51 @@
  8645. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  8646. +#ifndef CONFIG_PREEMPT_RT_FULL
  8647. DECLARE_PER_CPU(int, __kmap_atomic_idx);
  8648. +#endif
  8649. static inline int kmap_atomic_idx_push(void)
  8650. {
  8651. +#ifndef CONFIG_PREEMPT_RT_FULL
  8652. int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;
  8653. -#ifdef CONFIG_DEBUG_HIGHMEM
  8654. +# ifdef CONFIG_DEBUG_HIGHMEM
  8655. WARN_ON_ONCE(in_irq() && !irqs_disabled());
  8656. BUG_ON(idx >= KM_TYPE_NR);
  8657. -#endif
  8658. +# endif
  8659. return idx;
  8660. +#else
  8661. + current->kmap_idx++;
  8662. + BUG_ON(current->kmap_idx > KM_TYPE_NR);
  8663. + return current->kmap_idx - 1;
  8664. +#endif
  8665. }
  8666. static inline int kmap_atomic_idx(void)
  8667. {
  8668. +#ifndef CONFIG_PREEMPT_RT_FULL
  8669. return __this_cpu_read(__kmap_atomic_idx) - 1;
  8670. +#else
  8671. + return current->kmap_idx - 1;
  8672. +#endif
  8673. }
  8674. static inline void kmap_atomic_idx_pop(void)
  8675. {
  8676. -#ifdef CONFIG_DEBUG_HIGHMEM
  8677. +#ifndef CONFIG_PREEMPT_RT_FULL
  8678. +# ifdef CONFIG_DEBUG_HIGHMEM
  8679. int idx = __this_cpu_dec_return(__kmap_atomic_idx);
  8680. BUG_ON(idx < 0);
  8681. -#else
  8682. +# else
  8683. __this_cpu_dec(__kmap_atomic_idx);
  8684. +# endif
  8685. +#else
  8686. + current->kmap_idx--;
  8687. +# ifdef CONFIG_DEBUG_HIGHMEM
  8688. + BUG_ON(current->kmap_idx < 0);
  8689. +# endif
  8690. #endif
  8691. }
  8692. diff -Nur linux-4.1.13.orig/include/linux/hrtimer.h linux-4.1.13/include/linux/hrtimer.h
  8693. --- linux-4.1.13.orig/include/linux/hrtimer.h 2015-11-09 23:34:10.000000000 +0100
  8694. +++ linux-4.1.13/include/linux/hrtimer.h 2015-11-29 09:23:09.601614714 +0100
  8695. @@ -111,6 +111,11 @@
  8696. enum hrtimer_restart (*function)(struct hrtimer *);
  8697. struct hrtimer_clock_base *base;
  8698. unsigned long state;
  8699. + struct list_head cb_entry;
  8700. + int irqsafe;
  8701. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  8702. + ktime_t praecox;
  8703. +#endif
  8704. #ifdef CONFIG_TIMER_STATS
  8705. int start_pid;
  8706. void *start_site;
  8707. @@ -147,6 +152,7 @@
  8708. int index;
  8709. clockid_t clockid;
  8710. struct timerqueue_head active;
  8711. + struct list_head expired;
  8712. ktime_t resolution;
  8713. ktime_t (*get_time)(void);
  8714. ktime_t softirq_time;
  8715. @@ -194,6 +200,9 @@
  8716. unsigned long nr_hangs;
  8717. ktime_t max_hang_time;
  8718. #endif
  8719. +#ifdef CONFIG_PREEMPT_RT_BASE
  8720. + wait_queue_head_t wait;
  8721. +#endif
  8722. struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
  8723. };
  8724. @@ -381,6 +390,13 @@
  8725. return hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  8726. }
  8727. +/* Softirq preemption could deadlock timer removal */
  8728. +#ifdef CONFIG_PREEMPT_RT_BASE
  8729. + extern void hrtimer_wait_for_timer(const struct hrtimer *timer);
  8730. +#else
  8731. +# define hrtimer_wait_for_timer(timer) do { cpu_relax(); } while (0)
  8732. +#endif
  8733. +
  8734. /* Query timers: */
  8735. extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
  8736. extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
  8737. diff -Nur linux-4.1.13.orig/include/linux/idr.h linux-4.1.13/include/linux/idr.h
  8738. --- linux-4.1.13.orig/include/linux/idr.h 2015-11-09 23:34:10.000000000 +0100
  8739. +++ linux-4.1.13/include/linux/idr.h 2015-11-29 09:23:09.601614714 +0100
  8740. @@ -95,10 +95,14 @@
  8741. * Each idr_preload() should be matched with an invocation of this
  8742. * function. See idr_preload() for details.
  8743. */
  8744. +#ifdef CONFIG_PREEMPT_RT_FULL
  8745. +void idr_preload_end(void);
  8746. +#else
  8747. static inline void idr_preload_end(void)
  8748. {
  8749. preempt_enable();
  8750. }
  8751. +#endif
  8752. /**
  8753. * idr_find - return pointer for given id
  8754. diff -Nur linux-4.1.13.orig/include/linux/init_task.h linux-4.1.13/include/linux/init_task.h
  8755. --- linux-4.1.13.orig/include/linux/init_task.h 2015-11-09 23:34:10.000000000 +0100
  8756. +++ linux-4.1.13/include/linux/init_task.h 2015-11-29 09:23:09.601614714 +0100
  8757. @@ -147,9 +147,16 @@
  8758. # define INIT_PERF_EVENTS(tsk)
  8759. #endif
  8760. +#ifdef CONFIG_PREEMPT_RT_BASE
  8761. +# define INIT_TIMER_LIST .posix_timer_list = NULL,
  8762. +#else
  8763. +# define INIT_TIMER_LIST
  8764. +#endif
  8765. +
  8766. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  8767. # define INIT_VTIME(tsk) \
  8768. - .vtime_seqlock = __SEQLOCK_UNLOCKED(tsk.vtime_seqlock), \
  8769. + .vtime_lock = __RAW_SPIN_LOCK_UNLOCKED(tsk.vtime_lock), \
  8770. + .vtime_seq = SEQCNT_ZERO(tsk.vtime_seq), \
  8771. .vtime_snap = 0, \
  8772. .vtime_snap_whence = VTIME_SYS,
  8773. #else
  8774. @@ -238,6 +245,7 @@
  8775. .cpu_timers = INIT_CPU_TIMERS(tsk.cpu_timers), \
  8776. .pi_lock = __RAW_SPIN_LOCK_UNLOCKED(tsk.pi_lock), \
  8777. .timer_slack_ns = 50000, /* 50 usec default slack */ \
  8778. + INIT_TIMER_LIST \
  8779. .pids = { \
  8780. [PIDTYPE_PID] = INIT_PID_LINK(PIDTYPE_PID), \
  8781. [PIDTYPE_PGID] = INIT_PID_LINK(PIDTYPE_PGID), \
  8782. diff -Nur linux-4.1.13.orig/include/linux/interrupt.h linux-4.1.13/include/linux/interrupt.h
  8783. --- linux-4.1.13.orig/include/linux/interrupt.h 2015-11-09 23:34:10.000000000 +0100
  8784. +++ linux-4.1.13/include/linux/interrupt.h 2015-11-29 09:23:09.601614714 +0100
  8785. @@ -61,6 +61,7 @@
  8786. * interrupt handler after suspending interrupts. For system
  8787. * wakeup devices users need to implement wakeup detection in
  8788. * their interrupt handlers.
  8789. + * IRQF_NO_SOFTIRQ_CALL - Do not process softirqs in the irq thread context (RT)
  8790. */
  8791. #define IRQF_SHARED 0x00000080
  8792. #define IRQF_PROBE_SHARED 0x00000100
  8793. @@ -74,6 +75,7 @@
  8794. #define IRQF_NO_THREAD 0x00010000
  8795. #define IRQF_EARLY_RESUME 0x00020000
  8796. #define IRQF_COND_SUSPEND 0x00040000
  8797. +#define IRQF_NO_SOFTIRQ_CALL 0x00080000
  8798. #define IRQF_TIMER (__IRQF_TIMER | IRQF_NO_SUSPEND | IRQF_NO_THREAD)
  8799. @@ -102,6 +104,7 @@
  8800. * @flags: flags (see IRQF_* above)
  8801. * @thread_fn: interrupt handler function for threaded interrupts
  8802. * @thread: thread pointer for threaded interrupts
  8803. + * @secondary: pointer to secondary irqaction (force threading)
  8804. * @thread_flags: flags related to @thread
  8805. * @thread_mask: bitmask for keeping track of @thread activity
  8806. * @dir: pointer to the proc/irq/NN/name entry
  8807. @@ -113,6 +116,7 @@
  8808. struct irqaction *next;
  8809. irq_handler_t thread_fn;
  8810. struct task_struct *thread;
  8811. + struct irqaction *secondary;
  8812. unsigned int irq;
  8813. unsigned int flags;
  8814. unsigned long thread_flags;
  8815. @@ -184,7 +188,7 @@
  8816. #ifdef CONFIG_LOCKDEP
  8817. # define local_irq_enable_in_hardirq() do { } while (0)
  8818. #else
  8819. -# define local_irq_enable_in_hardirq() local_irq_enable()
  8820. +# define local_irq_enable_in_hardirq() local_irq_enable_nort()
  8821. #endif
  8822. extern void disable_irq_nosync(unsigned int irq);
  8823. @@ -215,6 +219,7 @@
  8824. unsigned int irq;
  8825. struct kref kref;
  8826. struct work_struct work;
  8827. + struct list_head list;
  8828. void (*notify)(struct irq_affinity_notify *, const cpumask_t *mask);
  8829. void (*release)(struct kref *ref);
  8830. };
  8831. @@ -377,9 +382,13 @@
  8832. bool state);
  8833. #ifdef CONFIG_IRQ_FORCED_THREADING
  8834. +# ifndef CONFIG_PREEMPT_RT_BASE
  8835. extern bool force_irqthreads;
  8836. +# else
  8837. +# define force_irqthreads (true)
  8838. +# endif
  8839. #else
  8840. -#define force_irqthreads (0)
  8841. +#define force_irqthreads (false)
  8842. #endif
  8843. #ifndef __ARCH_SET_SOFTIRQ_PENDING
  8844. @@ -435,9 +444,10 @@
  8845. void (*action)(struct softirq_action *);
  8846. };
  8847. +#ifndef CONFIG_PREEMPT_RT_FULL
  8848. asmlinkage void do_softirq(void);
  8849. asmlinkage void __do_softirq(void);
  8850. -
  8851. +static inline void thread_do_softirq(void) { do_softirq(); }
  8852. #ifdef __ARCH_HAS_DO_SOFTIRQ
  8853. void do_softirq_own_stack(void);
  8854. #else
  8855. @@ -446,6 +456,9 @@
  8856. __do_softirq();
  8857. }
  8858. #endif
  8859. +#else
  8860. +extern void thread_do_softirq(void);
  8861. +#endif
  8862. extern void open_softirq(int nr, void (*action)(struct softirq_action *));
  8863. extern void softirq_init(void);
  8864. @@ -453,6 +466,7 @@
  8865. extern void raise_softirq_irqoff(unsigned int nr);
  8866. extern void raise_softirq(unsigned int nr);
  8867. +extern void softirq_check_pending_idle(void);
  8868. DECLARE_PER_CPU(struct task_struct *, ksoftirqd);
  8869. @@ -474,8 +488,9 @@
  8870. to be executed on some cpu at least once after this.
  8871. * If the tasklet is already scheduled, but its execution is still not
  8872. started, it will be executed only once.
  8873. - * If this tasklet is already running on another CPU (or schedule is called
  8874. - from tasklet itself), it is rescheduled for later.
  8875. + * If this tasklet is already running on another CPU, it is rescheduled
  8876. + for later.
  8877. + * Schedule must not be called from the tasklet itself (a lockup occurs)
  8878. * Tasklet is strictly serialized wrt itself, but not
  8879. wrt another tasklets. If client needs some intertask synchronization,
  8880. he makes it with spinlocks.
  8881. @@ -500,27 +515,36 @@
  8882. enum
  8883. {
  8884. TASKLET_STATE_SCHED, /* Tasklet is scheduled for execution */
  8885. - TASKLET_STATE_RUN /* Tasklet is running (SMP only) */
  8886. + TASKLET_STATE_RUN, /* Tasklet is running (SMP only) */
  8887. + TASKLET_STATE_PENDING /* Tasklet is pending */
  8888. };
  8889. -#ifdef CONFIG_SMP
  8890. +#define TASKLET_STATEF_SCHED (1 << TASKLET_STATE_SCHED)
  8891. +#define TASKLET_STATEF_RUN (1 << TASKLET_STATE_RUN)
  8892. +#define TASKLET_STATEF_PENDING (1 << TASKLET_STATE_PENDING)
  8893. +
  8894. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  8895. static inline int tasklet_trylock(struct tasklet_struct *t)
  8896. {
  8897. return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
  8898. }
  8899. +static inline int tasklet_tryunlock(struct tasklet_struct *t)
  8900. +{
  8901. + return cmpxchg(&t->state, TASKLET_STATEF_RUN, 0) == TASKLET_STATEF_RUN;
  8902. +}
  8903. +
  8904. static inline void tasklet_unlock(struct tasklet_struct *t)
  8905. {
  8906. smp_mb__before_atomic();
  8907. clear_bit(TASKLET_STATE_RUN, &(t)->state);
  8908. }
  8909. -static inline void tasklet_unlock_wait(struct tasklet_struct *t)
  8910. -{
  8911. - while (test_bit(TASKLET_STATE_RUN, &(t)->state)) { barrier(); }
  8912. -}
  8913. +extern void tasklet_unlock_wait(struct tasklet_struct *t);
  8914. +
  8915. #else
  8916. #define tasklet_trylock(t) 1
  8917. +#define tasklet_tryunlock(t) 1
  8918. #define tasklet_unlock_wait(t) do { } while (0)
  8919. #define tasklet_unlock(t) do { } while (0)
  8920. #endif
  8921. @@ -569,12 +593,7 @@
  8922. smp_mb();
  8923. }
  8924. -static inline void tasklet_enable(struct tasklet_struct *t)
  8925. -{
  8926. - smp_mb__before_atomic();
  8927. - atomic_dec(&t->count);
  8928. -}
  8929. -
  8930. +extern void tasklet_enable(struct tasklet_struct *t);
  8931. extern void tasklet_kill(struct tasklet_struct *t);
  8932. extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
  8933. extern void tasklet_init(struct tasklet_struct *t,
  8934. @@ -605,6 +624,12 @@
  8935. tasklet_kill(&ttimer->tasklet);
  8936. }
  8937. +#ifdef CONFIG_PREEMPT_RT_FULL
  8938. +extern void softirq_early_init(void);
  8939. +#else
  8940. +static inline void softirq_early_init(void) { }
  8941. +#endif
  8942. +
  8943. /*
  8944. * Autoprobing for irqs:
  8945. *
  8946. diff -Nur linux-4.1.13.orig/include/linux/io-mapping.h linux-4.1.13/include/linux/io-mapping.h
  8947. --- linux-4.1.13.orig/include/linux/io-mapping.h 2015-11-09 23:34:10.000000000 +0100
  8948. +++ linux-4.1.13/include/linux/io-mapping.h 2015-11-29 09:23:09.601614714 +0100
  8949. @@ -141,6 +141,7 @@
  8950. io_mapping_map_atomic_wc(struct io_mapping *mapping,
  8951. unsigned long offset)
  8952. {
  8953. + preempt_disable();
  8954. pagefault_disable();
  8955. return ((char __force __iomem *) mapping) + offset;
  8956. }
  8957. @@ -149,6 +150,7 @@
  8958. io_mapping_unmap_atomic(void __iomem *vaddr)
  8959. {
  8960. pagefault_enable();
  8961. + preempt_enable();
  8962. }
  8963. /* Non-atomic map/unmap */
  8964. diff -Nur linux-4.1.13.orig/include/linux/irqdesc.h linux-4.1.13/include/linux/irqdesc.h
  8965. --- linux-4.1.13.orig/include/linux/irqdesc.h 2015-11-09 23:34:10.000000000 +0100
  8966. +++ linux-4.1.13/include/linux/irqdesc.h 2015-11-29 09:23:09.601614714 +0100
  8967. @@ -63,6 +63,7 @@
  8968. unsigned int irqs_unhandled;
  8969. atomic_t threads_handled;
  8970. int threads_handled_last;
  8971. + u64 random_ip;
  8972. raw_spinlock_t lock;
  8973. struct cpumask *percpu_enabled;
  8974. #ifdef CONFIG_SMP
  8975. diff -Nur linux-4.1.13.orig/include/linux/irqflags.h linux-4.1.13/include/linux/irqflags.h
  8976. --- linux-4.1.13.orig/include/linux/irqflags.h 2015-11-09 23:34:10.000000000 +0100
  8977. +++ linux-4.1.13/include/linux/irqflags.h 2015-11-29 09:23:09.601614714 +0100
  8978. @@ -25,8 +25,6 @@
  8979. # define trace_softirqs_enabled(p) ((p)->softirqs_enabled)
  8980. # define trace_hardirq_enter() do { current->hardirq_context++; } while (0)
  8981. # define trace_hardirq_exit() do { current->hardirq_context--; } while (0)
  8982. -# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  8983. -# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  8984. # define INIT_TRACE_IRQFLAGS .softirqs_enabled = 1,
  8985. #else
  8986. # define trace_hardirqs_on() do { } while (0)
  8987. @@ -39,9 +37,15 @@
  8988. # define trace_softirqs_enabled(p) 0
  8989. # define trace_hardirq_enter() do { } while (0)
  8990. # define trace_hardirq_exit() do { } while (0)
  8991. +# define INIT_TRACE_IRQFLAGS
  8992. +#endif
  8993. +
  8994. +#if defined(CONFIG_TRACE_IRQFLAGS) && !defined(CONFIG_PREEMPT_RT_FULL)
  8995. +# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  8996. +# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  8997. +#else
  8998. # define lockdep_softirq_enter() do { } while (0)
  8999. # define lockdep_softirq_exit() do { } while (0)
  9000. -# define INIT_TRACE_IRQFLAGS
  9001. #endif
  9002. #if defined(CONFIG_IRQSOFF_TRACER) || \
  9003. @@ -148,4 +152,23 @@
  9004. #define irqs_disabled_flags(flags) raw_irqs_disabled_flags(flags)
  9005. +/*
  9006. + * local_irq* variants depending on RT/!RT
  9007. + */
  9008. +#ifdef CONFIG_PREEMPT_RT_FULL
  9009. +# define local_irq_disable_nort() do { } while (0)
  9010. +# define local_irq_enable_nort() do { } while (0)
  9011. +# define local_irq_save_nort(flags) local_save_flags(flags)
  9012. +# define local_irq_restore_nort(flags) (void)(flags)
  9013. +# define local_irq_disable_rt() local_irq_disable()
  9014. +# define local_irq_enable_rt() local_irq_enable()
  9015. +#else
  9016. +# define local_irq_disable_nort() local_irq_disable()
  9017. +# define local_irq_enable_nort() local_irq_enable()
  9018. +# define local_irq_save_nort(flags) local_irq_save(flags)
  9019. +# define local_irq_restore_nort(flags) local_irq_restore(flags)
  9020. +# define local_irq_disable_rt() do { } while (0)
  9021. +# define local_irq_enable_rt() do { } while (0)
  9022. +#endif
  9023. +
  9024. #endif
  9025. diff -Nur linux-4.1.13.orig/include/linux/irq.h linux-4.1.13/include/linux/irq.h
  9026. --- linux-4.1.13.orig/include/linux/irq.h 2015-11-09 23:34:10.000000000 +0100
  9027. +++ linux-4.1.13/include/linux/irq.h 2015-11-29 09:23:09.601614714 +0100
  9028. @@ -72,6 +72,7 @@
  9029. * IRQ_IS_POLLED - Always polled by another interrupt. Exclude
  9030. * it from the spurious interrupt detection
  9031. * mechanism and from core side polling.
  9032. + * IRQ_NO_SOFTIRQ_CALL - No softirq processing in the irq thread context (RT)
  9033. */
  9034. enum {
  9035. IRQ_TYPE_NONE = 0x00000000,
  9036. @@ -97,13 +98,14 @@
  9037. IRQ_NOTHREAD = (1 << 16),
  9038. IRQ_PER_CPU_DEVID = (1 << 17),
  9039. IRQ_IS_POLLED = (1 << 18),
  9040. + IRQ_NO_SOFTIRQ_CALL = (1 << 19),
  9041. };
  9042. #define IRQF_MODIFY_MASK \
  9043. (IRQ_TYPE_SENSE_MASK | IRQ_NOPROBE | IRQ_NOREQUEST | \
  9044. IRQ_NOAUTOEN | IRQ_MOVE_PCNTXT | IRQ_LEVEL | IRQ_NO_BALANCING | \
  9045. IRQ_PER_CPU | IRQ_NESTED_THREAD | IRQ_NOTHREAD | IRQ_PER_CPU_DEVID | \
  9046. - IRQ_IS_POLLED)
  9047. + IRQ_IS_POLLED | IRQ_NO_SOFTIRQ_CALL)
  9048. #define IRQ_NO_BALANCING_MASK (IRQ_PER_CPU | IRQ_NO_BALANCING)
  9049. diff -Nur linux-4.1.13.orig/include/linux/irq_work.h linux-4.1.13/include/linux/irq_work.h
  9050. --- linux-4.1.13.orig/include/linux/irq_work.h 2015-11-09 23:34:10.000000000 +0100
  9051. +++ linux-4.1.13/include/linux/irq_work.h 2015-11-29 09:23:09.601614714 +0100
  9052. @@ -16,6 +16,7 @@
  9053. #define IRQ_WORK_BUSY 2UL
  9054. #define IRQ_WORK_FLAGS 3UL
  9055. #define IRQ_WORK_LAZY 4UL /* Doesn't want IPI, wait for tick */
  9056. +#define IRQ_WORK_HARD_IRQ 8UL /* Run hard IRQ context, even on RT */
  9057. struct irq_work {
  9058. unsigned long flags;
  9059. @@ -51,4 +52,10 @@
  9060. static inline void irq_work_run(void) { }
  9061. #endif
  9062. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  9063. +void irq_work_tick_soft(void);
  9064. +#else
  9065. +static inline void irq_work_tick_soft(void) { }
  9066. +#endif
  9067. +
  9068. #endif /* _LINUX_IRQ_WORK_H */
  9069. diff -Nur linux-4.1.13.orig/include/linux/jbd_common.h linux-4.1.13/include/linux/jbd_common.h
  9070. --- linux-4.1.13.orig/include/linux/jbd_common.h 2015-11-09 23:34:10.000000000 +0100
  9071. +++ linux-4.1.13/include/linux/jbd_common.h 2015-11-29 09:23:09.601614714 +0100
  9072. @@ -15,32 +15,56 @@
  9073. static inline void jbd_lock_bh_state(struct buffer_head *bh)
  9074. {
  9075. +#ifndef CONFIG_PREEMPT_RT_BASE
  9076. bit_spin_lock(BH_State, &bh->b_state);
  9077. +#else
  9078. + spin_lock(&bh->b_state_lock);
  9079. +#endif
  9080. }
  9081. static inline int jbd_trylock_bh_state(struct buffer_head *bh)
  9082. {
  9083. +#ifndef CONFIG_PREEMPT_RT_BASE
  9084. return bit_spin_trylock(BH_State, &bh->b_state);
  9085. +#else
  9086. + return spin_trylock(&bh->b_state_lock);
  9087. +#endif
  9088. }
  9089. static inline int jbd_is_locked_bh_state(struct buffer_head *bh)
  9090. {
  9091. +#ifndef CONFIG_PREEMPT_RT_BASE
  9092. return bit_spin_is_locked(BH_State, &bh->b_state);
  9093. +#else
  9094. + return spin_is_locked(&bh->b_state_lock);
  9095. +#endif
  9096. }
  9097. static inline void jbd_unlock_bh_state(struct buffer_head *bh)
  9098. {
  9099. +#ifndef CONFIG_PREEMPT_RT_BASE
  9100. bit_spin_unlock(BH_State, &bh->b_state);
  9101. +#else
  9102. + spin_unlock(&bh->b_state_lock);
  9103. +#endif
  9104. }
  9105. static inline void jbd_lock_bh_journal_head(struct buffer_head *bh)
  9106. {
  9107. +#ifndef CONFIG_PREEMPT_RT_BASE
  9108. bit_spin_lock(BH_JournalHead, &bh->b_state);
  9109. +#else
  9110. + spin_lock(&bh->b_journal_head_lock);
  9111. +#endif
  9112. }
  9113. static inline void jbd_unlock_bh_journal_head(struct buffer_head *bh)
  9114. {
  9115. +#ifndef CONFIG_PREEMPT_RT_BASE
  9116. bit_spin_unlock(BH_JournalHead, &bh->b_state);
  9117. +#else
  9118. + spin_unlock(&bh->b_journal_head_lock);
  9119. +#endif
  9120. }
  9121. #endif
  9122. diff -Nur linux-4.1.13.orig/include/linux/kdb.h linux-4.1.13/include/linux/kdb.h
  9123. --- linux-4.1.13.orig/include/linux/kdb.h 2015-11-09 23:34:10.000000000 +0100
  9124. +++ linux-4.1.13/include/linux/kdb.h 2015-11-29 09:23:09.601614714 +0100
  9125. @@ -167,6 +167,7 @@
  9126. extern __printf(1, 2) int kdb_printf(const char *, ...);
  9127. typedef __printf(1, 2) int (*kdb_printf_t)(const char *, ...);
  9128. +#define in_kdb_printk() (kdb_trap_printk)
  9129. extern void kdb_init(int level);
  9130. /* Access to kdb specific polling devices */
  9131. @@ -201,6 +202,7 @@
  9132. extern int kdb_unregister(char *);
  9133. #else /* ! CONFIG_KGDB_KDB */
  9134. static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }
  9135. +#define in_kdb_printk() (0)
  9136. static inline void kdb_init(int level) {}
  9137. static inline int kdb_register(char *cmd, kdb_func_t func, char *usage,
  9138. char *help, short minlen) { return 0; }
  9139. diff -Nur linux-4.1.13.orig/include/linux/kernel.h linux-4.1.13/include/linux/kernel.h
  9140. --- linux-4.1.13.orig/include/linux/kernel.h 2015-11-09 23:34:10.000000000 +0100
  9141. +++ linux-4.1.13/include/linux/kernel.h 2015-11-29 09:23:09.601614714 +0100
  9142. @@ -188,6 +188,9 @@
  9143. */
  9144. # define might_sleep() \
  9145. do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  9146. +
  9147. +# define might_sleep_no_state_check() \
  9148. + do { ___might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  9149. # define sched_annotate_sleep() (current->task_state_change = 0)
  9150. #else
  9151. static inline void ___might_sleep(const char *file, int line,
  9152. @@ -195,6 +198,7 @@
  9153. static inline void __might_sleep(const char *file, int line,
  9154. int preempt_offset) { }
  9155. # define might_sleep() do { might_resched(); } while (0)
  9156. +# define might_sleep_no_state_check() do { might_resched(); } while (0)
  9157. # define sched_annotate_sleep() do { } while (0)
  9158. #endif
  9159. @@ -244,7 +248,8 @@
  9160. #if defined(CONFIG_MMU) && \
  9161. (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
  9162. -void might_fault(void);
  9163. +#define might_fault() __might_fault(__FILE__, __LINE__)
  9164. +void __might_fault(const char *file, int line);
  9165. #else
  9166. static inline void might_fault(void) { }
  9167. #endif
  9168. @@ -466,6 +471,7 @@
  9169. SYSTEM_HALT,
  9170. SYSTEM_POWER_OFF,
  9171. SYSTEM_RESTART,
  9172. + SYSTEM_SUSPEND,
  9173. } system_state;
  9174. #define TAINT_PROPRIETARY_MODULE 0
  9175. diff -Nur linux-4.1.13.orig/include/linux/kvm_host.h linux-4.1.13/include/linux/kvm_host.h
  9176. --- linux-4.1.13.orig/include/linux/kvm_host.h 2015-11-09 23:34:10.000000000 +0100
  9177. +++ linux-4.1.13/include/linux/kvm_host.h 2015-11-29 09:23:09.601614714 +0100
  9178. @@ -230,7 +230,7 @@
  9179. int fpu_active;
  9180. int guest_fpu_loaded, guest_xcr0_loaded;
  9181. - wait_queue_head_t wq;
  9182. + struct swait_head wq;
  9183. struct pid *pid;
  9184. int sigset_active;
  9185. sigset_t sigset;
  9186. @@ -690,7 +690,7 @@
  9187. }
  9188. #endif
  9189. -static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  9190. +static inline struct swait_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  9191. {
  9192. #ifdef __KVM_HAVE_ARCH_WQP
  9193. return vcpu->arch.wqp;
  9194. diff -Nur linux-4.1.13.orig/include/linux/lglock.h linux-4.1.13/include/linux/lglock.h
  9195. --- linux-4.1.13.orig/include/linux/lglock.h 2015-11-09 23:34:10.000000000 +0100
  9196. +++ linux-4.1.13/include/linux/lglock.h 2015-11-29 09:23:09.601614714 +0100
  9197. @@ -34,22 +34,39 @@
  9198. #endif
  9199. struct lglock {
  9200. +#ifndef CONFIG_PREEMPT_RT_FULL
  9201. arch_spinlock_t __percpu *lock;
  9202. +#else
  9203. + struct rt_mutex __percpu *lock;
  9204. +#endif
  9205. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  9206. struct lock_class_key lock_key;
  9207. struct lockdep_map lock_dep_map;
  9208. #endif
  9209. };
  9210. -#define DEFINE_LGLOCK(name) \
  9211. +#ifndef CONFIG_PREEMPT_RT_FULL
  9212. +# define DEFINE_LGLOCK(name) \
  9213. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  9214. = __ARCH_SPIN_LOCK_UNLOCKED; \
  9215. struct lglock name = { .lock = &name ## _lock }
  9216. -#define DEFINE_STATIC_LGLOCK(name) \
  9217. +# define DEFINE_STATIC_LGLOCK(name) \
  9218. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  9219. = __ARCH_SPIN_LOCK_UNLOCKED; \
  9220. static struct lglock name = { .lock = &name ## _lock }
  9221. +#else
  9222. +
  9223. +# define DEFINE_LGLOCK(name) \
  9224. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  9225. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  9226. + struct lglock name = { .lock = &name ## _lock }
  9227. +
  9228. +# define DEFINE_STATIC_LGLOCK(name) \
  9229. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  9230. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  9231. + static struct lglock name = { .lock = &name ## _lock }
  9232. +#endif
  9233. void lg_lock_init(struct lglock *lg, char *name);
  9234. void lg_local_lock(struct lglock *lg);
  9235. @@ -59,6 +76,12 @@
  9236. void lg_global_lock(struct lglock *lg);
  9237. void lg_global_unlock(struct lglock *lg);
  9238. +#ifndef CONFIG_PREEMPT_RT_FULL
  9239. +#define lg_global_trylock_relax(name) lg_global_lock(name)
  9240. +#else
  9241. +void lg_global_trylock_relax(struct lglock *lg);
  9242. +#endif
  9243. +
  9244. #else
  9245. /* When !CONFIG_SMP, map lglock to spinlock */
  9246. #define lglock spinlock
  9247. diff -Nur linux-4.1.13.orig/include/linux/list_bl.h linux-4.1.13/include/linux/list_bl.h
  9248. --- linux-4.1.13.orig/include/linux/list_bl.h 2015-11-09 23:34:10.000000000 +0100
  9249. +++ linux-4.1.13/include/linux/list_bl.h 2015-11-29 09:23:09.601614714 +0100
  9250. @@ -2,6 +2,7 @@
  9251. #define _LINUX_LIST_BL_H
  9252. #include <linux/list.h>
  9253. +#include <linux/spinlock.h>
  9254. #include <linux/bit_spinlock.h>
  9255. /*
  9256. @@ -32,13 +33,22 @@
  9257. struct hlist_bl_head {
  9258. struct hlist_bl_node *first;
  9259. +#ifdef CONFIG_PREEMPT_RT_BASE
  9260. + raw_spinlock_t lock;
  9261. +#endif
  9262. };
  9263. struct hlist_bl_node {
  9264. struct hlist_bl_node *next, **pprev;
  9265. };
  9266. -#define INIT_HLIST_BL_HEAD(ptr) \
  9267. - ((ptr)->first = NULL)
  9268. +
  9269. +static inline void INIT_HLIST_BL_HEAD(struct hlist_bl_head *h)
  9270. +{
  9271. + h->first = NULL;
  9272. +#ifdef CONFIG_PREEMPT_RT_BASE
  9273. + raw_spin_lock_init(&h->lock);
  9274. +#endif
  9275. +}
  9276. static inline void INIT_HLIST_BL_NODE(struct hlist_bl_node *h)
  9277. {
  9278. @@ -117,12 +127,26 @@
  9279. static inline void hlist_bl_lock(struct hlist_bl_head *b)
  9280. {
  9281. +#ifndef CONFIG_PREEMPT_RT_BASE
  9282. bit_spin_lock(0, (unsigned long *)b);
  9283. +#else
  9284. + raw_spin_lock(&b->lock);
  9285. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  9286. + __set_bit(0, (unsigned long *)b);
  9287. +#endif
  9288. +#endif
  9289. }
  9290. static inline void hlist_bl_unlock(struct hlist_bl_head *b)
  9291. {
  9292. +#ifndef CONFIG_PREEMPT_RT_BASE
  9293. __bit_spin_unlock(0, (unsigned long *)b);
  9294. +#else
  9295. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  9296. + __clear_bit(0, (unsigned long *)b);
  9297. +#endif
  9298. + raw_spin_unlock(&b->lock);
  9299. +#endif
  9300. }
  9301. static inline bool hlist_bl_is_locked(struct hlist_bl_head *b)
  9302. diff -Nur linux-4.1.13.orig/include/linux/locallock.h linux-4.1.13/include/linux/locallock.h
  9303. --- linux-4.1.13.orig/include/linux/locallock.h 1970-01-01 01:00:00.000000000 +0100
  9304. +++ linux-4.1.13/include/linux/locallock.h 2015-11-29 09:23:09.605614450 +0100
  9305. @@ -0,0 +1,270 @@
  9306. +#ifndef _LINUX_LOCALLOCK_H
  9307. +#define _LINUX_LOCALLOCK_H
  9308. +
  9309. +#include <linux/percpu.h>
  9310. +#include <linux/spinlock.h>
  9311. +
  9312. +#ifdef CONFIG_PREEMPT_RT_BASE
  9313. +
  9314. +#ifdef CONFIG_DEBUG_SPINLOCK
  9315. +# define LL_WARN(cond) WARN_ON(cond)
  9316. +#else
  9317. +# define LL_WARN(cond) do { } while (0)
  9318. +#endif
  9319. +
  9320. +/*
  9321. + * per cpu lock based substitute for local_irq_*()
  9322. + */
  9323. +struct local_irq_lock {
  9324. + spinlock_t lock;
  9325. + struct task_struct *owner;
  9326. + int nestcnt;
  9327. + unsigned long flags;
  9328. +};
  9329. +
  9330. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) \
  9331. + DEFINE_PER_CPU(struct local_irq_lock, lvar) = { \
  9332. + .lock = __SPIN_LOCK_UNLOCKED((lvar).lock) }
  9333. +
  9334. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) \
  9335. + DECLARE_PER_CPU(struct local_irq_lock, lvar)
  9336. +
  9337. +#define local_irq_lock_init(lvar) \
  9338. + do { \
  9339. + int __cpu; \
  9340. + for_each_possible_cpu(__cpu) \
  9341. + spin_lock_init(&per_cpu(lvar, __cpu).lock); \
  9342. + } while (0)
  9343. +
  9344. +/*
  9345. + * spin_lock|trylock|unlock_local flavour that does not migrate disable
  9346. + * used for __local_lock|trylock|unlock where get_local_var/put_local_var
  9347. + * already takes care of the migrate_disable/enable
  9348. + * for CONFIG_PREEMPT_BASE map to the normal spin_* calls.
  9349. + */
  9350. +#ifdef CONFIG_PREEMPT_RT_FULL
  9351. +# define spin_lock_local(lock) rt_spin_lock(lock)
  9352. +# define spin_trylock_local(lock) rt_spin_trylock(lock)
  9353. +# define spin_unlock_local(lock) rt_spin_unlock(lock)
  9354. +#else
  9355. +# define spin_lock_local(lock) spin_lock(lock)
  9356. +# define spin_trylock_local(lock) spin_trylock(lock)
  9357. +# define spin_unlock_local(lock) spin_unlock(lock)
  9358. +#endif
  9359. +
  9360. +static inline void __local_lock(struct local_irq_lock *lv)
  9361. +{
  9362. + if (lv->owner != current) {
  9363. + spin_lock_local(&lv->lock);
  9364. + LL_WARN(lv->owner);
  9365. + LL_WARN(lv->nestcnt);
  9366. + lv->owner = current;
  9367. + }
  9368. + lv->nestcnt++;
  9369. +}
  9370. +
  9371. +#define local_lock(lvar) \
  9372. + do { __local_lock(&get_local_var(lvar)); } while (0)
  9373. +
  9374. +static inline int __local_trylock(struct local_irq_lock *lv)
  9375. +{
  9376. + if (lv->owner != current && spin_trylock_local(&lv->lock)) {
  9377. + LL_WARN(lv->owner);
  9378. + LL_WARN(lv->nestcnt);
  9379. + lv->owner = current;
  9380. + lv->nestcnt = 1;
  9381. + return 1;
  9382. + }
  9383. + return 0;
  9384. +}
  9385. +
  9386. +#define local_trylock(lvar) \
  9387. + ({ \
  9388. + int __locked; \
  9389. + __locked = __local_trylock(&get_local_var(lvar)); \
  9390. + if (!__locked) \
  9391. + put_local_var(lvar); \
  9392. + __locked; \
  9393. + })
  9394. +
  9395. +static inline void __local_unlock(struct local_irq_lock *lv)
  9396. +{
  9397. + LL_WARN(lv->nestcnt == 0);
  9398. + LL_WARN(lv->owner != current);
  9399. + if (--lv->nestcnt)
  9400. + return;
  9401. +
  9402. + lv->owner = NULL;
  9403. + spin_unlock_local(&lv->lock);
  9404. +}
  9405. +
  9406. +#define local_unlock(lvar) \
  9407. + do { \
  9408. + __local_unlock(this_cpu_ptr(&lvar)); \
  9409. + put_local_var(lvar); \
  9410. + } while (0)
  9411. +
  9412. +static inline void __local_lock_irq(struct local_irq_lock *lv)
  9413. +{
  9414. + spin_lock_irqsave(&lv->lock, lv->flags);
  9415. + LL_WARN(lv->owner);
  9416. + LL_WARN(lv->nestcnt);
  9417. + lv->owner = current;
  9418. + lv->nestcnt = 1;
  9419. +}
  9420. +
  9421. +#define local_lock_irq(lvar) \
  9422. + do { __local_lock_irq(&get_local_var(lvar)); } while (0)
  9423. +
  9424. +#define local_lock_irq_on(lvar, cpu) \
  9425. + do { __local_lock_irq(&per_cpu(lvar, cpu)); } while (0)
  9426. +
  9427. +static inline void __local_unlock_irq(struct local_irq_lock *lv)
  9428. +{
  9429. + LL_WARN(!lv->nestcnt);
  9430. + LL_WARN(lv->owner != current);
  9431. + lv->owner = NULL;
  9432. + lv->nestcnt = 0;
  9433. + spin_unlock_irq(&lv->lock);
  9434. +}
  9435. +
  9436. +#define local_unlock_irq(lvar) \
  9437. + do { \
  9438. + __local_unlock_irq(this_cpu_ptr(&lvar)); \
  9439. + put_local_var(lvar); \
  9440. + } while (0)
  9441. +
  9442. +#define local_unlock_irq_on(lvar, cpu) \
  9443. + do { \
  9444. + __local_unlock_irq(&per_cpu(lvar, cpu)); \
  9445. + } while (0)
  9446. +
  9447. +static inline int __local_lock_irqsave(struct local_irq_lock *lv)
  9448. +{
  9449. + if (lv->owner != current) {
  9450. + __local_lock_irq(lv);
  9451. + return 0;
  9452. + } else {
  9453. + lv->nestcnt++;
  9454. + return 1;
  9455. + }
  9456. +}
  9457. +
  9458. +#define local_lock_irqsave(lvar, _flags) \
  9459. + do { \
  9460. + if (__local_lock_irqsave(&get_local_var(lvar))) \
  9461. + put_local_var(lvar); \
  9462. + _flags = __this_cpu_read(lvar.flags); \
  9463. + } while (0)
  9464. +
  9465. +#define local_lock_irqsave_on(lvar, _flags, cpu) \
  9466. + do { \
  9467. + __local_lock_irqsave(&per_cpu(lvar, cpu)); \
  9468. + _flags = per_cpu(lvar, cpu).flags; \
  9469. + } while (0)
  9470. +
  9471. +static inline int __local_unlock_irqrestore(struct local_irq_lock *lv,
  9472. + unsigned long flags)
  9473. +{
  9474. + LL_WARN(!lv->nestcnt);
  9475. + LL_WARN(lv->owner != current);
  9476. + if (--lv->nestcnt)
  9477. + return 0;
  9478. +
  9479. + lv->owner = NULL;
  9480. + spin_unlock_irqrestore(&lv->lock, lv->flags);
  9481. + return 1;
  9482. +}
  9483. +
  9484. +#define local_unlock_irqrestore(lvar, flags) \
  9485. + do { \
  9486. + if (__local_unlock_irqrestore(this_cpu_ptr(&lvar), flags)) \
  9487. + put_local_var(lvar); \
  9488. + } while (0)
  9489. +
  9490. +#define local_unlock_irqrestore_on(lvar, flags, cpu) \
  9491. + do { \
  9492. + __local_unlock_irqrestore(&per_cpu(lvar, cpu), flags); \
  9493. + } while (0)
  9494. +
  9495. +#define local_spin_trylock_irq(lvar, lock) \
  9496. + ({ \
  9497. + int __locked; \
  9498. + local_lock_irq(lvar); \
  9499. + __locked = spin_trylock(lock); \
  9500. + if (!__locked) \
  9501. + local_unlock_irq(lvar); \
  9502. + __locked; \
  9503. + })
  9504. +
  9505. +#define local_spin_lock_irq(lvar, lock) \
  9506. + do { \
  9507. + local_lock_irq(lvar); \
  9508. + spin_lock(lock); \
  9509. + } while (0)
  9510. +
  9511. +#define local_spin_unlock_irq(lvar, lock) \
  9512. + do { \
  9513. + spin_unlock(lock); \
  9514. + local_unlock_irq(lvar); \
  9515. + } while (0)
  9516. +
  9517. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  9518. + do { \
  9519. + local_lock_irqsave(lvar, flags); \
  9520. + spin_lock(lock); \
  9521. + } while (0)
  9522. +
  9523. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  9524. + do { \
  9525. + spin_unlock(lock); \
  9526. + local_unlock_irqrestore(lvar, flags); \
  9527. + } while (0)
  9528. +
  9529. +#define get_locked_var(lvar, var) \
  9530. + (*({ \
  9531. + local_lock(lvar); \
  9532. + this_cpu_ptr(&var); \
  9533. + }))
  9534. +
  9535. +#define put_locked_var(lvar, var) local_unlock(lvar);
  9536. +
  9537. +#define local_lock_cpu(lvar) \
  9538. + ({ \
  9539. + local_lock(lvar); \
  9540. + smp_processor_id(); \
  9541. + })
  9542. +
  9543. +#define local_unlock_cpu(lvar) local_unlock(lvar)
  9544. +
  9545. +#else /* PREEMPT_RT_BASE */
  9546. +
  9547. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) __typeof__(const int) lvar
  9548. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) extern __typeof__(const int) lvar
  9549. +
  9550. +static inline void local_irq_lock_init(int lvar) { }
  9551. +
  9552. +#define local_lock(lvar) preempt_disable()
  9553. +#define local_unlock(lvar) preempt_enable()
  9554. +#define local_lock_irq(lvar) local_irq_disable()
  9555. +#define local_unlock_irq(lvar) local_irq_enable()
  9556. +#define local_lock_irqsave(lvar, flags) local_irq_save(flags)
  9557. +#define local_unlock_irqrestore(lvar, flags) local_irq_restore(flags)
  9558. +
  9559. +#define local_spin_trylock_irq(lvar, lock) spin_trylock_irq(lock)
  9560. +#define local_spin_lock_irq(lvar, lock) spin_lock_irq(lock)
  9561. +#define local_spin_unlock_irq(lvar, lock) spin_unlock_irq(lock)
  9562. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  9563. + spin_lock_irqsave(lock, flags)
  9564. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  9565. + spin_unlock_irqrestore(lock, flags)
  9566. +
  9567. +#define get_locked_var(lvar, var) get_cpu_var(var)
  9568. +#define put_locked_var(lvar, var) put_cpu_var(var)
  9569. +
  9570. +#define local_lock_cpu(lvar) get_cpu()
  9571. +#define local_unlock_cpu(lvar) put_cpu()
  9572. +
  9573. +#endif
  9574. +
  9575. +#endif
  9576. diff -Nur linux-4.1.13.orig/include/linux/mm_types.h linux-4.1.13/include/linux/mm_types.h
  9577. --- linux-4.1.13.orig/include/linux/mm_types.h 2015-11-09 23:34:10.000000000 +0100
  9578. +++ linux-4.1.13/include/linux/mm_types.h 2015-11-29 09:23:09.605614450 +0100
  9579. @@ -11,6 +11,7 @@
  9580. #include <linux/completion.h>
  9581. #include <linux/cpumask.h>
  9582. #include <linux/uprobes.h>
  9583. +#include <linux/rcupdate.h>
  9584. #include <linux/page-flags-layout.h>
  9585. #include <asm/page.h>
  9586. #include <asm/mmu.h>
  9587. @@ -453,6 +454,9 @@
  9588. bool tlb_flush_pending;
  9589. #endif
  9590. struct uprobes_state uprobes_state;
  9591. +#ifdef CONFIG_PREEMPT_RT_BASE
  9592. + struct rcu_head delayed_drop;
  9593. +#endif
  9594. #ifdef CONFIG_X86_INTEL_MPX
  9595. /* address of the bounds directory */
  9596. void __user *bd_addr;
  9597. diff -Nur linux-4.1.13.orig/include/linux/mutex.h linux-4.1.13/include/linux/mutex.h
  9598. --- linux-4.1.13.orig/include/linux/mutex.h 2015-11-09 23:34:10.000000000 +0100
  9599. +++ linux-4.1.13/include/linux/mutex.h 2015-11-29 09:23:09.609614185 +0100
  9600. @@ -19,6 +19,17 @@
  9601. #include <asm/processor.h>
  9602. #include <linux/osq_lock.h>
  9603. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  9604. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  9605. + , .dep_map = { .name = #lockname }
  9606. +#else
  9607. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  9608. +#endif
  9609. +
  9610. +#ifdef CONFIG_PREEMPT_RT_FULL
  9611. +# include <linux/mutex_rt.h>
  9612. +#else
  9613. +
  9614. /*
  9615. * Simple, straightforward mutexes with strict semantics:
  9616. *
  9617. @@ -99,13 +110,6 @@
  9618. static inline void mutex_destroy(struct mutex *lock) {}
  9619. #endif
  9620. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  9621. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  9622. - , .dep_map = { .name = #lockname }
  9623. -#else
  9624. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  9625. -#endif
  9626. -
  9627. #define __MUTEX_INITIALIZER(lockname) \
  9628. { .count = ATOMIC_INIT(1) \
  9629. , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  9630. @@ -173,6 +177,8 @@
  9631. extern int mutex_trylock(struct mutex *lock);
  9632. extern void mutex_unlock(struct mutex *lock);
  9633. +#endif /* !PREEMPT_RT_FULL */
  9634. +
  9635. extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
  9636. #endif /* __LINUX_MUTEX_H */
  9637. diff -Nur linux-4.1.13.orig/include/linux/mutex_rt.h linux-4.1.13/include/linux/mutex_rt.h
  9638. --- linux-4.1.13.orig/include/linux/mutex_rt.h 1970-01-01 01:00:00.000000000 +0100
  9639. +++ linux-4.1.13/include/linux/mutex_rt.h 2015-11-29 09:23:09.609614185 +0100
  9640. @@ -0,0 +1,84 @@
  9641. +#ifndef __LINUX_MUTEX_RT_H
  9642. +#define __LINUX_MUTEX_RT_H
  9643. +
  9644. +#ifndef __LINUX_MUTEX_H
  9645. +#error "Please include mutex.h"
  9646. +#endif
  9647. +
  9648. +#include <linux/rtmutex.h>
  9649. +
  9650. +/* FIXME: Just for __lockfunc */
  9651. +#include <linux/spinlock.h>
  9652. +
  9653. +struct mutex {
  9654. + struct rt_mutex lock;
  9655. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  9656. + struct lockdep_map dep_map;
  9657. +#endif
  9658. +};
  9659. +
  9660. +#define __MUTEX_INITIALIZER(mutexname) \
  9661. + { \
  9662. + .lock = __RT_MUTEX_INITIALIZER(mutexname.lock) \
  9663. + __DEP_MAP_MUTEX_INITIALIZER(mutexname) \
  9664. + }
  9665. +
  9666. +#define DEFINE_MUTEX(mutexname) \
  9667. + struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  9668. +
  9669. +extern void __mutex_do_init(struct mutex *lock, const char *name, struct lock_class_key *key);
  9670. +extern void __lockfunc _mutex_lock(struct mutex *lock);
  9671. +extern int __lockfunc _mutex_lock_interruptible(struct mutex *lock);
  9672. +extern int __lockfunc _mutex_lock_killable(struct mutex *lock);
  9673. +extern void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass);
  9674. +extern void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
  9675. +extern int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass);
  9676. +extern int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass);
  9677. +extern int __lockfunc _mutex_trylock(struct mutex *lock);
  9678. +extern void __lockfunc _mutex_unlock(struct mutex *lock);
  9679. +
  9680. +#define mutex_is_locked(l) rt_mutex_is_locked(&(l)->lock)
  9681. +#define mutex_lock(l) _mutex_lock(l)
  9682. +#define mutex_lock_interruptible(l) _mutex_lock_interruptible(l)
  9683. +#define mutex_lock_killable(l) _mutex_lock_killable(l)
  9684. +#define mutex_trylock(l) _mutex_trylock(l)
  9685. +#define mutex_unlock(l) _mutex_unlock(l)
  9686. +#define mutex_destroy(l) rt_mutex_destroy(&(l)->lock)
  9687. +
  9688. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  9689. +# define mutex_lock_nested(l, s) _mutex_lock_nested(l, s)
  9690. +# define mutex_lock_interruptible_nested(l, s) \
  9691. + _mutex_lock_interruptible_nested(l, s)
  9692. +# define mutex_lock_killable_nested(l, s) \
  9693. + _mutex_lock_killable_nested(l, s)
  9694. +
  9695. +# define mutex_lock_nest_lock(lock, nest_lock) \
  9696. +do { \
  9697. + typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  9698. + _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  9699. +} while (0)
  9700. +
  9701. +#else
  9702. +# define mutex_lock_nested(l, s) _mutex_lock(l)
  9703. +# define mutex_lock_interruptible_nested(l, s) \
  9704. + _mutex_lock_interruptible(l)
  9705. +# define mutex_lock_killable_nested(l, s) \
  9706. + _mutex_lock_killable(l)
  9707. +# define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
  9708. +#endif
  9709. +
  9710. +# define mutex_init(mutex) \
  9711. +do { \
  9712. + static struct lock_class_key __key; \
  9713. + \
  9714. + rt_mutex_init(&(mutex)->lock); \
  9715. + __mutex_do_init((mutex), #mutex, &__key); \
  9716. +} while (0)
  9717. +
  9718. +# define __mutex_init(mutex, name, key) \
  9719. +do { \
  9720. + rt_mutex_init(&(mutex)->lock); \
  9721. + __mutex_do_init((mutex), name, key); \
  9722. +} while (0)
  9723. +
  9724. +#endif
  9725. diff -Nur linux-4.1.13.orig/include/linux/netdevice.h linux-4.1.13/include/linux/netdevice.h
  9726. --- linux-4.1.13.orig/include/linux/netdevice.h 2015-11-09 23:34:10.000000000 +0100
  9727. +++ linux-4.1.13/include/linux/netdevice.h 2015-11-29 09:23:09.609614185 +0100
  9728. @@ -2469,6 +2469,7 @@
  9729. unsigned int dropped;
  9730. struct sk_buff_head input_pkt_queue;
  9731. struct napi_struct backlog;
  9732. + struct sk_buff_head tofree_queue;
  9733. };
  9734. diff -Nur linux-4.1.13.orig/include/linux/netfilter/x_tables.h linux-4.1.13/include/linux/netfilter/x_tables.h
  9735. --- linux-4.1.13.orig/include/linux/netfilter/x_tables.h 2015-11-09 23:34:10.000000000 +0100
  9736. +++ linux-4.1.13/include/linux/netfilter/x_tables.h 2015-11-29 09:23:09.609614185 +0100
  9737. @@ -3,6 +3,7 @@
  9738. #include <linux/netdevice.h>
  9739. +#include <linux/locallock.h>
  9740. #include <uapi/linux/netfilter/x_tables.h>
  9741. /**
  9742. @@ -282,6 +283,8 @@
  9743. */
  9744. DECLARE_PER_CPU(seqcount_t, xt_recseq);
  9745. +DECLARE_LOCAL_IRQ_LOCK(xt_write_lock);
  9746. +
  9747. /**
  9748. * xt_write_recseq_begin - start of a write section
  9749. *
  9750. @@ -296,6 +299,9 @@
  9751. {
  9752. unsigned int addend;
  9753. + /* RT protection */
  9754. + local_lock(xt_write_lock);
  9755. +
  9756. /*
  9757. * Low order bit of sequence is set if we already
  9758. * called xt_write_recseq_begin().
  9759. @@ -326,6 +332,7 @@
  9760. /* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
  9761. smp_wmb();
  9762. __this_cpu_add(xt_recseq.sequence, addend);
  9763. + local_unlock(xt_write_lock);
  9764. }
  9765. /*
  9766. diff -Nur linux-4.1.13.orig/include/linux/notifier.h linux-4.1.13/include/linux/notifier.h
  9767. --- linux-4.1.13.orig/include/linux/notifier.h 2015-11-09 23:34:10.000000000 +0100
  9768. +++ linux-4.1.13/include/linux/notifier.h 2015-11-29 09:23:09.609614185 +0100
  9769. @@ -6,7 +6,7 @@
  9770. *
  9771. * Alan Cox <Alan.Cox@linux.org>
  9772. */
  9773. -
  9774. +
  9775. #ifndef _LINUX_NOTIFIER_H
  9776. #define _LINUX_NOTIFIER_H
  9777. #include <linux/errno.h>
  9778. @@ -42,9 +42,7 @@
  9779. * in srcu_notifier_call_chain(): no cache bounces and no memory barriers.
  9780. * As compensation, srcu_notifier_chain_unregister() is rather expensive.
  9781. * SRCU notifier chains should be used when the chain will be called very
  9782. - * often but notifier_blocks will seldom be removed. Also, SRCU notifier
  9783. - * chains are slightly more difficult to use because they require special
  9784. - * runtime initialization.
  9785. + * often but notifier_blocks will seldom be removed.
  9786. */
  9787. typedef int (*notifier_fn_t)(struct notifier_block *nb,
  9788. @@ -88,7 +86,7 @@
  9789. (name)->head = NULL; \
  9790. } while (0)
  9791. -/* srcu_notifier_heads must be initialized and cleaned up dynamically */
  9792. +/* srcu_notifier_heads must be cleaned up dynamically */
  9793. extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  9794. #define srcu_cleanup_notifier_head(name) \
  9795. cleanup_srcu_struct(&(name)->srcu);
  9796. @@ -101,7 +99,13 @@
  9797. .head = NULL }
  9798. #define RAW_NOTIFIER_INIT(name) { \
  9799. .head = NULL }
  9800. -/* srcu_notifier_heads cannot be initialized statically */
  9801. +
  9802. +#define SRCU_NOTIFIER_INIT(name, pcpu) \
  9803. + { \
  9804. + .mutex = __MUTEX_INITIALIZER(name.mutex), \
  9805. + .head = NULL, \
  9806. + .srcu = __SRCU_STRUCT_INIT(name.srcu, pcpu), \
  9807. + }
  9808. #define ATOMIC_NOTIFIER_HEAD(name) \
  9809. struct atomic_notifier_head name = \
  9810. @@ -113,6 +117,18 @@
  9811. struct raw_notifier_head name = \
  9812. RAW_NOTIFIER_INIT(name)
  9813. +#define _SRCU_NOTIFIER_HEAD(name, mod) \
  9814. + static DEFINE_PER_CPU(struct srcu_struct_array, \
  9815. + name##_head_srcu_array); \
  9816. + mod struct srcu_notifier_head name = \
  9817. + SRCU_NOTIFIER_INIT(name, name##_head_srcu_array)
  9818. +
  9819. +#define SRCU_NOTIFIER_HEAD(name) \
  9820. + _SRCU_NOTIFIER_HEAD(name, )
  9821. +
  9822. +#define SRCU_NOTIFIER_HEAD_STATIC(name) \
  9823. + _SRCU_NOTIFIER_HEAD(name, static)
  9824. +
  9825. #ifdef __KERNEL__
  9826. extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
  9827. @@ -182,12 +198,12 @@
  9828. /*
  9829. * Declared notifiers so far. I can imagine quite a few more chains
  9830. - * over time (eg laptop power reset chains, reboot chain (to clean
  9831. + * over time (eg laptop power reset chains, reboot chain (to clean
  9832. * device units up), device [un]mount chain, module load/unload chain,
  9833. - * low memory chain, screenblank chain (for plug in modular screenblankers)
  9834. + * low memory chain, screenblank chain (for plug in modular screenblankers)
  9835. * VC switch chains (for loadable kernel svgalib VC switch helpers) etc...
  9836. */
  9837. -
  9838. +
  9839. /* CPU notfiers are defined in include/linux/cpu.h. */
  9840. /* netdevice notifiers are defined in include/linux/netdevice.h */
  9841. diff -Nur linux-4.1.13.orig/include/linux/percpu.h linux-4.1.13/include/linux/percpu.h
  9842. --- linux-4.1.13.orig/include/linux/percpu.h 2015-11-09 23:34:10.000000000 +0100
  9843. +++ linux-4.1.13/include/linux/percpu.h 2015-11-29 09:23:09.609614185 +0100
  9844. @@ -24,6 +24,35 @@
  9845. PERCPU_MODULE_RESERVE)
  9846. #endif
  9847. +#ifdef CONFIG_PREEMPT_RT_FULL
  9848. +
  9849. +#define get_local_var(var) (*({ \
  9850. + migrate_disable(); \
  9851. + this_cpu_ptr(&var); }))
  9852. +
  9853. +#define put_local_var(var) do { \
  9854. + (void)&(var); \
  9855. + migrate_enable(); \
  9856. +} while (0)
  9857. +
  9858. +# define get_local_ptr(var) ({ \
  9859. + migrate_disable(); \
  9860. + this_cpu_ptr(var); })
  9861. +
  9862. +# define put_local_ptr(var) do { \
  9863. + (void)(var); \
  9864. + migrate_enable(); \
  9865. +} while (0)
  9866. +
  9867. +#else
  9868. +
  9869. +#define get_local_var(var) get_cpu_var(var)
  9870. +#define put_local_var(var) put_cpu_var(var)
  9871. +#define get_local_ptr(var) get_cpu_ptr(var)
  9872. +#define put_local_ptr(var) put_cpu_ptr(var)
  9873. +
  9874. +#endif
  9875. +
  9876. /* minimum unit size, also is the maximum supported allocation size */
  9877. #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
  9878. diff -Nur linux-4.1.13.orig/include/linux/pid.h linux-4.1.13/include/linux/pid.h
  9879. --- linux-4.1.13.orig/include/linux/pid.h 2015-11-09 23:34:10.000000000 +0100
  9880. +++ linux-4.1.13/include/linux/pid.h 2015-11-29 09:23:09.609614185 +0100
  9881. @@ -2,6 +2,7 @@
  9882. #define _LINUX_PID_H
  9883. #include <linux/rcupdate.h>
  9884. +#include <linux/atomic.h>
  9885. enum pid_type
  9886. {
  9887. diff -Nur linux-4.1.13.orig/include/linux/preempt.h linux-4.1.13/include/linux/preempt.h
  9888. --- linux-4.1.13.orig/include/linux/preempt.h 2015-11-09 23:34:10.000000000 +0100
  9889. +++ linux-4.1.13/include/linux/preempt.h 2015-11-29 09:23:09.609614185 +0100
  9890. @@ -34,6 +34,20 @@
  9891. #define preempt_count_inc() preempt_count_add(1)
  9892. #define preempt_count_dec() preempt_count_sub(1)
  9893. +#ifdef CONFIG_PREEMPT_LAZY
  9894. +#define add_preempt_lazy_count(val) do { preempt_lazy_count() += (val); } while (0)
  9895. +#define sub_preempt_lazy_count(val) do { preempt_lazy_count() -= (val); } while (0)
  9896. +#define inc_preempt_lazy_count() add_preempt_lazy_count(1)
  9897. +#define dec_preempt_lazy_count() sub_preempt_lazy_count(1)
  9898. +#define preempt_lazy_count() (current_thread_info()->preempt_lazy_count)
  9899. +#else
  9900. +#define add_preempt_lazy_count(val) do { } while (0)
  9901. +#define sub_preempt_lazy_count(val) do { } while (0)
  9902. +#define inc_preempt_lazy_count() do { } while (0)
  9903. +#define dec_preempt_lazy_count() do { } while (0)
  9904. +#define preempt_lazy_count() (0)
  9905. +#endif
  9906. +
  9907. #ifdef CONFIG_PREEMPT_COUNT
  9908. #define preempt_disable() \
  9909. @@ -42,13 +56,25 @@
  9910. barrier(); \
  9911. } while (0)
  9912. +#define preempt_lazy_disable() \
  9913. +do { \
  9914. + inc_preempt_lazy_count(); \
  9915. + barrier(); \
  9916. +} while (0)
  9917. +
  9918. #define sched_preempt_enable_no_resched() \
  9919. do { \
  9920. barrier(); \
  9921. preempt_count_dec(); \
  9922. } while (0)
  9923. -#define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  9924. +#ifdef CONFIG_PREEMPT_RT_BASE
  9925. +# define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  9926. +# define preempt_check_resched_rt() preempt_check_resched()
  9927. +#else
  9928. +# define preempt_enable_no_resched() preempt_enable()
  9929. +# define preempt_check_resched_rt() barrier();
  9930. +#endif
  9931. #ifdef CONFIG_PREEMPT
  9932. #define preempt_enable() \
  9933. @@ -64,6 +90,13 @@
  9934. __preempt_schedule(); \
  9935. } while (0)
  9936. +#define preempt_lazy_enable() \
  9937. +do { \
  9938. + dec_preempt_lazy_count(); \
  9939. + barrier(); \
  9940. + preempt_check_resched(); \
  9941. +} while (0)
  9942. +
  9943. #else
  9944. #define preempt_enable() \
  9945. do { \
  9946. @@ -122,6 +155,7 @@
  9947. #define preempt_disable_notrace() barrier()
  9948. #define preempt_enable_no_resched_notrace() barrier()
  9949. #define preempt_enable_notrace() barrier()
  9950. +#define preempt_check_resched_rt() barrier()
  9951. #endif /* CONFIG_PREEMPT_COUNT */
  9952. @@ -141,10 +175,31 @@
  9953. } while (0)
  9954. #define preempt_fold_need_resched() \
  9955. do { \
  9956. - if (tif_need_resched()) \
  9957. + if (tif_need_resched_now()) \
  9958. set_preempt_need_resched(); \
  9959. } while (0)
  9960. +#ifdef CONFIG_PREEMPT_RT_FULL
  9961. +# define preempt_disable_rt() preempt_disable()
  9962. +# define preempt_enable_rt() preempt_enable()
  9963. +# define preempt_disable_nort() barrier()
  9964. +# define preempt_enable_nort() barrier()
  9965. +# ifdef CONFIG_SMP
  9966. + extern void migrate_disable(void);
  9967. + extern void migrate_enable(void);
  9968. +# else /* CONFIG_SMP */
  9969. +# define migrate_disable() barrier()
  9970. +# define migrate_enable() barrier()
  9971. +# endif /* CONFIG_SMP */
  9972. +#else
  9973. +# define preempt_disable_rt() barrier()
  9974. +# define preempt_enable_rt() barrier()
  9975. +# define preempt_disable_nort() preempt_disable()
  9976. +# define preempt_enable_nort() preempt_enable()
  9977. +# define migrate_disable() preempt_disable()
  9978. +# define migrate_enable() preempt_enable()
  9979. +#endif
  9980. +
  9981. #ifdef CONFIG_PREEMPT_NOTIFIERS
  9982. struct preempt_notifier;
  9983. diff -Nur linux-4.1.13.orig/include/linux/preempt_mask.h linux-4.1.13/include/linux/preempt_mask.h
  9984. --- linux-4.1.13.orig/include/linux/preempt_mask.h 2015-11-09 23:34:10.000000000 +0100
  9985. +++ linux-4.1.13/include/linux/preempt_mask.h 2015-11-29 09:23:09.609614185 +0100
  9986. @@ -44,16 +44,26 @@
  9987. #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
  9988. #define NMI_OFFSET (1UL << NMI_SHIFT)
  9989. -#define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  9990. +#ifndef CONFIG_PREEMPT_RT_FULL
  9991. +# define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  9992. +#else
  9993. +# define SOFTIRQ_DISABLE_OFFSET (0)
  9994. +#endif
  9995. #define PREEMPT_ACTIVE_BITS 1
  9996. #define PREEMPT_ACTIVE_SHIFT (NMI_SHIFT + NMI_BITS)
  9997. #define PREEMPT_ACTIVE (__IRQ_MASK(PREEMPT_ACTIVE_BITS) << PREEMPT_ACTIVE_SHIFT)
  9998. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  9999. -#define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  10000. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
  10001. | NMI_MASK))
  10002. +#ifndef CONFIG_PREEMPT_RT_FULL
  10003. +# define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  10004. +# define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  10005. +#else
  10006. +# define softirq_count() (0UL)
  10007. +extern int in_serving_softirq(void);
  10008. +#endif
  10009. /*
  10010. * Are we doing bottom half or hardware interrupt processing?
  10011. @@ -64,7 +74,6 @@
  10012. #define in_irq() (hardirq_count())
  10013. #define in_softirq() (softirq_count())
  10014. #define in_interrupt() (irq_count())
  10015. -#define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  10016. /*
  10017. * Are we in NMI context?
  10018. @@ -83,7 +92,11 @@
  10019. /*
  10020. * The preempt_count offset after spin_lock()
  10021. */
  10022. +#if !defined(CONFIG_PREEMPT_RT_FULL)
  10023. #define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
  10024. +#else
  10025. +#define PREEMPT_LOCK_OFFSET 0
  10026. +#endif
  10027. /*
  10028. * The preempt_count offset needed for things like:
  10029. diff -Nur linux-4.1.13.orig/include/linux/printk.h linux-4.1.13/include/linux/printk.h
  10030. --- linux-4.1.13.orig/include/linux/printk.h 2015-11-09 23:34:10.000000000 +0100
  10031. +++ linux-4.1.13/include/linux/printk.h 2015-11-29 09:23:09.609614185 +0100
  10032. @@ -115,9 +115,11 @@
  10033. #ifdef CONFIG_EARLY_PRINTK
  10034. extern asmlinkage __printf(1, 2)
  10035. void early_printk(const char *fmt, ...);
  10036. +extern void printk_kill(void);
  10037. #else
  10038. static inline __printf(1, 2) __cold
  10039. void early_printk(const char *s, ...) { }
  10040. +static inline void printk_kill(void) { }
  10041. #endif
  10042. typedef int(*printk_func_t)(const char *fmt, va_list args);
  10043. diff -Nur linux-4.1.13.orig/include/linux/radix-tree.h linux-4.1.13/include/linux/radix-tree.h
  10044. --- linux-4.1.13.orig/include/linux/radix-tree.h 2015-11-09 23:34:10.000000000 +0100
  10045. +++ linux-4.1.13/include/linux/radix-tree.h 2015-11-29 09:23:09.609614185 +0100
  10046. @@ -277,8 +277,13 @@
  10047. unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  10048. void ***results, unsigned long *indices,
  10049. unsigned long first_index, unsigned int max_items);
  10050. +#ifndef CONFIG_PREEMPT_RT_FULL
  10051. int radix_tree_preload(gfp_t gfp_mask);
  10052. int radix_tree_maybe_preload(gfp_t gfp_mask);
  10053. +#else
  10054. +static inline int radix_tree_preload(gfp_t gm) { return 0; }
  10055. +static inline int radix_tree_maybe_preload(gfp_t gfp_mask) { return 0; }
  10056. +#endif
  10057. void radix_tree_init(void);
  10058. void *radix_tree_tag_set(struct radix_tree_root *root,
  10059. unsigned long index, unsigned int tag);
  10060. @@ -303,7 +308,7 @@
  10061. static inline void radix_tree_preload_end(void)
  10062. {
  10063. - preempt_enable();
  10064. + preempt_enable_nort();
  10065. }
  10066. /**
  10067. diff -Nur linux-4.1.13.orig/include/linux/random.h linux-4.1.13/include/linux/random.h
  10068. --- linux-4.1.13.orig/include/linux/random.h 2015-11-09 23:34:10.000000000 +0100
  10069. +++ linux-4.1.13/include/linux/random.h 2015-11-29 09:23:09.609614185 +0100
  10070. @@ -11,7 +11,7 @@
  10071. extern void add_device_randomness(const void *, unsigned int);
  10072. extern void add_input_randomness(unsigned int type, unsigned int code,
  10073. unsigned int value);
  10074. -extern void add_interrupt_randomness(int irq, int irq_flags);
  10075. +extern void add_interrupt_randomness(int irq, int irq_flags, __u64 ip);
  10076. extern void get_random_bytes(void *buf, int nbytes);
  10077. extern void get_random_bytes_arch(void *buf, int nbytes);
  10078. diff -Nur linux-4.1.13.orig/include/linux/rcupdate.h linux-4.1.13/include/linux/rcupdate.h
  10079. --- linux-4.1.13.orig/include/linux/rcupdate.h 2015-11-09 23:34:10.000000000 +0100
  10080. +++ linux-4.1.13/include/linux/rcupdate.h 2015-11-29 09:23:09.609614185 +0100
  10081. @@ -167,6 +167,9 @@
  10082. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  10083. +#ifdef CONFIG_PREEMPT_RT_FULL
  10084. +#define call_rcu_bh call_rcu
  10085. +#else
  10086. /**
  10087. * call_rcu_bh() - Queue an RCU for invocation after a quicker grace period.
  10088. * @head: structure to be used for queueing the RCU updates.
  10089. @@ -190,6 +193,7 @@
  10090. */
  10091. void call_rcu_bh(struct rcu_head *head,
  10092. void (*func)(struct rcu_head *head));
  10093. +#endif
  10094. /**
  10095. * call_rcu_sched() - Queue an RCU for invocation after sched grace period.
  10096. @@ -260,6 +264,11 @@
  10097. * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
  10098. */
  10099. #define rcu_preempt_depth() (current->rcu_read_lock_nesting)
  10100. +#ifndef CONFIG_PREEMPT_RT_FULL
  10101. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  10102. +#else
  10103. +static inline int sched_rcu_preempt_depth(void) { return 0; }
  10104. +#endif
  10105. #else /* #ifdef CONFIG_PREEMPT_RCU */
  10106. @@ -283,6 +292,8 @@
  10107. return 0;
  10108. }
  10109. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  10110. +
  10111. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  10112. /* Internal to kernel */
  10113. @@ -463,7 +474,14 @@
  10114. int debug_lockdep_rcu_enabled(void);
  10115. int rcu_read_lock_held(void);
  10116. +#ifdef CONFIG_PREEMPT_RT_FULL
  10117. +static inline int rcu_read_lock_bh_held(void)
  10118. +{
  10119. + return rcu_read_lock_held();
  10120. +}
  10121. +#else
  10122. int rcu_read_lock_bh_held(void);
  10123. +#endif
  10124. /**
  10125. * rcu_read_lock_sched_held() - might we be in RCU-sched read-side critical section?
  10126. @@ -990,10 +1008,14 @@
  10127. static inline void rcu_read_lock_bh(void)
  10128. {
  10129. local_bh_disable();
  10130. +#ifdef CONFIG_PREEMPT_RT_FULL
  10131. + rcu_read_lock();
  10132. +#else
  10133. __acquire(RCU_BH);
  10134. rcu_lock_acquire(&rcu_bh_lock_map);
  10135. rcu_lockdep_assert(rcu_is_watching(),
  10136. "rcu_read_lock_bh() used illegally while idle");
  10137. +#endif
  10138. }
  10139. /*
  10140. @@ -1003,10 +1025,14 @@
  10141. */
  10142. static inline void rcu_read_unlock_bh(void)
  10143. {
  10144. +#ifdef CONFIG_PREEMPT_RT_FULL
  10145. + rcu_read_unlock();
  10146. +#else
  10147. rcu_lockdep_assert(rcu_is_watching(),
  10148. "rcu_read_unlock_bh() used illegally while idle");
  10149. rcu_lock_release(&rcu_bh_lock_map);
  10150. __release(RCU_BH);
  10151. +#endif
  10152. local_bh_enable();
  10153. }
  10154. diff -Nur linux-4.1.13.orig/include/linux/rcutree.h linux-4.1.13/include/linux/rcutree.h
  10155. --- linux-4.1.13.orig/include/linux/rcutree.h 2015-11-09 23:34:10.000000000 +0100
  10156. +++ linux-4.1.13/include/linux/rcutree.h 2015-11-29 09:23:09.609614185 +0100
  10157. @@ -46,7 +46,11 @@
  10158. rcu_note_context_switch();
  10159. }
  10160. +#ifdef CONFIG_PREEMPT_RT_FULL
  10161. +# define synchronize_rcu_bh synchronize_rcu
  10162. +#else
  10163. void synchronize_rcu_bh(void);
  10164. +#endif
  10165. void synchronize_sched_expedited(void);
  10166. void synchronize_rcu_expedited(void);
  10167. @@ -74,7 +78,11 @@
  10168. }
  10169. void rcu_barrier(void);
  10170. +#ifdef CONFIG_PREEMPT_RT_FULL
  10171. +# define rcu_barrier_bh rcu_barrier
  10172. +#else
  10173. void rcu_barrier_bh(void);
  10174. +#endif
  10175. void rcu_barrier_sched(void);
  10176. unsigned long get_state_synchronize_rcu(void);
  10177. void cond_synchronize_rcu(unsigned long oldstate);
  10178. @@ -85,12 +93,10 @@
  10179. unsigned long rcu_batches_started_bh(void);
  10180. unsigned long rcu_batches_started_sched(void);
  10181. unsigned long rcu_batches_completed(void);
  10182. -unsigned long rcu_batches_completed_bh(void);
  10183. unsigned long rcu_batches_completed_sched(void);
  10184. void show_rcu_gp_kthreads(void);
  10185. void rcu_force_quiescent_state(void);
  10186. -void rcu_bh_force_quiescent_state(void);
  10187. void rcu_sched_force_quiescent_state(void);
  10188. void exit_rcu(void);
  10189. @@ -100,6 +106,14 @@
  10190. bool rcu_is_watching(void);
  10191. +#ifndef CONFIG_PREEMPT_RT_FULL
  10192. +void rcu_bh_force_quiescent_state(void);
  10193. +unsigned long rcu_batches_completed_bh(void);
  10194. +#else
  10195. +# define rcu_bh_force_quiescent_state rcu_force_quiescent_state
  10196. +# define rcu_batches_completed_bh rcu_batches_completed
  10197. +#endif
  10198. +
  10199. void rcu_all_qs(void);
  10200. #endif /* __LINUX_RCUTREE_H */
  10201. diff -Nur linux-4.1.13.orig/include/linux/rtmutex.h linux-4.1.13/include/linux/rtmutex.h
  10202. --- linux-4.1.13.orig/include/linux/rtmutex.h 2015-11-09 23:34:10.000000000 +0100
  10203. +++ linux-4.1.13/include/linux/rtmutex.h 2015-11-29 09:23:09.609614185 +0100
  10204. @@ -14,10 +14,14 @@
  10205. #include <linux/linkage.h>
  10206. #include <linux/rbtree.h>
  10207. -#include <linux/spinlock_types.h>
  10208. +#include <linux/spinlock_types_raw.h>
  10209. extern int max_lock_depth; /* for sysctl */
  10210. +#ifdef CONFIG_DEBUG_MUTEXES
  10211. +#include <linux/debug_locks.h>
  10212. +#endif
  10213. +
  10214. /**
  10215. * The rt_mutex structure
  10216. *
  10217. @@ -31,8 +35,8 @@
  10218. struct rb_root waiters;
  10219. struct rb_node *waiters_leftmost;
  10220. struct task_struct *owner;
  10221. -#ifdef CONFIG_DEBUG_RT_MUTEXES
  10222. int save_state;
  10223. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  10224. const char *name, *file;
  10225. int line;
  10226. void *magic;
  10227. @@ -55,22 +59,33 @@
  10228. # define rt_mutex_debug_check_no_locks_held(task) do { } while (0)
  10229. #endif
  10230. +# define rt_mutex_init(mutex) \
  10231. + do { \
  10232. + raw_spin_lock_init(&(mutex)->wait_lock); \
  10233. + __rt_mutex_init(mutex, #mutex); \
  10234. + } while (0)
  10235. +
  10236. #ifdef CONFIG_DEBUG_RT_MUTEXES
  10237. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
  10238. , .name = #mutexname, .file = __FILE__, .line = __LINE__
  10239. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, __func__)
  10240. extern void rt_mutex_debug_task_free(struct task_struct *tsk);
  10241. #else
  10242. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  10243. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL)
  10244. # define rt_mutex_debug_task_free(t) do { } while (0)
  10245. #endif
  10246. -#define __RT_MUTEX_INITIALIZER(mutexname) \
  10247. - { .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  10248. +#define __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  10249. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  10250. , .waiters = RB_ROOT \
  10251. , .owner = NULL \
  10252. - __DEBUG_RT_MUTEX_INITIALIZER(mutexname)}
  10253. + __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  10254. +
  10255. +#define __RT_MUTEX_INITIALIZER(mutexname) \
  10256. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) }
  10257. +
  10258. +#define __RT_MUTEX_INITIALIZER_SAVE_STATE(mutexname) \
  10259. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  10260. + , .save_state = 1 }
  10261. #define DEFINE_RT_MUTEX(mutexname) \
  10262. struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
  10263. @@ -91,6 +106,7 @@
  10264. extern void rt_mutex_lock(struct rt_mutex *lock);
  10265. extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
  10266. +extern int rt_mutex_lock_killable(struct rt_mutex *lock);
  10267. extern int rt_mutex_timed_lock(struct rt_mutex *lock,
  10268. struct hrtimer_sleeper *timeout);
  10269. diff -Nur linux-4.1.13.orig/include/linux/rwlock_rt.h linux-4.1.13/include/linux/rwlock_rt.h
  10270. --- linux-4.1.13.orig/include/linux/rwlock_rt.h 1970-01-01 01:00:00.000000000 +0100
  10271. +++ linux-4.1.13/include/linux/rwlock_rt.h 2015-11-29 09:23:09.609614185 +0100
  10272. @@ -0,0 +1,99 @@
  10273. +#ifndef __LINUX_RWLOCK_RT_H
  10274. +#define __LINUX_RWLOCK_RT_H
  10275. +
  10276. +#ifndef __LINUX_SPINLOCK_H
  10277. +#error Do not include directly. Use spinlock.h
  10278. +#endif
  10279. +
  10280. +#define rwlock_init(rwl) \
  10281. +do { \
  10282. + static struct lock_class_key __key; \
  10283. + \
  10284. + rt_mutex_init(&(rwl)->lock); \
  10285. + __rt_rwlock_init(rwl, #rwl, &__key); \
  10286. +} while (0)
  10287. +
  10288. +extern void __lockfunc rt_write_lock(rwlock_t *rwlock);
  10289. +extern void __lockfunc rt_read_lock(rwlock_t *rwlock);
  10290. +extern int __lockfunc rt_write_trylock(rwlock_t *rwlock);
  10291. +extern int __lockfunc rt_write_trylock_irqsave(rwlock_t *trylock, unsigned long *flags);
  10292. +extern int __lockfunc rt_read_trylock(rwlock_t *rwlock);
  10293. +extern void __lockfunc rt_write_unlock(rwlock_t *rwlock);
  10294. +extern void __lockfunc rt_read_unlock(rwlock_t *rwlock);
  10295. +extern unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock);
  10296. +extern unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock);
  10297. +extern void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key);
  10298. +
  10299. +#define read_trylock(lock) __cond_lock(lock, rt_read_trylock(lock))
  10300. +#define write_trylock(lock) __cond_lock(lock, rt_write_trylock(lock))
  10301. +
  10302. +#define write_trylock_irqsave(lock, flags) \
  10303. + __cond_lock(lock, rt_write_trylock_irqsave(lock, &flags))
  10304. +
  10305. +#define read_lock_irqsave(lock, flags) \
  10306. + do { \
  10307. + typecheck(unsigned long, flags); \
  10308. + flags = rt_read_lock_irqsave(lock); \
  10309. + } while (0)
  10310. +
  10311. +#define write_lock_irqsave(lock, flags) \
  10312. + do { \
  10313. + typecheck(unsigned long, flags); \
  10314. + flags = rt_write_lock_irqsave(lock); \
  10315. + } while (0)
  10316. +
  10317. +#define read_lock(lock) rt_read_lock(lock)
  10318. +
  10319. +#define read_lock_bh(lock) \
  10320. + do { \
  10321. + local_bh_disable(); \
  10322. + rt_read_lock(lock); \
  10323. + } while (0)
  10324. +
  10325. +#define read_lock_irq(lock) read_lock(lock)
  10326. +
  10327. +#define write_lock(lock) rt_write_lock(lock)
  10328. +
  10329. +#define write_lock_bh(lock) \
  10330. + do { \
  10331. + local_bh_disable(); \
  10332. + rt_write_lock(lock); \
  10333. + } while (0)
  10334. +
  10335. +#define write_lock_irq(lock) write_lock(lock)
  10336. +
  10337. +#define read_unlock(lock) rt_read_unlock(lock)
  10338. +
  10339. +#define read_unlock_bh(lock) \
  10340. + do { \
  10341. + rt_read_unlock(lock); \
  10342. + local_bh_enable(); \
  10343. + } while (0)
  10344. +
  10345. +#define read_unlock_irq(lock) read_unlock(lock)
  10346. +
  10347. +#define write_unlock(lock) rt_write_unlock(lock)
  10348. +
  10349. +#define write_unlock_bh(lock) \
  10350. + do { \
  10351. + rt_write_unlock(lock); \
  10352. + local_bh_enable(); \
  10353. + } while (0)
  10354. +
  10355. +#define write_unlock_irq(lock) write_unlock(lock)
  10356. +
  10357. +#define read_unlock_irqrestore(lock, flags) \
  10358. + do { \
  10359. + typecheck(unsigned long, flags); \
  10360. + (void) flags; \
  10361. + rt_read_unlock(lock); \
  10362. + } while (0)
  10363. +
  10364. +#define write_unlock_irqrestore(lock, flags) \
  10365. + do { \
  10366. + typecheck(unsigned long, flags); \
  10367. + (void) flags; \
  10368. + rt_write_unlock(lock); \
  10369. + } while (0)
  10370. +
  10371. +#endif
  10372. diff -Nur linux-4.1.13.orig/include/linux/rwlock_types.h linux-4.1.13/include/linux/rwlock_types.h
  10373. --- linux-4.1.13.orig/include/linux/rwlock_types.h 2015-11-09 23:34:10.000000000 +0100
  10374. +++ linux-4.1.13/include/linux/rwlock_types.h 2015-11-29 09:23:09.609614185 +0100
  10375. @@ -1,6 +1,10 @@
  10376. #ifndef __LINUX_RWLOCK_TYPES_H
  10377. #define __LINUX_RWLOCK_TYPES_H
  10378. +#if !defined(__LINUX_SPINLOCK_TYPES_H)
  10379. +# error "Do not include directly, include spinlock_types.h"
  10380. +#endif
  10381. +
  10382. /*
  10383. * include/linux/rwlock_types.h - generic rwlock type definitions
  10384. * and initializers
  10385. @@ -43,6 +47,7 @@
  10386. RW_DEP_MAP_INIT(lockname) }
  10387. #endif
  10388. -#define DEFINE_RWLOCK(x) rwlock_t x = __RW_LOCK_UNLOCKED(x)
  10389. +#define DEFINE_RWLOCK(name) \
  10390. + rwlock_t name __cacheline_aligned_in_smp = __RW_LOCK_UNLOCKED(name)
  10391. #endif /* __LINUX_RWLOCK_TYPES_H */
  10392. diff -Nur linux-4.1.13.orig/include/linux/rwlock_types_rt.h linux-4.1.13/include/linux/rwlock_types_rt.h
  10393. --- linux-4.1.13.orig/include/linux/rwlock_types_rt.h 1970-01-01 01:00:00.000000000 +0100
  10394. +++ linux-4.1.13/include/linux/rwlock_types_rt.h 2015-11-29 09:23:09.609614185 +0100
  10395. @@ -0,0 +1,33 @@
  10396. +#ifndef __LINUX_RWLOCK_TYPES_RT_H
  10397. +#define __LINUX_RWLOCK_TYPES_RT_H
  10398. +
  10399. +#ifndef __LINUX_SPINLOCK_TYPES_H
  10400. +#error "Do not include directly. Include spinlock_types.h instead"
  10401. +#endif
  10402. +
  10403. +/*
  10404. + * rwlocks - rtmutex which allows single reader recursion
  10405. + */
  10406. +typedef struct {
  10407. + struct rt_mutex lock;
  10408. + int read_depth;
  10409. + unsigned int break_lock;
  10410. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10411. + struct lockdep_map dep_map;
  10412. +#endif
  10413. +} rwlock_t;
  10414. +
  10415. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10416. +# define RW_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  10417. +#else
  10418. +# define RW_DEP_MAP_INIT(lockname)
  10419. +#endif
  10420. +
  10421. +#define __RW_LOCK_UNLOCKED(name) \
  10422. + { .lock = __RT_MUTEX_INITIALIZER_SAVE_STATE(name.lock), \
  10423. + RW_DEP_MAP_INIT(name) }
  10424. +
  10425. +#define DEFINE_RWLOCK(name) \
  10426. + rwlock_t name __cacheline_aligned_in_smp = __RW_LOCK_UNLOCKED(name)
  10427. +
  10428. +#endif
  10429. diff -Nur linux-4.1.13.orig/include/linux/rwsem.h linux-4.1.13/include/linux/rwsem.h
  10430. --- linux-4.1.13.orig/include/linux/rwsem.h 2015-11-09 23:34:10.000000000 +0100
  10431. +++ linux-4.1.13/include/linux/rwsem.h 2015-11-29 09:23:09.609614185 +0100
  10432. @@ -18,6 +18,10 @@
  10433. #include <linux/osq_lock.h>
  10434. #endif
  10435. +#ifdef CONFIG_PREEMPT_RT_FULL
  10436. +#include <linux/rwsem_rt.h>
  10437. +#else /* PREEMPT_RT_FULL */
  10438. +
  10439. struct rw_semaphore;
  10440. #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
  10441. @@ -177,4 +181,6 @@
  10442. # define up_read_non_owner(sem) up_read(sem)
  10443. #endif
  10444. +#endif /* !PREEMPT_RT_FULL */
  10445. +
  10446. #endif /* _LINUX_RWSEM_H */
  10447. diff -Nur linux-4.1.13.orig/include/linux/rwsem_rt.h linux-4.1.13/include/linux/rwsem_rt.h
  10448. --- linux-4.1.13.orig/include/linux/rwsem_rt.h 1970-01-01 01:00:00.000000000 +0100
  10449. +++ linux-4.1.13/include/linux/rwsem_rt.h 2015-11-29 09:23:09.609614185 +0100
  10450. @@ -0,0 +1,140 @@
  10451. +#ifndef _LINUX_RWSEM_RT_H
  10452. +#define _LINUX_RWSEM_RT_H
  10453. +
  10454. +#ifndef _LINUX_RWSEM_H
  10455. +#error "Include rwsem.h"
  10456. +#endif
  10457. +
  10458. +/*
  10459. + * RW-semaphores are a spinlock plus a reader-depth count.
  10460. + *
  10461. + * Note that the semantics are different from the usual
  10462. + * Linux rw-sems, in PREEMPT_RT mode we do not allow
  10463. + * multiple readers to hold the lock at once, we only allow
  10464. + * a read-lock owner to read-lock recursively. This is
  10465. + * better for latency, makes the implementation inherently
  10466. + * fair and makes it simpler as well.
  10467. + */
  10468. +
  10469. +#include <linux/rtmutex.h>
  10470. +
  10471. +struct rw_semaphore {
  10472. + struct rt_mutex lock;
  10473. + int read_depth;
  10474. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10475. + struct lockdep_map dep_map;
  10476. +#endif
  10477. +};
  10478. +
  10479. +#define __RWSEM_INITIALIZER(name) \
  10480. + { .lock = __RT_MUTEX_INITIALIZER(name.lock), \
  10481. + RW_DEP_MAP_INIT(name) }
  10482. +
  10483. +#define DECLARE_RWSEM(lockname) \
  10484. + struct rw_semaphore lockname = __RWSEM_INITIALIZER(lockname)
  10485. +
  10486. +extern void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  10487. + struct lock_class_key *key);
  10488. +
  10489. +#define __rt_init_rwsem(sem, name, key) \
  10490. + do { \
  10491. + rt_mutex_init(&(sem)->lock); \
  10492. + __rt_rwsem_init((sem), (name), (key));\
  10493. + } while (0)
  10494. +
  10495. +#define __init_rwsem(sem, name, key) __rt_init_rwsem(sem, name, key)
  10496. +
  10497. +# define rt_init_rwsem(sem) \
  10498. +do { \
  10499. + static struct lock_class_key __key; \
  10500. + \
  10501. + __rt_init_rwsem((sem), #sem, &__key); \
  10502. +} while (0)
  10503. +
  10504. +extern void rt_down_write(struct rw_semaphore *rwsem);
  10505. +extern void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass);
  10506. +extern void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass);
  10507. +extern void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  10508. + struct lockdep_map *nest);
  10509. +extern void rt_down_read(struct rw_semaphore *rwsem);
  10510. +extern int rt_down_write_trylock(struct rw_semaphore *rwsem);
  10511. +extern int rt_down_read_trylock(struct rw_semaphore *rwsem);
  10512. +extern void __rt_up_read(struct rw_semaphore *rwsem);
  10513. +extern void rt_up_read(struct rw_semaphore *rwsem);
  10514. +extern void rt_up_write(struct rw_semaphore *rwsem);
  10515. +extern void rt_downgrade_write(struct rw_semaphore *rwsem);
  10516. +
  10517. +#define init_rwsem(sem) rt_init_rwsem(sem)
  10518. +#define rwsem_is_locked(s) rt_mutex_is_locked(&(s)->lock)
  10519. +
  10520. +static inline int rwsem_is_contended(struct rw_semaphore *sem)
  10521. +{
  10522. + /* rt_mutex_has_waiters() */
  10523. + return !RB_EMPTY_ROOT(&sem->lock.waiters);
  10524. +}
  10525. +
  10526. +static inline void down_read(struct rw_semaphore *sem)
  10527. +{
  10528. + rt_down_read(sem);
  10529. +}
  10530. +
  10531. +static inline int down_read_trylock(struct rw_semaphore *sem)
  10532. +{
  10533. + return rt_down_read_trylock(sem);
  10534. +}
  10535. +
  10536. +static inline void down_write(struct rw_semaphore *sem)
  10537. +{
  10538. + rt_down_write(sem);
  10539. +}
  10540. +
  10541. +static inline int down_write_trylock(struct rw_semaphore *sem)
  10542. +{
  10543. + return rt_down_write_trylock(sem);
  10544. +}
  10545. +
  10546. +static inline void __up_read(struct rw_semaphore *sem)
  10547. +{
  10548. + __rt_up_read(sem);
  10549. +}
  10550. +
  10551. +static inline void up_read(struct rw_semaphore *sem)
  10552. +{
  10553. + rt_up_read(sem);
  10554. +}
  10555. +
  10556. +static inline void up_write(struct rw_semaphore *sem)
  10557. +{
  10558. + rt_up_write(sem);
  10559. +}
  10560. +
  10561. +static inline void downgrade_write(struct rw_semaphore *sem)
  10562. +{
  10563. + rt_downgrade_write(sem);
  10564. +}
  10565. +
  10566. +static inline void down_read_nested(struct rw_semaphore *sem, int subclass)
  10567. +{
  10568. + return rt_down_read_nested(sem, subclass);
  10569. +}
  10570. +
  10571. +static inline void down_write_nested(struct rw_semaphore *sem, int subclass)
  10572. +{
  10573. + rt_down_write_nested(sem, subclass);
  10574. +}
  10575. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10576. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  10577. + struct rw_semaphore *nest_lock)
  10578. +{
  10579. + rt_down_write_nested_lock(sem, &nest_lock->dep_map);
  10580. +}
  10581. +
  10582. +#else
  10583. +
  10584. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  10585. + struct rw_semaphore *nest_lock)
  10586. +{
  10587. + rt_down_write_nested_lock(sem, NULL);
  10588. +}
  10589. +#endif
  10590. +#endif
  10591. diff -Nur linux-4.1.13.orig/include/linux/sched.h linux-4.1.13/include/linux/sched.h
  10592. --- linux-4.1.13.orig/include/linux/sched.h 2015-11-09 23:34:10.000000000 +0100
  10593. +++ linux-4.1.13/include/linux/sched.h 2015-11-29 09:23:09.617613651 +0100
  10594. @@ -26,6 +26,7 @@
  10595. #include <linux/nodemask.h>
  10596. #include <linux/mm_types.h>
  10597. #include <linux/preempt_mask.h>
  10598. +#include <asm/kmap_types.h>
  10599. #include <asm/page.h>
  10600. #include <asm/ptrace.h>
  10601. @@ -234,10 +235,7 @@
  10602. TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
  10603. __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
  10604. -#define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
  10605. #define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
  10606. -#define task_is_stopped_or_traced(task) \
  10607. - ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
  10608. #define task_contributes_to_load(task) \
  10609. ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
  10610. (task->flags & PF_FROZEN) == 0)
  10611. @@ -302,6 +300,11 @@
  10612. #endif
  10613. +#define __set_current_state_no_track(state_value) \
  10614. + do { current->state = (state_value); } while (0)
  10615. +#define set_current_state_no_track(state_value) \
  10616. + set_mb(current->state, (state_value))
  10617. +
  10618. /* Task command name length */
  10619. #define TASK_COMM_LEN 16
  10620. @@ -900,6 +903,50 @@
  10621. #define SCHED_CAPACITY_SCALE (1L << SCHED_CAPACITY_SHIFT)
  10622. /*
  10623. + * Wake-queues are lists of tasks with a pending wakeup, whose
  10624. + * callers have already marked the task as woken internally,
  10625. + * and can thus carry on. A common use case is being able to
  10626. + * do the wakeups once the corresponding user lock as been
  10627. + * released.
  10628. + *
  10629. + * We hold reference to each task in the list across the wakeup,
  10630. + * thus guaranteeing that the memory is still valid by the time
  10631. + * the actual wakeups are performed in wake_up_q().
  10632. + *
  10633. + * One per task suffices, because there's never a need for a task to be
  10634. + * in two wake queues simultaneously; it is forbidden to abandon a task
  10635. + * in a wake queue (a call to wake_up_q() _must_ follow), so if a task is
  10636. + * already in a wake queue, the wakeup will happen soon and the second
  10637. + * waker can just skip it.
  10638. + *
  10639. + * The WAKE_Q macro declares and initializes the list head.
  10640. + * wake_up_q() does NOT reinitialize the list; it's expected to be
  10641. + * called near the end of a function, where the fact that the queue is
  10642. + * not used again will be easy to see by inspection.
  10643. + *
  10644. + * Note that this can cause spurious wakeups. schedule() callers
  10645. + * must ensure the call is done inside a loop, confirming that the
  10646. + * wakeup condition has in fact occurred.
  10647. + */
  10648. +struct wake_q_node {
  10649. + struct wake_q_node *next;
  10650. +};
  10651. +
  10652. +struct wake_q_head {
  10653. + struct wake_q_node *first;
  10654. + struct wake_q_node **lastp;
  10655. +};
  10656. +
  10657. +#define WAKE_Q_TAIL ((struct wake_q_node *) 0x01)
  10658. +
  10659. +#define WAKE_Q(name) \
  10660. + struct wake_q_head name = { WAKE_Q_TAIL, &name.first }
  10661. +
  10662. +extern void wake_q_add(struct wake_q_head *head,
  10663. + struct task_struct *task);
  10664. +extern void wake_up_q(struct wake_q_head *head);
  10665. +
  10666. +/*
  10667. * sched-domains (multiprocessor balancing) declarations:
  10668. */
  10669. #ifdef CONFIG_SMP
  10670. @@ -1291,6 +1338,7 @@
  10671. struct task_struct {
  10672. volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
  10673. + volatile long saved_state; /* saved state for "spinlock sleepers" */
  10674. void *stack;
  10675. atomic_t usage;
  10676. unsigned int flags; /* per process flags, defined below */
  10677. @@ -1327,6 +1375,12 @@
  10678. #endif
  10679. unsigned int policy;
  10680. +#ifdef CONFIG_PREEMPT_RT_FULL
  10681. + int migrate_disable;
  10682. +# ifdef CONFIG_SCHED_DEBUG
  10683. + int migrate_disable_atomic;
  10684. +# endif
  10685. +#endif
  10686. int nr_cpus_allowed;
  10687. cpumask_t cpus_allowed;
  10688. @@ -1434,7 +1488,8 @@
  10689. struct cputime prev_cputime;
  10690. #endif
  10691. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  10692. - seqlock_t vtime_seqlock;
  10693. + raw_spinlock_t vtime_lock;
  10694. + seqcount_t vtime_seq;
  10695. unsigned long long vtime_snap;
  10696. enum {
  10697. VTIME_SLEEPING = 0,
  10698. @@ -1450,6 +1505,9 @@
  10699. struct task_cputime cputime_expires;
  10700. struct list_head cpu_timers[3];
  10701. +#ifdef CONFIG_PREEMPT_RT_BASE
  10702. + struct task_struct *posix_timer_list;
  10703. +#endif
  10704. /* process credentials */
  10705. const struct cred __rcu *real_cred; /* objective and real subjective task
  10706. @@ -1482,10 +1540,15 @@
  10707. /* signal handlers */
  10708. struct signal_struct *signal;
  10709. struct sighand_struct *sighand;
  10710. + struct sigqueue *sigqueue_cache;
  10711. sigset_t blocked, real_blocked;
  10712. sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
  10713. struct sigpending pending;
  10714. +#ifdef CONFIG_PREEMPT_RT_FULL
  10715. + /* TODO: move me into ->restart_block ? */
  10716. + struct siginfo forced_info;
  10717. +#endif
  10718. unsigned long sas_ss_sp;
  10719. size_t sas_ss_size;
  10720. @@ -1511,6 +1574,8 @@
  10721. /* Protection of the PI data structures: */
  10722. raw_spinlock_t pi_lock;
  10723. + struct wake_q_node wake_q;
  10724. +
  10725. #ifdef CONFIG_RT_MUTEXES
  10726. /* PI waiters blocked on a rt_mutex held by this task */
  10727. struct rb_root pi_waiters;
  10728. @@ -1705,6 +1770,12 @@
  10729. unsigned long trace;
  10730. /* bitmask and counter of trace recursion */
  10731. unsigned long trace_recursion;
  10732. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  10733. + u64 preempt_timestamp_hist;
  10734. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  10735. + long timer_offset;
  10736. +#endif
  10737. +#endif
  10738. #endif /* CONFIG_TRACING */
  10739. #ifdef CONFIG_MEMCG
  10740. struct memcg_oom_info {
  10741. @@ -1721,14 +1792,23 @@
  10742. unsigned int sequential_io;
  10743. unsigned int sequential_io_avg;
  10744. #endif
  10745. +#ifdef CONFIG_PREEMPT_RT_BASE
  10746. + struct rcu_head put_rcu;
  10747. + int softirq_nestcnt;
  10748. + unsigned int softirqs_raised;
  10749. +#endif
  10750. +#ifdef CONFIG_PREEMPT_RT_FULL
  10751. +# if defined CONFIG_HIGHMEM || defined CONFIG_X86_32
  10752. + int kmap_idx;
  10753. + pte_t kmap_pte[KM_TYPE_NR];
  10754. +# endif
  10755. +#endif
  10756. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  10757. unsigned long task_state_change;
  10758. #endif
  10759. + int pagefault_disabled;
  10760. };
  10761. -/* Future-safe accessor for struct task_struct's cpus_allowed. */
  10762. -#define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
  10763. -
  10764. #define TNF_MIGRATED 0x01
  10765. #define TNF_NO_GROUP 0x02
  10766. #define TNF_SHARED 0x04
  10767. @@ -1917,6 +1997,15 @@
  10768. extern void free_task(struct task_struct *tsk);
  10769. #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
  10770. +#ifdef CONFIG_PREEMPT_RT_BASE
  10771. +extern void __put_task_struct_cb(struct rcu_head *rhp);
  10772. +
  10773. +static inline void put_task_struct(struct task_struct *t)
  10774. +{
  10775. + if (atomic_dec_and_test(&t->usage))
  10776. + call_rcu(&t->put_rcu, __put_task_struct_cb);
  10777. +}
  10778. +#else
  10779. extern void __put_task_struct(struct task_struct *t);
  10780. static inline void put_task_struct(struct task_struct *t)
  10781. @@ -1924,6 +2013,7 @@
  10782. if (atomic_dec_and_test(&t->usage))
  10783. __put_task_struct(t);
  10784. }
  10785. +#endif
  10786. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  10787. extern void task_cputime(struct task_struct *t,
  10788. @@ -1962,6 +2052,7 @@
  10789. /*
  10790. * Per process flags
  10791. */
  10792. +#define PF_IN_SOFTIRQ 0x00000001 /* Task is serving softirq */
  10793. #define PF_EXITING 0x00000004 /* getting shut down */
  10794. #define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
  10795. #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
  10796. @@ -2126,6 +2217,10 @@
  10797. extern int set_cpus_allowed_ptr(struct task_struct *p,
  10798. const struct cpumask *new_mask);
  10799. +int migrate_me(void);
  10800. +void tell_sched_cpu_down_begin(int cpu);
  10801. +void tell_sched_cpu_down_done(int cpu);
  10802. +
  10803. #else
  10804. static inline void do_set_cpus_allowed(struct task_struct *p,
  10805. const struct cpumask *new_mask)
  10806. @@ -2138,6 +2233,9 @@
  10807. return -EINVAL;
  10808. return 0;
  10809. }
  10810. +static inline int migrate_me(void) { return 0; }
  10811. +static inline void tell_sched_cpu_down_begin(int cpu) { }
  10812. +static inline void tell_sched_cpu_down_done(int cpu) { }
  10813. #endif
  10814. #ifdef CONFIG_NO_HZ_COMMON
  10815. @@ -2354,6 +2452,7 @@
  10816. extern int wake_up_state(struct task_struct *tsk, unsigned int state);
  10817. extern int wake_up_process(struct task_struct *tsk);
  10818. +extern int wake_up_lock_sleeper(struct task_struct * tsk);
  10819. extern void wake_up_new_task(struct task_struct *tsk);
  10820. #ifdef CONFIG_SMP
  10821. extern void kick_process(struct task_struct *tsk);
  10822. @@ -2470,12 +2569,24 @@
  10823. /* mmdrop drops the mm and the page tables */
  10824. extern void __mmdrop(struct mm_struct *);
  10825. +
  10826. static inline void mmdrop(struct mm_struct * mm)
  10827. {
  10828. if (unlikely(atomic_dec_and_test(&mm->mm_count)))
  10829. __mmdrop(mm);
  10830. }
  10831. +#ifdef CONFIG_PREEMPT_RT_BASE
  10832. +extern void __mmdrop_delayed(struct rcu_head *rhp);
  10833. +static inline void mmdrop_delayed(struct mm_struct *mm)
  10834. +{
  10835. + if (atomic_dec_and_test(&mm->mm_count))
  10836. + call_rcu(&mm->delayed_drop, __mmdrop_delayed);
  10837. +}
  10838. +#else
  10839. +# define mmdrop_delayed(mm) mmdrop(mm)
  10840. +#endif
  10841. +
  10842. /* mmput gets rid of the mappings and all user-space */
  10843. extern void mmput(struct mm_struct *);
  10844. /* Grab a reference to a task's mm, if it is not already going away */
  10845. @@ -2787,6 +2898,43 @@
  10846. return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
  10847. }
  10848. +#ifdef CONFIG_PREEMPT_LAZY
  10849. +static inline void set_tsk_need_resched_lazy(struct task_struct *tsk)
  10850. +{
  10851. + set_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  10852. +}
  10853. +
  10854. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk)
  10855. +{
  10856. + clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  10857. +}
  10858. +
  10859. +static inline int test_tsk_need_resched_lazy(struct task_struct *tsk)
  10860. +{
  10861. + return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY));
  10862. +}
  10863. +
  10864. +static inline int need_resched_lazy(void)
  10865. +{
  10866. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  10867. +}
  10868. +
  10869. +static inline int need_resched_now(void)
  10870. +{
  10871. + return test_thread_flag(TIF_NEED_RESCHED);
  10872. +}
  10873. +
  10874. +#else
  10875. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk) { }
  10876. +static inline int need_resched_lazy(void) { return 0; }
  10877. +
  10878. +static inline int need_resched_now(void)
  10879. +{
  10880. + return test_thread_flag(TIF_NEED_RESCHED);
  10881. +}
  10882. +
  10883. +#endif
  10884. +
  10885. static inline int restart_syscall(void)
  10886. {
  10887. set_tsk_thread_flag(current, TIF_SIGPENDING);
  10888. @@ -2818,6 +2966,51 @@
  10889. return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
  10890. }
  10891. +static inline bool __task_is_stopped_or_traced(struct task_struct *task)
  10892. +{
  10893. + if (task->state & (__TASK_STOPPED | __TASK_TRACED))
  10894. + return true;
  10895. +#ifdef CONFIG_PREEMPT_RT_FULL
  10896. + if (task->saved_state & (__TASK_STOPPED | __TASK_TRACED))
  10897. + return true;
  10898. +#endif
  10899. + return false;
  10900. +}
  10901. +
  10902. +static inline bool task_is_stopped_or_traced(struct task_struct *task)
  10903. +{
  10904. + bool traced_stopped;
  10905. +
  10906. +#ifdef CONFIG_PREEMPT_RT_FULL
  10907. + unsigned long flags;
  10908. +
  10909. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  10910. + traced_stopped = __task_is_stopped_or_traced(task);
  10911. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  10912. +#else
  10913. + traced_stopped = __task_is_stopped_or_traced(task);
  10914. +#endif
  10915. + return traced_stopped;
  10916. +}
  10917. +
  10918. +static inline bool task_is_traced(struct task_struct *task)
  10919. +{
  10920. + bool traced = false;
  10921. +
  10922. + if (task->state & __TASK_TRACED)
  10923. + return true;
  10924. +#ifdef CONFIG_PREEMPT_RT_FULL
  10925. + /* in case the task is sleeping on tasklist_lock */
  10926. + raw_spin_lock_irq(&task->pi_lock);
  10927. + if (task->state & __TASK_TRACED)
  10928. + traced = true;
  10929. + else if (task->saved_state & __TASK_TRACED)
  10930. + traced = true;
  10931. + raw_spin_unlock_irq(&task->pi_lock);
  10932. +#endif
  10933. + return traced;
  10934. +}
  10935. +
  10936. /*
  10937. * cond_resched() and cond_resched_lock(): latency reduction via
  10938. * explicit rescheduling in places that are safe. The return
  10939. @@ -2839,12 +3032,16 @@
  10940. __cond_resched_lock(lock); \
  10941. })
  10942. +#ifndef CONFIG_PREEMPT_RT_FULL
  10943. extern int __cond_resched_softirq(void);
  10944. #define cond_resched_softirq() ({ \
  10945. ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
  10946. __cond_resched_softirq(); \
  10947. })
  10948. +#else
  10949. +# define cond_resched_softirq() cond_resched()
  10950. +#endif
  10951. static inline void cond_resched_rcu(void)
  10952. {
  10953. @@ -3011,6 +3208,26 @@
  10954. #endif /* CONFIG_SMP */
  10955. +static inline int __migrate_disabled(struct task_struct *p)
  10956. +{
  10957. +#ifdef CONFIG_PREEMPT_RT_FULL
  10958. + return p->migrate_disable;
  10959. +#else
  10960. + return 0;
  10961. +#endif
  10962. +}
  10963. +
  10964. +/* Future-safe accessor for struct task_struct's cpus_allowed. */
  10965. +static inline const struct cpumask *tsk_cpus_allowed(struct task_struct *p)
  10966. +{
  10967. +#ifdef CONFIG_PREEMPT_RT_FULL
  10968. + if (p->migrate_disable)
  10969. + return cpumask_of(task_cpu(p));
  10970. +#endif
  10971. +
  10972. + return &p->cpus_allowed;
  10973. +}
  10974. +
  10975. extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
  10976. extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
  10977. diff -Nur linux-4.1.13.orig/include/linux/seqlock.h linux-4.1.13/include/linux/seqlock.h
  10978. --- linux-4.1.13.orig/include/linux/seqlock.h 2015-11-09 23:34:10.000000000 +0100
  10979. +++ linux-4.1.13/include/linux/seqlock.h 2015-11-29 09:23:09.617613651 +0100
  10980. @@ -219,20 +219,30 @@
  10981. return __read_seqcount_retry(s, start);
  10982. }
  10983. -
  10984. -
  10985. -static inline void raw_write_seqcount_begin(seqcount_t *s)
  10986. +static inline void __raw_write_seqcount_begin(seqcount_t *s)
  10987. {
  10988. s->sequence++;
  10989. smp_wmb();
  10990. }
  10991. -static inline void raw_write_seqcount_end(seqcount_t *s)
  10992. +static inline void raw_write_seqcount_begin(seqcount_t *s)
  10993. +{
  10994. + preempt_disable_rt();
  10995. + __raw_write_seqcount_begin(s);
  10996. +}
  10997. +
  10998. +static inline void __raw_write_seqcount_end(seqcount_t *s)
  10999. {
  11000. smp_wmb();
  11001. s->sequence++;
  11002. }
  11003. +static inline void raw_write_seqcount_end(seqcount_t *s)
  11004. +{
  11005. + __raw_write_seqcount_end(s);
  11006. + preempt_enable_rt();
  11007. +}
  11008. +
  11009. /*
  11010. * raw_write_seqcount_latch - redirect readers to even/odd copy
  11011. * @s: pointer to seqcount_t
  11012. @@ -305,10 +315,32 @@
  11013. /*
  11014. * Read side functions for starting and finalizing a read side section.
  11015. */
  11016. +#ifndef CONFIG_PREEMPT_RT_FULL
  11017. static inline unsigned read_seqbegin(const seqlock_t *sl)
  11018. {
  11019. return read_seqcount_begin(&sl->seqcount);
  11020. }
  11021. +#else
  11022. +/*
  11023. + * Starvation safe read side for RT
  11024. + */
  11025. +static inline unsigned read_seqbegin(seqlock_t *sl)
  11026. +{
  11027. + unsigned ret;
  11028. +
  11029. +repeat:
  11030. + ret = ACCESS_ONCE(sl->seqcount.sequence);
  11031. + if (unlikely(ret & 1)) {
  11032. + /*
  11033. + * Take the lock and let the writer proceed (i.e. evtl
  11034. + * boost it), otherwise we could loop here forever.
  11035. + */
  11036. + spin_unlock_wait(&sl->lock);
  11037. + goto repeat;
  11038. + }
  11039. + return ret;
  11040. +}
  11041. +#endif
  11042. static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  11043. {
  11044. @@ -323,36 +355,36 @@
  11045. static inline void write_seqlock(seqlock_t *sl)
  11046. {
  11047. spin_lock(&sl->lock);
  11048. - write_seqcount_begin(&sl->seqcount);
  11049. + __raw_write_seqcount_begin(&sl->seqcount);
  11050. }
  11051. static inline void write_sequnlock(seqlock_t *sl)
  11052. {
  11053. - write_seqcount_end(&sl->seqcount);
  11054. + __raw_write_seqcount_end(&sl->seqcount);
  11055. spin_unlock(&sl->lock);
  11056. }
  11057. static inline void write_seqlock_bh(seqlock_t *sl)
  11058. {
  11059. spin_lock_bh(&sl->lock);
  11060. - write_seqcount_begin(&sl->seqcount);
  11061. + __raw_write_seqcount_begin(&sl->seqcount);
  11062. }
  11063. static inline void write_sequnlock_bh(seqlock_t *sl)
  11064. {
  11065. - write_seqcount_end(&sl->seqcount);
  11066. + __raw_write_seqcount_end(&sl->seqcount);
  11067. spin_unlock_bh(&sl->lock);
  11068. }
  11069. static inline void write_seqlock_irq(seqlock_t *sl)
  11070. {
  11071. spin_lock_irq(&sl->lock);
  11072. - write_seqcount_begin(&sl->seqcount);
  11073. + __raw_write_seqcount_begin(&sl->seqcount);
  11074. }
  11075. static inline void write_sequnlock_irq(seqlock_t *sl)
  11076. {
  11077. - write_seqcount_end(&sl->seqcount);
  11078. + __raw_write_seqcount_end(&sl->seqcount);
  11079. spin_unlock_irq(&sl->lock);
  11080. }
  11081. @@ -361,7 +393,7 @@
  11082. unsigned long flags;
  11083. spin_lock_irqsave(&sl->lock, flags);
  11084. - write_seqcount_begin(&sl->seqcount);
  11085. + __raw_write_seqcount_begin(&sl->seqcount);
  11086. return flags;
  11087. }
  11088. @@ -371,7 +403,7 @@
  11089. static inline void
  11090. write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
  11091. {
  11092. - write_seqcount_end(&sl->seqcount);
  11093. + __raw_write_seqcount_end(&sl->seqcount);
  11094. spin_unlock_irqrestore(&sl->lock, flags);
  11095. }
  11096. diff -Nur linux-4.1.13.orig/include/linux/signal.h linux-4.1.13/include/linux/signal.h
  11097. --- linux-4.1.13.orig/include/linux/signal.h 2015-11-09 23:34:10.000000000 +0100
  11098. +++ linux-4.1.13/include/linux/signal.h 2015-11-29 09:23:09.617613651 +0100
  11099. @@ -218,6 +218,7 @@
  11100. }
  11101. extern void flush_sigqueue(struct sigpending *queue);
  11102. +extern void flush_task_sigqueue(struct task_struct *tsk);
  11103. /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
  11104. static inline int valid_signal(unsigned long sig)
  11105. diff -Nur linux-4.1.13.orig/include/linux/skbuff.h linux-4.1.13/include/linux/skbuff.h
  11106. --- linux-4.1.13.orig/include/linux/skbuff.h 2015-11-09 23:34:10.000000000 +0100
  11107. +++ linux-4.1.13/include/linux/skbuff.h 2015-11-29 09:23:09.617613651 +0100
  11108. @@ -187,6 +187,7 @@
  11109. __u32 qlen;
  11110. spinlock_t lock;
  11111. + raw_spinlock_t raw_lock;
  11112. };
  11113. struct sk_buff;
  11114. @@ -1336,6 +1337,12 @@
  11115. __skb_queue_head_init(list);
  11116. }
  11117. +static inline void skb_queue_head_init_raw(struct sk_buff_head *list)
  11118. +{
  11119. + raw_spin_lock_init(&list->raw_lock);
  11120. + __skb_queue_head_init(list);
  11121. +}
  11122. +
  11123. static inline void skb_queue_head_init_class(struct sk_buff_head *list,
  11124. struct lock_class_key *class)
  11125. {
  11126. diff -Nur linux-4.1.13.orig/include/linux/smp.h linux-4.1.13/include/linux/smp.h
  11127. --- linux-4.1.13.orig/include/linux/smp.h 2015-11-09 23:34:10.000000000 +0100
  11128. +++ linux-4.1.13/include/linux/smp.h 2015-11-29 09:23:09.617613651 +0100
  11129. @@ -185,6 +185,9 @@
  11130. #define get_cpu() ({ preempt_disable(); smp_processor_id(); })
  11131. #define put_cpu() preempt_enable()
  11132. +#define get_cpu_light() ({ migrate_disable(); smp_processor_id(); })
  11133. +#define put_cpu_light() migrate_enable()
  11134. +
  11135. /*
  11136. * Callback to arch code if there's nosmp or maxcpus=0 on the
  11137. * boot command line:
  11138. diff -Nur linux-4.1.13.orig/include/linux/spinlock_api_smp.h linux-4.1.13/include/linux/spinlock_api_smp.h
  11139. --- linux-4.1.13.orig/include/linux/spinlock_api_smp.h 2015-11-09 23:34:10.000000000 +0100
  11140. +++ linux-4.1.13/include/linux/spinlock_api_smp.h 2015-11-29 09:23:09.617613651 +0100
  11141. @@ -189,6 +189,8 @@
  11142. return 0;
  11143. }
  11144. -#include <linux/rwlock_api_smp.h>
  11145. +#ifndef CONFIG_PREEMPT_RT_FULL
  11146. +# include <linux/rwlock_api_smp.h>
  11147. +#endif
  11148. #endif /* __LINUX_SPINLOCK_API_SMP_H */
  11149. diff -Nur linux-4.1.13.orig/include/linux/spinlock.h linux-4.1.13/include/linux/spinlock.h
  11150. --- linux-4.1.13.orig/include/linux/spinlock.h 2015-11-09 23:34:10.000000000 +0100
  11151. +++ linux-4.1.13/include/linux/spinlock.h 2015-11-29 09:23:09.617613651 +0100
  11152. @@ -281,7 +281,11 @@
  11153. #define raw_spin_can_lock(lock) (!raw_spin_is_locked(lock))
  11154. /* Include rwlock functions */
  11155. -#include <linux/rwlock.h>
  11156. +#ifdef CONFIG_PREEMPT_RT_FULL
  11157. +# include <linux/rwlock_rt.h>
  11158. +#else
  11159. +# include <linux/rwlock.h>
  11160. +#endif
  11161. /*
  11162. * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
  11163. @@ -292,6 +296,10 @@
  11164. # include <linux/spinlock_api_up.h>
  11165. #endif
  11166. +#ifdef CONFIG_PREEMPT_RT_FULL
  11167. +# include <linux/spinlock_rt.h>
  11168. +#else /* PREEMPT_RT_FULL */
  11169. +
  11170. /*
  11171. * Map the spin_lock functions to the raw variants for PREEMPT_RT=n
  11172. */
  11173. @@ -426,4 +434,6 @@
  11174. #define atomic_dec_and_lock(atomic, lock) \
  11175. __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
  11176. +#endif /* !PREEMPT_RT_FULL */
  11177. +
  11178. #endif /* __LINUX_SPINLOCK_H */
  11179. diff -Nur linux-4.1.13.orig/include/linux/spinlock_rt.h linux-4.1.13/include/linux/spinlock_rt.h
  11180. --- linux-4.1.13.orig/include/linux/spinlock_rt.h 1970-01-01 01:00:00.000000000 +0100
  11181. +++ linux-4.1.13/include/linux/spinlock_rt.h 2015-11-29 09:23:09.617613651 +0100
  11182. @@ -0,0 +1,174 @@
  11183. +#ifndef __LINUX_SPINLOCK_RT_H
  11184. +#define __LINUX_SPINLOCK_RT_H
  11185. +
  11186. +#ifndef __LINUX_SPINLOCK_H
  11187. +#error Do not include directly. Use spinlock.h
  11188. +#endif
  11189. +
  11190. +#include <linux/bug.h>
  11191. +
  11192. +extern void
  11193. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key);
  11194. +
  11195. +#define spin_lock_init(slock) \
  11196. +do { \
  11197. + static struct lock_class_key __key; \
  11198. + \
  11199. + rt_mutex_init(&(slock)->lock); \
  11200. + __rt_spin_lock_init(slock, #slock, &__key); \
  11201. +} while (0)
  11202. +
  11203. +extern void __lockfunc rt_spin_lock(spinlock_t *lock);
  11204. +extern unsigned long __lockfunc rt_spin_lock_trace_flags(spinlock_t *lock);
  11205. +extern void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass);
  11206. +extern void __lockfunc rt_spin_unlock(spinlock_t *lock);
  11207. +extern void __lockfunc rt_spin_unlock_wait(spinlock_t *lock);
  11208. +extern int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags);
  11209. +extern int __lockfunc rt_spin_trylock_bh(spinlock_t *lock);
  11210. +extern int __lockfunc rt_spin_trylock(spinlock_t *lock);
  11211. +extern int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock);
  11212. +
  11213. +/*
  11214. + * lockdep-less calls, for derived types like rwlock:
  11215. + * (for trylock they can use rt_mutex_trylock() directly.
  11216. + */
  11217. +extern void __lockfunc __rt_spin_lock(struct rt_mutex *lock);
  11218. +extern void __lockfunc __rt_spin_unlock(struct rt_mutex *lock);
  11219. +extern int __lockfunc __rt_spin_trylock(struct rt_mutex *lock);
  11220. +
  11221. +#define spin_lock(lock) \
  11222. + do { \
  11223. + migrate_disable(); \
  11224. + rt_spin_lock(lock); \
  11225. + } while (0)
  11226. +
  11227. +#define spin_lock_bh(lock) \
  11228. + do { \
  11229. + local_bh_disable(); \
  11230. + migrate_disable(); \
  11231. + rt_spin_lock(lock); \
  11232. + } while (0)
  11233. +
  11234. +#define spin_lock_irq(lock) spin_lock(lock)
  11235. +
  11236. +#define spin_do_trylock(lock) __cond_lock(lock, rt_spin_trylock(lock))
  11237. +
  11238. +#define spin_trylock(lock) \
  11239. +({ \
  11240. + int __locked; \
  11241. + migrate_disable(); \
  11242. + __locked = spin_do_trylock(lock); \
  11243. + if (!__locked) \
  11244. + migrate_enable(); \
  11245. + __locked; \
  11246. +})
  11247. +
  11248. +#ifdef CONFIG_LOCKDEP
  11249. +# define spin_lock_nested(lock, subclass) \
  11250. + do { \
  11251. + migrate_disable(); \
  11252. + rt_spin_lock_nested(lock, subclass); \
  11253. + } while (0)
  11254. +
  11255. +#define spin_lock_bh_nested(lock, subclass) \
  11256. + do { \
  11257. + local_bh_disable(); \
  11258. + migrate_disable(); \
  11259. + rt_spin_lock_nested(lock, subclass); \
  11260. + } while (0)
  11261. +
  11262. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  11263. + do { \
  11264. + typecheck(unsigned long, flags); \
  11265. + flags = 0; \
  11266. + migrate_disable(); \
  11267. + rt_spin_lock_nested(lock, subclass); \
  11268. + } while (0)
  11269. +#else
  11270. +# define spin_lock_nested(lock, subclass) spin_lock(lock)
  11271. +# define spin_lock_bh_nested(lock, subclass) spin_lock_bh(lock)
  11272. +
  11273. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  11274. + do { \
  11275. + typecheck(unsigned long, flags); \
  11276. + flags = 0; \
  11277. + spin_lock(lock); \
  11278. + } while (0)
  11279. +#endif
  11280. +
  11281. +#define spin_lock_irqsave(lock, flags) \
  11282. + do { \
  11283. + typecheck(unsigned long, flags); \
  11284. + flags = 0; \
  11285. + spin_lock(lock); \
  11286. + } while (0)
  11287. +
  11288. +static inline unsigned long spin_lock_trace_flags(spinlock_t *lock)
  11289. +{
  11290. + unsigned long flags = 0;
  11291. +#ifdef CONFIG_TRACE_IRQFLAGS
  11292. + flags = rt_spin_lock_trace_flags(lock);
  11293. +#else
  11294. + spin_lock(lock); /* lock_local */
  11295. +#endif
  11296. + return flags;
  11297. +}
  11298. +
  11299. +/* FIXME: we need rt_spin_lock_nest_lock */
  11300. +#define spin_lock_nest_lock(lock, nest_lock) spin_lock_nested(lock, 0)
  11301. +
  11302. +#define spin_unlock(lock) \
  11303. + do { \
  11304. + rt_spin_unlock(lock); \
  11305. + migrate_enable(); \
  11306. + } while (0)
  11307. +
  11308. +#define spin_unlock_bh(lock) \
  11309. + do { \
  11310. + rt_spin_unlock(lock); \
  11311. + migrate_enable(); \
  11312. + local_bh_enable(); \
  11313. + } while (0)
  11314. +
  11315. +#define spin_unlock_irq(lock) spin_unlock(lock)
  11316. +
  11317. +#define spin_unlock_irqrestore(lock, flags) \
  11318. + do { \
  11319. + typecheck(unsigned long, flags); \
  11320. + (void) flags; \
  11321. + spin_unlock(lock); \
  11322. + } while (0)
  11323. +
  11324. +#define spin_trylock_bh(lock) __cond_lock(lock, rt_spin_trylock_bh(lock))
  11325. +#define spin_trylock_irq(lock) spin_trylock(lock)
  11326. +
  11327. +#define spin_trylock_irqsave(lock, flags) \
  11328. + rt_spin_trylock_irqsave(lock, &(flags))
  11329. +
  11330. +#define spin_unlock_wait(lock) rt_spin_unlock_wait(lock)
  11331. +
  11332. +#ifdef CONFIG_GENERIC_LOCKBREAK
  11333. +# define spin_is_contended(lock) ((lock)->break_lock)
  11334. +#else
  11335. +# define spin_is_contended(lock) (((void)(lock), 0))
  11336. +#endif
  11337. +
  11338. +static inline int spin_can_lock(spinlock_t *lock)
  11339. +{
  11340. + return !rt_mutex_is_locked(&lock->lock);
  11341. +}
  11342. +
  11343. +static inline int spin_is_locked(spinlock_t *lock)
  11344. +{
  11345. + return rt_mutex_is_locked(&lock->lock);
  11346. +}
  11347. +
  11348. +static inline void assert_spin_locked(spinlock_t *lock)
  11349. +{
  11350. + BUG_ON(!spin_is_locked(lock));
  11351. +}
  11352. +
  11353. +#define atomic_dec_and_lock(atomic, lock) \
  11354. + atomic_dec_and_spin_lock(atomic, lock)
  11355. +
  11356. +#endif
  11357. diff -Nur linux-4.1.13.orig/include/linux/spinlock_types.h linux-4.1.13/include/linux/spinlock_types.h
  11358. --- linux-4.1.13.orig/include/linux/spinlock_types.h 2015-11-09 23:34:10.000000000 +0100
  11359. +++ linux-4.1.13/include/linux/spinlock_types.h 2015-11-29 09:23:09.617613651 +0100
  11360. @@ -9,80 +9,15 @@
  11361. * Released under the General Public License (GPL).
  11362. */
  11363. -#if defined(CONFIG_SMP)
  11364. -# include <asm/spinlock_types.h>
  11365. -#else
  11366. -# include <linux/spinlock_types_up.h>
  11367. -#endif
  11368. -
  11369. -#include <linux/lockdep.h>
  11370. -
  11371. -typedef struct raw_spinlock {
  11372. - arch_spinlock_t raw_lock;
  11373. -#ifdef CONFIG_GENERIC_LOCKBREAK
  11374. - unsigned int break_lock;
  11375. -#endif
  11376. -#ifdef CONFIG_DEBUG_SPINLOCK
  11377. - unsigned int magic, owner_cpu;
  11378. - void *owner;
  11379. -#endif
  11380. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11381. - struct lockdep_map dep_map;
  11382. -#endif
  11383. -} raw_spinlock_t;
  11384. -
  11385. -#define SPINLOCK_MAGIC 0xdead4ead
  11386. -
  11387. -#define SPINLOCK_OWNER_INIT ((void *)-1L)
  11388. -
  11389. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11390. -# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  11391. -#else
  11392. -# define SPIN_DEP_MAP_INIT(lockname)
  11393. -#endif
  11394. +#include <linux/spinlock_types_raw.h>
  11395. -#ifdef CONFIG_DEBUG_SPINLOCK
  11396. -# define SPIN_DEBUG_INIT(lockname) \
  11397. - .magic = SPINLOCK_MAGIC, \
  11398. - .owner_cpu = -1, \
  11399. - .owner = SPINLOCK_OWNER_INIT,
  11400. +#ifndef CONFIG_PREEMPT_RT_FULL
  11401. +# include <linux/spinlock_types_nort.h>
  11402. +# include <linux/rwlock_types.h>
  11403. #else
  11404. -# define SPIN_DEBUG_INIT(lockname)
  11405. +# include <linux/rtmutex.h>
  11406. +# include <linux/spinlock_types_rt.h>
  11407. +# include <linux/rwlock_types_rt.h>
  11408. #endif
  11409. -#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  11410. - { \
  11411. - .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  11412. - SPIN_DEBUG_INIT(lockname) \
  11413. - SPIN_DEP_MAP_INIT(lockname) }
  11414. -
  11415. -#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  11416. - (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  11417. -
  11418. -#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  11419. -
  11420. -typedef struct spinlock {
  11421. - union {
  11422. - struct raw_spinlock rlock;
  11423. -
  11424. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11425. -# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  11426. - struct {
  11427. - u8 __padding[LOCK_PADSIZE];
  11428. - struct lockdep_map dep_map;
  11429. - };
  11430. -#endif
  11431. - };
  11432. -} spinlock_t;
  11433. -
  11434. -#define __SPIN_LOCK_INITIALIZER(lockname) \
  11435. - { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  11436. -
  11437. -#define __SPIN_LOCK_UNLOCKED(lockname) \
  11438. - (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  11439. -
  11440. -#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  11441. -
  11442. -#include <linux/rwlock_types.h>
  11443. -
  11444. #endif /* __LINUX_SPINLOCK_TYPES_H */
  11445. diff -Nur linux-4.1.13.orig/include/linux/spinlock_types_nort.h linux-4.1.13/include/linux/spinlock_types_nort.h
  11446. --- linux-4.1.13.orig/include/linux/spinlock_types_nort.h 1970-01-01 01:00:00.000000000 +0100
  11447. +++ linux-4.1.13/include/linux/spinlock_types_nort.h 2015-11-29 09:23:09.617613651 +0100
  11448. @@ -0,0 +1,33 @@
  11449. +#ifndef __LINUX_SPINLOCK_TYPES_NORT_H
  11450. +#define __LINUX_SPINLOCK_TYPES_NORT_H
  11451. +
  11452. +#ifndef __LINUX_SPINLOCK_TYPES_H
  11453. +#error "Do not include directly. Include spinlock_types.h instead"
  11454. +#endif
  11455. +
  11456. +/*
  11457. + * The non RT version maps spinlocks to raw_spinlocks
  11458. + */
  11459. +typedef struct spinlock {
  11460. + union {
  11461. + struct raw_spinlock rlock;
  11462. +
  11463. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11464. +# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  11465. + struct {
  11466. + u8 __padding[LOCK_PADSIZE];
  11467. + struct lockdep_map dep_map;
  11468. + };
  11469. +#endif
  11470. + };
  11471. +} spinlock_t;
  11472. +
  11473. +#define __SPIN_LOCK_INITIALIZER(lockname) \
  11474. + { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  11475. +
  11476. +#define __SPIN_LOCK_UNLOCKED(lockname) \
  11477. + (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  11478. +
  11479. +#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  11480. +
  11481. +#endif
  11482. diff -Nur linux-4.1.13.orig/include/linux/spinlock_types_raw.h linux-4.1.13/include/linux/spinlock_types_raw.h
  11483. --- linux-4.1.13.orig/include/linux/spinlock_types_raw.h 1970-01-01 01:00:00.000000000 +0100
  11484. +++ linux-4.1.13/include/linux/spinlock_types_raw.h 2015-11-29 09:23:09.617613651 +0100
  11485. @@ -0,0 +1,56 @@
  11486. +#ifndef __LINUX_SPINLOCK_TYPES_RAW_H
  11487. +#define __LINUX_SPINLOCK_TYPES_RAW_H
  11488. +
  11489. +#if defined(CONFIG_SMP)
  11490. +# include <asm/spinlock_types.h>
  11491. +#else
  11492. +# include <linux/spinlock_types_up.h>
  11493. +#endif
  11494. +
  11495. +#include <linux/lockdep.h>
  11496. +
  11497. +typedef struct raw_spinlock {
  11498. + arch_spinlock_t raw_lock;
  11499. +#ifdef CONFIG_GENERIC_LOCKBREAK
  11500. + unsigned int break_lock;
  11501. +#endif
  11502. +#ifdef CONFIG_DEBUG_SPINLOCK
  11503. + unsigned int magic, owner_cpu;
  11504. + void *owner;
  11505. +#endif
  11506. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11507. + struct lockdep_map dep_map;
  11508. +#endif
  11509. +} raw_spinlock_t;
  11510. +
  11511. +#define SPINLOCK_MAGIC 0xdead4ead
  11512. +
  11513. +#define SPINLOCK_OWNER_INIT ((void *)-1L)
  11514. +
  11515. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11516. +# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  11517. +#else
  11518. +# define SPIN_DEP_MAP_INIT(lockname)
  11519. +#endif
  11520. +
  11521. +#ifdef CONFIG_DEBUG_SPINLOCK
  11522. +# define SPIN_DEBUG_INIT(lockname) \
  11523. + .magic = SPINLOCK_MAGIC, \
  11524. + .owner_cpu = -1, \
  11525. + .owner = SPINLOCK_OWNER_INIT,
  11526. +#else
  11527. +# define SPIN_DEBUG_INIT(lockname)
  11528. +#endif
  11529. +
  11530. +#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  11531. + { \
  11532. + .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  11533. + SPIN_DEBUG_INIT(lockname) \
  11534. + SPIN_DEP_MAP_INIT(lockname) }
  11535. +
  11536. +#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  11537. + (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  11538. +
  11539. +#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  11540. +
  11541. +#endif
  11542. diff -Nur linux-4.1.13.orig/include/linux/spinlock_types_rt.h linux-4.1.13/include/linux/spinlock_types_rt.h
  11543. --- linux-4.1.13.orig/include/linux/spinlock_types_rt.h 1970-01-01 01:00:00.000000000 +0100
  11544. +++ linux-4.1.13/include/linux/spinlock_types_rt.h 2015-11-29 09:23:09.617613651 +0100
  11545. @@ -0,0 +1,51 @@
  11546. +#ifndef __LINUX_SPINLOCK_TYPES_RT_H
  11547. +#define __LINUX_SPINLOCK_TYPES_RT_H
  11548. +
  11549. +#ifndef __LINUX_SPINLOCK_TYPES_H
  11550. +#error "Do not include directly. Include spinlock_types.h instead"
  11551. +#endif
  11552. +
  11553. +#include <linux/cache.h>
  11554. +
  11555. +/*
  11556. + * PREEMPT_RT: spinlocks - an RT mutex plus lock-break field:
  11557. + */
  11558. +typedef struct spinlock {
  11559. + struct rt_mutex lock;
  11560. + unsigned int break_lock;
  11561. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11562. + struct lockdep_map dep_map;
  11563. +#endif
  11564. +} spinlock_t;
  11565. +
  11566. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  11567. +# define __RT_SPIN_INITIALIZER(name) \
  11568. + { \
  11569. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  11570. + .save_state = 1, \
  11571. + .file = __FILE__, \
  11572. + .line = __LINE__ , \
  11573. + }
  11574. +#else
  11575. +# define __RT_SPIN_INITIALIZER(name) \
  11576. + { \
  11577. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  11578. + .save_state = 1, \
  11579. + }
  11580. +#endif
  11581. +
  11582. +/*
  11583. +.wait_list = PLIST_HEAD_INIT_RAW((name).lock.wait_list, (name).lock.wait_lock)
  11584. +*/
  11585. +
  11586. +#define __SPIN_LOCK_UNLOCKED(name) \
  11587. + { .lock = __RT_SPIN_INITIALIZER(name.lock), \
  11588. + SPIN_DEP_MAP_INIT(name) }
  11589. +
  11590. +#define __DEFINE_SPINLOCK(name) \
  11591. + spinlock_t name = __SPIN_LOCK_UNLOCKED(name)
  11592. +
  11593. +#define DEFINE_SPINLOCK(name) \
  11594. + spinlock_t name __cacheline_aligned_in_smp = __SPIN_LOCK_UNLOCKED(name)
  11595. +
  11596. +#endif
  11597. diff -Nur linux-4.1.13.orig/include/linux/srcu.h linux-4.1.13/include/linux/srcu.h
  11598. --- linux-4.1.13.orig/include/linux/srcu.h 2015-11-09 23:34:10.000000000 +0100
  11599. +++ linux-4.1.13/include/linux/srcu.h 2015-11-29 09:23:09.617613651 +0100
  11600. @@ -84,10 +84,10 @@
  11601. void process_srcu(struct work_struct *work);
  11602. -#define __SRCU_STRUCT_INIT(name) \
  11603. +#define __SRCU_STRUCT_INIT(name, pcpu_name) \
  11604. { \
  11605. .completed = -300, \
  11606. - .per_cpu_ref = &name##_srcu_array, \
  11607. + .per_cpu_ref = &pcpu_name, \
  11608. .queue_lock = __SPIN_LOCK_UNLOCKED(name.queue_lock), \
  11609. .running = false, \
  11610. .batch_queue = RCU_BATCH_INIT(name.batch_queue), \
  11611. @@ -104,7 +104,7 @@
  11612. */
  11613. #define __DEFINE_SRCU(name, is_static) \
  11614. static DEFINE_PER_CPU(struct srcu_struct_array, name##_srcu_array);\
  11615. - is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name)
  11616. + is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name##_srcu_array)
  11617. #define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
  11618. #define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
  11619. diff -Nur linux-4.1.13.orig/include/linux/swap.h linux-4.1.13/include/linux/swap.h
  11620. --- linux-4.1.13.orig/include/linux/swap.h 2015-11-09 23:34:10.000000000 +0100
  11621. +++ linux-4.1.13/include/linux/swap.h 2015-11-29 09:23:09.617613651 +0100
  11622. @@ -11,6 +11,7 @@
  11623. #include <linux/fs.h>
  11624. #include <linux/atomic.h>
  11625. #include <linux/page-flags.h>
  11626. +#include <linux/locallock.h>
  11627. #include <asm/page.h>
  11628. struct notifier_block;
  11629. @@ -252,7 +253,8 @@
  11630. void *workingset_eviction(struct address_space *mapping, struct page *page);
  11631. bool workingset_refault(void *shadow);
  11632. void workingset_activation(struct page *page);
  11633. -extern struct list_lru workingset_shadow_nodes;
  11634. +extern struct list_lru __workingset_shadow_nodes;
  11635. +DECLARE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  11636. static inline unsigned int workingset_node_pages(struct radix_tree_node *node)
  11637. {
  11638. @@ -296,6 +298,7 @@
  11639. /* linux/mm/swap.c */
  11640. +DECLARE_LOCAL_IRQ_LOCK(swapvec_lock);
  11641. extern void lru_cache_add(struct page *);
  11642. extern void lru_cache_add_anon(struct page *page);
  11643. extern void lru_cache_add_file(struct page *page);
  11644. diff -Nur linux-4.1.13.orig/include/linux/thread_info.h linux-4.1.13/include/linux/thread_info.h
  11645. --- linux-4.1.13.orig/include/linux/thread_info.h 2015-11-09 23:34:10.000000000 +0100
  11646. +++ linux-4.1.13/include/linux/thread_info.h 2015-11-29 09:23:09.617613651 +0100
  11647. @@ -102,7 +102,17 @@
  11648. #define test_thread_flag(flag) \
  11649. test_ti_thread_flag(current_thread_info(), flag)
  11650. -#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  11651. +#ifdef CONFIG_PREEMPT_LAZY
  11652. +#define tif_need_resched() (test_thread_flag(TIF_NEED_RESCHED) || \
  11653. + test_thread_flag(TIF_NEED_RESCHED_LAZY))
  11654. +#define tif_need_resched_now() (test_thread_flag(TIF_NEED_RESCHED))
  11655. +#define tif_need_resched_lazy() test_thread_flag(TIF_NEED_RESCHED_LAZY))
  11656. +
  11657. +#else
  11658. +#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  11659. +#define tif_need_resched_now() test_thread_flag(TIF_NEED_RESCHED)
  11660. +#define tif_need_resched_lazy() 0
  11661. +#endif
  11662. #if defined TIF_RESTORE_SIGMASK && !defined HAVE_SET_RESTORE_SIGMASK
  11663. /*
  11664. diff -Nur linux-4.1.13.orig/include/linux/timer.h linux-4.1.13/include/linux/timer.h
  11665. --- linux-4.1.13.orig/include/linux/timer.h 2015-11-09 23:34:10.000000000 +0100
  11666. +++ linux-4.1.13/include/linux/timer.h 2015-11-29 09:23:09.617613651 +0100
  11667. @@ -241,7 +241,7 @@
  11668. extern int try_to_del_timer_sync(struct timer_list *timer);
  11669. -#ifdef CONFIG_SMP
  11670. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  11671. extern int del_timer_sync(struct timer_list *timer);
  11672. #else
  11673. # define del_timer_sync(t) del_timer(t)
  11674. diff -Nur linux-4.1.13.orig/include/linux/uaccess.h linux-4.1.13/include/linux/uaccess.h
  11675. --- linux-4.1.13.orig/include/linux/uaccess.h 2015-11-09 23:34:10.000000000 +0100
  11676. +++ linux-4.1.13/include/linux/uaccess.h 2015-11-29 09:23:09.617613651 +0100
  11677. @@ -1,21 +1,31 @@
  11678. #ifndef __LINUX_UACCESS_H__
  11679. #define __LINUX_UACCESS_H__
  11680. -#include <linux/preempt.h>
  11681. +#include <linux/sched.h>
  11682. #include <asm/uaccess.h>
  11683. +static __always_inline void pagefault_disabled_inc(void)
  11684. +{
  11685. + current->pagefault_disabled++;
  11686. +}
  11687. +
  11688. +static __always_inline void pagefault_disabled_dec(void)
  11689. +{
  11690. + current->pagefault_disabled--;
  11691. + WARN_ON(current->pagefault_disabled < 0);
  11692. +}
  11693. +
  11694. /*
  11695. - * These routines enable/disable the pagefault handler in that
  11696. - * it will not take any locks and go straight to the fixup table.
  11697. + * These routines enable/disable the pagefault handler. If disabled, it will
  11698. + * not take any locks and go straight to the fixup table.
  11699. *
  11700. - * They have great resemblance to the preempt_disable/enable calls
  11701. - * and in fact they are identical; this is because currently there is
  11702. - * no other way to make the pagefault handlers do this. So we do
  11703. - * disable preemption but we don't necessarily care about that.
  11704. + * User access methods will not sleep when called from a pagefault_disabled()
  11705. + * environment.
  11706. */
  11707. static inline void pagefault_disable(void)
  11708. {
  11709. - preempt_count_inc();
  11710. + migrate_disable();
  11711. + pagefault_disabled_inc();
  11712. /*
  11713. * make sure to have issued the store before a pagefault
  11714. * can hit.
  11715. @@ -25,18 +35,32 @@
  11716. static inline void pagefault_enable(void)
  11717. {
  11718. -#ifndef CONFIG_PREEMPT
  11719. /*
  11720. * make sure to issue those last loads/stores before enabling
  11721. * the pagefault handler again.
  11722. */
  11723. barrier();
  11724. - preempt_count_dec();
  11725. -#else
  11726. - preempt_enable();
  11727. -#endif
  11728. + pagefault_disabled_dec();
  11729. + migrate_enable();
  11730. }
  11731. +/*
  11732. + * Is the pagefault handler disabled? If so, user access methods will not sleep.
  11733. + */
  11734. +#define pagefault_disabled() (current->pagefault_disabled != 0)
  11735. +
  11736. +/*
  11737. + * The pagefault handler is in general disabled by pagefault_disable() or
  11738. + * when in irq context (via in_atomic()).
  11739. + *
  11740. + * This function should only be used by the fault handlers. Other users should
  11741. + * stick to pagefault_disabled().
  11742. + * Please NEVER use preempt_disable() to disable the fault handler. With
  11743. + * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled.
  11744. + * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT.
  11745. + */
  11746. +#define faulthandler_disabled() (pagefault_disabled() || in_atomic())
  11747. +
  11748. #ifndef ARCH_HAS_NOCACHE_UACCESS
  11749. static inline unsigned long __copy_from_user_inatomic_nocache(void *to,
  11750. diff -Nur linux-4.1.13.orig/include/linux/uprobes.h linux-4.1.13/include/linux/uprobes.h
  11751. --- linux-4.1.13.orig/include/linux/uprobes.h 2015-11-09 23:34:10.000000000 +0100
  11752. +++ linux-4.1.13/include/linux/uprobes.h 2015-11-29 09:23:09.617613651 +0100
  11753. @@ -27,6 +27,7 @@
  11754. #include <linux/errno.h>
  11755. #include <linux/rbtree.h>
  11756. #include <linux/types.h>
  11757. +#include <linux/wait.h>
  11758. struct vm_area_struct;
  11759. struct mm_struct;
  11760. diff -Nur linux-4.1.13.orig/include/linux/vmstat.h linux-4.1.13/include/linux/vmstat.h
  11761. --- linux-4.1.13.orig/include/linux/vmstat.h 2015-11-09 23:34:10.000000000 +0100
  11762. +++ linux-4.1.13/include/linux/vmstat.h 2015-11-29 09:23:09.617613651 +0100
  11763. @@ -33,7 +33,9 @@
  11764. */
  11765. static inline void __count_vm_event(enum vm_event_item item)
  11766. {
  11767. + preempt_disable_rt();
  11768. raw_cpu_inc(vm_event_states.event[item]);
  11769. + preempt_enable_rt();
  11770. }
  11771. static inline void count_vm_event(enum vm_event_item item)
  11772. @@ -43,7 +45,9 @@
  11773. static inline void __count_vm_events(enum vm_event_item item, long delta)
  11774. {
  11775. + preempt_disable_rt();
  11776. raw_cpu_add(vm_event_states.event[item], delta);
  11777. + preempt_enable_rt();
  11778. }
  11779. static inline void count_vm_events(enum vm_event_item item, long delta)
  11780. diff -Nur linux-4.1.13.orig/include/linux/wait.h linux-4.1.13/include/linux/wait.h
  11781. --- linux-4.1.13.orig/include/linux/wait.h 2015-11-09 23:34:10.000000000 +0100
  11782. +++ linux-4.1.13/include/linux/wait.h 2015-11-29 09:23:09.621613384 +0100
  11783. @@ -8,6 +8,7 @@
  11784. #include <linux/spinlock.h>
  11785. #include <asm/current.h>
  11786. #include <uapi/linux/wait.h>
  11787. +#include <linux/atomic.h>
  11788. typedef struct __wait_queue wait_queue_t;
  11789. typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
  11790. diff -Nur linux-4.1.13.orig/include/linux/wait-simple.h linux-4.1.13/include/linux/wait-simple.h
  11791. --- linux-4.1.13.orig/include/linux/wait-simple.h 1970-01-01 01:00:00.000000000 +0100
  11792. +++ linux-4.1.13/include/linux/wait-simple.h 2015-11-29 09:23:09.617613651 +0100
  11793. @@ -0,0 +1,207 @@
  11794. +#ifndef _LINUX_WAIT_SIMPLE_H
  11795. +#define _LINUX_WAIT_SIMPLE_H
  11796. +
  11797. +#include <linux/spinlock.h>
  11798. +#include <linux/list.h>
  11799. +
  11800. +#include <asm/current.h>
  11801. +
  11802. +struct swaiter {
  11803. + struct task_struct *task;
  11804. + struct list_head node;
  11805. +};
  11806. +
  11807. +#define DEFINE_SWAITER(name) \
  11808. + struct swaiter name = { \
  11809. + .task = current, \
  11810. + .node = LIST_HEAD_INIT((name).node), \
  11811. + }
  11812. +
  11813. +struct swait_head {
  11814. + raw_spinlock_t lock;
  11815. + struct list_head list;
  11816. +};
  11817. +
  11818. +#define SWAIT_HEAD_INITIALIZER(name) { \
  11819. + .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
  11820. + .list = LIST_HEAD_INIT((name).list), \
  11821. + }
  11822. +
  11823. +#define DEFINE_SWAIT_HEAD(name) \
  11824. + struct swait_head name = SWAIT_HEAD_INITIALIZER(name)
  11825. +
  11826. +extern void __init_swait_head(struct swait_head *h, struct lock_class_key *key);
  11827. +
  11828. +#define init_swait_head(swh) \
  11829. + do { \
  11830. + static struct lock_class_key __key; \
  11831. + \
  11832. + __init_swait_head((swh), &__key); \
  11833. + } while (0)
  11834. +
  11835. +/*
  11836. + * Waiter functions
  11837. + */
  11838. +extern void swait_prepare_locked(struct swait_head *head, struct swaiter *w);
  11839. +extern void swait_prepare(struct swait_head *head, struct swaiter *w, int state);
  11840. +extern void swait_finish_locked(struct swait_head *head, struct swaiter *w);
  11841. +extern void swait_finish(struct swait_head *head, struct swaiter *w);
  11842. +
  11843. +/* Check whether a head has waiters enqueued */
  11844. +static inline bool swaitqueue_active(struct swait_head *h)
  11845. +{
  11846. + /* Make sure the condition is visible before checking list_empty() */
  11847. + smp_mb();
  11848. + return !list_empty(&h->list);
  11849. +}
  11850. +
  11851. +/*
  11852. + * Wakeup functions
  11853. + */
  11854. +extern unsigned int __swait_wake(struct swait_head *head, unsigned int state, unsigned int num);
  11855. +extern unsigned int __swait_wake_locked(struct swait_head *head, unsigned int state, unsigned int num);
  11856. +
  11857. +#define swait_wake(head) __swait_wake(head, TASK_NORMAL, 1)
  11858. +#define swait_wake_interruptible(head) __swait_wake(head, TASK_INTERRUPTIBLE, 1)
  11859. +#define swait_wake_all(head) __swait_wake(head, TASK_NORMAL, 0)
  11860. +#define swait_wake_all_interruptible(head) __swait_wake(head, TASK_INTERRUPTIBLE, 0)
  11861. +
  11862. +/*
  11863. + * Event API
  11864. + */
  11865. +#define __swait_event(wq, condition) \
  11866. +do { \
  11867. + DEFINE_SWAITER(__wait); \
  11868. + \
  11869. + for (;;) { \
  11870. + swait_prepare(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
  11871. + if (condition) \
  11872. + break; \
  11873. + schedule(); \
  11874. + } \
  11875. + swait_finish(&wq, &__wait); \
  11876. +} while (0)
  11877. +
  11878. +/**
  11879. + * swait_event - sleep until a condition gets true
  11880. + * @wq: the waitqueue to wait on
  11881. + * @condition: a C expression for the event to wait for
  11882. + *
  11883. + * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  11884. + * @condition evaluates to true. The @condition is checked each time
  11885. + * the waitqueue @wq is woken up.
  11886. + *
  11887. + * wake_up() has to be called after changing any variable that could
  11888. + * change the result of the wait condition.
  11889. + */
  11890. +#define swait_event(wq, condition) \
  11891. +do { \
  11892. + if (condition) \
  11893. + break; \
  11894. + __swait_event(wq, condition); \
  11895. +} while (0)
  11896. +
  11897. +#define __swait_event_interruptible(wq, condition, ret) \
  11898. +do { \
  11899. + DEFINE_SWAITER(__wait); \
  11900. + \
  11901. + for (;;) { \
  11902. + swait_prepare(&wq, &__wait, TASK_INTERRUPTIBLE); \
  11903. + if (condition) \
  11904. + break; \
  11905. + if (signal_pending(current)) { \
  11906. + ret = -ERESTARTSYS; \
  11907. + break; \
  11908. + } \
  11909. + schedule(); \
  11910. + } \
  11911. + swait_finish(&wq, &__wait); \
  11912. +} while (0)
  11913. +
  11914. +#define __swait_event_interruptible_timeout(wq, condition, ret) \
  11915. +do { \
  11916. + DEFINE_SWAITER(__wait); \
  11917. + \
  11918. + for (;;) { \
  11919. + swait_prepare(&wq, &__wait, TASK_INTERRUPTIBLE); \
  11920. + if (condition) \
  11921. + break; \
  11922. + if (signal_pending(current)) { \
  11923. + ret = -ERESTARTSYS; \
  11924. + break; \
  11925. + } \
  11926. + ret = schedule_timeout(ret); \
  11927. + if (!ret) \
  11928. + break; \
  11929. + } \
  11930. + swait_finish(&wq, &__wait); \
  11931. +} while (0)
  11932. +
  11933. +/**
  11934. + * swait_event_interruptible - sleep until a condition gets true
  11935. + * @wq: the waitqueue to wait on
  11936. + * @condition: a C expression for the event to wait for
  11937. + *
  11938. + * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  11939. + * @condition evaluates to true. The @condition is checked each time
  11940. + * the waitqueue @wq is woken up.
  11941. + *
  11942. + * wake_up() has to be called after changing any variable that could
  11943. + * change the result of the wait condition.
  11944. + */
  11945. +#define swait_event_interruptible(wq, condition) \
  11946. +({ \
  11947. + int __ret = 0; \
  11948. + if (!(condition)) \
  11949. + __swait_event_interruptible(wq, condition, __ret); \
  11950. + __ret; \
  11951. +})
  11952. +
  11953. +#define swait_event_interruptible_timeout(wq, condition, timeout) \
  11954. +({ \
  11955. + int __ret = timeout; \
  11956. + if (!(condition)) \
  11957. + __swait_event_interruptible_timeout(wq, condition, __ret); \
  11958. + __ret; \
  11959. +})
  11960. +
  11961. +#define __swait_event_timeout(wq, condition, ret) \
  11962. +do { \
  11963. + DEFINE_SWAITER(__wait); \
  11964. + \
  11965. + for (;;) { \
  11966. + swait_prepare(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
  11967. + if (condition) \
  11968. + break; \
  11969. + ret = schedule_timeout(ret); \
  11970. + if (!ret) \
  11971. + break; \
  11972. + } \
  11973. + swait_finish(&wq, &__wait); \
  11974. +} while (0)
  11975. +
  11976. +/**
  11977. + * swait_event_timeout - sleep until a condition gets true or a timeout elapses
  11978. + * @wq: the waitqueue to wait on
  11979. + * @condition: a C expression for the event to wait for
  11980. + * @timeout: timeout, in jiffies
  11981. + *
  11982. + * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  11983. + * @condition evaluates to true. The @condition is checked each time
  11984. + * the waitqueue @wq is woken up.
  11985. + *
  11986. + * wake_up() has to be called after changing any variable that could
  11987. + * change the result of the wait condition.
  11988. + *
  11989. + * The function returns 0 if the @timeout elapsed, and the remaining
  11990. + * jiffies if the condition evaluated to true before the timeout elapsed.
  11991. + */
  11992. +#define swait_event_timeout(wq, condition, timeout) \
  11993. +({ \
  11994. + long __ret = timeout; \
  11995. + if (!(condition)) \
  11996. + __swait_event_timeout(wq, condition, __ret); \
  11997. + __ret; \
  11998. +})
  11999. +
  12000. +#endif
  12001. diff -Nur linux-4.1.13.orig/include/linux/work-simple.h linux-4.1.13/include/linux/work-simple.h
  12002. --- linux-4.1.13.orig/include/linux/work-simple.h 1970-01-01 01:00:00.000000000 +0100
  12003. +++ linux-4.1.13/include/linux/work-simple.h 2015-11-29 09:23:09.621613384 +0100
  12004. @@ -0,0 +1,24 @@
  12005. +#ifndef _LINUX_SWORK_H
  12006. +#define _LINUX_SWORK_H
  12007. +
  12008. +#include <linux/list.h>
  12009. +
  12010. +struct swork_event {
  12011. + struct list_head item;
  12012. + unsigned long flags;
  12013. + void (*func)(struct swork_event *);
  12014. +};
  12015. +
  12016. +static inline void INIT_SWORK(struct swork_event *event,
  12017. + void (*func)(struct swork_event *))
  12018. +{
  12019. + event->flags = 0;
  12020. + event->func = func;
  12021. +}
  12022. +
  12023. +bool swork_queue(struct swork_event *sev);
  12024. +
  12025. +int swork_get(void);
  12026. +void swork_put(void);
  12027. +
  12028. +#endif /* _LINUX_SWORK_H */
  12029. diff -Nur linux-4.1.13.orig/include/net/dst.h linux-4.1.13/include/net/dst.h
  12030. --- linux-4.1.13.orig/include/net/dst.h 2015-11-09 23:34:10.000000000 +0100
  12031. +++ linux-4.1.13/include/net/dst.h 2015-11-29 09:23:09.629612854 +0100
  12032. @@ -403,7 +403,7 @@
  12033. static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n,
  12034. struct sk_buff *skb)
  12035. {
  12036. - const struct hh_cache *hh;
  12037. + struct hh_cache *hh;
  12038. if (dst->pending_confirm) {
  12039. unsigned long now = jiffies;
  12040. diff -Nur linux-4.1.13.orig/include/net/neighbour.h linux-4.1.13/include/net/neighbour.h
  12041. --- linux-4.1.13.orig/include/net/neighbour.h 2015-11-09 23:34:10.000000000 +0100
  12042. +++ linux-4.1.13/include/net/neighbour.h 2015-11-29 09:23:09.629612854 +0100
  12043. @@ -445,7 +445,7 @@
  12044. }
  12045. #endif
  12046. -static inline int neigh_hh_output(const struct hh_cache *hh, struct sk_buff *skb)
  12047. +static inline int neigh_hh_output(struct hh_cache *hh, struct sk_buff *skb)
  12048. {
  12049. unsigned int seq;
  12050. int hh_len;
  12051. @@ -500,7 +500,7 @@
  12052. #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
  12053. -static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
  12054. +static inline void neigh_ha_snapshot(char *dst, struct neighbour *n,
  12055. const struct net_device *dev)
  12056. {
  12057. unsigned int seq;
  12058. diff -Nur linux-4.1.13.orig/include/net/netns/ipv4.h linux-4.1.13/include/net/netns/ipv4.h
  12059. --- linux-4.1.13.orig/include/net/netns/ipv4.h 2015-11-09 23:34:10.000000000 +0100
  12060. +++ linux-4.1.13/include/net/netns/ipv4.h 2015-11-29 09:23:09.629612854 +0100
  12061. @@ -69,6 +69,7 @@
  12062. int sysctl_icmp_echo_ignore_all;
  12063. int sysctl_icmp_echo_ignore_broadcasts;
  12064. + int sysctl_icmp_echo_sysrq;
  12065. int sysctl_icmp_ignore_bogus_error_responses;
  12066. int sysctl_icmp_ratelimit;
  12067. int sysctl_icmp_ratemask;
  12068. diff -Nur linux-4.1.13.orig/include/trace/events/hist.h linux-4.1.13/include/trace/events/hist.h
  12069. --- linux-4.1.13.orig/include/trace/events/hist.h 1970-01-01 01:00:00.000000000 +0100
  12070. +++ linux-4.1.13/include/trace/events/hist.h 2015-11-29 09:23:09.629612854 +0100
  12071. @@ -0,0 +1,74 @@
  12072. +#undef TRACE_SYSTEM
  12073. +#define TRACE_SYSTEM hist
  12074. +
  12075. +#if !defined(_TRACE_HIST_H) || defined(TRACE_HEADER_MULTI_READ)
  12076. +#define _TRACE_HIST_H
  12077. +
  12078. +#include "latency_hist.h"
  12079. +#include <linux/tracepoint.h>
  12080. +
  12081. +#if !defined(CONFIG_PREEMPT_OFF_HIST) && !defined(CONFIG_INTERRUPT_OFF_HIST)
  12082. +#define trace_preemptirqsoff_hist(a, b)
  12083. +#define trace_preemptirqsoff_hist_rcuidle(a, b)
  12084. +#else
  12085. +TRACE_EVENT(preemptirqsoff_hist,
  12086. +
  12087. + TP_PROTO(int reason, int starthist),
  12088. +
  12089. + TP_ARGS(reason, starthist),
  12090. +
  12091. + TP_STRUCT__entry(
  12092. + __field(int, reason)
  12093. + __field(int, starthist)
  12094. + ),
  12095. +
  12096. + TP_fast_assign(
  12097. + __entry->reason = reason;
  12098. + __entry->starthist = starthist;
  12099. + ),
  12100. +
  12101. + TP_printk("reason=%s starthist=%s", getaction(__entry->reason),
  12102. + __entry->starthist ? "start" : "stop")
  12103. +);
  12104. +#endif
  12105. +
  12106. +#ifndef CONFIG_MISSED_TIMER_OFFSETS_HIST
  12107. +#define trace_hrtimer_interrupt(a, b, c, d)
  12108. +#define trace_hrtimer_interrupt_rcuidle(a, b, c, d)
  12109. +#else
  12110. +TRACE_EVENT(hrtimer_interrupt,
  12111. +
  12112. + TP_PROTO(int cpu, long long offset, struct task_struct *curr,
  12113. + struct task_struct *task),
  12114. +
  12115. + TP_ARGS(cpu, offset, curr, task),
  12116. +
  12117. + TP_STRUCT__entry(
  12118. + __field(int, cpu)
  12119. + __field(long long, offset)
  12120. + __array(char, ccomm, TASK_COMM_LEN)
  12121. + __field(int, cprio)
  12122. + __array(char, tcomm, TASK_COMM_LEN)
  12123. + __field(int, tprio)
  12124. + ),
  12125. +
  12126. + TP_fast_assign(
  12127. + __entry->cpu = cpu;
  12128. + __entry->offset = offset;
  12129. + memcpy(__entry->ccomm, curr->comm, TASK_COMM_LEN);
  12130. + __entry->cprio = curr->prio;
  12131. + memcpy(__entry->tcomm, task != NULL ? task->comm : "<none>",
  12132. + task != NULL ? TASK_COMM_LEN : 7);
  12133. + __entry->tprio = task != NULL ? task->prio : -1;
  12134. + ),
  12135. +
  12136. + TP_printk("cpu=%d offset=%lld curr=%s[%d] thread=%s[%d]",
  12137. + __entry->cpu, __entry->offset, __entry->ccomm,
  12138. + __entry->cprio, __entry->tcomm, __entry->tprio)
  12139. +);
  12140. +#endif
  12141. +
  12142. +#endif /* _TRACE_HIST_H */
  12143. +
  12144. +/* This part must be outside protection */
  12145. +#include <trace/define_trace.h>
  12146. diff -Nur linux-4.1.13.orig/include/trace/events/latency_hist.h linux-4.1.13/include/trace/events/latency_hist.h
  12147. --- linux-4.1.13.orig/include/trace/events/latency_hist.h 1970-01-01 01:00:00.000000000 +0100
  12148. +++ linux-4.1.13/include/trace/events/latency_hist.h 2015-11-29 09:23:09.629612854 +0100
  12149. @@ -0,0 +1,29 @@
  12150. +#ifndef _LATENCY_HIST_H
  12151. +#define _LATENCY_HIST_H
  12152. +
  12153. +enum hist_action {
  12154. + IRQS_ON,
  12155. + PREEMPT_ON,
  12156. + TRACE_STOP,
  12157. + IRQS_OFF,
  12158. + PREEMPT_OFF,
  12159. + TRACE_START,
  12160. +};
  12161. +
  12162. +static char *actions[] = {
  12163. + "IRQS_ON",
  12164. + "PREEMPT_ON",
  12165. + "TRACE_STOP",
  12166. + "IRQS_OFF",
  12167. + "PREEMPT_OFF",
  12168. + "TRACE_START",
  12169. +};
  12170. +
  12171. +static inline char *getaction(int action)
  12172. +{
  12173. + if (action >= 0 && action <= sizeof(actions)/sizeof(actions[0]))
  12174. + return actions[action];
  12175. + return "unknown";
  12176. +}
  12177. +
  12178. +#endif /* _LATENCY_HIST_H */
  12179. diff -Nur linux-4.1.13.orig/include/trace/events/sched.h linux-4.1.13/include/trace/events/sched.h
  12180. --- linux-4.1.13.orig/include/trace/events/sched.h 2015-11-09 23:34:10.000000000 +0100
  12181. +++ linux-4.1.13/include/trace/events/sched.h 2015-11-29 09:23:09.629612854 +0100
  12182. @@ -55,9 +55,9 @@
  12183. */
  12184. DECLARE_EVENT_CLASS(sched_wakeup_template,
  12185. - TP_PROTO(struct task_struct *p, int success),
  12186. + TP_PROTO(struct task_struct *p),
  12187. - TP_ARGS(__perf_task(p), success),
  12188. + TP_ARGS(__perf_task(p)),
  12189. TP_STRUCT__entry(
  12190. __array( char, comm, TASK_COMM_LEN )
  12191. @@ -71,25 +71,37 @@
  12192. memcpy(__entry->comm, p->comm, TASK_COMM_LEN);
  12193. __entry->pid = p->pid;
  12194. __entry->prio = p->prio;
  12195. - __entry->success = success;
  12196. + __entry->success = 1; /* rudiment, kill when possible */
  12197. __entry->target_cpu = task_cpu(p);
  12198. ),
  12199. - TP_printk("comm=%s pid=%d prio=%d success=%d target_cpu=%03d",
  12200. + TP_printk("comm=%s pid=%d prio=%d target_cpu=%03d",
  12201. __entry->comm, __entry->pid, __entry->prio,
  12202. - __entry->success, __entry->target_cpu)
  12203. + __entry->target_cpu)
  12204. );
  12205. +/*
  12206. + * Tracepoint called when waking a task; this tracepoint is guaranteed to be
  12207. + * called from the waking context.
  12208. + */
  12209. +DEFINE_EVENT(sched_wakeup_template, sched_waking,
  12210. + TP_PROTO(struct task_struct *p),
  12211. + TP_ARGS(p));
  12212. +
  12213. +/*
  12214. + * Tracepoint called when the task is actually woken; p->state == TASK_RUNNNG.
  12215. + * It it not always called from the waking context.
  12216. + */
  12217. DEFINE_EVENT(sched_wakeup_template, sched_wakeup,
  12218. - TP_PROTO(struct task_struct *p, int success),
  12219. - TP_ARGS(p, success));
  12220. + TP_PROTO(struct task_struct *p),
  12221. + TP_ARGS(p));
  12222. /*
  12223. * Tracepoint for waking up a new task:
  12224. */
  12225. DEFINE_EVENT(sched_wakeup_template, sched_wakeup_new,
  12226. - TP_PROTO(struct task_struct *p, int success),
  12227. - TP_ARGS(p, success));
  12228. + TP_PROTO(struct task_struct *p),
  12229. + TP_ARGS(p));
  12230. #ifdef CREATE_TRACE_POINTS
  12231. static inline long __trace_sched_switch_state(struct task_struct *p)
  12232. diff -Nur linux-4.1.13.orig/init/Kconfig linux-4.1.13/init/Kconfig
  12233. --- linux-4.1.13.orig/init/Kconfig 2015-11-09 23:34:10.000000000 +0100
  12234. +++ linux-4.1.13/init/Kconfig 2015-11-29 09:23:09.629612854 +0100
  12235. @@ -637,7 +637,7 @@
  12236. config RCU_FAST_NO_HZ
  12237. bool "Accelerate last non-dyntick-idle CPU's grace periods"
  12238. - depends on NO_HZ_COMMON && SMP
  12239. + depends on NO_HZ_COMMON && SMP && !PREEMPT_RT_FULL
  12240. default n
  12241. help
  12242. This option permits CPUs to enter dynticks-idle state even if
  12243. @@ -664,7 +664,7 @@
  12244. config RCU_BOOST
  12245. bool "Enable RCU priority boosting"
  12246. depends on RT_MUTEXES && PREEMPT_RCU
  12247. - default n
  12248. + default y if PREEMPT_RT_FULL
  12249. help
  12250. This option boosts the priority of preempted RCU readers that
  12251. block the current preemptible RCU grace period for too long.
  12252. @@ -1101,6 +1101,7 @@
  12253. config RT_GROUP_SCHED
  12254. bool "Group scheduling for SCHED_RR/FIFO"
  12255. depends on CGROUP_SCHED
  12256. + depends on !PREEMPT_RT_FULL
  12257. default n
  12258. help
  12259. This feature lets you explicitly allocate real CPU bandwidth
  12260. @@ -1688,6 +1689,7 @@
  12261. config SLAB
  12262. bool "SLAB"
  12263. + depends on !PREEMPT_RT_FULL
  12264. help
  12265. The regular slab allocator that is established and known to work
  12266. well in all environments. It organizes cache hot objects in
  12267. @@ -1706,6 +1708,7 @@
  12268. config SLOB
  12269. depends on EXPERT
  12270. bool "SLOB (Simple Allocator)"
  12271. + depends on !PREEMPT_RT_FULL
  12272. help
  12273. SLOB replaces the stock allocator with a drastically simpler
  12274. allocator. SLOB is generally more space efficient but
  12275. @@ -1715,7 +1718,7 @@
  12276. config SLUB_CPU_PARTIAL
  12277. default y
  12278. - depends on SLUB && SMP
  12279. + depends on SLUB && SMP && !PREEMPT_RT_FULL
  12280. bool "SLUB per cpu partial cache"
  12281. help
  12282. Per cpu partial caches accellerate objects allocation and freeing
  12283. diff -Nur linux-4.1.13.orig/init/main.c linux-4.1.13/init/main.c
  12284. --- linux-4.1.13.orig/init/main.c 2015-11-09 23:34:10.000000000 +0100
  12285. +++ linux-4.1.13/init/main.c 2015-11-29 09:23:09.629612854 +0100
  12286. @@ -525,6 +525,7 @@
  12287. setup_command_line(command_line);
  12288. setup_nr_cpu_ids();
  12289. setup_per_cpu_areas();
  12290. + softirq_early_init();
  12291. smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
  12292. build_all_zonelists(NULL, NULL);
  12293. diff -Nur linux-4.1.13.orig/init/Makefile linux-4.1.13/init/Makefile
  12294. --- linux-4.1.13.orig/init/Makefile 2015-11-09 23:34:10.000000000 +0100
  12295. +++ linux-4.1.13/init/Makefile 2015-11-29 09:23:09.629612854 +0100
  12296. @@ -33,4 +33,4 @@
  12297. include/generated/compile.h: FORCE
  12298. @$($(quiet)chk_compile.h)
  12299. $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkcompile_h $@ \
  12300. - "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CC) $(KBUILD_CFLAGS)"
  12301. + "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CONFIG_PREEMPT_RT_FULL)" "$(CC) $(KBUILD_CFLAGS)"
  12302. diff -Nur linux-4.1.13.orig/ipc/mqueue.c linux-4.1.13/ipc/mqueue.c
  12303. --- linux-4.1.13.orig/ipc/mqueue.c 2015-11-09 23:34:10.000000000 +0100
  12304. +++ linux-4.1.13/ipc/mqueue.c 2015-11-29 09:23:09.629612854 +0100
  12305. @@ -47,8 +47,7 @@
  12306. #define RECV 1
  12307. #define STATE_NONE 0
  12308. -#define STATE_PENDING 1
  12309. -#define STATE_READY 2
  12310. +#define STATE_READY 1
  12311. struct posix_msg_tree_node {
  12312. struct rb_node rb_node;
  12313. @@ -568,15 +567,12 @@
  12314. wq_add(info, sr, ewp);
  12315. for (;;) {
  12316. - set_current_state(TASK_INTERRUPTIBLE);
  12317. + __set_current_state(TASK_INTERRUPTIBLE);
  12318. spin_unlock(&info->lock);
  12319. time = schedule_hrtimeout_range_clock(timeout, 0,
  12320. HRTIMER_MODE_ABS, CLOCK_REALTIME);
  12321. - while (ewp->state == STATE_PENDING)
  12322. - cpu_relax();
  12323. -
  12324. if (ewp->state == STATE_READY) {
  12325. retval = 0;
  12326. goto out;
  12327. @@ -904,11 +900,15 @@
  12328. * list of waiting receivers. A sender checks that list before adding the new
  12329. * message into the message array. If there is a waiting receiver, then it
  12330. * bypasses the message array and directly hands the message over to the
  12331. - * receiver.
  12332. - * The receiver accepts the message and returns without grabbing the queue
  12333. - * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
  12334. - * are necessary. The same algorithm is used for sysv semaphores, see
  12335. - * ipc/sem.c for more details.
  12336. + * receiver. The receiver accepts the message and returns without grabbing the
  12337. + * queue spinlock:
  12338. + *
  12339. + * - Set pointer to message.
  12340. + * - Queue the receiver task for later wakeup (without the info->lock).
  12341. + * - Update its state to STATE_READY. Now the receiver can continue.
  12342. + * - Wake up the process after the lock is dropped. Should the process wake up
  12343. + * before this wakeup (due to a timeout or a signal) it will either see
  12344. + * STATE_READY and continue or acquire the lock to check the state again.
  12345. *
  12346. * The same algorithm is used for senders.
  12347. */
  12348. @@ -916,21 +916,29 @@
  12349. /* pipelined_send() - send a message directly to the task waiting in
  12350. * sys_mq_timedreceive() (without inserting message into a queue).
  12351. */
  12352. -static inline void pipelined_send(struct mqueue_inode_info *info,
  12353. +static inline void pipelined_send(struct wake_q_head *wake_q,
  12354. + struct mqueue_inode_info *info,
  12355. struct msg_msg *message,
  12356. struct ext_wait_queue *receiver)
  12357. {
  12358. receiver->msg = message;
  12359. list_del(&receiver->list);
  12360. - receiver->state = STATE_PENDING;
  12361. - wake_up_process(receiver->task);
  12362. - smp_wmb();
  12363. + wake_q_add(wake_q, receiver->task);
  12364. + /*
  12365. + * Rely on the implicit cmpxchg barrier from wake_q_add such
  12366. + * that we can ensure that updating receiver->state is the last
  12367. + * write operation: As once set, the receiver can continue,
  12368. + * and if we don't have the reference count from the wake_q,
  12369. + * yet, at that point we can later have a use-after-free
  12370. + * condition and bogus wakeup.
  12371. + */
  12372. receiver->state = STATE_READY;
  12373. }
  12374. /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
  12375. * gets its message and put to the queue (we have one free place for sure). */
  12376. -static inline void pipelined_receive(struct mqueue_inode_info *info)
  12377. +static inline void pipelined_receive(struct wake_q_head *wake_q,
  12378. + struct mqueue_inode_info *info)
  12379. {
  12380. struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
  12381. @@ -941,10 +949,9 @@
  12382. }
  12383. if (msg_insert(sender->msg, info))
  12384. return;
  12385. +
  12386. list_del(&sender->list);
  12387. - sender->state = STATE_PENDING;
  12388. - wake_up_process(sender->task);
  12389. - smp_wmb();
  12390. + wake_q_add(wake_q, sender->task);
  12391. sender->state = STATE_READY;
  12392. }
  12393. @@ -962,6 +969,7 @@
  12394. struct timespec ts;
  12395. struct posix_msg_tree_node *new_leaf = NULL;
  12396. int ret = 0;
  12397. + WAKE_Q(wake_q);
  12398. if (u_abs_timeout) {
  12399. int res = prepare_timeout(u_abs_timeout, &expires, &ts);
  12400. @@ -1045,7 +1053,7 @@
  12401. } else {
  12402. receiver = wq_get_first_waiter(info, RECV);
  12403. if (receiver) {
  12404. - pipelined_send(info, msg_ptr, receiver);
  12405. + pipelined_send(&wake_q, info, msg_ptr, receiver);
  12406. } else {
  12407. /* adds message to the queue */
  12408. ret = msg_insert(msg_ptr, info);
  12409. @@ -1058,6 +1066,7 @@
  12410. }
  12411. out_unlock:
  12412. spin_unlock(&info->lock);
  12413. + wake_up_q(&wake_q);
  12414. out_free:
  12415. if (ret)
  12416. free_msg(msg_ptr);
  12417. @@ -1144,14 +1153,17 @@
  12418. msg_ptr = wait.msg;
  12419. }
  12420. } else {
  12421. + WAKE_Q(wake_q);
  12422. +
  12423. msg_ptr = msg_get(info);
  12424. inode->i_atime = inode->i_mtime = inode->i_ctime =
  12425. CURRENT_TIME;
  12426. /* There is now free space in queue. */
  12427. - pipelined_receive(info);
  12428. + pipelined_receive(&wake_q, info);
  12429. spin_unlock(&info->lock);
  12430. + wake_up_q(&wake_q);
  12431. ret = 0;
  12432. }
  12433. if (ret == 0) {
  12434. diff -Nur linux-4.1.13.orig/ipc/msg.c linux-4.1.13/ipc/msg.c
  12435. --- linux-4.1.13.orig/ipc/msg.c 2015-11-09 23:34:10.000000000 +0100
  12436. +++ linux-4.1.13/ipc/msg.c 2015-11-29 09:23:09.629612854 +0100
  12437. @@ -188,6 +188,12 @@
  12438. struct msg_receiver *msr, *t;
  12439. list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
  12440. + /*
  12441. + * Make sure that the wakeup doesnt preempt
  12442. + * this CPU prematurely. (on PREEMPT_RT)
  12443. + */
  12444. + preempt_disable_rt();
  12445. +
  12446. msr->r_msg = NULL; /* initialize expunge ordering */
  12447. wake_up_process(msr->r_tsk);
  12448. /*
  12449. @@ -198,6 +204,8 @@
  12450. */
  12451. smp_mb();
  12452. msr->r_msg = ERR_PTR(res);
  12453. +
  12454. + preempt_enable_rt();
  12455. }
  12456. }
  12457. @@ -574,6 +582,11 @@
  12458. if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
  12459. !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
  12460. msr->r_msgtype, msr->r_mode)) {
  12461. + /*
  12462. + * Make sure that the wakeup doesnt preempt
  12463. + * this CPU prematurely. (on PREEMPT_RT)
  12464. + */
  12465. + preempt_disable_rt();
  12466. list_del(&msr->r_list);
  12467. if (msr->r_maxsize < msg->m_ts) {
  12468. @@ -595,12 +608,13 @@
  12469. */
  12470. smp_mb();
  12471. msr->r_msg = msg;
  12472. + preempt_enable_rt();
  12473. return 1;
  12474. }
  12475. + preempt_enable_rt();
  12476. }
  12477. }
  12478. -
  12479. return 0;
  12480. }
  12481. diff -Nur linux-4.1.13.orig/ipc/sem.c linux-4.1.13/ipc/sem.c
  12482. --- linux-4.1.13.orig/ipc/sem.c 2015-11-09 23:34:10.000000000 +0100
  12483. +++ linux-4.1.13/ipc/sem.c 2015-11-29 09:23:09.629612854 +0100
  12484. @@ -690,6 +690,13 @@
  12485. static void wake_up_sem_queue_prepare(struct list_head *pt,
  12486. struct sem_queue *q, int error)
  12487. {
  12488. +#ifdef CONFIG_PREEMPT_RT_BASE
  12489. + struct task_struct *p = q->sleeper;
  12490. + get_task_struct(p);
  12491. + q->status = error;
  12492. + wake_up_process(p);
  12493. + put_task_struct(p);
  12494. +#else
  12495. if (list_empty(pt)) {
  12496. /*
  12497. * Hold preempt off so that we don't get preempted and have the
  12498. @@ -701,6 +708,7 @@
  12499. q->pid = error;
  12500. list_add_tail(&q->list, pt);
  12501. +#endif
  12502. }
  12503. /**
  12504. @@ -714,6 +722,7 @@
  12505. */
  12506. static void wake_up_sem_queue_do(struct list_head *pt)
  12507. {
  12508. +#ifndef CONFIG_PREEMPT_RT_BASE
  12509. struct sem_queue *q, *t;
  12510. int did_something;
  12511. @@ -726,6 +735,7 @@
  12512. }
  12513. if (did_something)
  12514. preempt_enable();
  12515. +#endif
  12516. }
  12517. static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
  12518. diff -Nur linux-4.1.13.orig/kernel/bpf/hashtab.c linux-4.1.13/kernel/bpf/hashtab.c
  12519. --- linux-4.1.13.orig/kernel/bpf/hashtab.c 2015-11-09 23:34:10.000000000 +0100
  12520. +++ linux-4.1.13/kernel/bpf/hashtab.c 2015-11-29 09:23:09.629612854 +0100
  12521. @@ -17,7 +17,7 @@
  12522. struct bpf_htab {
  12523. struct bpf_map map;
  12524. struct hlist_head *buckets;
  12525. - spinlock_t lock;
  12526. + raw_spinlock_t lock;
  12527. u32 count; /* number of elements in this hashtable */
  12528. u32 n_buckets; /* number of hash buckets */
  12529. u32 elem_size; /* size of each element in bytes */
  12530. @@ -82,7 +82,7 @@
  12531. for (i = 0; i < htab->n_buckets; i++)
  12532. INIT_HLIST_HEAD(&htab->buckets[i]);
  12533. - spin_lock_init(&htab->lock);
  12534. + raw_spin_lock_init(&htab->lock);
  12535. htab->count = 0;
  12536. htab->elem_size = sizeof(struct htab_elem) +
  12537. @@ -230,7 +230,7 @@
  12538. l_new->hash = htab_map_hash(l_new->key, key_size);
  12539. /* bpf_map_update_elem() can be called in_irq() */
  12540. - spin_lock_irqsave(&htab->lock, flags);
  12541. + raw_spin_lock_irqsave(&htab->lock, flags);
  12542. head = select_bucket(htab, l_new->hash);
  12543. @@ -266,11 +266,11 @@
  12544. } else {
  12545. htab->count++;
  12546. }
  12547. - spin_unlock_irqrestore(&htab->lock, flags);
  12548. + raw_spin_unlock_irqrestore(&htab->lock, flags);
  12549. return 0;
  12550. err:
  12551. - spin_unlock_irqrestore(&htab->lock, flags);
  12552. + raw_spin_unlock_irqrestore(&htab->lock, flags);
  12553. kfree(l_new);
  12554. return ret;
  12555. }
  12556. @@ -291,7 +291,7 @@
  12557. hash = htab_map_hash(key, key_size);
  12558. - spin_lock_irqsave(&htab->lock, flags);
  12559. + raw_spin_lock_irqsave(&htab->lock, flags);
  12560. head = select_bucket(htab, hash);
  12561. @@ -304,7 +304,7 @@
  12562. ret = 0;
  12563. }
  12564. - spin_unlock_irqrestore(&htab->lock, flags);
  12565. + raw_spin_unlock_irqrestore(&htab->lock, flags);
  12566. return ret;
  12567. }
  12568. diff -Nur linux-4.1.13.orig/kernel/cgroup.c linux-4.1.13/kernel/cgroup.c
  12569. --- linux-4.1.13.orig/kernel/cgroup.c 2015-11-09 23:34:10.000000000 +0100
  12570. +++ linux-4.1.13/kernel/cgroup.c 2015-11-29 09:23:09.629612854 +0100
  12571. @@ -4422,10 +4422,10 @@
  12572. queue_work(cgroup_destroy_wq, &css->destroy_work);
  12573. }
  12574. -static void css_release_work_fn(struct work_struct *work)
  12575. +static void css_release_work_fn(struct swork_event *sev)
  12576. {
  12577. struct cgroup_subsys_state *css =
  12578. - container_of(work, struct cgroup_subsys_state, destroy_work);
  12579. + container_of(sev, struct cgroup_subsys_state, destroy_swork);
  12580. struct cgroup_subsys *ss = css->ss;
  12581. struct cgroup *cgrp = css->cgroup;
  12582. @@ -4464,8 +4464,8 @@
  12583. struct cgroup_subsys_state *css =
  12584. container_of(ref, struct cgroup_subsys_state, refcnt);
  12585. - INIT_WORK(&css->destroy_work, css_release_work_fn);
  12586. - queue_work(cgroup_destroy_wq, &css->destroy_work);
  12587. + INIT_SWORK(&css->destroy_swork, css_release_work_fn);
  12588. + swork_queue(&css->destroy_swork);
  12589. }
  12590. static void init_and_link_css(struct cgroup_subsys_state *css,
  12591. @@ -5069,6 +5069,7 @@
  12592. */
  12593. cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
  12594. BUG_ON(!cgroup_destroy_wq);
  12595. + BUG_ON(swork_get());
  12596. /*
  12597. * Used to destroy pidlists and separate to serve as flush domain.
  12598. diff -Nur linux-4.1.13.orig/kernel/cpu.c linux-4.1.13/kernel/cpu.c
  12599. --- linux-4.1.13.orig/kernel/cpu.c 2015-11-09 23:34:10.000000000 +0100
  12600. +++ linux-4.1.13/kernel/cpu.c 2015-11-29 09:23:09.633612589 +0100
  12601. @@ -74,8 +74,8 @@
  12602. #endif
  12603. } cpu_hotplug = {
  12604. .active_writer = NULL,
  12605. - .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  12606. .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
  12607. + .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  12608. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  12609. .dep_map = {.name = "cpu_hotplug.lock" },
  12610. #endif
  12611. @@ -88,6 +88,289 @@
  12612. #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
  12613. #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
  12614. +/**
  12615. + * hotplug_pcp - per cpu hotplug descriptor
  12616. + * @unplug: set when pin_current_cpu() needs to sync tasks
  12617. + * @sync_tsk: the task that waits for tasks to finish pinned sections
  12618. + * @refcount: counter of tasks in pinned sections
  12619. + * @grab_lock: set when the tasks entering pinned sections should wait
  12620. + * @synced: notifier for @sync_tsk to tell cpu_down it's finished
  12621. + * @mutex: the mutex to make tasks wait (used when @grab_lock is true)
  12622. + * @mutex_init: zero if the mutex hasn't been initialized yet.
  12623. + *
  12624. + * Although @unplug and @sync_tsk may point to the same task, the @unplug
  12625. + * is used as a flag and still exists after @sync_tsk has exited and
  12626. + * @sync_tsk set to NULL.
  12627. + */
  12628. +struct hotplug_pcp {
  12629. + struct task_struct *unplug;
  12630. + struct task_struct *sync_tsk;
  12631. + int refcount;
  12632. + int grab_lock;
  12633. + struct completion synced;
  12634. + struct completion unplug_wait;
  12635. +#ifdef CONFIG_PREEMPT_RT_FULL
  12636. + /*
  12637. + * Note, on PREEMPT_RT, the hotplug lock must save the state of
  12638. + * the task, otherwise the mutex will cause the task to fail
  12639. + * to sleep when required. (Because it's called from migrate_disable())
  12640. + *
  12641. + * The spinlock_t on PREEMPT_RT is a mutex that saves the task's
  12642. + * state.
  12643. + */
  12644. + spinlock_t lock;
  12645. +#else
  12646. + struct mutex mutex;
  12647. +#endif
  12648. + int mutex_init;
  12649. +};
  12650. +
  12651. +#ifdef CONFIG_PREEMPT_RT_FULL
  12652. +# define hotplug_lock(hp) rt_spin_lock(&(hp)->lock)
  12653. +# define hotplug_unlock(hp) rt_spin_unlock(&(hp)->lock)
  12654. +#else
  12655. +# define hotplug_lock(hp) mutex_lock(&(hp)->mutex)
  12656. +# define hotplug_unlock(hp) mutex_unlock(&(hp)->mutex)
  12657. +#endif
  12658. +
  12659. +static DEFINE_PER_CPU(struct hotplug_pcp, hotplug_pcp);
  12660. +
  12661. +/**
  12662. + * pin_current_cpu - Prevent the current cpu from being unplugged
  12663. + *
  12664. + * Lightweight version of get_online_cpus() to prevent cpu from being
  12665. + * unplugged when code runs in a migration disabled region.
  12666. + *
  12667. + * Must be called with preemption disabled (preempt_count = 1)!
  12668. + */
  12669. +void pin_current_cpu(void)
  12670. +{
  12671. + struct hotplug_pcp *hp;
  12672. + int force = 0;
  12673. +
  12674. +retry:
  12675. + hp = this_cpu_ptr(&hotplug_pcp);
  12676. +
  12677. + if (!hp->unplug || hp->refcount || force || preempt_count() > 1 ||
  12678. + hp->unplug == current) {
  12679. + hp->refcount++;
  12680. + return;
  12681. + }
  12682. + if (hp->grab_lock) {
  12683. + preempt_enable();
  12684. + hotplug_lock(hp);
  12685. + hotplug_unlock(hp);
  12686. + } else {
  12687. + preempt_enable();
  12688. + /*
  12689. + * Try to push this task off of this CPU.
  12690. + */
  12691. + if (!migrate_me()) {
  12692. + preempt_disable();
  12693. + hp = this_cpu_ptr(&hotplug_pcp);
  12694. + if (!hp->grab_lock) {
  12695. + /*
  12696. + * Just let it continue it's already pinned
  12697. + * or about to sleep.
  12698. + */
  12699. + force = 1;
  12700. + goto retry;
  12701. + }
  12702. + preempt_enable();
  12703. + }
  12704. + }
  12705. + preempt_disable();
  12706. + goto retry;
  12707. +}
  12708. +
  12709. +/**
  12710. + * unpin_current_cpu - Allow unplug of current cpu
  12711. + *
  12712. + * Must be called with preemption or interrupts disabled!
  12713. + */
  12714. +void unpin_current_cpu(void)
  12715. +{
  12716. + struct hotplug_pcp *hp = this_cpu_ptr(&hotplug_pcp);
  12717. +
  12718. + WARN_ON(hp->refcount <= 0);
  12719. +
  12720. + /* This is safe. sync_unplug_thread is pinned to this cpu */
  12721. + if (!--hp->refcount && hp->unplug && hp->unplug != current)
  12722. + wake_up_process(hp->unplug);
  12723. +}
  12724. +
  12725. +static void wait_for_pinned_cpus(struct hotplug_pcp *hp)
  12726. +{
  12727. + set_current_state(TASK_UNINTERRUPTIBLE);
  12728. + while (hp->refcount) {
  12729. + schedule_preempt_disabled();
  12730. + set_current_state(TASK_UNINTERRUPTIBLE);
  12731. + }
  12732. +}
  12733. +
  12734. +static int sync_unplug_thread(void *data)
  12735. +{
  12736. + struct hotplug_pcp *hp = data;
  12737. +
  12738. + wait_for_completion(&hp->unplug_wait);
  12739. + preempt_disable();
  12740. + hp->unplug = current;
  12741. + wait_for_pinned_cpus(hp);
  12742. +
  12743. + /*
  12744. + * This thread will synchronize the cpu_down() with threads
  12745. + * that have pinned the CPU. When the pinned CPU count reaches
  12746. + * zero, we inform the cpu_down code to continue to the next step.
  12747. + */
  12748. + set_current_state(TASK_UNINTERRUPTIBLE);
  12749. + preempt_enable();
  12750. + complete(&hp->synced);
  12751. +
  12752. + /*
  12753. + * If all succeeds, the next step will need tasks to wait till
  12754. + * the CPU is offline before continuing. To do this, the grab_lock
  12755. + * is set and tasks going into pin_current_cpu() will block on the
  12756. + * mutex. But we still need to wait for those that are already in
  12757. + * pinned CPU sections. If the cpu_down() failed, the kthread_should_stop()
  12758. + * will kick this thread out.
  12759. + */
  12760. + while (!hp->grab_lock && !kthread_should_stop()) {
  12761. + schedule();
  12762. + set_current_state(TASK_UNINTERRUPTIBLE);
  12763. + }
  12764. +
  12765. + /* Make sure grab_lock is seen before we see a stale completion */
  12766. + smp_mb();
  12767. +
  12768. + /*
  12769. + * Now just before cpu_down() enters stop machine, we need to make
  12770. + * sure all tasks that are in pinned CPU sections are out, and new
  12771. + * tasks will now grab the lock, keeping them from entering pinned
  12772. + * CPU sections.
  12773. + */
  12774. + if (!kthread_should_stop()) {
  12775. + preempt_disable();
  12776. + wait_for_pinned_cpus(hp);
  12777. + preempt_enable();
  12778. + complete(&hp->synced);
  12779. + }
  12780. +
  12781. + set_current_state(TASK_UNINTERRUPTIBLE);
  12782. + while (!kthread_should_stop()) {
  12783. + schedule();
  12784. + set_current_state(TASK_UNINTERRUPTIBLE);
  12785. + }
  12786. + set_current_state(TASK_RUNNING);
  12787. +
  12788. + /*
  12789. + * Force this thread off this CPU as it's going down and
  12790. + * we don't want any more work on this CPU.
  12791. + */
  12792. + current->flags &= ~PF_NO_SETAFFINITY;
  12793. + set_cpus_allowed_ptr(current, cpu_present_mask);
  12794. + migrate_me();
  12795. + return 0;
  12796. +}
  12797. +
  12798. +static void __cpu_unplug_sync(struct hotplug_pcp *hp)
  12799. +{
  12800. + wake_up_process(hp->sync_tsk);
  12801. + wait_for_completion(&hp->synced);
  12802. +}
  12803. +
  12804. +static void __cpu_unplug_wait(unsigned int cpu)
  12805. +{
  12806. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  12807. +
  12808. + complete(&hp->unplug_wait);
  12809. + wait_for_completion(&hp->synced);
  12810. +}
  12811. +
  12812. +/*
  12813. + * Start the sync_unplug_thread on the target cpu and wait for it to
  12814. + * complete.
  12815. + */
  12816. +static int cpu_unplug_begin(unsigned int cpu)
  12817. +{
  12818. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  12819. + int err;
  12820. +
  12821. + /* Protected by cpu_hotplug.lock */
  12822. + if (!hp->mutex_init) {
  12823. +#ifdef CONFIG_PREEMPT_RT_FULL
  12824. + spin_lock_init(&hp->lock);
  12825. +#else
  12826. + mutex_init(&hp->mutex);
  12827. +#endif
  12828. + hp->mutex_init = 1;
  12829. + }
  12830. +
  12831. + /* Inform the scheduler to migrate tasks off this CPU */
  12832. + tell_sched_cpu_down_begin(cpu);
  12833. +
  12834. + init_completion(&hp->synced);
  12835. + init_completion(&hp->unplug_wait);
  12836. +
  12837. + hp->sync_tsk = kthread_create(sync_unplug_thread, hp, "sync_unplug/%d", cpu);
  12838. + if (IS_ERR(hp->sync_tsk)) {
  12839. + err = PTR_ERR(hp->sync_tsk);
  12840. + hp->sync_tsk = NULL;
  12841. + return err;
  12842. + }
  12843. + kthread_bind(hp->sync_tsk, cpu);
  12844. +
  12845. + /*
  12846. + * Wait for tasks to get out of the pinned sections,
  12847. + * it's still OK if new tasks enter. Some CPU notifiers will
  12848. + * wait for tasks that are going to enter these sections and
  12849. + * we must not have them block.
  12850. + */
  12851. + wake_up_process(hp->sync_tsk);
  12852. + return 0;
  12853. +}
  12854. +
  12855. +static void cpu_unplug_sync(unsigned int cpu)
  12856. +{
  12857. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  12858. +
  12859. + init_completion(&hp->synced);
  12860. + /* The completion needs to be initialzied before setting grab_lock */
  12861. + smp_wmb();
  12862. +
  12863. + /* Grab the mutex before setting grab_lock */
  12864. + hotplug_lock(hp);
  12865. + hp->grab_lock = 1;
  12866. +
  12867. + /*
  12868. + * The CPU notifiers have been completed.
  12869. + * Wait for tasks to get out of pinned CPU sections and have new
  12870. + * tasks block until the CPU is completely down.
  12871. + */
  12872. + __cpu_unplug_sync(hp);
  12873. +
  12874. + /* All done with the sync thread */
  12875. + kthread_stop(hp->sync_tsk);
  12876. + hp->sync_tsk = NULL;
  12877. +}
  12878. +
  12879. +static void cpu_unplug_done(unsigned int cpu)
  12880. +{
  12881. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  12882. +
  12883. + hp->unplug = NULL;
  12884. + /* Let all tasks know cpu unplug is finished before cleaning up */
  12885. + smp_wmb();
  12886. +
  12887. + if (hp->sync_tsk)
  12888. + kthread_stop(hp->sync_tsk);
  12889. +
  12890. + if (hp->grab_lock) {
  12891. + hotplug_unlock(hp);
  12892. + /* protected by cpu_hotplug.lock */
  12893. + hp->grab_lock = 0;
  12894. + }
  12895. + tell_sched_cpu_down_done(cpu);
  12896. +}
  12897. void get_online_cpus(void)
  12898. {
  12899. @@ -349,13 +632,15 @@
  12900. /* Requires cpu_add_remove_lock to be held */
  12901. static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
  12902. {
  12903. - int err, nr_calls = 0;
  12904. + int mycpu, err, nr_calls = 0;
  12905. void *hcpu = (void *)(long)cpu;
  12906. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  12907. struct take_cpu_down_param tcd_param = {
  12908. .mod = mod,
  12909. .hcpu = hcpu,
  12910. };
  12911. + cpumask_var_t cpumask;
  12912. + cpumask_var_t cpumask_org;
  12913. if (num_online_cpus() == 1)
  12914. return -EBUSY;
  12915. @@ -363,7 +648,34 @@
  12916. if (!cpu_online(cpu))
  12917. return -EINVAL;
  12918. + /* Move the downtaker off the unplug cpu */
  12919. + if (!alloc_cpumask_var(&cpumask, GFP_KERNEL))
  12920. + return -ENOMEM;
  12921. + if (!alloc_cpumask_var(&cpumask_org, GFP_KERNEL)) {
  12922. + free_cpumask_var(cpumask);
  12923. + return -ENOMEM;
  12924. + }
  12925. +
  12926. + cpumask_copy(cpumask_org, tsk_cpus_allowed(current));
  12927. + cpumask_andnot(cpumask, cpu_online_mask, cpumask_of(cpu));
  12928. + set_cpus_allowed_ptr(current, cpumask);
  12929. + free_cpumask_var(cpumask);
  12930. + migrate_disable();
  12931. + mycpu = smp_processor_id();
  12932. + if (mycpu == cpu) {
  12933. + printk(KERN_ERR "Yuck! Still on unplug CPU\n!");
  12934. + migrate_enable();
  12935. + err = -EBUSY;
  12936. + goto restore_cpus;
  12937. + }
  12938. + migrate_enable();
  12939. +
  12940. cpu_hotplug_begin();
  12941. + err = cpu_unplug_begin(cpu);
  12942. + if (err) {
  12943. + printk("cpu_unplug_begin(%d) failed\n", cpu);
  12944. + goto out_cancel;
  12945. + }
  12946. err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
  12947. if (err) {
  12948. @@ -389,8 +701,12 @@
  12949. #endif
  12950. synchronize_rcu();
  12951. + __cpu_unplug_wait(cpu);
  12952. smpboot_park_threads(cpu);
  12953. + /* Notifiers are done. Don't let any more tasks pin this CPU. */
  12954. + cpu_unplug_sync(cpu);
  12955. +
  12956. /*
  12957. * So now all preempt/rcu users must observe !cpu_active().
  12958. */
  12959. @@ -427,9 +743,14 @@
  12960. check_for_tasks(cpu);
  12961. out_release:
  12962. + cpu_unplug_done(cpu);
  12963. +out_cancel:
  12964. cpu_hotplug_done();
  12965. if (!err)
  12966. cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
  12967. +restore_cpus:
  12968. + set_cpus_allowed_ptr(current, cpumask_org);
  12969. + free_cpumask_var(cpumask_org);
  12970. return err;
  12971. }
  12972. diff -Nur linux-4.1.13.orig/kernel/debug/kdb/kdb_io.c linux-4.1.13/kernel/debug/kdb/kdb_io.c
  12973. --- linux-4.1.13.orig/kernel/debug/kdb/kdb_io.c 2015-11-09 23:34:10.000000000 +0100
  12974. +++ linux-4.1.13/kernel/debug/kdb/kdb_io.c 2015-11-29 09:23:09.633612589 +0100
  12975. @@ -554,7 +554,6 @@
  12976. int linecount;
  12977. int colcount;
  12978. int logging, saved_loglevel = 0;
  12979. - int saved_trap_printk;
  12980. int got_printf_lock = 0;
  12981. int retlen = 0;
  12982. int fnd, len;
  12983. @@ -565,8 +564,6 @@
  12984. unsigned long uninitialized_var(flags);
  12985. preempt_disable();
  12986. - saved_trap_printk = kdb_trap_printk;
  12987. - kdb_trap_printk = 0;
  12988. /* Serialize kdb_printf if multiple cpus try to write at once.
  12989. * But if any cpu goes recursive in kdb, just print the output,
  12990. @@ -855,7 +852,6 @@
  12991. } else {
  12992. __release(kdb_printf_lock);
  12993. }
  12994. - kdb_trap_printk = saved_trap_printk;
  12995. preempt_enable();
  12996. return retlen;
  12997. }
  12998. @@ -865,9 +861,11 @@
  12999. va_list ap;
  13000. int r;
  13001. + kdb_trap_printk++;
  13002. va_start(ap, fmt);
  13003. r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
  13004. va_end(ap);
  13005. + kdb_trap_printk--;
  13006. return r;
  13007. }
  13008. diff -Nur linux-4.1.13.orig/kernel/events/core.c linux-4.1.13/kernel/events/core.c
  13009. --- linux-4.1.13.orig/kernel/events/core.c 2015-11-09 23:34:10.000000000 +0100
  13010. +++ linux-4.1.13/kernel/events/core.c 2015-11-29 09:23:09.633612589 +0100
  13011. @@ -6925,6 +6925,7 @@
  13012. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  13013. hwc->hrtimer.function = perf_swevent_hrtimer;
  13014. + hwc->hrtimer.irqsafe = 1;
  13015. /*
  13016. * Since hrtimers have a fixed rate, we can do a static freq->period
  13017. diff -Nur linux-4.1.13.orig/kernel/exit.c linux-4.1.13/kernel/exit.c
  13018. --- linux-4.1.13.orig/kernel/exit.c 2015-11-09 23:34:10.000000000 +0100
  13019. +++ linux-4.1.13/kernel/exit.c 2015-11-29 09:23:09.633612589 +0100
  13020. @@ -144,7 +144,7 @@
  13021. * Do this under ->siglock, we can race with another thread
  13022. * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
  13023. */
  13024. - flush_sigqueue(&tsk->pending);
  13025. + flush_task_sigqueue(tsk);
  13026. tsk->sighand = NULL;
  13027. spin_unlock(&sighand->siglock);
  13028. diff -Nur linux-4.1.13.orig/kernel/fork.c linux-4.1.13/kernel/fork.c
  13029. --- linux-4.1.13.orig/kernel/fork.c 2015-11-09 23:34:10.000000000 +0100
  13030. +++ linux-4.1.13/kernel/fork.c 2015-11-29 09:23:09.633612589 +0100
  13031. @@ -108,7 +108,7 @@
  13032. DEFINE_PER_CPU(unsigned long, process_counts) = 0;
  13033. -__cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
  13034. +DEFINE_RWLOCK(tasklist_lock); /* outer */
  13035. #ifdef CONFIG_PROVE_RCU
  13036. int lockdep_tasklist_lock_is_held(void)
  13037. @@ -244,7 +244,9 @@
  13038. if (atomic_dec_and_test(&sig->sigcnt))
  13039. free_signal_struct(sig);
  13040. }
  13041. -
  13042. +#ifdef CONFIG_PREEMPT_RT_BASE
  13043. +static
  13044. +#endif
  13045. void __put_task_struct(struct task_struct *tsk)
  13046. {
  13047. WARN_ON(!tsk->exit_state);
  13048. @@ -260,7 +262,18 @@
  13049. if (!profile_handoff_task(tsk))
  13050. free_task(tsk);
  13051. }
  13052. +#ifndef CONFIG_PREEMPT_RT_BASE
  13053. EXPORT_SYMBOL_GPL(__put_task_struct);
  13054. +#else
  13055. +void __put_task_struct_cb(struct rcu_head *rhp)
  13056. +{
  13057. + struct task_struct *tsk = container_of(rhp, struct task_struct, put_rcu);
  13058. +
  13059. + __put_task_struct(tsk);
  13060. +
  13061. +}
  13062. +EXPORT_SYMBOL_GPL(__put_task_struct_cb);
  13063. +#endif
  13064. void __init __weak arch_task_cache_init(void) { }
  13065. @@ -680,6 +693,19 @@
  13066. }
  13067. EXPORT_SYMBOL_GPL(__mmdrop);
  13068. +#ifdef CONFIG_PREEMPT_RT_BASE
  13069. +/*
  13070. + * RCU callback for delayed mm drop. Not strictly rcu, but we don't
  13071. + * want another facility to make this work.
  13072. + */
  13073. +void __mmdrop_delayed(struct rcu_head *rhp)
  13074. +{
  13075. + struct mm_struct *mm = container_of(rhp, struct mm_struct, delayed_drop);
  13076. +
  13077. + __mmdrop(mm);
  13078. +}
  13079. +#endif
  13080. +
  13081. /*
  13082. * Decrement the use count and release all resources for an mm.
  13083. */
  13084. @@ -1214,6 +1240,9 @@
  13085. */
  13086. static void posix_cpu_timers_init(struct task_struct *tsk)
  13087. {
  13088. +#ifdef CONFIG_PREEMPT_RT_BASE
  13089. + tsk->posix_timer_list = NULL;
  13090. +#endif
  13091. tsk->cputime_expires.prof_exp = 0;
  13092. tsk->cputime_expires.virt_exp = 0;
  13093. tsk->cputime_expires.sched_exp = 0;
  13094. @@ -1338,6 +1367,7 @@
  13095. spin_lock_init(&p->alloc_lock);
  13096. init_sigpending(&p->pending);
  13097. + p->sigqueue_cache = NULL;
  13098. p->utime = p->stime = p->gtime = 0;
  13099. p->utimescaled = p->stimescaled = 0;
  13100. @@ -1345,7 +1375,8 @@
  13101. p->prev_cputime.utime = p->prev_cputime.stime = 0;
  13102. #endif
  13103. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  13104. - seqlock_init(&p->vtime_seqlock);
  13105. + raw_spin_lock_init(&p->vtime_lock);
  13106. + seqcount_init(&p->vtime_seq);
  13107. p->vtime_snap = 0;
  13108. p->vtime_snap_whence = VTIME_SLEEPING;
  13109. #endif
  13110. @@ -1396,6 +1427,9 @@
  13111. p->hardirq_context = 0;
  13112. p->softirq_context = 0;
  13113. #endif
  13114. +
  13115. + p->pagefault_disabled = 0;
  13116. +
  13117. #ifdef CONFIG_LOCKDEP
  13118. p->lockdep_depth = 0; /* no locks held yet */
  13119. p->curr_chain_key = 0;
  13120. diff -Nur linux-4.1.13.orig/kernel/futex.c linux-4.1.13/kernel/futex.c
  13121. --- linux-4.1.13.orig/kernel/futex.c 2015-11-09 23:34:10.000000000 +0100
  13122. +++ linux-4.1.13/kernel/futex.c 2015-11-29 09:23:09.633612589 +0100
  13123. @@ -738,7 +738,9 @@
  13124. * task still owns the PI-state:
  13125. */
  13126. if (head->next != next) {
  13127. + raw_spin_unlock_irq(&curr->pi_lock);
  13128. spin_unlock(&hb->lock);
  13129. + raw_spin_lock_irq(&curr->pi_lock);
  13130. continue;
  13131. }
  13132. @@ -1090,9 +1092,11 @@
  13133. /*
  13134. * The hash bucket lock must be held when this is called.
  13135. - * Afterwards, the futex_q must not be accessed.
  13136. + * Afterwards, the futex_q must not be accessed. Callers
  13137. + * must ensure to later call wake_up_q() for the actual
  13138. + * wakeups to occur.
  13139. */
  13140. -static void wake_futex(struct futex_q *q)
  13141. +static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
  13142. {
  13143. struct task_struct *p = q->task;
  13144. @@ -1100,14 +1104,10 @@
  13145. return;
  13146. /*
  13147. - * We set q->lock_ptr = NULL _before_ we wake up the task. If
  13148. - * a non-futex wake up happens on another CPU then the task
  13149. - * might exit and p would dereference a non-existing task
  13150. - * struct. Prevent this by holding a reference on p across the
  13151. - * wake up.
  13152. + * Queue the task for later wakeup for after we've released
  13153. + * the hb->lock. wake_q_add() grabs reference to p.
  13154. */
  13155. - get_task_struct(p);
  13156. -
  13157. + wake_q_add(wake_q, p);
  13158. __unqueue_futex(q);
  13159. /*
  13160. * The waiting task can free the futex_q as soon as
  13161. @@ -1117,16 +1117,15 @@
  13162. */
  13163. smp_wmb();
  13164. q->lock_ptr = NULL;
  13165. -
  13166. - wake_up_state(p, TASK_NORMAL);
  13167. - put_task_struct(p);
  13168. }
  13169. -static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
  13170. +static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  13171. + struct futex_hash_bucket *hb)
  13172. {
  13173. struct task_struct *new_owner;
  13174. struct futex_pi_state *pi_state = this->pi_state;
  13175. u32 uninitialized_var(curval), newval;
  13176. + bool deboost;
  13177. int ret = 0;
  13178. if (!pi_state)
  13179. @@ -1178,7 +1177,17 @@
  13180. raw_spin_unlock_irq(&new_owner->pi_lock);
  13181. raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  13182. - rt_mutex_unlock(&pi_state->pi_mutex);
  13183. +
  13184. + deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex);
  13185. +
  13186. + /*
  13187. + * We deboost after dropping hb->lock. That prevents a double
  13188. + * wakeup on RT.
  13189. + */
  13190. + spin_unlock(&hb->lock);
  13191. +
  13192. + if (deboost)
  13193. + rt_mutex_adjust_prio(current);
  13194. return 0;
  13195. }
  13196. @@ -1217,6 +1226,7 @@
  13197. struct futex_q *this, *next;
  13198. union futex_key key = FUTEX_KEY_INIT;
  13199. int ret;
  13200. + WAKE_Q(wake_q);
  13201. if (!bitset)
  13202. return -EINVAL;
  13203. @@ -1244,13 +1254,14 @@
  13204. if (!(this->bitset & bitset))
  13205. continue;
  13206. - wake_futex(this);
  13207. + mark_wake_futex(&wake_q, this);
  13208. if (++ret >= nr_wake)
  13209. break;
  13210. }
  13211. }
  13212. spin_unlock(&hb->lock);
  13213. + wake_up_q(&wake_q);
  13214. out_put_key:
  13215. put_futex_key(&key);
  13216. out:
  13217. @@ -1269,6 +1280,7 @@
  13218. struct futex_hash_bucket *hb1, *hb2;
  13219. struct futex_q *this, *next;
  13220. int ret, op_ret;
  13221. + WAKE_Q(wake_q);
  13222. retry:
  13223. ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
  13224. @@ -1320,7 +1332,7 @@
  13225. ret = -EINVAL;
  13226. goto out_unlock;
  13227. }
  13228. - wake_futex(this);
  13229. + mark_wake_futex(&wake_q, this);
  13230. if (++ret >= nr_wake)
  13231. break;
  13232. }
  13233. @@ -1334,7 +1346,7 @@
  13234. ret = -EINVAL;
  13235. goto out_unlock;
  13236. }
  13237. - wake_futex(this);
  13238. + mark_wake_futex(&wake_q, this);
  13239. if (++op_ret >= nr_wake2)
  13240. break;
  13241. }
  13242. @@ -1344,6 +1356,7 @@
  13243. out_unlock:
  13244. double_unlock_hb(hb1, hb2);
  13245. + wake_up_q(&wake_q);
  13246. out_put_keys:
  13247. put_futex_key(&key2);
  13248. out_put_key1:
  13249. @@ -1503,6 +1516,7 @@
  13250. struct futex_pi_state *pi_state = NULL;
  13251. struct futex_hash_bucket *hb1, *hb2;
  13252. struct futex_q *this, *next;
  13253. + WAKE_Q(wake_q);
  13254. if (requeue_pi) {
  13255. /*
  13256. @@ -1679,7 +1693,7 @@
  13257. * woken by futex_unlock_pi().
  13258. */
  13259. if (++task_count <= nr_wake && !requeue_pi) {
  13260. - wake_futex(this);
  13261. + mark_wake_futex(&wake_q, this);
  13262. continue;
  13263. }
  13264. @@ -1705,6 +1719,16 @@
  13265. requeue_pi_wake_futex(this, &key2, hb2);
  13266. drop_count++;
  13267. continue;
  13268. + } else if (ret == -EAGAIN) {
  13269. + /*
  13270. + * Waiter was woken by timeout or
  13271. + * signal and has set pi_blocked_on to
  13272. + * PI_WAKEUP_INPROGRESS before we
  13273. + * tried to enqueue it on the rtmutex.
  13274. + */
  13275. + this->pi_state = NULL;
  13276. + free_pi_state(pi_state);
  13277. + continue;
  13278. } else if (ret) {
  13279. /* -EDEADLK */
  13280. this->pi_state = NULL;
  13281. @@ -1719,6 +1743,7 @@
  13282. out_unlock:
  13283. free_pi_state(pi_state);
  13284. double_unlock_hb(hb1, hb2);
  13285. + wake_up_q(&wake_q);
  13286. hb_waiters_dec(hb2);
  13287. /*
  13288. @@ -2412,13 +2437,26 @@
  13289. */
  13290. match = futex_top_waiter(hb, &key);
  13291. if (match) {
  13292. - ret = wake_futex_pi(uaddr, uval, match);
  13293. + ret = wake_futex_pi(uaddr, uval, match, hb);
  13294. +
  13295. + /*
  13296. + * In case of success wake_futex_pi dropped the hash
  13297. + * bucket lock.
  13298. + */
  13299. + if (!ret)
  13300. + goto out_putkey;
  13301. +
  13302. /*
  13303. * The atomic access to the futex value generated a
  13304. * pagefault, so retry the user-access and the wakeup:
  13305. */
  13306. if (ret == -EFAULT)
  13307. goto pi_faulted;
  13308. +
  13309. + /*
  13310. + * wake_futex_pi has detected invalid state. Tell user
  13311. + * space.
  13312. + */
  13313. goto out_unlock;
  13314. }
  13315. @@ -2439,6 +2477,7 @@
  13316. out_unlock:
  13317. spin_unlock(&hb->lock);
  13318. +out_putkey:
  13319. put_futex_key(&key);
  13320. return ret;
  13321. @@ -2549,7 +2588,7 @@
  13322. struct hrtimer_sleeper timeout, *to = NULL;
  13323. struct rt_mutex_waiter rt_waiter;
  13324. struct rt_mutex *pi_mutex = NULL;
  13325. - struct futex_hash_bucket *hb;
  13326. + struct futex_hash_bucket *hb, *hb2;
  13327. union futex_key key2 = FUTEX_KEY_INIT;
  13328. struct futex_q q = futex_q_init;
  13329. int res, ret;
  13330. @@ -2574,10 +2613,7 @@
  13331. * The waiter is allocated on our stack, manipulated by the requeue
  13332. * code while we sleep on uaddr.
  13333. */
  13334. - debug_rt_mutex_init_waiter(&rt_waiter);
  13335. - RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
  13336. - RB_CLEAR_NODE(&rt_waiter.tree_entry);
  13337. - rt_waiter.task = NULL;
  13338. + rt_mutex_init_waiter(&rt_waiter, false);
  13339. ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
  13340. if (unlikely(ret != 0))
  13341. @@ -2608,20 +2644,55 @@
  13342. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  13343. futex_wait_queue_me(hb, &q, to);
  13344. - spin_lock(&hb->lock);
  13345. - ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  13346. - spin_unlock(&hb->lock);
  13347. - if (ret)
  13348. - goto out_put_keys;
  13349. + /*
  13350. + * On RT we must avoid races with requeue and trying to block
  13351. + * on two mutexes (hb->lock and uaddr2's rtmutex) by
  13352. + * serializing access to pi_blocked_on with pi_lock.
  13353. + */
  13354. + raw_spin_lock_irq(&current->pi_lock);
  13355. + if (current->pi_blocked_on) {
  13356. + /*
  13357. + * We have been requeued or are in the process of
  13358. + * being requeued.
  13359. + */
  13360. + raw_spin_unlock_irq(&current->pi_lock);
  13361. + } else {
  13362. + /*
  13363. + * Setting pi_blocked_on to PI_WAKEUP_INPROGRESS
  13364. + * prevents a concurrent requeue from moving us to the
  13365. + * uaddr2 rtmutex. After that we can safely acquire
  13366. + * (and possibly block on) hb->lock.
  13367. + */
  13368. + current->pi_blocked_on = PI_WAKEUP_INPROGRESS;
  13369. + raw_spin_unlock_irq(&current->pi_lock);
  13370. +
  13371. + spin_lock(&hb->lock);
  13372. +
  13373. + /*
  13374. + * Clean up pi_blocked_on. We might leak it otherwise
  13375. + * when we succeeded with the hb->lock in the fast
  13376. + * path.
  13377. + */
  13378. + raw_spin_lock_irq(&current->pi_lock);
  13379. + current->pi_blocked_on = NULL;
  13380. + raw_spin_unlock_irq(&current->pi_lock);
  13381. +
  13382. + ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  13383. + spin_unlock(&hb->lock);
  13384. + if (ret)
  13385. + goto out_put_keys;
  13386. + }
  13387. /*
  13388. - * In order for us to be here, we know our q.key == key2, and since
  13389. - * we took the hb->lock above, we also know that futex_requeue() has
  13390. - * completed and we no longer have to concern ourselves with a wakeup
  13391. - * race with the atomic proxy lock acquisition by the requeue code. The
  13392. - * futex_requeue dropped our key1 reference and incremented our key2
  13393. - * reference count.
  13394. + * In order to be here, we have either been requeued, are in
  13395. + * the process of being requeued, or requeue successfully
  13396. + * acquired uaddr2 on our behalf. If pi_blocked_on was
  13397. + * non-null above, we may be racing with a requeue. Do not
  13398. + * rely on q->lock_ptr to be hb2->lock until after blocking on
  13399. + * hb->lock or hb2->lock. The futex_requeue dropped our key1
  13400. + * reference and incremented our key2 reference count.
  13401. */
  13402. + hb2 = hash_futex(&key2);
  13403. /* Check if the requeue code acquired the second futex for us. */
  13404. if (!q.rt_waiter) {
  13405. @@ -2630,9 +2701,10 @@
  13406. * did a lock-steal - fix up the PI-state in that case.
  13407. */
  13408. if (q.pi_state && (q.pi_state->owner != current)) {
  13409. - spin_lock(q.lock_ptr);
  13410. + spin_lock(&hb2->lock);
  13411. + BUG_ON(&hb2->lock != q.lock_ptr);
  13412. ret = fixup_pi_state_owner(uaddr2, &q, current);
  13413. - spin_unlock(q.lock_ptr);
  13414. + spin_unlock(&hb2->lock);
  13415. }
  13416. } else {
  13417. /*
  13418. @@ -2645,7 +2717,8 @@
  13419. ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter);
  13420. debug_rt_mutex_free_waiter(&rt_waiter);
  13421. - spin_lock(q.lock_ptr);
  13422. + spin_lock(&hb2->lock);
  13423. + BUG_ON(&hb2->lock != q.lock_ptr);
  13424. /*
  13425. * Fixup the pi_state owner and possibly acquire the lock if we
  13426. * haven't already.
  13427. diff -Nur linux-4.1.13.orig/kernel/irq/handle.c linux-4.1.13/kernel/irq/handle.c
  13428. --- linux-4.1.13.orig/kernel/irq/handle.c 2015-11-09 23:34:10.000000000 +0100
  13429. +++ linux-4.1.13/kernel/irq/handle.c 2015-11-29 09:23:09.633612589 +0100
  13430. @@ -133,6 +133,8 @@
  13431. irqreturn_t
  13432. handle_irq_event_percpu(struct irq_desc *desc, struct irqaction *action)
  13433. {
  13434. + struct pt_regs *regs = get_irq_regs();
  13435. + u64 ip = regs ? instruction_pointer(regs) : 0;
  13436. irqreturn_t retval = IRQ_NONE;
  13437. unsigned int flags = 0, irq = desc->irq_data.irq;
  13438. @@ -173,7 +175,11 @@
  13439. action = action->next;
  13440. } while (action);
  13441. - add_interrupt_randomness(irq, flags);
  13442. +#ifndef CONFIG_PREEMPT_RT_FULL
  13443. + add_interrupt_randomness(irq, flags, ip);
  13444. +#else
  13445. + desc->random_ip = ip;
  13446. +#endif
  13447. if (!noirqdebug)
  13448. note_interrupt(irq, desc, retval);
  13449. diff -Nur linux-4.1.13.orig/kernel/irq/manage.c linux-4.1.13/kernel/irq/manage.c
  13450. --- linux-4.1.13.orig/kernel/irq/manage.c 2015-11-09 23:34:10.000000000 +0100
  13451. +++ linux-4.1.13/kernel/irq/manage.c 2015-11-29 09:23:09.633612589 +0100
  13452. @@ -22,6 +22,7 @@
  13453. #include "internals.h"
  13454. #ifdef CONFIG_IRQ_FORCED_THREADING
  13455. +# ifndef CONFIG_PREEMPT_RT_BASE
  13456. __read_mostly bool force_irqthreads;
  13457. static int __init setup_forced_irqthreads(char *arg)
  13458. @@ -30,6 +31,7 @@
  13459. return 0;
  13460. }
  13461. early_param("threadirqs", setup_forced_irqthreads);
  13462. +# endif
  13463. #endif
  13464. static void __synchronize_hardirq(struct irq_desc *desc)
  13465. @@ -179,6 +181,62 @@
  13466. irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
  13467. #endif
  13468. +#ifdef CONFIG_PREEMPT_RT_FULL
  13469. +static void _irq_affinity_notify(struct irq_affinity_notify *notify);
  13470. +static struct task_struct *set_affinity_helper;
  13471. +static LIST_HEAD(affinity_list);
  13472. +static DEFINE_RAW_SPINLOCK(affinity_list_lock);
  13473. +
  13474. +static int set_affinity_thread(void *unused)
  13475. +{
  13476. + while (1) {
  13477. + struct irq_affinity_notify *notify;
  13478. + int empty;
  13479. +
  13480. + set_current_state(TASK_INTERRUPTIBLE);
  13481. +
  13482. + raw_spin_lock_irq(&affinity_list_lock);
  13483. + empty = list_empty(&affinity_list);
  13484. + raw_spin_unlock_irq(&affinity_list_lock);
  13485. +
  13486. + if (empty)
  13487. + schedule();
  13488. + if (kthread_should_stop())
  13489. + break;
  13490. + set_current_state(TASK_RUNNING);
  13491. +try_next:
  13492. + notify = NULL;
  13493. +
  13494. + raw_spin_lock_irq(&affinity_list_lock);
  13495. + if (!list_empty(&affinity_list)) {
  13496. + notify = list_first_entry(&affinity_list,
  13497. + struct irq_affinity_notify, list);
  13498. + list_del_init(&notify->list);
  13499. + }
  13500. + raw_spin_unlock_irq(&affinity_list_lock);
  13501. +
  13502. + if (!notify)
  13503. + continue;
  13504. + _irq_affinity_notify(notify);
  13505. + goto try_next;
  13506. + }
  13507. + return 0;
  13508. +}
  13509. +
  13510. +static void init_helper_thread(void)
  13511. +{
  13512. + if (set_affinity_helper)
  13513. + return;
  13514. + set_affinity_helper = kthread_run(set_affinity_thread, NULL,
  13515. + "affinity-cb");
  13516. + WARN_ON(IS_ERR(set_affinity_helper));
  13517. +}
  13518. +#else
  13519. +
  13520. +static inline void init_helper_thread(void) { }
  13521. +
  13522. +#endif
  13523. +
  13524. int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
  13525. bool force)
  13526. {
  13527. @@ -218,7 +276,17 @@
  13528. if (desc->affinity_notify) {
  13529. kref_get(&desc->affinity_notify->kref);
  13530. +
  13531. +#ifdef CONFIG_PREEMPT_RT_FULL
  13532. + raw_spin_lock(&affinity_list_lock);
  13533. + if (list_empty(&desc->affinity_notify->list))
  13534. + list_add_tail(&affinity_list,
  13535. + &desc->affinity_notify->list);
  13536. + raw_spin_unlock(&affinity_list_lock);
  13537. + wake_up_process(set_affinity_helper);
  13538. +#else
  13539. schedule_work(&desc->affinity_notify->work);
  13540. +#endif
  13541. }
  13542. irqd_set(data, IRQD_AFFINITY_SET);
  13543. @@ -256,10 +324,8 @@
  13544. }
  13545. EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
  13546. -static void irq_affinity_notify(struct work_struct *work)
  13547. +static void _irq_affinity_notify(struct irq_affinity_notify *notify)
  13548. {
  13549. - struct irq_affinity_notify *notify =
  13550. - container_of(work, struct irq_affinity_notify, work);
  13551. struct irq_desc *desc = irq_to_desc(notify->irq);
  13552. cpumask_var_t cpumask;
  13553. unsigned long flags;
  13554. @@ -281,6 +347,13 @@
  13555. kref_put(&notify->kref, notify->release);
  13556. }
  13557. +static void irq_affinity_notify(struct work_struct *work)
  13558. +{
  13559. + struct irq_affinity_notify *notify =
  13560. + container_of(work, struct irq_affinity_notify, work);
  13561. + _irq_affinity_notify(notify);
  13562. +}
  13563. +
  13564. /**
  13565. * irq_set_affinity_notifier - control notification of IRQ affinity changes
  13566. * @irq: Interrupt for which to enable/disable notification
  13567. @@ -310,6 +383,8 @@
  13568. notify->irq = irq;
  13569. kref_init(&notify->kref);
  13570. INIT_WORK(&notify->work, irq_affinity_notify);
  13571. + INIT_LIST_HEAD(&notify->list);
  13572. + init_helper_thread();
  13573. }
  13574. raw_spin_lock_irqsave(&desc->lock, flags);
  13575. @@ -697,6 +772,12 @@
  13576. return IRQ_NONE;
  13577. }
  13578. +static irqreturn_t irq_forced_secondary_handler(int irq, void *dev_id)
  13579. +{
  13580. + WARN(1, "Secondary action handler called for irq %d\n", irq);
  13581. + return IRQ_NONE;
  13582. +}
  13583. +
  13584. static int irq_wait_for_interrupt(struct irqaction *action)
  13585. {
  13586. set_current_state(TASK_INTERRUPTIBLE);
  13587. @@ -723,7 +804,8 @@
  13588. static void irq_finalize_oneshot(struct irq_desc *desc,
  13589. struct irqaction *action)
  13590. {
  13591. - if (!(desc->istate & IRQS_ONESHOT))
  13592. + if (!(desc->istate & IRQS_ONESHOT) ||
  13593. + action->handler == irq_forced_secondary_handler)
  13594. return;
  13595. again:
  13596. chip_bus_lock(desc);
  13597. @@ -825,7 +907,15 @@
  13598. local_bh_disable();
  13599. ret = action->thread_fn(action->irq, action->dev_id);
  13600. irq_finalize_oneshot(desc, action);
  13601. - local_bh_enable();
  13602. + /*
  13603. + * Interrupts which have real time requirements can be set up
  13604. + * to avoid softirq processing in the thread handler. This is
  13605. + * safe as these interrupts do not raise soft interrupts.
  13606. + */
  13607. + if (irq_settings_no_softirq_call(desc))
  13608. + _local_bh_enable();
  13609. + else
  13610. + local_bh_enable();
  13611. return ret;
  13612. }
  13613. @@ -877,6 +967,18 @@
  13614. irq_finalize_oneshot(desc, action);
  13615. }
  13616. +static void irq_wake_secondary(struct irq_desc *desc, struct irqaction *action)
  13617. +{
  13618. + struct irqaction *secondary = action->secondary;
  13619. +
  13620. + if (WARN_ON_ONCE(!secondary))
  13621. + return;
  13622. +
  13623. + raw_spin_lock_irq(&desc->lock);
  13624. + __irq_wake_thread(desc, secondary);
  13625. + raw_spin_unlock_irq(&desc->lock);
  13626. +}
  13627. +
  13628. /*
  13629. * Interrupt handler thread
  13630. */
  13631. @@ -907,7 +1009,15 @@
  13632. action_ret = handler_fn(desc, action);
  13633. if (action_ret == IRQ_HANDLED)
  13634. atomic_inc(&desc->threads_handled);
  13635. + if (action_ret == IRQ_WAKE_THREAD)
  13636. + irq_wake_secondary(desc, action);
  13637. +#ifdef CONFIG_PREEMPT_RT_FULL
  13638. + migrate_disable();
  13639. + add_interrupt_randomness(action->irq, 0,
  13640. + desc->random_ip ^ (unsigned long) action);
  13641. + migrate_enable();
  13642. +#endif
  13643. wake_threads_waitq(desc);
  13644. }
  13645. @@ -951,20 +1061,36 @@
  13646. }
  13647. EXPORT_SYMBOL_GPL(irq_wake_thread);
  13648. -static void irq_setup_forced_threading(struct irqaction *new)
  13649. +static int irq_setup_forced_threading(struct irqaction *new)
  13650. {
  13651. if (!force_irqthreads)
  13652. - return;
  13653. + return 0;
  13654. if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
  13655. - return;
  13656. + return 0;
  13657. new->flags |= IRQF_ONESHOT;
  13658. - if (!new->thread_fn) {
  13659. - set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
  13660. - new->thread_fn = new->handler;
  13661. - new->handler = irq_default_primary_handler;
  13662. - }
  13663. + /*
  13664. + * Handle the case where we have a real primary handler and a
  13665. + * thread handler. We force thread them as well by creating a
  13666. + * secondary action.
  13667. + */
  13668. + if (new->handler != irq_default_primary_handler && new->thread_fn) {
  13669. + /* Allocate the secondary action */
  13670. + new->secondary = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  13671. + if (!new->secondary)
  13672. + return -ENOMEM;
  13673. + new->secondary->handler = irq_forced_secondary_handler;
  13674. + new->secondary->thread_fn = new->thread_fn;
  13675. + new->secondary->dev_id = new->dev_id;
  13676. + new->secondary->irq = new->irq;
  13677. + new->secondary->name = new->name;
  13678. + }
  13679. + /* Deal with the primary handler */
  13680. + set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
  13681. + new->thread_fn = new->handler;
  13682. + new->handler = irq_default_primary_handler;
  13683. + return 0;
  13684. }
  13685. static int irq_request_resources(struct irq_desc *desc)
  13686. @@ -984,6 +1110,48 @@
  13687. c->irq_release_resources(d);
  13688. }
  13689. +static int
  13690. +setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary)
  13691. +{
  13692. + struct task_struct *t;
  13693. + struct sched_param param = {
  13694. + .sched_priority = MAX_USER_RT_PRIO/2,
  13695. + };
  13696. +
  13697. + if (!secondary) {
  13698. + t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
  13699. + new->name);
  13700. + } else {
  13701. + t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq,
  13702. + new->name);
  13703. + param.sched_priority += 1;
  13704. + }
  13705. +
  13706. + if (IS_ERR(t))
  13707. + return PTR_ERR(t);
  13708. +
  13709. + sched_setscheduler_nocheck(t, SCHED_FIFO, &param);
  13710. +
  13711. + /*
  13712. + * We keep the reference to the task struct even if
  13713. + * the thread dies to avoid that the interrupt code
  13714. + * references an already freed task_struct.
  13715. + */
  13716. + get_task_struct(t);
  13717. + new->thread = t;
  13718. + /*
  13719. + * Tell the thread to set its affinity. This is
  13720. + * important for shared interrupt handlers as we do
  13721. + * not invoke setup_affinity() for the secondary
  13722. + * handlers as everything is already set up. Even for
  13723. + * interrupts marked with IRQF_NO_BALANCE this is
  13724. + * correct as we want the thread to move to the cpu(s)
  13725. + * on which the requesting code placed the interrupt.
  13726. + */
  13727. + set_bit(IRQTF_AFFINITY, &new->thread_flags);
  13728. + return 0;
  13729. +}
  13730. +
  13731. /*
  13732. * Internal function to register an irqaction - typically used to
  13733. * allocate special interrupts that are part of the architecture.
  13734. @@ -1004,6 +1172,8 @@
  13735. if (!try_module_get(desc->owner))
  13736. return -ENODEV;
  13737. + new->irq = irq;
  13738. +
  13739. /*
  13740. * Check whether the interrupt nests into another interrupt
  13741. * thread.
  13742. @@ -1021,8 +1191,11 @@
  13743. */
  13744. new->handler = irq_nested_primary_handler;
  13745. } else {
  13746. - if (irq_settings_can_thread(desc))
  13747. - irq_setup_forced_threading(new);
  13748. + if (irq_settings_can_thread(desc)) {
  13749. + ret = irq_setup_forced_threading(new);
  13750. + if (ret)
  13751. + goto out_mput;
  13752. + }
  13753. }
  13754. /*
  13755. @@ -1031,37 +1204,14 @@
  13756. * thread.
  13757. */
  13758. if (new->thread_fn && !nested) {
  13759. - struct task_struct *t;
  13760. - static const struct sched_param param = {
  13761. - .sched_priority = MAX_USER_RT_PRIO/2,
  13762. - };
  13763. -
  13764. - t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
  13765. - new->name);
  13766. - if (IS_ERR(t)) {
  13767. - ret = PTR_ERR(t);
  13768. + ret = setup_irq_thread(new, irq, false);
  13769. + if (ret)
  13770. goto out_mput;
  13771. + if (new->secondary) {
  13772. + ret = setup_irq_thread(new->secondary, irq, true);
  13773. + if (ret)
  13774. + goto out_thread;
  13775. }
  13776. -
  13777. - sched_setscheduler_nocheck(t, SCHED_FIFO, &param);
  13778. -
  13779. - /*
  13780. - * We keep the reference to the task struct even if
  13781. - * the thread dies to avoid that the interrupt code
  13782. - * references an already freed task_struct.
  13783. - */
  13784. - get_task_struct(t);
  13785. - new->thread = t;
  13786. - /*
  13787. - * Tell the thread to set its affinity. This is
  13788. - * important for shared interrupt handlers as we do
  13789. - * not invoke setup_affinity() for the secondary
  13790. - * handlers as everything is already set up. Even for
  13791. - * interrupts marked with IRQF_NO_BALANCE this is
  13792. - * correct as we want the thread to move to the cpu(s)
  13793. - * on which the requesting code placed the interrupt.
  13794. - */
  13795. - set_bit(IRQTF_AFFINITY, &new->thread_flags);
  13796. }
  13797. if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
  13798. @@ -1221,6 +1371,9 @@
  13799. irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
  13800. }
  13801. + if (new->flags & IRQF_NO_SOFTIRQ_CALL)
  13802. + irq_settings_set_no_softirq_call(desc);
  13803. +
  13804. /* Set default affinity mask once everything is setup */
  13805. setup_affinity(irq, desc, mask);
  13806. @@ -1234,7 +1387,6 @@
  13807. irq, nmsk, omsk);
  13808. }
  13809. - new->irq = irq;
  13810. *old_ptr = new;
  13811. irq_pm_install_action(desc, new);
  13812. @@ -1260,6 +1412,8 @@
  13813. */
  13814. if (new->thread)
  13815. wake_up_process(new->thread);
  13816. + if (new->secondary)
  13817. + wake_up_process(new->secondary->thread);
  13818. register_irq_proc(irq, desc);
  13819. new->dir = NULL;
  13820. @@ -1290,6 +1444,13 @@
  13821. kthread_stop(t);
  13822. put_task_struct(t);
  13823. }
  13824. + if (new->secondary && new->secondary->thread) {
  13825. + struct task_struct *t = new->secondary->thread;
  13826. +
  13827. + new->secondary->thread = NULL;
  13828. + kthread_stop(t);
  13829. + put_task_struct(t);
  13830. + }
  13831. out_mput:
  13832. module_put(desc->owner);
  13833. return ret;
  13834. @@ -1397,9 +1558,14 @@
  13835. if (action->thread) {
  13836. kthread_stop(action->thread);
  13837. put_task_struct(action->thread);
  13838. + if (action->secondary && action->secondary->thread) {
  13839. + kthread_stop(action->secondary->thread);
  13840. + put_task_struct(action->secondary->thread);
  13841. + }
  13842. }
  13843. module_put(desc->owner);
  13844. + kfree(action->secondary);
  13845. return action;
  13846. }
  13847. @@ -1543,8 +1709,10 @@
  13848. retval = __setup_irq(irq, desc, action);
  13849. chip_bus_sync_unlock(desc);
  13850. - if (retval)
  13851. + if (retval) {
  13852. + kfree(action->secondary);
  13853. kfree(action);
  13854. + }
  13855. #ifdef CONFIG_DEBUG_SHIRQ_FIXME
  13856. if (!retval && (irqflags & IRQF_SHARED)) {
  13857. diff -Nur linux-4.1.13.orig/kernel/irq/settings.h linux-4.1.13/kernel/irq/settings.h
  13858. --- linux-4.1.13.orig/kernel/irq/settings.h 2015-11-09 23:34:10.000000000 +0100
  13859. +++ linux-4.1.13/kernel/irq/settings.h 2015-11-29 09:23:09.637612322 +0100
  13860. @@ -15,6 +15,7 @@
  13861. _IRQ_NESTED_THREAD = IRQ_NESTED_THREAD,
  13862. _IRQ_PER_CPU_DEVID = IRQ_PER_CPU_DEVID,
  13863. _IRQ_IS_POLLED = IRQ_IS_POLLED,
  13864. + _IRQ_NO_SOFTIRQ_CALL = IRQ_NO_SOFTIRQ_CALL,
  13865. _IRQF_MODIFY_MASK = IRQF_MODIFY_MASK,
  13866. };
  13867. @@ -28,6 +29,7 @@
  13868. #define IRQ_NESTED_THREAD GOT_YOU_MORON
  13869. #define IRQ_PER_CPU_DEVID GOT_YOU_MORON
  13870. #define IRQ_IS_POLLED GOT_YOU_MORON
  13871. +#define IRQ_NO_SOFTIRQ_CALL GOT_YOU_MORON
  13872. #undef IRQF_MODIFY_MASK
  13873. #define IRQF_MODIFY_MASK GOT_YOU_MORON
  13874. @@ -38,6 +40,16 @@
  13875. desc->status_use_accessors |= (set & _IRQF_MODIFY_MASK);
  13876. }
  13877. +static inline bool irq_settings_no_softirq_call(struct irq_desc *desc)
  13878. +{
  13879. + return desc->status_use_accessors & _IRQ_NO_SOFTIRQ_CALL;
  13880. +}
  13881. +
  13882. +static inline void irq_settings_set_no_softirq_call(struct irq_desc *desc)
  13883. +{
  13884. + desc->status_use_accessors |= _IRQ_NO_SOFTIRQ_CALL;
  13885. +}
  13886. +
  13887. static inline bool irq_settings_is_per_cpu(struct irq_desc *desc)
  13888. {
  13889. return desc->status_use_accessors & _IRQ_PER_CPU;
  13890. diff -Nur linux-4.1.13.orig/kernel/irq/spurious.c linux-4.1.13/kernel/irq/spurious.c
  13891. --- linux-4.1.13.orig/kernel/irq/spurious.c 2015-11-09 23:34:10.000000000 +0100
  13892. +++ linux-4.1.13/kernel/irq/spurious.c 2015-11-29 09:23:09.637612322 +0100
  13893. @@ -444,6 +444,10 @@
  13894. static int __init irqfixup_setup(char *str)
  13895. {
  13896. +#ifdef CONFIG_PREEMPT_RT_BASE
  13897. + pr_warn("irqfixup boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  13898. + return 1;
  13899. +#endif
  13900. irqfixup = 1;
  13901. printk(KERN_WARNING "Misrouted IRQ fixup support enabled.\n");
  13902. printk(KERN_WARNING "This may impact system performance.\n");
  13903. @@ -456,6 +460,10 @@
  13904. static int __init irqpoll_setup(char *str)
  13905. {
  13906. +#ifdef CONFIG_PREEMPT_RT_BASE
  13907. + pr_warn("irqpoll boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  13908. + return 1;
  13909. +#endif
  13910. irqfixup = 2;
  13911. printk(KERN_WARNING "Misrouted IRQ fixup and polling support "
  13912. "enabled\n");
  13913. diff -Nur linux-4.1.13.orig/kernel/irq_work.c linux-4.1.13/kernel/irq_work.c
  13914. --- linux-4.1.13.orig/kernel/irq_work.c 2015-11-09 23:34:10.000000000 +0100
  13915. +++ linux-4.1.13/kernel/irq_work.c 2015-11-29 09:23:09.637612322 +0100
  13916. @@ -17,6 +17,7 @@
  13917. #include <linux/cpu.h>
  13918. #include <linux/notifier.h>
  13919. #include <linux/smp.h>
  13920. +#include <linux/interrupt.h>
  13921. #include <asm/processor.h>
  13922. @@ -65,6 +66,8 @@
  13923. */
  13924. bool irq_work_queue_on(struct irq_work *work, int cpu)
  13925. {
  13926. + struct llist_head *list;
  13927. +
  13928. /* All work should have been flushed before going offline */
  13929. WARN_ON_ONCE(cpu_is_offline(cpu));
  13930. @@ -75,7 +78,12 @@
  13931. if (!irq_work_claim(work))
  13932. return false;
  13933. - if (llist_add(&work->llnode, &per_cpu(raised_list, cpu)))
  13934. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL) && !(work->flags & IRQ_WORK_HARD_IRQ))
  13935. + list = &per_cpu(lazy_list, cpu);
  13936. + else
  13937. + list = &per_cpu(raised_list, cpu);
  13938. +
  13939. + if (llist_add(&work->llnode, list))
  13940. arch_send_call_function_single_ipi(cpu);
  13941. return true;
  13942. @@ -86,6 +94,9 @@
  13943. /* Enqueue the irq work @work on the current CPU */
  13944. bool irq_work_queue(struct irq_work *work)
  13945. {
  13946. + struct llist_head *list;
  13947. + bool lazy_work, realtime = IS_ENABLED(CONFIG_PREEMPT_RT_FULL);
  13948. +
  13949. /* Only queue if not already pending */
  13950. if (!irq_work_claim(work))
  13951. return false;
  13952. @@ -93,13 +104,15 @@
  13953. /* Queue the entry and raise the IPI if needed. */
  13954. preempt_disable();
  13955. - /* If the work is "lazy", handle it from next tick if any */
  13956. - if (work->flags & IRQ_WORK_LAZY) {
  13957. - if (llist_add(&work->llnode, this_cpu_ptr(&lazy_list)) &&
  13958. - tick_nohz_tick_stopped())
  13959. - arch_irq_work_raise();
  13960. - } else {
  13961. - if (llist_add(&work->llnode, this_cpu_ptr(&raised_list)))
  13962. + lazy_work = work->flags & IRQ_WORK_LAZY;
  13963. +
  13964. + if (lazy_work || (realtime && !(work->flags & IRQ_WORK_HARD_IRQ)))
  13965. + list = this_cpu_ptr(&lazy_list);
  13966. + else
  13967. + list = this_cpu_ptr(&raised_list);
  13968. +
  13969. + if (llist_add(&work->llnode, list)) {
  13970. + if (!lazy_work || tick_nohz_tick_stopped())
  13971. arch_irq_work_raise();
  13972. }
  13973. @@ -116,9 +129,8 @@
  13974. raised = this_cpu_ptr(&raised_list);
  13975. lazy = this_cpu_ptr(&lazy_list);
  13976. - if (llist_empty(raised) || arch_irq_work_has_interrupt())
  13977. - if (llist_empty(lazy))
  13978. - return false;
  13979. + if (llist_empty(raised) && llist_empty(lazy))
  13980. + return false;
  13981. /* All work should have been flushed before going offline */
  13982. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  13983. @@ -132,7 +144,7 @@
  13984. struct irq_work *work;
  13985. struct llist_node *llnode;
  13986. - BUG_ON(!irqs_disabled());
  13987. + BUG_ON_NONRT(!irqs_disabled());
  13988. if (llist_empty(list))
  13989. return;
  13990. @@ -169,7 +181,16 @@
  13991. void irq_work_run(void)
  13992. {
  13993. irq_work_run_list(this_cpu_ptr(&raised_list));
  13994. - irq_work_run_list(this_cpu_ptr(&lazy_list));
  13995. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL)) {
  13996. + /*
  13997. + * NOTE: we raise softirq via IPI for safety,
  13998. + * and execute in irq_work_tick() to move the
  13999. + * overhead from hard to soft irq context.
  14000. + */
  14001. + if (!llist_empty(this_cpu_ptr(&lazy_list)))
  14002. + raise_softirq(TIMER_SOFTIRQ);
  14003. + } else
  14004. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  14005. }
  14006. EXPORT_SYMBOL_GPL(irq_work_run);
  14007. @@ -179,8 +200,17 @@
  14008. if (!llist_empty(raised) && !arch_irq_work_has_interrupt())
  14009. irq_work_run_list(raised);
  14010. +
  14011. + if (!IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  14012. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  14013. +}
  14014. +
  14015. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  14016. +void irq_work_tick_soft(void)
  14017. +{
  14018. irq_work_run_list(this_cpu_ptr(&lazy_list));
  14019. }
  14020. +#endif
  14021. /*
  14022. * Synchronize against the irq_work @entry, ensures the entry is not
  14023. diff -Nur linux-4.1.13.orig/kernel/Kconfig.locks linux-4.1.13/kernel/Kconfig.locks
  14024. --- linux-4.1.13.orig/kernel/Kconfig.locks 2015-11-09 23:34:10.000000000 +0100
  14025. +++ linux-4.1.13/kernel/Kconfig.locks 2015-11-29 09:23:09.629612854 +0100
  14026. @@ -225,11 +225,11 @@
  14027. config MUTEX_SPIN_ON_OWNER
  14028. def_bool y
  14029. - depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW
  14030. + depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  14031. config RWSEM_SPIN_ON_OWNER
  14032. def_bool y
  14033. - depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW
  14034. + depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  14035. config LOCK_SPIN_ON_OWNER
  14036. def_bool y
  14037. diff -Nur linux-4.1.13.orig/kernel/Kconfig.preempt linux-4.1.13/kernel/Kconfig.preempt
  14038. --- linux-4.1.13.orig/kernel/Kconfig.preempt 2015-11-09 23:34:10.000000000 +0100
  14039. +++ linux-4.1.13/kernel/Kconfig.preempt 2015-11-29 09:23:09.629612854 +0100
  14040. @@ -1,3 +1,16 @@
  14041. +config PREEMPT
  14042. + bool
  14043. + select PREEMPT_COUNT
  14044. +
  14045. +config PREEMPT_RT_BASE
  14046. + bool
  14047. + select PREEMPT
  14048. +
  14049. +config HAVE_PREEMPT_LAZY
  14050. + bool
  14051. +
  14052. +config PREEMPT_LAZY
  14053. + def_bool y if HAVE_PREEMPT_LAZY && PREEMPT_RT_FULL
  14054. choice
  14055. prompt "Preemption Model"
  14056. @@ -33,9 +46,9 @@
  14057. Select this if you are building a kernel for a desktop system.
  14058. -config PREEMPT
  14059. +config PREEMPT__LL
  14060. bool "Preemptible Kernel (Low-Latency Desktop)"
  14061. - select PREEMPT_COUNT
  14062. + select PREEMPT
  14063. select UNINLINE_SPIN_UNLOCK if !ARCH_INLINE_SPIN_UNLOCK
  14064. help
  14065. This option reduces the latency of the kernel by making
  14066. @@ -52,6 +65,22 @@
  14067. embedded system with latency requirements in the milliseconds
  14068. range.
  14069. +config PREEMPT_RTB
  14070. + bool "Preemptible Kernel (Basic RT)"
  14071. + select PREEMPT_RT_BASE
  14072. + help
  14073. + This option is basically the same as (Low-Latency Desktop) but
  14074. + enables changes which are preliminary for the full preemptible
  14075. + RT kernel.
  14076. +
  14077. +config PREEMPT_RT_FULL
  14078. + bool "Fully Preemptible Kernel (RT)"
  14079. + depends on IRQ_FORCED_THREADING
  14080. + select PREEMPT_RT_BASE
  14081. + select PREEMPT_RCU
  14082. + help
  14083. + All and everything
  14084. +
  14085. endchoice
  14086. config PREEMPT_COUNT
  14087. diff -Nur linux-4.1.13.orig/kernel/ksysfs.c linux-4.1.13/kernel/ksysfs.c
  14088. --- linux-4.1.13.orig/kernel/ksysfs.c 2015-11-09 23:34:10.000000000 +0100
  14089. +++ linux-4.1.13/kernel/ksysfs.c 2015-11-29 09:23:09.637612322 +0100
  14090. @@ -136,6 +136,15 @@
  14091. #endif /* CONFIG_KEXEC */
  14092. +#if defined(CONFIG_PREEMPT_RT_FULL)
  14093. +static ssize_t realtime_show(struct kobject *kobj,
  14094. + struct kobj_attribute *attr, char *buf)
  14095. +{
  14096. + return sprintf(buf, "%d\n", 1);
  14097. +}
  14098. +KERNEL_ATTR_RO(realtime);
  14099. +#endif
  14100. +
  14101. /* whether file capabilities are enabled */
  14102. static ssize_t fscaps_show(struct kobject *kobj,
  14103. struct kobj_attribute *attr, char *buf)
  14104. @@ -203,6 +212,9 @@
  14105. &vmcoreinfo_attr.attr,
  14106. #endif
  14107. &rcu_expedited_attr.attr,
  14108. +#ifdef CONFIG_PREEMPT_RT_FULL
  14109. + &realtime_attr.attr,
  14110. +#endif
  14111. NULL
  14112. };
  14113. diff -Nur linux-4.1.13.orig/kernel/locking/lglock.c linux-4.1.13/kernel/locking/lglock.c
  14114. --- linux-4.1.13.orig/kernel/locking/lglock.c 2015-11-09 23:34:10.000000000 +0100
  14115. +++ linux-4.1.13/kernel/locking/lglock.c 2015-11-29 09:23:09.637612322 +0100
  14116. @@ -4,6 +4,15 @@
  14117. #include <linux/cpu.h>
  14118. #include <linux/string.h>
  14119. +#ifndef CONFIG_PREEMPT_RT_FULL
  14120. +# define lg_lock_ptr arch_spinlock_t
  14121. +# define lg_do_lock(l) arch_spin_lock(l)
  14122. +# define lg_do_unlock(l) arch_spin_unlock(l)
  14123. +#else
  14124. +# define lg_lock_ptr struct rt_mutex
  14125. +# define lg_do_lock(l) __rt_spin_lock(l)
  14126. +# define lg_do_unlock(l) __rt_spin_unlock(l)
  14127. +#endif
  14128. /*
  14129. * Note there is no uninit, so lglocks cannot be defined in
  14130. * modules (but it's fine to use them from there)
  14131. @@ -12,51 +21,60 @@
  14132. void lg_lock_init(struct lglock *lg, char *name)
  14133. {
  14134. +#ifdef CONFIG_PREEMPT_RT_FULL
  14135. + int i;
  14136. +
  14137. + for_each_possible_cpu(i) {
  14138. + struct rt_mutex *lock = per_cpu_ptr(lg->lock, i);
  14139. +
  14140. + rt_mutex_init(lock);
  14141. + }
  14142. +#endif
  14143. LOCKDEP_INIT_MAP(&lg->lock_dep_map, name, &lg->lock_key, 0);
  14144. }
  14145. EXPORT_SYMBOL(lg_lock_init);
  14146. void lg_local_lock(struct lglock *lg)
  14147. {
  14148. - arch_spinlock_t *lock;
  14149. + lg_lock_ptr *lock;
  14150. - preempt_disable();
  14151. + migrate_disable();
  14152. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  14153. lock = this_cpu_ptr(lg->lock);
  14154. - arch_spin_lock(lock);
  14155. + lg_do_lock(lock);
  14156. }
  14157. EXPORT_SYMBOL(lg_local_lock);
  14158. void lg_local_unlock(struct lglock *lg)
  14159. {
  14160. - arch_spinlock_t *lock;
  14161. + lg_lock_ptr *lock;
  14162. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  14163. lock = this_cpu_ptr(lg->lock);
  14164. - arch_spin_unlock(lock);
  14165. - preempt_enable();
  14166. + lg_do_unlock(lock);
  14167. + migrate_enable();
  14168. }
  14169. EXPORT_SYMBOL(lg_local_unlock);
  14170. void lg_local_lock_cpu(struct lglock *lg, int cpu)
  14171. {
  14172. - arch_spinlock_t *lock;
  14173. + lg_lock_ptr *lock;
  14174. - preempt_disable();
  14175. + preempt_disable_nort();
  14176. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  14177. lock = per_cpu_ptr(lg->lock, cpu);
  14178. - arch_spin_lock(lock);
  14179. + lg_do_lock(lock);
  14180. }
  14181. EXPORT_SYMBOL(lg_local_lock_cpu);
  14182. void lg_local_unlock_cpu(struct lglock *lg, int cpu)
  14183. {
  14184. - arch_spinlock_t *lock;
  14185. + lg_lock_ptr *lock;
  14186. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  14187. lock = per_cpu_ptr(lg->lock, cpu);
  14188. - arch_spin_unlock(lock);
  14189. - preempt_enable();
  14190. + lg_do_unlock(lock);
  14191. + preempt_enable_nort();
  14192. }
  14193. EXPORT_SYMBOL(lg_local_unlock_cpu);
  14194. @@ -64,12 +82,12 @@
  14195. {
  14196. int i;
  14197. - preempt_disable();
  14198. + preempt_disable_nort();
  14199. lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  14200. for_each_possible_cpu(i) {
  14201. - arch_spinlock_t *lock;
  14202. + lg_lock_ptr *lock;
  14203. lock = per_cpu_ptr(lg->lock, i);
  14204. - arch_spin_lock(lock);
  14205. + lg_do_lock(lock);
  14206. }
  14207. }
  14208. EXPORT_SYMBOL(lg_global_lock);
  14209. @@ -80,10 +98,35 @@
  14210. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  14211. for_each_possible_cpu(i) {
  14212. - arch_spinlock_t *lock;
  14213. + lg_lock_ptr *lock;
  14214. lock = per_cpu_ptr(lg->lock, i);
  14215. - arch_spin_unlock(lock);
  14216. + lg_do_unlock(lock);
  14217. }
  14218. - preempt_enable();
  14219. + preempt_enable_nort();
  14220. }
  14221. EXPORT_SYMBOL(lg_global_unlock);
  14222. +
  14223. +#ifdef CONFIG_PREEMPT_RT_FULL
  14224. +/*
  14225. + * HACK: If you use this, you get to keep the pieces.
  14226. + * Used in queue_stop_cpus_work() when stop machinery
  14227. + * is called from inactive CPU, so we can't schedule.
  14228. + */
  14229. +# define lg_do_trylock_relax(l) \
  14230. + do { \
  14231. + while (!__rt_spin_trylock(l)) \
  14232. + cpu_relax(); \
  14233. + } while (0)
  14234. +
  14235. +void lg_global_trylock_relax(struct lglock *lg)
  14236. +{
  14237. + int i;
  14238. +
  14239. + lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  14240. + for_each_possible_cpu(i) {
  14241. + lg_lock_ptr *lock;
  14242. + lock = per_cpu_ptr(lg->lock, i);
  14243. + lg_do_trylock_relax(lock);
  14244. + }
  14245. +}
  14246. +#endif
  14247. diff -Nur linux-4.1.13.orig/kernel/locking/lockdep.c linux-4.1.13/kernel/locking/lockdep.c
  14248. --- linux-4.1.13.orig/kernel/locking/lockdep.c 2015-11-09 23:34:10.000000000 +0100
  14249. +++ linux-4.1.13/kernel/locking/lockdep.c 2015-11-29 09:23:09.637612322 +0100
  14250. @@ -3563,6 +3563,7 @@
  14251. }
  14252. }
  14253. +#ifndef CONFIG_PREEMPT_RT_FULL
  14254. /*
  14255. * We dont accurately track softirq state in e.g.
  14256. * hardirq contexts (such as on 4KSTACKS), so only
  14257. @@ -3577,6 +3578,7 @@
  14258. DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
  14259. }
  14260. }
  14261. +#endif
  14262. if (!debug_locks)
  14263. print_irqtrace_events(current);
  14264. diff -Nur linux-4.1.13.orig/kernel/locking/locktorture.c linux-4.1.13/kernel/locking/locktorture.c
  14265. --- linux-4.1.13.orig/kernel/locking/locktorture.c 2015-11-09 23:34:10.000000000 +0100
  14266. +++ linux-4.1.13/kernel/locking/locktorture.c 2015-11-29 09:23:09.637612322 +0100
  14267. @@ -24,7 +24,6 @@
  14268. #include <linux/module.h>
  14269. #include <linux/kthread.h>
  14270. #include <linux/spinlock.h>
  14271. -#include <linux/rwlock.h>
  14272. #include <linux/mutex.h>
  14273. #include <linux/rwsem.h>
  14274. #include <linux/smp.h>
  14275. diff -Nur linux-4.1.13.orig/kernel/locking/Makefile linux-4.1.13/kernel/locking/Makefile
  14276. --- linux-4.1.13.orig/kernel/locking/Makefile 2015-11-09 23:34:10.000000000 +0100
  14277. +++ linux-4.1.13/kernel/locking/Makefile 2015-11-29 09:23:09.637612322 +0100
  14278. @@ -1,5 +1,5 @@
  14279. -obj-y += mutex.o semaphore.o rwsem.o
  14280. +obj-y += semaphore.o
  14281. ifdef CONFIG_FUNCTION_TRACER
  14282. CFLAGS_REMOVE_lockdep.o = $(CC_FLAGS_FTRACE)
  14283. @@ -8,7 +8,11 @@
  14284. CFLAGS_REMOVE_rtmutex-debug.o = $(CC_FLAGS_FTRACE)
  14285. endif
  14286. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  14287. +obj-y += mutex.o
  14288. obj-$(CONFIG_DEBUG_MUTEXES) += mutex-debug.o
  14289. +obj-y += rwsem.o
  14290. +endif
  14291. obj-$(CONFIG_LOCKDEP) += lockdep.o
  14292. ifeq ($(CONFIG_PROC_FS),y)
  14293. obj-$(CONFIG_LOCKDEP) += lockdep_proc.o
  14294. @@ -22,8 +26,11 @@
  14295. obj-$(CONFIG_RT_MUTEX_TESTER) += rtmutex-tester.o
  14296. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o
  14297. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock_debug.o
  14298. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  14299. obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
  14300. obj-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem-xadd.o
  14301. +endif
  14302. obj-$(CONFIG_PERCPU_RWSEM) += percpu-rwsem.o
  14303. +obj-$(CONFIG_PREEMPT_RT_FULL) += rt.o
  14304. obj-$(CONFIG_QUEUE_RWLOCK) += qrwlock.o
  14305. obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o
  14306. diff -Nur linux-4.1.13.orig/kernel/locking/rt.c linux-4.1.13/kernel/locking/rt.c
  14307. --- linux-4.1.13.orig/kernel/locking/rt.c 1970-01-01 01:00:00.000000000 +0100
  14308. +++ linux-4.1.13/kernel/locking/rt.c 2015-11-29 09:23:09.637612322 +0100
  14309. @@ -0,0 +1,461 @@
  14310. +/*
  14311. + * kernel/rt.c
  14312. + *
  14313. + * Real-Time Preemption Support
  14314. + *
  14315. + * started by Ingo Molnar:
  14316. + *
  14317. + * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  14318. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  14319. + *
  14320. + * historic credit for proving that Linux spinlocks can be implemented via
  14321. + * RT-aware mutexes goes to many people: The Pmutex project (Dirk Grambow
  14322. + * and others) who prototyped it on 2.4 and did lots of comparative
  14323. + * research and analysis; TimeSys, for proving that you can implement a
  14324. + * fully preemptible kernel via the use of IRQ threading and mutexes;
  14325. + * Bill Huey for persuasively arguing on lkml that the mutex model is the
  14326. + * right one; and to MontaVista, who ported pmutexes to 2.6.
  14327. + *
  14328. + * This code is a from-scratch implementation and is not based on pmutexes,
  14329. + * but the idea of converting spinlocks to mutexes is used here too.
  14330. + *
  14331. + * lock debugging, locking tree, deadlock detection:
  14332. + *
  14333. + * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey
  14334. + * Released under the General Public License (GPL).
  14335. + *
  14336. + * Includes portions of the generic R/W semaphore implementation from:
  14337. + *
  14338. + * Copyright (c) 2001 David Howells (dhowells@redhat.com).
  14339. + * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
  14340. + * - Derived also from comments by Linus
  14341. + *
  14342. + * Pending ownership of locks and ownership stealing:
  14343. + *
  14344. + * Copyright (C) 2005, Kihon Technologies Inc., Steven Rostedt
  14345. + *
  14346. + * (also by Steven Rostedt)
  14347. + * - Converted single pi_lock to individual task locks.
  14348. + *
  14349. + * By Esben Nielsen:
  14350. + * Doing priority inheritance with help of the scheduler.
  14351. + *
  14352. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  14353. + * - major rework based on Esben Nielsens initial patch
  14354. + * - replaced thread_info references by task_struct refs
  14355. + * - removed task->pending_owner dependency
  14356. + * - BKL drop/reacquire for semaphore style locks to avoid deadlocks
  14357. + * in the scheduler return path as discussed with Steven Rostedt
  14358. + *
  14359. + * Copyright (C) 2006, Kihon Technologies Inc.
  14360. + * Steven Rostedt <rostedt@goodmis.org>
  14361. + * - debugged and patched Thomas Gleixner's rework.
  14362. + * - added back the cmpxchg to the rework.
  14363. + * - turned atomic require back on for SMP.
  14364. + */
  14365. +
  14366. +#include <linux/spinlock.h>
  14367. +#include <linux/rtmutex.h>
  14368. +#include <linux/sched.h>
  14369. +#include <linux/delay.h>
  14370. +#include <linux/module.h>
  14371. +#include <linux/kallsyms.h>
  14372. +#include <linux/syscalls.h>
  14373. +#include <linux/interrupt.h>
  14374. +#include <linux/plist.h>
  14375. +#include <linux/fs.h>
  14376. +#include <linux/futex.h>
  14377. +#include <linux/hrtimer.h>
  14378. +
  14379. +#include "rtmutex_common.h"
  14380. +
  14381. +/*
  14382. + * struct mutex functions
  14383. + */
  14384. +void __mutex_do_init(struct mutex *mutex, const char *name,
  14385. + struct lock_class_key *key)
  14386. +{
  14387. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14388. + /*
  14389. + * Make sure we are not reinitializing a held lock:
  14390. + */
  14391. + debug_check_no_locks_freed((void *)mutex, sizeof(*mutex));
  14392. + lockdep_init_map(&mutex->dep_map, name, key, 0);
  14393. +#endif
  14394. + mutex->lock.save_state = 0;
  14395. +}
  14396. +EXPORT_SYMBOL(__mutex_do_init);
  14397. +
  14398. +void __lockfunc _mutex_lock(struct mutex *lock)
  14399. +{
  14400. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  14401. + rt_mutex_lock(&lock->lock);
  14402. +}
  14403. +EXPORT_SYMBOL(_mutex_lock);
  14404. +
  14405. +int __lockfunc _mutex_lock_interruptible(struct mutex *lock)
  14406. +{
  14407. + int ret;
  14408. +
  14409. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  14410. + ret = rt_mutex_lock_interruptible(&lock->lock);
  14411. + if (ret)
  14412. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  14413. + return ret;
  14414. +}
  14415. +EXPORT_SYMBOL(_mutex_lock_interruptible);
  14416. +
  14417. +int __lockfunc _mutex_lock_killable(struct mutex *lock)
  14418. +{
  14419. + int ret;
  14420. +
  14421. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  14422. + ret = rt_mutex_lock_killable(&lock->lock);
  14423. + if (ret)
  14424. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  14425. + return ret;
  14426. +}
  14427. +EXPORT_SYMBOL(_mutex_lock_killable);
  14428. +
  14429. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14430. +void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass)
  14431. +{
  14432. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  14433. + rt_mutex_lock(&lock->lock);
  14434. +}
  14435. +EXPORT_SYMBOL(_mutex_lock_nested);
  14436. +
  14437. +void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
  14438. +{
  14439. + mutex_acquire_nest(&lock->dep_map, 0, 0, nest, _RET_IP_);
  14440. + rt_mutex_lock(&lock->lock);
  14441. +}
  14442. +EXPORT_SYMBOL(_mutex_lock_nest_lock);
  14443. +
  14444. +int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass)
  14445. +{
  14446. + int ret;
  14447. +
  14448. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  14449. + ret = rt_mutex_lock_interruptible(&lock->lock);
  14450. + if (ret)
  14451. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  14452. + return ret;
  14453. +}
  14454. +EXPORT_SYMBOL(_mutex_lock_interruptible_nested);
  14455. +
  14456. +int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass)
  14457. +{
  14458. + int ret;
  14459. +
  14460. + mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  14461. + ret = rt_mutex_lock_killable(&lock->lock);
  14462. + if (ret)
  14463. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  14464. + return ret;
  14465. +}
  14466. +EXPORT_SYMBOL(_mutex_lock_killable_nested);
  14467. +#endif
  14468. +
  14469. +int __lockfunc _mutex_trylock(struct mutex *lock)
  14470. +{
  14471. + int ret = rt_mutex_trylock(&lock->lock);
  14472. +
  14473. + if (ret)
  14474. + mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  14475. +
  14476. + return ret;
  14477. +}
  14478. +EXPORT_SYMBOL(_mutex_trylock);
  14479. +
  14480. +void __lockfunc _mutex_unlock(struct mutex *lock)
  14481. +{
  14482. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  14483. + rt_mutex_unlock(&lock->lock);
  14484. +}
  14485. +EXPORT_SYMBOL(_mutex_unlock);
  14486. +
  14487. +/*
  14488. + * rwlock_t functions
  14489. + */
  14490. +int __lockfunc rt_write_trylock(rwlock_t *rwlock)
  14491. +{
  14492. + int ret;
  14493. +
  14494. + migrate_disable();
  14495. + ret = rt_mutex_trylock(&rwlock->lock);
  14496. + if (ret)
  14497. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  14498. + else
  14499. + migrate_enable();
  14500. +
  14501. + return ret;
  14502. +}
  14503. +EXPORT_SYMBOL(rt_write_trylock);
  14504. +
  14505. +int __lockfunc rt_write_trylock_irqsave(rwlock_t *rwlock, unsigned long *flags)
  14506. +{
  14507. + int ret;
  14508. +
  14509. + *flags = 0;
  14510. + ret = rt_write_trylock(rwlock);
  14511. + return ret;
  14512. +}
  14513. +EXPORT_SYMBOL(rt_write_trylock_irqsave);
  14514. +
  14515. +int __lockfunc rt_read_trylock(rwlock_t *rwlock)
  14516. +{
  14517. + struct rt_mutex *lock = &rwlock->lock;
  14518. + int ret = 1;
  14519. +
  14520. + /*
  14521. + * recursive read locks succeed when current owns the lock,
  14522. + * but not when read_depth == 0 which means that the lock is
  14523. + * write locked.
  14524. + */
  14525. + if (rt_mutex_owner(lock) != current) {
  14526. + migrate_disable();
  14527. + ret = rt_mutex_trylock(lock);
  14528. + if (ret)
  14529. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  14530. + else
  14531. + migrate_enable();
  14532. +
  14533. + } else if (!rwlock->read_depth) {
  14534. + ret = 0;
  14535. + }
  14536. +
  14537. + if (ret)
  14538. + rwlock->read_depth++;
  14539. +
  14540. + return ret;
  14541. +}
  14542. +EXPORT_SYMBOL(rt_read_trylock);
  14543. +
  14544. +void __lockfunc rt_write_lock(rwlock_t *rwlock)
  14545. +{
  14546. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  14547. + migrate_disable();
  14548. + __rt_spin_lock(&rwlock->lock);
  14549. +}
  14550. +EXPORT_SYMBOL(rt_write_lock);
  14551. +
  14552. +void __lockfunc rt_read_lock(rwlock_t *rwlock)
  14553. +{
  14554. + struct rt_mutex *lock = &rwlock->lock;
  14555. +
  14556. +
  14557. + /*
  14558. + * recursive read locks succeed when current owns the lock
  14559. + */
  14560. + if (rt_mutex_owner(lock) != current) {
  14561. + migrate_disable();
  14562. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  14563. + __rt_spin_lock(lock);
  14564. + }
  14565. + rwlock->read_depth++;
  14566. +}
  14567. +
  14568. +EXPORT_SYMBOL(rt_read_lock);
  14569. +
  14570. +void __lockfunc rt_write_unlock(rwlock_t *rwlock)
  14571. +{
  14572. + /* NOTE: we always pass in '1' for nested, for simplicity */
  14573. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  14574. + __rt_spin_unlock(&rwlock->lock);
  14575. + migrate_enable();
  14576. +}
  14577. +EXPORT_SYMBOL(rt_write_unlock);
  14578. +
  14579. +void __lockfunc rt_read_unlock(rwlock_t *rwlock)
  14580. +{
  14581. + /* Release the lock only when read_depth is down to 0 */
  14582. + if (--rwlock->read_depth == 0) {
  14583. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  14584. + __rt_spin_unlock(&rwlock->lock);
  14585. + migrate_enable();
  14586. + }
  14587. +}
  14588. +EXPORT_SYMBOL(rt_read_unlock);
  14589. +
  14590. +unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock)
  14591. +{
  14592. + rt_write_lock(rwlock);
  14593. +
  14594. + return 0;
  14595. +}
  14596. +EXPORT_SYMBOL(rt_write_lock_irqsave);
  14597. +
  14598. +unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock)
  14599. +{
  14600. + rt_read_lock(rwlock);
  14601. +
  14602. + return 0;
  14603. +}
  14604. +EXPORT_SYMBOL(rt_read_lock_irqsave);
  14605. +
  14606. +void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key)
  14607. +{
  14608. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14609. + /*
  14610. + * Make sure we are not reinitializing a held lock:
  14611. + */
  14612. + debug_check_no_locks_freed((void *)rwlock, sizeof(*rwlock));
  14613. + lockdep_init_map(&rwlock->dep_map, name, key, 0);
  14614. +#endif
  14615. + rwlock->lock.save_state = 1;
  14616. + rwlock->read_depth = 0;
  14617. +}
  14618. +EXPORT_SYMBOL(__rt_rwlock_init);
  14619. +
  14620. +/*
  14621. + * rw_semaphores
  14622. + */
  14623. +
  14624. +void rt_up_write(struct rw_semaphore *rwsem)
  14625. +{
  14626. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  14627. + rt_mutex_unlock(&rwsem->lock);
  14628. +}
  14629. +EXPORT_SYMBOL(rt_up_write);
  14630. +
  14631. +void __rt_up_read(struct rw_semaphore *rwsem)
  14632. +{
  14633. + if (--rwsem->read_depth == 0)
  14634. + rt_mutex_unlock(&rwsem->lock);
  14635. +}
  14636. +
  14637. +void rt_up_read(struct rw_semaphore *rwsem)
  14638. +{
  14639. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  14640. + __rt_up_read(rwsem);
  14641. +}
  14642. +EXPORT_SYMBOL(rt_up_read);
  14643. +
  14644. +/*
  14645. + * downgrade a write lock into a read lock
  14646. + * - just wake up any readers at the front of the queue
  14647. + */
  14648. +void rt_downgrade_write(struct rw_semaphore *rwsem)
  14649. +{
  14650. + BUG_ON(rt_mutex_owner(&rwsem->lock) != current);
  14651. + rwsem->read_depth = 1;
  14652. +}
  14653. +EXPORT_SYMBOL(rt_downgrade_write);
  14654. +
  14655. +int rt_down_write_trylock(struct rw_semaphore *rwsem)
  14656. +{
  14657. + int ret = rt_mutex_trylock(&rwsem->lock);
  14658. +
  14659. + if (ret)
  14660. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  14661. + return ret;
  14662. +}
  14663. +EXPORT_SYMBOL(rt_down_write_trylock);
  14664. +
  14665. +void rt_down_write(struct rw_semaphore *rwsem)
  14666. +{
  14667. + rwsem_acquire(&rwsem->dep_map, 0, 0, _RET_IP_);
  14668. + rt_mutex_lock(&rwsem->lock);
  14669. +}
  14670. +EXPORT_SYMBOL(rt_down_write);
  14671. +
  14672. +void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass)
  14673. +{
  14674. + rwsem_acquire(&rwsem->dep_map, subclass, 0, _RET_IP_);
  14675. + rt_mutex_lock(&rwsem->lock);
  14676. +}
  14677. +EXPORT_SYMBOL(rt_down_write_nested);
  14678. +
  14679. +void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  14680. + struct lockdep_map *nest)
  14681. +{
  14682. + rwsem_acquire_nest(&rwsem->dep_map, 0, 0, nest, _RET_IP_);
  14683. + rt_mutex_lock(&rwsem->lock);
  14684. +}
  14685. +EXPORT_SYMBOL(rt_down_write_nested_lock);
  14686. +
  14687. +int rt_down_read_trylock(struct rw_semaphore *rwsem)
  14688. +{
  14689. + struct rt_mutex *lock = &rwsem->lock;
  14690. + int ret = 1;
  14691. +
  14692. + /*
  14693. + * recursive read locks succeed when current owns the rwsem,
  14694. + * but not when read_depth == 0 which means that the rwsem is
  14695. + * write locked.
  14696. + */
  14697. + if (rt_mutex_owner(lock) != current)
  14698. + ret = rt_mutex_trylock(&rwsem->lock);
  14699. + else if (!rwsem->read_depth)
  14700. + ret = 0;
  14701. +
  14702. + if (ret) {
  14703. + rwsem->read_depth++;
  14704. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  14705. + }
  14706. + return ret;
  14707. +}
  14708. +EXPORT_SYMBOL(rt_down_read_trylock);
  14709. +
  14710. +static void __rt_down_read(struct rw_semaphore *rwsem, int subclass)
  14711. +{
  14712. + struct rt_mutex *lock = &rwsem->lock;
  14713. +
  14714. + rwsem_acquire_read(&rwsem->dep_map, subclass, 0, _RET_IP_);
  14715. +
  14716. + if (rt_mutex_owner(lock) != current)
  14717. + rt_mutex_lock(&rwsem->lock);
  14718. + rwsem->read_depth++;
  14719. +}
  14720. +
  14721. +void rt_down_read(struct rw_semaphore *rwsem)
  14722. +{
  14723. + __rt_down_read(rwsem, 0);
  14724. +}
  14725. +EXPORT_SYMBOL(rt_down_read);
  14726. +
  14727. +void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass)
  14728. +{
  14729. + __rt_down_read(rwsem, subclass);
  14730. +}
  14731. +EXPORT_SYMBOL(rt_down_read_nested);
  14732. +
  14733. +void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  14734. + struct lock_class_key *key)
  14735. +{
  14736. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14737. + /*
  14738. + * Make sure we are not reinitializing a held lock:
  14739. + */
  14740. + debug_check_no_locks_freed((void *)rwsem, sizeof(*rwsem));
  14741. + lockdep_init_map(&rwsem->dep_map, name, key, 0);
  14742. +#endif
  14743. + rwsem->read_depth = 0;
  14744. + rwsem->lock.save_state = 0;
  14745. +}
  14746. +EXPORT_SYMBOL(__rt_rwsem_init);
  14747. +
  14748. +/**
  14749. + * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
  14750. + * @cnt: the atomic which we are to dec
  14751. + * @lock: the mutex to return holding if we dec to 0
  14752. + *
  14753. + * return true and hold lock if we dec to 0, return false otherwise
  14754. + */
  14755. +int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
  14756. +{
  14757. + /* dec if we can't possibly hit 0 */
  14758. + if (atomic_add_unless(cnt, -1, 1))
  14759. + return 0;
  14760. + /* we might hit 0, so take the lock */
  14761. + mutex_lock(lock);
  14762. + if (!atomic_dec_and_test(cnt)) {
  14763. + /* when we actually did the dec, we didn't hit 0 */
  14764. + mutex_unlock(lock);
  14765. + return 0;
  14766. + }
  14767. + /* we hit 0, and we hold the lock */
  14768. + return 1;
  14769. +}
  14770. +EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
  14771. diff -Nur linux-4.1.13.orig/kernel/locking/rtmutex.c linux-4.1.13/kernel/locking/rtmutex.c
  14772. --- linux-4.1.13.orig/kernel/locking/rtmutex.c 2015-11-09 23:34:10.000000000 +0100
  14773. +++ linux-4.1.13/kernel/locking/rtmutex.c 2015-11-29 09:23:09.637612322 +0100
  14774. @@ -7,6 +7,11 @@
  14775. * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  14776. * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
  14777. * Copyright (C) 2006 Esben Nielsen
  14778. + * Adaptive Spinlocks:
  14779. + * Copyright (C) 2008 Novell, Inc., Gregory Haskins, Sven Dietrich,
  14780. + * and Peter Morreale,
  14781. + * Adaptive Spinlocks simplification:
  14782. + * Copyright (C) 2008 Red Hat, Inc., Steven Rostedt <srostedt@redhat.com>
  14783. *
  14784. * See Documentation/locking/rt-mutex-design.txt for details.
  14785. */
  14786. @@ -16,6 +21,7 @@
  14787. #include <linux/sched/rt.h>
  14788. #include <linux/sched/deadline.h>
  14789. #include <linux/timer.h>
  14790. +#include <linux/ww_mutex.h>
  14791. #include "rtmutex_common.h"
  14792. @@ -69,6 +75,12 @@
  14793. clear_rt_mutex_waiters(lock);
  14794. }
  14795. +static int rt_mutex_real_waiter(struct rt_mutex_waiter *waiter)
  14796. +{
  14797. + return waiter && waiter != PI_WAKEUP_INPROGRESS &&
  14798. + waiter != PI_REQUEUE_INPROGRESS;
  14799. +}
  14800. +
  14801. /*
  14802. * We can speed up the acquire/release, if the architecture
  14803. * supports cmpxchg and if there's no debugging state to be set up
  14804. @@ -300,7 +312,7 @@
  14805. * of task. We do not use the spin_xx_mutex() variants here as we are
  14806. * outside of the debug path.)
  14807. */
  14808. -static void rt_mutex_adjust_prio(struct task_struct *task)
  14809. +void rt_mutex_adjust_prio(struct task_struct *task)
  14810. {
  14811. unsigned long flags;
  14812. @@ -335,6 +347,14 @@
  14813. return debug_rt_mutex_detect_deadlock(waiter, chwalk);
  14814. }
  14815. +static void rt_mutex_wake_waiter(struct rt_mutex_waiter *waiter)
  14816. +{
  14817. + if (waiter->savestate)
  14818. + wake_up_lock_sleeper(waiter->task);
  14819. + else
  14820. + wake_up_process(waiter->task);
  14821. +}
  14822. +
  14823. /*
  14824. * Max number of times we'll walk the boosting chain:
  14825. */
  14826. @@ -342,7 +362,8 @@
  14827. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  14828. {
  14829. - return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  14830. + return rt_mutex_real_waiter(p->pi_blocked_on) ?
  14831. + p->pi_blocked_on->lock : NULL;
  14832. }
  14833. /*
  14834. @@ -479,7 +500,7 @@
  14835. * reached or the state of the chain has changed while we
  14836. * dropped the locks.
  14837. */
  14838. - if (!waiter)
  14839. + if (!rt_mutex_real_waiter(waiter))
  14840. goto out_unlock_pi;
  14841. /*
  14842. @@ -641,13 +662,16 @@
  14843. * follow here. This is the end of the chain we are walking.
  14844. */
  14845. if (!rt_mutex_owner(lock)) {
  14846. + struct rt_mutex_waiter *lock_top_waiter;
  14847. +
  14848. /*
  14849. * If the requeue [7] above changed the top waiter,
  14850. * then we need to wake the new top waiter up to try
  14851. * to get the lock.
  14852. */
  14853. - if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
  14854. - wake_up_process(rt_mutex_top_waiter(lock)->task);
  14855. + lock_top_waiter = rt_mutex_top_waiter(lock);
  14856. + if (prerequeue_top_waiter != lock_top_waiter)
  14857. + rt_mutex_wake_waiter(lock_top_waiter);
  14858. raw_spin_unlock(&lock->wait_lock);
  14859. return 0;
  14860. }
  14861. @@ -740,6 +764,25 @@
  14862. return ret;
  14863. }
  14864. +
  14865. +#define STEAL_NORMAL 0
  14866. +#define STEAL_LATERAL 1
  14867. +
  14868. +/*
  14869. + * Note that RT tasks are excluded from lateral-steals to prevent the
  14870. + * introduction of an unbounded latency
  14871. + */
  14872. +static inline int lock_is_stealable(struct task_struct *task,
  14873. + struct task_struct *pendowner, int mode)
  14874. +{
  14875. + if (mode == STEAL_NORMAL || rt_task(task)) {
  14876. + if (task->prio >= pendowner->prio)
  14877. + return 0;
  14878. + } else if (task->prio > pendowner->prio)
  14879. + return 0;
  14880. + return 1;
  14881. +}
  14882. +
  14883. /*
  14884. * Try to take an rt-mutex
  14885. *
  14886. @@ -750,8 +793,9 @@
  14887. * @waiter: The waiter that is queued to the lock's wait list if the
  14888. * callsite called task_blocked_on_lock(), otherwise NULL
  14889. */
  14890. -static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  14891. - struct rt_mutex_waiter *waiter)
  14892. +static int __try_to_take_rt_mutex(struct rt_mutex *lock,
  14893. + struct task_struct *task,
  14894. + struct rt_mutex_waiter *waiter, int mode)
  14895. {
  14896. unsigned long flags;
  14897. @@ -790,8 +834,10 @@
  14898. * If waiter is not the highest priority waiter of
  14899. * @lock, give up.
  14900. */
  14901. - if (waiter != rt_mutex_top_waiter(lock))
  14902. + if (waiter != rt_mutex_top_waiter(lock)) {
  14903. + /* XXX lock_is_stealable() ? */
  14904. return 0;
  14905. + }
  14906. /*
  14907. * We can acquire the lock. Remove the waiter from the
  14908. @@ -809,14 +855,10 @@
  14909. * not need to be dequeued.
  14910. */
  14911. if (rt_mutex_has_waiters(lock)) {
  14912. - /*
  14913. - * If @task->prio is greater than or equal to
  14914. - * the top waiter priority (kernel view),
  14915. - * @task lost.
  14916. - */
  14917. - if (task->prio >= rt_mutex_top_waiter(lock)->prio)
  14918. - return 0;
  14919. + struct task_struct *pown = rt_mutex_top_waiter(lock)->task;
  14920. + if (task != pown && !lock_is_stealable(task, pown, mode))
  14921. + return 0;
  14922. /*
  14923. * The current top waiter stays enqueued. We
  14924. * don't have to change anything in the lock
  14925. @@ -865,6 +907,347 @@
  14926. return 1;
  14927. }
  14928. +#ifdef CONFIG_PREEMPT_RT_FULL
  14929. +/*
  14930. + * preemptible spin_lock functions:
  14931. + */
  14932. +static inline void rt_spin_lock_fastlock(struct rt_mutex *lock,
  14933. + void (*slowfn)(struct rt_mutex *lock))
  14934. +{
  14935. + might_sleep_no_state_check();
  14936. +
  14937. + if (likely(rt_mutex_cmpxchg(lock, NULL, current)))
  14938. + rt_mutex_deadlock_account_lock(lock, current);
  14939. + else
  14940. + slowfn(lock);
  14941. +}
  14942. +
  14943. +static inline void rt_spin_lock_fastunlock(struct rt_mutex *lock,
  14944. + void (*slowfn)(struct rt_mutex *lock))
  14945. +{
  14946. + if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  14947. + rt_mutex_deadlock_account_unlock(current);
  14948. + else
  14949. + slowfn(lock);
  14950. +}
  14951. +#ifdef CONFIG_SMP
  14952. +/*
  14953. + * Note that owner is a speculative pointer and dereferencing relies
  14954. + * on rcu_read_lock() and the check against the lock owner.
  14955. + */
  14956. +static int adaptive_wait(struct rt_mutex *lock,
  14957. + struct task_struct *owner)
  14958. +{
  14959. + int res = 0;
  14960. +
  14961. + rcu_read_lock();
  14962. + for (;;) {
  14963. + if (owner != rt_mutex_owner(lock))
  14964. + break;
  14965. + /*
  14966. + * Ensure that owner->on_cpu is dereferenced _after_
  14967. + * checking the above to be valid.
  14968. + */
  14969. + barrier();
  14970. + if (!owner->on_cpu) {
  14971. + res = 1;
  14972. + break;
  14973. + }
  14974. + cpu_relax();
  14975. + }
  14976. + rcu_read_unlock();
  14977. + return res;
  14978. +}
  14979. +#else
  14980. +static int adaptive_wait(struct rt_mutex *lock,
  14981. + struct task_struct *orig_owner)
  14982. +{
  14983. + return 1;
  14984. +}
  14985. +#endif
  14986. +
  14987. +# define pi_lock(lock) raw_spin_lock_irq(lock)
  14988. +# define pi_unlock(lock) raw_spin_unlock_irq(lock)
  14989. +
  14990. +static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  14991. + struct rt_mutex_waiter *waiter,
  14992. + struct task_struct *task,
  14993. + enum rtmutex_chainwalk chwalk);
  14994. +/*
  14995. + * Slow path lock function spin_lock style: this variant is very
  14996. + * careful not to miss any non-lock wakeups.
  14997. + *
  14998. + * We store the current state under p->pi_lock in p->saved_state and
  14999. + * the try_to_wake_up() code handles this accordingly.
  15000. + */
  15001. +static void noinline __sched rt_spin_lock_slowlock(struct rt_mutex *lock)
  15002. +{
  15003. + struct task_struct *lock_owner, *self = current;
  15004. + struct rt_mutex_waiter waiter, *top_waiter;
  15005. + int ret;
  15006. +
  15007. + rt_mutex_init_waiter(&waiter, true);
  15008. +
  15009. + raw_spin_lock(&lock->wait_lock);
  15010. +
  15011. + if (__try_to_take_rt_mutex(lock, self, NULL, STEAL_LATERAL)) {
  15012. + raw_spin_unlock(&lock->wait_lock);
  15013. + return;
  15014. + }
  15015. +
  15016. + BUG_ON(rt_mutex_owner(lock) == self);
  15017. +
  15018. + /*
  15019. + * We save whatever state the task is in and we'll restore it
  15020. + * after acquiring the lock taking real wakeups into account
  15021. + * as well. We are serialized via pi_lock against wakeups. See
  15022. + * try_to_wake_up().
  15023. + */
  15024. + pi_lock(&self->pi_lock);
  15025. + self->saved_state = self->state;
  15026. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  15027. + pi_unlock(&self->pi_lock);
  15028. +
  15029. + ret = task_blocks_on_rt_mutex(lock, &waiter, self, 0);
  15030. + BUG_ON(ret);
  15031. +
  15032. + for (;;) {
  15033. + /* Try to acquire the lock again. */
  15034. + if (__try_to_take_rt_mutex(lock, self, &waiter, STEAL_LATERAL))
  15035. + break;
  15036. +
  15037. + top_waiter = rt_mutex_top_waiter(lock);
  15038. + lock_owner = rt_mutex_owner(lock);
  15039. +
  15040. + raw_spin_unlock(&lock->wait_lock);
  15041. +
  15042. + debug_rt_mutex_print_deadlock(&waiter);
  15043. +
  15044. + if (top_waiter != &waiter || adaptive_wait(lock, lock_owner))
  15045. + schedule_rt_mutex(lock);
  15046. +
  15047. + raw_spin_lock(&lock->wait_lock);
  15048. +
  15049. + pi_lock(&self->pi_lock);
  15050. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  15051. + pi_unlock(&self->pi_lock);
  15052. + }
  15053. +
  15054. + /*
  15055. + * Restore the task state to current->saved_state. We set it
  15056. + * to the original state above and the try_to_wake_up() code
  15057. + * has possibly updated it when a real (non-rtmutex) wakeup
  15058. + * happened while we were blocked. Clear saved_state so
  15059. + * try_to_wakeup() does not get confused.
  15060. + */
  15061. + pi_lock(&self->pi_lock);
  15062. + __set_current_state_no_track(self->saved_state);
  15063. + self->saved_state = TASK_RUNNING;
  15064. + pi_unlock(&self->pi_lock);
  15065. +
  15066. + /*
  15067. + * try_to_take_rt_mutex() sets the waiter bit
  15068. + * unconditionally. We might have to fix that up:
  15069. + */
  15070. + fixup_rt_mutex_waiters(lock);
  15071. +
  15072. + BUG_ON(rt_mutex_has_waiters(lock) && &waiter == rt_mutex_top_waiter(lock));
  15073. + BUG_ON(!RB_EMPTY_NODE(&waiter.tree_entry));
  15074. +
  15075. + raw_spin_unlock(&lock->wait_lock);
  15076. +
  15077. + debug_rt_mutex_free_waiter(&waiter);
  15078. +}
  15079. +
  15080. +static void wakeup_next_waiter(struct rt_mutex *lock);
  15081. +/*
  15082. + * Slow path to release a rt_mutex spin_lock style
  15083. + */
  15084. +static void noinline __sched rt_spin_lock_slowunlock(struct rt_mutex *lock)
  15085. +{
  15086. + raw_spin_lock(&lock->wait_lock);
  15087. +
  15088. + debug_rt_mutex_unlock(lock);
  15089. +
  15090. + rt_mutex_deadlock_account_unlock(current);
  15091. +
  15092. + if (!rt_mutex_has_waiters(lock)) {
  15093. + lock->owner = NULL;
  15094. + raw_spin_unlock(&lock->wait_lock);
  15095. + return;
  15096. + }
  15097. +
  15098. + wakeup_next_waiter(lock);
  15099. +
  15100. + raw_spin_unlock(&lock->wait_lock);
  15101. +
  15102. + /* Undo pi boosting.when necessary */
  15103. + rt_mutex_adjust_prio(current);
  15104. +}
  15105. +
  15106. +void __lockfunc rt_spin_lock(spinlock_t *lock)
  15107. +{
  15108. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock);
  15109. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  15110. +}
  15111. +EXPORT_SYMBOL(rt_spin_lock);
  15112. +
  15113. +void __lockfunc __rt_spin_lock(struct rt_mutex *lock)
  15114. +{
  15115. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock);
  15116. +}
  15117. +EXPORT_SYMBOL(__rt_spin_lock);
  15118. +
  15119. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  15120. +void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass)
  15121. +{
  15122. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock);
  15123. + spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  15124. +}
  15125. +EXPORT_SYMBOL(rt_spin_lock_nested);
  15126. +#endif
  15127. +
  15128. +void __lockfunc rt_spin_unlock(spinlock_t *lock)
  15129. +{
  15130. + /* NOTE: we always pass in '1' for nested, for simplicity */
  15131. + spin_release(&lock->dep_map, 1, _RET_IP_);
  15132. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  15133. +}
  15134. +EXPORT_SYMBOL(rt_spin_unlock);
  15135. +
  15136. +void __lockfunc __rt_spin_unlock(struct rt_mutex *lock)
  15137. +{
  15138. + rt_spin_lock_fastunlock(lock, rt_spin_lock_slowunlock);
  15139. +}
  15140. +EXPORT_SYMBOL(__rt_spin_unlock);
  15141. +
  15142. +/*
  15143. + * Wait for the lock to get unlocked: instead of polling for an unlock
  15144. + * (like raw spinlocks do), we lock and unlock, to force the kernel to
  15145. + * schedule if there's contention:
  15146. + */
  15147. +void __lockfunc rt_spin_unlock_wait(spinlock_t *lock)
  15148. +{
  15149. + spin_lock(lock);
  15150. + spin_unlock(lock);
  15151. +}
  15152. +EXPORT_SYMBOL(rt_spin_unlock_wait);
  15153. +
  15154. +int __lockfunc __rt_spin_trylock(struct rt_mutex *lock)
  15155. +{
  15156. + return rt_mutex_trylock(lock);
  15157. +}
  15158. +
  15159. +int __lockfunc rt_spin_trylock(spinlock_t *lock)
  15160. +{
  15161. + int ret = rt_mutex_trylock(&lock->lock);
  15162. +
  15163. + if (ret)
  15164. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  15165. + return ret;
  15166. +}
  15167. +EXPORT_SYMBOL(rt_spin_trylock);
  15168. +
  15169. +int __lockfunc rt_spin_trylock_bh(spinlock_t *lock)
  15170. +{
  15171. + int ret;
  15172. +
  15173. + local_bh_disable();
  15174. + ret = rt_mutex_trylock(&lock->lock);
  15175. + if (ret) {
  15176. + migrate_disable();
  15177. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  15178. + } else
  15179. + local_bh_enable();
  15180. + return ret;
  15181. +}
  15182. +EXPORT_SYMBOL(rt_spin_trylock_bh);
  15183. +
  15184. +int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags)
  15185. +{
  15186. + int ret;
  15187. +
  15188. + *flags = 0;
  15189. + ret = rt_mutex_trylock(&lock->lock);
  15190. + if (ret) {
  15191. + migrate_disable();
  15192. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  15193. + }
  15194. + return ret;
  15195. +}
  15196. +EXPORT_SYMBOL(rt_spin_trylock_irqsave);
  15197. +
  15198. +int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock)
  15199. +{
  15200. + /* Subtract 1 from counter unless that drops it to 0 (ie. it was 1) */
  15201. + if (atomic_add_unless(atomic, -1, 1))
  15202. + return 0;
  15203. + migrate_disable();
  15204. + rt_spin_lock(lock);
  15205. + if (atomic_dec_and_test(atomic))
  15206. + return 1;
  15207. + rt_spin_unlock(lock);
  15208. + migrate_enable();
  15209. + return 0;
  15210. +}
  15211. +EXPORT_SYMBOL(atomic_dec_and_spin_lock);
  15212. +
  15213. + void
  15214. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key)
  15215. +{
  15216. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  15217. + /*
  15218. + * Make sure we are not reinitializing a held lock:
  15219. + */
  15220. + debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  15221. + lockdep_init_map(&lock->dep_map, name, key, 0);
  15222. +#endif
  15223. +}
  15224. +EXPORT_SYMBOL(__rt_spin_lock_init);
  15225. +
  15226. +#endif /* PREEMPT_RT_FULL */
  15227. +
  15228. +#ifdef CONFIG_PREEMPT_RT_FULL
  15229. + static inline int __sched
  15230. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  15231. +{
  15232. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  15233. + struct ww_acquire_ctx *hold_ctx = ACCESS_ONCE(ww->ctx);
  15234. +
  15235. + if (!hold_ctx)
  15236. + return 0;
  15237. +
  15238. + if (unlikely(ctx == hold_ctx))
  15239. + return -EALREADY;
  15240. +
  15241. + if (ctx->stamp - hold_ctx->stamp <= LONG_MAX &&
  15242. + (ctx->stamp != hold_ctx->stamp || ctx > hold_ctx)) {
  15243. +#ifdef CONFIG_DEBUG_MUTEXES
  15244. + DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
  15245. + ctx->contending_lock = ww;
  15246. +#endif
  15247. + return -EDEADLK;
  15248. + }
  15249. +
  15250. + return 0;
  15251. +}
  15252. +#else
  15253. + static inline int __sched
  15254. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  15255. +{
  15256. + BUG();
  15257. + return 0;
  15258. +}
  15259. +
  15260. +#endif
  15261. +
  15262. +static inline int
  15263. +try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  15264. + struct rt_mutex_waiter *waiter)
  15265. +{
  15266. + return __try_to_take_rt_mutex(lock, task, waiter, STEAL_NORMAL);
  15267. +}
  15268. +
  15269. /*
  15270. * Task blocks on lock.
  15271. *
  15272. @@ -896,6 +1279,23 @@
  15273. return -EDEADLK;
  15274. raw_spin_lock_irqsave(&task->pi_lock, flags);
  15275. +
  15276. + /*
  15277. + * In the case of futex requeue PI, this will be a proxy
  15278. + * lock. The task will wake unaware that it is enqueueed on
  15279. + * this lock. Avoid blocking on two locks and corrupting
  15280. + * pi_blocked_on via the PI_WAKEUP_INPROGRESS
  15281. + * flag. futex_wait_requeue_pi() sets this when it wakes up
  15282. + * before requeue (due to a signal or timeout). Do not enqueue
  15283. + * the task if PI_WAKEUP_INPROGRESS is set.
  15284. + */
  15285. + if (task != current && task->pi_blocked_on == PI_WAKEUP_INPROGRESS) {
  15286. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  15287. + return -EAGAIN;
  15288. + }
  15289. +
  15290. + BUG_ON(rt_mutex_real_waiter(task->pi_blocked_on));
  15291. +
  15292. __rt_mutex_adjust_prio(task);
  15293. waiter->task = task;
  15294. waiter->lock = lock;
  15295. @@ -919,7 +1319,7 @@
  15296. rt_mutex_enqueue_pi(owner, waiter);
  15297. __rt_mutex_adjust_prio(owner);
  15298. - if (owner->pi_blocked_on)
  15299. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  15300. chain_walk = 1;
  15301. } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  15302. chain_walk = 1;
  15303. @@ -957,8 +1357,9 @@
  15304. /*
  15305. * Wake up the next waiter on the lock.
  15306. *
  15307. - * Remove the top waiter from the current tasks pi waiter list and
  15308. - * wake it up.
  15309. + * Remove the top waiter from the current tasks pi waiter list,
  15310. + * wake it up and return whether the current task needs to undo
  15311. + * a potential priority boosting.
  15312. *
  15313. * Called with lock->wait_lock held.
  15314. */
  15315. @@ -996,7 +1397,7 @@
  15316. * long as we hold lock->wait_lock. The waiter task needs to
  15317. * acquire it in order to dequeue the waiter.
  15318. */
  15319. - wake_up_process(waiter->task);
  15320. + rt_mutex_wake_waiter(waiter);
  15321. }
  15322. /*
  15323. @@ -1010,7 +1411,7 @@
  15324. {
  15325. bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  15326. struct task_struct *owner = rt_mutex_owner(lock);
  15327. - struct rt_mutex *next_lock;
  15328. + struct rt_mutex *next_lock = NULL;
  15329. unsigned long flags;
  15330. raw_spin_lock_irqsave(&current->pi_lock, flags);
  15331. @@ -1035,7 +1436,8 @@
  15332. __rt_mutex_adjust_prio(owner);
  15333. /* Store the lock on which owner is blocked or NULL */
  15334. - next_lock = task_blocked_on_lock(owner);
  15335. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  15336. + next_lock = task_blocked_on_lock(owner);
  15337. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  15338. @@ -1071,17 +1473,17 @@
  15339. raw_spin_lock_irqsave(&task->pi_lock, flags);
  15340. waiter = task->pi_blocked_on;
  15341. - if (!waiter || (waiter->prio == task->prio &&
  15342. + if (!rt_mutex_real_waiter(waiter) || (waiter->prio == task->prio &&
  15343. !dl_prio(task->prio))) {
  15344. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  15345. return;
  15346. }
  15347. next_lock = waiter->lock;
  15348. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  15349. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  15350. get_task_struct(task);
  15351. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  15352. rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
  15353. next_lock, NULL, task);
  15354. }
  15355. @@ -1099,7 +1501,8 @@
  15356. static int __sched
  15357. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  15358. struct hrtimer_sleeper *timeout,
  15359. - struct rt_mutex_waiter *waiter)
  15360. + struct rt_mutex_waiter *waiter,
  15361. + struct ww_acquire_ctx *ww_ctx)
  15362. {
  15363. int ret = 0;
  15364. @@ -1122,6 +1525,12 @@
  15365. break;
  15366. }
  15367. + if (ww_ctx && ww_ctx->acquired > 0) {
  15368. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  15369. + if (ret)
  15370. + break;
  15371. + }
  15372. +
  15373. raw_spin_unlock(&lock->wait_lock);
  15374. debug_rt_mutex_print_deadlock(waiter);
  15375. @@ -1156,25 +1565,102 @@
  15376. }
  15377. }
  15378. +static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww,
  15379. + struct ww_acquire_ctx *ww_ctx)
  15380. +{
  15381. +#ifdef CONFIG_DEBUG_MUTEXES
  15382. + /*
  15383. + * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
  15384. + * but released with a normal mutex_unlock in this call.
  15385. + *
  15386. + * This should never happen, always use ww_mutex_unlock.
  15387. + */
  15388. + DEBUG_LOCKS_WARN_ON(ww->ctx);
  15389. +
  15390. + /*
  15391. + * Not quite done after calling ww_acquire_done() ?
  15392. + */
  15393. + DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
  15394. +
  15395. + if (ww_ctx->contending_lock) {
  15396. + /*
  15397. + * After -EDEADLK you tried to
  15398. + * acquire a different ww_mutex? Bad!
  15399. + */
  15400. + DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
  15401. +
  15402. + /*
  15403. + * You called ww_mutex_lock after receiving -EDEADLK,
  15404. + * but 'forgot' to unlock everything else first?
  15405. + */
  15406. + DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
  15407. + ww_ctx->contending_lock = NULL;
  15408. + }
  15409. +
  15410. + /*
  15411. + * Naughty, using a different class will lead to undefined behavior!
  15412. + */
  15413. + DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
  15414. +#endif
  15415. + ww_ctx->acquired++;
  15416. +}
  15417. +
  15418. +#ifdef CONFIG_PREEMPT_RT_FULL
  15419. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  15420. + struct ww_acquire_ctx *ww_ctx)
  15421. +{
  15422. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  15423. + struct rt_mutex_waiter *waiter, *n;
  15424. +
  15425. + /*
  15426. + * This branch gets optimized out for the common case,
  15427. + * and is only important for ww_mutex_lock.
  15428. + */
  15429. + ww_mutex_lock_acquired(ww, ww_ctx);
  15430. + ww->ctx = ww_ctx;
  15431. +
  15432. + /*
  15433. + * Give any possible sleeping processes the chance to wake up,
  15434. + * so they can recheck if they have to back off.
  15435. + */
  15436. + rbtree_postorder_for_each_entry_safe(waiter, n, &lock->waiters,
  15437. + tree_entry) {
  15438. + /* XXX debug rt mutex waiter wakeup */
  15439. +
  15440. + BUG_ON(waiter->lock != lock);
  15441. + rt_mutex_wake_waiter(waiter);
  15442. + }
  15443. +}
  15444. +
  15445. +#else
  15446. +
  15447. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  15448. + struct ww_acquire_ctx *ww_ctx)
  15449. +{
  15450. + BUG();
  15451. +}
  15452. +#endif
  15453. +
  15454. /*
  15455. * Slow path lock function:
  15456. */
  15457. static int __sched
  15458. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  15459. struct hrtimer_sleeper *timeout,
  15460. - enum rtmutex_chainwalk chwalk)
  15461. + enum rtmutex_chainwalk chwalk,
  15462. + struct ww_acquire_ctx *ww_ctx)
  15463. {
  15464. struct rt_mutex_waiter waiter;
  15465. int ret = 0;
  15466. - debug_rt_mutex_init_waiter(&waiter);
  15467. - RB_CLEAR_NODE(&waiter.pi_tree_entry);
  15468. - RB_CLEAR_NODE(&waiter.tree_entry);
  15469. + rt_mutex_init_waiter(&waiter, false);
  15470. raw_spin_lock(&lock->wait_lock);
  15471. /* Try to acquire the lock again: */
  15472. if (try_to_take_rt_mutex(lock, current, NULL)) {
  15473. + if (ww_ctx)
  15474. + ww_mutex_account_lock(lock, ww_ctx);
  15475. raw_spin_unlock(&lock->wait_lock);
  15476. return 0;
  15477. }
  15478. @@ -1192,13 +1678,23 @@
  15479. if (likely(!ret))
  15480. /* sleep on the mutex */
  15481. - ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  15482. + ret = __rt_mutex_slowlock(lock, state, timeout, &waiter,
  15483. + ww_ctx);
  15484. + else if (ww_ctx) {
  15485. + /* ww_mutex received EDEADLK, let it become EALREADY */
  15486. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  15487. + BUG_ON(!ret);
  15488. + }
  15489. if (unlikely(ret)) {
  15490. __set_current_state(TASK_RUNNING);
  15491. if (rt_mutex_has_waiters(lock))
  15492. remove_waiter(lock, &waiter);
  15493. - rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  15494. + /* ww_mutex want to report EDEADLK/EALREADY, let them */
  15495. + if (!ww_ctx)
  15496. + rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  15497. + } else if (ww_ctx) {
  15498. + ww_mutex_account_lock(lock, ww_ctx);
  15499. }
  15500. /*
  15501. @@ -1255,7 +1751,7 @@
  15502. /*
  15503. * Slow path to release a rt-mutex:
  15504. */
  15505. -static void __sched
  15506. +static bool __sched
  15507. rt_mutex_slowunlock(struct rt_mutex *lock)
  15508. {
  15509. raw_spin_lock(&lock->wait_lock);
  15510. @@ -1298,7 +1794,7 @@
  15511. while (!rt_mutex_has_waiters(lock)) {
  15512. /* Drops lock->wait_lock ! */
  15513. if (unlock_rt_mutex_safe(lock) == true)
  15514. - return;
  15515. + return false;
  15516. /* Relock the rtmutex and try again */
  15517. raw_spin_lock(&lock->wait_lock);
  15518. }
  15519. @@ -1311,8 +1807,7 @@
  15520. raw_spin_unlock(&lock->wait_lock);
  15521. - /* Undo pi boosting if necessary: */
  15522. - rt_mutex_adjust_prio(current);
  15523. + return true;
  15524. }
  15525. /*
  15526. @@ -1323,31 +1818,36 @@
  15527. */
  15528. static inline int
  15529. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  15530. + struct ww_acquire_ctx *ww_ctx,
  15531. int (*slowfn)(struct rt_mutex *lock, int state,
  15532. struct hrtimer_sleeper *timeout,
  15533. - enum rtmutex_chainwalk chwalk))
  15534. + enum rtmutex_chainwalk chwalk,
  15535. + struct ww_acquire_ctx *ww_ctx))
  15536. {
  15537. if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  15538. rt_mutex_deadlock_account_lock(lock, current);
  15539. return 0;
  15540. } else
  15541. - return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
  15542. + return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK,
  15543. + ww_ctx);
  15544. }
  15545. static inline int
  15546. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  15547. struct hrtimer_sleeper *timeout,
  15548. enum rtmutex_chainwalk chwalk,
  15549. + struct ww_acquire_ctx *ww_ctx,
  15550. int (*slowfn)(struct rt_mutex *lock, int state,
  15551. struct hrtimer_sleeper *timeout,
  15552. - enum rtmutex_chainwalk chwalk))
  15553. + enum rtmutex_chainwalk chwalk,
  15554. + struct ww_acquire_ctx *ww_ctx))
  15555. {
  15556. if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
  15557. likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  15558. rt_mutex_deadlock_account_lock(lock, current);
  15559. return 0;
  15560. } else
  15561. - return slowfn(lock, state, timeout, chwalk);
  15562. + return slowfn(lock, state, timeout, chwalk, ww_ctx);
  15563. }
  15564. static inline int
  15565. @@ -1363,12 +1863,14 @@
  15566. static inline void
  15567. rt_mutex_fastunlock(struct rt_mutex *lock,
  15568. - void (*slowfn)(struct rt_mutex *lock))
  15569. + bool (*slowfn)(struct rt_mutex *lock))
  15570. {
  15571. - if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  15572. + if (likely(rt_mutex_cmpxchg(lock, current, NULL))) {
  15573. rt_mutex_deadlock_account_unlock(current);
  15574. - else
  15575. - slowfn(lock);
  15576. + } else if (slowfn(lock)) {
  15577. + /* Undo pi boosting if necessary: */
  15578. + rt_mutex_adjust_prio(current);
  15579. + }
  15580. }
  15581. /**
  15582. @@ -1380,7 +1882,7 @@
  15583. {
  15584. might_sleep();
  15585. - rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
  15586. + rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, NULL, rt_mutex_slowlock);
  15587. }
  15588. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  15589. @@ -1397,7 +1899,7 @@
  15590. {
  15591. might_sleep();
  15592. - return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
  15593. + return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, NULL, rt_mutex_slowlock);
  15594. }
  15595. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  15596. @@ -1410,11 +1912,30 @@
  15597. might_sleep();
  15598. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  15599. - RT_MUTEX_FULL_CHAINWALK,
  15600. + RT_MUTEX_FULL_CHAINWALK, NULL,
  15601. rt_mutex_slowlock);
  15602. }
  15603. /**
  15604. + * rt_mutex_lock_killable - lock a rt_mutex killable
  15605. + *
  15606. + * @lock: the rt_mutex to be locked
  15607. + * @detect_deadlock: deadlock detection on/off
  15608. + *
  15609. + * Returns:
  15610. + * 0 on success
  15611. + * -EINTR when interrupted by a signal
  15612. + * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  15613. + */
  15614. +int __sched rt_mutex_lock_killable(struct rt_mutex *lock)
  15615. +{
  15616. + might_sleep();
  15617. +
  15618. + return rt_mutex_fastlock(lock, TASK_KILLABLE, NULL, rt_mutex_slowlock);
  15619. +}
  15620. +EXPORT_SYMBOL_GPL(rt_mutex_lock_killable);
  15621. +
  15622. +/**
  15623. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  15624. * the timeout structure is provided
  15625. * by the caller
  15626. @@ -1434,6 +1955,7 @@
  15627. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  15628. RT_MUTEX_MIN_CHAINWALK,
  15629. + NULL,
  15630. rt_mutex_slowlock);
  15631. }
  15632. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  15633. @@ -1463,6 +1985,22 @@
  15634. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  15635. /**
  15636. + * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
  15637. + * @lock: the rt_mutex to be unlocked
  15638. + *
  15639. + * Returns: true/false indicating whether priority adjustment is
  15640. + * required or not.
  15641. + */
  15642. +bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
  15643. +{
  15644. + if (likely(rt_mutex_cmpxchg(lock, current, NULL))) {
  15645. + rt_mutex_deadlock_account_unlock(current);
  15646. + return false;
  15647. + }
  15648. + return rt_mutex_slowunlock(lock);
  15649. +}
  15650. +
  15651. +/**
  15652. * rt_mutex_destroy - mark a mutex unusable
  15653. * @lock: the mutex to be destroyed
  15654. *
  15655. @@ -1492,13 +2030,12 @@
  15656. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  15657. {
  15658. lock->owner = NULL;
  15659. - raw_spin_lock_init(&lock->wait_lock);
  15660. lock->waiters = RB_ROOT;
  15661. lock->waiters_leftmost = NULL;
  15662. debug_rt_mutex_init(lock, name);
  15663. }
  15664. -EXPORT_SYMBOL_GPL(__rt_mutex_init);
  15665. +EXPORT_SYMBOL(__rt_mutex_init);
  15666. /**
  15667. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  15668. @@ -1513,7 +2050,7 @@
  15669. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  15670. struct task_struct *proxy_owner)
  15671. {
  15672. - __rt_mutex_init(lock, NULL);
  15673. + rt_mutex_init(lock);
  15674. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  15675. rt_mutex_set_owner(lock, proxy_owner);
  15676. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  15677. @@ -1561,6 +2098,35 @@
  15678. return 1;
  15679. }
  15680. +#ifdef CONFIG_PREEMPT_RT_FULL
  15681. + /*
  15682. + * In PREEMPT_RT there's an added race.
  15683. + * If the task, that we are about to requeue, times out,
  15684. + * it can set the PI_WAKEUP_INPROGRESS. This tells the requeue
  15685. + * to skip this task. But right after the task sets
  15686. + * its pi_blocked_on to PI_WAKEUP_INPROGRESS it can then
  15687. + * block on the spin_lock(&hb->lock), which in RT is an rtmutex.
  15688. + * This will replace the PI_WAKEUP_INPROGRESS with the actual
  15689. + * lock that it blocks on. We *must not* place this task
  15690. + * on this proxy lock in that case.
  15691. + *
  15692. + * To prevent this race, we first take the task's pi_lock
  15693. + * and check if it has updated its pi_blocked_on. If it has,
  15694. + * we assume that it woke up and we return -EAGAIN.
  15695. + * Otherwise, we set the task's pi_blocked_on to
  15696. + * PI_REQUEUE_INPROGRESS, so that if the task is waking up
  15697. + * it will know that we are in the process of requeuing it.
  15698. + */
  15699. + raw_spin_lock_irq(&task->pi_lock);
  15700. + if (task->pi_blocked_on) {
  15701. + raw_spin_unlock_irq(&task->pi_lock);
  15702. + raw_spin_unlock(&lock->wait_lock);
  15703. + return -EAGAIN;
  15704. + }
  15705. + task->pi_blocked_on = PI_REQUEUE_INPROGRESS;
  15706. + raw_spin_unlock_irq(&task->pi_lock);
  15707. +#endif
  15708. +
  15709. /* We enforce deadlock detection for futexes */
  15710. ret = task_blocks_on_rt_mutex(lock, waiter, task,
  15711. RT_MUTEX_FULL_CHAINWALK);
  15712. @@ -1575,7 +2141,7 @@
  15713. ret = 0;
  15714. }
  15715. - if (unlikely(ret))
  15716. + if (ret && rt_mutex_has_waiters(lock))
  15717. remove_waiter(lock, waiter);
  15718. raw_spin_unlock(&lock->wait_lock);
  15719. @@ -1631,7 +2197,7 @@
  15720. set_current_state(TASK_INTERRUPTIBLE);
  15721. /* sleep on the mutex */
  15722. - ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  15723. + ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter, NULL);
  15724. if (unlikely(ret))
  15725. remove_waiter(lock, waiter);
  15726. @@ -1646,3 +2212,89 @@
  15727. return ret;
  15728. }
  15729. +
  15730. +static inline int
  15731. +ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  15732. +{
  15733. +#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  15734. + unsigned tmp;
  15735. +
  15736. + if (ctx->deadlock_inject_countdown-- == 0) {
  15737. + tmp = ctx->deadlock_inject_interval;
  15738. + if (tmp > UINT_MAX/4)
  15739. + tmp = UINT_MAX;
  15740. + else
  15741. + tmp = tmp*2 + tmp + tmp/2;
  15742. +
  15743. + ctx->deadlock_inject_interval = tmp;
  15744. + ctx->deadlock_inject_countdown = tmp;
  15745. + ctx->contending_lock = lock;
  15746. +
  15747. + ww_mutex_unlock(lock);
  15748. +
  15749. + return -EDEADLK;
  15750. + }
  15751. +#endif
  15752. +
  15753. + return 0;
  15754. +}
  15755. +
  15756. +#ifdef CONFIG_PREEMPT_RT_FULL
  15757. +int __sched
  15758. +__ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  15759. +{
  15760. + int ret;
  15761. +
  15762. + might_sleep();
  15763. +
  15764. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  15765. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_INTERRUPTIBLE, NULL, 0, ww_ctx);
  15766. + if (ret)
  15767. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  15768. + else if (!ret && ww_ctx->acquired > 1)
  15769. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  15770. +
  15771. + return ret;
  15772. +}
  15773. +EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible);
  15774. +
  15775. +int __sched
  15776. +__ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  15777. +{
  15778. + int ret;
  15779. +
  15780. + might_sleep();
  15781. +
  15782. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  15783. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_UNINTERRUPTIBLE, NULL, 0, ww_ctx);
  15784. + if (ret)
  15785. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  15786. + else if (!ret && ww_ctx->acquired > 1)
  15787. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  15788. +
  15789. + return ret;
  15790. +}
  15791. +EXPORT_SYMBOL_GPL(__ww_mutex_lock);
  15792. +
  15793. +void __sched ww_mutex_unlock(struct ww_mutex *lock)
  15794. +{
  15795. + int nest = !!lock->ctx;
  15796. +
  15797. + /*
  15798. + * The unlocking fastpath is the 0->1 transition from 'locked'
  15799. + * into 'unlocked' state:
  15800. + */
  15801. + if (nest) {
  15802. +#ifdef CONFIG_DEBUG_MUTEXES
  15803. + DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
  15804. +#endif
  15805. + if (lock->ctx->acquired > 0)
  15806. + lock->ctx->acquired--;
  15807. + lock->ctx = NULL;
  15808. + }
  15809. +
  15810. + mutex_release(&lock->base.dep_map, nest, _RET_IP_);
  15811. + rt_mutex_unlock(&lock->base.lock);
  15812. +}
  15813. +EXPORT_SYMBOL(ww_mutex_unlock);
  15814. +#endif
  15815. diff -Nur linux-4.1.13.orig/kernel/locking/rtmutex_common.h linux-4.1.13/kernel/locking/rtmutex_common.h
  15816. --- linux-4.1.13.orig/kernel/locking/rtmutex_common.h 2015-11-09 23:34:10.000000000 +0100
  15817. +++ linux-4.1.13/kernel/locking/rtmutex_common.h 2015-11-29 09:23:09.637612322 +0100
  15818. @@ -49,6 +49,7 @@
  15819. struct rb_node pi_tree_entry;
  15820. struct task_struct *task;
  15821. struct rt_mutex *lock;
  15822. + bool savestate;
  15823. #ifdef CONFIG_DEBUG_RT_MUTEXES
  15824. unsigned long ip;
  15825. struct pid *deadlock_task_pid;
  15826. @@ -119,6 +120,9 @@
  15827. /*
  15828. * PI-futex support (proxy locking functions, etc.):
  15829. */
  15830. +#define PI_WAKEUP_INPROGRESS ((struct rt_mutex_waiter *) 1)
  15831. +#define PI_REQUEUE_INPROGRESS ((struct rt_mutex_waiter *) 2)
  15832. +
  15833. extern struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock);
  15834. extern void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  15835. struct task_struct *proxy_owner);
  15836. @@ -132,10 +136,24 @@
  15837. struct rt_mutex_waiter *waiter);
  15838. extern int rt_mutex_timed_futex_lock(struct rt_mutex *l, struct hrtimer_sleeper *to);
  15839. +extern bool rt_mutex_futex_unlock(struct rt_mutex *lock);
  15840. +
  15841. +extern void rt_mutex_adjust_prio(struct task_struct *task);
  15842. +
  15843. #ifdef CONFIG_DEBUG_RT_MUTEXES
  15844. # include "rtmutex-debug.h"
  15845. #else
  15846. # include "rtmutex.h"
  15847. #endif
  15848. +static inline void
  15849. +rt_mutex_init_waiter(struct rt_mutex_waiter *waiter, bool savestate)
  15850. +{
  15851. + debug_rt_mutex_init_waiter(waiter);
  15852. + waiter->task = NULL;
  15853. + waiter->savestate = savestate;
  15854. + RB_CLEAR_NODE(&waiter->pi_tree_entry);
  15855. + RB_CLEAR_NODE(&waiter->tree_entry);
  15856. +}
  15857. +
  15858. #endif
  15859. diff -Nur linux-4.1.13.orig/kernel/locking/spinlock.c linux-4.1.13/kernel/locking/spinlock.c
  15860. --- linux-4.1.13.orig/kernel/locking/spinlock.c 2015-11-09 23:34:10.000000000 +0100
  15861. +++ linux-4.1.13/kernel/locking/spinlock.c 2015-11-29 09:23:09.637612322 +0100
  15862. @@ -124,8 +124,11 @@
  15863. * __[spin|read|write]_lock_bh()
  15864. */
  15865. BUILD_LOCK_OPS(spin, raw_spinlock);
  15866. +
  15867. +#ifndef CONFIG_PREEMPT_RT_FULL
  15868. BUILD_LOCK_OPS(read, rwlock);
  15869. BUILD_LOCK_OPS(write, rwlock);
  15870. +#endif
  15871. #endif
  15872. @@ -209,6 +212,8 @@
  15873. EXPORT_SYMBOL(_raw_spin_unlock_bh);
  15874. #endif
  15875. +#ifndef CONFIG_PREEMPT_RT_FULL
  15876. +
  15877. #ifndef CONFIG_INLINE_READ_TRYLOCK
  15878. int __lockfunc _raw_read_trylock(rwlock_t *lock)
  15879. {
  15880. @@ -353,6 +358,8 @@
  15881. EXPORT_SYMBOL(_raw_write_unlock_bh);
  15882. #endif
  15883. +#endif /* !PREEMPT_RT_FULL */
  15884. +
  15885. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  15886. void __lockfunc _raw_spin_lock_nested(raw_spinlock_t *lock, int subclass)
  15887. diff -Nur linux-4.1.13.orig/kernel/locking/spinlock_debug.c linux-4.1.13/kernel/locking/spinlock_debug.c
  15888. --- linux-4.1.13.orig/kernel/locking/spinlock_debug.c 2015-11-09 23:34:10.000000000 +0100
  15889. +++ linux-4.1.13/kernel/locking/spinlock_debug.c 2015-11-29 09:23:09.637612322 +0100
  15890. @@ -31,6 +31,7 @@
  15891. EXPORT_SYMBOL(__raw_spin_lock_init);
  15892. +#ifndef CONFIG_PREEMPT_RT_FULL
  15893. void __rwlock_init(rwlock_t *lock, const char *name,
  15894. struct lock_class_key *key)
  15895. {
  15896. @@ -48,6 +49,7 @@
  15897. }
  15898. EXPORT_SYMBOL(__rwlock_init);
  15899. +#endif
  15900. static void spin_dump(raw_spinlock_t *lock, const char *msg)
  15901. {
  15902. @@ -159,6 +161,7 @@
  15903. arch_spin_unlock(&lock->raw_lock);
  15904. }
  15905. +#ifndef CONFIG_PREEMPT_RT_FULL
  15906. static void rwlock_bug(rwlock_t *lock, const char *msg)
  15907. {
  15908. if (!debug_locks_off())
  15909. @@ -300,3 +303,5 @@
  15910. debug_write_unlock(lock);
  15911. arch_write_unlock(&lock->raw_lock);
  15912. }
  15913. +
  15914. +#endif
  15915. diff -Nur linux-4.1.13.orig/kernel/panic.c linux-4.1.13/kernel/panic.c
  15916. --- linux-4.1.13.orig/kernel/panic.c 2015-11-09 23:34:10.000000000 +0100
  15917. +++ linux-4.1.13/kernel/panic.c 2015-11-29 09:23:09.637612322 +0100
  15918. @@ -387,9 +387,11 @@
  15919. static int init_oops_id(void)
  15920. {
  15921. +#ifndef CONFIG_PREEMPT_RT_FULL
  15922. if (!oops_id)
  15923. get_random_bytes(&oops_id, sizeof(oops_id));
  15924. else
  15925. +#endif
  15926. oops_id++;
  15927. return 0;
  15928. diff -Nur linux-4.1.13.orig/kernel/power/hibernate.c linux-4.1.13/kernel/power/hibernate.c
  15929. --- linux-4.1.13.orig/kernel/power/hibernate.c 2015-11-09 23:34:10.000000000 +0100
  15930. +++ linux-4.1.13/kernel/power/hibernate.c 2015-11-29 09:23:09.637612322 +0100
  15931. @@ -285,6 +285,8 @@
  15932. local_irq_disable();
  15933. + system_state = SYSTEM_SUSPEND;
  15934. +
  15935. error = syscore_suspend();
  15936. if (error) {
  15937. printk(KERN_ERR "PM: Some system devices failed to power down, "
  15938. @@ -314,6 +316,7 @@
  15939. syscore_resume();
  15940. Enable_irqs:
  15941. + system_state = SYSTEM_RUNNING;
  15942. local_irq_enable();
  15943. Enable_cpus:
  15944. @@ -437,6 +440,7 @@
  15945. goto Enable_cpus;
  15946. local_irq_disable();
  15947. + system_state = SYSTEM_SUSPEND;
  15948. error = syscore_suspend();
  15949. if (error)
  15950. @@ -470,6 +474,7 @@
  15951. syscore_resume();
  15952. Enable_irqs:
  15953. + system_state = SYSTEM_RUNNING;
  15954. local_irq_enable();
  15955. Enable_cpus:
  15956. @@ -555,6 +560,7 @@
  15957. goto Platform_finish;
  15958. local_irq_disable();
  15959. + system_state = SYSTEM_SUSPEND;
  15960. syscore_suspend();
  15961. if (pm_wakeup_pending()) {
  15962. error = -EAGAIN;
  15963. @@ -567,6 +573,7 @@
  15964. Power_up:
  15965. syscore_resume();
  15966. + system_state = SYSTEM_RUNNING;
  15967. local_irq_enable();
  15968. enable_nonboot_cpus();
  15969. diff -Nur linux-4.1.13.orig/kernel/power/suspend.c linux-4.1.13/kernel/power/suspend.c
  15970. --- linux-4.1.13.orig/kernel/power/suspend.c 2015-11-09 23:34:10.000000000 +0100
  15971. +++ linux-4.1.13/kernel/power/suspend.c 2015-11-29 09:23:09.637612322 +0100
  15972. @@ -356,6 +356,8 @@
  15973. arch_suspend_disable_irqs();
  15974. BUG_ON(!irqs_disabled());
  15975. + system_state = SYSTEM_SUSPEND;
  15976. +
  15977. error = syscore_suspend();
  15978. if (!error) {
  15979. *wakeup = pm_wakeup_pending();
  15980. @@ -370,6 +372,8 @@
  15981. syscore_resume();
  15982. }
  15983. + system_state = SYSTEM_RUNNING;
  15984. +
  15985. arch_suspend_enable_irqs();
  15986. BUG_ON(irqs_disabled());
  15987. diff -Nur linux-4.1.13.orig/kernel/printk/printk.c linux-4.1.13/kernel/printk/printk.c
  15988. --- linux-4.1.13.orig/kernel/printk/printk.c 2015-11-09 23:34:10.000000000 +0100
  15989. +++ linux-4.1.13/kernel/printk/printk.c 2015-11-29 09:23:09.641612055 +0100
  15990. @@ -1163,6 +1163,7 @@
  15991. {
  15992. char *text;
  15993. int len = 0;
  15994. + int attempts = 0;
  15995. text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
  15996. if (!text)
  15997. @@ -1174,7 +1175,14 @@
  15998. u64 seq;
  15999. u32 idx;
  16000. enum log_flags prev;
  16001. -
  16002. + int num_msg;
  16003. +try_again:
  16004. + attempts++;
  16005. + if (attempts > 10) {
  16006. + len = -EBUSY;
  16007. + goto out;
  16008. + }
  16009. + num_msg = 0;
  16010. if (clear_seq < log_first_seq) {
  16011. /* messages are gone, move to first available one */
  16012. clear_seq = log_first_seq;
  16013. @@ -1195,6 +1203,14 @@
  16014. prev = msg->flags;
  16015. idx = log_next(idx);
  16016. seq++;
  16017. + num_msg++;
  16018. + if (num_msg > 5) {
  16019. + num_msg = 0;
  16020. + raw_spin_unlock_irq(&logbuf_lock);
  16021. + raw_spin_lock_irq(&logbuf_lock);
  16022. + if (clear_seq < log_first_seq)
  16023. + goto try_again;
  16024. + }
  16025. }
  16026. /* move first record forward until length fits into the buffer */
  16027. @@ -1208,6 +1224,14 @@
  16028. prev = msg->flags;
  16029. idx = log_next(idx);
  16030. seq++;
  16031. + num_msg++;
  16032. + if (num_msg > 5) {
  16033. + num_msg = 0;
  16034. + raw_spin_unlock_irq(&logbuf_lock);
  16035. + raw_spin_lock_irq(&logbuf_lock);
  16036. + if (clear_seq < log_first_seq)
  16037. + goto try_again;
  16038. + }
  16039. }
  16040. /* last message fitting into this dump */
  16041. @@ -1248,6 +1272,7 @@
  16042. clear_seq = log_next_seq;
  16043. clear_idx = log_next_idx;
  16044. }
  16045. +out:
  16046. raw_spin_unlock_irq(&logbuf_lock);
  16047. kfree(text);
  16048. @@ -1401,6 +1426,7 @@
  16049. if (!console_drivers)
  16050. return;
  16051. + migrate_disable();
  16052. for_each_console(con) {
  16053. if (exclusive_console && con != exclusive_console)
  16054. continue;
  16055. @@ -1413,6 +1439,7 @@
  16056. continue;
  16057. con->write(con, text, len);
  16058. }
  16059. + migrate_enable();
  16060. }
  16061. /*
  16062. @@ -1473,6 +1500,15 @@
  16063. static int console_trylock_for_printk(void)
  16064. {
  16065. unsigned int cpu = smp_processor_id();
  16066. +#ifdef CONFIG_PREEMPT_RT_FULL
  16067. + int lock = !early_boot_irqs_disabled && (preempt_count() == 0) &&
  16068. + !irqs_disabled();
  16069. +#else
  16070. + int lock = 1;
  16071. +#endif
  16072. +
  16073. + if (!lock)
  16074. + return 0;
  16075. if (!console_trylock())
  16076. return 0;
  16077. @@ -1607,6 +1643,62 @@
  16078. return textlen;
  16079. }
  16080. +#ifdef CONFIG_EARLY_PRINTK
  16081. +struct console *early_console;
  16082. +
  16083. +static void early_vprintk(const char *fmt, va_list ap)
  16084. +{
  16085. + if (early_console) {
  16086. + char buf[512];
  16087. + int n = vscnprintf(buf, sizeof(buf), fmt, ap);
  16088. +
  16089. + early_console->write(early_console, buf, n);
  16090. + }
  16091. +}
  16092. +
  16093. +asmlinkage void early_printk(const char *fmt, ...)
  16094. +{
  16095. + va_list ap;
  16096. +
  16097. + va_start(ap, fmt);
  16098. + early_vprintk(fmt, ap);
  16099. + va_end(ap);
  16100. +}
  16101. +
  16102. +/*
  16103. + * This is independent of any log levels - a global
  16104. + * kill switch that turns off all of printk.
  16105. + *
  16106. + * Used by the NMI watchdog if early-printk is enabled.
  16107. + */
  16108. +static bool __read_mostly printk_killswitch;
  16109. +
  16110. +static int __init force_early_printk_setup(char *str)
  16111. +{
  16112. + printk_killswitch = true;
  16113. + return 0;
  16114. +}
  16115. +early_param("force_early_printk", force_early_printk_setup);
  16116. +
  16117. +void printk_kill(void)
  16118. +{
  16119. + printk_killswitch = true;
  16120. +}
  16121. +
  16122. +static int forced_early_printk(const char *fmt, va_list ap)
  16123. +{
  16124. + if (!printk_killswitch)
  16125. + return 0;
  16126. + early_vprintk(fmt, ap);
  16127. + return 1;
  16128. +}
  16129. +#else
  16130. +static inline int forced_early_printk(const char *fmt, va_list ap)
  16131. +{
  16132. + return 0;
  16133. +}
  16134. +#endif
  16135. +
  16136. asmlinkage int vprintk_emit(int facility, int level,
  16137. const char *dict, size_t dictlen,
  16138. const char *fmt, va_list args)
  16139. @@ -1623,6 +1715,13 @@
  16140. /* cpu currently holding logbuf_lock in this function */
  16141. static unsigned int logbuf_cpu = UINT_MAX;
  16142. + /*
  16143. + * Fall back to early_printk if a debugging subsystem has
  16144. + * killed printk output
  16145. + */
  16146. + if (unlikely(forced_early_printk(fmt, args)))
  16147. + return 1;
  16148. +
  16149. if (level == LOGLEVEL_SCHED) {
  16150. level = LOGLEVEL_DEFAULT;
  16151. in_sched = true;
  16152. @@ -1764,8 +1863,7 @@
  16153. * console_sem which would prevent anyone from printing to
  16154. * console
  16155. */
  16156. - preempt_disable();
  16157. -
  16158. + migrate_disable();
  16159. /*
  16160. * Try to acquire and then immediately release the console
  16161. * semaphore. The release will print out buffers and wake up
  16162. @@ -1773,7 +1871,7 @@
  16163. */
  16164. if (console_trylock_for_printk())
  16165. console_unlock();
  16166. - preempt_enable();
  16167. + migrate_enable();
  16168. lockdep_on();
  16169. }
  16170. @@ -1902,26 +2000,6 @@
  16171. #endif /* CONFIG_PRINTK */
  16172. -#ifdef CONFIG_EARLY_PRINTK
  16173. -struct console *early_console;
  16174. -
  16175. -asmlinkage __visible void early_printk(const char *fmt, ...)
  16176. -{
  16177. - va_list ap;
  16178. - char buf[512];
  16179. - int n;
  16180. -
  16181. - if (!early_console)
  16182. - return;
  16183. -
  16184. - va_start(ap, fmt);
  16185. - n = vscnprintf(buf, sizeof(buf), fmt, ap);
  16186. - va_end(ap);
  16187. -
  16188. - early_console->write(early_console, buf, n);
  16189. -}
  16190. -#endif
  16191. -
  16192. static int __add_preferred_console(char *name, int idx, char *options,
  16193. char *brl_options)
  16194. {
  16195. @@ -2143,11 +2221,16 @@
  16196. goto out;
  16197. len = cont_print_text(text, size);
  16198. +#ifndef CONFIG_PREEMPT_RT_FULL
  16199. raw_spin_unlock(&logbuf_lock);
  16200. stop_critical_timings();
  16201. call_console_drivers(cont.level, text, len);
  16202. start_critical_timings();
  16203. local_irq_restore(flags);
  16204. +#else
  16205. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  16206. + call_console_drivers(cont.level, text, len);
  16207. +#endif
  16208. return;
  16209. out:
  16210. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  16211. @@ -2235,12 +2318,17 @@
  16212. console_idx = log_next(console_idx);
  16213. console_seq++;
  16214. console_prev = msg->flags;
  16215. +#ifdef CONFIG_PREEMPT_RT_FULL
  16216. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  16217. + call_console_drivers(level, text, len);
  16218. +#else
  16219. raw_spin_unlock(&logbuf_lock);
  16220. stop_critical_timings(); /* don't trace print latency */
  16221. call_console_drivers(level, text, len);
  16222. start_critical_timings();
  16223. local_irq_restore(flags);
  16224. +#endif
  16225. }
  16226. console_locked = 0;
  16227. diff -Nur linux-4.1.13.orig/kernel/ptrace.c linux-4.1.13/kernel/ptrace.c
  16228. --- linux-4.1.13.orig/kernel/ptrace.c 2015-11-09 23:34:10.000000000 +0100
  16229. +++ linux-4.1.13/kernel/ptrace.c 2015-11-29 09:23:09.641612055 +0100
  16230. @@ -129,7 +129,12 @@
  16231. spin_lock_irq(&task->sighand->siglock);
  16232. if (task_is_traced(task) && !__fatal_signal_pending(task)) {
  16233. - task->state = __TASK_TRACED;
  16234. + raw_spin_lock_irq(&task->pi_lock);
  16235. + if (task->state & __TASK_TRACED)
  16236. + task->state = __TASK_TRACED;
  16237. + else
  16238. + task->saved_state = __TASK_TRACED;
  16239. + raw_spin_unlock_irq(&task->pi_lock);
  16240. ret = true;
  16241. }
  16242. spin_unlock_irq(&task->sighand->siglock);
  16243. diff -Nur linux-4.1.13.orig/kernel/rcu/tree.c linux-4.1.13/kernel/rcu/tree.c
  16244. --- linux-4.1.13.orig/kernel/rcu/tree.c 2015-11-09 23:34:10.000000000 +0100
  16245. +++ linux-4.1.13/kernel/rcu/tree.c 2015-11-29 09:23:09.641612055 +0100
  16246. @@ -56,6 +56,11 @@
  16247. #include <linux/random.h>
  16248. #include <linux/ftrace_event.h>
  16249. #include <linux/suspend.h>
  16250. +#include <linux/delay.h>
  16251. +#include <linux/gfp.h>
  16252. +#include <linux/oom.h>
  16253. +#include <linux/smpboot.h>
  16254. +#include "../time/tick-internal.h"
  16255. #include "tree.h"
  16256. #include "rcu.h"
  16257. @@ -220,6 +225,19 @@
  16258. }
  16259. }
  16260. +#ifdef CONFIG_PREEMPT_RT_FULL
  16261. +static void rcu_preempt_qs(void);
  16262. +
  16263. +void rcu_bh_qs(void)
  16264. +{
  16265. + unsigned long flags;
  16266. +
  16267. + /* Callers to this function, rcu_preempt_qs(), must disable irqs. */
  16268. + local_irq_save(flags);
  16269. + rcu_preempt_qs();
  16270. + local_irq_restore(flags);
  16271. +}
  16272. +#else
  16273. void rcu_bh_qs(void)
  16274. {
  16275. if (!__this_cpu_read(rcu_bh_data.passed_quiesce)) {
  16276. @@ -229,6 +247,7 @@
  16277. __this_cpu_write(rcu_bh_data.passed_quiesce, 1);
  16278. }
  16279. }
  16280. +#endif
  16281. static DEFINE_PER_CPU(int, rcu_sched_qs_mask);
  16282. @@ -404,6 +423,7 @@
  16283. }
  16284. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  16285. +#ifndef CONFIG_PREEMPT_RT_FULL
  16286. /*
  16287. * Return the number of RCU BH batches completed thus far for debug & stats.
  16288. */
  16289. @@ -431,6 +451,13 @@
  16290. }
  16291. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  16292. +#else
  16293. +void rcu_force_quiescent_state(void)
  16294. +{
  16295. +}
  16296. +EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  16297. +#endif
  16298. +
  16299. /*
  16300. * Force a quiescent state for RCU-sched.
  16301. */
  16302. @@ -1545,7 +1572,7 @@
  16303. !ACCESS_ONCE(rsp->gp_flags) ||
  16304. !rsp->gp_kthread)
  16305. return;
  16306. - wake_up(&rsp->gp_wq);
  16307. + swait_wake(&rsp->gp_wq);
  16308. }
  16309. /*
  16310. @@ -1986,7 +2013,7 @@
  16311. ACCESS_ONCE(rsp->gpnum),
  16312. TPS("reqwait"));
  16313. rsp->gp_state = RCU_GP_WAIT_GPS;
  16314. - wait_event_interruptible(rsp->gp_wq,
  16315. + swait_event_interruptible(rsp->gp_wq,
  16316. ACCESS_ONCE(rsp->gp_flags) &
  16317. RCU_GP_FLAG_INIT);
  16318. /* Locking provides needed memory barrier. */
  16319. @@ -2015,7 +2042,7 @@
  16320. ACCESS_ONCE(rsp->gpnum),
  16321. TPS("fqswait"));
  16322. rsp->gp_state = RCU_GP_WAIT_FQS;
  16323. - ret = wait_event_interruptible_timeout(rsp->gp_wq,
  16324. + ret = swait_event_interruptible_timeout(rsp->gp_wq,
  16325. ((gf = ACCESS_ONCE(rsp->gp_flags)) &
  16326. RCU_GP_FLAG_FQS) ||
  16327. (!ACCESS_ONCE(rnp->qsmask) &&
  16328. @@ -2860,18 +2887,17 @@
  16329. /*
  16330. * Do RCU core processing for the current CPU.
  16331. */
  16332. -static void rcu_process_callbacks(struct softirq_action *unused)
  16333. +static void rcu_process_callbacks(void)
  16334. {
  16335. struct rcu_state *rsp;
  16336. if (cpu_is_offline(smp_processor_id()))
  16337. return;
  16338. - trace_rcu_utilization(TPS("Start RCU core"));
  16339. for_each_rcu_flavor(rsp)
  16340. __rcu_process_callbacks(rsp);
  16341. - trace_rcu_utilization(TPS("End RCU core"));
  16342. }
  16343. +static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  16344. /*
  16345. * Schedule RCU callback invocation. If the specified type of RCU
  16346. * does not support RCU priority boosting, just do a direct call,
  16347. @@ -2883,18 +2909,105 @@
  16348. {
  16349. if (unlikely(!ACCESS_ONCE(rcu_scheduler_fully_active)))
  16350. return;
  16351. - if (likely(!rsp->boost)) {
  16352. - rcu_do_batch(rsp, rdp);
  16353. + rcu_do_batch(rsp, rdp);
  16354. +}
  16355. +
  16356. +static void rcu_wake_cond(struct task_struct *t, int status)
  16357. +{
  16358. + /*
  16359. + * If the thread is yielding, only wake it when this
  16360. + * is invoked from idle
  16361. + */
  16362. + if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)))
  16363. + wake_up_process(t);
  16364. +}
  16365. +
  16366. +/*
  16367. + * Wake up this CPU's rcuc kthread to do RCU core processing.
  16368. + */
  16369. +static void invoke_rcu_core(void)
  16370. +{
  16371. + unsigned long flags;
  16372. + struct task_struct *t;
  16373. +
  16374. + if (!cpu_online(smp_processor_id()))
  16375. return;
  16376. + local_irq_save(flags);
  16377. + __this_cpu_write(rcu_cpu_has_work, 1);
  16378. + t = __this_cpu_read(rcu_cpu_kthread_task);
  16379. + if (t != NULL && current != t)
  16380. + rcu_wake_cond(t, __this_cpu_read(rcu_cpu_kthread_status));
  16381. + local_irq_restore(flags);
  16382. +}
  16383. +
  16384. +static void rcu_cpu_kthread_park(unsigned int cpu)
  16385. +{
  16386. + per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  16387. +}
  16388. +
  16389. +static int rcu_cpu_kthread_should_run(unsigned int cpu)
  16390. +{
  16391. + return __this_cpu_read(rcu_cpu_has_work);
  16392. +}
  16393. +
  16394. +/*
  16395. + * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  16396. + * RCU softirq used in flavors and configurations of RCU that do not
  16397. + * support RCU priority boosting.
  16398. + */
  16399. +static void rcu_cpu_kthread(unsigned int cpu)
  16400. +{
  16401. + unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  16402. + char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  16403. + int spincnt;
  16404. +
  16405. + for (spincnt = 0; spincnt < 10; spincnt++) {
  16406. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  16407. + local_bh_disable();
  16408. + *statusp = RCU_KTHREAD_RUNNING;
  16409. + this_cpu_inc(rcu_cpu_kthread_loops);
  16410. + local_irq_disable();
  16411. + work = *workp;
  16412. + *workp = 0;
  16413. + local_irq_enable();
  16414. + if (work)
  16415. + rcu_process_callbacks();
  16416. + local_bh_enable();
  16417. + if (*workp == 0) {
  16418. + trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  16419. + *statusp = RCU_KTHREAD_WAITING;
  16420. + return;
  16421. + }
  16422. }
  16423. - invoke_rcu_callbacks_kthread();
  16424. + *statusp = RCU_KTHREAD_YIELDING;
  16425. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  16426. + schedule_timeout_interruptible(2);
  16427. + trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  16428. + *statusp = RCU_KTHREAD_WAITING;
  16429. }
  16430. -static void invoke_rcu_core(void)
  16431. +static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  16432. + .store = &rcu_cpu_kthread_task,
  16433. + .thread_should_run = rcu_cpu_kthread_should_run,
  16434. + .thread_fn = rcu_cpu_kthread,
  16435. + .thread_comm = "rcuc/%u",
  16436. + .setup = rcu_cpu_kthread_setup,
  16437. + .park = rcu_cpu_kthread_park,
  16438. +};
  16439. +
  16440. +/*
  16441. + * Spawn per-CPU RCU core processing kthreads.
  16442. + */
  16443. +static int __init rcu_spawn_core_kthreads(void)
  16444. {
  16445. - if (cpu_online(smp_processor_id()))
  16446. - raise_softirq(RCU_SOFTIRQ);
  16447. + int cpu;
  16448. +
  16449. + for_each_possible_cpu(cpu)
  16450. + per_cpu(rcu_cpu_has_work, cpu) = 0;
  16451. + BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  16452. + return 0;
  16453. }
  16454. +early_initcall(rcu_spawn_core_kthreads);
  16455. /*
  16456. * Handle any core-RCU processing required by a call_rcu() invocation.
  16457. @@ -3040,6 +3153,7 @@
  16458. }
  16459. EXPORT_SYMBOL_GPL(call_rcu_sched);
  16460. +#ifndef CONFIG_PREEMPT_RT_FULL
  16461. /*
  16462. * Queue an RCU callback for invocation after a quicker grace period.
  16463. */
  16464. @@ -3048,6 +3162,7 @@
  16465. __call_rcu(head, func, &rcu_bh_state, -1, 0);
  16466. }
  16467. EXPORT_SYMBOL_GPL(call_rcu_bh);
  16468. +#endif
  16469. /*
  16470. * Queue an RCU callback for lazy invocation after a grace period.
  16471. @@ -3139,6 +3254,7 @@
  16472. }
  16473. EXPORT_SYMBOL_GPL(synchronize_sched);
  16474. +#ifndef CONFIG_PREEMPT_RT_FULL
  16475. /**
  16476. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  16477. *
  16478. @@ -3165,6 +3281,7 @@
  16479. wait_rcu_gp(call_rcu_bh);
  16480. }
  16481. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  16482. +#endif
  16483. /**
  16484. * get_state_synchronize_rcu - Snapshot current RCU state
  16485. @@ -3677,6 +3794,7 @@
  16486. mutex_unlock(&rsp->barrier_mutex);
  16487. }
  16488. +#ifndef CONFIG_PREEMPT_RT_FULL
  16489. /**
  16490. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  16491. */
  16492. @@ -3685,6 +3803,7 @@
  16493. _rcu_barrier(&rcu_bh_state);
  16494. }
  16495. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  16496. +#endif
  16497. /**
  16498. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  16499. @@ -4021,7 +4140,7 @@
  16500. }
  16501. }
  16502. - init_waitqueue_head(&rsp->gp_wq);
  16503. + init_swait_head(&rsp->gp_wq);
  16504. rnp = rsp->level[rcu_num_lvls - 1];
  16505. for_each_possible_cpu(i) {
  16506. while (i > rnp->grphi)
  16507. @@ -4120,7 +4239,6 @@
  16508. rcu_init_one(&rcu_bh_state, &rcu_bh_data);
  16509. rcu_init_one(&rcu_sched_state, &rcu_sched_data);
  16510. __rcu_init_preempt();
  16511. - open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  16512. /*
  16513. * We don't need protection against CPU-hotplug here because
  16514. diff -Nur linux-4.1.13.orig/kernel/rcu/tree.h linux-4.1.13/kernel/rcu/tree.h
  16515. --- linux-4.1.13.orig/kernel/rcu/tree.h 2015-11-09 23:34:10.000000000 +0100
  16516. +++ linux-4.1.13/kernel/rcu/tree.h 2015-11-29 09:23:09.641612055 +0100
  16517. @@ -27,6 +27,7 @@
  16518. #include <linux/threads.h>
  16519. #include <linux/cpumask.h>
  16520. #include <linux/seqlock.h>
  16521. +#include <linux/wait-simple.h>
  16522. /*
  16523. * Define shape of hierarchy based on NR_CPUS, CONFIG_RCU_FANOUT, and
  16524. @@ -210,7 +211,7 @@
  16525. /* This can happen due to race conditions. */
  16526. #endif /* #ifdef CONFIG_RCU_BOOST */
  16527. #ifdef CONFIG_RCU_NOCB_CPU
  16528. - wait_queue_head_t nocb_gp_wq[2];
  16529. + struct swait_head nocb_gp_wq[2];
  16530. /* Place for rcu_nocb_kthread() to wait GP. */
  16531. #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
  16532. int need_future_gp[2];
  16533. @@ -349,7 +350,7 @@
  16534. atomic_long_t nocb_q_count_lazy; /* invocation (all stages). */
  16535. struct rcu_head *nocb_follower_head; /* CBs ready to invoke. */
  16536. struct rcu_head **nocb_follower_tail;
  16537. - wait_queue_head_t nocb_wq; /* For nocb kthreads to sleep on. */
  16538. + struct swait_head nocb_wq; /* For nocb kthreads to sleep on. */
  16539. struct task_struct *nocb_kthread;
  16540. int nocb_defer_wakeup; /* Defer wakeup of nocb_kthread. */
  16541. @@ -438,7 +439,7 @@
  16542. unsigned long gpnum; /* Current gp number. */
  16543. unsigned long completed; /* # of last completed gp. */
  16544. struct task_struct *gp_kthread; /* Task for grace periods. */
  16545. - wait_queue_head_t gp_wq; /* Where GP task waits. */
  16546. + struct swait_head gp_wq; /* Where GP task waits. */
  16547. short gp_flags; /* Commands for GP task. */
  16548. short gp_state; /* GP kthread sleep state. */
  16549. @@ -529,12 +530,10 @@
  16550. DECLARE_PER_CPU(struct rcu_data, rcu_preempt_data);
  16551. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  16552. -#ifdef CONFIG_RCU_BOOST
  16553. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  16554. DECLARE_PER_CPU(int, rcu_cpu_kthread_cpu);
  16555. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  16556. DECLARE_PER_CPU(char, rcu_cpu_has_work);
  16557. -#endif /* #ifdef CONFIG_RCU_BOOST */
  16558. #ifndef RCU_TREE_NONCORE
  16559. @@ -553,10 +552,9 @@
  16560. static void __init __rcu_init_preempt(void);
  16561. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags);
  16562. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp);
  16563. -static void invoke_rcu_callbacks_kthread(void);
  16564. static bool rcu_is_callbacks_kthread(void);
  16565. +static void rcu_cpu_kthread_setup(unsigned int cpu);
  16566. #ifdef CONFIG_RCU_BOOST
  16567. -static void rcu_preempt_do_callbacks(void);
  16568. static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  16569. struct rcu_node *rnp);
  16570. #endif /* #ifdef CONFIG_RCU_BOOST */
  16571. diff -Nur linux-4.1.13.orig/kernel/rcu/tree_plugin.h linux-4.1.13/kernel/rcu/tree_plugin.h
  16572. --- linux-4.1.13.orig/kernel/rcu/tree_plugin.h 2015-11-09 23:34:10.000000000 +0100
  16573. +++ linux-4.1.13/kernel/rcu/tree_plugin.h 2015-11-29 09:23:09.641612055 +0100
  16574. @@ -24,27 +24,20 @@
  16575. * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  16576. */
  16577. -#include <linux/delay.h>
  16578. -#include <linux/gfp.h>
  16579. -#include <linux/oom.h>
  16580. -#include <linux/smpboot.h>
  16581. -#include "../time/tick-internal.h"
  16582. -
  16583. #ifdef CONFIG_RCU_BOOST
  16584. #include "../locking/rtmutex_common.h"
  16585. +#endif /* #ifdef CONFIG_RCU_BOOST */
  16586. +
  16587. /*
  16588. * Control variables for per-CPU and per-rcu_node kthreads. These
  16589. * handle all flavors of RCU.
  16590. */
  16591. -static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  16592. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  16593. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  16594. DEFINE_PER_CPU(char, rcu_cpu_has_work);
  16595. -#endif /* #ifdef CONFIG_RCU_BOOST */
  16596. -
  16597. #ifdef CONFIG_RCU_NOCB_CPU
  16598. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  16599. static bool have_rcu_nocb_mask; /* Was rcu_nocb_mask allocated? */
  16600. @@ -291,7 +284,7 @@
  16601. }
  16602. /* Hardware IRQ handlers cannot block, complain if they get here. */
  16603. - if (in_irq() || in_serving_softirq()) {
  16604. + if (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_OFFSET)) {
  16605. lockdep_rcu_suspicious(__FILE__, __LINE__,
  16606. "rcu_read_unlock() from irq or softirq with blocking in critical section!!!\n");
  16607. pr_alert("->rcu_read_unlock_special: %#x (b: %d, nq: %d)\n",
  16608. @@ -496,15 +489,6 @@
  16609. t->rcu_read_unlock_special.b.need_qs = true;
  16610. }
  16611. -#ifdef CONFIG_RCU_BOOST
  16612. -
  16613. -static void rcu_preempt_do_callbacks(void)
  16614. -{
  16615. - rcu_do_batch(&rcu_preempt_state, this_cpu_ptr(&rcu_preempt_data));
  16616. -}
  16617. -
  16618. -#endif /* #ifdef CONFIG_RCU_BOOST */
  16619. -
  16620. /*
  16621. * Queue a preemptible-RCU callback for invocation after a grace period.
  16622. */
  16623. @@ -939,6 +923,19 @@
  16624. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  16625. +/*
  16626. + * If boosting, set rcuc kthreads to realtime priority.
  16627. + */
  16628. +static void rcu_cpu_kthread_setup(unsigned int cpu)
  16629. +{
  16630. +#ifdef CONFIG_RCU_BOOST
  16631. + struct sched_param sp;
  16632. +
  16633. + sp.sched_priority = kthread_prio;
  16634. + sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  16635. +#endif /* #ifdef CONFIG_RCU_BOOST */
  16636. +}
  16637. +
  16638. #ifdef CONFIG_RCU_BOOST
  16639. #include "../locking/rtmutex_common.h"
  16640. @@ -970,16 +967,6 @@
  16641. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  16642. -static void rcu_wake_cond(struct task_struct *t, int status)
  16643. -{
  16644. - /*
  16645. - * If the thread is yielding, only wake it when this
  16646. - * is invoked from idle
  16647. - */
  16648. - if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
  16649. - wake_up_process(t);
  16650. -}
  16651. -
  16652. /*
  16653. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  16654. * or ->boost_tasks, advancing the pointer to the next task in the
  16655. @@ -1125,23 +1112,6 @@
  16656. }
  16657. /*
  16658. - * Wake up the per-CPU kthread to invoke RCU callbacks.
  16659. - */
  16660. -static void invoke_rcu_callbacks_kthread(void)
  16661. -{
  16662. - unsigned long flags;
  16663. -
  16664. - local_irq_save(flags);
  16665. - __this_cpu_write(rcu_cpu_has_work, 1);
  16666. - if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
  16667. - current != __this_cpu_read(rcu_cpu_kthread_task)) {
  16668. - rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
  16669. - __this_cpu_read(rcu_cpu_kthread_status));
  16670. - }
  16671. - local_irq_restore(flags);
  16672. -}
  16673. -
  16674. -/*
  16675. * Is the current CPU running the RCU-callbacks kthread?
  16676. * Caller must have preemption disabled.
  16677. */
  16678. @@ -1196,67 +1166,6 @@
  16679. return 0;
  16680. }
  16681. -static void rcu_kthread_do_work(void)
  16682. -{
  16683. - rcu_do_batch(&rcu_sched_state, this_cpu_ptr(&rcu_sched_data));
  16684. - rcu_do_batch(&rcu_bh_state, this_cpu_ptr(&rcu_bh_data));
  16685. - rcu_preempt_do_callbacks();
  16686. -}
  16687. -
  16688. -static void rcu_cpu_kthread_setup(unsigned int cpu)
  16689. -{
  16690. - struct sched_param sp;
  16691. -
  16692. - sp.sched_priority = kthread_prio;
  16693. - sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  16694. -}
  16695. -
  16696. -static void rcu_cpu_kthread_park(unsigned int cpu)
  16697. -{
  16698. - per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  16699. -}
  16700. -
  16701. -static int rcu_cpu_kthread_should_run(unsigned int cpu)
  16702. -{
  16703. - return __this_cpu_read(rcu_cpu_has_work);
  16704. -}
  16705. -
  16706. -/*
  16707. - * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  16708. - * RCU softirq used in flavors and configurations of RCU that do not
  16709. - * support RCU priority boosting.
  16710. - */
  16711. -static void rcu_cpu_kthread(unsigned int cpu)
  16712. -{
  16713. - unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  16714. - char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  16715. - int spincnt;
  16716. -
  16717. - for (spincnt = 0; spincnt < 10; spincnt++) {
  16718. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  16719. - local_bh_disable();
  16720. - *statusp = RCU_KTHREAD_RUNNING;
  16721. - this_cpu_inc(rcu_cpu_kthread_loops);
  16722. - local_irq_disable();
  16723. - work = *workp;
  16724. - *workp = 0;
  16725. - local_irq_enable();
  16726. - if (work)
  16727. - rcu_kthread_do_work();
  16728. - local_bh_enable();
  16729. - if (*workp == 0) {
  16730. - trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  16731. - *statusp = RCU_KTHREAD_WAITING;
  16732. - return;
  16733. - }
  16734. - }
  16735. - *statusp = RCU_KTHREAD_YIELDING;
  16736. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  16737. - schedule_timeout_interruptible(2);
  16738. - trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  16739. - *statusp = RCU_KTHREAD_WAITING;
  16740. -}
  16741. -
  16742. /*
  16743. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  16744. * served by the rcu_node in question. The CPU hotplug lock is still
  16745. @@ -1286,26 +1195,12 @@
  16746. free_cpumask_var(cm);
  16747. }
  16748. -static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  16749. - .store = &rcu_cpu_kthread_task,
  16750. - .thread_should_run = rcu_cpu_kthread_should_run,
  16751. - .thread_fn = rcu_cpu_kthread,
  16752. - .thread_comm = "rcuc/%u",
  16753. - .setup = rcu_cpu_kthread_setup,
  16754. - .park = rcu_cpu_kthread_park,
  16755. -};
  16756. -
  16757. /*
  16758. * Spawn boost kthreads -- called as soon as the scheduler is running.
  16759. */
  16760. static void __init rcu_spawn_boost_kthreads(void)
  16761. {
  16762. struct rcu_node *rnp;
  16763. - int cpu;
  16764. -
  16765. - for_each_possible_cpu(cpu)
  16766. - per_cpu(rcu_cpu_has_work, cpu) = 0;
  16767. - BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  16768. rcu_for_each_leaf_node(rcu_state_p, rnp)
  16769. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  16770. }
  16771. @@ -1328,11 +1223,6 @@
  16772. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  16773. }
  16774. -static void invoke_rcu_callbacks_kthread(void)
  16775. -{
  16776. - WARN_ON_ONCE(1);
  16777. -}
  16778. -
  16779. static bool rcu_is_callbacks_kthread(void)
  16780. {
  16781. return false;
  16782. @@ -1356,7 +1246,7 @@
  16783. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  16784. -#if !defined(CONFIG_RCU_FAST_NO_HZ)
  16785. +#if !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL)
  16786. /*
  16787. * Check to see if any future RCU-related work will need to be done
  16788. @@ -1374,7 +1264,9 @@
  16789. return rcu_cpu_has_callbacks(NULL);
  16790. }
  16791. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  16792. +#endif /* !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL) */
  16793. +#if !defined(CONFIG_RCU_FAST_NO_HZ)
  16794. /*
  16795. * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
  16796. * after it.
  16797. @@ -1472,6 +1364,8 @@
  16798. return cbs_ready;
  16799. }
  16800. +#ifndef CONFIG_PREEMPT_RT_FULL
  16801. +
  16802. /*
  16803. * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
  16804. * to invoke. If the CPU has callbacks, try to advance them. Tell the
  16805. @@ -1512,7 +1406,7 @@
  16806. return 0;
  16807. }
  16808. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  16809. -
  16810. +#endif /* #ifndef CONFIG_PREEMPT_RT_FULL */
  16811. /*
  16812. * Prepare a CPU for idle from an RCU perspective. The first major task
  16813. * is to sense whether nohz mode has been enabled or disabled via sysfs.
  16814. @@ -1859,7 +1753,7 @@
  16815. */
  16816. static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  16817. {
  16818. - wake_up_all(&rnp->nocb_gp_wq[rnp->completed & 0x1]);
  16819. + swait_wake_all(&rnp->nocb_gp_wq[rnp->completed & 0x1]);
  16820. }
  16821. /*
  16822. @@ -1877,8 +1771,8 @@
  16823. static void rcu_init_one_nocb(struct rcu_node *rnp)
  16824. {
  16825. - init_waitqueue_head(&rnp->nocb_gp_wq[0]);
  16826. - init_waitqueue_head(&rnp->nocb_gp_wq[1]);
  16827. + init_swait_head(&rnp->nocb_gp_wq[0]);
  16828. + init_swait_head(&rnp->nocb_gp_wq[1]);
  16829. }
  16830. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  16831. @@ -1903,7 +1797,7 @@
  16832. if (ACCESS_ONCE(rdp_leader->nocb_leader_sleep) || force) {
  16833. /* Prior smp_mb__after_atomic() orders against prior enqueue. */
  16834. ACCESS_ONCE(rdp_leader->nocb_leader_sleep) = false;
  16835. - wake_up(&rdp_leader->nocb_wq);
  16836. + swait_wake(&rdp_leader->nocb_wq);
  16837. }
  16838. }
  16839. @@ -2116,7 +2010,7 @@
  16840. */
  16841. trace_rcu_future_gp(rnp, rdp, c, TPS("StartWait"));
  16842. for (;;) {
  16843. - wait_event_interruptible(
  16844. + swait_event_interruptible(
  16845. rnp->nocb_gp_wq[c & 0x1],
  16846. (d = ULONG_CMP_GE(ACCESS_ONCE(rnp->completed), c)));
  16847. if (likely(d))
  16848. @@ -2144,7 +2038,7 @@
  16849. /* Wait for callbacks to appear. */
  16850. if (!rcu_nocb_poll) {
  16851. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, "Sleep");
  16852. - wait_event_interruptible(my_rdp->nocb_wq,
  16853. + swait_event_interruptible(my_rdp->nocb_wq,
  16854. !ACCESS_ONCE(my_rdp->nocb_leader_sleep));
  16855. /* Memory barrier handled by smp_mb() calls below and repoll. */
  16856. } else if (firsttime) {
  16857. @@ -2219,7 +2113,7 @@
  16858. * List was empty, wake up the follower.
  16859. * Memory barriers supplied by atomic_long_add().
  16860. */
  16861. - wake_up(&rdp->nocb_wq);
  16862. + swait_wake(&rdp->nocb_wq);
  16863. }
  16864. }
  16865. @@ -2240,7 +2134,7 @@
  16866. if (!rcu_nocb_poll) {
  16867. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  16868. "FollowerSleep");
  16869. - wait_event_interruptible(rdp->nocb_wq,
  16870. + swait_event_interruptible(rdp->nocb_wq,
  16871. ACCESS_ONCE(rdp->nocb_follower_head));
  16872. } else if (firsttime) {
  16873. /* Don't drown trace log with "Poll"! */
  16874. @@ -2399,7 +2293,7 @@
  16875. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  16876. {
  16877. rdp->nocb_tail = &rdp->nocb_head;
  16878. - init_waitqueue_head(&rdp->nocb_wq);
  16879. + init_swait_head(&rdp->nocb_wq);
  16880. rdp->nocb_follower_tail = &rdp->nocb_follower_head;
  16881. }
  16882. diff -Nur linux-4.1.13.orig/kernel/rcu/update.c linux-4.1.13/kernel/rcu/update.c
  16883. --- linux-4.1.13.orig/kernel/rcu/update.c 2015-11-09 23:34:10.000000000 +0100
  16884. +++ linux-4.1.13/kernel/rcu/update.c 2015-11-29 09:23:09.641612055 +0100
  16885. @@ -227,6 +227,7 @@
  16886. }
  16887. EXPORT_SYMBOL_GPL(rcu_read_lock_held);
  16888. +#ifndef CONFIG_PREEMPT_RT_FULL
  16889. /**
  16890. * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
  16891. *
  16892. @@ -253,6 +254,7 @@
  16893. return in_softirq() || irqs_disabled();
  16894. }
  16895. EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
  16896. +#endif
  16897. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  16898. diff -Nur linux-4.1.13.orig/kernel/relay.c linux-4.1.13/kernel/relay.c
  16899. --- linux-4.1.13.orig/kernel/relay.c 2015-11-09 23:34:10.000000000 +0100
  16900. +++ linux-4.1.13/kernel/relay.c 2015-11-29 09:23:09.641612055 +0100
  16901. @@ -339,6 +339,10 @@
  16902. {
  16903. struct rchan_buf *buf = (struct rchan_buf *)data;
  16904. wake_up_interruptible(&buf->read_wait);
  16905. + /*
  16906. + * Stupid polling for now:
  16907. + */
  16908. + mod_timer(&buf->timer, jiffies + 1);
  16909. }
  16910. /**
  16911. @@ -356,6 +360,7 @@
  16912. init_waitqueue_head(&buf->read_wait);
  16913. kref_init(&buf->kref);
  16914. setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
  16915. + mod_timer(&buf->timer, jiffies + 1);
  16916. } else
  16917. del_timer_sync(&buf->timer);
  16918. @@ -739,15 +744,6 @@
  16919. else
  16920. buf->early_bytes += buf->chan->subbuf_size -
  16921. buf->padding[old_subbuf];
  16922. - smp_mb();
  16923. - if (waitqueue_active(&buf->read_wait))
  16924. - /*
  16925. - * Calling wake_up_interruptible() from here
  16926. - * will deadlock if we happen to be logging
  16927. - * from the scheduler (trying to re-grab
  16928. - * rq->lock), so defer it.
  16929. - */
  16930. - mod_timer(&buf->timer, jiffies + 1);
  16931. }
  16932. old = buf->data;
  16933. diff -Nur linux-4.1.13.orig/kernel/sched/completion.c linux-4.1.13/kernel/sched/completion.c
  16934. --- linux-4.1.13.orig/kernel/sched/completion.c 2015-11-09 23:34:10.000000000 +0100
  16935. +++ linux-4.1.13/kernel/sched/completion.c 2015-11-29 09:23:09.641612055 +0100
  16936. @@ -30,10 +30,10 @@
  16937. {
  16938. unsigned long flags;
  16939. - spin_lock_irqsave(&x->wait.lock, flags);
  16940. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  16941. x->done++;
  16942. - __wake_up_locked(&x->wait, TASK_NORMAL, 1);
  16943. - spin_unlock_irqrestore(&x->wait.lock, flags);
  16944. + __swait_wake_locked(&x->wait, TASK_NORMAL, 1);
  16945. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  16946. }
  16947. EXPORT_SYMBOL(complete);
  16948. @@ -50,10 +50,10 @@
  16949. {
  16950. unsigned long flags;
  16951. - spin_lock_irqsave(&x->wait.lock, flags);
  16952. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  16953. x->done += UINT_MAX/2;
  16954. - __wake_up_locked(&x->wait, TASK_NORMAL, 0);
  16955. - spin_unlock_irqrestore(&x->wait.lock, flags);
  16956. + __swait_wake_locked(&x->wait, TASK_NORMAL, 0);
  16957. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  16958. }
  16959. EXPORT_SYMBOL(complete_all);
  16960. @@ -62,20 +62,20 @@
  16961. long (*action)(long), long timeout, int state)
  16962. {
  16963. if (!x->done) {
  16964. - DECLARE_WAITQUEUE(wait, current);
  16965. + DEFINE_SWAITER(wait);
  16966. - __add_wait_queue_tail_exclusive(&x->wait, &wait);
  16967. + swait_prepare_locked(&x->wait, &wait);
  16968. do {
  16969. if (signal_pending_state(state, current)) {
  16970. timeout = -ERESTARTSYS;
  16971. break;
  16972. }
  16973. __set_current_state(state);
  16974. - spin_unlock_irq(&x->wait.lock);
  16975. + raw_spin_unlock_irq(&x->wait.lock);
  16976. timeout = action(timeout);
  16977. - spin_lock_irq(&x->wait.lock);
  16978. + raw_spin_lock_irq(&x->wait.lock);
  16979. } while (!x->done && timeout);
  16980. - __remove_wait_queue(&x->wait, &wait);
  16981. + swait_finish_locked(&x->wait, &wait);
  16982. if (!x->done)
  16983. return timeout;
  16984. }
  16985. @@ -89,9 +89,9 @@
  16986. {
  16987. might_sleep();
  16988. - spin_lock_irq(&x->wait.lock);
  16989. + raw_spin_lock_irq(&x->wait.lock);
  16990. timeout = do_wait_for_common(x, action, timeout, state);
  16991. - spin_unlock_irq(&x->wait.lock);
  16992. + raw_spin_unlock_irq(&x->wait.lock);
  16993. return timeout;
  16994. }
  16995. @@ -277,12 +277,12 @@
  16996. if (!READ_ONCE(x->done))
  16997. return 0;
  16998. - spin_lock_irqsave(&x->wait.lock, flags);
  16999. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  17000. if (!x->done)
  17001. ret = 0;
  17002. else
  17003. x->done--;
  17004. - spin_unlock_irqrestore(&x->wait.lock, flags);
  17005. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  17006. return ret;
  17007. }
  17008. EXPORT_SYMBOL(try_wait_for_completion);
  17009. @@ -311,7 +311,7 @@
  17010. * after it's acquired the lock.
  17011. */
  17012. smp_rmb();
  17013. - spin_unlock_wait(&x->wait.lock);
  17014. + raw_spin_unlock_wait(&x->wait.lock);
  17015. return true;
  17016. }
  17017. EXPORT_SYMBOL(completion_done);
  17018. diff -Nur linux-4.1.13.orig/kernel/sched/core.c linux-4.1.13/kernel/sched/core.c
  17019. --- linux-4.1.13.orig/kernel/sched/core.c 2015-11-09 23:34:10.000000000 +0100
  17020. +++ linux-4.1.13/kernel/sched/core.c 2015-11-29 09:23:09.645611789 +0100
  17021. @@ -282,7 +282,11 @@
  17022. * Number of tasks to iterate in a single balance run.
  17023. * Limited because this is done with IRQs disabled.
  17024. */
  17025. +#ifndef CONFIG_PREEMPT_RT_FULL
  17026. const_debug unsigned int sysctl_sched_nr_migrate = 32;
  17027. +#else
  17028. +const_debug unsigned int sysctl_sched_nr_migrate = 8;
  17029. +#endif
  17030. /*
  17031. * period over which we average the RT time consumption, measured
  17032. @@ -461,6 +465,7 @@
  17033. hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  17034. rq->hrtick_timer.function = hrtick;
  17035. + rq->hrtick_timer.irqsafe = 1;
  17036. }
  17037. #else /* CONFIG_SCHED_HRTICK */
  17038. static inline void hrtick_clear(struct rq *rq)
  17039. @@ -541,6 +546,52 @@
  17040. #endif
  17041. #endif
  17042. +void wake_q_add(struct wake_q_head *head, struct task_struct *task)
  17043. +{
  17044. + struct wake_q_node *node = &task->wake_q;
  17045. +
  17046. + /*
  17047. + * Atomically grab the task, if ->wake_q is !nil already it means
  17048. + * its already queued (either by us or someone else) and will get the
  17049. + * wakeup due to that.
  17050. + *
  17051. + * This cmpxchg() implies a full barrier, which pairs with the write
  17052. + * barrier implied by the wakeup in wake_up_list().
  17053. + */
  17054. + if (cmpxchg(&node->next, NULL, WAKE_Q_TAIL))
  17055. + return;
  17056. +
  17057. + get_task_struct(task);
  17058. +
  17059. + /*
  17060. + * The head is context local, there can be no concurrency.
  17061. + */
  17062. + *head->lastp = node;
  17063. + head->lastp = &node->next;
  17064. +}
  17065. +
  17066. +void wake_up_q(struct wake_q_head *head)
  17067. +{
  17068. + struct wake_q_node *node = head->first;
  17069. +
  17070. + while (node != WAKE_Q_TAIL) {
  17071. + struct task_struct *task;
  17072. +
  17073. + task = container_of(node, struct task_struct, wake_q);
  17074. + BUG_ON(!task);
  17075. + /* task can safely be re-inserted now */
  17076. + node = node->next;
  17077. + task->wake_q.next = NULL;
  17078. +
  17079. + /*
  17080. + * wake_up_process() implies a wmb() to pair with the queueing
  17081. + * in wake_q_add() so as not to miss wakeups.
  17082. + */
  17083. + wake_up_process(task);
  17084. + put_task_struct(task);
  17085. + }
  17086. +}
  17087. +
  17088. /*
  17089. * resched_curr - mark rq's current task 'to be rescheduled now'.
  17090. *
  17091. @@ -572,6 +623,38 @@
  17092. trace_sched_wake_idle_without_ipi(cpu);
  17093. }
  17094. +#ifdef CONFIG_PREEMPT_LAZY
  17095. +void resched_curr_lazy(struct rq *rq)
  17096. +{
  17097. + struct task_struct *curr = rq->curr;
  17098. + int cpu;
  17099. +
  17100. + if (!sched_feat(PREEMPT_LAZY)) {
  17101. + resched_curr(rq);
  17102. + return;
  17103. + }
  17104. +
  17105. + lockdep_assert_held(&rq->lock);
  17106. +
  17107. + if (test_tsk_need_resched(curr))
  17108. + return;
  17109. +
  17110. + if (test_tsk_need_resched_lazy(curr))
  17111. + return;
  17112. +
  17113. + set_tsk_need_resched_lazy(curr);
  17114. +
  17115. + cpu = cpu_of(rq);
  17116. + if (cpu == smp_processor_id())
  17117. + return;
  17118. +
  17119. + /* NEED_RESCHED_LAZY must be visible before we test polling */
  17120. + smp_mb();
  17121. + if (!tsk_is_polling(curr))
  17122. + smp_send_reschedule(cpu);
  17123. +}
  17124. +#endif
  17125. +
  17126. void resched_cpu(int cpu)
  17127. {
  17128. struct rq *rq = cpu_rq(cpu);
  17129. @@ -595,12 +678,14 @@
  17130. */
  17131. int get_nohz_timer_target(int pinned)
  17132. {
  17133. - int cpu = smp_processor_id();
  17134. + int cpu;
  17135. int i;
  17136. struct sched_domain *sd;
  17137. + preempt_disable_rt();
  17138. + cpu = smp_processor_id();
  17139. if (pinned || !get_sysctl_timer_migration() || !idle_cpu(cpu))
  17140. - return cpu;
  17141. + goto preempt_en_rt;
  17142. rcu_read_lock();
  17143. for_each_domain(cpu, sd) {
  17144. @@ -613,6 +698,8 @@
  17145. }
  17146. unlock:
  17147. rcu_read_unlock();
  17148. +preempt_en_rt:
  17149. + preempt_enable_rt();
  17150. return cpu;
  17151. }
  17152. /*
  17153. @@ -1164,6 +1251,18 @@
  17154. static int migration_cpu_stop(void *data);
  17155. +static bool check_task_state(struct task_struct *p, long match_state)
  17156. +{
  17157. + bool match = false;
  17158. +
  17159. + raw_spin_lock_irq(&p->pi_lock);
  17160. + if (p->state == match_state || p->saved_state == match_state)
  17161. + match = true;
  17162. + raw_spin_unlock_irq(&p->pi_lock);
  17163. +
  17164. + return match;
  17165. +}
  17166. +
  17167. /*
  17168. * wait_task_inactive - wait for a thread to unschedule.
  17169. *
  17170. @@ -1208,7 +1307,7 @@
  17171. * is actually now running somewhere else!
  17172. */
  17173. while (task_running(rq, p)) {
  17174. - if (match_state && unlikely(p->state != match_state))
  17175. + if (match_state && !check_task_state(p, match_state))
  17176. return 0;
  17177. cpu_relax();
  17178. }
  17179. @@ -1223,7 +1322,8 @@
  17180. running = task_running(rq, p);
  17181. queued = task_on_rq_queued(p);
  17182. ncsw = 0;
  17183. - if (!match_state || p->state == match_state)
  17184. + if (!match_state || p->state == match_state ||
  17185. + p->saved_state == match_state)
  17186. ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
  17187. task_rq_unlock(rq, p, &flags);
  17188. @@ -1449,10 +1549,6 @@
  17189. {
  17190. activate_task(rq, p, en_flags);
  17191. p->on_rq = TASK_ON_RQ_QUEUED;
  17192. -
  17193. - /* if a worker is waking up, notify workqueue */
  17194. - if (p->flags & PF_WQ_WORKER)
  17195. - wq_worker_waking_up(p, cpu_of(rq));
  17196. }
  17197. /*
  17198. @@ -1462,9 +1558,9 @@
  17199. ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
  17200. {
  17201. check_preempt_curr(rq, p, wake_flags);
  17202. - trace_sched_wakeup(p, true);
  17203. -
  17204. p->state = TASK_RUNNING;
  17205. + trace_sched_wakeup(p);
  17206. +
  17207. #ifdef CONFIG_SMP
  17208. if (p->sched_class->task_woken)
  17209. p->sched_class->task_woken(rq, p);
  17210. @@ -1666,8 +1762,29 @@
  17211. */
  17212. smp_mb__before_spinlock();
  17213. raw_spin_lock_irqsave(&p->pi_lock, flags);
  17214. - if (!(p->state & state))
  17215. + if (!(p->state & state)) {
  17216. + /*
  17217. + * The task might be running due to a spinlock sleeper
  17218. + * wakeup. Check the saved state and set it to running
  17219. + * if the wakeup condition is true.
  17220. + */
  17221. + if (!(wake_flags & WF_LOCK_SLEEPER)) {
  17222. + if (p->saved_state & state) {
  17223. + p->saved_state = TASK_RUNNING;
  17224. + success = 1;
  17225. + }
  17226. + }
  17227. goto out;
  17228. + }
  17229. +
  17230. + /*
  17231. + * If this is a regular wakeup, then we can unconditionally
  17232. + * clear the saved state of a "lock sleeper".
  17233. + */
  17234. + if (!(wake_flags & WF_LOCK_SLEEPER))
  17235. + p->saved_state = TASK_RUNNING;
  17236. +
  17237. + trace_sched_waking(p);
  17238. success = 1; /* we're going to change ->state */
  17239. cpu = task_cpu(p);
  17240. @@ -1710,42 +1827,6 @@
  17241. }
  17242. /**
  17243. - * try_to_wake_up_local - try to wake up a local task with rq lock held
  17244. - * @p: the thread to be awakened
  17245. - *
  17246. - * Put @p on the run-queue if it's not already there. The caller must
  17247. - * ensure that this_rq() is locked, @p is bound to this_rq() and not
  17248. - * the current task.
  17249. - */
  17250. -static void try_to_wake_up_local(struct task_struct *p)
  17251. -{
  17252. - struct rq *rq = task_rq(p);
  17253. -
  17254. - if (WARN_ON_ONCE(rq != this_rq()) ||
  17255. - WARN_ON_ONCE(p == current))
  17256. - return;
  17257. -
  17258. - lockdep_assert_held(&rq->lock);
  17259. -
  17260. - if (!raw_spin_trylock(&p->pi_lock)) {
  17261. - raw_spin_unlock(&rq->lock);
  17262. - raw_spin_lock(&p->pi_lock);
  17263. - raw_spin_lock(&rq->lock);
  17264. - }
  17265. -
  17266. - if (!(p->state & TASK_NORMAL))
  17267. - goto out;
  17268. -
  17269. - if (!task_on_rq_queued(p))
  17270. - ttwu_activate(rq, p, ENQUEUE_WAKEUP);
  17271. -
  17272. - ttwu_do_wakeup(rq, p, 0);
  17273. - ttwu_stat(p, smp_processor_id(), 0);
  17274. -out:
  17275. - raw_spin_unlock(&p->pi_lock);
  17276. -}
  17277. -
  17278. -/**
  17279. * wake_up_process - Wake up a specific process
  17280. * @p: The process to be woken up.
  17281. *
  17282. @@ -1759,11 +1840,23 @@
  17283. */
  17284. int wake_up_process(struct task_struct *p)
  17285. {
  17286. - WARN_ON(task_is_stopped_or_traced(p));
  17287. + WARN_ON(__task_is_stopped_or_traced(p));
  17288. return try_to_wake_up(p, TASK_NORMAL, 0);
  17289. }
  17290. EXPORT_SYMBOL(wake_up_process);
  17291. +/**
  17292. + * wake_up_lock_sleeper - Wake up a specific process blocked on a "sleeping lock"
  17293. + * @p: The process to be woken up.
  17294. + *
  17295. + * Same as wake_up_process() above, but wake_flags=WF_LOCK_SLEEPER to indicate
  17296. + * the nature of the wakeup.
  17297. + */
  17298. +int wake_up_lock_sleeper(struct task_struct *p)
  17299. +{
  17300. + return try_to_wake_up(p, TASK_ALL, WF_LOCK_SLEEPER);
  17301. +}
  17302. +
  17303. int wake_up_state(struct task_struct *p, unsigned int state)
  17304. {
  17305. return try_to_wake_up(p, state, 0);
  17306. @@ -1959,6 +2052,9 @@
  17307. p->on_cpu = 0;
  17308. #endif
  17309. init_task_preempt_count(p);
  17310. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  17311. + task_thread_info(p)->preempt_lazy_count = 0;
  17312. +#endif
  17313. #ifdef CONFIG_SMP
  17314. plist_node_init(&p->pushable_tasks, MAX_PRIO);
  17315. RB_CLEAR_NODE(&p->pushable_dl_tasks);
  17316. @@ -2094,7 +2190,7 @@
  17317. rq = __task_rq_lock(p);
  17318. activate_task(rq, p, 0);
  17319. p->on_rq = TASK_ON_RQ_QUEUED;
  17320. - trace_sched_wakeup_new(p, true);
  17321. + trace_sched_wakeup_new(p);
  17322. check_preempt_curr(rq, p, WF_FORK);
  17323. #ifdef CONFIG_SMP
  17324. if (p->sched_class->task_woken)
  17325. @@ -2231,8 +2327,12 @@
  17326. finish_arch_post_lock_switch();
  17327. fire_sched_in_preempt_notifiers(current);
  17328. + /*
  17329. + * We use mmdrop_delayed() here so we don't have to do the
  17330. + * full __mmdrop() when we are the last user.
  17331. + */
  17332. if (mm)
  17333. - mmdrop(mm);
  17334. + mmdrop_delayed(mm);
  17335. if (unlikely(prev_state == TASK_DEAD)) {
  17336. if (prev->sched_class->task_dead)
  17337. prev->sched_class->task_dead(prev);
  17338. @@ -2657,6 +2757,133 @@
  17339. schedstat_inc(this_rq(), sched_count);
  17340. }
  17341. +#if defined(CONFIG_PREEMPT_RT_FULL) && defined(CONFIG_SMP)
  17342. +#define MIGRATE_DISABLE_SET_AFFIN (1<<30) /* Can't make a negative */
  17343. +#define migrate_disabled_updated(p) ((p)->migrate_disable & MIGRATE_DISABLE_SET_AFFIN)
  17344. +#define migrate_disable_count(p) ((p)->migrate_disable & ~MIGRATE_DISABLE_SET_AFFIN)
  17345. +
  17346. +static inline void update_migrate_disable(struct task_struct *p)
  17347. +{
  17348. + const struct cpumask *mask;
  17349. +
  17350. + if (likely(!p->migrate_disable))
  17351. + return;
  17352. +
  17353. + /* Did we already update affinity? */
  17354. + if (unlikely(migrate_disabled_updated(p)))
  17355. + return;
  17356. +
  17357. + /*
  17358. + * Since this is always current we can get away with only locking
  17359. + * rq->lock, the ->cpus_allowed value can normally only be changed
  17360. + * while holding both p->pi_lock and rq->lock, but seeing that this
  17361. + * is current, we cannot actually be waking up, so all code that
  17362. + * relies on serialization against p->pi_lock is out of scope.
  17363. + *
  17364. + * Having rq->lock serializes us against things like
  17365. + * set_cpus_allowed_ptr() that can still happen concurrently.
  17366. + */
  17367. + mask = tsk_cpus_allowed(p);
  17368. +
  17369. + if (p->sched_class->set_cpus_allowed)
  17370. + p->sched_class->set_cpus_allowed(p, mask);
  17371. + /* mask==cpumask_of(task_cpu(p)) which has a cpumask_weight==1 */
  17372. + p->nr_cpus_allowed = 1;
  17373. +
  17374. + /* Let migrate_enable know to fix things back up */
  17375. + p->migrate_disable |= MIGRATE_DISABLE_SET_AFFIN;
  17376. +}
  17377. +
  17378. +void migrate_disable(void)
  17379. +{
  17380. + struct task_struct *p = current;
  17381. +
  17382. + if (in_atomic()) {
  17383. +#ifdef CONFIG_SCHED_DEBUG
  17384. + p->migrate_disable_atomic++;
  17385. +#endif
  17386. + return;
  17387. + }
  17388. +
  17389. +#ifdef CONFIG_SCHED_DEBUG
  17390. + if (unlikely(p->migrate_disable_atomic)) {
  17391. + tracing_off();
  17392. + WARN_ON_ONCE(1);
  17393. + }
  17394. +#endif
  17395. +
  17396. + if (p->migrate_disable) {
  17397. + p->migrate_disable++;
  17398. + return;
  17399. + }
  17400. +
  17401. + preempt_disable();
  17402. + preempt_lazy_disable();
  17403. + pin_current_cpu();
  17404. + p->migrate_disable = 1;
  17405. + preempt_enable();
  17406. +}
  17407. +EXPORT_SYMBOL(migrate_disable);
  17408. +
  17409. +void migrate_enable(void)
  17410. +{
  17411. + struct task_struct *p = current;
  17412. + const struct cpumask *mask;
  17413. + unsigned long flags;
  17414. + struct rq *rq;
  17415. +
  17416. + if (in_atomic()) {
  17417. +#ifdef CONFIG_SCHED_DEBUG
  17418. + p->migrate_disable_atomic--;
  17419. +#endif
  17420. + return;
  17421. + }
  17422. +
  17423. +#ifdef CONFIG_SCHED_DEBUG
  17424. + if (unlikely(p->migrate_disable_atomic)) {
  17425. + tracing_off();
  17426. + WARN_ON_ONCE(1);
  17427. + }
  17428. +#endif
  17429. + WARN_ON_ONCE(p->migrate_disable <= 0);
  17430. +
  17431. + if (migrate_disable_count(p) > 1) {
  17432. + p->migrate_disable--;
  17433. + return;
  17434. + }
  17435. +
  17436. + preempt_disable();
  17437. + if (unlikely(migrate_disabled_updated(p))) {
  17438. + /*
  17439. + * Undo whatever update_migrate_disable() did, also see there
  17440. + * about locking.
  17441. + */
  17442. + rq = this_rq();
  17443. + raw_spin_lock_irqsave(&rq->lock, flags);
  17444. +
  17445. + /*
  17446. + * Clearing migrate_disable causes tsk_cpus_allowed to
  17447. + * show the tasks original cpu affinity.
  17448. + */
  17449. + p->migrate_disable = 0;
  17450. + mask = tsk_cpus_allowed(p);
  17451. + if (p->sched_class->set_cpus_allowed)
  17452. + p->sched_class->set_cpus_allowed(p, mask);
  17453. + p->nr_cpus_allowed = cpumask_weight(mask);
  17454. + raw_spin_unlock_irqrestore(&rq->lock, flags);
  17455. + } else
  17456. + p->migrate_disable = 0;
  17457. +
  17458. + unpin_current_cpu();
  17459. + preempt_enable();
  17460. + preempt_lazy_enable();
  17461. +}
  17462. +EXPORT_SYMBOL(migrate_enable);
  17463. +#else
  17464. +static inline void update_migrate_disable(struct task_struct *p) { }
  17465. +#define migrate_disabled_updated(p) 0
  17466. +#endif
  17467. +
  17468. /*
  17469. * Pick up the highest-prio task:
  17470. */
  17471. @@ -2763,6 +2990,8 @@
  17472. smp_mb__before_spinlock();
  17473. raw_spin_lock_irq(&rq->lock);
  17474. + update_migrate_disable(prev);
  17475. +
  17476. rq->clock_skip_update <<= 1; /* promote REQ to ACT */
  17477. switch_count = &prev->nivcsw;
  17478. @@ -2772,19 +3001,6 @@
  17479. } else {
  17480. deactivate_task(rq, prev, DEQUEUE_SLEEP);
  17481. prev->on_rq = 0;
  17482. -
  17483. - /*
  17484. - * If a worker went to sleep, notify and ask workqueue
  17485. - * whether it wants to wake up a task to maintain
  17486. - * concurrency.
  17487. - */
  17488. - if (prev->flags & PF_WQ_WORKER) {
  17489. - struct task_struct *to_wakeup;
  17490. -
  17491. - to_wakeup = wq_worker_sleeping(prev, cpu);
  17492. - if (to_wakeup)
  17493. - try_to_wake_up_local(to_wakeup);
  17494. - }
  17495. }
  17496. switch_count = &prev->nvcsw;
  17497. }
  17498. @@ -2794,6 +3010,7 @@
  17499. next = pick_next_task(rq, prev);
  17500. clear_tsk_need_resched(prev);
  17501. + clear_tsk_need_resched_lazy(prev);
  17502. clear_preempt_need_resched();
  17503. rq->clock_skip_update = 0;
  17504. @@ -2814,8 +3031,19 @@
  17505. static inline void sched_submit_work(struct task_struct *tsk)
  17506. {
  17507. - if (!tsk->state || tsk_is_pi_blocked(tsk))
  17508. + if (!tsk->state)
  17509. + return;
  17510. + /*
  17511. + * If a worker went to sleep, notify and ask workqueue whether
  17512. + * it wants to wake up a task to maintain concurrency.
  17513. + */
  17514. + if (tsk->flags & PF_WQ_WORKER)
  17515. + wq_worker_sleeping(tsk);
  17516. +
  17517. +
  17518. + if (tsk_is_pi_blocked(tsk))
  17519. return;
  17520. +
  17521. /*
  17522. * If we are going to sleep and we have plugged IO queued,
  17523. * make sure to submit it to avoid deadlocks.
  17524. @@ -2824,6 +3052,12 @@
  17525. blk_schedule_flush_plug(tsk);
  17526. }
  17527. +static void sched_update_worker(struct task_struct *tsk)
  17528. +{
  17529. + if (tsk->flags & PF_WQ_WORKER)
  17530. + wq_worker_running(tsk);
  17531. +}
  17532. +
  17533. asmlinkage __visible void __sched schedule(void)
  17534. {
  17535. struct task_struct *tsk = current;
  17536. @@ -2832,6 +3066,7 @@
  17537. do {
  17538. __schedule();
  17539. } while (need_resched());
  17540. + sched_update_worker(tsk);
  17541. }
  17542. EXPORT_SYMBOL(schedule);
  17543. @@ -2923,6 +3158,14 @@
  17544. if (likely(!preemptible()))
  17545. return;
  17546. +#ifdef CONFIG_PREEMPT_LAZY
  17547. + /*
  17548. + * Check for lazy preemption
  17549. + */
  17550. + if (current_thread_info()->preempt_lazy_count &&
  17551. + !test_thread_flag(TIF_NEED_RESCHED))
  17552. + return;
  17553. +#endif
  17554. do {
  17555. __preempt_count_add(PREEMPT_ACTIVE);
  17556. /*
  17557. @@ -2931,7 +3174,16 @@
  17558. * an infinite recursion.
  17559. */
  17560. prev_ctx = exception_enter();
  17561. + /*
  17562. + * The add/subtract must not be traced by the function
  17563. + * tracer. But we still want to account for the
  17564. + * preempt off latency tracer. Since the _notrace versions
  17565. + * of add/subtract skip the accounting for latency tracer
  17566. + * we must force it manually.
  17567. + */
  17568. + start_critical_timings();
  17569. __schedule();
  17570. + stop_critical_timings();
  17571. exception_exit(prev_ctx);
  17572. __preempt_count_sub(PREEMPT_ACTIVE);
  17573. @@ -4268,6 +4520,7 @@
  17574. }
  17575. EXPORT_SYMBOL(__cond_resched_lock);
  17576. +#ifndef CONFIG_PREEMPT_RT_FULL
  17577. int __sched __cond_resched_softirq(void)
  17578. {
  17579. BUG_ON(!in_softirq());
  17580. @@ -4281,6 +4534,7 @@
  17581. return 0;
  17582. }
  17583. EXPORT_SYMBOL(__cond_resched_softirq);
  17584. +#endif
  17585. /**
  17586. * yield - yield the current processor to other threads.
  17587. @@ -4635,7 +4889,9 @@
  17588. /* Set the preempt count _outside_ the spinlocks! */
  17589. init_idle_preempt_count(idle, cpu);
  17590. -
  17591. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  17592. + task_thread_info(idle)->preempt_lazy_count = 0;
  17593. +#endif
  17594. /*
  17595. * The idle tasks have their own, simple scheduling class:
  17596. */
  17597. @@ -4755,11 +5011,91 @@
  17598. void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  17599. {
  17600. - if (p->sched_class->set_cpus_allowed)
  17601. - p->sched_class->set_cpus_allowed(p, new_mask);
  17602. + if (!migrate_disabled_updated(p)) {
  17603. + if (p->sched_class->set_cpus_allowed)
  17604. + p->sched_class->set_cpus_allowed(p, new_mask);
  17605. + p->nr_cpus_allowed = cpumask_weight(new_mask);
  17606. + }
  17607. cpumask_copy(&p->cpus_allowed, new_mask);
  17608. - p->nr_cpus_allowed = cpumask_weight(new_mask);
  17609. +}
  17610. +
  17611. +static DEFINE_PER_CPU(struct cpumask, sched_cpumasks);
  17612. +static DEFINE_MUTEX(sched_down_mutex);
  17613. +static cpumask_t sched_down_cpumask;
  17614. +
  17615. +void tell_sched_cpu_down_begin(int cpu)
  17616. +{
  17617. + mutex_lock(&sched_down_mutex);
  17618. + cpumask_set_cpu(cpu, &sched_down_cpumask);
  17619. + mutex_unlock(&sched_down_mutex);
  17620. +}
  17621. +
  17622. +void tell_sched_cpu_down_done(int cpu)
  17623. +{
  17624. + mutex_lock(&sched_down_mutex);
  17625. + cpumask_clear_cpu(cpu, &sched_down_cpumask);
  17626. + mutex_unlock(&sched_down_mutex);
  17627. +}
  17628. +
  17629. +/**
  17630. + * migrate_me - try to move the current task off this cpu
  17631. + *
  17632. + * Used by the pin_current_cpu() code to try to get tasks
  17633. + * to move off the current CPU as it is going down.
  17634. + * It will only move the task if the task isn't pinned to
  17635. + * the CPU (with migrate_disable, affinity or NO_SETAFFINITY)
  17636. + * and the task has to be in a RUNNING state. Otherwise the
  17637. + * movement of the task will wake it up (change its state
  17638. + * to running) when the task did not expect it.
  17639. + *
  17640. + * Returns 1 if it succeeded in moving the current task
  17641. + * 0 otherwise.
  17642. + */
  17643. +int migrate_me(void)
  17644. +{
  17645. + struct task_struct *p = current;
  17646. + struct migration_arg arg;
  17647. + struct cpumask *cpumask;
  17648. + struct cpumask *mask;
  17649. + unsigned long flags;
  17650. + unsigned int dest_cpu;
  17651. + struct rq *rq;
  17652. +
  17653. + /*
  17654. + * We can not migrate tasks bounded to a CPU or tasks not
  17655. + * running. The movement of the task will wake it up.
  17656. + */
  17657. + if (p->flags & PF_NO_SETAFFINITY || p->state)
  17658. + return 0;
  17659. +
  17660. + mutex_lock(&sched_down_mutex);
  17661. + rq = task_rq_lock(p, &flags);
  17662. +
  17663. + cpumask = this_cpu_ptr(&sched_cpumasks);
  17664. + mask = &p->cpus_allowed;
  17665. +
  17666. + cpumask_andnot(cpumask, mask, &sched_down_cpumask);
  17667. +
  17668. + if (!cpumask_weight(cpumask)) {
  17669. + /* It's only on this CPU? */
  17670. + task_rq_unlock(rq, p, &flags);
  17671. + mutex_unlock(&sched_down_mutex);
  17672. + return 0;
  17673. + }
  17674. +
  17675. + dest_cpu = cpumask_any_and(cpu_active_mask, cpumask);
  17676. +
  17677. + arg.task = p;
  17678. + arg.dest_cpu = dest_cpu;
  17679. +
  17680. + task_rq_unlock(rq, p, &flags);
  17681. +
  17682. + stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
  17683. + tlb_migrate_finish(p->mm);
  17684. + mutex_unlock(&sched_down_mutex);
  17685. +
  17686. + return 1;
  17687. }
  17688. /*
  17689. @@ -4805,7 +5141,7 @@
  17690. do_set_cpus_allowed(p, new_mask);
  17691. /* Can the task run on the task's current CPU? If so, we're done */
  17692. - if (cpumask_test_cpu(task_cpu(p), new_mask))
  17693. + if (cpumask_test_cpu(task_cpu(p), new_mask) || __migrate_disabled(p))
  17694. goto out;
  17695. dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
  17696. @@ -4945,6 +5281,8 @@
  17697. #ifdef CONFIG_HOTPLUG_CPU
  17698. +static DEFINE_PER_CPU(struct mm_struct *, idle_last_mm);
  17699. +
  17700. /*
  17701. * Ensures that the idle task is using init_mm right before its cpu goes
  17702. * offline.
  17703. @@ -4959,7 +5297,11 @@
  17704. switch_mm(mm, &init_mm, current);
  17705. finish_arch_post_lock_switch();
  17706. }
  17707. - mmdrop(mm);
  17708. + /*
  17709. + * Defer the cleanup to an alive cpu. On RT we can neither
  17710. + * call mmdrop() nor mmdrop_delayed() from here.
  17711. + */
  17712. + per_cpu(idle_last_mm, smp_processor_id()) = mm;
  17713. }
  17714. /*
  17715. @@ -5302,6 +5644,10 @@
  17716. case CPU_DEAD:
  17717. calc_load_migrate(rq);
  17718. + if (per_cpu(idle_last_mm, cpu)) {
  17719. + mmdrop(per_cpu(idle_last_mm, cpu));
  17720. + per_cpu(idle_last_mm, cpu) = NULL;
  17721. + }
  17722. break;
  17723. #endif
  17724. }
  17725. @@ -7281,7 +7627,8 @@
  17726. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  17727. static inline int preempt_count_equals(int preempt_offset)
  17728. {
  17729. - int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
  17730. + int nested = (preempt_count() & ~PREEMPT_ACTIVE) +
  17731. + sched_rcu_preempt_depth();
  17732. return (nested == preempt_offset);
  17733. }
  17734. diff -Nur linux-4.1.13.orig/kernel/sched/cputime.c linux-4.1.13/kernel/sched/cputime.c
  17735. --- linux-4.1.13.orig/kernel/sched/cputime.c 2015-11-09 23:34:10.000000000 +0100
  17736. +++ linux-4.1.13/kernel/sched/cputime.c 2015-11-29 09:23:09.645611789 +0100
  17737. @@ -675,37 +675,45 @@
  17738. void vtime_account_system(struct task_struct *tsk)
  17739. {
  17740. - write_seqlock(&tsk->vtime_seqlock);
  17741. + raw_spin_lock(&tsk->vtime_lock);
  17742. + write_seqcount_begin(&tsk->vtime_seq);
  17743. __vtime_account_system(tsk);
  17744. - write_sequnlock(&tsk->vtime_seqlock);
  17745. + write_seqcount_end(&tsk->vtime_seq);
  17746. + raw_spin_unlock(&tsk->vtime_lock);
  17747. }
  17748. void vtime_gen_account_irq_exit(struct task_struct *tsk)
  17749. {
  17750. - write_seqlock(&tsk->vtime_seqlock);
  17751. + raw_spin_lock(&tsk->vtime_lock);
  17752. + write_seqcount_begin(&tsk->vtime_seq);
  17753. __vtime_account_system(tsk);
  17754. if (context_tracking_in_user())
  17755. tsk->vtime_snap_whence = VTIME_USER;
  17756. - write_sequnlock(&tsk->vtime_seqlock);
  17757. + write_seqcount_end(&tsk->vtime_seq);
  17758. + raw_spin_unlock(&tsk->vtime_lock);
  17759. }
  17760. void vtime_account_user(struct task_struct *tsk)
  17761. {
  17762. cputime_t delta_cpu;
  17763. - write_seqlock(&tsk->vtime_seqlock);
  17764. + raw_spin_lock(&tsk->vtime_lock);
  17765. + write_seqcount_begin(&tsk->vtime_seq);
  17766. delta_cpu = get_vtime_delta(tsk);
  17767. tsk->vtime_snap_whence = VTIME_SYS;
  17768. account_user_time(tsk, delta_cpu, cputime_to_scaled(delta_cpu));
  17769. - write_sequnlock(&tsk->vtime_seqlock);
  17770. + write_seqcount_end(&tsk->vtime_seq);
  17771. + raw_spin_unlock(&tsk->vtime_lock);
  17772. }
  17773. void vtime_user_enter(struct task_struct *tsk)
  17774. {
  17775. - write_seqlock(&tsk->vtime_seqlock);
  17776. + raw_spin_lock(&tsk->vtime_lock);
  17777. + write_seqcount_begin(&tsk->vtime_seq);
  17778. __vtime_account_system(tsk);
  17779. tsk->vtime_snap_whence = VTIME_USER;
  17780. - write_sequnlock(&tsk->vtime_seqlock);
  17781. + write_seqcount_end(&tsk->vtime_seq);
  17782. + raw_spin_unlock(&tsk->vtime_lock);
  17783. }
  17784. void vtime_guest_enter(struct task_struct *tsk)
  17785. @@ -717,19 +725,23 @@
  17786. * synchronization against the reader (task_gtime())
  17787. * that can thus safely catch up with a tickless delta.
  17788. */
  17789. - write_seqlock(&tsk->vtime_seqlock);
  17790. + raw_spin_lock(&tsk->vtime_lock);
  17791. + write_seqcount_begin(&tsk->vtime_seq);
  17792. __vtime_account_system(tsk);
  17793. current->flags |= PF_VCPU;
  17794. - write_sequnlock(&tsk->vtime_seqlock);
  17795. + write_seqcount_end(&tsk->vtime_seq);
  17796. + raw_spin_unlock(&tsk->vtime_lock);
  17797. }
  17798. EXPORT_SYMBOL_GPL(vtime_guest_enter);
  17799. void vtime_guest_exit(struct task_struct *tsk)
  17800. {
  17801. - write_seqlock(&tsk->vtime_seqlock);
  17802. + raw_spin_lock(&tsk->vtime_lock);
  17803. + write_seqcount_begin(&tsk->vtime_seq);
  17804. __vtime_account_system(tsk);
  17805. current->flags &= ~PF_VCPU;
  17806. - write_sequnlock(&tsk->vtime_seqlock);
  17807. + write_seqcount_end(&tsk->vtime_seq);
  17808. + raw_spin_unlock(&tsk->vtime_lock);
  17809. }
  17810. EXPORT_SYMBOL_GPL(vtime_guest_exit);
  17811. @@ -742,24 +754,30 @@
  17812. void arch_vtime_task_switch(struct task_struct *prev)
  17813. {
  17814. - write_seqlock(&prev->vtime_seqlock);
  17815. + raw_spin_lock(&prev->vtime_lock);
  17816. + write_seqcount_begin(&prev->vtime_seq);
  17817. prev->vtime_snap_whence = VTIME_SLEEPING;
  17818. - write_sequnlock(&prev->vtime_seqlock);
  17819. + write_seqcount_end(&prev->vtime_seq);
  17820. + raw_spin_unlock(&prev->vtime_lock);
  17821. - write_seqlock(&current->vtime_seqlock);
  17822. + raw_spin_lock(&current->vtime_lock);
  17823. + write_seqcount_begin(&current->vtime_seq);
  17824. current->vtime_snap_whence = VTIME_SYS;
  17825. current->vtime_snap = sched_clock_cpu(smp_processor_id());
  17826. - write_sequnlock(&current->vtime_seqlock);
  17827. + write_seqcount_end(&current->vtime_seq);
  17828. + raw_spin_unlock(&current->vtime_lock);
  17829. }
  17830. void vtime_init_idle(struct task_struct *t, int cpu)
  17831. {
  17832. unsigned long flags;
  17833. - write_seqlock_irqsave(&t->vtime_seqlock, flags);
  17834. + raw_spin_lock_irqsave(&t->vtime_lock, flags);
  17835. + write_seqcount_begin(&t->vtime_seq);
  17836. t->vtime_snap_whence = VTIME_SYS;
  17837. t->vtime_snap = sched_clock_cpu(cpu);
  17838. - write_sequnlock_irqrestore(&t->vtime_seqlock, flags);
  17839. + write_seqcount_end(&t->vtime_seq);
  17840. + raw_spin_unlock_irqrestore(&t->vtime_lock, flags);
  17841. }
  17842. cputime_t task_gtime(struct task_struct *t)
  17843. @@ -768,13 +786,13 @@
  17844. cputime_t gtime;
  17845. do {
  17846. - seq = read_seqbegin(&t->vtime_seqlock);
  17847. + seq = read_seqcount_begin(&t->vtime_seq);
  17848. gtime = t->gtime;
  17849. if (t->flags & PF_VCPU)
  17850. gtime += vtime_delta(t);
  17851. - } while (read_seqretry(&t->vtime_seqlock, seq));
  17852. + } while (read_seqcount_retry(&t->vtime_seq, seq));
  17853. return gtime;
  17854. }
  17855. @@ -797,7 +815,7 @@
  17856. *udelta = 0;
  17857. *sdelta = 0;
  17858. - seq = read_seqbegin(&t->vtime_seqlock);
  17859. + seq = read_seqcount_begin(&t->vtime_seq);
  17860. if (u_dst)
  17861. *u_dst = *u_src;
  17862. @@ -821,7 +839,7 @@
  17863. if (t->vtime_snap_whence == VTIME_SYS)
  17864. *sdelta = delta;
  17865. }
  17866. - } while (read_seqretry(&t->vtime_seqlock, seq));
  17867. + } while (read_seqcount_retry(&t->vtime_seq, seq));
  17868. }
  17869. diff -Nur linux-4.1.13.orig/kernel/sched/deadline.c linux-4.1.13/kernel/sched/deadline.c
  17870. --- linux-4.1.13.orig/kernel/sched/deadline.c 2015-11-09 23:34:10.000000000 +0100
  17871. +++ linux-4.1.13/kernel/sched/deadline.c 2015-11-29 09:23:09.645611789 +0100
  17872. @@ -637,6 +637,7 @@
  17873. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  17874. timer->function = dl_task_timer;
  17875. + timer->irqsafe = 1;
  17876. }
  17877. static
  17878. diff -Nur linux-4.1.13.orig/kernel/sched/debug.c linux-4.1.13/kernel/sched/debug.c
  17879. --- linux-4.1.13.orig/kernel/sched/debug.c 2015-11-09 23:34:10.000000000 +0100
  17880. +++ linux-4.1.13/kernel/sched/debug.c 2015-11-29 09:23:09.645611789 +0100
  17881. @@ -260,6 +260,9 @@
  17882. P(rt_throttled);
  17883. PN(rt_time);
  17884. PN(rt_runtime);
  17885. +#ifdef CONFIG_SMP
  17886. + P(rt_nr_migratory);
  17887. +#endif
  17888. #undef PN
  17889. #undef P
  17890. @@ -648,6 +651,10 @@
  17891. #endif
  17892. P(policy);
  17893. P(prio);
  17894. +#ifdef CONFIG_PREEMPT_RT_FULL
  17895. + P(migrate_disable);
  17896. +#endif
  17897. + P(nr_cpus_allowed);
  17898. #undef PN
  17899. #undef __PN
  17900. #undef P
  17901. diff -Nur linux-4.1.13.orig/kernel/sched/fair.c linux-4.1.13/kernel/sched/fair.c
  17902. --- linux-4.1.13.orig/kernel/sched/fair.c 2015-11-09 23:34:10.000000000 +0100
  17903. +++ linux-4.1.13/kernel/sched/fair.c 2015-11-29 09:23:09.645611789 +0100
  17904. @@ -3201,7 +3201,7 @@
  17905. ideal_runtime = sched_slice(cfs_rq, curr);
  17906. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  17907. if (delta_exec > ideal_runtime) {
  17908. - resched_curr(rq_of(cfs_rq));
  17909. + resched_curr_lazy(rq_of(cfs_rq));
  17910. /*
  17911. * The current task ran long enough, ensure it doesn't get
  17912. * re-elected due to buddy favours.
  17913. @@ -3225,7 +3225,7 @@
  17914. return;
  17915. if (delta > ideal_runtime)
  17916. - resched_curr(rq_of(cfs_rq));
  17917. + resched_curr_lazy(rq_of(cfs_rq));
  17918. }
  17919. static void
  17920. @@ -3366,7 +3366,7 @@
  17921. * validating it and just reschedule.
  17922. */
  17923. if (queued) {
  17924. - resched_curr(rq_of(cfs_rq));
  17925. + resched_curr_lazy(rq_of(cfs_rq));
  17926. return;
  17927. }
  17928. /*
  17929. @@ -3557,7 +3557,7 @@
  17930. * hierarchy can be throttled
  17931. */
  17932. if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
  17933. - resched_curr(rq_of(cfs_rq));
  17934. + resched_curr_lazy(rq_of(cfs_rq));
  17935. }
  17936. static __always_inline
  17937. @@ -4180,7 +4180,7 @@
  17938. if (delta < 0) {
  17939. if (rq->curr == p)
  17940. - resched_curr(rq);
  17941. + resched_curr_lazy(rq);
  17942. return;
  17943. }
  17944. hrtick_start(rq, delta);
  17945. @@ -5076,7 +5076,7 @@
  17946. return;
  17947. preempt:
  17948. - resched_curr(rq);
  17949. + resched_curr_lazy(rq);
  17950. /*
  17951. * Only set the backward buddy when the current task is still
  17952. * on the rq. This can happen when a wakeup gets interleaved
  17953. @@ -7869,7 +7869,7 @@
  17954. * 'current' within the tree based on its new key value.
  17955. */
  17956. swap(curr->vruntime, se->vruntime);
  17957. - resched_curr(rq);
  17958. + resched_curr_lazy(rq);
  17959. }
  17960. se->vruntime -= cfs_rq->min_vruntime;
  17961. @@ -7894,7 +7894,7 @@
  17962. */
  17963. if (rq->curr == p) {
  17964. if (p->prio > oldprio)
  17965. - resched_curr(rq);
  17966. + resched_curr_lazy(rq);
  17967. } else
  17968. check_preempt_curr(rq, p, 0);
  17969. }
  17970. diff -Nur linux-4.1.13.orig/kernel/sched/features.h linux-4.1.13/kernel/sched/features.h
  17971. --- linux-4.1.13.orig/kernel/sched/features.h 2015-11-09 23:34:10.000000000 +0100
  17972. +++ linux-4.1.13/kernel/sched/features.h 2015-11-29 09:23:09.645611789 +0100
  17973. @@ -50,11 +50,19 @@
  17974. */
  17975. SCHED_FEAT(NONTASK_CAPACITY, true)
  17976. +#ifdef CONFIG_PREEMPT_RT_FULL
  17977. +SCHED_FEAT(TTWU_QUEUE, false)
  17978. +# ifdef CONFIG_PREEMPT_LAZY
  17979. +SCHED_FEAT(PREEMPT_LAZY, true)
  17980. +# endif
  17981. +#else
  17982. +
  17983. /*
  17984. * Queue remote wakeups on the target CPU and process them
  17985. * using the scheduler IPI. Reduces rq->lock contention/bounces.
  17986. */
  17987. SCHED_FEAT(TTWU_QUEUE, true)
  17988. +#endif
  17989. #ifdef HAVE_RT_PUSH_IPI
  17990. /*
  17991. diff -Nur linux-4.1.13.orig/kernel/sched/Makefile linux-4.1.13/kernel/sched/Makefile
  17992. --- linux-4.1.13.orig/kernel/sched/Makefile 2015-11-09 23:34:10.000000000 +0100
  17993. +++ linux-4.1.13/kernel/sched/Makefile 2015-11-29 09:23:09.641612055 +0100
  17994. @@ -13,7 +13,7 @@
  17995. obj-y += core.o proc.o clock.o cputime.o
  17996. obj-y += idle_task.o fair.o rt.o deadline.o stop_task.o
  17997. -obj-y += wait.o completion.o idle.o
  17998. +obj-y += wait.o wait-simple.o work-simple.o completion.o idle.o
  17999. obj-$(CONFIG_SMP) += cpupri.o cpudeadline.o
  18000. obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o
  18001. obj-$(CONFIG_SCHEDSTATS) += stats.o
  18002. diff -Nur linux-4.1.13.orig/kernel/sched/rt.c linux-4.1.13/kernel/sched/rt.c
  18003. --- linux-4.1.13.orig/kernel/sched/rt.c 2015-11-09 23:34:10.000000000 +0100
  18004. +++ linux-4.1.13/kernel/sched/rt.c 2015-11-29 09:23:09.645611789 +0100
  18005. @@ -44,6 +44,7 @@
  18006. hrtimer_init(&rt_b->rt_period_timer,
  18007. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  18008. + rt_b->rt_period_timer.irqsafe = 1;
  18009. rt_b->rt_period_timer.function = sched_rt_period_timer;
  18010. }
  18011. @@ -89,6 +90,7 @@
  18012. rt_rq->push_cpu = nr_cpu_ids;
  18013. raw_spin_lock_init(&rt_rq->push_lock);
  18014. init_irq_work(&rt_rq->push_work, push_irq_work_func);
  18015. + rt_rq->push_work.flags |= IRQ_WORK_HARD_IRQ;
  18016. #endif
  18017. #endif /* CONFIG_SMP */
  18018. /* We start is dequeued state, because no RT tasks are queued */
  18019. diff -Nur linux-4.1.13.orig/kernel/sched/sched.h linux-4.1.13/kernel/sched/sched.h
  18020. --- linux-4.1.13.orig/kernel/sched/sched.h 2015-11-09 23:34:10.000000000 +0100
  18021. +++ linux-4.1.13/kernel/sched/sched.h 2015-11-29 09:23:09.645611789 +0100
  18022. @@ -1093,6 +1093,7 @@
  18023. #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
  18024. #define WF_FORK 0x02 /* child wakeup after fork */
  18025. #define WF_MIGRATED 0x4 /* internal use, task got migrated */
  18026. +#define WF_LOCK_SLEEPER 0x08 /* wakeup spinlock "sleeper" */
  18027. /*
  18028. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  18029. @@ -1290,6 +1291,15 @@
  18030. extern void resched_curr(struct rq *rq);
  18031. extern void resched_cpu(int cpu);
  18032. +#ifdef CONFIG_PREEMPT_LAZY
  18033. +extern void resched_curr_lazy(struct rq *rq);
  18034. +#else
  18035. +static inline void resched_curr_lazy(struct rq *rq)
  18036. +{
  18037. + resched_curr(rq);
  18038. +}
  18039. +#endif
  18040. +
  18041. extern struct rt_bandwidth def_rt_bandwidth;
  18042. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  18043. diff -Nur linux-4.1.13.orig/kernel/sched/wait-simple.c linux-4.1.13/kernel/sched/wait-simple.c
  18044. --- linux-4.1.13.orig/kernel/sched/wait-simple.c 1970-01-01 01:00:00.000000000 +0100
  18045. +++ linux-4.1.13/kernel/sched/wait-simple.c 2015-11-29 09:23:09.645611789 +0100
  18046. @@ -0,0 +1,115 @@
  18047. +/*
  18048. + * Simple waitqueues without fancy flags and callbacks
  18049. + *
  18050. + * (C) 2011 Thomas Gleixner <tglx@linutronix.de>
  18051. + *
  18052. + * Based on kernel/wait.c
  18053. + *
  18054. + * For licencing details see kernel-base/COPYING
  18055. + */
  18056. +#include <linux/init.h>
  18057. +#include <linux/export.h>
  18058. +#include <linux/sched.h>
  18059. +#include <linux/wait-simple.h>
  18060. +
  18061. +/* Adds w to head->list. Must be called with head->lock locked. */
  18062. +static inline void __swait_enqueue(struct swait_head *head, struct swaiter *w)
  18063. +{
  18064. + list_add(&w->node, &head->list);
  18065. + /* We can't let the condition leak before the setting of head */
  18066. + smp_mb();
  18067. +}
  18068. +
  18069. +/* Removes w from head->list. Must be called with head->lock locked. */
  18070. +static inline void __swait_dequeue(struct swaiter *w)
  18071. +{
  18072. + list_del_init(&w->node);
  18073. +}
  18074. +
  18075. +void __init_swait_head(struct swait_head *head, struct lock_class_key *key)
  18076. +{
  18077. + raw_spin_lock_init(&head->lock);
  18078. + lockdep_set_class(&head->lock, key);
  18079. + INIT_LIST_HEAD(&head->list);
  18080. +}
  18081. +EXPORT_SYMBOL(__init_swait_head);
  18082. +
  18083. +void swait_prepare_locked(struct swait_head *head, struct swaiter *w)
  18084. +{
  18085. + w->task = current;
  18086. + if (list_empty(&w->node))
  18087. + __swait_enqueue(head, w);
  18088. +}
  18089. +
  18090. +void swait_prepare(struct swait_head *head, struct swaiter *w, int state)
  18091. +{
  18092. + unsigned long flags;
  18093. +
  18094. + raw_spin_lock_irqsave(&head->lock, flags);
  18095. + swait_prepare_locked(head, w);
  18096. + __set_current_state(state);
  18097. + raw_spin_unlock_irqrestore(&head->lock, flags);
  18098. +}
  18099. +EXPORT_SYMBOL(swait_prepare);
  18100. +
  18101. +void swait_finish_locked(struct swait_head *head, struct swaiter *w)
  18102. +{
  18103. + __set_current_state(TASK_RUNNING);
  18104. + if (w->task)
  18105. + __swait_dequeue(w);
  18106. +}
  18107. +
  18108. +void swait_finish(struct swait_head *head, struct swaiter *w)
  18109. +{
  18110. + unsigned long flags;
  18111. +
  18112. + __set_current_state(TASK_RUNNING);
  18113. + if (w->task) {
  18114. + raw_spin_lock_irqsave(&head->lock, flags);
  18115. + __swait_dequeue(w);
  18116. + raw_spin_unlock_irqrestore(&head->lock, flags);
  18117. + }
  18118. +}
  18119. +EXPORT_SYMBOL(swait_finish);
  18120. +
  18121. +unsigned int
  18122. +__swait_wake_locked(struct swait_head *head, unsigned int state, unsigned int num)
  18123. +{
  18124. + struct swaiter *curr, *next;
  18125. + int woken = 0;
  18126. +
  18127. + list_for_each_entry_safe(curr, next, &head->list, node) {
  18128. + if (wake_up_state(curr->task, state)) {
  18129. + __swait_dequeue(curr);
  18130. + /*
  18131. + * The waiting task can free the waiter as
  18132. + * soon as curr->task = NULL is written,
  18133. + * without taking any locks. A memory barrier
  18134. + * is required here to prevent the following
  18135. + * store to curr->task from getting ahead of
  18136. + * the dequeue operation.
  18137. + */
  18138. + smp_wmb();
  18139. + curr->task = NULL;
  18140. + if (++woken == num)
  18141. + break;
  18142. + }
  18143. + }
  18144. + return woken;
  18145. +}
  18146. +
  18147. +unsigned int
  18148. +__swait_wake(struct swait_head *head, unsigned int state, unsigned int num)
  18149. +{
  18150. + unsigned long flags;
  18151. + int woken;
  18152. +
  18153. + if (!swaitqueue_active(head))
  18154. + return 0;
  18155. +
  18156. + raw_spin_lock_irqsave(&head->lock, flags);
  18157. + woken = __swait_wake_locked(head, state, num);
  18158. + raw_spin_unlock_irqrestore(&head->lock, flags);
  18159. + return woken;
  18160. +}
  18161. +EXPORT_SYMBOL(__swait_wake);
  18162. diff -Nur linux-4.1.13.orig/kernel/sched/work-simple.c linux-4.1.13/kernel/sched/work-simple.c
  18163. --- linux-4.1.13.orig/kernel/sched/work-simple.c 1970-01-01 01:00:00.000000000 +0100
  18164. +++ linux-4.1.13/kernel/sched/work-simple.c 2015-11-29 09:23:09.645611789 +0100
  18165. @@ -0,0 +1,173 @@
  18166. +/*
  18167. + * Copyright (C) 2014 BMW Car IT GmbH, Daniel Wagner daniel.wagner@bmw-carit.de
  18168. + *
  18169. + * Provides a framework for enqueuing callbacks from irq context
  18170. + * PREEMPT_RT_FULL safe. The callbacks are executed in kthread context.
  18171. + */
  18172. +
  18173. +#include <linux/wait-simple.h>
  18174. +#include <linux/work-simple.h>
  18175. +#include <linux/kthread.h>
  18176. +#include <linux/slab.h>
  18177. +#include <linux/spinlock.h>
  18178. +#include <linux/export.h>
  18179. +
  18180. +#define SWORK_EVENT_PENDING (1 << 0)
  18181. +
  18182. +static DEFINE_MUTEX(worker_mutex);
  18183. +static struct sworker *glob_worker;
  18184. +
  18185. +struct sworker {
  18186. + struct list_head events;
  18187. + struct swait_head wq;
  18188. +
  18189. + raw_spinlock_t lock;
  18190. +
  18191. + struct task_struct *task;
  18192. + int refs;
  18193. +};
  18194. +
  18195. +static bool swork_readable(struct sworker *worker)
  18196. +{
  18197. + bool r;
  18198. +
  18199. + if (kthread_should_stop())
  18200. + return true;
  18201. +
  18202. + raw_spin_lock_irq(&worker->lock);
  18203. + r = !list_empty(&worker->events);
  18204. + raw_spin_unlock_irq(&worker->lock);
  18205. +
  18206. + return r;
  18207. +}
  18208. +
  18209. +static int swork_kthread(void *arg)
  18210. +{
  18211. + struct sworker *worker = arg;
  18212. +
  18213. + for (;;) {
  18214. + swait_event_interruptible(worker->wq,
  18215. + swork_readable(worker));
  18216. + if (kthread_should_stop())
  18217. + break;
  18218. +
  18219. + raw_spin_lock_irq(&worker->lock);
  18220. + while (!list_empty(&worker->events)) {
  18221. + struct swork_event *sev;
  18222. +
  18223. + sev = list_first_entry(&worker->events,
  18224. + struct swork_event, item);
  18225. + list_del(&sev->item);
  18226. + raw_spin_unlock_irq(&worker->lock);
  18227. +
  18228. + WARN_ON_ONCE(!test_and_clear_bit(SWORK_EVENT_PENDING,
  18229. + &sev->flags));
  18230. + sev->func(sev);
  18231. + raw_spin_lock_irq(&worker->lock);
  18232. + }
  18233. + raw_spin_unlock_irq(&worker->lock);
  18234. + }
  18235. + return 0;
  18236. +}
  18237. +
  18238. +static struct sworker *swork_create(void)
  18239. +{
  18240. + struct sworker *worker;
  18241. +
  18242. + worker = kzalloc(sizeof(*worker), GFP_KERNEL);
  18243. + if (!worker)
  18244. + return ERR_PTR(-ENOMEM);
  18245. +
  18246. + INIT_LIST_HEAD(&worker->events);
  18247. + raw_spin_lock_init(&worker->lock);
  18248. + init_swait_head(&worker->wq);
  18249. +
  18250. + worker->task = kthread_run(swork_kthread, worker, "kswork");
  18251. + if (IS_ERR(worker->task)) {
  18252. + kfree(worker);
  18253. + return ERR_PTR(-ENOMEM);
  18254. + }
  18255. +
  18256. + return worker;
  18257. +}
  18258. +
  18259. +static void swork_destroy(struct sworker *worker)
  18260. +{
  18261. + kthread_stop(worker->task);
  18262. +
  18263. + WARN_ON(!list_empty(&worker->events));
  18264. + kfree(worker);
  18265. +}
  18266. +
  18267. +/**
  18268. + * swork_queue - queue swork
  18269. + *
  18270. + * Returns %false if @work was already on a queue, %true otherwise.
  18271. + *
  18272. + * The work is queued and processed on a random CPU
  18273. + */
  18274. +bool swork_queue(struct swork_event *sev)
  18275. +{
  18276. + unsigned long flags;
  18277. +
  18278. + if (test_and_set_bit(SWORK_EVENT_PENDING, &sev->flags))
  18279. + return false;
  18280. +
  18281. + raw_spin_lock_irqsave(&glob_worker->lock, flags);
  18282. + list_add_tail(&sev->item, &glob_worker->events);
  18283. + raw_spin_unlock_irqrestore(&glob_worker->lock, flags);
  18284. +
  18285. + swait_wake(&glob_worker->wq);
  18286. + return true;
  18287. +}
  18288. +EXPORT_SYMBOL_GPL(swork_queue);
  18289. +
  18290. +/**
  18291. + * swork_get - get an instance of the sworker
  18292. + *
  18293. + * Returns an negative error code if the initialization if the worker did not
  18294. + * work, %0 otherwise.
  18295. + *
  18296. + */
  18297. +int swork_get(void)
  18298. +{
  18299. + struct sworker *worker;
  18300. +
  18301. + mutex_lock(&worker_mutex);
  18302. + if (!glob_worker) {
  18303. + worker = swork_create();
  18304. + if (IS_ERR(worker)) {
  18305. + mutex_unlock(&worker_mutex);
  18306. + return -ENOMEM;
  18307. + }
  18308. +
  18309. + glob_worker = worker;
  18310. + }
  18311. +
  18312. + glob_worker->refs++;
  18313. + mutex_unlock(&worker_mutex);
  18314. +
  18315. + return 0;
  18316. +}
  18317. +EXPORT_SYMBOL_GPL(swork_get);
  18318. +
  18319. +/**
  18320. + * swork_put - puts an instance of the sworker
  18321. + *
  18322. + * Will destroy the sworker thread. This function must not be called until all
  18323. + * queued events have been completed.
  18324. + */
  18325. +void swork_put(void)
  18326. +{
  18327. + mutex_lock(&worker_mutex);
  18328. +
  18329. + glob_worker->refs--;
  18330. + if (glob_worker->refs > 0)
  18331. + goto out;
  18332. +
  18333. + swork_destroy(glob_worker);
  18334. + glob_worker = NULL;
  18335. +out:
  18336. + mutex_unlock(&worker_mutex);
  18337. +}
  18338. +EXPORT_SYMBOL_GPL(swork_put);
  18339. diff -Nur linux-4.1.13.orig/kernel/signal.c linux-4.1.13/kernel/signal.c
  18340. --- linux-4.1.13.orig/kernel/signal.c 2015-11-09 23:34:10.000000000 +0100
  18341. +++ linux-4.1.13/kernel/signal.c 2015-11-29 09:23:09.649611524 +0100
  18342. @@ -14,6 +14,7 @@
  18343. #include <linux/export.h>
  18344. #include <linux/init.h>
  18345. #include <linux/sched.h>
  18346. +#include <linux/sched/rt.h>
  18347. #include <linux/fs.h>
  18348. #include <linux/tty.h>
  18349. #include <linux/binfmts.h>
  18350. @@ -352,13 +353,45 @@
  18351. return false;
  18352. }
  18353. +#ifdef __HAVE_ARCH_CMPXCHG
  18354. +static inline struct sigqueue *get_task_cache(struct task_struct *t)
  18355. +{
  18356. + struct sigqueue *q = t->sigqueue_cache;
  18357. +
  18358. + if (cmpxchg(&t->sigqueue_cache, q, NULL) != q)
  18359. + return NULL;
  18360. + return q;
  18361. +}
  18362. +
  18363. +static inline int put_task_cache(struct task_struct *t, struct sigqueue *q)
  18364. +{
  18365. + if (cmpxchg(&t->sigqueue_cache, NULL, q) == NULL)
  18366. + return 0;
  18367. + return 1;
  18368. +}
  18369. +
  18370. +#else
  18371. +
  18372. +static inline struct sigqueue *get_task_cache(struct task_struct *t)
  18373. +{
  18374. + return NULL;
  18375. +}
  18376. +
  18377. +static inline int put_task_cache(struct task_struct *t, struct sigqueue *q)
  18378. +{
  18379. + return 1;
  18380. +}
  18381. +
  18382. +#endif
  18383. +
  18384. /*
  18385. * allocate a new signal queue record
  18386. * - this may be called without locks if and only if t == current, otherwise an
  18387. * appropriate lock must be held to stop the target task from exiting
  18388. */
  18389. static struct sigqueue *
  18390. -__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
  18391. +__sigqueue_do_alloc(int sig, struct task_struct *t, gfp_t flags,
  18392. + int override_rlimit, int fromslab)
  18393. {
  18394. struct sigqueue *q = NULL;
  18395. struct user_struct *user;
  18396. @@ -375,7 +408,10 @@
  18397. if (override_rlimit ||
  18398. atomic_read(&user->sigpending) <=
  18399. task_rlimit(t, RLIMIT_SIGPENDING)) {
  18400. - q = kmem_cache_alloc(sigqueue_cachep, flags);
  18401. + if (!fromslab)
  18402. + q = get_task_cache(t);
  18403. + if (!q)
  18404. + q = kmem_cache_alloc(sigqueue_cachep, flags);
  18405. } else {
  18406. print_dropped_signal(sig);
  18407. }
  18408. @@ -392,6 +428,13 @@
  18409. return q;
  18410. }
  18411. +static struct sigqueue *
  18412. +__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags,
  18413. + int override_rlimit)
  18414. +{
  18415. + return __sigqueue_do_alloc(sig, t, flags, override_rlimit, 0);
  18416. +}
  18417. +
  18418. static void __sigqueue_free(struct sigqueue *q)
  18419. {
  18420. if (q->flags & SIGQUEUE_PREALLOC)
  18421. @@ -401,6 +444,21 @@
  18422. kmem_cache_free(sigqueue_cachep, q);
  18423. }
  18424. +static void sigqueue_free_current(struct sigqueue *q)
  18425. +{
  18426. + struct user_struct *up;
  18427. +
  18428. + if (q->flags & SIGQUEUE_PREALLOC)
  18429. + return;
  18430. +
  18431. + up = q->user;
  18432. + if (rt_prio(current->normal_prio) && !put_task_cache(current, q)) {
  18433. + atomic_dec(&up->sigpending);
  18434. + free_uid(up);
  18435. + } else
  18436. + __sigqueue_free(q);
  18437. +}
  18438. +
  18439. void flush_sigqueue(struct sigpending *queue)
  18440. {
  18441. struct sigqueue *q;
  18442. @@ -414,6 +472,21 @@
  18443. }
  18444. /*
  18445. + * Called from __exit_signal. Flush tsk->pending and
  18446. + * tsk->sigqueue_cache
  18447. + */
  18448. +void flush_task_sigqueue(struct task_struct *tsk)
  18449. +{
  18450. + struct sigqueue *q;
  18451. +
  18452. + flush_sigqueue(&tsk->pending);
  18453. +
  18454. + q = get_task_cache(tsk);
  18455. + if (q)
  18456. + kmem_cache_free(sigqueue_cachep, q);
  18457. +}
  18458. +
  18459. +/*
  18460. * Flush all pending signals for a task.
  18461. */
  18462. void __flush_signals(struct task_struct *t)
  18463. @@ -565,7 +638,7 @@
  18464. still_pending:
  18465. list_del_init(&first->list);
  18466. copy_siginfo(info, &first->info);
  18467. - __sigqueue_free(first);
  18468. + sigqueue_free_current(first);
  18469. } else {
  18470. /*
  18471. * Ok, it wasn't in the queue. This must be
  18472. @@ -611,6 +684,8 @@
  18473. {
  18474. int signr;
  18475. + WARN_ON_ONCE(tsk != current);
  18476. +
  18477. /* We only dequeue private signals from ourselves, we don't let
  18478. * signalfd steal them
  18479. */
  18480. @@ -1207,8 +1282,8 @@
  18481. * We don't want to have recursive SIGSEGV's etc, for example,
  18482. * that is why we also clear SIGNAL_UNKILLABLE.
  18483. */
  18484. -int
  18485. -force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  18486. +static int
  18487. +do_force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  18488. {
  18489. unsigned long int flags;
  18490. int ret, blocked, ignored;
  18491. @@ -1233,6 +1308,39 @@
  18492. return ret;
  18493. }
  18494. +int force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  18495. +{
  18496. +/*
  18497. + * On some archs, PREEMPT_RT has to delay sending a signal from a trap
  18498. + * since it can not enable preemption, and the signal code's spin_locks
  18499. + * turn into mutexes. Instead, it must set TIF_NOTIFY_RESUME which will
  18500. + * send the signal on exit of the trap.
  18501. + */
  18502. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  18503. + if (in_atomic()) {
  18504. + if (WARN_ON_ONCE(t != current))
  18505. + return 0;
  18506. + if (WARN_ON_ONCE(t->forced_info.si_signo))
  18507. + return 0;
  18508. +
  18509. + if (is_si_special(info)) {
  18510. + WARN_ON_ONCE(info != SEND_SIG_PRIV);
  18511. + t->forced_info.si_signo = sig;
  18512. + t->forced_info.si_errno = 0;
  18513. + t->forced_info.si_code = SI_KERNEL;
  18514. + t->forced_info.si_pid = 0;
  18515. + t->forced_info.si_uid = 0;
  18516. + } else {
  18517. + t->forced_info = *info;
  18518. + }
  18519. +
  18520. + set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  18521. + return 0;
  18522. + }
  18523. +#endif
  18524. + return do_force_sig_info(sig, info, t);
  18525. +}
  18526. +
  18527. /*
  18528. * Nuke all other threads in the group.
  18529. */
  18530. @@ -1267,12 +1375,12 @@
  18531. * Disable interrupts early to avoid deadlocks.
  18532. * See rcu_read_unlock() comment header for details.
  18533. */
  18534. - local_irq_save(*flags);
  18535. + local_irq_save_nort(*flags);
  18536. rcu_read_lock();
  18537. sighand = rcu_dereference(tsk->sighand);
  18538. if (unlikely(sighand == NULL)) {
  18539. rcu_read_unlock();
  18540. - local_irq_restore(*flags);
  18541. + local_irq_restore_nort(*flags);
  18542. break;
  18543. }
  18544. /*
  18545. @@ -1293,7 +1401,7 @@
  18546. }
  18547. spin_unlock(&sighand->siglock);
  18548. rcu_read_unlock();
  18549. - local_irq_restore(*flags);
  18550. + local_irq_restore_nort(*flags);
  18551. }
  18552. return sighand;
  18553. @@ -1536,7 +1644,8 @@
  18554. */
  18555. struct sigqueue *sigqueue_alloc(void)
  18556. {
  18557. - struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
  18558. + /* Preallocated sigqueue objects always from the slabcache ! */
  18559. + struct sigqueue *q = __sigqueue_do_alloc(-1, current, GFP_KERNEL, 0, 1);
  18560. if (q)
  18561. q->flags |= SIGQUEUE_PREALLOC;
  18562. @@ -1897,15 +2006,7 @@
  18563. if (gstop_done && ptrace_reparented(current))
  18564. do_notify_parent_cldstop(current, false, why);
  18565. - /*
  18566. - * Don't want to allow preemption here, because
  18567. - * sys_ptrace() needs this task to be inactive.
  18568. - *
  18569. - * XXX: implement read_unlock_no_resched().
  18570. - */
  18571. - preempt_disable();
  18572. read_unlock(&tasklist_lock);
  18573. - preempt_enable_no_resched();
  18574. freezable_schedule();
  18575. } else {
  18576. /*
  18577. diff -Nur linux-4.1.13.orig/kernel/softirq.c linux-4.1.13/kernel/softirq.c
  18578. --- linux-4.1.13.orig/kernel/softirq.c 2015-11-09 23:34:10.000000000 +0100
  18579. +++ linux-4.1.13/kernel/softirq.c 2015-11-29 09:23:09.649611524 +0100
  18580. @@ -21,10 +21,12 @@
  18581. #include <linux/freezer.h>
  18582. #include <linux/kthread.h>
  18583. #include <linux/rcupdate.h>
  18584. +#include <linux/delay.h>
  18585. #include <linux/ftrace.h>
  18586. #include <linux/smp.h>
  18587. #include <linux/smpboot.h>
  18588. #include <linux/tick.h>
  18589. +#include <linux/locallock.h>
  18590. #include <linux/irq.h>
  18591. #define CREATE_TRACE_POINTS
  18592. @@ -62,6 +64,98 @@
  18593. "TASKLET", "SCHED", "HRTIMER", "RCU"
  18594. };
  18595. +#ifdef CONFIG_NO_HZ_COMMON
  18596. +# ifdef CONFIG_PREEMPT_RT_FULL
  18597. +
  18598. +struct softirq_runner {
  18599. + struct task_struct *runner[NR_SOFTIRQS];
  18600. +};
  18601. +
  18602. +static DEFINE_PER_CPU(struct softirq_runner, softirq_runners);
  18603. +
  18604. +static inline void softirq_set_runner(unsigned int sirq)
  18605. +{
  18606. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  18607. +
  18608. + sr->runner[sirq] = current;
  18609. +}
  18610. +
  18611. +static inline void softirq_clr_runner(unsigned int sirq)
  18612. +{
  18613. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  18614. +
  18615. + sr->runner[sirq] = NULL;
  18616. +}
  18617. +
  18618. +/*
  18619. + * On preempt-rt a softirq running context might be blocked on a
  18620. + * lock. There might be no other runnable task on this CPU because the
  18621. + * lock owner runs on some other CPU. So we have to go into idle with
  18622. + * the pending bit set. Therefor we need to check this otherwise we
  18623. + * warn about false positives which confuses users and defeats the
  18624. + * whole purpose of this test.
  18625. + *
  18626. + * This code is called with interrupts disabled.
  18627. + */
  18628. +void softirq_check_pending_idle(void)
  18629. +{
  18630. + static int rate_limit;
  18631. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  18632. + u32 warnpending;
  18633. + int i;
  18634. +
  18635. + if (rate_limit >= 10)
  18636. + return;
  18637. +
  18638. + warnpending = local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK;
  18639. + for (i = 0; i < NR_SOFTIRQS; i++) {
  18640. + struct task_struct *tsk = sr->runner[i];
  18641. +
  18642. + /*
  18643. + * The wakeup code in rtmutex.c wakes up the task
  18644. + * _before_ it sets pi_blocked_on to NULL under
  18645. + * tsk->pi_lock. So we need to check for both: state
  18646. + * and pi_blocked_on.
  18647. + */
  18648. + if (tsk) {
  18649. + raw_spin_lock(&tsk->pi_lock);
  18650. + if (tsk->pi_blocked_on || tsk->state == TASK_RUNNING) {
  18651. + /* Clear all bits pending in that task */
  18652. + warnpending &= ~(tsk->softirqs_raised);
  18653. + warnpending &= ~(1 << i);
  18654. + }
  18655. + raw_spin_unlock(&tsk->pi_lock);
  18656. + }
  18657. + }
  18658. +
  18659. + if (warnpending) {
  18660. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  18661. + warnpending);
  18662. + rate_limit++;
  18663. + }
  18664. +}
  18665. +# else
  18666. +/*
  18667. + * On !PREEMPT_RT we just printk rate limited:
  18668. + */
  18669. +void softirq_check_pending_idle(void)
  18670. +{
  18671. + static int rate_limit;
  18672. +
  18673. + if (rate_limit < 10 &&
  18674. + (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  18675. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  18676. + local_softirq_pending());
  18677. + rate_limit++;
  18678. + }
  18679. +}
  18680. +# endif
  18681. +
  18682. +#else /* !CONFIG_NO_HZ_COMMON */
  18683. +static inline void softirq_set_runner(unsigned int sirq) { }
  18684. +static inline void softirq_clr_runner(unsigned int sirq) { }
  18685. +#endif
  18686. +
  18687. /*
  18688. * we cannot loop indefinitely here to avoid userspace starvation,
  18689. * but we also don't want to introduce a worst case 1/HZ latency
  18690. @@ -77,6 +171,68 @@
  18691. wake_up_process(tsk);
  18692. }
  18693. +static void handle_softirq(unsigned int vec_nr)
  18694. +{
  18695. + struct softirq_action *h = softirq_vec + vec_nr;
  18696. + int prev_count;
  18697. +
  18698. + prev_count = preempt_count();
  18699. +
  18700. + kstat_incr_softirqs_this_cpu(vec_nr);
  18701. +
  18702. + trace_softirq_entry(vec_nr);
  18703. + h->action(h);
  18704. + trace_softirq_exit(vec_nr);
  18705. + if (unlikely(prev_count != preempt_count())) {
  18706. + pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  18707. + vec_nr, softirq_to_name[vec_nr], h->action,
  18708. + prev_count, preempt_count());
  18709. + preempt_count_set(prev_count);
  18710. + }
  18711. +}
  18712. +
  18713. +#ifndef CONFIG_PREEMPT_RT_FULL
  18714. +static inline int ksoftirqd_softirq_pending(void)
  18715. +{
  18716. + return local_softirq_pending();
  18717. +}
  18718. +
  18719. +static void handle_pending_softirqs(u32 pending)
  18720. +{
  18721. + struct softirq_action *h = softirq_vec;
  18722. + int softirq_bit;
  18723. +
  18724. + local_irq_enable();
  18725. +
  18726. + h = softirq_vec;
  18727. +
  18728. + while ((softirq_bit = ffs(pending))) {
  18729. + unsigned int vec_nr;
  18730. +
  18731. + h += softirq_bit - 1;
  18732. + vec_nr = h - softirq_vec;
  18733. + handle_softirq(vec_nr);
  18734. +
  18735. + h++;
  18736. + pending >>= softirq_bit;
  18737. + }
  18738. +
  18739. + rcu_bh_qs();
  18740. + local_irq_disable();
  18741. +}
  18742. +
  18743. +static void run_ksoftirqd(unsigned int cpu)
  18744. +{
  18745. + local_irq_disable();
  18746. + if (ksoftirqd_softirq_pending()) {
  18747. + __do_softirq();
  18748. + local_irq_enable();
  18749. + cond_resched_rcu_qs();
  18750. + return;
  18751. + }
  18752. + local_irq_enable();
  18753. +}
  18754. +
  18755. /*
  18756. * preempt_count and SOFTIRQ_OFFSET usage:
  18757. * - preempt_count is changed by SOFTIRQ_OFFSET on entering or leaving
  18758. @@ -232,10 +388,8 @@
  18759. unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
  18760. unsigned long old_flags = current->flags;
  18761. int max_restart = MAX_SOFTIRQ_RESTART;
  18762. - struct softirq_action *h;
  18763. bool in_hardirq;
  18764. __u32 pending;
  18765. - int softirq_bit;
  18766. /*
  18767. * Mask out PF_MEMALLOC s current task context is borrowed for the
  18768. @@ -254,36 +408,7 @@
  18769. /* Reset the pending bitmask before enabling irqs */
  18770. set_softirq_pending(0);
  18771. - local_irq_enable();
  18772. -
  18773. - h = softirq_vec;
  18774. -
  18775. - while ((softirq_bit = ffs(pending))) {
  18776. - unsigned int vec_nr;
  18777. - int prev_count;
  18778. -
  18779. - h += softirq_bit - 1;
  18780. -
  18781. - vec_nr = h - softirq_vec;
  18782. - prev_count = preempt_count();
  18783. -
  18784. - kstat_incr_softirqs_this_cpu(vec_nr);
  18785. -
  18786. - trace_softirq_entry(vec_nr);
  18787. - h->action(h);
  18788. - trace_softirq_exit(vec_nr);
  18789. - if (unlikely(prev_count != preempt_count())) {
  18790. - pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  18791. - vec_nr, softirq_to_name[vec_nr], h->action,
  18792. - prev_count, preempt_count());
  18793. - preempt_count_set(prev_count);
  18794. - }
  18795. - h++;
  18796. - pending >>= softirq_bit;
  18797. - }
  18798. -
  18799. - rcu_bh_qs();
  18800. - local_irq_disable();
  18801. + handle_pending_softirqs(pending);
  18802. pending = local_softirq_pending();
  18803. if (pending) {
  18804. @@ -320,6 +445,255 @@
  18805. }
  18806. /*
  18807. + * This function must run with irqs disabled!
  18808. + */
  18809. +void raise_softirq_irqoff(unsigned int nr)
  18810. +{
  18811. + __raise_softirq_irqoff(nr);
  18812. +
  18813. + /*
  18814. + * If we're in an interrupt or softirq, we're done
  18815. + * (this also catches softirq-disabled code). We will
  18816. + * actually run the softirq once we return from
  18817. + * the irq or softirq.
  18818. + *
  18819. + * Otherwise we wake up ksoftirqd to make sure we
  18820. + * schedule the softirq soon.
  18821. + */
  18822. + if (!in_interrupt())
  18823. + wakeup_softirqd();
  18824. +}
  18825. +
  18826. +void __raise_softirq_irqoff(unsigned int nr)
  18827. +{
  18828. + trace_softirq_raise(nr);
  18829. + or_softirq_pending(1UL << nr);
  18830. +}
  18831. +
  18832. +static inline void local_bh_disable_nort(void) { local_bh_disable(); }
  18833. +static inline void _local_bh_enable_nort(void) { _local_bh_enable(); }
  18834. +static void ksoftirqd_set_sched_params(unsigned int cpu) { }
  18835. +static void ksoftirqd_clr_sched_params(unsigned int cpu, bool online) { }
  18836. +
  18837. +#else /* !PREEMPT_RT_FULL */
  18838. +
  18839. +/*
  18840. + * On RT we serialize softirq execution with a cpu local lock per softirq
  18841. + */
  18842. +static DEFINE_PER_CPU(struct local_irq_lock [NR_SOFTIRQS], local_softirq_locks);
  18843. +
  18844. +void __init softirq_early_init(void)
  18845. +{
  18846. + int i;
  18847. +
  18848. + for (i = 0; i < NR_SOFTIRQS; i++)
  18849. + local_irq_lock_init(local_softirq_locks[i]);
  18850. +}
  18851. +
  18852. +static void lock_softirq(int which)
  18853. +{
  18854. + local_lock(local_softirq_locks[which]);
  18855. +}
  18856. +
  18857. +static void unlock_softirq(int which)
  18858. +{
  18859. + local_unlock(local_softirq_locks[which]);
  18860. +}
  18861. +
  18862. +static void do_single_softirq(int which)
  18863. +{
  18864. + unsigned long old_flags = current->flags;
  18865. +
  18866. + current->flags &= ~PF_MEMALLOC;
  18867. + vtime_account_irq_enter(current);
  18868. + current->flags |= PF_IN_SOFTIRQ;
  18869. + lockdep_softirq_enter();
  18870. + local_irq_enable();
  18871. + handle_softirq(which);
  18872. + local_irq_disable();
  18873. + lockdep_softirq_exit();
  18874. + current->flags &= ~PF_IN_SOFTIRQ;
  18875. + vtime_account_irq_enter(current);
  18876. + tsk_restore_flags(current, old_flags, PF_MEMALLOC);
  18877. +}
  18878. +
  18879. +/*
  18880. + * Called with interrupts disabled. Process softirqs which were raised
  18881. + * in current context (or on behalf of ksoftirqd).
  18882. + */
  18883. +static void do_current_softirqs(void)
  18884. +{
  18885. + while (current->softirqs_raised) {
  18886. + int i = __ffs(current->softirqs_raised);
  18887. + unsigned int pending, mask = (1U << i);
  18888. +
  18889. + current->softirqs_raised &= ~mask;
  18890. + local_irq_enable();
  18891. +
  18892. + /*
  18893. + * If the lock is contended, we boost the owner to
  18894. + * process the softirq or leave the critical section
  18895. + * now.
  18896. + */
  18897. + lock_softirq(i);
  18898. + local_irq_disable();
  18899. + softirq_set_runner(i);
  18900. + /*
  18901. + * Check with the local_softirq_pending() bits,
  18902. + * whether we need to process this still or if someone
  18903. + * else took care of it.
  18904. + */
  18905. + pending = local_softirq_pending();
  18906. + if (pending & mask) {
  18907. + set_softirq_pending(pending & ~mask);
  18908. + do_single_softirq(i);
  18909. + }
  18910. + softirq_clr_runner(i);
  18911. + unlock_softirq(i);
  18912. + WARN_ON(current->softirq_nestcnt != 1);
  18913. + }
  18914. +}
  18915. +
  18916. +void __local_bh_disable(void)
  18917. +{
  18918. + if (++current->softirq_nestcnt == 1)
  18919. + migrate_disable();
  18920. +}
  18921. +EXPORT_SYMBOL(__local_bh_disable);
  18922. +
  18923. +void __local_bh_enable(void)
  18924. +{
  18925. + if (WARN_ON(current->softirq_nestcnt == 0))
  18926. + return;
  18927. +
  18928. + local_irq_disable();
  18929. + if (current->softirq_nestcnt == 1 && current->softirqs_raised)
  18930. + do_current_softirqs();
  18931. + local_irq_enable();
  18932. +
  18933. + if (--current->softirq_nestcnt == 0)
  18934. + migrate_enable();
  18935. +}
  18936. +EXPORT_SYMBOL(__local_bh_enable);
  18937. +
  18938. +void _local_bh_enable(void)
  18939. +{
  18940. + if (WARN_ON(current->softirq_nestcnt == 0))
  18941. + return;
  18942. + if (--current->softirq_nestcnt == 0)
  18943. + migrate_enable();
  18944. +}
  18945. +EXPORT_SYMBOL(_local_bh_enable);
  18946. +
  18947. +int in_serving_softirq(void)
  18948. +{
  18949. + return current->flags & PF_IN_SOFTIRQ;
  18950. +}
  18951. +EXPORT_SYMBOL(in_serving_softirq);
  18952. +
  18953. +/* Called with preemption disabled */
  18954. +static void run_ksoftirqd(unsigned int cpu)
  18955. +{
  18956. + local_irq_disable();
  18957. + current->softirq_nestcnt++;
  18958. +
  18959. + do_current_softirqs();
  18960. + current->softirq_nestcnt--;
  18961. + rcu_note_context_switch();
  18962. + local_irq_enable();
  18963. +}
  18964. +
  18965. +/*
  18966. + * Called from netif_rx_ni(). Preemption enabled, but migration
  18967. + * disabled. So the cpu can't go away under us.
  18968. + */
  18969. +void thread_do_softirq(void)
  18970. +{
  18971. + if (!in_serving_softirq() && current->softirqs_raised) {
  18972. + current->softirq_nestcnt++;
  18973. + do_current_softirqs();
  18974. + current->softirq_nestcnt--;
  18975. + }
  18976. +}
  18977. +
  18978. +static void do_raise_softirq_irqoff(unsigned int nr)
  18979. +{
  18980. + trace_softirq_raise(nr);
  18981. + or_softirq_pending(1UL << nr);
  18982. +
  18983. + /*
  18984. + * If we are not in a hard interrupt and inside a bh disabled
  18985. + * region, we simply raise the flag on current. local_bh_enable()
  18986. + * will make sure that the softirq is executed. Otherwise we
  18987. + * delegate it to ksoftirqd.
  18988. + */
  18989. + if (!in_irq() && current->softirq_nestcnt)
  18990. + current->softirqs_raised |= (1U << nr);
  18991. + else if (__this_cpu_read(ksoftirqd))
  18992. + __this_cpu_read(ksoftirqd)->softirqs_raised |= (1U << nr);
  18993. +}
  18994. +
  18995. +void __raise_softirq_irqoff(unsigned int nr)
  18996. +{
  18997. + do_raise_softirq_irqoff(nr);
  18998. + if (!in_irq() && !current->softirq_nestcnt)
  18999. + wakeup_softirqd();
  19000. +}
  19001. +
  19002. +/*
  19003. + * This function must run with irqs disabled!
  19004. + */
  19005. +void raise_softirq_irqoff(unsigned int nr)
  19006. +{
  19007. + do_raise_softirq_irqoff(nr);
  19008. +
  19009. + /*
  19010. + * If we're in an hard interrupt we let irq return code deal
  19011. + * with the wakeup of ksoftirqd.
  19012. + */
  19013. + if (in_irq())
  19014. + return;
  19015. + /*
  19016. + * If we are in thread context but outside of a bh disabled
  19017. + * region, we need to wake ksoftirqd as well.
  19018. + *
  19019. + * CHECKME: Some of the places which do that could be wrapped
  19020. + * into local_bh_disable/enable pairs. Though it's unclear
  19021. + * whether this is worth the effort. To find those places just
  19022. + * raise a WARN() if the condition is met.
  19023. + */
  19024. + if (!current->softirq_nestcnt)
  19025. + wakeup_softirqd();
  19026. +}
  19027. +
  19028. +static inline int ksoftirqd_softirq_pending(void)
  19029. +{
  19030. + return current->softirqs_raised;
  19031. +}
  19032. +
  19033. +static inline void local_bh_disable_nort(void) { }
  19034. +static inline void _local_bh_enable_nort(void) { }
  19035. +
  19036. +static inline void ksoftirqd_set_sched_params(unsigned int cpu)
  19037. +{
  19038. + struct sched_param param = { .sched_priority = 1 };
  19039. +
  19040. + sched_setscheduler(current, SCHED_FIFO, &param);
  19041. + /* Take over all pending softirqs when starting */
  19042. + local_irq_disable();
  19043. + current->softirqs_raised = local_softirq_pending();
  19044. + local_irq_enable();
  19045. +}
  19046. +
  19047. +static inline void ksoftirqd_clr_sched_params(unsigned int cpu, bool online)
  19048. +{
  19049. + struct sched_param param = { .sched_priority = 0 };
  19050. +
  19051. + sched_setscheduler(current, SCHED_NORMAL, &param);
  19052. +}
  19053. +
  19054. +#endif /* PREEMPT_RT_FULL */
  19055. +/*
  19056. * Enter an interrupt context.
  19057. */
  19058. void irq_enter(void)
  19059. @@ -330,9 +704,9 @@
  19060. * Prevent raise_softirq from needlessly waking up ksoftirqd
  19061. * here, as softirq will be serviced on return from interrupt.
  19062. */
  19063. - local_bh_disable();
  19064. + local_bh_disable_nort();
  19065. tick_irq_enter();
  19066. - _local_bh_enable();
  19067. + _local_bh_enable_nort();
  19068. }
  19069. __irq_enter();
  19070. @@ -340,6 +714,7 @@
  19071. static inline void invoke_softirq(void)
  19072. {
  19073. +#ifndef CONFIG_PREEMPT_RT_FULL
  19074. if (!force_irqthreads) {
  19075. #ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
  19076. /*
  19077. @@ -359,6 +734,15 @@
  19078. } else {
  19079. wakeup_softirqd();
  19080. }
  19081. +#else /* PREEMPT_RT_FULL */
  19082. + unsigned long flags;
  19083. +
  19084. + local_irq_save(flags);
  19085. + if (__this_cpu_read(ksoftirqd) &&
  19086. + __this_cpu_read(ksoftirqd)->softirqs_raised)
  19087. + wakeup_softirqd();
  19088. + local_irq_restore(flags);
  19089. +#endif
  19090. }
  19091. static inline void tick_irq_exit(void)
  19092. @@ -395,26 +779,6 @@
  19093. trace_hardirq_exit(); /* must be last! */
  19094. }
  19095. -/*
  19096. - * This function must run with irqs disabled!
  19097. - */
  19098. -inline void raise_softirq_irqoff(unsigned int nr)
  19099. -{
  19100. - __raise_softirq_irqoff(nr);
  19101. -
  19102. - /*
  19103. - * If we're in an interrupt or softirq, we're done
  19104. - * (this also catches softirq-disabled code). We will
  19105. - * actually run the softirq once we return from
  19106. - * the irq or softirq.
  19107. - *
  19108. - * Otherwise we wake up ksoftirqd to make sure we
  19109. - * schedule the softirq soon.
  19110. - */
  19111. - if (!in_interrupt())
  19112. - wakeup_softirqd();
  19113. -}
  19114. -
  19115. void raise_softirq(unsigned int nr)
  19116. {
  19117. unsigned long flags;
  19118. @@ -424,12 +788,6 @@
  19119. local_irq_restore(flags);
  19120. }
  19121. -void __raise_softirq_irqoff(unsigned int nr)
  19122. -{
  19123. - trace_softirq_raise(nr);
  19124. - or_softirq_pending(1UL << nr);
  19125. -}
  19126. -
  19127. void open_softirq(int nr, void (*action)(struct softirq_action *))
  19128. {
  19129. softirq_vec[nr].action = action;
  19130. @@ -446,15 +804,45 @@
  19131. static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
  19132. static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
  19133. +static void inline
  19134. +__tasklet_common_schedule(struct tasklet_struct *t, struct tasklet_head *head, unsigned int nr)
  19135. +{
  19136. + if (tasklet_trylock(t)) {
  19137. +again:
  19138. + /* We may have been preempted before tasklet_trylock
  19139. + * and __tasklet_action may have already run.
  19140. + * So double check the sched bit while the takslet
  19141. + * is locked before adding it to the list.
  19142. + */
  19143. + if (test_bit(TASKLET_STATE_SCHED, &t->state)) {
  19144. + t->next = NULL;
  19145. + *head->tail = t;
  19146. + head->tail = &(t->next);
  19147. + raise_softirq_irqoff(nr);
  19148. + tasklet_unlock(t);
  19149. + } else {
  19150. + /* This is subtle. If we hit the corner case above
  19151. + * It is possible that we get preempted right here,
  19152. + * and another task has successfully called
  19153. + * tasklet_schedule(), then this function, and
  19154. + * failed on the trylock. Thus we must be sure
  19155. + * before releasing the tasklet lock, that the
  19156. + * SCHED_BIT is clear. Otherwise the tasklet
  19157. + * may get its SCHED_BIT set, but not added to the
  19158. + * list
  19159. + */
  19160. + if (!tasklet_tryunlock(t))
  19161. + goto again;
  19162. + }
  19163. + }
  19164. +}
  19165. +
  19166. void __tasklet_schedule(struct tasklet_struct *t)
  19167. {
  19168. unsigned long flags;
  19169. local_irq_save(flags);
  19170. - t->next = NULL;
  19171. - *__this_cpu_read(tasklet_vec.tail) = t;
  19172. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  19173. - raise_softirq_irqoff(TASKLET_SOFTIRQ);
  19174. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
  19175. local_irq_restore(flags);
  19176. }
  19177. EXPORT_SYMBOL(__tasklet_schedule);
  19178. @@ -464,10 +852,7 @@
  19179. unsigned long flags;
  19180. local_irq_save(flags);
  19181. - t->next = NULL;
  19182. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  19183. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  19184. - raise_softirq_irqoff(HI_SOFTIRQ);
  19185. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
  19186. local_irq_restore(flags);
  19187. }
  19188. EXPORT_SYMBOL(__tasklet_hi_schedule);
  19189. @@ -476,82 +861,122 @@
  19190. {
  19191. BUG_ON(!irqs_disabled());
  19192. - t->next = __this_cpu_read(tasklet_hi_vec.head);
  19193. - __this_cpu_write(tasklet_hi_vec.head, t);
  19194. - __raise_softirq_irqoff(HI_SOFTIRQ);
  19195. + __tasklet_hi_schedule(t);
  19196. }
  19197. EXPORT_SYMBOL(__tasklet_hi_schedule_first);
  19198. -static void tasklet_action(struct softirq_action *a)
  19199. +void tasklet_enable(struct tasklet_struct *t)
  19200. {
  19201. - struct tasklet_struct *list;
  19202. + if (!atomic_dec_and_test(&t->count))
  19203. + return;
  19204. + if (test_and_clear_bit(TASKLET_STATE_PENDING, &t->state))
  19205. + tasklet_schedule(t);
  19206. +}
  19207. +EXPORT_SYMBOL(tasklet_enable);
  19208. - local_irq_disable();
  19209. - list = __this_cpu_read(tasklet_vec.head);
  19210. - __this_cpu_write(tasklet_vec.head, NULL);
  19211. - __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  19212. - local_irq_enable();
  19213. +static void __tasklet_action(struct softirq_action *a,
  19214. + struct tasklet_struct *list)
  19215. +{
  19216. + int loops = 1000000;
  19217. while (list) {
  19218. struct tasklet_struct *t = list;
  19219. list = list->next;
  19220. - if (tasklet_trylock(t)) {
  19221. - if (!atomic_read(&t->count)) {
  19222. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  19223. - &t->state))
  19224. - BUG();
  19225. - t->func(t->data);
  19226. - tasklet_unlock(t);
  19227. - continue;
  19228. - }
  19229. - tasklet_unlock(t);
  19230. + /*
  19231. + * Should always succeed - after a tasklist got on the
  19232. + * list (after getting the SCHED bit set from 0 to 1),
  19233. + * nothing but the tasklet softirq it got queued to can
  19234. + * lock it:
  19235. + */
  19236. + if (!tasklet_trylock(t)) {
  19237. + WARN_ON(1);
  19238. + continue;
  19239. }
  19240. - local_irq_disable();
  19241. t->next = NULL;
  19242. - *__this_cpu_read(tasklet_vec.tail) = t;
  19243. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  19244. - __raise_softirq_irqoff(TASKLET_SOFTIRQ);
  19245. - local_irq_enable();
  19246. +
  19247. + /*
  19248. + * If we cannot handle the tasklet because it's disabled,
  19249. + * mark it as pending. tasklet_enable() will later
  19250. + * re-schedule the tasklet.
  19251. + */
  19252. + if (unlikely(atomic_read(&t->count))) {
  19253. +out_disabled:
  19254. + /* implicit unlock: */
  19255. + wmb();
  19256. + t->state = TASKLET_STATEF_PENDING;
  19257. + continue;
  19258. + }
  19259. +
  19260. + /*
  19261. + * After this point on the tasklet might be rescheduled
  19262. + * on another CPU, but it can only be added to another
  19263. + * CPU's tasklet list if we unlock the tasklet (which we
  19264. + * dont do yet).
  19265. + */
  19266. + if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  19267. + WARN_ON(1);
  19268. +
  19269. +again:
  19270. + t->func(t->data);
  19271. +
  19272. + /*
  19273. + * Try to unlock the tasklet. We must use cmpxchg, because
  19274. + * another CPU might have scheduled or disabled the tasklet.
  19275. + * We only allow the STATE_RUN -> 0 transition here.
  19276. + */
  19277. + while (!tasklet_tryunlock(t)) {
  19278. + /*
  19279. + * If it got disabled meanwhile, bail out:
  19280. + */
  19281. + if (atomic_read(&t->count))
  19282. + goto out_disabled;
  19283. + /*
  19284. + * If it got scheduled meanwhile, re-execute
  19285. + * the tasklet function:
  19286. + */
  19287. + if (test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  19288. + goto again;
  19289. + if (!--loops) {
  19290. + printk("hm, tasklet state: %08lx\n", t->state);
  19291. + WARN_ON(1);
  19292. + tasklet_unlock(t);
  19293. + break;
  19294. + }
  19295. + }
  19296. }
  19297. }
  19298. +static void tasklet_action(struct softirq_action *a)
  19299. +{
  19300. + struct tasklet_struct *list;
  19301. +
  19302. + local_irq_disable();
  19303. +
  19304. + list = __this_cpu_read(tasklet_vec.head);
  19305. + __this_cpu_write(tasklet_vec.head, NULL);
  19306. + __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  19307. +
  19308. + local_irq_enable();
  19309. +
  19310. + __tasklet_action(a, list);
  19311. +}
  19312. +
  19313. static void tasklet_hi_action(struct softirq_action *a)
  19314. {
  19315. struct tasklet_struct *list;
  19316. local_irq_disable();
  19317. +
  19318. list = __this_cpu_read(tasklet_hi_vec.head);
  19319. __this_cpu_write(tasklet_hi_vec.head, NULL);
  19320. __this_cpu_write(tasklet_hi_vec.tail, this_cpu_ptr(&tasklet_hi_vec.head));
  19321. - local_irq_enable();
  19322. - while (list) {
  19323. - struct tasklet_struct *t = list;
  19324. -
  19325. - list = list->next;
  19326. -
  19327. - if (tasklet_trylock(t)) {
  19328. - if (!atomic_read(&t->count)) {
  19329. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  19330. - &t->state))
  19331. - BUG();
  19332. - t->func(t->data);
  19333. - tasklet_unlock(t);
  19334. - continue;
  19335. - }
  19336. - tasklet_unlock(t);
  19337. - }
  19338. + local_irq_enable();
  19339. - local_irq_disable();
  19340. - t->next = NULL;
  19341. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  19342. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  19343. - __raise_softirq_irqoff(HI_SOFTIRQ);
  19344. - local_irq_enable();
  19345. - }
  19346. + __tasklet_action(a, list);
  19347. }
  19348. void tasklet_init(struct tasklet_struct *t,
  19349. @@ -572,7 +997,7 @@
  19350. while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
  19351. do {
  19352. - yield();
  19353. + msleep(1);
  19354. } while (test_bit(TASKLET_STATE_SCHED, &t->state));
  19355. }
  19356. tasklet_unlock_wait(t);
  19357. @@ -646,25 +1071,26 @@
  19358. open_softirq(HI_SOFTIRQ, tasklet_hi_action);
  19359. }
  19360. -static int ksoftirqd_should_run(unsigned int cpu)
  19361. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  19362. +void tasklet_unlock_wait(struct tasklet_struct *t)
  19363. {
  19364. - return local_softirq_pending();
  19365. -}
  19366. -
  19367. -static void run_ksoftirqd(unsigned int cpu)
  19368. -{
  19369. - local_irq_disable();
  19370. - if (local_softirq_pending()) {
  19371. + while (test_bit(TASKLET_STATE_RUN, &(t)->state)) {
  19372. /*
  19373. - * We can safely run softirq on inline stack, as we are not deep
  19374. - * in the task stack here.
  19375. + * Hack for now to avoid this busy-loop:
  19376. */
  19377. - __do_softirq();
  19378. - local_irq_enable();
  19379. - cond_resched_rcu_qs();
  19380. - return;
  19381. +#ifdef CONFIG_PREEMPT_RT_FULL
  19382. + msleep(1);
  19383. +#else
  19384. + barrier();
  19385. +#endif
  19386. }
  19387. - local_irq_enable();
  19388. +}
  19389. +EXPORT_SYMBOL(tasklet_unlock_wait);
  19390. +#endif
  19391. +
  19392. +static int ksoftirqd_should_run(unsigned int cpu)
  19393. +{
  19394. + return ksoftirqd_softirq_pending();
  19395. }
  19396. #ifdef CONFIG_HOTPLUG_CPU
  19397. @@ -746,6 +1172,8 @@
  19398. static struct smp_hotplug_thread softirq_threads = {
  19399. .store = &ksoftirqd,
  19400. + .setup = ksoftirqd_set_sched_params,
  19401. + .cleanup = ksoftirqd_clr_sched_params,
  19402. .thread_should_run = ksoftirqd_should_run,
  19403. .thread_fn = run_ksoftirqd,
  19404. .thread_comm = "ksoftirqd/%u",
  19405. diff -Nur linux-4.1.13.orig/kernel/stop_machine.c linux-4.1.13/kernel/stop_machine.c
  19406. --- linux-4.1.13.orig/kernel/stop_machine.c 2015-11-09 23:34:10.000000000 +0100
  19407. +++ linux-4.1.13/kernel/stop_machine.c 2015-11-29 09:23:09.649611524 +0100
  19408. @@ -30,12 +30,12 @@
  19409. atomic_t nr_todo; /* nr left to execute */
  19410. bool executed; /* actually executed? */
  19411. int ret; /* collected return value */
  19412. - struct completion completion; /* fired if nr_todo reaches 0 */
  19413. + struct task_struct *waiter; /* woken when nr_todo reaches 0 */
  19414. };
  19415. /* the actual stopper, one per every possible cpu, enabled on online cpus */
  19416. struct cpu_stopper {
  19417. - spinlock_t lock;
  19418. + raw_spinlock_t lock;
  19419. bool enabled; /* is this stopper enabled? */
  19420. struct list_head works; /* list of pending works */
  19421. };
  19422. @@ -56,7 +56,7 @@
  19423. {
  19424. memset(done, 0, sizeof(*done));
  19425. atomic_set(&done->nr_todo, nr_todo);
  19426. - init_completion(&done->completion);
  19427. + done->waiter = current;
  19428. }
  19429. /* signal completion unless @done is NULL */
  19430. @@ -65,8 +65,10 @@
  19431. if (done) {
  19432. if (executed)
  19433. done->executed = true;
  19434. - if (atomic_dec_and_test(&done->nr_todo))
  19435. - complete(&done->completion);
  19436. + if (atomic_dec_and_test(&done->nr_todo)) {
  19437. + wake_up_process(done->waiter);
  19438. + done->waiter = NULL;
  19439. + }
  19440. }
  19441. }
  19442. @@ -78,7 +80,7 @@
  19443. unsigned long flags;
  19444. - spin_lock_irqsave(&stopper->lock, flags);
  19445. + raw_spin_lock_irqsave(&stopper->lock, flags);
  19446. if (stopper->enabled) {
  19447. list_add_tail(&work->list, &stopper->works);
  19448. @@ -86,7 +88,23 @@
  19449. } else
  19450. cpu_stop_signal_done(work->done, false);
  19451. - spin_unlock_irqrestore(&stopper->lock, flags);
  19452. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  19453. +}
  19454. +
  19455. +static void wait_for_stop_done(struct cpu_stop_done *done)
  19456. +{
  19457. + set_current_state(TASK_UNINTERRUPTIBLE);
  19458. + while (atomic_read(&done->nr_todo)) {
  19459. + schedule();
  19460. + set_current_state(TASK_UNINTERRUPTIBLE);
  19461. + }
  19462. + /*
  19463. + * We need to wait until cpu_stop_signal_done() has cleared
  19464. + * done->waiter.
  19465. + */
  19466. + while (done->waiter)
  19467. + cpu_relax();
  19468. + set_current_state(TASK_RUNNING);
  19469. }
  19470. /**
  19471. @@ -120,7 +138,7 @@
  19472. cpu_stop_init_done(&done, 1);
  19473. cpu_stop_queue_work(cpu, &work);
  19474. - wait_for_completion(&done.completion);
  19475. + wait_for_stop_done(&done);
  19476. return done.executed ? done.ret : -ENOENT;
  19477. }
  19478. @@ -248,7 +266,7 @@
  19479. struct irq_cpu_stop_queue_work_info call_args;
  19480. struct multi_stop_data msdata;
  19481. - preempt_disable();
  19482. + preempt_disable_nort();
  19483. msdata = (struct multi_stop_data){
  19484. .fn = fn,
  19485. .data = arg,
  19486. @@ -281,7 +299,7 @@
  19487. * This relies on the stopper workqueues to be FIFO.
  19488. */
  19489. if (!cpu_active(cpu1) || !cpu_active(cpu2)) {
  19490. - preempt_enable();
  19491. + preempt_enable_nort();
  19492. return -ENOENT;
  19493. }
  19494. @@ -295,9 +313,9 @@
  19495. &irq_cpu_stop_queue_work,
  19496. &call_args, 1);
  19497. lg_local_unlock(&stop_cpus_lock);
  19498. - preempt_enable();
  19499. + preempt_enable_nort();
  19500. - wait_for_completion(&done.completion);
  19501. + wait_for_stop_done(&done);
  19502. return done.executed ? done.ret : -ENOENT;
  19503. }
  19504. @@ -329,7 +347,7 @@
  19505. static void queue_stop_cpus_work(const struct cpumask *cpumask,
  19506. cpu_stop_fn_t fn, void *arg,
  19507. - struct cpu_stop_done *done)
  19508. + struct cpu_stop_done *done, bool inactive)
  19509. {
  19510. struct cpu_stop_work *work;
  19511. unsigned int cpu;
  19512. @@ -343,11 +361,13 @@
  19513. }
  19514. /*
  19515. - * Disable preemption while queueing to avoid getting
  19516. - * preempted by a stopper which might wait for other stoppers
  19517. - * to enter @fn which can lead to deadlock.
  19518. + * Make sure that all work is queued on all cpus before
  19519. + * any of the cpus can execute it.
  19520. */
  19521. - lg_global_lock(&stop_cpus_lock);
  19522. + if (!inactive)
  19523. + lg_global_lock(&stop_cpus_lock);
  19524. + else
  19525. + lg_global_trylock_relax(&stop_cpus_lock);
  19526. for_each_cpu(cpu, cpumask)
  19527. cpu_stop_queue_work(cpu, &per_cpu(stop_cpus_work, cpu));
  19528. lg_global_unlock(&stop_cpus_lock);
  19529. @@ -359,8 +379,8 @@
  19530. struct cpu_stop_done done;
  19531. cpu_stop_init_done(&done, cpumask_weight(cpumask));
  19532. - queue_stop_cpus_work(cpumask, fn, arg, &done);
  19533. - wait_for_completion(&done.completion);
  19534. + queue_stop_cpus_work(cpumask, fn, arg, &done, false);
  19535. + wait_for_stop_done(&done);
  19536. return done.executed ? done.ret : -ENOENT;
  19537. }
  19538. @@ -439,9 +459,9 @@
  19539. unsigned long flags;
  19540. int run;
  19541. - spin_lock_irqsave(&stopper->lock, flags);
  19542. + raw_spin_lock_irqsave(&stopper->lock, flags);
  19543. run = !list_empty(&stopper->works);
  19544. - spin_unlock_irqrestore(&stopper->lock, flags);
  19545. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  19546. return run;
  19547. }
  19548. @@ -453,13 +473,13 @@
  19549. repeat:
  19550. work = NULL;
  19551. - spin_lock_irq(&stopper->lock);
  19552. + raw_spin_lock_irq(&stopper->lock);
  19553. if (!list_empty(&stopper->works)) {
  19554. work = list_first_entry(&stopper->works,
  19555. struct cpu_stop_work, list);
  19556. list_del_init(&work->list);
  19557. }
  19558. - spin_unlock_irq(&stopper->lock);
  19559. + raw_spin_unlock_irq(&stopper->lock);
  19560. if (work) {
  19561. cpu_stop_fn_t fn = work->fn;
  19562. @@ -467,6 +487,16 @@
  19563. struct cpu_stop_done *done = work->done;
  19564. char ksym_buf[KSYM_NAME_LEN] __maybe_unused;
  19565. + /*
  19566. + * Wait until the stopper finished scheduling on all
  19567. + * cpus
  19568. + */
  19569. + lg_global_lock(&stop_cpus_lock);
  19570. + /*
  19571. + * Let other cpu threads continue as well
  19572. + */
  19573. + lg_global_unlock(&stop_cpus_lock);
  19574. +
  19575. /* cpu stop callbacks are not allowed to sleep */
  19576. preempt_disable();
  19577. @@ -481,7 +511,13 @@
  19578. kallsyms_lookup((unsigned long)fn, NULL, NULL, NULL,
  19579. ksym_buf), arg);
  19580. + /*
  19581. + * Make sure that the wakeup and setting done->waiter
  19582. + * to NULL is atomic.
  19583. + */
  19584. + local_irq_disable();
  19585. cpu_stop_signal_done(done, true);
  19586. + local_irq_enable();
  19587. goto repeat;
  19588. }
  19589. }
  19590. @@ -500,20 +536,20 @@
  19591. unsigned long flags;
  19592. /* drain remaining works */
  19593. - spin_lock_irqsave(&stopper->lock, flags);
  19594. + raw_spin_lock_irqsave(&stopper->lock, flags);
  19595. list_for_each_entry(work, &stopper->works, list)
  19596. cpu_stop_signal_done(work->done, false);
  19597. stopper->enabled = false;
  19598. - spin_unlock_irqrestore(&stopper->lock, flags);
  19599. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  19600. }
  19601. static void cpu_stop_unpark(unsigned int cpu)
  19602. {
  19603. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  19604. - spin_lock_irq(&stopper->lock);
  19605. + raw_spin_lock_irq(&stopper->lock);
  19606. stopper->enabled = true;
  19607. - spin_unlock_irq(&stopper->lock);
  19608. + raw_spin_unlock_irq(&stopper->lock);
  19609. }
  19610. static struct smp_hotplug_thread cpu_stop_threads = {
  19611. @@ -535,10 +571,12 @@
  19612. for_each_possible_cpu(cpu) {
  19613. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  19614. - spin_lock_init(&stopper->lock);
  19615. + raw_spin_lock_init(&stopper->lock);
  19616. INIT_LIST_HEAD(&stopper->works);
  19617. }
  19618. + lg_lock_init(&stop_cpus_lock, "stop_cpus_lock");
  19619. +
  19620. BUG_ON(smpboot_register_percpu_thread(&cpu_stop_threads));
  19621. stop_machine_initialized = true;
  19622. return 0;
  19623. @@ -634,11 +672,11 @@
  19624. set_state(&msdata, MULTI_STOP_PREPARE);
  19625. cpu_stop_init_done(&done, num_active_cpus());
  19626. queue_stop_cpus_work(cpu_active_mask, multi_cpu_stop, &msdata,
  19627. - &done);
  19628. + &done, true);
  19629. ret = multi_cpu_stop(&msdata);
  19630. /* Busy wait for completion. */
  19631. - while (!completion_done(&done.completion))
  19632. + while (atomic_read(&done.nr_todo))
  19633. cpu_relax();
  19634. mutex_unlock(&stop_cpus_mutex);
  19635. diff -Nur linux-4.1.13.orig/kernel/time/hrtimer.c linux-4.1.13/kernel/time/hrtimer.c
  19636. --- linux-4.1.13.orig/kernel/time/hrtimer.c 2015-11-09 23:34:10.000000000 +0100
  19637. +++ linux-4.1.13/kernel/time/hrtimer.c 2015-11-29 09:23:09.649611524 +0100
  19638. @@ -48,11 +48,13 @@
  19639. #include <linux/sched/rt.h>
  19640. #include <linux/sched/deadline.h>
  19641. #include <linux/timer.h>
  19642. +#include <linux/kthread.h>
  19643. #include <linux/freezer.h>
  19644. #include <asm/uaccess.h>
  19645. #include <trace/events/timer.h>
  19646. +#include <trace/events/hist.h>
  19647. #include "tick-internal.h"
  19648. @@ -576,8 +578,7 @@
  19649. * When the callback is running, we do not reprogram the clock event
  19650. * device. The timer callback is either running on a different CPU or
  19651. * the callback is executed in the hrtimer_interrupt context. The
  19652. - * reprogramming is handled either by the softirq, which called the
  19653. - * callback or at the end of the hrtimer_interrupt.
  19654. + * reprogramming is handled at the end of the hrtimer_interrupt.
  19655. */
  19656. if (hrtimer_callback_running(timer))
  19657. return 0;
  19658. @@ -621,6 +622,9 @@
  19659. return res;
  19660. }
  19661. +static void __run_hrtimer(struct hrtimer *timer, ktime_t *now);
  19662. +static int hrtimer_rt_defer(struct hrtimer *timer);
  19663. +
  19664. /*
  19665. * Initialize the high resolution related parts of cpu_base
  19666. */
  19667. @@ -630,6 +634,21 @@
  19668. base->hres_active = 0;
  19669. }
  19670. +static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  19671. + struct hrtimer_clock_base *base,
  19672. + int wakeup)
  19673. +{
  19674. + if (!hrtimer_reprogram(timer, base))
  19675. + return 0;
  19676. + if (!wakeup)
  19677. + return -ETIME;
  19678. +#ifdef CONFIG_PREEMPT_RT_BASE
  19679. + if (!hrtimer_rt_defer(timer))
  19680. + return -ETIME;
  19681. +#endif
  19682. + return 1;
  19683. +}
  19684. +
  19685. static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
  19686. {
  19687. ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
  19688. @@ -695,6 +714,44 @@
  19689. static DECLARE_WORK(hrtimer_work, clock_was_set_work);
  19690. +#ifdef CONFIG_PREEMPT_RT_FULL
  19691. +/*
  19692. + * RT can not call schedule_work from real interrupt context.
  19693. + * Need to make a thread to do the real work.
  19694. + */
  19695. +static struct task_struct *clock_set_delay_thread;
  19696. +static bool do_clock_set_delay;
  19697. +
  19698. +static int run_clock_set_delay(void *ignore)
  19699. +{
  19700. + while (!kthread_should_stop()) {
  19701. + set_current_state(TASK_INTERRUPTIBLE);
  19702. + if (do_clock_set_delay) {
  19703. + do_clock_set_delay = false;
  19704. + schedule_work(&hrtimer_work);
  19705. + }
  19706. + schedule();
  19707. + }
  19708. + __set_current_state(TASK_RUNNING);
  19709. + return 0;
  19710. +}
  19711. +
  19712. +void clock_was_set_delayed(void)
  19713. +{
  19714. + do_clock_set_delay = true;
  19715. + /* Make visible before waking up process */
  19716. + smp_wmb();
  19717. + wake_up_process(clock_set_delay_thread);
  19718. +}
  19719. +
  19720. +static __init int create_clock_set_delay_thread(void)
  19721. +{
  19722. + clock_set_delay_thread = kthread_run(run_clock_set_delay, NULL, "kclksetdelayd");
  19723. + BUG_ON(!clock_set_delay_thread);
  19724. + return 0;
  19725. +}
  19726. +early_initcall(create_clock_set_delay_thread);
  19727. +#else /* PREEMPT_RT_FULL */
  19728. /*
  19729. * Called from timekeeping and resume code to reprogramm the hrtimer
  19730. * interrupt device on all cpus.
  19731. @@ -703,6 +760,7 @@
  19732. {
  19733. schedule_work(&hrtimer_work);
  19734. }
  19735. +#endif
  19736. #else
  19737. @@ -711,6 +769,13 @@
  19738. static inline int hrtimer_switch_to_hres(void) { return 0; }
  19739. static inline void
  19740. hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
  19741. +static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  19742. + struct hrtimer_clock_base *base,
  19743. + int wakeup)
  19744. +{
  19745. + return 0;
  19746. +}
  19747. +
  19748. static inline int hrtimer_reprogram(struct hrtimer *timer,
  19749. struct hrtimer_clock_base *base)
  19750. {
  19751. @@ -718,7 +783,6 @@
  19752. }
  19753. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  19754. static inline void retrigger_next_event(void *arg) { }
  19755. -
  19756. #endif /* CONFIG_HIGH_RES_TIMERS */
  19757. /*
  19758. @@ -836,6 +900,32 @@
  19759. }
  19760. EXPORT_SYMBOL_GPL(hrtimer_forward);
  19761. +#ifdef CONFIG_PREEMPT_RT_BASE
  19762. +# define wake_up_timer_waiters(b) wake_up(&(b)->wait)
  19763. +
  19764. +/**
  19765. + * hrtimer_wait_for_timer - Wait for a running timer
  19766. + *
  19767. + * @timer: timer to wait for
  19768. + *
  19769. + * The function waits in case the timers callback function is
  19770. + * currently executed on the waitqueue of the timer base. The
  19771. + * waitqueue is woken up after the timer callback function has
  19772. + * finished execution.
  19773. + */
  19774. +void hrtimer_wait_for_timer(const struct hrtimer *timer)
  19775. +{
  19776. + struct hrtimer_clock_base *base = timer->base;
  19777. +
  19778. + if (base && base->cpu_base && !timer->irqsafe)
  19779. + wait_event(base->cpu_base->wait,
  19780. + !(timer->state & HRTIMER_STATE_CALLBACK));
  19781. +}
  19782. +
  19783. +#else
  19784. +# define wake_up_timer_waiters(b) do { } while (0)
  19785. +#endif
  19786. +
  19787. /*
  19788. * enqueue_hrtimer - internal function to (re)start a timer
  19789. *
  19790. @@ -879,6 +969,11 @@
  19791. if (!(timer->state & HRTIMER_STATE_ENQUEUED))
  19792. goto out;
  19793. + if (unlikely(!list_empty(&timer->cb_entry))) {
  19794. + list_del_init(&timer->cb_entry);
  19795. + goto out;
  19796. + }
  19797. +
  19798. next_timer = timerqueue_getnext(&base->active);
  19799. timerqueue_del(&base->active, &timer->node);
  19800. if (&timer->node == next_timer) {
  19801. @@ -966,7 +1061,16 @@
  19802. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  19803. timer_stats_hrtimer_set_start_info(timer);
  19804. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  19805. + {
  19806. + ktime_t now = new_base->get_time();
  19807. + if (ktime_to_ns(tim) < ktime_to_ns(now))
  19808. + timer->praecox = now;
  19809. + else
  19810. + timer->praecox = ktime_set(0, 0);
  19811. + }
  19812. +#endif
  19813. leftmost = enqueue_hrtimer(timer, new_base);
  19814. if (!leftmost) {
  19815. @@ -980,15 +1084,26 @@
  19816. * on dynticks target.
  19817. */
  19818. wake_up_nohz_cpu(new_base->cpu_base->cpu);
  19819. - } else if (new_base->cpu_base == this_cpu_ptr(&hrtimer_bases) &&
  19820. - hrtimer_reprogram(timer, new_base)) {
  19821. + } else if (new_base->cpu_base == this_cpu_ptr(&hrtimer_bases)) {
  19822. +
  19823. + ret = hrtimer_enqueue_reprogram(timer, new_base, wakeup);
  19824. + if (ret < 0) {
  19825. + /*
  19826. + * In case we failed to reprogram the timer (mostly
  19827. + * because out current timer is already elapsed),
  19828. + * remove it again and report a failure. This avoids
  19829. + * stale base->first entries.
  19830. + */
  19831. + debug_deactivate(timer);
  19832. + __remove_hrtimer(timer, new_base,
  19833. + timer->state & HRTIMER_STATE_CALLBACK, 0);
  19834. + } else if (ret > 0) {
  19835. /*
  19836. * Only allow reprogramming if the new base is on this CPU.
  19837. * (it might still be on another CPU if the timer was pending)
  19838. *
  19839. * XXX send_remote_softirq() ?
  19840. */
  19841. - if (wakeup) {
  19842. /*
  19843. * We need to drop cpu_base->lock to avoid a
  19844. * lock ordering issue vs. rq->lock.
  19845. @@ -996,9 +1111,7 @@
  19846. raw_spin_unlock(&new_base->cpu_base->lock);
  19847. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  19848. local_irq_restore(flags);
  19849. - return ret;
  19850. - } else {
  19851. - __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  19852. + return 0;
  19853. }
  19854. }
  19855. @@ -1089,7 +1202,7 @@
  19856. if (ret >= 0)
  19857. return ret;
  19858. - cpu_relax();
  19859. + hrtimer_wait_for_timer(timer);
  19860. }
  19861. }
  19862. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  19863. @@ -1153,6 +1266,7 @@
  19864. base = hrtimer_clockid_to_base(clock_id);
  19865. timer->base = &cpu_base->clock_base[base];
  19866. + INIT_LIST_HEAD(&timer->cb_entry);
  19867. timerqueue_init(&timer->node);
  19868. #ifdef CONFIG_TIMER_STATS
  19869. @@ -1236,6 +1350,126 @@
  19870. timer->state &= ~HRTIMER_STATE_CALLBACK;
  19871. }
  19872. +static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer);
  19873. +
  19874. +#ifdef CONFIG_PREEMPT_RT_BASE
  19875. +static void hrtimer_rt_reprogram(int restart, struct hrtimer *timer,
  19876. + struct hrtimer_clock_base *base)
  19877. +{
  19878. + /*
  19879. + * Note, we clear the callback flag before we requeue the
  19880. + * timer otherwise we trigger the callback_running() check
  19881. + * in hrtimer_reprogram().
  19882. + */
  19883. + timer->state &= ~HRTIMER_STATE_CALLBACK;
  19884. +
  19885. + if (restart != HRTIMER_NORESTART) {
  19886. + BUG_ON(hrtimer_active(timer));
  19887. + /*
  19888. + * Enqueue the timer, if it's the leftmost timer then
  19889. + * we need to reprogram it.
  19890. + */
  19891. + if (!enqueue_hrtimer(timer, base))
  19892. + return;
  19893. +
  19894. +#ifndef CONFIG_HIGH_RES_TIMERS
  19895. + }
  19896. +#else
  19897. + if (base->cpu_base->hres_active &&
  19898. + hrtimer_reprogram(timer, base))
  19899. + goto requeue;
  19900. +
  19901. + } else if (hrtimer_active(timer)) {
  19902. + /*
  19903. + * If the timer was rearmed on another CPU, reprogram
  19904. + * the event device.
  19905. + */
  19906. + if (&timer->node == base->active.next &&
  19907. + base->cpu_base->hres_active &&
  19908. + hrtimer_reprogram(timer, base))
  19909. + goto requeue;
  19910. + }
  19911. + return;
  19912. +
  19913. +requeue:
  19914. + /*
  19915. + * Timer is expired. Thus move it from tree to pending list
  19916. + * again.
  19917. + */
  19918. + __remove_hrtimer(timer, base, timer->state, 0);
  19919. + list_add_tail(&timer->cb_entry, &base->expired);
  19920. +#endif
  19921. +}
  19922. +
  19923. +/*
  19924. + * The changes in mainline which removed the callback modes from
  19925. + * hrtimer are not yet working with -rt. The non wakeup_process()
  19926. + * based callbacks which involve sleeping locks need to be treated
  19927. + * seperately.
  19928. + */
  19929. +static void hrtimer_rt_run_pending(void)
  19930. +{
  19931. + enum hrtimer_restart (*fn)(struct hrtimer *);
  19932. + struct hrtimer_cpu_base *cpu_base;
  19933. + struct hrtimer_clock_base *base;
  19934. + struct hrtimer *timer;
  19935. + int index, restart;
  19936. +
  19937. + local_irq_disable();
  19938. + cpu_base = &per_cpu(hrtimer_bases, smp_processor_id());
  19939. +
  19940. + raw_spin_lock(&cpu_base->lock);
  19941. +
  19942. + for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  19943. + base = &cpu_base->clock_base[index];
  19944. +
  19945. + while (!list_empty(&base->expired)) {
  19946. + timer = list_first_entry(&base->expired,
  19947. + struct hrtimer, cb_entry);
  19948. +
  19949. + /*
  19950. + * Same as the above __run_hrtimer function
  19951. + * just we run with interrupts enabled.
  19952. + */
  19953. + debug_hrtimer_deactivate(timer);
  19954. + __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  19955. + timer_stats_account_hrtimer(timer);
  19956. + fn = timer->function;
  19957. +
  19958. + raw_spin_unlock_irq(&cpu_base->lock);
  19959. + restart = fn(timer);
  19960. + raw_spin_lock_irq(&cpu_base->lock);
  19961. +
  19962. + hrtimer_rt_reprogram(restart, timer, base);
  19963. + }
  19964. + }
  19965. +
  19966. + raw_spin_unlock_irq(&cpu_base->lock);
  19967. +
  19968. + wake_up_timer_waiters(cpu_base);
  19969. +}
  19970. +
  19971. +static int hrtimer_rt_defer(struct hrtimer *timer)
  19972. +{
  19973. + if (timer->irqsafe)
  19974. + return 0;
  19975. +
  19976. + __remove_hrtimer(timer, timer->base, timer->state, 0);
  19977. + list_add_tail(&timer->cb_entry, &timer->base->expired);
  19978. + return 1;
  19979. +}
  19980. +
  19981. +#else
  19982. +
  19983. +static inline void hrtimer_rt_run_pending(void)
  19984. +{
  19985. + hrtimer_peek_ahead_timers();
  19986. +}
  19987. +
  19988. +static inline int hrtimer_rt_defer(struct hrtimer *timer) { return 0; }
  19989. +
  19990. +#endif
  19991. +
  19992. #ifdef CONFIG_HIGH_RES_TIMERS
  19993. /*
  19994. @@ -1246,7 +1480,7 @@
  19995. {
  19996. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  19997. ktime_t expires_next, now, entry_time, delta;
  19998. - int i, retries = 0;
  19999. + int i, retries = 0, raise = 0;
  20000. BUG_ON(!cpu_base->hres_active);
  20001. cpu_base->nr_events++;
  20002. @@ -1281,6 +1515,15 @@
  20003. timer = container_of(node, struct hrtimer, node);
  20004. + trace_hrtimer_interrupt(raw_smp_processor_id(),
  20005. + ktime_to_ns(ktime_sub(ktime_to_ns(timer->praecox) ?
  20006. + timer->praecox : hrtimer_get_expires(timer),
  20007. + basenow)),
  20008. + current,
  20009. + timer->function == hrtimer_wakeup ?
  20010. + container_of(timer, struct hrtimer_sleeper,
  20011. + timer)->task : NULL);
  20012. +
  20013. /*
  20014. * The immediate goal for using the softexpires is
  20015. * minimizing wakeups, not running timers at the
  20016. @@ -1296,7 +1539,10 @@
  20017. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer))
  20018. break;
  20019. - __run_hrtimer(timer, &basenow);
  20020. + if (!hrtimer_rt_defer(timer))
  20021. + __run_hrtimer(timer, &basenow);
  20022. + else
  20023. + raise = 1;
  20024. }
  20025. }
  20026. /* Reevaluate the clock bases for the next expiry */
  20027. @@ -1313,7 +1559,7 @@
  20028. if (expires_next.tv64 == KTIME_MAX ||
  20029. !tick_program_event(expires_next, 0)) {
  20030. cpu_base->hang_detected = 0;
  20031. - return;
  20032. + goto out;
  20033. }
  20034. /*
  20035. @@ -1357,6 +1603,9 @@
  20036. tick_program_event(expires_next, 1);
  20037. printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
  20038. ktime_to_ns(delta));
  20039. +out:
  20040. + if (raise)
  20041. + raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  20042. }
  20043. /*
  20044. @@ -1392,18 +1641,18 @@
  20045. __hrtimer_peek_ahead_timers();
  20046. local_irq_restore(flags);
  20047. }
  20048. -
  20049. -static void run_hrtimer_softirq(struct softirq_action *h)
  20050. -{
  20051. - hrtimer_peek_ahead_timers();
  20052. -}
  20053. -
  20054. #else /* CONFIG_HIGH_RES_TIMERS */
  20055. static inline void __hrtimer_peek_ahead_timers(void) { }
  20056. #endif /* !CONFIG_HIGH_RES_TIMERS */
  20057. +
  20058. +static void run_hrtimer_softirq(struct softirq_action *h)
  20059. +{
  20060. + hrtimer_rt_run_pending();
  20061. +}
  20062. +
  20063. /*
  20064. * Called from timer softirq every jiffy, expire hrtimers:
  20065. *
  20066. @@ -1436,7 +1685,7 @@
  20067. struct timerqueue_node *node;
  20068. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  20069. struct hrtimer_clock_base *base;
  20070. - int index, gettime = 1;
  20071. + int index, gettime = 1, raise = 0;
  20072. if (hrtimer_hres_active())
  20073. return;
  20074. @@ -1461,10 +1710,16 @@
  20075. hrtimer_get_expires_tv64(timer))
  20076. break;
  20077. - __run_hrtimer(timer, &base->softirq_time);
  20078. + if (!hrtimer_rt_defer(timer))
  20079. + __run_hrtimer(timer, &base->softirq_time);
  20080. + else
  20081. + raise = 1;
  20082. }
  20083. raw_spin_unlock(&cpu_base->lock);
  20084. }
  20085. +
  20086. + if (raise)
  20087. + raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  20088. }
  20089. /*
  20090. @@ -1486,16 +1741,18 @@
  20091. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  20092. {
  20093. sl->timer.function = hrtimer_wakeup;
  20094. + sl->timer.irqsafe = 1;
  20095. sl->task = task;
  20096. }
  20097. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  20098. -static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  20099. +static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode,
  20100. + unsigned long state)
  20101. {
  20102. hrtimer_init_sleeper(t, current);
  20103. do {
  20104. - set_current_state(TASK_INTERRUPTIBLE);
  20105. + set_current_state(state);
  20106. hrtimer_start_expires(&t->timer, mode);
  20107. if (!hrtimer_active(&t->timer))
  20108. t->task = NULL;
  20109. @@ -1539,7 +1796,8 @@
  20110. HRTIMER_MODE_ABS);
  20111. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  20112. - if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  20113. + /* cpu_chill() does not care about restart state. */
  20114. + if (do_nanosleep(&t, HRTIMER_MODE_ABS, TASK_INTERRUPTIBLE))
  20115. goto out;
  20116. rmtp = restart->nanosleep.rmtp;
  20117. @@ -1556,8 +1814,10 @@
  20118. return ret;
  20119. }
  20120. -long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  20121. - const enum hrtimer_mode mode, const clockid_t clockid)
  20122. +static long
  20123. +__hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  20124. + const enum hrtimer_mode mode, const clockid_t clockid,
  20125. + unsigned long state)
  20126. {
  20127. struct restart_block *restart;
  20128. struct hrtimer_sleeper t;
  20129. @@ -1570,7 +1830,7 @@
  20130. hrtimer_init_on_stack(&t.timer, clockid, mode);
  20131. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  20132. - if (do_nanosleep(&t, mode))
  20133. + if (do_nanosleep(&t, mode, state))
  20134. goto out;
  20135. /* Absolute timers do not update the rmtp value and restart: */
  20136. @@ -1597,6 +1857,12 @@
  20137. return ret;
  20138. }
  20139. +long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  20140. + const enum hrtimer_mode mode, const clockid_t clockid)
  20141. +{
  20142. + return __hrtimer_nanosleep(rqtp, rmtp, mode, clockid, TASK_INTERRUPTIBLE);
  20143. +}
  20144. +
  20145. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  20146. struct timespec __user *, rmtp)
  20147. {
  20148. @@ -1611,6 +1877,26 @@
  20149. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  20150. }
  20151. +#ifdef CONFIG_PREEMPT_RT_FULL
  20152. +/*
  20153. + * Sleep for 1 ms in hope whoever holds what we want will let it go.
  20154. + */
  20155. +void cpu_chill(void)
  20156. +{
  20157. + struct timespec tu = {
  20158. + .tv_nsec = NSEC_PER_MSEC,
  20159. + };
  20160. + unsigned int freeze_flag = current->flags & PF_NOFREEZE;
  20161. +
  20162. + current->flags |= PF_NOFREEZE;
  20163. + __hrtimer_nanosleep(&tu, NULL, HRTIMER_MODE_REL, CLOCK_MONOTONIC,
  20164. + TASK_UNINTERRUPTIBLE);
  20165. + if (!freeze_flag)
  20166. + current->flags &= ~PF_NOFREEZE;
  20167. +}
  20168. +EXPORT_SYMBOL(cpu_chill);
  20169. +#endif
  20170. +
  20171. /*
  20172. * Functions related to boot-time initialization:
  20173. */
  20174. @@ -1622,10 +1908,14 @@
  20175. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  20176. cpu_base->clock_base[i].cpu_base = cpu_base;
  20177. timerqueue_init_head(&cpu_base->clock_base[i].active);
  20178. + INIT_LIST_HEAD(&cpu_base->clock_base[i].expired);
  20179. }
  20180. cpu_base->cpu = cpu;
  20181. hrtimer_init_hres(cpu_base);
  20182. +#ifdef CONFIG_PREEMPT_RT_BASE
  20183. + init_waitqueue_head(&cpu_base->wait);
  20184. +#endif
  20185. }
  20186. #ifdef CONFIG_HOTPLUG_CPU
  20187. @@ -1731,9 +2021,7 @@
  20188. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  20189. (void *)(long)smp_processor_id());
  20190. register_cpu_notifier(&hrtimers_nb);
  20191. -#ifdef CONFIG_HIGH_RES_TIMERS
  20192. open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  20193. -#endif
  20194. }
  20195. /**
  20196. diff -Nur linux-4.1.13.orig/kernel/time/itimer.c linux-4.1.13/kernel/time/itimer.c
  20197. --- linux-4.1.13.orig/kernel/time/itimer.c 2015-11-09 23:34:10.000000000 +0100
  20198. +++ linux-4.1.13/kernel/time/itimer.c 2015-11-29 09:23:09.649611524 +0100
  20199. @@ -213,6 +213,7 @@
  20200. /* We are sharing ->siglock with it_real_fn() */
  20201. if (hrtimer_try_to_cancel(timer) < 0) {
  20202. spin_unlock_irq(&tsk->sighand->siglock);
  20203. + hrtimer_wait_for_timer(&tsk->signal->real_timer);
  20204. goto again;
  20205. }
  20206. expires = timeval_to_ktime(value->it_value);
  20207. diff -Nur linux-4.1.13.orig/kernel/time/jiffies.c linux-4.1.13/kernel/time/jiffies.c
  20208. --- linux-4.1.13.orig/kernel/time/jiffies.c 2015-11-09 23:34:10.000000000 +0100
  20209. +++ linux-4.1.13/kernel/time/jiffies.c 2015-11-29 09:23:09.649611524 +0100
  20210. @@ -74,7 +74,8 @@
  20211. .max_cycles = 10,
  20212. };
  20213. -__cacheline_aligned_in_smp DEFINE_SEQLOCK(jiffies_lock);
  20214. +__cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock);
  20215. +__cacheline_aligned_in_smp seqcount_t jiffies_seq;
  20216. #if (BITS_PER_LONG < 64)
  20217. u64 get_jiffies_64(void)
  20218. @@ -83,9 +84,9 @@
  20219. u64 ret;
  20220. do {
  20221. - seq = read_seqbegin(&jiffies_lock);
  20222. + seq = read_seqcount_begin(&jiffies_seq);
  20223. ret = jiffies_64;
  20224. - } while (read_seqretry(&jiffies_lock, seq));
  20225. + } while (read_seqcount_retry(&jiffies_seq, seq));
  20226. return ret;
  20227. }
  20228. EXPORT_SYMBOL(get_jiffies_64);
  20229. diff -Nur linux-4.1.13.orig/kernel/time/ntp.c linux-4.1.13/kernel/time/ntp.c
  20230. --- linux-4.1.13.orig/kernel/time/ntp.c 2015-11-09 23:34:10.000000000 +0100
  20231. +++ linux-4.1.13/kernel/time/ntp.c 2015-11-29 09:23:09.649611524 +0100
  20232. @@ -10,6 +10,7 @@
  20233. #include <linux/workqueue.h>
  20234. #include <linux/hrtimer.h>
  20235. #include <linux/jiffies.h>
  20236. +#include <linux/kthread.h>
  20237. #include <linux/math64.h>
  20238. #include <linux/timex.h>
  20239. #include <linux/time.h>
  20240. @@ -529,10 +530,52 @@
  20241. &sync_cmos_work, timespec_to_jiffies(&next));
  20242. }
  20243. +#ifdef CONFIG_PREEMPT_RT_FULL
  20244. +/*
  20245. + * RT can not call schedule_delayed_work from real interrupt context.
  20246. + * Need to make a thread to do the real work.
  20247. + */
  20248. +static struct task_struct *cmos_delay_thread;
  20249. +static bool do_cmos_delay;
  20250. +
  20251. +static int run_cmos_delay(void *ignore)
  20252. +{
  20253. + while (!kthread_should_stop()) {
  20254. + set_current_state(TASK_INTERRUPTIBLE);
  20255. + if (do_cmos_delay) {
  20256. + do_cmos_delay = false;
  20257. + queue_delayed_work(system_power_efficient_wq,
  20258. + &sync_cmos_work, 0);
  20259. + }
  20260. + schedule();
  20261. + }
  20262. + __set_current_state(TASK_RUNNING);
  20263. + return 0;
  20264. +}
  20265. +
  20266. +void ntp_notify_cmos_timer(void)
  20267. +{
  20268. + do_cmos_delay = true;
  20269. + /* Make visible before waking up process */
  20270. + smp_wmb();
  20271. + wake_up_process(cmos_delay_thread);
  20272. +}
  20273. +
  20274. +static __init int create_cmos_delay_thread(void)
  20275. +{
  20276. + cmos_delay_thread = kthread_run(run_cmos_delay, NULL, "kcmosdelayd");
  20277. + BUG_ON(!cmos_delay_thread);
  20278. + return 0;
  20279. +}
  20280. +early_initcall(create_cmos_delay_thread);
  20281. +
  20282. +#else
  20283. +
  20284. void ntp_notify_cmos_timer(void)
  20285. {
  20286. queue_delayed_work(system_power_efficient_wq, &sync_cmos_work, 0);
  20287. }
  20288. +#endif /* CONFIG_PREEMPT_RT_FULL */
  20289. #else
  20290. void ntp_notify_cmos_timer(void) { }
  20291. diff -Nur linux-4.1.13.orig/kernel/time/posix-cpu-timers.c linux-4.1.13/kernel/time/posix-cpu-timers.c
  20292. --- linux-4.1.13.orig/kernel/time/posix-cpu-timers.c 2015-11-09 23:34:10.000000000 +0100
  20293. +++ linux-4.1.13/kernel/time/posix-cpu-timers.c 2015-11-29 09:23:09.649611524 +0100
  20294. @@ -3,6 +3,7 @@
  20295. */
  20296. #include <linux/sched.h>
  20297. +#include <linux/sched/rt.h>
  20298. #include <linux/posix-timers.h>
  20299. #include <linux/errno.h>
  20300. #include <linux/math64.h>
  20301. @@ -626,7 +627,7 @@
  20302. /*
  20303. * Disarm any old timer after extracting its expiry time.
  20304. */
  20305. - WARN_ON_ONCE(!irqs_disabled());
  20306. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  20307. ret = 0;
  20308. old_incr = timer->it.cpu.incr;
  20309. @@ -1047,7 +1048,7 @@
  20310. /*
  20311. * Now re-arm for the new expiry time.
  20312. */
  20313. - WARN_ON_ONCE(!irqs_disabled());
  20314. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  20315. arm_timer(timer);
  20316. unlock_task_sighand(p, &flags);
  20317. @@ -1113,10 +1114,11 @@
  20318. sig = tsk->signal;
  20319. if (sig->cputimer.running) {
  20320. struct task_cputime group_sample;
  20321. + unsigned long flags;
  20322. - raw_spin_lock(&sig->cputimer.lock);
  20323. + raw_spin_lock_irqsave(&sig->cputimer.lock, flags);
  20324. group_sample = sig->cputimer.cputime;
  20325. - raw_spin_unlock(&sig->cputimer.lock);
  20326. + raw_spin_unlock_irqrestore(&sig->cputimer.lock, flags);
  20327. if (task_cputime_expired(&group_sample, &sig->cputime_expires))
  20328. return 1;
  20329. @@ -1130,13 +1132,13 @@
  20330. * already updated our counts. We need to check if any timers fire now.
  20331. * Interrupts are disabled.
  20332. */
  20333. -void run_posix_cpu_timers(struct task_struct *tsk)
  20334. +static void __run_posix_cpu_timers(struct task_struct *tsk)
  20335. {
  20336. LIST_HEAD(firing);
  20337. struct k_itimer *timer, *next;
  20338. unsigned long flags;
  20339. - WARN_ON_ONCE(!irqs_disabled());
  20340. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  20341. /*
  20342. * The fast path checks that there are no expired thread or thread
  20343. @@ -1194,6 +1196,190 @@
  20344. }
  20345. }
  20346. +#ifdef CONFIG_PREEMPT_RT_BASE
  20347. +#include <linux/kthread.h>
  20348. +#include <linux/cpu.h>
  20349. +DEFINE_PER_CPU(struct task_struct *, posix_timer_task);
  20350. +DEFINE_PER_CPU(struct task_struct *, posix_timer_tasklist);
  20351. +
  20352. +static int posix_cpu_timers_thread(void *data)
  20353. +{
  20354. + int cpu = (long)data;
  20355. +
  20356. + BUG_ON(per_cpu(posix_timer_task,cpu) != current);
  20357. +
  20358. + while (!kthread_should_stop()) {
  20359. + struct task_struct *tsk = NULL;
  20360. + struct task_struct *next = NULL;
  20361. +
  20362. + if (cpu_is_offline(cpu))
  20363. + goto wait_to_die;
  20364. +
  20365. + /* grab task list */
  20366. + raw_local_irq_disable();
  20367. + tsk = per_cpu(posix_timer_tasklist, cpu);
  20368. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  20369. + raw_local_irq_enable();
  20370. +
  20371. + /* its possible the list is empty, just return */
  20372. + if (!tsk) {
  20373. + set_current_state(TASK_INTERRUPTIBLE);
  20374. + schedule();
  20375. + __set_current_state(TASK_RUNNING);
  20376. + continue;
  20377. + }
  20378. +
  20379. + /* Process task list */
  20380. + while (1) {
  20381. + /* save next */
  20382. + next = tsk->posix_timer_list;
  20383. +
  20384. + /* run the task timers, clear its ptr and
  20385. + * unreference it
  20386. + */
  20387. + __run_posix_cpu_timers(tsk);
  20388. + tsk->posix_timer_list = NULL;
  20389. + put_task_struct(tsk);
  20390. +
  20391. + /* check if this is the last on the list */
  20392. + if (next == tsk)
  20393. + break;
  20394. + tsk = next;
  20395. + }
  20396. + }
  20397. + return 0;
  20398. +
  20399. +wait_to_die:
  20400. + /* Wait for kthread_stop */
  20401. + set_current_state(TASK_INTERRUPTIBLE);
  20402. + while (!kthread_should_stop()) {
  20403. + schedule();
  20404. + set_current_state(TASK_INTERRUPTIBLE);
  20405. + }
  20406. + __set_current_state(TASK_RUNNING);
  20407. + return 0;
  20408. +}
  20409. +
  20410. +static inline int __fastpath_timer_check(struct task_struct *tsk)
  20411. +{
  20412. + /* tsk == current, ensure it is safe to use ->signal/sighand */
  20413. + if (unlikely(tsk->exit_state))
  20414. + return 0;
  20415. +
  20416. + if (!task_cputime_zero(&tsk->cputime_expires))
  20417. + return 1;
  20418. +
  20419. + if (!task_cputime_zero(&tsk->signal->cputime_expires))
  20420. + return 1;
  20421. +
  20422. + return 0;
  20423. +}
  20424. +
  20425. +void run_posix_cpu_timers(struct task_struct *tsk)
  20426. +{
  20427. + unsigned long cpu = smp_processor_id();
  20428. + struct task_struct *tasklist;
  20429. +
  20430. + BUG_ON(!irqs_disabled());
  20431. + if(!per_cpu(posix_timer_task, cpu))
  20432. + return;
  20433. + /* get per-cpu references */
  20434. + tasklist = per_cpu(posix_timer_tasklist, cpu);
  20435. +
  20436. + /* check to see if we're already queued */
  20437. + if (!tsk->posix_timer_list && __fastpath_timer_check(tsk)) {
  20438. + get_task_struct(tsk);
  20439. + if (tasklist) {
  20440. + tsk->posix_timer_list = tasklist;
  20441. + } else {
  20442. + /*
  20443. + * The list is terminated by a self-pointing
  20444. + * task_struct
  20445. + */
  20446. + tsk->posix_timer_list = tsk;
  20447. + }
  20448. + per_cpu(posix_timer_tasklist, cpu) = tsk;
  20449. +
  20450. + wake_up_process(per_cpu(posix_timer_task, cpu));
  20451. + }
  20452. +}
  20453. +
  20454. +/*
  20455. + * posix_cpu_thread_call - callback that gets triggered when a CPU is added.
  20456. + * Here we can start up the necessary migration thread for the new CPU.
  20457. + */
  20458. +static int posix_cpu_thread_call(struct notifier_block *nfb,
  20459. + unsigned long action, void *hcpu)
  20460. +{
  20461. + int cpu = (long)hcpu;
  20462. + struct task_struct *p;
  20463. + struct sched_param param;
  20464. +
  20465. + switch (action) {
  20466. + case CPU_UP_PREPARE:
  20467. + p = kthread_create(posix_cpu_timers_thread, hcpu,
  20468. + "posixcputmr/%d",cpu);
  20469. + if (IS_ERR(p))
  20470. + return NOTIFY_BAD;
  20471. + p->flags |= PF_NOFREEZE;
  20472. + kthread_bind(p, cpu);
  20473. + /* Must be high prio to avoid getting starved */
  20474. + param.sched_priority = MAX_RT_PRIO-1;
  20475. + sched_setscheduler(p, SCHED_FIFO, &param);
  20476. + per_cpu(posix_timer_task,cpu) = p;
  20477. + break;
  20478. + case CPU_ONLINE:
  20479. + /* Strictly unneccessary, as first user will wake it. */
  20480. + wake_up_process(per_cpu(posix_timer_task,cpu));
  20481. + break;
  20482. +#ifdef CONFIG_HOTPLUG_CPU
  20483. + case CPU_UP_CANCELED:
  20484. + /* Unbind it from offline cpu so it can run. Fall thru. */
  20485. + kthread_bind(per_cpu(posix_timer_task, cpu),
  20486. + cpumask_any(cpu_online_mask));
  20487. + kthread_stop(per_cpu(posix_timer_task,cpu));
  20488. + per_cpu(posix_timer_task,cpu) = NULL;
  20489. + break;
  20490. + case CPU_DEAD:
  20491. + kthread_stop(per_cpu(posix_timer_task,cpu));
  20492. + per_cpu(posix_timer_task,cpu) = NULL;
  20493. + break;
  20494. +#endif
  20495. + }
  20496. + return NOTIFY_OK;
  20497. +}
  20498. +
  20499. +/* Register at highest priority so that task migration (migrate_all_tasks)
  20500. + * happens before everything else.
  20501. + */
  20502. +static struct notifier_block posix_cpu_thread_notifier = {
  20503. + .notifier_call = posix_cpu_thread_call,
  20504. + .priority = 10
  20505. +};
  20506. +
  20507. +static int __init posix_cpu_thread_init(void)
  20508. +{
  20509. + void *hcpu = (void *)(long)smp_processor_id();
  20510. + /* Start one for boot CPU. */
  20511. + unsigned long cpu;
  20512. +
  20513. + /* init the per-cpu posix_timer_tasklets */
  20514. + for_each_possible_cpu(cpu)
  20515. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  20516. +
  20517. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_UP_PREPARE, hcpu);
  20518. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_ONLINE, hcpu);
  20519. + register_cpu_notifier(&posix_cpu_thread_notifier);
  20520. + return 0;
  20521. +}
  20522. +early_initcall(posix_cpu_thread_init);
  20523. +#else /* CONFIG_PREEMPT_RT_BASE */
  20524. +void run_posix_cpu_timers(struct task_struct *tsk)
  20525. +{
  20526. + __run_posix_cpu_timers(tsk);
  20527. +}
  20528. +#endif /* CONFIG_PREEMPT_RT_BASE */
  20529. +
  20530. /*
  20531. * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
  20532. * The tsk->sighand->siglock must be held by the caller.
  20533. diff -Nur linux-4.1.13.orig/kernel/time/posix-timers.c linux-4.1.13/kernel/time/posix-timers.c
  20534. --- linux-4.1.13.orig/kernel/time/posix-timers.c 2015-11-09 23:34:10.000000000 +0100
  20535. +++ linux-4.1.13/kernel/time/posix-timers.c 2015-11-29 09:23:09.649611524 +0100
  20536. @@ -499,6 +499,7 @@
  20537. static struct pid *good_sigevent(sigevent_t * event)
  20538. {
  20539. struct task_struct *rtn = current->group_leader;
  20540. + int sig = event->sigev_signo;
  20541. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  20542. (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
  20543. @@ -507,7 +508,8 @@
  20544. return NULL;
  20545. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  20546. - ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  20547. + (sig <= 0 || sig > SIGRTMAX || sig_kernel_only(sig) ||
  20548. + sig_kernel_coredump(sig)))
  20549. return NULL;
  20550. return task_pid(rtn);
  20551. @@ -819,6 +821,20 @@
  20552. return overrun;
  20553. }
  20554. +/*
  20555. + * Protected by RCU!
  20556. + */
  20557. +static void timer_wait_for_callback(struct k_clock *kc, struct k_itimer *timr)
  20558. +{
  20559. +#ifdef CONFIG_PREEMPT_RT_FULL
  20560. + if (kc->timer_set == common_timer_set)
  20561. + hrtimer_wait_for_timer(&timr->it.real.timer);
  20562. + else
  20563. + /* FIXME: Whacky hack for posix-cpu-timers */
  20564. + schedule_timeout(1);
  20565. +#endif
  20566. +}
  20567. +
  20568. /* Set a POSIX.1b interval timer. */
  20569. /* timr->it_lock is taken. */
  20570. static int
  20571. @@ -896,6 +912,7 @@
  20572. if (!timr)
  20573. return -EINVAL;
  20574. + rcu_read_lock();
  20575. kc = clockid_to_kclock(timr->it_clock);
  20576. if (WARN_ON_ONCE(!kc || !kc->timer_set))
  20577. error = -EINVAL;
  20578. @@ -904,9 +921,12 @@
  20579. unlock_timer(timr, flag);
  20580. if (error == TIMER_RETRY) {
  20581. + timer_wait_for_callback(kc, timr);
  20582. rtn = NULL; // We already got the old time...
  20583. + rcu_read_unlock();
  20584. goto retry;
  20585. }
  20586. + rcu_read_unlock();
  20587. if (old_setting && !error &&
  20588. copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
  20589. @@ -944,10 +964,15 @@
  20590. if (!timer)
  20591. return -EINVAL;
  20592. + rcu_read_lock();
  20593. if (timer_delete_hook(timer) == TIMER_RETRY) {
  20594. unlock_timer(timer, flags);
  20595. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  20596. + timer);
  20597. + rcu_read_unlock();
  20598. goto retry_delete;
  20599. }
  20600. + rcu_read_unlock();
  20601. spin_lock(&current->sighand->siglock);
  20602. list_del(&timer->list);
  20603. @@ -973,8 +998,18 @@
  20604. retry_delete:
  20605. spin_lock_irqsave(&timer->it_lock, flags);
  20606. + /* On RT we can race with a deletion */
  20607. + if (!timer->it_signal) {
  20608. + unlock_timer(timer, flags);
  20609. + return;
  20610. + }
  20611. +
  20612. if (timer_delete_hook(timer) == TIMER_RETRY) {
  20613. + rcu_read_lock();
  20614. unlock_timer(timer, flags);
  20615. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  20616. + timer);
  20617. + rcu_read_unlock();
  20618. goto retry_delete;
  20619. }
  20620. list_del(&timer->list);
  20621. diff -Nur linux-4.1.13.orig/kernel/time/tick-common.c linux-4.1.13/kernel/time/tick-common.c
  20622. --- linux-4.1.13.orig/kernel/time/tick-common.c 2015-11-09 23:34:10.000000000 +0100
  20623. +++ linux-4.1.13/kernel/time/tick-common.c 2015-11-29 09:23:09.649611524 +0100
  20624. @@ -78,13 +78,15 @@
  20625. static void tick_periodic(int cpu)
  20626. {
  20627. if (tick_do_timer_cpu == cpu) {
  20628. - write_seqlock(&jiffies_lock);
  20629. + raw_spin_lock(&jiffies_lock);
  20630. + write_seqcount_begin(&jiffies_seq);
  20631. /* Keep track of the next tick event */
  20632. tick_next_period = ktime_add(tick_next_period, tick_period);
  20633. do_timer(1);
  20634. - write_sequnlock(&jiffies_lock);
  20635. + write_seqcount_end(&jiffies_seq);
  20636. + raw_spin_unlock(&jiffies_lock);
  20637. update_wall_time();
  20638. }
  20639. @@ -146,9 +148,9 @@
  20640. ktime_t next;
  20641. do {
  20642. - seq = read_seqbegin(&jiffies_lock);
  20643. + seq = read_seqcount_begin(&jiffies_seq);
  20644. next = tick_next_period;
  20645. - } while (read_seqretry(&jiffies_lock, seq));
  20646. + } while (read_seqcount_retry(&jiffies_seq, seq));
  20647. clockevents_set_state(dev, CLOCK_EVT_STATE_ONESHOT);
  20648. diff -Nur linux-4.1.13.orig/kernel/time/tick-sched.c linux-4.1.13/kernel/time/tick-sched.c
  20649. --- linux-4.1.13.orig/kernel/time/tick-sched.c 2015-11-09 23:34:10.000000000 +0100
  20650. +++ linux-4.1.13/kernel/time/tick-sched.c 2015-11-29 09:23:09.649611524 +0100
  20651. @@ -62,7 +62,8 @@
  20652. return;
  20653. /* Reevalute with jiffies_lock held */
  20654. - write_seqlock(&jiffies_lock);
  20655. + raw_spin_lock(&jiffies_lock);
  20656. + write_seqcount_begin(&jiffies_seq);
  20657. delta = ktime_sub(now, last_jiffies_update);
  20658. if (delta.tv64 >= tick_period.tv64) {
  20659. @@ -85,10 +86,12 @@
  20660. /* Keep the tick_next_period variable up to date */
  20661. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  20662. } else {
  20663. - write_sequnlock(&jiffies_lock);
  20664. + write_seqcount_end(&jiffies_seq);
  20665. + raw_spin_unlock(&jiffies_lock);
  20666. return;
  20667. }
  20668. - write_sequnlock(&jiffies_lock);
  20669. + write_seqcount_end(&jiffies_seq);
  20670. + raw_spin_unlock(&jiffies_lock);
  20671. update_wall_time();
  20672. }
  20673. @@ -99,12 +102,14 @@
  20674. {
  20675. ktime_t period;
  20676. - write_seqlock(&jiffies_lock);
  20677. + raw_spin_lock(&jiffies_lock);
  20678. + write_seqcount_begin(&jiffies_seq);
  20679. /* Did we start the jiffies update yet ? */
  20680. if (last_jiffies_update.tv64 == 0)
  20681. last_jiffies_update = tick_next_period;
  20682. period = last_jiffies_update;
  20683. - write_sequnlock(&jiffies_lock);
  20684. + write_seqcount_end(&jiffies_seq);
  20685. + raw_spin_unlock(&jiffies_lock);
  20686. return period;
  20687. }
  20688. @@ -176,6 +181,11 @@
  20689. return false;
  20690. }
  20691. + if (!arch_irq_work_has_interrupt()) {
  20692. + trace_tick_stop(0, "missing irq work interrupt\n");
  20693. + return false;
  20694. + }
  20695. +
  20696. /* sched_clock_tick() needs us? */
  20697. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  20698. /*
  20699. @@ -222,6 +232,7 @@
  20700. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
  20701. .func = nohz_full_kick_work_func,
  20702. + .flags = IRQ_WORK_HARD_IRQ,
  20703. };
  20704. /*
  20705. @@ -578,10 +589,10 @@
  20706. /* Read jiffies and the time when jiffies were updated last */
  20707. do {
  20708. - seq = read_seqbegin(&jiffies_lock);
  20709. + seq = read_seqcount_begin(&jiffies_seq);
  20710. last_update = last_jiffies_update;
  20711. last_jiffies = jiffies;
  20712. - } while (read_seqretry(&jiffies_lock, seq));
  20713. + } while (read_seqcount_retry(&jiffies_seq, seq));
  20714. if (rcu_needs_cpu(&rcu_delta_jiffies) ||
  20715. arch_needs_cpu() || irq_work_needs_cpu()) {
  20716. @@ -759,14 +770,7 @@
  20717. return false;
  20718. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  20719. - static int ratelimit;
  20720. -
  20721. - if (ratelimit < 10 &&
  20722. - (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  20723. - pr_warn("NOHZ: local_softirq_pending %02x\n",
  20724. - (unsigned int) local_softirq_pending());
  20725. - ratelimit++;
  20726. - }
  20727. + softirq_check_pending_idle();
  20728. return false;
  20729. }
  20730. @@ -1154,6 +1158,7 @@
  20731. * Emulate tick processing via per-CPU hrtimers:
  20732. */
  20733. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  20734. + ts->sched_timer.irqsafe = 1;
  20735. ts->sched_timer.function = tick_sched_timer;
  20736. /* Get the next period (per cpu) */
  20737. diff -Nur linux-4.1.13.orig/kernel/time/timekeeping.c linux-4.1.13/kernel/time/timekeeping.c
  20738. --- linux-4.1.13.orig/kernel/time/timekeeping.c 2015-11-09 23:34:10.000000000 +0100
  20739. +++ linux-4.1.13/kernel/time/timekeeping.c 2015-11-29 09:23:09.649611524 +0100
  20740. @@ -2065,8 +2065,10 @@
  20741. */
  20742. void xtime_update(unsigned long ticks)
  20743. {
  20744. - write_seqlock(&jiffies_lock);
  20745. + raw_spin_lock(&jiffies_lock);
  20746. + write_seqcount_begin(&jiffies_seq);
  20747. do_timer(ticks);
  20748. - write_sequnlock(&jiffies_lock);
  20749. + write_seqcount_end(&jiffies_seq);
  20750. + raw_spin_unlock(&jiffies_lock);
  20751. update_wall_time();
  20752. }
  20753. diff -Nur linux-4.1.13.orig/kernel/time/timekeeping.h linux-4.1.13/kernel/time/timekeeping.h
  20754. --- linux-4.1.13.orig/kernel/time/timekeeping.h 2015-11-09 23:34:10.000000000 +0100
  20755. +++ linux-4.1.13/kernel/time/timekeeping.h 2015-11-29 09:23:09.653611260 +0100
  20756. @@ -22,7 +22,8 @@
  20757. extern void do_timer(unsigned long ticks);
  20758. extern void update_wall_time(void);
  20759. -extern seqlock_t jiffies_lock;
  20760. +extern raw_spinlock_t jiffies_lock;
  20761. +extern seqcount_t jiffies_seq;
  20762. #define CS_NAME_LEN 32
  20763. diff -Nur linux-4.1.13.orig/kernel/time/timer.c linux-4.1.13/kernel/time/timer.c
  20764. --- linux-4.1.13.orig/kernel/time/timer.c 2015-11-09 23:34:10.000000000 +0100
  20765. +++ linux-4.1.13/kernel/time/timer.c 2015-11-29 09:23:09.653611260 +0100
  20766. @@ -78,6 +78,9 @@
  20767. struct tvec_base {
  20768. spinlock_t lock;
  20769. struct timer_list *running_timer;
  20770. +#ifdef CONFIG_PREEMPT_RT_FULL
  20771. + wait_queue_head_t wait_for_running_timer;
  20772. +#endif
  20773. unsigned long timer_jiffies;
  20774. unsigned long next_timer;
  20775. unsigned long active_timers;
  20776. @@ -768,6 +771,36 @@
  20777. }
  20778. }
  20779. +#ifndef CONFIG_PREEMPT_RT_FULL
  20780. +static inline struct tvec_base *switch_timer_base(struct timer_list *timer,
  20781. + struct tvec_base *old,
  20782. + struct tvec_base *new)
  20783. +{
  20784. + /* See the comment in lock_timer_base() */
  20785. + timer_set_base(timer, NULL);
  20786. + spin_unlock(&old->lock);
  20787. + spin_lock(&new->lock);
  20788. + timer_set_base(timer, new);
  20789. + return new;
  20790. +}
  20791. +#else
  20792. +static inline struct tvec_base *switch_timer_base(struct timer_list *timer,
  20793. + struct tvec_base *old,
  20794. + struct tvec_base *new)
  20795. +{
  20796. + /*
  20797. + * We cannot do the above because we might be preempted and
  20798. + * then the preempter would see NULL and loop forever.
  20799. + */
  20800. + if (spin_trylock(&new->lock)) {
  20801. + timer_set_base(timer, new);
  20802. + spin_unlock(&old->lock);
  20803. + return new;
  20804. + }
  20805. + return old;
  20806. +}
  20807. +#endif
  20808. +
  20809. static inline int
  20810. __mod_timer(struct timer_list *timer, unsigned long expires,
  20811. bool pending_only, int pinned)
  20812. @@ -798,14 +831,8 @@
  20813. * handler yet has not finished. This also guarantees that
  20814. * the timer is serialized wrt itself.
  20815. */
  20816. - if (likely(base->running_timer != timer)) {
  20817. - /* See the comment in lock_timer_base() */
  20818. - timer_set_base(timer, NULL);
  20819. - spin_unlock(&base->lock);
  20820. - base = new_base;
  20821. - spin_lock(&base->lock);
  20822. - timer_set_base(timer, base);
  20823. - }
  20824. + if (likely(base->running_timer != timer))
  20825. + base = switch_timer_base(timer, base, new_base);
  20826. }
  20827. timer->expires = expires;
  20828. @@ -979,6 +1006,29 @@
  20829. }
  20830. EXPORT_SYMBOL_GPL(add_timer_on);
  20831. +#ifdef CONFIG_PREEMPT_RT_FULL
  20832. +/*
  20833. + * Wait for a running timer
  20834. + */
  20835. +static void wait_for_running_timer(struct timer_list *timer)
  20836. +{
  20837. + struct tvec_base *base = timer->base;
  20838. +
  20839. + if (base->running_timer == timer)
  20840. + wait_event(base->wait_for_running_timer,
  20841. + base->running_timer != timer);
  20842. +}
  20843. +
  20844. +# define wakeup_timer_waiters(b) wake_up(&(b)->wait_for_running_timer)
  20845. +#else
  20846. +static inline void wait_for_running_timer(struct timer_list *timer)
  20847. +{
  20848. + cpu_relax();
  20849. +}
  20850. +
  20851. +# define wakeup_timer_waiters(b) do { } while (0)
  20852. +#endif
  20853. +
  20854. /**
  20855. * del_timer - deactive a timer.
  20856. * @timer: the timer to be deactivated
  20857. @@ -1036,7 +1086,7 @@
  20858. }
  20859. EXPORT_SYMBOL(try_to_del_timer_sync);
  20860. -#ifdef CONFIG_SMP
  20861. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  20862. static DEFINE_PER_CPU(struct tvec_base, __tvec_bases);
  20863. /**
  20864. @@ -1098,7 +1148,7 @@
  20865. int ret = try_to_del_timer_sync(timer);
  20866. if (ret >= 0)
  20867. return ret;
  20868. - cpu_relax();
  20869. + wait_for_running_timer(timer);
  20870. }
  20871. }
  20872. EXPORT_SYMBOL(del_timer_sync);
  20873. @@ -1219,15 +1269,17 @@
  20874. if (irqsafe) {
  20875. spin_unlock(&base->lock);
  20876. call_timer_fn(timer, fn, data);
  20877. + base->running_timer = NULL;
  20878. spin_lock(&base->lock);
  20879. } else {
  20880. spin_unlock_irq(&base->lock);
  20881. call_timer_fn(timer, fn, data);
  20882. + base->running_timer = NULL;
  20883. spin_lock_irq(&base->lock);
  20884. }
  20885. }
  20886. }
  20887. - base->running_timer = NULL;
  20888. + wakeup_timer_waiters(base);
  20889. spin_unlock_irq(&base->lock);
  20890. }
  20891. @@ -1367,6 +1419,14 @@
  20892. if (cpu_is_offline(smp_processor_id()))
  20893. return expires;
  20894. +#ifdef CONFIG_PREEMPT_RT_FULL
  20895. + /*
  20896. + * On PREEMPT_RT we cannot sleep here. As a result we can't take
  20897. + * the base lock to check when the next timer is pending and so
  20898. + * we assume the next jiffy.
  20899. + */
  20900. + return now + 1;
  20901. +#endif
  20902. spin_lock(&base->lock);
  20903. if (base->active_timers) {
  20904. if (time_before_eq(base->next_timer, base->timer_jiffies))
  20905. @@ -1392,13 +1452,13 @@
  20906. /* Note: this timer irq context must be accounted for as well. */
  20907. account_process_tick(p, user_tick);
  20908. + scheduler_tick();
  20909. run_local_timers();
  20910. rcu_check_callbacks(user_tick);
  20911. -#ifdef CONFIG_IRQ_WORK
  20912. +#if defined(CONFIG_IRQ_WORK)
  20913. if (in_irq())
  20914. irq_work_tick();
  20915. #endif
  20916. - scheduler_tick();
  20917. run_posix_cpu_timers(p);
  20918. }
  20919. @@ -1411,6 +1471,8 @@
  20920. hrtimer_run_pending();
  20921. + irq_work_tick_soft();
  20922. +
  20923. if (time_after_eq(jiffies, base->timer_jiffies))
  20924. __run_timers(base);
  20925. }
  20926. @@ -1566,7 +1628,7 @@
  20927. BUG_ON(cpu_online(cpu));
  20928. old_base = per_cpu(tvec_bases, cpu);
  20929. - new_base = get_cpu_var(tvec_bases);
  20930. + new_base = get_local_var(tvec_bases);
  20931. /*
  20932. * The caller is globally serialized and nobody else
  20933. * takes two locks at once, deadlock is not possible.
  20934. @@ -1590,7 +1652,7 @@
  20935. spin_unlock(&old_base->lock);
  20936. spin_unlock_irq(&new_base->lock);
  20937. - put_cpu_var(tvec_bases);
  20938. + put_local_var(tvec_bases);
  20939. }
  20940. static int timer_cpu_notify(struct notifier_block *self,
  20941. @@ -1625,6 +1687,9 @@
  20942. base->cpu = cpu;
  20943. per_cpu(tvec_bases, cpu) = base;
  20944. spin_lock_init(&base->lock);
  20945. +#ifdef CONFIG_PREEMPT_RT_FULL
  20946. + init_waitqueue_head(&base->wait_for_running_timer);
  20947. +#endif
  20948. for (j = 0; j < TVN_SIZE; j++) {
  20949. INIT_LIST_HEAD(base->tv5.vec + j);
  20950. diff -Nur linux-4.1.13.orig/kernel/trace/Kconfig linux-4.1.13/kernel/trace/Kconfig
  20951. --- linux-4.1.13.orig/kernel/trace/Kconfig 2015-11-09 23:34:10.000000000 +0100
  20952. +++ linux-4.1.13/kernel/trace/Kconfig 2015-11-29 09:23:09.653611260 +0100
  20953. @@ -187,6 +187,24 @@
  20954. enabled. This option and the preempt-off timing option can be
  20955. used together or separately.)
  20956. +config INTERRUPT_OFF_HIST
  20957. + bool "Interrupts-off Latency Histogram"
  20958. + depends on IRQSOFF_TRACER
  20959. + help
  20960. + This option generates continuously updated histograms (one per cpu)
  20961. + of the duration of time periods with interrupts disabled. The
  20962. + histograms are disabled by default. To enable them, write a non-zero
  20963. + number to
  20964. +
  20965. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  20966. +
  20967. + If PREEMPT_OFF_HIST is also selected, additional histograms (one
  20968. + per cpu) are generated that accumulate the duration of time periods
  20969. + when both interrupts and preemption are disabled. The histogram data
  20970. + will be located in the debug file system at
  20971. +
  20972. + /sys/kernel/debug/tracing/latency_hist/irqsoff
  20973. +
  20974. config PREEMPT_TRACER
  20975. bool "Preemption-off Latency Tracer"
  20976. default n
  20977. @@ -211,6 +229,24 @@
  20978. enabled. This option and the irqs-off timing option can be
  20979. used together or separately.)
  20980. +config PREEMPT_OFF_HIST
  20981. + bool "Preemption-off Latency Histogram"
  20982. + depends on PREEMPT_TRACER
  20983. + help
  20984. + This option generates continuously updated histograms (one per cpu)
  20985. + of the duration of time periods with preemption disabled. The
  20986. + histograms are disabled by default. To enable them, write a non-zero
  20987. + number to
  20988. +
  20989. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  20990. +
  20991. + If INTERRUPT_OFF_HIST is also selected, additional histograms (one
  20992. + per cpu) are generated that accumulate the duration of time periods
  20993. + when both interrupts and preemption are disabled. The histogram data
  20994. + will be located in the debug file system at
  20995. +
  20996. + /sys/kernel/debug/tracing/latency_hist/preemptoff
  20997. +
  20998. config SCHED_TRACER
  20999. bool "Scheduling Latency Tracer"
  21000. select GENERIC_TRACER
  21001. @@ -221,6 +257,74 @@
  21002. This tracer tracks the latency of the highest priority task
  21003. to be scheduled in, starting from the point it has woken up.
  21004. +config WAKEUP_LATENCY_HIST
  21005. + bool "Scheduling Latency Histogram"
  21006. + depends on SCHED_TRACER
  21007. + help
  21008. + This option generates continuously updated histograms (one per cpu)
  21009. + of the scheduling latency of the highest priority task.
  21010. + The histograms are disabled by default. To enable them, write a
  21011. + non-zero number to
  21012. +
  21013. + /sys/kernel/debug/tracing/latency_hist/enable/wakeup
  21014. +
  21015. + Two different algorithms are used, one to determine the latency of
  21016. + processes that exclusively use the highest priority of the system and
  21017. + another one to determine the latency of processes that share the
  21018. + highest system priority with other processes. The former is used to
  21019. + improve hardware and system software, the latter to optimize the
  21020. + priority design of a given system. The histogram data will be
  21021. + located in the debug file system at
  21022. +
  21023. + /sys/kernel/debug/tracing/latency_hist/wakeup
  21024. +
  21025. + and
  21026. +
  21027. + /sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio
  21028. +
  21029. + If both Scheduling Latency Histogram and Missed Timer Offsets
  21030. + Histogram are selected, additional histogram data will be collected
  21031. + that contain, in addition to the wakeup latency, the timer latency, in
  21032. + case the wakeup was triggered by an expired timer. These histograms
  21033. + are available in the
  21034. +
  21035. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  21036. +
  21037. + directory. They reflect the apparent interrupt and scheduling latency
  21038. + and are best suitable to determine the worst-case latency of a given
  21039. + system. To enable these histograms, write a non-zero number to
  21040. +
  21041. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  21042. +
  21043. +config MISSED_TIMER_OFFSETS_HIST
  21044. + depends on HIGH_RES_TIMERS
  21045. + select GENERIC_TRACER
  21046. + bool "Missed Timer Offsets Histogram"
  21047. + help
  21048. + Generate a histogram of missed timer offsets in microseconds. The
  21049. + histograms are disabled by default. To enable them, write a non-zero
  21050. + number to
  21051. +
  21052. + /sys/kernel/debug/tracing/latency_hist/enable/missed_timer_offsets
  21053. +
  21054. + The histogram data will be located in the debug file system at
  21055. +
  21056. + /sys/kernel/debug/tracing/latency_hist/missed_timer_offsets
  21057. +
  21058. + If both Scheduling Latency Histogram and Missed Timer Offsets
  21059. + Histogram are selected, additional histogram data will be collected
  21060. + that contain, in addition to the wakeup latency, the timer latency, in
  21061. + case the wakeup was triggered by an expired timer. These histograms
  21062. + are available in the
  21063. +
  21064. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  21065. +
  21066. + directory. They reflect the apparent interrupt and scheduling latency
  21067. + and are best suitable to determine the worst-case latency of a given
  21068. + system. To enable these histograms, write a non-zero number to
  21069. +
  21070. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  21071. +
  21072. config ENABLE_DEFAULT_TRACERS
  21073. bool "Trace process context switches and events"
  21074. depends on !GENERIC_TRACER
  21075. diff -Nur linux-4.1.13.orig/kernel/trace/latency_hist.c linux-4.1.13/kernel/trace/latency_hist.c
  21076. --- linux-4.1.13.orig/kernel/trace/latency_hist.c 1970-01-01 01:00:00.000000000 +0100
  21077. +++ linux-4.1.13/kernel/trace/latency_hist.c 2015-11-29 09:23:09.653611260 +0100
  21078. @@ -0,0 +1,1178 @@
  21079. +/*
  21080. + * kernel/trace/latency_hist.c
  21081. + *
  21082. + * Add support for histograms of preemption-off latency and
  21083. + * interrupt-off latency and wakeup latency, it depends on
  21084. + * Real-Time Preemption Support.
  21085. + *
  21086. + * Copyright (C) 2005 MontaVista Software, Inc.
  21087. + * Yi Yang <yyang@ch.mvista.com>
  21088. + *
  21089. + * Converted to work with the new latency tracer.
  21090. + * Copyright (C) 2008 Red Hat, Inc.
  21091. + * Steven Rostedt <srostedt@redhat.com>
  21092. + *
  21093. + */
  21094. +#include <linux/module.h>
  21095. +#include <linux/debugfs.h>
  21096. +#include <linux/seq_file.h>
  21097. +#include <linux/percpu.h>
  21098. +#include <linux/kallsyms.h>
  21099. +#include <linux/uaccess.h>
  21100. +#include <linux/sched.h>
  21101. +#include <linux/sched/rt.h>
  21102. +#include <linux/slab.h>
  21103. +#include <linux/atomic.h>
  21104. +#include <asm/div64.h>
  21105. +
  21106. +#include "trace.h"
  21107. +#include <trace/events/sched.h>
  21108. +
  21109. +#define NSECS_PER_USECS 1000L
  21110. +
  21111. +#define CREATE_TRACE_POINTS
  21112. +#include <trace/events/hist.h>
  21113. +
  21114. +enum {
  21115. + IRQSOFF_LATENCY = 0,
  21116. + PREEMPTOFF_LATENCY,
  21117. + PREEMPTIRQSOFF_LATENCY,
  21118. + WAKEUP_LATENCY,
  21119. + WAKEUP_LATENCY_SHAREDPRIO,
  21120. + MISSED_TIMER_OFFSETS,
  21121. + TIMERANDWAKEUP_LATENCY,
  21122. + MAX_LATENCY_TYPE,
  21123. +};
  21124. +
  21125. +#define MAX_ENTRY_NUM 10240
  21126. +
  21127. +struct hist_data {
  21128. + atomic_t hist_mode; /* 0 log, 1 don't log */
  21129. + long offset; /* set it to MAX_ENTRY_NUM/2 for a bipolar scale */
  21130. + long min_lat;
  21131. + long max_lat;
  21132. + unsigned long long below_hist_bound_samples;
  21133. + unsigned long long above_hist_bound_samples;
  21134. + long long accumulate_lat;
  21135. + unsigned long long total_samples;
  21136. + unsigned long long hist_array[MAX_ENTRY_NUM];
  21137. +};
  21138. +
  21139. +struct enable_data {
  21140. + int latency_type;
  21141. + int enabled;
  21142. +};
  21143. +
  21144. +static char *latency_hist_dir_root = "latency_hist";
  21145. +
  21146. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  21147. +static DEFINE_PER_CPU(struct hist_data, irqsoff_hist);
  21148. +static char *irqsoff_hist_dir = "irqsoff";
  21149. +static DEFINE_PER_CPU(cycles_t, hist_irqsoff_start);
  21150. +static DEFINE_PER_CPU(int, hist_irqsoff_counting);
  21151. +#endif
  21152. +
  21153. +#ifdef CONFIG_PREEMPT_OFF_HIST
  21154. +static DEFINE_PER_CPU(struct hist_data, preemptoff_hist);
  21155. +static char *preemptoff_hist_dir = "preemptoff";
  21156. +static DEFINE_PER_CPU(cycles_t, hist_preemptoff_start);
  21157. +static DEFINE_PER_CPU(int, hist_preemptoff_counting);
  21158. +#endif
  21159. +
  21160. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  21161. +static DEFINE_PER_CPU(struct hist_data, preemptirqsoff_hist);
  21162. +static char *preemptirqsoff_hist_dir = "preemptirqsoff";
  21163. +static DEFINE_PER_CPU(cycles_t, hist_preemptirqsoff_start);
  21164. +static DEFINE_PER_CPU(int, hist_preemptirqsoff_counting);
  21165. +#endif
  21166. +
  21167. +#if defined(CONFIG_PREEMPT_OFF_HIST) || defined(CONFIG_INTERRUPT_OFF_HIST)
  21168. +static notrace void probe_preemptirqsoff_hist(void *v, int reason, int start);
  21169. +static struct enable_data preemptirqsoff_enabled_data = {
  21170. + .latency_type = PREEMPTIRQSOFF_LATENCY,
  21171. + .enabled = 0,
  21172. +};
  21173. +#endif
  21174. +
  21175. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  21176. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  21177. +struct maxlatproc_data {
  21178. + char comm[FIELD_SIZEOF(struct task_struct, comm)];
  21179. + char current_comm[FIELD_SIZEOF(struct task_struct, comm)];
  21180. + int pid;
  21181. + int current_pid;
  21182. + int prio;
  21183. + int current_prio;
  21184. + long latency;
  21185. + long timeroffset;
  21186. + cycle_t timestamp;
  21187. +};
  21188. +#endif
  21189. +
  21190. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  21191. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist);
  21192. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist_sharedprio);
  21193. +static char *wakeup_latency_hist_dir = "wakeup";
  21194. +static char *wakeup_latency_hist_dir_sharedprio = "sharedprio";
  21195. +static notrace void probe_wakeup_latency_hist_start(void *v,
  21196. + struct task_struct *p);
  21197. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  21198. + struct task_struct *prev, struct task_struct *next);
  21199. +static notrace void probe_sched_migrate_task(void *,
  21200. + struct task_struct *task, int cpu);
  21201. +static struct enable_data wakeup_latency_enabled_data = {
  21202. + .latency_type = WAKEUP_LATENCY,
  21203. + .enabled = 0,
  21204. +};
  21205. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc);
  21206. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc_sharedprio);
  21207. +static DEFINE_PER_CPU(struct task_struct *, wakeup_task);
  21208. +static DEFINE_PER_CPU(int, wakeup_sharedprio);
  21209. +static unsigned long wakeup_pid;
  21210. +#endif
  21211. +
  21212. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  21213. +static DEFINE_PER_CPU(struct hist_data, missed_timer_offsets);
  21214. +static char *missed_timer_offsets_dir = "missed_timer_offsets";
  21215. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  21216. + long long offset, struct task_struct *curr, struct task_struct *task);
  21217. +static struct enable_data missed_timer_offsets_enabled_data = {
  21218. + .latency_type = MISSED_TIMER_OFFSETS,
  21219. + .enabled = 0,
  21220. +};
  21221. +static DEFINE_PER_CPU(struct maxlatproc_data, missed_timer_offsets_maxlatproc);
  21222. +static unsigned long missed_timer_offsets_pid;
  21223. +#endif
  21224. +
  21225. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  21226. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  21227. +static DEFINE_PER_CPU(struct hist_data, timerandwakeup_latency_hist);
  21228. +static char *timerandwakeup_latency_hist_dir = "timerandwakeup";
  21229. +static struct enable_data timerandwakeup_enabled_data = {
  21230. + .latency_type = TIMERANDWAKEUP_LATENCY,
  21231. + .enabled = 0,
  21232. +};
  21233. +static DEFINE_PER_CPU(struct maxlatproc_data, timerandwakeup_maxlatproc);
  21234. +#endif
  21235. +
  21236. +void notrace latency_hist(int latency_type, int cpu, long latency,
  21237. + long timeroffset, cycle_t stop,
  21238. + struct task_struct *p)
  21239. +{
  21240. + struct hist_data *my_hist;
  21241. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  21242. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  21243. + struct maxlatproc_data *mp = NULL;
  21244. +#endif
  21245. +
  21246. + if (!cpu_possible(cpu) || latency_type < 0 ||
  21247. + latency_type >= MAX_LATENCY_TYPE)
  21248. + return;
  21249. +
  21250. + switch (latency_type) {
  21251. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  21252. + case IRQSOFF_LATENCY:
  21253. + my_hist = &per_cpu(irqsoff_hist, cpu);
  21254. + break;
  21255. +#endif
  21256. +#ifdef CONFIG_PREEMPT_OFF_HIST
  21257. + case PREEMPTOFF_LATENCY:
  21258. + my_hist = &per_cpu(preemptoff_hist, cpu);
  21259. + break;
  21260. +#endif
  21261. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  21262. + case PREEMPTIRQSOFF_LATENCY:
  21263. + my_hist = &per_cpu(preemptirqsoff_hist, cpu);
  21264. + break;
  21265. +#endif
  21266. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  21267. + case WAKEUP_LATENCY:
  21268. + my_hist = &per_cpu(wakeup_latency_hist, cpu);
  21269. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  21270. + break;
  21271. + case WAKEUP_LATENCY_SHAREDPRIO:
  21272. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  21273. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  21274. + break;
  21275. +#endif
  21276. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  21277. + case MISSED_TIMER_OFFSETS:
  21278. + my_hist = &per_cpu(missed_timer_offsets, cpu);
  21279. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  21280. + break;
  21281. +#endif
  21282. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  21283. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  21284. + case TIMERANDWAKEUP_LATENCY:
  21285. + my_hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  21286. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  21287. + break;
  21288. +#endif
  21289. +
  21290. + default:
  21291. + return;
  21292. + }
  21293. +
  21294. + latency += my_hist->offset;
  21295. +
  21296. + if (atomic_read(&my_hist->hist_mode) == 0)
  21297. + return;
  21298. +
  21299. + if (latency < 0 || latency >= MAX_ENTRY_NUM) {
  21300. + if (latency < 0)
  21301. + my_hist->below_hist_bound_samples++;
  21302. + else
  21303. + my_hist->above_hist_bound_samples++;
  21304. + } else
  21305. + my_hist->hist_array[latency]++;
  21306. +
  21307. + if (unlikely(latency > my_hist->max_lat ||
  21308. + my_hist->min_lat == LONG_MAX)) {
  21309. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  21310. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  21311. + if (latency_type == WAKEUP_LATENCY ||
  21312. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  21313. + latency_type == MISSED_TIMER_OFFSETS ||
  21314. + latency_type == TIMERANDWAKEUP_LATENCY) {
  21315. + strncpy(mp->comm, p->comm, sizeof(mp->comm));
  21316. + strncpy(mp->current_comm, current->comm,
  21317. + sizeof(mp->current_comm));
  21318. + mp->pid = task_pid_nr(p);
  21319. + mp->current_pid = task_pid_nr(current);
  21320. + mp->prio = p->prio;
  21321. + mp->current_prio = current->prio;
  21322. + mp->latency = latency;
  21323. + mp->timeroffset = timeroffset;
  21324. + mp->timestamp = stop;
  21325. + }
  21326. +#endif
  21327. + my_hist->max_lat = latency;
  21328. + }
  21329. + if (unlikely(latency < my_hist->min_lat))
  21330. + my_hist->min_lat = latency;
  21331. + my_hist->total_samples++;
  21332. + my_hist->accumulate_lat += latency;
  21333. +}
  21334. +
  21335. +static void *l_start(struct seq_file *m, loff_t *pos)
  21336. +{
  21337. + loff_t *index_ptr = NULL;
  21338. + loff_t index = *pos;
  21339. + struct hist_data *my_hist = m->private;
  21340. +
  21341. + if (index == 0) {
  21342. + char minstr[32], avgstr[32], maxstr[32];
  21343. +
  21344. + atomic_dec(&my_hist->hist_mode);
  21345. +
  21346. + if (likely(my_hist->total_samples)) {
  21347. + long avg = (long) div64_s64(my_hist->accumulate_lat,
  21348. + my_hist->total_samples);
  21349. + snprintf(minstr, sizeof(minstr), "%ld",
  21350. + my_hist->min_lat - my_hist->offset);
  21351. + snprintf(avgstr, sizeof(avgstr), "%ld",
  21352. + avg - my_hist->offset);
  21353. + snprintf(maxstr, sizeof(maxstr), "%ld",
  21354. + my_hist->max_lat - my_hist->offset);
  21355. + } else {
  21356. + strcpy(minstr, "<undef>");
  21357. + strcpy(avgstr, minstr);
  21358. + strcpy(maxstr, minstr);
  21359. + }
  21360. +
  21361. + seq_printf(m, "#Minimum latency: %s microseconds\n"
  21362. + "#Average latency: %s microseconds\n"
  21363. + "#Maximum latency: %s microseconds\n"
  21364. + "#Total samples: %llu\n"
  21365. + "#There are %llu samples lower than %ld"
  21366. + " microseconds.\n"
  21367. + "#There are %llu samples greater or equal"
  21368. + " than %ld microseconds.\n"
  21369. + "#usecs\t%16s\n",
  21370. + minstr, avgstr, maxstr,
  21371. + my_hist->total_samples,
  21372. + my_hist->below_hist_bound_samples,
  21373. + -my_hist->offset,
  21374. + my_hist->above_hist_bound_samples,
  21375. + MAX_ENTRY_NUM - my_hist->offset,
  21376. + "samples");
  21377. + }
  21378. + if (index < MAX_ENTRY_NUM) {
  21379. + index_ptr = kmalloc(sizeof(loff_t), GFP_KERNEL);
  21380. + if (index_ptr)
  21381. + *index_ptr = index;
  21382. + }
  21383. +
  21384. + return index_ptr;
  21385. +}
  21386. +
  21387. +static void *l_next(struct seq_file *m, void *p, loff_t *pos)
  21388. +{
  21389. + loff_t *index_ptr = p;
  21390. + struct hist_data *my_hist = m->private;
  21391. +
  21392. + if (++*pos >= MAX_ENTRY_NUM) {
  21393. + atomic_inc(&my_hist->hist_mode);
  21394. + return NULL;
  21395. + }
  21396. + *index_ptr = *pos;
  21397. + return index_ptr;
  21398. +}
  21399. +
  21400. +static void l_stop(struct seq_file *m, void *p)
  21401. +{
  21402. + kfree(p);
  21403. +}
  21404. +
  21405. +static int l_show(struct seq_file *m, void *p)
  21406. +{
  21407. + int index = *(loff_t *) p;
  21408. + struct hist_data *my_hist = m->private;
  21409. +
  21410. + seq_printf(m, "%6ld\t%16llu\n", index - my_hist->offset,
  21411. + my_hist->hist_array[index]);
  21412. + return 0;
  21413. +}
  21414. +
  21415. +static const struct seq_operations latency_hist_seq_op = {
  21416. + .start = l_start,
  21417. + .next = l_next,
  21418. + .stop = l_stop,
  21419. + .show = l_show
  21420. +};
  21421. +
  21422. +static int latency_hist_open(struct inode *inode, struct file *file)
  21423. +{
  21424. + int ret;
  21425. +
  21426. + ret = seq_open(file, &latency_hist_seq_op);
  21427. + if (!ret) {
  21428. + struct seq_file *seq = file->private_data;
  21429. + seq->private = inode->i_private;
  21430. + }
  21431. + return ret;
  21432. +}
  21433. +
  21434. +static const struct file_operations latency_hist_fops = {
  21435. + .open = latency_hist_open,
  21436. + .read = seq_read,
  21437. + .llseek = seq_lseek,
  21438. + .release = seq_release,
  21439. +};
  21440. +
  21441. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  21442. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  21443. +static void clear_maxlatprocdata(struct maxlatproc_data *mp)
  21444. +{
  21445. + mp->comm[0] = mp->current_comm[0] = '\0';
  21446. + mp->prio = mp->current_prio = mp->pid = mp->current_pid =
  21447. + mp->latency = mp->timeroffset = -1;
  21448. + mp->timestamp = 0;
  21449. +}
  21450. +#endif
  21451. +
  21452. +static void hist_reset(struct hist_data *hist)
  21453. +{
  21454. + atomic_dec(&hist->hist_mode);
  21455. +
  21456. + memset(hist->hist_array, 0, sizeof(hist->hist_array));
  21457. + hist->below_hist_bound_samples = 0ULL;
  21458. + hist->above_hist_bound_samples = 0ULL;
  21459. + hist->min_lat = LONG_MAX;
  21460. + hist->max_lat = LONG_MIN;
  21461. + hist->total_samples = 0ULL;
  21462. + hist->accumulate_lat = 0LL;
  21463. +
  21464. + atomic_inc(&hist->hist_mode);
  21465. +}
  21466. +
  21467. +static ssize_t
  21468. +latency_hist_reset(struct file *file, const char __user *a,
  21469. + size_t size, loff_t *off)
  21470. +{
  21471. + int cpu;
  21472. + struct hist_data *hist = NULL;
  21473. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  21474. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  21475. + struct maxlatproc_data *mp = NULL;
  21476. +#endif
  21477. + off_t latency_type = (off_t) file->private_data;
  21478. +
  21479. + for_each_online_cpu(cpu) {
  21480. +
  21481. + switch (latency_type) {
  21482. +#ifdef CONFIG_PREEMPT_OFF_HIST
  21483. + case PREEMPTOFF_LATENCY:
  21484. + hist = &per_cpu(preemptoff_hist, cpu);
  21485. + break;
  21486. +#endif
  21487. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  21488. + case IRQSOFF_LATENCY:
  21489. + hist = &per_cpu(irqsoff_hist, cpu);
  21490. + break;
  21491. +#endif
  21492. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  21493. + case PREEMPTIRQSOFF_LATENCY:
  21494. + hist = &per_cpu(preemptirqsoff_hist, cpu);
  21495. + break;
  21496. +#endif
  21497. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  21498. + case WAKEUP_LATENCY:
  21499. + hist = &per_cpu(wakeup_latency_hist, cpu);
  21500. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  21501. + break;
  21502. + case WAKEUP_LATENCY_SHAREDPRIO:
  21503. + hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  21504. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  21505. + break;
  21506. +#endif
  21507. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  21508. + case MISSED_TIMER_OFFSETS:
  21509. + hist = &per_cpu(missed_timer_offsets, cpu);
  21510. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  21511. + break;
  21512. +#endif
  21513. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  21514. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  21515. + case TIMERANDWAKEUP_LATENCY:
  21516. + hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  21517. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  21518. + break;
  21519. +#endif
  21520. + }
  21521. +
  21522. + hist_reset(hist);
  21523. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  21524. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  21525. + if (latency_type == WAKEUP_LATENCY ||
  21526. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  21527. + latency_type == MISSED_TIMER_OFFSETS ||
  21528. + latency_type == TIMERANDWAKEUP_LATENCY)
  21529. + clear_maxlatprocdata(mp);
  21530. +#endif
  21531. + }
  21532. +
  21533. + return size;
  21534. +}
  21535. +
  21536. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  21537. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  21538. +static ssize_t
  21539. +show_pid(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  21540. +{
  21541. + char buf[64];
  21542. + int r;
  21543. + unsigned long *this_pid = file->private_data;
  21544. +
  21545. + r = snprintf(buf, sizeof(buf), "%lu\n", *this_pid);
  21546. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  21547. +}
  21548. +
  21549. +static ssize_t do_pid(struct file *file, const char __user *ubuf,
  21550. + size_t cnt, loff_t *ppos)
  21551. +{
  21552. + char buf[64];
  21553. + unsigned long pid;
  21554. + unsigned long *this_pid = file->private_data;
  21555. +
  21556. + if (cnt >= sizeof(buf))
  21557. + return -EINVAL;
  21558. +
  21559. + if (copy_from_user(&buf, ubuf, cnt))
  21560. + return -EFAULT;
  21561. +
  21562. + buf[cnt] = '\0';
  21563. +
  21564. + if (kstrtoul(buf, 10, &pid))
  21565. + return -EINVAL;
  21566. +
  21567. + *this_pid = pid;
  21568. +
  21569. + return cnt;
  21570. +}
  21571. +#endif
  21572. +
  21573. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  21574. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  21575. +static ssize_t
  21576. +show_maxlatproc(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  21577. +{
  21578. + int r;
  21579. + struct maxlatproc_data *mp = file->private_data;
  21580. + int strmaxlen = (TASK_COMM_LEN * 2) + (8 * 8);
  21581. + unsigned long long t;
  21582. + unsigned long usecs, secs;
  21583. + char *buf;
  21584. +
  21585. + if (mp->pid == -1 || mp->current_pid == -1) {
  21586. + buf = "(none)\n";
  21587. + return simple_read_from_buffer(ubuf, cnt, ppos, buf,
  21588. + strlen(buf));
  21589. + }
  21590. +
  21591. + buf = kmalloc(strmaxlen, GFP_KERNEL);
  21592. + if (buf == NULL)
  21593. + return -ENOMEM;
  21594. +
  21595. + t = ns2usecs(mp->timestamp);
  21596. + usecs = do_div(t, USEC_PER_SEC);
  21597. + secs = (unsigned long) t;
  21598. + r = snprintf(buf, strmaxlen,
  21599. + "%d %d %ld (%ld) %s <- %d %d %s %lu.%06lu\n", mp->pid,
  21600. + MAX_RT_PRIO-1 - mp->prio, mp->latency, mp->timeroffset, mp->comm,
  21601. + mp->current_pid, MAX_RT_PRIO-1 - mp->current_prio, mp->current_comm,
  21602. + secs, usecs);
  21603. + r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  21604. + kfree(buf);
  21605. + return r;
  21606. +}
  21607. +#endif
  21608. +
  21609. +static ssize_t
  21610. +show_enable(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  21611. +{
  21612. + char buf[64];
  21613. + struct enable_data *ed = file->private_data;
  21614. + int r;
  21615. +
  21616. + r = snprintf(buf, sizeof(buf), "%d\n", ed->enabled);
  21617. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  21618. +}
  21619. +
  21620. +static ssize_t
  21621. +do_enable(struct file *file, const char __user *ubuf, size_t cnt, loff_t *ppos)
  21622. +{
  21623. + char buf[64];
  21624. + long enable;
  21625. + struct enable_data *ed = file->private_data;
  21626. +
  21627. + if (cnt >= sizeof(buf))
  21628. + return -EINVAL;
  21629. +
  21630. + if (copy_from_user(&buf, ubuf, cnt))
  21631. + return -EFAULT;
  21632. +
  21633. + buf[cnt] = 0;
  21634. +
  21635. + if (kstrtoul(buf, 10, &enable))
  21636. + return -EINVAL;
  21637. +
  21638. + if ((enable && ed->enabled) || (!enable && !ed->enabled))
  21639. + return cnt;
  21640. +
  21641. + if (enable) {
  21642. + int ret;
  21643. +
  21644. + switch (ed->latency_type) {
  21645. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  21646. + case PREEMPTIRQSOFF_LATENCY:
  21647. + ret = register_trace_preemptirqsoff_hist(
  21648. + probe_preemptirqsoff_hist, NULL);
  21649. + if (ret) {
  21650. + pr_info("wakeup trace: Couldn't assign "
  21651. + "probe_preemptirqsoff_hist "
  21652. + "to trace_preemptirqsoff_hist\n");
  21653. + return ret;
  21654. + }
  21655. + break;
  21656. +#endif
  21657. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  21658. + case WAKEUP_LATENCY:
  21659. + ret = register_trace_sched_wakeup(
  21660. + probe_wakeup_latency_hist_start, NULL);
  21661. + if (ret) {
  21662. + pr_info("wakeup trace: Couldn't assign "
  21663. + "probe_wakeup_latency_hist_start "
  21664. + "to trace_sched_wakeup\n");
  21665. + return ret;
  21666. + }
  21667. + ret = register_trace_sched_wakeup_new(
  21668. + probe_wakeup_latency_hist_start, NULL);
  21669. + if (ret) {
  21670. + pr_info("wakeup trace: Couldn't assign "
  21671. + "probe_wakeup_latency_hist_start "
  21672. + "to trace_sched_wakeup_new\n");
  21673. + unregister_trace_sched_wakeup(
  21674. + probe_wakeup_latency_hist_start, NULL);
  21675. + return ret;
  21676. + }
  21677. + ret = register_trace_sched_switch(
  21678. + probe_wakeup_latency_hist_stop, NULL);
  21679. + if (ret) {
  21680. + pr_info("wakeup trace: Couldn't assign "
  21681. + "probe_wakeup_latency_hist_stop "
  21682. + "to trace_sched_switch\n");
  21683. + unregister_trace_sched_wakeup(
  21684. + probe_wakeup_latency_hist_start, NULL);
  21685. + unregister_trace_sched_wakeup_new(
  21686. + probe_wakeup_latency_hist_start, NULL);
  21687. + return ret;
  21688. + }
  21689. + ret = register_trace_sched_migrate_task(
  21690. + probe_sched_migrate_task, NULL);
  21691. + if (ret) {
  21692. + pr_info("wakeup trace: Couldn't assign "
  21693. + "probe_sched_migrate_task "
  21694. + "to trace_sched_migrate_task\n");
  21695. + unregister_trace_sched_wakeup(
  21696. + probe_wakeup_latency_hist_start, NULL);
  21697. + unregister_trace_sched_wakeup_new(
  21698. + probe_wakeup_latency_hist_start, NULL);
  21699. + unregister_trace_sched_switch(
  21700. + probe_wakeup_latency_hist_stop, NULL);
  21701. + return ret;
  21702. + }
  21703. + break;
  21704. +#endif
  21705. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  21706. + case MISSED_TIMER_OFFSETS:
  21707. + ret = register_trace_hrtimer_interrupt(
  21708. + probe_hrtimer_interrupt, NULL);
  21709. + if (ret) {
  21710. + pr_info("wakeup trace: Couldn't assign "
  21711. + "probe_hrtimer_interrupt "
  21712. + "to trace_hrtimer_interrupt\n");
  21713. + return ret;
  21714. + }
  21715. + break;
  21716. +#endif
  21717. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  21718. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  21719. + case TIMERANDWAKEUP_LATENCY:
  21720. + if (!wakeup_latency_enabled_data.enabled ||
  21721. + !missed_timer_offsets_enabled_data.enabled)
  21722. + return -EINVAL;
  21723. + break;
  21724. +#endif
  21725. + default:
  21726. + break;
  21727. + }
  21728. + } else {
  21729. + switch (ed->latency_type) {
  21730. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  21731. + case PREEMPTIRQSOFF_LATENCY:
  21732. + {
  21733. + int cpu;
  21734. +
  21735. + unregister_trace_preemptirqsoff_hist(
  21736. + probe_preemptirqsoff_hist, NULL);
  21737. + for_each_online_cpu(cpu) {
  21738. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  21739. + per_cpu(hist_irqsoff_counting,
  21740. + cpu) = 0;
  21741. +#endif
  21742. +#ifdef CONFIG_PREEMPT_OFF_HIST
  21743. + per_cpu(hist_preemptoff_counting,
  21744. + cpu) = 0;
  21745. +#endif
  21746. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  21747. + per_cpu(hist_preemptirqsoff_counting,
  21748. + cpu) = 0;
  21749. +#endif
  21750. + }
  21751. + }
  21752. + break;
  21753. +#endif
  21754. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  21755. + case WAKEUP_LATENCY:
  21756. + {
  21757. + int cpu;
  21758. +
  21759. + unregister_trace_sched_wakeup(
  21760. + probe_wakeup_latency_hist_start, NULL);
  21761. + unregister_trace_sched_wakeup_new(
  21762. + probe_wakeup_latency_hist_start, NULL);
  21763. + unregister_trace_sched_switch(
  21764. + probe_wakeup_latency_hist_stop, NULL);
  21765. + unregister_trace_sched_migrate_task(
  21766. + probe_sched_migrate_task, NULL);
  21767. +
  21768. + for_each_online_cpu(cpu) {
  21769. + per_cpu(wakeup_task, cpu) = NULL;
  21770. + per_cpu(wakeup_sharedprio, cpu) = 0;
  21771. + }
  21772. + }
  21773. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  21774. + timerandwakeup_enabled_data.enabled = 0;
  21775. +#endif
  21776. + break;
  21777. +#endif
  21778. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  21779. + case MISSED_TIMER_OFFSETS:
  21780. + unregister_trace_hrtimer_interrupt(
  21781. + probe_hrtimer_interrupt, NULL);
  21782. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  21783. + timerandwakeup_enabled_data.enabled = 0;
  21784. +#endif
  21785. + break;
  21786. +#endif
  21787. + default:
  21788. + break;
  21789. + }
  21790. + }
  21791. + ed->enabled = enable;
  21792. + return cnt;
  21793. +}
  21794. +
  21795. +static const struct file_operations latency_hist_reset_fops = {
  21796. + .open = tracing_open_generic,
  21797. + .write = latency_hist_reset,
  21798. +};
  21799. +
  21800. +static const struct file_operations enable_fops = {
  21801. + .open = tracing_open_generic,
  21802. + .read = show_enable,
  21803. + .write = do_enable,
  21804. +};
  21805. +
  21806. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  21807. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  21808. +static const struct file_operations pid_fops = {
  21809. + .open = tracing_open_generic,
  21810. + .read = show_pid,
  21811. + .write = do_pid,
  21812. +};
  21813. +
  21814. +static const struct file_operations maxlatproc_fops = {
  21815. + .open = tracing_open_generic,
  21816. + .read = show_maxlatproc,
  21817. +};
  21818. +#endif
  21819. +
  21820. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  21821. +static notrace void probe_preemptirqsoff_hist(void *v, int reason,
  21822. + int starthist)
  21823. +{
  21824. + int cpu = raw_smp_processor_id();
  21825. + int time_set = 0;
  21826. +
  21827. + if (starthist) {
  21828. + cycle_t uninitialized_var(start);
  21829. +
  21830. + if (!preempt_count() && !irqs_disabled())
  21831. + return;
  21832. +
  21833. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  21834. + if ((reason == IRQS_OFF || reason == TRACE_START) &&
  21835. + !per_cpu(hist_irqsoff_counting, cpu)) {
  21836. + per_cpu(hist_irqsoff_counting, cpu) = 1;
  21837. + start = ftrace_now(cpu);
  21838. + time_set++;
  21839. + per_cpu(hist_irqsoff_start, cpu) = start;
  21840. + }
  21841. +#endif
  21842. +
  21843. +#ifdef CONFIG_PREEMPT_OFF_HIST
  21844. + if ((reason == PREEMPT_OFF || reason == TRACE_START) &&
  21845. + !per_cpu(hist_preemptoff_counting, cpu)) {
  21846. + per_cpu(hist_preemptoff_counting, cpu) = 1;
  21847. + if (!(time_set++))
  21848. + start = ftrace_now(cpu);
  21849. + per_cpu(hist_preemptoff_start, cpu) = start;
  21850. + }
  21851. +#endif
  21852. +
  21853. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  21854. + if (per_cpu(hist_irqsoff_counting, cpu) &&
  21855. + per_cpu(hist_preemptoff_counting, cpu) &&
  21856. + !per_cpu(hist_preemptirqsoff_counting, cpu)) {
  21857. + per_cpu(hist_preemptirqsoff_counting, cpu) = 1;
  21858. + if (!time_set)
  21859. + start = ftrace_now(cpu);
  21860. + per_cpu(hist_preemptirqsoff_start, cpu) = start;
  21861. + }
  21862. +#endif
  21863. + } else {
  21864. + cycle_t uninitialized_var(stop);
  21865. +
  21866. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  21867. + if ((reason == IRQS_ON || reason == TRACE_STOP) &&
  21868. + per_cpu(hist_irqsoff_counting, cpu)) {
  21869. + cycle_t start = per_cpu(hist_irqsoff_start, cpu);
  21870. +
  21871. + stop = ftrace_now(cpu);
  21872. + time_set++;
  21873. + if (start) {
  21874. + long latency = ((long) (stop - start)) /
  21875. + NSECS_PER_USECS;
  21876. +
  21877. + latency_hist(IRQSOFF_LATENCY, cpu, latency, 0,
  21878. + stop, NULL);
  21879. + }
  21880. + per_cpu(hist_irqsoff_counting, cpu) = 0;
  21881. + }
  21882. +#endif
  21883. +
  21884. +#ifdef CONFIG_PREEMPT_OFF_HIST
  21885. + if ((reason == PREEMPT_ON || reason == TRACE_STOP) &&
  21886. + per_cpu(hist_preemptoff_counting, cpu)) {
  21887. + cycle_t start = per_cpu(hist_preemptoff_start, cpu);
  21888. +
  21889. + if (!(time_set++))
  21890. + stop = ftrace_now(cpu);
  21891. + if (start) {
  21892. + long latency = ((long) (stop - start)) /
  21893. + NSECS_PER_USECS;
  21894. +
  21895. + latency_hist(PREEMPTOFF_LATENCY, cpu, latency,
  21896. + 0, stop, NULL);
  21897. + }
  21898. + per_cpu(hist_preemptoff_counting, cpu) = 0;
  21899. + }
  21900. +#endif
  21901. +
  21902. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  21903. + if ((!per_cpu(hist_irqsoff_counting, cpu) ||
  21904. + !per_cpu(hist_preemptoff_counting, cpu)) &&
  21905. + per_cpu(hist_preemptirqsoff_counting, cpu)) {
  21906. + cycle_t start = per_cpu(hist_preemptirqsoff_start, cpu);
  21907. +
  21908. + if (!time_set)
  21909. + stop = ftrace_now(cpu);
  21910. + if (start) {
  21911. + long latency = ((long) (stop - start)) /
  21912. + NSECS_PER_USECS;
  21913. +
  21914. + latency_hist(PREEMPTIRQSOFF_LATENCY, cpu,
  21915. + latency, 0, stop, NULL);
  21916. + }
  21917. + per_cpu(hist_preemptirqsoff_counting, cpu) = 0;
  21918. + }
  21919. +#endif
  21920. + }
  21921. +}
  21922. +#endif
  21923. +
  21924. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  21925. +static DEFINE_RAW_SPINLOCK(wakeup_lock);
  21926. +static notrace void probe_sched_migrate_task(void *v, struct task_struct *task,
  21927. + int cpu)
  21928. +{
  21929. + int old_cpu = task_cpu(task);
  21930. +
  21931. + if (cpu != old_cpu) {
  21932. + unsigned long flags;
  21933. + struct task_struct *cpu_wakeup_task;
  21934. +
  21935. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  21936. +
  21937. + cpu_wakeup_task = per_cpu(wakeup_task, old_cpu);
  21938. + if (task == cpu_wakeup_task) {
  21939. + put_task_struct(cpu_wakeup_task);
  21940. + per_cpu(wakeup_task, old_cpu) = NULL;
  21941. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = task;
  21942. + get_task_struct(cpu_wakeup_task);
  21943. + }
  21944. +
  21945. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  21946. + }
  21947. +}
  21948. +
  21949. +static notrace void probe_wakeup_latency_hist_start(void *v,
  21950. + struct task_struct *p)
  21951. +{
  21952. + unsigned long flags;
  21953. + struct task_struct *curr = current;
  21954. + int cpu = task_cpu(p);
  21955. + struct task_struct *cpu_wakeup_task;
  21956. +
  21957. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  21958. +
  21959. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  21960. +
  21961. + if (wakeup_pid) {
  21962. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  21963. + p->prio == curr->prio)
  21964. + per_cpu(wakeup_sharedprio, cpu) = 1;
  21965. + if (likely(wakeup_pid != task_pid_nr(p)))
  21966. + goto out;
  21967. + } else {
  21968. + if (likely(!rt_task(p)) ||
  21969. + (cpu_wakeup_task && p->prio > cpu_wakeup_task->prio) ||
  21970. + p->prio > curr->prio)
  21971. + goto out;
  21972. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  21973. + p->prio == curr->prio)
  21974. + per_cpu(wakeup_sharedprio, cpu) = 1;
  21975. + }
  21976. +
  21977. + if (cpu_wakeup_task)
  21978. + put_task_struct(cpu_wakeup_task);
  21979. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = p;
  21980. + get_task_struct(cpu_wakeup_task);
  21981. + cpu_wakeup_task->preempt_timestamp_hist =
  21982. + ftrace_now(raw_smp_processor_id());
  21983. +out:
  21984. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  21985. +}
  21986. +
  21987. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  21988. + struct task_struct *prev, struct task_struct *next)
  21989. +{
  21990. + unsigned long flags;
  21991. + int cpu = task_cpu(next);
  21992. + long latency;
  21993. + cycle_t stop;
  21994. + struct task_struct *cpu_wakeup_task;
  21995. +
  21996. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  21997. +
  21998. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  21999. +
  22000. + if (cpu_wakeup_task == NULL)
  22001. + goto out;
  22002. +
  22003. + /* Already running? */
  22004. + if (unlikely(current == cpu_wakeup_task))
  22005. + goto out_reset;
  22006. +
  22007. + if (next != cpu_wakeup_task) {
  22008. + if (next->prio < cpu_wakeup_task->prio)
  22009. + goto out_reset;
  22010. +
  22011. + if (next->prio == cpu_wakeup_task->prio)
  22012. + per_cpu(wakeup_sharedprio, cpu) = 1;
  22013. +
  22014. + goto out;
  22015. + }
  22016. +
  22017. + if (current->prio == cpu_wakeup_task->prio)
  22018. + per_cpu(wakeup_sharedprio, cpu) = 1;
  22019. +
  22020. + /*
  22021. + * The task we are waiting for is about to be switched to.
  22022. + * Calculate latency and store it in histogram.
  22023. + */
  22024. + stop = ftrace_now(raw_smp_processor_id());
  22025. +
  22026. + latency = ((long) (stop - next->preempt_timestamp_hist)) /
  22027. + NSECS_PER_USECS;
  22028. +
  22029. + if (per_cpu(wakeup_sharedprio, cpu)) {
  22030. + latency_hist(WAKEUP_LATENCY_SHAREDPRIO, cpu, latency, 0, stop,
  22031. + next);
  22032. + per_cpu(wakeup_sharedprio, cpu) = 0;
  22033. + } else {
  22034. + latency_hist(WAKEUP_LATENCY, cpu, latency, 0, stop, next);
  22035. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22036. + if (timerandwakeup_enabled_data.enabled) {
  22037. + latency_hist(TIMERANDWAKEUP_LATENCY, cpu,
  22038. + next->timer_offset + latency, next->timer_offset,
  22039. + stop, next);
  22040. + }
  22041. +#endif
  22042. + }
  22043. +
  22044. +out_reset:
  22045. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22046. + next->timer_offset = 0;
  22047. +#endif
  22048. + put_task_struct(cpu_wakeup_task);
  22049. + per_cpu(wakeup_task, cpu) = NULL;
  22050. +out:
  22051. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  22052. +}
  22053. +#endif
  22054. +
  22055. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22056. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  22057. + long long latency_ns, struct task_struct *curr,
  22058. + struct task_struct *task)
  22059. +{
  22060. + if (latency_ns <= 0 && task != NULL && rt_task(task) &&
  22061. + (task->prio < curr->prio ||
  22062. + (task->prio == curr->prio &&
  22063. + !cpumask_test_cpu(cpu, &task->cpus_allowed)))) {
  22064. + long latency;
  22065. + cycle_t now;
  22066. +
  22067. + if (missed_timer_offsets_pid) {
  22068. + if (likely(missed_timer_offsets_pid !=
  22069. + task_pid_nr(task)))
  22070. + return;
  22071. + }
  22072. +
  22073. + now = ftrace_now(cpu);
  22074. + latency = (long) div_s64(-latency_ns, NSECS_PER_USECS);
  22075. + latency_hist(MISSED_TIMER_OFFSETS, cpu, latency, latency, now,
  22076. + task);
  22077. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22078. + task->timer_offset = latency;
  22079. +#endif
  22080. + }
  22081. +}
  22082. +#endif
  22083. +
  22084. +static __init int latency_hist_init(void)
  22085. +{
  22086. + struct dentry *latency_hist_root = NULL;
  22087. + struct dentry *dentry;
  22088. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22089. + struct dentry *dentry_sharedprio;
  22090. +#endif
  22091. + struct dentry *entry;
  22092. + struct dentry *enable_root;
  22093. + int i = 0;
  22094. + struct hist_data *my_hist;
  22095. + char name[64];
  22096. + char *cpufmt = "CPU%d";
  22097. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22098. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22099. + char *cpufmt_maxlatproc = "max_latency-CPU%d";
  22100. + struct maxlatproc_data *mp = NULL;
  22101. +#endif
  22102. +
  22103. + dentry = tracing_init_dentry();
  22104. + latency_hist_root = debugfs_create_dir(latency_hist_dir_root, dentry);
  22105. + enable_root = debugfs_create_dir("enable", latency_hist_root);
  22106. +
  22107. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  22108. + dentry = debugfs_create_dir(irqsoff_hist_dir, latency_hist_root);
  22109. + for_each_possible_cpu(i) {
  22110. + sprintf(name, cpufmt, i);
  22111. + entry = debugfs_create_file(name, 0444, dentry,
  22112. + &per_cpu(irqsoff_hist, i), &latency_hist_fops);
  22113. + my_hist = &per_cpu(irqsoff_hist, i);
  22114. + atomic_set(&my_hist->hist_mode, 1);
  22115. + my_hist->min_lat = LONG_MAX;
  22116. + }
  22117. + entry = debugfs_create_file("reset", 0644, dentry,
  22118. + (void *)IRQSOFF_LATENCY, &latency_hist_reset_fops);
  22119. +#endif
  22120. +
  22121. +#ifdef CONFIG_PREEMPT_OFF_HIST
  22122. + dentry = debugfs_create_dir(preemptoff_hist_dir,
  22123. + latency_hist_root);
  22124. + for_each_possible_cpu(i) {
  22125. + sprintf(name, cpufmt, i);
  22126. + entry = debugfs_create_file(name, 0444, dentry,
  22127. + &per_cpu(preemptoff_hist, i), &latency_hist_fops);
  22128. + my_hist = &per_cpu(preemptoff_hist, i);
  22129. + atomic_set(&my_hist->hist_mode, 1);
  22130. + my_hist->min_lat = LONG_MAX;
  22131. + }
  22132. + entry = debugfs_create_file("reset", 0644, dentry,
  22133. + (void *)PREEMPTOFF_LATENCY, &latency_hist_reset_fops);
  22134. +#endif
  22135. +
  22136. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  22137. + dentry = debugfs_create_dir(preemptirqsoff_hist_dir,
  22138. + latency_hist_root);
  22139. + for_each_possible_cpu(i) {
  22140. + sprintf(name, cpufmt, i);
  22141. + entry = debugfs_create_file(name, 0444, dentry,
  22142. + &per_cpu(preemptirqsoff_hist, i), &latency_hist_fops);
  22143. + my_hist = &per_cpu(preemptirqsoff_hist, i);
  22144. + atomic_set(&my_hist->hist_mode, 1);
  22145. + my_hist->min_lat = LONG_MAX;
  22146. + }
  22147. + entry = debugfs_create_file("reset", 0644, dentry,
  22148. + (void *)PREEMPTIRQSOFF_LATENCY, &latency_hist_reset_fops);
  22149. +#endif
  22150. +
  22151. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  22152. + entry = debugfs_create_file("preemptirqsoff", 0644,
  22153. + enable_root, (void *)&preemptirqsoff_enabled_data,
  22154. + &enable_fops);
  22155. +#endif
  22156. +
  22157. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22158. + dentry = debugfs_create_dir(wakeup_latency_hist_dir,
  22159. + latency_hist_root);
  22160. + dentry_sharedprio = debugfs_create_dir(
  22161. + wakeup_latency_hist_dir_sharedprio, dentry);
  22162. + for_each_possible_cpu(i) {
  22163. + sprintf(name, cpufmt, i);
  22164. +
  22165. + entry = debugfs_create_file(name, 0444, dentry,
  22166. + &per_cpu(wakeup_latency_hist, i),
  22167. + &latency_hist_fops);
  22168. + my_hist = &per_cpu(wakeup_latency_hist, i);
  22169. + atomic_set(&my_hist->hist_mode, 1);
  22170. + my_hist->min_lat = LONG_MAX;
  22171. +
  22172. + entry = debugfs_create_file(name, 0444, dentry_sharedprio,
  22173. + &per_cpu(wakeup_latency_hist_sharedprio, i),
  22174. + &latency_hist_fops);
  22175. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, i);
  22176. + atomic_set(&my_hist->hist_mode, 1);
  22177. + my_hist->min_lat = LONG_MAX;
  22178. +
  22179. + sprintf(name, cpufmt_maxlatproc, i);
  22180. +
  22181. + mp = &per_cpu(wakeup_maxlatproc, i);
  22182. + entry = debugfs_create_file(name, 0444, dentry, mp,
  22183. + &maxlatproc_fops);
  22184. + clear_maxlatprocdata(mp);
  22185. +
  22186. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, i);
  22187. + entry = debugfs_create_file(name, 0444, dentry_sharedprio, mp,
  22188. + &maxlatproc_fops);
  22189. + clear_maxlatprocdata(mp);
  22190. + }
  22191. + entry = debugfs_create_file("pid", 0644, dentry,
  22192. + (void *)&wakeup_pid, &pid_fops);
  22193. + entry = debugfs_create_file("reset", 0644, dentry,
  22194. + (void *)WAKEUP_LATENCY, &latency_hist_reset_fops);
  22195. + entry = debugfs_create_file("reset", 0644, dentry_sharedprio,
  22196. + (void *)WAKEUP_LATENCY_SHAREDPRIO, &latency_hist_reset_fops);
  22197. + entry = debugfs_create_file("wakeup", 0644,
  22198. + enable_root, (void *)&wakeup_latency_enabled_data,
  22199. + &enable_fops);
  22200. +#endif
  22201. +
  22202. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22203. + dentry = debugfs_create_dir(missed_timer_offsets_dir,
  22204. + latency_hist_root);
  22205. + for_each_possible_cpu(i) {
  22206. + sprintf(name, cpufmt, i);
  22207. + entry = debugfs_create_file(name, 0444, dentry,
  22208. + &per_cpu(missed_timer_offsets, i), &latency_hist_fops);
  22209. + my_hist = &per_cpu(missed_timer_offsets, i);
  22210. + atomic_set(&my_hist->hist_mode, 1);
  22211. + my_hist->min_lat = LONG_MAX;
  22212. +
  22213. + sprintf(name, cpufmt_maxlatproc, i);
  22214. + mp = &per_cpu(missed_timer_offsets_maxlatproc, i);
  22215. + entry = debugfs_create_file(name, 0444, dentry, mp,
  22216. + &maxlatproc_fops);
  22217. + clear_maxlatprocdata(mp);
  22218. + }
  22219. + entry = debugfs_create_file("pid", 0644, dentry,
  22220. + (void *)&missed_timer_offsets_pid, &pid_fops);
  22221. + entry = debugfs_create_file("reset", 0644, dentry,
  22222. + (void *)MISSED_TIMER_OFFSETS, &latency_hist_reset_fops);
  22223. + entry = debugfs_create_file("missed_timer_offsets", 0644,
  22224. + enable_root, (void *)&missed_timer_offsets_enabled_data,
  22225. + &enable_fops);
  22226. +#endif
  22227. +
  22228. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  22229. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22230. + dentry = debugfs_create_dir(timerandwakeup_latency_hist_dir,
  22231. + latency_hist_root);
  22232. + for_each_possible_cpu(i) {
  22233. + sprintf(name, cpufmt, i);
  22234. + entry = debugfs_create_file(name, 0444, dentry,
  22235. + &per_cpu(timerandwakeup_latency_hist, i),
  22236. + &latency_hist_fops);
  22237. + my_hist = &per_cpu(timerandwakeup_latency_hist, i);
  22238. + atomic_set(&my_hist->hist_mode, 1);
  22239. + my_hist->min_lat = LONG_MAX;
  22240. +
  22241. + sprintf(name, cpufmt_maxlatproc, i);
  22242. + mp = &per_cpu(timerandwakeup_maxlatproc, i);
  22243. + entry = debugfs_create_file(name, 0444, dentry, mp,
  22244. + &maxlatproc_fops);
  22245. + clear_maxlatprocdata(mp);
  22246. + }
  22247. + entry = debugfs_create_file("reset", 0644, dentry,
  22248. + (void *)TIMERANDWAKEUP_LATENCY, &latency_hist_reset_fops);
  22249. + entry = debugfs_create_file("timerandwakeup", 0644,
  22250. + enable_root, (void *)&timerandwakeup_enabled_data,
  22251. + &enable_fops);
  22252. +#endif
  22253. + return 0;
  22254. +}
  22255. +
  22256. +device_initcall(latency_hist_init);
  22257. diff -Nur linux-4.1.13.orig/kernel/trace/Makefile linux-4.1.13/kernel/trace/Makefile
  22258. --- linux-4.1.13.orig/kernel/trace/Makefile 2015-11-09 23:34:10.000000000 +0100
  22259. +++ linux-4.1.13/kernel/trace/Makefile 2015-11-29 09:23:09.653611260 +0100
  22260. @@ -36,6 +36,10 @@
  22261. obj-$(CONFIG_IRQSOFF_TRACER) += trace_irqsoff.o
  22262. obj-$(CONFIG_PREEMPT_TRACER) += trace_irqsoff.o
  22263. obj-$(CONFIG_SCHED_TRACER) += trace_sched_wakeup.o
  22264. +obj-$(CONFIG_INTERRUPT_OFF_HIST) += latency_hist.o
  22265. +obj-$(CONFIG_PREEMPT_OFF_HIST) += latency_hist.o
  22266. +obj-$(CONFIG_WAKEUP_LATENCY_HIST) += latency_hist.o
  22267. +obj-$(CONFIG_MISSED_TIMER_OFFSETS_HIST) += latency_hist.o
  22268. obj-$(CONFIG_NOP_TRACER) += trace_nop.o
  22269. obj-$(CONFIG_STACK_TRACER) += trace_stack.o
  22270. obj-$(CONFIG_MMIOTRACE) += trace_mmiotrace.o
  22271. diff -Nur linux-4.1.13.orig/kernel/trace/trace.c linux-4.1.13/kernel/trace/trace.c
  22272. --- linux-4.1.13.orig/kernel/trace/trace.c 2015-11-09 23:34:10.000000000 +0100
  22273. +++ linux-4.1.13/kernel/trace/trace.c 2015-11-29 09:23:09.653611260 +0100
  22274. @@ -1630,6 +1630,7 @@
  22275. struct task_struct *tsk = current;
  22276. entry->preempt_count = pc & 0xff;
  22277. + entry->preempt_lazy_count = preempt_lazy_count();
  22278. entry->pid = (tsk) ? tsk->pid : 0;
  22279. entry->flags =
  22280. #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
  22281. @@ -1639,8 +1640,11 @@
  22282. #endif
  22283. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  22284. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  22285. - (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
  22286. + (tif_need_resched_now() ? TRACE_FLAG_NEED_RESCHED : 0) |
  22287. + (need_resched_lazy() ? TRACE_FLAG_NEED_RESCHED_LAZY : 0) |
  22288. (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
  22289. +
  22290. + entry->migrate_disable = (tsk) ? __migrate_disabled(tsk) & 0xFF : 0;
  22291. }
  22292. EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
  22293. @@ -2558,14 +2562,17 @@
  22294. static void print_lat_help_header(struct seq_file *m)
  22295. {
  22296. - seq_puts(m, "# _------=> CPU# \n"
  22297. - "# / _-----=> irqs-off \n"
  22298. - "# | / _----=> need-resched \n"
  22299. - "# || / _---=> hardirq/softirq \n"
  22300. - "# ||| / _--=> preempt-depth \n"
  22301. - "# |||| / delay \n"
  22302. - "# cmd pid ||||| time | caller \n"
  22303. - "# \\ / ||||| \\ | / \n");
  22304. + seq_puts(m, "# _--------=> CPU# \n"
  22305. + "# / _-------=> irqs-off \n"
  22306. + "# | / _------=> need-resched \n"
  22307. + "# || / _-----=> need-resched_lazy \n"
  22308. + "# ||| / _----=> hardirq/softirq \n"
  22309. + "# |||| / _---=> preempt-depth \n"
  22310. + "# ||||| / _--=> preempt-lazy-depth\n"
  22311. + "# |||||| / _-=> migrate-disable \n"
  22312. + "# ||||||| / delay \n"
  22313. + "# cmd pid |||||||| time | caller \n"
  22314. + "# \\ / |||||||| \\ | / \n");
  22315. }
  22316. static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
  22317. @@ -2591,11 +2598,14 @@
  22318. print_event_info(buf, m);
  22319. seq_puts(m, "# _-----=> irqs-off\n"
  22320. "# / _----=> need-resched\n"
  22321. - "# | / _---=> hardirq/softirq\n"
  22322. - "# || / _--=> preempt-depth\n"
  22323. - "# ||| / delay\n"
  22324. - "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"
  22325. - "# | | | |||| | |\n");
  22326. + "# |/ _-----=> need-resched_lazy\n"
  22327. + "# || / _---=> hardirq/softirq\n"
  22328. + "# ||| / _--=> preempt-depth\n"
  22329. + "# |||| /_--=> preempt-lazy-depth\n"
  22330. + "# ||||| _-=> migrate-disable \n"
  22331. + "# ||||| / delay\n"
  22332. + "# TASK-PID CPU# |||||| TIMESTAMP FUNCTION\n"
  22333. + "# | | | |||||| | |\n");
  22334. }
  22335. void
  22336. diff -Nur linux-4.1.13.orig/kernel/trace/trace_events.c linux-4.1.13/kernel/trace/trace_events.c
  22337. --- linux-4.1.13.orig/kernel/trace/trace_events.c 2015-11-09 23:34:10.000000000 +0100
  22338. +++ linux-4.1.13/kernel/trace/trace_events.c 2015-11-29 09:23:09.653611260 +0100
  22339. @@ -162,6 +162,8 @@
  22340. __common_field(unsigned char, flags);
  22341. __common_field(unsigned char, preempt_count);
  22342. __common_field(int, pid);
  22343. + __common_field(unsigned short, migrate_disable);
  22344. + __common_field(unsigned short, padding);
  22345. return ret;
  22346. }
  22347. diff -Nur linux-4.1.13.orig/kernel/trace/trace.h linux-4.1.13/kernel/trace/trace.h
  22348. --- linux-4.1.13.orig/kernel/trace/trace.h 2015-11-09 23:34:10.000000000 +0100
  22349. +++ linux-4.1.13/kernel/trace/trace.h 2015-11-29 09:23:09.653611260 +0100
  22350. @@ -120,6 +120,7 @@
  22351. * NEED_RESCHED - reschedule is requested
  22352. * HARDIRQ - inside an interrupt handler
  22353. * SOFTIRQ - inside a softirq handler
  22354. + * NEED_RESCHED_LAZY - lazy reschedule is requested
  22355. */
  22356. enum trace_flag_type {
  22357. TRACE_FLAG_IRQS_OFF = 0x01,
  22358. @@ -128,6 +129,7 @@
  22359. TRACE_FLAG_HARDIRQ = 0x08,
  22360. TRACE_FLAG_SOFTIRQ = 0x10,
  22361. TRACE_FLAG_PREEMPT_RESCHED = 0x20,
  22362. + TRACE_FLAG_NEED_RESCHED_LAZY = 0x40,
  22363. };
  22364. #define TRACE_BUF_SIZE 1024
  22365. diff -Nur linux-4.1.13.orig/kernel/trace/trace_irqsoff.c linux-4.1.13/kernel/trace/trace_irqsoff.c
  22366. --- linux-4.1.13.orig/kernel/trace/trace_irqsoff.c 2015-11-09 23:34:10.000000000 +0100
  22367. +++ linux-4.1.13/kernel/trace/trace_irqsoff.c 2015-11-29 09:23:09.653611260 +0100
  22368. @@ -13,6 +13,7 @@
  22369. #include <linux/uaccess.h>
  22370. #include <linux/module.h>
  22371. #include <linux/ftrace.h>
  22372. +#include <trace/events/hist.h>
  22373. #include "trace.h"
  22374. @@ -433,11 +434,13 @@
  22375. {
  22376. if (preempt_trace() || irq_trace())
  22377. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  22378. + trace_preemptirqsoff_hist(TRACE_START, 1);
  22379. }
  22380. EXPORT_SYMBOL_GPL(start_critical_timings);
  22381. void stop_critical_timings(void)
  22382. {
  22383. + trace_preemptirqsoff_hist(TRACE_STOP, 0);
  22384. if (preempt_trace() || irq_trace())
  22385. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  22386. }
  22387. @@ -447,6 +450,7 @@
  22388. #ifdef CONFIG_PROVE_LOCKING
  22389. void time_hardirqs_on(unsigned long a0, unsigned long a1)
  22390. {
  22391. + trace_preemptirqsoff_hist_rcuidle(IRQS_ON, 0);
  22392. if (!preempt_trace() && irq_trace())
  22393. stop_critical_timing(a0, a1);
  22394. }
  22395. @@ -455,6 +459,7 @@
  22396. {
  22397. if (!preempt_trace() && irq_trace())
  22398. start_critical_timing(a0, a1);
  22399. + trace_preemptirqsoff_hist_rcuidle(IRQS_OFF, 1);
  22400. }
  22401. #else /* !CONFIG_PROVE_LOCKING */
  22402. @@ -480,6 +485,7 @@
  22403. */
  22404. void trace_hardirqs_on(void)
  22405. {
  22406. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  22407. if (!preempt_trace() && irq_trace())
  22408. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  22409. }
  22410. @@ -489,11 +495,13 @@
  22411. {
  22412. if (!preempt_trace() && irq_trace())
  22413. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  22414. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  22415. }
  22416. EXPORT_SYMBOL(trace_hardirqs_off);
  22417. __visible void trace_hardirqs_on_caller(unsigned long caller_addr)
  22418. {
  22419. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  22420. if (!preempt_trace() && irq_trace())
  22421. stop_critical_timing(CALLER_ADDR0, caller_addr);
  22422. }
  22423. @@ -503,6 +511,7 @@
  22424. {
  22425. if (!preempt_trace() && irq_trace())
  22426. start_critical_timing(CALLER_ADDR0, caller_addr);
  22427. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  22428. }
  22429. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  22430. @@ -512,12 +521,14 @@
  22431. #ifdef CONFIG_PREEMPT_TRACER
  22432. void trace_preempt_on(unsigned long a0, unsigned long a1)
  22433. {
  22434. + trace_preemptirqsoff_hist(PREEMPT_ON, 0);
  22435. if (preempt_trace() && !irq_trace())
  22436. stop_critical_timing(a0, a1);
  22437. }
  22438. void trace_preempt_off(unsigned long a0, unsigned long a1)
  22439. {
  22440. + trace_preemptirqsoff_hist(PREEMPT_ON, 1);
  22441. if (preempt_trace() && !irq_trace())
  22442. start_critical_timing(a0, a1);
  22443. }
  22444. diff -Nur linux-4.1.13.orig/kernel/trace/trace_output.c linux-4.1.13/kernel/trace/trace_output.c
  22445. --- linux-4.1.13.orig/kernel/trace/trace_output.c 2015-11-09 23:34:10.000000000 +0100
  22446. +++ linux-4.1.13/kernel/trace/trace_output.c 2015-11-29 09:23:09.653611260 +0100
  22447. @@ -430,6 +430,7 @@
  22448. {
  22449. char hardsoft_irq;
  22450. char need_resched;
  22451. + char need_resched_lazy;
  22452. char irqs_off;
  22453. int hardirq;
  22454. int softirq;
  22455. @@ -457,6 +458,8 @@
  22456. need_resched = '.';
  22457. break;
  22458. }
  22459. + need_resched_lazy =
  22460. + (entry->flags & TRACE_FLAG_NEED_RESCHED_LAZY) ? 'L' : '.';
  22461. hardsoft_irq =
  22462. (hardirq && softirq) ? 'H' :
  22463. @@ -464,14 +467,25 @@
  22464. softirq ? 's' :
  22465. '.';
  22466. - trace_seq_printf(s, "%c%c%c",
  22467. - irqs_off, need_resched, hardsoft_irq);
  22468. + trace_seq_printf(s, "%c%c%c%c",
  22469. + irqs_off, need_resched, need_resched_lazy,
  22470. + hardsoft_irq);
  22471. if (entry->preempt_count)
  22472. trace_seq_printf(s, "%x", entry->preempt_count);
  22473. else
  22474. trace_seq_putc(s, '.');
  22475. + if (entry->preempt_lazy_count)
  22476. + trace_seq_printf(s, "%x", entry->preempt_lazy_count);
  22477. + else
  22478. + trace_seq_putc(s, '.');
  22479. +
  22480. + if (entry->migrate_disable)
  22481. + trace_seq_printf(s, "%x", entry->migrate_disable);
  22482. + else
  22483. + trace_seq_putc(s, '.');
  22484. +
  22485. return !trace_seq_has_overflowed(s);
  22486. }
  22487. diff -Nur linux-4.1.13.orig/kernel/trace/trace_sched_switch.c linux-4.1.13/kernel/trace/trace_sched_switch.c
  22488. --- linux-4.1.13.orig/kernel/trace/trace_sched_switch.c 2015-11-09 23:34:10.000000000 +0100
  22489. +++ linux-4.1.13/kernel/trace/trace_sched_switch.c 2015-11-29 09:23:09.653611260 +0100
  22490. @@ -26,7 +26,7 @@
  22491. }
  22492. static void
  22493. -probe_sched_wakeup(void *ignore, struct task_struct *wakee, int success)
  22494. +probe_sched_wakeup(void *ignore, struct task_struct *wakee)
  22495. {
  22496. if (unlikely(!sched_ref))
  22497. return;
  22498. diff -Nur linux-4.1.13.orig/kernel/trace/trace_sched_wakeup.c linux-4.1.13/kernel/trace/trace_sched_wakeup.c
  22499. --- linux-4.1.13.orig/kernel/trace/trace_sched_wakeup.c 2015-11-09 23:34:10.000000000 +0100
  22500. +++ linux-4.1.13/kernel/trace/trace_sched_wakeup.c 2015-11-29 09:23:09.657610995 +0100
  22501. @@ -514,7 +514,7 @@
  22502. }
  22503. static void
  22504. -probe_wakeup(void *ignore, struct task_struct *p, int success)
  22505. +probe_wakeup(void *ignore, struct task_struct *p)
  22506. {
  22507. struct trace_array_cpu *data;
  22508. int cpu = smp_processor_id();
  22509. diff -Nur linux-4.1.13.orig/kernel/user.c linux-4.1.13/kernel/user.c
  22510. --- linux-4.1.13.orig/kernel/user.c 2015-11-09 23:34:10.000000000 +0100
  22511. +++ linux-4.1.13/kernel/user.c 2015-11-29 09:23:09.657610995 +0100
  22512. @@ -161,11 +161,11 @@
  22513. if (!up)
  22514. return;
  22515. - local_irq_save(flags);
  22516. + local_irq_save_nort(flags);
  22517. if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  22518. free_user(up, flags);
  22519. else
  22520. - local_irq_restore(flags);
  22521. + local_irq_restore_nort(flags);
  22522. }
  22523. struct user_struct *alloc_uid(kuid_t uid)
  22524. diff -Nur linux-4.1.13.orig/kernel/watchdog.c linux-4.1.13/kernel/watchdog.c
  22525. --- linux-4.1.13.orig/kernel/watchdog.c 2015-11-09 23:34:10.000000000 +0100
  22526. +++ linux-4.1.13/kernel/watchdog.c 2015-11-29 09:23:09.657610995 +0100
  22527. @@ -262,6 +262,8 @@
  22528. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  22529. +static DEFINE_RAW_SPINLOCK(watchdog_output_lock);
  22530. +
  22531. static struct perf_event_attr wd_hw_attr = {
  22532. .type = PERF_TYPE_HARDWARE,
  22533. .config = PERF_COUNT_HW_CPU_CYCLES,
  22534. @@ -295,13 +297,21 @@
  22535. /* only print hardlockups once */
  22536. if (__this_cpu_read(hard_watchdog_warn) == true)
  22537. return;
  22538. + /*
  22539. + * If early-printk is enabled then make sure we do not
  22540. + * lock up in printk() and kill console logging:
  22541. + */
  22542. + printk_kill();
  22543. - if (hardlockup_panic)
  22544. + if (hardlockup_panic) {
  22545. panic("Watchdog detected hard LOCKUP on cpu %d",
  22546. this_cpu);
  22547. - else
  22548. + } else {
  22549. + raw_spin_lock(&watchdog_output_lock);
  22550. WARN(1, "Watchdog detected hard LOCKUP on cpu %d",
  22551. this_cpu);
  22552. + raw_spin_unlock(&watchdog_output_lock);
  22553. + }
  22554. __this_cpu_write(hard_watchdog_warn, true);
  22555. return;
  22556. @@ -444,6 +454,7 @@
  22557. /* kick off the timer for the hardlockup detector */
  22558. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  22559. hrtimer->function = watchdog_timer_fn;
  22560. + hrtimer->irqsafe = 1;
  22561. /* Enable the perf event */
  22562. watchdog_nmi_enable(cpu);
  22563. diff -Nur linux-4.1.13.orig/kernel/workqueue.c linux-4.1.13/kernel/workqueue.c
  22564. --- linux-4.1.13.orig/kernel/workqueue.c 2015-11-09 23:34:10.000000000 +0100
  22565. +++ linux-4.1.13/kernel/workqueue.c 2015-11-29 09:23:09.657610995 +0100
  22566. @@ -48,6 +48,8 @@
  22567. #include <linux/nodemask.h>
  22568. #include <linux/moduleparam.h>
  22569. #include <linux/uaccess.h>
  22570. +#include <linux/locallock.h>
  22571. +#include <linux/delay.h>
  22572. #include "workqueue_internal.h"
  22573. @@ -121,15 +123,20 @@
  22574. * cpu or grabbing pool->lock is enough for read access. If
  22575. * POOL_DISASSOCIATED is set, it's identical to L.
  22576. *
  22577. + * On RT we need the extra protection via rt_lock_idle_list() for
  22578. + * the list manipulations against read access from
  22579. + * wq_worker_sleeping(). All other places are nicely serialized via
  22580. + * pool->lock.
  22581. + *
  22582. * A: pool->attach_mutex protected.
  22583. *
  22584. * PL: wq_pool_mutex protected.
  22585. *
  22586. - * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
  22587. + * PR: wq_pool_mutex protected for writes. RCU protected for reads.
  22588. *
  22589. * WQ: wq->mutex protected.
  22590. *
  22591. - * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
  22592. + * WR: wq->mutex protected for writes. RCU protected for reads.
  22593. *
  22594. * MD: wq_mayday_lock protected.
  22595. */
  22596. @@ -178,7 +185,7 @@
  22597. atomic_t nr_running ____cacheline_aligned_in_smp;
  22598. /*
  22599. - * Destruction of pool is sched-RCU protected to allow dereferences
  22600. + * Destruction of pool is RCU protected to allow dereferences
  22601. * from get_work_pool().
  22602. */
  22603. struct rcu_head rcu;
  22604. @@ -207,7 +214,7 @@
  22605. /*
  22606. * Release of unbound pwq is punted to system_wq. See put_pwq()
  22607. * and pwq_unbound_release_workfn() for details. pool_workqueue
  22608. - * itself is also sched-RCU protected so that the first pwq can be
  22609. + * itself is also RCU protected so that the first pwq can be
  22610. * determined without grabbing wq->mutex.
  22611. */
  22612. struct work_struct unbound_release_work;
  22613. @@ -329,6 +336,8 @@
  22614. struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
  22615. EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
  22616. +static DEFINE_LOCAL_IRQ_LOCK(pendingb_lock);
  22617. +
  22618. static int worker_thread(void *__worker);
  22619. static void copy_workqueue_attrs(struct workqueue_attrs *to,
  22620. const struct workqueue_attrs *from);
  22621. @@ -338,14 +347,14 @@
  22622. #include <trace/events/workqueue.h>
  22623. #define assert_rcu_or_pool_mutex() \
  22624. - rcu_lockdep_assert(rcu_read_lock_sched_held() || \
  22625. + rcu_lockdep_assert(rcu_read_lock_held() || \
  22626. lockdep_is_held(&wq_pool_mutex), \
  22627. - "sched RCU or wq_pool_mutex should be held")
  22628. + "RCU or wq_pool_mutex should be held")
  22629. #define assert_rcu_or_wq_mutex(wq) \
  22630. - rcu_lockdep_assert(rcu_read_lock_sched_held() || \
  22631. + rcu_lockdep_assert(rcu_read_lock_held() || \
  22632. lockdep_is_held(&wq->mutex), \
  22633. - "sched RCU or wq->mutex should be held")
  22634. + "RCU or wq->mutex should be held")
  22635. #define for_each_cpu_worker_pool(pool, cpu) \
  22636. for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
  22637. @@ -357,7 +366,7 @@
  22638. * @pool: iteration cursor
  22639. * @pi: integer used for iteration
  22640. *
  22641. - * This must be called either with wq_pool_mutex held or sched RCU read
  22642. + * This must be called either with wq_pool_mutex held or RCU read
  22643. * locked. If the pool needs to be used beyond the locking in effect, the
  22644. * caller is responsible for guaranteeing that the pool stays online.
  22645. *
  22646. @@ -389,7 +398,7 @@
  22647. * @pwq: iteration cursor
  22648. * @wq: the target workqueue
  22649. *
  22650. - * This must be called either with wq->mutex held or sched RCU read locked.
  22651. + * This must be called either with wq->mutex held or RCU read locked.
  22652. * If the pwq needs to be used beyond the locking in effect, the caller is
  22653. * responsible for guaranteeing that the pwq stays online.
  22654. *
  22655. @@ -401,6 +410,31 @@
  22656. if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
  22657. else
  22658. +#ifdef CONFIG_PREEMPT_RT_BASE
  22659. +static inline void rt_lock_idle_list(struct worker_pool *pool)
  22660. +{
  22661. + preempt_disable();
  22662. +}
  22663. +static inline void rt_unlock_idle_list(struct worker_pool *pool)
  22664. +{
  22665. + preempt_enable();
  22666. +}
  22667. +static inline void sched_lock_idle_list(struct worker_pool *pool) { }
  22668. +static inline void sched_unlock_idle_list(struct worker_pool *pool) { }
  22669. +#else
  22670. +static inline void rt_lock_idle_list(struct worker_pool *pool) { }
  22671. +static inline void rt_unlock_idle_list(struct worker_pool *pool) { }
  22672. +static inline void sched_lock_idle_list(struct worker_pool *pool)
  22673. +{
  22674. + spin_lock_irq(&pool->lock);
  22675. +}
  22676. +static inline void sched_unlock_idle_list(struct worker_pool *pool)
  22677. +{
  22678. + spin_unlock_irq(&pool->lock);
  22679. +}
  22680. +#endif
  22681. +
  22682. +
  22683. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  22684. static struct debug_obj_descr work_debug_descr;
  22685. @@ -551,7 +585,7 @@
  22686. * @wq: the target workqueue
  22687. * @node: the node ID
  22688. *
  22689. - * This must be called either with pwq_lock held or sched RCU read locked.
  22690. + * This must be called either with pwq_lock held or RCU read locked.
  22691. * If the pwq needs to be used beyond the locking in effect, the caller is
  22692. * responsible for guaranteeing that the pwq stays online.
  22693. *
  22694. @@ -655,8 +689,8 @@
  22695. * @work: the work item of interest
  22696. *
  22697. * Pools are created and destroyed under wq_pool_mutex, and allows read
  22698. - * access under sched-RCU read lock. As such, this function should be
  22699. - * called under wq_pool_mutex or with preemption disabled.
  22700. + * access under RCU read lock. As such, this function should be
  22701. + * called under wq_pool_mutex or inside of a rcu_read_lock() region.
  22702. *
  22703. * All fields of the returned pool are accessible as long as the above
  22704. * mentioned locking is in effect. If the returned pool needs to be used
  22705. @@ -793,51 +827,44 @@
  22706. */
  22707. static void wake_up_worker(struct worker_pool *pool)
  22708. {
  22709. - struct worker *worker = first_idle_worker(pool);
  22710. + struct worker *worker;
  22711. +
  22712. + rt_lock_idle_list(pool);
  22713. +
  22714. + worker = first_idle_worker(pool);
  22715. if (likely(worker))
  22716. wake_up_process(worker->task);
  22717. +
  22718. + rt_unlock_idle_list(pool);
  22719. }
  22720. /**
  22721. - * wq_worker_waking_up - a worker is waking up
  22722. - * @task: task waking up
  22723. - * @cpu: CPU @task is waking up to
  22724. + * wq_worker_running - a worker is running again
  22725. + * @task: task returning from sleep
  22726. *
  22727. - * This function is called during try_to_wake_up() when a worker is
  22728. - * being awoken.
  22729. - *
  22730. - * CONTEXT:
  22731. - * spin_lock_irq(rq->lock)
  22732. + * This function is called when a worker returns from schedule()
  22733. */
  22734. -void wq_worker_waking_up(struct task_struct *task, int cpu)
  22735. +void wq_worker_running(struct task_struct *task)
  22736. {
  22737. struct worker *worker = kthread_data(task);
  22738. - if (!(worker->flags & WORKER_NOT_RUNNING)) {
  22739. - WARN_ON_ONCE(worker->pool->cpu != cpu);
  22740. + if (!worker->sleeping)
  22741. + return;
  22742. + if (!(worker->flags & WORKER_NOT_RUNNING))
  22743. atomic_inc(&worker->pool->nr_running);
  22744. - }
  22745. + worker->sleeping = 0;
  22746. }
  22747. /**
  22748. * wq_worker_sleeping - a worker is going to sleep
  22749. * @task: task going to sleep
  22750. - * @cpu: CPU in question, must be the current CPU number
  22751. - *
  22752. - * This function is called during schedule() when a busy worker is
  22753. - * going to sleep. Worker on the same cpu can be woken up by
  22754. - * returning pointer to its task.
  22755. - *
  22756. - * CONTEXT:
  22757. - * spin_lock_irq(rq->lock)
  22758. - *
  22759. - * Return:
  22760. - * Worker task on @cpu to wake up, %NULL if none.
  22761. + * This function is called from schedule() when a busy worker is
  22762. + * going to sleep.
  22763. */
  22764. -struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
  22765. +void wq_worker_sleeping(struct task_struct *task)
  22766. {
  22767. - struct worker *worker = kthread_data(task), *to_wakeup = NULL;
  22768. + struct worker *worker = kthread_data(task);
  22769. struct worker_pool *pool;
  22770. /*
  22771. @@ -846,29 +873,26 @@
  22772. * checking NOT_RUNNING.
  22773. */
  22774. if (worker->flags & WORKER_NOT_RUNNING)
  22775. - return NULL;
  22776. + return;
  22777. pool = worker->pool;
  22778. - /* this can only happen on the local cpu */
  22779. - if (WARN_ON_ONCE(cpu != raw_smp_processor_id() || pool->cpu != cpu))
  22780. - return NULL;
  22781. + if (WARN_ON_ONCE(worker->sleeping))
  22782. + return;
  22783. +
  22784. + worker->sleeping = 1;
  22785. /*
  22786. * The counterpart of the following dec_and_test, implied mb,
  22787. * worklist not empty test sequence is in insert_work().
  22788. * Please read comment there.
  22789. - *
  22790. - * NOT_RUNNING is clear. This means that we're bound to and
  22791. - * running on the local cpu w/ rq lock held and preemption
  22792. - * disabled, which in turn means that none else could be
  22793. - * manipulating idle_list, so dereferencing idle_list without pool
  22794. - * lock is safe.
  22795. */
  22796. if (atomic_dec_and_test(&pool->nr_running) &&
  22797. - !list_empty(&pool->worklist))
  22798. - to_wakeup = first_idle_worker(pool);
  22799. - return to_wakeup ? to_wakeup->task : NULL;
  22800. + !list_empty(&pool->worklist)) {
  22801. + sched_lock_idle_list(pool);
  22802. + wake_up_worker(pool);
  22803. + sched_unlock_idle_list(pool);
  22804. + }
  22805. }
  22806. /**
  22807. @@ -1062,12 +1086,12 @@
  22808. {
  22809. if (pwq) {
  22810. /*
  22811. - * As both pwqs and pools are sched-RCU protected, the
  22812. + * As both pwqs and pools are RCU protected, the
  22813. * following lock operations are safe.
  22814. */
  22815. - spin_lock_irq(&pwq->pool->lock);
  22816. + local_spin_lock_irq(pendingb_lock, &pwq->pool->lock);
  22817. put_pwq(pwq);
  22818. - spin_unlock_irq(&pwq->pool->lock);
  22819. + local_spin_unlock_irq(pendingb_lock, &pwq->pool->lock);
  22820. }
  22821. }
  22822. @@ -1169,7 +1193,7 @@
  22823. struct worker_pool *pool;
  22824. struct pool_workqueue *pwq;
  22825. - local_irq_save(*flags);
  22826. + local_lock_irqsave(pendingb_lock, *flags);
  22827. /* try to steal the timer if it exists */
  22828. if (is_dwork) {
  22829. @@ -1188,6 +1212,7 @@
  22830. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
  22831. return 0;
  22832. + rcu_read_lock();
  22833. /*
  22834. * The queueing is in progress, or it is already queued. Try to
  22835. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  22836. @@ -1226,14 +1251,16 @@
  22837. set_work_pool_and_keep_pending(work, pool->id);
  22838. spin_unlock(&pool->lock);
  22839. + rcu_read_unlock();
  22840. return 1;
  22841. }
  22842. spin_unlock(&pool->lock);
  22843. fail:
  22844. - local_irq_restore(*flags);
  22845. + rcu_read_unlock();
  22846. + local_unlock_irqrestore(pendingb_lock, *flags);
  22847. if (work_is_canceling(work))
  22848. return -ENOENT;
  22849. - cpu_relax();
  22850. + cpu_chill();
  22851. return -EAGAIN;
  22852. }
  22853. @@ -1302,7 +1329,7 @@
  22854. * queued or lose PENDING. Grabbing PENDING and queueing should
  22855. * happen with IRQ disabled.
  22856. */
  22857. - WARN_ON_ONCE(!irqs_disabled());
  22858. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  22859. debug_work_activate(work);
  22860. @@ -1310,6 +1337,8 @@
  22861. if (unlikely(wq->flags & __WQ_DRAINING) &&
  22862. WARN_ON_ONCE(!is_chained_work(wq)))
  22863. return;
  22864. +
  22865. + rcu_read_lock();
  22866. retry:
  22867. if (req_cpu == WORK_CPU_UNBOUND)
  22868. cpu = raw_smp_processor_id();
  22869. @@ -1366,10 +1395,8 @@
  22870. /* pwq determined, queue */
  22871. trace_workqueue_queue_work(req_cpu, pwq, work);
  22872. - if (WARN_ON(!list_empty(&work->entry))) {
  22873. - spin_unlock(&pwq->pool->lock);
  22874. - return;
  22875. - }
  22876. + if (WARN_ON(!list_empty(&work->entry)))
  22877. + goto out;
  22878. pwq->nr_in_flight[pwq->work_color]++;
  22879. work_flags = work_color_to_flags(pwq->work_color);
  22880. @@ -1385,7 +1412,9 @@
  22881. insert_work(pwq, work, worklist, work_flags);
  22882. +out:
  22883. spin_unlock(&pwq->pool->lock);
  22884. + rcu_read_unlock();
  22885. }
  22886. /**
  22887. @@ -1405,14 +1434,14 @@
  22888. bool ret = false;
  22889. unsigned long flags;
  22890. - local_irq_save(flags);
  22891. + local_lock_irqsave(pendingb_lock,flags);
  22892. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  22893. __queue_work(cpu, wq, work);
  22894. ret = true;
  22895. }
  22896. - local_irq_restore(flags);
  22897. + local_unlock_irqrestore(pendingb_lock, flags);
  22898. return ret;
  22899. }
  22900. EXPORT_SYMBOL(queue_work_on);
  22901. @@ -1479,14 +1508,14 @@
  22902. unsigned long flags;
  22903. /* read the comment in __queue_work() */
  22904. - local_irq_save(flags);
  22905. + local_lock_irqsave(pendingb_lock, flags);
  22906. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  22907. __queue_delayed_work(cpu, wq, dwork, delay);
  22908. ret = true;
  22909. }
  22910. - local_irq_restore(flags);
  22911. + local_unlock_irqrestore(pendingb_lock, flags);
  22912. return ret;
  22913. }
  22914. EXPORT_SYMBOL(queue_delayed_work_on);
  22915. @@ -1521,7 +1550,7 @@
  22916. if (likely(ret >= 0)) {
  22917. __queue_delayed_work(cpu, wq, dwork, delay);
  22918. - local_irq_restore(flags);
  22919. + local_unlock_irqrestore(pendingb_lock, flags);
  22920. }
  22921. /* -ENOENT from try_to_grab_pending() becomes %true */
  22922. @@ -1554,7 +1583,9 @@
  22923. worker->last_active = jiffies;
  22924. /* idle_list is LIFO */
  22925. + rt_lock_idle_list(pool);
  22926. list_add(&worker->entry, &pool->idle_list);
  22927. + rt_unlock_idle_list(pool);
  22928. if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
  22929. mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
  22930. @@ -1587,7 +1618,9 @@
  22931. return;
  22932. worker_clr_flags(worker, WORKER_IDLE);
  22933. pool->nr_idle--;
  22934. + rt_lock_idle_list(pool);
  22935. list_del_init(&worker->entry);
  22936. + rt_unlock_idle_list(pool);
  22937. }
  22938. static struct worker *alloc_worker(int node)
  22939. @@ -1755,7 +1788,9 @@
  22940. pool->nr_workers--;
  22941. pool->nr_idle--;
  22942. + rt_lock_idle_list(pool);
  22943. list_del_init(&worker->entry);
  22944. + rt_unlock_idle_list(pool);
  22945. worker->flags |= WORKER_DIE;
  22946. wake_up_process(worker->task);
  22947. }
  22948. @@ -2672,14 +2707,14 @@
  22949. might_sleep();
  22950. - local_irq_disable();
  22951. + rcu_read_lock();
  22952. pool = get_work_pool(work);
  22953. if (!pool) {
  22954. - local_irq_enable();
  22955. + rcu_read_unlock();
  22956. return false;
  22957. }
  22958. - spin_lock(&pool->lock);
  22959. + spin_lock_irq(&pool->lock);
  22960. /* see the comment in try_to_grab_pending() with the same code */
  22961. pwq = get_work_pwq(work);
  22962. if (pwq) {
  22963. @@ -2706,10 +2741,11 @@
  22964. else
  22965. lock_map_acquire_read(&pwq->wq->lockdep_map);
  22966. lock_map_release(&pwq->wq->lockdep_map);
  22967. -
  22968. + rcu_read_unlock();
  22969. return true;
  22970. already_gone:
  22971. spin_unlock_irq(&pool->lock);
  22972. + rcu_read_unlock();
  22973. return false;
  22974. }
  22975. @@ -2796,7 +2832,7 @@
  22976. /* tell other tasks trying to grab @work to back off */
  22977. mark_work_canceling(work);
  22978. - local_irq_restore(flags);
  22979. + local_unlock_irqrestore(pendingb_lock, flags);
  22980. flush_work(work);
  22981. clear_work_data(work);
  22982. @@ -2851,10 +2887,10 @@
  22983. */
  22984. bool flush_delayed_work(struct delayed_work *dwork)
  22985. {
  22986. - local_irq_disable();
  22987. + local_lock_irq(pendingb_lock);
  22988. if (del_timer_sync(&dwork->timer))
  22989. __queue_work(dwork->cpu, dwork->wq, &dwork->work);
  22990. - local_irq_enable();
  22991. + local_unlock_irq(pendingb_lock);
  22992. return flush_work(&dwork->work);
  22993. }
  22994. EXPORT_SYMBOL(flush_delayed_work);
  22995. @@ -2889,7 +2925,7 @@
  22996. set_work_pool_and_clear_pending(&dwork->work,
  22997. get_work_pool_id(&dwork->work));
  22998. - local_irq_restore(flags);
  22999. + local_unlock_irqrestore(pendingb_lock, flags);
  23000. return ret;
  23001. }
  23002. EXPORT_SYMBOL(cancel_delayed_work);
  23003. @@ -3147,7 +3183,7 @@
  23004. * put_unbound_pool - put a worker_pool
  23005. * @pool: worker_pool to put
  23006. *
  23007. - * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
  23008. + * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU
  23009. * safe manner. get_unbound_pool() calls this function on its failure path
  23010. * and this function should be able to release pools which went through,
  23011. * successfully or not, init_worker_pool().
  23012. @@ -3201,8 +3237,8 @@
  23013. del_timer_sync(&pool->idle_timer);
  23014. del_timer_sync(&pool->mayday_timer);
  23015. - /* sched-RCU protected to allow dereferences from get_work_pool() */
  23016. - call_rcu_sched(&pool->rcu, rcu_free_pool);
  23017. + /* RCU protected to allow dereferences from get_work_pool() */
  23018. + call_rcu(&pool->rcu, rcu_free_pool);
  23019. }
  23020. /**
  23021. @@ -3307,14 +3343,14 @@
  23022. put_unbound_pool(pool);
  23023. mutex_unlock(&wq_pool_mutex);
  23024. - call_rcu_sched(&pwq->rcu, rcu_free_pwq);
  23025. + call_rcu(&pwq->rcu, rcu_free_pwq);
  23026. /*
  23027. * If we're the last pwq going away, @wq is already dead and no one
  23028. * is gonna access it anymore. Schedule RCU free.
  23029. */
  23030. if (is_last)
  23031. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  23032. + call_rcu(&wq->rcu, rcu_free_wq);
  23033. }
  23034. /**
  23035. @@ -3920,7 +3956,7 @@
  23036. * The base ref is never dropped on per-cpu pwqs. Directly
  23037. * schedule RCU free.
  23038. */
  23039. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  23040. + call_rcu(&wq->rcu, rcu_free_wq);
  23041. } else {
  23042. /*
  23043. * We're the sole accessor of @wq at this point. Directly
  23044. @@ -4013,7 +4049,8 @@
  23045. struct pool_workqueue *pwq;
  23046. bool ret;
  23047. - rcu_read_lock_sched();
  23048. + rcu_read_lock();
  23049. + preempt_disable();
  23050. if (cpu == WORK_CPU_UNBOUND)
  23051. cpu = smp_processor_id();
  23052. @@ -4024,7 +4061,8 @@
  23053. pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
  23054. ret = !list_empty(&pwq->delayed_works);
  23055. - rcu_read_unlock_sched();
  23056. + preempt_enable();
  23057. + rcu_read_unlock();
  23058. return ret;
  23059. }
  23060. @@ -4050,15 +4088,15 @@
  23061. if (work_pending(work))
  23062. ret |= WORK_BUSY_PENDING;
  23063. - local_irq_save(flags);
  23064. + rcu_read_lock();
  23065. pool = get_work_pool(work);
  23066. if (pool) {
  23067. - spin_lock(&pool->lock);
  23068. + spin_lock_irqsave(&pool->lock, flags);
  23069. if (find_worker_executing_work(pool, work))
  23070. ret |= WORK_BUSY_RUNNING;
  23071. - spin_unlock(&pool->lock);
  23072. + spin_unlock_irqrestore(&pool->lock, flags);
  23073. }
  23074. - local_irq_restore(flags);
  23075. + rcu_read_unlock();
  23076. return ret;
  23077. }
  23078. @@ -4247,7 +4285,7 @@
  23079. unsigned long flags;
  23080. int pi;
  23081. - rcu_read_lock_sched();
  23082. + rcu_read_lock();
  23083. pr_info("Showing busy workqueues and worker pools:\n");
  23084. @@ -4298,7 +4336,7 @@
  23085. spin_unlock_irqrestore(&pool->lock, flags);
  23086. }
  23087. - rcu_read_unlock_sched();
  23088. + rcu_read_unlock();
  23089. }
  23090. /*
  23091. @@ -4648,16 +4686,16 @@
  23092. * nr_active is monotonically decreasing. It's safe
  23093. * to peek without lock.
  23094. */
  23095. - rcu_read_lock_sched();
  23096. + rcu_read_lock();
  23097. for_each_pwq(pwq, wq) {
  23098. WARN_ON_ONCE(pwq->nr_active < 0);
  23099. if (pwq->nr_active) {
  23100. busy = true;
  23101. - rcu_read_unlock_sched();
  23102. + rcu_read_unlock();
  23103. goto out_unlock;
  23104. }
  23105. }
  23106. - rcu_read_unlock_sched();
  23107. + rcu_read_unlock();
  23108. }
  23109. out_unlock:
  23110. mutex_unlock(&wq_pool_mutex);
  23111. @@ -4771,7 +4809,8 @@
  23112. const char *delim = "";
  23113. int node, written = 0;
  23114. - rcu_read_lock_sched();
  23115. + get_online_cpus();
  23116. + rcu_read_lock();
  23117. for_each_node(node) {
  23118. written += scnprintf(buf + written, PAGE_SIZE - written,
  23119. "%s%d:%d", delim, node,
  23120. @@ -4779,7 +4818,8 @@
  23121. delim = " ";
  23122. }
  23123. written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
  23124. - rcu_read_unlock_sched();
  23125. + rcu_read_unlock();
  23126. + put_online_cpus();
  23127. return written;
  23128. }
  23129. diff -Nur linux-4.1.13.orig/kernel/workqueue_internal.h linux-4.1.13/kernel/workqueue_internal.h
  23130. --- linux-4.1.13.orig/kernel/workqueue_internal.h 2015-11-09 23:34:10.000000000 +0100
  23131. +++ linux-4.1.13/kernel/workqueue_internal.h 2015-11-29 09:23:09.657610995 +0100
  23132. @@ -43,6 +43,7 @@
  23133. unsigned long last_active; /* L: last active timestamp */
  23134. unsigned int flags; /* X: flags */
  23135. int id; /* I: worker id */
  23136. + int sleeping; /* None */
  23137. /*
  23138. * Opaque string set with work_set_desc(). Printed out with task
  23139. @@ -68,7 +69,7 @@
  23140. * Scheduler hooks for concurrency managed workqueue. Only to be used from
  23141. * sched/core.c and workqueue.c.
  23142. */
  23143. -void wq_worker_waking_up(struct task_struct *task, int cpu);
  23144. -struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu);
  23145. +void wq_worker_running(struct task_struct *task);
  23146. +void wq_worker_sleeping(struct task_struct *task);
  23147. #endif /* _KERNEL_WORKQUEUE_INTERNAL_H */
  23148. diff -Nur linux-4.1.13.orig/lib/debugobjects.c linux-4.1.13/lib/debugobjects.c
  23149. --- linux-4.1.13.orig/lib/debugobjects.c 2015-11-09 23:34:10.000000000 +0100
  23150. +++ linux-4.1.13/lib/debugobjects.c 2015-11-29 09:23:09.657610995 +0100
  23151. @@ -309,7 +309,10 @@
  23152. struct debug_obj *obj;
  23153. unsigned long flags;
  23154. - fill_pool();
  23155. +#ifdef CONFIG_PREEMPT_RT_FULL
  23156. + if (preempt_count() == 0 && !irqs_disabled())
  23157. +#endif
  23158. + fill_pool();
  23159. db = get_bucket((unsigned long) addr);
  23160. diff -Nur linux-4.1.13.orig/lib/dump_stack.c linux-4.1.13/lib/dump_stack.c
  23161. --- linux-4.1.13.orig/lib/dump_stack.c 2015-11-09 23:34:10.000000000 +0100
  23162. +++ linux-4.1.13/lib/dump_stack.c 2015-11-29 09:23:09.657610995 +0100
  23163. @@ -33,7 +33,7 @@
  23164. * Permit this cpu to perform nested stack dumps while serialising
  23165. * against other CPUs
  23166. */
  23167. - preempt_disable();
  23168. + migrate_disable();
  23169. retry:
  23170. cpu = smp_processor_id();
  23171. @@ -52,7 +52,7 @@
  23172. if (!was_locked)
  23173. atomic_set(&dump_lock, -1);
  23174. - preempt_enable();
  23175. + migrate_enable();
  23176. }
  23177. #else
  23178. asmlinkage __visible void dump_stack(void)
  23179. diff -Nur linux-4.1.13.orig/lib/idr.c linux-4.1.13/lib/idr.c
  23180. --- linux-4.1.13.orig/lib/idr.c 2015-11-09 23:34:10.000000000 +0100
  23181. +++ linux-4.1.13/lib/idr.c 2015-11-29 09:23:09.657610995 +0100
  23182. @@ -30,6 +30,7 @@
  23183. #include <linux/idr.h>
  23184. #include <linux/spinlock.h>
  23185. #include <linux/percpu.h>
  23186. +#include <linux/locallock.h>
  23187. #define MAX_IDR_SHIFT (sizeof(int) * 8 - 1)
  23188. #define MAX_IDR_BIT (1U << MAX_IDR_SHIFT)
  23189. @@ -366,6 +367,35 @@
  23190. idr_mark_full(pa, id);
  23191. }
  23192. +#ifdef CONFIG_PREEMPT_RT_FULL
  23193. +static DEFINE_LOCAL_IRQ_LOCK(idr_lock);
  23194. +
  23195. +static inline void idr_preload_lock(void)
  23196. +{
  23197. + local_lock(idr_lock);
  23198. +}
  23199. +
  23200. +static inline void idr_preload_unlock(void)
  23201. +{
  23202. + local_unlock(idr_lock);
  23203. +}
  23204. +
  23205. +void idr_preload_end(void)
  23206. +{
  23207. + idr_preload_unlock();
  23208. +}
  23209. +EXPORT_SYMBOL(idr_preload_end);
  23210. +#else
  23211. +static inline void idr_preload_lock(void)
  23212. +{
  23213. + preempt_disable();
  23214. +}
  23215. +
  23216. +static inline void idr_preload_unlock(void)
  23217. +{
  23218. + preempt_enable();
  23219. +}
  23220. +#endif
  23221. /**
  23222. * idr_preload - preload for idr_alloc()
  23223. @@ -401,7 +431,7 @@
  23224. WARN_ON_ONCE(in_interrupt());
  23225. might_sleep_if(gfp_mask & __GFP_WAIT);
  23226. - preempt_disable();
  23227. + idr_preload_lock();
  23228. /*
  23229. * idr_alloc() is likely to succeed w/o full idr_layer buffer and
  23230. @@ -413,9 +443,9 @@
  23231. while (__this_cpu_read(idr_preload_cnt) < MAX_IDR_FREE) {
  23232. struct idr_layer *new;
  23233. - preempt_enable();
  23234. + idr_preload_unlock();
  23235. new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
  23236. - preempt_disable();
  23237. + idr_preload_lock();
  23238. if (!new)
  23239. break;
  23240. diff -Nur linux-4.1.13.orig/lib/Kconfig linux-4.1.13/lib/Kconfig
  23241. --- linux-4.1.13.orig/lib/Kconfig 2015-11-09 23:34:10.000000000 +0100
  23242. +++ linux-4.1.13/lib/Kconfig 2015-11-29 09:23:09.657610995 +0100
  23243. @@ -391,6 +391,7 @@
  23244. config CPUMASK_OFFSTACK
  23245. bool "Force CPU masks off stack" if DEBUG_PER_CPU_MAPS
  23246. + depends on !PREEMPT_RT_FULL
  23247. help
  23248. Use dynamic allocation for cpumask_var_t, instead of putting
  23249. them on the stack. This is a bit more expensive, but avoids
  23250. diff -Nur linux-4.1.13.orig/lib/locking-selftest.c linux-4.1.13/lib/locking-selftest.c
  23251. --- linux-4.1.13.orig/lib/locking-selftest.c 2015-11-09 23:34:10.000000000 +0100
  23252. +++ linux-4.1.13/lib/locking-selftest.c 2015-11-29 09:23:09.657610995 +0100
  23253. @@ -590,6 +590,8 @@
  23254. #include "locking-selftest-spin-hardirq.h"
  23255. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
  23256. +#ifndef CONFIG_PREEMPT_RT_FULL
  23257. +
  23258. #include "locking-selftest-rlock-hardirq.h"
  23259. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
  23260. @@ -605,9 +607,12 @@
  23261. #include "locking-selftest-wlock-softirq.h"
  23262. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
  23263. +#endif
  23264. +
  23265. #undef E1
  23266. #undef E2
  23267. +#ifndef CONFIG_PREEMPT_RT_FULL
  23268. /*
  23269. * Enabling hardirqs with a softirq-safe lock held:
  23270. */
  23271. @@ -640,6 +645,8 @@
  23272. #undef E1
  23273. #undef E2
  23274. +#endif
  23275. +
  23276. /*
  23277. * Enabling irqs with an irq-safe lock held:
  23278. */
  23279. @@ -663,6 +670,8 @@
  23280. #include "locking-selftest-spin-hardirq.h"
  23281. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
  23282. +#ifndef CONFIG_PREEMPT_RT_FULL
  23283. +
  23284. #include "locking-selftest-rlock-hardirq.h"
  23285. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
  23286. @@ -678,6 +687,8 @@
  23287. #include "locking-selftest-wlock-softirq.h"
  23288. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
  23289. +#endif
  23290. +
  23291. #undef E1
  23292. #undef E2
  23293. @@ -709,6 +720,8 @@
  23294. #include "locking-selftest-spin-hardirq.h"
  23295. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
  23296. +#ifndef CONFIG_PREEMPT_RT_FULL
  23297. +
  23298. #include "locking-selftest-rlock-hardirq.h"
  23299. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
  23300. @@ -724,6 +737,8 @@
  23301. #include "locking-selftest-wlock-softirq.h"
  23302. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
  23303. +#endif
  23304. +
  23305. #undef E1
  23306. #undef E2
  23307. #undef E3
  23308. @@ -757,6 +772,8 @@
  23309. #include "locking-selftest-spin-hardirq.h"
  23310. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
  23311. +#ifndef CONFIG_PREEMPT_RT_FULL
  23312. +
  23313. #include "locking-selftest-rlock-hardirq.h"
  23314. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
  23315. @@ -772,10 +789,14 @@
  23316. #include "locking-selftest-wlock-softirq.h"
  23317. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
  23318. +#endif
  23319. +
  23320. #undef E1
  23321. #undef E2
  23322. #undef E3
  23323. +#ifndef CONFIG_PREEMPT_RT_FULL
  23324. +
  23325. /*
  23326. * read-lock / write-lock irq inversion.
  23327. *
  23328. @@ -838,6 +859,10 @@
  23329. #undef E2
  23330. #undef E3
  23331. +#endif
  23332. +
  23333. +#ifndef CONFIG_PREEMPT_RT_FULL
  23334. +
  23335. /*
  23336. * read-lock / write-lock recursion that is actually safe.
  23337. */
  23338. @@ -876,6 +901,8 @@
  23339. #undef E2
  23340. #undef E3
  23341. +#endif
  23342. +
  23343. /*
  23344. * read-lock / write-lock recursion that is unsafe.
  23345. */
  23346. @@ -1858,6 +1885,7 @@
  23347. printk(" --------------------------------------------------------------------------\n");
  23348. +#ifndef CONFIG_PREEMPT_RT_FULL
  23349. /*
  23350. * irq-context testcases:
  23351. */
  23352. @@ -1870,6 +1898,28 @@
  23353. DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
  23354. // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
  23355. +#else
  23356. + /* On -rt, we only do hardirq context test for raw spinlock */
  23357. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 12);
  23358. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 21);
  23359. +
  23360. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 12);
  23361. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 21);
  23362. +
  23363. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 123);
  23364. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 132);
  23365. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 213);
  23366. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 231);
  23367. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 312);
  23368. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 321);
  23369. +
  23370. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 123);
  23371. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 132);
  23372. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 213);
  23373. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 231);
  23374. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 312);
  23375. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 321);
  23376. +#endif
  23377. ww_tests();
  23378. diff -Nur linux-4.1.13.orig/lib/percpu_ida.c linux-4.1.13/lib/percpu_ida.c
  23379. --- linux-4.1.13.orig/lib/percpu_ida.c 2015-11-09 23:34:10.000000000 +0100
  23380. +++ linux-4.1.13/lib/percpu_ida.c 2015-11-29 09:23:09.657610995 +0100
  23381. @@ -26,6 +26,9 @@
  23382. #include <linux/string.h>
  23383. #include <linux/spinlock.h>
  23384. #include <linux/percpu_ida.h>
  23385. +#include <linux/locallock.h>
  23386. +
  23387. +static DEFINE_LOCAL_IRQ_LOCK(irq_off_lock);
  23388. struct percpu_ida_cpu {
  23389. /*
  23390. @@ -148,13 +151,13 @@
  23391. unsigned long flags;
  23392. int tag;
  23393. - local_irq_save(flags);
  23394. + local_lock_irqsave(irq_off_lock, flags);
  23395. tags = this_cpu_ptr(pool->tag_cpu);
  23396. /* Fastpath */
  23397. tag = alloc_local_tag(tags);
  23398. if (likely(tag >= 0)) {
  23399. - local_irq_restore(flags);
  23400. + local_unlock_irqrestore(irq_off_lock, flags);
  23401. return tag;
  23402. }
  23403. @@ -173,6 +176,7 @@
  23404. if (!tags->nr_free)
  23405. alloc_global_tags(pool, tags);
  23406. +
  23407. if (!tags->nr_free)
  23408. steal_tags(pool, tags);
  23409. @@ -184,7 +188,7 @@
  23410. }
  23411. spin_unlock(&pool->lock);
  23412. - local_irq_restore(flags);
  23413. + local_unlock_irqrestore(irq_off_lock, flags);
  23414. if (tag >= 0 || state == TASK_RUNNING)
  23415. break;
  23416. @@ -196,7 +200,7 @@
  23417. schedule();
  23418. - local_irq_save(flags);
  23419. + local_lock_irqsave(irq_off_lock, flags);
  23420. tags = this_cpu_ptr(pool->tag_cpu);
  23421. }
  23422. if (state != TASK_RUNNING)
  23423. @@ -221,7 +225,7 @@
  23424. BUG_ON(tag >= pool->nr_tags);
  23425. - local_irq_save(flags);
  23426. + local_lock_irqsave(irq_off_lock, flags);
  23427. tags = this_cpu_ptr(pool->tag_cpu);
  23428. spin_lock(&tags->lock);
  23429. @@ -253,7 +257,7 @@
  23430. spin_unlock(&pool->lock);
  23431. }
  23432. - local_irq_restore(flags);
  23433. + local_unlock_irqrestore(irq_off_lock, flags);
  23434. }
  23435. EXPORT_SYMBOL_GPL(percpu_ida_free);
  23436. @@ -345,7 +349,7 @@
  23437. struct percpu_ida_cpu *remote;
  23438. unsigned cpu, i, err = 0;
  23439. - local_irq_save(flags);
  23440. + local_lock_irqsave(irq_off_lock, flags);
  23441. for_each_possible_cpu(cpu) {
  23442. remote = per_cpu_ptr(pool->tag_cpu, cpu);
  23443. spin_lock(&remote->lock);
  23444. @@ -367,7 +371,7 @@
  23445. }
  23446. spin_unlock(&pool->lock);
  23447. out:
  23448. - local_irq_restore(flags);
  23449. + local_unlock_irqrestore(irq_off_lock, flags);
  23450. return err;
  23451. }
  23452. EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
  23453. diff -Nur linux-4.1.13.orig/lib/radix-tree.c linux-4.1.13/lib/radix-tree.c
  23454. --- linux-4.1.13.orig/lib/radix-tree.c 2015-11-09 23:34:10.000000000 +0100
  23455. +++ linux-4.1.13/lib/radix-tree.c 2015-11-29 09:23:09.657610995 +0100
  23456. @@ -195,12 +195,13 @@
  23457. * succeed in getting a node here (and never reach
  23458. * kmem_cache_alloc)
  23459. */
  23460. - rtp = this_cpu_ptr(&radix_tree_preloads);
  23461. + rtp = &get_cpu_var(radix_tree_preloads);
  23462. if (rtp->nr) {
  23463. ret = rtp->nodes[rtp->nr - 1];
  23464. rtp->nodes[rtp->nr - 1] = NULL;
  23465. rtp->nr--;
  23466. }
  23467. + put_cpu_var(radix_tree_preloads);
  23468. /*
  23469. * Update the allocation stack trace as this is more useful
  23470. * for debugging.
  23471. @@ -240,6 +241,7 @@
  23472. call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
  23473. }
  23474. +#ifndef CONFIG_PREEMPT_RT_FULL
  23475. /*
  23476. * Load up this CPU's radix_tree_node buffer with sufficient objects to
  23477. * ensure that the addition of a single element in the tree cannot fail. On
  23478. @@ -305,6 +307,7 @@
  23479. return 0;
  23480. }
  23481. EXPORT_SYMBOL(radix_tree_maybe_preload);
  23482. +#endif
  23483. /*
  23484. * Return the maximum key which can be store into a
  23485. diff -Nur linux-4.1.13.orig/lib/scatterlist.c linux-4.1.13/lib/scatterlist.c
  23486. --- linux-4.1.13.orig/lib/scatterlist.c 2015-11-09 23:34:10.000000000 +0100
  23487. +++ linux-4.1.13/lib/scatterlist.c 2015-11-29 09:23:09.657610995 +0100
  23488. @@ -592,7 +592,7 @@
  23489. flush_kernel_dcache_page(miter->page);
  23490. if (miter->__flags & SG_MITER_ATOMIC) {
  23491. - WARN_ON_ONCE(preemptible());
  23492. + WARN_ON_ONCE(!pagefault_disabled());
  23493. kunmap_atomic(miter->addr);
  23494. } else
  23495. kunmap(miter->page);
  23496. @@ -637,7 +637,7 @@
  23497. if (!sg_miter_skip(&miter, skip))
  23498. return false;
  23499. - local_irq_save(flags);
  23500. + local_irq_save_nort(flags);
  23501. while (sg_miter_next(&miter) && offset < buflen) {
  23502. unsigned int len;
  23503. @@ -654,7 +654,7 @@
  23504. sg_miter_stop(&miter);
  23505. - local_irq_restore(flags);
  23506. + local_irq_restore_nort(flags);
  23507. return offset;
  23508. }
  23509. diff -Nur linux-4.1.13.orig/lib/smp_processor_id.c linux-4.1.13/lib/smp_processor_id.c
  23510. --- linux-4.1.13.orig/lib/smp_processor_id.c 2015-11-09 23:34:10.000000000 +0100
  23511. +++ linux-4.1.13/lib/smp_processor_id.c 2015-11-29 09:23:09.657610995 +0100
  23512. @@ -39,8 +39,9 @@
  23513. if (!printk_ratelimit())
  23514. goto out_enable;
  23515. - printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
  23516. - what1, what2, preempt_count() - 1, current->comm, current->pid);
  23517. + printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x %08x] code: %s/%d\n",
  23518. + what1, what2, preempt_count() - 1, __migrate_disabled(current),
  23519. + current->comm, current->pid);
  23520. print_symbol("caller is %s\n", (long)__builtin_return_address(0));
  23521. dump_stack();
  23522. diff -Nur linux-4.1.13.orig/lib/strnlen_user.c linux-4.1.13/lib/strnlen_user.c
  23523. --- linux-4.1.13.orig/lib/strnlen_user.c 2015-11-09 23:34:10.000000000 +0100
  23524. +++ linux-4.1.13/lib/strnlen_user.c 2015-11-29 09:23:09.657610995 +0100
  23525. @@ -85,7 +85,8 @@
  23526. * @str: The string to measure.
  23527. * @count: Maximum count (including NUL character)
  23528. *
  23529. - * Context: User context only. This function may sleep.
  23530. + * Context: User context only. This function may sleep if pagefaults are
  23531. + * enabled.
  23532. *
  23533. * Get the size of a NUL-terminated string in user space.
  23534. *
  23535. @@ -121,7 +122,8 @@
  23536. * strlen_user: - Get the size of a user string INCLUDING final NUL.
  23537. * @str: The string to measure.
  23538. *
  23539. - * Context: User context only. This function may sleep.
  23540. + * Context: User context only. This function may sleep if pagefaults are
  23541. + * enabled.
  23542. *
  23543. * Get the size of a NUL-terminated string in user space.
  23544. *
  23545. diff -Nur linux-4.1.13.orig/mm/compaction.c linux-4.1.13/mm/compaction.c
  23546. --- linux-4.1.13.orig/mm/compaction.c 2015-11-09 23:34:10.000000000 +0100
  23547. +++ linux-4.1.13/mm/compaction.c 2015-11-29 09:23:09.661610728 +0100
  23548. @@ -1406,10 +1406,12 @@
  23549. cc->migrate_pfn & ~((1UL << cc->order) - 1);
  23550. if (last_migrated_pfn < current_block_start) {
  23551. - cpu = get_cpu();
  23552. + cpu = get_cpu_light();
  23553. + local_lock_irq(swapvec_lock);
  23554. lru_add_drain_cpu(cpu);
  23555. + local_unlock_irq(swapvec_lock);
  23556. drain_local_pages(zone);
  23557. - put_cpu();
  23558. + put_cpu_light();
  23559. /* No more flushing until we migrate again */
  23560. last_migrated_pfn = 0;
  23561. }
  23562. diff -Nur linux-4.1.13.orig/mm/filemap.c linux-4.1.13/mm/filemap.c
  23563. --- linux-4.1.13.orig/mm/filemap.c 2015-11-09 23:34:10.000000000 +0100
  23564. +++ linux-4.1.13/mm/filemap.c 2015-11-29 09:23:09.661610728 +0100
  23565. @@ -167,7 +167,9 @@
  23566. if (!workingset_node_pages(node) &&
  23567. list_empty(&node->private_list)) {
  23568. node->private_data = mapping;
  23569. - list_lru_add(&workingset_shadow_nodes, &node->private_list);
  23570. + local_lock(workingset_shadow_lock);
  23571. + list_lru_add(&__workingset_shadow_nodes, &node->private_list);
  23572. + local_unlock(workingset_shadow_lock);
  23573. }
  23574. }
  23575. @@ -533,9 +535,12 @@
  23576. * node->private_list is protected by
  23577. * mapping->tree_lock.
  23578. */
  23579. - if (!list_empty(&node->private_list))
  23580. - list_lru_del(&workingset_shadow_nodes,
  23581. + if (!list_empty(&node->private_list)) {
  23582. + local_lock(workingset_shadow_lock);
  23583. + list_lru_del(&__workingset_shadow_nodes,
  23584. &node->private_list);
  23585. + local_unlock(workingset_shadow_lock);
  23586. + }
  23587. }
  23588. return 0;
  23589. }
  23590. diff -Nur linux-4.1.13.orig/mm/highmem.c linux-4.1.13/mm/highmem.c
  23591. --- linux-4.1.13.orig/mm/highmem.c 2015-11-09 23:34:10.000000000 +0100
  23592. +++ linux-4.1.13/mm/highmem.c 2015-11-29 09:23:09.661610728 +0100
  23593. @@ -29,10 +29,11 @@
  23594. #include <linux/kgdb.h>
  23595. #include <asm/tlbflush.h>
  23596. -
  23597. +#ifndef CONFIG_PREEMPT_RT_FULL
  23598. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  23599. DEFINE_PER_CPU(int, __kmap_atomic_idx);
  23600. #endif
  23601. +#endif
  23602. /*
  23603. * Virtual_count is not a pure "count".
  23604. @@ -107,8 +108,9 @@
  23605. unsigned long totalhigh_pages __read_mostly;
  23606. EXPORT_SYMBOL(totalhigh_pages);
  23607. -
  23608. +#ifndef CONFIG_PREEMPT_RT_FULL
  23609. EXPORT_PER_CPU_SYMBOL(__kmap_atomic_idx);
  23610. +#endif
  23611. unsigned int nr_free_highpages (void)
  23612. {
  23613. diff -Nur linux-4.1.13.orig/mm/Kconfig linux-4.1.13/mm/Kconfig
  23614. --- linux-4.1.13.orig/mm/Kconfig 2015-11-09 23:34:10.000000000 +0100
  23615. +++ linux-4.1.13/mm/Kconfig 2015-11-29 09:23:09.661610728 +0100
  23616. @@ -409,7 +409,7 @@
  23617. config TRANSPARENT_HUGEPAGE
  23618. bool "Transparent Hugepage Support"
  23619. - depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE
  23620. + depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE && !PREEMPT_RT_FULL
  23621. select COMPACTION
  23622. help
  23623. Transparent Hugepages allows the kernel to use huge pages and
  23624. diff -Nur linux-4.1.13.orig/mm/memcontrol.c linux-4.1.13/mm/memcontrol.c
  23625. --- linux-4.1.13.orig/mm/memcontrol.c 2015-11-09 23:34:10.000000000 +0100
  23626. +++ linux-4.1.13/mm/memcontrol.c 2015-11-29 09:23:09.661610728 +0100
  23627. @@ -66,6 +66,8 @@
  23628. #include <net/sock.h>
  23629. #include <net/ip.h>
  23630. #include <net/tcp_memcontrol.h>
  23631. +#include <linux/locallock.h>
  23632. +
  23633. #include "slab.h"
  23634. #include <asm/uaccess.h>
  23635. @@ -85,6 +87,7 @@
  23636. #define do_swap_account 0
  23637. #endif
  23638. +static DEFINE_LOCAL_IRQ_LOCK(event_lock);
  23639. static const char * const mem_cgroup_stat_names[] = {
  23640. "cache",
  23641. "rss",
  23642. @@ -2124,14 +2127,17 @@
  23643. */
  23644. static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
  23645. {
  23646. - struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
  23647. + struct memcg_stock_pcp *stock;
  23648. + int cpu = get_cpu_light();
  23649. +
  23650. + stock = &per_cpu(memcg_stock, cpu);
  23651. if (stock->cached != memcg) { /* reset if necessary */
  23652. drain_stock(stock);
  23653. stock->cached = memcg;
  23654. }
  23655. stock->nr_pages += nr_pages;
  23656. - put_cpu_var(memcg_stock);
  23657. + put_cpu_light();
  23658. }
  23659. /*
  23660. @@ -2147,7 +2153,7 @@
  23661. return;
  23662. /* Notify other cpus that system-wide "drain" is running */
  23663. get_online_cpus();
  23664. - curcpu = get_cpu();
  23665. + curcpu = get_cpu_light();
  23666. for_each_online_cpu(cpu) {
  23667. struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
  23668. struct mem_cgroup *memcg;
  23669. @@ -2164,7 +2170,7 @@
  23670. schedule_work_on(cpu, &stock->work);
  23671. }
  23672. }
  23673. - put_cpu();
  23674. + put_cpu_light();
  23675. put_online_cpus();
  23676. mutex_unlock(&percpu_charge_mutex);
  23677. }
  23678. @@ -4802,12 +4808,12 @@
  23679. ret = 0;
  23680. - local_irq_disable();
  23681. + local_lock_irq(event_lock);
  23682. mem_cgroup_charge_statistics(to, page, nr_pages);
  23683. memcg_check_events(to, page);
  23684. mem_cgroup_charge_statistics(from, page, -nr_pages);
  23685. memcg_check_events(from, page);
  23686. - local_irq_enable();
  23687. + local_unlock_irq(event_lock);
  23688. out_unlock:
  23689. unlock_page(page);
  23690. out:
  23691. @@ -5544,10 +5550,10 @@
  23692. VM_BUG_ON_PAGE(!PageTransHuge(page), page);
  23693. }
  23694. - local_irq_disable();
  23695. + local_lock_irq(event_lock);
  23696. mem_cgroup_charge_statistics(memcg, page, nr_pages);
  23697. memcg_check_events(memcg, page);
  23698. - local_irq_enable();
  23699. + local_unlock_irq(event_lock);
  23700. if (do_swap_account && PageSwapCache(page)) {
  23701. swp_entry_t entry = { .val = page_private(page) };
  23702. @@ -5603,14 +5609,14 @@
  23703. memcg_oom_recover(memcg);
  23704. }
  23705. - local_irq_save(flags);
  23706. + local_lock_irqsave(event_lock, flags);
  23707. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS], nr_anon);
  23708. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_CACHE], nr_file);
  23709. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], nr_huge);
  23710. __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT], pgpgout);
  23711. __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
  23712. memcg_check_events(memcg, dummy_page);
  23713. - local_irq_restore(flags);
  23714. + local_unlock_irqrestore(event_lock, flags);
  23715. if (!mem_cgroup_is_root(memcg))
  23716. css_put_many(&memcg->css, nr_pages);
  23717. @@ -5814,6 +5820,7 @@
  23718. {
  23719. struct mem_cgroup *memcg;
  23720. unsigned short oldid;
  23721. + unsigned long flags;
  23722. VM_BUG_ON_PAGE(PageLRU(page), page);
  23723. VM_BUG_ON_PAGE(page_count(page), page);
  23724. @@ -5836,9 +5843,11 @@
  23725. if (!mem_cgroup_is_root(memcg))
  23726. page_counter_uncharge(&memcg->memory, 1);
  23727. + local_lock_irqsave(event_lock, flags);
  23728. /* Caller disabled preemption with mapping->tree_lock */
  23729. mem_cgroup_charge_statistics(memcg, page, -1);
  23730. memcg_check_events(memcg, page);
  23731. + local_unlock_irqrestore(event_lock, flags);
  23732. }
  23733. /**
  23734. diff -Nur linux-4.1.13.orig/mm/memory.c linux-4.1.13/mm/memory.c
  23735. --- linux-4.1.13.orig/mm/memory.c 2015-11-09 23:34:10.000000000 +0100
  23736. +++ linux-4.1.13/mm/memory.c 2015-11-29 09:23:09.661610728 +0100
  23737. @@ -3743,7 +3743,7 @@
  23738. }
  23739. #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
  23740. -void might_fault(void)
  23741. +void __might_fault(const char *file, int line)
  23742. {
  23743. /*
  23744. * Some code (nfs/sunrpc) uses socket ops on kernel memory while
  23745. @@ -3753,21 +3753,15 @@
  23746. */
  23747. if (segment_eq(get_fs(), KERNEL_DS))
  23748. return;
  23749. -
  23750. - /*
  23751. - * it would be nicer only to annotate paths which are not under
  23752. - * pagefault_disable, however that requires a larger audit and
  23753. - * providing helpers like get_user_atomic.
  23754. - */
  23755. - if (in_atomic())
  23756. + if (pagefault_disabled())
  23757. return;
  23758. -
  23759. - __might_sleep(__FILE__, __LINE__, 0);
  23760. -
  23761. + __might_sleep(file, line, 0);
  23762. +#if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
  23763. if (current->mm)
  23764. might_lock_read(&current->mm->mmap_sem);
  23765. +#endif
  23766. }
  23767. -EXPORT_SYMBOL(might_fault);
  23768. +EXPORT_SYMBOL(__might_fault);
  23769. #endif
  23770. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
  23771. diff -Nur linux-4.1.13.orig/mm/mmu_context.c linux-4.1.13/mm/mmu_context.c
  23772. --- linux-4.1.13.orig/mm/mmu_context.c 2015-11-09 23:34:10.000000000 +0100
  23773. +++ linux-4.1.13/mm/mmu_context.c 2015-11-29 09:23:09.661610728 +0100
  23774. @@ -23,6 +23,7 @@
  23775. struct task_struct *tsk = current;
  23776. task_lock(tsk);
  23777. + preempt_disable_rt();
  23778. active_mm = tsk->active_mm;
  23779. if (active_mm != mm) {
  23780. atomic_inc(&mm->mm_count);
  23781. @@ -30,6 +31,7 @@
  23782. }
  23783. tsk->mm = mm;
  23784. switch_mm(active_mm, mm, tsk);
  23785. + preempt_enable_rt();
  23786. task_unlock(tsk);
  23787. #ifdef finish_arch_post_lock_switch
  23788. finish_arch_post_lock_switch();
  23789. diff -Nur linux-4.1.13.orig/mm/page_alloc.c linux-4.1.13/mm/page_alloc.c
  23790. --- linux-4.1.13.orig/mm/page_alloc.c 2015-11-09 23:34:10.000000000 +0100
  23791. +++ linux-4.1.13/mm/page_alloc.c 2015-11-29 09:23:09.661610728 +0100
  23792. @@ -60,6 +60,7 @@
  23793. #include <linux/page_ext.h>
  23794. #include <linux/hugetlb.h>
  23795. #include <linux/sched/rt.h>
  23796. +#include <linux/locallock.h>
  23797. #include <linux/page_owner.h>
  23798. #include <asm/sections.h>
  23799. @@ -233,6 +234,18 @@
  23800. EXPORT_SYMBOL(nr_online_nodes);
  23801. #endif
  23802. +static DEFINE_LOCAL_IRQ_LOCK(pa_lock);
  23803. +
  23804. +#ifdef CONFIG_PREEMPT_RT_BASE
  23805. +# define cpu_lock_irqsave(cpu, flags) \
  23806. + local_lock_irqsave_on(pa_lock, flags, cpu)
  23807. +# define cpu_unlock_irqrestore(cpu, flags) \
  23808. + local_unlock_irqrestore_on(pa_lock, flags, cpu)
  23809. +#else
  23810. +# define cpu_lock_irqsave(cpu, flags) local_irq_save(flags)
  23811. +# define cpu_unlock_irqrestore(cpu, flags) local_irq_restore(flags)
  23812. +#endif
  23813. +
  23814. int page_group_by_mobility_disabled __read_mostly;
  23815. void set_pageblock_migratetype(struct page *page, int migratetype)
  23816. @@ -681,7 +694,7 @@
  23817. }
  23818. /*
  23819. - * Frees a number of pages from the PCP lists
  23820. + * Frees a number of pages which have been collected from the pcp lists.
  23821. * Assumes all pages on list are in same zone, and of same order.
  23822. * count is the number of pages to free.
  23823. *
  23824. @@ -692,18 +705,51 @@
  23825. * pinned" detection logic.
  23826. */
  23827. static void free_pcppages_bulk(struct zone *zone, int count,
  23828. - struct per_cpu_pages *pcp)
  23829. + struct list_head *list)
  23830. {
  23831. - int migratetype = 0;
  23832. - int batch_free = 0;
  23833. int to_free = count;
  23834. unsigned long nr_scanned;
  23835. + unsigned long flags;
  23836. +
  23837. + spin_lock_irqsave(&zone->lock, flags);
  23838. - spin_lock(&zone->lock);
  23839. nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
  23840. if (nr_scanned)
  23841. __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
  23842. + while (!list_empty(list)) {
  23843. + struct page *page = list_first_entry(list, struct page, lru);
  23844. + int mt; /* migratetype of the to-be-freed page */
  23845. +
  23846. + /* must delete as __free_one_page list manipulates */
  23847. + list_del(&page->lru);
  23848. +
  23849. + mt = get_freepage_migratetype(page);
  23850. + if (unlikely(has_isolate_pageblock(zone)))
  23851. + mt = get_pageblock_migratetype(page);
  23852. +
  23853. + /* MIGRATE_MOVABLE list may include MIGRATE_RESERVEs */
  23854. + __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  23855. + trace_mm_page_pcpu_drain(page, 0, mt);
  23856. + to_free--;
  23857. + }
  23858. + WARN_ON(to_free != 0);
  23859. + spin_unlock_irqrestore(&zone->lock, flags);
  23860. +}
  23861. +
  23862. +/*
  23863. + * Moves a number of pages from the PCP lists to free list which
  23864. + * is freed outside of the locked region.
  23865. + *
  23866. + * Assumes all pages on list are in same zone, and of same order.
  23867. + * count is the number of pages to free.
  23868. + */
  23869. +static void isolate_pcp_pages(int to_free, struct per_cpu_pages *src,
  23870. + struct list_head *dst)
  23871. +{
  23872. + int migratetype = 0;
  23873. + int batch_free = 0;
  23874. +
  23875. while (to_free) {
  23876. struct page *page;
  23877. struct list_head *list;
  23878. @@ -719,7 +765,7 @@
  23879. batch_free++;
  23880. if (++migratetype == MIGRATE_PCPTYPES)
  23881. migratetype = 0;
  23882. - list = &pcp->lists[migratetype];
  23883. + list = &src->lists[migratetype];
  23884. } while (list_empty(list));
  23885. /* This is the only non-empty list. Free them all. */
  23886. @@ -727,21 +773,11 @@
  23887. batch_free = to_free;
  23888. do {
  23889. - int mt; /* migratetype of the to-be-freed page */
  23890. -
  23891. - page = list_entry(list->prev, struct page, lru);
  23892. - /* must delete as __free_one_page list manipulates */
  23893. + page = list_last_entry(list, struct page, lru);
  23894. list_del(&page->lru);
  23895. - mt = get_freepage_migratetype(page);
  23896. - if (unlikely(has_isolate_pageblock(zone)))
  23897. - mt = get_pageblock_migratetype(page);
  23898. -
  23899. - /* MIGRATE_MOVABLE list may include MIGRATE_RESERVEs */
  23900. - __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  23901. - trace_mm_page_pcpu_drain(page, 0, mt);
  23902. + list_add(&page->lru, dst);
  23903. } while (--to_free && --batch_free && !list_empty(list));
  23904. }
  23905. - spin_unlock(&zone->lock);
  23906. }
  23907. static void free_one_page(struct zone *zone,
  23908. @@ -750,7 +786,9 @@
  23909. int migratetype)
  23910. {
  23911. unsigned long nr_scanned;
  23912. - spin_lock(&zone->lock);
  23913. + unsigned long flags;
  23914. +
  23915. + spin_lock_irqsave(&zone->lock, flags);
  23916. nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
  23917. if (nr_scanned)
  23918. __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
  23919. @@ -760,7 +798,7 @@
  23920. migratetype = get_pfnblock_migratetype(page, pfn);
  23921. }
  23922. __free_one_page(page, pfn, zone, order, migratetype);
  23923. - spin_unlock(&zone->lock);
  23924. + spin_unlock_irqrestore(&zone->lock, flags);
  23925. }
  23926. static int free_tail_pages_check(struct page *head_page, struct page *page)
  23927. @@ -825,11 +863,11 @@
  23928. return;
  23929. migratetype = get_pfnblock_migratetype(page, pfn);
  23930. - local_irq_save(flags);
  23931. + local_lock_irqsave(pa_lock, flags);
  23932. __count_vm_events(PGFREE, 1 << order);
  23933. set_freepage_migratetype(page, migratetype);
  23934. free_one_page(page_zone(page), page, pfn, order, migratetype);
  23935. - local_irq_restore(flags);
  23936. + local_unlock_irqrestore(pa_lock, flags);
  23937. }
  23938. void __init __free_pages_bootmem(struct page *page, unsigned int order)
  23939. @@ -1371,16 +1409,18 @@
  23940. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
  23941. {
  23942. unsigned long flags;
  23943. + LIST_HEAD(dst);
  23944. int to_drain, batch;
  23945. - local_irq_save(flags);
  23946. + local_lock_irqsave(pa_lock, flags);
  23947. batch = READ_ONCE(pcp->batch);
  23948. to_drain = min(pcp->count, batch);
  23949. if (to_drain > 0) {
  23950. - free_pcppages_bulk(zone, to_drain, pcp);
  23951. + isolate_pcp_pages(to_drain, pcp, &dst);
  23952. pcp->count -= to_drain;
  23953. }
  23954. - local_irq_restore(flags);
  23955. + local_unlock_irqrestore(pa_lock, flags);
  23956. + free_pcppages_bulk(zone, to_drain, &dst);
  23957. }
  23958. #endif
  23959. @@ -1396,16 +1436,21 @@
  23960. unsigned long flags;
  23961. struct per_cpu_pageset *pset;
  23962. struct per_cpu_pages *pcp;
  23963. + LIST_HEAD(dst);
  23964. + int count;
  23965. - local_irq_save(flags);
  23966. + cpu_lock_irqsave(cpu, flags);
  23967. pset = per_cpu_ptr(zone->pageset, cpu);
  23968. pcp = &pset->pcp;
  23969. - if (pcp->count) {
  23970. - free_pcppages_bulk(zone, pcp->count, pcp);
  23971. + count = pcp->count;
  23972. + if (count) {
  23973. + isolate_pcp_pages(count, pcp, &dst);
  23974. pcp->count = 0;
  23975. }
  23976. - local_irq_restore(flags);
  23977. + cpu_unlock_irqrestore(cpu, flags);
  23978. + if (count)
  23979. + free_pcppages_bulk(zone, count, &dst);
  23980. }
  23981. /*
  23982. @@ -1491,8 +1536,17 @@
  23983. else
  23984. cpumask_clear_cpu(cpu, &cpus_with_pcps);
  23985. }
  23986. +#ifndef CONFIG_PREEMPT_RT_BASE
  23987. on_each_cpu_mask(&cpus_with_pcps, (smp_call_func_t) drain_local_pages,
  23988. zone, 1);
  23989. +#else
  23990. + for_each_cpu(cpu, &cpus_with_pcps) {
  23991. + if (zone)
  23992. + drain_pages_zone(cpu, zone);
  23993. + else
  23994. + drain_pages(cpu);
  23995. + }
  23996. +#endif
  23997. }
  23998. #ifdef CONFIG_HIBERNATION
  23999. @@ -1548,7 +1602,7 @@
  24000. migratetype = get_pfnblock_migratetype(page, pfn);
  24001. set_freepage_migratetype(page, migratetype);
  24002. - local_irq_save(flags);
  24003. + local_lock_irqsave(pa_lock, flags);
  24004. __count_vm_event(PGFREE);
  24005. /*
  24006. @@ -1574,12 +1628,17 @@
  24007. pcp->count++;
  24008. if (pcp->count >= pcp->high) {
  24009. unsigned long batch = READ_ONCE(pcp->batch);
  24010. - free_pcppages_bulk(zone, batch, pcp);
  24011. + LIST_HEAD(dst);
  24012. +
  24013. + isolate_pcp_pages(batch, pcp, &dst);
  24014. pcp->count -= batch;
  24015. + local_unlock_irqrestore(pa_lock, flags);
  24016. + free_pcppages_bulk(zone, batch, &dst);
  24017. + return;
  24018. }
  24019. out:
  24020. - local_irq_restore(flags);
  24021. + local_unlock_irqrestore(pa_lock, flags);
  24022. }
  24023. /*
  24024. @@ -1710,7 +1769,7 @@
  24025. struct per_cpu_pages *pcp;
  24026. struct list_head *list;
  24027. - local_irq_save(flags);
  24028. + local_lock_irqsave(pa_lock, flags);
  24029. pcp = &this_cpu_ptr(zone->pageset)->pcp;
  24030. list = &pcp->lists[migratetype];
  24031. if (list_empty(list)) {
  24032. @@ -1742,13 +1801,15 @@
  24033. */
  24034. WARN_ON_ONCE(order > 1);
  24035. }
  24036. - spin_lock_irqsave(&zone->lock, flags);
  24037. + local_spin_lock_irqsave(pa_lock, &zone->lock, flags);
  24038. page = __rmqueue(zone, order, migratetype);
  24039. - spin_unlock(&zone->lock);
  24040. - if (!page)
  24041. + if (!page) {
  24042. + spin_unlock(&zone->lock);
  24043. goto failed;
  24044. + }
  24045. __mod_zone_freepage_state(zone, -(1 << order),
  24046. get_freepage_migratetype(page));
  24047. + spin_unlock(&zone->lock);
  24048. }
  24049. __mod_zone_page_state(zone, NR_ALLOC_BATCH, -(1 << order));
  24050. @@ -1758,13 +1819,13 @@
  24051. __count_zone_vm_events(PGALLOC, zone, 1 << order);
  24052. zone_statistics(preferred_zone, zone, gfp_flags);
  24053. - local_irq_restore(flags);
  24054. + local_unlock_irqrestore(pa_lock, flags);
  24055. VM_BUG_ON_PAGE(bad_range(zone, page), page);
  24056. return page;
  24057. failed:
  24058. - local_irq_restore(flags);
  24059. + local_unlock_irqrestore(pa_lock, flags);
  24060. return NULL;
  24061. }
  24062. @@ -5653,6 +5714,7 @@
  24063. void __init page_alloc_init(void)
  24064. {
  24065. hotcpu_notifier(page_alloc_cpu_notify, 0);
  24066. + local_irq_lock_init(pa_lock);
  24067. }
  24068. /*
  24069. @@ -6547,7 +6609,7 @@
  24070. struct per_cpu_pageset *pset;
  24071. /* avoid races with drain_pages() */
  24072. - local_irq_save(flags);
  24073. + local_lock_irqsave(pa_lock, flags);
  24074. if (zone->pageset != &boot_pageset) {
  24075. for_each_online_cpu(cpu) {
  24076. pset = per_cpu_ptr(zone->pageset, cpu);
  24077. @@ -6556,7 +6618,7 @@
  24078. free_percpu(zone->pageset);
  24079. zone->pageset = &boot_pageset;
  24080. }
  24081. - local_irq_restore(flags);
  24082. + local_unlock_irqrestore(pa_lock, flags);
  24083. }
  24084. #ifdef CONFIG_MEMORY_HOTREMOVE
  24085. diff -Nur linux-4.1.13.orig/mm/slab.h linux-4.1.13/mm/slab.h
  24086. --- linux-4.1.13.orig/mm/slab.h 2015-11-09 23:34:10.000000000 +0100
  24087. +++ linux-4.1.13/mm/slab.h 2015-11-29 09:23:09.661610728 +0100
  24088. @@ -330,7 +330,11 @@
  24089. * The slab lists for all objects.
  24090. */
  24091. struct kmem_cache_node {
  24092. +#ifdef CONFIG_SLUB
  24093. + raw_spinlock_t list_lock;
  24094. +#else
  24095. spinlock_t list_lock;
  24096. +#endif
  24097. #ifdef CONFIG_SLAB
  24098. struct list_head slabs_partial; /* partial list first, better asm code */
  24099. diff -Nur linux-4.1.13.orig/mm/slub.c linux-4.1.13/mm/slub.c
  24100. --- linux-4.1.13.orig/mm/slub.c 2015-11-09 23:34:10.000000000 +0100
  24101. +++ linux-4.1.13/mm/slub.c 2015-11-29 09:23:09.665610461 +0100
  24102. @@ -1069,7 +1069,7 @@
  24103. {
  24104. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  24105. - spin_lock_irqsave(&n->list_lock, *flags);
  24106. + raw_spin_lock_irqsave(&n->list_lock, *flags);
  24107. slab_lock(page);
  24108. if (!check_slab(s, page))
  24109. @@ -1116,7 +1116,7 @@
  24110. fail:
  24111. slab_unlock(page);
  24112. - spin_unlock_irqrestore(&n->list_lock, *flags);
  24113. + raw_spin_unlock_irqrestore(&n->list_lock, *flags);
  24114. slab_fix(s, "Object at 0x%p not freed", object);
  24115. return NULL;
  24116. }
  24117. @@ -1242,6 +1242,12 @@
  24118. #endif /* CONFIG_SLUB_DEBUG */
  24119. +struct slub_free_list {
  24120. + raw_spinlock_t lock;
  24121. + struct list_head list;
  24122. +};
  24123. +static DEFINE_PER_CPU(struct slub_free_list, slub_free_list);
  24124. +
  24125. /*
  24126. * Hooks for other subsystems that check memory allocations. In a typical
  24127. * production configuration these hooks all should produce no code at all.
  24128. @@ -1306,6 +1312,17 @@
  24129. kasan_slab_free(s, x);
  24130. }
  24131. +static void setup_object(struct kmem_cache *s, struct page *page,
  24132. + void *object)
  24133. +{
  24134. + setup_object_debug(s, page, object);
  24135. + if (unlikely(s->ctor)) {
  24136. + kasan_unpoison_object_data(s, object);
  24137. + s->ctor(object);
  24138. + kasan_poison_object_data(s, object);
  24139. + }
  24140. +}
  24141. +
  24142. /*
  24143. * Slab allocation and freeing
  24144. */
  24145. @@ -1336,10 +1353,17 @@
  24146. struct page *page;
  24147. struct kmem_cache_order_objects oo = s->oo;
  24148. gfp_t alloc_gfp;
  24149. + void *start, *p;
  24150. + int idx, order;
  24151. + bool enableirqs;
  24152. flags &= gfp_allowed_mask;
  24153. - if (flags & __GFP_WAIT)
  24154. + enableirqs = (flags & __GFP_WAIT) != 0;
  24155. +#ifdef CONFIG_PREEMPT_RT_FULL
  24156. + enableirqs |= system_state == SYSTEM_RUNNING;
  24157. +#endif
  24158. + if (enableirqs)
  24159. local_irq_enable();
  24160. flags |= s->allocflags;
  24161. @@ -1359,13 +1383,13 @@
  24162. * Try a lower order alloc if possible
  24163. */
  24164. page = alloc_slab_page(s, alloc_gfp, node, oo);
  24165. -
  24166. - if (page)
  24167. - stat(s, ORDER_FALLBACK);
  24168. + if (unlikely(!page))
  24169. + goto out;
  24170. + stat(s, ORDER_FALLBACK);
  24171. }
  24172. - if (kmemcheck_enabled && page
  24173. - && !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
  24174. + if (kmemcheck_enabled &&
  24175. + !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
  24176. int pages = 1 << oo_order(oo);
  24177. kmemcheck_alloc_shadow(page, oo_order(oo), alloc_gfp, node);
  24178. @@ -1380,51 +1404,9 @@
  24179. kmemcheck_mark_unallocated_pages(page, pages);
  24180. }
  24181. - if (flags & __GFP_WAIT)
  24182. - local_irq_disable();
  24183. - if (!page)
  24184. - return NULL;
  24185. -
  24186. page->objects = oo_objects(oo);
  24187. - mod_zone_page_state(page_zone(page),
  24188. - (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  24189. - NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  24190. - 1 << oo_order(oo));
  24191. -
  24192. - return page;
  24193. -}
  24194. -
  24195. -static void setup_object(struct kmem_cache *s, struct page *page,
  24196. - void *object)
  24197. -{
  24198. - setup_object_debug(s, page, object);
  24199. - if (unlikely(s->ctor)) {
  24200. - kasan_unpoison_object_data(s, object);
  24201. - s->ctor(object);
  24202. - kasan_poison_object_data(s, object);
  24203. - }
  24204. -}
  24205. -
  24206. -static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
  24207. -{
  24208. - struct page *page;
  24209. - void *start;
  24210. - void *p;
  24211. - int order;
  24212. - int idx;
  24213. -
  24214. - if (unlikely(flags & GFP_SLAB_BUG_MASK)) {
  24215. - pr_emerg("gfp: %u\n", flags & GFP_SLAB_BUG_MASK);
  24216. - BUG();
  24217. - }
  24218. -
  24219. - page = allocate_slab(s,
  24220. - flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
  24221. - if (!page)
  24222. - goto out;
  24223. order = compound_order(page);
  24224. - inc_slabs_node(s, page_to_nid(page), page->objects);
  24225. page->slab_cache = s;
  24226. __SetPageSlab(page);
  24227. if (page_is_pfmemalloc(page))
  24228. @@ -1448,10 +1430,34 @@
  24229. page->freelist = start;
  24230. page->inuse = page->objects;
  24231. page->frozen = 1;
  24232. +
  24233. out:
  24234. + if (enableirqs)
  24235. + local_irq_disable();
  24236. + if (!page)
  24237. + return NULL;
  24238. +
  24239. + mod_zone_page_state(page_zone(page),
  24240. + (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  24241. + NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  24242. + 1 << oo_order(oo));
  24243. +
  24244. + inc_slabs_node(s, page_to_nid(page), page->objects);
  24245. +
  24246. return page;
  24247. }
  24248. +static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
  24249. +{
  24250. + if (unlikely(flags & GFP_SLAB_BUG_MASK)) {
  24251. + pr_emerg("gfp: %u\n", flags & GFP_SLAB_BUG_MASK);
  24252. + BUG();
  24253. + }
  24254. +
  24255. + return allocate_slab(s,
  24256. + flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
  24257. +}
  24258. +
  24259. static void __free_slab(struct kmem_cache *s, struct page *page)
  24260. {
  24261. int order = compound_order(page);
  24262. @@ -1483,6 +1489,16 @@
  24263. memcg_uncharge_slab(s, order);
  24264. }
  24265. +static void free_delayed(struct list_head *h)
  24266. +{
  24267. + while(!list_empty(h)) {
  24268. + struct page *page = list_first_entry(h, struct page, lru);
  24269. +
  24270. + list_del(&page->lru);
  24271. + __free_slab(page->slab_cache, page);
  24272. + }
  24273. +}
  24274. +
  24275. #define need_reserve_slab_rcu \
  24276. (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
  24277. @@ -1517,6 +1533,12 @@
  24278. }
  24279. call_rcu(head, rcu_free_slab);
  24280. + } else if (irqs_disabled()) {
  24281. + struct slub_free_list *f = this_cpu_ptr(&slub_free_list);
  24282. +
  24283. + raw_spin_lock(&f->lock);
  24284. + list_add(&page->lru, &f->list);
  24285. + raw_spin_unlock(&f->lock);
  24286. } else
  24287. __free_slab(s, page);
  24288. }
  24289. @@ -1630,7 +1652,7 @@
  24290. if (!n || !n->nr_partial)
  24291. return NULL;
  24292. - spin_lock(&n->list_lock);
  24293. + raw_spin_lock(&n->list_lock);
  24294. list_for_each_entry_safe(page, page2, &n->partial, lru) {
  24295. void *t;
  24296. @@ -1655,7 +1677,7 @@
  24297. break;
  24298. }
  24299. - spin_unlock(&n->list_lock);
  24300. + raw_spin_unlock(&n->list_lock);
  24301. return object;
  24302. }
  24303. @@ -1901,7 +1923,7 @@
  24304. * that acquire_slab() will see a slab page that
  24305. * is frozen
  24306. */
  24307. - spin_lock(&n->list_lock);
  24308. + raw_spin_lock(&n->list_lock);
  24309. }
  24310. } else {
  24311. m = M_FULL;
  24312. @@ -1912,7 +1934,7 @@
  24313. * slabs from diagnostic functions will not see
  24314. * any frozen slabs.
  24315. */
  24316. - spin_lock(&n->list_lock);
  24317. + raw_spin_lock(&n->list_lock);
  24318. }
  24319. }
  24320. @@ -1947,7 +1969,7 @@
  24321. goto redo;
  24322. if (lock)
  24323. - spin_unlock(&n->list_lock);
  24324. + raw_spin_unlock(&n->list_lock);
  24325. if (m == M_FREE) {
  24326. stat(s, DEACTIVATE_EMPTY);
  24327. @@ -1979,10 +2001,10 @@
  24328. n2 = get_node(s, page_to_nid(page));
  24329. if (n != n2) {
  24330. if (n)
  24331. - spin_unlock(&n->list_lock);
  24332. + raw_spin_unlock(&n->list_lock);
  24333. n = n2;
  24334. - spin_lock(&n->list_lock);
  24335. + raw_spin_lock(&n->list_lock);
  24336. }
  24337. do {
  24338. @@ -2011,7 +2033,7 @@
  24339. }
  24340. if (n)
  24341. - spin_unlock(&n->list_lock);
  24342. + raw_spin_unlock(&n->list_lock);
  24343. while (discard_page) {
  24344. page = discard_page;
  24345. @@ -2050,14 +2072,21 @@
  24346. pobjects = oldpage->pobjects;
  24347. pages = oldpage->pages;
  24348. if (drain && pobjects > s->cpu_partial) {
  24349. + struct slub_free_list *f;
  24350. unsigned long flags;
  24351. + LIST_HEAD(tofree);
  24352. /*
  24353. * partial array is full. Move the existing
  24354. * set to the per node partial list.
  24355. */
  24356. local_irq_save(flags);
  24357. unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
  24358. + f = this_cpu_ptr(&slub_free_list);
  24359. + raw_spin_lock(&f->lock);
  24360. + list_splice_init(&f->list, &tofree);
  24361. + raw_spin_unlock(&f->lock);
  24362. local_irq_restore(flags);
  24363. + free_delayed(&tofree);
  24364. oldpage = NULL;
  24365. pobjects = 0;
  24366. pages = 0;
  24367. @@ -2129,7 +2158,22 @@
  24368. static void flush_all(struct kmem_cache *s)
  24369. {
  24370. + LIST_HEAD(tofree);
  24371. + int cpu;
  24372. +
  24373. on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1, GFP_ATOMIC);
  24374. + for_each_online_cpu(cpu) {
  24375. + struct slub_free_list *f;
  24376. +
  24377. + if (!has_cpu_slab(cpu, s))
  24378. + continue;
  24379. +
  24380. + f = &per_cpu(slub_free_list, cpu);
  24381. + raw_spin_lock_irq(&f->lock);
  24382. + list_splice_init(&f->list, &tofree);
  24383. + raw_spin_unlock_irq(&f->lock);
  24384. + free_delayed(&tofree);
  24385. + }
  24386. }
  24387. /*
  24388. @@ -2165,10 +2209,10 @@
  24389. unsigned long x = 0;
  24390. struct page *page;
  24391. - spin_lock_irqsave(&n->list_lock, flags);
  24392. + raw_spin_lock_irqsave(&n->list_lock, flags);
  24393. list_for_each_entry(page, &n->partial, lru)
  24394. x += get_count(page);
  24395. - spin_unlock_irqrestore(&n->list_lock, flags);
  24396. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  24397. return x;
  24398. }
  24399. #endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
  24400. @@ -2305,9 +2349,11 @@
  24401. static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  24402. unsigned long addr, struct kmem_cache_cpu *c)
  24403. {
  24404. + struct slub_free_list *f;
  24405. void *freelist;
  24406. struct page *page;
  24407. unsigned long flags;
  24408. + LIST_HEAD(tofree);
  24409. local_irq_save(flags);
  24410. #ifdef CONFIG_PREEMPT
  24411. @@ -2375,7 +2421,13 @@
  24412. VM_BUG_ON(!c->page->frozen);
  24413. c->freelist = get_freepointer(s, freelist);
  24414. c->tid = next_tid(c->tid);
  24415. +out:
  24416. + f = this_cpu_ptr(&slub_free_list);
  24417. + raw_spin_lock(&f->lock);
  24418. + list_splice_init(&f->list, &tofree);
  24419. + raw_spin_unlock(&f->lock);
  24420. local_irq_restore(flags);
  24421. + free_delayed(&tofree);
  24422. return freelist;
  24423. new_slab:
  24424. @@ -2392,8 +2444,7 @@
  24425. if (unlikely(!freelist)) {
  24426. slab_out_of_memory(s, gfpflags, node);
  24427. - local_irq_restore(flags);
  24428. - return NULL;
  24429. + goto out;
  24430. }
  24431. page = c->page;
  24432. @@ -2408,8 +2459,7 @@
  24433. deactivate_slab(s, page, get_freepointer(s, freelist));
  24434. c->page = NULL;
  24435. c->freelist = NULL;
  24436. - local_irq_restore(flags);
  24437. - return freelist;
  24438. + goto out;
  24439. }
  24440. /*
  24441. @@ -2593,7 +2643,7 @@
  24442. do {
  24443. if (unlikely(n)) {
  24444. - spin_unlock_irqrestore(&n->list_lock, flags);
  24445. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  24446. n = NULL;
  24447. }
  24448. prior = page->freelist;
  24449. @@ -2625,7 +2675,7 @@
  24450. * Otherwise the list_lock will synchronize with
  24451. * other processors updating the list of slabs.
  24452. */
  24453. - spin_lock_irqsave(&n->list_lock, flags);
  24454. + raw_spin_lock_irqsave(&n->list_lock, flags);
  24455. }
  24456. }
  24457. @@ -2667,7 +2717,7 @@
  24458. add_partial(n, page, DEACTIVATE_TO_TAIL);
  24459. stat(s, FREE_ADD_PARTIAL);
  24460. }
  24461. - spin_unlock_irqrestore(&n->list_lock, flags);
  24462. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  24463. return;
  24464. slab_empty:
  24465. @@ -2682,7 +2732,7 @@
  24466. remove_full(s, n, page);
  24467. }
  24468. - spin_unlock_irqrestore(&n->list_lock, flags);
  24469. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  24470. stat(s, FREE_SLAB);
  24471. discard_slab(s, page);
  24472. }
  24473. @@ -2881,7 +2931,7 @@
  24474. init_kmem_cache_node(struct kmem_cache_node *n)
  24475. {
  24476. n->nr_partial = 0;
  24477. - spin_lock_init(&n->list_lock);
  24478. + raw_spin_lock_init(&n->list_lock);
  24479. INIT_LIST_HEAD(&n->partial);
  24480. #ifdef CONFIG_SLUB_DEBUG
  24481. atomic_long_set(&n->nr_slabs, 0);
  24482. @@ -3463,7 +3513,7 @@
  24483. for (i = 0; i < SHRINK_PROMOTE_MAX; i++)
  24484. INIT_LIST_HEAD(promote + i);
  24485. - spin_lock_irqsave(&n->list_lock, flags);
  24486. + raw_spin_lock_irqsave(&n->list_lock, flags);
  24487. /*
  24488. * Build lists of slabs to discard or promote.
  24489. @@ -3494,7 +3544,7 @@
  24490. for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--)
  24491. list_splice(promote + i, &n->partial);
  24492. - spin_unlock_irqrestore(&n->list_lock, flags);
  24493. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  24494. /* Release empty slabs */
  24495. list_for_each_entry_safe(page, t, &discard, lru)
  24496. @@ -3670,6 +3720,12 @@
  24497. {
  24498. static __initdata struct kmem_cache boot_kmem_cache,
  24499. boot_kmem_cache_node;
  24500. + int cpu;
  24501. +
  24502. + for_each_possible_cpu(cpu) {
  24503. + raw_spin_lock_init(&per_cpu(slub_free_list, cpu).lock);
  24504. + INIT_LIST_HEAD(&per_cpu(slub_free_list, cpu).list);
  24505. + }
  24506. if (debug_guardpage_minorder())
  24507. slub_max_order = 0;
  24508. @@ -3912,7 +3968,7 @@
  24509. struct page *page;
  24510. unsigned long flags;
  24511. - spin_lock_irqsave(&n->list_lock, flags);
  24512. + raw_spin_lock_irqsave(&n->list_lock, flags);
  24513. list_for_each_entry(page, &n->partial, lru) {
  24514. validate_slab_slab(s, page, map);
  24515. @@ -3934,7 +3990,7 @@
  24516. s->name, count, atomic_long_read(&n->nr_slabs));
  24517. out:
  24518. - spin_unlock_irqrestore(&n->list_lock, flags);
  24519. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  24520. return count;
  24521. }
  24522. @@ -4122,12 +4178,12 @@
  24523. if (!atomic_long_read(&n->nr_slabs))
  24524. continue;
  24525. - spin_lock_irqsave(&n->list_lock, flags);
  24526. + raw_spin_lock_irqsave(&n->list_lock, flags);
  24527. list_for_each_entry(page, &n->partial, lru)
  24528. process_slab(&t, s, page, alloc, map);
  24529. list_for_each_entry(page, &n->full, lru)
  24530. process_slab(&t, s, page, alloc, map);
  24531. - spin_unlock_irqrestore(&n->list_lock, flags);
  24532. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  24533. }
  24534. for (i = 0; i < t.count; i++) {
  24535. diff -Nur linux-4.1.13.orig/mm/swap.c linux-4.1.13/mm/swap.c
  24536. --- linux-4.1.13.orig/mm/swap.c 2015-11-09 23:34:10.000000000 +0100
  24537. +++ linux-4.1.13/mm/swap.c 2015-11-29 09:23:09.665610461 +0100
  24538. @@ -32,6 +32,7 @@
  24539. #include <linux/gfp.h>
  24540. #include <linux/uio.h>
  24541. #include <linux/hugetlb.h>
  24542. +#include <linux/locallock.h>
  24543. #include "internal.h"
  24544. @@ -45,6 +46,9 @@
  24545. static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
  24546. static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
  24547. +static DEFINE_LOCAL_IRQ_LOCK(rotate_lock);
  24548. +DEFINE_LOCAL_IRQ_LOCK(swapvec_lock);
  24549. +
  24550. /*
  24551. * This path almost never happens for VM activity - pages are normally
  24552. * freed via pagevecs. But it gets used by networking.
  24553. @@ -481,11 +485,11 @@
  24554. unsigned long flags;
  24555. page_cache_get(page);
  24556. - local_irq_save(flags);
  24557. + local_lock_irqsave(rotate_lock, flags);
  24558. pvec = this_cpu_ptr(&lru_rotate_pvecs);
  24559. if (!pagevec_add(pvec, page))
  24560. pagevec_move_tail(pvec);
  24561. - local_irq_restore(flags);
  24562. + local_unlock_irqrestore(rotate_lock, flags);
  24563. }
  24564. }
  24565. @@ -536,12 +540,13 @@
  24566. void activate_page(struct page *page)
  24567. {
  24568. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  24569. - struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
  24570. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  24571. + activate_page_pvecs);
  24572. page_cache_get(page);
  24573. if (!pagevec_add(pvec, page))
  24574. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  24575. - put_cpu_var(activate_page_pvecs);
  24576. + put_locked_var(swapvec_lock, activate_page_pvecs);
  24577. }
  24578. }
  24579. @@ -567,7 +572,7 @@
  24580. static void __lru_cache_activate_page(struct page *page)
  24581. {
  24582. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  24583. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  24584. int i;
  24585. /*
  24586. @@ -589,7 +594,7 @@
  24587. }
  24588. }
  24589. - put_cpu_var(lru_add_pvec);
  24590. + put_locked_var(swapvec_lock, lru_add_pvec);
  24591. }
  24592. /*
  24593. @@ -628,13 +633,13 @@
  24594. static void __lru_cache_add(struct page *page)
  24595. {
  24596. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  24597. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  24598. page_cache_get(page);
  24599. if (!pagevec_space(pvec))
  24600. __pagevec_lru_add(pvec);
  24601. pagevec_add(pvec, page);
  24602. - put_cpu_var(lru_add_pvec);
  24603. + put_locked_var(swapvec_lock, lru_add_pvec);
  24604. }
  24605. /**
  24606. @@ -814,9 +819,9 @@
  24607. unsigned long flags;
  24608. /* No harm done if a racing interrupt already did this */
  24609. - local_irq_save(flags);
  24610. + local_lock_irqsave(rotate_lock, flags);
  24611. pagevec_move_tail(pvec);
  24612. - local_irq_restore(flags);
  24613. + local_unlock_irqrestore(rotate_lock, flags);
  24614. }
  24615. pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
  24616. @@ -844,18 +849,19 @@
  24617. return;
  24618. if (likely(get_page_unless_zero(page))) {
  24619. - struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
  24620. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  24621. + lru_deactivate_file_pvecs);
  24622. if (!pagevec_add(pvec, page))
  24623. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  24624. - put_cpu_var(lru_deactivate_file_pvecs);
  24625. + put_locked_var(swapvec_lock, lru_deactivate_file_pvecs);
  24626. }
  24627. }
  24628. void lru_add_drain(void)
  24629. {
  24630. - lru_add_drain_cpu(get_cpu());
  24631. - put_cpu();
  24632. + lru_add_drain_cpu(local_lock_cpu(swapvec_lock));
  24633. + local_unlock_cpu(swapvec_lock);
  24634. }
  24635. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  24636. diff -Nur linux-4.1.13.orig/mm/truncate.c linux-4.1.13/mm/truncate.c
  24637. --- linux-4.1.13.orig/mm/truncate.c 2015-11-09 23:34:10.000000000 +0100
  24638. +++ linux-4.1.13/mm/truncate.c 2015-11-29 09:23:09.665610461 +0100
  24639. @@ -56,8 +56,11 @@
  24640. * protected by mapping->tree_lock.
  24641. */
  24642. if (!workingset_node_shadows(node) &&
  24643. - !list_empty(&node->private_list))
  24644. - list_lru_del(&workingset_shadow_nodes, &node->private_list);
  24645. + !list_empty(&node->private_list)) {
  24646. + local_lock(workingset_shadow_lock);
  24647. + list_lru_del(&__workingset_shadow_nodes, &node->private_list);
  24648. + local_unlock(workingset_shadow_lock);
  24649. + }
  24650. __radix_tree_delete_node(&mapping->page_tree, node);
  24651. unlock:
  24652. spin_unlock_irq(&mapping->tree_lock);
  24653. diff -Nur linux-4.1.13.orig/mm/vmalloc.c linux-4.1.13/mm/vmalloc.c
  24654. --- linux-4.1.13.orig/mm/vmalloc.c 2015-11-09 23:34:10.000000000 +0100
  24655. +++ linux-4.1.13/mm/vmalloc.c 2015-11-29 09:23:09.665610461 +0100
  24656. @@ -819,7 +819,7 @@
  24657. struct vmap_block *vb;
  24658. struct vmap_area *va;
  24659. unsigned long vb_idx;
  24660. - int node, err;
  24661. + int node, err, cpu;
  24662. void *vaddr;
  24663. node = numa_node_id();
  24664. @@ -862,11 +862,12 @@
  24665. BUG_ON(err);
  24666. radix_tree_preload_end();
  24667. - vbq = &get_cpu_var(vmap_block_queue);
  24668. + cpu = get_cpu_light();
  24669. + vbq = this_cpu_ptr(&vmap_block_queue);
  24670. spin_lock(&vbq->lock);
  24671. list_add_tail_rcu(&vb->free_list, &vbq->free);
  24672. spin_unlock(&vbq->lock);
  24673. - put_cpu_var(vmap_block_queue);
  24674. + put_cpu_light();
  24675. return vaddr;
  24676. }
  24677. @@ -935,6 +936,7 @@
  24678. struct vmap_block *vb;
  24679. void *vaddr = NULL;
  24680. unsigned int order;
  24681. + int cpu;
  24682. BUG_ON(size & ~PAGE_MASK);
  24683. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  24684. @@ -949,7 +951,8 @@
  24685. order = get_order(size);
  24686. rcu_read_lock();
  24687. - vbq = &get_cpu_var(vmap_block_queue);
  24688. + cpu = get_cpu_light();
  24689. + vbq = this_cpu_ptr(&vmap_block_queue);
  24690. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  24691. unsigned long pages_off;
  24692. @@ -972,7 +975,7 @@
  24693. break;
  24694. }
  24695. - put_cpu_var(vmap_block_queue);
  24696. + put_cpu_light();
  24697. rcu_read_unlock();
  24698. /* Allocate new block if nothing was found */
  24699. diff -Nur linux-4.1.13.orig/mm/vmstat.c linux-4.1.13/mm/vmstat.c
  24700. --- linux-4.1.13.orig/mm/vmstat.c 2015-11-09 23:34:10.000000000 +0100
  24701. +++ linux-4.1.13/mm/vmstat.c 2015-11-29 09:23:09.665610461 +0100
  24702. @@ -226,6 +226,7 @@
  24703. long x;
  24704. long t;
  24705. + preempt_disable_rt();
  24706. x = delta + __this_cpu_read(*p);
  24707. t = __this_cpu_read(pcp->stat_threshold);
  24708. @@ -235,6 +236,7 @@
  24709. x = 0;
  24710. }
  24711. __this_cpu_write(*p, x);
  24712. + preempt_enable_rt();
  24713. }
  24714. EXPORT_SYMBOL(__mod_zone_page_state);
  24715. @@ -267,6 +269,7 @@
  24716. s8 __percpu *p = pcp->vm_stat_diff + item;
  24717. s8 v, t;
  24718. + preempt_disable_rt();
  24719. v = __this_cpu_inc_return(*p);
  24720. t = __this_cpu_read(pcp->stat_threshold);
  24721. if (unlikely(v > t)) {
  24722. @@ -275,6 +278,7 @@
  24723. zone_page_state_add(v + overstep, zone, item);
  24724. __this_cpu_write(*p, -overstep);
  24725. }
  24726. + preempt_enable_rt();
  24727. }
  24728. void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
  24729. @@ -289,6 +293,7 @@
  24730. s8 __percpu *p = pcp->vm_stat_diff + item;
  24731. s8 v, t;
  24732. + preempt_disable_rt();
  24733. v = __this_cpu_dec_return(*p);
  24734. t = __this_cpu_read(pcp->stat_threshold);
  24735. if (unlikely(v < - t)) {
  24736. @@ -297,6 +302,7 @@
  24737. zone_page_state_add(v - overstep, zone, item);
  24738. __this_cpu_write(*p, overstep);
  24739. }
  24740. + preempt_enable_rt();
  24741. }
  24742. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  24743. diff -Nur linux-4.1.13.orig/mm/workingset.c linux-4.1.13/mm/workingset.c
  24744. --- linux-4.1.13.orig/mm/workingset.c 2015-11-09 23:34:10.000000000 +0100
  24745. +++ linux-4.1.13/mm/workingset.c 2015-11-29 09:23:09.665610461 +0100
  24746. @@ -264,7 +264,8 @@
  24747. * point where they would still be useful.
  24748. */
  24749. -struct list_lru workingset_shadow_nodes;
  24750. +struct list_lru __workingset_shadow_nodes;
  24751. +DEFINE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  24752. static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  24753. struct shrink_control *sc)
  24754. @@ -274,9 +275,9 @@
  24755. unsigned long pages;
  24756. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  24757. - local_irq_disable();
  24758. - shadow_nodes = list_lru_shrink_count(&workingset_shadow_nodes, sc);
  24759. - local_irq_enable();
  24760. + local_lock_irq(workingset_shadow_lock);
  24761. + shadow_nodes = list_lru_shrink_count(&__workingset_shadow_nodes, sc);
  24762. + local_unlock_irq(workingset_shadow_lock);
  24763. pages = node_present_pages(sc->nid);
  24764. /*
  24765. @@ -363,9 +364,9 @@
  24766. spin_unlock(&mapping->tree_lock);
  24767. ret = LRU_REMOVED_RETRY;
  24768. out:
  24769. - local_irq_enable();
  24770. + local_unlock_irq(workingset_shadow_lock);
  24771. cond_resched();
  24772. - local_irq_disable();
  24773. + local_lock_irq(workingset_shadow_lock);
  24774. spin_lock(lru_lock);
  24775. return ret;
  24776. }
  24777. @@ -376,10 +377,10 @@
  24778. unsigned long ret;
  24779. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  24780. - local_irq_disable();
  24781. - ret = list_lru_shrink_walk(&workingset_shadow_nodes, sc,
  24782. + local_lock_irq(workingset_shadow_lock);
  24783. + ret = list_lru_shrink_walk(&__workingset_shadow_nodes, sc,
  24784. shadow_lru_isolate, NULL);
  24785. - local_irq_enable();
  24786. + local_unlock_irq(workingset_shadow_lock);
  24787. return ret;
  24788. }
  24789. @@ -400,7 +401,7 @@
  24790. {
  24791. int ret;
  24792. - ret = list_lru_init_key(&workingset_shadow_nodes, &shadow_nodes_key);
  24793. + ret = list_lru_init_key(&__workingset_shadow_nodes, &shadow_nodes_key);
  24794. if (ret)
  24795. goto err;
  24796. ret = register_shrinker(&workingset_shadow_shrinker);
  24797. @@ -408,7 +409,7 @@
  24798. goto err_list_lru;
  24799. return 0;
  24800. err_list_lru:
  24801. - list_lru_destroy(&workingset_shadow_nodes);
  24802. + list_lru_destroy(&__workingset_shadow_nodes);
  24803. err:
  24804. return ret;
  24805. }
  24806. diff -Nur linux-4.1.13.orig/net/core/dev.c linux-4.1.13/net/core/dev.c
  24807. --- linux-4.1.13.orig/net/core/dev.c 2015-11-09 23:34:10.000000000 +0100
  24808. +++ linux-4.1.13/net/core/dev.c 2015-11-29 09:23:09.665610461 +0100
  24809. @@ -184,6 +184,7 @@
  24810. static DEFINE_HASHTABLE(napi_hash, 8);
  24811. static seqcount_t devnet_rename_seq;
  24812. +static DEFINE_MUTEX(devnet_rename_mutex);
  24813. static inline void dev_base_seq_inc(struct net *net)
  24814. {
  24815. @@ -205,14 +206,14 @@
  24816. static inline void rps_lock(struct softnet_data *sd)
  24817. {
  24818. #ifdef CONFIG_RPS
  24819. - spin_lock(&sd->input_pkt_queue.lock);
  24820. + raw_spin_lock(&sd->input_pkt_queue.raw_lock);
  24821. #endif
  24822. }
  24823. static inline void rps_unlock(struct softnet_data *sd)
  24824. {
  24825. #ifdef CONFIG_RPS
  24826. - spin_unlock(&sd->input_pkt_queue.lock);
  24827. + raw_spin_unlock(&sd->input_pkt_queue.raw_lock);
  24828. #endif
  24829. }
  24830. @@ -852,7 +853,8 @@
  24831. strcpy(name, dev->name);
  24832. rcu_read_unlock();
  24833. if (read_seqcount_retry(&devnet_rename_seq, seq)) {
  24834. - cond_resched();
  24835. + mutex_lock(&devnet_rename_mutex);
  24836. + mutex_unlock(&devnet_rename_mutex);
  24837. goto retry;
  24838. }
  24839. @@ -1121,20 +1123,17 @@
  24840. if (dev->flags & IFF_UP)
  24841. return -EBUSY;
  24842. - write_seqcount_begin(&devnet_rename_seq);
  24843. + mutex_lock(&devnet_rename_mutex);
  24844. + __raw_write_seqcount_begin(&devnet_rename_seq);
  24845. - if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
  24846. - write_seqcount_end(&devnet_rename_seq);
  24847. - return 0;
  24848. - }
  24849. + if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
  24850. + goto outunlock;
  24851. memcpy(oldname, dev->name, IFNAMSIZ);
  24852. err = dev_get_valid_name(net, dev, newname);
  24853. - if (err < 0) {
  24854. - write_seqcount_end(&devnet_rename_seq);
  24855. - return err;
  24856. - }
  24857. + if (err < 0)
  24858. + goto outunlock;
  24859. if (oldname[0] && !strchr(oldname, '%'))
  24860. netdev_info(dev, "renamed from %s\n", oldname);
  24861. @@ -1147,11 +1146,12 @@
  24862. if (ret) {
  24863. memcpy(dev->name, oldname, IFNAMSIZ);
  24864. dev->name_assign_type = old_assign_type;
  24865. - write_seqcount_end(&devnet_rename_seq);
  24866. - return ret;
  24867. + err = ret;
  24868. + goto outunlock;
  24869. }
  24870. - write_seqcount_end(&devnet_rename_seq);
  24871. + __raw_write_seqcount_end(&devnet_rename_seq);
  24872. + mutex_unlock(&devnet_rename_mutex);
  24873. netdev_adjacent_rename_links(dev, oldname);
  24874. @@ -1172,7 +1172,8 @@
  24875. /* err >= 0 after dev_alloc_name() or stores the first errno */
  24876. if (err >= 0) {
  24877. err = ret;
  24878. - write_seqcount_begin(&devnet_rename_seq);
  24879. + mutex_lock(&devnet_rename_mutex);
  24880. + __raw_write_seqcount_begin(&devnet_rename_seq);
  24881. memcpy(dev->name, oldname, IFNAMSIZ);
  24882. memcpy(oldname, newname, IFNAMSIZ);
  24883. dev->name_assign_type = old_assign_type;
  24884. @@ -1185,6 +1186,11 @@
  24885. }
  24886. return err;
  24887. +
  24888. +outunlock:
  24889. + __raw_write_seqcount_end(&devnet_rename_seq);
  24890. + mutex_unlock(&devnet_rename_mutex);
  24891. + return err;
  24892. }
  24893. /**
  24894. @@ -2214,6 +2220,7 @@
  24895. sd->output_queue_tailp = &q->next_sched;
  24896. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  24897. local_irq_restore(flags);
  24898. + preempt_check_resched_rt();
  24899. }
  24900. void __netif_schedule(struct Qdisc *q)
  24901. @@ -2295,6 +2302,7 @@
  24902. __this_cpu_write(softnet_data.completion_queue, skb);
  24903. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  24904. local_irq_restore(flags);
  24905. + preempt_check_resched_rt();
  24906. }
  24907. EXPORT_SYMBOL(__dev_kfree_skb_irq);
  24908. @@ -3365,6 +3373,7 @@
  24909. rps_unlock(sd);
  24910. local_irq_restore(flags);
  24911. + preempt_check_resched_rt();
  24912. atomic_long_inc(&skb->dev->rx_dropped);
  24913. kfree_skb(skb);
  24914. @@ -3383,7 +3392,7 @@
  24915. struct rps_dev_flow voidflow, *rflow = &voidflow;
  24916. int cpu;
  24917. - preempt_disable();
  24918. + migrate_disable();
  24919. rcu_read_lock();
  24920. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  24921. @@ -3393,13 +3402,13 @@
  24922. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  24923. rcu_read_unlock();
  24924. - preempt_enable();
  24925. + migrate_enable();
  24926. } else
  24927. #endif
  24928. {
  24929. unsigned int qtail;
  24930. - ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
  24931. - put_cpu();
  24932. + ret = enqueue_to_backlog(skb, get_cpu_light(), &qtail);
  24933. + put_cpu_light();
  24934. }
  24935. return ret;
  24936. }
  24937. @@ -3433,16 +3442,44 @@
  24938. trace_netif_rx_ni_entry(skb);
  24939. - preempt_disable();
  24940. + local_bh_disable();
  24941. err = netif_rx_internal(skb);
  24942. - if (local_softirq_pending())
  24943. - do_softirq();
  24944. - preempt_enable();
  24945. + local_bh_enable();
  24946. return err;
  24947. }
  24948. EXPORT_SYMBOL(netif_rx_ni);
  24949. +#ifdef CONFIG_PREEMPT_RT_FULL
  24950. +/*
  24951. + * RT runs ksoftirqd as a real time thread and the root_lock is a
  24952. + * "sleeping spinlock". If the trylock fails then we can go into an
  24953. + * infinite loop when ksoftirqd preempted the task which actually
  24954. + * holds the lock, because we requeue q and raise NET_TX softirq
  24955. + * causing ksoftirqd to loop forever.
  24956. + *
  24957. + * It's safe to use spin_lock on RT here as softirqs run in thread
  24958. + * context and cannot deadlock against the thread which is holding
  24959. + * root_lock.
  24960. + *
  24961. + * On !RT the trylock might fail, but there we bail out from the
  24962. + * softirq loop after 10 attempts which we can't do on RT. And the
  24963. + * task holding root_lock cannot be preempted, so the only downside of
  24964. + * that trylock is that we need 10 loops to decide that we should have
  24965. + * given up in the first one :)
  24966. + */
  24967. +static inline int take_root_lock(spinlock_t *lock)
  24968. +{
  24969. + spin_lock(lock);
  24970. + return 1;
  24971. +}
  24972. +#else
  24973. +static inline int take_root_lock(spinlock_t *lock)
  24974. +{
  24975. + return spin_trylock(lock);
  24976. +}
  24977. +#endif
  24978. +
  24979. static void net_tx_action(struct softirq_action *h)
  24980. {
  24981. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  24982. @@ -3484,7 +3521,7 @@
  24983. head = head->next_sched;
  24984. root_lock = qdisc_lock(q);
  24985. - if (spin_trylock(root_lock)) {
  24986. + if (take_root_lock(root_lock)) {
  24987. smp_mb__before_atomic();
  24988. clear_bit(__QDISC_STATE_SCHED,
  24989. &q->state);
  24990. @@ -3881,7 +3918,7 @@
  24991. skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
  24992. if (skb->dev == dev) {
  24993. __skb_unlink(skb, &sd->input_pkt_queue);
  24994. - kfree_skb(skb);
  24995. + __skb_queue_tail(&sd->tofree_queue, skb);
  24996. input_queue_head_incr(sd);
  24997. }
  24998. }
  24999. @@ -3890,10 +3927,13 @@
  25000. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  25001. if (skb->dev == dev) {
  25002. __skb_unlink(skb, &sd->process_queue);
  25003. - kfree_skb(skb);
  25004. + __skb_queue_tail(&sd->tofree_queue, skb);
  25005. input_queue_head_incr(sd);
  25006. }
  25007. }
  25008. +
  25009. + if (!skb_queue_empty(&sd->tofree_queue))
  25010. + raise_softirq_irqoff(NET_RX_SOFTIRQ);
  25011. }
  25012. static int napi_gro_complete(struct sk_buff *skb)
  25013. @@ -4344,6 +4384,7 @@
  25014. sd->rps_ipi_list = NULL;
  25015. local_irq_enable();
  25016. + preempt_check_resched_rt();
  25017. /* Send pending IPI's to kick RPS processing on remote cpus. */
  25018. while (remsd) {
  25019. @@ -4357,6 +4398,7 @@
  25020. } else
  25021. #endif
  25022. local_irq_enable();
  25023. + preempt_check_resched_rt();
  25024. }
  25025. static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
  25026. @@ -4438,6 +4480,7 @@
  25027. local_irq_save(flags);
  25028. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  25029. local_irq_restore(flags);
  25030. + preempt_check_resched_rt();
  25031. }
  25032. EXPORT_SYMBOL(__napi_schedule);
  25033. @@ -6926,7 +6969,7 @@
  25034. void synchronize_net(void)
  25035. {
  25036. might_sleep();
  25037. - if (rtnl_is_locked())
  25038. + if (rtnl_is_locked() && !IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  25039. synchronize_rcu_expedited();
  25040. else
  25041. synchronize_rcu();
  25042. @@ -7167,16 +7210,20 @@
  25043. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  25044. local_irq_enable();
  25045. + preempt_check_resched_rt();
  25046. /* Process offline CPU's input_pkt_queue */
  25047. while ((skb = __skb_dequeue(&oldsd->process_queue))) {
  25048. netif_rx_ni(skb);
  25049. input_queue_head_incr(oldsd);
  25050. }
  25051. - while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
  25052. + while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
  25053. netif_rx_ni(skb);
  25054. input_queue_head_incr(oldsd);
  25055. }
  25056. + while ((skb = __skb_dequeue(&oldsd->tofree_queue))) {
  25057. + kfree_skb(skb);
  25058. + }
  25059. return NOTIFY_OK;
  25060. }
  25061. @@ -7478,8 +7525,9 @@
  25062. for_each_possible_cpu(i) {
  25063. struct softnet_data *sd = &per_cpu(softnet_data, i);
  25064. - skb_queue_head_init(&sd->input_pkt_queue);
  25065. - skb_queue_head_init(&sd->process_queue);
  25066. + skb_queue_head_init_raw(&sd->input_pkt_queue);
  25067. + skb_queue_head_init_raw(&sd->process_queue);
  25068. + skb_queue_head_init_raw(&sd->tofree_queue);
  25069. INIT_LIST_HEAD(&sd->poll_list);
  25070. sd->output_queue_tailp = &sd->output_queue;
  25071. #ifdef CONFIG_RPS
  25072. diff -Nur linux-4.1.13.orig/net/core/skbuff.c linux-4.1.13/net/core/skbuff.c
  25073. --- linux-4.1.13.orig/net/core/skbuff.c 2015-11-09 23:34:10.000000000 +0100
  25074. +++ linux-4.1.13/net/core/skbuff.c 2015-11-29 09:23:09.669610195 +0100
  25075. @@ -63,6 +63,7 @@
  25076. #include <linux/errqueue.h>
  25077. #include <linux/prefetch.h>
  25078. #include <linux/if_vlan.h>
  25079. +#include <linux/locallock.h>
  25080. #include <net/protocol.h>
  25081. #include <net/dst.h>
  25082. @@ -356,6 +357,7 @@
  25083. };
  25084. static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
  25085. static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache);
  25086. +static DEFINE_LOCAL_IRQ_LOCK(netdev_alloc_lock);
  25087. static struct page *__page_frag_refill(struct netdev_alloc_cache *nc,
  25088. gfp_t gfp_mask)
  25089. @@ -433,9 +435,9 @@
  25090. unsigned long flags;
  25091. void *data;
  25092. - local_irq_save(flags);
  25093. + local_lock_irqsave(netdev_alloc_lock, flags);
  25094. data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask);
  25095. - local_irq_restore(flags);
  25096. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  25097. return data;
  25098. }
  25099. diff -Nur linux-4.1.13.orig/net/core/sock.c linux-4.1.13/net/core/sock.c
  25100. --- linux-4.1.13.orig/net/core/sock.c 2015-11-09 23:34:10.000000000 +0100
  25101. +++ linux-4.1.13/net/core/sock.c 2015-11-29 09:23:09.669610195 +0100
  25102. @@ -2370,12 +2370,11 @@
  25103. if (sk->sk_lock.owned)
  25104. __lock_sock(sk);
  25105. sk->sk_lock.owned = 1;
  25106. - spin_unlock(&sk->sk_lock.slock);
  25107. + spin_unlock_bh(&sk->sk_lock.slock);
  25108. /*
  25109. * The sk_lock has mutex_lock() semantics here:
  25110. */
  25111. mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
  25112. - local_bh_enable();
  25113. }
  25114. EXPORT_SYMBOL(lock_sock_nested);
  25115. diff -Nur linux-4.1.13.orig/net/ipv4/icmp.c linux-4.1.13/net/ipv4/icmp.c
  25116. --- linux-4.1.13.orig/net/ipv4/icmp.c 2015-11-09 23:34:10.000000000 +0100
  25117. +++ linux-4.1.13/net/ipv4/icmp.c 2015-11-29 09:23:09.669610195 +0100
  25118. @@ -69,6 +69,7 @@
  25119. #include <linux/jiffies.h>
  25120. #include <linux/kernel.h>
  25121. #include <linux/fcntl.h>
  25122. +#include <linux/sysrq.h>
  25123. #include <linux/socket.h>
  25124. #include <linux/in.h>
  25125. #include <linux/inet.h>
  25126. @@ -867,6 +868,30 @@
  25127. }
  25128. /*
  25129. + * 32bit and 64bit have different timestamp length, so we check for
  25130. + * the cookie at offset 20 and verify it is repeated at offset 50
  25131. + */
  25132. +#define CO_POS0 20
  25133. +#define CO_POS1 50
  25134. +#define CO_SIZE sizeof(int)
  25135. +#define ICMP_SYSRQ_SIZE 57
  25136. +
  25137. +/*
  25138. + * We got a ICMP_SYSRQ_SIZE sized ping request. Check for the cookie
  25139. + * pattern and if it matches send the next byte as a trigger to sysrq.
  25140. + */
  25141. +static void icmp_check_sysrq(struct net *net, struct sk_buff *skb)
  25142. +{
  25143. + int cookie = htonl(net->ipv4.sysctl_icmp_echo_sysrq);
  25144. + char *p = skb->data;
  25145. +
  25146. + if (!memcmp(&cookie, p + CO_POS0, CO_SIZE) &&
  25147. + !memcmp(&cookie, p + CO_POS1, CO_SIZE) &&
  25148. + p[CO_POS0 + CO_SIZE] == p[CO_POS1 + CO_SIZE])
  25149. + handle_sysrq(p[CO_POS0 + CO_SIZE]);
  25150. +}
  25151. +
  25152. +/*
  25153. * Handle ICMP_ECHO ("ping") requests.
  25154. *
  25155. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  25156. @@ -893,6 +918,11 @@
  25157. icmp_param.data_len = skb->len;
  25158. icmp_param.head_len = sizeof(struct icmphdr);
  25159. icmp_reply(&icmp_param, skb);
  25160. +
  25161. + if (skb->len == ICMP_SYSRQ_SIZE &&
  25162. + net->ipv4.sysctl_icmp_echo_sysrq) {
  25163. + icmp_check_sysrq(net, skb);
  25164. + }
  25165. }
  25166. /* should there be an ICMP stat for ignored echos? */
  25167. return true;
  25168. diff -Nur linux-4.1.13.orig/net/ipv4/sysctl_net_ipv4.c linux-4.1.13/net/ipv4/sysctl_net_ipv4.c
  25169. --- linux-4.1.13.orig/net/ipv4/sysctl_net_ipv4.c 2015-11-09 23:34:10.000000000 +0100
  25170. +++ linux-4.1.13/net/ipv4/sysctl_net_ipv4.c 2015-11-29 09:23:09.669610195 +0100
  25171. @@ -779,6 +779,13 @@
  25172. .proc_handler = proc_dointvec
  25173. },
  25174. {
  25175. + .procname = "icmp_echo_sysrq",
  25176. + .data = &init_net.ipv4.sysctl_icmp_echo_sysrq,
  25177. + .maxlen = sizeof(int),
  25178. + .mode = 0644,
  25179. + .proc_handler = proc_dointvec
  25180. + },
  25181. + {
  25182. .procname = "icmp_ignore_bogus_error_responses",
  25183. .data = &init_net.ipv4.sysctl_icmp_ignore_bogus_error_responses,
  25184. .maxlen = sizeof(int),
  25185. diff -Nur linux-4.1.13.orig/net/mac80211/rx.c linux-4.1.13/net/mac80211/rx.c
  25186. --- linux-4.1.13.orig/net/mac80211/rx.c 2015-11-09 23:34:10.000000000 +0100
  25187. +++ linux-4.1.13/net/mac80211/rx.c 2015-11-29 09:23:09.669610195 +0100
  25188. @@ -3554,7 +3554,7 @@
  25189. struct ieee80211_supported_band *sband;
  25190. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  25191. - WARN_ON_ONCE(softirq_count() == 0);
  25192. + WARN_ON_ONCE_NONRT(softirq_count() == 0);
  25193. if (WARN_ON(status->band >= IEEE80211_NUM_BANDS))
  25194. goto drop;
  25195. diff -Nur linux-4.1.13.orig/net/netfilter/core.c linux-4.1.13/net/netfilter/core.c
  25196. --- linux-4.1.13.orig/net/netfilter/core.c 2015-11-09 23:34:10.000000000 +0100
  25197. +++ linux-4.1.13/net/netfilter/core.c 2015-11-29 09:23:09.669610195 +0100
  25198. @@ -22,11 +22,17 @@
  25199. #include <linux/proc_fs.h>
  25200. #include <linux/mutex.h>
  25201. #include <linux/slab.h>
  25202. +#include <linux/locallock.h>
  25203. #include <net/net_namespace.h>
  25204. #include <net/sock.h>
  25205. #include "nf_internals.h"
  25206. +#ifdef CONFIG_PREEMPT_RT_BASE
  25207. +DEFINE_LOCAL_IRQ_LOCK(xt_write_lock);
  25208. +EXPORT_PER_CPU_SYMBOL(xt_write_lock);
  25209. +#endif
  25210. +
  25211. static DEFINE_MUTEX(afinfo_mutex);
  25212. const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
  25213. diff -Nur linux-4.1.13.orig/net/packet/af_packet.c linux-4.1.13/net/packet/af_packet.c
  25214. --- linux-4.1.13.orig/net/packet/af_packet.c 2015-11-09 23:34:10.000000000 +0100
  25215. +++ linux-4.1.13/net/packet/af_packet.c 2015-11-29 09:23:09.669610195 +0100
  25216. @@ -63,6 +63,7 @@
  25217. #include <linux/if_packet.h>
  25218. #include <linux/wireless.h>
  25219. #include <linux/kernel.h>
  25220. +#include <linux/delay.h>
  25221. #include <linux/kmod.h>
  25222. #include <linux/slab.h>
  25223. #include <linux/vmalloc.h>
  25224. @@ -698,7 +699,7 @@
  25225. if (BLOCK_NUM_PKTS(pbd)) {
  25226. while (atomic_read(&pkc->blk_fill_in_prog)) {
  25227. /* Waiting for skb_copy_bits to finish... */
  25228. - cpu_relax();
  25229. + cpu_chill();
  25230. }
  25231. }
  25232. @@ -960,7 +961,7 @@
  25233. if (!(status & TP_STATUS_BLK_TMO)) {
  25234. while (atomic_read(&pkc->blk_fill_in_prog)) {
  25235. /* Waiting for skb_copy_bits to finish... */
  25236. - cpu_relax();
  25237. + cpu_chill();
  25238. }
  25239. }
  25240. prb_close_block(pkc, pbd, po, status);
  25241. diff -Nur linux-4.1.13.orig/net/rds/ib_rdma.c linux-4.1.13/net/rds/ib_rdma.c
  25242. --- linux-4.1.13.orig/net/rds/ib_rdma.c 2015-11-09 23:34:10.000000000 +0100
  25243. +++ linux-4.1.13/net/rds/ib_rdma.c 2015-11-29 09:23:09.669610195 +0100
  25244. @@ -34,6 +34,7 @@
  25245. #include <linux/slab.h>
  25246. #include <linux/rculist.h>
  25247. #include <linux/llist.h>
  25248. +#include <linux/delay.h>
  25249. #include "rds.h"
  25250. #include "ib.h"
  25251. @@ -286,7 +287,7 @@
  25252. for_each_online_cpu(cpu) {
  25253. flag = &per_cpu(clean_list_grace, cpu);
  25254. while (test_bit(CLEAN_LIST_BUSY_BIT, flag))
  25255. - cpu_relax();
  25256. + cpu_chill();
  25257. }
  25258. }
  25259. diff -Nur linux-4.1.13.orig/net/sched/sch_generic.c linux-4.1.13/net/sched/sch_generic.c
  25260. --- linux-4.1.13.orig/net/sched/sch_generic.c 2015-11-09 23:34:10.000000000 +0100
  25261. +++ linux-4.1.13/net/sched/sch_generic.c 2015-11-29 09:23:09.669610195 +0100
  25262. @@ -894,7 +894,7 @@
  25263. /* Wait for outstanding qdisc_run calls. */
  25264. list_for_each_entry(dev, head, close_list)
  25265. while (some_qdisc_is_busy(dev))
  25266. - yield();
  25267. + msleep(1);
  25268. }
  25269. void dev_deactivate(struct net_device *dev)
  25270. diff -Nur linux-4.1.13.orig/net/sunrpc/svc_xprt.c linux-4.1.13/net/sunrpc/svc_xprt.c
  25271. --- linux-4.1.13.orig/net/sunrpc/svc_xprt.c 2015-11-09 23:34:10.000000000 +0100
  25272. +++ linux-4.1.13/net/sunrpc/svc_xprt.c 2015-11-29 09:23:09.669610195 +0100
  25273. @@ -341,7 +341,7 @@
  25274. goto out;
  25275. }
  25276. - cpu = get_cpu();
  25277. + cpu = get_cpu_light();
  25278. pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
  25279. atomic_long_inc(&pool->sp_stats.packets);
  25280. @@ -377,7 +377,7 @@
  25281. atomic_long_inc(&pool->sp_stats.threads_woken);
  25282. wake_up_process(rqstp->rq_task);
  25283. - put_cpu();
  25284. + put_cpu_light();
  25285. goto out;
  25286. }
  25287. rcu_read_unlock();
  25288. @@ -398,7 +398,7 @@
  25289. goto redo_search;
  25290. }
  25291. rqstp = NULL;
  25292. - put_cpu();
  25293. + put_cpu_light();
  25294. out:
  25295. trace_svc_xprt_do_enqueue(xprt, rqstp);
  25296. }
  25297. diff -Nur linux-4.1.13.orig/scripts/mkcompile_h linux-4.1.13/scripts/mkcompile_h
  25298. --- linux-4.1.13.orig/scripts/mkcompile_h 2015-11-09 23:34:10.000000000 +0100
  25299. +++ linux-4.1.13/scripts/mkcompile_h 2015-11-29 09:23:09.669610195 +0100
  25300. @@ -4,7 +4,8 @@
  25301. ARCH=$2
  25302. SMP=$3
  25303. PREEMPT=$4
  25304. -CC=$5
  25305. +RT=$5
  25306. +CC=$6
  25307. vecho() { [ "${quiet}" = "silent_" ] || echo "$@" ; }
  25308. @@ -57,6 +58,7 @@
  25309. CONFIG_FLAGS=""
  25310. if [ -n "$SMP" ] ; then CONFIG_FLAGS="SMP"; fi
  25311. if [ -n "$PREEMPT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS PREEMPT"; fi
  25312. +if [ -n "$RT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS RT"; fi
  25313. UTS_VERSION="$UTS_VERSION $CONFIG_FLAGS $TIMESTAMP"
  25314. # Truncate to maximum length
  25315. diff -Nur linux-4.1.13.orig/sound/core/pcm_native.c linux-4.1.13/sound/core/pcm_native.c
  25316. --- linux-4.1.13.orig/sound/core/pcm_native.c 2015-11-09 23:34:10.000000000 +0100
  25317. +++ linux-4.1.13/sound/core/pcm_native.c 2015-11-29 09:23:09.669610195 +0100
  25318. @@ -123,7 +123,7 @@
  25319. void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
  25320. {
  25321. if (!substream->pcm->nonatomic)
  25322. - local_irq_disable();
  25323. + local_irq_disable_nort();
  25324. snd_pcm_stream_lock(substream);
  25325. }
  25326. EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
  25327. @@ -138,7 +138,7 @@
  25328. {
  25329. snd_pcm_stream_unlock(substream);
  25330. if (!substream->pcm->nonatomic)
  25331. - local_irq_enable();
  25332. + local_irq_enable_nort();
  25333. }
  25334. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
  25335. @@ -146,7 +146,7 @@
  25336. {
  25337. unsigned long flags = 0;
  25338. if (!substream->pcm->nonatomic)
  25339. - local_irq_save(flags);
  25340. + local_irq_save_nort(flags);
  25341. snd_pcm_stream_lock(substream);
  25342. return flags;
  25343. }
  25344. @@ -164,7 +164,7 @@
  25345. {
  25346. snd_pcm_stream_unlock(substream);
  25347. if (!substream->pcm->nonatomic)
  25348. - local_irq_restore(flags);
  25349. + local_irq_restore_nort(flags);
  25350. }
  25351. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
  25352. diff -Nur linux-4.1.13.orig/sound/soc/intel/atom/sst/sst.c linux-4.1.13/sound/soc/intel/atom/sst/sst.c
  25353. --- linux-4.1.13.orig/sound/soc/intel/atom/sst/sst.c 2015-11-09 23:34:10.000000000 +0100
  25354. +++ linux-4.1.13/sound/soc/intel/atom/sst/sst.c 2015-11-29 09:23:09.673609931 +0100
  25355. @@ -368,8 +368,8 @@
  25356. * initialize by FW or driver when firmware is loaded
  25357. */
  25358. spin_lock_irqsave(&ctx->ipc_spin_lock, irq_flags);
  25359. - sst_shim_write64(shim, SST_IMRX, shim_regs->imrx),
  25360. - sst_shim_write64(shim, SST_CSR, shim_regs->csr),
  25361. + sst_shim_write64(shim, SST_IMRX, shim_regs->imrx);
  25362. + sst_shim_write64(shim, SST_CSR, shim_regs->csr);
  25363. spin_unlock_irqrestore(&ctx->ipc_spin_lock, irq_flags);
  25364. }
  25365. diff -Nur linux-4.1.13.orig/virt/kvm/async_pf.c linux-4.1.13/virt/kvm/async_pf.c
  25366. --- linux-4.1.13.orig/virt/kvm/async_pf.c 2015-11-09 23:34:10.000000000 +0100
  25367. +++ linux-4.1.13/virt/kvm/async_pf.c 2015-11-29 09:23:09.673609931 +0100
  25368. @@ -94,8 +94,8 @@
  25369. trace_kvm_async_pf_completed(addr, gva);
  25370. - if (waitqueue_active(&vcpu->wq))
  25371. - wake_up_interruptible(&vcpu->wq);
  25372. + if (swaitqueue_active(&vcpu->wq))
  25373. + swait_wake_interruptible(&vcpu->wq);
  25374. mmput(mm);
  25375. kvm_put_kvm(vcpu->kvm);
  25376. diff -Nur linux-4.1.13.orig/virt/kvm/kvm_main.c linux-4.1.13/virt/kvm/kvm_main.c
  25377. --- linux-4.1.13.orig/virt/kvm/kvm_main.c 2015-11-09 23:34:10.000000000 +0100
  25378. +++ linux-4.1.13/virt/kvm/kvm_main.c 2015-11-29 09:23:09.701608072 +0100
  25379. @@ -218,7 +218,7 @@
  25380. vcpu->kvm = kvm;
  25381. vcpu->vcpu_id = id;
  25382. vcpu->pid = NULL;
  25383. - init_waitqueue_head(&vcpu->wq);
  25384. + init_swait_head(&vcpu->wq);
  25385. kvm_async_pf_vcpu_init(vcpu);
  25386. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  25387. @@ -1779,7 +1779,7 @@
  25388. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  25389. {
  25390. ktime_t start, cur;
  25391. - DEFINE_WAIT(wait);
  25392. + DEFINE_SWAITER(wait);
  25393. bool waited = false;
  25394. start = cur = ktime_get();
  25395. @@ -1800,7 +1800,7 @@
  25396. }
  25397. for (;;) {
  25398. - prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  25399. + swait_prepare(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  25400. if (kvm_vcpu_check_block(vcpu) < 0)
  25401. break;
  25402. @@ -1809,7 +1809,7 @@
  25403. schedule();
  25404. }
  25405. - finish_wait(&vcpu->wq, &wait);
  25406. + swait_finish(&vcpu->wq, &wait);
  25407. cur = ktime_get();
  25408. out:
  25409. @@ -1825,11 +1825,11 @@
  25410. {
  25411. int me;
  25412. int cpu = vcpu->cpu;
  25413. - wait_queue_head_t *wqp;
  25414. + struct swait_head *wqp;
  25415. wqp = kvm_arch_vcpu_wq(vcpu);
  25416. - if (waitqueue_active(wqp)) {
  25417. - wake_up_interruptible(wqp);
  25418. + if (swaitqueue_active(wqp)) {
  25419. + swait_wake_interruptible(wqp);
  25420. ++vcpu->stat.halt_wakeup;
  25421. }
  25422. @@ -1930,7 +1930,7 @@
  25423. continue;
  25424. if (vcpu == me)
  25425. continue;
  25426. - if (waitqueue_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  25427. + if (swaitqueue_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  25428. continue;
  25429. if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
  25430. continue;