realtime.patch 818 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376
  1. diff -Nur linux-4.1.20.orig/Documentation/hwlat_detector.txt linux-4.1.20/Documentation/hwlat_detector.txt
  2. --- linux-4.1.20.orig/Documentation/hwlat_detector.txt 1970-01-01 01:00:00.000000000 +0100
  3. +++ linux-4.1.20/Documentation/hwlat_detector.txt 2016-03-21 20:18:28.000000000 +0100
  4. @@ -0,0 +1,64 @@
  5. +Introduction:
  6. +-------------
  7. +
  8. +The module hwlat_detector is a special purpose kernel module that is used to
  9. +detect large system latencies induced by the behavior of certain underlying
  10. +hardware or firmware, independent of Linux itself. The code was developed
  11. +originally to detect SMIs (System Management Interrupts) on x86 systems,
  12. +however there is nothing x86 specific about this patchset. It was
  13. +originally written for use by the "RT" patch since the Real Time
  14. +kernel is highly latency sensitive.
  15. +
  16. +SMIs are usually not serviced by the Linux kernel, which typically does not
  17. +even know that they are occuring. SMIs are instead are set up by BIOS code
  18. +and are serviced by BIOS code, usually for "critical" events such as
  19. +management of thermal sensors and fans. Sometimes though, SMIs are used for
  20. +other tasks and those tasks can spend an inordinate amount of time in the
  21. +handler (sometimes measured in milliseconds). Obviously this is a problem if
  22. +you are trying to keep event service latencies down in the microsecond range.
  23. +
  24. +The hardware latency detector works by hogging all of the cpus for configurable
  25. +amounts of time (by calling stop_machine()), polling the CPU Time Stamp Counter
  26. +for some period, then looking for gaps in the TSC data. Any gap indicates a
  27. +time when the polling was interrupted and since the machine is stopped and
  28. +interrupts turned off the only thing that could do that would be an SMI.
  29. +
  30. +Note that the SMI detector should *NEVER* be used in a production environment.
  31. +It is intended to be run manually to determine if the hardware platform has a
  32. +problem with long system firmware service routines.
  33. +
  34. +Usage:
  35. +------
  36. +
  37. +Loading the module hwlat_detector passing the parameter "enabled=1" (or by
  38. +setting the "enable" entry in "hwlat_detector" debugfs toggled on) is the only
  39. +step required to start the hwlat_detector. It is possible to redefine the
  40. +threshold in microseconds (us) above which latency spikes will be taken
  41. +into account (parameter "threshold=").
  42. +
  43. +Example:
  44. +
  45. + # modprobe hwlat_detector enabled=1 threshold=100
  46. +
  47. +After the module is loaded, it creates a directory named "hwlat_detector" under
  48. +the debugfs mountpoint, "/debug/hwlat_detector" for this text. It is necessary
  49. +to have debugfs mounted, which might be on /sys/debug on your system.
  50. +
  51. +The /debug/hwlat_detector interface contains the following files:
  52. +
  53. +count - number of latency spikes observed since last reset
  54. +enable - a global enable/disable toggle (0/1), resets count
  55. +max - maximum hardware latency actually observed (usecs)
  56. +sample - a pipe from which to read current raw sample data
  57. + in the format <timestamp> <latency observed usecs>
  58. + (can be opened O_NONBLOCK for a single sample)
  59. +threshold - minimum latency value to be considered (usecs)
  60. +width - time period to sample with CPUs held (usecs)
  61. + must be less than the total window size (enforced)
  62. +window - total period of sampling, width being inside (usecs)
  63. +
  64. +By default we will set width to 500,000 and window to 1,000,000, meaning that
  65. +we will sample every 1,000,000 usecs (1s) for 500,000 usecs (0.5s). If we
  66. +observe any latencies that exceed the threshold (initially 100 usecs),
  67. +then we write to a global sample ring buffer of 8K samples, which is
  68. +consumed by reading from the "sample" (pipe) debugfs file interface.
  69. diff -Nur linux-4.1.20.orig/Documentation/sysrq.txt linux-4.1.20/Documentation/sysrq.txt
  70. --- linux-4.1.20.orig/Documentation/sysrq.txt 2016-03-17 19:11:03.000000000 +0100
  71. +++ linux-4.1.20/Documentation/sysrq.txt 2016-03-21 20:18:28.000000000 +0100
  72. @@ -59,10 +59,17 @@
  73. On other - If you know of the key combos for other architectures, please
  74. let me know so I can add them to this section.
  75. -On all - write a character to /proc/sysrq-trigger. e.g.:
  76. -
  77. +On all - write a character to /proc/sysrq-trigger, e.g.:
  78. echo t > /proc/sysrq-trigger
  79. +On all - Enable network SysRq by writing a cookie to icmp_echo_sysrq, e.g.
  80. + echo 0x01020304 >/proc/sys/net/ipv4/icmp_echo_sysrq
  81. + Send an ICMP echo request with this pattern plus the particular
  82. + SysRq command key. Example:
  83. + # ping -c1 -s57 -p0102030468
  84. + will trigger the SysRq-H (help) command.
  85. +
  86. +
  87. * What are the 'command' keys?
  88. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  89. 'b' - Will immediately reboot the system without syncing or unmounting
  90. diff -Nur linux-4.1.20.orig/Documentation/trace/histograms.txt linux-4.1.20/Documentation/trace/histograms.txt
  91. --- linux-4.1.20.orig/Documentation/trace/histograms.txt 1970-01-01 01:00:00.000000000 +0100
  92. +++ linux-4.1.20/Documentation/trace/histograms.txt 2016-03-21 20:18:28.000000000 +0100
  93. @@ -0,0 +1,186 @@
  94. + Using the Linux Kernel Latency Histograms
  95. +
  96. +
  97. +This document gives a short explanation how to enable, configure and use
  98. +latency histograms. Latency histograms are primarily relevant in the
  99. +context of real-time enabled kernels (CONFIG_PREEMPT/CONFIG_PREEMPT_RT)
  100. +and are used in the quality management of the Linux real-time
  101. +capabilities.
  102. +
  103. +
  104. +* Purpose of latency histograms
  105. +
  106. +A latency histogram continuously accumulates the frequencies of latency
  107. +data. There are two types of histograms
  108. +- potential sources of latencies
  109. +- effective latencies
  110. +
  111. +
  112. +* Potential sources of latencies
  113. +
  114. +Potential sources of latencies are code segments where interrupts,
  115. +preemption or both are disabled (aka critical sections). To create
  116. +histograms of potential sources of latency, the kernel stores the time
  117. +stamp at the start of a critical section, determines the time elapsed
  118. +when the end of the section is reached, and increments the frequency
  119. +counter of that latency value - irrespective of whether any concurrently
  120. +running process is affected by latency or not.
  121. +- Configuration items (in the Kernel hacking/Tracers submenu)
  122. + CONFIG_INTERRUPT_OFF_LATENCY
  123. + CONFIG_PREEMPT_OFF_LATENCY
  124. +
  125. +
  126. +* Effective latencies
  127. +
  128. +Effective latencies are actually occuring during wakeup of a process. To
  129. +determine effective latencies, the kernel stores the time stamp when a
  130. +process is scheduled to be woken up, and determines the duration of the
  131. +wakeup time shortly before control is passed over to this process. Note
  132. +that the apparent latency in user space may be somewhat longer, since the
  133. +process may be interrupted after control is passed over to it but before
  134. +the execution in user space takes place. Simply measuring the interval
  135. +between enqueuing and wakeup may also not appropriate in cases when a
  136. +process is scheduled as a result of a timer expiration. The timer may have
  137. +missed its deadline, e.g. due to disabled interrupts, but this latency
  138. +would not be registered. Therefore, the offsets of missed timers are
  139. +recorded in a separate histogram. If both wakeup latency and missed timer
  140. +offsets are configured and enabled, a third histogram may be enabled that
  141. +records the overall latency as a sum of the timer latency, if any, and the
  142. +wakeup latency. This histogram is called "timerandwakeup".
  143. +- Configuration items (in the Kernel hacking/Tracers submenu)
  144. + CONFIG_WAKEUP_LATENCY
  145. + CONFIG_MISSED_TIMER_OFSETS
  146. +
  147. +
  148. +* Usage
  149. +
  150. +The interface to the administration of the latency histograms is located
  151. +in the debugfs file system. To mount it, either enter
  152. +
  153. +mount -t sysfs nodev /sys
  154. +mount -t debugfs nodev /sys/kernel/debug
  155. +
  156. +from shell command line level, or add
  157. +
  158. +nodev /sys sysfs defaults 0 0
  159. +nodev /sys/kernel/debug debugfs defaults 0 0
  160. +
  161. +to the file /etc/fstab. All latency histogram related files are then
  162. +available in the directory /sys/kernel/debug/tracing/latency_hist. A
  163. +particular histogram type is enabled by writing non-zero to the related
  164. +variable in the /sys/kernel/debug/tracing/latency_hist/enable directory.
  165. +Select "preemptirqsoff" for the histograms of potential sources of
  166. +latencies and "wakeup" for histograms of effective latencies etc. The
  167. +histogram data - one per CPU - are available in the files
  168. +
  169. +/sys/kernel/debug/tracing/latency_hist/preemptoff/CPUx
  170. +/sys/kernel/debug/tracing/latency_hist/irqsoff/CPUx
  171. +/sys/kernel/debug/tracing/latency_hist/preemptirqsoff/CPUx
  172. +/sys/kernel/debug/tracing/latency_hist/wakeup/CPUx
  173. +/sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio/CPUx
  174. +/sys/kernel/debug/tracing/latency_hist/missed_timer_offsets/CPUx
  175. +/sys/kernel/debug/tracing/latency_hist/timerandwakeup/CPUx
  176. +
  177. +The histograms are reset by writing non-zero to the file "reset" in a
  178. +particular latency directory. To reset all latency data, use
  179. +
  180. +#!/bin/sh
  181. +
  182. +TRACINGDIR=/sys/kernel/debug/tracing
  183. +HISTDIR=$TRACINGDIR/latency_hist
  184. +
  185. +if test -d $HISTDIR
  186. +then
  187. + cd $HISTDIR
  188. + for i in `find . | grep /reset$`
  189. + do
  190. + echo 1 >$i
  191. + done
  192. +fi
  193. +
  194. +
  195. +* Data format
  196. +
  197. +Latency data are stored with a resolution of one microsecond. The
  198. +maximum latency is 10,240 microseconds. The data are only valid, if the
  199. +overflow register is empty. Every output line contains the latency in
  200. +microseconds in the first row and the number of samples in the second
  201. +row. To display only lines with a positive latency count, use, for
  202. +example,
  203. +
  204. +grep -v " 0$" /sys/kernel/debug/tracing/latency_hist/preemptoff/CPU0
  205. +
  206. +#Minimum latency: 0 microseconds.
  207. +#Average latency: 0 microseconds.
  208. +#Maximum latency: 25 microseconds.
  209. +#Total samples: 3104770694
  210. +#There are 0 samples greater or equal than 10240 microseconds
  211. +#usecs samples
  212. + 0 2984486876
  213. + 1 49843506
  214. + 2 58219047
  215. + 3 5348126
  216. + 4 2187960
  217. + 5 3388262
  218. + 6 959289
  219. + 7 208294
  220. + 8 40420
  221. + 9 4485
  222. + 10 14918
  223. + 11 18340
  224. + 12 25052
  225. + 13 19455
  226. + 14 5602
  227. + 15 969
  228. + 16 47
  229. + 17 18
  230. + 18 14
  231. + 19 1
  232. + 20 3
  233. + 21 2
  234. + 22 5
  235. + 23 2
  236. + 25 1
  237. +
  238. +
  239. +* Wakeup latency of a selected process
  240. +
  241. +To only collect wakeup latency data of a particular process, write the
  242. +PID of the requested process to
  243. +
  244. +/sys/kernel/debug/tracing/latency_hist/wakeup/pid
  245. +
  246. +PIDs are not considered, if this variable is set to 0.
  247. +
  248. +
  249. +* Details of the process with the highest wakeup latency so far
  250. +
  251. +Selected data of the process that suffered from the highest wakeup
  252. +latency that occurred in a particular CPU are available in the file
  253. +
  254. +/sys/kernel/debug/tracing/latency_hist/wakeup/max_latency-CPUx.
  255. +
  256. +In addition, other relevant system data at the time when the
  257. +latency occurred are given.
  258. +
  259. +The format of the data is (all in one line):
  260. +<PID> <Priority> <Latency> (<Timeroffset>) <Command> \
  261. +<- <PID> <Priority> <Command> <Timestamp>
  262. +
  263. +The value of <Timeroffset> is only relevant in the combined timer
  264. +and wakeup latency recording. In the wakeup recording, it is
  265. +always 0, in the missed_timer_offsets recording, it is the same
  266. +as <Latency>.
  267. +
  268. +When retrospectively searching for the origin of a latency and
  269. +tracing was not enabled, it may be helpful to know the name and
  270. +some basic data of the task that (finally) was switching to the
  271. +late real-tlme task. In addition to the victim's data, also the
  272. +data of the possible culprit are therefore displayed after the
  273. +"<-" symbol.
  274. +
  275. +Finally, the timestamp of the time when the latency occurred
  276. +in <seconds>.<microseconds> after the most recent system boot
  277. +is provided.
  278. +
  279. +These data are also reset when the wakeup histogram is reset.
  280. diff -Nur linux-4.1.20.orig/arch/Kconfig linux-4.1.20/arch/Kconfig
  281. --- linux-4.1.20.orig/arch/Kconfig 2016-03-17 19:11:03.000000000 +0100
  282. +++ linux-4.1.20/arch/Kconfig 2016-03-21 20:18:28.000000000 +0100
  283. @@ -6,6 +6,7 @@
  284. tristate "OProfile system profiling"
  285. depends on PROFILING
  286. depends on HAVE_OPROFILE
  287. + depends on !PREEMPT_RT_FULL
  288. select RING_BUFFER
  289. select RING_BUFFER_ALLOW_SWAP
  290. help
  291. @@ -49,6 +50,7 @@
  292. config JUMP_LABEL
  293. bool "Optimize very unlikely/likely branches"
  294. depends on HAVE_ARCH_JUMP_LABEL
  295. + depends on (!INTERRUPT_OFF_HIST && !PREEMPT_OFF_HIST && !WAKEUP_LATENCY_HIST && !MISSED_TIMER_OFFSETS_HIST)
  296. help
  297. This option enables a transparent branch optimization that
  298. makes certain almost-always-true or almost-always-false branch
  299. diff -Nur linux-4.1.20.orig/arch/alpha/mm/fault.c linux-4.1.20/arch/alpha/mm/fault.c
  300. --- linux-4.1.20.orig/arch/alpha/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  301. +++ linux-4.1.20/arch/alpha/mm/fault.c 2016-03-21 20:18:28.000000000 +0100
  302. @@ -23,8 +23,7 @@
  303. #include <linux/smp.h>
  304. #include <linux/interrupt.h>
  305. #include <linux/module.h>
  306. -
  307. -#include <asm/uaccess.h>
  308. +#include <linux/uaccess.h>
  309. extern void die_if_kernel(char *,struct pt_regs *,long, unsigned long *);
  310. @@ -107,7 +106,7 @@
  311. /* If we're in an interrupt context, or have no user context,
  312. we must not take the fault. */
  313. - if (!mm || in_atomic())
  314. + if (!mm || faulthandler_disabled())
  315. goto no_context;
  316. #ifdef CONFIG_ALPHA_LARGE_VMALLOC
  317. diff -Nur linux-4.1.20.orig/arch/arc/include/asm/futex.h linux-4.1.20/arch/arc/include/asm/futex.h
  318. --- linux-4.1.20.orig/arch/arc/include/asm/futex.h 2016-03-17 19:11:03.000000000 +0100
  319. +++ linux-4.1.20/arch/arc/include/asm/futex.h 2016-03-21 20:18:28.000000000 +0100
  320. @@ -53,7 +53,7 @@
  321. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(int)))
  322. return -EFAULT;
  323. - pagefault_disable(); /* implies preempt_disable() */
  324. + pagefault_disable();
  325. switch (op) {
  326. case FUTEX_OP_SET:
  327. @@ -75,7 +75,7 @@
  328. ret = -ENOSYS;
  329. }
  330. - pagefault_enable(); /* subsumes preempt_enable() */
  331. + pagefault_enable();
  332. if (!ret) {
  333. switch (cmp) {
  334. @@ -104,7 +104,7 @@
  335. return ret;
  336. }
  337. -/* Compare-xchg with preemption disabled.
  338. +/* Compare-xchg with pagefaults disabled.
  339. * Notes:
  340. * -Best-Effort: Exchg happens only if compare succeeds.
  341. * If compare fails, returns; leaving retry/looping to upper layers
  342. @@ -121,7 +121,7 @@
  343. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(int)))
  344. return -EFAULT;
  345. - pagefault_disable(); /* implies preempt_disable() */
  346. + pagefault_disable();
  347. /* TBD : can use llock/scond */
  348. __asm__ __volatile__(
  349. @@ -142,7 +142,7 @@
  350. : "r"(oldval), "r"(newval), "r"(uaddr), "ir"(-EFAULT)
  351. : "cc", "memory");
  352. - pagefault_enable(); /* subsumes preempt_enable() */
  353. + pagefault_enable();
  354. *uval = val;
  355. return val;
  356. diff -Nur linux-4.1.20.orig/arch/arc/mm/fault.c linux-4.1.20/arch/arc/mm/fault.c
  357. --- linux-4.1.20.orig/arch/arc/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  358. +++ linux-4.1.20/arch/arc/mm/fault.c 2016-03-21 20:18:28.000000000 +0100
  359. @@ -86,7 +86,7 @@
  360. * If we're in an interrupt or have no user
  361. * context, we must not take the fault..
  362. */
  363. - if (in_atomic() || !mm)
  364. + if (faulthandler_disabled() || !mm)
  365. goto no_context;
  366. if (user_mode(regs))
  367. diff -Nur linux-4.1.20.orig/arch/arm/Kconfig linux-4.1.20/arch/arm/Kconfig
  368. --- linux-4.1.20.orig/arch/arm/Kconfig 2016-03-17 19:11:03.000000000 +0100
  369. +++ linux-4.1.20/arch/arm/Kconfig 2016-03-21 20:18:28.000000000 +0100
  370. @@ -31,7 +31,7 @@
  371. select HARDIRQS_SW_RESEND
  372. select HAVE_ARCH_AUDITSYSCALL if (AEABI && !OABI_COMPAT)
  373. select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6
  374. - select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL
  375. + select HAVE_ARCH_JUMP_LABEL if (!XIP_KERNEL && !PREEMPT_RT_BASE)
  376. select HAVE_ARCH_KGDB
  377. select HAVE_ARCH_SECCOMP_FILTER if (AEABI && !OABI_COMPAT)
  378. select HAVE_ARCH_TRACEHOOK
  379. @@ -66,6 +66,7 @@
  380. select HAVE_PERF_EVENTS
  381. select HAVE_PERF_REGS
  382. select HAVE_PERF_USER_STACK_DUMP
  383. + select HAVE_PREEMPT_LAZY
  384. select HAVE_RCU_TABLE_FREE if (SMP && ARM_LPAE)
  385. select HAVE_REGS_AND_STACK_ACCESS_API
  386. select HAVE_SYSCALL_TRACEPOINTS
  387. diff -Nur linux-4.1.20.orig/arch/arm/include/asm/cmpxchg.h linux-4.1.20/arch/arm/include/asm/cmpxchg.h
  388. --- linux-4.1.20.orig/arch/arm/include/asm/cmpxchg.h 2016-03-17 19:11:03.000000000 +0100
  389. +++ linux-4.1.20/arch/arm/include/asm/cmpxchg.h 2016-03-21 20:18:28.000000000 +0100
  390. @@ -129,6 +129,8 @@
  391. #else /* min ARCH >= ARMv6 */
  392. +#define __HAVE_ARCH_CMPXCHG 1
  393. +
  394. extern void __bad_cmpxchg(volatile void *ptr, int size);
  395. /*
  396. diff -Nur linux-4.1.20.orig/arch/arm/include/asm/futex.h linux-4.1.20/arch/arm/include/asm/futex.h
  397. --- linux-4.1.20.orig/arch/arm/include/asm/futex.h 2016-03-17 19:11:03.000000000 +0100
  398. +++ linux-4.1.20/arch/arm/include/asm/futex.h 2016-03-21 20:18:28.000000000 +0100
  399. @@ -93,6 +93,7 @@
  400. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  401. return -EFAULT;
  402. + preempt_disable();
  403. __asm__ __volatile__("@futex_atomic_cmpxchg_inatomic\n"
  404. "1: " TUSER(ldr) " %1, [%4]\n"
  405. " teq %1, %2\n"
  406. @@ -104,6 +105,8 @@
  407. : "cc", "memory");
  408. *uval = val;
  409. + preempt_enable();
  410. +
  411. return ret;
  412. }
  413. @@ -124,7 +127,10 @@
  414. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  415. return -EFAULT;
  416. - pagefault_disable(); /* implies preempt_disable() */
  417. +#ifndef CONFIG_SMP
  418. + preempt_disable();
  419. +#endif
  420. + pagefault_disable();
  421. switch (op) {
  422. case FUTEX_OP_SET:
  423. @@ -146,7 +152,10 @@
  424. ret = -ENOSYS;
  425. }
  426. - pagefault_enable(); /* subsumes preempt_enable() */
  427. + pagefault_enable();
  428. +#ifndef CONFIG_SMP
  429. + preempt_enable();
  430. +#endif
  431. if (!ret) {
  432. switch (cmp) {
  433. diff -Nur linux-4.1.20.orig/arch/arm/include/asm/switch_to.h linux-4.1.20/arch/arm/include/asm/switch_to.h
  434. --- linux-4.1.20.orig/arch/arm/include/asm/switch_to.h 2016-03-17 19:11:03.000000000 +0100
  435. +++ linux-4.1.20/arch/arm/include/asm/switch_to.h 2016-03-21 20:18:28.000000000 +0100
  436. @@ -3,6 +3,13 @@
  437. #include <linux/thread_info.h>
  438. +#if defined CONFIG_PREEMPT_RT_FULL && defined CONFIG_HIGHMEM
  439. +void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p);
  440. +#else
  441. +static inline void
  442. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  443. +#endif
  444. +
  445. /*
  446. * For v7 SMP cores running a preemptible kernel we may be pre-empted
  447. * during a TLB maintenance operation, so execute an inner-shareable dsb
  448. @@ -22,6 +29,7 @@
  449. #define switch_to(prev,next,last) \
  450. do { \
  451. + switch_kmaps(prev, next); \
  452. last = __switch_to(prev,task_thread_info(prev), task_thread_info(next)); \
  453. } while (0)
  454. diff -Nur linux-4.1.20.orig/arch/arm/include/asm/thread_info.h linux-4.1.20/arch/arm/include/asm/thread_info.h
  455. --- linux-4.1.20.orig/arch/arm/include/asm/thread_info.h 2016-03-17 19:11:03.000000000 +0100
  456. +++ linux-4.1.20/arch/arm/include/asm/thread_info.h 2016-03-21 20:18:28.000000000 +0100
  457. @@ -50,6 +50,7 @@
  458. struct thread_info {
  459. unsigned long flags; /* low level flags */
  460. int preempt_count; /* 0 => preemptable, <0 => bug */
  461. + int preempt_lazy_count; /* 0 => preemptable, <0 => bug */
  462. mm_segment_t addr_limit; /* address limit */
  463. struct task_struct *task; /* main task structure */
  464. __u32 cpu; /* cpu */
  465. @@ -147,6 +148,7 @@
  466. #define TIF_SIGPENDING 0
  467. #define TIF_NEED_RESCHED 1
  468. #define TIF_NOTIFY_RESUME 2 /* callback before returning to user */
  469. +#define TIF_NEED_RESCHED_LAZY 3
  470. #define TIF_UPROBE 7
  471. #define TIF_SYSCALL_TRACE 8
  472. #define TIF_SYSCALL_AUDIT 9
  473. @@ -160,6 +162,7 @@
  474. #define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
  475. #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
  476. #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
  477. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  478. #define _TIF_UPROBE (1 << TIF_UPROBE)
  479. #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
  480. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  481. diff -Nur linux-4.1.20.orig/arch/arm/kernel/asm-offsets.c linux-4.1.20/arch/arm/kernel/asm-offsets.c
  482. --- linux-4.1.20.orig/arch/arm/kernel/asm-offsets.c 2016-03-17 19:11:03.000000000 +0100
  483. +++ linux-4.1.20/arch/arm/kernel/asm-offsets.c 2016-03-21 20:18:28.000000000 +0100
  484. @@ -65,6 +65,7 @@
  485. BLANK();
  486. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  487. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  488. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  489. DEFINE(TI_ADDR_LIMIT, offsetof(struct thread_info, addr_limit));
  490. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  491. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  492. diff -Nur linux-4.1.20.orig/arch/arm/kernel/entry-armv.S linux-4.1.20/arch/arm/kernel/entry-armv.S
  493. --- linux-4.1.20.orig/arch/arm/kernel/entry-armv.S 2016-03-17 19:11:03.000000000 +0100
  494. +++ linux-4.1.20/arch/arm/kernel/entry-armv.S 2016-03-21 20:18:28.000000000 +0100
  495. @@ -208,11 +208,18 @@
  496. #ifdef CONFIG_PREEMPT
  497. get_thread_info tsk
  498. ldr r8, [tsk, #TI_PREEMPT] @ get preempt count
  499. - ldr r0, [tsk, #TI_FLAGS] @ get flags
  500. teq r8, #0 @ if preempt count != 0
  501. + bne 1f @ return from exeption
  502. + ldr r0, [tsk, #TI_FLAGS] @ get flags
  503. + tst r0, #_TIF_NEED_RESCHED @ if NEED_RESCHED is set
  504. + blne svc_preempt @ preempt!
  505. +
  506. + ldr r8, [tsk, #TI_PREEMPT_LAZY] @ get preempt lazy count
  507. + teq r8, #0 @ if preempt lazy count != 0
  508. movne r0, #0 @ force flags to 0
  509. - tst r0, #_TIF_NEED_RESCHED
  510. + tst r0, #_TIF_NEED_RESCHED_LAZY
  511. blne svc_preempt
  512. +1:
  513. #endif
  514. svc_exit r5, irq = 1 @ return from exception
  515. @@ -227,6 +234,8 @@
  516. 1: bl preempt_schedule_irq @ irq en/disable is done inside
  517. ldr r0, [tsk, #TI_FLAGS] @ get new tasks TI_FLAGS
  518. tst r0, #_TIF_NEED_RESCHED
  519. + bne 1b
  520. + tst r0, #_TIF_NEED_RESCHED_LAZY
  521. reteq r8 @ go again
  522. b 1b
  523. #endif
  524. diff -Nur linux-4.1.20.orig/arch/arm/kernel/process.c linux-4.1.20/arch/arm/kernel/process.c
  525. --- linux-4.1.20.orig/arch/arm/kernel/process.c 2016-03-17 19:11:03.000000000 +0100
  526. +++ linux-4.1.20/arch/arm/kernel/process.c 2016-03-21 20:18:28.000000000 +0100
  527. @@ -290,6 +290,30 @@
  528. }
  529. #ifdef CONFIG_MMU
  530. +/*
  531. + * CONFIG_SPLIT_PTLOCK_CPUS results in a page->ptl lock. If the lock is not
  532. + * initialized by pgtable_page_ctor() then a coredump of the vector page will
  533. + * fail.
  534. + */
  535. +static int __init vectors_user_mapping_init_page(void)
  536. +{
  537. + struct page *page;
  538. + unsigned long addr = 0xffff0000;
  539. + pgd_t *pgd;
  540. + pud_t *pud;
  541. + pmd_t *pmd;
  542. +
  543. + pgd = pgd_offset_k(addr);
  544. + pud = pud_offset(pgd, addr);
  545. + pmd = pmd_offset(pud, addr);
  546. + page = pmd_page(*(pmd));
  547. +
  548. + pgtable_page_ctor(page);
  549. +
  550. + return 0;
  551. +}
  552. +late_initcall(vectors_user_mapping_init_page);
  553. +
  554. #ifdef CONFIG_KUSER_HELPERS
  555. /*
  556. * The vectors page is always readable from user space for the
  557. diff -Nur linux-4.1.20.orig/arch/arm/kernel/signal.c linux-4.1.20/arch/arm/kernel/signal.c
  558. --- linux-4.1.20.orig/arch/arm/kernel/signal.c 2016-03-17 19:11:03.000000000 +0100
  559. +++ linux-4.1.20/arch/arm/kernel/signal.c 2016-03-21 20:18:28.000000000 +0100
  560. @@ -568,7 +568,8 @@
  561. do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
  562. {
  563. do {
  564. - if (likely(thread_flags & _TIF_NEED_RESCHED)) {
  565. + if (likely(thread_flags & (_TIF_NEED_RESCHED |
  566. + _TIF_NEED_RESCHED_LAZY))) {
  567. schedule();
  568. } else {
  569. if (unlikely(!user_mode(regs)))
  570. diff -Nur linux-4.1.20.orig/arch/arm/kernel/smp.c linux-4.1.20/arch/arm/kernel/smp.c
  571. --- linux-4.1.20.orig/arch/arm/kernel/smp.c 2016-03-17 19:11:03.000000000 +0100
  572. +++ linux-4.1.20/arch/arm/kernel/smp.c 2016-03-21 20:18:28.000000000 +0100
  573. @@ -213,8 +213,6 @@
  574. flush_cache_louis();
  575. local_flush_tlb_all();
  576. - clear_tasks_mm_cpumask(cpu);
  577. -
  578. return 0;
  579. }
  580. @@ -230,6 +228,9 @@
  581. pr_err("CPU%u: cpu didn't die\n", cpu);
  582. return;
  583. }
  584. +
  585. + clear_tasks_mm_cpumask(cpu);
  586. +
  587. pr_notice("CPU%u: shutdown\n", cpu);
  588. /*
  589. diff -Nur linux-4.1.20.orig/arch/arm/kernel/unwind.c linux-4.1.20/arch/arm/kernel/unwind.c
  590. --- linux-4.1.20.orig/arch/arm/kernel/unwind.c 2016-03-17 19:11:03.000000000 +0100
  591. +++ linux-4.1.20/arch/arm/kernel/unwind.c 2016-03-21 20:18:28.000000000 +0100
  592. @@ -93,7 +93,7 @@
  593. static const struct unwind_idx *__origin_unwind_idx;
  594. extern const struct unwind_idx __stop_unwind_idx[];
  595. -static DEFINE_SPINLOCK(unwind_lock);
  596. +static DEFINE_RAW_SPINLOCK(unwind_lock);
  597. static LIST_HEAD(unwind_tables);
  598. /* Convert a prel31 symbol to an absolute address */
  599. @@ -201,7 +201,7 @@
  600. /* module unwind tables */
  601. struct unwind_table *table;
  602. - spin_lock_irqsave(&unwind_lock, flags);
  603. + raw_spin_lock_irqsave(&unwind_lock, flags);
  604. list_for_each_entry(table, &unwind_tables, list) {
  605. if (addr >= table->begin_addr &&
  606. addr < table->end_addr) {
  607. @@ -213,7 +213,7 @@
  608. break;
  609. }
  610. }
  611. - spin_unlock_irqrestore(&unwind_lock, flags);
  612. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  613. }
  614. pr_debug("%s: idx = %p\n", __func__, idx);
  615. @@ -529,9 +529,9 @@
  616. tab->begin_addr = text_addr;
  617. tab->end_addr = text_addr + text_size;
  618. - spin_lock_irqsave(&unwind_lock, flags);
  619. + raw_spin_lock_irqsave(&unwind_lock, flags);
  620. list_add_tail(&tab->list, &unwind_tables);
  621. - spin_unlock_irqrestore(&unwind_lock, flags);
  622. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  623. return tab;
  624. }
  625. @@ -543,9 +543,9 @@
  626. if (!tab)
  627. return;
  628. - spin_lock_irqsave(&unwind_lock, flags);
  629. + raw_spin_lock_irqsave(&unwind_lock, flags);
  630. list_del(&tab->list);
  631. - spin_unlock_irqrestore(&unwind_lock, flags);
  632. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  633. kfree(tab);
  634. }
  635. diff -Nur linux-4.1.20.orig/arch/arm/kvm/arm.c linux-4.1.20/arch/arm/kvm/arm.c
  636. --- linux-4.1.20.orig/arch/arm/kvm/arm.c 2016-03-17 19:11:03.000000000 +0100
  637. +++ linux-4.1.20/arch/arm/kvm/arm.c 2016-03-21 20:18:28.000000000 +0100
  638. @@ -474,9 +474,9 @@
  639. static void vcpu_pause(struct kvm_vcpu *vcpu)
  640. {
  641. - wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  642. + struct swait_head *wq = kvm_arch_vcpu_wq(vcpu);
  643. - wait_event_interruptible(*wq, !vcpu->arch.pause);
  644. + swait_event_interruptible(*wq, !vcpu->arch.pause);
  645. }
  646. static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
  647. diff -Nur linux-4.1.20.orig/arch/arm/kvm/psci.c linux-4.1.20/arch/arm/kvm/psci.c
  648. --- linux-4.1.20.orig/arch/arm/kvm/psci.c 2016-03-17 19:11:03.000000000 +0100
  649. +++ linux-4.1.20/arch/arm/kvm/psci.c 2016-03-21 20:18:28.000000000 +0100
  650. @@ -68,7 +68,7 @@
  651. {
  652. struct kvm *kvm = source_vcpu->kvm;
  653. struct kvm_vcpu *vcpu = NULL;
  654. - wait_queue_head_t *wq;
  655. + struct swait_head *wq;
  656. unsigned long cpu_id;
  657. unsigned long context_id;
  658. phys_addr_t target_pc;
  659. @@ -117,7 +117,7 @@
  660. smp_mb(); /* Make sure the above is visible */
  661. wq = kvm_arch_vcpu_wq(vcpu);
  662. - wake_up_interruptible(wq);
  663. + swait_wake_interruptible(wq);
  664. return PSCI_RET_SUCCESS;
  665. }
  666. diff -Nur linux-4.1.20.orig/arch/arm/mach-exynos/platsmp.c linux-4.1.20/arch/arm/mach-exynos/platsmp.c
  667. --- linux-4.1.20.orig/arch/arm/mach-exynos/platsmp.c 2016-03-17 19:11:03.000000000 +0100
  668. +++ linux-4.1.20/arch/arm/mach-exynos/platsmp.c 2016-03-21 20:18:28.000000000 +0100
  669. @@ -231,7 +231,7 @@
  670. return (void __iomem *)(S5P_VA_SCU);
  671. }
  672. -static DEFINE_SPINLOCK(boot_lock);
  673. +static DEFINE_RAW_SPINLOCK(boot_lock);
  674. static void exynos_secondary_init(unsigned int cpu)
  675. {
  676. @@ -244,8 +244,8 @@
  677. /*
  678. * Synchronise with the boot thread.
  679. */
  680. - spin_lock(&boot_lock);
  681. - spin_unlock(&boot_lock);
  682. + raw_spin_lock(&boot_lock);
  683. + raw_spin_unlock(&boot_lock);
  684. }
  685. static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  686. @@ -259,7 +259,7 @@
  687. * Set synchronisation state between this boot processor
  688. * and the secondary one
  689. */
  690. - spin_lock(&boot_lock);
  691. + raw_spin_lock(&boot_lock);
  692. /*
  693. * The secondary processor is waiting to be released from
  694. @@ -286,7 +286,7 @@
  695. if (timeout == 0) {
  696. printk(KERN_ERR "cpu1 power enable failed");
  697. - spin_unlock(&boot_lock);
  698. + raw_spin_unlock(&boot_lock);
  699. return -ETIMEDOUT;
  700. }
  701. }
  702. @@ -342,7 +342,7 @@
  703. * calibrations, then wait for it to finish
  704. */
  705. fail:
  706. - spin_unlock(&boot_lock);
  707. + raw_spin_unlock(&boot_lock);
  708. return pen_release != -1 ? ret : 0;
  709. }
  710. diff -Nur linux-4.1.20.orig/arch/arm/mach-hisi/platmcpm.c linux-4.1.20/arch/arm/mach-hisi/platmcpm.c
  711. --- linux-4.1.20.orig/arch/arm/mach-hisi/platmcpm.c 2016-03-17 19:11:03.000000000 +0100
  712. +++ linux-4.1.20/arch/arm/mach-hisi/platmcpm.c 2016-03-21 20:18:28.000000000 +0100
  713. @@ -57,7 +57,7 @@
  714. static void __iomem *sysctrl, *fabric;
  715. static int hip04_cpu_table[HIP04_MAX_CLUSTERS][HIP04_MAX_CPUS_PER_CLUSTER];
  716. -static DEFINE_SPINLOCK(boot_lock);
  717. +static DEFINE_RAW_SPINLOCK(boot_lock);
  718. static u32 fabric_phys_addr;
  719. /*
  720. * [0]: bootwrapper physical address
  721. @@ -104,7 +104,7 @@
  722. if (cluster >= HIP04_MAX_CLUSTERS || cpu >= HIP04_MAX_CPUS_PER_CLUSTER)
  723. return -EINVAL;
  724. - spin_lock_irq(&boot_lock);
  725. + raw_spin_lock_irq(&boot_lock);
  726. if (hip04_cpu_table[cluster][cpu])
  727. goto out;
  728. @@ -133,7 +133,7 @@
  729. udelay(20);
  730. out:
  731. hip04_cpu_table[cluster][cpu]++;
  732. - spin_unlock_irq(&boot_lock);
  733. + raw_spin_unlock_irq(&boot_lock);
  734. return 0;
  735. }
  736. @@ -149,7 +149,7 @@
  737. __mcpm_cpu_going_down(cpu, cluster);
  738. - spin_lock(&boot_lock);
  739. + raw_spin_lock(&boot_lock);
  740. BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
  741. hip04_cpu_table[cluster][cpu]--;
  742. if (hip04_cpu_table[cluster][cpu] == 1) {
  743. @@ -162,7 +162,7 @@
  744. last_man = hip04_cluster_is_down(cluster);
  745. if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
  746. - spin_unlock(&boot_lock);
  747. + raw_spin_unlock(&boot_lock);
  748. /* Since it's Cortex A15, disable L2 prefetching. */
  749. asm volatile(
  750. "mcr p15, 1, %0, c15, c0, 3 \n\t"
  751. @@ -173,7 +173,7 @@
  752. hip04_set_snoop_filter(cluster, 0);
  753. __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
  754. } else {
  755. - spin_unlock(&boot_lock);
  756. + raw_spin_unlock(&boot_lock);
  757. v7_exit_coherency_flush(louis);
  758. }
  759. @@ -192,7 +192,7 @@
  760. cpu >= HIP04_MAX_CPUS_PER_CLUSTER);
  761. count = TIMEOUT_MSEC / POLL_MSEC;
  762. - spin_lock_irq(&boot_lock);
  763. + raw_spin_lock_irq(&boot_lock);
  764. for (tries = 0; tries < count; tries++) {
  765. if (hip04_cpu_table[cluster][cpu]) {
  766. ret = -EBUSY;
  767. @@ -202,10 +202,10 @@
  768. data = readl_relaxed(sysctrl + SC_CPU_RESET_STATUS(cluster));
  769. if (data & CORE_WFI_STATUS(cpu))
  770. break;
  771. - spin_unlock_irq(&boot_lock);
  772. + raw_spin_unlock_irq(&boot_lock);
  773. /* Wait for clean L2 when the whole cluster is down. */
  774. msleep(POLL_MSEC);
  775. - spin_lock_irq(&boot_lock);
  776. + raw_spin_lock_irq(&boot_lock);
  777. }
  778. if (tries >= count)
  779. goto err;
  780. @@ -220,10 +220,10 @@
  781. }
  782. if (tries >= count)
  783. goto err;
  784. - spin_unlock_irq(&boot_lock);
  785. + raw_spin_unlock_irq(&boot_lock);
  786. return 0;
  787. err:
  788. - spin_unlock_irq(&boot_lock);
  789. + raw_spin_unlock_irq(&boot_lock);
  790. return ret;
  791. }
  792. @@ -235,10 +235,10 @@
  793. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  794. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  795. - spin_lock(&boot_lock);
  796. + raw_spin_lock(&boot_lock);
  797. if (!hip04_cpu_table[cluster][cpu])
  798. hip04_cpu_table[cluster][cpu] = 1;
  799. - spin_unlock(&boot_lock);
  800. + raw_spin_unlock(&boot_lock);
  801. }
  802. static void __naked hip04_mcpm_power_up_setup(unsigned int affinity_level)
  803. diff -Nur linux-4.1.20.orig/arch/arm/mach-omap2/gpio.c linux-4.1.20/arch/arm/mach-omap2/gpio.c
  804. --- linux-4.1.20.orig/arch/arm/mach-omap2/gpio.c 2016-03-17 19:11:03.000000000 +0100
  805. +++ linux-4.1.20/arch/arm/mach-omap2/gpio.c 2016-03-21 20:18:28.000000000 +0100
  806. @@ -130,7 +130,6 @@
  807. }
  808. pwrdm = omap_hwmod_get_pwrdm(oh);
  809. - pdata->loses_context = pwrdm_can_ever_lose_context(pwrdm);
  810. pdev = omap_device_build(name, id - 1, oh, pdata, sizeof(*pdata));
  811. kfree(pdata);
  812. diff -Nur linux-4.1.20.orig/arch/arm/mach-omap2/omap-smp.c linux-4.1.20/arch/arm/mach-omap2/omap-smp.c
  813. --- linux-4.1.20.orig/arch/arm/mach-omap2/omap-smp.c 2016-03-17 19:11:03.000000000 +0100
  814. +++ linux-4.1.20/arch/arm/mach-omap2/omap-smp.c 2016-03-21 20:18:28.000000000 +0100
  815. @@ -43,7 +43,7 @@
  816. /* SCU base address */
  817. static void __iomem *scu_base;
  818. -static DEFINE_SPINLOCK(boot_lock);
  819. +static DEFINE_RAW_SPINLOCK(boot_lock);
  820. void __iomem *omap4_get_scu_base(void)
  821. {
  822. @@ -74,8 +74,8 @@
  823. /*
  824. * Synchronise with the boot thread.
  825. */
  826. - spin_lock(&boot_lock);
  827. - spin_unlock(&boot_lock);
  828. + raw_spin_lock(&boot_lock);
  829. + raw_spin_unlock(&boot_lock);
  830. }
  831. static int omap4_boot_secondary(unsigned int cpu, struct task_struct *idle)
  832. @@ -89,7 +89,7 @@
  833. * Set synchronisation state between this boot processor
  834. * and the secondary one
  835. */
  836. - spin_lock(&boot_lock);
  837. + raw_spin_lock(&boot_lock);
  838. /*
  839. * Update the AuxCoreBoot0 with boot state for secondary core.
  840. @@ -166,7 +166,7 @@
  841. * Now the secondary core is starting up let it run its
  842. * calibrations, then wait for it to finish
  843. */
  844. - spin_unlock(&boot_lock);
  845. + raw_spin_unlock(&boot_lock);
  846. return 0;
  847. }
  848. diff -Nur linux-4.1.20.orig/arch/arm/mach-omap2/powerdomain.c linux-4.1.20/arch/arm/mach-omap2/powerdomain.c
  849. --- linux-4.1.20.orig/arch/arm/mach-omap2/powerdomain.c 2016-03-17 19:11:03.000000000 +0100
  850. +++ linux-4.1.20/arch/arm/mach-omap2/powerdomain.c 2016-03-21 20:18:28.000000000 +0100
  851. @@ -1166,43 +1166,3 @@
  852. return count;
  853. }
  854. -/**
  855. - * pwrdm_can_ever_lose_context - can this powerdomain ever lose context?
  856. - * @pwrdm: struct powerdomain *
  857. - *
  858. - * Given a struct powerdomain * @pwrdm, returns 1 if the powerdomain
  859. - * can lose either memory or logic context or if @pwrdm is invalid, or
  860. - * returns 0 otherwise. This function is not concerned with how the
  861. - * powerdomain registers are programmed (i.e., to go off or not); it's
  862. - * concerned with whether it's ever possible for this powerdomain to
  863. - * go off while some other part of the chip is active. This function
  864. - * assumes that every powerdomain can go to either ON or INACTIVE.
  865. - */
  866. -bool pwrdm_can_ever_lose_context(struct powerdomain *pwrdm)
  867. -{
  868. - int i;
  869. -
  870. - if (!pwrdm) {
  871. - pr_debug("powerdomain: %s: invalid powerdomain pointer\n",
  872. - __func__);
  873. - return 1;
  874. - }
  875. -
  876. - if (pwrdm->pwrsts & PWRSTS_OFF)
  877. - return 1;
  878. -
  879. - if (pwrdm->pwrsts & PWRSTS_RET) {
  880. - if (pwrdm->pwrsts_logic_ret & PWRSTS_OFF)
  881. - return 1;
  882. -
  883. - for (i = 0; i < pwrdm->banks; i++)
  884. - if (pwrdm->pwrsts_mem_ret[i] & PWRSTS_OFF)
  885. - return 1;
  886. - }
  887. -
  888. - for (i = 0; i < pwrdm->banks; i++)
  889. - if (pwrdm->pwrsts_mem_on[i] & PWRSTS_OFF)
  890. - return 1;
  891. -
  892. - return 0;
  893. -}
  894. diff -Nur linux-4.1.20.orig/arch/arm/mach-omap2/powerdomain.h linux-4.1.20/arch/arm/mach-omap2/powerdomain.h
  895. --- linux-4.1.20.orig/arch/arm/mach-omap2/powerdomain.h 2016-03-17 19:11:03.000000000 +0100
  896. +++ linux-4.1.20/arch/arm/mach-omap2/powerdomain.h 2016-03-21 20:18:28.000000000 +0100
  897. @@ -244,7 +244,6 @@
  898. int pwrdm_pre_transition(struct powerdomain *pwrdm);
  899. int pwrdm_post_transition(struct powerdomain *pwrdm);
  900. int pwrdm_get_context_loss_count(struct powerdomain *pwrdm);
  901. -bool pwrdm_can_ever_lose_context(struct powerdomain *pwrdm);
  902. extern int omap_set_pwrdm_state(struct powerdomain *pwrdm, u8 state);
  903. diff -Nur linux-4.1.20.orig/arch/arm/mach-prima2/platsmp.c linux-4.1.20/arch/arm/mach-prima2/platsmp.c
  904. --- linux-4.1.20.orig/arch/arm/mach-prima2/platsmp.c 2016-03-17 19:11:03.000000000 +0100
  905. +++ linux-4.1.20/arch/arm/mach-prima2/platsmp.c 2016-03-21 20:18:28.000000000 +0100
  906. @@ -22,7 +22,7 @@
  907. static void __iomem *clk_base;
  908. -static DEFINE_SPINLOCK(boot_lock);
  909. +static DEFINE_RAW_SPINLOCK(boot_lock);
  910. static void sirfsoc_secondary_init(unsigned int cpu)
  911. {
  912. @@ -36,8 +36,8 @@
  913. /*
  914. * Synchronise with the boot thread.
  915. */
  916. - spin_lock(&boot_lock);
  917. - spin_unlock(&boot_lock);
  918. + raw_spin_lock(&boot_lock);
  919. + raw_spin_unlock(&boot_lock);
  920. }
  921. static const struct of_device_id clk_ids[] = {
  922. @@ -75,7 +75,7 @@
  923. /* make sure write buffer is drained */
  924. mb();
  925. - spin_lock(&boot_lock);
  926. + raw_spin_lock(&boot_lock);
  927. /*
  928. * The secondary processor is waiting to be released from
  929. @@ -107,7 +107,7 @@
  930. * now the secondary core is starting up let it run its
  931. * calibrations, then wait for it to finish
  932. */
  933. - spin_unlock(&boot_lock);
  934. + raw_spin_unlock(&boot_lock);
  935. return pen_release != -1 ? -ENOSYS : 0;
  936. }
  937. diff -Nur linux-4.1.20.orig/arch/arm/mach-qcom/platsmp.c linux-4.1.20/arch/arm/mach-qcom/platsmp.c
  938. --- linux-4.1.20.orig/arch/arm/mach-qcom/platsmp.c 2016-03-17 19:11:03.000000000 +0100
  939. +++ linux-4.1.20/arch/arm/mach-qcom/platsmp.c 2016-03-21 20:18:28.000000000 +0100
  940. @@ -46,7 +46,7 @@
  941. extern void secondary_startup_arm(void);
  942. -static DEFINE_SPINLOCK(boot_lock);
  943. +static DEFINE_RAW_SPINLOCK(boot_lock);
  944. #ifdef CONFIG_HOTPLUG_CPU
  945. static void __ref qcom_cpu_die(unsigned int cpu)
  946. @@ -60,8 +60,8 @@
  947. /*
  948. * Synchronise with the boot thread.
  949. */
  950. - spin_lock(&boot_lock);
  951. - spin_unlock(&boot_lock);
  952. + raw_spin_lock(&boot_lock);
  953. + raw_spin_unlock(&boot_lock);
  954. }
  955. static int scss_release_secondary(unsigned int cpu)
  956. @@ -284,7 +284,7 @@
  957. * set synchronisation state between this boot processor
  958. * and the secondary one
  959. */
  960. - spin_lock(&boot_lock);
  961. + raw_spin_lock(&boot_lock);
  962. /*
  963. * Send the secondary CPU a soft interrupt, thereby causing
  964. @@ -297,7 +297,7 @@
  965. * now the secondary core is starting up let it run its
  966. * calibrations, then wait for it to finish
  967. */
  968. - spin_unlock(&boot_lock);
  969. + raw_spin_unlock(&boot_lock);
  970. return ret;
  971. }
  972. diff -Nur linux-4.1.20.orig/arch/arm/mach-spear/platsmp.c linux-4.1.20/arch/arm/mach-spear/platsmp.c
  973. --- linux-4.1.20.orig/arch/arm/mach-spear/platsmp.c 2016-03-17 19:11:03.000000000 +0100
  974. +++ linux-4.1.20/arch/arm/mach-spear/platsmp.c 2016-03-21 20:18:28.000000000 +0100
  975. @@ -32,7 +32,7 @@
  976. sync_cache_w(&pen_release);
  977. }
  978. -static DEFINE_SPINLOCK(boot_lock);
  979. +static DEFINE_RAW_SPINLOCK(boot_lock);
  980. static void __iomem *scu_base = IOMEM(VA_SCU_BASE);
  981. @@ -47,8 +47,8 @@
  982. /*
  983. * Synchronise with the boot thread.
  984. */
  985. - spin_lock(&boot_lock);
  986. - spin_unlock(&boot_lock);
  987. + raw_spin_lock(&boot_lock);
  988. + raw_spin_unlock(&boot_lock);
  989. }
  990. static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
  991. @@ -59,7 +59,7 @@
  992. * set synchronisation state between this boot processor
  993. * and the secondary one
  994. */
  995. - spin_lock(&boot_lock);
  996. + raw_spin_lock(&boot_lock);
  997. /*
  998. * The secondary processor is waiting to be released from
  999. @@ -84,7 +84,7 @@
  1000. * now the secondary core is starting up let it run its
  1001. * calibrations, then wait for it to finish
  1002. */
  1003. - spin_unlock(&boot_lock);
  1004. + raw_spin_unlock(&boot_lock);
  1005. return pen_release != -1 ? -ENOSYS : 0;
  1006. }
  1007. diff -Nur linux-4.1.20.orig/arch/arm/mach-sti/platsmp.c linux-4.1.20/arch/arm/mach-sti/platsmp.c
  1008. --- linux-4.1.20.orig/arch/arm/mach-sti/platsmp.c 2016-03-17 19:11:03.000000000 +0100
  1009. +++ linux-4.1.20/arch/arm/mach-sti/platsmp.c 2016-03-21 20:18:28.000000000 +0100
  1010. @@ -34,7 +34,7 @@
  1011. sync_cache_w(&pen_release);
  1012. }
  1013. -static DEFINE_SPINLOCK(boot_lock);
  1014. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1015. static void sti_secondary_init(unsigned int cpu)
  1016. {
  1017. @@ -49,8 +49,8 @@
  1018. /*
  1019. * Synchronise with the boot thread.
  1020. */
  1021. - spin_lock(&boot_lock);
  1022. - spin_unlock(&boot_lock);
  1023. + raw_spin_lock(&boot_lock);
  1024. + raw_spin_unlock(&boot_lock);
  1025. }
  1026. static int sti_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1027. @@ -61,7 +61,7 @@
  1028. * set synchronisation state between this boot processor
  1029. * and the secondary one
  1030. */
  1031. - spin_lock(&boot_lock);
  1032. + raw_spin_lock(&boot_lock);
  1033. /*
  1034. * The secondary processor is waiting to be released from
  1035. @@ -92,7 +92,7 @@
  1036. * now the secondary core is starting up let it run its
  1037. * calibrations, then wait for it to finish
  1038. */
  1039. - spin_unlock(&boot_lock);
  1040. + raw_spin_unlock(&boot_lock);
  1041. return pen_release != -1 ? -ENOSYS : 0;
  1042. }
  1043. diff -Nur linux-4.1.20.orig/arch/arm/mach-ux500/platsmp.c linux-4.1.20/arch/arm/mach-ux500/platsmp.c
  1044. --- linux-4.1.20.orig/arch/arm/mach-ux500/platsmp.c 2016-03-17 19:11:03.000000000 +0100
  1045. +++ linux-4.1.20/arch/arm/mach-ux500/platsmp.c 2016-03-21 20:18:28.000000000 +0100
  1046. @@ -51,7 +51,7 @@
  1047. return NULL;
  1048. }
  1049. -static DEFINE_SPINLOCK(boot_lock);
  1050. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1051. static void ux500_secondary_init(unsigned int cpu)
  1052. {
  1053. @@ -64,8 +64,8 @@
  1054. /*
  1055. * Synchronise with the boot thread.
  1056. */
  1057. - spin_lock(&boot_lock);
  1058. - spin_unlock(&boot_lock);
  1059. + raw_spin_lock(&boot_lock);
  1060. + raw_spin_unlock(&boot_lock);
  1061. }
  1062. static int ux500_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1063. @@ -76,7 +76,7 @@
  1064. * set synchronisation state between this boot processor
  1065. * and the secondary one
  1066. */
  1067. - spin_lock(&boot_lock);
  1068. + raw_spin_lock(&boot_lock);
  1069. /*
  1070. * The secondary processor is waiting to be released from
  1071. @@ -97,7 +97,7 @@
  1072. * now the secondary core is starting up let it run its
  1073. * calibrations, then wait for it to finish
  1074. */
  1075. - spin_unlock(&boot_lock);
  1076. + raw_spin_unlock(&boot_lock);
  1077. return pen_release != -1 ? -ENOSYS : 0;
  1078. }
  1079. diff -Nur linux-4.1.20.orig/arch/arm/mm/fault.c linux-4.1.20/arch/arm/mm/fault.c
  1080. --- linux-4.1.20.orig/arch/arm/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1081. +++ linux-4.1.20/arch/arm/mm/fault.c 2016-03-21 20:18:28.000000000 +0100
  1082. @@ -276,7 +276,7 @@
  1083. * If we're in an interrupt or have no user
  1084. * context, we must not take the fault..
  1085. */
  1086. - if (in_atomic() || !mm)
  1087. + if (faulthandler_disabled() || !mm)
  1088. goto no_context;
  1089. if (user_mode(regs))
  1090. @@ -430,6 +430,9 @@
  1091. if (addr < TASK_SIZE)
  1092. return do_page_fault(addr, fsr, regs);
  1093. + if (interrupts_enabled(regs))
  1094. + local_irq_enable();
  1095. +
  1096. if (user_mode(regs))
  1097. goto bad_area;
  1098. @@ -497,6 +500,9 @@
  1099. static int
  1100. do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  1101. {
  1102. + if (interrupts_enabled(regs))
  1103. + local_irq_enable();
  1104. +
  1105. do_bad_area(addr, fsr, regs);
  1106. return 0;
  1107. }
  1108. diff -Nur linux-4.1.20.orig/arch/arm/mm/highmem.c linux-4.1.20/arch/arm/mm/highmem.c
  1109. --- linux-4.1.20.orig/arch/arm/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  1110. +++ linux-4.1.20/arch/arm/mm/highmem.c 2016-03-21 20:18:28.000000000 +0100
  1111. @@ -54,11 +54,13 @@
  1112. void *kmap_atomic(struct page *page)
  1113. {
  1114. + pte_t pte = mk_pte(page, kmap_prot);
  1115. unsigned int idx;
  1116. unsigned long vaddr;
  1117. void *kmap;
  1118. int type;
  1119. + preempt_disable_nort();
  1120. pagefault_disable();
  1121. if (!PageHighMem(page))
  1122. return page_address(page);
  1123. @@ -92,7 +94,10 @@
  1124. * in place, so the contained TLB flush ensures the TLB is updated
  1125. * with the new mapping.
  1126. */
  1127. - set_fixmap_pte(idx, mk_pte(page, kmap_prot));
  1128. +#ifdef CONFIG_PREEMPT_RT_FULL
  1129. + current->kmap_pte[type] = pte;
  1130. +#endif
  1131. + set_fixmap_pte(idx, pte);
  1132. return (void *)vaddr;
  1133. }
  1134. @@ -109,27 +114,33 @@
  1135. if (cache_is_vivt())
  1136. __cpuc_flush_dcache_area((void *)vaddr, PAGE_SIZE);
  1137. +#ifdef CONFIG_PREEMPT_RT_FULL
  1138. + current->kmap_pte[type] = __pte(0);
  1139. +#endif
  1140. #ifdef CONFIG_DEBUG_HIGHMEM
  1141. BUG_ON(vaddr != __fix_to_virt(idx));
  1142. - set_fixmap_pte(idx, __pte(0));
  1143. #else
  1144. (void) idx; /* to kill a warning */
  1145. #endif
  1146. + set_fixmap_pte(idx, __pte(0));
  1147. kmap_atomic_idx_pop();
  1148. } else if (vaddr >= PKMAP_ADDR(0) && vaddr < PKMAP_ADDR(LAST_PKMAP)) {
  1149. /* this address was obtained through kmap_high_get() */
  1150. kunmap_high(pte_page(pkmap_page_table[PKMAP_NR(vaddr)]));
  1151. }
  1152. pagefault_enable();
  1153. + preempt_enable_nort();
  1154. }
  1155. EXPORT_SYMBOL(__kunmap_atomic);
  1156. void *kmap_atomic_pfn(unsigned long pfn)
  1157. {
  1158. + pte_t pte = pfn_pte(pfn, kmap_prot);
  1159. unsigned long vaddr;
  1160. int idx, type;
  1161. struct page *page = pfn_to_page(pfn);
  1162. + preempt_disable_nort();
  1163. pagefault_disable();
  1164. if (!PageHighMem(page))
  1165. return page_address(page);
  1166. @@ -140,7 +151,10 @@
  1167. #ifdef CONFIG_DEBUG_HIGHMEM
  1168. BUG_ON(!pte_none(get_fixmap_pte(vaddr)));
  1169. #endif
  1170. - set_fixmap_pte(idx, pfn_pte(pfn, kmap_prot));
  1171. +#ifdef CONFIG_PREEMPT_RT_FULL
  1172. + current->kmap_pte[type] = pte;
  1173. +#endif
  1174. + set_fixmap_pte(idx, pte);
  1175. return (void *)vaddr;
  1176. }
  1177. @@ -154,3 +168,28 @@
  1178. return pte_page(get_fixmap_pte(vaddr));
  1179. }
  1180. +
  1181. +#if defined CONFIG_PREEMPT_RT_FULL
  1182. +void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  1183. +{
  1184. + int i;
  1185. +
  1186. + /*
  1187. + * Clear @prev's kmap_atomic mappings
  1188. + */
  1189. + for (i = 0; i < prev_p->kmap_idx; i++) {
  1190. + int idx = i + KM_TYPE_NR * smp_processor_id();
  1191. +
  1192. + set_fixmap_pte(idx, __pte(0));
  1193. + }
  1194. + /*
  1195. + * Restore @next_p's kmap_atomic mappings
  1196. + */
  1197. + for (i = 0; i < next_p->kmap_idx; i++) {
  1198. + int idx = i + KM_TYPE_NR * smp_processor_id();
  1199. +
  1200. + if (!pte_none(next_p->kmap_pte[i]))
  1201. + set_fixmap_pte(idx, next_p->kmap_pte[i]);
  1202. + }
  1203. +}
  1204. +#endif
  1205. diff -Nur linux-4.1.20.orig/arch/arm/plat-versatile/platsmp.c linux-4.1.20/arch/arm/plat-versatile/platsmp.c
  1206. --- linux-4.1.20.orig/arch/arm/plat-versatile/platsmp.c 2016-03-17 19:11:03.000000000 +0100
  1207. +++ linux-4.1.20/arch/arm/plat-versatile/platsmp.c 2016-03-21 20:18:28.000000000 +0100
  1208. @@ -30,7 +30,7 @@
  1209. sync_cache_w(&pen_release);
  1210. }
  1211. -static DEFINE_SPINLOCK(boot_lock);
  1212. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1213. void versatile_secondary_init(unsigned int cpu)
  1214. {
  1215. @@ -43,8 +43,8 @@
  1216. /*
  1217. * Synchronise with the boot thread.
  1218. */
  1219. - spin_lock(&boot_lock);
  1220. - spin_unlock(&boot_lock);
  1221. + raw_spin_lock(&boot_lock);
  1222. + raw_spin_unlock(&boot_lock);
  1223. }
  1224. int versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1225. @@ -55,7 +55,7 @@
  1226. * Set synchronisation state between this boot processor
  1227. * and the secondary one
  1228. */
  1229. - spin_lock(&boot_lock);
  1230. + raw_spin_lock(&boot_lock);
  1231. /*
  1232. * This is really belt and braces; we hold unintended secondary
  1233. @@ -85,7 +85,7 @@
  1234. * now the secondary core is starting up let it run its
  1235. * calibrations, then wait for it to finish
  1236. */
  1237. - spin_unlock(&boot_lock);
  1238. + raw_spin_unlock(&boot_lock);
  1239. return pen_release != -1 ? -ENOSYS : 0;
  1240. }
  1241. diff -Nur linux-4.1.20.orig/arch/arm64/Kconfig linux-4.1.20/arch/arm64/Kconfig
  1242. --- linux-4.1.20.orig/arch/arm64/Kconfig 2016-03-17 19:11:03.000000000 +0100
  1243. +++ linux-4.1.20/arch/arm64/Kconfig 2016-03-21 20:18:28.000000000 +0100
  1244. @@ -69,8 +69,10 @@
  1245. select HAVE_PERF_REGS
  1246. select HAVE_PERF_USER_STACK_DUMP
  1247. select HAVE_RCU_TABLE_FREE
  1248. + select HAVE_PREEMPT_LAZY
  1249. select HAVE_SYSCALL_TRACEPOINTS
  1250. select IRQ_DOMAIN
  1251. + select IRQ_FORCED_THREADING
  1252. select MODULES_USE_ELF_RELA
  1253. select NO_BOOTMEM
  1254. select OF
  1255. @@ -599,7 +601,7 @@
  1256. config XEN
  1257. bool "Xen guest support on ARM64"
  1258. - depends on ARM64 && OF
  1259. + depends on ARM64 && OF && !PREEMPT_RT_FULL
  1260. select SWIOTLB_XEN
  1261. help
  1262. Say Y if you want to run Linux in a Virtual Machine on Xen on ARM64.
  1263. diff -Nur linux-4.1.20.orig/arch/arm64/include/asm/futex.h linux-4.1.20/arch/arm64/include/asm/futex.h
  1264. --- linux-4.1.20.orig/arch/arm64/include/asm/futex.h 2016-03-17 19:11:03.000000000 +0100
  1265. +++ linux-4.1.20/arch/arm64/include/asm/futex.h 2016-03-21 20:18:28.000000000 +0100
  1266. @@ -58,7 +58,7 @@
  1267. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  1268. return -EFAULT;
  1269. - pagefault_disable(); /* implies preempt_disable() */
  1270. + pagefault_disable();
  1271. switch (op) {
  1272. case FUTEX_OP_SET:
  1273. @@ -85,7 +85,7 @@
  1274. ret = -ENOSYS;
  1275. }
  1276. - pagefault_enable(); /* subsumes preempt_enable() */
  1277. + pagefault_enable();
  1278. if (!ret) {
  1279. switch (cmp) {
  1280. diff -Nur linux-4.1.20.orig/arch/arm64/include/asm/thread_info.h linux-4.1.20/arch/arm64/include/asm/thread_info.h
  1281. --- linux-4.1.20.orig/arch/arm64/include/asm/thread_info.h 2016-03-17 19:11:03.000000000 +0100
  1282. +++ linux-4.1.20/arch/arm64/include/asm/thread_info.h 2016-03-21 20:18:28.000000000 +0100
  1283. @@ -47,6 +47,7 @@
  1284. mm_segment_t addr_limit; /* address limit */
  1285. struct task_struct *task; /* main task structure */
  1286. int preempt_count; /* 0 => preemptable, <0 => bug */
  1287. + int preempt_lazy_count; /* 0 => preemptable, <0 => bug */
  1288. int cpu; /* cpu */
  1289. };
  1290. @@ -101,6 +102,7 @@
  1291. #define TIF_NEED_RESCHED 1
  1292. #define TIF_NOTIFY_RESUME 2 /* callback before returning to user */
  1293. #define TIF_FOREIGN_FPSTATE 3 /* CPU's FP state is not current's */
  1294. +#define TIF_NEED_RESCHED_LAZY 4
  1295. #define TIF_NOHZ 7
  1296. #define TIF_SYSCALL_TRACE 8
  1297. #define TIF_SYSCALL_AUDIT 9
  1298. @@ -117,6 +119,7 @@
  1299. #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
  1300. #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
  1301. #define _TIF_FOREIGN_FPSTATE (1 << TIF_FOREIGN_FPSTATE)
  1302. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  1303. #define _TIF_NOHZ (1 << TIF_NOHZ)
  1304. #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
  1305. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  1306. diff -Nur linux-4.1.20.orig/arch/arm64/kernel/asm-offsets.c linux-4.1.20/arch/arm64/kernel/asm-offsets.c
  1307. --- linux-4.1.20.orig/arch/arm64/kernel/asm-offsets.c 2016-03-17 19:11:03.000000000 +0100
  1308. +++ linux-4.1.20/arch/arm64/kernel/asm-offsets.c 2016-03-21 20:18:28.000000000 +0100
  1309. @@ -35,6 +35,7 @@
  1310. BLANK();
  1311. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  1312. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  1313. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  1314. DEFINE(TI_ADDR_LIMIT, offsetof(struct thread_info, addr_limit));
  1315. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  1316. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  1317. diff -Nur linux-4.1.20.orig/arch/arm64/kernel/entry.S linux-4.1.20/arch/arm64/kernel/entry.S
  1318. --- linux-4.1.20.orig/arch/arm64/kernel/entry.S 2016-03-17 19:11:03.000000000 +0100
  1319. +++ linux-4.1.20/arch/arm64/kernel/entry.S 2016-03-21 20:18:28.000000000 +0100
  1320. @@ -367,11 +367,16 @@
  1321. #ifdef CONFIG_PREEMPT
  1322. get_thread_info tsk
  1323. ldr w24, [tsk, #TI_PREEMPT] // get preempt count
  1324. - cbnz w24, 1f // preempt count != 0
  1325. + cbnz w24, 2f // preempt count != 0
  1326. ldr x0, [tsk, #TI_FLAGS] // get flags
  1327. - tbz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  1328. - bl el1_preempt
  1329. + tbnz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  1330. +
  1331. + ldr w24, [tsk, #TI_PREEMPT_LAZY] // get preempt lazy count
  1332. + cbnz w24, 2f // preempt lazy count != 0
  1333. + tbz x0, #TIF_NEED_RESCHED_LAZY, 2f // needs rescheduling?
  1334. 1:
  1335. + bl el1_preempt
  1336. +2:
  1337. #endif
  1338. #ifdef CONFIG_TRACE_IRQFLAGS
  1339. bl trace_hardirqs_on
  1340. @@ -385,6 +390,7 @@
  1341. 1: bl preempt_schedule_irq // irq en/disable is done inside
  1342. ldr x0, [tsk, #TI_FLAGS] // get new tasks TI_FLAGS
  1343. tbnz x0, #TIF_NEED_RESCHED, 1b // needs rescheduling?
  1344. + tbnz x0, #TIF_NEED_RESCHED_LAZY, 1b // needs rescheduling?
  1345. ret x24
  1346. #endif
  1347. @@ -622,6 +628,7 @@
  1348. str x0, [sp, #S_X0] // returned x0
  1349. work_pending:
  1350. tbnz x1, #TIF_NEED_RESCHED, work_resched
  1351. + tbnz x1, #TIF_NEED_RESCHED_LAZY, work_resched
  1352. /* TIF_SIGPENDING, TIF_NOTIFY_RESUME or TIF_FOREIGN_FPSTATE case */
  1353. ldr x2, [sp, #S_PSTATE]
  1354. mov x0, sp // 'regs'
  1355. diff -Nur linux-4.1.20.orig/arch/arm64/kernel/insn.c linux-4.1.20/arch/arm64/kernel/insn.c
  1356. --- linux-4.1.20.orig/arch/arm64/kernel/insn.c 2016-03-17 19:11:03.000000000 +0100
  1357. +++ linux-4.1.20/arch/arm64/kernel/insn.c 2016-03-21 20:18:28.000000000 +0100
  1358. @@ -77,7 +77,7 @@
  1359. }
  1360. }
  1361. -static DEFINE_SPINLOCK(patch_lock);
  1362. +static DEFINE_RAW_SPINLOCK(patch_lock);
  1363. static void __kprobes *patch_map(void *addr, int fixmap)
  1364. {
  1365. @@ -124,13 +124,13 @@
  1366. unsigned long flags = 0;
  1367. int ret;
  1368. - spin_lock_irqsave(&patch_lock, flags);
  1369. + raw_spin_lock_irqsave(&patch_lock, flags);
  1370. waddr = patch_map(addr, FIX_TEXT_POKE0);
  1371. ret = probe_kernel_write(waddr, &insn, AARCH64_INSN_SIZE);
  1372. patch_unmap(FIX_TEXT_POKE0);
  1373. - spin_unlock_irqrestore(&patch_lock, flags);
  1374. + raw_spin_unlock_irqrestore(&patch_lock, flags);
  1375. return ret;
  1376. }
  1377. diff -Nur linux-4.1.20.orig/arch/arm64/kernel/perf_event.c linux-4.1.20/arch/arm64/kernel/perf_event.c
  1378. --- linux-4.1.20.orig/arch/arm64/kernel/perf_event.c 2016-03-17 19:11:03.000000000 +0100
  1379. +++ linux-4.1.20/arch/arm64/kernel/perf_event.c 2016-03-21 20:18:28.000000000 +0100
  1380. @@ -488,7 +488,7 @@
  1381. }
  1382. err = request_irq(irq, armpmu->handle_irq,
  1383. - IRQF_NOBALANCING,
  1384. + IRQF_NOBALANCING | IRQF_NO_THREAD,
  1385. "arm-pmu", armpmu);
  1386. if (err) {
  1387. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  1388. diff -Nur linux-4.1.20.orig/arch/arm64/mm/fault.c linux-4.1.20/arch/arm64/mm/fault.c
  1389. --- linux-4.1.20.orig/arch/arm64/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1390. +++ linux-4.1.20/arch/arm64/mm/fault.c 2016-03-21 20:18:28.000000000 +0100
  1391. @@ -211,7 +211,7 @@
  1392. * If we're in an interrupt or have no user context, we must not take
  1393. * the fault.
  1394. */
  1395. - if (in_atomic() || !mm)
  1396. + if (faulthandler_disabled() || !mm)
  1397. goto no_context;
  1398. if (user_mode(regs))
  1399. diff -Nur linux-4.1.20.orig/arch/avr32/include/asm/uaccess.h linux-4.1.20/arch/avr32/include/asm/uaccess.h
  1400. --- linux-4.1.20.orig/arch/avr32/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  1401. +++ linux-4.1.20/arch/avr32/include/asm/uaccess.h 2016-03-21 20:18:28.000000000 +0100
  1402. @@ -97,7 +97,8 @@
  1403. * @x: Value to copy to user space.
  1404. * @ptr: Destination address, in user space.
  1405. *
  1406. - * Context: User context only. This function may sleep.
  1407. + * Context: User context only. This function may sleep if pagefaults are
  1408. + * enabled.
  1409. *
  1410. * This macro copies a single simple value from kernel space to user
  1411. * space. It supports simple types like char and int, but not larger
  1412. @@ -116,7 +117,8 @@
  1413. * @x: Variable to store result.
  1414. * @ptr: Source address, in user space.
  1415. *
  1416. - * Context: User context only. This function may sleep.
  1417. + * Context: User context only. This function may sleep if pagefaults are
  1418. + * enabled.
  1419. *
  1420. * This macro copies a single simple variable from user space to kernel
  1421. * space. It supports simple types like char and int, but not larger
  1422. @@ -136,7 +138,8 @@
  1423. * @x: Value to copy to user space.
  1424. * @ptr: Destination address, in user space.
  1425. *
  1426. - * Context: User context only. This function may sleep.
  1427. + * Context: User context only. This function may sleep if pagefaults are
  1428. + * enabled.
  1429. *
  1430. * This macro copies a single simple value from kernel space to user
  1431. * space. It supports simple types like char and int, but not larger
  1432. @@ -158,7 +161,8 @@
  1433. * @x: Variable to store result.
  1434. * @ptr: Source address, in user space.
  1435. *
  1436. - * Context: User context only. This function may sleep.
  1437. + * Context: User context only. This function may sleep if pagefaults are
  1438. + * enabled.
  1439. *
  1440. * This macro copies a single simple variable from user space to kernel
  1441. * space. It supports simple types like char and int, but not larger
  1442. diff -Nur linux-4.1.20.orig/arch/avr32/mm/fault.c linux-4.1.20/arch/avr32/mm/fault.c
  1443. --- linux-4.1.20.orig/arch/avr32/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1444. +++ linux-4.1.20/arch/avr32/mm/fault.c 2016-03-21 20:18:28.000000000 +0100
  1445. @@ -14,11 +14,11 @@
  1446. #include <linux/pagemap.h>
  1447. #include <linux/kdebug.h>
  1448. #include <linux/kprobes.h>
  1449. +#include <linux/uaccess.h>
  1450. #include <asm/mmu_context.h>
  1451. #include <asm/sysreg.h>
  1452. #include <asm/tlb.h>
  1453. -#include <asm/uaccess.h>
  1454. #ifdef CONFIG_KPROBES
  1455. static inline int notify_page_fault(struct pt_regs *regs, int trap)
  1456. @@ -81,7 +81,7 @@
  1457. * If we're in an interrupt or have no user context, we must
  1458. * not take the fault...
  1459. */
  1460. - if (in_atomic() || !mm || regs->sr & SYSREG_BIT(GM))
  1461. + if (faulthandler_disabled() || !mm || regs->sr & SYSREG_BIT(GM))
  1462. goto no_context;
  1463. local_irq_enable();
  1464. diff -Nur linux-4.1.20.orig/arch/cris/mm/fault.c linux-4.1.20/arch/cris/mm/fault.c
  1465. --- linux-4.1.20.orig/arch/cris/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1466. +++ linux-4.1.20/arch/cris/mm/fault.c 2016-03-21 20:18:28.000000000 +0100
  1467. @@ -8,7 +8,7 @@
  1468. #include <linux/interrupt.h>
  1469. #include <linux/module.h>
  1470. #include <linux/wait.h>
  1471. -#include <asm/uaccess.h>
  1472. +#include <linux/uaccess.h>
  1473. #include <arch/system.h>
  1474. extern int find_fixup_code(struct pt_regs *);
  1475. @@ -109,11 +109,11 @@
  1476. info.si_code = SEGV_MAPERR;
  1477. /*
  1478. - * If we're in an interrupt or "atomic" operation or have no
  1479. + * If we're in an interrupt, have pagefaults disabled or have no
  1480. * user context, we must not take the fault.
  1481. */
  1482. - if (in_atomic() || !mm)
  1483. + if (faulthandler_disabled() || !mm)
  1484. goto no_context;
  1485. if (user_mode(regs))
  1486. diff -Nur linux-4.1.20.orig/arch/frv/mm/fault.c linux-4.1.20/arch/frv/mm/fault.c
  1487. --- linux-4.1.20.orig/arch/frv/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1488. +++ linux-4.1.20/arch/frv/mm/fault.c 2016-03-21 20:18:28.000000000 +0100
  1489. @@ -19,9 +19,9 @@
  1490. #include <linux/kernel.h>
  1491. #include <linux/ptrace.h>
  1492. #include <linux/hardirq.h>
  1493. +#include <linux/uaccess.h>
  1494. #include <asm/pgtable.h>
  1495. -#include <asm/uaccess.h>
  1496. #include <asm/gdb-stub.h>
  1497. /*****************************************************************************/
  1498. @@ -78,7 +78,7 @@
  1499. * If we're in an interrupt or have no user
  1500. * context, we must not take the fault..
  1501. */
  1502. - if (in_atomic() || !mm)
  1503. + if (faulthandler_disabled() || !mm)
  1504. goto no_context;
  1505. if (user_mode(__frame))
  1506. diff -Nur linux-4.1.20.orig/arch/frv/mm/highmem.c linux-4.1.20/arch/frv/mm/highmem.c
  1507. --- linux-4.1.20.orig/arch/frv/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  1508. +++ linux-4.1.20/arch/frv/mm/highmem.c 2016-03-21 20:18:28.000000000 +0100
  1509. @@ -42,6 +42,7 @@
  1510. unsigned long paddr;
  1511. int type;
  1512. + preempt_disable();
  1513. pagefault_disable();
  1514. type = kmap_atomic_idx_push();
  1515. paddr = page_to_phys(page);
  1516. @@ -85,5 +86,6 @@
  1517. }
  1518. kmap_atomic_idx_pop();
  1519. pagefault_enable();
  1520. + preempt_enable();
  1521. }
  1522. EXPORT_SYMBOL(__kunmap_atomic);
  1523. diff -Nur linux-4.1.20.orig/arch/hexagon/include/asm/uaccess.h linux-4.1.20/arch/hexagon/include/asm/uaccess.h
  1524. --- linux-4.1.20.orig/arch/hexagon/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  1525. +++ linux-4.1.20/arch/hexagon/include/asm/uaccess.h 2016-03-21 20:18:28.000000000 +0100
  1526. @@ -36,7 +36,8 @@
  1527. * @addr: User space pointer to start of block to check
  1528. * @size: Size of block to check
  1529. *
  1530. - * Context: User context only. This function may sleep.
  1531. + * Context: User context only. This function may sleep if pagefaults are
  1532. + * enabled.
  1533. *
  1534. * Checks if a pointer to a block of memory in user space is valid.
  1535. *
  1536. diff -Nur linux-4.1.20.orig/arch/ia64/mm/fault.c linux-4.1.20/arch/ia64/mm/fault.c
  1537. --- linux-4.1.20.orig/arch/ia64/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1538. +++ linux-4.1.20/arch/ia64/mm/fault.c 2016-03-21 20:18:28.000000000 +0100
  1539. @@ -11,10 +11,10 @@
  1540. #include <linux/kprobes.h>
  1541. #include <linux/kdebug.h>
  1542. #include <linux/prefetch.h>
  1543. +#include <linux/uaccess.h>
  1544. #include <asm/pgtable.h>
  1545. #include <asm/processor.h>
  1546. -#include <asm/uaccess.h>
  1547. extern int die(char *, struct pt_regs *, long);
  1548. @@ -96,7 +96,7 @@
  1549. /*
  1550. * If we're in an interrupt or have no user context, we must not take the fault..
  1551. */
  1552. - if (in_atomic() || !mm)
  1553. + if (faulthandler_disabled() || !mm)
  1554. goto no_context;
  1555. #ifdef CONFIG_VIRTUAL_MEM_MAP
  1556. diff -Nur linux-4.1.20.orig/arch/m32r/include/asm/uaccess.h linux-4.1.20/arch/m32r/include/asm/uaccess.h
  1557. --- linux-4.1.20.orig/arch/m32r/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  1558. +++ linux-4.1.20/arch/m32r/include/asm/uaccess.h 2016-03-21 20:18:29.000000000 +0100
  1559. @@ -91,7 +91,8 @@
  1560. * @addr: User space pointer to start of block to check
  1561. * @size: Size of block to check
  1562. *
  1563. - * Context: User context only. This function may sleep.
  1564. + * Context: User context only. This function may sleep if pagefaults are
  1565. + * enabled.
  1566. *
  1567. * Checks if a pointer to a block of memory in user space is valid.
  1568. *
  1569. @@ -155,7 +156,8 @@
  1570. * @x: Variable to store result.
  1571. * @ptr: Source address, in user space.
  1572. *
  1573. - * Context: User context only. This function may sleep.
  1574. + * Context: User context only. This function may sleep if pagefaults are
  1575. + * enabled.
  1576. *
  1577. * This macro copies a single simple variable from user space to kernel
  1578. * space. It supports simple types like char and int, but not larger
  1579. @@ -175,7 +177,8 @@
  1580. * @x: Value to copy to user space.
  1581. * @ptr: Destination address, in user space.
  1582. *
  1583. - * Context: User context only. This function may sleep.
  1584. + * Context: User context only. This function may sleep if pagefaults are
  1585. + * enabled.
  1586. *
  1587. * This macro copies a single simple value from kernel space to user
  1588. * space. It supports simple types like char and int, but not larger
  1589. @@ -194,7 +197,8 @@
  1590. * @x: Variable to store result.
  1591. * @ptr: Source address, in user space.
  1592. *
  1593. - * Context: User context only. This function may sleep.
  1594. + * Context: User context only. This function may sleep if pagefaults are
  1595. + * enabled.
  1596. *
  1597. * This macro copies a single simple variable from user space to kernel
  1598. * space. It supports simple types like char and int, but not larger
  1599. @@ -274,7 +278,8 @@
  1600. * @x: Value to copy to user space.
  1601. * @ptr: Destination address, in user space.
  1602. *
  1603. - * Context: User context only. This function may sleep.
  1604. + * Context: User context only. This function may sleep if pagefaults are
  1605. + * enabled.
  1606. *
  1607. * This macro copies a single simple value from kernel space to user
  1608. * space. It supports simple types like char and int, but not larger
  1609. @@ -568,7 +573,8 @@
  1610. * @from: Source address, in kernel space.
  1611. * @n: Number of bytes to copy.
  1612. *
  1613. - * Context: User context only. This function may sleep.
  1614. + * Context: User context only. This function may sleep if pagefaults are
  1615. + * enabled.
  1616. *
  1617. * Copy data from kernel space to user space. Caller must check
  1618. * the specified block with access_ok() before calling this function.
  1619. @@ -588,7 +594,8 @@
  1620. * @from: Source address, in kernel space.
  1621. * @n: Number of bytes to copy.
  1622. *
  1623. - * Context: User context only. This function may sleep.
  1624. + * Context: User context only. This function may sleep if pagefaults are
  1625. + * enabled.
  1626. *
  1627. * Copy data from kernel space to user space.
  1628. *
  1629. @@ -606,7 +613,8 @@
  1630. * @from: Source address, in user space.
  1631. * @n: Number of bytes to copy.
  1632. *
  1633. - * Context: User context only. This function may sleep.
  1634. + * Context: User context only. This function may sleep if pagefaults are
  1635. + * enabled.
  1636. *
  1637. * Copy data from user space to kernel space. Caller must check
  1638. * the specified block with access_ok() before calling this function.
  1639. @@ -626,7 +634,8 @@
  1640. * @from: Source address, in user space.
  1641. * @n: Number of bytes to copy.
  1642. *
  1643. - * Context: User context only. This function may sleep.
  1644. + * Context: User context only. This function may sleep if pagefaults are
  1645. + * enabled.
  1646. *
  1647. * Copy data from user space to kernel space.
  1648. *
  1649. @@ -677,7 +686,8 @@
  1650. * strlen_user: - Get the size of a string in user space.
  1651. * @str: The string to measure.
  1652. *
  1653. - * Context: User context only. This function may sleep.
  1654. + * Context: User context only. This function may sleep if pagefaults are
  1655. + * enabled.
  1656. *
  1657. * Get the size of a NUL-terminated string in user space.
  1658. *
  1659. diff -Nur linux-4.1.20.orig/arch/m32r/mm/fault.c linux-4.1.20/arch/m32r/mm/fault.c
  1660. --- linux-4.1.20.orig/arch/m32r/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1661. +++ linux-4.1.20/arch/m32r/mm/fault.c 2016-03-21 20:18:29.000000000 +0100
  1662. @@ -24,9 +24,9 @@
  1663. #include <linux/vt_kern.h> /* For unblank_screen() */
  1664. #include <linux/highmem.h>
  1665. #include <linux/module.h>
  1666. +#include <linux/uaccess.h>
  1667. #include <asm/m32r.h>
  1668. -#include <asm/uaccess.h>
  1669. #include <asm/hardirq.h>
  1670. #include <asm/mmu_context.h>
  1671. #include <asm/tlbflush.h>
  1672. @@ -111,10 +111,10 @@
  1673. mm = tsk->mm;
  1674. /*
  1675. - * If we're in an interrupt or have no user context or are running in an
  1676. - * atomic region then we must not take the fault..
  1677. + * If we're in an interrupt or have no user context or have pagefaults
  1678. + * disabled then we must not take the fault.
  1679. */
  1680. - if (in_atomic() || !mm)
  1681. + if (faulthandler_disabled() || !mm)
  1682. goto bad_area_nosemaphore;
  1683. if (error_code & ACE_USERMODE)
  1684. diff -Nur linux-4.1.20.orig/arch/m68k/mm/fault.c linux-4.1.20/arch/m68k/mm/fault.c
  1685. --- linux-4.1.20.orig/arch/m68k/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1686. +++ linux-4.1.20/arch/m68k/mm/fault.c 2016-03-21 20:18:29.000000000 +0100
  1687. @@ -10,10 +10,10 @@
  1688. #include <linux/ptrace.h>
  1689. #include <linux/interrupt.h>
  1690. #include <linux/module.h>
  1691. +#include <linux/uaccess.h>
  1692. #include <asm/setup.h>
  1693. #include <asm/traps.h>
  1694. -#include <asm/uaccess.h>
  1695. #include <asm/pgalloc.h>
  1696. extern void die_if_kernel(char *, struct pt_regs *, long);
  1697. @@ -81,7 +81,7 @@
  1698. * If we're in an interrupt or have no user
  1699. * context, we must not take the fault..
  1700. */
  1701. - if (in_atomic() || !mm)
  1702. + if (faulthandler_disabled() || !mm)
  1703. goto no_context;
  1704. if (user_mode(regs))
  1705. diff -Nur linux-4.1.20.orig/arch/metag/mm/fault.c linux-4.1.20/arch/metag/mm/fault.c
  1706. --- linux-4.1.20.orig/arch/metag/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1707. +++ linux-4.1.20/arch/metag/mm/fault.c 2016-03-21 20:18:29.000000000 +0100
  1708. @@ -105,7 +105,7 @@
  1709. mm = tsk->mm;
  1710. - if (in_atomic() || !mm)
  1711. + if (faulthandler_disabled() || !mm)
  1712. goto no_context;
  1713. if (user_mode(regs))
  1714. diff -Nur linux-4.1.20.orig/arch/metag/mm/highmem.c linux-4.1.20/arch/metag/mm/highmem.c
  1715. --- linux-4.1.20.orig/arch/metag/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  1716. +++ linux-4.1.20/arch/metag/mm/highmem.c 2016-03-21 20:18:29.000000000 +0100
  1717. @@ -43,7 +43,7 @@
  1718. unsigned long vaddr;
  1719. int type;
  1720. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  1721. + preempt_disable();
  1722. pagefault_disable();
  1723. if (!PageHighMem(page))
  1724. return page_address(page);
  1725. @@ -82,6 +82,7 @@
  1726. }
  1727. pagefault_enable();
  1728. + preempt_enable();
  1729. }
  1730. EXPORT_SYMBOL(__kunmap_atomic);
  1731. @@ -95,6 +96,7 @@
  1732. unsigned long vaddr;
  1733. int type;
  1734. + preempt_disable();
  1735. pagefault_disable();
  1736. type = kmap_atomic_idx_push();
  1737. diff -Nur linux-4.1.20.orig/arch/microblaze/include/asm/uaccess.h linux-4.1.20/arch/microblaze/include/asm/uaccess.h
  1738. --- linux-4.1.20.orig/arch/microblaze/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  1739. +++ linux-4.1.20/arch/microblaze/include/asm/uaccess.h 2016-03-21 20:18:29.000000000 +0100
  1740. @@ -178,7 +178,8 @@
  1741. * @x: Variable to store result.
  1742. * @ptr: Source address, in user space.
  1743. *
  1744. - * Context: User context only. This function may sleep.
  1745. + * Context: User context only. This function may sleep if pagefaults are
  1746. + * enabled.
  1747. *
  1748. * This macro copies a single simple variable from user space to kernel
  1749. * space. It supports simple types like char and int, but not larger
  1750. @@ -290,7 +291,8 @@
  1751. * @x: Value to copy to user space.
  1752. * @ptr: Destination address, in user space.
  1753. *
  1754. - * Context: User context only. This function may sleep.
  1755. + * Context: User context only. This function may sleep if pagefaults are
  1756. + * enabled.
  1757. *
  1758. * This macro copies a single simple value from kernel space to user
  1759. * space. It supports simple types like char and int, but not larger
  1760. diff -Nur linux-4.1.20.orig/arch/microblaze/mm/fault.c linux-4.1.20/arch/microblaze/mm/fault.c
  1761. --- linux-4.1.20.orig/arch/microblaze/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1762. +++ linux-4.1.20/arch/microblaze/mm/fault.c 2016-03-21 20:18:29.000000000 +0100
  1763. @@ -107,14 +107,14 @@
  1764. if ((error_code & 0x13) == 0x13 || (error_code & 0x11) == 0x11)
  1765. is_write = 0;
  1766. - if (unlikely(in_atomic() || !mm)) {
  1767. + if (unlikely(faulthandler_disabled() || !mm)) {
  1768. if (kernel_mode(regs))
  1769. goto bad_area_nosemaphore;
  1770. - /* in_atomic() in user mode is really bad,
  1771. + /* faulthandler_disabled() in user mode is really bad,
  1772. as is current->mm == NULL. */
  1773. - pr_emerg("Page fault in user mode with in_atomic(), mm = %p\n",
  1774. - mm);
  1775. + pr_emerg("Page fault in user mode with faulthandler_disabled(), mm = %p\n",
  1776. + mm);
  1777. pr_emerg("r15 = %lx MSR = %lx\n",
  1778. regs->r15, regs->msr);
  1779. die("Weird page fault", regs, SIGSEGV);
  1780. diff -Nur linux-4.1.20.orig/arch/microblaze/mm/highmem.c linux-4.1.20/arch/microblaze/mm/highmem.c
  1781. --- linux-4.1.20.orig/arch/microblaze/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  1782. +++ linux-4.1.20/arch/microblaze/mm/highmem.c 2016-03-21 20:18:29.000000000 +0100
  1783. @@ -37,7 +37,7 @@
  1784. unsigned long vaddr;
  1785. int idx, type;
  1786. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  1787. + preempt_disable();
  1788. pagefault_disable();
  1789. if (!PageHighMem(page))
  1790. return page_address(page);
  1791. @@ -63,6 +63,7 @@
  1792. if (vaddr < __fix_to_virt(FIX_KMAP_END)) {
  1793. pagefault_enable();
  1794. + preempt_enable();
  1795. return;
  1796. }
  1797. @@ -84,5 +85,6 @@
  1798. #endif
  1799. kmap_atomic_idx_pop();
  1800. pagefault_enable();
  1801. + preempt_enable();
  1802. }
  1803. EXPORT_SYMBOL(__kunmap_atomic);
  1804. diff -Nur linux-4.1.20.orig/arch/mips/Kconfig linux-4.1.20/arch/mips/Kconfig
  1805. --- linux-4.1.20.orig/arch/mips/Kconfig 2016-03-17 19:11:03.000000000 +0100
  1806. +++ linux-4.1.20/arch/mips/Kconfig 2016-03-21 20:18:29.000000000 +0100
  1807. @@ -2366,7 +2366,7 @@
  1808. #
  1809. config HIGHMEM
  1810. bool "High Memory Support"
  1811. - depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA
  1812. + depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA && !PREEMPT_RT_FULL
  1813. config CPU_SUPPORTS_HIGHMEM
  1814. bool
  1815. diff -Nur linux-4.1.20.orig/arch/mips/include/asm/uaccess.h linux-4.1.20/arch/mips/include/asm/uaccess.h
  1816. --- linux-4.1.20.orig/arch/mips/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  1817. +++ linux-4.1.20/arch/mips/include/asm/uaccess.h 2016-03-21 20:18:29.000000000 +0100
  1818. @@ -103,7 +103,8 @@
  1819. * @addr: User space pointer to start of block to check
  1820. * @size: Size of block to check
  1821. *
  1822. - * Context: User context only. This function may sleep.
  1823. + * Context: User context only. This function may sleep if pagefaults are
  1824. + * enabled.
  1825. *
  1826. * Checks if a pointer to a block of memory in user space is valid.
  1827. *
  1828. @@ -138,7 +139,8 @@
  1829. * @x: Value to copy to user space.
  1830. * @ptr: Destination address, in user space.
  1831. *
  1832. - * Context: User context only. This function may sleep.
  1833. + * Context: User context only. This function may sleep if pagefaults are
  1834. + * enabled.
  1835. *
  1836. * This macro copies a single simple value from kernel space to user
  1837. * space. It supports simple types like char and int, but not larger
  1838. @@ -157,7 +159,8 @@
  1839. * @x: Variable to store result.
  1840. * @ptr: Source address, in user space.
  1841. *
  1842. - * Context: User context only. This function may sleep.
  1843. + * Context: User context only. This function may sleep if pagefaults are
  1844. + * enabled.
  1845. *
  1846. * This macro copies a single simple variable from user space to kernel
  1847. * space. It supports simple types like char and int, but not larger
  1848. @@ -177,7 +180,8 @@
  1849. * @x: Value to copy to user space.
  1850. * @ptr: Destination address, in user space.
  1851. *
  1852. - * Context: User context only. This function may sleep.
  1853. + * Context: User context only. This function may sleep if pagefaults are
  1854. + * enabled.
  1855. *
  1856. * This macro copies a single simple value from kernel space to user
  1857. * space. It supports simple types like char and int, but not larger
  1858. @@ -199,7 +203,8 @@
  1859. * @x: Variable to store result.
  1860. * @ptr: Source address, in user space.
  1861. *
  1862. - * Context: User context only. This function may sleep.
  1863. + * Context: User context only. This function may sleep if pagefaults are
  1864. + * enabled.
  1865. *
  1866. * This macro copies a single simple variable from user space to kernel
  1867. * space. It supports simple types like char and int, but not larger
  1868. @@ -498,7 +503,8 @@
  1869. * @x: Value to copy to user space.
  1870. * @ptr: Destination address, in user space.
  1871. *
  1872. - * Context: User context only. This function may sleep.
  1873. + * Context: User context only. This function may sleep if pagefaults are
  1874. + * enabled.
  1875. *
  1876. * This macro copies a single simple value from kernel space to user
  1877. * space. It supports simple types like char and int, but not larger
  1878. @@ -517,7 +523,8 @@
  1879. * @x: Variable to store result.
  1880. * @ptr: Source address, in user space.
  1881. *
  1882. - * Context: User context only. This function may sleep.
  1883. + * Context: User context only. This function may sleep if pagefaults are
  1884. + * enabled.
  1885. *
  1886. * This macro copies a single simple variable from user space to kernel
  1887. * space. It supports simple types like char and int, but not larger
  1888. @@ -537,7 +544,8 @@
  1889. * @x: Value to copy to user space.
  1890. * @ptr: Destination address, in user space.
  1891. *
  1892. - * Context: User context only. This function may sleep.
  1893. + * Context: User context only. This function may sleep if pagefaults are
  1894. + * enabled.
  1895. *
  1896. * This macro copies a single simple value from kernel space to user
  1897. * space. It supports simple types like char and int, but not larger
  1898. @@ -559,7 +567,8 @@
  1899. * @x: Variable to store result.
  1900. * @ptr: Source address, in user space.
  1901. *
  1902. - * Context: User context only. This function may sleep.
  1903. + * Context: User context only. This function may sleep if pagefaults are
  1904. + * enabled.
  1905. *
  1906. * This macro copies a single simple variable from user space to kernel
  1907. * space. It supports simple types like char and int, but not larger
  1908. @@ -815,7 +824,8 @@
  1909. * @from: Source address, in kernel space.
  1910. * @n: Number of bytes to copy.
  1911. *
  1912. - * Context: User context only. This function may sleep.
  1913. + * Context: User context only. This function may sleep if pagefaults are
  1914. + * enabled.
  1915. *
  1916. * Copy data from kernel space to user space. Caller must check
  1917. * the specified block with access_ok() before calling this function.
  1918. @@ -888,7 +898,8 @@
  1919. * @from: Source address, in kernel space.
  1920. * @n: Number of bytes to copy.
  1921. *
  1922. - * Context: User context only. This function may sleep.
  1923. + * Context: User context only. This function may sleep if pagefaults are
  1924. + * enabled.
  1925. *
  1926. * Copy data from kernel space to user space.
  1927. *
  1928. @@ -1075,7 +1086,8 @@
  1929. * @from: Source address, in user space.
  1930. * @n: Number of bytes to copy.
  1931. *
  1932. - * Context: User context only. This function may sleep.
  1933. + * Context: User context only. This function may sleep if pagefaults are
  1934. + * enabled.
  1935. *
  1936. * Copy data from user space to kernel space. Caller must check
  1937. * the specified block with access_ok() before calling this function.
  1938. @@ -1107,7 +1119,8 @@
  1939. * @from: Source address, in user space.
  1940. * @n: Number of bytes to copy.
  1941. *
  1942. - * Context: User context only. This function may sleep.
  1943. + * Context: User context only. This function may sleep if pagefaults are
  1944. + * enabled.
  1945. *
  1946. * Copy data from user space to kernel space.
  1947. *
  1948. @@ -1329,7 +1342,8 @@
  1949. * strlen_user: - Get the size of a string in user space.
  1950. * @str: The string to measure.
  1951. *
  1952. - * Context: User context only. This function may sleep.
  1953. + * Context: User context only. This function may sleep if pagefaults are
  1954. + * enabled.
  1955. *
  1956. * Get the size of a NUL-terminated string in user space.
  1957. *
  1958. @@ -1398,7 +1412,8 @@
  1959. * strnlen_user: - Get the size of a string in user space.
  1960. * @str: The string to measure.
  1961. *
  1962. - * Context: User context only. This function may sleep.
  1963. + * Context: User context only. This function may sleep if pagefaults are
  1964. + * enabled.
  1965. *
  1966. * Get the size of a NUL-terminated string in user space.
  1967. *
  1968. diff -Nur linux-4.1.20.orig/arch/mips/kernel/signal-common.h linux-4.1.20/arch/mips/kernel/signal-common.h
  1969. --- linux-4.1.20.orig/arch/mips/kernel/signal-common.h 2016-03-17 19:11:03.000000000 +0100
  1970. +++ linux-4.1.20/arch/mips/kernel/signal-common.h 2016-03-21 20:18:29.000000000 +0100
  1971. @@ -28,12 +28,7 @@
  1972. extern int fpcsr_pending(unsigned int __user *fpcsr);
  1973. /* Make sure we will not lose FPU ownership */
  1974. -#ifdef CONFIG_PREEMPT
  1975. -#define lock_fpu_owner() preempt_disable()
  1976. -#define unlock_fpu_owner() preempt_enable()
  1977. -#else
  1978. -#define lock_fpu_owner() pagefault_disable()
  1979. -#define unlock_fpu_owner() pagefault_enable()
  1980. -#endif
  1981. +#define lock_fpu_owner() ({ preempt_disable(); pagefault_disable(); })
  1982. +#define unlock_fpu_owner() ({ pagefault_enable(); preempt_enable(); })
  1983. #endif /* __SIGNAL_COMMON_H */
  1984. diff -Nur linux-4.1.20.orig/arch/mips/mm/fault.c linux-4.1.20/arch/mips/mm/fault.c
  1985. --- linux-4.1.20.orig/arch/mips/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1986. +++ linux-4.1.20/arch/mips/mm/fault.c 2016-03-21 20:18:29.000000000 +0100
  1987. @@ -21,10 +21,10 @@
  1988. #include <linux/module.h>
  1989. #include <linux/kprobes.h>
  1990. #include <linux/perf_event.h>
  1991. +#include <linux/uaccess.h>
  1992. #include <asm/branch.h>
  1993. #include <asm/mmu_context.h>
  1994. -#include <asm/uaccess.h>
  1995. #include <asm/ptrace.h>
  1996. #include <asm/highmem.h> /* For VMALLOC_END */
  1997. #include <linux/kdebug.h>
  1998. @@ -94,7 +94,7 @@
  1999. * If we're in an interrupt or have no user
  2000. * context, we must not take the fault..
  2001. */
  2002. - if (in_atomic() || !mm)
  2003. + if (faulthandler_disabled() || !mm)
  2004. goto bad_area_nosemaphore;
  2005. if (user_mode(regs))
  2006. diff -Nur linux-4.1.20.orig/arch/mips/mm/highmem.c linux-4.1.20/arch/mips/mm/highmem.c
  2007. --- linux-4.1.20.orig/arch/mips/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  2008. +++ linux-4.1.20/arch/mips/mm/highmem.c 2016-03-21 20:18:29.000000000 +0100
  2009. @@ -47,7 +47,7 @@
  2010. unsigned long vaddr;
  2011. int idx, type;
  2012. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  2013. + preempt_disable();
  2014. pagefault_disable();
  2015. if (!PageHighMem(page))
  2016. return page_address(page);
  2017. @@ -72,6 +72,7 @@
  2018. if (vaddr < FIXADDR_START) { // FIXME
  2019. pagefault_enable();
  2020. + preempt_enable();
  2021. return;
  2022. }
  2023. @@ -92,6 +93,7 @@
  2024. #endif
  2025. kmap_atomic_idx_pop();
  2026. pagefault_enable();
  2027. + preempt_enable();
  2028. }
  2029. EXPORT_SYMBOL(__kunmap_atomic);
  2030. @@ -104,6 +106,7 @@
  2031. unsigned long vaddr;
  2032. int idx, type;
  2033. + preempt_disable();
  2034. pagefault_disable();
  2035. type = kmap_atomic_idx_push();
  2036. diff -Nur linux-4.1.20.orig/arch/mips/mm/init.c linux-4.1.20/arch/mips/mm/init.c
  2037. --- linux-4.1.20.orig/arch/mips/mm/init.c 2016-03-17 19:11:03.000000000 +0100
  2038. +++ linux-4.1.20/arch/mips/mm/init.c 2016-03-21 20:18:29.000000000 +0100
  2039. @@ -90,6 +90,7 @@
  2040. BUG_ON(Page_dcache_dirty(page));
  2041. + preempt_disable();
  2042. pagefault_disable();
  2043. idx = (addr >> PAGE_SHIFT) & (FIX_N_COLOURS - 1);
  2044. idx += in_interrupt() ? FIX_N_COLOURS : 0;
  2045. @@ -152,6 +153,7 @@
  2046. write_c0_entryhi(old_ctx);
  2047. local_irq_restore(flags);
  2048. pagefault_enable();
  2049. + preempt_enable();
  2050. }
  2051. void copy_user_highpage(struct page *to, struct page *from,
  2052. diff -Nur linux-4.1.20.orig/arch/mn10300/include/asm/highmem.h linux-4.1.20/arch/mn10300/include/asm/highmem.h
  2053. --- linux-4.1.20.orig/arch/mn10300/include/asm/highmem.h 2016-03-17 19:11:03.000000000 +0100
  2054. +++ linux-4.1.20/arch/mn10300/include/asm/highmem.h 2016-03-21 20:18:29.000000000 +0100
  2055. @@ -75,6 +75,7 @@
  2056. unsigned long vaddr;
  2057. int idx, type;
  2058. + preempt_disable();
  2059. pagefault_disable();
  2060. if (page < highmem_start_page)
  2061. return page_address(page);
  2062. @@ -98,6 +99,7 @@
  2063. if (vaddr < FIXADDR_START) { /* FIXME */
  2064. pagefault_enable();
  2065. + preempt_enable();
  2066. return;
  2067. }
  2068. @@ -122,6 +124,7 @@
  2069. kmap_atomic_idx_pop();
  2070. pagefault_enable();
  2071. + preempt_enable();
  2072. }
  2073. #endif /* __KERNEL__ */
  2074. diff -Nur linux-4.1.20.orig/arch/mn10300/mm/fault.c linux-4.1.20/arch/mn10300/mm/fault.c
  2075. --- linux-4.1.20.orig/arch/mn10300/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  2076. +++ linux-4.1.20/arch/mn10300/mm/fault.c 2016-03-21 20:18:29.000000000 +0100
  2077. @@ -23,8 +23,8 @@
  2078. #include <linux/interrupt.h>
  2079. #include <linux/init.h>
  2080. #include <linux/vt_kern.h> /* For unblank_screen() */
  2081. +#include <linux/uaccess.h>
  2082. -#include <asm/uaccess.h>
  2083. #include <asm/pgalloc.h>
  2084. #include <asm/hardirq.h>
  2085. #include <asm/cpu-regs.h>
  2086. @@ -168,7 +168,7 @@
  2087. * If we're in an interrupt or have no user
  2088. * context, we must not take the fault..
  2089. */
  2090. - if (in_atomic() || !mm)
  2091. + if (faulthandler_disabled() || !mm)
  2092. goto no_context;
  2093. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR)
  2094. diff -Nur linux-4.1.20.orig/arch/nios2/mm/fault.c linux-4.1.20/arch/nios2/mm/fault.c
  2095. --- linux-4.1.20.orig/arch/nios2/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  2096. +++ linux-4.1.20/arch/nios2/mm/fault.c 2016-03-21 20:18:29.000000000 +0100
  2097. @@ -77,7 +77,7 @@
  2098. * If we're in an interrupt or have no user
  2099. * context, we must not take the fault..
  2100. */
  2101. - if (in_atomic() || !mm)
  2102. + if (faulthandler_disabled() || !mm)
  2103. goto bad_area_nosemaphore;
  2104. if (user_mode(regs))
  2105. diff -Nur linux-4.1.20.orig/arch/parisc/include/asm/cacheflush.h linux-4.1.20/arch/parisc/include/asm/cacheflush.h
  2106. --- linux-4.1.20.orig/arch/parisc/include/asm/cacheflush.h 2016-03-17 19:11:03.000000000 +0100
  2107. +++ linux-4.1.20/arch/parisc/include/asm/cacheflush.h 2016-03-21 20:18:29.000000000 +0100
  2108. @@ -142,6 +142,7 @@
  2109. static inline void *kmap_atomic(struct page *page)
  2110. {
  2111. + preempt_disable();
  2112. pagefault_disable();
  2113. return page_address(page);
  2114. }
  2115. @@ -150,6 +151,7 @@
  2116. {
  2117. flush_kernel_dcache_page_addr(addr);
  2118. pagefault_enable();
  2119. + preempt_enable();
  2120. }
  2121. #define kmap_atomic_prot(page, prot) kmap_atomic(page)
  2122. diff -Nur linux-4.1.20.orig/arch/parisc/kernel/traps.c linux-4.1.20/arch/parisc/kernel/traps.c
  2123. --- linux-4.1.20.orig/arch/parisc/kernel/traps.c 2016-03-17 19:11:03.000000000 +0100
  2124. +++ linux-4.1.20/arch/parisc/kernel/traps.c 2016-03-21 20:18:29.000000000 +0100
  2125. @@ -26,9 +26,9 @@
  2126. #include <linux/console.h>
  2127. #include <linux/bug.h>
  2128. #include <linux/ratelimit.h>
  2129. +#include <linux/uaccess.h>
  2130. #include <asm/assembly.h>
  2131. -#include <asm/uaccess.h>
  2132. #include <asm/io.h>
  2133. #include <asm/irq.h>
  2134. #include <asm/traps.h>
  2135. @@ -796,7 +796,7 @@
  2136. * unless pagefault_disable() was called before.
  2137. */
  2138. - if (fault_space == 0 && !in_atomic())
  2139. + if (fault_space == 0 && !faulthandler_disabled())
  2140. {
  2141. pdc_chassis_send_status(PDC_CHASSIS_DIRECT_PANIC);
  2142. parisc_terminate("Kernel Fault", regs, code, fault_address);
  2143. diff -Nur linux-4.1.20.orig/arch/parisc/mm/fault.c linux-4.1.20/arch/parisc/mm/fault.c
  2144. --- linux-4.1.20.orig/arch/parisc/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  2145. +++ linux-4.1.20/arch/parisc/mm/fault.c 2016-03-21 20:18:29.000000000 +0100
  2146. @@ -15,8 +15,8 @@
  2147. #include <linux/sched.h>
  2148. #include <linux/interrupt.h>
  2149. #include <linux/module.h>
  2150. +#include <linux/uaccess.h>
  2151. -#include <asm/uaccess.h>
  2152. #include <asm/traps.h>
  2153. /* Various important other fields */
  2154. @@ -207,7 +207,7 @@
  2155. int fault;
  2156. unsigned int flags;
  2157. - if (in_atomic())
  2158. + if (pagefault_disabled())
  2159. goto no_context;
  2160. tsk = current;
  2161. diff -Nur linux-4.1.20.orig/arch/powerpc/Kconfig linux-4.1.20/arch/powerpc/Kconfig
  2162. --- linux-4.1.20.orig/arch/powerpc/Kconfig 2016-03-17 19:11:03.000000000 +0100
  2163. +++ linux-4.1.20/arch/powerpc/Kconfig 2016-03-21 20:18:29.000000000 +0100
  2164. @@ -60,10 +60,11 @@
  2165. config RWSEM_GENERIC_SPINLOCK
  2166. bool
  2167. + default y if PREEMPT_RT_FULL
  2168. config RWSEM_XCHGADD_ALGORITHM
  2169. bool
  2170. - default y
  2171. + default y if !PREEMPT_RT_FULL
  2172. config GENERIC_LOCKBREAK
  2173. bool
  2174. @@ -138,6 +139,7 @@
  2175. select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
  2176. select GENERIC_STRNCPY_FROM_USER
  2177. select GENERIC_STRNLEN_USER
  2178. + select HAVE_PREEMPT_LAZY
  2179. select HAVE_MOD_ARCH_SPECIFIC
  2180. select MODULES_USE_ELF_RELA
  2181. select CLONE_BACKWARDS
  2182. @@ -312,7 +314,7 @@
  2183. config HIGHMEM
  2184. bool "High memory support"
  2185. - depends on PPC32
  2186. + depends on PPC32 && !PREEMPT_RT_FULL
  2187. source kernel/Kconfig.hz
  2188. source kernel/Kconfig.preempt
  2189. diff -Nur linux-4.1.20.orig/arch/powerpc/include/asm/kvm_host.h linux-4.1.20/arch/powerpc/include/asm/kvm_host.h
  2190. --- linux-4.1.20.orig/arch/powerpc/include/asm/kvm_host.h 2016-03-17 19:11:03.000000000 +0100
  2191. +++ linux-4.1.20/arch/powerpc/include/asm/kvm_host.h 2016-03-21 20:18:29.000000000 +0100
  2192. @@ -280,7 +280,7 @@
  2193. u8 in_guest;
  2194. struct list_head runnable_threads;
  2195. spinlock_t lock;
  2196. - wait_queue_head_t wq;
  2197. + struct swait_head wq;
  2198. spinlock_t stoltb_lock; /* protects stolen_tb and preempt_tb */
  2199. u64 stolen_tb;
  2200. u64 preempt_tb;
  2201. @@ -613,7 +613,7 @@
  2202. u8 prodded;
  2203. u32 last_inst;
  2204. - wait_queue_head_t *wqp;
  2205. + struct swait_head *wqp;
  2206. struct kvmppc_vcore *vcore;
  2207. int ret;
  2208. int trap;
  2209. diff -Nur linux-4.1.20.orig/arch/powerpc/include/asm/thread_info.h linux-4.1.20/arch/powerpc/include/asm/thread_info.h
  2210. --- linux-4.1.20.orig/arch/powerpc/include/asm/thread_info.h 2016-03-17 19:11:03.000000000 +0100
  2211. +++ linux-4.1.20/arch/powerpc/include/asm/thread_info.h 2016-03-21 20:18:29.000000000 +0100
  2212. @@ -42,6 +42,8 @@
  2213. int cpu; /* cpu we're on */
  2214. int preempt_count; /* 0 => preemptable,
  2215. <0 => BUG */
  2216. + int preempt_lazy_count; /* 0 => preemptable,
  2217. + <0 => BUG */
  2218. unsigned long local_flags; /* private flags for thread */
  2219. /* low level flags - has atomic operations done on it */
  2220. @@ -82,8 +84,7 @@
  2221. #define TIF_SYSCALL_TRACE 0 /* syscall trace active */
  2222. #define TIF_SIGPENDING 1 /* signal pending */
  2223. #define TIF_NEED_RESCHED 2 /* rescheduling necessary */
  2224. -#define TIF_POLLING_NRFLAG 3 /* true if poll_idle() is polling
  2225. - TIF_NEED_RESCHED */
  2226. +#define TIF_NEED_RESCHED_LAZY 3 /* lazy rescheduling necessary */
  2227. #define TIF_32BIT 4 /* 32 bit binary */
  2228. #define TIF_RESTORE_TM 5 /* need to restore TM FP/VEC/VSX */
  2229. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  2230. @@ -101,6 +102,8 @@
  2231. #if defined(CONFIG_PPC64)
  2232. #define TIF_ELF2ABI 18 /* function descriptors must die! */
  2233. #endif
  2234. +#define TIF_POLLING_NRFLAG 19 /* true if poll_idle() is polling
  2235. + TIF_NEED_RESCHED */
  2236. /* as above, but as bit values */
  2237. #define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
  2238. @@ -119,14 +122,16 @@
  2239. #define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
  2240. #define _TIF_EMULATE_STACK_STORE (1<<TIF_EMULATE_STACK_STORE)
  2241. #define _TIF_NOHZ (1<<TIF_NOHZ)
  2242. +#define _TIF_NEED_RESCHED_LAZY (1<<TIF_NEED_RESCHED_LAZY)
  2243. #define _TIF_SYSCALL_DOTRACE (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  2244. _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT | \
  2245. _TIF_NOHZ)
  2246. #define _TIF_USER_WORK_MASK (_TIF_SIGPENDING | _TIF_NEED_RESCHED | \
  2247. _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  2248. - _TIF_RESTORE_TM)
  2249. + _TIF_RESTORE_TM | _TIF_NEED_RESCHED_LAZY)
  2250. #define _TIF_PERSYSCALL_MASK (_TIF_RESTOREALL|_TIF_NOERROR)
  2251. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  2252. /* Bits in local_flags */
  2253. /* Don't move TLF_NAPPING without adjusting the code in entry_32.S */
  2254. diff -Nur linux-4.1.20.orig/arch/powerpc/kernel/asm-offsets.c linux-4.1.20/arch/powerpc/kernel/asm-offsets.c
  2255. --- linux-4.1.20.orig/arch/powerpc/kernel/asm-offsets.c 2016-03-17 19:11:03.000000000 +0100
  2256. +++ linux-4.1.20/arch/powerpc/kernel/asm-offsets.c 2016-03-21 20:18:29.000000000 +0100
  2257. @@ -160,6 +160,7 @@
  2258. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  2259. DEFINE(TI_LOCAL_FLAGS, offsetof(struct thread_info, local_flags));
  2260. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  2261. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  2262. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  2263. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  2264. diff -Nur linux-4.1.20.orig/arch/powerpc/kernel/entry_32.S linux-4.1.20/arch/powerpc/kernel/entry_32.S
  2265. --- linux-4.1.20.orig/arch/powerpc/kernel/entry_32.S 2016-03-17 19:11:03.000000000 +0100
  2266. +++ linux-4.1.20/arch/powerpc/kernel/entry_32.S 2016-03-21 20:18:29.000000000 +0100
  2267. @@ -813,7 +813,14 @@
  2268. cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  2269. bne restore
  2270. andi. r8,r8,_TIF_NEED_RESCHED
  2271. + bne+ 1f
  2272. + lwz r0,TI_PREEMPT_LAZY(r9)
  2273. + cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  2274. + bne restore
  2275. + lwz r0,TI_FLAGS(r9)
  2276. + andi. r0,r0,_TIF_NEED_RESCHED_LAZY
  2277. beq+ restore
  2278. +1:
  2279. lwz r3,_MSR(r1)
  2280. andi. r0,r3,MSR_EE /* interrupts off? */
  2281. beq restore /* don't schedule if so */
  2282. @@ -824,11 +831,11 @@
  2283. */
  2284. bl trace_hardirqs_off
  2285. #endif
  2286. -1: bl preempt_schedule_irq
  2287. +2: bl preempt_schedule_irq
  2288. CURRENT_THREAD_INFO(r9, r1)
  2289. lwz r3,TI_FLAGS(r9)
  2290. - andi. r0,r3,_TIF_NEED_RESCHED
  2291. - bne- 1b
  2292. + andi. r0,r3,_TIF_NEED_RESCHED_MASK
  2293. + bne- 2b
  2294. #ifdef CONFIG_TRACE_IRQFLAGS
  2295. /* And now, to properly rebalance the above, we tell lockdep they
  2296. * are being turned back on, which will happen when we return
  2297. @@ -1149,7 +1156,7 @@
  2298. #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
  2299. do_work: /* r10 contains MSR_KERNEL here */
  2300. - andi. r0,r9,_TIF_NEED_RESCHED
  2301. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  2302. beq do_user_signal
  2303. do_resched: /* r10 contains MSR_KERNEL here */
  2304. @@ -1170,7 +1177,7 @@
  2305. MTMSRD(r10) /* disable interrupts */
  2306. CURRENT_THREAD_INFO(r9, r1)
  2307. lwz r9,TI_FLAGS(r9)
  2308. - andi. r0,r9,_TIF_NEED_RESCHED
  2309. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  2310. bne- do_resched
  2311. andi. r0,r9,_TIF_USER_WORK_MASK
  2312. beq restore_user
  2313. diff -Nur linux-4.1.20.orig/arch/powerpc/kernel/entry_64.S linux-4.1.20/arch/powerpc/kernel/entry_64.S
  2314. --- linux-4.1.20.orig/arch/powerpc/kernel/entry_64.S 2016-03-17 19:11:03.000000000 +0100
  2315. +++ linux-4.1.20/arch/powerpc/kernel/entry_64.S 2016-03-21 20:18:29.000000000 +0100
  2316. @@ -636,7 +636,7 @@
  2317. #else
  2318. beq restore
  2319. #endif
  2320. -1: andi. r0,r4,_TIF_NEED_RESCHED
  2321. +1: andi. r0,r4,_TIF_NEED_RESCHED_MASK
  2322. beq 2f
  2323. bl restore_interrupts
  2324. SCHEDULE_USER
  2325. @@ -698,10 +698,18 @@
  2326. #ifdef CONFIG_PREEMPT
  2327. /* Check if we need to preempt */
  2328. + lwz r8,TI_PREEMPT(r9)
  2329. + cmpwi 0,r8,0 /* if non-zero, just restore regs and return */
  2330. + bne restore
  2331. andi. r0,r4,_TIF_NEED_RESCHED
  2332. + bne+ check_count
  2333. +
  2334. + andi. r0,r4,_TIF_NEED_RESCHED_LAZY
  2335. beq+ restore
  2336. + lwz r8,TI_PREEMPT_LAZY(r9)
  2337. +
  2338. /* Check that preempt_count() == 0 and interrupts are enabled */
  2339. - lwz r8,TI_PREEMPT(r9)
  2340. +check_count:
  2341. cmpwi cr1,r8,0
  2342. ld r0,SOFTE(r1)
  2343. cmpdi r0,0
  2344. @@ -718,7 +726,7 @@
  2345. /* Re-test flags and eventually loop */
  2346. CURRENT_THREAD_INFO(r9, r1)
  2347. ld r4,TI_FLAGS(r9)
  2348. - andi. r0,r4,_TIF_NEED_RESCHED
  2349. + andi. r0,r4,_TIF_NEED_RESCHED_MASK
  2350. bne 1b
  2351. /*
  2352. diff -Nur linux-4.1.20.orig/arch/powerpc/kernel/irq.c linux-4.1.20/arch/powerpc/kernel/irq.c
  2353. --- linux-4.1.20.orig/arch/powerpc/kernel/irq.c 2016-03-17 19:11:03.000000000 +0100
  2354. +++ linux-4.1.20/arch/powerpc/kernel/irq.c 2016-03-21 20:18:30.000000000 +0100
  2355. @@ -614,6 +614,7 @@
  2356. }
  2357. }
  2358. +#ifndef CONFIG_PREEMPT_RT_FULL
  2359. void do_softirq_own_stack(void)
  2360. {
  2361. struct thread_info *curtp, *irqtp;
  2362. @@ -631,6 +632,7 @@
  2363. if (irqtp->flags)
  2364. set_bits(irqtp->flags, &curtp->flags);
  2365. }
  2366. +#endif
  2367. irq_hw_number_t virq_to_hw(unsigned int virq)
  2368. {
  2369. diff -Nur linux-4.1.20.orig/arch/powerpc/kernel/misc_32.S linux-4.1.20/arch/powerpc/kernel/misc_32.S
  2370. --- linux-4.1.20.orig/arch/powerpc/kernel/misc_32.S 2016-03-17 19:11:03.000000000 +0100
  2371. +++ linux-4.1.20/arch/powerpc/kernel/misc_32.S 2016-03-21 20:18:30.000000000 +0100
  2372. @@ -40,6 +40,7 @@
  2373. * We store the saved ksp_limit in the unused part
  2374. * of the STACK_FRAME_OVERHEAD
  2375. */
  2376. +#ifndef CONFIG_PREEMPT_RT_FULL
  2377. _GLOBAL(call_do_softirq)
  2378. mflr r0
  2379. stw r0,4(r1)
  2380. @@ -56,6 +57,7 @@
  2381. stw r10,THREAD+KSP_LIMIT(r2)
  2382. mtlr r0
  2383. blr
  2384. +#endif
  2385. /*
  2386. * void call_do_irq(struct pt_regs *regs, struct thread_info *irqtp);
  2387. diff -Nur linux-4.1.20.orig/arch/powerpc/kernel/misc_64.S linux-4.1.20/arch/powerpc/kernel/misc_64.S
  2388. --- linux-4.1.20.orig/arch/powerpc/kernel/misc_64.S 2016-03-17 19:11:03.000000000 +0100
  2389. +++ linux-4.1.20/arch/powerpc/kernel/misc_64.S 2016-03-21 20:18:30.000000000 +0100
  2390. @@ -29,6 +29,7 @@
  2391. .text
  2392. +#ifndef CONFIG_PREEMPT_RT_FULL
  2393. _GLOBAL(call_do_softirq)
  2394. mflr r0
  2395. std r0,16(r1)
  2396. @@ -39,6 +40,7 @@
  2397. ld r0,16(r1)
  2398. mtlr r0
  2399. blr
  2400. +#endif
  2401. _GLOBAL(call_do_irq)
  2402. mflr r0
  2403. diff -Nur linux-4.1.20.orig/arch/powerpc/kvm/Kconfig linux-4.1.20/arch/powerpc/kvm/Kconfig
  2404. --- linux-4.1.20.orig/arch/powerpc/kvm/Kconfig 2016-03-17 19:11:03.000000000 +0100
  2405. +++ linux-4.1.20/arch/powerpc/kvm/Kconfig 2016-03-21 20:18:30.000000000 +0100
  2406. @@ -172,6 +172,7 @@
  2407. config KVM_MPIC
  2408. bool "KVM in-kernel MPIC emulation"
  2409. depends on KVM && E500
  2410. + depends on !PREEMPT_RT_FULL
  2411. select HAVE_KVM_IRQCHIP
  2412. select HAVE_KVM_IRQFD
  2413. select HAVE_KVM_IRQ_ROUTING
  2414. diff -Nur linux-4.1.20.orig/arch/powerpc/kvm/book3s_hv.c linux-4.1.20/arch/powerpc/kvm/book3s_hv.c
  2415. --- linux-4.1.20.orig/arch/powerpc/kvm/book3s_hv.c 2016-03-17 19:11:03.000000000 +0100
  2416. +++ linux-4.1.20/arch/powerpc/kvm/book3s_hv.c 2016-03-21 20:18:30.000000000 +0100
  2417. @@ -115,11 +115,11 @@
  2418. static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
  2419. {
  2420. int cpu = vcpu->cpu;
  2421. - wait_queue_head_t *wqp;
  2422. + struct swait_head *wqp;
  2423. wqp = kvm_arch_vcpu_wq(vcpu);
  2424. - if (waitqueue_active(wqp)) {
  2425. - wake_up_interruptible(wqp);
  2426. + if (swaitqueue_active(wqp)) {
  2427. + swait_wake_interruptible(wqp);
  2428. ++vcpu->stat.halt_wakeup;
  2429. }
  2430. @@ -692,8 +692,8 @@
  2431. tvcpu->arch.prodded = 1;
  2432. smp_mb();
  2433. if (vcpu->arch.ceded) {
  2434. - if (waitqueue_active(&vcpu->wq)) {
  2435. - wake_up_interruptible(&vcpu->wq);
  2436. + if (swaitqueue_active(&vcpu->wq)) {
  2437. + swait_wake_interruptible(&vcpu->wq);
  2438. vcpu->stat.halt_wakeup++;
  2439. }
  2440. }
  2441. @@ -1432,7 +1432,7 @@
  2442. INIT_LIST_HEAD(&vcore->runnable_threads);
  2443. spin_lock_init(&vcore->lock);
  2444. spin_lock_init(&vcore->stoltb_lock);
  2445. - init_waitqueue_head(&vcore->wq);
  2446. + init_swait_head(&vcore->wq);
  2447. vcore->preempt_tb = TB_NIL;
  2448. vcore->lpcr = kvm->arch.lpcr;
  2449. vcore->first_vcpuid = core * threads_per_subcore;
  2450. @@ -2079,10 +2079,9 @@
  2451. {
  2452. struct kvm_vcpu *vcpu;
  2453. int do_sleep = 1;
  2454. + DEFINE_SWAITER(wait);
  2455. - DEFINE_WAIT(wait);
  2456. -
  2457. - prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  2458. + swait_prepare(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  2459. /*
  2460. * Check one last time for pending exceptions and ceded state after
  2461. @@ -2096,7 +2095,7 @@
  2462. }
  2463. if (!do_sleep) {
  2464. - finish_wait(&vc->wq, &wait);
  2465. + swait_finish(&vc->wq, &wait);
  2466. return;
  2467. }
  2468. @@ -2104,7 +2103,7 @@
  2469. trace_kvmppc_vcore_blocked(vc, 0);
  2470. spin_unlock(&vc->lock);
  2471. schedule();
  2472. - finish_wait(&vc->wq, &wait);
  2473. + swait_finish(&vc->wq, &wait);
  2474. spin_lock(&vc->lock);
  2475. vc->vcore_state = VCORE_INACTIVE;
  2476. trace_kvmppc_vcore_blocked(vc, 1);
  2477. @@ -2148,7 +2147,7 @@
  2478. kvmppc_start_thread(vcpu);
  2479. trace_kvm_guest_enter(vcpu);
  2480. } else if (vc->vcore_state == VCORE_SLEEPING) {
  2481. - wake_up(&vc->wq);
  2482. + swait_wake(&vc->wq);
  2483. }
  2484. }
  2485. diff -Nur linux-4.1.20.orig/arch/powerpc/mm/fault.c linux-4.1.20/arch/powerpc/mm/fault.c
  2486. --- linux-4.1.20.orig/arch/powerpc/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  2487. +++ linux-4.1.20/arch/powerpc/mm/fault.c 2016-03-21 20:18:30.000000000 +0100
  2488. @@ -33,13 +33,13 @@
  2489. #include <linux/ratelimit.h>
  2490. #include <linux/context_tracking.h>
  2491. #include <linux/hugetlb.h>
  2492. +#include <linux/uaccess.h>
  2493. #include <asm/firmware.h>
  2494. #include <asm/page.h>
  2495. #include <asm/pgtable.h>
  2496. #include <asm/mmu.h>
  2497. #include <asm/mmu_context.h>
  2498. -#include <asm/uaccess.h>
  2499. #include <asm/tlbflush.h>
  2500. #include <asm/siginfo.h>
  2501. #include <asm/debug.h>
  2502. @@ -272,15 +272,16 @@
  2503. if (!arch_irq_disabled_regs(regs))
  2504. local_irq_enable();
  2505. - if (in_atomic() || mm == NULL) {
  2506. + if (faulthandler_disabled() || mm == NULL) {
  2507. if (!user_mode(regs)) {
  2508. rc = SIGSEGV;
  2509. goto bail;
  2510. }
  2511. - /* in_atomic() in user mode is really bad,
  2512. + /* faulthandler_disabled() in user mode is really bad,
  2513. as is current->mm == NULL. */
  2514. printk(KERN_EMERG "Page fault in user mode with "
  2515. - "in_atomic() = %d mm = %p\n", in_atomic(), mm);
  2516. + "faulthandler_disabled() = %d mm = %p\n",
  2517. + faulthandler_disabled(), mm);
  2518. printk(KERN_EMERG "NIP = %lx MSR = %lx\n",
  2519. regs->nip, regs->msr);
  2520. die("Weird page fault", regs, SIGSEGV);
  2521. diff -Nur linux-4.1.20.orig/arch/powerpc/mm/highmem.c linux-4.1.20/arch/powerpc/mm/highmem.c
  2522. --- linux-4.1.20.orig/arch/powerpc/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  2523. +++ linux-4.1.20/arch/powerpc/mm/highmem.c 2016-03-21 20:18:30.000000000 +0100
  2524. @@ -34,7 +34,7 @@
  2525. unsigned long vaddr;
  2526. int idx, type;
  2527. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  2528. + preempt_disable();
  2529. pagefault_disable();
  2530. if (!PageHighMem(page))
  2531. return page_address(page);
  2532. @@ -59,6 +59,7 @@
  2533. if (vaddr < __fix_to_virt(FIX_KMAP_END)) {
  2534. pagefault_enable();
  2535. + preempt_enable();
  2536. return;
  2537. }
  2538. @@ -82,5 +83,6 @@
  2539. kmap_atomic_idx_pop();
  2540. pagefault_enable();
  2541. + preempt_enable();
  2542. }
  2543. EXPORT_SYMBOL(__kunmap_atomic);
  2544. diff -Nur linux-4.1.20.orig/arch/powerpc/platforms/ps3/device-init.c linux-4.1.20/arch/powerpc/platforms/ps3/device-init.c
  2545. --- linux-4.1.20.orig/arch/powerpc/platforms/ps3/device-init.c 2016-03-17 19:11:03.000000000 +0100
  2546. +++ linux-4.1.20/arch/powerpc/platforms/ps3/device-init.c 2016-03-21 20:18:30.000000000 +0100
  2547. @@ -752,7 +752,7 @@
  2548. }
  2549. pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
  2550. - res = wait_event_interruptible(dev->done.wait,
  2551. + res = swait_event_interruptible(dev->done.wait,
  2552. dev->done.done || kthread_should_stop());
  2553. if (kthread_should_stop())
  2554. res = -EINTR;
  2555. diff -Nur linux-4.1.20.orig/arch/s390/include/asm/kvm_host.h linux-4.1.20/arch/s390/include/asm/kvm_host.h
  2556. --- linux-4.1.20.orig/arch/s390/include/asm/kvm_host.h 2016-03-17 19:11:03.000000000 +0100
  2557. +++ linux-4.1.20/arch/s390/include/asm/kvm_host.h 2016-03-21 20:18:30.000000000 +0100
  2558. @@ -419,7 +419,7 @@
  2559. struct kvm_s390_local_interrupt {
  2560. spinlock_t lock;
  2561. struct kvm_s390_float_interrupt *float_int;
  2562. - wait_queue_head_t *wq;
  2563. + struct swait_head *wq;
  2564. atomic_t *cpuflags;
  2565. DECLARE_BITMAP(sigp_emerg_pending, KVM_MAX_VCPUS);
  2566. struct kvm_s390_irq_payload irq;
  2567. diff -Nur linux-4.1.20.orig/arch/s390/include/asm/uaccess.h linux-4.1.20/arch/s390/include/asm/uaccess.h
  2568. --- linux-4.1.20.orig/arch/s390/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  2569. +++ linux-4.1.20/arch/s390/include/asm/uaccess.h 2016-03-21 20:18:30.000000000 +0100
  2570. @@ -98,7 +98,8 @@
  2571. * @from: Source address, in user space.
  2572. * @n: Number of bytes to copy.
  2573. *
  2574. - * Context: User context only. This function may sleep.
  2575. + * Context: User context only. This function may sleep if pagefaults are
  2576. + * enabled.
  2577. *
  2578. * Copy data from user space to kernel space. Caller must check
  2579. * the specified block with access_ok() before calling this function.
  2580. @@ -118,7 +119,8 @@
  2581. * @from: Source address, in kernel space.
  2582. * @n: Number of bytes to copy.
  2583. *
  2584. - * Context: User context only. This function may sleep.
  2585. + * Context: User context only. This function may sleep if pagefaults are
  2586. + * enabled.
  2587. *
  2588. * Copy data from kernel space to user space. Caller must check
  2589. * the specified block with access_ok() before calling this function.
  2590. @@ -264,7 +266,8 @@
  2591. * @from: Source address, in kernel space.
  2592. * @n: Number of bytes to copy.
  2593. *
  2594. - * Context: User context only. This function may sleep.
  2595. + * Context: User context only. This function may sleep if pagefaults are
  2596. + * enabled.
  2597. *
  2598. * Copy data from kernel space to user space.
  2599. *
  2600. @@ -290,7 +293,8 @@
  2601. * @from: Source address, in user space.
  2602. * @n: Number of bytes to copy.
  2603. *
  2604. - * Context: User context only. This function may sleep.
  2605. + * Context: User context only. This function may sleep if pagefaults are
  2606. + * enabled.
  2607. *
  2608. * Copy data from user space to kernel space.
  2609. *
  2610. @@ -348,7 +352,8 @@
  2611. * strlen_user: - Get the size of a string in user space.
  2612. * @str: The string to measure.
  2613. *
  2614. - * Context: User context only. This function may sleep.
  2615. + * Context: User context only. This function may sleep if pagefaults are
  2616. + * enabled.
  2617. *
  2618. * Get the size of a NUL-terminated string in user space.
  2619. *
  2620. diff -Nur linux-4.1.20.orig/arch/s390/kvm/interrupt.c linux-4.1.20/arch/s390/kvm/interrupt.c
  2621. --- linux-4.1.20.orig/arch/s390/kvm/interrupt.c 2016-03-17 19:11:03.000000000 +0100
  2622. +++ linux-4.1.20/arch/s390/kvm/interrupt.c 2016-03-21 20:18:30.000000000 +0100
  2623. @@ -875,13 +875,13 @@
  2624. void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
  2625. {
  2626. - if (waitqueue_active(&vcpu->wq)) {
  2627. + if (swaitqueue_active(&vcpu->wq)) {
  2628. /*
  2629. * The vcpu gave up the cpu voluntarily, mark it as a good
  2630. * yield-candidate.
  2631. */
  2632. vcpu->preempted = true;
  2633. - wake_up_interruptible(&vcpu->wq);
  2634. + swait_wake_interruptible(&vcpu->wq);
  2635. vcpu->stat.halt_wakeup++;
  2636. }
  2637. }
  2638. @@ -987,7 +987,7 @@
  2639. spin_lock(&li->lock);
  2640. irq.u.pgm.code = code;
  2641. __inject_prog(vcpu, &irq);
  2642. - BUG_ON(waitqueue_active(li->wq));
  2643. + BUG_ON(swaitqueue_active(li->wq));
  2644. spin_unlock(&li->lock);
  2645. return 0;
  2646. }
  2647. @@ -1006,7 +1006,7 @@
  2648. spin_lock(&li->lock);
  2649. irq.u.pgm = *pgm_info;
  2650. rc = __inject_prog(vcpu, &irq);
  2651. - BUG_ON(waitqueue_active(li->wq));
  2652. + BUG_ON(swaitqueue_active(li->wq));
  2653. spin_unlock(&li->lock);
  2654. return rc;
  2655. }
  2656. diff -Nur linux-4.1.20.orig/arch/s390/mm/fault.c linux-4.1.20/arch/s390/mm/fault.c
  2657. --- linux-4.1.20.orig/arch/s390/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  2658. +++ linux-4.1.20/arch/s390/mm/fault.c 2016-03-21 20:18:30.000000000 +0100
  2659. @@ -399,7 +399,7 @@
  2660. * user context.
  2661. */
  2662. fault = VM_FAULT_BADCONTEXT;
  2663. - if (unlikely(!user_space_fault(regs) || in_atomic() || !mm))
  2664. + if (unlikely(!user_space_fault(regs) || faulthandler_disabled() || !mm))
  2665. goto out;
  2666. address = trans_exc_code & __FAIL_ADDR_MASK;
  2667. diff -Nur linux-4.1.20.orig/arch/score/include/asm/uaccess.h linux-4.1.20/arch/score/include/asm/uaccess.h
  2668. --- linux-4.1.20.orig/arch/score/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  2669. +++ linux-4.1.20/arch/score/include/asm/uaccess.h 2016-03-21 20:18:30.000000000 +0100
  2670. @@ -36,7 +36,8 @@
  2671. * @addr: User space pointer to start of block to check
  2672. * @size: Size of block to check
  2673. *
  2674. - * Context: User context only. This function may sleep.
  2675. + * Context: User context only. This function may sleep if pagefaults are
  2676. + * enabled.
  2677. *
  2678. * Checks if a pointer to a block of memory in user space is valid.
  2679. *
  2680. @@ -61,7 +62,8 @@
  2681. * @x: Value to copy to user space.
  2682. * @ptr: Destination address, in user space.
  2683. *
  2684. - * Context: User context only. This function may sleep.
  2685. + * Context: User context only. This function may sleep if pagefaults are
  2686. + * enabled.
  2687. *
  2688. * This macro copies a single simple value from kernel space to user
  2689. * space. It supports simple types like char and int, but not larger
  2690. @@ -79,7 +81,8 @@
  2691. * @x: Variable to store result.
  2692. * @ptr: Source address, in user space.
  2693. *
  2694. - * Context: User context only. This function may sleep.
  2695. + * Context: User context only. This function may sleep if pagefaults are
  2696. + * enabled.
  2697. *
  2698. * This macro copies a single simple variable from user space to kernel
  2699. * space. It supports simple types like char and int, but not larger
  2700. @@ -98,7 +101,8 @@
  2701. * @x: Value to copy to user space.
  2702. * @ptr: Destination address, in user space.
  2703. *
  2704. - * Context: User context only. This function may sleep.
  2705. + * Context: User context only. This function may sleep if pagefaults are
  2706. + * enabled.
  2707. *
  2708. * This macro copies a single simple value from kernel space to user
  2709. * space. It supports simple types like char and int, but not larger
  2710. @@ -119,7 +123,8 @@
  2711. * @x: Variable to store result.
  2712. * @ptr: Source address, in user space.
  2713. *
  2714. - * Context: User context only. This function may sleep.
  2715. + * Context: User context only. This function may sleep if pagefaults are
  2716. + * enabled.
  2717. *
  2718. * This macro copies a single simple variable from user space to kernel
  2719. * space. It supports simple types like char and int, but not larger
  2720. diff -Nur linux-4.1.20.orig/arch/score/mm/fault.c linux-4.1.20/arch/score/mm/fault.c
  2721. --- linux-4.1.20.orig/arch/score/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  2722. +++ linux-4.1.20/arch/score/mm/fault.c 2016-03-21 20:18:30.000000000 +0100
  2723. @@ -34,6 +34,7 @@
  2724. #include <linux/string.h>
  2725. #include <linux/types.h>
  2726. #include <linux/ptrace.h>
  2727. +#include <linux/uaccess.h>
  2728. /*
  2729. * This routine handles page faults. It determines the address,
  2730. @@ -73,7 +74,7 @@
  2731. * If we're in an interrupt or have no user
  2732. * context, we must not take the fault..
  2733. */
  2734. - if (in_atomic() || !mm)
  2735. + if (pagefault_disabled() || !mm)
  2736. goto bad_area_nosemaphore;
  2737. if (user_mode(regs))
  2738. diff -Nur linux-4.1.20.orig/arch/sh/kernel/irq.c linux-4.1.20/arch/sh/kernel/irq.c
  2739. --- linux-4.1.20.orig/arch/sh/kernel/irq.c 2016-03-17 19:11:03.000000000 +0100
  2740. +++ linux-4.1.20/arch/sh/kernel/irq.c 2016-03-21 20:18:30.000000000 +0100
  2741. @@ -147,6 +147,7 @@
  2742. hardirq_ctx[cpu] = NULL;
  2743. }
  2744. +#ifndef CONFIG_PREEMPT_RT_FULL
  2745. void do_softirq_own_stack(void)
  2746. {
  2747. struct thread_info *curctx;
  2748. @@ -174,6 +175,7 @@
  2749. "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
  2750. );
  2751. }
  2752. +#endif
  2753. #else
  2754. static inline void handle_one_irq(unsigned int irq)
  2755. {
  2756. diff -Nur linux-4.1.20.orig/arch/sh/mm/fault.c linux-4.1.20/arch/sh/mm/fault.c
  2757. --- linux-4.1.20.orig/arch/sh/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  2758. +++ linux-4.1.20/arch/sh/mm/fault.c 2016-03-21 20:18:30.000000000 +0100
  2759. @@ -17,6 +17,7 @@
  2760. #include <linux/kprobes.h>
  2761. #include <linux/perf_event.h>
  2762. #include <linux/kdebug.h>
  2763. +#include <linux/uaccess.h>
  2764. #include <asm/io_trapped.h>
  2765. #include <asm/mmu_context.h>
  2766. #include <asm/tlbflush.h>
  2767. @@ -438,9 +439,9 @@
  2768. /*
  2769. * If we're in an interrupt, have no user context or are running
  2770. - * in an atomic region then we must not take the fault:
  2771. + * with pagefaults disabled then we must not take the fault:
  2772. */
  2773. - if (unlikely(in_atomic() || !mm)) {
  2774. + if (unlikely(faulthandler_disabled() || !mm)) {
  2775. bad_area_nosemaphore(regs, error_code, address);
  2776. return;
  2777. }
  2778. diff -Nur linux-4.1.20.orig/arch/sparc/Kconfig linux-4.1.20/arch/sparc/Kconfig
  2779. --- linux-4.1.20.orig/arch/sparc/Kconfig 2016-03-17 19:11:03.000000000 +0100
  2780. +++ linux-4.1.20/arch/sparc/Kconfig 2016-03-21 20:18:30.000000000 +0100
  2781. @@ -189,12 +189,10 @@
  2782. source kernel/Kconfig.hz
  2783. config RWSEM_GENERIC_SPINLOCK
  2784. - bool
  2785. - default y if SPARC32
  2786. + def_bool PREEMPT_RT_FULL
  2787. config RWSEM_XCHGADD_ALGORITHM
  2788. - bool
  2789. - default y if SPARC64
  2790. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  2791. config GENERIC_HWEIGHT
  2792. bool
  2793. diff -Nur linux-4.1.20.orig/arch/sparc/kernel/irq_64.c linux-4.1.20/arch/sparc/kernel/irq_64.c
  2794. --- linux-4.1.20.orig/arch/sparc/kernel/irq_64.c 2016-03-17 19:11:03.000000000 +0100
  2795. +++ linux-4.1.20/arch/sparc/kernel/irq_64.c 2016-03-21 20:18:30.000000000 +0100
  2796. @@ -849,6 +849,7 @@
  2797. set_irq_regs(old_regs);
  2798. }
  2799. +#ifndef CONFIG_PREEMPT_RT_FULL
  2800. void do_softirq_own_stack(void)
  2801. {
  2802. void *orig_sp, *sp = softirq_stack[smp_processor_id()];
  2803. @@ -863,6 +864,7 @@
  2804. __asm__ __volatile__("mov %0, %%sp"
  2805. : : "r" (orig_sp));
  2806. }
  2807. +#endif
  2808. #ifdef CONFIG_HOTPLUG_CPU
  2809. void fixup_irqs(void)
  2810. diff -Nur linux-4.1.20.orig/arch/sparc/mm/fault_32.c linux-4.1.20/arch/sparc/mm/fault_32.c
  2811. --- linux-4.1.20.orig/arch/sparc/mm/fault_32.c 2016-03-17 19:11:03.000000000 +0100
  2812. +++ linux-4.1.20/arch/sparc/mm/fault_32.c 2016-03-21 20:18:30.000000000 +0100
  2813. @@ -21,6 +21,7 @@
  2814. #include <linux/perf_event.h>
  2815. #include <linux/interrupt.h>
  2816. #include <linux/kdebug.h>
  2817. +#include <linux/uaccess.h>
  2818. #include <asm/page.h>
  2819. #include <asm/pgtable.h>
  2820. @@ -29,7 +30,6 @@
  2821. #include <asm/setup.h>
  2822. #include <asm/smp.h>
  2823. #include <asm/traps.h>
  2824. -#include <asm/uaccess.h>
  2825. #include "mm_32.h"
  2826. @@ -196,7 +196,7 @@
  2827. * If we're in an interrupt or have no user
  2828. * context, we must not take the fault..
  2829. */
  2830. - if (in_atomic() || !mm)
  2831. + if (pagefault_disabled() || !mm)
  2832. goto no_context;
  2833. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  2834. diff -Nur linux-4.1.20.orig/arch/sparc/mm/fault_64.c linux-4.1.20/arch/sparc/mm/fault_64.c
  2835. --- linux-4.1.20.orig/arch/sparc/mm/fault_64.c 2016-03-17 19:11:03.000000000 +0100
  2836. +++ linux-4.1.20/arch/sparc/mm/fault_64.c 2016-03-21 20:18:30.000000000 +0100
  2837. @@ -22,12 +22,12 @@
  2838. #include <linux/kdebug.h>
  2839. #include <linux/percpu.h>
  2840. #include <linux/context_tracking.h>
  2841. +#include <linux/uaccess.h>
  2842. #include <asm/page.h>
  2843. #include <asm/pgtable.h>
  2844. #include <asm/openprom.h>
  2845. #include <asm/oplib.h>
  2846. -#include <asm/uaccess.h>
  2847. #include <asm/asi.h>
  2848. #include <asm/lsu.h>
  2849. #include <asm/sections.h>
  2850. @@ -330,7 +330,7 @@
  2851. * If we're in an interrupt or have no user
  2852. * context, we must not take the fault..
  2853. */
  2854. - if (in_atomic() || !mm)
  2855. + if (faulthandler_disabled() || !mm)
  2856. goto intr_or_no_mm;
  2857. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  2858. diff -Nur linux-4.1.20.orig/arch/sparc/mm/highmem.c linux-4.1.20/arch/sparc/mm/highmem.c
  2859. --- linux-4.1.20.orig/arch/sparc/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  2860. +++ linux-4.1.20/arch/sparc/mm/highmem.c 2016-03-21 20:18:30.000000000 +0100
  2861. @@ -53,7 +53,7 @@
  2862. unsigned long vaddr;
  2863. long idx, type;
  2864. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  2865. + preempt_disable();
  2866. pagefault_disable();
  2867. if (!PageHighMem(page))
  2868. return page_address(page);
  2869. @@ -91,6 +91,7 @@
  2870. if (vaddr < FIXADDR_START) { // FIXME
  2871. pagefault_enable();
  2872. + preempt_enable();
  2873. return;
  2874. }
  2875. @@ -126,5 +127,6 @@
  2876. kmap_atomic_idx_pop();
  2877. pagefault_enable();
  2878. + preempt_enable();
  2879. }
  2880. EXPORT_SYMBOL(__kunmap_atomic);
  2881. diff -Nur linux-4.1.20.orig/arch/sparc/mm/init_64.c linux-4.1.20/arch/sparc/mm/init_64.c
  2882. --- linux-4.1.20.orig/arch/sparc/mm/init_64.c 2016-03-17 19:11:03.000000000 +0100
  2883. +++ linux-4.1.20/arch/sparc/mm/init_64.c 2016-03-21 20:18:30.000000000 +0100
  2884. @@ -2738,7 +2738,7 @@
  2885. struct mm_struct *mm = current->mm;
  2886. struct tsb_config *tp;
  2887. - if (in_atomic() || !mm) {
  2888. + if (faulthandler_disabled() || !mm) {
  2889. const struct exception_table_entry *entry;
  2890. entry = search_exception_tables(regs->tpc);
  2891. diff -Nur linux-4.1.20.orig/arch/tile/include/asm/uaccess.h linux-4.1.20/arch/tile/include/asm/uaccess.h
  2892. --- linux-4.1.20.orig/arch/tile/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  2893. +++ linux-4.1.20/arch/tile/include/asm/uaccess.h 2016-03-21 20:18:30.000000000 +0100
  2894. @@ -78,7 +78,8 @@
  2895. * @addr: User space pointer to start of block to check
  2896. * @size: Size of block to check
  2897. *
  2898. - * Context: User context only. This function may sleep.
  2899. + * Context: User context only. This function may sleep if pagefaults are
  2900. + * enabled.
  2901. *
  2902. * Checks if a pointer to a block of memory in user space is valid.
  2903. *
  2904. @@ -192,7 +193,8 @@
  2905. * @x: Variable to store result.
  2906. * @ptr: Source address, in user space.
  2907. *
  2908. - * Context: User context only. This function may sleep.
  2909. + * Context: User context only. This function may sleep if pagefaults are
  2910. + * enabled.
  2911. *
  2912. * This macro copies a single simple variable from user space to kernel
  2913. * space. It supports simple types like char and int, but not larger
  2914. @@ -274,7 +276,8 @@
  2915. * @x: Value to copy to user space.
  2916. * @ptr: Destination address, in user space.
  2917. *
  2918. - * Context: User context only. This function may sleep.
  2919. + * Context: User context only. This function may sleep if pagefaults are
  2920. + * enabled.
  2921. *
  2922. * This macro copies a single simple value from kernel space to user
  2923. * space. It supports simple types like char and int, but not larger
  2924. @@ -330,7 +333,8 @@
  2925. * @from: Source address, in kernel space.
  2926. * @n: Number of bytes to copy.
  2927. *
  2928. - * Context: User context only. This function may sleep.
  2929. + * Context: User context only. This function may sleep if pagefaults are
  2930. + * enabled.
  2931. *
  2932. * Copy data from kernel space to user space. Caller must check
  2933. * the specified block with access_ok() before calling this function.
  2934. @@ -366,7 +370,8 @@
  2935. * @from: Source address, in user space.
  2936. * @n: Number of bytes to copy.
  2937. *
  2938. - * Context: User context only. This function may sleep.
  2939. + * Context: User context only. This function may sleep if pagefaults are
  2940. + * enabled.
  2941. *
  2942. * Copy data from user space to kernel space. Caller must check
  2943. * the specified block with access_ok() before calling this function.
  2944. @@ -437,7 +442,8 @@
  2945. * @from: Source address, in user space.
  2946. * @n: Number of bytes to copy.
  2947. *
  2948. - * Context: User context only. This function may sleep.
  2949. + * Context: User context only. This function may sleep if pagefaults are
  2950. + * enabled.
  2951. *
  2952. * Copy data from user space to user space. Caller must check
  2953. * the specified blocks with access_ok() before calling this function.
  2954. diff -Nur linux-4.1.20.orig/arch/tile/mm/fault.c linux-4.1.20/arch/tile/mm/fault.c
  2955. --- linux-4.1.20.orig/arch/tile/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  2956. +++ linux-4.1.20/arch/tile/mm/fault.c 2016-03-21 20:18:30.000000000 +0100
  2957. @@ -354,9 +354,9 @@
  2958. /*
  2959. * If we're in an interrupt, have no user context or are running in an
  2960. - * atomic region then we must not take the fault.
  2961. + * region with pagefaults disabled then we must not take the fault.
  2962. */
  2963. - if (in_atomic() || !mm) {
  2964. + if (pagefault_disabled() || !mm) {
  2965. vma = NULL; /* happy compiler */
  2966. goto bad_area_nosemaphore;
  2967. }
  2968. diff -Nur linux-4.1.20.orig/arch/tile/mm/highmem.c linux-4.1.20/arch/tile/mm/highmem.c
  2969. --- linux-4.1.20.orig/arch/tile/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  2970. +++ linux-4.1.20/arch/tile/mm/highmem.c 2016-03-21 20:18:30.000000000 +0100
  2971. @@ -201,7 +201,7 @@
  2972. int idx, type;
  2973. pte_t *pte;
  2974. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  2975. + preempt_disable();
  2976. pagefault_disable();
  2977. /* Avoid icache flushes by disallowing atomic executable mappings. */
  2978. @@ -259,6 +259,7 @@
  2979. }
  2980. pagefault_enable();
  2981. + preempt_enable();
  2982. }
  2983. EXPORT_SYMBOL(__kunmap_atomic);
  2984. diff -Nur linux-4.1.20.orig/arch/um/kernel/trap.c linux-4.1.20/arch/um/kernel/trap.c
  2985. --- linux-4.1.20.orig/arch/um/kernel/trap.c 2016-03-17 19:11:03.000000000 +0100
  2986. +++ linux-4.1.20/arch/um/kernel/trap.c 2016-03-21 20:18:30.000000000 +0100
  2987. @@ -35,10 +35,10 @@
  2988. *code_out = SEGV_MAPERR;
  2989. /*
  2990. - * If the fault was during atomic operation, don't take the fault, just
  2991. + * If the fault was with pagefaults disabled, don't take the fault, just
  2992. * fail.
  2993. */
  2994. - if (in_atomic())
  2995. + if (faulthandler_disabled())
  2996. goto out_nosemaphore;
  2997. if (is_user)
  2998. diff -Nur linux-4.1.20.orig/arch/unicore32/mm/fault.c linux-4.1.20/arch/unicore32/mm/fault.c
  2999. --- linux-4.1.20.orig/arch/unicore32/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  3000. +++ linux-4.1.20/arch/unicore32/mm/fault.c 2016-03-21 20:18:30.000000000 +0100
  3001. @@ -218,7 +218,7 @@
  3002. * If we're in an interrupt or have no user
  3003. * context, we must not take the fault..
  3004. */
  3005. - if (in_atomic() || !mm)
  3006. + if (faulthandler_disabled() || !mm)
  3007. goto no_context;
  3008. if (user_mode(regs))
  3009. diff -Nur linux-4.1.20.orig/arch/x86/Kconfig linux-4.1.20/arch/x86/Kconfig
  3010. --- linux-4.1.20.orig/arch/x86/Kconfig 2016-03-17 19:11:03.000000000 +0100
  3011. +++ linux-4.1.20/arch/x86/Kconfig 2016-03-21 20:18:30.000000000 +0100
  3012. @@ -22,6 +22,7 @@
  3013. ### Arch settings
  3014. config X86
  3015. def_bool y
  3016. + select HAVE_PREEMPT_LAZY
  3017. select ACPI_SYSTEM_POWER_STATES_SUPPORT if ACPI
  3018. select ARCH_MIGHT_HAVE_ACPI_PDC if ACPI
  3019. select ARCH_HAS_DEBUG_STRICT_USER_COPY_CHECKS
  3020. @@ -203,8 +204,11 @@
  3021. def_bool y
  3022. depends on ISA_DMA_API
  3023. +config RWSEM_GENERIC_SPINLOCK
  3024. + def_bool PREEMPT_RT_FULL
  3025. +
  3026. config RWSEM_XCHGADD_ALGORITHM
  3027. - def_bool y
  3028. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  3029. config GENERIC_CALIBRATE_DELAY
  3030. def_bool y
  3031. @@ -838,7 +842,7 @@
  3032. config MAXSMP
  3033. bool "Enable Maximum number of SMP Processors and NUMA Nodes"
  3034. depends on X86_64 && SMP && DEBUG_KERNEL
  3035. - select CPUMASK_OFFSTACK
  3036. + select CPUMASK_OFFSTACK if !PREEMPT_RT_FULL
  3037. ---help---
  3038. Enable maximum number of CPUS and NUMA Nodes for this architecture.
  3039. If unsure, say N.
  3040. diff -Nur linux-4.1.20.orig/arch/x86/crypto/aesni-intel_glue.c linux-4.1.20/arch/x86/crypto/aesni-intel_glue.c
  3041. --- linux-4.1.20.orig/arch/x86/crypto/aesni-intel_glue.c 2016-03-17 19:11:03.000000000 +0100
  3042. +++ linux-4.1.20/arch/x86/crypto/aesni-intel_glue.c 2016-03-21 20:18:30.000000000 +0100
  3043. @@ -382,14 +382,14 @@
  3044. err = blkcipher_walk_virt(desc, &walk);
  3045. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  3046. - kernel_fpu_begin();
  3047. while ((nbytes = walk.nbytes)) {
  3048. + kernel_fpu_begin();
  3049. aesni_ecb_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  3050. - nbytes & AES_BLOCK_MASK);
  3051. + nbytes & AES_BLOCK_MASK);
  3052. + kernel_fpu_end();
  3053. nbytes &= AES_BLOCK_SIZE - 1;
  3054. err = blkcipher_walk_done(desc, &walk, nbytes);
  3055. }
  3056. - kernel_fpu_end();
  3057. return err;
  3058. }
  3059. @@ -406,14 +406,14 @@
  3060. err = blkcipher_walk_virt(desc, &walk);
  3061. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  3062. - kernel_fpu_begin();
  3063. while ((nbytes = walk.nbytes)) {
  3064. + kernel_fpu_begin();
  3065. aesni_ecb_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  3066. nbytes & AES_BLOCK_MASK);
  3067. + kernel_fpu_end();
  3068. nbytes &= AES_BLOCK_SIZE - 1;
  3069. err = blkcipher_walk_done(desc, &walk, nbytes);
  3070. }
  3071. - kernel_fpu_end();
  3072. return err;
  3073. }
  3074. @@ -430,14 +430,14 @@
  3075. err = blkcipher_walk_virt(desc, &walk);
  3076. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  3077. - kernel_fpu_begin();
  3078. while ((nbytes = walk.nbytes)) {
  3079. + kernel_fpu_begin();
  3080. aesni_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  3081. nbytes & AES_BLOCK_MASK, walk.iv);
  3082. + kernel_fpu_end();
  3083. nbytes &= AES_BLOCK_SIZE - 1;
  3084. err = blkcipher_walk_done(desc, &walk, nbytes);
  3085. }
  3086. - kernel_fpu_end();
  3087. return err;
  3088. }
  3089. @@ -454,14 +454,14 @@
  3090. err = blkcipher_walk_virt(desc, &walk);
  3091. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  3092. - kernel_fpu_begin();
  3093. while ((nbytes = walk.nbytes)) {
  3094. + kernel_fpu_begin();
  3095. aesni_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  3096. nbytes & AES_BLOCK_MASK, walk.iv);
  3097. + kernel_fpu_end();
  3098. nbytes &= AES_BLOCK_SIZE - 1;
  3099. err = blkcipher_walk_done(desc, &walk, nbytes);
  3100. }
  3101. - kernel_fpu_end();
  3102. return err;
  3103. }
  3104. @@ -513,18 +513,20 @@
  3105. err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
  3106. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  3107. - kernel_fpu_begin();
  3108. while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
  3109. + kernel_fpu_begin();
  3110. aesni_ctr_enc_tfm(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  3111. nbytes & AES_BLOCK_MASK, walk.iv);
  3112. + kernel_fpu_end();
  3113. nbytes &= AES_BLOCK_SIZE - 1;
  3114. err = blkcipher_walk_done(desc, &walk, nbytes);
  3115. }
  3116. if (walk.nbytes) {
  3117. + kernel_fpu_begin();
  3118. ctr_crypt_final(ctx, &walk);
  3119. + kernel_fpu_end();
  3120. err = blkcipher_walk_done(desc, &walk, 0);
  3121. }
  3122. - kernel_fpu_end();
  3123. return err;
  3124. }
  3125. diff -Nur linux-4.1.20.orig/arch/x86/crypto/cast5_avx_glue.c linux-4.1.20/arch/x86/crypto/cast5_avx_glue.c
  3126. --- linux-4.1.20.orig/arch/x86/crypto/cast5_avx_glue.c 2016-03-17 19:11:03.000000000 +0100
  3127. +++ linux-4.1.20/arch/x86/crypto/cast5_avx_glue.c 2016-03-21 20:18:30.000000000 +0100
  3128. @@ -60,7 +60,7 @@
  3129. static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  3130. bool enc)
  3131. {
  3132. - bool fpu_enabled = false;
  3133. + bool fpu_enabled;
  3134. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  3135. const unsigned int bsize = CAST5_BLOCK_SIZE;
  3136. unsigned int nbytes;
  3137. @@ -76,7 +76,7 @@
  3138. u8 *wsrc = walk->src.virt.addr;
  3139. u8 *wdst = walk->dst.virt.addr;
  3140. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  3141. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  3142. /* Process multi-block batch */
  3143. if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
  3144. @@ -104,10 +104,9 @@
  3145. } while (nbytes >= bsize);
  3146. done:
  3147. + cast5_fpu_end(fpu_enabled);
  3148. err = blkcipher_walk_done(desc, walk, nbytes);
  3149. }
  3150. -
  3151. - cast5_fpu_end(fpu_enabled);
  3152. return err;
  3153. }
  3154. @@ -228,7 +227,7 @@
  3155. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  3156. struct scatterlist *src, unsigned int nbytes)
  3157. {
  3158. - bool fpu_enabled = false;
  3159. + bool fpu_enabled;
  3160. struct blkcipher_walk walk;
  3161. int err;
  3162. @@ -237,12 +236,11 @@
  3163. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  3164. while ((nbytes = walk.nbytes)) {
  3165. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  3166. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  3167. nbytes = __cbc_decrypt(desc, &walk);
  3168. + cast5_fpu_end(fpu_enabled);
  3169. err = blkcipher_walk_done(desc, &walk, nbytes);
  3170. }
  3171. -
  3172. - cast5_fpu_end(fpu_enabled);
  3173. return err;
  3174. }
  3175. @@ -312,7 +310,7 @@
  3176. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  3177. struct scatterlist *src, unsigned int nbytes)
  3178. {
  3179. - bool fpu_enabled = false;
  3180. + bool fpu_enabled;
  3181. struct blkcipher_walk walk;
  3182. int err;
  3183. @@ -321,13 +319,12 @@
  3184. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  3185. while ((nbytes = walk.nbytes) >= CAST5_BLOCK_SIZE) {
  3186. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  3187. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  3188. nbytes = __ctr_crypt(desc, &walk);
  3189. + cast5_fpu_end(fpu_enabled);
  3190. err = blkcipher_walk_done(desc, &walk, nbytes);
  3191. }
  3192. - cast5_fpu_end(fpu_enabled);
  3193. -
  3194. if (walk.nbytes) {
  3195. ctr_crypt_final(desc, &walk);
  3196. err = blkcipher_walk_done(desc, &walk, 0);
  3197. diff -Nur linux-4.1.20.orig/arch/x86/crypto/glue_helper.c linux-4.1.20/arch/x86/crypto/glue_helper.c
  3198. --- linux-4.1.20.orig/arch/x86/crypto/glue_helper.c 2016-03-17 19:11:03.000000000 +0100
  3199. +++ linux-4.1.20/arch/x86/crypto/glue_helper.c 2016-03-21 20:18:30.000000000 +0100
  3200. @@ -39,7 +39,7 @@
  3201. void *ctx = crypto_blkcipher_ctx(desc->tfm);
  3202. const unsigned int bsize = 128 / 8;
  3203. unsigned int nbytes, i, func_bytes;
  3204. - bool fpu_enabled = false;
  3205. + bool fpu_enabled;
  3206. int err;
  3207. err = blkcipher_walk_virt(desc, walk);
  3208. @@ -49,7 +49,7 @@
  3209. u8 *wdst = walk->dst.virt.addr;
  3210. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  3211. - desc, fpu_enabled, nbytes);
  3212. + desc, false, nbytes);
  3213. for (i = 0; i < gctx->num_funcs; i++) {
  3214. func_bytes = bsize * gctx->funcs[i].num_blocks;
  3215. @@ -71,10 +71,10 @@
  3216. }
  3217. done:
  3218. + glue_fpu_end(fpu_enabled);
  3219. err = blkcipher_walk_done(desc, walk, nbytes);
  3220. }
  3221. - glue_fpu_end(fpu_enabled);
  3222. return err;
  3223. }
  3224. @@ -194,7 +194,7 @@
  3225. struct scatterlist *src, unsigned int nbytes)
  3226. {
  3227. const unsigned int bsize = 128 / 8;
  3228. - bool fpu_enabled = false;
  3229. + bool fpu_enabled;
  3230. struct blkcipher_walk walk;
  3231. int err;
  3232. @@ -203,12 +203,12 @@
  3233. while ((nbytes = walk.nbytes)) {
  3234. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  3235. - desc, fpu_enabled, nbytes);
  3236. + desc, false, nbytes);
  3237. nbytes = __glue_cbc_decrypt_128bit(gctx, desc, &walk);
  3238. + glue_fpu_end(fpu_enabled);
  3239. err = blkcipher_walk_done(desc, &walk, nbytes);
  3240. }
  3241. - glue_fpu_end(fpu_enabled);
  3242. return err;
  3243. }
  3244. EXPORT_SYMBOL_GPL(glue_cbc_decrypt_128bit);
  3245. @@ -277,7 +277,7 @@
  3246. struct scatterlist *src, unsigned int nbytes)
  3247. {
  3248. const unsigned int bsize = 128 / 8;
  3249. - bool fpu_enabled = false;
  3250. + bool fpu_enabled;
  3251. struct blkcipher_walk walk;
  3252. int err;
  3253. @@ -286,13 +286,12 @@
  3254. while ((nbytes = walk.nbytes) >= bsize) {
  3255. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  3256. - desc, fpu_enabled, nbytes);
  3257. + desc, false, nbytes);
  3258. nbytes = __glue_ctr_crypt_128bit(gctx, desc, &walk);
  3259. + glue_fpu_end(fpu_enabled);
  3260. err = blkcipher_walk_done(desc, &walk, nbytes);
  3261. }
  3262. - glue_fpu_end(fpu_enabled);
  3263. -
  3264. if (walk.nbytes) {
  3265. glue_ctr_crypt_final_128bit(
  3266. gctx->funcs[gctx->num_funcs - 1].fn_u.ctr, desc, &walk);
  3267. @@ -347,7 +346,7 @@
  3268. void *tweak_ctx, void *crypt_ctx)
  3269. {
  3270. const unsigned int bsize = 128 / 8;
  3271. - bool fpu_enabled = false;
  3272. + bool fpu_enabled;
  3273. struct blkcipher_walk walk;
  3274. int err;
  3275. @@ -360,21 +359,21 @@
  3276. /* set minimum length to bsize, for tweak_fn */
  3277. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  3278. - desc, fpu_enabled,
  3279. + desc, false,
  3280. nbytes < bsize ? bsize : nbytes);
  3281. -
  3282. /* calculate first value of T */
  3283. tweak_fn(tweak_ctx, walk.iv, walk.iv);
  3284. + glue_fpu_end(fpu_enabled);
  3285. while (nbytes) {
  3286. + fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  3287. + desc, false, nbytes);
  3288. nbytes = __glue_xts_crypt_128bit(gctx, crypt_ctx, desc, &walk);
  3289. + glue_fpu_end(fpu_enabled);
  3290. err = blkcipher_walk_done(desc, &walk, nbytes);
  3291. nbytes = walk.nbytes;
  3292. }
  3293. -
  3294. - glue_fpu_end(fpu_enabled);
  3295. -
  3296. return err;
  3297. }
  3298. EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit);
  3299. diff -Nur linux-4.1.20.orig/arch/x86/include/asm/preempt.h linux-4.1.20/arch/x86/include/asm/preempt.h
  3300. --- linux-4.1.20.orig/arch/x86/include/asm/preempt.h 2016-03-17 19:11:03.000000000 +0100
  3301. +++ linux-4.1.20/arch/x86/include/asm/preempt.h 2016-03-21 20:18:30.000000000 +0100
  3302. @@ -82,17 +82,33 @@
  3303. * a decrement which hits zero means we have no preempt_count and should
  3304. * reschedule.
  3305. */
  3306. -static __always_inline bool __preempt_count_dec_and_test(void)
  3307. +static __always_inline bool ____preempt_count_dec_and_test(void)
  3308. {
  3309. GEN_UNARY_RMWcc("decl", __preempt_count, __percpu_arg(0), "e");
  3310. }
  3311. +static __always_inline bool __preempt_count_dec_and_test(void)
  3312. +{
  3313. + if (____preempt_count_dec_and_test())
  3314. + return true;
  3315. +#ifdef CONFIG_PREEMPT_LAZY
  3316. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  3317. +#else
  3318. + return false;
  3319. +#endif
  3320. +}
  3321. +
  3322. /*
  3323. * Returns true when we need to resched and can (barring IRQ state).
  3324. */
  3325. static __always_inline bool should_resched(int preempt_offset)
  3326. {
  3327. +#ifdef CONFIG_PREEMPT_LAZY
  3328. + return unlikely(raw_cpu_read_4(__preempt_count) == preempt_offset ||
  3329. + test_thread_flag(TIF_NEED_RESCHED_LAZY));
  3330. +#else
  3331. return unlikely(raw_cpu_read_4(__preempt_count) == preempt_offset);
  3332. +#endif
  3333. }
  3334. #ifdef CONFIG_PREEMPT
  3335. diff -Nur linux-4.1.20.orig/arch/x86/include/asm/signal.h linux-4.1.20/arch/x86/include/asm/signal.h
  3336. --- linux-4.1.20.orig/arch/x86/include/asm/signal.h 2016-03-17 19:11:03.000000000 +0100
  3337. +++ linux-4.1.20/arch/x86/include/asm/signal.h 2016-03-21 20:18:30.000000000 +0100
  3338. @@ -23,6 +23,19 @@
  3339. unsigned long sig[_NSIG_WORDS];
  3340. } sigset_t;
  3341. +/*
  3342. + * Because some traps use the IST stack, we must keep preemption
  3343. + * disabled while calling do_trap(), but do_trap() may call
  3344. + * force_sig_info() which will grab the signal spin_locks for the
  3345. + * task, which in PREEMPT_RT_FULL are mutexes. By defining
  3346. + * ARCH_RT_DELAYS_SIGNAL_SEND the force_sig_info() will set
  3347. + * TIF_NOTIFY_RESUME and set up the signal to be sent on exit of the
  3348. + * trap.
  3349. + */
  3350. +#if defined(CONFIG_PREEMPT_RT_FULL)
  3351. +#define ARCH_RT_DELAYS_SIGNAL_SEND
  3352. +#endif
  3353. +
  3354. #ifndef CONFIG_COMPAT
  3355. typedef sigset_t compat_sigset_t;
  3356. #endif
  3357. diff -Nur linux-4.1.20.orig/arch/x86/include/asm/stackprotector.h linux-4.1.20/arch/x86/include/asm/stackprotector.h
  3358. --- linux-4.1.20.orig/arch/x86/include/asm/stackprotector.h 2016-03-17 19:11:03.000000000 +0100
  3359. +++ linux-4.1.20/arch/x86/include/asm/stackprotector.h 2016-03-21 20:18:30.000000000 +0100
  3360. @@ -57,7 +57,7 @@
  3361. */
  3362. static __always_inline void boot_init_stack_canary(void)
  3363. {
  3364. - u64 canary;
  3365. + u64 uninitialized_var(canary);
  3366. u64 tsc;
  3367. #ifdef CONFIG_X86_64
  3368. @@ -68,8 +68,16 @@
  3369. * of randomness. The TSC only matters for very early init,
  3370. * there it already has some randomness on most systems. Later
  3371. * on during the bootup the random pool has true entropy too.
  3372. + *
  3373. + * For preempt-rt we need to weaken the randomness a bit, as
  3374. + * we can't call into the random generator from atomic context
  3375. + * due to locking constraints. We just leave canary
  3376. + * uninitialized and use the TSC based randomness on top of
  3377. + * it.
  3378. */
  3379. +#ifndef CONFIG_PREEMPT_RT_FULL
  3380. get_random_bytes(&canary, sizeof(canary));
  3381. +#endif
  3382. tsc = __native_read_tsc();
  3383. canary += tsc + (tsc << 32UL);
  3384. diff -Nur linux-4.1.20.orig/arch/x86/include/asm/thread_info.h linux-4.1.20/arch/x86/include/asm/thread_info.h
  3385. --- linux-4.1.20.orig/arch/x86/include/asm/thread_info.h 2016-03-17 19:11:03.000000000 +0100
  3386. +++ linux-4.1.20/arch/x86/include/asm/thread_info.h 2016-03-21 20:18:30.000000000 +0100
  3387. @@ -55,6 +55,8 @@
  3388. __u32 status; /* thread synchronous flags */
  3389. __u32 cpu; /* current CPU */
  3390. int saved_preempt_count;
  3391. + int preempt_lazy_count; /* 0 => lazy preemptable
  3392. + <0 => BUG */
  3393. mm_segment_t addr_limit;
  3394. void __user *sysenter_return;
  3395. unsigned int sig_on_uaccess_error:1;
  3396. @@ -95,6 +97,7 @@
  3397. #define TIF_SYSCALL_EMU 6 /* syscall emulation active */
  3398. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  3399. #define TIF_SECCOMP 8 /* secure computing */
  3400. +#define TIF_NEED_RESCHED_LAZY 9 /* lazy rescheduling necessary */
  3401. #define TIF_USER_RETURN_NOTIFY 11 /* notify kernel of userspace return */
  3402. #define TIF_UPROBE 12 /* breakpointed or singlestepping */
  3403. #define TIF_NOTSC 16 /* TSC is not accessible in userland */
  3404. @@ -119,6 +122,7 @@
  3405. #define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU)
  3406. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  3407. #define _TIF_SECCOMP (1 << TIF_SECCOMP)
  3408. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  3409. #define _TIF_USER_RETURN_NOTIFY (1 << TIF_USER_RETURN_NOTIFY)
  3410. #define _TIF_UPROBE (1 << TIF_UPROBE)
  3411. #define _TIF_NOTSC (1 << TIF_NOTSC)
  3412. @@ -168,6 +172,8 @@
  3413. #define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW|_TIF_USER_RETURN_NOTIFY)
  3414. #define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)
  3415. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  3416. +
  3417. #define STACK_WARN (THREAD_SIZE/8)
  3418. /*
  3419. diff -Nur linux-4.1.20.orig/arch/x86/include/asm/uaccess.h linux-4.1.20/arch/x86/include/asm/uaccess.h
  3420. --- linux-4.1.20.orig/arch/x86/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  3421. +++ linux-4.1.20/arch/x86/include/asm/uaccess.h 2016-03-21 20:18:30.000000000 +0100
  3422. @@ -74,7 +74,8 @@
  3423. * @addr: User space pointer to start of block to check
  3424. * @size: Size of block to check
  3425. *
  3426. - * Context: User context only. This function may sleep.
  3427. + * Context: User context only. This function may sleep if pagefaults are
  3428. + * enabled.
  3429. *
  3430. * Checks if a pointer to a block of memory in user space is valid.
  3431. *
  3432. @@ -145,7 +146,8 @@
  3433. * @x: Variable to store result.
  3434. * @ptr: Source address, in user space.
  3435. *
  3436. - * Context: User context only. This function may sleep.
  3437. + * Context: User context only. This function may sleep if pagefaults are
  3438. + * enabled.
  3439. *
  3440. * This macro copies a single simple variable from user space to kernel
  3441. * space. It supports simple types like char and int, but not larger
  3442. @@ -240,7 +242,8 @@
  3443. * @x: Value to copy to user space.
  3444. * @ptr: Destination address, in user space.
  3445. *
  3446. - * Context: User context only. This function may sleep.
  3447. + * Context: User context only. This function may sleep if pagefaults are
  3448. + * enabled.
  3449. *
  3450. * This macro copies a single simple value from kernel space to user
  3451. * space. It supports simple types like char and int, but not larger
  3452. @@ -455,7 +458,8 @@
  3453. * @x: Variable to store result.
  3454. * @ptr: Source address, in user space.
  3455. *
  3456. - * Context: User context only. This function may sleep.
  3457. + * Context: User context only. This function may sleep if pagefaults are
  3458. + * enabled.
  3459. *
  3460. * This macro copies a single simple variable from user space to kernel
  3461. * space. It supports simple types like char and int, but not larger
  3462. @@ -479,7 +483,8 @@
  3463. * @x: Value to copy to user space.
  3464. * @ptr: Destination address, in user space.
  3465. *
  3466. - * Context: User context only. This function may sleep.
  3467. + * Context: User context only. This function may sleep if pagefaults are
  3468. + * enabled.
  3469. *
  3470. * This macro copies a single simple value from kernel space to user
  3471. * space. It supports simple types like char and int, but not larger
  3472. diff -Nur linux-4.1.20.orig/arch/x86/include/asm/uaccess_32.h linux-4.1.20/arch/x86/include/asm/uaccess_32.h
  3473. --- linux-4.1.20.orig/arch/x86/include/asm/uaccess_32.h 2016-03-17 19:11:03.000000000 +0100
  3474. +++ linux-4.1.20/arch/x86/include/asm/uaccess_32.h 2016-03-21 20:18:30.000000000 +0100
  3475. @@ -70,7 +70,8 @@
  3476. * @from: Source address, in kernel space.
  3477. * @n: Number of bytes to copy.
  3478. *
  3479. - * Context: User context only. This function may sleep.
  3480. + * Context: User context only. This function may sleep if pagefaults are
  3481. + * enabled.
  3482. *
  3483. * Copy data from kernel space to user space. Caller must check
  3484. * the specified block with access_ok() before calling this function.
  3485. @@ -117,7 +118,8 @@
  3486. * @from: Source address, in user space.
  3487. * @n: Number of bytes to copy.
  3488. *
  3489. - * Context: User context only. This function may sleep.
  3490. + * Context: User context only. This function may sleep if pagefaults are
  3491. + * enabled.
  3492. *
  3493. * Copy data from user space to kernel space. Caller must check
  3494. * the specified block with access_ok() before calling this function.
  3495. diff -Nur linux-4.1.20.orig/arch/x86/include/asm/uv/uv_bau.h linux-4.1.20/arch/x86/include/asm/uv/uv_bau.h
  3496. --- linux-4.1.20.orig/arch/x86/include/asm/uv/uv_bau.h 2016-03-17 19:11:03.000000000 +0100
  3497. +++ linux-4.1.20/arch/x86/include/asm/uv/uv_bau.h 2016-03-21 20:18:30.000000000 +0100
  3498. @@ -615,9 +615,9 @@
  3499. cycles_t send_message;
  3500. cycles_t period_end;
  3501. cycles_t period_time;
  3502. - spinlock_t uvhub_lock;
  3503. - spinlock_t queue_lock;
  3504. - spinlock_t disable_lock;
  3505. + raw_spinlock_t uvhub_lock;
  3506. + raw_spinlock_t queue_lock;
  3507. + raw_spinlock_t disable_lock;
  3508. /* tunables */
  3509. int max_concurr;
  3510. int max_concurr_const;
  3511. @@ -776,15 +776,15 @@
  3512. * to be lowered below the current 'v'. atomic_add_unless can only stop
  3513. * on equal.
  3514. */
  3515. -static inline int atomic_inc_unless_ge(spinlock_t *lock, atomic_t *v, int u)
  3516. +static inline int atomic_inc_unless_ge(raw_spinlock_t *lock, atomic_t *v, int u)
  3517. {
  3518. - spin_lock(lock);
  3519. + raw_spin_lock(lock);
  3520. if (atomic_read(v) >= u) {
  3521. - spin_unlock(lock);
  3522. + raw_spin_unlock(lock);
  3523. return 0;
  3524. }
  3525. atomic_inc(v);
  3526. - spin_unlock(lock);
  3527. + raw_spin_unlock(lock);
  3528. return 1;
  3529. }
  3530. diff -Nur linux-4.1.20.orig/arch/x86/include/asm/uv/uv_hub.h linux-4.1.20/arch/x86/include/asm/uv/uv_hub.h
  3531. --- linux-4.1.20.orig/arch/x86/include/asm/uv/uv_hub.h 2016-03-17 19:11:03.000000000 +0100
  3532. +++ linux-4.1.20/arch/x86/include/asm/uv/uv_hub.h 2016-03-21 20:18:30.000000000 +0100
  3533. @@ -492,7 +492,7 @@
  3534. unsigned short nr_online_cpus;
  3535. unsigned short pnode;
  3536. short memory_nid;
  3537. - spinlock_t nmi_lock; /* obsolete, see uv_hub_nmi */
  3538. + raw_spinlock_t nmi_lock; /* obsolete, see uv_hub_nmi */
  3539. unsigned long nmi_count; /* obsolete, see uv_hub_nmi */
  3540. };
  3541. extern struct uv_blade_info *uv_blade_info;
  3542. diff -Nur linux-4.1.20.orig/arch/x86/kernel/apic/io_apic.c linux-4.1.20/arch/x86/kernel/apic/io_apic.c
  3543. --- linux-4.1.20.orig/arch/x86/kernel/apic/io_apic.c 2016-03-17 19:11:03.000000000 +0100
  3544. +++ linux-4.1.20/arch/x86/kernel/apic/io_apic.c 2016-03-21 20:18:30.000000000 +0100
  3545. @@ -1891,7 +1891,8 @@
  3546. static inline bool ioapic_irqd_mask(struct irq_data *data, struct irq_cfg *cfg)
  3547. {
  3548. /* If we are moving the irq we need to mask it */
  3549. - if (unlikely(irqd_is_setaffinity_pending(data))) {
  3550. + if (unlikely(irqd_is_setaffinity_pending(data) &&
  3551. + !irqd_irq_inprogress(data))) {
  3552. mask_ioapic(cfg);
  3553. return true;
  3554. }
  3555. diff -Nur linux-4.1.20.orig/arch/x86/kernel/apic/x2apic_uv_x.c linux-4.1.20/arch/x86/kernel/apic/x2apic_uv_x.c
  3556. --- linux-4.1.20.orig/arch/x86/kernel/apic/x2apic_uv_x.c 2016-03-17 19:11:03.000000000 +0100
  3557. +++ linux-4.1.20/arch/x86/kernel/apic/x2apic_uv_x.c 2016-03-21 20:18:30.000000000 +0100
  3558. @@ -949,7 +949,7 @@
  3559. uv_blade_info[blade].pnode = pnode;
  3560. uv_blade_info[blade].nr_possible_cpus = 0;
  3561. uv_blade_info[blade].nr_online_cpus = 0;
  3562. - spin_lock_init(&uv_blade_info[blade].nmi_lock);
  3563. + raw_spin_lock_init(&uv_blade_info[blade].nmi_lock);
  3564. min_pnode = min(pnode, min_pnode);
  3565. max_pnode = max(pnode, max_pnode);
  3566. blade++;
  3567. diff -Nur linux-4.1.20.orig/arch/x86/kernel/asm-offsets.c linux-4.1.20/arch/x86/kernel/asm-offsets.c
  3568. --- linux-4.1.20.orig/arch/x86/kernel/asm-offsets.c 2016-03-17 19:11:03.000000000 +0100
  3569. +++ linux-4.1.20/arch/x86/kernel/asm-offsets.c 2016-03-21 20:18:30.000000000 +0100
  3570. @@ -32,6 +32,7 @@
  3571. OFFSET(TI_flags, thread_info, flags);
  3572. OFFSET(TI_status, thread_info, status);
  3573. OFFSET(TI_addr_limit, thread_info, addr_limit);
  3574. + OFFSET(TI_preempt_lazy_count, thread_info, preempt_lazy_count);
  3575. BLANK();
  3576. OFFSET(crypto_tfm_ctx_offset, crypto_tfm, __crt_ctx);
  3577. @@ -71,4 +72,5 @@
  3578. BLANK();
  3579. DEFINE(PTREGS_SIZE, sizeof(struct pt_regs));
  3580. + DEFINE(_PREEMPT_ENABLED, PREEMPT_ENABLED);
  3581. }
  3582. diff -Nur linux-4.1.20.orig/arch/x86/kernel/cpu/mcheck/mce.c linux-4.1.20/arch/x86/kernel/cpu/mcheck/mce.c
  3583. --- linux-4.1.20.orig/arch/x86/kernel/cpu/mcheck/mce.c 2016-03-17 19:11:03.000000000 +0100
  3584. +++ linux-4.1.20/arch/x86/kernel/cpu/mcheck/mce.c 2016-03-21 20:18:30.000000000 +0100
  3585. @@ -41,6 +41,8 @@
  3586. #include <linux/debugfs.h>
  3587. #include <linux/irq_work.h>
  3588. #include <linux/export.h>
  3589. +#include <linux/jiffies.h>
  3590. +#include <linux/work-simple.h>
  3591. #include <asm/processor.h>
  3592. #include <asm/traps.h>
  3593. @@ -1267,7 +1269,7 @@
  3594. static unsigned long check_interval = INITIAL_CHECK_INTERVAL;
  3595. static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
  3596. -static DEFINE_PER_CPU(struct timer_list, mce_timer);
  3597. +static DEFINE_PER_CPU(struct hrtimer, mce_timer);
  3598. static unsigned long mce_adjust_timer_default(unsigned long interval)
  3599. {
  3600. @@ -1276,32 +1278,18 @@
  3601. static unsigned long (*mce_adjust_timer)(unsigned long interval) = mce_adjust_timer_default;
  3602. -static void __restart_timer(struct timer_list *t, unsigned long interval)
  3603. +static enum hrtimer_restart __restart_timer(struct hrtimer *timer, unsigned long interval)
  3604. {
  3605. - unsigned long when = jiffies + interval;
  3606. - unsigned long flags;
  3607. -
  3608. - local_irq_save(flags);
  3609. -
  3610. - if (timer_pending(t)) {
  3611. - if (time_before(when, t->expires))
  3612. - mod_timer_pinned(t, when);
  3613. - } else {
  3614. - t->expires = round_jiffies(when);
  3615. - add_timer_on(t, smp_processor_id());
  3616. - }
  3617. -
  3618. - local_irq_restore(flags);
  3619. + if (!interval)
  3620. + return HRTIMER_NORESTART;
  3621. + hrtimer_forward_now(timer, ns_to_ktime(jiffies_to_nsecs(interval)));
  3622. + return HRTIMER_RESTART;
  3623. }
  3624. -static void mce_timer_fn(unsigned long data)
  3625. +static enum hrtimer_restart mce_timer_fn(struct hrtimer *timer)
  3626. {
  3627. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  3628. - int cpu = smp_processor_id();
  3629. unsigned long iv;
  3630. - WARN_ON(cpu != data);
  3631. -
  3632. iv = __this_cpu_read(mce_next_interval);
  3633. if (mce_available(this_cpu_ptr(&cpu_info))) {
  3634. @@ -1324,7 +1312,7 @@
  3635. done:
  3636. __this_cpu_write(mce_next_interval, iv);
  3637. - __restart_timer(t, iv);
  3638. + return __restart_timer(timer, iv);
  3639. }
  3640. /*
  3641. @@ -1332,7 +1320,7 @@
  3642. */
  3643. void mce_timer_kick(unsigned long interval)
  3644. {
  3645. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  3646. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  3647. unsigned long iv = __this_cpu_read(mce_next_interval);
  3648. __restart_timer(t, interval);
  3649. @@ -1347,7 +1335,7 @@
  3650. int cpu;
  3651. for_each_online_cpu(cpu)
  3652. - del_timer_sync(&per_cpu(mce_timer, cpu));
  3653. + hrtimer_cancel(&per_cpu(mce_timer, cpu));
  3654. }
  3655. static void mce_do_trigger(struct work_struct *work)
  3656. @@ -1357,6 +1345,56 @@
  3657. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  3658. +static void __mce_notify_work(struct swork_event *event)
  3659. +{
  3660. + /* Not more than two messages every minute */
  3661. + static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  3662. +
  3663. + /* wake processes polling /dev/mcelog */
  3664. + wake_up_interruptible(&mce_chrdev_wait);
  3665. +
  3666. + /*
  3667. + * There is no risk of missing notifications because
  3668. + * work_pending is always cleared before the function is
  3669. + * executed.
  3670. + */
  3671. + if (mce_helper[0] && !work_pending(&mce_trigger_work))
  3672. + schedule_work(&mce_trigger_work);
  3673. +
  3674. + if (__ratelimit(&ratelimit))
  3675. + pr_info(HW_ERR "Machine check events logged\n");
  3676. +}
  3677. +
  3678. +#ifdef CONFIG_PREEMPT_RT_FULL
  3679. +static bool notify_work_ready __read_mostly;
  3680. +static struct swork_event notify_work;
  3681. +
  3682. +static int mce_notify_work_init(void)
  3683. +{
  3684. + int err;
  3685. +
  3686. + err = swork_get();
  3687. + if (err)
  3688. + return err;
  3689. +
  3690. + INIT_SWORK(&notify_work, __mce_notify_work);
  3691. + notify_work_ready = true;
  3692. + return 0;
  3693. +}
  3694. +
  3695. +static void mce_notify_work(void)
  3696. +{
  3697. + if (notify_work_ready)
  3698. + swork_queue(&notify_work);
  3699. +}
  3700. +#else
  3701. +static void mce_notify_work(void)
  3702. +{
  3703. + __mce_notify_work(NULL);
  3704. +}
  3705. +static inline int mce_notify_work_init(void) { return 0; }
  3706. +#endif
  3707. +
  3708. /*
  3709. * Notify the user(s) about new machine check events.
  3710. * Can be called from interrupt context, but not from machine check/NMI
  3711. @@ -1364,19 +1402,8 @@
  3712. */
  3713. int mce_notify_irq(void)
  3714. {
  3715. - /* Not more than two messages every minute */
  3716. - static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  3717. -
  3718. if (test_and_clear_bit(0, &mce_need_notify)) {
  3719. - /* wake processes polling /dev/mcelog */
  3720. - wake_up_interruptible(&mce_chrdev_wait);
  3721. -
  3722. - if (mce_helper[0])
  3723. - schedule_work(&mce_trigger_work);
  3724. -
  3725. - if (__ratelimit(&ratelimit))
  3726. - pr_info(HW_ERR "Machine check events logged\n");
  3727. -
  3728. + mce_notify_work();
  3729. return 1;
  3730. }
  3731. return 0;
  3732. @@ -1649,7 +1676,7 @@
  3733. }
  3734. }
  3735. -static void mce_start_timer(unsigned int cpu, struct timer_list *t)
  3736. +static void mce_start_timer(unsigned int cpu, struct hrtimer *t)
  3737. {
  3738. unsigned long iv = check_interval * HZ;
  3739. @@ -1658,16 +1685,17 @@
  3740. per_cpu(mce_next_interval, cpu) = iv;
  3741. - t->expires = round_jiffies(jiffies + iv);
  3742. - add_timer_on(t, cpu);
  3743. + hrtimer_start_range_ns(t, ns_to_ktime(jiffies_to_usecs(iv) * 1000ULL),
  3744. + 0, HRTIMER_MODE_REL_PINNED);
  3745. }
  3746. static void __mcheck_cpu_init_timer(void)
  3747. {
  3748. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  3749. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  3750. unsigned int cpu = smp_processor_id();
  3751. - setup_timer(t, mce_timer_fn, cpu);
  3752. + hrtimer_init(t, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  3753. + t->function = mce_timer_fn;
  3754. mce_start_timer(cpu, t);
  3755. }
  3756. @@ -2345,6 +2373,8 @@
  3757. if (!mce_available(raw_cpu_ptr(&cpu_info)))
  3758. return;
  3759. + hrtimer_cancel(this_cpu_ptr(&mce_timer));
  3760. +
  3761. if (!(action & CPU_TASKS_FROZEN))
  3762. cmci_clear();
  3763. for (i = 0; i < mca_cfg.banks; i++) {
  3764. @@ -2371,6 +2401,7 @@
  3765. if (b->init)
  3766. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  3767. }
  3768. + __mcheck_cpu_init_timer();
  3769. }
  3770. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  3771. @@ -2378,7 +2409,6 @@
  3772. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  3773. {
  3774. unsigned int cpu = (unsigned long)hcpu;
  3775. - struct timer_list *t = &per_cpu(mce_timer, cpu);
  3776. switch (action & ~CPU_TASKS_FROZEN) {
  3777. case CPU_ONLINE:
  3778. @@ -2398,11 +2428,9 @@
  3779. break;
  3780. case CPU_DOWN_PREPARE:
  3781. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  3782. - del_timer_sync(t);
  3783. break;
  3784. case CPU_DOWN_FAILED:
  3785. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  3786. - mce_start_timer(cpu, t);
  3787. break;
  3788. }
  3789. @@ -2441,6 +2469,10 @@
  3790. goto err_out;
  3791. }
  3792. + err = mce_notify_work_init();
  3793. + if (err)
  3794. + goto err_out;
  3795. +
  3796. if (!zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL)) {
  3797. err = -ENOMEM;
  3798. goto err_out;
  3799. diff -Nur linux-4.1.20.orig/arch/x86/kernel/dumpstack_32.c linux-4.1.20/arch/x86/kernel/dumpstack_32.c
  3800. --- linux-4.1.20.orig/arch/x86/kernel/dumpstack_32.c 2016-03-17 19:11:03.000000000 +0100
  3801. +++ linux-4.1.20/arch/x86/kernel/dumpstack_32.c 2016-03-21 20:18:30.000000000 +0100
  3802. @@ -42,7 +42,7 @@
  3803. unsigned long *stack, unsigned long bp,
  3804. const struct stacktrace_ops *ops, void *data)
  3805. {
  3806. - const unsigned cpu = get_cpu();
  3807. + const unsigned cpu = get_cpu_light();
  3808. int graph = 0;
  3809. u32 *prev_esp;
  3810. @@ -86,7 +86,7 @@
  3811. break;
  3812. touch_nmi_watchdog();
  3813. }
  3814. - put_cpu();
  3815. + put_cpu_light();
  3816. }
  3817. EXPORT_SYMBOL(dump_trace);
  3818. diff -Nur linux-4.1.20.orig/arch/x86/kernel/dumpstack_64.c linux-4.1.20/arch/x86/kernel/dumpstack_64.c
  3819. --- linux-4.1.20.orig/arch/x86/kernel/dumpstack_64.c 2016-03-17 19:11:03.000000000 +0100
  3820. +++ linux-4.1.20/arch/x86/kernel/dumpstack_64.c 2016-03-21 20:18:30.000000000 +0100
  3821. @@ -152,7 +152,7 @@
  3822. unsigned long *stack, unsigned long bp,
  3823. const struct stacktrace_ops *ops, void *data)
  3824. {
  3825. - const unsigned cpu = get_cpu();
  3826. + const unsigned cpu = get_cpu_light();
  3827. struct thread_info *tinfo;
  3828. unsigned long *irq_stack = (unsigned long *)per_cpu(irq_stack_ptr, cpu);
  3829. unsigned long dummy;
  3830. @@ -241,7 +241,7 @@
  3831. * This handles the process stack:
  3832. */
  3833. bp = ops->walk_stack(tinfo, stack, bp, ops, data, NULL, &graph);
  3834. - put_cpu();
  3835. + put_cpu_light();
  3836. }
  3837. EXPORT_SYMBOL(dump_trace);
  3838. @@ -255,7 +255,7 @@
  3839. int cpu;
  3840. int i;
  3841. - preempt_disable();
  3842. + migrate_disable();
  3843. cpu = smp_processor_id();
  3844. irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
  3845. @@ -291,7 +291,7 @@
  3846. pr_cont(" %016lx", *stack++);
  3847. touch_nmi_watchdog();
  3848. }
  3849. - preempt_enable();
  3850. + migrate_enable();
  3851. pr_cont("\n");
  3852. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  3853. diff -Nur linux-4.1.20.orig/arch/x86/kernel/entry_32.S linux-4.1.20/arch/x86/kernel/entry_32.S
  3854. --- linux-4.1.20.orig/arch/x86/kernel/entry_32.S 2016-03-17 19:11:03.000000000 +0100
  3855. +++ linux-4.1.20/arch/x86/kernel/entry_32.S 2016-03-21 20:18:30.000000000 +0100
  3856. @@ -359,8 +359,24 @@
  3857. ENTRY(resume_kernel)
  3858. DISABLE_INTERRUPTS(CLBR_ANY)
  3859. need_resched:
  3860. + # preempt count == 0 + NEED_RS set?
  3861. cmpl $0,PER_CPU_VAR(__preempt_count)
  3862. +#ifndef CONFIG_PREEMPT_LAZY
  3863. jnz restore_all
  3864. +#else
  3865. + jz test_int_off
  3866. +
  3867. + # atleast preempt count == 0 ?
  3868. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  3869. + jne restore_all
  3870. +
  3871. + cmpl $0,TI_preempt_lazy_count(%ebp) # non-zero preempt_lazy_count ?
  3872. + jnz restore_all
  3873. +
  3874. + testl $_TIF_NEED_RESCHED_LAZY, TI_flags(%ebp)
  3875. + jz restore_all
  3876. +test_int_off:
  3877. +#endif
  3878. testl $X86_EFLAGS_IF,PT_EFLAGS(%esp) # interrupts off (exception path) ?
  3879. jz restore_all
  3880. call preempt_schedule_irq
  3881. @@ -594,7 +610,7 @@
  3882. ALIGN
  3883. RING0_PTREGS_FRAME # can't unwind into user space anyway
  3884. work_pending:
  3885. - testb $_TIF_NEED_RESCHED, %cl
  3886. + testl $_TIF_NEED_RESCHED_MASK, %ecx
  3887. jz work_notifysig
  3888. work_resched:
  3889. call schedule
  3890. @@ -607,7 +623,7 @@
  3891. andl $_TIF_WORK_MASK, %ecx # is there any work to be done other
  3892. # than syscall tracing?
  3893. jz restore_all
  3894. - testb $_TIF_NEED_RESCHED, %cl
  3895. + testl $_TIF_NEED_RESCHED_MASK, %ecx
  3896. jnz work_resched
  3897. work_notifysig: # deal with pending signals and
  3898. diff -Nur linux-4.1.20.orig/arch/x86/kernel/entry_64.S linux-4.1.20/arch/x86/kernel/entry_64.S
  3899. --- linux-4.1.20.orig/arch/x86/kernel/entry_64.S 2016-03-17 19:11:03.000000000 +0100
  3900. +++ linux-4.1.20/arch/x86/kernel/entry_64.S 2016-03-21 20:18:30.000000000 +0100
  3901. @@ -370,8 +370,8 @@
  3902. /* First do a reschedule test. */
  3903. /* edx: work, edi: workmask */
  3904. int_careful:
  3905. - bt $TIF_NEED_RESCHED,%edx
  3906. - jnc int_very_careful
  3907. + testl $_TIF_NEED_RESCHED_MASK,%edx
  3908. + jz int_very_careful
  3909. TRACE_IRQS_ON
  3910. ENABLE_INTERRUPTS(CLBR_NONE)
  3911. pushq_cfi %rdi
  3912. @@ -776,7 +776,23 @@
  3913. bt $9,EFLAGS(%rsp) /* interrupts were off? */
  3914. jnc 1f
  3915. 0: cmpl $0,PER_CPU_VAR(__preempt_count)
  3916. +#ifndef CONFIG_PREEMPT_LAZY
  3917. jnz 1f
  3918. +#else
  3919. + jz do_preempt_schedule_irq
  3920. +
  3921. + # atleast preempt count == 0 ?
  3922. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  3923. + jnz 1f
  3924. +
  3925. + GET_THREAD_INFO(%rcx)
  3926. + cmpl $0, TI_preempt_lazy_count(%rcx)
  3927. + jnz 1f
  3928. +
  3929. + bt $TIF_NEED_RESCHED_LAZY,TI_flags(%rcx)
  3930. + jnc 1f
  3931. +do_preempt_schedule_irq:
  3932. +#endif
  3933. call preempt_schedule_irq
  3934. jmp 0b
  3935. 1:
  3936. @@ -844,8 +860,8 @@
  3937. /* edi: workmask, edx: work */
  3938. retint_careful:
  3939. CFI_RESTORE_STATE
  3940. - bt $TIF_NEED_RESCHED,%edx
  3941. - jnc retint_signal
  3942. + testl $_TIF_NEED_RESCHED_MASK,%edx
  3943. + jz retint_signal
  3944. TRACE_IRQS_ON
  3945. ENABLE_INTERRUPTS(CLBR_NONE)
  3946. pushq_cfi %rdi
  3947. @@ -1118,6 +1134,7 @@
  3948. jmp 2b
  3949. .previous
  3950. +#ifndef CONFIG_PREEMPT_RT_FULL
  3951. /* Call softirq on interrupt stack. Interrupts are off. */
  3952. ENTRY(do_softirq_own_stack)
  3953. CFI_STARTPROC
  3954. @@ -1137,6 +1154,7 @@
  3955. ret
  3956. CFI_ENDPROC
  3957. END(do_softirq_own_stack)
  3958. +#endif
  3959. #ifdef CONFIG_XEN
  3960. idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
  3961. diff -Nur linux-4.1.20.orig/arch/x86/kernel/irq_32.c linux-4.1.20/arch/x86/kernel/irq_32.c
  3962. --- linux-4.1.20.orig/arch/x86/kernel/irq_32.c 2016-03-17 19:11:03.000000000 +0100
  3963. +++ linux-4.1.20/arch/x86/kernel/irq_32.c 2016-03-21 20:18:30.000000000 +0100
  3964. @@ -135,6 +135,7 @@
  3965. cpu, per_cpu(hardirq_stack, cpu), per_cpu(softirq_stack, cpu));
  3966. }
  3967. +#ifndef CONFIG_PREEMPT_RT_FULL
  3968. void do_softirq_own_stack(void)
  3969. {
  3970. struct thread_info *curstk;
  3971. @@ -153,6 +154,7 @@
  3972. call_on_stack(__do_softirq, isp);
  3973. }
  3974. +#endif
  3975. bool handle_irq(unsigned irq, struct pt_regs *regs)
  3976. {
  3977. diff -Nur linux-4.1.20.orig/arch/x86/kernel/process_32.c linux-4.1.20/arch/x86/kernel/process_32.c
  3978. --- linux-4.1.20.orig/arch/x86/kernel/process_32.c 2016-03-17 19:11:03.000000000 +0100
  3979. +++ linux-4.1.20/arch/x86/kernel/process_32.c 2016-03-21 20:18:30.000000000 +0100
  3980. @@ -35,6 +35,7 @@
  3981. #include <linux/uaccess.h>
  3982. #include <linux/io.h>
  3983. #include <linux/kdebug.h>
  3984. +#include <linux/highmem.h>
  3985. #include <asm/pgtable.h>
  3986. #include <asm/ldt.h>
  3987. @@ -210,6 +211,35 @@
  3988. }
  3989. EXPORT_SYMBOL_GPL(start_thread);
  3990. +#ifdef CONFIG_PREEMPT_RT_FULL
  3991. +static void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  3992. +{
  3993. + int i;
  3994. +
  3995. + /*
  3996. + * Clear @prev's kmap_atomic mappings
  3997. + */
  3998. + for (i = 0; i < prev_p->kmap_idx; i++) {
  3999. + int idx = i + KM_TYPE_NR * smp_processor_id();
  4000. + pte_t *ptep = kmap_pte - idx;
  4001. +
  4002. + kpte_clear_flush(ptep, __fix_to_virt(FIX_KMAP_BEGIN + idx));
  4003. + }
  4004. + /*
  4005. + * Restore @next_p's kmap_atomic mappings
  4006. + */
  4007. + for (i = 0; i < next_p->kmap_idx; i++) {
  4008. + int idx = i + KM_TYPE_NR * smp_processor_id();
  4009. +
  4010. + if (!pte_none(next_p->kmap_pte[i]))
  4011. + set_pte(kmap_pte - idx, next_p->kmap_pte[i]);
  4012. + }
  4013. +}
  4014. +#else
  4015. +static inline void
  4016. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  4017. +#endif
  4018. +
  4019. /*
  4020. * switch_to(x,y) should switch tasks from x to y.
  4021. @@ -292,6 +322,8 @@
  4022. task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
  4023. __switch_to_xtra(prev_p, next_p, tss);
  4024. + switch_kmaps(prev_p, next_p);
  4025. +
  4026. /*
  4027. * Leave lazy mode, flushing any hypercalls made here.
  4028. * This must be done before restoring TLS segments so
  4029. diff -Nur linux-4.1.20.orig/arch/x86/kernel/signal.c linux-4.1.20/arch/x86/kernel/signal.c
  4030. --- linux-4.1.20.orig/arch/x86/kernel/signal.c 2016-03-17 19:11:03.000000000 +0100
  4031. +++ linux-4.1.20/arch/x86/kernel/signal.c 2016-03-21 20:18:30.000000000 +0100
  4032. @@ -726,6 +726,14 @@
  4033. {
  4034. user_exit();
  4035. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  4036. + if (unlikely(current->forced_info.si_signo)) {
  4037. + struct task_struct *t = current;
  4038. + force_sig_info(t->forced_info.si_signo, &t->forced_info, t);
  4039. + t->forced_info.si_signo = 0;
  4040. + }
  4041. +#endif
  4042. +
  4043. if (thread_info_flags & _TIF_UPROBE)
  4044. uprobe_notify_resume(regs);
  4045. diff -Nur linux-4.1.20.orig/arch/x86/kvm/lapic.c linux-4.1.20/arch/x86/kvm/lapic.c
  4046. --- linux-4.1.20.orig/arch/x86/kvm/lapic.c 2016-03-17 19:11:03.000000000 +0100
  4047. +++ linux-4.1.20/arch/x86/kvm/lapic.c 2016-03-21 20:18:30.000000000 +0100
  4048. @@ -1106,7 +1106,7 @@
  4049. static void apic_timer_expired(struct kvm_lapic *apic)
  4050. {
  4051. struct kvm_vcpu *vcpu = apic->vcpu;
  4052. - wait_queue_head_t *q = &vcpu->wq;
  4053. + struct swait_head *q = &vcpu->wq;
  4054. struct kvm_timer *ktimer = &apic->lapic_timer;
  4055. if (atomic_read(&apic->lapic_timer.pending))
  4056. @@ -1115,8 +1115,8 @@
  4057. atomic_inc(&apic->lapic_timer.pending);
  4058. kvm_set_pending_timer(vcpu);
  4059. - if (waitqueue_active(q))
  4060. - wake_up_interruptible(q);
  4061. + if (swaitqueue_active(q))
  4062. + swait_wake_interruptible(q);
  4063. if (apic_lvtt_tscdeadline(apic))
  4064. ktimer->expired_tscdeadline = ktimer->tscdeadline;
  4065. @@ -1169,8 +1169,36 @@
  4066. __delay(tsc_deadline - guest_tsc);
  4067. }
  4068. +static enum hrtimer_restart apic_timer_fn(struct hrtimer *data);
  4069. +
  4070. +static void __apic_timer_expired(struct hrtimer *data)
  4071. +{
  4072. + int ret, i = 0;
  4073. + enum hrtimer_restart r;
  4074. + struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
  4075. +
  4076. + r = apic_timer_fn(data);
  4077. +
  4078. + if (r == HRTIMER_RESTART) {
  4079. + do {
  4080. + ret = hrtimer_start_expires(data, HRTIMER_MODE_ABS);
  4081. + if (ret == -ETIME)
  4082. + hrtimer_add_expires_ns(&ktimer->timer,
  4083. + ktimer->period);
  4084. + i++;
  4085. + } while (ret == -ETIME && i < 10);
  4086. +
  4087. + if (ret == -ETIME) {
  4088. + printk_once(KERN_ERR "%s: failed to reprogram timer\n",
  4089. + __func__);
  4090. + WARN_ON_ONCE(1);
  4091. + }
  4092. + }
  4093. +}
  4094. +
  4095. static void start_apic_timer(struct kvm_lapic *apic)
  4096. {
  4097. + int ret;
  4098. ktime_t now;
  4099. atomic_set(&apic->lapic_timer.pending, 0);
  4100. @@ -1201,9 +1229,11 @@
  4101. }
  4102. }
  4103. - hrtimer_start(&apic->lapic_timer.timer,
  4104. + ret = hrtimer_start(&apic->lapic_timer.timer,
  4105. ktime_add_ns(now, apic->lapic_timer.period),
  4106. HRTIMER_MODE_ABS);
  4107. + if (ret == -ETIME)
  4108. + __apic_timer_expired(&apic->lapic_timer.timer);
  4109. apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
  4110. PRIx64 ", "
  4111. @@ -1235,8 +1265,10 @@
  4112. do_div(ns, this_tsc_khz);
  4113. expire = ktime_add_ns(now, ns);
  4114. expire = ktime_sub_ns(expire, lapic_timer_advance_ns);
  4115. - hrtimer_start(&apic->lapic_timer.timer,
  4116. + ret = hrtimer_start(&apic->lapic_timer.timer,
  4117. expire, HRTIMER_MODE_ABS);
  4118. + if (ret == -ETIME)
  4119. + __apic_timer_expired(&apic->lapic_timer.timer);
  4120. } else
  4121. apic_timer_expired(apic);
  4122. @@ -1709,6 +1741,7 @@
  4123. hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
  4124. HRTIMER_MODE_ABS);
  4125. apic->lapic_timer.timer.function = apic_timer_fn;
  4126. + apic->lapic_timer.timer.irqsafe = 1;
  4127. /*
  4128. * APIC is created enabled. This will prevent kvm_lapic_set_base from
  4129. @@ -1836,7 +1869,8 @@
  4130. timer = &vcpu->arch.apic->lapic_timer.timer;
  4131. if (hrtimer_cancel(timer))
  4132. - hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  4133. + if (hrtimer_start_expires(timer, HRTIMER_MODE_ABS) == -ETIME)
  4134. + __apic_timer_expired(timer);
  4135. }
  4136. /*
  4137. diff -Nur linux-4.1.20.orig/arch/x86/kvm/x86.c linux-4.1.20/arch/x86/kvm/x86.c
  4138. --- linux-4.1.20.orig/arch/x86/kvm/x86.c 2016-03-17 19:11:03.000000000 +0100
  4139. +++ linux-4.1.20/arch/x86/kvm/x86.c 2016-03-21 20:18:30.000000000 +0100
  4140. @@ -5809,6 +5809,13 @@
  4141. goto out;
  4142. }
  4143. +#ifdef CONFIG_PREEMPT_RT_FULL
  4144. + if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  4145. + printk(KERN_ERR "RT requires X86_FEATURE_CONSTANT_TSC\n");
  4146. + return -EOPNOTSUPP;
  4147. + }
  4148. +#endif
  4149. +
  4150. r = kvm_mmu_module_init();
  4151. if (r)
  4152. goto out_free_percpu;
  4153. diff -Nur linux-4.1.20.orig/arch/x86/lib/usercopy_32.c linux-4.1.20/arch/x86/lib/usercopy_32.c
  4154. --- linux-4.1.20.orig/arch/x86/lib/usercopy_32.c 2016-03-17 19:11:03.000000000 +0100
  4155. +++ linux-4.1.20/arch/x86/lib/usercopy_32.c 2016-03-21 20:18:30.000000000 +0100
  4156. @@ -647,7 +647,8 @@
  4157. * @from: Source address, in kernel space.
  4158. * @n: Number of bytes to copy.
  4159. *
  4160. - * Context: User context only. This function may sleep.
  4161. + * Context: User context only. This function may sleep if pagefaults are
  4162. + * enabled.
  4163. *
  4164. * Copy data from kernel space to user space.
  4165. *
  4166. @@ -668,7 +669,8 @@
  4167. * @from: Source address, in user space.
  4168. * @n: Number of bytes to copy.
  4169. *
  4170. - * Context: User context only. This function may sleep.
  4171. + * Context: User context only. This function may sleep if pagefaults are
  4172. + * enabled.
  4173. *
  4174. * Copy data from user space to kernel space.
  4175. *
  4176. diff -Nur linux-4.1.20.orig/arch/x86/mm/fault.c linux-4.1.20/arch/x86/mm/fault.c
  4177. --- linux-4.1.20.orig/arch/x86/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  4178. +++ linux-4.1.20/arch/x86/mm/fault.c 2016-03-21 20:18:30.000000000 +0100
  4179. @@ -13,6 +13,7 @@
  4180. #include <linux/hugetlb.h> /* hstate_index_to_shift */
  4181. #include <linux/prefetch.h> /* prefetchw */
  4182. #include <linux/context_tracking.h> /* exception_enter(), ... */
  4183. +#include <linux/uaccess.h> /* faulthandler_disabled() */
  4184. #include <asm/traps.h> /* dotraplinkage, ... */
  4185. #include <asm/pgalloc.h> /* pgd_*(), ... */
  4186. @@ -1133,9 +1134,9 @@
  4187. /*
  4188. * If we're in an interrupt, have no user context or are running
  4189. - * in an atomic region then we must not take the fault:
  4190. + * in a region with pagefaults disabled then we must not take the fault
  4191. */
  4192. - if (unlikely(in_atomic() || !mm)) {
  4193. + if (unlikely(faulthandler_disabled() || !mm)) {
  4194. bad_area_nosemaphore(regs, error_code, address);
  4195. return;
  4196. }
  4197. diff -Nur linux-4.1.20.orig/arch/x86/mm/highmem_32.c linux-4.1.20/arch/x86/mm/highmem_32.c
  4198. --- linux-4.1.20.orig/arch/x86/mm/highmem_32.c 2016-03-17 19:11:03.000000000 +0100
  4199. +++ linux-4.1.20/arch/x86/mm/highmem_32.c 2016-03-21 20:18:30.000000000 +0100
  4200. @@ -32,10 +32,11 @@
  4201. */
  4202. void *kmap_atomic_prot(struct page *page, pgprot_t prot)
  4203. {
  4204. + pte_t pte = mk_pte(page, prot);
  4205. unsigned long vaddr;
  4206. int idx, type;
  4207. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  4208. + preempt_disable_nort();
  4209. pagefault_disable();
  4210. if (!PageHighMem(page))
  4211. @@ -45,7 +46,10 @@
  4212. idx = type + KM_TYPE_NR*smp_processor_id();
  4213. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  4214. BUG_ON(!pte_none(*(kmap_pte-idx)));
  4215. - set_pte(kmap_pte-idx, mk_pte(page, prot));
  4216. +#ifdef CONFIG_PREEMPT_RT_FULL
  4217. + current->kmap_pte[type] = pte;
  4218. +#endif
  4219. + set_pte(kmap_pte-idx, pte);
  4220. arch_flush_lazy_mmu_mode();
  4221. return (void *)vaddr;
  4222. @@ -88,6 +92,9 @@
  4223. * is a bad idea also, in case the page changes cacheability
  4224. * attributes or becomes a protected page in a hypervisor.
  4225. */
  4226. +#ifdef CONFIG_PREEMPT_RT_FULL
  4227. + current->kmap_pte[type] = __pte(0);
  4228. +#endif
  4229. kpte_clear_flush(kmap_pte-idx, vaddr);
  4230. kmap_atomic_idx_pop();
  4231. arch_flush_lazy_mmu_mode();
  4232. @@ -100,6 +107,7 @@
  4233. #endif
  4234. pagefault_enable();
  4235. + preempt_enable_nort();
  4236. }
  4237. EXPORT_SYMBOL(__kunmap_atomic);
  4238. diff -Nur linux-4.1.20.orig/arch/x86/mm/iomap_32.c linux-4.1.20/arch/x86/mm/iomap_32.c
  4239. --- linux-4.1.20.orig/arch/x86/mm/iomap_32.c 2016-03-17 19:11:03.000000000 +0100
  4240. +++ linux-4.1.20/arch/x86/mm/iomap_32.c 2016-03-21 20:18:30.000000000 +0100
  4241. @@ -56,15 +56,22 @@
  4242. void *kmap_atomic_prot_pfn(unsigned long pfn, pgprot_t prot)
  4243. {
  4244. + pte_t pte = pfn_pte(pfn, prot);
  4245. unsigned long vaddr;
  4246. int idx, type;
  4247. + preempt_disable();
  4248. pagefault_disable();
  4249. type = kmap_atomic_idx_push();
  4250. idx = type + KM_TYPE_NR * smp_processor_id();
  4251. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  4252. - set_pte(kmap_pte - idx, pfn_pte(pfn, prot));
  4253. + WARN_ON(!pte_none(*(kmap_pte - idx)));
  4254. +
  4255. +#ifdef CONFIG_PREEMPT_RT_FULL
  4256. + current->kmap_pte[type] = pte;
  4257. +#endif
  4258. + set_pte(kmap_pte - idx, pte);
  4259. arch_flush_lazy_mmu_mode();
  4260. return (void *)vaddr;
  4261. @@ -112,10 +119,14 @@
  4262. * is a bad idea also, in case the page changes cacheability
  4263. * attributes or becomes a protected page in a hypervisor.
  4264. */
  4265. +#ifdef CONFIG_PREEMPT_RT_FULL
  4266. + current->kmap_pte[type] = __pte(0);
  4267. +#endif
  4268. kpte_clear_flush(kmap_pte-idx, vaddr);
  4269. kmap_atomic_idx_pop();
  4270. }
  4271. pagefault_enable();
  4272. + preempt_enable();
  4273. }
  4274. EXPORT_SYMBOL_GPL(iounmap_atomic);
  4275. diff -Nur linux-4.1.20.orig/arch/x86/platform/uv/tlb_uv.c linux-4.1.20/arch/x86/platform/uv/tlb_uv.c
  4276. --- linux-4.1.20.orig/arch/x86/platform/uv/tlb_uv.c 2016-03-17 19:11:03.000000000 +0100
  4277. +++ linux-4.1.20/arch/x86/platform/uv/tlb_uv.c 2016-03-21 20:18:30.000000000 +0100
  4278. @@ -714,9 +714,9 @@
  4279. quiesce_local_uvhub(hmaster);
  4280. - spin_lock(&hmaster->queue_lock);
  4281. + raw_spin_lock(&hmaster->queue_lock);
  4282. reset_with_ipi(&bau_desc->distribution, bcp);
  4283. - spin_unlock(&hmaster->queue_lock);
  4284. + raw_spin_unlock(&hmaster->queue_lock);
  4285. end_uvhub_quiesce(hmaster);
  4286. @@ -736,9 +736,9 @@
  4287. quiesce_local_uvhub(hmaster);
  4288. - spin_lock(&hmaster->queue_lock);
  4289. + raw_spin_lock(&hmaster->queue_lock);
  4290. reset_with_ipi(&bau_desc->distribution, bcp);
  4291. - spin_unlock(&hmaster->queue_lock);
  4292. + raw_spin_unlock(&hmaster->queue_lock);
  4293. end_uvhub_quiesce(hmaster);
  4294. @@ -759,7 +759,7 @@
  4295. cycles_t tm1;
  4296. hmaster = bcp->uvhub_master;
  4297. - spin_lock(&hmaster->disable_lock);
  4298. + raw_spin_lock(&hmaster->disable_lock);
  4299. if (!bcp->baudisabled) {
  4300. stat->s_bau_disabled++;
  4301. tm1 = get_cycles();
  4302. @@ -772,7 +772,7 @@
  4303. }
  4304. }
  4305. }
  4306. - spin_unlock(&hmaster->disable_lock);
  4307. + raw_spin_unlock(&hmaster->disable_lock);
  4308. }
  4309. static void count_max_concurr(int stat, struct bau_control *bcp,
  4310. @@ -835,7 +835,7 @@
  4311. */
  4312. static void uv1_throttle(struct bau_control *hmaster, struct ptc_stats *stat)
  4313. {
  4314. - spinlock_t *lock = &hmaster->uvhub_lock;
  4315. + raw_spinlock_t *lock = &hmaster->uvhub_lock;
  4316. atomic_t *v;
  4317. v = &hmaster->active_descriptor_count;
  4318. @@ -968,7 +968,7 @@
  4319. struct bau_control *hmaster;
  4320. hmaster = bcp->uvhub_master;
  4321. - spin_lock(&hmaster->disable_lock);
  4322. + raw_spin_lock(&hmaster->disable_lock);
  4323. if (bcp->baudisabled && (get_cycles() >= bcp->set_bau_on_time)) {
  4324. stat->s_bau_reenabled++;
  4325. for_each_present_cpu(tcpu) {
  4326. @@ -980,10 +980,10 @@
  4327. tbcp->period_giveups = 0;
  4328. }
  4329. }
  4330. - spin_unlock(&hmaster->disable_lock);
  4331. + raw_spin_unlock(&hmaster->disable_lock);
  4332. return 0;
  4333. }
  4334. - spin_unlock(&hmaster->disable_lock);
  4335. + raw_spin_unlock(&hmaster->disable_lock);
  4336. return -1;
  4337. }
  4338. @@ -1901,9 +1901,9 @@
  4339. bcp->cong_reps = congested_reps;
  4340. bcp->disabled_period = sec_2_cycles(disabled_period);
  4341. bcp->giveup_limit = giveup_limit;
  4342. - spin_lock_init(&bcp->queue_lock);
  4343. - spin_lock_init(&bcp->uvhub_lock);
  4344. - spin_lock_init(&bcp->disable_lock);
  4345. + raw_spin_lock_init(&bcp->queue_lock);
  4346. + raw_spin_lock_init(&bcp->uvhub_lock);
  4347. + raw_spin_lock_init(&bcp->disable_lock);
  4348. }
  4349. }
  4350. diff -Nur linux-4.1.20.orig/arch/x86/platform/uv/uv_time.c linux-4.1.20/arch/x86/platform/uv/uv_time.c
  4351. --- linux-4.1.20.orig/arch/x86/platform/uv/uv_time.c 2016-03-17 19:11:03.000000000 +0100
  4352. +++ linux-4.1.20/arch/x86/platform/uv/uv_time.c 2016-03-21 20:18:30.000000000 +0100
  4353. @@ -58,7 +58,7 @@
  4354. /* There is one of these allocated per node */
  4355. struct uv_rtc_timer_head {
  4356. - spinlock_t lock;
  4357. + raw_spinlock_t lock;
  4358. /* next cpu waiting for timer, local node relative: */
  4359. int next_cpu;
  4360. /* number of cpus on this node: */
  4361. @@ -178,7 +178,7 @@
  4362. uv_rtc_deallocate_timers();
  4363. return -ENOMEM;
  4364. }
  4365. - spin_lock_init(&head->lock);
  4366. + raw_spin_lock_init(&head->lock);
  4367. head->ncpus = uv_blade_nr_possible_cpus(bid);
  4368. head->next_cpu = -1;
  4369. blade_info[bid] = head;
  4370. @@ -232,7 +232,7 @@
  4371. unsigned long flags;
  4372. int next_cpu;
  4373. - spin_lock_irqsave(&head->lock, flags);
  4374. + raw_spin_lock_irqsave(&head->lock, flags);
  4375. next_cpu = head->next_cpu;
  4376. *t = expires;
  4377. @@ -244,12 +244,12 @@
  4378. if (uv_setup_intr(cpu, expires)) {
  4379. *t = ULLONG_MAX;
  4380. uv_rtc_find_next_timer(head, pnode);
  4381. - spin_unlock_irqrestore(&head->lock, flags);
  4382. + raw_spin_unlock_irqrestore(&head->lock, flags);
  4383. return -ETIME;
  4384. }
  4385. }
  4386. - spin_unlock_irqrestore(&head->lock, flags);
  4387. + raw_spin_unlock_irqrestore(&head->lock, flags);
  4388. return 0;
  4389. }
  4390. @@ -268,7 +268,7 @@
  4391. unsigned long flags;
  4392. int rc = 0;
  4393. - spin_lock_irqsave(&head->lock, flags);
  4394. + raw_spin_lock_irqsave(&head->lock, flags);
  4395. if ((head->next_cpu == bcpu && uv_read_rtc(NULL) >= *t) || force)
  4396. rc = 1;
  4397. @@ -280,7 +280,7 @@
  4398. uv_rtc_find_next_timer(head, pnode);
  4399. }
  4400. - spin_unlock_irqrestore(&head->lock, flags);
  4401. + raw_spin_unlock_irqrestore(&head->lock, flags);
  4402. return rc;
  4403. }
  4404. @@ -300,13 +300,18 @@
  4405. static cycle_t uv_read_rtc(struct clocksource *cs)
  4406. {
  4407. unsigned long offset;
  4408. + cycle_t cycles;
  4409. + preempt_disable();
  4410. if (uv_get_min_hub_revision_id() == 1)
  4411. offset = 0;
  4412. else
  4413. offset = (uv_blade_processor_id() * L1_CACHE_BYTES) % PAGE_SIZE;
  4414. - return (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  4415. + cycles = (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  4416. + preempt_enable();
  4417. +
  4418. + return cycles;
  4419. }
  4420. /*
  4421. diff -Nur linux-4.1.20.orig/arch/xtensa/mm/fault.c linux-4.1.20/arch/xtensa/mm/fault.c
  4422. --- linux-4.1.20.orig/arch/xtensa/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  4423. +++ linux-4.1.20/arch/xtensa/mm/fault.c 2016-03-21 20:18:30.000000000 +0100
  4424. @@ -15,10 +15,10 @@
  4425. #include <linux/mm.h>
  4426. #include <linux/module.h>
  4427. #include <linux/hardirq.h>
  4428. +#include <linux/uaccess.h>
  4429. #include <asm/mmu_context.h>
  4430. #include <asm/cacheflush.h>
  4431. #include <asm/hardirq.h>
  4432. -#include <asm/uaccess.h>
  4433. #include <asm/pgalloc.h>
  4434. DEFINE_PER_CPU(unsigned long, asid_cache) = ASID_USER_FIRST;
  4435. @@ -57,7 +57,7 @@
  4436. /* If we're in an interrupt or have no user
  4437. * context, we must not take the fault..
  4438. */
  4439. - if (in_atomic() || !mm) {
  4440. + if (faulthandler_disabled() || !mm) {
  4441. bad_page_fault(regs, address, SIGSEGV);
  4442. return;
  4443. }
  4444. diff -Nur linux-4.1.20.orig/arch/xtensa/mm/highmem.c linux-4.1.20/arch/xtensa/mm/highmem.c
  4445. --- linux-4.1.20.orig/arch/xtensa/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  4446. +++ linux-4.1.20/arch/xtensa/mm/highmem.c 2016-03-21 20:18:30.000000000 +0100
  4447. @@ -42,6 +42,7 @@
  4448. enum fixed_addresses idx;
  4449. unsigned long vaddr;
  4450. + preempt_disable();
  4451. pagefault_disable();
  4452. if (!PageHighMem(page))
  4453. return page_address(page);
  4454. @@ -79,6 +80,7 @@
  4455. }
  4456. pagefault_enable();
  4457. + preempt_enable();
  4458. }
  4459. EXPORT_SYMBOL(__kunmap_atomic);
  4460. diff -Nur linux-4.1.20.orig/block/blk-core.c linux-4.1.20/block/blk-core.c
  4461. --- linux-4.1.20.orig/block/blk-core.c 2016-03-17 19:11:03.000000000 +0100
  4462. +++ linux-4.1.20/block/blk-core.c 2016-03-21 20:18:30.000000000 +0100
  4463. @@ -100,6 +100,9 @@
  4464. INIT_LIST_HEAD(&rq->queuelist);
  4465. INIT_LIST_HEAD(&rq->timeout_list);
  4466. +#ifdef CONFIG_PREEMPT_RT_FULL
  4467. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  4468. +#endif
  4469. rq->cpu = -1;
  4470. rq->q = q;
  4471. rq->__sector = (sector_t) -1;
  4472. @@ -194,7 +197,7 @@
  4473. **/
  4474. void blk_start_queue(struct request_queue *q)
  4475. {
  4476. - WARN_ON(!irqs_disabled());
  4477. + WARN_ON_NONRT(!irqs_disabled());
  4478. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  4479. __blk_run_queue(q);
  4480. @@ -661,7 +664,7 @@
  4481. q->bypass_depth = 1;
  4482. __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
  4483. - init_waitqueue_head(&q->mq_freeze_wq);
  4484. + init_swait_head(&q->mq_freeze_wq);
  4485. if (blkcg_init_queue(q))
  4486. goto fail_bdi;
  4487. @@ -3077,7 +3080,7 @@
  4488. blk_run_queue_async(q);
  4489. else
  4490. __blk_run_queue(q);
  4491. - spin_unlock(q->queue_lock);
  4492. + spin_unlock_irq(q->queue_lock);
  4493. }
  4494. static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
  4495. @@ -3125,7 +3128,6 @@
  4496. void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  4497. {
  4498. struct request_queue *q;
  4499. - unsigned long flags;
  4500. struct request *rq;
  4501. LIST_HEAD(list);
  4502. unsigned int depth;
  4503. @@ -3145,11 +3147,6 @@
  4504. q = NULL;
  4505. depth = 0;
  4506. - /*
  4507. - * Save and disable interrupts here, to avoid doing it for every
  4508. - * queue lock we have to take.
  4509. - */
  4510. - local_irq_save(flags);
  4511. while (!list_empty(&list)) {
  4512. rq = list_entry_rq(list.next);
  4513. list_del_init(&rq->queuelist);
  4514. @@ -3162,7 +3159,7 @@
  4515. queue_unplugged(q, depth, from_schedule);
  4516. q = rq->q;
  4517. depth = 0;
  4518. - spin_lock(q->queue_lock);
  4519. + spin_lock_irq(q->queue_lock);
  4520. }
  4521. /*
  4522. @@ -3189,8 +3186,6 @@
  4523. */
  4524. if (q)
  4525. queue_unplugged(q, depth, from_schedule);
  4526. -
  4527. - local_irq_restore(flags);
  4528. }
  4529. void blk_finish_plug(struct blk_plug *plug)
  4530. diff -Nur linux-4.1.20.orig/block/blk-ioc.c linux-4.1.20/block/blk-ioc.c
  4531. --- linux-4.1.20.orig/block/blk-ioc.c 2016-03-17 19:11:03.000000000 +0100
  4532. +++ linux-4.1.20/block/blk-ioc.c 2016-03-21 20:18:31.000000000 +0100
  4533. @@ -7,6 +7,7 @@
  4534. #include <linux/bio.h>
  4535. #include <linux/blkdev.h>
  4536. #include <linux/slab.h>
  4537. +#include <linux/delay.h>
  4538. #include "blk.h"
  4539. @@ -109,7 +110,7 @@
  4540. spin_unlock(q->queue_lock);
  4541. } else {
  4542. spin_unlock_irqrestore(&ioc->lock, flags);
  4543. - cpu_relax();
  4544. + cpu_chill();
  4545. spin_lock_irqsave_nested(&ioc->lock, flags, 1);
  4546. }
  4547. }
  4548. @@ -187,7 +188,7 @@
  4549. spin_unlock(icq->q->queue_lock);
  4550. } else {
  4551. spin_unlock_irqrestore(&ioc->lock, flags);
  4552. - cpu_relax();
  4553. + cpu_chill();
  4554. goto retry;
  4555. }
  4556. }
  4557. diff -Nur linux-4.1.20.orig/block/blk-iopoll.c linux-4.1.20/block/blk-iopoll.c
  4558. --- linux-4.1.20.orig/block/blk-iopoll.c 2016-03-17 19:11:03.000000000 +0100
  4559. +++ linux-4.1.20/block/blk-iopoll.c 2016-03-21 20:18:31.000000000 +0100
  4560. @@ -35,6 +35,7 @@
  4561. list_add_tail(&iop->list, this_cpu_ptr(&blk_cpu_iopoll));
  4562. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  4563. local_irq_restore(flags);
  4564. + preempt_check_resched_rt();
  4565. }
  4566. EXPORT_SYMBOL(blk_iopoll_sched);
  4567. @@ -132,6 +133,7 @@
  4568. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  4569. local_irq_enable();
  4570. + preempt_check_resched_rt();
  4571. }
  4572. /**
  4573. @@ -201,6 +203,7 @@
  4574. this_cpu_ptr(&blk_cpu_iopoll));
  4575. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  4576. local_irq_enable();
  4577. + preempt_check_resched_rt();
  4578. }
  4579. return NOTIFY_OK;
  4580. diff -Nur linux-4.1.20.orig/block/blk-mq-cpu.c linux-4.1.20/block/blk-mq-cpu.c
  4581. --- linux-4.1.20.orig/block/blk-mq-cpu.c 2016-03-17 19:11:03.000000000 +0100
  4582. +++ linux-4.1.20/block/blk-mq-cpu.c 2016-03-21 20:18:31.000000000 +0100
  4583. @@ -16,7 +16,7 @@
  4584. #include "blk-mq.h"
  4585. static LIST_HEAD(blk_mq_cpu_notify_list);
  4586. -static DEFINE_RAW_SPINLOCK(blk_mq_cpu_notify_lock);
  4587. +static DEFINE_SPINLOCK(blk_mq_cpu_notify_lock);
  4588. static int blk_mq_main_cpu_notify(struct notifier_block *self,
  4589. unsigned long action, void *hcpu)
  4590. @@ -25,7 +25,10 @@
  4591. struct blk_mq_cpu_notifier *notify;
  4592. int ret = NOTIFY_OK;
  4593. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  4594. + if (action != CPU_POST_DEAD)
  4595. + return NOTIFY_OK;
  4596. +
  4597. + spin_lock(&blk_mq_cpu_notify_lock);
  4598. list_for_each_entry(notify, &blk_mq_cpu_notify_list, list) {
  4599. ret = notify->notify(notify->data, action, cpu);
  4600. @@ -33,7 +36,7 @@
  4601. break;
  4602. }
  4603. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  4604. + spin_unlock(&blk_mq_cpu_notify_lock);
  4605. return ret;
  4606. }
  4607. @@ -41,16 +44,16 @@
  4608. {
  4609. BUG_ON(!notifier->notify);
  4610. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  4611. + spin_lock(&blk_mq_cpu_notify_lock);
  4612. list_add_tail(&notifier->list, &blk_mq_cpu_notify_list);
  4613. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  4614. + spin_unlock(&blk_mq_cpu_notify_lock);
  4615. }
  4616. void blk_mq_unregister_cpu_notifier(struct blk_mq_cpu_notifier *notifier)
  4617. {
  4618. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  4619. + spin_lock(&blk_mq_cpu_notify_lock);
  4620. list_del(&notifier->list);
  4621. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  4622. + spin_unlock(&blk_mq_cpu_notify_lock);
  4623. }
  4624. void blk_mq_init_cpu_notifier(struct blk_mq_cpu_notifier *notifier,
  4625. diff -Nur linux-4.1.20.orig/block/blk-mq.c linux-4.1.20/block/blk-mq.c
  4626. --- linux-4.1.20.orig/block/blk-mq.c 2016-03-17 19:11:03.000000000 +0100
  4627. +++ linux-4.1.20/block/blk-mq.c 2016-03-21 20:18:31.000000000 +0100
  4628. @@ -88,7 +88,7 @@
  4629. if (!(gfp & __GFP_WAIT))
  4630. return -EBUSY;
  4631. - ret = wait_event_interruptible(q->mq_freeze_wq,
  4632. + ret = swait_event_interruptible(q->mq_freeze_wq,
  4633. !q->mq_freeze_depth || blk_queue_dying(q));
  4634. if (blk_queue_dying(q))
  4635. return -ENODEV;
  4636. @@ -107,7 +107,7 @@
  4637. struct request_queue *q =
  4638. container_of(ref, struct request_queue, mq_usage_counter);
  4639. - wake_up_all(&q->mq_freeze_wq);
  4640. + swait_wake_all(&q->mq_freeze_wq);
  4641. }
  4642. void blk_mq_freeze_queue_start(struct request_queue *q)
  4643. @@ -127,7 +127,7 @@
  4644. static void blk_mq_freeze_queue_wait(struct request_queue *q)
  4645. {
  4646. - wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->mq_usage_counter));
  4647. + swait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->mq_usage_counter));
  4648. }
  4649. /*
  4650. @@ -151,7 +151,7 @@
  4651. spin_unlock_irq(q->queue_lock);
  4652. if (wake) {
  4653. percpu_ref_reinit(&q->mq_usage_counter);
  4654. - wake_up_all(&q->mq_freeze_wq);
  4655. + swait_wake_all(&q->mq_freeze_wq);
  4656. }
  4657. }
  4658. EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
  4659. @@ -170,7 +170,7 @@
  4660. * dying, we need to ensure that processes currently waiting on
  4661. * the queue are notified as well.
  4662. */
  4663. - wake_up_all(&q->mq_freeze_wq);
  4664. + swait_wake_all(&q->mq_freeze_wq);
  4665. }
  4666. bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
  4667. @@ -217,6 +217,9 @@
  4668. rq->resid_len = 0;
  4669. rq->sense = NULL;
  4670. +#ifdef CONFIG_PREEMPT_RT_FULL
  4671. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  4672. +#endif
  4673. INIT_LIST_HEAD(&rq->timeout_list);
  4674. rq->timeout = 0;
  4675. @@ -346,6 +349,17 @@
  4676. }
  4677. EXPORT_SYMBOL(blk_mq_end_request);
  4678. +#ifdef CONFIG_PREEMPT_RT_FULL
  4679. +
  4680. +void __blk_mq_complete_request_remote_work(struct work_struct *work)
  4681. +{
  4682. + struct request *rq = container_of(work, struct request, work);
  4683. +
  4684. + rq->q->softirq_done_fn(rq);
  4685. +}
  4686. +
  4687. +#else
  4688. +
  4689. static void __blk_mq_complete_request_remote(void *data)
  4690. {
  4691. struct request *rq = data;
  4692. @@ -353,6 +367,8 @@
  4693. rq->q->softirq_done_fn(rq);
  4694. }
  4695. +#endif
  4696. +
  4697. static void blk_mq_ipi_complete_request(struct request *rq)
  4698. {
  4699. struct blk_mq_ctx *ctx = rq->mq_ctx;
  4700. @@ -364,19 +380,23 @@
  4701. return;
  4702. }
  4703. - cpu = get_cpu();
  4704. + cpu = get_cpu_light();
  4705. if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
  4706. shared = cpus_share_cache(cpu, ctx->cpu);
  4707. if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
  4708. +#ifdef CONFIG_PREEMPT_RT_FULL
  4709. + schedule_work_on(ctx->cpu, &rq->work);
  4710. +#else
  4711. rq->csd.func = __blk_mq_complete_request_remote;
  4712. rq->csd.info = rq;
  4713. rq->csd.flags = 0;
  4714. smp_call_function_single_async(ctx->cpu, &rq->csd);
  4715. +#endif
  4716. } else {
  4717. rq->q->softirq_done_fn(rq);
  4718. }
  4719. - put_cpu();
  4720. + put_cpu_light();
  4721. }
  4722. void __blk_mq_complete_request(struct request *rq)
  4723. @@ -905,14 +925,14 @@
  4724. return;
  4725. if (!async) {
  4726. - int cpu = get_cpu();
  4727. + int cpu = get_cpu_light();
  4728. if (cpumask_test_cpu(cpu, hctx->cpumask)) {
  4729. __blk_mq_run_hw_queue(hctx);
  4730. - put_cpu();
  4731. + put_cpu_light();
  4732. return;
  4733. }
  4734. - put_cpu();
  4735. + put_cpu_light();
  4736. }
  4737. kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
  4738. @@ -1589,7 +1609,7 @@
  4739. {
  4740. struct blk_mq_hw_ctx *hctx = data;
  4741. - if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
  4742. + if (action == CPU_POST_DEAD)
  4743. return blk_mq_hctx_cpu_offline(hctx, cpu);
  4744. /*
  4745. diff -Nur linux-4.1.20.orig/block/blk-mq.h linux-4.1.20/block/blk-mq.h
  4746. --- linux-4.1.20.orig/block/blk-mq.h 2016-03-17 19:11:03.000000000 +0100
  4747. +++ linux-4.1.20/block/blk-mq.h 2016-03-21 20:18:31.000000000 +0100
  4748. @@ -76,7 +76,10 @@
  4749. static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
  4750. unsigned int cpu)
  4751. {
  4752. - return per_cpu_ptr(q->queue_ctx, cpu);
  4753. + struct blk_mq_ctx *ctx;
  4754. +
  4755. + ctx = per_cpu_ptr(q->queue_ctx, cpu);
  4756. + return ctx;
  4757. }
  4758. /*
  4759. @@ -87,12 +90,12 @@
  4760. */
  4761. static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
  4762. {
  4763. - return __blk_mq_get_ctx(q, get_cpu());
  4764. + return __blk_mq_get_ctx(q, get_cpu_light());
  4765. }
  4766. static inline void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
  4767. {
  4768. - put_cpu();
  4769. + put_cpu_light();
  4770. }
  4771. struct blk_mq_alloc_data {
  4772. diff -Nur linux-4.1.20.orig/block/blk-softirq.c linux-4.1.20/block/blk-softirq.c
  4773. --- linux-4.1.20.orig/block/blk-softirq.c 2016-03-17 19:11:03.000000000 +0100
  4774. +++ linux-4.1.20/block/blk-softirq.c 2016-03-21 20:18:31.000000000 +0100
  4775. @@ -51,6 +51,7 @@
  4776. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  4777. local_irq_restore(flags);
  4778. + preempt_check_resched_rt();
  4779. }
  4780. /*
  4781. @@ -93,6 +94,7 @@
  4782. this_cpu_ptr(&blk_cpu_done));
  4783. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  4784. local_irq_enable();
  4785. + preempt_check_resched_rt();
  4786. }
  4787. return NOTIFY_OK;
  4788. @@ -150,6 +152,7 @@
  4789. goto do_local;
  4790. local_irq_restore(flags);
  4791. + preempt_check_resched_rt();
  4792. }
  4793. /**
  4794. diff -Nur linux-4.1.20.orig/block/bounce.c linux-4.1.20/block/bounce.c
  4795. --- linux-4.1.20.orig/block/bounce.c 2016-03-17 19:11:03.000000000 +0100
  4796. +++ linux-4.1.20/block/bounce.c 2016-03-21 20:18:31.000000000 +0100
  4797. @@ -54,11 +54,11 @@
  4798. unsigned long flags;
  4799. unsigned char *vto;
  4800. - local_irq_save(flags);
  4801. + local_irq_save_nort(flags);
  4802. vto = kmap_atomic(to->bv_page);
  4803. memcpy(vto + to->bv_offset, vfrom, to->bv_len);
  4804. kunmap_atomic(vto);
  4805. - local_irq_restore(flags);
  4806. + local_irq_restore_nort(flags);
  4807. }
  4808. #else /* CONFIG_HIGHMEM */
  4809. diff -Nur linux-4.1.20.orig/crypto/algapi.c linux-4.1.20/crypto/algapi.c
  4810. --- linux-4.1.20.orig/crypto/algapi.c 2016-03-17 19:11:03.000000000 +0100
  4811. +++ linux-4.1.20/crypto/algapi.c 2016-03-21 20:18:31.000000000 +0100
  4812. @@ -695,13 +695,13 @@
  4813. int crypto_register_notifier(struct notifier_block *nb)
  4814. {
  4815. - return blocking_notifier_chain_register(&crypto_chain, nb);
  4816. + return srcu_notifier_chain_register(&crypto_chain, nb);
  4817. }
  4818. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  4819. int crypto_unregister_notifier(struct notifier_block *nb)
  4820. {
  4821. - return blocking_notifier_chain_unregister(&crypto_chain, nb);
  4822. + return srcu_notifier_chain_unregister(&crypto_chain, nb);
  4823. }
  4824. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  4825. diff -Nur linux-4.1.20.orig/crypto/api.c linux-4.1.20/crypto/api.c
  4826. --- linux-4.1.20.orig/crypto/api.c 2016-03-17 19:11:03.000000000 +0100
  4827. +++ linux-4.1.20/crypto/api.c 2016-03-21 20:18:31.000000000 +0100
  4828. @@ -31,7 +31,7 @@
  4829. DECLARE_RWSEM(crypto_alg_sem);
  4830. EXPORT_SYMBOL_GPL(crypto_alg_sem);
  4831. -BLOCKING_NOTIFIER_HEAD(crypto_chain);
  4832. +SRCU_NOTIFIER_HEAD(crypto_chain);
  4833. EXPORT_SYMBOL_GPL(crypto_chain);
  4834. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg);
  4835. @@ -236,10 +236,10 @@
  4836. {
  4837. int ok;
  4838. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  4839. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  4840. if (ok == NOTIFY_DONE) {
  4841. request_module("cryptomgr");
  4842. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  4843. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  4844. }
  4845. return ok;
  4846. diff -Nur linux-4.1.20.orig/crypto/internal.h linux-4.1.20/crypto/internal.h
  4847. --- linux-4.1.20.orig/crypto/internal.h 2016-03-17 19:11:03.000000000 +0100
  4848. +++ linux-4.1.20/crypto/internal.h 2016-03-21 20:18:31.000000000 +0100
  4849. @@ -48,7 +48,7 @@
  4850. extern struct list_head crypto_alg_list;
  4851. extern struct rw_semaphore crypto_alg_sem;
  4852. -extern struct blocking_notifier_head crypto_chain;
  4853. +extern struct srcu_notifier_head crypto_chain;
  4854. #ifdef CONFIG_PROC_FS
  4855. void __init crypto_init_proc(void);
  4856. @@ -142,7 +142,7 @@
  4857. static inline void crypto_notify(unsigned long val, void *v)
  4858. {
  4859. - blocking_notifier_call_chain(&crypto_chain, val, v);
  4860. + srcu_notifier_call_chain(&crypto_chain, val, v);
  4861. }
  4862. #endif /* _CRYPTO_INTERNAL_H */
  4863. diff -Nur linux-4.1.20.orig/drivers/acpi/acpica/acglobal.h linux-4.1.20/drivers/acpi/acpica/acglobal.h
  4864. --- linux-4.1.20.orig/drivers/acpi/acpica/acglobal.h 2016-03-17 19:11:03.000000000 +0100
  4865. +++ linux-4.1.20/drivers/acpi/acpica/acglobal.h 2016-03-21 20:18:31.000000000 +0100
  4866. @@ -112,7 +112,7 @@
  4867. * interrupt level
  4868. */
  4869. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_gpe_lock); /* For GPE data structs and registers */
  4870. -ACPI_GLOBAL(acpi_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  4871. +ACPI_GLOBAL(acpi_raw_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  4872. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_reference_count_lock);
  4873. /* Mutex for _OSI support */
  4874. diff -Nur linux-4.1.20.orig/drivers/acpi/acpica/hwregs.c linux-4.1.20/drivers/acpi/acpica/hwregs.c
  4875. --- linux-4.1.20.orig/drivers/acpi/acpica/hwregs.c 2016-03-17 19:11:03.000000000 +0100
  4876. +++ linux-4.1.20/drivers/acpi/acpica/hwregs.c 2016-03-21 20:18:31.000000000 +0100
  4877. @@ -269,14 +269,14 @@
  4878. ACPI_BITMASK_ALL_FIXED_STATUS,
  4879. ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status.address)));
  4880. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  4881. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  4882. /* Clear the fixed events in PM1 A/B */
  4883. status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
  4884. ACPI_BITMASK_ALL_FIXED_STATUS);
  4885. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  4886. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  4887. if (ACPI_FAILURE(status)) {
  4888. goto exit;
  4889. diff -Nur linux-4.1.20.orig/drivers/acpi/acpica/hwxface.c linux-4.1.20/drivers/acpi/acpica/hwxface.c
  4890. --- linux-4.1.20.orig/drivers/acpi/acpica/hwxface.c 2016-03-17 19:11:03.000000000 +0100
  4891. +++ linux-4.1.20/drivers/acpi/acpica/hwxface.c 2016-03-21 20:18:31.000000000 +0100
  4892. @@ -374,7 +374,7 @@
  4893. return_ACPI_STATUS(AE_BAD_PARAMETER);
  4894. }
  4895. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  4896. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  4897. /*
  4898. * At this point, we know that the parent register is one of the
  4899. @@ -435,7 +435,7 @@
  4900. unlock_and_exit:
  4901. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  4902. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  4903. return_ACPI_STATUS(status);
  4904. }
  4905. diff -Nur linux-4.1.20.orig/drivers/acpi/acpica/utmutex.c linux-4.1.20/drivers/acpi/acpica/utmutex.c
  4906. --- linux-4.1.20.orig/drivers/acpi/acpica/utmutex.c 2016-03-17 19:11:03.000000000 +0100
  4907. +++ linux-4.1.20/drivers/acpi/acpica/utmutex.c 2016-03-21 20:18:31.000000000 +0100
  4908. @@ -88,7 +88,7 @@
  4909. return_ACPI_STATUS (status);
  4910. }
  4911. - status = acpi_os_create_lock (&acpi_gbl_hardware_lock);
  4912. + status = acpi_os_create_raw_lock (&acpi_gbl_hardware_lock);
  4913. if (ACPI_FAILURE (status)) {
  4914. return_ACPI_STATUS (status);
  4915. }
  4916. @@ -141,7 +141,7 @@
  4917. /* Delete the spinlocks */
  4918. acpi_os_delete_lock(acpi_gbl_gpe_lock);
  4919. - acpi_os_delete_lock(acpi_gbl_hardware_lock);
  4920. + acpi_os_delete_raw_lock(acpi_gbl_hardware_lock);
  4921. acpi_os_delete_lock(acpi_gbl_reference_count_lock);
  4922. /* Delete the reader/writer lock */
  4923. diff -Nur linux-4.1.20.orig/drivers/ata/libata-sff.c linux-4.1.20/drivers/ata/libata-sff.c
  4924. --- linux-4.1.20.orig/drivers/ata/libata-sff.c 2016-03-17 19:11:03.000000000 +0100
  4925. +++ linux-4.1.20/drivers/ata/libata-sff.c 2016-03-21 20:18:31.000000000 +0100
  4926. @@ -678,9 +678,9 @@
  4927. unsigned long flags;
  4928. unsigned int consumed;
  4929. - local_irq_save(flags);
  4930. + local_irq_save_nort(flags);
  4931. consumed = ata_sff_data_xfer32(dev, buf, buflen, rw);
  4932. - local_irq_restore(flags);
  4933. + local_irq_restore_nort(flags);
  4934. return consumed;
  4935. }
  4936. @@ -719,7 +719,7 @@
  4937. unsigned long flags;
  4938. /* FIXME: use a bounce buffer */
  4939. - local_irq_save(flags);
  4940. + local_irq_save_nort(flags);
  4941. buf = kmap_atomic(page);
  4942. /* do the actual data transfer */
  4943. @@ -727,7 +727,7 @@
  4944. do_write);
  4945. kunmap_atomic(buf);
  4946. - local_irq_restore(flags);
  4947. + local_irq_restore_nort(flags);
  4948. } else {
  4949. buf = page_address(page);
  4950. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  4951. @@ -864,7 +864,7 @@
  4952. unsigned long flags;
  4953. /* FIXME: use bounce buffer */
  4954. - local_irq_save(flags);
  4955. + local_irq_save_nort(flags);
  4956. buf = kmap_atomic(page);
  4957. /* do the actual data transfer */
  4958. @@ -872,7 +872,7 @@
  4959. count, rw);
  4960. kunmap_atomic(buf);
  4961. - local_irq_restore(flags);
  4962. + local_irq_restore_nort(flags);
  4963. } else {
  4964. buf = page_address(page);
  4965. consumed = ap->ops->sff_data_xfer(dev, buf + offset,
  4966. diff -Nur linux-4.1.20.orig/drivers/char/random.c linux-4.1.20/drivers/char/random.c
  4967. --- linux-4.1.20.orig/drivers/char/random.c 2016-03-17 19:11:03.000000000 +0100
  4968. +++ linux-4.1.20/drivers/char/random.c 2016-03-21 20:18:31.000000000 +0100
  4969. @@ -776,8 +776,6 @@
  4970. } sample;
  4971. long delta, delta2, delta3;
  4972. - preempt_disable();
  4973. -
  4974. sample.jiffies = jiffies;
  4975. sample.cycles = random_get_entropy();
  4976. sample.num = num;
  4977. @@ -818,7 +816,6 @@
  4978. */
  4979. credit_entropy_bits(r, min_t(int, fls(delta>>1), 11));
  4980. }
  4981. - preempt_enable();
  4982. }
  4983. void add_input_randomness(unsigned int type, unsigned int code,
  4984. @@ -871,28 +868,27 @@
  4985. return *(ptr + f->reg_idx++);
  4986. }
  4987. -void add_interrupt_randomness(int irq, int irq_flags)
  4988. +void add_interrupt_randomness(int irq, int irq_flags, __u64 ip)
  4989. {
  4990. struct entropy_store *r;
  4991. struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness);
  4992. - struct pt_regs *regs = get_irq_regs();
  4993. unsigned long now = jiffies;
  4994. cycles_t cycles = random_get_entropy();
  4995. __u32 c_high, j_high;
  4996. - __u64 ip;
  4997. unsigned long seed;
  4998. int credit = 0;
  4999. if (cycles == 0)
  5000. - cycles = get_reg(fast_pool, regs);
  5001. + cycles = get_reg(fast_pool, NULL);
  5002. c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0;
  5003. j_high = (sizeof(now) > 4) ? now >> 32 : 0;
  5004. fast_pool->pool[0] ^= cycles ^ j_high ^ irq;
  5005. fast_pool->pool[1] ^= now ^ c_high;
  5006. - ip = regs ? instruction_pointer(regs) : _RET_IP_;
  5007. + if (!ip)
  5008. + ip = _RET_IP_;
  5009. fast_pool->pool[2] ^= ip;
  5010. fast_pool->pool[3] ^= (sizeof(ip) > 4) ? ip >> 32 :
  5011. - get_reg(fast_pool, regs);
  5012. + get_reg(fast_pool, NULL);
  5013. fast_mix(fast_pool);
  5014. add_interrupt_bench(cycles);
  5015. diff -Nur linux-4.1.20.orig/drivers/clocksource/tcb_clksrc.c linux-4.1.20/drivers/clocksource/tcb_clksrc.c
  5016. --- linux-4.1.20.orig/drivers/clocksource/tcb_clksrc.c 2016-03-17 19:11:03.000000000 +0100
  5017. +++ linux-4.1.20/drivers/clocksource/tcb_clksrc.c 2016-03-21 20:18:31.000000000 +0100
  5018. @@ -23,8 +23,7 @@
  5019. * this 32 bit free-running counter. the second channel is not used.
  5020. *
  5021. * - The third channel may be used to provide a 16-bit clockevent
  5022. - * source, used in either periodic or oneshot mode. This runs
  5023. - * at 32 KiHZ, and can handle delays of up to two seconds.
  5024. + * source, used in either periodic or oneshot mode.
  5025. *
  5026. * A boot clocksource and clockevent source are also currently needed,
  5027. * unless the relevant platforms (ARM/AT91, AVR32/AT32) are changed so
  5028. @@ -74,6 +73,7 @@
  5029. struct tc_clkevt_device {
  5030. struct clock_event_device clkevt;
  5031. struct clk *clk;
  5032. + u32 freq;
  5033. void __iomem *regs;
  5034. };
  5035. @@ -82,13 +82,6 @@
  5036. return container_of(clkevt, struct tc_clkevt_device, clkevt);
  5037. }
  5038. -/* For now, we always use the 32K clock ... this optimizes for NO_HZ,
  5039. - * because using one of the divided clocks would usually mean the
  5040. - * tick rate can never be less than several dozen Hz (vs 0.5 Hz).
  5041. - *
  5042. - * A divided clock could be good for high resolution timers, since
  5043. - * 30.5 usec resolution can seem "low".
  5044. - */
  5045. static u32 timer_clock;
  5046. static void tc_mode(enum clock_event_mode m, struct clock_event_device *d)
  5047. @@ -111,11 +104,12 @@
  5048. case CLOCK_EVT_MODE_PERIODIC:
  5049. clk_enable(tcd->clk);
  5050. - /* slow clock, count up to RC, then irq and restart */
  5051. + /* count up to RC, then irq and restart */
  5052. __raw_writel(timer_clock
  5053. | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  5054. regs + ATMEL_TC_REG(2, CMR));
  5055. - __raw_writel((32768 + HZ/2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  5056. + __raw_writel((tcd->freq + HZ / 2) / HZ,
  5057. + tcaddr + ATMEL_TC_REG(2, RC));
  5058. /* Enable clock and interrupts on RC compare */
  5059. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  5060. @@ -128,7 +122,7 @@
  5061. case CLOCK_EVT_MODE_ONESHOT:
  5062. clk_enable(tcd->clk);
  5063. - /* slow clock, count up to RC, then irq and stop */
  5064. + /* count up to RC, then irq and stop */
  5065. __raw_writel(timer_clock | ATMEL_TC_CPCSTOP
  5066. | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  5067. regs + ATMEL_TC_REG(2, CMR));
  5068. @@ -157,8 +151,12 @@
  5069. .name = "tc_clkevt",
  5070. .features = CLOCK_EVT_FEAT_PERIODIC
  5071. | CLOCK_EVT_FEAT_ONESHOT,
  5072. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  5073. /* Should be lower than at91rm9200's system timer */
  5074. .rating = 125,
  5075. +#else
  5076. + .rating = 200,
  5077. +#endif
  5078. .set_next_event = tc_next_event,
  5079. .set_mode = tc_mode,
  5080. },
  5081. @@ -178,8 +176,9 @@
  5082. return IRQ_NONE;
  5083. }
  5084. -static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  5085. +static int __init setup_clkevents(struct atmel_tc *tc, int divisor_idx)
  5086. {
  5087. + unsigned divisor = atmel_tc_divisors[divisor_idx];
  5088. int ret;
  5089. struct clk *t2_clk = tc->clk[2];
  5090. int irq = tc->irq[2];
  5091. @@ -193,7 +192,11 @@
  5092. clkevt.regs = tc->regs;
  5093. clkevt.clk = t2_clk;
  5094. - timer_clock = clk32k_divisor_idx;
  5095. + timer_clock = divisor_idx;
  5096. + if (!divisor)
  5097. + clkevt.freq = 32768;
  5098. + else
  5099. + clkevt.freq = clk_get_rate(t2_clk) / divisor;
  5100. clkevt.clkevt.cpumask = cpumask_of(0);
  5101. @@ -203,7 +206,7 @@
  5102. return ret;
  5103. }
  5104. - clockevents_config_and_register(&clkevt.clkevt, 32768, 1, 0xffff);
  5105. + clockevents_config_and_register(&clkevt.clkevt, clkevt.freq, 1, 0xffff);
  5106. return ret;
  5107. }
  5108. @@ -340,7 +343,11 @@
  5109. goto err_disable_t1;
  5110. /* channel 2: periodic and oneshot timer support */
  5111. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  5112. ret = setup_clkevents(tc, clk32k_divisor_idx);
  5113. +#else
  5114. + ret = setup_clkevents(tc, best_divisor_idx);
  5115. +#endif
  5116. if (ret)
  5117. goto err_unregister_clksrc;
  5118. diff -Nur linux-4.1.20.orig/drivers/clocksource/timer-atmel-pit.c linux-4.1.20/drivers/clocksource/timer-atmel-pit.c
  5119. --- linux-4.1.20.orig/drivers/clocksource/timer-atmel-pit.c 2016-03-17 19:11:03.000000000 +0100
  5120. +++ linux-4.1.20/drivers/clocksource/timer-atmel-pit.c 2016-03-21 20:18:31.000000000 +0100
  5121. @@ -90,6 +90,7 @@
  5122. return elapsed;
  5123. }
  5124. +static struct irqaction at91sam926x_pit_irq;
  5125. /*
  5126. * Clockevent device: interrupts every 1/HZ (== pit_cycles * MCK/16)
  5127. */
  5128. @@ -100,6 +101,8 @@
  5129. switch (mode) {
  5130. case CLOCK_EVT_MODE_PERIODIC:
  5131. + /* Set up irq handler */
  5132. + setup_irq(at91sam926x_pit_irq.irq, &at91sam926x_pit_irq);
  5133. /* update clocksource counter */
  5134. data->cnt += data->cycle * PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
  5135. pit_write(data->base, AT91_PIT_MR,
  5136. @@ -113,6 +116,7 @@
  5137. /* disable irq, leaving the clocksource active */
  5138. pit_write(data->base, AT91_PIT_MR,
  5139. (data->cycle - 1) | AT91_PIT_PITEN);
  5140. + remove_irq(at91sam926x_pit_irq.irq, &at91sam926x_pit_irq);
  5141. break;
  5142. case CLOCK_EVT_MODE_RESUME:
  5143. break;
  5144. diff -Nur linux-4.1.20.orig/drivers/clocksource/timer-atmel-st.c linux-4.1.20/drivers/clocksource/timer-atmel-st.c
  5145. --- linux-4.1.20.orig/drivers/clocksource/timer-atmel-st.c 2016-03-17 19:11:03.000000000 +0100
  5146. +++ linux-4.1.20/drivers/clocksource/timer-atmel-st.c 2016-03-21 20:18:31.000000000 +0100
  5147. @@ -131,6 +131,7 @@
  5148. break;
  5149. case CLOCK_EVT_MODE_SHUTDOWN:
  5150. case CLOCK_EVT_MODE_UNUSED:
  5151. + remove_irq(NR_IRQS_LEGACY + AT91_ID_SYS, &at91rm9200_timer_irq);
  5152. case CLOCK_EVT_MODE_RESUME:
  5153. irqmask = 0;
  5154. break;
  5155. diff -Nur linux-4.1.20.orig/drivers/cpufreq/Kconfig.x86 linux-4.1.20/drivers/cpufreq/Kconfig.x86
  5156. --- linux-4.1.20.orig/drivers/cpufreq/Kconfig.x86 2016-03-17 19:11:03.000000000 +0100
  5157. +++ linux-4.1.20/drivers/cpufreq/Kconfig.x86 2016-03-21 20:18:31.000000000 +0100
  5158. @@ -123,7 +123,7 @@
  5159. config X86_POWERNOW_K8
  5160. tristate "AMD Opteron/Athlon64 PowerNow!"
  5161. - depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ
  5162. + depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ && !PREEMPT_RT_BASE
  5163. help
  5164. This adds the CPUFreq driver for K8/early Opteron/Athlon64 processors.
  5165. Support for K10 and newer processors is now in acpi-cpufreq.
  5166. diff -Nur linux-4.1.20.orig/drivers/cpufreq/cpufreq.c linux-4.1.20/drivers/cpufreq/cpufreq.c
  5167. --- linux-4.1.20.orig/drivers/cpufreq/cpufreq.c 2016-03-17 19:11:03.000000000 +0100
  5168. +++ linux-4.1.20/drivers/cpufreq/cpufreq.c 2016-03-21 20:18:31.000000000 +0100
  5169. @@ -64,12 +64,6 @@
  5170. return cpufreq_driver->target_index || cpufreq_driver->target;
  5171. }
  5172. -/*
  5173. - * rwsem to guarantee that cpufreq driver module doesn't unload during critical
  5174. - * sections
  5175. - */
  5176. -static DECLARE_RWSEM(cpufreq_rwsem);
  5177. -
  5178. /* internal prototypes */
  5179. static int __cpufreq_governor(struct cpufreq_policy *policy,
  5180. unsigned int event);
  5181. @@ -215,9 +209,6 @@
  5182. if (cpu >= nr_cpu_ids)
  5183. return NULL;
  5184. - if (!down_read_trylock(&cpufreq_rwsem))
  5185. - return NULL;
  5186. -
  5187. /* get the cpufreq driver */
  5188. read_lock_irqsave(&cpufreq_driver_lock, flags);
  5189. @@ -230,9 +221,6 @@
  5190. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  5191. - if (!policy)
  5192. - up_read(&cpufreq_rwsem);
  5193. -
  5194. return policy;
  5195. }
  5196. EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
  5197. @@ -240,7 +228,6 @@
  5198. void cpufreq_cpu_put(struct cpufreq_policy *policy)
  5199. {
  5200. kobject_put(&policy->kobj);
  5201. - up_read(&cpufreq_rwsem);
  5202. }
  5203. EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
  5204. @@ -765,9 +752,6 @@
  5205. struct freq_attr *fattr = to_attr(attr);
  5206. ssize_t ret;
  5207. - if (!down_read_trylock(&cpufreq_rwsem))
  5208. - return -EINVAL;
  5209. -
  5210. down_read(&policy->rwsem);
  5211. if (fattr->show)
  5212. @@ -776,7 +760,6 @@
  5213. ret = -EIO;
  5214. up_read(&policy->rwsem);
  5215. - up_read(&cpufreq_rwsem);
  5216. return ret;
  5217. }
  5218. @@ -793,9 +776,6 @@
  5219. if (!cpu_online(policy->cpu))
  5220. goto unlock;
  5221. - if (!down_read_trylock(&cpufreq_rwsem))
  5222. - goto unlock;
  5223. -
  5224. down_write(&policy->rwsem);
  5225. if (fattr->store)
  5226. @@ -804,8 +784,6 @@
  5227. ret = -EIO;
  5228. up_write(&policy->rwsem);
  5229. -
  5230. - up_read(&cpufreq_rwsem);
  5231. unlock:
  5232. put_online_cpus();
  5233. @@ -1117,16 +1095,12 @@
  5234. if (unlikely(policy))
  5235. return 0;
  5236. - if (!down_read_trylock(&cpufreq_rwsem))
  5237. - return 0;
  5238. -
  5239. /* Check if this cpu was hot-unplugged earlier and has siblings */
  5240. read_lock_irqsave(&cpufreq_driver_lock, flags);
  5241. for_each_policy(policy) {
  5242. if (cpumask_test_cpu(cpu, policy->related_cpus)) {
  5243. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  5244. ret = cpufreq_add_policy_cpu(policy, cpu, dev);
  5245. - up_read(&cpufreq_rwsem);
  5246. return ret;
  5247. }
  5248. }
  5249. @@ -1269,8 +1243,6 @@
  5250. kobject_uevent(&policy->kobj, KOBJ_ADD);
  5251. - up_read(&cpufreq_rwsem);
  5252. -
  5253. /* Callback for handling stuff after policy is ready */
  5254. if (cpufreq_driver->ready)
  5255. cpufreq_driver->ready(policy);
  5256. @@ -1304,8 +1276,6 @@
  5257. cpufreq_policy_free(policy);
  5258. nomem_out:
  5259. - up_read(&cpufreq_rwsem);
  5260. -
  5261. return ret;
  5262. }
  5263. @@ -2499,19 +2469,20 @@
  5264. pr_debug("unregistering driver %s\n", driver->name);
  5265. + /* Protect against concurrent cpu hotplug */
  5266. + get_online_cpus();
  5267. subsys_interface_unregister(&cpufreq_interface);
  5268. if (cpufreq_boost_supported())
  5269. cpufreq_sysfs_remove_file(&boost.attr);
  5270. unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
  5271. - down_write(&cpufreq_rwsem);
  5272. write_lock_irqsave(&cpufreq_driver_lock, flags);
  5273. cpufreq_driver = NULL;
  5274. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  5275. - up_write(&cpufreq_rwsem);
  5276. + put_online_cpus();
  5277. return 0;
  5278. }
  5279. diff -Nur linux-4.1.20.orig/drivers/gpio/Kconfig linux-4.1.20/drivers/gpio/Kconfig
  5280. --- linux-4.1.20.orig/drivers/gpio/Kconfig 2016-03-17 19:11:03.000000000 +0100
  5281. +++ linux-4.1.20/drivers/gpio/Kconfig 2016-03-21 20:18:31.000000000 +0100
  5282. @@ -308,7 +308,7 @@
  5283. family of SOCs.
  5284. config GPIO_OMAP
  5285. - bool "TI OMAP GPIO support" if COMPILE_TEST && !ARCH_OMAP2PLUS
  5286. + tristate "TI OMAP GPIO support" if ARCH_OMAP2PLUS || COMPILE_TEST
  5287. default y if ARCH_OMAP
  5288. depends on ARM
  5289. select GENERIC_IRQ_CHIP
  5290. diff -Nur linux-4.1.20.orig/drivers/gpio/gpio-omap.c linux-4.1.20/drivers/gpio/gpio-omap.c
  5291. --- linux-4.1.20.orig/drivers/gpio/gpio-omap.c 2016-03-17 19:11:03.000000000 +0100
  5292. +++ linux-4.1.20/drivers/gpio/gpio-omap.c 2016-03-21 20:18:31.000000000 +0100
  5293. @@ -29,6 +29,7 @@
  5294. #include <linux/platform_data/gpio-omap.h>
  5295. #define OFF_MODE 1
  5296. +#define OMAP4_GPIO_DEBOUNCINGTIME_MASK 0xFF
  5297. static LIST_HEAD(omap_gpio_list);
  5298. @@ -50,14 +51,15 @@
  5299. struct gpio_bank {
  5300. struct list_head node;
  5301. void __iomem *base;
  5302. - u16 irq;
  5303. + int irq;
  5304. u32 non_wakeup_gpios;
  5305. u32 enabled_non_wakeup_gpios;
  5306. struct gpio_regs context;
  5307. u32 saved_datain;
  5308. u32 level_mask;
  5309. u32 toggle_mask;
  5310. - spinlock_t lock;
  5311. + raw_spinlock_t lock;
  5312. + raw_spinlock_t wa_lock;
  5313. struct gpio_chip chip;
  5314. struct clk *dbck;
  5315. u32 mod_usage;
  5316. @@ -67,7 +69,7 @@
  5317. struct device *dev;
  5318. bool is_mpuio;
  5319. bool dbck_flag;
  5320. - bool loses_context;
  5321. +
  5322. bool context_valid;
  5323. int stride;
  5324. u32 width;
  5325. @@ -175,7 +177,7 @@
  5326. static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
  5327. {
  5328. if (bank->dbck_enable_mask && !bank->dbck_enabled) {
  5329. - clk_prepare_enable(bank->dbck);
  5330. + clk_enable(bank->dbck);
  5331. bank->dbck_enabled = true;
  5332. writel_relaxed(bank->dbck_enable_mask,
  5333. @@ -193,7 +195,7 @@
  5334. */
  5335. writel_relaxed(0, bank->base + bank->regs->debounce_en);
  5336. - clk_disable_unprepare(bank->dbck);
  5337. + clk_disable(bank->dbck);
  5338. bank->dbck_enabled = false;
  5339. }
  5340. }
  5341. @@ -204,8 +206,9 @@
  5342. * @offset: the gpio number on this @bank
  5343. * @debounce: debounce time to use
  5344. *
  5345. - * OMAP's debounce time is in 31us steps so we need
  5346. - * to convert and round up to the closest unit.
  5347. + * OMAP's debounce time is in 31us steps
  5348. + * <debounce time> = (GPIO_DEBOUNCINGTIME[7:0].DEBOUNCETIME + 1) x 31
  5349. + * so we need to convert and round up to the closest unit.
  5350. */
  5351. static void omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned offset,
  5352. unsigned debounce)
  5353. @@ -213,34 +216,33 @@
  5354. void __iomem *reg;
  5355. u32 val;
  5356. u32 l;
  5357. + bool enable = !!debounce;
  5358. if (!bank->dbck_flag)
  5359. return;
  5360. - if (debounce < 32)
  5361. - debounce = 0x01;
  5362. - else if (debounce > 7936)
  5363. - debounce = 0xff;
  5364. - else
  5365. - debounce = (debounce / 0x1f) - 1;
  5366. + if (enable) {
  5367. + debounce = DIV_ROUND_UP(debounce, 31) - 1;
  5368. + debounce &= OMAP4_GPIO_DEBOUNCINGTIME_MASK;
  5369. + }
  5370. l = BIT(offset);
  5371. - clk_prepare_enable(bank->dbck);
  5372. + clk_enable(bank->dbck);
  5373. reg = bank->base + bank->regs->debounce;
  5374. writel_relaxed(debounce, reg);
  5375. reg = bank->base + bank->regs->debounce_en;
  5376. val = readl_relaxed(reg);
  5377. - if (debounce)
  5378. + if (enable)
  5379. val |= l;
  5380. else
  5381. val &= ~l;
  5382. bank->dbck_enable_mask = val;
  5383. writel_relaxed(val, reg);
  5384. - clk_disable_unprepare(bank->dbck);
  5385. + clk_disable(bank->dbck);
  5386. /*
  5387. * Enable debounce clock per module.
  5388. * This call is mandatory because in omap_gpio_request() when
  5389. @@ -285,7 +287,7 @@
  5390. bank->context.debounce = 0;
  5391. writel_relaxed(bank->context.debounce, bank->base +
  5392. bank->regs->debounce);
  5393. - clk_disable_unprepare(bank->dbck);
  5394. + clk_disable(bank->dbck);
  5395. bank->dbck_enabled = false;
  5396. }
  5397. }
  5398. @@ -488,9 +490,6 @@
  5399. unsigned long flags;
  5400. unsigned offset = d->hwirq;
  5401. - if (!BANK_USED(bank))
  5402. - pm_runtime_get_sync(bank->dev);
  5403. -
  5404. if (type & ~IRQ_TYPE_SENSE_MASK)
  5405. return -EINVAL;
  5406. @@ -498,20 +497,28 @@
  5407. (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
  5408. return -EINVAL;
  5409. - spin_lock_irqsave(&bank->lock, flags);
  5410. + raw_spin_lock_irqsave(&bank->lock, flags);
  5411. retval = omap_set_gpio_triggering(bank, offset, type);
  5412. + if (retval) {
  5413. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5414. + goto error;
  5415. + }
  5416. omap_gpio_init_irq(bank, offset);
  5417. if (!omap_gpio_is_input(bank, offset)) {
  5418. - spin_unlock_irqrestore(&bank->lock, flags);
  5419. - return -EINVAL;
  5420. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5421. + retval = -EINVAL;
  5422. + goto error;
  5423. }
  5424. - spin_unlock_irqrestore(&bank->lock, flags);
  5425. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5426. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  5427. __irq_set_handler_locked(d->irq, handle_level_irq);
  5428. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  5429. __irq_set_handler_locked(d->irq, handle_edge_irq);
  5430. + return 0;
  5431. +
  5432. +error:
  5433. return retval;
  5434. }
  5435. @@ -626,34 +633,30 @@
  5436. return -EINVAL;
  5437. }
  5438. - spin_lock_irqsave(&bank->lock, flags);
  5439. + raw_spin_lock_irqsave(&bank->lock, flags);
  5440. if (enable)
  5441. bank->context.wake_en |= gpio_bit;
  5442. else
  5443. bank->context.wake_en &= ~gpio_bit;
  5444. writel_relaxed(bank->context.wake_en, bank->base + bank->regs->wkup_en);
  5445. - spin_unlock_irqrestore(&bank->lock, flags);
  5446. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5447. return 0;
  5448. }
  5449. -static void omap_reset_gpio(struct gpio_bank *bank, unsigned offset)
  5450. -{
  5451. - omap_set_gpio_direction(bank, offset, 1);
  5452. - omap_set_gpio_irqenable(bank, offset, 0);
  5453. - omap_clear_gpio_irqstatus(bank, offset);
  5454. - omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
  5455. - omap_clear_gpio_debounce(bank, offset);
  5456. -}
  5457. -
  5458. /* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
  5459. static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
  5460. {
  5461. struct gpio_bank *bank = omap_irq_data_get_bank(d);
  5462. unsigned offset = d->hwirq;
  5463. + int ret;
  5464. +
  5465. + ret = omap_set_gpio_wakeup(bank, offset, enable);
  5466. + if (!ret)
  5467. + ret = irq_set_irq_wake(bank->irq, enable);
  5468. - return omap_set_gpio_wakeup(bank, offset, enable);
  5469. + return ret;
  5470. }
  5471. static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
  5472. @@ -668,17 +671,10 @@
  5473. if (!BANK_USED(bank))
  5474. pm_runtime_get_sync(bank->dev);
  5475. - spin_lock_irqsave(&bank->lock, flags);
  5476. - /* Set trigger to none. You need to enable the desired trigger with
  5477. - * request_irq() or set_irq_type(). Only do this if the IRQ line has
  5478. - * not already been requested.
  5479. - */
  5480. - if (!LINE_USED(bank->irq_usage, offset)) {
  5481. - omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
  5482. - omap_enable_gpio_module(bank, offset);
  5483. - }
  5484. + raw_spin_lock_irqsave(&bank->lock, flags);
  5485. + omap_enable_gpio_module(bank, offset);
  5486. bank->mod_usage |= BIT(offset);
  5487. - spin_unlock_irqrestore(&bank->lock, flags);
  5488. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5489. return 0;
  5490. }
  5491. @@ -688,11 +684,14 @@
  5492. struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
  5493. unsigned long flags;
  5494. - spin_lock_irqsave(&bank->lock, flags);
  5495. + raw_spin_lock_irqsave(&bank->lock, flags);
  5496. bank->mod_usage &= ~(BIT(offset));
  5497. + if (!LINE_USED(bank->irq_usage, offset)) {
  5498. + omap_set_gpio_direction(bank, offset, 1);
  5499. + omap_clear_gpio_debounce(bank, offset);
  5500. + }
  5501. omap_disable_gpio_module(bank, offset);
  5502. - omap_reset_gpio(bank, offset);
  5503. - spin_unlock_irqrestore(&bank->lock, flags);
  5504. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5505. /*
  5506. * If this is the last gpio to be freed in the bank,
  5507. @@ -711,29 +710,27 @@
  5508. * line's interrupt handler has been run, we may miss some nested
  5509. * interrupts.
  5510. */
  5511. -static void omap_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  5512. +static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank)
  5513. {
  5514. void __iomem *isr_reg = NULL;
  5515. u32 isr;
  5516. unsigned int bit;
  5517. - struct gpio_bank *bank;
  5518. - int unmasked = 0;
  5519. - struct irq_chip *irqchip = irq_desc_get_chip(desc);
  5520. - struct gpio_chip *chip = irq_get_handler_data(irq);
  5521. + struct gpio_bank *bank = gpiobank;
  5522. + unsigned long wa_lock_flags;
  5523. + unsigned long lock_flags;
  5524. - chained_irq_enter(irqchip, desc);
  5525. -
  5526. - bank = container_of(chip, struct gpio_bank, chip);
  5527. isr_reg = bank->base + bank->regs->irqstatus;
  5528. - pm_runtime_get_sync(bank->dev);
  5529. -
  5530. if (WARN_ON(!isr_reg))
  5531. goto exit;
  5532. + pm_runtime_get_sync(bank->dev);
  5533. +
  5534. while (1) {
  5535. u32 isr_saved, level_mask = 0;
  5536. u32 enabled;
  5537. + raw_spin_lock_irqsave(&bank->lock, lock_flags);
  5538. +
  5539. enabled = omap_get_gpio_irqbank_mask(bank);
  5540. isr_saved = isr = readl_relaxed(isr_reg) & enabled;
  5541. @@ -747,12 +744,7 @@
  5542. omap_clear_gpio_irqbank(bank, isr_saved & ~level_mask);
  5543. omap_enable_gpio_irqbank(bank, isr_saved & ~level_mask);
  5544. - /* if there is only edge sensitive GPIO pin interrupts
  5545. - configured, we could unmask GPIO bank interrupt immediately */
  5546. - if (!level_mask && !unmasked) {
  5547. - unmasked = 1;
  5548. - chained_irq_exit(irqchip, desc);
  5549. - }
  5550. + raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
  5551. if (!isr)
  5552. break;
  5553. @@ -761,6 +753,7 @@
  5554. bit = __ffs(isr);
  5555. isr &= ~(BIT(bit));
  5556. + raw_spin_lock_irqsave(&bank->lock, lock_flags);
  5557. /*
  5558. * Some chips can't respond to both rising and falling
  5559. * at the same time. If this irq was requested with
  5560. @@ -771,18 +764,20 @@
  5561. if (bank->toggle_mask & (BIT(bit)))
  5562. omap_toggle_gpio_edge_triggering(bank, bit);
  5563. + raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
  5564. +
  5565. + raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags);
  5566. +
  5567. generic_handle_irq(irq_find_mapping(bank->chip.irqdomain,
  5568. bit));
  5569. +
  5570. + raw_spin_unlock_irqrestore(&bank->wa_lock,
  5571. + wa_lock_flags);
  5572. }
  5573. }
  5574. - /* if bank has any level sensitive GPIO pin interrupt
  5575. - configured, we must unmask the bank interrupt only after
  5576. - handler(s) are executed in order to avoid spurious bank
  5577. - interrupt */
  5578. exit:
  5579. - if (!unmasked)
  5580. - chained_irq_exit(irqchip, desc);
  5581. pm_runtime_put(bank->dev);
  5582. + return IRQ_HANDLED;
  5583. }
  5584. static unsigned int omap_gpio_irq_startup(struct irq_data *d)
  5585. @@ -791,15 +786,22 @@
  5586. unsigned long flags;
  5587. unsigned offset = d->hwirq;
  5588. - if (!BANK_USED(bank))
  5589. - pm_runtime_get_sync(bank->dev);
  5590. + raw_spin_lock_irqsave(&bank->lock, flags);
  5591. - spin_lock_irqsave(&bank->lock, flags);
  5592. - omap_gpio_init_irq(bank, offset);
  5593. - spin_unlock_irqrestore(&bank->lock, flags);
  5594. + if (!LINE_USED(bank->mod_usage, offset))
  5595. + omap_set_gpio_direction(bank, offset, 1);
  5596. + else if (!omap_gpio_is_input(bank, offset))
  5597. + goto err;
  5598. + omap_enable_gpio_module(bank, offset);
  5599. + bank->irq_usage |= BIT(offset);
  5600. +
  5601. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5602. omap_gpio_unmask_irq(d);
  5603. return 0;
  5604. +err:
  5605. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5606. + return -EINVAL;
  5607. }
  5608. static void omap_gpio_irq_shutdown(struct irq_data *d)
  5609. @@ -808,11 +810,28 @@
  5610. unsigned long flags;
  5611. unsigned offset = d->hwirq;
  5612. - spin_lock_irqsave(&bank->lock, flags);
  5613. + raw_spin_lock_irqsave(&bank->lock, flags);
  5614. bank->irq_usage &= ~(BIT(offset));
  5615. + omap_set_gpio_irqenable(bank, offset, 0);
  5616. + omap_clear_gpio_irqstatus(bank, offset);
  5617. + omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
  5618. + if (!LINE_USED(bank->mod_usage, offset))
  5619. + omap_clear_gpio_debounce(bank, offset);
  5620. omap_disable_gpio_module(bank, offset);
  5621. - omap_reset_gpio(bank, offset);
  5622. - spin_unlock_irqrestore(&bank->lock, flags);
  5623. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5624. +}
  5625. +
  5626. +static void omap_gpio_irq_bus_lock(struct irq_data *data)
  5627. +{
  5628. + struct gpio_bank *bank = omap_irq_data_get_bank(data);
  5629. +
  5630. + if (!BANK_USED(bank))
  5631. + pm_runtime_get_sync(bank->dev);
  5632. +}
  5633. +
  5634. +static void gpio_irq_bus_sync_unlock(struct irq_data *data)
  5635. +{
  5636. + struct gpio_bank *bank = omap_irq_data_get_bank(data);
  5637. /*
  5638. * If this is the last IRQ to be freed in the bank,
  5639. @@ -836,10 +855,10 @@
  5640. unsigned offset = d->hwirq;
  5641. unsigned long flags;
  5642. - spin_lock_irqsave(&bank->lock, flags);
  5643. + raw_spin_lock_irqsave(&bank->lock, flags);
  5644. omap_set_gpio_irqenable(bank, offset, 0);
  5645. omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
  5646. - spin_unlock_irqrestore(&bank->lock, flags);
  5647. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5648. }
  5649. static void omap_gpio_unmask_irq(struct irq_data *d)
  5650. @@ -849,7 +868,7 @@
  5651. u32 trigger = irqd_get_trigger_type(d);
  5652. unsigned long flags;
  5653. - spin_lock_irqsave(&bank->lock, flags);
  5654. + raw_spin_lock_irqsave(&bank->lock, flags);
  5655. if (trigger)
  5656. omap_set_gpio_triggering(bank, offset, trigger);
  5657. @@ -861,7 +880,7 @@
  5658. }
  5659. omap_set_gpio_irqenable(bank, offset, 1);
  5660. - spin_unlock_irqrestore(&bank->lock, flags);
  5661. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5662. }
  5663. /*---------------------------------------------------------------------*/
  5664. @@ -874,9 +893,9 @@
  5665. OMAP_MPUIO_GPIO_MASKIT / bank->stride;
  5666. unsigned long flags;
  5667. - spin_lock_irqsave(&bank->lock, flags);
  5668. + raw_spin_lock_irqsave(&bank->lock, flags);
  5669. writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
  5670. - spin_unlock_irqrestore(&bank->lock, flags);
  5671. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5672. return 0;
  5673. }
  5674. @@ -889,9 +908,9 @@
  5675. OMAP_MPUIO_GPIO_MASKIT / bank->stride;
  5676. unsigned long flags;
  5677. - spin_lock_irqsave(&bank->lock, flags);
  5678. + raw_spin_lock_irqsave(&bank->lock, flags);
  5679. writel_relaxed(bank->context.wake_en, mask_reg);
  5680. - spin_unlock_irqrestore(&bank->lock, flags);
  5681. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5682. return 0;
  5683. }
  5684. @@ -937,9 +956,9 @@
  5685. bank = container_of(chip, struct gpio_bank, chip);
  5686. reg = bank->base + bank->regs->direction;
  5687. - spin_lock_irqsave(&bank->lock, flags);
  5688. + raw_spin_lock_irqsave(&bank->lock, flags);
  5689. dir = !!(readl_relaxed(reg) & BIT(offset));
  5690. - spin_unlock_irqrestore(&bank->lock, flags);
  5691. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5692. return dir;
  5693. }
  5694. @@ -949,9 +968,9 @@
  5695. unsigned long flags;
  5696. bank = container_of(chip, struct gpio_bank, chip);
  5697. - spin_lock_irqsave(&bank->lock, flags);
  5698. + raw_spin_lock_irqsave(&bank->lock, flags);
  5699. omap_set_gpio_direction(bank, offset, 1);
  5700. - spin_unlock_irqrestore(&bank->lock, flags);
  5701. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5702. return 0;
  5703. }
  5704. @@ -973,10 +992,10 @@
  5705. unsigned long flags;
  5706. bank = container_of(chip, struct gpio_bank, chip);
  5707. - spin_lock_irqsave(&bank->lock, flags);
  5708. + raw_spin_lock_irqsave(&bank->lock, flags);
  5709. bank->set_dataout(bank, offset, value);
  5710. omap_set_gpio_direction(bank, offset, 0);
  5711. - spin_unlock_irqrestore(&bank->lock, flags);
  5712. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5713. return 0;
  5714. }
  5715. @@ -988,9 +1007,9 @@
  5716. bank = container_of(chip, struct gpio_bank, chip);
  5717. - spin_lock_irqsave(&bank->lock, flags);
  5718. + raw_spin_lock_irqsave(&bank->lock, flags);
  5719. omap2_set_gpio_debounce(bank, offset, debounce);
  5720. - spin_unlock_irqrestore(&bank->lock, flags);
  5721. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5722. return 0;
  5723. }
  5724. @@ -1001,9 +1020,9 @@
  5725. unsigned long flags;
  5726. bank = container_of(chip, struct gpio_bank, chip);
  5727. - spin_lock_irqsave(&bank->lock, flags);
  5728. + raw_spin_lock_irqsave(&bank->lock, flags);
  5729. bank->set_dataout(bank, offset, value);
  5730. - spin_unlock_irqrestore(&bank->lock, flags);
  5731. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5732. }
  5733. /*---------------------------------------------------------------------*/
  5734. @@ -1048,10 +1067,6 @@
  5735. /* Initialize interface clk ungated, module enabled */
  5736. if (bank->regs->ctrl)
  5737. writel_relaxed(0, base + bank->regs->ctrl);
  5738. -
  5739. - bank->dbck = clk_get(bank->dev, "dbclk");
  5740. - if (IS_ERR(bank->dbck))
  5741. - dev_err(bank->dev, "Could not get gpio dbck\n");
  5742. }
  5743. static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
  5744. @@ -1080,7 +1095,6 @@
  5745. } else {
  5746. bank->chip.label = "gpio";
  5747. bank->chip.base = gpio;
  5748. - gpio += bank->width;
  5749. }
  5750. bank->chip.ngpio = bank->width;
  5751. @@ -1090,6 +1104,9 @@
  5752. return ret;
  5753. }
  5754. + if (!bank->is_mpuio)
  5755. + gpio += bank->width;
  5756. +
  5757. #ifdef CONFIG_ARCH_OMAP1
  5758. /*
  5759. * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
  5760. @@ -1112,7 +1129,7 @@
  5761. }
  5762. ret = gpiochip_irqchip_add(&bank->chip, irqc,
  5763. - irq_base, omap_gpio_irq_handler,
  5764. + irq_base, handle_bad_irq,
  5765. IRQ_TYPE_NONE);
  5766. if (ret) {
  5767. @@ -1121,10 +1138,14 @@
  5768. return -ENODEV;
  5769. }
  5770. - gpiochip_set_chained_irqchip(&bank->chip, irqc,
  5771. - bank->irq, omap_gpio_irq_handler);
  5772. + gpiochip_set_chained_irqchip(&bank->chip, irqc, bank->irq, NULL);
  5773. - return 0;
  5774. + ret = devm_request_irq(bank->dev, bank->irq, omap_gpio_irq_handler,
  5775. + 0, dev_name(bank->dev), bank);
  5776. + if (ret)
  5777. + gpiochip_remove(&bank->chip);
  5778. +
  5779. + return ret;
  5780. }
  5781. static const struct of_device_id omap_gpio_match[];
  5782. @@ -1163,17 +1184,23 @@
  5783. irqc->irq_unmask = omap_gpio_unmask_irq,
  5784. irqc->irq_set_type = omap_gpio_irq_type,
  5785. irqc->irq_set_wake = omap_gpio_wake_enable,
  5786. + irqc->irq_bus_lock = omap_gpio_irq_bus_lock,
  5787. + irqc->irq_bus_sync_unlock = gpio_irq_bus_sync_unlock,
  5788. irqc->name = dev_name(&pdev->dev);
  5789. - res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  5790. - if (unlikely(!res)) {
  5791. - dev_err(dev, "Invalid IRQ resource\n");
  5792. - return -ENODEV;
  5793. + bank->irq = platform_get_irq(pdev, 0);
  5794. + if (bank->irq <= 0) {
  5795. + if (!bank->irq)
  5796. + bank->irq = -ENXIO;
  5797. + if (bank->irq != -EPROBE_DEFER)
  5798. + dev_err(dev,
  5799. + "can't get irq resource ret=%d\n", bank->irq);
  5800. + return bank->irq;
  5801. }
  5802. - bank->irq = res->start;
  5803. bank->dev = dev;
  5804. bank->chip.dev = dev;
  5805. + bank->chip.owner = THIS_MODULE;
  5806. bank->dbck_flag = pdata->dbck_flag;
  5807. bank->stride = pdata->bank_stride;
  5808. bank->width = pdata->bank_width;
  5809. @@ -1183,15 +1210,9 @@
  5810. #ifdef CONFIG_OF_GPIO
  5811. bank->chip.of_node = of_node_get(node);
  5812. #endif
  5813. - if (node) {
  5814. - if (!of_property_read_bool(node, "ti,gpio-always-on"))
  5815. - bank->loses_context = true;
  5816. - } else {
  5817. - bank->loses_context = pdata->loses_context;
  5818. -
  5819. - if (bank->loses_context)
  5820. - bank->get_context_loss_count =
  5821. - pdata->get_context_loss_count;
  5822. + if (!node) {
  5823. + bank->get_context_loss_count =
  5824. + pdata->get_context_loss_count;
  5825. }
  5826. if (bank->regs->set_dataout && bank->regs->clr_dataout)
  5827. @@ -1199,16 +1220,27 @@
  5828. else
  5829. bank->set_dataout = omap_set_gpio_dataout_mask;
  5830. - spin_lock_init(&bank->lock);
  5831. + raw_spin_lock_init(&bank->lock);
  5832. + raw_spin_lock_init(&bank->wa_lock);
  5833. /* Static mapping, never released */
  5834. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  5835. bank->base = devm_ioremap_resource(dev, res);
  5836. if (IS_ERR(bank->base)) {
  5837. - irq_domain_remove(bank->chip.irqdomain);
  5838. return PTR_ERR(bank->base);
  5839. }
  5840. + if (bank->dbck_flag) {
  5841. + bank->dbck = devm_clk_get(bank->dev, "dbclk");
  5842. + if (IS_ERR(bank->dbck)) {
  5843. + dev_err(bank->dev,
  5844. + "Could not get gpio dbck. Disable debounce\n");
  5845. + bank->dbck_flag = false;
  5846. + } else {
  5847. + clk_prepare(bank->dbck);
  5848. + }
  5849. + }
  5850. +
  5851. platform_set_drvdata(pdev, bank);
  5852. pm_runtime_enable(bank->dev);
  5853. @@ -1221,8 +1253,11 @@
  5854. omap_gpio_mod_init(bank);
  5855. ret = omap_gpio_chip_init(bank, irqc);
  5856. - if (ret)
  5857. + if (ret) {
  5858. + pm_runtime_put_sync(bank->dev);
  5859. + pm_runtime_disable(bank->dev);
  5860. return ret;
  5861. + }
  5862. omap_gpio_show_rev(bank);
  5863. @@ -1233,6 +1268,19 @@
  5864. return 0;
  5865. }
  5866. +static int omap_gpio_remove(struct platform_device *pdev)
  5867. +{
  5868. + struct gpio_bank *bank = platform_get_drvdata(pdev);
  5869. +
  5870. + list_del(&bank->node);
  5871. + gpiochip_remove(&bank->chip);
  5872. + pm_runtime_disable(bank->dev);
  5873. + if (bank->dbck_flag)
  5874. + clk_unprepare(bank->dbck);
  5875. +
  5876. + return 0;
  5877. +}
  5878. +
  5879. #ifdef CONFIG_ARCH_OMAP2PLUS
  5880. #if defined(CONFIG_PM)
  5881. @@ -1246,7 +1294,7 @@
  5882. unsigned long flags;
  5883. u32 wake_low, wake_hi;
  5884. - spin_lock_irqsave(&bank->lock, flags);
  5885. + raw_spin_lock_irqsave(&bank->lock, flags);
  5886. /*
  5887. * Only edges can generate a wakeup event to the PRCM.
  5888. @@ -1299,7 +1347,7 @@
  5889. bank->get_context_loss_count(bank->dev);
  5890. omap_gpio_dbck_disable(bank);
  5891. - spin_unlock_irqrestore(&bank->lock, flags);
  5892. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5893. return 0;
  5894. }
  5895. @@ -1314,14 +1362,14 @@
  5896. unsigned long flags;
  5897. int c;
  5898. - spin_lock_irqsave(&bank->lock, flags);
  5899. + raw_spin_lock_irqsave(&bank->lock, flags);
  5900. /*
  5901. * On the first resume during the probe, the context has not
  5902. * been initialised and so initialise it now. Also initialise
  5903. * the context loss count.
  5904. */
  5905. - if (bank->loses_context && !bank->context_valid) {
  5906. + if (!bank->context_valid) {
  5907. omap_gpio_init_context(bank);
  5908. if (bank->get_context_loss_count)
  5909. @@ -1342,22 +1390,20 @@
  5910. writel_relaxed(bank->context.risingdetect,
  5911. bank->base + bank->regs->risingdetect);
  5912. - if (bank->loses_context) {
  5913. - if (!bank->get_context_loss_count) {
  5914. + if (!bank->get_context_loss_count) {
  5915. + omap_gpio_restore_context(bank);
  5916. + } else {
  5917. + c = bank->get_context_loss_count(bank->dev);
  5918. + if (c != bank->context_loss_count) {
  5919. omap_gpio_restore_context(bank);
  5920. } else {
  5921. - c = bank->get_context_loss_count(bank->dev);
  5922. - if (c != bank->context_loss_count) {
  5923. - omap_gpio_restore_context(bank);
  5924. - } else {
  5925. - spin_unlock_irqrestore(&bank->lock, flags);
  5926. - return 0;
  5927. - }
  5928. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5929. + return 0;
  5930. }
  5931. }
  5932. if (!bank->workaround_enabled) {
  5933. - spin_unlock_irqrestore(&bank->lock, flags);
  5934. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5935. return 0;
  5936. }
  5937. @@ -1412,18 +1458,19 @@
  5938. }
  5939. bank->workaround_enabled = false;
  5940. - spin_unlock_irqrestore(&bank->lock, flags);
  5941. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5942. return 0;
  5943. }
  5944. #endif /* CONFIG_PM */
  5945. +#if IS_BUILTIN(CONFIG_GPIO_OMAP)
  5946. void omap2_gpio_prepare_for_idle(int pwr_mode)
  5947. {
  5948. struct gpio_bank *bank;
  5949. list_for_each_entry(bank, &omap_gpio_list, node) {
  5950. - if (!BANK_USED(bank) || !bank->loses_context)
  5951. + if (!BANK_USED(bank))
  5952. continue;
  5953. bank->power_mode = pwr_mode;
  5954. @@ -1437,12 +1484,13 @@
  5955. struct gpio_bank *bank;
  5956. list_for_each_entry(bank, &omap_gpio_list, node) {
  5957. - if (!BANK_USED(bank) || !bank->loses_context)
  5958. + if (!BANK_USED(bank))
  5959. continue;
  5960. pm_runtime_get_sync(bank->dev);
  5961. }
  5962. }
  5963. +#endif
  5964. #if defined(CONFIG_PM)
  5965. static void omap_gpio_init_context(struct gpio_bank *p)
  5966. @@ -1598,6 +1646,7 @@
  5967. static struct platform_driver omap_gpio_driver = {
  5968. .probe = omap_gpio_probe,
  5969. + .remove = omap_gpio_remove,
  5970. .driver = {
  5971. .name = "omap_gpio",
  5972. .pm = &gpio_pm_ops,
  5973. @@ -1615,3 +1664,13 @@
  5974. return platform_driver_register(&omap_gpio_driver);
  5975. }
  5976. postcore_initcall(omap_gpio_drv_reg);
  5977. +
  5978. +static void __exit omap_gpio_exit(void)
  5979. +{
  5980. + platform_driver_unregister(&omap_gpio_driver);
  5981. +}
  5982. +module_exit(omap_gpio_exit);
  5983. +
  5984. +MODULE_DESCRIPTION("omap gpio driver");
  5985. +MODULE_ALIAS("platform:gpio-omap");
  5986. +MODULE_LICENSE("GPL v2");
  5987. diff -Nur linux-4.1.20.orig/drivers/gpu/drm/i915/i915_gem_execbuffer.c linux-4.1.20/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  5988. --- linux-4.1.20.orig/drivers/gpu/drm/i915/i915_gem_execbuffer.c 2016-03-17 19:11:03.000000000 +0100
  5989. +++ linux-4.1.20/drivers/gpu/drm/i915/i915_gem_execbuffer.c 2016-03-21 20:18:31.000000000 +0100
  5990. @@ -32,6 +32,7 @@
  5991. #include "i915_trace.h"
  5992. #include "intel_drv.h"
  5993. #include <linux/dma_remapping.h>
  5994. +#include <linux/uaccess.h>
  5995. #define __EXEC_OBJECT_HAS_PIN (1<<31)
  5996. #define __EXEC_OBJECT_HAS_FENCE (1<<30)
  5997. @@ -465,7 +466,7 @@
  5998. }
  5999. /* We can't wait for rendering with pagefaults disabled */
  6000. - if (obj->active && in_atomic())
  6001. + if (obj->active && pagefault_disabled())
  6002. return -EFAULT;
  6003. if (use_cpu_reloc(obj))
  6004. @@ -1338,7 +1339,9 @@
  6005. return ret;
  6006. }
  6007. +#ifndef CONFIG_PREEMPT_RT_BASE
  6008. trace_i915_gem_ring_dispatch(intel_ring_get_request(ring), dispatch_flags);
  6009. +#endif
  6010. i915_gem_execbuffer_move_to_active(vmas, ring);
  6011. i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
  6012. diff -Nur linux-4.1.20.orig/drivers/gpu/drm/i915/i915_gem_shrinker.c linux-4.1.20/drivers/gpu/drm/i915/i915_gem_shrinker.c
  6013. --- linux-4.1.20.orig/drivers/gpu/drm/i915/i915_gem_shrinker.c 2016-03-17 19:11:03.000000000 +0100
  6014. +++ linux-4.1.20/drivers/gpu/drm/i915/i915_gem_shrinker.c 2016-03-21 20:18:31.000000000 +0100
  6015. @@ -39,7 +39,7 @@
  6016. if (!mutex_is_locked(mutex))
  6017. return false;
  6018. -#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
  6019. +#if (defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)) && !defined(CONFIG_PREEMPT_RT_BASE)
  6020. return mutex->owner == task;
  6021. #else
  6022. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  6023. diff -Nur linux-4.1.20.orig/drivers/gpu/drm/i915/i915_irq.c linux-4.1.20/drivers/gpu/drm/i915/i915_irq.c
  6024. --- linux-4.1.20.orig/drivers/gpu/drm/i915/i915_irq.c 2016-03-17 19:11:03.000000000 +0100
  6025. +++ linux-4.1.20/drivers/gpu/drm/i915/i915_irq.c 2016-03-21 20:18:31.000000000 +0100
  6026. @@ -676,6 +676,7 @@
  6027. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6028. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  6029. + preempt_disable_rt();
  6030. /* Get optional system timestamp before query. */
  6031. if (stime)
  6032. @@ -727,6 +728,7 @@
  6033. *etime = ktime_get();
  6034. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  6035. + preempt_enable_rt();
  6036. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6037. diff -Nur linux-4.1.20.orig/drivers/gpu/drm/i915/intel_display.c linux-4.1.20/drivers/gpu/drm/i915/intel_display.c
  6038. --- linux-4.1.20.orig/drivers/gpu/drm/i915/intel_display.c 2016-03-17 19:11:03.000000000 +0100
  6039. +++ linux-4.1.20/drivers/gpu/drm/i915/intel_display.c 2016-03-21 20:18:31.000000000 +0100
  6040. @@ -10088,7 +10088,7 @@
  6041. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  6042. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6043. - WARN_ON(!in_interrupt());
  6044. + WARN_ON_NONRT(!in_interrupt());
  6045. if (crtc == NULL)
  6046. return;
  6047. diff -Nur linux-4.1.20.orig/drivers/gpu/drm/i915/intel_sprite.c linux-4.1.20/drivers/gpu/drm/i915/intel_sprite.c
  6048. --- linux-4.1.20.orig/drivers/gpu/drm/i915/intel_sprite.c 2016-03-17 19:11:03.000000000 +0100
  6049. +++ linux-4.1.20/drivers/gpu/drm/i915/intel_sprite.c 2016-03-21 20:18:31.000000000 +0100
  6050. @@ -37,6 +37,7 @@
  6051. #include "intel_drv.h"
  6052. #include <drm/i915_drm.h>
  6053. #include "i915_drv.h"
  6054. +#include <linux/locallock.h>
  6055. static bool
  6056. format_is_yuv(uint32_t format)
  6057. @@ -61,6 +62,8 @@
  6058. return DIV_ROUND_UP(usecs * mode->crtc_clock, 1000 * mode->crtc_htotal);
  6059. }
  6060. +static DEFINE_LOCAL_IRQ_LOCK(pipe_update_lock);
  6061. +
  6062. /**
  6063. * intel_pipe_update_start() - start update of a set of display registers
  6064. * @crtc: the crtc of which the registers are going to be updated
  6065. @@ -101,7 +104,7 @@
  6066. if (WARN_ON(drm_crtc_vblank_get(&crtc->base)))
  6067. return false;
  6068. - local_irq_disable();
  6069. + local_lock_irq(pipe_update_lock);
  6070. trace_i915_pipe_update_start(crtc, min, max);
  6071. @@ -123,11 +126,11 @@
  6072. break;
  6073. }
  6074. - local_irq_enable();
  6075. + local_unlock_irq(pipe_update_lock);
  6076. timeout = schedule_timeout(timeout);
  6077. - local_irq_disable();
  6078. + local_lock_irq(pipe_update_lock);
  6079. }
  6080. finish_wait(wq, &wait);
  6081. @@ -158,7 +161,7 @@
  6082. trace_i915_pipe_update_end(crtc, end_vbl_count);
  6083. - local_irq_enable();
  6084. + local_unlock_irq(pipe_update_lock);
  6085. if (start_vbl_count != end_vbl_count)
  6086. DRM_ERROR("Atomic update failure on pipe %c (start=%u end=%u)\n",
  6087. diff -Nur linux-4.1.20.orig/drivers/gpu/drm/radeon/radeon_display.c linux-4.1.20/drivers/gpu/drm/radeon/radeon_display.c
  6088. --- linux-4.1.20.orig/drivers/gpu/drm/radeon/radeon_display.c 2016-03-17 19:11:03.000000000 +0100
  6089. +++ linux-4.1.20/drivers/gpu/drm/radeon/radeon_display.c 2016-03-21 20:18:31.000000000 +0100
  6090. @@ -1798,6 +1798,7 @@
  6091. struct radeon_device *rdev = dev->dev_private;
  6092. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  6093. + preempt_disable_rt();
  6094. /* Get optional system timestamp before query. */
  6095. if (stime)
  6096. @@ -1890,6 +1891,7 @@
  6097. *etime = ktime_get();
  6098. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  6099. + preempt_enable_rt();
  6100. /* Decode into vertical and horizontal scanout position. */
  6101. *vpos = position & 0x1fff;
  6102. diff -Nur linux-4.1.20.orig/drivers/i2c/busses/i2c-omap.c linux-4.1.20/drivers/i2c/busses/i2c-omap.c
  6103. --- linux-4.1.20.orig/drivers/i2c/busses/i2c-omap.c 2016-03-17 19:11:03.000000000 +0100
  6104. +++ linux-4.1.20/drivers/i2c/busses/i2c-omap.c 2016-03-21 20:18:31.000000000 +0100
  6105. @@ -996,15 +996,12 @@
  6106. u16 mask;
  6107. u16 stat;
  6108. - spin_lock(&dev->lock);
  6109. - mask = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
  6110. stat = omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
  6111. + mask = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
  6112. if (stat & mask)
  6113. ret = IRQ_WAKE_THREAD;
  6114. - spin_unlock(&dev->lock);
  6115. -
  6116. return ret;
  6117. }
  6118. diff -Nur linux-4.1.20.orig/drivers/ide/alim15x3.c linux-4.1.20/drivers/ide/alim15x3.c
  6119. --- linux-4.1.20.orig/drivers/ide/alim15x3.c 2016-03-17 19:11:03.000000000 +0100
  6120. +++ linux-4.1.20/drivers/ide/alim15x3.c 2016-03-21 20:18:31.000000000 +0100
  6121. @@ -234,7 +234,7 @@
  6122. isa_dev = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
  6123. - local_irq_save(flags);
  6124. + local_irq_save_nort(flags);
  6125. if (m5229_revision < 0xC2) {
  6126. /*
  6127. @@ -325,7 +325,7 @@
  6128. }
  6129. pci_dev_put(north);
  6130. pci_dev_put(isa_dev);
  6131. - local_irq_restore(flags);
  6132. + local_irq_restore_nort(flags);
  6133. return 0;
  6134. }
  6135. diff -Nur linux-4.1.20.orig/drivers/ide/hpt366.c linux-4.1.20/drivers/ide/hpt366.c
  6136. --- linux-4.1.20.orig/drivers/ide/hpt366.c 2016-03-17 19:11:03.000000000 +0100
  6137. +++ linux-4.1.20/drivers/ide/hpt366.c 2016-03-21 20:18:31.000000000 +0100
  6138. @@ -1241,7 +1241,7 @@
  6139. dma_old = inb(base + 2);
  6140. - local_irq_save(flags);
  6141. + local_irq_save_nort(flags);
  6142. dma_new = dma_old;
  6143. pci_read_config_byte(dev, hwif->channel ? 0x4b : 0x43, &masterdma);
  6144. @@ -1252,7 +1252,7 @@
  6145. if (dma_new != dma_old)
  6146. outb(dma_new, base + 2);
  6147. - local_irq_restore(flags);
  6148. + local_irq_restore_nort(flags);
  6149. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx\n",
  6150. hwif->name, base, base + 7);
  6151. diff -Nur linux-4.1.20.orig/drivers/ide/ide-io-std.c linux-4.1.20/drivers/ide/ide-io-std.c
  6152. --- linux-4.1.20.orig/drivers/ide/ide-io-std.c 2016-03-17 19:11:03.000000000 +0100
  6153. +++ linux-4.1.20/drivers/ide/ide-io-std.c 2016-03-21 20:18:31.000000000 +0100
  6154. @@ -175,7 +175,7 @@
  6155. unsigned long uninitialized_var(flags);
  6156. if ((io_32bit & 2) && !mmio) {
  6157. - local_irq_save(flags);
  6158. + local_irq_save_nort(flags);
  6159. ata_vlb_sync(io_ports->nsect_addr);
  6160. }
  6161. @@ -186,7 +186,7 @@
  6162. insl(data_addr, buf, words);
  6163. if ((io_32bit & 2) && !mmio)
  6164. - local_irq_restore(flags);
  6165. + local_irq_restore_nort(flags);
  6166. if (((len + 1) & 3) < 2)
  6167. return;
  6168. @@ -219,7 +219,7 @@
  6169. unsigned long uninitialized_var(flags);
  6170. if ((io_32bit & 2) && !mmio) {
  6171. - local_irq_save(flags);
  6172. + local_irq_save_nort(flags);
  6173. ata_vlb_sync(io_ports->nsect_addr);
  6174. }
  6175. @@ -230,7 +230,7 @@
  6176. outsl(data_addr, buf, words);
  6177. if ((io_32bit & 2) && !mmio)
  6178. - local_irq_restore(flags);
  6179. + local_irq_restore_nort(flags);
  6180. if (((len + 1) & 3) < 2)
  6181. return;
  6182. diff -Nur linux-4.1.20.orig/drivers/ide/ide-io.c linux-4.1.20/drivers/ide/ide-io.c
  6183. --- linux-4.1.20.orig/drivers/ide/ide-io.c 2016-03-17 19:11:03.000000000 +0100
  6184. +++ linux-4.1.20/drivers/ide/ide-io.c 2016-03-21 20:18:31.000000000 +0100
  6185. @@ -659,7 +659,7 @@
  6186. /* disable_irq_nosync ?? */
  6187. disable_irq(hwif->irq);
  6188. /* local CPU only, as if we were handling an interrupt */
  6189. - local_irq_disable();
  6190. + local_irq_disable_nort();
  6191. if (hwif->polling) {
  6192. startstop = handler(drive);
  6193. } else if (drive_is_ready(drive)) {
  6194. diff -Nur linux-4.1.20.orig/drivers/ide/ide-iops.c linux-4.1.20/drivers/ide/ide-iops.c
  6195. --- linux-4.1.20.orig/drivers/ide/ide-iops.c 2016-03-17 19:11:03.000000000 +0100
  6196. +++ linux-4.1.20/drivers/ide/ide-iops.c 2016-03-21 20:18:31.000000000 +0100
  6197. @@ -129,12 +129,12 @@
  6198. if ((stat & ATA_BUSY) == 0)
  6199. break;
  6200. - local_irq_restore(flags);
  6201. + local_irq_restore_nort(flags);
  6202. *rstat = stat;
  6203. return -EBUSY;
  6204. }
  6205. }
  6206. - local_irq_restore(flags);
  6207. + local_irq_restore_nort(flags);
  6208. }
  6209. /*
  6210. * Allow status to settle, then read it again.
  6211. diff -Nur linux-4.1.20.orig/drivers/ide/ide-probe.c linux-4.1.20/drivers/ide/ide-probe.c
  6212. --- linux-4.1.20.orig/drivers/ide/ide-probe.c 2016-03-17 19:11:03.000000000 +0100
  6213. +++ linux-4.1.20/drivers/ide/ide-probe.c 2016-03-21 20:18:31.000000000 +0100
  6214. @@ -196,10 +196,10 @@
  6215. int bswap = 1;
  6216. /* local CPU only; some systems need this */
  6217. - local_irq_save(flags);
  6218. + local_irq_save_nort(flags);
  6219. /* read 512 bytes of id info */
  6220. hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
  6221. - local_irq_restore(flags);
  6222. + local_irq_restore_nort(flags);
  6223. drive->dev_flags |= IDE_DFLAG_ID_READ;
  6224. #ifdef DEBUG
  6225. diff -Nur linux-4.1.20.orig/drivers/ide/ide-taskfile.c linux-4.1.20/drivers/ide/ide-taskfile.c
  6226. --- linux-4.1.20.orig/drivers/ide/ide-taskfile.c 2016-03-17 19:11:03.000000000 +0100
  6227. +++ linux-4.1.20/drivers/ide/ide-taskfile.c 2016-03-21 20:18:31.000000000 +0100
  6228. @@ -250,7 +250,7 @@
  6229. page_is_high = PageHighMem(page);
  6230. if (page_is_high)
  6231. - local_irq_save(flags);
  6232. + local_irq_save_nort(flags);
  6233. buf = kmap_atomic(page) + offset;
  6234. @@ -271,7 +271,7 @@
  6235. kunmap_atomic(buf);
  6236. if (page_is_high)
  6237. - local_irq_restore(flags);
  6238. + local_irq_restore_nort(flags);
  6239. len -= nr_bytes;
  6240. }
  6241. @@ -414,7 +414,7 @@
  6242. }
  6243. if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
  6244. - local_irq_disable();
  6245. + local_irq_disable_nort();
  6246. ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
  6247. diff -Nur linux-4.1.20.orig/drivers/infiniband/ulp/ipoib/ipoib_multicast.c linux-4.1.20/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  6248. --- linux-4.1.20.orig/drivers/infiniband/ulp/ipoib/ipoib_multicast.c 2016-03-17 19:11:03.000000000 +0100
  6249. +++ linux-4.1.20/drivers/infiniband/ulp/ipoib/ipoib_multicast.c 2016-03-21 20:18:31.000000000 +0100
  6250. @@ -821,7 +821,7 @@
  6251. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  6252. - local_irq_save(flags);
  6253. + local_irq_save_nort(flags);
  6254. netif_addr_lock(dev);
  6255. spin_lock(&priv->lock);
  6256. @@ -903,7 +903,7 @@
  6257. spin_unlock(&priv->lock);
  6258. netif_addr_unlock(dev);
  6259. - local_irq_restore(flags);
  6260. + local_irq_restore_nort(flags);
  6261. /*
  6262. * make sure the in-flight joins have finished before we attempt
  6263. diff -Nur linux-4.1.20.orig/drivers/input/gameport/gameport.c linux-4.1.20/drivers/input/gameport/gameport.c
  6264. --- linux-4.1.20.orig/drivers/input/gameport/gameport.c 2016-03-17 19:11:03.000000000 +0100
  6265. +++ linux-4.1.20/drivers/input/gameport/gameport.c 2016-03-21 20:18:31.000000000 +0100
  6266. @@ -124,12 +124,12 @@
  6267. tx = 1 << 30;
  6268. for(i = 0; i < 50; i++) {
  6269. - local_irq_save(flags);
  6270. + local_irq_save_nort(flags);
  6271. GET_TIME(t1);
  6272. for (t = 0; t < 50; t++) gameport_read(gameport);
  6273. GET_TIME(t2);
  6274. GET_TIME(t3);
  6275. - local_irq_restore(flags);
  6276. + local_irq_restore_nort(flags);
  6277. udelay(i * 10);
  6278. if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
  6279. }
  6280. @@ -148,11 +148,11 @@
  6281. tx = 1 << 30;
  6282. for(i = 0; i < 50; i++) {
  6283. - local_irq_save(flags);
  6284. + local_irq_save_nort(flags);
  6285. rdtscl(t1);
  6286. for (t = 0; t < 50; t++) gameport_read(gameport);
  6287. rdtscl(t2);
  6288. - local_irq_restore(flags);
  6289. + local_irq_restore_nort(flags);
  6290. udelay(i * 10);
  6291. if (t2 - t1 < tx) tx = t2 - t1;
  6292. }
  6293. diff -Nur linux-4.1.20.orig/drivers/leds/trigger/Kconfig linux-4.1.20/drivers/leds/trigger/Kconfig
  6294. --- linux-4.1.20.orig/drivers/leds/trigger/Kconfig 2016-03-17 19:11:03.000000000 +0100
  6295. +++ linux-4.1.20/drivers/leds/trigger/Kconfig 2016-03-21 20:18:31.000000000 +0100
  6296. @@ -61,7 +61,7 @@
  6297. config LEDS_TRIGGER_CPU
  6298. bool "LED CPU Trigger"
  6299. - depends on LEDS_TRIGGERS
  6300. + depends on LEDS_TRIGGERS && !PREEMPT_RT_BASE
  6301. help
  6302. This allows LEDs to be controlled by active CPUs. This shows
  6303. the active CPUs across an array of LEDs so you can see which
  6304. diff -Nur linux-4.1.20.orig/drivers/md/bcache/Kconfig linux-4.1.20/drivers/md/bcache/Kconfig
  6305. --- linux-4.1.20.orig/drivers/md/bcache/Kconfig 2016-03-17 19:11:03.000000000 +0100
  6306. +++ linux-4.1.20/drivers/md/bcache/Kconfig 2016-03-21 20:18:31.000000000 +0100
  6307. @@ -1,6 +1,7 @@
  6308. config BCACHE
  6309. tristate "Block device as cache"
  6310. + depends on !PREEMPT_RT_FULL
  6311. ---help---
  6312. Allows a block device to be used as cache for other devices; uses
  6313. a btree for indexing and the layout is optimized for SSDs.
  6314. diff -Nur linux-4.1.20.orig/drivers/md/dm.c linux-4.1.20/drivers/md/dm.c
  6315. --- linux-4.1.20.orig/drivers/md/dm.c 2016-03-17 19:11:03.000000000 +0100
  6316. +++ linux-4.1.20/drivers/md/dm.c 2016-03-21 20:18:31.000000000 +0100
  6317. @@ -2134,7 +2134,7 @@
  6318. /* Establish tio->ti before queuing work (map_tio_request) */
  6319. tio->ti = ti;
  6320. queue_kthread_work(&md->kworker, &tio->work);
  6321. - BUG_ON(!irqs_disabled());
  6322. + BUG_ON_NONRT(!irqs_disabled());
  6323. }
  6324. goto out;
  6325. diff -Nur linux-4.1.20.orig/drivers/md/raid5.c linux-4.1.20/drivers/md/raid5.c
  6326. --- linux-4.1.20.orig/drivers/md/raid5.c 2016-03-17 19:11:03.000000000 +0100
  6327. +++ linux-4.1.20/drivers/md/raid5.c 2016-03-21 20:18:31.000000000 +0100
  6328. @@ -1918,8 +1918,9 @@
  6329. struct raid5_percpu *percpu;
  6330. unsigned long cpu;
  6331. - cpu = get_cpu();
  6332. + cpu = get_cpu_light();
  6333. percpu = per_cpu_ptr(conf->percpu, cpu);
  6334. + spin_lock(&percpu->lock);
  6335. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  6336. ops_run_biofill(sh);
  6337. overlap_clear++;
  6338. @@ -1975,7 +1976,8 @@
  6339. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  6340. wake_up(&sh->raid_conf->wait_for_overlap);
  6341. }
  6342. - put_cpu();
  6343. + spin_unlock(&percpu->lock);
  6344. + put_cpu_light();
  6345. }
  6346. static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp)
  6347. @@ -6363,6 +6365,7 @@
  6348. __func__, cpu);
  6349. break;
  6350. }
  6351. + spin_lock_init(&per_cpu_ptr(conf->percpu, cpu)->lock);
  6352. }
  6353. put_online_cpus();
  6354. diff -Nur linux-4.1.20.orig/drivers/md/raid5.h linux-4.1.20/drivers/md/raid5.h
  6355. --- linux-4.1.20.orig/drivers/md/raid5.h 2016-03-17 19:11:03.000000000 +0100
  6356. +++ linux-4.1.20/drivers/md/raid5.h 2016-03-21 20:18:31.000000000 +0100
  6357. @@ -495,6 +495,7 @@
  6358. int recovery_disabled;
  6359. /* per cpu variables */
  6360. struct raid5_percpu {
  6361. + spinlock_t lock; /* Protection for -RT */
  6362. struct page *spare_page; /* Used when checking P/Q in raid6 */
  6363. struct flex_array *scribble; /* space for constructing buffer
  6364. * lists and performing address
  6365. diff -Nur linux-4.1.20.orig/drivers/misc/Kconfig linux-4.1.20/drivers/misc/Kconfig
  6366. --- linux-4.1.20.orig/drivers/misc/Kconfig 2016-03-17 19:11:03.000000000 +0100
  6367. +++ linux-4.1.20/drivers/misc/Kconfig 2016-03-21 20:18:31.000000000 +0100
  6368. @@ -54,6 +54,7 @@
  6369. config ATMEL_TCLIB
  6370. bool "Atmel AT32/AT91 Timer/Counter Library"
  6371. depends on (AVR32 || ARCH_AT91)
  6372. + default y if PREEMPT_RT_FULL
  6373. help
  6374. Select this if you want a library to allocate the Timer/Counter
  6375. blocks found on many Atmel processors. This facilitates using
  6376. @@ -69,8 +70,7 @@
  6377. are combined to make a single 32-bit timer.
  6378. When GENERIC_CLOCKEVENTS is defined, the third timer channel
  6379. - may be used as a clock event device supporting oneshot mode
  6380. - (delays of up to two seconds) based on the 32 KiHz clock.
  6381. + may be used as a clock event device supporting oneshot mode.
  6382. config ATMEL_TCB_CLKSRC_BLOCK
  6383. int
  6384. @@ -84,6 +84,15 @@
  6385. TC can be used for other purposes, such as PWM generation and
  6386. interval timing.
  6387. +config ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  6388. + bool "TC Block use 32 KiHz clock"
  6389. + depends on ATMEL_TCB_CLKSRC
  6390. + default y if !PREEMPT_RT_FULL
  6391. + help
  6392. + Select this to use 32 KiHz base clock rate as TC block clock
  6393. + source for clock events.
  6394. +
  6395. +
  6396. config DUMMY_IRQ
  6397. tristate "Dummy IRQ handler"
  6398. default n
  6399. @@ -113,6 +122,35 @@
  6400. for information on the specific driver level and support statement
  6401. for your IBM server.
  6402. +config HWLAT_DETECTOR
  6403. + tristate "Testing module to detect hardware-induced latencies"
  6404. + depends on DEBUG_FS
  6405. + depends on RING_BUFFER
  6406. + default m
  6407. + ---help---
  6408. + A simple hardware latency detector. Use this module to detect
  6409. + large latencies introduced by the behavior of the underlying
  6410. + system firmware external to Linux. We do this using periodic
  6411. + use of stop_machine to grab all available CPUs and measure
  6412. + for unexplainable gaps in the CPU timestamp counter(s). By
  6413. + default, the module is not enabled until the "enable" file
  6414. + within the "hwlat_detector" debugfs directory is toggled.
  6415. +
  6416. + This module is often used to detect SMI (System Management
  6417. + Interrupts) on x86 systems, though is not x86 specific. To
  6418. + this end, we default to using a sample window of 1 second,
  6419. + during which we will sample for 0.5 seconds. If an SMI or
  6420. + similar event occurs during that time, it is recorded
  6421. + into an 8K samples global ring buffer until retreived.
  6422. +
  6423. + WARNING: This software should never be enabled (it can be built
  6424. + but should not be turned on after it is loaded) in a production
  6425. + environment where high latencies are a concern since the
  6426. + sampling mechanism actually introduces latencies for
  6427. + regular tasks while the CPU(s) are being held.
  6428. +
  6429. + If unsure, say N
  6430. +
  6431. config PHANTOM
  6432. tristate "Sensable PHANToM (PCI)"
  6433. depends on PCI
  6434. diff -Nur linux-4.1.20.orig/drivers/misc/Makefile linux-4.1.20/drivers/misc/Makefile
  6435. --- linux-4.1.20.orig/drivers/misc/Makefile 2016-03-17 19:11:03.000000000 +0100
  6436. +++ linux-4.1.20/drivers/misc/Makefile 2016-03-21 20:18:31.000000000 +0100
  6437. @@ -38,6 +38,7 @@
  6438. obj-$(CONFIG_HMC6352) += hmc6352.o
  6439. obj-y += eeprom/
  6440. obj-y += cb710/
  6441. +obj-$(CONFIG_HWLAT_DETECTOR) += hwlat_detector.o
  6442. obj-$(CONFIG_SPEAR13XX_PCIE_GADGET) += spear13xx_pcie_gadget.o
  6443. obj-$(CONFIG_VMWARE_BALLOON) += vmw_balloon.o
  6444. obj-$(CONFIG_ARM_CHARLCD) += arm-charlcd.o
  6445. diff -Nur linux-4.1.20.orig/drivers/misc/hwlat_detector.c linux-4.1.20/drivers/misc/hwlat_detector.c
  6446. --- linux-4.1.20.orig/drivers/misc/hwlat_detector.c 1970-01-01 01:00:00.000000000 +0100
  6447. +++ linux-4.1.20/drivers/misc/hwlat_detector.c 2016-03-21 20:18:31.000000000 +0100
  6448. @@ -0,0 +1,1240 @@
  6449. +/*
  6450. + * hwlat_detector.c - A simple Hardware Latency detector.
  6451. + *
  6452. + * Use this module to detect large system latencies induced by the behavior of
  6453. + * certain underlying system hardware or firmware, independent of Linux itself.
  6454. + * The code was developed originally to detect the presence of SMIs on Intel
  6455. + * and AMD systems, although there is no dependency upon x86 herein.
  6456. + *
  6457. + * The classical example usage of this module is in detecting the presence of
  6458. + * SMIs or System Management Interrupts on Intel and AMD systems. An SMI is a
  6459. + * somewhat special form of hardware interrupt spawned from earlier CPU debug
  6460. + * modes in which the (BIOS/EFI/etc.) firmware arranges for the South Bridge
  6461. + * LPC (or other device) to generate a special interrupt under certain
  6462. + * circumstances, for example, upon expiration of a special SMI timer device,
  6463. + * due to certain external thermal readings, on certain I/O address accesses,
  6464. + * and other situations. An SMI hits a special CPU pin, triggers a special
  6465. + * SMI mode (complete with special memory map), and the OS is unaware.
  6466. + *
  6467. + * Although certain hardware-inducing latencies are necessary (for example,
  6468. + * a modern system often requires an SMI handler for correct thermal control
  6469. + * and remote management) they can wreak havoc upon any OS-level performance
  6470. + * guarantees toward low-latency, especially when the OS is not even made
  6471. + * aware of the presence of these interrupts. For this reason, we need a
  6472. + * somewhat brute force mechanism to detect these interrupts. In this case,
  6473. + * we do it by hogging all of the CPU(s) for configurable timer intervals,
  6474. + * sampling the built-in CPU timer, looking for discontiguous readings.
  6475. + *
  6476. + * WARNING: This implementation necessarily introduces latencies. Therefore,
  6477. + * you should NEVER use this module in a production environment
  6478. + * requiring any kind of low-latency performance guarantee(s).
  6479. + *
  6480. + * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com>
  6481. + *
  6482. + * Includes useful feedback from Clark Williams <clark@redhat.com>
  6483. + *
  6484. + * This file is licensed under the terms of the GNU General Public
  6485. + * License version 2. This program is licensed "as is" without any
  6486. + * warranty of any kind, whether express or implied.
  6487. + */
  6488. +
  6489. +#include <linux/module.h>
  6490. +#include <linux/init.h>
  6491. +#include <linux/ring_buffer.h>
  6492. +#include <linux/time.h>
  6493. +#include <linux/hrtimer.h>
  6494. +#include <linux/kthread.h>
  6495. +#include <linux/debugfs.h>
  6496. +#include <linux/seq_file.h>
  6497. +#include <linux/uaccess.h>
  6498. +#include <linux/version.h>
  6499. +#include <linux/delay.h>
  6500. +#include <linux/slab.h>
  6501. +#include <linux/trace_clock.h>
  6502. +
  6503. +#define BUF_SIZE_DEFAULT 262144UL /* 8K*(sizeof(entry)) */
  6504. +#define BUF_FLAGS (RB_FL_OVERWRITE) /* no block on full */
  6505. +#define U64STR_SIZE 22 /* 20 digits max */
  6506. +
  6507. +#define VERSION "1.0.0"
  6508. +#define BANNER "hwlat_detector: "
  6509. +#define DRVNAME "hwlat_detector"
  6510. +#define DEFAULT_SAMPLE_WINDOW 1000000 /* 1s */
  6511. +#define DEFAULT_SAMPLE_WIDTH 500000 /* 0.5s */
  6512. +#define DEFAULT_LAT_THRESHOLD 10 /* 10us */
  6513. +
  6514. +/* Module metadata */
  6515. +
  6516. +MODULE_LICENSE("GPL");
  6517. +MODULE_AUTHOR("Jon Masters <jcm@redhat.com>");
  6518. +MODULE_DESCRIPTION("A simple hardware latency detector");
  6519. +MODULE_VERSION(VERSION);
  6520. +
  6521. +/* Module parameters */
  6522. +
  6523. +static int debug;
  6524. +static int enabled;
  6525. +static int threshold;
  6526. +
  6527. +module_param(debug, int, 0); /* enable debug */
  6528. +module_param(enabled, int, 0); /* enable detector */
  6529. +module_param(threshold, int, 0); /* latency threshold */
  6530. +
  6531. +/* Buffering and sampling */
  6532. +
  6533. +static struct ring_buffer *ring_buffer; /* sample buffer */
  6534. +static DEFINE_MUTEX(ring_buffer_mutex); /* lock changes */
  6535. +static unsigned long buf_size = BUF_SIZE_DEFAULT;
  6536. +static struct task_struct *kthread; /* sampling thread */
  6537. +
  6538. +/* DebugFS filesystem entries */
  6539. +
  6540. +static struct dentry *debug_dir; /* debugfs directory */
  6541. +static struct dentry *debug_max; /* maximum TSC delta */
  6542. +static struct dentry *debug_count; /* total detect count */
  6543. +static struct dentry *debug_sample_width; /* sample width us */
  6544. +static struct dentry *debug_sample_window; /* sample window us */
  6545. +static struct dentry *debug_sample; /* raw samples us */
  6546. +static struct dentry *debug_threshold; /* threshold us */
  6547. +static struct dentry *debug_enable; /* enable/disable */
  6548. +
  6549. +/* Individual samples and global state */
  6550. +
  6551. +struct sample; /* latency sample */
  6552. +struct data; /* Global state */
  6553. +
  6554. +/* Sampling functions */
  6555. +static int __buffer_add_sample(struct sample *sample);
  6556. +static struct sample *buffer_get_sample(struct sample *sample);
  6557. +
  6558. +/* Threading and state */
  6559. +static int kthread_fn(void *unused);
  6560. +static int start_kthread(void);
  6561. +static int stop_kthread(void);
  6562. +static void __reset_stats(void);
  6563. +static int init_stats(void);
  6564. +
  6565. +/* Debugfs interface */
  6566. +static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
  6567. + size_t cnt, loff_t *ppos, const u64 *entry);
  6568. +static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
  6569. + size_t cnt, loff_t *ppos, u64 *entry);
  6570. +static int debug_sample_fopen(struct inode *inode, struct file *filp);
  6571. +static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
  6572. + size_t cnt, loff_t *ppos);
  6573. +static int debug_sample_release(struct inode *inode, struct file *filp);
  6574. +static int debug_enable_fopen(struct inode *inode, struct file *filp);
  6575. +static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
  6576. + size_t cnt, loff_t *ppos);
  6577. +static ssize_t debug_enable_fwrite(struct file *file,
  6578. + const char __user *user_buffer,
  6579. + size_t user_size, loff_t *offset);
  6580. +
  6581. +/* Initialization functions */
  6582. +static int init_debugfs(void);
  6583. +static void free_debugfs(void);
  6584. +static int detector_init(void);
  6585. +static void detector_exit(void);
  6586. +
  6587. +/* Individual latency samples are stored here when detected and packed into
  6588. + * the ring_buffer circular buffer, where they are overwritten when
  6589. + * more than buf_size/sizeof(sample) samples are received. */
  6590. +struct sample {
  6591. + u64 seqnum; /* unique sequence */
  6592. + u64 duration; /* ktime delta */
  6593. + u64 outer_duration; /* ktime delta (outer loop) */
  6594. + struct timespec timestamp; /* wall time */
  6595. + unsigned long lost;
  6596. +};
  6597. +
  6598. +/* keep the global state somewhere. */
  6599. +static struct data {
  6600. +
  6601. + struct mutex lock; /* protect changes */
  6602. +
  6603. + u64 count; /* total since reset */
  6604. + u64 max_sample; /* max hardware latency */
  6605. + u64 threshold; /* sample threshold level */
  6606. +
  6607. + u64 sample_window; /* total sampling window (on+off) */
  6608. + u64 sample_width; /* active sampling portion of window */
  6609. +
  6610. + atomic_t sample_open; /* whether the sample file is open */
  6611. +
  6612. + wait_queue_head_t wq; /* waitqeue for new sample values */
  6613. +
  6614. +} data;
  6615. +
  6616. +/**
  6617. + * __buffer_add_sample - add a new latency sample recording to the ring buffer
  6618. + * @sample: The new latency sample value
  6619. + *
  6620. + * This receives a new latency sample and records it in a global ring buffer.
  6621. + * No additional locking is used in this case.
  6622. + */
  6623. +static int __buffer_add_sample(struct sample *sample)
  6624. +{
  6625. + return ring_buffer_write(ring_buffer,
  6626. + sizeof(struct sample), sample);
  6627. +}
  6628. +
  6629. +/**
  6630. + * buffer_get_sample - remove a hardware latency sample from the ring buffer
  6631. + * @sample: Pre-allocated storage for the sample
  6632. + *
  6633. + * This retrieves a hardware latency sample from the global circular buffer
  6634. + */
  6635. +static struct sample *buffer_get_sample(struct sample *sample)
  6636. +{
  6637. + struct ring_buffer_event *e = NULL;
  6638. + struct sample *s = NULL;
  6639. + unsigned int cpu = 0;
  6640. +
  6641. + if (!sample)
  6642. + return NULL;
  6643. +
  6644. + mutex_lock(&ring_buffer_mutex);
  6645. + for_each_online_cpu(cpu) {
  6646. + e = ring_buffer_consume(ring_buffer, cpu, NULL, &sample->lost);
  6647. + if (e)
  6648. + break;
  6649. + }
  6650. +
  6651. + if (e) {
  6652. + s = ring_buffer_event_data(e);
  6653. + memcpy(sample, s, sizeof(struct sample));
  6654. + } else
  6655. + sample = NULL;
  6656. + mutex_unlock(&ring_buffer_mutex);
  6657. +
  6658. + return sample;
  6659. +}
  6660. +
  6661. +#ifndef CONFIG_TRACING
  6662. +#define time_type ktime_t
  6663. +#define time_get() ktime_get()
  6664. +#define time_to_us(x) ktime_to_us(x)
  6665. +#define time_sub(a, b) ktime_sub(a, b)
  6666. +#define init_time(a, b) (a).tv64 = b
  6667. +#define time_u64(a) ((a).tv64)
  6668. +#else
  6669. +#define time_type u64
  6670. +#define time_get() trace_clock_local()
  6671. +#define time_to_us(x) div_u64(x, 1000)
  6672. +#define time_sub(a, b) ((a) - (b))
  6673. +#define init_time(a, b) (a = b)
  6674. +#define time_u64(a) a
  6675. +#endif
  6676. +/**
  6677. + * get_sample - sample the CPU TSC and look for likely hardware latencies
  6678. + *
  6679. + * Used to repeatedly capture the CPU TSC (or similar), looking for potential
  6680. + * hardware-induced latency. Called with interrupts disabled and with
  6681. + * data.lock held.
  6682. + */
  6683. +static int get_sample(void)
  6684. +{
  6685. + time_type start, t1, t2, last_t2;
  6686. + s64 diff, total = 0;
  6687. + u64 sample = 0;
  6688. + u64 outer_sample = 0;
  6689. + int ret = -1;
  6690. +
  6691. + init_time(last_t2, 0);
  6692. + start = time_get(); /* start timestamp */
  6693. +
  6694. + do {
  6695. +
  6696. + t1 = time_get(); /* we'll look for a discontinuity */
  6697. + t2 = time_get();
  6698. +
  6699. + if (time_u64(last_t2)) {
  6700. + /* Check the delta from outer loop (t2 to next t1) */
  6701. + diff = time_to_us(time_sub(t1, last_t2));
  6702. + /* This shouldn't happen */
  6703. + if (diff < 0) {
  6704. + pr_err(BANNER "time running backwards\n");
  6705. + goto out;
  6706. + }
  6707. + if (diff > outer_sample)
  6708. + outer_sample = diff;
  6709. + }
  6710. + last_t2 = t2;
  6711. +
  6712. + total = time_to_us(time_sub(t2, start)); /* sample width */
  6713. +
  6714. + /* This checks the inner loop (t1 to t2) */
  6715. + diff = time_to_us(time_sub(t2, t1)); /* current diff */
  6716. +
  6717. + /* This shouldn't happen */
  6718. + if (diff < 0) {
  6719. + pr_err(BANNER "time running backwards\n");
  6720. + goto out;
  6721. + }
  6722. +
  6723. + if (diff > sample)
  6724. + sample = diff; /* only want highest value */
  6725. +
  6726. + } while (total <= data.sample_width);
  6727. +
  6728. + ret = 0;
  6729. +
  6730. + /* If we exceed the threshold value, we have found a hardware latency */
  6731. + if (sample > data.threshold || outer_sample > data.threshold) {
  6732. + struct sample s;
  6733. +
  6734. + ret = 1;
  6735. +
  6736. + data.count++;
  6737. + s.seqnum = data.count;
  6738. + s.duration = sample;
  6739. + s.outer_duration = outer_sample;
  6740. + s.timestamp = CURRENT_TIME;
  6741. + __buffer_add_sample(&s);
  6742. +
  6743. + /* Keep a running maximum ever recorded hardware latency */
  6744. + if (sample > data.max_sample)
  6745. + data.max_sample = sample;
  6746. + }
  6747. +
  6748. +out:
  6749. + return ret;
  6750. +}
  6751. +
  6752. +/*
  6753. + * kthread_fn - The CPU time sampling/hardware latency detection kernel thread
  6754. + * @unused: A required part of the kthread API.
  6755. + *
  6756. + * Used to periodically sample the CPU TSC via a call to get_sample. We
  6757. + * disable interrupts, which does (intentionally) introduce latency since we
  6758. + * need to ensure nothing else might be running (and thus pre-empting).
  6759. + * Obviously this should never be used in production environments.
  6760. + *
  6761. + * Currently this runs on which ever CPU it was scheduled on, but most
  6762. + * real-worald hardware latency situations occur across several CPUs,
  6763. + * but we might later generalize this if we find there are any actualy
  6764. + * systems with alternate SMI delivery or other hardware latencies.
  6765. + */
  6766. +static int kthread_fn(void *unused)
  6767. +{
  6768. + int ret;
  6769. + u64 interval;
  6770. +
  6771. + while (!kthread_should_stop()) {
  6772. +
  6773. + mutex_lock(&data.lock);
  6774. +
  6775. + local_irq_disable();
  6776. + ret = get_sample();
  6777. + local_irq_enable();
  6778. +
  6779. + if (ret > 0)
  6780. + wake_up(&data.wq); /* wake up reader(s) */
  6781. +
  6782. + interval = data.sample_window - data.sample_width;
  6783. + do_div(interval, USEC_PER_MSEC); /* modifies interval value */
  6784. +
  6785. + mutex_unlock(&data.lock);
  6786. +
  6787. + if (msleep_interruptible(interval))
  6788. + break;
  6789. + }
  6790. +
  6791. + return 0;
  6792. +}
  6793. +
  6794. +/**
  6795. + * start_kthread - Kick off the hardware latency sampling/detector kthread
  6796. + *
  6797. + * This starts a kernel thread that will sit and sample the CPU timestamp
  6798. + * counter (TSC or similar) and look for potential hardware latencies.
  6799. + */
  6800. +static int start_kthread(void)
  6801. +{
  6802. + kthread = kthread_run(kthread_fn, NULL,
  6803. + DRVNAME);
  6804. + if (IS_ERR(kthread)) {
  6805. + pr_err(BANNER "could not start sampling thread\n");
  6806. + enabled = 0;
  6807. + return -ENOMEM;
  6808. + }
  6809. +
  6810. + return 0;
  6811. +}
  6812. +
  6813. +/**
  6814. + * stop_kthread - Inform the hardware latency samping/detector kthread to stop
  6815. + *
  6816. + * This kicks the running hardware latency sampling/detector kernel thread and
  6817. + * tells it to stop sampling now. Use this on unload and at system shutdown.
  6818. + */
  6819. +static int stop_kthread(void)
  6820. +{
  6821. + int ret;
  6822. +
  6823. + ret = kthread_stop(kthread);
  6824. +
  6825. + return ret;
  6826. +}
  6827. +
  6828. +/**
  6829. + * __reset_stats - Reset statistics for the hardware latency detector
  6830. + *
  6831. + * We use data to store various statistics and global state. We call this
  6832. + * function in order to reset those when "enable" is toggled on or off, and
  6833. + * also at initialization. Should be called with data.lock held.
  6834. + */
  6835. +static void __reset_stats(void)
  6836. +{
  6837. + data.count = 0;
  6838. + data.max_sample = 0;
  6839. + ring_buffer_reset(ring_buffer); /* flush out old sample entries */
  6840. +}
  6841. +
  6842. +/**
  6843. + * init_stats - Setup global state statistics for the hardware latency detector
  6844. + *
  6845. + * We use data to store various statistics and global state. We also use
  6846. + * a global ring buffer (ring_buffer) to keep raw samples of detected hardware
  6847. + * induced system latencies. This function initializes these structures and
  6848. + * allocates the global ring buffer also.
  6849. + */
  6850. +static int init_stats(void)
  6851. +{
  6852. + int ret = -ENOMEM;
  6853. +
  6854. + mutex_init(&data.lock);
  6855. + init_waitqueue_head(&data.wq);
  6856. + atomic_set(&data.sample_open, 0);
  6857. +
  6858. + ring_buffer = ring_buffer_alloc(buf_size, BUF_FLAGS);
  6859. +
  6860. + if (WARN(!ring_buffer, KERN_ERR BANNER
  6861. + "failed to allocate ring buffer!\n"))
  6862. + goto out;
  6863. +
  6864. + __reset_stats();
  6865. + data.threshold = threshold ?: DEFAULT_LAT_THRESHOLD; /* threshold us */
  6866. + data.sample_window = DEFAULT_SAMPLE_WINDOW; /* window us */
  6867. + data.sample_width = DEFAULT_SAMPLE_WIDTH; /* width us */
  6868. +
  6869. + ret = 0;
  6870. +
  6871. +out:
  6872. + return ret;
  6873. +
  6874. +}
  6875. +
  6876. +/*
  6877. + * simple_data_read - Wrapper read function for global state debugfs entries
  6878. + * @filp: The active open file structure for the debugfs "file"
  6879. + * @ubuf: The userspace provided buffer to read value into
  6880. + * @cnt: The maximum number of bytes to read
  6881. + * @ppos: The current "file" position
  6882. + * @entry: The entry to read from
  6883. + *
  6884. + * This function provides a generic read implementation for the global state
  6885. + * "data" structure debugfs filesystem entries. It would be nice to use
  6886. + * simple_attr_read directly, but we need to make sure that the data.lock
  6887. + * is held during the actual read.
  6888. + */
  6889. +static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
  6890. + size_t cnt, loff_t *ppos, const u64 *entry)
  6891. +{
  6892. + char buf[U64STR_SIZE];
  6893. + u64 val = 0;
  6894. + int len = 0;
  6895. +
  6896. + memset(buf, 0, sizeof(buf));
  6897. +
  6898. + if (!entry)
  6899. + return -EFAULT;
  6900. +
  6901. + mutex_lock(&data.lock);
  6902. + val = *entry;
  6903. + mutex_unlock(&data.lock);
  6904. +
  6905. + len = snprintf(buf, sizeof(buf), "%llu\n", (unsigned long long)val);
  6906. +
  6907. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  6908. +
  6909. +}
  6910. +
  6911. +/*
  6912. + * simple_data_write - Wrapper write function for global state debugfs entries
  6913. + * @filp: The active open file structure for the debugfs "file"
  6914. + * @ubuf: The userspace provided buffer to write value from
  6915. + * @cnt: The maximum number of bytes to write
  6916. + * @ppos: The current "file" position
  6917. + * @entry: The entry to write to
  6918. + *
  6919. + * This function provides a generic write implementation for the global state
  6920. + * "data" structure debugfs filesystem entries. It would be nice to use
  6921. + * simple_attr_write directly, but we need to make sure that the data.lock
  6922. + * is held during the actual write.
  6923. + */
  6924. +static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
  6925. + size_t cnt, loff_t *ppos, u64 *entry)
  6926. +{
  6927. + char buf[U64STR_SIZE];
  6928. + int csize = min(cnt, sizeof(buf));
  6929. + u64 val = 0;
  6930. + int err = 0;
  6931. +
  6932. + memset(buf, '\0', sizeof(buf));
  6933. + if (copy_from_user(buf, ubuf, csize))
  6934. + return -EFAULT;
  6935. +
  6936. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  6937. + err = kstrtoull(buf, 10, &val);
  6938. + if (err)
  6939. + return -EINVAL;
  6940. +
  6941. + mutex_lock(&data.lock);
  6942. + *entry = val;
  6943. + mutex_unlock(&data.lock);
  6944. +
  6945. + return csize;
  6946. +}
  6947. +
  6948. +/**
  6949. + * debug_count_fopen - Open function for "count" debugfs entry
  6950. + * @inode: The in-kernel inode representation of the debugfs "file"
  6951. + * @filp: The active open file structure for the debugfs "file"
  6952. + *
  6953. + * This function provides an open implementation for the "count" debugfs
  6954. + * interface to the hardware latency detector.
  6955. + */
  6956. +static int debug_count_fopen(struct inode *inode, struct file *filp)
  6957. +{
  6958. + return 0;
  6959. +}
  6960. +
  6961. +/**
  6962. + * debug_count_fread - Read function for "count" debugfs entry
  6963. + * @filp: The active open file structure for the debugfs "file"
  6964. + * @ubuf: The userspace provided buffer to read value into
  6965. + * @cnt: The maximum number of bytes to read
  6966. + * @ppos: The current "file" position
  6967. + *
  6968. + * This function provides a read implementation for the "count" debugfs
  6969. + * interface to the hardware latency detector. Can be used to read the
  6970. + * number of latency readings exceeding the configured threshold since
  6971. + * the detector was last reset (e.g. by writing a zero into "count").
  6972. + */
  6973. +static ssize_t debug_count_fread(struct file *filp, char __user *ubuf,
  6974. + size_t cnt, loff_t *ppos)
  6975. +{
  6976. + return simple_data_read(filp, ubuf, cnt, ppos, &data.count);
  6977. +}
  6978. +
  6979. +/**
  6980. + * debug_count_fwrite - Write function for "count" debugfs entry
  6981. + * @filp: The active open file structure for the debugfs "file"
  6982. + * @ubuf: The user buffer that contains the value to write
  6983. + * @cnt: The maximum number of bytes to write to "file"
  6984. + * @ppos: The current position in the debugfs "file"
  6985. + *
  6986. + * This function provides a write implementation for the "count" debugfs
  6987. + * interface to the hardware latency detector. Can be used to write a
  6988. + * desired value, especially to zero the total count.
  6989. + */
  6990. +static ssize_t debug_count_fwrite(struct file *filp,
  6991. + const char __user *ubuf,
  6992. + size_t cnt,
  6993. + loff_t *ppos)
  6994. +{
  6995. + return simple_data_write(filp, ubuf, cnt, ppos, &data.count);
  6996. +}
  6997. +
  6998. +/**
  6999. + * debug_enable_fopen - Dummy open function for "enable" debugfs interface
  7000. + * @inode: The in-kernel inode representation of the debugfs "file"
  7001. + * @filp: The active open file structure for the debugfs "file"
  7002. + *
  7003. + * This function provides an open implementation for the "enable" debugfs
  7004. + * interface to the hardware latency detector.
  7005. + */
  7006. +static int debug_enable_fopen(struct inode *inode, struct file *filp)
  7007. +{
  7008. + return 0;
  7009. +}
  7010. +
  7011. +/**
  7012. + * debug_enable_fread - Read function for "enable" debugfs interface
  7013. + * @filp: The active open file structure for the debugfs "file"
  7014. + * @ubuf: The userspace provided buffer to read value into
  7015. + * @cnt: The maximum number of bytes to read
  7016. + * @ppos: The current "file" position
  7017. + *
  7018. + * This function provides a read implementation for the "enable" debugfs
  7019. + * interface to the hardware latency detector. Can be used to determine
  7020. + * whether the detector is currently enabled ("0\n" or "1\n" returned).
  7021. + */
  7022. +static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
  7023. + size_t cnt, loff_t *ppos)
  7024. +{
  7025. + char buf[4];
  7026. +
  7027. + if ((cnt < sizeof(buf)) || (*ppos))
  7028. + return 0;
  7029. +
  7030. + buf[0] = enabled ? '1' : '0';
  7031. + buf[1] = '\n';
  7032. + buf[2] = '\0';
  7033. + if (copy_to_user(ubuf, buf, strlen(buf)))
  7034. + return -EFAULT;
  7035. + return *ppos = strlen(buf);
  7036. +}
  7037. +
  7038. +/**
  7039. + * debug_enable_fwrite - Write function for "enable" debugfs interface
  7040. + * @filp: The active open file structure for the debugfs "file"
  7041. + * @ubuf: The user buffer that contains the value to write
  7042. + * @cnt: The maximum number of bytes to write to "file"
  7043. + * @ppos: The current position in the debugfs "file"
  7044. + *
  7045. + * This function provides a write implementation for the "enable" debugfs
  7046. + * interface to the hardware latency detector. Can be used to enable or
  7047. + * disable the detector, which will have the side-effect of possibly
  7048. + * also resetting the global stats and kicking off the measuring
  7049. + * kthread (on an enable) or the converse (upon a disable).
  7050. + */
  7051. +static ssize_t debug_enable_fwrite(struct file *filp,
  7052. + const char __user *ubuf,
  7053. + size_t cnt,
  7054. + loff_t *ppos)
  7055. +{
  7056. + char buf[4];
  7057. + int csize = min(cnt, sizeof(buf));
  7058. + long val = 0;
  7059. + int err = 0;
  7060. +
  7061. + memset(buf, '\0', sizeof(buf));
  7062. + if (copy_from_user(buf, ubuf, csize))
  7063. + return -EFAULT;
  7064. +
  7065. + buf[sizeof(buf)-1] = '\0'; /* just in case */
  7066. + err = kstrtoul(buf, 10, &val);
  7067. + if (0 != err)
  7068. + return -EINVAL;
  7069. +
  7070. + if (val) {
  7071. + if (enabled)
  7072. + goto unlock;
  7073. + enabled = 1;
  7074. + __reset_stats();
  7075. + if (start_kthread())
  7076. + return -EFAULT;
  7077. + } else {
  7078. + if (!enabled)
  7079. + goto unlock;
  7080. + enabled = 0;
  7081. + err = stop_kthread();
  7082. + if (err) {
  7083. + pr_err(BANNER "cannot stop kthread\n");
  7084. + return -EFAULT;
  7085. + }
  7086. + wake_up(&data.wq); /* reader(s) should return */
  7087. + }
  7088. +unlock:
  7089. + return csize;
  7090. +}
  7091. +
  7092. +/**
  7093. + * debug_max_fopen - Open function for "max" debugfs entry
  7094. + * @inode: The in-kernel inode representation of the debugfs "file"
  7095. + * @filp: The active open file structure for the debugfs "file"
  7096. + *
  7097. + * This function provides an open implementation for the "max" debugfs
  7098. + * interface to the hardware latency detector.
  7099. + */
  7100. +static int debug_max_fopen(struct inode *inode, struct file *filp)
  7101. +{
  7102. + return 0;
  7103. +}
  7104. +
  7105. +/**
  7106. + * debug_max_fread - Read function for "max" debugfs entry
  7107. + * @filp: The active open file structure for the debugfs "file"
  7108. + * @ubuf: The userspace provided buffer to read value into
  7109. + * @cnt: The maximum number of bytes to read
  7110. + * @ppos: The current "file" position
  7111. + *
  7112. + * This function provides a read implementation for the "max" debugfs
  7113. + * interface to the hardware latency detector. Can be used to determine
  7114. + * the maximum latency value observed since it was last reset.
  7115. + */
  7116. +static ssize_t debug_max_fread(struct file *filp, char __user *ubuf,
  7117. + size_t cnt, loff_t *ppos)
  7118. +{
  7119. + return simple_data_read(filp, ubuf, cnt, ppos, &data.max_sample);
  7120. +}
  7121. +
  7122. +/**
  7123. + * debug_max_fwrite - Write function for "max" debugfs entry
  7124. + * @filp: The active open file structure for the debugfs "file"
  7125. + * @ubuf: The user buffer that contains the value to write
  7126. + * @cnt: The maximum number of bytes to write to "file"
  7127. + * @ppos: The current position in the debugfs "file"
  7128. + *
  7129. + * This function provides a write implementation for the "max" debugfs
  7130. + * interface to the hardware latency detector. Can be used to reset the
  7131. + * maximum or set it to some other desired value - if, then, subsequent
  7132. + * measurements exceed this value, the maximum will be updated.
  7133. + */
  7134. +static ssize_t debug_max_fwrite(struct file *filp,
  7135. + const char __user *ubuf,
  7136. + size_t cnt,
  7137. + loff_t *ppos)
  7138. +{
  7139. + return simple_data_write(filp, ubuf, cnt, ppos, &data.max_sample);
  7140. +}
  7141. +
  7142. +
  7143. +/**
  7144. + * debug_sample_fopen - An open function for "sample" debugfs interface
  7145. + * @inode: The in-kernel inode representation of this debugfs "file"
  7146. + * @filp: The active open file structure for the debugfs "file"
  7147. + *
  7148. + * This function handles opening the "sample" file within the hardware
  7149. + * latency detector debugfs directory interface. This file is used to read
  7150. + * raw samples from the global ring_buffer and allows the user to see a
  7151. + * running latency history. Can be opened blocking or non-blocking,
  7152. + * affecting whether it behaves as a buffer read pipe, or does not.
  7153. + * Implements simple locking to prevent multiple simultaneous use.
  7154. + */
  7155. +static int debug_sample_fopen(struct inode *inode, struct file *filp)
  7156. +{
  7157. + if (!atomic_add_unless(&data.sample_open, 1, 1))
  7158. + return -EBUSY;
  7159. + else
  7160. + return 0;
  7161. +}
  7162. +
  7163. +/**
  7164. + * debug_sample_fread - A read function for "sample" debugfs interface
  7165. + * @filp: The active open file structure for the debugfs "file"
  7166. + * @ubuf: The user buffer that will contain the samples read
  7167. + * @cnt: The maximum bytes to read from the debugfs "file"
  7168. + * @ppos: The current position in the debugfs "file"
  7169. + *
  7170. + * This function handles reading from the "sample" file within the hardware
  7171. + * latency detector debugfs directory interface. This file is used to read
  7172. + * raw samples from the global ring_buffer and allows the user to see a
  7173. + * running latency history. By default this will block pending a new
  7174. + * value written into the sample buffer, unless there are already a
  7175. + * number of value(s) waiting in the buffer, or the sample file was
  7176. + * previously opened in a non-blocking mode of operation.
  7177. + */
  7178. +static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
  7179. + size_t cnt, loff_t *ppos)
  7180. +{
  7181. + int len = 0;
  7182. + char buf[64];
  7183. + struct sample *sample = NULL;
  7184. +
  7185. + if (!enabled)
  7186. + return 0;
  7187. +
  7188. + sample = kzalloc(sizeof(struct sample), GFP_KERNEL);
  7189. + if (!sample)
  7190. + return -ENOMEM;
  7191. +
  7192. + while (!buffer_get_sample(sample)) {
  7193. +
  7194. + DEFINE_WAIT(wait);
  7195. +
  7196. + if (filp->f_flags & O_NONBLOCK) {
  7197. + len = -EAGAIN;
  7198. + goto out;
  7199. + }
  7200. +
  7201. + prepare_to_wait(&data.wq, &wait, TASK_INTERRUPTIBLE);
  7202. + schedule();
  7203. + finish_wait(&data.wq, &wait);
  7204. +
  7205. + if (signal_pending(current)) {
  7206. + len = -EINTR;
  7207. + goto out;
  7208. + }
  7209. +
  7210. + if (!enabled) { /* enable was toggled */
  7211. + len = 0;
  7212. + goto out;
  7213. + }
  7214. + }
  7215. +
  7216. + len = snprintf(buf, sizeof(buf), "%010lu.%010lu\t%llu\t%llu\n",
  7217. + sample->timestamp.tv_sec,
  7218. + sample->timestamp.tv_nsec,
  7219. + sample->duration,
  7220. + sample->outer_duration);
  7221. +
  7222. +
  7223. + /* handling partial reads is more trouble than it's worth */
  7224. + if (len > cnt)
  7225. + goto out;
  7226. +
  7227. + if (copy_to_user(ubuf, buf, len))
  7228. + len = -EFAULT;
  7229. +
  7230. +out:
  7231. + kfree(sample);
  7232. + return len;
  7233. +}
  7234. +
  7235. +/**
  7236. + * debug_sample_release - Release function for "sample" debugfs interface
  7237. + * @inode: The in-kernel inode represenation of the debugfs "file"
  7238. + * @filp: The active open file structure for the debugfs "file"
  7239. + *
  7240. + * This function completes the close of the debugfs interface "sample" file.
  7241. + * Frees the sample_open "lock" so that other users may open the interface.
  7242. + */
  7243. +static int debug_sample_release(struct inode *inode, struct file *filp)
  7244. +{
  7245. + atomic_dec(&data.sample_open);
  7246. +
  7247. + return 0;
  7248. +}
  7249. +
  7250. +/**
  7251. + * debug_threshold_fopen - Open function for "threshold" debugfs entry
  7252. + * @inode: The in-kernel inode representation of the debugfs "file"
  7253. + * @filp: The active open file structure for the debugfs "file"
  7254. + *
  7255. + * This function provides an open implementation for the "threshold" debugfs
  7256. + * interface to the hardware latency detector.
  7257. + */
  7258. +static int debug_threshold_fopen(struct inode *inode, struct file *filp)
  7259. +{
  7260. + return 0;
  7261. +}
  7262. +
  7263. +/**
  7264. + * debug_threshold_fread - Read function for "threshold" debugfs entry
  7265. + * @filp: The active open file structure for the debugfs "file"
  7266. + * @ubuf: The userspace provided buffer to read value into
  7267. + * @cnt: The maximum number of bytes to read
  7268. + * @ppos: The current "file" position
  7269. + *
  7270. + * This function provides a read implementation for the "threshold" debugfs
  7271. + * interface to the hardware latency detector. It can be used to determine
  7272. + * the current threshold level at which a latency will be recorded in the
  7273. + * global ring buffer, typically on the order of 10us.
  7274. + */
  7275. +static ssize_t debug_threshold_fread(struct file *filp, char __user *ubuf,
  7276. + size_t cnt, loff_t *ppos)
  7277. +{
  7278. + return simple_data_read(filp, ubuf, cnt, ppos, &data.threshold);
  7279. +}
  7280. +
  7281. +/**
  7282. + * debug_threshold_fwrite - Write function for "threshold" debugfs entry
  7283. + * @filp: The active open file structure for the debugfs "file"
  7284. + * @ubuf: The user buffer that contains the value to write
  7285. + * @cnt: The maximum number of bytes to write to "file"
  7286. + * @ppos: The current position in the debugfs "file"
  7287. + *
  7288. + * This function provides a write implementation for the "threshold" debugfs
  7289. + * interface to the hardware latency detector. It can be used to configure
  7290. + * the threshold level at which any subsequently detected latencies will
  7291. + * be recorded into the global ring buffer.
  7292. + */
  7293. +static ssize_t debug_threshold_fwrite(struct file *filp,
  7294. + const char __user *ubuf,
  7295. + size_t cnt,
  7296. + loff_t *ppos)
  7297. +{
  7298. + int ret;
  7299. +
  7300. + ret = simple_data_write(filp, ubuf, cnt, ppos, &data.threshold);
  7301. +
  7302. + if (enabled)
  7303. + wake_up_process(kthread);
  7304. +
  7305. + return ret;
  7306. +}
  7307. +
  7308. +/**
  7309. + * debug_width_fopen - Open function for "width" debugfs entry
  7310. + * @inode: The in-kernel inode representation of the debugfs "file"
  7311. + * @filp: The active open file structure for the debugfs "file"
  7312. + *
  7313. + * This function provides an open implementation for the "width" debugfs
  7314. + * interface to the hardware latency detector.
  7315. + */
  7316. +static int debug_width_fopen(struct inode *inode, struct file *filp)
  7317. +{
  7318. + return 0;
  7319. +}
  7320. +
  7321. +/**
  7322. + * debug_width_fread - Read function for "width" debugfs entry
  7323. + * @filp: The active open file structure for the debugfs "file"
  7324. + * @ubuf: The userspace provided buffer to read value into
  7325. + * @cnt: The maximum number of bytes to read
  7326. + * @ppos: The current "file" position
  7327. + *
  7328. + * This function provides a read implementation for the "width" debugfs
  7329. + * interface to the hardware latency detector. It can be used to determine
  7330. + * for how many us of the total window us we will actively sample for any
  7331. + * hardware-induced latecy periods. Obviously, it is not possible to
  7332. + * sample constantly and have the system respond to a sample reader, or,
  7333. + * worse, without having the system appear to have gone out to lunch.
  7334. + */
  7335. +static ssize_t debug_width_fread(struct file *filp, char __user *ubuf,
  7336. + size_t cnt, loff_t *ppos)
  7337. +{
  7338. + return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_width);
  7339. +}
  7340. +
  7341. +/**
  7342. + * debug_width_fwrite - Write function for "width" debugfs entry
  7343. + * @filp: The active open file structure for the debugfs "file"
  7344. + * @ubuf: The user buffer that contains the value to write
  7345. + * @cnt: The maximum number of bytes to write to "file"
  7346. + * @ppos: The current position in the debugfs "file"
  7347. + *
  7348. + * This function provides a write implementation for the "width" debugfs
  7349. + * interface to the hardware latency detector. It can be used to configure
  7350. + * for how many us of the total window us we will actively sample for any
  7351. + * hardware-induced latency periods. Obviously, it is not possible to
  7352. + * sample constantly and have the system respond to a sample reader, or,
  7353. + * worse, without having the system appear to have gone out to lunch. It
  7354. + * is enforced that width is less that the total window size.
  7355. + */
  7356. +static ssize_t debug_width_fwrite(struct file *filp,
  7357. + const char __user *ubuf,
  7358. + size_t cnt,
  7359. + loff_t *ppos)
  7360. +{
  7361. + char buf[U64STR_SIZE];
  7362. + int csize = min(cnt, sizeof(buf));
  7363. + u64 val = 0;
  7364. + int err = 0;
  7365. +
  7366. + memset(buf, '\0', sizeof(buf));
  7367. + if (copy_from_user(buf, ubuf, csize))
  7368. + return -EFAULT;
  7369. +
  7370. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  7371. + err = kstrtoull(buf, 10, &val);
  7372. + if (0 != err)
  7373. + return -EINVAL;
  7374. +
  7375. + mutex_lock(&data.lock);
  7376. + if (val < data.sample_window)
  7377. + data.sample_width = val;
  7378. + else {
  7379. + mutex_unlock(&data.lock);
  7380. + return -EINVAL;
  7381. + }
  7382. + mutex_unlock(&data.lock);
  7383. +
  7384. + if (enabled)
  7385. + wake_up_process(kthread);
  7386. +
  7387. + return csize;
  7388. +}
  7389. +
  7390. +/**
  7391. + * debug_window_fopen - Open function for "window" debugfs entry
  7392. + * @inode: The in-kernel inode representation of the debugfs "file"
  7393. + * @filp: The active open file structure for the debugfs "file"
  7394. + *
  7395. + * This function provides an open implementation for the "window" debugfs
  7396. + * interface to the hardware latency detector. The window is the total time
  7397. + * in us that will be considered one sample period. Conceptually, windows
  7398. + * occur back-to-back and contain a sample width period during which
  7399. + * actual sampling occurs.
  7400. + */
  7401. +static int debug_window_fopen(struct inode *inode, struct file *filp)
  7402. +{
  7403. + return 0;
  7404. +}
  7405. +
  7406. +/**
  7407. + * debug_window_fread - Read function for "window" debugfs entry
  7408. + * @filp: The active open file structure for the debugfs "file"
  7409. + * @ubuf: The userspace provided buffer to read value into
  7410. + * @cnt: The maximum number of bytes to read
  7411. + * @ppos: The current "file" position
  7412. + *
  7413. + * This function provides a read implementation for the "window" debugfs
  7414. + * interface to the hardware latency detector. The window is the total time
  7415. + * in us that will be considered one sample period. Conceptually, windows
  7416. + * occur back-to-back and contain a sample width period during which
  7417. + * actual sampling occurs. Can be used to read the total window size.
  7418. + */
  7419. +static ssize_t debug_window_fread(struct file *filp, char __user *ubuf,
  7420. + size_t cnt, loff_t *ppos)
  7421. +{
  7422. + return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_window);
  7423. +}
  7424. +
  7425. +/**
  7426. + * debug_window_fwrite - Write function for "window" debugfs entry
  7427. + * @filp: The active open file structure for the debugfs "file"
  7428. + * @ubuf: The user buffer that contains the value to write
  7429. + * @cnt: The maximum number of bytes to write to "file"
  7430. + * @ppos: The current position in the debugfs "file"
  7431. + *
  7432. + * This function provides a write implementation for the "window" debufds
  7433. + * interface to the hardware latency detetector. The window is the total time
  7434. + * in us that will be considered one sample period. Conceptually, windows
  7435. + * occur back-to-back and contain a sample width period during which
  7436. + * actual sampling occurs. Can be used to write a new total window size. It
  7437. + * is enfoced that any value written must be greater than the sample width
  7438. + * size, or an error results.
  7439. + */
  7440. +static ssize_t debug_window_fwrite(struct file *filp,
  7441. + const char __user *ubuf,
  7442. + size_t cnt,
  7443. + loff_t *ppos)
  7444. +{
  7445. + char buf[U64STR_SIZE];
  7446. + int csize = min(cnt, sizeof(buf));
  7447. + u64 val = 0;
  7448. + int err = 0;
  7449. +
  7450. + memset(buf, '\0', sizeof(buf));
  7451. + if (copy_from_user(buf, ubuf, csize))
  7452. + return -EFAULT;
  7453. +
  7454. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  7455. + err = kstrtoull(buf, 10, &val);
  7456. + if (0 != err)
  7457. + return -EINVAL;
  7458. +
  7459. + mutex_lock(&data.lock);
  7460. + if (data.sample_width < val)
  7461. + data.sample_window = val;
  7462. + else {
  7463. + mutex_unlock(&data.lock);
  7464. + return -EINVAL;
  7465. + }
  7466. + mutex_unlock(&data.lock);
  7467. +
  7468. + return csize;
  7469. +}
  7470. +
  7471. +/*
  7472. + * Function pointers for the "count" debugfs file operations
  7473. + */
  7474. +static const struct file_operations count_fops = {
  7475. + .open = debug_count_fopen,
  7476. + .read = debug_count_fread,
  7477. + .write = debug_count_fwrite,
  7478. + .owner = THIS_MODULE,
  7479. +};
  7480. +
  7481. +/*
  7482. + * Function pointers for the "enable" debugfs file operations
  7483. + */
  7484. +static const struct file_operations enable_fops = {
  7485. + .open = debug_enable_fopen,
  7486. + .read = debug_enable_fread,
  7487. + .write = debug_enable_fwrite,
  7488. + .owner = THIS_MODULE,
  7489. +};
  7490. +
  7491. +/*
  7492. + * Function pointers for the "max" debugfs file operations
  7493. + */
  7494. +static const struct file_operations max_fops = {
  7495. + .open = debug_max_fopen,
  7496. + .read = debug_max_fread,
  7497. + .write = debug_max_fwrite,
  7498. + .owner = THIS_MODULE,
  7499. +};
  7500. +
  7501. +/*
  7502. + * Function pointers for the "sample" debugfs file operations
  7503. + */
  7504. +static const struct file_operations sample_fops = {
  7505. + .open = debug_sample_fopen,
  7506. + .read = debug_sample_fread,
  7507. + .release = debug_sample_release,
  7508. + .owner = THIS_MODULE,
  7509. +};
  7510. +
  7511. +/*
  7512. + * Function pointers for the "threshold" debugfs file operations
  7513. + */
  7514. +static const struct file_operations threshold_fops = {
  7515. + .open = debug_threshold_fopen,
  7516. + .read = debug_threshold_fread,
  7517. + .write = debug_threshold_fwrite,
  7518. + .owner = THIS_MODULE,
  7519. +};
  7520. +
  7521. +/*
  7522. + * Function pointers for the "width" debugfs file operations
  7523. + */
  7524. +static const struct file_operations width_fops = {
  7525. + .open = debug_width_fopen,
  7526. + .read = debug_width_fread,
  7527. + .write = debug_width_fwrite,
  7528. + .owner = THIS_MODULE,
  7529. +};
  7530. +
  7531. +/*
  7532. + * Function pointers for the "window" debugfs file operations
  7533. + */
  7534. +static const struct file_operations window_fops = {
  7535. + .open = debug_window_fopen,
  7536. + .read = debug_window_fread,
  7537. + .write = debug_window_fwrite,
  7538. + .owner = THIS_MODULE,
  7539. +};
  7540. +
  7541. +/**
  7542. + * init_debugfs - A function to initialize the debugfs interface files
  7543. + *
  7544. + * This function creates entries in debugfs for "hwlat_detector", including
  7545. + * files to read values from the detector, current samples, and the
  7546. + * maximum sample that has been captured since the hardware latency
  7547. + * dectector was started.
  7548. + */
  7549. +static int init_debugfs(void)
  7550. +{
  7551. + int ret = -ENOMEM;
  7552. +
  7553. + debug_dir = debugfs_create_dir(DRVNAME, NULL);
  7554. + if (!debug_dir)
  7555. + goto err_debug_dir;
  7556. +
  7557. + debug_sample = debugfs_create_file("sample", 0444,
  7558. + debug_dir, NULL,
  7559. + &sample_fops);
  7560. + if (!debug_sample)
  7561. + goto err_sample;
  7562. +
  7563. + debug_count = debugfs_create_file("count", 0444,
  7564. + debug_dir, NULL,
  7565. + &count_fops);
  7566. + if (!debug_count)
  7567. + goto err_count;
  7568. +
  7569. + debug_max = debugfs_create_file("max", 0444,
  7570. + debug_dir, NULL,
  7571. + &max_fops);
  7572. + if (!debug_max)
  7573. + goto err_max;
  7574. +
  7575. + debug_sample_window = debugfs_create_file("window", 0644,
  7576. + debug_dir, NULL,
  7577. + &window_fops);
  7578. + if (!debug_sample_window)
  7579. + goto err_window;
  7580. +
  7581. + debug_sample_width = debugfs_create_file("width", 0644,
  7582. + debug_dir, NULL,
  7583. + &width_fops);
  7584. + if (!debug_sample_width)
  7585. + goto err_width;
  7586. +
  7587. + debug_threshold = debugfs_create_file("threshold", 0644,
  7588. + debug_dir, NULL,
  7589. + &threshold_fops);
  7590. + if (!debug_threshold)
  7591. + goto err_threshold;
  7592. +
  7593. + debug_enable = debugfs_create_file("enable", 0644,
  7594. + debug_dir, &enabled,
  7595. + &enable_fops);
  7596. + if (!debug_enable)
  7597. + goto err_enable;
  7598. +
  7599. + else {
  7600. + ret = 0;
  7601. + goto out;
  7602. + }
  7603. +
  7604. +err_enable:
  7605. + debugfs_remove(debug_threshold);
  7606. +err_threshold:
  7607. + debugfs_remove(debug_sample_width);
  7608. +err_width:
  7609. + debugfs_remove(debug_sample_window);
  7610. +err_window:
  7611. + debugfs_remove(debug_max);
  7612. +err_max:
  7613. + debugfs_remove(debug_count);
  7614. +err_count:
  7615. + debugfs_remove(debug_sample);
  7616. +err_sample:
  7617. + debugfs_remove(debug_dir);
  7618. +err_debug_dir:
  7619. +out:
  7620. + return ret;
  7621. +}
  7622. +
  7623. +/**
  7624. + * free_debugfs - A function to cleanup the debugfs file interface
  7625. + */
  7626. +static void free_debugfs(void)
  7627. +{
  7628. + /* could also use a debugfs_remove_recursive */
  7629. + debugfs_remove(debug_enable);
  7630. + debugfs_remove(debug_threshold);
  7631. + debugfs_remove(debug_sample_width);
  7632. + debugfs_remove(debug_sample_window);
  7633. + debugfs_remove(debug_max);
  7634. + debugfs_remove(debug_count);
  7635. + debugfs_remove(debug_sample);
  7636. + debugfs_remove(debug_dir);
  7637. +}
  7638. +
  7639. +/**
  7640. + * detector_init - Standard module initialization code
  7641. + */
  7642. +static int detector_init(void)
  7643. +{
  7644. + int ret = -ENOMEM;
  7645. +
  7646. + pr_info(BANNER "version %s\n", VERSION);
  7647. +
  7648. + ret = init_stats();
  7649. + if (0 != ret)
  7650. + goto out;
  7651. +
  7652. + ret = init_debugfs();
  7653. + if (0 != ret)
  7654. + goto err_stats;
  7655. +
  7656. + if (enabled)
  7657. + ret = start_kthread();
  7658. +
  7659. + goto out;
  7660. +
  7661. +err_stats:
  7662. + ring_buffer_free(ring_buffer);
  7663. +out:
  7664. + return ret;
  7665. +
  7666. +}
  7667. +
  7668. +/**
  7669. + * detector_exit - Standard module cleanup code
  7670. + */
  7671. +static void detector_exit(void)
  7672. +{
  7673. + int err;
  7674. +
  7675. + if (enabled) {
  7676. + enabled = 0;
  7677. + err = stop_kthread();
  7678. + if (err)
  7679. + pr_err(BANNER "cannot stop kthread\n");
  7680. + }
  7681. +
  7682. + free_debugfs();
  7683. + ring_buffer_free(ring_buffer); /* free up the ring buffer */
  7684. +
  7685. +}
  7686. +
  7687. +module_init(detector_init);
  7688. +module_exit(detector_exit);
  7689. diff -Nur linux-4.1.20.orig/drivers/mmc/host/mmci.c linux-4.1.20/drivers/mmc/host/mmci.c
  7690. --- linux-4.1.20.orig/drivers/mmc/host/mmci.c 2016-03-17 19:11:03.000000000 +0100
  7691. +++ linux-4.1.20/drivers/mmc/host/mmci.c 2016-03-21 20:18:31.000000000 +0100
  7692. @@ -1155,15 +1155,12 @@
  7693. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  7694. struct variant_data *variant = host->variant;
  7695. void __iomem *base = host->base;
  7696. - unsigned long flags;
  7697. u32 status;
  7698. status = readl(base + MMCISTATUS);
  7699. dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
  7700. - local_irq_save(flags);
  7701. -
  7702. do {
  7703. unsigned int remain, len;
  7704. char *buffer;
  7705. @@ -1203,8 +1200,6 @@
  7706. sg_miter_stop(sg_miter);
  7707. - local_irq_restore(flags);
  7708. -
  7709. /*
  7710. * If we have less than the fifo 'half-full' threshold to transfer,
  7711. * trigger a PIO interrupt as soon as any data is available.
  7712. diff -Nur linux-4.1.20.orig/drivers/net/ethernet/3com/3c59x.c linux-4.1.20/drivers/net/ethernet/3com/3c59x.c
  7713. --- linux-4.1.20.orig/drivers/net/ethernet/3com/3c59x.c 2016-03-17 19:11:03.000000000 +0100
  7714. +++ linux-4.1.20/drivers/net/ethernet/3com/3c59x.c 2016-03-21 20:18:31.000000000 +0100
  7715. @@ -842,9 +842,9 @@
  7716. {
  7717. struct vortex_private *vp = netdev_priv(dev);
  7718. unsigned long flags;
  7719. - local_irq_save(flags);
  7720. + local_irq_save_nort(flags);
  7721. (vp->full_bus_master_rx ? boomerang_interrupt:vortex_interrupt)(dev->irq,dev);
  7722. - local_irq_restore(flags);
  7723. + local_irq_restore_nort(flags);
  7724. }
  7725. #endif
  7726. @@ -1916,12 +1916,12 @@
  7727. * Block interrupts because vortex_interrupt does a bare spin_lock()
  7728. */
  7729. unsigned long flags;
  7730. - local_irq_save(flags);
  7731. + local_irq_save_nort(flags);
  7732. if (vp->full_bus_master_tx)
  7733. boomerang_interrupt(dev->irq, dev);
  7734. else
  7735. vortex_interrupt(dev->irq, dev);
  7736. - local_irq_restore(flags);
  7737. + local_irq_restore_nort(flags);
  7738. }
  7739. }
  7740. diff -Nur linux-4.1.20.orig/drivers/net/ethernet/atheros/atl1c/atl1c_main.c linux-4.1.20/drivers/net/ethernet/atheros/atl1c/atl1c_main.c
  7741. --- linux-4.1.20.orig/drivers/net/ethernet/atheros/atl1c/atl1c_main.c 2016-03-17 19:11:03.000000000 +0100
  7742. +++ linux-4.1.20/drivers/net/ethernet/atheros/atl1c/atl1c_main.c 2016-03-21 20:18:31.000000000 +0100
  7743. @@ -2212,11 +2212,7 @@
  7744. }
  7745. tpd_req = atl1c_cal_tpd_req(skb);
  7746. - if (!spin_trylock_irqsave(&adapter->tx_lock, flags)) {
  7747. - if (netif_msg_pktdata(adapter))
  7748. - dev_info(&adapter->pdev->dev, "tx locked\n");
  7749. - return NETDEV_TX_LOCKED;
  7750. - }
  7751. + spin_lock_irqsave(&adapter->tx_lock, flags);
  7752. if (atl1c_tpd_avail(adapter, type) < tpd_req) {
  7753. /* no enough descriptor, just stop queue */
  7754. diff -Nur linux-4.1.20.orig/drivers/net/ethernet/atheros/atl1e/atl1e_main.c linux-4.1.20/drivers/net/ethernet/atheros/atl1e/atl1e_main.c
  7755. --- linux-4.1.20.orig/drivers/net/ethernet/atheros/atl1e/atl1e_main.c 2016-03-17 19:11:03.000000000 +0100
  7756. +++ linux-4.1.20/drivers/net/ethernet/atheros/atl1e/atl1e_main.c 2016-03-21 20:18:31.000000000 +0100
  7757. @@ -1880,8 +1880,7 @@
  7758. return NETDEV_TX_OK;
  7759. }
  7760. tpd_req = atl1e_cal_tdp_req(skb);
  7761. - if (!spin_trylock_irqsave(&adapter->tx_lock, flags))
  7762. - return NETDEV_TX_LOCKED;
  7763. + spin_lock_irqsave(&adapter->tx_lock, flags);
  7764. if (atl1e_tpd_avail(adapter) < tpd_req) {
  7765. /* no enough descriptor, just stop queue */
  7766. diff -Nur linux-4.1.20.orig/drivers/net/ethernet/chelsio/cxgb/sge.c linux-4.1.20/drivers/net/ethernet/chelsio/cxgb/sge.c
  7767. --- linux-4.1.20.orig/drivers/net/ethernet/chelsio/cxgb/sge.c 2016-03-17 19:11:03.000000000 +0100
  7768. +++ linux-4.1.20/drivers/net/ethernet/chelsio/cxgb/sge.c 2016-03-21 20:18:31.000000000 +0100
  7769. @@ -1664,8 +1664,7 @@
  7770. struct cmdQ *q = &sge->cmdQ[qid];
  7771. unsigned int credits, pidx, genbit, count, use_sched_skb = 0;
  7772. - if (!spin_trylock(&q->lock))
  7773. - return NETDEV_TX_LOCKED;
  7774. + spin_lock(&q->lock);
  7775. reclaim_completed_tx(sge, q);
  7776. diff -Nur linux-4.1.20.orig/drivers/net/ethernet/freescale/gianfar.c linux-4.1.20/drivers/net/ethernet/freescale/gianfar.c
  7777. --- linux-4.1.20.orig/drivers/net/ethernet/freescale/gianfar.c 2016-03-17 19:11:03.000000000 +0100
  7778. +++ linux-4.1.20/drivers/net/ethernet/freescale/gianfar.c 2016-03-21 20:18:31.000000000 +0100
  7779. @@ -1540,7 +1540,7 @@
  7780. if (netif_running(ndev)) {
  7781. - local_irq_save(flags);
  7782. + local_irq_save_nort(flags);
  7783. lock_tx_qs(priv);
  7784. gfar_halt_nodisable(priv);
  7785. @@ -1556,7 +1556,7 @@
  7786. gfar_write(&regs->maccfg1, tempval);
  7787. unlock_tx_qs(priv);
  7788. - local_irq_restore(flags);
  7789. + local_irq_restore_nort(flags);
  7790. disable_napi(priv);
  7791. @@ -1598,7 +1598,7 @@
  7792. /* Disable Magic Packet mode, in case something
  7793. * else woke us up.
  7794. */
  7795. - local_irq_save(flags);
  7796. + local_irq_save_nort(flags);
  7797. lock_tx_qs(priv);
  7798. tempval = gfar_read(&regs->maccfg2);
  7799. @@ -1608,7 +1608,7 @@
  7800. gfar_start(priv);
  7801. unlock_tx_qs(priv);
  7802. - local_irq_restore(flags);
  7803. + local_irq_restore_nort(flags);
  7804. netif_device_attach(ndev);
  7805. @@ -3418,14 +3418,14 @@
  7806. dev->stats.tx_dropped++;
  7807. atomic64_inc(&priv->extra_stats.tx_underrun);
  7808. - local_irq_save(flags);
  7809. + local_irq_save_nort(flags);
  7810. lock_tx_qs(priv);
  7811. /* Reactivate the Tx Queues */
  7812. gfar_write(&regs->tstat, gfargrp->tstat);
  7813. unlock_tx_qs(priv);
  7814. - local_irq_restore(flags);
  7815. + local_irq_restore_nort(flags);
  7816. }
  7817. netif_dbg(priv, tx_err, dev, "Transmit Error\n");
  7818. }
  7819. diff -Nur linux-4.1.20.orig/drivers/net/ethernet/neterion/s2io.c linux-4.1.20/drivers/net/ethernet/neterion/s2io.c
  7820. --- linux-4.1.20.orig/drivers/net/ethernet/neterion/s2io.c 2016-03-17 19:11:03.000000000 +0100
  7821. +++ linux-4.1.20/drivers/net/ethernet/neterion/s2io.c 2016-03-21 20:18:31.000000000 +0100
  7822. @@ -4084,12 +4084,7 @@
  7823. [skb->priority & (MAX_TX_FIFOS - 1)];
  7824. fifo = &mac_control->fifos[queue];
  7825. - if (do_spin_lock)
  7826. - spin_lock_irqsave(&fifo->tx_lock, flags);
  7827. - else {
  7828. - if (unlikely(!spin_trylock_irqsave(&fifo->tx_lock, flags)))
  7829. - return NETDEV_TX_LOCKED;
  7830. - }
  7831. + spin_lock_irqsave(&fifo->tx_lock, flags);
  7832. if (sp->config.multiq) {
  7833. if (__netif_subqueue_stopped(dev, fifo->fifo_no)) {
  7834. diff -Nur linux-4.1.20.orig/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c linux-4.1.20/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c
  7835. --- linux-4.1.20.orig/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c 2016-03-17 19:11:03.000000000 +0100
  7836. +++ linux-4.1.20/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c 2016-03-21 20:18:32.000000000 +0100
  7837. @@ -2137,10 +2137,8 @@
  7838. struct pch_gbe_tx_ring *tx_ring = adapter->tx_ring;
  7839. unsigned long flags;
  7840. - if (!spin_trylock_irqsave(&tx_ring->tx_lock, flags)) {
  7841. - /* Collision - tell upper layer to requeue */
  7842. - return NETDEV_TX_LOCKED;
  7843. - }
  7844. + spin_lock_irqsave(&tx_ring->tx_lock, flags);
  7845. +
  7846. if (unlikely(!PCH_GBE_DESC_UNUSED(tx_ring))) {
  7847. netif_stop_queue(netdev);
  7848. spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
  7849. diff -Nur linux-4.1.20.orig/drivers/net/ethernet/realtek/8139too.c linux-4.1.20/drivers/net/ethernet/realtek/8139too.c
  7850. --- linux-4.1.20.orig/drivers/net/ethernet/realtek/8139too.c 2016-03-17 19:11:03.000000000 +0100
  7851. +++ linux-4.1.20/drivers/net/ethernet/realtek/8139too.c 2016-03-21 20:18:32.000000000 +0100
  7852. @@ -2229,7 +2229,7 @@
  7853. struct rtl8139_private *tp = netdev_priv(dev);
  7854. const int irq = tp->pci_dev->irq;
  7855. - disable_irq(irq);
  7856. + disable_irq_nosync(irq);
  7857. rtl8139_interrupt(irq, dev);
  7858. enable_irq(irq);
  7859. }
  7860. diff -Nur linux-4.1.20.orig/drivers/net/ethernet/tehuti/tehuti.c linux-4.1.20/drivers/net/ethernet/tehuti/tehuti.c
  7861. --- linux-4.1.20.orig/drivers/net/ethernet/tehuti/tehuti.c 2016-03-17 19:11:03.000000000 +0100
  7862. +++ linux-4.1.20/drivers/net/ethernet/tehuti/tehuti.c 2016-03-21 20:18:32.000000000 +0100
  7863. @@ -1629,13 +1629,8 @@
  7864. unsigned long flags;
  7865. ENTER;
  7866. - local_irq_save(flags);
  7867. - if (!spin_trylock(&priv->tx_lock)) {
  7868. - local_irq_restore(flags);
  7869. - DBG("%s[%s]: TX locked, returning NETDEV_TX_LOCKED\n",
  7870. - BDX_DRV_NAME, ndev->name);
  7871. - return NETDEV_TX_LOCKED;
  7872. - }
  7873. +
  7874. + spin_lock_irqsave(&priv->tx_lock, flags);
  7875. /* build tx descriptor */
  7876. BDX_ASSERT(f->m.wptr >= f->m.memsz); /* started with valid wptr */
  7877. diff -Nur linux-4.1.20.orig/drivers/net/rionet.c linux-4.1.20/drivers/net/rionet.c
  7878. --- linux-4.1.20.orig/drivers/net/rionet.c 2016-03-17 19:11:03.000000000 +0100
  7879. +++ linux-4.1.20/drivers/net/rionet.c 2016-03-21 20:18:32.000000000 +0100
  7880. @@ -174,11 +174,7 @@
  7881. unsigned long flags;
  7882. int add_num = 1;
  7883. - local_irq_save(flags);
  7884. - if (!spin_trylock(&rnet->tx_lock)) {
  7885. - local_irq_restore(flags);
  7886. - return NETDEV_TX_LOCKED;
  7887. - }
  7888. + spin_lock_irqsave(&rnet->tx_lock, flags);
  7889. if (is_multicast_ether_addr(eth->h_dest))
  7890. add_num = nets[rnet->mport->id].nact;
  7891. diff -Nur linux-4.1.20.orig/drivers/net/wireless/orinoco/orinoco_usb.c linux-4.1.20/drivers/net/wireless/orinoco/orinoco_usb.c
  7892. --- linux-4.1.20.orig/drivers/net/wireless/orinoco/orinoco_usb.c 2016-03-17 19:11:03.000000000 +0100
  7893. +++ linux-4.1.20/drivers/net/wireless/orinoco/orinoco_usb.c 2016-03-21 20:18:32.000000000 +0100
  7894. @@ -697,7 +697,7 @@
  7895. while (!ctx->done.done && msecs--)
  7896. udelay(1000);
  7897. } else {
  7898. - wait_event_interruptible(ctx->done.wait,
  7899. + swait_event_interruptible(ctx->done.wait,
  7900. ctx->done.done);
  7901. }
  7902. break;
  7903. diff -Nur linux-4.1.20.orig/drivers/pci/access.c linux-4.1.20/drivers/pci/access.c
  7904. --- linux-4.1.20.orig/drivers/pci/access.c 2016-03-17 19:11:03.000000000 +0100
  7905. +++ linux-4.1.20/drivers/pci/access.c 2016-03-21 20:18:32.000000000 +0100
  7906. @@ -561,7 +561,7 @@
  7907. WARN_ON(!dev->block_cfg_access);
  7908. dev->block_cfg_access = 0;
  7909. - wake_up_all(&pci_cfg_wait);
  7910. + wake_up_all_locked(&pci_cfg_wait);
  7911. raw_spin_unlock_irqrestore(&pci_lock, flags);
  7912. }
  7913. EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
  7914. diff -Nur linux-4.1.20.orig/drivers/scsi/fcoe/fcoe.c linux-4.1.20/drivers/scsi/fcoe/fcoe.c
  7915. --- linux-4.1.20.orig/drivers/scsi/fcoe/fcoe.c 2016-03-17 19:11:03.000000000 +0100
  7916. +++ linux-4.1.20/drivers/scsi/fcoe/fcoe.c 2016-03-21 20:18:32.000000000 +0100
  7917. @@ -1287,7 +1287,7 @@
  7918. struct sk_buff *skb;
  7919. #ifdef CONFIG_SMP
  7920. struct fcoe_percpu_s *p0;
  7921. - unsigned targ_cpu = get_cpu();
  7922. + unsigned targ_cpu = get_cpu_light();
  7923. #endif /* CONFIG_SMP */
  7924. FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
  7925. @@ -1343,7 +1343,7 @@
  7926. kfree_skb(skb);
  7927. spin_unlock_bh(&p->fcoe_rx_list.lock);
  7928. }
  7929. - put_cpu();
  7930. + put_cpu_light();
  7931. #else
  7932. /*
  7933. * This a non-SMP scenario where the singular Rx thread is
  7934. @@ -1567,11 +1567,11 @@
  7935. static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
  7936. {
  7937. struct fcoe_percpu_s *fps;
  7938. - int rc;
  7939. + int rc, cpu = get_cpu_light();
  7940. - fps = &get_cpu_var(fcoe_percpu);
  7941. + fps = &per_cpu(fcoe_percpu, cpu);
  7942. rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
  7943. - put_cpu_var(fcoe_percpu);
  7944. + put_cpu_light();
  7945. return rc;
  7946. }
  7947. @@ -1767,11 +1767,11 @@
  7948. return 0;
  7949. }
  7950. - stats = per_cpu_ptr(lport->stats, get_cpu());
  7951. + stats = per_cpu_ptr(lport->stats, get_cpu_light());
  7952. stats->InvalidCRCCount++;
  7953. if (stats->InvalidCRCCount < 5)
  7954. printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
  7955. - put_cpu();
  7956. + put_cpu_light();
  7957. return -EINVAL;
  7958. }
  7959. @@ -1847,13 +1847,13 @@
  7960. goto drop;
  7961. if (!fcoe_filter_frames(lport, fp)) {
  7962. - put_cpu();
  7963. + put_cpu_light();
  7964. fc_exch_recv(lport, fp);
  7965. return;
  7966. }
  7967. drop:
  7968. stats->ErrorFrames++;
  7969. - put_cpu();
  7970. + put_cpu_light();
  7971. kfree_skb(skb);
  7972. }
  7973. diff -Nur linux-4.1.20.orig/drivers/scsi/fcoe/fcoe_ctlr.c linux-4.1.20/drivers/scsi/fcoe/fcoe_ctlr.c
  7974. --- linux-4.1.20.orig/drivers/scsi/fcoe/fcoe_ctlr.c 2016-03-17 19:11:03.000000000 +0100
  7975. +++ linux-4.1.20/drivers/scsi/fcoe/fcoe_ctlr.c 2016-03-21 20:18:32.000000000 +0100
  7976. @@ -831,7 +831,7 @@
  7977. INIT_LIST_HEAD(&del_list);
  7978. - stats = per_cpu_ptr(fip->lp->stats, get_cpu());
  7979. + stats = per_cpu_ptr(fip->lp->stats, get_cpu_light());
  7980. list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
  7981. deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
  7982. @@ -867,7 +867,7 @@
  7983. sel_time = fcf->time;
  7984. }
  7985. }
  7986. - put_cpu();
  7987. + put_cpu_light();
  7988. list_for_each_entry_safe(fcf, next, &del_list, list) {
  7989. /* Removes fcf from current list */
  7990. diff -Nur linux-4.1.20.orig/drivers/scsi/libfc/fc_exch.c linux-4.1.20/drivers/scsi/libfc/fc_exch.c
  7991. --- linux-4.1.20.orig/drivers/scsi/libfc/fc_exch.c 2016-03-17 19:11:03.000000000 +0100
  7992. +++ linux-4.1.20/drivers/scsi/libfc/fc_exch.c 2016-03-21 20:18:32.000000000 +0100
  7993. @@ -814,10 +814,10 @@
  7994. }
  7995. memset(ep, 0, sizeof(*ep));
  7996. - cpu = get_cpu();
  7997. + cpu = get_cpu_light();
  7998. pool = per_cpu_ptr(mp->pool, cpu);
  7999. spin_lock_bh(&pool->lock);
  8000. - put_cpu();
  8001. + put_cpu_light();
  8002. /* peek cache of free slot */
  8003. if (pool->left != FC_XID_UNKNOWN) {
  8004. diff -Nur linux-4.1.20.orig/drivers/scsi/libsas/sas_ata.c linux-4.1.20/drivers/scsi/libsas/sas_ata.c
  8005. --- linux-4.1.20.orig/drivers/scsi/libsas/sas_ata.c 2016-03-17 19:11:03.000000000 +0100
  8006. +++ linux-4.1.20/drivers/scsi/libsas/sas_ata.c 2016-03-21 20:18:32.000000000 +0100
  8007. @@ -190,7 +190,7 @@
  8008. /* TODO: audit callers to ensure they are ready for qc_issue to
  8009. * unconditionally re-enable interrupts
  8010. */
  8011. - local_irq_save(flags);
  8012. + local_irq_save_nort(flags);
  8013. spin_unlock(ap->lock);
  8014. /* If the device fell off, no sense in issuing commands */
  8015. @@ -255,7 +255,7 @@
  8016. out:
  8017. spin_lock(ap->lock);
  8018. - local_irq_restore(flags);
  8019. + local_irq_restore_nort(flags);
  8020. return ret;
  8021. }
  8022. diff -Nur linux-4.1.20.orig/drivers/scsi/qla2xxx/qla_inline.h linux-4.1.20/drivers/scsi/qla2xxx/qla_inline.h
  8023. --- linux-4.1.20.orig/drivers/scsi/qla2xxx/qla_inline.h 2016-03-17 19:11:03.000000000 +0100
  8024. +++ linux-4.1.20/drivers/scsi/qla2xxx/qla_inline.h 2016-03-21 20:18:32.000000000 +0100
  8025. @@ -59,12 +59,12 @@
  8026. {
  8027. unsigned long flags;
  8028. struct qla_hw_data *ha = rsp->hw;
  8029. - local_irq_save(flags);
  8030. + local_irq_save_nort(flags);
  8031. if (IS_P3P_TYPE(ha))
  8032. qla82xx_poll(0, rsp);
  8033. else
  8034. ha->isp_ops->intr_handler(0, rsp);
  8035. - local_irq_restore(flags);
  8036. + local_irq_restore_nort(flags);
  8037. }
  8038. static inline uint8_t *
  8039. diff -Nur linux-4.1.20.orig/drivers/thermal/x86_pkg_temp_thermal.c linux-4.1.20/drivers/thermal/x86_pkg_temp_thermal.c
  8040. --- linux-4.1.20.orig/drivers/thermal/x86_pkg_temp_thermal.c 2016-03-17 19:11:03.000000000 +0100
  8041. +++ linux-4.1.20/drivers/thermal/x86_pkg_temp_thermal.c 2016-03-21 20:18:32.000000000 +0100
  8042. @@ -29,6 +29,7 @@
  8043. #include <linux/pm.h>
  8044. #include <linux/thermal.h>
  8045. #include <linux/debugfs.h>
  8046. +#include <linux/work-simple.h>
  8047. #include <asm/cpu_device_id.h>
  8048. #include <asm/mce.h>
  8049. @@ -352,7 +353,7 @@
  8050. }
  8051. }
  8052. -static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  8053. +static void platform_thermal_notify_work(struct swork_event *event)
  8054. {
  8055. unsigned long flags;
  8056. int cpu = smp_processor_id();
  8057. @@ -369,7 +370,7 @@
  8058. pkg_work_scheduled[phy_id]) {
  8059. disable_pkg_thres_interrupt();
  8060. spin_unlock_irqrestore(&pkg_work_lock, flags);
  8061. - return -EINVAL;
  8062. + return;
  8063. }
  8064. pkg_work_scheduled[phy_id] = 1;
  8065. spin_unlock_irqrestore(&pkg_work_lock, flags);
  8066. @@ -378,9 +379,48 @@
  8067. schedule_delayed_work_on(cpu,
  8068. &per_cpu(pkg_temp_thermal_threshold_work, cpu),
  8069. msecs_to_jiffies(notify_delay_ms));
  8070. +}
  8071. +
  8072. +#ifdef CONFIG_PREEMPT_RT_FULL
  8073. +static struct swork_event notify_work;
  8074. +
  8075. +static int thermal_notify_work_init(void)
  8076. +{
  8077. + int err;
  8078. +
  8079. + err = swork_get();
  8080. + if (err)
  8081. + return err;
  8082. +
  8083. + INIT_SWORK(&notify_work, platform_thermal_notify_work);
  8084. return 0;
  8085. }
  8086. +static void thermal_notify_work_cleanup(void)
  8087. +{
  8088. + swork_put();
  8089. +}
  8090. +
  8091. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  8092. +{
  8093. + swork_queue(&notify_work);
  8094. + return 0;
  8095. +}
  8096. +
  8097. +#else /* !CONFIG_PREEMPT_RT_FULL */
  8098. +
  8099. +static int thermal_notify_work_init(void) { return 0; }
  8100. +
  8101. +static void thermal_notify_work_cleanup(void) { }
  8102. +
  8103. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  8104. +{
  8105. + platform_thermal_notify_work(NULL);
  8106. +
  8107. + return 0;
  8108. +}
  8109. +#endif /* CONFIG_PREEMPT_RT_FULL */
  8110. +
  8111. static int find_siblings_cpu(int cpu)
  8112. {
  8113. int i;
  8114. @@ -584,6 +624,9 @@
  8115. if (!x86_match_cpu(pkg_temp_thermal_ids))
  8116. return -ENODEV;
  8117. + if (!thermal_notify_work_init())
  8118. + return -ENODEV;
  8119. +
  8120. spin_lock_init(&pkg_work_lock);
  8121. platform_thermal_package_notify =
  8122. pkg_temp_thermal_platform_thermal_notify;
  8123. @@ -608,7 +651,7 @@
  8124. kfree(pkg_work_scheduled);
  8125. platform_thermal_package_notify = NULL;
  8126. platform_thermal_package_rate_control = NULL;
  8127. -
  8128. + thermal_notify_work_cleanup();
  8129. return -ENODEV;
  8130. }
  8131. @@ -633,6 +676,7 @@
  8132. mutex_unlock(&phy_dev_list_mutex);
  8133. platform_thermal_package_notify = NULL;
  8134. platform_thermal_package_rate_control = NULL;
  8135. + thermal_notify_work_cleanup();
  8136. for_each_online_cpu(i)
  8137. cancel_delayed_work_sync(
  8138. &per_cpu(pkg_temp_thermal_threshold_work, i));
  8139. diff -Nur linux-4.1.20.orig/drivers/tty/serial/8250/8250_core.c linux-4.1.20/drivers/tty/serial/8250/8250_core.c
  8140. --- linux-4.1.20.orig/drivers/tty/serial/8250/8250_core.c 2016-03-17 19:11:03.000000000 +0100
  8141. +++ linux-4.1.20/drivers/tty/serial/8250/8250_core.c 2016-03-21 20:18:32.000000000 +0100
  8142. @@ -36,6 +36,7 @@
  8143. #include <linux/nmi.h>
  8144. #include <linux/mutex.h>
  8145. #include <linux/slab.h>
  8146. +#include <linux/kdb.h>
  8147. #include <linux/uaccess.h>
  8148. #include <linux/pm_runtime.h>
  8149. #ifdef CONFIG_SPARC
  8150. @@ -80,7 +81,16 @@
  8151. #define DEBUG_INTR(fmt...) do { } while (0)
  8152. #endif
  8153. -#define PASS_LIMIT 512
  8154. +/*
  8155. + * On -rt we can have a more delays, and legitimately
  8156. + * so - so don't drop work spuriously and spam the
  8157. + * syslog:
  8158. + */
  8159. +#ifdef CONFIG_PREEMPT_RT_FULL
  8160. +# define PASS_LIMIT 1000000
  8161. +#else
  8162. +# define PASS_LIMIT 512
  8163. +#endif
  8164. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  8165. @@ -3372,7 +3382,7 @@
  8166. if (port->sysrq)
  8167. locked = 0;
  8168. - else if (oops_in_progress)
  8169. + else if (oops_in_progress || in_kdb_printk())
  8170. locked = spin_trylock_irqsave(&port->lock, flags);
  8171. else
  8172. spin_lock_irqsave(&port->lock, flags);
  8173. diff -Nur linux-4.1.20.orig/drivers/tty/serial/amba-pl011.c linux-4.1.20/drivers/tty/serial/amba-pl011.c
  8174. --- linux-4.1.20.orig/drivers/tty/serial/amba-pl011.c 2016-03-17 19:11:03.000000000 +0100
  8175. +++ linux-4.1.20/drivers/tty/serial/amba-pl011.c 2016-03-21 20:18:32.000000000 +0100
  8176. @@ -2000,13 +2000,19 @@
  8177. clk_enable(uap->clk);
  8178. - local_irq_save(flags);
  8179. + /*
  8180. + * local_irq_save(flags);
  8181. + *
  8182. + * This local_irq_save() is nonsense. If we come in via sysrq
  8183. + * handling then interrupts are already disabled. Aside of
  8184. + * that the port.sysrq check is racy on SMP regardless.
  8185. + */
  8186. if (uap->port.sysrq)
  8187. locked = 0;
  8188. else if (oops_in_progress)
  8189. - locked = spin_trylock(&uap->port.lock);
  8190. + locked = spin_trylock_irqsave(&uap->port.lock, flags);
  8191. else
  8192. - spin_lock(&uap->port.lock);
  8193. + spin_lock_irqsave(&uap->port.lock, flags);
  8194. /*
  8195. * First save the CR then disable the interrupts
  8196. @@ -2028,8 +2034,7 @@
  8197. writew(old_cr, uap->port.membase + UART011_CR);
  8198. if (locked)
  8199. - spin_unlock(&uap->port.lock);
  8200. - local_irq_restore(flags);
  8201. + spin_unlock_irqrestore(&uap->port.lock, flags);
  8202. clk_disable(uap->clk);
  8203. }
  8204. diff -Nur linux-4.1.20.orig/drivers/tty/serial/omap-serial.c linux-4.1.20/drivers/tty/serial/omap-serial.c
  8205. --- linux-4.1.20.orig/drivers/tty/serial/omap-serial.c 2016-03-17 19:11:03.000000000 +0100
  8206. +++ linux-4.1.20/drivers/tty/serial/omap-serial.c 2016-03-21 20:18:32.000000000 +0100
  8207. @@ -1282,13 +1282,10 @@
  8208. pm_runtime_get_sync(up->dev);
  8209. - local_irq_save(flags);
  8210. - if (up->port.sysrq)
  8211. - locked = 0;
  8212. - else if (oops_in_progress)
  8213. - locked = spin_trylock(&up->port.lock);
  8214. + if (up->port.sysrq || oops_in_progress)
  8215. + locked = spin_trylock_irqsave(&up->port.lock, flags);
  8216. else
  8217. - spin_lock(&up->port.lock);
  8218. + spin_lock_irqsave(&up->port.lock, flags);
  8219. /*
  8220. * First save the IER then disable the interrupts
  8221. @@ -1317,8 +1314,7 @@
  8222. pm_runtime_mark_last_busy(up->dev);
  8223. pm_runtime_put_autosuspend(up->dev);
  8224. if (locked)
  8225. - spin_unlock(&up->port.lock);
  8226. - local_irq_restore(flags);
  8227. + spin_unlock_irqrestore(&up->port.lock, flags);
  8228. }
  8229. static int __init
  8230. diff -Nur linux-4.1.20.orig/drivers/usb/core/hcd.c linux-4.1.20/drivers/usb/core/hcd.c
  8231. --- linux-4.1.20.orig/drivers/usb/core/hcd.c 2016-03-17 19:11:03.000000000 +0100
  8232. +++ linux-4.1.20/drivers/usb/core/hcd.c 2016-03-21 20:18:32.000000000 +0100
  8233. @@ -1684,9 +1684,9 @@
  8234. * and no one may trigger the above deadlock situation when
  8235. * running complete() in tasklet.
  8236. */
  8237. - local_irq_save(flags);
  8238. + local_irq_save_nort(flags);
  8239. urb->complete(urb);
  8240. - local_irq_restore(flags);
  8241. + local_irq_restore_nort(flags);
  8242. usb_anchor_resume_wakeups(anchor);
  8243. atomic_dec(&urb->use_count);
  8244. diff -Nur linux-4.1.20.orig/drivers/usb/gadget/function/f_fs.c linux-4.1.20/drivers/usb/gadget/function/f_fs.c
  8245. --- linux-4.1.20.orig/drivers/usb/gadget/function/f_fs.c 2016-03-17 19:11:03.000000000 +0100
  8246. +++ linux-4.1.20/drivers/usb/gadget/function/f_fs.c 2016-03-21 20:18:32.000000000 +0100
  8247. @@ -1405,7 +1405,7 @@
  8248. pr_info("%s(): freeing\n", __func__);
  8249. ffs_data_clear(ffs);
  8250. BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
  8251. - waitqueue_active(&ffs->ep0req_completion.wait));
  8252. + swaitqueue_active(&ffs->ep0req_completion.wait));
  8253. kfree(ffs->dev_name);
  8254. kfree(ffs);
  8255. }
  8256. diff -Nur linux-4.1.20.orig/drivers/usb/gadget/legacy/inode.c linux-4.1.20/drivers/usb/gadget/legacy/inode.c
  8257. --- linux-4.1.20.orig/drivers/usb/gadget/legacy/inode.c 2016-03-17 19:11:03.000000000 +0100
  8258. +++ linux-4.1.20/drivers/usb/gadget/legacy/inode.c 2016-03-21 20:18:32.000000000 +0100
  8259. @@ -345,7 +345,7 @@
  8260. spin_unlock_irq (&epdata->dev->lock);
  8261. if (likely (value == 0)) {
  8262. - value = wait_event_interruptible (done.wait, done.done);
  8263. + value = swait_event_interruptible (done.wait, done.done);
  8264. if (value != 0) {
  8265. spin_lock_irq (&epdata->dev->lock);
  8266. if (likely (epdata->ep != NULL)) {
  8267. @@ -354,7 +354,7 @@
  8268. usb_ep_dequeue (epdata->ep, epdata->req);
  8269. spin_unlock_irq (&epdata->dev->lock);
  8270. - wait_event (done.wait, done.done);
  8271. + swait_event (done.wait, done.done);
  8272. if (epdata->status == -ECONNRESET)
  8273. epdata->status = -EINTR;
  8274. } else {
  8275. diff -Nur linux-4.1.20.orig/fs/aio.c linux-4.1.20/fs/aio.c
  8276. --- linux-4.1.20.orig/fs/aio.c 2016-03-17 19:11:03.000000000 +0100
  8277. +++ linux-4.1.20/fs/aio.c 2016-03-21 20:18:32.000000000 +0100
  8278. @@ -40,6 +40,7 @@
  8279. #include <linux/ramfs.h>
  8280. #include <linux/percpu-refcount.h>
  8281. #include <linux/mount.h>
  8282. +#include <linux/work-simple.h>
  8283. #include <asm/kmap_types.h>
  8284. #include <asm/uaccess.h>
  8285. @@ -115,7 +116,7 @@
  8286. struct page **ring_pages;
  8287. long nr_pages;
  8288. - struct work_struct free_work;
  8289. + struct swork_event free_work;
  8290. /*
  8291. * signals when all in-flight requests are done
  8292. @@ -253,6 +254,7 @@
  8293. .mount = aio_mount,
  8294. .kill_sb = kill_anon_super,
  8295. };
  8296. + BUG_ON(swork_get());
  8297. aio_mnt = kern_mount(&aio_fs);
  8298. if (IS_ERR(aio_mnt))
  8299. panic("Failed to create aio fs mount.");
  8300. @@ -559,9 +561,9 @@
  8301. return cancel(&kiocb->common);
  8302. }
  8303. -static void free_ioctx(struct work_struct *work)
  8304. +static void free_ioctx(struct swork_event *sev)
  8305. {
  8306. - struct kioctx *ctx = container_of(work, struct kioctx, free_work);
  8307. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  8308. pr_debug("freeing %p\n", ctx);
  8309. @@ -580,8 +582,8 @@
  8310. if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
  8311. complete(&ctx->rq_wait->comp);
  8312. - INIT_WORK(&ctx->free_work, free_ioctx);
  8313. - schedule_work(&ctx->free_work);
  8314. + INIT_SWORK(&ctx->free_work, free_ioctx);
  8315. + swork_queue(&ctx->free_work);
  8316. }
  8317. /*
  8318. @@ -589,9 +591,9 @@
  8319. * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
  8320. * now it's safe to cancel any that need to be.
  8321. */
  8322. -static void free_ioctx_users(struct percpu_ref *ref)
  8323. +static void free_ioctx_users_work(struct swork_event *sev)
  8324. {
  8325. - struct kioctx *ctx = container_of(ref, struct kioctx, users);
  8326. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  8327. struct aio_kiocb *req;
  8328. spin_lock_irq(&ctx->ctx_lock);
  8329. @@ -610,6 +612,14 @@
  8330. percpu_ref_put(&ctx->reqs);
  8331. }
  8332. +static void free_ioctx_users(struct percpu_ref *ref)
  8333. +{
  8334. + struct kioctx *ctx = container_of(ref, struct kioctx, users);
  8335. +
  8336. + INIT_SWORK(&ctx->free_work, free_ioctx_users_work);
  8337. + swork_queue(&ctx->free_work);
  8338. +}
  8339. +
  8340. static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
  8341. {
  8342. unsigned i, new_nr;
  8343. diff -Nur linux-4.1.20.orig/fs/autofs4/autofs_i.h linux-4.1.20/fs/autofs4/autofs_i.h
  8344. --- linux-4.1.20.orig/fs/autofs4/autofs_i.h 2016-03-17 19:11:03.000000000 +0100
  8345. +++ linux-4.1.20/fs/autofs4/autofs_i.h 2016-03-21 20:18:32.000000000 +0100
  8346. @@ -34,6 +34,7 @@
  8347. #include <linux/sched.h>
  8348. #include <linux/mount.h>
  8349. #include <linux/namei.h>
  8350. +#include <linux/delay.h>
  8351. #include <asm/current.h>
  8352. #include <asm/uaccess.h>
  8353. diff -Nur linux-4.1.20.orig/fs/autofs4/expire.c linux-4.1.20/fs/autofs4/expire.c
  8354. --- linux-4.1.20.orig/fs/autofs4/expire.c 2016-03-17 19:11:03.000000000 +0100
  8355. +++ linux-4.1.20/fs/autofs4/expire.c 2016-03-21 20:18:32.000000000 +0100
  8356. @@ -150,7 +150,7 @@
  8357. parent = p->d_parent;
  8358. if (!spin_trylock(&parent->d_lock)) {
  8359. spin_unlock(&p->d_lock);
  8360. - cpu_relax();
  8361. + cpu_chill();
  8362. goto relock;
  8363. }
  8364. spin_unlock(&p->d_lock);
  8365. diff -Nur linux-4.1.20.orig/fs/buffer.c linux-4.1.20/fs/buffer.c
  8366. --- linux-4.1.20.orig/fs/buffer.c 2016-03-17 19:11:03.000000000 +0100
  8367. +++ linux-4.1.20/fs/buffer.c 2016-03-21 20:18:32.000000000 +0100
  8368. @@ -301,8 +301,7 @@
  8369. * decide that the page is now completely done.
  8370. */
  8371. first = page_buffers(page);
  8372. - local_irq_save(flags);
  8373. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  8374. + flags = bh_uptodate_lock_irqsave(first);
  8375. clear_buffer_async_read(bh);
  8376. unlock_buffer(bh);
  8377. tmp = bh;
  8378. @@ -315,8 +314,7 @@
  8379. }
  8380. tmp = tmp->b_this_page;
  8381. } while (tmp != bh);
  8382. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  8383. - local_irq_restore(flags);
  8384. + bh_uptodate_unlock_irqrestore(first, flags);
  8385. /*
  8386. * If none of the buffers had errors and they are all
  8387. @@ -328,9 +326,7 @@
  8388. return;
  8389. still_busy:
  8390. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  8391. - local_irq_restore(flags);
  8392. - return;
  8393. + bh_uptodate_unlock_irqrestore(first, flags);
  8394. }
  8395. /*
  8396. @@ -358,8 +354,7 @@
  8397. }
  8398. first = page_buffers(page);
  8399. - local_irq_save(flags);
  8400. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  8401. + flags = bh_uptodate_lock_irqsave(first);
  8402. clear_buffer_async_write(bh);
  8403. unlock_buffer(bh);
  8404. @@ -371,15 +366,12 @@
  8405. }
  8406. tmp = tmp->b_this_page;
  8407. }
  8408. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  8409. - local_irq_restore(flags);
  8410. + bh_uptodate_unlock_irqrestore(first, flags);
  8411. end_page_writeback(page);
  8412. return;
  8413. still_busy:
  8414. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  8415. - local_irq_restore(flags);
  8416. - return;
  8417. + bh_uptodate_unlock_irqrestore(first, flags);
  8418. }
  8419. EXPORT_SYMBOL(end_buffer_async_write);
  8420. @@ -3325,6 +3317,7 @@
  8421. struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
  8422. if (ret) {
  8423. INIT_LIST_HEAD(&ret->b_assoc_buffers);
  8424. + buffer_head_init_locks(ret);
  8425. preempt_disable();
  8426. __this_cpu_inc(bh_accounting.nr);
  8427. recalc_bh_state();
  8428. diff -Nur linux-4.1.20.orig/fs/dcache.c linux-4.1.20/fs/dcache.c
  8429. --- linux-4.1.20.orig/fs/dcache.c 2016-03-17 19:11:03.000000000 +0100
  8430. +++ linux-4.1.20/fs/dcache.c 2016-03-21 20:18:32.000000000 +0100
  8431. @@ -19,6 +19,7 @@
  8432. #include <linux/mm.h>
  8433. #include <linux/fs.h>
  8434. #include <linux/fsnotify.h>
  8435. +#include <linux/delay.h>
  8436. #include <linux/slab.h>
  8437. #include <linux/init.h>
  8438. #include <linux/hash.h>
  8439. @@ -578,7 +579,7 @@
  8440. failed:
  8441. spin_unlock(&dentry->d_lock);
  8442. - cpu_relax();
  8443. + cpu_chill();
  8444. return dentry; /* try again with same dentry */
  8445. }
  8446. @@ -2388,7 +2389,7 @@
  8447. if (dentry->d_lockref.count == 1) {
  8448. if (!spin_trylock(&inode->i_lock)) {
  8449. spin_unlock(&dentry->d_lock);
  8450. - cpu_relax();
  8451. + cpu_chill();
  8452. goto again;
  8453. }
  8454. dentry->d_flags &= ~DCACHE_CANT_MOUNT;
  8455. diff -Nur linux-4.1.20.orig/fs/eventpoll.c linux-4.1.20/fs/eventpoll.c
  8456. --- linux-4.1.20.orig/fs/eventpoll.c 2016-03-17 19:11:03.000000000 +0100
  8457. +++ linux-4.1.20/fs/eventpoll.c 2016-03-21 20:18:32.000000000 +0100
  8458. @@ -505,12 +505,12 @@
  8459. */
  8460. static void ep_poll_safewake(wait_queue_head_t *wq)
  8461. {
  8462. - int this_cpu = get_cpu();
  8463. + int this_cpu = get_cpu_light();
  8464. ep_call_nested(&poll_safewake_ncalls, EP_MAX_NESTS,
  8465. ep_poll_wakeup_proc, NULL, wq, (void *) (long) this_cpu);
  8466. - put_cpu();
  8467. + put_cpu_light();
  8468. }
  8469. static void ep_remove_wait_queue(struct eppoll_entry *pwq)
  8470. diff -Nur linux-4.1.20.orig/fs/exec.c linux-4.1.20/fs/exec.c
  8471. --- linux-4.1.20.orig/fs/exec.c 2016-03-17 19:11:03.000000000 +0100
  8472. +++ linux-4.1.20/fs/exec.c 2016-03-21 20:18:32.000000000 +0100
  8473. @@ -859,12 +859,14 @@
  8474. }
  8475. }
  8476. task_lock(tsk);
  8477. + preempt_disable_rt();
  8478. active_mm = tsk->active_mm;
  8479. tsk->mm = mm;
  8480. tsk->active_mm = mm;
  8481. activate_mm(active_mm, mm);
  8482. tsk->mm->vmacache_seqnum = 0;
  8483. vmacache_flush(tsk);
  8484. + preempt_enable_rt();
  8485. task_unlock(tsk);
  8486. if (old_mm) {
  8487. up_read(&old_mm->mmap_sem);
  8488. diff -Nur linux-4.1.20.orig/fs/f2fs/f2fs.h linux-4.1.20/fs/f2fs/f2fs.h
  8489. --- linux-4.1.20.orig/fs/f2fs/f2fs.h 2016-03-17 19:11:03.000000000 +0100
  8490. +++ linux-4.1.20/fs/f2fs/f2fs.h 2016-03-21 20:18:32.000000000 +0100
  8491. @@ -22,7 +22,6 @@
  8492. #ifdef CONFIG_F2FS_CHECK_FS
  8493. #define f2fs_bug_on(sbi, condition) BUG_ON(condition)
  8494. -#define f2fs_down_write(x, y) down_write_nest_lock(x, y)
  8495. #else
  8496. #define f2fs_bug_on(sbi, condition) \
  8497. do { \
  8498. @@ -31,7 +30,6 @@
  8499. set_sbi_flag(sbi, SBI_NEED_FSCK); \
  8500. } \
  8501. } while (0)
  8502. -#define f2fs_down_write(x, y) down_write(x)
  8503. #endif
  8504. /*
  8505. @@ -838,7 +836,7 @@
  8506. static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
  8507. {
  8508. - f2fs_down_write(&sbi->cp_rwsem, &sbi->cp_mutex);
  8509. + down_write(&sbi->cp_rwsem);
  8510. }
  8511. static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
  8512. diff -Nur linux-4.1.20.orig/fs/jbd/checkpoint.c linux-4.1.20/fs/jbd/checkpoint.c
  8513. --- linux-4.1.20.orig/fs/jbd/checkpoint.c 2016-03-17 19:11:03.000000000 +0100
  8514. +++ linux-4.1.20/fs/jbd/checkpoint.c 2016-03-21 20:18:32.000000000 +0100
  8515. @@ -129,6 +129,8 @@
  8516. if (journal->j_flags & JFS_ABORT)
  8517. return;
  8518. spin_unlock(&journal->j_state_lock);
  8519. + if (current->plug)
  8520. + io_schedule();
  8521. mutex_lock(&journal->j_checkpoint_mutex);
  8522. /*
  8523. diff -Nur linux-4.1.20.orig/fs/jbd2/checkpoint.c linux-4.1.20/fs/jbd2/checkpoint.c
  8524. --- linux-4.1.20.orig/fs/jbd2/checkpoint.c 2016-03-17 19:11:03.000000000 +0100
  8525. +++ linux-4.1.20/fs/jbd2/checkpoint.c 2016-03-21 20:18:32.000000000 +0100
  8526. @@ -116,6 +116,8 @@
  8527. nblocks = jbd2_space_needed(journal);
  8528. while (jbd2_log_space_left(journal) < nblocks) {
  8529. write_unlock(&journal->j_state_lock);
  8530. + if (current->plug)
  8531. + io_schedule();
  8532. mutex_lock(&journal->j_checkpoint_mutex);
  8533. /*
  8534. diff -Nur linux-4.1.20.orig/fs/namespace.c linux-4.1.20/fs/namespace.c
  8535. --- linux-4.1.20.orig/fs/namespace.c 2016-03-17 19:11:03.000000000 +0100
  8536. +++ linux-4.1.20/fs/namespace.c 2016-03-21 20:18:32.000000000 +0100
  8537. @@ -14,6 +14,7 @@
  8538. #include <linux/mnt_namespace.h>
  8539. #include <linux/user_namespace.h>
  8540. #include <linux/namei.h>
  8541. +#include <linux/delay.h>
  8542. #include <linux/security.h>
  8543. #include <linux/idr.h>
  8544. #include <linux/init.h> /* init_rootfs */
  8545. @@ -353,8 +354,11 @@
  8546. * incremented count after it has set MNT_WRITE_HOLD.
  8547. */
  8548. smp_mb();
  8549. - while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
  8550. - cpu_relax();
  8551. + while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
  8552. + preempt_enable();
  8553. + cpu_chill();
  8554. + preempt_disable();
  8555. + }
  8556. /*
  8557. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  8558. * be set to match its requirements. So we must not load that until
  8559. diff -Nur linux-4.1.20.orig/fs/ntfs/aops.c linux-4.1.20/fs/ntfs/aops.c
  8560. --- linux-4.1.20.orig/fs/ntfs/aops.c 2016-03-17 19:11:03.000000000 +0100
  8561. +++ linux-4.1.20/fs/ntfs/aops.c 2016-03-21 20:18:32.000000000 +0100
  8562. @@ -107,8 +107,7 @@
  8563. "0x%llx.", (unsigned long long)bh->b_blocknr);
  8564. }
  8565. first = page_buffers(page);
  8566. - local_irq_save(flags);
  8567. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  8568. + flags = bh_uptodate_lock_irqsave(first);
  8569. clear_buffer_async_read(bh);
  8570. unlock_buffer(bh);
  8571. tmp = bh;
  8572. @@ -123,8 +122,7 @@
  8573. }
  8574. tmp = tmp->b_this_page;
  8575. } while (tmp != bh);
  8576. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  8577. - local_irq_restore(flags);
  8578. + bh_uptodate_unlock_irqrestore(first, flags);
  8579. /*
  8580. * If none of the buffers had errors then we can set the page uptodate,
  8581. * but we first have to perform the post read mst fixups, if the
  8582. @@ -145,13 +143,13 @@
  8583. recs = PAGE_CACHE_SIZE / rec_size;
  8584. /* Should have been verified before we got here... */
  8585. BUG_ON(!recs);
  8586. - local_irq_save(flags);
  8587. + local_irq_save_nort(flags);
  8588. kaddr = kmap_atomic(page);
  8589. for (i = 0; i < recs; i++)
  8590. post_read_mst_fixup((NTFS_RECORD*)(kaddr +
  8591. i * rec_size), rec_size);
  8592. kunmap_atomic(kaddr);
  8593. - local_irq_restore(flags);
  8594. + local_irq_restore_nort(flags);
  8595. flush_dcache_page(page);
  8596. if (likely(page_uptodate && !PageError(page)))
  8597. SetPageUptodate(page);
  8598. @@ -159,9 +157,7 @@
  8599. unlock_page(page);
  8600. return;
  8601. still_busy:
  8602. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  8603. - local_irq_restore(flags);
  8604. - return;
  8605. + bh_uptodate_unlock_irqrestore(first, flags);
  8606. }
  8607. /**
  8608. diff -Nur linux-4.1.20.orig/fs/timerfd.c linux-4.1.20/fs/timerfd.c
  8609. --- linux-4.1.20.orig/fs/timerfd.c 2016-03-17 19:11:03.000000000 +0100
  8610. +++ linux-4.1.20/fs/timerfd.c 2016-03-21 20:18:32.000000000 +0100
  8611. @@ -450,7 +450,10 @@
  8612. break;
  8613. }
  8614. spin_unlock_irq(&ctx->wqh.lock);
  8615. - cpu_relax();
  8616. + if (isalarm(ctx))
  8617. + hrtimer_wait_for_timer(&ctx->t.alarm.timer);
  8618. + else
  8619. + hrtimer_wait_for_timer(&ctx->t.tmr);
  8620. }
  8621. /*
  8622. diff -Nur linux-4.1.20.orig/fs/xfs/xfs_inode.c linux-4.1.20/fs/xfs/xfs_inode.c
  8623. --- linux-4.1.20.orig/fs/xfs/xfs_inode.c 2016-03-17 19:11:03.000000000 +0100
  8624. +++ linux-4.1.20/fs/xfs/xfs_inode.c 2016-03-21 20:18:32.000000000 +0100
  8625. @@ -164,7 +164,7 @@
  8626. (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
  8627. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  8628. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  8629. - ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  8630. + ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
  8631. if (lock_flags & XFS_IOLOCK_EXCL)
  8632. mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  8633. @@ -212,7 +212,7 @@
  8634. (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
  8635. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  8636. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  8637. - ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  8638. + ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
  8639. if (lock_flags & XFS_IOLOCK_EXCL) {
  8640. if (!mrtryupdate(&ip->i_iolock))
  8641. @@ -281,7 +281,7 @@
  8642. (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
  8643. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  8644. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  8645. - ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  8646. + ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
  8647. ASSERT(lock_flags != 0);
  8648. if (lock_flags & XFS_IOLOCK_EXCL)
  8649. @@ -364,30 +364,38 @@
  8650. /*
  8651. * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
  8652. - * value. This shouldn't be called for page fault locking, but we also need to
  8653. - * ensure we don't overrun the number of lockdep subclasses for the iolock or
  8654. - * mmaplock as that is limited to 12 by the mmap lock lockdep annotations.
  8655. + * value. This can be called for any type of inode lock combination, including
  8656. + * parent locking. Care must be taken to ensure we don't overrun the subclass
  8657. + * storage fields in the class mask we build.
  8658. */
  8659. static inline int
  8660. xfs_lock_inumorder(int lock_mode, int subclass)
  8661. {
  8662. + int class = 0;
  8663. +
  8664. + ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
  8665. + XFS_ILOCK_RTSUM)));
  8666. +
  8667. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
  8668. - ASSERT(subclass + XFS_LOCK_INUMORDER <
  8669. - (1 << (XFS_MMAPLOCK_SHIFT - XFS_IOLOCK_SHIFT)));
  8670. - lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
  8671. + ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
  8672. + ASSERT(subclass + XFS_IOLOCK_PARENT_VAL <
  8673. + MAX_LOCKDEP_SUBCLASSES);
  8674. + class += subclass << XFS_IOLOCK_SHIFT;
  8675. + if (lock_mode & XFS_IOLOCK_PARENT)
  8676. + class += XFS_IOLOCK_PARENT_VAL << XFS_IOLOCK_SHIFT;
  8677. }
  8678. if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
  8679. - ASSERT(subclass + XFS_LOCK_INUMORDER <
  8680. - (1 << (XFS_ILOCK_SHIFT - XFS_MMAPLOCK_SHIFT)));
  8681. - lock_mode |= (subclass + XFS_LOCK_INUMORDER) <<
  8682. - XFS_MMAPLOCK_SHIFT;
  8683. + ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
  8684. + class += subclass << XFS_MMAPLOCK_SHIFT;
  8685. }
  8686. - if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
  8687. - lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
  8688. + if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
  8689. + ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
  8690. + class += subclass << XFS_ILOCK_SHIFT;
  8691. + }
  8692. - return lock_mode;
  8693. + return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
  8694. }
  8695. /*
  8696. @@ -399,6 +407,11 @@
  8697. * transaction (such as truncate). This can result in deadlock since the long
  8698. * running trans might need to wait for the inode we just locked in order to
  8699. * push the tail and free space in the log.
  8700. + *
  8701. + * xfs_lock_inodes() can only be used to lock one type of lock at a time -
  8702. + * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
  8703. + * lock more than one at a time, lockdep will report false positives saying we
  8704. + * have violated locking orders.
  8705. */
  8706. void
  8707. xfs_lock_inodes(
  8708. @@ -409,8 +422,29 @@
  8709. int attempts = 0, i, j, try_lock;
  8710. xfs_log_item_t *lp;
  8711. - /* currently supports between 2 and 5 inodes */
  8712. + /*
  8713. + * Currently supports between 2 and 5 inodes with exclusive locking. We
  8714. + * support an arbitrary depth of locking here, but absolute limits on
  8715. + * inodes depend on the the type of locking and the limits placed by
  8716. + * lockdep annotations in xfs_lock_inumorder. These are all checked by
  8717. + * the asserts.
  8718. + */
  8719. ASSERT(ips && inodes >= 2 && inodes <= 5);
  8720. + ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
  8721. + XFS_ILOCK_EXCL));
  8722. + ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
  8723. + XFS_ILOCK_SHARED)));
  8724. + ASSERT(!(lock_mode & XFS_IOLOCK_EXCL) ||
  8725. + inodes <= XFS_IOLOCK_MAX_SUBCLASS + 1);
  8726. + ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
  8727. + inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
  8728. + ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
  8729. + inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
  8730. +
  8731. + if (lock_mode & XFS_IOLOCK_EXCL) {
  8732. + ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
  8733. + } else if (lock_mode & XFS_MMAPLOCK_EXCL)
  8734. + ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
  8735. try_lock = 0;
  8736. i = 0;
  8737. diff -Nur linux-4.1.20.orig/fs/xfs/xfs_inode.h linux-4.1.20/fs/xfs/xfs_inode.h
  8738. --- linux-4.1.20.orig/fs/xfs/xfs_inode.h 2016-03-17 19:11:03.000000000 +0100
  8739. +++ linux-4.1.20/fs/xfs/xfs_inode.h 2016-03-21 20:18:32.000000000 +0100
  8740. @@ -284,9 +284,9 @@
  8741. * Flags for lockdep annotations.
  8742. *
  8743. * XFS_LOCK_PARENT - for directory operations that require locking a
  8744. - * parent directory inode and a child entry inode. The parent gets locked
  8745. - * with this flag so it gets a lockdep subclass of 1 and the child entry
  8746. - * lock will have a lockdep subclass of 0.
  8747. + * parent directory inode and a child entry inode. IOLOCK requires nesting,
  8748. + * MMAPLOCK does not support this class, ILOCK requires a single subclass
  8749. + * to differentiate parent from child.
  8750. *
  8751. * XFS_LOCK_RTBITMAP/XFS_LOCK_RTSUM - the realtime device bitmap and summary
  8752. * inodes do not participate in the normal lock order, and thus have their
  8753. @@ -295,30 +295,63 @@
  8754. * XFS_LOCK_INUMORDER - for locking several inodes at the some time
  8755. * with xfs_lock_inodes(). This flag is used as the starting subclass
  8756. * and each subsequent lock acquired will increment the subclass by one.
  8757. - * So the first lock acquired will have a lockdep subclass of 4, the
  8758. - * second lock will have a lockdep subclass of 5, and so on. It is
  8759. - * the responsibility of the class builder to shift this to the correct
  8760. - * portion of the lock_mode lockdep mask.
  8761. + * However, MAX_LOCKDEP_SUBCLASSES == 8, which means we are greatly
  8762. + * limited to the subclasses we can represent via nesting. We need at least
  8763. + * 5 inodes nest depth for the ILOCK through rename, and we also have to support
  8764. + * XFS_ILOCK_PARENT, which gives 6 subclasses. Then we have XFS_ILOCK_RTBITMAP
  8765. + * and XFS_ILOCK_RTSUM, which are another 2 unique subclasses, so that's all
  8766. + * 8 subclasses supported by lockdep.
  8767. + *
  8768. + * This also means we have to number the sub-classes in the lowest bits of
  8769. + * the mask we keep, and we have to ensure we never exceed 3 bits of lockdep
  8770. + * mask and we can't use bit-masking to build the subclasses. What a mess.
  8771. + *
  8772. + * Bit layout:
  8773. + *
  8774. + * Bit Lock Region
  8775. + * 16-19 XFS_IOLOCK_SHIFT dependencies
  8776. + * 20-23 XFS_MMAPLOCK_SHIFT dependencies
  8777. + * 24-31 XFS_ILOCK_SHIFT dependencies
  8778. + *
  8779. + * IOLOCK values
  8780. + *
  8781. + * 0-3 subclass value
  8782. + * 4-7 PARENT subclass values
  8783. + *
  8784. + * MMAPLOCK values
  8785. + *
  8786. + * 0-3 subclass value
  8787. + * 4-7 unused
  8788. + *
  8789. + * ILOCK values
  8790. + * 0-4 subclass values
  8791. + * 5 PARENT subclass (not nestable)
  8792. + * 6 RTBITMAP subclass (not nestable)
  8793. + * 7 RTSUM subclass (not nestable)
  8794. + *
  8795. */
  8796. -#define XFS_LOCK_PARENT 1
  8797. -#define XFS_LOCK_RTBITMAP 2
  8798. -#define XFS_LOCK_RTSUM 3
  8799. -#define XFS_LOCK_INUMORDER 4
  8800. -
  8801. -#define XFS_IOLOCK_SHIFT 16
  8802. -#define XFS_IOLOCK_PARENT (XFS_LOCK_PARENT << XFS_IOLOCK_SHIFT)
  8803. -
  8804. -#define XFS_MMAPLOCK_SHIFT 20
  8805. -
  8806. -#define XFS_ILOCK_SHIFT 24
  8807. -#define XFS_ILOCK_PARENT (XFS_LOCK_PARENT << XFS_ILOCK_SHIFT)
  8808. -#define XFS_ILOCK_RTBITMAP (XFS_LOCK_RTBITMAP << XFS_ILOCK_SHIFT)
  8809. -#define XFS_ILOCK_RTSUM (XFS_LOCK_RTSUM << XFS_ILOCK_SHIFT)
  8810. -
  8811. -#define XFS_IOLOCK_DEP_MASK 0x000f0000
  8812. -#define XFS_MMAPLOCK_DEP_MASK 0x00f00000
  8813. -#define XFS_ILOCK_DEP_MASK 0xff000000
  8814. -#define XFS_LOCK_DEP_MASK (XFS_IOLOCK_DEP_MASK | \
  8815. +#define XFS_IOLOCK_SHIFT 16
  8816. +#define XFS_IOLOCK_PARENT_VAL 4
  8817. +#define XFS_IOLOCK_MAX_SUBCLASS (XFS_IOLOCK_PARENT_VAL - 1)
  8818. +#define XFS_IOLOCK_DEP_MASK 0x000f0000
  8819. +#define XFS_IOLOCK_PARENT (XFS_IOLOCK_PARENT_VAL << XFS_IOLOCK_SHIFT)
  8820. +
  8821. +#define XFS_MMAPLOCK_SHIFT 20
  8822. +#define XFS_MMAPLOCK_NUMORDER 0
  8823. +#define XFS_MMAPLOCK_MAX_SUBCLASS 3
  8824. +#define XFS_MMAPLOCK_DEP_MASK 0x00f00000
  8825. +
  8826. +#define XFS_ILOCK_SHIFT 24
  8827. +#define XFS_ILOCK_PARENT_VAL 5
  8828. +#define XFS_ILOCK_MAX_SUBCLASS (XFS_ILOCK_PARENT_VAL - 1)
  8829. +#define XFS_ILOCK_RTBITMAP_VAL 6
  8830. +#define XFS_ILOCK_RTSUM_VAL 7
  8831. +#define XFS_ILOCK_DEP_MASK 0xff000000
  8832. +#define XFS_ILOCK_PARENT (XFS_ILOCK_PARENT_VAL << XFS_ILOCK_SHIFT)
  8833. +#define XFS_ILOCK_RTBITMAP (XFS_ILOCK_RTBITMAP_VAL << XFS_ILOCK_SHIFT)
  8834. +#define XFS_ILOCK_RTSUM (XFS_ILOCK_RTSUM_VAL << XFS_ILOCK_SHIFT)
  8835. +
  8836. +#define XFS_LOCK_SUBCLASS_MASK (XFS_IOLOCK_DEP_MASK | \
  8837. XFS_MMAPLOCK_DEP_MASK | \
  8838. XFS_ILOCK_DEP_MASK)
  8839. diff -Nur linux-4.1.20.orig/include/acpi/platform/aclinux.h linux-4.1.20/include/acpi/platform/aclinux.h
  8840. --- linux-4.1.20.orig/include/acpi/platform/aclinux.h 2016-03-17 19:11:03.000000000 +0100
  8841. +++ linux-4.1.20/include/acpi/platform/aclinux.h 2016-03-21 20:18:32.000000000 +0100
  8842. @@ -123,6 +123,7 @@
  8843. #define acpi_cache_t struct kmem_cache
  8844. #define acpi_spinlock spinlock_t *
  8845. +#define acpi_raw_spinlock raw_spinlock_t *
  8846. #define acpi_cpu_flags unsigned long
  8847. /* Use native linux version of acpi_os_allocate_zeroed */
  8848. @@ -141,6 +142,20 @@
  8849. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_get_thread_id
  8850. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_create_lock
  8851. +#define acpi_os_create_raw_lock(__handle) \
  8852. +({ \
  8853. + raw_spinlock_t *lock = ACPI_ALLOCATE(sizeof(*lock)); \
  8854. + \
  8855. + if (lock) { \
  8856. + *(__handle) = lock; \
  8857. + raw_spin_lock_init(*(__handle)); \
  8858. + } \
  8859. + lock ? AE_OK : AE_NO_MEMORY; \
  8860. + })
  8861. +
  8862. +#define acpi_os_delete_raw_lock(__handle) kfree(__handle)
  8863. +
  8864. +
  8865. /*
  8866. * OSL interfaces used by debugger/disassembler
  8867. */
  8868. diff -Nur linux-4.1.20.orig/include/asm-generic/bug.h linux-4.1.20/include/asm-generic/bug.h
  8869. --- linux-4.1.20.orig/include/asm-generic/bug.h 2016-03-17 19:11:03.000000000 +0100
  8870. +++ linux-4.1.20/include/asm-generic/bug.h 2016-03-21 20:18:33.000000000 +0100
  8871. @@ -206,6 +206,20 @@
  8872. # define WARN_ON_SMP(x) ({0;})
  8873. #endif
  8874. +#ifdef CONFIG_PREEMPT_RT_BASE
  8875. +# define BUG_ON_RT(c) BUG_ON(c)
  8876. +# define BUG_ON_NONRT(c) do { } while (0)
  8877. +# define WARN_ON_RT(condition) WARN_ON(condition)
  8878. +# define WARN_ON_NONRT(condition) do { } while (0)
  8879. +# define WARN_ON_ONCE_NONRT(condition) do { } while (0)
  8880. +#else
  8881. +# define BUG_ON_RT(c) do { } while (0)
  8882. +# define BUG_ON_NONRT(c) BUG_ON(c)
  8883. +# define WARN_ON_RT(condition) do { } while (0)
  8884. +# define WARN_ON_NONRT(condition) WARN_ON(condition)
  8885. +# define WARN_ON_ONCE_NONRT(condition) WARN_ON_ONCE(condition)
  8886. +#endif
  8887. +
  8888. #endif /* __ASSEMBLY__ */
  8889. #endif
  8890. diff -Nur linux-4.1.20.orig/include/asm-generic/futex.h linux-4.1.20/include/asm-generic/futex.h
  8891. --- linux-4.1.20.orig/include/asm-generic/futex.h 2016-03-17 19:11:03.000000000 +0100
  8892. +++ linux-4.1.20/include/asm-generic/futex.h 2016-03-21 20:18:33.000000000 +0100
  8893. @@ -8,8 +8,7 @@
  8894. #ifndef CONFIG_SMP
  8895. /*
  8896. * The following implementation only for uniprocessor machines.
  8897. - * For UP, it's relies on the fact that pagefault_disable() also disables
  8898. - * preemption to ensure mutual exclusion.
  8899. + * It relies on preempt_disable() ensuring mutual exclusion.
  8900. *
  8901. */
  8902. @@ -38,6 +37,7 @@
  8903. if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
  8904. oparg = 1 << oparg;
  8905. + preempt_disable();
  8906. pagefault_disable();
  8907. ret = -EFAULT;
  8908. @@ -72,6 +72,7 @@
  8909. out_pagefault_enable:
  8910. pagefault_enable();
  8911. + preempt_enable();
  8912. if (ret == 0) {
  8913. switch (cmp) {
  8914. @@ -106,6 +107,7 @@
  8915. {
  8916. u32 val;
  8917. + preempt_disable();
  8918. if (unlikely(get_user(val, uaddr) != 0))
  8919. return -EFAULT;
  8920. @@ -113,6 +115,7 @@
  8921. return -EFAULT;
  8922. *uval = val;
  8923. + preempt_enable();
  8924. return 0;
  8925. }
  8926. diff -Nur linux-4.1.20.orig/include/linux/blk-mq.h linux-4.1.20/include/linux/blk-mq.h
  8927. --- linux-4.1.20.orig/include/linux/blk-mq.h 2016-03-17 19:11:03.000000000 +0100
  8928. +++ linux-4.1.20/include/linux/blk-mq.h 2016-03-21 20:18:33.000000000 +0100
  8929. @@ -202,6 +202,7 @@
  8930. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_index);
  8931. struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *, unsigned int, int);
  8932. +void __blk_mq_complete_request_remote_work(struct work_struct *work);
  8933. int blk_mq_request_started(struct request *rq);
  8934. void blk_mq_start_request(struct request *rq);
  8935. diff -Nur linux-4.1.20.orig/include/linux/blkdev.h linux-4.1.20/include/linux/blkdev.h
  8936. --- linux-4.1.20.orig/include/linux/blkdev.h 2016-03-17 19:11:03.000000000 +0100
  8937. +++ linux-4.1.20/include/linux/blkdev.h 2016-03-21 20:18:33.000000000 +0100
  8938. @@ -101,6 +101,7 @@
  8939. struct list_head queuelist;
  8940. union {
  8941. struct call_single_data csd;
  8942. + struct work_struct work;
  8943. unsigned long fifo_time;
  8944. };
  8945. @@ -482,7 +483,7 @@
  8946. struct throtl_data *td;
  8947. #endif
  8948. struct rcu_head rcu_head;
  8949. - wait_queue_head_t mq_freeze_wq;
  8950. + struct swait_head mq_freeze_wq;
  8951. struct percpu_ref mq_usage_counter;
  8952. struct list_head all_q_node;
  8953. diff -Nur linux-4.1.20.orig/include/linux/bottom_half.h linux-4.1.20/include/linux/bottom_half.h
  8954. --- linux-4.1.20.orig/include/linux/bottom_half.h 2016-03-17 19:11:03.000000000 +0100
  8955. +++ linux-4.1.20/include/linux/bottom_half.h 2016-03-21 20:18:33.000000000 +0100
  8956. @@ -4,6 +4,39 @@
  8957. #include <linux/preempt.h>
  8958. #include <linux/preempt_mask.h>
  8959. +#ifdef CONFIG_PREEMPT_RT_FULL
  8960. +
  8961. +extern void __local_bh_disable(void);
  8962. +extern void _local_bh_enable(void);
  8963. +extern void __local_bh_enable(void);
  8964. +
  8965. +static inline void local_bh_disable(void)
  8966. +{
  8967. + __local_bh_disable();
  8968. +}
  8969. +
  8970. +static inline void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
  8971. +{
  8972. + __local_bh_disable();
  8973. +}
  8974. +
  8975. +static inline void local_bh_enable(void)
  8976. +{
  8977. + __local_bh_enable();
  8978. +}
  8979. +
  8980. +static inline void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
  8981. +{
  8982. + __local_bh_enable();
  8983. +}
  8984. +
  8985. +static inline void local_bh_enable_ip(unsigned long ip)
  8986. +{
  8987. + __local_bh_enable();
  8988. +}
  8989. +
  8990. +#else
  8991. +
  8992. #ifdef CONFIG_TRACE_IRQFLAGS
  8993. extern void __local_bh_disable_ip(unsigned long ip, unsigned int cnt);
  8994. #else
  8995. @@ -31,5 +64,6 @@
  8996. {
  8997. __local_bh_enable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET);
  8998. }
  8999. +#endif
  9000. #endif /* _LINUX_BH_H */
  9001. diff -Nur linux-4.1.20.orig/include/linux/buffer_head.h linux-4.1.20/include/linux/buffer_head.h
  9002. --- linux-4.1.20.orig/include/linux/buffer_head.h 2016-03-17 19:11:03.000000000 +0100
  9003. +++ linux-4.1.20/include/linux/buffer_head.h 2016-03-21 20:18:33.000000000 +0100
  9004. @@ -75,8 +75,52 @@
  9005. struct address_space *b_assoc_map; /* mapping this buffer is
  9006. associated with */
  9007. atomic_t b_count; /* users using this buffer_head */
  9008. +#ifdef CONFIG_PREEMPT_RT_BASE
  9009. + spinlock_t b_uptodate_lock;
  9010. +#if defined(CONFIG_JBD) || defined(CONFIG_JBD_MODULE) || \
  9011. + defined(CONFIG_JBD2) || defined(CONFIG_JBD2_MODULE)
  9012. + spinlock_t b_state_lock;
  9013. + spinlock_t b_journal_head_lock;
  9014. +#endif
  9015. +#endif
  9016. };
  9017. +static inline unsigned long bh_uptodate_lock_irqsave(struct buffer_head *bh)
  9018. +{
  9019. + unsigned long flags;
  9020. +
  9021. +#ifndef CONFIG_PREEMPT_RT_BASE
  9022. + local_irq_save(flags);
  9023. + bit_spin_lock(BH_Uptodate_Lock, &bh->b_state);
  9024. +#else
  9025. + spin_lock_irqsave(&bh->b_uptodate_lock, flags);
  9026. +#endif
  9027. + return flags;
  9028. +}
  9029. +
  9030. +static inline void
  9031. +bh_uptodate_unlock_irqrestore(struct buffer_head *bh, unsigned long flags)
  9032. +{
  9033. +#ifndef CONFIG_PREEMPT_RT_BASE
  9034. + bit_spin_unlock(BH_Uptodate_Lock, &bh->b_state);
  9035. + local_irq_restore(flags);
  9036. +#else
  9037. + spin_unlock_irqrestore(&bh->b_uptodate_lock, flags);
  9038. +#endif
  9039. +}
  9040. +
  9041. +static inline void buffer_head_init_locks(struct buffer_head *bh)
  9042. +{
  9043. +#ifdef CONFIG_PREEMPT_RT_BASE
  9044. + spin_lock_init(&bh->b_uptodate_lock);
  9045. +#if defined(CONFIG_JBD) || defined(CONFIG_JBD_MODULE) || \
  9046. + defined(CONFIG_JBD2) || defined(CONFIG_JBD2_MODULE)
  9047. + spin_lock_init(&bh->b_state_lock);
  9048. + spin_lock_init(&bh->b_journal_head_lock);
  9049. +#endif
  9050. +#endif
  9051. +}
  9052. +
  9053. /*
  9054. * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
  9055. * and buffer_foo() functions.
  9056. diff -Nur linux-4.1.20.orig/include/linux/cgroup-defs.h linux-4.1.20/include/linux/cgroup-defs.h
  9057. --- linux-4.1.20.orig/include/linux/cgroup-defs.h 2016-03-17 19:11:03.000000000 +0100
  9058. +++ linux-4.1.20/include/linux/cgroup-defs.h 2016-03-21 20:18:33.000000000 +0100
  9059. @@ -124,6 +124,7 @@
  9060. /* percpu_ref killing and RCU release */
  9061. struct rcu_head rcu_head;
  9062. struct work_struct destroy_work;
  9063. + struct swork_event destroy_swork;
  9064. };
  9065. /*
  9066. diff -Nur linux-4.1.20.orig/include/linux/cgroup.h linux-4.1.20/include/linux/cgroup.h
  9067. --- linux-4.1.20.orig/include/linux/cgroup.h 2016-03-17 19:11:03.000000000 +0100
  9068. +++ linux-4.1.20/include/linux/cgroup.h 2016-03-21 20:18:33.000000000 +0100
  9069. @@ -17,6 +17,8 @@
  9070. #include <linux/fs.h>
  9071. #include <linux/seq_file.h>
  9072. #include <linux/kernfs.h>
  9073. +#include <linux/wait.h>
  9074. +#include <linux/work-simple.h>
  9075. #include <linux/cgroup-defs.h>
  9076. diff -Nur linux-4.1.20.orig/include/linux/completion.h linux-4.1.20/include/linux/completion.h
  9077. --- linux-4.1.20.orig/include/linux/completion.h 2016-03-17 19:11:03.000000000 +0100
  9078. +++ linux-4.1.20/include/linux/completion.h 2016-03-21 20:18:33.000000000 +0100
  9079. @@ -7,8 +7,7 @@
  9080. * Atomic wait-for-completion handler data structures.
  9081. * See kernel/sched/completion.c for details.
  9082. */
  9083. -
  9084. -#include <linux/wait.h>
  9085. +#include <linux/wait-simple.h>
  9086. /*
  9087. * struct completion - structure used to maintain state for a "completion"
  9088. @@ -24,11 +23,11 @@
  9089. */
  9090. struct completion {
  9091. unsigned int done;
  9092. - wait_queue_head_t wait;
  9093. + struct swait_head wait;
  9094. };
  9095. #define COMPLETION_INITIALIZER(work) \
  9096. - { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  9097. + { 0, SWAIT_HEAD_INITIALIZER((work).wait) }
  9098. #define COMPLETION_INITIALIZER_ONSTACK(work) \
  9099. ({ init_completion(&work); work; })
  9100. @@ -73,7 +72,7 @@
  9101. static inline void init_completion(struct completion *x)
  9102. {
  9103. x->done = 0;
  9104. - init_waitqueue_head(&x->wait);
  9105. + init_swait_head(&x->wait);
  9106. }
  9107. /**
  9108. diff -Nur linux-4.1.20.orig/include/linux/cpu.h linux-4.1.20/include/linux/cpu.h
  9109. --- linux-4.1.20.orig/include/linux/cpu.h 2016-03-17 19:11:03.000000000 +0100
  9110. +++ linux-4.1.20/include/linux/cpu.h 2016-03-21 20:18:33.000000000 +0100
  9111. @@ -231,6 +231,8 @@
  9112. extern void put_online_cpus(void);
  9113. extern void cpu_hotplug_disable(void);
  9114. extern void cpu_hotplug_enable(void);
  9115. +extern void pin_current_cpu(void);
  9116. +extern void unpin_current_cpu(void);
  9117. #define hotcpu_notifier(fn, pri) cpu_notifier(fn, pri)
  9118. #define __hotcpu_notifier(fn, pri) __cpu_notifier(fn, pri)
  9119. #define register_hotcpu_notifier(nb) register_cpu_notifier(nb)
  9120. @@ -249,6 +251,8 @@
  9121. #define put_online_cpus() do { } while (0)
  9122. #define cpu_hotplug_disable() do { } while (0)
  9123. #define cpu_hotplug_enable() do { } while (0)
  9124. +static inline void pin_current_cpu(void) { }
  9125. +static inline void unpin_current_cpu(void) { }
  9126. #define hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  9127. #define __hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  9128. /* These aren't inline functions due to a GCC bug. */
  9129. diff -Nur linux-4.1.20.orig/include/linux/delay.h linux-4.1.20/include/linux/delay.h
  9130. --- linux-4.1.20.orig/include/linux/delay.h 2016-03-17 19:11:03.000000000 +0100
  9131. +++ linux-4.1.20/include/linux/delay.h 2016-03-21 20:18:33.000000000 +0100
  9132. @@ -52,4 +52,10 @@
  9133. msleep(seconds * 1000);
  9134. }
  9135. +#ifdef CONFIG_PREEMPT_RT_FULL
  9136. +extern void cpu_chill(void);
  9137. +#else
  9138. +# define cpu_chill() cpu_relax()
  9139. +#endif
  9140. +
  9141. #endif /* defined(_LINUX_DELAY_H) */
  9142. diff -Nur linux-4.1.20.orig/include/linux/ftrace.h linux-4.1.20/include/linux/ftrace.h
  9143. --- linux-4.1.20.orig/include/linux/ftrace.h 2016-03-17 19:11:03.000000000 +0100
  9144. +++ linux-4.1.20/include/linux/ftrace.h 2016-03-21 20:18:33.000000000 +0100
  9145. @@ -682,6 +682,18 @@
  9146. #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
  9147. #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
  9148. +static inline unsigned long get_lock_parent_ip(void)
  9149. +{
  9150. + unsigned long addr = CALLER_ADDR0;
  9151. +
  9152. + if (!in_lock_functions(addr))
  9153. + return addr;
  9154. + addr = CALLER_ADDR1;
  9155. + if (!in_lock_functions(addr))
  9156. + return addr;
  9157. + return CALLER_ADDR2;
  9158. +}
  9159. +
  9160. #ifdef CONFIG_IRQSOFF_TRACER
  9161. extern void time_hardirqs_on(unsigned long a0, unsigned long a1);
  9162. extern void time_hardirqs_off(unsigned long a0, unsigned long a1);
  9163. diff -Nur linux-4.1.20.orig/include/linux/ftrace_event.h linux-4.1.20/include/linux/ftrace_event.h
  9164. --- linux-4.1.20.orig/include/linux/ftrace_event.h 2016-03-17 19:11:03.000000000 +0100
  9165. +++ linux-4.1.20/include/linux/ftrace_event.h 2016-03-21 20:18:33.000000000 +0100
  9166. @@ -66,6 +66,9 @@
  9167. unsigned char flags;
  9168. unsigned char preempt_count;
  9169. int pid;
  9170. + unsigned short migrate_disable;
  9171. + unsigned short padding;
  9172. + unsigned char preempt_lazy_count;
  9173. };
  9174. #define FTRACE_MAX_EVENT \
  9175. diff -Nur linux-4.1.20.orig/include/linux/highmem.h linux-4.1.20/include/linux/highmem.h
  9176. --- linux-4.1.20.orig/include/linux/highmem.h 2016-03-17 19:11:03.000000000 +0100
  9177. +++ linux-4.1.20/include/linux/highmem.h 2016-03-21 20:18:33.000000000 +0100
  9178. @@ -7,6 +7,7 @@
  9179. #include <linux/mm.h>
  9180. #include <linux/uaccess.h>
  9181. #include <linux/hardirq.h>
  9182. +#include <linux/sched.h>
  9183. #include <asm/cacheflush.h>
  9184. @@ -65,6 +66,7 @@
  9185. static inline void *kmap_atomic(struct page *page)
  9186. {
  9187. + preempt_disable_nort();
  9188. pagefault_disable();
  9189. return page_address(page);
  9190. }
  9191. @@ -73,6 +75,7 @@
  9192. static inline void __kunmap_atomic(void *addr)
  9193. {
  9194. pagefault_enable();
  9195. + preempt_enable_nort();
  9196. }
  9197. #define kmap_atomic_pfn(pfn) kmap_atomic(pfn_to_page(pfn))
  9198. @@ -85,32 +88,51 @@
  9199. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  9200. +#ifndef CONFIG_PREEMPT_RT_FULL
  9201. DECLARE_PER_CPU(int, __kmap_atomic_idx);
  9202. +#endif
  9203. static inline int kmap_atomic_idx_push(void)
  9204. {
  9205. +#ifndef CONFIG_PREEMPT_RT_FULL
  9206. int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;
  9207. -#ifdef CONFIG_DEBUG_HIGHMEM
  9208. +# ifdef CONFIG_DEBUG_HIGHMEM
  9209. WARN_ON_ONCE(in_irq() && !irqs_disabled());
  9210. BUG_ON(idx >= KM_TYPE_NR);
  9211. -#endif
  9212. +# endif
  9213. return idx;
  9214. +#else
  9215. + current->kmap_idx++;
  9216. + BUG_ON(current->kmap_idx > KM_TYPE_NR);
  9217. + return current->kmap_idx - 1;
  9218. +#endif
  9219. }
  9220. static inline int kmap_atomic_idx(void)
  9221. {
  9222. +#ifndef CONFIG_PREEMPT_RT_FULL
  9223. return __this_cpu_read(__kmap_atomic_idx) - 1;
  9224. +#else
  9225. + return current->kmap_idx - 1;
  9226. +#endif
  9227. }
  9228. static inline void kmap_atomic_idx_pop(void)
  9229. {
  9230. -#ifdef CONFIG_DEBUG_HIGHMEM
  9231. +#ifndef CONFIG_PREEMPT_RT_FULL
  9232. +# ifdef CONFIG_DEBUG_HIGHMEM
  9233. int idx = __this_cpu_dec_return(__kmap_atomic_idx);
  9234. BUG_ON(idx < 0);
  9235. -#else
  9236. +# else
  9237. __this_cpu_dec(__kmap_atomic_idx);
  9238. +# endif
  9239. +#else
  9240. + current->kmap_idx--;
  9241. +# ifdef CONFIG_DEBUG_HIGHMEM
  9242. + BUG_ON(current->kmap_idx < 0);
  9243. +# endif
  9244. #endif
  9245. }
  9246. diff -Nur linux-4.1.20.orig/include/linux/hrtimer.h linux-4.1.20/include/linux/hrtimer.h
  9247. --- linux-4.1.20.orig/include/linux/hrtimer.h 2016-03-17 19:11:03.000000000 +0100
  9248. +++ linux-4.1.20/include/linux/hrtimer.h 2016-03-21 20:18:33.000000000 +0100
  9249. @@ -111,6 +111,11 @@
  9250. enum hrtimer_restart (*function)(struct hrtimer *);
  9251. struct hrtimer_clock_base *base;
  9252. unsigned long state;
  9253. + struct list_head cb_entry;
  9254. + int irqsafe;
  9255. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  9256. + ktime_t praecox;
  9257. +#endif
  9258. #ifdef CONFIG_TIMER_STATS
  9259. int start_pid;
  9260. void *start_site;
  9261. @@ -147,6 +152,7 @@
  9262. int index;
  9263. clockid_t clockid;
  9264. struct timerqueue_head active;
  9265. + struct list_head expired;
  9266. ktime_t resolution;
  9267. ktime_t (*get_time)(void);
  9268. ktime_t softirq_time;
  9269. @@ -194,6 +200,9 @@
  9270. unsigned long nr_hangs;
  9271. ktime_t max_hang_time;
  9272. #endif
  9273. +#ifdef CONFIG_PREEMPT_RT_BASE
  9274. + wait_queue_head_t wait;
  9275. +#endif
  9276. struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
  9277. };
  9278. @@ -381,6 +390,13 @@
  9279. return hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  9280. }
  9281. +/* Softirq preemption could deadlock timer removal */
  9282. +#ifdef CONFIG_PREEMPT_RT_BASE
  9283. + extern void hrtimer_wait_for_timer(const struct hrtimer *timer);
  9284. +#else
  9285. +# define hrtimer_wait_for_timer(timer) do { cpu_relax(); } while (0)
  9286. +#endif
  9287. +
  9288. /* Query timers: */
  9289. extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
  9290. extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
  9291. diff -Nur linux-4.1.20.orig/include/linux/idr.h linux-4.1.20/include/linux/idr.h
  9292. --- linux-4.1.20.orig/include/linux/idr.h 2016-03-17 19:11:03.000000000 +0100
  9293. +++ linux-4.1.20/include/linux/idr.h 2016-03-21 20:18:33.000000000 +0100
  9294. @@ -95,10 +95,14 @@
  9295. * Each idr_preload() should be matched with an invocation of this
  9296. * function. See idr_preload() for details.
  9297. */
  9298. +#ifdef CONFIG_PREEMPT_RT_FULL
  9299. +void idr_preload_end(void);
  9300. +#else
  9301. static inline void idr_preload_end(void)
  9302. {
  9303. preempt_enable();
  9304. }
  9305. +#endif
  9306. /**
  9307. * idr_find - return pointer for given id
  9308. diff -Nur linux-4.1.20.orig/include/linux/init_task.h linux-4.1.20/include/linux/init_task.h
  9309. --- linux-4.1.20.orig/include/linux/init_task.h 2016-03-17 19:11:03.000000000 +0100
  9310. +++ linux-4.1.20/include/linux/init_task.h 2016-03-21 20:18:33.000000000 +0100
  9311. @@ -147,9 +147,16 @@
  9312. # define INIT_PERF_EVENTS(tsk)
  9313. #endif
  9314. +#ifdef CONFIG_PREEMPT_RT_BASE
  9315. +# define INIT_TIMER_LIST .posix_timer_list = NULL,
  9316. +#else
  9317. +# define INIT_TIMER_LIST
  9318. +#endif
  9319. +
  9320. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  9321. # define INIT_VTIME(tsk) \
  9322. - .vtime_seqlock = __SEQLOCK_UNLOCKED(tsk.vtime_seqlock), \
  9323. + .vtime_lock = __RAW_SPIN_LOCK_UNLOCKED(tsk.vtime_lock), \
  9324. + .vtime_seq = SEQCNT_ZERO(tsk.vtime_seq), \
  9325. .vtime_snap = 0, \
  9326. .vtime_snap_whence = VTIME_SYS,
  9327. #else
  9328. @@ -238,6 +245,7 @@
  9329. .cpu_timers = INIT_CPU_TIMERS(tsk.cpu_timers), \
  9330. .pi_lock = __RAW_SPIN_LOCK_UNLOCKED(tsk.pi_lock), \
  9331. .timer_slack_ns = 50000, /* 50 usec default slack */ \
  9332. + INIT_TIMER_LIST \
  9333. .pids = { \
  9334. [PIDTYPE_PID] = INIT_PID_LINK(PIDTYPE_PID), \
  9335. [PIDTYPE_PGID] = INIT_PID_LINK(PIDTYPE_PGID), \
  9336. diff -Nur linux-4.1.20.orig/include/linux/interrupt.h linux-4.1.20/include/linux/interrupt.h
  9337. --- linux-4.1.20.orig/include/linux/interrupt.h 2016-03-17 19:11:03.000000000 +0100
  9338. +++ linux-4.1.20/include/linux/interrupt.h 2016-03-21 20:18:33.000000000 +0100
  9339. @@ -61,6 +61,7 @@
  9340. * interrupt handler after suspending interrupts. For system
  9341. * wakeup devices users need to implement wakeup detection in
  9342. * their interrupt handlers.
  9343. + * IRQF_NO_SOFTIRQ_CALL - Do not process softirqs in the irq thread context (RT)
  9344. */
  9345. #define IRQF_SHARED 0x00000080
  9346. #define IRQF_PROBE_SHARED 0x00000100
  9347. @@ -74,6 +75,7 @@
  9348. #define IRQF_NO_THREAD 0x00010000
  9349. #define IRQF_EARLY_RESUME 0x00020000
  9350. #define IRQF_COND_SUSPEND 0x00040000
  9351. +#define IRQF_NO_SOFTIRQ_CALL 0x00080000
  9352. #define IRQF_TIMER (__IRQF_TIMER | IRQF_NO_SUSPEND | IRQF_NO_THREAD)
  9353. @@ -102,6 +104,7 @@
  9354. * @flags: flags (see IRQF_* above)
  9355. * @thread_fn: interrupt handler function for threaded interrupts
  9356. * @thread: thread pointer for threaded interrupts
  9357. + * @secondary: pointer to secondary irqaction (force threading)
  9358. * @thread_flags: flags related to @thread
  9359. * @thread_mask: bitmask for keeping track of @thread activity
  9360. * @dir: pointer to the proc/irq/NN/name entry
  9361. @@ -113,6 +116,7 @@
  9362. struct irqaction *next;
  9363. irq_handler_t thread_fn;
  9364. struct task_struct *thread;
  9365. + struct irqaction *secondary;
  9366. unsigned int irq;
  9367. unsigned int flags;
  9368. unsigned long thread_flags;
  9369. @@ -184,7 +188,7 @@
  9370. #ifdef CONFIG_LOCKDEP
  9371. # define local_irq_enable_in_hardirq() do { } while (0)
  9372. #else
  9373. -# define local_irq_enable_in_hardirq() local_irq_enable()
  9374. +# define local_irq_enable_in_hardirq() local_irq_enable_nort()
  9375. #endif
  9376. extern void disable_irq_nosync(unsigned int irq);
  9377. @@ -215,6 +219,7 @@
  9378. unsigned int irq;
  9379. struct kref kref;
  9380. struct work_struct work;
  9381. + struct list_head list;
  9382. void (*notify)(struct irq_affinity_notify *, const cpumask_t *mask);
  9383. void (*release)(struct kref *ref);
  9384. };
  9385. @@ -377,9 +382,13 @@
  9386. bool state);
  9387. #ifdef CONFIG_IRQ_FORCED_THREADING
  9388. +# ifndef CONFIG_PREEMPT_RT_BASE
  9389. extern bool force_irqthreads;
  9390. +# else
  9391. +# define force_irqthreads (true)
  9392. +# endif
  9393. #else
  9394. -#define force_irqthreads (0)
  9395. +#define force_irqthreads (false)
  9396. #endif
  9397. #ifndef __ARCH_SET_SOFTIRQ_PENDING
  9398. @@ -435,9 +444,10 @@
  9399. void (*action)(struct softirq_action *);
  9400. };
  9401. +#ifndef CONFIG_PREEMPT_RT_FULL
  9402. asmlinkage void do_softirq(void);
  9403. asmlinkage void __do_softirq(void);
  9404. -
  9405. +static inline void thread_do_softirq(void) { do_softirq(); }
  9406. #ifdef __ARCH_HAS_DO_SOFTIRQ
  9407. void do_softirq_own_stack(void);
  9408. #else
  9409. @@ -446,13 +456,25 @@
  9410. __do_softirq();
  9411. }
  9412. #endif
  9413. +#else
  9414. +extern void thread_do_softirq(void);
  9415. +#endif
  9416. extern void open_softirq(int nr, void (*action)(struct softirq_action *));
  9417. extern void softirq_init(void);
  9418. extern void __raise_softirq_irqoff(unsigned int nr);
  9419. +#ifdef CONFIG_PREEMPT_RT_FULL
  9420. +extern void __raise_softirq_irqoff_ksoft(unsigned int nr);
  9421. +#else
  9422. +static inline void __raise_softirq_irqoff_ksoft(unsigned int nr)
  9423. +{
  9424. + __raise_softirq_irqoff(nr);
  9425. +}
  9426. +#endif
  9427. extern void raise_softirq_irqoff(unsigned int nr);
  9428. extern void raise_softirq(unsigned int nr);
  9429. +extern void softirq_check_pending_idle(void);
  9430. DECLARE_PER_CPU(struct task_struct *, ksoftirqd);
  9431. @@ -474,8 +496,9 @@
  9432. to be executed on some cpu at least once after this.
  9433. * If the tasklet is already scheduled, but its execution is still not
  9434. started, it will be executed only once.
  9435. - * If this tasklet is already running on another CPU (or schedule is called
  9436. - from tasklet itself), it is rescheduled for later.
  9437. + * If this tasklet is already running on another CPU, it is rescheduled
  9438. + for later.
  9439. + * Schedule must not be called from the tasklet itself (a lockup occurs)
  9440. * Tasklet is strictly serialized wrt itself, but not
  9441. wrt another tasklets. If client needs some intertask synchronization,
  9442. he makes it with spinlocks.
  9443. @@ -500,27 +523,36 @@
  9444. enum
  9445. {
  9446. TASKLET_STATE_SCHED, /* Tasklet is scheduled for execution */
  9447. - TASKLET_STATE_RUN /* Tasklet is running (SMP only) */
  9448. + TASKLET_STATE_RUN, /* Tasklet is running (SMP only) */
  9449. + TASKLET_STATE_PENDING /* Tasklet is pending */
  9450. };
  9451. -#ifdef CONFIG_SMP
  9452. +#define TASKLET_STATEF_SCHED (1 << TASKLET_STATE_SCHED)
  9453. +#define TASKLET_STATEF_RUN (1 << TASKLET_STATE_RUN)
  9454. +#define TASKLET_STATEF_PENDING (1 << TASKLET_STATE_PENDING)
  9455. +
  9456. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  9457. static inline int tasklet_trylock(struct tasklet_struct *t)
  9458. {
  9459. return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
  9460. }
  9461. +static inline int tasklet_tryunlock(struct tasklet_struct *t)
  9462. +{
  9463. + return cmpxchg(&t->state, TASKLET_STATEF_RUN, 0) == TASKLET_STATEF_RUN;
  9464. +}
  9465. +
  9466. static inline void tasklet_unlock(struct tasklet_struct *t)
  9467. {
  9468. smp_mb__before_atomic();
  9469. clear_bit(TASKLET_STATE_RUN, &(t)->state);
  9470. }
  9471. -static inline void tasklet_unlock_wait(struct tasklet_struct *t)
  9472. -{
  9473. - while (test_bit(TASKLET_STATE_RUN, &(t)->state)) { barrier(); }
  9474. -}
  9475. +extern void tasklet_unlock_wait(struct tasklet_struct *t);
  9476. +
  9477. #else
  9478. #define tasklet_trylock(t) 1
  9479. +#define tasklet_tryunlock(t) 1
  9480. #define tasklet_unlock_wait(t) do { } while (0)
  9481. #define tasklet_unlock(t) do { } while (0)
  9482. #endif
  9483. @@ -569,12 +601,7 @@
  9484. smp_mb();
  9485. }
  9486. -static inline void tasklet_enable(struct tasklet_struct *t)
  9487. -{
  9488. - smp_mb__before_atomic();
  9489. - atomic_dec(&t->count);
  9490. -}
  9491. -
  9492. +extern void tasklet_enable(struct tasklet_struct *t);
  9493. extern void tasklet_kill(struct tasklet_struct *t);
  9494. extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
  9495. extern void tasklet_init(struct tasklet_struct *t,
  9496. @@ -605,6 +632,12 @@
  9497. tasklet_kill(&ttimer->tasklet);
  9498. }
  9499. +#ifdef CONFIG_PREEMPT_RT_FULL
  9500. +extern void softirq_early_init(void);
  9501. +#else
  9502. +static inline void softirq_early_init(void) { }
  9503. +#endif
  9504. +
  9505. /*
  9506. * Autoprobing for irqs:
  9507. *
  9508. diff -Nur linux-4.1.20.orig/include/linux/io-mapping.h linux-4.1.20/include/linux/io-mapping.h
  9509. --- linux-4.1.20.orig/include/linux/io-mapping.h 2016-03-17 19:11:03.000000000 +0100
  9510. +++ linux-4.1.20/include/linux/io-mapping.h 2016-03-21 20:18:33.000000000 +0100
  9511. @@ -141,6 +141,7 @@
  9512. io_mapping_map_atomic_wc(struct io_mapping *mapping,
  9513. unsigned long offset)
  9514. {
  9515. + preempt_disable();
  9516. pagefault_disable();
  9517. return ((char __force __iomem *) mapping) + offset;
  9518. }
  9519. @@ -149,6 +150,7 @@
  9520. io_mapping_unmap_atomic(void __iomem *vaddr)
  9521. {
  9522. pagefault_enable();
  9523. + preempt_enable();
  9524. }
  9525. /* Non-atomic map/unmap */
  9526. diff -Nur linux-4.1.20.orig/include/linux/irq.h linux-4.1.20/include/linux/irq.h
  9527. --- linux-4.1.20.orig/include/linux/irq.h 2016-03-17 19:11:03.000000000 +0100
  9528. +++ linux-4.1.20/include/linux/irq.h 2016-03-21 20:18:33.000000000 +0100
  9529. @@ -72,6 +72,7 @@
  9530. * IRQ_IS_POLLED - Always polled by another interrupt. Exclude
  9531. * it from the spurious interrupt detection
  9532. * mechanism and from core side polling.
  9533. + * IRQ_NO_SOFTIRQ_CALL - No softirq processing in the irq thread context (RT)
  9534. */
  9535. enum {
  9536. IRQ_TYPE_NONE = 0x00000000,
  9537. @@ -97,13 +98,14 @@
  9538. IRQ_NOTHREAD = (1 << 16),
  9539. IRQ_PER_CPU_DEVID = (1 << 17),
  9540. IRQ_IS_POLLED = (1 << 18),
  9541. + IRQ_NO_SOFTIRQ_CALL = (1 << 19),
  9542. };
  9543. #define IRQF_MODIFY_MASK \
  9544. (IRQ_TYPE_SENSE_MASK | IRQ_NOPROBE | IRQ_NOREQUEST | \
  9545. IRQ_NOAUTOEN | IRQ_MOVE_PCNTXT | IRQ_LEVEL | IRQ_NO_BALANCING | \
  9546. IRQ_PER_CPU | IRQ_NESTED_THREAD | IRQ_NOTHREAD | IRQ_PER_CPU_DEVID | \
  9547. - IRQ_IS_POLLED)
  9548. + IRQ_IS_POLLED | IRQ_NO_SOFTIRQ_CALL)
  9549. #define IRQ_NO_BALANCING_MASK (IRQ_PER_CPU | IRQ_NO_BALANCING)
  9550. diff -Nur linux-4.1.20.orig/include/linux/irq_work.h linux-4.1.20/include/linux/irq_work.h
  9551. --- linux-4.1.20.orig/include/linux/irq_work.h 2016-03-17 19:11:03.000000000 +0100
  9552. +++ linux-4.1.20/include/linux/irq_work.h 2016-03-21 20:18:33.000000000 +0100
  9553. @@ -16,6 +16,7 @@
  9554. #define IRQ_WORK_BUSY 2UL
  9555. #define IRQ_WORK_FLAGS 3UL
  9556. #define IRQ_WORK_LAZY 4UL /* Doesn't want IPI, wait for tick */
  9557. +#define IRQ_WORK_HARD_IRQ 8UL /* Run hard IRQ context, even on RT */
  9558. struct irq_work {
  9559. unsigned long flags;
  9560. @@ -51,4 +52,10 @@
  9561. static inline void irq_work_run(void) { }
  9562. #endif
  9563. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  9564. +void irq_work_tick_soft(void);
  9565. +#else
  9566. +static inline void irq_work_tick_soft(void) { }
  9567. +#endif
  9568. +
  9569. #endif /* _LINUX_IRQ_WORK_H */
  9570. diff -Nur linux-4.1.20.orig/include/linux/irqdesc.h linux-4.1.20/include/linux/irqdesc.h
  9571. --- linux-4.1.20.orig/include/linux/irqdesc.h 2016-03-17 19:11:03.000000000 +0100
  9572. +++ linux-4.1.20/include/linux/irqdesc.h 2016-03-21 20:18:33.000000000 +0100
  9573. @@ -63,6 +63,7 @@
  9574. unsigned int irqs_unhandled;
  9575. atomic_t threads_handled;
  9576. int threads_handled_last;
  9577. + u64 random_ip;
  9578. raw_spinlock_t lock;
  9579. struct cpumask *percpu_enabled;
  9580. #ifdef CONFIG_SMP
  9581. diff -Nur linux-4.1.20.orig/include/linux/irqflags.h linux-4.1.20/include/linux/irqflags.h
  9582. --- linux-4.1.20.orig/include/linux/irqflags.h 2016-03-17 19:11:03.000000000 +0100
  9583. +++ linux-4.1.20/include/linux/irqflags.h 2016-03-21 20:18:33.000000000 +0100
  9584. @@ -25,8 +25,6 @@
  9585. # define trace_softirqs_enabled(p) ((p)->softirqs_enabled)
  9586. # define trace_hardirq_enter() do { current->hardirq_context++; } while (0)
  9587. # define trace_hardirq_exit() do { current->hardirq_context--; } while (0)
  9588. -# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  9589. -# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  9590. # define INIT_TRACE_IRQFLAGS .softirqs_enabled = 1,
  9591. #else
  9592. # define trace_hardirqs_on() do { } while (0)
  9593. @@ -39,9 +37,15 @@
  9594. # define trace_softirqs_enabled(p) 0
  9595. # define trace_hardirq_enter() do { } while (0)
  9596. # define trace_hardirq_exit() do { } while (0)
  9597. +# define INIT_TRACE_IRQFLAGS
  9598. +#endif
  9599. +
  9600. +#if defined(CONFIG_TRACE_IRQFLAGS) && !defined(CONFIG_PREEMPT_RT_FULL)
  9601. +# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  9602. +# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  9603. +#else
  9604. # define lockdep_softirq_enter() do { } while (0)
  9605. # define lockdep_softirq_exit() do { } while (0)
  9606. -# define INIT_TRACE_IRQFLAGS
  9607. #endif
  9608. #if defined(CONFIG_IRQSOFF_TRACER) || \
  9609. @@ -148,4 +152,23 @@
  9610. #define irqs_disabled_flags(flags) raw_irqs_disabled_flags(flags)
  9611. +/*
  9612. + * local_irq* variants depending on RT/!RT
  9613. + */
  9614. +#ifdef CONFIG_PREEMPT_RT_FULL
  9615. +# define local_irq_disable_nort() do { } while (0)
  9616. +# define local_irq_enable_nort() do { } while (0)
  9617. +# define local_irq_save_nort(flags) local_save_flags(flags)
  9618. +# define local_irq_restore_nort(flags) (void)(flags)
  9619. +# define local_irq_disable_rt() local_irq_disable()
  9620. +# define local_irq_enable_rt() local_irq_enable()
  9621. +#else
  9622. +# define local_irq_disable_nort() local_irq_disable()
  9623. +# define local_irq_enable_nort() local_irq_enable()
  9624. +# define local_irq_save_nort(flags) local_irq_save(flags)
  9625. +# define local_irq_restore_nort(flags) local_irq_restore(flags)
  9626. +# define local_irq_disable_rt() do { } while (0)
  9627. +# define local_irq_enable_rt() do { } while (0)
  9628. +#endif
  9629. +
  9630. #endif
  9631. diff -Nur linux-4.1.20.orig/include/linux/jbd_common.h linux-4.1.20/include/linux/jbd_common.h
  9632. --- linux-4.1.20.orig/include/linux/jbd_common.h 2016-03-17 19:11:03.000000000 +0100
  9633. +++ linux-4.1.20/include/linux/jbd_common.h 2016-03-21 20:18:33.000000000 +0100
  9634. @@ -15,32 +15,56 @@
  9635. static inline void jbd_lock_bh_state(struct buffer_head *bh)
  9636. {
  9637. +#ifndef CONFIG_PREEMPT_RT_BASE
  9638. bit_spin_lock(BH_State, &bh->b_state);
  9639. +#else
  9640. + spin_lock(&bh->b_state_lock);
  9641. +#endif
  9642. }
  9643. static inline int jbd_trylock_bh_state(struct buffer_head *bh)
  9644. {
  9645. +#ifndef CONFIG_PREEMPT_RT_BASE
  9646. return bit_spin_trylock(BH_State, &bh->b_state);
  9647. +#else
  9648. + return spin_trylock(&bh->b_state_lock);
  9649. +#endif
  9650. }
  9651. static inline int jbd_is_locked_bh_state(struct buffer_head *bh)
  9652. {
  9653. +#ifndef CONFIG_PREEMPT_RT_BASE
  9654. return bit_spin_is_locked(BH_State, &bh->b_state);
  9655. +#else
  9656. + return spin_is_locked(&bh->b_state_lock);
  9657. +#endif
  9658. }
  9659. static inline void jbd_unlock_bh_state(struct buffer_head *bh)
  9660. {
  9661. +#ifndef CONFIG_PREEMPT_RT_BASE
  9662. bit_spin_unlock(BH_State, &bh->b_state);
  9663. +#else
  9664. + spin_unlock(&bh->b_state_lock);
  9665. +#endif
  9666. }
  9667. static inline void jbd_lock_bh_journal_head(struct buffer_head *bh)
  9668. {
  9669. +#ifndef CONFIG_PREEMPT_RT_BASE
  9670. bit_spin_lock(BH_JournalHead, &bh->b_state);
  9671. +#else
  9672. + spin_lock(&bh->b_journal_head_lock);
  9673. +#endif
  9674. }
  9675. static inline void jbd_unlock_bh_journal_head(struct buffer_head *bh)
  9676. {
  9677. +#ifndef CONFIG_PREEMPT_RT_BASE
  9678. bit_spin_unlock(BH_JournalHead, &bh->b_state);
  9679. +#else
  9680. + spin_unlock(&bh->b_journal_head_lock);
  9681. +#endif
  9682. }
  9683. #endif
  9684. diff -Nur linux-4.1.20.orig/include/linux/kdb.h linux-4.1.20/include/linux/kdb.h
  9685. --- linux-4.1.20.orig/include/linux/kdb.h 2016-03-17 19:11:03.000000000 +0100
  9686. +++ linux-4.1.20/include/linux/kdb.h 2016-03-21 20:18:33.000000000 +0100
  9687. @@ -167,6 +167,7 @@
  9688. extern __printf(1, 2) int kdb_printf(const char *, ...);
  9689. typedef __printf(1, 2) int (*kdb_printf_t)(const char *, ...);
  9690. +#define in_kdb_printk() (kdb_trap_printk)
  9691. extern void kdb_init(int level);
  9692. /* Access to kdb specific polling devices */
  9693. @@ -201,6 +202,7 @@
  9694. extern int kdb_unregister(char *);
  9695. #else /* ! CONFIG_KGDB_KDB */
  9696. static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }
  9697. +#define in_kdb_printk() (0)
  9698. static inline void kdb_init(int level) {}
  9699. static inline int kdb_register(char *cmd, kdb_func_t func, char *usage,
  9700. char *help, short minlen) { return 0; }
  9701. diff -Nur linux-4.1.20.orig/include/linux/kernel.h linux-4.1.20/include/linux/kernel.h
  9702. --- linux-4.1.20.orig/include/linux/kernel.h 2016-03-17 19:11:03.000000000 +0100
  9703. +++ linux-4.1.20/include/linux/kernel.h 2016-03-21 20:18:33.000000000 +0100
  9704. @@ -188,6 +188,9 @@
  9705. */
  9706. # define might_sleep() \
  9707. do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  9708. +
  9709. +# define might_sleep_no_state_check() \
  9710. + do { ___might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  9711. # define sched_annotate_sleep() (current->task_state_change = 0)
  9712. #else
  9713. static inline void ___might_sleep(const char *file, int line,
  9714. @@ -195,6 +198,7 @@
  9715. static inline void __might_sleep(const char *file, int line,
  9716. int preempt_offset) { }
  9717. # define might_sleep() do { might_resched(); } while (0)
  9718. +# define might_sleep_no_state_check() do { might_resched(); } while (0)
  9719. # define sched_annotate_sleep() do { } while (0)
  9720. #endif
  9721. @@ -244,7 +248,8 @@
  9722. #if defined(CONFIG_MMU) && \
  9723. (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
  9724. -void might_fault(void);
  9725. +#define might_fault() __might_fault(__FILE__, __LINE__)
  9726. +void __might_fault(const char *file, int line);
  9727. #else
  9728. static inline void might_fault(void) { }
  9729. #endif
  9730. @@ -466,6 +471,7 @@
  9731. SYSTEM_HALT,
  9732. SYSTEM_POWER_OFF,
  9733. SYSTEM_RESTART,
  9734. + SYSTEM_SUSPEND,
  9735. } system_state;
  9736. #define TAINT_PROPRIETARY_MODULE 0
  9737. diff -Nur linux-4.1.20.orig/include/linux/kvm_host.h linux-4.1.20/include/linux/kvm_host.h
  9738. --- linux-4.1.20.orig/include/linux/kvm_host.h 2016-03-17 19:11:03.000000000 +0100
  9739. +++ linux-4.1.20/include/linux/kvm_host.h 2016-03-21 20:18:33.000000000 +0100
  9740. @@ -230,7 +230,7 @@
  9741. int fpu_active;
  9742. int guest_fpu_loaded, guest_xcr0_loaded;
  9743. - wait_queue_head_t wq;
  9744. + struct swait_head wq;
  9745. struct pid *pid;
  9746. int sigset_active;
  9747. sigset_t sigset;
  9748. @@ -701,7 +701,7 @@
  9749. }
  9750. #endif
  9751. -static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  9752. +static inline struct swait_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  9753. {
  9754. #ifdef __KVM_HAVE_ARCH_WQP
  9755. return vcpu->arch.wqp;
  9756. diff -Nur linux-4.1.20.orig/include/linux/lglock.h linux-4.1.20/include/linux/lglock.h
  9757. --- linux-4.1.20.orig/include/linux/lglock.h 2016-03-17 19:11:03.000000000 +0100
  9758. +++ linux-4.1.20/include/linux/lglock.h 2016-03-21 20:18:33.000000000 +0100
  9759. @@ -34,22 +34,39 @@
  9760. #endif
  9761. struct lglock {
  9762. +#ifndef CONFIG_PREEMPT_RT_FULL
  9763. arch_spinlock_t __percpu *lock;
  9764. +#else
  9765. + struct rt_mutex __percpu *lock;
  9766. +#endif
  9767. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  9768. struct lock_class_key lock_key;
  9769. struct lockdep_map lock_dep_map;
  9770. #endif
  9771. };
  9772. -#define DEFINE_LGLOCK(name) \
  9773. +#ifndef CONFIG_PREEMPT_RT_FULL
  9774. +# define DEFINE_LGLOCK(name) \
  9775. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  9776. = __ARCH_SPIN_LOCK_UNLOCKED; \
  9777. struct lglock name = { .lock = &name ## _lock }
  9778. -#define DEFINE_STATIC_LGLOCK(name) \
  9779. +# define DEFINE_STATIC_LGLOCK(name) \
  9780. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  9781. = __ARCH_SPIN_LOCK_UNLOCKED; \
  9782. static struct lglock name = { .lock = &name ## _lock }
  9783. +#else
  9784. +
  9785. +# define DEFINE_LGLOCK(name) \
  9786. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  9787. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  9788. + struct lglock name = { .lock = &name ## _lock }
  9789. +
  9790. +# define DEFINE_STATIC_LGLOCK(name) \
  9791. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  9792. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  9793. + static struct lglock name = { .lock = &name ## _lock }
  9794. +#endif
  9795. void lg_lock_init(struct lglock *lg, char *name);
  9796. void lg_local_lock(struct lglock *lg);
  9797. @@ -59,6 +76,12 @@
  9798. void lg_global_lock(struct lglock *lg);
  9799. void lg_global_unlock(struct lglock *lg);
  9800. +#ifndef CONFIG_PREEMPT_RT_FULL
  9801. +#define lg_global_trylock_relax(name) lg_global_lock(name)
  9802. +#else
  9803. +void lg_global_trylock_relax(struct lglock *lg);
  9804. +#endif
  9805. +
  9806. #else
  9807. /* When !CONFIG_SMP, map lglock to spinlock */
  9808. #define lglock spinlock
  9809. diff -Nur linux-4.1.20.orig/include/linux/list_bl.h linux-4.1.20/include/linux/list_bl.h
  9810. --- linux-4.1.20.orig/include/linux/list_bl.h 2016-03-17 19:11:03.000000000 +0100
  9811. +++ linux-4.1.20/include/linux/list_bl.h 2016-03-21 20:18:33.000000000 +0100
  9812. @@ -2,6 +2,7 @@
  9813. #define _LINUX_LIST_BL_H
  9814. #include <linux/list.h>
  9815. +#include <linux/spinlock.h>
  9816. #include <linux/bit_spinlock.h>
  9817. /*
  9818. @@ -32,13 +33,22 @@
  9819. struct hlist_bl_head {
  9820. struct hlist_bl_node *first;
  9821. +#ifdef CONFIG_PREEMPT_RT_BASE
  9822. + raw_spinlock_t lock;
  9823. +#endif
  9824. };
  9825. struct hlist_bl_node {
  9826. struct hlist_bl_node *next, **pprev;
  9827. };
  9828. -#define INIT_HLIST_BL_HEAD(ptr) \
  9829. - ((ptr)->first = NULL)
  9830. +
  9831. +static inline void INIT_HLIST_BL_HEAD(struct hlist_bl_head *h)
  9832. +{
  9833. + h->first = NULL;
  9834. +#ifdef CONFIG_PREEMPT_RT_BASE
  9835. + raw_spin_lock_init(&h->lock);
  9836. +#endif
  9837. +}
  9838. static inline void INIT_HLIST_BL_NODE(struct hlist_bl_node *h)
  9839. {
  9840. @@ -117,12 +127,26 @@
  9841. static inline void hlist_bl_lock(struct hlist_bl_head *b)
  9842. {
  9843. +#ifndef CONFIG_PREEMPT_RT_BASE
  9844. bit_spin_lock(0, (unsigned long *)b);
  9845. +#else
  9846. + raw_spin_lock(&b->lock);
  9847. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  9848. + __set_bit(0, (unsigned long *)b);
  9849. +#endif
  9850. +#endif
  9851. }
  9852. static inline void hlist_bl_unlock(struct hlist_bl_head *b)
  9853. {
  9854. +#ifndef CONFIG_PREEMPT_RT_BASE
  9855. __bit_spin_unlock(0, (unsigned long *)b);
  9856. +#else
  9857. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  9858. + __clear_bit(0, (unsigned long *)b);
  9859. +#endif
  9860. + raw_spin_unlock(&b->lock);
  9861. +#endif
  9862. }
  9863. static inline bool hlist_bl_is_locked(struct hlist_bl_head *b)
  9864. diff -Nur linux-4.1.20.orig/include/linux/locallock.h linux-4.1.20/include/linux/locallock.h
  9865. --- linux-4.1.20.orig/include/linux/locallock.h 1970-01-01 01:00:00.000000000 +0100
  9866. +++ linux-4.1.20/include/linux/locallock.h 2016-03-21 20:18:33.000000000 +0100
  9867. @@ -0,0 +1,270 @@
  9868. +#ifndef _LINUX_LOCALLOCK_H
  9869. +#define _LINUX_LOCALLOCK_H
  9870. +
  9871. +#include <linux/percpu.h>
  9872. +#include <linux/spinlock.h>
  9873. +
  9874. +#ifdef CONFIG_PREEMPT_RT_BASE
  9875. +
  9876. +#ifdef CONFIG_DEBUG_SPINLOCK
  9877. +# define LL_WARN(cond) WARN_ON(cond)
  9878. +#else
  9879. +# define LL_WARN(cond) do { } while (0)
  9880. +#endif
  9881. +
  9882. +/*
  9883. + * per cpu lock based substitute for local_irq_*()
  9884. + */
  9885. +struct local_irq_lock {
  9886. + spinlock_t lock;
  9887. + struct task_struct *owner;
  9888. + int nestcnt;
  9889. + unsigned long flags;
  9890. +};
  9891. +
  9892. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) \
  9893. + DEFINE_PER_CPU(struct local_irq_lock, lvar) = { \
  9894. + .lock = __SPIN_LOCK_UNLOCKED((lvar).lock) }
  9895. +
  9896. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) \
  9897. + DECLARE_PER_CPU(struct local_irq_lock, lvar)
  9898. +
  9899. +#define local_irq_lock_init(lvar) \
  9900. + do { \
  9901. + int __cpu; \
  9902. + for_each_possible_cpu(__cpu) \
  9903. + spin_lock_init(&per_cpu(lvar, __cpu).lock); \
  9904. + } while (0)
  9905. +
  9906. +/*
  9907. + * spin_lock|trylock|unlock_local flavour that does not migrate disable
  9908. + * used for __local_lock|trylock|unlock where get_local_var/put_local_var
  9909. + * already takes care of the migrate_disable/enable
  9910. + * for CONFIG_PREEMPT_BASE map to the normal spin_* calls.
  9911. + */
  9912. +#ifdef CONFIG_PREEMPT_RT_FULL
  9913. +# define spin_lock_local(lock) rt_spin_lock(lock)
  9914. +# define spin_trylock_local(lock) rt_spin_trylock(lock)
  9915. +# define spin_unlock_local(lock) rt_spin_unlock(lock)
  9916. +#else
  9917. +# define spin_lock_local(lock) spin_lock(lock)
  9918. +# define spin_trylock_local(lock) spin_trylock(lock)
  9919. +# define spin_unlock_local(lock) spin_unlock(lock)
  9920. +#endif
  9921. +
  9922. +static inline void __local_lock(struct local_irq_lock *lv)
  9923. +{
  9924. + if (lv->owner != current) {
  9925. + spin_lock_local(&lv->lock);
  9926. + LL_WARN(lv->owner);
  9927. + LL_WARN(lv->nestcnt);
  9928. + lv->owner = current;
  9929. + }
  9930. + lv->nestcnt++;
  9931. +}
  9932. +
  9933. +#define local_lock(lvar) \
  9934. + do { __local_lock(&get_local_var(lvar)); } while (0)
  9935. +
  9936. +static inline int __local_trylock(struct local_irq_lock *lv)
  9937. +{
  9938. + if (lv->owner != current && spin_trylock_local(&lv->lock)) {
  9939. + LL_WARN(lv->owner);
  9940. + LL_WARN(lv->nestcnt);
  9941. + lv->owner = current;
  9942. + lv->nestcnt = 1;
  9943. + return 1;
  9944. + }
  9945. + return 0;
  9946. +}
  9947. +
  9948. +#define local_trylock(lvar) \
  9949. + ({ \
  9950. + int __locked; \
  9951. + __locked = __local_trylock(&get_local_var(lvar)); \
  9952. + if (!__locked) \
  9953. + put_local_var(lvar); \
  9954. + __locked; \
  9955. + })
  9956. +
  9957. +static inline void __local_unlock(struct local_irq_lock *lv)
  9958. +{
  9959. + LL_WARN(lv->nestcnt == 0);
  9960. + LL_WARN(lv->owner != current);
  9961. + if (--lv->nestcnt)
  9962. + return;
  9963. +
  9964. + lv->owner = NULL;
  9965. + spin_unlock_local(&lv->lock);
  9966. +}
  9967. +
  9968. +#define local_unlock(lvar) \
  9969. + do { \
  9970. + __local_unlock(this_cpu_ptr(&lvar)); \
  9971. + put_local_var(lvar); \
  9972. + } while (0)
  9973. +
  9974. +static inline void __local_lock_irq(struct local_irq_lock *lv)
  9975. +{
  9976. + spin_lock_irqsave(&lv->lock, lv->flags);
  9977. + LL_WARN(lv->owner);
  9978. + LL_WARN(lv->nestcnt);
  9979. + lv->owner = current;
  9980. + lv->nestcnt = 1;
  9981. +}
  9982. +
  9983. +#define local_lock_irq(lvar) \
  9984. + do { __local_lock_irq(&get_local_var(lvar)); } while (0)
  9985. +
  9986. +#define local_lock_irq_on(lvar, cpu) \
  9987. + do { __local_lock_irq(&per_cpu(lvar, cpu)); } while (0)
  9988. +
  9989. +static inline void __local_unlock_irq(struct local_irq_lock *lv)
  9990. +{
  9991. + LL_WARN(!lv->nestcnt);
  9992. + LL_WARN(lv->owner != current);
  9993. + lv->owner = NULL;
  9994. + lv->nestcnt = 0;
  9995. + spin_unlock_irq(&lv->lock);
  9996. +}
  9997. +
  9998. +#define local_unlock_irq(lvar) \
  9999. + do { \
  10000. + __local_unlock_irq(this_cpu_ptr(&lvar)); \
  10001. + put_local_var(lvar); \
  10002. + } while (0)
  10003. +
  10004. +#define local_unlock_irq_on(lvar, cpu) \
  10005. + do { \
  10006. + __local_unlock_irq(&per_cpu(lvar, cpu)); \
  10007. + } while (0)
  10008. +
  10009. +static inline int __local_lock_irqsave(struct local_irq_lock *lv)
  10010. +{
  10011. + if (lv->owner != current) {
  10012. + __local_lock_irq(lv);
  10013. + return 0;
  10014. + } else {
  10015. + lv->nestcnt++;
  10016. + return 1;
  10017. + }
  10018. +}
  10019. +
  10020. +#define local_lock_irqsave(lvar, _flags) \
  10021. + do { \
  10022. + if (__local_lock_irqsave(&get_local_var(lvar))) \
  10023. + put_local_var(lvar); \
  10024. + _flags = __this_cpu_read(lvar.flags); \
  10025. + } while (0)
  10026. +
  10027. +#define local_lock_irqsave_on(lvar, _flags, cpu) \
  10028. + do { \
  10029. + __local_lock_irqsave(&per_cpu(lvar, cpu)); \
  10030. + _flags = per_cpu(lvar, cpu).flags; \
  10031. + } while (0)
  10032. +
  10033. +static inline int __local_unlock_irqrestore(struct local_irq_lock *lv,
  10034. + unsigned long flags)
  10035. +{
  10036. + LL_WARN(!lv->nestcnt);
  10037. + LL_WARN(lv->owner != current);
  10038. + if (--lv->nestcnt)
  10039. + return 0;
  10040. +
  10041. + lv->owner = NULL;
  10042. + spin_unlock_irqrestore(&lv->lock, lv->flags);
  10043. + return 1;
  10044. +}
  10045. +
  10046. +#define local_unlock_irqrestore(lvar, flags) \
  10047. + do { \
  10048. + if (__local_unlock_irqrestore(this_cpu_ptr(&lvar), flags)) \
  10049. + put_local_var(lvar); \
  10050. + } while (0)
  10051. +
  10052. +#define local_unlock_irqrestore_on(lvar, flags, cpu) \
  10053. + do { \
  10054. + __local_unlock_irqrestore(&per_cpu(lvar, cpu), flags); \
  10055. + } while (0)
  10056. +
  10057. +#define local_spin_trylock_irq(lvar, lock) \
  10058. + ({ \
  10059. + int __locked; \
  10060. + local_lock_irq(lvar); \
  10061. + __locked = spin_trylock(lock); \
  10062. + if (!__locked) \
  10063. + local_unlock_irq(lvar); \
  10064. + __locked; \
  10065. + })
  10066. +
  10067. +#define local_spin_lock_irq(lvar, lock) \
  10068. + do { \
  10069. + local_lock_irq(lvar); \
  10070. + spin_lock(lock); \
  10071. + } while (0)
  10072. +
  10073. +#define local_spin_unlock_irq(lvar, lock) \
  10074. + do { \
  10075. + spin_unlock(lock); \
  10076. + local_unlock_irq(lvar); \
  10077. + } while (0)
  10078. +
  10079. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  10080. + do { \
  10081. + local_lock_irqsave(lvar, flags); \
  10082. + spin_lock(lock); \
  10083. + } while (0)
  10084. +
  10085. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  10086. + do { \
  10087. + spin_unlock(lock); \
  10088. + local_unlock_irqrestore(lvar, flags); \
  10089. + } while (0)
  10090. +
  10091. +#define get_locked_var(lvar, var) \
  10092. + (*({ \
  10093. + local_lock(lvar); \
  10094. + this_cpu_ptr(&var); \
  10095. + }))
  10096. +
  10097. +#define put_locked_var(lvar, var) local_unlock(lvar);
  10098. +
  10099. +#define local_lock_cpu(lvar) \
  10100. + ({ \
  10101. + local_lock(lvar); \
  10102. + smp_processor_id(); \
  10103. + })
  10104. +
  10105. +#define local_unlock_cpu(lvar) local_unlock(lvar)
  10106. +
  10107. +#else /* PREEMPT_RT_BASE */
  10108. +
  10109. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) __typeof__(const int) lvar
  10110. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) extern __typeof__(const int) lvar
  10111. +
  10112. +static inline void local_irq_lock_init(int lvar) { }
  10113. +
  10114. +#define local_lock(lvar) preempt_disable()
  10115. +#define local_unlock(lvar) preempt_enable()
  10116. +#define local_lock_irq(lvar) local_irq_disable()
  10117. +#define local_unlock_irq(lvar) local_irq_enable()
  10118. +#define local_lock_irqsave(lvar, flags) local_irq_save(flags)
  10119. +#define local_unlock_irqrestore(lvar, flags) local_irq_restore(flags)
  10120. +
  10121. +#define local_spin_trylock_irq(lvar, lock) spin_trylock_irq(lock)
  10122. +#define local_spin_lock_irq(lvar, lock) spin_lock_irq(lock)
  10123. +#define local_spin_unlock_irq(lvar, lock) spin_unlock_irq(lock)
  10124. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  10125. + spin_lock_irqsave(lock, flags)
  10126. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  10127. + spin_unlock_irqrestore(lock, flags)
  10128. +
  10129. +#define get_locked_var(lvar, var) get_cpu_var(var)
  10130. +#define put_locked_var(lvar, var) put_cpu_var(var)
  10131. +
  10132. +#define local_lock_cpu(lvar) get_cpu()
  10133. +#define local_unlock_cpu(lvar) put_cpu()
  10134. +
  10135. +#endif
  10136. +
  10137. +#endif
  10138. diff -Nur linux-4.1.20.orig/include/linux/mm_types.h linux-4.1.20/include/linux/mm_types.h
  10139. --- linux-4.1.20.orig/include/linux/mm_types.h 2016-03-17 19:11:03.000000000 +0100
  10140. +++ linux-4.1.20/include/linux/mm_types.h 2016-03-21 20:18:33.000000000 +0100
  10141. @@ -11,6 +11,7 @@
  10142. #include <linux/completion.h>
  10143. #include <linux/cpumask.h>
  10144. #include <linux/uprobes.h>
  10145. +#include <linux/rcupdate.h>
  10146. #include <linux/page-flags-layout.h>
  10147. #include <asm/page.h>
  10148. #include <asm/mmu.h>
  10149. @@ -453,6 +454,9 @@
  10150. bool tlb_flush_pending;
  10151. #endif
  10152. struct uprobes_state uprobes_state;
  10153. +#ifdef CONFIG_PREEMPT_RT_BASE
  10154. + struct rcu_head delayed_drop;
  10155. +#endif
  10156. #ifdef CONFIG_X86_INTEL_MPX
  10157. /* address of the bounds directory */
  10158. void __user *bd_addr;
  10159. diff -Nur linux-4.1.20.orig/include/linux/mutex.h linux-4.1.20/include/linux/mutex.h
  10160. --- linux-4.1.20.orig/include/linux/mutex.h 2016-03-17 19:11:03.000000000 +0100
  10161. +++ linux-4.1.20/include/linux/mutex.h 2016-03-21 20:18:33.000000000 +0100
  10162. @@ -19,6 +19,17 @@
  10163. #include <asm/processor.h>
  10164. #include <linux/osq_lock.h>
  10165. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10166. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  10167. + , .dep_map = { .name = #lockname }
  10168. +#else
  10169. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  10170. +#endif
  10171. +
  10172. +#ifdef CONFIG_PREEMPT_RT_FULL
  10173. +# include <linux/mutex_rt.h>
  10174. +#else
  10175. +
  10176. /*
  10177. * Simple, straightforward mutexes with strict semantics:
  10178. *
  10179. @@ -99,13 +110,6 @@
  10180. static inline void mutex_destroy(struct mutex *lock) {}
  10181. #endif
  10182. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10183. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  10184. - , .dep_map = { .name = #lockname }
  10185. -#else
  10186. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  10187. -#endif
  10188. -
  10189. #define __MUTEX_INITIALIZER(lockname) \
  10190. { .count = ATOMIC_INIT(1) \
  10191. , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  10192. @@ -173,6 +177,8 @@
  10193. extern int mutex_trylock(struct mutex *lock);
  10194. extern void mutex_unlock(struct mutex *lock);
  10195. +#endif /* !PREEMPT_RT_FULL */
  10196. +
  10197. extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
  10198. #endif /* __LINUX_MUTEX_H */
  10199. diff -Nur linux-4.1.20.orig/include/linux/mutex_rt.h linux-4.1.20/include/linux/mutex_rt.h
  10200. --- linux-4.1.20.orig/include/linux/mutex_rt.h 1970-01-01 01:00:00.000000000 +0100
  10201. +++ linux-4.1.20/include/linux/mutex_rt.h 2016-03-21 20:18:33.000000000 +0100
  10202. @@ -0,0 +1,84 @@
  10203. +#ifndef __LINUX_MUTEX_RT_H
  10204. +#define __LINUX_MUTEX_RT_H
  10205. +
  10206. +#ifndef __LINUX_MUTEX_H
  10207. +#error "Please include mutex.h"
  10208. +#endif
  10209. +
  10210. +#include <linux/rtmutex.h>
  10211. +
  10212. +/* FIXME: Just for __lockfunc */
  10213. +#include <linux/spinlock.h>
  10214. +
  10215. +struct mutex {
  10216. + struct rt_mutex lock;
  10217. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10218. + struct lockdep_map dep_map;
  10219. +#endif
  10220. +};
  10221. +
  10222. +#define __MUTEX_INITIALIZER(mutexname) \
  10223. + { \
  10224. + .lock = __RT_MUTEX_INITIALIZER(mutexname.lock) \
  10225. + __DEP_MAP_MUTEX_INITIALIZER(mutexname) \
  10226. + }
  10227. +
  10228. +#define DEFINE_MUTEX(mutexname) \
  10229. + struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  10230. +
  10231. +extern void __mutex_do_init(struct mutex *lock, const char *name, struct lock_class_key *key);
  10232. +extern void __lockfunc _mutex_lock(struct mutex *lock);
  10233. +extern int __lockfunc _mutex_lock_interruptible(struct mutex *lock);
  10234. +extern int __lockfunc _mutex_lock_killable(struct mutex *lock);
  10235. +extern void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass);
  10236. +extern void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
  10237. +extern int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass);
  10238. +extern int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass);
  10239. +extern int __lockfunc _mutex_trylock(struct mutex *lock);
  10240. +extern void __lockfunc _mutex_unlock(struct mutex *lock);
  10241. +
  10242. +#define mutex_is_locked(l) rt_mutex_is_locked(&(l)->lock)
  10243. +#define mutex_lock(l) _mutex_lock(l)
  10244. +#define mutex_lock_interruptible(l) _mutex_lock_interruptible(l)
  10245. +#define mutex_lock_killable(l) _mutex_lock_killable(l)
  10246. +#define mutex_trylock(l) _mutex_trylock(l)
  10247. +#define mutex_unlock(l) _mutex_unlock(l)
  10248. +#define mutex_destroy(l) rt_mutex_destroy(&(l)->lock)
  10249. +
  10250. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10251. +# define mutex_lock_nested(l, s) _mutex_lock_nested(l, s)
  10252. +# define mutex_lock_interruptible_nested(l, s) \
  10253. + _mutex_lock_interruptible_nested(l, s)
  10254. +# define mutex_lock_killable_nested(l, s) \
  10255. + _mutex_lock_killable_nested(l, s)
  10256. +
  10257. +# define mutex_lock_nest_lock(lock, nest_lock) \
  10258. +do { \
  10259. + typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  10260. + _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  10261. +} while (0)
  10262. +
  10263. +#else
  10264. +# define mutex_lock_nested(l, s) _mutex_lock(l)
  10265. +# define mutex_lock_interruptible_nested(l, s) \
  10266. + _mutex_lock_interruptible(l)
  10267. +# define mutex_lock_killable_nested(l, s) \
  10268. + _mutex_lock_killable(l)
  10269. +# define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
  10270. +#endif
  10271. +
  10272. +# define mutex_init(mutex) \
  10273. +do { \
  10274. + static struct lock_class_key __key; \
  10275. + \
  10276. + rt_mutex_init(&(mutex)->lock); \
  10277. + __mutex_do_init((mutex), #mutex, &__key); \
  10278. +} while (0)
  10279. +
  10280. +# define __mutex_init(mutex, name, key) \
  10281. +do { \
  10282. + rt_mutex_init(&(mutex)->lock); \
  10283. + __mutex_do_init((mutex), name, key); \
  10284. +} while (0)
  10285. +
  10286. +#endif
  10287. diff -Nur linux-4.1.20.orig/include/linux/netdevice.h linux-4.1.20/include/linux/netdevice.h
  10288. --- linux-4.1.20.orig/include/linux/netdevice.h 2016-03-17 19:11:03.000000000 +0100
  10289. +++ linux-4.1.20/include/linux/netdevice.h 2016-03-21 20:18:33.000000000 +0100
  10290. @@ -2192,11 +2192,20 @@
  10291. void synchronize_net(void);
  10292. int init_dummy_netdev(struct net_device *dev);
  10293. +#ifdef CONFIG_PREEMPT_RT_FULL
  10294. +static inline int dev_recursion_level(void)
  10295. +{
  10296. + return current->xmit_recursion;
  10297. +}
  10298. +
  10299. +#else
  10300. +
  10301. DECLARE_PER_CPU(int, xmit_recursion);
  10302. static inline int dev_recursion_level(void)
  10303. {
  10304. return this_cpu_read(xmit_recursion);
  10305. }
  10306. +#endif
  10307. struct net_device *dev_get_by_index(struct net *net, int ifindex);
  10308. struct net_device *__dev_get_by_index(struct net *net, int ifindex);
  10309. @@ -2469,6 +2478,7 @@
  10310. unsigned int dropped;
  10311. struct sk_buff_head input_pkt_queue;
  10312. struct napi_struct backlog;
  10313. + struct sk_buff_head tofree_queue;
  10314. };
  10315. diff -Nur linux-4.1.20.orig/include/linux/netfilter/x_tables.h linux-4.1.20/include/linux/netfilter/x_tables.h
  10316. --- linux-4.1.20.orig/include/linux/netfilter/x_tables.h 2016-03-17 19:11:03.000000000 +0100
  10317. +++ linux-4.1.20/include/linux/netfilter/x_tables.h 2016-03-21 20:18:33.000000000 +0100
  10318. @@ -3,6 +3,7 @@
  10319. #include <linux/netdevice.h>
  10320. +#include <linux/locallock.h>
  10321. #include <uapi/linux/netfilter/x_tables.h>
  10322. /**
  10323. @@ -282,6 +283,8 @@
  10324. */
  10325. DECLARE_PER_CPU(seqcount_t, xt_recseq);
  10326. +DECLARE_LOCAL_IRQ_LOCK(xt_write_lock);
  10327. +
  10328. /**
  10329. * xt_write_recseq_begin - start of a write section
  10330. *
  10331. @@ -296,6 +299,9 @@
  10332. {
  10333. unsigned int addend;
  10334. + /* RT protection */
  10335. + local_lock(xt_write_lock);
  10336. +
  10337. /*
  10338. * Low order bit of sequence is set if we already
  10339. * called xt_write_recseq_begin().
  10340. @@ -326,6 +332,7 @@
  10341. /* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
  10342. smp_wmb();
  10343. __this_cpu_add(xt_recseq.sequence, addend);
  10344. + local_unlock(xt_write_lock);
  10345. }
  10346. /*
  10347. diff -Nur linux-4.1.20.orig/include/linux/notifier.h linux-4.1.20/include/linux/notifier.h
  10348. --- linux-4.1.20.orig/include/linux/notifier.h 2016-03-17 19:11:03.000000000 +0100
  10349. +++ linux-4.1.20/include/linux/notifier.h 2016-03-21 20:18:33.000000000 +0100
  10350. @@ -6,7 +6,7 @@
  10351. *
  10352. * Alan Cox <Alan.Cox@linux.org>
  10353. */
  10354. -
  10355. +
  10356. #ifndef _LINUX_NOTIFIER_H
  10357. #define _LINUX_NOTIFIER_H
  10358. #include <linux/errno.h>
  10359. @@ -42,9 +42,7 @@
  10360. * in srcu_notifier_call_chain(): no cache bounces and no memory barriers.
  10361. * As compensation, srcu_notifier_chain_unregister() is rather expensive.
  10362. * SRCU notifier chains should be used when the chain will be called very
  10363. - * often but notifier_blocks will seldom be removed. Also, SRCU notifier
  10364. - * chains are slightly more difficult to use because they require special
  10365. - * runtime initialization.
  10366. + * often but notifier_blocks will seldom be removed.
  10367. */
  10368. typedef int (*notifier_fn_t)(struct notifier_block *nb,
  10369. @@ -88,7 +86,7 @@
  10370. (name)->head = NULL; \
  10371. } while (0)
  10372. -/* srcu_notifier_heads must be initialized and cleaned up dynamically */
  10373. +/* srcu_notifier_heads must be cleaned up dynamically */
  10374. extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  10375. #define srcu_cleanup_notifier_head(name) \
  10376. cleanup_srcu_struct(&(name)->srcu);
  10377. @@ -101,7 +99,13 @@
  10378. .head = NULL }
  10379. #define RAW_NOTIFIER_INIT(name) { \
  10380. .head = NULL }
  10381. -/* srcu_notifier_heads cannot be initialized statically */
  10382. +
  10383. +#define SRCU_NOTIFIER_INIT(name, pcpu) \
  10384. + { \
  10385. + .mutex = __MUTEX_INITIALIZER(name.mutex), \
  10386. + .head = NULL, \
  10387. + .srcu = __SRCU_STRUCT_INIT(name.srcu, pcpu), \
  10388. + }
  10389. #define ATOMIC_NOTIFIER_HEAD(name) \
  10390. struct atomic_notifier_head name = \
  10391. @@ -113,6 +117,18 @@
  10392. struct raw_notifier_head name = \
  10393. RAW_NOTIFIER_INIT(name)
  10394. +#define _SRCU_NOTIFIER_HEAD(name, mod) \
  10395. + static DEFINE_PER_CPU(struct srcu_struct_array, \
  10396. + name##_head_srcu_array); \
  10397. + mod struct srcu_notifier_head name = \
  10398. + SRCU_NOTIFIER_INIT(name, name##_head_srcu_array)
  10399. +
  10400. +#define SRCU_NOTIFIER_HEAD(name) \
  10401. + _SRCU_NOTIFIER_HEAD(name, )
  10402. +
  10403. +#define SRCU_NOTIFIER_HEAD_STATIC(name) \
  10404. + _SRCU_NOTIFIER_HEAD(name, static)
  10405. +
  10406. #ifdef __KERNEL__
  10407. extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
  10408. @@ -182,12 +198,12 @@
  10409. /*
  10410. * Declared notifiers so far. I can imagine quite a few more chains
  10411. - * over time (eg laptop power reset chains, reboot chain (to clean
  10412. + * over time (eg laptop power reset chains, reboot chain (to clean
  10413. * device units up), device [un]mount chain, module load/unload chain,
  10414. - * low memory chain, screenblank chain (for plug in modular screenblankers)
  10415. + * low memory chain, screenblank chain (for plug in modular screenblankers)
  10416. * VC switch chains (for loadable kernel svgalib VC switch helpers) etc...
  10417. */
  10418. -
  10419. +
  10420. /* CPU notfiers are defined in include/linux/cpu.h. */
  10421. /* netdevice notifiers are defined in include/linux/netdevice.h */
  10422. diff -Nur linux-4.1.20.orig/include/linux/percpu.h linux-4.1.20/include/linux/percpu.h
  10423. --- linux-4.1.20.orig/include/linux/percpu.h 2016-03-17 19:11:03.000000000 +0100
  10424. +++ linux-4.1.20/include/linux/percpu.h 2016-03-21 20:18:33.000000000 +0100
  10425. @@ -24,6 +24,35 @@
  10426. PERCPU_MODULE_RESERVE)
  10427. #endif
  10428. +#ifdef CONFIG_PREEMPT_RT_FULL
  10429. +
  10430. +#define get_local_var(var) (*({ \
  10431. + migrate_disable(); \
  10432. + this_cpu_ptr(&var); }))
  10433. +
  10434. +#define put_local_var(var) do { \
  10435. + (void)&(var); \
  10436. + migrate_enable(); \
  10437. +} while (0)
  10438. +
  10439. +# define get_local_ptr(var) ({ \
  10440. + migrate_disable(); \
  10441. + this_cpu_ptr(var); })
  10442. +
  10443. +# define put_local_ptr(var) do { \
  10444. + (void)(var); \
  10445. + migrate_enable(); \
  10446. +} while (0)
  10447. +
  10448. +#else
  10449. +
  10450. +#define get_local_var(var) get_cpu_var(var)
  10451. +#define put_local_var(var) put_cpu_var(var)
  10452. +#define get_local_ptr(var) get_cpu_ptr(var)
  10453. +#define put_local_ptr(var) put_cpu_ptr(var)
  10454. +
  10455. +#endif
  10456. +
  10457. /* minimum unit size, also is the maximum supported allocation size */
  10458. #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
  10459. diff -Nur linux-4.1.20.orig/include/linux/pid.h linux-4.1.20/include/linux/pid.h
  10460. --- linux-4.1.20.orig/include/linux/pid.h 2016-03-17 19:11:03.000000000 +0100
  10461. +++ linux-4.1.20/include/linux/pid.h 2016-03-21 20:18:33.000000000 +0100
  10462. @@ -2,6 +2,7 @@
  10463. #define _LINUX_PID_H
  10464. #include <linux/rcupdate.h>
  10465. +#include <linux/atomic.h>
  10466. enum pid_type
  10467. {
  10468. diff -Nur linux-4.1.20.orig/include/linux/platform_data/gpio-omap.h linux-4.1.20/include/linux/platform_data/gpio-omap.h
  10469. --- linux-4.1.20.orig/include/linux/platform_data/gpio-omap.h 2016-03-17 19:11:03.000000000 +0100
  10470. +++ linux-4.1.20/include/linux/platform_data/gpio-omap.h 2016-03-21 20:18:33.000000000 +0100
  10471. @@ -198,7 +198,6 @@
  10472. int bank_width; /* GPIO bank width */
  10473. int bank_stride; /* Only needed for omap1 MPUIO */
  10474. bool dbck_flag; /* dbck required or not - True for OMAP3&4 */
  10475. - bool loses_context; /* whether the bank would ever lose context */
  10476. bool is_mpuio; /* whether the bank is of type MPUIO */
  10477. u32 non_wakeup_gpios;
  10478. @@ -208,9 +207,17 @@
  10479. int (*get_context_loss_count)(struct device *dev);
  10480. };
  10481. +#if IS_BUILTIN(CONFIG_GPIO_OMAP)
  10482. extern void omap2_gpio_prepare_for_idle(int off_mode);
  10483. extern void omap2_gpio_resume_after_idle(void);
  10484. -extern void omap_set_gpio_debounce(int gpio, int enable);
  10485. -extern void omap_set_gpio_debounce_time(int gpio, int enable);
  10486. +#else
  10487. +static inline void omap2_gpio_prepare_for_idle(int off_mode)
  10488. +{
  10489. +}
  10490. +
  10491. +static inline void omap2_gpio_resume_after_idle(void)
  10492. +{
  10493. +}
  10494. +#endif
  10495. #endif
  10496. diff -Nur linux-4.1.20.orig/include/linux/preempt.h linux-4.1.20/include/linux/preempt.h
  10497. --- linux-4.1.20.orig/include/linux/preempt.h 2016-03-17 19:11:03.000000000 +0100
  10498. +++ linux-4.1.20/include/linux/preempt.h 2016-03-21 20:18:33.000000000 +0100
  10499. @@ -34,6 +34,20 @@
  10500. #define preempt_count_inc() preempt_count_add(1)
  10501. #define preempt_count_dec() preempt_count_sub(1)
  10502. +#ifdef CONFIG_PREEMPT_LAZY
  10503. +#define add_preempt_lazy_count(val) do { preempt_lazy_count() += (val); } while (0)
  10504. +#define sub_preempt_lazy_count(val) do { preempt_lazy_count() -= (val); } while (0)
  10505. +#define inc_preempt_lazy_count() add_preempt_lazy_count(1)
  10506. +#define dec_preempt_lazy_count() sub_preempt_lazy_count(1)
  10507. +#define preempt_lazy_count() (current_thread_info()->preempt_lazy_count)
  10508. +#else
  10509. +#define add_preempt_lazy_count(val) do { } while (0)
  10510. +#define sub_preempt_lazy_count(val) do { } while (0)
  10511. +#define inc_preempt_lazy_count() do { } while (0)
  10512. +#define dec_preempt_lazy_count() do { } while (0)
  10513. +#define preempt_lazy_count() (0)
  10514. +#endif
  10515. +
  10516. #ifdef CONFIG_PREEMPT_COUNT
  10517. #define preempt_disable() \
  10518. @@ -42,13 +56,25 @@
  10519. barrier(); \
  10520. } while (0)
  10521. +#define preempt_lazy_disable() \
  10522. +do { \
  10523. + inc_preempt_lazy_count(); \
  10524. + barrier(); \
  10525. +} while (0)
  10526. +
  10527. #define sched_preempt_enable_no_resched() \
  10528. do { \
  10529. barrier(); \
  10530. preempt_count_dec(); \
  10531. } while (0)
  10532. -#define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  10533. +#ifdef CONFIG_PREEMPT_RT_BASE
  10534. +# define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  10535. +# define preempt_check_resched_rt() preempt_check_resched()
  10536. +#else
  10537. +# define preempt_enable_no_resched() preempt_enable()
  10538. +# define preempt_check_resched_rt() barrier();
  10539. +#endif
  10540. #ifdef CONFIG_PREEMPT
  10541. #define preempt_enable() \
  10542. @@ -64,6 +90,13 @@
  10543. __preempt_schedule(); \
  10544. } while (0)
  10545. +#define preempt_lazy_enable() \
  10546. +do { \
  10547. + dec_preempt_lazy_count(); \
  10548. + barrier(); \
  10549. + preempt_check_resched(); \
  10550. +} while (0)
  10551. +
  10552. #else
  10553. #define preempt_enable() \
  10554. do { \
  10555. @@ -122,6 +155,7 @@
  10556. #define preempt_disable_notrace() barrier()
  10557. #define preempt_enable_no_resched_notrace() barrier()
  10558. #define preempt_enable_notrace() barrier()
  10559. +#define preempt_check_resched_rt() barrier()
  10560. #endif /* CONFIG_PREEMPT_COUNT */
  10561. @@ -141,10 +175,31 @@
  10562. } while (0)
  10563. #define preempt_fold_need_resched() \
  10564. do { \
  10565. - if (tif_need_resched()) \
  10566. + if (tif_need_resched_now()) \
  10567. set_preempt_need_resched(); \
  10568. } while (0)
  10569. +#ifdef CONFIG_PREEMPT_RT_FULL
  10570. +# define preempt_disable_rt() preempt_disable()
  10571. +# define preempt_enable_rt() preempt_enable()
  10572. +# define preempt_disable_nort() barrier()
  10573. +# define preempt_enable_nort() barrier()
  10574. +# ifdef CONFIG_SMP
  10575. + extern void migrate_disable(void);
  10576. + extern void migrate_enable(void);
  10577. +# else /* CONFIG_SMP */
  10578. +# define migrate_disable() barrier()
  10579. +# define migrate_enable() barrier()
  10580. +# endif /* CONFIG_SMP */
  10581. +#else
  10582. +# define preempt_disable_rt() barrier()
  10583. +# define preempt_enable_rt() barrier()
  10584. +# define preempt_disable_nort() preempt_disable()
  10585. +# define preempt_enable_nort() preempt_enable()
  10586. +# define migrate_disable() preempt_disable()
  10587. +# define migrate_enable() preempt_enable()
  10588. +#endif
  10589. +
  10590. #ifdef CONFIG_PREEMPT_NOTIFIERS
  10591. struct preempt_notifier;
  10592. diff -Nur linux-4.1.20.orig/include/linux/preempt_mask.h linux-4.1.20/include/linux/preempt_mask.h
  10593. --- linux-4.1.20.orig/include/linux/preempt_mask.h 2016-03-17 19:11:03.000000000 +0100
  10594. +++ linux-4.1.20/include/linux/preempt_mask.h 2016-03-21 20:18:33.000000000 +0100
  10595. @@ -44,16 +44,26 @@
  10596. #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
  10597. #define NMI_OFFSET (1UL << NMI_SHIFT)
  10598. -#define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  10599. +#ifndef CONFIG_PREEMPT_RT_FULL
  10600. +# define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  10601. +#else
  10602. +# define SOFTIRQ_DISABLE_OFFSET (0)
  10603. +#endif
  10604. #define PREEMPT_ACTIVE_BITS 1
  10605. #define PREEMPT_ACTIVE_SHIFT (NMI_SHIFT + NMI_BITS)
  10606. #define PREEMPT_ACTIVE (__IRQ_MASK(PREEMPT_ACTIVE_BITS) << PREEMPT_ACTIVE_SHIFT)
  10607. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  10608. -#define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  10609. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
  10610. | NMI_MASK))
  10611. +#ifndef CONFIG_PREEMPT_RT_FULL
  10612. +# define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  10613. +# define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  10614. +#else
  10615. +# define softirq_count() (0UL)
  10616. +extern int in_serving_softirq(void);
  10617. +#endif
  10618. /*
  10619. * Are we doing bottom half or hardware interrupt processing?
  10620. @@ -64,7 +74,6 @@
  10621. #define in_irq() (hardirq_count())
  10622. #define in_softirq() (softirq_count())
  10623. #define in_interrupt() (irq_count())
  10624. -#define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  10625. /*
  10626. * Are we in NMI context?
  10627. @@ -83,7 +92,11 @@
  10628. /*
  10629. * The preempt_count offset after spin_lock()
  10630. */
  10631. +#if !defined(CONFIG_PREEMPT_RT_FULL)
  10632. #define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
  10633. +#else
  10634. +#define PREEMPT_LOCK_OFFSET 0
  10635. +#endif
  10636. /*
  10637. * The preempt_count offset needed for things like:
  10638. diff -Nur linux-4.1.20.orig/include/linux/printk.h linux-4.1.20/include/linux/printk.h
  10639. --- linux-4.1.20.orig/include/linux/printk.h 2016-03-17 19:11:03.000000000 +0100
  10640. +++ linux-4.1.20/include/linux/printk.h 2016-03-21 20:18:33.000000000 +0100
  10641. @@ -115,9 +115,11 @@
  10642. #ifdef CONFIG_EARLY_PRINTK
  10643. extern asmlinkage __printf(1, 2)
  10644. void early_printk(const char *fmt, ...);
  10645. +extern void printk_kill(void);
  10646. #else
  10647. static inline __printf(1, 2) __cold
  10648. void early_printk(const char *s, ...) { }
  10649. +static inline void printk_kill(void) { }
  10650. #endif
  10651. typedef int(*printk_func_t)(const char *fmt, va_list args);
  10652. diff -Nur linux-4.1.20.orig/include/linux/radix-tree.h linux-4.1.20/include/linux/radix-tree.h
  10653. --- linux-4.1.20.orig/include/linux/radix-tree.h 2016-03-17 19:11:03.000000000 +0100
  10654. +++ linux-4.1.20/include/linux/radix-tree.h 2016-03-21 20:18:33.000000000 +0100
  10655. @@ -277,8 +277,13 @@
  10656. unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  10657. void ***results, unsigned long *indices,
  10658. unsigned long first_index, unsigned int max_items);
  10659. +#ifndef CONFIG_PREEMPT_RT_FULL
  10660. int radix_tree_preload(gfp_t gfp_mask);
  10661. int radix_tree_maybe_preload(gfp_t gfp_mask);
  10662. +#else
  10663. +static inline int radix_tree_preload(gfp_t gm) { return 0; }
  10664. +static inline int radix_tree_maybe_preload(gfp_t gfp_mask) { return 0; }
  10665. +#endif
  10666. void radix_tree_init(void);
  10667. void *radix_tree_tag_set(struct radix_tree_root *root,
  10668. unsigned long index, unsigned int tag);
  10669. @@ -303,7 +308,7 @@
  10670. static inline void radix_tree_preload_end(void)
  10671. {
  10672. - preempt_enable();
  10673. + preempt_enable_nort();
  10674. }
  10675. /**
  10676. diff -Nur linux-4.1.20.orig/include/linux/random.h linux-4.1.20/include/linux/random.h
  10677. --- linux-4.1.20.orig/include/linux/random.h 2016-03-17 19:11:03.000000000 +0100
  10678. +++ linux-4.1.20/include/linux/random.h 2016-03-21 20:18:33.000000000 +0100
  10679. @@ -11,7 +11,7 @@
  10680. extern void add_device_randomness(const void *, unsigned int);
  10681. extern void add_input_randomness(unsigned int type, unsigned int code,
  10682. unsigned int value);
  10683. -extern void add_interrupt_randomness(int irq, int irq_flags);
  10684. +extern void add_interrupt_randomness(int irq, int irq_flags, __u64 ip);
  10685. extern void get_random_bytes(void *buf, int nbytes);
  10686. extern void get_random_bytes_arch(void *buf, int nbytes);
  10687. diff -Nur linux-4.1.20.orig/include/linux/rcupdate.h linux-4.1.20/include/linux/rcupdate.h
  10688. --- linux-4.1.20.orig/include/linux/rcupdate.h 2016-03-17 19:11:03.000000000 +0100
  10689. +++ linux-4.1.20/include/linux/rcupdate.h 2016-03-21 20:18:33.000000000 +0100
  10690. @@ -167,6 +167,9 @@
  10691. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  10692. +#ifdef CONFIG_PREEMPT_RT_FULL
  10693. +#define call_rcu_bh call_rcu
  10694. +#else
  10695. /**
  10696. * call_rcu_bh() - Queue an RCU for invocation after a quicker grace period.
  10697. * @head: structure to be used for queueing the RCU updates.
  10698. @@ -190,6 +193,7 @@
  10699. */
  10700. void call_rcu_bh(struct rcu_head *head,
  10701. void (*func)(struct rcu_head *head));
  10702. +#endif
  10703. /**
  10704. * call_rcu_sched() - Queue an RCU for invocation after sched grace period.
  10705. @@ -260,6 +264,11 @@
  10706. * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
  10707. */
  10708. #define rcu_preempt_depth() (current->rcu_read_lock_nesting)
  10709. +#ifndef CONFIG_PREEMPT_RT_FULL
  10710. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  10711. +#else
  10712. +static inline int sched_rcu_preempt_depth(void) { return 0; }
  10713. +#endif
  10714. #else /* #ifdef CONFIG_PREEMPT_RCU */
  10715. @@ -283,6 +292,8 @@
  10716. return 0;
  10717. }
  10718. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  10719. +
  10720. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  10721. /* Internal to kernel */
  10722. @@ -463,7 +474,14 @@
  10723. int debug_lockdep_rcu_enabled(void);
  10724. int rcu_read_lock_held(void);
  10725. +#ifdef CONFIG_PREEMPT_RT_FULL
  10726. +static inline int rcu_read_lock_bh_held(void)
  10727. +{
  10728. + return rcu_read_lock_held();
  10729. +}
  10730. +#else
  10731. int rcu_read_lock_bh_held(void);
  10732. +#endif
  10733. /**
  10734. * rcu_read_lock_sched_held() - might we be in RCU-sched read-side critical section?
  10735. @@ -990,10 +1008,14 @@
  10736. static inline void rcu_read_lock_bh(void)
  10737. {
  10738. local_bh_disable();
  10739. +#ifdef CONFIG_PREEMPT_RT_FULL
  10740. + rcu_read_lock();
  10741. +#else
  10742. __acquire(RCU_BH);
  10743. rcu_lock_acquire(&rcu_bh_lock_map);
  10744. rcu_lockdep_assert(rcu_is_watching(),
  10745. "rcu_read_lock_bh() used illegally while idle");
  10746. +#endif
  10747. }
  10748. /*
  10749. @@ -1003,10 +1025,14 @@
  10750. */
  10751. static inline void rcu_read_unlock_bh(void)
  10752. {
  10753. +#ifdef CONFIG_PREEMPT_RT_FULL
  10754. + rcu_read_unlock();
  10755. +#else
  10756. rcu_lockdep_assert(rcu_is_watching(),
  10757. "rcu_read_unlock_bh() used illegally while idle");
  10758. rcu_lock_release(&rcu_bh_lock_map);
  10759. __release(RCU_BH);
  10760. +#endif
  10761. local_bh_enable();
  10762. }
  10763. diff -Nur linux-4.1.20.orig/include/linux/rcutree.h linux-4.1.20/include/linux/rcutree.h
  10764. --- linux-4.1.20.orig/include/linux/rcutree.h 2016-03-17 19:11:03.000000000 +0100
  10765. +++ linux-4.1.20/include/linux/rcutree.h 2016-03-21 20:18:33.000000000 +0100
  10766. @@ -46,7 +46,11 @@
  10767. rcu_note_context_switch();
  10768. }
  10769. +#ifdef CONFIG_PREEMPT_RT_FULL
  10770. +# define synchronize_rcu_bh synchronize_rcu
  10771. +#else
  10772. void synchronize_rcu_bh(void);
  10773. +#endif
  10774. void synchronize_sched_expedited(void);
  10775. void synchronize_rcu_expedited(void);
  10776. @@ -74,7 +78,11 @@
  10777. }
  10778. void rcu_barrier(void);
  10779. +#ifdef CONFIG_PREEMPT_RT_FULL
  10780. +# define rcu_barrier_bh rcu_barrier
  10781. +#else
  10782. void rcu_barrier_bh(void);
  10783. +#endif
  10784. void rcu_barrier_sched(void);
  10785. unsigned long get_state_synchronize_rcu(void);
  10786. void cond_synchronize_rcu(unsigned long oldstate);
  10787. @@ -85,12 +93,10 @@
  10788. unsigned long rcu_batches_started_bh(void);
  10789. unsigned long rcu_batches_started_sched(void);
  10790. unsigned long rcu_batches_completed(void);
  10791. -unsigned long rcu_batches_completed_bh(void);
  10792. unsigned long rcu_batches_completed_sched(void);
  10793. void show_rcu_gp_kthreads(void);
  10794. void rcu_force_quiescent_state(void);
  10795. -void rcu_bh_force_quiescent_state(void);
  10796. void rcu_sched_force_quiescent_state(void);
  10797. void exit_rcu(void);
  10798. @@ -100,6 +106,14 @@
  10799. bool rcu_is_watching(void);
  10800. +#ifndef CONFIG_PREEMPT_RT_FULL
  10801. +void rcu_bh_force_quiescent_state(void);
  10802. +unsigned long rcu_batches_completed_bh(void);
  10803. +#else
  10804. +# define rcu_bh_force_quiescent_state rcu_force_quiescent_state
  10805. +# define rcu_batches_completed_bh rcu_batches_completed
  10806. +#endif
  10807. +
  10808. void rcu_all_qs(void);
  10809. #endif /* __LINUX_RCUTREE_H */
  10810. diff -Nur linux-4.1.20.orig/include/linux/rtmutex.h linux-4.1.20/include/linux/rtmutex.h
  10811. --- linux-4.1.20.orig/include/linux/rtmutex.h 2016-03-17 19:11:03.000000000 +0100
  10812. +++ linux-4.1.20/include/linux/rtmutex.h 2016-03-21 20:18:33.000000000 +0100
  10813. @@ -14,10 +14,14 @@
  10814. #include <linux/linkage.h>
  10815. #include <linux/rbtree.h>
  10816. -#include <linux/spinlock_types.h>
  10817. +#include <linux/spinlock_types_raw.h>
  10818. extern int max_lock_depth; /* for sysctl */
  10819. +#ifdef CONFIG_DEBUG_MUTEXES
  10820. +#include <linux/debug_locks.h>
  10821. +#endif
  10822. +
  10823. /**
  10824. * The rt_mutex structure
  10825. *
  10826. @@ -31,8 +35,8 @@
  10827. struct rb_root waiters;
  10828. struct rb_node *waiters_leftmost;
  10829. struct task_struct *owner;
  10830. -#ifdef CONFIG_DEBUG_RT_MUTEXES
  10831. int save_state;
  10832. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  10833. const char *name, *file;
  10834. int line;
  10835. void *magic;
  10836. @@ -55,22 +59,33 @@
  10837. # define rt_mutex_debug_check_no_locks_held(task) do { } while (0)
  10838. #endif
  10839. +# define rt_mutex_init(mutex) \
  10840. + do { \
  10841. + raw_spin_lock_init(&(mutex)->wait_lock); \
  10842. + __rt_mutex_init(mutex, #mutex); \
  10843. + } while (0)
  10844. +
  10845. #ifdef CONFIG_DEBUG_RT_MUTEXES
  10846. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
  10847. , .name = #mutexname, .file = __FILE__, .line = __LINE__
  10848. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, __func__)
  10849. extern void rt_mutex_debug_task_free(struct task_struct *tsk);
  10850. #else
  10851. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  10852. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL)
  10853. # define rt_mutex_debug_task_free(t) do { } while (0)
  10854. #endif
  10855. -#define __RT_MUTEX_INITIALIZER(mutexname) \
  10856. - { .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  10857. +#define __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  10858. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  10859. , .waiters = RB_ROOT \
  10860. , .owner = NULL \
  10861. - __DEBUG_RT_MUTEX_INITIALIZER(mutexname)}
  10862. + __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  10863. +
  10864. +#define __RT_MUTEX_INITIALIZER(mutexname) \
  10865. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) }
  10866. +
  10867. +#define __RT_MUTEX_INITIALIZER_SAVE_STATE(mutexname) \
  10868. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  10869. + , .save_state = 1 }
  10870. #define DEFINE_RT_MUTEX(mutexname) \
  10871. struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
  10872. @@ -91,6 +106,7 @@
  10873. extern void rt_mutex_lock(struct rt_mutex *lock);
  10874. extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
  10875. +extern int rt_mutex_lock_killable(struct rt_mutex *lock);
  10876. extern int rt_mutex_timed_lock(struct rt_mutex *lock,
  10877. struct hrtimer_sleeper *timeout);
  10878. diff -Nur linux-4.1.20.orig/include/linux/rwlock_rt.h linux-4.1.20/include/linux/rwlock_rt.h
  10879. --- linux-4.1.20.orig/include/linux/rwlock_rt.h 1970-01-01 01:00:00.000000000 +0100
  10880. +++ linux-4.1.20/include/linux/rwlock_rt.h 2016-03-21 20:18:33.000000000 +0100
  10881. @@ -0,0 +1,99 @@
  10882. +#ifndef __LINUX_RWLOCK_RT_H
  10883. +#define __LINUX_RWLOCK_RT_H
  10884. +
  10885. +#ifndef __LINUX_SPINLOCK_H
  10886. +#error Do not include directly. Use spinlock.h
  10887. +#endif
  10888. +
  10889. +#define rwlock_init(rwl) \
  10890. +do { \
  10891. + static struct lock_class_key __key; \
  10892. + \
  10893. + rt_mutex_init(&(rwl)->lock); \
  10894. + __rt_rwlock_init(rwl, #rwl, &__key); \
  10895. +} while (0)
  10896. +
  10897. +extern void __lockfunc rt_write_lock(rwlock_t *rwlock);
  10898. +extern void __lockfunc rt_read_lock(rwlock_t *rwlock);
  10899. +extern int __lockfunc rt_write_trylock(rwlock_t *rwlock);
  10900. +extern int __lockfunc rt_write_trylock_irqsave(rwlock_t *trylock, unsigned long *flags);
  10901. +extern int __lockfunc rt_read_trylock(rwlock_t *rwlock);
  10902. +extern void __lockfunc rt_write_unlock(rwlock_t *rwlock);
  10903. +extern void __lockfunc rt_read_unlock(rwlock_t *rwlock);
  10904. +extern unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock);
  10905. +extern unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock);
  10906. +extern void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key);
  10907. +
  10908. +#define read_trylock(lock) __cond_lock(lock, rt_read_trylock(lock))
  10909. +#define write_trylock(lock) __cond_lock(lock, rt_write_trylock(lock))
  10910. +
  10911. +#define write_trylock_irqsave(lock, flags) \
  10912. + __cond_lock(lock, rt_write_trylock_irqsave(lock, &flags))
  10913. +
  10914. +#define read_lock_irqsave(lock, flags) \
  10915. + do { \
  10916. + typecheck(unsigned long, flags); \
  10917. + flags = rt_read_lock_irqsave(lock); \
  10918. + } while (0)
  10919. +
  10920. +#define write_lock_irqsave(lock, flags) \
  10921. + do { \
  10922. + typecheck(unsigned long, flags); \
  10923. + flags = rt_write_lock_irqsave(lock); \
  10924. + } while (0)
  10925. +
  10926. +#define read_lock(lock) rt_read_lock(lock)
  10927. +
  10928. +#define read_lock_bh(lock) \
  10929. + do { \
  10930. + local_bh_disable(); \
  10931. + rt_read_lock(lock); \
  10932. + } while (0)
  10933. +
  10934. +#define read_lock_irq(lock) read_lock(lock)
  10935. +
  10936. +#define write_lock(lock) rt_write_lock(lock)
  10937. +
  10938. +#define write_lock_bh(lock) \
  10939. + do { \
  10940. + local_bh_disable(); \
  10941. + rt_write_lock(lock); \
  10942. + } while (0)
  10943. +
  10944. +#define write_lock_irq(lock) write_lock(lock)
  10945. +
  10946. +#define read_unlock(lock) rt_read_unlock(lock)
  10947. +
  10948. +#define read_unlock_bh(lock) \
  10949. + do { \
  10950. + rt_read_unlock(lock); \
  10951. + local_bh_enable(); \
  10952. + } while (0)
  10953. +
  10954. +#define read_unlock_irq(lock) read_unlock(lock)
  10955. +
  10956. +#define write_unlock(lock) rt_write_unlock(lock)
  10957. +
  10958. +#define write_unlock_bh(lock) \
  10959. + do { \
  10960. + rt_write_unlock(lock); \
  10961. + local_bh_enable(); \
  10962. + } while (0)
  10963. +
  10964. +#define write_unlock_irq(lock) write_unlock(lock)
  10965. +
  10966. +#define read_unlock_irqrestore(lock, flags) \
  10967. + do { \
  10968. + typecheck(unsigned long, flags); \
  10969. + (void) flags; \
  10970. + rt_read_unlock(lock); \
  10971. + } while (0)
  10972. +
  10973. +#define write_unlock_irqrestore(lock, flags) \
  10974. + do { \
  10975. + typecheck(unsigned long, flags); \
  10976. + (void) flags; \
  10977. + rt_write_unlock(lock); \
  10978. + } while (0)
  10979. +
  10980. +#endif
  10981. diff -Nur linux-4.1.20.orig/include/linux/rwlock_types.h linux-4.1.20/include/linux/rwlock_types.h
  10982. --- linux-4.1.20.orig/include/linux/rwlock_types.h 2016-03-17 19:11:03.000000000 +0100
  10983. +++ linux-4.1.20/include/linux/rwlock_types.h 2016-03-21 20:18:33.000000000 +0100
  10984. @@ -1,6 +1,10 @@
  10985. #ifndef __LINUX_RWLOCK_TYPES_H
  10986. #define __LINUX_RWLOCK_TYPES_H
  10987. +#if !defined(__LINUX_SPINLOCK_TYPES_H)
  10988. +# error "Do not include directly, include spinlock_types.h"
  10989. +#endif
  10990. +
  10991. /*
  10992. * include/linux/rwlock_types.h - generic rwlock type definitions
  10993. * and initializers
  10994. @@ -43,6 +47,7 @@
  10995. RW_DEP_MAP_INIT(lockname) }
  10996. #endif
  10997. -#define DEFINE_RWLOCK(x) rwlock_t x = __RW_LOCK_UNLOCKED(x)
  10998. +#define DEFINE_RWLOCK(name) \
  10999. + rwlock_t name __cacheline_aligned_in_smp = __RW_LOCK_UNLOCKED(name)
  11000. #endif /* __LINUX_RWLOCK_TYPES_H */
  11001. diff -Nur linux-4.1.20.orig/include/linux/rwlock_types_rt.h linux-4.1.20/include/linux/rwlock_types_rt.h
  11002. --- linux-4.1.20.orig/include/linux/rwlock_types_rt.h 1970-01-01 01:00:00.000000000 +0100
  11003. +++ linux-4.1.20/include/linux/rwlock_types_rt.h 2016-03-21 20:18:33.000000000 +0100
  11004. @@ -0,0 +1,33 @@
  11005. +#ifndef __LINUX_RWLOCK_TYPES_RT_H
  11006. +#define __LINUX_RWLOCK_TYPES_RT_H
  11007. +
  11008. +#ifndef __LINUX_SPINLOCK_TYPES_H
  11009. +#error "Do not include directly. Include spinlock_types.h instead"
  11010. +#endif
  11011. +
  11012. +/*
  11013. + * rwlocks - rtmutex which allows single reader recursion
  11014. + */
  11015. +typedef struct {
  11016. + struct rt_mutex lock;
  11017. + int read_depth;
  11018. + unsigned int break_lock;
  11019. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11020. + struct lockdep_map dep_map;
  11021. +#endif
  11022. +} rwlock_t;
  11023. +
  11024. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11025. +# define RW_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  11026. +#else
  11027. +# define RW_DEP_MAP_INIT(lockname)
  11028. +#endif
  11029. +
  11030. +#define __RW_LOCK_UNLOCKED(name) \
  11031. + { .lock = __RT_MUTEX_INITIALIZER_SAVE_STATE(name.lock), \
  11032. + RW_DEP_MAP_INIT(name) }
  11033. +
  11034. +#define DEFINE_RWLOCK(name) \
  11035. + rwlock_t name __cacheline_aligned_in_smp = __RW_LOCK_UNLOCKED(name)
  11036. +
  11037. +#endif
  11038. diff -Nur linux-4.1.20.orig/include/linux/rwsem.h linux-4.1.20/include/linux/rwsem.h
  11039. --- linux-4.1.20.orig/include/linux/rwsem.h 2016-03-17 19:11:03.000000000 +0100
  11040. +++ linux-4.1.20/include/linux/rwsem.h 2016-03-21 20:18:33.000000000 +0100
  11041. @@ -18,6 +18,10 @@
  11042. #include <linux/osq_lock.h>
  11043. #endif
  11044. +#ifdef CONFIG_PREEMPT_RT_FULL
  11045. +#include <linux/rwsem_rt.h>
  11046. +#else /* PREEMPT_RT_FULL */
  11047. +
  11048. struct rw_semaphore;
  11049. #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
  11050. @@ -177,4 +181,6 @@
  11051. # define up_read_non_owner(sem) up_read(sem)
  11052. #endif
  11053. +#endif /* !PREEMPT_RT_FULL */
  11054. +
  11055. #endif /* _LINUX_RWSEM_H */
  11056. diff -Nur linux-4.1.20.orig/include/linux/rwsem_rt.h linux-4.1.20/include/linux/rwsem_rt.h
  11057. --- linux-4.1.20.orig/include/linux/rwsem_rt.h 1970-01-01 01:00:00.000000000 +0100
  11058. +++ linux-4.1.20/include/linux/rwsem_rt.h 2016-03-21 20:18:33.000000000 +0100
  11059. @@ -0,0 +1,140 @@
  11060. +#ifndef _LINUX_RWSEM_RT_H
  11061. +#define _LINUX_RWSEM_RT_H
  11062. +
  11063. +#ifndef _LINUX_RWSEM_H
  11064. +#error "Include rwsem.h"
  11065. +#endif
  11066. +
  11067. +/*
  11068. + * RW-semaphores are a spinlock plus a reader-depth count.
  11069. + *
  11070. + * Note that the semantics are different from the usual
  11071. + * Linux rw-sems, in PREEMPT_RT mode we do not allow
  11072. + * multiple readers to hold the lock at once, we only allow
  11073. + * a read-lock owner to read-lock recursively. This is
  11074. + * better for latency, makes the implementation inherently
  11075. + * fair and makes it simpler as well.
  11076. + */
  11077. +
  11078. +#include <linux/rtmutex.h>
  11079. +
  11080. +struct rw_semaphore {
  11081. + struct rt_mutex lock;
  11082. + int read_depth;
  11083. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11084. + struct lockdep_map dep_map;
  11085. +#endif
  11086. +};
  11087. +
  11088. +#define __RWSEM_INITIALIZER(name) \
  11089. + { .lock = __RT_MUTEX_INITIALIZER(name.lock), \
  11090. + RW_DEP_MAP_INIT(name) }
  11091. +
  11092. +#define DECLARE_RWSEM(lockname) \
  11093. + struct rw_semaphore lockname = __RWSEM_INITIALIZER(lockname)
  11094. +
  11095. +extern void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  11096. + struct lock_class_key *key);
  11097. +
  11098. +#define __rt_init_rwsem(sem, name, key) \
  11099. + do { \
  11100. + rt_mutex_init(&(sem)->lock); \
  11101. + __rt_rwsem_init((sem), (name), (key));\
  11102. + } while (0)
  11103. +
  11104. +#define __init_rwsem(sem, name, key) __rt_init_rwsem(sem, name, key)
  11105. +
  11106. +# define rt_init_rwsem(sem) \
  11107. +do { \
  11108. + static struct lock_class_key __key; \
  11109. + \
  11110. + __rt_init_rwsem((sem), #sem, &__key); \
  11111. +} while (0)
  11112. +
  11113. +extern void rt_down_write(struct rw_semaphore *rwsem);
  11114. +extern void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass);
  11115. +extern void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass);
  11116. +extern void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  11117. + struct lockdep_map *nest);
  11118. +extern void rt_down_read(struct rw_semaphore *rwsem);
  11119. +extern int rt_down_write_trylock(struct rw_semaphore *rwsem);
  11120. +extern int rt_down_read_trylock(struct rw_semaphore *rwsem);
  11121. +extern void __rt_up_read(struct rw_semaphore *rwsem);
  11122. +extern void rt_up_read(struct rw_semaphore *rwsem);
  11123. +extern void rt_up_write(struct rw_semaphore *rwsem);
  11124. +extern void rt_downgrade_write(struct rw_semaphore *rwsem);
  11125. +
  11126. +#define init_rwsem(sem) rt_init_rwsem(sem)
  11127. +#define rwsem_is_locked(s) rt_mutex_is_locked(&(s)->lock)
  11128. +
  11129. +static inline int rwsem_is_contended(struct rw_semaphore *sem)
  11130. +{
  11131. + /* rt_mutex_has_waiters() */
  11132. + return !RB_EMPTY_ROOT(&sem->lock.waiters);
  11133. +}
  11134. +
  11135. +static inline void down_read(struct rw_semaphore *sem)
  11136. +{
  11137. + rt_down_read(sem);
  11138. +}
  11139. +
  11140. +static inline int down_read_trylock(struct rw_semaphore *sem)
  11141. +{
  11142. + return rt_down_read_trylock(sem);
  11143. +}
  11144. +
  11145. +static inline void down_write(struct rw_semaphore *sem)
  11146. +{
  11147. + rt_down_write(sem);
  11148. +}
  11149. +
  11150. +static inline int down_write_trylock(struct rw_semaphore *sem)
  11151. +{
  11152. + return rt_down_write_trylock(sem);
  11153. +}
  11154. +
  11155. +static inline void __up_read(struct rw_semaphore *sem)
  11156. +{
  11157. + __rt_up_read(sem);
  11158. +}
  11159. +
  11160. +static inline void up_read(struct rw_semaphore *sem)
  11161. +{
  11162. + rt_up_read(sem);
  11163. +}
  11164. +
  11165. +static inline void up_write(struct rw_semaphore *sem)
  11166. +{
  11167. + rt_up_write(sem);
  11168. +}
  11169. +
  11170. +static inline void downgrade_write(struct rw_semaphore *sem)
  11171. +{
  11172. + rt_downgrade_write(sem);
  11173. +}
  11174. +
  11175. +static inline void down_read_nested(struct rw_semaphore *sem, int subclass)
  11176. +{
  11177. + return rt_down_read_nested(sem, subclass);
  11178. +}
  11179. +
  11180. +static inline void down_write_nested(struct rw_semaphore *sem, int subclass)
  11181. +{
  11182. + rt_down_write_nested(sem, subclass);
  11183. +}
  11184. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11185. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  11186. + struct rw_semaphore *nest_lock)
  11187. +{
  11188. + rt_down_write_nested_lock(sem, &nest_lock->dep_map);
  11189. +}
  11190. +
  11191. +#else
  11192. +
  11193. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  11194. + struct rw_semaphore *nest_lock)
  11195. +{
  11196. + rt_down_write_nested_lock(sem, NULL);
  11197. +}
  11198. +#endif
  11199. +#endif
  11200. diff -Nur linux-4.1.20.orig/include/linux/sched.h linux-4.1.20/include/linux/sched.h
  11201. --- linux-4.1.20.orig/include/linux/sched.h 2016-03-17 19:11:03.000000000 +0100
  11202. +++ linux-4.1.20/include/linux/sched.h 2016-03-21 20:18:33.000000000 +0100
  11203. @@ -26,6 +26,7 @@
  11204. #include <linux/nodemask.h>
  11205. #include <linux/mm_types.h>
  11206. #include <linux/preempt_mask.h>
  11207. +#include <asm/kmap_types.h>
  11208. #include <asm/page.h>
  11209. #include <asm/ptrace.h>
  11210. @@ -175,8 +176,6 @@
  11211. extern void calc_global_load(unsigned long ticks);
  11212. extern void update_cpu_load_nohz(void);
  11213. -extern unsigned long get_parent_ip(unsigned long addr);
  11214. -
  11215. extern void dump_cpu_task(int cpu);
  11216. struct seq_file;
  11217. @@ -234,10 +233,7 @@
  11218. TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
  11219. __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
  11220. -#define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
  11221. #define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
  11222. -#define task_is_stopped_or_traced(task) \
  11223. - ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
  11224. #define task_contributes_to_load(task) \
  11225. ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
  11226. (task->flags & PF_FROZEN) == 0)
  11227. @@ -302,6 +298,11 @@
  11228. #endif
  11229. +#define __set_current_state_no_track(state_value) \
  11230. + do { current->state = (state_value); } while (0)
  11231. +#define set_current_state_no_track(state_value) \
  11232. + set_mb(current->state, (state_value))
  11233. +
  11234. /* Task command name length */
  11235. #define TASK_COMM_LEN 16
  11236. @@ -901,6 +902,50 @@
  11237. #define SCHED_CAPACITY_SCALE (1L << SCHED_CAPACITY_SHIFT)
  11238. /*
  11239. + * Wake-queues are lists of tasks with a pending wakeup, whose
  11240. + * callers have already marked the task as woken internally,
  11241. + * and can thus carry on. A common use case is being able to
  11242. + * do the wakeups once the corresponding user lock as been
  11243. + * released.
  11244. + *
  11245. + * We hold reference to each task in the list across the wakeup,
  11246. + * thus guaranteeing that the memory is still valid by the time
  11247. + * the actual wakeups are performed in wake_up_q().
  11248. + *
  11249. + * One per task suffices, because there's never a need for a task to be
  11250. + * in two wake queues simultaneously; it is forbidden to abandon a task
  11251. + * in a wake queue (a call to wake_up_q() _must_ follow), so if a task is
  11252. + * already in a wake queue, the wakeup will happen soon and the second
  11253. + * waker can just skip it.
  11254. + *
  11255. + * The WAKE_Q macro declares and initializes the list head.
  11256. + * wake_up_q() does NOT reinitialize the list; it's expected to be
  11257. + * called near the end of a function, where the fact that the queue is
  11258. + * not used again will be easy to see by inspection.
  11259. + *
  11260. + * Note that this can cause spurious wakeups. schedule() callers
  11261. + * must ensure the call is done inside a loop, confirming that the
  11262. + * wakeup condition has in fact occurred.
  11263. + */
  11264. +struct wake_q_node {
  11265. + struct wake_q_node *next;
  11266. +};
  11267. +
  11268. +struct wake_q_head {
  11269. + struct wake_q_node *first;
  11270. + struct wake_q_node **lastp;
  11271. +};
  11272. +
  11273. +#define WAKE_Q_TAIL ((struct wake_q_node *) 0x01)
  11274. +
  11275. +#define WAKE_Q(name) \
  11276. + struct wake_q_head name = { WAKE_Q_TAIL, &name.first }
  11277. +
  11278. +extern void wake_q_add(struct wake_q_head *head,
  11279. + struct task_struct *task);
  11280. +extern void wake_up_q(struct wake_q_head *head);
  11281. +
  11282. +/*
  11283. * sched-domains (multiprocessor balancing) declarations:
  11284. */
  11285. #ifdef CONFIG_SMP
  11286. @@ -1292,6 +1337,7 @@
  11287. struct task_struct {
  11288. volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
  11289. + volatile long saved_state; /* saved state for "spinlock sleepers" */
  11290. void *stack;
  11291. atomic_t usage;
  11292. unsigned int flags; /* per process flags, defined below */
  11293. @@ -1328,6 +1374,12 @@
  11294. #endif
  11295. unsigned int policy;
  11296. +#ifdef CONFIG_PREEMPT_RT_FULL
  11297. + int migrate_disable;
  11298. +# ifdef CONFIG_SCHED_DEBUG
  11299. + int migrate_disable_atomic;
  11300. +# endif
  11301. +#endif
  11302. int nr_cpus_allowed;
  11303. cpumask_t cpus_allowed;
  11304. @@ -1435,7 +1487,8 @@
  11305. struct cputime prev_cputime;
  11306. #endif
  11307. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  11308. - seqlock_t vtime_seqlock;
  11309. + raw_spinlock_t vtime_lock;
  11310. + seqcount_t vtime_seq;
  11311. unsigned long long vtime_snap;
  11312. enum {
  11313. VTIME_SLEEPING = 0,
  11314. @@ -1451,6 +1504,9 @@
  11315. struct task_cputime cputime_expires;
  11316. struct list_head cpu_timers[3];
  11317. +#ifdef CONFIG_PREEMPT_RT_BASE
  11318. + struct task_struct *posix_timer_list;
  11319. +#endif
  11320. /* process credentials */
  11321. const struct cred __rcu *real_cred; /* objective and real subjective task
  11322. @@ -1483,10 +1539,15 @@
  11323. /* signal handlers */
  11324. struct signal_struct *signal;
  11325. struct sighand_struct *sighand;
  11326. + struct sigqueue *sigqueue_cache;
  11327. sigset_t blocked, real_blocked;
  11328. sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
  11329. struct sigpending pending;
  11330. +#ifdef CONFIG_PREEMPT_RT_FULL
  11331. + /* TODO: move me into ->restart_block ? */
  11332. + struct siginfo forced_info;
  11333. +#endif
  11334. unsigned long sas_ss_sp;
  11335. size_t sas_ss_size;
  11336. @@ -1512,6 +1573,8 @@
  11337. /* Protection of the PI data structures: */
  11338. raw_spinlock_t pi_lock;
  11339. + struct wake_q_node wake_q;
  11340. +
  11341. #ifdef CONFIG_RT_MUTEXES
  11342. /* PI waiters blocked on a rt_mutex held by this task */
  11343. struct rb_root pi_waiters;
  11344. @@ -1706,6 +1769,12 @@
  11345. unsigned long trace;
  11346. /* bitmask and counter of trace recursion */
  11347. unsigned long trace_recursion;
  11348. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  11349. + u64 preempt_timestamp_hist;
  11350. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  11351. + long timer_offset;
  11352. +#endif
  11353. +#endif
  11354. #endif /* CONFIG_TRACING */
  11355. #ifdef CONFIG_MEMCG
  11356. struct memcg_oom_info {
  11357. @@ -1722,14 +1791,26 @@
  11358. unsigned int sequential_io;
  11359. unsigned int sequential_io_avg;
  11360. #endif
  11361. +#ifdef CONFIG_PREEMPT_RT_BASE
  11362. + struct rcu_head put_rcu;
  11363. + int softirq_nestcnt;
  11364. + unsigned int softirqs_raised;
  11365. +#endif
  11366. +#ifdef CONFIG_PREEMPT_RT_FULL
  11367. +# if defined CONFIG_HIGHMEM || defined CONFIG_X86_32
  11368. + int kmap_idx;
  11369. + pte_t kmap_pte[KM_TYPE_NR];
  11370. +# endif
  11371. +#endif
  11372. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  11373. unsigned long task_state_change;
  11374. #endif
  11375. +#ifdef CONFIG_PREEMPT_RT_FULL
  11376. + int xmit_recursion;
  11377. +#endif
  11378. + int pagefault_disabled;
  11379. };
  11380. -/* Future-safe accessor for struct task_struct's cpus_allowed. */
  11381. -#define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
  11382. -
  11383. #define TNF_MIGRATED 0x01
  11384. #define TNF_NO_GROUP 0x02
  11385. #define TNF_SHARED 0x04
  11386. @@ -1918,6 +1999,15 @@
  11387. extern void free_task(struct task_struct *tsk);
  11388. #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
  11389. +#ifdef CONFIG_PREEMPT_RT_BASE
  11390. +extern void __put_task_struct_cb(struct rcu_head *rhp);
  11391. +
  11392. +static inline void put_task_struct(struct task_struct *t)
  11393. +{
  11394. + if (atomic_dec_and_test(&t->usage))
  11395. + call_rcu(&t->put_rcu, __put_task_struct_cb);
  11396. +}
  11397. +#else
  11398. extern void __put_task_struct(struct task_struct *t);
  11399. static inline void put_task_struct(struct task_struct *t)
  11400. @@ -1925,6 +2015,7 @@
  11401. if (atomic_dec_and_test(&t->usage))
  11402. __put_task_struct(t);
  11403. }
  11404. +#endif
  11405. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  11406. extern void task_cputime(struct task_struct *t,
  11407. @@ -1963,6 +2054,7 @@
  11408. /*
  11409. * Per process flags
  11410. */
  11411. +#define PF_IN_SOFTIRQ 0x00000001 /* Task is serving softirq */
  11412. #define PF_EXITING 0x00000004 /* getting shut down */
  11413. #define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
  11414. #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
  11415. @@ -2127,6 +2219,10 @@
  11416. extern int set_cpus_allowed_ptr(struct task_struct *p,
  11417. const struct cpumask *new_mask);
  11418. +int migrate_me(void);
  11419. +void tell_sched_cpu_down_begin(int cpu);
  11420. +void tell_sched_cpu_down_done(int cpu);
  11421. +
  11422. #else
  11423. static inline void do_set_cpus_allowed(struct task_struct *p,
  11424. const struct cpumask *new_mask)
  11425. @@ -2139,6 +2235,9 @@
  11426. return -EINVAL;
  11427. return 0;
  11428. }
  11429. +static inline int migrate_me(void) { return 0; }
  11430. +static inline void tell_sched_cpu_down_begin(int cpu) { }
  11431. +static inline void tell_sched_cpu_down_done(int cpu) { }
  11432. #endif
  11433. #ifdef CONFIG_NO_HZ_COMMON
  11434. @@ -2355,6 +2454,7 @@
  11435. extern int wake_up_state(struct task_struct *tsk, unsigned int state);
  11436. extern int wake_up_process(struct task_struct *tsk);
  11437. +extern int wake_up_lock_sleeper(struct task_struct * tsk);
  11438. extern void wake_up_new_task(struct task_struct *tsk);
  11439. #ifdef CONFIG_SMP
  11440. extern void kick_process(struct task_struct *tsk);
  11441. @@ -2471,12 +2571,24 @@
  11442. /* mmdrop drops the mm and the page tables */
  11443. extern void __mmdrop(struct mm_struct *);
  11444. +
  11445. static inline void mmdrop(struct mm_struct * mm)
  11446. {
  11447. if (unlikely(atomic_dec_and_test(&mm->mm_count)))
  11448. __mmdrop(mm);
  11449. }
  11450. +#ifdef CONFIG_PREEMPT_RT_BASE
  11451. +extern void __mmdrop_delayed(struct rcu_head *rhp);
  11452. +static inline void mmdrop_delayed(struct mm_struct *mm)
  11453. +{
  11454. + if (atomic_dec_and_test(&mm->mm_count))
  11455. + call_rcu(&mm->delayed_drop, __mmdrop_delayed);
  11456. +}
  11457. +#else
  11458. +# define mmdrop_delayed(mm) mmdrop(mm)
  11459. +#endif
  11460. +
  11461. /* mmput gets rid of the mappings and all user-space */
  11462. extern void mmput(struct mm_struct *);
  11463. /* Grab a reference to a task's mm, if it is not already going away */
  11464. @@ -2788,6 +2900,43 @@
  11465. return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
  11466. }
  11467. +#ifdef CONFIG_PREEMPT_LAZY
  11468. +static inline void set_tsk_need_resched_lazy(struct task_struct *tsk)
  11469. +{
  11470. + set_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  11471. +}
  11472. +
  11473. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk)
  11474. +{
  11475. + clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  11476. +}
  11477. +
  11478. +static inline int test_tsk_need_resched_lazy(struct task_struct *tsk)
  11479. +{
  11480. + return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY));
  11481. +}
  11482. +
  11483. +static inline int need_resched_lazy(void)
  11484. +{
  11485. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  11486. +}
  11487. +
  11488. +static inline int need_resched_now(void)
  11489. +{
  11490. + return test_thread_flag(TIF_NEED_RESCHED);
  11491. +}
  11492. +
  11493. +#else
  11494. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk) { }
  11495. +static inline int need_resched_lazy(void) { return 0; }
  11496. +
  11497. +static inline int need_resched_now(void)
  11498. +{
  11499. + return test_thread_flag(TIF_NEED_RESCHED);
  11500. +}
  11501. +
  11502. +#endif
  11503. +
  11504. static inline int restart_syscall(void)
  11505. {
  11506. set_tsk_thread_flag(current, TIF_SIGPENDING);
  11507. @@ -2819,6 +2968,51 @@
  11508. return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
  11509. }
  11510. +static inline bool __task_is_stopped_or_traced(struct task_struct *task)
  11511. +{
  11512. + if (task->state & (__TASK_STOPPED | __TASK_TRACED))
  11513. + return true;
  11514. +#ifdef CONFIG_PREEMPT_RT_FULL
  11515. + if (task->saved_state & (__TASK_STOPPED | __TASK_TRACED))
  11516. + return true;
  11517. +#endif
  11518. + return false;
  11519. +}
  11520. +
  11521. +static inline bool task_is_stopped_or_traced(struct task_struct *task)
  11522. +{
  11523. + bool traced_stopped;
  11524. +
  11525. +#ifdef CONFIG_PREEMPT_RT_FULL
  11526. + unsigned long flags;
  11527. +
  11528. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  11529. + traced_stopped = __task_is_stopped_or_traced(task);
  11530. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  11531. +#else
  11532. + traced_stopped = __task_is_stopped_or_traced(task);
  11533. +#endif
  11534. + return traced_stopped;
  11535. +}
  11536. +
  11537. +static inline bool task_is_traced(struct task_struct *task)
  11538. +{
  11539. + bool traced = false;
  11540. +
  11541. + if (task->state & __TASK_TRACED)
  11542. + return true;
  11543. +#ifdef CONFIG_PREEMPT_RT_FULL
  11544. + /* in case the task is sleeping on tasklist_lock */
  11545. + raw_spin_lock_irq(&task->pi_lock);
  11546. + if (task->state & __TASK_TRACED)
  11547. + traced = true;
  11548. + else if (task->saved_state & __TASK_TRACED)
  11549. + traced = true;
  11550. + raw_spin_unlock_irq(&task->pi_lock);
  11551. +#endif
  11552. + return traced;
  11553. +}
  11554. +
  11555. /*
  11556. * cond_resched() and cond_resched_lock(): latency reduction via
  11557. * explicit rescheduling in places that are safe. The return
  11558. @@ -2840,12 +3034,16 @@
  11559. __cond_resched_lock(lock); \
  11560. })
  11561. +#ifndef CONFIG_PREEMPT_RT_FULL
  11562. extern int __cond_resched_softirq(void);
  11563. #define cond_resched_softirq() ({ \
  11564. ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
  11565. __cond_resched_softirq(); \
  11566. })
  11567. +#else
  11568. +# define cond_resched_softirq() cond_resched()
  11569. +#endif
  11570. static inline void cond_resched_rcu(void)
  11571. {
  11572. @@ -3012,6 +3210,26 @@
  11573. #endif /* CONFIG_SMP */
  11574. +static inline int __migrate_disabled(struct task_struct *p)
  11575. +{
  11576. +#ifdef CONFIG_PREEMPT_RT_FULL
  11577. + return p->migrate_disable;
  11578. +#else
  11579. + return 0;
  11580. +#endif
  11581. +}
  11582. +
  11583. +/* Future-safe accessor for struct task_struct's cpus_allowed. */
  11584. +static inline const struct cpumask *tsk_cpus_allowed(struct task_struct *p)
  11585. +{
  11586. +#ifdef CONFIG_PREEMPT_RT_FULL
  11587. + if (p->migrate_disable)
  11588. + return cpumask_of(task_cpu(p));
  11589. +#endif
  11590. +
  11591. + return &p->cpus_allowed;
  11592. +}
  11593. +
  11594. extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
  11595. extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
  11596. diff -Nur linux-4.1.20.orig/include/linux/seqlock.h linux-4.1.20/include/linux/seqlock.h
  11597. --- linux-4.1.20.orig/include/linux/seqlock.h 2016-03-17 19:11:03.000000000 +0100
  11598. +++ linux-4.1.20/include/linux/seqlock.h 2016-03-21 20:18:33.000000000 +0100
  11599. @@ -219,20 +219,30 @@
  11600. return __read_seqcount_retry(s, start);
  11601. }
  11602. -
  11603. -
  11604. -static inline void raw_write_seqcount_begin(seqcount_t *s)
  11605. +static inline void __raw_write_seqcount_begin(seqcount_t *s)
  11606. {
  11607. s->sequence++;
  11608. smp_wmb();
  11609. }
  11610. -static inline void raw_write_seqcount_end(seqcount_t *s)
  11611. +static inline void raw_write_seqcount_begin(seqcount_t *s)
  11612. +{
  11613. + preempt_disable_rt();
  11614. + __raw_write_seqcount_begin(s);
  11615. +}
  11616. +
  11617. +static inline void __raw_write_seqcount_end(seqcount_t *s)
  11618. {
  11619. smp_wmb();
  11620. s->sequence++;
  11621. }
  11622. +static inline void raw_write_seqcount_end(seqcount_t *s)
  11623. +{
  11624. + __raw_write_seqcount_end(s);
  11625. + preempt_enable_rt();
  11626. +}
  11627. +
  11628. /*
  11629. * raw_write_seqcount_latch - redirect readers to even/odd copy
  11630. * @s: pointer to seqcount_t
  11631. @@ -305,10 +315,32 @@
  11632. /*
  11633. * Read side functions for starting and finalizing a read side section.
  11634. */
  11635. +#ifndef CONFIG_PREEMPT_RT_FULL
  11636. static inline unsigned read_seqbegin(const seqlock_t *sl)
  11637. {
  11638. return read_seqcount_begin(&sl->seqcount);
  11639. }
  11640. +#else
  11641. +/*
  11642. + * Starvation safe read side for RT
  11643. + */
  11644. +static inline unsigned read_seqbegin(seqlock_t *sl)
  11645. +{
  11646. + unsigned ret;
  11647. +
  11648. +repeat:
  11649. + ret = ACCESS_ONCE(sl->seqcount.sequence);
  11650. + if (unlikely(ret & 1)) {
  11651. + /*
  11652. + * Take the lock and let the writer proceed (i.e. evtl
  11653. + * boost it), otherwise we could loop here forever.
  11654. + */
  11655. + spin_unlock_wait(&sl->lock);
  11656. + goto repeat;
  11657. + }
  11658. + return ret;
  11659. +}
  11660. +#endif
  11661. static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  11662. {
  11663. @@ -323,36 +355,36 @@
  11664. static inline void write_seqlock(seqlock_t *sl)
  11665. {
  11666. spin_lock(&sl->lock);
  11667. - write_seqcount_begin(&sl->seqcount);
  11668. + __raw_write_seqcount_begin(&sl->seqcount);
  11669. }
  11670. static inline void write_sequnlock(seqlock_t *sl)
  11671. {
  11672. - write_seqcount_end(&sl->seqcount);
  11673. + __raw_write_seqcount_end(&sl->seqcount);
  11674. spin_unlock(&sl->lock);
  11675. }
  11676. static inline void write_seqlock_bh(seqlock_t *sl)
  11677. {
  11678. spin_lock_bh(&sl->lock);
  11679. - write_seqcount_begin(&sl->seqcount);
  11680. + __raw_write_seqcount_begin(&sl->seqcount);
  11681. }
  11682. static inline void write_sequnlock_bh(seqlock_t *sl)
  11683. {
  11684. - write_seqcount_end(&sl->seqcount);
  11685. + __raw_write_seqcount_end(&sl->seqcount);
  11686. spin_unlock_bh(&sl->lock);
  11687. }
  11688. static inline void write_seqlock_irq(seqlock_t *sl)
  11689. {
  11690. spin_lock_irq(&sl->lock);
  11691. - write_seqcount_begin(&sl->seqcount);
  11692. + __raw_write_seqcount_begin(&sl->seqcount);
  11693. }
  11694. static inline void write_sequnlock_irq(seqlock_t *sl)
  11695. {
  11696. - write_seqcount_end(&sl->seqcount);
  11697. + __raw_write_seqcount_end(&sl->seqcount);
  11698. spin_unlock_irq(&sl->lock);
  11699. }
  11700. @@ -361,7 +393,7 @@
  11701. unsigned long flags;
  11702. spin_lock_irqsave(&sl->lock, flags);
  11703. - write_seqcount_begin(&sl->seqcount);
  11704. + __raw_write_seqcount_begin(&sl->seqcount);
  11705. return flags;
  11706. }
  11707. @@ -371,7 +403,7 @@
  11708. static inline void
  11709. write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
  11710. {
  11711. - write_seqcount_end(&sl->seqcount);
  11712. + __raw_write_seqcount_end(&sl->seqcount);
  11713. spin_unlock_irqrestore(&sl->lock, flags);
  11714. }
  11715. diff -Nur linux-4.1.20.orig/include/linux/signal.h linux-4.1.20/include/linux/signal.h
  11716. --- linux-4.1.20.orig/include/linux/signal.h 2016-03-17 19:11:03.000000000 +0100
  11717. +++ linux-4.1.20/include/linux/signal.h 2016-03-21 20:18:33.000000000 +0100
  11718. @@ -218,6 +218,7 @@
  11719. }
  11720. extern void flush_sigqueue(struct sigpending *queue);
  11721. +extern void flush_task_sigqueue(struct task_struct *tsk);
  11722. /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
  11723. static inline int valid_signal(unsigned long sig)
  11724. diff -Nur linux-4.1.20.orig/include/linux/skbuff.h linux-4.1.20/include/linux/skbuff.h
  11725. --- linux-4.1.20.orig/include/linux/skbuff.h 2016-03-17 19:11:03.000000000 +0100
  11726. +++ linux-4.1.20/include/linux/skbuff.h 2016-03-21 20:18:33.000000000 +0100
  11727. @@ -187,6 +187,7 @@
  11728. __u32 qlen;
  11729. spinlock_t lock;
  11730. + raw_spinlock_t raw_lock;
  11731. };
  11732. struct sk_buff;
  11733. @@ -1337,6 +1338,12 @@
  11734. __skb_queue_head_init(list);
  11735. }
  11736. +static inline void skb_queue_head_init_raw(struct sk_buff_head *list)
  11737. +{
  11738. + raw_spin_lock_init(&list->raw_lock);
  11739. + __skb_queue_head_init(list);
  11740. +}
  11741. +
  11742. static inline void skb_queue_head_init_class(struct sk_buff_head *list,
  11743. struct lock_class_key *class)
  11744. {
  11745. diff -Nur linux-4.1.20.orig/include/linux/smp.h linux-4.1.20/include/linux/smp.h
  11746. --- linux-4.1.20.orig/include/linux/smp.h 2016-03-17 19:11:03.000000000 +0100
  11747. +++ linux-4.1.20/include/linux/smp.h 2016-03-21 20:18:33.000000000 +0100
  11748. @@ -185,6 +185,9 @@
  11749. #define get_cpu() ({ preempt_disable(); smp_processor_id(); })
  11750. #define put_cpu() preempt_enable()
  11751. +#define get_cpu_light() ({ migrate_disable(); smp_processor_id(); })
  11752. +#define put_cpu_light() migrate_enable()
  11753. +
  11754. /*
  11755. * Callback to arch code if there's nosmp or maxcpus=0 on the
  11756. * boot command line:
  11757. diff -Nur linux-4.1.20.orig/include/linux/spinlock.h linux-4.1.20/include/linux/spinlock.h
  11758. --- linux-4.1.20.orig/include/linux/spinlock.h 2016-03-17 19:11:03.000000000 +0100
  11759. +++ linux-4.1.20/include/linux/spinlock.h 2016-03-21 20:18:33.000000000 +0100
  11760. @@ -281,7 +281,11 @@
  11761. #define raw_spin_can_lock(lock) (!raw_spin_is_locked(lock))
  11762. /* Include rwlock functions */
  11763. -#include <linux/rwlock.h>
  11764. +#ifdef CONFIG_PREEMPT_RT_FULL
  11765. +# include <linux/rwlock_rt.h>
  11766. +#else
  11767. +# include <linux/rwlock.h>
  11768. +#endif
  11769. /*
  11770. * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
  11771. @@ -292,6 +296,10 @@
  11772. # include <linux/spinlock_api_up.h>
  11773. #endif
  11774. +#ifdef CONFIG_PREEMPT_RT_FULL
  11775. +# include <linux/spinlock_rt.h>
  11776. +#else /* PREEMPT_RT_FULL */
  11777. +
  11778. /*
  11779. * Map the spin_lock functions to the raw variants for PREEMPT_RT=n
  11780. */
  11781. @@ -426,4 +434,6 @@
  11782. #define atomic_dec_and_lock(atomic, lock) \
  11783. __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
  11784. +#endif /* !PREEMPT_RT_FULL */
  11785. +
  11786. #endif /* __LINUX_SPINLOCK_H */
  11787. diff -Nur linux-4.1.20.orig/include/linux/spinlock_api_smp.h linux-4.1.20/include/linux/spinlock_api_smp.h
  11788. --- linux-4.1.20.orig/include/linux/spinlock_api_smp.h 2016-03-17 19:11:03.000000000 +0100
  11789. +++ linux-4.1.20/include/linux/spinlock_api_smp.h 2016-03-21 20:18:33.000000000 +0100
  11790. @@ -189,6 +189,8 @@
  11791. return 0;
  11792. }
  11793. -#include <linux/rwlock_api_smp.h>
  11794. +#ifndef CONFIG_PREEMPT_RT_FULL
  11795. +# include <linux/rwlock_api_smp.h>
  11796. +#endif
  11797. #endif /* __LINUX_SPINLOCK_API_SMP_H */
  11798. diff -Nur linux-4.1.20.orig/include/linux/spinlock_rt.h linux-4.1.20/include/linux/spinlock_rt.h
  11799. --- linux-4.1.20.orig/include/linux/spinlock_rt.h 1970-01-01 01:00:00.000000000 +0100
  11800. +++ linux-4.1.20/include/linux/spinlock_rt.h 2016-03-21 20:18:33.000000000 +0100
  11801. @@ -0,0 +1,174 @@
  11802. +#ifndef __LINUX_SPINLOCK_RT_H
  11803. +#define __LINUX_SPINLOCK_RT_H
  11804. +
  11805. +#ifndef __LINUX_SPINLOCK_H
  11806. +#error Do not include directly. Use spinlock.h
  11807. +#endif
  11808. +
  11809. +#include <linux/bug.h>
  11810. +
  11811. +extern void
  11812. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key);
  11813. +
  11814. +#define spin_lock_init(slock) \
  11815. +do { \
  11816. + static struct lock_class_key __key; \
  11817. + \
  11818. + rt_mutex_init(&(slock)->lock); \
  11819. + __rt_spin_lock_init(slock, #slock, &__key); \
  11820. +} while (0)
  11821. +
  11822. +extern void __lockfunc rt_spin_lock(spinlock_t *lock);
  11823. +extern unsigned long __lockfunc rt_spin_lock_trace_flags(spinlock_t *lock);
  11824. +extern void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass);
  11825. +extern void __lockfunc rt_spin_unlock(spinlock_t *lock);
  11826. +extern void __lockfunc rt_spin_unlock_wait(spinlock_t *lock);
  11827. +extern int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags);
  11828. +extern int __lockfunc rt_spin_trylock_bh(spinlock_t *lock);
  11829. +extern int __lockfunc rt_spin_trylock(spinlock_t *lock);
  11830. +extern int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock);
  11831. +
  11832. +/*
  11833. + * lockdep-less calls, for derived types like rwlock:
  11834. + * (for trylock they can use rt_mutex_trylock() directly.
  11835. + */
  11836. +extern void __lockfunc __rt_spin_lock(struct rt_mutex *lock);
  11837. +extern void __lockfunc __rt_spin_unlock(struct rt_mutex *lock);
  11838. +extern int __lockfunc __rt_spin_trylock(struct rt_mutex *lock);
  11839. +
  11840. +#define spin_lock(lock) \
  11841. + do { \
  11842. + migrate_disable(); \
  11843. + rt_spin_lock(lock); \
  11844. + } while (0)
  11845. +
  11846. +#define spin_lock_bh(lock) \
  11847. + do { \
  11848. + local_bh_disable(); \
  11849. + migrate_disable(); \
  11850. + rt_spin_lock(lock); \
  11851. + } while (0)
  11852. +
  11853. +#define spin_lock_irq(lock) spin_lock(lock)
  11854. +
  11855. +#define spin_do_trylock(lock) __cond_lock(lock, rt_spin_trylock(lock))
  11856. +
  11857. +#define spin_trylock(lock) \
  11858. +({ \
  11859. + int __locked; \
  11860. + migrate_disable(); \
  11861. + __locked = spin_do_trylock(lock); \
  11862. + if (!__locked) \
  11863. + migrate_enable(); \
  11864. + __locked; \
  11865. +})
  11866. +
  11867. +#ifdef CONFIG_LOCKDEP
  11868. +# define spin_lock_nested(lock, subclass) \
  11869. + do { \
  11870. + migrate_disable(); \
  11871. + rt_spin_lock_nested(lock, subclass); \
  11872. + } while (0)
  11873. +
  11874. +#define spin_lock_bh_nested(lock, subclass) \
  11875. + do { \
  11876. + local_bh_disable(); \
  11877. + migrate_disable(); \
  11878. + rt_spin_lock_nested(lock, subclass); \
  11879. + } while (0)
  11880. +
  11881. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  11882. + do { \
  11883. + typecheck(unsigned long, flags); \
  11884. + flags = 0; \
  11885. + migrate_disable(); \
  11886. + rt_spin_lock_nested(lock, subclass); \
  11887. + } while (0)
  11888. +#else
  11889. +# define spin_lock_nested(lock, subclass) spin_lock(lock)
  11890. +# define spin_lock_bh_nested(lock, subclass) spin_lock_bh(lock)
  11891. +
  11892. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  11893. + do { \
  11894. + typecheck(unsigned long, flags); \
  11895. + flags = 0; \
  11896. + spin_lock(lock); \
  11897. + } while (0)
  11898. +#endif
  11899. +
  11900. +#define spin_lock_irqsave(lock, flags) \
  11901. + do { \
  11902. + typecheck(unsigned long, flags); \
  11903. + flags = 0; \
  11904. + spin_lock(lock); \
  11905. + } while (0)
  11906. +
  11907. +static inline unsigned long spin_lock_trace_flags(spinlock_t *lock)
  11908. +{
  11909. + unsigned long flags = 0;
  11910. +#ifdef CONFIG_TRACE_IRQFLAGS
  11911. + flags = rt_spin_lock_trace_flags(lock);
  11912. +#else
  11913. + spin_lock(lock); /* lock_local */
  11914. +#endif
  11915. + return flags;
  11916. +}
  11917. +
  11918. +/* FIXME: we need rt_spin_lock_nest_lock */
  11919. +#define spin_lock_nest_lock(lock, nest_lock) spin_lock_nested(lock, 0)
  11920. +
  11921. +#define spin_unlock(lock) \
  11922. + do { \
  11923. + rt_spin_unlock(lock); \
  11924. + migrate_enable(); \
  11925. + } while (0)
  11926. +
  11927. +#define spin_unlock_bh(lock) \
  11928. + do { \
  11929. + rt_spin_unlock(lock); \
  11930. + migrate_enable(); \
  11931. + local_bh_enable(); \
  11932. + } while (0)
  11933. +
  11934. +#define spin_unlock_irq(lock) spin_unlock(lock)
  11935. +
  11936. +#define spin_unlock_irqrestore(lock, flags) \
  11937. + do { \
  11938. + typecheck(unsigned long, flags); \
  11939. + (void) flags; \
  11940. + spin_unlock(lock); \
  11941. + } while (0)
  11942. +
  11943. +#define spin_trylock_bh(lock) __cond_lock(lock, rt_spin_trylock_bh(lock))
  11944. +#define spin_trylock_irq(lock) spin_trylock(lock)
  11945. +
  11946. +#define spin_trylock_irqsave(lock, flags) \
  11947. + rt_spin_trylock_irqsave(lock, &(flags))
  11948. +
  11949. +#define spin_unlock_wait(lock) rt_spin_unlock_wait(lock)
  11950. +
  11951. +#ifdef CONFIG_GENERIC_LOCKBREAK
  11952. +# define spin_is_contended(lock) ((lock)->break_lock)
  11953. +#else
  11954. +# define spin_is_contended(lock) (((void)(lock), 0))
  11955. +#endif
  11956. +
  11957. +static inline int spin_can_lock(spinlock_t *lock)
  11958. +{
  11959. + return !rt_mutex_is_locked(&lock->lock);
  11960. +}
  11961. +
  11962. +static inline int spin_is_locked(spinlock_t *lock)
  11963. +{
  11964. + return rt_mutex_is_locked(&lock->lock);
  11965. +}
  11966. +
  11967. +static inline void assert_spin_locked(spinlock_t *lock)
  11968. +{
  11969. + BUG_ON(!spin_is_locked(lock));
  11970. +}
  11971. +
  11972. +#define atomic_dec_and_lock(atomic, lock) \
  11973. + atomic_dec_and_spin_lock(atomic, lock)
  11974. +
  11975. +#endif
  11976. diff -Nur linux-4.1.20.orig/include/linux/spinlock_types.h linux-4.1.20/include/linux/spinlock_types.h
  11977. --- linux-4.1.20.orig/include/linux/spinlock_types.h 2016-03-17 19:11:03.000000000 +0100
  11978. +++ linux-4.1.20/include/linux/spinlock_types.h 2016-03-21 20:18:33.000000000 +0100
  11979. @@ -9,80 +9,15 @@
  11980. * Released under the General Public License (GPL).
  11981. */
  11982. -#if defined(CONFIG_SMP)
  11983. -# include <asm/spinlock_types.h>
  11984. -#else
  11985. -# include <linux/spinlock_types_up.h>
  11986. -#endif
  11987. -
  11988. -#include <linux/lockdep.h>
  11989. -
  11990. -typedef struct raw_spinlock {
  11991. - arch_spinlock_t raw_lock;
  11992. -#ifdef CONFIG_GENERIC_LOCKBREAK
  11993. - unsigned int break_lock;
  11994. -#endif
  11995. -#ifdef CONFIG_DEBUG_SPINLOCK
  11996. - unsigned int magic, owner_cpu;
  11997. - void *owner;
  11998. -#endif
  11999. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12000. - struct lockdep_map dep_map;
  12001. -#endif
  12002. -} raw_spinlock_t;
  12003. -
  12004. -#define SPINLOCK_MAGIC 0xdead4ead
  12005. -
  12006. -#define SPINLOCK_OWNER_INIT ((void *)-1L)
  12007. -
  12008. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12009. -# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  12010. -#else
  12011. -# define SPIN_DEP_MAP_INIT(lockname)
  12012. -#endif
  12013. +#include <linux/spinlock_types_raw.h>
  12014. -#ifdef CONFIG_DEBUG_SPINLOCK
  12015. -# define SPIN_DEBUG_INIT(lockname) \
  12016. - .magic = SPINLOCK_MAGIC, \
  12017. - .owner_cpu = -1, \
  12018. - .owner = SPINLOCK_OWNER_INIT,
  12019. +#ifndef CONFIG_PREEMPT_RT_FULL
  12020. +# include <linux/spinlock_types_nort.h>
  12021. +# include <linux/rwlock_types.h>
  12022. #else
  12023. -# define SPIN_DEBUG_INIT(lockname)
  12024. +# include <linux/rtmutex.h>
  12025. +# include <linux/spinlock_types_rt.h>
  12026. +# include <linux/rwlock_types_rt.h>
  12027. #endif
  12028. -#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  12029. - { \
  12030. - .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  12031. - SPIN_DEBUG_INIT(lockname) \
  12032. - SPIN_DEP_MAP_INIT(lockname) }
  12033. -
  12034. -#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  12035. - (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  12036. -
  12037. -#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  12038. -
  12039. -typedef struct spinlock {
  12040. - union {
  12041. - struct raw_spinlock rlock;
  12042. -
  12043. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12044. -# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  12045. - struct {
  12046. - u8 __padding[LOCK_PADSIZE];
  12047. - struct lockdep_map dep_map;
  12048. - };
  12049. -#endif
  12050. - };
  12051. -} spinlock_t;
  12052. -
  12053. -#define __SPIN_LOCK_INITIALIZER(lockname) \
  12054. - { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  12055. -
  12056. -#define __SPIN_LOCK_UNLOCKED(lockname) \
  12057. - (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  12058. -
  12059. -#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  12060. -
  12061. -#include <linux/rwlock_types.h>
  12062. -
  12063. #endif /* __LINUX_SPINLOCK_TYPES_H */
  12064. diff -Nur linux-4.1.20.orig/include/linux/spinlock_types_nort.h linux-4.1.20/include/linux/spinlock_types_nort.h
  12065. --- linux-4.1.20.orig/include/linux/spinlock_types_nort.h 1970-01-01 01:00:00.000000000 +0100
  12066. +++ linux-4.1.20/include/linux/spinlock_types_nort.h 2016-03-21 20:18:33.000000000 +0100
  12067. @@ -0,0 +1,33 @@
  12068. +#ifndef __LINUX_SPINLOCK_TYPES_NORT_H
  12069. +#define __LINUX_SPINLOCK_TYPES_NORT_H
  12070. +
  12071. +#ifndef __LINUX_SPINLOCK_TYPES_H
  12072. +#error "Do not include directly. Include spinlock_types.h instead"
  12073. +#endif
  12074. +
  12075. +/*
  12076. + * The non RT version maps spinlocks to raw_spinlocks
  12077. + */
  12078. +typedef struct spinlock {
  12079. + union {
  12080. + struct raw_spinlock rlock;
  12081. +
  12082. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12083. +# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  12084. + struct {
  12085. + u8 __padding[LOCK_PADSIZE];
  12086. + struct lockdep_map dep_map;
  12087. + };
  12088. +#endif
  12089. + };
  12090. +} spinlock_t;
  12091. +
  12092. +#define __SPIN_LOCK_INITIALIZER(lockname) \
  12093. + { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  12094. +
  12095. +#define __SPIN_LOCK_UNLOCKED(lockname) \
  12096. + (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  12097. +
  12098. +#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  12099. +
  12100. +#endif
  12101. diff -Nur linux-4.1.20.orig/include/linux/spinlock_types_raw.h linux-4.1.20/include/linux/spinlock_types_raw.h
  12102. --- linux-4.1.20.orig/include/linux/spinlock_types_raw.h 1970-01-01 01:00:00.000000000 +0100
  12103. +++ linux-4.1.20/include/linux/spinlock_types_raw.h 2016-03-21 20:18:33.000000000 +0100
  12104. @@ -0,0 +1,56 @@
  12105. +#ifndef __LINUX_SPINLOCK_TYPES_RAW_H
  12106. +#define __LINUX_SPINLOCK_TYPES_RAW_H
  12107. +
  12108. +#if defined(CONFIG_SMP)
  12109. +# include <asm/spinlock_types.h>
  12110. +#else
  12111. +# include <linux/spinlock_types_up.h>
  12112. +#endif
  12113. +
  12114. +#include <linux/lockdep.h>
  12115. +
  12116. +typedef struct raw_spinlock {
  12117. + arch_spinlock_t raw_lock;
  12118. +#ifdef CONFIG_GENERIC_LOCKBREAK
  12119. + unsigned int break_lock;
  12120. +#endif
  12121. +#ifdef CONFIG_DEBUG_SPINLOCK
  12122. + unsigned int magic, owner_cpu;
  12123. + void *owner;
  12124. +#endif
  12125. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12126. + struct lockdep_map dep_map;
  12127. +#endif
  12128. +} raw_spinlock_t;
  12129. +
  12130. +#define SPINLOCK_MAGIC 0xdead4ead
  12131. +
  12132. +#define SPINLOCK_OWNER_INIT ((void *)-1L)
  12133. +
  12134. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12135. +# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  12136. +#else
  12137. +# define SPIN_DEP_MAP_INIT(lockname)
  12138. +#endif
  12139. +
  12140. +#ifdef CONFIG_DEBUG_SPINLOCK
  12141. +# define SPIN_DEBUG_INIT(lockname) \
  12142. + .magic = SPINLOCK_MAGIC, \
  12143. + .owner_cpu = -1, \
  12144. + .owner = SPINLOCK_OWNER_INIT,
  12145. +#else
  12146. +# define SPIN_DEBUG_INIT(lockname)
  12147. +#endif
  12148. +
  12149. +#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  12150. + { \
  12151. + .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  12152. + SPIN_DEBUG_INIT(lockname) \
  12153. + SPIN_DEP_MAP_INIT(lockname) }
  12154. +
  12155. +#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  12156. + (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  12157. +
  12158. +#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  12159. +
  12160. +#endif
  12161. diff -Nur linux-4.1.20.orig/include/linux/spinlock_types_rt.h linux-4.1.20/include/linux/spinlock_types_rt.h
  12162. --- linux-4.1.20.orig/include/linux/spinlock_types_rt.h 1970-01-01 01:00:00.000000000 +0100
  12163. +++ linux-4.1.20/include/linux/spinlock_types_rt.h 2016-03-21 20:18:33.000000000 +0100
  12164. @@ -0,0 +1,51 @@
  12165. +#ifndef __LINUX_SPINLOCK_TYPES_RT_H
  12166. +#define __LINUX_SPINLOCK_TYPES_RT_H
  12167. +
  12168. +#ifndef __LINUX_SPINLOCK_TYPES_H
  12169. +#error "Do not include directly. Include spinlock_types.h instead"
  12170. +#endif
  12171. +
  12172. +#include <linux/cache.h>
  12173. +
  12174. +/*
  12175. + * PREEMPT_RT: spinlocks - an RT mutex plus lock-break field:
  12176. + */
  12177. +typedef struct spinlock {
  12178. + struct rt_mutex lock;
  12179. + unsigned int break_lock;
  12180. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12181. + struct lockdep_map dep_map;
  12182. +#endif
  12183. +} spinlock_t;
  12184. +
  12185. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  12186. +# define __RT_SPIN_INITIALIZER(name) \
  12187. + { \
  12188. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  12189. + .save_state = 1, \
  12190. + .file = __FILE__, \
  12191. + .line = __LINE__ , \
  12192. + }
  12193. +#else
  12194. +# define __RT_SPIN_INITIALIZER(name) \
  12195. + { \
  12196. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  12197. + .save_state = 1, \
  12198. + }
  12199. +#endif
  12200. +
  12201. +/*
  12202. +.wait_list = PLIST_HEAD_INIT_RAW((name).lock.wait_list, (name).lock.wait_lock)
  12203. +*/
  12204. +
  12205. +#define __SPIN_LOCK_UNLOCKED(name) \
  12206. + { .lock = __RT_SPIN_INITIALIZER(name.lock), \
  12207. + SPIN_DEP_MAP_INIT(name) }
  12208. +
  12209. +#define __DEFINE_SPINLOCK(name) \
  12210. + spinlock_t name = __SPIN_LOCK_UNLOCKED(name)
  12211. +
  12212. +#define DEFINE_SPINLOCK(name) \
  12213. + spinlock_t name __cacheline_aligned_in_smp = __SPIN_LOCK_UNLOCKED(name)
  12214. +
  12215. +#endif
  12216. diff -Nur linux-4.1.20.orig/include/linux/srcu.h linux-4.1.20/include/linux/srcu.h
  12217. --- linux-4.1.20.orig/include/linux/srcu.h 2016-03-17 19:11:03.000000000 +0100
  12218. +++ linux-4.1.20/include/linux/srcu.h 2016-03-21 20:18:33.000000000 +0100
  12219. @@ -84,10 +84,10 @@
  12220. void process_srcu(struct work_struct *work);
  12221. -#define __SRCU_STRUCT_INIT(name) \
  12222. +#define __SRCU_STRUCT_INIT(name, pcpu_name) \
  12223. { \
  12224. .completed = -300, \
  12225. - .per_cpu_ref = &name##_srcu_array, \
  12226. + .per_cpu_ref = &pcpu_name, \
  12227. .queue_lock = __SPIN_LOCK_UNLOCKED(name.queue_lock), \
  12228. .running = false, \
  12229. .batch_queue = RCU_BATCH_INIT(name.batch_queue), \
  12230. @@ -104,7 +104,7 @@
  12231. */
  12232. #define __DEFINE_SRCU(name, is_static) \
  12233. static DEFINE_PER_CPU(struct srcu_struct_array, name##_srcu_array);\
  12234. - is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name)
  12235. + is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name##_srcu_array)
  12236. #define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
  12237. #define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
  12238. diff -Nur linux-4.1.20.orig/include/linux/swap.h linux-4.1.20/include/linux/swap.h
  12239. --- linux-4.1.20.orig/include/linux/swap.h 2016-03-17 19:11:03.000000000 +0100
  12240. +++ linux-4.1.20/include/linux/swap.h 2016-03-21 20:18:33.000000000 +0100
  12241. @@ -11,6 +11,7 @@
  12242. #include <linux/fs.h>
  12243. #include <linux/atomic.h>
  12244. #include <linux/page-flags.h>
  12245. +#include <linux/locallock.h>
  12246. #include <asm/page.h>
  12247. struct notifier_block;
  12248. @@ -252,7 +253,8 @@
  12249. void *workingset_eviction(struct address_space *mapping, struct page *page);
  12250. bool workingset_refault(void *shadow);
  12251. void workingset_activation(struct page *page);
  12252. -extern struct list_lru workingset_shadow_nodes;
  12253. +extern struct list_lru __workingset_shadow_nodes;
  12254. +DECLARE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  12255. static inline unsigned int workingset_node_pages(struct radix_tree_node *node)
  12256. {
  12257. @@ -296,6 +298,7 @@
  12258. /* linux/mm/swap.c */
  12259. +DECLARE_LOCAL_IRQ_LOCK(swapvec_lock);
  12260. extern void lru_cache_add(struct page *);
  12261. extern void lru_cache_add_anon(struct page *page);
  12262. extern void lru_cache_add_file(struct page *page);
  12263. diff -Nur linux-4.1.20.orig/include/linux/thread_info.h linux-4.1.20/include/linux/thread_info.h
  12264. --- linux-4.1.20.orig/include/linux/thread_info.h 2016-03-17 19:11:03.000000000 +0100
  12265. +++ linux-4.1.20/include/linux/thread_info.h 2016-03-21 20:18:33.000000000 +0100
  12266. @@ -102,7 +102,17 @@
  12267. #define test_thread_flag(flag) \
  12268. test_ti_thread_flag(current_thread_info(), flag)
  12269. -#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  12270. +#ifdef CONFIG_PREEMPT_LAZY
  12271. +#define tif_need_resched() (test_thread_flag(TIF_NEED_RESCHED) || \
  12272. + test_thread_flag(TIF_NEED_RESCHED_LAZY))
  12273. +#define tif_need_resched_now() (test_thread_flag(TIF_NEED_RESCHED))
  12274. +#define tif_need_resched_lazy() test_thread_flag(TIF_NEED_RESCHED_LAZY))
  12275. +
  12276. +#else
  12277. +#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  12278. +#define tif_need_resched_now() test_thread_flag(TIF_NEED_RESCHED)
  12279. +#define tif_need_resched_lazy() 0
  12280. +#endif
  12281. #if defined TIF_RESTORE_SIGMASK && !defined HAVE_SET_RESTORE_SIGMASK
  12282. /*
  12283. diff -Nur linux-4.1.20.orig/include/linux/timer.h linux-4.1.20/include/linux/timer.h
  12284. --- linux-4.1.20.orig/include/linux/timer.h 2016-03-17 19:11:03.000000000 +0100
  12285. +++ linux-4.1.20/include/linux/timer.h 2016-03-21 20:18:33.000000000 +0100
  12286. @@ -241,7 +241,7 @@
  12287. extern int try_to_del_timer_sync(struct timer_list *timer);
  12288. -#ifdef CONFIG_SMP
  12289. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  12290. extern int del_timer_sync(struct timer_list *timer);
  12291. #else
  12292. # define del_timer_sync(t) del_timer(t)
  12293. diff -Nur linux-4.1.20.orig/include/linux/uaccess.h linux-4.1.20/include/linux/uaccess.h
  12294. --- linux-4.1.20.orig/include/linux/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  12295. +++ linux-4.1.20/include/linux/uaccess.h 2016-03-21 20:18:33.000000000 +0100
  12296. @@ -1,21 +1,31 @@
  12297. #ifndef __LINUX_UACCESS_H__
  12298. #define __LINUX_UACCESS_H__
  12299. -#include <linux/preempt.h>
  12300. +#include <linux/sched.h>
  12301. #include <asm/uaccess.h>
  12302. +static __always_inline void pagefault_disabled_inc(void)
  12303. +{
  12304. + current->pagefault_disabled++;
  12305. +}
  12306. +
  12307. +static __always_inline void pagefault_disabled_dec(void)
  12308. +{
  12309. + current->pagefault_disabled--;
  12310. + WARN_ON(current->pagefault_disabled < 0);
  12311. +}
  12312. +
  12313. /*
  12314. - * These routines enable/disable the pagefault handler in that
  12315. - * it will not take any locks and go straight to the fixup table.
  12316. + * These routines enable/disable the pagefault handler. If disabled, it will
  12317. + * not take any locks and go straight to the fixup table.
  12318. *
  12319. - * They have great resemblance to the preempt_disable/enable calls
  12320. - * and in fact they are identical; this is because currently there is
  12321. - * no other way to make the pagefault handlers do this. So we do
  12322. - * disable preemption but we don't necessarily care about that.
  12323. + * User access methods will not sleep when called from a pagefault_disabled()
  12324. + * environment.
  12325. */
  12326. static inline void pagefault_disable(void)
  12327. {
  12328. - preempt_count_inc();
  12329. + migrate_disable();
  12330. + pagefault_disabled_inc();
  12331. /*
  12332. * make sure to have issued the store before a pagefault
  12333. * can hit.
  12334. @@ -25,18 +35,32 @@
  12335. static inline void pagefault_enable(void)
  12336. {
  12337. -#ifndef CONFIG_PREEMPT
  12338. /*
  12339. * make sure to issue those last loads/stores before enabling
  12340. * the pagefault handler again.
  12341. */
  12342. barrier();
  12343. - preempt_count_dec();
  12344. -#else
  12345. - preempt_enable();
  12346. -#endif
  12347. + pagefault_disabled_dec();
  12348. + migrate_enable();
  12349. }
  12350. +/*
  12351. + * Is the pagefault handler disabled? If so, user access methods will not sleep.
  12352. + */
  12353. +#define pagefault_disabled() (current->pagefault_disabled != 0)
  12354. +
  12355. +/*
  12356. + * The pagefault handler is in general disabled by pagefault_disable() or
  12357. + * when in irq context (via in_atomic()).
  12358. + *
  12359. + * This function should only be used by the fault handlers. Other users should
  12360. + * stick to pagefault_disabled().
  12361. + * Please NEVER use preempt_disable() to disable the fault handler. With
  12362. + * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled.
  12363. + * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT.
  12364. + */
  12365. +#define faulthandler_disabled() (pagefault_disabled() || in_atomic())
  12366. +
  12367. #ifndef ARCH_HAS_NOCACHE_UACCESS
  12368. static inline unsigned long __copy_from_user_inatomic_nocache(void *to,
  12369. diff -Nur linux-4.1.20.orig/include/linux/uprobes.h linux-4.1.20/include/linux/uprobes.h
  12370. --- linux-4.1.20.orig/include/linux/uprobes.h 2016-03-17 19:11:03.000000000 +0100
  12371. +++ linux-4.1.20/include/linux/uprobes.h 2016-03-21 20:18:33.000000000 +0100
  12372. @@ -27,6 +27,7 @@
  12373. #include <linux/errno.h>
  12374. #include <linux/rbtree.h>
  12375. #include <linux/types.h>
  12376. +#include <linux/wait.h>
  12377. struct vm_area_struct;
  12378. struct mm_struct;
  12379. diff -Nur linux-4.1.20.orig/include/linux/vmstat.h linux-4.1.20/include/linux/vmstat.h
  12380. --- linux-4.1.20.orig/include/linux/vmstat.h 2016-03-17 19:11:03.000000000 +0100
  12381. +++ linux-4.1.20/include/linux/vmstat.h 2016-03-21 20:18:33.000000000 +0100
  12382. @@ -33,7 +33,9 @@
  12383. */
  12384. static inline void __count_vm_event(enum vm_event_item item)
  12385. {
  12386. + preempt_disable_rt();
  12387. raw_cpu_inc(vm_event_states.event[item]);
  12388. + preempt_enable_rt();
  12389. }
  12390. static inline void count_vm_event(enum vm_event_item item)
  12391. @@ -43,7 +45,9 @@
  12392. static inline void __count_vm_events(enum vm_event_item item, long delta)
  12393. {
  12394. + preempt_disable_rt();
  12395. raw_cpu_add(vm_event_states.event[item], delta);
  12396. + preempt_enable_rt();
  12397. }
  12398. static inline void count_vm_events(enum vm_event_item item, long delta)
  12399. diff -Nur linux-4.1.20.orig/include/linux/wait-simple.h linux-4.1.20/include/linux/wait-simple.h
  12400. --- linux-4.1.20.orig/include/linux/wait-simple.h 1970-01-01 01:00:00.000000000 +0100
  12401. +++ linux-4.1.20/include/linux/wait-simple.h 2016-03-21 20:18:33.000000000 +0100
  12402. @@ -0,0 +1,207 @@
  12403. +#ifndef _LINUX_WAIT_SIMPLE_H
  12404. +#define _LINUX_WAIT_SIMPLE_H
  12405. +
  12406. +#include <linux/spinlock.h>
  12407. +#include <linux/list.h>
  12408. +
  12409. +#include <asm/current.h>
  12410. +
  12411. +struct swaiter {
  12412. + struct task_struct *task;
  12413. + struct list_head node;
  12414. +};
  12415. +
  12416. +#define DEFINE_SWAITER(name) \
  12417. + struct swaiter name = { \
  12418. + .task = current, \
  12419. + .node = LIST_HEAD_INIT((name).node), \
  12420. + }
  12421. +
  12422. +struct swait_head {
  12423. + raw_spinlock_t lock;
  12424. + struct list_head list;
  12425. +};
  12426. +
  12427. +#define SWAIT_HEAD_INITIALIZER(name) { \
  12428. + .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
  12429. + .list = LIST_HEAD_INIT((name).list), \
  12430. + }
  12431. +
  12432. +#define DEFINE_SWAIT_HEAD(name) \
  12433. + struct swait_head name = SWAIT_HEAD_INITIALIZER(name)
  12434. +
  12435. +extern void __init_swait_head(struct swait_head *h, struct lock_class_key *key);
  12436. +
  12437. +#define init_swait_head(swh) \
  12438. + do { \
  12439. + static struct lock_class_key __key; \
  12440. + \
  12441. + __init_swait_head((swh), &__key); \
  12442. + } while (0)
  12443. +
  12444. +/*
  12445. + * Waiter functions
  12446. + */
  12447. +extern void swait_prepare_locked(struct swait_head *head, struct swaiter *w);
  12448. +extern void swait_prepare(struct swait_head *head, struct swaiter *w, int state);
  12449. +extern void swait_finish_locked(struct swait_head *head, struct swaiter *w);
  12450. +extern void swait_finish(struct swait_head *head, struct swaiter *w);
  12451. +
  12452. +/* Check whether a head has waiters enqueued */
  12453. +static inline bool swaitqueue_active(struct swait_head *h)
  12454. +{
  12455. + /* Make sure the condition is visible before checking list_empty() */
  12456. + smp_mb();
  12457. + return !list_empty(&h->list);
  12458. +}
  12459. +
  12460. +/*
  12461. + * Wakeup functions
  12462. + */
  12463. +extern unsigned int __swait_wake(struct swait_head *head, unsigned int state, unsigned int num);
  12464. +extern unsigned int __swait_wake_locked(struct swait_head *head, unsigned int state, unsigned int num);
  12465. +
  12466. +#define swait_wake(head) __swait_wake(head, TASK_NORMAL, 1)
  12467. +#define swait_wake_interruptible(head) __swait_wake(head, TASK_INTERRUPTIBLE, 1)
  12468. +#define swait_wake_all(head) __swait_wake(head, TASK_NORMAL, 0)
  12469. +#define swait_wake_all_interruptible(head) __swait_wake(head, TASK_INTERRUPTIBLE, 0)
  12470. +
  12471. +/*
  12472. + * Event API
  12473. + */
  12474. +#define __swait_event(wq, condition) \
  12475. +do { \
  12476. + DEFINE_SWAITER(__wait); \
  12477. + \
  12478. + for (;;) { \
  12479. + swait_prepare(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
  12480. + if (condition) \
  12481. + break; \
  12482. + schedule(); \
  12483. + } \
  12484. + swait_finish(&wq, &__wait); \
  12485. +} while (0)
  12486. +
  12487. +/**
  12488. + * swait_event - sleep until a condition gets true
  12489. + * @wq: the waitqueue to wait on
  12490. + * @condition: a C expression for the event to wait for
  12491. + *
  12492. + * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  12493. + * @condition evaluates to true. The @condition is checked each time
  12494. + * the waitqueue @wq is woken up.
  12495. + *
  12496. + * wake_up() has to be called after changing any variable that could
  12497. + * change the result of the wait condition.
  12498. + */
  12499. +#define swait_event(wq, condition) \
  12500. +do { \
  12501. + if (condition) \
  12502. + break; \
  12503. + __swait_event(wq, condition); \
  12504. +} while (0)
  12505. +
  12506. +#define __swait_event_interruptible(wq, condition, ret) \
  12507. +do { \
  12508. + DEFINE_SWAITER(__wait); \
  12509. + \
  12510. + for (;;) { \
  12511. + swait_prepare(&wq, &__wait, TASK_INTERRUPTIBLE); \
  12512. + if (condition) \
  12513. + break; \
  12514. + if (signal_pending(current)) { \
  12515. + ret = -ERESTARTSYS; \
  12516. + break; \
  12517. + } \
  12518. + schedule(); \
  12519. + } \
  12520. + swait_finish(&wq, &__wait); \
  12521. +} while (0)
  12522. +
  12523. +#define __swait_event_interruptible_timeout(wq, condition, ret) \
  12524. +do { \
  12525. + DEFINE_SWAITER(__wait); \
  12526. + \
  12527. + for (;;) { \
  12528. + swait_prepare(&wq, &__wait, TASK_INTERRUPTIBLE); \
  12529. + if (condition) \
  12530. + break; \
  12531. + if (signal_pending(current)) { \
  12532. + ret = -ERESTARTSYS; \
  12533. + break; \
  12534. + } \
  12535. + ret = schedule_timeout(ret); \
  12536. + if (!ret) \
  12537. + break; \
  12538. + } \
  12539. + swait_finish(&wq, &__wait); \
  12540. +} while (0)
  12541. +
  12542. +/**
  12543. + * swait_event_interruptible - sleep until a condition gets true
  12544. + * @wq: the waitqueue to wait on
  12545. + * @condition: a C expression for the event to wait for
  12546. + *
  12547. + * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  12548. + * @condition evaluates to true. The @condition is checked each time
  12549. + * the waitqueue @wq is woken up.
  12550. + *
  12551. + * wake_up() has to be called after changing any variable that could
  12552. + * change the result of the wait condition.
  12553. + */
  12554. +#define swait_event_interruptible(wq, condition) \
  12555. +({ \
  12556. + int __ret = 0; \
  12557. + if (!(condition)) \
  12558. + __swait_event_interruptible(wq, condition, __ret); \
  12559. + __ret; \
  12560. +})
  12561. +
  12562. +#define swait_event_interruptible_timeout(wq, condition, timeout) \
  12563. +({ \
  12564. + int __ret = timeout; \
  12565. + if (!(condition)) \
  12566. + __swait_event_interruptible_timeout(wq, condition, __ret); \
  12567. + __ret; \
  12568. +})
  12569. +
  12570. +#define __swait_event_timeout(wq, condition, ret) \
  12571. +do { \
  12572. + DEFINE_SWAITER(__wait); \
  12573. + \
  12574. + for (;;) { \
  12575. + swait_prepare(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
  12576. + if (condition) \
  12577. + break; \
  12578. + ret = schedule_timeout(ret); \
  12579. + if (!ret) \
  12580. + break; \
  12581. + } \
  12582. + swait_finish(&wq, &__wait); \
  12583. +} while (0)
  12584. +
  12585. +/**
  12586. + * swait_event_timeout - sleep until a condition gets true or a timeout elapses
  12587. + * @wq: the waitqueue to wait on
  12588. + * @condition: a C expression for the event to wait for
  12589. + * @timeout: timeout, in jiffies
  12590. + *
  12591. + * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  12592. + * @condition evaluates to true. The @condition is checked each time
  12593. + * the waitqueue @wq is woken up.
  12594. + *
  12595. + * wake_up() has to be called after changing any variable that could
  12596. + * change the result of the wait condition.
  12597. + *
  12598. + * The function returns 0 if the @timeout elapsed, and the remaining
  12599. + * jiffies if the condition evaluated to true before the timeout elapsed.
  12600. + */
  12601. +#define swait_event_timeout(wq, condition, timeout) \
  12602. +({ \
  12603. + long __ret = timeout; \
  12604. + if (!(condition)) \
  12605. + __swait_event_timeout(wq, condition, __ret); \
  12606. + __ret; \
  12607. +})
  12608. +
  12609. +#endif
  12610. diff -Nur linux-4.1.20.orig/include/linux/wait.h linux-4.1.20/include/linux/wait.h
  12611. --- linux-4.1.20.orig/include/linux/wait.h 2016-03-17 19:11:03.000000000 +0100
  12612. +++ linux-4.1.20/include/linux/wait.h 2016-03-21 20:18:33.000000000 +0100
  12613. @@ -8,6 +8,7 @@
  12614. #include <linux/spinlock.h>
  12615. #include <asm/current.h>
  12616. #include <uapi/linux/wait.h>
  12617. +#include <linux/atomic.h>
  12618. typedef struct __wait_queue wait_queue_t;
  12619. typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
  12620. diff -Nur linux-4.1.20.orig/include/linux/work-simple.h linux-4.1.20/include/linux/work-simple.h
  12621. --- linux-4.1.20.orig/include/linux/work-simple.h 1970-01-01 01:00:00.000000000 +0100
  12622. +++ linux-4.1.20/include/linux/work-simple.h 2016-03-21 20:18:33.000000000 +0100
  12623. @@ -0,0 +1,24 @@
  12624. +#ifndef _LINUX_SWORK_H
  12625. +#define _LINUX_SWORK_H
  12626. +
  12627. +#include <linux/list.h>
  12628. +
  12629. +struct swork_event {
  12630. + struct list_head item;
  12631. + unsigned long flags;
  12632. + void (*func)(struct swork_event *);
  12633. +};
  12634. +
  12635. +static inline void INIT_SWORK(struct swork_event *event,
  12636. + void (*func)(struct swork_event *))
  12637. +{
  12638. + event->flags = 0;
  12639. + event->func = func;
  12640. +}
  12641. +
  12642. +bool swork_queue(struct swork_event *sev);
  12643. +
  12644. +int swork_get(void);
  12645. +void swork_put(void);
  12646. +
  12647. +#endif /* _LINUX_SWORK_H */
  12648. diff -Nur linux-4.1.20.orig/include/net/dst.h linux-4.1.20/include/net/dst.h
  12649. --- linux-4.1.20.orig/include/net/dst.h 2016-03-17 19:11:03.000000000 +0100
  12650. +++ linux-4.1.20/include/net/dst.h 2016-03-21 20:18:33.000000000 +0100
  12651. @@ -436,7 +436,7 @@
  12652. static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n,
  12653. struct sk_buff *skb)
  12654. {
  12655. - const struct hh_cache *hh;
  12656. + struct hh_cache *hh;
  12657. if (dst->pending_confirm) {
  12658. unsigned long now = jiffies;
  12659. diff -Nur linux-4.1.20.orig/include/net/neighbour.h linux-4.1.20/include/net/neighbour.h
  12660. --- linux-4.1.20.orig/include/net/neighbour.h 2016-03-17 19:11:03.000000000 +0100
  12661. +++ linux-4.1.20/include/net/neighbour.h 2016-03-21 20:18:33.000000000 +0100
  12662. @@ -445,7 +445,7 @@
  12663. }
  12664. #endif
  12665. -static inline int neigh_hh_output(const struct hh_cache *hh, struct sk_buff *skb)
  12666. +static inline int neigh_hh_output(struct hh_cache *hh, struct sk_buff *skb)
  12667. {
  12668. unsigned int seq;
  12669. int hh_len;
  12670. @@ -500,7 +500,7 @@
  12671. #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
  12672. -static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
  12673. +static inline void neigh_ha_snapshot(char *dst, struct neighbour *n,
  12674. const struct net_device *dev)
  12675. {
  12676. unsigned int seq;
  12677. diff -Nur linux-4.1.20.orig/include/net/netns/ipv4.h linux-4.1.20/include/net/netns/ipv4.h
  12678. --- linux-4.1.20.orig/include/net/netns/ipv4.h 2016-03-17 19:11:03.000000000 +0100
  12679. +++ linux-4.1.20/include/net/netns/ipv4.h 2016-03-21 20:18:33.000000000 +0100
  12680. @@ -69,6 +69,7 @@
  12681. int sysctl_icmp_echo_ignore_all;
  12682. int sysctl_icmp_echo_ignore_broadcasts;
  12683. + int sysctl_icmp_echo_sysrq;
  12684. int sysctl_icmp_ignore_bogus_error_responses;
  12685. int sysctl_icmp_ratelimit;
  12686. int sysctl_icmp_ratemask;
  12687. diff -Nur linux-4.1.20.orig/include/trace/events/hist.h linux-4.1.20/include/trace/events/hist.h
  12688. --- linux-4.1.20.orig/include/trace/events/hist.h 1970-01-01 01:00:00.000000000 +0100
  12689. +++ linux-4.1.20/include/trace/events/hist.h 2016-03-21 20:18:33.000000000 +0100
  12690. @@ -0,0 +1,74 @@
  12691. +#undef TRACE_SYSTEM
  12692. +#define TRACE_SYSTEM hist
  12693. +
  12694. +#if !defined(_TRACE_HIST_H) || defined(TRACE_HEADER_MULTI_READ)
  12695. +#define _TRACE_HIST_H
  12696. +
  12697. +#include "latency_hist.h"
  12698. +#include <linux/tracepoint.h>
  12699. +
  12700. +#if !defined(CONFIG_PREEMPT_OFF_HIST) && !defined(CONFIG_INTERRUPT_OFF_HIST)
  12701. +#define trace_preemptirqsoff_hist(a, b)
  12702. +#define trace_preemptirqsoff_hist_rcuidle(a, b)
  12703. +#else
  12704. +TRACE_EVENT(preemptirqsoff_hist,
  12705. +
  12706. + TP_PROTO(int reason, int starthist),
  12707. +
  12708. + TP_ARGS(reason, starthist),
  12709. +
  12710. + TP_STRUCT__entry(
  12711. + __field(int, reason)
  12712. + __field(int, starthist)
  12713. + ),
  12714. +
  12715. + TP_fast_assign(
  12716. + __entry->reason = reason;
  12717. + __entry->starthist = starthist;
  12718. + ),
  12719. +
  12720. + TP_printk("reason=%s starthist=%s", getaction(__entry->reason),
  12721. + __entry->starthist ? "start" : "stop")
  12722. +);
  12723. +#endif
  12724. +
  12725. +#ifndef CONFIG_MISSED_TIMER_OFFSETS_HIST
  12726. +#define trace_hrtimer_interrupt(a, b, c, d)
  12727. +#define trace_hrtimer_interrupt_rcuidle(a, b, c, d)
  12728. +#else
  12729. +TRACE_EVENT(hrtimer_interrupt,
  12730. +
  12731. + TP_PROTO(int cpu, long long offset, struct task_struct *curr,
  12732. + struct task_struct *task),
  12733. +
  12734. + TP_ARGS(cpu, offset, curr, task),
  12735. +
  12736. + TP_STRUCT__entry(
  12737. + __field(int, cpu)
  12738. + __field(long long, offset)
  12739. + __array(char, ccomm, TASK_COMM_LEN)
  12740. + __field(int, cprio)
  12741. + __array(char, tcomm, TASK_COMM_LEN)
  12742. + __field(int, tprio)
  12743. + ),
  12744. +
  12745. + TP_fast_assign(
  12746. + __entry->cpu = cpu;
  12747. + __entry->offset = offset;
  12748. + memcpy(__entry->ccomm, curr->comm, TASK_COMM_LEN);
  12749. + __entry->cprio = curr->prio;
  12750. + memcpy(__entry->tcomm, task != NULL ? task->comm : "<none>",
  12751. + task != NULL ? TASK_COMM_LEN : 7);
  12752. + __entry->tprio = task != NULL ? task->prio : -1;
  12753. + ),
  12754. +
  12755. + TP_printk("cpu=%d offset=%lld curr=%s[%d] thread=%s[%d]",
  12756. + __entry->cpu, __entry->offset, __entry->ccomm,
  12757. + __entry->cprio, __entry->tcomm, __entry->tprio)
  12758. +);
  12759. +#endif
  12760. +
  12761. +#endif /* _TRACE_HIST_H */
  12762. +
  12763. +/* This part must be outside protection */
  12764. +#include <trace/define_trace.h>
  12765. diff -Nur linux-4.1.20.orig/include/trace/events/latency_hist.h linux-4.1.20/include/trace/events/latency_hist.h
  12766. --- linux-4.1.20.orig/include/trace/events/latency_hist.h 1970-01-01 01:00:00.000000000 +0100
  12767. +++ linux-4.1.20/include/trace/events/latency_hist.h 2016-03-21 20:18:33.000000000 +0100
  12768. @@ -0,0 +1,29 @@
  12769. +#ifndef _LATENCY_HIST_H
  12770. +#define _LATENCY_HIST_H
  12771. +
  12772. +enum hist_action {
  12773. + IRQS_ON,
  12774. + PREEMPT_ON,
  12775. + TRACE_STOP,
  12776. + IRQS_OFF,
  12777. + PREEMPT_OFF,
  12778. + TRACE_START,
  12779. +};
  12780. +
  12781. +static char *actions[] = {
  12782. + "IRQS_ON",
  12783. + "PREEMPT_ON",
  12784. + "TRACE_STOP",
  12785. + "IRQS_OFF",
  12786. + "PREEMPT_OFF",
  12787. + "TRACE_START",
  12788. +};
  12789. +
  12790. +static inline char *getaction(int action)
  12791. +{
  12792. + if (action >= 0 && action <= sizeof(actions)/sizeof(actions[0]))
  12793. + return actions[action];
  12794. + return "unknown";
  12795. +}
  12796. +
  12797. +#endif /* _LATENCY_HIST_H */
  12798. diff -Nur linux-4.1.20.orig/include/trace/events/sched.h linux-4.1.20/include/trace/events/sched.h
  12799. --- linux-4.1.20.orig/include/trace/events/sched.h 2016-03-17 19:11:03.000000000 +0100
  12800. +++ linux-4.1.20/include/trace/events/sched.h 2016-03-21 20:18:33.000000000 +0100
  12801. @@ -55,9 +55,9 @@
  12802. */
  12803. DECLARE_EVENT_CLASS(sched_wakeup_template,
  12804. - TP_PROTO(struct task_struct *p, int success),
  12805. + TP_PROTO(struct task_struct *p),
  12806. - TP_ARGS(__perf_task(p), success),
  12807. + TP_ARGS(__perf_task(p)),
  12808. TP_STRUCT__entry(
  12809. __array( char, comm, TASK_COMM_LEN )
  12810. @@ -71,25 +71,37 @@
  12811. memcpy(__entry->comm, p->comm, TASK_COMM_LEN);
  12812. __entry->pid = p->pid;
  12813. __entry->prio = p->prio;
  12814. - __entry->success = success;
  12815. + __entry->success = 1; /* rudiment, kill when possible */
  12816. __entry->target_cpu = task_cpu(p);
  12817. ),
  12818. - TP_printk("comm=%s pid=%d prio=%d success=%d target_cpu=%03d",
  12819. + TP_printk("comm=%s pid=%d prio=%d target_cpu=%03d",
  12820. __entry->comm, __entry->pid, __entry->prio,
  12821. - __entry->success, __entry->target_cpu)
  12822. + __entry->target_cpu)
  12823. );
  12824. +/*
  12825. + * Tracepoint called when waking a task; this tracepoint is guaranteed to be
  12826. + * called from the waking context.
  12827. + */
  12828. +DEFINE_EVENT(sched_wakeup_template, sched_waking,
  12829. + TP_PROTO(struct task_struct *p),
  12830. + TP_ARGS(p));
  12831. +
  12832. +/*
  12833. + * Tracepoint called when the task is actually woken; p->state == TASK_RUNNNG.
  12834. + * It it not always called from the waking context.
  12835. + */
  12836. DEFINE_EVENT(sched_wakeup_template, sched_wakeup,
  12837. - TP_PROTO(struct task_struct *p, int success),
  12838. - TP_ARGS(p, success));
  12839. + TP_PROTO(struct task_struct *p),
  12840. + TP_ARGS(p));
  12841. /*
  12842. * Tracepoint for waking up a new task:
  12843. */
  12844. DEFINE_EVENT(sched_wakeup_template, sched_wakeup_new,
  12845. - TP_PROTO(struct task_struct *p, int success),
  12846. - TP_ARGS(p, success));
  12847. + TP_PROTO(struct task_struct *p),
  12848. + TP_ARGS(p));
  12849. #ifdef CREATE_TRACE_POINTS
  12850. static inline long __trace_sched_switch_state(struct task_struct *p)
  12851. diff -Nur linux-4.1.20.orig/init/Kconfig linux-4.1.20/init/Kconfig
  12852. --- linux-4.1.20.orig/init/Kconfig 2016-03-17 19:11:03.000000000 +0100
  12853. +++ linux-4.1.20/init/Kconfig 2016-03-21 20:18:33.000000000 +0100
  12854. @@ -637,7 +637,7 @@
  12855. config RCU_FAST_NO_HZ
  12856. bool "Accelerate last non-dyntick-idle CPU's grace periods"
  12857. - depends on NO_HZ_COMMON && SMP
  12858. + depends on NO_HZ_COMMON && SMP && !PREEMPT_RT_FULL
  12859. default n
  12860. help
  12861. This option permits CPUs to enter dynticks-idle state even if
  12862. @@ -664,7 +664,7 @@
  12863. config RCU_BOOST
  12864. bool "Enable RCU priority boosting"
  12865. depends on RT_MUTEXES && PREEMPT_RCU
  12866. - default n
  12867. + default y if PREEMPT_RT_FULL
  12868. help
  12869. This option boosts the priority of preempted RCU readers that
  12870. block the current preemptible RCU grace period for too long.
  12871. @@ -1101,6 +1101,7 @@
  12872. config RT_GROUP_SCHED
  12873. bool "Group scheduling for SCHED_RR/FIFO"
  12874. depends on CGROUP_SCHED
  12875. + depends on !PREEMPT_RT_FULL
  12876. default n
  12877. help
  12878. This feature lets you explicitly allocate real CPU bandwidth
  12879. @@ -1688,6 +1689,7 @@
  12880. config SLAB
  12881. bool "SLAB"
  12882. + depends on !PREEMPT_RT_FULL
  12883. help
  12884. The regular slab allocator that is established and known to work
  12885. well in all environments. It organizes cache hot objects in
  12886. @@ -1706,6 +1708,7 @@
  12887. config SLOB
  12888. depends on EXPERT
  12889. bool "SLOB (Simple Allocator)"
  12890. + depends on !PREEMPT_RT_FULL
  12891. help
  12892. SLOB replaces the stock allocator with a drastically simpler
  12893. allocator. SLOB is generally more space efficient but
  12894. @@ -1715,7 +1718,7 @@
  12895. config SLUB_CPU_PARTIAL
  12896. default y
  12897. - depends on SLUB && SMP
  12898. + depends on SLUB && SMP && !PREEMPT_RT_FULL
  12899. bool "SLUB per cpu partial cache"
  12900. help
  12901. Per cpu partial caches accellerate objects allocation and freeing
  12902. diff -Nur linux-4.1.20.orig/init/Makefile linux-4.1.20/init/Makefile
  12903. --- linux-4.1.20.orig/init/Makefile 2016-03-17 19:11:03.000000000 +0100
  12904. +++ linux-4.1.20/init/Makefile 2016-03-21 20:18:33.000000000 +0100
  12905. @@ -33,4 +33,4 @@
  12906. include/generated/compile.h: FORCE
  12907. @$($(quiet)chk_compile.h)
  12908. $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkcompile_h $@ \
  12909. - "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CC) $(KBUILD_CFLAGS)"
  12910. + "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CONFIG_PREEMPT_RT_FULL)" "$(CC) $(KBUILD_CFLAGS)"
  12911. diff -Nur linux-4.1.20.orig/init/main.c linux-4.1.20/init/main.c
  12912. --- linux-4.1.20.orig/init/main.c 2016-03-17 19:11:03.000000000 +0100
  12913. +++ linux-4.1.20/init/main.c 2016-03-21 20:18:33.000000000 +0100
  12914. @@ -525,6 +525,7 @@
  12915. setup_command_line(command_line);
  12916. setup_nr_cpu_ids();
  12917. setup_per_cpu_areas();
  12918. + softirq_early_init();
  12919. smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
  12920. build_all_zonelists(NULL, NULL);
  12921. diff -Nur linux-4.1.20.orig/ipc/mqueue.c linux-4.1.20/ipc/mqueue.c
  12922. --- linux-4.1.20.orig/ipc/mqueue.c 2016-03-17 19:11:03.000000000 +0100
  12923. +++ linux-4.1.20/ipc/mqueue.c 2016-03-21 20:18:33.000000000 +0100
  12924. @@ -47,8 +47,7 @@
  12925. #define RECV 1
  12926. #define STATE_NONE 0
  12927. -#define STATE_PENDING 1
  12928. -#define STATE_READY 2
  12929. +#define STATE_READY 1
  12930. struct posix_msg_tree_node {
  12931. struct rb_node rb_node;
  12932. @@ -568,15 +567,12 @@
  12933. wq_add(info, sr, ewp);
  12934. for (;;) {
  12935. - set_current_state(TASK_INTERRUPTIBLE);
  12936. + __set_current_state(TASK_INTERRUPTIBLE);
  12937. spin_unlock(&info->lock);
  12938. time = schedule_hrtimeout_range_clock(timeout, 0,
  12939. HRTIMER_MODE_ABS, CLOCK_REALTIME);
  12940. - while (ewp->state == STATE_PENDING)
  12941. - cpu_relax();
  12942. -
  12943. if (ewp->state == STATE_READY) {
  12944. retval = 0;
  12945. goto out;
  12946. @@ -904,11 +900,15 @@
  12947. * list of waiting receivers. A sender checks that list before adding the new
  12948. * message into the message array. If there is a waiting receiver, then it
  12949. * bypasses the message array and directly hands the message over to the
  12950. - * receiver.
  12951. - * The receiver accepts the message and returns without grabbing the queue
  12952. - * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
  12953. - * are necessary. The same algorithm is used for sysv semaphores, see
  12954. - * ipc/sem.c for more details.
  12955. + * receiver. The receiver accepts the message and returns without grabbing the
  12956. + * queue spinlock:
  12957. + *
  12958. + * - Set pointer to message.
  12959. + * - Queue the receiver task for later wakeup (without the info->lock).
  12960. + * - Update its state to STATE_READY. Now the receiver can continue.
  12961. + * - Wake up the process after the lock is dropped. Should the process wake up
  12962. + * before this wakeup (due to a timeout or a signal) it will either see
  12963. + * STATE_READY and continue or acquire the lock to check the state again.
  12964. *
  12965. * The same algorithm is used for senders.
  12966. */
  12967. @@ -916,21 +916,29 @@
  12968. /* pipelined_send() - send a message directly to the task waiting in
  12969. * sys_mq_timedreceive() (without inserting message into a queue).
  12970. */
  12971. -static inline void pipelined_send(struct mqueue_inode_info *info,
  12972. +static inline void pipelined_send(struct wake_q_head *wake_q,
  12973. + struct mqueue_inode_info *info,
  12974. struct msg_msg *message,
  12975. struct ext_wait_queue *receiver)
  12976. {
  12977. receiver->msg = message;
  12978. list_del(&receiver->list);
  12979. - receiver->state = STATE_PENDING;
  12980. - wake_up_process(receiver->task);
  12981. - smp_wmb();
  12982. + wake_q_add(wake_q, receiver->task);
  12983. + /*
  12984. + * Rely on the implicit cmpxchg barrier from wake_q_add such
  12985. + * that we can ensure that updating receiver->state is the last
  12986. + * write operation: As once set, the receiver can continue,
  12987. + * and if we don't have the reference count from the wake_q,
  12988. + * yet, at that point we can later have a use-after-free
  12989. + * condition and bogus wakeup.
  12990. + */
  12991. receiver->state = STATE_READY;
  12992. }
  12993. /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
  12994. * gets its message and put to the queue (we have one free place for sure). */
  12995. -static inline void pipelined_receive(struct mqueue_inode_info *info)
  12996. +static inline void pipelined_receive(struct wake_q_head *wake_q,
  12997. + struct mqueue_inode_info *info)
  12998. {
  12999. struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
  13000. @@ -941,10 +949,9 @@
  13001. }
  13002. if (msg_insert(sender->msg, info))
  13003. return;
  13004. +
  13005. list_del(&sender->list);
  13006. - sender->state = STATE_PENDING;
  13007. - wake_up_process(sender->task);
  13008. - smp_wmb();
  13009. + wake_q_add(wake_q, sender->task);
  13010. sender->state = STATE_READY;
  13011. }
  13012. @@ -962,6 +969,7 @@
  13013. struct timespec ts;
  13014. struct posix_msg_tree_node *new_leaf = NULL;
  13015. int ret = 0;
  13016. + WAKE_Q(wake_q);
  13017. if (u_abs_timeout) {
  13018. int res = prepare_timeout(u_abs_timeout, &expires, &ts);
  13019. @@ -1045,7 +1053,7 @@
  13020. } else {
  13021. receiver = wq_get_first_waiter(info, RECV);
  13022. if (receiver) {
  13023. - pipelined_send(info, msg_ptr, receiver);
  13024. + pipelined_send(&wake_q, info, msg_ptr, receiver);
  13025. } else {
  13026. /* adds message to the queue */
  13027. ret = msg_insert(msg_ptr, info);
  13028. @@ -1058,6 +1066,7 @@
  13029. }
  13030. out_unlock:
  13031. spin_unlock(&info->lock);
  13032. + wake_up_q(&wake_q);
  13033. out_free:
  13034. if (ret)
  13035. free_msg(msg_ptr);
  13036. @@ -1144,14 +1153,17 @@
  13037. msg_ptr = wait.msg;
  13038. }
  13039. } else {
  13040. + WAKE_Q(wake_q);
  13041. +
  13042. msg_ptr = msg_get(info);
  13043. inode->i_atime = inode->i_mtime = inode->i_ctime =
  13044. CURRENT_TIME;
  13045. /* There is now free space in queue. */
  13046. - pipelined_receive(info);
  13047. + pipelined_receive(&wake_q, info);
  13048. spin_unlock(&info->lock);
  13049. + wake_up_q(&wake_q);
  13050. ret = 0;
  13051. }
  13052. if (ret == 0) {
  13053. diff -Nur linux-4.1.20.orig/ipc/msg.c linux-4.1.20/ipc/msg.c
  13054. --- linux-4.1.20.orig/ipc/msg.c 2016-03-17 19:11:03.000000000 +0100
  13055. +++ linux-4.1.20/ipc/msg.c 2016-03-21 20:18:33.000000000 +0100
  13056. @@ -188,6 +188,12 @@
  13057. struct msg_receiver *msr, *t;
  13058. list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
  13059. + /*
  13060. + * Make sure that the wakeup doesnt preempt
  13061. + * this CPU prematurely. (on PREEMPT_RT)
  13062. + */
  13063. + preempt_disable_rt();
  13064. +
  13065. msr->r_msg = NULL; /* initialize expunge ordering */
  13066. wake_up_process(msr->r_tsk);
  13067. /*
  13068. @@ -198,6 +204,8 @@
  13069. */
  13070. smp_mb();
  13071. msr->r_msg = ERR_PTR(res);
  13072. +
  13073. + preempt_enable_rt();
  13074. }
  13075. }
  13076. @@ -574,6 +582,11 @@
  13077. if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
  13078. !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
  13079. msr->r_msgtype, msr->r_mode)) {
  13080. + /*
  13081. + * Make sure that the wakeup doesnt preempt
  13082. + * this CPU prematurely. (on PREEMPT_RT)
  13083. + */
  13084. + preempt_disable_rt();
  13085. list_del(&msr->r_list);
  13086. if (msr->r_maxsize < msg->m_ts) {
  13087. @@ -595,12 +608,13 @@
  13088. */
  13089. smp_mb();
  13090. msr->r_msg = msg;
  13091. + preempt_enable_rt();
  13092. return 1;
  13093. }
  13094. + preempt_enable_rt();
  13095. }
  13096. }
  13097. -
  13098. return 0;
  13099. }
  13100. diff -Nur linux-4.1.20.orig/ipc/sem.c linux-4.1.20/ipc/sem.c
  13101. --- linux-4.1.20.orig/ipc/sem.c 2016-03-17 19:11:03.000000000 +0100
  13102. +++ linux-4.1.20/ipc/sem.c 2016-03-21 20:18:33.000000000 +0100
  13103. @@ -690,6 +690,13 @@
  13104. static void wake_up_sem_queue_prepare(struct list_head *pt,
  13105. struct sem_queue *q, int error)
  13106. {
  13107. +#ifdef CONFIG_PREEMPT_RT_BASE
  13108. + struct task_struct *p = q->sleeper;
  13109. + get_task_struct(p);
  13110. + q->status = error;
  13111. + wake_up_process(p);
  13112. + put_task_struct(p);
  13113. +#else
  13114. if (list_empty(pt)) {
  13115. /*
  13116. * Hold preempt off so that we don't get preempted and have the
  13117. @@ -701,6 +708,7 @@
  13118. q->pid = error;
  13119. list_add_tail(&q->list, pt);
  13120. +#endif
  13121. }
  13122. /**
  13123. @@ -714,6 +722,7 @@
  13124. */
  13125. static void wake_up_sem_queue_do(struct list_head *pt)
  13126. {
  13127. +#ifndef CONFIG_PREEMPT_RT_BASE
  13128. struct sem_queue *q, *t;
  13129. int did_something;
  13130. @@ -726,6 +735,7 @@
  13131. }
  13132. if (did_something)
  13133. preempt_enable();
  13134. +#endif
  13135. }
  13136. static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
  13137. diff -Nur linux-4.1.20.orig/kernel/Kconfig.locks linux-4.1.20/kernel/Kconfig.locks
  13138. --- linux-4.1.20.orig/kernel/Kconfig.locks 2016-03-17 19:11:03.000000000 +0100
  13139. +++ linux-4.1.20/kernel/Kconfig.locks 2016-03-21 20:18:33.000000000 +0100
  13140. @@ -225,11 +225,11 @@
  13141. config MUTEX_SPIN_ON_OWNER
  13142. def_bool y
  13143. - depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW
  13144. + depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  13145. config RWSEM_SPIN_ON_OWNER
  13146. def_bool y
  13147. - depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW
  13148. + depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  13149. config LOCK_SPIN_ON_OWNER
  13150. def_bool y
  13151. diff -Nur linux-4.1.20.orig/kernel/Kconfig.preempt linux-4.1.20/kernel/Kconfig.preempt
  13152. --- linux-4.1.20.orig/kernel/Kconfig.preempt 2016-03-17 19:11:03.000000000 +0100
  13153. +++ linux-4.1.20/kernel/Kconfig.preempt 2016-03-21 20:18:33.000000000 +0100
  13154. @@ -1,3 +1,16 @@
  13155. +config PREEMPT
  13156. + bool
  13157. + select PREEMPT_COUNT
  13158. +
  13159. +config PREEMPT_RT_BASE
  13160. + bool
  13161. + select PREEMPT
  13162. +
  13163. +config HAVE_PREEMPT_LAZY
  13164. + bool
  13165. +
  13166. +config PREEMPT_LAZY
  13167. + def_bool y if HAVE_PREEMPT_LAZY && PREEMPT_RT_FULL
  13168. choice
  13169. prompt "Preemption Model"
  13170. @@ -33,9 +46,9 @@
  13171. Select this if you are building a kernel for a desktop system.
  13172. -config PREEMPT
  13173. +config PREEMPT__LL
  13174. bool "Preemptible Kernel (Low-Latency Desktop)"
  13175. - select PREEMPT_COUNT
  13176. + select PREEMPT
  13177. select UNINLINE_SPIN_UNLOCK if !ARCH_INLINE_SPIN_UNLOCK
  13178. help
  13179. This option reduces the latency of the kernel by making
  13180. @@ -52,6 +65,22 @@
  13181. embedded system with latency requirements in the milliseconds
  13182. range.
  13183. +config PREEMPT_RTB
  13184. + bool "Preemptible Kernel (Basic RT)"
  13185. + select PREEMPT_RT_BASE
  13186. + help
  13187. + This option is basically the same as (Low-Latency Desktop) but
  13188. + enables changes which are preliminary for the full preemptible
  13189. + RT kernel.
  13190. +
  13191. +config PREEMPT_RT_FULL
  13192. + bool "Fully Preemptible Kernel (RT)"
  13193. + depends on IRQ_FORCED_THREADING
  13194. + select PREEMPT_RT_BASE
  13195. + select PREEMPT_RCU
  13196. + help
  13197. + All and everything
  13198. +
  13199. endchoice
  13200. config PREEMPT_COUNT
  13201. diff -Nur linux-4.1.20.orig/kernel/bpf/hashtab.c linux-4.1.20/kernel/bpf/hashtab.c
  13202. --- linux-4.1.20.orig/kernel/bpf/hashtab.c 2016-03-17 19:11:03.000000000 +0100
  13203. +++ linux-4.1.20/kernel/bpf/hashtab.c 2016-03-21 20:18:33.000000000 +0100
  13204. @@ -17,7 +17,7 @@
  13205. struct bpf_htab {
  13206. struct bpf_map map;
  13207. struct hlist_head *buckets;
  13208. - spinlock_t lock;
  13209. + raw_spinlock_t lock;
  13210. u32 count; /* number of elements in this hashtable */
  13211. u32 n_buckets; /* number of hash buckets */
  13212. u32 elem_size; /* size of each element in bytes */
  13213. @@ -82,7 +82,7 @@
  13214. for (i = 0; i < htab->n_buckets; i++)
  13215. INIT_HLIST_HEAD(&htab->buckets[i]);
  13216. - spin_lock_init(&htab->lock);
  13217. + raw_spin_lock_init(&htab->lock);
  13218. htab->count = 0;
  13219. htab->elem_size = sizeof(struct htab_elem) +
  13220. @@ -230,7 +230,7 @@
  13221. l_new->hash = htab_map_hash(l_new->key, key_size);
  13222. /* bpf_map_update_elem() can be called in_irq() */
  13223. - spin_lock_irqsave(&htab->lock, flags);
  13224. + raw_spin_lock_irqsave(&htab->lock, flags);
  13225. head = select_bucket(htab, l_new->hash);
  13226. @@ -266,11 +266,11 @@
  13227. } else {
  13228. htab->count++;
  13229. }
  13230. - spin_unlock_irqrestore(&htab->lock, flags);
  13231. + raw_spin_unlock_irqrestore(&htab->lock, flags);
  13232. return 0;
  13233. err:
  13234. - spin_unlock_irqrestore(&htab->lock, flags);
  13235. + raw_spin_unlock_irqrestore(&htab->lock, flags);
  13236. kfree(l_new);
  13237. return ret;
  13238. }
  13239. @@ -291,7 +291,7 @@
  13240. hash = htab_map_hash(key, key_size);
  13241. - spin_lock_irqsave(&htab->lock, flags);
  13242. + raw_spin_lock_irqsave(&htab->lock, flags);
  13243. head = select_bucket(htab, hash);
  13244. @@ -304,7 +304,7 @@
  13245. ret = 0;
  13246. }
  13247. - spin_unlock_irqrestore(&htab->lock, flags);
  13248. + raw_spin_unlock_irqrestore(&htab->lock, flags);
  13249. return ret;
  13250. }
  13251. diff -Nur linux-4.1.20.orig/kernel/cgroup.c linux-4.1.20/kernel/cgroup.c
  13252. --- linux-4.1.20.orig/kernel/cgroup.c 2016-03-17 19:11:03.000000000 +0100
  13253. +++ linux-4.1.20/kernel/cgroup.c 2016-03-21 20:18:33.000000000 +0100
  13254. @@ -4422,10 +4422,10 @@
  13255. queue_work(cgroup_destroy_wq, &css->destroy_work);
  13256. }
  13257. -static void css_release_work_fn(struct work_struct *work)
  13258. +static void css_release_work_fn(struct swork_event *sev)
  13259. {
  13260. struct cgroup_subsys_state *css =
  13261. - container_of(work, struct cgroup_subsys_state, destroy_work);
  13262. + container_of(sev, struct cgroup_subsys_state, destroy_swork);
  13263. struct cgroup_subsys *ss = css->ss;
  13264. struct cgroup *cgrp = css->cgroup;
  13265. @@ -4464,8 +4464,8 @@
  13266. struct cgroup_subsys_state *css =
  13267. container_of(ref, struct cgroup_subsys_state, refcnt);
  13268. - INIT_WORK(&css->destroy_work, css_release_work_fn);
  13269. - queue_work(cgroup_destroy_wq, &css->destroy_work);
  13270. + INIT_SWORK(&css->destroy_swork, css_release_work_fn);
  13271. + swork_queue(&css->destroy_swork);
  13272. }
  13273. static void init_and_link_css(struct cgroup_subsys_state *css,
  13274. @@ -5081,6 +5081,7 @@
  13275. */
  13276. cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
  13277. BUG_ON(!cgroup_destroy_wq);
  13278. + BUG_ON(swork_get());
  13279. /*
  13280. * Used to destroy pidlists and separate to serve as flush domain.
  13281. diff -Nur linux-4.1.20.orig/kernel/cpu.c linux-4.1.20/kernel/cpu.c
  13282. --- linux-4.1.20.orig/kernel/cpu.c 2016-03-17 19:11:03.000000000 +0100
  13283. +++ linux-4.1.20/kernel/cpu.c 2016-03-21 20:18:33.000000000 +0100
  13284. @@ -74,8 +74,8 @@
  13285. #endif
  13286. } cpu_hotplug = {
  13287. .active_writer = NULL,
  13288. - .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  13289. .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
  13290. + .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  13291. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  13292. .dep_map = {.name = "cpu_hotplug.lock" },
  13293. #endif
  13294. @@ -88,6 +88,289 @@
  13295. #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
  13296. #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
  13297. +/**
  13298. + * hotplug_pcp - per cpu hotplug descriptor
  13299. + * @unplug: set when pin_current_cpu() needs to sync tasks
  13300. + * @sync_tsk: the task that waits for tasks to finish pinned sections
  13301. + * @refcount: counter of tasks in pinned sections
  13302. + * @grab_lock: set when the tasks entering pinned sections should wait
  13303. + * @synced: notifier for @sync_tsk to tell cpu_down it's finished
  13304. + * @mutex: the mutex to make tasks wait (used when @grab_lock is true)
  13305. + * @mutex_init: zero if the mutex hasn't been initialized yet.
  13306. + *
  13307. + * Although @unplug and @sync_tsk may point to the same task, the @unplug
  13308. + * is used as a flag and still exists after @sync_tsk has exited and
  13309. + * @sync_tsk set to NULL.
  13310. + */
  13311. +struct hotplug_pcp {
  13312. + struct task_struct *unplug;
  13313. + struct task_struct *sync_tsk;
  13314. + int refcount;
  13315. + int grab_lock;
  13316. + struct completion synced;
  13317. + struct completion unplug_wait;
  13318. +#ifdef CONFIG_PREEMPT_RT_FULL
  13319. + /*
  13320. + * Note, on PREEMPT_RT, the hotplug lock must save the state of
  13321. + * the task, otherwise the mutex will cause the task to fail
  13322. + * to sleep when required. (Because it's called from migrate_disable())
  13323. + *
  13324. + * The spinlock_t on PREEMPT_RT is a mutex that saves the task's
  13325. + * state.
  13326. + */
  13327. + spinlock_t lock;
  13328. +#else
  13329. + struct mutex mutex;
  13330. +#endif
  13331. + int mutex_init;
  13332. +};
  13333. +
  13334. +#ifdef CONFIG_PREEMPT_RT_FULL
  13335. +# define hotplug_lock(hp) rt_spin_lock(&(hp)->lock)
  13336. +# define hotplug_unlock(hp) rt_spin_unlock(&(hp)->lock)
  13337. +#else
  13338. +# define hotplug_lock(hp) mutex_lock(&(hp)->mutex)
  13339. +# define hotplug_unlock(hp) mutex_unlock(&(hp)->mutex)
  13340. +#endif
  13341. +
  13342. +static DEFINE_PER_CPU(struct hotplug_pcp, hotplug_pcp);
  13343. +
  13344. +/**
  13345. + * pin_current_cpu - Prevent the current cpu from being unplugged
  13346. + *
  13347. + * Lightweight version of get_online_cpus() to prevent cpu from being
  13348. + * unplugged when code runs in a migration disabled region.
  13349. + *
  13350. + * Must be called with preemption disabled (preempt_count = 1)!
  13351. + */
  13352. +void pin_current_cpu(void)
  13353. +{
  13354. + struct hotplug_pcp *hp;
  13355. + int force = 0;
  13356. +
  13357. +retry:
  13358. + hp = this_cpu_ptr(&hotplug_pcp);
  13359. +
  13360. + if (!hp->unplug || hp->refcount || force || preempt_count() > 1 ||
  13361. + hp->unplug == current) {
  13362. + hp->refcount++;
  13363. + return;
  13364. + }
  13365. + if (hp->grab_lock) {
  13366. + preempt_enable();
  13367. + hotplug_lock(hp);
  13368. + hotplug_unlock(hp);
  13369. + } else {
  13370. + preempt_enable();
  13371. + /*
  13372. + * Try to push this task off of this CPU.
  13373. + */
  13374. + if (!migrate_me()) {
  13375. + preempt_disable();
  13376. + hp = this_cpu_ptr(&hotplug_pcp);
  13377. + if (!hp->grab_lock) {
  13378. + /*
  13379. + * Just let it continue it's already pinned
  13380. + * or about to sleep.
  13381. + */
  13382. + force = 1;
  13383. + goto retry;
  13384. + }
  13385. + preempt_enable();
  13386. + }
  13387. + }
  13388. + preempt_disable();
  13389. + goto retry;
  13390. +}
  13391. +
  13392. +/**
  13393. + * unpin_current_cpu - Allow unplug of current cpu
  13394. + *
  13395. + * Must be called with preemption or interrupts disabled!
  13396. + */
  13397. +void unpin_current_cpu(void)
  13398. +{
  13399. + struct hotplug_pcp *hp = this_cpu_ptr(&hotplug_pcp);
  13400. +
  13401. + WARN_ON(hp->refcount <= 0);
  13402. +
  13403. + /* This is safe. sync_unplug_thread is pinned to this cpu */
  13404. + if (!--hp->refcount && hp->unplug && hp->unplug != current)
  13405. + wake_up_process(hp->unplug);
  13406. +}
  13407. +
  13408. +static void wait_for_pinned_cpus(struct hotplug_pcp *hp)
  13409. +{
  13410. + set_current_state(TASK_UNINTERRUPTIBLE);
  13411. + while (hp->refcount) {
  13412. + schedule_preempt_disabled();
  13413. + set_current_state(TASK_UNINTERRUPTIBLE);
  13414. + }
  13415. +}
  13416. +
  13417. +static int sync_unplug_thread(void *data)
  13418. +{
  13419. + struct hotplug_pcp *hp = data;
  13420. +
  13421. + wait_for_completion(&hp->unplug_wait);
  13422. + preempt_disable();
  13423. + hp->unplug = current;
  13424. + wait_for_pinned_cpus(hp);
  13425. +
  13426. + /*
  13427. + * This thread will synchronize the cpu_down() with threads
  13428. + * that have pinned the CPU. When the pinned CPU count reaches
  13429. + * zero, we inform the cpu_down code to continue to the next step.
  13430. + */
  13431. + set_current_state(TASK_UNINTERRUPTIBLE);
  13432. + preempt_enable();
  13433. + complete(&hp->synced);
  13434. +
  13435. + /*
  13436. + * If all succeeds, the next step will need tasks to wait till
  13437. + * the CPU is offline before continuing. To do this, the grab_lock
  13438. + * is set and tasks going into pin_current_cpu() will block on the
  13439. + * mutex. But we still need to wait for those that are already in
  13440. + * pinned CPU sections. If the cpu_down() failed, the kthread_should_stop()
  13441. + * will kick this thread out.
  13442. + */
  13443. + while (!hp->grab_lock && !kthread_should_stop()) {
  13444. + schedule();
  13445. + set_current_state(TASK_UNINTERRUPTIBLE);
  13446. + }
  13447. +
  13448. + /* Make sure grab_lock is seen before we see a stale completion */
  13449. + smp_mb();
  13450. +
  13451. + /*
  13452. + * Now just before cpu_down() enters stop machine, we need to make
  13453. + * sure all tasks that are in pinned CPU sections are out, and new
  13454. + * tasks will now grab the lock, keeping them from entering pinned
  13455. + * CPU sections.
  13456. + */
  13457. + if (!kthread_should_stop()) {
  13458. + preempt_disable();
  13459. + wait_for_pinned_cpus(hp);
  13460. + preempt_enable();
  13461. + complete(&hp->synced);
  13462. + }
  13463. +
  13464. + set_current_state(TASK_UNINTERRUPTIBLE);
  13465. + while (!kthread_should_stop()) {
  13466. + schedule();
  13467. + set_current_state(TASK_UNINTERRUPTIBLE);
  13468. + }
  13469. + set_current_state(TASK_RUNNING);
  13470. +
  13471. + /*
  13472. + * Force this thread off this CPU as it's going down and
  13473. + * we don't want any more work on this CPU.
  13474. + */
  13475. + current->flags &= ~PF_NO_SETAFFINITY;
  13476. + set_cpus_allowed_ptr(current, cpu_present_mask);
  13477. + migrate_me();
  13478. + return 0;
  13479. +}
  13480. +
  13481. +static void __cpu_unplug_sync(struct hotplug_pcp *hp)
  13482. +{
  13483. + wake_up_process(hp->sync_tsk);
  13484. + wait_for_completion(&hp->synced);
  13485. +}
  13486. +
  13487. +static void __cpu_unplug_wait(unsigned int cpu)
  13488. +{
  13489. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  13490. +
  13491. + complete(&hp->unplug_wait);
  13492. + wait_for_completion(&hp->synced);
  13493. +}
  13494. +
  13495. +/*
  13496. + * Start the sync_unplug_thread on the target cpu and wait for it to
  13497. + * complete.
  13498. + */
  13499. +static int cpu_unplug_begin(unsigned int cpu)
  13500. +{
  13501. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  13502. + int err;
  13503. +
  13504. + /* Protected by cpu_hotplug.lock */
  13505. + if (!hp->mutex_init) {
  13506. +#ifdef CONFIG_PREEMPT_RT_FULL
  13507. + spin_lock_init(&hp->lock);
  13508. +#else
  13509. + mutex_init(&hp->mutex);
  13510. +#endif
  13511. + hp->mutex_init = 1;
  13512. + }
  13513. +
  13514. + /* Inform the scheduler to migrate tasks off this CPU */
  13515. + tell_sched_cpu_down_begin(cpu);
  13516. +
  13517. + init_completion(&hp->synced);
  13518. + init_completion(&hp->unplug_wait);
  13519. +
  13520. + hp->sync_tsk = kthread_create(sync_unplug_thread, hp, "sync_unplug/%d", cpu);
  13521. + if (IS_ERR(hp->sync_tsk)) {
  13522. + err = PTR_ERR(hp->sync_tsk);
  13523. + hp->sync_tsk = NULL;
  13524. + return err;
  13525. + }
  13526. + kthread_bind(hp->sync_tsk, cpu);
  13527. +
  13528. + /*
  13529. + * Wait for tasks to get out of the pinned sections,
  13530. + * it's still OK if new tasks enter. Some CPU notifiers will
  13531. + * wait for tasks that are going to enter these sections and
  13532. + * we must not have them block.
  13533. + */
  13534. + wake_up_process(hp->sync_tsk);
  13535. + return 0;
  13536. +}
  13537. +
  13538. +static void cpu_unplug_sync(unsigned int cpu)
  13539. +{
  13540. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  13541. +
  13542. + init_completion(&hp->synced);
  13543. + /* The completion needs to be initialzied before setting grab_lock */
  13544. + smp_wmb();
  13545. +
  13546. + /* Grab the mutex before setting grab_lock */
  13547. + hotplug_lock(hp);
  13548. + hp->grab_lock = 1;
  13549. +
  13550. + /*
  13551. + * The CPU notifiers have been completed.
  13552. + * Wait for tasks to get out of pinned CPU sections and have new
  13553. + * tasks block until the CPU is completely down.
  13554. + */
  13555. + __cpu_unplug_sync(hp);
  13556. +
  13557. + /* All done with the sync thread */
  13558. + kthread_stop(hp->sync_tsk);
  13559. + hp->sync_tsk = NULL;
  13560. +}
  13561. +
  13562. +static void cpu_unplug_done(unsigned int cpu)
  13563. +{
  13564. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  13565. +
  13566. + hp->unplug = NULL;
  13567. + /* Let all tasks know cpu unplug is finished before cleaning up */
  13568. + smp_wmb();
  13569. +
  13570. + if (hp->sync_tsk)
  13571. + kthread_stop(hp->sync_tsk);
  13572. +
  13573. + if (hp->grab_lock) {
  13574. + hotplug_unlock(hp);
  13575. + /* protected by cpu_hotplug.lock */
  13576. + hp->grab_lock = 0;
  13577. + }
  13578. + tell_sched_cpu_down_done(cpu);
  13579. +}
  13580. void get_online_cpus(void)
  13581. {
  13582. @@ -349,13 +632,15 @@
  13583. /* Requires cpu_add_remove_lock to be held */
  13584. static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
  13585. {
  13586. - int err, nr_calls = 0;
  13587. + int mycpu, err, nr_calls = 0;
  13588. void *hcpu = (void *)(long)cpu;
  13589. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  13590. struct take_cpu_down_param tcd_param = {
  13591. .mod = mod,
  13592. .hcpu = hcpu,
  13593. };
  13594. + cpumask_var_t cpumask;
  13595. + cpumask_var_t cpumask_org;
  13596. if (num_online_cpus() == 1)
  13597. return -EBUSY;
  13598. @@ -363,7 +648,34 @@
  13599. if (!cpu_online(cpu))
  13600. return -EINVAL;
  13601. + /* Move the downtaker off the unplug cpu */
  13602. + if (!alloc_cpumask_var(&cpumask, GFP_KERNEL))
  13603. + return -ENOMEM;
  13604. + if (!alloc_cpumask_var(&cpumask_org, GFP_KERNEL)) {
  13605. + free_cpumask_var(cpumask);
  13606. + return -ENOMEM;
  13607. + }
  13608. +
  13609. + cpumask_copy(cpumask_org, tsk_cpus_allowed(current));
  13610. + cpumask_andnot(cpumask, cpu_online_mask, cpumask_of(cpu));
  13611. + set_cpus_allowed_ptr(current, cpumask);
  13612. + free_cpumask_var(cpumask);
  13613. + migrate_disable();
  13614. + mycpu = smp_processor_id();
  13615. + if (mycpu == cpu) {
  13616. + printk(KERN_ERR "Yuck! Still on unplug CPU\n!");
  13617. + migrate_enable();
  13618. + err = -EBUSY;
  13619. + goto restore_cpus;
  13620. + }
  13621. + migrate_enable();
  13622. +
  13623. cpu_hotplug_begin();
  13624. + err = cpu_unplug_begin(cpu);
  13625. + if (err) {
  13626. + printk("cpu_unplug_begin(%d) failed\n", cpu);
  13627. + goto out_cancel;
  13628. + }
  13629. err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
  13630. if (err) {
  13631. @@ -389,8 +701,12 @@
  13632. #endif
  13633. synchronize_rcu();
  13634. + __cpu_unplug_wait(cpu);
  13635. smpboot_park_threads(cpu);
  13636. + /* Notifiers are done. Don't let any more tasks pin this CPU. */
  13637. + cpu_unplug_sync(cpu);
  13638. +
  13639. /*
  13640. * So now all preempt/rcu users must observe !cpu_active().
  13641. */
  13642. @@ -427,9 +743,14 @@
  13643. check_for_tasks(cpu);
  13644. out_release:
  13645. + cpu_unplug_done(cpu);
  13646. +out_cancel:
  13647. cpu_hotplug_done();
  13648. if (!err)
  13649. cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
  13650. +restore_cpus:
  13651. + set_cpus_allowed_ptr(current, cpumask_org);
  13652. + free_cpumask_var(cpumask_org);
  13653. return err;
  13654. }
  13655. diff -Nur linux-4.1.20.orig/kernel/debug/kdb/kdb_io.c linux-4.1.20/kernel/debug/kdb/kdb_io.c
  13656. --- linux-4.1.20.orig/kernel/debug/kdb/kdb_io.c 2016-03-17 19:11:03.000000000 +0100
  13657. +++ linux-4.1.20/kernel/debug/kdb/kdb_io.c 2016-03-21 20:18:33.000000000 +0100
  13658. @@ -554,7 +554,6 @@
  13659. int linecount;
  13660. int colcount;
  13661. int logging, saved_loglevel = 0;
  13662. - int saved_trap_printk;
  13663. int got_printf_lock = 0;
  13664. int retlen = 0;
  13665. int fnd, len;
  13666. @@ -565,8 +564,6 @@
  13667. unsigned long uninitialized_var(flags);
  13668. preempt_disable();
  13669. - saved_trap_printk = kdb_trap_printk;
  13670. - kdb_trap_printk = 0;
  13671. /* Serialize kdb_printf if multiple cpus try to write at once.
  13672. * But if any cpu goes recursive in kdb, just print the output,
  13673. @@ -855,7 +852,6 @@
  13674. } else {
  13675. __release(kdb_printf_lock);
  13676. }
  13677. - kdb_trap_printk = saved_trap_printk;
  13678. preempt_enable();
  13679. return retlen;
  13680. }
  13681. @@ -865,9 +861,11 @@
  13682. va_list ap;
  13683. int r;
  13684. + kdb_trap_printk++;
  13685. va_start(ap, fmt);
  13686. r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
  13687. va_end(ap);
  13688. + kdb_trap_printk--;
  13689. return r;
  13690. }
  13691. diff -Nur linux-4.1.20.orig/kernel/events/core.c linux-4.1.20/kernel/events/core.c
  13692. --- linux-4.1.20.orig/kernel/events/core.c 2016-03-17 19:11:03.000000000 +0100
  13693. +++ linux-4.1.20/kernel/events/core.c 2016-03-21 20:18:33.000000000 +0100
  13694. @@ -6925,6 +6925,7 @@
  13695. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  13696. hwc->hrtimer.function = perf_swevent_hrtimer;
  13697. + hwc->hrtimer.irqsafe = 1;
  13698. /*
  13699. * Since hrtimers have a fixed rate, we can do a static freq->period
  13700. diff -Nur linux-4.1.20.orig/kernel/exit.c linux-4.1.20/kernel/exit.c
  13701. --- linux-4.1.20.orig/kernel/exit.c 2016-03-17 19:11:03.000000000 +0100
  13702. +++ linux-4.1.20/kernel/exit.c 2016-03-21 20:18:33.000000000 +0100
  13703. @@ -144,7 +144,7 @@
  13704. * Do this under ->siglock, we can race with another thread
  13705. * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
  13706. */
  13707. - flush_sigqueue(&tsk->pending);
  13708. + flush_task_sigqueue(tsk);
  13709. tsk->sighand = NULL;
  13710. spin_unlock(&sighand->siglock);
  13711. diff -Nur linux-4.1.20.orig/kernel/fork.c linux-4.1.20/kernel/fork.c
  13712. --- linux-4.1.20.orig/kernel/fork.c 2016-03-17 19:11:03.000000000 +0100
  13713. +++ linux-4.1.20/kernel/fork.c 2016-03-21 20:18:33.000000000 +0100
  13714. @@ -108,7 +108,7 @@
  13715. DEFINE_PER_CPU(unsigned long, process_counts) = 0;
  13716. -__cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
  13717. +DEFINE_RWLOCK(tasklist_lock); /* outer */
  13718. #ifdef CONFIG_PROVE_RCU
  13719. int lockdep_tasklist_lock_is_held(void)
  13720. @@ -244,7 +244,9 @@
  13721. if (atomic_dec_and_test(&sig->sigcnt))
  13722. free_signal_struct(sig);
  13723. }
  13724. -
  13725. +#ifdef CONFIG_PREEMPT_RT_BASE
  13726. +static
  13727. +#endif
  13728. void __put_task_struct(struct task_struct *tsk)
  13729. {
  13730. WARN_ON(!tsk->exit_state);
  13731. @@ -260,7 +262,18 @@
  13732. if (!profile_handoff_task(tsk))
  13733. free_task(tsk);
  13734. }
  13735. +#ifndef CONFIG_PREEMPT_RT_BASE
  13736. EXPORT_SYMBOL_GPL(__put_task_struct);
  13737. +#else
  13738. +void __put_task_struct_cb(struct rcu_head *rhp)
  13739. +{
  13740. + struct task_struct *tsk = container_of(rhp, struct task_struct, put_rcu);
  13741. +
  13742. + __put_task_struct(tsk);
  13743. +
  13744. +}
  13745. +EXPORT_SYMBOL_GPL(__put_task_struct_cb);
  13746. +#endif
  13747. void __init __weak arch_task_cache_init(void) { }
  13748. @@ -374,6 +387,7 @@
  13749. #endif
  13750. tsk->splice_pipe = NULL;
  13751. tsk->task_frag.page = NULL;
  13752. + tsk->wake_q.next = NULL;
  13753. account_kernel_stack(ti, 1);
  13754. @@ -680,6 +694,19 @@
  13755. }
  13756. EXPORT_SYMBOL_GPL(__mmdrop);
  13757. +#ifdef CONFIG_PREEMPT_RT_BASE
  13758. +/*
  13759. + * RCU callback for delayed mm drop. Not strictly rcu, but we don't
  13760. + * want another facility to make this work.
  13761. + */
  13762. +void __mmdrop_delayed(struct rcu_head *rhp)
  13763. +{
  13764. + struct mm_struct *mm = container_of(rhp, struct mm_struct, delayed_drop);
  13765. +
  13766. + __mmdrop(mm);
  13767. +}
  13768. +#endif
  13769. +
  13770. /*
  13771. * Decrement the use count and release all resources for an mm.
  13772. */
  13773. @@ -1214,6 +1241,9 @@
  13774. */
  13775. static void posix_cpu_timers_init(struct task_struct *tsk)
  13776. {
  13777. +#ifdef CONFIG_PREEMPT_RT_BASE
  13778. + tsk->posix_timer_list = NULL;
  13779. +#endif
  13780. tsk->cputime_expires.prof_exp = 0;
  13781. tsk->cputime_expires.virt_exp = 0;
  13782. tsk->cputime_expires.sched_exp = 0;
  13783. @@ -1338,6 +1368,7 @@
  13784. spin_lock_init(&p->alloc_lock);
  13785. init_sigpending(&p->pending);
  13786. + p->sigqueue_cache = NULL;
  13787. p->utime = p->stime = p->gtime = 0;
  13788. p->utimescaled = p->stimescaled = 0;
  13789. @@ -1345,7 +1376,8 @@
  13790. p->prev_cputime.utime = p->prev_cputime.stime = 0;
  13791. #endif
  13792. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  13793. - seqlock_init(&p->vtime_seqlock);
  13794. + raw_spin_lock_init(&p->vtime_lock);
  13795. + seqcount_init(&p->vtime_seq);
  13796. p->vtime_snap = 0;
  13797. p->vtime_snap_whence = VTIME_SLEEPING;
  13798. #endif
  13799. @@ -1396,6 +1428,9 @@
  13800. p->hardirq_context = 0;
  13801. p->softirq_context = 0;
  13802. #endif
  13803. +
  13804. + p->pagefault_disabled = 0;
  13805. +
  13806. #ifdef CONFIG_LOCKDEP
  13807. p->lockdep_depth = 0; /* no locks held yet */
  13808. p->curr_chain_key = 0;
  13809. diff -Nur linux-4.1.20.orig/kernel/futex.c linux-4.1.20/kernel/futex.c
  13810. --- linux-4.1.20.orig/kernel/futex.c 2016-03-17 19:11:03.000000000 +0100
  13811. +++ linux-4.1.20/kernel/futex.c 2016-03-21 20:18:33.000000000 +0100
  13812. @@ -738,7 +738,9 @@
  13813. * task still owns the PI-state:
  13814. */
  13815. if (head->next != next) {
  13816. + raw_spin_unlock_irq(&curr->pi_lock);
  13817. spin_unlock(&hb->lock);
  13818. + raw_spin_lock_irq(&curr->pi_lock);
  13819. continue;
  13820. }
  13821. @@ -1090,9 +1092,11 @@
  13822. /*
  13823. * The hash bucket lock must be held when this is called.
  13824. - * Afterwards, the futex_q must not be accessed.
  13825. + * Afterwards, the futex_q must not be accessed. Callers
  13826. + * must ensure to later call wake_up_q() for the actual
  13827. + * wakeups to occur.
  13828. */
  13829. -static void wake_futex(struct futex_q *q)
  13830. +static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
  13831. {
  13832. struct task_struct *p = q->task;
  13833. @@ -1100,14 +1104,10 @@
  13834. return;
  13835. /*
  13836. - * We set q->lock_ptr = NULL _before_ we wake up the task. If
  13837. - * a non-futex wake up happens on another CPU then the task
  13838. - * might exit and p would dereference a non-existing task
  13839. - * struct. Prevent this by holding a reference on p across the
  13840. - * wake up.
  13841. + * Queue the task for later wakeup for after we've released
  13842. + * the hb->lock. wake_q_add() grabs reference to p.
  13843. */
  13844. - get_task_struct(p);
  13845. -
  13846. + wake_q_add(wake_q, p);
  13847. __unqueue_futex(q);
  13848. /*
  13849. * The waiting task can free the futex_q as soon as
  13850. @@ -1117,16 +1117,15 @@
  13851. */
  13852. smp_wmb();
  13853. q->lock_ptr = NULL;
  13854. -
  13855. - wake_up_state(p, TASK_NORMAL);
  13856. - put_task_struct(p);
  13857. }
  13858. -static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
  13859. +static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  13860. + struct futex_hash_bucket *hb)
  13861. {
  13862. struct task_struct *new_owner;
  13863. struct futex_pi_state *pi_state = this->pi_state;
  13864. u32 uninitialized_var(curval), newval;
  13865. + bool deboost;
  13866. int ret = 0;
  13867. if (!pi_state)
  13868. @@ -1178,7 +1177,17 @@
  13869. raw_spin_unlock_irq(&new_owner->pi_lock);
  13870. raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  13871. - rt_mutex_unlock(&pi_state->pi_mutex);
  13872. +
  13873. + deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex);
  13874. +
  13875. + /*
  13876. + * We deboost after dropping hb->lock. That prevents a double
  13877. + * wakeup on RT.
  13878. + */
  13879. + spin_unlock(&hb->lock);
  13880. +
  13881. + if (deboost)
  13882. + rt_mutex_adjust_prio(current);
  13883. return 0;
  13884. }
  13885. @@ -1217,6 +1226,7 @@
  13886. struct futex_q *this, *next;
  13887. union futex_key key = FUTEX_KEY_INIT;
  13888. int ret;
  13889. + WAKE_Q(wake_q);
  13890. if (!bitset)
  13891. return -EINVAL;
  13892. @@ -1244,13 +1254,14 @@
  13893. if (!(this->bitset & bitset))
  13894. continue;
  13895. - wake_futex(this);
  13896. + mark_wake_futex(&wake_q, this);
  13897. if (++ret >= nr_wake)
  13898. break;
  13899. }
  13900. }
  13901. spin_unlock(&hb->lock);
  13902. + wake_up_q(&wake_q);
  13903. out_put_key:
  13904. put_futex_key(&key);
  13905. out:
  13906. @@ -1269,6 +1280,7 @@
  13907. struct futex_hash_bucket *hb1, *hb2;
  13908. struct futex_q *this, *next;
  13909. int ret, op_ret;
  13910. + WAKE_Q(wake_q);
  13911. retry:
  13912. ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
  13913. @@ -1320,7 +1332,7 @@
  13914. ret = -EINVAL;
  13915. goto out_unlock;
  13916. }
  13917. - wake_futex(this);
  13918. + mark_wake_futex(&wake_q, this);
  13919. if (++ret >= nr_wake)
  13920. break;
  13921. }
  13922. @@ -1334,7 +1346,7 @@
  13923. ret = -EINVAL;
  13924. goto out_unlock;
  13925. }
  13926. - wake_futex(this);
  13927. + mark_wake_futex(&wake_q, this);
  13928. if (++op_ret >= nr_wake2)
  13929. break;
  13930. }
  13931. @@ -1344,6 +1356,7 @@
  13932. out_unlock:
  13933. double_unlock_hb(hb1, hb2);
  13934. + wake_up_q(&wake_q);
  13935. out_put_keys:
  13936. put_futex_key(&key2);
  13937. out_put_key1:
  13938. @@ -1503,6 +1516,7 @@
  13939. struct futex_pi_state *pi_state = NULL;
  13940. struct futex_hash_bucket *hb1, *hb2;
  13941. struct futex_q *this, *next;
  13942. + WAKE_Q(wake_q);
  13943. if (requeue_pi) {
  13944. /*
  13945. @@ -1679,7 +1693,7 @@
  13946. * woken by futex_unlock_pi().
  13947. */
  13948. if (++task_count <= nr_wake && !requeue_pi) {
  13949. - wake_futex(this);
  13950. + mark_wake_futex(&wake_q, this);
  13951. continue;
  13952. }
  13953. @@ -1705,6 +1719,16 @@
  13954. requeue_pi_wake_futex(this, &key2, hb2);
  13955. drop_count++;
  13956. continue;
  13957. + } else if (ret == -EAGAIN) {
  13958. + /*
  13959. + * Waiter was woken by timeout or
  13960. + * signal and has set pi_blocked_on to
  13961. + * PI_WAKEUP_INPROGRESS before we
  13962. + * tried to enqueue it on the rtmutex.
  13963. + */
  13964. + this->pi_state = NULL;
  13965. + free_pi_state(pi_state);
  13966. + continue;
  13967. } else if (ret) {
  13968. /* -EDEADLK */
  13969. this->pi_state = NULL;
  13970. @@ -1719,6 +1743,7 @@
  13971. out_unlock:
  13972. free_pi_state(pi_state);
  13973. double_unlock_hb(hb1, hb2);
  13974. + wake_up_q(&wake_q);
  13975. hb_waiters_dec(hb2);
  13976. /*
  13977. @@ -2412,13 +2437,26 @@
  13978. */
  13979. match = futex_top_waiter(hb, &key);
  13980. if (match) {
  13981. - ret = wake_futex_pi(uaddr, uval, match);
  13982. + ret = wake_futex_pi(uaddr, uval, match, hb);
  13983. +
  13984. + /*
  13985. + * In case of success wake_futex_pi dropped the hash
  13986. + * bucket lock.
  13987. + */
  13988. + if (!ret)
  13989. + goto out_putkey;
  13990. +
  13991. /*
  13992. * The atomic access to the futex value generated a
  13993. * pagefault, so retry the user-access and the wakeup:
  13994. */
  13995. if (ret == -EFAULT)
  13996. goto pi_faulted;
  13997. +
  13998. + /*
  13999. + * wake_futex_pi has detected invalid state. Tell user
  14000. + * space.
  14001. + */
  14002. goto out_unlock;
  14003. }
  14004. @@ -2439,6 +2477,7 @@
  14005. out_unlock:
  14006. spin_unlock(&hb->lock);
  14007. +out_putkey:
  14008. put_futex_key(&key);
  14009. return ret;
  14010. @@ -2549,7 +2588,7 @@
  14011. struct hrtimer_sleeper timeout, *to = NULL;
  14012. struct rt_mutex_waiter rt_waiter;
  14013. struct rt_mutex *pi_mutex = NULL;
  14014. - struct futex_hash_bucket *hb;
  14015. + struct futex_hash_bucket *hb, *hb2;
  14016. union futex_key key2 = FUTEX_KEY_INIT;
  14017. struct futex_q q = futex_q_init;
  14018. int res, ret;
  14019. @@ -2574,10 +2613,7 @@
  14020. * The waiter is allocated on our stack, manipulated by the requeue
  14021. * code while we sleep on uaddr.
  14022. */
  14023. - debug_rt_mutex_init_waiter(&rt_waiter);
  14024. - RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
  14025. - RB_CLEAR_NODE(&rt_waiter.tree_entry);
  14026. - rt_waiter.task = NULL;
  14027. + rt_mutex_init_waiter(&rt_waiter, false);
  14028. ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
  14029. if (unlikely(ret != 0))
  14030. @@ -2608,20 +2644,55 @@
  14031. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  14032. futex_wait_queue_me(hb, &q, to);
  14033. - spin_lock(&hb->lock);
  14034. - ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  14035. - spin_unlock(&hb->lock);
  14036. - if (ret)
  14037. - goto out_put_keys;
  14038. + /*
  14039. + * On RT we must avoid races with requeue and trying to block
  14040. + * on two mutexes (hb->lock and uaddr2's rtmutex) by
  14041. + * serializing access to pi_blocked_on with pi_lock.
  14042. + */
  14043. + raw_spin_lock_irq(&current->pi_lock);
  14044. + if (current->pi_blocked_on) {
  14045. + /*
  14046. + * We have been requeued or are in the process of
  14047. + * being requeued.
  14048. + */
  14049. + raw_spin_unlock_irq(&current->pi_lock);
  14050. + } else {
  14051. + /*
  14052. + * Setting pi_blocked_on to PI_WAKEUP_INPROGRESS
  14053. + * prevents a concurrent requeue from moving us to the
  14054. + * uaddr2 rtmutex. After that we can safely acquire
  14055. + * (and possibly block on) hb->lock.
  14056. + */
  14057. + current->pi_blocked_on = PI_WAKEUP_INPROGRESS;
  14058. + raw_spin_unlock_irq(&current->pi_lock);
  14059. +
  14060. + spin_lock(&hb->lock);
  14061. +
  14062. + /*
  14063. + * Clean up pi_blocked_on. We might leak it otherwise
  14064. + * when we succeeded with the hb->lock in the fast
  14065. + * path.
  14066. + */
  14067. + raw_spin_lock_irq(&current->pi_lock);
  14068. + current->pi_blocked_on = NULL;
  14069. + raw_spin_unlock_irq(&current->pi_lock);
  14070. +
  14071. + ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  14072. + spin_unlock(&hb->lock);
  14073. + if (ret)
  14074. + goto out_put_keys;
  14075. + }
  14076. /*
  14077. - * In order for us to be here, we know our q.key == key2, and since
  14078. - * we took the hb->lock above, we also know that futex_requeue() has
  14079. - * completed and we no longer have to concern ourselves with a wakeup
  14080. - * race with the atomic proxy lock acquisition by the requeue code. The
  14081. - * futex_requeue dropped our key1 reference and incremented our key2
  14082. - * reference count.
  14083. + * In order to be here, we have either been requeued, are in
  14084. + * the process of being requeued, or requeue successfully
  14085. + * acquired uaddr2 on our behalf. If pi_blocked_on was
  14086. + * non-null above, we may be racing with a requeue. Do not
  14087. + * rely on q->lock_ptr to be hb2->lock until after blocking on
  14088. + * hb->lock or hb2->lock. The futex_requeue dropped our key1
  14089. + * reference and incremented our key2 reference count.
  14090. */
  14091. + hb2 = hash_futex(&key2);
  14092. /* Check if the requeue code acquired the second futex for us. */
  14093. if (!q.rt_waiter) {
  14094. @@ -2630,14 +2701,15 @@
  14095. * did a lock-steal - fix up the PI-state in that case.
  14096. */
  14097. if (q.pi_state && (q.pi_state->owner != current)) {
  14098. - spin_lock(q.lock_ptr);
  14099. + spin_lock(&hb2->lock);
  14100. + BUG_ON(&hb2->lock != q.lock_ptr);
  14101. ret = fixup_pi_state_owner(uaddr2, &q, current);
  14102. /*
  14103. * Drop the reference to the pi state which
  14104. * the requeue_pi() code acquired for us.
  14105. */
  14106. free_pi_state(q.pi_state);
  14107. - spin_unlock(q.lock_ptr);
  14108. + spin_unlock(&hb2->lock);
  14109. }
  14110. } else {
  14111. /*
  14112. @@ -2650,7 +2722,8 @@
  14113. ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter);
  14114. debug_rt_mutex_free_waiter(&rt_waiter);
  14115. - spin_lock(q.lock_ptr);
  14116. + spin_lock(&hb2->lock);
  14117. + BUG_ON(&hb2->lock != q.lock_ptr);
  14118. /*
  14119. * Fixup the pi_state owner and possibly acquire the lock if we
  14120. * haven't already.
  14121. diff -Nur linux-4.1.20.orig/kernel/irq/handle.c linux-4.1.20/kernel/irq/handle.c
  14122. --- linux-4.1.20.orig/kernel/irq/handle.c 2016-03-17 19:11:03.000000000 +0100
  14123. +++ linux-4.1.20/kernel/irq/handle.c 2016-03-21 20:18:33.000000000 +0100
  14124. @@ -133,6 +133,8 @@
  14125. irqreturn_t
  14126. handle_irq_event_percpu(struct irq_desc *desc, struct irqaction *action)
  14127. {
  14128. + struct pt_regs *regs = get_irq_regs();
  14129. + u64 ip = regs ? instruction_pointer(regs) : 0;
  14130. irqreturn_t retval = IRQ_NONE;
  14131. unsigned int flags = 0, irq = desc->irq_data.irq;
  14132. @@ -173,7 +175,11 @@
  14133. action = action->next;
  14134. } while (action);
  14135. - add_interrupt_randomness(irq, flags);
  14136. +#ifndef CONFIG_PREEMPT_RT_FULL
  14137. + add_interrupt_randomness(irq, flags, ip);
  14138. +#else
  14139. + desc->random_ip = ip;
  14140. +#endif
  14141. if (!noirqdebug)
  14142. note_interrupt(irq, desc, retval);
  14143. diff -Nur linux-4.1.20.orig/kernel/irq/manage.c linux-4.1.20/kernel/irq/manage.c
  14144. --- linux-4.1.20.orig/kernel/irq/manage.c 2016-03-17 19:11:03.000000000 +0100
  14145. +++ linux-4.1.20/kernel/irq/manage.c 2016-03-21 20:18:33.000000000 +0100
  14146. @@ -22,6 +22,7 @@
  14147. #include "internals.h"
  14148. #ifdef CONFIG_IRQ_FORCED_THREADING
  14149. +# ifndef CONFIG_PREEMPT_RT_BASE
  14150. __read_mostly bool force_irqthreads;
  14151. static int __init setup_forced_irqthreads(char *arg)
  14152. @@ -30,6 +31,7 @@
  14153. return 0;
  14154. }
  14155. early_param("threadirqs", setup_forced_irqthreads);
  14156. +# endif
  14157. #endif
  14158. static void __synchronize_hardirq(struct irq_desc *desc)
  14159. @@ -179,6 +181,62 @@
  14160. irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
  14161. #endif
  14162. +#ifdef CONFIG_PREEMPT_RT_FULL
  14163. +static void _irq_affinity_notify(struct irq_affinity_notify *notify);
  14164. +static struct task_struct *set_affinity_helper;
  14165. +static LIST_HEAD(affinity_list);
  14166. +static DEFINE_RAW_SPINLOCK(affinity_list_lock);
  14167. +
  14168. +static int set_affinity_thread(void *unused)
  14169. +{
  14170. + while (1) {
  14171. + struct irq_affinity_notify *notify;
  14172. + int empty;
  14173. +
  14174. + set_current_state(TASK_INTERRUPTIBLE);
  14175. +
  14176. + raw_spin_lock_irq(&affinity_list_lock);
  14177. + empty = list_empty(&affinity_list);
  14178. + raw_spin_unlock_irq(&affinity_list_lock);
  14179. +
  14180. + if (empty)
  14181. + schedule();
  14182. + if (kthread_should_stop())
  14183. + break;
  14184. + set_current_state(TASK_RUNNING);
  14185. +try_next:
  14186. + notify = NULL;
  14187. +
  14188. + raw_spin_lock_irq(&affinity_list_lock);
  14189. + if (!list_empty(&affinity_list)) {
  14190. + notify = list_first_entry(&affinity_list,
  14191. + struct irq_affinity_notify, list);
  14192. + list_del_init(&notify->list);
  14193. + }
  14194. + raw_spin_unlock_irq(&affinity_list_lock);
  14195. +
  14196. + if (!notify)
  14197. + continue;
  14198. + _irq_affinity_notify(notify);
  14199. + goto try_next;
  14200. + }
  14201. + return 0;
  14202. +}
  14203. +
  14204. +static void init_helper_thread(void)
  14205. +{
  14206. + if (set_affinity_helper)
  14207. + return;
  14208. + set_affinity_helper = kthread_run(set_affinity_thread, NULL,
  14209. + "affinity-cb");
  14210. + WARN_ON(IS_ERR(set_affinity_helper));
  14211. +}
  14212. +#else
  14213. +
  14214. +static inline void init_helper_thread(void) { }
  14215. +
  14216. +#endif
  14217. +
  14218. int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
  14219. bool force)
  14220. {
  14221. @@ -218,7 +276,17 @@
  14222. if (desc->affinity_notify) {
  14223. kref_get(&desc->affinity_notify->kref);
  14224. +
  14225. +#ifdef CONFIG_PREEMPT_RT_FULL
  14226. + raw_spin_lock(&affinity_list_lock);
  14227. + if (list_empty(&desc->affinity_notify->list))
  14228. + list_add_tail(&affinity_list,
  14229. + &desc->affinity_notify->list);
  14230. + raw_spin_unlock(&affinity_list_lock);
  14231. + wake_up_process(set_affinity_helper);
  14232. +#else
  14233. schedule_work(&desc->affinity_notify->work);
  14234. +#endif
  14235. }
  14236. irqd_set(data, IRQD_AFFINITY_SET);
  14237. @@ -256,10 +324,8 @@
  14238. }
  14239. EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
  14240. -static void irq_affinity_notify(struct work_struct *work)
  14241. +static void _irq_affinity_notify(struct irq_affinity_notify *notify)
  14242. {
  14243. - struct irq_affinity_notify *notify =
  14244. - container_of(work, struct irq_affinity_notify, work);
  14245. struct irq_desc *desc = irq_to_desc(notify->irq);
  14246. cpumask_var_t cpumask;
  14247. unsigned long flags;
  14248. @@ -281,6 +347,13 @@
  14249. kref_put(&notify->kref, notify->release);
  14250. }
  14251. +static void irq_affinity_notify(struct work_struct *work)
  14252. +{
  14253. + struct irq_affinity_notify *notify =
  14254. + container_of(work, struct irq_affinity_notify, work);
  14255. + _irq_affinity_notify(notify);
  14256. +}
  14257. +
  14258. /**
  14259. * irq_set_affinity_notifier - control notification of IRQ affinity changes
  14260. * @irq: Interrupt for which to enable/disable notification
  14261. @@ -310,6 +383,8 @@
  14262. notify->irq = irq;
  14263. kref_init(&notify->kref);
  14264. INIT_WORK(&notify->work, irq_affinity_notify);
  14265. + INIT_LIST_HEAD(&notify->list);
  14266. + init_helper_thread();
  14267. }
  14268. raw_spin_lock_irqsave(&desc->lock, flags);
  14269. @@ -697,6 +772,12 @@
  14270. return IRQ_NONE;
  14271. }
  14272. +static irqreturn_t irq_forced_secondary_handler(int irq, void *dev_id)
  14273. +{
  14274. + WARN(1, "Secondary action handler called for irq %d\n", irq);
  14275. + return IRQ_NONE;
  14276. +}
  14277. +
  14278. static int irq_wait_for_interrupt(struct irqaction *action)
  14279. {
  14280. set_current_state(TASK_INTERRUPTIBLE);
  14281. @@ -723,7 +804,8 @@
  14282. static void irq_finalize_oneshot(struct irq_desc *desc,
  14283. struct irqaction *action)
  14284. {
  14285. - if (!(desc->istate & IRQS_ONESHOT))
  14286. + if (!(desc->istate & IRQS_ONESHOT) ||
  14287. + action->handler == irq_forced_secondary_handler)
  14288. return;
  14289. again:
  14290. chip_bus_lock(desc);
  14291. @@ -825,7 +907,15 @@
  14292. local_bh_disable();
  14293. ret = action->thread_fn(action->irq, action->dev_id);
  14294. irq_finalize_oneshot(desc, action);
  14295. - local_bh_enable();
  14296. + /*
  14297. + * Interrupts which have real time requirements can be set up
  14298. + * to avoid softirq processing in the thread handler. This is
  14299. + * safe as these interrupts do not raise soft interrupts.
  14300. + */
  14301. + if (irq_settings_no_softirq_call(desc))
  14302. + _local_bh_enable();
  14303. + else
  14304. + local_bh_enable();
  14305. return ret;
  14306. }
  14307. @@ -877,6 +967,18 @@
  14308. irq_finalize_oneshot(desc, action);
  14309. }
  14310. +static void irq_wake_secondary(struct irq_desc *desc, struct irqaction *action)
  14311. +{
  14312. + struct irqaction *secondary = action->secondary;
  14313. +
  14314. + if (WARN_ON_ONCE(!secondary))
  14315. + return;
  14316. +
  14317. + raw_spin_lock_irq(&desc->lock);
  14318. + __irq_wake_thread(desc, secondary);
  14319. + raw_spin_unlock_irq(&desc->lock);
  14320. +}
  14321. +
  14322. /*
  14323. * Interrupt handler thread
  14324. */
  14325. @@ -907,7 +1009,15 @@
  14326. action_ret = handler_fn(desc, action);
  14327. if (action_ret == IRQ_HANDLED)
  14328. atomic_inc(&desc->threads_handled);
  14329. + if (action_ret == IRQ_WAKE_THREAD)
  14330. + irq_wake_secondary(desc, action);
  14331. +#ifdef CONFIG_PREEMPT_RT_FULL
  14332. + migrate_disable();
  14333. + add_interrupt_randomness(action->irq, 0,
  14334. + desc->random_ip ^ (unsigned long) action);
  14335. + migrate_enable();
  14336. +#endif
  14337. wake_threads_waitq(desc);
  14338. }
  14339. @@ -951,20 +1061,36 @@
  14340. }
  14341. EXPORT_SYMBOL_GPL(irq_wake_thread);
  14342. -static void irq_setup_forced_threading(struct irqaction *new)
  14343. +static int irq_setup_forced_threading(struct irqaction *new)
  14344. {
  14345. if (!force_irqthreads)
  14346. - return;
  14347. + return 0;
  14348. if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
  14349. - return;
  14350. + return 0;
  14351. new->flags |= IRQF_ONESHOT;
  14352. - if (!new->thread_fn) {
  14353. - set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
  14354. - new->thread_fn = new->handler;
  14355. - new->handler = irq_default_primary_handler;
  14356. - }
  14357. + /*
  14358. + * Handle the case where we have a real primary handler and a
  14359. + * thread handler. We force thread them as well by creating a
  14360. + * secondary action.
  14361. + */
  14362. + if (new->handler != irq_default_primary_handler && new->thread_fn) {
  14363. + /* Allocate the secondary action */
  14364. + new->secondary = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  14365. + if (!new->secondary)
  14366. + return -ENOMEM;
  14367. + new->secondary->handler = irq_forced_secondary_handler;
  14368. + new->secondary->thread_fn = new->thread_fn;
  14369. + new->secondary->dev_id = new->dev_id;
  14370. + new->secondary->irq = new->irq;
  14371. + new->secondary->name = new->name;
  14372. + }
  14373. + /* Deal with the primary handler */
  14374. + set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
  14375. + new->thread_fn = new->handler;
  14376. + new->handler = irq_default_primary_handler;
  14377. + return 0;
  14378. }
  14379. static int irq_request_resources(struct irq_desc *desc)
  14380. @@ -984,6 +1110,48 @@
  14381. c->irq_release_resources(d);
  14382. }
  14383. +static int
  14384. +setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary)
  14385. +{
  14386. + struct task_struct *t;
  14387. + struct sched_param param = {
  14388. + .sched_priority = MAX_USER_RT_PRIO/2,
  14389. + };
  14390. +
  14391. + if (!secondary) {
  14392. + t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
  14393. + new->name);
  14394. + } else {
  14395. + t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq,
  14396. + new->name);
  14397. + param.sched_priority += 1;
  14398. + }
  14399. +
  14400. + if (IS_ERR(t))
  14401. + return PTR_ERR(t);
  14402. +
  14403. + sched_setscheduler_nocheck(t, SCHED_FIFO, &param);
  14404. +
  14405. + /*
  14406. + * We keep the reference to the task struct even if
  14407. + * the thread dies to avoid that the interrupt code
  14408. + * references an already freed task_struct.
  14409. + */
  14410. + get_task_struct(t);
  14411. + new->thread = t;
  14412. + /*
  14413. + * Tell the thread to set its affinity. This is
  14414. + * important for shared interrupt handlers as we do
  14415. + * not invoke setup_affinity() for the secondary
  14416. + * handlers as everything is already set up. Even for
  14417. + * interrupts marked with IRQF_NO_BALANCE this is
  14418. + * correct as we want the thread to move to the cpu(s)
  14419. + * on which the requesting code placed the interrupt.
  14420. + */
  14421. + set_bit(IRQTF_AFFINITY, &new->thread_flags);
  14422. + return 0;
  14423. +}
  14424. +
  14425. /*
  14426. * Internal function to register an irqaction - typically used to
  14427. * allocate special interrupts that are part of the architecture.
  14428. @@ -1004,6 +1172,8 @@
  14429. if (!try_module_get(desc->owner))
  14430. return -ENODEV;
  14431. + new->irq = irq;
  14432. +
  14433. /*
  14434. * Check whether the interrupt nests into another interrupt
  14435. * thread.
  14436. @@ -1021,8 +1191,11 @@
  14437. */
  14438. new->handler = irq_nested_primary_handler;
  14439. } else {
  14440. - if (irq_settings_can_thread(desc))
  14441. - irq_setup_forced_threading(new);
  14442. + if (irq_settings_can_thread(desc)) {
  14443. + ret = irq_setup_forced_threading(new);
  14444. + if (ret)
  14445. + goto out_mput;
  14446. + }
  14447. }
  14448. /*
  14449. @@ -1031,37 +1204,14 @@
  14450. * thread.
  14451. */
  14452. if (new->thread_fn && !nested) {
  14453. - struct task_struct *t;
  14454. - static const struct sched_param param = {
  14455. - .sched_priority = MAX_USER_RT_PRIO/2,
  14456. - };
  14457. -
  14458. - t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
  14459. - new->name);
  14460. - if (IS_ERR(t)) {
  14461. - ret = PTR_ERR(t);
  14462. + ret = setup_irq_thread(new, irq, false);
  14463. + if (ret)
  14464. goto out_mput;
  14465. + if (new->secondary) {
  14466. + ret = setup_irq_thread(new->secondary, irq, true);
  14467. + if (ret)
  14468. + goto out_thread;
  14469. }
  14470. -
  14471. - sched_setscheduler_nocheck(t, SCHED_FIFO, &param);
  14472. -
  14473. - /*
  14474. - * We keep the reference to the task struct even if
  14475. - * the thread dies to avoid that the interrupt code
  14476. - * references an already freed task_struct.
  14477. - */
  14478. - get_task_struct(t);
  14479. - new->thread = t;
  14480. - /*
  14481. - * Tell the thread to set its affinity. This is
  14482. - * important for shared interrupt handlers as we do
  14483. - * not invoke setup_affinity() for the secondary
  14484. - * handlers as everything is already set up. Even for
  14485. - * interrupts marked with IRQF_NO_BALANCE this is
  14486. - * correct as we want the thread to move to the cpu(s)
  14487. - * on which the requesting code placed the interrupt.
  14488. - */
  14489. - set_bit(IRQTF_AFFINITY, &new->thread_flags);
  14490. }
  14491. if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
  14492. @@ -1221,6 +1371,9 @@
  14493. irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
  14494. }
  14495. + if (new->flags & IRQF_NO_SOFTIRQ_CALL)
  14496. + irq_settings_set_no_softirq_call(desc);
  14497. +
  14498. /* Set default affinity mask once everything is setup */
  14499. setup_affinity(irq, desc, mask);
  14500. @@ -1234,7 +1387,6 @@
  14501. irq, nmsk, omsk);
  14502. }
  14503. - new->irq = irq;
  14504. *old_ptr = new;
  14505. irq_pm_install_action(desc, new);
  14506. @@ -1260,6 +1412,8 @@
  14507. */
  14508. if (new->thread)
  14509. wake_up_process(new->thread);
  14510. + if (new->secondary)
  14511. + wake_up_process(new->secondary->thread);
  14512. register_irq_proc(irq, desc);
  14513. new->dir = NULL;
  14514. @@ -1290,6 +1444,13 @@
  14515. kthread_stop(t);
  14516. put_task_struct(t);
  14517. }
  14518. + if (new->secondary && new->secondary->thread) {
  14519. + struct task_struct *t = new->secondary->thread;
  14520. +
  14521. + new->secondary->thread = NULL;
  14522. + kthread_stop(t);
  14523. + put_task_struct(t);
  14524. + }
  14525. out_mput:
  14526. module_put(desc->owner);
  14527. return ret;
  14528. @@ -1397,9 +1558,14 @@
  14529. if (action->thread) {
  14530. kthread_stop(action->thread);
  14531. put_task_struct(action->thread);
  14532. + if (action->secondary && action->secondary->thread) {
  14533. + kthread_stop(action->secondary->thread);
  14534. + put_task_struct(action->secondary->thread);
  14535. + }
  14536. }
  14537. module_put(desc->owner);
  14538. + kfree(action->secondary);
  14539. return action;
  14540. }
  14541. @@ -1543,8 +1709,10 @@
  14542. retval = __setup_irq(irq, desc, action);
  14543. chip_bus_sync_unlock(desc);
  14544. - if (retval)
  14545. + if (retval) {
  14546. + kfree(action->secondary);
  14547. kfree(action);
  14548. + }
  14549. #ifdef CONFIG_DEBUG_SHIRQ_FIXME
  14550. if (!retval && (irqflags & IRQF_SHARED)) {
  14551. diff -Nur linux-4.1.20.orig/kernel/irq/settings.h linux-4.1.20/kernel/irq/settings.h
  14552. --- linux-4.1.20.orig/kernel/irq/settings.h 2016-03-17 19:11:03.000000000 +0100
  14553. +++ linux-4.1.20/kernel/irq/settings.h 2016-03-21 20:18:33.000000000 +0100
  14554. @@ -15,6 +15,7 @@
  14555. _IRQ_NESTED_THREAD = IRQ_NESTED_THREAD,
  14556. _IRQ_PER_CPU_DEVID = IRQ_PER_CPU_DEVID,
  14557. _IRQ_IS_POLLED = IRQ_IS_POLLED,
  14558. + _IRQ_NO_SOFTIRQ_CALL = IRQ_NO_SOFTIRQ_CALL,
  14559. _IRQF_MODIFY_MASK = IRQF_MODIFY_MASK,
  14560. };
  14561. @@ -28,6 +29,7 @@
  14562. #define IRQ_NESTED_THREAD GOT_YOU_MORON
  14563. #define IRQ_PER_CPU_DEVID GOT_YOU_MORON
  14564. #define IRQ_IS_POLLED GOT_YOU_MORON
  14565. +#define IRQ_NO_SOFTIRQ_CALL GOT_YOU_MORON
  14566. #undef IRQF_MODIFY_MASK
  14567. #define IRQF_MODIFY_MASK GOT_YOU_MORON
  14568. @@ -38,6 +40,16 @@
  14569. desc->status_use_accessors |= (set & _IRQF_MODIFY_MASK);
  14570. }
  14571. +static inline bool irq_settings_no_softirq_call(struct irq_desc *desc)
  14572. +{
  14573. + return desc->status_use_accessors & _IRQ_NO_SOFTIRQ_CALL;
  14574. +}
  14575. +
  14576. +static inline void irq_settings_set_no_softirq_call(struct irq_desc *desc)
  14577. +{
  14578. + desc->status_use_accessors |= _IRQ_NO_SOFTIRQ_CALL;
  14579. +}
  14580. +
  14581. static inline bool irq_settings_is_per_cpu(struct irq_desc *desc)
  14582. {
  14583. return desc->status_use_accessors & _IRQ_PER_CPU;
  14584. diff -Nur linux-4.1.20.orig/kernel/irq/spurious.c linux-4.1.20/kernel/irq/spurious.c
  14585. --- linux-4.1.20.orig/kernel/irq/spurious.c 2016-03-17 19:11:03.000000000 +0100
  14586. +++ linux-4.1.20/kernel/irq/spurious.c 2016-03-21 20:18:33.000000000 +0100
  14587. @@ -444,6 +444,10 @@
  14588. static int __init irqfixup_setup(char *str)
  14589. {
  14590. +#ifdef CONFIG_PREEMPT_RT_BASE
  14591. + pr_warn("irqfixup boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  14592. + return 1;
  14593. +#endif
  14594. irqfixup = 1;
  14595. printk(KERN_WARNING "Misrouted IRQ fixup support enabled.\n");
  14596. printk(KERN_WARNING "This may impact system performance.\n");
  14597. @@ -456,6 +460,10 @@
  14598. static int __init irqpoll_setup(char *str)
  14599. {
  14600. +#ifdef CONFIG_PREEMPT_RT_BASE
  14601. + pr_warn("irqpoll boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  14602. + return 1;
  14603. +#endif
  14604. irqfixup = 2;
  14605. printk(KERN_WARNING "Misrouted IRQ fixup and polling support "
  14606. "enabled\n");
  14607. diff -Nur linux-4.1.20.orig/kernel/irq_work.c linux-4.1.20/kernel/irq_work.c
  14608. --- linux-4.1.20.orig/kernel/irq_work.c 2016-03-17 19:11:03.000000000 +0100
  14609. +++ linux-4.1.20/kernel/irq_work.c 2016-03-21 20:18:33.000000000 +0100
  14610. @@ -17,6 +17,7 @@
  14611. #include <linux/cpu.h>
  14612. #include <linux/notifier.h>
  14613. #include <linux/smp.h>
  14614. +#include <linux/interrupt.h>
  14615. #include <asm/processor.h>
  14616. @@ -65,6 +66,8 @@
  14617. */
  14618. bool irq_work_queue_on(struct irq_work *work, int cpu)
  14619. {
  14620. + struct llist_head *list;
  14621. +
  14622. /* All work should have been flushed before going offline */
  14623. WARN_ON_ONCE(cpu_is_offline(cpu));
  14624. @@ -75,7 +78,12 @@
  14625. if (!irq_work_claim(work))
  14626. return false;
  14627. - if (llist_add(&work->llnode, &per_cpu(raised_list, cpu)))
  14628. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL) && !(work->flags & IRQ_WORK_HARD_IRQ))
  14629. + list = &per_cpu(lazy_list, cpu);
  14630. + else
  14631. + list = &per_cpu(raised_list, cpu);
  14632. +
  14633. + if (llist_add(&work->llnode, list))
  14634. arch_send_call_function_single_ipi(cpu);
  14635. return true;
  14636. @@ -86,6 +94,9 @@
  14637. /* Enqueue the irq work @work on the current CPU */
  14638. bool irq_work_queue(struct irq_work *work)
  14639. {
  14640. + struct llist_head *list;
  14641. + bool lazy_work, realtime = IS_ENABLED(CONFIG_PREEMPT_RT_FULL);
  14642. +
  14643. /* Only queue if not already pending */
  14644. if (!irq_work_claim(work))
  14645. return false;
  14646. @@ -93,13 +104,15 @@
  14647. /* Queue the entry and raise the IPI if needed. */
  14648. preempt_disable();
  14649. - /* If the work is "lazy", handle it from next tick if any */
  14650. - if (work->flags & IRQ_WORK_LAZY) {
  14651. - if (llist_add(&work->llnode, this_cpu_ptr(&lazy_list)) &&
  14652. - tick_nohz_tick_stopped())
  14653. - arch_irq_work_raise();
  14654. - } else {
  14655. - if (llist_add(&work->llnode, this_cpu_ptr(&raised_list)))
  14656. + lazy_work = work->flags & IRQ_WORK_LAZY;
  14657. +
  14658. + if (lazy_work || (realtime && !(work->flags & IRQ_WORK_HARD_IRQ)))
  14659. + list = this_cpu_ptr(&lazy_list);
  14660. + else
  14661. + list = this_cpu_ptr(&raised_list);
  14662. +
  14663. + if (llist_add(&work->llnode, list)) {
  14664. + if (!lazy_work || tick_nohz_tick_stopped())
  14665. arch_irq_work_raise();
  14666. }
  14667. @@ -116,9 +129,8 @@
  14668. raised = this_cpu_ptr(&raised_list);
  14669. lazy = this_cpu_ptr(&lazy_list);
  14670. - if (llist_empty(raised) || arch_irq_work_has_interrupt())
  14671. - if (llist_empty(lazy))
  14672. - return false;
  14673. + if (llist_empty(raised) && llist_empty(lazy))
  14674. + return false;
  14675. /* All work should have been flushed before going offline */
  14676. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  14677. @@ -132,7 +144,7 @@
  14678. struct irq_work *work;
  14679. struct llist_node *llnode;
  14680. - BUG_ON(!irqs_disabled());
  14681. + BUG_ON_NONRT(!irqs_disabled());
  14682. if (llist_empty(list))
  14683. return;
  14684. @@ -169,7 +181,16 @@
  14685. void irq_work_run(void)
  14686. {
  14687. irq_work_run_list(this_cpu_ptr(&raised_list));
  14688. - irq_work_run_list(this_cpu_ptr(&lazy_list));
  14689. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL)) {
  14690. + /*
  14691. + * NOTE: we raise softirq via IPI for safety,
  14692. + * and execute in irq_work_tick() to move the
  14693. + * overhead from hard to soft irq context.
  14694. + */
  14695. + if (!llist_empty(this_cpu_ptr(&lazy_list)))
  14696. + raise_softirq(TIMER_SOFTIRQ);
  14697. + } else
  14698. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  14699. }
  14700. EXPORT_SYMBOL_GPL(irq_work_run);
  14701. @@ -179,8 +200,17 @@
  14702. if (!llist_empty(raised) && !arch_irq_work_has_interrupt())
  14703. irq_work_run_list(raised);
  14704. +
  14705. + if (!IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  14706. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  14707. +}
  14708. +
  14709. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  14710. +void irq_work_tick_soft(void)
  14711. +{
  14712. irq_work_run_list(this_cpu_ptr(&lazy_list));
  14713. }
  14714. +#endif
  14715. /*
  14716. * Synchronize against the irq_work @entry, ensures the entry is not
  14717. diff -Nur linux-4.1.20.orig/kernel/ksysfs.c linux-4.1.20/kernel/ksysfs.c
  14718. --- linux-4.1.20.orig/kernel/ksysfs.c 2016-03-17 19:11:03.000000000 +0100
  14719. +++ linux-4.1.20/kernel/ksysfs.c 2016-03-21 20:18:33.000000000 +0100
  14720. @@ -136,6 +136,15 @@
  14721. #endif /* CONFIG_KEXEC */
  14722. +#if defined(CONFIG_PREEMPT_RT_FULL)
  14723. +static ssize_t realtime_show(struct kobject *kobj,
  14724. + struct kobj_attribute *attr, char *buf)
  14725. +{
  14726. + return sprintf(buf, "%d\n", 1);
  14727. +}
  14728. +KERNEL_ATTR_RO(realtime);
  14729. +#endif
  14730. +
  14731. /* whether file capabilities are enabled */
  14732. static ssize_t fscaps_show(struct kobject *kobj,
  14733. struct kobj_attribute *attr, char *buf)
  14734. @@ -203,6 +212,9 @@
  14735. &vmcoreinfo_attr.attr,
  14736. #endif
  14737. &rcu_expedited_attr.attr,
  14738. +#ifdef CONFIG_PREEMPT_RT_FULL
  14739. + &realtime_attr.attr,
  14740. +#endif
  14741. NULL
  14742. };
  14743. diff -Nur linux-4.1.20.orig/kernel/locking/Makefile linux-4.1.20/kernel/locking/Makefile
  14744. --- linux-4.1.20.orig/kernel/locking/Makefile 2016-03-17 19:11:03.000000000 +0100
  14745. +++ linux-4.1.20/kernel/locking/Makefile 2016-03-21 20:18:33.000000000 +0100
  14746. @@ -1,5 +1,5 @@
  14747. -obj-y += mutex.o semaphore.o rwsem.o
  14748. +obj-y += semaphore.o
  14749. ifdef CONFIG_FUNCTION_TRACER
  14750. CFLAGS_REMOVE_lockdep.o = $(CC_FLAGS_FTRACE)
  14751. @@ -8,7 +8,11 @@
  14752. CFLAGS_REMOVE_rtmutex-debug.o = $(CC_FLAGS_FTRACE)
  14753. endif
  14754. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  14755. +obj-y += mutex.o
  14756. obj-$(CONFIG_DEBUG_MUTEXES) += mutex-debug.o
  14757. +obj-y += rwsem.o
  14758. +endif
  14759. obj-$(CONFIG_LOCKDEP) += lockdep.o
  14760. ifeq ($(CONFIG_PROC_FS),y)
  14761. obj-$(CONFIG_LOCKDEP) += lockdep_proc.o
  14762. @@ -22,8 +26,11 @@
  14763. obj-$(CONFIG_RT_MUTEX_TESTER) += rtmutex-tester.o
  14764. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o
  14765. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock_debug.o
  14766. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  14767. obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
  14768. obj-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem-xadd.o
  14769. +endif
  14770. obj-$(CONFIG_PERCPU_RWSEM) += percpu-rwsem.o
  14771. +obj-$(CONFIG_PREEMPT_RT_FULL) += rt.o
  14772. obj-$(CONFIG_QUEUE_RWLOCK) += qrwlock.o
  14773. obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o
  14774. diff -Nur linux-4.1.20.orig/kernel/locking/lglock.c linux-4.1.20/kernel/locking/lglock.c
  14775. --- linux-4.1.20.orig/kernel/locking/lglock.c 2016-03-17 19:11:03.000000000 +0100
  14776. +++ linux-4.1.20/kernel/locking/lglock.c 2016-03-21 20:18:33.000000000 +0100
  14777. @@ -4,6 +4,15 @@
  14778. #include <linux/cpu.h>
  14779. #include <linux/string.h>
  14780. +#ifndef CONFIG_PREEMPT_RT_FULL
  14781. +# define lg_lock_ptr arch_spinlock_t
  14782. +# define lg_do_lock(l) arch_spin_lock(l)
  14783. +# define lg_do_unlock(l) arch_spin_unlock(l)
  14784. +#else
  14785. +# define lg_lock_ptr struct rt_mutex
  14786. +# define lg_do_lock(l) __rt_spin_lock(l)
  14787. +# define lg_do_unlock(l) __rt_spin_unlock(l)
  14788. +#endif
  14789. /*
  14790. * Note there is no uninit, so lglocks cannot be defined in
  14791. * modules (but it's fine to use them from there)
  14792. @@ -12,51 +21,60 @@
  14793. void lg_lock_init(struct lglock *lg, char *name)
  14794. {
  14795. +#ifdef CONFIG_PREEMPT_RT_FULL
  14796. + int i;
  14797. +
  14798. + for_each_possible_cpu(i) {
  14799. + struct rt_mutex *lock = per_cpu_ptr(lg->lock, i);
  14800. +
  14801. + rt_mutex_init(lock);
  14802. + }
  14803. +#endif
  14804. LOCKDEP_INIT_MAP(&lg->lock_dep_map, name, &lg->lock_key, 0);
  14805. }
  14806. EXPORT_SYMBOL(lg_lock_init);
  14807. void lg_local_lock(struct lglock *lg)
  14808. {
  14809. - arch_spinlock_t *lock;
  14810. + lg_lock_ptr *lock;
  14811. - preempt_disable();
  14812. + migrate_disable();
  14813. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  14814. lock = this_cpu_ptr(lg->lock);
  14815. - arch_spin_lock(lock);
  14816. + lg_do_lock(lock);
  14817. }
  14818. EXPORT_SYMBOL(lg_local_lock);
  14819. void lg_local_unlock(struct lglock *lg)
  14820. {
  14821. - arch_spinlock_t *lock;
  14822. + lg_lock_ptr *lock;
  14823. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  14824. lock = this_cpu_ptr(lg->lock);
  14825. - arch_spin_unlock(lock);
  14826. - preempt_enable();
  14827. + lg_do_unlock(lock);
  14828. + migrate_enable();
  14829. }
  14830. EXPORT_SYMBOL(lg_local_unlock);
  14831. void lg_local_lock_cpu(struct lglock *lg, int cpu)
  14832. {
  14833. - arch_spinlock_t *lock;
  14834. + lg_lock_ptr *lock;
  14835. - preempt_disable();
  14836. + preempt_disable_nort();
  14837. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  14838. lock = per_cpu_ptr(lg->lock, cpu);
  14839. - arch_spin_lock(lock);
  14840. + lg_do_lock(lock);
  14841. }
  14842. EXPORT_SYMBOL(lg_local_lock_cpu);
  14843. void lg_local_unlock_cpu(struct lglock *lg, int cpu)
  14844. {
  14845. - arch_spinlock_t *lock;
  14846. + lg_lock_ptr *lock;
  14847. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  14848. lock = per_cpu_ptr(lg->lock, cpu);
  14849. - arch_spin_unlock(lock);
  14850. - preempt_enable();
  14851. + lg_do_unlock(lock);
  14852. + preempt_enable_nort();
  14853. }
  14854. EXPORT_SYMBOL(lg_local_unlock_cpu);
  14855. @@ -64,12 +82,12 @@
  14856. {
  14857. int i;
  14858. - preempt_disable();
  14859. + preempt_disable_nort();
  14860. lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  14861. for_each_possible_cpu(i) {
  14862. - arch_spinlock_t *lock;
  14863. + lg_lock_ptr *lock;
  14864. lock = per_cpu_ptr(lg->lock, i);
  14865. - arch_spin_lock(lock);
  14866. + lg_do_lock(lock);
  14867. }
  14868. }
  14869. EXPORT_SYMBOL(lg_global_lock);
  14870. @@ -80,10 +98,35 @@
  14871. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  14872. for_each_possible_cpu(i) {
  14873. - arch_spinlock_t *lock;
  14874. + lg_lock_ptr *lock;
  14875. lock = per_cpu_ptr(lg->lock, i);
  14876. - arch_spin_unlock(lock);
  14877. + lg_do_unlock(lock);
  14878. }
  14879. - preempt_enable();
  14880. + preempt_enable_nort();
  14881. }
  14882. EXPORT_SYMBOL(lg_global_unlock);
  14883. +
  14884. +#ifdef CONFIG_PREEMPT_RT_FULL
  14885. +/*
  14886. + * HACK: If you use this, you get to keep the pieces.
  14887. + * Used in queue_stop_cpus_work() when stop machinery
  14888. + * is called from inactive CPU, so we can't schedule.
  14889. + */
  14890. +# define lg_do_trylock_relax(l) \
  14891. + do { \
  14892. + while (!__rt_spin_trylock(l)) \
  14893. + cpu_relax(); \
  14894. + } while (0)
  14895. +
  14896. +void lg_global_trylock_relax(struct lglock *lg)
  14897. +{
  14898. + int i;
  14899. +
  14900. + lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  14901. + for_each_possible_cpu(i) {
  14902. + lg_lock_ptr *lock;
  14903. + lock = per_cpu_ptr(lg->lock, i);
  14904. + lg_do_trylock_relax(lock);
  14905. + }
  14906. +}
  14907. +#endif
  14908. diff -Nur linux-4.1.20.orig/kernel/locking/lockdep.c linux-4.1.20/kernel/locking/lockdep.c
  14909. --- linux-4.1.20.orig/kernel/locking/lockdep.c 2016-03-17 19:11:03.000000000 +0100
  14910. +++ linux-4.1.20/kernel/locking/lockdep.c 2016-03-21 20:18:33.000000000 +0100
  14911. @@ -3563,6 +3563,7 @@
  14912. }
  14913. }
  14914. +#ifndef CONFIG_PREEMPT_RT_FULL
  14915. /*
  14916. * We dont accurately track softirq state in e.g.
  14917. * hardirq contexts (such as on 4KSTACKS), so only
  14918. @@ -3577,6 +3578,7 @@
  14919. DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
  14920. }
  14921. }
  14922. +#endif
  14923. if (!debug_locks)
  14924. print_irqtrace_events(current);
  14925. diff -Nur linux-4.1.20.orig/kernel/locking/locktorture.c linux-4.1.20/kernel/locking/locktorture.c
  14926. --- linux-4.1.20.orig/kernel/locking/locktorture.c 2016-03-17 19:11:03.000000000 +0100
  14927. +++ linux-4.1.20/kernel/locking/locktorture.c 2016-03-21 20:18:33.000000000 +0100
  14928. @@ -24,7 +24,6 @@
  14929. #include <linux/module.h>
  14930. #include <linux/kthread.h>
  14931. #include <linux/spinlock.h>
  14932. -#include <linux/rwlock.h>
  14933. #include <linux/mutex.h>
  14934. #include <linux/rwsem.h>
  14935. #include <linux/smp.h>
  14936. diff -Nur linux-4.1.20.orig/kernel/locking/rt.c linux-4.1.20/kernel/locking/rt.c
  14937. --- linux-4.1.20.orig/kernel/locking/rt.c 1970-01-01 01:00:00.000000000 +0100
  14938. +++ linux-4.1.20/kernel/locking/rt.c 2016-03-21 20:18:33.000000000 +0100
  14939. @@ -0,0 +1,461 @@
  14940. +/*
  14941. + * kernel/rt.c
  14942. + *
  14943. + * Real-Time Preemption Support
  14944. + *
  14945. + * started by Ingo Molnar:
  14946. + *
  14947. + * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  14948. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  14949. + *
  14950. + * historic credit for proving that Linux spinlocks can be implemented via
  14951. + * RT-aware mutexes goes to many people: The Pmutex project (Dirk Grambow
  14952. + * and others) who prototyped it on 2.4 and did lots of comparative
  14953. + * research and analysis; TimeSys, for proving that you can implement a
  14954. + * fully preemptible kernel via the use of IRQ threading and mutexes;
  14955. + * Bill Huey for persuasively arguing on lkml that the mutex model is the
  14956. + * right one; and to MontaVista, who ported pmutexes to 2.6.
  14957. + *
  14958. + * This code is a from-scratch implementation and is not based on pmutexes,
  14959. + * but the idea of converting spinlocks to mutexes is used here too.
  14960. + *
  14961. + * lock debugging, locking tree, deadlock detection:
  14962. + *
  14963. + * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey
  14964. + * Released under the General Public License (GPL).
  14965. + *
  14966. + * Includes portions of the generic R/W semaphore implementation from:
  14967. + *
  14968. + * Copyright (c) 2001 David Howells (dhowells@redhat.com).
  14969. + * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
  14970. + * - Derived also from comments by Linus
  14971. + *
  14972. + * Pending ownership of locks and ownership stealing:
  14973. + *
  14974. + * Copyright (C) 2005, Kihon Technologies Inc., Steven Rostedt
  14975. + *
  14976. + * (also by Steven Rostedt)
  14977. + * - Converted single pi_lock to individual task locks.
  14978. + *
  14979. + * By Esben Nielsen:
  14980. + * Doing priority inheritance with help of the scheduler.
  14981. + *
  14982. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  14983. + * - major rework based on Esben Nielsens initial patch
  14984. + * - replaced thread_info references by task_struct refs
  14985. + * - removed task->pending_owner dependency
  14986. + * - BKL drop/reacquire for semaphore style locks to avoid deadlocks
  14987. + * in the scheduler return path as discussed with Steven Rostedt
  14988. + *
  14989. + * Copyright (C) 2006, Kihon Technologies Inc.
  14990. + * Steven Rostedt <rostedt@goodmis.org>
  14991. + * - debugged and patched Thomas Gleixner's rework.
  14992. + * - added back the cmpxchg to the rework.
  14993. + * - turned atomic require back on for SMP.
  14994. + */
  14995. +
  14996. +#include <linux/spinlock.h>
  14997. +#include <linux/rtmutex.h>
  14998. +#include <linux/sched.h>
  14999. +#include <linux/delay.h>
  15000. +#include <linux/module.h>
  15001. +#include <linux/kallsyms.h>
  15002. +#include <linux/syscalls.h>
  15003. +#include <linux/interrupt.h>
  15004. +#include <linux/plist.h>
  15005. +#include <linux/fs.h>
  15006. +#include <linux/futex.h>
  15007. +#include <linux/hrtimer.h>
  15008. +
  15009. +#include "rtmutex_common.h"
  15010. +
  15011. +/*
  15012. + * struct mutex functions
  15013. + */
  15014. +void __mutex_do_init(struct mutex *mutex, const char *name,
  15015. + struct lock_class_key *key)
  15016. +{
  15017. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  15018. + /*
  15019. + * Make sure we are not reinitializing a held lock:
  15020. + */
  15021. + debug_check_no_locks_freed((void *)mutex, sizeof(*mutex));
  15022. + lockdep_init_map(&mutex->dep_map, name, key, 0);
  15023. +#endif
  15024. + mutex->lock.save_state = 0;
  15025. +}
  15026. +EXPORT_SYMBOL(__mutex_do_init);
  15027. +
  15028. +void __lockfunc _mutex_lock(struct mutex *lock)
  15029. +{
  15030. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  15031. + rt_mutex_lock(&lock->lock);
  15032. +}
  15033. +EXPORT_SYMBOL(_mutex_lock);
  15034. +
  15035. +int __lockfunc _mutex_lock_interruptible(struct mutex *lock)
  15036. +{
  15037. + int ret;
  15038. +
  15039. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  15040. + ret = rt_mutex_lock_interruptible(&lock->lock);
  15041. + if (ret)
  15042. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  15043. + return ret;
  15044. +}
  15045. +EXPORT_SYMBOL(_mutex_lock_interruptible);
  15046. +
  15047. +int __lockfunc _mutex_lock_killable(struct mutex *lock)
  15048. +{
  15049. + int ret;
  15050. +
  15051. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  15052. + ret = rt_mutex_lock_killable(&lock->lock);
  15053. + if (ret)
  15054. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  15055. + return ret;
  15056. +}
  15057. +EXPORT_SYMBOL(_mutex_lock_killable);
  15058. +
  15059. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  15060. +void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass)
  15061. +{
  15062. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  15063. + rt_mutex_lock(&lock->lock);
  15064. +}
  15065. +EXPORT_SYMBOL(_mutex_lock_nested);
  15066. +
  15067. +void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
  15068. +{
  15069. + mutex_acquire_nest(&lock->dep_map, 0, 0, nest, _RET_IP_);
  15070. + rt_mutex_lock(&lock->lock);
  15071. +}
  15072. +EXPORT_SYMBOL(_mutex_lock_nest_lock);
  15073. +
  15074. +int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass)
  15075. +{
  15076. + int ret;
  15077. +
  15078. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  15079. + ret = rt_mutex_lock_interruptible(&lock->lock);
  15080. + if (ret)
  15081. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  15082. + return ret;
  15083. +}
  15084. +EXPORT_SYMBOL(_mutex_lock_interruptible_nested);
  15085. +
  15086. +int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass)
  15087. +{
  15088. + int ret;
  15089. +
  15090. + mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  15091. + ret = rt_mutex_lock_killable(&lock->lock);
  15092. + if (ret)
  15093. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  15094. + return ret;
  15095. +}
  15096. +EXPORT_SYMBOL(_mutex_lock_killable_nested);
  15097. +#endif
  15098. +
  15099. +int __lockfunc _mutex_trylock(struct mutex *lock)
  15100. +{
  15101. + int ret = rt_mutex_trylock(&lock->lock);
  15102. +
  15103. + if (ret)
  15104. + mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  15105. +
  15106. + return ret;
  15107. +}
  15108. +EXPORT_SYMBOL(_mutex_trylock);
  15109. +
  15110. +void __lockfunc _mutex_unlock(struct mutex *lock)
  15111. +{
  15112. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  15113. + rt_mutex_unlock(&lock->lock);
  15114. +}
  15115. +EXPORT_SYMBOL(_mutex_unlock);
  15116. +
  15117. +/*
  15118. + * rwlock_t functions
  15119. + */
  15120. +int __lockfunc rt_write_trylock(rwlock_t *rwlock)
  15121. +{
  15122. + int ret;
  15123. +
  15124. + migrate_disable();
  15125. + ret = rt_mutex_trylock(&rwlock->lock);
  15126. + if (ret)
  15127. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  15128. + else
  15129. + migrate_enable();
  15130. +
  15131. + return ret;
  15132. +}
  15133. +EXPORT_SYMBOL(rt_write_trylock);
  15134. +
  15135. +int __lockfunc rt_write_trylock_irqsave(rwlock_t *rwlock, unsigned long *flags)
  15136. +{
  15137. + int ret;
  15138. +
  15139. + *flags = 0;
  15140. + ret = rt_write_trylock(rwlock);
  15141. + return ret;
  15142. +}
  15143. +EXPORT_SYMBOL(rt_write_trylock_irqsave);
  15144. +
  15145. +int __lockfunc rt_read_trylock(rwlock_t *rwlock)
  15146. +{
  15147. + struct rt_mutex *lock = &rwlock->lock;
  15148. + int ret = 1;
  15149. +
  15150. + /*
  15151. + * recursive read locks succeed when current owns the lock,
  15152. + * but not when read_depth == 0 which means that the lock is
  15153. + * write locked.
  15154. + */
  15155. + if (rt_mutex_owner(lock) != current) {
  15156. + migrate_disable();
  15157. + ret = rt_mutex_trylock(lock);
  15158. + if (ret)
  15159. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  15160. + else
  15161. + migrate_enable();
  15162. +
  15163. + } else if (!rwlock->read_depth) {
  15164. + ret = 0;
  15165. + }
  15166. +
  15167. + if (ret)
  15168. + rwlock->read_depth++;
  15169. +
  15170. + return ret;
  15171. +}
  15172. +EXPORT_SYMBOL(rt_read_trylock);
  15173. +
  15174. +void __lockfunc rt_write_lock(rwlock_t *rwlock)
  15175. +{
  15176. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  15177. + migrate_disable();
  15178. + __rt_spin_lock(&rwlock->lock);
  15179. +}
  15180. +EXPORT_SYMBOL(rt_write_lock);
  15181. +
  15182. +void __lockfunc rt_read_lock(rwlock_t *rwlock)
  15183. +{
  15184. + struct rt_mutex *lock = &rwlock->lock;
  15185. +
  15186. +
  15187. + /*
  15188. + * recursive read locks succeed when current owns the lock
  15189. + */
  15190. + if (rt_mutex_owner(lock) != current) {
  15191. + migrate_disable();
  15192. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  15193. + __rt_spin_lock(lock);
  15194. + }
  15195. + rwlock->read_depth++;
  15196. +}
  15197. +
  15198. +EXPORT_SYMBOL(rt_read_lock);
  15199. +
  15200. +void __lockfunc rt_write_unlock(rwlock_t *rwlock)
  15201. +{
  15202. + /* NOTE: we always pass in '1' for nested, for simplicity */
  15203. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  15204. + __rt_spin_unlock(&rwlock->lock);
  15205. + migrate_enable();
  15206. +}
  15207. +EXPORT_SYMBOL(rt_write_unlock);
  15208. +
  15209. +void __lockfunc rt_read_unlock(rwlock_t *rwlock)
  15210. +{
  15211. + /* Release the lock only when read_depth is down to 0 */
  15212. + if (--rwlock->read_depth == 0) {
  15213. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  15214. + __rt_spin_unlock(&rwlock->lock);
  15215. + migrate_enable();
  15216. + }
  15217. +}
  15218. +EXPORT_SYMBOL(rt_read_unlock);
  15219. +
  15220. +unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock)
  15221. +{
  15222. + rt_write_lock(rwlock);
  15223. +
  15224. + return 0;
  15225. +}
  15226. +EXPORT_SYMBOL(rt_write_lock_irqsave);
  15227. +
  15228. +unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock)
  15229. +{
  15230. + rt_read_lock(rwlock);
  15231. +
  15232. + return 0;
  15233. +}
  15234. +EXPORT_SYMBOL(rt_read_lock_irqsave);
  15235. +
  15236. +void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key)
  15237. +{
  15238. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  15239. + /*
  15240. + * Make sure we are not reinitializing a held lock:
  15241. + */
  15242. + debug_check_no_locks_freed((void *)rwlock, sizeof(*rwlock));
  15243. + lockdep_init_map(&rwlock->dep_map, name, key, 0);
  15244. +#endif
  15245. + rwlock->lock.save_state = 1;
  15246. + rwlock->read_depth = 0;
  15247. +}
  15248. +EXPORT_SYMBOL(__rt_rwlock_init);
  15249. +
  15250. +/*
  15251. + * rw_semaphores
  15252. + */
  15253. +
  15254. +void rt_up_write(struct rw_semaphore *rwsem)
  15255. +{
  15256. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  15257. + rt_mutex_unlock(&rwsem->lock);
  15258. +}
  15259. +EXPORT_SYMBOL(rt_up_write);
  15260. +
  15261. +void __rt_up_read(struct rw_semaphore *rwsem)
  15262. +{
  15263. + if (--rwsem->read_depth == 0)
  15264. + rt_mutex_unlock(&rwsem->lock);
  15265. +}
  15266. +
  15267. +void rt_up_read(struct rw_semaphore *rwsem)
  15268. +{
  15269. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  15270. + __rt_up_read(rwsem);
  15271. +}
  15272. +EXPORT_SYMBOL(rt_up_read);
  15273. +
  15274. +/*
  15275. + * downgrade a write lock into a read lock
  15276. + * - just wake up any readers at the front of the queue
  15277. + */
  15278. +void rt_downgrade_write(struct rw_semaphore *rwsem)
  15279. +{
  15280. + BUG_ON(rt_mutex_owner(&rwsem->lock) != current);
  15281. + rwsem->read_depth = 1;
  15282. +}
  15283. +EXPORT_SYMBOL(rt_downgrade_write);
  15284. +
  15285. +int rt_down_write_trylock(struct rw_semaphore *rwsem)
  15286. +{
  15287. + int ret = rt_mutex_trylock(&rwsem->lock);
  15288. +
  15289. + if (ret)
  15290. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  15291. + return ret;
  15292. +}
  15293. +EXPORT_SYMBOL(rt_down_write_trylock);
  15294. +
  15295. +void rt_down_write(struct rw_semaphore *rwsem)
  15296. +{
  15297. + rwsem_acquire(&rwsem->dep_map, 0, 0, _RET_IP_);
  15298. + rt_mutex_lock(&rwsem->lock);
  15299. +}
  15300. +EXPORT_SYMBOL(rt_down_write);
  15301. +
  15302. +void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass)
  15303. +{
  15304. + rwsem_acquire(&rwsem->dep_map, subclass, 0, _RET_IP_);
  15305. + rt_mutex_lock(&rwsem->lock);
  15306. +}
  15307. +EXPORT_SYMBOL(rt_down_write_nested);
  15308. +
  15309. +void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  15310. + struct lockdep_map *nest)
  15311. +{
  15312. + rwsem_acquire_nest(&rwsem->dep_map, 0, 0, nest, _RET_IP_);
  15313. + rt_mutex_lock(&rwsem->lock);
  15314. +}
  15315. +EXPORT_SYMBOL(rt_down_write_nested_lock);
  15316. +
  15317. +int rt_down_read_trylock(struct rw_semaphore *rwsem)
  15318. +{
  15319. + struct rt_mutex *lock = &rwsem->lock;
  15320. + int ret = 1;
  15321. +
  15322. + /*
  15323. + * recursive read locks succeed when current owns the rwsem,
  15324. + * but not when read_depth == 0 which means that the rwsem is
  15325. + * write locked.
  15326. + */
  15327. + if (rt_mutex_owner(lock) != current)
  15328. + ret = rt_mutex_trylock(&rwsem->lock);
  15329. + else if (!rwsem->read_depth)
  15330. + ret = 0;
  15331. +
  15332. + if (ret) {
  15333. + rwsem->read_depth++;
  15334. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  15335. + }
  15336. + return ret;
  15337. +}
  15338. +EXPORT_SYMBOL(rt_down_read_trylock);
  15339. +
  15340. +static void __rt_down_read(struct rw_semaphore *rwsem, int subclass)
  15341. +{
  15342. + struct rt_mutex *lock = &rwsem->lock;
  15343. +
  15344. + rwsem_acquire_read(&rwsem->dep_map, subclass, 0, _RET_IP_);
  15345. +
  15346. + if (rt_mutex_owner(lock) != current)
  15347. + rt_mutex_lock(&rwsem->lock);
  15348. + rwsem->read_depth++;
  15349. +}
  15350. +
  15351. +void rt_down_read(struct rw_semaphore *rwsem)
  15352. +{
  15353. + __rt_down_read(rwsem, 0);
  15354. +}
  15355. +EXPORT_SYMBOL(rt_down_read);
  15356. +
  15357. +void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass)
  15358. +{
  15359. + __rt_down_read(rwsem, subclass);
  15360. +}
  15361. +EXPORT_SYMBOL(rt_down_read_nested);
  15362. +
  15363. +void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  15364. + struct lock_class_key *key)
  15365. +{
  15366. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  15367. + /*
  15368. + * Make sure we are not reinitializing a held lock:
  15369. + */
  15370. + debug_check_no_locks_freed((void *)rwsem, sizeof(*rwsem));
  15371. + lockdep_init_map(&rwsem->dep_map, name, key, 0);
  15372. +#endif
  15373. + rwsem->read_depth = 0;
  15374. + rwsem->lock.save_state = 0;
  15375. +}
  15376. +EXPORT_SYMBOL(__rt_rwsem_init);
  15377. +
  15378. +/**
  15379. + * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
  15380. + * @cnt: the atomic which we are to dec
  15381. + * @lock: the mutex to return holding if we dec to 0
  15382. + *
  15383. + * return true and hold lock if we dec to 0, return false otherwise
  15384. + */
  15385. +int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
  15386. +{
  15387. + /* dec if we can't possibly hit 0 */
  15388. + if (atomic_add_unless(cnt, -1, 1))
  15389. + return 0;
  15390. + /* we might hit 0, so take the lock */
  15391. + mutex_lock(lock);
  15392. + if (!atomic_dec_and_test(cnt)) {
  15393. + /* when we actually did the dec, we didn't hit 0 */
  15394. + mutex_unlock(lock);
  15395. + return 0;
  15396. + }
  15397. + /* we hit 0, and we hold the lock */
  15398. + return 1;
  15399. +}
  15400. +EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
  15401. diff -Nur linux-4.1.20.orig/kernel/locking/rtmutex.c linux-4.1.20/kernel/locking/rtmutex.c
  15402. --- linux-4.1.20.orig/kernel/locking/rtmutex.c 2016-03-17 19:11:03.000000000 +0100
  15403. +++ linux-4.1.20/kernel/locking/rtmutex.c 2016-03-21 20:18:33.000000000 +0100
  15404. @@ -7,6 +7,11 @@
  15405. * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  15406. * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
  15407. * Copyright (C) 2006 Esben Nielsen
  15408. + * Adaptive Spinlocks:
  15409. + * Copyright (C) 2008 Novell, Inc., Gregory Haskins, Sven Dietrich,
  15410. + * and Peter Morreale,
  15411. + * Adaptive Spinlocks simplification:
  15412. + * Copyright (C) 2008 Red Hat, Inc., Steven Rostedt <srostedt@redhat.com>
  15413. *
  15414. * See Documentation/locking/rt-mutex-design.txt for details.
  15415. */
  15416. @@ -16,6 +21,7 @@
  15417. #include <linux/sched/rt.h>
  15418. #include <linux/sched/deadline.h>
  15419. #include <linux/timer.h>
  15420. +#include <linux/ww_mutex.h>
  15421. #include "rtmutex_common.h"
  15422. @@ -69,6 +75,12 @@
  15423. clear_rt_mutex_waiters(lock);
  15424. }
  15425. +static int rt_mutex_real_waiter(struct rt_mutex_waiter *waiter)
  15426. +{
  15427. + return waiter && waiter != PI_WAKEUP_INPROGRESS &&
  15428. + waiter != PI_REQUEUE_INPROGRESS;
  15429. +}
  15430. +
  15431. /*
  15432. * We can speed up the acquire/release, if the architecture
  15433. * supports cmpxchg and if there's no debugging state to be set up
  15434. @@ -300,7 +312,7 @@
  15435. * of task. We do not use the spin_xx_mutex() variants here as we are
  15436. * outside of the debug path.)
  15437. */
  15438. -static void rt_mutex_adjust_prio(struct task_struct *task)
  15439. +void rt_mutex_adjust_prio(struct task_struct *task)
  15440. {
  15441. unsigned long flags;
  15442. @@ -335,6 +347,14 @@
  15443. return debug_rt_mutex_detect_deadlock(waiter, chwalk);
  15444. }
  15445. +static void rt_mutex_wake_waiter(struct rt_mutex_waiter *waiter)
  15446. +{
  15447. + if (waiter->savestate)
  15448. + wake_up_lock_sleeper(waiter->task);
  15449. + else
  15450. + wake_up_process(waiter->task);
  15451. +}
  15452. +
  15453. /*
  15454. * Max number of times we'll walk the boosting chain:
  15455. */
  15456. @@ -342,7 +362,8 @@
  15457. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  15458. {
  15459. - return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  15460. + return rt_mutex_real_waiter(p->pi_blocked_on) ?
  15461. + p->pi_blocked_on->lock : NULL;
  15462. }
  15463. /*
  15464. @@ -479,7 +500,7 @@
  15465. * reached or the state of the chain has changed while we
  15466. * dropped the locks.
  15467. */
  15468. - if (!waiter)
  15469. + if (!rt_mutex_real_waiter(waiter))
  15470. goto out_unlock_pi;
  15471. /*
  15472. @@ -641,13 +662,16 @@
  15473. * follow here. This is the end of the chain we are walking.
  15474. */
  15475. if (!rt_mutex_owner(lock)) {
  15476. + struct rt_mutex_waiter *lock_top_waiter;
  15477. +
  15478. /*
  15479. * If the requeue [7] above changed the top waiter,
  15480. * then we need to wake the new top waiter up to try
  15481. * to get the lock.
  15482. */
  15483. - if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
  15484. - wake_up_process(rt_mutex_top_waiter(lock)->task);
  15485. + lock_top_waiter = rt_mutex_top_waiter(lock);
  15486. + if (prerequeue_top_waiter != lock_top_waiter)
  15487. + rt_mutex_wake_waiter(lock_top_waiter);
  15488. raw_spin_unlock(&lock->wait_lock);
  15489. return 0;
  15490. }
  15491. @@ -740,6 +764,25 @@
  15492. return ret;
  15493. }
  15494. +
  15495. +#define STEAL_NORMAL 0
  15496. +#define STEAL_LATERAL 1
  15497. +
  15498. +/*
  15499. + * Note that RT tasks are excluded from lateral-steals to prevent the
  15500. + * introduction of an unbounded latency
  15501. + */
  15502. +static inline int lock_is_stealable(struct task_struct *task,
  15503. + struct task_struct *pendowner, int mode)
  15504. +{
  15505. + if (mode == STEAL_NORMAL || rt_task(task)) {
  15506. + if (task->prio >= pendowner->prio)
  15507. + return 0;
  15508. + } else if (task->prio > pendowner->prio)
  15509. + return 0;
  15510. + return 1;
  15511. +}
  15512. +
  15513. /*
  15514. * Try to take an rt-mutex
  15515. *
  15516. @@ -750,8 +793,9 @@
  15517. * @waiter: The waiter that is queued to the lock's wait list if the
  15518. * callsite called task_blocked_on_lock(), otherwise NULL
  15519. */
  15520. -static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  15521. - struct rt_mutex_waiter *waiter)
  15522. +static int __try_to_take_rt_mutex(struct rt_mutex *lock,
  15523. + struct task_struct *task,
  15524. + struct rt_mutex_waiter *waiter, int mode)
  15525. {
  15526. unsigned long flags;
  15527. @@ -790,8 +834,10 @@
  15528. * If waiter is not the highest priority waiter of
  15529. * @lock, give up.
  15530. */
  15531. - if (waiter != rt_mutex_top_waiter(lock))
  15532. + if (waiter != rt_mutex_top_waiter(lock)) {
  15533. + /* XXX lock_is_stealable() ? */
  15534. return 0;
  15535. + }
  15536. /*
  15537. * We can acquire the lock. Remove the waiter from the
  15538. @@ -809,14 +855,10 @@
  15539. * not need to be dequeued.
  15540. */
  15541. if (rt_mutex_has_waiters(lock)) {
  15542. - /*
  15543. - * If @task->prio is greater than or equal to
  15544. - * the top waiter priority (kernel view),
  15545. - * @task lost.
  15546. - */
  15547. - if (task->prio >= rt_mutex_top_waiter(lock)->prio)
  15548. - return 0;
  15549. + struct task_struct *pown = rt_mutex_top_waiter(lock)->task;
  15550. + if (task != pown && !lock_is_stealable(task, pown, mode))
  15551. + return 0;
  15552. /*
  15553. * The current top waiter stays enqueued. We
  15554. * don't have to change anything in the lock
  15555. @@ -865,6 +907,347 @@
  15556. return 1;
  15557. }
  15558. +#ifdef CONFIG_PREEMPT_RT_FULL
  15559. +/*
  15560. + * preemptible spin_lock functions:
  15561. + */
  15562. +static inline void rt_spin_lock_fastlock(struct rt_mutex *lock,
  15563. + void (*slowfn)(struct rt_mutex *lock))
  15564. +{
  15565. + might_sleep_no_state_check();
  15566. +
  15567. + if (likely(rt_mutex_cmpxchg(lock, NULL, current)))
  15568. + rt_mutex_deadlock_account_lock(lock, current);
  15569. + else
  15570. + slowfn(lock);
  15571. +}
  15572. +
  15573. +static inline void rt_spin_lock_fastunlock(struct rt_mutex *lock,
  15574. + void (*slowfn)(struct rt_mutex *lock))
  15575. +{
  15576. + if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  15577. + rt_mutex_deadlock_account_unlock(current);
  15578. + else
  15579. + slowfn(lock);
  15580. +}
  15581. +#ifdef CONFIG_SMP
  15582. +/*
  15583. + * Note that owner is a speculative pointer and dereferencing relies
  15584. + * on rcu_read_lock() and the check against the lock owner.
  15585. + */
  15586. +static int adaptive_wait(struct rt_mutex *lock,
  15587. + struct task_struct *owner)
  15588. +{
  15589. + int res = 0;
  15590. +
  15591. + rcu_read_lock();
  15592. + for (;;) {
  15593. + if (owner != rt_mutex_owner(lock))
  15594. + break;
  15595. + /*
  15596. + * Ensure that owner->on_cpu is dereferenced _after_
  15597. + * checking the above to be valid.
  15598. + */
  15599. + barrier();
  15600. + if (!owner->on_cpu) {
  15601. + res = 1;
  15602. + break;
  15603. + }
  15604. + cpu_relax();
  15605. + }
  15606. + rcu_read_unlock();
  15607. + return res;
  15608. +}
  15609. +#else
  15610. +static int adaptive_wait(struct rt_mutex *lock,
  15611. + struct task_struct *orig_owner)
  15612. +{
  15613. + return 1;
  15614. +}
  15615. +#endif
  15616. +
  15617. +# define pi_lock(lock) raw_spin_lock_irq(lock)
  15618. +# define pi_unlock(lock) raw_spin_unlock_irq(lock)
  15619. +
  15620. +static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  15621. + struct rt_mutex_waiter *waiter,
  15622. + struct task_struct *task,
  15623. + enum rtmutex_chainwalk chwalk);
  15624. +/*
  15625. + * Slow path lock function spin_lock style: this variant is very
  15626. + * careful not to miss any non-lock wakeups.
  15627. + *
  15628. + * We store the current state under p->pi_lock in p->saved_state and
  15629. + * the try_to_wake_up() code handles this accordingly.
  15630. + */
  15631. +static void noinline __sched rt_spin_lock_slowlock(struct rt_mutex *lock)
  15632. +{
  15633. + struct task_struct *lock_owner, *self = current;
  15634. + struct rt_mutex_waiter waiter, *top_waiter;
  15635. + int ret;
  15636. +
  15637. + rt_mutex_init_waiter(&waiter, true);
  15638. +
  15639. + raw_spin_lock(&lock->wait_lock);
  15640. +
  15641. + if (__try_to_take_rt_mutex(lock, self, NULL, STEAL_LATERAL)) {
  15642. + raw_spin_unlock(&lock->wait_lock);
  15643. + return;
  15644. + }
  15645. +
  15646. + BUG_ON(rt_mutex_owner(lock) == self);
  15647. +
  15648. + /*
  15649. + * We save whatever state the task is in and we'll restore it
  15650. + * after acquiring the lock taking real wakeups into account
  15651. + * as well. We are serialized via pi_lock against wakeups. See
  15652. + * try_to_wake_up().
  15653. + */
  15654. + pi_lock(&self->pi_lock);
  15655. + self->saved_state = self->state;
  15656. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  15657. + pi_unlock(&self->pi_lock);
  15658. +
  15659. + ret = task_blocks_on_rt_mutex(lock, &waiter, self, RT_MUTEX_MIN_CHAINWALK);
  15660. + BUG_ON(ret);
  15661. +
  15662. + for (;;) {
  15663. + /* Try to acquire the lock again. */
  15664. + if (__try_to_take_rt_mutex(lock, self, &waiter, STEAL_LATERAL))
  15665. + break;
  15666. +
  15667. + top_waiter = rt_mutex_top_waiter(lock);
  15668. + lock_owner = rt_mutex_owner(lock);
  15669. +
  15670. + raw_spin_unlock(&lock->wait_lock);
  15671. +
  15672. + debug_rt_mutex_print_deadlock(&waiter);
  15673. +
  15674. + if (top_waiter != &waiter || adaptive_wait(lock, lock_owner))
  15675. + schedule_rt_mutex(lock);
  15676. +
  15677. + raw_spin_lock(&lock->wait_lock);
  15678. +
  15679. + pi_lock(&self->pi_lock);
  15680. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  15681. + pi_unlock(&self->pi_lock);
  15682. + }
  15683. +
  15684. + /*
  15685. + * Restore the task state to current->saved_state. We set it
  15686. + * to the original state above and the try_to_wake_up() code
  15687. + * has possibly updated it when a real (non-rtmutex) wakeup
  15688. + * happened while we were blocked. Clear saved_state so
  15689. + * try_to_wakeup() does not get confused.
  15690. + */
  15691. + pi_lock(&self->pi_lock);
  15692. + __set_current_state_no_track(self->saved_state);
  15693. + self->saved_state = TASK_RUNNING;
  15694. + pi_unlock(&self->pi_lock);
  15695. +
  15696. + /*
  15697. + * try_to_take_rt_mutex() sets the waiter bit
  15698. + * unconditionally. We might have to fix that up:
  15699. + */
  15700. + fixup_rt_mutex_waiters(lock);
  15701. +
  15702. + BUG_ON(rt_mutex_has_waiters(lock) && &waiter == rt_mutex_top_waiter(lock));
  15703. + BUG_ON(!RB_EMPTY_NODE(&waiter.tree_entry));
  15704. +
  15705. + raw_spin_unlock(&lock->wait_lock);
  15706. +
  15707. + debug_rt_mutex_free_waiter(&waiter);
  15708. +}
  15709. +
  15710. +static void wakeup_next_waiter(struct rt_mutex *lock);
  15711. +/*
  15712. + * Slow path to release a rt_mutex spin_lock style
  15713. + */
  15714. +static void noinline __sched rt_spin_lock_slowunlock(struct rt_mutex *lock)
  15715. +{
  15716. + raw_spin_lock(&lock->wait_lock);
  15717. +
  15718. + debug_rt_mutex_unlock(lock);
  15719. +
  15720. + rt_mutex_deadlock_account_unlock(current);
  15721. +
  15722. + if (!rt_mutex_has_waiters(lock)) {
  15723. + lock->owner = NULL;
  15724. + raw_spin_unlock(&lock->wait_lock);
  15725. + return;
  15726. + }
  15727. +
  15728. + wakeup_next_waiter(lock);
  15729. +
  15730. + raw_spin_unlock(&lock->wait_lock);
  15731. +
  15732. + /* Undo pi boosting.when necessary */
  15733. + rt_mutex_adjust_prio(current);
  15734. +}
  15735. +
  15736. +void __lockfunc rt_spin_lock(spinlock_t *lock)
  15737. +{
  15738. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock);
  15739. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  15740. +}
  15741. +EXPORT_SYMBOL(rt_spin_lock);
  15742. +
  15743. +void __lockfunc __rt_spin_lock(struct rt_mutex *lock)
  15744. +{
  15745. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock);
  15746. +}
  15747. +EXPORT_SYMBOL(__rt_spin_lock);
  15748. +
  15749. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  15750. +void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass)
  15751. +{
  15752. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock);
  15753. + spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  15754. +}
  15755. +EXPORT_SYMBOL(rt_spin_lock_nested);
  15756. +#endif
  15757. +
  15758. +void __lockfunc rt_spin_unlock(spinlock_t *lock)
  15759. +{
  15760. + /* NOTE: we always pass in '1' for nested, for simplicity */
  15761. + spin_release(&lock->dep_map, 1, _RET_IP_);
  15762. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  15763. +}
  15764. +EXPORT_SYMBOL(rt_spin_unlock);
  15765. +
  15766. +void __lockfunc __rt_spin_unlock(struct rt_mutex *lock)
  15767. +{
  15768. + rt_spin_lock_fastunlock(lock, rt_spin_lock_slowunlock);
  15769. +}
  15770. +EXPORT_SYMBOL(__rt_spin_unlock);
  15771. +
  15772. +/*
  15773. + * Wait for the lock to get unlocked: instead of polling for an unlock
  15774. + * (like raw spinlocks do), we lock and unlock, to force the kernel to
  15775. + * schedule if there's contention:
  15776. + */
  15777. +void __lockfunc rt_spin_unlock_wait(spinlock_t *lock)
  15778. +{
  15779. + spin_lock(lock);
  15780. + spin_unlock(lock);
  15781. +}
  15782. +EXPORT_SYMBOL(rt_spin_unlock_wait);
  15783. +
  15784. +int __lockfunc __rt_spin_trylock(struct rt_mutex *lock)
  15785. +{
  15786. + return rt_mutex_trylock(lock);
  15787. +}
  15788. +
  15789. +int __lockfunc rt_spin_trylock(spinlock_t *lock)
  15790. +{
  15791. + int ret = rt_mutex_trylock(&lock->lock);
  15792. +
  15793. + if (ret)
  15794. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  15795. + return ret;
  15796. +}
  15797. +EXPORT_SYMBOL(rt_spin_trylock);
  15798. +
  15799. +int __lockfunc rt_spin_trylock_bh(spinlock_t *lock)
  15800. +{
  15801. + int ret;
  15802. +
  15803. + local_bh_disable();
  15804. + ret = rt_mutex_trylock(&lock->lock);
  15805. + if (ret) {
  15806. + migrate_disable();
  15807. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  15808. + } else
  15809. + local_bh_enable();
  15810. + return ret;
  15811. +}
  15812. +EXPORT_SYMBOL(rt_spin_trylock_bh);
  15813. +
  15814. +int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags)
  15815. +{
  15816. + int ret;
  15817. +
  15818. + *flags = 0;
  15819. + ret = rt_mutex_trylock(&lock->lock);
  15820. + if (ret) {
  15821. + migrate_disable();
  15822. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  15823. + }
  15824. + return ret;
  15825. +}
  15826. +EXPORT_SYMBOL(rt_spin_trylock_irqsave);
  15827. +
  15828. +int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock)
  15829. +{
  15830. + /* Subtract 1 from counter unless that drops it to 0 (ie. it was 1) */
  15831. + if (atomic_add_unless(atomic, -1, 1))
  15832. + return 0;
  15833. + migrate_disable();
  15834. + rt_spin_lock(lock);
  15835. + if (atomic_dec_and_test(atomic))
  15836. + return 1;
  15837. + rt_spin_unlock(lock);
  15838. + migrate_enable();
  15839. + return 0;
  15840. +}
  15841. +EXPORT_SYMBOL(atomic_dec_and_spin_lock);
  15842. +
  15843. + void
  15844. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key)
  15845. +{
  15846. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  15847. + /*
  15848. + * Make sure we are not reinitializing a held lock:
  15849. + */
  15850. + debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  15851. + lockdep_init_map(&lock->dep_map, name, key, 0);
  15852. +#endif
  15853. +}
  15854. +EXPORT_SYMBOL(__rt_spin_lock_init);
  15855. +
  15856. +#endif /* PREEMPT_RT_FULL */
  15857. +
  15858. +#ifdef CONFIG_PREEMPT_RT_FULL
  15859. + static inline int __sched
  15860. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  15861. +{
  15862. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  15863. + struct ww_acquire_ctx *hold_ctx = ACCESS_ONCE(ww->ctx);
  15864. +
  15865. + if (!hold_ctx)
  15866. + return 0;
  15867. +
  15868. + if (unlikely(ctx == hold_ctx))
  15869. + return -EALREADY;
  15870. +
  15871. + if (ctx->stamp - hold_ctx->stamp <= LONG_MAX &&
  15872. + (ctx->stamp != hold_ctx->stamp || ctx > hold_ctx)) {
  15873. +#ifdef CONFIG_DEBUG_MUTEXES
  15874. + DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
  15875. + ctx->contending_lock = ww;
  15876. +#endif
  15877. + return -EDEADLK;
  15878. + }
  15879. +
  15880. + return 0;
  15881. +}
  15882. +#else
  15883. + static inline int __sched
  15884. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  15885. +{
  15886. + BUG();
  15887. + return 0;
  15888. +}
  15889. +
  15890. +#endif
  15891. +
  15892. +static inline int
  15893. +try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  15894. + struct rt_mutex_waiter *waiter)
  15895. +{
  15896. + return __try_to_take_rt_mutex(lock, task, waiter, STEAL_NORMAL);
  15897. +}
  15898. +
  15899. /*
  15900. * Task blocks on lock.
  15901. *
  15902. @@ -896,6 +1279,23 @@
  15903. return -EDEADLK;
  15904. raw_spin_lock_irqsave(&task->pi_lock, flags);
  15905. +
  15906. + /*
  15907. + * In the case of futex requeue PI, this will be a proxy
  15908. + * lock. The task will wake unaware that it is enqueueed on
  15909. + * this lock. Avoid blocking on two locks and corrupting
  15910. + * pi_blocked_on via the PI_WAKEUP_INPROGRESS
  15911. + * flag. futex_wait_requeue_pi() sets this when it wakes up
  15912. + * before requeue (due to a signal or timeout). Do not enqueue
  15913. + * the task if PI_WAKEUP_INPROGRESS is set.
  15914. + */
  15915. + if (task != current && task->pi_blocked_on == PI_WAKEUP_INPROGRESS) {
  15916. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  15917. + return -EAGAIN;
  15918. + }
  15919. +
  15920. + BUG_ON(rt_mutex_real_waiter(task->pi_blocked_on));
  15921. +
  15922. __rt_mutex_adjust_prio(task);
  15923. waiter->task = task;
  15924. waiter->lock = lock;
  15925. @@ -919,7 +1319,7 @@
  15926. rt_mutex_enqueue_pi(owner, waiter);
  15927. __rt_mutex_adjust_prio(owner);
  15928. - if (owner->pi_blocked_on)
  15929. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  15930. chain_walk = 1;
  15931. } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  15932. chain_walk = 1;
  15933. @@ -957,8 +1357,9 @@
  15934. /*
  15935. * Wake up the next waiter on the lock.
  15936. *
  15937. - * Remove the top waiter from the current tasks pi waiter list and
  15938. - * wake it up.
  15939. + * Remove the top waiter from the current tasks pi waiter list,
  15940. + * wake it up and return whether the current task needs to undo
  15941. + * a potential priority boosting.
  15942. *
  15943. * Called with lock->wait_lock held.
  15944. */
  15945. @@ -996,7 +1397,7 @@
  15946. * long as we hold lock->wait_lock. The waiter task needs to
  15947. * acquire it in order to dequeue the waiter.
  15948. */
  15949. - wake_up_process(waiter->task);
  15950. + rt_mutex_wake_waiter(waiter);
  15951. }
  15952. /*
  15953. @@ -1010,7 +1411,7 @@
  15954. {
  15955. bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  15956. struct task_struct *owner = rt_mutex_owner(lock);
  15957. - struct rt_mutex *next_lock;
  15958. + struct rt_mutex *next_lock = NULL;
  15959. unsigned long flags;
  15960. raw_spin_lock_irqsave(&current->pi_lock, flags);
  15961. @@ -1035,7 +1436,8 @@
  15962. __rt_mutex_adjust_prio(owner);
  15963. /* Store the lock on which owner is blocked or NULL */
  15964. - next_lock = task_blocked_on_lock(owner);
  15965. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  15966. + next_lock = task_blocked_on_lock(owner);
  15967. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  15968. @@ -1071,17 +1473,17 @@
  15969. raw_spin_lock_irqsave(&task->pi_lock, flags);
  15970. waiter = task->pi_blocked_on;
  15971. - if (!waiter || (waiter->prio == task->prio &&
  15972. + if (!rt_mutex_real_waiter(waiter) || (waiter->prio == task->prio &&
  15973. !dl_prio(task->prio))) {
  15974. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  15975. return;
  15976. }
  15977. next_lock = waiter->lock;
  15978. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  15979. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  15980. get_task_struct(task);
  15981. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  15982. rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
  15983. next_lock, NULL, task);
  15984. }
  15985. @@ -1099,7 +1501,8 @@
  15986. static int __sched
  15987. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  15988. struct hrtimer_sleeper *timeout,
  15989. - struct rt_mutex_waiter *waiter)
  15990. + struct rt_mutex_waiter *waiter,
  15991. + struct ww_acquire_ctx *ww_ctx)
  15992. {
  15993. int ret = 0;
  15994. @@ -1122,6 +1525,12 @@
  15995. break;
  15996. }
  15997. + if (ww_ctx && ww_ctx->acquired > 0) {
  15998. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  15999. + if (ret)
  16000. + break;
  16001. + }
  16002. +
  16003. raw_spin_unlock(&lock->wait_lock);
  16004. debug_rt_mutex_print_deadlock(waiter);
  16005. @@ -1156,25 +1565,102 @@
  16006. }
  16007. }
  16008. +static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww,
  16009. + struct ww_acquire_ctx *ww_ctx)
  16010. +{
  16011. +#ifdef CONFIG_DEBUG_MUTEXES
  16012. + /*
  16013. + * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
  16014. + * but released with a normal mutex_unlock in this call.
  16015. + *
  16016. + * This should never happen, always use ww_mutex_unlock.
  16017. + */
  16018. + DEBUG_LOCKS_WARN_ON(ww->ctx);
  16019. +
  16020. + /*
  16021. + * Not quite done after calling ww_acquire_done() ?
  16022. + */
  16023. + DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
  16024. +
  16025. + if (ww_ctx->contending_lock) {
  16026. + /*
  16027. + * After -EDEADLK you tried to
  16028. + * acquire a different ww_mutex? Bad!
  16029. + */
  16030. + DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
  16031. +
  16032. + /*
  16033. + * You called ww_mutex_lock after receiving -EDEADLK,
  16034. + * but 'forgot' to unlock everything else first?
  16035. + */
  16036. + DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
  16037. + ww_ctx->contending_lock = NULL;
  16038. + }
  16039. +
  16040. + /*
  16041. + * Naughty, using a different class will lead to undefined behavior!
  16042. + */
  16043. + DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
  16044. +#endif
  16045. + ww_ctx->acquired++;
  16046. +}
  16047. +
  16048. +#ifdef CONFIG_PREEMPT_RT_FULL
  16049. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  16050. + struct ww_acquire_ctx *ww_ctx)
  16051. +{
  16052. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  16053. + struct rt_mutex_waiter *waiter, *n;
  16054. +
  16055. + /*
  16056. + * This branch gets optimized out for the common case,
  16057. + * and is only important for ww_mutex_lock.
  16058. + */
  16059. + ww_mutex_lock_acquired(ww, ww_ctx);
  16060. + ww->ctx = ww_ctx;
  16061. +
  16062. + /*
  16063. + * Give any possible sleeping processes the chance to wake up,
  16064. + * so they can recheck if they have to back off.
  16065. + */
  16066. + rbtree_postorder_for_each_entry_safe(waiter, n, &lock->waiters,
  16067. + tree_entry) {
  16068. + /* XXX debug rt mutex waiter wakeup */
  16069. +
  16070. + BUG_ON(waiter->lock != lock);
  16071. + rt_mutex_wake_waiter(waiter);
  16072. + }
  16073. +}
  16074. +
  16075. +#else
  16076. +
  16077. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  16078. + struct ww_acquire_ctx *ww_ctx)
  16079. +{
  16080. + BUG();
  16081. +}
  16082. +#endif
  16083. +
  16084. /*
  16085. * Slow path lock function:
  16086. */
  16087. static int __sched
  16088. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  16089. struct hrtimer_sleeper *timeout,
  16090. - enum rtmutex_chainwalk chwalk)
  16091. + enum rtmutex_chainwalk chwalk,
  16092. + struct ww_acquire_ctx *ww_ctx)
  16093. {
  16094. struct rt_mutex_waiter waiter;
  16095. int ret = 0;
  16096. - debug_rt_mutex_init_waiter(&waiter);
  16097. - RB_CLEAR_NODE(&waiter.pi_tree_entry);
  16098. - RB_CLEAR_NODE(&waiter.tree_entry);
  16099. + rt_mutex_init_waiter(&waiter, false);
  16100. raw_spin_lock(&lock->wait_lock);
  16101. /* Try to acquire the lock again: */
  16102. if (try_to_take_rt_mutex(lock, current, NULL)) {
  16103. + if (ww_ctx)
  16104. + ww_mutex_account_lock(lock, ww_ctx);
  16105. raw_spin_unlock(&lock->wait_lock);
  16106. return 0;
  16107. }
  16108. @@ -1192,13 +1678,23 @@
  16109. if (likely(!ret))
  16110. /* sleep on the mutex */
  16111. - ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  16112. + ret = __rt_mutex_slowlock(lock, state, timeout, &waiter,
  16113. + ww_ctx);
  16114. + else if (ww_ctx) {
  16115. + /* ww_mutex received EDEADLK, let it become EALREADY */
  16116. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  16117. + BUG_ON(!ret);
  16118. + }
  16119. if (unlikely(ret)) {
  16120. __set_current_state(TASK_RUNNING);
  16121. if (rt_mutex_has_waiters(lock))
  16122. remove_waiter(lock, &waiter);
  16123. - rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  16124. + /* ww_mutex want to report EDEADLK/EALREADY, let them */
  16125. + if (!ww_ctx)
  16126. + rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  16127. + } else if (ww_ctx) {
  16128. + ww_mutex_account_lock(lock, ww_ctx);
  16129. }
  16130. /*
  16131. @@ -1255,7 +1751,7 @@
  16132. /*
  16133. * Slow path to release a rt-mutex:
  16134. */
  16135. -static void __sched
  16136. +static bool __sched
  16137. rt_mutex_slowunlock(struct rt_mutex *lock)
  16138. {
  16139. raw_spin_lock(&lock->wait_lock);
  16140. @@ -1298,7 +1794,7 @@
  16141. while (!rt_mutex_has_waiters(lock)) {
  16142. /* Drops lock->wait_lock ! */
  16143. if (unlock_rt_mutex_safe(lock) == true)
  16144. - return;
  16145. + return false;
  16146. /* Relock the rtmutex and try again */
  16147. raw_spin_lock(&lock->wait_lock);
  16148. }
  16149. @@ -1311,8 +1807,7 @@
  16150. raw_spin_unlock(&lock->wait_lock);
  16151. - /* Undo pi boosting if necessary: */
  16152. - rt_mutex_adjust_prio(current);
  16153. + return true;
  16154. }
  16155. /*
  16156. @@ -1323,31 +1818,36 @@
  16157. */
  16158. static inline int
  16159. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  16160. + struct ww_acquire_ctx *ww_ctx,
  16161. int (*slowfn)(struct rt_mutex *lock, int state,
  16162. struct hrtimer_sleeper *timeout,
  16163. - enum rtmutex_chainwalk chwalk))
  16164. + enum rtmutex_chainwalk chwalk,
  16165. + struct ww_acquire_ctx *ww_ctx))
  16166. {
  16167. if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  16168. rt_mutex_deadlock_account_lock(lock, current);
  16169. return 0;
  16170. } else
  16171. - return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
  16172. + return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK,
  16173. + ww_ctx);
  16174. }
  16175. static inline int
  16176. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  16177. struct hrtimer_sleeper *timeout,
  16178. enum rtmutex_chainwalk chwalk,
  16179. + struct ww_acquire_ctx *ww_ctx,
  16180. int (*slowfn)(struct rt_mutex *lock, int state,
  16181. struct hrtimer_sleeper *timeout,
  16182. - enum rtmutex_chainwalk chwalk))
  16183. + enum rtmutex_chainwalk chwalk,
  16184. + struct ww_acquire_ctx *ww_ctx))
  16185. {
  16186. if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
  16187. likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  16188. rt_mutex_deadlock_account_lock(lock, current);
  16189. return 0;
  16190. } else
  16191. - return slowfn(lock, state, timeout, chwalk);
  16192. + return slowfn(lock, state, timeout, chwalk, ww_ctx);
  16193. }
  16194. static inline int
  16195. @@ -1363,12 +1863,14 @@
  16196. static inline void
  16197. rt_mutex_fastunlock(struct rt_mutex *lock,
  16198. - void (*slowfn)(struct rt_mutex *lock))
  16199. + bool (*slowfn)(struct rt_mutex *lock))
  16200. {
  16201. - if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  16202. + if (likely(rt_mutex_cmpxchg(lock, current, NULL))) {
  16203. rt_mutex_deadlock_account_unlock(current);
  16204. - else
  16205. - slowfn(lock);
  16206. + } else if (slowfn(lock)) {
  16207. + /* Undo pi boosting if necessary: */
  16208. + rt_mutex_adjust_prio(current);
  16209. + }
  16210. }
  16211. /**
  16212. @@ -1380,7 +1882,7 @@
  16213. {
  16214. might_sleep();
  16215. - rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
  16216. + rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, NULL, rt_mutex_slowlock);
  16217. }
  16218. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  16219. @@ -1397,7 +1899,7 @@
  16220. {
  16221. might_sleep();
  16222. - return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
  16223. + return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, NULL, rt_mutex_slowlock);
  16224. }
  16225. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  16226. @@ -1410,11 +1912,30 @@
  16227. might_sleep();
  16228. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  16229. - RT_MUTEX_FULL_CHAINWALK,
  16230. + RT_MUTEX_FULL_CHAINWALK, NULL,
  16231. rt_mutex_slowlock);
  16232. }
  16233. /**
  16234. + * rt_mutex_lock_killable - lock a rt_mutex killable
  16235. + *
  16236. + * @lock: the rt_mutex to be locked
  16237. + * @detect_deadlock: deadlock detection on/off
  16238. + *
  16239. + * Returns:
  16240. + * 0 on success
  16241. + * -EINTR when interrupted by a signal
  16242. + * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  16243. + */
  16244. +int __sched rt_mutex_lock_killable(struct rt_mutex *lock)
  16245. +{
  16246. + might_sleep();
  16247. +
  16248. + return rt_mutex_fastlock(lock, TASK_KILLABLE, NULL, rt_mutex_slowlock);
  16249. +}
  16250. +EXPORT_SYMBOL_GPL(rt_mutex_lock_killable);
  16251. +
  16252. +/**
  16253. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  16254. * the timeout structure is provided
  16255. * by the caller
  16256. @@ -1434,6 +1955,7 @@
  16257. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  16258. RT_MUTEX_MIN_CHAINWALK,
  16259. + NULL,
  16260. rt_mutex_slowlock);
  16261. }
  16262. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  16263. @@ -1463,6 +1985,22 @@
  16264. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  16265. /**
  16266. + * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
  16267. + * @lock: the rt_mutex to be unlocked
  16268. + *
  16269. + * Returns: true/false indicating whether priority adjustment is
  16270. + * required or not.
  16271. + */
  16272. +bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
  16273. +{
  16274. + if (likely(rt_mutex_cmpxchg(lock, current, NULL))) {
  16275. + rt_mutex_deadlock_account_unlock(current);
  16276. + return false;
  16277. + }
  16278. + return rt_mutex_slowunlock(lock);
  16279. +}
  16280. +
  16281. +/**
  16282. * rt_mutex_destroy - mark a mutex unusable
  16283. * @lock: the mutex to be destroyed
  16284. *
  16285. @@ -1492,13 +2030,12 @@
  16286. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  16287. {
  16288. lock->owner = NULL;
  16289. - raw_spin_lock_init(&lock->wait_lock);
  16290. lock->waiters = RB_ROOT;
  16291. lock->waiters_leftmost = NULL;
  16292. debug_rt_mutex_init(lock, name);
  16293. }
  16294. -EXPORT_SYMBOL_GPL(__rt_mutex_init);
  16295. +EXPORT_SYMBOL(__rt_mutex_init);
  16296. /**
  16297. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  16298. @@ -1513,7 +2050,7 @@
  16299. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  16300. struct task_struct *proxy_owner)
  16301. {
  16302. - __rt_mutex_init(lock, NULL);
  16303. + rt_mutex_init(lock);
  16304. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  16305. rt_mutex_set_owner(lock, proxy_owner);
  16306. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  16307. @@ -1561,6 +2098,35 @@
  16308. return 1;
  16309. }
  16310. +#ifdef CONFIG_PREEMPT_RT_FULL
  16311. + /*
  16312. + * In PREEMPT_RT there's an added race.
  16313. + * If the task, that we are about to requeue, times out,
  16314. + * it can set the PI_WAKEUP_INPROGRESS. This tells the requeue
  16315. + * to skip this task. But right after the task sets
  16316. + * its pi_blocked_on to PI_WAKEUP_INPROGRESS it can then
  16317. + * block on the spin_lock(&hb->lock), which in RT is an rtmutex.
  16318. + * This will replace the PI_WAKEUP_INPROGRESS with the actual
  16319. + * lock that it blocks on. We *must not* place this task
  16320. + * on this proxy lock in that case.
  16321. + *
  16322. + * To prevent this race, we first take the task's pi_lock
  16323. + * and check if it has updated its pi_blocked_on. If it has,
  16324. + * we assume that it woke up and we return -EAGAIN.
  16325. + * Otherwise, we set the task's pi_blocked_on to
  16326. + * PI_REQUEUE_INPROGRESS, so that if the task is waking up
  16327. + * it will know that we are in the process of requeuing it.
  16328. + */
  16329. + raw_spin_lock_irq(&task->pi_lock);
  16330. + if (task->pi_blocked_on) {
  16331. + raw_spin_unlock_irq(&task->pi_lock);
  16332. + raw_spin_unlock(&lock->wait_lock);
  16333. + return -EAGAIN;
  16334. + }
  16335. + task->pi_blocked_on = PI_REQUEUE_INPROGRESS;
  16336. + raw_spin_unlock_irq(&task->pi_lock);
  16337. +#endif
  16338. +
  16339. /* We enforce deadlock detection for futexes */
  16340. ret = task_blocks_on_rt_mutex(lock, waiter, task,
  16341. RT_MUTEX_FULL_CHAINWALK);
  16342. @@ -1575,7 +2141,7 @@
  16343. ret = 0;
  16344. }
  16345. - if (unlikely(ret))
  16346. + if (ret && rt_mutex_has_waiters(lock))
  16347. remove_waiter(lock, waiter);
  16348. raw_spin_unlock(&lock->wait_lock);
  16349. @@ -1631,7 +2197,7 @@
  16350. set_current_state(TASK_INTERRUPTIBLE);
  16351. /* sleep on the mutex */
  16352. - ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  16353. + ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter, NULL);
  16354. if (unlikely(ret))
  16355. remove_waiter(lock, waiter);
  16356. @@ -1646,3 +2212,89 @@
  16357. return ret;
  16358. }
  16359. +
  16360. +static inline int
  16361. +ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  16362. +{
  16363. +#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  16364. + unsigned tmp;
  16365. +
  16366. + if (ctx->deadlock_inject_countdown-- == 0) {
  16367. + tmp = ctx->deadlock_inject_interval;
  16368. + if (tmp > UINT_MAX/4)
  16369. + tmp = UINT_MAX;
  16370. + else
  16371. + tmp = tmp*2 + tmp + tmp/2;
  16372. +
  16373. + ctx->deadlock_inject_interval = tmp;
  16374. + ctx->deadlock_inject_countdown = tmp;
  16375. + ctx->contending_lock = lock;
  16376. +
  16377. + ww_mutex_unlock(lock);
  16378. +
  16379. + return -EDEADLK;
  16380. + }
  16381. +#endif
  16382. +
  16383. + return 0;
  16384. +}
  16385. +
  16386. +#ifdef CONFIG_PREEMPT_RT_FULL
  16387. +int __sched
  16388. +__ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  16389. +{
  16390. + int ret;
  16391. +
  16392. + might_sleep();
  16393. +
  16394. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  16395. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_INTERRUPTIBLE, NULL, 0, ww_ctx);
  16396. + if (ret)
  16397. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  16398. + else if (!ret && ww_ctx->acquired > 1)
  16399. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  16400. +
  16401. + return ret;
  16402. +}
  16403. +EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible);
  16404. +
  16405. +int __sched
  16406. +__ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  16407. +{
  16408. + int ret;
  16409. +
  16410. + might_sleep();
  16411. +
  16412. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  16413. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_UNINTERRUPTIBLE, NULL, 0, ww_ctx);
  16414. + if (ret)
  16415. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  16416. + else if (!ret && ww_ctx->acquired > 1)
  16417. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  16418. +
  16419. + return ret;
  16420. +}
  16421. +EXPORT_SYMBOL_GPL(__ww_mutex_lock);
  16422. +
  16423. +void __sched ww_mutex_unlock(struct ww_mutex *lock)
  16424. +{
  16425. + int nest = !!lock->ctx;
  16426. +
  16427. + /*
  16428. + * The unlocking fastpath is the 0->1 transition from 'locked'
  16429. + * into 'unlocked' state:
  16430. + */
  16431. + if (nest) {
  16432. +#ifdef CONFIG_DEBUG_MUTEXES
  16433. + DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
  16434. +#endif
  16435. + if (lock->ctx->acquired > 0)
  16436. + lock->ctx->acquired--;
  16437. + lock->ctx = NULL;
  16438. + }
  16439. +
  16440. + mutex_release(&lock->base.dep_map, nest, _RET_IP_);
  16441. + rt_mutex_unlock(&lock->base.lock);
  16442. +}
  16443. +EXPORT_SYMBOL(ww_mutex_unlock);
  16444. +#endif
  16445. diff -Nur linux-4.1.20.orig/kernel/locking/rtmutex_common.h linux-4.1.20/kernel/locking/rtmutex_common.h
  16446. --- linux-4.1.20.orig/kernel/locking/rtmutex_common.h 2016-03-17 19:11:03.000000000 +0100
  16447. +++ linux-4.1.20/kernel/locking/rtmutex_common.h 2016-03-21 20:18:33.000000000 +0100
  16448. @@ -49,6 +49,7 @@
  16449. struct rb_node pi_tree_entry;
  16450. struct task_struct *task;
  16451. struct rt_mutex *lock;
  16452. + bool savestate;
  16453. #ifdef CONFIG_DEBUG_RT_MUTEXES
  16454. unsigned long ip;
  16455. struct pid *deadlock_task_pid;
  16456. @@ -119,6 +120,9 @@
  16457. /*
  16458. * PI-futex support (proxy locking functions, etc.):
  16459. */
  16460. +#define PI_WAKEUP_INPROGRESS ((struct rt_mutex_waiter *) 1)
  16461. +#define PI_REQUEUE_INPROGRESS ((struct rt_mutex_waiter *) 2)
  16462. +
  16463. extern struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock);
  16464. extern void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  16465. struct task_struct *proxy_owner);
  16466. @@ -132,10 +136,24 @@
  16467. struct rt_mutex_waiter *waiter);
  16468. extern int rt_mutex_timed_futex_lock(struct rt_mutex *l, struct hrtimer_sleeper *to);
  16469. +extern bool rt_mutex_futex_unlock(struct rt_mutex *lock);
  16470. +
  16471. +extern void rt_mutex_adjust_prio(struct task_struct *task);
  16472. +
  16473. #ifdef CONFIG_DEBUG_RT_MUTEXES
  16474. # include "rtmutex-debug.h"
  16475. #else
  16476. # include "rtmutex.h"
  16477. #endif
  16478. +static inline void
  16479. +rt_mutex_init_waiter(struct rt_mutex_waiter *waiter, bool savestate)
  16480. +{
  16481. + debug_rt_mutex_init_waiter(waiter);
  16482. + waiter->task = NULL;
  16483. + waiter->savestate = savestate;
  16484. + RB_CLEAR_NODE(&waiter->pi_tree_entry);
  16485. + RB_CLEAR_NODE(&waiter->tree_entry);
  16486. +}
  16487. +
  16488. #endif
  16489. diff -Nur linux-4.1.20.orig/kernel/locking/spinlock.c linux-4.1.20/kernel/locking/spinlock.c
  16490. --- linux-4.1.20.orig/kernel/locking/spinlock.c 2016-03-17 19:11:03.000000000 +0100
  16491. +++ linux-4.1.20/kernel/locking/spinlock.c 2016-03-21 20:18:33.000000000 +0100
  16492. @@ -124,8 +124,11 @@
  16493. * __[spin|read|write]_lock_bh()
  16494. */
  16495. BUILD_LOCK_OPS(spin, raw_spinlock);
  16496. +
  16497. +#ifndef CONFIG_PREEMPT_RT_FULL
  16498. BUILD_LOCK_OPS(read, rwlock);
  16499. BUILD_LOCK_OPS(write, rwlock);
  16500. +#endif
  16501. #endif
  16502. @@ -209,6 +212,8 @@
  16503. EXPORT_SYMBOL(_raw_spin_unlock_bh);
  16504. #endif
  16505. +#ifndef CONFIG_PREEMPT_RT_FULL
  16506. +
  16507. #ifndef CONFIG_INLINE_READ_TRYLOCK
  16508. int __lockfunc _raw_read_trylock(rwlock_t *lock)
  16509. {
  16510. @@ -353,6 +358,8 @@
  16511. EXPORT_SYMBOL(_raw_write_unlock_bh);
  16512. #endif
  16513. +#endif /* !PREEMPT_RT_FULL */
  16514. +
  16515. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  16516. void __lockfunc _raw_spin_lock_nested(raw_spinlock_t *lock, int subclass)
  16517. diff -Nur linux-4.1.20.orig/kernel/locking/spinlock_debug.c linux-4.1.20/kernel/locking/spinlock_debug.c
  16518. --- linux-4.1.20.orig/kernel/locking/spinlock_debug.c 2016-03-17 19:11:03.000000000 +0100
  16519. +++ linux-4.1.20/kernel/locking/spinlock_debug.c 2016-03-21 20:18:33.000000000 +0100
  16520. @@ -31,6 +31,7 @@
  16521. EXPORT_SYMBOL(__raw_spin_lock_init);
  16522. +#ifndef CONFIG_PREEMPT_RT_FULL
  16523. void __rwlock_init(rwlock_t *lock, const char *name,
  16524. struct lock_class_key *key)
  16525. {
  16526. @@ -48,6 +49,7 @@
  16527. }
  16528. EXPORT_SYMBOL(__rwlock_init);
  16529. +#endif
  16530. static void spin_dump(raw_spinlock_t *lock, const char *msg)
  16531. {
  16532. @@ -159,6 +161,7 @@
  16533. arch_spin_unlock(&lock->raw_lock);
  16534. }
  16535. +#ifndef CONFIG_PREEMPT_RT_FULL
  16536. static void rwlock_bug(rwlock_t *lock, const char *msg)
  16537. {
  16538. if (!debug_locks_off())
  16539. @@ -300,3 +303,5 @@
  16540. debug_write_unlock(lock);
  16541. arch_write_unlock(&lock->raw_lock);
  16542. }
  16543. +
  16544. +#endif
  16545. diff -Nur linux-4.1.20.orig/kernel/panic.c linux-4.1.20/kernel/panic.c
  16546. --- linux-4.1.20.orig/kernel/panic.c 2016-03-17 19:11:03.000000000 +0100
  16547. +++ linux-4.1.20/kernel/panic.c 2016-03-21 20:18:33.000000000 +0100
  16548. @@ -399,9 +399,11 @@
  16549. static int init_oops_id(void)
  16550. {
  16551. +#ifndef CONFIG_PREEMPT_RT_FULL
  16552. if (!oops_id)
  16553. get_random_bytes(&oops_id, sizeof(oops_id));
  16554. else
  16555. +#endif
  16556. oops_id++;
  16557. return 0;
  16558. diff -Nur linux-4.1.20.orig/kernel/power/hibernate.c linux-4.1.20/kernel/power/hibernate.c
  16559. --- linux-4.1.20.orig/kernel/power/hibernate.c 2016-03-17 19:11:03.000000000 +0100
  16560. +++ linux-4.1.20/kernel/power/hibernate.c 2016-03-21 20:18:33.000000000 +0100
  16561. @@ -285,6 +285,8 @@
  16562. local_irq_disable();
  16563. + system_state = SYSTEM_SUSPEND;
  16564. +
  16565. error = syscore_suspend();
  16566. if (error) {
  16567. printk(KERN_ERR "PM: Some system devices failed to power down, "
  16568. @@ -314,6 +316,7 @@
  16569. syscore_resume();
  16570. Enable_irqs:
  16571. + system_state = SYSTEM_RUNNING;
  16572. local_irq_enable();
  16573. Enable_cpus:
  16574. @@ -437,6 +440,7 @@
  16575. goto Enable_cpus;
  16576. local_irq_disable();
  16577. + system_state = SYSTEM_SUSPEND;
  16578. error = syscore_suspend();
  16579. if (error)
  16580. @@ -470,6 +474,7 @@
  16581. syscore_resume();
  16582. Enable_irqs:
  16583. + system_state = SYSTEM_RUNNING;
  16584. local_irq_enable();
  16585. Enable_cpus:
  16586. @@ -555,6 +560,7 @@
  16587. goto Platform_finish;
  16588. local_irq_disable();
  16589. + system_state = SYSTEM_SUSPEND;
  16590. syscore_suspend();
  16591. if (pm_wakeup_pending()) {
  16592. error = -EAGAIN;
  16593. @@ -567,6 +573,7 @@
  16594. Power_up:
  16595. syscore_resume();
  16596. + system_state = SYSTEM_RUNNING;
  16597. local_irq_enable();
  16598. enable_nonboot_cpus();
  16599. diff -Nur linux-4.1.20.orig/kernel/power/suspend.c linux-4.1.20/kernel/power/suspend.c
  16600. --- linux-4.1.20.orig/kernel/power/suspend.c 2016-03-17 19:11:03.000000000 +0100
  16601. +++ linux-4.1.20/kernel/power/suspend.c 2016-03-21 20:18:33.000000000 +0100
  16602. @@ -356,6 +356,8 @@
  16603. arch_suspend_disable_irqs();
  16604. BUG_ON(!irqs_disabled());
  16605. + system_state = SYSTEM_SUSPEND;
  16606. +
  16607. error = syscore_suspend();
  16608. if (!error) {
  16609. *wakeup = pm_wakeup_pending();
  16610. @@ -370,6 +372,8 @@
  16611. syscore_resume();
  16612. }
  16613. + system_state = SYSTEM_RUNNING;
  16614. +
  16615. arch_suspend_enable_irqs();
  16616. BUG_ON(irqs_disabled());
  16617. diff -Nur linux-4.1.20.orig/kernel/printk/printk.c linux-4.1.20/kernel/printk/printk.c
  16618. --- linux-4.1.20.orig/kernel/printk/printk.c 2016-03-17 19:11:03.000000000 +0100
  16619. +++ linux-4.1.20/kernel/printk/printk.c 2016-03-21 20:18:33.000000000 +0100
  16620. @@ -1163,6 +1163,7 @@
  16621. {
  16622. char *text;
  16623. int len = 0;
  16624. + int attempts = 0;
  16625. text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
  16626. if (!text)
  16627. @@ -1174,7 +1175,14 @@
  16628. u64 seq;
  16629. u32 idx;
  16630. enum log_flags prev;
  16631. -
  16632. + int num_msg;
  16633. +try_again:
  16634. + attempts++;
  16635. + if (attempts > 10) {
  16636. + len = -EBUSY;
  16637. + goto out;
  16638. + }
  16639. + num_msg = 0;
  16640. if (clear_seq < log_first_seq) {
  16641. /* messages are gone, move to first available one */
  16642. clear_seq = log_first_seq;
  16643. @@ -1195,6 +1203,14 @@
  16644. prev = msg->flags;
  16645. idx = log_next(idx);
  16646. seq++;
  16647. + num_msg++;
  16648. + if (num_msg > 5) {
  16649. + num_msg = 0;
  16650. + raw_spin_unlock_irq(&logbuf_lock);
  16651. + raw_spin_lock_irq(&logbuf_lock);
  16652. + if (clear_seq < log_first_seq)
  16653. + goto try_again;
  16654. + }
  16655. }
  16656. /* move first record forward until length fits into the buffer */
  16657. @@ -1208,6 +1224,14 @@
  16658. prev = msg->flags;
  16659. idx = log_next(idx);
  16660. seq++;
  16661. + num_msg++;
  16662. + if (num_msg > 5) {
  16663. + num_msg = 0;
  16664. + raw_spin_unlock_irq(&logbuf_lock);
  16665. + raw_spin_lock_irq(&logbuf_lock);
  16666. + if (clear_seq < log_first_seq)
  16667. + goto try_again;
  16668. + }
  16669. }
  16670. /* last message fitting into this dump */
  16671. @@ -1248,6 +1272,7 @@
  16672. clear_seq = log_next_seq;
  16673. clear_idx = log_next_idx;
  16674. }
  16675. +out:
  16676. raw_spin_unlock_irq(&logbuf_lock);
  16677. kfree(text);
  16678. @@ -1401,6 +1426,7 @@
  16679. if (!console_drivers)
  16680. return;
  16681. + migrate_disable();
  16682. for_each_console(con) {
  16683. if (exclusive_console && con != exclusive_console)
  16684. continue;
  16685. @@ -1413,6 +1439,7 @@
  16686. continue;
  16687. con->write(con, text, len);
  16688. }
  16689. + migrate_enable();
  16690. }
  16691. /*
  16692. @@ -1473,6 +1500,15 @@
  16693. static int console_trylock_for_printk(void)
  16694. {
  16695. unsigned int cpu = smp_processor_id();
  16696. +#ifdef CONFIG_PREEMPT_RT_FULL
  16697. + int lock = !early_boot_irqs_disabled && (preempt_count() == 0) &&
  16698. + !irqs_disabled();
  16699. +#else
  16700. + int lock = 1;
  16701. +#endif
  16702. +
  16703. + if (!lock)
  16704. + return 0;
  16705. if (!console_trylock())
  16706. return 0;
  16707. @@ -1607,6 +1643,62 @@
  16708. return textlen;
  16709. }
  16710. +#ifdef CONFIG_EARLY_PRINTK
  16711. +struct console *early_console;
  16712. +
  16713. +static void early_vprintk(const char *fmt, va_list ap)
  16714. +{
  16715. + if (early_console) {
  16716. + char buf[512];
  16717. + int n = vscnprintf(buf, sizeof(buf), fmt, ap);
  16718. +
  16719. + early_console->write(early_console, buf, n);
  16720. + }
  16721. +}
  16722. +
  16723. +asmlinkage void early_printk(const char *fmt, ...)
  16724. +{
  16725. + va_list ap;
  16726. +
  16727. + va_start(ap, fmt);
  16728. + early_vprintk(fmt, ap);
  16729. + va_end(ap);
  16730. +}
  16731. +
  16732. +/*
  16733. + * This is independent of any log levels - a global
  16734. + * kill switch that turns off all of printk.
  16735. + *
  16736. + * Used by the NMI watchdog if early-printk is enabled.
  16737. + */
  16738. +static bool __read_mostly printk_killswitch;
  16739. +
  16740. +static int __init force_early_printk_setup(char *str)
  16741. +{
  16742. + printk_killswitch = true;
  16743. + return 0;
  16744. +}
  16745. +early_param("force_early_printk", force_early_printk_setup);
  16746. +
  16747. +void printk_kill(void)
  16748. +{
  16749. + printk_killswitch = true;
  16750. +}
  16751. +
  16752. +static int forced_early_printk(const char *fmt, va_list ap)
  16753. +{
  16754. + if (!printk_killswitch)
  16755. + return 0;
  16756. + early_vprintk(fmt, ap);
  16757. + return 1;
  16758. +}
  16759. +#else
  16760. +static inline int forced_early_printk(const char *fmt, va_list ap)
  16761. +{
  16762. + return 0;
  16763. +}
  16764. +#endif
  16765. +
  16766. asmlinkage int vprintk_emit(int facility, int level,
  16767. const char *dict, size_t dictlen,
  16768. const char *fmt, va_list args)
  16769. @@ -1623,6 +1715,13 @@
  16770. /* cpu currently holding logbuf_lock in this function */
  16771. static unsigned int logbuf_cpu = UINT_MAX;
  16772. + /*
  16773. + * Fall back to early_printk if a debugging subsystem has
  16774. + * killed printk output
  16775. + */
  16776. + if (unlikely(forced_early_printk(fmt, args)))
  16777. + return 1;
  16778. +
  16779. if (level == LOGLEVEL_SCHED) {
  16780. level = LOGLEVEL_DEFAULT;
  16781. in_sched = true;
  16782. @@ -1764,8 +1863,7 @@
  16783. * console_sem which would prevent anyone from printing to
  16784. * console
  16785. */
  16786. - preempt_disable();
  16787. -
  16788. + migrate_disable();
  16789. /*
  16790. * Try to acquire and then immediately release the console
  16791. * semaphore. The release will print out buffers and wake up
  16792. @@ -1773,7 +1871,7 @@
  16793. */
  16794. if (console_trylock_for_printk())
  16795. console_unlock();
  16796. - preempt_enable();
  16797. + migrate_enable();
  16798. lockdep_on();
  16799. }
  16800. @@ -1902,26 +2000,6 @@
  16801. #endif /* CONFIG_PRINTK */
  16802. -#ifdef CONFIG_EARLY_PRINTK
  16803. -struct console *early_console;
  16804. -
  16805. -asmlinkage __visible void early_printk(const char *fmt, ...)
  16806. -{
  16807. - va_list ap;
  16808. - char buf[512];
  16809. - int n;
  16810. -
  16811. - if (!early_console)
  16812. - return;
  16813. -
  16814. - va_start(ap, fmt);
  16815. - n = vscnprintf(buf, sizeof(buf), fmt, ap);
  16816. - va_end(ap);
  16817. -
  16818. - early_console->write(early_console, buf, n);
  16819. -}
  16820. -#endif
  16821. -
  16822. static int __add_preferred_console(char *name, int idx, char *options,
  16823. char *brl_options)
  16824. {
  16825. @@ -2143,11 +2221,16 @@
  16826. goto out;
  16827. len = cont_print_text(text, size);
  16828. +#ifndef CONFIG_PREEMPT_RT_FULL
  16829. raw_spin_unlock(&logbuf_lock);
  16830. stop_critical_timings();
  16831. call_console_drivers(cont.level, text, len);
  16832. start_critical_timings();
  16833. local_irq_restore(flags);
  16834. +#else
  16835. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  16836. + call_console_drivers(cont.level, text, len);
  16837. +#endif
  16838. return;
  16839. out:
  16840. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  16841. @@ -2246,12 +2329,17 @@
  16842. console_idx = log_next(console_idx);
  16843. console_seq++;
  16844. console_prev = msg->flags;
  16845. +#ifdef CONFIG_PREEMPT_RT_FULL
  16846. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  16847. + call_console_drivers(level, text, len);
  16848. +#else
  16849. raw_spin_unlock(&logbuf_lock);
  16850. stop_critical_timings(); /* don't trace print latency */
  16851. call_console_drivers(level, text, len);
  16852. start_critical_timings();
  16853. local_irq_restore(flags);
  16854. +#endif
  16855. if (do_cond_resched)
  16856. cond_resched();
  16857. diff -Nur linux-4.1.20.orig/kernel/ptrace.c linux-4.1.20/kernel/ptrace.c
  16858. --- linux-4.1.20.orig/kernel/ptrace.c 2016-03-17 19:11:03.000000000 +0100
  16859. +++ linux-4.1.20/kernel/ptrace.c 2016-03-21 20:18:33.000000000 +0100
  16860. @@ -129,7 +129,14 @@
  16861. spin_lock_irq(&task->sighand->siglock);
  16862. if (task_is_traced(task) && !__fatal_signal_pending(task)) {
  16863. - task->state = __TASK_TRACED;
  16864. + unsigned long flags;
  16865. +
  16866. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  16867. + if (task->state & __TASK_TRACED)
  16868. + task->state = __TASK_TRACED;
  16869. + else
  16870. + task->saved_state = __TASK_TRACED;
  16871. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16872. ret = true;
  16873. }
  16874. spin_unlock_irq(&task->sighand->siglock);
  16875. diff -Nur linux-4.1.20.orig/kernel/rcu/rcutorture.c linux-4.1.20/kernel/rcu/rcutorture.c
  16876. --- linux-4.1.20.orig/kernel/rcu/rcutorture.c 2016-03-17 19:11:03.000000000 +0100
  16877. +++ linux-4.1.20/kernel/rcu/rcutorture.c 2016-03-21 20:18:33.000000000 +0100
  16878. @@ -389,6 +389,7 @@
  16879. .name = "rcu"
  16880. };
  16881. +#ifndef CONFIG_PREEMPT_RT_FULL
  16882. /*
  16883. * Definitions for rcu_bh torture testing.
  16884. */
  16885. @@ -428,6 +429,12 @@
  16886. .name = "rcu_bh"
  16887. };
  16888. +#else
  16889. +static struct rcu_torture_ops rcu_bh_ops = {
  16890. + .ttype = INVALID_RCU_FLAVOR,
  16891. +};
  16892. +#endif
  16893. +
  16894. /*
  16895. * Don't even think about trying any of these in real life!!!
  16896. * The names includes "busted", and they really means it!
  16897. diff -Nur linux-4.1.20.orig/kernel/rcu/tree.c linux-4.1.20/kernel/rcu/tree.c
  16898. --- linux-4.1.20.orig/kernel/rcu/tree.c 2016-03-17 19:11:03.000000000 +0100
  16899. +++ linux-4.1.20/kernel/rcu/tree.c 2016-03-21 20:18:33.000000000 +0100
  16900. @@ -56,6 +56,11 @@
  16901. #include <linux/random.h>
  16902. #include <linux/ftrace_event.h>
  16903. #include <linux/suspend.h>
  16904. +#include <linux/delay.h>
  16905. +#include <linux/gfp.h>
  16906. +#include <linux/oom.h>
  16907. +#include <linux/smpboot.h>
  16908. +#include "../time/tick-internal.h"
  16909. #include "tree.h"
  16910. #include "rcu.h"
  16911. @@ -220,6 +225,19 @@
  16912. }
  16913. }
  16914. +#ifdef CONFIG_PREEMPT_RT_FULL
  16915. +static void rcu_preempt_qs(void);
  16916. +
  16917. +void rcu_bh_qs(void)
  16918. +{
  16919. + unsigned long flags;
  16920. +
  16921. + /* Callers to this function, rcu_preempt_qs(), must disable irqs. */
  16922. + local_irq_save(flags);
  16923. + rcu_preempt_qs();
  16924. + local_irq_restore(flags);
  16925. +}
  16926. +#else
  16927. void rcu_bh_qs(void)
  16928. {
  16929. if (!__this_cpu_read(rcu_bh_data.passed_quiesce)) {
  16930. @@ -229,6 +247,7 @@
  16931. __this_cpu_write(rcu_bh_data.passed_quiesce, 1);
  16932. }
  16933. }
  16934. +#endif
  16935. static DEFINE_PER_CPU(int, rcu_sched_qs_mask);
  16936. @@ -404,6 +423,7 @@
  16937. }
  16938. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  16939. +#ifndef CONFIG_PREEMPT_RT_FULL
  16940. /*
  16941. * Return the number of RCU BH batches completed thus far for debug & stats.
  16942. */
  16943. @@ -431,6 +451,13 @@
  16944. }
  16945. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  16946. +#else
  16947. +void rcu_force_quiescent_state(void)
  16948. +{
  16949. +}
  16950. +EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  16951. +#endif
  16952. +
  16953. /*
  16954. * Force a quiescent state for RCU-sched.
  16955. */
  16956. @@ -1545,7 +1572,7 @@
  16957. !ACCESS_ONCE(rsp->gp_flags) ||
  16958. !rsp->gp_kthread)
  16959. return;
  16960. - wake_up(&rsp->gp_wq);
  16961. + swait_wake(&rsp->gp_wq);
  16962. }
  16963. /*
  16964. @@ -1986,7 +2013,7 @@
  16965. ACCESS_ONCE(rsp->gpnum),
  16966. TPS("reqwait"));
  16967. rsp->gp_state = RCU_GP_WAIT_GPS;
  16968. - wait_event_interruptible(rsp->gp_wq,
  16969. + swait_event_interruptible(rsp->gp_wq,
  16970. ACCESS_ONCE(rsp->gp_flags) &
  16971. RCU_GP_FLAG_INIT);
  16972. /* Locking provides needed memory barrier. */
  16973. @@ -2015,7 +2042,7 @@
  16974. ACCESS_ONCE(rsp->gpnum),
  16975. TPS("fqswait"));
  16976. rsp->gp_state = RCU_GP_WAIT_FQS;
  16977. - ret = wait_event_interruptible_timeout(rsp->gp_wq,
  16978. + ret = swait_event_interruptible_timeout(rsp->gp_wq,
  16979. ((gf = ACCESS_ONCE(rsp->gp_flags)) &
  16980. RCU_GP_FLAG_FQS) ||
  16981. (!ACCESS_ONCE(rnp->qsmask) &&
  16982. @@ -2860,18 +2887,17 @@
  16983. /*
  16984. * Do RCU core processing for the current CPU.
  16985. */
  16986. -static void rcu_process_callbacks(struct softirq_action *unused)
  16987. +static void rcu_process_callbacks(void)
  16988. {
  16989. struct rcu_state *rsp;
  16990. if (cpu_is_offline(smp_processor_id()))
  16991. return;
  16992. - trace_rcu_utilization(TPS("Start RCU core"));
  16993. for_each_rcu_flavor(rsp)
  16994. __rcu_process_callbacks(rsp);
  16995. - trace_rcu_utilization(TPS("End RCU core"));
  16996. }
  16997. +static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  16998. /*
  16999. * Schedule RCU callback invocation. If the specified type of RCU
  17000. * does not support RCU priority boosting, just do a direct call,
  17001. @@ -2883,18 +2909,105 @@
  17002. {
  17003. if (unlikely(!ACCESS_ONCE(rcu_scheduler_fully_active)))
  17004. return;
  17005. - if (likely(!rsp->boost)) {
  17006. - rcu_do_batch(rsp, rdp);
  17007. + rcu_do_batch(rsp, rdp);
  17008. +}
  17009. +
  17010. +static void rcu_wake_cond(struct task_struct *t, int status)
  17011. +{
  17012. + /*
  17013. + * If the thread is yielding, only wake it when this
  17014. + * is invoked from idle
  17015. + */
  17016. + if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)))
  17017. + wake_up_process(t);
  17018. +}
  17019. +
  17020. +/*
  17021. + * Wake up this CPU's rcuc kthread to do RCU core processing.
  17022. + */
  17023. +static void invoke_rcu_core(void)
  17024. +{
  17025. + unsigned long flags;
  17026. + struct task_struct *t;
  17027. +
  17028. + if (!cpu_online(smp_processor_id()))
  17029. return;
  17030. + local_irq_save(flags);
  17031. + __this_cpu_write(rcu_cpu_has_work, 1);
  17032. + t = __this_cpu_read(rcu_cpu_kthread_task);
  17033. + if (t != NULL && current != t)
  17034. + rcu_wake_cond(t, __this_cpu_read(rcu_cpu_kthread_status));
  17035. + local_irq_restore(flags);
  17036. +}
  17037. +
  17038. +static void rcu_cpu_kthread_park(unsigned int cpu)
  17039. +{
  17040. + per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  17041. +}
  17042. +
  17043. +static int rcu_cpu_kthread_should_run(unsigned int cpu)
  17044. +{
  17045. + return __this_cpu_read(rcu_cpu_has_work);
  17046. +}
  17047. +
  17048. +/*
  17049. + * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  17050. + * RCU softirq used in flavors and configurations of RCU that do not
  17051. + * support RCU priority boosting.
  17052. + */
  17053. +static void rcu_cpu_kthread(unsigned int cpu)
  17054. +{
  17055. + unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  17056. + char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  17057. + int spincnt;
  17058. +
  17059. + for (spincnt = 0; spincnt < 10; spincnt++) {
  17060. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  17061. + local_bh_disable();
  17062. + *statusp = RCU_KTHREAD_RUNNING;
  17063. + this_cpu_inc(rcu_cpu_kthread_loops);
  17064. + local_irq_disable();
  17065. + work = *workp;
  17066. + *workp = 0;
  17067. + local_irq_enable();
  17068. + if (work)
  17069. + rcu_process_callbacks();
  17070. + local_bh_enable();
  17071. + if (*workp == 0) {
  17072. + trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  17073. + *statusp = RCU_KTHREAD_WAITING;
  17074. + return;
  17075. + }
  17076. }
  17077. - invoke_rcu_callbacks_kthread();
  17078. + *statusp = RCU_KTHREAD_YIELDING;
  17079. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  17080. + schedule_timeout_interruptible(2);
  17081. + trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  17082. + *statusp = RCU_KTHREAD_WAITING;
  17083. }
  17084. -static void invoke_rcu_core(void)
  17085. +static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  17086. + .store = &rcu_cpu_kthread_task,
  17087. + .thread_should_run = rcu_cpu_kthread_should_run,
  17088. + .thread_fn = rcu_cpu_kthread,
  17089. + .thread_comm = "rcuc/%u",
  17090. + .setup = rcu_cpu_kthread_setup,
  17091. + .park = rcu_cpu_kthread_park,
  17092. +};
  17093. +
  17094. +/*
  17095. + * Spawn per-CPU RCU core processing kthreads.
  17096. + */
  17097. +static int __init rcu_spawn_core_kthreads(void)
  17098. {
  17099. - if (cpu_online(smp_processor_id()))
  17100. - raise_softirq(RCU_SOFTIRQ);
  17101. + int cpu;
  17102. +
  17103. + for_each_possible_cpu(cpu)
  17104. + per_cpu(rcu_cpu_has_work, cpu) = 0;
  17105. + BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  17106. + return 0;
  17107. }
  17108. +early_initcall(rcu_spawn_core_kthreads);
  17109. /*
  17110. * Handle any core-RCU processing required by a call_rcu() invocation.
  17111. @@ -3040,6 +3153,7 @@
  17112. }
  17113. EXPORT_SYMBOL_GPL(call_rcu_sched);
  17114. +#ifndef CONFIG_PREEMPT_RT_FULL
  17115. /*
  17116. * Queue an RCU callback for invocation after a quicker grace period.
  17117. */
  17118. @@ -3048,6 +3162,7 @@
  17119. __call_rcu(head, func, &rcu_bh_state, -1, 0);
  17120. }
  17121. EXPORT_SYMBOL_GPL(call_rcu_bh);
  17122. +#endif
  17123. /*
  17124. * Queue an RCU callback for lazy invocation after a grace period.
  17125. @@ -3139,6 +3254,7 @@
  17126. }
  17127. EXPORT_SYMBOL_GPL(synchronize_sched);
  17128. +#ifndef CONFIG_PREEMPT_RT_FULL
  17129. /**
  17130. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  17131. *
  17132. @@ -3165,6 +3281,7 @@
  17133. wait_rcu_gp(call_rcu_bh);
  17134. }
  17135. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  17136. +#endif
  17137. /**
  17138. * get_state_synchronize_rcu - Snapshot current RCU state
  17139. @@ -3677,6 +3794,7 @@
  17140. mutex_unlock(&rsp->barrier_mutex);
  17141. }
  17142. +#ifndef CONFIG_PREEMPT_RT_FULL
  17143. /**
  17144. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  17145. */
  17146. @@ -3685,6 +3803,7 @@
  17147. _rcu_barrier(&rcu_bh_state);
  17148. }
  17149. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  17150. +#endif
  17151. /**
  17152. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  17153. @@ -4021,7 +4140,7 @@
  17154. }
  17155. }
  17156. - init_waitqueue_head(&rsp->gp_wq);
  17157. + init_swait_head(&rsp->gp_wq);
  17158. rnp = rsp->level[rcu_num_lvls - 1];
  17159. for_each_possible_cpu(i) {
  17160. while (i > rnp->grphi)
  17161. @@ -4120,7 +4239,6 @@
  17162. rcu_init_one(&rcu_bh_state, &rcu_bh_data);
  17163. rcu_init_one(&rcu_sched_state, &rcu_sched_data);
  17164. __rcu_init_preempt();
  17165. - open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  17166. /*
  17167. * We don't need protection against CPU-hotplug here because
  17168. diff -Nur linux-4.1.20.orig/kernel/rcu/tree.h linux-4.1.20/kernel/rcu/tree.h
  17169. --- linux-4.1.20.orig/kernel/rcu/tree.h 2016-03-17 19:11:03.000000000 +0100
  17170. +++ linux-4.1.20/kernel/rcu/tree.h 2016-03-21 20:18:33.000000000 +0100
  17171. @@ -27,6 +27,7 @@
  17172. #include <linux/threads.h>
  17173. #include <linux/cpumask.h>
  17174. #include <linux/seqlock.h>
  17175. +#include <linux/wait-simple.h>
  17176. /*
  17177. * Define shape of hierarchy based on NR_CPUS, CONFIG_RCU_FANOUT, and
  17178. @@ -210,7 +211,7 @@
  17179. /* This can happen due to race conditions. */
  17180. #endif /* #ifdef CONFIG_RCU_BOOST */
  17181. #ifdef CONFIG_RCU_NOCB_CPU
  17182. - wait_queue_head_t nocb_gp_wq[2];
  17183. + struct swait_head nocb_gp_wq[2];
  17184. /* Place for rcu_nocb_kthread() to wait GP. */
  17185. #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
  17186. int need_future_gp[2];
  17187. @@ -349,7 +350,7 @@
  17188. atomic_long_t nocb_q_count_lazy; /* invocation (all stages). */
  17189. struct rcu_head *nocb_follower_head; /* CBs ready to invoke. */
  17190. struct rcu_head **nocb_follower_tail;
  17191. - wait_queue_head_t nocb_wq; /* For nocb kthreads to sleep on. */
  17192. + struct swait_head nocb_wq; /* For nocb kthreads to sleep on. */
  17193. struct task_struct *nocb_kthread;
  17194. int nocb_defer_wakeup; /* Defer wakeup of nocb_kthread. */
  17195. @@ -438,7 +439,7 @@
  17196. unsigned long gpnum; /* Current gp number. */
  17197. unsigned long completed; /* # of last completed gp. */
  17198. struct task_struct *gp_kthread; /* Task for grace periods. */
  17199. - wait_queue_head_t gp_wq; /* Where GP task waits. */
  17200. + struct swait_head gp_wq; /* Where GP task waits. */
  17201. short gp_flags; /* Commands for GP task. */
  17202. short gp_state; /* GP kthread sleep state. */
  17203. @@ -529,12 +530,10 @@
  17204. DECLARE_PER_CPU(struct rcu_data, rcu_preempt_data);
  17205. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  17206. -#ifdef CONFIG_RCU_BOOST
  17207. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  17208. DECLARE_PER_CPU(int, rcu_cpu_kthread_cpu);
  17209. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  17210. DECLARE_PER_CPU(char, rcu_cpu_has_work);
  17211. -#endif /* #ifdef CONFIG_RCU_BOOST */
  17212. #ifndef RCU_TREE_NONCORE
  17213. @@ -553,10 +552,9 @@
  17214. static void __init __rcu_init_preempt(void);
  17215. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags);
  17216. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp);
  17217. -static void invoke_rcu_callbacks_kthread(void);
  17218. static bool rcu_is_callbacks_kthread(void);
  17219. +static void rcu_cpu_kthread_setup(unsigned int cpu);
  17220. #ifdef CONFIG_RCU_BOOST
  17221. -static void rcu_preempt_do_callbacks(void);
  17222. static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  17223. struct rcu_node *rnp);
  17224. #endif /* #ifdef CONFIG_RCU_BOOST */
  17225. diff -Nur linux-4.1.20.orig/kernel/rcu/tree_plugin.h linux-4.1.20/kernel/rcu/tree_plugin.h
  17226. --- linux-4.1.20.orig/kernel/rcu/tree_plugin.h 2016-03-17 19:11:03.000000000 +0100
  17227. +++ linux-4.1.20/kernel/rcu/tree_plugin.h 2016-03-21 20:18:33.000000000 +0100
  17228. @@ -24,27 +24,20 @@
  17229. * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  17230. */
  17231. -#include <linux/delay.h>
  17232. -#include <linux/gfp.h>
  17233. -#include <linux/oom.h>
  17234. -#include <linux/smpboot.h>
  17235. -#include "../time/tick-internal.h"
  17236. -
  17237. #ifdef CONFIG_RCU_BOOST
  17238. #include "../locking/rtmutex_common.h"
  17239. +#endif /* #ifdef CONFIG_RCU_BOOST */
  17240. +
  17241. /*
  17242. * Control variables for per-CPU and per-rcu_node kthreads. These
  17243. * handle all flavors of RCU.
  17244. */
  17245. -static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  17246. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  17247. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  17248. DEFINE_PER_CPU(char, rcu_cpu_has_work);
  17249. -#endif /* #ifdef CONFIG_RCU_BOOST */
  17250. -
  17251. #ifdef CONFIG_RCU_NOCB_CPU
  17252. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  17253. static bool have_rcu_nocb_mask; /* Was rcu_nocb_mask allocated? */
  17254. @@ -291,7 +284,7 @@
  17255. }
  17256. /* Hardware IRQ handlers cannot block, complain if they get here. */
  17257. - if (in_irq() || in_serving_softirq()) {
  17258. + if (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_OFFSET)) {
  17259. lockdep_rcu_suspicious(__FILE__, __LINE__,
  17260. "rcu_read_unlock() from irq or softirq with blocking in critical section!!!\n");
  17261. pr_alert("->rcu_read_unlock_special: %#x (b: %d, nq: %d)\n",
  17262. @@ -496,15 +489,6 @@
  17263. t->rcu_read_unlock_special.b.need_qs = true;
  17264. }
  17265. -#ifdef CONFIG_RCU_BOOST
  17266. -
  17267. -static void rcu_preempt_do_callbacks(void)
  17268. -{
  17269. - rcu_do_batch(&rcu_preempt_state, this_cpu_ptr(&rcu_preempt_data));
  17270. -}
  17271. -
  17272. -#endif /* #ifdef CONFIG_RCU_BOOST */
  17273. -
  17274. /*
  17275. * Queue a preemptible-RCU callback for invocation after a grace period.
  17276. */
  17277. @@ -939,6 +923,19 @@
  17278. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  17279. +/*
  17280. + * If boosting, set rcuc kthreads to realtime priority.
  17281. + */
  17282. +static void rcu_cpu_kthread_setup(unsigned int cpu)
  17283. +{
  17284. +#ifdef CONFIG_RCU_BOOST
  17285. + struct sched_param sp;
  17286. +
  17287. + sp.sched_priority = kthread_prio;
  17288. + sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  17289. +#endif /* #ifdef CONFIG_RCU_BOOST */
  17290. +}
  17291. +
  17292. #ifdef CONFIG_RCU_BOOST
  17293. #include "../locking/rtmutex_common.h"
  17294. @@ -970,16 +967,6 @@
  17295. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  17296. -static void rcu_wake_cond(struct task_struct *t, int status)
  17297. -{
  17298. - /*
  17299. - * If the thread is yielding, only wake it when this
  17300. - * is invoked from idle
  17301. - */
  17302. - if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
  17303. - wake_up_process(t);
  17304. -}
  17305. -
  17306. /*
  17307. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  17308. * or ->boost_tasks, advancing the pointer to the next task in the
  17309. @@ -1125,23 +1112,6 @@
  17310. }
  17311. /*
  17312. - * Wake up the per-CPU kthread to invoke RCU callbacks.
  17313. - */
  17314. -static void invoke_rcu_callbacks_kthread(void)
  17315. -{
  17316. - unsigned long flags;
  17317. -
  17318. - local_irq_save(flags);
  17319. - __this_cpu_write(rcu_cpu_has_work, 1);
  17320. - if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
  17321. - current != __this_cpu_read(rcu_cpu_kthread_task)) {
  17322. - rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
  17323. - __this_cpu_read(rcu_cpu_kthread_status));
  17324. - }
  17325. - local_irq_restore(flags);
  17326. -}
  17327. -
  17328. -/*
  17329. * Is the current CPU running the RCU-callbacks kthread?
  17330. * Caller must have preemption disabled.
  17331. */
  17332. @@ -1196,67 +1166,6 @@
  17333. return 0;
  17334. }
  17335. -static void rcu_kthread_do_work(void)
  17336. -{
  17337. - rcu_do_batch(&rcu_sched_state, this_cpu_ptr(&rcu_sched_data));
  17338. - rcu_do_batch(&rcu_bh_state, this_cpu_ptr(&rcu_bh_data));
  17339. - rcu_preempt_do_callbacks();
  17340. -}
  17341. -
  17342. -static void rcu_cpu_kthread_setup(unsigned int cpu)
  17343. -{
  17344. - struct sched_param sp;
  17345. -
  17346. - sp.sched_priority = kthread_prio;
  17347. - sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  17348. -}
  17349. -
  17350. -static void rcu_cpu_kthread_park(unsigned int cpu)
  17351. -{
  17352. - per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  17353. -}
  17354. -
  17355. -static int rcu_cpu_kthread_should_run(unsigned int cpu)
  17356. -{
  17357. - return __this_cpu_read(rcu_cpu_has_work);
  17358. -}
  17359. -
  17360. -/*
  17361. - * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  17362. - * RCU softirq used in flavors and configurations of RCU that do not
  17363. - * support RCU priority boosting.
  17364. - */
  17365. -static void rcu_cpu_kthread(unsigned int cpu)
  17366. -{
  17367. - unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  17368. - char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  17369. - int spincnt;
  17370. -
  17371. - for (spincnt = 0; spincnt < 10; spincnt++) {
  17372. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  17373. - local_bh_disable();
  17374. - *statusp = RCU_KTHREAD_RUNNING;
  17375. - this_cpu_inc(rcu_cpu_kthread_loops);
  17376. - local_irq_disable();
  17377. - work = *workp;
  17378. - *workp = 0;
  17379. - local_irq_enable();
  17380. - if (work)
  17381. - rcu_kthread_do_work();
  17382. - local_bh_enable();
  17383. - if (*workp == 0) {
  17384. - trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  17385. - *statusp = RCU_KTHREAD_WAITING;
  17386. - return;
  17387. - }
  17388. - }
  17389. - *statusp = RCU_KTHREAD_YIELDING;
  17390. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  17391. - schedule_timeout_interruptible(2);
  17392. - trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  17393. - *statusp = RCU_KTHREAD_WAITING;
  17394. -}
  17395. -
  17396. /*
  17397. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  17398. * served by the rcu_node in question. The CPU hotplug lock is still
  17399. @@ -1286,26 +1195,12 @@
  17400. free_cpumask_var(cm);
  17401. }
  17402. -static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  17403. - .store = &rcu_cpu_kthread_task,
  17404. - .thread_should_run = rcu_cpu_kthread_should_run,
  17405. - .thread_fn = rcu_cpu_kthread,
  17406. - .thread_comm = "rcuc/%u",
  17407. - .setup = rcu_cpu_kthread_setup,
  17408. - .park = rcu_cpu_kthread_park,
  17409. -};
  17410. -
  17411. /*
  17412. * Spawn boost kthreads -- called as soon as the scheduler is running.
  17413. */
  17414. static void __init rcu_spawn_boost_kthreads(void)
  17415. {
  17416. struct rcu_node *rnp;
  17417. - int cpu;
  17418. -
  17419. - for_each_possible_cpu(cpu)
  17420. - per_cpu(rcu_cpu_has_work, cpu) = 0;
  17421. - BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  17422. rcu_for_each_leaf_node(rcu_state_p, rnp)
  17423. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  17424. }
  17425. @@ -1328,11 +1223,6 @@
  17426. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  17427. }
  17428. -static void invoke_rcu_callbacks_kthread(void)
  17429. -{
  17430. - WARN_ON_ONCE(1);
  17431. -}
  17432. -
  17433. static bool rcu_is_callbacks_kthread(void)
  17434. {
  17435. return false;
  17436. @@ -1356,7 +1246,7 @@
  17437. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  17438. -#if !defined(CONFIG_RCU_FAST_NO_HZ)
  17439. +#if !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL)
  17440. /*
  17441. * Check to see if any future RCU-related work will need to be done
  17442. @@ -1374,7 +1264,9 @@
  17443. return rcu_cpu_has_callbacks(NULL);
  17444. }
  17445. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  17446. +#endif /* !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL) */
  17447. +#if !defined(CONFIG_RCU_FAST_NO_HZ)
  17448. /*
  17449. * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
  17450. * after it.
  17451. @@ -1472,6 +1364,8 @@
  17452. return cbs_ready;
  17453. }
  17454. +#ifndef CONFIG_PREEMPT_RT_FULL
  17455. +
  17456. /*
  17457. * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
  17458. * to invoke. If the CPU has callbacks, try to advance them. Tell the
  17459. @@ -1512,7 +1406,7 @@
  17460. return 0;
  17461. }
  17462. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  17463. -
  17464. +#endif /* #ifndef CONFIG_PREEMPT_RT_FULL */
  17465. /*
  17466. * Prepare a CPU for idle from an RCU perspective. The first major task
  17467. * is to sense whether nohz mode has been enabled or disabled via sysfs.
  17468. @@ -1859,7 +1753,7 @@
  17469. */
  17470. static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  17471. {
  17472. - wake_up_all(&rnp->nocb_gp_wq[rnp->completed & 0x1]);
  17473. + swait_wake_all(&rnp->nocb_gp_wq[rnp->completed & 0x1]);
  17474. }
  17475. /*
  17476. @@ -1877,8 +1771,8 @@
  17477. static void rcu_init_one_nocb(struct rcu_node *rnp)
  17478. {
  17479. - init_waitqueue_head(&rnp->nocb_gp_wq[0]);
  17480. - init_waitqueue_head(&rnp->nocb_gp_wq[1]);
  17481. + init_swait_head(&rnp->nocb_gp_wq[0]);
  17482. + init_swait_head(&rnp->nocb_gp_wq[1]);
  17483. }
  17484. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  17485. @@ -1903,7 +1797,7 @@
  17486. if (ACCESS_ONCE(rdp_leader->nocb_leader_sleep) || force) {
  17487. /* Prior smp_mb__after_atomic() orders against prior enqueue. */
  17488. ACCESS_ONCE(rdp_leader->nocb_leader_sleep) = false;
  17489. - wake_up(&rdp_leader->nocb_wq);
  17490. + swait_wake(&rdp_leader->nocb_wq);
  17491. }
  17492. }
  17493. @@ -2116,7 +2010,7 @@
  17494. */
  17495. trace_rcu_future_gp(rnp, rdp, c, TPS("StartWait"));
  17496. for (;;) {
  17497. - wait_event_interruptible(
  17498. + swait_event_interruptible(
  17499. rnp->nocb_gp_wq[c & 0x1],
  17500. (d = ULONG_CMP_GE(ACCESS_ONCE(rnp->completed), c)));
  17501. if (likely(d))
  17502. @@ -2144,7 +2038,7 @@
  17503. /* Wait for callbacks to appear. */
  17504. if (!rcu_nocb_poll) {
  17505. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, "Sleep");
  17506. - wait_event_interruptible(my_rdp->nocb_wq,
  17507. + swait_event_interruptible(my_rdp->nocb_wq,
  17508. !ACCESS_ONCE(my_rdp->nocb_leader_sleep));
  17509. /* Memory barrier handled by smp_mb() calls below and repoll. */
  17510. } else if (firsttime) {
  17511. @@ -2219,7 +2113,7 @@
  17512. * List was empty, wake up the follower.
  17513. * Memory barriers supplied by atomic_long_add().
  17514. */
  17515. - wake_up(&rdp->nocb_wq);
  17516. + swait_wake(&rdp->nocb_wq);
  17517. }
  17518. }
  17519. @@ -2240,7 +2134,7 @@
  17520. if (!rcu_nocb_poll) {
  17521. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  17522. "FollowerSleep");
  17523. - wait_event_interruptible(rdp->nocb_wq,
  17524. + swait_event_interruptible(rdp->nocb_wq,
  17525. ACCESS_ONCE(rdp->nocb_follower_head));
  17526. } else if (firsttime) {
  17527. /* Don't drown trace log with "Poll"! */
  17528. @@ -2399,7 +2293,7 @@
  17529. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  17530. {
  17531. rdp->nocb_tail = &rdp->nocb_head;
  17532. - init_waitqueue_head(&rdp->nocb_wq);
  17533. + init_swait_head(&rdp->nocb_wq);
  17534. rdp->nocb_follower_tail = &rdp->nocb_follower_head;
  17535. }
  17536. diff -Nur linux-4.1.20.orig/kernel/rcu/update.c linux-4.1.20/kernel/rcu/update.c
  17537. --- linux-4.1.20.orig/kernel/rcu/update.c 2016-03-17 19:11:03.000000000 +0100
  17538. +++ linux-4.1.20/kernel/rcu/update.c 2016-03-21 20:18:33.000000000 +0100
  17539. @@ -227,6 +227,7 @@
  17540. }
  17541. EXPORT_SYMBOL_GPL(rcu_read_lock_held);
  17542. +#ifndef CONFIG_PREEMPT_RT_FULL
  17543. /**
  17544. * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
  17545. *
  17546. @@ -253,6 +254,7 @@
  17547. return in_softirq() || irqs_disabled();
  17548. }
  17549. EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
  17550. +#endif
  17551. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  17552. diff -Nur linux-4.1.20.orig/kernel/relay.c linux-4.1.20/kernel/relay.c
  17553. --- linux-4.1.20.orig/kernel/relay.c 2016-03-17 19:11:03.000000000 +0100
  17554. +++ linux-4.1.20/kernel/relay.c 2016-03-21 20:18:33.000000000 +0100
  17555. @@ -339,6 +339,10 @@
  17556. {
  17557. struct rchan_buf *buf = (struct rchan_buf *)data;
  17558. wake_up_interruptible(&buf->read_wait);
  17559. + /*
  17560. + * Stupid polling for now:
  17561. + */
  17562. + mod_timer(&buf->timer, jiffies + 1);
  17563. }
  17564. /**
  17565. @@ -356,6 +360,7 @@
  17566. init_waitqueue_head(&buf->read_wait);
  17567. kref_init(&buf->kref);
  17568. setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
  17569. + mod_timer(&buf->timer, jiffies + 1);
  17570. } else
  17571. del_timer_sync(&buf->timer);
  17572. @@ -739,15 +744,6 @@
  17573. else
  17574. buf->early_bytes += buf->chan->subbuf_size -
  17575. buf->padding[old_subbuf];
  17576. - smp_mb();
  17577. - if (waitqueue_active(&buf->read_wait))
  17578. - /*
  17579. - * Calling wake_up_interruptible() from here
  17580. - * will deadlock if we happen to be logging
  17581. - * from the scheduler (trying to re-grab
  17582. - * rq->lock), so defer it.
  17583. - */
  17584. - mod_timer(&buf->timer, jiffies + 1);
  17585. }
  17586. old = buf->data;
  17587. diff -Nur linux-4.1.20.orig/kernel/sched/Makefile linux-4.1.20/kernel/sched/Makefile
  17588. --- linux-4.1.20.orig/kernel/sched/Makefile 2016-03-17 19:11:03.000000000 +0100
  17589. +++ linux-4.1.20/kernel/sched/Makefile 2016-03-21 20:18:33.000000000 +0100
  17590. @@ -13,7 +13,7 @@
  17591. obj-y += core.o proc.o clock.o cputime.o
  17592. obj-y += idle_task.o fair.o rt.o deadline.o stop_task.o
  17593. -obj-y += wait.o completion.o idle.o
  17594. +obj-y += wait.o wait-simple.o work-simple.o completion.o idle.o
  17595. obj-$(CONFIG_SMP) += cpupri.o cpudeadline.o
  17596. obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o
  17597. obj-$(CONFIG_SCHEDSTATS) += stats.o
  17598. diff -Nur linux-4.1.20.orig/kernel/sched/completion.c linux-4.1.20/kernel/sched/completion.c
  17599. --- linux-4.1.20.orig/kernel/sched/completion.c 2016-03-17 19:11:03.000000000 +0100
  17600. +++ linux-4.1.20/kernel/sched/completion.c 2016-03-21 20:18:33.000000000 +0100
  17601. @@ -30,10 +30,10 @@
  17602. {
  17603. unsigned long flags;
  17604. - spin_lock_irqsave(&x->wait.lock, flags);
  17605. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  17606. x->done++;
  17607. - __wake_up_locked(&x->wait, TASK_NORMAL, 1);
  17608. - spin_unlock_irqrestore(&x->wait.lock, flags);
  17609. + __swait_wake_locked(&x->wait, TASK_NORMAL, 1);
  17610. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  17611. }
  17612. EXPORT_SYMBOL(complete);
  17613. @@ -50,10 +50,10 @@
  17614. {
  17615. unsigned long flags;
  17616. - spin_lock_irqsave(&x->wait.lock, flags);
  17617. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  17618. x->done += UINT_MAX/2;
  17619. - __wake_up_locked(&x->wait, TASK_NORMAL, 0);
  17620. - spin_unlock_irqrestore(&x->wait.lock, flags);
  17621. + __swait_wake_locked(&x->wait, TASK_NORMAL, 0);
  17622. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  17623. }
  17624. EXPORT_SYMBOL(complete_all);
  17625. @@ -62,20 +62,20 @@
  17626. long (*action)(long), long timeout, int state)
  17627. {
  17628. if (!x->done) {
  17629. - DECLARE_WAITQUEUE(wait, current);
  17630. + DEFINE_SWAITER(wait);
  17631. - __add_wait_queue_tail_exclusive(&x->wait, &wait);
  17632. + swait_prepare_locked(&x->wait, &wait);
  17633. do {
  17634. if (signal_pending_state(state, current)) {
  17635. timeout = -ERESTARTSYS;
  17636. break;
  17637. }
  17638. __set_current_state(state);
  17639. - spin_unlock_irq(&x->wait.lock);
  17640. + raw_spin_unlock_irq(&x->wait.lock);
  17641. timeout = action(timeout);
  17642. - spin_lock_irq(&x->wait.lock);
  17643. + raw_spin_lock_irq(&x->wait.lock);
  17644. } while (!x->done && timeout);
  17645. - __remove_wait_queue(&x->wait, &wait);
  17646. + swait_finish_locked(&x->wait, &wait);
  17647. if (!x->done)
  17648. return timeout;
  17649. }
  17650. @@ -89,9 +89,9 @@
  17651. {
  17652. might_sleep();
  17653. - spin_lock_irq(&x->wait.lock);
  17654. + raw_spin_lock_irq(&x->wait.lock);
  17655. timeout = do_wait_for_common(x, action, timeout, state);
  17656. - spin_unlock_irq(&x->wait.lock);
  17657. + raw_spin_unlock_irq(&x->wait.lock);
  17658. return timeout;
  17659. }
  17660. @@ -277,12 +277,12 @@
  17661. if (!READ_ONCE(x->done))
  17662. return 0;
  17663. - spin_lock_irqsave(&x->wait.lock, flags);
  17664. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  17665. if (!x->done)
  17666. ret = 0;
  17667. else
  17668. x->done--;
  17669. - spin_unlock_irqrestore(&x->wait.lock, flags);
  17670. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  17671. return ret;
  17672. }
  17673. EXPORT_SYMBOL(try_wait_for_completion);
  17674. @@ -311,7 +311,7 @@
  17675. * after it's acquired the lock.
  17676. */
  17677. smp_rmb();
  17678. - spin_unlock_wait(&x->wait.lock);
  17679. + raw_spin_unlock_wait(&x->wait.lock);
  17680. return true;
  17681. }
  17682. EXPORT_SYMBOL(completion_done);
  17683. diff -Nur linux-4.1.20.orig/kernel/sched/core.c linux-4.1.20/kernel/sched/core.c
  17684. --- linux-4.1.20.orig/kernel/sched/core.c 2016-03-17 19:11:03.000000000 +0100
  17685. +++ linux-4.1.20/kernel/sched/core.c 2016-03-21 20:18:33.000000000 +0100
  17686. @@ -282,7 +282,11 @@
  17687. * Number of tasks to iterate in a single balance run.
  17688. * Limited because this is done with IRQs disabled.
  17689. */
  17690. +#ifndef CONFIG_PREEMPT_RT_FULL
  17691. const_debug unsigned int sysctl_sched_nr_migrate = 32;
  17692. +#else
  17693. +const_debug unsigned int sysctl_sched_nr_migrate = 8;
  17694. +#endif
  17695. /*
  17696. * period over which we average the RT time consumption, measured
  17697. @@ -461,6 +465,7 @@
  17698. hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  17699. rq->hrtick_timer.function = hrtick;
  17700. + rq->hrtick_timer.irqsafe = 1;
  17701. }
  17702. #else /* CONFIG_SCHED_HRTICK */
  17703. static inline void hrtick_clear(struct rq *rq)
  17704. @@ -541,6 +546,52 @@
  17705. #endif
  17706. #endif
  17707. +void wake_q_add(struct wake_q_head *head, struct task_struct *task)
  17708. +{
  17709. + struct wake_q_node *node = &task->wake_q;
  17710. +
  17711. + /*
  17712. + * Atomically grab the task, if ->wake_q is !nil already it means
  17713. + * its already queued (either by us or someone else) and will get the
  17714. + * wakeup due to that.
  17715. + *
  17716. + * This cmpxchg() implies a full barrier, which pairs with the write
  17717. + * barrier implied by the wakeup in wake_up_list().
  17718. + */
  17719. + if (cmpxchg(&node->next, NULL, WAKE_Q_TAIL))
  17720. + return;
  17721. +
  17722. + get_task_struct(task);
  17723. +
  17724. + /*
  17725. + * The head is context local, there can be no concurrency.
  17726. + */
  17727. + *head->lastp = node;
  17728. + head->lastp = &node->next;
  17729. +}
  17730. +
  17731. +void wake_up_q(struct wake_q_head *head)
  17732. +{
  17733. + struct wake_q_node *node = head->first;
  17734. +
  17735. + while (node != WAKE_Q_TAIL) {
  17736. + struct task_struct *task;
  17737. +
  17738. + task = container_of(node, struct task_struct, wake_q);
  17739. + BUG_ON(!task);
  17740. + /* task can safely be re-inserted now */
  17741. + node = node->next;
  17742. + task->wake_q.next = NULL;
  17743. +
  17744. + /*
  17745. + * wake_up_process() implies a wmb() to pair with the queueing
  17746. + * in wake_q_add() so as not to miss wakeups.
  17747. + */
  17748. + wake_up_process(task);
  17749. + put_task_struct(task);
  17750. + }
  17751. +}
  17752. +
  17753. /*
  17754. * resched_curr - mark rq's current task 'to be rescheduled now'.
  17755. *
  17756. @@ -572,6 +623,38 @@
  17757. trace_sched_wake_idle_without_ipi(cpu);
  17758. }
  17759. +#ifdef CONFIG_PREEMPT_LAZY
  17760. +void resched_curr_lazy(struct rq *rq)
  17761. +{
  17762. + struct task_struct *curr = rq->curr;
  17763. + int cpu;
  17764. +
  17765. + if (!sched_feat(PREEMPT_LAZY)) {
  17766. + resched_curr(rq);
  17767. + return;
  17768. + }
  17769. +
  17770. + lockdep_assert_held(&rq->lock);
  17771. +
  17772. + if (test_tsk_need_resched(curr))
  17773. + return;
  17774. +
  17775. + if (test_tsk_need_resched_lazy(curr))
  17776. + return;
  17777. +
  17778. + set_tsk_need_resched_lazy(curr);
  17779. +
  17780. + cpu = cpu_of(rq);
  17781. + if (cpu == smp_processor_id())
  17782. + return;
  17783. +
  17784. + /* NEED_RESCHED_LAZY must be visible before we test polling */
  17785. + smp_mb();
  17786. + if (!tsk_is_polling(curr))
  17787. + smp_send_reschedule(cpu);
  17788. +}
  17789. +#endif
  17790. +
  17791. void resched_cpu(int cpu)
  17792. {
  17793. struct rq *rq = cpu_rq(cpu);
  17794. @@ -595,12 +678,14 @@
  17795. */
  17796. int get_nohz_timer_target(int pinned)
  17797. {
  17798. - int cpu = smp_processor_id();
  17799. + int cpu;
  17800. int i;
  17801. struct sched_domain *sd;
  17802. + preempt_disable_rt();
  17803. + cpu = smp_processor_id();
  17804. if (pinned || !get_sysctl_timer_migration() || !idle_cpu(cpu))
  17805. - return cpu;
  17806. + goto preempt_en_rt;
  17807. rcu_read_lock();
  17808. for_each_domain(cpu, sd) {
  17809. @@ -613,6 +698,8 @@
  17810. }
  17811. unlock:
  17812. rcu_read_unlock();
  17813. +preempt_en_rt:
  17814. + preempt_enable_rt();
  17815. return cpu;
  17816. }
  17817. /*
  17818. @@ -1164,6 +1251,18 @@
  17819. static int migration_cpu_stop(void *data);
  17820. +static bool check_task_state(struct task_struct *p, long match_state)
  17821. +{
  17822. + bool match = false;
  17823. +
  17824. + raw_spin_lock_irq(&p->pi_lock);
  17825. + if (p->state == match_state || p->saved_state == match_state)
  17826. + match = true;
  17827. + raw_spin_unlock_irq(&p->pi_lock);
  17828. +
  17829. + return match;
  17830. +}
  17831. +
  17832. /*
  17833. * wait_task_inactive - wait for a thread to unschedule.
  17834. *
  17835. @@ -1208,7 +1307,7 @@
  17836. * is actually now running somewhere else!
  17837. */
  17838. while (task_running(rq, p)) {
  17839. - if (match_state && unlikely(p->state != match_state))
  17840. + if (match_state && !check_task_state(p, match_state))
  17841. return 0;
  17842. cpu_relax();
  17843. }
  17844. @@ -1223,7 +1322,8 @@
  17845. running = task_running(rq, p);
  17846. queued = task_on_rq_queued(p);
  17847. ncsw = 0;
  17848. - if (!match_state || p->state == match_state)
  17849. + if (!match_state || p->state == match_state ||
  17850. + p->saved_state == match_state)
  17851. ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
  17852. task_rq_unlock(rq, p, &flags);
  17853. @@ -1449,10 +1549,6 @@
  17854. {
  17855. activate_task(rq, p, en_flags);
  17856. p->on_rq = TASK_ON_RQ_QUEUED;
  17857. -
  17858. - /* if a worker is waking up, notify workqueue */
  17859. - if (p->flags & PF_WQ_WORKER)
  17860. - wq_worker_waking_up(p, cpu_of(rq));
  17861. }
  17862. /*
  17863. @@ -1462,9 +1558,9 @@
  17864. ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
  17865. {
  17866. check_preempt_curr(rq, p, wake_flags);
  17867. - trace_sched_wakeup(p, true);
  17868. -
  17869. p->state = TASK_RUNNING;
  17870. + trace_sched_wakeup(p);
  17871. +
  17872. #ifdef CONFIG_SMP
  17873. if (p->sched_class->task_woken)
  17874. p->sched_class->task_woken(rq, p);
  17875. @@ -1666,8 +1762,29 @@
  17876. */
  17877. smp_mb__before_spinlock();
  17878. raw_spin_lock_irqsave(&p->pi_lock, flags);
  17879. - if (!(p->state & state))
  17880. + if (!(p->state & state)) {
  17881. + /*
  17882. + * The task might be running due to a spinlock sleeper
  17883. + * wakeup. Check the saved state and set it to running
  17884. + * if the wakeup condition is true.
  17885. + */
  17886. + if (!(wake_flags & WF_LOCK_SLEEPER)) {
  17887. + if (p->saved_state & state) {
  17888. + p->saved_state = TASK_RUNNING;
  17889. + success = 1;
  17890. + }
  17891. + }
  17892. goto out;
  17893. + }
  17894. +
  17895. + /*
  17896. + * If this is a regular wakeup, then we can unconditionally
  17897. + * clear the saved state of a "lock sleeper".
  17898. + */
  17899. + if (!(wake_flags & WF_LOCK_SLEEPER))
  17900. + p->saved_state = TASK_RUNNING;
  17901. +
  17902. + trace_sched_waking(p);
  17903. success = 1; /* we're going to change ->state */
  17904. cpu = task_cpu(p);
  17905. @@ -1710,42 +1827,6 @@
  17906. }
  17907. /**
  17908. - * try_to_wake_up_local - try to wake up a local task with rq lock held
  17909. - * @p: the thread to be awakened
  17910. - *
  17911. - * Put @p on the run-queue if it's not already there. The caller must
  17912. - * ensure that this_rq() is locked, @p is bound to this_rq() and not
  17913. - * the current task.
  17914. - */
  17915. -static void try_to_wake_up_local(struct task_struct *p)
  17916. -{
  17917. - struct rq *rq = task_rq(p);
  17918. -
  17919. - if (WARN_ON_ONCE(rq != this_rq()) ||
  17920. - WARN_ON_ONCE(p == current))
  17921. - return;
  17922. -
  17923. - lockdep_assert_held(&rq->lock);
  17924. -
  17925. - if (!raw_spin_trylock(&p->pi_lock)) {
  17926. - raw_spin_unlock(&rq->lock);
  17927. - raw_spin_lock(&p->pi_lock);
  17928. - raw_spin_lock(&rq->lock);
  17929. - }
  17930. -
  17931. - if (!(p->state & TASK_NORMAL))
  17932. - goto out;
  17933. -
  17934. - if (!task_on_rq_queued(p))
  17935. - ttwu_activate(rq, p, ENQUEUE_WAKEUP);
  17936. -
  17937. - ttwu_do_wakeup(rq, p, 0);
  17938. - ttwu_stat(p, smp_processor_id(), 0);
  17939. -out:
  17940. - raw_spin_unlock(&p->pi_lock);
  17941. -}
  17942. -
  17943. -/**
  17944. * wake_up_process - Wake up a specific process
  17945. * @p: The process to be woken up.
  17946. *
  17947. @@ -1759,11 +1840,23 @@
  17948. */
  17949. int wake_up_process(struct task_struct *p)
  17950. {
  17951. - WARN_ON(task_is_stopped_or_traced(p));
  17952. + WARN_ON(__task_is_stopped_or_traced(p));
  17953. return try_to_wake_up(p, TASK_NORMAL, 0);
  17954. }
  17955. EXPORT_SYMBOL(wake_up_process);
  17956. +/**
  17957. + * wake_up_lock_sleeper - Wake up a specific process blocked on a "sleeping lock"
  17958. + * @p: The process to be woken up.
  17959. + *
  17960. + * Same as wake_up_process() above, but wake_flags=WF_LOCK_SLEEPER to indicate
  17961. + * the nature of the wakeup.
  17962. + */
  17963. +int wake_up_lock_sleeper(struct task_struct *p)
  17964. +{
  17965. + return try_to_wake_up(p, TASK_ALL, WF_LOCK_SLEEPER);
  17966. +}
  17967. +
  17968. int wake_up_state(struct task_struct *p, unsigned int state)
  17969. {
  17970. return try_to_wake_up(p, state, 0);
  17971. @@ -1959,6 +2052,9 @@
  17972. p->on_cpu = 0;
  17973. #endif
  17974. init_task_preempt_count(p);
  17975. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  17976. + task_thread_info(p)->preempt_lazy_count = 0;
  17977. +#endif
  17978. #ifdef CONFIG_SMP
  17979. plist_node_init(&p->pushable_tasks, MAX_PRIO);
  17980. RB_CLEAR_NODE(&p->pushable_dl_tasks);
  17981. @@ -2094,7 +2190,7 @@
  17982. rq = __task_rq_lock(p);
  17983. activate_task(rq, p, 0);
  17984. p->on_rq = TASK_ON_RQ_QUEUED;
  17985. - trace_sched_wakeup_new(p, true);
  17986. + trace_sched_wakeup_new(p);
  17987. check_preempt_curr(rq, p, WF_FORK);
  17988. #ifdef CONFIG_SMP
  17989. if (p->sched_class->task_woken)
  17990. @@ -2231,8 +2327,12 @@
  17991. finish_arch_post_lock_switch();
  17992. fire_sched_in_preempt_notifiers(current);
  17993. + /*
  17994. + * We use mmdrop_delayed() here so we don't have to do the
  17995. + * full __mmdrop() when we are the last user.
  17996. + */
  17997. if (mm)
  17998. - mmdrop(mm);
  17999. + mmdrop_delayed(mm);
  18000. if (unlikely(prev_state == TASK_DEAD)) {
  18001. if (prev->sched_class->task_dead)
  18002. prev->sched_class->task_dead(prev);
  18003. @@ -2543,16 +2643,6 @@
  18004. }
  18005. #endif
  18006. -notrace unsigned long get_parent_ip(unsigned long addr)
  18007. -{
  18008. - if (in_lock_functions(addr)) {
  18009. - addr = CALLER_ADDR2;
  18010. - if (in_lock_functions(addr))
  18011. - addr = CALLER_ADDR3;
  18012. - }
  18013. - return addr;
  18014. -}
  18015. -
  18016. #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
  18017. defined(CONFIG_PREEMPT_TRACER))
  18018. @@ -2574,7 +2664,7 @@
  18019. PREEMPT_MASK - 10);
  18020. #endif
  18021. if (preempt_count() == val) {
  18022. - unsigned long ip = get_parent_ip(CALLER_ADDR1);
  18023. + unsigned long ip = get_lock_parent_ip();
  18024. #ifdef CONFIG_DEBUG_PREEMPT
  18025. current->preempt_disable_ip = ip;
  18026. #endif
  18027. @@ -2601,7 +2691,7 @@
  18028. #endif
  18029. if (preempt_count() == val)
  18030. - trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  18031. + trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
  18032. __preempt_count_sub(val);
  18033. }
  18034. EXPORT_SYMBOL(preempt_count_sub);
  18035. @@ -2657,6 +2747,133 @@
  18036. schedstat_inc(this_rq(), sched_count);
  18037. }
  18038. +#if defined(CONFIG_PREEMPT_RT_FULL) && defined(CONFIG_SMP)
  18039. +#define MIGRATE_DISABLE_SET_AFFIN (1<<30) /* Can't make a negative */
  18040. +#define migrate_disabled_updated(p) ((p)->migrate_disable & MIGRATE_DISABLE_SET_AFFIN)
  18041. +#define migrate_disable_count(p) ((p)->migrate_disable & ~MIGRATE_DISABLE_SET_AFFIN)
  18042. +
  18043. +static inline void update_migrate_disable(struct task_struct *p)
  18044. +{
  18045. + const struct cpumask *mask;
  18046. +
  18047. + if (likely(!p->migrate_disable))
  18048. + return;
  18049. +
  18050. + /* Did we already update affinity? */
  18051. + if (unlikely(migrate_disabled_updated(p)))
  18052. + return;
  18053. +
  18054. + /*
  18055. + * Since this is always current we can get away with only locking
  18056. + * rq->lock, the ->cpus_allowed value can normally only be changed
  18057. + * while holding both p->pi_lock and rq->lock, but seeing that this
  18058. + * is current, we cannot actually be waking up, so all code that
  18059. + * relies on serialization against p->pi_lock is out of scope.
  18060. + *
  18061. + * Having rq->lock serializes us against things like
  18062. + * set_cpus_allowed_ptr() that can still happen concurrently.
  18063. + */
  18064. + mask = tsk_cpus_allowed(p);
  18065. +
  18066. + if (p->sched_class->set_cpus_allowed)
  18067. + p->sched_class->set_cpus_allowed(p, mask);
  18068. + /* mask==cpumask_of(task_cpu(p)) which has a cpumask_weight==1 */
  18069. + p->nr_cpus_allowed = 1;
  18070. +
  18071. + /* Let migrate_enable know to fix things back up */
  18072. + p->migrate_disable |= MIGRATE_DISABLE_SET_AFFIN;
  18073. +}
  18074. +
  18075. +void migrate_disable(void)
  18076. +{
  18077. + struct task_struct *p = current;
  18078. +
  18079. + if (in_atomic() || irqs_disabled()) {
  18080. +#ifdef CONFIG_SCHED_DEBUG
  18081. + p->migrate_disable_atomic++;
  18082. +#endif
  18083. + return;
  18084. + }
  18085. +
  18086. +#ifdef CONFIG_SCHED_DEBUG
  18087. + if (unlikely(p->migrate_disable_atomic)) {
  18088. + tracing_off();
  18089. + WARN_ON_ONCE(1);
  18090. + }
  18091. +#endif
  18092. +
  18093. + if (p->migrate_disable) {
  18094. + p->migrate_disable++;
  18095. + return;
  18096. + }
  18097. +
  18098. + preempt_disable();
  18099. + preempt_lazy_disable();
  18100. + pin_current_cpu();
  18101. + p->migrate_disable = 1;
  18102. + preempt_enable();
  18103. +}
  18104. +EXPORT_SYMBOL(migrate_disable);
  18105. +
  18106. +void migrate_enable(void)
  18107. +{
  18108. + struct task_struct *p = current;
  18109. + const struct cpumask *mask;
  18110. + unsigned long flags;
  18111. + struct rq *rq;
  18112. +
  18113. + if (in_atomic() || irqs_disabled()) {
  18114. +#ifdef CONFIG_SCHED_DEBUG
  18115. + p->migrate_disable_atomic--;
  18116. +#endif
  18117. + return;
  18118. + }
  18119. +
  18120. +#ifdef CONFIG_SCHED_DEBUG
  18121. + if (unlikely(p->migrate_disable_atomic)) {
  18122. + tracing_off();
  18123. + WARN_ON_ONCE(1);
  18124. + }
  18125. +#endif
  18126. + WARN_ON_ONCE(p->migrate_disable <= 0);
  18127. +
  18128. + if (migrate_disable_count(p) > 1) {
  18129. + p->migrate_disable--;
  18130. + return;
  18131. + }
  18132. +
  18133. + preempt_disable();
  18134. + if (unlikely(migrate_disabled_updated(p))) {
  18135. + /*
  18136. + * Undo whatever update_migrate_disable() did, also see there
  18137. + * about locking.
  18138. + */
  18139. + rq = this_rq();
  18140. + raw_spin_lock_irqsave(&rq->lock, flags);
  18141. +
  18142. + /*
  18143. + * Clearing migrate_disable causes tsk_cpus_allowed to
  18144. + * show the tasks original cpu affinity.
  18145. + */
  18146. + p->migrate_disable = 0;
  18147. + mask = tsk_cpus_allowed(p);
  18148. + if (p->sched_class->set_cpus_allowed)
  18149. + p->sched_class->set_cpus_allowed(p, mask);
  18150. + p->nr_cpus_allowed = cpumask_weight(mask);
  18151. + raw_spin_unlock_irqrestore(&rq->lock, flags);
  18152. + } else
  18153. + p->migrate_disable = 0;
  18154. +
  18155. + unpin_current_cpu();
  18156. + preempt_enable();
  18157. + preempt_lazy_enable();
  18158. +}
  18159. +EXPORT_SYMBOL(migrate_enable);
  18160. +#else
  18161. +static inline void update_migrate_disable(struct task_struct *p) { }
  18162. +#define migrate_disabled_updated(p) 0
  18163. +#endif
  18164. +
  18165. /*
  18166. * Pick up the highest-prio task:
  18167. */
  18168. @@ -2763,6 +2980,8 @@
  18169. smp_mb__before_spinlock();
  18170. raw_spin_lock_irq(&rq->lock);
  18171. + update_migrate_disable(prev);
  18172. +
  18173. rq->clock_skip_update <<= 1; /* promote REQ to ACT */
  18174. switch_count = &prev->nivcsw;
  18175. @@ -2772,19 +2991,6 @@
  18176. } else {
  18177. deactivate_task(rq, prev, DEQUEUE_SLEEP);
  18178. prev->on_rq = 0;
  18179. -
  18180. - /*
  18181. - * If a worker went to sleep, notify and ask workqueue
  18182. - * whether it wants to wake up a task to maintain
  18183. - * concurrency.
  18184. - */
  18185. - if (prev->flags & PF_WQ_WORKER) {
  18186. - struct task_struct *to_wakeup;
  18187. -
  18188. - to_wakeup = wq_worker_sleeping(prev, cpu);
  18189. - if (to_wakeup)
  18190. - try_to_wake_up_local(to_wakeup);
  18191. - }
  18192. }
  18193. switch_count = &prev->nvcsw;
  18194. }
  18195. @@ -2794,6 +3000,7 @@
  18196. next = pick_next_task(rq, prev);
  18197. clear_tsk_need_resched(prev);
  18198. + clear_tsk_need_resched_lazy(prev);
  18199. clear_preempt_need_resched();
  18200. rq->clock_skip_update = 0;
  18201. @@ -2814,8 +3021,19 @@
  18202. static inline void sched_submit_work(struct task_struct *tsk)
  18203. {
  18204. - if (!tsk->state || tsk_is_pi_blocked(tsk))
  18205. + if (!tsk->state)
  18206. + return;
  18207. + /*
  18208. + * If a worker went to sleep, notify and ask workqueue whether
  18209. + * it wants to wake up a task to maintain concurrency.
  18210. + */
  18211. + if (tsk->flags & PF_WQ_WORKER)
  18212. + wq_worker_sleeping(tsk);
  18213. +
  18214. +
  18215. + if (tsk_is_pi_blocked(tsk))
  18216. return;
  18217. +
  18218. /*
  18219. * If we are going to sleep and we have plugged IO queued,
  18220. * make sure to submit it to avoid deadlocks.
  18221. @@ -2824,6 +3042,12 @@
  18222. blk_schedule_flush_plug(tsk);
  18223. }
  18224. +static void sched_update_worker(struct task_struct *tsk)
  18225. +{
  18226. + if (tsk->flags & PF_WQ_WORKER)
  18227. + wq_worker_running(tsk);
  18228. +}
  18229. +
  18230. asmlinkage __visible void __sched schedule(void)
  18231. {
  18232. struct task_struct *tsk = current;
  18233. @@ -2832,6 +3056,7 @@
  18234. do {
  18235. __schedule();
  18236. } while (need_resched());
  18237. + sched_update_worker(tsk);
  18238. }
  18239. EXPORT_SYMBOL(schedule);
  18240. @@ -2881,6 +3106,30 @@
  18241. } while (need_resched());
  18242. }
  18243. +#ifdef CONFIG_PREEMPT_LAZY
  18244. +/*
  18245. + * If TIF_NEED_RESCHED is then we allow to be scheduled away since this is
  18246. + * set by a RT task. Oterwise we try to avoid beeing scheduled out as long as
  18247. + * preempt_lazy_count counter >0.
  18248. + */
  18249. +static __always_inline int preemptible_lazy(void)
  18250. +{
  18251. + if (test_thread_flag(TIF_NEED_RESCHED))
  18252. + return 1;
  18253. + if (current_thread_info()->preempt_lazy_count)
  18254. + return 0;
  18255. + return 1;
  18256. +}
  18257. +
  18258. +#else
  18259. +
  18260. +static int preemptible_lazy(void)
  18261. +{
  18262. + return 1;
  18263. +}
  18264. +
  18265. +#endif
  18266. +
  18267. #ifdef CONFIG_PREEMPT
  18268. /*
  18269. * this is the entry point to schedule() from in-kernel preemption
  18270. @@ -2895,6 +3144,8 @@
  18271. */
  18272. if (likely(!preemptible()))
  18273. return;
  18274. + if (!preemptible_lazy())
  18275. + return;
  18276. preempt_schedule_common();
  18277. }
  18278. @@ -2922,6 +3173,8 @@
  18279. if (likely(!preemptible()))
  18280. return;
  18281. + if (!preemptible_lazy())
  18282. + return;
  18283. do {
  18284. __preempt_count_add(PREEMPT_ACTIVE);
  18285. @@ -2931,7 +3184,16 @@
  18286. * an infinite recursion.
  18287. */
  18288. prev_ctx = exception_enter();
  18289. + /*
  18290. + * The add/subtract must not be traced by the function
  18291. + * tracer. But we still want to account for the
  18292. + * preempt off latency tracer. Since the _notrace versions
  18293. + * of add/subtract skip the accounting for latency tracer
  18294. + * we must force it manually.
  18295. + */
  18296. + start_critical_timings();
  18297. __schedule();
  18298. + stop_critical_timings();
  18299. exception_exit(prev_ctx);
  18300. __preempt_count_sub(PREEMPT_ACTIVE);
  18301. @@ -4268,6 +4530,7 @@
  18302. }
  18303. EXPORT_SYMBOL(__cond_resched_lock);
  18304. +#ifndef CONFIG_PREEMPT_RT_FULL
  18305. int __sched __cond_resched_softirq(void)
  18306. {
  18307. BUG_ON(!in_softirq());
  18308. @@ -4281,6 +4544,7 @@
  18309. return 0;
  18310. }
  18311. EXPORT_SYMBOL(__cond_resched_softirq);
  18312. +#endif
  18313. /**
  18314. * yield - yield the current processor to other threads.
  18315. @@ -4635,7 +4899,9 @@
  18316. /* Set the preempt count _outside_ the spinlocks! */
  18317. init_idle_preempt_count(idle, cpu);
  18318. -
  18319. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  18320. + task_thread_info(idle)->preempt_lazy_count = 0;
  18321. +#endif
  18322. /*
  18323. * The idle tasks have their own, simple scheduling class:
  18324. */
  18325. @@ -4755,11 +5021,91 @@
  18326. void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  18327. {
  18328. - if (p->sched_class->set_cpus_allowed)
  18329. - p->sched_class->set_cpus_allowed(p, new_mask);
  18330. + if (!migrate_disabled_updated(p)) {
  18331. + if (p->sched_class->set_cpus_allowed)
  18332. + p->sched_class->set_cpus_allowed(p, new_mask);
  18333. + p->nr_cpus_allowed = cpumask_weight(new_mask);
  18334. + }
  18335. cpumask_copy(&p->cpus_allowed, new_mask);
  18336. - p->nr_cpus_allowed = cpumask_weight(new_mask);
  18337. +}
  18338. +
  18339. +static DEFINE_PER_CPU(struct cpumask, sched_cpumasks);
  18340. +static DEFINE_MUTEX(sched_down_mutex);
  18341. +static cpumask_t sched_down_cpumask;
  18342. +
  18343. +void tell_sched_cpu_down_begin(int cpu)
  18344. +{
  18345. + mutex_lock(&sched_down_mutex);
  18346. + cpumask_set_cpu(cpu, &sched_down_cpumask);
  18347. + mutex_unlock(&sched_down_mutex);
  18348. +}
  18349. +
  18350. +void tell_sched_cpu_down_done(int cpu)
  18351. +{
  18352. + mutex_lock(&sched_down_mutex);
  18353. + cpumask_clear_cpu(cpu, &sched_down_cpumask);
  18354. + mutex_unlock(&sched_down_mutex);
  18355. +}
  18356. +
  18357. +/**
  18358. + * migrate_me - try to move the current task off this cpu
  18359. + *
  18360. + * Used by the pin_current_cpu() code to try to get tasks
  18361. + * to move off the current CPU as it is going down.
  18362. + * It will only move the task if the task isn't pinned to
  18363. + * the CPU (with migrate_disable, affinity or NO_SETAFFINITY)
  18364. + * and the task has to be in a RUNNING state. Otherwise the
  18365. + * movement of the task will wake it up (change its state
  18366. + * to running) when the task did not expect it.
  18367. + *
  18368. + * Returns 1 if it succeeded in moving the current task
  18369. + * 0 otherwise.
  18370. + */
  18371. +int migrate_me(void)
  18372. +{
  18373. + struct task_struct *p = current;
  18374. + struct migration_arg arg;
  18375. + struct cpumask *cpumask;
  18376. + struct cpumask *mask;
  18377. + unsigned long flags;
  18378. + unsigned int dest_cpu;
  18379. + struct rq *rq;
  18380. +
  18381. + /*
  18382. + * We can not migrate tasks bounded to a CPU or tasks not
  18383. + * running. The movement of the task will wake it up.
  18384. + */
  18385. + if (p->flags & PF_NO_SETAFFINITY || p->state)
  18386. + return 0;
  18387. +
  18388. + mutex_lock(&sched_down_mutex);
  18389. + rq = task_rq_lock(p, &flags);
  18390. +
  18391. + cpumask = this_cpu_ptr(&sched_cpumasks);
  18392. + mask = &p->cpus_allowed;
  18393. +
  18394. + cpumask_andnot(cpumask, mask, &sched_down_cpumask);
  18395. +
  18396. + if (!cpumask_weight(cpumask)) {
  18397. + /* It's only on this CPU? */
  18398. + task_rq_unlock(rq, p, &flags);
  18399. + mutex_unlock(&sched_down_mutex);
  18400. + return 0;
  18401. + }
  18402. +
  18403. + dest_cpu = cpumask_any_and(cpu_active_mask, cpumask);
  18404. +
  18405. + arg.task = p;
  18406. + arg.dest_cpu = dest_cpu;
  18407. +
  18408. + task_rq_unlock(rq, p, &flags);
  18409. +
  18410. + stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
  18411. + tlb_migrate_finish(p->mm);
  18412. + mutex_unlock(&sched_down_mutex);
  18413. +
  18414. + return 1;
  18415. }
  18416. /*
  18417. @@ -4805,7 +5151,7 @@
  18418. do_set_cpus_allowed(p, new_mask);
  18419. /* Can the task run on the task's current CPU? If so, we're done */
  18420. - if (cpumask_test_cpu(task_cpu(p), new_mask))
  18421. + if (cpumask_test_cpu(task_cpu(p), new_mask) || __migrate_disabled(p))
  18422. goto out;
  18423. dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
  18424. @@ -4945,6 +5291,8 @@
  18425. #ifdef CONFIG_HOTPLUG_CPU
  18426. +static DEFINE_PER_CPU(struct mm_struct *, idle_last_mm);
  18427. +
  18428. /*
  18429. * Ensures that the idle task is using init_mm right before its cpu goes
  18430. * offline.
  18431. @@ -4959,7 +5307,11 @@
  18432. switch_mm(mm, &init_mm, current);
  18433. finish_arch_post_lock_switch();
  18434. }
  18435. - mmdrop(mm);
  18436. + /*
  18437. + * Defer the cleanup to an alive cpu. On RT we can neither
  18438. + * call mmdrop() nor mmdrop_delayed() from here.
  18439. + */
  18440. + per_cpu(idle_last_mm, smp_processor_id()) = mm;
  18441. }
  18442. /*
  18443. @@ -5302,6 +5654,10 @@
  18444. case CPU_DEAD:
  18445. calc_load_migrate(rq);
  18446. + if (per_cpu(idle_last_mm, cpu)) {
  18447. + mmdrop(per_cpu(idle_last_mm, cpu));
  18448. + per_cpu(idle_last_mm, cpu) = NULL;
  18449. + }
  18450. break;
  18451. #endif
  18452. }
  18453. @@ -7281,7 +7637,8 @@
  18454. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  18455. static inline int preempt_count_equals(int preempt_offset)
  18456. {
  18457. - int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
  18458. + int nested = (preempt_count() & ~PREEMPT_ACTIVE) +
  18459. + sched_rcu_preempt_depth();
  18460. return (nested == preempt_offset);
  18461. }
  18462. diff -Nur linux-4.1.20.orig/kernel/sched/cputime.c linux-4.1.20/kernel/sched/cputime.c
  18463. --- linux-4.1.20.orig/kernel/sched/cputime.c 2016-03-17 19:11:03.000000000 +0100
  18464. +++ linux-4.1.20/kernel/sched/cputime.c 2016-03-21 20:18:33.000000000 +0100
  18465. @@ -675,37 +675,45 @@
  18466. void vtime_account_system(struct task_struct *tsk)
  18467. {
  18468. - write_seqlock(&tsk->vtime_seqlock);
  18469. + raw_spin_lock(&tsk->vtime_lock);
  18470. + write_seqcount_begin(&tsk->vtime_seq);
  18471. __vtime_account_system(tsk);
  18472. - write_sequnlock(&tsk->vtime_seqlock);
  18473. + write_seqcount_end(&tsk->vtime_seq);
  18474. + raw_spin_unlock(&tsk->vtime_lock);
  18475. }
  18476. void vtime_gen_account_irq_exit(struct task_struct *tsk)
  18477. {
  18478. - write_seqlock(&tsk->vtime_seqlock);
  18479. + raw_spin_lock(&tsk->vtime_lock);
  18480. + write_seqcount_begin(&tsk->vtime_seq);
  18481. __vtime_account_system(tsk);
  18482. if (context_tracking_in_user())
  18483. tsk->vtime_snap_whence = VTIME_USER;
  18484. - write_sequnlock(&tsk->vtime_seqlock);
  18485. + write_seqcount_end(&tsk->vtime_seq);
  18486. + raw_spin_unlock(&tsk->vtime_lock);
  18487. }
  18488. void vtime_account_user(struct task_struct *tsk)
  18489. {
  18490. cputime_t delta_cpu;
  18491. - write_seqlock(&tsk->vtime_seqlock);
  18492. + raw_spin_lock(&tsk->vtime_lock);
  18493. + write_seqcount_begin(&tsk->vtime_seq);
  18494. delta_cpu = get_vtime_delta(tsk);
  18495. tsk->vtime_snap_whence = VTIME_SYS;
  18496. account_user_time(tsk, delta_cpu, cputime_to_scaled(delta_cpu));
  18497. - write_sequnlock(&tsk->vtime_seqlock);
  18498. + write_seqcount_end(&tsk->vtime_seq);
  18499. + raw_spin_unlock(&tsk->vtime_lock);
  18500. }
  18501. void vtime_user_enter(struct task_struct *tsk)
  18502. {
  18503. - write_seqlock(&tsk->vtime_seqlock);
  18504. + raw_spin_lock(&tsk->vtime_lock);
  18505. + write_seqcount_begin(&tsk->vtime_seq);
  18506. __vtime_account_system(tsk);
  18507. tsk->vtime_snap_whence = VTIME_USER;
  18508. - write_sequnlock(&tsk->vtime_seqlock);
  18509. + write_seqcount_end(&tsk->vtime_seq);
  18510. + raw_spin_unlock(&tsk->vtime_lock);
  18511. }
  18512. void vtime_guest_enter(struct task_struct *tsk)
  18513. @@ -717,19 +725,23 @@
  18514. * synchronization against the reader (task_gtime())
  18515. * that can thus safely catch up with a tickless delta.
  18516. */
  18517. - write_seqlock(&tsk->vtime_seqlock);
  18518. + raw_spin_lock(&tsk->vtime_lock);
  18519. + write_seqcount_begin(&tsk->vtime_seq);
  18520. __vtime_account_system(tsk);
  18521. current->flags |= PF_VCPU;
  18522. - write_sequnlock(&tsk->vtime_seqlock);
  18523. + write_seqcount_end(&tsk->vtime_seq);
  18524. + raw_spin_unlock(&tsk->vtime_lock);
  18525. }
  18526. EXPORT_SYMBOL_GPL(vtime_guest_enter);
  18527. void vtime_guest_exit(struct task_struct *tsk)
  18528. {
  18529. - write_seqlock(&tsk->vtime_seqlock);
  18530. + raw_spin_lock(&tsk->vtime_lock);
  18531. + write_seqcount_begin(&tsk->vtime_seq);
  18532. __vtime_account_system(tsk);
  18533. current->flags &= ~PF_VCPU;
  18534. - write_sequnlock(&tsk->vtime_seqlock);
  18535. + write_seqcount_end(&tsk->vtime_seq);
  18536. + raw_spin_unlock(&tsk->vtime_lock);
  18537. }
  18538. EXPORT_SYMBOL_GPL(vtime_guest_exit);
  18539. @@ -742,24 +754,30 @@
  18540. void arch_vtime_task_switch(struct task_struct *prev)
  18541. {
  18542. - write_seqlock(&prev->vtime_seqlock);
  18543. + raw_spin_lock(&prev->vtime_lock);
  18544. + write_seqcount_begin(&prev->vtime_seq);
  18545. prev->vtime_snap_whence = VTIME_SLEEPING;
  18546. - write_sequnlock(&prev->vtime_seqlock);
  18547. + write_seqcount_end(&prev->vtime_seq);
  18548. + raw_spin_unlock(&prev->vtime_lock);
  18549. - write_seqlock(&current->vtime_seqlock);
  18550. + raw_spin_lock(&current->vtime_lock);
  18551. + write_seqcount_begin(&current->vtime_seq);
  18552. current->vtime_snap_whence = VTIME_SYS;
  18553. current->vtime_snap = sched_clock_cpu(smp_processor_id());
  18554. - write_sequnlock(&current->vtime_seqlock);
  18555. + write_seqcount_end(&current->vtime_seq);
  18556. + raw_spin_unlock(&current->vtime_lock);
  18557. }
  18558. void vtime_init_idle(struct task_struct *t, int cpu)
  18559. {
  18560. unsigned long flags;
  18561. - write_seqlock_irqsave(&t->vtime_seqlock, flags);
  18562. + raw_spin_lock_irqsave(&t->vtime_lock, flags);
  18563. + write_seqcount_begin(&t->vtime_seq);
  18564. t->vtime_snap_whence = VTIME_SYS;
  18565. t->vtime_snap = sched_clock_cpu(cpu);
  18566. - write_sequnlock_irqrestore(&t->vtime_seqlock, flags);
  18567. + write_seqcount_end(&t->vtime_seq);
  18568. + raw_spin_unlock_irqrestore(&t->vtime_lock, flags);
  18569. }
  18570. cputime_t task_gtime(struct task_struct *t)
  18571. @@ -768,13 +786,13 @@
  18572. cputime_t gtime;
  18573. do {
  18574. - seq = read_seqbegin(&t->vtime_seqlock);
  18575. + seq = read_seqcount_begin(&t->vtime_seq);
  18576. gtime = t->gtime;
  18577. if (t->flags & PF_VCPU)
  18578. gtime += vtime_delta(t);
  18579. - } while (read_seqretry(&t->vtime_seqlock, seq));
  18580. + } while (read_seqcount_retry(&t->vtime_seq, seq));
  18581. return gtime;
  18582. }
  18583. @@ -797,7 +815,7 @@
  18584. *udelta = 0;
  18585. *sdelta = 0;
  18586. - seq = read_seqbegin(&t->vtime_seqlock);
  18587. + seq = read_seqcount_begin(&t->vtime_seq);
  18588. if (u_dst)
  18589. *u_dst = *u_src;
  18590. @@ -821,7 +839,7 @@
  18591. if (t->vtime_snap_whence == VTIME_SYS)
  18592. *sdelta = delta;
  18593. }
  18594. - } while (read_seqretry(&t->vtime_seqlock, seq));
  18595. + } while (read_seqcount_retry(&t->vtime_seq, seq));
  18596. }
  18597. diff -Nur linux-4.1.20.orig/kernel/sched/deadline.c linux-4.1.20/kernel/sched/deadline.c
  18598. --- linux-4.1.20.orig/kernel/sched/deadline.c 2016-03-17 19:11:03.000000000 +0100
  18599. +++ linux-4.1.20/kernel/sched/deadline.c 2016-03-21 20:18:33.000000000 +0100
  18600. @@ -637,6 +637,7 @@
  18601. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  18602. timer->function = dl_task_timer;
  18603. + timer->irqsafe = 1;
  18604. }
  18605. static
  18606. diff -Nur linux-4.1.20.orig/kernel/sched/debug.c linux-4.1.20/kernel/sched/debug.c
  18607. --- linux-4.1.20.orig/kernel/sched/debug.c 2016-03-17 19:11:03.000000000 +0100
  18608. +++ linux-4.1.20/kernel/sched/debug.c 2016-03-21 20:18:33.000000000 +0100
  18609. @@ -260,6 +260,9 @@
  18610. P(rt_throttled);
  18611. PN(rt_time);
  18612. PN(rt_runtime);
  18613. +#ifdef CONFIG_SMP
  18614. + P(rt_nr_migratory);
  18615. +#endif
  18616. #undef PN
  18617. #undef P
  18618. @@ -648,6 +651,10 @@
  18619. #endif
  18620. P(policy);
  18621. P(prio);
  18622. +#ifdef CONFIG_PREEMPT_RT_FULL
  18623. + P(migrate_disable);
  18624. +#endif
  18625. + P(nr_cpus_allowed);
  18626. #undef PN
  18627. #undef __PN
  18628. #undef P
  18629. diff -Nur linux-4.1.20.orig/kernel/sched/fair.c linux-4.1.20/kernel/sched/fair.c
  18630. --- linux-4.1.20.orig/kernel/sched/fair.c 2016-03-17 19:11:03.000000000 +0100
  18631. +++ linux-4.1.20/kernel/sched/fair.c 2016-03-21 20:18:33.000000000 +0100
  18632. @@ -3201,7 +3201,7 @@
  18633. ideal_runtime = sched_slice(cfs_rq, curr);
  18634. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  18635. if (delta_exec > ideal_runtime) {
  18636. - resched_curr(rq_of(cfs_rq));
  18637. + resched_curr_lazy(rq_of(cfs_rq));
  18638. /*
  18639. * The current task ran long enough, ensure it doesn't get
  18640. * re-elected due to buddy favours.
  18641. @@ -3225,7 +3225,7 @@
  18642. return;
  18643. if (delta > ideal_runtime)
  18644. - resched_curr(rq_of(cfs_rq));
  18645. + resched_curr_lazy(rq_of(cfs_rq));
  18646. }
  18647. static void
  18648. @@ -3366,7 +3366,7 @@
  18649. * validating it and just reschedule.
  18650. */
  18651. if (queued) {
  18652. - resched_curr(rq_of(cfs_rq));
  18653. + resched_curr_lazy(rq_of(cfs_rq));
  18654. return;
  18655. }
  18656. /*
  18657. @@ -3557,7 +3557,7 @@
  18658. * hierarchy can be throttled
  18659. */
  18660. if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
  18661. - resched_curr(rq_of(cfs_rq));
  18662. + resched_curr_lazy(rq_of(cfs_rq));
  18663. }
  18664. static __always_inline
  18665. @@ -4180,7 +4180,7 @@
  18666. if (delta < 0) {
  18667. if (rq->curr == p)
  18668. - resched_curr(rq);
  18669. + resched_curr_lazy(rq);
  18670. return;
  18671. }
  18672. hrtick_start(rq, delta);
  18673. @@ -5076,7 +5076,7 @@
  18674. return;
  18675. preempt:
  18676. - resched_curr(rq);
  18677. + resched_curr_lazy(rq);
  18678. /*
  18679. * Only set the backward buddy when the current task is still
  18680. * on the rq. This can happen when a wakeup gets interleaved
  18681. @@ -7869,7 +7869,7 @@
  18682. * 'current' within the tree based on its new key value.
  18683. */
  18684. swap(curr->vruntime, se->vruntime);
  18685. - resched_curr(rq);
  18686. + resched_curr_lazy(rq);
  18687. }
  18688. se->vruntime -= cfs_rq->min_vruntime;
  18689. @@ -7894,7 +7894,7 @@
  18690. */
  18691. if (rq->curr == p) {
  18692. if (p->prio > oldprio)
  18693. - resched_curr(rq);
  18694. + resched_curr_lazy(rq);
  18695. } else
  18696. check_preempt_curr(rq, p, 0);
  18697. }
  18698. diff -Nur linux-4.1.20.orig/kernel/sched/features.h linux-4.1.20/kernel/sched/features.h
  18699. --- linux-4.1.20.orig/kernel/sched/features.h 2016-03-17 19:11:03.000000000 +0100
  18700. +++ linux-4.1.20/kernel/sched/features.h 2016-03-21 20:18:33.000000000 +0100
  18701. @@ -50,11 +50,19 @@
  18702. */
  18703. SCHED_FEAT(NONTASK_CAPACITY, true)
  18704. +#ifdef CONFIG_PREEMPT_RT_FULL
  18705. +SCHED_FEAT(TTWU_QUEUE, false)
  18706. +# ifdef CONFIG_PREEMPT_LAZY
  18707. +SCHED_FEAT(PREEMPT_LAZY, true)
  18708. +# endif
  18709. +#else
  18710. +
  18711. /*
  18712. * Queue remote wakeups on the target CPU and process them
  18713. * using the scheduler IPI. Reduces rq->lock contention/bounces.
  18714. */
  18715. SCHED_FEAT(TTWU_QUEUE, true)
  18716. +#endif
  18717. #ifdef HAVE_RT_PUSH_IPI
  18718. /*
  18719. diff -Nur linux-4.1.20.orig/kernel/sched/rt.c linux-4.1.20/kernel/sched/rt.c
  18720. --- linux-4.1.20.orig/kernel/sched/rt.c 2016-03-17 19:11:03.000000000 +0100
  18721. +++ linux-4.1.20/kernel/sched/rt.c 2016-03-21 20:18:33.000000000 +0100
  18722. @@ -44,6 +44,7 @@
  18723. hrtimer_init(&rt_b->rt_period_timer,
  18724. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  18725. + rt_b->rt_period_timer.irqsafe = 1;
  18726. rt_b->rt_period_timer.function = sched_rt_period_timer;
  18727. }
  18728. @@ -89,6 +90,7 @@
  18729. rt_rq->push_cpu = nr_cpu_ids;
  18730. raw_spin_lock_init(&rt_rq->push_lock);
  18731. init_irq_work(&rt_rq->push_work, push_irq_work_func);
  18732. + rt_rq->push_work.flags |= IRQ_WORK_HARD_IRQ;
  18733. #endif
  18734. #endif /* CONFIG_SMP */
  18735. /* We start is dequeued state, because no RT tasks are queued */
  18736. diff -Nur linux-4.1.20.orig/kernel/sched/sched.h linux-4.1.20/kernel/sched/sched.h
  18737. --- linux-4.1.20.orig/kernel/sched/sched.h 2016-03-17 19:11:03.000000000 +0100
  18738. +++ linux-4.1.20/kernel/sched/sched.h 2016-03-21 20:18:33.000000000 +0100
  18739. @@ -1093,6 +1093,7 @@
  18740. #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
  18741. #define WF_FORK 0x02 /* child wakeup after fork */
  18742. #define WF_MIGRATED 0x4 /* internal use, task got migrated */
  18743. +#define WF_LOCK_SLEEPER 0x08 /* wakeup spinlock "sleeper" */
  18744. /*
  18745. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  18746. @@ -1290,6 +1291,15 @@
  18747. extern void resched_curr(struct rq *rq);
  18748. extern void resched_cpu(int cpu);
  18749. +#ifdef CONFIG_PREEMPT_LAZY
  18750. +extern void resched_curr_lazy(struct rq *rq);
  18751. +#else
  18752. +static inline void resched_curr_lazy(struct rq *rq)
  18753. +{
  18754. + resched_curr(rq);
  18755. +}
  18756. +#endif
  18757. +
  18758. extern struct rt_bandwidth def_rt_bandwidth;
  18759. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  18760. diff -Nur linux-4.1.20.orig/kernel/sched/wait-simple.c linux-4.1.20/kernel/sched/wait-simple.c
  18761. --- linux-4.1.20.orig/kernel/sched/wait-simple.c 1970-01-01 01:00:00.000000000 +0100
  18762. +++ linux-4.1.20/kernel/sched/wait-simple.c 2016-03-21 20:18:33.000000000 +0100
  18763. @@ -0,0 +1,115 @@
  18764. +/*
  18765. + * Simple waitqueues without fancy flags and callbacks
  18766. + *
  18767. + * (C) 2011 Thomas Gleixner <tglx@linutronix.de>
  18768. + *
  18769. + * Based on kernel/wait.c
  18770. + *
  18771. + * For licencing details see kernel-base/COPYING
  18772. + */
  18773. +#include <linux/init.h>
  18774. +#include <linux/export.h>
  18775. +#include <linux/sched.h>
  18776. +#include <linux/wait-simple.h>
  18777. +
  18778. +/* Adds w to head->list. Must be called with head->lock locked. */
  18779. +static inline void __swait_enqueue(struct swait_head *head, struct swaiter *w)
  18780. +{
  18781. + list_add(&w->node, &head->list);
  18782. + /* We can't let the condition leak before the setting of head */
  18783. + smp_mb();
  18784. +}
  18785. +
  18786. +/* Removes w from head->list. Must be called with head->lock locked. */
  18787. +static inline void __swait_dequeue(struct swaiter *w)
  18788. +{
  18789. + list_del_init(&w->node);
  18790. +}
  18791. +
  18792. +void __init_swait_head(struct swait_head *head, struct lock_class_key *key)
  18793. +{
  18794. + raw_spin_lock_init(&head->lock);
  18795. + lockdep_set_class(&head->lock, key);
  18796. + INIT_LIST_HEAD(&head->list);
  18797. +}
  18798. +EXPORT_SYMBOL(__init_swait_head);
  18799. +
  18800. +void swait_prepare_locked(struct swait_head *head, struct swaiter *w)
  18801. +{
  18802. + w->task = current;
  18803. + if (list_empty(&w->node))
  18804. + __swait_enqueue(head, w);
  18805. +}
  18806. +
  18807. +void swait_prepare(struct swait_head *head, struct swaiter *w, int state)
  18808. +{
  18809. + unsigned long flags;
  18810. +
  18811. + raw_spin_lock_irqsave(&head->lock, flags);
  18812. + swait_prepare_locked(head, w);
  18813. + __set_current_state(state);
  18814. + raw_spin_unlock_irqrestore(&head->lock, flags);
  18815. +}
  18816. +EXPORT_SYMBOL(swait_prepare);
  18817. +
  18818. +void swait_finish_locked(struct swait_head *head, struct swaiter *w)
  18819. +{
  18820. + __set_current_state(TASK_RUNNING);
  18821. + if (w->task)
  18822. + __swait_dequeue(w);
  18823. +}
  18824. +
  18825. +void swait_finish(struct swait_head *head, struct swaiter *w)
  18826. +{
  18827. + unsigned long flags;
  18828. +
  18829. + __set_current_state(TASK_RUNNING);
  18830. + if (w->task) {
  18831. + raw_spin_lock_irqsave(&head->lock, flags);
  18832. + __swait_dequeue(w);
  18833. + raw_spin_unlock_irqrestore(&head->lock, flags);
  18834. + }
  18835. +}
  18836. +EXPORT_SYMBOL(swait_finish);
  18837. +
  18838. +unsigned int
  18839. +__swait_wake_locked(struct swait_head *head, unsigned int state, unsigned int num)
  18840. +{
  18841. + struct swaiter *curr, *next;
  18842. + int woken = 0;
  18843. +
  18844. + list_for_each_entry_safe(curr, next, &head->list, node) {
  18845. + if (wake_up_state(curr->task, state)) {
  18846. + __swait_dequeue(curr);
  18847. + /*
  18848. + * The waiting task can free the waiter as
  18849. + * soon as curr->task = NULL is written,
  18850. + * without taking any locks. A memory barrier
  18851. + * is required here to prevent the following
  18852. + * store to curr->task from getting ahead of
  18853. + * the dequeue operation.
  18854. + */
  18855. + smp_wmb();
  18856. + curr->task = NULL;
  18857. + if (++woken == num)
  18858. + break;
  18859. + }
  18860. + }
  18861. + return woken;
  18862. +}
  18863. +
  18864. +unsigned int
  18865. +__swait_wake(struct swait_head *head, unsigned int state, unsigned int num)
  18866. +{
  18867. + unsigned long flags;
  18868. + int woken;
  18869. +
  18870. + if (!swaitqueue_active(head))
  18871. + return 0;
  18872. +
  18873. + raw_spin_lock_irqsave(&head->lock, flags);
  18874. + woken = __swait_wake_locked(head, state, num);
  18875. + raw_spin_unlock_irqrestore(&head->lock, flags);
  18876. + return woken;
  18877. +}
  18878. +EXPORT_SYMBOL(__swait_wake);
  18879. diff -Nur linux-4.1.20.orig/kernel/sched/work-simple.c linux-4.1.20/kernel/sched/work-simple.c
  18880. --- linux-4.1.20.orig/kernel/sched/work-simple.c 1970-01-01 01:00:00.000000000 +0100
  18881. +++ linux-4.1.20/kernel/sched/work-simple.c 2016-03-21 20:18:33.000000000 +0100
  18882. @@ -0,0 +1,173 @@
  18883. +/*
  18884. + * Copyright (C) 2014 BMW Car IT GmbH, Daniel Wagner daniel.wagner@bmw-carit.de
  18885. + *
  18886. + * Provides a framework for enqueuing callbacks from irq context
  18887. + * PREEMPT_RT_FULL safe. The callbacks are executed in kthread context.
  18888. + */
  18889. +
  18890. +#include <linux/wait-simple.h>
  18891. +#include <linux/work-simple.h>
  18892. +#include <linux/kthread.h>
  18893. +#include <linux/slab.h>
  18894. +#include <linux/spinlock.h>
  18895. +#include <linux/export.h>
  18896. +
  18897. +#define SWORK_EVENT_PENDING (1 << 0)
  18898. +
  18899. +static DEFINE_MUTEX(worker_mutex);
  18900. +static struct sworker *glob_worker;
  18901. +
  18902. +struct sworker {
  18903. + struct list_head events;
  18904. + struct swait_head wq;
  18905. +
  18906. + raw_spinlock_t lock;
  18907. +
  18908. + struct task_struct *task;
  18909. + int refs;
  18910. +};
  18911. +
  18912. +static bool swork_readable(struct sworker *worker)
  18913. +{
  18914. + bool r;
  18915. +
  18916. + if (kthread_should_stop())
  18917. + return true;
  18918. +
  18919. + raw_spin_lock_irq(&worker->lock);
  18920. + r = !list_empty(&worker->events);
  18921. + raw_spin_unlock_irq(&worker->lock);
  18922. +
  18923. + return r;
  18924. +}
  18925. +
  18926. +static int swork_kthread(void *arg)
  18927. +{
  18928. + struct sworker *worker = arg;
  18929. +
  18930. + for (;;) {
  18931. + swait_event_interruptible(worker->wq,
  18932. + swork_readable(worker));
  18933. + if (kthread_should_stop())
  18934. + break;
  18935. +
  18936. + raw_spin_lock_irq(&worker->lock);
  18937. + while (!list_empty(&worker->events)) {
  18938. + struct swork_event *sev;
  18939. +
  18940. + sev = list_first_entry(&worker->events,
  18941. + struct swork_event, item);
  18942. + list_del(&sev->item);
  18943. + raw_spin_unlock_irq(&worker->lock);
  18944. +
  18945. + WARN_ON_ONCE(!test_and_clear_bit(SWORK_EVENT_PENDING,
  18946. + &sev->flags));
  18947. + sev->func(sev);
  18948. + raw_spin_lock_irq(&worker->lock);
  18949. + }
  18950. + raw_spin_unlock_irq(&worker->lock);
  18951. + }
  18952. + return 0;
  18953. +}
  18954. +
  18955. +static struct sworker *swork_create(void)
  18956. +{
  18957. + struct sworker *worker;
  18958. +
  18959. + worker = kzalloc(sizeof(*worker), GFP_KERNEL);
  18960. + if (!worker)
  18961. + return ERR_PTR(-ENOMEM);
  18962. +
  18963. + INIT_LIST_HEAD(&worker->events);
  18964. + raw_spin_lock_init(&worker->lock);
  18965. + init_swait_head(&worker->wq);
  18966. +
  18967. + worker->task = kthread_run(swork_kthread, worker, "kswork");
  18968. + if (IS_ERR(worker->task)) {
  18969. + kfree(worker);
  18970. + return ERR_PTR(-ENOMEM);
  18971. + }
  18972. +
  18973. + return worker;
  18974. +}
  18975. +
  18976. +static void swork_destroy(struct sworker *worker)
  18977. +{
  18978. + kthread_stop(worker->task);
  18979. +
  18980. + WARN_ON(!list_empty(&worker->events));
  18981. + kfree(worker);
  18982. +}
  18983. +
  18984. +/**
  18985. + * swork_queue - queue swork
  18986. + *
  18987. + * Returns %false if @work was already on a queue, %true otherwise.
  18988. + *
  18989. + * The work is queued and processed on a random CPU
  18990. + */
  18991. +bool swork_queue(struct swork_event *sev)
  18992. +{
  18993. + unsigned long flags;
  18994. +
  18995. + if (test_and_set_bit(SWORK_EVENT_PENDING, &sev->flags))
  18996. + return false;
  18997. +
  18998. + raw_spin_lock_irqsave(&glob_worker->lock, flags);
  18999. + list_add_tail(&sev->item, &glob_worker->events);
  19000. + raw_spin_unlock_irqrestore(&glob_worker->lock, flags);
  19001. +
  19002. + swait_wake(&glob_worker->wq);
  19003. + return true;
  19004. +}
  19005. +EXPORT_SYMBOL_GPL(swork_queue);
  19006. +
  19007. +/**
  19008. + * swork_get - get an instance of the sworker
  19009. + *
  19010. + * Returns an negative error code if the initialization if the worker did not
  19011. + * work, %0 otherwise.
  19012. + *
  19013. + */
  19014. +int swork_get(void)
  19015. +{
  19016. + struct sworker *worker;
  19017. +
  19018. + mutex_lock(&worker_mutex);
  19019. + if (!glob_worker) {
  19020. + worker = swork_create();
  19021. + if (IS_ERR(worker)) {
  19022. + mutex_unlock(&worker_mutex);
  19023. + return -ENOMEM;
  19024. + }
  19025. +
  19026. + glob_worker = worker;
  19027. + }
  19028. +
  19029. + glob_worker->refs++;
  19030. + mutex_unlock(&worker_mutex);
  19031. +
  19032. + return 0;
  19033. +}
  19034. +EXPORT_SYMBOL_GPL(swork_get);
  19035. +
  19036. +/**
  19037. + * swork_put - puts an instance of the sworker
  19038. + *
  19039. + * Will destroy the sworker thread. This function must not be called until all
  19040. + * queued events have been completed.
  19041. + */
  19042. +void swork_put(void)
  19043. +{
  19044. + mutex_lock(&worker_mutex);
  19045. +
  19046. + glob_worker->refs--;
  19047. + if (glob_worker->refs > 0)
  19048. + goto out;
  19049. +
  19050. + swork_destroy(glob_worker);
  19051. + glob_worker = NULL;
  19052. +out:
  19053. + mutex_unlock(&worker_mutex);
  19054. +}
  19055. +EXPORT_SYMBOL_GPL(swork_put);
  19056. diff -Nur linux-4.1.20.orig/kernel/signal.c linux-4.1.20/kernel/signal.c
  19057. --- linux-4.1.20.orig/kernel/signal.c 2016-03-17 19:11:03.000000000 +0100
  19058. +++ linux-4.1.20/kernel/signal.c 2016-03-21 20:18:33.000000000 +0100
  19059. @@ -14,6 +14,7 @@
  19060. #include <linux/export.h>
  19061. #include <linux/init.h>
  19062. #include <linux/sched.h>
  19063. +#include <linux/sched/rt.h>
  19064. #include <linux/fs.h>
  19065. #include <linux/tty.h>
  19066. #include <linux/binfmts.h>
  19067. @@ -352,13 +353,45 @@
  19068. return false;
  19069. }
  19070. +#ifdef __HAVE_ARCH_CMPXCHG
  19071. +static inline struct sigqueue *get_task_cache(struct task_struct *t)
  19072. +{
  19073. + struct sigqueue *q = t->sigqueue_cache;
  19074. +
  19075. + if (cmpxchg(&t->sigqueue_cache, q, NULL) != q)
  19076. + return NULL;
  19077. + return q;
  19078. +}
  19079. +
  19080. +static inline int put_task_cache(struct task_struct *t, struct sigqueue *q)
  19081. +{
  19082. + if (cmpxchg(&t->sigqueue_cache, NULL, q) == NULL)
  19083. + return 0;
  19084. + return 1;
  19085. +}
  19086. +
  19087. +#else
  19088. +
  19089. +static inline struct sigqueue *get_task_cache(struct task_struct *t)
  19090. +{
  19091. + return NULL;
  19092. +}
  19093. +
  19094. +static inline int put_task_cache(struct task_struct *t, struct sigqueue *q)
  19095. +{
  19096. + return 1;
  19097. +}
  19098. +
  19099. +#endif
  19100. +
  19101. /*
  19102. * allocate a new signal queue record
  19103. * - this may be called without locks if and only if t == current, otherwise an
  19104. * appropriate lock must be held to stop the target task from exiting
  19105. */
  19106. static struct sigqueue *
  19107. -__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
  19108. +__sigqueue_do_alloc(int sig, struct task_struct *t, gfp_t flags,
  19109. + int override_rlimit, int fromslab)
  19110. {
  19111. struct sigqueue *q = NULL;
  19112. struct user_struct *user;
  19113. @@ -375,7 +408,10 @@
  19114. if (override_rlimit ||
  19115. atomic_read(&user->sigpending) <=
  19116. task_rlimit(t, RLIMIT_SIGPENDING)) {
  19117. - q = kmem_cache_alloc(sigqueue_cachep, flags);
  19118. + if (!fromslab)
  19119. + q = get_task_cache(t);
  19120. + if (!q)
  19121. + q = kmem_cache_alloc(sigqueue_cachep, flags);
  19122. } else {
  19123. print_dropped_signal(sig);
  19124. }
  19125. @@ -392,6 +428,13 @@
  19126. return q;
  19127. }
  19128. +static struct sigqueue *
  19129. +__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags,
  19130. + int override_rlimit)
  19131. +{
  19132. + return __sigqueue_do_alloc(sig, t, flags, override_rlimit, 0);
  19133. +}
  19134. +
  19135. static void __sigqueue_free(struct sigqueue *q)
  19136. {
  19137. if (q->flags & SIGQUEUE_PREALLOC)
  19138. @@ -401,6 +444,21 @@
  19139. kmem_cache_free(sigqueue_cachep, q);
  19140. }
  19141. +static void sigqueue_free_current(struct sigqueue *q)
  19142. +{
  19143. + struct user_struct *up;
  19144. +
  19145. + if (q->flags & SIGQUEUE_PREALLOC)
  19146. + return;
  19147. +
  19148. + up = q->user;
  19149. + if (rt_prio(current->normal_prio) && !put_task_cache(current, q)) {
  19150. + atomic_dec(&up->sigpending);
  19151. + free_uid(up);
  19152. + } else
  19153. + __sigqueue_free(q);
  19154. +}
  19155. +
  19156. void flush_sigqueue(struct sigpending *queue)
  19157. {
  19158. struct sigqueue *q;
  19159. @@ -414,6 +472,21 @@
  19160. }
  19161. /*
  19162. + * Called from __exit_signal. Flush tsk->pending and
  19163. + * tsk->sigqueue_cache
  19164. + */
  19165. +void flush_task_sigqueue(struct task_struct *tsk)
  19166. +{
  19167. + struct sigqueue *q;
  19168. +
  19169. + flush_sigqueue(&tsk->pending);
  19170. +
  19171. + q = get_task_cache(tsk);
  19172. + if (q)
  19173. + kmem_cache_free(sigqueue_cachep, q);
  19174. +}
  19175. +
  19176. +/*
  19177. * Flush all pending signals for a task.
  19178. */
  19179. void __flush_signals(struct task_struct *t)
  19180. @@ -565,7 +638,7 @@
  19181. still_pending:
  19182. list_del_init(&first->list);
  19183. copy_siginfo(info, &first->info);
  19184. - __sigqueue_free(first);
  19185. + sigqueue_free_current(first);
  19186. } else {
  19187. /*
  19188. * Ok, it wasn't in the queue. This must be
  19189. @@ -611,6 +684,8 @@
  19190. {
  19191. int signr;
  19192. + WARN_ON_ONCE(tsk != current);
  19193. +
  19194. /* We only dequeue private signals from ourselves, we don't let
  19195. * signalfd steal them
  19196. */
  19197. @@ -1207,8 +1282,8 @@
  19198. * We don't want to have recursive SIGSEGV's etc, for example,
  19199. * that is why we also clear SIGNAL_UNKILLABLE.
  19200. */
  19201. -int
  19202. -force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  19203. +static int
  19204. +do_force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  19205. {
  19206. unsigned long int flags;
  19207. int ret, blocked, ignored;
  19208. @@ -1233,6 +1308,39 @@
  19209. return ret;
  19210. }
  19211. +int force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  19212. +{
  19213. +/*
  19214. + * On some archs, PREEMPT_RT has to delay sending a signal from a trap
  19215. + * since it can not enable preemption, and the signal code's spin_locks
  19216. + * turn into mutexes. Instead, it must set TIF_NOTIFY_RESUME which will
  19217. + * send the signal on exit of the trap.
  19218. + */
  19219. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  19220. + if (in_atomic()) {
  19221. + if (WARN_ON_ONCE(t != current))
  19222. + return 0;
  19223. + if (WARN_ON_ONCE(t->forced_info.si_signo))
  19224. + return 0;
  19225. +
  19226. + if (is_si_special(info)) {
  19227. + WARN_ON_ONCE(info != SEND_SIG_PRIV);
  19228. + t->forced_info.si_signo = sig;
  19229. + t->forced_info.si_errno = 0;
  19230. + t->forced_info.si_code = SI_KERNEL;
  19231. + t->forced_info.si_pid = 0;
  19232. + t->forced_info.si_uid = 0;
  19233. + } else {
  19234. + t->forced_info = *info;
  19235. + }
  19236. +
  19237. + set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  19238. + return 0;
  19239. + }
  19240. +#endif
  19241. + return do_force_sig_info(sig, info, t);
  19242. +}
  19243. +
  19244. /*
  19245. * Nuke all other threads in the group.
  19246. */
  19247. @@ -1267,12 +1375,12 @@
  19248. * Disable interrupts early to avoid deadlocks.
  19249. * See rcu_read_unlock() comment header for details.
  19250. */
  19251. - local_irq_save(*flags);
  19252. + local_irq_save_nort(*flags);
  19253. rcu_read_lock();
  19254. sighand = rcu_dereference(tsk->sighand);
  19255. if (unlikely(sighand == NULL)) {
  19256. rcu_read_unlock();
  19257. - local_irq_restore(*flags);
  19258. + local_irq_restore_nort(*flags);
  19259. break;
  19260. }
  19261. /*
  19262. @@ -1293,7 +1401,7 @@
  19263. }
  19264. spin_unlock(&sighand->siglock);
  19265. rcu_read_unlock();
  19266. - local_irq_restore(*flags);
  19267. + local_irq_restore_nort(*flags);
  19268. }
  19269. return sighand;
  19270. @@ -1536,7 +1644,8 @@
  19271. */
  19272. struct sigqueue *sigqueue_alloc(void)
  19273. {
  19274. - struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
  19275. + /* Preallocated sigqueue objects always from the slabcache ! */
  19276. + struct sigqueue *q = __sigqueue_do_alloc(-1, current, GFP_KERNEL, 0, 1);
  19277. if (q)
  19278. q->flags |= SIGQUEUE_PREALLOC;
  19279. @@ -1897,15 +2006,7 @@
  19280. if (gstop_done && ptrace_reparented(current))
  19281. do_notify_parent_cldstop(current, false, why);
  19282. - /*
  19283. - * Don't want to allow preemption here, because
  19284. - * sys_ptrace() needs this task to be inactive.
  19285. - *
  19286. - * XXX: implement read_unlock_no_resched().
  19287. - */
  19288. - preempt_disable();
  19289. read_unlock(&tasklist_lock);
  19290. - preempt_enable_no_resched();
  19291. freezable_schedule();
  19292. } else {
  19293. /*
  19294. diff -Nur linux-4.1.20.orig/kernel/softirq.c linux-4.1.20/kernel/softirq.c
  19295. --- linux-4.1.20.orig/kernel/softirq.c 2016-03-17 19:11:03.000000000 +0100
  19296. +++ linux-4.1.20/kernel/softirq.c 2016-03-21 20:18:33.000000000 +0100
  19297. @@ -21,10 +21,12 @@
  19298. #include <linux/freezer.h>
  19299. #include <linux/kthread.h>
  19300. #include <linux/rcupdate.h>
  19301. +#include <linux/delay.h>
  19302. #include <linux/ftrace.h>
  19303. #include <linux/smp.h>
  19304. #include <linux/smpboot.h>
  19305. #include <linux/tick.h>
  19306. +#include <linux/locallock.h>
  19307. #include <linux/irq.h>
  19308. #define CREATE_TRACE_POINTS
  19309. @@ -56,12 +58,108 @@
  19310. static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
  19311. DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
  19312. +#ifdef CONFIG_PREEMPT_RT_FULL
  19313. +#define TIMER_SOFTIRQS ((1 << TIMER_SOFTIRQ) | (1 << HRTIMER_SOFTIRQ))
  19314. +DEFINE_PER_CPU(struct task_struct *, ktimer_softirqd);
  19315. +#endif
  19316. const char * const softirq_to_name[NR_SOFTIRQS] = {
  19317. "HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "BLOCK_IOPOLL",
  19318. "TASKLET", "SCHED", "HRTIMER", "RCU"
  19319. };
  19320. +#ifdef CONFIG_NO_HZ_COMMON
  19321. +# ifdef CONFIG_PREEMPT_RT_FULL
  19322. +
  19323. +struct softirq_runner {
  19324. + struct task_struct *runner[NR_SOFTIRQS];
  19325. +};
  19326. +
  19327. +static DEFINE_PER_CPU(struct softirq_runner, softirq_runners);
  19328. +
  19329. +static inline void softirq_set_runner(unsigned int sirq)
  19330. +{
  19331. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  19332. +
  19333. + sr->runner[sirq] = current;
  19334. +}
  19335. +
  19336. +static inline void softirq_clr_runner(unsigned int sirq)
  19337. +{
  19338. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  19339. +
  19340. + sr->runner[sirq] = NULL;
  19341. +}
  19342. +
  19343. +/*
  19344. + * On preempt-rt a softirq running context might be blocked on a
  19345. + * lock. There might be no other runnable task on this CPU because the
  19346. + * lock owner runs on some other CPU. So we have to go into idle with
  19347. + * the pending bit set. Therefor we need to check this otherwise we
  19348. + * warn about false positives which confuses users and defeats the
  19349. + * whole purpose of this test.
  19350. + *
  19351. + * This code is called with interrupts disabled.
  19352. + */
  19353. +void softirq_check_pending_idle(void)
  19354. +{
  19355. + static int rate_limit;
  19356. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  19357. + u32 warnpending;
  19358. + int i;
  19359. +
  19360. + if (rate_limit >= 10)
  19361. + return;
  19362. +
  19363. + warnpending = local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK;
  19364. + for (i = 0; i < NR_SOFTIRQS; i++) {
  19365. + struct task_struct *tsk = sr->runner[i];
  19366. +
  19367. + /*
  19368. + * The wakeup code in rtmutex.c wakes up the task
  19369. + * _before_ it sets pi_blocked_on to NULL under
  19370. + * tsk->pi_lock. So we need to check for both: state
  19371. + * and pi_blocked_on.
  19372. + */
  19373. + if (tsk) {
  19374. + raw_spin_lock(&tsk->pi_lock);
  19375. + if (tsk->pi_blocked_on || tsk->state == TASK_RUNNING) {
  19376. + /* Clear all bits pending in that task */
  19377. + warnpending &= ~(tsk->softirqs_raised);
  19378. + warnpending &= ~(1 << i);
  19379. + }
  19380. + raw_spin_unlock(&tsk->pi_lock);
  19381. + }
  19382. + }
  19383. +
  19384. + if (warnpending) {
  19385. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  19386. + warnpending);
  19387. + rate_limit++;
  19388. + }
  19389. +}
  19390. +# else
  19391. +/*
  19392. + * On !PREEMPT_RT we just printk rate limited:
  19393. + */
  19394. +void softirq_check_pending_idle(void)
  19395. +{
  19396. + static int rate_limit;
  19397. +
  19398. + if (rate_limit < 10 &&
  19399. + (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  19400. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  19401. + local_softirq_pending());
  19402. + rate_limit++;
  19403. + }
  19404. +}
  19405. +# endif
  19406. +
  19407. +#else /* !CONFIG_NO_HZ_COMMON */
  19408. +static inline void softirq_set_runner(unsigned int sirq) { }
  19409. +static inline void softirq_clr_runner(unsigned int sirq) { }
  19410. +#endif
  19411. +
  19412. /*
  19413. * we cannot loop indefinitely here to avoid userspace starvation,
  19414. * but we also don't want to introduce a worst case 1/HZ latency
  19415. @@ -77,6 +175,79 @@
  19416. wake_up_process(tsk);
  19417. }
  19418. +#ifdef CONFIG_PREEMPT_RT_FULL
  19419. +static void wakeup_timer_softirqd(void)
  19420. +{
  19421. + /* Interrupts are disabled: no need to stop preemption */
  19422. + struct task_struct *tsk = __this_cpu_read(ktimer_softirqd);
  19423. +
  19424. + if (tsk && tsk->state != TASK_RUNNING)
  19425. + wake_up_process(tsk);
  19426. +}
  19427. +#endif
  19428. +
  19429. +static void handle_softirq(unsigned int vec_nr)
  19430. +{
  19431. + struct softirq_action *h = softirq_vec + vec_nr;
  19432. + int prev_count;
  19433. +
  19434. + prev_count = preempt_count();
  19435. +
  19436. + kstat_incr_softirqs_this_cpu(vec_nr);
  19437. +
  19438. + trace_softirq_entry(vec_nr);
  19439. + h->action(h);
  19440. + trace_softirq_exit(vec_nr);
  19441. + if (unlikely(prev_count != preempt_count())) {
  19442. + pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  19443. + vec_nr, softirq_to_name[vec_nr], h->action,
  19444. + prev_count, preempt_count());
  19445. + preempt_count_set(prev_count);
  19446. + }
  19447. +}
  19448. +
  19449. +#ifndef CONFIG_PREEMPT_RT_FULL
  19450. +static inline int ksoftirqd_softirq_pending(void)
  19451. +{
  19452. + return local_softirq_pending();
  19453. +}
  19454. +
  19455. +static void handle_pending_softirqs(u32 pending)
  19456. +{
  19457. + struct softirq_action *h = softirq_vec;
  19458. + int softirq_bit;
  19459. +
  19460. + local_irq_enable();
  19461. +
  19462. + h = softirq_vec;
  19463. +
  19464. + while ((softirq_bit = ffs(pending))) {
  19465. + unsigned int vec_nr;
  19466. +
  19467. + h += softirq_bit - 1;
  19468. + vec_nr = h - softirq_vec;
  19469. + handle_softirq(vec_nr);
  19470. +
  19471. + h++;
  19472. + pending >>= softirq_bit;
  19473. + }
  19474. +
  19475. + rcu_bh_qs();
  19476. + local_irq_disable();
  19477. +}
  19478. +
  19479. +static void run_ksoftirqd(unsigned int cpu)
  19480. +{
  19481. + local_irq_disable();
  19482. + if (ksoftirqd_softirq_pending()) {
  19483. + __do_softirq();
  19484. + local_irq_enable();
  19485. + cond_resched_rcu_qs();
  19486. + return;
  19487. + }
  19488. + local_irq_enable();
  19489. +}
  19490. +
  19491. /*
  19492. * preempt_count and SOFTIRQ_OFFSET usage:
  19493. * - preempt_count is changed by SOFTIRQ_OFFSET on entering or leaving
  19494. @@ -116,9 +287,9 @@
  19495. if (preempt_count() == cnt) {
  19496. #ifdef CONFIG_DEBUG_PREEMPT
  19497. - current->preempt_disable_ip = get_parent_ip(CALLER_ADDR1);
  19498. + current->preempt_disable_ip = get_lock_parent_ip();
  19499. #endif
  19500. - trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  19501. + trace_preempt_off(CALLER_ADDR0, get_lock_parent_ip());
  19502. }
  19503. }
  19504. EXPORT_SYMBOL(__local_bh_disable_ip);
  19505. @@ -232,10 +403,8 @@
  19506. unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
  19507. unsigned long old_flags = current->flags;
  19508. int max_restart = MAX_SOFTIRQ_RESTART;
  19509. - struct softirq_action *h;
  19510. bool in_hardirq;
  19511. __u32 pending;
  19512. - int softirq_bit;
  19513. /*
  19514. * Mask out PF_MEMALLOC s current task context is borrowed for the
  19515. @@ -254,36 +423,7 @@
  19516. /* Reset the pending bitmask before enabling irqs */
  19517. set_softirq_pending(0);
  19518. - local_irq_enable();
  19519. -
  19520. - h = softirq_vec;
  19521. -
  19522. - while ((softirq_bit = ffs(pending))) {
  19523. - unsigned int vec_nr;
  19524. - int prev_count;
  19525. -
  19526. - h += softirq_bit - 1;
  19527. -
  19528. - vec_nr = h - softirq_vec;
  19529. - prev_count = preempt_count();
  19530. -
  19531. - kstat_incr_softirqs_this_cpu(vec_nr);
  19532. -
  19533. - trace_softirq_entry(vec_nr);
  19534. - h->action(h);
  19535. - trace_softirq_exit(vec_nr);
  19536. - if (unlikely(prev_count != preempt_count())) {
  19537. - pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  19538. - vec_nr, softirq_to_name[vec_nr], h->action,
  19539. - prev_count, preempt_count());
  19540. - preempt_count_set(prev_count);
  19541. - }
  19542. - h++;
  19543. - pending >>= softirq_bit;
  19544. - }
  19545. -
  19546. - rcu_bh_qs();
  19547. - local_irq_disable();
  19548. + handle_pending_softirqs(pending);
  19549. pending = local_softirq_pending();
  19550. if (pending) {
  19551. @@ -320,6 +460,310 @@
  19552. }
  19553. /*
  19554. + * This function must run with irqs disabled!
  19555. + */
  19556. +void raise_softirq_irqoff(unsigned int nr)
  19557. +{
  19558. + __raise_softirq_irqoff(nr);
  19559. +
  19560. + /*
  19561. + * If we're in an interrupt or softirq, we're done
  19562. + * (this also catches softirq-disabled code). We will
  19563. + * actually run the softirq once we return from
  19564. + * the irq or softirq.
  19565. + *
  19566. + * Otherwise we wake up ksoftirqd to make sure we
  19567. + * schedule the softirq soon.
  19568. + */
  19569. + if (!in_interrupt())
  19570. + wakeup_softirqd();
  19571. +}
  19572. +
  19573. +void __raise_softirq_irqoff(unsigned int nr)
  19574. +{
  19575. + trace_softirq_raise(nr);
  19576. + or_softirq_pending(1UL << nr);
  19577. +}
  19578. +
  19579. +static inline void local_bh_disable_nort(void) { local_bh_disable(); }
  19580. +static inline void _local_bh_enable_nort(void) { _local_bh_enable(); }
  19581. +static void ksoftirqd_set_sched_params(unsigned int cpu) { }
  19582. +
  19583. +#else /* !PREEMPT_RT_FULL */
  19584. +
  19585. +/*
  19586. + * On RT we serialize softirq execution with a cpu local lock per softirq
  19587. + */
  19588. +static DEFINE_PER_CPU(struct local_irq_lock [NR_SOFTIRQS], local_softirq_locks);
  19589. +
  19590. +void __init softirq_early_init(void)
  19591. +{
  19592. + int i;
  19593. +
  19594. + for (i = 0; i < NR_SOFTIRQS; i++)
  19595. + local_irq_lock_init(local_softirq_locks[i]);
  19596. +}
  19597. +
  19598. +static void lock_softirq(int which)
  19599. +{
  19600. + local_lock(local_softirq_locks[which]);
  19601. +}
  19602. +
  19603. +static void unlock_softirq(int which)
  19604. +{
  19605. + local_unlock(local_softirq_locks[which]);
  19606. +}
  19607. +
  19608. +static void do_single_softirq(int which)
  19609. +{
  19610. + unsigned long old_flags = current->flags;
  19611. +
  19612. + current->flags &= ~PF_MEMALLOC;
  19613. + vtime_account_irq_enter(current);
  19614. + current->flags |= PF_IN_SOFTIRQ;
  19615. + lockdep_softirq_enter();
  19616. + local_irq_enable();
  19617. + handle_softirq(which);
  19618. + local_irq_disable();
  19619. + lockdep_softirq_exit();
  19620. + current->flags &= ~PF_IN_SOFTIRQ;
  19621. + vtime_account_irq_enter(current);
  19622. + tsk_restore_flags(current, old_flags, PF_MEMALLOC);
  19623. +}
  19624. +
  19625. +/*
  19626. + * Called with interrupts disabled. Process softirqs which were raised
  19627. + * in current context (or on behalf of ksoftirqd).
  19628. + */
  19629. +static void do_current_softirqs(void)
  19630. +{
  19631. + while (current->softirqs_raised) {
  19632. + int i = __ffs(current->softirqs_raised);
  19633. + unsigned int pending, mask = (1U << i);
  19634. +
  19635. + current->softirqs_raised &= ~mask;
  19636. + local_irq_enable();
  19637. +
  19638. + /*
  19639. + * If the lock is contended, we boost the owner to
  19640. + * process the softirq or leave the critical section
  19641. + * now.
  19642. + */
  19643. + lock_softirq(i);
  19644. + local_irq_disable();
  19645. + softirq_set_runner(i);
  19646. + /*
  19647. + * Check with the local_softirq_pending() bits,
  19648. + * whether we need to process this still or if someone
  19649. + * else took care of it.
  19650. + */
  19651. + pending = local_softirq_pending();
  19652. + if (pending & mask) {
  19653. + set_softirq_pending(pending & ~mask);
  19654. + do_single_softirq(i);
  19655. + }
  19656. + softirq_clr_runner(i);
  19657. + WARN_ON(current->softirq_nestcnt != 1);
  19658. + local_irq_enable();
  19659. + unlock_softirq(i);
  19660. + local_irq_disable();
  19661. + }
  19662. +}
  19663. +
  19664. +void __local_bh_disable(void)
  19665. +{
  19666. + if (++current->softirq_nestcnt == 1)
  19667. + migrate_disable();
  19668. +}
  19669. +EXPORT_SYMBOL(__local_bh_disable);
  19670. +
  19671. +void __local_bh_enable(void)
  19672. +{
  19673. + if (WARN_ON(current->softirq_nestcnt == 0))
  19674. + return;
  19675. +
  19676. + local_irq_disable();
  19677. + if (current->softirq_nestcnt == 1 && current->softirqs_raised)
  19678. + do_current_softirqs();
  19679. + local_irq_enable();
  19680. +
  19681. + if (--current->softirq_nestcnt == 0)
  19682. + migrate_enable();
  19683. +}
  19684. +EXPORT_SYMBOL(__local_bh_enable);
  19685. +
  19686. +void _local_bh_enable(void)
  19687. +{
  19688. + if (WARN_ON(current->softirq_nestcnt == 0))
  19689. + return;
  19690. + if (--current->softirq_nestcnt == 0)
  19691. + migrate_enable();
  19692. +}
  19693. +EXPORT_SYMBOL(_local_bh_enable);
  19694. +
  19695. +int in_serving_softirq(void)
  19696. +{
  19697. + return current->flags & PF_IN_SOFTIRQ;
  19698. +}
  19699. +EXPORT_SYMBOL(in_serving_softirq);
  19700. +
  19701. +/* Called with preemption disabled */
  19702. +static void run_ksoftirqd(unsigned int cpu)
  19703. +{
  19704. + local_irq_disable();
  19705. + current->softirq_nestcnt++;
  19706. +
  19707. + do_current_softirqs();
  19708. + current->softirq_nestcnt--;
  19709. + local_irq_enable();
  19710. + cond_resched_rcu_qs();
  19711. +}
  19712. +
  19713. +/*
  19714. + * Called from netif_rx_ni(). Preemption enabled, but migration
  19715. + * disabled. So the cpu can't go away under us.
  19716. + */
  19717. +void thread_do_softirq(void)
  19718. +{
  19719. + if (!in_serving_softirq() && current->softirqs_raised) {
  19720. + current->softirq_nestcnt++;
  19721. + do_current_softirqs();
  19722. + current->softirq_nestcnt--;
  19723. + }
  19724. +}
  19725. +
  19726. +static void do_raise_softirq_irqoff(unsigned int nr)
  19727. +{
  19728. + unsigned int mask;
  19729. +
  19730. + mask = 1UL << nr;
  19731. +
  19732. + trace_softirq_raise(nr);
  19733. + or_softirq_pending(mask);
  19734. +
  19735. + /*
  19736. + * If we are not in a hard interrupt and inside a bh disabled
  19737. + * region, we simply raise the flag on current. local_bh_enable()
  19738. + * will make sure that the softirq is executed. Otherwise we
  19739. + * delegate it to ksoftirqd.
  19740. + */
  19741. + if (!in_irq() && current->softirq_nestcnt)
  19742. + current->softirqs_raised |= mask;
  19743. + else if (!__this_cpu_read(ksoftirqd) || !__this_cpu_read(ktimer_softirqd))
  19744. + return;
  19745. +
  19746. + if (mask & TIMER_SOFTIRQS)
  19747. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  19748. + else
  19749. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  19750. +}
  19751. +
  19752. +static void wakeup_proper_softirq(unsigned int nr)
  19753. +{
  19754. + if ((1UL << nr) & TIMER_SOFTIRQS)
  19755. + wakeup_timer_softirqd();
  19756. + else
  19757. + wakeup_softirqd();
  19758. +}
  19759. +
  19760. +
  19761. +void __raise_softirq_irqoff(unsigned int nr)
  19762. +{
  19763. + do_raise_softirq_irqoff(nr);
  19764. + if (!in_irq() && !current->softirq_nestcnt)
  19765. + wakeup_proper_softirq(nr);
  19766. +}
  19767. +
  19768. +/*
  19769. + * Same as __raise_softirq_irqoff() but will process them in ksoftirqd
  19770. + */
  19771. +void __raise_softirq_irqoff_ksoft(unsigned int nr)
  19772. +{
  19773. + unsigned int mask;
  19774. +
  19775. + if (WARN_ON_ONCE(!__this_cpu_read(ksoftirqd) ||
  19776. + !__this_cpu_read(ktimer_softirqd)))
  19777. + return;
  19778. + mask = 1UL << nr;
  19779. +
  19780. + trace_softirq_raise(nr);
  19781. + or_softirq_pending(mask);
  19782. + if (mask & TIMER_SOFTIRQS)
  19783. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  19784. + else
  19785. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  19786. + wakeup_proper_softirq(nr);
  19787. +}
  19788. +
  19789. +/*
  19790. + * This function must run with irqs disabled!
  19791. + */
  19792. +void raise_softirq_irqoff(unsigned int nr)
  19793. +{
  19794. + do_raise_softirq_irqoff(nr);
  19795. +
  19796. + /*
  19797. + * If we're in an hard interrupt we let irq return code deal
  19798. + * with the wakeup of ksoftirqd.
  19799. + */
  19800. + if (in_irq())
  19801. + return;
  19802. + /*
  19803. + * If we are in thread context but outside of a bh disabled
  19804. + * region, we need to wake ksoftirqd as well.
  19805. + *
  19806. + * CHECKME: Some of the places which do that could be wrapped
  19807. + * into local_bh_disable/enable pairs. Though it's unclear
  19808. + * whether this is worth the effort. To find those places just
  19809. + * raise a WARN() if the condition is met.
  19810. + */
  19811. + if (!current->softirq_nestcnt)
  19812. + wakeup_proper_softirq(nr);
  19813. +}
  19814. +
  19815. +static inline int ksoftirqd_softirq_pending(void)
  19816. +{
  19817. + return current->softirqs_raised;
  19818. +}
  19819. +
  19820. +static inline void local_bh_disable_nort(void) { }
  19821. +static inline void _local_bh_enable_nort(void) { }
  19822. +
  19823. +static inline void ksoftirqd_set_sched_params(unsigned int cpu)
  19824. +{
  19825. + /* Take over all but timer pending softirqs when starting */
  19826. + local_irq_disable();
  19827. + current->softirqs_raised = local_softirq_pending() & ~TIMER_SOFTIRQS;
  19828. + local_irq_enable();
  19829. +}
  19830. +
  19831. +static inline void ktimer_softirqd_set_sched_params(unsigned int cpu)
  19832. +{
  19833. + struct sched_param param = { .sched_priority = 1 };
  19834. +
  19835. + sched_setscheduler(current, SCHED_FIFO, &param);
  19836. +
  19837. + /* Take over timer pending softirqs when starting */
  19838. + local_irq_disable();
  19839. + current->softirqs_raised = local_softirq_pending() & TIMER_SOFTIRQS;
  19840. + local_irq_enable();
  19841. +}
  19842. +
  19843. +static inline void ktimer_softirqd_clr_sched_params(unsigned int cpu,
  19844. + bool online)
  19845. +{
  19846. + struct sched_param param = { .sched_priority = 0 };
  19847. +
  19848. + sched_setscheduler(current, SCHED_NORMAL, &param);
  19849. +}
  19850. +
  19851. +static int ktimer_softirqd_should_run(unsigned int cpu)
  19852. +{
  19853. + return current->softirqs_raised;
  19854. +}
  19855. +
  19856. +#endif /* PREEMPT_RT_FULL */
  19857. +/*
  19858. * Enter an interrupt context.
  19859. */
  19860. void irq_enter(void)
  19861. @@ -330,9 +774,9 @@
  19862. * Prevent raise_softirq from needlessly waking up ksoftirqd
  19863. * here, as softirq will be serviced on return from interrupt.
  19864. */
  19865. - local_bh_disable();
  19866. + local_bh_disable_nort();
  19867. tick_irq_enter();
  19868. - _local_bh_enable();
  19869. + _local_bh_enable_nort();
  19870. }
  19871. __irq_enter();
  19872. @@ -340,6 +784,7 @@
  19873. static inline void invoke_softirq(void)
  19874. {
  19875. +#ifndef CONFIG_PREEMPT_RT_FULL
  19876. if (!force_irqthreads) {
  19877. #ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
  19878. /*
  19879. @@ -359,6 +804,18 @@
  19880. } else {
  19881. wakeup_softirqd();
  19882. }
  19883. +#else /* PREEMPT_RT_FULL */
  19884. + unsigned long flags;
  19885. +
  19886. + local_irq_save(flags);
  19887. + if (__this_cpu_read(ksoftirqd) &&
  19888. + __this_cpu_read(ksoftirqd)->softirqs_raised)
  19889. + wakeup_softirqd();
  19890. + if (__this_cpu_read(ktimer_softirqd) &&
  19891. + __this_cpu_read(ktimer_softirqd)->softirqs_raised)
  19892. + wakeup_timer_softirqd();
  19893. + local_irq_restore(flags);
  19894. +#endif
  19895. }
  19896. static inline void tick_irq_exit(void)
  19897. @@ -395,26 +852,6 @@
  19898. trace_hardirq_exit(); /* must be last! */
  19899. }
  19900. -/*
  19901. - * This function must run with irqs disabled!
  19902. - */
  19903. -inline void raise_softirq_irqoff(unsigned int nr)
  19904. -{
  19905. - __raise_softirq_irqoff(nr);
  19906. -
  19907. - /*
  19908. - * If we're in an interrupt or softirq, we're done
  19909. - * (this also catches softirq-disabled code). We will
  19910. - * actually run the softirq once we return from
  19911. - * the irq or softirq.
  19912. - *
  19913. - * Otherwise we wake up ksoftirqd to make sure we
  19914. - * schedule the softirq soon.
  19915. - */
  19916. - if (!in_interrupt())
  19917. - wakeup_softirqd();
  19918. -}
  19919. -
  19920. void raise_softirq(unsigned int nr)
  19921. {
  19922. unsigned long flags;
  19923. @@ -424,12 +861,6 @@
  19924. local_irq_restore(flags);
  19925. }
  19926. -void __raise_softirq_irqoff(unsigned int nr)
  19927. -{
  19928. - trace_softirq_raise(nr);
  19929. - or_softirq_pending(1UL << nr);
  19930. -}
  19931. -
  19932. void open_softirq(int nr, void (*action)(struct softirq_action *))
  19933. {
  19934. softirq_vec[nr].action = action;
  19935. @@ -446,15 +877,45 @@
  19936. static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
  19937. static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
  19938. +static void inline
  19939. +__tasklet_common_schedule(struct tasklet_struct *t, struct tasklet_head *head, unsigned int nr)
  19940. +{
  19941. + if (tasklet_trylock(t)) {
  19942. +again:
  19943. + /* We may have been preempted before tasklet_trylock
  19944. + * and __tasklet_action may have already run.
  19945. + * So double check the sched bit while the takslet
  19946. + * is locked before adding it to the list.
  19947. + */
  19948. + if (test_bit(TASKLET_STATE_SCHED, &t->state)) {
  19949. + t->next = NULL;
  19950. + *head->tail = t;
  19951. + head->tail = &(t->next);
  19952. + raise_softirq_irqoff(nr);
  19953. + tasklet_unlock(t);
  19954. + } else {
  19955. + /* This is subtle. If we hit the corner case above
  19956. + * It is possible that we get preempted right here,
  19957. + * and another task has successfully called
  19958. + * tasklet_schedule(), then this function, and
  19959. + * failed on the trylock. Thus we must be sure
  19960. + * before releasing the tasklet lock, that the
  19961. + * SCHED_BIT is clear. Otherwise the tasklet
  19962. + * may get its SCHED_BIT set, but not added to the
  19963. + * list
  19964. + */
  19965. + if (!tasklet_tryunlock(t))
  19966. + goto again;
  19967. + }
  19968. + }
  19969. +}
  19970. +
  19971. void __tasklet_schedule(struct tasklet_struct *t)
  19972. {
  19973. unsigned long flags;
  19974. local_irq_save(flags);
  19975. - t->next = NULL;
  19976. - *__this_cpu_read(tasklet_vec.tail) = t;
  19977. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  19978. - raise_softirq_irqoff(TASKLET_SOFTIRQ);
  19979. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
  19980. local_irq_restore(flags);
  19981. }
  19982. EXPORT_SYMBOL(__tasklet_schedule);
  19983. @@ -464,10 +925,7 @@
  19984. unsigned long flags;
  19985. local_irq_save(flags);
  19986. - t->next = NULL;
  19987. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  19988. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  19989. - raise_softirq_irqoff(HI_SOFTIRQ);
  19990. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
  19991. local_irq_restore(flags);
  19992. }
  19993. EXPORT_SYMBOL(__tasklet_hi_schedule);
  19994. @@ -476,82 +934,122 @@
  19995. {
  19996. BUG_ON(!irqs_disabled());
  19997. - t->next = __this_cpu_read(tasklet_hi_vec.head);
  19998. - __this_cpu_write(tasklet_hi_vec.head, t);
  19999. - __raise_softirq_irqoff(HI_SOFTIRQ);
  20000. + __tasklet_hi_schedule(t);
  20001. }
  20002. EXPORT_SYMBOL(__tasklet_hi_schedule_first);
  20003. -static void tasklet_action(struct softirq_action *a)
  20004. +void tasklet_enable(struct tasklet_struct *t)
  20005. {
  20006. - struct tasklet_struct *list;
  20007. + if (!atomic_dec_and_test(&t->count))
  20008. + return;
  20009. + if (test_and_clear_bit(TASKLET_STATE_PENDING, &t->state))
  20010. + tasklet_schedule(t);
  20011. +}
  20012. +EXPORT_SYMBOL(tasklet_enable);
  20013. - local_irq_disable();
  20014. - list = __this_cpu_read(tasklet_vec.head);
  20015. - __this_cpu_write(tasklet_vec.head, NULL);
  20016. - __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  20017. - local_irq_enable();
  20018. +static void __tasklet_action(struct softirq_action *a,
  20019. + struct tasklet_struct *list)
  20020. +{
  20021. + int loops = 1000000;
  20022. while (list) {
  20023. struct tasklet_struct *t = list;
  20024. list = list->next;
  20025. - if (tasklet_trylock(t)) {
  20026. - if (!atomic_read(&t->count)) {
  20027. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  20028. - &t->state))
  20029. - BUG();
  20030. - t->func(t->data);
  20031. - tasklet_unlock(t);
  20032. - continue;
  20033. - }
  20034. - tasklet_unlock(t);
  20035. + /*
  20036. + * Should always succeed - after a tasklist got on the
  20037. + * list (after getting the SCHED bit set from 0 to 1),
  20038. + * nothing but the tasklet softirq it got queued to can
  20039. + * lock it:
  20040. + */
  20041. + if (!tasklet_trylock(t)) {
  20042. + WARN_ON(1);
  20043. + continue;
  20044. }
  20045. - local_irq_disable();
  20046. t->next = NULL;
  20047. - *__this_cpu_read(tasklet_vec.tail) = t;
  20048. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  20049. - __raise_softirq_irqoff(TASKLET_SOFTIRQ);
  20050. - local_irq_enable();
  20051. +
  20052. + /*
  20053. + * If we cannot handle the tasklet because it's disabled,
  20054. + * mark it as pending. tasklet_enable() will later
  20055. + * re-schedule the tasklet.
  20056. + */
  20057. + if (unlikely(atomic_read(&t->count))) {
  20058. +out_disabled:
  20059. + /* implicit unlock: */
  20060. + wmb();
  20061. + t->state = TASKLET_STATEF_PENDING;
  20062. + continue;
  20063. + }
  20064. +
  20065. + /*
  20066. + * After this point on the tasklet might be rescheduled
  20067. + * on another CPU, but it can only be added to another
  20068. + * CPU's tasklet list if we unlock the tasklet (which we
  20069. + * dont do yet).
  20070. + */
  20071. + if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  20072. + WARN_ON(1);
  20073. +
  20074. +again:
  20075. + t->func(t->data);
  20076. +
  20077. + /*
  20078. + * Try to unlock the tasklet. We must use cmpxchg, because
  20079. + * another CPU might have scheduled or disabled the tasklet.
  20080. + * We only allow the STATE_RUN -> 0 transition here.
  20081. + */
  20082. + while (!tasklet_tryunlock(t)) {
  20083. + /*
  20084. + * If it got disabled meanwhile, bail out:
  20085. + */
  20086. + if (atomic_read(&t->count))
  20087. + goto out_disabled;
  20088. + /*
  20089. + * If it got scheduled meanwhile, re-execute
  20090. + * the tasklet function:
  20091. + */
  20092. + if (test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  20093. + goto again;
  20094. + if (!--loops) {
  20095. + printk("hm, tasklet state: %08lx\n", t->state);
  20096. + WARN_ON(1);
  20097. + tasklet_unlock(t);
  20098. + break;
  20099. + }
  20100. + }
  20101. }
  20102. }
  20103. +static void tasklet_action(struct softirq_action *a)
  20104. +{
  20105. + struct tasklet_struct *list;
  20106. +
  20107. + local_irq_disable();
  20108. +
  20109. + list = __this_cpu_read(tasklet_vec.head);
  20110. + __this_cpu_write(tasklet_vec.head, NULL);
  20111. + __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  20112. +
  20113. + local_irq_enable();
  20114. +
  20115. + __tasklet_action(a, list);
  20116. +}
  20117. +
  20118. static void tasklet_hi_action(struct softirq_action *a)
  20119. {
  20120. struct tasklet_struct *list;
  20121. local_irq_disable();
  20122. +
  20123. list = __this_cpu_read(tasklet_hi_vec.head);
  20124. __this_cpu_write(tasklet_hi_vec.head, NULL);
  20125. __this_cpu_write(tasklet_hi_vec.tail, this_cpu_ptr(&tasklet_hi_vec.head));
  20126. - local_irq_enable();
  20127. - while (list) {
  20128. - struct tasklet_struct *t = list;
  20129. -
  20130. - list = list->next;
  20131. -
  20132. - if (tasklet_trylock(t)) {
  20133. - if (!atomic_read(&t->count)) {
  20134. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  20135. - &t->state))
  20136. - BUG();
  20137. - t->func(t->data);
  20138. - tasklet_unlock(t);
  20139. - continue;
  20140. - }
  20141. - tasklet_unlock(t);
  20142. - }
  20143. + local_irq_enable();
  20144. - local_irq_disable();
  20145. - t->next = NULL;
  20146. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  20147. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  20148. - __raise_softirq_irqoff(HI_SOFTIRQ);
  20149. - local_irq_enable();
  20150. - }
  20151. + __tasklet_action(a, list);
  20152. }
  20153. void tasklet_init(struct tasklet_struct *t,
  20154. @@ -572,7 +1070,7 @@
  20155. while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
  20156. do {
  20157. - yield();
  20158. + msleep(1);
  20159. } while (test_bit(TASKLET_STATE_SCHED, &t->state));
  20160. }
  20161. tasklet_unlock_wait(t);
  20162. @@ -646,25 +1144,26 @@
  20163. open_softirq(HI_SOFTIRQ, tasklet_hi_action);
  20164. }
  20165. -static int ksoftirqd_should_run(unsigned int cpu)
  20166. -{
  20167. - return local_softirq_pending();
  20168. -}
  20169. -
  20170. -static void run_ksoftirqd(unsigned int cpu)
  20171. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  20172. +void tasklet_unlock_wait(struct tasklet_struct *t)
  20173. {
  20174. - local_irq_disable();
  20175. - if (local_softirq_pending()) {
  20176. + while (test_bit(TASKLET_STATE_RUN, &(t)->state)) {
  20177. /*
  20178. - * We can safely run softirq on inline stack, as we are not deep
  20179. - * in the task stack here.
  20180. + * Hack for now to avoid this busy-loop:
  20181. */
  20182. - __do_softirq();
  20183. - local_irq_enable();
  20184. - cond_resched_rcu_qs();
  20185. - return;
  20186. +#ifdef CONFIG_PREEMPT_RT_FULL
  20187. + msleep(1);
  20188. +#else
  20189. + barrier();
  20190. +#endif
  20191. }
  20192. - local_irq_enable();
  20193. +}
  20194. +EXPORT_SYMBOL(tasklet_unlock_wait);
  20195. +#endif
  20196. +
  20197. +static int ksoftirqd_should_run(unsigned int cpu)
  20198. +{
  20199. + return ksoftirqd_softirq_pending();
  20200. }
  20201. #ifdef CONFIG_HOTPLUG_CPU
  20202. @@ -746,16 +1245,31 @@
  20203. static struct smp_hotplug_thread softirq_threads = {
  20204. .store = &ksoftirqd,
  20205. + .setup = ksoftirqd_set_sched_params,
  20206. .thread_should_run = ksoftirqd_should_run,
  20207. .thread_fn = run_ksoftirqd,
  20208. .thread_comm = "ksoftirqd/%u",
  20209. };
  20210. +#ifdef CONFIG_PREEMPT_RT_FULL
  20211. +static struct smp_hotplug_thread softirq_timer_threads = {
  20212. + .store = &ktimer_softirqd,
  20213. + .setup = ktimer_softirqd_set_sched_params,
  20214. + .cleanup = ktimer_softirqd_clr_sched_params,
  20215. + .thread_should_run = ktimer_softirqd_should_run,
  20216. + .thread_fn = run_ksoftirqd,
  20217. + .thread_comm = "ktimersoftd/%u",
  20218. +};
  20219. +#endif
  20220. +
  20221. static __init int spawn_ksoftirqd(void)
  20222. {
  20223. register_cpu_notifier(&cpu_nfb);
  20224. BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
  20225. +#ifdef CONFIG_PREEMPT_RT_FULL
  20226. + BUG_ON(smpboot_register_percpu_thread(&softirq_timer_threads));
  20227. +#endif
  20228. return 0;
  20229. }
  20230. diff -Nur linux-4.1.20.orig/kernel/stop_machine.c linux-4.1.20/kernel/stop_machine.c
  20231. --- linux-4.1.20.orig/kernel/stop_machine.c 2016-03-17 19:11:03.000000000 +0100
  20232. +++ linux-4.1.20/kernel/stop_machine.c 2016-03-21 20:18:33.000000000 +0100
  20233. @@ -35,7 +35,7 @@
  20234. /* the actual stopper, one per every possible cpu, enabled on online cpus */
  20235. struct cpu_stopper {
  20236. - spinlock_t lock;
  20237. + raw_spinlock_t lock;
  20238. bool enabled; /* is this stopper enabled? */
  20239. struct list_head works; /* list of pending works */
  20240. };
  20241. @@ -78,7 +78,7 @@
  20242. unsigned long flags;
  20243. - spin_lock_irqsave(&stopper->lock, flags);
  20244. + raw_spin_lock_irqsave(&stopper->lock, flags);
  20245. if (stopper->enabled) {
  20246. list_add_tail(&work->list, &stopper->works);
  20247. @@ -86,7 +86,7 @@
  20248. } else
  20249. cpu_stop_signal_done(work->done, false);
  20250. - spin_unlock_irqrestore(&stopper->lock, flags);
  20251. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  20252. }
  20253. /**
  20254. @@ -248,7 +248,7 @@
  20255. struct irq_cpu_stop_queue_work_info call_args;
  20256. struct multi_stop_data msdata;
  20257. - preempt_disable();
  20258. + preempt_disable_nort();
  20259. msdata = (struct multi_stop_data){
  20260. .fn = fn,
  20261. .data = arg,
  20262. @@ -281,7 +281,7 @@
  20263. * This relies on the stopper workqueues to be FIFO.
  20264. */
  20265. if (!cpu_active(cpu1) || !cpu_active(cpu2)) {
  20266. - preempt_enable();
  20267. + preempt_enable_nort();
  20268. return -ENOENT;
  20269. }
  20270. @@ -295,7 +295,7 @@
  20271. &irq_cpu_stop_queue_work,
  20272. &call_args, 1);
  20273. lg_local_unlock(&stop_cpus_lock);
  20274. - preempt_enable();
  20275. + preempt_enable_nort();
  20276. wait_for_completion(&done.completion);
  20277. @@ -329,7 +329,7 @@
  20278. static void queue_stop_cpus_work(const struct cpumask *cpumask,
  20279. cpu_stop_fn_t fn, void *arg,
  20280. - struct cpu_stop_done *done)
  20281. + struct cpu_stop_done *done, bool inactive)
  20282. {
  20283. struct cpu_stop_work *work;
  20284. unsigned int cpu;
  20285. @@ -343,11 +343,13 @@
  20286. }
  20287. /*
  20288. - * Disable preemption while queueing to avoid getting
  20289. - * preempted by a stopper which might wait for other stoppers
  20290. - * to enter @fn which can lead to deadlock.
  20291. + * Make sure that all work is queued on all cpus before
  20292. + * any of the cpus can execute it.
  20293. */
  20294. - lg_global_lock(&stop_cpus_lock);
  20295. + if (!inactive)
  20296. + lg_global_lock(&stop_cpus_lock);
  20297. + else
  20298. + lg_global_trylock_relax(&stop_cpus_lock);
  20299. for_each_cpu(cpu, cpumask)
  20300. cpu_stop_queue_work(cpu, &per_cpu(stop_cpus_work, cpu));
  20301. lg_global_unlock(&stop_cpus_lock);
  20302. @@ -359,7 +361,7 @@
  20303. struct cpu_stop_done done;
  20304. cpu_stop_init_done(&done, cpumask_weight(cpumask));
  20305. - queue_stop_cpus_work(cpumask, fn, arg, &done);
  20306. + queue_stop_cpus_work(cpumask, fn, arg, &done, false);
  20307. wait_for_completion(&done.completion);
  20308. return done.executed ? done.ret : -ENOENT;
  20309. }
  20310. @@ -439,9 +441,9 @@
  20311. unsigned long flags;
  20312. int run;
  20313. - spin_lock_irqsave(&stopper->lock, flags);
  20314. + raw_spin_lock_irqsave(&stopper->lock, flags);
  20315. run = !list_empty(&stopper->works);
  20316. - spin_unlock_irqrestore(&stopper->lock, flags);
  20317. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  20318. return run;
  20319. }
  20320. @@ -453,13 +455,13 @@
  20321. repeat:
  20322. work = NULL;
  20323. - spin_lock_irq(&stopper->lock);
  20324. + raw_spin_lock_irq(&stopper->lock);
  20325. if (!list_empty(&stopper->works)) {
  20326. work = list_first_entry(&stopper->works,
  20327. struct cpu_stop_work, list);
  20328. list_del_init(&work->list);
  20329. }
  20330. - spin_unlock_irq(&stopper->lock);
  20331. + raw_spin_unlock_irq(&stopper->lock);
  20332. if (work) {
  20333. cpu_stop_fn_t fn = work->fn;
  20334. @@ -467,6 +469,16 @@
  20335. struct cpu_stop_done *done = work->done;
  20336. char ksym_buf[KSYM_NAME_LEN] __maybe_unused;
  20337. + /*
  20338. + * Wait until the stopper finished scheduling on all
  20339. + * cpus
  20340. + */
  20341. + lg_global_lock(&stop_cpus_lock);
  20342. + /*
  20343. + * Let other cpu threads continue as well
  20344. + */
  20345. + lg_global_unlock(&stop_cpus_lock);
  20346. +
  20347. /* cpu stop callbacks are not allowed to sleep */
  20348. preempt_disable();
  20349. @@ -500,20 +512,20 @@
  20350. unsigned long flags;
  20351. /* drain remaining works */
  20352. - spin_lock_irqsave(&stopper->lock, flags);
  20353. + raw_spin_lock_irqsave(&stopper->lock, flags);
  20354. list_for_each_entry(work, &stopper->works, list)
  20355. cpu_stop_signal_done(work->done, false);
  20356. stopper->enabled = false;
  20357. - spin_unlock_irqrestore(&stopper->lock, flags);
  20358. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  20359. }
  20360. static void cpu_stop_unpark(unsigned int cpu)
  20361. {
  20362. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  20363. - spin_lock_irq(&stopper->lock);
  20364. + raw_spin_lock_irq(&stopper->lock);
  20365. stopper->enabled = true;
  20366. - spin_unlock_irq(&stopper->lock);
  20367. + raw_spin_unlock_irq(&stopper->lock);
  20368. }
  20369. static struct smp_hotplug_thread cpu_stop_threads = {
  20370. @@ -535,10 +547,12 @@
  20371. for_each_possible_cpu(cpu) {
  20372. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  20373. - spin_lock_init(&stopper->lock);
  20374. + raw_spin_lock_init(&stopper->lock);
  20375. INIT_LIST_HEAD(&stopper->works);
  20376. }
  20377. + lg_lock_init(&stop_cpus_lock, "stop_cpus_lock");
  20378. +
  20379. BUG_ON(smpboot_register_percpu_thread(&cpu_stop_threads));
  20380. stop_machine_initialized = true;
  20381. return 0;
  20382. @@ -634,7 +648,7 @@
  20383. set_state(&msdata, MULTI_STOP_PREPARE);
  20384. cpu_stop_init_done(&done, num_active_cpus());
  20385. queue_stop_cpus_work(cpu_active_mask, multi_cpu_stop, &msdata,
  20386. - &done);
  20387. + &done, true);
  20388. ret = multi_cpu_stop(&msdata);
  20389. /* Busy wait for completion. */
  20390. diff -Nur linux-4.1.20.orig/kernel/time/hrtimer.c linux-4.1.20/kernel/time/hrtimer.c
  20391. --- linux-4.1.20.orig/kernel/time/hrtimer.c 2016-03-17 19:11:03.000000000 +0100
  20392. +++ linux-4.1.20/kernel/time/hrtimer.c 2016-03-21 20:18:33.000000000 +0100
  20393. @@ -48,11 +48,13 @@
  20394. #include <linux/sched/rt.h>
  20395. #include <linux/sched/deadline.h>
  20396. #include <linux/timer.h>
  20397. +#include <linux/kthread.h>
  20398. #include <linux/freezer.h>
  20399. #include <asm/uaccess.h>
  20400. #include <trace/events/timer.h>
  20401. +#include <trace/events/hist.h>
  20402. #include "tick-internal.h"
  20403. @@ -576,8 +578,7 @@
  20404. * When the callback is running, we do not reprogram the clock event
  20405. * device. The timer callback is either running on a different CPU or
  20406. * the callback is executed in the hrtimer_interrupt context. The
  20407. - * reprogramming is handled either by the softirq, which called the
  20408. - * callback or at the end of the hrtimer_interrupt.
  20409. + * reprogramming is handled at the end of the hrtimer_interrupt.
  20410. */
  20411. if (hrtimer_callback_running(timer))
  20412. return 0;
  20413. @@ -621,6 +622,9 @@
  20414. return res;
  20415. }
  20416. +static void __run_hrtimer(struct hrtimer *timer, ktime_t *now);
  20417. +static int hrtimer_rt_defer(struct hrtimer *timer);
  20418. +
  20419. /*
  20420. * Initialize the high resolution related parts of cpu_base
  20421. */
  20422. @@ -630,6 +634,21 @@
  20423. base->hres_active = 0;
  20424. }
  20425. +static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  20426. + struct hrtimer_clock_base *base,
  20427. + int wakeup)
  20428. +{
  20429. + if (!hrtimer_reprogram(timer, base))
  20430. + return 0;
  20431. + if (!wakeup)
  20432. + return -ETIME;
  20433. +#ifdef CONFIG_PREEMPT_RT_BASE
  20434. + if (!hrtimer_rt_defer(timer))
  20435. + return -ETIME;
  20436. +#endif
  20437. + return 1;
  20438. +}
  20439. +
  20440. static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
  20441. {
  20442. ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
  20443. @@ -695,6 +714,44 @@
  20444. static DECLARE_WORK(hrtimer_work, clock_was_set_work);
  20445. +#ifdef CONFIG_PREEMPT_RT_FULL
  20446. +/*
  20447. + * RT can not call schedule_work from real interrupt context.
  20448. + * Need to make a thread to do the real work.
  20449. + */
  20450. +static struct task_struct *clock_set_delay_thread;
  20451. +static bool do_clock_set_delay;
  20452. +
  20453. +static int run_clock_set_delay(void *ignore)
  20454. +{
  20455. + while (!kthread_should_stop()) {
  20456. + set_current_state(TASK_INTERRUPTIBLE);
  20457. + if (do_clock_set_delay) {
  20458. + do_clock_set_delay = false;
  20459. + schedule_work(&hrtimer_work);
  20460. + }
  20461. + schedule();
  20462. + }
  20463. + __set_current_state(TASK_RUNNING);
  20464. + return 0;
  20465. +}
  20466. +
  20467. +void clock_was_set_delayed(void)
  20468. +{
  20469. + do_clock_set_delay = true;
  20470. + /* Make visible before waking up process */
  20471. + smp_wmb();
  20472. + wake_up_process(clock_set_delay_thread);
  20473. +}
  20474. +
  20475. +static __init int create_clock_set_delay_thread(void)
  20476. +{
  20477. + clock_set_delay_thread = kthread_run(run_clock_set_delay, NULL, "kclksetdelayd");
  20478. + BUG_ON(!clock_set_delay_thread);
  20479. + return 0;
  20480. +}
  20481. +early_initcall(create_clock_set_delay_thread);
  20482. +#else /* PREEMPT_RT_FULL */
  20483. /*
  20484. * Called from timekeeping and resume code to reprogramm the hrtimer
  20485. * interrupt device on all cpus.
  20486. @@ -703,6 +760,7 @@
  20487. {
  20488. schedule_work(&hrtimer_work);
  20489. }
  20490. +#endif
  20491. #else
  20492. @@ -711,6 +769,13 @@
  20493. static inline int hrtimer_switch_to_hres(void) { return 0; }
  20494. static inline void
  20495. hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
  20496. +static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  20497. + struct hrtimer_clock_base *base,
  20498. + int wakeup)
  20499. +{
  20500. + return 0;
  20501. +}
  20502. +
  20503. static inline int hrtimer_reprogram(struct hrtimer *timer,
  20504. struct hrtimer_clock_base *base)
  20505. {
  20506. @@ -718,7 +783,6 @@
  20507. }
  20508. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  20509. static inline void retrigger_next_event(void *arg) { }
  20510. -
  20511. #endif /* CONFIG_HIGH_RES_TIMERS */
  20512. /*
  20513. @@ -836,6 +900,32 @@
  20514. }
  20515. EXPORT_SYMBOL_GPL(hrtimer_forward);
  20516. +#ifdef CONFIG_PREEMPT_RT_BASE
  20517. +# define wake_up_timer_waiters(b) wake_up(&(b)->wait)
  20518. +
  20519. +/**
  20520. + * hrtimer_wait_for_timer - Wait for a running timer
  20521. + *
  20522. + * @timer: timer to wait for
  20523. + *
  20524. + * The function waits in case the timers callback function is
  20525. + * currently executed on the waitqueue of the timer base. The
  20526. + * waitqueue is woken up after the timer callback function has
  20527. + * finished execution.
  20528. + */
  20529. +void hrtimer_wait_for_timer(const struct hrtimer *timer)
  20530. +{
  20531. + struct hrtimer_clock_base *base = timer->base;
  20532. +
  20533. + if (base && base->cpu_base && !timer->irqsafe)
  20534. + wait_event(base->cpu_base->wait,
  20535. + !(timer->state & HRTIMER_STATE_CALLBACK));
  20536. +}
  20537. +
  20538. +#else
  20539. +# define wake_up_timer_waiters(b) do { } while (0)
  20540. +#endif
  20541. +
  20542. /*
  20543. * enqueue_hrtimer - internal function to (re)start a timer
  20544. *
  20545. @@ -879,6 +969,11 @@
  20546. if (!(timer->state & HRTIMER_STATE_ENQUEUED))
  20547. goto out;
  20548. + if (unlikely(!list_empty(&timer->cb_entry))) {
  20549. + list_del_init(&timer->cb_entry);
  20550. + goto out;
  20551. + }
  20552. +
  20553. next_timer = timerqueue_getnext(&base->active);
  20554. timerqueue_del(&base->active, &timer->node);
  20555. if (&timer->node == next_timer) {
  20556. @@ -966,7 +1061,16 @@
  20557. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  20558. timer_stats_hrtimer_set_start_info(timer);
  20559. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  20560. + {
  20561. + ktime_t now = new_base->get_time();
  20562. + if (ktime_to_ns(tim) < ktime_to_ns(now))
  20563. + timer->praecox = now;
  20564. + else
  20565. + timer->praecox = ktime_set(0, 0);
  20566. + }
  20567. +#endif
  20568. leftmost = enqueue_hrtimer(timer, new_base);
  20569. if (!leftmost) {
  20570. @@ -980,15 +1084,26 @@
  20571. * on dynticks target.
  20572. */
  20573. wake_up_nohz_cpu(new_base->cpu_base->cpu);
  20574. - } else if (new_base->cpu_base == this_cpu_ptr(&hrtimer_bases) &&
  20575. - hrtimer_reprogram(timer, new_base)) {
  20576. + } else if (new_base->cpu_base == this_cpu_ptr(&hrtimer_bases)) {
  20577. +
  20578. + ret = hrtimer_enqueue_reprogram(timer, new_base, wakeup);
  20579. + if (ret < 0) {
  20580. + /*
  20581. + * In case we failed to reprogram the timer (mostly
  20582. + * because out current timer is already elapsed),
  20583. + * remove it again and report a failure. This avoids
  20584. + * stale base->first entries.
  20585. + */
  20586. + debug_deactivate(timer);
  20587. + __remove_hrtimer(timer, new_base,
  20588. + timer->state & HRTIMER_STATE_CALLBACK, 0);
  20589. + } else if (ret > 0) {
  20590. /*
  20591. * Only allow reprogramming if the new base is on this CPU.
  20592. * (it might still be on another CPU if the timer was pending)
  20593. *
  20594. * XXX send_remote_softirq() ?
  20595. */
  20596. - if (wakeup) {
  20597. /*
  20598. * We need to drop cpu_base->lock to avoid a
  20599. * lock ordering issue vs. rq->lock.
  20600. @@ -996,9 +1111,7 @@
  20601. raw_spin_unlock(&new_base->cpu_base->lock);
  20602. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  20603. local_irq_restore(flags);
  20604. - return ret;
  20605. - } else {
  20606. - __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  20607. + return 0;
  20608. }
  20609. }
  20610. @@ -1089,7 +1202,7 @@
  20611. if (ret >= 0)
  20612. return ret;
  20613. - cpu_relax();
  20614. + hrtimer_wait_for_timer(timer);
  20615. }
  20616. }
  20617. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  20618. @@ -1153,6 +1266,7 @@
  20619. base = hrtimer_clockid_to_base(clock_id);
  20620. timer->base = &cpu_base->clock_base[base];
  20621. + INIT_LIST_HEAD(&timer->cb_entry);
  20622. timerqueue_init(&timer->node);
  20623. #ifdef CONFIG_TIMER_STATS
  20624. @@ -1236,6 +1350,126 @@
  20625. timer->state &= ~HRTIMER_STATE_CALLBACK;
  20626. }
  20627. +static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer);
  20628. +
  20629. +#ifdef CONFIG_PREEMPT_RT_BASE
  20630. +static void hrtimer_rt_reprogram(int restart, struct hrtimer *timer,
  20631. + struct hrtimer_clock_base *base)
  20632. +{
  20633. + /*
  20634. + * Note, we clear the callback flag before we requeue the
  20635. + * timer otherwise we trigger the callback_running() check
  20636. + * in hrtimer_reprogram().
  20637. + */
  20638. + timer->state &= ~HRTIMER_STATE_CALLBACK;
  20639. +
  20640. + if (restart != HRTIMER_NORESTART) {
  20641. + BUG_ON(hrtimer_active(timer));
  20642. + /*
  20643. + * Enqueue the timer, if it's the leftmost timer then
  20644. + * we need to reprogram it.
  20645. + */
  20646. + if (!enqueue_hrtimer(timer, base))
  20647. + return;
  20648. +
  20649. +#ifndef CONFIG_HIGH_RES_TIMERS
  20650. + }
  20651. +#else
  20652. + if (base->cpu_base->hres_active &&
  20653. + hrtimer_reprogram(timer, base))
  20654. + goto requeue;
  20655. +
  20656. + } else if (hrtimer_active(timer)) {
  20657. + /*
  20658. + * If the timer was rearmed on another CPU, reprogram
  20659. + * the event device.
  20660. + */
  20661. + if (&timer->node == base->active.next &&
  20662. + base->cpu_base->hres_active &&
  20663. + hrtimer_reprogram(timer, base))
  20664. + goto requeue;
  20665. + }
  20666. + return;
  20667. +
  20668. +requeue:
  20669. + /*
  20670. + * Timer is expired. Thus move it from tree to pending list
  20671. + * again.
  20672. + */
  20673. + __remove_hrtimer(timer, base, timer->state, 0);
  20674. + list_add_tail(&timer->cb_entry, &base->expired);
  20675. +#endif
  20676. +}
  20677. +
  20678. +/*
  20679. + * The changes in mainline which removed the callback modes from
  20680. + * hrtimer are not yet working with -rt. The non wakeup_process()
  20681. + * based callbacks which involve sleeping locks need to be treated
  20682. + * seperately.
  20683. + */
  20684. +static void hrtimer_rt_run_pending(void)
  20685. +{
  20686. + enum hrtimer_restart (*fn)(struct hrtimer *);
  20687. + struct hrtimer_cpu_base *cpu_base;
  20688. + struct hrtimer_clock_base *base;
  20689. + struct hrtimer *timer;
  20690. + int index, restart;
  20691. +
  20692. + local_irq_disable();
  20693. + cpu_base = &per_cpu(hrtimer_bases, smp_processor_id());
  20694. +
  20695. + raw_spin_lock(&cpu_base->lock);
  20696. +
  20697. + for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  20698. + base = &cpu_base->clock_base[index];
  20699. +
  20700. + while (!list_empty(&base->expired)) {
  20701. + timer = list_first_entry(&base->expired,
  20702. + struct hrtimer, cb_entry);
  20703. +
  20704. + /*
  20705. + * Same as the above __run_hrtimer function
  20706. + * just we run with interrupts enabled.
  20707. + */
  20708. + debug_hrtimer_deactivate(timer);
  20709. + __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  20710. + timer_stats_account_hrtimer(timer);
  20711. + fn = timer->function;
  20712. +
  20713. + raw_spin_unlock_irq(&cpu_base->lock);
  20714. + restart = fn(timer);
  20715. + raw_spin_lock_irq(&cpu_base->lock);
  20716. +
  20717. + hrtimer_rt_reprogram(restart, timer, base);
  20718. + }
  20719. + }
  20720. +
  20721. + raw_spin_unlock_irq(&cpu_base->lock);
  20722. +
  20723. + wake_up_timer_waiters(cpu_base);
  20724. +}
  20725. +
  20726. +static int hrtimer_rt_defer(struct hrtimer *timer)
  20727. +{
  20728. + if (timer->irqsafe)
  20729. + return 0;
  20730. +
  20731. + __remove_hrtimer(timer, timer->base, timer->state, 0);
  20732. + list_add_tail(&timer->cb_entry, &timer->base->expired);
  20733. + return 1;
  20734. +}
  20735. +
  20736. +#else
  20737. +
  20738. +static inline void hrtimer_rt_run_pending(void)
  20739. +{
  20740. + hrtimer_peek_ahead_timers();
  20741. +}
  20742. +
  20743. +static inline int hrtimer_rt_defer(struct hrtimer *timer) { return 0; }
  20744. +
  20745. +#endif
  20746. +
  20747. #ifdef CONFIG_HIGH_RES_TIMERS
  20748. /*
  20749. @@ -1246,7 +1480,7 @@
  20750. {
  20751. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  20752. ktime_t expires_next, now, entry_time, delta;
  20753. - int i, retries = 0;
  20754. + int i, retries = 0, raise = 0;
  20755. BUG_ON(!cpu_base->hres_active);
  20756. cpu_base->nr_events++;
  20757. @@ -1281,6 +1515,15 @@
  20758. timer = container_of(node, struct hrtimer, node);
  20759. + trace_hrtimer_interrupt(raw_smp_processor_id(),
  20760. + ktime_to_ns(ktime_sub(ktime_to_ns(timer->praecox) ?
  20761. + timer->praecox : hrtimer_get_expires(timer),
  20762. + basenow)),
  20763. + current,
  20764. + timer->function == hrtimer_wakeup ?
  20765. + container_of(timer, struct hrtimer_sleeper,
  20766. + timer)->task : NULL);
  20767. +
  20768. /*
  20769. * The immediate goal for using the softexpires is
  20770. * minimizing wakeups, not running timers at the
  20771. @@ -1296,7 +1539,10 @@
  20772. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer))
  20773. break;
  20774. - __run_hrtimer(timer, &basenow);
  20775. + if (!hrtimer_rt_defer(timer))
  20776. + __run_hrtimer(timer, &basenow);
  20777. + else
  20778. + raise = 1;
  20779. }
  20780. }
  20781. /* Reevaluate the clock bases for the next expiry */
  20782. @@ -1313,7 +1559,7 @@
  20783. if (expires_next.tv64 == KTIME_MAX ||
  20784. !tick_program_event(expires_next, 0)) {
  20785. cpu_base->hang_detected = 0;
  20786. - return;
  20787. + goto out;
  20788. }
  20789. /*
  20790. @@ -1357,6 +1603,9 @@
  20791. tick_program_event(expires_next, 1);
  20792. printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
  20793. ktime_to_ns(delta));
  20794. +out:
  20795. + if (raise)
  20796. + raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  20797. }
  20798. /*
  20799. @@ -1392,18 +1641,18 @@
  20800. __hrtimer_peek_ahead_timers();
  20801. local_irq_restore(flags);
  20802. }
  20803. -
  20804. -static void run_hrtimer_softirq(struct softirq_action *h)
  20805. -{
  20806. - hrtimer_peek_ahead_timers();
  20807. -}
  20808. -
  20809. #else /* CONFIG_HIGH_RES_TIMERS */
  20810. static inline void __hrtimer_peek_ahead_timers(void) { }
  20811. #endif /* !CONFIG_HIGH_RES_TIMERS */
  20812. +
  20813. +static void run_hrtimer_softirq(struct softirq_action *h)
  20814. +{
  20815. + hrtimer_rt_run_pending();
  20816. +}
  20817. +
  20818. /*
  20819. * Called from timer softirq every jiffy, expire hrtimers:
  20820. *
  20821. @@ -1436,7 +1685,7 @@
  20822. struct timerqueue_node *node;
  20823. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  20824. struct hrtimer_clock_base *base;
  20825. - int index, gettime = 1;
  20826. + int index, gettime = 1, raise = 0;
  20827. if (hrtimer_hres_active())
  20828. return;
  20829. @@ -1461,10 +1710,16 @@
  20830. hrtimer_get_expires_tv64(timer))
  20831. break;
  20832. - __run_hrtimer(timer, &base->softirq_time);
  20833. + if (!hrtimer_rt_defer(timer))
  20834. + __run_hrtimer(timer, &base->softirq_time);
  20835. + else
  20836. + raise = 1;
  20837. }
  20838. raw_spin_unlock(&cpu_base->lock);
  20839. }
  20840. +
  20841. + if (raise)
  20842. + raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  20843. }
  20844. /*
  20845. @@ -1486,16 +1741,18 @@
  20846. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  20847. {
  20848. sl->timer.function = hrtimer_wakeup;
  20849. + sl->timer.irqsafe = 1;
  20850. sl->task = task;
  20851. }
  20852. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  20853. -static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  20854. +static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode,
  20855. + unsigned long state)
  20856. {
  20857. hrtimer_init_sleeper(t, current);
  20858. do {
  20859. - set_current_state(TASK_INTERRUPTIBLE);
  20860. + set_current_state(state);
  20861. hrtimer_start_expires(&t->timer, mode);
  20862. if (!hrtimer_active(&t->timer))
  20863. t->task = NULL;
  20864. @@ -1539,7 +1796,8 @@
  20865. HRTIMER_MODE_ABS);
  20866. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  20867. - if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  20868. + /* cpu_chill() does not care about restart state. */
  20869. + if (do_nanosleep(&t, HRTIMER_MODE_ABS, TASK_INTERRUPTIBLE))
  20870. goto out;
  20871. rmtp = restart->nanosleep.rmtp;
  20872. @@ -1556,8 +1814,10 @@
  20873. return ret;
  20874. }
  20875. -long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  20876. - const enum hrtimer_mode mode, const clockid_t clockid)
  20877. +static long
  20878. +__hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  20879. + const enum hrtimer_mode mode, const clockid_t clockid,
  20880. + unsigned long state)
  20881. {
  20882. struct restart_block *restart;
  20883. struct hrtimer_sleeper t;
  20884. @@ -1570,7 +1830,7 @@
  20885. hrtimer_init_on_stack(&t.timer, clockid, mode);
  20886. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  20887. - if (do_nanosleep(&t, mode))
  20888. + if (do_nanosleep(&t, mode, state))
  20889. goto out;
  20890. /* Absolute timers do not update the rmtp value and restart: */
  20891. @@ -1597,6 +1857,12 @@
  20892. return ret;
  20893. }
  20894. +long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  20895. + const enum hrtimer_mode mode, const clockid_t clockid)
  20896. +{
  20897. + return __hrtimer_nanosleep(rqtp, rmtp, mode, clockid, TASK_INTERRUPTIBLE);
  20898. +}
  20899. +
  20900. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  20901. struct timespec __user *, rmtp)
  20902. {
  20903. @@ -1611,6 +1877,26 @@
  20904. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  20905. }
  20906. +#ifdef CONFIG_PREEMPT_RT_FULL
  20907. +/*
  20908. + * Sleep for 1 ms in hope whoever holds what we want will let it go.
  20909. + */
  20910. +void cpu_chill(void)
  20911. +{
  20912. + struct timespec tu = {
  20913. + .tv_nsec = NSEC_PER_MSEC,
  20914. + };
  20915. + unsigned int freeze_flag = current->flags & PF_NOFREEZE;
  20916. +
  20917. + current->flags |= PF_NOFREEZE;
  20918. + __hrtimer_nanosleep(&tu, NULL, HRTIMER_MODE_REL, CLOCK_MONOTONIC,
  20919. + TASK_UNINTERRUPTIBLE);
  20920. + if (!freeze_flag)
  20921. + current->flags &= ~PF_NOFREEZE;
  20922. +}
  20923. +EXPORT_SYMBOL(cpu_chill);
  20924. +#endif
  20925. +
  20926. /*
  20927. * Functions related to boot-time initialization:
  20928. */
  20929. @@ -1622,10 +1908,14 @@
  20930. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  20931. cpu_base->clock_base[i].cpu_base = cpu_base;
  20932. timerqueue_init_head(&cpu_base->clock_base[i].active);
  20933. + INIT_LIST_HEAD(&cpu_base->clock_base[i].expired);
  20934. }
  20935. cpu_base->cpu = cpu;
  20936. hrtimer_init_hres(cpu_base);
  20937. +#ifdef CONFIG_PREEMPT_RT_BASE
  20938. + init_waitqueue_head(&cpu_base->wait);
  20939. +#endif
  20940. }
  20941. #ifdef CONFIG_HOTPLUG_CPU
  20942. @@ -1731,9 +2021,7 @@
  20943. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  20944. (void *)(long)smp_processor_id());
  20945. register_cpu_notifier(&hrtimers_nb);
  20946. -#ifdef CONFIG_HIGH_RES_TIMERS
  20947. open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  20948. -#endif
  20949. }
  20950. /**
  20951. diff -Nur linux-4.1.20.orig/kernel/time/itimer.c linux-4.1.20/kernel/time/itimer.c
  20952. --- linux-4.1.20.orig/kernel/time/itimer.c 2016-03-17 19:11:03.000000000 +0100
  20953. +++ linux-4.1.20/kernel/time/itimer.c 2016-03-21 20:18:33.000000000 +0100
  20954. @@ -213,6 +213,7 @@
  20955. /* We are sharing ->siglock with it_real_fn() */
  20956. if (hrtimer_try_to_cancel(timer) < 0) {
  20957. spin_unlock_irq(&tsk->sighand->siglock);
  20958. + hrtimer_wait_for_timer(&tsk->signal->real_timer);
  20959. goto again;
  20960. }
  20961. expires = timeval_to_ktime(value->it_value);
  20962. diff -Nur linux-4.1.20.orig/kernel/time/jiffies.c linux-4.1.20/kernel/time/jiffies.c
  20963. --- linux-4.1.20.orig/kernel/time/jiffies.c 2016-03-17 19:11:03.000000000 +0100
  20964. +++ linux-4.1.20/kernel/time/jiffies.c 2016-03-21 20:18:33.000000000 +0100
  20965. @@ -74,7 +74,8 @@
  20966. .max_cycles = 10,
  20967. };
  20968. -__cacheline_aligned_in_smp DEFINE_SEQLOCK(jiffies_lock);
  20969. +__cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock);
  20970. +__cacheline_aligned_in_smp seqcount_t jiffies_seq;
  20971. #if (BITS_PER_LONG < 64)
  20972. u64 get_jiffies_64(void)
  20973. @@ -83,9 +84,9 @@
  20974. u64 ret;
  20975. do {
  20976. - seq = read_seqbegin(&jiffies_lock);
  20977. + seq = read_seqcount_begin(&jiffies_seq);
  20978. ret = jiffies_64;
  20979. - } while (read_seqretry(&jiffies_lock, seq));
  20980. + } while (read_seqcount_retry(&jiffies_seq, seq));
  20981. return ret;
  20982. }
  20983. EXPORT_SYMBOL(get_jiffies_64);
  20984. diff -Nur linux-4.1.20.orig/kernel/time/ntp.c linux-4.1.20/kernel/time/ntp.c
  20985. --- linux-4.1.20.orig/kernel/time/ntp.c 2016-03-17 19:11:03.000000000 +0100
  20986. +++ linux-4.1.20/kernel/time/ntp.c 2016-03-21 20:18:33.000000000 +0100
  20987. @@ -10,6 +10,7 @@
  20988. #include <linux/workqueue.h>
  20989. #include <linux/hrtimer.h>
  20990. #include <linux/jiffies.h>
  20991. +#include <linux/kthread.h>
  20992. #include <linux/math64.h>
  20993. #include <linux/timex.h>
  20994. #include <linux/time.h>
  20995. @@ -529,10 +530,52 @@
  20996. &sync_cmos_work, timespec_to_jiffies(&next));
  20997. }
  20998. +#ifdef CONFIG_PREEMPT_RT_FULL
  20999. +/*
  21000. + * RT can not call schedule_delayed_work from real interrupt context.
  21001. + * Need to make a thread to do the real work.
  21002. + */
  21003. +static struct task_struct *cmos_delay_thread;
  21004. +static bool do_cmos_delay;
  21005. +
  21006. +static int run_cmos_delay(void *ignore)
  21007. +{
  21008. + while (!kthread_should_stop()) {
  21009. + set_current_state(TASK_INTERRUPTIBLE);
  21010. + if (do_cmos_delay) {
  21011. + do_cmos_delay = false;
  21012. + queue_delayed_work(system_power_efficient_wq,
  21013. + &sync_cmos_work, 0);
  21014. + }
  21015. + schedule();
  21016. + }
  21017. + __set_current_state(TASK_RUNNING);
  21018. + return 0;
  21019. +}
  21020. +
  21021. +void ntp_notify_cmos_timer(void)
  21022. +{
  21023. + do_cmos_delay = true;
  21024. + /* Make visible before waking up process */
  21025. + smp_wmb();
  21026. + wake_up_process(cmos_delay_thread);
  21027. +}
  21028. +
  21029. +static __init int create_cmos_delay_thread(void)
  21030. +{
  21031. + cmos_delay_thread = kthread_run(run_cmos_delay, NULL, "kcmosdelayd");
  21032. + BUG_ON(!cmos_delay_thread);
  21033. + return 0;
  21034. +}
  21035. +early_initcall(create_cmos_delay_thread);
  21036. +
  21037. +#else
  21038. +
  21039. void ntp_notify_cmos_timer(void)
  21040. {
  21041. queue_delayed_work(system_power_efficient_wq, &sync_cmos_work, 0);
  21042. }
  21043. +#endif /* CONFIG_PREEMPT_RT_FULL */
  21044. #else
  21045. void ntp_notify_cmos_timer(void) { }
  21046. diff -Nur linux-4.1.20.orig/kernel/time/posix-cpu-timers.c linux-4.1.20/kernel/time/posix-cpu-timers.c
  21047. --- linux-4.1.20.orig/kernel/time/posix-cpu-timers.c 2016-03-17 19:11:03.000000000 +0100
  21048. +++ linux-4.1.20/kernel/time/posix-cpu-timers.c 2016-03-21 20:18:33.000000000 +0100
  21049. @@ -3,6 +3,7 @@
  21050. */
  21051. #include <linux/sched.h>
  21052. +#include <linux/sched/rt.h>
  21053. #include <linux/posix-timers.h>
  21054. #include <linux/errno.h>
  21055. #include <linux/math64.h>
  21056. @@ -626,7 +627,7 @@
  21057. /*
  21058. * Disarm any old timer after extracting its expiry time.
  21059. */
  21060. - WARN_ON_ONCE(!irqs_disabled());
  21061. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  21062. ret = 0;
  21063. old_incr = timer->it.cpu.incr;
  21064. @@ -1047,7 +1048,7 @@
  21065. /*
  21066. * Now re-arm for the new expiry time.
  21067. */
  21068. - WARN_ON_ONCE(!irqs_disabled());
  21069. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  21070. arm_timer(timer);
  21071. unlock_task_sighand(p, &flags);
  21072. @@ -1113,10 +1114,11 @@
  21073. sig = tsk->signal;
  21074. if (sig->cputimer.running) {
  21075. struct task_cputime group_sample;
  21076. + unsigned long flags;
  21077. - raw_spin_lock(&sig->cputimer.lock);
  21078. + raw_spin_lock_irqsave(&sig->cputimer.lock, flags);
  21079. group_sample = sig->cputimer.cputime;
  21080. - raw_spin_unlock(&sig->cputimer.lock);
  21081. + raw_spin_unlock_irqrestore(&sig->cputimer.lock, flags);
  21082. if (task_cputime_expired(&group_sample, &sig->cputime_expires))
  21083. return 1;
  21084. @@ -1130,13 +1132,13 @@
  21085. * already updated our counts. We need to check if any timers fire now.
  21086. * Interrupts are disabled.
  21087. */
  21088. -void run_posix_cpu_timers(struct task_struct *tsk)
  21089. +static void __run_posix_cpu_timers(struct task_struct *tsk)
  21090. {
  21091. LIST_HEAD(firing);
  21092. struct k_itimer *timer, *next;
  21093. unsigned long flags;
  21094. - WARN_ON_ONCE(!irqs_disabled());
  21095. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  21096. /*
  21097. * The fast path checks that there are no expired thread or thread
  21098. @@ -1194,6 +1196,190 @@
  21099. }
  21100. }
  21101. +#ifdef CONFIG_PREEMPT_RT_BASE
  21102. +#include <linux/kthread.h>
  21103. +#include <linux/cpu.h>
  21104. +DEFINE_PER_CPU(struct task_struct *, posix_timer_task);
  21105. +DEFINE_PER_CPU(struct task_struct *, posix_timer_tasklist);
  21106. +
  21107. +static int posix_cpu_timers_thread(void *data)
  21108. +{
  21109. + int cpu = (long)data;
  21110. +
  21111. + BUG_ON(per_cpu(posix_timer_task,cpu) != current);
  21112. +
  21113. + while (!kthread_should_stop()) {
  21114. + struct task_struct *tsk = NULL;
  21115. + struct task_struct *next = NULL;
  21116. +
  21117. + if (cpu_is_offline(cpu))
  21118. + goto wait_to_die;
  21119. +
  21120. + /* grab task list */
  21121. + raw_local_irq_disable();
  21122. + tsk = per_cpu(posix_timer_tasklist, cpu);
  21123. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  21124. + raw_local_irq_enable();
  21125. +
  21126. + /* its possible the list is empty, just return */
  21127. + if (!tsk) {
  21128. + set_current_state(TASK_INTERRUPTIBLE);
  21129. + schedule();
  21130. + __set_current_state(TASK_RUNNING);
  21131. + continue;
  21132. + }
  21133. +
  21134. + /* Process task list */
  21135. + while (1) {
  21136. + /* save next */
  21137. + next = tsk->posix_timer_list;
  21138. +
  21139. + /* run the task timers, clear its ptr and
  21140. + * unreference it
  21141. + */
  21142. + __run_posix_cpu_timers(tsk);
  21143. + tsk->posix_timer_list = NULL;
  21144. + put_task_struct(tsk);
  21145. +
  21146. + /* check if this is the last on the list */
  21147. + if (next == tsk)
  21148. + break;
  21149. + tsk = next;
  21150. + }
  21151. + }
  21152. + return 0;
  21153. +
  21154. +wait_to_die:
  21155. + /* Wait for kthread_stop */
  21156. + set_current_state(TASK_INTERRUPTIBLE);
  21157. + while (!kthread_should_stop()) {
  21158. + schedule();
  21159. + set_current_state(TASK_INTERRUPTIBLE);
  21160. + }
  21161. + __set_current_state(TASK_RUNNING);
  21162. + return 0;
  21163. +}
  21164. +
  21165. +static inline int __fastpath_timer_check(struct task_struct *tsk)
  21166. +{
  21167. + /* tsk == current, ensure it is safe to use ->signal/sighand */
  21168. + if (unlikely(tsk->exit_state))
  21169. + return 0;
  21170. +
  21171. + if (!task_cputime_zero(&tsk->cputime_expires))
  21172. + return 1;
  21173. +
  21174. + if (!task_cputime_zero(&tsk->signal->cputime_expires))
  21175. + return 1;
  21176. +
  21177. + return 0;
  21178. +}
  21179. +
  21180. +void run_posix_cpu_timers(struct task_struct *tsk)
  21181. +{
  21182. + unsigned long cpu = smp_processor_id();
  21183. + struct task_struct *tasklist;
  21184. +
  21185. + BUG_ON(!irqs_disabled());
  21186. + if(!per_cpu(posix_timer_task, cpu))
  21187. + return;
  21188. + /* get per-cpu references */
  21189. + tasklist = per_cpu(posix_timer_tasklist, cpu);
  21190. +
  21191. + /* check to see if we're already queued */
  21192. + if (!tsk->posix_timer_list && __fastpath_timer_check(tsk)) {
  21193. + get_task_struct(tsk);
  21194. + if (tasklist) {
  21195. + tsk->posix_timer_list = tasklist;
  21196. + } else {
  21197. + /*
  21198. + * The list is terminated by a self-pointing
  21199. + * task_struct
  21200. + */
  21201. + tsk->posix_timer_list = tsk;
  21202. + }
  21203. + per_cpu(posix_timer_tasklist, cpu) = tsk;
  21204. +
  21205. + wake_up_process(per_cpu(posix_timer_task, cpu));
  21206. + }
  21207. +}
  21208. +
  21209. +/*
  21210. + * posix_cpu_thread_call - callback that gets triggered when a CPU is added.
  21211. + * Here we can start up the necessary migration thread for the new CPU.
  21212. + */
  21213. +static int posix_cpu_thread_call(struct notifier_block *nfb,
  21214. + unsigned long action, void *hcpu)
  21215. +{
  21216. + int cpu = (long)hcpu;
  21217. + struct task_struct *p;
  21218. + struct sched_param param;
  21219. +
  21220. + switch (action) {
  21221. + case CPU_UP_PREPARE:
  21222. + p = kthread_create(posix_cpu_timers_thread, hcpu,
  21223. + "posixcputmr/%d",cpu);
  21224. + if (IS_ERR(p))
  21225. + return NOTIFY_BAD;
  21226. + p->flags |= PF_NOFREEZE;
  21227. + kthread_bind(p, cpu);
  21228. + /* Must be high prio to avoid getting starved */
  21229. + param.sched_priority = MAX_RT_PRIO-1;
  21230. + sched_setscheduler(p, SCHED_FIFO, &param);
  21231. + per_cpu(posix_timer_task,cpu) = p;
  21232. + break;
  21233. + case CPU_ONLINE:
  21234. + /* Strictly unneccessary, as first user will wake it. */
  21235. + wake_up_process(per_cpu(posix_timer_task,cpu));
  21236. + break;
  21237. +#ifdef CONFIG_HOTPLUG_CPU
  21238. + case CPU_UP_CANCELED:
  21239. + /* Unbind it from offline cpu so it can run. Fall thru. */
  21240. + kthread_bind(per_cpu(posix_timer_task, cpu),
  21241. + cpumask_any(cpu_online_mask));
  21242. + kthread_stop(per_cpu(posix_timer_task,cpu));
  21243. + per_cpu(posix_timer_task,cpu) = NULL;
  21244. + break;
  21245. + case CPU_DEAD:
  21246. + kthread_stop(per_cpu(posix_timer_task,cpu));
  21247. + per_cpu(posix_timer_task,cpu) = NULL;
  21248. + break;
  21249. +#endif
  21250. + }
  21251. + return NOTIFY_OK;
  21252. +}
  21253. +
  21254. +/* Register at highest priority so that task migration (migrate_all_tasks)
  21255. + * happens before everything else.
  21256. + */
  21257. +static struct notifier_block posix_cpu_thread_notifier = {
  21258. + .notifier_call = posix_cpu_thread_call,
  21259. + .priority = 10
  21260. +};
  21261. +
  21262. +static int __init posix_cpu_thread_init(void)
  21263. +{
  21264. + void *hcpu = (void *)(long)smp_processor_id();
  21265. + /* Start one for boot CPU. */
  21266. + unsigned long cpu;
  21267. +
  21268. + /* init the per-cpu posix_timer_tasklets */
  21269. + for_each_possible_cpu(cpu)
  21270. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  21271. +
  21272. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_UP_PREPARE, hcpu);
  21273. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_ONLINE, hcpu);
  21274. + register_cpu_notifier(&posix_cpu_thread_notifier);
  21275. + return 0;
  21276. +}
  21277. +early_initcall(posix_cpu_thread_init);
  21278. +#else /* CONFIG_PREEMPT_RT_BASE */
  21279. +void run_posix_cpu_timers(struct task_struct *tsk)
  21280. +{
  21281. + __run_posix_cpu_timers(tsk);
  21282. +}
  21283. +#endif /* CONFIG_PREEMPT_RT_BASE */
  21284. +
  21285. /*
  21286. * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
  21287. * The tsk->sighand->siglock must be held by the caller.
  21288. diff -Nur linux-4.1.20.orig/kernel/time/posix-timers.c linux-4.1.20/kernel/time/posix-timers.c
  21289. --- linux-4.1.20.orig/kernel/time/posix-timers.c 2016-03-17 19:11:03.000000000 +0100
  21290. +++ linux-4.1.20/kernel/time/posix-timers.c 2016-03-21 20:18:34.000000000 +0100
  21291. @@ -499,6 +499,7 @@
  21292. static struct pid *good_sigevent(sigevent_t * event)
  21293. {
  21294. struct task_struct *rtn = current->group_leader;
  21295. + int sig = event->sigev_signo;
  21296. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  21297. (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
  21298. @@ -507,7 +508,8 @@
  21299. return NULL;
  21300. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  21301. - ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  21302. + (sig <= 0 || sig > SIGRTMAX || sig_kernel_only(sig) ||
  21303. + sig_kernel_coredump(sig)))
  21304. return NULL;
  21305. return task_pid(rtn);
  21306. @@ -819,6 +821,20 @@
  21307. return overrun;
  21308. }
  21309. +/*
  21310. + * Protected by RCU!
  21311. + */
  21312. +static void timer_wait_for_callback(struct k_clock *kc, struct k_itimer *timr)
  21313. +{
  21314. +#ifdef CONFIG_PREEMPT_RT_FULL
  21315. + if (kc->timer_set == common_timer_set)
  21316. + hrtimer_wait_for_timer(&timr->it.real.timer);
  21317. + else
  21318. + /* FIXME: Whacky hack for posix-cpu-timers */
  21319. + schedule_timeout(1);
  21320. +#endif
  21321. +}
  21322. +
  21323. /* Set a POSIX.1b interval timer. */
  21324. /* timr->it_lock is taken. */
  21325. static int
  21326. @@ -896,6 +912,7 @@
  21327. if (!timr)
  21328. return -EINVAL;
  21329. + rcu_read_lock();
  21330. kc = clockid_to_kclock(timr->it_clock);
  21331. if (WARN_ON_ONCE(!kc || !kc->timer_set))
  21332. error = -EINVAL;
  21333. @@ -904,9 +921,12 @@
  21334. unlock_timer(timr, flag);
  21335. if (error == TIMER_RETRY) {
  21336. + timer_wait_for_callback(kc, timr);
  21337. rtn = NULL; // We already got the old time...
  21338. + rcu_read_unlock();
  21339. goto retry;
  21340. }
  21341. + rcu_read_unlock();
  21342. if (old_setting && !error &&
  21343. copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
  21344. @@ -944,10 +964,15 @@
  21345. if (!timer)
  21346. return -EINVAL;
  21347. + rcu_read_lock();
  21348. if (timer_delete_hook(timer) == TIMER_RETRY) {
  21349. unlock_timer(timer, flags);
  21350. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  21351. + timer);
  21352. + rcu_read_unlock();
  21353. goto retry_delete;
  21354. }
  21355. + rcu_read_unlock();
  21356. spin_lock(&current->sighand->siglock);
  21357. list_del(&timer->list);
  21358. @@ -973,8 +998,18 @@
  21359. retry_delete:
  21360. spin_lock_irqsave(&timer->it_lock, flags);
  21361. + /* On RT we can race with a deletion */
  21362. + if (!timer->it_signal) {
  21363. + unlock_timer(timer, flags);
  21364. + return;
  21365. + }
  21366. +
  21367. if (timer_delete_hook(timer) == TIMER_RETRY) {
  21368. + rcu_read_lock();
  21369. unlock_timer(timer, flags);
  21370. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  21371. + timer);
  21372. + rcu_read_unlock();
  21373. goto retry_delete;
  21374. }
  21375. list_del(&timer->list);
  21376. diff -Nur linux-4.1.20.orig/kernel/time/tick-broadcast-hrtimer.c linux-4.1.20/kernel/time/tick-broadcast-hrtimer.c
  21377. --- linux-4.1.20.orig/kernel/time/tick-broadcast-hrtimer.c 2016-03-17 19:11:03.000000000 +0100
  21378. +++ linux-4.1.20/kernel/time/tick-broadcast-hrtimer.c 2016-03-21 20:18:34.000000000 +0100
  21379. @@ -109,5 +109,6 @@
  21380. {
  21381. hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  21382. bctimer.function = bc_handler;
  21383. + bctimer.irqsafe = true;
  21384. clockevents_register_device(&ce_broadcast_hrtimer);
  21385. }
  21386. diff -Nur linux-4.1.20.orig/kernel/time/tick-common.c linux-4.1.20/kernel/time/tick-common.c
  21387. --- linux-4.1.20.orig/kernel/time/tick-common.c 2016-03-17 19:11:03.000000000 +0100
  21388. +++ linux-4.1.20/kernel/time/tick-common.c 2016-03-21 20:18:34.000000000 +0100
  21389. @@ -78,13 +78,15 @@
  21390. static void tick_periodic(int cpu)
  21391. {
  21392. if (tick_do_timer_cpu == cpu) {
  21393. - write_seqlock(&jiffies_lock);
  21394. + raw_spin_lock(&jiffies_lock);
  21395. + write_seqcount_begin(&jiffies_seq);
  21396. /* Keep track of the next tick event */
  21397. tick_next_period = ktime_add(tick_next_period, tick_period);
  21398. do_timer(1);
  21399. - write_sequnlock(&jiffies_lock);
  21400. + write_seqcount_end(&jiffies_seq);
  21401. + raw_spin_unlock(&jiffies_lock);
  21402. update_wall_time();
  21403. }
  21404. @@ -146,9 +148,9 @@
  21405. ktime_t next;
  21406. do {
  21407. - seq = read_seqbegin(&jiffies_lock);
  21408. + seq = read_seqcount_begin(&jiffies_seq);
  21409. next = tick_next_period;
  21410. - } while (read_seqretry(&jiffies_lock, seq));
  21411. + } while (read_seqcount_retry(&jiffies_seq, seq));
  21412. clockevents_set_state(dev, CLOCK_EVT_STATE_ONESHOT);
  21413. diff -Nur linux-4.1.20.orig/kernel/time/tick-sched.c linux-4.1.20/kernel/time/tick-sched.c
  21414. --- linux-4.1.20.orig/kernel/time/tick-sched.c 2016-03-17 19:11:03.000000000 +0100
  21415. +++ linux-4.1.20/kernel/time/tick-sched.c 2016-03-21 20:18:34.000000000 +0100
  21416. @@ -62,7 +62,8 @@
  21417. return;
  21418. /* Reevalute with jiffies_lock held */
  21419. - write_seqlock(&jiffies_lock);
  21420. + raw_spin_lock(&jiffies_lock);
  21421. + write_seqcount_begin(&jiffies_seq);
  21422. delta = ktime_sub(now, last_jiffies_update);
  21423. if (delta.tv64 >= tick_period.tv64) {
  21424. @@ -85,10 +86,12 @@
  21425. /* Keep the tick_next_period variable up to date */
  21426. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  21427. } else {
  21428. - write_sequnlock(&jiffies_lock);
  21429. + write_seqcount_end(&jiffies_seq);
  21430. + raw_spin_unlock(&jiffies_lock);
  21431. return;
  21432. }
  21433. - write_sequnlock(&jiffies_lock);
  21434. + write_seqcount_end(&jiffies_seq);
  21435. + raw_spin_unlock(&jiffies_lock);
  21436. update_wall_time();
  21437. }
  21438. @@ -99,12 +102,14 @@
  21439. {
  21440. ktime_t period;
  21441. - write_seqlock(&jiffies_lock);
  21442. + raw_spin_lock(&jiffies_lock);
  21443. + write_seqcount_begin(&jiffies_seq);
  21444. /* Did we start the jiffies update yet ? */
  21445. if (last_jiffies_update.tv64 == 0)
  21446. last_jiffies_update = tick_next_period;
  21447. period = last_jiffies_update;
  21448. - write_sequnlock(&jiffies_lock);
  21449. + write_seqcount_end(&jiffies_seq);
  21450. + raw_spin_unlock(&jiffies_lock);
  21451. return period;
  21452. }
  21453. @@ -176,6 +181,11 @@
  21454. return false;
  21455. }
  21456. + if (!arch_irq_work_has_interrupt()) {
  21457. + trace_tick_stop(0, "missing irq work interrupt\n");
  21458. + return false;
  21459. + }
  21460. +
  21461. /* sched_clock_tick() needs us? */
  21462. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  21463. /*
  21464. @@ -222,6 +232,7 @@
  21465. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
  21466. .func = nohz_full_kick_work_func,
  21467. + .flags = IRQ_WORK_HARD_IRQ,
  21468. };
  21469. /*
  21470. @@ -578,10 +589,10 @@
  21471. /* Read jiffies and the time when jiffies were updated last */
  21472. do {
  21473. - seq = read_seqbegin(&jiffies_lock);
  21474. + seq = read_seqcount_begin(&jiffies_seq);
  21475. last_update = last_jiffies_update;
  21476. last_jiffies = jiffies;
  21477. - } while (read_seqretry(&jiffies_lock, seq));
  21478. + } while (read_seqcount_retry(&jiffies_seq, seq));
  21479. if (rcu_needs_cpu(&rcu_delta_jiffies) ||
  21480. arch_needs_cpu() || irq_work_needs_cpu()) {
  21481. @@ -759,14 +770,7 @@
  21482. return false;
  21483. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  21484. - static int ratelimit;
  21485. -
  21486. - if (ratelimit < 10 &&
  21487. - (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  21488. - pr_warn("NOHZ: local_softirq_pending %02x\n",
  21489. - (unsigned int) local_softirq_pending());
  21490. - ratelimit++;
  21491. - }
  21492. + softirq_check_pending_idle();
  21493. return false;
  21494. }
  21495. @@ -1154,6 +1158,7 @@
  21496. * Emulate tick processing via per-CPU hrtimers:
  21497. */
  21498. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  21499. + ts->sched_timer.irqsafe = 1;
  21500. ts->sched_timer.function = tick_sched_timer;
  21501. /* Get the next period (per cpu) */
  21502. diff -Nur linux-4.1.20.orig/kernel/time/timekeeping.c linux-4.1.20/kernel/time/timekeeping.c
  21503. --- linux-4.1.20.orig/kernel/time/timekeeping.c 2016-03-17 19:11:03.000000000 +0100
  21504. +++ linux-4.1.20/kernel/time/timekeeping.c 2016-03-21 20:18:34.000000000 +0100
  21505. @@ -2064,8 +2064,10 @@
  21506. */
  21507. void xtime_update(unsigned long ticks)
  21508. {
  21509. - write_seqlock(&jiffies_lock);
  21510. + raw_spin_lock(&jiffies_lock);
  21511. + write_seqcount_begin(&jiffies_seq);
  21512. do_timer(ticks);
  21513. - write_sequnlock(&jiffies_lock);
  21514. + write_seqcount_end(&jiffies_seq);
  21515. + raw_spin_unlock(&jiffies_lock);
  21516. update_wall_time();
  21517. }
  21518. diff -Nur linux-4.1.20.orig/kernel/time/timekeeping.h linux-4.1.20/kernel/time/timekeeping.h
  21519. --- linux-4.1.20.orig/kernel/time/timekeeping.h 2016-03-17 19:11:03.000000000 +0100
  21520. +++ linux-4.1.20/kernel/time/timekeeping.h 2016-03-21 20:18:34.000000000 +0100
  21521. @@ -22,7 +22,8 @@
  21522. extern void do_timer(unsigned long ticks);
  21523. extern void update_wall_time(void);
  21524. -extern seqlock_t jiffies_lock;
  21525. +extern raw_spinlock_t jiffies_lock;
  21526. +extern seqcount_t jiffies_seq;
  21527. #define CS_NAME_LEN 32
  21528. diff -Nur linux-4.1.20.orig/kernel/time/timer.c linux-4.1.20/kernel/time/timer.c
  21529. --- linux-4.1.20.orig/kernel/time/timer.c 2016-03-17 19:11:03.000000000 +0100
  21530. +++ linux-4.1.20/kernel/time/timer.c 2016-03-21 20:18:34.000000000 +0100
  21531. @@ -78,6 +78,9 @@
  21532. struct tvec_base {
  21533. spinlock_t lock;
  21534. struct timer_list *running_timer;
  21535. +#ifdef CONFIG_PREEMPT_RT_FULL
  21536. + wait_queue_head_t wait_for_running_timer;
  21537. +#endif
  21538. unsigned long timer_jiffies;
  21539. unsigned long next_timer;
  21540. unsigned long active_timers;
  21541. @@ -768,6 +771,36 @@
  21542. }
  21543. }
  21544. +#ifndef CONFIG_PREEMPT_RT_FULL
  21545. +static inline struct tvec_base *switch_timer_base(struct timer_list *timer,
  21546. + struct tvec_base *old,
  21547. + struct tvec_base *new)
  21548. +{
  21549. + /* See the comment in lock_timer_base() */
  21550. + timer_set_base(timer, NULL);
  21551. + spin_unlock(&old->lock);
  21552. + spin_lock(&new->lock);
  21553. + timer_set_base(timer, new);
  21554. + return new;
  21555. +}
  21556. +#else
  21557. +static inline struct tvec_base *switch_timer_base(struct timer_list *timer,
  21558. + struct tvec_base *old,
  21559. + struct tvec_base *new)
  21560. +{
  21561. + /*
  21562. + * We cannot do the above because we might be preempted and
  21563. + * then the preempter would see NULL and loop forever.
  21564. + */
  21565. + if (spin_trylock(&new->lock)) {
  21566. + timer_set_base(timer, new);
  21567. + spin_unlock(&old->lock);
  21568. + return new;
  21569. + }
  21570. + return old;
  21571. +}
  21572. +#endif
  21573. +
  21574. static inline int
  21575. __mod_timer(struct timer_list *timer, unsigned long expires,
  21576. bool pending_only, int pinned)
  21577. @@ -798,14 +831,8 @@
  21578. * handler yet has not finished. This also guarantees that
  21579. * the timer is serialized wrt itself.
  21580. */
  21581. - if (likely(base->running_timer != timer)) {
  21582. - /* See the comment in lock_timer_base() */
  21583. - timer_set_base(timer, NULL);
  21584. - spin_unlock(&base->lock);
  21585. - base = new_base;
  21586. - spin_lock(&base->lock);
  21587. - timer_set_base(timer, base);
  21588. - }
  21589. + if (likely(base->running_timer != timer))
  21590. + base = switch_timer_base(timer, base, new_base);
  21591. }
  21592. timer->expires = expires;
  21593. @@ -979,6 +1006,29 @@
  21594. }
  21595. EXPORT_SYMBOL_GPL(add_timer_on);
  21596. +#ifdef CONFIG_PREEMPT_RT_FULL
  21597. +/*
  21598. + * Wait for a running timer
  21599. + */
  21600. +static void wait_for_running_timer(struct timer_list *timer)
  21601. +{
  21602. + struct tvec_base *base = timer->base;
  21603. +
  21604. + if (base->running_timer == timer)
  21605. + wait_event(base->wait_for_running_timer,
  21606. + base->running_timer != timer);
  21607. +}
  21608. +
  21609. +# define wakeup_timer_waiters(b) wake_up(&(b)->wait_for_running_timer)
  21610. +#else
  21611. +static inline void wait_for_running_timer(struct timer_list *timer)
  21612. +{
  21613. + cpu_relax();
  21614. +}
  21615. +
  21616. +# define wakeup_timer_waiters(b) do { } while (0)
  21617. +#endif
  21618. +
  21619. /**
  21620. * del_timer - deactive a timer.
  21621. * @timer: the timer to be deactivated
  21622. @@ -1036,7 +1086,7 @@
  21623. }
  21624. EXPORT_SYMBOL(try_to_del_timer_sync);
  21625. -#ifdef CONFIG_SMP
  21626. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  21627. static DEFINE_PER_CPU(struct tvec_base, __tvec_bases);
  21628. /**
  21629. @@ -1098,7 +1148,7 @@
  21630. int ret = try_to_del_timer_sync(timer);
  21631. if (ret >= 0)
  21632. return ret;
  21633. - cpu_relax();
  21634. + wait_for_running_timer(timer);
  21635. }
  21636. }
  21637. EXPORT_SYMBOL(del_timer_sync);
  21638. @@ -1219,15 +1269,17 @@
  21639. if (irqsafe) {
  21640. spin_unlock(&base->lock);
  21641. call_timer_fn(timer, fn, data);
  21642. + base->running_timer = NULL;
  21643. spin_lock(&base->lock);
  21644. } else {
  21645. spin_unlock_irq(&base->lock);
  21646. call_timer_fn(timer, fn, data);
  21647. + base->running_timer = NULL;
  21648. spin_lock_irq(&base->lock);
  21649. }
  21650. }
  21651. }
  21652. - base->running_timer = NULL;
  21653. + wakeup_timer_waiters(base);
  21654. spin_unlock_irq(&base->lock);
  21655. }
  21656. @@ -1367,6 +1419,14 @@
  21657. if (cpu_is_offline(smp_processor_id()))
  21658. return expires;
  21659. +#ifdef CONFIG_PREEMPT_RT_FULL
  21660. + /*
  21661. + * On PREEMPT_RT we cannot sleep here. As a result we can't take
  21662. + * the base lock to check when the next timer is pending and so
  21663. + * we assume the next jiffy.
  21664. + */
  21665. + return now + 1;
  21666. +#endif
  21667. spin_lock(&base->lock);
  21668. if (base->active_timers) {
  21669. if (time_before_eq(base->next_timer, base->timer_jiffies))
  21670. @@ -1392,13 +1452,13 @@
  21671. /* Note: this timer irq context must be accounted for as well. */
  21672. account_process_tick(p, user_tick);
  21673. + scheduler_tick();
  21674. run_local_timers();
  21675. rcu_check_callbacks(user_tick);
  21676. -#ifdef CONFIG_IRQ_WORK
  21677. +#if defined(CONFIG_IRQ_WORK)
  21678. if (in_irq())
  21679. irq_work_tick();
  21680. #endif
  21681. - scheduler_tick();
  21682. run_posix_cpu_timers(p);
  21683. }
  21684. @@ -1411,6 +1471,8 @@
  21685. hrtimer_run_pending();
  21686. + irq_work_tick_soft();
  21687. +
  21688. if (time_after_eq(jiffies, base->timer_jiffies))
  21689. __run_timers(base);
  21690. }
  21691. @@ -1566,7 +1628,7 @@
  21692. BUG_ON(cpu_online(cpu));
  21693. old_base = per_cpu(tvec_bases, cpu);
  21694. - new_base = get_cpu_var(tvec_bases);
  21695. + new_base = get_local_var(tvec_bases);
  21696. /*
  21697. * The caller is globally serialized and nobody else
  21698. * takes two locks at once, deadlock is not possible.
  21699. @@ -1590,7 +1652,7 @@
  21700. spin_unlock(&old_base->lock);
  21701. spin_unlock_irq(&new_base->lock);
  21702. - put_cpu_var(tvec_bases);
  21703. + put_local_var(tvec_bases);
  21704. }
  21705. static int timer_cpu_notify(struct notifier_block *self,
  21706. @@ -1625,6 +1687,9 @@
  21707. base->cpu = cpu;
  21708. per_cpu(tvec_bases, cpu) = base;
  21709. spin_lock_init(&base->lock);
  21710. +#ifdef CONFIG_PREEMPT_RT_FULL
  21711. + init_waitqueue_head(&base->wait_for_running_timer);
  21712. +#endif
  21713. for (j = 0; j < TVN_SIZE; j++) {
  21714. INIT_LIST_HEAD(base->tv5.vec + j);
  21715. diff -Nur linux-4.1.20.orig/kernel/trace/Kconfig linux-4.1.20/kernel/trace/Kconfig
  21716. --- linux-4.1.20.orig/kernel/trace/Kconfig 2016-03-17 19:11:03.000000000 +0100
  21717. +++ linux-4.1.20/kernel/trace/Kconfig 2016-03-21 20:18:34.000000000 +0100
  21718. @@ -187,6 +187,24 @@
  21719. enabled. This option and the preempt-off timing option can be
  21720. used together or separately.)
  21721. +config INTERRUPT_OFF_HIST
  21722. + bool "Interrupts-off Latency Histogram"
  21723. + depends on IRQSOFF_TRACER
  21724. + help
  21725. + This option generates continuously updated histograms (one per cpu)
  21726. + of the duration of time periods with interrupts disabled. The
  21727. + histograms are disabled by default. To enable them, write a non-zero
  21728. + number to
  21729. +
  21730. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  21731. +
  21732. + If PREEMPT_OFF_HIST is also selected, additional histograms (one
  21733. + per cpu) are generated that accumulate the duration of time periods
  21734. + when both interrupts and preemption are disabled. The histogram data
  21735. + will be located in the debug file system at
  21736. +
  21737. + /sys/kernel/debug/tracing/latency_hist/irqsoff
  21738. +
  21739. config PREEMPT_TRACER
  21740. bool "Preemption-off Latency Tracer"
  21741. default n
  21742. @@ -211,6 +229,24 @@
  21743. enabled. This option and the irqs-off timing option can be
  21744. used together or separately.)
  21745. +config PREEMPT_OFF_HIST
  21746. + bool "Preemption-off Latency Histogram"
  21747. + depends on PREEMPT_TRACER
  21748. + help
  21749. + This option generates continuously updated histograms (one per cpu)
  21750. + of the duration of time periods with preemption disabled. The
  21751. + histograms are disabled by default. To enable them, write a non-zero
  21752. + number to
  21753. +
  21754. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  21755. +
  21756. + If INTERRUPT_OFF_HIST is also selected, additional histograms (one
  21757. + per cpu) are generated that accumulate the duration of time periods
  21758. + when both interrupts and preemption are disabled. The histogram data
  21759. + will be located in the debug file system at
  21760. +
  21761. + /sys/kernel/debug/tracing/latency_hist/preemptoff
  21762. +
  21763. config SCHED_TRACER
  21764. bool "Scheduling Latency Tracer"
  21765. select GENERIC_TRACER
  21766. @@ -221,6 +257,74 @@
  21767. This tracer tracks the latency of the highest priority task
  21768. to be scheduled in, starting from the point it has woken up.
  21769. +config WAKEUP_LATENCY_HIST
  21770. + bool "Scheduling Latency Histogram"
  21771. + depends on SCHED_TRACER
  21772. + help
  21773. + This option generates continuously updated histograms (one per cpu)
  21774. + of the scheduling latency of the highest priority task.
  21775. + The histograms are disabled by default. To enable them, write a
  21776. + non-zero number to
  21777. +
  21778. + /sys/kernel/debug/tracing/latency_hist/enable/wakeup
  21779. +
  21780. + Two different algorithms are used, one to determine the latency of
  21781. + processes that exclusively use the highest priority of the system and
  21782. + another one to determine the latency of processes that share the
  21783. + highest system priority with other processes. The former is used to
  21784. + improve hardware and system software, the latter to optimize the
  21785. + priority design of a given system. The histogram data will be
  21786. + located in the debug file system at
  21787. +
  21788. + /sys/kernel/debug/tracing/latency_hist/wakeup
  21789. +
  21790. + and
  21791. +
  21792. + /sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio
  21793. +
  21794. + If both Scheduling Latency Histogram and Missed Timer Offsets
  21795. + Histogram are selected, additional histogram data will be collected
  21796. + that contain, in addition to the wakeup latency, the timer latency, in
  21797. + case the wakeup was triggered by an expired timer. These histograms
  21798. + are available in the
  21799. +
  21800. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  21801. +
  21802. + directory. They reflect the apparent interrupt and scheduling latency
  21803. + and are best suitable to determine the worst-case latency of a given
  21804. + system. To enable these histograms, write a non-zero number to
  21805. +
  21806. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  21807. +
  21808. +config MISSED_TIMER_OFFSETS_HIST
  21809. + depends on HIGH_RES_TIMERS
  21810. + select GENERIC_TRACER
  21811. + bool "Missed Timer Offsets Histogram"
  21812. + help
  21813. + Generate a histogram of missed timer offsets in microseconds. The
  21814. + histograms are disabled by default. To enable them, write a non-zero
  21815. + number to
  21816. +
  21817. + /sys/kernel/debug/tracing/latency_hist/enable/missed_timer_offsets
  21818. +
  21819. + The histogram data will be located in the debug file system at
  21820. +
  21821. + /sys/kernel/debug/tracing/latency_hist/missed_timer_offsets
  21822. +
  21823. + If both Scheduling Latency Histogram and Missed Timer Offsets
  21824. + Histogram are selected, additional histogram data will be collected
  21825. + that contain, in addition to the wakeup latency, the timer latency, in
  21826. + case the wakeup was triggered by an expired timer. These histograms
  21827. + are available in the
  21828. +
  21829. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  21830. +
  21831. + directory. They reflect the apparent interrupt and scheduling latency
  21832. + and are best suitable to determine the worst-case latency of a given
  21833. + system. To enable these histograms, write a non-zero number to
  21834. +
  21835. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  21836. +
  21837. config ENABLE_DEFAULT_TRACERS
  21838. bool "Trace process context switches and events"
  21839. depends on !GENERIC_TRACER
  21840. diff -Nur linux-4.1.20.orig/kernel/trace/Makefile linux-4.1.20/kernel/trace/Makefile
  21841. --- linux-4.1.20.orig/kernel/trace/Makefile 2016-03-17 19:11:03.000000000 +0100
  21842. +++ linux-4.1.20/kernel/trace/Makefile 2016-03-21 20:18:34.000000000 +0100
  21843. @@ -36,6 +36,10 @@
  21844. obj-$(CONFIG_IRQSOFF_TRACER) += trace_irqsoff.o
  21845. obj-$(CONFIG_PREEMPT_TRACER) += trace_irqsoff.o
  21846. obj-$(CONFIG_SCHED_TRACER) += trace_sched_wakeup.o
  21847. +obj-$(CONFIG_INTERRUPT_OFF_HIST) += latency_hist.o
  21848. +obj-$(CONFIG_PREEMPT_OFF_HIST) += latency_hist.o
  21849. +obj-$(CONFIG_WAKEUP_LATENCY_HIST) += latency_hist.o
  21850. +obj-$(CONFIG_MISSED_TIMER_OFFSETS_HIST) += latency_hist.o
  21851. obj-$(CONFIG_NOP_TRACER) += trace_nop.o
  21852. obj-$(CONFIG_STACK_TRACER) += trace_stack.o
  21853. obj-$(CONFIG_MMIOTRACE) += trace_mmiotrace.o
  21854. diff -Nur linux-4.1.20.orig/kernel/trace/latency_hist.c linux-4.1.20/kernel/trace/latency_hist.c
  21855. --- linux-4.1.20.orig/kernel/trace/latency_hist.c 1970-01-01 01:00:00.000000000 +0100
  21856. +++ linux-4.1.20/kernel/trace/latency_hist.c 2016-03-21 20:18:34.000000000 +0100
  21857. @@ -0,0 +1,1178 @@
  21858. +/*
  21859. + * kernel/trace/latency_hist.c
  21860. + *
  21861. + * Add support for histograms of preemption-off latency and
  21862. + * interrupt-off latency and wakeup latency, it depends on
  21863. + * Real-Time Preemption Support.
  21864. + *
  21865. + * Copyright (C) 2005 MontaVista Software, Inc.
  21866. + * Yi Yang <yyang@ch.mvista.com>
  21867. + *
  21868. + * Converted to work with the new latency tracer.
  21869. + * Copyright (C) 2008 Red Hat, Inc.
  21870. + * Steven Rostedt <srostedt@redhat.com>
  21871. + *
  21872. + */
  21873. +#include <linux/module.h>
  21874. +#include <linux/debugfs.h>
  21875. +#include <linux/seq_file.h>
  21876. +#include <linux/percpu.h>
  21877. +#include <linux/kallsyms.h>
  21878. +#include <linux/uaccess.h>
  21879. +#include <linux/sched.h>
  21880. +#include <linux/sched/rt.h>
  21881. +#include <linux/slab.h>
  21882. +#include <linux/atomic.h>
  21883. +#include <asm/div64.h>
  21884. +
  21885. +#include "trace.h"
  21886. +#include <trace/events/sched.h>
  21887. +
  21888. +#define NSECS_PER_USECS 1000L
  21889. +
  21890. +#define CREATE_TRACE_POINTS
  21891. +#include <trace/events/hist.h>
  21892. +
  21893. +enum {
  21894. + IRQSOFF_LATENCY = 0,
  21895. + PREEMPTOFF_LATENCY,
  21896. + PREEMPTIRQSOFF_LATENCY,
  21897. + WAKEUP_LATENCY,
  21898. + WAKEUP_LATENCY_SHAREDPRIO,
  21899. + MISSED_TIMER_OFFSETS,
  21900. + TIMERANDWAKEUP_LATENCY,
  21901. + MAX_LATENCY_TYPE,
  21902. +};
  21903. +
  21904. +#define MAX_ENTRY_NUM 10240
  21905. +
  21906. +struct hist_data {
  21907. + atomic_t hist_mode; /* 0 log, 1 don't log */
  21908. + long offset; /* set it to MAX_ENTRY_NUM/2 for a bipolar scale */
  21909. + long min_lat;
  21910. + long max_lat;
  21911. + unsigned long long below_hist_bound_samples;
  21912. + unsigned long long above_hist_bound_samples;
  21913. + long long accumulate_lat;
  21914. + unsigned long long total_samples;
  21915. + unsigned long long hist_array[MAX_ENTRY_NUM];
  21916. +};
  21917. +
  21918. +struct enable_data {
  21919. + int latency_type;
  21920. + int enabled;
  21921. +};
  21922. +
  21923. +static char *latency_hist_dir_root = "latency_hist";
  21924. +
  21925. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  21926. +static DEFINE_PER_CPU(struct hist_data, irqsoff_hist);
  21927. +static char *irqsoff_hist_dir = "irqsoff";
  21928. +static DEFINE_PER_CPU(cycles_t, hist_irqsoff_start);
  21929. +static DEFINE_PER_CPU(int, hist_irqsoff_counting);
  21930. +#endif
  21931. +
  21932. +#ifdef CONFIG_PREEMPT_OFF_HIST
  21933. +static DEFINE_PER_CPU(struct hist_data, preemptoff_hist);
  21934. +static char *preemptoff_hist_dir = "preemptoff";
  21935. +static DEFINE_PER_CPU(cycles_t, hist_preemptoff_start);
  21936. +static DEFINE_PER_CPU(int, hist_preemptoff_counting);
  21937. +#endif
  21938. +
  21939. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  21940. +static DEFINE_PER_CPU(struct hist_data, preemptirqsoff_hist);
  21941. +static char *preemptirqsoff_hist_dir = "preemptirqsoff";
  21942. +static DEFINE_PER_CPU(cycles_t, hist_preemptirqsoff_start);
  21943. +static DEFINE_PER_CPU(int, hist_preemptirqsoff_counting);
  21944. +#endif
  21945. +
  21946. +#if defined(CONFIG_PREEMPT_OFF_HIST) || defined(CONFIG_INTERRUPT_OFF_HIST)
  21947. +static notrace void probe_preemptirqsoff_hist(void *v, int reason, int start);
  21948. +static struct enable_data preemptirqsoff_enabled_data = {
  21949. + .latency_type = PREEMPTIRQSOFF_LATENCY,
  21950. + .enabled = 0,
  21951. +};
  21952. +#endif
  21953. +
  21954. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  21955. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  21956. +struct maxlatproc_data {
  21957. + char comm[FIELD_SIZEOF(struct task_struct, comm)];
  21958. + char current_comm[FIELD_SIZEOF(struct task_struct, comm)];
  21959. + int pid;
  21960. + int current_pid;
  21961. + int prio;
  21962. + int current_prio;
  21963. + long latency;
  21964. + long timeroffset;
  21965. + cycle_t timestamp;
  21966. +};
  21967. +#endif
  21968. +
  21969. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  21970. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist);
  21971. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist_sharedprio);
  21972. +static char *wakeup_latency_hist_dir = "wakeup";
  21973. +static char *wakeup_latency_hist_dir_sharedprio = "sharedprio";
  21974. +static notrace void probe_wakeup_latency_hist_start(void *v,
  21975. + struct task_struct *p);
  21976. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  21977. + struct task_struct *prev, struct task_struct *next);
  21978. +static notrace void probe_sched_migrate_task(void *,
  21979. + struct task_struct *task, int cpu);
  21980. +static struct enable_data wakeup_latency_enabled_data = {
  21981. + .latency_type = WAKEUP_LATENCY,
  21982. + .enabled = 0,
  21983. +};
  21984. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc);
  21985. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc_sharedprio);
  21986. +static DEFINE_PER_CPU(struct task_struct *, wakeup_task);
  21987. +static DEFINE_PER_CPU(int, wakeup_sharedprio);
  21988. +static unsigned long wakeup_pid;
  21989. +#endif
  21990. +
  21991. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  21992. +static DEFINE_PER_CPU(struct hist_data, missed_timer_offsets);
  21993. +static char *missed_timer_offsets_dir = "missed_timer_offsets";
  21994. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  21995. + long long offset, struct task_struct *curr, struct task_struct *task);
  21996. +static struct enable_data missed_timer_offsets_enabled_data = {
  21997. + .latency_type = MISSED_TIMER_OFFSETS,
  21998. + .enabled = 0,
  21999. +};
  22000. +static DEFINE_PER_CPU(struct maxlatproc_data, missed_timer_offsets_maxlatproc);
  22001. +static unsigned long missed_timer_offsets_pid;
  22002. +#endif
  22003. +
  22004. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  22005. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22006. +static DEFINE_PER_CPU(struct hist_data, timerandwakeup_latency_hist);
  22007. +static char *timerandwakeup_latency_hist_dir = "timerandwakeup";
  22008. +static struct enable_data timerandwakeup_enabled_data = {
  22009. + .latency_type = TIMERANDWAKEUP_LATENCY,
  22010. + .enabled = 0,
  22011. +};
  22012. +static DEFINE_PER_CPU(struct maxlatproc_data, timerandwakeup_maxlatproc);
  22013. +#endif
  22014. +
  22015. +void notrace latency_hist(int latency_type, int cpu, long latency,
  22016. + long timeroffset, cycle_t stop,
  22017. + struct task_struct *p)
  22018. +{
  22019. + struct hist_data *my_hist;
  22020. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22021. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22022. + struct maxlatproc_data *mp = NULL;
  22023. +#endif
  22024. +
  22025. + if (!cpu_possible(cpu) || latency_type < 0 ||
  22026. + latency_type >= MAX_LATENCY_TYPE)
  22027. + return;
  22028. +
  22029. + switch (latency_type) {
  22030. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  22031. + case IRQSOFF_LATENCY:
  22032. + my_hist = &per_cpu(irqsoff_hist, cpu);
  22033. + break;
  22034. +#endif
  22035. +#ifdef CONFIG_PREEMPT_OFF_HIST
  22036. + case PREEMPTOFF_LATENCY:
  22037. + my_hist = &per_cpu(preemptoff_hist, cpu);
  22038. + break;
  22039. +#endif
  22040. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  22041. + case PREEMPTIRQSOFF_LATENCY:
  22042. + my_hist = &per_cpu(preemptirqsoff_hist, cpu);
  22043. + break;
  22044. +#endif
  22045. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22046. + case WAKEUP_LATENCY:
  22047. + my_hist = &per_cpu(wakeup_latency_hist, cpu);
  22048. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  22049. + break;
  22050. + case WAKEUP_LATENCY_SHAREDPRIO:
  22051. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  22052. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  22053. + break;
  22054. +#endif
  22055. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22056. + case MISSED_TIMER_OFFSETS:
  22057. + my_hist = &per_cpu(missed_timer_offsets, cpu);
  22058. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  22059. + break;
  22060. +#endif
  22061. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  22062. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22063. + case TIMERANDWAKEUP_LATENCY:
  22064. + my_hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  22065. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  22066. + break;
  22067. +#endif
  22068. +
  22069. + default:
  22070. + return;
  22071. + }
  22072. +
  22073. + latency += my_hist->offset;
  22074. +
  22075. + if (atomic_read(&my_hist->hist_mode) == 0)
  22076. + return;
  22077. +
  22078. + if (latency < 0 || latency >= MAX_ENTRY_NUM) {
  22079. + if (latency < 0)
  22080. + my_hist->below_hist_bound_samples++;
  22081. + else
  22082. + my_hist->above_hist_bound_samples++;
  22083. + } else
  22084. + my_hist->hist_array[latency]++;
  22085. +
  22086. + if (unlikely(latency > my_hist->max_lat ||
  22087. + my_hist->min_lat == LONG_MAX)) {
  22088. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22089. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22090. + if (latency_type == WAKEUP_LATENCY ||
  22091. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  22092. + latency_type == MISSED_TIMER_OFFSETS ||
  22093. + latency_type == TIMERANDWAKEUP_LATENCY) {
  22094. + strncpy(mp->comm, p->comm, sizeof(mp->comm));
  22095. + strncpy(mp->current_comm, current->comm,
  22096. + sizeof(mp->current_comm));
  22097. + mp->pid = task_pid_nr(p);
  22098. + mp->current_pid = task_pid_nr(current);
  22099. + mp->prio = p->prio;
  22100. + mp->current_prio = current->prio;
  22101. + mp->latency = latency;
  22102. + mp->timeroffset = timeroffset;
  22103. + mp->timestamp = stop;
  22104. + }
  22105. +#endif
  22106. + my_hist->max_lat = latency;
  22107. + }
  22108. + if (unlikely(latency < my_hist->min_lat))
  22109. + my_hist->min_lat = latency;
  22110. + my_hist->total_samples++;
  22111. + my_hist->accumulate_lat += latency;
  22112. +}
  22113. +
  22114. +static void *l_start(struct seq_file *m, loff_t *pos)
  22115. +{
  22116. + loff_t *index_ptr = NULL;
  22117. + loff_t index = *pos;
  22118. + struct hist_data *my_hist = m->private;
  22119. +
  22120. + if (index == 0) {
  22121. + char minstr[32], avgstr[32], maxstr[32];
  22122. +
  22123. + atomic_dec(&my_hist->hist_mode);
  22124. +
  22125. + if (likely(my_hist->total_samples)) {
  22126. + long avg = (long) div64_s64(my_hist->accumulate_lat,
  22127. + my_hist->total_samples);
  22128. + snprintf(minstr, sizeof(minstr), "%ld",
  22129. + my_hist->min_lat - my_hist->offset);
  22130. + snprintf(avgstr, sizeof(avgstr), "%ld",
  22131. + avg - my_hist->offset);
  22132. + snprintf(maxstr, sizeof(maxstr), "%ld",
  22133. + my_hist->max_lat - my_hist->offset);
  22134. + } else {
  22135. + strcpy(minstr, "<undef>");
  22136. + strcpy(avgstr, minstr);
  22137. + strcpy(maxstr, minstr);
  22138. + }
  22139. +
  22140. + seq_printf(m, "#Minimum latency: %s microseconds\n"
  22141. + "#Average latency: %s microseconds\n"
  22142. + "#Maximum latency: %s microseconds\n"
  22143. + "#Total samples: %llu\n"
  22144. + "#There are %llu samples lower than %ld"
  22145. + " microseconds.\n"
  22146. + "#There are %llu samples greater or equal"
  22147. + " than %ld microseconds.\n"
  22148. + "#usecs\t%16s\n",
  22149. + minstr, avgstr, maxstr,
  22150. + my_hist->total_samples,
  22151. + my_hist->below_hist_bound_samples,
  22152. + -my_hist->offset,
  22153. + my_hist->above_hist_bound_samples,
  22154. + MAX_ENTRY_NUM - my_hist->offset,
  22155. + "samples");
  22156. + }
  22157. + if (index < MAX_ENTRY_NUM) {
  22158. + index_ptr = kmalloc(sizeof(loff_t), GFP_KERNEL);
  22159. + if (index_ptr)
  22160. + *index_ptr = index;
  22161. + }
  22162. +
  22163. + return index_ptr;
  22164. +}
  22165. +
  22166. +static void *l_next(struct seq_file *m, void *p, loff_t *pos)
  22167. +{
  22168. + loff_t *index_ptr = p;
  22169. + struct hist_data *my_hist = m->private;
  22170. +
  22171. + if (++*pos >= MAX_ENTRY_NUM) {
  22172. + atomic_inc(&my_hist->hist_mode);
  22173. + return NULL;
  22174. + }
  22175. + *index_ptr = *pos;
  22176. + return index_ptr;
  22177. +}
  22178. +
  22179. +static void l_stop(struct seq_file *m, void *p)
  22180. +{
  22181. + kfree(p);
  22182. +}
  22183. +
  22184. +static int l_show(struct seq_file *m, void *p)
  22185. +{
  22186. + int index = *(loff_t *) p;
  22187. + struct hist_data *my_hist = m->private;
  22188. +
  22189. + seq_printf(m, "%6ld\t%16llu\n", index - my_hist->offset,
  22190. + my_hist->hist_array[index]);
  22191. + return 0;
  22192. +}
  22193. +
  22194. +static const struct seq_operations latency_hist_seq_op = {
  22195. + .start = l_start,
  22196. + .next = l_next,
  22197. + .stop = l_stop,
  22198. + .show = l_show
  22199. +};
  22200. +
  22201. +static int latency_hist_open(struct inode *inode, struct file *file)
  22202. +{
  22203. + int ret;
  22204. +
  22205. + ret = seq_open(file, &latency_hist_seq_op);
  22206. + if (!ret) {
  22207. + struct seq_file *seq = file->private_data;
  22208. + seq->private = inode->i_private;
  22209. + }
  22210. + return ret;
  22211. +}
  22212. +
  22213. +static const struct file_operations latency_hist_fops = {
  22214. + .open = latency_hist_open,
  22215. + .read = seq_read,
  22216. + .llseek = seq_lseek,
  22217. + .release = seq_release,
  22218. +};
  22219. +
  22220. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22221. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22222. +static void clear_maxlatprocdata(struct maxlatproc_data *mp)
  22223. +{
  22224. + mp->comm[0] = mp->current_comm[0] = '\0';
  22225. + mp->prio = mp->current_prio = mp->pid = mp->current_pid =
  22226. + mp->latency = mp->timeroffset = -1;
  22227. + mp->timestamp = 0;
  22228. +}
  22229. +#endif
  22230. +
  22231. +static void hist_reset(struct hist_data *hist)
  22232. +{
  22233. + atomic_dec(&hist->hist_mode);
  22234. +
  22235. + memset(hist->hist_array, 0, sizeof(hist->hist_array));
  22236. + hist->below_hist_bound_samples = 0ULL;
  22237. + hist->above_hist_bound_samples = 0ULL;
  22238. + hist->min_lat = LONG_MAX;
  22239. + hist->max_lat = LONG_MIN;
  22240. + hist->total_samples = 0ULL;
  22241. + hist->accumulate_lat = 0LL;
  22242. +
  22243. + atomic_inc(&hist->hist_mode);
  22244. +}
  22245. +
  22246. +static ssize_t
  22247. +latency_hist_reset(struct file *file, const char __user *a,
  22248. + size_t size, loff_t *off)
  22249. +{
  22250. + int cpu;
  22251. + struct hist_data *hist = NULL;
  22252. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22253. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22254. + struct maxlatproc_data *mp = NULL;
  22255. +#endif
  22256. + off_t latency_type = (off_t) file->private_data;
  22257. +
  22258. + for_each_online_cpu(cpu) {
  22259. +
  22260. + switch (latency_type) {
  22261. +#ifdef CONFIG_PREEMPT_OFF_HIST
  22262. + case PREEMPTOFF_LATENCY:
  22263. + hist = &per_cpu(preemptoff_hist, cpu);
  22264. + break;
  22265. +#endif
  22266. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  22267. + case IRQSOFF_LATENCY:
  22268. + hist = &per_cpu(irqsoff_hist, cpu);
  22269. + break;
  22270. +#endif
  22271. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  22272. + case PREEMPTIRQSOFF_LATENCY:
  22273. + hist = &per_cpu(preemptirqsoff_hist, cpu);
  22274. + break;
  22275. +#endif
  22276. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22277. + case WAKEUP_LATENCY:
  22278. + hist = &per_cpu(wakeup_latency_hist, cpu);
  22279. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  22280. + break;
  22281. + case WAKEUP_LATENCY_SHAREDPRIO:
  22282. + hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  22283. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  22284. + break;
  22285. +#endif
  22286. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22287. + case MISSED_TIMER_OFFSETS:
  22288. + hist = &per_cpu(missed_timer_offsets, cpu);
  22289. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  22290. + break;
  22291. +#endif
  22292. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  22293. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22294. + case TIMERANDWAKEUP_LATENCY:
  22295. + hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  22296. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  22297. + break;
  22298. +#endif
  22299. + }
  22300. +
  22301. + hist_reset(hist);
  22302. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22303. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22304. + if (latency_type == WAKEUP_LATENCY ||
  22305. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  22306. + latency_type == MISSED_TIMER_OFFSETS ||
  22307. + latency_type == TIMERANDWAKEUP_LATENCY)
  22308. + clear_maxlatprocdata(mp);
  22309. +#endif
  22310. + }
  22311. +
  22312. + return size;
  22313. +}
  22314. +
  22315. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22316. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22317. +static ssize_t
  22318. +show_pid(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  22319. +{
  22320. + char buf[64];
  22321. + int r;
  22322. + unsigned long *this_pid = file->private_data;
  22323. +
  22324. + r = snprintf(buf, sizeof(buf), "%lu\n", *this_pid);
  22325. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  22326. +}
  22327. +
  22328. +static ssize_t do_pid(struct file *file, const char __user *ubuf,
  22329. + size_t cnt, loff_t *ppos)
  22330. +{
  22331. + char buf[64];
  22332. + unsigned long pid;
  22333. + unsigned long *this_pid = file->private_data;
  22334. +
  22335. + if (cnt >= sizeof(buf))
  22336. + return -EINVAL;
  22337. +
  22338. + if (copy_from_user(&buf, ubuf, cnt))
  22339. + return -EFAULT;
  22340. +
  22341. + buf[cnt] = '\0';
  22342. +
  22343. + if (kstrtoul(buf, 10, &pid))
  22344. + return -EINVAL;
  22345. +
  22346. + *this_pid = pid;
  22347. +
  22348. + return cnt;
  22349. +}
  22350. +#endif
  22351. +
  22352. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22353. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22354. +static ssize_t
  22355. +show_maxlatproc(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  22356. +{
  22357. + int r;
  22358. + struct maxlatproc_data *mp = file->private_data;
  22359. + int strmaxlen = (TASK_COMM_LEN * 2) + (8 * 8);
  22360. + unsigned long long t;
  22361. + unsigned long usecs, secs;
  22362. + char *buf;
  22363. +
  22364. + if (mp->pid == -1 || mp->current_pid == -1) {
  22365. + buf = "(none)\n";
  22366. + return simple_read_from_buffer(ubuf, cnt, ppos, buf,
  22367. + strlen(buf));
  22368. + }
  22369. +
  22370. + buf = kmalloc(strmaxlen, GFP_KERNEL);
  22371. + if (buf == NULL)
  22372. + return -ENOMEM;
  22373. +
  22374. + t = ns2usecs(mp->timestamp);
  22375. + usecs = do_div(t, USEC_PER_SEC);
  22376. + secs = (unsigned long) t;
  22377. + r = snprintf(buf, strmaxlen,
  22378. + "%d %d %ld (%ld) %s <- %d %d %s %lu.%06lu\n", mp->pid,
  22379. + MAX_RT_PRIO-1 - mp->prio, mp->latency, mp->timeroffset, mp->comm,
  22380. + mp->current_pid, MAX_RT_PRIO-1 - mp->current_prio, mp->current_comm,
  22381. + secs, usecs);
  22382. + r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  22383. + kfree(buf);
  22384. + return r;
  22385. +}
  22386. +#endif
  22387. +
  22388. +static ssize_t
  22389. +show_enable(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  22390. +{
  22391. + char buf[64];
  22392. + struct enable_data *ed = file->private_data;
  22393. + int r;
  22394. +
  22395. + r = snprintf(buf, sizeof(buf), "%d\n", ed->enabled);
  22396. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  22397. +}
  22398. +
  22399. +static ssize_t
  22400. +do_enable(struct file *file, const char __user *ubuf, size_t cnt, loff_t *ppos)
  22401. +{
  22402. + char buf[64];
  22403. + long enable;
  22404. + struct enable_data *ed = file->private_data;
  22405. +
  22406. + if (cnt >= sizeof(buf))
  22407. + return -EINVAL;
  22408. +
  22409. + if (copy_from_user(&buf, ubuf, cnt))
  22410. + return -EFAULT;
  22411. +
  22412. + buf[cnt] = 0;
  22413. +
  22414. + if (kstrtoul(buf, 10, &enable))
  22415. + return -EINVAL;
  22416. +
  22417. + if ((enable && ed->enabled) || (!enable && !ed->enabled))
  22418. + return cnt;
  22419. +
  22420. + if (enable) {
  22421. + int ret;
  22422. +
  22423. + switch (ed->latency_type) {
  22424. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  22425. + case PREEMPTIRQSOFF_LATENCY:
  22426. + ret = register_trace_preemptirqsoff_hist(
  22427. + probe_preemptirqsoff_hist, NULL);
  22428. + if (ret) {
  22429. + pr_info("wakeup trace: Couldn't assign "
  22430. + "probe_preemptirqsoff_hist "
  22431. + "to trace_preemptirqsoff_hist\n");
  22432. + return ret;
  22433. + }
  22434. + break;
  22435. +#endif
  22436. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22437. + case WAKEUP_LATENCY:
  22438. + ret = register_trace_sched_wakeup(
  22439. + probe_wakeup_latency_hist_start, NULL);
  22440. + if (ret) {
  22441. + pr_info("wakeup trace: Couldn't assign "
  22442. + "probe_wakeup_latency_hist_start "
  22443. + "to trace_sched_wakeup\n");
  22444. + return ret;
  22445. + }
  22446. + ret = register_trace_sched_wakeup_new(
  22447. + probe_wakeup_latency_hist_start, NULL);
  22448. + if (ret) {
  22449. + pr_info("wakeup trace: Couldn't assign "
  22450. + "probe_wakeup_latency_hist_start "
  22451. + "to trace_sched_wakeup_new\n");
  22452. + unregister_trace_sched_wakeup(
  22453. + probe_wakeup_latency_hist_start, NULL);
  22454. + return ret;
  22455. + }
  22456. + ret = register_trace_sched_switch(
  22457. + probe_wakeup_latency_hist_stop, NULL);
  22458. + if (ret) {
  22459. + pr_info("wakeup trace: Couldn't assign "
  22460. + "probe_wakeup_latency_hist_stop "
  22461. + "to trace_sched_switch\n");
  22462. + unregister_trace_sched_wakeup(
  22463. + probe_wakeup_latency_hist_start, NULL);
  22464. + unregister_trace_sched_wakeup_new(
  22465. + probe_wakeup_latency_hist_start, NULL);
  22466. + return ret;
  22467. + }
  22468. + ret = register_trace_sched_migrate_task(
  22469. + probe_sched_migrate_task, NULL);
  22470. + if (ret) {
  22471. + pr_info("wakeup trace: Couldn't assign "
  22472. + "probe_sched_migrate_task "
  22473. + "to trace_sched_migrate_task\n");
  22474. + unregister_trace_sched_wakeup(
  22475. + probe_wakeup_latency_hist_start, NULL);
  22476. + unregister_trace_sched_wakeup_new(
  22477. + probe_wakeup_latency_hist_start, NULL);
  22478. + unregister_trace_sched_switch(
  22479. + probe_wakeup_latency_hist_stop, NULL);
  22480. + return ret;
  22481. + }
  22482. + break;
  22483. +#endif
  22484. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22485. + case MISSED_TIMER_OFFSETS:
  22486. + ret = register_trace_hrtimer_interrupt(
  22487. + probe_hrtimer_interrupt, NULL);
  22488. + if (ret) {
  22489. + pr_info("wakeup trace: Couldn't assign "
  22490. + "probe_hrtimer_interrupt "
  22491. + "to trace_hrtimer_interrupt\n");
  22492. + return ret;
  22493. + }
  22494. + break;
  22495. +#endif
  22496. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  22497. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22498. + case TIMERANDWAKEUP_LATENCY:
  22499. + if (!wakeup_latency_enabled_data.enabled ||
  22500. + !missed_timer_offsets_enabled_data.enabled)
  22501. + return -EINVAL;
  22502. + break;
  22503. +#endif
  22504. + default:
  22505. + break;
  22506. + }
  22507. + } else {
  22508. + switch (ed->latency_type) {
  22509. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  22510. + case PREEMPTIRQSOFF_LATENCY:
  22511. + {
  22512. + int cpu;
  22513. +
  22514. + unregister_trace_preemptirqsoff_hist(
  22515. + probe_preemptirqsoff_hist, NULL);
  22516. + for_each_online_cpu(cpu) {
  22517. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  22518. + per_cpu(hist_irqsoff_counting,
  22519. + cpu) = 0;
  22520. +#endif
  22521. +#ifdef CONFIG_PREEMPT_OFF_HIST
  22522. + per_cpu(hist_preemptoff_counting,
  22523. + cpu) = 0;
  22524. +#endif
  22525. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  22526. + per_cpu(hist_preemptirqsoff_counting,
  22527. + cpu) = 0;
  22528. +#endif
  22529. + }
  22530. + }
  22531. + break;
  22532. +#endif
  22533. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22534. + case WAKEUP_LATENCY:
  22535. + {
  22536. + int cpu;
  22537. +
  22538. + unregister_trace_sched_wakeup(
  22539. + probe_wakeup_latency_hist_start, NULL);
  22540. + unregister_trace_sched_wakeup_new(
  22541. + probe_wakeup_latency_hist_start, NULL);
  22542. + unregister_trace_sched_switch(
  22543. + probe_wakeup_latency_hist_stop, NULL);
  22544. + unregister_trace_sched_migrate_task(
  22545. + probe_sched_migrate_task, NULL);
  22546. +
  22547. + for_each_online_cpu(cpu) {
  22548. + per_cpu(wakeup_task, cpu) = NULL;
  22549. + per_cpu(wakeup_sharedprio, cpu) = 0;
  22550. + }
  22551. + }
  22552. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22553. + timerandwakeup_enabled_data.enabled = 0;
  22554. +#endif
  22555. + break;
  22556. +#endif
  22557. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22558. + case MISSED_TIMER_OFFSETS:
  22559. + unregister_trace_hrtimer_interrupt(
  22560. + probe_hrtimer_interrupt, NULL);
  22561. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22562. + timerandwakeup_enabled_data.enabled = 0;
  22563. +#endif
  22564. + break;
  22565. +#endif
  22566. + default:
  22567. + break;
  22568. + }
  22569. + }
  22570. + ed->enabled = enable;
  22571. + return cnt;
  22572. +}
  22573. +
  22574. +static const struct file_operations latency_hist_reset_fops = {
  22575. + .open = tracing_open_generic,
  22576. + .write = latency_hist_reset,
  22577. +};
  22578. +
  22579. +static const struct file_operations enable_fops = {
  22580. + .open = tracing_open_generic,
  22581. + .read = show_enable,
  22582. + .write = do_enable,
  22583. +};
  22584. +
  22585. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22586. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22587. +static const struct file_operations pid_fops = {
  22588. + .open = tracing_open_generic,
  22589. + .read = show_pid,
  22590. + .write = do_pid,
  22591. +};
  22592. +
  22593. +static const struct file_operations maxlatproc_fops = {
  22594. + .open = tracing_open_generic,
  22595. + .read = show_maxlatproc,
  22596. +};
  22597. +#endif
  22598. +
  22599. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  22600. +static notrace void probe_preemptirqsoff_hist(void *v, int reason,
  22601. + int starthist)
  22602. +{
  22603. + int cpu = raw_smp_processor_id();
  22604. + int time_set = 0;
  22605. +
  22606. + if (starthist) {
  22607. + cycle_t uninitialized_var(start);
  22608. +
  22609. + if (!preempt_count() && !irqs_disabled())
  22610. + return;
  22611. +
  22612. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  22613. + if ((reason == IRQS_OFF || reason == TRACE_START) &&
  22614. + !per_cpu(hist_irqsoff_counting, cpu)) {
  22615. + per_cpu(hist_irqsoff_counting, cpu) = 1;
  22616. + start = ftrace_now(cpu);
  22617. + time_set++;
  22618. + per_cpu(hist_irqsoff_start, cpu) = start;
  22619. + }
  22620. +#endif
  22621. +
  22622. +#ifdef CONFIG_PREEMPT_OFF_HIST
  22623. + if ((reason == PREEMPT_OFF || reason == TRACE_START) &&
  22624. + !per_cpu(hist_preemptoff_counting, cpu)) {
  22625. + per_cpu(hist_preemptoff_counting, cpu) = 1;
  22626. + if (!(time_set++))
  22627. + start = ftrace_now(cpu);
  22628. + per_cpu(hist_preemptoff_start, cpu) = start;
  22629. + }
  22630. +#endif
  22631. +
  22632. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  22633. + if (per_cpu(hist_irqsoff_counting, cpu) &&
  22634. + per_cpu(hist_preemptoff_counting, cpu) &&
  22635. + !per_cpu(hist_preemptirqsoff_counting, cpu)) {
  22636. + per_cpu(hist_preemptirqsoff_counting, cpu) = 1;
  22637. + if (!time_set)
  22638. + start = ftrace_now(cpu);
  22639. + per_cpu(hist_preemptirqsoff_start, cpu) = start;
  22640. + }
  22641. +#endif
  22642. + } else {
  22643. + cycle_t uninitialized_var(stop);
  22644. +
  22645. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  22646. + if ((reason == IRQS_ON || reason == TRACE_STOP) &&
  22647. + per_cpu(hist_irqsoff_counting, cpu)) {
  22648. + cycle_t start = per_cpu(hist_irqsoff_start, cpu);
  22649. +
  22650. + stop = ftrace_now(cpu);
  22651. + time_set++;
  22652. + if (start) {
  22653. + long latency = ((long) (stop - start)) /
  22654. + NSECS_PER_USECS;
  22655. +
  22656. + latency_hist(IRQSOFF_LATENCY, cpu, latency, 0,
  22657. + stop, NULL);
  22658. + }
  22659. + per_cpu(hist_irqsoff_counting, cpu) = 0;
  22660. + }
  22661. +#endif
  22662. +
  22663. +#ifdef CONFIG_PREEMPT_OFF_HIST
  22664. + if ((reason == PREEMPT_ON || reason == TRACE_STOP) &&
  22665. + per_cpu(hist_preemptoff_counting, cpu)) {
  22666. + cycle_t start = per_cpu(hist_preemptoff_start, cpu);
  22667. +
  22668. + if (!(time_set++))
  22669. + stop = ftrace_now(cpu);
  22670. + if (start) {
  22671. + long latency = ((long) (stop - start)) /
  22672. + NSECS_PER_USECS;
  22673. +
  22674. + latency_hist(PREEMPTOFF_LATENCY, cpu, latency,
  22675. + 0, stop, NULL);
  22676. + }
  22677. + per_cpu(hist_preemptoff_counting, cpu) = 0;
  22678. + }
  22679. +#endif
  22680. +
  22681. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  22682. + if ((!per_cpu(hist_irqsoff_counting, cpu) ||
  22683. + !per_cpu(hist_preemptoff_counting, cpu)) &&
  22684. + per_cpu(hist_preemptirqsoff_counting, cpu)) {
  22685. + cycle_t start = per_cpu(hist_preemptirqsoff_start, cpu);
  22686. +
  22687. + if (!time_set)
  22688. + stop = ftrace_now(cpu);
  22689. + if (start) {
  22690. + long latency = ((long) (stop - start)) /
  22691. + NSECS_PER_USECS;
  22692. +
  22693. + latency_hist(PREEMPTIRQSOFF_LATENCY, cpu,
  22694. + latency, 0, stop, NULL);
  22695. + }
  22696. + per_cpu(hist_preemptirqsoff_counting, cpu) = 0;
  22697. + }
  22698. +#endif
  22699. + }
  22700. +}
  22701. +#endif
  22702. +
  22703. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22704. +static DEFINE_RAW_SPINLOCK(wakeup_lock);
  22705. +static notrace void probe_sched_migrate_task(void *v, struct task_struct *task,
  22706. + int cpu)
  22707. +{
  22708. + int old_cpu = task_cpu(task);
  22709. +
  22710. + if (cpu != old_cpu) {
  22711. + unsigned long flags;
  22712. + struct task_struct *cpu_wakeup_task;
  22713. +
  22714. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  22715. +
  22716. + cpu_wakeup_task = per_cpu(wakeup_task, old_cpu);
  22717. + if (task == cpu_wakeup_task) {
  22718. + put_task_struct(cpu_wakeup_task);
  22719. + per_cpu(wakeup_task, old_cpu) = NULL;
  22720. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = task;
  22721. + get_task_struct(cpu_wakeup_task);
  22722. + }
  22723. +
  22724. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  22725. + }
  22726. +}
  22727. +
  22728. +static notrace void probe_wakeup_latency_hist_start(void *v,
  22729. + struct task_struct *p)
  22730. +{
  22731. + unsigned long flags;
  22732. + struct task_struct *curr = current;
  22733. + int cpu = task_cpu(p);
  22734. + struct task_struct *cpu_wakeup_task;
  22735. +
  22736. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  22737. +
  22738. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  22739. +
  22740. + if (wakeup_pid) {
  22741. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  22742. + p->prio == curr->prio)
  22743. + per_cpu(wakeup_sharedprio, cpu) = 1;
  22744. + if (likely(wakeup_pid != task_pid_nr(p)))
  22745. + goto out;
  22746. + } else {
  22747. + if (likely(!rt_task(p)) ||
  22748. + (cpu_wakeup_task && p->prio > cpu_wakeup_task->prio) ||
  22749. + p->prio > curr->prio)
  22750. + goto out;
  22751. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  22752. + p->prio == curr->prio)
  22753. + per_cpu(wakeup_sharedprio, cpu) = 1;
  22754. + }
  22755. +
  22756. + if (cpu_wakeup_task)
  22757. + put_task_struct(cpu_wakeup_task);
  22758. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = p;
  22759. + get_task_struct(cpu_wakeup_task);
  22760. + cpu_wakeup_task->preempt_timestamp_hist =
  22761. + ftrace_now(raw_smp_processor_id());
  22762. +out:
  22763. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  22764. +}
  22765. +
  22766. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  22767. + struct task_struct *prev, struct task_struct *next)
  22768. +{
  22769. + unsigned long flags;
  22770. + int cpu = task_cpu(next);
  22771. + long latency;
  22772. + cycle_t stop;
  22773. + struct task_struct *cpu_wakeup_task;
  22774. +
  22775. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  22776. +
  22777. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  22778. +
  22779. + if (cpu_wakeup_task == NULL)
  22780. + goto out;
  22781. +
  22782. + /* Already running? */
  22783. + if (unlikely(current == cpu_wakeup_task))
  22784. + goto out_reset;
  22785. +
  22786. + if (next != cpu_wakeup_task) {
  22787. + if (next->prio < cpu_wakeup_task->prio)
  22788. + goto out_reset;
  22789. +
  22790. + if (next->prio == cpu_wakeup_task->prio)
  22791. + per_cpu(wakeup_sharedprio, cpu) = 1;
  22792. +
  22793. + goto out;
  22794. + }
  22795. +
  22796. + if (current->prio == cpu_wakeup_task->prio)
  22797. + per_cpu(wakeup_sharedprio, cpu) = 1;
  22798. +
  22799. + /*
  22800. + * The task we are waiting for is about to be switched to.
  22801. + * Calculate latency and store it in histogram.
  22802. + */
  22803. + stop = ftrace_now(raw_smp_processor_id());
  22804. +
  22805. + latency = ((long) (stop - next->preempt_timestamp_hist)) /
  22806. + NSECS_PER_USECS;
  22807. +
  22808. + if (per_cpu(wakeup_sharedprio, cpu)) {
  22809. + latency_hist(WAKEUP_LATENCY_SHAREDPRIO, cpu, latency, 0, stop,
  22810. + next);
  22811. + per_cpu(wakeup_sharedprio, cpu) = 0;
  22812. + } else {
  22813. + latency_hist(WAKEUP_LATENCY, cpu, latency, 0, stop, next);
  22814. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22815. + if (timerandwakeup_enabled_data.enabled) {
  22816. + latency_hist(TIMERANDWAKEUP_LATENCY, cpu,
  22817. + next->timer_offset + latency, next->timer_offset,
  22818. + stop, next);
  22819. + }
  22820. +#endif
  22821. + }
  22822. +
  22823. +out_reset:
  22824. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22825. + next->timer_offset = 0;
  22826. +#endif
  22827. + put_task_struct(cpu_wakeup_task);
  22828. + per_cpu(wakeup_task, cpu) = NULL;
  22829. +out:
  22830. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  22831. +}
  22832. +#endif
  22833. +
  22834. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22835. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  22836. + long long latency_ns, struct task_struct *curr,
  22837. + struct task_struct *task)
  22838. +{
  22839. + if (latency_ns <= 0 && task != NULL && rt_task(task) &&
  22840. + (task->prio < curr->prio ||
  22841. + (task->prio == curr->prio &&
  22842. + !cpumask_test_cpu(cpu, &task->cpus_allowed)))) {
  22843. + long latency;
  22844. + cycle_t now;
  22845. +
  22846. + if (missed_timer_offsets_pid) {
  22847. + if (likely(missed_timer_offsets_pid !=
  22848. + task_pid_nr(task)))
  22849. + return;
  22850. + }
  22851. +
  22852. + now = ftrace_now(cpu);
  22853. + latency = (long) div_s64(-latency_ns, NSECS_PER_USECS);
  22854. + latency_hist(MISSED_TIMER_OFFSETS, cpu, latency, latency, now,
  22855. + task);
  22856. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22857. + task->timer_offset = latency;
  22858. +#endif
  22859. + }
  22860. +}
  22861. +#endif
  22862. +
  22863. +static __init int latency_hist_init(void)
  22864. +{
  22865. + struct dentry *latency_hist_root = NULL;
  22866. + struct dentry *dentry;
  22867. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22868. + struct dentry *dentry_sharedprio;
  22869. +#endif
  22870. + struct dentry *entry;
  22871. + struct dentry *enable_root;
  22872. + int i = 0;
  22873. + struct hist_data *my_hist;
  22874. + char name[64];
  22875. + char *cpufmt = "CPU%d";
  22876. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22877. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22878. + char *cpufmt_maxlatproc = "max_latency-CPU%d";
  22879. + struct maxlatproc_data *mp = NULL;
  22880. +#endif
  22881. +
  22882. + dentry = tracing_init_dentry();
  22883. + latency_hist_root = debugfs_create_dir(latency_hist_dir_root, dentry);
  22884. + enable_root = debugfs_create_dir("enable", latency_hist_root);
  22885. +
  22886. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  22887. + dentry = debugfs_create_dir(irqsoff_hist_dir, latency_hist_root);
  22888. + for_each_possible_cpu(i) {
  22889. + sprintf(name, cpufmt, i);
  22890. + entry = debugfs_create_file(name, 0444, dentry,
  22891. + &per_cpu(irqsoff_hist, i), &latency_hist_fops);
  22892. + my_hist = &per_cpu(irqsoff_hist, i);
  22893. + atomic_set(&my_hist->hist_mode, 1);
  22894. + my_hist->min_lat = LONG_MAX;
  22895. + }
  22896. + entry = debugfs_create_file("reset", 0644, dentry,
  22897. + (void *)IRQSOFF_LATENCY, &latency_hist_reset_fops);
  22898. +#endif
  22899. +
  22900. +#ifdef CONFIG_PREEMPT_OFF_HIST
  22901. + dentry = debugfs_create_dir(preemptoff_hist_dir,
  22902. + latency_hist_root);
  22903. + for_each_possible_cpu(i) {
  22904. + sprintf(name, cpufmt, i);
  22905. + entry = debugfs_create_file(name, 0444, dentry,
  22906. + &per_cpu(preemptoff_hist, i), &latency_hist_fops);
  22907. + my_hist = &per_cpu(preemptoff_hist, i);
  22908. + atomic_set(&my_hist->hist_mode, 1);
  22909. + my_hist->min_lat = LONG_MAX;
  22910. + }
  22911. + entry = debugfs_create_file("reset", 0644, dentry,
  22912. + (void *)PREEMPTOFF_LATENCY, &latency_hist_reset_fops);
  22913. +#endif
  22914. +
  22915. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  22916. + dentry = debugfs_create_dir(preemptirqsoff_hist_dir,
  22917. + latency_hist_root);
  22918. + for_each_possible_cpu(i) {
  22919. + sprintf(name, cpufmt, i);
  22920. + entry = debugfs_create_file(name, 0444, dentry,
  22921. + &per_cpu(preemptirqsoff_hist, i), &latency_hist_fops);
  22922. + my_hist = &per_cpu(preemptirqsoff_hist, i);
  22923. + atomic_set(&my_hist->hist_mode, 1);
  22924. + my_hist->min_lat = LONG_MAX;
  22925. + }
  22926. + entry = debugfs_create_file("reset", 0644, dentry,
  22927. + (void *)PREEMPTIRQSOFF_LATENCY, &latency_hist_reset_fops);
  22928. +#endif
  22929. +
  22930. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  22931. + entry = debugfs_create_file("preemptirqsoff", 0644,
  22932. + enable_root, (void *)&preemptirqsoff_enabled_data,
  22933. + &enable_fops);
  22934. +#endif
  22935. +
  22936. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22937. + dentry = debugfs_create_dir(wakeup_latency_hist_dir,
  22938. + latency_hist_root);
  22939. + dentry_sharedprio = debugfs_create_dir(
  22940. + wakeup_latency_hist_dir_sharedprio, dentry);
  22941. + for_each_possible_cpu(i) {
  22942. + sprintf(name, cpufmt, i);
  22943. +
  22944. + entry = debugfs_create_file(name, 0444, dentry,
  22945. + &per_cpu(wakeup_latency_hist, i),
  22946. + &latency_hist_fops);
  22947. + my_hist = &per_cpu(wakeup_latency_hist, i);
  22948. + atomic_set(&my_hist->hist_mode, 1);
  22949. + my_hist->min_lat = LONG_MAX;
  22950. +
  22951. + entry = debugfs_create_file(name, 0444, dentry_sharedprio,
  22952. + &per_cpu(wakeup_latency_hist_sharedprio, i),
  22953. + &latency_hist_fops);
  22954. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, i);
  22955. + atomic_set(&my_hist->hist_mode, 1);
  22956. + my_hist->min_lat = LONG_MAX;
  22957. +
  22958. + sprintf(name, cpufmt_maxlatproc, i);
  22959. +
  22960. + mp = &per_cpu(wakeup_maxlatproc, i);
  22961. + entry = debugfs_create_file(name, 0444, dentry, mp,
  22962. + &maxlatproc_fops);
  22963. + clear_maxlatprocdata(mp);
  22964. +
  22965. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, i);
  22966. + entry = debugfs_create_file(name, 0444, dentry_sharedprio, mp,
  22967. + &maxlatproc_fops);
  22968. + clear_maxlatprocdata(mp);
  22969. + }
  22970. + entry = debugfs_create_file("pid", 0644, dentry,
  22971. + (void *)&wakeup_pid, &pid_fops);
  22972. + entry = debugfs_create_file("reset", 0644, dentry,
  22973. + (void *)WAKEUP_LATENCY, &latency_hist_reset_fops);
  22974. + entry = debugfs_create_file("reset", 0644, dentry_sharedprio,
  22975. + (void *)WAKEUP_LATENCY_SHAREDPRIO, &latency_hist_reset_fops);
  22976. + entry = debugfs_create_file("wakeup", 0644,
  22977. + enable_root, (void *)&wakeup_latency_enabled_data,
  22978. + &enable_fops);
  22979. +#endif
  22980. +
  22981. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22982. + dentry = debugfs_create_dir(missed_timer_offsets_dir,
  22983. + latency_hist_root);
  22984. + for_each_possible_cpu(i) {
  22985. + sprintf(name, cpufmt, i);
  22986. + entry = debugfs_create_file(name, 0444, dentry,
  22987. + &per_cpu(missed_timer_offsets, i), &latency_hist_fops);
  22988. + my_hist = &per_cpu(missed_timer_offsets, i);
  22989. + atomic_set(&my_hist->hist_mode, 1);
  22990. + my_hist->min_lat = LONG_MAX;
  22991. +
  22992. + sprintf(name, cpufmt_maxlatproc, i);
  22993. + mp = &per_cpu(missed_timer_offsets_maxlatproc, i);
  22994. + entry = debugfs_create_file(name, 0444, dentry, mp,
  22995. + &maxlatproc_fops);
  22996. + clear_maxlatprocdata(mp);
  22997. + }
  22998. + entry = debugfs_create_file("pid", 0644, dentry,
  22999. + (void *)&missed_timer_offsets_pid, &pid_fops);
  23000. + entry = debugfs_create_file("reset", 0644, dentry,
  23001. + (void *)MISSED_TIMER_OFFSETS, &latency_hist_reset_fops);
  23002. + entry = debugfs_create_file("missed_timer_offsets", 0644,
  23003. + enable_root, (void *)&missed_timer_offsets_enabled_data,
  23004. + &enable_fops);
  23005. +#endif
  23006. +
  23007. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  23008. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  23009. + dentry = debugfs_create_dir(timerandwakeup_latency_hist_dir,
  23010. + latency_hist_root);
  23011. + for_each_possible_cpu(i) {
  23012. + sprintf(name, cpufmt, i);
  23013. + entry = debugfs_create_file(name, 0444, dentry,
  23014. + &per_cpu(timerandwakeup_latency_hist, i),
  23015. + &latency_hist_fops);
  23016. + my_hist = &per_cpu(timerandwakeup_latency_hist, i);
  23017. + atomic_set(&my_hist->hist_mode, 1);
  23018. + my_hist->min_lat = LONG_MAX;
  23019. +
  23020. + sprintf(name, cpufmt_maxlatproc, i);
  23021. + mp = &per_cpu(timerandwakeup_maxlatproc, i);
  23022. + entry = debugfs_create_file(name, 0444, dentry, mp,
  23023. + &maxlatproc_fops);
  23024. + clear_maxlatprocdata(mp);
  23025. + }
  23026. + entry = debugfs_create_file("reset", 0644, dentry,
  23027. + (void *)TIMERANDWAKEUP_LATENCY, &latency_hist_reset_fops);
  23028. + entry = debugfs_create_file("timerandwakeup", 0644,
  23029. + enable_root, (void *)&timerandwakeup_enabled_data,
  23030. + &enable_fops);
  23031. +#endif
  23032. + return 0;
  23033. +}
  23034. +
  23035. +device_initcall(latency_hist_init);
  23036. diff -Nur linux-4.1.20.orig/kernel/trace/trace.c linux-4.1.20/kernel/trace/trace.c
  23037. --- linux-4.1.20.orig/kernel/trace/trace.c 2016-03-17 19:11:03.000000000 +0100
  23038. +++ linux-4.1.20/kernel/trace/trace.c 2016-03-21 20:18:34.000000000 +0100
  23039. @@ -1630,6 +1630,7 @@
  23040. struct task_struct *tsk = current;
  23041. entry->preempt_count = pc & 0xff;
  23042. + entry->preempt_lazy_count = preempt_lazy_count();
  23043. entry->pid = (tsk) ? tsk->pid : 0;
  23044. entry->flags =
  23045. #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
  23046. @@ -1639,8 +1640,11 @@
  23047. #endif
  23048. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  23049. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  23050. - (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
  23051. + (tif_need_resched_now() ? TRACE_FLAG_NEED_RESCHED : 0) |
  23052. + (need_resched_lazy() ? TRACE_FLAG_NEED_RESCHED_LAZY : 0) |
  23053. (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
  23054. +
  23055. + entry->migrate_disable = (tsk) ? __migrate_disabled(tsk) & 0xFF : 0;
  23056. }
  23057. EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
  23058. @@ -2558,14 +2562,17 @@
  23059. static void print_lat_help_header(struct seq_file *m)
  23060. {
  23061. - seq_puts(m, "# _------=> CPU# \n"
  23062. - "# / _-----=> irqs-off \n"
  23063. - "# | / _----=> need-resched \n"
  23064. - "# || / _---=> hardirq/softirq \n"
  23065. - "# ||| / _--=> preempt-depth \n"
  23066. - "# |||| / delay \n"
  23067. - "# cmd pid ||||| time | caller \n"
  23068. - "# \\ / ||||| \\ | / \n");
  23069. + seq_puts(m, "# _--------=> CPU# \n"
  23070. + "# / _-------=> irqs-off \n"
  23071. + "# | / _------=> need-resched \n"
  23072. + "# || / _-----=> need-resched_lazy \n"
  23073. + "# ||| / _----=> hardirq/softirq \n"
  23074. + "# |||| / _---=> preempt-depth \n"
  23075. + "# ||||| / _--=> preempt-lazy-depth\n"
  23076. + "# |||||| / _-=> migrate-disable \n"
  23077. + "# ||||||| / delay \n"
  23078. + "# cmd pid |||||||| time | caller \n"
  23079. + "# \\ / |||||||| \\ | / \n");
  23080. }
  23081. static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
  23082. @@ -2591,11 +2598,14 @@
  23083. print_event_info(buf, m);
  23084. seq_puts(m, "# _-----=> irqs-off\n"
  23085. "# / _----=> need-resched\n"
  23086. - "# | / _---=> hardirq/softirq\n"
  23087. - "# || / _--=> preempt-depth\n"
  23088. - "# ||| / delay\n"
  23089. - "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"
  23090. - "# | | | |||| | |\n");
  23091. + "# |/ _-----=> need-resched_lazy\n"
  23092. + "# || / _---=> hardirq/softirq\n"
  23093. + "# ||| / _--=> preempt-depth\n"
  23094. + "# |||| /_--=> preempt-lazy-depth\n"
  23095. + "# ||||| _-=> migrate-disable \n"
  23096. + "# ||||| / delay\n"
  23097. + "# TASK-PID CPU# |||||| TIMESTAMP FUNCTION\n"
  23098. + "# | | | |||||| | |\n");
  23099. }
  23100. void
  23101. diff -Nur linux-4.1.20.orig/kernel/trace/trace.h linux-4.1.20/kernel/trace/trace.h
  23102. --- linux-4.1.20.orig/kernel/trace/trace.h 2016-03-17 19:11:03.000000000 +0100
  23103. +++ linux-4.1.20/kernel/trace/trace.h 2016-03-21 20:18:34.000000000 +0100
  23104. @@ -120,6 +120,7 @@
  23105. * NEED_RESCHED - reschedule is requested
  23106. * HARDIRQ - inside an interrupt handler
  23107. * SOFTIRQ - inside a softirq handler
  23108. + * NEED_RESCHED_LAZY - lazy reschedule is requested
  23109. */
  23110. enum trace_flag_type {
  23111. TRACE_FLAG_IRQS_OFF = 0x01,
  23112. @@ -128,6 +129,7 @@
  23113. TRACE_FLAG_HARDIRQ = 0x08,
  23114. TRACE_FLAG_SOFTIRQ = 0x10,
  23115. TRACE_FLAG_PREEMPT_RESCHED = 0x20,
  23116. + TRACE_FLAG_NEED_RESCHED_LAZY = 0x40,
  23117. };
  23118. #define TRACE_BUF_SIZE 1024
  23119. diff -Nur linux-4.1.20.orig/kernel/trace/trace_events.c linux-4.1.20/kernel/trace/trace_events.c
  23120. --- linux-4.1.20.orig/kernel/trace/trace_events.c 2016-03-17 19:11:03.000000000 +0100
  23121. +++ linux-4.1.20/kernel/trace/trace_events.c 2016-03-21 20:18:34.000000000 +0100
  23122. @@ -162,6 +162,8 @@
  23123. __common_field(unsigned char, flags);
  23124. __common_field(unsigned char, preempt_count);
  23125. __common_field(int, pid);
  23126. + __common_field(unsigned short, migrate_disable);
  23127. + __common_field(unsigned short, padding);
  23128. return ret;
  23129. }
  23130. diff -Nur linux-4.1.20.orig/kernel/trace/trace_irqsoff.c linux-4.1.20/kernel/trace/trace_irqsoff.c
  23131. --- linux-4.1.20.orig/kernel/trace/trace_irqsoff.c 2016-03-17 19:11:03.000000000 +0100
  23132. +++ linux-4.1.20/kernel/trace/trace_irqsoff.c 2016-03-21 20:18:34.000000000 +0100
  23133. @@ -13,6 +13,7 @@
  23134. #include <linux/uaccess.h>
  23135. #include <linux/module.h>
  23136. #include <linux/ftrace.h>
  23137. +#include <trace/events/hist.h>
  23138. #include "trace.h"
  23139. @@ -433,11 +434,13 @@
  23140. {
  23141. if (preempt_trace() || irq_trace())
  23142. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  23143. + trace_preemptirqsoff_hist_rcuidle(TRACE_START, 1);
  23144. }
  23145. EXPORT_SYMBOL_GPL(start_critical_timings);
  23146. void stop_critical_timings(void)
  23147. {
  23148. + trace_preemptirqsoff_hist_rcuidle(TRACE_STOP, 0);
  23149. if (preempt_trace() || irq_trace())
  23150. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  23151. }
  23152. @@ -447,6 +450,7 @@
  23153. #ifdef CONFIG_PROVE_LOCKING
  23154. void time_hardirqs_on(unsigned long a0, unsigned long a1)
  23155. {
  23156. + trace_preemptirqsoff_hist_rcuidle(IRQS_ON, 0);
  23157. if (!preempt_trace() && irq_trace())
  23158. stop_critical_timing(a0, a1);
  23159. }
  23160. @@ -455,6 +459,7 @@
  23161. {
  23162. if (!preempt_trace() && irq_trace())
  23163. start_critical_timing(a0, a1);
  23164. + trace_preemptirqsoff_hist_rcuidle(IRQS_OFF, 1);
  23165. }
  23166. #else /* !CONFIG_PROVE_LOCKING */
  23167. @@ -480,6 +485,7 @@
  23168. */
  23169. void trace_hardirqs_on(void)
  23170. {
  23171. + trace_preemptirqsoff_hist_rcuidle(IRQS_ON, 0);
  23172. if (!preempt_trace() && irq_trace())
  23173. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  23174. }
  23175. @@ -489,11 +495,13 @@
  23176. {
  23177. if (!preempt_trace() && irq_trace())
  23178. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  23179. + trace_preemptirqsoff_hist_rcuidle(IRQS_OFF, 1);
  23180. }
  23181. EXPORT_SYMBOL(trace_hardirqs_off);
  23182. __visible void trace_hardirqs_on_caller(unsigned long caller_addr)
  23183. {
  23184. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  23185. if (!preempt_trace() && irq_trace())
  23186. stop_critical_timing(CALLER_ADDR0, caller_addr);
  23187. }
  23188. @@ -503,6 +511,7 @@
  23189. {
  23190. if (!preempt_trace() && irq_trace())
  23191. start_critical_timing(CALLER_ADDR0, caller_addr);
  23192. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  23193. }
  23194. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  23195. @@ -512,12 +521,14 @@
  23196. #ifdef CONFIG_PREEMPT_TRACER
  23197. void trace_preempt_on(unsigned long a0, unsigned long a1)
  23198. {
  23199. + trace_preemptirqsoff_hist(PREEMPT_ON, 0);
  23200. if (preempt_trace() && !irq_trace())
  23201. stop_critical_timing(a0, a1);
  23202. }
  23203. void trace_preempt_off(unsigned long a0, unsigned long a1)
  23204. {
  23205. + trace_preemptirqsoff_hist(PREEMPT_ON, 1);
  23206. if (preempt_trace() && !irq_trace())
  23207. start_critical_timing(a0, a1);
  23208. }
  23209. diff -Nur linux-4.1.20.orig/kernel/trace/trace_output.c linux-4.1.20/kernel/trace/trace_output.c
  23210. --- linux-4.1.20.orig/kernel/trace/trace_output.c 2016-03-17 19:11:03.000000000 +0100
  23211. +++ linux-4.1.20/kernel/trace/trace_output.c 2016-03-21 20:18:34.000000000 +0100
  23212. @@ -430,6 +430,7 @@
  23213. {
  23214. char hardsoft_irq;
  23215. char need_resched;
  23216. + char need_resched_lazy;
  23217. char irqs_off;
  23218. int hardirq;
  23219. int softirq;
  23220. @@ -457,6 +458,8 @@
  23221. need_resched = '.';
  23222. break;
  23223. }
  23224. + need_resched_lazy =
  23225. + (entry->flags & TRACE_FLAG_NEED_RESCHED_LAZY) ? 'L' : '.';
  23226. hardsoft_irq =
  23227. (hardirq && softirq) ? 'H' :
  23228. @@ -464,14 +467,25 @@
  23229. softirq ? 's' :
  23230. '.';
  23231. - trace_seq_printf(s, "%c%c%c",
  23232. - irqs_off, need_resched, hardsoft_irq);
  23233. + trace_seq_printf(s, "%c%c%c%c",
  23234. + irqs_off, need_resched, need_resched_lazy,
  23235. + hardsoft_irq);
  23236. if (entry->preempt_count)
  23237. trace_seq_printf(s, "%x", entry->preempt_count);
  23238. else
  23239. trace_seq_putc(s, '.');
  23240. + if (entry->preempt_lazy_count)
  23241. + trace_seq_printf(s, "%x", entry->preempt_lazy_count);
  23242. + else
  23243. + trace_seq_putc(s, '.');
  23244. +
  23245. + if (entry->migrate_disable)
  23246. + trace_seq_printf(s, "%x", entry->migrate_disable);
  23247. + else
  23248. + trace_seq_putc(s, '.');
  23249. +
  23250. return !trace_seq_has_overflowed(s);
  23251. }
  23252. diff -Nur linux-4.1.20.orig/kernel/trace/trace_sched_switch.c linux-4.1.20/kernel/trace/trace_sched_switch.c
  23253. --- linux-4.1.20.orig/kernel/trace/trace_sched_switch.c 2016-03-17 19:11:03.000000000 +0100
  23254. +++ linux-4.1.20/kernel/trace/trace_sched_switch.c 2016-03-21 20:18:34.000000000 +0100
  23255. @@ -26,7 +26,7 @@
  23256. }
  23257. static void
  23258. -probe_sched_wakeup(void *ignore, struct task_struct *wakee, int success)
  23259. +probe_sched_wakeup(void *ignore, struct task_struct *wakee)
  23260. {
  23261. if (unlikely(!sched_ref))
  23262. return;
  23263. diff -Nur linux-4.1.20.orig/kernel/trace/trace_sched_wakeup.c linux-4.1.20/kernel/trace/trace_sched_wakeup.c
  23264. --- linux-4.1.20.orig/kernel/trace/trace_sched_wakeup.c 2016-03-17 19:11:03.000000000 +0100
  23265. +++ linux-4.1.20/kernel/trace/trace_sched_wakeup.c 2016-03-21 20:18:34.000000000 +0100
  23266. @@ -514,7 +514,7 @@
  23267. }
  23268. static void
  23269. -probe_wakeup(void *ignore, struct task_struct *p, int success)
  23270. +probe_wakeup(void *ignore, struct task_struct *p)
  23271. {
  23272. struct trace_array_cpu *data;
  23273. int cpu = smp_processor_id();
  23274. diff -Nur linux-4.1.20.orig/kernel/user.c linux-4.1.20/kernel/user.c
  23275. --- linux-4.1.20.orig/kernel/user.c 2016-03-17 19:11:03.000000000 +0100
  23276. +++ linux-4.1.20/kernel/user.c 2016-03-21 20:18:34.000000000 +0100
  23277. @@ -161,11 +161,11 @@
  23278. if (!up)
  23279. return;
  23280. - local_irq_save(flags);
  23281. + local_irq_save_nort(flags);
  23282. if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  23283. free_user(up, flags);
  23284. else
  23285. - local_irq_restore(flags);
  23286. + local_irq_restore_nort(flags);
  23287. }
  23288. struct user_struct *alloc_uid(kuid_t uid)
  23289. diff -Nur linux-4.1.20.orig/kernel/watchdog.c linux-4.1.20/kernel/watchdog.c
  23290. --- linux-4.1.20.orig/kernel/watchdog.c 2016-03-17 19:11:03.000000000 +0100
  23291. +++ linux-4.1.20/kernel/watchdog.c 2016-03-21 20:18:34.000000000 +0100
  23292. @@ -262,6 +262,8 @@
  23293. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  23294. +static DEFINE_RAW_SPINLOCK(watchdog_output_lock);
  23295. +
  23296. static struct perf_event_attr wd_hw_attr = {
  23297. .type = PERF_TYPE_HARDWARE,
  23298. .config = PERF_COUNT_HW_CPU_CYCLES,
  23299. @@ -295,13 +297,21 @@
  23300. /* only print hardlockups once */
  23301. if (__this_cpu_read(hard_watchdog_warn) == true)
  23302. return;
  23303. + /*
  23304. + * If early-printk is enabled then make sure we do not
  23305. + * lock up in printk() and kill console logging:
  23306. + */
  23307. + printk_kill();
  23308. - if (hardlockup_panic)
  23309. + if (hardlockup_panic) {
  23310. panic("Watchdog detected hard LOCKUP on cpu %d",
  23311. this_cpu);
  23312. - else
  23313. + } else {
  23314. + raw_spin_lock(&watchdog_output_lock);
  23315. WARN(1, "Watchdog detected hard LOCKUP on cpu %d",
  23316. this_cpu);
  23317. + raw_spin_unlock(&watchdog_output_lock);
  23318. + }
  23319. __this_cpu_write(hard_watchdog_warn, true);
  23320. return;
  23321. @@ -444,6 +454,7 @@
  23322. /* kick off the timer for the hardlockup detector */
  23323. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  23324. hrtimer->function = watchdog_timer_fn;
  23325. + hrtimer->irqsafe = 1;
  23326. /* Enable the perf event */
  23327. watchdog_nmi_enable(cpu);
  23328. diff -Nur linux-4.1.20.orig/kernel/workqueue.c linux-4.1.20/kernel/workqueue.c
  23329. --- linux-4.1.20.orig/kernel/workqueue.c 2016-03-17 19:11:03.000000000 +0100
  23330. +++ linux-4.1.20/kernel/workqueue.c 2016-03-21 20:18:34.000000000 +0100
  23331. @@ -48,6 +48,8 @@
  23332. #include <linux/nodemask.h>
  23333. #include <linux/moduleparam.h>
  23334. #include <linux/uaccess.h>
  23335. +#include <linux/locallock.h>
  23336. +#include <linux/delay.h>
  23337. #include "workqueue_internal.h"
  23338. @@ -121,11 +123,16 @@
  23339. * cpu or grabbing pool->lock is enough for read access. If
  23340. * POOL_DISASSOCIATED is set, it's identical to L.
  23341. *
  23342. + * On RT we need the extra protection via rt_lock_idle_list() for
  23343. + * the list manipulations against read access from
  23344. + * wq_worker_sleeping(). All other places are nicely serialized via
  23345. + * pool->lock.
  23346. + *
  23347. * A: pool->attach_mutex protected.
  23348. *
  23349. * PL: wq_pool_mutex protected.
  23350. *
  23351. - * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
  23352. + * PR: wq_pool_mutex protected for writes. RCU protected for reads.
  23353. *
  23354. * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads.
  23355. *
  23356. @@ -134,7 +141,7 @@
  23357. *
  23358. * WQ: wq->mutex protected.
  23359. *
  23360. - * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
  23361. + * WR: wq->mutex protected for writes. RCU protected for reads.
  23362. *
  23363. * MD: wq_mayday_lock protected.
  23364. */
  23365. @@ -183,7 +190,7 @@
  23366. atomic_t nr_running ____cacheline_aligned_in_smp;
  23367. /*
  23368. - * Destruction of pool is sched-RCU protected to allow dereferences
  23369. + * Destruction of pool is RCU protected to allow dereferences
  23370. * from get_work_pool().
  23371. */
  23372. struct rcu_head rcu;
  23373. @@ -212,7 +219,7 @@
  23374. /*
  23375. * Release of unbound pwq is punted to system_wq. See put_pwq()
  23376. * and pwq_unbound_release_workfn() for details. pool_workqueue
  23377. - * itself is also sched-RCU protected so that the first pwq can be
  23378. + * itself is also RCU protected so that the first pwq can be
  23379. * determined without grabbing wq->mutex.
  23380. */
  23381. struct work_struct unbound_release_work;
  23382. @@ -334,6 +341,8 @@
  23383. struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
  23384. EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
  23385. +static DEFINE_LOCAL_IRQ_LOCK(pendingb_lock);
  23386. +
  23387. static int worker_thread(void *__worker);
  23388. static void copy_workqueue_attrs(struct workqueue_attrs *to,
  23389. const struct workqueue_attrs *from);
  23390. @@ -343,14 +352,14 @@
  23391. #include <trace/events/workqueue.h>
  23392. #define assert_rcu_or_pool_mutex() \
  23393. - rcu_lockdep_assert(rcu_read_lock_sched_held() || \
  23394. + rcu_lockdep_assert(rcu_read_lock_held() || \
  23395. lockdep_is_held(&wq_pool_mutex), \
  23396. - "sched RCU or wq_pool_mutex should be held")
  23397. + "RCU or wq_pool_mutex should be held")
  23398. #define assert_rcu_or_wq_mutex(wq) \
  23399. - rcu_lockdep_assert(rcu_read_lock_sched_held() || \
  23400. + rcu_lockdep_assert(rcu_read_lock_held() || \
  23401. lockdep_is_held(&wq->mutex), \
  23402. - "sched RCU or wq->mutex should be held")
  23403. + "RCU or wq->mutex should be held")
  23404. #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \
  23405. rcu_lockdep_assert(rcu_read_lock_sched_held() || \
  23406. @@ -368,7 +377,7 @@
  23407. * @pool: iteration cursor
  23408. * @pi: integer used for iteration
  23409. *
  23410. - * This must be called either with wq_pool_mutex held or sched RCU read
  23411. + * This must be called either with wq_pool_mutex held or RCU read
  23412. * locked. If the pool needs to be used beyond the locking in effect, the
  23413. * caller is responsible for guaranteeing that the pool stays online.
  23414. *
  23415. @@ -400,7 +409,7 @@
  23416. * @pwq: iteration cursor
  23417. * @wq: the target workqueue
  23418. *
  23419. - * This must be called either with wq->mutex held or sched RCU read locked.
  23420. + * This must be called either with wq->mutex held or RCU read locked.
  23421. * If the pwq needs to be used beyond the locking in effect, the caller is
  23422. * responsible for guaranteeing that the pwq stays online.
  23423. *
  23424. @@ -412,6 +421,31 @@
  23425. if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
  23426. else
  23427. +#ifdef CONFIG_PREEMPT_RT_BASE
  23428. +static inline void rt_lock_idle_list(struct worker_pool *pool)
  23429. +{
  23430. + preempt_disable();
  23431. +}
  23432. +static inline void rt_unlock_idle_list(struct worker_pool *pool)
  23433. +{
  23434. + preempt_enable();
  23435. +}
  23436. +static inline void sched_lock_idle_list(struct worker_pool *pool) { }
  23437. +static inline void sched_unlock_idle_list(struct worker_pool *pool) { }
  23438. +#else
  23439. +static inline void rt_lock_idle_list(struct worker_pool *pool) { }
  23440. +static inline void rt_unlock_idle_list(struct worker_pool *pool) { }
  23441. +static inline void sched_lock_idle_list(struct worker_pool *pool)
  23442. +{
  23443. + spin_lock_irq(&pool->lock);
  23444. +}
  23445. +static inline void sched_unlock_idle_list(struct worker_pool *pool)
  23446. +{
  23447. + spin_unlock_irq(&pool->lock);
  23448. +}
  23449. +#endif
  23450. +
  23451. +
  23452. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  23453. static struct debug_obj_descr work_debug_descr;
  23454. @@ -562,8 +596,7 @@
  23455. * @wq: the target workqueue
  23456. * @node: the node ID
  23457. *
  23458. - * This must be called with any of wq_pool_mutex, wq->mutex or sched RCU
  23459. - * read locked.
  23460. + * This must be called with any of wq_pool_mutex, wq->mutex or RCU read locked.
  23461. * If the pwq needs to be used beyond the locking in effect, the caller is
  23462. * responsible for guaranteeing that the pwq stays online.
  23463. *
  23464. @@ -677,8 +710,8 @@
  23465. * @work: the work item of interest
  23466. *
  23467. * Pools are created and destroyed under wq_pool_mutex, and allows read
  23468. - * access under sched-RCU read lock. As such, this function should be
  23469. - * called under wq_pool_mutex or with preemption disabled.
  23470. + * access under RCU read lock. As such, this function should be
  23471. + * called under wq_pool_mutex or inside of a rcu_read_lock() region.
  23472. *
  23473. * All fields of the returned pool are accessible as long as the above
  23474. * mentioned locking is in effect. If the returned pool needs to be used
  23475. @@ -815,51 +848,44 @@
  23476. */
  23477. static void wake_up_worker(struct worker_pool *pool)
  23478. {
  23479. - struct worker *worker = first_idle_worker(pool);
  23480. + struct worker *worker;
  23481. +
  23482. + rt_lock_idle_list(pool);
  23483. +
  23484. + worker = first_idle_worker(pool);
  23485. if (likely(worker))
  23486. wake_up_process(worker->task);
  23487. +
  23488. + rt_unlock_idle_list(pool);
  23489. }
  23490. /**
  23491. - * wq_worker_waking_up - a worker is waking up
  23492. - * @task: task waking up
  23493. - * @cpu: CPU @task is waking up to
  23494. - *
  23495. - * This function is called during try_to_wake_up() when a worker is
  23496. - * being awoken.
  23497. + * wq_worker_running - a worker is running again
  23498. + * @task: task returning from sleep
  23499. *
  23500. - * CONTEXT:
  23501. - * spin_lock_irq(rq->lock)
  23502. + * This function is called when a worker returns from schedule()
  23503. */
  23504. -void wq_worker_waking_up(struct task_struct *task, int cpu)
  23505. +void wq_worker_running(struct task_struct *task)
  23506. {
  23507. struct worker *worker = kthread_data(task);
  23508. - if (!(worker->flags & WORKER_NOT_RUNNING)) {
  23509. - WARN_ON_ONCE(worker->pool->cpu != cpu);
  23510. + if (!worker->sleeping)
  23511. + return;
  23512. + if (!(worker->flags & WORKER_NOT_RUNNING))
  23513. atomic_inc(&worker->pool->nr_running);
  23514. - }
  23515. + worker->sleeping = 0;
  23516. }
  23517. /**
  23518. * wq_worker_sleeping - a worker is going to sleep
  23519. * @task: task going to sleep
  23520. - * @cpu: CPU in question, must be the current CPU number
  23521. - *
  23522. - * This function is called during schedule() when a busy worker is
  23523. - * going to sleep. Worker on the same cpu can be woken up by
  23524. - * returning pointer to its task.
  23525. - *
  23526. - * CONTEXT:
  23527. - * spin_lock_irq(rq->lock)
  23528. - *
  23529. - * Return:
  23530. - * Worker task on @cpu to wake up, %NULL if none.
  23531. + * This function is called from schedule() when a busy worker is
  23532. + * going to sleep.
  23533. */
  23534. -struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
  23535. +void wq_worker_sleeping(struct task_struct *task)
  23536. {
  23537. - struct worker *worker = kthread_data(task), *to_wakeup = NULL;
  23538. + struct worker *worker = kthread_data(task);
  23539. struct worker_pool *pool;
  23540. /*
  23541. @@ -868,29 +894,26 @@
  23542. * checking NOT_RUNNING.
  23543. */
  23544. if (worker->flags & WORKER_NOT_RUNNING)
  23545. - return NULL;
  23546. + return;
  23547. pool = worker->pool;
  23548. - /* this can only happen on the local cpu */
  23549. - if (WARN_ON_ONCE(cpu != raw_smp_processor_id() || pool->cpu != cpu))
  23550. - return NULL;
  23551. + if (WARN_ON_ONCE(worker->sleeping))
  23552. + return;
  23553. +
  23554. + worker->sleeping = 1;
  23555. /*
  23556. * The counterpart of the following dec_and_test, implied mb,
  23557. * worklist not empty test sequence is in insert_work().
  23558. * Please read comment there.
  23559. - *
  23560. - * NOT_RUNNING is clear. This means that we're bound to and
  23561. - * running on the local cpu w/ rq lock held and preemption
  23562. - * disabled, which in turn means that none else could be
  23563. - * manipulating idle_list, so dereferencing idle_list without pool
  23564. - * lock is safe.
  23565. */
  23566. if (atomic_dec_and_test(&pool->nr_running) &&
  23567. - !list_empty(&pool->worklist))
  23568. - to_wakeup = first_idle_worker(pool);
  23569. - return to_wakeup ? to_wakeup->task : NULL;
  23570. + !list_empty(&pool->worklist)) {
  23571. + sched_lock_idle_list(pool);
  23572. + wake_up_worker(pool);
  23573. + sched_unlock_idle_list(pool);
  23574. + }
  23575. }
  23576. /**
  23577. @@ -1084,12 +1107,12 @@
  23578. {
  23579. if (pwq) {
  23580. /*
  23581. - * As both pwqs and pools are sched-RCU protected, the
  23582. + * As both pwqs and pools are RCU protected, the
  23583. * following lock operations are safe.
  23584. */
  23585. - spin_lock_irq(&pwq->pool->lock);
  23586. + local_spin_lock_irq(pendingb_lock, &pwq->pool->lock);
  23587. put_pwq(pwq);
  23588. - spin_unlock_irq(&pwq->pool->lock);
  23589. + local_spin_unlock_irq(pendingb_lock, &pwq->pool->lock);
  23590. }
  23591. }
  23592. @@ -1191,7 +1214,7 @@
  23593. struct worker_pool *pool;
  23594. struct pool_workqueue *pwq;
  23595. - local_irq_save(*flags);
  23596. + local_lock_irqsave(pendingb_lock, *flags);
  23597. /* try to steal the timer if it exists */
  23598. if (is_dwork) {
  23599. @@ -1210,6 +1233,7 @@
  23600. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
  23601. return 0;
  23602. + rcu_read_lock();
  23603. /*
  23604. * The queueing is in progress, or it is already queued. Try to
  23605. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  23606. @@ -1248,14 +1272,16 @@
  23607. set_work_pool_and_keep_pending(work, pool->id);
  23608. spin_unlock(&pool->lock);
  23609. + rcu_read_unlock();
  23610. return 1;
  23611. }
  23612. spin_unlock(&pool->lock);
  23613. fail:
  23614. - local_irq_restore(*flags);
  23615. + rcu_read_unlock();
  23616. + local_unlock_irqrestore(pendingb_lock, *flags);
  23617. if (work_is_canceling(work))
  23618. return -ENOENT;
  23619. - cpu_relax();
  23620. + cpu_chill();
  23621. return -EAGAIN;
  23622. }
  23623. @@ -1324,7 +1350,7 @@
  23624. * queued or lose PENDING. Grabbing PENDING and queueing should
  23625. * happen with IRQ disabled.
  23626. */
  23627. - WARN_ON_ONCE(!irqs_disabled());
  23628. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  23629. debug_work_activate(work);
  23630. @@ -1332,6 +1358,8 @@
  23631. if (unlikely(wq->flags & __WQ_DRAINING) &&
  23632. WARN_ON_ONCE(!is_chained_work(wq)))
  23633. return;
  23634. +
  23635. + rcu_read_lock();
  23636. retry:
  23637. if (req_cpu == WORK_CPU_UNBOUND)
  23638. cpu = raw_smp_processor_id();
  23639. @@ -1388,10 +1416,8 @@
  23640. /* pwq determined, queue */
  23641. trace_workqueue_queue_work(req_cpu, pwq, work);
  23642. - if (WARN_ON(!list_empty(&work->entry))) {
  23643. - spin_unlock(&pwq->pool->lock);
  23644. - return;
  23645. - }
  23646. + if (WARN_ON(!list_empty(&work->entry)))
  23647. + goto out;
  23648. pwq->nr_in_flight[pwq->work_color]++;
  23649. work_flags = work_color_to_flags(pwq->work_color);
  23650. @@ -1407,7 +1433,9 @@
  23651. insert_work(pwq, work, worklist, work_flags);
  23652. +out:
  23653. spin_unlock(&pwq->pool->lock);
  23654. + rcu_read_unlock();
  23655. }
  23656. /**
  23657. @@ -1427,14 +1455,14 @@
  23658. bool ret = false;
  23659. unsigned long flags;
  23660. - local_irq_save(flags);
  23661. + local_lock_irqsave(pendingb_lock,flags);
  23662. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  23663. __queue_work(cpu, wq, work);
  23664. ret = true;
  23665. }
  23666. - local_irq_restore(flags);
  23667. + local_unlock_irqrestore(pendingb_lock, flags);
  23668. return ret;
  23669. }
  23670. EXPORT_SYMBOL(queue_work_on);
  23671. @@ -1501,14 +1529,14 @@
  23672. unsigned long flags;
  23673. /* read the comment in __queue_work() */
  23674. - local_irq_save(flags);
  23675. + local_lock_irqsave(pendingb_lock, flags);
  23676. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  23677. __queue_delayed_work(cpu, wq, dwork, delay);
  23678. ret = true;
  23679. }
  23680. - local_irq_restore(flags);
  23681. + local_unlock_irqrestore(pendingb_lock, flags);
  23682. return ret;
  23683. }
  23684. EXPORT_SYMBOL(queue_delayed_work_on);
  23685. @@ -1543,7 +1571,7 @@
  23686. if (likely(ret >= 0)) {
  23687. __queue_delayed_work(cpu, wq, dwork, delay);
  23688. - local_irq_restore(flags);
  23689. + local_unlock_irqrestore(pendingb_lock, flags);
  23690. }
  23691. /* -ENOENT from try_to_grab_pending() becomes %true */
  23692. @@ -1576,7 +1604,9 @@
  23693. worker->last_active = jiffies;
  23694. /* idle_list is LIFO */
  23695. + rt_lock_idle_list(pool);
  23696. list_add(&worker->entry, &pool->idle_list);
  23697. + rt_unlock_idle_list(pool);
  23698. if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
  23699. mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
  23700. @@ -1609,7 +1639,9 @@
  23701. return;
  23702. worker_clr_flags(worker, WORKER_IDLE);
  23703. pool->nr_idle--;
  23704. + rt_lock_idle_list(pool);
  23705. list_del_init(&worker->entry);
  23706. + rt_unlock_idle_list(pool);
  23707. }
  23708. static struct worker *alloc_worker(int node)
  23709. @@ -1777,7 +1809,9 @@
  23710. pool->nr_workers--;
  23711. pool->nr_idle--;
  23712. + rt_lock_idle_list(pool);
  23713. list_del_init(&worker->entry);
  23714. + rt_unlock_idle_list(pool);
  23715. worker->flags |= WORKER_DIE;
  23716. wake_up_process(worker->task);
  23717. }
  23718. @@ -2694,14 +2728,14 @@
  23719. might_sleep();
  23720. - local_irq_disable();
  23721. + rcu_read_lock();
  23722. pool = get_work_pool(work);
  23723. if (!pool) {
  23724. - local_irq_enable();
  23725. + rcu_read_unlock();
  23726. return false;
  23727. }
  23728. - spin_lock(&pool->lock);
  23729. + spin_lock_irq(&pool->lock);
  23730. /* see the comment in try_to_grab_pending() with the same code */
  23731. pwq = get_work_pwq(work);
  23732. if (pwq) {
  23733. @@ -2728,10 +2762,11 @@
  23734. else
  23735. lock_map_acquire_read(&pwq->wq->lockdep_map);
  23736. lock_map_release(&pwq->wq->lockdep_map);
  23737. -
  23738. + rcu_read_unlock();
  23739. return true;
  23740. already_gone:
  23741. spin_unlock_irq(&pool->lock);
  23742. + rcu_read_unlock();
  23743. return false;
  23744. }
  23745. @@ -2818,7 +2853,7 @@
  23746. /* tell other tasks trying to grab @work to back off */
  23747. mark_work_canceling(work);
  23748. - local_irq_restore(flags);
  23749. + local_unlock_irqrestore(pendingb_lock, flags);
  23750. flush_work(work);
  23751. clear_work_data(work);
  23752. @@ -2873,10 +2908,10 @@
  23753. */
  23754. bool flush_delayed_work(struct delayed_work *dwork)
  23755. {
  23756. - local_irq_disable();
  23757. + local_lock_irq(pendingb_lock);
  23758. if (del_timer_sync(&dwork->timer))
  23759. __queue_work(dwork->cpu, dwork->wq, &dwork->work);
  23760. - local_irq_enable();
  23761. + local_unlock_irq(pendingb_lock);
  23762. return flush_work(&dwork->work);
  23763. }
  23764. EXPORT_SYMBOL(flush_delayed_work);
  23765. @@ -2911,7 +2946,7 @@
  23766. set_work_pool_and_clear_pending(&dwork->work,
  23767. get_work_pool_id(&dwork->work));
  23768. - local_irq_restore(flags);
  23769. + local_unlock_irqrestore(pendingb_lock, flags);
  23770. return ret;
  23771. }
  23772. EXPORT_SYMBOL(cancel_delayed_work);
  23773. @@ -3169,7 +3204,7 @@
  23774. * put_unbound_pool - put a worker_pool
  23775. * @pool: worker_pool to put
  23776. *
  23777. - * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
  23778. + * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU
  23779. * safe manner. get_unbound_pool() calls this function on its failure path
  23780. * and this function should be able to release pools which went through,
  23781. * successfully or not, init_worker_pool().
  23782. @@ -3223,8 +3258,8 @@
  23783. del_timer_sync(&pool->idle_timer);
  23784. del_timer_sync(&pool->mayday_timer);
  23785. - /* sched-RCU protected to allow dereferences from get_work_pool() */
  23786. - call_rcu_sched(&pool->rcu, rcu_free_pool);
  23787. + /* RCU protected to allow dereferences from get_work_pool() */
  23788. + call_rcu(&pool->rcu, rcu_free_pool);
  23789. }
  23790. /**
  23791. @@ -3329,14 +3364,14 @@
  23792. put_unbound_pool(pool);
  23793. mutex_unlock(&wq_pool_mutex);
  23794. - call_rcu_sched(&pwq->rcu, rcu_free_pwq);
  23795. + call_rcu(&pwq->rcu, rcu_free_pwq);
  23796. /*
  23797. * If we're the last pwq going away, @wq is already dead and no one
  23798. * is gonna access it anymore. Schedule RCU free.
  23799. */
  23800. if (is_last)
  23801. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  23802. + call_rcu(&wq->rcu, rcu_free_wq);
  23803. }
  23804. /**
  23805. @@ -3974,7 +4009,7 @@
  23806. * The base ref is never dropped on per-cpu pwqs. Directly
  23807. * schedule RCU free.
  23808. */
  23809. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  23810. + call_rcu(&wq->rcu, rcu_free_wq);
  23811. } else {
  23812. /*
  23813. * We're the sole accessor of @wq at this point. Directly
  23814. @@ -4067,7 +4102,8 @@
  23815. struct pool_workqueue *pwq;
  23816. bool ret;
  23817. - rcu_read_lock_sched();
  23818. + rcu_read_lock();
  23819. + preempt_disable();
  23820. if (cpu == WORK_CPU_UNBOUND)
  23821. cpu = smp_processor_id();
  23822. @@ -4078,7 +4114,8 @@
  23823. pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
  23824. ret = !list_empty(&pwq->delayed_works);
  23825. - rcu_read_unlock_sched();
  23826. + preempt_enable();
  23827. + rcu_read_unlock();
  23828. return ret;
  23829. }
  23830. @@ -4104,15 +4141,15 @@
  23831. if (work_pending(work))
  23832. ret |= WORK_BUSY_PENDING;
  23833. - local_irq_save(flags);
  23834. + rcu_read_lock();
  23835. pool = get_work_pool(work);
  23836. if (pool) {
  23837. - spin_lock(&pool->lock);
  23838. + spin_lock_irqsave(&pool->lock, flags);
  23839. if (find_worker_executing_work(pool, work))
  23840. ret |= WORK_BUSY_RUNNING;
  23841. - spin_unlock(&pool->lock);
  23842. + spin_unlock_irqrestore(&pool->lock, flags);
  23843. }
  23844. - local_irq_restore(flags);
  23845. + rcu_read_unlock();
  23846. return ret;
  23847. }
  23848. @@ -4301,7 +4338,7 @@
  23849. unsigned long flags;
  23850. int pi;
  23851. - rcu_read_lock_sched();
  23852. + rcu_read_lock();
  23853. pr_info("Showing busy workqueues and worker pools:\n");
  23854. @@ -4352,7 +4389,7 @@
  23855. spin_unlock_irqrestore(&pool->lock, flags);
  23856. }
  23857. - rcu_read_unlock_sched();
  23858. + rcu_read_unlock();
  23859. }
  23860. /*
  23861. @@ -4702,16 +4739,16 @@
  23862. * nr_active is monotonically decreasing. It's safe
  23863. * to peek without lock.
  23864. */
  23865. - rcu_read_lock_sched();
  23866. + rcu_read_lock();
  23867. for_each_pwq(pwq, wq) {
  23868. WARN_ON_ONCE(pwq->nr_active < 0);
  23869. if (pwq->nr_active) {
  23870. busy = true;
  23871. - rcu_read_unlock_sched();
  23872. + rcu_read_unlock();
  23873. goto out_unlock;
  23874. }
  23875. }
  23876. - rcu_read_unlock_sched();
  23877. + rcu_read_unlock();
  23878. }
  23879. out_unlock:
  23880. mutex_unlock(&wq_pool_mutex);
  23881. @@ -4825,7 +4862,8 @@
  23882. const char *delim = "";
  23883. int node, written = 0;
  23884. - rcu_read_lock_sched();
  23885. + get_online_cpus();
  23886. + rcu_read_lock();
  23887. for_each_node(node) {
  23888. written += scnprintf(buf + written, PAGE_SIZE - written,
  23889. "%s%d:%d", delim, node,
  23890. @@ -4833,7 +4871,8 @@
  23891. delim = " ";
  23892. }
  23893. written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
  23894. - rcu_read_unlock_sched();
  23895. + rcu_read_unlock();
  23896. + put_online_cpus();
  23897. return written;
  23898. }
  23899. diff -Nur linux-4.1.20.orig/kernel/workqueue_internal.h linux-4.1.20/kernel/workqueue_internal.h
  23900. --- linux-4.1.20.orig/kernel/workqueue_internal.h 2016-03-17 19:11:03.000000000 +0100
  23901. +++ linux-4.1.20/kernel/workqueue_internal.h 2016-03-21 20:18:34.000000000 +0100
  23902. @@ -43,6 +43,7 @@
  23903. unsigned long last_active; /* L: last active timestamp */
  23904. unsigned int flags; /* X: flags */
  23905. int id; /* I: worker id */
  23906. + int sleeping; /* None */
  23907. /*
  23908. * Opaque string set with work_set_desc(). Printed out with task
  23909. @@ -68,7 +69,7 @@
  23910. * Scheduler hooks for concurrency managed workqueue. Only to be used from
  23911. * sched/core.c and workqueue.c.
  23912. */
  23913. -void wq_worker_waking_up(struct task_struct *task, int cpu);
  23914. -struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu);
  23915. +void wq_worker_running(struct task_struct *task);
  23916. +void wq_worker_sleeping(struct task_struct *task);
  23917. #endif /* _KERNEL_WORKQUEUE_INTERNAL_H */
  23918. diff -Nur linux-4.1.20.orig/lib/Kconfig linux-4.1.20/lib/Kconfig
  23919. --- linux-4.1.20.orig/lib/Kconfig 2016-03-17 19:11:03.000000000 +0100
  23920. +++ linux-4.1.20/lib/Kconfig 2016-03-21 20:18:34.000000000 +0100
  23921. @@ -391,6 +391,7 @@
  23922. config CPUMASK_OFFSTACK
  23923. bool "Force CPU masks off stack" if DEBUG_PER_CPU_MAPS
  23924. + depends on !PREEMPT_RT_FULL
  23925. help
  23926. Use dynamic allocation for cpumask_var_t, instead of putting
  23927. them on the stack. This is a bit more expensive, but avoids
  23928. diff -Nur linux-4.1.20.orig/lib/debugobjects.c linux-4.1.20/lib/debugobjects.c
  23929. --- linux-4.1.20.orig/lib/debugobjects.c 2016-03-17 19:11:03.000000000 +0100
  23930. +++ linux-4.1.20/lib/debugobjects.c 2016-03-21 20:18:34.000000000 +0100
  23931. @@ -309,7 +309,10 @@
  23932. struct debug_obj *obj;
  23933. unsigned long flags;
  23934. - fill_pool();
  23935. +#ifdef CONFIG_PREEMPT_RT_FULL
  23936. + if (preempt_count() == 0 && !irqs_disabled())
  23937. +#endif
  23938. + fill_pool();
  23939. db = get_bucket((unsigned long) addr);
  23940. diff -Nur linux-4.1.20.orig/lib/dump_stack.c linux-4.1.20/lib/dump_stack.c
  23941. --- linux-4.1.20.orig/lib/dump_stack.c 2016-03-17 19:11:03.000000000 +0100
  23942. +++ linux-4.1.20/lib/dump_stack.c 2016-03-21 20:18:34.000000000 +0100
  23943. @@ -8,6 +8,7 @@
  23944. #include <linux/sched.h>
  23945. #include <linux/smp.h>
  23946. #include <linux/atomic.h>
  23947. +#include <linux/locallock.h>
  23948. static void __dump_stack(void)
  23949. {
  23950. diff -Nur linux-4.1.20.orig/lib/idr.c linux-4.1.20/lib/idr.c
  23951. --- linux-4.1.20.orig/lib/idr.c 2016-03-17 19:11:03.000000000 +0100
  23952. +++ linux-4.1.20/lib/idr.c 2016-03-21 20:18:34.000000000 +0100
  23953. @@ -30,6 +30,7 @@
  23954. #include <linux/idr.h>
  23955. #include <linux/spinlock.h>
  23956. #include <linux/percpu.h>
  23957. +#include <linux/locallock.h>
  23958. #define MAX_IDR_SHIFT (sizeof(int) * 8 - 1)
  23959. #define MAX_IDR_BIT (1U << MAX_IDR_SHIFT)
  23960. @@ -366,6 +367,35 @@
  23961. idr_mark_full(pa, id);
  23962. }
  23963. +#ifdef CONFIG_PREEMPT_RT_FULL
  23964. +static DEFINE_LOCAL_IRQ_LOCK(idr_lock);
  23965. +
  23966. +static inline void idr_preload_lock(void)
  23967. +{
  23968. + local_lock(idr_lock);
  23969. +}
  23970. +
  23971. +static inline void idr_preload_unlock(void)
  23972. +{
  23973. + local_unlock(idr_lock);
  23974. +}
  23975. +
  23976. +void idr_preload_end(void)
  23977. +{
  23978. + idr_preload_unlock();
  23979. +}
  23980. +EXPORT_SYMBOL(idr_preload_end);
  23981. +#else
  23982. +static inline void idr_preload_lock(void)
  23983. +{
  23984. + preempt_disable();
  23985. +}
  23986. +
  23987. +static inline void idr_preload_unlock(void)
  23988. +{
  23989. + preempt_enable();
  23990. +}
  23991. +#endif
  23992. /**
  23993. * idr_preload - preload for idr_alloc()
  23994. @@ -401,7 +431,7 @@
  23995. WARN_ON_ONCE(in_interrupt());
  23996. might_sleep_if(gfp_mask & __GFP_WAIT);
  23997. - preempt_disable();
  23998. + idr_preload_lock();
  23999. /*
  24000. * idr_alloc() is likely to succeed w/o full idr_layer buffer and
  24001. @@ -413,9 +443,9 @@
  24002. while (__this_cpu_read(idr_preload_cnt) < MAX_IDR_FREE) {
  24003. struct idr_layer *new;
  24004. - preempt_enable();
  24005. + idr_preload_unlock();
  24006. new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
  24007. - preempt_disable();
  24008. + idr_preload_lock();
  24009. if (!new)
  24010. break;
  24011. diff -Nur linux-4.1.20.orig/lib/locking-selftest.c linux-4.1.20/lib/locking-selftest.c
  24012. --- linux-4.1.20.orig/lib/locking-selftest.c 2016-03-17 19:11:03.000000000 +0100
  24013. +++ linux-4.1.20/lib/locking-selftest.c 2016-03-21 20:18:34.000000000 +0100
  24014. @@ -590,6 +590,8 @@
  24015. #include "locking-selftest-spin-hardirq.h"
  24016. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
  24017. +#ifndef CONFIG_PREEMPT_RT_FULL
  24018. +
  24019. #include "locking-selftest-rlock-hardirq.h"
  24020. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
  24021. @@ -605,9 +607,12 @@
  24022. #include "locking-selftest-wlock-softirq.h"
  24023. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
  24024. +#endif
  24025. +
  24026. #undef E1
  24027. #undef E2
  24028. +#ifndef CONFIG_PREEMPT_RT_FULL
  24029. /*
  24030. * Enabling hardirqs with a softirq-safe lock held:
  24031. */
  24032. @@ -640,6 +645,8 @@
  24033. #undef E1
  24034. #undef E2
  24035. +#endif
  24036. +
  24037. /*
  24038. * Enabling irqs with an irq-safe lock held:
  24039. */
  24040. @@ -663,6 +670,8 @@
  24041. #include "locking-selftest-spin-hardirq.h"
  24042. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
  24043. +#ifndef CONFIG_PREEMPT_RT_FULL
  24044. +
  24045. #include "locking-selftest-rlock-hardirq.h"
  24046. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
  24047. @@ -678,6 +687,8 @@
  24048. #include "locking-selftest-wlock-softirq.h"
  24049. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
  24050. +#endif
  24051. +
  24052. #undef E1
  24053. #undef E2
  24054. @@ -709,6 +720,8 @@
  24055. #include "locking-selftest-spin-hardirq.h"
  24056. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
  24057. +#ifndef CONFIG_PREEMPT_RT_FULL
  24058. +
  24059. #include "locking-selftest-rlock-hardirq.h"
  24060. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
  24061. @@ -724,6 +737,8 @@
  24062. #include "locking-selftest-wlock-softirq.h"
  24063. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
  24064. +#endif
  24065. +
  24066. #undef E1
  24067. #undef E2
  24068. #undef E3
  24069. @@ -757,6 +772,8 @@
  24070. #include "locking-selftest-spin-hardirq.h"
  24071. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
  24072. +#ifndef CONFIG_PREEMPT_RT_FULL
  24073. +
  24074. #include "locking-selftest-rlock-hardirq.h"
  24075. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
  24076. @@ -772,10 +789,14 @@
  24077. #include "locking-selftest-wlock-softirq.h"
  24078. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
  24079. +#endif
  24080. +
  24081. #undef E1
  24082. #undef E2
  24083. #undef E3
  24084. +#ifndef CONFIG_PREEMPT_RT_FULL
  24085. +
  24086. /*
  24087. * read-lock / write-lock irq inversion.
  24088. *
  24089. @@ -838,6 +859,10 @@
  24090. #undef E2
  24091. #undef E3
  24092. +#endif
  24093. +
  24094. +#ifndef CONFIG_PREEMPT_RT_FULL
  24095. +
  24096. /*
  24097. * read-lock / write-lock recursion that is actually safe.
  24098. */
  24099. @@ -876,6 +901,8 @@
  24100. #undef E2
  24101. #undef E3
  24102. +#endif
  24103. +
  24104. /*
  24105. * read-lock / write-lock recursion that is unsafe.
  24106. */
  24107. @@ -1858,6 +1885,7 @@
  24108. printk(" --------------------------------------------------------------------------\n");
  24109. +#ifndef CONFIG_PREEMPT_RT_FULL
  24110. /*
  24111. * irq-context testcases:
  24112. */
  24113. @@ -1870,6 +1898,28 @@
  24114. DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
  24115. // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
  24116. +#else
  24117. + /* On -rt, we only do hardirq context test for raw spinlock */
  24118. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 12);
  24119. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 21);
  24120. +
  24121. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 12);
  24122. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 21);
  24123. +
  24124. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 123);
  24125. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 132);
  24126. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 213);
  24127. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 231);
  24128. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 312);
  24129. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 321);
  24130. +
  24131. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 123);
  24132. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 132);
  24133. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 213);
  24134. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 231);
  24135. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 312);
  24136. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 321);
  24137. +#endif
  24138. ww_tests();
  24139. diff -Nur linux-4.1.20.orig/lib/percpu_ida.c linux-4.1.20/lib/percpu_ida.c
  24140. --- linux-4.1.20.orig/lib/percpu_ida.c 2016-03-17 19:11:03.000000000 +0100
  24141. +++ linux-4.1.20/lib/percpu_ida.c 2016-03-21 20:18:34.000000000 +0100
  24142. @@ -26,6 +26,9 @@
  24143. #include <linux/string.h>
  24144. #include <linux/spinlock.h>
  24145. #include <linux/percpu_ida.h>
  24146. +#include <linux/locallock.h>
  24147. +
  24148. +static DEFINE_LOCAL_IRQ_LOCK(irq_off_lock);
  24149. struct percpu_ida_cpu {
  24150. /*
  24151. @@ -148,13 +151,13 @@
  24152. unsigned long flags;
  24153. int tag;
  24154. - local_irq_save(flags);
  24155. + local_lock_irqsave(irq_off_lock, flags);
  24156. tags = this_cpu_ptr(pool->tag_cpu);
  24157. /* Fastpath */
  24158. tag = alloc_local_tag(tags);
  24159. if (likely(tag >= 0)) {
  24160. - local_irq_restore(flags);
  24161. + local_unlock_irqrestore(irq_off_lock, flags);
  24162. return tag;
  24163. }
  24164. @@ -173,6 +176,7 @@
  24165. if (!tags->nr_free)
  24166. alloc_global_tags(pool, tags);
  24167. +
  24168. if (!tags->nr_free)
  24169. steal_tags(pool, tags);
  24170. @@ -184,7 +188,7 @@
  24171. }
  24172. spin_unlock(&pool->lock);
  24173. - local_irq_restore(flags);
  24174. + local_unlock_irqrestore(irq_off_lock, flags);
  24175. if (tag >= 0 || state == TASK_RUNNING)
  24176. break;
  24177. @@ -196,7 +200,7 @@
  24178. schedule();
  24179. - local_irq_save(flags);
  24180. + local_lock_irqsave(irq_off_lock, flags);
  24181. tags = this_cpu_ptr(pool->tag_cpu);
  24182. }
  24183. if (state != TASK_RUNNING)
  24184. @@ -221,7 +225,7 @@
  24185. BUG_ON(tag >= pool->nr_tags);
  24186. - local_irq_save(flags);
  24187. + local_lock_irqsave(irq_off_lock, flags);
  24188. tags = this_cpu_ptr(pool->tag_cpu);
  24189. spin_lock(&tags->lock);
  24190. @@ -253,7 +257,7 @@
  24191. spin_unlock(&pool->lock);
  24192. }
  24193. - local_irq_restore(flags);
  24194. + local_unlock_irqrestore(irq_off_lock, flags);
  24195. }
  24196. EXPORT_SYMBOL_GPL(percpu_ida_free);
  24197. @@ -345,7 +349,7 @@
  24198. struct percpu_ida_cpu *remote;
  24199. unsigned cpu, i, err = 0;
  24200. - local_irq_save(flags);
  24201. + local_lock_irqsave(irq_off_lock, flags);
  24202. for_each_possible_cpu(cpu) {
  24203. remote = per_cpu_ptr(pool->tag_cpu, cpu);
  24204. spin_lock(&remote->lock);
  24205. @@ -367,7 +371,7 @@
  24206. }
  24207. spin_unlock(&pool->lock);
  24208. out:
  24209. - local_irq_restore(flags);
  24210. + local_unlock_irqrestore(irq_off_lock, flags);
  24211. return err;
  24212. }
  24213. EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
  24214. diff -Nur linux-4.1.20.orig/lib/radix-tree.c linux-4.1.20/lib/radix-tree.c
  24215. --- linux-4.1.20.orig/lib/radix-tree.c 2016-03-17 19:11:03.000000000 +0100
  24216. +++ linux-4.1.20/lib/radix-tree.c 2016-03-21 20:18:34.000000000 +0100
  24217. @@ -195,12 +195,13 @@
  24218. * succeed in getting a node here (and never reach
  24219. * kmem_cache_alloc)
  24220. */
  24221. - rtp = this_cpu_ptr(&radix_tree_preloads);
  24222. + rtp = &get_cpu_var(radix_tree_preloads);
  24223. if (rtp->nr) {
  24224. ret = rtp->nodes[rtp->nr - 1];
  24225. rtp->nodes[rtp->nr - 1] = NULL;
  24226. rtp->nr--;
  24227. }
  24228. + put_cpu_var(radix_tree_preloads);
  24229. /*
  24230. * Update the allocation stack trace as this is more useful
  24231. * for debugging.
  24232. @@ -240,6 +241,7 @@
  24233. call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
  24234. }
  24235. +#ifndef CONFIG_PREEMPT_RT_FULL
  24236. /*
  24237. * Load up this CPU's radix_tree_node buffer with sufficient objects to
  24238. * ensure that the addition of a single element in the tree cannot fail. On
  24239. @@ -305,6 +307,7 @@
  24240. return 0;
  24241. }
  24242. EXPORT_SYMBOL(radix_tree_maybe_preload);
  24243. +#endif
  24244. /*
  24245. * Return the maximum key which can be store into a
  24246. diff -Nur linux-4.1.20.orig/lib/scatterlist.c linux-4.1.20/lib/scatterlist.c
  24247. --- linux-4.1.20.orig/lib/scatterlist.c 2016-03-17 19:11:03.000000000 +0100
  24248. +++ linux-4.1.20/lib/scatterlist.c 2016-03-21 20:18:34.000000000 +0100
  24249. @@ -592,7 +592,7 @@
  24250. flush_kernel_dcache_page(miter->page);
  24251. if (miter->__flags & SG_MITER_ATOMIC) {
  24252. - WARN_ON_ONCE(preemptible());
  24253. + WARN_ON_ONCE(!pagefault_disabled());
  24254. kunmap_atomic(miter->addr);
  24255. } else
  24256. kunmap(miter->page);
  24257. @@ -637,7 +637,7 @@
  24258. if (!sg_miter_skip(&miter, skip))
  24259. return false;
  24260. - local_irq_save(flags);
  24261. + local_irq_save_nort(flags);
  24262. while (sg_miter_next(&miter) && offset < buflen) {
  24263. unsigned int len;
  24264. @@ -654,7 +654,7 @@
  24265. sg_miter_stop(&miter);
  24266. - local_irq_restore(flags);
  24267. + local_irq_restore_nort(flags);
  24268. return offset;
  24269. }
  24270. diff -Nur linux-4.1.20.orig/lib/smp_processor_id.c linux-4.1.20/lib/smp_processor_id.c
  24271. --- linux-4.1.20.orig/lib/smp_processor_id.c 2016-03-17 19:11:03.000000000 +0100
  24272. +++ linux-4.1.20/lib/smp_processor_id.c 2016-03-21 20:18:34.000000000 +0100
  24273. @@ -39,8 +39,9 @@
  24274. if (!printk_ratelimit())
  24275. goto out_enable;
  24276. - printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
  24277. - what1, what2, preempt_count() - 1, current->comm, current->pid);
  24278. + printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x %08x] code: %s/%d\n",
  24279. + what1, what2, preempt_count() - 1, __migrate_disabled(current),
  24280. + current->comm, current->pid);
  24281. print_symbol("caller is %s\n", (long)__builtin_return_address(0));
  24282. dump_stack();
  24283. diff -Nur linux-4.1.20.orig/lib/strnlen_user.c linux-4.1.20/lib/strnlen_user.c
  24284. --- linux-4.1.20.orig/lib/strnlen_user.c 2016-03-17 19:11:03.000000000 +0100
  24285. +++ linux-4.1.20/lib/strnlen_user.c 2016-03-21 20:18:34.000000000 +0100
  24286. @@ -85,7 +85,8 @@
  24287. * @str: The string to measure.
  24288. * @count: Maximum count (including NUL character)
  24289. *
  24290. - * Context: User context only. This function may sleep.
  24291. + * Context: User context only. This function may sleep if pagefaults are
  24292. + * enabled.
  24293. *
  24294. * Get the size of a NUL-terminated string in user space.
  24295. *
  24296. @@ -121,7 +122,8 @@
  24297. * strlen_user: - Get the size of a user string INCLUDING final NUL.
  24298. * @str: The string to measure.
  24299. *
  24300. - * Context: User context only. This function may sleep.
  24301. + * Context: User context only. This function may sleep if pagefaults are
  24302. + * enabled.
  24303. *
  24304. * Get the size of a NUL-terminated string in user space.
  24305. *
  24306. diff -Nur linux-4.1.20.orig/mm/Kconfig linux-4.1.20/mm/Kconfig
  24307. --- linux-4.1.20.orig/mm/Kconfig 2016-03-17 19:11:03.000000000 +0100
  24308. +++ linux-4.1.20/mm/Kconfig 2016-03-21 20:18:34.000000000 +0100
  24309. @@ -409,7 +409,7 @@
  24310. config TRANSPARENT_HUGEPAGE
  24311. bool "Transparent Hugepage Support"
  24312. - depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE
  24313. + depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE && !PREEMPT_RT_FULL
  24314. select COMPACTION
  24315. help
  24316. Transparent Hugepages allows the kernel to use huge pages and
  24317. diff -Nur linux-4.1.20.orig/mm/compaction.c linux-4.1.20/mm/compaction.c
  24318. --- linux-4.1.20.orig/mm/compaction.c 2016-03-17 19:11:03.000000000 +0100
  24319. +++ linux-4.1.20/mm/compaction.c 2016-03-21 20:18:34.000000000 +0100
  24320. @@ -1406,10 +1406,12 @@
  24321. cc->migrate_pfn & ~((1UL << cc->order) - 1);
  24322. if (last_migrated_pfn < current_block_start) {
  24323. - cpu = get_cpu();
  24324. + cpu = get_cpu_light();
  24325. + local_lock_irq(swapvec_lock);
  24326. lru_add_drain_cpu(cpu);
  24327. + local_unlock_irq(swapvec_lock);
  24328. drain_local_pages(zone);
  24329. - put_cpu();
  24330. + put_cpu_light();
  24331. /* No more flushing until we migrate again */
  24332. last_migrated_pfn = 0;
  24333. }
  24334. diff -Nur linux-4.1.20.orig/mm/filemap.c linux-4.1.20/mm/filemap.c
  24335. --- linux-4.1.20.orig/mm/filemap.c 2016-03-17 19:11:03.000000000 +0100
  24336. +++ linux-4.1.20/mm/filemap.c 2016-03-21 20:18:34.000000000 +0100
  24337. @@ -167,7 +167,9 @@
  24338. if (!workingset_node_pages(node) &&
  24339. list_empty(&node->private_list)) {
  24340. node->private_data = mapping;
  24341. - list_lru_add(&workingset_shadow_nodes, &node->private_list);
  24342. + local_lock(workingset_shadow_lock);
  24343. + list_lru_add(&__workingset_shadow_nodes, &node->private_list);
  24344. + local_unlock(workingset_shadow_lock);
  24345. }
  24346. }
  24347. @@ -533,9 +535,12 @@
  24348. * node->private_list is protected by
  24349. * mapping->tree_lock.
  24350. */
  24351. - if (!list_empty(&node->private_list))
  24352. - list_lru_del(&workingset_shadow_nodes,
  24353. + if (!list_empty(&node->private_list)) {
  24354. + local_lock(workingset_shadow_lock);
  24355. + list_lru_del(&__workingset_shadow_nodes,
  24356. &node->private_list);
  24357. + local_unlock(workingset_shadow_lock);
  24358. + }
  24359. }
  24360. return 0;
  24361. }
  24362. diff -Nur linux-4.1.20.orig/mm/highmem.c linux-4.1.20/mm/highmem.c
  24363. --- linux-4.1.20.orig/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  24364. +++ linux-4.1.20/mm/highmem.c 2016-03-21 20:18:34.000000000 +0100
  24365. @@ -29,10 +29,11 @@
  24366. #include <linux/kgdb.h>
  24367. #include <asm/tlbflush.h>
  24368. -
  24369. +#ifndef CONFIG_PREEMPT_RT_FULL
  24370. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  24371. DEFINE_PER_CPU(int, __kmap_atomic_idx);
  24372. #endif
  24373. +#endif
  24374. /*
  24375. * Virtual_count is not a pure "count".
  24376. @@ -107,8 +108,9 @@
  24377. unsigned long totalhigh_pages __read_mostly;
  24378. EXPORT_SYMBOL(totalhigh_pages);
  24379. -
  24380. +#ifndef CONFIG_PREEMPT_RT_FULL
  24381. EXPORT_PER_CPU_SYMBOL(__kmap_atomic_idx);
  24382. +#endif
  24383. unsigned int nr_free_highpages (void)
  24384. {
  24385. diff -Nur linux-4.1.20.orig/mm/memcontrol.c linux-4.1.20/mm/memcontrol.c
  24386. --- linux-4.1.20.orig/mm/memcontrol.c 2016-03-17 19:11:03.000000000 +0100
  24387. +++ linux-4.1.20/mm/memcontrol.c 2016-03-21 20:18:34.000000000 +0100
  24388. @@ -66,6 +66,8 @@
  24389. #include <net/sock.h>
  24390. #include <net/ip.h>
  24391. #include <net/tcp_memcontrol.h>
  24392. +#include <linux/locallock.h>
  24393. +
  24394. #include "slab.h"
  24395. #include <asm/uaccess.h>
  24396. @@ -85,6 +87,7 @@
  24397. #define do_swap_account 0
  24398. #endif
  24399. +static DEFINE_LOCAL_IRQ_LOCK(event_lock);
  24400. static const char * const mem_cgroup_stat_names[] = {
  24401. "cache",
  24402. "rss",
  24403. @@ -2124,14 +2127,17 @@
  24404. */
  24405. static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
  24406. {
  24407. - struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
  24408. + struct memcg_stock_pcp *stock;
  24409. + int cpu = get_cpu_light();
  24410. +
  24411. + stock = &per_cpu(memcg_stock, cpu);
  24412. if (stock->cached != memcg) { /* reset if necessary */
  24413. drain_stock(stock);
  24414. stock->cached = memcg;
  24415. }
  24416. stock->nr_pages += nr_pages;
  24417. - put_cpu_var(memcg_stock);
  24418. + put_cpu_light();
  24419. }
  24420. /*
  24421. @@ -2147,7 +2153,7 @@
  24422. return;
  24423. /* Notify other cpus that system-wide "drain" is running */
  24424. get_online_cpus();
  24425. - curcpu = get_cpu();
  24426. + curcpu = get_cpu_light();
  24427. for_each_online_cpu(cpu) {
  24428. struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
  24429. struct mem_cgroup *memcg;
  24430. @@ -2164,7 +2170,7 @@
  24431. schedule_work_on(cpu, &stock->work);
  24432. }
  24433. }
  24434. - put_cpu();
  24435. + put_cpu_light();
  24436. put_online_cpus();
  24437. mutex_unlock(&percpu_charge_mutex);
  24438. }
  24439. @@ -4803,12 +4809,12 @@
  24440. ret = 0;
  24441. - local_irq_disable();
  24442. + local_lock_irq(event_lock);
  24443. mem_cgroup_charge_statistics(to, page, nr_pages);
  24444. memcg_check_events(to, page);
  24445. mem_cgroup_charge_statistics(from, page, -nr_pages);
  24446. memcg_check_events(from, page);
  24447. - local_irq_enable();
  24448. + local_unlock_irq(event_lock);
  24449. out_unlock:
  24450. unlock_page(page);
  24451. out:
  24452. @@ -5545,10 +5551,10 @@
  24453. VM_BUG_ON_PAGE(!PageTransHuge(page), page);
  24454. }
  24455. - local_irq_disable();
  24456. + local_lock_irq(event_lock);
  24457. mem_cgroup_charge_statistics(memcg, page, nr_pages);
  24458. memcg_check_events(memcg, page);
  24459. - local_irq_enable();
  24460. + local_unlock_irq(event_lock);
  24461. if (do_swap_account && PageSwapCache(page)) {
  24462. swp_entry_t entry = { .val = page_private(page) };
  24463. @@ -5604,14 +5610,14 @@
  24464. memcg_oom_recover(memcg);
  24465. }
  24466. - local_irq_save(flags);
  24467. + local_lock_irqsave(event_lock, flags);
  24468. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS], nr_anon);
  24469. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_CACHE], nr_file);
  24470. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], nr_huge);
  24471. __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT], pgpgout);
  24472. __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
  24473. memcg_check_events(memcg, dummy_page);
  24474. - local_irq_restore(flags);
  24475. + local_unlock_irqrestore(event_lock, flags);
  24476. if (!mem_cgroup_is_root(memcg))
  24477. css_put_many(&memcg->css, nr_pages);
  24478. @@ -5815,6 +5821,7 @@
  24479. {
  24480. struct mem_cgroup *memcg;
  24481. unsigned short oldid;
  24482. + unsigned long flags;
  24483. VM_BUG_ON_PAGE(PageLRU(page), page);
  24484. VM_BUG_ON_PAGE(page_count(page), page);
  24485. @@ -5837,9 +5844,11 @@
  24486. if (!mem_cgroup_is_root(memcg))
  24487. page_counter_uncharge(&memcg->memory, 1);
  24488. + local_lock_irqsave(event_lock, flags);
  24489. /* Caller disabled preemption with mapping->tree_lock */
  24490. mem_cgroup_charge_statistics(memcg, page, -1);
  24491. memcg_check_events(memcg, page);
  24492. + local_unlock_irqrestore(event_lock, flags);
  24493. }
  24494. /**
  24495. diff -Nur linux-4.1.20.orig/mm/memory.c linux-4.1.20/mm/memory.c
  24496. --- linux-4.1.20.orig/mm/memory.c 2016-03-17 19:11:03.000000000 +0100
  24497. +++ linux-4.1.20/mm/memory.c 2016-03-21 20:18:34.000000000 +0100
  24498. @@ -3753,7 +3753,7 @@
  24499. }
  24500. #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
  24501. -void might_fault(void)
  24502. +void __might_fault(const char *file, int line)
  24503. {
  24504. /*
  24505. * Some code (nfs/sunrpc) uses socket ops on kernel memory while
  24506. @@ -3763,21 +3763,15 @@
  24507. */
  24508. if (segment_eq(get_fs(), KERNEL_DS))
  24509. return;
  24510. -
  24511. - /*
  24512. - * it would be nicer only to annotate paths which are not under
  24513. - * pagefault_disable, however that requires a larger audit and
  24514. - * providing helpers like get_user_atomic.
  24515. - */
  24516. - if (in_atomic())
  24517. + if (pagefault_disabled())
  24518. return;
  24519. -
  24520. - __might_sleep(__FILE__, __LINE__, 0);
  24521. -
  24522. + __might_sleep(file, line, 0);
  24523. +#if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
  24524. if (current->mm)
  24525. might_lock_read(&current->mm->mmap_sem);
  24526. +#endif
  24527. }
  24528. -EXPORT_SYMBOL(might_fault);
  24529. +EXPORT_SYMBOL(__might_fault);
  24530. #endif
  24531. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
  24532. diff -Nur linux-4.1.20.orig/mm/mmu_context.c linux-4.1.20/mm/mmu_context.c
  24533. --- linux-4.1.20.orig/mm/mmu_context.c 2016-03-17 19:11:03.000000000 +0100
  24534. +++ linux-4.1.20/mm/mmu_context.c 2016-03-21 20:18:34.000000000 +0100
  24535. @@ -23,6 +23,7 @@
  24536. struct task_struct *tsk = current;
  24537. task_lock(tsk);
  24538. + preempt_disable_rt();
  24539. active_mm = tsk->active_mm;
  24540. if (active_mm != mm) {
  24541. atomic_inc(&mm->mm_count);
  24542. @@ -30,6 +31,7 @@
  24543. }
  24544. tsk->mm = mm;
  24545. switch_mm(active_mm, mm, tsk);
  24546. + preempt_enable_rt();
  24547. task_unlock(tsk);
  24548. #ifdef finish_arch_post_lock_switch
  24549. finish_arch_post_lock_switch();
  24550. diff -Nur linux-4.1.20.orig/mm/page_alloc.c linux-4.1.20/mm/page_alloc.c
  24551. --- linux-4.1.20.orig/mm/page_alloc.c 2016-03-17 19:11:03.000000000 +0100
  24552. +++ linux-4.1.20/mm/page_alloc.c 2016-03-21 20:18:34.000000000 +0100
  24553. @@ -60,6 +60,7 @@
  24554. #include <linux/page_ext.h>
  24555. #include <linux/hugetlb.h>
  24556. #include <linux/sched/rt.h>
  24557. +#include <linux/locallock.h>
  24558. #include <linux/page_owner.h>
  24559. #include <asm/sections.h>
  24560. @@ -233,6 +234,18 @@
  24561. EXPORT_SYMBOL(nr_online_nodes);
  24562. #endif
  24563. +static DEFINE_LOCAL_IRQ_LOCK(pa_lock);
  24564. +
  24565. +#ifdef CONFIG_PREEMPT_RT_BASE
  24566. +# define cpu_lock_irqsave(cpu, flags) \
  24567. + local_lock_irqsave_on(pa_lock, flags, cpu)
  24568. +# define cpu_unlock_irqrestore(cpu, flags) \
  24569. + local_unlock_irqrestore_on(pa_lock, flags, cpu)
  24570. +#else
  24571. +# define cpu_lock_irqsave(cpu, flags) local_irq_save(flags)
  24572. +# define cpu_unlock_irqrestore(cpu, flags) local_irq_restore(flags)
  24573. +#endif
  24574. +
  24575. int page_group_by_mobility_disabled __read_mostly;
  24576. void set_pageblock_migratetype(struct page *page, int migratetype)
  24577. @@ -681,7 +694,7 @@
  24578. }
  24579. /*
  24580. - * Frees a number of pages from the PCP lists
  24581. + * Frees a number of pages which have been collected from the pcp lists.
  24582. * Assumes all pages on list are in same zone, and of same order.
  24583. * count is the number of pages to free.
  24584. *
  24585. @@ -692,18 +705,51 @@
  24586. * pinned" detection logic.
  24587. */
  24588. static void free_pcppages_bulk(struct zone *zone, int count,
  24589. - struct per_cpu_pages *pcp)
  24590. + struct list_head *list)
  24591. {
  24592. - int migratetype = 0;
  24593. - int batch_free = 0;
  24594. int to_free = count;
  24595. unsigned long nr_scanned;
  24596. + unsigned long flags;
  24597. +
  24598. + spin_lock_irqsave(&zone->lock, flags);
  24599. - spin_lock(&zone->lock);
  24600. nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
  24601. if (nr_scanned)
  24602. __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
  24603. + while (!list_empty(list)) {
  24604. + struct page *page = list_first_entry(list, struct page, lru);
  24605. + int mt; /* migratetype of the to-be-freed page */
  24606. +
  24607. + /* must delete as __free_one_page list manipulates */
  24608. + list_del(&page->lru);
  24609. +
  24610. + mt = get_freepage_migratetype(page);
  24611. + if (unlikely(has_isolate_pageblock(zone)))
  24612. + mt = get_pageblock_migratetype(page);
  24613. +
  24614. + /* MIGRATE_MOVABLE list may include MIGRATE_RESERVEs */
  24615. + __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  24616. + trace_mm_page_pcpu_drain(page, 0, mt);
  24617. + to_free--;
  24618. + }
  24619. + WARN_ON(to_free != 0);
  24620. + spin_unlock_irqrestore(&zone->lock, flags);
  24621. +}
  24622. +
  24623. +/*
  24624. + * Moves a number of pages from the PCP lists to free list which
  24625. + * is freed outside of the locked region.
  24626. + *
  24627. + * Assumes all pages on list are in same zone, and of same order.
  24628. + * count is the number of pages to free.
  24629. + */
  24630. +static void isolate_pcp_pages(int to_free, struct per_cpu_pages *src,
  24631. + struct list_head *dst)
  24632. +{
  24633. + int migratetype = 0;
  24634. + int batch_free = 0;
  24635. +
  24636. while (to_free) {
  24637. struct page *page;
  24638. struct list_head *list;
  24639. @@ -719,7 +765,7 @@
  24640. batch_free++;
  24641. if (++migratetype == MIGRATE_PCPTYPES)
  24642. migratetype = 0;
  24643. - list = &pcp->lists[migratetype];
  24644. + list = &src->lists[migratetype];
  24645. } while (list_empty(list));
  24646. /* This is the only non-empty list. Free them all. */
  24647. @@ -727,21 +773,11 @@
  24648. batch_free = to_free;
  24649. do {
  24650. - int mt; /* migratetype of the to-be-freed page */
  24651. -
  24652. - page = list_entry(list->prev, struct page, lru);
  24653. - /* must delete as __free_one_page list manipulates */
  24654. + page = list_last_entry(list, struct page, lru);
  24655. list_del(&page->lru);
  24656. - mt = get_freepage_migratetype(page);
  24657. - if (unlikely(has_isolate_pageblock(zone)))
  24658. - mt = get_pageblock_migratetype(page);
  24659. -
  24660. - /* MIGRATE_MOVABLE list may include MIGRATE_RESERVEs */
  24661. - __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  24662. - trace_mm_page_pcpu_drain(page, 0, mt);
  24663. + list_add(&page->lru, dst);
  24664. } while (--to_free && --batch_free && !list_empty(list));
  24665. }
  24666. - spin_unlock(&zone->lock);
  24667. }
  24668. static void free_one_page(struct zone *zone,
  24669. @@ -750,7 +786,9 @@
  24670. int migratetype)
  24671. {
  24672. unsigned long nr_scanned;
  24673. - spin_lock(&zone->lock);
  24674. + unsigned long flags;
  24675. +
  24676. + spin_lock_irqsave(&zone->lock, flags);
  24677. nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
  24678. if (nr_scanned)
  24679. __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
  24680. @@ -760,7 +798,7 @@
  24681. migratetype = get_pfnblock_migratetype(page, pfn);
  24682. }
  24683. __free_one_page(page, pfn, zone, order, migratetype);
  24684. - spin_unlock(&zone->lock);
  24685. + spin_unlock_irqrestore(&zone->lock, flags);
  24686. }
  24687. static int free_tail_pages_check(struct page *head_page, struct page *page)
  24688. @@ -825,11 +863,11 @@
  24689. return;
  24690. migratetype = get_pfnblock_migratetype(page, pfn);
  24691. - local_irq_save(flags);
  24692. + local_lock_irqsave(pa_lock, flags);
  24693. __count_vm_events(PGFREE, 1 << order);
  24694. set_freepage_migratetype(page, migratetype);
  24695. free_one_page(page_zone(page), page, pfn, order, migratetype);
  24696. - local_irq_restore(flags);
  24697. + local_unlock_irqrestore(pa_lock, flags);
  24698. }
  24699. void __init __free_pages_bootmem(struct page *page, unsigned int order)
  24700. @@ -1371,16 +1409,18 @@
  24701. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
  24702. {
  24703. unsigned long flags;
  24704. + LIST_HEAD(dst);
  24705. int to_drain, batch;
  24706. - local_irq_save(flags);
  24707. + local_lock_irqsave(pa_lock, flags);
  24708. batch = READ_ONCE(pcp->batch);
  24709. to_drain = min(pcp->count, batch);
  24710. if (to_drain > 0) {
  24711. - free_pcppages_bulk(zone, to_drain, pcp);
  24712. + isolate_pcp_pages(to_drain, pcp, &dst);
  24713. pcp->count -= to_drain;
  24714. }
  24715. - local_irq_restore(flags);
  24716. + local_unlock_irqrestore(pa_lock, flags);
  24717. + free_pcppages_bulk(zone, to_drain, &dst);
  24718. }
  24719. #endif
  24720. @@ -1396,16 +1436,21 @@
  24721. unsigned long flags;
  24722. struct per_cpu_pageset *pset;
  24723. struct per_cpu_pages *pcp;
  24724. + LIST_HEAD(dst);
  24725. + int count;
  24726. - local_irq_save(flags);
  24727. + cpu_lock_irqsave(cpu, flags);
  24728. pset = per_cpu_ptr(zone->pageset, cpu);
  24729. pcp = &pset->pcp;
  24730. - if (pcp->count) {
  24731. - free_pcppages_bulk(zone, pcp->count, pcp);
  24732. + count = pcp->count;
  24733. + if (count) {
  24734. + isolate_pcp_pages(count, pcp, &dst);
  24735. pcp->count = 0;
  24736. }
  24737. - local_irq_restore(flags);
  24738. + cpu_unlock_irqrestore(cpu, flags);
  24739. + if (count)
  24740. + free_pcppages_bulk(zone, count, &dst);
  24741. }
  24742. /*
  24743. @@ -1491,8 +1536,17 @@
  24744. else
  24745. cpumask_clear_cpu(cpu, &cpus_with_pcps);
  24746. }
  24747. +#ifndef CONFIG_PREEMPT_RT_BASE
  24748. on_each_cpu_mask(&cpus_with_pcps, (smp_call_func_t) drain_local_pages,
  24749. zone, 1);
  24750. +#else
  24751. + for_each_cpu(cpu, &cpus_with_pcps) {
  24752. + if (zone)
  24753. + drain_pages_zone(cpu, zone);
  24754. + else
  24755. + drain_pages(cpu);
  24756. + }
  24757. +#endif
  24758. }
  24759. #ifdef CONFIG_HIBERNATION
  24760. @@ -1548,7 +1602,7 @@
  24761. migratetype = get_pfnblock_migratetype(page, pfn);
  24762. set_freepage_migratetype(page, migratetype);
  24763. - local_irq_save(flags);
  24764. + local_lock_irqsave(pa_lock, flags);
  24765. __count_vm_event(PGFREE);
  24766. /*
  24767. @@ -1574,12 +1628,17 @@
  24768. pcp->count++;
  24769. if (pcp->count >= pcp->high) {
  24770. unsigned long batch = READ_ONCE(pcp->batch);
  24771. - free_pcppages_bulk(zone, batch, pcp);
  24772. + LIST_HEAD(dst);
  24773. +
  24774. + isolate_pcp_pages(batch, pcp, &dst);
  24775. pcp->count -= batch;
  24776. + local_unlock_irqrestore(pa_lock, flags);
  24777. + free_pcppages_bulk(zone, batch, &dst);
  24778. + return;
  24779. }
  24780. out:
  24781. - local_irq_restore(flags);
  24782. + local_unlock_irqrestore(pa_lock, flags);
  24783. }
  24784. /*
  24785. @@ -1710,7 +1769,7 @@
  24786. struct per_cpu_pages *pcp;
  24787. struct list_head *list;
  24788. - local_irq_save(flags);
  24789. + local_lock_irqsave(pa_lock, flags);
  24790. pcp = &this_cpu_ptr(zone->pageset)->pcp;
  24791. list = &pcp->lists[migratetype];
  24792. if (list_empty(list)) {
  24793. @@ -1742,13 +1801,15 @@
  24794. */
  24795. WARN_ON_ONCE(order > 1);
  24796. }
  24797. - spin_lock_irqsave(&zone->lock, flags);
  24798. + local_spin_lock_irqsave(pa_lock, &zone->lock, flags);
  24799. page = __rmqueue(zone, order, migratetype);
  24800. - spin_unlock(&zone->lock);
  24801. - if (!page)
  24802. + if (!page) {
  24803. + spin_unlock(&zone->lock);
  24804. goto failed;
  24805. + }
  24806. __mod_zone_freepage_state(zone, -(1 << order),
  24807. get_freepage_migratetype(page));
  24808. + spin_unlock(&zone->lock);
  24809. }
  24810. __mod_zone_page_state(zone, NR_ALLOC_BATCH, -(1 << order));
  24811. @@ -1758,13 +1819,13 @@
  24812. __count_zone_vm_events(PGALLOC, zone, 1 << order);
  24813. zone_statistics(preferred_zone, zone, gfp_flags);
  24814. - local_irq_restore(flags);
  24815. + local_unlock_irqrestore(pa_lock, flags);
  24816. VM_BUG_ON_PAGE(bad_range(zone, page), page);
  24817. return page;
  24818. failed:
  24819. - local_irq_restore(flags);
  24820. + local_unlock_irqrestore(pa_lock, flags);
  24821. return NULL;
  24822. }
  24823. @@ -5653,6 +5714,7 @@
  24824. void __init page_alloc_init(void)
  24825. {
  24826. hotcpu_notifier(page_alloc_cpu_notify, 0);
  24827. + local_irq_lock_init(pa_lock);
  24828. }
  24829. /*
  24830. @@ -6547,7 +6609,7 @@
  24831. struct per_cpu_pageset *pset;
  24832. /* avoid races with drain_pages() */
  24833. - local_irq_save(flags);
  24834. + local_lock_irqsave(pa_lock, flags);
  24835. if (zone->pageset != &boot_pageset) {
  24836. for_each_online_cpu(cpu) {
  24837. pset = per_cpu_ptr(zone->pageset, cpu);
  24838. @@ -6556,7 +6618,7 @@
  24839. free_percpu(zone->pageset);
  24840. zone->pageset = &boot_pageset;
  24841. }
  24842. - local_irq_restore(flags);
  24843. + local_unlock_irqrestore(pa_lock, flags);
  24844. }
  24845. #ifdef CONFIG_MEMORY_HOTREMOVE
  24846. diff -Nur linux-4.1.20.orig/mm/slab.h linux-4.1.20/mm/slab.h
  24847. --- linux-4.1.20.orig/mm/slab.h 2016-03-17 19:11:03.000000000 +0100
  24848. +++ linux-4.1.20/mm/slab.h 2016-03-21 20:18:34.000000000 +0100
  24849. @@ -330,7 +330,11 @@
  24850. * The slab lists for all objects.
  24851. */
  24852. struct kmem_cache_node {
  24853. +#ifdef CONFIG_SLUB
  24854. + raw_spinlock_t list_lock;
  24855. +#else
  24856. spinlock_t list_lock;
  24857. +#endif
  24858. #ifdef CONFIG_SLAB
  24859. struct list_head slabs_partial; /* partial list first, better asm code */
  24860. diff -Nur linux-4.1.20.orig/mm/slub.c linux-4.1.20/mm/slub.c
  24861. --- linux-4.1.20.orig/mm/slub.c 2016-03-17 19:11:03.000000000 +0100
  24862. +++ linux-4.1.20/mm/slub.c 2016-03-21 20:18:34.000000000 +0100
  24863. @@ -1069,7 +1069,7 @@
  24864. {
  24865. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  24866. - spin_lock_irqsave(&n->list_lock, *flags);
  24867. + raw_spin_lock_irqsave(&n->list_lock, *flags);
  24868. slab_lock(page);
  24869. if (!check_slab(s, page))
  24870. @@ -1116,7 +1116,7 @@
  24871. fail:
  24872. slab_unlock(page);
  24873. - spin_unlock_irqrestore(&n->list_lock, *flags);
  24874. + raw_spin_unlock_irqrestore(&n->list_lock, *flags);
  24875. slab_fix(s, "Object at 0x%p not freed", object);
  24876. return NULL;
  24877. }
  24878. @@ -1242,6 +1242,12 @@
  24879. #endif /* CONFIG_SLUB_DEBUG */
  24880. +struct slub_free_list {
  24881. + raw_spinlock_t lock;
  24882. + struct list_head list;
  24883. +};
  24884. +static DEFINE_PER_CPU(struct slub_free_list, slub_free_list);
  24885. +
  24886. /*
  24887. * Hooks for other subsystems that check memory allocations. In a typical
  24888. * production configuration these hooks all should produce no code at all.
  24889. @@ -1306,6 +1312,17 @@
  24890. kasan_slab_free(s, x);
  24891. }
  24892. +static void setup_object(struct kmem_cache *s, struct page *page,
  24893. + void *object)
  24894. +{
  24895. + setup_object_debug(s, page, object);
  24896. + if (unlikely(s->ctor)) {
  24897. + kasan_unpoison_object_data(s, object);
  24898. + s->ctor(object);
  24899. + kasan_poison_object_data(s, object);
  24900. + }
  24901. +}
  24902. +
  24903. /*
  24904. * Slab allocation and freeing
  24905. */
  24906. @@ -1336,10 +1353,17 @@
  24907. struct page *page;
  24908. struct kmem_cache_order_objects oo = s->oo;
  24909. gfp_t alloc_gfp;
  24910. + void *start, *p;
  24911. + int idx, order;
  24912. + bool enableirqs;
  24913. flags &= gfp_allowed_mask;
  24914. - if (flags & __GFP_WAIT)
  24915. + enableirqs = (flags & __GFP_WAIT) != 0;
  24916. +#ifdef CONFIG_PREEMPT_RT_FULL
  24917. + enableirqs |= system_state == SYSTEM_RUNNING;
  24918. +#endif
  24919. + if (enableirqs)
  24920. local_irq_enable();
  24921. flags |= s->allocflags;
  24922. @@ -1359,13 +1383,13 @@
  24923. * Try a lower order alloc if possible
  24924. */
  24925. page = alloc_slab_page(s, alloc_gfp, node, oo);
  24926. -
  24927. - if (page)
  24928. - stat(s, ORDER_FALLBACK);
  24929. + if (unlikely(!page))
  24930. + goto out;
  24931. + stat(s, ORDER_FALLBACK);
  24932. }
  24933. - if (kmemcheck_enabled && page
  24934. - && !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
  24935. + if (kmemcheck_enabled &&
  24936. + !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
  24937. int pages = 1 << oo_order(oo);
  24938. kmemcheck_alloc_shadow(page, oo_order(oo), alloc_gfp, node);
  24939. @@ -1380,51 +1404,9 @@
  24940. kmemcheck_mark_unallocated_pages(page, pages);
  24941. }
  24942. - if (flags & __GFP_WAIT)
  24943. - local_irq_disable();
  24944. - if (!page)
  24945. - return NULL;
  24946. -
  24947. page->objects = oo_objects(oo);
  24948. - mod_zone_page_state(page_zone(page),
  24949. - (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  24950. - NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  24951. - 1 << oo_order(oo));
  24952. -
  24953. - return page;
  24954. -}
  24955. -
  24956. -static void setup_object(struct kmem_cache *s, struct page *page,
  24957. - void *object)
  24958. -{
  24959. - setup_object_debug(s, page, object);
  24960. - if (unlikely(s->ctor)) {
  24961. - kasan_unpoison_object_data(s, object);
  24962. - s->ctor(object);
  24963. - kasan_poison_object_data(s, object);
  24964. - }
  24965. -}
  24966. -
  24967. -static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
  24968. -{
  24969. - struct page *page;
  24970. - void *start;
  24971. - void *p;
  24972. - int order;
  24973. - int idx;
  24974. -
  24975. - if (unlikely(flags & GFP_SLAB_BUG_MASK)) {
  24976. - pr_emerg("gfp: %u\n", flags & GFP_SLAB_BUG_MASK);
  24977. - BUG();
  24978. - }
  24979. -
  24980. - page = allocate_slab(s,
  24981. - flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
  24982. - if (!page)
  24983. - goto out;
  24984. order = compound_order(page);
  24985. - inc_slabs_node(s, page_to_nid(page), page->objects);
  24986. page->slab_cache = s;
  24987. __SetPageSlab(page);
  24988. if (page_is_pfmemalloc(page))
  24989. @@ -1448,10 +1430,34 @@
  24990. page->freelist = start;
  24991. page->inuse = page->objects;
  24992. page->frozen = 1;
  24993. +
  24994. out:
  24995. + if (enableirqs)
  24996. + local_irq_disable();
  24997. + if (!page)
  24998. + return NULL;
  24999. +
  25000. + mod_zone_page_state(page_zone(page),
  25001. + (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  25002. + NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  25003. + 1 << oo_order(oo));
  25004. +
  25005. + inc_slabs_node(s, page_to_nid(page), page->objects);
  25006. +
  25007. return page;
  25008. }
  25009. +static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
  25010. +{
  25011. + if (unlikely(flags & GFP_SLAB_BUG_MASK)) {
  25012. + pr_emerg("gfp: %u\n", flags & GFP_SLAB_BUG_MASK);
  25013. + BUG();
  25014. + }
  25015. +
  25016. + return allocate_slab(s,
  25017. + flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
  25018. +}
  25019. +
  25020. static void __free_slab(struct kmem_cache *s, struct page *page)
  25021. {
  25022. int order = compound_order(page);
  25023. @@ -1483,6 +1489,16 @@
  25024. memcg_uncharge_slab(s, order);
  25025. }
  25026. +static void free_delayed(struct list_head *h)
  25027. +{
  25028. + while(!list_empty(h)) {
  25029. + struct page *page = list_first_entry(h, struct page, lru);
  25030. +
  25031. + list_del(&page->lru);
  25032. + __free_slab(page->slab_cache, page);
  25033. + }
  25034. +}
  25035. +
  25036. #define need_reserve_slab_rcu \
  25037. (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
  25038. @@ -1517,6 +1533,12 @@
  25039. }
  25040. call_rcu(head, rcu_free_slab);
  25041. + } else if (irqs_disabled()) {
  25042. + struct slub_free_list *f = this_cpu_ptr(&slub_free_list);
  25043. +
  25044. + raw_spin_lock(&f->lock);
  25045. + list_add(&page->lru, &f->list);
  25046. + raw_spin_unlock(&f->lock);
  25047. } else
  25048. __free_slab(s, page);
  25049. }
  25050. @@ -1630,7 +1652,7 @@
  25051. if (!n || !n->nr_partial)
  25052. return NULL;
  25053. - spin_lock(&n->list_lock);
  25054. + raw_spin_lock(&n->list_lock);
  25055. list_for_each_entry_safe(page, page2, &n->partial, lru) {
  25056. void *t;
  25057. @@ -1655,7 +1677,7 @@
  25058. break;
  25059. }
  25060. - spin_unlock(&n->list_lock);
  25061. + raw_spin_unlock(&n->list_lock);
  25062. return object;
  25063. }
  25064. @@ -1901,7 +1923,7 @@
  25065. * that acquire_slab() will see a slab page that
  25066. * is frozen
  25067. */
  25068. - spin_lock(&n->list_lock);
  25069. + raw_spin_lock(&n->list_lock);
  25070. }
  25071. } else {
  25072. m = M_FULL;
  25073. @@ -1912,7 +1934,7 @@
  25074. * slabs from diagnostic functions will not see
  25075. * any frozen slabs.
  25076. */
  25077. - spin_lock(&n->list_lock);
  25078. + raw_spin_lock(&n->list_lock);
  25079. }
  25080. }
  25081. @@ -1947,7 +1969,7 @@
  25082. goto redo;
  25083. if (lock)
  25084. - spin_unlock(&n->list_lock);
  25085. + raw_spin_unlock(&n->list_lock);
  25086. if (m == M_FREE) {
  25087. stat(s, DEACTIVATE_EMPTY);
  25088. @@ -1979,10 +2001,10 @@
  25089. n2 = get_node(s, page_to_nid(page));
  25090. if (n != n2) {
  25091. if (n)
  25092. - spin_unlock(&n->list_lock);
  25093. + raw_spin_unlock(&n->list_lock);
  25094. n = n2;
  25095. - spin_lock(&n->list_lock);
  25096. + raw_spin_lock(&n->list_lock);
  25097. }
  25098. do {
  25099. @@ -2011,7 +2033,7 @@
  25100. }
  25101. if (n)
  25102. - spin_unlock(&n->list_lock);
  25103. + raw_spin_unlock(&n->list_lock);
  25104. while (discard_page) {
  25105. page = discard_page;
  25106. @@ -2050,14 +2072,21 @@
  25107. pobjects = oldpage->pobjects;
  25108. pages = oldpage->pages;
  25109. if (drain && pobjects > s->cpu_partial) {
  25110. + struct slub_free_list *f;
  25111. unsigned long flags;
  25112. + LIST_HEAD(tofree);
  25113. /*
  25114. * partial array is full. Move the existing
  25115. * set to the per node partial list.
  25116. */
  25117. local_irq_save(flags);
  25118. unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
  25119. + f = this_cpu_ptr(&slub_free_list);
  25120. + raw_spin_lock(&f->lock);
  25121. + list_splice_init(&f->list, &tofree);
  25122. + raw_spin_unlock(&f->lock);
  25123. local_irq_restore(flags);
  25124. + free_delayed(&tofree);
  25125. oldpage = NULL;
  25126. pobjects = 0;
  25127. pages = 0;
  25128. @@ -2129,7 +2158,22 @@
  25129. static void flush_all(struct kmem_cache *s)
  25130. {
  25131. + LIST_HEAD(tofree);
  25132. + int cpu;
  25133. +
  25134. on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1, GFP_ATOMIC);
  25135. + for_each_online_cpu(cpu) {
  25136. + struct slub_free_list *f;
  25137. +
  25138. + if (!has_cpu_slab(cpu, s))
  25139. + continue;
  25140. +
  25141. + f = &per_cpu(slub_free_list, cpu);
  25142. + raw_spin_lock_irq(&f->lock);
  25143. + list_splice_init(&f->list, &tofree);
  25144. + raw_spin_unlock_irq(&f->lock);
  25145. + free_delayed(&tofree);
  25146. + }
  25147. }
  25148. /*
  25149. @@ -2165,10 +2209,10 @@
  25150. unsigned long x = 0;
  25151. struct page *page;
  25152. - spin_lock_irqsave(&n->list_lock, flags);
  25153. + raw_spin_lock_irqsave(&n->list_lock, flags);
  25154. list_for_each_entry(page, &n->partial, lru)
  25155. x += get_count(page);
  25156. - spin_unlock_irqrestore(&n->list_lock, flags);
  25157. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  25158. return x;
  25159. }
  25160. #endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
  25161. @@ -2305,9 +2349,11 @@
  25162. static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  25163. unsigned long addr, struct kmem_cache_cpu *c)
  25164. {
  25165. + struct slub_free_list *f;
  25166. void *freelist;
  25167. struct page *page;
  25168. unsigned long flags;
  25169. + LIST_HEAD(tofree);
  25170. local_irq_save(flags);
  25171. #ifdef CONFIG_PREEMPT
  25172. @@ -2375,7 +2421,13 @@
  25173. VM_BUG_ON(!c->page->frozen);
  25174. c->freelist = get_freepointer(s, freelist);
  25175. c->tid = next_tid(c->tid);
  25176. +out:
  25177. + f = this_cpu_ptr(&slub_free_list);
  25178. + raw_spin_lock(&f->lock);
  25179. + list_splice_init(&f->list, &tofree);
  25180. + raw_spin_unlock(&f->lock);
  25181. local_irq_restore(flags);
  25182. + free_delayed(&tofree);
  25183. return freelist;
  25184. new_slab:
  25185. @@ -2392,8 +2444,7 @@
  25186. if (unlikely(!freelist)) {
  25187. slab_out_of_memory(s, gfpflags, node);
  25188. - local_irq_restore(flags);
  25189. - return NULL;
  25190. + goto out;
  25191. }
  25192. page = c->page;
  25193. @@ -2408,8 +2459,7 @@
  25194. deactivate_slab(s, page, get_freepointer(s, freelist));
  25195. c->page = NULL;
  25196. c->freelist = NULL;
  25197. - local_irq_restore(flags);
  25198. - return freelist;
  25199. + goto out;
  25200. }
  25201. /*
  25202. @@ -2593,7 +2643,7 @@
  25203. do {
  25204. if (unlikely(n)) {
  25205. - spin_unlock_irqrestore(&n->list_lock, flags);
  25206. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  25207. n = NULL;
  25208. }
  25209. prior = page->freelist;
  25210. @@ -2625,7 +2675,7 @@
  25211. * Otherwise the list_lock will synchronize with
  25212. * other processors updating the list of slabs.
  25213. */
  25214. - spin_lock_irqsave(&n->list_lock, flags);
  25215. + raw_spin_lock_irqsave(&n->list_lock, flags);
  25216. }
  25217. }
  25218. @@ -2667,7 +2717,7 @@
  25219. add_partial(n, page, DEACTIVATE_TO_TAIL);
  25220. stat(s, FREE_ADD_PARTIAL);
  25221. }
  25222. - spin_unlock_irqrestore(&n->list_lock, flags);
  25223. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  25224. return;
  25225. slab_empty:
  25226. @@ -2682,7 +2732,7 @@
  25227. remove_full(s, n, page);
  25228. }
  25229. - spin_unlock_irqrestore(&n->list_lock, flags);
  25230. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  25231. stat(s, FREE_SLAB);
  25232. discard_slab(s, page);
  25233. }
  25234. @@ -2881,7 +2931,7 @@
  25235. init_kmem_cache_node(struct kmem_cache_node *n)
  25236. {
  25237. n->nr_partial = 0;
  25238. - spin_lock_init(&n->list_lock);
  25239. + raw_spin_lock_init(&n->list_lock);
  25240. INIT_LIST_HEAD(&n->partial);
  25241. #ifdef CONFIG_SLUB_DEBUG
  25242. atomic_long_set(&n->nr_slabs, 0);
  25243. @@ -3463,7 +3513,7 @@
  25244. for (i = 0; i < SHRINK_PROMOTE_MAX; i++)
  25245. INIT_LIST_HEAD(promote + i);
  25246. - spin_lock_irqsave(&n->list_lock, flags);
  25247. + raw_spin_lock_irqsave(&n->list_lock, flags);
  25248. /*
  25249. * Build lists of slabs to discard or promote.
  25250. @@ -3494,7 +3544,7 @@
  25251. for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--)
  25252. list_splice(promote + i, &n->partial);
  25253. - spin_unlock_irqrestore(&n->list_lock, flags);
  25254. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  25255. /* Release empty slabs */
  25256. list_for_each_entry_safe(page, t, &discard, lru)
  25257. @@ -3670,6 +3720,12 @@
  25258. {
  25259. static __initdata struct kmem_cache boot_kmem_cache,
  25260. boot_kmem_cache_node;
  25261. + int cpu;
  25262. +
  25263. + for_each_possible_cpu(cpu) {
  25264. + raw_spin_lock_init(&per_cpu(slub_free_list, cpu).lock);
  25265. + INIT_LIST_HEAD(&per_cpu(slub_free_list, cpu).list);
  25266. + }
  25267. if (debug_guardpage_minorder())
  25268. slub_max_order = 0;
  25269. @@ -3912,7 +3968,7 @@
  25270. struct page *page;
  25271. unsigned long flags;
  25272. - spin_lock_irqsave(&n->list_lock, flags);
  25273. + raw_spin_lock_irqsave(&n->list_lock, flags);
  25274. list_for_each_entry(page, &n->partial, lru) {
  25275. validate_slab_slab(s, page, map);
  25276. @@ -3934,7 +3990,7 @@
  25277. s->name, count, atomic_long_read(&n->nr_slabs));
  25278. out:
  25279. - spin_unlock_irqrestore(&n->list_lock, flags);
  25280. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  25281. return count;
  25282. }
  25283. @@ -4122,12 +4178,12 @@
  25284. if (!atomic_long_read(&n->nr_slabs))
  25285. continue;
  25286. - spin_lock_irqsave(&n->list_lock, flags);
  25287. + raw_spin_lock_irqsave(&n->list_lock, flags);
  25288. list_for_each_entry(page, &n->partial, lru)
  25289. process_slab(&t, s, page, alloc, map);
  25290. list_for_each_entry(page, &n->full, lru)
  25291. process_slab(&t, s, page, alloc, map);
  25292. - spin_unlock_irqrestore(&n->list_lock, flags);
  25293. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  25294. }
  25295. for (i = 0; i < t.count; i++) {
  25296. diff -Nur linux-4.1.20.orig/mm/swap.c linux-4.1.20/mm/swap.c
  25297. --- linux-4.1.20.orig/mm/swap.c 2016-03-17 19:11:03.000000000 +0100
  25298. +++ linux-4.1.20/mm/swap.c 2016-03-21 20:18:34.000000000 +0100
  25299. @@ -32,6 +32,7 @@
  25300. #include <linux/gfp.h>
  25301. #include <linux/uio.h>
  25302. #include <linux/hugetlb.h>
  25303. +#include <linux/locallock.h>
  25304. #include "internal.h"
  25305. @@ -45,6 +46,9 @@
  25306. static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
  25307. static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
  25308. +static DEFINE_LOCAL_IRQ_LOCK(rotate_lock);
  25309. +DEFINE_LOCAL_IRQ_LOCK(swapvec_lock);
  25310. +
  25311. /*
  25312. * This path almost never happens for VM activity - pages are normally
  25313. * freed via pagevecs. But it gets used by networking.
  25314. @@ -481,11 +485,11 @@
  25315. unsigned long flags;
  25316. page_cache_get(page);
  25317. - local_irq_save(flags);
  25318. + local_lock_irqsave(rotate_lock, flags);
  25319. pvec = this_cpu_ptr(&lru_rotate_pvecs);
  25320. if (!pagevec_add(pvec, page))
  25321. pagevec_move_tail(pvec);
  25322. - local_irq_restore(flags);
  25323. + local_unlock_irqrestore(rotate_lock, flags);
  25324. }
  25325. }
  25326. @@ -536,12 +540,13 @@
  25327. void activate_page(struct page *page)
  25328. {
  25329. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  25330. - struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
  25331. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  25332. + activate_page_pvecs);
  25333. page_cache_get(page);
  25334. if (!pagevec_add(pvec, page))
  25335. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  25336. - put_cpu_var(activate_page_pvecs);
  25337. + put_locked_var(swapvec_lock, activate_page_pvecs);
  25338. }
  25339. }
  25340. @@ -567,7 +572,7 @@
  25341. static void __lru_cache_activate_page(struct page *page)
  25342. {
  25343. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  25344. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  25345. int i;
  25346. /*
  25347. @@ -589,7 +594,7 @@
  25348. }
  25349. }
  25350. - put_cpu_var(lru_add_pvec);
  25351. + put_locked_var(swapvec_lock, lru_add_pvec);
  25352. }
  25353. /*
  25354. @@ -628,13 +633,13 @@
  25355. static void __lru_cache_add(struct page *page)
  25356. {
  25357. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  25358. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  25359. page_cache_get(page);
  25360. if (!pagevec_space(pvec))
  25361. __pagevec_lru_add(pvec);
  25362. pagevec_add(pvec, page);
  25363. - put_cpu_var(lru_add_pvec);
  25364. + put_locked_var(swapvec_lock, lru_add_pvec);
  25365. }
  25366. /**
  25367. @@ -814,9 +819,9 @@
  25368. unsigned long flags;
  25369. /* No harm done if a racing interrupt already did this */
  25370. - local_irq_save(flags);
  25371. + local_lock_irqsave(rotate_lock, flags);
  25372. pagevec_move_tail(pvec);
  25373. - local_irq_restore(flags);
  25374. + local_unlock_irqrestore(rotate_lock, flags);
  25375. }
  25376. pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
  25377. @@ -844,18 +849,19 @@
  25378. return;
  25379. if (likely(get_page_unless_zero(page))) {
  25380. - struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
  25381. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  25382. + lru_deactivate_file_pvecs);
  25383. if (!pagevec_add(pvec, page))
  25384. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  25385. - put_cpu_var(lru_deactivate_file_pvecs);
  25386. + put_locked_var(swapvec_lock, lru_deactivate_file_pvecs);
  25387. }
  25388. }
  25389. void lru_add_drain(void)
  25390. {
  25391. - lru_add_drain_cpu(get_cpu());
  25392. - put_cpu();
  25393. + lru_add_drain_cpu(local_lock_cpu(swapvec_lock));
  25394. + local_unlock_cpu(swapvec_lock);
  25395. }
  25396. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  25397. diff -Nur linux-4.1.20.orig/mm/truncate.c linux-4.1.20/mm/truncate.c
  25398. --- linux-4.1.20.orig/mm/truncate.c 2016-03-17 19:11:03.000000000 +0100
  25399. +++ linux-4.1.20/mm/truncate.c 2016-03-21 20:18:34.000000000 +0100
  25400. @@ -56,8 +56,11 @@
  25401. * protected by mapping->tree_lock.
  25402. */
  25403. if (!workingset_node_shadows(node) &&
  25404. - !list_empty(&node->private_list))
  25405. - list_lru_del(&workingset_shadow_nodes, &node->private_list);
  25406. + !list_empty(&node->private_list)) {
  25407. + local_lock(workingset_shadow_lock);
  25408. + list_lru_del(&__workingset_shadow_nodes, &node->private_list);
  25409. + local_unlock(workingset_shadow_lock);
  25410. + }
  25411. __radix_tree_delete_node(&mapping->page_tree, node);
  25412. unlock:
  25413. spin_unlock_irq(&mapping->tree_lock);
  25414. diff -Nur linux-4.1.20.orig/mm/vmalloc.c linux-4.1.20/mm/vmalloc.c
  25415. --- linux-4.1.20.orig/mm/vmalloc.c 2016-03-17 19:11:03.000000000 +0100
  25416. +++ linux-4.1.20/mm/vmalloc.c 2016-03-21 20:18:34.000000000 +0100
  25417. @@ -819,7 +819,7 @@
  25418. struct vmap_block *vb;
  25419. struct vmap_area *va;
  25420. unsigned long vb_idx;
  25421. - int node, err;
  25422. + int node, err, cpu;
  25423. void *vaddr;
  25424. node = numa_node_id();
  25425. @@ -862,11 +862,12 @@
  25426. BUG_ON(err);
  25427. radix_tree_preload_end();
  25428. - vbq = &get_cpu_var(vmap_block_queue);
  25429. + cpu = get_cpu_light();
  25430. + vbq = this_cpu_ptr(&vmap_block_queue);
  25431. spin_lock(&vbq->lock);
  25432. list_add_tail_rcu(&vb->free_list, &vbq->free);
  25433. spin_unlock(&vbq->lock);
  25434. - put_cpu_var(vmap_block_queue);
  25435. + put_cpu_light();
  25436. return vaddr;
  25437. }
  25438. @@ -935,6 +936,7 @@
  25439. struct vmap_block *vb;
  25440. void *vaddr = NULL;
  25441. unsigned int order;
  25442. + int cpu;
  25443. BUG_ON(size & ~PAGE_MASK);
  25444. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  25445. @@ -949,7 +951,8 @@
  25446. order = get_order(size);
  25447. rcu_read_lock();
  25448. - vbq = &get_cpu_var(vmap_block_queue);
  25449. + cpu = get_cpu_light();
  25450. + vbq = this_cpu_ptr(&vmap_block_queue);
  25451. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  25452. unsigned long pages_off;
  25453. @@ -972,7 +975,7 @@
  25454. break;
  25455. }
  25456. - put_cpu_var(vmap_block_queue);
  25457. + put_cpu_light();
  25458. rcu_read_unlock();
  25459. /* Allocate new block if nothing was found */
  25460. diff -Nur linux-4.1.20.orig/mm/vmstat.c linux-4.1.20/mm/vmstat.c
  25461. --- linux-4.1.20.orig/mm/vmstat.c 2016-03-17 19:11:03.000000000 +0100
  25462. +++ linux-4.1.20/mm/vmstat.c 2016-03-21 20:18:34.000000000 +0100
  25463. @@ -226,6 +226,7 @@
  25464. long x;
  25465. long t;
  25466. + preempt_disable_rt();
  25467. x = delta + __this_cpu_read(*p);
  25468. t = __this_cpu_read(pcp->stat_threshold);
  25469. @@ -235,6 +236,7 @@
  25470. x = 0;
  25471. }
  25472. __this_cpu_write(*p, x);
  25473. + preempt_enable_rt();
  25474. }
  25475. EXPORT_SYMBOL(__mod_zone_page_state);
  25476. @@ -267,6 +269,7 @@
  25477. s8 __percpu *p = pcp->vm_stat_diff + item;
  25478. s8 v, t;
  25479. + preempt_disable_rt();
  25480. v = __this_cpu_inc_return(*p);
  25481. t = __this_cpu_read(pcp->stat_threshold);
  25482. if (unlikely(v > t)) {
  25483. @@ -275,6 +278,7 @@
  25484. zone_page_state_add(v + overstep, zone, item);
  25485. __this_cpu_write(*p, -overstep);
  25486. }
  25487. + preempt_enable_rt();
  25488. }
  25489. void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
  25490. @@ -289,6 +293,7 @@
  25491. s8 __percpu *p = pcp->vm_stat_diff + item;
  25492. s8 v, t;
  25493. + preempt_disable_rt();
  25494. v = __this_cpu_dec_return(*p);
  25495. t = __this_cpu_read(pcp->stat_threshold);
  25496. if (unlikely(v < - t)) {
  25497. @@ -297,6 +302,7 @@
  25498. zone_page_state_add(v - overstep, zone, item);
  25499. __this_cpu_write(*p, overstep);
  25500. }
  25501. + preempt_enable_rt();
  25502. }
  25503. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  25504. diff -Nur linux-4.1.20.orig/mm/workingset.c linux-4.1.20/mm/workingset.c
  25505. --- linux-4.1.20.orig/mm/workingset.c 2016-03-17 19:11:03.000000000 +0100
  25506. +++ linux-4.1.20/mm/workingset.c 2016-03-21 20:18:34.000000000 +0100
  25507. @@ -264,7 +264,8 @@
  25508. * point where they would still be useful.
  25509. */
  25510. -struct list_lru workingset_shadow_nodes;
  25511. +struct list_lru __workingset_shadow_nodes;
  25512. +DEFINE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  25513. static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  25514. struct shrink_control *sc)
  25515. @@ -274,9 +275,9 @@
  25516. unsigned long pages;
  25517. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  25518. - local_irq_disable();
  25519. - shadow_nodes = list_lru_shrink_count(&workingset_shadow_nodes, sc);
  25520. - local_irq_enable();
  25521. + local_lock_irq(workingset_shadow_lock);
  25522. + shadow_nodes = list_lru_shrink_count(&__workingset_shadow_nodes, sc);
  25523. + local_unlock_irq(workingset_shadow_lock);
  25524. pages = node_present_pages(sc->nid);
  25525. /*
  25526. @@ -363,9 +364,9 @@
  25527. spin_unlock(&mapping->tree_lock);
  25528. ret = LRU_REMOVED_RETRY;
  25529. out:
  25530. - local_irq_enable();
  25531. + local_unlock_irq(workingset_shadow_lock);
  25532. cond_resched();
  25533. - local_irq_disable();
  25534. + local_lock_irq(workingset_shadow_lock);
  25535. spin_lock(lru_lock);
  25536. return ret;
  25537. }
  25538. @@ -376,10 +377,10 @@
  25539. unsigned long ret;
  25540. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  25541. - local_irq_disable();
  25542. - ret = list_lru_shrink_walk(&workingset_shadow_nodes, sc,
  25543. + local_lock_irq(workingset_shadow_lock);
  25544. + ret = list_lru_shrink_walk(&__workingset_shadow_nodes, sc,
  25545. shadow_lru_isolate, NULL);
  25546. - local_irq_enable();
  25547. + local_unlock_irq(workingset_shadow_lock);
  25548. return ret;
  25549. }
  25550. @@ -400,7 +401,7 @@
  25551. {
  25552. int ret;
  25553. - ret = list_lru_init_key(&workingset_shadow_nodes, &shadow_nodes_key);
  25554. + ret = list_lru_init_key(&__workingset_shadow_nodes, &shadow_nodes_key);
  25555. if (ret)
  25556. goto err;
  25557. ret = register_shrinker(&workingset_shadow_shrinker);
  25558. @@ -408,7 +409,7 @@
  25559. goto err_list_lru;
  25560. return 0;
  25561. err_list_lru:
  25562. - list_lru_destroy(&workingset_shadow_nodes);
  25563. + list_lru_destroy(&__workingset_shadow_nodes);
  25564. err:
  25565. return ret;
  25566. }
  25567. diff -Nur linux-4.1.20.orig/net/core/dev.c linux-4.1.20/net/core/dev.c
  25568. --- linux-4.1.20.orig/net/core/dev.c 2016-03-17 19:11:03.000000000 +0100
  25569. +++ linux-4.1.20/net/core/dev.c 2016-03-21 20:18:34.000000000 +0100
  25570. @@ -184,6 +184,7 @@
  25571. static DEFINE_HASHTABLE(napi_hash, 8);
  25572. static seqcount_t devnet_rename_seq;
  25573. +static DEFINE_MUTEX(devnet_rename_mutex);
  25574. static inline void dev_base_seq_inc(struct net *net)
  25575. {
  25576. @@ -205,14 +206,14 @@
  25577. static inline void rps_lock(struct softnet_data *sd)
  25578. {
  25579. #ifdef CONFIG_RPS
  25580. - spin_lock(&sd->input_pkt_queue.lock);
  25581. + raw_spin_lock(&sd->input_pkt_queue.raw_lock);
  25582. #endif
  25583. }
  25584. static inline void rps_unlock(struct softnet_data *sd)
  25585. {
  25586. #ifdef CONFIG_RPS
  25587. - spin_unlock(&sd->input_pkt_queue.lock);
  25588. + raw_spin_unlock(&sd->input_pkt_queue.raw_lock);
  25589. #endif
  25590. }
  25591. @@ -852,7 +853,8 @@
  25592. strcpy(name, dev->name);
  25593. rcu_read_unlock();
  25594. if (read_seqcount_retry(&devnet_rename_seq, seq)) {
  25595. - cond_resched();
  25596. + mutex_lock(&devnet_rename_mutex);
  25597. + mutex_unlock(&devnet_rename_mutex);
  25598. goto retry;
  25599. }
  25600. @@ -1121,20 +1123,17 @@
  25601. if (dev->flags & IFF_UP)
  25602. return -EBUSY;
  25603. - write_seqcount_begin(&devnet_rename_seq);
  25604. + mutex_lock(&devnet_rename_mutex);
  25605. + __raw_write_seqcount_begin(&devnet_rename_seq);
  25606. - if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
  25607. - write_seqcount_end(&devnet_rename_seq);
  25608. - return 0;
  25609. - }
  25610. + if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
  25611. + goto outunlock;
  25612. memcpy(oldname, dev->name, IFNAMSIZ);
  25613. err = dev_get_valid_name(net, dev, newname);
  25614. - if (err < 0) {
  25615. - write_seqcount_end(&devnet_rename_seq);
  25616. - return err;
  25617. - }
  25618. + if (err < 0)
  25619. + goto outunlock;
  25620. if (oldname[0] && !strchr(oldname, '%'))
  25621. netdev_info(dev, "renamed from %s\n", oldname);
  25622. @@ -1147,11 +1146,12 @@
  25623. if (ret) {
  25624. memcpy(dev->name, oldname, IFNAMSIZ);
  25625. dev->name_assign_type = old_assign_type;
  25626. - write_seqcount_end(&devnet_rename_seq);
  25627. - return ret;
  25628. + err = ret;
  25629. + goto outunlock;
  25630. }
  25631. - write_seqcount_end(&devnet_rename_seq);
  25632. + __raw_write_seqcount_end(&devnet_rename_seq);
  25633. + mutex_unlock(&devnet_rename_mutex);
  25634. netdev_adjacent_rename_links(dev, oldname);
  25635. @@ -1172,7 +1172,8 @@
  25636. /* err >= 0 after dev_alloc_name() or stores the first errno */
  25637. if (err >= 0) {
  25638. err = ret;
  25639. - write_seqcount_begin(&devnet_rename_seq);
  25640. + mutex_lock(&devnet_rename_mutex);
  25641. + __raw_write_seqcount_begin(&devnet_rename_seq);
  25642. memcpy(dev->name, oldname, IFNAMSIZ);
  25643. memcpy(oldname, newname, IFNAMSIZ);
  25644. dev->name_assign_type = old_assign_type;
  25645. @@ -1185,6 +1186,11 @@
  25646. }
  25647. return err;
  25648. +
  25649. +outunlock:
  25650. + __raw_write_seqcount_end(&devnet_rename_seq);
  25651. + mutex_unlock(&devnet_rename_mutex);
  25652. + return err;
  25653. }
  25654. /**
  25655. @@ -2214,6 +2220,7 @@
  25656. sd->output_queue_tailp = &q->next_sched;
  25657. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  25658. local_irq_restore(flags);
  25659. + preempt_check_resched_rt();
  25660. }
  25661. void __netif_schedule(struct Qdisc *q)
  25662. @@ -2295,6 +2302,7 @@
  25663. __this_cpu_write(softnet_data.completion_queue, skb);
  25664. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  25665. local_irq_restore(flags);
  25666. + preempt_check_resched_rt();
  25667. }
  25668. EXPORT_SYMBOL(__dev_kfree_skb_irq);
  25669. @@ -2880,9 +2888,44 @@
  25670. #define skb_update_prio(skb)
  25671. #endif
  25672. +#ifdef CONFIG_PREEMPT_RT_FULL
  25673. +
  25674. +static inline int xmit_rec_read(void)
  25675. +{
  25676. + return current->xmit_recursion;
  25677. +}
  25678. +
  25679. +static inline void xmit_rec_inc(void)
  25680. +{
  25681. + current->xmit_recursion++;
  25682. +}
  25683. +
  25684. +static inline void xmit_rec_dec(void)
  25685. +{
  25686. + current->xmit_recursion--;
  25687. +}
  25688. +
  25689. +#else
  25690. +
  25691. DEFINE_PER_CPU(int, xmit_recursion);
  25692. EXPORT_SYMBOL(xmit_recursion);
  25693. +static inline int xmit_rec_read(void)
  25694. +{
  25695. + return __this_cpu_read(xmit_recursion);
  25696. +}
  25697. +
  25698. +static inline void xmit_rec_inc(void)
  25699. +{
  25700. + __this_cpu_inc(xmit_recursion);
  25701. +}
  25702. +
  25703. +static inline int xmit_rec_dec(void)
  25704. +{
  25705. + __this_cpu_dec(xmit_recursion);
  25706. +}
  25707. +#endif
  25708. +
  25709. #define RECURSION_LIMIT 10
  25710. /**
  25711. @@ -2984,7 +3027,7 @@
  25712. if (txq->xmit_lock_owner != cpu) {
  25713. - if (__this_cpu_read(xmit_recursion) > RECURSION_LIMIT)
  25714. + if (xmit_rec_read() > RECURSION_LIMIT)
  25715. goto recursion_alert;
  25716. skb = validate_xmit_skb(skb, dev);
  25717. @@ -2994,9 +3037,9 @@
  25718. HARD_TX_LOCK(dev, txq, cpu);
  25719. if (!netif_xmit_stopped(txq)) {
  25720. - __this_cpu_inc(xmit_recursion);
  25721. + xmit_rec_inc();
  25722. skb = dev_hard_start_xmit(skb, dev, txq, &rc);
  25723. - __this_cpu_dec(xmit_recursion);
  25724. + xmit_rec_dec();
  25725. if (dev_xmit_complete(rc)) {
  25726. HARD_TX_UNLOCK(dev, txq);
  25727. goto out;
  25728. @@ -3370,6 +3413,7 @@
  25729. rps_unlock(sd);
  25730. local_irq_restore(flags);
  25731. + preempt_check_resched_rt();
  25732. atomic_long_inc(&skb->dev->rx_dropped);
  25733. kfree_skb(skb);
  25734. @@ -3388,7 +3432,7 @@
  25735. struct rps_dev_flow voidflow, *rflow = &voidflow;
  25736. int cpu;
  25737. - preempt_disable();
  25738. + migrate_disable();
  25739. rcu_read_lock();
  25740. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  25741. @@ -3398,13 +3442,13 @@
  25742. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  25743. rcu_read_unlock();
  25744. - preempt_enable();
  25745. + migrate_enable();
  25746. } else
  25747. #endif
  25748. {
  25749. unsigned int qtail;
  25750. - ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
  25751. - put_cpu();
  25752. + ret = enqueue_to_backlog(skb, get_cpu_light(), &qtail);
  25753. + put_cpu_light();
  25754. }
  25755. return ret;
  25756. }
  25757. @@ -3438,16 +3482,44 @@
  25758. trace_netif_rx_ni_entry(skb);
  25759. - preempt_disable();
  25760. + local_bh_disable();
  25761. err = netif_rx_internal(skb);
  25762. - if (local_softirq_pending())
  25763. - do_softirq();
  25764. - preempt_enable();
  25765. + local_bh_enable();
  25766. return err;
  25767. }
  25768. EXPORT_SYMBOL(netif_rx_ni);
  25769. +#ifdef CONFIG_PREEMPT_RT_FULL
  25770. +/*
  25771. + * RT runs ksoftirqd as a real time thread and the root_lock is a
  25772. + * "sleeping spinlock". If the trylock fails then we can go into an
  25773. + * infinite loop when ksoftirqd preempted the task which actually
  25774. + * holds the lock, because we requeue q and raise NET_TX softirq
  25775. + * causing ksoftirqd to loop forever.
  25776. + *
  25777. + * It's safe to use spin_lock on RT here as softirqs run in thread
  25778. + * context and cannot deadlock against the thread which is holding
  25779. + * root_lock.
  25780. + *
  25781. + * On !RT the trylock might fail, but there we bail out from the
  25782. + * softirq loop after 10 attempts which we can't do on RT. And the
  25783. + * task holding root_lock cannot be preempted, so the only downside of
  25784. + * that trylock is that we need 10 loops to decide that we should have
  25785. + * given up in the first one :)
  25786. + */
  25787. +static inline int take_root_lock(spinlock_t *lock)
  25788. +{
  25789. + spin_lock(lock);
  25790. + return 1;
  25791. +}
  25792. +#else
  25793. +static inline int take_root_lock(spinlock_t *lock)
  25794. +{
  25795. + return spin_trylock(lock);
  25796. +}
  25797. +#endif
  25798. +
  25799. static void net_tx_action(struct softirq_action *h)
  25800. {
  25801. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  25802. @@ -3489,7 +3561,7 @@
  25803. head = head->next_sched;
  25804. root_lock = qdisc_lock(q);
  25805. - if (spin_trylock(root_lock)) {
  25806. + if (take_root_lock(root_lock)) {
  25807. smp_mb__before_atomic();
  25808. clear_bit(__QDISC_STATE_SCHED,
  25809. &q->state);
  25810. @@ -3886,7 +3958,7 @@
  25811. skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
  25812. if (skb->dev == dev) {
  25813. __skb_unlink(skb, &sd->input_pkt_queue);
  25814. - kfree_skb(skb);
  25815. + __skb_queue_tail(&sd->tofree_queue, skb);
  25816. input_queue_head_incr(sd);
  25817. }
  25818. }
  25819. @@ -3895,10 +3967,13 @@
  25820. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  25821. if (skb->dev == dev) {
  25822. __skb_unlink(skb, &sd->process_queue);
  25823. - kfree_skb(skb);
  25824. + __skb_queue_tail(&sd->tofree_queue, skb);
  25825. input_queue_head_incr(sd);
  25826. }
  25827. }
  25828. +
  25829. + if (!skb_queue_empty(&sd->tofree_queue))
  25830. + raise_softirq_irqoff(NET_RX_SOFTIRQ);
  25831. }
  25832. static int napi_gro_complete(struct sk_buff *skb)
  25833. @@ -4349,6 +4424,7 @@
  25834. sd->rps_ipi_list = NULL;
  25835. local_irq_enable();
  25836. + preempt_check_resched_rt();
  25837. /* Send pending IPI's to kick RPS processing on remote cpus. */
  25838. while (remsd) {
  25839. @@ -4362,6 +4438,7 @@
  25840. } else
  25841. #endif
  25842. local_irq_enable();
  25843. + preempt_check_resched_rt();
  25844. }
  25845. static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
  25846. @@ -4443,6 +4520,7 @@
  25847. local_irq_save(flags);
  25848. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  25849. local_irq_restore(flags);
  25850. + preempt_check_resched_rt();
  25851. }
  25852. EXPORT_SYMBOL(__napi_schedule);
  25853. @@ -4717,7 +4795,7 @@
  25854. list_splice_tail(&repoll, &list);
  25855. list_splice(&list, &sd->poll_list);
  25856. if (!list_empty(&sd->poll_list))
  25857. - __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  25858. + __raise_softirq_irqoff_ksoft(NET_RX_SOFTIRQ);
  25859. net_rps_action_and_irq_enable(sd);
  25860. }
  25861. @@ -6931,7 +7009,7 @@
  25862. void synchronize_net(void)
  25863. {
  25864. might_sleep();
  25865. - if (rtnl_is_locked())
  25866. + if (rtnl_is_locked() && !IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  25867. synchronize_rcu_expedited();
  25868. else
  25869. synchronize_rcu();
  25870. @@ -7172,16 +7250,20 @@
  25871. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  25872. local_irq_enable();
  25873. + preempt_check_resched_rt();
  25874. /* Process offline CPU's input_pkt_queue */
  25875. while ((skb = __skb_dequeue(&oldsd->process_queue))) {
  25876. netif_rx_ni(skb);
  25877. input_queue_head_incr(oldsd);
  25878. }
  25879. - while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
  25880. + while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
  25881. netif_rx_ni(skb);
  25882. input_queue_head_incr(oldsd);
  25883. }
  25884. + while ((skb = __skb_dequeue(&oldsd->tofree_queue))) {
  25885. + kfree_skb(skb);
  25886. + }
  25887. return NOTIFY_OK;
  25888. }
  25889. @@ -7483,8 +7565,9 @@
  25890. for_each_possible_cpu(i) {
  25891. struct softnet_data *sd = &per_cpu(softnet_data, i);
  25892. - skb_queue_head_init(&sd->input_pkt_queue);
  25893. - skb_queue_head_init(&sd->process_queue);
  25894. + skb_queue_head_init_raw(&sd->input_pkt_queue);
  25895. + skb_queue_head_init_raw(&sd->process_queue);
  25896. + skb_queue_head_init_raw(&sd->tofree_queue);
  25897. INIT_LIST_HEAD(&sd->poll_list);
  25898. sd->output_queue_tailp = &sd->output_queue;
  25899. #ifdef CONFIG_RPS
  25900. diff -Nur linux-4.1.20.orig/net/core/skbuff.c linux-4.1.20/net/core/skbuff.c
  25901. --- linux-4.1.20.orig/net/core/skbuff.c 2016-03-17 19:11:03.000000000 +0100
  25902. +++ linux-4.1.20/net/core/skbuff.c 2016-03-21 20:18:34.000000000 +0100
  25903. @@ -63,6 +63,7 @@
  25904. #include <linux/errqueue.h>
  25905. #include <linux/prefetch.h>
  25906. #include <linux/if_vlan.h>
  25907. +#include <linux/locallock.h>
  25908. #include <net/protocol.h>
  25909. #include <net/dst.h>
  25910. @@ -358,6 +359,8 @@
  25911. };
  25912. static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
  25913. static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache);
  25914. +static DEFINE_LOCAL_IRQ_LOCK(netdev_alloc_lock);
  25915. +static DEFINE_LOCAL_IRQ_LOCK(napi_alloc_cache_lock);
  25916. static struct page *__page_frag_refill(struct netdev_alloc_cache *nc,
  25917. gfp_t gfp_mask)
  25918. @@ -435,9 +438,9 @@
  25919. unsigned long flags;
  25920. void *data;
  25921. - local_irq_save(flags);
  25922. + local_lock_irqsave(netdev_alloc_lock, flags);
  25923. data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask);
  25924. - local_irq_restore(flags);
  25925. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  25926. return data;
  25927. }
  25928. @@ -456,7 +459,12 @@
  25929. static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  25930. {
  25931. - return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask);
  25932. + void *data;
  25933. +
  25934. + local_lock(napi_alloc_cache_lock);
  25935. + data = __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask);
  25936. + local_unlock(napi_alloc_cache_lock);
  25937. + return data;
  25938. }
  25939. void *napi_alloc_frag(unsigned int fragsz)
  25940. diff -Nur linux-4.1.20.orig/net/core/sock.c linux-4.1.20/net/core/sock.c
  25941. --- linux-4.1.20.orig/net/core/sock.c 2016-03-17 19:11:03.000000000 +0100
  25942. +++ linux-4.1.20/net/core/sock.c 2016-03-21 20:18:34.000000000 +0100
  25943. @@ -2369,12 +2369,11 @@
  25944. if (sk->sk_lock.owned)
  25945. __lock_sock(sk);
  25946. sk->sk_lock.owned = 1;
  25947. - spin_unlock(&sk->sk_lock.slock);
  25948. + spin_unlock_bh(&sk->sk_lock.slock);
  25949. /*
  25950. * The sk_lock has mutex_lock() semantics here:
  25951. */
  25952. mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
  25953. - local_bh_enable();
  25954. }
  25955. EXPORT_SYMBOL(lock_sock_nested);
  25956. diff -Nur linux-4.1.20.orig/net/ipv4/icmp.c linux-4.1.20/net/ipv4/icmp.c
  25957. --- linux-4.1.20.orig/net/ipv4/icmp.c 2016-03-17 19:11:03.000000000 +0100
  25958. +++ linux-4.1.20/net/ipv4/icmp.c 2016-03-21 20:18:34.000000000 +0100
  25959. @@ -69,6 +69,7 @@
  25960. #include <linux/jiffies.h>
  25961. #include <linux/kernel.h>
  25962. #include <linux/fcntl.h>
  25963. +#include <linux/sysrq.h>
  25964. #include <linux/socket.h>
  25965. #include <linux/in.h>
  25966. #include <linux/inet.h>
  25967. @@ -867,6 +868,30 @@
  25968. }
  25969. /*
  25970. + * 32bit and 64bit have different timestamp length, so we check for
  25971. + * the cookie at offset 20 and verify it is repeated at offset 50
  25972. + */
  25973. +#define CO_POS0 20
  25974. +#define CO_POS1 50
  25975. +#define CO_SIZE sizeof(int)
  25976. +#define ICMP_SYSRQ_SIZE 57
  25977. +
  25978. +/*
  25979. + * We got a ICMP_SYSRQ_SIZE sized ping request. Check for the cookie
  25980. + * pattern and if it matches send the next byte as a trigger to sysrq.
  25981. + */
  25982. +static void icmp_check_sysrq(struct net *net, struct sk_buff *skb)
  25983. +{
  25984. + int cookie = htonl(net->ipv4.sysctl_icmp_echo_sysrq);
  25985. + char *p = skb->data;
  25986. +
  25987. + if (!memcmp(&cookie, p + CO_POS0, CO_SIZE) &&
  25988. + !memcmp(&cookie, p + CO_POS1, CO_SIZE) &&
  25989. + p[CO_POS0 + CO_SIZE] == p[CO_POS1 + CO_SIZE])
  25990. + handle_sysrq(p[CO_POS0 + CO_SIZE]);
  25991. +}
  25992. +
  25993. +/*
  25994. * Handle ICMP_ECHO ("ping") requests.
  25995. *
  25996. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  25997. @@ -893,6 +918,11 @@
  25998. icmp_param.data_len = skb->len;
  25999. icmp_param.head_len = sizeof(struct icmphdr);
  26000. icmp_reply(&icmp_param, skb);
  26001. +
  26002. + if (skb->len == ICMP_SYSRQ_SIZE &&
  26003. + net->ipv4.sysctl_icmp_echo_sysrq) {
  26004. + icmp_check_sysrq(net, skb);
  26005. + }
  26006. }
  26007. /* should there be an ICMP stat for ignored echos? */
  26008. return true;
  26009. diff -Nur linux-4.1.20.orig/net/ipv4/sysctl_net_ipv4.c linux-4.1.20/net/ipv4/sysctl_net_ipv4.c
  26010. --- linux-4.1.20.orig/net/ipv4/sysctl_net_ipv4.c 2016-03-17 19:11:03.000000000 +0100
  26011. +++ linux-4.1.20/net/ipv4/sysctl_net_ipv4.c 2016-03-21 20:18:34.000000000 +0100
  26012. @@ -779,6 +779,13 @@
  26013. .proc_handler = proc_dointvec
  26014. },
  26015. {
  26016. + .procname = "icmp_echo_sysrq",
  26017. + .data = &init_net.ipv4.sysctl_icmp_echo_sysrq,
  26018. + .maxlen = sizeof(int),
  26019. + .mode = 0644,
  26020. + .proc_handler = proc_dointvec
  26021. + },
  26022. + {
  26023. .procname = "icmp_ignore_bogus_error_responses",
  26024. .data = &init_net.ipv4.sysctl_icmp_ignore_bogus_error_responses,
  26025. .maxlen = sizeof(int),
  26026. diff -Nur linux-4.1.20.orig/net/mac80211/rx.c linux-4.1.20/net/mac80211/rx.c
  26027. --- linux-4.1.20.orig/net/mac80211/rx.c 2016-03-17 19:11:03.000000000 +0100
  26028. +++ linux-4.1.20/net/mac80211/rx.c 2016-03-21 20:18:34.000000000 +0100
  26029. @@ -3554,7 +3554,7 @@
  26030. struct ieee80211_supported_band *sband;
  26031. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  26032. - WARN_ON_ONCE(softirq_count() == 0);
  26033. + WARN_ON_ONCE_NONRT(softirq_count() == 0);
  26034. if (WARN_ON(status->band >= IEEE80211_NUM_BANDS))
  26035. goto drop;
  26036. diff -Nur linux-4.1.20.orig/net/netfilter/core.c linux-4.1.20/net/netfilter/core.c
  26037. --- linux-4.1.20.orig/net/netfilter/core.c 2016-03-17 19:11:03.000000000 +0100
  26038. +++ linux-4.1.20/net/netfilter/core.c 2016-03-21 20:18:34.000000000 +0100
  26039. @@ -22,11 +22,17 @@
  26040. #include <linux/proc_fs.h>
  26041. #include <linux/mutex.h>
  26042. #include <linux/slab.h>
  26043. +#include <linux/locallock.h>
  26044. #include <net/net_namespace.h>
  26045. #include <net/sock.h>
  26046. #include "nf_internals.h"
  26047. +#ifdef CONFIG_PREEMPT_RT_BASE
  26048. +DEFINE_LOCAL_IRQ_LOCK(xt_write_lock);
  26049. +EXPORT_PER_CPU_SYMBOL(xt_write_lock);
  26050. +#endif
  26051. +
  26052. static DEFINE_MUTEX(afinfo_mutex);
  26053. const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
  26054. diff -Nur linux-4.1.20.orig/net/packet/af_packet.c linux-4.1.20/net/packet/af_packet.c
  26055. --- linux-4.1.20.orig/net/packet/af_packet.c 2016-03-17 19:11:03.000000000 +0100
  26056. +++ linux-4.1.20/net/packet/af_packet.c 2016-03-21 20:18:34.000000000 +0100
  26057. @@ -63,6 +63,7 @@
  26058. #include <linux/if_packet.h>
  26059. #include <linux/wireless.h>
  26060. #include <linux/kernel.h>
  26061. +#include <linux/delay.h>
  26062. #include <linux/kmod.h>
  26063. #include <linux/slab.h>
  26064. #include <linux/vmalloc.h>
  26065. @@ -698,7 +699,7 @@
  26066. if (BLOCK_NUM_PKTS(pbd)) {
  26067. while (atomic_read(&pkc->blk_fill_in_prog)) {
  26068. /* Waiting for skb_copy_bits to finish... */
  26069. - cpu_relax();
  26070. + cpu_chill();
  26071. }
  26072. }
  26073. @@ -960,7 +961,7 @@
  26074. if (!(status & TP_STATUS_BLK_TMO)) {
  26075. while (atomic_read(&pkc->blk_fill_in_prog)) {
  26076. /* Waiting for skb_copy_bits to finish... */
  26077. - cpu_relax();
  26078. + cpu_chill();
  26079. }
  26080. }
  26081. prb_close_block(pkc, pbd, po, status);
  26082. diff -Nur linux-4.1.20.orig/net/rds/ib_rdma.c linux-4.1.20/net/rds/ib_rdma.c
  26083. --- linux-4.1.20.orig/net/rds/ib_rdma.c 2016-03-17 19:11:03.000000000 +0100
  26084. +++ linux-4.1.20/net/rds/ib_rdma.c 2016-03-21 20:18:34.000000000 +0100
  26085. @@ -34,6 +34,7 @@
  26086. #include <linux/slab.h>
  26087. #include <linux/rculist.h>
  26088. #include <linux/llist.h>
  26089. +#include <linux/delay.h>
  26090. #include "rds.h"
  26091. #include "ib.h"
  26092. @@ -286,7 +287,7 @@
  26093. for_each_online_cpu(cpu) {
  26094. flag = &per_cpu(clean_list_grace, cpu);
  26095. while (test_bit(CLEAN_LIST_BUSY_BIT, flag))
  26096. - cpu_relax();
  26097. + cpu_chill();
  26098. }
  26099. }
  26100. diff -Nur linux-4.1.20.orig/net/sched/sch_generic.c linux-4.1.20/net/sched/sch_generic.c
  26101. --- linux-4.1.20.orig/net/sched/sch_generic.c 2016-03-17 19:11:03.000000000 +0100
  26102. +++ linux-4.1.20/net/sched/sch_generic.c 2016-03-21 20:18:34.000000000 +0100
  26103. @@ -896,7 +896,7 @@
  26104. /* Wait for outstanding qdisc_run calls. */
  26105. list_for_each_entry(dev, head, close_list)
  26106. while (some_qdisc_is_busy(dev))
  26107. - yield();
  26108. + msleep(1);
  26109. }
  26110. void dev_deactivate(struct net_device *dev)
  26111. diff -Nur linux-4.1.20.orig/net/sunrpc/svc_xprt.c linux-4.1.20/net/sunrpc/svc_xprt.c
  26112. --- linux-4.1.20.orig/net/sunrpc/svc_xprt.c 2016-03-17 19:11:03.000000000 +0100
  26113. +++ linux-4.1.20/net/sunrpc/svc_xprt.c 2016-03-21 20:18:34.000000000 +0100
  26114. @@ -341,7 +341,7 @@
  26115. goto out;
  26116. }
  26117. - cpu = get_cpu();
  26118. + cpu = get_cpu_light();
  26119. pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
  26120. atomic_long_inc(&pool->sp_stats.packets);
  26121. @@ -377,7 +377,7 @@
  26122. atomic_long_inc(&pool->sp_stats.threads_woken);
  26123. wake_up_process(rqstp->rq_task);
  26124. - put_cpu();
  26125. + put_cpu_light();
  26126. goto out;
  26127. }
  26128. rcu_read_unlock();
  26129. @@ -398,7 +398,7 @@
  26130. goto redo_search;
  26131. }
  26132. rqstp = NULL;
  26133. - put_cpu();
  26134. + put_cpu_light();
  26135. out:
  26136. trace_svc_xprt_do_enqueue(xprt, rqstp);
  26137. }
  26138. diff -Nur linux-4.1.20.orig/scripts/mkcompile_h linux-4.1.20/scripts/mkcompile_h
  26139. --- linux-4.1.20.orig/scripts/mkcompile_h 2016-03-17 19:11:03.000000000 +0100
  26140. +++ linux-4.1.20/scripts/mkcompile_h 2016-03-21 20:18:34.000000000 +0100
  26141. @@ -4,7 +4,8 @@
  26142. ARCH=$2
  26143. SMP=$3
  26144. PREEMPT=$4
  26145. -CC=$5
  26146. +RT=$5
  26147. +CC=$6
  26148. vecho() { [ "${quiet}" = "silent_" ] || echo "$@" ; }
  26149. @@ -57,6 +58,7 @@
  26150. CONFIG_FLAGS=""
  26151. if [ -n "$SMP" ] ; then CONFIG_FLAGS="SMP"; fi
  26152. if [ -n "$PREEMPT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS PREEMPT"; fi
  26153. +if [ -n "$RT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS RT"; fi
  26154. UTS_VERSION="$UTS_VERSION $CONFIG_FLAGS $TIMESTAMP"
  26155. # Truncate to maximum length
  26156. diff -Nur linux-4.1.20.orig/sound/core/pcm_native.c linux-4.1.20/sound/core/pcm_native.c
  26157. --- linux-4.1.20.orig/sound/core/pcm_native.c 2016-03-17 19:11:03.000000000 +0100
  26158. +++ linux-4.1.20/sound/core/pcm_native.c 2016-03-21 20:18:34.000000000 +0100
  26159. @@ -135,7 +135,7 @@
  26160. void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
  26161. {
  26162. if (!substream->pcm->nonatomic)
  26163. - local_irq_disable();
  26164. + local_irq_disable_nort();
  26165. snd_pcm_stream_lock(substream);
  26166. }
  26167. EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
  26168. @@ -150,7 +150,7 @@
  26169. {
  26170. snd_pcm_stream_unlock(substream);
  26171. if (!substream->pcm->nonatomic)
  26172. - local_irq_enable();
  26173. + local_irq_enable_nort();
  26174. }
  26175. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
  26176. @@ -158,7 +158,7 @@
  26177. {
  26178. unsigned long flags = 0;
  26179. if (!substream->pcm->nonatomic)
  26180. - local_irq_save(flags);
  26181. + local_irq_save_nort(flags);
  26182. snd_pcm_stream_lock(substream);
  26183. return flags;
  26184. }
  26185. @@ -176,7 +176,7 @@
  26186. {
  26187. snd_pcm_stream_unlock(substream);
  26188. if (!substream->pcm->nonatomic)
  26189. - local_irq_restore(flags);
  26190. + local_irq_restore_nort(flags);
  26191. }
  26192. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
  26193. diff -Nur linux-4.1.20.orig/sound/soc/intel/atom/sst/sst.c linux-4.1.20/sound/soc/intel/atom/sst/sst.c
  26194. --- linux-4.1.20.orig/sound/soc/intel/atom/sst/sst.c 2016-03-17 19:11:03.000000000 +0100
  26195. +++ linux-4.1.20/sound/soc/intel/atom/sst/sst.c 2016-03-21 20:18:34.000000000 +0100
  26196. @@ -368,8 +368,8 @@
  26197. * initialize by FW or driver when firmware is loaded
  26198. */
  26199. spin_lock_irqsave(&ctx->ipc_spin_lock, irq_flags);
  26200. - sst_shim_write64(shim, SST_IMRX, shim_regs->imrx),
  26201. - sst_shim_write64(shim, SST_CSR, shim_regs->csr),
  26202. + sst_shim_write64(shim, SST_IMRX, shim_regs->imrx);
  26203. + sst_shim_write64(shim, SST_CSR, shim_regs->csr);
  26204. spin_unlock_irqrestore(&ctx->ipc_spin_lock, irq_flags);
  26205. }
  26206. diff -Nur linux-4.1.20.orig/virt/kvm/async_pf.c linux-4.1.20/virt/kvm/async_pf.c
  26207. --- linux-4.1.20.orig/virt/kvm/async_pf.c 2016-03-17 19:11:03.000000000 +0100
  26208. +++ linux-4.1.20/virt/kvm/async_pf.c 2016-03-21 20:18:34.000000000 +0100
  26209. @@ -94,8 +94,8 @@
  26210. trace_kvm_async_pf_completed(addr, gva);
  26211. - if (waitqueue_active(&vcpu->wq))
  26212. - wake_up_interruptible(&vcpu->wq);
  26213. + if (swaitqueue_active(&vcpu->wq))
  26214. + swait_wake_interruptible(&vcpu->wq);
  26215. mmput(mm);
  26216. kvm_put_kvm(vcpu->kvm);
  26217. diff -Nur linux-4.1.20.orig/virt/kvm/kvm_main.c linux-4.1.20/virt/kvm/kvm_main.c
  26218. --- linux-4.1.20.orig/virt/kvm/kvm_main.c 2016-03-17 19:11:03.000000000 +0100
  26219. +++ linux-4.1.20/virt/kvm/kvm_main.c 2016-03-21 20:18:34.000000000 +0100
  26220. @@ -218,7 +218,7 @@
  26221. vcpu->kvm = kvm;
  26222. vcpu->vcpu_id = id;
  26223. vcpu->pid = NULL;
  26224. - init_waitqueue_head(&vcpu->wq);
  26225. + init_swait_head(&vcpu->wq);
  26226. kvm_async_pf_vcpu_init(vcpu);
  26227. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  26228. @@ -1779,7 +1779,7 @@
  26229. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  26230. {
  26231. ktime_t start, cur;
  26232. - DEFINE_WAIT(wait);
  26233. + DEFINE_SWAITER(wait);
  26234. bool waited = false;
  26235. start = cur = ktime_get();
  26236. @@ -1800,7 +1800,7 @@
  26237. }
  26238. for (;;) {
  26239. - prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  26240. + swait_prepare(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  26241. if (kvm_vcpu_check_block(vcpu) < 0)
  26242. break;
  26243. @@ -1809,7 +1809,7 @@
  26244. schedule();
  26245. }
  26246. - finish_wait(&vcpu->wq, &wait);
  26247. + swait_finish(&vcpu->wq, &wait);
  26248. cur = ktime_get();
  26249. out:
  26250. @@ -1825,11 +1825,11 @@
  26251. {
  26252. int me;
  26253. int cpu = vcpu->cpu;
  26254. - wait_queue_head_t *wqp;
  26255. + struct swait_head *wqp;
  26256. wqp = kvm_arch_vcpu_wq(vcpu);
  26257. - if (waitqueue_active(wqp)) {
  26258. - wake_up_interruptible(wqp);
  26259. + if (swaitqueue_active(wqp)) {
  26260. + swait_wake_interruptible(wqp);
  26261. ++vcpu->stat.halt_wakeup;
  26262. }
  26263. @@ -1930,7 +1930,7 @@
  26264. continue;
  26265. if (vcpu == me)
  26266. continue;
  26267. - if (waitqueue_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  26268. + if (swaitqueue_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  26269. continue;
  26270. if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
  26271. continue;